KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: DecryptAndVerifySignature.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.Clean;
23 import net.sourceforge.jcetaglib.lib.Hybrid;
24 import net.sourceforge.jcetaglib.lib.X509Cert;
25 import net.sourceforge.jcetaglib.tools.SignerCertificate;
26 import org.bouncycastle.util.encoders.Base64;
27
28 import javax.servlet.jsp.JspException JavaDoc;
29 import javax.servlet.jsp.JspWriter JavaDoc;
30 import javax.servlet.jsp.PageContext JavaDoc;
31 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.security.PrivateKey JavaDoc;
34 import java.security.cert.Certificate JavaDoc;
35
36 /**
37  * JSP tag used for decrypting & verify signed data
38  *
39  * @jsp.tag
40  * name="decryptandverifysignature"
41  * display-name="DecryptAndVerifySignature"
42  * body-content="JSP"
43  * example="
44  * <jce:decryptandverifysignature

45  * 	var=\"decrypted\"

46  * 	scope=\"page\"

47  * 	storefile=\"C:/keystores/alice.p12\"

48  * 	storeentry=\"user\"

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

50  * 	validsignature=\"status\"

51  * 	certificate=\"cert\"/>"
52  *
53  * description="JSP tag used for decrypting & verify signed data"
54  *
55  * @author Gert Van Ham
56  * @author hamgert@users.sourceforge.net
57  * @author http://jcetaglib.sourceforge.net
58  * @version $Id: DecryptAndVerifySignature.java,v 1.5 2004/04/15 07:28:35 hamgert Exp $
59  */

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

70     private String JavaDoc file; // tag attribute
71
private String JavaDoc newfile; // tag attribute
72

73     private String JavaDoc signame = "MD5withRSA"; // tag attribute
74
private String JavaDoc algorithm = "AES"; // tag attribute
75
private String JavaDoc mode = "CBC"; // tag attribute
76
private String JavaDoc padding = "PKCS7Padding"; // tag attribute
77

78     // P12 keystore...
79
private String JavaDoc storefile; // tag attribute
80
private String JavaDoc storeentry; // tag attribute
81
private StringBuffer JavaDoc storepassword; // tag attribute
82

83     // return values
84
private String JavaDoc validsignature;
85     private String JavaDoc certificate;
86
87     private StringBuffer JavaDoc input; // what we'll store in scope:var
88
private StringBuffer JavaDoc output; // return text
89

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

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

105     public int doEndTag() throws JspException JavaDoc {
106         PrivateKey JavaDoc privKey = null;
107
108         // determine the value by...
109
if (value != null) {
110             // ... reading our attribute
111
input = value;
112         } else {
113             // ... retrieving and trimming our body
114
if (bodyContent == null || bodyContent.getString() == null) {
115                 input = new StringBuffer JavaDoc("");
116             } else {
117                 input = new StringBuffer JavaDoc(bodyContent.getString().trim());
118             }
119         }
120
121         // retrieve the receiver's private key for decryption
122
try {
123             privKey = X509Cert.getPrivateFromP12(storefile, storeentry, storepassword);
124         } catch (Exception JavaDoc e) {
125             throw new JspException JavaDoc("JCE Exception - keystore could not be loaded: " + e.getMessage(), e);
126         }
127
128         SignerCertificate info = new SignerCertificate();
129
130         // decrypt
131
try {
132             if (file != null) {
133                 Hybrid.decryptFileAndVerify(file
134                         , newfile
135                         , privKey
136                         , info
137                         , signame
138                         , algorithm
139                         , mode
140                         , padding);
141             } else {
142                 output = Hybrid.decryptAndVerify(input
143                         , privKey
144                         , info
145                         , signame
146                         , algorithm
147                         , mode
148                         , padding);
149             }
150
151             Certificate cert = info.getCert();
152
153             byte certoutput[] = cert.getEncoded();
154             byte certB64[] = Base64.encode(certoutput);
155
156             // return values
157
pageContext.setAttribute(certificate, "-----BEGIN CERTIFICATE-----\n" + new String JavaDoc(certB64) + "\n-----END CERTIFICATE-----", scope);
158             //pageContext.setAttribute(validsignature, info.getStatus(), scope);
159

160         } catch (Exception JavaDoc e) {
161             throw new JspException JavaDoc("JCE Exception: " + e.getMessage(), e);
162         }
163
164         // decide what to do with the result
165
if (var != null) {
166             if (output != null) {
167                 pageContext.setAttribute(var, output, scope);
168             }
169         } else {
170             if (file == null || file == "") {
171                 if (bodyContent != null) {
172                     bodyContent.clearBody();
173                 }
174
175                 try {
176                     JspWriter JavaDoc w = pageContext.getOut();
177                     w.print(output);
178                 } catch (IOException JavaDoc ex) {
179                     throw new JspException JavaDoc(ex.getMessage(), ex);
180                 }
181             }
182         }
183
184         privKey = null;
185
186         return EVAL_PAGE;
187     }
188
189     public void release() {
190         // Cleanup all sensitive information
191
Clean.blank(value);
192         Clean.blank(storepassword);
193         Clean.blank(input);
194         Clean.blank(output);
195
196         super.release();
197     } //release()
198

199     /**
200      * @jsp.attribute
201      * description="Optional attribute, the string to decrypt. The body of the tag will be taken if omitted"
202      * type="java.lang.StringBuffer"
203      * required="false"
204      * rtexprvalue="true"
205      */

206     public void setValue(StringBuffer JavaDoc value) {
207         this.value = value;
208     }
209
210     public StringBuffer JavaDoc getValue() {
211         return value;
212     }
213
214     /**
215      * @jsp.attribute
216      * description="Optional attribute, variable to store the decrypted string. The string will be printed if omitted"
217      * type="java.lang.String"
218      * required="false"
219      * rtexprvalue="false"
220      */

221     public void setVar(String JavaDoc var) {
222         this.var = var;
223     }
224
225     public String JavaDoc getVar() {
226         return var;
227     }
228
229     /**
230      * @jsp.attribute
231      * description="Scope of the 'var' attribute. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
232      * type="java.lang.String"
233      * required="false"
234      * rtexprvalue="false"
235      */

236     public void setScope(String JavaDoc scope) {
237         this.scope = getScope(scope);
238     }
239
240     /**
241      * @jsp.attribute
242      * description="The signature algorithm. Default is 'MD5WithRSA'"
243      * type="java.lang.String"
244      * required="false"
245      * rtexprvalue="true"
246      */

247     public void setSigname(String JavaDoc signame) {
248         this.signame = signame;
249     }
250
251     /**
252      * @jsp.attribute
253      * description="The key algorithm. Default is 'AES'"
254      * type="java.lang.String"
255      * required="false"
256      * rtexprvalue="true"
257      */

258     public void setAlgorithm(String JavaDoc algorithm) {
259         this.algorithm = algorithm;
260     }
261
262     /**
263      * @jsp.attribute
264      * description="Encryption mode. Default is 'CBC'"
265      * type="java.lang.String"
266      * required="false"
267      * rtexprvalue="true"
268      */

269     public void setMode(String JavaDoc mode) {
270         this.mode = mode;
271     }
272
273     /**
274      * @jsp.attribute
275      * description="Padding scheme. Default is 'PKCS7Padding'"
276      * type="java.lang.String"
277      * required="false"
278      * rtexprvalue="true"
279      */

280     public void setPadding(String JavaDoc padding) {
281         this.padding = padding;
282     }
283
284     /**
285      * @jsp.attribute
286      * description="The PKCS#12 (P12) keystore where the private key is stored"
287      * type="java.lang.String"
288      * required="true"
289      * rtexprvalue="true"
290      */

291     public void setStorefile(String JavaDoc storefile) {
292         this.storefile = storefile;
293     }
294
295     /**
296      * @jsp.attribute
297      * description="The PKCS#12 (P12) keystore entry name for private key"
298      * type="java.lang.String"
299      * required="true"
300      * rtexprvalue="true"
301      */

302     public void setStoreentry(String JavaDoc storeentry) {
303         this.storeentry = storeentry;
304     }
305
306     /**
307      * @jsp.attribute
308      * description="The PKCS#12 (P12) keystore password"
309      * type="java.lang.StringBuffer"
310      * required="true"
311      * rtexprvalue="true"
312      */

313     public void setStorepassword(StringBuffer JavaDoc storepassword) {
314         this.storepassword = storepassword;
315     }
316
317     /**
318      * @jsp.attribute
319      * description="returns 'TRUE' if the signature is valid, otherwise 'FALSE'"
320      * type="java.lang.String"
321      * required="true"
322      * rtexprvalue="true"
323      */

324     public String JavaDoc getValidsignature() {
325         return validsignature;
326     }
327
328     public void setValidsignature(String JavaDoc validsignature) {
329         this.validsignature = validsignature;
330     }
331
332     /**
333      * @jsp.attribute
334      * description="Returns the sender's certificate as a PEM formatted string"
335      * type="java.lang.String"
336      * required="true"
337      * rtexprvalue="true"
338      */

339     public void setCertificate(String JavaDoc certificate) {
340         this.certificate = certificate;
341     }
342
343     public String JavaDoc getCertificate() {
344         return certificate;
345     }
346
347     /**
348      * @jsp.attribute
349      * description="Decrypts a file instead of a string"
350      * type="java.lang.String"
351      * required="false"
352      * rtexprvalue="true"
353      */

354     public void setFile(String JavaDoc file) {
355         this.file = file;
356     }
357
358     /**
359      * @jsp.attribute
360      * description="File(name) to store the decrypted data "
361      * type="java.lang.String"
362      * required="false"
363      * rtexprvalue="true"
364      */

365     public void setNewfile(String JavaDoc newfile) {
366         this.newfile = newfile;
367     }
368 }
Popular Tags