1 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 ; 20 import java.security.KeyManagementException ; 21 import java.security.cert.CertificateException ; 22 import java.security.cert.X509Certificate ; 23 import java.security.cert.CertificateExpiredException ; 24 import java.security.cert.CertificateNotYetValidException ; 25 import java.net.Socket ; 26 import java.net.InetAddress ; 27 import java.io.IOException ; 28 29 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, new TrustManager[] { new DummyTrustManager() }, 45 new java.security.SecureRandom ()); 46 factory = sslcontent.getSocketFactory(); 47 } 48 catch (NoSuchAlgorithmException e) { 49 Log.error(e); 50 } 51 catch (KeyManagementException e) { 52 Log.error(e); 53 } 54 } 55 56 public static SocketFactory getDefault() { 57 return new SimpleSSLSocketFactory(); 58 } 59 60 public Socket createSocket(Socket socket, String s, int i, boolean flag) 61 throws IOException 62 { 63 return factory.createSocket(socket, s, i, flag); 64 } 65 66 public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr2, int j) 67 throws IOException 68 { 69 return factory.createSocket(inaddr, i, inaddr2, j); 70 } 71 72 public Socket createSocket(InetAddress inaddr, int i) 73 throws IOException 74 { 75 return factory.createSocket(inaddr, i); 76 } 77 78 public Socket createSocket(String s, int i, InetAddress inaddr, int j) 79 throws IOException 80 { 81 return factory.createSocket(s, i, inaddr, j); 82 } 83 84 public Socket createSocket(String s, int i) 85 throws IOException 86 { 87 return factory.createSocket(s, i); 88 } 89 90 public String [] getDefaultCipherSuites() { 91 return factory.getSupportedCipherSuites(); 92 } 93 94 public String [] getSupportedCipherSuites() { 95 return factory.getSupportedCipherSuites(); 96 } 97 98 private static class DummyTrustManager implements X509TrustManager { 99 100 public boolean isClientTrusted(X509Certificate [] cert) { 101 return true; 102 } 103 104 public boolean isServerTrusted(X509Certificate [] cert) { 105 try { 106 cert[0].checkValidity(); 107 return true; 108 } 109 catch (CertificateExpiredException e) { 110 return false; 111 } 112 catch (CertificateNotYetValidException e) { 113 return false; 114 } 115 } 116 117 public void checkClientTrusted(java.security.cert.X509Certificate [] x509Certificates, 118 String s) throws CertificateException 119 { 120 } 121 122 public void checkServerTrusted(java.security.cert.X509Certificate [] x509Certificates, 123 String s) throws CertificateException 124 { 125 } 126 127 public X509Certificate [] getAcceptedIssuers() { 128 return new X509Certificate [0]; 129 } 130 } 131 } | Popular Tags |