KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Random JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import com.maverick.crypto.asn1.ASN1Sequence;
30 import com.maverick.crypto.asn1.DERInputStream;
31 import com.maverick.crypto.asn1.x509.X509Certificate;
32 import com.maverick.crypto.asn1.x509.X509CertificateStructure;
33 import com.maverick.crypto.security.SecureRandom;
34
35 /**
36  * A context for an SSL connection.
37  *
38  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
39  */

40 public class SSLContext {
41
42     Hashtable JavaDoc cipherClassesByID = new Hashtable JavaDoc();
43     Hashtable JavaDoc cipherIDsByName = new Hashtable JavaDoc();
44     Vector JavaDoc cipherIDs = new Vector JavaDoc();
45     SecureRandom rnd = SecureRandom.getInstance();
46
47     boolean allowUntrustedCertificates = false;
48     boolean allowInvalidCertificates = false;
49
50     TrustedCACertStore cacerts;
51
52     public SSLContext() throws IOException JavaDoc {
53
54         cacerts = new TrustedCACertStore();
55
56         addCipherSuite(0x00, 0x04, "SSL_RSA_WITH_RC4_128_MD5", //$NON-NLS-1$
57
SSL_RSA_WITH_RC4_128_MD5.class);
58
59         try {
60             allowUntrustedCertificates = Boolean.valueOf(System.getProperty("com.maverick.ssl.allowUntrustedCertificates", "false")).booleanValue(); //$NON-NLS-1$ //$NON-NLS-2$
61
} catch (Exception JavaDoc ex) {
62         }
63
64         try {
65             allowInvalidCertificates = Boolean.valueOf(System.getProperty("com.maverick.ssl.allowInvalidCertificates", "false")).booleanValue(); //$NON-NLS-1$ //$NON-NLS-2$
66
} catch (Exception JavaDoc ex1) {
67         }
68
69     }
70
71     public void addCipherSuite(int id1, int id2, String JavaDoc name, Class JavaDoc suite) {
72
73         SSLCipherSuiteID id = new SSLCipherSuiteID(id1, id2);
74         cipherClassesByID.put(id, suite);
75         cipherIDsByName.put(name, id);
76         cipherIDs.addElement(id);
77     }
78
79     public TrustedCACertStore getTrustedCACerts() {
80         return cacerts;
81     }
82
83     public Class JavaDoc getCipherSuiteClass(SSLCipherSuiteID id) {
84
85         Enumeration JavaDoc e = cipherClassesByID.keys();
86         while (e.hasMoreElements()) {
87             SSLCipherSuiteID i = (SSLCipherSuiteID) e.nextElement();
88             if (i.equals(id)) {
89                 return (Class JavaDoc) cipherClassesByID.get(i);
90             }
91         }
92
93         return null;
94     }
95
96     public SSLCipherSuiteID[] getCipherSuiteIDs() {
97         SSLCipherSuiteID[] ids = new SSLCipherSuiteID[cipherIDs.size()];
98         cipherIDs.copyInto(ids);
99         return ids;
100     }
101
102     public Random JavaDoc getRND() {
103         return rnd;
104     }
105
106     public boolean isUntrustedCertificateAllowed() {
107         return allowUntrustedCertificates;
108     }
109
110     public boolean isInvalidCertificateAllowed() {
111         return allowInvalidCertificates;
112     }
113
114     public static void main(String JavaDoc[] args) {
115
116         try {
117             SSLContext ssl = new SSLContext();
118             // Now read the certificate
119
DERInputStream der = new DERInputStream(new FileInputStream JavaDoc("c:/exported.cer")); //$NON-NLS-1$
120

121             ASN1Sequence certificate = (ASN1Sequence) der.readObject();
122
123             // Get the x509 certificate structure
124
X509Certificate x509 = new X509Certificate(X509CertificateStructure.getInstance(certificate));
125
126             System.out.println(x509.getIssuerDN());
127             System.out.println(x509.getSubjectDN());
128             ssl.getTrustedCACerts().isTrustedCertificate(x509, true, true);
129         } catch (Exception JavaDoc ex) {
130             ex.printStackTrace();
131         }
132
133     }
134
135 }
136
Popular Tags