"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Rcac_exports = {};
__export(Rcac_exports, {
  Rcac: () => Rcac
});
module.exports = __toCommonJS(Rcac_exports);
var import_general = require("@matter/general");
var import_types = require("@matter/types");
var import_Certificate = require("./Certificate.js");
var import_base = require("./definitions/base.js");
var import_operational = require("./definitions/operational.js");
var import_OperationalBase = require("./OperationalBase.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class Rcac extends import_OperationalBase.OperationalBase {
  /** Construct the class from a Tlv version of the certificate */
  static fromTlv(tlv) {
    return new Rcac(import_operational.OperationalCertificate.TlvRcac.decode(tlv));
  }
  static publicKeyOfTlv(tlv) {
    return Rcac.fromTlv(tlv).cert.ellipticCurvePublicKey;
  }
  /** Construct the class from an ASN.1/DER encoded certificate */
  static fromAsn1(asn1) {
    const cert = import_Certificate.Certificate.parseAsn1Certificate(asn1);
    return new Rcac(cert);
  }
  /** Validates all basic certificate fields on construction. */
  validateFields() {
    const {
      extensions: {
        basicConstraints: { isCa }
      }
    } = this.cert;
    if (!isCa) {
      throw new import_general.CertificateError("Root certificate must be a CA.");
    }
  }
  /**
   * Encodes the certificate with the signature as Matter Tlv.
   * If the certificate is not signed, it throws a CertificateError.
   */
  asSignedTlv() {
    return import_operational.OperationalCertificate.TlvRcac.encode({ ...this.cert, signature: this.signature.bytes });
  }
  /**
   * Verify requirements a Matter Root certificate must fulfill.
   * Rules for this are listed in @see {@link MatterSpecification.v12.Core} §6.5.x
   */
  async verify(crypto) {
    this.generalVerify();
    const { subject, extensions } = this.cert;
    const { fabricId, rcacId } = subject;
    const { basicConstraints, subjectKeyIdentifier, authorityKeyIdentifier } = extensions;
    if ("nodeId" in subject) {
      throw new import_general.CertificateError(`Root certificate must not contain a nodeId.`);
    }
    if (fabricId !== void 0) {
      if (Array.isArray(fabricId)) {
        throw new import_general.CertificateError(`Invalid fabricId in NoC certificate: ${import_general.Diagnostic.json(fabricId)}`);
      }
      if (fabricId === (0, import_types.FabricId)(0)) {
        throw new import_general.CertificateError(`Invalid fabricId in NoC certificate: ${import_general.Diagnostic.json(fabricId)}`);
      }
    }
    if ("icacId" in subject) {
      throw new import_general.CertificateError(`Root certificate must not contain an icacId.`);
    }
    if (rcacId === void 0 || Array.isArray(rcacId)) {
      throw new import_general.CertificateError(`Invalid rcacId in Root certificate: ${import_general.Diagnostic.json(rcacId)}`);
    }
    if ("caseAuthenticatedTags" in subject) {
      throw new import_general.CertificateError(`Root certificate must not contain a caseAuthenticatedTags.`);
    }
    if ("vvsId" in subject) {
      throw new import_general.CertificateError(`Root certificate must not contain a vvsId.`);
    }
    if (basicConstraints.isCa !== true) {
      throw new import_general.CertificateError(`Root certificate must have isCa set to true.`);
    }
    const keyUsage = import_base.ExtensionKeyUsageSchema.encode(extensions.keyUsage);
    if (keyUsage !== 96 && keyUsage !== 97) {
      throw new import_general.CertificateError(
        `Root certificate keyUsage must have keyCertSign and CRLSign and optionally digitalSignature set.`
      );
    }
    if (extensions.extendedKeyUsage !== void 0) {
      throw new import_general.CertificateError(`Root certificate must not have extendedKeyUsage set.`);
    }
    if (subjectKeyIdentifier === void 0) {
      throw new import_general.CertificateError(`Root certificate must have subjectKeyIdentifier set.`);
    }
    if (subjectKeyIdentifier.byteLength !== 20) {
      throw new import_general.CertificateError(`Root certificate subjectKeyIdentifier must be 160 bit.`);
    }
    if (authorityKeyIdentifier === void 0) {
      throw new import_general.CertificateError(`Root certificate must have authorityKeyIdentifier set.`);
    }
    if (authorityKeyIdentifier.byteLength !== 20) {
      throw new import_general.CertificateError(`Root certificate authorityKeyIdentifier must be 160 bit.`);
    }
    if (!import_general.Bytes.areEqual(authorityKeyIdentifier, subjectKeyIdentifier)) {
      throw new import_general.CertificateError(
        `Root certificate authorityKeyIdentifier must be equal to subjectKeyIdentifier.`
      );
    }
    await crypto.verifyEcdsa((0, import_general.PublicKey)(this.cert.ellipticCurvePublicKey), this.asUnsignedDer(), this.signature);
  }
}
//# sourceMappingURL=Rcac.js.map
