SSL_shutdown.pod 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. =head1 NOTES
  11. SSL_shutdown() tries to send the "close notify" shutdown alert to the peer.
  12. Whether the operation succeeds or not, the SSL_SENT_SHUTDOWN flag is set and
  13. a currently open session is considered closed and good and will be kept in the
  14. session cache for further reuse.
  15. The shutdown procedure consists of 2 steps: the sending of the "close notify"
  16. shutdown alert and the reception of the peer's "close notify" shutdown
  17. alert. According to the TLS standard, it is acceptable for an application
  18. to only send its shutdown alert and then close the underlying connection
  19. without waiting for the peer's response (this way resources can be saved,
  20. as the process can already terminate or serve another connection).
  21. When the underlying connection shall be used for more communications, the
  22. complete shutdown procedure (bidirectional "close notify" alerts) must be
  23. performed, so that the peers stay synchronized.
  24. SSL_shutdown() supports both uni- and bidirectional shutdown by its 2 step
  25. behaviour.
  26. SSL_shutdown() only closes the write direction.
  27. It is not possible to call SSL_write() after calling SSL_shutdown().
  28. The read direction is closed by the peer.
  29. =head2 First to close the connection
  30. When the application is the first party to send the "close notify"
  31. alert, SSL_shutdown() will only send the alert and then set the
  32. SSL_SENT_SHUTDOWN flag (so that the session is considered good and will
  33. be kept in the cache).
  34. SSL_shutdown() will then return with 0.
  35. If a unidirectional shutdown is enough (the underlying connection shall be
  36. closed anyway), this first call to SSL_shutdown() is sufficient.
  37. In order to complete the bidirectional shutdown handshake, the peer needs
  38. to send back a "close notify" alert.
  39. The SSL_RECEIVED_SHUTDOWN flag will be set after receiving and processing
  40. it.
  41. SSL_shutdown() will return 1 when it has been received.
  42. The peer is still allowed to send data after receiving the "close notify"
  43. event.
  44. If the peer did send data it needs to be processed by calling SSL_read()
  45. before calling SSL_shutdown() a second time.
  46. SSL_read() will indicate the end of the peer data by returning <= 0
  47. and SSL_get_error() returning SSL_ERROR_ZERO_RETURN.
  48. It is recommended to call SSL_read() between SSL_shutdown() calls.
  49. =head2 Peer closes the connection
  50. If the peer already sent the "close notify" alert B<and> it was
  51. already processed implicitly inside another function
  52. (L<SSL_read(3)>), the SSL_RECEIVED_SHUTDOWN flag is set.
  53. SSL_read() will return <= 0 in that case, and SSL_get_error() will return
  54. SSL_ERROR_ZERO_RETURN.
  55. SSL_shutdown() will send the "close notify" alert, set the SSL_SENT_SHUTDOWN
  56. flag and will immediately return with 1.
  57. Whether SSL_RECEIVED_SHUTDOWN is already set can be checked using the
  58. SSL_get_shutdown() (see also L<SSL_set_shutdown(3)> call.
  59. =head1 NOTES
  60. It is recommended to do a bidirectional shutdown by checking the return value
  61. of SSL_shutdown() and call it again until it returns 1 or a fatal error.
  62. The behaviour of SSL_shutdown() additionally depends on the underlying BIO.
  63. If the underlying BIO is B<blocking>, SSL_shutdown() will only return once the
  64. handshake step has been finished or an error occurred.
  65. If the underlying BIO is B<non-blocking>, SSL_shutdown() will also return
  66. when the underlying BIO could not satisfy the needs of SSL_shutdown()
  67. to continue the handshake. In this case a call to SSL_get_error() with the
  68. return value of SSL_shutdown() will yield B<SSL_ERROR_WANT_READ> or
  69. B<SSL_ERROR_WANT_WRITE>. The calling process then must repeat the call after
  70. taking appropriate action to satisfy the needs of SSL_shutdown().
  71. The action depends on the underlying BIO. When using a non-blocking socket,
  72. nothing is to be done, but select() can be used to check for the required
  73. condition. When using a buffering BIO, like a BIO pair, data must be written
  74. into or retrieved out of the BIO before being able to continue.
  75. SSL_shutdown() can be modified to only set the connection to "shutdown"
  76. state but not actually send the "close notify" alert messages,
  77. see L<SSL_CTX_set_quiet_shutdown(3)>.
  78. When "quiet shutdown" is enabled, SSL_shutdown() will always succeed
  79. and return 1.
  80. =head1 RETURN VALUES
  81. The following return values can occur:
  82. =over 4
  83. =item Z<>0
  84. The shutdown is not yet finished: the "close notify" was send but the peer
  85. did not send it back yet.
  86. Call SSL_shutdown() again to do a bidirectional shutdown.
  87. The output of L<SSL_get_error(3)> may be misleading, as an
  88. erroneous SSL_ERROR_SYSCALL may be flagged even though no error occurred.
  89. =item Z<>1
  90. The shutdown was successfully completed. The "close notify" alert was sent
  91. and the peer's "close notify" alert was received.
  92. =item E<lt>0
  93. The shutdown was not successful.
  94. Call L<SSL_get_error(3)> with the return value B<ret> to find out the reason.
  95. It can occur if an action is needed to continue the operation for non-blocking
  96. BIOs.
  97. It can also occur when not all data was read using SSL_read().
  98. =back
  99. =head1 SEE ALSO
  100. L<SSL_get_error(3)>, L<SSL_connect(3)>,
  101. L<SSL_accept(3)>, L<SSL_set_shutdown(3)>,
  102. L<SSL_CTX_set_quiet_shutdown(3)>,
  103. L<SSL_clear(3)>, L<SSL_free(3)>,
  104. L<ssl(7)>, L<bio(7)>
  105. =head1 COPYRIGHT
  106. Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
  107. Licensed under the OpenSSL license (the "License"). You may not use
  108. this file except in compliance with the License. You can obtain a copy
  109. in the file LICENSE in the source distribution or at
  110. L<https://www.openssl.org/source/license.html>.
  111. =cut