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