JDK 1.5 SSLHandshakeException

时间:2016-02-22 09:16:58

标签: java sockets ssl sslhandshakeexception jdk1.5

我有一个奇怪的问题,我无法修复。

我通过套接字进行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:连接已关闭:   javax.net.ssl.SSLHandshakeException:远程主机关闭连接   在握手期间   com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1154)     在   com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:65)     在   sun.nio.cs.StreamDecoder $ CharsetSD.readBytes(StreamDecoder.java:411)     在   sun.nio.cs.StreamDecoder $ CharsetSD.implRead(StreamDecoder.java:453)     在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)引起:   javax.net.ssl.SSLHandshakeException:远程主机关闭连接   在握手期间   com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:739)     在   com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1025)     在   com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:619)     在   com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:59)     在   sun.nio.cs.StreamEncoder $ CharsetSE.writeBytes(StreamEncoder.java:336)     在   sun.nio.cs.StreamEncoder $ CharsetSE.implFlushBuffer(StreamEncoder.java:404)     在   sun.nio.cs.StreamEncoder $ CharsetSE.implFlush(StreamEncoder.java:408)     在sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:152)at   java.io.OutputStreamWriter.flush(OutputStreamWriter.java:213)at 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)引起:   java.io.EOFException:SSL对等方未正确关闭   com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:321)     在   com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:720)     ......还有13个

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

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

  

有效期从2月16日星期二 20:07:36 获取2016年至2月16日星期二 20:07:36 GET   2017 1.7正确

     

有效期从2月16日星期二 16:07:36 格林尼治标准时间2016至2月16日星期四 16:07:36 GMT   2017 1.6错误

2 个答案:

答案 0 :(得分:1)

经过大量的研究,我发现,没有办法做到这一点,当然,安装无限制的政策也是一个难看的解决方案。 Sun不建议我们改变政策。解决该问题的最佳方法是始终保持Java版本优于此版本。我不得不写1.5并且没有其他机会简单地升级系统并且决定更糟糕但是唯一的解决方案,当然是有效的。我在同一台机器上使用Java 1.8 + Wildlfy 8.2创建了某种代理服务,使用不同的Jboss端口并从那里调用服务。 1.5和1.8应用程序使用简单的soap协议进行通信。问题“已修复”。

答案 1 :(得分:0)

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

尝试添加系统属性“javax.net.debug = ssl”,以便在系统输出中获得更好的错误说明。例如:

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

或添加命令行参数:

-Djavax.net.debug=ssl

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