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.PrivateKey ; 36 import java.security.Security ; 37 import java.security.cert.X509Certificate ; 38 39 66 67 public class Sign extends TagSupport { 68 private static final String PAGE = "page"; 69 private static final String REQUEST = "request"; 70 private static final String SESSION = "session"; 71 private static final String APPLICATION = "application"; 72 73 private String keypairalgorithm = "RSA"; private int keylength = 1024; private String signaturealgorithm = "MD5WithRSAEncryption"; 77 private String subjectdn; 78 private long validity = 365; 79 private boolean isca = false; 80 81 private String crldisturi; 82 83 private String netscapeextensions; 84 85 private String signfile; private String signentry; private StringBuffer signpassword; 89 private int scope = PageContext.PAGE_SCOPE; 91 private String serialnumber; 92 private String fingerprint; 93 94 96 private String storefile; private String storeentry; private StringBuffer storepassword; 101 private String certificate; 103 private String privatekey; 104 105 private StringBuffer pkey; 106 107 public static int getScope(String scope) { 108 int ret = PageContext.PAGE_SCOPE; 110 if (REQUEST.equalsIgnoreCase(scope)) 111 ret = PageContext.REQUEST_SCOPE; 112 else if (SESSION.equalsIgnoreCase(scope)) 113 ret = PageContext.SESSION_SCOPE; 114 else if (APPLICATION.equalsIgnoreCase(scope)) 115 ret = PageContext.APPLICATION_SCOPE; 116 else if (PAGE.equalsIgnoreCase(scope)) 117 ret = PageContext.PAGE_SCOPE; 118 119 return ret; 120 } 122 public int doEndTag() throws JspException { 123 124 Security.addProvider(new BouncyCastleProvider()); 126 127 try { 128 PrivateKey CAprivateKey = X509Cert.getPrivateFromP12(signfile, signentry, signpassword); 130 X509Certificate caCert = X509Cert.getCertificateFromP12(signfile, signentry, signpassword); 131 132 KeyPair p = X509Cert.generateKeyPair(keypairalgorithm, keylength, null); 134 135 X509Certificate cert = X509Cert.sign(p.getPublic() 137 , CAprivateKey 138 , caCert 139 , signaturealgorithm 140 , validity 141 , subjectdn 142 , isca 143 , crldisturi 144 , netscapeextensions); 145 146 147 if (storefile == null || storefile == "") { 148 byte output[] = cert.getEncoded(); 150 byte certB64[] = Base64.encode(output); 151 152 pageContext.setAttribute(certificate, "-----BEGIN CERTIFICATE-----\n" + new String (certB64) + "\n-----END CERTIFICATE-----", scope); 153 154 byte keyoutput[] = p.getPrivate().getEncoded(); 156 byte keyB64[] = Base64.encode(keyoutput); 157 158 pkey = new StringBuffer ("-----BEGIN PRIVATE KEY-----\n"); 159 pkey.append(new String (keyB64)); 160 pkey.append("\n-----END PRIVATE KEY-----"); 161 162 pageContext.setAttribute(privatekey, pkey, scope); 163 164 Clean.blank(keyoutput); 165 Clean.blank(keyB64); 166 167 } else { 168 KeyStore store = KeyStore.getInstance("PKCS12", "BC"); 170 171 store = KeyTools.createP12(storeentry, p.getPrivate(), cert, caCert); 172 store.store(new FileOutputStream (storefile), storepassword.toString().toCharArray()); 173 } 174 175 pageContext.setAttribute(serialnumber, cert.getSerialNumber().toString(), scope); 177 pageContext.setAttribute(fingerprint, CertTools.getFingerprintAsString(cert), scope); 178 } catch (Exception e) { 179 throw new JspException ("JCE Exception: Unable to generate certificate: " 180 + e.getMessage(), e); 181 } 182 183 return EVAL_PAGE; 184 } 186 public void release() { 187 Clean.blank(pkey); 189 Clean.blank(storepassword); 190 Clean.blank(signpassword); 191 192 super.release(); 193 } 195 196 203 public void setKeypairalgorithm(String keypairalgorithm) { 204 this.keypairalgorithm = keypairalgorithm; 205 } 206 207 214 public void setKeylength(int keylength) { 215 this.keylength = keylength; 216 } 217 218 225 public void setSignaturealgorithm(String signaturealgorithm) { 226 this.signaturealgorithm = signaturealgorithm; 227 } 228 229 236 public void setSubjectdn(String subjectdn) { 237 this.subjectdn = subjectdn; 238 } 239 240 247 public void setValidity(long validity) { 248 this.validity = validity; 249 } 250 251 258 public void setIsca(boolean isca) { 259 this.isca = isca; 260 } 261 262 269 public void setCrldisturi(String crldisturi) { 270 this.crldisturi = crldisturi; 271 } 272 273 284 public void setNetscapeextensions(String netscapeextensions) { 285 this.netscapeextensions = netscapeextensions; 286 } 287 288 295 public void setSignfile(String signfile) { 296 this.signfile = signfile; 297 } 298 299 306 public void setSignentry(String signentry) { 307 this.signentry = signentry; 308 } 309 310 317 public void setSignpassword(StringBuffer signpassword) { 318 this.signpassword = signpassword; 319 } 320 321 328 public void setScope(String scope) { 329 this.scope = getScope(scope); 330 } 331 332 339 public void setSerialnumber(String serialnumber) { 340 this.serialnumber = serialnumber; 341 } 342 343 public String getSerialnumber() { 344 return serialnumber; 345 } 346 347 354 public void setFingerprint(String fingerprint) { 355 this.fingerprint = fingerprint; 356 } 357 358 public String getFingerprint() { 359 return fingerprint; 360 } 361 362 369 public void setStorefile(String storefile) { 370 this.storefile = storefile; 371 } 372 373 380 public void setStoreentry(String storeentry) { 381 this.storeentry = storeentry; 382 } 383 384 391 public void setStorepassword(StringBuffer storepassword) { 392 this.storepassword = storepassword; 393 } 394 395 402 public void setCertificate(String certificate) { 403 this.certificate = certificate; 404 } 405 406 public String getCertificate() { 407 return certificate; 408 } 409 410 417 public void setPrivatekey(String privatekey) { 418 this.privatekey = privatekey; 419 } 420 421 public String getPrivatekey() { 422 return privatekey; 423 } 424 } | Popular Tags |