gpt4 book ai didi

java - 在 Java 中完成 TLS 握手之前,accept() 是否返回?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:41 26 4
gpt4 key购买 nike

我正在使用 Java 的 SSL 库来保护我的应用程序之间的连接。我注意到即使握手失败,SSLServerSocket.accept() 也会返回一个套接字。

  • 这是否意味着 SSLServerSocket.accept() 不会等到初始握手完成?和
  • 如果没有,我如何等待握手完成并检测握手失败的客户端?或者我可以简单地开始在新的 SSLSocket 上操作,握手会在实际操作之前自动完成吗?

此外,在当前握手完成之前,写入和读取当前正在(重新)握手的 SSLSockets 是否会阻塞?如果不是,在握手套接字上操作是否安全?握手和应用数据会不会并行发送,互不影响?

最佳答案

accept() 不会发起握手,它只是返回已接受的套接字。当您开始在接受的套接字上执行 I/O 时,握手就会启动。这是记录在案的行为:

http://docs.oracle.com/javase/7/docs/api/javax/net/ssl/SSLSocket.html

The initial handshake on this connection can be initiated in one of three ways:

  • calling startHandshake which explicitly begins handshakes, or
  • any attempt to read or write application data on this socket causes an implicit handshake, or
  • a call to getSession tries to set up a session if there is no currently valid session, and an implicit handshake is done.

If handshaking fails for any reason, the SSLSocket is closed, and no futher communications can be done.

...

When SSLSockets are first created, no handshaking is done so that applications may first set their communication preferences: what cipher suites to use, whether the socket should be in client or server mode, etc. However, security is always provided by the time that application data is sent over the connection.

至于握手重新协商,也有文档说明:

http://docs.oracle.com/javase/7/docs/api/javax/net/ssl/SSLSocket.html#startHandshake()

If data has already been sent on the connection, it continues to flow during this handshake. When the handshake completes, this will be signaled with an event. This method is synchronous for the initial handshake on a connection and returns when the negotiated handshake is complete. Some protocols may not support multiple handshakes on an existing socket and may throw an IOException.

关于java - 在 Java 中完成 TLS 握手之前,accept() 是否返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25363641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com