113d40330Schristos=pod 213d40330Schristos 313d40330Schristos=head1 NAME 413d40330Schristos 513d40330SchristosSSL_shutdown - shut down a TLS/SSL connection 613d40330Schristos 713d40330Schristos=head1 SYNOPSIS 813d40330Schristos 913d40330Schristos #include <openssl/ssl.h> 1013d40330Schristos 1113d40330Schristos int SSL_shutdown(SSL *ssl); 1213d40330Schristos 1313d40330Schristos=head1 DESCRIPTION 1413d40330Schristos 1513d40330SchristosSSL_shutdown() shuts down an active TLS/SSL connection. It sends the 16f4f044c4Schristosclose_notify shutdown alert to the peer. 1713d40330Schristos 18f4f044c4SchristosSSL_shutdown() tries to send the close_notify shutdown alert to the peer. 1913d40330SchristosWhether the operation succeeds or not, the SSL_SENT_SHUTDOWN flag is set and 2013d40330Schristosa currently open session is considered closed and good and will be kept in the 2113d40330Schristossession cache for further reuse. 2213d40330Schristos 23b88c74d5SchristosNote that SSL_shutdown() must not be called if a previous fatal error has 24b88c74d5Schristosoccurred on a connection i.e. if SSL_get_error() has returned SSL_ERROR_SYSCALL 25b88c74d5Schristosor SSL_ERROR_SSL. 26b88c74d5Schristos 27f4f044c4SchristosThe shutdown procedure consists of two steps: sending of the close_notify 28f4f044c4Schristosshutdown alert, and reception of the peer's close_notify shutdown alert. 29f4f044c4SchristosThe order of those two steps depends on the application. 3013d40330Schristos 31f4f044c4SchristosIt is acceptable for an application to only send its shutdown alert and 32f4f044c4Schristosthen close the underlying connection without waiting for the peer's response. 33f4f044c4SchristosThis way resources can be saved, as the process can already terminate or 34f4f044c4Schristosserve another connection. 35f4f044c4SchristosThis should only be done when it is known that the other side will not send more 36f4f044c4Schristosdata, otherwise there is a risk of a truncation attack. 37f4f044c4Schristos 38f4f044c4SchristosWhen a client only writes and never reads from the connection, and the server 39f4f044c4Schristoshas sent a session ticket to establish a session, the client might not be able 40f4f044c4Schristosto resume the session because it did not received and process the session ticket 41f4f044c4Schristosfrom the server. 42f4f044c4SchristosIn case the application wants to be able to resume the session, it is recommended to 43f4f044c4Schristosdo a complete shutdown procedure (bidirectional close_notify alerts). 44f4f044c4Schristos 45f4f044c4SchristosWhen the underlying connection shall be used for more communications, the 46f4f044c4Schristoscomplete shutdown procedure must be performed, so that the peers stay 47f4f044c4Schristossynchronized. 4813d40330Schristos 4913d40330SchristosSSL_shutdown() only closes the write direction. 5013d40330SchristosIt is not possible to call SSL_write() after calling SSL_shutdown(). 5113d40330SchristosThe read direction is closed by the peer. 5213d40330Schristos 53*b0d17251SchristosThe behaviour of SSL_shutdown() additionally depends on the underlying BIO. 54*b0d17251SchristosIf the underlying BIO is B<blocking>, SSL_shutdown() will only return once the 55*b0d17251Schristoshandshake step has been finished or an error occurred. 56*b0d17251Schristos 57*b0d17251SchristosIf the underlying BIO is B<nonblocking>, SSL_shutdown() will also return 58*b0d17251Schristoswhen the underlying BIO could not satisfy the needs of SSL_shutdown() 59*b0d17251Schristosto continue the handshake. In this case a call to SSL_get_error() with the 60*b0d17251Schristosreturn value of SSL_shutdown() will yield B<SSL_ERROR_WANT_READ> or 61*b0d17251SchristosB<SSL_ERROR_WANT_WRITE>. The calling process then must repeat the call after 62*b0d17251Schristostaking appropriate action to satisfy the needs of SSL_shutdown(). 63*b0d17251SchristosThe action depends on the underlying BIO. When using a nonblocking socket, 64*b0d17251Schristosnothing is to be done, but select() can be used to check for the required 65*b0d17251Schristoscondition. When using a buffering BIO, like a BIO pair, data must be written 66*b0d17251Schristosinto or retrieved out of the BIO before being able to continue. 67*b0d17251Schristos 68*b0d17251SchristosAfter SSL_shutdown() returned 0, it is possible to call SSL_shutdown() again 69*b0d17251Schristosto wait for the peer's close_notify alert. 70*b0d17251SchristosSSL_shutdown() will return 1 in that case. 71*b0d17251SchristosHowever, it is recommended to wait for it using SSL_read() instead. 72*b0d17251Schristos 73*b0d17251SchristosSSL_shutdown() can be modified to only set the connection to "shutdown" 74*b0d17251Schristosstate but not actually send the close_notify alert messages, 75*b0d17251Schristossee L<SSL_CTX_set_quiet_shutdown(3)>. 76*b0d17251SchristosWhen "quiet shutdown" is enabled, SSL_shutdown() will always succeed 77*b0d17251Schristosand return 1. 78*b0d17251SchristosNote that this is not standard compliant behaviour. 79*b0d17251SchristosIt should only be done when the peer has a way to make sure all 80*b0d17251Schristosdata has been received and doesn't wait for the close_notify alert 81*b0d17251Schristosmessage, otherwise an unexpected EOF will be reported. 82*b0d17251Schristos 83*b0d17251SchristosThere are implementations that do not send the required close_notify alert. 84*b0d17251SchristosIf there is a need to communicate with such an implementation, and it's clear 85*b0d17251Schristosthat all data has been received, do not wait for the peer's close_notify alert. 86*b0d17251SchristosWaiting for the close_notify alert when the peer just closes the connection 87*b0d17251Schristoswill result in an error being generated. 88*b0d17251SchristosThe error can be ignored using the B<SSL_OP_IGNORE_UNEXPECTED_EOF>. 89*b0d17251SchristosFor more information see L<SSL_CTX_set_options(3)>. 90*b0d17251Schristos 9113d40330Schristos=head2 First to close the connection 9213d40330Schristos 93f4f044c4SchristosWhen the application is the first party to send the close_notify 9413d40330Schristosalert, SSL_shutdown() will only send the alert and then set the 9513d40330SchristosSSL_SENT_SHUTDOWN flag (so that the session is considered good and will 9613d40330Schristosbe kept in the cache). 97f4f044c4SchristosIf successful, SSL_shutdown() will return 0. 98f4f044c4Schristos 9913d40330SchristosIf a unidirectional shutdown is enough (the underlying connection shall be 100f4f044c4Schristosclosed anyway), this first successful call to SSL_shutdown() is sufficient. 10113d40330Schristos 10213d40330SchristosIn order to complete the bidirectional shutdown handshake, the peer needs 103f4f044c4Schristosto send back a close_notify alert. 10413d40330SchristosThe SSL_RECEIVED_SHUTDOWN flag will be set after receiving and processing 10513d40330Schristosit. 10613d40330Schristos 107f4f044c4SchristosThe peer is still allowed to send data after receiving the close_notify 10813d40330Schristosevent. 109f4f044c4SchristosWhen it is done sending data, it will send the close_notify alert. 110f4f044c4SchristosSSL_read() should be called until all data is received. 11113d40330SchristosSSL_read() will indicate the end of the peer data by returning <= 0 11213d40330Schristosand SSL_get_error() returning SSL_ERROR_ZERO_RETURN. 11313d40330Schristos 11413d40330Schristos=head2 Peer closes the connection 11513d40330Schristos 116f4f044c4SchristosIf the peer already sent the close_notify alert B<and> it was 11713d40330Schristosalready processed implicitly inside another function 11813d40330Schristos(L<SSL_read(3)>), the SSL_RECEIVED_SHUTDOWN flag is set. 11913d40330SchristosSSL_read() will return <= 0 in that case, and SSL_get_error() will return 12013d40330SchristosSSL_ERROR_ZERO_RETURN. 121f4f044c4SchristosSSL_shutdown() will send the close_notify alert, set the SSL_SENT_SHUTDOWN 122f4f044c4Schristosflag. 123f4f044c4SchristosIf successful, SSL_shutdown() will return 1. 124f4f044c4Schristos 12513d40330SchristosWhether SSL_RECEIVED_SHUTDOWN is already set can be checked using the 12613d40330SchristosSSL_get_shutdown() (see also L<SSL_set_shutdown(3)> call. 12713d40330Schristos 12813d40330Schristos=head1 RETURN VALUES 12913d40330Schristos 13013d40330SchristosThe following return values can occur: 13113d40330Schristos 13213d40330Schristos=over 4 13313d40330Schristos 13413d40330Schristos=item Z<>0 13513d40330Schristos 136f4f044c4SchristosThe shutdown is not yet finished: the close_notify was sent but the peer 13713d40330Schristosdid not send it back yet. 138f4f044c4SchristosCall SSL_read() to do a bidirectional shutdown. 139f30e0929Schristos 140f30e0929SchristosUnlike most other function, returning 0 does not indicate an error. 141f30e0929SchristosL<SSL_get_error(3)> should not get called, it may misleadingly 142f30e0929Schristosindicate an error even though no error occurred. 14313d40330Schristos 14413d40330Schristos=item Z<>1 14513d40330Schristos 146f4f044c4SchristosThe shutdown was successfully completed. The close_notify alert was sent 147f4f044c4Schristosand the peer's close_notify alert was received. 14813d40330Schristos 14913d40330Schristos=item E<lt>0 15013d40330Schristos 15113d40330SchristosThe shutdown was not successful. 15213d40330SchristosCall L<SSL_get_error(3)> with the return value B<ret> to find out the reason. 153f30e0929SchristosIt can occur if an action is needed to continue the operation for nonblocking 15413d40330SchristosBIOs. 15513d40330Schristos 15613d40330SchristosIt can also occur when not all data was read using SSL_read(). 15713d40330Schristos 15813d40330Schristos=back 15913d40330Schristos 16013d40330Schristos=head1 SEE ALSO 16113d40330Schristos 16213d40330SchristosL<SSL_get_error(3)>, L<SSL_connect(3)>, 16313d40330SchristosL<SSL_accept(3)>, L<SSL_set_shutdown(3)>, 164*b0d17251SchristosL<SSL_CTX_set_quiet_shutdown(3)>, L<SSL_CTX_set_options(3)> 16513d40330SchristosL<SSL_clear(3)>, L<SSL_free(3)>, 16613d40330SchristosL<ssl(7)>, L<bio(7)> 16713d40330Schristos 16813d40330Schristos=head1 COPYRIGHT 16913d40330Schristos 170f30e0929SchristosCopyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved. 17113d40330Schristos 172*b0d17251SchristosLicensed under the Apache License 2.0 (the "License"). You may not use 17313d40330Schristosthis file except in compliance with the License. You can obtain a copy 17413d40330Schristosin the file LICENSE in the source distribution or at 17513d40330SchristosL<https://www.openssl.org/source/license.html>. 17613d40330Schristos 17713d40330Schristos=cut 178