|
| 1 | +package org.grapheneos.tls; |
| 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 ModernTLSSocketFactory extends SSLSocketFactory { |
| 15 | + private final SSLSocketFactory wrapped; |
| 16 | + |
| 17 | + public ModernTLSSocketFactory() { |
| 18 | + try { |
| 19 | + final SSLContext context = SSLContext.getInstance("TLS"); |
| 20 | + context.init(null, null, null); |
| 21 | + wrapped = context.getSocketFactory(); |
| 22 | + } catch (final KeyManagementException | NoSuchAlgorithmException e) { |
| 23 | + throw new RuntimeException(e); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public String[] getDefaultCipherSuites() { |
| 29 | + return wrapped.getDefaultCipherSuites(); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public String[] getSupportedCipherSuites() { |
| 34 | + return wrapped.getSupportedCipherSuites(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public Socket createSocket() throws IOException { |
| 39 | + return configureSocket(wrapped.createSocket()); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Socket createSocket(final Socket s, final String host, final int port, |
| 44 | + final boolean autoClose) throws IOException { |
| 45 | + return configureSocket(wrapped.createSocket(s, host, port, autoClose)); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Socket createSocket(final String host, final int port) |
| 50 | + throws IOException, UnknownHostException { |
| 51 | + return configureSocket(wrapped.createSocket(host, port)); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Socket createSocket(final String host, final int port, final InetAddress localHost, |
| 56 | + final int localPort) throws IOException, UnknownHostException { |
| 57 | + return configureSocket(wrapped.createSocket(host, port, localHost, localPort)); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Socket createSocket(final InetAddress host, final int port) throws IOException { |
| 62 | + return configureSocket(wrapped.createSocket(host, port)); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Socket createSocket(final InetAddress address, final int port, |
| 67 | + final InetAddress localAddress, final int localPort) throws IOException { |
| 68 | + return configureSocket(wrapped.createSocket(address, port, localAddress, localPort)); |
| 69 | + } |
| 70 | + |
| 71 | + private static Socket configureSocket(final Socket socket) { |
| 72 | + ((SSLSocket) socket).setEnabledProtocols(new String[] {"TLSv1.3"}); |
| 73 | + return socket; |
| 74 | + } |
| 75 | +} |
0 commit comments