0N/A/*
3909N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.crypto;
0N/A
0N/Aimport java.security.AlgorithmParameters;
0N/Aimport java.security.Provider;
0N/Aimport java.security.Key;
0N/Aimport java.security.Security;
0N/Aimport java.security.NoSuchAlgorithmException;
0N/Aimport java.security.NoSuchProviderException;
0N/Aimport java.security.InvalidKeyException;
0N/Aimport java.security.InvalidAlgorithmParameterException;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/A
0N/Aimport sun.security.jca.GetInstance.Instance;
0N/A
0N/A/**
0N/A * This class provides the functionality of an exemption mechanism, examples
0N/A * of which are <i>key recovery</i>, <i>key weakening</i>, and
0N/A * <i>key escrow</i>.
0N/A *
0N/A * <p>Applications or applets that use an exemption mechanism may be granted
0N/A * stronger encryption capabilities than those which don't.
0N/A *
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic class ExemptionMechanism {
0N/A
0N/A // The provider
0N/A private Provider provider;
0N/A
0N/A // The provider implementation (delegate)
0N/A private ExemptionMechanismSpi exmechSpi;
0N/A
0N/A // The name of the exemption mechanism.
0N/A private String mechanism;
0N/A
0N/A // Flag which indicates whether this ExemptionMechanism
0N/A // result is generated successfully.
0N/A private boolean done = false;
0N/A
0N/A // State information
0N/A private boolean initialized = false;
0N/A
0N/A // Store away the key at init() time for later comparison.
0N/A private Key keyStored = null;
0N/A
0N/A /**
0N/A * Creates a ExemptionMechanism object.
0N/A *
0N/A * @param exmechSpi the delegate
0N/A * @param provider the provider
0N/A * @param mechanism the exemption mechanism
0N/A */
0N/A protected ExemptionMechanism(ExemptionMechanismSpi exmechSpi,
0N/A Provider provider,
0N/A String mechanism) {
0N/A this.exmechSpi = exmechSpi;
0N/A this.provider = provider;
0N/A this.mechanism = mechanism;
0N/A }
0N/A
0N/A /**
0N/A * Returns the exemption mechanism name of this
0N/A * <code>ExemptionMechanism</code> object.
0N/A *
0N/A * <p>This is the same name that was specified in one of the
0N/A * <code>getInstance</code> calls that created this
0N/A * <code>ExemptionMechanism</code> object.
0N/A *
0N/A * @return the exemption mechanism name of this
0N/A * <code>ExemptionMechanism</code> object.
0N/A */
0N/A public final String getName() {
0N/A return this.mechanism;
0N/A }
0N/A
0N/A /**
0N/A * Returns an <code>ExemptionMechanism</code> object that implements the
0N/A * specified exemption mechanism algorithm.
0N/A *
0N/A * <p> This method traverses the list of registered security Providers,
0N/A * starting with the most preferred Provider.
0N/A * A new ExemptionMechanism object encapsulating the
0N/A * ExemptionMechanismSpi implementation from the first
0N/A * Provider that supports the specified algorithm is returned.
0N/A *
0N/A * <p> Note that the list of registered providers may be retrieved via
0N/A * the {@link Security#getProviders() Security.getProviders()} method.
0N/A *
0N/A * @param algorithm the standard name of the requested exemption
0N/A * mechanism.
3465N/A * See the ExemptionMechanism section in the
0N/A * <a href=
3465N/A * "{docRoot}/../technotes/guides/security/StandardNames.html#Exemption">
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
0N/A * for information about standard exemption mechanism names.
0N/A *
0N/A * @return the new <code>ExemptionMechanism</code> object.
0N/A *
0N/A * @exception NullPointerException if <code>algorithm</code>
0N/A * is null.
0N/A *
0N/A * @exception NoSuchAlgorithmException if no Provider supports an
0N/A * ExemptionMechanismSpi implementation for the
0N/A * specified algorithm.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static final ExemptionMechanism getInstance(String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A Instance instance = JceSecurity.getInstance("ExemptionMechanism",
0N/A ExemptionMechanismSpi.class, algorithm);
0N/A return new ExemptionMechanism((ExemptionMechanismSpi)instance.impl,
0N/A instance.provider, algorithm);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns an <code>ExemptionMechanism</code> object that implements the
0N/A * specified exemption mechanism algorithm.
0N/A *
0N/A * <p> A new ExemptionMechanism object encapsulating the
0N/A * ExemptionMechanismSpi implementation from the specified provider
0N/A * is returned. The specified provider must be registered
0N/A * in the security provider list.
0N/A *
0N/A * <p> Note that the list of registered providers may be retrieved via
0N/A * the {@link Security#getProviders() Security.getProviders()} method.
0N/A
0N/A * @param algorithm the standard name of the requested exemption mechanism.
3465N/A * See the ExemptionMechanism section in the
0N/A * <a href=
3465N/A * "{docRoot}/../technotes/guides/security/StandardNames.html#Exemption">
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
0N/A * for information about standard exemption mechanism names.
0N/A *
0N/A * @param provider the name of the provider.
0N/A *
0N/A * @return the new <code>ExemptionMechanism</code> object.
0N/A *
0N/A * @exception NullPointerException if <code>algorithm</code>
0N/A * is null.
0N/A *
0N/A * @exception NoSuchAlgorithmException if an ExemptionMechanismSpi
0N/A * implementation for the specified algorithm is not
0N/A * available from the specified provider.
0N/A *
0N/A * @exception NoSuchProviderException if the specified provider is not
0N/A * registered in the security provider list.
0N/A *
0N/A * @exception IllegalArgumentException if the <code>provider</code>
0N/A * is null or empty.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static final ExemptionMechanism getInstance(String algorithm,
0N/A String provider) throws NoSuchAlgorithmException,
0N/A NoSuchProviderException {
0N/A Instance instance = JceSecurity.getInstance("ExemptionMechanism",
0N/A ExemptionMechanismSpi.class, algorithm, provider);
0N/A return new ExemptionMechanism((ExemptionMechanismSpi)instance.impl,
0N/A instance.provider, algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns an <code>ExemptionMechanism</code> object that implements the
0N/A * specified exemption mechanism algorithm.
0N/A *
0N/A * <p> A new ExemptionMechanism object encapsulating the
0N/A * ExemptionMechanismSpi implementation from the specified Provider
0N/A * object is returned. Note that the specified Provider object
0N/A * does not have to be registered in the provider list.
0N/A *
0N/A * @param algorithm the standard name of the requested exemption mechanism.
3465N/A * See the ExemptionMechanism section in the
0N/A * <a href=
3465N/A * "{docRoot}/../technotes/guides/security/StandardNames.html#Exemption">
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
0N/A * for information about standard exemption mechanism names.
0N/A *
0N/A * @param provider the provider.
0N/A *
0N/A * @return the new <code>ExemptionMechanism</code> object.
0N/A *
0N/A * @exception NullPointerException if <code>algorithm</code>
0N/A * is null.
0N/A *
0N/A * @exception NoSuchAlgorithmException if an ExemptionMechanismSpi
0N/A * implementation for the specified algorithm is not available
0N/A * from the specified Provider object.
0N/A *
0N/A * @exception IllegalArgumentException if the <code>provider</code>
0N/A * is null.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static final ExemptionMechanism getInstance(String algorithm,
0N/A Provider provider) throws NoSuchAlgorithmException {
0N/A Instance instance = JceSecurity.getInstance("ExemptionMechanism",
0N/A ExemptionMechanismSpi.class, algorithm, provider);
0N/A return new ExemptionMechanism((ExemptionMechanismSpi)instance.impl,
0N/A instance.provider, algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns the provider of this <code>ExemptionMechanism</code> object.
0N/A *
0N/A * @return the provider of this <code>ExemptionMechanism</code> object.
0N/A */
0N/A public final Provider getProvider() {
0N/A return this.provider;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether the result blob has been generated successfully by this
0N/A * exemption mechanism.
0N/A *
0N/A * <p>The method also makes sure that the key passed in is the same as
0N/A * the one this exemption mechanism used in initializing and generating
0N/A * phases.
0N/A *
0N/A * @param key the key the crypto is going to use.
0N/A *
0N/A * @return whether the result blob of the same key has been generated
0N/A * successfully by this exemption mechanism; false if <code>key</code>
0N/A * is null.
0N/A *
0N/A * @exception ExemptionMechanismException if problem(s) encountered
0N/A * while determining whether the result blob has been generated successfully
0N/A * by this exemption mechanism object.
0N/A */
0N/A public final boolean isCryptoAllowed(Key key)
0N/A throws ExemptionMechanismException {
0N/A boolean ret = false;
0N/A if (done && (key != null)) {
0N/A // Check if the key passed in is the same as the one
0N/A // this exemption mechanism used.
0N/A ret = keyStored.equals(key);
0N/A }
0N/A return ret;
0N/A }
0N/A
0N/A /**
0N/A * Returns the length in bytes that an output buffer would need to be in
0N/A * order to hold the result of the next
0N/A * {@link #genExemptionBlob(byte[]) genExemptionBlob}
0N/A * operation, given the input length <code>inputLen</code> (in bytes).
0N/A *
0N/A * <p>The actual output length of the next
0N/A * {@link #genExemptionBlob(byte[]) genExemptionBlob}
0N/A * call may be smaller than the length returned by this method.
0N/A *
0N/A * @param inputLen the input length (in bytes)
0N/A *
0N/A * @return the required output buffer size (in bytes)
0N/A *
0N/A * @exception IllegalStateException if this exemption mechanism is in a
0N/A * wrong state (e.g., has not yet been initialized)
0N/A */
0N/A public final int getOutputSize(int inputLen) throws IllegalStateException {
0N/A if (!initialized) {
0N/A throw new IllegalStateException(
0N/A "ExemptionMechanism not initialized");
0N/A }
0N/A if (inputLen < 0) {
0N/A throw new IllegalArgumentException(
0N/A "Input size must be equal to " + "or greater than zero");
0N/A }
0N/A return exmechSpi.engineGetOutputSize(inputLen);
0N/A }
0N/A
0N/A /**
0N/A * Initializes this exemption mechanism with a key.
0N/A *
0N/A * <p>If this exemption mechanism requires any algorithm parameters
0N/A * that cannot be derived from the given <code>key</code>, the
0N/A * underlying exemption mechanism implementation is supposed to
0N/A * generate the required parameters itself (using provider-specific
0N/A * default values); in the case that algorithm parameters must be
0N/A * specified by the caller, an <code>InvalidKeyException</code> is raised.
0N/A *
0N/A * @param key the key for this exemption mechanism
0N/A *
0N/A * @exception InvalidKeyException if the given key is inappropriate for
0N/A * this exemption mechanism.
0N/A * @exception ExemptionMechanismException if problem(s) encountered in the
0N/A * process of initializing.
0N/A */
0N/A public final void init(Key key)
0N/A throws InvalidKeyException, ExemptionMechanismException {
0N/A done = false;
0N/A initialized = false;
0N/A
0N/A keyStored = key;
0N/A exmechSpi.engineInit(key);
0N/A initialized = true;
0N/A }
0N/A
0N/A /**
0N/A * Initializes this exemption mechanism with a key and a set of algorithm
0N/A * parameters.
0N/A *
0N/A * <p>If this exemption mechanism requires any algorithm parameters
0N/A * and <code>params</code> is null, the underlying exemption
0N/A * mechanism implementation is supposed to generate the required
0N/A * parameters itself (using provider-specific default values); in the case
0N/A * that algorithm parameters must be specified by the caller, an
0N/A * <code>InvalidAlgorithmParameterException</code> is raised.
0N/A *
0N/A * @param key the key for this exemption mechanism
0N/A * @param params the algorithm parameters
0N/A *
0N/A * @exception InvalidKeyException if the given key is inappropriate for
0N/A * this exemption mechanism.
0N/A * @exception InvalidAlgorithmParameterException if the given algorithm
0N/A * parameters are inappropriate for this exemption mechanism.
0N/A * @exception ExemptionMechanismException if problem(s) encountered in the
0N/A * process of initializing.
0N/A */
0N/A public final void init(Key key, AlgorithmParameterSpec params)
0N/A throws InvalidKeyException, InvalidAlgorithmParameterException,
0N/A ExemptionMechanismException {
0N/A done = false;
0N/A initialized = false;
0N/A
0N/A keyStored = key;
0N/A exmechSpi.engineInit(key, params);
0N/A initialized = true;
0N/A }
0N/A
0N/A /**
0N/A * Initializes this exemption mechanism with a key and a set of algorithm
0N/A * parameters.
0N/A *
0N/A * <p>If this exemption mechanism requires any algorithm parameters
0N/A * and <code>params</code> is null, the underlying exemption mechanism
0N/A * implementation is supposed to generate the required parameters itself
0N/A * (using provider-specific default values); in the case that algorithm
0N/A * parameters must be specified by the caller, an
0N/A * <code>InvalidAlgorithmParameterException</code> is raised.
0N/A *
0N/A * @param key the key for this exemption mechanism
0N/A * @param params the algorithm parameters
0N/A *
0N/A * @exception InvalidKeyException if the given key is inappropriate for
0N/A * this exemption mechanism.
0N/A * @exception InvalidAlgorithmParameterException if the given algorithm
0N/A * parameters are inappropriate for this exemption mechanism.
0N/A * @exception ExemptionMechanismException if problem(s) encountered in the
0N/A * process of initializing.
0N/A */
0N/A public final void init(Key key, AlgorithmParameters params)
0N/A throws InvalidKeyException, InvalidAlgorithmParameterException,
0N/A ExemptionMechanismException {
0N/A done = false;
0N/A initialized = false;
0N/A
0N/A keyStored = key;
0N/A exmechSpi.engineInit(key, params);
0N/A initialized = true;
0N/A }
0N/A
0N/A /**
0N/A * Generates the exemption mechanism key blob.
0N/A *
0N/A * @return the new buffer with the result key blob.
0N/A *
0N/A * @exception IllegalStateException if this exemption mechanism is in
0N/A * a wrong state (e.g., has not been initialized).
0N/A * @exception ExemptionMechanismException if problem(s) encountered in the
0N/A * process of generating.
0N/A */
0N/A public final byte[] genExemptionBlob() throws IllegalStateException,
0N/A ExemptionMechanismException {
0N/A if (!initialized) {
0N/A throw new IllegalStateException(
0N/A "ExemptionMechanism not initialized");
0N/A }
0N/A byte[] blob = exmechSpi.engineGenExemptionBlob();
0N/A done = true;
0N/A return blob;
0N/A }
0N/A
0N/A /**
0N/A * Generates the exemption mechanism key blob, and stores the result in
0N/A * the <code>output</code> buffer.
0N/A *
0N/A * <p>If the <code>output</code> buffer is too small to hold the result,
0N/A * a <code>ShortBufferException</code> is thrown. In this case, repeat this
0N/A * call with a larger output buffer. Use
0N/A * {@link #getOutputSize(int) getOutputSize} to determine how big
0N/A * the output buffer should be.
0N/A *
0N/A * @param output the buffer for the result
0N/A *
0N/A * @return the number of bytes stored in <code>output</code>
0N/A *
0N/A * @exception IllegalStateException if this exemption mechanism is in
0N/A * a wrong state (e.g., has not been initialized).
0N/A * @exception ShortBufferException if the given output buffer is too small
0N/A * to hold the result.
0N/A * @exception ExemptionMechanismException if problem(s) encountered in the
0N/A * process of generating.
0N/A */
0N/A public final int genExemptionBlob(byte[] output)
0N/A throws IllegalStateException, ShortBufferException,
0N/A ExemptionMechanismException {
0N/A if (!initialized) {
0N/A throw new IllegalStateException
0N/A ("ExemptionMechanism not initialized");
0N/A }
0N/A int n = exmechSpi.engineGenExemptionBlob(output, 0);
0N/A done = true;
0N/A return n;
0N/A }
0N/A
0N/A /**
0N/A * Generates the exemption mechanism key blob, and stores the result in
0N/A * the <code>output</code> buffer, starting at <code>outputOffset</code>
0N/A * inclusive.
0N/A *
0N/A * <p>If the <code>output</code> buffer is too small to hold the result,
0N/A * a <code>ShortBufferException</code> is thrown. In this case, repeat this
0N/A * call with a larger output buffer. Use
0N/A * {@link #getOutputSize(int) getOutputSize} to determine how big
0N/A * the output buffer should be.
0N/A *
0N/A * @param output the buffer for the result
0N/A * @param outputOffset the offset in <code>output</code> where the result
0N/A * is stored
0N/A *
0N/A * @return the number of bytes stored in <code>output</code>
0N/A *
0N/A * @exception IllegalStateException if this exemption mechanism is in
0N/A * a wrong state (e.g., has not been initialized).
0N/A * @exception ShortBufferException if the given output buffer is too small
0N/A * to hold the result.
0N/A * @exception ExemptionMechanismException if problem(s) encountered in the
0N/A * process of generating.
0N/A */
0N/A public final int genExemptionBlob(byte[] output, int outputOffset)
0N/A throws IllegalStateException, ShortBufferException,
0N/A ExemptionMechanismException {
0N/A if (!initialized) {
0N/A throw new IllegalStateException
0N/A ("ExemptionMechanism not initialized");
0N/A }
0N/A int n = exmechSpi.engineGenExemptionBlob(output, outputOffset);
0N/A done = true;
0N/A return n;
0N/A }
0N/A
0N/A /**
0N/A * Ensures that the key stored away by this ExemptionMechanism
0N/A * object will be wiped out when there are no more references to it.
0N/A */
0N/A protected void finalize() {
0N/A keyStored = null;
0N/A // Are there anything else we could do?
0N/A }
0N/A}