1 20 21 package org.jivesoftware.smack; 22 23 import javax.net.SocketFactory; 24 import javax.net.ssl.SSLContext; 25 import javax.net.ssl.SSLSocketFactory; 26 import javax.net.ssl.TrustManager; 27 import java.io.IOException ; 28 import java.net.InetAddress ; 29 import java.net.Socket ; 30 import java.security.KeyManagementException ; 31 import java.security.NoSuchAlgorithmException ; 32 33 38 public class SSLXMPPConnection extends XMPPConnection { 39 40 private static SocketFactory socketFactory = new DummySSLSocketFactory(); 41 42 54 public SSLXMPPConnection(String host) throws XMPPException { 55 this(host, 5223); 56 } 57 58 70 public SSLXMPPConnection(String host, int port) throws XMPPException { 71 this(host, port, host); 72 } 73 74 86 public SSLXMPPConnection(String host, int port, String serviceName) throws XMPPException { 87 super(host, port, serviceName, socketFactory); 88 } 89 90 public boolean isSecureConnection() { 91 return true; 92 } 93 94 98 private static class DummySSLSocketFactory extends SSLSocketFactory { 99 100 private SSLSocketFactory factory; 101 102 public DummySSLSocketFactory() { 103 104 try { 105 SSLContext sslcontent = SSLContext.getInstance("TLS"); 106 sslcontent.init(null, new TrustManager[] { new OpenTrustManager() }, 108 new java.security.SecureRandom ()); 109 factory = sslcontent.getSocketFactory(); 110 } 111 catch (NoSuchAlgorithmException e) { 112 e.printStackTrace(); 113 } 114 catch (KeyManagementException e) { 115 e.printStackTrace(); 116 } 117 } 118 119 public static SocketFactory getDefault() { 120 return new DummySSLSocketFactory(); 121 } 122 123 public Socket createSocket(Socket socket, String s, int i, boolean flag) 124 throws IOException 125 { 126 return factory.createSocket(socket, s, i, flag); 127 } 128 129 public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr2, int j) 130 throws IOException 131 { 132 return factory.createSocket(inaddr, i, inaddr2, j); 133 } 134 135 public Socket createSocket(InetAddress inaddr, int i) throws IOException { 136 return factory.createSocket(inaddr, i); 137 } 138 139 public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException { 140 return factory.createSocket(s, i, inaddr, j); 141 } 142 143 public Socket createSocket(String s, int i) throws IOException { 144 return factory.createSocket(s, i); 145 } 146 147 public String [] getDefaultCipherSuites() { 148 return factory.getSupportedCipherSuites(); 149 } 150 151 public String [] getSupportedCipherSuites() { 152 return factory.getSupportedCipherSuites(); 153 } 154 } 155 } 156 | Popular Tags |