SSLProtocolException: Read error: ssl=0x9af236c0: Failure in SSL library, usually a protocol error in android emulator

0

I am using the following method to read the stream.

public static String readStream(InputStream inputStream) {
    try {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        int i = inputStream.read();
        while (i != -1) {
            bo.write(i);
            i = inputStream.read();
        }
        return bo.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
}

I am getting the following Exception at this line

int i = inputStream.read();

Exception:

javax.net.ssl.SSLProtocolException: Read error: ssl=0x9af236c0: Failure in SSL library, usually a protocol error
2019-03-15 16:25:25.978 5367-5389/com.healics.myhealics W/System.err: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT (external/boringssl/src/crypto/cipher/e_aes.c:1143 0x989b8e9f:0x00000000)
2019-03-15 16:25:25.978 5367-5389/com.healics.myhealics W/System.err: error:1000008b:SSL routines:OPENSSL_internal:DECRYPTION_FAILED_OR_BAD_RECORD_MAC (external/boringssl/src/ssl/tls_record.c:277 0x989b8e9f:0x00000000)
2019-03-15 16:25:25.979 5367-5389/com.healics.myhealics W/System.err:     at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
2019-03-15 16:25:25.980 5367-5389/com.healics.myhealics W/System.err:     at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:741)
2019-03-15 16:25:25.980 5367-5389/com.healics.myhealics W/System.err:     at com.android.okhttp.okio.Okio$2.read(Okio.java:136)
2019-03-15 16:25:25.980 5367-5389/com.healics.myhealics W/System.err:     at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
2019-03-15 16:25:25.980 5367-5389/com.healics.myhealics W/System.err:     at com.android.okhttp.okio.RealBufferedSource.read(RealBufferedSource.java:50)
2019-03-15 16:25:25.980 5367-5389/com.healics.myhealics W/System.err:     at com.android.okhttp.internal.http.Http1xStream$FixedLengthSource.read(Http1xStream.java:393)
2019-03-15 16:25:25.980 5367-5389/com.healics.myhealics W/System.err:     at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:371)
2019-03-15 16:25:25.981 5367-5389/com.healics.myhealics W/System.err:     at java.io.BufferedInputStream.fill(BufferedInputStream.java:248)
2019-03-15 16:25:25.982 5367-5389/com.healics.myhealics W/System.err:     at java.io.BufferedInputStream.read(BufferedInputStream.java:267)
2019-03-15 16:25:25.983 5367-5389/com.healics.myhealics W/System.err:     at com.interrahealth.i3user.util.StreamReader.readStream(StreamReader.java:18)

Now, this is happening only on the emulator when I run the code in the real android device it does not happen. This is an intermittent issue sometimes coming not every time.

Adding the code

I have not added any code which sets up the SSL/TLS settings below is my code

private static String mGetResponseFromNetworkRequest(String userPass, String apiEndpoint, String
            outputStreamBytes, String requestMethod, String contentType) {
        long startTime = System.currentTimeMillis();
        String response = "";
        Logger.d("apiEndpoint: " + apiEndpoint);
        try {
            HttpURLConnection httpURLConnection = getHttpUrlConnectionWithDigestAuth(apiEndpoint, requestMethod);
            httpURLConnection.setRequestMethod(requestMethod);
            httpURLConnection.setRequestProperty("Content-Type", contentType);
            httpURLConnection.setRequestProperty("Content-Language", "en-US");
            httpURLConnection.setRequestProperty("charset", "utf-8");
            httpURLConnection.addRequestProperty("User-Agent", "XYZ");
            httpURLConnection.addRequestProperty("Full-App-Version", BuildConfig.VERSION_NAME);
            httpURLConnection.addRequestProperty("Platform", "Android");
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);

            DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
            dataOutputStream.writeBytes(outputStreamBytes);

            dataOutputStream.flush();
            dataOutputStream.close();

            Logger.d("dataOutputStream: " + outputStreamBytes);
            Logger.d("Response Code: " + httpURLConnection.getResponseCode());
            Logger.d("httpURLConnection.getHeaderFields(): " + httpURLConnection.getHeaderFields().toString());
            Logger.d("httpURLConnection: " + httpURLConnection.toString());


            // WAS THROWING ERRORS
            if (httpURLConnection.getResponseCode() == 500) {
                return "500";
            } else {
                InputStream inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
                response = StreamReader.readStream(inputStream);
                httpURLConnection.disconnect();
            }

            Logger.d("Result: " + response);

        } catch (MalformedInputException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("mGetResponseFromNetworkRequest", "Connection Time = " + (System.currentTimeMillis() - startTime));
        return response;
    }
android
emulation
asked on Stack Overflow Mar 15, 2019 by Mahesh Marathe • edited Mar 18, 2019 by Mahesh Marathe

1 Answer

0

You need to set the setSSLSocketFactory on your HttpsURLConnection like so:

 httpURLConnection.setSSLSocketFactory(new MyFactory());

In Android API level 16+ TLS 1.1 and 1.2 are not enabled by default so it needs to be enabled. Above we are using a class called MyFactory which is where you will set the SSLContext. Here is a link to show you what you need to do https://blog.dev-area.net/2015/08/13/android-4-1-enable-tls-1-1-and-tls-1-2/

answered on Stack Overflow Mar 18, 2019 by James Blackburn

User contributions licensed under CC BY-SA 3.0