From 97011b85926d80583a925d93475cd9a7adf3c876 Mon Sep 17 00:00:00 2001 From: "nicolson%netscape.com" Date: Tue, 15 Apr 2003 21:55:53 +0000 Subject: [PATCH] Catch NULL pointers before passing them to C and risking a crash. Improve javadoc. --- .../jss/SecretDecoderRing/KeyManager.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/security/jss/org/mozilla/jss/SecretDecoderRing/KeyManager.java b/security/jss/org/mozilla/jss/SecretDecoderRing/KeyManager.java index b25b741f5c0d..8cd2ca7896c6 100644 --- a/security/jss/org/mozilla/jss/SecretDecoderRing/KeyManager.java +++ b/security/jss/org/mozilla/jss/SecretDecoderRing/KeyManager.java @@ -57,7 +57,7 @@ public class KeyManager { KeyGenAlgorithm.DES3; /** - * The default key size. This is only relevant for algorithms + * The default key size (in bytes). This is only relevant for algorithms * with variable-length keys, such as AES. */ public static final int DEFAULT_KEYSIZE = 0; @@ -69,6 +69,9 @@ public class KeyManager { * @param token The token on which this KeyManager operates. */ public KeyManager(CryptoToken token) { + if( token == null ) { + throw new NullPointerException("token is null"); + } this.token = token; } @@ -87,6 +90,9 @@ public class KeyManager { /** * Generates an SDR key with the given algorithm and key size. + * @param keySize Length of key in bytes. This is only relevant for + * algorithms that take more than one key size. Otherwise it can just + * be set to 0. * @return The keyID of the generated key. A random keyID will be chosen * that is not currently used on the token. The keyID must be stored * by the application in order to use this key for encryption in the @@ -95,11 +101,17 @@ public class KeyManager { public byte[] generateKey(KeyGenAlgorithm alg, int keySize) throws TokenException { + if( alg == null ) { + throw new NullPointerException("alg is null"); + } byte[] keyID = generateUnusedKeyID(); generateKeyNative(token, alg, keyID, keySize); return keyID; } + /** + * @param keySize Key length in bytes. + */ private native void generateKeyNative(CryptoToken token, KeyGenAlgorithm alg, byte[] keyID, int keySize); @@ -140,6 +152,9 @@ public class KeyManager { public SecretKey lookupKey(EncryptionAlgorithm alg, byte[] keyid) throws TokenException { + if( alg == null || keyid == null ) { + throw new NullPointerException(); + } SymmetricKey k = lookupKeyNative(token, alg, keyid); if( k == null ) { return null; @@ -169,6 +184,9 @@ public class KeyManager { public void deleteKey(SecretKey key) throws TokenException, InvalidKeyException { + if( key == null ) { + throw new NullPointerException(); + } if( ! (key instanceof SecretKeyFacade) ) { throw new InvalidKeyException("Key must be a JSS key"); }