From 795230247d4e17197bb3497419eef1baf414bb21 Mon Sep 17 00:00:00 2001 From: Valere Date: Tue, 29 Apr 2025 15:56:51 +0200 Subject: [PATCH] WIP ratcheting --- package.json | 4 ++-- src/e2ee/matrixKeyProvider.ts | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 6ff4198d..28302e68 100644 --- a/package.json +++ b/package.json @@ -99,10 +99,10 @@ "i18next-parser": "^9.1.0", "jsdom": "^26.0.0", "knip": "^5.27.2", - "livekit-client": "^2.11.3", + "livekit-client": "github:BillCarsonFr/client-sdk-js#4ac197085ee063f66b48d8ec24c88b2321fcbf9e", "lodash-es": "^4.17.21", "loglevel": "^1.9.1", - "matrix-js-sdk": "github:matrix-org/matrix-js-sdk#19b1b901f575755d29d1fe03ca48cbf7c1cae05c", + "matrix-js-sdk": "github:matrix-org/matrix-js-sdk#44d1a860a1c56a0d4193aa7dcd09e10822398a4d", "matrix-widget-api": "1.11.0", "normalize.css": "^8.0.1", "observable-hooks": "^4.2.3", diff --git a/src/e2ee/matrixKeyProvider.ts b/src/e2ee/matrixKeyProvider.ts index 9b190ed8..515fec64 100644 --- a/src/e2ee/matrixKeyProvider.ts +++ b/src/e2ee/matrixKeyProvider.ts @@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE in the repository root for full details. */ -import { BaseKeyProvider, createKeyMaterialFromBuffer } from "livekit-client"; +import { BaseKeyProvider, createKeyMaterialFromBuffer, importKey, KeyProviderEvent } from "livekit-client"; import { logger } from "matrix-js-sdk/lib/logger"; import { type MatrixRTCSession, @@ -15,8 +15,22 @@ import { export class MatrixKeyProvider extends BaseKeyProvider { private rtcSession?: MatrixRTCSession; + private readonly onKeyRatchetComplete: (material: ArrayBuffer, keyIndex?: number) => void; + public constructor() { - super({ ratchetWindowSize: 10, keyringSize: 256 }); + super({ ratchetWindowSize: 10, keyringSize: 10 }); + + this.onKeyRatchetComplete = (material: ArrayBuffer, keyIndex?: number): void => { + logger.debug(`key ratcheted event received for index `, keyIndex ); + this.rtcSession?.onOwnKeyRatcheted(material, keyIndex).catch((e) => { + logger.error( + `Failed to ratchet key for livekit room=${this.rtcSession?.room.roomId} keyIndex=${keyIndex}`, + e, + ); + }); + }; + + this.on(KeyProviderEvent.RatchetRequestCompleted, this.onKeyRatchetComplete); } public setRTCSession(rtcSession: MatrixRTCSession): void { @@ -25,6 +39,11 @@ export class MatrixKeyProvider extends BaseKeyProvider { MatrixRTCSessionEvent.EncryptionKeyChanged, this.onEncryptionKeyChanged, ); + this.rtcSession.off( + MatrixRTCSessionEvent.EncryptionKeyQueryRatchetStep, + this.doRatchetKey, + ); + } this.rtcSession = rtcSession; @@ -34,17 +53,27 @@ export class MatrixKeyProvider extends BaseKeyProvider { this.onEncryptionKeyChanged, ); + this.rtcSession.on( + MatrixRTCSessionEvent.EncryptionKeyQueryRatchetStep, + this.doRatchetKey, + ); + + // The new session could be aware of keys of which the old session wasn't, // so emit key changed events this.rtcSession.reemitEncryptionKeys(); } + private doRatchetKey = (participantId:string, keyIndex:number): void => { + this.ratchetKey(participantId, keyIndex); + } + private onEncryptionKeyChanged = ( encryptionKey: Uint8Array, encryptionKeyIndex: number, participantId: string, ): void => { - createKeyMaterialFromBuffer(encryptionKey).then( + importKey(encryptionKey, "HKDF", 'derive').then( (keyMaterial) => { this.onSetEncryptionKey(keyMaterial, participantId, encryptionKeyIndex);