Commit 0226bf8a78 for openssl.org
commit 0226bf8a782560ad07adfdf32a60c6a9836c2a25
Author: Ryan Hooper <ryanh@openssl.foundation>
Date: Mon Nov 16 14:17:09 2026 -0500
DTLS: Don't repeat the fragment header room on a write retry
dtls1_do_write() makes room for each fragment's handshake header by
moving init_off back and init_num forward by DTLS1_HM_HEADER_LENGTH. It
did this at the top of every pass for any fragment after the first, on
the assumption that this was the first attempt at the fragment. If the
fragment was then not written (the write BIO returned a retry, for
instance), the adjustment was left in place, and the next call applied
it again.
The retry therefore started 12 bytes further back for each rejection.
Over a few retries the next fragment went out at the wrong fragment
offset, so the peer reassembled a handshake message with data overwritten
by an earlier header and missing from the end. After enough retries
init_off reached DTLS1_HM_HEADER_LENGTH and dtls1_do_write() failed with
SSL_ERROR_SYSCALL and an empty error queue.
Take the room back on every exit where the fragment was not written, so
the next call makes it once. This includes the MTU exceeded fall-through,
which would otherwise make the room a second time on its next pass.
Add test_dtls_fragment_write_retry, which rejects the second fragment of
a fragmented ClientHello several times before letting it through. It
fails if any fragment does not start where the previous one ended. It
runs for DTLS 1.2 and 1.3, with and without a msg callback. Handshake
writes go through the buffering BIO, so the test reaches the flush
failure exit only. The other exits are covered by the change but not by
the test.
Fixes: #33712
Assisted-by: Claude:claude-sonnet-5-5
Reviewed-by: Tomas Mraz <tomas@openssl.foundation>
Reviewed-by: Saša NedvÄ›dický <sashan@openssl.org>
Reviewed-by: Mounir Idrassi <mounir.idrassi@idrix.fr>
Merge-date: Mon Nov 23 17:12:49 2026
Merged-from: https://github.com/openssl/openssl/pull/33777
diff --git a/ssl/statem/statem_dtls.c b/ssl/statem/statem_dtls.c
index cfb2342421..c0917f2d92 100644
--- a/ssl/statem/statem_dtls.c
+++ b/ssl/statem/statem_dtls.c
@@ -206,6 +206,19 @@ static int dtls1_write_hm_header(unsigned char *msgheaderstart,
return 1;
}
+/*
+ * Take back the room dtls1_do_write() made for a fragment's header, if any, so
+ * that the message is described again as it was when the fragment was started.
+ * Used whenever the fragment is not accepted, so that the next call makes the
+ * room once rather than adding to it.
+ */
+static void dtls1_undo_fragment_header_room(SSL_CONNECTION *s, size_t *room)
+{
+ s->init_off += *room;
+ s->init_num -= *room;
+ *room = 0;
+}
+
/*
* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE,
* SSL3_RT_CHANGE_CIPHER_SPEC or SSL3_RT_ACK)
@@ -231,6 +244,8 @@ int dtls1_do_write(SSL_CONNECTION *s, uint8_t recordtype)
SSL *ssl = SSL_CONNECTION_GET_SSL(s);
SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
uint8_t saved_payload[DTLS1_HM_HEADER_LENGTH];
+ /* Room made in front of the current fragment for its header, if any */
+ size_t header_room = 0;
/* msg_len, msg_seq, msg_type are only used for recordtype == SSL3_RT_HANDSHAKE */
const size_t msg_len = s->d1->w_msg.msg_body_len;
const unsigned short msg_seq = s->d1->w_msg.msg_seq;
@@ -281,6 +296,7 @@ int dtls1_do_write(SSL_CONNECTION *s, uint8_t recordtype)
*/
s->init_off -= DTLS1_HM_HEADER_LENGTH;
s->init_num += DTLS1_HM_HEADER_LENGTH;
+ header_room = DTLS1_HM_HEADER_LENGTH;
}
used_len = BIO_wpending(s->wbio) + overhead;
@@ -296,13 +312,14 @@ int dtls1_do_write(SSL_CONNECTION *s, uint8_t recordtype)
ret = BIO_flush(s->wbio);
if (ret <= 0) {
s->rwstate = SSL_WRITING;
- return ret;
+ goto err;
}
if (s->d1->mtu >= overhead + min_len) {
curr_mtu = s->d1->mtu - overhead;
} else {
/* Shouldn't happen */
- return -1;
+ ret = -1;
+ goto err;
}
}
@@ -348,12 +365,14 @@ int dtls1_do_write(SSL_CONNECTION *s, uint8_t recordtype)
if (len < DTLS1_HM_HEADER_LENGTH
|| !dtls1_write_hm_header(msgstart, msg_type, msg_len,
- msg_seq, fragoff, fraglen))
+ msg_seq, fragoff, fraglen)) {
/*
* len is so small that we really can't do anything sensible
* so fail
*/
- return -1;
+ ret = -1;
+ goto err;
+ }
/*
* Recorded so do_dtls1_write() (rec_layer_d1.c) can tag this
@@ -376,19 +395,24 @@ int dtls1_do_write(SSL_CONNECTION *s, uint8_t recordtype)
* retransmit anything. continue as if everything is fine and
* wait for an alert to handle the retransmit
*/
+ ret = -1;
if (retry && BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0
&& !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
size_t old_mtu = s->d1->mtu;
if (!dtls1_query_mtu(s))
- return -1;
+ goto err;
/* If MTU didn't change, retry is meaningless */
if (s->d1->mtu == old_mtu)
- return -1;
- /* Have one more go */
+ goto err;
+ /*
+ * Have one more go. The fragment was not written, so take back
+ * the room for its header. Going round again makes it once more.
+ */
+ dtls1_undo_fragment_header_room(s, &header_room);
retry = 0;
} else {
- return -1;
+ goto err;
}
} else {
@@ -396,8 +420,13 @@ int dtls1_do_write(SSL_CONNECTION *s, uint8_t recordtype)
* bad if this assert fails, only part of the handshake message
* got sent. but why would this happen?
*/
- if (!ossl_assert(len == written))
- return -1;
+ if (!ossl_assert(len == written)) {
+ ret = -1;
+ goto err;
+ }
+
+ /* The fragment, header included, has been accepted */
+ header_room = 0;
/*
* We should not exceed the MTU size. If compression is in use
@@ -472,6 +501,11 @@ int dtls1_do_write(SSL_CONNECTION *s, uint8_t recordtype)
}
}
return 0;
+
+err:
+ /* The fragment was not written, so don't leave the room for its header made */
+ dtls1_undo_fragment_header_room(s, &header_room);
+ return ret;
}
int dtls_get_message(SSL_CONNECTION *s, int *mt)
diff --git a/test/dtlstest.c b/test/dtlstest.c
index b64f0a541d..d853d5ee2c 100644
--- a/test/dtlstest.c
+++ b/test/dtlstest.c
@@ -1768,13 +1768,11 @@ end:
#endif /* OPENSSL_NO_DTLS1_3 */
/*
- * frag_bio and the four retransmit tests below need only one of DTLS1.2 or
+ * frag_bio and the tests below that use it need only one of DTLS1.2 or
* DTLS1.3 to be available - the frag_bio helpers are shared by both, so
* they can't be scoped to either protocol's guard alone.
*/
-#if !defined(OPENSSL_NO_DTLS1_2) \
- || (!defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_ECX) \
- && !defined(OPENSSL_NO_ML_KEM) && !defined(OPENSSL_NO_DTLS1_3))
+#if !defined(OPENSSL_NO_DTLS1_2) || !defined(OPENSSL_NO_DTLS1_3)
/*
* Number of times the retransmit timer is fired while a write is parked, in
@@ -1786,8 +1784,58 @@ typedef struct {
BIO *bio;
int allowed; /* number of fragment writes to let through before suspending */
int write_calls; /* number of times frag_write() has been invoked at all */
+ /*
+ * Offset at which the next plaintext handshake fragment must start for the
+ * message to stay contiguous (or 0 before any). A fragment after a
+ * rejected write that starts anywhere else was built from a shifted
+ * position in the message, so it overlaps or skips data already sent.
+ */
+ long next_off;
+ int offset_mismatch; /* a fragment did not start where the last ended */
+ int rejections; /* number of writes this BIO rejected */
+ /*
+ * The first rejected write, kept to compare with the first write accepted
+ * afterwards. The record sequence number may legitimately change on the
+ * retry, so only the data after the record header is compared.
+ */
+ unsigned char rejected[8192];
+ size_t rejected_len;
+ int retry_compared; /* the write after the rejection was compared */
+ int retry_mismatch; /* ... and it differed from the rejected one */
} frag_bio;
+/*
+ * Parse the plaintext (epoch 0) handshake record in the buffer and return its
+ * fragment offset and length. Returns 0 if it is not one. DTLS 1.3 epoch 0
+ * records still use the DTLS record header.
+ */
+static int frag_handshake_info(const char *buf, size_t len, long *off,
+ long *flen)
+{
+ PACKET pkt;
+ unsigned int rec_type, rec_version, epoch, rec_len, msg_type, msg_seq;
+ unsigned long msg_len, frag_off, frag_len;
+
+ if (!PACKET_buf_init(&pkt, (const unsigned char *)buf, len)
+ || !PACKET_get_1(&pkt, &rec_type)
+ || !PACKET_get_net_2(&pkt, &rec_version)
+ || !PACKET_get_net_2(&pkt, &epoch)
+ || !PACKET_forward(&pkt, DTLS1_RT_HEADER_SEQ_LEN)
+ || !PACKET_get_net_2(&pkt, &rec_len)
+ || rec_type != SSL3_RT_HANDSHAKE
+ || epoch != 0
+ || !PACKET_get_1(&pkt, &msg_type)
+ || !PACKET_get_net_3(&pkt, &msg_len)
+ || !PACKET_get_net_2(&pkt, &msg_seq)
+ || !PACKET_get_net_3(&pkt, &frag_off)
+ || !PACKET_get_net_3(&pkt, &frag_len))
+ return 0;
+
+ *off = (long)frag_off;
+ *flen = (long)frag_len;
+ return 1;
+}
+
/*
* Each call to this function corresponds to exactly one DTLS fragment being
* handed to the BIO by dtls1_do_write(). Once |allowed| fragments have been
@@ -1803,12 +1851,37 @@ static int frag_write(BIO *bio, const char *buf, size_t len, size_t *written)
f->write_calls++;
if (f->allowed <= 0) {
+ if (f->rejections++ == 0 && len <= sizeof(f->rejected)) {
+ memcpy(f->rejected, buf, len);
+ f->rejected_len = len;
+ }
BIO_set_retry_write(bio);
*written = 0;
return 0;
}
f->allowed--;
+ if (f->rejected_len > 0 && !f->retry_compared) {
+ f->retry_compared = 1;
+ if (len != f->rejected_len
+ || len < DTLS1_RT_HEADER_LENGTH
+ || memcmp(buf + DTLS1_RT_HEADER_LENGTH,
+ f->rejected + DTLS1_RT_HEADER_LENGTH,
+ len - DTLS1_RT_HEADER_LENGTH)
+ != 0)
+ f->retry_mismatch = 1;
+ }
+
+ {
+ long off, flen;
+
+ if (frag_handshake_info(buf, len, &off, &flen)) {
+ if (off != 0 && off != f->next_off)
+ f->offset_mismatch = 1;
+ f->next_off = off + flen;
+ }
+ }
+
if (!BIO_write_ex(f->bio, buf, len, written)) {
fprintf(stderr, "Failed to send data via BIO_write_ex\n");
return 0;
@@ -2433,7 +2506,152 @@ end:
return testresult;
}
#endif /* !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_ECX) && ... */
-#endif /* !defined(OPENSSL_NO_DTLS1_2) || (DTLS1.3-capable) */
+
+static void frag_retry_msg_cb(int write_p, int version, int content_type,
+ const void *buf, size_t len, SSL *ssl, void *arg)
+{
+}
+
+/*
+ * A record write that is rejected part way through a fragmented handshake
+ * message must be retried as the same fragment. dtls1_do_write() makes room
+ * for each fragment's header by moving its position in the message back by
+ * DTLS1_HM_HEADER_LENGTH; if that is applied again on the retry, the fragment
+ * goes out at the wrong offset and carries the previous header as data.
+ *
+ * The ClientHello is padded with ALPN so it takes several fragments at an MTU
+ * of 256. The first fragment is let through and the second is rejected, then
+ * retried. The fragment-checking BIO flags any fragment that does not start
+ * where the previous one ended, which is what a retry that moved the position
+ * a second time produces for the fragment after it.
+ *
+ * Handshake writes go through a buffering BIO, so the rejection is seen when
+ * dtls1_do_write() flushes it, not when the record is written.
+ *
+ * idx bit 0: DTLS 1.3 instead of 1.2
+ * idx bit 1: set a msg callback (it restores the overwritten payload but not
+ * the offset)
+ * idx bit 2: use an MTU and ClientHello larger than the 4096 byte buffering
+ * BIO. Fragments that size bypass the buffer, so the rejection is
+ * returned by the record write itself, not by the flush, and the
+ * retry has to rebuild the fragment.
+ */
+static int test_dtls_fragment_write_retry(int idx)
+{
+ SSL_CTX *sctx = NULL, *cctx = NULL;
+ SSL *serverssl = NULL, *clientssl = NULL;
+ int testresult = 0;
+ int dtls13 = idx & 1, msg_cb = (idx >> 1) & 1, large = (idx >> 2) & 1;
+ int ret, i;
+ static unsigned char alpn[14000];
+ size_t j, used = 0;
+ const size_t nprotos = large ? 56 : 3;
+ BIO *c_to_s_bio = NULL, *frag_wbio;
+ frag_bio *fb;
+ int version = dtls13 ? DTLS1_3_VERSION : DTLS1_2_VERSION;
+
+#ifdef OPENSSL_NO_DTLS1_2
+ if (!dtls13)
+ return TEST_skip("DTLS 1.2 is disabled");
+#endif
+#ifdef OPENSSL_NO_DTLS1_3
+ if (dtls13)
+ return TEST_skip("DTLS 1.3 is disabled");
+#endif
+
+ if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
+ DTLS_client_method(), version, version,
+ &sctx, &cctx, cert, privkey)))
+ return 0;
+
+ SSL_CTX_set_verify(cctx, SSL_VERIFY_NONE, NULL);
+
+ /* Pad the ClientHello out via ALPN so it needs multiple fragments. */
+ for (j = 0; j < nprotos; j++) {
+ char name[250];
+ int n = snprintf(name, sizeof(name), "proto-%04zu-%s", j,
+ "padpadpadpadpadpadpadpadpadpadpadpadpadpadpad"
+ "padpadpadpadpadpadpadpadpadpadpadpadpadpadpad"
+ "padpadpadpadpadpadpadpadpadpadpadpadpadpadpad"
+ "padpadpadpadpadpadpadpadpadpadpadpadpadpadpad"
+ "padpadpadpadpadpadpadpadpadpadpadpadpadpad");
+
+ if (!TEST_int_ge(n, 0) || !TEST_size_t_lt((size_t)n, sizeof(name))
+ || !TEST_size_t_le(used + 1 + (size_t)n, sizeof(alpn)))
+ goto end;
+
+ alpn[used++] = (unsigned char)n;
+ memcpy(alpn + used, name, (size_t)n);
+ used += (size_t)n;
+ }
+ if (!TEST_false(SSL_CTX_set_alpn_protos(cctx, alpn, (unsigned int)used)))
+ goto end;
+
+ if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
+ NULL, NULL)))
+ goto end;
+
+ if (msg_cb)
+ SSL_set_msg_callback(clientssl, frag_retry_msg_cb);
+
+ SSL_set_options(clientssl, SSL_OP_NO_QUERY_MTU);
+ if (!TEST_true(SSL_set_mtu(clientssl, large ? 6000 : 256)))
+ goto end;
+
+ /* See test_dtls_client_retransmit() for why the extra reference. */
+ c_to_s_bio = SSL_get_wbio(clientssl);
+ if (!TEST_ptr(c_to_s_bio) || !TEST_true(BIO_up_ref(c_to_s_bio)))
+ goto end;
+
+ /* Let only the first ClientHello fragment through. */
+ frag_wbio = frag_new(c_to_s_bio, 1);
+ if (!TEST_ptr(frag_wbio)) {
+ BIO_free(c_to_s_bio);
+ goto end;
+ }
+ fb = BIO_get_data(frag_wbio);
+ SSL_set0_wbio(clientssl, frag_wbio);
+
+ ret = SSL_connect(clientssl);
+ if (!TEST_int_le(ret, 0)
+ || !TEST_int_eq(SSL_get_error(clientssl, ret), SSL_ERROR_WANT_WRITE))
+ goto end;
+
+ /* Each retry is rejected again, with the BIO still blocked. */
+ for (i = 0; i < 3; i++) {
+ ret = SSL_connect(clientssl);
+ if (!TEST_int_le(ret, 0)
+ || !TEST_int_eq(SSL_get_error(clientssl, ret), SSL_ERROR_WANT_WRITE))
+ goto end;
+ }
+
+ /* Unblock: the next write is the retry of the rejected fragment. */
+ fb->allowed = 100;
+
+ if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
+ goto end;
+
+ /* Every fragment started where the previous one ended. */
+ if (!TEST_int_gt(fb->rejections, 0)
+ || !TEST_false(fb->offset_mismatch))
+ goto end;
+
+ /* The retry carried the same handshake header and payload as the reject. */
+ if (!TEST_size_t_gt(fb->rejected_len, DTLS1_RT_HEADER_LENGTH)
+ || !TEST_true(fb->retry_compared)
+ || !TEST_false(fb->retry_mismatch))
+ goto end;
+
+ testresult = 1;
+end:
+ SSL_free(serverssl);
+ SSL_free(clientssl);
+ SSL_CTX_free(sctx);
+ SSL_CTX_free(cctx);
+
+ return testresult;
+}
+#endif /* !defined(OPENSSL_NO_DTLS1_2) || !defined(OPENSSL_NO_DTLS1_3) */
/* Confirm that we can create a connections using DTLSv1_listen() */
#ifndef OPENSSL_NO_DTLS1_2
@@ -2915,6 +3133,9 @@ int setup_tests(void)
ADD_TEST(test_dtls_server_retransmit_dtls13);
#endif
ADD_ALL_TESTS(test_dtls_connect_state_after_client_hello, 2);
+#if !defined(OPENSSL_NO_DTLS1_2) || !defined(OPENSSL_NO_DTLS1_3)
+ ADD_ALL_TESTS(test_dtls_fragment_write_retry, 8);
+#endif
return 1;
}