`
nanjingjiangbiao_T
  • 浏览: 2657991 次
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

SSL_shutdown返回值的研究(1)

 
阅读更多
Format

LIBS := CSSL 
#include <openssl/ssl.h>
int SSL_shutdown(SSL *ssl)

ssl
    A pointer to a token returned on the SSL_new call.

Normal return

    Return code 0 indicates that the application issued the SSL_shutdown function first. Continue issuing the SSL_shutdown function until you receive return code 1, which indicates the remote application has also shut down.
    In SSL version 3 and TLS version 1, return code 1 indicates that both the client and server applications have issued the SSL_shutdown function.
    In SSL version 2, a return code of 1 is always returned.

Error return

A return code equal to -1 indicates an error. Issue the SSL_get_error function to obtain specific information about the error.
Programming considerations

    To use this function, you must include the library specified in the prototype in your makefile.
    The SSL_shutdown function is the normal way to shut down an SSL session. It is a good idea that you shut down an SSL session before the socket is shut down and closed.
    An alert is sent to the remote partner to notify it that the connection is ending normally. Normal shutdown is required if you want to resume the session across a different SSLsocket at a later time.
    Both the client and server applications must issue the SSL_shutdown function to shut down the connection normally.

####################################################################################

DESCRIPTION
       SSL_shutdown() shuts down an active TLS/SSL connection. It sends the
       "close notify" shutdown alert to the peer.

NOTES
       SSL_shutdown() tries to send the "close notify" shutdown alert to the
       peer.  Whether the operation succeeds or not, the SSL_SENT_SHUTDOWN
       flag is set and a currently open session is considered closed and good
       and will be kept in the session cache for further reuse.

       The shutdown procedure consists of 2 steps: the sending of the "close
       notify" shutdown alert and the reception of the peer's "close notify"
       shutdown alert. According to the TLS standard, it is acceptable for an
       application to only send its shutdown alert and then close the underly-
       ing connection without waiting for the peer's response (this way
       resources can be saved, as the process can already terminate or serve
       another connection).  When the underlying connection shall be used for
       more communications, the complete shutdown procedure (bidirectional
       "close notify" alerts) must be performed, so that the peers stay syn-
       chronized.

       SSL_shutdown() supports both uni- and bidirectional shutdown by its 2
       step behaviour.

       When the application is the first party to send the "close notify"
       alert, SSL_shutdown() will only send the alert and the set the
       SSL_SENT_SHUTDOWN flag (so that the session is considered good and will
       be kept in cache). SSL_shutdown() will then return with 0. If a unidi-
       rectional shutdown is enough (the underlying connection shall be closed
       anyway), this first call to SSL_shutdown() is sufficient. In order to
       complete the bidirectional shutdown handshake, SSL_shutdown() must be
       called again. The second call will make SSL_shutdown() wait for the
       peer's "close notify" shutdown alert. On success, the second call to
       SSL_shutdown() will return with 1.
       If the peer already sent the "close notify" alert and it was already
       processed implicitly inside another function (SSL_read(3)), the
       SSL_RECEIVED_SHUTDOWN flag is set. SSL_shutdown() will send the "close
       notify" alert, set the SSL_SENT_SHUTDOWN flag and will immediately
       return with 1. Whether SSL_RECEIVED_SHUTDOWN is already set can be
       checked using the SSL_get_shutdown() (see also SSL_set_shutdown(3)
       call.

       It is therefore recommended, to check the return value of SSL_shut-
       down() and call SSL_shutdown() again, if the bidirectional shutdown is
       not yet complete (return value of the first call is 0). As the shutdown
       is not specially handled in the SSLv2 protocol, SSL_shutdown() will
       succeed on the first call.

       The behaviour of SSL_shutdown() additionally depends on the underlying
       BIO.

       If the underlying BIO is blocking, SSL_shutdown() will only return once
       the handshake step has been finished or an error occurred.

       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() can be modified to only set the connection to "shutdown"
       state but not actually send the "close notify" alert messages, see
       SSL_CTX_set_quiet_shutdown(3).  When "quiet shutdown" is enabled,
       SSL_shutdown() will always succeed and return 1.

RETURN VALUES
       The following return values can occur:

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

       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 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.

分享到:
评论

相关推荐

    ssl相关应用程序,实现身份认证功能、数据传输加密功能源代码.doc

    `ssl_free`函数用于释放分配的资源,包括调用`SSL_shutdown`关闭SSL连接,如果`shutdown`标志被设置,还会调用`SSL_free`释放SSL句柄,并清理`BIO`结构。 `ssl_read`函数执行SSL的读操作。它首先检查是否已完成SSL...

    OpenSSL编程实例复习过程.pdf

    1. Winsock 初始化:在编写 Windows 平台上的网络程序时,需要初始化 Winsock 库,使用 `WSAStartup` 函数来启动 Winsock,並检查返回值是否为 0,表示初始化成功。 2. 套接字创建:使用 `socket` 函数创建一个套接...

    openssl-example-master, OpenSSL异步连接源码

    1. **初始化 OpenSSL**:在使用 OpenSSL 之前,需要先进行库的初始化,通过 `SSL_library_init()` 和 `SSL_load_error_strings()` 函数完成。这将确保所有的内部数据结构被正确设置,错误消息字符串也被加载。 2. *...

    OpenSSLSocketImpl.rar_Windows编程_Unix_Linux_

    在会话结束时,`OpenSSLSocketImpl`需要关闭SSL连接,这涉及到调用`SSL_shutdown()`和`SSL_free()`。同时,底层的socket也需要关闭,防止资源泄露。 为了保证安全性,`OpenSSLSocketImpl`还需要处理各种异常情况,...

    nopoll实现websocket

    4. **错误处理**:在使用过程中,应定期检查`nopoll_conn_is_ok`函数的返回值,判断连接是否仍然有效。如果遇到错误,可以通过`nopoll_conn_get_error`获取错误信息。 5. **关闭连接**:完成通信后,可以调用`...

    Windows Sockets 完全进阶

    1. **错误检查**:Winsock函数返回值通常表示操作状态,负值通常表示错误。使用`WSAGetLastError()`获取错误代码,并根据错误码进行错误处理。 2. **调试工具**:Wireshark等网络抓包工具可以帮助分析网络通信,...

    Windows_Server_Socket_Program_C++_tcp_

    当通信结束后,使用`shutdown`函数停止发送或接收数据,然后调用`close`函数关闭套接字。注意,关闭套接字并不立即断开连接,而是发送一个FIN标志,等待对方确认后才能完全断开。 十、示例代码 在“Socket_...

    PHP通过ice调用python程序.pdf

    此外,代码还检查了是否启用SSL以及调用模式等。 总的来说,通过Ice,我们可以轻松地在PHP和Python之间建立通信,实现服务调用。这种方式特别适用于PHP无法直接处理的复杂任务,或者需要利用Python的强大功能,如...

    PHP通过ice调用python程序借鉴.pdf

    7. **性能优化**:根据项目需求,可能需要调整连接参数,如端口号、SSL配置等,以优化通信性能和安全性。 总的来说,通过ICE,PHP可以方便地调用Python服务,实现跨语言的协同工作,充分利用Python的强大功能,同时...

    流式套接字实现简单的客户端服务端通信过程

    - TCP本身不提供加密,但可以与SSL/TLS结合使用,形成安全套接字层(Secure Sockets Layer,SSL)或传输层安全(Transport Layer Security,TLS),以保护数据传输的安全性。 通过以上步骤,我们可以理解并实现...

Global site tag (gtag.js) - Google Analytics