|
17 | 17 |
|
18 | 18 | package org.quantumbadger.redreader.http;
|
19 | 19 |
|
20 |
| -import androidx.annotation.NonNull; |
21 |
| - |
22 |
| -import javax.net.ssl.SSLSocket; |
23 |
| -import javax.net.ssl.SSLSocketFactory; |
24 | 20 | import java.io.IOException;
|
25 | 21 | import java.net.InetAddress;
|
26 | 22 | import java.net.Socket;
|
27 | 23 |
|
28 |
| -public class LegacyTLSSocketFactory extends SSLSocketFactory { |
| 24 | +import javax.net.ssl.SSLSocket; |
| 25 | +import javax.net.ssl.SSLSocketFactory; |
29 | 26 |
|
30 |
| - private static final String[] TLS_V1_2_ONLY = {"TLSv1.2"}; |
| 27 | +public final class InternalSSLSocketFactory extends SSLSocketFactory { |
| 28 | + private static final String[] ENABLED_PROTOCOLS = {"TLSv1.2", "TLSv1.3"}; |
31 | 29 |
|
32 |
| - private final SSLSocketFactory delegate; |
| 30 | + private final SSLSocketFactory sslSocketFactory; |
33 | 31 |
|
34 |
| - public LegacyTLSSocketFactory(@NonNull final SSLSocketFactory base) { |
35 |
| - this.delegate = base; |
| 32 | + public InternalSSLSocketFactory(final SSLSocketFactory sslSocketFactory) { |
| 33 | + this.sslSocketFactory = sslSocketFactory; |
36 | 34 | }
|
37 | 35 |
|
38 | 36 | @Override
|
39 | 37 | public String[] getDefaultCipherSuites() {
|
40 |
| - return delegate.getDefaultCipherSuites(); |
| 38 | + return sslSocketFactory.getDefaultCipherSuites(); |
41 | 39 | }
|
42 | 40 |
|
43 | 41 | @Override
|
44 | 42 | public String[] getSupportedCipherSuites() {
|
45 |
| - return delegate.getSupportedCipherSuites(); |
| 43 | + return sslSocketFactory.getSupportedCipherSuites(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Socket createSocket() throws IOException { |
| 48 | + return enableTLSOnSocket(sslSocketFactory.createSocket()); |
46 | 49 | }
|
47 | 50 |
|
48 | 51 | @Override
|
49 | 52 | public Socket createSocket(
|
50 |
| - final Socket s, |
| 53 | + final Socket socket, |
51 | 54 | final String host,
|
52 | 55 | final int port,
|
53 |
| - final boolean autoClose) throws IOException { |
54 |
| - return enableTLS1_2(delegate.createSocket(s, host, port, autoClose)); |
| 56 | + final boolean autoClose |
| 57 | + ) throws IOException { |
| 58 | + return enableTLSOnSocket(sslSocketFactory.createSocket(socket, host, port, autoClose)); |
55 | 59 | }
|
56 | 60 |
|
57 | 61 | @Override
|
58 | 62 | public Socket createSocket(final String host, final int port) throws IOException {
|
59 |
| - return enableTLS1_2(delegate.createSocket(host, port)); |
| 63 | + return enableTLSOnSocket(sslSocketFactory.createSocket(host, port)); |
60 | 64 | }
|
61 | 65 |
|
62 | 66 | @Override
|
63 | 67 | public Socket createSocket(
|
64 | 68 | final String host,
|
65 | 69 | final int port,
|
66 | 70 | final InetAddress localHost,
|
67 |
| - final int localPort) throws IOException { |
68 |
| - return enableTLS1_2(delegate.createSocket(host, port, localHost, localPort)); |
| 71 | + final int localPort |
| 72 | + ) throws IOException { |
| 73 | + return enableTLSOnSocket(sslSocketFactory.createSocket(host, port, localHost, localPort)); |
69 | 74 | }
|
70 | 75 |
|
71 | 76 | @Override
|
72 |
| - public Socket createSocket( |
73 |
| - final InetAddress host, |
74 |
| - final int port) throws IOException { |
75 |
| - return enableTLS1_2(delegate.createSocket(host, port)); |
| 77 | + public Socket createSocket(final InetAddress host, final int port) throws IOException { |
| 78 | + return enableTLSOnSocket(sslSocketFactory.createSocket(host, port)); |
76 | 79 | }
|
77 | 80 |
|
78 | 81 | @Override
|
79 | 82 | public Socket createSocket(
|
80 | 83 | final InetAddress address,
|
81 | 84 | final int port,
|
82 | 85 | final InetAddress localAddress,
|
83 |
| - final int localPort) throws IOException { |
84 |
| - return enableTLS1_2(delegate.createSocket(address, port, localAddress, localPort)); |
| 86 | + final int localPort |
| 87 | + ) throws IOException { |
| 88 | + return enableTLSOnSocket( |
| 89 | + sslSocketFactory.createSocket(address, port, localAddress, localPort) |
| 90 | + ); |
85 | 91 | }
|
86 | 92 |
|
87 |
| - private Socket enableTLS1_2(final Socket s) { |
88 |
| - if (s instanceof SSLSocket) { |
89 |
| - ((SSLSocket)s).setEnabledProtocols(TLS_V1_2_ONLY); |
| 93 | + private Socket enableTLSOnSocket(final Socket socket) { |
| 94 | + if (socket instanceof SSLSocket) { |
| 95 | + final SSLSocket sslSocket = (SSLSocket) socket; |
| 96 | + sslSocket.setEnabledProtocols(ENABLED_PROTOCOLS); |
90 | 97 | }
|
91 |
| - return s; |
| 98 | + return socket; |
92 | 99 | }
|
93 | 100 | }
|
0 commit comments