Guest User

Untitled

a guest
Jan 15th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.IOException;
  4. import java.net.InetAddress;
  5. import java.net.Socket;
  6. import java.net.UnknownHostException;
  7. import java.security.KeyManagementException;
  8. import java.security.NoSuchAlgorithmException;
  9. import java.security.SecureRandom;
  10. import java.security.cert.X509Certificate;
  11.  
  12. import javax.net.ssl.SSLContext;
  13. import javax.net.ssl.SSLSocket;
  14. import javax.net.ssl.SSLSocketFactory;
  15. import javax.net.ssl.TrustManager;
  16. import javax.net.ssl.X509TrustManager;
  17.  
  18. public class MySSLSocketFactory extends SSLSocketFactory {
  19.  
  20.     SSLSocketFactory factory;
  21.     String cipher[] = null;
  22.     String protocol[] = null;
  23.  
  24.     public MySSLSocketFactory(int cipher, int protocol) throws NoSuchAlgorithmException, KeyManagementException {
  25.         super();
  26.         TrustManager[] byPassTrustManagers = new TrustManager[] { new X509TrustManager() {
  27.             public X509Certificate[] getAcceptedIssuers() {
  28.                 return new X509Certificate[0];
  29.             }
  30.  
  31.             public void checkClientTrusted(X509Certificate[] chain, String authType) {
  32.             }
  33.  
  34.             public void checkServerTrusted(X509Certificate[] chain, String authType) {
  35.                 for (X509Certificate cert : chain) {
  36.                     System.out.println("srv: " + cert.getSubjectDN());
  37.                 }
  38.             }
  39.         } };
  40.         //http://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SSLContext
  41.         int c = (cipher > Main.ciphers.length - 1) ? Main.ciphers.length - 1 : cipher;
  42.         int p = (protocol > Main.protocols.length - 1) ? Main.protocols.length - 1 : protocol;
  43.         this.cipher = new String[1];
  44.         this.cipher[0] = Main.ciphers[c];
  45.         this.protocol = new String[1];
  46.         this.protocol[0] = Main.protocols[p];
  47.                
  48.         SSLContext sslContext = SSLContext.getInstance(this.protocol[0]);
  49.         sslContext.init(null, byPassTrustManagers, new SecureRandom());
  50.  
  51.         factory = sslContext.getSocketFactory();
  52.         System.out.println("SSL: cipher= "+this.cipher[0]+" protocol="+this.protocol[0]);
  53.     }
  54.  
  55.     @Override
  56.     public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
  57.         SSLSocket socket=(SSLSocket) factory.createSocket(s, host, port, autoClose);
  58.         socket.setEnabledCipherSuites(cipher);
  59.         socket.setEnabledProtocols(protocol);
  60.         return socket;
  61.     }
  62.  
  63.     @Override
  64.     public String[] getDefaultCipherSuites() {
  65.         return cipher;
  66.     }
  67.  
  68.     @Override
  69.     public String[] getSupportedCipherSuites() {
  70.         return cipher;
  71.     }
  72.  
  73.     @Override
  74.     public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException {
  75.         SSLSocket socket=(SSLSocket) factory.createSocket(arg0, arg1);
  76.         socket.setEnabledCipherSuites(cipher);
  77.         socket.setEnabledProtocols(protocol);
  78.         return socket;
  79.     }
  80.  
  81.     @Override
  82.     public Socket createSocket(InetAddress arg0, int arg1) throws IOException {
  83.         SSLSocket socket=(SSLSocket) factory.createSocket(arg0, arg1);
  84.         socket.setEnabledCipherSuites(cipher);
  85.         socket.setEnabledProtocols(protocol);
  86.         return socket;
  87.     }
  88.  
  89.     @Override
  90.     public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException {
  91.         SSLSocket socket=(SSLSocket) factory.createSocket(arg0, arg1, arg2, arg3);
  92.         socket.setEnabledCipherSuites(cipher);
  93.         socket.setEnabledProtocols(protocol);
  94.         return socket;
  95.     }
  96.  
  97.     @Override
  98.     public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException {
  99.         SSLSocket socket=(SSLSocket) factory.createSocket(arg0, arg1, arg2, arg3);
  100.         socket.setEnabledCipherSuites(cipher);
  101.         socket.setEnabledProtocols(protocol);
  102.         return socket;
  103.        
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment