|
| 1 | +package com.microsoft.codepush.react; |
| 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 | + |
| 10 | +import javax.net.ssl.SSLContext; |
| 11 | +import javax.net.ssl.SSLSocket; |
| 12 | +import javax.net.ssl.SSLSocketFactory; |
| 13 | + |
| 14 | +public class TLSSocketFactory extends SSLSocketFactory { |
| 15 | + |
| 16 | + private SSLSocketFactory delegate; |
| 17 | + |
| 18 | + public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException { |
| 19 | + SSLContext context = SSLContext.getInstance("TLS"); |
| 20 | + context.init(null, null, null); |
| 21 | + delegate = context.getSocketFactory(); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public String[] getDefaultCipherSuites() { |
| 26 | + return delegate.getDefaultCipherSuites(); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public String[] getSupportedCipherSuites() { |
| 31 | + return delegate.getSupportedCipherSuites(); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Socket createSocket() throws IOException { |
| 36 | + return enableTLSOnSocket(delegate.createSocket()); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { |
| 41 | + return enableTLSOnSocket(delegate.createSocket(s, host, port, autoClose)); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public Socket createSocket(String host, int port) throws IOException, UnknownHostException { |
| 46 | + return enableTLSOnSocket(delegate.createSocket(host, port)); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Socket createSocket(String host, int port, InetAddress localHost, int localPort) |
| 51 | + throws IOException, UnknownHostException { |
| 52 | + return enableTLSOnSocket(delegate.createSocket(host, port, localHost, localPort)); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Socket createSocket(InetAddress host, int port) throws IOException { |
| 57 | + return enableTLSOnSocket(delegate.createSocket(host, port)); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) |
| 62 | + throws IOException { |
| 63 | + return enableTLSOnSocket(delegate.createSocket(address, port, localAddress, localPort)); |
| 64 | + } |
| 65 | + |
| 66 | + private Socket enableTLSOnSocket(Socket socket) { |
| 67 | + if (socket != null && (socket instanceof SSLSocket)) { |
| 68 | + ((SSLSocket) socket).setEnabledProtocols(new String[] { "TLSv1.1", "TLSv1.2" }); |
| 69 | + } |
| 70 | + return socket; |
| 71 | + } |
| 72 | +} |
0 commit comments