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