--- /dev/null
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+"""Fake QUIC backend checking which Destination Connection ID haproxy uses.
+
+RFC 9000 7.2: a client switches the Destination Connection ID it sends
+packets to to the Source Connection ID of the first *valid* Initial packet
+it received from the server. RFC 9000 11.1: a discarded Initial packet
+must not leave the effects of its processing behind.
+
+This script impersonates the QUIC server of a haproxy backend connection:
+
+ 1. wait for haproxy's Initial and derive the server Initial secrets from
+ the Destination Connection ID it chose;
+ 2. answer with an Initial whose Source Connection ID is SCID_A but whose
+ AEAD tag is corrupt: header protection removal succeeds, decryption
+ fails, haproxy drops the packet;
+ 3. answer with a valid Initial whose Source Connection ID is SCID_B;
+ 4. read the next packet haproxy sends and report the Destination
+ Connection ID it carries. It must be SCID_B.
+
+Without the fix, haproxy adopts SCID_A while parsing the header of the
+corrupt packet, before it is authenticated, and the flag set at that point
+prevents the valid Initial from replacing it: the ACK of that Initial, and
+every packet sent afterwards, carry an unroutable CID.
+
+Only PING frames are used, so that haproxy answers from the same I/O
+callback, without CRYPTO data being fed to the TLS stack first.
+
+The script reports "RESULT OK" on success, or "RESULT <reason>" otherwise.
+"""
+import argparse
+import os
+import socket
+import sys
+import time
+
+sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
+ "qclient"))
+
+from qcrypto import initial_keys, protect_long
+
+# Source Connection IDs of the corrupt then of the valid Initial packet.
+SCID_A = bytes.fromhex("a0a0a0a0a0a0a001")
+SCID_B = bytes.fromhex("b0b0b0b0b0b0b002")
+
+# A single PING frame.
+PING = b"\x01"
+
+
+def parse_long_header(dgram):
+ """Return the (dcid, scid) pair of a long header packet, or None."""
+ if len(dgram) < 7 or not dgram[0] & 0x80:
+ return None
+
+ dcid_len = dgram[5]
+ if dcid_len > 20 or len(dgram) < 7 + dcid_len:
+ return None
+
+ off = 6 + dcid_len
+ scid_len = dgram[off]
+ if scid_len > 20 or len(dgram) < off + 1 + scid_len:
+ return None
+
+ return dgram[6:6 + dcid_len], dgram[off + 1:off + 1 + scid_len]
+
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument("--port", type=int, required=True)
+ ap.add_argument("--timeout", type=float, default=10.0)
+ args = ap.parse_args()
+
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ s.settimeout(args.timeout)
+ s.bind(("127.0.0.1", args.port))
+ print("READY", flush=True)
+
+ # The exit status always stays 0: the VTC greps the "RESULT" line from
+ # stdout so that a failure reports the reason instead of a bare status.
+ try:
+ dgram, peer = s.recvfrom(65535)
+ except socket.timeout:
+ print("RESULT no Initial received from haproxy", flush=True)
+ return 0
+
+ hdr = parse_long_header(dgram)
+ if not hdr:
+ print("RESULT first datagram is not a long header packet", flush=True)
+ return 0
+ odcid, hap_scid = hdr
+
+ # Initial secrets of the server direction, derived from the Destination
+ # Connection ID chosen by haproxy for its own Initial packets.
+ _, srv = initial_keys(odcid)
+
+ # Initial whose Source Connection ID must be ignored: the packet is
+ # correctly header-protected but its AEAD tag is corrupted.
+ bad = bytearray(protect_long(srv, 0, hap_scid, SCID_A, 0, PING))
+ bad[-1] ^= 0xff
+ s.sendto(bytes(bad), peer)
+
+ # Valid Initial: this is the first one haproxy may adopt.
+ s.sendto(protect_long(srv, 0, hap_scid, SCID_B, 1, PING), peer)
+ print("SENT", flush=True)
+
+ deadline = time.time() + args.timeout
+ while True:
+ remaining = deadline - time.time()
+ if remaining <= 0:
+ break
+
+ s.settimeout(remaining)
+ try:
+ dgram, _ = s.recvfrom(65535)
+ except socket.timeout:
+ break
+ hdr = parse_long_header(dgram)
+ if not hdr:
+ continue
+
+ dcid, _ = hdr
+ print("DCID=" + dcid.hex(), flush=True)
+
+ if dcid == SCID_B:
+ print("RESULT OK", flush=True)
+ return 0
+ if dcid == SCID_A:
+ print("RESULT DCID=%s is the SCID of the discarded Initial" %
+ dcid.hex(), flush=True)
+ return 0
+
+ print("RESULT no packet with the expected DCID", flush=True)
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
--- /dev/null
+varnishtest "QUIC backend: adopt the SCID of the first valid Initial only"
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+#REGTEST_TYPE=bug
+#REGTEST_EXTRA=qclient
+
+feature ignore_unknown_macro
+feature cmd "python3 --version"
+feature cmd "python3 -c 'import cryptography'"
+
+# QUIC backend connections are not supported with USE_QUIC_OPENSSL_COMPAT.
+feature cmd "$HAPROXY_PROGRAM -cc 'feature(QUIC) && !feature(QUIC_OPENSSL_COMPAT) && !feature(OPENSSL_WOLFSSL)'"
+
+# RFC 9000 7.2: a client uses the Source Connection ID of the first valid
+# Initial packet from the server as the Destination Connection ID of the
+# packets it sends afterwards. Header protection removal and decryption
+# happen after the packet header has been parsed, so a packet that fails
+# either of them must not have its Source Connection ID adopted.
+# RFC 9000 11.1: a discarded Initial packet must not leave the effects of
+# its processing behind.
+#
+# The fake backend answers haproxy's Initial with a packet whose Source
+# Connection ID (A) must be ignored -- it is header-protected correctly but
+# its AEAD tag is corrupted -- then with a valid Initial whose Source
+# Connection ID is B. The ACK haproxy sends back must carry B: it used to
+# carry A, which the server cannot route, and which also makes the
+# initial_source_connection_id transport parameter check fail later on.
+
+# Reserve the UDP port used by the fake QUIC backend.
+server s_backend {} -start
+server s_backend -break
+
+process p1 "python3 ${testdir}/scid_initial_backend.py --port ${s_backend_port}" -start
+process p1 -expect-text 0 0 "READY"
+
+haproxy h1 -conf {
+ global
+ expose-experimental-directives
+
+ defaults
+ mode http
+ timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
+
+ frontend fe
+ bind "fd@${fe}"
+ default_backend be
+
+ backend be
+ retries 0
+ server qsrv quic4@127.0.0.1:${s_backend_port} ssl verify none alpn h3
+} -start
+
+# The handshake cannot complete: the fake backend never sends a ServerHello.
+# The request is expected to fail, only the packets exchanged before that
+# matter.
+client c1 -connect ${h1_fe_sock} {
+ txreq
+ rxresp
+ expect resp.status == 503
+} -run
+
+process p1 -expect-text 0 0 "RESULT"
+
+# Surface the driver's own message when it did not succeed, instead of
+# leaving only a bare exit status behind.
+shell {
+ if ! grep -q "RESULT OK" "${p1_out}"; then
+ echo "--- p1 stdout ---"; cat "${p1_out}"
+ echo "--- p1 stderr ---"; cat "${p1_err}"
+ exit 1
+ fi
+}
+
+process p1 -wait