Change embedded E2EE implementation

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
Šimon Brandner
2023-08-29 16:50:26 +02:00
parent 127e8c9103
commit b57454fbc1
2 changed files with 21 additions and 46 deletions

View File

@@ -14,9 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { logger } from "@sentry/utils";
import { BaseKeyProvider, createKeyMaterialFromString } from "livekit-client"; import { BaseKeyProvider, createKeyMaterialFromString } from "livekit-client";
import { CallMembership } from "matrix-js-sdk/src/matrixrtc/CallMembership";
import { import {
MatrixRTCSession, MatrixRTCSession,
MatrixRTCSessionEvent, MatrixRTCSessionEvent,
@@ -26,20 +24,9 @@ export class MatrixKeyProvider extends BaseKeyProvider {
private rtcSession?: MatrixRTCSession; private rtcSession?: MatrixRTCSession;
public setRTCSession(rtcSession: MatrixRTCSession) { public setRTCSession(rtcSession: MatrixRTCSession) {
const encryptionKey = rtcSession.activeEncryptionKey;
if (!encryptionKey) {
throw new Error(
"MatrixKeyProvider requires the given MatrixRTCSession to have an activeEncryptionKey"
);
}
if (this.rtcSession) { if (this.rtcSession) {
this.rtcSession.off( this.rtcSession.off(
MatrixRTCSessionEvent.MembershipsChanged, MatrixRTCSessionEvent.EncryptionKeyChanged,
this.onMemberShipsChanged
);
this.rtcSession.off(
MatrixRTCSessionEvent.ActiveEncryptionKeyChanged,
this.onEncryptionKeyChanged this.onEncryptionKeyChanged
); );
} }
@@ -47,41 +34,30 @@ export class MatrixKeyProvider extends BaseKeyProvider {
this.rtcSession = rtcSession; this.rtcSession = rtcSession;
this.rtcSession.on( this.rtcSession.on(
MatrixRTCSessionEvent.MembershipsChanged, MatrixRTCSessionEvent.EncryptionKeyChanged,
this.onMemberShipsChanged
);
this.rtcSession.on(
MatrixRTCSessionEvent.ActiveEncryptionKeyChanged,
this.onEncryptionKeyChanged this.onEncryptionKeyChanged
); );
this.onEncryptionKeyChanged(encryptionKey); for (const [
this.onMemberShipsChanged([], this.rtcSession.memberships); participant,
} encryptionKey,
] of this.rtcSession.encryptionKeys.entries()) {
private onEncryptionKeyChanged = async (key: string) => { this.onEncryptionKeyChanged(
this.onSetEncryptionKey(await createKeyMaterialFromString(key), undefined); encryptionKey,
}; participant.userId,
participant.deviceId
private onMemberShipsChanged = async (
_: CallMembership[],
newMemberships: CallMembership[]
) => {
for (const membership of newMemberships) {
const participantId = `${membership.member.userId}:${membership.deviceId}`;
const encryptionKey = await membership.getActiveEncryptionKey();
if (!encryptionKey) {
logger.warn(
`Participant ${participantId} did not share a key over Matrix`
);
continue;
}
this.onSetEncryptionKey(
await createKeyMaterialFromString(encryptionKey),
participantId
); );
} }
}
private onEncryptionKeyChanged = async (
encryptionKey: string,
userId: string,
deviceId: string
) => {
this.onSetEncryptionKey(
await createKeyMaterialFromString(encryptionKey),
`${userId}:${deviceId}`
);
}; };
} }

View File

@@ -26,7 +26,6 @@ import { useEffect, useMemo, useRef } from "react";
import E2EEWorker from "livekit-client/e2ee-worker?worker"; import E2EEWorker from "livekit-client/e2ee-worker?worker";
import { logger } from "matrix-js-sdk/src/logger"; import { logger } from "matrix-js-sdk/src/logger";
import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession"; import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession";
import { ExternalE2EEKeyProvider } from "livekit-client/dist/src/e2ee/KeyProvider";
import { defaultLiveKitOptions } from "./options"; import { defaultLiveKitOptions } from "./options";
import { SFUConfig } from "./openIDSFU"; import { SFUConfig } from "./openIDSFU";