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