KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > jce > provider > JCERSAPrivateCrtKey


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.jce.provider;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.math.BigInteger JavaDoc;
23 import java.security.interfaces.RSAPrivateCrtKey JavaDoc;
24 import java.security.spec.RSAPrivateCrtKeySpec JavaDoc;
25
26 import org.apache.geronimo.util.asn1.ASN1Sequence;
27 import org.apache.geronimo.util.asn1.DERNull;
28 import org.apache.geronimo.util.asn1.DEROutputStream;
29 import org.apache.geronimo.util.asn1.pkcs.PKCSObjectIdentifiers;
30 import org.apache.geronimo.util.asn1.pkcs.PrivateKeyInfo;
31 import org.apache.geronimo.util.asn1.pkcs.RSAPrivateKeyStructure;
32 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
33 import org.apache.geronimo.util.crypto.params.RSAPrivateCrtKeyParameters;
34
35 /**
36  * A provider representation for a RSA private key, with CRT factors included.
37  */

38 public class JCERSAPrivateCrtKey
39     extends JCERSAPrivateKey
40     implements RSAPrivateCrtKey JavaDoc
41 {
42     private BigInteger JavaDoc publicExponent;
43     private BigInteger JavaDoc primeP;
44     private BigInteger JavaDoc primeQ;
45     private BigInteger JavaDoc primeExponentP;
46     private BigInteger JavaDoc primeExponentQ;
47     private BigInteger JavaDoc crtCoefficient;
48
49     /**
50      * construct a private key from it's org.apache.geronimo.util.crypto equivalent.
51      *
52      * @param key the parameters object representing the private key.
53      */

54     JCERSAPrivateCrtKey(
55         RSAPrivateCrtKeyParameters key)
56     {
57         super(key);
58
59         this.publicExponent = key.getPublicExponent();
60         this.primeP = key.getP();
61         this.primeQ = key.getQ();
62         this.primeExponentP = key.getDP();
63         this.primeExponentQ = key.getDQ();
64         this.crtCoefficient = key.getQInv();
65     }
66
67     /**
68      * construct a private key from an RSAPrivateCrtKeySpec
69      *
70      * @param spec the spec to be used in construction.
71      */

72     JCERSAPrivateCrtKey(
73         RSAPrivateCrtKeySpec JavaDoc spec)
74     {
75         this.modulus = spec.getModulus();
76         this.publicExponent = spec.getPublicExponent();
77         this.privateExponent = spec.getPrivateExponent();
78         this.primeP = spec.getPrimeP();
79         this.primeQ = spec.getPrimeQ();
80         this.primeExponentP = spec.getPrimeExponentP();
81         this.primeExponentQ = spec.getPrimeExponentQ();
82         this.crtCoefficient = spec.getCrtCoefficient();
83     }
84
85     /**
86      * construct a private key from another RSAPrivateCrtKey.
87      *
88      * @param key the object implementing the RSAPrivateCrtKey interface.
89      */

90     JCERSAPrivateCrtKey(
91         RSAPrivateCrtKey JavaDoc key)
92     {
93         this.modulus = key.getModulus();
94         this.publicExponent = key.getPublicExponent();
95         this.privateExponent = key.getPrivateExponent();
96         this.primeP = key.getPrimeP();
97         this.primeQ = key.getPrimeQ();
98         this.primeExponentP = key.getPrimeExponentP();
99         this.primeExponentQ = key.getPrimeExponentQ();
100         this.crtCoefficient = key.getCrtCoefficient();
101     }
102
103     /**
104      * construct an RSA key from a private key info object.
105      */

106     JCERSAPrivateCrtKey(
107         PrivateKeyInfo info)
108     {
109         this(new RSAPrivateKeyStructure((ASN1Sequence)info.getPrivateKey()));
110     }
111
112     /**
113      * construct an RSA key from a ASN.1 RSA private key object.
114      */

115     JCERSAPrivateCrtKey(
116         RSAPrivateKeyStructure key)
117     {
118         this.modulus = key.getModulus();
119         this.publicExponent = key.getPublicExponent();
120         this.privateExponent = key.getPrivateExponent();
121         this.primeP = key.getPrime1();
122         this.primeQ = key.getPrime2();
123         this.primeExponentP = key.getExponent1();
124         this.primeExponentQ = key.getExponent2();
125         this.crtCoefficient = key.getCoefficient();
126     }
127
128     /**
129      * return the encoding format we produce in getEncoded().
130      *
131      * @return the encoding format we produce in getEncoded().
132      */

133     public String JavaDoc getFormat()
134     {
135         return "PKCS#8";
136     }
137
138     /**
139      * Return a PKCS8 representation of the key. The sequence returned
140      * represents a full PrivateKeyInfo object.
141      *
142      * @return a PKCS8 representation of the key.
143      */

144     public byte[] getEncoded()
145     {
146         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
147         DEROutputStream dOut = new DEROutputStream(bOut);
148         PrivateKeyInfo info = new PrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, new DERNull()), new RSAPrivateKeyStructure(getModulus(), getPublicExponent(), getPrivateExponent(), getPrimeP(), getPrimeQ(), getPrimeExponentP(), getPrimeExponentQ(), getCrtCoefficient()).getDERObject());
149
150         try
151         {
152             dOut.writeObject(info);
153             dOut.close();
154         }
155         catch (IOException JavaDoc e)
156         {
157             throw new RuntimeException JavaDoc("Error encoding RSA public key");
158         }
159
160         return bOut.toByteArray();
161     }
162
163     /**
164      * return the public exponent.
165      *
166      * @return the public exponent.
167      */

168     public BigInteger JavaDoc getPublicExponent()
169     {
170         return publicExponent;
171     }
172
173     /**
174      * return the prime P.
175      *
176      * @return the prime P.
177      */

178     public BigInteger JavaDoc getPrimeP()
179     {
180         return primeP;
181     }
182
183     /**
184      * return the prime Q.
185      *
186      * @return the prime Q.
187      */

188     public BigInteger JavaDoc getPrimeQ()
189     {
190         return primeQ;
191     }
192
193     /**
194      * return the prime exponent for P.
195      *
196      * @return the prime exponent for P.
197      */

198     public BigInteger JavaDoc getPrimeExponentP()
199     {
200         return primeExponentP;
201     }
202
203     /**
204      * return the prime exponent for Q.
205      *
206      * @return the prime exponent for Q.
207      */

208     public BigInteger JavaDoc getPrimeExponentQ()
209     {
210         return primeExponentQ;
211     }
212
213     /**
214      * return the CRT coefficient.
215      *
216      * @return the CRT coefficient.
217      */

218     public BigInteger JavaDoc getCrtCoefficient()
219     {
220         return crtCoefficient;
221     }
222
223     public boolean equals(Object JavaDoc o)
224     {
225         if ( !(o instanceof RSAPrivateCrtKey JavaDoc) )
226         {
227             return false;
228         }
229
230         if ( o == this )
231         {
232             return true;
233         }
234
235         RSAPrivateCrtKey JavaDoc key = (RSAPrivateCrtKey JavaDoc)o;
236
237         return this.getModulus().equals(key.getModulus())
238          && this.getPublicExponent().equals(key.getPublicExponent())
239          && this.getPrivateExponent().equals(key.getPrivateExponent())
240          && this.getPrimeP().equals(key.getPrimeP())
241          && this.getPrimeQ().equals(key.getPrimeQ())
242          && this.getPrimeExponentP().equals(key.getPrimeExponentP())
243          && this.getPrimeExponentQ().equals(key.getPrimeExponentQ())
244          && this.getCrtCoefficient().equals(key.getCrtCoefficient());
245     }
246
247     public String JavaDoc toString()
248     {
249         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
250         String JavaDoc nl = System.getProperty("line.separator");
251
252         buf.append("RSA Private CRT Key" + nl);
253         buf.append(" modulus: " + this.getModulus().toString(16) + nl);
254         buf.append(" public exponent: " + this.getPublicExponent().toString(16) + nl);
255         buf.append(" private exponent: " + this.getPrivateExponent().toString(16) + nl);
256         buf.append(" primeP: " + this.getPrimeP().toString(16) + nl);
257         buf.append(" primeQ: " + this.getPrimeQ().toString(16) + nl);
258         buf.append(" primeExponentP: " + this.getPrimeExponentP().toString(16) + nl);
259         buf.append(" primeExponentQ: " + this.getPrimeExponentQ().toString(16) + nl);
260         buf.append(" crtCoefficient: " + this.getCrtCoefficient().toString(16) + nl);
261
262         return buf.toString();
263     }
264 }
265
Popular Tags