javaandroidsslhttpurlconnection

Android System.err: javax.net.ssl.SSLHandshakeException: Handshake failed


Can someone advise me on how to solve SSLHandshakeException in Android? See below the error I keep getting for this one URL that I am trying to access using HttpURLConnection object.

W/bile.allfundin: Accessing hidden field Ljava/net/Socket;->impl:Ljava/net/SocketImpl; (light greylist, reflection)
W/bile.allfundin: Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (light greylist, linking)
W/bile.allfundin: Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (light greylist, linking)
W/bile.allfundin: Accessing hidden field Ljava/io/FileDescriptor;->descriptor:I (light greylist, JNI)
I/DynamiteModule: Considering local module com.google.android.gms.ads.dynamite:0 and remote module com.google.android.gms.ads.dynamite:21001
    Selected remote version of com.google.android.gms.ads.dynamite, version >= 21001
D/DynamitePackage: Instantiated singleton DynamitePackage.
    Instantiating com.google.android.gms.ads.ChimeraMobileAdsSettingManagerCreatorImpl
I/Ads: Updating ad debug logging enablement.
V/NativeCrypto: SSL handshake aborted: ssl=0x78ce68f9c8: Failure in SSL library, usually a protocol error
    error:1000042e:SSL routines:OPENSSL_internal:TLSV1_ALERT_PROTOCOL_VERSION (third_party/openssl/boringssl/src/ssl/tls_record.cc:592 0x78ce688688:0x00000001)
W/bile.allfundin: Accessing hidden method Ldalvik/system/BlockGuard;->getThreadPolicy()Ldalvik/system/BlockGuard$Policy; (light greylist, linking)
W/bile.allfundin: Accessing hidden method Ldalvik/system/BlockGuard$Policy;->onNetwork()V (light greylist, linking)
    Accessing hidden method Ldalvik/system/CloseGuard;->close()V (light greylist, linking)
I/WebViewFactory: Loading com.android.chrome version 79.0.3945.93 (code 394509337)
I/WebViewFactory: SystemProperties GMSVERSION = 9_201910
    Get WebViewChromiumFactoryProviderForO
I/cr_LibraryLoader: Time to load native libraries: 5 ms
V/NativeCrypto: SSL handshake aborted: ssl=0x78ce68f9c8: Failure in SSL library, usually a protocol error
    error:1000042e:SSL routines:OPENSSL_internal:TLSV1_ALERT_PROTOCOL_VERSION (third_party/openssl/boringssl/src/ssl/tls_record.cc:592 0x78ce688688:0x00000001)
W/System.err: javax.net.ssl.SSLHandshakeException: Handshake failed
W/System.err:     at com.google.android.gms.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(:com.google.android.gms@19831029@19.8.31 (100406-284611645):38)
        at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:192)
        at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:149)
        at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
        at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
        at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
W/System.err:     at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26)
        at com.techlinemobile.allfunding.utilities.NetworkUtils.getResponseFromHttpUrl(NetworkUtils.java:55)
        at com.techlinemobile.allfunding.MainActivity$scholarshipQueryTask.doInBackground(MainActivity.java:92)
W/System.err:     at com.techlinemobile.allfunding.MainActivity$scholarshipQueryTask.doInBackground(MainActivity.java:84)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
W/System.err: Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x78ce68f9c8: Failure in SSL library, usually a protocol error
    error:1000042e:SSL routines:OPENSSL_internal:TLSV1_ALERT_PROTOCOL_VERSION (third_party/openssl/boringssl/src/ssl/tls_record.cc:592 0x78ce688688:0x00000001)
        at com.google.android.gms.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
        at com.google.android.gms.org.conscrypt.NativeSsl.doHandshake(:com.google.android.gms@19831029@19.8.31 (100406-284611645):6)
        at com.google.android.gms.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(:com.google.android.gms@19831029@19.8.31 (100406-284611645):14)
        ... 22 more
I/chromium: [INFO:library_loader_hooks.cc(51)] Chromium logging enabled: level = 0, default verbosity = 0
I/cr_LibraryLoader: Expected native library version number "79.0.3945.93", actual native library version number "79.0.3945.93"
W/cr_ChildProcLH: Create a new ChildConnectionAllocator with package name = com.android.chrome, sandboxed = true

See the code of my method below

 public static String getResponseFromHttpUrl(URL url) throws IOException {
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        try {
            InputStream in = urlConnection.getInputStream();
            Scanner scanner = new Scanner(in);
            scanner.useDelimiter("\\A");

            boolean hasInput = scanner.hasNext();
            if (hasInput) {
                return scanner.next();
            } else {
                return null;
            }
        } finally {
            urlConnection.disconnect();
        }
    }

Solution

  • I can't take any credit for this article but this will show you what packages need to be installed to fix the TLS/SSLv3 issue.

    The other thing that you may need to do is add

    <application
      android:usesCleartextTraffic="true"
      ...
    >
    

    to your AndroidManifest.xml, which is something that I needed to do during unit tests.