KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: EncryptWithHMAC.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.Hybrid;
25
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.JspWriter JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.security.cert.X509Certificate JavaDoc;
34
35 /**
36  * JSP tag used for encrypting data with a HMAC code
37  *
38  * @jsp.tag
39  * name="encryptwithhmac"
40  * display-name="EncryptWithHMAC"
41  * body-content="JSP"
42  * example="
43  * <jce:encryptwithhmac

44  * 	var=\"foralice\"

45  * 	value=\"<%= new StringBuffer(\"Encrypt this string\") %>\"

46  * 	scope=\"page\"

47  * 	recpemfile=\"C:/keystores/alice.cert\"/>"
48  *
49  * description="JSP tag used for encrypting data with a HMAC code"
50  *
51  * @author Gert Van Ham
52  * @author hamgert@users.sourceforge.net
53  * @author http://jcetaglib.sourceforge.net
54  * @version $Id: EncryptWithHMAC.java,v 1.5 2004/04/15 07:28:35 hamgert Exp $
55  */

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

66     private String JavaDoc file; // tag attribute
67
private String JavaDoc newfile; // tag attribute
68

69     private String JavaDoc algorithm = "AES"; // tag attribute
70
private String JavaDoc seed; // tag attribute
71
private int strength = 256; // tag attribute
72
private String JavaDoc mode = "CBC"; // tag attribute
73
private String JavaDoc padding = "PKCS7Padding"; // tag attribute
74

75     /* Attributes for receiver's certificate */
76
77     // receiver's certificate as string
78
private String JavaDoc recpemstring; // tag attribute
79

80     // ... OR as PEM file
81
private String JavaDoc recpemfile; // tag attribute
82

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

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

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

101     public int doEndTag() throws JspException JavaDoc {
102         X509Certificate JavaDoc reccert = null;
103
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         // loading the receiver's public key
118
try {
119             if (recpemfile == null || recpemfile == "") {
120                 // use PEM string
121
InputStream JavaDoc pemstream = new ByteArrayInputStream JavaDoc(recpemstring.getBytes());
122                 reccert = CertTools.getCertfromPEM(pemstream);
123             } else {
124                 // use PEM store
125
reccert = CertTools.getCertfromPEM(recpemfile);
126             }
127         } catch (Exception JavaDoc e) {
128             throw new JspException JavaDoc("JCE Exception - PEM could not be loaded: " + e.getMessage(), e);
129         }
130
131         // encrypt with MAC
132
try {
133             if (file != null) {
134                 if (seed == null) {
135                     Hybrid.encryptFileWithHMAC(file
136                             , newfile
137                             , reccert.getPublicKey()
138                             , algorithm
139                             , null
140                             , strength
141                             , mode
142                             , padding);
143                 } else {
144                     Hybrid.encryptFileWithHMAC(file
145                             , newfile
146                             , reccert.getPublicKey()
147                             , algorithm
148                             , seed.getBytes()
149                             , strength
150                             , mode
151                             , padding);
152                 }
153             } else {
154                 if (seed == null) {
155                     output = Hybrid.encryptWithHMAC(input
156                             , reccert.getPublicKey()
157                             , algorithm
158                             , null
159                             , strength
160                             , mode
161                             , padding);
162                 } else {
163                     output = Hybrid.encryptWithHMAC(input
164                             , reccert.getPublicKey()
165                             , algorithm
166                             , seed.getBytes()
167                             , strength
168                             , mode
169                             , padding);
170                 }
171             }
172         } catch (Exception JavaDoc e) {
173             throw new JspException JavaDoc("JCE Exception: " + e.getMessage(), e);
174         }
175
176         // decide what to do with the result
177
if (var != null) {
178             if (output != null) {
179                 pageContext.setAttribute(var, output, scope);
180             }
181         } else {
182             if (file == null || file == "") {
183                 if (bodyContent != null) {
184                     bodyContent.clearBody();
185                 }
186
187                 try {
188                     JspWriter JavaDoc w = pageContext.getOut();
189                     w.print(output);
190                 } catch (IOException JavaDoc ex) {
191                     throw new JspException JavaDoc(ex.getMessage(), ex);
192                 }
193             }
194         }
195
196         return EVAL_PAGE;
197     }
198
199     public void release() {
200         // Cleanup all sensitive information
201
Clean.blank(value);
202         Clean.blank(input);
203         Clean.blank(output);
204
205         super.release();
206     } //release()
207

208     /**
209      * @jsp.attribute
210      * description="Optional attribute, the string to encrypt or decrypt. The body of the tag will be taken if omitted"
211      * type="java.lang.StringBuffer"
212      * required="false"
213      * rtexprvalue="true"
214      */

215     public void setValue(StringBuffer JavaDoc value) {
216         this.value = value;
217     }
218
219     public StringBuffer JavaDoc getValue() {
220         return value;
221     }
222
223     /**
224      * @jsp.attribute
225      * description="Optional attribute, variable to store the encrypted string. The string will be printed if omitted"
226      * type="java.lang.String"
227      * required="false"
228      * rtexprvalue="false"
229      */

230     public void setVar(String JavaDoc var) {
231         this.var = var;
232     }
233
234     public String JavaDoc getVar() {
235         return var;
236     }
237
238     /**
239      * @jsp.attribute
240      * description="Scope of the 'var' attribute. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
241      * type="java.lang.String"
242      * required="false"
243      * rtexprvalue="false"
244      */

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

256     public void setAlgorithm(String JavaDoc algorithm) {
257         this.algorithm = algorithm;
258     }
259
260     /**
261      * @jsp.attribute
262      * description="Optional seed for SecureRandom "
263      * type="java.lang.String"
264      * required="false"
265      * rtexprvalue="true"
266      */

267     public void setSeed(String JavaDoc seed) {
268         this.seed = seed;
269     }
270
271     /**
272      * @jsp.attribute
273      * description="The key size in bits (integer value). Values depends on the used algorithm (see next paragraph). Default is 256 (for AES)"
274      * type="java.lang.String"
275      * required="false"
276      * rtexprvalue="true"
277      */

278     public void setStrength(int strength) {
279         this.strength = strength;
280     }
281
282     /**
283      * @jsp.attribute
284      * description="Encryption mode. Default is 'CBC'"
285      * type="java.lang.String"
286      * required="false"
287      * rtexprvalue="true"
288      */

289     public void setMode(String JavaDoc mode) {
290         this.mode = mode;
291     }
292
293     /**
294      * @jsp.attribute
295      * description="Padding scheme. Default is 'PKCS7Padding'"
296      * type="java.lang.String"
297      * required="false"
298      * rtexprvalue="true"
299      */

300     public void setPadding(String JavaDoc padding) {
301         this.padding = padding;
302     }
303
304     /**
305      * @jsp.attribute
306      * description="The receiver's certificate stored in a PEM file"
307      * type="java.lang.String"
308      * required="false"
309      * rtexprvalue="true"
310      */

311     public void setRecpemfile(String JavaDoc recpemfile) {
312         this.recpemfile = recpemfile;
313     }
314
315     /**
316      * @jsp.attribute
317      * description="The receiver's certificate as a PEM formatted string"
318      * type="java.lang.String"
319      * required="false"
320      * rtexprvalue="true"
321      */

322     public void setRecpemstring(String JavaDoc recpemstring) {
323         this.recpemstring = recpemstring;
324     }
325
326     /**
327      * @jsp.attribute
328      * description="Encrypts a file instead of a string"
329      * type="java.lang.String"
330      * required="false"
331      * rtexprvalue="true"
332      */

333     public void setFile(String JavaDoc file) {
334         this.file = file;
335     }
336
337     /**
338      * @jsp.attribute
339      * description="File(name) to store the encrypted data"
340      * type="java.lang.String"
341      * required="false"
342      * rtexprvalue="true"
343      */

344     public void setNewfile(String JavaDoc newfile) {
345         this.newfile = newfile;
346     }
347 }
Popular Tags