1 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 import net.sourceforge.jcetaglib.lib.X509Cert; 26 27 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.JspWriter ; 29 import javax.servlet.jsp.PageContext ; 30 import javax.servlet.jsp.tagext.BodyTagSupport ; 31 import java.io.ByteArrayInputStream ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.security.PrivateKey ; 35 import java.security.cert.X509Certificate ; 36 37 61 public class EncryptAndSign extends BodyTagSupport { 62 private static final String PAGE = "page"; 63 private static final String REQUEST = "request"; 64 private static final String SESSION = "session"; 65 private static final String APPLICATION = "application"; 66 67 private StringBuffer value; private String var; private int scope = PageContext.PAGE_SCOPE; 71 private String file; private String newfile; 74 private String signame = "MD5withRSA"; private String algorithm = "AES"; private String seed; private int strength = 256; private String mode = "CBC"; private String padding = "PKCS7Padding"; 81 82 83 private String storefile; private String storeentry; private StringBuffer storepassword; 88 89 90 private String recpemstring; 93 private String recpemfile; 96 private StringBuffer input; private StringBuffer output; 99 public static int getScope(String scope) { 100 int ret = PageContext.PAGE_SCOPE; 102 if (REQUEST.equalsIgnoreCase(scope)) 103 ret = PageContext.REQUEST_SCOPE; 104 else if (SESSION.equalsIgnoreCase(scope)) 105 ret = PageContext.SESSION_SCOPE; 106 else if (APPLICATION.equalsIgnoreCase(scope)) 107 ret = PageContext.APPLICATION_SCOPE; 108 else if (PAGE.equalsIgnoreCase(scope)) 109 ret = PageContext.PAGE_SCOPE; 110 111 return ret; 112 } 114 public int doEndTag() throws JspException { 115 X509Certificate cert = null; 116 X509Certificate reccert = null; 117 PrivateKey signingKey = null; 118 119 if (value != null) { 121 input = value; 123 } else { 124 if (bodyContent == null || bodyContent.getString() == null) { 126 input = new StringBuffer (""); 127 } else { 128 input = new StringBuffer (bodyContent.getString().trim()); 129 } 130 } 131 132 try { 134 signingKey = X509Cert.getPrivateFromP12(storefile, storeentry, storepassword); 135 cert = X509Cert.getCertificateFromP12(storefile, storeentry, storepassword); 136 } catch (Exception e) { 137 throw new JspException ("JCE Exception - keystore could not be loaded: " + e.getMessage(), e); 138 } 139 140 try { 142 if (recpemfile == null || recpemfile == "") { 143 InputStream pemstream = new ByteArrayInputStream (recpemstring.getBytes()); 145 reccert = CertTools.getCertfromPEM(pemstream); 146 } else { 147 reccert = CertTools.getCertfromPEM(recpemfile); 149 } 150 } catch (Exception e) { 151 throw new JspException ("JCE Exception - PEM could not be loaded: " + e.getMessage(), e); 152 } 153 154 try { 156 if (file != null) { 157 if (seed == null) { 158 Hybrid.encryptFileAndSign(file 159 , newfile 160 , reccert.getPublicKey() 161 , signingKey 162 , cert 163 , signame 164 , algorithm 165 , null 166 , strength 167 , mode 168 , padding); 169 } else { 170 Hybrid.encryptFileAndSign(file 171 , newfile 172 , reccert.getPublicKey() 173 , signingKey 174 , cert 175 , signame 176 , algorithm 177 , seed.getBytes() 178 , strength 179 , mode 180 , padding); 181 } 182 } else { 183 if (seed == null) { 184 output = Hybrid.encryptAndSign(input 185 , reccert.getPublicKey() 186 , signingKey 187 , cert 188 , signame 189 , algorithm 190 , null 191 , strength 192 , mode 193 , padding); 194 } else { 195 output = Hybrid.encryptAndSign(input 196 , reccert.getPublicKey() 197 , signingKey 198 , cert 199 , signame 200 , algorithm 201 , seed.getBytes() 202 , strength 203 , mode 204 , padding); 205 } 206 } 207 } catch (Exception e) { 208 throw new JspException ("JCE Exception: " + e.getMessage(), e); 209 } 210 211 if (var != null) { 213 if (output != null) { 214 pageContext.setAttribute(var, output, scope); 215 } 216 } else { 217 if (file == null || file == "") { 218 if (bodyContent != null) { 219 bodyContent.clearBody(); 220 } 221 222 try { 223 JspWriter w = pageContext.getOut(); 224 w.print(output); 225 } catch (IOException ex) { 226 throw new JspException (ex.getMessage(), ex); 227 } 228 } 229 } 230 231 signingKey = null; 232 233 return EVAL_PAGE; 234 } 235 236 public void release() { 237 Clean.blank(value); 239 Clean.blank(storepassword); 240 Clean.blank(input); 241 Clean.blank(output); 242 243 super.release(); 244 } 246 253 public void setValue(StringBuffer value) { 254 this.value = value; 255 } 256 257 public StringBuffer getValue() { 258 return value; 259 } 260 261 268 public void setVar(String var) { 269 this.var = var; 270 } 271 272 public String getVar() { 273 return var; 274 } 275 276 283 public void setScope(String scope) { 284 this.scope = getScope(scope); 285 } 286 287 294 public void setSigname(String signame) { 295 this.signame = signame; 296 } 297 298 305 public void setAlgorithm(String algorithm) { 306 this.algorithm = algorithm; 307 } 308 309 316 public void setSeed(String seed) { 317 this.seed = seed; 318 } 319 320 327 public void setStrength(int strength) { 328 this.strength = strength; 329 } 330 331 338 public void setMode(String mode) { 339 this.mode = mode; 340 } 341 342 349 public void setPadding(String padding) { 350 this.padding = padding; 351 } 352 353 360 public void setStorefile(String storefile) { 361 this.storefile = storefile; 362 } 363 364 371 public void setStoreentry(String storeentry) { 372 this.storeentry = storeentry; 373 } 374 375 382 public void setStorepassword(StringBuffer storepassword) { 383 this.storepassword = storepassword; 384 } 385 386 393 public void setRecpemfile(String recpemfile) { 394 this.recpemfile = recpemfile; 395 } 396 397 404 public void setRecpemstring(String recpemstring) { 405 this.recpemstring = recpemstring; 406 } 407 408 415 public void setFile(String file) { 416 this.file = file; 417 } 418 419 426 public void setNewfile(String newfile) { 427 this.newfile = newfile; 428 } 429 } | Popular Tags |