1 19 20 package com.maverick.ssl; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.text.MessageFormat ; 27 import java.util.Hashtable ; 28 29 import com.maverick.crypto.asn1.ASN1Sequence; 30 import com.maverick.crypto.asn1.DERInputStream; 31 import com.maverick.crypto.asn1.x509.CertificateException; 32 import com.maverick.crypto.asn1.x509.X509Certificate; 33 import com.maverick.crypto.asn1.x509.X509CertificateStructure; 34 35 39 public class CertificateStore { 40 41 org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(CertificateStore.class); 43 45 Hashtable certificates = new Hashtable (); 46 static CertificateStore instance; 47 48 public CertificateStore() throws IOException { 49 50 addTrustedCACertificate("/gtecybertrustca.cert"); addTrustedCACertificate("/baltimorecodesigningca.cert"); addTrustedCACertificate("/baltimorecybertrustca.cert"); addTrustedCACertificate("/entrust2048ca.cert"); addTrustedCACertificate("/entrustclientca.cert"); addTrustedCACertificate("/entrustglobalclientca.cert"); addTrustedCACertificate("/entrustserverca.cert"); addTrustedCACertificate("/entrustgsslca.cert"); addTrustedCACertificate("/equifaxsecureca.cert"); addTrustedCACertificate("/equifaxsecureebusinessca1.cert"); addTrustedCACertificate("/equifaxsecureebusinessca2.cert"); addTrustedCACertificate("/equifaxsecureglobalebusinessca1.cert"); addTrustedCACertificate("/geotrustglobalca.cert"); addTrustedCACertificate("/gtecybertrustglobalca.cert"); addTrustedCACertificate("/gtecybertrust5ca.cert"); addTrustedCACertificate("/thawtepersonalbasicca.cert"); addTrustedCACertificate("/thawtepersonalfreemailca.cert"); addTrustedCACertificate("/thawtepersonalpremiumca.cert"); addTrustedCACertificate("/thawtepremiumserverca.cert"); addTrustedCACertificate("/thawteserverca.cert"); addTrustedCACertificate("/verisignclass1ca.cert"); addTrustedCACertificate("/verisignclass2ca.cert"); addTrustedCACertificate("/verisignclass3ca.cert"); addTrustedCACertificate("/verisignclass4ca.cert"); addTrustedCACertificate("/verisignserverca.cert"); addTrustedCACertificate("/AddTrustUTNServerCA.cert"); 78 } 79 80 public static CertificateStore getInstance() throws IOException { 81 return instance == null ? instance = new CertificateStore() : instance; 82 } 83 84 public boolean contains(String dn) throws CertificateException { 85 return certificates.containsKey(dn); 86 } 87 88 public X509Certificate get(String sig) { 89 return (X509Certificate) certificates.get(sig); 90 } 91 92 public void addTrustedCACertificate(InputStream in) { 93 94 DERInputStream der = null; 95 try { 96 97 der = new DERInputStream(in); 98 99 ASN1Sequence certificate = (ASN1Sequence) der.readObject(); 100 101 X509Certificate x509 = new X509Certificate(X509CertificateStructure.getInstance(certificate)); 102 103 if (certificates.containsKey(x509.getSubjectDN().toString())) { 104 if (log.isDebugEnabled()) 106 log.debug(Messages.getString("CertificateStore.alreadyExists") + x509.getSubjectDN().toString()); } else { 109 if (log.isDebugEnabled()) 111 log.debug(MessageFormat.format(Messages.getString("CertificateStore.addingTrustedCA"), new Object [] { x509.getSubjectDN().toString() })); certificates.put(x509.getSubjectDN().toString(), x509); 114 } 115 } catch (Exception ex) { 116 ex.printStackTrace(); 117 } finally { 118 try { 119 if (in != null) { 120 in.close(); 121 } 122 } catch (IOException ex) { 123 } 124 try { 125 if (der != null) { 126 der.close(); 127 } 128 } catch (IOException ex) { 129 } 130 } 131 } 132 133 public void addTrustedCACertificate(File certificateFile) throws IOException { 134 InputStream in = new FileInputStream (certificateFile); 135 addTrustedCACertificate(in); 136 } 137 138 public void addTrustedCACertificate(String resource) throws IOException { 139 InputStream in = TrustedCACertStore.class.getResourceAsStream(resource); 140 if (in == null) { 141 throw new IOException (MessageFormat.format(Messages.getString("CertificateStore.couldNotLocateTrustedCAResource"), new Object [] { resource })); } 143 addTrustedCACertificate(in); 144 } 145 } 146 | Popular Tags |