JAVA HttpsURLConnection - Exception NoSuchAlgorithmException (Cannot recover key)

Viewed 54

I've this code in Java 1.8.0_201 that connect to a url using HttpsURLConnection

            String u = "https://test.com/prova.asmx?WSDL";
            URL url = new URL(u);
            System.out.println("Link: " + u);
            
            HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
            //con.setSSLSocketFactory(sc.getSocketFactory());
            System.out.println("After url connection");
            BufferedReader br = new BufferedReader(new     InputStreamReader(con.getInputStream()));
            String input;
            while ((input = br.readLine()) != null) {
                System.out.println(input);
            }
            br.close();

I launch the program from command line with this comand

java -Djavax.net.ssl.trustStore=ws_cacerts_prod -Djavax.net.ssl.trustStorePassword=changeit -Djavax.net.ssl.keyStore=ws_cacerts_prod -Djavax.net.ssl.keyStorePassword=changeit -Djavax.net.debug=ssl -Djdk.tls.client.protocols=TLSv1 -Dhttps.protocols=TLSv1 TestHTTPS

And i get this exception

Exception in thread "main" java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext)
        at javax.net.ssl.DefaultSSLSocketFactory.throwException(Unknown Source)
        at javax.net.ssl.DefaultSSLSocketFactory.createSocket(Unknown Source)
        at sun.net.www.protocol.https.HttpsClient.createSocket(Unknown Source)
        at sun.net.NetworkClient.doConnect(Unknown Source)
        at sun.net.www.http.HttpClient.openServer(Unknown Source)
        at sun.net.www.http.HttpClient.openServer(Unknown Source)
        at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
        at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
        at TestHTTPS.main(TestHTTPS.java:19)
Caused by: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext)
        at java.security.Provider$Service.newInstance(Unknown Source)
        at sun.security.jca.GetInstance.getInstance(Unknown Source)
        at sun.security.jca.GetInstance.getInstance(Unknown Source)
        at javax.net.ssl.SSLContext.getInstance(Unknown Source)
        at javax.net.ssl.SSLContext.getDefault(Unknown Source)
        at javax.net.ssl.SSLSocketFactory.getDefault(Unknown Source)
        at javax.net.ssl.HttpsURLConnection.getDefaultSSLSocketFactory(Unknown Source)
        at javax.net.ssl.HttpsURLConnection.<init>(Unknown Source)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.<init>(Unknown Source)
        at sun.net.www.protocol.https.Handler.openConnection(Unknown Source)
        at sun.net.www.protocol.https.Handler.openConnection(Unknown Source)
        at java.net.URL.openConnection(Unknown Source)
        at TestHTTPS.main(TestHTTPS.java:18)
Caused by: java.security.UnrecoverableKeyException: Cannot recover key
        at sun.security.provider.KeyProtector.recover(Unknown Source)
        at sun.security.provider.JavaKeyStore.engineGetKey(Unknown Source)
        at sun.security.provider.JavaKeyStore$JKS.engineGetKey(Unknown Source)
        at sun.security.provider.KeyStoreDelegator.engineGetKey(Unknown Source)
        at sun.security.provider.JavaKeyStore$DualFormatJKS.engineGetKey(Unknown Source)
        at java.security.KeyStore.getKey(Unknown Source)
        at sun.security.ssl.SunX509KeyManagerImpl.<init>(Unknown Source)
        at sun.security.ssl.KeyManagerFactoryImpl$SunX509.engineInit(Unknown Source)
        at javax.net.ssl.KeyManagerFactory.init(Unknown Source)
        at sun.security.ssl.SSLContextImpl$DefaultManagersHolder.getKeyManagers(Unknown Source)
        at sun.security.ssl.SSLContextImpl$DefaultManagersHolder.<clinit>(Unknown Source)
        at sun.security.ssl.SSLContextImpl$DefaultSSLContext.<init>(Unknown Source)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        ... 13 more

I try to change all password certificate in keystore as the as the same of keystore but the result is the same. How can i solve this problem?

1 Answers

The error because; your connection didn't route as a https connection. Can you try like this:

SSLContext sc = SSLContext.getInstance("TLSv1.2");
sc.init(null, null, new java.security.SecureRandom());
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setSSLSocketFactory(sc.getSocketFactory());
Related