E2EE for embeded mode

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
Šimon Brandner
2023-08-22 17:23:29 +02:00
parent 923f081d74
commit fa5b014abe
3 changed files with 86 additions and 1 deletions

View File

@@ -0,0 +1,81 @@
/*
Copyright 2023 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { logger } from "@sentry/utils";
import {
BaseKeyProvider,
KeyProviderOptions,
createKeyMaterialFromString,
} from "livekit-client";
import { CallMembership } from "matrix-js-sdk/src/matrixrtc/CallMembership";
import {
MatrixRTCSession,
MatrixRTCSessionEvent,
} from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession";
export class MatrixKeyProvider extends BaseKeyProvider {
constructor(
private rtcSession: MatrixRTCSession,
keyProviderOptions: Partial<KeyProviderOptions> = {}
) {
super(keyProviderOptions);
const encryptionKey = this.rtcSession.activeEncryptionKey;
if (!encryptionKey) {
throw new Error(
"MatrixKeyProvider requires the given MatrixRTCSession to have an activeEncryptionKey"
);
}
this.rtcSession.on(
MatrixRTCSessionEvent.MembershipsChanged,
this.onMemberShipsChanged
);
this.rtcSession.on(
MatrixRTCSessionEvent.ActiveEncryptionKeyChanged,
this.onEncryptionKeyChanged
);
this.onEncryptionKeyChanged(encryptionKey);
this.onMemberShipsChanged([], this.rtcSession.memberships);
}
private onEncryptionKeyChanged = async (key: string) => {
this.onSetEncryptionKey(await createKeyMaterialFromString(key), undefined);
};
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
);
}
};
}

View File

@@ -17,7 +17,6 @@ limitations under the License.
import {
ConnectionState,
E2EEOptions,
ExternalE2EEKeyProvider,
Room,
RoomOptions,
setLogLevel,
@@ -26,6 +25,8 @@ import { useLiveKitRoom } from "@livekit/components-react";
import { useEffect, useMemo, useRef } from "react";
import E2EEWorker from "livekit-client/e2ee-worker?worker";
import { logger } from "matrix-js-sdk/src/logger";
import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession";
import { ExternalE2EEKeyProvider } from "livekit-client/dist/src/e2ee/KeyProvider";
import { defaultLiveKitOptions } from "./options";
import { SFUConfig } from "./openIDSFU";
@@ -39,6 +40,7 @@ import {
ECConnectionState,
useECConnectionState,
} from "./useECConnectionState";
import { MatrixKeyProvider } from "../e2ee/matrixKeyProvider";
export type E2EEConfig = {
sharedKey: string;
@@ -53,6 +55,7 @@ interface UseLivekitResult {
export function useLiveKit(
muteStates: MuteStates,
rtcSession: MatrixRTCSession,
sfuConfig?: SFUConfig,
e2eeConfig?: E2EEConfig
): UseLivekitResult {

View File

@@ -95,6 +95,7 @@ export function ActiveCall(props: ActiveCallProps) {
const sfuConfig = useOpenIDSFU(props.client, props.rtcSession);
const { livekitRoom, connState } = useLiveKit(
props.muteStates,
props.rtcSession,
sfuConfig,
props.e2eeConfig
);