尝试从 SSLSocket 读取时收到 EOFException

发布于 2024-08-27 21:08:04 字数 3402 浏览 1 评论 0原文

我正在开发一个 SSL 客户端,它将向 SSL 服务器发出一个简单的请求并等待响应。 SSL 握手和写入正常,但我无法从套接字读取数据。我打开 java.net.ssl 的调试并得到以下信息:

[..] 
main, READ: TLSv1 Change Cipher Spec, length = 1 
[Raw read]: length = 5 0000: 16 03 01 00 20                 .... 
[Raw read]: length = 32 [..] 
main, READ: TLSv1 Handshake, length = 32 Padded plaintext after DECRYPTION:  len = 32 
[..]
    *** Finished verify_data:  { 29, 1, 139, 226, 25, 1, 96, 254, 176, 51, 206, 35 }
    *** %% Didn't cache non-resumable client session: [Session-1, SSL_RSA_WITH_RC4_128_MD5] [read] MD5 and SHA1 hashes:  len = 16 0000: 14 00 00 0C 1D 01 8B E2   19 01 60 FE B0 33 CE 23  ..........`..3.# Padded plaintext before ENCRYPTION:  len = 70 [..]                               a.j.y. 
main, WRITE: TLSv1 Application Data, length = 70 
[Raw write]: length = 75 
[..] 
Padded plaintext before ENCRYPTION:  len = 70 
[..] 
main, WRITE: TLSv1 Application Data, length = 70 
[Raw write]: length = 75 
[..] 
main, received EOFException: ignored main, called closeInternal(false) main, SEND TLSv1 ALERT:  warning, description = close_notify Padded plaintext before ENCRYPTION:  len = 18 [..] 
ain, WRITE: TLSv1 Alert, length = 18 [Raw write]: length = 23 
[..] main, called close() 
main, called closeInternal(true) 
main, called close() 
main, called closeInternal(true)

[..] 是证书链。

这是一个代码片段:

try {
            System.setProperty("javax.net.debug","all");
            /*
             * Set up a key manager for client authentication
             * if asked by the server.  Use the implementation's
             * default TrustStore and secureRandom routines.
             */
            SSLSocketFactory factory = null;
            try {
            SSLContext ctx;
            KeyManagerFactory kmf;
            KeyStore ks;
            char[] passphrase = "importkey".toCharArray();

            ctx = SSLContext.getInstance("TLS");
            kmf = KeyManagerFactory.getInstance("SunX509");
            ks = KeyStore.getInstance("JKS");

            ks.load(new FileInputStream("keystore.jks"), passphrase);

            kmf.init(ks, passphrase);
            ctx.init(kmf.getKeyManagers(), null, null);

            factory = ctx.getSocketFactory();
            } catch (Exception e) {
                throw new IOException(e.getMessage());
            }

            SSLSocket socket = (SSLSocket)factory.createSocket("server ip", 9999);

            /*
             * send http request
             *
             * See SSLSocketClient.java for more information about why
             * there is a forced handshake here when using PrintWriters.
             */
            SSLSession session = socket.getSession();

            [build query]

            byte[] buff = query.toWire();

            out.write(buff);
            out.flush();

            InputStream input = socket.getInputStream();

            int readBytes = -1;
            int randomLength = 1024;
            byte[] buffer  = new byte[randomLength];
            while((readBytes = input.read(buffer, 0, randomLength)) != -1) {
                LOG.debug("Read: " + new String(buffer));
            }
            input.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

我可以多次写入,并且没有收到任何错误,但在第一次读取时发生 EOFException。

我的套接字或 SSL 身份验证是否有问题?

谢谢。

I am developing a SSL client that will do a simple request to a SSL server and wait for the response. The SSL handshake and the writing goes OK but I can't READ data from the socket. I turned on the debug of java.net.ssl and got the following:

[..] 
main, READ: TLSv1 Change Cipher Spec, length = 1 
[Raw read]: length = 5 0000: 16 03 01 00 20                 .... 
[Raw read]: length = 32 [..] 
main, READ: TLSv1 Handshake, length = 32 Padded plaintext after DECRYPTION:  len = 32 
[..]
    *** Finished verify_data:  { 29, 1, 139, 226, 25, 1, 96, 254, 176, 51, 206, 35 }
    *** %% Didn't cache non-resumable client session: [Session-1, SSL_RSA_WITH_RC4_128_MD5] [read] MD5 and SHA1 hashes:  len = 16 0000: 14 00 00 0C 1D 01 8B E2   19 01 60 FE B0 33 CE 23  ..........`..3.# Padded plaintext before ENCRYPTION:  len = 70 [..]                               a.j.y. 
main, WRITE: TLSv1 Application Data, length = 70 
[Raw write]: length = 75 
[..] 
Padded plaintext before ENCRYPTION:  len = 70 
[..] 
main, WRITE: TLSv1 Application Data, length = 70 
[Raw write]: length = 75 
[..] 
main, received EOFException: ignored main, called closeInternal(false) main, SEND TLSv1 ALERT:  warning, description = close_notify Padded plaintext before ENCRYPTION:  len = 18 [..] 
ain, WRITE: TLSv1 Alert, length = 18 [Raw write]: length = 23 
[..] main, called close() 
main, called closeInternal(true) 
main, called close() 
main, called closeInternal(true)

The [..] are the certificate chain.

Here is a code snippet:

try {
            System.setProperty("javax.net.debug","all");
            /*
             * Set up a key manager for client authentication
             * if asked by the server.  Use the implementation's
             * default TrustStore and secureRandom routines.
             */
            SSLSocketFactory factory = null;
            try {
            SSLContext ctx;
            KeyManagerFactory kmf;
            KeyStore ks;
            char[] passphrase = "importkey".toCharArray();

            ctx = SSLContext.getInstance("TLS");
            kmf = KeyManagerFactory.getInstance("SunX509");
            ks = KeyStore.getInstance("JKS");

            ks.load(new FileInputStream("keystore.jks"), passphrase);

            kmf.init(ks, passphrase);
            ctx.init(kmf.getKeyManagers(), null, null);

            factory = ctx.getSocketFactory();
            } catch (Exception e) {
                throw new IOException(e.getMessage());
            }

            SSLSocket socket = (SSLSocket)factory.createSocket("server ip", 9999);

            /*
             * send http request
             *
             * See SSLSocketClient.java for more information about why
             * there is a forced handshake here when using PrintWriters.
             */
            SSLSession session = socket.getSession();

            [build query]

            byte[] buff = query.toWire();

            out.write(buff);
            out.flush();

            InputStream input = socket.getInputStream();

            int readBytes = -1;
            int randomLength = 1024;
            byte[] buffer  = new byte[randomLength];
            while((readBytes = input.read(buffer, 0, randomLength)) != -1) {
                LOG.debug("Read: " + new String(buffer));
            }
            input.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

I can write multiple times and I don't get any error but the EOFException happens on the first read.

Am I doing something wrong with the socket or with the SSL authentication?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2024-09-03 21:08:04

问题出在我发送的数据包上。服务器正在获取数据包,检查其格式是否错误并断开连接。修复数据包格式解决了问题。

The problem was with the packet I was sending. The server was getting the packet, checking that it was badly formatted and dropping the connection. Fixing the packet format fixed the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文