1 19 20 package net.sourceforge.jcetaglib.taglib.x509; 21 22 import net.sourceforge.jcetaglib.lib.Clean; 23 import net.sourceforge.jcetaglib.lib.X509Cert; 24 import org.bouncycastle.jce.provider.BouncyCastleProvider; 25 import org.bouncycastle.util.encoders.Base64; 26 27 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.tagext.TagSupport ; 30 import java.io.FileInputStream ; 31 import java.security.KeyStore ; 32 import java.security.PrivateKey ; 33 import java.security.Security ; 34 import java.security.cert.X509Certificate ; 35 36 57 58 public class ReadP12 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 String storefile; private String storeentry; private StringBuffer storepassword; 68 private boolean returnprivatekey = false; 69 70 private int scope = PageContext.PAGE_SCOPE; 71 72 private String certificate; 73 private String cacertificate; 74 private String privatekey; 75 76 private StringBuffer pkey; 77 78 public static int getScope(String scope) { 79 int ret = PageContext.PAGE_SCOPE; 81 if (REQUEST.equalsIgnoreCase(scope)) 82 ret = PageContext.REQUEST_SCOPE; 83 else if (SESSION.equalsIgnoreCase(scope)) 84 ret = PageContext.SESSION_SCOPE; 85 else if (APPLICATION.equalsIgnoreCase(scope)) 86 ret = PageContext.APPLICATION_SCOPE; 87 else if (PAGE.equalsIgnoreCase(scope)) 88 ret = PageContext.PAGE_SCOPE; 89 90 return ret; 91 } 93 public int doEndTag() throws JspException { 94 Security.addProvider(new BouncyCastleProvider()); 95 96 X509Certificate cert = null; 97 X509Certificate CAcert = null; 98 PrivateKey privKey = null; 99 100 try { 101 KeyStore store = KeyStore.getInstance("PKCS12", "BC"); 103 store.load(new FileInputStream (storefile), storepassword.toString().toCharArray()); 104 105 if (returnprivatekey) { 106 privKey = X509Cert.getPrivateFromP12(storefile, storeentry, storepassword); 107 } 108 109 cert = X509Cert.getCertificateFromP12(storefile, storeentry, storepassword); 110 CAcert = X509Cert.getCACertificateFromP12(storefile, storeentry, storepassword); 111 112 byte output[] = cert.getEncoded(); 114 byte certB64[] = Base64.encode(output); 115 116 pageContext.setAttribute(certificate, "-----BEGIN CERTIFICATE-----\n" + new String (certB64) + "\n-----END CERTIFICATE-----", scope); 117 118 byte CAoutput[] = CAcert.getEncoded(); 120 byte CAcertB64[] = Base64.encode(CAoutput); 121 122 pageContext.setAttribute(cacertificate, "-----BEGIN CERTIFICATE-----\n" + new String (CAcertB64) + "\n-----END CERTIFICATE-----", scope); 123 124 if (returnprivatekey) { 126 byte keyoutput[] = privKey.getEncoded(); 127 byte keyB64[] = Base64.encode(keyoutput); 128 129 pkey = new StringBuffer ("-----BEGIN PRIVATE KEY-----\n"); 130 pkey.append(new String (keyB64)); 131 pkey.append("\n-----END PRIVATE KEY-----"); 132 133 pageContext.setAttribute(privatekey, pkey, scope); 134 135 Clean.blank(keyoutput); 136 Clean.blank(keyB64); 137 privKey = null; 138 } 139 } catch (Exception e) { 140 throw new JspException ("JCE Exception: Unable to read keystore \"" + storefile + "\": " 141 + e.getMessage(), e); 142 } 143 144 return EVAL_PAGE; 145 } 147 public void release() { 148 Clean.blank(pkey); 150 151 super.release(); 152 } 154 161 public void setStorefile(String storefile) { 162 this.storefile = storefile; 163 } 164 165 172 public void setStoreentry(String storeentry) { 173 this.storeentry = storeentry; 174 } 175 176 183 public void setStorepassword(StringBuffer storepassword) { 184 this.storepassword = storepassword; 185 } 186 187 194 public void setScope(String scope) { 195 this.scope = getScope(scope); 196 } 197 198 205 public void setReturnprivatekey(boolean returnprivatekey) { 206 this.returnprivatekey = returnprivatekey; 207 } 208 209 216 public void setCertificate(String certificate) { 217 this.certificate = certificate; 218 } 219 220 public String getCertificate() { 221 return certificate; 222 } 223 224 231 public void setCacertificate(String cacertificate) { 232 this.cacertificate = cacertificate; 233 } 234 235 public String getCacertificate() { 236 return cacertificate; 237 } 238 239 246 public void setPrivatekey(String privatekey) { 247 this.privatekey = privatekey; 248 } 249 250 public String getPrivatekey() { 251 return privatekey; 252 } 253 } | Popular Tags |