View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.jce.provider;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.ObjectInputStream;
25  import java.io.ObjectOutputStream;
26  import java.math.BigInteger;
27  import java.security.interfaces.RSAPrivateKey;
28  import java.security.spec.RSAPrivateKeySpec;
29  import java.util.Enumeration;
30  import java.util.Hashtable;
31  import java.util.Vector;
32  
33  import org.apache.geronimo.util.asn1.ASN1InputStream;
34  import org.apache.geronimo.util.asn1.ASN1OutputStream;
35  import org.apache.geronimo.util.asn1.DEREncodable;
36  import org.apache.geronimo.util.asn1.DERObjectIdentifier;
37  import org.apache.geronimo.util.crypto.params.RSAKeyParameters;
38  import org.apache.geronimo.util.jce.interfaces.PKCS12BagAttributeCarrier;
39  
40  public class JCERSAPrivateKey
41      implements RSAPrivateKey, PKCS12BagAttributeCarrier
42  {
43      protected BigInteger modulus;
44      protected BigInteger privateExponent;
45  
46      private Hashtable   pkcs12Attributes = new Hashtable();
47      private Vector      pkcs12Ordering = new Vector();
48  
49      protected JCERSAPrivateKey()
50      {
51      }
52  
53      JCERSAPrivateKey(
54          RSAKeyParameters key)
55      {
56          this.modulus = key.getModulus();
57          this.privateExponent = key.getExponent();
58      }
59  
60      JCERSAPrivateKey(
61          RSAPrivateKeySpec spec)
62      {
63          this.modulus = spec.getModulus();
64          this.privateExponent = spec.getPrivateExponent();
65      }
66  
67      JCERSAPrivateKey(
68          RSAPrivateKey key)
69      {
70          this.modulus = key.getModulus();
71          this.privateExponent = key.getPrivateExponent();
72      }
73  
74      public BigInteger getModulus()
75      {
76          return modulus;
77      }
78  
79      public BigInteger getPrivateExponent()
80      {
81          return privateExponent;
82      }
83  
84      public String getAlgorithm()
85      {
86          return "RSA";
87      }
88  
89      public String getFormat()
90      {
91          return "NULL";
92      }
93  
94      public byte[] getEncoded()
95      {
96          return null;
97      }
98  
99      public boolean equals(Object o)
100     {
101         if ( !(o instanceof RSAPrivateKey) )
102         {
103             return false;
104         }
105 
106         if ( o == this )
107         {
108             return true;
109         }
110 
111         RSAPrivateKey key = (RSAPrivateKey)o;
112 
113         return getModulus().equals(key.getModulus())
114             && getPrivateExponent().equals(key.getPrivateExponent());
115     }
116 
117     public void setBagAttribute(
118         DERObjectIdentifier oid,
119         DEREncodable        attribute)
120     {
121         pkcs12Attributes.put(oid, attribute);
122         pkcs12Ordering.addElement(oid);
123     }
124 
125     public DEREncodable getBagAttribute(
126         DERObjectIdentifier oid)
127     {
128         return (DEREncodable)pkcs12Attributes.get(oid);
129     }
130 
131     public Enumeration getBagAttributeKeys()
132     {
133         return pkcs12Ordering.elements();
134     }
135 
136     private void readObject(
137         ObjectInputStream   in)
138         throws IOException, ClassNotFoundException
139     {
140         this.modulus = (BigInteger)in.readObject();
141 
142         Object  obj = in.readObject();
143 
144         if (obj instanceof Hashtable)
145         {
146             this.pkcs12Attributes = (Hashtable)obj;
147             this.pkcs12Ordering = (Vector)in.readObject();
148         }
149         else
150         {
151             this.pkcs12Attributes = new Hashtable();
152             this.pkcs12Ordering = new Vector();
153 
154             ByteArrayInputStream    bIn = new ByteArrayInputStream((byte[])obj);
155             ASN1InputStream         aIn = new ASN1InputStream(bIn);
156 
157             DERObjectIdentifier    oid;
158 
159             while ((oid = (DERObjectIdentifier)aIn.readObject()) != null)
160             {
161                 this.setBagAttribute(oid, aIn.readObject());
162             }
163         }
164 
165         this.privateExponent = (BigInteger)in.readObject();
166     }
167 
168     private void writeObject(
169         ObjectOutputStream  out)
170         throws IOException
171     {
172         out.writeObject(modulus);
173 
174         if (pkcs12Ordering.size() == 0)
175         {
176             out.writeObject(pkcs12Attributes);
177             out.writeObject(pkcs12Ordering);
178         }
179         else
180         {
181             ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
182             ASN1OutputStream        aOut = new ASN1OutputStream(bOut);
183 
184             Enumeration             e = this.getBagAttributeKeys();
185 
186             while (e.hasMoreElements())
187             {
188                 DEREncodable    oid = (DEREncodable)e.nextElement();
189 
190                 aOut.writeObject(oid);
191                 aOut.writeObject(pkcs12Attributes.get(oid));
192             }
193 
194             out.writeObject(bOut.toByteArray());
195         }
196 
197         out.writeObject(privateExponent);
198     }
199 }