1SSL_SHUTDOWN(3ossl) OpenSSL SSL_SHUTDOWN(3ossl)
2
3
4
6 SSL_shutdown - shut down a TLS/SSL connection
7
9 #include <openssl/ssl.h>
10
11 int SSL_shutdown(SSL *ssl);
12
14 SSL_shutdown() shuts down an active TLS/SSL connection. It sends the
15 close_notify shutdown alert to the peer.
16
17 SSL_shutdown() tries to send the close_notify shutdown alert to the
18 peer. Whether the operation succeeds or not, the SSL_SENT_SHUTDOWN
19 flag is set and a currently open session is considered closed and good
20 and will be kept in the session cache for further reuse.
21
22 Note that SSL_shutdown() must not be called if a previous fatal error
23 has occurred on a connection i.e. if SSL_get_error() has returned
24 SSL_ERROR_SYSCALL or SSL_ERROR_SSL.
25
26 The shutdown procedure consists of two steps: sending of the
27 close_notify shutdown alert, and reception of the peer's close_notify
28 shutdown alert. The order of those two steps depends on the
29 application.
30
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
33 response. This way resources can be saved, as the process can already
34 terminate or serve another connection. This should only be done when
35 it is known that the other side will not send more data, otherwise
36 there is a risk of a truncation attack.
37
38 When a client only writes and never reads from the connection, and the
39 server has sent a session ticket to establish a session, the client
40 might not be able to resume the session because it did not received and
41 process the session ticket from the server. In case the application
42 wants to be able to resume the session, it is recommended to do a
43 complete shutdown procedure (bidirectional close_notify alerts).
44
45 When the underlying connection shall be used for more communications,
46 the complete shutdown procedure must be performed, so that the peers
47 stay synchronized.
48
49 SSL_shutdown() only closes the write direction. It is not possible to
50 call SSL_write() after calling SSL_shutdown(). The read direction is
51 closed by the peer.
52
53 The behaviour of SSL_shutdown() additionally depends on the underlying
54 BIO. If the underlying BIO is blocking, SSL_shutdown() will only
55 return once the handshake step has been finished or an error occurred.
56
57 If the underlying BIO is nonblocking, 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
60 the return value of SSL_shutdown() will yield SSL_ERROR_WANT_READ or
61 SSL_ERROR_WANT_WRITE. The calling process then must repeat the call
62 after taking appropriate action to satisfy the needs of SSL_shutdown().
63 The action depends on the underlying BIO. When using a nonblocking
64 socket, nothing is to be done, but select() can be used to check for
65 the required condition. When using a buffering BIO, like a BIO pair,
66 data must be written into or retrieved out of the BIO before being able
67 to continue.
68
69 After SSL_shutdown() returned 0, it is possible to call SSL_shutdown()
70 again to wait for the peer's close_notify alert. SSL_shutdown() will
71 return 1 in that case. However, it is recommended to wait for it using
72 SSL_read() instead.
73
74 SSL_shutdown() can be modified to only set the connection to "shutdown"
75 state but not actually send the close_notify alert messages, see
76 SSL_CTX_set_quiet_shutdown(3). When "quiet shutdown" is enabled,
77 SSL_shutdown() will always succeed and return 1. Note that this is not
78 standard compliant behaviour. It should only be done when the peer has
79 a way to make sure all data has been received and doesn't wait for the
80 close_notify alert message, otherwise an unexpected EOF will be
81 reported.
82
83 There are implementations that do not send the required close_notify
84 alert. If there is a need to communicate with such an implementation,
85 and it's clear that all data has been received, do not wait for the
86 peer's close_notify alert. Waiting for the close_notify alert when the
87 peer just closes the connection will result in an error being
88 generated. The error can be ignored using the
89 SSL_OP_IGNORE_UNEXPECTED_EOF. For more information see
90 SSL_CTX_set_options(3).
91
92 First to close the connection
93 When the application is the first party to send the close_notify alert,
94 SSL_shutdown() will only send the alert and then set the
95 SSL_SENT_SHUTDOWN flag (so that the session is considered good and will
96 be kept in the cache). If successful, SSL_shutdown() will return 0.
97
98 If a unidirectional shutdown is enough (the underlying connection shall
99 be closed anyway), this first successful call to SSL_shutdown() is
100 sufficient.
101
102 In order to complete the bidirectional shutdown handshake, the peer
103 needs to send back a close_notify alert. The SSL_RECEIVED_SHUTDOWN
104 flag will be set after receiving and processing it.
105
106 The peer is still allowed to send data after receiving the close_notify
107 event. When it is done sending data, it will send the close_notify
108 alert. SSL_read() should be called until all data is received.
109 SSL_read() will indicate the end of the peer data by returning <= 0 and
110 SSL_get_error() returning SSL_ERROR_ZERO_RETURN.
111
112 Peer closes the connection
113 If the peer already sent the close_notify alert and it was already
114 processed implicitly inside another function (SSL_read(3)), the
115 SSL_RECEIVED_SHUTDOWN flag is set. SSL_read() will return <= 0 in that
116 case, and SSL_get_error() will return SSL_ERROR_ZERO_RETURN.
117 SSL_shutdown() will send the close_notify alert, set the
118 SSL_SENT_SHUTDOWN flag. If successful, SSL_shutdown() will return 1.
119
120 Whether SSL_RECEIVED_SHUTDOWN is already set can be checked using the
121 SSL_get_shutdown() (see also SSL_set_shutdown(3) call.
122
124 The following return values can occur:
125
126 0 The shutdown is not yet finished: the close_notify was sent but the
127 peer did not send it back yet. Call SSL_read() to do a
128 bidirectional shutdown.
129
130 Unlike most other function, returning 0 does not indicate an error.
131 SSL_get_error(3) should not get called, it may misleadingly
132 indicate an error even though no error occurred.
133
134 1 The shutdown was successfully completed. The close_notify alert was
135 sent and the peer's close_notify alert was received.
136
137 <0 The shutdown was not successful. Call SSL_get_error(3) with the
138 return value ret to find out the reason. It can occur if an action
139 is needed to continue the operation for nonblocking BIOs.
140
141 It can also occur when not all data was read using SSL_read().
142
144 SSL_get_error(3), SSL_connect(3), SSL_accept(3), SSL_set_shutdown(3),
145 SSL_CTX_set_quiet_shutdown(3), SSL_CTX_set_options(3) SSL_clear(3),
146 SSL_free(3), ssl(7), bio(7)
147
149 Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
150
151 Licensed under the Apache License 2.0 (the "License"). You may not use
152 this file except in compliance with the License. You can obtain a copy
153 in the file LICENSE in the source distribution or at
154 <https://www.openssl.org/source/license.html>.
155
156
157
1583.0.9 2023-07-27 SSL_SHUTDOWN(3ossl)