1 19 20 package net.sourceforge.jcetaglib.taglib.x509; 21 22 import net.sourceforge.jcetaglib.lib.CertTools; 23 import net.sourceforge.jcetaglib.lib.Clean; 24 import net.sourceforge.jcetaglib.lib.X509Cert; 25 import net.sourceforge.jcetaglib.tools.FileTools; 26 import org.bouncycastle.jce.provider.BouncyCastleProvider; 27 28 import javax.servlet.jsp.JspException ; 29 import javax.servlet.jsp.PageContext ; 30 import javax.servlet.jsp.tagext.TagSupport ; 31 import java.io.ByteArrayInputStream ; 32 import java.io.InputStream ; 33 import java.security.Security ; 34 import java.security.cert.X509CRL ; 35 import java.security.cert.X509Certificate ; 36 37 62 63 public class VerifyCertificate extends TagSupport { 64 private static final String PAGE = "page"; 65 private static final String REQUEST = "request"; 66 private static final String SESSION = "session"; 67 private static final String APPLICATION = "application"; 68 69 private String crlfile; 70 71 private int scope = PageContext.PAGE_SCOPE; 73 private String verifyinfo; 74 75 76 77 private String storefile; private String storeentry; private StringBuffer storepassword; 82 private String pemstring; 85 private String pemfile; 88 89 90 private String castorefile; private String castoreentry; private StringBuffer castorepassword; 95 private String capemstring; 98 private String capemfile; 101 public static int getScope(String scope) { 102 int ret = PageContext.PAGE_SCOPE; 104 if (REQUEST.equalsIgnoreCase(scope)) 105 ret = PageContext.REQUEST_SCOPE; 106 else if (SESSION.equalsIgnoreCase(scope)) 107 ret = PageContext.SESSION_SCOPE; 108 else if (APPLICATION.equalsIgnoreCase(scope)) 109 ret = PageContext.APPLICATION_SCOPE; 110 else if (PAGE.equalsIgnoreCase(scope)) 111 ret = PageContext.PAGE_SCOPE; 112 113 return ret; 114 } 116 public int doEndTag() throws JspException { 117 Security.addProvider(new BouncyCastleProvider()); 119 120 X509Certificate cert = null; 121 X509Certificate cacert = null; 122 X509CRL x509crl = null; 123 124 InputStream pemstream = null; 125 126 try { 127 if (storefile == null || storefile == "") { 129 if (pemfile == null || pemfile == "") { 130 pemstream = new ByteArrayInputStream (pemstring.getBytes()); 132 cert = CertTools.getCertfromPEM(pemstream); 133 } else { 134 cert = CertTools.getCertfromPEM(pemfile); 136 } 137 } else { 138 cert = X509Cert.getCertificateFromP12(storefile, storeentry, storepassword); 140 } 141 142 if (castorefile == null || castorefile == "") { 144 if (capemfile == null || capemfile == "") { 145 pemstream = new ByteArrayInputStream (capemstring.getBytes()); 147 cacert = CertTools.getCertfromPEM(pemstream); 148 } else { 149 cacert = CertTools.getCertfromPEM(capemfile); 151 } 152 } else { 153 cacert = X509Cert.getCACertificateFromP12(castorefile, castoreentry, castorepassword); 155 } 156 157 byte[] crl = FileTools.readFiletoBuffer(crlfile); 159 x509crl = CertTools.getCRLfromByteArray(crl); 160 161 pageContext.setAttribute(verifyinfo, X509Cert.verifyCertificate(cert, cacert, x509crl), scope); 162 } catch (Exception e) { 163 throw new JspException ("JCE Exception: Could not verify certificate: " + e.toString(), e); 164 } 165 166 return EVAL_PAGE; 167 } 169 public void release() { 170 Clean.blank(storepassword); 172 Clean.blank(castorepassword); 173 174 super.release(); 175 } 177 184 public void setScope(String scope) { 185 this.scope = getScope(scope); 186 } 187 188 195 public void setCrlfile(String crlfile) { 196 this.crlfile = crlfile; 197 } 198 199 206 public void setStorefile(String storefile) { 207 this.storefile = storefile; 208 } 209 210 217 public void setStoreentry(String storeentry) { 218 this.storeentry = storeentry; 219 } 220 221 228 public void setStorepassword(StringBuffer storepassword) { 229 this.storepassword = storepassword; 230 } 231 232 239 public void setPemfile(String pemfile) { 240 this.pemfile = pemfile; 241 } 242 243 250 public void setPemstring(String pemstring) { 251 this.pemstring = pemstring; 252 } 253 254 261 public void setCastorefile(String castorefile) { 262 this.castorefile = castorefile; 263 } 264 265 272 public void setCastoreentry(String castoreentry) { 273 this.castoreentry = castoreentry; 274 } 275 276 283 public void setCastorepassword(StringBuffer castorepassword) { 284 this.castorepassword = castorepassword; 285 } 286 287 294 public void setCapemfile(String capemfile) { 295 this.capemfile = capemfile; 296 } 297 298 305 public void setCapemstring(String capemstring) { 306 this.capemstring = capemstring; 307 } 308 309 320 public void setVerifyinfo(String verifyinfo) { 321 this.verifyinfo = verifyinfo; 322 } 323 324 public String getVerifyinfo() { 325 return verifyinfo; 326 } 327 } | Popular Tags |