1 19 20 package com.sslexplorer.boot; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.net.InetAddress ; 26 import java.net.Socket ; 27 import java.net.UnknownHostException ; 28 import java.security.KeyManagementException ; 29 import java.security.KeyStore ; 30 import java.security.KeyStoreException ; 31 import java.security.NoSuchAlgorithmException ; 32 import java.security.UnrecoverableKeyException ; 33 import java.security.cert.CertPath ; 34 import java.security.cert.CertPathValidator ; 35 import java.security.cert.CertPathValidatorResult ; 36 import java.security.cert.Certificate ; 37 import java.security.cert.CertificateException ; 38 import java.security.cert.CertificateFactory ; 39 import java.security.cert.PKIXCertPathValidatorResult ; 40 import java.security.cert.PKIXParameters ; 41 import java.security.cert.TrustAnchor ; 42 import java.security.cert.X509Certificate ; 43 import java.util.Arrays ; 44 import java.util.Enumeration ; 45 46 import javax.net.SocketFactory; 47 import javax.net.ssl.KeyManager; 48 import javax.net.ssl.KeyManagerFactory; 49 import javax.net.ssl.SSLContext; 50 import javax.net.ssl.SSLSocket; 51 import javax.net.ssl.SSLSocketFactory; 52 import javax.net.ssl.TrustManager; 53 import javax.net.ssl.X509TrustManager; 54 55 import org.apache.commons.logging.Log; 56 import org.apache.commons.logging.LogFactory; 57 58 60 public class CustomSSLSocketFactory extends SocketFactory { 61 62 private static final Log log = LogFactory.getLog(CustomSSLSocketFactory.class); 63 private static SocketFactory instance; 64 private static Class socketFactoryImpl = CustomSSLSocketFactory.class; 65 66 67 69 public CustomSSLSocketFactory() { 70 71 } 72 73 public static SocketFactory getDefault() { 74 try { 75 return instance == null ? instance = (SocketFactory) socketFactoryImpl.newInstance() : instance; 76 } catch (Exception e) { 77 log.error("Could not create instance of class " + socketFactoryImpl.getCanonicalName(), e); 78 return instance == null ? instance = new CustomSSLSocketFactory() : instance; 79 } 80 } 81 82 85 public static void setFactoryImpl(Class socketFactoryImpl) { 86 CustomSSLSocketFactory.socketFactoryImpl = socketFactoryImpl; 87 } 88 89 public Socket createSocket() throws IOException { 90 SSLSocket theSocket = (SSLSocket) getSocketFactory().createSocket(); 91 return theSocket; 92 } 93 94 public Socket createSocket(String hostname, int port) throws IOException , UnknownHostException { 95 SSLSocket theSocket = (SSLSocket) getSocketFactory().createSocket(hostname, port); 96 return theSocket; 97 } 98 99 public Socket createSocket(String hostname, int port, InetAddress arg2, int arg3) throws IOException , UnknownHostException { 100 SSLSocket theSocket = (SSLSocket) getSocketFactory().createSocket(hostname, port, arg2, arg3); 101 return theSocket; 102 } 103 104 public Socket createSocket(InetAddress arg0, int arg1) throws IOException { 105 SSLSocket theSocket = (SSLSocket) getSocketFactory().createSocket(arg0, arg1); 106 return theSocket; 107 } 108 109 public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException { 110 SSLSocket theSocket = (SSLSocket) getSocketFactory().createSocket(arg0, arg1, arg2, arg3); 111 return theSocket; 112 } 113 114 private SSLSocketFactory getSocketFactory() throws IOException { 115 try { 116 SSLContext sslCtx = SSLContext.getInstance("SSL"); 117 KeyManager[] aKM = SSLKeyManager.getKeyManagerArray(); 118 TrustManager[] aTM = SSLTrustManager.getTrustManagerArray(); 119 sslCtx.init(aKM, aTM, null); 120 SSLSocketFactory socketFactory = sslCtx.getSocketFactory(); 121 return socketFactory; 122 } catch (KeyManagementException e) { 123 log.error("Cannot create SSL socket", e); 124 throw new IOException ("Cannot create SSL socket: " + e.getMessage()); 125 } catch (NoSuchAlgorithmException e) { 126 log.error("Cannot create SSL socket", e); 127 throw new IOException ("Cannot create SSL socket: " + e.getMessage()); 128 } 129 } 130 131 132 133 134 }
| Popular Tags
|