KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: CreatePKCS10.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 org.bouncycastle.jce.PKCS10CertificationRequest;
24 import org.bouncycastle.jce.X509Principal;
25 import org.bouncycastle.jce.provider.BouncyCastleProvider;
26 import org.bouncycastle.util.encoders.Base64;
27
28 import javax.servlet.jsp.JspException JavaDoc;
29 import javax.servlet.jsp.PageContext JavaDoc;
30 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
31 import java.security.*;
32
33 /**
34  * JSP tag for creating a PKCS#10 certificate request
35  *
36  * @jsp.tag
37  * name="createpkcs10"
38  * display-name="CreatePKCS10"
39  * body-content="empty"
40  * example="<jce:createpkcs10

41  * 	subjectdn=\"C=BE, O=NET, OU=Home, CN=Bob, EmailAddress=bob@somewhere.org\"

42  * 	request=\"req\"

43  * 	privatekey=\"privkey\"/>"
44  *
45  * description="JSP tag for creating a PKCS#10 certificate request"
46  *
47  * @author Gert Van Ham
48  * @author hamgert@users.sourceforge.net
49  * @author http://jcetaglib.sourceforge.net
50  * @version $Id: CreatePKCS10.java,v 1.6 2004/04/15 07:28:36 hamgert Exp $
51  */

52
53 public class CreatePKCS10 extends TagSupport 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     private String JavaDoc subjectdn;
60
61     private String JavaDoc keypairalgorithm = "RSA"; // tag attribute
62
private int keylength = 1024; // tag attribute
63
private String JavaDoc signaturealgorithm = "MD5WithRSAEncryption"; // tag attribute
64

65     private int scope = PageContext.PAGE_SCOPE;
66
67     // return certificates...
68
// variables
69
private String JavaDoc request;
70     private String JavaDoc privatekey;
71
72     private StringBuffer JavaDoc pkey;
73
74     public static int getScope(String JavaDoc scope) {
75         int ret = PageContext.PAGE_SCOPE; // default
76

77         if (REQUEST.equalsIgnoreCase(scope))
78             ret = PageContext.REQUEST_SCOPE;
79         else if (SESSION.equalsIgnoreCase(scope))
80             ret = PageContext.SESSION_SCOPE;
81         else if (APPLICATION.equalsIgnoreCase(scope))
82             ret = PageContext.APPLICATION_SCOPE;
83         else if (PAGE.equalsIgnoreCase(scope))
84             ret = PageContext.PAGE_SCOPE;
85
86         return ret;
87     } //getScope()
88

89     public int doEndTag() throws JspException JavaDoc {
90         Security.addProvider(new BouncyCastleProvider());
91
92         //
93
// set up the keys
94
//
95
PrivateKey privKey;
96         PublicKey pubKey;
97
98         try {
99             KeyPairGenerator g = KeyPairGenerator.getInstance(keypairalgorithm, "BC");
100
101             g.initialize(keylength, new SecureRandom());
102             KeyPair p = g.generateKeyPair();
103
104             privKey = p.getPrivate();
105             pubKey = p.getPublic();
106
107             PKCS10CertificationRequest req = new PKCS10CertificationRequest(signaturealgorithm,
108                     new X509Principal(subjectdn),
109                     pubKey,
110                     null,
111                     privKey);
112
113             // Return certificate as string
114
byte output[] = req.getEncoded();
115             byte reqB64[] = Base64.encode(output);
116
117             pageContext.setAttribute(request, "-----BEGIN CERTIFICATE REQUEST-----\n" + new String JavaDoc(reqB64) + "\n-----END CERTIFICATE REQUEST-----", scope);
118
119             // Return private key as string
120
byte keyoutput[] = privKey.getEncoded();
121             byte keyB64[] = Base64.encode(keyoutput);
122
123             pkey = new StringBuffer JavaDoc("-----BEGIN PRIVATE KEY-----\n");
124             pkey.append(new String JavaDoc(keyB64));
125             pkey.append("\n-----END PRIVATE KEY-----");
126
127             pageContext.setAttribute(privatekey, pkey, scope);
128
129             Clean.blank(keyoutput);
130             Clean.blank(keyB64);
131             privKey = null;
132
133         } catch (Exception JavaDoc e) {
134             throw new JspException JavaDoc("JCE Exception: Unable to generate PKCS#10 request: "
135                     + e.getMessage(), e);
136         }
137
138         return EVAL_PAGE;
139     } // doEndTag()
140

141     public void release() {
142         // Cleanup all sensitive information
143
Clean.blank(pkey);
144
145         super.release();
146     } //release()
147

148     /**
149      * @jsp.attribute
150      * description="Keypair algorithm. 'RSA', 'DSA' or 'EC-DSA'. Default is 'RSA'"
151      * type="java.lang.String"
152      * required="false"
153      * rtexprvalue="true"
154      */

155     public void setKeypairalgorithm(String JavaDoc keypairalgorithm) {
156         this.keypairalgorithm = keypairalgorithm;
157     }
158
159     /**
160      * @jsp.attribute
161      * description="Key length. 512, 1024 or 2048. Default is 1024. Please note that not all products support key lengths greater than 1024"
162      * type="java.lang.Integer"
163      * required="false"
164      * rtexprvalue="true"
165      */

166     public void setKeylength(int keylength) {
167         this.keylength = keylength;
168     }
169
170     /**
171      * @jsp.attribute
172      * description="Signature algorithm. Default is 'MD5WithRSAEncryption'"
173      * type="java.lang.String"
174      * required="false"
175      * rtexprvalue="true"
176      */

177     public void setSignaturealgorithm(String JavaDoc signaturealgorithm) {
178         this.signaturealgorithm = signaturealgorithm;
179     }
180
181     /**
182      * @jsp.attribute
183      * description="The subject DN string. e.g. 'C=BE, O=NET, OU=Sourceforge, CN=CertAuthority, E=info@certauthority.org' Important: if you want to use the certificate for SSL server purposes you must specify the full server & domain name as the 'CN' entry (e.g. '... CN=www.oracle.com ...')"
184      * type="java.lang.String"
185      * required="true"
186      * rtexprvalue="true"
187      */

188     public void setSubjectdn(String JavaDoc subjectdn) {
189         this.subjectdn = subjectdn;
190     }
191
192     /**
193      * @jsp.attribute
194      * description="Scope of the return variables. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
195      * type="java.lang.String"
196      * required="false"
197      * rtexprvalue="false"
198      */

199     public void setScope(String JavaDoc scope) {
200         this.scope = getScope(scope);
201     }
202
203     /**
204      * @jsp.attribute
205      * description="Return variable. Contains the generated certificate as a PEM formatted string"
206      * type="java.lang.String"
207      * required="true"
208      * rtexprvalue="false"
209      */

210     public void setRequest(String JavaDoc request) {
211         this.request = request;
212     }
213
214     public String JavaDoc getRequest() {
215         return request;
216     }
217
218     /**
219      * @jsp.attribute
220      * description="Return variable. Contains the generated private key as a PEM formatted StringBuffer"
221      * type="java.lang.String"
222      * required="true"
223      * rtexprvalue="false"
224      */

225     public void setPrivatekey(String JavaDoc privatekey) {
226         this.privatekey = privatekey;
227     }
228
229     public String JavaDoc getPrivatekey() {
230         return privatekey;
231     }
232 }
Popular Tags