2
votes

I am trying a demo Android app to connect servlet (both local server and aws instance) it gives Handshake failed error. I have tried with volley and http client also. The relevant code and logcat result is following. Currently I am using Android version 7.1 and redmi 5A cellphone for testing.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        execute();
    }

    void execute() {
        new Thread(new Runnable() {
            public void run() {
                try {
            URL url = new URL("https://192.168.0.7:9999/WebS/welcome/test");
            URLConnection connection = url.openConnection();

            String inputString = "hello server";
            //inputString = URLEncoder.encode(inputString, "UTF-8");

            Log.d("inputString", inputString);

            connection.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
            out.write(inputString);
            out.close();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            Toast.makeText(MainActivity.this, in.toString(), Toast.LENGTH_LONG).show();

            in.close();
        } catch (Exception e) {
            Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
        }
    }
}).start();}}

Logcat result:

app_url E/YOUR_APP_LOG_TAG: I got an error javax.net.ssl.SSLHandshakeException: Handshake failed at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:429) at com.android.okhttp.Connection.connectTls(Connection.java:235) at com.android.okhttp.Connection.connectSocket(Connection.java:199) at com.android.okhttp.Connection.connect(Connection.java:172) at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367) at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:130) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:247) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:257) at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218) at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java) at com.example.cg_dte.app_url.MainActivity$1.run(MainActivity.java:41) at java.lang.Thread.run(Thread.java:760) Suppressed: javax.net.ssl.SSLHandshakeException: Handshake failed ... 15 more Suppressed: javax.net.ssl.SSLHandshakeException: Handshake failed ... 15 more Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7fa2258640: Failure in SSL library, usually a protocol error error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER (external/boringssl/src/ssl/tls_record.c:192 0x7f94590e7e:0x00000000) at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method) at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:357) ... 14 more Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7fa2258640: Failure in SSL library, usually a protocol error error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER (external/boringssl/src/ssl/tls_record.c:192 0x7f94590e7e:0x00000000) at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method) at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:357) ... 14 more Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7fa2258640: Failure in SSL library, usually a protocol error error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER (external/boringssl/src/ssl/tls_record.c:192 0x7f94590e7e:0x00000000) at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method) at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:357) ... 14 more

2
Try adding -Djavax.net.debug=all parameter to get the detailed information about SSL connection process.kaos

2 Answers

1
votes
URL url = new URL("https://192.168.0.7:9999/WebS/welcome/test");

This url contains also the port specification (port 9999). Make sure your SSL server instance (HTTPS protocol) is configured to listen at that port, maybe you are by mistake connecting to the non-SSL instance of your server (HTTP protocol).

Try for example insecure URL url = new URL("http://192.168.0.7:9999/WebS/welcome/test"); to see if the communication works with HTTP protocol on that address. If yes, then you need to connect to different port for HTTPS. The easiest bet is to try first with default SSL port (443), i.e. just remove the port number: URL url = new URL("https://192.168.0.7/WebS/welcome/test");

You can also try all these varianst of url in your favourite browser to see what it does think about it (I'm using personally firefox, the url with port pointing to HTTP did produce weird errors about wrong certificate length, etc... once I fixed my url to point to the HTTPS instance, the firefox did report only insecure connection due to self-signed certificate used, which was expected and understandable.

The correct HTTPS url, without further extra configuration of mobile app, will probably fail with javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. - if you are using self-signed certificate for your local server. Which is different problem and there's lot of documentation how to deal with that (and overall how to pin certificates, check domain names and create secure connection).

But the WRONG_VERSION_NUMBER (external/boringssl/src/ssl/tls_record.c: suggest you are connecting by accident to the unencrypted HTTP instance of your server, then the SSL handshake is completely confused.

-3
votes

I had the same issue and I was able to solve it by removing 's' from the url.

Please change the url to

URL url = new URL("http://192.168.0.7:9999/WebS/welcome/test");