"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
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");
const p_queue_1 = __importDefault(require("p-queue"));
const fs_1 = require("fs");
class DcapQuoteVerifyService {
    constructor() {
        this.quoteVerifier = new SgxAttestationVerifier();
    }
    async extractQuoteFromCert(pemOrDerCert) {
        if (!pemOrDerCert?.length) {
            throw new errors_1.AttestationDcapQuoteVerifyEnclaveError("empty certificate");
        }
        return this.quoteVerifier.extractQuoteFromCert(pemOrDerCert);
    }
    async validateQuoteVerifierEnclave(quote, checkSecurity) {
        checkSecurity = checkSecurity ?? false;
        if (!quote?.length) {
            throw new errors_1.AttestationDcapQuoteVerifyEnclaveError("empty quote");
        }
        const verifyResult = this.quoteVerifier.verifyQuoteDcap(quote);
        if (checkSecurity && verifyResult.smtEnabled === consts_1.PckFlag.PCK_FLAG_TRUE) {
            throw new errors_1.QuoteSecurityValidationError(verifyResult);
        }
        if (verifyResult.verificationResult !== 0) {
            throw new errors_1.QuoteValidationError(verifyResult.verificationResult);
        }
    }
    async generateTDXQuote(userData) {
        return this.quoteVerifier.generateTDXQuote(userData);
    }
    async validateMode() {
        if (DcapQuoteVerifyService.isInGramineMode) {
            return;
        }
        try {
            await DcapQuoteVerifyService.executeLikeWithMutex.add(async () => {
                await fs_1.promises.access("/dev/attestation/quote");
                DcapQuoteVerifyService.isInGramineMode = true;
            });
        }
        catch (error) {
            throw new errors_1.QuoteGenerationError(`invalid running mode, is it run in gramine / gramine? - ${error}`);
        }
    }
    async generateSGXQuote(userData) {
        await this.validateMode();
        let result = Buffer.alloc(0);
        await DcapQuoteVerifyService.executeLikeWithMutex.add(async () => {
            await fs_1.promises.writeFile("/dev/attestation/user_report_data", Buffer.concat([userData, Buffer.alloc(64)]).slice(0, 64));
            result = await fs_1.promises.readFile("/dev/attestation/quote");
        });
        return result;
    }
}
exports.DcapQuoteVerifyService = DcapQuoteVerifyService;
DcapQuoteVerifyService.isInGramineMode = false;
DcapQuoteVerifyService.executeLikeWithMutex = new p_queue_1.default({ concurrency: 1 });
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGNhcC1xdW90ZS12ZXJpZnkuc2VydmljZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zZ3gtbmF0aXZlLW1vZHVsZS9kY2FwLXF1b3RlLXZlcmlmeS5zZXJ2aWNlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7OztBQUFBLE1BQU0sRUFBRSxzQkFBc0IsRUFBRSxHQUFHLE9BQU8sQ0FBQyx5REFBeUQsQ0FBQyxDQUFDO0FBQ3RHLHFDQUFnRTtBQUNoRSxxQ0FLa0I7QUFDbEIsc0RBQTRCO0FBQzVCLDJCQUE4QjtBQVE5QixNQUFhLHNCQUFzQjtJQUsvQjtRQUNJLElBQUksQ0FBQyxhQUFhLEdBQUcsSUFBSSxzQkFBc0IsRUFBRSxDQUFDO0lBQ3RELENBQUM7SUFFRCxLQUFLLENBQUMsb0JBQW9CLENBQUMsWUFBb0I7UUFDM0MsSUFBSSxDQUFDLFlBQVksRUFBRSxNQUFNLEVBQUUsQ0FBQztZQUN4QixNQUFNLElBQUksK0NBQXNDLENBQUMsbUJBQW1CLENBQUMsQ0FBQztRQUMxRSxDQUFDO1FBRUQsT0FBTyxJQUFJLENBQUMsYUFBYSxDQUFDLG9CQUFvQixDQUFDLFlBQVksQ0FBQyxDQUFDO0lBQ2pFLENBQUM7SUFFRCxLQUFLLENBQUMsNEJBQTRCLENBQUMsS0FBYSxFQUFFLGFBQXVCO1FBQ3JFLGFBQWEsR0FBRyxhQUFhLElBQUksS0FBSyxDQUFDO1FBRXZDLElBQUksQ0FBQy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