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.KeyTools; 26 import org.bouncycastle.jce.provider.BouncyCastleProvider; 27 import org.bouncycastle.util.encoders.Base64; 28 29 import javax.servlet.jsp.JspException ; 30 import javax.servlet.jsp.PageContext ; 31 import javax.servlet.jsp.tagext.TagSupport ; 32 import java.io.FileOutputStream ; 33 import java.security.KeyPair ; 34 import java.security.KeyStore ; 35 import java.security.Security ; 36 import java.security.cert.X509Certificate ; 37 38 67 68 public class SelfSign extends TagSupport { 69 private static final String PAGE = "page"; 70 private static final String REQUEST = "request"; 71 private static final String SESSION = "session"; 72 private static final String APPLICATION = "application"; 73 74 private String keypairalgorithm = "RSA"; private int keylength = 1024; private String signaturealgorithm = "MD5WithRSAEncryption"; 78 private String subjectdn; 79 private long validity = 365; 80 private boolean isca = true; 81 82 private String netscapeextensions; 83 84 private int scope = PageContext.PAGE_SCOPE; 86 private String serialnumber; 87 private String fingerprint; 88 89 91 private String storefile; private String storeentry; private StringBuffer storepassword; 96 private String certificate; 98 private String privatekey; 99 100 private StringBuffer pkey; 101 102 public static int getScope(String scope) { 103 int ret = PageContext.PAGE_SCOPE; 105 if (REQUEST.equalsIgnoreCase(scope)) 106 ret = PageContext.REQUEST_SCOPE; 107 else if (SESSION.equalsIgnoreCase(scope)) 108 ret = PageContext.SESSION_SCOPE; 109 else if (APPLICATION.equalsIgnoreCase(scope)) 110 ret = PageContext.APPLICATION_SCOPE; 111 else if (PAGE.equalsIgnoreCase(scope)) 112 ret = PageContext.PAGE_SCOPE; 113 114 return ret; 115 } 117 public int doEndTag() throws JspException { 118 119 Security.addProvider(new BouncyCastleProvider()); 121 122 try { 123 KeyPair p = X509Cert.generateKeyPair(keypairalgorithm, keylength, null); 125 126 X509Certificate cert = X509Cert.selfsign(p.getPrivate() 128 , p.getPublic() 129 , signaturealgorithm 130 , validity 131 , subjectdn 132 , isca 133 , netscapeextensions); 134 135 if (storefile == null || storefile == "") { 136 byte output[] = cert.getEncoded(); 138 byte certB64[] = Base64.encode(output); 139 140 pageContext.setAttribute(certificate, "-----BEGIN CERTIFICATE-----\n" + new String (certB64) + "\n-----END CERTIFICATE-----", scope); 141 142 byte keyoutput[] = p.getPrivate().getEncoded(); 144 byte keyB64[] = Base64.encode(keyoutput); 145 146 pkey = new StringBuffer ("-----BEGIN PRIVATE KEY-----\n"); 147 pkey.append(new String (keyB64)); 148 pkey.append("\n-----END PRIVATE KEY-----"); 149 150 pageContext.setAttribute(privatekey, pkey, scope); 151 152 Clean.blank(keyoutput); 153 Clean.blank(keyB64); 154 155 } else { 156 KeyStore store = KeyStore.getInstance("PKCS12", "BC"); 158 159 store = KeyTools.createP12(storeentry, p.getPrivate(), cert, cert); 160 store.store(new FileOutputStream (storefile), storepassword.toString().toCharArray()); 161 } 162 163 pageContext.setAttribute(serialnumber, cert.getSerialNumber().toString(), scope); 165 pageContext.setAttribute(fingerprint, CertTools.getFingerprintAsString(cert), scope); 166 } catch (Exception e) { 167 throw new JspException ("JCE Exception: Unable to generate certificate: " 168 + e.getMessage(), e); 169 } 170 171 return EVAL_PAGE; 172 } 174 public void release() { 175 Clean.blank(pkey); 177 Clean.blank(storepassword); 178 179 super.release(); 180 } 182 189 public void setKeypairalgorithm(String keypairalgorithm) { 190 this.keypairalgorithm = keypairalgorithm; 191 } 192 193 200 public void setKeylength(int keylength) { 201 this.keylength = keylength; 202 } 203 204 211 public void setSignaturealgorithm(String signaturealgorithm) { 212 this.signaturealgorithm = signaturealgorithm; 213 } 214 215 222 public void setSubjectdn(String subjectdn) { 223 this.subjectdn = subjectdn; 224 } 225 226 233 public void setValidity(long validity) { 234 this.validity = validity; 235 } 236 237 244 public void setIsca(boolean isca) { 245 this.isca = isca; 246 } 247 248 259 public void setNetscapeextensions(String netscapeextensions) { 260 this.netscapeextensions = netscapeextensions; 261 } 262 263 270 public void setScope(String scope) { 271 this.scope = getScope(scope); 272 } 273 274 281 public void setSerialnumber(String serialnumber) { 282 this.serialnumber = serialnumber; 283 } 284 285 public String getSerialnumber() { 286 return serialnumber; 287 } 288 289 296 public void setFingerprint(String fingerprint) { 297 this.fingerprint = fingerprint; 298 } 299 300 public String getFingerprint() { 301 return fingerprint; 302 } 303 304 311 public void setStorefile(String storefile) { 312 this.storefile = storefile; 313 } 314 315 322 public void setStoreentry(String storeentry) { 323 this.storeentry = storeentry; 324 } 325 326 333 public void setStorepassword(StringBuffer storepassword) { 334 this.storepassword = storepassword; 335 } 336 337 344 public void setCertificate(String certificate) { 345 this.certificate = certificate; 346 } 347 348 public String getCertificate() { 349 return certificate; 350 } 351 352 359 public void setPrivatekey(String privatekey) { 360 this.privatekey = privatekey; 361 } 362 363 public String getPrivatekey() { 364 return privatekey; 365 } 366 } | Popular Tags |