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