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

95 lines
3.0 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";
2025-03-13 13:58:43 +01:00
import { logger } from "matrix-js-sdk/lib/logger";
import {
type MatrixRTCSession,
MatrixRTCSessionEvent,
2025-03-13 13:58:43 +01:00
} from "matrix-js-sdk/lib/matrixrtc";
2025-12-15 18:23:30 +01:00
import { type CallMembershipIdentityParts } from "matrix-js-sdk/lib/matrixrtc/EncryptionManager";
import { firstValueFrom } from "rxjs";
2025-12-15 18:23:30 +01:00
import {
computeLivekitParticipantIdentity$,
2025-12-15 18:23:30 +01:00
livekitIdentityInput,
} from "../state/CallViewModel/remoteMembers/LivekitParticipantIdentity";
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 = (
2023-10-20 17:31:15 +01:00
encryptionKey: Uint8Array,
encryptionKeyIndex: number,
2025-12-15 18:23:30 +01:00
membership: CallMembershipIdentityParts,
): void => {
2025-12-15 18:23:30 +01:00
const unhashedIdentity = livekitIdentityInput(membership);
// This is the only way we can get the kind of the membership event we just received the key for.
// best case we want to recompute this once the memberships change (you can receive the key before the participant...)
//
// TODO change this to `?? "rtc"` for newer versions.
const kind =
this.rtcSession?.memberships.find(
(m) =>
m.userId === membership.userId &&
m.deviceId === membership.deviceId &&
m.memberId === membership.memberId,
)?.kind ?? "session";
Promise.all([
crypto.subtle.importKey("raw", encryptionKey, "HKDF", false, [
"deriveBits",
"deriveKey",
2025-12-15 18:23:30 +01:00
]),
firstValueFrom(computeLivekitParticipantIdentity$(membership, kind)),
2025-12-15 18:23:30 +01:00
]).then(
([keyMaterial, livekitParticipantId]) => {
this.onSetEncryptionKey(
keyMaterial,
livekitParticipantId,
encryptionKeyIndex,
);
2025-12-15 18:23:30 +01:00
logger.debug(
`Sent new key to livekit room=${this.rtcSession?.room.roomId} participantId=${livekitParticipantId} (before hash: ${unhashedIdentity}) encryptionKeyIndex=${encryptionKeyIndex}`,
);
},
(e) => {
logger.error(
`Failed to create key material from buffer for livekit room=${this.rtcSession?.room.roomId} participantId before hash=${unhashedIdentity} encryptionKeyIndex=${encryptionKeyIndex}`,
e,
);
},
);
};
}