2022-06-19 00:25:21 +00:00
|
|
|
import path from "path";
|
|
|
|
import fs from "fs";
|
|
|
|
import crypto from "crypto";
|
|
|
|
import {md, pki} from "node-forge";
|
|
|
|
import log from "../log";
|
|
|
|
import Config from "../config";
|
|
|
|
|
|
|
|
export default {
|
2020-03-30 20:15:32 +00:00
|
|
|
get,
|
|
|
|
remove,
|
|
|
|
};
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export type ClientCertificateType = {
|
|
|
|
private_key: string;
|
|
|
|
certificate: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
function get(uuid: string): ClientCertificateType | null {
|
2022-05-01 19:12:39 +00:00
|
|
|
if (Config.values.public) {
|
2020-03-30 20:15:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
const folderPath = Config.getClientCertificatesPath();
|
2020-03-30 20:15:32 +00:00
|
|
|
const paths = getPaths(folderPath, uuid);
|
|
|
|
|
|
|
|
if (!fs.existsSync(paths.privateKeyPath) || !fs.existsSync(paths.certificatePath)) {
|
|
|
|
return generateAndWrite(folderPath, paths);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return {
|
|
|
|
private_key: fs.readFileSync(paths.privateKeyPath, "utf-8"),
|
|
|
|
certificate: fs.readFileSync(paths.certificatePath, "utf-8"),
|
2022-06-19 00:25:21 +00:00
|
|
|
} as ClientCertificateType;
|
|
|
|
} catch (e: any) {
|
2022-02-04 22:06:53 +00:00
|
|
|
log.error("Unable to get certificate", e);
|
2020-03-30 20:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function remove(uuid: string) {
|
2022-05-01 19:12:39 +00:00
|
|
|
if (Config.values.public) {
|
2020-03-30 20:15:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
const paths = getPaths(Config.getClientCertificatesPath(), uuid);
|
2020-03-30 20:15:32 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (fs.existsSync(paths.privateKeyPath)) {
|
|
|
|
fs.unlinkSync(paths.privateKeyPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.existsSync(paths.certificatePath)) {
|
|
|
|
fs.unlinkSync(paths.certificatePath);
|
|
|
|
}
|
2022-06-19 00:25:21 +00:00
|
|
|
} catch (e: any) {
|
2020-03-30 20:15:32 +00:00
|
|
|
log.error("Unable to remove certificate", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function generateAndWrite(folderPath: string, paths: {privateKeyPath: any; certificatePath: any}) {
|
2020-03-30 20:15:32 +00:00
|
|
|
const certificate = generate();
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.mkdirSync(folderPath, {recursive: true});
|
|
|
|
|
|
|
|
fs.writeFileSync(paths.privateKeyPath, certificate.private_key, {
|
|
|
|
mode: 0o600,
|
|
|
|
});
|
|
|
|
fs.writeFileSync(paths.certificatePath, certificate.certificate, {
|
|
|
|
mode: 0o600,
|
|
|
|
});
|
|
|
|
|
|
|
|
return certificate;
|
2022-06-19 00:25:21 +00:00
|
|
|
} catch (e: any) {
|
|
|
|
log.error("Unable to write certificate", String(e));
|
2020-03-30 20:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generate() {
|
|
|
|
const keys = pki.rsa.generateKeyPair(2048);
|
|
|
|
const cert = pki.createCertificate();
|
|
|
|
|
|
|
|
cert.publicKey = keys.publicKey;
|
|
|
|
cert.serialNumber = crypto.randomBytes(16).toString("hex").toUpperCase();
|
|
|
|
|
|
|
|
// Set notBefore a day earlier just in case the time between
|
|
|
|
// the client and server is not perfectly in sync
|
|
|
|
cert.validity.notBefore = new Date();
|
|
|
|
cert.validity.notBefore.setDate(cert.validity.notBefore.getDate() - 1);
|
|
|
|
|
|
|
|
// Set notAfter 100 years into the future just in case
|
|
|
|
// the server actually validates this field
|
|
|
|
cert.validity.notAfter = new Date();
|
2022-06-19 00:25:21 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
2020-03-30 20:15:32 +00:00
|
|
|
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 100);
|
|
|
|
|
|
|
|
const attrs = [
|
|
|
|
{
|
|
|
|
name: "commonName",
|
2023-10-09 10:28:14 +00:00
|
|
|
value: "Hard Lounge IRC Client",
|
2020-03-30 20:15:32 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
cert.setSubject(attrs);
|
|
|
|
cert.setIssuer(attrs);
|
|
|
|
|
|
|
|
// Set extensions that indicate this is a client authentication certificate
|
|
|
|
cert.setExtensions([
|
|
|
|
{
|
|
|
|
name: "extKeyUsage",
|
|
|
|
clientAuth: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nsCertType",
|
|
|
|
client: true,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Sign this certificate with a SHA256 signature
|
|
|
|
cert.sign(keys.privateKey, md.sha256.create());
|
|
|
|
|
|
|
|
const pem = {
|
|
|
|
private_key: pki.privateKeyToPem(keys.privateKey),
|
|
|
|
certificate: pki.certificateToPem(cert),
|
2022-06-19 00:25:21 +00:00
|
|
|
} as ClientCertificateType;
|
2020-03-30 20:15:32 +00:00
|
|
|
|
|
|
|
return pem;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function getPaths(folderPath: string, uuid: string) {
|
2020-03-30 20:15:32 +00:00
|
|
|
return {
|
|
|
|
privateKeyPath: path.join(folderPath, `${uuid}.pem`),
|
|
|
|
certificatePath: path.join(folderPath, `${uuid}.crt`),
|
|
|
|
};
|
|
|
|
}
|