diff options
Diffstat (limited to 'gnu/java/security/key/rsa')
-rw-r--r-- | gnu/java/security/key/rsa/GnuRSAKey.java | 21 | ||||
-rw-r--r-- | gnu/java/security/key/rsa/GnuRSAPrivateKey.java | 25 | ||||
-rw-r--r-- | gnu/java/security/key/rsa/GnuRSAPublicKey.java | 17 | ||||
-rw-r--r-- | gnu/java/security/key/rsa/RSAKeyPairGenerator.java | 14 | ||||
-rw-r--r-- | gnu/java/security/key/rsa/RSAKeyPairPKCS8Codec.java | 13 | ||||
-rw-r--r-- | gnu/java/security/key/rsa/RSAKeyPairX509Codec.java | 3 |
6 files changed, 84 insertions, 9 deletions
diff --git a/gnu/java/security/key/rsa/GnuRSAKey.java b/gnu/java/security/key/rsa/GnuRSAKey.java index 098d372dc..3009dd71f 100644 --- a/gnu/java/security/key/rsa/GnuRSAKey.java +++ b/gnu/java/security/key/rsa/GnuRSAKey.java @@ -38,8 +38,8 @@ exception statement from your version. */ 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 @@ public abstract class GnuRSAKey implements Key, RSAKey */ protected final int defaultFormat; + /** String representation of this key. Cached for speed. */ + private transient String str; + // Constructor(s) // ------------------------------------------------------------------------- @@ -111,7 +114,7 @@ public abstract class GnuRSAKey implements Key, RSAKey /** @deprecated see getEncoded(int). */ public byte[] getEncoded() { - return getEncoded(IKeyPairCodec.RAW_FORMAT); + return getEncoded(defaultFormat); } public String getFormat() @@ -173,6 +176,20 @@ public abstract class GnuRSAKey implements Key, RSAKey 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); diff --git a/gnu/java/security/key/rsa/GnuRSAPrivateKey.java b/gnu/java/security/key/rsa/GnuRSAPrivateKey.java index 7d3144bcd..920534487 100644 --- a/gnu/java/security/key/rsa/GnuRSAPrivateKey.java +++ b/gnu/java/security/key/rsa/GnuRSAPrivateKey.java @@ -38,6 +38,7 @@ exception statement from your version. */ 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 @@ import java.security.interfaces.RSAPrivateKey; 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 @@ public class GnuRSAPrivateKey extends GnuRSAKey implements PrivateKey, /** 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 @@ public class GnuRSAPrivateKey extends GnuRSAKey implements PrivateKey, } 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; + } } diff --git a/gnu/java/security/key/rsa/GnuRSAPublicKey.java b/gnu/java/security/key/rsa/GnuRSAPublicKey.java index bb5d0a49f..8badede14 100644 --- a/gnu/java/security/key/rsa/GnuRSAPublicKey.java +++ b/gnu/java/security/key/rsa/GnuRSAPublicKey.java @@ -38,6 +38,7 @@ exception statement from your version. */ 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 @@ import java.security.interfaces.RSAPublicKey; 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 @@ public class GnuRSAPublicKey extends GnuRSAKey implements PublicKey, 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; + } } diff --git a/gnu/java/security/key/rsa/RSAKeyPairGenerator.java b/gnu/java/security/key/rsa/RSAKeyPairGenerator.java index 9c7338f66..39063381f 100644 --- a/gnu/java/security/key/rsa/RSAKeyPairGenerator.java +++ b/gnu/java/security/key/rsa/RSAKeyPairGenerator.java @@ -50,6 +50,7 @@ import java.security.PublicKey; 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 @@ import java.util.Map; */ 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 class RSAKeyPairGenerator implements IKeyPairGenerator */ 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 @@ public class RSAKeyPairGenerator implements IKeyPairGenerator 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 class RSAKeyPairGenerator implements IKeyPairGenerator */ 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 @@ public class RSAKeyPairGenerator implements IKeyPairGenerator 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 ---------------------------------------------------------- diff --git a/gnu/java/security/key/rsa/RSAKeyPairPKCS8Codec.java b/gnu/java/security/key/rsa/RSAKeyPairPKCS8Codec.java index a7f65b610..0b9809032 100644 --- a/gnu/java/security/key/rsa/RSAKeyPairPKCS8Codec.java +++ b/gnu/java/security/key/rsa/RSAKeyPairPKCS8Codec.java @@ -45,6 +45,7 @@ import java.security.InvalidParameterException; 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 @@ import gnu.java.security.util.DerUtil; 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 class RSAKeyPairPKCS8Codec */ 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 @@ public class RSAKeyPairPKCS8Codec throw y; } + log.exiting(this.getClass().getName(), "encodePrivateKey()", result); return result; } @@ -208,6 +213,8 @@ public class RSAKeyPairPKCS8Codec */ 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 @@ public class RSAKeyPairPKCS8Codec 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; } } diff --git a/gnu/java/security/key/rsa/RSAKeyPairX509Codec.java b/gnu/java/security/key/rsa/RSAKeyPairX509Codec.java index 1c362784b..882d9c7b2 100644 --- a/gnu/java/security/key/rsa/RSAKeyPairX509Codec.java +++ b/gnu/java/security/key/rsa/RSAKeyPairX509Codec.java @@ -128,8 +128,9 @@ public class RSAKeyPairX509Codec 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); |