001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.crypto.jce.provider;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.math.BigInteger;
021: import java.security.interfaces.RSAPrivateCrtKey;
022: import java.security.spec.RSAPrivateCrtKeySpec;
023:
024: import org.apache.geronimo.crypto.asn1.ASN1Sequence;
025: import org.apache.geronimo.crypto.asn1.DERNull;
026: import org.apache.geronimo.crypto.asn1.DEROutputStream;
027: import org.apache.geronimo.crypto.asn1.pkcs.PKCSObjectIdentifiers;
028: import org.apache.geronimo.crypto.asn1.pkcs.PrivateKeyInfo;
029: import org.apache.geronimo.crypto.asn1.pkcs.RSAPrivateKeyStructure;
030: import org.apache.geronimo.crypto.asn1.x509.AlgorithmIdentifier;
031: import org.apache.geronimo.crypto.crypto.params.RSAPrivateCrtKeyParameters;
032:
033: /**
034: * A provider representation for a RSA private key, with CRT factors included.
035: */
036: public class JCERSAPrivateCrtKey extends JCERSAPrivateKey implements
037: RSAPrivateCrtKey {
038: private BigInteger publicExponent;
039: private BigInteger primeP;
040: private BigInteger primeQ;
041: private BigInteger primeExponentP;
042: private BigInteger primeExponentQ;
043: private BigInteger crtCoefficient;
044:
045: /**
046: * construct a private key from it's org.apache.geronimo.crypto.crypto equivalent.
047: *
048: * @param key the parameters object representing the private key.
049: */
050: JCERSAPrivateCrtKey(RSAPrivateCrtKeyParameters key) {
051: super (key);
052:
053: this .publicExponent = key.getPublicExponent();
054: this .primeP = key.getP();
055: this .primeQ = key.getQ();
056: this .primeExponentP = key.getDP();
057: this .primeExponentQ = key.getDQ();
058: this .crtCoefficient = key.getQInv();
059: }
060:
061: /**
062: * construct a private key from an RSAPrivateCrtKeySpec
063: *
064: * @param spec the spec to be used in construction.
065: */
066: JCERSAPrivateCrtKey(RSAPrivateCrtKeySpec spec) {
067: this .modulus = spec.getModulus();
068: this .publicExponent = spec.getPublicExponent();
069: this .privateExponent = spec.getPrivateExponent();
070: this .primeP = spec.getPrimeP();
071: this .primeQ = spec.getPrimeQ();
072: this .primeExponentP = spec.getPrimeExponentP();
073: this .primeExponentQ = spec.getPrimeExponentQ();
074: this .crtCoefficient = spec.getCrtCoefficient();
075: }
076:
077: /**
078: * construct a private key from another RSAPrivateCrtKey.
079: *
080: * @param key the object implementing the RSAPrivateCrtKey interface.
081: */
082: JCERSAPrivateCrtKey(RSAPrivateCrtKey key) {
083: this .modulus = key.getModulus();
084: this .publicExponent = key.getPublicExponent();
085: this .privateExponent = key.getPrivateExponent();
086: this .primeP = key.getPrimeP();
087: this .primeQ = key.getPrimeQ();
088: this .primeExponentP = key.getPrimeExponentP();
089: this .primeExponentQ = key.getPrimeExponentQ();
090: this .crtCoefficient = key.getCrtCoefficient();
091: }
092:
093: /**
094: * construct an RSA key from a private key info object.
095: */
096: JCERSAPrivateCrtKey(PrivateKeyInfo info) {
097: this (new RSAPrivateKeyStructure((ASN1Sequence) info
098: .getPrivateKey()));
099: }
100:
101: /**
102: * construct an RSA key from a ASN.1 RSA private key object.
103: */
104: JCERSAPrivateCrtKey(RSAPrivateKeyStructure key) {
105: this .modulus = key.getModulus();
106: this .publicExponent = key.getPublicExponent();
107: this .privateExponent = key.getPrivateExponent();
108: this .primeP = key.getPrime1();
109: this .primeQ = key.getPrime2();
110: this .primeExponentP = key.getExponent1();
111: this .primeExponentQ = key.getExponent2();
112: this .crtCoefficient = key.getCoefficient();
113: }
114:
115: /**
116: * return the encoding format we produce in getEncoded().
117: *
118: * @return the encoding format we produce in getEncoded().
119: */
120: public String getFormat() {
121: return "PKCS#8";
122: }
123:
124: /**
125: * Return a PKCS8 representation of the key. The sequence returned
126: * represents a full PrivateKeyInfo object.
127: *
128: * @return a PKCS8 representation of the key.
129: */
130: public byte[] getEncoded() {
131: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
132: DEROutputStream dOut = new DEROutputStream(bOut);
133: PrivateKeyInfo info = new PrivateKeyInfo(
134: new AlgorithmIdentifier(
135: PKCSObjectIdentifiers.rsaEncryption,
136: new DERNull()), new RSAPrivateKeyStructure(
137: getModulus(), getPublicExponent(),
138: getPrivateExponent(), getPrimeP(), getPrimeQ(),
139: getPrimeExponentP(), getPrimeExponentQ(),
140: getCrtCoefficient()).getDERObject());
141:
142: try {
143: dOut.writeObject(info);
144: dOut.close();
145: } catch (IOException e) {
146: throw new RuntimeException("Error encoding RSA public key",
147: e);
148: }
149:
150: return bOut.toByteArray();
151: }
152:
153: /**
154: * return the public exponent.
155: *
156: * @return the public exponent.
157: */
158: public BigInteger getPublicExponent() {
159: return publicExponent;
160: }
161:
162: /**
163: * return the prime P.
164: *
165: * @return the prime P.
166: */
167: public BigInteger getPrimeP() {
168: return primeP;
169: }
170:
171: /**
172: * return the prime Q.
173: *
174: * @return the prime Q.
175: */
176: public BigInteger getPrimeQ() {
177: return primeQ;
178: }
179:
180: /**
181: * return the prime exponent for P.
182: *
183: * @return the prime exponent for P.
184: */
185: public BigInteger getPrimeExponentP() {
186: return primeExponentP;
187: }
188:
189: /**
190: * return the prime exponent for Q.
191: *
192: * @return the prime exponent for Q.
193: */
194: public BigInteger getPrimeExponentQ() {
195: return primeExponentQ;
196: }
197:
198: /**
199: * return the CRT coefficient.
200: *
201: * @return the CRT coefficient.
202: */
203: public BigInteger getCrtCoefficient() {
204: return crtCoefficient;
205: }
206:
207: public boolean equals(Object o) {
208: if (!(o instanceof RSAPrivateCrtKey)) {
209: return false;
210: }
211:
212: if (o == this ) {
213: return true;
214: }
215:
216: RSAPrivateCrtKey key = (RSAPrivateCrtKey) o;
217:
218: return this .getModulus().equals(key.getModulus())
219: && this .getPublicExponent().equals(
220: key.getPublicExponent())
221: && this .getPrivateExponent().equals(
222: key.getPrivateExponent())
223: && this .getPrimeP().equals(key.getPrimeP())
224: && this .getPrimeQ().equals(key.getPrimeQ())
225: && this .getPrimeExponentP().equals(
226: key.getPrimeExponentP())
227: && this .getPrimeExponentQ().equals(
228: key.getPrimeExponentQ())
229: && this .getCrtCoefficient().equals(
230: key.getCrtCoefficient());
231: }
232:
233: public String toString() {
234: StringBuffer buf = new StringBuffer();
235: String nl = System.getProperty("line.separator");
236:
237: buf.append("RSA Private CRT Key" + nl);
238: buf.append(" modulus: "
239: + this .getModulus().toString(16) + nl);
240: buf.append(" public exponent: "
241: + this .getPublicExponent().toString(16) + nl);
242: buf.append(" private exponent: "
243: + this .getPrivateExponent().toString(16) + nl);
244: buf.append(" primeP: "
245: + this .getPrimeP().toString(16) + nl);
246: buf.append(" primeQ: "
247: + this .getPrimeQ().toString(16) + nl);
248: buf.append(" primeExponentP: "
249: + this .getPrimeExponentP().toString(16) + nl);
250: buf.append(" primeExponentQ: "
251: + this .getPrimeExponentQ().toString(16) + nl);
252: buf.append(" crtCoefficient: "
253: + this .getCrtCoefficient().toString(16) + nl);
254:
255: return buf.toString();
256: }
257: }
|