// The certificate HUD is served with in front of a PDS of its own (the e2e // world, `just local`), at the harness's hostname for it, `HOST`: signed // by a certificate authority of this checkout's, made once and kept in // .local/tls/ (not in git), so a simulator or a keychain told to trust it // once trusts every run's. HUD's certificate is made once too, and again // when it nears its end; Apple's limits are kept (a server certificate of // at most 825 days, for serverAuth, its name in its subjectAltName). // // import { HOST, certificates } from './tls.mjs'; // const { ca, cert, key } = certificates(); import { execFileSync } from 'node:child_process'; import { existsSync, mkdirSync, writeFileSync } from 'node:fs'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; /// HUD's hostname in front of the harness's PDS: `.internal` is for /// private use, never a public name, and the PDS takes it as a client's /// (it refuses local TLDs such as .test and .localhost). export const HOST = 'hud.harness.internal'; const dir = resolve(dirname(fileURLToPath(import.meta.url)), '../.local/tls'); const openssl = (...args) => execFileSync('openssl', args, { stdio: ['ignore', 'pipe', 'pipe'] }); /// Whether the certificate at `path` is good for a week more. const lasting = (path) => { try { openssl('x509', '-in', path, '-noout', '-checkend', String(7 * 86400)); return true; } catch { return false; } }; /// Whether the authority at `path` may sign for HUD's hostname alone: one /// made before it was constrained is made again. const constrained = (path) => { try { return openssl('x509', '-in', path, '-noout', '-ext', 'nameConstraints').toString().includes(`DNS:${HOST}`); } catch { return false; } }; /// The authority's and HUD's certificates, and HUD's key, made where /// missing or ending: `{ ca, cert, key }`, each a path. The authority goes /// into simulators' keychains and its key sits unencrypted here, so it may /// sign for HUD's hostname alone (a name constraint): its key mints no /// certificate a simulator trusts for any other name. export function certificates() { mkdirSync(dir, { recursive: true }); const ca = join(dir, 'ca.pem'); const caKey = join(dir, 'ca-key.pem'); const cert = join(dir, 'hud.pem'); const key = join(dir, 'hud-key.pem'); const fresh = !existsSync(ca) || !lasting(ca) || !constrained(ca); if (fresh) { openssl( 'req', '-x509', '-newkey', 'ec', '-pkeyopt', 'ec_paramgen_curve:prime256v1', '-nodes', '-keyout', caKey, '-out', ca, '-days', '3650', '-subj', '/CN=HUD harness local authority', '-addext', 'basicConstraints=critical,CA:TRUE', '-addext', 'keyUsage=critical,keyCertSign,cRLSign', '-addext', `nameConstraints=critical,permitted;DNS:${HOST}`, ); } if (fresh || !existsSync(cert) || !lasting(cert)) { const request = join(dir, 'hud.csr'); const extensions = join(dir, 'hud.ext'); writeFileSync( extensions, [ 'basicConstraints=critical,CA:FALSE', 'keyUsage=critical,digitalSignature', 'extendedKeyUsage=serverAuth', `subjectAltName=DNS:${HOST}`, ].join('\n'), ); openssl('req', '-newkey', 'ec', '-pkeyopt', 'ec_paramgen_curve:prime256v1', '-nodes', '-keyout', key, '-out', request, '-subj', `/CN=${HOST}`); openssl('x509', '-req', '-in', request, '-CA', ca, '-CAkey', caKey, '-CAcreateserial', '-out', cert, '-days', '397', '-sha256', '-extfile', extensions); } return { ca, cert, key }; }