Java 1.6 TLS 1.2 handshake_failure

I am trying to communicate with a URI that uses TLS 1.2 using java jdk 1.6.30, and I tried setting up the BouncyCastle provider on my system, since TLS 1.2 is not supported by java jdk 1.6.30 by default. I also installed the certificate on my local computer, but I get the following error:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1806)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:986)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1170)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1197)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1181)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1014)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)
    at main.main(main.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

The code I use is as follows:

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.net.*;
import java.security.Security;

public class main {

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    public static void main(String[] args) throws Exception {
        final URL url = new URL("URL");
        final String postData = "POST_DATA";
        final byte[] postDataBytes = postData.getBytes("UTF-8");
        final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        httpURLConnection.setDoOutput(true);
        httpURLConnection.getOutputStream().write(postDataBytes);
        httpURLConnection.getInputStream();
        System.out.println("OK");
    }
}

I tried google, but I did not find any solution ( TLS 1.2 + Java 1.6 + BouncyCastle , Fatal alert received: handshake_failure with Tomcat , etc.).

Can I try to trust the certificate?

Thank you in advance

+4
source share
1 answer

, BouncyCastle 1.60 Java 1.6_65

BouncyCastleJsseProvider provider = new BouncyCastleJsseProvider(new BouncyCastleProvider());
SSLContext context = SSLContext.getInstance("TLS", provider);
TrustAllX509TrustManager manager = new TrustAllX509TrustManager();

context.init(null, new X509TrustManager[] { manager }, new SecureRandom());

URL url = new URL("https://stackoverflow.com:443");

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(context.getSocketFactory());

connection.connect();

, java 1.6 TLS 1.2 https://www.oracle.com/technetwork/java/javase/overview-156328.html#R160_121

0

Source: https://habr.com/ru/post/1624266/


All Articles