"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PkiService = exports.ECPCurve = exports.CertificateFormat = exports.CertificateKeyType = void 0;
const { CryptoPrimitives } = require("../../bindings/sgx-native/build/Release/sgx_native.node");
var CertificateKeyType;
(function (CertificateKeyType) {
    CertificateKeyType["RSA"] = "RSA";
    CertificateKeyType["ECP"] = "ECP";
})(CertificateKeyType || (exports.CertificateKeyType = CertificateKeyType = {}));
var CertificateFormat;
(function (CertificateFormat) {
    CertificateFormat["PEM"] = "PEM";
    CertificateFormat["DER"] = "DER";
})(CertificateFormat || (exports.CertificateFormat = CertificateFormat = {}));
var ECPCurve;
(function (ECPCurve) {
    ECPCurve["SECP192R1"] = "SECP192R1"; /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */
    ECPCurve["SECP224R1"] = "SECP224R1"; /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */
    ECPCurve["SECP256R1"] = "SECP256R1"; /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */
    ECPCurve["SECP384R1"] = "SECP384R1"; /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */
    ECPCurve["SECP521R1"] = "SECP521R1"; /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */
    ECPCurve["BP256R1"] = "BP256R1"; /*!< Domain parameters for 256-bit Brainpool curve. */
    ECPCurve["BP384R1"] = "BP384R1"; /*!< Domain parameters for 384-bit Brainpool curve. */
    ECPCurve["BP512R1"] = "BP512R1"; /*!< Domain parameters for 512-bit Brainpool curve. */
    // @TODO: MBEDTLS NOT SUPPORT EXRPORT CURVE25519 = "CURVE25519", /*!< Domain parameters for Curve25519. */
    ECPCurve["SECP192K1"] = "SECP192K1"; /*!< Domain parameters for 192-bit "Koblitz" curve. */
    ECPCurve["SECP224K1"] = "SECP224K1"; /*!< Domain parameters for 224-bit "Koblitz" curve. */
    ECPCurve["SECP256K1"] = "SECP256K1"; /*!< Domain parameters for 256-bit "Koblitz" curve. */
    // @TODO: MBEDTLS NOT SUPPORT EXRPORT    CURVE448 = "CURVE448",     /*!< Domain parameters for Curve448. */
})(ECPCurve || (exports.ECPCurve = ECPCurve = {}));
class PkiService {
    constructor() {
        this.cryptoPrimitives = new CryptoPrimitives();
    }
    async validateChain(certs, format = CertificateFormat.PEM) {
        if (format === CertificateFormat.PEM) {
            this.cryptoPrimitives.VerifyCertificateChain(certs);
        }
        else {
            this.cryptoPrimitives.VerifyCertificateChain(certs, format.toString());
        }
        return true;
    }
    async validateHostname(certs, hostnameOrIp, format = CertificateFormat.PEM) {
        if (format === CertificateFormat.PEM) {
            return this.cryptoPrimitives.CheckHostnameOrIP(hostnameOrIp, certs);
        }
        return this.cryptoPrimitives.CheckHostnameOrIP(hostnameOrIp, certs, format.toString());
    }
    async generateTlsCertificate(params = {}) {
        const subject = {
            commonName: params.subject?.commonName ?? "localhost",
            countryName: params.subject?.countryName ?? "US",
            state: params.subject?.state ?? "New York",
            localityName: params.subject?.localityName ?? "New York",
            organizationName: params.subject?.organizationName ?? "SuperProtocol",
            organizationUnit: params.subject?.organizationUnit ?? "TEE",
        };
        const format = params.format ?? CertificateFormat.PEM;
        const keyType = params.keyType ?? CertificateKeyType.RSA;
        let rsaKeyBits;
        let ecpCurve;
        if (keyType === CertificateKeyType.RSA) {
            rsaKeyBits = params.rsaKeyBits ?? 3072;
        }
        else if (keyType === CertificateKeyType.ECP) {
            ecpCurve = params.ecpCurve ?? ECPCurve.SECP384R1;
        }
        const withQuote = params.withQuote ?? false;
        const serialNumber = params.serialNumber ?? "01";
        const days = params.days ?? 365;
        const dnsNames = params.dnsNames ?? [];
        const ips = params.ips ?? [];
        return this.cryptoPrimitives.GenerateTlsCertificate({
            format,
            subject,
            keyType,
            withQuote,
            rsaKeyBits,
            ecpCurve,
            serialNumber,
            days,
            dnsNames,
            ips,
        });
    }
}
exports.PkiService = PkiService;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozL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