KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > util > keystore > PKCS12KeyStore


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/util/keystore/PKCS12KeyStore.java,v 1.3 2004/02/13 02:40:55 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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
19 package org.apache.jmeter.util.keystore;
20
21 import iaik.pkcs.pkcs12.PKCS12;
22 import iaik.pkcs.pkcs12.KeyBag;
23 import iaik.pkcs.pkcs12.CertificateBag;
24 import iaik.pkcs.PKCSParsingException;
25 import iaik.pkcs.PKCSException;
26 import iaik.x509.ChainVerifier;
27 import iaik.utils.Util;
28
29 import java.security.cert.X509Certificate JavaDoc;
30 import java.security.cert.CertificateException JavaDoc;
31 import java.security.PrivateKey JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.Arrays JavaDoc;
35
36 /**
37  * Use this Keystore for iSaSiLk SSL Managers.
38  *
39  * @author <a HREF="bloritsch@apache.org">Berin Loritsch</a>
40  * @version CVS $Revision: 1.3 $ $Date: 2004/02/13 02:40:55 $
41  */

42 public class PKCS12KeyStore extends JmeterKeyStore
43 {
44     /** The Certificate chain */
45     private X509Certificate JavaDoc[] certChain;
46
47     /** The private key */
48     private PrivateKey JavaDoc key;
49
50     /** The alias */
51     private String JavaDoc alias;
52
53     public PKCS12KeyStore(String JavaDoc type) throws Exception JavaDoc
54     {
55         if (!"PKCS12".equalsIgnoreCase(type))
56         {
57             throw new Exception JavaDoc("Invalid keystore type");
58         }
59     }
60
61     public final String JavaDoc getAlias()
62     {
63         return this.alias;
64     }
65
66     /**
67      * Process PKCS12 input stream into the private key and certificate chain.
68      */

69     public void load(InputStream JavaDoc is, String JavaDoc pword)
70         throws IOException JavaDoc, PKCSException, CertificateException JavaDoc
71     {
72         PKCS12 p12 = new PKCS12(is);
73         is.close();
74
75         p12.decrypt(pword.toCharArray());
76
77         KeyBag keyBag = p12.getKeyBag();
78
79         if (null == keyBag)
80         {
81             throw new PKCSException("No private key found");
82         }
83
84         byte[] keyBagLocalKeyId = keyBag.getLocalKeyID();
85
86         this.key = keyBag.getPrivateKey();
87
88         CertificateBag[] certBags = p12.getCertificateBags();
89         if ((null == certBags) || (certBags.length == 0))
90         {
91             throw new PKCSException("No certificates found");
92         }
93
94         this.alias = new String JavaDoc(keyBagLocalKeyId);
95         X509Certificate JavaDoc myCert = null;
96
97         for (int i = 0; i < certBags.length; i++)
98         {
99             byte[] certBagLocalKeyId = certBags[i].getLocalKeyID();
100             if ((null != keyBagLocalKeyId) && (null != certBagLocalKeyId))
101             {
102                 if (Arrays.equals(certBagLocalKeyId, keyBagLocalKeyId))
103                 {
104                     myCert = certBags[i].getCertificate();
105                     break;
106                 }
107             }
108         }
109
110         if (null == myCert)
111         {
112             throw new PKCSException("No owner certificate found");
113         }
114
115         iaik.x509.X509Certificate[] certChain =
116             CertificateBag.getCertificates(certBags);
117         this.certChain = Util.arrangeCertificateChain(certChain, false);
118     }
119
120     /**
121      * Get the ordered certificate chain.
122      */

123     public final X509Certificate JavaDoc[] getCertificateChain()
124     {
125         return this.certChain;
126     }
127
128     /**
129      * Return the private Key
130      */

131     public final PrivateKey JavaDoc getPrivateKey()
132     {
133         return this.key;
134     }
135 }
Popular Tags