Commit 32fee030e0 for openssl.org

commit 32fee030e0c869bd854dcbcf5aabd142faaad668
Author: Liu Dongmiao <473522+liudongmiao@users.noreply.github.com>
Date:   Wed Nov 18 12:50:39 2026 +0800

    quic: resend unacknowledged Initial CRYPTO data in PTO probes

    A PTO probe carried only PING when no new CRYPTO data was pending, so
    the ClientHello was not retransmitted until its packet was declared
    lost, which a long RTT delays past the server's reply.

    Request two probes on an Initial PTO, as RFC 9002 s. 6.2.4 allows,
    counting any still pending from an earlier PTO, and fill each with the
    next unacknowledged Initial CRYPTO data that fits. Initial CRYPTO data
    still queued, such as the tail of a ClientHello larger than the
    congestion window, goes out first. A CRYPTO frame is ACK-eliciting, so
    it may use the byte reserved for PING, and a probe can carry as much as
    the packet it resends. A ClientHello of two packets can then be resent
    in full, and anything beyond the two probes stays in flight. When one
    probe carries all that is left, or nothing is left, a single probe is
    sent.

    Fixes #33758

    Assisted-by: Claude:claude-opus-5-5
    Reviewed-by: Saša NedvÄ›dický <sashan@openssl.org>
    Reviewed-by: Neil Horman <nhorman@openssl.org>
    Merge-date: Fri Dec  4 13:55:46 2026
    Merged-from: https://github.com/openssl/openssl/pull/33782

diff --git a/include/internal/quic_stream.h b/include/internal/quic_stream.h
index 1c0fd3f86e..94b64fb070 100644
--- a/include/internal/quic_stream.h
+++ b/include/internal/quic_stream.h
@@ -199,6 +199,21 @@ int ossl_quic_sstream_mark_lost(QUIC_SSTREAM *qss,
  */
 int ossl_quic_sstream_mark_lost_fin(QUIC_SSTREAM *qss);

+/**
+ * @brief Gets the first unacknowledged range of stream data at or after an
+ * offset.
+ *
+ * (TX packetiser use.)
+ *
+ * @param qss   the send stream
+ * @param from  the offset to search from
+ * @param start receives the first offset of the range
+ * @param end   receives the last offset of the range, inclusive
+ * @returns 1 if such a range exists, or 0 otherwise
+ */
+int ossl_quic_sstream_get_first_unacked(QUIC_SSTREAM *qss, uint64_t from,
+    uint64_t *start, uint64_t *end);
+
 /*
  * (RX/ACKM use.) Marks a logical range of the send stream as having been
  * acknowledged, meaning that the storage for the data in that range of the
diff --git a/ssl/quic/quic_ackm.c b/ssl/quic/quic_ackm.c
index 8e2577a37b..d9fc46628f 100644
--- a/ssl/quic/quic_ackm.c
+++ b/ssl/quic/quic_ackm.c
@@ -1361,11 +1361,14 @@ static void ackm_queue_probe_anti_deadlock_initial(OSSL_ACKM *ackm)
 static void ackm_queue_probe(OSSL_ACKM *ackm, int pkt_space)
 {
     /*
-     * TODO(QUIC FUTURE): We are allowed to send either one or two probe
-     * packets here.
-     * Determine a strategy for when we should send two probe packets.
+     * We may send one or two probe packets (RFC 9002 s. 6.2.4). Request two
+     * for Initial, so that Initial CRYPTO data of two packets can be resent.
+     * Probes still pending from an earlier PTO count towards the two.
      */
-    ++ackm->pending_probe.pto[pkt_space];
+    if (pkt_space == QUIC_PN_SPACE_INITIAL)
+        ackm->pending_probe.pto[pkt_space] = 2;
+    else
+        ++ackm->pending_probe.pto[pkt_space];
 }

 int ossl_ackm_on_timeout(OSSL_ACKM *ackm)
diff --git a/ssl/quic/quic_sstream.c b/ssl/quic/quic_sstream.c
index ce20800b6f..de24922276 100644
--- a/ssl/quic/quic_sstream.c
+++ b/ssl/quic/quic_sstream.c
@@ -259,6 +259,31 @@ int ossl_quic_sstream_mark_lost_fin(QUIC_SSTREAM *qss)
     return 1;
 }

+int ossl_quic_sstream_get_first_unacked(QUIC_SSTREAM *qss, uint64_t from,
+    uint64_t *start, uint64_t *end)
+{
+    UINT_SET_ITEM *acked;
+
+    *start = from;
+    for (acked = ossl_list_uint_set_head(&qss->acked_set); acked != NULL;
+        acked = ossl_list_uint_set_next(acked)) {
+        if (acked->range.end < *start)
+            continue;
+
+        if (acked->range.start > *start)
+            break;
+
+        *start = acked->range.end + 1;
+    }
+
+    if (*start >= qss->ring_buf.head_offset)
+        return 0;
+
+    *end = acked != NULL ? acked->range.start - 1
+                         : qss->ring_buf.head_offset - 1;
+    return 1;
+}
+
 int ossl_quic_sstream_mark_acked(QUIC_SSTREAM *qss,
     uint64_t start,
     uint64_t end)
diff --git a/ssl/quic/quic_txp.c b/ssl/quic/quic_txp.c
index a2491a7ee3..c5758f252f 100644
--- a/ssl/quic/quic_txp.c
+++ b/ssl/quic/quic_txp.c
@@ -69,6 +69,9 @@ struct ossl_quic_tx_packetiser_st {

     size_t unvalidated_credit; /* Limit of data we can send until validated */

+    /* Initial CRYPTO offset from which the next PTO probe resends */
+    uint64_t crypto_probe_initial_offset;
+
     /* Internal state - frame (re)generation flags. */
     unsigned int want_handshake_done : 1;
     unsigned int want_max_data : 1;
@@ -86,6 +89,9 @@ struct ossl_quic_tx_packetiser_st {
      */
     unsigned int want_conn_close : 1;

+    /* Connection close terminal state, not unset unlike want_conn_close. */
+    unsigned int conn_close_scheduled : 1;
+
     /* Has the handshake been completed? */
     unsigned int handshake_complete : 1;

@@ -277,6 +283,15 @@ static size_t tx_helper_get_space_left(struct tx_helper *h)
         - (h->reserve_allowed ? 0 : h->reserve) - h->bytes_appended;
 }

+/*
+ * How many more bytes of space do we have left for an ACK-eliciting frame? It
+ * may use the bytes reserved for PING.
+ */
+static size_t tx_helper_get_space_left_ack_eliciting(struct tx_helper *h)
+{
+    return h->max_ppl - h->bytes_appended;
+}
+
 /*
  * Begin a control frame serialization transaction. This allows the
  * serialization of the control frame to be backed out if it turns out it won't
@@ -2141,6 +2156,124 @@ static int determine_stream_len(struct tx_helper *h,
     return determine_len(space_left, orig_len, base_hdr_len, hlen, len);
 }

+/*
+ * Resend unacknowledged Initial CRYPTO data, such as the ClientHello, in a PTO
+ * probe rather than a bare PING (RFC 9002 s. 6.2.4), until the Handshake keys
+ * are available. Data still queued goes out first. Otherwise each probe takes
+ * what fits after the data of the probe before it, and the rest stays in
+ * flight. On allocation failure the probe carries what was queued, or a PING.
+ */
+static void txp_requeue_crypto_for_probe(OSSL_QUIC_TX_PACKETISER *txp,
+    struct txp_pkt *pkt, uint32_t pn_space)
+{
+    QUIC_SSTREAM *crypto;
+    OSSL_ACKM_PROBE_INFO *probe_info;
+    OSSL_QUIC_FRAME_CRYPTO chdr = { 0 };
+    uint64_t probe_offset, start, end, hdr_bytes;
+    size_t space_left;
+
+    /* TODO(QUIC FUTURE): resend Handshake CRYPTO data too */
+    if (pn_space != QUIC_PN_SPACE_INITIAL)
+        return;
+
+    if (ossl_qtx_is_enc_level_provisioned(txp->args.qtx,
+            QUIC_ENC_LEVEL_HANDSHAKE))
+        return;
+
+    probe_info = ossl_ackm_get0_probe_request(txp->args.ackm);
+    if (probe_info->pto[pn_space] == 0)
+        return;
+
+    if (txp->conn_close_scheduled) {
+        probe_info->pto[pn_space] = 0;
+        return;
+    }
+
+    crypto = txp->args.crypto[pn_space];
+    if (sstream_is_pending(crypto))
+        return;
+
+    /*
+     * Data requeued here stays queued even if the probe is not sent, so wait
+     * until the anti-amplification limit lets a full probe out.
+     */
+    if (!ossl_quic_tx_packetiser_check_unvalidated_credit(txp,
+            pkt->h.max_ppl + pkt->geom.pkt_overhead))
+        return;
+
+    probe_offset = txp->crypto_probe_initial_offset;
+    space_left = tx_helper_get_space_left_ack_eliciting(&pkt->h);
+    while (space_left >= MIN_FRAME_SIZE_CRYPTO) {
+        if (!ossl_quic_sstream_get_first_unacked(crypto, probe_offset,
+                &start, &end))
+            break;
+
+        chdr.offset = start;
+        chdr.len = end - start + 1;
+        if (!determine_crypto_len(&pkt->h, &chdr, space_left, &hdr_bytes,
+                &chdr.len)
+            || !ossl_quic_sstream_mark_lost(crypto, start,
+                start + chdr.len - 1))
+            break;
+
+        probe_offset = start + chdr.len;
+        space_left -= (size_t)(hdr_bytes + chdr.len);
+    }
+}
+
+/*
+ * After a PTO probe is sent, move the start of the next probe past the CRYPTO
+ * data this one carried, unless unacknowledged data between the start and that
+ * data is still to be resent. With nothing more to resend, or once the
+ * Handshake keys are available, a single probe is enough. When no probe is
+ * left, the next PTO starts from the beginning again.
+ */
+static void txp_crypto_probe_sent(OSSL_QUIC_TX_PACKETISER *txp,
+    QUIC_TXPIM_PKT *tpkt, OSSL_ACKM_PROBE_INFO *probe_info, uint32_t pn_space)
+{
+    QUIC_SSTREAM *crypto;
+    const QUIC_TXPIM_CHUNK *chunks;
+    size_t i, num_chunks;
+    uint64_t probe_offset, start, end;
+
+    /* TODO(QUIC FUTURE): resend Handshake CRYPTO data too */
+    if (pn_space != QUIC_PN_SPACE_INITIAL)
+        return;
+
+    if (probe_info->pto[pn_space] == 0
+        || ossl_qtx_is_enc_level_provisioned(txp->args.qtx,
+            QUIC_ENC_LEVEL_HANDSHAKE))
+        goto reset;
+
+    crypto = txp->args.crypto[pn_space];
+    chunks = ossl_quic_txpim_pkt_get_chunks(tpkt);
+    num_chunks = ossl_quic_txpim_pkt_get_num_chunks(tpkt);
+    probe_offset = txp->crypto_probe_initial_offset;
+    for (i = 0; i < num_chunks; ++i) {
+        if (chunks[i].stream_id != UINT64_MAX || chunks[i].end < probe_offset)
+            continue;
+
+        if (!ossl_quic_sstream_get_first_unacked(crypto, probe_offset,
+                &start, &end))
+            goto reset;
+
+        if (start < chunks[i].start)
+            goto update;
+
+        probe_offset = chunks[i].end + 1;
+    }
+
+    if (ossl_quic_sstream_get_first_unacked(crypto, probe_offset, &start, &end))
+        goto update;
+
+reset:
+    probe_info->pto[pn_space] = 0;
+    probe_offset = 0;
+
+update:
+    txp->crypto_probe_initial_offset = probe_offset;
+}
+
 static int txp_generate_crypto_frames(OSSL_QUIC_TX_PACKETISER *txp,
     struct txp_pkt *pkt,
     int *have_ack_eliciting)
@@ -2159,7 +2292,7 @@ static int txp_generate_crypto_frames(OSSL_QUIC_TX_PACKETISER *txp,
     size_t i, j, space_left;

     for (i = 0;; ++i) {
-        space_left = tx_helper_get_space_left(h);
+        space_left = tx_helper_get_space_left_ack_eliciting(h);

         if (space_left < MIN_FRAME_SIZE_CRYPTO)
             return 1; /* no point trying */
@@ -2889,9 +3022,11 @@ static int txp_generate_for_el(OSSL_QUIC_TX_PACKETISER *txp,
             done_pre_token = 1;

     /* CRYPTO Frames */
-    if (a.allow_crypto)
+    if (a.allow_crypto) {
+        txp_requeue_crypto_for_probe(txp, pkt, pn_space);
         if (!txp_generate_crypto_frames(txp, pkt, &have_ack_eliciting))
             goto fatal_err;
+    }

     /* Stream-specific frames */
     if (a.allow_stream_rel && txp->handshake_complete)
@@ -3175,8 +3310,10 @@ static int txp_pkt_commit(OSSL_QUIC_TX_PACKETISER *txp,
             --probe_info->anti_deadlock_handshake;

         if (a.allow_force_ack_eliciting /* (i.e., not for 0-RTT) */
-            && probe_info->pto[pn_space] > 0)
+            && probe_info->pto[pn_space] > 0) {
             --probe_info->pto[pn_space];
+            txp_crypto_probe_sent(txp, tpkt, probe_info, pn_space);
+        }
     }

     ossl_quic_fifd_pkt_discard_unreliable(&txp->fifd, tpkt);
@@ -3230,6 +3367,7 @@ int ossl_quic_tx_packetiser_schedule_conn_close(OSSL_QUIC_TX_PACKETISER *txp,
     txp->conn_close_frame.reason = reason;
     txp->conn_close_frame.reason_len = reason_len;
     txp->want_conn_close = 1;
+    txp->conn_close_scheduled = 1;
     return 1;
 }

diff --git a/test/quic_ackm_test.c b/test/quic_ackm_test.c
index 13c35dcb29..c2efd83e49 100644
--- a/test/quic_ackm_test.c
+++ b/test/quic_ackm_test.c
@@ -506,7 +506,7 @@ static int test_tx_ack_case_actual(int tidx, int space, int mode)
                     goto err;
             } else {
                 if (!TEST_int_eq(test_probe_counts(&probe, 0, 0,
-                                     space == QUIC_PN_SPACE_INITIAL,
+                                     space == QUIC_PN_SPACE_INITIAL ? 2 : 0,
                                      space == QUIC_PN_SPACE_HANDSHAKE,
                                      space == QUIC_PN_SPACE_APP),
                         1))