KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > ssl > CertificateStore


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.ssl;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.Hashtable JavaDoc;
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 /**
36  *
37  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
38  */

39 public class CertificateStore {
40
41     // #ifdef DEBUG
42
org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(CertificateStore.class);
43     // #endif
44

45     Hashtable JavaDoc certificates = new Hashtable JavaDoc();
46     static CertificateStore instance;
47
48     public CertificateStore() throws IOException JavaDoc {
49
50         addTrustedCACertificate("/gtecybertrustca.cert"); //$NON-NLS-1$
51
addTrustedCACertificate("/baltimorecodesigningca.cert"); //$NON-NLS-1$
52
addTrustedCACertificate("/baltimorecybertrustca.cert"); //$NON-NLS-1$
53
addTrustedCACertificate("/entrust2048ca.cert"); //$NON-NLS-1$
54
addTrustedCACertificate("/entrustclientca.cert"); //$NON-NLS-1$
55
addTrustedCACertificate("/entrustglobalclientca.cert"); //$NON-NLS-1$
56
addTrustedCACertificate("/entrustserverca.cert"); //$NON-NLS-1$
57
addTrustedCACertificate("/entrustgsslca.cert"); //$NON-NLS-1$
58
addTrustedCACertificate("/equifaxsecureca.cert"); //$NON-NLS-1$
59
addTrustedCACertificate("/equifaxsecureebusinessca1.cert"); //$NON-NLS-1$
60
addTrustedCACertificate("/equifaxsecureebusinessca2.cert"); //$NON-NLS-1$
61
addTrustedCACertificate("/equifaxsecureglobalebusinessca1.cert"); //$NON-NLS-1$
62
addTrustedCACertificate("/geotrustglobalca.cert"); //$NON-NLS-1$
63
addTrustedCACertificate("/gtecybertrustglobalca.cert"); //$NON-NLS-1$
64
addTrustedCACertificate("/gtecybertrust5ca.cert"); //$NON-NLS-1$
65
addTrustedCACertificate("/thawtepersonalbasicca.cert"); //$NON-NLS-1$
66
addTrustedCACertificate("/thawtepersonalfreemailca.cert"); //$NON-NLS-1$
67
addTrustedCACertificate("/thawtepersonalpremiumca.cert"); //$NON-NLS-1$
68
addTrustedCACertificate("/thawtepremiumserverca.cert"); //$NON-NLS-1$
69
addTrustedCACertificate("/thawteserverca.cert"); //$NON-NLS-1$
70
addTrustedCACertificate("/verisignclass1ca.cert"); //$NON-NLS-1$
71
addTrustedCACertificate("/verisignclass2ca.cert"); //$NON-NLS-1$
72
addTrustedCACertificate("/verisignclass3ca.cert"); //$NON-NLS-1$
73
addTrustedCACertificate("/verisignclass4ca.cert"); //$NON-NLS-1$
74
addTrustedCACertificate("/verisignserverca.cert"); //$NON-NLS-1$
75
// addTrustedCACertificate("/UTN-USERFirst-Hardware.cert");
76
addTrustedCACertificate("/AddTrustUTNServerCA.cert"); //$NON-NLS-1$
77

78     }
79
80     public static CertificateStore getInstance() throws IOException JavaDoc {
81         return instance == null ? instance = new CertificateStore() : instance;
82     }
83
84     public boolean contains(String JavaDoc dn) throws CertificateException {
85         return certificates.containsKey(dn);
86     }
87
88     public X509Certificate get(String JavaDoc sig) {
89         return (X509Certificate) certificates.get(sig);
90     }
91
92     public void addTrustedCACertificate(InputStream JavaDoc 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                 // #ifdef DEBUG
105
if (log.isDebugEnabled())
106                     log.debug(Messages.getString("CertificateStore.alreadyExists") + x509.getSubjectDN().toString()); //$NON-NLS-1$
107
// #endif
108
} else {
109                 // #ifdef DEBUG
110
if (log.isDebugEnabled())
111                     log.debug(MessageFormat.format(Messages.getString("CertificateStore.addingTrustedCA"), new Object JavaDoc[] { x509.getSubjectDN().toString() })); //$NON-NLS-1$
112
// #endif
113
certificates.put(x509.getSubjectDN().toString(), x509);
114             }
115         } catch (Exception JavaDoc ex) {
116             ex.printStackTrace();
117         } finally {
118             try {
119                 if (in != null) {
120                     in.close();
121                 }
122             } catch (IOException JavaDoc ex) {
123             }
124             try {
125                 if (der != null) {
126                     der.close();
127                 }
128             } catch (IOException JavaDoc ex) {
129             }
130         }
131     }
132
133     public void addTrustedCACertificate(File JavaDoc certificateFile) throws IOException JavaDoc {
134         InputStream JavaDoc in = new FileInputStream JavaDoc(certificateFile);
135         addTrustedCACertificate(in);
136     }
137
138     public void addTrustedCACertificate(String JavaDoc resource) throws IOException JavaDoc {
139         InputStream JavaDoc in = TrustedCACertStore.class.getResourceAsStream(resource);
140         if (in == null) {
141             throw new IOException JavaDoc(MessageFormat.format(Messages.getString("CertificateStore.couldNotLocateTrustedCAResource"), new Object JavaDoc[] { resource }));//$NON-NLS-1$
142
}
143         addTrustedCACertificate(in);
144     }
145 }
146
Popular Tags