SSL_shutdown.pod 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. =pod
  2. =head1 NAME
  3. SSL_shutdown - shut down a TLS/SSL connection
  4. =head1 SYNOPSIS
  5. #include <openssl/ssl.h>
  6. int SSL_shutdown(SSL *ssl);
  7. =head1 DESCRIPTION
  8. SSL_shutdown() shuts down an active TLS/SSL connection. It sends the
  9. close_notify shutdown alert to the peer.
  10. SSL_shutdown() tries to send the close_notify shutdown alert to the peer.
  11. Whether the operation succeeds or not, the SSL_SENT_SHUTDOWN flag is set and
  12. a currently open session is considered closed and good and will be kept in the
  13. session cache for further reuse.
  14. Note that SSL_shutdown() must not be called if a previous fatal error has
  15. occurred on a connection i.e. if SSL_get_error() has returned SSL_ERROR_SYSCALL
  16. or SSL_ERROR_SSL.
  17. The shutdown procedure consists of two steps: sending of the close_notify
  18. shutdown alert, and reception of the peer's close_notify shutdown alert.
  19. The order of those two steps depends on the application.
  20. It is acceptable for an application to only send its shutdown alert and
  21. then close the underlying connection without waiting for the peer's response.
  22. This way resources can be saved, as the process can already terminate or
  23. serve another connection.
  24. This should only be done when it is known that the other side will not send more
  25. data, otherwise there is a risk of a truncation attack.
  26. When a client only writes and never reads from the connection, and the server
  27. has sent a session ticket to establish a session, the client might not be able
  28. to resume the session because it did not received and process the session ticket
  29. from the server.
  30. In case the application wants to be able to resume the session, it is recommended to
  31. do a complete shutdown procedure (bidirectional close_notify alerts).
  32. When the underlying connection shall be used for more communications, the
  33. complete shutdown procedure must be performed, so that the peers stay
  34. synchronized.
  35. SSL_shutdown() only closes the write direction.
  36. It is not possible to call SSL_write() after calling SSL_shutdown().
  37. The read direction is closed by the peer.
  38. The behaviour of SSL_shutdown() additionally depends on the underlying BIO.
  39. If the underlying BIO is B<blocking>, SSL_shutdown() will only return once the
  40. handshake step has been finished or an error occurred.
  41. If the underlying BIO is B<nonblocking>, SSL_shutdown() will also return
  42. when the underlying BIO could not satisfy the needs of SSL_shutdown()
  43. to continue the handshake. In this case a call to SSL_get_error() with the
  44. return value of SSL_shutdown() will yield B<SSL_ERROR_WANT_READ> or
  45. B<SSL_ERROR_WANT_WRITE>. The calling process then must repeat the call after
  46. taking appropriate action to satisfy the needs of SSL_shutdown().
  47. The action depends on the underlying BIO. When using a nonblocking socket,
  48. nothing is to be done, but select() can be used to check for the required
  49. condition. When using a buffering BIO, like a BIO pair, data must be written
  50. into or retrieved out of the BIO before being able to continue.
  51. After SSL_shutdown() returned 0, it is possible to call SSL_shutdown() again
  52. to wait for the peer's close_notify alert.
  53. SSL_shutdown() will return 1 in that case.
  54. However, it is recommended to wait for it using SSL_read() instead.
  55. SSL_shutdown() can be modified to only set the connection to "shutdown"
  56. state but not actually send the close_notify alert messages,
  57. see L<SSL_CTX_set_quiet_shutdown(3)>.
  58. When "quiet shutdown" is enabled, SSL_shutdown() will always succeed
  59. and return 1.
  60. Note that this is not standard compliant behaviour.
  61. It should only be done when the peer has a way to make sure all
  62. data has been received and doesn't wait for the close_notify alert
  63. message, otherwise an unexpected EOF will be reported.
  64. There are implementations that do not send the required close_notify alert.
  65. If there is a need to communicate with such an implementation, and it's clear
  66. that all data has been received, do not wait for the peer's close_notify alert.
  67. Waiting for the close_notify alert when the peer just closes the connection
  68. will result in an error being generated.
  69. The error can be ignored using the B<SSL_OP_IGNORE_UNEXPECTED_EOF>.
  70. For more information see L<SSL_CTX_set_options(3)>.
  71. =head2 First to close the connection
  72. When the application is the first party to send the close_notify
  73. alert, SSL_shutdown() will only send the alert and then set the
  74. SSL_SENT_SHUTDOWN flag (so that the session is considered good and will
  75. be kept in the cache).
  76. If successful, SSL_shutdown() will return 0.
  77. If a unidirectional shutdown is enough (the underlying connection shall be
  78. closed anyway), this first successful call to SSL_shutdown() is sufficient.
  79. In order to complete the bidirectional shutdown handshake, the peer needs
  80. to send back a close_notify alert.
  81. The SSL_RECEIVED_SHUTDOWN flag will be set after receiving and processing
  82. it.
  83. The peer is still allowed to send data after receiving the close_notify
  84. event.
  85. When it is done sending data, it will send the close_notify alert.
  86. SSL_read() should be called until all data is received.
  87. SSL_read() will indicate the end of the peer data by returning <= 0
  88. and SSL_get_error() returning SSL_ERROR_ZERO_RETURN.
  89. =head2 Peer closes the connection
  90. If the peer already sent the close_notify alert B<and> it was
  91. already processed implicitly inside another function
  92. (L<SSL_read(3)>), the SSL_RECEIVED_SHUTDOWN flag is set.
  93. SSL_read() will return <= 0 in that case, and SSL_get_error() will return
  94. SSL_ERROR_ZERO_RETURN.
  95. SSL_shutdown() will send the close_notify alert, set the SSL_SENT_SHUTDOWN
  96. flag.
  97. If successful, SSL_shutdown() will return 1.
  98. Whether SSL_RECEIVED_SHUTDOWN is already set can be checked using the
  99. SSL_get_shutdown() (see also L<SSL_set_shutdown(3)> call.
  100. =head1 RETURN VALUES
  101. The following return values can occur:
  102. =over 4
  103. =item Z<>0
  104. The shutdown is not yet finished: the close_notify was sent but the peer
  105. did not send it back yet.
  106. Call SSL_read() to do a bidirectional shutdown.
  107. Unlike most other function, returning 0 does not indicate an error.
  108. L<SSL_get_error(3)> should not get called, it may misleadingly
  109. indicate an error even though no error occurred.
  110. =item Z<>1
  111. The shutdown was successfully completed. The close_notify alert was sent
  112. and the peer's close_notify alert was received.
  113. =item E<lt>0
  114. The shutdown was not successful.
  115. Call L<SSL_get_error(3)> with the return value B<ret> to find out the reason.
  116. It can occur if an action is needed to continue the operation for nonblocking
  117. BIOs.
  118. It can also occur when not all data was read using SSL_read().
  119. =back
  120. =head1 SEE ALSO
  121. L<SSL_get_error(3)>, L<SSL_connect(3)>,
  122. L<SSL_accept(3)>, L<SSL_set_shutdown(3)>,
  123. L<SSL_CTX_set_quiet_shutdown(3)>, L<SSL_CTX_set_options(3)>
  124. L<SSL_clear(3)>, L<SSL_free(3)>,
  125. L<ssl(7)>, L<bio(7)>
  126. =head1 COPYRIGHT
  127. Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
  128. Licensed under the Apache License 2.0 (the "License"). You may not use
  129. this file except in compliance with the License. You can obtain a copy
  130. in the file LICENSE in the source distribution or at
  131. L<https://www.openssl.org/source/license.html>.
  132. =cut