Update documentation on how to close a connection
[openssl.git] / doc / man3 / SSL_shutdown.pod
1 =pod
2
3 =head1 NAME
4
5 SSL_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
15 SSL_shutdown() shuts down an active TLS/SSL connection. It sends the
16 "close notify" shutdown alert to the peer.
17
18 =head1 NOTES
19
20 SSL_shutdown() tries to send the "close notify" shutdown alert to the peer.
21 Whether the operation succeeds or not, the SSL_SENT_SHUTDOWN flag is set and
22 a currently open session is considered closed and good and will be kept in the
23 session cache for further reuse.
24
25 The shutdown procedure consists of 2 steps: the sending of the "close notify"
26 shutdown alert and the reception of the peer's "close notify" shutdown
27 alert. According to the TLS standard, it is acceptable for an application
28 to only send its shutdown alert and then close the underlying connection
29 without waiting for the peer's response (this way resources can be saved,
30 as the process can already terminate or serve another connection).
31 When the underlying connection shall be used for more communications, the
32 complete shutdown procedure (bidirectional "close notify" alerts) must be
33 performed, so that the peers stay synchronized.
34
35 SSL_shutdown() supports both uni- and bidirectional shutdown by its 2 step
36 behaviour.
37
38 SSL_shutdown() only closes the write direction.
39 It is not possible to call SSL_write() after calling SSL_shutdown().
40 The read direction is closed by the peer.
41
42 =head2 First to close the connection
43
44 When the application is the first party to send the "close notify"
45 alert, SSL_shutdown() will only send the alert and then set the
46 SSL_SENT_SHUTDOWN flag (so that the session is considered good and will
47 be kept in the cache).
48 SSL_shutdown() will then return with 0.
49 If a unidirectional shutdown is enough (the underlying connection shall be
50 closed anyway), this first call to SSL_shutdown() is sufficient.
51
52 In order to complete the bidirectional shutdown handshake, the peer needs
53 to send back a "close notify" alert.
54 The SSL_RECEIVED_SHUTDOWN flag will be set after receiving and processing
55 it.
56 SSL_shutdown() will return 1 when it has been received.
57
58 The peer is still allowed to send data after receiving the "close notify"
59 event.
60 If the peer did send data it needs to be processed by calling SSL_read()
61 before calling SSL_shutdown() a second time.
62 SSL_read() will indicate the end of the peer data by returning <= 0
63 and SSL_get_error() returning SSL_ERROR_ZERO_RETURN.
64 It is recommended to call SSL_read() between SSL_shutdown() calls.
65
66 =head2 Peer closes the connection
67
68 If the peer already sent the "close notify" alert B<and> it was
69 already processed implicitly inside another function
70 (L<SSL_read(3)>), the SSL_RECEIVED_SHUTDOWN flag is set.
71 SSL_read() will return <= 0 in that case, and SSL_get_error() will return
72 SSL_ERROR_ZERO_RETURN.
73 SSL_shutdown() will send the "close notify" alert, set the SSL_SENT_SHUTDOWN
74 flag and will immediately return with 1.
75 Whether SSL_RECEIVED_SHUTDOWN is already set can be checked using the
76 SSL_get_shutdown() (see also L<SSL_set_shutdown(3)> call.
77
78 =head1 NOTES
79
80 It is recommended to do a bidirectional shutdown by checking the return value
81 of SSL_shutdown() and call it again until it returns 1 or a fatal error.
82
83 The behaviour of SSL_shutdown() additionally depends on the underlying BIO.
84 If the underlying BIO is B<blocking>, SSL_shutdown() will only return once the
85 handshake step has been finished or an error occurred.
86
87 If the underlying BIO is B<non-blocking>, SSL_shutdown() will also return
88 when the underlying BIO could not satisfy the needs of SSL_shutdown()
89 to continue the handshake. In this case a call to SSL_get_error() with the
90 return value of SSL_shutdown() will yield B<SSL_ERROR_WANT_READ> or
91 B<SSL_ERROR_WANT_WRITE>. The calling process then must repeat the call after
92 taking appropriate action to satisfy the needs of SSL_shutdown().
93 The action depends on the underlying BIO. When using a non-blocking socket,
94 nothing is to be done, but select() can be used to check for the required
95 condition. When using a buffering BIO, like a BIO pair, data must be written
96 into or retrieved out of the BIO before being able to continue.
97
98 SSL_shutdown() can be modified to only set the connection to "shutdown"
99 state but not actually send the "close notify" alert messages,
100 see L<SSL_CTX_set_quiet_shutdown(3)>.
101 When "quiet shutdown" is enabled, SSL_shutdown() will always succeed
102 and return 1.
103
104 =head1 RETURN VALUES
105
106 The following return values can occur:
107
108 =over 4
109
110 =item Z<>0
111
112 The shutdown is not yet finished: the "close notify" was send but the peer
113 did not send it back yet.
114 Call SSL_shutdown() again to do a bidirectional shutdown.
115 The output of L<SSL_get_error(3)> may be misleading, as an
116 erroneous SSL_ERROR_SYSCALL may be flagged even though no error occurred.
117
118 =item Z<>1
119
120 The shutdown was successfully completed. The "close notify" alert was sent
121 and the peer's "close notify" alert was received.
122
123 =item E<lt>0
124
125 The shutdown was not successful.
126 Call L<SSL_get_error(3)> with the return value B<ret> to find out the reason.
127 It can occur if an action is needed to continue the operation for non-blocking
128 BIOs.
129
130 It can also occur when not all data was read using SSL_read().
131
132 =back
133
134 =head1 SEE ALSO
135
136 L<SSL_get_error(3)>, L<SSL_connect(3)>,
137 L<SSL_accept(3)>, L<SSL_set_shutdown(3)>,
138 L<SSL_CTX_set_quiet_shutdown(3)>,
139 L<SSL_clear(3)>, L<SSL_free(3)>,
140 L<ssl(7)>, L<bio(7)>
141
142 =head1 COPYRIGHT
143
144 Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
145
146 Licensed under the OpenSSL license (the "License").  You may not use
147 this file except in compliance with the License.  You can obtain a copy
148 in the file LICENSE in the source distribution or at
149 L<https://www.openssl.org/source/license.html>.
150
151 =cut