KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > SimpleSSLSocketFactory


1 /**
2  * $RCSfile: SimpleSSLSocketFactory.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2005/07/26 17:10:33 $
5  *
6  * Copyright (C) 2004-2005 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.util;
13
14 import javax.net.ssl.SSLSocketFactory;
15 import javax.net.ssl.SSLContext;
16 import javax.net.ssl.TrustManager;
17 import javax.net.ssl.X509TrustManager;
18 import javax.net.SocketFactory;
19 import java.security.NoSuchAlgorithmException JavaDoc;
20 import java.security.KeyManagementException JavaDoc;
21 import java.security.cert.CertificateException JavaDoc;
22 import java.security.cert.X509Certificate JavaDoc;
23 import java.security.cert.CertificateExpiredException JavaDoc;
24 import java.security.cert.CertificateNotYetValidException JavaDoc;
25 import java.net.Socket JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 /**
30  * SSLSocketFactory that accepts any certificate chain and also accepts expired
31  * certificates.
32  *
33  * @author Matt Tucker
34  */

35 public class SimpleSSLSocketFactory extends SSLSocketFactory {
36
37     private SSLSocketFactory factory;
38
39     public SimpleSSLSocketFactory() {
40
41         try {
42             SSLContext sslcontent = SSLContext.getInstance("TLS");
43             sslcontent.init(null, // KeyManager not required
44
new TrustManager[] { new DummyTrustManager() },
45                             new java.security.SecureRandom JavaDoc());
46             factory = sslcontent.getSocketFactory();
47         }
48         catch (NoSuchAlgorithmException JavaDoc e) {
49             Log.error(e);
50         }
51         catch (KeyManagementException JavaDoc e) {
52             Log.error(e);
53         }
54     }
55
56     public static SocketFactory getDefault() {
57         return new SimpleSSLSocketFactory();
58     }
59
60     public Socket JavaDoc createSocket(Socket JavaDoc socket, String JavaDoc s, int i, boolean flag)
61             throws IOException JavaDoc
62     {
63         return factory.createSocket(socket, s, i, flag);
64     }
65
66     public Socket JavaDoc createSocket(InetAddress JavaDoc inaddr, int i, InetAddress JavaDoc inaddr2, int j)
67             throws IOException JavaDoc
68     {
69         return factory.createSocket(inaddr, i, inaddr2, j);
70     }
71
72     public Socket JavaDoc createSocket(InetAddress JavaDoc inaddr, int i)
73             throws IOException JavaDoc
74     {
75         return factory.createSocket(inaddr, i);
76     }
77
78     public Socket JavaDoc createSocket(String JavaDoc s, int i, InetAddress JavaDoc inaddr, int j)
79             throws IOException JavaDoc
80     {
81         return factory.createSocket(s, i, inaddr, j);
82     }
83
84     public Socket JavaDoc createSocket(String JavaDoc s, int i)
85             throws IOException JavaDoc
86     {
87         return factory.createSocket(s, i);
88     }
89
90     public String JavaDoc[] getDefaultCipherSuites() {
91         return factory.getSupportedCipherSuites();
92     }
93
94     public String JavaDoc[] getSupportedCipherSuites() {
95         return factory.getSupportedCipherSuites();
96     }
97
98     private static class DummyTrustManager implements X509TrustManager {
99
100         public boolean isClientTrusted(X509Certificate JavaDoc[] cert) {
101             return true;
102         }
103
104         public boolean isServerTrusted(X509Certificate JavaDoc[] cert) {
105             try {
106                 cert[0].checkValidity();
107                 return true;
108             }
109             catch (CertificateExpiredException JavaDoc e) {
110                 return false;
111             }
112             catch (CertificateNotYetValidException JavaDoc e) {
113                 return false;
114             }
115         }
116
117         public void checkClientTrusted(java.security.cert.X509Certificate JavaDoc[] x509Certificates,
118                 String JavaDoc s) throws CertificateException JavaDoc
119         {
120         }
121
122         public void checkServerTrusted(java.security.cert.X509Certificate JavaDoc[] x509Certificates,
123                 String JavaDoc s) throws CertificateException JavaDoc
124         {
125         }
126
127         public X509Certificate JavaDoc[] getAcceptedIssuers() {
128             return new X509Certificate JavaDoc[0];
129         }
130     }
131 }
Popular Tags