Commite6b7cfdfRecorded18 Oct 2026Repositorysigil-quic
Pin TLS server negotiation rejection before authentication
Changed
scripts/inject-negotiation-faults.mjs | 25 +++++++++++++++++++++++++
scripts/native-tls-tests.mjs | 7 ++++++-
test/test-tls-negotiation.sgl | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 170 insertions(+), 1 deletion(-)Diff
scripts/inject-negotiation-faults.mjsadded
@@ -0,0 +1,25 @@
+1
// Remote-only sensitivity check for the public negotiation rejection tests.+2
// Canonical production inputs remain untouched; restore before full gates.+3
import { readFileSync, writeFileSync } from 'node:fs';+4
+5
const [input, output] = process.argv.slice(2);+6
if (!input || !output || input === output) {+7
throw new Error('Usage: inject-negotiation-faults.mjs INPUT OUTPUT');+8
}+9
let source = readFileSync(input, 'utf8');+10
if (source.includes('TEST ONLY')) throw new Error('Expected canonical handshake source');+11
function replaceOnce(before, after) {+12
if (source.split(before).length !== 2) throw new Error('Expected one canonical guard');+13
source = source.replace(before, () => after);+14
}+15
replaceOnce(` (validate-server-extension-block extensions (tls-handshake-offered engine)+16
(if retry? 'hello-retry-request 'server-hello))`, ' #t');+17
replaceOnce(` (validate-server-extension-block extensions (tls-handshake-offered engine)+18
'encrypted-extensions)`, ' #t');+19
replaceOnce(` (when (assoc 0 extensions)+20
(unless (bytes=? (cdr (assoc 0 extensions)) #u8())+21
(error "TLS: nonempty server_name acknowledgement")))`, ' #t');+22
replaceOnce(` (unless (bytes=? selected+23
(select-alpn (list selected) (tls-handshake-protocols engine)))+24
(error "TLS: unoffered ALPN"))`, ' #t');+25
writeFileSync(output, ';;; TEST ONLY: negotiation guards removed for mutation sensitivity.\n' + source);scripts/native-tls-tests.mjsmodified
@@ -12,6 +12,7 @@ mkdirSync(source, { recursive: true });
12
const files = [ 13
'test-tls-codec.sgl', 'test-tls-extensions.sgl', 14
'test-tls-seen.sgl',+15
'test-tls-negotiation.sgl', 16
'test-tls-authentication.sgl', 'test-tls-handshake.sgl', 17
'test-tls-input-bounds.sgl', 18
'test-tls-alerts.sgl',@@ -58,7 +59,8 @@ const cidSequence = process.argv.includes('--cid-sequence');
59
const handlerReadiness = process.argv.includes('--handler-readiness'); 60
const seenBudget = process.argv.includes('--seen-budget'); 61
const ticketValidation = process.argv.includes('--ticket-validation');−61
if ([cleanupFaults, secretCleanup, secretRetention, handshakeCleanup, packetRetention, factoryCleanup, eventCleanup, constructionCleanup, pathResponse, cidZero, cidSequence, handlerReadiness, seenBudget, ticketValidation].filter(Boolean).length > 1) {+62
const negotiation = process.argv.includes('--negotiation');+63
if ([cleanupFaults, secretCleanup, secretRetention, handshakeCleanup, packetRetention, factoryCleanup, eventCleanup, constructionCleanup, pathResponse, cidZero, cidSequence, handlerReadiness, seenBudget, ticketValidation, negotiation].filter(Boolean).length > 1) { 64
throw new Error('Select one focused test suite'); 65
} 66
if (cleanupFaults) {@@ -103,6 +105,9 @@ if (seenBudget) {
105
if (ticketValidation) { 106
files.splice(0, files.length, 'test-ticket-validation.sgl'); 107
}+108
if (negotiation) {+109
files.splice(0, files.length, 'test-tls-negotiation.sgl');+110
} 111
const bodies = files.map(file => { 112
const text = readFileSync(join(root, 'test', file), 'utf8'); 113
const boundary = text.indexOf('\n\n');test/test-tls-negotiation.sgladded
@@ -0,0 +1,139 @@
+1
(import (sigil test)+2
(sigil crypto)+3
(sigil quic tls)+4
(sigil quic tls messages)+5
(sigil quic tls extensions)+6
(sigil quic tls authentication))+7
+8
(define (ql13n-wipe! bytes)+9
(when (bytevector? bytes)+10
(do ((i 0 (+ i 1))) ((= i (bytevector-length bytes)))+11
(bytevector-u8-set! bytes i 0))))+12
+13
(define (ql13n-wipe-events! events)+14
(for-each+15
(lambda (event)+16
(when (eq? (tls-event-kind event) 'secrets)+17
(ql13n-wipe! (car (tls-event-data event)))+18
(ql13n-wipe! (cdr (tls-event-data event)))))+19
events))+20
+21
(define (ql13n-message events type)+22
(let ((matches+23
(filter+24
(lambda (event)+25
(and (eq? (tls-event-kind event) 'crypto)+26
(= type (bytevector-u8-ref (tls-event-data event) 0))))+27
events)))+28
(unless (= 1 (length matches))+29
(error "Negotiation fixture did not produce the requested message" type))+30
(tls-event-data (car matches))))+31
+32
(define (ql13n-with-flight identity procedure)+33
(let ((key (ecdsa-p256-generate-keypair)) (client #f) (server #f) (flight '()))+34
(dynamic-wind+35
(lambda () #t)+36
(lambda ()+37
(set! client+38
(make-tls-client (list #u8(113 49)) identity (list (cons 15 #u8(1)))+39
(if (eq? identity 'pinned-key)+40
(make-tls-pin-verifier (cdr key))+41
(lambda (chain name)+42
(and (equal? chain (list #u8(48 1 0)))+43
(equal? name identity) (cdr key))))))+44
(set! server+45
(make-tls-server (list #u8(113 49))+46
(list (cons 0 #u8(9)) (cons 15 #u8(2))) (list #u8(48 1 0)) (car key)))+47
(tls-bind-peer! client #u8(9) #u8(2) #f)+48
(tls-bind-peer! server #u8(9) #u8(1) #f)+49
(tls-start! client)+50
(tls-input! server 'initial (ql13n-message (tls-take-events! client) 1))+51
(set! flight (tls-take-events! server))+52
(unless (eq? (tls-state client) 'server-hello)+53
(error "Negotiation fixture client is not awaiting ServerHello"))+54
(procedure client (ql13n-message flight 2) (ql13n-message flight 8)))+55
(lambda ()+56
(when client+57
(ql13n-wipe-events! (tls-take-events! client))+58
(tls-abort! client))+59
(when server (tls-abort! server))+60
(ql13n-wipe-events! flight)+61
(ql13n-wipe! (car key))))))+62
+63
(define (ql13n-rejected! client level wire reason (keys: (alert #f)))+64
(let* ((failure (guard (exn (else exn)) (tls-input! client level wire) #f))+65
(events (tls-take-events! client))+66
(rejected?+67
(and (error-object? failure)+68
(equal? reason (error-object-message failure))+69
(or (not alert) (and (tls-alert? failure) (= alert (tls-alert-code failure))))+70
(tls-failed? client)+71
(null? events)+72
(not (tls-peer-parameters client))+73
(not (tls-negotiated-alpn client)))))+74
(ql13n-wipe-events! events)+75
(assert-true rejected?)))+76
+77
(define (ql13n-server-hello! id data)+78
(ql13n-with-flight 'pinned-key+79
(lambda (client hello-wire extensions-wire)+80
(let ((hello (decode-server-hello hello-wire)))+81
(ql13n-rejected! client 'initial+82
(encode-server-hello (tls-server-hello-random hello)+83
(tls-server-hello-session-id hello) (tls-server-hello-cipher-suite hello)+84
(append (tls-server-hello-extensions hello) (list (cons id data))))+85
"TLS: misplaced or unsolicited server extension")))))+86
+87
(define (ql13n-change-ee wire id data)+88
(encode-encrypted-extensions+89
(append+90
(filter (lambda (entry) (not (= id (car entry)))) (decode-encrypted-extensions wire))+91
(list (cons id data)))))+92
+93
(define (ql13n-before-ee! client hello)+94
(tls-input! client 'initial hello)+95
(unless (eq? (tls-state client) 'encrypted-extensions)+96
(error "Negotiation fixture did not reach EncryptedExtensions"))+97
(ql13n-wipe-events! (tls-take-events! client)))+98
+99
(define (ql13n-valid! identity)+100
(ql13n-with-flight identity+101
(lambda (client hello extensions)+102
(ql13n-before-ee! client hello)+103
(tls-input! client 'handshake extensions)+104
(assert-equal 'certificate (tls-state client))+105
(assert-false (tls-failed? client))+106
(assert-equal '() (tls-take-events! client))+107
(assert-false (tls-peer-parameters client))+108
(assert-false (tls-negotiated-alpn client)))))+109
+110
(test-group "Server negotiation rejection at the offending message"+111
(test "ql13-negotiation: ServerHello rejects PSK selection before key installation"+112
(ql13n-server-hello! 41 #u8(0 0)))+113
(test "ql13-negotiation: ServerHello rejects early-data selection before key installation"+114
(ql13n-server-hello! 42 #u8()))+115
(test "ql13-negotiation: ServerHello rejects misplaced ALPN before key installation"+116
(ql13n-server-hello! 16 (encode-alpn (list #u8(113 49)))))+117
(test "ql13-negotiation: EncryptedExtensions rejects unoffered ALPN with alert 120"+118
(ql13n-with-flight 'pinned-key+119
(lambda (client hello extensions)+120
(ql13n-before-ee! client hello)+121
(ql13n-rejected! client 'handshake+122
(ql13n-change-ee extensions 16 (encode-alpn (list #u8(113 57))))+123
"TLS ALPN: no_application_protocol" alert: 120))))+124
(test "ql13-negotiation: EncryptedExtensions rejects unsolicited SNI acknowledgement"+125
(ql13n-with-flight 'pinned-key+126
(lambda (client hello extensions)+127
(ql13n-before-ee! client hello)+128
(ql13n-rejected! client 'handshake (ql13n-change-ee extensions 0 #u8())+129
"TLS: misplaced or unsolicited server extension"))))+130
(test "ql13-negotiation: EncryptedExtensions rejects nonempty offered-SNI acknowledgement"+131
(ql13n-with-flight "negotiation.test"+132
(lambda (client hello extensions)+133
(ql13n-before-ee! client hello)+134
(ql13n-rejected! client 'handshake (ql13n-change-ee extensions 0 #u8(1))+135
"TLS: nonempty server_name acknowledgement"))))+136
(test "ql13-negotiation-control: valid pinned flight advances without SNI acknowledgement"+137
(ql13n-valid! 'pinned-key))+138
(test "ql13-negotiation-control: valid named flight accepts empty SNI acknowledgement"+139
(ql13n-valid! "negotiation.test")))