1 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 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspWriter ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.tagext.BodyTagSupport ; 30 import java.io.IOException ; 31 import java.security.PrivateKey ; 32 33 55 public class DecryptAndVerifyHMAC extends BodyTagSupport { 56 private static final String PAGE = "page"; 57 private static final String REQUEST = "request"; 58 private static final String SESSION = "session"; 59 private static final String APPLICATION = "application"; 60 61 private StringBuffer value; private String var; private int scope = PageContext.PAGE_SCOPE; 65 private String file; private String newfile; 68 private String algorithm = "AES"; private String mode = "CBC"; private String padding = "PKCS7Padding"; 72 private String storefile; private String storeentry; private StringBuffer storepassword; 77 private StringBuffer input; private StringBuffer output; 80 public static int getScope(String scope) { 81 int ret = PageContext.PAGE_SCOPE; 83 if (REQUEST.equalsIgnoreCase(scope)) 84 ret = PageContext.REQUEST_SCOPE; 85 else if (SESSION.equalsIgnoreCase(scope)) 86 ret = PageContext.SESSION_SCOPE; 87 else if (APPLICATION.equalsIgnoreCase(scope)) 88 ret = PageContext.APPLICATION_SCOPE; 89 else if (PAGE.equalsIgnoreCase(scope)) 90 ret = PageContext.PAGE_SCOPE; 91 92 return ret; 93 } 95 public int doEndTag() throws JspException { 96 PrivateKey privKey = null; 97 98 if (value != null) { 100 input = value; 102 } else { 103 if (bodyContent == null || bodyContent.getString() == null) { 105 input = new StringBuffer (""); 106 } else { 107 input = new StringBuffer (bodyContent.getString().trim()); 108 } 109 } 110 111 try { 113 privKey = X509Cert.getPrivateFromP12(storefile, storeentry, storepassword); 114 } catch (Exception e) { 115 throw new JspException ("JCE Exception - keystore could not be loaded: " + e.getMessage(), e); 116 } 117 118 try { 120 if (file != null) { 121 Hybrid.decryptFileAndVerifyHMAC(file 122 , newfile 123 , privKey 124 , algorithm 125 , mode 126 , padding); 127 } else { 128 output = Hybrid.decryptAndVerifyHMAC(input 129 , privKey 130 , algorithm 131 , mode 132 , padding); 133 } 134 } catch (Exception e) { 135 throw new JspException ("JCE Exception: " + e.getMessage(), e); 136 } 137 138 if (var != null) { 140 if (output != null) { 141 pageContext.setAttribute(var, output, scope); 142 } 143 } else { 144 if (file == null || file == "") { 145 if (bodyContent != null) { 146 bodyContent.clearBody(); 147 } 148 149 try { 150 JspWriter w = pageContext.getOut(); 151 w.print(output); 152 } catch (IOException ex) { 153 throw new JspException (ex.getMessage(), ex); 154 } 155 } 156 } 157 158 privKey = null; 159 160 return EVAL_PAGE; 161 } 162 163 public void release() { 164 Clean.blank(value); 166 Clean.blank(storepassword); 167 Clean.blank(input); 168 Clean.blank(output); 169 170 super.release(); 171 } 173 180 public void setValue(StringBuffer value) { 181 this.value = value; 182 } 183 184 public StringBuffer getValue() { 185 return value; 186 } 187 188 195 public void setVar(String var) { 196 this.var = var; 197 } 198 199 public String getVar() { 200 return var; 201 } 202 203 210 public void setScope(String scope) { 211 this.scope = getScope(scope); 212 } 213 214 221 public void setAlgorithm(String algorithm) { 222 this.algorithm = algorithm; 223 } 224 225 232 public void setMode(String mode) { 233 this.mode = mode; 234 } 235 236 243 public void setPadding(String padding) { 244 this.padding = padding; 245 } 246 247 254 public void setStorefile(String storefile) { 255 this.storefile = storefile; 256 } 257 258 265 public void setStoreentry(String storeentry) { 266 this.storeentry = storeentry; 267 } 268 269 276 public void setStorepassword(StringBuffer storepassword) { 277 this.storepassword = storepassword; 278 } 279 280 287 public void setFile(String file) { 288 this.file = file; 289 } 290 291 298 public void setNewfile(String newfile) { 299 this.newfile = newfile; 300 } 301 } | Popular Tags |