1SSL_SHUTDOWN(3) OpenSSL SSL_SHUTDOWN(3)
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
18 SSL_shutdown() tries to send the close_notify shutdown alert to the
19 peer. Whether the operation succeeds or not, the SSL_SENT_SHUTDOWN
20 flag is set and a currently open session is considered closed and good
21 and will be kept in the session cache for further reuse.
22
23 Note that SSL_shutdown() must not be called if a previous fatal error
24 has occurred on a connection i.e. if SSL_get_error() has returned
25 SSL_ERROR_SYSCALL or SSL_ERROR_SSL.
26
27 The shutdown procedure consists of two steps: sending of the
28 close_notify shutdown alert, and reception of the peer's close_notify
29 shutdown alert. The order of those two steps depends on the
30 application.
31
32 It is acceptable for an application to only send its shutdown alert and
33 then close the underlying connection without waiting for the peer's
34 response. This way resources can be saved, as the process can already
35 terminate or serve another connection. This should only be done when
36 it is known that the other side will not send more data, otherwise
37 there is a risk of a truncation attack.
38
39 When a client only writes and never reads from the connection, and the
40 server has sent a session ticket to establish a session, the client
41 might not be able to resume the session because it did not received and
42 process the session ticket from the server. In case the application
43 wants to be able to resume the session, it is recommended to do a
44 complete shutdown procedure (bidirectional close_notify alerts).
45
46 When the underlying connection shall be used for more communications,
47 the complete shutdown procedure must be performed, so that the peers
48 stay synchronized.
49
50 SSL_shutdown() only closes the write direction. It is not possible to
51 call SSL_write() after calling SSL_shutdown(). The read direction is
52 closed by the peer.
53
54 First to close the connection
55 When the application is the first party to send the close_notify alert,
56 SSL_shutdown() will only send the alert and then set the
57 SSL_SENT_SHUTDOWN flag (so that the session is considered good and will
58 be kept in the cache). If successful, SSL_shutdown() will return 0.
59
60 If a unidirectional shutdown is enough (the underlying connection shall
61 be closed anyway), this first successful call to SSL_shutdown() is
62 sufficient.
63
64 In order to complete the bidirectional shutdown handshake, the peer
65 needs to send back a close_notify alert. The SSL_RECEIVED_SHUTDOWN
66 flag will be set after receiving and processing it.
67
68 The peer is still allowed to send data after receiving the close_notify
69 event. When it is done sending data, it will send the close_notify
70 alert. SSL_read() should be called until all data is received.
71 SSL_read() will indicate the end of the peer data by returning <= 0 and
72 SSL_get_error() returning SSL_ERROR_ZERO_RETURN.
73
74 Peer closes the connection
75 If the peer already sent the close_notify alert and it was already
76 processed implicitly inside another function (SSL_read(3)), the
77 SSL_RECEIVED_SHUTDOWN flag is set. SSL_read() will return <= 0 in that
78 case, and SSL_get_error() will return SSL_ERROR_ZERO_RETURN.
79 SSL_shutdown() will send the close_notify alert, set the
80 SSL_SENT_SHUTDOWN flag. If successful, SSL_shutdown() will return 1.
81
82 Whether SSL_RECEIVED_SHUTDOWN is already set can be checked using the
83 SSL_get_shutdown() (see also SSL_set_shutdown(3) call.
84
86 The behaviour of SSL_shutdown() additionally depends on the underlying
87 BIO. If the underlying BIO is blocking, SSL_shutdown() will only
88 return once the handshake step has been finished or an error occurred.
89
90 If the underlying BIO is nonblocking, SSL_shutdown() will also return
91 when the underlying BIO could not satisfy the needs of SSL_shutdown()
92 to continue the handshake. In this case a call to SSL_get_error() with
93 the return value of SSL_shutdown() will yield SSL_ERROR_WANT_READ or
94 SSL_ERROR_WANT_WRITE. The calling process then must repeat the call
95 after taking appropriate action to satisfy the needs of SSL_shutdown().
96 The action depends on the underlying BIO. When using a nonblocking
97 socket, nothing is to be done, but select() can be used to check for
98 the required condition. When using a buffering BIO, like a BIO pair,
99 data must be written into or retrieved out of the BIO before being able
100 to continue.
101
102 After SSL_shutdown() returned 0, it is possible to call SSL_shutdown()
103 again to wait for the peer's close_notify alert. SSL_shutdown() will
104 return 1 in that case. However, it is recommended to wait for it using
105 SSL_read() instead.
106
107 SSL_shutdown() can be modified to only set the connection to "shutdown"
108 state but not actually send the close_notify alert messages, see
109 SSL_CTX_set_quiet_shutdown(3). When "quiet shutdown" is enabled,
110 SSL_shutdown() will always succeed and return 1. Note that this is not
111 standard compliant behaviour. It should only be done when the peer has
112 a way to make sure all data has been received and doesn't wait for the
113 close_notify alert message, otherwise an unexpected EOF will be
114 reported.
115
116 There are implementations that do not send the required close_notify
117 alert. If there is a need to communicate with such an implementation,
118 and it's clear that all data has been received, do not wait for the
119 peer's close_notify alert. Waiting for the close_notify alert when the
120 peer just closes the connection will result in an error being
121 generated.
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_clear(3), SSL_free(3), ssl(7),
146 bio(7)
147
149 Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
150
151 Licensed under the OpenSSL license (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
1581.1.1k 2021-03-26 SSL_SHUTDOWN(3)