AtlatestRepositorysigil-quic

sigil-quic / tree / testtest-tls-authentication.sgl

1(import (sigil test)
2 (sigil crypto)
3 (sigil process)
4 (sigil quic bytes)
5 (sigil quic vectors)
6 (sigil quic tls codec)
7 (sigil quic tls authentication)
8 (sigil quic tls transcript)
9 (sigil quic tls vectors))
11(define (check-authentication-wire expected actual)
12 (assert-equal
13 (if (equal? (getenv "QUIC_TLS_AUTH_RED") "1") (flip-byte expected 0) expected)
14 actual))
16(test-group "TLS authentication flight"
17 (test "auth-vector: RFC8448 EncryptedExtensions"
18 (check-authentication-wire (fixture 'EncryptedExtensions)
19 (encode-encrypted-extensions (decode-encrypted-extensions (fixture 'EncryptedExtensions)))))
20 (test "auth-vector: RFC8448 Certificate"
21 (let ((certificate (decode-certificate (fixture 'Certificate))))
22 (check-authentication-wire (fixture 'Certificate)
23 (encode-certificate (tls-certificate-context certificate)
24 (tls-certificate-entries certificate)))))
25 (test "auth-vector: RFC8448 CertificateVerify"
26 (let ((signature (decode-certificate-verify (fixture 'CertificateVerify))))
27 (check-authentication-wire (fixture 'CertificateVerify)
28 (encode-certificate-verify (car signature) (cdr signature)))))
29 (test "auth-vector: RFC8448 Finished"
30 (check-authentication-wire (fixture 'ServerFinished)
31 (encode-finished (decode-finished (fixture 'ServerFinished)))))
32 (test "auth-vector: RFC8448 CertificateRequest"
33 (let ((request (decode-certificate-request (handshake-fixture 'certificate-request))))
34 (check-authentication-wire (handshake-fixture 'certificate-request)
35 (encode-certificate-request (car request) (cdr request)))))
36 (test "published certificate fields decoded without establishing trust"
37 (let* ((certificate (decode-certificate (fixture 'Certificate)))
38 (entries (tls-certificate-entries certificate)))
39 (assert-equal #u8() (tls-certificate-context certificate))
40 (assert-equal 1 (length entries))
41 (assert-equal 432 (bytevector-length (caar entries)))
42 (assert-equal '() (cdar entries))))
43 (test "empty client certificate and opaque entry extensions round trip"
44 (let ((empty (decode-certificate (encode-certificate #u8() '()))))
45 (assert-equal '() (tls-certificate-entries empty)))
46 (let* ((entries (list (cons #u8(48 0) (list (cons 65000 #u8(1 2))))))
47 (certificate (decode-certificate (encode-certificate #u8(1) entries))))
48 (assert-equal #u8(1) (tls-certificate-context certificate))
49 (assert-equal entries (tls-certificate-entries certificate))))
50 (test "certificate chain count is bounded in both directions"
51 (let ((entries (make-list (+ tls-certificate-limit 1) (cons #u8(48 0) '()))))
52 (assert-error (encode-certificate #u8() entries)))
53 (let ((chain (apply bytes-append (make-list 17 #u8(0 0 1 1 0 0)))))
54 (assert-error (decode-certificate
55 (encode-handshake 11 (bytes-append #u8(0) (tls-vector chain 3 0 1000)))))))
56 (test "certificate lengths and nested extensions are bounded"
57 (assert-error (encode-certificate #u8() (list (cons #u8() '()))))
58 (assert-error (encode-certificate #u8() (list (cons (make-bytevector tls-handshake-limit) '()))))
59 (assert-error (decode-certificate (encode-handshake 11 #u8(0 0 0 3 0 0 0))))
60 (assert-error (decode-certificate (encode-handshake 11 #u8(0 0 0 6 0 0 1 1 0 1)))))
61 (test "CertificateRequest requires a nonempty extension block"
62 (assert-error (encode-certificate-request #u8() '()))
63 (assert-error (decode-certificate-request (encode-handshake 13 #u8(0 0 0)))))
64 (test "SHA256 Finished has an exact length and message type"
65 (assert-error (encode-finished (make-bytevector 31)))
66 (assert-error (decode-finished (encode-handshake 20 (make-bytevector 31))))
67 (assert-error (decode-finished (encode-handshake 20 (make-bytevector 33))))
68 (assert-error (decode-finished (fixture 'CertificateVerify))))
69 (test "all authentication message truncations are rejected"
70 (for-each
71 (lambda (pair)
72 (let ((wire (car pair)) (decode (cdr pair)))
73 (do ((n 0 (+ n 1))) ((= n (bytevector-length wire)))
74 (assert-error (decode (bytevector-copy wire 0 n))))))
75 (list (cons (fixture 'EncryptedExtensions) decode-encrypted-extensions)
76 (cons (fixture 'Certificate) decode-certificate)
77 (cons (fixture 'CertificateVerify) decode-certificate-verify)
78 (cons (fixture 'ServerFinished) decode-finished)
79 (cons (handshake-fixture 'certificate-request) decode-certificate-request))))
80 (test "authenticator comparison rejects every changed byte"
81 (let ((value (fixture 'server-verify)))
82 (assert-true (bytes-timing-safe=? value (bytevector-copy value)))
83 (do ((i 0 (+ i 1))) ((= i (bytevector-length value)))
84 (assert-false (bytes-timing-safe=? value (flip-byte value i))))
85 (assert-false (bytes-timing-safe=? value #u8()))
86 (assert-false (bytes-timing-safe=? value "not bytes")))
87 (assert-true (bytes-timing-safe=? #u8() #u8()))))
89(test-group "TLS handshake transcript"
90 (test "auth-vector: RFC8448 simple Hello transcript hash"
91 (let ((transcript (make-tls-transcript)))
92 (tls-transcript-add! transcript (fixture 'ClientHello))
93 (tls-transcript-add! transcript (fixture 'ServerHello))
94 (check-authentication-wire (fixture 'client-hs-hash) (tls-transcript-hash transcript))))
95 (test "auth-vector: RFC8448 HRR Hello transcript hash"
96 (let ((transcript (make-tls-transcript)))
97 (tls-transcript-add! transcript (handshake-fixture 'hrr-client-hello-1))
98 (tls-transcript-retry! transcript (handshake-fixture 'hrr-request))
99 (tls-transcript-add! transcript (handshake-fixture 'hrr-client-hello-2))
100 (tls-transcript-add! transcript (handshake-fixture 'hrr-server-hello))
101 (check-authentication-wire (handshake-fixture 'hrr-hello-hash)
102 (tls-transcript-hash transcript))))
103 (test "ordinary concatenation cannot substitute for HRR message_hash"
104 (assert-false
105 (bytes=? (handshake-fixture 'hrr-hello-hash)
106 (sha256-bytes
107 (bytes-append (handshake-fixture 'hrr-client-hello-1)
108 (handshake-fixture 'hrr-request) (handshake-fixture 'hrr-client-hello-2)
109 (handshake-fixture 'hrr-server-hello))))))
110 (test "HRR requires an existing ClientHello and the dedicated transformation"
111 (let ((transcript (make-tls-transcript)))
112 (assert-error (tls-transcript-retry! transcript (handshake-fixture 'hrr-request)))
113 (tls-transcript-add! transcript (handshake-fixture 'hrr-client-hello-1))
114 (assert-error (tls-transcript-add! transcript (handshake-fixture 'hrr-request)))
115 (tls-transcript-retry! transcript (handshake-fixture 'hrr-request))
116 (assert-error (tls-transcript-retry! transcript (handshake-fixture 'hrr-request)))))
117 (test "synthetic or malformed wire messages cannot be injected"
118 (let ((transcript (make-tls-transcript)))
119 (assert-error (tls-transcript-add! transcript (fixture 'ServerFinished)))
120 (tls-transcript-add! transcript (fixture 'ClientHello))
121 (assert-error (tls-transcript-add! transcript (encode-handshake 254 (make-bytevector 32))))
122 (assert-error (tls-transcript-add! transcript #u8(20 0 0 1)))))
123 (test "transcript message cap rejects input without changing the hash"
124 (let ((transcript (make-tls-transcript)))
125 (tls-transcript-add! transcript (fixture 'ClientHello))
126 (do ((i 1 (+ i 1))) ((= i 64))
127 (tls-transcript-add! transcript (encode-handshake 20 #u8())))
128 (let ((before (tls-transcript-hash transcript)))
129 (assert-error (tls-transcript-add! transcript (encode-handshake 20 #u8())))
130 (assert-equal before (tls-transcript-hash transcript)))))
131 (test "transcript byte cap rejects input without changing the hash"
132 (let ((transcript (make-tls-transcript))
133 (large (encode-handshake 11 (make-bytevector tls-handshake-limit))))
134 (tls-transcript-add! transcript (fixture 'ClientHello))
135 (do ((i 0 (+ i 1))) ((= i 3))
136 (tls-transcript-add! transcript large))
137 (let ((before (tls-transcript-hash transcript)))
138 (assert-error (tls-transcript-add! transcript large))
139 (assert-equal before (tls-transcript-hash transcript))))))