Skip to content

Commit 794f6e7

Browse files
authored
Merge pull request #33 from taskrabbit/tls
Force TLS to 1.1 or 1.2
2 parents 262048e + 3c15296 commit 794f6e7

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

android/app/src/main/java/com/sample/MainActivity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.sample;
22

3+
import android.os.Bundle;
4+
35
import com.facebook.react.ReactActivity;
46

57
public class MainActivity extends ReactActivity {
@@ -12,4 +14,11 @@ public class MainActivity extends ReactActivity {
1214
protected String getMainComponentName() {
1315
return "Sample";
1416
}
17+
18+
@Override
19+
protected void onCreate(Bundle savedInstanceState) {
20+
super.onCreate(savedInstanceState);
21+
22+
TLSSetup.configure();
23+
}
1524
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.sample;
2+
3+
import android.util.Log;
4+
import com.facebook.react.modules.network.OkHttpClientProvider;
5+
import com.facebook.react.modules.network.ReactCookieJarContainer;
6+
import okhttp3.ConnectionSpec;
7+
import okhttp3.OkHttpClient;
8+
import okhttp3.TlsVersion;
9+
10+
import javax.net.ssl.*;
11+
import java.util.Arrays;
12+
import java.util.concurrent.TimeUnit;
13+
14+
public class TLSSetup {
15+
16+
static String TAG = "TLSSetup";
17+
18+
public static void configure(){
19+
try {
20+
SSLContext sc = SSLContext.getInstance("TLSv1.1");
21+
sc.init(null, null, null);
22+
ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
23+
.tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1)
24+
.build();
25+
// Taken from OkHttpClientProvider.java
26+
// Set no timeout by default
27+
OkHttpClient sClient = new OkHttpClient.Builder()
28+
.connectTimeout(0, TimeUnit.MILLISECONDS)
29+
.readTimeout(0, TimeUnit.MILLISECONDS)
30+
.writeTimeout(0, TimeUnit.MILLISECONDS)
31+
.cookieJar(new ReactCookieJarContainer())
32+
// set sslSocketFactory
33+
.sslSocketFactory(new TLSSocketFactory(sc.getSocketFactory()))
34+
// set connectionSpecs
35+
.connectionSpecs(Arrays.asList(cs, ConnectionSpec.COMPATIBLE_TLS, ConnectionSpec.CLEARTEXT))
36+
.build();
37+
38+
OkHttpClientProvider.replaceOkHttpClient(sClient);
39+
} catch (Exception e) {
40+
Log.e(TAG, e.getMessage());
41+
}
42+
}
43+
44+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.sample;
2+
3+
import java.io.IOException;
4+
import java.net.InetAddress;
5+
import java.net.Socket;
6+
import java.net.UnknownHostException;
7+
8+
import javax.net.ssl.SSLSocket;
9+
import javax.net.ssl.SSLSocketFactory;
10+
11+
/**
12+
* Taken from https://gist.github.com/mlc/549409f649251897ebef
13+
*
14+
* Enables TLS when creating SSLSockets.
15+
*
16+
* @link https://developer.android.com/reference/javax/net/ssl/SSLSocket.html
17+
* @see SSLSocketFactory
18+
*/
19+
class TLSSocketFactory extends SSLSocketFactory {
20+
final SSLSocketFactory delegate;
21+
22+
public TLSSocketFactory(SSLSocketFactory delegate) {
23+
this.delegate = delegate;
24+
}
25+
26+
@Override
27+
public String[] getDefaultCipherSuites() {
28+
return delegate.getDefaultCipherSuites();
29+
}
30+
31+
@Override
32+
public String[] getSupportedCipherSuites() {
33+
return delegate.getSupportedCipherSuites();
34+
}
35+
36+
@Override
37+
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
38+
return patch(delegate.createSocket(s, host, port, autoClose));
39+
}
40+
41+
@Override
42+
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
43+
return patch(delegate.createSocket(host, port));
44+
}
45+
46+
@Override
47+
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
48+
return patch(delegate.createSocket(host, port, localHost, localPort));
49+
}
50+
51+
@Override
52+
public Socket createSocket(InetAddress host, int port) throws IOException {
53+
return patch(delegate.createSocket(host, port));
54+
}
55+
56+
@Override
57+
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
58+
return patch(delegate.createSocket(address, port, localAddress, localPort));
59+
}
60+
61+
private Socket patch(Socket s) {
62+
if (s instanceof SSLSocket) {
63+
((SSLSocket) s).setEnabledProtocols(((SSLSocket) s).getSupportedProtocols());
64+
}
65+
return s;
66+
}
67+
}

0 commit comments

Comments
 (0)