AtlatestRepositorysigil-quic
sigil-quic / tree / tools / hqprepare-fixtures.mjs
1
// Test credentials are generated on the test machine. Never run this as part2
// of the library build; neither private key files nor peer libraries ship.3
import { readFileSync, writeFileSync } from 'node:fs';4
import { join } from 'node:path';5
import { createPrivateKey, X509Certificate } from 'node:crypto';7
const directory = process.argv[2];8
if (!directory) throw new Error('Expected the test credentials directory');9
const certificate = new X509Certificate(readFileSync(join(directory, 'cert.pem')));10
const privateKey = createPrivateKey(readFileSync(join(directory, 'key.pem')));11
const secret = privateKey.export({ format: 'jwk' });12
const publicKey = certificate.publicKey.export({ format: 'jwk' });13
if (secret.crv !== 'P-256' || publicKey.crv !== 'P-256' ||14
secret.x !== publicKey.x || secret.y !== publicKey.y) {15
throw new Error('Certificate and private P-256 key must match');16
}17
const scalar = Buffer.from(secret.d, 'base64url');18
const rawPublic = Buffer.concat([19
Buffer.from([4]), Buffer.from(publicKey.x, 'base64url'), Buffer.from(publicKey.y, 'base64url'),20
]);21
if (scalar.length !== 32 || rawPublic.length !== 65) throw new Error('Invalid P-256 dimensions');22
writeFileSync(join(directory, 'cert.der'), certificate.raw);23
writeFileSync(join(directory, 'private.raw'), scalar, { mode: 0o600 });24
writeFileSync(join(directory, 'public.raw'), rawPublic);25
scalar.fill(0);