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.PublicKey ; 35 import java.security.cert.X509Certificate ; 36 37 58 public class VerifySignature 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 StringBuffer signature; private String file; 73 74 75 private String storefile; private String storeentry; private StringBuffer storepassword; 80 private String pemstring; 83 private String pemfile; 86 private StringBuffer input; private StringBuffer output; 89 public static int getScope(String scope) { 90 int ret = PageContext.PAGE_SCOPE; 92 if (REQUEST.equalsIgnoreCase(scope)) 93 ret = PageContext.REQUEST_SCOPE; 94 else if (SESSION.equalsIgnoreCase(scope)) 95 ret = PageContext.SESSION_SCOPE; 96 else if (APPLICATION.equalsIgnoreCase(scope)) 97 ret = PageContext.APPLICATION_SCOPE; 98 else if (PAGE.equalsIgnoreCase(scope)) 99 ret = PageContext.PAGE_SCOPE; 100 101 return ret; 102 } 104 public int doEndTag() throws JspException { 105 boolean ret; 106 107 if (value != null) { 109 input = value; 111 } else { 112 if (bodyContent == null || bodyContent.getString() == null) { 114 input = new StringBuffer (""); 115 } else { 116 input = new StringBuffer (bodyContent.getString().trim()); 117 } 118 } 119 120 X509Certificate cert = null; 121 PublicKey verifyKey; 122 123 try { 124 if (storefile == null || storefile == "") { 126 if (pemfile == null || pemfile == "") { 127 InputStream pemstream = new ByteArrayInputStream (pemstring.getBytes()); 129 cert = CertTools.getCertfromPEM(pemstream); 130 } else { 131 cert = CertTools.getCertfromPEM(pemfile); 133 } 134 } else { 135 cert = X509Cert.getCertificateFromP12(storefile, storeentry, storepassword); 137 } 138 } catch (Exception e) { 139 throw new JspException ("JCE Exception - keystore could not be loaded: " + e.getMessage(), e); 140 } 141 142 verifyKey = cert.getPublicKey(); 144 145 try { 146 if (file != null) { 147 ret = Signatures.verifyFileSIG(file, signature, verifyKey, signame); 148 } else { 149 ret = Signatures.verifySIG(input, signature, verifyKey, signame); 150 } 151 } catch (Exception e) { 152 throw new JspException ("JCE Exception: " + e.toString(), e); 153 } 154 155 if (ret) { 156 output = new StringBuffer ("true"); 157 } else { 158 output = new StringBuffer ("false"); 159 } 160 161 if (var != null) { 163 if (output != null) { 164 pageContext.setAttribute(var, output, scope); 165 } 166 } else { 167 if (bodyContent != null) { 168 bodyContent.clearBody(); 169 } 170 171 try { 172 JspWriter w = pageContext.getOut(); 173 w.print(output); 174 } catch (IOException ex) { 175 throw new JspException (ex.getMessage(), ex); 176 } 177 } 178 179 return EVAL_PAGE; 180 } 182 public void release() { 183 Clean.blank(value); 185 Clean.blank(storepassword); 186 Clean.blank(signature); 187 Clean.blank(input); 188 Clean.blank(output); 189 190 super.release(); 191 } 193 194 201 public void setValue(StringBuffer value) { 202 this.value = value; 203 } 204 205 public StringBuffer getValue() { 206 return value; 207 } 208 209 216 public void setVar(String var) { 217 this.var = var; 218 } 219 220 public String getVar() { 221 return var; 222 } 223 224 231 public void setScope(String scope) { 232 this.scope = getScope(scope); 233 } 234 235 242 public void setSignature(StringBuffer signature) { 243 this.signature = signature; 244 } 245 246 253 public void setSigname(String signame) { 254 this.signame = signame; 255 } 256 257 264 public void setFile(String file) { 265 this.file = file; 266 } 267 268 275 public void setStorefile(String storefile) { 276 this.storefile = storefile; 277 } 278 279 286 public void setStoreentry(String storeentry) { 287 this.storeentry = storeentry; 288 } 289 290 297 public void setStorepassword(StringBuffer storepassword) { 298 this.storepassword = storepassword; 299 } 300 301 308 public void setPemfile(String pemfile) { 309 this.pemfile = pemfile; 310 } 311 312 319 public void setPemstring(String pemstring) { 320 this.pemstring = pemstring; 321 } 322 323 } | Popular Tags |