Better description of the behaviour of SSL_shutdown() as it is now, broken
[openssl.git] / doc / ssl / 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 receipt ion of the peer's "close notify" shutdown
27 alert:
28
29 =over 4
30
31 =item When the application is the first party to send the "close notify"
32 alert, SSL_shutdown() will only send the alert and the set the
33 SSL_SENT_SHUTDOWN flag (so that the session is considered good and will
34 be kept in cache). SSL_shutdown() will then return with 0. In order to
35 complete the shutdown handshake, SSL_shutdown() must be called again.
36 The second call will make SSL_shutdown() wait for the peer's "close notify"
37 shutdown alert. On success, the second call to SSL_shutdown() will return
38 with 1.
39
40 =item If the peer already sent the "close notify" alert B<and> it was
41 already processed implicitly inside another call of e.g.
42 B<SSL_read(3)|SSL_read(3)>, SSL_shutdown() will send the "close notify"
43 alert and will immediately return with 1.
44
45 =back
46
47 It is therefore recommended, to check the return value of SSL_shutdown()
48 and call SSL_shutdown() again, if the bidirectional shutdown is not yet
49 complete (return value of the first call is 0). As the shutdown is not
50 specially handled in the SSLv2 protocol, SSL_shutdown() will succeed on
51 the first call.
52
53 The behaviour of SSL_shutdown() additionally depends on the underlying BIO. 
54
55 If the underlying BIO is B<blocking>, SSL_shutdown() will only return once the
56 handshake step has been finished or an error occurred.
57
58 If the underlying BIO is B<non-blocking>, SSL_shutdown() will also return
59 when the underlying BIO could not satisfy the needs of SSL_shutdown()
60 to continue the handshake. In this case a call to SSL_get_error() with the
61 return value of SSL_shutdown() will yield B<SSL_ERROR_WANT_READ> or
62 B<SSL_ERROR_WANT_WRITE>. The calling process then must repeat the call after
63 taking appropriate action to satisfy the needs of SSL_shutdown().
64 The action depends on the underlying BIO. When using a non-blocking socket,
65 nothing is to be done, but select() can be used to check for the required
66 condition. When using a buffering BIO, like a BIO pair, data must be written
67 into or retrieved out of the BIO before being able to continue.
68
69 =head1 RETURN VALUES
70
71 The following return values can occur:
72
73 =over 4
74
75 =item 1
76
77 The shutdown was successfully completed. The "close notify" alert was sent
78 and the peer's "close notify" alert was received.
79
80 =item 0
81
82 The shutdown is not yet finished. Call SSL_shutdown() for a second time.
83 The output of L<SSL_get_error(3)|SSL_get_error(3)> may be misleading, as an
84 erroneous SSL_ERROR_SYSCALL may be flagged even though no error occurred.
85
86 =item -1
87
88 The shutdown was not successful because a fatal error occurred either
89 at the protocol level or a connection failure occurred. It can also occur if
90 action is need to continue the operation for non-blocking BIOs.
91 Call L<SSL_get_error(3)|SSL_get_error(3)> with the return value B<ret>
92 to find out the reason.
93
94 =back
95
96 =head1 SEE ALSO
97
98 L<SSL_get_error(3)|SSL_get_error(3)>, L<SSL_connect(3)|SSL_connect(3)>,
99 L<SSL_accept(3)|SSL_accept(3)>, L<SSL_set_shutdown(3)|SSL_set_shutdown(3)>,
100 L<SSL_clear(3)|SSL_clear(3)>, L<SSL_free(3)|SSL_free(3)>,
101 L<ssl(3)|ssl(3)>, L<bio(3)|bio(3)>
102
103 =cut