1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5e71b7053SJung-uk KimSSL_shutdown - shut down a TLS/SSL connection 6e71b7053SJung-uk Kim 7e71b7053SJung-uk Kim=head1 SYNOPSIS 8e71b7053SJung-uk Kim 9e71b7053SJung-uk Kim #include <openssl/ssl.h> 10e71b7053SJung-uk Kim 11e71b7053SJung-uk Kim int SSL_shutdown(SSL *ssl); 12e71b7053SJung-uk Kim 13e71b7053SJung-uk Kim=head1 DESCRIPTION 14e71b7053SJung-uk Kim 15e71b7053SJung-uk KimSSL_shutdown() shuts down an active TLS/SSL connection. It sends the 16c9cf7b5cSJung-uk Kimclose_notify shutdown alert to the peer. 17e71b7053SJung-uk Kim 18e71b7053SJung-uk Kim=head1 NOTES 19e71b7053SJung-uk Kim 20c9cf7b5cSJung-uk KimSSL_shutdown() tries to send the close_notify shutdown alert to the peer. 21e71b7053SJung-uk KimWhether the operation succeeds or not, the SSL_SENT_SHUTDOWN flag is set and 22e71b7053SJung-uk Kima currently open session is considered closed and good and will be kept in the 23e71b7053SJung-uk Kimsession cache for further reuse. 24e71b7053SJung-uk Kim 256935a639SJung-uk KimNote that SSL_shutdown() must not be called if a previous fatal error has 266935a639SJung-uk Kimoccurred on a connection i.e. if SSL_get_error() has returned SSL_ERROR_SYSCALL 276935a639SJung-uk Kimor SSL_ERROR_SSL. 286935a639SJung-uk Kim 29c9cf7b5cSJung-uk KimThe shutdown procedure consists of two steps: sending of the close_notify 30c9cf7b5cSJung-uk Kimshutdown alert, and reception of the peer's close_notify shutdown alert. 31c9cf7b5cSJung-uk KimThe order of those two steps depends on the application. 32e71b7053SJung-uk Kim 33c9cf7b5cSJung-uk KimIt is acceptable for an application to only send its shutdown alert and 34c9cf7b5cSJung-uk Kimthen close the underlying connection without waiting for the peer's response. 35c9cf7b5cSJung-uk KimThis way resources can be saved, as the process can already terminate or 36c9cf7b5cSJung-uk Kimserve another connection. 37c9cf7b5cSJung-uk KimThis should only be done when it is known that the other side will not send more 38c9cf7b5cSJung-uk Kimdata, otherwise there is a risk of a truncation attack. 39c9cf7b5cSJung-uk Kim 40c9cf7b5cSJung-uk KimWhen a client only writes and never reads from the connection, and the server 41c9cf7b5cSJung-uk Kimhas sent a session ticket to establish a session, the client might not be able 42c9cf7b5cSJung-uk Kimto resume the session because it did not received and process the session ticket 43c9cf7b5cSJung-uk Kimfrom the server. 44c9cf7b5cSJung-uk KimIn case the application wants to be able to resume the session, it is recommended to 45c9cf7b5cSJung-uk Kimdo a complete shutdown procedure (bidirectional close_notify alerts). 46c9cf7b5cSJung-uk Kim 47c9cf7b5cSJung-uk KimWhen the underlying connection shall be used for more communications, the 48c9cf7b5cSJung-uk Kimcomplete shutdown procedure must be performed, so that the peers stay 49c9cf7b5cSJung-uk Kimsynchronized. 50e71b7053SJung-uk Kim 51e71b7053SJung-uk KimSSL_shutdown() only closes the write direction. 52e71b7053SJung-uk KimIt is not possible to call SSL_write() after calling SSL_shutdown(). 53e71b7053SJung-uk KimThe read direction is closed by the peer. 54e71b7053SJung-uk Kim 55e71b7053SJung-uk Kim=head2 First to close the connection 56e71b7053SJung-uk Kim 57c9cf7b5cSJung-uk KimWhen the application is the first party to send the close_notify 58e71b7053SJung-uk Kimalert, SSL_shutdown() will only send the alert and then set the 59e71b7053SJung-uk KimSSL_SENT_SHUTDOWN flag (so that the session is considered good and will 60e71b7053SJung-uk Kimbe kept in the cache). 61c9cf7b5cSJung-uk KimIf successful, SSL_shutdown() will return 0. 62c9cf7b5cSJung-uk Kim 63e71b7053SJung-uk KimIf a unidirectional shutdown is enough (the underlying connection shall be 64c9cf7b5cSJung-uk Kimclosed anyway), this first successful call to SSL_shutdown() is sufficient. 65e71b7053SJung-uk Kim 66e71b7053SJung-uk KimIn order to complete the bidirectional shutdown handshake, the peer needs 67c9cf7b5cSJung-uk Kimto send back a close_notify alert. 68e71b7053SJung-uk KimThe SSL_RECEIVED_SHUTDOWN flag will be set after receiving and processing 69e71b7053SJung-uk Kimit. 70e71b7053SJung-uk Kim 71c9cf7b5cSJung-uk KimThe peer is still allowed to send data after receiving the close_notify 72e71b7053SJung-uk Kimevent. 73c9cf7b5cSJung-uk KimWhen it is done sending data, it will send the close_notify alert. 74c9cf7b5cSJung-uk KimSSL_read() should be called until all data is received. 75e71b7053SJung-uk KimSSL_read() will indicate the end of the peer data by returning <= 0 76e71b7053SJung-uk Kimand SSL_get_error() returning SSL_ERROR_ZERO_RETURN. 77e71b7053SJung-uk Kim 78e71b7053SJung-uk Kim=head2 Peer closes the connection 79e71b7053SJung-uk Kim 80c9cf7b5cSJung-uk KimIf the peer already sent the close_notify alert B<and> it was 81e71b7053SJung-uk Kimalready processed implicitly inside another function 82e71b7053SJung-uk Kim(L<SSL_read(3)>), the SSL_RECEIVED_SHUTDOWN flag is set. 83e71b7053SJung-uk KimSSL_read() will return <= 0 in that case, and SSL_get_error() will return 84e71b7053SJung-uk KimSSL_ERROR_ZERO_RETURN. 85c9cf7b5cSJung-uk KimSSL_shutdown() will send the close_notify alert, set the SSL_SENT_SHUTDOWN 86c9cf7b5cSJung-uk Kimflag. 87c9cf7b5cSJung-uk KimIf successful, SSL_shutdown() will return 1. 88c9cf7b5cSJung-uk Kim 89e71b7053SJung-uk KimWhether SSL_RECEIVED_SHUTDOWN is already set can be checked using the 90e71b7053SJung-uk KimSSL_get_shutdown() (see also L<SSL_set_shutdown(3)> call. 91e71b7053SJung-uk Kim 92e71b7053SJung-uk Kim=head1 NOTES 93e71b7053SJung-uk Kim 94e71b7053SJung-uk KimThe behaviour of SSL_shutdown() additionally depends on the underlying BIO. 95e71b7053SJung-uk KimIf the underlying BIO is B<blocking>, SSL_shutdown() will only return once the 96e71b7053SJung-uk Kimhandshake step has been finished or an error occurred. 97e71b7053SJung-uk Kim 98*58f35182SJung-uk KimIf the underlying BIO is B<nonblocking>, SSL_shutdown() will also return 99e71b7053SJung-uk Kimwhen the underlying BIO could not satisfy the needs of SSL_shutdown() 100e71b7053SJung-uk Kimto continue the handshake. In this case a call to SSL_get_error() with the 101e71b7053SJung-uk Kimreturn value of SSL_shutdown() will yield B<SSL_ERROR_WANT_READ> or 102e71b7053SJung-uk KimB<SSL_ERROR_WANT_WRITE>. The calling process then must repeat the call after 103e71b7053SJung-uk Kimtaking appropriate action to satisfy the needs of SSL_shutdown(). 104*58f35182SJung-uk KimThe action depends on the underlying BIO. When using a nonblocking socket, 105e71b7053SJung-uk Kimnothing is to be done, but select() can be used to check for the required 106e71b7053SJung-uk Kimcondition. When using a buffering BIO, like a BIO pair, data must be written 107e71b7053SJung-uk Kiminto or retrieved out of the BIO before being able to continue. 108e71b7053SJung-uk Kim 109c9cf7b5cSJung-uk KimAfter SSL_shutdown() returned 0, it is possible to call SSL_shutdown() again 110c9cf7b5cSJung-uk Kimto wait for the peer's close_notify alert. 111c9cf7b5cSJung-uk KimSSL_shutdown() will return 1 in that case. 112c9cf7b5cSJung-uk KimHowever, it is recommended to wait for it using SSL_read() instead. 113c9cf7b5cSJung-uk Kim 114e71b7053SJung-uk KimSSL_shutdown() can be modified to only set the connection to "shutdown" 115c9cf7b5cSJung-uk Kimstate but not actually send the close_notify alert messages, 116e71b7053SJung-uk Kimsee L<SSL_CTX_set_quiet_shutdown(3)>. 117e71b7053SJung-uk KimWhen "quiet shutdown" is enabled, SSL_shutdown() will always succeed 118e71b7053SJung-uk Kimand return 1. 119*58f35182SJung-uk KimNote that this is not standard compliant behaviour. 120*58f35182SJung-uk KimIt should only be done when the peer has a way to make sure all 121*58f35182SJung-uk Kimdata has been received and doesn't wait for the close_notify alert 122*58f35182SJung-uk Kimmessage, otherwise an unexpected EOF will be reported. 123*58f35182SJung-uk Kim 124*58f35182SJung-uk KimThere are implementations that do not send the required close_notify alert. 125*58f35182SJung-uk KimIf there is a need to communicate with such an implementation, and it's clear 126*58f35182SJung-uk Kimthat all data has been received, do not wait for the peer's close_notify alert. 127*58f35182SJung-uk KimWaiting for the close_notify alert when the peer just closes the connection will 128*58f35182SJung-uk Kimresult in an error being generated. 129e71b7053SJung-uk Kim 130e71b7053SJung-uk Kim=head1 RETURN VALUES 131e71b7053SJung-uk Kim 132e71b7053SJung-uk KimThe following return values can occur: 133e71b7053SJung-uk Kim 134e71b7053SJung-uk Kim=over 4 135e71b7053SJung-uk Kim 136e71b7053SJung-uk Kim=item Z<>0 137e71b7053SJung-uk Kim 138c9cf7b5cSJung-uk KimThe shutdown is not yet finished: the close_notify was sent but the peer 139e71b7053SJung-uk Kimdid not send it back yet. 140c9cf7b5cSJung-uk KimCall SSL_read() to do a bidirectional shutdown. 141*58f35182SJung-uk Kim 142*58f35182SJung-uk KimUnlike most other function, returning 0 does not indicate an error. 143*58f35182SJung-uk KimL<SSL_get_error(3)> should not get called, it may misleadingly 144*58f35182SJung-uk Kimindicate an error even though no error occurred. 145e71b7053SJung-uk Kim 146e71b7053SJung-uk Kim=item Z<>1 147e71b7053SJung-uk Kim 148c9cf7b5cSJung-uk KimThe shutdown was successfully completed. The close_notify alert was sent 149c9cf7b5cSJung-uk Kimand the peer's close_notify alert was received. 150e71b7053SJung-uk Kim 151e71b7053SJung-uk Kim=item E<lt>0 152e71b7053SJung-uk Kim 153e71b7053SJung-uk KimThe shutdown was not successful. 154e71b7053SJung-uk KimCall L<SSL_get_error(3)> with the return value B<ret> to find out the reason. 155*58f35182SJung-uk KimIt can occur if an action is needed to continue the operation for nonblocking 156e71b7053SJung-uk KimBIOs. 157e71b7053SJung-uk Kim 158e71b7053SJung-uk KimIt can also occur when not all data was read using SSL_read(). 159e71b7053SJung-uk Kim 160e71b7053SJung-uk Kim=back 161e71b7053SJung-uk Kim 162e71b7053SJung-uk Kim=head1 SEE ALSO 163e71b7053SJung-uk Kim 164e71b7053SJung-uk KimL<SSL_get_error(3)>, L<SSL_connect(3)>, 165e71b7053SJung-uk KimL<SSL_accept(3)>, L<SSL_set_shutdown(3)>, 166e71b7053SJung-uk KimL<SSL_CTX_set_quiet_shutdown(3)>, 167e71b7053SJung-uk KimL<SSL_clear(3)>, L<SSL_free(3)>, 168e71b7053SJung-uk KimL<ssl(7)>, L<bio(7)> 169e71b7053SJung-uk Kim 170e71b7053SJung-uk Kim=head1 COPYRIGHT 171e71b7053SJung-uk Kim 172*58f35182SJung-uk KimCopyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved. 173e71b7053SJung-uk Kim 174e71b7053SJung-uk KimLicensed under the OpenSSL license (the "License"). You may not use 175e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 176e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 177e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 178e71b7053SJung-uk Kim 179e71b7053SJung-uk Kim=cut 180