1

I've been searching for a solution for this issue for a while, but i just can't seem to figure it out

I am trying to use ssl over http connection between an android app and the server, I've created a keystore with my self signed certificate

i have this code :

    URL url;
    SSLContext sslContext = null;
    KeyStore keyStore = KeyStore.getInstance("BKS");    
    InputStream input = SportsApplication.getContext().getResources().openRawResource(R.raw.mykeystore);
    keyStore.load(input, "mypass".toCharArray());
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
    kmf.init(keyStore, "mypass".toCharArray());
    KeyManager[] keyManagers = kmf.getKeyManagers();
    sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyManagers, null, null);

    HttpsURLConnection connection = null;
    String urlParameters = mJObject.toString();
    try {
        Log.w("client", mUrl);
        Log.v("client", "request: "+urlParameters);
        // Create connection
        mUrl = mUrl.replaceFirst("http", "https");
        url = new URL(mUrl);
        connection = (HttpsURLConnection) url.openConnection();
        try{

            connection.setSSLSocketFactory(sslContext.getSocketFactory());
        }
        catch (Exception e1) {
            e1.printStackTrace();
            int tx =0;
        }
        try{
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        connection.setReadTimeout(dataTimeout);
        connection.setConnectTimeout(com.inmobly.buckeyes.client.Constants.DEFAULT_CONN_TIMEOUT);

        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        }
        catch (Exception e1) {
            e1.printStackTrace();
            int tx =0;
        }
        // Send request
        DataOutputStream wr = null;
        try{
         wr = new DataOutputStream(connection.getOutputStream());
        }
        catch (Exception e1) {
            e1.printStackTrace();
            int tx =0;
        }

but i keep getting this exception:

javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x1b1b0e0: Failure in SSL library, usually a protocol errorerror:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:683 0x4029bcf5:0x00000000)

what am i doing wrong ?

Yasmin Reda
  • 385
  • 1
  • 6
  • 18

2 Answers2

1

since the SSL handshake itself is not going through, instead of debugging it using the information in the exception can you install wireshark on ur system (it this is reproducible on simulator as well) and take a look at the packet capture at the os level. If the error is because of the protocol version mismatch between ssl implementations at server and client, packets should have the necessary information. You can try a simple ssl handshake first to make sure handshake happens properly with the server. Let us know what you info you get and we will debug futher!

  • 1
    always happy to see such comments :) keep rockin. – Rajarajan Pudupatti Sundari Je Jul 08 '15 at 13:23
  • @Bhavit Can you share what bug was that. I had same issue on lower version of android. Implemented in NoSSlFactory from SO. But still not worked. – Vindhya Pratap Singh Jun 27 '17 at 05:41
  • @VindhyaPratapSingh I wrote an answer about that bug here : https://stackoverflow.com/questions/29916962/javax-net-ssl-sslhandshakeexception-javax-net-ssl-sslprotocolexception-ssl-han/30302235#30302235 – Bhavit S. Sengar Jun 28 '17 at 05:58
  • Thanks @Bhavita, My problem was fixed by changing SSL Protocols on server side. My Device was having API level 15, as described in docs it can only support TLSv1. That protocol was disabled on serverside SSL Certificate. So after making it enabled. Its working fine. – Vindhya Pratap Singh Jun 28 '17 at 06:23
0

Refer to my answer at How to disable SSLv3 in HttpsUrlConnection in Android

Community
  • 1
  • 1
Bhavit S. Sengar
  • 8,794
  • 6
  • 24
  • 34