Skip to content

Enable TLS 1.1 and TLS 1.2 on Android 4.1-4.4 for web socket connection #14042

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

Closed
wants to merge 7 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@

package com.facebook.react.modules.websocket;

import android.os.Build;
import android.util.Base64;
import javax.annotation.Nullable;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -24,6 +36,9 @@
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.modules.network.ForwardingCookieHandler;
import com.facebook.react.modules.network.TLSSocketFactory;

import okhttp3.ConnectionSpec;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -35,8 +50,26 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import okhttp3.ResponseBody;
import okhttp3.TlsVersion;
import okhttp3.ws.WebSocket;
import okhttp3.ws.WebSocketCall;
import okhttp3.ws.WebSocketListener;

import java.net.URISyntaxException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okio.Buffer;

import okhttp3.WebSocket;
import okhttp3.WebSocketListener;

import okio.ByteString;

@ReactModule(name = "WebSocketModule", hasConstants = false)
Expand Down Expand Up @@ -71,6 +104,32 @@ public String getName() {
return "WebSocketModule";
}

/*
On Android 4.1-4.4 (API level 16 to 19) TLS 1.1 and 1.2 are
available but not enabled by default. The following method
enables it.
*/
public static OkHttpClient.Builder enableTls12OnPreLollipop(OkHttpClient.Builder client) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
try {
client.sslSocketFactory(new TLSSocketFactory());

ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.build();

List<ConnectionSpec> specs = new ArrayList<>();
specs.add(cs);
specs.add(ConnectionSpec.COMPATIBLE_TLS);
specs.add(ConnectionSpec.CLEARTEXT);

client.connectionSpecs(specs);
} catch (Exception exc) {
FLog.e("OkHttpClientProvider", "Error while enabling TLS 1.2", exc);
}
}

return client;
public void setContentHandler(final int id, final ContentHandler contentHandler) {
if (contentHandler != null) {
mContentHandlers.put(id, contentHandler);
Expand All @@ -85,11 +144,12 @@ public void connect(
@Nullable final ReadableArray protocols,
@Nullable final ReadableMap options,
final int id) {
OkHttpClient client = new OkHttpClient.Builder()
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.MINUTES) // Disable timeouts for read
.build();
.readTimeout(0, TimeUnit.MINUTES); // Disable timeouts for read

OkHttpClient client = enableTls12OnPreLollipop(clientBuilder).build();

Request.Builder builder = new Request.Builder().tag(id).url(url);

Expand Down