Browse Source

Add support for TLS 1.2 when running Android 4 (#430)

Jeff Hellman 7 years ago
parent
commit
6bb7c65161
1 changed files with 33 additions and 1 deletions
  1. 33
    1
      android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+ 33
- 1
android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

@@ -7,10 +7,12 @@ import android.content.Intent;
7 7
 import android.content.IntentFilter;
8 8
 import android.database.Cursor;
9 9
 import android.net.Uri;
10
+import android.os.Build;
10 11
 import android.util.Base64;
11 12
 
12 13
 import com.RNFetchBlob.Response.RNFetchBlobDefaultResp;
13 14
 import com.RNFetchBlob.Response.RNFetchBlobFileResp;
15
+import com.facebook.common.logging.FLog;
14 16
 import com.facebook.react.bridge.Arguments;
15 17
 import com.facebook.react.bridge.Callback;
16 18
 import com.facebook.react.bridge.ReactApplicationContext;
@@ -21,6 +23,7 @@ import com.facebook.react.bridge.WritableArray;
21 23
 import com.facebook.react.bridge.WritableMap;
22 24
 import com.facebook.react.modules.core.DeviceEventManagerModule;
23 25
 import com.facebook.react.modules.network.OkHttpClientProvider;
26
+import com.facebook.react.modules.network.TLSSocketFactory;
24 27
 
25 28
 import java.io.File;
26 29
 import java.io.FileOutputStream;
@@ -35,11 +38,14 @@ import java.nio.charset.CharacterCodingException;
35 38
 import java.nio.charset.Charset;
36 39
 import java.nio.charset.CharsetEncoder;
37 40
 import java.util.ArrayList;
41
+import java.util.List;
38 42
 import java.util.HashMap;
43
+
39 44
 import java.util.concurrent.TimeUnit;
40 45
 
41 46
 import okhttp3.Call;
42 47
 import okhttp3.ConnectionPool;
48
+import okhttp3.ConnectionSpec;
43 49
 import okhttp3.Headers;
44 50
 import okhttp3.Interceptor;
45 51
 import okhttp3.MediaType;
@@ -48,6 +54,8 @@ import okhttp3.Request;
48 54
 import okhttp3.RequestBody;
49 55
 import okhttp3.Response;
50 56
 import okhttp3.ResponseBody;
57
+import okhttp3.TlsVersion;
58
+
51 59
 
52 60
 public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
53 61
 
@@ -366,9 +374,10 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
366 374
             clientBuilder.retryOnConnectionFailure(false);
367 375
             clientBuilder.followRedirects(options.followRedirect);
368 376
             clientBuilder.followSslRedirects(options.followRedirect);
377
+            clientBuilder.retryOnConnectionFailure(true);
369 378
 
379
+            OkHttpClient client = enableTls12OnPreLollipop(clientBuilder).build();
370 380
 
371
-            OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();
372 381
             Call call =  client.newCall(req);
373 382
             taskTable.put(taskId, call);
374 383
             call.enqueue(new okhttp3.Callback() {
@@ -683,5 +692,28 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
683 692
         }
684 693
     }
685 694
 
695
+    public static OkHttpClient.Builder enableTls12OnPreLollipop(OkHttpClient.Builder client) {
696
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
697
+            try {
698
+                client.sslSocketFactory(new TLSSocketFactory());
699
+
700
+                ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
701
+                        .tlsVersions(TlsVersion.TLS_1_2)
702
+                        .build();
703
+
704
+                List< ConnectionSpec > specs = new ArrayList < > ();
705
+                specs.add(cs);
706
+                specs.add(ConnectionSpec.COMPATIBLE_TLS);
707
+                specs.add(ConnectionSpec.CLEARTEXT);
708
+
709
+                client.connectionSpecs(specs);
710
+            } catch (Exception exc) {
711
+                FLog.e("OkHttpClientProvider", "Error while enabling TLS 1.2", exc);
712
+            }
713
+        }
714
+
715
+        return client;
716
+    }
717
+
686 718
 
687 719
 }