"use strict";
/**
 * @deprecated Moved to packages/cactus-test-tooling/src/main/typescript/pki/self-signed-pki-generator.ts
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelfSignedPkiGenerator = void 0;
const node_forge_1 = require("node-forge");
const uuid_1 = require("uuid");
const cactus_common_1 = require("@hyperledger/cactus-common");
/**
 * @deprecated Moved to \@hyperledger/cactus-test-tooling
 *
 * Do not use this for anything in a production deployment. It's meant as a helper
 * class for development and testing purposes (enhancing developer experience).
 *
 * Secure by default is one of our core design principles and it's much harder to
 * enforce/implement that it sounds when you also do not want to ruin the ease
 * of use of the software. Dynamically pre-provisioning PKI is notoriously
 * complicated and error prone to the average user/developer.
 *
 */
class SelfSignedPkiGenerator {
    create(commonName, parent) {
        const keyPair = node_forge_1.pki.rsa.generateKeyPair(4096);
        const privateKeyPem = node_forge_1.pki.privateKeyToPem(keyPair.privateKey);
        const certificate = node_forge_1.pki.createCertificate();
        this.configureCertificateParameters(keyPair, certificate, commonName);
        if (parent) {
            certificate.setIssuer(parent.certificate.subject.attributes);
            certificate.publicKey = keyPair.publicKey;
            // certificate.privateKey = keyPair.privateKey;
            certificate.sign(parent.keyPair.privateKey, node_forge_1.md.sha512.create());
            if (!parent.certificate.verify(certificate)) {
                throw new Error("Could not verify newly generated certificate");
            }
        }
        else {
            certificate.sign(keyPair.privateKey, node_forge_1.md.sha512.create());
        }
        const certificatePem = node_forge_1.pki.certificateToPem(certificate);
        return { keyPair, certificate, certificatePem, privateKeyPem };
    }
    configureCertificateParameters(keyPair, certificate, commonName) {
        // 20 octets max for serial numbers of certs as per the standard
        const serialNumber = cactus_common_1.Strings.replaceAll((0, uuid_1.v4)(), "-", "").substring(0, 19);
        certificate.serialNumber = serialNumber;
        certificate.publicKey = keyPair.publicKey;
        certificate.privateKey = keyPair.privateKey;
        certificate.validity.notBefore = new Date();
        certificate.validity.notAfter = new Date();
        const nextYear = certificate.validity.notBefore.getFullYear() + 1;
        certificate.validity.notAfter.setFullYear(nextYear);
        const certificateFields = [
            {
                shortName: "CN",
                name: "commonName",
                value: commonName,
            },
            {
                name: "countryName",
                value: "Universe",
            },
            {
                shortName: "ST",
                value: "Milky Way",
            },
            {
                shortName: "L",
                name: "localityName",
                value: "Planet Earth",
            },
            {
                shortName: "O",
                name: "organizationName",
                value: "Hyperledger",
            },
            {
                shortName: "OU",
                value: "Cactus",
            },
            {
                name: "unstructuredName",
                value: "Cactus Dummy Self Signed Certificates",
            },
        ];
        certificate.setSubject(certificateFields);
        certificate.setIssuer(certificateFields);
        certificate.setExtensions([
            {
                name: "basicConstraints",
                cA: true,
            },
            {
                name: "keyUsage",
                keyCertSign: true,
                digitalSignature: true,
                nonRepudiation: true,
                keyEncipherment: true,
                dataEncipherment: true,
            },
            {
                name: "extKeyUsage",
                serverAuth: true,
                clientAuth: true,
                codeSigning: true,
                emailProtection: true,
                timeStamping: true,
            },
            {
                name: "nsCertType",
                client: true,
                server: true,
                email: true,
                objsign: true,
                sslCA: true,
                emailCA: true,
                objCA: true,
            },
            {
                name: "subjectAltName",
                altNames: [
                    {
                        type: 7, // IP
                        ip: "127.0.0.1",
                    },
                ],
            },
            {
                name: "subjectKeyIdentifier",
            },
        ]);
        return certificate;
    }
}
exports.SelfSignedPkiGenerator = SelfSignedPkiGenerator;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2VsZi1zaWduZWQtcGtpLWdlbmVyYXRvci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uL3NyYy9tYWluL3R5cGVzY3JpcHQvY29uZmlnL3NlbGYtc2lnbmVkLXBraS1nZW5lcmF0b3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOztHQUVHOzs7QUFFSCwyQ0FBcUM7QUFDckMsK0JBQW9DO0FBQ3BDLDhEQUFxRDtBQStCckQ7Ozs7Ozs7Ozs7O0dBV0c7QUFDSCxNQUFhLHNCQUFzQjtJ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