/** * @license * Copyright 2022-2026 Matter.js Authors * SPDX-License-Identifier: Apache-2.0 */ import { Bytes, UnexpectedDataError } from "@matter/general"; import { SecureMessageType, TlvSchema } from "@matter/types"; import { ExchangeSendOptions } from "../../protocol/MessageExchange.js"; import { DEFAULT_NORMAL_PROCESSING_TIME, SecureChannelMessenger } from "../../securechannel/SecureChannelMessenger.js"; import { PasePake1, PasePake2, PasePake3, PbkdfParamRequest, PbkdfParamResponse, TlvPasePake1, TlvPasePake2, TlvPasePake3, TlvPbkdfParamRequest, TlvPbkdfParamResponse, } from "./PaseMessages.js"; export const DEFAULT_PASSCODE_ID = 0; export const SPAKE_CONTEXT = Bytes.fromString("CHIP PAKE V1 Commissioning"); export class PaseServerMessenger extends SecureChannelMessenger { #closed = false; async readPbkdfParamRequest() { const message = await this.nextMessage({ type: SecureMessageType.PbkdfParamRequest, expectedProcessingTime: DEFAULT_NORMAL_PROCESSING_TIME, }); return { requestPayload: message.payload, request: this.decode(TlvPbkdfParamRequest, message) as PbkdfParamRequest, }; } async sendPbkdfParamResponse(response: PbkdfParamResponse) { return this.send(response, SecureMessageType.PbkdfParamResponse, TlvPbkdfParamResponse, { expectedProcessingTime: DEFAULT_NORMAL_PROCESSING_TIME, }); } readPasePake1() { return this.nextMessageDecoded(TlvPasePake1, { type: SecureMessageType.PasePake1 }); } sendPasePake2(pasePake2: PasePake2) { return this.send(pasePake2, SecureMessageType.PasePake2, TlvPasePake2); } readPasePake3() { return this.nextMessageDecoded(TlvPasePake3, { type: SecureMessageType.PasePake3 }); } override close() { this.#closed = true; return super.close(); } override send(message: T, type: number, schema: TlvSchema, options?: ExchangeSendOptions) { if (this.#closed) { throw new UnexpectedDataError("Cannot send message, messenger is closed"); } return super.send(message, type, schema, options); } override async nextMessage(options: SecureChannelMessenger.ReadOptions) { if (this.#closed) { throw new UnexpectedDataError("Cannot read message, messenger is closed"); } const result = await super.nextMessage(options); if (this.#closed) { throw new UnexpectedDataError("Cannot read message, messenger is closed"); } return result; } } export class PaseClientMessenger extends SecureChannelMessenger { sendPbkdfParamRequest(request: PbkdfParamRequest, options?: ExchangeSendOptions) { return this.send(request, SecureMessageType.PbkdfParamRequest, TlvPbkdfParamRequest, { expectedProcessingTime: DEFAULT_NORMAL_PROCESSING_TIME, ...options, }); } async readPbkdfParamResponse(options?: SecureChannelMessenger.ReadOptions) { const message = await this.nextMessage({ type: SecureMessageType.PbkdfParamResponse, expectedProcessingTime: DEFAULT_NORMAL_PROCESSING_TIME, abort: options?.abort, }); // TODO Add support for BUSY response and resend the message after waiting time return { responsePayload: message.payload, response: this.decode(TlvPbkdfParamResponse, message) as PbkdfParamResponse, }; } sendPasePake1(pasePake1: PasePake1, options?: ExchangeSendOptions) { return this.send(pasePake1, SecureMessageType.PasePake1, TlvPasePake1, options); } readPasePake2(options?: SecureChannelMessenger.ReadOptions) { return this.nextMessageDecoded(TlvPasePake2, { type: SecureMessageType.PasePake2, ...options }); } sendPasePake3(pasePake3: PasePake3, options?: ExchangeSendOptions) { return this.send(pasePake3, SecureMessageType.PasePake3, TlvPasePake3, options); } }