hello there,

this patch --already committed-- adds support for raw DSS and RSA 
encoding / decoding with JCE KeyFactory methods.  it also ensures that 
all format-related methods throw InvalidParameterException.


2006-02-11  Raif S. Naffah  <[EMAIL PROTECTED]>

        * gnu/java/security/jce/sig/EncodedKeyFactory.java
        (engineGeneratePublic): Added support for raw key-specifications.
        (engineGeneratePrivate): Likewise.
        (decodeDSSPublicKey): New method.
        (decodeRSAPublicKey): Likewise.
        (decodeDSSPrivateKey): Likewise.
        (decodeRSAPrivateKey): Likewise.
        * gnu/java/security/key/rsa/RSAKeyPairX509Codec.java
        (encodePrivateKey): Throw InvalidParameterException.
        (decodePublicKey): Likewise.
        (decodePrivateKey): Likewise.
        * gnu/java/security/key/rsa/RSAKeyPairPKCS8Codec.java
        (encodePublicKey): Likewise.
        (encodePrivateKey): Likewise.
        (decodePublicKey): Likewise.
        * gnu/java/security/key/dss/DSSKeyPairX509Codec.java
        (encodePrivateKey): Likewise.
        (decodePublicKey): Likewise.
        (decodePrivateKey): Likewise.
        * gnu/java/security/key/dss/DSSKeyPairPKCS8Codec.java
        (encodePublicKey): Likewise.
        (encodePrivateKey): Likewise.
        (decodePublicKey): Likewise.

a new Mauve test (gnu.testlet.gnu.java.security.jce.TestOfKeyFactory was 
added to test the above.


cheers;
rsn
Index: RSAKeyPairX509Codec.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/security/key/rsa/RSAKeyPairX509Codec.java,v
retrieving revision 1.1
diff -u -r1.1 RSAKeyPairX509Codec.java
--- RSAKeyPairX509Codec.java	9 Feb 2006 11:52:48 -0000	1.1
+++ RSAKeyPairX509Codec.java	11 Feb 2006 08:35:26 -0000
@@ -167,9 +167,12 @@
     return result;
   }

+  /**
+   * @throws InvalidParameterException ALWAYS.
+   */
   public byte[] encodePrivateKey(PrivateKey key)
   {
-    throw new IllegalArgumentException("Wrong format for private keys");
+    throw new InvalidParameterException("Wrong format for private keys");
   }

   /**
@@ -201,7 +204,7 @@

         OID algOID = (OID) derOID.getValue();
         if (! algOID.equals(RSA_ALG_OID))
-          throw new IllegalArgumentException("Unexpected OID: " + algOID);
+          throw new InvalidParameterException("Unexpected OID: " + algOID);

         DERValue val = der.read();
         if (! (val.getValue() instanceof BitString))
@@ -230,8 +233,11 @@
     return new GnuRSAPublicKey(Registry.X509_ENCODING_ID, n, e);
   }

+  /**
+   * @throws InvalidParameterException ALWAYS.
+   */
   public PrivateKey decodePrivateKey(byte[] input)
   {
-    throw new IllegalArgumentException("Wrong format for private keys");
+    throw new InvalidParameterException("Wrong format for private keys");
   }
 }
Index: RSAKeyPairPKCS8Codec.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/security/key/rsa/RSAKeyPairPKCS8Codec.java,v
retrieving revision 1.1
diff -u -r1.1 RSAKeyPairPKCS8Codec.java
--- RSAKeyPairPKCS8Codec.java	9 Feb 2006 11:52:48 -0000	1.1
+++ RSAKeyPairPKCS8Codec.java	11 Feb 2006 08:46:27 -0000
@@ -82,9 +82,12 @@
     return PKCS8_FORMAT;
   }

+  /**
+   * @throws InvalidParameterException ALWAYS.
+   */
   public byte[] encodePublicKey(PublicKey key)
   {
-    throw new IllegalArgumentException("Wrong format for public keys");
+    throw new InvalidParameterException("Wrong format for public keys");
   }

   /**
@@ -129,7 +132,7 @@
   public byte[] encodePrivateKey(PrivateKey key)
   {
     if (! (key instanceof GnuRSAPrivateKey))
-      throw new IllegalArgumentException("Wrong key type");
+      throw new InvalidParameterException("Wrong key type");

     GnuRSAPrivateKey pk = (GnuRSAPrivateKey) key;
     BigInteger n = pk.getN();
@@ -198,9 +201,12 @@
     return result;
   }

+  /**
+   * @throws InvalidParameterException ALWAYS.
+   */
   public PublicKey decodePublicKey(byte[] input)
   {
-    throw new IllegalArgumentException("Wrong format for public keys");
+    throw new InvalidParameterException("Wrong format for public keys");
   }

   /**
Index: DSSKeyPairX509Codec.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/security/key/dss/DSSKeyPairX509Codec.java,v
retrieving revision 1.1
diff -u -r1.1 DSSKeyPairX509Codec.java
--- DSSKeyPairX509Codec.java	7 Feb 2006 12:06:48 -0000	1.1
+++ DSSKeyPairX509Codec.java	11 Feb 2006 08:46:49 -0000
@@ -176,9 +176,12 @@
     return result;
   }

+  /**
+   * @throws InvalidParameterException ALWAYS.
+   */
   public byte[] encodePrivateKey(PrivateKey key)
   {
-    throw new IllegalArgumentException("Wrong format for private keys");
+    throw new InvalidParameterException("Wrong format for private keys");
   }

   /**
@@ -210,7 +213,7 @@

         OID algOID = (OID) derOID.getValue();
         if (! algOID.equals(DSA_ALG_OID))
-          throw new IllegalArgumentException("Unexpected OID: " + algOID);
+          throw new InvalidParameterException("Unexpected OID: " + algOID);

         DERValue derParams = der.read();
         checkIsConstructed(derParams, "Wrong DSS Parameters field");
@@ -246,8 +249,11 @@
     return new DSSPublicKey(Registry.X509_ENCODING_ID, p, q, g, y);
   }

+  /**
+   * @throws InvalidParameterException ALWAYS.
+   */
   public PrivateKey decodePrivateKey(byte[] input)
   {
-    throw new IllegalArgumentException("Wrong format for private keys");
+    throw new InvalidParameterException("Wrong format for private keys");
   }
 }
Index: DSSKeyPairPKCS8Codec.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/security/key/dss/DSSKeyPairPKCS8Codec.java,v
retrieving revision 1.2
diff -u -r1.2 DSSKeyPairPKCS8Codec.java
--- DSSKeyPairPKCS8Codec.java	9 Feb 2006 11:52:49 -0000	1.2
+++ DSSKeyPairPKCS8Codec.java	11 Feb 2006 08:47:09 -0000
@@ -85,9 +85,12 @@
     return PKCS8_FORMAT;
   }

+  /**
+   * @throws InvalidParameterException ALWAYS.
+   */
   public byte[] encodePublicKey(PublicKey key)
   {
-    throw new IllegalArgumentException("Wrong format for public keys");
+    throw new InvalidParameterException("Wrong format for public keys");
   }

   /**
@@ -121,7 +124,7 @@
   public byte[] encodePrivateKey(PrivateKey key)
   {
     if (! (key instanceof DSSPrivateKey))
-      throw new IllegalArgumentException("Wrong key type");
+      throw new InvalidParameterException("Wrong key type");

     DERValue derVersion = new DERValue(DER.INTEGER, BigInteger.ZERO);

@@ -170,9 +173,12 @@
     return result;
   }

+  /**
+   * @throws InvalidParameterException ALWAYS.
+   */
   public PublicKey decodePublicKey(byte[] input)
   {
-    throw new IllegalArgumentException("Wrong format for public keys");
+    throw new InvalidParameterException("Wrong format for public keys");
   }

   /**
Index: EncodedKeyFactory.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/security/jce/sig/EncodedKeyFactory.java,v
retrieving revision 1.2
diff -u -r1.2 EncodedKeyFactory.java
--- EncodedKeyFactory.java	9 Feb 2006 11:52:48 -0000	1.2
+++ EncodedKeyFactory.java	11 Feb 2006 08:47:29 -0000
@@ -44,15 +44,20 @@
 import gnu.java.security.key.rsa.GnuRSAPrivateKey;
 import gnu.java.security.key.rsa.GnuRSAPublicKey;

+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;

 /**
@@ -67,8 +72,14 @@
   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 X509EncodedKeySpec))
-      throw new InvalidKeySpecException("only supports X.509 key specs");
+      throw new InvalidKeySpecException("Unsupported key specification");

     byte[] input = ((X509EncodedKeySpec) keySpec).getEncoded();

@@ -83,12 +94,12 @@

     // try RSA
     try
-    {
-      return GnuRSAPublicKey.valueOf(input);
-    }
-  catch (InvalidParameterException ignored)
-    {
-    }
+      {
+        return GnuRSAPublicKey.valueOf(input);
+      }
+    catch (InvalidParameterException ignored)
+      {
+      }

     // FIXME: try DH

@@ -98,8 +109,14 @@
   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 PKCS8EncodedKeySpec))
-      throw new InvalidKeySpecException("only supports PKCS8 key specs");
+      throw new InvalidKeySpecException("Unsupported key specification");

     byte[] input = ((PKCS8EncodedKeySpec) keySpec).getEncoded();

@@ -114,12 +131,12 @@

     // try RSA
     try
-    {
-      return GnuRSAPrivateKey.valueOf(input);
-    }
-  catch (InvalidParameterException ignored)
-    {
-    }
+      {
+        return GnuRSAPrivateKey.valueOf(input);
+      }
+    catch (InvalidParameterException ignored)
+      {
+      }

     // FIXME: try DH

@@ -146,4 +163,63 @@
   {
     throw new InvalidKeyException("Key translation not supported");
   }
+
+  /**
+   * @param spec an instance of [EMAIL PROTECTED] DSAPublicKeySpec} to decode.
+   * @return an instance of [EMAIL PROTECTED] 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 [EMAIL PROTECTED] RSAPublicKeySpec} to decode.
+   * @return an instance of [EMAIL PROTECTED] 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 [EMAIL PROTECTED] DSAPrivateKeySpec} to decode.
+   * @return an instance of [EMAIL PROTECTED] 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 [EMAIL PROTECTED] RSAPrivateCrtKeySpec} to decode.
+   * @return an instance of [EMAIL PROTECTED] 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);
+  }
 }

Attachment: pgp3TY4TSL8HI.pgp
Description: PGP signature

Reply via email to