AtlatestRepositorysigil-quic

sigil-quic / tree / tools / hqprepare-fixtures.mjs

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