/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Bytes, ContextTaggedBytes, DerCodec, Pkcs7, PrivateKey, Shs, X962 } from "@matter/general";
import { assertCertificateDerSize } from "./common.js";
import { CertificationDeclaration as CertificationDeclarationDef } from "./definitions/certification-declaration.js";
const TestCMS_SignerPrivateKey = Bytes.fromHex("AEF3484116E9481EC57BE0472DF41BF499064E5024AD869ECA5E889802D48075");
const TestCMS_SignerSubjectKeyIdentifier = Bytes.fromHex("62FA823359ACFAA9963E1CFA140ADDF504F37160");
class CertificationDeclaration {
  #eContent;
  #subjectKeyIdentifier;
  /**
   * Generator which is the main usage for the class from outside.
   * It constructs the class with the relevant details and returns a signed ASN.1 DER version of the CD.
   */
  static generate(crypto, vendorId, productId, provisional = false) {
    const cd = new CertificationDeclaration(
      {
        formatVersion: 1,
        vendorId,
        produceIdArray: [productId],
        deviceTypeId: 22,
        certificateId: "CSA00000SWC00000-00",
        securityLevel: 0,
        securityInformation: 0,
        versionNumber: 1,
        certificationType: provisional ? 1 : 0
        // 0 = Test, 1 = Provisional/In certification, 2 = official
      },
      TestCMS_SignerSubjectKeyIdentifier
    );
    return cd.asSignedAsn1(crypto, PrivateKey(TestCMS_SignerPrivateKey));
  }
  constructor(content, subjectKeyIdentifier) {
    this.#eContent = CertificationDeclarationDef.TlvDc.encode(content);
    this.#subjectKeyIdentifier = subjectKeyIdentifier;
  }
  /**
   * Returns the signed certificate in ASN.1 DER format.
   */
  async asSignedAsn1(crypto, privateKey) {
    const cert = {
      version: 3,
      digestAlgorithm: [Shs.SHA256_CMS],
      encapContentInfo: Pkcs7.Data(this.#eContent),
      signerInfo: [
        {
          version: 3,
          subjectKeyIdentifier: ContextTaggedBytes(0, this.#subjectKeyIdentifier),
          digestAlgorithm: Shs.SHA256_CMS,
          signatureAlgorithm: X962.EcdsaWithSHA256,
          signature: (await crypto.signEcdsa(privateKey, this.#eContent)).der
        }
      ]
    };
    const certBytes = DerCodec.encode(Pkcs7.SignedData(cert));
    assertCertificateDerSize(certBytes);
    return certBytes;
  }
}
export {
  CertificationDeclaration
};
//# sourceMappingURL=CertificationDeclaration.js.map
