// DCAP Quote Verification Constants
// Converted from constants.rs
const { Buffer } = require('buffer');

// Type aliases (represented as comments in JS)
// MrSigner = Uint8Array(32)
// MrEnclave = Uint8Array(32)
// Fmspc = Uint8Array(6)
// CpuSvn = Uint8Array(16)
// Svn = number (u16)

const ATTESTATION_KEY_TYPE_ECDSA256_WITH_P256_CURVE = 2;
const ATTESTATION_KEY_TYPE_ECDSA484_WITH_P384_CURVE = 3;

const TEE_TYPE_SGX = 0x00000000;
const TEE_TYPE_TDX = 0x00000081;

const ECDSA_256_WITH_P256_CURVE = 2;
const ECDSA_384_WITH_P384_CURVE = 3;
const ECDSA_P256_SIGNATURE_BYTE_LEN = 64;
const BODY_BYTE_SIZE = 6;
const BODY_SGX_ENCLAVE_REPORT_TYPE = 1;
const BODY_TD_REPORT10_TYPE = 2;
const BODY_TD_REPORT15_TYPE = 3;
const ENCLAVE_REPORT_BYTE_LEN = 384;
const TD_REPORT10_BYTE_LEN = 584;
const TD_REPORT15_BYTE_LEN = 648;

const PCK_ID_PLAIN_PPID = 1;
const PCK_ID_ENCRYPTED_PPID_2048 = 2;
const PCK_ID_ENCRYPTED_PPID_3072 = 3;
const PCK_ID_PCK_CERTIFICATE = 4;
const PCK_ID_PCK_CERT_CHAIN = 5;
const PCK_ID_QE_REPORT_CERTIFICATION_DATA = 6;

const ALLOWED_QUOTE_VERSIONS = [3, 4, 5];
const ALLOWED_BODY_TYPES = [
    BODY_SGX_ENCLAVE_REPORT_TYPE,
    BODY_TD_REPORT10_TYPE,
    BODY_TD_REPORT15_TYPE,
];
const ALLOWED_TEE_TYPES = [TEE_TYPE_SGX, TEE_TYPE_TDX];
const ALLOWED_ATTESTATION_KEY_TYPES = [ECDSA_256_WITH_P256_CURVE];
const INTEL_QE_VENDOR_ID = new Uint8Array([
    0x93, 0x9A, 0x72, 0x33, 0xF7, 0x9C, 0x4C, 0xA9, 0x94, 0x0A, 0x0D, 0xB3, 0x95, 0x7F, 0x06, 0x07,
]);
const HEADER_BYTE_LEN = 48;
const AUTH_DATA_SIZE_BYTE_LEN = 4;

const ECDSA_SIGNATURE_BYTE_LEN = 64;
const ECDSA_PUBKEY_BYTE_LEN = 64;
const QE_REPORT_BYTE_LEN = ENCLAVE_REPORT_BYTE_LEN;
const QE_REPORT_SIG_BYTE_LEN = ECDSA_SIGNATURE_BYTE_LEN;
const CERTIFICATION_DATA_TYPE_BYTE_LEN = 2;
const CERTIFICATION_DATA_SIZE_BYTE_LEN = 4;
const QE_AUTH_DATA_SIZE_BYTE_LEN = 2;
const QE_CERT_DATA_TYPE_BYTE_LEN = 2;
const QE_CERT_DATA_SIZE_BYTE_LEN = 4;

const AUTH_DATA_MIN_BYTE_LEN = ECDSA_SIGNATURE_BYTE_LEN
    + ECDSA_PUBKEY_BYTE_LEN
    + QE_REPORT_BYTE_LEN
    + QE_REPORT_SIG_BYTE_LEN
    + QE_AUTH_DATA_SIZE_BYTE_LEN
    + QE_CERT_DATA_TYPE_BYTE_LEN
    + QE_CERT_DATA_SIZE_BYTE_LEN;

const QUOTE_MIN_BYTE_LEN =
    HEADER_BYTE_LEN
    + ENCLAVE_REPORT_BYTE_LEN
    + AUTH_DATA_SIZE_BYTE_LEN
    + AUTH_DATA_MIN_BYTE_LEN;

const ATTESTATION_KEY_LEN = 64;
const AUTHENTICATION_DATA_LEN = 32;
const QE_HASH_DATA_BYTE_LEN = ATTESTATION_KEY_LEN + AUTHENTICATION_DATA_LEN;

const PCK_ID_PLAIN = 1;
const PCK_ID_RSA_2048_OAEP = 2;
const PCK_ID_RSA_3072_OAEP = 3;
const PCK_LEAF_CERT_PLAIN = 4;
const PCK_CERT_CHAIN = 5;
const QE_REPORT_CERT = 6;
const PLATFORM_MANIFEST = 7;

// PCK certificate issuer identifiers
const PROCESSOR_ISSUER = "Processor";
const PLATFORM_ISSUER = "Platform";
const PROCESSOR_ISSUER_ID = "processor";
const PLATFORM_ISSUER_ID = "platform";

// The root cert - embedded as base64 string for browser compatibility
// Original file: TrustedRootCA.der (Intel SGX Root CA)
const TRUSTED_ROOT_CA_DER_BASE64 =
    'MIICjzCCAjSgAwIBAgIUImUM1lqdNInzg7SVUr9QGzknBqwwCgYIKoZIzj0EAwIwaDEaMBgGA1UE' +
    'AwwRSW50ZWwgU0dYIFJvb3QgQ0ExGjAYBgNVBAoMEUludGVsIENvcnBvcmF0aW9uMRQwEgYDVQQH' +
    'DAtTYW50YSBDbGFyYTELMAkGA1UECAwCQ0ExCzAJBgNVBAYTAlVTMB4XDTE4MDUyMTEwNDUxMFoX' +
    'DTQ5MTIzMTIzNTk1OVowaDEaMBgGA1UEAwwRSW50ZWwgU0dYIFJvb3QgQ0ExGjAYBgNVBAoMEUlu' +
    'dGVsIENvcnBvcmF0aW9uMRQwEgYDVQQHDAtTYW50YSBDbGFyYTELMAkGA1UECAwCQ0ExCzAJBgNV' +
    'BAYTAlVTMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEC6nEwMDIYZOj/iPWsCzaEKi71OiOSLRF' +
    'hWGjbnBVJfVnkY4u3IjkDYYL0MxO4mqsyYjlBalTVYxFP2sJBK5zlKOBuzCBuDAfBgNVHSMEGDAW' +
    'gBQiZQzWWp00ifODtJVSv1AbOScGrDBSBgNVHR8ESzBJMEegRaBDhkFodHRwczovL2NlcnRpZmlj' +
    'YXRlcy50cnVzdGVkc2VydmljZXMuaW50ZWwuY29tL0ludGVsU0dYUm9vdENBLmRlcjAdBgNVHQ4E' +
    'FgQUImUM1lqdNInzg7SVUr9QGzknBqwwDgYDVR0PAQH/BAQDAgEGMBIGA1UdEwEB/wQIMAYBAf8C' +
    'AQEwCgYIKoZIzj0EAwIDSQAwRgIhAOW/5QkR+S9CiSDcNoowLuPRLsWGf/Yi7GSX94BgwTwgAiEA' +
    '4J0lrHoMs+Xo5o/sX6O9QWxHRAvZUGOdRQ7cvqRXaqI=';

// Convert base64 to Buffer (works in both Node.js and browsers with Buffer polyfill)
const TRUSTED_ROOT_CA_DER = Buffer.from(TRUSTED_ROOT_CA_DER_BASE64, 'base64');

module.exports = {
    ATTESTATION_KEY_TYPE_ECDSA256_WITH_P256_CURVE,
    ATTESTATION_KEY_TYPE_ECDSA484_WITH_P384_CURVE,
    TEE_TYPE_SGX,
    TEE_TYPE_TDX,
    ECDSA_256_WITH_P256_CURVE,
    ECDSA_384_WITH_P384_CURVE,
    ECDSA_P256_SIGNATURE_BYTE_LEN,
    BODY_BYTE_SIZE,
    BODY_SGX_ENCLAVE_REPORT_TYPE,
    BODY_TD_REPORT10_TYPE,
    BODY_TD_REPORT15_TYPE,
    ENCLAVE_REPORT_BYTE_LEN,
    TD_REPORT10_BYTE_LEN,
    TD_REPORT15_BYTE_LEN,
    PCK_ID_PLAIN_PPID,
    PCK_ID_ENCRYPTED_PPID_2048,
    PCK_ID_ENCRYPTED_PPID_3072,
    PCK_ID_PCK_CERTIFICATE,
    PCK_ID_PCK_CERT_CHAIN,
    PCK_ID_QE_REPORT_CERTIFICATION_DATA,
    ALLOWED_QUOTE_VERSIONS,
    ALLOWED_BODY_TYPES,
    ALLOWED_TEE_TYPES,
    ALLOWED_ATTESTATION_KEY_TYPES,
    INTEL_QE_VENDOR_ID,
    HEADER_BYTE_LEN,
    AUTH_DATA_SIZE_BYTE_LEN,
    ECDSA_SIGNATURE_BYTE_LEN,
    ECDSA_PUBKEY_BYTE_LEN,
    QE_REPORT_BYTE_LEN,
    QE_REPORT_SIG_BYTE_LEN,
    CERTIFICATION_DATA_TYPE_BYTE_LEN,
    CERTIFICATION_DATA_SIZE_BYTE_LEN,
    QE_AUTH_DATA_SIZE_BYTE_LEN,
    QE_CERT_DATA_TYPE_BYTE_LEN,
    QE_CERT_DATA_SIZE_BYTE_LEN,
    AUTH_DATA_MIN_BYTE_LEN,
    QUOTE_MIN_BYTE_LEN,
    ATTESTATION_KEY_LEN,
    AUTHENTICATION_DATA_LEN,
    QE_HASH_DATA_BYTE_LEN,
    PCK_ID_PLAIN,
    PCK_ID_RSA_2048_OAEP,
    PCK_ID_RSA_3072_OAEP,
    PCK_LEAF_CERT_PLAIN,
    PCK_CERT_CHAIN,
    QE_REPORT_CERT,
    PLATFORM_MANIFEST,
    PROCESSOR_ISSUER,
    PLATFORM_ISSUER,
    PROCESSOR_ISSUER_ID,
    PLATFORM_ISSUER_ID,
    TRUSTED_ROOT_CA_DER,
};
