1 19 20 package net.sourceforge.jcetaglib.taglib.x509; 21 22 import net.sourceforge.jcetaglib.lib.Clean; 23 import org.bouncycastle.jce.PKCS10CertificationRequest; 24 import org.bouncycastle.jce.X509Principal; 25 import org.bouncycastle.jce.provider.BouncyCastleProvider; 26 import org.bouncycastle.util.encoders.Base64; 27 28 import javax.servlet.jsp.JspException ; 29 import javax.servlet.jsp.PageContext ; 30 import javax.servlet.jsp.tagext.TagSupport ; 31 import java.security.*; 32 33 52 53 public class CreatePKCS10 extends TagSupport { 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 private String subjectdn; 60 61 private String keypairalgorithm = "RSA"; private int keylength = 1024; private String signaturealgorithm = "MD5WithRSAEncryption"; 65 private int scope = PageContext.PAGE_SCOPE; 66 67 private String request; 70 private String privatekey; 71 72 private StringBuffer pkey; 73 74 public static int getScope(String scope) { 75 int ret = PageContext.PAGE_SCOPE; 77 if (REQUEST.equalsIgnoreCase(scope)) 78 ret = PageContext.REQUEST_SCOPE; 79 else if (SESSION.equalsIgnoreCase(scope)) 80 ret = PageContext.SESSION_SCOPE; 81 else if (APPLICATION.equalsIgnoreCase(scope)) 82 ret = PageContext.APPLICATION_SCOPE; 83 else if (PAGE.equalsIgnoreCase(scope)) 84 ret = PageContext.PAGE_SCOPE; 85 86 return ret; 87 } 89 public int doEndTag() throws JspException { 90 Security.addProvider(new BouncyCastleProvider()); 91 92 PrivateKey privKey; 96 PublicKey pubKey; 97 98 try { 99 KeyPairGenerator g = KeyPairGenerator.getInstance(keypairalgorithm, "BC"); 100 101 g.initialize(keylength, new SecureRandom()); 102 KeyPair p = g.generateKeyPair(); 103 104 privKey = p.getPrivate(); 105 pubKey = p.getPublic(); 106 107 PKCS10CertificationRequest req = new PKCS10CertificationRequest(signaturealgorithm, 108 new X509Principal(subjectdn), 109 pubKey, 110 null, 111 privKey); 112 113 byte output[] = req.getEncoded(); 115 byte reqB64[] = Base64.encode(output); 116 117 pageContext.setAttribute(request, "-----BEGIN CERTIFICATE REQUEST-----\n" + new String (reqB64) + "\n-----END CERTIFICATE REQUEST-----", scope); 118 119 byte keyoutput[] = privKey.getEncoded(); 121 byte keyB64[] = Base64.encode(keyoutput); 122 123 pkey = new StringBuffer ("-----BEGIN PRIVATE KEY-----\n"); 124 pkey.append(new String (keyB64)); 125 pkey.append("\n-----END PRIVATE KEY-----"); 126 127 pageContext.setAttribute(privatekey, pkey, scope); 128 129 Clean.blank(keyoutput); 130 Clean.blank(keyB64); 131 privKey = null; 132 133 } catch (Exception e) { 134 throw new JspException ("JCE Exception: Unable to generate PKCS#10 request: " 135 + e.getMessage(), e); 136 } 137 138 return EVAL_PAGE; 139 } 141 public void release() { 142 Clean.blank(pkey); 144 145 super.release(); 146 } 148 155 public void setKeypairalgorithm(String keypairalgorithm) { 156 this.keypairalgorithm = keypairalgorithm; 157 } 158 159 166 public void setKeylength(int keylength) { 167 this.keylength = keylength; 168 } 169 170 177 public void setSignaturealgorithm(String signaturealgorithm) { 178 this.signaturealgorithm = signaturealgorithm; 179 } 180 181 188 public void setSubjectdn(String subjectdn) { 189 this.subjectdn = subjectdn; 190 } 191 192 199 public void setScope(String scope) { 200 this.scope = getScope(scope); 201 } 202 203 210 public void setRequest(String request) { 211 this.request = request; 212 } 213 214 public String getRequest() { 215 return request; 216 } 217 218 225 public void setPrivatekey(String privatekey) { 226 this.privatekey = privatekey; 227 } 228 229 public String getPrivatekey() { 230 return privatekey; 231 } 232 } | Popular Tags |