summaryrefslogtreecommitdiff
path: root/gnu/java/security/jce/sig
diff options
context:
space:
mode:
authorGuilhem Lavaux <guilhem@kaffe.org>2006-02-18 18:33:27 +0000
committerGuilhem Lavaux <guilhem@kaffe.org>2006-02-18 18:33:27 +0000
commitcc0db4315db5e0550da9a4a13f4b5e502e58a5c2 (patch)
tree25a49d0fc9588fae44f8e5edfc496f612a316931 /gnu/java/security/jce/sig
parent5093bf804963cd90f1c6c6053c0417f78b945ca6 (diff)
downloadclasspath-cc0db4315db5e0550da9a4a13f4b5e502e58a5c2.tar.gz
Merged with HEAD.
Diffstat (limited to 'gnu/java/security/jce/sig')
-rw-r--r--gnu/java/security/jce/sig/DSSKeyFactory.java236
-rw-r--r--gnu/java/security/jce/sig/DSSKeyPairGeneratorSpi.java6
-rw-r--r--gnu/java/security/jce/sig/EncodedKeyFactory.java423
-rw-r--r--gnu/java/security/jce/sig/KeyPairGeneratorAdapter.java4
-rw-r--r--gnu/java/security/jce/sig/RSAKeyFactory.java265
-rw-r--r--gnu/java/security/jce/sig/RSAKeyPairGeneratorSpi.java6
6 files changed, 936 insertions, 4 deletions
diff --git a/gnu/java/security/jce/sig/DSSKeyFactory.java b/gnu/java/security/jce/sig/DSSKeyFactory.java
new file mode 100644
index 000000000..818d0513d
--- /dev/null
+++ b/gnu/java/security/jce/sig/DSSKeyFactory.java
@@ -0,0 +1,236 @@
+/* DSSKeyFactory.java -- JCE DSA key factory Adapter
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.security.jce.sig;
+
+import gnu.java.security.Registry;
+import gnu.java.security.key.dss.DSSKeyPairPKCS8Codec;
+import gnu.java.security.key.dss.DSSKeyPairX509Codec;
+import gnu.java.security.key.dss.DSSPrivateKey;
+import gnu.java.security.key.dss.DSSPublicKey;
+
+import java.math.BigInteger;
+import java.security.InvalidKeyException;
+import java.security.Key;
+import java.security.KeyFactorySpi;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+import java.security.interfaces.DSAPrivateKey;
+import java.security.interfaces.DSAPublicKey;
+import java.security.spec.DSAPrivateKeySpec;
+import java.security.spec.DSAPublicKeySpec;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.KeySpec;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.X509EncodedKeySpec;
+
+/**
+ * DSA key factory.
+ *
+ * @author Casey Marshall (rsdio@metastatic.org)
+ */
+public class DSSKeyFactory extends KeyFactorySpi
+{
+ // implicit 0-arguments constructor
+
+ protected PublicKey engineGeneratePublic(KeySpec keySpec)
+ throws InvalidKeySpecException
+ {
+ if (keySpec instanceof DSAPublicKeySpec)
+ {
+ DSAPublicKeySpec spec = (DSAPublicKeySpec) keySpec;
+ BigInteger p = spec.getP();
+ BigInteger q = spec.getQ();
+ BigInteger g = spec.getG();
+ BigInteger y = spec.getY();
+ return new DSSPublicKey(Registry.X509_ENCODING_ID, p, q, g, y);
+ }
+
+ if (keySpec instanceof X509EncodedKeySpec)
+ {
+ X509EncodedKeySpec spec = (X509EncodedKeySpec) keySpec;
+ byte[] encoded = spec.getEncoded();
+ PublicKey result;
+ try
+ {
+ result = new DSSKeyPairX509Codec().decodePublicKey(encoded);
+ }
+ catch (RuntimeException x)
+ {
+ InvalidKeySpecException y = new InvalidKeySpecException();
+ y.initCause(x);
+ throw y;
+ }
+ }
+
+ throw new InvalidKeySpecException("Unsupported (public) key specification");
+ }
+
+ protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
+ throws InvalidKeySpecException
+ {
+ if (keySpec instanceof DSAPrivateKeySpec)
+ {
+ DSAPrivateKeySpec spec = (DSAPrivateKeySpec) keySpec;
+ BigInteger p = spec.getP();
+ BigInteger q = spec.getQ();
+ BigInteger g = spec.getG();
+ BigInteger x = spec.getX();
+ return new DSSPrivateKey(Registry.PKCS8_ENCODING_ID, p, q, g, x);
+ }
+
+ if (keySpec instanceof PKCS8EncodedKeySpec)
+ {
+ PKCS8EncodedKeySpec spec = (PKCS8EncodedKeySpec) keySpec;
+ byte[] encoded = spec.getEncoded();
+ PrivateKey result;
+ try
+ {
+ result = new DSSKeyPairPKCS8Codec().decodePrivateKey(encoded);
+ }
+ catch (RuntimeException x)
+ {
+ InvalidKeySpecException y = new InvalidKeySpecException();
+ y.initCause(x);
+ throw y;
+ }
+ }
+
+ throw new InvalidKeySpecException("Unsupported (private) key specification");
+ }
+
+ protected KeySpec engineGetKeySpec(Key key, Class keySpec)
+ throws InvalidKeySpecException
+ {
+ if (key instanceof DSAPublicKey)
+ {
+ if (keySpec.isAssignableFrom(DSAPublicKeySpec.class))
+ {
+ DSAPublicKey dsaKey = (DSAPublicKey) key;
+ BigInteger p = dsaKey.getParams().getP();
+ BigInteger q = dsaKey.getParams().getQ();
+ BigInteger g = dsaKey.getParams().getG();
+ BigInteger y = dsaKey.getY();
+ return new DSAPublicKeySpec(y, p, q, g);
+ }
+
+ if (keySpec.isAssignableFrom(X509EncodedKeySpec.class))
+ {
+ if (key instanceof DSSPublicKey)
+ {
+ DSSPublicKey dssKey = (DSSPublicKey) key;
+ byte[] encoded = dssKey.getEncoded(Registry.X509_ENCODING_ID);
+ return new X509EncodedKeySpec(encoded);
+ }
+
+ if (Registry.X509_ENCODING_SORT_NAME.equalsIgnoreCase(key.getFormat()))
+ {
+ byte[] encoded = key.getEncoded();
+ return new X509EncodedKeySpec(encoded);
+ }
+
+ throw new InvalidKeySpecException("Wrong key type or unsupported (public) key specification");
+ }
+
+ throw new InvalidKeySpecException("Unsupported (public) key specification");
+ }
+
+ if (key instanceof DSAPrivateKey)
+ {
+ if (keySpec.isAssignableFrom(DSAPrivateKeySpec.class))
+ {
+ DSAPrivateKey dsaKey = (DSAPrivateKey) key;
+ BigInteger p = dsaKey.getParams().getP();
+ BigInteger q = dsaKey.getParams().getQ();
+ BigInteger g = dsaKey.getParams().getG();
+ BigInteger x = dsaKey.getX();
+ return new DSAPrivateKeySpec(x, p, q, g);
+ }
+
+ if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class))
+ {
+ if (key instanceof DSSPrivateKey)
+ {
+ DSSPrivateKey dssKey = (DSSPrivateKey) key;
+ byte[] encoded = dssKey.getEncoded(Registry.PKCS8_ENCODING_ID);
+ return new PKCS8EncodedKeySpec(encoded);
+ }
+
+ if (Registry.PKCS8_ENCODING_SHORT_NAME.equalsIgnoreCase(key.getFormat()))
+ {
+ byte[] encoded = key.getEncoded();
+ return new PKCS8EncodedKeySpec(encoded);
+ }
+
+ throw new InvalidKeySpecException("Wrong key type or unsupported (private) key specification");
+ }
+
+ throw new InvalidKeySpecException("Unsupported (private) key specification");
+ }
+
+ throw new InvalidKeySpecException("Wrong key type or unsupported key specification");
+ }
+
+ protected Key engineTranslateKey(Key key) throws InvalidKeyException
+ {
+ if ((key instanceof DSSPublicKey) || (key instanceof DSSPrivateKey))
+ return key;
+
+ if (key instanceof DSAPublicKey)
+ {
+ DSAPublicKey dsaKey = (DSAPublicKey) key;
+ BigInteger p = dsaKey.getParams().getP();
+ BigInteger q = dsaKey.getParams().getQ();
+ BigInteger g = dsaKey.getParams().getG();
+ BigInteger y = dsaKey.getY();
+ return new DSSPublicKey(Registry.X509_ENCODING_ID, p, q, g, y);
+ }
+
+ if (key instanceof DSAPrivateKey)
+ {
+ DSAPrivateKey dsaKey = (DSAPrivateKey) key;
+ BigInteger p = dsaKey.getParams().getP();
+ BigInteger q = dsaKey.getParams().getQ();
+ BigInteger g = dsaKey.getParams().getG();
+ BigInteger x = dsaKey.getX();
+ return new DSSPrivateKey(Registry.PKCS8_ENCODING_ID, p, q, g, x);
+ }
+
+ throw new InvalidKeyException("Wrong key type");
+ }
+}
diff --git a/gnu/java/security/jce/sig/DSSKeyPairGeneratorSpi.java b/gnu/java/security/jce/sig/DSSKeyPairGeneratorSpi.java
index 2f4d36d51..7605522e1 100644
--- a/gnu/java/security/jce/sig/DSSKeyPairGeneratorSpi.java
+++ b/gnu/java/security/jce/sig/DSSKeyPairGeneratorSpi.java
@@ -58,7 +58,7 @@ import java.util.HashMap;
* a call to an <code>initialize()</code> method), the GNU Crypto provider
* uses a default <i>modulus</i> size (keysize) of 1024 bits.<p>
*
- * @version $Revision: 1.1.2.1 $
+ * @version $Revision: 1.1.2.2 $
*/
public class DSSKeyPairGeneratorSpi extends KeyPairGeneratorAdapter implements
DSAKeyPairGenerator
@@ -105,6 +105,8 @@ public class DSSKeyPairGeneratorSpi extends KeyPairGeneratorAdapter implements
attributes.put(DSSKeyPairGenerator.SOURCE_OF_RANDOMNESS, random);
}
+ attributes.put(DSSKeyPairGenerator.PREFERRED_ENCODING_FORMAT,
+ new Integer(Registry.ASN1_ENCODING_ID));
try
{
adaptee.setup(attributes);
@@ -151,6 +153,8 @@ public class DSSKeyPairGeneratorSpi extends KeyPairGeneratorAdapter implements
attributes.put(DSSKeyPairGenerator.USE_DEFAULTS,
Boolean.valueOf(!genParams));
attributes.put(DSSKeyPairGenerator.STRICT_DEFAULTS, Boolean.TRUE);
+ attributes.put(DSSKeyPairGenerator.PREFERRED_ENCODING_FORMAT,
+ new Integer(Registry.ASN1_ENCODING_ID));
try
{
adaptee.setup(attributes);
diff --git a/gnu/java/security/jce/sig/EncodedKeyFactory.java b/gnu/java/security/jce/sig/EncodedKeyFactory.java
new file mode 100644
index 000000000..6c1a19abd
--- /dev/null
+++ b/gnu/java/security/jce/sig/EncodedKeyFactory.java
@@ -0,0 +1,423 @@
+/* EncodedKeyFactory.java -- JCE Encoded key factory Adapter
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.security.jce.sig;
+
+import gnu.java.security.Registry;
+import gnu.java.security.key.dss.DSSPrivateKey;
+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;
+import java.security.Key;
+import java.security.KeyFactorySpi;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+import java.security.spec.DSAPrivateKeySpec;
+import java.security.spec.DSAPublicKeySpec;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.KeySpec;
+import java.security.spec.PKCS8EncodedKeySpec;
+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).
+ */
+public class EncodedKeyFactory
+ extends KeyFactorySpi
+{
+ // 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
+ {
+ if (keySpec instanceof DSAPublicKeySpec)
+ return decodeDSSPublicKey((DSAPublicKeySpec) keySpec);
+
+ 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");
+
+ byte[] input = ((X509EncodedKeySpec) keySpec).getEncoded();
+
+ // try DSS
+ try
+ {
+ return DSSPublicKey.valueOf(input);
+ }
+ catch (InvalidParameterException ignored)
+ {
+ }
+
+ // try RSA
+ try
+ {
+ return GnuRSAPublicKey.valueOf(input);
+ }
+ catch (InvalidParameterException ignored)
+ {
+ }
+
+ // try DH
+ return decodeDHPublicKey(input);
+ }
+
+ protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
+ throws InvalidKeySpecException
+ {
+ if (keySpec instanceof DSAPrivateKeySpec)
+ return decodeDSSPrivateKey((DSAPrivateKeySpec) keySpec);
+
+ 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");
+
+ byte[] input = ((PKCS8EncodedKeySpec) keySpec).getEncoded();
+
+ // try DSS
+ try
+ {
+ return DSSPrivateKey.valueOf(input);
+ }
+ catch (InvalidParameterException ignored)
+ {
+ }
+
+ // try RSA
+ try
+ {
+ return GnuRSAPrivateKey.valueOf(input);
+ }
+ catch (InvalidParameterException ignored)
+ {
+ }
+
+ // try DH
+ return decodeDHPrivateKey(input);
+ }
+
+ protected KeySpec engineGetKeySpec(Key key, Class keySpec)
+ throws InvalidKeySpecException
+ {
+ if (key instanceof PublicKey
+ && Registry.X509_ENCODING_SORT_NAME.equalsIgnoreCase(key.getFormat())
+ && keySpec.isAssignableFrom(X509EncodedKeySpec.class))
+ return new X509EncodedKeySpec(key.getEncoded());
+
+ if (key instanceof PrivateKey
+ && Registry.PKCS8_ENCODING_SHORT_NAME.equalsIgnoreCase(key.getFormat())
+ && keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class))
+ return new PKCS8EncodedKeySpec(key.getEncoded());
+
+ throw new InvalidKeySpecException("Unsupported format or invalid key spec class");
+ }
+
+ protected Key engineTranslateKey(Key key) throws InvalidKeyException
+ {
+ throw new InvalidKeyException("Key translation not supported");
+ }
+
+ /**
+ * @param spec an instance of {@link DSAPublicKeySpec} to decode.
+ * @return an instance of {@link DSSPublicKey} constructed from the
+ * information in the designated key-specification.
+ */
+ private DSSPublicKey decodeDSSPublicKey(DSAPublicKeySpec spec)
+ {
+ BigInteger p = spec.getP();
+ BigInteger q = spec.getQ();
+ BigInteger g = spec.getG();
+ BigInteger y = spec.getY();
+ return new DSSPublicKey(Registry.X509_ENCODING_ID, p, q, g, y);
+ }
+
+ /**
+ * @param spec an instance of {@link RSAPublicKeySpec} to decode.
+ * @return an instance of {@link GnuRSAPublicKey} constructed from the
+ * information in the designated key-specification.
+ */
+ private GnuRSAPublicKey decodeRSAPublicKey(RSAPublicKeySpec spec)
+ {
+ BigInteger n = spec.getModulus();
+ BigInteger e = spec.getPublicExponent();
+ return new GnuRSAPublicKey(Registry.X509_ENCODING_ID, n, e);
+ }
+
+ /**
+ * @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.
+ */
+ private PrivateKey decodeDSSPrivateKey(DSAPrivateKeySpec spec)
+ {
+ BigInteger p = spec.getP();
+ BigInteger q = spec.getQ();
+ BigInteger g = spec.getG();
+ BigInteger x = spec.getX();
+ return new DSSPrivateKey(Registry.PKCS8_ENCODING_ID, p, q, g, x);
+ }
+
+ /**
+ * @param spec an instance of {@link RSAPrivateCrtKeySpec} to decode.
+ * @return an instance of {@link GnuRSAPrivateKey} constructed from the
+ * information in the designated key-specification.
+ */
+ private PrivateKey decodeRSAPrivateKey(RSAPrivateCrtKeySpec spec)
+ {
+ BigInteger n = spec.getModulus();
+ BigInteger e = spec.getPublicExponent();
+ BigInteger d = spec.getPrivateExponent();
+ BigInteger p = spec.getPrimeP();
+ BigInteger q = spec.getPrimeQ();
+ BigInteger dP = spec.getPrimeExponentP();
+ BigInteger dQ = spec.getPrimeExponentQ();
+ BigInteger qInv = spec.getCrtCoefficient();
+ 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 b1dab1de8..a170b69a4 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.1.2.1 $
+ * @version $Revision: 1.1.2.2 $
*/
-abstract class KeyPairGeneratorAdapter extends KeyPairGenerator
+public abstract class KeyPairGeneratorAdapter extends KeyPairGenerator
{
// Constants and variables
diff --git a/gnu/java/security/jce/sig/RSAKeyFactory.java b/gnu/java/security/jce/sig/RSAKeyFactory.java
new file mode 100644
index 000000000..fecf54cb8
--- /dev/null
+++ b/gnu/java/security/jce/sig/RSAKeyFactory.java
@@ -0,0 +1,265 @@
+/* RSAKeyFactory.java -- RSA key-factory JCE Adapter
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.security.jce.sig;
+
+import gnu.java.security.Registry;
+import gnu.java.security.key.rsa.GnuRSAPrivateKey;
+import gnu.java.security.key.rsa.GnuRSAPublicKey;
+import gnu.java.security.key.rsa.RSAKeyPairPKCS8Codec;
+import gnu.java.security.key.rsa.RSAKeyPairX509Codec;
+
+import java.math.BigInteger;
+import java.security.InvalidKeyException;
+import java.security.Key;
+import java.security.KeyFactorySpi;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+import java.security.interfaces.RSAPrivateCrtKey;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.interfaces.RSAPublicKey;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.KeySpec;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.RSAPrivateCrtKeySpec;
+import java.security.spec.RSAPrivateKeySpec;
+import java.security.spec.RSAPublicKeySpec;
+import java.security.spec.X509EncodedKeySpec;
+
+public class RSAKeyFactory
+ extends KeyFactorySpi
+{
+ // implicit 0-arguments constructor
+
+ protected PublicKey engineGeneratePublic(KeySpec keySpec)
+ throws InvalidKeySpecException
+ {
+ if (keySpec instanceof RSAPublicKeySpec)
+ {
+ RSAPublicKeySpec spec = (RSAPublicKeySpec) keySpec;
+ BigInteger n = spec.getModulus();
+ BigInteger e = spec.getPublicExponent();
+ return new GnuRSAPublicKey(Registry.X509_ENCODING_ID, n, e);
+ }
+
+ if (keySpec instanceof X509EncodedKeySpec)
+ {
+ X509EncodedKeySpec spec = (X509EncodedKeySpec) keySpec;
+ byte[] encoded = spec.getEncoded();
+ PublicKey result;
+ try
+ {
+ result = new RSAKeyPairX509Codec().decodePublicKey(encoded);
+ }
+ catch (RuntimeException x)
+ {
+ InvalidKeySpecException y = new InvalidKeySpecException();
+ y.initCause(x);
+ throw y;
+ }
+ }
+
+ throw new InvalidKeySpecException("Unsupported (public) key specification");
+ }
+
+ protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
+ throws InvalidKeySpecException
+ {
+ if (keySpec instanceof RSAPrivateCrtKeySpec)
+ {
+ RSAPrivateCrtKeySpec spec = (RSAPrivateCrtKeySpec) keySpec;
+ BigInteger n = spec.getModulus();
+ BigInteger e = spec.getPublicExponent();
+ BigInteger d = spec.getPrivateExponent();
+ BigInteger p = spec.getPrimeP();
+ BigInteger q = spec.getPrimeQ();
+ BigInteger dP = spec.getPrimeExponentP();
+ BigInteger dQ = spec.getPrimeExponentQ();
+ BigInteger qInv = spec.getCrtCoefficient();
+ return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
+ n, e, d, p, q, dP, dQ, qInv);
+ }
+
+// if (keySpec instanceof RSAPrivateKeySpec)
+// {
+// RSAPrivateKeySpec spec = (RSAPrivateKeySpec) keySpec;
+// BigInteger n = spec.getModulus();
+// BigInteger d = spec.getPrivateExponent();
+// return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
+// n, null, d, null, null, null, null, null);
+// }
+
+ if (keySpec instanceof PKCS8EncodedKeySpec)
+ {
+ PKCS8EncodedKeySpec spec = (PKCS8EncodedKeySpec) keySpec;
+ byte[] encoded = spec.getEncoded();
+ PrivateKey result;
+ try
+ {
+ result = new RSAKeyPairPKCS8Codec().decodePrivateKey(encoded);
+ }
+ catch (RuntimeException x)
+ {
+ InvalidKeySpecException y = new InvalidKeySpecException();
+ y.initCause(x);
+ throw y;
+ }
+ }
+
+ throw new InvalidKeySpecException("Unsupported (private) key specification");
+ }
+
+ protected KeySpec engineGetKeySpec(Key key, Class keySpec)
+ throws InvalidKeySpecException
+ {
+ if (key instanceof RSAPublicKey)
+ {
+ if (keySpec.isAssignableFrom(RSAPublicKeySpec.class))
+ {
+ RSAPublicKey rsaKey = (RSAPublicKey) key;
+ BigInteger n = rsaKey.getModulus();
+ BigInteger e = rsaKey.getPublicExponent();
+ return new RSAPublicKeySpec(n, e);
+ }
+
+ if (keySpec.isAssignableFrom(X509EncodedKeySpec.class))
+ {
+ if (key instanceof GnuRSAPublicKey)
+ {
+ GnuRSAPublicKey rsaKey = (GnuRSAPublicKey) key;
+ byte[] encoded = rsaKey.getEncoded(Registry.X509_ENCODING_ID);
+ return new X509EncodedKeySpec(encoded);
+ }
+
+ if (Registry.X509_ENCODING_SORT_NAME.equalsIgnoreCase(key.getFormat()))
+ {
+ byte[] encoded = key.getEncoded();
+ return new X509EncodedKeySpec(encoded);
+ }
+
+ throw new InvalidKeySpecException("Wrong key type or unsupported (public) key specification");
+ }
+
+ throw new InvalidKeySpecException("Unsupported (public) key specification");
+ }
+
+ if ((key instanceof RSAPrivateCrtKey)
+ && keySpec.isAssignableFrom(RSAPrivateCrtKeySpec.class))
+ {
+ RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
+ BigInteger n = rsaKey.getModulus();
+ BigInteger e = rsaKey.getPublicExponent();
+ BigInteger d = rsaKey.getPrivateExponent();
+ BigInteger p = rsaKey.getPrimeP();
+ BigInteger q = rsaKey.getPrimeQ();
+ BigInteger dP = rsaKey.getPrimeExponentP();
+ BigInteger dQ = rsaKey.getPrimeExponentQ();
+ BigInteger qInv = rsaKey.getCrtCoefficient();
+ return new RSAPrivateCrtKeySpec(n, e, d, p, q, dP, dQ, qInv);
+ }
+
+ if ((key instanceof RSAPrivateKey)
+ && keySpec.isAssignableFrom(RSAPrivateKeySpec.class))
+ {
+ RSAPrivateKey rsaKey = (RSAPrivateKey) key;
+ BigInteger n = rsaKey.getModulus();
+ BigInteger d = rsaKey.getPrivateExponent();
+ return new RSAPrivateKeySpec(n, d);
+ }
+
+ if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class))
+ {
+ if (key instanceof GnuRSAPrivateKey)
+ {
+ GnuRSAPrivateKey rsaKey = (GnuRSAPrivateKey) key;
+ byte[] encoded = rsaKey.getEncoded(Registry.PKCS8_ENCODING_ID);
+ return new PKCS8EncodedKeySpec(encoded);
+ }
+
+ if (Registry.PKCS8_ENCODING_SHORT_NAME.equalsIgnoreCase(key.getFormat()))
+ {
+ byte[] encoded = key.getEncoded();
+ return new PKCS8EncodedKeySpec(encoded);
+ }
+
+ throw new InvalidKeySpecException("Wrong key type or unsupported (private) key specification");
+ }
+
+ throw new InvalidKeySpecException("Wrong key type or unsupported key specification");
+ }
+
+ protected Key engineTranslateKey(Key key) throws InvalidKeyException
+ {
+ if ((key instanceof GnuRSAPublicKey) || (key instanceof GnuRSAPrivateKey))
+ return key;
+
+ if (key instanceof RSAPublicKey)
+ {
+ RSAPublicKey rsaKey = (RSAPublicKey) key;
+ BigInteger n = rsaKey.getModulus();
+ BigInteger e = rsaKey.getPublicExponent();
+ return new GnuRSAPublicKey(Registry.X509_ENCODING_ID, n, e);
+ }
+
+ if (key instanceof RSAPrivateCrtKey)
+ {
+ RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
+ BigInteger n = rsaKey.getModulus();
+ BigInteger e = rsaKey.getPublicExponent();
+ BigInteger d = rsaKey.getPrivateExponent();
+ BigInteger p = rsaKey.getPrimeP();
+ BigInteger q = rsaKey.getPrimeQ();
+ BigInteger dP = rsaKey.getPrimeExponentP();
+ BigInteger dQ = rsaKey.getPrimeExponentQ();
+ BigInteger qInv = rsaKey.getCrtCoefficient();
+ return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
+ n, e, d, p, q, dP, dQ, qInv);
+ }
+
+// if (key instanceof RSAPrivateKey)
+// {
+// RSAPrivateKey rsaKey = (RSAPrivateKey) key;
+// BigInteger n = rsaKey.getModulus();
+// BigInteger d = rsaKey.getPrivateExponent();
+// return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
+// n, null, d, null, null, null, null, null);
+// }
+
+ throw new InvalidKeyException("Unsupported key type");
+ }
+}
diff --git a/gnu/java/security/jce/sig/RSAKeyPairGeneratorSpi.java b/gnu/java/security/jce/sig/RSAKeyPairGeneratorSpi.java
index 54783eacf..24dc7c501 100644
--- a/gnu/java/security/jce/sig/RSAKeyPairGeneratorSpi.java
+++ b/gnu/java/security/jce/sig/RSAKeyPairGeneratorSpi.java
@@ -1,4 +1,4 @@
-/* RSAKeyPairGeneratorSpi.java --
+/* RSAKeyPairGeneratorSpi.java -- JCE RSA KeyPairGenerator Adapter
Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
This file is a part of GNU Classpath.
@@ -84,6 +84,8 @@ public class RSAKeyPairGeneratorSpi extends KeyPairGeneratorAdapter
attributes.put(RSAKeyPairGenerator.SOURCE_OF_RANDOMNESS, random);
}
+ attributes.put(RSAKeyPairGenerator.PREFERRED_ENCODING_FORMAT,
+ new Integer(Registry.ASN1_ENCODING_ID));
adaptee.setup(attributes);
}
@@ -106,6 +108,8 @@ public class RSAKeyPairGeneratorSpi extends KeyPairGeneratorAdapter
attributes.put(RSAKeyPairGenerator.SOURCE_OF_RANDOMNESS, random);
}
+ attributes.put(RSAKeyPairGenerator.PREFERRED_ENCODING_FORMAT,
+ new Integer(Registry.ASN1_ENCODING_ID));
adaptee.setup(attributes);
}
}