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.

3.1.1 on Android crashes with NPE when trusting all certificates

See original GitHub issue
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[] {
        new X509TrustManager() {
            @Override
            public void checkClientTrusted(final X509Certificate[] chain, final String authType) {
            }

            @Override
            public void checkServerTrusted(final X509Certificate[] chain, final String authType) {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        }
};

// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new SecureRandom());

final OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .sslSocketFactory(sslContext.getSocketFactory())
        .hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(final String hostname, final SSLSession session) {
                return true;
            }
        })
        .build();

This crashes with the following stack trace:

Caused by: java.lang.NullPointerException: Attempt to get length of null array
   at okhttp3.internal.tls.RealTrustRootIndex.<init>(RealTrustRootIndex.java:31)
   at okhttp3.internal.Platform.trustRootIndex(Platform.java:97)
   at okhttp3.internal.Platform$Android.trustRootIndex(Platform.java:271)
   at okhttp3.OkHttpClient.<init>(OkHttpClient.java:189)
   at okhttp3.OkHttpClient.<init>(OkHttpClient.java:60)
   at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:718)

This used to worked with OkHttp 2 and 3.0.1. Now after updating to 3.1.1, our app crashes.

P. S. I know it’s not secure, don’t lecture me. 😉

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
swankjessecommented, Feb 24, 2016

@rmakki does changing getAcceptedIssuers() work?

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
6reactions
JakeWhartoncommented, Feb 9, 2016

Do not return null from getAcceptedIssuers(), return a zero-length array instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Studio 3.1.1, changing fragment causes input method ...
When changing the fragment with EditTexts in an activity,"java.lang.NullPointerException" happens (full stack trace is below).
Read more >
NullPointerException in CertificatePinner.check() · Issue #2337
It's an interesting bug report. It suggests that somehow the certificate isn't trusted, but this happens after a successful TLS handshake If you ......
Read more >
Crashes - Android Developers
An Android app crashes whenever there's an unexpected exit caused by an unhandled exception or signal. An app that is written using Java...
Read more >
3.x Change Log - OkHttp
Fix: Don't crash when building HandshakeCertificates on Android API 28. ... OkHttp now uses @Nullable to annotate all possibly-null values.
Read more >
Firebase Android SDK Release Notes
Learn how to install these SDKs in your app: Add Firebase to your Android Project ... Fixed a NullPointerException crash when instrumenting screen...
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