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

95 lines
3.4 KiB
TypeScript
Raw Normal View History

2023-08-08 13:46:51 +02:00
/*
Copyright 2023, 2024 New Vector Ltd.
2023-08-08 13:46:51 +02:00
SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details.
2023-08-08 13:46:51 +02:00
*/
2023-08-11 16:57:55 +02:00
import { useEffect, useMemo } from "react";
2023-08-08 13:46:51 +02:00
import { setLocalStorageItem, useLocalStorage } from "../useLocalStorage";
import { UrlParams, getUrlParams, useUrlParams } from "../UrlParams";
2024-04-23 15:15:13 +02:00
import { E2eeType } from "./e2eeType";
import { useClient } from "../ClientContext";
2023-08-09 13:29:45 +02:00
export function saveKeyForRoom(roomId: string, password: string): void {
setLocalStorageItem(getRoomSharedKeyLocalStorageKey(roomId), password);
}
const getRoomSharedKeyLocalStorageKey = (roomId: string): string =>
2023-08-09 13:29:45 +02:00
`room-shared-key-${roomId}`;
const useInternalRoomSharedKey = (roomId: string): string | null => {
2023-10-11 12:53:33 +01:00
const key = getRoomSharedKeyLocalStorageKey(roomId);
const roomSharedKey = useLocalStorage(key)[0];
2023-08-09 13:29:45 +02:00
return roomSharedKey;
2023-08-09 13:29:45 +02:00
};
2023-08-11 16:57:55 +02:00
export function getKeyForRoom(roomId: string): string | null {
saveKeyFromUrlParams(getUrlParams());
const key = getRoomSharedKeyLocalStorageKey(roomId);
return localStorage.getItem(key);
}
function saveKeyFromUrlParams(urlParams: UrlParams): void {
if (!urlParams.password || !urlParams.roomId) return;
2023-11-21 09:59:07 +00:00
// Take the key from the URL and save it.
// It's important to always use the room ID specified in the URL
// when saving keys rather than whatever the current room ID might be,
// in case we've moved to a different room but the URL hasn't changed.
saveKeyForRoom(urlParams.roomId, urlParams.password);
}
2023-10-11 12:53:33 +01:00
/**
* Extracts the room password from the URL if one is present, saving it in localstorage
* and returning it in a tuple with the corresponding room ID from the URL.
* @returns A tuple of the roomId and password from the URL if the URL has both,
* otherwise [undefined, undefined]
*/
const useKeyFromUrl = (): [string, string] | [undefined, undefined] => {
const urlParams = useUrlParams();
useEffect(() => saveKeyFromUrlParams(urlParams), [urlParams]);
2023-09-20 17:05:10 +01:00
2023-10-11 12:53:33 +01:00
return urlParams.roomId && urlParams.password
? [urlParams.roomId, urlParams.password]
: [undefined, undefined];
};
2024-04-23 15:15:13 +02:00
export type Unencrypted = { kind: E2eeType.NONE };
export type SharedSecret = { kind: E2eeType.SHARED_KEY; secret: string };
export type PerParticipantE2EE = { kind: E2eeType.PER_PARTICIPANT };
export type EncryptionSystem = Unencrypted | SharedSecret | PerParticipantE2EE;
export function useRoomEncryptionSystem(roomId: string): EncryptionSystem {
const { client } = useClient();
// make sure we've extracted the key from the URL first
2023-09-20 17:05:10 +01:00
// (and we still need to take the value it returns because
// the effect won't run in time for it to save to localstorage in
// time for us to read it out again).
2024-04-23 15:15:13 +02:00
const [urlRoomId, passwordFromUrl] = useKeyFromUrl();
2023-10-11 12:53:33 +01:00
const storedPassword = useInternalRoomSharedKey(roomId);
2024-04-23 15:15:13 +02:00
const room = client?.getRoom(roomId);
const e2eeSystem = <EncryptionSystem>useMemo(() => {
if (!room) return { kind: E2eeType.NONE };
if (storedPassword)
return {
kind: E2eeType.SHARED_KEY,
secret: storedPassword,
};
if (urlRoomId === roomId)
return {
kind: E2eeType.SHARED_KEY,
secret: passwordFromUrl,
};
if (room.hasEncryptionStateEvent()) {
return { kind: E2eeType.PER_PARTICIPANT };
}
return { kind: E2eeType.NONE };
}, [passwordFromUrl, room, roomId, storedPassword, urlRoomId]);
return e2eeSystem;
}