5 SSL_shutdown - shut down a TLS/SSL connection
9 #include <openssl/ssl.h>
11 int SSL_shutdown(SSL *ssl);
15 SSL_shutdown() shuts down an active TLS/SSL connection. It sends the
16 close_notify shutdown alert to the peer.
18 SSL_shutdown() tries to send the close_notify shutdown alert to the peer.
19 Whether the operation succeeds or not, the SSL_SENT_SHUTDOWN flag is set and
20 a currently open session is considered closed and good and will be kept in the
21 session cache for further reuse.
23 Note that SSL_shutdown() must not be called if a previous fatal error has
24 occurred on a connection i.e. if SSL_get_error() has returned SSL_ERROR_SYSCALL
27 The shutdown procedure consists of two steps: sending of the close_notify
28 shutdown alert, and reception of the peer's close_notify shutdown alert.
29 The order of those two steps depends on the application.
31 It is acceptable for an application to only send its shutdown alert and
32 then close the underlying connection without waiting for the peer's response.
33 This way resources can be saved, as the process can already terminate or
34 serve another connection.
35 This should only be done when it is known that the other side will not send more
36 data, otherwise there is a risk of a truncation attack.
38 When a client only writes and never reads from the connection, and the server
39 has sent a session ticket to establish a session, the client might not be able
40 to resume the session because it did not received and process the session ticket
42 In case the application wants to be able to resume the session, it is recommended to
43 do a complete shutdown procedure (bidirectional close_notify alerts).
45 When the underlying connection shall be used for more communications, the
46 complete shutdown procedure must be performed, so that the peers stay
49 SSL_shutdown() only closes the write direction.
50 It is not possible to call SSL_write() after calling SSL_shutdown().
51 The read direction is closed by the peer.
53 The behaviour of SSL_shutdown() additionally depends on the underlying BIO.
54 If the underlying BIO is B<blocking>, SSL_shutdown() will only return once the
55 handshake step has been finished or an error occurred.
57 If the underlying BIO is B<non-blocking>, SSL_shutdown() will also return
58 when the underlying BIO could not satisfy the needs of SSL_shutdown()
59 to continue the handshake. In this case a call to SSL_get_error() with the
60 return value of SSL_shutdown() will yield B<SSL_ERROR_WANT_READ> or
61 B<SSL_ERROR_WANT_WRITE>. The calling process then must repeat the call after
62 taking appropriate action to satisfy the needs of SSL_shutdown().
63 The action depends on the underlying BIO. When using a non-blocking socket,
64 nothing is to be done, but select() can be used to check for the required
65 condition. When using a buffering BIO, like a BIO pair, data must be written
66 into or retrieved out of the BIO before being able to continue.
68 After SSL_shutdown() returned 0, it is possible to call SSL_shutdown() again
69 to wait for the peer's close_notify alert.
70 SSL_shutdown() will return 1 in that case.
71 However, it is recommended to wait for it using SSL_read() instead.
73 SSL_shutdown() can be modified to only set the connection to "shutdown"
74 state but not actually send the close_notify alert messages,
75 see L<SSL_CTX_set_quiet_shutdown(3)>.
76 When "quiet shutdown" is enabled, SSL_shutdown() will always succeed
79 =head2 First to close the connection
81 When the application is the first party to send the close_notify
82 alert, SSL_shutdown() will only send the alert and then set the
83 SSL_SENT_SHUTDOWN flag (so that the session is considered good and will
84 be kept in the cache).
85 If successful, SSL_shutdown() will return 0.
87 If a unidirectional shutdown is enough (the underlying connection shall be
88 closed anyway), this first successful call to SSL_shutdown() is sufficient.
90 In order to complete the bidirectional shutdown handshake, the peer needs
91 to send back a close_notify alert.
92 The SSL_RECEIVED_SHUTDOWN flag will be set after receiving and processing
95 The peer is still allowed to send data after receiving the close_notify
97 When it is done sending data, it will send the close_notify alert.
98 SSL_read() should be called until all data is received.
99 SSL_read() will indicate the end of the peer data by returning <= 0
100 and SSL_get_error() returning SSL_ERROR_ZERO_RETURN.
102 =head2 Peer closes the connection
104 If the peer already sent the close_notify alert B<and> it was
105 already processed implicitly inside another function
106 (L<SSL_read(3)>), the SSL_RECEIVED_SHUTDOWN flag is set.
107 SSL_read() will return <= 0 in that case, and SSL_get_error() will return
108 SSL_ERROR_ZERO_RETURN.
109 SSL_shutdown() will send the close_notify alert, set the SSL_SENT_SHUTDOWN
111 If successful, SSL_shutdown() will return 1.
113 Whether SSL_RECEIVED_SHUTDOWN is already set can be checked using the
114 SSL_get_shutdown() (see also L<SSL_set_shutdown(3)> call.
118 The following return values can occur:
124 The shutdown is not yet finished: the close_notify was sent but the peer
125 did not send it back yet.
126 Call SSL_read() to do a bidirectional shutdown.
127 The output of L<SSL_get_error(3)> may be misleading, as an
128 erroneous SSL_ERROR_SYSCALL may be flagged even though no error occurred.
132 The shutdown was successfully completed. The close_notify alert was sent
133 and the peer's close_notify alert was received.
137 The shutdown was not successful.
138 Call L<SSL_get_error(3)> with the return value B<ret> to find out the reason.
139 It can occur if an action is needed to continue the operation for non-blocking
142 It can also occur when not all data was read using SSL_read().
148 L<SSL_get_error(3)>, L<SSL_connect(3)>,
149 L<SSL_accept(3)>, L<SSL_set_shutdown(3)>,
150 L<SSL_CTX_set_quiet_shutdown(3)>,
151 L<SSL_clear(3)>, L<SSL_free(3)>,
156 Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
158 Licensed under the Apache License 2.0 (the "License"). You may not use
159 this file except in compliance with the License. You can obtain a copy
160 in the file LICENSE in the source distribution or at
161 L<https://www.openssl.org/source/license.html>.