question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

okhttp 4.3.0 crash with custom X509TrustManager

See original GitHub issue

Android 10 specific.

Custom X509TrustManager in use, extending from X509ExtendedTrustManager, installed with OkHttpClient.Builder#sslSocketFactory().

Updating okhttp from 4.2.2 to 4.3.0 causes a crash when installing the trust manager:

java.lang.IllegalArgumentException: Required method checkServerTrusted(X509Certificate[], String, String, String) missing        
        at android.net.http.X509TrustManagerExtensions.<init>(X509TrustManagerExtensions.java:71)
        at okhttp3.internal.platform.android.Android10CertificateChainCleaner.<init>(Android10CertificateChainCleaner.kt:36)
        at okhttp3.internal.platform.Android10Platform.buildCertificateChainCleaner(Android10Platform.kt:62)
        at okhttp3.internal.tls.CertificateChainCleaner$Companion.get(CertificateChainCleaner.kt:42)
        at okhttp3.OkHttpClient$Builder.sslSocketFactory(OkHttpClient.kt:735)

Looking up the commits this seems to be introduced by https://github.com/square/okhttp/commit/a41361efcb0d4ed2e7f09313c9e9fcc3d72e837b that cut down on reflective calls on the trust manager.

Fixed this by adding an implementation of the missing method in the custom trust manager. Note the Android platform exception message is off-by-one String arg.

        private X509ExtendedTrustManager delegate;
        private Method checkServerTrusted;

        CustomExtendedTrustManager(@NonNull X509ExtendedTrustManager delegate) {
            this.delegate = delegate;

            try {
                checkServerTrusted = delegate.getClass().getMethod("checkServerTrusted",
                        X509Certificate[].class,
                        String.class,
                        String.class);
            } catch (NoSuchMethodException ignored) {
            }
        }

        @SuppressWarnings("unused")
        @Keep
        public List<X509Certificate> checkServerTrusted(X509Certificate[] chain, String authType, String host) throws CertificateException {
            if (checkServerTrusted == null) {
                throw new CertificateException("checkServerTrusted(X509Certificate[], String, String) not implemented in delegate");
            }

            List<X509Certificate> list;
            try {
                //noinspection unchecked
                list = (List<X509Certificate>) checkServerTrusted.invoke(delegate, chain, authType, host);
            } catch (IllegalAccessException e) {
                throw new CertificateException("Failed to call checkServerTrusted", e);
            } catch (InvocationTargetException e) {
                if (e.getCause() instanceof CertificateException) {
                    throw (CertificateException) e.getCause();
                }
                if (e.getCause() instanceof RuntimeException) {
                    throw (RuntimeException) e.getCause();
                }
                throw new CertificateException("checkServerTrusted failed", e.getCause());
            }

            return list;
        }

With this in place the app is no longer crashing and not really expecting okhttp to change here. Posting this issue as a reference to others who might have the same issue.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
swankjessecommented, Jan 3, 2020

@laalto this is one of the nicest issue reports I’ve seen. Thanks!

0reactions
laaltocommented, Jan 7, 2020

Thanks, much appreciated!

Read more comments on GitHub >

github_iconTop Results From Across the Web

4.x Change Log - OkHttp
This regression was introduced in OkHttp 4.3.0. Fix: Don't crash with an IllegalArgumentException when using custom trust managers on Android 10.
Read more >
How to disable SSL verification? - android - Stack Overflow
Update 1: Ensure you are using okhttp with version 4.3.1 or above. You need to add the following to the proguard file inorder...
Read more >
Diff - platform/external/okhttp - Google Git
This + prevents a potential crash when using certificate pinning with the ... + +To log to a custom location, pass a `Logger`...
Read more >
Trusting All Certificates in OkHttp | Baeldung
Let's create our array of TrustManager containing a single X509TrustManager that disables the default certificate validations by overriding ...
Read more >
JDK-8068884 Reliable, reproducable SIGSEGV - Bug ID
EXPECTED VERSUS ACTUAL BEHAVIOR : VM Crash. replay_pid30617.log JvmtiExport ... instanceKlass org/hibernate/loader/custom/NonScalarReturn instanceKlass ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found