0

I know that there are a lot of example of this problem, but I can't understand what's wrong with my code. I need to perform a volley request, but I get this:

com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x79d4ef38: Failure in SSL library, usually a protocol error
    error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:744 0x726387e8:0x00000000)

I make a new request by:

RequestQueue queue = Volley.newRequestQueue(context, new HurlStack(null, getSocketFactory()));
.....

 private SSLSocketFactory getSocketFactory() {
        CertificateFactory cf = null;
        try {
            cf = CertificateFactory.getInstance("X.509");
            InputStream caInput = context.getResources().openRawResource(R.raw.name);
            Certificate ca;
            try {
                ca = cf.generateCertificate(caInput);
                Log.e("CERT", "ca=" + ((X509Certificate) ca).getSubjectDN());
            } finally {
                caInput.close();
            }

            String keyStoreType = KeyStore.getDefaultType();
            KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);

            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
            tmf.init(keyStore);

            HostnameVerifier hostnameVerifier = new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    Log.e("CipherUsed", session.getCipherSuite());
                    return hostname.compareTo("hostname")==0; //The Hostname of your server.//
                }
            };

            HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, tmf.getTrustManagers(), null);
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());

            SSLSocketFactory sf = context.getSocketFactory();
            return sf;

        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }

        return null;
    }

And this is the code that I found. It seems that will work but for me not. Is there something missing? I don't want to trust all certificates.

Simone
  • 311
  • 4
  • 16
  • checkout this https://stackoverflow.com/questions/32184787/com-android-volley-noconnectionerror-javax-net-ssl-sslhandshakeexception-javax – Ankit Sep 14 '19 at 12:45
  • Also tried this but nothing change – Simone Sep 14 '19 at 13:28
  • try setting this code queue.setRetryPolicy(new DefaultRetryPolicy(0, -1, 0)); to solve timeout or noconnection issue – Ankit Sep 14 '19 at 13:30
  • Thank you but no change. I noticed that I've got this error on android version 4.4 but not in android version 7> – Simone Sep 14 '19 at 13:37
  • This is the complete volley error: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7a191210: Failure in SSL library, usually a protocol error error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:744 0x726387e8:0x00000000) – Simone Sep 14 '19 at 13:49
  • 1
    your problem is related with this https://stackoverflow.com/questions/29916962/javax-net-ssl-sslhandshakeexception-javax-net-ssl-sslprotocolexception-ssl-han – Ankit Sep 14 '19 at 13:52

0 Answers0