Commita17d374dRecorded18 Oct 2026Repositorysigil-quic

Validate Retry tokens before private endpoint admission

Changed
 README.md                    |  26 ++++++++++-
 scripts/native-tls-tests.mjs |   9 +++-
 src/sigil/quic/endpoint.sgl  | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
 test/test-endpoint-retry.sgl | 387 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 547 insertions(+), 25 deletions(-)
Diff
README.mdmodified
@@ -212,7 +212,8 @@ are available in `(sigil quic packet retry)` and re-exported by the packet faça
212
the validator drops malformed peer packets. Both bound CIDs and datagram size.
213
The standardized public integrity tag does not authenticate a server or validate
214
an address. Private token admission, version negotiation and connection-ID policy
−215
remain separate; no server listener issues Retry yet.
+215
remain separate from this packet helper; the private loopback endpoint has an
+216
opt-in Retry admission path described below.
217
218
`(sigil quic retry token)` provides private server tokens through
219
`make-retry-token-manager`, `make-retry-token`, `validate-retry-token` and
@@ -353,7 +354,7 @@ Retry tokens over 1024 bytes are dropped; the retained first ClientHello is capp
354
at 64 KiB. CRYPTO fragmentation accounts for the token within 1200-byte Initials.
355
Retry does not restart the handshake deadline. The first accepted server Initial
356
binds the original, server Initial and Retry source IDs to the TLS transport checks.
−356
Server Retry/token admission, VN, key updates, migration and connection-ID rotation
+357
VN, key updates, migration and connection-ID rotation
358
are not implemented. Stream ownership/flow control and
359
path/CID policy still belong to the layers above this primitive. No public
360
listener is provided, and independent security review is still required.
@@ -462,6 +463,27 @@ release the slot and discard their output while preserving the original
463
factory and configured port restriction; a confirmed connection retains the
464
endpoint's single-connection lifetime.
465
+466
For private Retry testing, call `quic-endpoint-accept-retry! endpoint factory`
+467
instead. Its factory receives three copied IDs: original destination, client
+468
source and Retry source. Construct the server with the third ID as
+469
`retry-source-id:`, attach its stream handler, and return the owned connection.
+470
The endpoint checks the returned connection's CID metadata and aborts a
+471
rejected return. An optional `peer-port:` restricts admission;
+472
`token-lifetime:` is 1–60,000 milliseconds, default 10,000.
+473
+474
The endpoint issues a challenge only after authenticating a tokenless Initial
+475
of at least 1,200 bytes with nonempty CRYPTO at offset zero. It retains no
+476
challenge queue or peer pin. Before invoking the factory or pinning a source,
+477
it validates the private token against the actual loopback address and UDP
+478
port, current source/destination CIDs and monotonic time, then authenticates
+479
the retried Initial. Invalid token-bearing Initials drop without another Retry.
+480
Each receive pass permits at most 32 challenge attempts; each challenge fits
+481
1,200 bytes and the three-times-received budget. A blocked challenge send drops.
+482
The endpoint owns a fresh token key, keeps it across failed tentative
+483
connections, and retires it on confirmation or endpoint close. The connection
+484
keeps its conservative amplification limit until authenticated Handshake.
+485
This path remains restricted to loopback pending admission tests and review.
+486
487
Call `quic-endpoint-step! endpoint wait-ms: milliseconds` repeatedly alongside
488
application reads/writes. Each call waits at most 50 milliseconds and drives
489
receive, timers and protected output. Temporary send blockage retains the
scripts/native-tls-tests.mjsmodified
@@ -51,6 +51,7 @@ const files = [
51
'test-stream-sequence.sgl',
52
'test-endpoint.sgl',
53
'test-endpoint-admission.sgl',
+54
'test-endpoint-retry.sgl',
55
];
56
const cleanupFaults = process.argv.includes('--cleanup-faults');
57
const secretCleanup = process.argv.includes('--secret-cleanup');
@@ -74,7 +75,8 @@ const retryPacket = process.argv.includes('--retry-packet');
75
const clientRetry = process.argv.includes('--client-retry');
76
const retryToken = process.argv.includes('--retry-token');
77
const serverRetry = process.argv.includes('--server-retry');
−77
if ([cleanupFaults, secretCleanup, secretRetention, handshakeCleanup, packetRetention, factoryCleanup, eventCleanup, constructionCleanup, pathResponse, cidZero, cidSequence, handlerReadiness, seenBudget, ticketValidation, negotiation, certificatePolicy, retryPolicy, earlyPackets, retryPacket, clientRetry, retryToken, serverRetry].filter(Boolean).length > 1) {
+78
const endpointRetry = process.argv.includes('--endpoint-retry');
+79
if ([cleanupFaults, secretCleanup, secretRetention, handshakeCleanup, packetRetention, factoryCleanup, eventCleanup, constructionCleanup, pathResponse, cidZero, cidSequence, handlerReadiness, seenBudget, ticketValidation, negotiation, certificatePolicy, retryPolicy, earlyPackets, retryPacket, clientRetry, retryToken, serverRetry, endpointRetry].filter(Boolean).length > 1) {
80
throw new Error('Select one focused test suite');
81
}
82
if (cleanupFaults) {
@@ -143,6 +145,9 @@ if (retryToken) {
145
if (serverRetry) {
146
files.splice(0, files.length, 'test-server-retry.sgl');
147
}
+148
if (endpointRetry) {
+149
files.splice(0, files.length, 'test-endpoint-retry.sgl');
+150
}
151
const bodies = files.map(file => {
152
const text = readFileSync(join(root, 'test', file), 'utf8');
153
const boundary = text.indexOf('\n\n');
@@ -156,7 +161,7 @@ writeFileSync(join(source, 'native.sgl'), `(define-library (sigil quic tests nat
161
(sigil math)
162
(sigil io)
163
(sigil socket)
−159
(sigil process)
+164
(sigil process)${files.includes('test-endpoint-retry.sgl') ? '\n (sigil time)' : ''}
165
(sigil crypto)
166
(sigil quic bytes)
167
(sigil quic vectors)
src/sigil/quic/endpoint.sglmodified
@@ -1,16 +1,20 @@
1
;;; Private Q1 UDP adapter. Loopback only until independent security review.
2
;;; One fixed peer, one connection, bounded work and protected output per step.
3
(define-library (sigil quic endpoint)
−4
(import (sigil struct)
+4
(import (sigil crypto)
+5
(sigil struct)
6
(sigil io)
7
(sigil socket)
8
(sigil time)
+9
(sigil quic bytes)
10
(sigil quic packet)
11
(sigil quic frame)
+12
(sigil quic retry token)
13
(sigil quic connection))
14
(export open-quic-endpoint quic-endpoint? quic-endpoint-port
15
quic-endpoint-connection quic-endpoint-closed? quic-endpoint-failure
−13
quic-endpoint-attach! quic-endpoint-accept! quic-endpoint-step!
+16
quic-endpoint-attach! quic-endpoint-accept! quic-endpoint-accept-retry!
+17
quic-endpoint-step!
18
quic-endpoint-close!)
19
(begin
20
(define-struct quic-endpoint
@@ -22,6 +26,8 @@
26
(connection default: #f mutable: #t)
27
(acceptor default: #f mutable: #t)
28
(accept-port default: #f mutable: #t)
+29
(retry-manager default: #f mutable: #t)
+30
(retry-attempts default: 0 mutable: #t)
31
(pending default: '() mutable: #t)
32
(pending-since default: #f mutable: #t)
33
(closed? default: #f mutable: #t)
@@ -74,6 +80,30 @@
80
(set-quic-endpoint-accept-port! endpoint peer-port)
81
(set-quic-endpoint-acceptor! endpoint factory))
82
+83
;; This factory receives copied (original-DCID, client-SCID, Retry-SCID).
+84
;; It must return an owned, fresh retry-aware server with its handler set.
+85
;; The endpoint owns the private token key until confirmation or close.
+86
(define (quic-endpoint-accept-retry! endpoint factory
+87
(keys: (peer-port #f) (token-lifetime 10000)))
+88
(require-unattached! endpoint)
+89
(unless (and (procedure? factory) (or (not peer-port) (port? peer-port 1)))
+90
(error "QUIC endpoint: invalid server factory or peer port"))
+91
(let ((manager (make-retry-token-manager lifetime: token-lifetime)))
+92
(guard (exn
+93
(else
+94
(close-retry-token-manager! manager)
+95
(quic-endpoint-close! endpoint)
+96
(raise exn)))
+97
(set-quic-endpoint-retry-manager! endpoint manager)
+98
(set-quic-endpoint-peer-port! endpoint peer-port)
+99
(set-quic-endpoint-accept-port! endpoint peer-port)
+100
(set-quic-endpoint-acceptor! endpoint factory))))
+101
+102
(define (retire-retry-manager! endpoint)
+103
(let ((manager (quic-endpoint-retry-manager endpoint)))
+104
(set-quic-endpoint-retry-manager! endpoint #f)
+105
(when manager (close-retry-token-manager! manager))))
+106
107
(define (send-pending! endpoint now)
108
(let loop ()
109
(when (pair? (quic-endpoint-pending endpoint))
@@ -120,20 +150,90 @@
150
(decode-frames (opened-quic-packet-payload opened) 'initial 'client))))))
151
(lambda () (discard-packet-keys! keys)))))
152
−123
(define (admit! endpoint bytes peer-port)
−124
(when (and (quic-endpoint-acceptor endpoint) (>= (bytevector-length bytes) 1200))
+153
;; The native socket is IPv4 and receive! has checked the literal address.
+154
;; Bind the token to the actual UDP source port, not a requested peer port.
+155
(define (retry-address peer-port)
+156
(bytevector 127 0 0 1 (quotient peer-port 256) (modulo peer-port 256)))
+157
+158
(define (send-retry! endpoint bytes header peer-port now)
+159
;; Count attempts before authentication, including malformed ciphertext.
+160
;; No source pin, connection, output queue or per-source state is retained.
+161
(when (< (quic-endpoint-retry-attempts endpoint) 32)
+162
(set-quic-endpoint-retry-attempts! endpoint (+ 1 (quic-endpoint-retry-attempts endpoint)))
+163
(when (authenticated-initial? bytes header)
+164
(let* ((original (quic-header-destination-id header))
+165
(client (quic-header-source-id header))
+166
(random-id (random-bytes 8))
+167
(retry (if (bytes=? original random-id) (flip-byte random-id 0) random-id))
+168
(token (make-retry-token (quic-endpoint-retry-manager endpoint)
+169
(retry-address peer-port) original retry client now))
+170
(packet (encode-retry-packet original client retry token)))
+171
(when (or (> (bytevector-length packet) 1200)
+172
(> (bytevector-length packet) (* 3 (bytevector-length bytes))))
+173
(error "QUIC endpoint: Retry output budget exceeded"))
+174
;; A blocked send is a drop. A retransmitted Initial can request
+175
;; another challenge without preserving any unsent output here.
+176
(let ((sent (udp-send (quic-endpoint-socket endpoint) packet "127.0.0.1" peer-port)))
+177
(when (and sent (not (= sent (bytevector-length packet))))
+178
(error "QUIC endpoint: partial Retry datagram send")))))))
+179
+180
(define (admit-retry-connection! endpoint original client retry peer-port)
+181
(let ((connection #f) (transferred? #f))
+182
(dynamic-wind
+183
(lambda () #t)
+184
(lambda ()
+185
(set! connection ((quic-endpoint-acceptor endpoint)
+186
(bytevector-copy original)
+187
(bytevector-copy client)
+188
(bytevector-copy retry)))
+189
(unless (and (quic-connection? connection) (eq? (quic-role connection) 'server)
+190
(not (quic-closed? connection)) (not (quic-peer-parameters connection))
+191
(bytes=? client (quic-peer-id connection)))
+192
(error "QUIC endpoint: Retry factory must return an owned fresh server"))
+193
(let* ((parameters (quic-local-parameters connection))
+194
(original-parameter (assoc 0 parameters))
+195
(source-parameter (assoc 15 parameters))
+196
(retry-parameter (assoc 16 parameters)))
+197
(unless (and original-parameter source-parameter retry-parameter
+198
(bytes=? original (cdr original-parameter))
+199
(bytes=? (quic-local-id connection) (cdr source-parameter))
+200
(bytes=? retry (cdr retry-parameter)))
+201
(error "QUIC endpoint: Retry factory connection ID metadata mismatch")))
+202
(set-quic-endpoint-peer-port! endpoint peer-port)
+203
(set-quic-endpoint-connection! endpoint connection)
+204
(set! transferred? #t))
+205
(lambda ()
+206
(when (and (not transferred?) (quic-connection? connection))
+207
(quic-abort! connection))))))
+208
+209
(define (admit! endpoint bytes peer-port now)
+210
(when (and (quic-endpoint-acceptor endpoint) (port? peer-port 1)
+211
(>= (bytevector-length bytes) 1200))
212
(let ((header (guard (exn (else #f)) (decode-packet-header bytes 0 0))))
213
(when (and header (eq? (quic-header-kind header) 'initial)
−127
(>= (bytevector-length (quic-header-destination-id header)) 8)
−128
(authenticated-initial? bytes header))
−129
(let ((connection ((quic-endpoint-acceptor endpoint)
−130
(bytevector-copy (quic-header-destination-id header))
−131
(bytevector-copy (quic-header-source-id header)))))
−132
(unless (and (quic-connection? connection) (eq? (quic-role connection) 'server)
−133
(not (quic-closed? connection)))
−134
(error "QUIC endpoint: factory must return a fresh server connection"))
−135
(set-quic-endpoint-peer-port! endpoint peer-port)
−136
(set-quic-endpoint-connection! endpoint connection))))))
+214
(>= (bytevector-length (quic-header-destination-id header)) 8))
+215
(cond
+216
((quic-endpoint-retry-manager endpoint)
+217
(if (= 0 (bytevector-length (quic-header-token header)))
+218
(send-retry! endpoint bytes header peer-port now)
+219
;; Invalid token-bearing Initials never get a second Retry.
+220
(let ((original (validate-retry-token (quic-endpoint-retry-manager endpoint)
+221
(retry-address peer-port)
+222
(quic-header-destination-id header)
+223
(quic-header-source-id header)
+224
(quic-header-token header) now)))
+225
(when (and original (authenticated-initial? bytes header))
+226
(admit-retry-connection! endpoint original
+227
(quic-header-source-id header) (quic-header-destination-id header) peer-port)))))
+228
((authenticated-initial? bytes header)
+229
(let ((connection ((quic-endpoint-acceptor endpoint)
+230
(bytevector-copy (quic-header-destination-id header))
+231
(bytevector-copy (quic-header-source-id header)))))
+232
(unless (and (quic-connection? connection) (eq? (quic-role connection) 'server)
+233
(not (quic-closed? connection)))
+234
(error "QUIC endpoint: factory must return a fresh server connection"))
+235
(set-quic-endpoint-peer-port! endpoint peer-port)
+236
(set-quic-endpoint-connection! endpoint connection))))))))
237
238
;; Preserve the configured admission port, not the tentative source pin.
239
;; A failure before confirmation releases the slot without closing UDP.
@@ -141,7 +241,9 @@
241
(let ((connection (quic-endpoint-connection endpoint)))
242
(when (and connection (quic-endpoint-acceptor endpoint))
243
(cond
−144
((quic-confirmed? connection) (set-quic-endpoint-acceptor! endpoint #f))
+244
((quic-confirmed? connection)
+245
(set-quic-endpoint-acceptor! endpoint #f)
+246
(retire-retry-manager! endpoint))
247
((quic-closed? connection)
248
;; This candidate no longer owns the socket or a routed CID.
249
;; Retained references must not keep its closing replay alive.
@@ -154,6 +256,7 @@
256
(define (receive! endpoint now)
257
;; Out-of-peer noise must not consume the connection's receive budget.
258
;; A separate read ceiling still bounds work under a continuous flood.
+259
(set-quic-endpoint-retry-attempts! endpoint 0)
260
(let loop ((reads 0) (admitted 0))
261
(when (and (< reads 96) (< admitted 32))
262
(let ((received (udp-receive (quic-endpoint-socket endpoint) (+ packet-datagram-limit 1))))
@@ -166,7 +269,7 @@
269
;; copies these exact bytes, including NUL and invalid UTF-8.
270
(let ((bytes (string->utf8 (car received))))
271
(when (<= 1 (bytevector-length bytes) packet-datagram-limit)
−169
(unless (quic-endpoint-connection endpoint) (admit! endpoint bytes (caddr received)))
+272
(unless (quic-endpoint-connection endpoint) (admit! endpoint bytes (caddr received) now))
273
(when (quic-endpoint-connection endpoint)
274
(set! processed? #t)
275
(quic-receive! (quic-endpoint-connection endpoint) bytes now)
@@ -190,11 +293,16 @@
293
(set-quic-endpoint-pending! endpoint '())
294
(set-quic-endpoint-pending-since! endpoint #f)
295
(set-quic-endpoint-acceptor! endpoint #f)
−193
(socket-close (quic-endpoint-socket endpoint))
−194
(set-quic-endpoint-closed?! endpoint #t)
−195
;; RFC 9000 10.2 permits early termination when the dedicated
−196
;; UDP socket is closed. Release the standalone cache too.
−197
(when connection (quic-abort! connection)))))))
+296
(dynamic-wind
+297
(lambda () #t)
+298
(lambda () (retire-retry-manager! endpoint))
+299
(lambda ()
+300
(socket-close (quic-endpoint-socket endpoint))
+301
(set-quic-endpoint-closed?! endpoint #t)
+302
;; Retire packet/TLS secrets even if token cleanup raises.
+303
;; RFC 9000 10.2 permits early termination when the
+304
;; dedicated UDP socket closes, including its close cache.
+305
(when connection (quic-abort! connection)))))))))
306
307
(define (step! endpoint wait-ms)
308
(let ((connection (quic-endpoint-connection endpoint)) (now (endpoint-now endpoint)))
test/test-endpoint-retry.sgladded
@@ -0,0 +1,387 @@
+1
(import (sigil test)
+2
(sigil io)
+3
(sigil socket)
+4
(sigil time)
+5
(sigil crypto)
+6
(sigil quic bytes)
+7
(sigil quic frame)
+8
(sigil quic packet)
+9
(sigil quic connection)
+10
(sigil quic stream)
+11
(sigil quic endpoint))
+12
+13
(define er-original #u8(1 2 3 4 5 6 7 8))
+14
(define er-client-id #u8(11 12 13 14 15 16 17 18))
+15
+16
;; Public UDP fixture: client, endpoint, primary/alternate sockets, client
+17
;; stream, returned server/stream, factory calls/IDs, clock origin, signing key.
+18
;; All sockets and candidate connections remain owned even on assertion errors.
+19
(define (er-with-pair procedure
+20
(keys: (retry? #t) (pin? #f) (defer? #f) (lifetime 10000) (bad-factory? #f)))
+21
(let ((key (ecdsa-p256-generate-keypair)) (client #f) (endpoint #f)
+22
(primary #f) (alternate #f) (pair #f))
+23
(dynamic-wind
+24
(lambda () #t)
+25
(lambda ()
+26
(set! client
+27
(make-quic-client (list #u8(113 49)) "example.test"
+28
(stream-transport-parameters 'client)
+29
(lambda (certificates name)
+30
(and (equal? certificates (list #u8(48 1 0))) (cdr key)))
+31
destination-id: er-original local-id: er-client-id defer-key-share?: defer?))
+32
(set! endpoint (open-quic-endpoint))
+33
(set! primary (udp-socket))
+34
(set! alternate (udp-socket))
+35
(for-each
+36
(lambda (socket)
+37
(unless (and socket (udp-bind socket 0 "127.0.0.1")
+38
(socket-set-non-blocking! socket #t))
+39
(error "endpoint Retry fixture: socket setup failed")))
+40
(list primary alternate))
+41
(set! pair (vector client endpoint primary alternate (make-quic-stream client)
+42
#f #f 0 #f (current-jiffy) key))
+43
(let ((factory
+44
(lambda (original source retry)
+45
(vector-set! pair 7 (+ 1 (vector-ref pair 7)))
+46
(vector-set! pair 8 (list (bytevector-copy original) (bytevector-copy source)
+47
(and retry (bytevector-copy retry))))
+48
(let ((server
+49
(make-quic-server (list #u8(113 49))
+50
(stream-transport-parameters 'server) (list #u8(48 1 0)) (car key)
+51
original source retry-source-id: (and (not bad-factory?) retry))))
+52
(vector-set! pair 5 server)
+53
(vector-set! pair 6 (make-quic-stream server))
+54
server))))
+55
(if retry?
+56
(quic-endpoint-accept-retry! endpoint factory
+57
peer-port: (and pin? (cadr (socket-local-address primary))) token-lifetime: lifetime)
+58
(quic-endpoint-accept! endpoint
+59
(lambda (original source) (factory original source #f)))))
+60
(quic-start! client 0)
+61
(procedure pair))
+62
(lambda ()
+63
(when primary (socket-close primary))
+64
(when alternate (socket-close alternate))
+65
(when endpoint (quic-endpoint-close! endpoint))
+66
(when client (quic-abort! client))
+67
(when (and pair (vector-ref pair 5)) (quic-abort! (vector-ref pair 5)))
+68
(do ((index 0 (+ index 1))) ((= index (bytevector-length (car key))))
+69
(bytevector-u8-set! (car key) index 0))))))
+70
+71
(define (er-now pair)
+72
(quotient (* 1000 (- (current-jiffy) (vector-ref pair 9))) (jiffies-per-second)))
+73
+74
(define (er-send! pair packet (keys: (alternate? #f)))
+75
(let ((sent (udp-send (vector-ref pair (if alternate? 3 2)) packet "127.0.0.1"
+76
(quic-endpoint-port (vector-ref pair 1)))))
+77
(unless (and sent (= sent (bytevector-length packet)))
+78
(error "endpoint Retry fixture: datagram send failed"))))
+79
+80
(define (er-read pair (keys: (alternate? #f)))
+81
(let ((received (udp-receive (vector-ref pair (if alternate? 3 2)) 65528)))
+82
(and received
+83
(equal? (cadr received) "127.0.0.1")
+84
(= (caddr received) (quic-endpoint-port (vector-ref pair 1)))
+85
(string->utf8 (car received)))))
+86
+87
(define (er-empty? pair)
+88
(and (= 0 (vector-ref pair 7)) (not (quic-endpoint-connection (vector-ref pair 1)))
+89
(not (quic-endpoint-closed? (vector-ref pair 1)))))
+90
+91
(define (er-challenge! pair)
+92
(for-each (lambda (packet) (er-send! pair packet))
+93
(quic-take-datagrams! (vector-ref pair 0) (er-now pair)))
+94
(quic-endpoint-step! (vector-ref pair 1) wait-ms: 1)
+95
(let ((packet (er-read pair)))
+96
(unless (and packet (er-empty? pair) (valid-retry-packet? er-original packet))
+97
(error "endpoint Retry fixture: no stateless challenge"))
+98
packet))
+99
+100
(define (er-retried! pair challenge)
+101
(quic-receive! (vector-ref pair 0) challenge (er-now pair))
+102
(let ((packets (quic-take-datagrams! (vector-ref pair 0) (er-now pair))))
+103
(unless (pair? packets) (error "endpoint Retry fixture: no retried Initial"))
+104
(car packets)))
+105
+106
(define (er-pump! pair predicate)
+107
(let loop ((round 0))
+108
(unless (predicate)
+109
(when (= round 1000) (error "endpoint Retry fixture: bounded pump exhausted"))
+110
(let ((now (er-now pair)))
+111
(quic-tick! (vector-ref pair 0) now)
+112
(for-each (lambda (packet) (er-send! pair packet))
+113
(quic-take-datagrams! (vector-ref pair 0) now)))
+114
(quic-endpoint-step! (vector-ref pair 1) wait-ms: 1)
+115
(let drain ((count 0))
+116
(when (< count 64)
+117
(let ((packet (er-read pair)))
+118
(when packet
+119
(quic-receive! (vector-ref pair 0) packet (er-now pair))
+120
(drain (+ count 1))))))
+121
(loop (+ round 1)))))
+122
+123
(define (er-ready! pair)
+124
(er-pump! pair
+125
(lambda ()
+126
(and (quic-confirmed? (vector-ref pair 0)) (vector-ref pair 5)
+127
(quic-confirmed? (vector-ref pair 5))))))
+128
+129
(define (er-crypto offset data)
+130
(make-quic-frame 6 (list (cons 'offset offset) (cons 'data data))))
+131
+132
;; Independently frame a public Initial around an observed token. No token
+133
;; secret or private endpoint field is exposed to the fixture.
+134
(define (er-initial destination source token frames (keys: (size 1200)))
+135
(let* ((keys (initial-keys destination 'client))
+136
(body (encode-frames frames 'initial 'client))
+137
(header (encode-packet-header 'initial destination source token 0 4 17 length-width: 2))
+138
(payload (bytes-append body
+139
(make-bytevector (- size (bytevector-length header) 16 (bytevector-length body)) 0))))
+140
(dynamic-wind
+141
(lambda () #t)
+142
(lambda ()
+143
(seal-quic-packet keys 'initial destination source 0 payload token: token length-width: 2))
+144
(lambda () (discard-packet-keys! keys)))))
+145
+146
(define (er-forged header (keys: (destination #f) (source #f) (token #f) (frames #f)))
+147
(er-initial (or destination (quic-header-source-id header)) (or source er-client-id)
+148
(or token (quic-header-token header))
+149
(or frames (list (er-crypto 0 #u8(1 0 0 0))))))
+150
+151
(define (er-drop? pair packet (keys: (alternate? #f)))
+152
(er-send! pair packet alternate?: alternate?)
+153
(quic-endpoint-step! (vector-ref pair 1) wait-ms: 1)
+154
(and (er-empty? pair) (not (er-read pair alternate?: alternate?))))
+155
+156
(define (er-positive procedure)
+157
(assert-true (guard (exn (else #f)) (procedure))))
+158
+159
(test-group "Private loopback Retry admission"
+160
(test "ql12-endpoint-retry: authenticated Initial produces bounded challenge without factory or pin"
+161
(er-positive
+162
(lambda ()
+163
(er-with-pair
+164
(lambda (pair)
+165
(let* ((packet (er-challenge! pair)) (header (decode-packet-header packet 0 0)))
+166
(and (<= (bytevector-length packet) 1200)
+167
(bytes=? er-client-id (quic-header-destination-id header))
+168
(= 8 (bytevector-length (quic-header-source-id header)))
+169
(not (bytes=? er-original (quic-header-source-id header)))
+170
(> (bytevector-length (quic-header-token header)) 0)
+171
(er-empty? pair)
+172
(begin
+173
(er-send! pair
+174
(er-initial er-original er-client-id #u8() (list (er-crypto 0 #u8(1))))
+175
alternate?: #t)
+176
(quic-endpoint-step! (vector-ref pair 1) wait-ms: 1)
+177
(let ((other (er-read pair alternate?: #t)))
+178
(and other (valid-retry-packet? er-original other) (er-empty? pair)))))))))))
+179
(test "ql12-endpoint-retry: token return confirms both roles and transfers binary streams"
+180
(er-positive
+181
(lambda ()
+182
(er-with-pair
+183
(lambda (pair)
+184
(er-ready! pair)
+185
(quic-stream-write! (vector-ref pair 4) #u8(0 255 1 128) fin?: #t)
+186
(let ((request #u8()) (response #u8()))
+187
(er-pump! pair
+188
(lambda ()
+189
(set! request (bytes-append request (quic-stream-read! (vector-ref pair 6))))
+190
(quic-stream-eof? (vector-ref pair 6))))
+191
(quic-stream-write! (vector-ref pair 6) #u8(255 0 129 2) fin?: #t)
+192
(er-pump! pair
+193
(lambda ()
+194
(set! response (bytes-append response (quic-stream-read! (vector-ref pair 4))))
+195
(quic-stream-eof? (vector-ref pair 4))))
+196
(and (= 1 (vector-ref pair 7)) (bytes=? request #u8(0 255 1 128))
+197
(bytes=? #u8(255 0 129 2) response)
+198
(bytes=? er-original (car (vector-ref pair 8)))
+199
(bytes=? er-client-id (cadr (vector-ref pair 8)))
+200
(bytes=? (caddr (vector-ref pair 8))
+201
(cdr (assoc 16 (quic-peer-parameters (vector-ref pair 0))))))))))))
+202
(test "ql12-endpoint-retry: QUIC Retry and TLS HelloRetryRequest complete together"
+203
(er-positive
+204
(lambda ()
+205
(er-with-pair
+206
(lambda (pair)
+207
(er-ready! pair)
+208
(and (= 1 (vector-ref pair 7))
+209
(assoc 16 (quic-peer-parameters (vector-ref pair 0))) #t)) defer?: #t))))
+210
(test "ql12-endpoint-retry: token replay from another UDP source port drops without second Retry"
+211
(er-positive
+212
(lambda ()
+213
(er-with-pair
+214
(lambda (pair)
+215
(let* ((challenge (er-challenge! pair)) (packet (er-retried! pair challenge)))
+216
(and (er-drop? pair packet alternate?: #t)
+217
(begin (er-send! pair packet) (er-ready! pair) (= 1 (vector-ref pair 7))))))))))
+218
(test "ql12-endpoint-retry: changed client source CID invalidates token before factory"
+219
(er-positive
+220
(lambda ()
+221
(er-with-pair
+222
(lambda (pair)
+223
(let ((header (decode-packet-header (er-challenge! pair) 0 0)))
+224
(er-drop? pair (er-forged header source: (flip-byte er-client-id 0)))))))))
+225
(test "ql12-endpoint-retry: changed Retry destination CID invalidates token before factory"
+226
(er-positive
+227
(lambda ()
+228
(er-with-pair
+229
(lambda (pair)
+230
(let ((header (decode-packet-header (er-challenge! pair) 0 0)))
+231
(er-drop? pair (er-forged header destination: (flip-byte (quic-header-source-id header) 0)))))))))
+232
(test "ql12-endpoint-retry: malformed or altered tokens silently drop without another Retry"
+233
(er-positive
+234
(lambda ()
+235
(er-with-pair
+236
(lambda (pair)
+237
(let ((header (decode-packet-header (er-challenge! pair) 0 0)))
+238
(every (lambda (token) (er-drop? pair (er-forged header token: token)))
+239
(list #u8(1) (flip-byte (quic-header-token header) 0)
+240
(flip-byte (quic-header-token header) (- (bytevector-length (quic-header-token header)) 1))))))))))
+241
(test "ql12-endpoint-retry: valid token with bad Initial tag cannot allocate or pin"
+242
(er-positive
+243
(lambda ()
+244
(er-with-pair
+245
(lambda (pair)
+246
(let* ((challenge (er-challenge! pair)) (packet (er-retried! pair challenge)))
+247
(and (er-drop? pair (flip-byte packet (- (bytevector-length packet) 1)))
+248
(begin (er-send! pair packet) (er-ready! pair) (= 1 (vector-ref pair 7))))))))))
+249
(test "ql12-endpoint-retry: valid token still requires nonempty CRYPTO at offset zero"
+250
(er-positive
+251
(lambda ()
+252
(er-with-pair
+253
(lambda (pair)
+254
(let ((header (decode-packet-header (er-challenge! pair) 0 0)))
+255
(every (lambda (frames) (er-drop? pair (er-forged header frames: frames)))
+256
(list (list (make-quic-frame 1 '()))
+257
(list (er-crypto 1 #u8(1 0 0 0))) (list (er-crypto 0 #u8()))))))))))
+258
(test "ql12-endpoint-retry: expired token cannot allocate or solicit another Retry"
+259
(er-positive
+260
(lambda ()
+261
(er-with-pair
+262
(lambda (pair)
+263
(let ((packet (er-retried! pair (er-challenge! pair))))
+264
(quic-endpoint-step! (vector-ref pair 1) wait-ms: 10)
+265
(er-drop? pair packet))) lifetime: 1))))
+266
(test "ql12-endpoint-retry: bad tag and missing first CRYPTO do not solicit a challenge"
+267
(er-positive
+268
(lambda ()
+269
(er-with-pair
+270
(lambda (pair)
+271
(let ((packet (er-initial er-original er-client-id #u8() (list (er-crypto 0 #u8(1))))))
+272
(and (er-drop? pair (flip-byte packet (- (bytevector-length packet) 1)))
+273
(every
+274
(lambda (frames) (er-drop? pair (er-initial er-original er-client-id #u8() frames)))
+275
(list (list (make-quic-frame 1 '())) (list (er-crypto 1 #u8(1)))
+276
(list (er-crypto 0 #u8()))))
+277
(begin (er-challenge! pair) #t))))))))
+278
(test "ql12-endpoint-retry: authenticated Initial under 1200 bytes cannot solicit Retry"
+279
(er-positive
+280
(lambda ()
+281
(er-with-pair
+282
(lambda (pair)
+283
(and (er-drop? pair
+284
(er-initial er-original er-client-id #u8() (list (er-crypto 0 #u8(1))) size: 1199))
+285
(begin (er-challenge! pair) #t)))))))
+286
(test "ql12-endpoint-retry: one receive pass emits at most 32 challenges"
+287
(er-positive
+288
(lambda ()
+289
(er-with-pair
+290
(lambda (pair)
+291
(let ((packet (car (quic-take-datagrams! (vector-ref pair 0) (er-now pair)))))
+292
(do ((index 0 (+ index 1))) ((= index 40)) (er-send! pair packet))
+293
(quic-endpoint-step! (vector-ref pair 1) wait-ms: 1)
+294
(let drain ((count 0))
+295
(let ((challenge (er-read pair)))
+296
(if challenge
+297
(and (< count 40) (valid-retry-packet? er-original challenge) (drain (+ count 1)))
+298
(and (= count 32) (er-empty? pair)))))))))))
+299
(test "ql12-endpoint-retry: failed authentication consumes challenge attempt budget and next pass resets it"
+300
(er-positive
+301
(lambda ()
+302
(er-with-pair
+303
(lambda (pair)
+304
(let* ((packet (car (quic-take-datagrams! (vector-ref pair 0) (er-now pair))))
+305
(bad (flip-byte packet (- (bytevector-length packet) 1))))
+306
(do ((index 0 (+ index 1))) ((= index 32)) (er-send! pair bad))
+307
(er-send! pair packet)
+308
(quic-endpoint-step! (vector-ref pair 1) wait-ms: 1)
+309
(and (er-empty? pair) (not (er-read pair))
+310
(begin
+311
(er-send! pair packet)
+312
(quic-endpoint-step! (vector-ref pair 1) wait-ms: 1)
+313
(let ((challenge (er-read pair)))
+314
(and challenge (valid-retry-packet? er-original challenge) (er-empty? pair)))))))))))
+315
(test "ql12-endpoint-retry: configured source port filters challenge requests before admission"
+316
(er-positive
+317
(lambda ()
+318
(er-with-pair
+319
(lambda (pair)
+320
(let ((packet (car (quic-take-datagrams! (vector-ref pair 0) (er-now pair)))))
+321
(and (er-drop? pair packet alternate?: #t)
+322
(begin
+323
(er-send! pair packet)
+324
(quic-endpoint-step! (vector-ref pair 1) wait-ms: 1)
+325
(let ((challenge (er-read pair)))
+326
(and challenge (valid-retry-packet? er-original challenge)
+327
(begin (er-send! pair (er-retried! pair challenge))
+328
(er-ready! pair) (= 1 (vector-ref pair 7))))))))) pin?: #t))))
+329
(test "ql12-endpoint-retry: failed tentative TLS candidate re-arms the same token and factory"
+330
(er-positive
+331
(lambda ()
+332
(er-with-pair
+333
(lambda (pair)
+334
(let* ((challenge (er-challenge! pair)) (header (decode-packet-header challenge 0 0))
+335
(packet (er-retried! pair challenge)))
+336
(er-send! pair (er-forged header))
+337
(quic-endpoint-step! (vector-ref pair 1) wait-ms: 1)
+338
(and (= 1 (vector-ref pair 7)) (quic-closed? (vector-ref pair 5))
+339
(not (quic-endpoint-connection (vector-ref pair 1)))
+340
(begin
+341
(let drain ((count 0))
+342
(when (and (< count 64) (er-read pair)) (drain (+ count 1))))
+343
(er-send! pair packet)
+344
(er-ready! pair)
+345
(= 2 (vector-ref pair 7))))))))))
+346
(test "ql12-endpoint-retry: wrong factory Retry metadata aborts returned candidate and closes endpoint"
+347
(er-positive
+348
(lambda ()
+349
(er-with-pair
+350
(lambda (pair)
+351
(let ((packet (er-retried! pair (er-challenge! pair))))
+352
(er-send! pair packet)
+353
(let ((raised? (guard (exn (else #t)) (quic-endpoint-step! (vector-ref pair 1) wait-ms: 1) #f)))
+354
(and raised? (= 1 (vector-ref pair 7)) (quic-endpoint-closed? (vector-ref pair 1))
+355
(quic-closed? (vector-ref pair 5))
+356
(guard (exn (else #t)) (quic-stream-read! (vector-ref pair 6)) #f))))) bad-factory?: #t))))
+357
(test "ql12-endpoint-control: legacy two-ID admission still confirms both roles"
+358
(er-with-pair
+359
(lambda (pair)
+360
(er-ready! pair)
+361
(assert-equal 1 (vector-ref pair 7))
+362
(assert-false (assoc 16 (quic-peer-parameters (vector-ref pair 0))))) retry?: #f))
+363
(test "ql12-endpoint-control: invalid lifetime factory and port reject before accepting"
+364
(let ((endpoint (open-quic-endpoint)))
+365
(dynamic-wind
+366
(lambda () #t)
+367
(lambda ()
+368
(for-each
+369
(lambda (lifetime)
+370
(assert-error (quic-endpoint-accept-retry! endpoint (lambda args #f) token-lifetime: lifetime)))
+371
'(0 -1 60001 1.5))
+372
(assert-error (quic-endpoint-accept-retry! endpoint #f))
+373
(assert-error (quic-endpoint-accept-retry! endpoint (lambda args #f) peer-port: 0))
+374
(assert-false (quic-endpoint-closed? endpoint))
+375
(assert-false (quic-endpoint-connection endpoint)))
+376
(lambda () (quic-endpoint-close! endpoint)))))
+377
(test "ql12-endpoint-control: close before token return is idempotent and releases socket"
+378
(er-with-pair
+379
(lambda (pair)
+380
(let ((endpoint (vector-ref pair 1)))
+381
(quic-endpoint-close! endpoint)
+382
(quic-endpoint-close! endpoint)
+383
(assert-true (quic-endpoint-closed? endpoint))
+384
(assert-false (quic-endpoint-step! endpoint))
+385
(assert-equal 0 (vector-ref pair 7))
+386
(let ((replacement (open-quic-endpoint port: (quic-endpoint-port endpoint))))
+387
(quic-endpoint-close! replacement)))))))