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.Signatures; 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 36 58 public class CreateSignature extends BodyTagSupport { 59 60 private static final String PAGE = "page"; 61 private static final String REQUEST = "request"; 62 private static final String SESSION = "session"; 63 private static final String APPLICATION = "application"; 64 65 private StringBuffer value; private String var; private int scope = PageContext.PAGE_SCOPE; 69 private String signame = "RIPEMD160WithRSA/ISO9796-2"; private String file; 72 73 74 private String storefile; private String storeentry; private StringBuffer storepassword; 79 private String pemstring; 82 private String pemfile; 85 private StringBuffer input; private StringBuffer output; 88 public static int getScope(String scope) { 89 int ret = PageContext.PAGE_SCOPE; 91 if (REQUEST.equalsIgnoreCase(scope)) 92 ret = PageContext.REQUEST_SCOPE; 93 else if (SESSION.equalsIgnoreCase(scope)) 94 ret = PageContext.SESSION_SCOPE; 95 else if (APPLICATION.equalsIgnoreCase(scope)) 96 ret = PageContext.APPLICATION_SCOPE; 97 else if (PAGE.equalsIgnoreCase(scope)) 98 ret = PageContext.PAGE_SCOPE; 99 100 return ret; 101 } 103 public int doEndTag() throws JspException { 104 if (value != null) { 106 input = value; 108 } else { 109 if (bodyContent == null || bodyContent.getString() == null) { 111 input = new StringBuffer (""); 112 } else { 113 input = new StringBuffer (bodyContent.getString().trim()); 114 } 115 } 116 117 PrivateKey signingKey; 118 119 try { 120 if (storefile == null || storefile == "") { 122 if (pemfile == null || pemfile == "") { 123 InputStream pemstream = new ByteArrayInputStream (pemstring.getBytes()); 125 signingKey = CertTools.getPrivatefromPEM(pemstream, ""); 126 } else { 127 signingKey = CertTools.getPrivatefromPEM(pemfile, ""); 129 } 130 } else { 131 signingKey = X509Cert.getPrivateFromP12(storefile, storeentry, storepassword); 133 } 134 } catch (Exception e) { 135 throw new JspException ("JCE Exception - keystore could not be loaded: " + e.getMessage(), e); 136 } 137 138 try { 139 if (file != null) { 140 output = Signatures.generateFileSIG(file, signingKey, signame); 141 } else { 142 output = Signatures.generateSIG(input, signingKey, signame); 143 } 144 } catch (Exception e) { 145 throw new JspException ("JCE Exception: " + e.getMessage(), e); 146 } 147 148 if (var != null) { 150 if (output != null) { 151 pageContext.setAttribute(var, output, scope); 152 } 153 } else { 154 if (bodyContent != null) { 155 bodyContent.clearBody(); 156 } 157 158 try { 159 JspWriter w = pageContext.getOut(); 160 w.print(output); 161 } catch (IOException ex) { 162 throw new JspException (ex.getMessage(), ex); 163 } 164 } 165 166 return EVAL_PAGE; 167 } 169 public void release() { 170 Clean.blank(value); 172 Clean.blank(storepassword); 173 Clean.blank(input); 174 Clean.blank(output); 175 176 super.release(); 177 } 179 186 public void setValue(StringBuffer value) { 187 this.value = value; 188 } 189 190 public StringBuffer getValue() { 191 return value; 192 } 193 194 201 public void setVar(String var) { 202 this.var = var; 203 } 204 205 public String getVar() { 206 return var; 207 } 208 209 216 public void setScope(String scope) { 217 this.scope = getScope(scope); 218 } 219 220 227 public void setSigname(String signame) { 228 this.signame = signame; 229 } 230 231 238 public void setFile(String file) { 239 this.file = file; 240 } 241 242 249 public void setStorefile(String storefile) { 250 this.storefile = storefile; 251 } 252 253 260 public void setStoreentry(String storeentry) { 261 this.storeentry = storeentry; 262 } 263 264 271 public void setStorepassword(StringBuffer storepassword) { 272 this.storepassword = storepassword; 273 } 274 275 282 public void setPemfile(String pemfile) { 283 this.pemfile = pemfile; 284 } 285 286 293 public void setPemstring(String pemstring) { 294 this.pemstring = pemstring; 295 } 296 297 } | Popular Tags |