DTLSV1_LISTEN(3ossl)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

πŸ’… DTLSV1_LISTEN(3ossl) πŸŽ€

okay bestie, welcome to DTLSV1_LISTEN(3ossl) β€” that's the OpenSSL manual page, section 3ossl, and it's serving statelessly listen for incoming connections realness. two functions, one vibe. ngl this one is kind of a banger βœ¨πŸ¦‹


🌸 NAME 🌸

SSL_stateless, DTLSv1_listen β€” statelessly listen for incoming connections πŸŽ€

✨ SYNOPSIS ✨

#include <openssl/ssl.h>

int SSL_stateless(SSL *s);
int DTLSv1_listen(SSL *ssl, BIO_ADDR *peer);

πŸ‘‘ DESCRIPTION πŸ‘‘

okay so here's the tea β˜•: SSL_stateless() statelessly listens for new incoming TLSv1.3 connections. DTLSv1_listen() does the exact same stateless-listening thing but for new incoming DTLS connections.

now for the fun part πŸ’… β€” if a ClientHello comes in that does not contain a cookie, they respond with a request for a brand-new ClientHello that does contain a cookie. and if a ClientHello arrives carrying a cookie that gets verified? the function returns, so the handshake can be completed (for example by using SSL_accept()). slay. periodt. πŸ”₯

🌈 NOTES 🌈

first up, some lore you genuinely need: some transport protocols (UDP, i'm looking at you πŸ‘€) can be susceptible to amplification attacks. unlike TCP, there's no initial connection setup in UDP that validates the client can actually receive messages on its advertised source address. so an attacker can forge its source IP address, then send handshake initiation messages to the server, and the server just... sends its response to the forged source IP. 😭 and if those response messages are bigger than the original message, then the amplification attack has succeeded. not iconic.

if DTLS is used over UDP (or any datagram-based protocol that doesn't validate the source IP), then it's susceptible to this type of attack. TLSv1.3 is designed to operate over a stream-based transport protocol (like TCP) β€” and if TCP is being used, then there's no need to use SSL_stateless(). however (and this is the plot twist), some stream-based transport protocols (e.g. QUIC) may not validate the source address, and in that case a TLSv1.3 application would be susceptible to this attack. πŸ’€

so as a countermeasure to this whole issue, TLSv1.3 and DTLS include a stateless cookie mechanism. the idea is: when a client attempts to connect to a server, it sends a ClientHello message. the server responds with a HelloRetryRequest (in TLSv1.3) or a HelloVerifyRequest (in DTLS) which contains a unique cookie πŸͺ. the client then resends the ClientHello, but this time includes the cookie in the message β€” thus proving the client is capable of receiving messages sent to that address. and all of this can be done by the server without allocating any state, and thus without consuming expensive resources. main character energy fr fr ✨

OpenSSL implements this capability via the SSL_stateless() and DTLSv1_listen() functions. the ssl parameter should be a newly allocated SSL object with its read and write BIOs set, in the same way as might be done for a call to SSL_accept(). typically, for DTLS, the read BIO will be in an "unconnected" state and thus capable of receiving messages from any peer. 🫢

when a ClientHello is received that contains a cookie that has been verified, then these functions will return with the ssl parameter updated into a state where the handshake can be continued by a call to (for example) SSL_accept(). additionally, for DTLSv1_listen(), the BIO_ADDR pointed to by peer will be filled in with details of the peer that sent the ClientHello. if the underlying BIO is unable to obtain the BIO_ADDR of the peer (for example because the BIO does not support this), then *peer will be cleared and the family set to AF_UNSPEC. typically user code is expected to "connect" the underlying socket to the peer and continue the handshake in a connected state. πŸ’–


⚠️ warning bestie, this part is serious ⚠️ it is essential that the calling code connects the underlying socket to the peer after making use of DTLSv1_listen(). in the typical case where BIO_s_datagram(3) is used, the peer address is updated when receiving a datagram on an unconnected socket. if the socket is not connected, it can receive datagrams from any host on the network, which will cause subsequent outgoing datagrams transmitted by DTLS to be transmitted to that host. in other words: failing to call BIO_connect() or a similar OS-specific function on a socket means that any host on the network can cause outgoing DTLS traffic to be redirected to it by sending a datagram to the socket in question. 😱 this does not break the cryptographic protections of DTLS, but it may facilitate a denial-of-service attack or allow unencrypted information in the DTLS handshake to be learned by an attacker. this is due to the historical design of BIO_s_datagram(3); see BIO_s_datagram(3) for details on this issue.

once a socket has been connected, BIO_ctrl_set_connected(3) should be used to inform the BIO that the socket is to be used in connected mode. πŸŽ€

prior to calling DTLSv1_listen(), user code must ensure that cookie generation and verification callbacks have been set up using SSL_CTX_set_cookie_generate_cb(3) and SSL_CTX_set_cookie_verify_cb(3) respectively. for SSL_stateless(), SSL_CTX_set_stateless_cookie_generate_cb(3) and SSL_CTX_set_stateless_cookie_verify_cb(3) must be used instead. no skipping this step, iykyk πŸ’…

since DTLSv1_listen() operates entirely statelessly whilst processing incoming ClientHellos, it is unable to process fragmented messages (since this would require the allocation of state). an implication of this is that DTLSv1_listen() only supports ClientHellos that fit inside a single datagram. πŸ¦‹

for SSL_stateless(), if an entire ClientHello message cannot be read without the "read" BIO becoming empty, then the SSL_stateless() call will fail. it is the application's responsibility to ensure that data read from the "read" BIO during a single SSL_stateless() call is all from the same peer. πŸ‘‘

SSL_stateless() will fail (with a 0 return value) if some TLS version less than TLSv1.3 is used. πŸ™ˆ

both SSL_stateless() and DTLSv1_listen() will clear the error queue when they start. yes, both. it's giving tidy. ✨

SSL_stateless() cannot be used with QUIC SSL objects and returns an error if called on such an object. πŸ’€

🧚 RETURN VALUES 🧚

  • SSL_stateless() β€” a return value of 1 indicates success, and the ssl object will be set up ready to continue the handshake. a return value of 0 or -1 indicates failure. if the value is 0, then a HelloRetryRequest was sent. a value of -1 indicates any other error. user code may retry the SSL_stateless() call. πŸ”
  • DTLSv1_listen() β€” a return value of >= 1 indicates success. the ssl object will be set up ready to continue the handshake. the peer value will also be filled in. πŸ’–
  • a return value of 0 indicates a non-fatal error. this could (for example) be because of nonblocking IO, or some invalid message having been received from a peer. errors may be placed on the OpenSSL error queue with further information if appropriate. typically user code is expected to retry the call to DTLSv1_listen() in the event of a non-fatal error. 🫢
  • a return value of <0 indicates a fatal error. this could (for example) be because of a failure to allocate sufficient memory for the operation. 😭
  • for DTLSv1_listen(), prior to OpenSSL 1.1.0, fatal and non-fatal errors both produce return codes <= 0 (in typical implementations user code treats all errors as non-fatal), whilst return codes >0 indicate success. ancient history but bestie we keep the receipts. πŸ“œ

πŸ¦‹ SEE ALSO πŸ¦‹

πŸ“œ HISTORY πŸ“œ

  • the SSL_stateless() function was added in OpenSSL 1.1.1. ✨
  • the DTLSv1_listen() return codes were clarified in OpenSSL 1.1.0. the type of "peer" also changed in OpenSSL 1.1.0. πŸ”₯

Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at https://www.openssl.org/source/license.html. πŸŽ€


and that's the whole page bestie, mwah πŸ’‹ β€” this one's from OpenSSL 3.5.6, dated 2026-04-07, and it's the DTLSV1_LISTEN(3ossl) manual page. go forth and listen statelessly, you absolute icon πŸ‘‘πŸŒˆ

πŸ“¦ where this page comes from