WIP ratcheting

This commit is contained in:
Valere
2025-04-29 15:56:51 +02:00
parent 34b3b9d733
commit 795230247d
2 changed files with 34 additions and 5 deletions

View File

@@ -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",

View File

@@ -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);