KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.math.BigInteger JavaDoc;
26 import java.security.interfaces.RSAPrivateKey JavaDoc;
27 import java.security.spec.RSAPrivateKeySpec JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import org.apache.geronimo.util.asn1.ASN1InputStream;
33 import org.apache.geronimo.util.asn1.ASN1OutputStream;
34 import org.apache.geronimo.util.asn1.DEREncodable;
35 import org.apache.geronimo.util.asn1.DERObjectIdentifier;
36 import org.apache.geronimo.util.crypto.params.RSAKeyParameters;
37 import org.apache.geronimo.util.jce.interfaces.PKCS12BagAttributeCarrier;
38
39 public class JCERSAPrivateKey
40     implements RSAPrivateKey JavaDoc, PKCS12BagAttributeCarrier
41 {
42     protected BigInteger JavaDoc modulus;
43     protected BigInteger JavaDoc privateExponent;
44
45     private Hashtable JavaDoc pkcs12Attributes = new Hashtable JavaDoc();
46     private Vector JavaDoc pkcs12Ordering = new Vector JavaDoc();
47
48     protected JCERSAPrivateKey()
49     {
50     }
51
52     JCERSAPrivateKey(
53         RSAKeyParameters key)
54     {
55         this.modulus = key.getModulus();
56         this.privateExponent = key.getExponent();
57     }
58
59     JCERSAPrivateKey(
60         RSAPrivateKeySpec JavaDoc spec)
61     {
62         this.modulus = spec.getModulus();
63         this.privateExponent = spec.getPrivateExponent();
64     }
65
66     JCERSAPrivateKey(
67         RSAPrivateKey JavaDoc key)
68     {
69         this.modulus = key.getModulus();
70         this.privateExponent = key.getPrivateExponent();
71     }
72
73     public BigInteger JavaDoc getModulus()
74     {
75         return modulus;
76     }
77
78     public BigInteger JavaDoc getPrivateExponent()
79     {
80         return privateExponent;
81     }
82
83     public String JavaDoc getAlgorithm()
84     {
85         return "RSA";
86     }
87
88     public String JavaDoc getFormat()
89     {
90         return "NULL";
91     }
92
93     public byte[] getEncoded()
94     {
95         return null;
96     }
97
98     public boolean equals(Object JavaDoc o)
99     {
100         if ( !(o instanceof RSAPrivateKey JavaDoc) )
101         {
102             return false;
103         }
104
105         if ( o == this )
106         {
107             return true;
108         }
109
110         RSAPrivateKey JavaDoc key = (RSAPrivateKey JavaDoc)o;
111
112         return getModulus().equals(key.getModulus())
113             && getPrivateExponent().equals(key.getPrivateExponent());
114     }
115
116     public void setBagAttribute(
117         DERObjectIdentifier oid,
118         DEREncodable attribute)
119     {
120         pkcs12Attributes.put(oid, attribute);
121         pkcs12Ordering.addElement(oid);
122     }
123
124     public DEREncodable getBagAttribute(
125         DERObjectIdentifier oid)
126     {
127         return (DEREncodable)pkcs12Attributes.get(oid);
128     }
129
130     public Enumeration JavaDoc getBagAttributeKeys()
131     {
132         return pkcs12Ordering.elements();
133     }
134
135     private void readObject(
136         ObjectInputStream JavaDoc in)
137         throws IOException JavaDoc, ClassNotFoundException JavaDoc
138     {
139         this.modulus = (BigInteger JavaDoc)in.readObject();
140
141         Object JavaDoc obj = in.readObject();
142
143         if (obj instanceof Hashtable JavaDoc)
144         {
145             this.pkcs12Attributes = (Hashtable JavaDoc)obj;
146             this.pkcs12Ordering = (Vector JavaDoc)in.readObject();
147         }
148         else
149         {
150             this.pkcs12Attributes = new Hashtable JavaDoc();
151             this.pkcs12Ordering = new Vector JavaDoc();
152
153             ByteArrayInputStream JavaDoc bIn = new ByteArrayInputStream JavaDoc((byte[])obj);
154             ASN1InputStream aIn = new ASN1InputStream(bIn);
155
156             DERObjectIdentifier oid;
157
158             while ((oid = (DERObjectIdentifier)aIn.readObject()) != null)
159             {
160                 this.setBagAttribute(oid, aIn.readObject());
161             }
162         }
163
164         this.privateExponent = (BigInteger JavaDoc)in.readObject();
165     }
166
167     private void writeObject(
168         ObjectOutputStream JavaDoc out)
169         throws IOException JavaDoc
170     {
171         out.writeObject(modulus);
172
173         if (pkcs12Ordering.size() == 0)
174         {
175             out.writeObject(pkcs12Attributes);
176             out.writeObject(pkcs12Ordering);
177         }
178         else
179         {
180             ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
181             ASN1OutputStream aOut = new ASN1OutputStream(bOut);
182
183             Enumeration JavaDoc e = this.getBagAttributeKeys();
184
185             while (e.hasMoreElements())
186             {
187                 DEREncodable oid = (DEREncodable)e.nextElement();
188
189                 aOut.writeObject(oid);
190                 aOut.writeObject(pkcs12Attributes.get(oid));
191             }
192
193             out.writeObject(bOut.toByteArray());
194         }
195
196         out.writeObject(privateExponent);
197     }
198 }
199
Popular Tags