hello there, the attached patch --already committed-- fixes some bugs with the RSA key-pair handling; adds more trace/debug statements to the RSA classes, aligning them with the DSS ones; and adds an "RSA" alias to the "MD5withRSA" signature algorithm (making it easy to specify the RSA default signature algorithm when using the keytool).
2006-04-29 Raif S. Naffah <[EMAIL PROTECTED]>
* gnu/java/security/provider/Gnu.java (run):
Add "RSA" as an alias to MD5withRSA.
* gnu/java/security/key/rsa/RSAKeyPairX509Codec.java (encodePublicKey):
Always encode a NULL as the value of an algorithm parameters field.
* gnu/java/security/key/rsa/RSAKeyPairPKCS8Codec.java (log): New field.
(encodePrivateKey): Added trace/log statements.
(decodePrivateKey): Likewise.
* gnu/java/security/key/rsa/RSAKeyPairGenerator.java (log): New field.
(setup): Added trace/log statements.
(generate): Likewise.
* gnu/java/security/key/rsa/GnuRSAPublicKey.java (str): New field.
(toString): New method.
* gnu/java/security/key/rsa/GnuRSAPrivateKey.java (DEBUG): New constant.
(str): New field.
(toString): New method.
* gnu/java/security/key/rsa/GnuRSAKey.java (str): New field.
(getEncoded): Use defaultFormat.
(toString): New method.
* gnu/java/security/key/dss/DSSKey.java (toString):
Include defaultFormat in string.
* gnu/java/security/jce/sig/RSAKeyFactory.java (engineGeneratePublic):
Break if successfully decoded public key.
(engineGeneratePrivate): Break if successfully decoded private key.
cheers;
rsn
Index: RSAKeyFactory.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/gnu/java/security/jce/sig/RSAKeyFactory.java,v
retrieving revision 1.1
diff -u -r1.1 RSAKeyFactory.java
--- RSAKeyFactory.java 10 Feb 2006 11:41:24 -0000 1.1
+++ RSAKeyFactory.java 29 Apr 2006 06:24:11 -0000
@@ -84,7 +84,7 @@
PublicKey result;
try
{
- result = new RSAKeyPairX509Codec().decodePublicKey(encoded);
+ return new RSAKeyPairX509Codec().decodePublicKey(encoded);
}
catch (RuntimeException x)
{
@@ -131,7 +131,7 @@
PrivateKey result;
try
{
- result = new RSAKeyPairPKCS8Codec().decodePrivateKey(encoded);
+ return new RSAKeyPairPKCS8Codec().decodePrivateKey(encoded);
}
catch (RuntimeException x)
{
Index: DSSKey.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/security/key/dss/DSSKey.java,v
retrieving revision 1.6
diff -u -r1.6 DSSKey.java
--- DSSKey.java 26 Mar 2006 22:57:46 -0000 1.6
+++ DSSKey.java 29 Apr 2006 06:27:55 -0000
@@ -185,6 +185,7 @@
{
String ls = SystemProperties.getProperty("line.separator");
str = new StringBuilder().append(ls)
+ .append("defaultFormat=").append(defaultFormat).append(",").append(ls)
.append("p=0x").append(p.toString(16)).append(",").append(ls)
.append("q=0x").append(q.toString(16)).append(",").append(ls)
.append("g=0x").append(g.toString(16))
Index: GnuRSAKey.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/gnu/java/security/key/rsa/GnuRSAKey.java,v
retrieving revision 1.4
diff -u -r1.4 GnuRSAKey.java
--- GnuRSAKey.java 26 Mar 2006 22:57:46 -0000 1.4
+++ GnuRSAKey.java 29 Apr 2006 06:29:49 -0000
@@ -38,8 +38,8 @@
package gnu.java.security.key.rsa;
+import gnu.classpath.SystemProperties;
import gnu.java.security.Registry;
-import gnu.java.security.key.IKeyPairCodec;
import gnu.java.security.util.FormatUtil;
import java.math.BigInteger;
@@ -67,6 +67,9 @@
*/
protected final int defaultFormat;
+ /** String representation of this key. Cached for speed. */
+ private transient String str;
+
// Constructor(s)
// -------------------------------------------------------------------------
@@ -111,7 +114,7 @@
/** @deprecated see getEncoded(int). */
public byte[] getEncoded()
{
- return getEncoded(IKeyPairCodec.RAW_FORMAT);
+ return getEncoded(defaultFormat);
}
public String getFormat()
@@ -173,6 +176,20 @@
return n.equals(that.getModulus());
}
+ public String toString()
+ {
+ if (str == null)
+ {
+ String ls = SystemProperties.getProperty("line.separator");
+ str = new StringBuilder().append(ls)
+
.append("defaultFormat=").append(defaultFormat).append(",").append(ls)
+ .append("n=0x").append(n.toString(16)).append(",").append(ls)
+ .append("e=0x").append(e.toString(16))
+ .toString();
+ }
+ return str;
+ }
+
// abstract methods to be implemented by subclasses ------------------------
public abstract byte[] getEncoded(int format);
Index: GnuRSAPrivateKey.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/gnu/java/security/key/rsa/GnuRSAPrivateKey.java,v
retrieving revision 1.4
diff -u -r1.4 GnuRSAPrivateKey.java
--- GnuRSAPrivateKey.java 26 Mar 2006 22:57:46 -0000 1.4
+++ GnuRSAPrivateKey.java 29 Apr 2006 06:36:52 -0000
@@ -38,6 +38,7 @@
package gnu.java.security.key.rsa;
+import gnu.classpath.SystemProperties;
import gnu.java.security.Registry;
import gnu.java.security.key.IKeyPairCodec;
@@ -60,10 +61,11 @@
public class GnuRSAPrivateKey extends GnuRSAKey implements PrivateKey,
RSAPrivateCrtKey
{
-
// Constants and variables
// -------------------------------------------------------------------------
+ private static final boolean DEBUG = false;
+
/** The first prime divisor of the modulus. */
private final BigInteger p;
@@ -84,6 +86,9 @@
/** The CRT (Chinese Remainder Theorem) coefficient. */
private final BigInteger qInv;
+ /** String representation of this key. Cached for speed. */
+ private transient String str;
+
// Constructor(s)
// -------------------------------------------------------------------------
@@ -294,4 +299,22 @@
}
return false;
}
+
+ public String toString()
+ {
+ if (str == null)
+ {
+ String ls = SystemProperties.getProperty("line.separator");
+ str = new StringBuilder(this.getClass().getName()).append("(")
+ .append(super.toString()).append(",").append(ls)
+ .append("d=0x").append(DEBUG ? d.toString(16) :
"**...*").append(ls)
+ .append("p=0x").append(DEBUG ? p.toString(16) :
"**...*").append(ls)
+ .append("q=0x").append(DEBUG ? q.toString(16) :
"**...*").append(ls)
+ .append("dP=0x").append(DEBUG ? dP.toString(16) :
"**...*").append(ls)
+ .append("dQ=0x").append(DEBUG ? dQ.toString(16) :
"**...*").append(ls)
+ .append("qInv=0x").append(DEBUG ? qInv.toString(16) :
"**...*").append(ls)
+ .append(")").toString();
+ }
+ return str;
+ }
}
Index: GnuRSAPublicKey.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/gnu/java/security/key/rsa/GnuRSAPublicKey.java,v
retrieving revision 1.3
diff -u -r1.3 GnuRSAPublicKey.java
--- GnuRSAPublicKey.java 26 Mar 2006 22:57:46 -0000 1.3
+++ GnuRSAPublicKey.java 29 Apr 2006 06:40:01 -0000
@@ -38,6 +38,7 @@
package gnu.java.security.key.rsa;
+import gnu.classpath.SystemProperties;
import gnu.java.security.Registry;
import gnu.java.security.key.IKeyPairCodec;
@@ -59,10 +60,12 @@
public class GnuRSAPublicKey extends GnuRSAKey implements PublicKey,
RSAPublicKey
{
-
// Constants and variables
// -------------------------------------------------------------------------
+ /** String representation of this key. Cached for speed. */
+ private transient String str;
+
// Constructor(s)
// -------------------------------------------------------------------------
@@ -180,4 +183,16 @@
return super.equals(that)
&& getPublicExponent().equals(that.getPublicExponent());
}
+
+ public String toString()
+ {
+ if (str == null)
+ {
+ String ls = SystemProperties.getProperty("line.separator");
+ str = new StringBuilder(this.getClass().getName()).append("(")
+ .append(super.toString()).append(",").append(ls)
+ .append(")").toString();
+ }
+ return str;
+ }
}
Index: RSAKeyPairGenerator.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/gnu/java/security/key/rsa/RSAKeyPairGenerator.java,v
retrieving revision 1.3
diff -u -r1.3 RSAKeyPairGenerator.java
--- RSAKeyPairGenerator.java 9 Feb 2006 11:52:48 -0000 1.3
+++ RSAKeyPairGenerator.java 29 Apr 2006 06:41:58 -0000
@@ -50,6 +50,7 @@
import java.security.SecureRandom;
import java.security.spec.RSAKeyGenParameterSpec;
import java.util.Map;
+import java.util.logging.Logger;
/**
* <p>A key-pair generator for asymetric keys to use in conjunction with the
RSA
@@ -68,10 +69,11 @@
*/
public class RSAKeyPairGenerator implements IKeyPairGenerator
{
-
// Constants and variables
// -------------------------------------------------------------------------
+ private static final Logger log =
Logger.getLogger(RSAKeyPairGenerator.class.getName());
+
/** The BigInteger constant 1. */
private static final BigInteger ONE = BigInteger.ONE;
@@ -150,6 +152,8 @@
*/
public void setup(Map attributes)
{
+ 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);
@@ -177,6 +181,8 @@
Integer formatID = (Integer) attributes.get(PREFERRED_ENCODING_FORMAT);
preferredFormat = formatID == null ? DEFAULT_ENCODING_FORMAT
: formatID.intValue();
+
+ log.exiting(this.getClass().getName(), "setup");
}
/**
@@ -187,6 +193,8 @@
*/
public KeyPair generate()
{
+ log.entering(this.getClass().getName(), "generate");
+
BigInteger p, q, n, d;
// 1. Generate a prime p in the interval [2**(M-1), 2**M - 1], where
@@ -234,7 +242,9 @@
PublicKey pubK = new GnuRSAPublicKey(preferredFormat, n, e);
PrivateKey secK = new GnuRSAPrivateKey(preferredFormat, p, q, e, d);
- return new KeyPair(pubK, secK);
+ KeyPair result = new KeyPair(pubK, secK);
+ log.exiting(this.getClass().getName(), "generate", result);
+ return result;
}
// helper methods ----------------------------------------------------------
Index: RSAKeyPairPKCS8Codec.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/gnu/java/security/key/rsa/RSAKeyPairPKCS8Codec.java,v
retrieving revision 1.3
diff -u -r1.3 RSAKeyPairPKCS8Codec.java
--- RSAKeyPairPKCS8Codec.java 23 Feb 2006 12:54:46 -0000 1.3
+++ RSAKeyPairPKCS8Codec.java 29 Apr 2006 06:44:30 -0000
@@ -45,6 +45,7 @@
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
+import java.util.logging.Logger;
import gnu.java.security.OID;
import gnu.java.security.Registry;
@@ -62,6 +63,7 @@
public class RSAKeyPairPKCS8Codec
implements IKeyPairCodec
{
+ private static final Logger log =
Logger.getLogger(RSAKeyPairPKCS8Codec.class.getName());
private static final OID RSA_ALG_OID = new OID(Registry.RSA_OID_STRING);
// implicit 0-arguments constructor
@@ -120,6 +122,8 @@
*/
public byte[] encodePrivateKey(PrivateKey key)
{
+ log.entering(this.getClass().getName(), "encodePrivateKey()", key);
+
if (! (key instanceof GnuRSAPrivateKey))
throw new InvalidParameterException("Wrong key type");
@@ -187,6 +191,7 @@
throw y;
}
+ log.exiting(this.getClass().getName(), "encodePrivateKey()", result);
return result;
}
@@ -208,6 +213,8 @@
*/
public PrivateKey decodePrivateKey(byte[] input)
{
+ log.entering(this.getClass().getName(), "decodePrivateKey()", input);
+
if (input == null)
throw new InvalidParameterException("Input bytes MUST NOT be null");
@@ -278,7 +285,9 @@
throw y;
}
- return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID, n, e, d, p, q,
- dP, dQ, qInv);
+ PrivateKey result = new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID, n, e,
+ d, p, q, dP, dQ, qInv);
+ log.exiting(this.getClass().getName(), "decodePrivateKey()", result);
+ return result;
}
}
Index: RSAKeyPairX509Codec.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/gnu/java/security/key/rsa/RSAKeyPairX509Codec.java,v
retrieving revision 1.4
diff -u -r1.4 RSAKeyPairX509Codec.java
--- RSAKeyPairX509Codec.java 26 Feb 2006 04:10:38 -0000 1.4
+++ RSAKeyPairX509Codec.java 29 Apr 2006 06:46:59 -0000
@@ -128,8 +128,9 @@
DERValue derN = new DERValue(DER.INTEGER, n);
DERValue derE = new DERValue(DER.INTEGER, e);
- ArrayList algorithmID = new ArrayList(1);
+ ArrayList algorithmID = new ArrayList(2);
algorithmID.add(derOID);
+ algorithmID.add(new DERValue(DER.NULL, null));
DERValue derAlgorithmID = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
algorithmID);
Index: Gnu.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/security/provider/Gnu.java,v
retrieving revision 1.12
diff -u -r1.12 Gnu.java
--- Gnu.java 26 Feb 2006 04:49:18 -0000 1.12
+++ Gnu.java 29 Apr 2006 06:50:26 -0000
@@ -86,6 +86,7 @@
put("Alg.Alias.Signature.md5WithRSAEncryption", "MD5withRSA");
put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.4", "MD5withRSA");
put("Alg.Alias.Signature.1.2.840.113549.1.1.4", "MD5withRSA");
+ put("Alg.Alias.Signature.RSA", "MD5withRSA");
put("Signature.SHA160withRSA",
gnu.java.security.jce.sig.SHA160withRSA.class.getName());
pgptBWlIAt4fg.pgp
Description: PGP signature
