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