KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.security.InvalidKeyException JavaDoc;
24 import java.security.Key JavaDoc;
25 import java.security.KeyFactorySpi JavaDoc;
26 import java.security.PrivateKey JavaDoc;
27 import java.security.PublicKey JavaDoc;
28 import java.security.interfaces.DSAPrivateKey JavaDoc;
29 import java.security.interfaces.DSAPublicKey JavaDoc;
30 import java.security.interfaces.RSAPrivateCrtKey JavaDoc;
31 import java.security.interfaces.RSAPrivateKey JavaDoc;
32 import java.security.interfaces.RSAPublicKey JavaDoc;
33 import java.security.spec.DSAPrivateKeySpec JavaDoc;
34 import java.security.spec.DSAPublicKeySpec JavaDoc;
35 import java.security.spec.InvalidKeySpecException JavaDoc;
36 import java.security.spec.KeySpec JavaDoc;
37 import java.security.spec.PKCS8EncodedKeySpec JavaDoc;
38 import java.security.spec.RSAPrivateCrtKeySpec JavaDoc;
39 import java.security.spec.RSAPrivateKeySpec JavaDoc;
40 import java.security.spec.RSAPublicKeySpec JavaDoc;
41 import java.security.spec.X509EncodedKeySpec JavaDoc;
42
43 import javax.crypto.interfaces.DHPrivateKey;
44 import javax.crypto.interfaces.DHPublicKey;
45 import javax.crypto.spec.DHPrivateKeySpec;
46 import javax.crypto.spec.DHPublicKeySpec;
47
48 import org.apache.geronimo.util.asn1.ASN1InputStream;
49 import org.apache.geronimo.util.asn1.ASN1Sequence;
50 import org.apache.geronimo.util.asn1.cryptopro.CryptoProObjectIdentifiers;
51 import org.apache.geronimo.util.asn1.oiw.OIWObjectIdentifiers;
52 import org.apache.geronimo.util.asn1.pkcs.PKCSObjectIdentifiers;
53 import org.apache.geronimo.util.asn1.pkcs.PrivateKeyInfo;
54 import org.apache.geronimo.util.asn1.pkcs.RSAPrivateKeyStructure;
55 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
56 import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo;
57 import org.apache.geronimo.util.asn1.x509.X509ObjectIdentifiers;
58 import org.apache.geronimo.util.asn1.x9.X9ObjectIdentifiers;
59 import org.apache.geronimo.util.jce.provider.JCEDHPrivateKey;
60 import org.apache.geronimo.util.jce.provider.JCEDHPublicKey;
61
62 public abstract class JDKKeyFactory
63     extends KeyFactorySpi JavaDoc
64 {
65     public JDKKeyFactory()
66     {
67     }
68
69     protected KeySpec JavaDoc engineGetKeySpec(
70         Key JavaDoc key,
71         Class JavaDoc spec)
72     throws InvalidKeySpecException JavaDoc
73     {
74        if (spec.isAssignableFrom(PKCS8EncodedKeySpec JavaDoc.class) && key.getFormat().equals("PKCS#8"))
75        {
76                return new PKCS8EncodedKeySpec JavaDoc(key.getEncoded());
77        }
78        else if (spec.isAssignableFrom(X509EncodedKeySpec JavaDoc.class) && key.getFormat().equals("X.509"))
79        {
80                return new X509EncodedKeySpec JavaDoc(key.getEncoded());
81        }
82        else if (spec.isAssignableFrom(RSAPublicKeySpec JavaDoc.class) && key instanceof RSAPublicKey JavaDoc)
83        {
84             RSAPublicKey JavaDoc k = (RSAPublicKey JavaDoc)key;
85
86             return new RSAPublicKeySpec JavaDoc(k.getModulus(), k.getPublicExponent());
87        }
88        else if (spec.isAssignableFrom(RSAPrivateKeySpec JavaDoc.class) && key instanceof RSAPrivateKey JavaDoc)
89        {
90             RSAPrivateKey JavaDoc k = (RSAPrivateKey JavaDoc)key;
91
92             return new RSAPrivateKeySpec JavaDoc(k.getModulus(), k.getPrivateExponent());
93        }
94        else if (spec.isAssignableFrom(RSAPrivateCrtKeySpec JavaDoc.class) && key instanceof RSAPrivateCrtKey JavaDoc)
95        {
96             RSAPrivateCrtKey JavaDoc k = (RSAPrivateCrtKey JavaDoc)key;
97
98             return new RSAPrivateCrtKeySpec JavaDoc(
99                             k.getModulus(), k.getPublicExponent(),
100                             k.getPrivateExponent(),
101                             k.getPrimeP(), k.getPrimeQ(),
102                             k.getPrimeExponentP(), k.getPrimeExponentQ(),
103                             k.getCrtCoefficient());
104        }
105        else if (spec.isAssignableFrom(DHPrivateKeySpec.class) && key instanceof DHPrivateKey)
106        {
107            DHPrivateKey k = (DHPrivateKey)key;
108
109            return new DHPrivateKeySpec(k.getX(), k.getParams().getP(), k.getParams().getG());
110        }
111        else if (spec.isAssignableFrom(DHPublicKeySpec.class) && key instanceof DHPublicKey)
112        {
113            DHPublicKey k = (DHPublicKey)key;
114
115            return new DHPublicKeySpec(k.getY(), k.getParams().getP(), k.getParams().getG());
116        }
117
118         throw new RuntimeException JavaDoc("not implemented yet " + key + " " + spec);
119     }
120
121     protected Key JavaDoc engineTranslateKey(
122         Key JavaDoc key)
123         throws InvalidKeyException JavaDoc
124     {
125         if (key instanceof RSAPublicKey JavaDoc)
126         {
127             return new JCERSAPublicKey((RSAPublicKey JavaDoc)key);
128         }
129         else if (key instanceof RSAPrivateCrtKey JavaDoc)
130         {
131             return new JCERSAPrivateCrtKey((RSAPrivateCrtKey JavaDoc)key);
132         }
133         else if (key instanceof RSAPrivateKey JavaDoc)
134         {
135             return new JCERSAPrivateKey((RSAPrivateKey JavaDoc)key);
136         }
137         else if (key instanceof DHPublicKey)
138         {
139             return new JCEDHPublicKey((DHPublicKey)key);
140         }
141         else if (key instanceof DHPrivateKey)
142         {
143             return new JCEDHPrivateKey((DHPrivateKey)key);
144         }
145         else if (key instanceof DSAPublicKey JavaDoc)
146         {
147             return new JDKDSAPublicKey((DSAPublicKey JavaDoc)key);
148         }
149         else if (key instanceof DSAPrivateKey JavaDoc)
150         {
151             return new JDKDSAPrivateKey((DSAPrivateKey JavaDoc)key);
152         }
153         throw new InvalidKeyException JavaDoc("key type unknown");
154     }
155
156     /**
157      * create a public key from the given DER encoded input stream.
158      */

159     static PublicKey JavaDoc createPublicKeyFromDERStream(
160         InputStream JavaDoc in)
161         throws IOException JavaDoc
162     {
163         return createPublicKeyFromPublicKeyInfo(
164                 new SubjectPublicKeyInfo((ASN1Sequence)(new ASN1InputStream(in).readObject())));
165     }
166
167     /**
168      * create a public key from the given public key info object.
169      */

170     static PublicKey JavaDoc createPublicKeyFromPublicKeyInfo(
171         SubjectPublicKeyInfo info)
172     {
173         AlgorithmIdentifier algId = info.getAlgorithmId();
174
175         if (algId.getObjectId().equals(PKCSObjectIdentifiers.rsaEncryption)
176             || algId.getObjectId().equals(X509ObjectIdentifiers.id_ea_rsa))
177         {
178               return new JCERSAPublicKey(info);
179         }
180         else if (algId.getObjectId().equals(PKCSObjectIdentifiers.dhKeyAgreement))
181         {
182               return new JCEDHPublicKey(info);
183         }
184         else if (algId.getObjectId().equals(X9ObjectIdentifiers.dhpublicnumber))
185         {
186               return new JCEDHPublicKey(info);
187         }
188         else if (algId.getObjectId().equals(X9ObjectIdentifiers.id_dsa))
189         {
190               return new JDKDSAPublicKey(info);
191         }
192         else if (algId.getObjectId().equals(OIWObjectIdentifiers.dsaWithSHA1))
193         {
194               return new JDKDSAPublicKey(info);
195         }
196         else
197         {
198             throw new RuntimeException JavaDoc("algorithm identifier in key not recognised");
199         }
200     }
201
202     /**
203      * create a private key from the given DER encoded input stream.
204      */

205     static PrivateKey JavaDoc createPrivateKeyFromDERStream(
206         InputStream JavaDoc in)
207         throws IOException JavaDoc
208     {
209         return createPrivateKeyFromPrivateKeyInfo(
210                 new PrivateKeyInfo((ASN1Sequence)(new ASN1InputStream(in).readObject())));
211     }
212
213     /**
214      * create a private key from the given public key info object.
215      */

216     static PrivateKey JavaDoc createPrivateKeyFromPrivateKeyInfo(
217         PrivateKeyInfo info)
218     {
219         AlgorithmIdentifier algId = info.getAlgorithmId();
220
221         if (algId.getObjectId().equals(PKCSObjectIdentifiers.rsaEncryption))
222         {
223               return new JCERSAPrivateCrtKey(info);
224         }
225         else if (algId.getObjectId().equals(PKCSObjectIdentifiers.dhKeyAgreement))
226         {
227               return new JCEDHPrivateKey(info);
228         }
229         else if (algId.getObjectId().equals(X9ObjectIdentifiers.id_dsa))
230         {
231               return new JDKDSAPrivateKey(info);
232         }
233         else
234         {
235             throw new RuntimeException JavaDoc("algorithm identifier in key not recognised");
236         }
237     }
238
239     public static class RSA
240         extends JDKKeyFactory
241     {
242         public RSA()
243         {
244         }
245
246         protected PrivateKey JavaDoc engineGeneratePrivate(
247             KeySpec JavaDoc keySpec)
248             throws InvalidKeySpecException JavaDoc
249         {
250             if (keySpec instanceof PKCS8EncodedKeySpec JavaDoc)
251             {
252                 try
253                 {
254                     return JDKKeyFactory.createPrivateKeyFromDERStream(
255                                 new ByteArrayInputStream JavaDoc(((PKCS8EncodedKeySpec JavaDoc)keySpec).getEncoded()));
256                 }
257                 catch (Exception JavaDoc e)
258                 {
259                     //
260
// in case it's just a RSAPrivateKey object...
261
//
262
try
263                     {
264                         return new JCERSAPrivateCrtKey(
265                             new RSAPrivateKeyStructure(
266                                 (ASN1Sequence)new ASN1InputStream(new ByteArrayInputStream JavaDoc(((PKCS8EncodedKeySpec JavaDoc)keySpec).getEncoded())).readObject()));
267                     }
268                     catch (Exception JavaDoc ex)
269                     {
270                         throw new InvalidKeySpecException JavaDoc(ex.toString());
271                     }
272                 }
273             }
274             else if (keySpec instanceof RSAPrivateCrtKeySpec JavaDoc)
275             {
276                 return new JCERSAPrivateCrtKey((RSAPrivateCrtKeySpec JavaDoc)keySpec);
277             }
278             else if (keySpec instanceof RSAPrivateKeySpec JavaDoc)
279             {
280                 return new JCERSAPrivateKey((RSAPrivateKeySpec JavaDoc)keySpec);
281             }
282
283             throw new InvalidKeySpecException JavaDoc("Unknown KeySpec type.");
284         }
285
286         protected PublicKey JavaDoc engineGeneratePublic(
287             KeySpec JavaDoc keySpec)
288             throws InvalidKeySpecException JavaDoc
289         {
290             if (keySpec instanceof X509EncodedKeySpec JavaDoc)
291             {
292                 try
293                 {
294                     return JDKKeyFactory.createPublicKeyFromDERStream(
295                                 new ByteArrayInputStream JavaDoc(((X509EncodedKeySpec JavaDoc)keySpec).getEncoded()));
296                 }
297                 catch (Exception JavaDoc e)
298                 {
299                     throw new InvalidKeySpecException JavaDoc(e.toString());
300                 }
301             }
302             else if (keySpec instanceof RSAPublicKeySpec JavaDoc)
303             {
304                 return new JCERSAPublicKey((RSAPublicKeySpec JavaDoc)keySpec);
305             }
306
307             throw new InvalidKeySpecException JavaDoc("Unknown KeySpec type.");
308         }
309     }
310
311     public static class DH
312         extends JDKKeyFactory
313     {
314         public DH()
315         {
316         }
317
318         protected PrivateKey JavaDoc engineGeneratePrivate(
319             KeySpec JavaDoc keySpec)
320             throws InvalidKeySpecException JavaDoc
321         {
322             if (keySpec instanceof PKCS8EncodedKeySpec JavaDoc)
323             {
324                 try
325                 {
326                     return JDKKeyFactory.createPrivateKeyFromDERStream(
327                                 new ByteArrayInputStream JavaDoc(((PKCS8EncodedKeySpec JavaDoc)keySpec).getEncoded()));
328                 }
329                 catch (Exception JavaDoc e)
330                 {
331                     throw new InvalidKeySpecException JavaDoc(e.toString());
332                 }
333             }
334             else if (keySpec instanceof DHPrivateKeySpec)
335             {
336                 return new JCEDHPrivateKey((DHPrivateKeySpec)keySpec);
337             }
338
339             throw new InvalidKeySpecException JavaDoc("Unknown KeySpec type.");
340         }
341
342         protected PublicKey JavaDoc engineGeneratePublic(
343             KeySpec JavaDoc keySpec)
344             throws InvalidKeySpecException JavaDoc
345         {
346             if (keySpec instanceof X509EncodedKeySpec JavaDoc)
347             {
348                 try
349                 {
350                     return JDKKeyFactory.createPublicKeyFromDERStream(
351                                 new ByteArrayInputStream JavaDoc(((X509EncodedKeySpec JavaDoc)keySpec).getEncoded()));
352                 }
353                 catch (Exception JavaDoc e)
354                 {
355                     throw new InvalidKeySpecException JavaDoc(e.toString());
356                 }
357             }
358             else if (keySpec instanceof DHPublicKeySpec)
359             {
360                 return new JCEDHPublicKey((DHPublicKeySpec)keySpec);
361             }
362
363             throw new InvalidKeySpecException JavaDoc("Unknown KeySpec type.");
364         }
365     }
366
367     public static class DSA
368         extends JDKKeyFactory
369     {
370         public DSA()
371         {
372         }
373
374         protected PrivateKey JavaDoc engineGeneratePrivate(
375             KeySpec JavaDoc keySpec)
376             throws InvalidKeySpecException JavaDoc
377         {
378             if (keySpec instanceof PKCS8EncodedKeySpec JavaDoc)
379             {
380                 try
381                 {
382                     return JDKKeyFactory.createPrivateKeyFromDERStream(
383                                 new ByteArrayInputStream JavaDoc(((PKCS8EncodedKeySpec JavaDoc)keySpec).getEncoded()));
384                 }
385                 catch (Exception JavaDoc e)
386                 {
387                     throw new InvalidKeySpecException JavaDoc(e.toString());
388                 }
389             }
390             else if (keySpec instanceof DSAPrivateKeySpec JavaDoc)
391             {
392                 return new JDKDSAPrivateKey((DSAPrivateKeySpec JavaDoc)keySpec);
393             }
394
395             throw new InvalidKeySpecException JavaDoc("Unknown KeySpec type.");
396         }
397
398         protected PublicKey JavaDoc engineGeneratePublic(
399             KeySpec JavaDoc keySpec)
400             throws InvalidKeySpecException JavaDoc
401         {
402             if (keySpec instanceof X509EncodedKeySpec JavaDoc)
403             {
404                 try
405                 {
406                     return JDKKeyFactory.createPublicKeyFromDERStream(
407                                 new ByteArrayInputStream JavaDoc(((X509EncodedKeySpec JavaDoc)keySpec).getEncoded()));
408                 }
409                 catch (Exception JavaDoc e)
410                 {
411                     throw new InvalidKeySpecException JavaDoc(e.toString());
412                 }
413             }
414             else if (keySpec instanceof DSAPublicKeySpec JavaDoc)
415             {
416                 return new JDKDSAPublicKey((DSAPublicKeySpec JavaDoc)keySpec);
417             }
418
419             throw new InvalidKeySpecException JavaDoc("Unknown KeySpec type.");
420         }
421     }
422
423
424     public static class EC
425         extends JDKKeyFactory
426     {
427         String JavaDoc algorithm;
428
429         public EC()
430         {
431             this("EC");
432         }
433
434         public EC(
435             String JavaDoc algorithm)
436         {
437             this.algorithm = algorithm;
438         }
439
440         protected PrivateKey JavaDoc engineGeneratePrivate(
441             KeySpec JavaDoc keySpec)
442             throws InvalidKeySpecException JavaDoc
443         {
444             if (keySpec instanceof PKCS8EncodedKeySpec JavaDoc)
445             {
446                 try
447                 {
448                     return JDKKeyFactory.createPrivateKeyFromDERStream(
449                                 new ByteArrayInputStream JavaDoc(((PKCS8EncodedKeySpec JavaDoc)keySpec).getEncoded()));
450                 }
451                 catch (Exception JavaDoc e)
452                 {
453                     throw new InvalidKeySpecException JavaDoc(e.toString());
454                 }
455             }
456
457             throw new InvalidKeySpecException JavaDoc("Unknown KeySpec type.");
458         }
459
460         protected PublicKey JavaDoc engineGeneratePublic(
461             KeySpec JavaDoc keySpec)
462             throws InvalidKeySpecException JavaDoc
463         {
464             if (keySpec instanceof X509EncodedKeySpec JavaDoc)
465             {
466                 try
467                 {
468                     return JDKKeyFactory.createPublicKeyFromDERStream(
469                                 new ByteArrayInputStream JavaDoc(((X509EncodedKeySpec JavaDoc)keySpec).getEncoded()));
470                 }
471                 catch (Exception JavaDoc e)
472                 {
473                     throw new InvalidKeySpecException JavaDoc(e.toString());
474                 }
475             }
476
477             throw new InvalidKeySpecException JavaDoc("Unknown KeySpec type.");
478         }
479     }
480 }
481
Popular Tags