KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: CertificateInfo.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.CertTools;
23 import net.sourceforge.jcetaglib.lib.Clean;
24 import net.sourceforge.jcetaglib.lib.X509Cert;
25 import org.bouncycastle.jce.provider.BouncyCastleProvider;
26
27 import javax.servlet.jsp.JspException JavaDoc;
28 import javax.servlet.jsp.JspWriter JavaDoc;
29 import javax.servlet.jsp.PageContext JavaDoc;
30 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
31 import java.io.ByteArrayInputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.security.Security JavaDoc;
35 import java.security.cert.X509Certificate JavaDoc;
36
37 /**
38  * JSP tag used retrieving information from a X.509 certificate
39  *
40  * @jsp.tag
41  * name="certificateinfo"
42  * display-name="CertificateInfo"
43  * body-content="empty"
44  * example="<%-- Prints the certificate information --%>

45  * <jce:certificateinfo

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

47  * 	storeentry=\"user\"

48  * 	storepassword=\"password\"/>"
49  *
50  * description="JSP tag used retrieving information from a X.509 certificate"
51  *
52  * @author Gert Van Ham
53  * @author hamgert@users.sourceforge.net
54  * @author http://jcetaglib.sourceforge.net
55  * @version $Id: CertificateInfo.java,v 1.7 2004/04/15 07:28:36 hamgert Exp $
56  */

57
58 public class CertificateInfo 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     // return info
65
private int scope = PageContext.PAGE_SCOPE;
66     private String JavaDoc subject;
67     private String JavaDoc issuer;
68     private String JavaDoc validfrom;
69     private String JavaDoc validto;
70     private String JavaDoc algorithm;
71     private String JavaDoc serialnumber;
72     private String JavaDoc fingerprint;
73
74     /* Attributes for X.509 keystore */
75
76     // P12 keystore...
77
private String JavaDoc storefile; // tag attribute
78
private String JavaDoc storeentry; // tag attribute
79
private StringBuffer JavaDoc storepassword; // tag attribute
80

81     // ... OR PEM string
82
private String JavaDoc pemstring; // tag attribute
83

84     // ... OR PEM file
85
private String JavaDoc pemfile; // tag attribute
86

87     public static int getScope(String JavaDoc scope) {
88         int ret = PageContext.PAGE_SCOPE; // default
89

90         if (REQUEST.equalsIgnoreCase(scope))
91             ret = PageContext.REQUEST_SCOPE;
92         else if (SESSION.equalsIgnoreCase(scope))
93             ret = PageContext.SESSION_SCOPE;
94         else if (APPLICATION.equalsIgnoreCase(scope))
95             ret = PageContext.APPLICATION_SCOPE;
96         else if (PAGE.equalsIgnoreCase(scope))
97             ret = PageContext.PAGE_SCOPE;
98
99         return ret;
100     } //getScope()
101

102     public int doEndTag() throws JspException JavaDoc {
103         Security.addProvider(new BouncyCastleProvider());
104
105         X509Certificate JavaDoc cert = null;
106
107         try {
108             // Retrieve the certificate from one of the three possible keystores
109
if (storefile == null || storefile == "") {
110                 if (pemfile == null || pemfile == "") {
111                     // use PEM string
112
InputStream JavaDoc pemstream = new ByteArrayInputStream JavaDoc(pemstring.getBytes());
113                     cert = CertTools.getCertfromPEM(pemstream);
114                 } else {
115                     // use PEM store
116
cert = CertTools.getCertfromPEM(pemfile);
117                 }
118             } else {
119                 // use PKCS #12 keystore
120
cert = X509Cert.getCertificateFromP12(storefile, storeentry, storepassword);
121             }
122
123             if (subject == null || subject == "") {
124                 // print values
125
try {
126                     JspWriter JavaDoc w = pageContext.getOut();
127                     w.print("For: " + cert.getSubjectDN() + "<BR>");
128                     w.print("Issued by: " + cert.getIssuerDN() + "<BR>");
129                     w.print("Valid from " + cert.getNotBefore() + " to " + cert.getNotAfter() + "<BR>");
130                     w.print("Certificate SN#: " + cert.getSerialNumber() + "<BR>");
131                     w.print("Generated with: " + cert.getSigAlgName() + "<BR>");
132                     w.print("Fingerprint: " + CertTools.getFingerprintAsString(cert) + "<BR>");
133                 } catch (IOException JavaDoc ex) {
134                     throw new JspException JavaDoc(ex.getMessage(), ex);
135                 }
136             } else {
137                 // return values
138
pageContext.setAttribute(subject, cert.getSubjectDN(), scope);
139                 pageContext.setAttribute(issuer, cert.getIssuerDN(), scope);
140                 pageContext.setAttribute(validfrom, cert.getNotBefore(), scope);
141                 pageContext.setAttribute(validto, cert.getNotAfter(), scope);
142                 pageContext.setAttribute(algorithm, cert.getSigAlgName(), scope);
143                 pageContext.setAttribute(serialnumber, cert.getSerialNumber(), scope);
144                 pageContext.setAttribute(fingerprint, CertTools.getFingerprintAsString(cert), scope);
145             }
146         } catch (Exception JavaDoc e) {
147             throw new JspException JavaDoc("JCE Exception: Could not retrieve certificate info: " + e.getMessage(), e);
148         }
149
150         return EVAL_PAGE;
151     } // doEndTag()
152

153     public void release() {
154         // Cleanup all sensitive information
155
Clean.blank(storepassword);
156
157         super.release();
158     } //release()
159

160     /**
161      * @jsp.attribute
162      * description="Scope of the return variables. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
163      * type="java.lang.String"
164      * required="false"
165      * rtexprvalue="false"
166      */

167     public void setScope(String JavaDoc scope) {
168         this.scope = getScope(scope);
169     }
170
171     /**
172      * @jsp.attribute
173      * description="The PKCS#12 (P12) keystore where the certificate is stored"
174      * type="java.lang.String"
175      * required="false"
176      * rtexprvalue="true"
177      */

178     public void setStorefile(String JavaDoc storefile) {
179         this.storefile = storefile;
180     }
181
182     /**
183      * @jsp.attribute
184      * description=" The PKCS#12 (P12) keystore entry name for this certificate"
185      * type="java.lang.String"
186      * required="false"
187      * rtexprvalue="true"
188      */

189     public void setStoreentry(String JavaDoc storeentry) {
190         this.storeentry = storeentry;
191     }
192
193     /**
194      * @jsp.attribute
195      * description="The PKCS#12 (P12) keystore password"
196      * type="java.lang.StringBuffer"
197      * required="false"
198      * rtexprvalue="true"
199      */

200     public void setStorepassword(StringBuffer JavaDoc storepassword) {
201         this.storepassword = storepassword;
202     }
203
204     /**
205      * @jsp.attribute
206      * description="The certificate as a PEM formatted file"
207      * type="java.lang.String"
208      * required="false"
209      * rtexprvalue="true"
210      */

211     public void setPemfile(String JavaDoc pemfile) {
212         this.pemfile = pemfile;
213     }
214
215     /**
216      * @jsp.attribute
217      * description="The certificate as a PEM formatted string"
218      * type="java.lang.String"
219      * required="false"
220      * rtexprvalue="true"
221      */

222     public void setPemstring(String JavaDoc pemstring) {
223         this.pemstring = pemstring;
224     }
225
226     /**
227      * @jsp.attribute
228      * description="Return variable for storing the certificate's subject"
229      * type="java.lang.String"
230      * required="false"
231      * rtexprvalue="true"
232      */

233     public void setSubject(String JavaDoc subject) {
234         this.subject = subject;
235     }
236
237     public String JavaDoc getSubject() {
238         return subject;
239     }
240
241     /**
242      * @jsp.attribute
243      * description="Return variable for storing the certificate's issuer"
244      * type="java.lang.String"
245      * required="false"
246      * rtexprvalue="true"
247      */

248     public void setIssuer(String JavaDoc issuer) {
249         this.issuer = issuer;
250     }
251
252     public String JavaDoc getIssuer() {
253         return issuer;
254     }
255
256     /**
257      * @jsp.attribute
258      * description="Return variable for storing the certificate's valid from field"
259      * type="java.lang.String"
260      * required="false"
261      * rtexprvalue="true"
262      */

263     public void setValidfrom(String JavaDoc validfrom) {
264         this.validfrom = validfrom;
265     }
266
267     public String JavaDoc getValidfrom() {
268         return validfrom;
269     }
270
271     /**
272      * @jsp.attribute
273      * description="Return variable for storing the certificate's valid to field"
274      * type="java.lang.String"
275      * required="false"
276      * rtexprvalue="true"
277      */

278     public void setValidto(String JavaDoc validto) {
279         this.validto = validto;
280     }
281
282     public String JavaDoc getValidto() {
283         return validto;
284     }
285
286     /**
287      * @jsp.attribute
288      * description="Return variable for storing the certificate's algorithm"
289      * type="java.lang.String"
290      * required="false"
291      * rtexprvalue="true"
292      */

293     public void setAlgorithm(String JavaDoc algorithm) {
294         this.algorithm = algorithm;
295     }
296
297     public String JavaDoc getAlgorithm() {
298         return algorithm;
299     }
300
301     /**
302      * @jsp.attribute
303      * description="Return variable for storing the certificate's serialnumber"
304      * type="java.lang.String"
305      * required="false"
306      * rtexprvalue="true"
307      */

308     public void setSerialnumber(String JavaDoc serialnumber) {
309         this.serialnumber = serialnumber;
310     }
311
312     public String JavaDoc getSerialnumber() {
313         return serialnumber;
314     }
315
316     /**
317      * @jsp.attribute
318      * description="Return variable for storing the certificate's fingerprint"
319      * type="java.lang.String"
320      * required="false"
321      * rtexprvalue="true"
322      */

323     public void setFingerprint(String JavaDoc fingerprint) {
324         this.fingerprint = fingerprint;
325     }
326
327     public String JavaDoc getFingerprint() {
328         return fingerprint;
329     }
330 }
Popular Tags