1 36 package org.columba.ristretto.ssl; 37 38 import java.io.IOException ; 39 import java.net.InetAddress ; 40 import java.net.Socket ; 41 import java.security.KeyManagementException ; 42 import java.security.NoSuchAlgorithmException ; 43 44 import javax.net.ssl.KeyManager; 45 import javax.net.ssl.SSLContext; 46 import javax.net.ssl.SSLSocketFactory; 47 import javax.net.ssl.TrustManager; 48 49 55 56 public class RistrettoSSLSocketFactory { 57 58 private static RistrettoSSLSocketFactory myInstance; 59 60 private SSLSocketFactory socketFactory; 61 62 67 public static RistrettoSSLSocketFactory getInstance() { 68 if( myInstance == null ) { 69 myInstance = new RistrettoSSLSocketFactory(); 70 } 71 72 return myInstance; 73 } 74 75 protected RistrettoSSLSocketFactory() { 76 try { 77 SSLContext sslContext = SSLContext.getInstance("TLS"); 78 79 sslContext.init(null, new TrustManager[] { new DefaultTrustManager() }, new java.security.SecureRandom ()); 80 81 socketFactory = sslContext.getSocketFactory(); 82 } catch (NoSuchAlgorithmException e) { 83 e.printStackTrace(System.out); 84 } catch (KeyManagementException e) { 85 e.printStackTrace(System.out); 86 } 87 } 88 89 94 public void setTrustManager(TrustManager tm) { 95 try { 96 SSLContext sslContext = SSLContext.getInstance("TLS"); 97 98 sslContext.init(null, new TrustManager[] { tm }, new java.security.SecureRandom ()); 99 100 socketFactory = sslContext.getSocketFactory(); 101 } catch (NoSuchAlgorithmException e) { 102 e.printStackTrace(System.out); 103 } catch (KeyManagementException e) { 104 e.printStackTrace(System.out); 105 } 106 } 107 108 113 public void setKeyManager(KeyManager km) { 114 try { 115 SSLContext sslContext = SSLContext.getInstance("TLS"); 116 117 sslContext.init(new KeyManager[] { km }, null, new java.security.SecureRandom ()); 118 119 socketFactory = sslContext.getSocketFactory(); 120 } catch (NoSuchAlgorithmException e) { 121 e.printStackTrace(System.out); 122 } catch (KeyManagementException e) { 123 e.printStackTrace(System.out); 124 } 125 } 126 127 135 public Socket createSocket(InetAddress address, int port) throws IOException { 136 return socketFactory.createSocket(address, port); 137 } 138 139 149 public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { 150 return socketFactory.createSocket(address, port, localAddress, localPort); 151 } 152 153 161 public Socket createSocket(String host, int port) throws IOException { 162 return socketFactory.createSocket(host, port); 163 } 164 165 175 public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException { 176 return socketFactory.createSocket(host, port, localHost, localPort); 177 } 178 179 189 public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException { 190 return socketFactory.createSocket(socket, host, port, autoClose); 191 } 192 } 193 | Popular Tags |