forked from mtusi2026/mtusi2026lab7
140 lines
4.2 KiB
TypeScript
140 lines
4.2 KiB
TypeScript
import { mkdir, readFile, chmod, copyFile } from "node:fs/promises";
|
|
import { resolve } from "node:path";
|
|
import { run, privateWrite, requireThat } from "./runtime";
|
|
import { validateLeaf } from "./security-guards";
|
|
export function caConfig(base: string) {
|
|
return `[ca]\ndefault_ca=CA\n[CA]\ndatabase=${base}/index\nnew_certs_dir=${base}/issued\ncertificate=${base}/ca.crt\nprivate_key=${base}/ca.key\nserial=${base}/serial\ncrlnumber=${base}/crlnumber\ndefault_md=sha256\ndefault_days=2\ndefault_crl_days=1\nunique_subject=no\npolicy=policy\n[policy]\ncommonName=supplied\n`;
|
|
}
|
|
export async function createCA(base: string, passphrase: string) {
|
|
requireThat(passphrase.length >= 20, "OPERATOR_PASSWORD_TOO_SHORT");
|
|
await mkdir(resolve(base, "issued"), { recursive: true, mode: 0o700 });
|
|
await privateWrite(resolve(base, "index"), "");
|
|
await privateWrite(resolve(base, "serial"), "1000\n");
|
|
await privateWrite(resolve(base, "crlnumber"), "1000\n");
|
|
await privateWrite(resolve(base, "ca.cnf"), caConfig(base));
|
|
await run(
|
|
[
|
|
"openssl",
|
|
"req",
|
|
"-x509",
|
|
"-newkey",
|
|
"rsa:3072",
|
|
"-sha256",
|
|
"-days",
|
|
"30",
|
|
"-subj",
|
|
"/CN=VP7 private lab CA",
|
|
"-addext",
|
|
"basicConstraints=critical,CA:TRUE,pathlen:0",
|
|
"-addext",
|
|
"keyUsage=critical,keyCertSign,cRLSign",
|
|
"-keyout",
|
|
resolve(base, "ca.key"),
|
|
"-out",
|
|
resolve(base, "ca.crt"),
|
|
"-passout",
|
|
"stdin",
|
|
],
|
|
{ input: passphrase + "\n" },
|
|
);
|
|
await chmod(resolve(base, "ca.key"), 0o600);
|
|
await refreshCRL(base, passphrase);
|
|
}
|
|
export async function refreshCRL(base: string, passphrase: string) {
|
|
await run(
|
|
[
|
|
"openssl",
|
|
"ca",
|
|
"-batch",
|
|
"-config",
|
|
resolve(base, "ca.cnf"),
|
|
"-gencrl",
|
|
"-out",
|
|
resolve(base, "ca.crl"),
|
|
"-passin",
|
|
"stdin",
|
|
],
|
|
{ input: passphrase + "\n" },
|
|
);
|
|
}
|
|
export async function issue(
|
|
base: string,
|
|
destination: string,
|
|
identity: string,
|
|
passphrase: string,
|
|
dates?: { start: string; end: string },
|
|
) {
|
|
requireThat(
|
|
/^[a-z0-9-]{1,48}$/.test(identity),
|
|
"INVALID_CERTIFICATE_IDENTITY",
|
|
);
|
|
await mkdir(destination, { recursive: true, mode: 0o700 });
|
|
const extensions = resolve(destination, "leaf.cnf");
|
|
await privateWrite(
|
|
extensions,
|
|
`[leaf]\nbasicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature,keyEncipherment\nextendedKeyUsage=serverAuth,clientAuth\nsubjectAltName=DNS:${identity}${["lb", "openbao", "grafana"].includes(identity) ? ",DNS:localhost,IP:127.0.0.1" : ""}\nauthorityKeyIdentifier=keyid,issuer\nsubjectKeyIdentifier=hash\n`,
|
|
);
|
|
await run([
|
|
"openssl",
|
|
"req",
|
|
"-new",
|
|
"-newkey",
|
|
"rsa:2048",
|
|
"-noenc",
|
|
"-subj",
|
|
`/CN=${identity}`,
|
|
"-keyout",
|
|
resolve(destination, "key.pem"),
|
|
"-out",
|
|
resolve(destination, "request.csr"),
|
|
]);
|
|
await run(
|
|
[
|
|
"openssl",
|
|
"ca",
|
|
"-batch",
|
|
"-notext",
|
|
"-config",
|
|
resolve(base, "ca.cnf"),
|
|
"-extensions",
|
|
"leaf",
|
|
"-extfile",
|
|
extensions,
|
|
"-in",
|
|
resolve(destination, "request.csr"),
|
|
"-out",
|
|
resolve(destination, "cert.pem"),
|
|
"-passin",
|
|
"stdin",
|
|
...(dates ? ["-startdate", dates.start, "-enddate", dates.end] : []),
|
|
],
|
|
{ input: passphrase + "\n" },
|
|
);
|
|
const cert = await readFile(resolve(destination, "cert.pem"), "utf8"),
|
|
key = await readFile(resolve(destination, "key.pem"), "utf8"),
|
|
ca = await readFile(resolve(base, "ca.crt"), "utf8");
|
|
if (!dates) validateLeaf(cert, key, ca, identity);
|
|
await privateWrite(resolve(destination, "server.pem"), cert + key);
|
|
await copyFile(resolve(base, "ca.crt"), resolve(destination, "ca.crt"));
|
|
await copyFile(resolve(base, "ca.crl"), resolve(destination, "ca.crl"));
|
|
for (const file of ["key.pem", "server.pem", "request.csr", "leaf.cnf"])
|
|
await chmod(resolve(destination, file), 0o600);
|
|
}
|
|
export async function revoke(base: string, cert: string, passphrase: string) {
|
|
await run(
|
|
[
|
|
"openssl",
|
|
"ca",
|
|
"-batch",
|
|
"-config",
|
|
resolve(base, "ca.cnf"),
|
|
"-revoke",
|
|
cert,
|
|
"-passin",
|
|
"stdin",
|
|
],
|
|
{ input: passphrase + "\n" },
|
|
);
|
|
await refreshCRL(base, passphrase);
|
|
}
|