diff options
| author | Raif S. Naffah <raif@swiftdsl.com.au> | 2006-02-12 08:57:57 +0000 |
|---|---|---|
| committer | Raif S. Naffah <raif@swiftdsl.com.au> | 2006-02-12 08:57:57 +0000 |
| commit | 39b9875ed3e533f0597f7d8f60cae494a516581e (patch) | |
| tree | 97cbf58ef4170028b6cd5506f18e65d21503b869 /gnu/java/security/jce | |
| parent | f87886ecabe41379d04e44eede5f07de84ba1277 (diff) | |
| download | classpath-39b9875ed3e533f0597f7d8f60cae494a516581e.tar.gz | |
2006-02-12 Raif S. Naffah <raif@swiftdsl.com.au>
* gnu/javax/crypto/key/dh/GnuDHPublicKey.java
(GnuDHPublicKey(4)): Call constructor with 5 arguments.
(GnuDHPublicKey): New constructor.
(getEncoded): Removed.
(valueOf): Added support for ASN.1 encoding.
(getEncoded(int)): Likewise.
(equals): New method.
* gnu/javax/crypto/key/dh/GnuDHPrivateKey.java
(GnuDHPrivateKey(4)): Call constructor with 5 arguments.
(GnuDHPrivateKey(5)): New constructor.
(getEncoded): Removed.
(valueOf): Added support for ASN.1 encoding.
(getEncoded(int)): Likewise.
(equals): New method.
* gnu/javax/crypto/key/dh/GnuDHKeyPairGenerator.java
(PREFERRED_ENCODING_FORMAT): New constant.
(DEFAULT_ENCODING_FORMAT): Likewise.
(preferredFormat): New field.
(setup): Handle preferred encoding format identifier.
(generate): Call constructors with format identifier.
* gnu/javax/crypto/key/dh/GnuDHKey.java (defaultFormat): New field.
(GnuDHKey): Added an int argument.
(getEncoded): New method.
(getFormat): New implementation.
(getEncoded(int)): New abstract method.
* gnu/javax/crypto/key/dh/DHKeyPairX509Codec.java: New file.
* gnu/javax/crypto/key/dh/DHKeyPairPKCS8Codec.java: Likewise.
* gnu/javax/crypto/jce/GnuCrypto.java (run): Added mappings for DH
key-pair generator and key-factory.
* gnu/javax/crypto/jce/sig/DHKeyPairGeneratorSpi.java: New file.
* gnu/javax/crypto/jce/sig/DHKeyFactory.java: Likewise.
* gnu/java/security/jce/sig/KeyPairGeneratorAdapter.java: Made it public.
* gnu/java/security/jce/sig/EncodedKeyFactory.java
(invokeConstructor): New method.
(getConcreteClass): Likewise.
(getConcreteCtor): Likewise.
(invokeValueOf): Likewise.
(getValueOfMethod): Likewise.
(engineGeneratePublic): Add support for DH keys.
(engineGeneratePrivate): Likewise.
(decodeDHPublicKey(DHPublicKeySpec)): New method.
(decodeDHPublicKey(byte[])): Likewise.
(decodeDHPrivateKey(DHPrivateKeySpec)): Likewise.
(decodeDHPrivateKey(byte[])): Likewise.
Diffstat (limited to 'gnu/java/security/jce')
| -rw-r--r-- | gnu/java/security/jce/sig/EncodedKeyFactory.java | 210 | ||||
| -rw-r--r-- | gnu/java/security/jce/sig/KeyPairGeneratorAdapter.java | 4 |
2 files changed, 206 insertions, 8 deletions
diff --git a/gnu/java/security/jce/sig/EncodedKeyFactory.java b/gnu/java/security/jce/sig/EncodedKeyFactory.java index 6206c0021..6c1a19abd 100644 --- a/gnu/java/security/jce/sig/EncodedKeyFactory.java +++ b/gnu/java/security/jce/sig/EncodedKeyFactory.java @@ -44,6 +44,9 @@ import gnu.java.security.key.dss.DSSPublicKey; import gnu.java.security.key.rsa.GnuRSAPrivateKey; import gnu.java.security.key.rsa.GnuRSAPublicKey; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.InvalidParameterException; @@ -60,6 +63,11 @@ import java.security.spec.RSAPrivateCrtKeySpec; import java.security.spec.RSAPublicKeySpec; import java.security.spec.X509EncodedKeySpec; +import javax.crypto.interfaces.DHPrivateKey; +import javax.crypto.interfaces.DHPublicKey; +import javax.crypto.spec.DHPrivateKeySpec; +import javax.crypto.spec.DHPublicKeySpec; + /** * A factory for keys encoded in either the X.509 format (for public keys) or * the PKCS#8 format (for private keys). @@ -69,6 +77,118 @@ public class EncodedKeyFactory { // implicit 0-arguments constructor + // Class methods + // -------------------------------------------------------------------------- + + private static Object invokeConstructor(String className, Object[] params) + throws InvalidKeySpecException + { + Class clazz = getConcreteClass(className); + try + { + Constructor ctor = getConcreteCtor(clazz); + Object result = ctor.newInstance(params); + return result; + } + catch (InstantiationException x) + { + InvalidKeySpecException y = new InvalidKeySpecException(); + y.initCause(x); + throw y; + } + catch (IllegalAccessException x) + { + InvalidKeySpecException y = new InvalidKeySpecException(); + y.initCause(y); + throw y; + } + catch (InvocationTargetException x) + { + InvalidKeySpecException y = new InvalidKeySpecException(); + y.initCause(x); + throw y; + } + } + + private static Class getConcreteClass(String className) + throws InvalidKeySpecException + { + try + { + Class result = Class.forName(className); + return result; + } + catch (ClassNotFoundException x) + { + InvalidKeySpecException y = new InvalidKeySpecException(); + y.initCause(x); + throw y; + } + } + + private static Constructor getConcreteCtor(Class clazz) + throws InvalidKeySpecException + { + try + { + Constructor result = clazz.getConstructor(new Class[] {int.class, + BigInteger.class, + BigInteger.class, + BigInteger.class, + BigInteger.class}); + return result; + } + catch (NoSuchMethodException x) + { + InvalidKeySpecException y = new InvalidKeySpecException(); + y.initCause(x); + throw y; + } + } + + private static Object invokeValueOf(String className, byte[] encoded) + throws InvalidKeySpecException + { + Class clazz = getConcreteClass(className); + try + { + Method valueOf = getValueOfMethod(clazz); + Object result = valueOf.invoke(null, new Object[] { encoded }); + return result; + } + catch (IllegalAccessException x) + { + InvalidKeySpecException y = new InvalidKeySpecException(); + y.initCause(x); + throw y; + } + catch (InvocationTargetException x) + { + InvalidKeySpecException y = new InvalidKeySpecException(); + y.initCause(x); + throw y; + } + } + + private static Method getValueOfMethod(Class clazz) + throws InvalidKeySpecException + { + try + { + Method result = clazz.getMethod("valueOf", new Class[] {byte[].class}); + return result; + } + catch (NoSuchMethodException x) + { + InvalidKeySpecException y = new InvalidKeySpecException(); + y.initCause(x); + throw y; + } + } + + // Instance methods + // -------------------------------------------------------------------------- + protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException { @@ -78,6 +198,9 @@ public class EncodedKeyFactory if (keySpec instanceof RSAPublicKeySpec) return decodeRSAPublicKey((RSAPublicKeySpec) keySpec); + if (keySpec instanceof DHPublicKeySpec) + return decodeDHPublicKey((DHPublicKeySpec) keySpec); + if (! (keySpec instanceof X509EncodedKeySpec)) throw new InvalidKeySpecException("Unsupported key specification"); @@ -101,9 +224,8 @@ public class EncodedKeyFactory { } - // FIXME: try DH - - throw new InvalidKeySpecException(); + // try DH + return decodeDHPublicKey(input); } protected PrivateKey engineGeneratePrivate(KeySpec keySpec) @@ -115,6 +237,9 @@ public class EncodedKeyFactory if (keySpec instanceof RSAPrivateCrtKeySpec) return decodeRSAPrivateKey((RSAPrivateCrtKeySpec) keySpec); + if (keySpec instanceof DHPrivateKeySpec) + return decodeDHPrivateKey((DHPrivateKeySpec) keySpec); + if (! (keySpec instanceof PKCS8EncodedKeySpec)) throw new InvalidKeySpecException("Unsupported key specification"); @@ -138,9 +263,8 @@ public class EncodedKeyFactory { } - // FIXME: try DH - - throw new InvalidKeySpecException(); + // try DH + return decodeDHPrivateKey(input); } protected KeySpec engineGetKeySpec(Key key, Class keySpec) @@ -191,6 +315,43 @@ public class EncodedKeyFactory } /** + * @param spec an instance of {@link DHPublicKeySpec} to decode. + * @return an instance of a {@link DHPublicKey} constructed from the + * information in the designated key-specification. + * @throws InvalidKeySpecException if no concrete implementation of the + * {@link DHPublicKey} interface exists at run-time, or if an + * exception occurs during its instantiation. + */ + private DHPublicKey decodeDHPublicKey(DHPublicKeySpec spec) + throws InvalidKeySpecException + { + BigInteger p = spec.getP(); + BigInteger g = spec.getG(); + BigInteger y = spec.getY(); + Object[] params = new Object[] {new Integer(Registry.X509_ENCODING_ID), + null, p, g, y}; + Object obj = invokeConstructor("gnu.javax.crypto.key.dh.GnuDHPublicKey", + params); + return (DHPublicKey) obj; + } + + /** + * @param encoded the bytes to decode. + * @return an instance of a {@link DHPublicKey} constructed from the + * information in the designated key-specification. + * @throws InvalidKeySpecException if no concrete implementation of the + * {@link DHPublicKey} interface exists at run-time, or if an + * exception occurs during its instantiation. + */ + private DHPublicKey decodeDHPublicKey(byte[] encoded) + throws InvalidKeySpecException + { + Object obj = invokeValueOf("gnu.javax.crypto.key.dh.GnuDHPublicKey", + encoded); + return (DHPublicKey) obj; + } + + /** * @param spec an instance of {@link DSAPrivateKeySpec} to decode. * @return an instance of {@link DSSPrivateKey} constructed from the * information in the designated key-specification. @@ -222,4 +383,41 @@ public class EncodedKeyFactory return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID, n, e, d, p, q, dP, dQ, qInv); } + + /** + * @param spec an instance of {@link DHPrivateKeySpec} to decode. + * @return an instance of a {@link DHPrivateKey} constructed from the + * information in the designated key-specification. + * @throws InvalidKeySpecException if no concrete implementation of the + * {@link DHPrivateKey} interface exists at run-time, or if an + * exception occurs during its instantiation. + */ + private DHPrivateKey decodeDHPrivateKey(DHPrivateKeySpec spec) + throws InvalidKeySpecException + { + BigInteger p = spec.getP(); + BigInteger g = spec.getG(); + BigInteger x = spec.getX(); + Object[] params = new Object[] {new Integer(Registry.PKCS8_ENCODING_ID), + null, p, g, x}; + Object obj = invokeConstructor("gnu.javax.crypto.key.dh.GnuDHPrivateKey", + params); + return (DHPrivateKey) obj; + } + + /** + * @param encoded the bytes to decode. + * @return an instance of a {@link DHPrivateKey} constructed from the + * information in the designated key-specification. + * @throws InvalidKeySpecException if no concrete implementation of the + * {@link DHPrivateKey} interface exists at run-time, or if an + * exception occurs during its instantiation. + */ + private DHPrivateKey decodeDHPrivateKey(byte[] encoded) + throws InvalidKeySpecException + { + Object obj = invokeValueOf("gnu.javax.crypto.key.dh.GnuDHPrivateKey", + encoded); + return (DHPrivateKey) obj; + } } diff --git a/gnu/java/security/jce/sig/KeyPairGeneratorAdapter.java b/gnu/java/security/jce/sig/KeyPairGeneratorAdapter.java index 8718218b9..602682991 100644 --- a/gnu/java/security/jce/sig/KeyPairGeneratorAdapter.java +++ b/gnu/java/security/jce/sig/KeyPairGeneratorAdapter.java @@ -64,9 +64,9 @@ import java.security.spec.AlgorithmParameterSpec; * Crypto provider uses a default <i>modulus</i> size (keysize) of 1024 bits for * the DSS (Digital Signature Standard) a.k.a <i>DSA</i>.<p> * - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ */ -abstract class KeyPairGeneratorAdapter extends KeyPairGenerator +public abstract class KeyPairGeneratorAdapter extends KeyPairGenerator { // Constants and variables |
