/** * @license * Copyright 2022-2026 Matter.js Authors * SPDX-License-Identifier: Apache-2.0 */ import { Bytes, CertificateError, EcdsaSignature, Logger, Time } from "@matter/general"; import { Certificate } from "./Certificate.js"; import { MlDsaSignature } from "./CertificateSignature.js"; import { Unsigned } from "./common.js"; import { MatterCertificate } from "./definitions/base.js"; const logger = Logger.get("OperationalBaseCertificate"); /** * Base class for all operational certificates (RCAC, ICAC, NOC) */ export abstract class OperationalBase extends Certificate { /** §6.1.3 caps a Matter operational certificate (NOC chain + VVSC) at 400 bytes in TLV form. */ static readonly MAX_TLV_BYTES = 400; protected static assertTlvSize(tlv: Bytes): void { if (tlv.byteLength > OperationalBase.MAX_TLV_BYTES) { throw new CertificateError( `Matter-TLV operational certificate of ${tlv.byteLength} bytes exceeds the ${OperationalBase.MAX_TLV_BYTES} byte limit.`, ); } } constructor(cert: CT | Unsigned) { super(cert); this.validateFields(); } /** Operational certificates are always signed with ecdsa-with-SHA256. */ override get signature(): EcdsaSignature { const signature = super.signature; if (signature instanceof MlDsaSignature) { throw new CertificateError("Operational certificate is not signed with ecdsa-with-SHA256"); } return signature; } override set signature(signature: EcdsaSignature) { super.signature = signature; } /** Validates all basic certificate fields on construction. */ protected abstract validateFields(): void; /** Encodes the signed certificate into the Matter TLV format. */ abstract asSignedTlv(): Bytes; /** * Verifies general requirements a Matter certificate fields must fulfill. * Rules for this are listed in @see {@link MatterSpecification.v161.Core} §6.5.x */ generalVerify() { const cert = this.cert; // Spec §6.5 mandates serial number ≤ 20 octets. Tolerate 21 octets (seen in the wild, e.g. some LG TVs) // with a warning; reject anything longer to avoid accepting arbitrarily malformed certificates. if (cert.serialNumber.byteLength > 21) { throw new CertificateError( `Serial number must not be longer than 20 octets. Current serial number has ${cert.serialNumber.byteLength} octets.`, ); } if (cert.serialNumber.byteLength > 20) { logger.warn( `Certificate serial number has ${cert.serialNumber.byteLength} octets, exceeds spec maximum of 20 octets.`, ); } if (cert.signatureAlgorithm !== 1) { // ecdsa-with-sha256 throw new CertificateError(`Unsupported signature algorithm: ${cert.signatureAlgorithm}`); } if (cert.publicKeyAlgorithm !== 1) { // ec-pub-key throw new CertificateError(`Unsupported public key algorithm: ${cert.publicKeyAlgorithm}`); } if (cert.ellipticCurveIdentifier !== 1) { // prime256v1 throw new CertificateError(`Unsupported elliptic curve identifier: ${cert.ellipticCurveIdentifier}`); } // All implementations SHALL reject Matter certificates with more than 5 RDNs in a single DN. if (Object.keys(cert.subject).length > 5) { throw new CertificateError(`Certificate subject must not contain more than 5 RDNs.`); } if (Object.keys(cert.issuer).length > 5) { throw new CertificateError(`Certificate issuer must not contain more than 5 RDNs.`); } // notBefore date should be already reached, notAfter is not checked right now // TODO: implement real checks when we add "Last known Good UTC time" if (cert.notBefore * 1000 > Time.nowMs) { logger.warn(`Certificate notBefore date is in the future: ${cert.notBefore * 1000} vs ${Time.nowMs}`); /*throw new CertificateError( `Certificate notBefore date is in the future: ${cert.notBefore * 1000} vs ${Time.nowMs}`, );*/ } } }