1 19 20 package net.sourceforge.jcetaglib.taglib.x509; 21 22 import net.sourceforge.jcetaglib.lib.X509Cert; 23 import org.bouncycastle.asn1.pkcs.CertificationRequestInfo; 24 import org.bouncycastle.asn1.x509.X509Name; 25 import org.bouncycastle.jce.PKCS10CertificationRequest; 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.BodyTagSupport ; 31 import java.security.Security ; 32 33 52 53 public class CertificateRequestInfo extends BodyTagSupport { 54 private static final String PAGE = "page"; 55 private static final String REQUEST = "request"; 56 private static final String SESSION = "session"; 57 private static final String APPLICATION = "application"; 58 59 protected String certrequest; 61 private int scope = PageContext.PAGE_SCOPE; 62 63 private String subjectdn; 64 65 public static int getScope(String scope) { 66 int ret = PageContext.PAGE_SCOPE; 68 if (REQUEST.equalsIgnoreCase(scope)) 69 ret = PageContext.REQUEST_SCOPE; 70 else if (SESSION.equalsIgnoreCase(scope)) 71 ret = PageContext.SESSION_SCOPE; 72 else if (APPLICATION.equalsIgnoreCase(scope)) 73 ret = PageContext.APPLICATION_SCOPE; 74 else if (PAGE.equalsIgnoreCase(scope)) 75 ret = PageContext.PAGE_SCOPE; 76 77 return ret; 78 } 80 public int doEndTag() throws JspException { 81 String input; 82 83 if (certrequest != null) { 85 input = certrequest; 87 } else { 88 if (bodyContent == null || bodyContent.getString() == null) { 90 input = ""; 91 } else { 92 input = bodyContent.getString().trim(); 93 } 94 } 95 96 Security.addProvider(new BouncyCastleProvider()); 97 98 try { 99 PKCS10CertificationRequest pkcs10 = X509Cert.getPKCS10Request(input); 101 102 if (pkcs10.verify() == false) { 103 throw new JspException ("JCE Exception: Unable to retrieve info from PKCS#10 request: Not a valid PKCS10 request"); 104 } 105 106 CertificationRequestInfo reqinfo = pkcs10.getCertificationRequestInfo(); 107 X509Name x = reqinfo.getSubject(); 108 109 pageContext.setAttribute(subjectdn, x.toString(), scope); 110 111 } catch (Exception e) { 112 throw new JspException ("JCE Exception: Unable to retrieve info from PKCS#10 request: " 113 + e.getMessage(), e); 114 } 115 116 return EVAL_PAGE; 117 } 119 126 public void setCertrequest(String certrequest) { 127 this.certrequest = certrequest; 128 } 129 130 137 public void setScope(String scope) { 138 this.scope = getScope(scope); 139 } 140 141 148 public void setSubjectdn(String subjectdn) { 149 this.subjectdn = subjectdn; 150 } 151 152 public String getSubjectdn() { 153 return subjectdn; 154 } 155 } | Popular Tags |