Skip to content

Commit 9488f6b

Browse files
committed
require TLSv1.3
1 parent 2071058 commit 9488f6b

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

app/src/main/java/app/grapheneos/apps/util/HttpUtils.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@ import android.net.Network
44
import java.net.HttpURLConnection
55
import java.net.ProtocolException
66
import java.net.URL
7+
import javax.net.ssl.HttpsURLConnection
8+
import org.grapheneos.tls.ModernTLSSocketFactory
79

8-
inline fun openConnection(network: Network?, urlString: String, configure: HttpURLConnection.() -> Unit): ScopedHttpConnection {
10+
private val tlsSocketFactory = ModernTLSSocketFactory()
11+
12+
fun openConnection(network: Network?, urlString: String, configure: HttpURLConnection.() -> Unit): ScopedHttpConnection {
913
val url = URL(urlString)
1014
val connection = if (network != null) {
1115
network.openConnection(url)
1216
} else {
1317
url.openConnection()
14-
} as HttpURLConnection
18+
} as HttpsURLConnection
1519

1620
connection.apply {
21+
sslSocketFactory = tlsSocketFactory
1722
connectTimeout = 10_000
1823
readTimeout = 30_000
1924
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)