java.net.SocketException:连接重置

发布于 2024-11-07 06:48:49 字数 693 浏览 0 评论 0原文

我正在编写一个应用程序,在其中控制客户端和服务器的代码。 我正在使用 SSLSockets 来实现它。 我已经使用普通的不安全套接字运行了该协议,但是当我尝试切换到 SSLSockets(使用完全相同的协议)时,我不断收到以下堆栈跟踪:

java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:168)
        at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:293)
        at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:331)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:782)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:739)

出于某种原因,完全相同的代码可以完美地与不安全的套接字一起运行。为什么会这样呢?

任何反馈将不胜感激。谢谢。

巴勃罗

I am coding an application where I control the code of both the client and the server.
I am using SSLSockets to implement it.
I have the protocol already running with normal unsecured sockets, but when I try to switch to SSLSockets (using exactly the same protocol), I keep getting the following stack trace:

java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:168)
        at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:293)
        at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:331)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:782)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:739)

For some reason, the exact same code works perfectly with unsecured sockets. Why could this be?

Any feedback would be appreciated. Thank you.

Pablo

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

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

发布评论

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

评论(3

总以为 2024-11-14 06:48:49

从您的帖子中不可能检测到问题。
当您切换到安全套接字时,默认情况下会使用最安全的密码。
如果您没有正确配置信任库/密钥库(或者没有启用未经身份验证的套件),那么 SSL 握手将会失败。
异常似乎表明了这一点。
您可以做的是使用 javax.net.debug=ssl,handshake 运行您的程序以启用 SSL 调试信息,并发布调试信息和您的代码(如果您希望有人帮助您)。

From your post it is not possile to detect the problem.
When you switch to secure sockets the most secure ciphers are used by default.
If you have not configured your truststore/keystore correctly (or have not enabled the non-authenticated suites) then the SSL handshake will fail.
The exception seems to indicate that.
What you can do is run your program using javax.net.debug=ssl,handshake to enable SSL debugging info and post the debugging info and your code if you expect someone to help you.

红颜悴 2024-11-14 06:48:49

根据您使用的操作系统,可能需要管理员/根权限才能绑定或侦听 SSL 端口。尝试使用管理员权限(在 Windows 中)或 sudo(在 Linux 上)运行您的应用程序。

Depending on what OS you are using, it may require admin/root priveledges to bind to or listen to the SSL port. Trying running your application with admin rights (in Windows) or sudo'd (on Linux).

倾听心声的旋律 2024-11-14 06:48:49

原因可能有所不同,正如 Vladimir Dyuzhev 所建议的,-Djavax.net.debug=ssl 是你的朋友。

不管怎样,这可能是一个证书问题——确保你有正确的密钥库和信任库。您将需要密钥库中的一个条目:

  • 私钥
  • 证书
  • 完整的证书颁发者链

和信任库:

  • 服务器证书的完整证书链

我在生成正确的密钥库时遇到问题(trustore 很容易 - 只需使用 keytool)。对于密钥库,您需要像这样的(Linux with openssl + java):

# convert all to PEM
openssl x509 -in ${ca}.der -inform DER -outform PEM -out ${ca}.pem
openssl x509 -in ${subca}.der -inform DER -outform PEM -out ${subca}.pem
# create one large PEM file containing certificate chain
cat ${ca}.pem ${subca}.pem > tmp_cert_chain.pem
# generate PKCS#12 BUNDLE
openssl pkcs12 -export -in ${cert}.pem -inkey ${key}.pem -certfile tmp_cert_chain.pem -out tmp_pkcs12.pfx
# convert PKCS#12 bundle to JKS
keytool -importkeystore -srckeystore tmp_pkcs12.pfx -srcstoretype pkcs12 -srcstorepass ${storepass} -destkeystore $keystore -deststoretype jks -deststorepass ${storepass}
# print out JKS keystore
keytool -list -keystore $keystore -storepass $storepass

Reasons can vary, -Djavax.net.debug=ssl is your friend, as suggested by Vladimir Dyuzhev.

Anyway, it may be a certificate problem -- make sure you have correct keystore and trustore. You will require one entry in keystore with:

  • private key
  • certificate
  • complete chain of issuer of the certificate

And a truststore:

  • complete chain of certificates for server certificate

I have problems generating proper keystore (trustore is easy -- just use keytool). For keystore you need st like this (Linux with openssl + java):

# convert all to PEM
openssl x509 -in ${ca}.der -inform DER -outform PEM -out ${ca}.pem
openssl x509 -in ${subca}.der -inform DER -outform PEM -out ${subca}.pem
# create one large PEM file containing certificate chain
cat ${ca}.pem ${subca}.pem > tmp_cert_chain.pem
# generate PKCS#12 BUNDLE
openssl pkcs12 -export -in ${cert}.pem -inkey ${key}.pem -certfile tmp_cert_chain.pem -out tmp_pkcs12.pfx
# convert PKCS#12 bundle to JKS
keytool -importkeystore -srckeystore tmp_pkcs12.pfx -srcstoretype pkcs12 -srcstorepass ${storepass} -destkeystore $keystore -deststoretype jks -deststorepass ${storepass}
# print out JKS keystore
keytool -list -keystore $keystore -storepass $storepass
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文