Skip to content

DTLS 1.3 - #6185

Open
reneme wants to merge 2 commits into
randombit:masterfrom
Rohde-Schwarz:feature/dtls13
Open

reneme wants to merge 2 commits into
randombit:masterfrom
Rohde-Schwarz:feature/dtls13

Conversation

@reneme

@reneme reneme commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

This introduces an implementation of DTLS 1.3 (RFC 9147) as an additional experimental module "dtls13". It is based on the various preparatory PRs listed in #5000 and slides into the abstractions introduced in them.

Essentially, the entire I/O layer is now abstract and provides two implementations for TLS and DTLS respectively. Most of this complexity is encapsulated in the Channel_IO class and its TLS_Channel_IO/DTLS_Channel_IO children which orchestrate flavor-specific sub-classes of Record_Layer, Handshake_Layer, and Cipher_State. The TLS state machine essentially stays the same for both TLS and DTLS with some minor conditional additions in case of DTLS.

Both BoGo (with a few exclusions) as well as plzn integration tests pass. First rounds of AI review didn't find any smoking guns. But of course, we're eager to see what Anthropic's OSS scanners come up with.

While we did have performance in mind for all parts of the implementation, this PR aims at a functionally correct protocol implementation first-and-foremost. Future improvements both on performance as well as resilience are obviously welcome, but given that this is meant to be an experimental module, we'd suggest to apply them iteratively as follow-ups.

New Components

Overview of added DTLS-specific components

DTLS_Channel_IO

  • Packing of handshake message flights into datagrams
  • Timer-based retransmission of handshake records
  • Handshake record acknowledgements
  • DTLS-specifics of post-handshake KeyUpdate (epoch rollover after ACK)

DTLS_Record_Layer

  • Parsing/Creation of DTLS records (plaintext as well as protected)
  • Rejection of invalid records (replays, decoding errors, failed deprotection, etc.)
  • Management of un-acknowledged (i.e. to-be-retransmitted) outgoing records

DTLS_Handshake_Layer

  • Fragmentation of serialized handshake messages
  • Reconstruction of incoming fragmented messages
  • Support for parsing of HelloVerifyRequest for downgrade to DTLS 1.2

DTLS_Cipher_State

  • Management of multiple (de)protection epochs
  • Encryption/Decryption of record sequence number hints
  • (De)protection of DTLS records

SingleshotTimer

  • Wraps TLS::Channel::tls_register_deferred_operation()
  • Handles pre-maturely called registered deferred operations via re-scheduling
  • Optionally provides a cancellable timer handle

RetransmissionTimer

  • Wraps multiple SingleshotTimers
  • Handles exponential backoff for retransmissions
  • Aborts associations on timeout or retransmission limit exhaustion
  • Optionally launches a "flight timer" bounding the wait for progress after a peer (implicitly) ACKed our flight (more details: [DTLS 1.3] Support for "Flight Timeouts" plzn#10)

Known Limitations and Open Issues

Deliberate omissions as well as potential future work

Downgrade transcript can pile up junk

The way the (D)TLS downgrade is implemented we currently pile up the entire incoming data transcript. When the state machine eventually detects a 1.3 to 1.2 downgrade, the internal implementation is exchanged and the entire received transcript is replayed for the then-active DTLS 1.2 implementation. Also the physical datagram boundaries are lost in the transcript stash, so truncated or deliberately invalid datagrams may corrupt the replay for the downgraded 1.2 implementation. This allows an attacker to pile up megabytes of junk. We'll have to make this more robust and perhaps cover this with some plzn regression tests.

DTLS 1.3 currently hard-requires TLS 1.3

The "dtls13" module depends on the "tls13" module, so DTLS 1.3 cannot be enabled without also enabling "tls13" at the moment. This could be addressed in the future, by introducing an internal "tls13_common" module that carries only the state machine implementation and abstract I/O layer components.

No support for Connection IDs (RFC 9146, RFC 9147 Section 9)

Connection IDs are transported in plaintext as part of the unified header of protected DTLS records and allow peers to map incoming datagrams with their appropriate security associations. This is helpful for applications that want to migrate existing DTLS sessions across different network transports.

Currently, Botan won't negotiate this extension and is thus compatible with implementations that support CIDs as long as their configuration profiles don't hard-require them.

Epoch0-Restart is not supported

If a peer sends a fresh ClientHello (epoch=0) into an existing association, it is simply ignored. No effort is taken to perform a new handshake that, once successful, replaces the existing association. In contrast, DTLS 1.2 does that. Note: an application can work around this limitation by actively detecting incoming ClientHello datagrams and launching their own shadow TLS::Server instance.

At this point it is unclear if and when this would ever be supported in DTLS 1.3. We could, however, implement a new TLS::Callbacks hook that notifies of an incoming ClientHello.

Unprotected ACKs can abort a handshake early-on

RFC 9147 7.2 Errata 8108 says:

If any element of record_numbers in the ACK references an epoch that
is higher than the epoch in which the ACK was received, the
implementation MUST terminate the connection with an
"illegal_parameter" alert.

For consistency with BoringSSL (and to pass the relevant BoGo tests), we currently implemented the validation verbatim. However, that means that an off-path attacker could kill an upcoming handshake by sending such a malicious unauthenticated ACK record.

The vulnerable window is small, because we blanket-reject all plaintext ACK records as soon as we successfully deprotected any protected record from the peer (which BoringSSL does as well).

There is a recent, ongoing discussion about it in in the TLS-WG:

We plan to revisit this once the spec issue is resolved.

The deprecated TLS::Channel::timeout_check() is not supported

When an existing application that uses timeout_check() to steer DTLS retransmissions upgrades to a Botan version that default-enables DTLS 1.3, it might surprisingly experience Not_Implemented exceptions from invoking this function. timeout_check() was deprecated recently for removal in Botan4, but still worth to be aware of. We don't plan to support the timeout_check() method for DTLS 1.3.

Re-fragmentation of messages after an MTU-change

The implementation can handle incoming retransmissions that were re-fragmented (eg. data offset overlaps with previous fragment), but it is currently not able to re-fragment and re-pack handshake datagrams after the MTU value reported by TLS::Policy::dtls_default_mtu() got reduced.

Future addition of this feature could benefit applications that require PQ keys and/or large certificate chains and thus have to rely on message fragmentation. If the MTU isn't known a-priori such applications will have to rely on a conservative guess of the MTU and thus loose throughput due to inefficient packing of datagrams.

While we believe that sophisticated MTU-discovery mechanisms should be out-of-scope for the library, a future improvement should provide enough flexibility so that an application can implement them efficiently for their specific use case.

Handshake message reassembly allocates upfront

We should look into abstracting the work done in #5803 and see if we can reuse it for DTLS 1.3 as well. The current implementation here works functionally but is vulnerable to large message pre-allocation.

Handling of pending KeyUpdates is currently sub-par

As discussed here, a pending KeyUpdate currently blocks all other user-solicited post-handshake messages (e.g. TLS::Server::send_new_session_tickets()) via an Invalid_State exception. Additionally, the ACKnowledgement of pending KeyUpdates is currently not handled via notify_received_complete_flight(). Instead, there's an ad-hoc handling tailored to just recognize the KeyUpdate's ACK and then act on it (i.e. advance the write epoch).

For now this is fine, but we should refactor that so that future (more complicated) post-handshake mechanisms such as post-handshake client auth, Connection ID updates or the ExtendedKeyUpdate can be implemented based on a reusable scheme.

Retransmission triggers reflect BoringSSL deficiencies

RFC 9147 7.2 requests us to retransmit on receiving a partial ACK. Additionally, if we receive a retransmission of an already-seen flight without any of our messages being acknowledged, RFC 9147 5.8.1 asks us to retransmit the previous flight (see numbered list, specifically point 3).

Both of these conditions are not yet implemented. Instead, retransmissions are currently entirely timer-based. This (unintentionally) reflects BoringSSL's behavior crbug.com/383016430, which is why we decided to leave this as future work.

Closes #5000.

Comment on lines +22 to +25
PLZN_REPO="reneme/plzn"

# The git revision of plzn to use
PLZN_GIT_REV=6676a26b08458f8fe5dad96316cb4624c7d3ef34
PLZN_GIT_REV=814920de917acca6bbc48073ffa8e851180d0ee0

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: Don't forget to switch that back to randombit/plnz once randombit/plzn#10 landed.

atreiber94 and others added 2 commits December 2, 2026 13:14
Not yet supported is the Connection ID (CID) and
variable MTUs for retransmissions.

Co-Authored-By: René Meusel <rene.meusel@rohde-schwarz.com>
@reneme
reneme marked this pull request as ready for review December 2, 2026 13:13
@reneme
reneme requested review from randombit and a max review from Copilot December 2, 2026 13:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical defects permit replay-window poisoning, AEAD nonce reuse, and deprotection-limit bypasses, and can cause handshake timeouts and post-close ACK failures.

9 open issues
What changed in this PR

Adds experimental DTLS 1.3 support to Botan by connecting datagram-specific I/O to the existing TLS 1.3 state machine, completing #5000.

Changes:

  • Implements DTLS records, fragmentation, acknowledgements, retransmissions, and flight timers.
  • Integrates version negotiation, DTLS 1.2 downgrade handling, and policy settings.
  • Updates conformance shims, CI configurations, tests, and documentation.
File Description
src/​tests/​unit_tls.cpp Adds DTLS downgrade and version-mismatch cases.
src/​tests/​test_tls_protocol_version.cpp Recognizes available DTLS 1.3 builds.
src/​tests/​test_dtls12.cpp Keeps DTLS 1.2 tests version-specific.
src/​scripts/​ci/​gha_linux_packages.py Supplies dependencies for the new CI variant.
src/​scripts/​ci_build.py Adds builds without DTLS 1.3.
src/​plzn_shim/​plzn_shim.cpp Enables DTLS 1.3 conformance configuration.
src/​plzn_shim/​expectations.json Excludes unsupported live-MTU testing.
src/​lib/​tls/​tls13/​tls_types_13.h Adds fragment and serialized-record wrappers.
src/​lib/​tls/​tls13/​tls_server_impl_13.cpp Initializes protocol-specific server I/O.
src/​lib/​tls/​tls13/​tls_record_layer_13.cpp Uses the shared content-type length constant.
src/​lib/​tls/​tls13/​tls_record_13.h Adds ACK records to the record variant.
src/​lib/​tls/​tls13/​tls_record_13.cpp Dispatches ACK record content.
src/​lib/​tls/​tls13/​tls_messages_13.h Includes HelloVerifyRequest downgrade messages.
src/​lib/​tls/​tls13/​tls_handshake_state_13.h Declares HelloVerifyRequest state storage.
src/​lib/​tls/​tls13/​tls_handshake_state_13.cpp Stores HelloVerifyRequest messages.
src/​lib/​tls/​tls13/​tls_handshake_layer_13.cpp Adds HelloVerifyRequest boundary validation.
src/​lib/​tls/​tls13/​tls_client_impl_13.h Declares the DTLS downgrade handler.
src/​lib/​tls/​tls13/​tls_client_impl_13.cpp Initializes I/O and handles DTLS downgrades.
src/​lib/​tls/​tls13/​tls_cipher_state.h Exposes authentication-failure counters.
src/​lib/​tls/​tls13/​tls_cipher_state.cpp Selects DTLS cipher states and initializes counters.
src/​lib/​tls/​tls13/​tls_channel_io.h Extends shared I/O interfaces for DTLS.
src/​lib/​tls/​tls13/​tls_channel_io.cpp Routes ACKs and DTLS key-update triggers.
src/​lib/​tls/​tls13/​tls_channel_impl_13.h Exposes shutdown and I/O initialization.
src/​lib/​tls/​tls13/​tls_channel_impl_13.cpp Initializes I/O and adjusts plaintext-record handling.
src/​lib/​tls/​tls_version.cpp Recognizes DTLS 1.3 when available.
src/​lib/​tls/​tls_text_policy.cpp Parses new DTLS policy settings.
src/​lib/​tls/​tls_policy.h Declares DTLS timers and resource limits.
src/​lib/​tls/​tls_policy.cpp Implements DTLS defaults and flight-timeout calculation.
src/​lib/​tls/​tls_magic.h Adds ACK and shared timing/record constants.
src/​lib/​tls/​tls_extensions.cpp Offers DTLS 1.3 in supported versions.
src/​lib/​tls/​tls_client.cpp Allows DTLS 1.3 client creation.
src/​lib/​tls/​tls_channel.h Clarifies DTLS timer accessor documentation.
src/​lib/​tls/​tls_channel_impl.h Grants DTLS I/O channel-state access.
src/​lib/​tls/​msg_client_hello.cpp Uses flavor-specific legacy version selection.
src/​lib/​tls/​dtls13/​tls_utils_dtls13.h Implements sequence reconstruction and replay windows.
src/​lib/​tls/​dtls13/​tls_types_dtls13.h Defines DTLS-specific type wrappers.
src/​lib/​tls/​dtls13/​tls_timer_dtls13.h Implements deferred retransmission and flight timers.
src/​lib/​tls/​dtls13/​tls_record_layer_dtls13.h Declares DTLS record-layer state and operations.
src/​lib/​tls/​dtls13/​tls_record_layer_dtls13.cpp Processes datagrams, retransmissions, and acknowledgements.
src/​lib/​tls/​dtls13/​tls_record_dtls13.h Defines plaintext and protected record structures.
src/​lib/​tls/​dtls13/​tls_record_dtls13.cpp Parses and serializes DTLS record headers.
src/​lib/​tls/​dtls13/​tls_handshake_layer_dtls13.h Declares fragmentation and reassembly interfaces.
src/​lib/​tls/​dtls13/​tls_handshake_layer_dtls13.cpp Fragments and reassembles handshake messages.
src/​lib/​tls/​dtls13/​tls_cipher_state_dtls13.h Declares retained protection epochs.
src/​lib/​tls/​dtls13/​tls_cipher_state_dtls13.cpp Protects records and masks sequence numbers.
src/​lib/​tls/​dtls13/​tls_channel_io_dtls13.h Declares DTLS I/O coordination and timers.
src/​lib/​tls/​dtls13/​tls_ack_record_dtls13.h Declares ACK payload operations.
src/​lib/​tls/​dtls13/​tls_ack_record_dtls13.cpp Parses, serializes, and validates acknowledgements.
src/​lib/​tls/​dtls13/​info.txt Registers the experimental DTLS 1.3 module.
src/​editors/​vscode/​scripts/​bogo.py Adds DTLS-disabled conformance-run options.
src/​configs/​repo_config.env Updates the plzn harness source and revision.
src/​bogo_shim/​config.json Enables DTLS 1.3 conformance cases.
src/​bogo_shim/​config_no_tls12.json Enables DTLS 1.3-only conformance coverage.
src/​bogo_shim/​config_no_dtls13.json Adds expectations for DTLS-disabled builds.
src/​bogo_shim/​bogo_shim.cpp Adds DTLS policies and test controls.
readme.rst Advertises experimental DTLS 1.3 support.
doc/​credits.rst Credits DTLS 1.3 contributions.
doc/​api_ref/​tls.rst Documents DTLS support, timers, and API constraints.
.github/​workflows/​ci.yml Adds the DTLS-disabled CI matrix entry.

Review effort: Max

const auto max_ack_records = (max_plaintext_length - length_field_size) / record_number_size;
BOTAN_DEBUG_ASSERT(max_ack_records > 0);

const auto acks_to_serialize = std::min<size_t>(m_record_numbers.size(), max_ack_records);
Comment on lines +257 to +258
send_data(Record_Type::ACK, m_record_layer.acknowledgements().serialize(max_plaintext_length), cipher_state().get());
}
Comment on lines +166 to +167
const auto marshalled_record =
marshall_and_protect(epoch, unified_header.serialize(), payload, type, padding_bytes, out);
Comment on lines +279 to +280
if(cs != nullptr && cs->current_read_epoch_deprotection_failures() > m_deprotection_failure_limit) {
throw TLS_Exception(Alert::BadRecordMac, "Too many deprotection failures in current read epoch");
// the lifetime of the association. This check SHOULD happen after
// deprotecting the record; otherwise, the record discard might
// itself serve as a timing channel for the record number.
auto& window = replay_window_for_epoch(record.epoch.value());
.retransmission_timer = {},
.flight_timer = {},
})) {
BOTAN_ASSERT_NONNULL(m_token->callbacks);
Comment on lines +405 to +406
#if defined(BOTAN_HAS_DTLS_13)
return true;
Comment on lines +144 to +156
auto [itr, _] =
m_current_read_message.try_emplace(header.message_sequence_number,
ReassembledMessage{
.message =
{
.epoch_number = data_from_peer.epoch,
.type = read_handshake_message_type(header.msg_type),
.header = header.serialize_tls_header_portion(),
.payload = SerializedHandshakeMessage(header.message_length),
},
.received_bytes = bitvector(header.message_length),
.complete = false,
});
Comment on lines +401 to +404
const size_t padding_length =
(pt_size_with_type_tag < max_record_size)
? std::min(policy.record_padding_bytes(pt_size_with_type_tag), max_record_size - pt_size_with_type_tag)
: 0;
@randombit

Copy link
Copy Markdown
Owner

@reneme I should have some time to start reviewing this next week.

Epoch0-Restart is not supported

I think we should just flatly not support this in DTLS 1.3. This has been a continual nightmare in 1.2. I just checked and the only other implementation I can even find that supports this is mbedtls, and it does so only in a very limited form - if enabled, and the server sees a epoch-0 CH on an active session, it performs a new cookie exchange [if the server didn't set a cookie or doesn't pass an identity string to mbedtls, it instead just drops the CH]. If the cookie exchange passes, mbedtls immediately clears the session and returns a specific error, after which the application itself must re-initiate the handshake.

I was actually surprised, thinking this mechanism is not what the 1.2 RFC specifies, but (https://www.rfc-editor.org/info/rfc6347/#section-4.2.8)

In cases where a server believes it has an existing association on a given host/port quartet and it receives an epoch=0 ClientHello, it SHOULD proceed with a new handshake but MUST NOT destroy the existing association until the client has demonstrated reachability either by completing a cookie exchange

Something of this form might be easy enough to support vs trying to keep a parallel handshake state in sync. And actually might play well with an idea you suggested a while back that we don't actually negotiate at all within the same Server object, but instead just trigger a callback notifying the application "a client [who did the cookie exchange proving reachability] just attempted a restart on this session, you might want to stop routing to this channel". This pushes some complexity to the application but saves us a lot of work, and I think the use cases for epoch0 range from niche to non-existent.

Alternative imo (and what I was about to suggest until I looked further at how mbedtls does it) would be to completely deprecate and drop epoch0 restart handling in 1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DTLS v1.3

4 participants