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