diff options
| author | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2015-02-10 02:01:19 +0000 |
|---|---|---|
| committer | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2015-02-10 02:01:19 +0000 |
| commit | a09acb2a1f9dc481c980ab38e19f510581e43ea8 (patch) | |
| tree | 9d92ed44029bb9fbaa368fa9df681f11bb809e31 /gnu/java/security/key/rsa/RSAKeyPairGenerator.java | |
| parent | 55c5097f9ce57cb5ebdbec6383c6c89ad4933348 (diff) | |
| download | classpath-a09acb2a1f9dc481c980ab38e19f510581e43ea8.tar.gz | |
PR64902: Keys returned by KeyPairGenerator don't use standardised algorithm names
PR64904: KeyPairGenerator.genKeyPair() fails if not explicitly initialised
2015-02-02 Andrew John Hughes <gnu_andrew@member.fsf.org>
PR classpath/64902
PR classpath/64904
* NEWS: Updated.
* gnu/java/security/jce/sig/KeyPairGeneratorAdapter.java:
(KeyPairGeneratorAdapter(String)): Filter incoming generator
names so all standardised names are handled (e.g. the
DiffieHellman alias for DH.
(generateKeyPair()): Check whether the generator has been
initialized and initialize it with defaults if not.
(getAlgorithm()): Return the standardised name, not the
internal one.
(localiseName(String)): Convert requested standardised
name to the internal equivalent.
* gnu/java/security/key/IKeyPairGenerator.java:
(isInitialized()): New method to check whether the generator
has been initialized.
(getDefaultKeySize()): Return the default key size used
by the generator.
* gnu/java/security/key/dss/DSSKey.java:
(getAlgorithm()): Return standard "DSA" rather than "dsa".
* gnu/java/security/key/dss/DSSKeyPairGenerator.java:
(initialized): Flag to indicate whether the generator has
been initialized or not.
(initLock): Lock to prevent multiple concurrent initializations.
(setup(Map)): Wrap initialization in a lock and set initialized
flag when done.
(isInitialized()): Returns the value of the initialized flag.
(getDefaultKeySize()): Returns the default key size.
* gnu/java/security/key/rsa/GnuRSAKey.java:
(getAlgorithm()): Return standard "RSA" rather than "rsa".
* gnu/java/security/key/rsa/RSAKeyPairGenerator.java:
(initialized): Flag to indicate whether the generator has
been initialized or not.
(initLock): Lock to prevent multiple concurrent initializations.
(setup(Map)): Wrap initialization in a lock and set initialized
flag when done.
(isInitialized()): Returns the value of the initialized flag.
(getDefaultKeySize()): Returns the default key size.
* gnu/javax/crypto/key/dh/GnuDHKey.java:
(getAlgorithm()): Return standard "DH" rather than "dh".
* gnu/javax/crypto/key/dh/GnuDHKeyPairGenerator.java:
(initialized): Flag to indicate whether the generator has
been initialized or not.
(initLock): Lock to prevent multiple concurrent initializations.
(setup(Map)): Wrap initialization in a lock and set initialized
flag when done.
(isInitialized()): Returns the value of the initialized flag.
(getDefaultKeySize()): Returns the default key size.
* gnu/javax/crypto/key/srp6/SRPKeyPairGenerator.java:
(initialized): Flag to indicate whether the generator has
been initialized or not.
(initLock): Lock to prevent multiple concurrent initializations.
(setup(Map)): Wrap initialization in a lock and set initialized
flag when done.
(isInitialized()): Returns the value of the initialized flag.
(getDefaultKeySize()): Returns the default key size.
Diffstat (limited to 'gnu/java/security/key/rsa/RSAKeyPairGenerator.java')
| -rw-r--r-- | gnu/java/security/key/rsa/RSAKeyPairGenerator.java | 80 |
1 files changed, 61 insertions, 19 deletions
diff --git a/gnu/java/security/key/rsa/RSAKeyPairGenerator.java b/gnu/java/security/key/rsa/RSAKeyPairGenerator.java index a7fc21efe..5f321ce5b 100644 --- a/gnu/java/security/key/rsa/RSAKeyPairGenerator.java +++ b/gnu/java/security/key/rsa/RSAKeyPairGenerator.java @@ -1,5 +1,5 @@ /* RSAKeyPairGenerator.java -- - Copyright 2001, 2002, 2003, 2006, 2010, 2014 Free Software Foundation, Inc. + Copyright 2001, 2002, 2003, 2006, 2010, 2014, 2015 Free Software Foundation, Inc. This file is a part of GNU Classpath. @@ -50,6 +50,9 @@ import java.security.PublicKey; import java.security.SecureRandom; import java.security.spec.RSAKeyGenParameterSpec; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Logger; /** @@ -127,6 +130,12 @@ public class RSAKeyPairGenerator /** Preferred encoding format of generated keys. */ private int preferredFormat; + /** Flag to indicate whether the generator has been initialized */ + private AtomicBoolean initialized = new AtomicBoolean(false); + + /** Lock to prevent concurrent initialization */ + private Lock initLock = new ReentrantLock(); + // implicit 0-arguments constructor @Override @@ -147,28 +156,38 @@ public class RSAKeyPairGenerator { if (Configuration.DEBUG) log.entering(this.getClass().getName(), "setup", attributes); - // do we have a SecureRandom, or should we use our own? - rnd = (SecureRandom) attributes.get(SOURCE_OF_RANDOMNESS); - // are we given a set of RSA params or we shall use our own? - RSAKeyGenParameterSpec params = (RSAKeyGenParameterSpec) attributes.get(RSA_PARAMETERS); - // find out the modulus length - if (params != null) + initLock.lock(); + try { - L = params.getKeysize(); - e = params.getPublicExponent(); + // do we have a SecureRandom, or should we use our own? + rnd = (SecureRandom) attributes.get(SOURCE_OF_RANDOMNESS); + // are we given a set of RSA params or we shall use our own? + RSAKeyGenParameterSpec params = (RSAKeyGenParameterSpec) attributes.get(RSA_PARAMETERS); + // find out the modulus length + if (params != null) + { + L = params.getKeysize(); + e = params.getPublicExponent(); + } + else + { + Integer l = (Integer) attributes.get(MODULUS_LENGTH); + L = (l == null ? DEFAULT_MODULUS_LENGTH : l.intValue()); + } + if (L < 1024) + throw new IllegalArgumentException("Invalid modulus size"); + + // what is the preferred encoding format + Integer formatID = (Integer) attributes.get(PREFERRED_ENCODING_FORMAT); + preferredFormat = formatID == null ? DEFAULT_ENCODING_FORMAT + : formatID.intValue(); + + initialized.set(true); } - else + finally { - Integer l = (Integer) attributes.get(MODULUS_LENGTH); - L = (l == null ? DEFAULT_MODULUS_LENGTH : l.intValue()); + initLock.unlock(); } - if (L < 1024) - throw new IllegalArgumentException(MODULUS_LENGTH); - - // what is the preferred encoding format - Integer formatID = (Integer) attributes.get(PREFERRED_ENCODING_FORMAT); - preferredFormat = formatID == null ? DEFAULT_ENCODING_FORMAT - : formatID.intValue(); if (Configuration.DEBUG) log.exiting(this.getClass().getName(), "setup"); } @@ -247,4 +266,27 @@ public class RSAKeyPairGenerator return prng; } + + @Override + public boolean isInitialized() + { + boolean initFlag = false; + + initLock.lock(); + try + { + initFlag = initialized.get(); + } + finally + { + initLock.unlock(); + } + return initFlag; + } + + @Override + public int getDefaultKeySize() + { + return DEFAULT_MODULUS_LENGTH; + } } |
