Skip to content
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

Problem with java.lang.RuntimeException: java.lang.IllegalStateException: Use of own trust manager but none defined #357

Closed
cristoferfeliz03 opened this issue May 3, 2024 · 1 comment

Comments

@cristoferfeliz03
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch react-native-blob-util@0.17.2 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-blob-util/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilUtils.java b/node_modules/react-native-blob-util/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilUtils.java
index 1565e51..b869034 100644
--- a/node_modules/react-native-blob-util/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilUtils.java
+++ b/node_modules/react-native-blob-util/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilUtils.java
@@ -2,15 +2,19 @@ package com.ReactNativeBlobUtil;
 
 import android.net.Uri;
 import android.util.Base64;
+import android.util.Log;
 
 import com.ReactNativeBlobUtil.Utils.PathResolver;
 import com.facebook.react.bridge.Arguments;
 import com.facebook.react.bridge.WritableMap;
 import com.facebook.react.modules.core.DeviceEventManagerModule;
+import com.facebook.react.modules.network.ReactCookieJarContainer;
 
 import java.nio.charset.Charset;
 import java.security.MessageDigest;
+import java.security.cert.CertificateException;
 import java.util.Locale;
+import java.util.concurrent.TimeUnit;
 
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.SSLContext;
@@ -22,6 +26,7 @@ import javax.net.ssl.X509TrustManager;
 import okhttp3.OkHttpClient;
 
 public class ReactNativeBlobUtilUtils {
+    private static final String TAG = "OkHttpClientFactory";
 
     public static X509TrustManager sharedTrustManager;
 
@@ -61,19 +66,36 @@ public class ReactNativeBlobUtilUtils {
 
     public static OkHttpClient.Builder getUnsafeOkHttpClient(OkHttpClient client) {
         try {
-
-            if (sharedTrustManager == null) throw new IllegalStateException("Use of own trust manager but none defined");
-
-            final TrustManager[] trustAllCerts = new TrustManager[]{sharedTrustManager};
+            // Create a trust manager that does not validate certificate chains
+            final TrustManager[] trustAllCerts = new TrustManager[]{
+                    new X509TrustManager() {
+                        @Override
+                        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
+                        }
+
+                        @Override
+                        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
+                        }
+
+                        @Override
+                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
+                            return new java.security.cert.X509Certificate[]{};
+                        }
+                    }
+            };
 
             // Install the all-trusting trust manager
             final SSLContext sslContext = SSLContext.getInstance("SSL");
             sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
-            // Create an ssl socLket factory with our all-trusting manager
+            // Create an ssl socket factory with our all-trusting manager
             final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
 
-            OkHttpClient.Builder builder = client.newBuilder();
-            builder.sslSocketFactory(sslSocketFactory, sharedTrustManager);
+
+
+            OkHttpClient.Builder builder = new OkHttpClient.Builder()
+                    .connectTimeout(0, TimeUnit.MILLISECONDS).readTimeout(0, TimeUnit.MILLISECONDS)
+                    .writeTimeout(0, TimeUnit.MILLISECONDS).cookieJar(new ReactCookieJarContainer());
+            builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
             builder.hostnameVerifier(new HostnameVerifier() {
                 @Override
                 public boolean verify(String hostname, SSLSession session) {
@@ -81,8 +103,10 @@ public class ReactNativeBlobUtilUtils {
                 }
             });
 
-            return builder;
+            OkHttpClient okHttpClient = builder.build();
+            return okHttpClient.newBuilder();
         } catch (Exception e) {
+            Log.e(TAG, e.getMessage());
             throw new RuntimeException(e);
         }
     }

This issue body was partially generated by patch-package.

@RonRadtke
Copy link
Owner

Did you specify an own trust manager based on the docs?
We don't define a general own trust manager but that must be done by yourself in your native code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants