JDK 1.5 SSLHandshakeException

JDK 1.5 SSLHandshakeException

我有一个奇怪的问题,我无法解决。

我有 JDK 1.5 版本和基于 SSL 的套接字通信,只需发送和接收字符串数据。

try {

    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(new FileInputStream(
            "path_to_.jks"),
            "secret_of_jks".toCharArray());

    TrustManagerFactory tmf = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(ks);
    KeyManagerFactory kmf = KeyManagerFactory
            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(ks, "secret_of_jks".toCharArray());
    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    Socket s = ctx.getSocketFactory().createSocket("address_of_server", PORT);

    String jsonEx = "json text to send server";
    StringBuilder sb = new StringBuilder();
    sb.append(jsonEx.getBytes().length);
    sb.append("\r\n");
    sb.append(jsonEx);


    PrintWriter writer = new PrintWriter(s.getOutputStream(), true);
    writer.println(sb.toString());
    BufferedReader in =  new BufferedReader(new InputStreamReader(s.getInputStream()));
    System.out.println(in.readLine());
    writer.flush();
} catch (Exception e) {
    e.printStackTrace();
}

当我使用 JDK 1.7+ 时一切正常,但是当我切换到 1.6- 时它会抛出 javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake

我的证书是 2048 加密的,我还安装了 JCE Unlimited Strength Jurisdiction Policy http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html

如果有人感兴趣,这里是完整的例外:

javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1154) at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:65) at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:411) at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:453) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:183) at java.io.InputStreamReader.read(InputStreamReader.java:167) at java.io.BufferedReader.fill(BufferedReader.java:136) at java.io.BufferedReader.readLine(BufferedReader.java:299) at java.io.BufferedReader.readLine(BufferedReader.java:362) at ConnectorTest.main(ConnectorTest.java:45) Caused by: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:739) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1025) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:619) at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:59) at sun.nio.cs.StreamEncoder$CharsetSE.writeBytes(StreamEncoder.java:336) at sun.nio.cs.StreamEncoder$CharsetSE.implFlushBuffer(StreamEncoder.java:404) at sun.nio.cs.StreamEncoder$CharsetSE.implFlush(StreamEncoder.java:408) at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:152) at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:213) at java.io.BufferedWriter.flush(BufferedWriter.java:236) at java.io.PrintWriter.newLine(PrintWriter.java:410) at java.io.PrintWriter.println(PrintWriter.java:559) at java.io.PrintWriter.println(PrintWriter.java:670) at ConnectorTest.main(ConnectorTest.java:43) Caused by: java.io.EOFException: SSL peer shut down incorrectly at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:321) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:720) ... 13 more

ConnectorTest Line 43 is

System.out.println(in.readLine());

已更新

trigger seeding of SecureRandom
done seeding SecureRandom
Allow unsafe renegotiation: false
Allow legacy hello messages: true
Is initial handshake: true
Is secure renegotiation: false
%% No cached client session
*** ClientHello, TLSv1
RandomCookie:  GMT: 1439443814 bytes = { 228, 36, 73, 128, 109, 225, 11, 36, 62, 40, 147, 150, 27, 145, 150, 163, 244, 28, 97, 56, 188, 81, 117, 31, 235, 60, 101, 224 }
Session ID:  {}
Cipher Suites: [SSL_RSA_WITH_RC4_128_MD5, SSL_RSA_WITH_RC4_128_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_DES_CBC_SHA, SSL_DHE_RSA_WITH_DES_CBC_SHA, SSL_DHE_DSS_WITH_DES_CBC_SHA, SSL_RSA_EXPORT_WITH_RC4_40_MD5, SSL_RSA_EXPORT_WITH_DES40_CBC_SHA, SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA, SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA, TLS_EMPTY_RENEGOTIATION_INFO_SCSV]
Compression Methods:  { 0 }
***
main, WRITE: TLSv1 Handshake, length = 75
main, WRITE: SSLv2 client hello message, length = 101
main, received EOFException: error
main, handling exception: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
main, SEND TLSv1 ALERT:  fatal, description = handshake_failure
main, WRITE: TLSv1 Alert, length = 2
main, called closeSocket()

更新 2

我刚刚发现,它们之间的区别是:

Valid from Tue Feb 16 20:07:36 GET 2016 until Thu Feb 16 20:07:36 GET 2017 1.7 Correct

Valid from Tue Feb 16 16:07:36 GMT 2016 until Thu Feb 16 16:07:36 GMT 2017 1.6 Error

可能是服务器不支持客户端的 SSL 版本(客户端提供的 SSL 版本太低)。

尝试添加系统 属性 "javax.net.debug=ssl" 以便在系统输出中获得更好的错误描述。例如:

System.setProperty("javax.net.debug", "ssl");

或添加命令行参数:

-Djavax.net.debug=ssl

为什么要使用旧的 Java?如果您必须使用 1.6,请尝试将其更新到最新的补丁版本。

经过大量研究,我发现没有办法做到这一点,当然,安装无限策略也是丑陋的解决方案。 Sun 不建议我们更改策略。解决该问题的最佳方法是,始终将 Java 版本维护得比这个更好。我不得不在 1.5 上编写并且没有其他机会简单地升级系统并决定更糟但唯一有效的解决方案,当然。我在具有不同 Jboss 端口的同一台机器上使用 Java 1.8 + Wildlfy 8.2 创建了某种代理服务,并从那里调用服务。 1.5 和 1.8 应用程序使用简单的 soap 协议进行通信。问题 "fixed".