DTLSv1_listen.pod 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. =pod
  2. =head1 NAME
  3. SSL_stateless,
  4. DTLSv1_listen
  5. - Statelessly listen for incoming connections
  6. =head1 SYNOPSIS
  7. #include <openssl/ssl.h>
  8. int SSL_stateless(SSL *s);
  9. int DTLSv1_listen(SSL *ssl, BIO_ADDR *peer);
  10. =head1 DESCRIPTION
  11. SSL_stateless() statelessly listens for new incoming TLSv1.3 connections.
  12. DTLSv1_listen() statelessly listens for new incoming DTLS connections. If a
  13. ClientHello is received that does not contain a cookie, then they respond with a
  14. request for a new ClientHello that does contain a cookie. If a ClientHello is
  15. received with a cookie that is verified then the function returns in order to
  16. enable the handshake to be completed (for example by using SSL_accept()).
  17. =head1 NOTES
  18. Some transport protocols (such as UDP) can be susceptible to amplification
  19. attacks. Unlike TCP there is no initial connection setup in UDP that
  20. validates that the client can actually receive messages on its advertised source
  21. address. An attacker could forge its source IP address and then send handshake
  22. initiation messages to the server. The server would then send its response to
  23. the forged source IP. If the response messages are larger than the original
  24. message then the amplification attack has succeeded.
  25. If DTLS is used over UDP (or any datagram based protocol that does not validate
  26. the source IP) then it is susceptible to this type of attack. TLSv1.3 is
  27. designed to operate over a stream-based transport protocol (such as TCP).
  28. If TCP is being used then there is no need to use SSL_stateless(). However, some
  29. stream-based transport protocols (e.g. QUIC) may not validate the source
  30. address. In this case a TLSv1.3 application would be susceptible to this attack.
  31. As a countermeasure to this issue TLSv1.3 and DTLS include a stateless cookie
  32. mechanism. The idea is that when a client attempts to connect to a server it
  33. sends a ClientHello message. The server responds with a HelloRetryRequest (in
  34. TLSv1.3) or a HelloVerifyRequest (in DTLS) which contains a unique cookie. The
  35. client then resends the ClientHello, but this time includes the cookie in the
  36. message thus proving that the client is capable of receiving messages sent to
  37. that address. All of this can be done by the server without allocating any
  38. state, and thus without consuming expensive resources.
  39. OpenSSL implements this capability via the SSL_stateless() and DTLSv1_listen()
  40. functions. The B<ssl> parameter should be a newly allocated SSL object with its
  41. read and write BIOs set, in the same way as might be done for a call to
  42. SSL_accept(). Typically, for DTLS, the read BIO will be in an "unconnected"
  43. state and thus capable of receiving messages from any peer.
  44. When a ClientHello is received that contains a cookie that has been verified,
  45. then these functions will return with the B<ssl> parameter updated into a state
  46. where the handshake can be continued by a call to (for example) SSL_accept().
  47. Additionally, for DTLSv1_listen(), the B<BIO_ADDR> pointed to by B<peer> will be
  48. filled in with details of the peer that sent the ClientHello. If the underlying
  49. BIO is unable to obtain the B<BIO_ADDR> of the peer (for example because the BIO
  50. does not support this), then B<*peer> will be cleared and the family set to
  51. AF_UNSPEC. Typically user code is expected to "connect" the underlying socket to
  52. the peer and continue the handshake in a connected state.
  53. Warning: It is essential that the calling code connects the underlying socket to
  54. the peer after making use of DTLSv1_listen(). In the typical case where
  55. L<BIO_s_datagram(3)> is used, the peer address is updated when receiving a
  56. datagram on an unconnected socket. If the socket is not connected, it can
  57. receive datagrams from any host on the network, which will cause subsequent
  58. outgoing datagrams transmitted by DTLS to be transmitted to that host. In other
  59. words, failing to call BIO_connect() or a similar OS-specific function on a
  60. socket means that any host on the network can cause outgoing DTLS traffic to be
  61. redirected to it by sending a datagram to the socket in question. This does not
  62. break the cryptographic protections of DTLS but may facilitate a
  63. denial-of-service attack or allow unencrypted information in the DTLS handshake
  64. to be learned by an attacker. This is due to the historical design of
  65. L<BIO_s_datagram(3)>; see L<BIO_s_datagram(3)> for details on this issue.
  66. Once a socket has been connected, L<BIO_ctrl_set_connected(3)> should be used to
  67. inform the BIO that the socket is to be used in connected mode.
  68. Prior to calling DTLSv1_listen() user code must ensure that cookie generation
  69. and verification callbacks have been set up using
  70. L<SSL_CTX_set_cookie_generate_cb(3)> and L<SSL_CTX_set_cookie_verify_cb(3)>
  71. respectively. For SSL_stateless(), L<SSL_CTX_set_stateless_cookie_generate_cb(3)>
  72. and L<SSL_CTX_set_stateless_cookie_verify_cb(3)> must be used instead.
  73. Since DTLSv1_listen() operates entirely statelessly whilst processing incoming
  74. ClientHellos it is unable to process fragmented messages (since this would
  75. require the allocation of state). An implication of this is that DTLSv1_listen()
  76. B<only> supports ClientHellos that fit inside a single datagram.
  77. For SSL_stateless() if an entire ClientHello message cannot be read without the
  78. "read" BIO becoming empty then the SSL_stateless() call will fail. It is the
  79. application's responsibility to ensure that data read from the "read" BIO during
  80. a single SSL_stateless() call is all from the same peer.
  81. SSL_stateless() will fail (with a 0 return value) if some TLS version less than
  82. TLSv1.3 is used.
  83. Both SSL_stateless() and DTLSv1_listen() will clear the error queue when they
  84. start.
  85. SSL_stateless() cannot be used with QUIC SSL objects and returns an error if
  86. called on such an object.
  87. =head1 RETURN VALUES
  88. For SSL_stateless() a return value of 1 indicates success and the B<ssl> object
  89. will be set up ready to continue the handshake. A return value of 0 or -1
  90. indicates failure. If the value is 0 then a HelloRetryRequest was sent. A value
  91. of -1 indicates any other error. User code may retry the SSL_stateless() call.
  92. For DTLSv1_listen() a return value of >= 1 indicates success. The B<ssl> object
  93. will be set up ready to continue the handshake. the B<peer> value will also be
  94. filled in.
  95. A return value of 0 indicates a non-fatal error. This could (for
  96. example) be because of nonblocking IO, or some invalid message having been
  97. received from a peer. Errors may be placed on the OpenSSL error queue with
  98. further information if appropriate. Typically user code is expected to retry the
  99. call to DTLSv1_listen() in the event of a non-fatal error.
  100. A return value of <0 indicates a fatal error. This could (for example) be
  101. because of a failure to allocate sufficient memory for the operation.
  102. For DTLSv1_listen(), prior to OpenSSL 1.1.0, fatal and non-fatal errors both
  103. produce return codes <= 0 (in typical implementations user code treats all
  104. errors as non-fatal), whilst return codes >0 indicate success.
  105. =head1 SEE ALSO
  106. L<SSL_CTX_set_cookie_generate_cb(3)>, L<SSL_CTX_set_cookie_verify_cb(3)>,
  107. L<SSL_CTX_set_stateless_cookie_generate_cb(3)>,
  108. L<SSL_CTX_set_stateless_cookie_verify_cb(3)>, L<SSL_get_error(3)>,
  109. L<SSL_accept(3)>, L<ssl(7)>, L<bio(7)>
  110. =head1 HISTORY
  111. The SSL_stateless() function was added in OpenSSL 1.1.1.
  112. The DTLSv1_listen() return codes were clarified in OpenSSL 1.1.0.
  113. The type of "peer" also changed in OpenSSL 1.1.0.
  114. =head1 COPYRIGHT
  115. Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved.
  116. Licensed under the Apache License 2.0 (the "License"). You may not use
  117. this file except in compliance with the License. You can obtain a copy
  118. in the file LICENSE in the source distribution or at
  119. L<https://www.openssl.org/source/license.html>.
  120. =cut