Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.39 KB | None | 0 0
  1. package com.baozun.web.helpers;
  2.  
  3. import java.io.IOException;
  4.  
  5. import java.io.InputStream;
  6.  
  7. import java.net.InetAddress;
  8.  
  9. import java.net.Socket;
  10.  
  11. import java.security.KeyStore;
  12.  
  13. import java.security.Principal;
  14.  
  15. import java.security.PrivateKey;
  16.  
  17. import java.security.SecureRandom;
  18.  
  19. import java.security.cert.Certificate;
  20.  
  21. import java.security.cert.X509Certificate;
  22.  
  23. import java.util.Arrays;
  24.  
  25. import java.util.LinkedHashMap;
  26.  
  27. import java.util.Map;
  28.  
  29. import javax.net.ssl.KeyManager;
  30.  
  31. import javax.net.ssl.SSLContext;
  32.  
  33. import javax.net.ssl.SSLSocket;
  34.  
  35. import javax.net.ssl.SSLSocketFactory;
  36.  
  37. import javax.net.ssl.TrustManager;
  38.  
  39. import javax.net.ssl.X509KeyManager;
  40.  
  41. import javax.net.ssl.X509TrustManager;
  42.  
  43. /**
  44.  *
  45.  * Used for ssl tests to simplify setup.
  46.  *
  47.  */
  48.  
  49. public final class TrustingSSLSocketFactory extends SSLSocketFactory implements
  50.  
  51.         X509TrustManager, X509KeyManager {
  52.  
  53.     private static final Map<String, SSLSocketFactory> sslSocketFactories = new LinkedHashMap<String, SSLSocketFactory>();
  54.  
  55.     private static final char[] KEYSTORE_PASSWORD = "password".toCharArray();
  56.  
  57.     private final static String[] ENABLED_CIPHER_SUITES = { "SSL_RSA_WITH_3DES_EDE_CBC_SHA" };
  58.  
  59.     private final SSLSocketFactory delegate;
  60.  
  61.     private final String serverAlias;
  62.  
  63.     private final PrivateKey privateKey;
  64.  
  65.     private final X509Certificate[] certificateChain;
  66.  
  67.     private TrustingSSLSocketFactory(String serverAlias) {
  68.  
  69.         try {
  70.  
  71.             SSLContext sc = SSLContext.getInstance("TLSv1.2");
  72.  
  73.             sc.init(new KeyManager[] { this }, new TrustManager[] { this },
  74.  
  75.                     new SecureRandom());
  76.  
  77.             this.delegate = sc.getSocketFactory();
  78.  
  79.         } catch (Exception e) {
  80.  
  81.             throw new RuntimeException(e);
  82.  
  83.         }
  84.  
  85.         this.serverAlias = serverAlias;
  86.  
  87.         if (serverAlias.isEmpty()) {
  88.  
  89.             this.privateKey = null;
  90.  
  91.             this.certificateChain = null;
  92.  
  93.         } else {
  94.  
  95.             try {
  96.  
  97.                 KeyStore keyStore = loadKeyStore(TrustingSSLSocketFactory.class
  98.  
  99.                         .getResourceAsStream("/keystore.jks"));
  100.  
  101.                 this.privateKey = (PrivateKey) keyStore.getKey(serverAlias,
  102.  
  103.                         KEYSTORE_PASSWORD);
  104.  
  105.                 Certificate[] rawChain = keyStore
  106.  
  107.                         .getCertificateChain(serverAlias);
  108.  
  109.                 this.certificateChain = Arrays.copyOf(rawChain,
  110.  
  111.                         rawChain.length, X509Certificate[].class);
  112.  
  113.             } catch (Exception e) {
  114.  
  115.                 throw new RuntimeException(e);
  116.  
  117.             }
  118.  
  119.         }
  120.  
  121.     }
  122.  
  123.     public static SSLSocketFactory get() {
  124.  
  125.         return get("");
  126.  
  127.     }
  128.  
  129.     public synchronized static SSLSocketFactory get(String serverAlias) {
  130.  
  131.         if (!sslSocketFactories.containsKey(serverAlias)) {
  132.  
  133.             sslSocketFactories.put(serverAlias, new TrustingSSLSocketFactory(
  134.  
  135.                     serverAlias));
  136.  
  137.         }
  138.  
  139.         return sslSocketFactories.get(serverAlias);
  140.  
  141.     }
  142.  
  143.     static Socket setEnabledCipherSuites(Socket socket) {
  144.  
  145.         SSLSocket.class.cast(socket).setEnabledCipherSuites(
  146.  
  147.                 ENABLED_CIPHER_SUITES);
  148.  
  149.         return socket;
  150.  
  151.     }
  152.  
  153.     private static KeyStore loadKeyStore(InputStream inputStream)
  154.  
  155.             throws IOException {
  156.  
  157.         try {
  158.  
  159.             KeyStore keyStore = KeyStore.getInstance("JKS");
  160.  
  161.             keyStore.load(inputStream, KEYSTORE_PASSWORD);
  162.  
  163.             return keyStore;
  164.  
  165.         } catch (Exception e) {
  166.  
  167.             throw new RuntimeException(e);
  168.  
  169.         } finally {
  170.  
  171.             inputStream.close();
  172.  
  173.         }
  174.  
  175.     }
  176.  
  177.     @Override
  178.  
  179.     public String[] getDefaultCipherSuites() {
  180.  
  181.         return ENABLED_CIPHER_SUITES;
  182.  
  183.     }
  184.  
  185.     @Override
  186.  
  187.     public String[] getSupportedCipherSuites() {
  188.  
  189.         return ENABLED_CIPHER_SUITES;
  190.  
  191.     }
  192.  
  193.     @Override
  194.  
  195.     public Socket createSocket(Socket s, String host, int port,
  196.  
  197.             boolean autoClose) throws IOException {
  198.  
  199.         return setEnabledCipherSuites(delegate.createSocket(s, host, port,
  200.  
  201.                 autoClose));
  202.  
  203.     }
  204.  
  205.     @Override
  206.  
  207.     public Socket createSocket(String host, int port) throws IOException {
  208.  
  209.         return setEnabledCipherSuites(delegate.createSocket(host, port));
  210.  
  211.     }
  212.  
  213.     @Override
  214.  
  215.     public Socket createSocket(InetAddress host, int port) throws IOException {
  216.  
  217.         return setEnabledCipherSuites(delegate.createSocket(host, port));
  218.  
  219.     }
  220.  
  221.     @Override
  222.  
  223.     public Socket createSocket(String host, int port, InetAddress localHost,
  224.  
  225.             int localPort) throws IOException {
  226.  
  227.         return setEnabledCipherSuites(delegate.createSocket(host, port,
  228.  
  229.                 localHost, localPort));
  230.  
  231.     }
  232.  
  233.     @Override
  234.  
  235.     public Socket createSocket(InetAddress address, int port,
  236.  
  237.             InetAddress localAddress, int localPort) throws IOException {
  238.  
  239.         return setEnabledCipherSuites(delegate.createSocket(address, port,
  240.  
  241.                 localAddress, localPort));
  242.  
  243.     }
  244.  
  245.     public X509Certificate[] getAcceptedIssuers() {
  246.  
  247.         return null;
  248.  
  249.     }
  250.  
  251.     public void checkClientTrusted(X509Certificate[] certs, String authType) {
  252.  
  253.     }
  254.  
  255.     public void checkServerTrusted(X509Certificate[] certs, String authType) {
  256.  
  257.     }
  258.  
  259.     @Override
  260.  
  261.     public String[] getClientAliases(String keyType, Principal[] issuers) {
  262.  
  263.         return null;
  264.  
  265.     }
  266.  
  267.     @Override
  268.  
  269.     public String chooseClientAlias(String[] keyType, Principal[] issuers,
  270.  
  271.             Socket socket) {
  272.  
  273.         return null;
  274.  
  275.     }
  276.  
  277.     @Override
  278.  
  279.     public String[] getServerAliases(String keyType, Principal[] issuers) {
  280.  
  281.         return null;
  282.  
  283.     }
  284.  
  285.     @Override
  286.  
  287.     public String chooseServerAlias(String keyType, Principal[] issuers,
  288.  
  289.             Socket socket) {
  290.  
  291.         return serverAlias;
  292.  
  293.     }
  294.  
  295.     @Override
  296.  
  297.     public X509Certificate[] getCertificateChain(String alias) {
  298.  
  299.         return certificateChain;
  300.  
  301.     }
  302.  
  303.     @Override
  304.  
  305.     public PrivateKey getPrivateKey(String alias) {
  306.  
  307.         return privateKey;
  308.  
  309.     }
  310.  
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement