繁体   English   中英

正确处理 SSL_shutdown

[英]Handling SSL_shutdown correctly

SSL_shutdown上的 OpenSSL 文档指出:

因此,如果双向关闭尚未完成(第一次调用的返回值为 0),建议检查SSL_shutdown()的返回值并再次调用SSL_shutdown() )。

https://www.openssl.org/docs/ssl/SSL_shutdown.html

我在下面有一个代码片段,我在其中检查SSL_shutdown返回值 0 并再次调用它,我一直在使用它。 我的问题是,在第二次调用时忽略SSL_shutdown的返回值是否可以,或者我们应该继续重试SSL_shutdown直到返回 1(双向关闭完成)。

int r = SSL_shutdown(ssl);
//error handling here if r < 0 
if(!r)
{
    shutdown(fd,1);
    SSL_shutdown(ssl); //how should I handle return value and error handling here is it required?? 
}
SSL_free(ssl);
SSLMap.erase(fd);
shutdown(fd,2);
close(fd);

openssl有点黑暗艺术。

首先,您引用的页面对返回值进行了 HTML 化处理。 这是手册页的实际内容

  RETURN VALUES

   The following return values can occur:

   0   The shutdown is not yet finished. Call SSL_shutdown() for a second
       time, if a bidirectional shutdown shall be performed.  The output
       of SSL_get_error(3) may be misleading, as an erroneous
       SSL_ERROR_SYSCALL may be flagged even though no error occurred.

   1   The shutdown was successfully completed. The "close notify" alert
       was sent and the peer's "close notify" alert was received.

   -1  The shutdown was not successful because a fatal error occurred
       either at the protocol level or a connection failure occurred. It
       can also occur if action is need to continue the operation for non-
       blocking BIOs.  Call SSL_get_error(3) with the return value ret to
       find out the reason.

如果您有阻塞 BIO,事情就相对简单了。 第一次调用时为 0 意味着如果您想要完全双向关闭,则需要再次调用SSL_shutdown 基本上这意味着您发送了 close_notify 警报但还没有回复)。 1 表示您之前收到了来自其他对等方的 close_notify 警报,并且您完全完成了。 -1 表示不可恢复的错误。 在第二次调用时(只有在返回 0 时才这样做),然后启动双向关闭(即现在等待对方向您发送“close_notify”警报)。 逻辑决定你不能再次得到 0(因为它是一个阻塞的 BIO 并且已经完成了第一步)。 -1 表示错误,1 表示完成成功。

如果你有非阻塞 BIO,同样的“可能 0 然后 1”返回值适用,除了你需要通过整个SSL_ERROR_WANT_READSSL_ERROR_WANT_WRITE的事实,即:

   If the underlying BIO is non-blocking, SSL_shutdown() will also return
   when the underlying BIO could not satisfy the needs of SSL_shutdown()
   to continue the handshake. In this case a call to SSL_get_error() with
   the return value of SSL_shutdown() will yield SSL_ERROR_WANT_READ or
   SSL_ERROR_WANT_WRITE. The calling process then must repeat the call
   after taking appropriate action to satisfy the needs of SSL_shutdown().
   The action depends on the underlying BIO. When using a non-blocking
   socket, nothing is to be done, but select() can be used to check for
   the required condition. When using a buffering BIO, like a BIO pair,
   data must be written into or retrieved out of the BIO before being able
   to continue.

所以你有两个级别的重复。 你叫SSL_shutdown “第一”时间,但重复,如果你得到SSL_ERROR_WANT_READSSL_ERROR_WANT_WRITE绕来绕去后select()循环以正常的方式,只算“第一” SSL_shutdown为已完成,如果你得到一个非SSL_ERROR_WANT_错误代码(其中如果失败),或者您会得到01回报。 如果你得到1回报,你就完成了。 如果返回0 ,并且想要双向关闭,则必须进行第二次调用,再次调用时需要检查SSL_ERROR_WANT_READSSL_ERROR_WANT_WRITE试选择; 不应返回1 ,但可能返回 0 或错误。

不简单。

文档中的更多注释:在第一次调用SSL_shutdown并返回“0”之后,您可以选择调用SSL_read而不是SSL_shutdown (以防对等方仍在该 SSL 套接字上向您发送任何数据),并且,我猜测,“希望”他们最终会从他们身边向您发送一条关闭消息,以冲洗管道。

此外,如果您计划在关闭完成后“无论如何”关闭套接字,您可以完全跳过对SSL_shutdown的第二次调用(“0 然后 1”的“1”),然后继续关闭套接字,内核应该注意丢弃“现在被忽略”的 close_notify 警报,大概他们应该发送......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM