201245 convert SecretKey to SecretKeyFacade

This commit is contained in:
glen.beasley%sun.com 2003-04-21 21:38:34 +00:00
Родитель e50f01e0d2
Коммит 5973e1c1e8
1 изменённых файлов: 33 добавлений и 5 удалений

Просмотреть файл

@ -45,6 +45,8 @@ import javax.crypto.ShortBufferException;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.IvParameterSpec;
import javax.crypto.SecretKeyFactory;
import javax.crypto.SecretKey;
import org.mozilla.jss.crypto.KeyWrapper; import org.mozilla.jss.crypto.KeyWrapper;
import org.mozilla.jss.crypto.KeyWrapAlgorithm; import org.mozilla.jss.crypto.KeyWrapAlgorithm;
import org.mozilla.jss.crypto.EncryptionAlgorithm; import org.mozilla.jss.crypto.EncryptionAlgorithm;
@ -54,6 +56,7 @@ import org.mozilla.jss.crypto.Algorithm;
import org.mozilla.jss.crypto.SecretKeyFacade; import org.mozilla.jss.crypto.SecretKeyFacade;
import org.mozilla.jss.crypto.SymmetricKey; import org.mozilla.jss.crypto.SymmetricKey;
import org.mozilla.jss.crypto.TokenException; import org.mozilla.jss.crypto.TokenException;
import org.mozilla.jss.crypto.SecretKeyFacade;
import org.mozilla.jss.crypto.TokenRuntimeException; import org.mozilla.jss.crypto.TokenRuntimeException;
import org.mozilla.jss.crypto.JSSSecureRandom; import org.mozilla.jss.crypto.JSSSecureRandom;
import org.mozilla.jss.CryptoManager; import org.mozilla.jss.CryptoManager;
@ -94,6 +97,31 @@ class JSSCipherSpi extends javax.crypto.CipherSpi {
this.algPadding = padding; this.algPadding = padding;
} }
static private SecretKey importKey(Key key)
throws InvalidKeyException
{
if (key instanceof SecretKey) {
SecretKey sKey = (SecretKey) key;
SecretKeyFactory fact = null;
try {
fact = SecretKeyFactory.getInstance(sKey.getAlgorithm(),
"Mozilla-JSS");
} catch (NoSuchAlgorithmException e) {
throw new InvalidKeyException(
"Unable to translate key with Algorithm"
+ key.getAlgorithm());
} catch (NoSuchProviderException ex) {
throw new InvalidKeyException(
"Unable to find provider, this should not happen");
}
return fact.translateKey(sKey);
}else {
throw new InvalidKeyException("Invalid key type: " +
key.getClass().getName());
}
}
public void engineInit(int opmode, Key key, public void engineInit(int opmode, Key key,
AlgorithmParameterSpec givenParams, SecureRandom random) AlgorithmParameterSpec givenParams, SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException throws InvalidKeyException, InvalidAlgorithmParameterException
@ -127,8 +155,8 @@ class JSSCipherSpi extends javax.crypto.CipherSpi {
} }
if( opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.DECRYPT_MODE ) { if( opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.DECRYPT_MODE ) {
if( ! (key instanceof SecretKeyFacade) ) { if( ! (key instanceof SecretKeyFacade) ) {
throw new InvalidKeyException("key must be JSS key"); key = importKey(key);
} }
SymmetricKey symkey = ((SecretKeyFacade)key).key; SymmetricKey symkey = ((SecretKeyFacade)key).key;
@ -461,9 +489,9 @@ class JSSCipherSpi extends javax.crypto.CipherSpi {
SymmetricKey symkey = ((SecretKeyFacade)key).key; SymmetricKey symkey = ((SecretKeyFacade)key).key;
return symkey.getLength(); return symkey.getLength();
} else { } else {
throw new InvalidKeyException( key = importKey(key);
"Unsupported key type: " + key.getClass().getName() + SymmetricKey symkey = ((SecretKeyFacade)key).key;
". Only JSS keys are supported."); return symkey.getLength();
} }
} }