Skip to content

Force TLS to 1.1 or 1.2 #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions android/app/src/main/java/com/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sample;

import android.os.Bundle;

import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {
Expand All @@ -12,4 +14,11 @@ public class MainActivity extends ReactActivity {
protected String getMainComponentName() {
return "Sample";
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TLSSetup.configure();
}
}
44 changes: 44 additions & 0 deletions android/app/src/main/java/com/sample/TLSSetup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.sample;

import android.util.Log;
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.react.modules.network.ReactCookieJarContainer;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import okhttp3.TlsVersion;

import javax.net.ssl.*;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

public class TLSSetup {

static String TAG = "TLSSetup";

public static void configure(){
try {
SSLContext sc = SSLContext.getInstance("TLSv1.1");
sc.init(null, null, null);
ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1)
.build();
// Taken from OkHttpClientProvider.java
// Set no timeout by default
OkHttpClient sClient = new OkHttpClient.Builder()
.connectTimeout(0, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
.writeTimeout(0, TimeUnit.MILLISECONDS)
.cookieJar(new ReactCookieJarContainer())
// set sslSocketFactory
.sslSocketFactory(new TLSSocketFactory(sc.getSocketFactory()))
// set connectionSpecs
.connectionSpecs(Arrays.asList(cs, ConnectionSpec.COMPATIBLE_TLS, ConnectionSpec.CLEARTEXT))
.build();

OkHttpClientProvider.replaceOkHttpClient(sClient);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}

}
67 changes: 67 additions & 0 deletions android/app/src/main/java/com/sample/TLSSocketFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.sample;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

/**
* Taken from https://gist.github.com/mlc/549409f649251897ebef
*
* Enables TLS when creating SSLSockets.
*
* @link https://developer.android.com/reference/javax/net/ssl/SSLSocket.html
* @see SSLSocketFactory
*/
class TLSSocketFactory extends SSLSocketFactory {
final SSLSocketFactory delegate;

public TLSSocketFactory(SSLSocketFactory delegate) {
this.delegate = delegate;
}

@Override
public String[] getDefaultCipherSuites() {
return delegate.getDefaultCipherSuites();
}

@Override
public String[] getSupportedCipherSuites() {
return delegate.getSupportedCipherSuites();
}

@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return patch(delegate.createSocket(s, host, port, autoClose));
}

@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return patch(delegate.createSocket(host, port));
}

@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return patch(delegate.createSocket(host, port, localHost, localPort));
}

@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return patch(delegate.createSocket(host, port));
}

@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return patch(delegate.createSocket(address, port, localAddress, localPort));
}

private Socket patch(Socket s) {
if (s instanceof SSLSocket) {
((SSLSocket) s).setEnabledProtocols(((SSLSocket) s).getSupportedProtocols());
}
return s;
}
}