"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DcapQuoteVerifyService = void 0;
const { SgxAttestationVerifier } = require("../../bindings/sgx-native/build/Release/sgx_native.node");
const consts_1 = require("./consts");
const errors_1 = require("./errors");
class DcapQuoteVerifyService {
    constructor() {
        this.quoteVerifier = new SgxAttestationVerifier();
    }
    static checkErrorOrThrow(result) {
        if (result !== consts_1.DcapQuoteVerificationStatus.OK) {
            throw new errors_1.AttestationDcapQuoteVerifyServiceError(consts_1.DcapQuoteVerificationStatusMap[result]);
        }
    }
    async extractQuoteFromCert(pemOrDerCert) {
        if (!pemOrDerCert?.length) {
            throw new errors_1.AttestationDcapQuoteVerifyEnclaveError("empty certificate");
        }
        return this.quoteVerifier.extractQuoteFromCert(pemOrDerCert);
    }
    async validateQuoteVerifierEnclave(quote) {
        if (!quote?.length) {
            throw new errors_1.AttestationDcapQuoteVerifyEnclaveError("empty quote");
        }
        const verifyResult = this.quoteVerifier.verifyQuoteDcap(quote);
        if (verifyResult !== 0) {
            throw new errors_1.QuoteValidationError(verifyResult);
        }
    }
    async validateQuote({ quote, pemPckCertificate, pemIntermediateCrl, tcbInfoJson, qeIdentityJson, }) {
        DcapQuoteVerifyService.checkErrorOrThrow(this.quoteVerifier.verifyQuote(quote, pemPckCertificate, pemIntermediateCrl, tcbInfoJson, qeIdentityJson));
    }
    async validateEnclaveReport({ enclaveReport, enclaveIdentity, }) {
        DcapQuoteVerifyService.checkErrorOrThrow(this.quoteVerifier.verifyEnclaveReport(enclaveReport, enclaveIdentity));
    }
    libraryEnclaveGetVersion() {
        return this.quoteVerifier.enclaveGetVersion();
    }
    libraryVersion() {
        return this.quoteVerifier.getVersion();
    }
    async getQeCertificationData({ quote }) {
        const [status, pemCertChain, qeCertType] = this.quoteVerifier.getQECertificationData(quote);
        DcapQuoteVerifyService.checkErrorOrThrow(status);
        return {
            pemCertChain,
            qeCertType,
        };
    }
    async validatePckCertificate({ pemCertChain, pemRootCaCrl, pemIntermediateCaCrl, pemRootCaCertificate, expirationCheckDate, }) {
        DcapQuoteVerifyService.checkErrorOrThrow(this.quoteVerifier.verifyPCKCertificate(pemCertChain, [pemRootCaCrl, pemIntermediateCaCrl], pemRootCaCertificate, expirationCheckDate));
    }
    async validateTcbInfo({ tcbInfoJson, pemCertChain, pemRootCaCrl, pemRootCaCertificate, expirationCheckDate, }) {
        DcapQuoteVerifyService.checkErrorOrThrow(this.quoteVerifier.verifyTCBInfo(tcbInfoJson, pemCertChain, pemRootCaCrl, pemRootCaCertificate, expirationCheckDate));
    }
    async validateEnclaveIdentity({ enclaveIdentityJson, pemCertChain, pemRootCaCrl, pemRootCaCertificate, expirationCheckDate, }) {
        DcapQuoteVerifyService.checkErrorOrThrow(this.quoteVerifier.verifyEnclaveIdentity(enclaveIdentityJson, pemCertChain, pemRootCaCrl, pemRootCaCertificate, expirationCheckDate));
    }
    async validatePckRevocationList({ pemCrl, pemCaCertChain, pemTrustedRootCaCert, }) {
        DcapQuoteVerifyService.checkErrorOrThrow(this.quoteVerifier.verifyPCKRevocationList(pemCrl, pemCaCertChain, pemTrustedRootCaCert));
    }
}
exports.DcapQuoteVerifyService = DcapQuoteVerifyService;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGNhcC1xdW90ZS12ZXJpZnkuc2VydmljZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zZ3gtbmF0aXZlLW1vZHVsZS9kY2FwLXF1b3RlLXZlcmlmeS5zZXJ2aWNlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUFBLE1BQU0sRUFBRSxzQkFBc0IsRUFBRSxHQUFHLE9BQU8sQ0FBQyx5REFBeUQsQ0FBQyxDQUFDO0FBQ3RHLHFDQUF1RjtBQUN2RixxQ0FJa0I7QUFxRGxCLE1BQWEsc0JBQXNCO0lBRy9CO1FBQ0ksSUFBSSxDQUFDLGFBQWEsR0FBRyx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