SSL_shutdown.pod revision 344604
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 
16"close notify" shutdown alert to the peer.
17
18=head1 NOTES
19
20SSL_shutdown() tries to send the "close notify" shutdown alert to the peer.
21Whether the operation succeeds or not, the SSL_SENT_SHUTDOWN flag is set and
22a currently open session is considered closed and good and will be kept in the
23session cache for further reuse.
24
25Note that SSL_shutdown() must not be called if a previous fatal error has
26occurred on a connection i.e. if SSL_get_error() has returned SSL_ERROR_SYSCALL
27or SSL_ERROR_SSL.
28
29The shutdown procedure consists of 2 steps: the sending of the "close notify"
30shutdown alert and the reception of the peer's "close notify" shutdown
31alert. According to the TLS standard, it is acceptable for an application
32to only send its shutdown alert and then close the underlying connection
33without waiting for the peer's response (this way resources can be saved,
34as the process can already terminate or serve another connection).
35When the underlying connection shall be used for more communications, the
36complete shutdown procedure (bidirectional "close notify" alerts) must be
37performed, so that the peers stay synchronized.
38
39SSL_shutdown() supports both uni- and bidirectional shutdown by its 2 step
40behaviour.
41
42=over 4
43
44=item When the application is the first party to send the "close notify"
45alert, SSL_shutdown() will only send the alert and then set the
46SSL_SENT_SHUTDOWN flag (so that the session is considered good and will
47be kept in cache). SSL_shutdown() will then return with 0. If a unidirectional
48shutdown is enough (the underlying connection shall be closed anyway), this
49first call to SSL_shutdown() is sufficient. In order to complete the
50bidirectional shutdown handshake, SSL_shutdown() must be called again.
51The second call will make SSL_shutdown() wait for the peer's "close notify"
52shutdown alert. On success, the second call to SSL_shutdown() will return
53with 1.
54
55=item If the peer already sent the "close notify" alert B<and> it was
56already processed implicitly inside another function
57(L<SSL_read(3)|SSL_read(3)>), the SSL_RECEIVED_SHUTDOWN flag is set.
58SSL_shutdown() will send the "close notify" alert, set the SSL_SENT_SHUTDOWN
59flag and will immediately return with 1.
60Whether SSL_RECEIVED_SHUTDOWN is already set can be checked using the
61SSL_get_shutdown() (see also L<SSL_set_shutdown(3)|SSL_set_shutdown(3)> call.
62
63=back
64
65It is therefore recommended, to check the return value of SSL_shutdown()
66and call SSL_shutdown() again, if the bidirectional shutdown is not yet
67complete (return value of the first call is 0). As the shutdown is not
68specially handled in the SSLv2 protocol, SSL_shutdown() will succeed on
69the first call.
70
71The behaviour of SSL_shutdown() additionally depends on the underlying BIO. 
72
73If the underlying BIO is B<blocking>, SSL_shutdown() will only return once the
74handshake step has been finished or an error occurred.
75
76If the underlying BIO is B<non-blocking>, SSL_shutdown() will also return
77when the underlying BIO could not satisfy the needs of SSL_shutdown()
78to continue the handshake. In this case a call to SSL_get_error() with the
79return value of SSL_shutdown() will yield B<SSL_ERROR_WANT_READ> or
80B<SSL_ERROR_WANT_WRITE>. The calling process then must repeat the call after
81taking appropriate action to satisfy the needs of SSL_shutdown().
82The action depends on the underlying BIO. When using a non-blocking socket,
83nothing is to be done, but select() can be used to check for the required
84condition. When using a buffering BIO, like a BIO pair, data must be written
85into or retrieved out of the BIO before being able to continue.
86
87SSL_shutdown() can be modified to only set the connection to "shutdown"
88state but not actually send the "close notify" alert messages,
89see L<SSL_CTX_set_quiet_shutdown(3)|SSL_CTX_set_quiet_shutdown(3)>.
90When "quiet shutdown" is enabled, SSL_shutdown() will always succeed
91and return 1.
92
93=head1 RETURN VALUES
94
95The following return values can occur:
96
97=over 4
98
99=item Z<>0
100
101The shutdown is not yet finished. Call SSL_shutdown() for a second time,
102if a bidirectional shutdown shall be performed.
103The output of L<SSL_get_error(3)|SSL_get_error(3)> may be misleading, as an
104erroneous SSL_ERROR_SYSCALL may be flagged even though no error occurred.
105
106=item Z<>1
107
108The shutdown was successfully completed. The "close notify" alert was sent
109and the peer's "close notify" alert was received.
110
111=item E<lt>0
112
113The shutdown was not successful because a fatal error occurred either
114at the protocol level or a connection failure occurred. It can also occur if
115action is need to continue the operation for non-blocking BIOs.
116Call L<SSL_get_error(3)|SSL_get_error(3)> with the return value B<ret>
117to find out the reason.
118
119=back
120
121=head1 SEE ALSO
122
123L<SSL_get_error(3)|SSL_get_error(3)>, L<SSL_connect(3)|SSL_connect(3)>,
124L<SSL_accept(3)|SSL_accept(3)>, L<SSL_set_shutdown(3)|SSL_set_shutdown(3)>,
125L<SSL_CTX_set_quiet_shutdown(3)|SSL_CTX_set_quiet_shutdown(3)>,
126L<SSL_clear(3)|SSL_clear(3)>, L<SSL_free(3)|SSL_free(3)>,
127L<ssl(3)|ssl(3)>, L<bio(3)|bio(3)>
128
129=cut
130