зеркало из https://github.com/mozilla/pjs.git
allow algorithms to support multiple parameter classes.
This commit is contained in:
Родитель
94aeb16532
Коммит
bfb6472918
|
@ -68,7 +68,21 @@ public class Algorithm {
|
|||
Class paramClass)
|
||||
{
|
||||
this(oidIndex, name, oid);
|
||||
this.parameterClass = paramClass;
|
||||
if( paramClass == null ) {
|
||||
this.parameterClasses = new Class[0];
|
||||
} else {
|
||||
this.parameterClasses = new Class[1];
|
||||
this.parameterClasses[0] = paramClass;
|
||||
}
|
||||
}
|
||||
|
||||
protected Algorithm(int oidIndex, String name, OBJECT_IDENTIFIER oid,
|
||||
Class []paramClasses)
|
||||
{
|
||||
this(oidIndex, name, oid);
|
||||
if( paramClasses != null ) {
|
||||
this.parameterClasses = paramClasses;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -94,9 +108,48 @@ public class Algorithm {
|
|||
/**
|
||||
* The type of parameter that this algorithm expects. Returns
|
||||
* <code>null</code> if this algorithm does not take any parameters.
|
||||
* If the algorithm can accept more than one type of parameter,
|
||||
* this method returns only one of them. It is better to call
|
||||
* <tt>getParameterClasses()</tt>.
|
||||
* @deprecated Call <tt>getParameterClasses()</tt> instead.
|
||||
*/
|
||||
public Class getParameterClass() {
|
||||
return parameterClass;
|
||||
if( parameterClasses.length == 0) {
|
||||
return null;
|
||||
} else {
|
||||
return parameterClasses[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The types of parameter that this algorithm expects. Returns
|
||||
* <code>null</code> if this algorithm does not take any parameters.
|
||||
*/
|
||||
public Class[] getParameterClasses() {
|
||||
return (Class[]) parameterClasses.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if the given Object can be used as a parameter
|
||||
* for this algorithm.
|
||||
* <p>If <tt>null</tt> is passed in, this method will return <tt>true</tt>
|
||||
* if this algorithm takes no parameters, and <tt>false</tt>
|
||||
* if this algorithm does take parameters.
|
||||
*/
|
||||
public boolean isValidParameterObject(Object o) {
|
||||
if( o == null ) {
|
||||
return (parameterClasses.length == 0);
|
||||
}
|
||||
if( parameterClasses.length == 0 ){
|
||||
return false;
|
||||
}
|
||||
Class c = o.getClass();
|
||||
for( int i = 0; i < parameterClasses.length; ++i) {
|
||||
if( c.equals( parameterClasses[i] ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -105,7 +158,7 @@ public class Algorithm {
|
|||
protected int oidIndex;
|
||||
String name;
|
||||
protected OBJECT_IDENTIFIER oid;
|
||||
private Class parameterClass=null;
|
||||
private Class[] parameterClasses=new Class[0];
|
||||
|
||||
//////////////////////////////////////////////////////////////
|
||||
// Algorithm OIDs
|
||||
|
|
|
@ -42,8 +42,7 @@ import java.security.NoSuchAlgorithmException;
|
|||
public class KeyWrapAlgorithm extends Algorithm {
|
||||
protected KeyWrapAlgorithm(int oidTag, String name, Class paramClass,
|
||||
boolean padded, int blockSize) {
|
||||
super(oidTag, name);
|
||||
parameterClass = paramClass;
|
||||
super(oidTag, name, null, paramClass);
|
||||
this.padded = padded;
|
||||
this.blockSize = blockSize;
|
||||
if( name != null ) {
|
||||
|
@ -51,7 +50,6 @@ public class KeyWrapAlgorithm extends Algorithm {
|
|||
}
|
||||
}
|
||||
|
||||
private Class parameterClass;
|
||||
private boolean padded;
|
||||
private int blockSize;
|
||||
|
||||
|
@ -68,14 +66,6 @@ public class KeyWrapAlgorithm extends Algorithm {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The type of parameter that this algorithm expects. Returns
|
||||
* <code>null</code> if this algorithm does not take any parameters.
|
||||
*/
|
||||
public Class getParameterClass() {
|
||||
return parameterClass;
|
||||
}
|
||||
|
||||
public boolean isPadded() {
|
||||
return padded;
|
||||
}
|
||||
|
|
|
@ -95,11 +95,17 @@ public final class PK11KeyGenerator implements KeyGenerator {
|
|||
public void initialize(int strength)
|
||||
throws InvalidAlgorithmParameterException
|
||||
{
|
||||
// validate the strength for our algorithm
|
||||
if( PBEKeyGenParams.class.equals(algorithm.getParameterClass())) {
|
||||
// if this algorithm only accepts PBE key gen params, it can't
|
||||
// use a strength
|
||||
Class[] paramClasses = algorithm.getParameterClasses();
|
||||
if( paramClasses.length == 1 &&
|
||||
paramClasses[0].equals(PBEKeyGenParams.class) )
|
||||
{
|
||||
throw new InvalidAlgorithmParameterException("PBE keygen "+
|
||||
"algorithms require PBEKeyGenParams ");
|
||||
}
|
||||
|
||||
// validate the strength for our algorithm
|
||||
if( ! algorithm.isValidStrength(strength) ) {
|
||||
throw new InvalidAlgorithmParameterException(strength+
|
||||
" is not a valid strength for "+algorithm);
|
||||
|
@ -116,16 +122,15 @@ public final class PK11KeyGenerator implements KeyGenerator {
|
|||
public void initialize(AlgorithmParameterSpec parameters)
|
||||
throws InvalidAlgorithmParameterException
|
||||
{
|
||||
if( PBEKeyGenParams.class.equals(algorithm.getParameterClass())) {
|
||||
if( ! (parameters instanceof PBEKeyGenParams) ) {
|
||||
throw new InvalidAlgorithmParameterException(
|
||||
"PBE keygen algorithms require PBEKeyGenParams");
|
||||
if( ! algorithm.isValidParameterObject(parameters) ) {
|
||||
String name = "null";
|
||||
if( parameters != null ) {
|
||||
name = parameters.getClass().getName();
|
||||
}
|
||||
this.parameters = parameters;
|
||||
} else {
|
||||
throw new InvalidAlgorithmParameterException(
|
||||
algorithm+" does not take any parameters");
|
||||
algorithm + " cannot use a " + name + " parameter");
|
||||
}
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
|
||||
|
@ -136,7 +141,10 @@ public final class PK11KeyGenerator implements KeyGenerator {
|
|||
public SymmetricKey generate()
|
||||
throws IllegalStateException, TokenException, CharConversionException
|
||||
{
|
||||
if( PBEKeyGenParams.class.equals(algorithm.getParameterClass())) {
|
||||
Class[] paramClasses = algorithm.getParameterClasses();
|
||||
if( paramClasses.length == 1 &&
|
||||
paramClasses[0].equals(PBEKeyGenParams.class) )
|
||||
{
|
||||
if(parameters==null || !(parameters instanceof PBEKeyGenParams)) {
|
||||
throw new IllegalStateException(
|
||||
"PBE keygen algorithms require PBEKeyGenParams");
|
||||
|
@ -170,7 +178,10 @@ public final class PK11KeyGenerator implements KeyGenerator {
|
|||
public byte[] generatePBE_IV()
|
||||
throws TokenException, CharConversionException
|
||||
{
|
||||
if( PBEKeyGenParams.class.equals(algorithm.getParameterClass())) {
|
||||
Class[] paramClasses = algorithm.getParameterClasses();
|
||||
if( paramClasses.length == 1 &&
|
||||
paramClasses[0].equals(PBEKeyGenParams.class) )
|
||||
{
|
||||
if(parameters==null || !(parameters instanceof PBEKeyGenParams)) {
|
||||
throw new IllegalStateException(
|
||||
"PBE keygen algorithms require PBEKeyGenParams");
|
||||
|
|
|
@ -229,26 +229,23 @@ final class PK11KeyWrapper implements KeyWrapper {
|
|||
private void checkParams(AlgorithmParameterSpec params)
|
||||
throws InvalidAlgorithmParameterException
|
||||
{
|
||||
Class paramClass = algorithm.getParameterClass();
|
||||
if(params==null) {
|
||||
if(paramClass != null) {
|
||||
// this algorithm takes a parameter, but none was given
|
||||
throw new InvalidAlgorithmParameterException(algorithm+
|
||||
" requires an algorithm parameter");
|
||||
if( ! algorithm.isValidParameterObject(params) ) {
|
||||
String name = "null";
|
||||
if( params != null ) {
|
||||
name = params.getClass().getName();
|
||||
}
|
||||
} else {
|
||||
if( paramClass == null ) {
|
||||
//this algorithm doesn't take a param, but one was given
|
||||
throw new InvalidAlgorithmParameterException(algorithm+
|
||||
" does not take a parameter");
|
||||
} else if( ! ( paramClass.isInstance(params) ) ) {
|
||||
throw new InvalidAlgorithmParameterException(algorithm+
|
||||
" expects a parameter of type "+paramClass);
|
||||
}
|
||||
|
||||
if( params instanceof IVParameterSpec ) {
|
||||
IV = ((IVParameterSpec)params).getIV();
|
||||
throw new InvalidAlgorithmParameterException(
|
||||
algorithm + " cannot use a " + name + " parameter");
|
||||
}
|
||||
if( params instanceof IVParameterSpec ) {
|
||||
IV = ((IVParameterSpec)params).getIV();
|
||||
}
|
||||
try {
|
||||
if( params instanceof javax.crypto.spec.IvParameterSpec ) {
|
||||
IV = ((javax.crypto.spec.IvParameterSpec)params).getIV();
|
||||
}
|
||||
} catch(NoClassDefFoundError e) {
|
||||
// we must be running in JDK < 1.4. Ignore.
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче