AtlatestRepositorysigil-quic

sigil-quic / tree / scriptssecret-cleanup-tests.sgl

1(import (sigil test)
2 (sigil quic tls)
3 (sigil quic tests secret spy))
4
5;; Explicitly run against inject-secret-spy.mjs variants. The spy retains
6;; actual primitive-input references and a nonzero flag, without secret copies.
7(define (cleanup-wipe! bytes)
8 (when (bytevector? bytes)
9 (do ((i 0 (+ i 1))) ((= i (bytevector-length bytes)))
10 (bytevector-u8-set! bytes i 0))))
12(define (cleanup-zero? bytes)
13 (let loop ((i 0))
14 (or (= i (bytevector-length bytes))
15 (and (= (bytevector-u8-ref bytes i) 0) (loop (+ i 1))))))
17(define (cleanup-with-state procedure)
18 (let* ((inputs (map (lambda (byte) (make-bytevector 32 byte)) '(7 11 13 17)))
19 (owned inputs))
20 (define (own bytes)
21 (set! owned (cons bytes owned))
22 bytes)
23 (dynamic-wind
24 secret-spy-reset!
25 (lambda () (procedure inputs own))
26 (lambda ()
27 ;; Cleanup happens after assertions, including failed baseline ones.
28 (for-each (lambda (entry) (cleanup-wipe! (vector-ref entry 1)))
29 (secret-spy-inputs))
30 (secret-spy-reset!)
31 (for-each cleanup-wipe! owned)))))
33(define (cleanup-kind operation)
34 (if (eq? operation 'finished) 'hmac 'extract))
36(define (cleanup-call operation inputs)
37 (case operation
38 ((handshake) (handshake-secret (car inputs) (cadr inputs)))
39 ((master) (master-secret (caddr inputs)))
40 ((finished) (finished-verify-data (caddr inputs) (cadddr inputs)))))
42(define (cleanup-failure! operation inputs)
43 (let ((failure
44 (guard (exception (else exception))
45 (cleanup-call operation inputs)
46 #f)))
47 (assert-true (error-object? failure))
48 (when (error-object? failure)
49 (assert-equal "QL7 injected primitive failure" (exception-message failure))
50 (assert-equal (list (cleanup-kind operation)) (error-object-irritants failure)))))
52(define (cleanup-check! operation fail?)
53 (cleanup-with-state
54 (lambda (inputs own)
55 (if fail?
56 (begin
57 (secret-spy-fail! (cleanup-kind operation))
58 (cleanup-failure! operation inputs))
59 (let ((result (own (cleanup-call operation inputs))))
60 (assert-equal 32 (bytevector-length result))
61 (assert-false (cleanup-zero? result))))
62 (let ((entries (secret-spy-inputs)))
63 (assert-equal 1 (length entries))
64 (assert-equal (cleanup-kind operation) (vector-ref (car entries) 0))
65 (assert-true (vector-ref (car entries) 2))
66 (assert-equal 32 (bytevector-length (vector-ref (car entries) 1)))
67 (assert-true (cleanup-zero? (vector-ref (car entries) 1)))))))
69(test-group "Reachable TLS schedule secret cleanup"
70 (test "ql7-schedule: handshake derived salt clears on return"
71 (cleanup-check! 'handshake #f))
72 (test "ql7-schedule: handshake derived salt clears on exception"
73 (cleanup-check! 'handshake #t))
74 (test "ql7-schedule: master derived salt clears on return"
75 (cleanup-check! 'master #f))
76 (test "ql7-schedule: master derived salt clears on exception"
77 (cleanup-check! 'master #t))
78 (test "ql7-schedule: Finished HMAC key clears on return"
79 (cleanup-check! 'finished #f))
80 (test "ql7-schedule: Finished HMAC key clears on exception"
81 (cleanup-check! 'finished #t))
82 (test "ql7-schedule-control: returned bytes and borrowed inputs retain ownership"
83 (for-each
84 (lambda (operation)
85 (cleanup-with-state
86 (lambda (inputs own)
87 (let* ((copies (map (lambda (bytes) (own (bytevector-copy bytes))) inputs))
88 (result (own (cleanup-call operation inputs))))
89 (assert-equal copies inputs)
90 (assert-equal 32 (bytevector-length result))
91 (assert-false (cleanup-zero? result))))))
92 '(handshake master finished)))
93 (test "ql7-schedule-control: primitive exceptions preserve inputs and their marker"
94 (for-each
95 (lambda (operation)
96 (cleanup-with-state
97 (lambda (inputs own)
98 (let ((copies (map (lambda (bytes) (own (bytevector-copy bytes))) inputs)))
99 (secret-spy-fail! (cleanup-kind operation))
100 (cleanup-failure! operation inputs)
101 (assert-equal copies inputs)))))
102 '(handshake master finished))))