KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > taglib > x509 > ReadP12


1 /*
2   Name: ReadP12.java
3   Licensing: LGPL
4
5   API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
6   Provider: Bouncy Castle (http://www.bouncycastle.org)
7
8   Disclaimer:
9
10   COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
11   EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
12   IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
13   RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
14   PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
15   ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
16   CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
17   HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
18 */

19
20 package net.sourceforge.jcetaglib.taglib.x509;
21
22 import net.sourceforge.jcetaglib.lib.Clean;
23 import net.sourceforge.jcetaglib.lib.X509Cert;
24 import org.bouncycastle.jce.provider.BouncyCastleProvider;
25 import org.bouncycastle.util.encoders.Base64;
26
27 import javax.servlet.jsp.JspException JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.security.KeyStore JavaDoc;
32 import java.security.PrivateKey JavaDoc;
33 import java.security.Security JavaDoc;
34 import java.security.cert.X509Certificate JavaDoc;
35
36 /**
37  * JSP tag for reading certificates from a PKCS#12 (P12) keystore
38  *
39  * @jsp.tag
40  * name="readp12"
41  * display-name="ReadP12"
42  * body-content="empty"
43  * example="<jce:readp12

44  * 	storefile=\"C:/keystores/bob.p12\"

45  * 	storeentry=\"user\"

46  * 	storepassword=\"<%= new StringBuffer(\"password\") %>\"

47  * 	certificate=\"cert\"

48  * 	cacertificate=\"cacert\">>"
49  *
50  * description="JSP tag for reading certificates from a PKCS#12 (P12) keystore"
51  *
52  * @author Gert Van Ham
53  * @author hamgert@users.sourceforge.net
54  * @author http://jcetaglib.sourceforge.net
55  * @version $Id: ReadP12.java,v 1.5 2004/04/15 07:28:36 hamgert Exp $
56  */

57
58 public class ReadP12 extends TagSupport JavaDoc {
59     private static final String JavaDoc PAGE = "page";
60     private static final String JavaDoc REQUEST = "request";
61     private static final String JavaDoc SESSION = "session";
62     private static final String JavaDoc APPLICATION = "application";
63
64     private String JavaDoc storefile; // tag attribute
65
private String JavaDoc storeentry; // tag attribute
66
private StringBuffer JavaDoc storepassword; // tag attribute
67

68     private boolean returnprivatekey = false;
69
70     private int scope = PageContext.PAGE_SCOPE;
71
72     private String JavaDoc certificate;
73     private String JavaDoc cacertificate;
74     private String JavaDoc privatekey;
75
76     private StringBuffer JavaDoc pkey;
77
78     public static int getScope(String JavaDoc scope) {
79         int ret = PageContext.PAGE_SCOPE; // default
80

81         if (REQUEST.equalsIgnoreCase(scope))
82             ret = PageContext.REQUEST_SCOPE;
83         else if (SESSION.equalsIgnoreCase(scope))
84             ret = PageContext.SESSION_SCOPE;
85         else if (APPLICATION.equalsIgnoreCase(scope))
86             ret = PageContext.APPLICATION_SCOPE;
87         else if (PAGE.equalsIgnoreCase(scope))
88             ret = PageContext.PAGE_SCOPE;
89
90         return ret;
91     } //getScope()
92

93     public int doEndTag() throws JspException JavaDoc {
94         Security.addProvider(new BouncyCastleProvider());
95
96         X509Certificate JavaDoc cert = null;
97         X509Certificate JavaDoc CAcert = null;
98         PrivateKey JavaDoc privKey = null;
99
100         try {
101             // read keystore
102
KeyStore JavaDoc store = KeyStore.getInstance("PKCS12", "BC");
103             store.load(new FileInputStream JavaDoc(storefile), storepassword.toString().toCharArray());
104
105             if (returnprivatekey) {
106                 privKey = X509Cert.getPrivateFromP12(storefile, storeentry, storepassword);
107             }
108
109             cert = X509Cert.getCertificateFromP12(storefile, storeentry, storepassword);
110             CAcert = X509Cert.getCACertificateFromP12(storefile, storeentry, storepassword);
111
112             // Return certificate
113
byte output[] = cert.getEncoded();
114             byte certB64[] = Base64.encode(output);
115
116             pageContext.setAttribute(certificate, "-----BEGIN CERTIFICATE-----\n" + new String JavaDoc(certB64) + "\n-----END CERTIFICATE-----", scope);
117
118             // Return CA certificate
119
byte CAoutput[] = CAcert.getEncoded();
120             byte CAcertB64[] = Base64.encode(CAoutput);
121
122             pageContext.setAttribute(cacertificate, "-----BEGIN CERTIFICATE-----\n" + new String JavaDoc(CAcertB64) + "\n-----END CERTIFICATE-----", scope);
123
124             // Return private key
125
if (returnprivatekey) {
126                 byte keyoutput[] = privKey.getEncoded();
127                 byte keyB64[] = Base64.encode(keyoutput);
128
129                 pkey = new StringBuffer JavaDoc("-----BEGIN PRIVATE KEY-----\n");
130                 pkey.append(new String JavaDoc(keyB64));
131                 pkey.append("\n-----END PRIVATE KEY-----");
132
133                 pageContext.setAttribute(privatekey, pkey, scope);
134
135                 Clean.blank(keyoutput);
136                 Clean.blank(keyB64);
137                 privKey = null;
138             }
139         } catch (Exception JavaDoc e) {
140             throw new JspException JavaDoc("JCE Exception: Unable to read keystore \"" + storefile + "\": "
141                     + e.getMessage(), e);
142         }
143
144         return EVAL_PAGE;
145     } // doEndTag()
146

147     public void release() {
148         // Cleanup all sensitive information
149
Clean.blank(pkey);
150
151         super.release();
152     } //release()
153

154     /**
155      * @jsp.attribute
156      * description="The PKCS#12 (P12) keystore to store the certificate"
157      * type="java.lang.String"
158      * required="true"
159      * rtexprvalue="true"
160      */

161     public void setStorefile(String JavaDoc storefile) {
162         this.storefile = storefile;
163     }
164
165     /**
166      * @jsp.attribute
167      * description="The PKCS#12 (P12) keystore entry name for this certificate"
168      * type="java.lang.String"
169      * required="true"
170      * rtexprvalue="true"
171      */

172     public void setStoreentry(String JavaDoc storeentry) {
173         this.storeentry = storeentry;
174     }
175
176     /**
177      * @jsp.attribute
178      * description="The PKCS#12 (P12) keystore password"
179      * type="java.lang.StringBuffer"
180      * required="true"
181      * rtexprvalue="true"
182      */

183     public void setStorepassword(StringBuffer JavaDoc storepassword) {
184         this.storepassword = storepassword;
185     }
186
187     /**
188      * @jsp.attribute
189      * description="Scope of the return variables. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
190      * type="java.lang.String"
191      * required="false"
192      * rtexprvalue="false"
193      */

194     public void setScope(String JavaDoc scope) {
195         this.scope = getScope(scope);
196     }
197
198     /**
199      * @jsp.attribute
200      * description="Indicates if the private key must be exported as well. Default is false"
201      * type="java.lang.Boolean"
202      * required="false"
203      * rtexprvalue="true"
204      */

205     public void setReturnprivatekey(boolean returnprivatekey) {
206         this.returnprivatekey = returnprivatekey;
207     }
208
209     /**
210      * @jsp.attribute
211      * description="Variable to store the certificate"
212      * type="java.lang.String"
213      * required="true"
214      * rtexprvalue="true"
215      */

216     public void setCertificate(String JavaDoc certificate) {
217         this.certificate = certificate;
218     }
219
220     public String JavaDoc getCertificate() {
221         return certificate;
222     }
223
224     /**
225      * @jsp.attribute
226      * description="Variable to store the CA certificate"
227      * type="java.lang.String"
228      * required="true"
229      * rtexprvalue="false"
230      */

231     public void setCacertificate(String JavaDoc cacertificate) {
232         this.cacertificate = cacertificate;
233     }
234
235     public String JavaDoc getCacertificate() {
236         return cacertificate;
237     }
238
239     /**
240      * @jsp.attribute
241      * description="Variable to store the private key (as StringBuffer)"
242      * type="java.lang.String"
243      * required="false"
244      * rtexprvalue="false"
245      */

246     public void setPrivatekey(String JavaDoc privatekey) {
247         this.privatekey = privatekey;
248     }
249
250     public String JavaDoc getPrivatekey() {
251         return privatekey;
252     }
253 }
Popular Tags