KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: CertificateRequestInfo.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.X509Cert;
23 import org.bouncycastle.asn1.pkcs.CertificationRequestInfo;
24 import org.bouncycastle.asn1.x509.X509Name;
25 import org.bouncycastle.jce.PKCS10CertificationRequest;
26 import org.bouncycastle.jce.provider.BouncyCastleProvider;
27
28 import javax.servlet.jsp.JspException JavaDoc;
29 import javax.servlet.jsp.PageContext JavaDoc;
30 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
31 import java.security.Security JavaDoc;
32
33 /**
34  * JSP tag for retrieving info from PKCS#10 requests
35  *
36  * @jsp.tag
37  * name="certificaterequestinfo"
38  * display-name="CertificateRequestInfo"
39  * body-content="JSP"
40  * example="<%-- Get subject DN from request--%>

41  * <jce:certificaterequestinfo

42  * 	certrequest=\"<%= request.getParameter(\"pkcs10req\") %>\"

43  * 	subjectdn=\"sdn\"/>"
44  *
45  * description="JSP tag for retrieving info from PKCS#10 requests"
46  *
47  * @author Gert Van Ham
48  * @author hamgert@users.sourceforge.net
49  * @author http://jcetaglib.sourceforge.net
50  * @version $Id: CertificateRequestInfo.java,v 1.7 2004/04/15 07:28:36 hamgert Exp $
51  */

52
53 public class CertificateRequestInfo extends BodyTagSupport JavaDoc {
54     private static final String JavaDoc PAGE = "page";
55     private static final String JavaDoc REQUEST = "request";
56     private static final String JavaDoc SESSION = "session";
57     private static final String JavaDoc APPLICATION = "application";
58
59     protected String JavaDoc certrequest; // tag attribute
60

61     private int scope = PageContext.PAGE_SCOPE;
62
63     private String JavaDoc subjectdn;
64
65     public static int getScope(String JavaDoc scope) {
66         int ret = PageContext.PAGE_SCOPE; // default
67

68         if (REQUEST.equalsIgnoreCase(scope))
69             ret = PageContext.REQUEST_SCOPE;
70         else if (SESSION.equalsIgnoreCase(scope))
71             ret = PageContext.SESSION_SCOPE;
72         else if (APPLICATION.equalsIgnoreCase(scope))
73             ret = PageContext.APPLICATION_SCOPE;
74         else if (PAGE.equalsIgnoreCase(scope))
75             ret = PageContext.PAGE_SCOPE;
76
77         return ret;
78     } //getScope()
79

80     public int doEndTag() throws JspException JavaDoc {
81         String JavaDoc input;
82
83         // determine the value by...
84
if (certrequest != null) {
85             // ... reading our attribute
86
input = certrequest;
87         } else {
88             // ... retrieving and trimming our body
89
if (bodyContent == null || bodyContent.getString() == null) {
90                 input = "";
91             } else {
92                 input = bodyContent.getString().trim();
93             }
94         }
95
96         Security.addProvider(new BouncyCastleProvider());
97
98         try {
99             // PKCS10 Request type
100
PKCS10CertificationRequest pkcs10 = X509Cert.getPKCS10Request(input);
101
102             if (pkcs10.verify() == false) {
103                 throw new JspException JavaDoc("JCE Exception: Unable to retrieve info from PKCS#10 request: Not a valid PKCS10 request");
104             }
105
106             CertificationRequestInfo reqinfo = pkcs10.getCertificationRequestInfo();
107             X509Name x = reqinfo.getSubject();
108
109             pageContext.setAttribute(subjectdn, x.toString(), scope);
110
111         } catch (Exception JavaDoc e) {
112             throw new JspException JavaDoc("JCE Exception: Unable to retrieve info from PKCS#10 request: "
113                     + e.getMessage(), e);
114         }
115
116         return EVAL_PAGE;
117     } //doEndTag()
118

119     /**
120      * @jsp.attribute
121      * description="Optional attribute, the certificate request. The body of the tag will be taken if omitted"
122      * type="java.lang.String"
123      * required="false"
124      * rtexprvalue="true"
125      */

126     public void setCertrequest(String JavaDoc certrequest) {
127         this.certrequest = certrequest;
128     }
129
130     /**
131      * @jsp.attribute
132      * description="Scope of the return variables. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
133      * type="java.lang.String"
134      * required="false"
135      * rtexprvalue="false"
136      */

137     public void setScope(String JavaDoc scope) {
138         this.scope = getScope(scope);
139     }
140
141     /**
142      * @jsp.attribute
143      * description="Return variable. Contains the subject DN from the PKCS#10 request"
144      * type="java.lang.String"
145      * required="true"
146      * rtexprvalue="false"
147      */

148     public void setSubjectdn(String JavaDoc subjectdn) {
149         this.subjectdn = subjectdn;
150     }
151
152     public String JavaDoc getSubjectdn() {
153         return subjectdn;
154     }
155 }
Popular Tags