AtlatestRepositorysigil-quic
sigil-quic / tree / scriptsextract-handshake-vectors.mjs
1
// Extract RFC 8448 sections 5 and 6 code-component fixtures; see LICENSE.2
// Usage: node scripts/extract-handshake-vectors.mjs rfc8448.txt3
import { readFileSync, writeFileSync } from 'node:fs';4
const source = readFileSync(process.argv[2], 'utf8')5
.replace(/^Thomson.*\n|^RFC 8448.*\n|\f/gm, '')6
.replace(/\n\s*\n/g, '\n');7
const retry = source.split('5. HelloRetryRequest').at(-1).split('6. Client Authentication')[0];8
const authentication = source.split('6. Client Authentication').at(-1).split('7. Compatibility Mode')[0];9
function field(text, label, occurrence = 0) {10
const escaped = label.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');11
const pattern = new RegExp(`${escaped} \\((\\d+) octets\\):[ \\t]*((?:[0-9a-f]{2}(?:\\s+|$))+)`, 'g');12
const match = [...text.matchAll(pattern)][occurrence];13
if (!match) throw new Error(`Missing ${label} occurrence ${occurrence}`);14
const hex = match[2].replace(/\s/g, '');15
if (hex.length !== 2 * Number(match[1])) throw new Error(`Wrong octet count for ${label}`);16
return hex;17
}18
const derivationStart = retry.indexOf('derive secret "tls13 c hs traffic"');19
if (derivationStart < 0) throw new Error('Missing HRR handshake-traffic derivation');20
const derivation = retry.slice(derivationStart);21
const data = new Map([22
['hrr-client-hello-1', field(retry, 'ClientHello', 0)],23
['hrr-request', field(retry, 'ServerHello', 0)],24
['hrr-client-hello-2', field(retry, 'ClientHello', 1)],25
['hrr-server-hello', field(retry, 'ServerHello', 1)],26
['hrr-hello-hash', field(derivation, 'hash')],27
['certificate-request', field(authentication, 'CertificateRequest')],28
]);29
let output = `;;; RFC 8448 sections 5 (HRR) and 6 (client authentication) code components.30
;;; Source: https://www.rfc-editor.org/rfc/rfc8448.txt31
;;; Extract with scripts/extract-handshake-vectors.mjs; see LICENSE.32
(define-library (sigil quic tls vectors)33
(import (sigil quic bytes))34
(export handshake-fixture)35
(begin36
(define (handshake-fixture name)37
(hex->bytes38
(case name39
`;40
for (const [name, hex] of data) {41
output += ` ((${name})\n (string-append\n`;42
for (let i = 0; i < hex.length; i += 64) output += ` "${hex.slice(i, i + 64)}"\n`;43
output += ' ))\n';44
}45
output += ' (else (error "unknown handshake vector" name)))))))\n';46
writeFileSync('src/sigil/quic/tls/vectors.sgl', output);47
process.stdout.write(`Extracted ${data.size} handshake fixtures with checked octet counts\n`);