1 19 20 package net.sourceforge.jcetaglib.taglib.crypto; 21 22 import net.sourceforge.jcetaglib.lib.Asymmetric; 23 import net.sourceforge.jcetaglib.lib.CertTools; 24 import net.sourceforge.jcetaglib.lib.Clean; 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.PublicKey ; 36 import java.security.cert.X509Certificate ; 37 38 64 public class EncryptWithCert extends BodyTagSupport { 65 66 private static final String PAGE = "page"; 67 private static final String REQUEST = "request"; 68 private static final String SESSION = "session"; 69 private static final String APPLICATION = "application"; 70 71 private static final String ENCRYPT = "encrypt"; 72 73 private StringBuffer value; private String var; private int scope = PageContext.PAGE_SCOPE; 77 78 79 private String storefile; private String storeentry; private StringBuffer storepassword; 84 private String pemstring; 87 private String pemfile; 90 private String action = "ENCRYPT"; 92 private StringBuffer input; private StringBuffer output; 95 public static int getScope(String scope) { 96 int ret = PageContext.PAGE_SCOPE; 98 if (REQUEST.equalsIgnoreCase(scope)) 99 ret = PageContext.REQUEST_SCOPE; 100 else if (SESSION.equalsIgnoreCase(scope)) 101 ret = PageContext.SESSION_SCOPE; 102 else if (APPLICATION.equalsIgnoreCase(scope)) 103 ret = PageContext.APPLICATION_SCOPE; 104 else if (PAGE.equalsIgnoreCase(scope)) 105 ret = PageContext.PAGE_SCOPE; 106 107 return ret; 108 } 110 public int doEndTag() throws JspException { 111 112 if (value != null) { 114 input = value; 116 } else { 117 if (bodyContent == null || bodyContent.getString() == null) { 119 input = new StringBuffer (""); 120 } else { 121 input = new StringBuffer (bodyContent.getString().trim()); 122 } 123 } 124 125 try { 127 if (ENCRYPT.equalsIgnoreCase(action)) { 128 X509Certificate cert = null; 129 PublicKey encryptKey; 130 131 try { 132 if (storefile == null || storefile == "") { 134 if (pemfile == null || pemfile == "") { 135 InputStream pemstream = new ByteArrayInputStream (pemstring.getBytes()); 137 cert = CertTools.getCertfromPEM(pemstream); 138 } else { 139 cert = CertTools.getCertfromPEM(pemfile); 141 } 142 } else { 143 cert = X509Cert.getCertificateFromP12(storefile, storeentry, storepassword); 145 } 146 } catch (Exception e) { 147 throw new JspException ("JCE Exception - keystore could not be loaded: " + e.getMessage(), e); 148 } 149 150 encryptKey = cert.getPublicKey(); 152 153 output = Asymmetric.encrypt(input, encryptKey); 154 155 } else { 156 PrivateKey decryptKey; 157 158 try { 159 if (storefile == null || storefile == "") { 161 if (pemfile == null || pemfile == "") { 162 InputStream pemstream = new ByteArrayInputStream (pemstring.getBytes()); 164 decryptKey = CertTools.getPrivatefromPEM(pemstream, ""); 165 } else { 166 decryptKey = CertTools.getPrivatefromPEM(pemfile, ""); 168 } 169 } else { 170 decryptKey = X509Cert.getPrivateFromP12(storefile, storeentry, storepassword); 172 } 173 } catch (Exception e) { 174 throw new JspException ("JCE Exception - keystore could not be loaded: " + e.getMessage(), e); 175 } 176 177 output = Asymmetric.decrypt(input, decryptKey); 178 } 179 } catch (Exception e) { 180 throw new JspException ("JCE Exception: " + e.getMessage(), e); 181 } 182 183 if (var != null) { 185 if (output != null) { 186 pageContext.setAttribute(var, output, scope); 187 } 188 } else { 189 if (bodyContent != null) { 190 bodyContent.clearBody(); 191 } 192 193 try { 194 JspWriter w = pageContext.getOut(); 195 w.print(output); 196 } catch (IOException ex) { 197 throw new JspException (ex.getMessage(), ex); 198 } 199 } 200 201 return EVAL_PAGE; 202 } 204 public void release() { 205 Clean.blank(value); 207 Clean.blank(storepassword); 208 Clean.blank(input); 209 Clean.blank(output); 210 211 super.release(); 212 } 214 221 public void setValue(StringBuffer value) { 222 this.value = value; 223 } 224 225 public StringBuffer getValue() { 226 return value; 227 } 228 229 236 public void setVar(String var) { 237 this.var = var; 238 } 239 240 public String getVar() { 241 return var; 242 } 243 244 251 public void setScope(String scope) { 252 this.scope = getScope(scope); 253 } 254 255 262 public void setStorefile(String storefile) { 263 this.storefile = storefile; 264 } 265 266 273 public void setStoreentry(String storeentry) { 274 this.storeentry = storeentry; 275 } 276 277 284 public void setStorepassword(StringBuffer storepassword) { 285 this.storepassword = storepassword; 286 } 287 288 295 public void setPemfile(String pemfile) { 296 this.pemfile = pemfile; 297 } 298 299 306 public void setPemstring(String pemstring) { 307 this.pemstring = pemstring; 308 } 309 310 317 public void setAction(String action) { 318 this.action = action; 319 } 320 } | Popular Tags |