AtlatestRepositorysigil-http3

sigil-http3 / tree / scriptsprepare-certificate.mjs

1// Run only on Linux. Reuse the pinned QUIC fixture converter for raw keys.
2import { readFileSync, writeFileSync } from 'node:fs';
3import { join } from 'node:path';
4import { createHash, X509Certificate } from 'node:crypto';
5import { spawnSync } from 'node:child_process';
6
7const directory = process.argv[2];
8if (!directory) throw new Error('Expected certificate directory');
9const converted = spawnSync(process.execPath,
10 ['.deps/sigil-quic/tools/hq/prepare-fixtures.mjs', directory], { stdio: 'inherit' });
11if (converted.status !== 0) throw new Error('Pinned QUIC certificate conversion failed');
12const certificate = new X509Certificate(readFileSync(join(directory, 'cert.pem')));
13const lifetime = Date.parse(certificate.validTo) - Date.parse(certificate.validFrom);
14if (lifetime <= 0 || lifetime > 14 * 86400000 || Date.parse(certificate.validTo) <= Date.now()) {
15 throw new Error('WebTransport certificate lifetime must be at most 14 days and currently valid');
17const hash = createHash('sha256').update(certificate.raw).digest();
18const receipt = {
19 sha256: hash.toString('hex'), bytes: [...hash], validFrom: certificate.validFrom,
20 validTo: certificate.validTo, curve: certificate.publicKey.export({ format: 'jwk' }).crv,
21};
22if (receipt.curve !== 'P-256') throw new Error('Expected P-256 certificate');
23writeFileSync(join(directory, 'certificate.json'), JSON.stringify(receipt, null, 2) + '\n');
24console.log(`RESULT certificate-sha256=${receipt.sha256} valid-to=${receipt.validTo}`);