FileDocCategorySizeDatePackage
ExemptionMechanism.javaAPI DocAndroid 1.5 API14663Wed May 06 22:41:02 BST 2009javax.crypto

ExemptionMechanism

public class ExemptionMechanism extends Object
This class implements the functionality of an exemption mechanism such as key recovery, key weakening, or key escrow.
since
Android 1.0

Fields Summary
private static final org.apache.harmony.security.fortress.Engine
engine
private final Provider
provider
private final ExemptionMechanismSpi
spiImpl
private final String
mechanism
private boolean
isInit
private Key
initKey
private boolean
generated
Constructors Summary
protected ExemptionMechanism(ExemptionMechanismSpi exmechSpi, Provider provider, String mechanism)
Creates a {@code ExemptionMechanism} instance.

param
exmechSpi the implementation delegate.
param
provider the associated provider.
param
mechanism the name of the mechanism.
since
Android 1.0


                                                                    
      
                
        this.mechanism = mechanism;
        this.spiImpl = exmechSpi;
        this.provider = provider;
        isInit = false;
    
Methods Summary
protected voidfinalize()
Frees the references to the key used to initialize this instance.

since
Android 1.0

        initKey = null;
    
public final byte[]genExemptionBlob()
Generates the result key blob for this exemption mechanism.

return
the result key blob for this exemption mechanism.
throws
IllegalStateException if this {@code ExemptionMechanism} instance is not initialized.
throws
ExemptionMechanismException if error(s) occur during generation.
since
Android 1.0

        if (!isInit) {
            throw new IllegalStateException(Messages.getString("crypto.2D"));
        }
        generated = false;
        byte[] result = spiImpl.engineGenExemptionBlob();
        generated = true;
        return result;
    
public final intgenExemptionBlob(byte[] output)
Generates the result key blob for this exemption mechanism and stores it into the {@code output} buffer.

param
output the output buffer for the result key blob.
return
the number of bytes written to the {@code output} buffer.
throws
IllegalStateException if this {@code ExemptionMechanism} instance is not initialized.
throws
ShortBufferException if the provided buffer is too small for the result key blob.
throws
ExemptionMechanismException if error(s) occur during generation.
since
Android 1.0

        return genExemptionBlob(output, 0);
    
public final intgenExemptionBlob(byte[] output, int outputOffset)
Generates the result key blob for this exemption mechanism and stores it into the {@code output} buffer at offset {@code outputOffset}.

param
output the output buffer for the result key blob.
param
outputOffset the offset in the output buffer to start.
return
the number of bytes written to the {@code output} buffer.
throws
IllegalStateException if this {@code ExemptionMechanism} instance is not initialized.
throws
ShortBufferException if the provided buffer is too small for the result key blob.
throws
ExemptionMechanismException if error(s) occur during generation.
since
Android 1.0

        if (!isInit) {
            throw new IllegalStateException(Messages.getString("crypto.2D"));
        }
        generated = false;
        int len = spiImpl.engineGenExemptionBlob(output, outputOffset);
        generated = true;
        return len;
    
public static final javax.crypto.ExemptionMechanismgetInstance(java.lang.String algorithm)
Returns a new {@code ExemptionMechanism} instance that provides the specified exemption mechanism algorithm.

param
algorithm the name of the requested exemption mechanism.
return
the new {@code ExemptionMechanism} instance.
throws
NoSuchAlgorithmException if the specified algorithm is not available by any provider.
throws
NullPointerException if the algorithm parameter is {@code null}.
since
Android 1.0

        if (algorithm == null) {
            throw new NullPointerException(Messages.getString("crypto.02")); //$NON-NLS-1$
        }
        synchronized (engine) {
            engine.getInstance(algorithm, null);
            return new ExemptionMechanism((ExemptionMechanismSpi) engine.spi,
                    engine.provider, algorithm);
        }
    
public static final javax.crypto.ExemptionMechanismgetInstance(java.lang.String algorithm, java.lang.String provider)
Returns a new {@code ExemptionMechansm} instance that provides the specified exemption mechanism algorithm from the specified provider.

param
algorithm the name of the requested exemption mechanism.
param
provider the name of the provider that is providing the algorithm.
return
the new {@code ExemptionMechanism} instance.
throws
NoSuchAlgorithmException if the specified algorithm is not provided by the specified provider.
throws
NoSuchProviderException if the specified provider is not available.
throws
NullPointerException if the algorithm parameter is {@code null}.
throws
IllegalArgumentException if the provider parameter is {@code null}.
since
Android 1.0

        if (provider == null) {
            throw new IllegalArgumentException(Messages.getString("crypto.04")); //$NON-NLS-1$
        }
        Provider impProvider = Security.getProvider(provider);
        if (impProvider == null) {
            throw new NoSuchProviderException(provider);
        }
        if (algorithm == null) {
            throw new NullPointerException(Messages.getString("crypto.02")); //$NON-NLS-1$
        }
        return getInstance(algorithm, impProvider);
    
public static final javax.crypto.ExemptionMechanismgetInstance(java.lang.String algorithm, java.security.Provider provider)
Returns a new {@code ExemptionMechanism} instance that provides the specified exemption mechanism algorithm from the specified provider.

param
algorithm the name of the requested exemption mechanism.
param
provider the provider that is providing the algorithm.
return
the new {@code ExemptionMechanism} instance.
throws
NoSuchAlgorithmException if the specified algorithm is not provided by the specified provider.
throws
NullPointerException if the algorithm parameter is {@code null}.
throws
IllegalArgumentException if the provider parameter is {@code null}.
since
Android 1.0

        if (algorithm == null) {
            throw new NullPointerException(Messages.getString("crypto.02")); //$NON-NLS-1$
        }
        if (provider == null) {
            throw new IllegalArgumentException(Messages.getString("crypto.04")); //$NON-NLS-1$
        }
        synchronized (engine) {
            engine.getInstance(algorithm, provider, null);
            return new ExemptionMechanism((ExemptionMechanismSpi) engine.spi,
                    provider, algorithm);
        }
    
public final java.lang.StringgetName()
Returns the name of this {@code ExemptionMechanism}.

return
the name of this {@code ExemptionMechanism}.
since
Android 1.0

        return mechanism;
    
public final intgetOutputSize(int inputLen)
Returns the size in bytes for the output buffer needed to hold the output of the next {@link #genExemptionBlob} call, given the specified {@code inputLen} (in bytes).

param
inputLen the specified input length (in bytes).
return
the size in bytes for the output buffer.
throws
IllegalStateException if this {@code ExemptionMechanism} instance is not initialized.
since
Android 1.0

        if (!isInit) {
            throw new IllegalStateException(Messages.getString("crypto.2D"));
        }
        return spiImpl.engineGetOutputSize(inputLen);
    
public final java.security.ProvidergetProvider()
Returns the provider of this {@code ExemptionMechanism} instance.

return
the provider of this {@code ExemptionMechanism} instance.
since
Android 1.0

        return provider;
    
public final voidinit(java.security.Key key, java.security.AlgorithmParameters param)
Initializes this {@code ExemptionMechanism} instance with the specified key and algorithm parameters.

param
key the key to initialize this instance with.
param
param the parameters for this exemption mechanism algorithm.
throws
InvalidKeyException if the key cannot be used to initialize this mechanism.
throws
InvalidAlgorithmParameterException if the parameters cannot be used to initialize this mechanism.
throws
ExemptionMechanismException if error(s) occur during initialization.
since
Android 1.0

        generated = false;
        spiImpl.engineInit(key, param);
        initKey = key;
        isInit = true;
    
public final voidinit(java.security.Key key, java.security.spec.AlgorithmParameterSpec param)
Initializes this {@code ExemptionMechanism} instance with the specified key and algorithm parameters.

param
key the key to initialize this instance with.
param
param the parameters for this exemption mechanism algorithm.
throws
InvalidKeyException if the key cannot be used to initialize this mechanism.
throws
InvalidAlgorithmParameterException the the parameters cannot be used to initialize this mechanism.
throws
ExemptionMechanismException if error(s) occur during initialization.
since
Android 1.0

        generated = false;
        spiImpl.engineInit(key, param);
        initKey = key;
        isInit = true;
    
public final voidinit(java.security.Key key)
Initializes this {@code ExemptionMechanism} instance with the specified key.

param
key the key to initialize this instance with.
throws
InvalidKeyException if the key cannot be used to initialize this mechanism.
throws
ExemptionMechanismException if error(s) occur during initialization.
since
Android 1.0

        generated = false;
        spiImpl.engineInit(key);
        initKey = key;
        isInit = true;
    
public final booleanisCryptoAllowed(java.security.Key key)
Returns whether the result blob for this {@code ExemptionMechanism} instance has been generated successfully and that the specified key is the same as the one that was used to initialize and generate.

param
key the key to verify.
return
whether the result blob for this {@code ExemptionMechanism} instance has been generated successfully.
throws
ExemptionMechanismException if an error occurs while determining whether the result blob has been generated successfully.
since
Android 1.0


        if (generated
                && (initKey.equals(key) || Arrays.equals(initKey.getEncoded(),
                        key.getEncoded()))) {
            return true;
        }
        return false;