Skip to content

Commit e9f86ce

Browse files
committed
WIP ratcheting
1 parent 3808801 commit e9f86ce

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@
9898
"i18next-parser": "^9.1.0",
9999
"jsdom": "^26.0.0",
100100
"knip": "^5.27.2",
101-
"livekit-client": "^2.11.3",
101+
"livekit-client": "github:BillCarsonFr/client-sdk-js#4ac197085ee063f66b48d8ec24c88b2321fcbf9e",
102102
"lodash-es": "^4.17.21",
103103
"loglevel": "^1.9.1",
104-
"matrix-js-sdk": "github:matrix-org/matrix-js-sdk#19b1b901f575755d29d1fe03ca48cbf7c1cae05c",
104+
"matrix-js-sdk": "github:matrix-org/matrix-js-sdk#44d1a860a1c56a0d4193aa7dcd09e10822398a4d",
105105
"matrix-widget-api": "1.11.0",
106106
"normalize.css": "^8.0.1",
107107
"observable-hooks": "^4.2.3",

src/e2ee/matrixKeyProvider.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
55
Please see LICENSE in the repository root for full details.
66
*/
77

8-
import { BaseKeyProvider, createKeyMaterialFromBuffer } from "livekit-client";
8+
import { BaseKeyProvider, createKeyMaterialFromBuffer, importKey, KeyProviderEvent } from "livekit-client";
99
import { logger } from "matrix-js-sdk/lib/logger";
1010
import {
1111
type MatrixRTCSession,
@@ -15,8 +15,22 @@ import {
1515
export class MatrixKeyProvider extends BaseKeyProvider {
1616
private rtcSession?: MatrixRTCSession;
1717

18+
private readonly onKeyRatchetComplete: (material: ArrayBuffer, keyIndex?: number) => void;
19+
1820
public constructor() {
19-
super({ ratchetWindowSize: 10, keyringSize: 256 });
21+
super({ ratchetWindowSize: 10, keyringSize: 10 });
22+
23+
this.onKeyRatchetComplete = (material: ArrayBuffer, keyIndex?: number): void => {
24+
logger.debug(`key ratcheted event received for index `, keyIndex );
25+
this.rtcSession?.onOwnKeyRatcheted(material, keyIndex).catch((e) => {
26+
logger.error(
27+
`Failed to ratchet key for livekit room=${this.rtcSession?.room.roomId} keyIndex=${keyIndex}`,
28+
e,
29+
);
30+
});
31+
};
32+
33+
this.on(KeyProviderEvent.RatchetRequestCompleted, this.onKeyRatchetComplete);
2034
}
2135

2236
public setRTCSession(rtcSession: MatrixRTCSession): void {
@@ -25,6 +39,11 @@ export class MatrixKeyProvider extends BaseKeyProvider {
2539
MatrixRTCSessionEvent.EncryptionKeyChanged,
2640
this.onEncryptionKeyChanged,
2741
);
42+
this.rtcSession.off(
43+
MatrixRTCSessionEvent.EncryptionKeyQueryRatchetStep,
44+
this.doRatchetKey,
45+
);
46+
2847
}
2948

3049
this.rtcSession = rtcSession;
@@ -34,17 +53,27 @@ export class MatrixKeyProvider extends BaseKeyProvider {
3453
this.onEncryptionKeyChanged,
3554
);
3655

56+
this.rtcSession.on(
57+
MatrixRTCSessionEvent.EncryptionKeyQueryRatchetStep,
58+
this.doRatchetKey,
59+
);
60+
61+
3762
// The new session could be aware of keys of which the old session wasn't,
3863
// so emit key changed events
3964
this.rtcSession.reemitEncryptionKeys();
4065
}
4166

67+
private doRatchetKey = (participantId:string, keyIndex:number): void => {
68+
this.ratchetKey(participantId, keyIndex);
69+
}
70+
4271
private onEncryptionKeyChanged = (
4372
encryptionKey: Uint8Array,
4473
encryptionKeyIndex: number,
4574
participantId: string,
4675
): void => {
47-
createKeyMaterialFromBuffer(encryptionKey).then(
76+
importKey(encryptionKey, "HKDF", 'derive').then(
4877
(keyMaterial) => {
4978
this.onSetEncryptionKey(keyMaterial, participantId, encryptionKeyIndex);
5079

0 commit comments

Comments
 (0)