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 import net.sourceforge.jcetaglib.tools.SignerCertificate; 26 import org.bouncycastle.util.encoders.Base64; 27 28 import javax.servlet.jsp.JspException ; 29 import javax.servlet.jsp.JspWriter ; 30 import javax.servlet.jsp.PageContext ; 31 import javax.servlet.jsp.tagext.BodyTagSupport ; 32 import java.io.IOException ; 33 import java.security.PrivateKey ; 34 import java.security.cert.Certificate ; 35 36 60 public class DecryptAndVerifySignature extends BodyTagSupport { 61 private static final String PAGE = "page"; 62 private static final String REQUEST = "request"; 63 private static final String SESSION = "session"; 64 private static final String APPLICATION = "application"; 65 66 private StringBuffer value; private String var; private int scope = PageContext.PAGE_SCOPE; 70 private String file; private String newfile; 73 private String signame = "MD5withRSA"; private String algorithm = "AES"; private String mode = "CBC"; private String padding = "PKCS7Padding"; 78 private String storefile; private String storeentry; private StringBuffer storepassword; 83 private String validsignature; 85 private String certificate; 86 87 private StringBuffer input; private StringBuffer output; 90 public static int getScope(String scope) { 91 int ret = PageContext.PAGE_SCOPE; 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 } 105 public int doEndTag() throws JspException { 106 PrivateKey privKey = null; 107 108 if (value != null) { 110 input = value; 112 } else { 113 if (bodyContent == null || bodyContent.getString() == null) { 115 input = new StringBuffer (""); 116 } else { 117 input = new StringBuffer (bodyContent.getString().trim()); 118 } 119 } 120 121 try { 123 privKey = X509Cert.getPrivateFromP12(storefile, storeentry, storepassword); 124 } catch (Exception e) { 125 throw new JspException ("JCE Exception - keystore could not be loaded: " + e.getMessage(), e); 126 } 127 128 SignerCertificate info = new SignerCertificate(); 129 130 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 pageContext.setAttribute(certificate, "-----BEGIN CERTIFICATE-----\n" + new String (certB64) + "\n-----END CERTIFICATE-----", scope); 158 160 } catch (Exception e) { 161 throw new JspException ("JCE Exception: " + e.getMessage(), e); 162 } 163 164 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 w = pageContext.getOut(); 177 w.print(output); 178 } catch (IOException ex) { 179 throw new JspException (ex.getMessage(), ex); 180 } 181 } 182 } 183 184 privKey = null; 185 186 return EVAL_PAGE; 187 } 188 189 public void release() { 190 Clean.blank(value); 192 Clean.blank(storepassword); 193 Clean.blank(input); 194 Clean.blank(output); 195 196 super.release(); 197 } 199 206 public void setValue(StringBuffer value) { 207 this.value = value; 208 } 209 210 public StringBuffer getValue() { 211 return value; 212 } 213 214 221 public void setVar(String var) { 222 this.var = var; 223 } 224 225 public String getVar() { 226 return var; 227 } 228 229 236 public void setScope(String scope) { 237 this.scope = getScope(scope); 238 } 239 240 247 public void setSigname(String signame) { 248 this.signame = signame; 249 } 250 251 258 public void setAlgorithm(String algorithm) { 259 this.algorithm = algorithm; 260 } 261 262 269 public void setMode(String mode) { 270 this.mode = mode; 271 } 272 273 280 public void setPadding(String padding) { 281 this.padding = padding; 282 } 283 284 291 public void setStorefile(String storefile) { 292 this.storefile = storefile; 293 } 294 295 302 public void setStoreentry(String storeentry) { 303 this.storeentry = storeentry; 304 } 305 306 313 public void setStorepassword(StringBuffer storepassword) { 314 this.storepassword = storepassword; 315 } 316 317 324 public String getValidsignature() { 325 return validsignature; 326 } 327 328 public void setValidsignature(String validsignature) { 329 this.validsignature = validsignature; 330 } 331 332 339 public void setCertificate(String certificate) { 340 this.certificate = certificate; 341 } 342 343 public String getCertificate() { 344 return certificate; 345 } 346 347 354 public void setFile(String file) { 355 this.file = file; 356 } 357 358 365 public void setNewfile(String newfile) { 366 this.newfile = newfile; 367 } 368 } | Popular Tags |