001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.asn1.ASN1Sequence;
004: import org.bouncycastle.asn1.DERNull;
005: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
006: import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
007: import org.bouncycastle.asn1.pkcs.RSAPrivateKeyStructure;
008: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
009: import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
010:
011: import java.math.BigInteger;
012: import java.security.interfaces.RSAPrivateCrtKey;
013: import java.security.spec.RSAPrivateCrtKeySpec;
014:
015: /**
016: * A provider representation for a RSA private key, with CRT factors included.
017: */
018: public class JCERSAPrivateCrtKey extends JCERSAPrivateKey implements
019: RSAPrivateCrtKey {
020: static final long serialVersionUID = 7834723820638524718L;
021:
022: private BigInteger publicExponent;
023: private BigInteger primeP;
024: private BigInteger primeQ;
025: private BigInteger primeExponentP;
026: private BigInteger primeExponentQ;
027: private BigInteger crtCoefficient;
028:
029: /**
030: * construct a private key from it's org.bouncycastle.crypto equivalent.
031: *
032: * @param key the parameters object representing the private key.
033: */
034: JCERSAPrivateCrtKey(RSAPrivateCrtKeyParameters key) {
035: super (key);
036:
037: this .publicExponent = key.getPublicExponent();
038: this .primeP = key.getP();
039: this .primeQ = key.getQ();
040: this .primeExponentP = key.getDP();
041: this .primeExponentQ = key.getDQ();
042: this .crtCoefficient = key.getQInv();
043: }
044:
045: /**
046: * construct a private key from an RSAPrivateCrtKeySpec
047: *
048: * @param spec the spec to be used in construction.
049: */
050: JCERSAPrivateCrtKey(RSAPrivateCrtKeySpec spec) {
051: this .modulus = spec.getModulus();
052: this .publicExponent = spec.getPublicExponent();
053: this .privateExponent = spec.getPrivateExponent();
054: this .primeP = spec.getPrimeP();
055: this .primeQ = spec.getPrimeQ();
056: this .primeExponentP = spec.getPrimeExponentP();
057: this .primeExponentQ = spec.getPrimeExponentQ();
058: this .crtCoefficient = spec.getCrtCoefficient();
059: }
060:
061: /**
062: * construct a private key from another RSAPrivateCrtKey.
063: *
064: * @param key the object implementing the RSAPrivateCrtKey interface.
065: */
066: JCERSAPrivateCrtKey(RSAPrivateCrtKey key) {
067: this .modulus = key.getModulus();
068: this .publicExponent = key.getPublicExponent();
069: this .privateExponent = key.getPrivateExponent();
070: this .primeP = key.getPrimeP();
071: this .primeQ = key.getPrimeQ();
072: this .primeExponentP = key.getPrimeExponentP();
073: this .primeExponentQ = key.getPrimeExponentQ();
074: this .crtCoefficient = key.getCrtCoefficient();
075: }
076:
077: /**
078: * construct an RSA key from a private key info object.
079: */
080: JCERSAPrivateCrtKey(PrivateKeyInfo info) {
081: this (new RSAPrivateKeyStructure((ASN1Sequence) info
082: .getPrivateKey()));
083: }
084:
085: /**
086: * construct an RSA key from a ASN.1 RSA private key object.
087: */
088: JCERSAPrivateCrtKey(RSAPrivateKeyStructure key) {
089: this .modulus = key.getModulus();
090: this .publicExponent = key.getPublicExponent();
091: this .privateExponent = key.getPrivateExponent();
092: this .primeP = key.getPrime1();
093: this .primeQ = key.getPrime2();
094: this .primeExponentP = key.getExponent1();
095: this .primeExponentQ = key.getExponent2();
096: this .crtCoefficient = key.getCoefficient();
097: }
098:
099: /**
100: * return the encoding format we produce in getEncoded().
101: *
102: * @return the encoding format we produce in getEncoded().
103: */
104: public String getFormat() {
105: return "PKCS#8";
106: }
107:
108: /**
109: * Return a PKCS8 representation of the key. The sequence returned
110: * represents a full PrivateKeyInfo object.
111: *
112: * @return a PKCS8 representation of the key.
113: */
114: public byte[] getEncoded() {
115: PrivateKeyInfo info = new PrivateKeyInfo(
116: new AlgorithmIdentifier(
117: PKCSObjectIdentifiers.rsaEncryption,
118: new DERNull()), new RSAPrivateKeyStructure(
119: getModulus(), getPublicExponent(),
120: getPrivateExponent(), getPrimeP(), getPrimeQ(),
121: getPrimeExponentP(), getPrimeExponentQ(),
122: getCrtCoefficient()).getDERObject());
123:
124: return info.getDEREncoded();
125: }
126:
127: /**
128: * return the public exponent.
129: *
130: * @return the public exponent.
131: */
132: public BigInteger getPublicExponent() {
133: return publicExponent;
134: }
135:
136: /**
137: * return the prime P.
138: *
139: * @return the prime P.
140: */
141: public BigInteger getPrimeP() {
142: return primeP;
143: }
144:
145: /**
146: * return the prime Q.
147: *
148: * @return the prime Q.
149: */
150: public BigInteger getPrimeQ() {
151: return primeQ;
152: }
153:
154: /**
155: * return the prime exponent for P.
156: *
157: * @return the prime exponent for P.
158: */
159: public BigInteger getPrimeExponentP() {
160: return primeExponentP;
161: }
162:
163: /**
164: * return the prime exponent for Q.
165: *
166: * @return the prime exponent for Q.
167: */
168: public BigInteger getPrimeExponentQ() {
169: return primeExponentQ;
170: }
171:
172: /**
173: * return the CRT coefficient.
174: *
175: * @return the CRT coefficient.
176: */
177: public BigInteger getCrtCoefficient() {
178: return crtCoefficient;
179: }
180:
181: public int hashCode() {
182: return this .getModulus().hashCode()
183: ^ this .getPublicExponent().hashCode()
184: ^ this .getPrivateExponent().hashCode();
185: }
186:
187: public boolean equals(Object o) {
188: if (o == this ) {
189: return true;
190: }
191:
192: if (!(o instanceof RSAPrivateCrtKey)) {
193: return false;
194: }
195:
196: RSAPrivateCrtKey key = (RSAPrivateCrtKey) o;
197:
198: return this .getModulus().equals(key.getModulus())
199: && this .getPublicExponent().equals(
200: key.getPublicExponent())
201: && this .getPrivateExponent().equals(
202: key.getPrivateExponent())
203: && this .getPrimeP().equals(key.getPrimeP())
204: && this .getPrimeQ().equals(key.getPrimeQ())
205: && this .getPrimeExponentP().equals(
206: key.getPrimeExponentP())
207: && this .getPrimeExponentQ().equals(
208: key.getPrimeExponentQ())
209: && this .getCrtCoefficient().equals(
210: key.getCrtCoefficient());
211: }
212:
213: public String toString() {
214: StringBuffer buf = new StringBuffer();
215: String nl = System.getProperty("line.separator");
216:
217: buf.append("RSA Private CRT Key").append(nl);
218: buf.append(" modulus: ").append(
219: this .getModulus().toString(16)).append(nl);
220: buf.append(" public exponent: ").append(
221: this .getPublicExponent().toString(16)).append(nl);
222: buf.append(" private exponent: ").append(
223: this .getPrivateExponent().toString(16)).append(nl);
224: buf.append(" primeP: ").append(
225: this .getPrimeP().toString(16)).append(nl);
226: buf.append(" primeQ: ").append(
227: this .getPrimeQ().toString(16)).append(nl);
228: buf.append(" primeExponentP: ").append(
229: this .getPrimeExponentP().toString(16)).append(nl);
230: buf.append(" primeExponentQ: ").append(
231: this .getPrimeExponentQ().toString(16)).append(nl);
232: buf.append(" crtCoefficient: ").append(
233: this .getCrtCoefficient().toString(16)).append(nl);
234:
235: return buf.toString();
236: }
237: }
|