Files
element-call/src/e2ee/matrixKeyProvider.ts
T

76 lines
2.3 KiB
TypeScript
Raw Normal View History

/*
Copyright 2023, 2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { BaseKeyProvider } from "livekit-client";
import {
type MatrixRTCSession,
MatrixRTCSessionEvent,
2025-03-13 13:58:43 +01:00
} from "matrix-js-sdk/lib/matrixrtc";
2025-12-19 19:23:41 +01:00
import { logger as rootLogger } from "matrix-js-sdk/lib/logger";
2025-12-29 17:38:54 +01:00
import { type CallMembershipIdentityParts } from "matrix-js-sdk/lib/matrixrtc/EncryptionManager";
2025-12-19 19:23:41 +01:00
const logger = rootLogger.getChild("[MatrixKeyProvider]");
export class MatrixKeyProvider extends BaseKeyProvider {
private rtcSession?: MatrixRTCSession;
2023-10-19 16:50:29 +01:00
public constructor() {
super({ ratchetWindowSize: 10, keyringSize: 256 });
2023-10-19 16:50:29 +01:00
}
public setRTCSession(rtcSession: MatrixRTCSession): void {
if (this.rtcSession) {
this.rtcSession.off(
MatrixRTCSessionEvent.EncryptionKeyChanged,
this.onEncryptionKeyChanged,
);
}
this.rtcSession = rtcSession;
this.rtcSession.on(
MatrixRTCSessionEvent.EncryptionKeyChanged,
this.onEncryptionKeyChanged,
);
// The new session could be aware of keys of which the old session wasn't,
// so emit key changed events
this.rtcSession.reemitEncryptionKeys();
}
private onEncryptionKeyChanged = (
2026-01-05 18:47:57 +01:00
encryptionKey: Uint8Array<ArrayBuffer>,
encryptionKeyIndex: number,
2025-12-29 17:38:54 +01:00
membershipParts: CallMembershipIdentityParts,
rtcBackendIdentity: string,
): void => {
2025-12-19 19:23:41 +01:00
crypto.subtle
.importKey("raw", encryptionKey, "HKDF", false, [
"deriveBits",
"deriveKey",
2025-12-19 19:23:41 +01:00
])
.then(
(keyMaterial) => {
this.onSetEncryptionKey(
keyMaterial,
rtcBackendIdentity,
2025-12-19 19:23:41 +01:00
encryptionKeyIndex,
);
2025-12-19 19:23:41 +01:00
logger.debug(
2026-01-05 21:58:26 +01:00
`Sent new key to livekit room=${this.rtcSession?.room.roomId} participantId=${rtcBackendIdentity} (before hash: ${membershipParts.userId}:${membershipParts.deviceId}) encryptionKeyIndex=${encryptionKeyIndex}`,
2025-12-19 19:23:41 +01:00
);
},
(e) => {
logger.error(
2026-01-05 21:58:26 +01:00
`Failed to create key material from buffer for livekit room=${this.rtcSession?.room.roomId} participantId before hash=${membershipParts.userId}:${membershipParts.deviceId} encryptionKeyIndex=${encryptionKeyIndex}`,
2025-12-19 19:23:41 +01:00
e,
);
},
);
};
}