1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.security.util;
18
19 import java.io.IOException;
20 import java.net.InetAddress;
21 import java.net.Socket;
22 import java.net.UnknownHostException;
23 import java.security.KeyManagementException;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.SecureRandom;
26 import java.security.cert.CertificateException;
27 import java.security.cert.X509Certificate;
28 import javax.net.ssl.X509TrustManager;
29 import javax.net.SocketFactory;
30 import javax.net.ssl.SSLContext;
31 import javax.net.ssl.SSLSocketFactory;
32 import javax.net.ssl.TrustManager;
33
34 /***
35 * Socket Factory for SSL connections which do not provide an authentication
36 * This is used to connect to servers where we are just interested in
37 * an encypted tunnel, and not to verify that both parties trust each other.
38 *
39 * @author <a href="mailto:b.vanhalderen@hippo.nl">Berry van Halderen</a>
40 * @version $Id: GullibleSSLSocketFactory.java 516448 2007-03-09 16:25:47Z ate $
41 *
42 */
43 public class GullibleSSLSocketFactory extends SSLSocketFactory {
44
45 class GullibleTrustManager implements X509TrustManager
46 {
47 GullibleTrustManager() { }
48 public void checkClientTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
49 }
50
51 public void checkServerTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
52 }
53
54 public X509Certificate[] getAcceptedIssuers() {
55 return new X509Certificate[0];
56 }
57 }
58
59 private SSLSocketFactory factory;
60 protected GullibleSSLSocketFactory() {
61 try {
62 SSLContext context = SSLContext.getInstance("TLS");
63 context.init(null, new TrustManager[] {new GullibleTrustManager()},
64 new SecureRandom());
65 factory = context.getSocketFactory();
66 } catch (NoSuchAlgorithmException e) {
67 e.printStackTrace();
68 } catch (KeyManagementException e) {
69 e.printStackTrace();
70 }
71 }
72 public static SocketFactory getDefault() {
73 return new GullibleSSLSocketFactory();
74 }
75 public String[] getDefaultCipherSuites() {
76 return factory.getDefaultCipherSuites();
77 }
78 public String[] getSupportedCipherSuites() {
79 return factory.getSupportedCipherSuites();
80 }
81 public Socket createSocket(final Socket s, final String host, final int port, final boolean autoClose) throws IOException {
82 return factory.createSocket(s, host, port, autoClose);
83 }
84 public Socket createSocket(final String host, final int port) throws IOException, UnknownHostException {
85 return factory.createSocket(host, port);
86 }
87 public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort) throws IOException, UnknownHostException {
88 return factory.createSocket(host, port, localAddress, localPort);
89 }
90 public Socket createSocket(final InetAddress host, final int port) throws IOException {
91 return factory.createSocket(host, port);
92 }
93 public Socket createSocket(final InetAddress address, final int port, final InetAddress localAddress, final int localPort) throws IOException {
94 return factory.createSocket(address, port, localAddress, localPort);
95 }
96 }