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 org.bouncycastle.jce.provider.BouncyCastleProvider; 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.TagSupport ; 31 import java.io.ByteArrayInputStream ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.security.Security ; 35 import java.security.cert.X509Certificate ; 36 37 57 58 public class CertificateInfo extends TagSupport { 59 private static final String PAGE = "page"; 60 private static final String REQUEST = "request"; 61 private static final String SESSION = "session"; 62 private static final String APPLICATION = "application"; 63 64 private int scope = PageContext.PAGE_SCOPE; 66 private String subject; 67 private String issuer; 68 private String validfrom; 69 private String validto; 70 private String algorithm; 71 private String serialnumber; 72 private String fingerprint; 73 74 75 76 private String storefile; private String storeentry; private StringBuffer storepassword; 81 private String pemstring; 84 private String pemfile; 87 public static int getScope(String scope) { 88 int ret = PageContext.PAGE_SCOPE; 90 if (REQUEST.equalsIgnoreCase(scope)) 91 ret = PageContext.REQUEST_SCOPE; 92 else if (SESSION.equalsIgnoreCase(scope)) 93 ret = PageContext.SESSION_SCOPE; 94 else if (APPLICATION.equalsIgnoreCase(scope)) 95 ret = PageContext.APPLICATION_SCOPE; 96 else if (PAGE.equalsIgnoreCase(scope)) 97 ret = PageContext.PAGE_SCOPE; 98 99 return ret; 100 } 102 public int doEndTag() throws JspException { 103 Security.addProvider(new BouncyCastleProvider()); 104 105 X509Certificate cert = null; 106 107 try { 108 if (storefile == null || storefile == "") { 110 if (pemfile == null || pemfile == "") { 111 InputStream pemstream = new ByteArrayInputStream (pemstring.getBytes()); 113 cert = CertTools.getCertfromPEM(pemstream); 114 } else { 115 cert = CertTools.getCertfromPEM(pemfile); 117 } 118 } else { 119 cert = X509Cert.getCertificateFromP12(storefile, storeentry, storepassword); 121 } 122 123 if (subject == null || subject == "") { 124 try { 126 JspWriter w = pageContext.getOut(); 127 w.print("For: " + cert.getSubjectDN() + "<BR>"); 128 w.print("Issued by: " + cert.getIssuerDN() + "<BR>"); 129 w.print("Valid from " + cert.getNotBefore() + " to " + cert.getNotAfter() + "<BR>"); 130 w.print("Certificate SN#: " + cert.getSerialNumber() + "<BR>"); 131 w.print("Generated with: " + cert.getSigAlgName() + "<BR>"); 132 w.print("Fingerprint: " + CertTools.getFingerprintAsString(cert) + "<BR>"); 133 } catch (IOException ex) { 134 throw new JspException (ex.getMessage(), ex); 135 } 136 } else { 137 pageContext.setAttribute(subject, cert.getSubjectDN(), scope); 139 pageContext.setAttribute(issuer, cert.getIssuerDN(), scope); 140 pageContext.setAttribute(validfrom, cert.getNotBefore(), scope); 141 pageContext.setAttribute(validto, cert.getNotAfter(), scope); 142 pageContext.setAttribute(algorithm, cert.getSigAlgName(), scope); 143 pageContext.setAttribute(serialnumber, cert.getSerialNumber(), scope); 144 pageContext.setAttribute(fingerprint, CertTools.getFingerprintAsString(cert), scope); 145 } 146 } catch (Exception e) { 147 throw new JspException ("JCE Exception: Could not retrieve certificate info: " + e.getMessage(), e); 148 } 149 150 return EVAL_PAGE; 151 } 153 public void release() { 154 Clean.blank(storepassword); 156 157 super.release(); 158 } 160 167 public void setScope(String scope) { 168 this.scope = getScope(scope); 169 } 170 171 178 public void setStorefile(String storefile) { 179 this.storefile = storefile; 180 } 181 182 189 public void setStoreentry(String storeentry) { 190 this.storeentry = storeentry; 191 } 192 193 200 public void setStorepassword(StringBuffer storepassword) { 201 this.storepassword = storepassword; 202 } 203 204 211 public void setPemfile(String pemfile) { 212 this.pemfile = pemfile; 213 } 214 215 222 public void setPemstring(String pemstring) { 223 this.pemstring = pemstring; 224 } 225 226 233 public void setSubject(String subject) { 234 this.subject = subject; 235 } 236 237 public String getSubject() { 238 return subject; 239 } 240 241 248 public void setIssuer(String issuer) { 249 this.issuer = issuer; 250 } 251 252 public String getIssuer() { 253 return issuer; 254 } 255 256 263 public void setValidfrom(String validfrom) { 264 this.validfrom = validfrom; 265 } 266 267 public String getValidfrom() { 268 return validfrom; 269 } 270 271 278 public void setValidto(String validto) { 279 this.validto = validto; 280 } 281 282 public String getValidto() { 283 return validto; 284 } 285 286 293 public void setAlgorithm(String algorithm) { 294 this.algorithm = algorithm; 295 } 296 297 public String getAlgorithm() { 298 return algorithm; 299 } 300 301 308 public void setSerialnumber(String serialnumber) { 309 this.serialnumber = serialnumber; 310 } 311 312 public String getSerialnumber() { 313 return serialnumber; 314 } 315 316 323 public void setFingerprint(String fingerprint) { 324 this.fingerprint = fingerprint; 325 } 326 327 public String getFingerprint() { 328 return fingerprint; 329 } 330 } | Popular Tags |