ExemptionMechanism.java [plain text]
package javax.crypto;
import gnu.java.security.Engine;
import java.lang.reflect.InvocationTargetException;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;
public class ExemptionMechanism
{
private static final String SERVICE = "ExemptionMechanism";
private ExemptionMechanismSpi emSpi;
private Provider provider;
private String mechanism;
private boolean virgin;
protected ExemptionMechanism(ExemptionMechanismSpi emSpi, Provider provider,
String mechanism)
{
this.emSpi = emSpi;
this.provider = provider;
this.mechanism = mechanism;
virgin = true;
}
public static final ExemptionMechanism getInstance(String mechanism)
throws NoSuchAlgorithmException
{
Provider[] provs = Security.getProviders();
String msg = "";
for (int i = 0; i < provs.length; i++)
{
try
{
return getInstance(mechanism, provs[i]);
}
catch (NoSuchAlgorithmException nsae)
{
msg = nsae.getMessage();
}
}
throw new NoSuchAlgorithmException(msg);
}
public static final ExemptionMechanism getInstance(String mechanism,
String provider)
throws NoSuchAlgorithmException, NoSuchProviderException
{
Provider p = Security.getProvider(provider);
if (p == null)
{
throw new NoSuchProviderException(provider);
}
return getInstance(mechanism, p);
}
public static final ExemptionMechanism getInstance(String mechanism,
Provider provider)
throws NoSuchAlgorithmException
{
try
{
return new ExemptionMechanism((ExemptionMechanismSpi)
Engine.getInstance(SERVICE, mechanism, provider),
provider, mechanism);
}
catch (InvocationTargetException ite)
{
if (ite.getCause() instanceof NoSuchAlgorithmException)
throw (NoSuchAlgorithmException) ite.getCause();
else
throw new NoSuchAlgorithmException(mechanism);
}
catch (ClassCastException cce)
{
throw new NoSuchAlgorithmException(mechanism);
}
}
public final byte[] genExemptionBlob()
throws IllegalStateException, ExemptionMechanismException
{
if (virgin)
{
throw new IllegalStateException("not initialized");
}
return emSpi.engineGenExemptionBlob();
}
public final int genExemptionBlob(byte[] output)
throws IllegalStateException, ExemptionMechanismException,
ShortBufferException
{
return genExemptionBlob(output, 0);
}
public final int genExemptionBlob(byte[] output, int outputOffset)
throws IllegalStateException, ExemptionMechanismException,
ShortBufferException
{
if (virgin)
{
throw new IllegalStateException("not initialized");
}
return emSpi.engineGenExemptionBlob(output, outputOffset);
}
public final String getName()
{
return mechanism;
}
public final int getOutputSize(int inputLength) throws IllegalStateException
{
if (virgin)
{
throw new IllegalStateException("not initialized");
}
return emSpi.engineGetOutputSize(inputLength);
}
public final Provider getProvider()
{
return provider;
}
public final void init(Key key)
throws ExemptionMechanismException, InvalidKeyException
{
emSpi.engineInit(key);
virgin = false;
}
public final void init(Key key, AlgorithmParameters params)
throws ExemptionMechanismException, InvalidAlgorithmParameterException,
InvalidKeyException
{
emSpi.engineInit(key, params);
virgin = false;
}
public final void init(Key key, AlgorithmParameterSpec params)
throws ExemptionMechanismException, InvalidAlgorithmParameterException,
InvalidKeyException
{
emSpi.engineInit(key, params);
virgin = false;
}
public final boolean isCryptoAllowed(Key key)
throws ExemptionMechanismException
{
return true;
}
protected void finalize()
{
}
}