Frames | No Frames |
1: /* GnuRSAPrivateKey.java -- 2: Copyright 2001, 2002, 2003, 2006 Free Software Foundation, Inc. 3: 4: This file is a part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2 of the License, or (at 9: your option) any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; if not, write to the Free Software 18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19: USA 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.java.security.key.rsa; 40: 41: import gnu.java.lang.CPStringBuilder; 42: 43: import gnu.java.security.Configuration; 44: import gnu.java.security.action.GetPropertyAction; 45: import gnu.java.security.Registry; 46: import gnu.java.security.key.IKeyPairCodec; 47: 48: import java.math.BigInteger; 49: import java.security.AccessController; 50: import java.security.PrivateKey; 51: import java.security.interfaces.RSAPrivateCrtKey; 52: import java.security.interfaces.RSAPrivateKey; 53: 54: /** 55: * An object that embodies an RSA private key. 56: * <p> 57: * References: 58: * <ol> 59: * <li><a 60: * href="http://www.cosic.esat.kuleuven.ac.be/nessie/workshop/submissions/rsa-pss.zip"> 61: * RSA-PSS Signature Scheme with Appendix, part B.</a><br> 62: * Primitive specification and supporting documentation.<br> 63: * Jakob Jonsson and Burt Kaliski.</li> 64: * </ol> 65: */ 66: public class GnuRSAPrivateKey 67: extends GnuRSAKey 68: implements PrivateKey, RSAPrivateCrtKey 69: { 70: /** The first prime divisor of the modulus. */ 71: private final BigInteger p; 72: 73: /** The second prime divisor of the modulus. */ 74: private final BigInteger q; 75: 76: /** The private exponent of an RSA private key. */ 77: private final BigInteger d; 78: 79: /** The first factor's exponent. */ 80: private final BigInteger dP; 81: 82: /** The second factor's exponent. */ 83: private final BigInteger dQ; 84: 85: /** The CRT (Chinese Remainder Theorem) coefficient. */ 86: private final BigInteger qInv; 87: 88: /** String representation of this key. Cached for speed. */ 89: private transient String str; 90: 91: /** 92: * Convenience constructor. Calls the constructor with 5 arguments passing 93: * {@link Registry#RAW_ENCODING_ID} as the identifier of the preferred 94: * encoding format. 95: * 96: * @param p the modulus first prime divisor. 97: * @param q the modulus second prime divisor. 98: * @param e the public exponent. 99: * @param d the private exponent. 100: */ 101: public GnuRSAPrivateKey(BigInteger p, BigInteger q, BigInteger e, BigInteger d) 102: { 103: this(Registry.RAW_ENCODING_ID, p, q, e, d); 104: } 105: 106: /** 107: * Constructs a new instance of a <code>GnuRSAPrivateKey</code> given the 108: * designated arguments. 109: * 110: * @param preferredFormat the indetifier of the preferred encoding format to 111: * use when externalizing this key. 112: * @param p the modulus first prime divisor. 113: * @param q the modulus second prime divisor. 114: * @param e the public exponent. 115: * @param d the private exponent. 116: */ 117: public GnuRSAPrivateKey(int preferredFormat, BigInteger p, BigInteger q, 118: BigInteger e, BigInteger d) 119: { 120: this(preferredFormat, 121: p.multiply(q), 122: e, d, p, q, 123: e.modInverse(p.subtract(BigInteger.ONE)), 124: e.modInverse(q.subtract(BigInteger.ONE)), 125: q.modInverse(p)); 126: } 127: 128: /** 129: * Constructs a new instance of a <code>GnuRSAPrivateKey</code> given the 130: * designated arguments. 131: * 132: * @param preferredFormat the indetifier of the preferred encoding format to 133: * use when externalizing this key. 134: * @param n the public modulus, which is also the product of <code>p</code> 135: * and <code>q</code>. 136: * @param e the public exponent. 137: * @param d the private exponent. 138: * @param p the modulus first prime divisor. 139: * @param q the modulus second prime divisor. 140: * @param dP the first prime's exponen. A positive integer less than 141: * <code>p</code> and <code>q</code>, satisfying 142: * <code>e * dP = 1 (mod p-1)</code>. 143: * @param dQ the second prime's exponent. A positive integer less than 144: * <code>p</code> and <code>q</code>, satisfying 145: * <code>e * dQ = 1 (mod p-1)</code>. 146: * @param qInv the Chinese Remainder Theorem coefiicient. A positive integer 147: * less than <code>p</code>, satisfying 148: * <code>q * qInv = 1 (mod p)</code>. 149: */ 150: public GnuRSAPrivateKey(int preferredFormat, BigInteger n, BigInteger e, 151: BigInteger d, BigInteger p, BigInteger q, 152: BigInteger dP, BigInteger dQ, BigInteger qInv) 153: { 154: super(preferredFormat == Registry.ASN1_ENCODING_ID ? Registry.PKCS8_ENCODING_ID 155: : preferredFormat, 156: n, e); 157: this.d = d; 158: this.p = p; 159: this.q = q; 160: // the exponents dP and dQ are positive integers less than p and q 161: // respectively satisfying 162: // e * dP = 1 (mod p-1); 163: // e * dQ = 1 (mod q-1), 164: this.dP = dP; 165: this.dQ = dQ; 166: // the CRT coefficient qInv is a positive integer less than p satisfying 167: // q * qInv = 1 (mod p). 168: this.qInv = qInv; 169: } 170: 171: /** 172: * A class method that takes the output of the <code>encodePrivateKey()</code> 173: * method of an RSA keypair codec object (an instance implementing 174: * {@link IKeyPairCodec} for RSA keys, and re-constructs an instance of this 175: * object. 176: * 177: * @param k the contents of a previously encoded instance of this object. 178: * @throws ArrayIndexOutOfBoundsException if there is not enough bytes, in 179: * <code>k</code>, to represent a valid encoding of an instance 180: * of this object. 181: * @throws IllegalArgumentException if the byte sequence does not represent a 182: * valid encoding of an instance of this object. 183: */ 184: public static GnuRSAPrivateKey valueOf(final byte[] k) 185: { 186: // try RAW codec 187: if (k[0] == Registry.MAGIC_RAW_RSA_PRIVATE_KEY[0]) 188: try 189: { 190: return (GnuRSAPrivateKey) new RSAKeyPairRawCodec().decodePrivateKey(k); 191: } 192: catch (IllegalArgumentException ignored) 193: { 194: } 195: // try PKCS#8 codec 196: return (GnuRSAPrivateKey) new RSAKeyPairPKCS8Codec().decodePrivateKey(k); 197: } 198: 199: public BigInteger getPrimeP() 200: { 201: return p; 202: } 203: 204: public BigInteger getPrimeQ() 205: { 206: return q; 207: } 208: 209: public BigInteger getPrimeExponentP() 210: { 211: return dP; 212: } 213: 214: public BigInteger getPrimeExponentQ() 215: { 216: return dQ; 217: } 218: 219: public BigInteger getCrtCoefficient() 220: { 221: return qInv; 222: } 223: 224: public BigInteger getPrivateExponent() 225: { 226: return d; 227: } 228: 229: /** 230: * Returns the encoded form of this private key according to the designated 231: * format. 232: * 233: * @param format the desired format identifier of the resulting encoding. 234: * @return the byte sequence encoding this key according to the designated 235: * format. 236: * @throws IllegalArgumentException if the format is not supported. 237: * @see RSAKeyPairRawCodec 238: * @see RSAKeyPairPKCS8Codec 239: */ 240: public byte[] getEncoded(int format) 241: { 242: final byte[] result; 243: switch (format) 244: { 245: case IKeyPairCodec.RAW_FORMAT: 246: result = new RSAKeyPairRawCodec().encodePrivateKey(this); 247: break; 248: case IKeyPairCodec.PKCS8_FORMAT: 249: result = new RSAKeyPairPKCS8Codec().encodePrivateKey(this); 250: break; 251: default: 252: throw new IllegalArgumentException("Unsupported encoding format: " 253: + format); 254: } 255: return result; 256: } 257: 258: /** 259: * Returns <code>true</code> if the designated object is an instance of this 260: * class and has the same RSA parameter values as this one. 261: * 262: * @param obj the other non-null RSA key to compare to. 263: * @return <code>true</code> if the designated object is of the same type 264: * and value as this one. 265: */ 266: public boolean equals(final Object obj) 267: { 268: if (obj == null) 269: return false; 270: 271: if (obj instanceof RSAPrivateKey) 272: { 273: final RSAPrivateKey that = (RSAPrivateKey) obj; 274: return super.equals(that) && d.equals(that.getPrivateExponent()); 275: } 276: if (obj instanceof RSAPrivateCrtKey) 277: { 278: final RSAPrivateCrtKey that = (RSAPrivateCrtKey) obj; 279: return super.equals(that) && p.equals(that.getPrimeP()) 280: && q.equals(that.getPrimeQ()) 281: && dP.equals(that.getPrimeExponentP()) 282: && dQ.equals(that.getPrimeExponentQ()) 283: && qInv.equals(that.getCrtCoefficient()); 284: } 285: return false; 286: } 287: 288: public String toString() 289: { 290: if (str == null) 291: { 292: String ls = (String) AccessController.doPrivileged 293: (new GetPropertyAction("line.separator")); 294: str = new CPStringBuilder(this.getClass().getName()).append("(") 295: .append(super.toString()).append(",").append(ls) 296: .append("d=0x").append(Configuration.DEBUG ? d.toString(16) 297: : "**...*").append(ls) 298: .append("p=0x").append(Configuration.DEBUG ? p.toString(16) 299: : "**...*").append(ls) 300: .append("q=0x").append(Configuration.DEBUG ? q.toString(16) 301: : "**...*").append(ls) 302: .append("dP=0x").append(Configuration.DEBUG ? dP.toString(16) 303: : "**...*").append(ls) 304: .append("dQ=0x").append(Configuration.DEBUG ? dQ.toString(16) 305: : "**...*").append(ls) 306: .append("qInv=0x").append(Configuration.DEBUG ? qInv.toString(16) 307: : "**...*").append(ls) 308: .append(")") 309: .toString(); 310: } 311: return str; 312: } 313: }