KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > net > SecureSocket


1 package rero.net;
2
3 import java.net.*;
4 import java.io.*;
5 import javax.net.ssl.*;
6 import javax.net.*;
7
8 import java.security.*;
9 import java.security.cert.*;
10
11 public class SecureSocket
12 {
13     protected SSLSocket socket;
14
15     public SecureSocket(String JavaDoc host, int port) throws Exception JavaDoc
16     {
17        socket = null;
18
19        DummySSLSocketFactory factory = new DummySSLSocketFactory();
20        socket = (SSLSocket)factory.createSocket(host, port);
21        socket.startHandshake();
22     }
23
24     public Socket getSocket()
25     {
26        return socket;
27     }
28
29     private static class DummySSLSocketFactory extends SSLSocketFactory
30     {
31        private SSLSocketFactory factory;
32
33        public DummySSLSocketFactory()
34        {
35           try
36           {
37               SSLContext sslcontext = SSLContext.getInstance("SSL");
38               sslcontext.init(null, new TrustManager[] {new DummyTrustManager()}, new java.security.SecureRandom JavaDoc());
39               factory = (SSLSocketFactory) sslcontext.getSocketFactory();
40           }
41           catch(Exception JavaDoc ex)
42           {
43               ex.printStackTrace();
44           }
45        }
46
47        public static SocketFactory getDefault()
48        {
49           return new DummySSLSocketFactory();
50        }
51
52        public Socket createSocket(Socket socket, String JavaDoc s, int i, boolean flag) throws IOException
53        {
54           return factory.createSocket(socket, s, i, flag);
55        }
56
57        public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr1, int j) throws IOException
58        {
59           return factory.createSocket(inaddr, i, inaddr1, j);
60        }
61
62        public Socket createSocket(InetAddress inaddr, int i) throws IOException
63        {
64           return factory.createSocket(inaddr, i);
65        }
66
67        public Socket createSocket(String JavaDoc s, int i, InetAddress inaddr, int j) throws IOException
68        {
69           return factory.createSocket(s, i, inaddr, j);
70        }
71
72        public Socket createSocket(String JavaDoc s, int i) throws IOException
73        {
74           return factory.createSocket(s, i);
75        }
76
77        public String JavaDoc[] getDefaultCipherSuites()
78        {
79           return factory.getSupportedCipherSuites();
80        }
81
82        public String JavaDoc[] getSupportedCipherSuites()
83        {
84           return factory.getSupportedCipherSuites();
85        }
86    }
87
88    private static class DummyTrustManager implements X509TrustManager
89    {
90        public void checkClientTrusted(X509Certificate ax509certificate[], String JavaDoc authType)
91        {
92            return;
93        }
94
95        public void checkServerTrusted(X509Certificate ax509certificate[], String JavaDoc authType)
96        {
97            return;
98        }
99                 
100        public boolean isClientTrusted(X509Certificate[] cert)
101        {
102            return true;
103        }
104
105        public boolean isServerTrusted(X509Certificate[] cert)
106        {
107            return true;
108        }
109
110        public X509Certificate[] getAcceptedIssuers()
111        {
112            return new X509Certificate[0];
113        }
114    }
115 }
116
117
Popular Tags