KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > taglib > crypto > CreateSignature


1 /*
2   Name: CreateSignature.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.crypto;
21
22 import net.sourceforge.jcetaglib.lib.CertTools;
23 import net.sourceforge.jcetaglib.lib.Clean;
24 import net.sourceforge.jcetaglib.lib.Signatures;
25 import net.sourceforge.jcetaglib.lib.X509Cert;
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.BodyTagSupport JavaDoc;
31 import java.io.ByteArrayInputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.security.PrivateKey JavaDoc;
35
36 /**
37  * JSP tag used for creating SIG (signatures) of strings and files
38  *
39  * @jsp.tag
40  * name="createsignature"
41  * display-name="CreateSignature"
42  * body-content="JSP"
43  * example="
44  * <%-- Create and print a signature from the tag body --%>

45  * <jce:createsignature

46  * 	signame=\"MD5WithRSA/ISO9796-2\"

47  * 	pemfile=\"c:/pemfolder/myprivatekey.pem\">

48  * Please generate a SIG

49  * </jce:createsignature>"
50  *
51  * description="JSP tag used for creating SIG (signatures) of strings and files"
52  *
53  * @author Gert Van Ham
54  * @author hamgert@users.sourceforge.net
55  * @author http://jcetaglib.sourceforge.net
56  * @version $Id: CreateSignature.java,v 1.5 2004/04/15 07:28:35 hamgert Exp $
57  */

58 public class CreateSignature extends BodyTagSupport JavaDoc {
59
60     private static final String JavaDoc PAGE = "page";
61     private static final String JavaDoc REQUEST = "request";
62     private static final String JavaDoc SESSION = "session";
63     private static final String JavaDoc APPLICATION = "application";
64
65     private StringBuffer JavaDoc value; // tag attribute
66
private String JavaDoc var; // tag attribute
67
private int scope = PageContext.PAGE_SCOPE; // tag attribute
68

69     private String JavaDoc signame = "RIPEMD160WithRSA/ISO9796-2"; // tag attribute
70
private String JavaDoc file; // tag attribute
71

72     /* Attributes for X.509 keystore */
73
74     // P12 keystore...
75
private String JavaDoc storefile; // tag attribute
76
private String JavaDoc storeentry; // tag attribute
77
private StringBuffer JavaDoc storepassword; // tag attribute
78

79     // ... OR PEM string
80
private String JavaDoc pemstring; // tag attribute
81

82     // ... OR PEM file
83
private String JavaDoc pemfile; // tag attribute
84

85     private StringBuffer JavaDoc input; // what we'll store in scope:var
86
private StringBuffer JavaDoc output; // return text
87

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

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

103     public int doEndTag() throws JspException JavaDoc {
104         // determine the value by...
105
if (value != null) {
106             // ... reading our attribute
107
input = value;
108         } else {
109             // ... retrieving and trimming our body
110
if (bodyContent == null || bodyContent.getString() == null) {
111                 input = new StringBuffer JavaDoc("");
112             } else {
113                 input = new StringBuffer JavaDoc(bodyContent.getString().trim());
114             }
115         }
116
117         PrivateKey JavaDoc signingKey;
118
119         try {
120             // Retrieve the private key (for signing) from one of the three possible keystores
121
if (storefile == null || storefile == "") {
122                 if (pemfile == null || pemfile == "") {
123                     // use PEM string
124
InputStream JavaDoc pemstream = new ByteArrayInputStream JavaDoc(pemstring.getBytes());
125                     signingKey = CertTools.getPrivatefromPEM(pemstream, "");
126                 } else {
127                     // use PEM store
128
signingKey = CertTools.getPrivatefromPEM(pemfile, "");
129                 }
130             } else {
131                 // use PKCS #12 keystore
132
signingKey = X509Cert.getPrivateFromP12(storefile, storeentry, storepassword);
133             }
134         } catch (Exception JavaDoc e) {
135             throw new JspException JavaDoc("JCE Exception - keystore could not be loaded: " + e.getMessage(), e);
136         }
137
138         try {
139             if (file != null) {
140                 output = Signatures.generateFileSIG(file, signingKey, signame);
141             } else {
142                 output = Signatures.generateSIG(input, signingKey, signame);
143             }
144         } catch (Exception JavaDoc e) {
145             throw new JspException JavaDoc("JCE Exception: " + e.getMessage(), e);
146         }
147
148         // decide what to do with the result
149
if (var != null) {
150             if (output != null) {
151                 pageContext.setAttribute(var, output, scope);
152             }
153         } else {
154             if (bodyContent != null) {
155                 bodyContent.clearBody();
156             }
157
158             try {
159                 JspWriter JavaDoc w = pageContext.getOut();
160                 w.print(output);
161             } catch (IOException JavaDoc ex) {
162                 throw new JspException JavaDoc(ex.getMessage(), ex);
163             }
164         }
165
166         return EVAL_PAGE;
167     } // doEndTag()
168

169     public void release() {
170         // Cleanup all sensitive information
171
Clean.blank(value);
172         Clean.blank(storepassword);
173         Clean.blank(input);
174         Clean.blank(output);
175
176         super.release();
177     } //release()
178

179     /**
180      * @jsp.attribute
181      * description="Optional attribute, the string to generate the SIG from. The body of the tag will be taken if omitted"
182      * type="java.lang.StringBuffer"
183      * required="false"
184      * rtexprvalue="true"
185      */

186     public void setValue(StringBuffer JavaDoc value) {
187         this.value = value;
188     }
189
190     public StringBuffer JavaDoc getValue() {
191         return value;
192     }
193
194     /**
195      * @jsp.attribute
196      * description="Optional attribute, variable to store the SIG. The SIG will be printed if omitted"
197      * type="java.lang.String"
198      * required="false"
199      * rtexprvalue="false"
200      */

201     public void setVar(String JavaDoc var) {
202         this.var = var;
203     }
204
205     public String JavaDoc getVar() {
206         return var;
207     }
208
209     /**
210      * @jsp.attribute
211      * description="Scope of the 'var' attribute. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
212      * type="java.lang.String"
213      * required="false"
214      * rtexprvalue="false"
215      */

216     public void setScope(String JavaDoc scope) {
217         this.scope = getScope(scope);
218     }
219
220     /**
221      * @jsp.attribute
222      * description="The signature algorithm. Default is 'RIPEMD160WithRSA/ISO9796-2'"
223      * type="java.lang.String"
224      * required="false"
225      * rtexprvalue="true"
226      */

227     public void setSigname(String JavaDoc signame) {
228         this.signame = signame;
229     }
230
231     /**
232      * @jsp.attribute
233      * description="Create a SIG from a file instead of a string"
234      * type="java.lang.String"
235      * required="false"
236      * rtexprvalue="true"
237      */

238     public void setFile(String JavaDoc file) {
239         this.file = file;
240     }
241
242     /**
243      * @jsp.attribute
244      * description="The PKCS#12 (P12) keystore where the private key is stored."
245      * type="java.lang.String"
246      * required="false"
247      * rtexprvalue="true"
248      */

249     public void setStorefile(String JavaDoc storefile) {
250         this.storefile = storefile;
251     }
252
253     /**
254      * @jsp.attribute
255      * description="The PKCS#12 (P12) keystore entry name for private key"
256      * type="java.lang.String"
257      * required="false"
258      * rtexprvalue="true"
259      */

260     public void setStoreentry(String JavaDoc storeentry) {
261         this.storeentry = storeentry;
262     }
263
264     /**
265      * @jsp.attribute
266      * description="The PKCS#12 (P12) keystore password "
267      * type="java.lang.StringBuffer"
268      * required="false"
269      * rtexprvalue="true"
270      */

271     public void setStorepassword(StringBuffer JavaDoc storepassword) {
272         this.storepassword = storepassword;
273     }
274
275     /**
276      * @jsp.attribute
277      * description="The sender's private key stored in a PEM file"
278      * type="java.lang.String"
279      * required="false"
280      * rtexprvalue="true"
281      */

282     public void setPemfile(String JavaDoc pemfile) {
283         this.pemfile = pemfile;
284     }
285
286     /**
287      * @jsp.attribute
288      * description="The sender's private key as a PEM formatted string "
289      * type="java.lang.String"
290      * required="false"
291      * rtexprvalue="true"
292      */

293     public void setPemstring(String JavaDoc pemstring) {
294         this.pemstring = pemstring;
295     }
296
297 }
Popular Tags