2019-12-05 15:20:30 +00:00
|
|
|
/*
|
2020-09-03 13:50:49 +00:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2019-12-05 15:20:30 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2024-06-24 09:14:42 +00:00
|
|
|
import { Crypto, ICryptoCallbacks, encodeBase64, SecretStorage } from "matrix-js-sdk/src/matrix";
|
2021-12-09 09:10:23 +00:00
|
|
|
import { deriveKey } from "matrix-js-sdk/src/crypto/key_passphrase";
|
|
|
|
import { decodeRecoveryKey } from "matrix-js-sdk/src/crypto/recoverykey";
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
2023-02-28 10:31:48 +00:00
|
|
|
import type CreateSecretStorageDialog from "./async-components/views/dialogs/security/CreateSecretStorageDialog";
|
2019-12-05 15:20:30 +00:00
|
|
|
import Modal from "./Modal";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { MatrixClientPeg } from "./MatrixClientPeg";
|
2019-12-11 15:05:03 +00:00
|
|
|
import { _t } from "./languageHandler";
|
2020-08-14 17:06:35 +00:00
|
|
|
import { isSecureBackupRequired } from "./utils/WellKnownUtils";
|
2023-02-28 10:31:48 +00:00
|
|
|
import AccessSecretStorageDialog, { KeyParams } from "./components/views/dialogs/security/AccessSecretStorageDialog";
|
2020-10-02 01:41:03 +00:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2024-04-12 15:15:17 +00:00
|
|
|
import { ModuleRunner } from "./modules/ModuleRunner";
|
2021-11-08 10:27:52 +00:00
|
|
|
import QuestionDialog from "./components/views/dialogs/QuestionDialog";
|
2022-03-02 23:33:40 +00:00
|
|
|
import InteractiveAuthDialog from "./components/views/dialogs/InteractiveAuthDialog";
|
2021-09-21 15:48:09 +00:00
|
|
|
|
2019-12-11 14:28:02 +00:00
|
|
|
// This stores the secret storage private keys in memory for the JS SDK. This is
|
|
|
|
// only meant to act as a cache to avoid prompting the user multiple times
|
2019-12-11 15:05:03 +00:00
|
|
|
// during the same single operation. Use `accessSecretStorage` below to scope a
|
|
|
|
// single secret storage operation, as it will clear the cached keys once the
|
|
|
|
// operation ends.
|
2020-10-09 15:59:56 +00:00
|
|
|
let secretStorageKeys: Record<string, Uint8Array> = {};
|
2024-03-22 12:28:13 +00:00
|
|
|
let secretStorageKeyInfo: Record<string, SecretStorage.SecretStorageKeyDescription> = {};
|
2020-01-30 14:18:12 +00:00
|
|
|
let secretStorageBeingAccessed = false;
|
|
|
|
|
2020-10-09 15:59:56 +00:00
|
|
|
let dehydrationCache: {
|
2021-07-01 22:23:03 +00:00
|
|
|
key?: Uint8Array;
|
2024-03-22 12:28:13 +00:00
|
|
|
keyInfo?: SecretStorage.SecretStorageKeyDescription;
|
2020-10-09 15:59:56 +00:00
|
|
|
} = {};
|
2020-09-03 20:28:42 +00:00
|
|
|
|
2020-08-19 12:28:03 +00:00
|
|
|
/**
|
|
|
|
* This can be used by other components to check if secret storage access is in
|
|
|
|
* progress, so that we can e.g. avoid intermittently showing toasts during
|
|
|
|
* secret storage setup.
|
|
|
|
*
|
|
|
|
* @returns {bool}
|
|
|
|
*/
|
2020-10-09 15:59:56 +00:00
|
|
|
export function isSecretStorageBeingAccessed(): boolean {
|
2020-08-19 12:28:03 +00:00
|
|
|
return secretStorageBeingAccessed;
|
|
|
|
}
|
|
|
|
|
2020-01-31 10:35:05 +00:00
|
|
|
export class AccessCancelledError extends Error {
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor() {
|
2020-01-31 10:35:05 +00:00
|
|
|
super("Secret storage access canceled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 15:59:56 +00:00
|
|
|
async function confirmToDismiss(): Promise<boolean> {
|
2020-02-06 15:51:02 +00:00
|
|
|
const [sure] = await Modal.createDialog(QuestionDialog, {
|
2023-09-22 15:39:40 +00:00
|
|
|
title: _t("encryption|cancel_entering_passphrase_title"),
|
|
|
|
description: _t("encryption|cancel_entering_passphrase_description"),
|
2020-06-25 13:52:59 +00:00
|
|
|
danger: false,
|
2023-08-23 10:57:22 +00:00
|
|
|
button: _t("action|go_back"),
|
|
|
|
cancelButton: _t("action|cancel"),
|
2020-02-06 15:51:02 +00:00
|
|
|
}).finished;
|
2020-06-25 13:52:59 +00:00
|
|
|
return !sure;
|
2020-02-06 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 12:28:13 +00:00
|
|
|
function makeInputToKey(
|
|
|
|
keyInfo: SecretStorage.SecretStorageKeyDescription,
|
|
|
|
): (keyParams: KeyParams) => Promise<Uint8Array> {
|
2023-01-12 13:25:14 +00:00
|
|
|
return async ({ passphrase, recoveryKey }): Promise<Uint8Array> => {
|
2020-09-30 04:52:47 +00:00
|
|
|
if (passphrase) {
|
|
|
|
return deriveKey(passphrase, keyInfo.passphrase.salt, keyInfo.passphrase.iterations);
|
2023-03-07 13:19:18 +00:00
|
|
|
} else if (recoveryKey) {
|
2020-09-30 04:52:47 +00:00
|
|
|
return decodeRecoveryKey(recoveryKey);
|
|
|
|
}
|
2023-03-07 13:19:18 +00:00
|
|
|
throw new Error("Invalid input, passphrase or recoveryKey need to be provided");
|
2020-09-30 04:52:47 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-09 15:59:56 +00:00
|
|
|
async function getSecretStorageKey({
|
|
|
|
keys: keyInfos,
|
|
|
|
}: {
|
2024-03-22 12:28:13 +00:00
|
|
|
keys: Record<string, SecretStorage.SecretStorageKeyDescription>;
|
2020-10-09 15:59:56 +00:00
|
|
|
}): Promise<[string, Uint8Array]> {
|
2023-06-21 16:29:44 +00:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2021-02-11 21:34:15 +00:00
|
|
|
let keyId = await cli.getDefaultSecretStorageKeyId();
|
2024-03-22 12:28:13 +00:00
|
|
|
let keyInfo!: SecretStorage.SecretStorageKeyDescription;
|
2021-02-11 21:34:15 +00:00
|
|
|
if (keyId) {
|
|
|
|
// use the default SSSS key if set
|
|
|
|
keyInfo = keyInfos[keyId];
|
|
|
|
if (!keyInfo) {
|
2021-02-24 22:55:27 +00:00
|
|
|
// if the default key is not available, pretend the default key
|
|
|
|
// isn't set
|
2023-02-15 13:36:22 +00:00
|
|
|
keyId = null;
|
2021-02-11 21:34:15 +00:00
|
|
|
}
|
2021-02-24 22:55:27 +00:00
|
|
|
}
|
|
|
|
if (!keyId) {
|
2021-02-11 21:34:15 +00:00
|
|
|
// if no default SSSS key is set, fall back to a heuristic of using the
|
|
|
|
// only available key, if only one key is set
|
|
|
|
const keyInfoEntries = Object.entries(keyInfos);
|
|
|
|
if (keyInfoEntries.length > 1) {
|
|
|
|
throw new Error("Multiple storage key requests not implemented");
|
|
|
|
}
|
|
|
|
[keyId, keyInfo] = keyInfoEntries[0];
|
2019-12-05 15:20:30 +00:00
|
|
|
}
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.debug(`getSecretStorageKey: request for 4S keys [${Object.keys(keyInfos)}]: looking for key ${keyId}`);
|
2019-12-11 14:28:02 +00:00
|
|
|
|
|
|
|
// Check the in-memory cache
|
2024-07-13 10:27:59 +00:00
|
|
|
if (secretStorageBeingAccessed && secretStorageKeys[keyId]) {
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.debug(`getSecretStorageKey: returning key ${keyId} from cache`);
|
2020-08-28 11:10:17 +00:00
|
|
|
return [keyId, secretStorageKeys[keyId]];
|
2019-12-11 14:28:02 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 00:23:12 +00:00
|
|
|
if (dehydrationCache.key) {
|
2023-06-21 16:29:44 +00:00
|
|
|
if (await MatrixClientPeg.safeGet().checkSecretStorageKey(dehydrationCache.key, keyInfo)) {
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.debug("getSecretStorageKey: returning key from dehydration cache");
|
2020-10-09 15:59:56 +00:00
|
|
|
cacheSecretStorageKey(keyId, keyInfo, dehydrationCache.key);
|
2020-10-02 00:23:12 +00:00
|
|
|
return [keyId, dehydrationCache.key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 15:15:17 +00:00
|
|
|
const keyFromCustomisations = ModuleRunner.instance.extensions.cryptoSetup.getSecretStorageKey();
|
2020-10-08 15:35:17 +00:00
|
|
|
if (keyFromCustomisations) {
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.log("getSecretStorageKey: Using secret storage key from CryptoSetupExtension");
|
2020-10-08 15:35:17 +00:00
|
|
|
cacheSecretStorageKey(keyId, keyInfo, keyFromCustomisations);
|
|
|
|
return [keyId, keyFromCustomisations];
|
|
|
|
}
|
|
|
|
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.debug("getSecretStorageKey: prompting user for key");
|
2020-09-30 04:52:47 +00:00
|
|
|
const inputToKey = makeInputToKey(keyInfo);
|
2022-06-14 16:51:51 +00:00
|
|
|
const { finished } = Modal.createDialog(
|
2019-12-06 17:54:00 +00:00
|
|
|
AccessSecretStorageDialog,
|
2020-02-06 12:11:24 +00:00
|
|
|
/* props= */
|
2019-12-06 17:54:00 +00:00
|
|
|
{
|
2020-08-28 11:10:17 +00:00
|
|
|
keyInfo,
|
2023-01-12 13:25:14 +00:00
|
|
|
checkPrivateKey: async (input: KeyParams): Promise<boolean> => {
|
2019-12-06 17:54:00 +00:00
|
|
|
const key = await inputToKey(input);
|
2023-06-21 16:29:44 +00:00
|
|
|
return MatrixClientPeg.safeGet().checkSecretStorageKey(key, keyInfo);
|
2019-12-06 17:54:00 +00:00
|
|
|
},
|
|
|
|
},
|
2023-02-15 13:36:22 +00:00
|
|
|
/* className= */ undefined,
|
2020-02-06 12:11:24 +00:00
|
|
|
/* isPriorityModal= */ false,
|
|
|
|
/* isStaticModal= */ false,
|
|
|
|
/* options= */ {
|
2023-01-12 13:25:14 +00:00
|
|
|
onBeforeClose: async (reason): Promise<boolean> => {
|
2020-02-06 15:51:02 +00:00
|
|
|
if (reason === "backgroundClick") {
|
2020-07-06 14:26:40 +00:00
|
|
|
return confirmToDismiss();
|
2020-02-06 12:11:24 +00:00
|
|
|
}
|
2020-02-06 15:51:02 +00:00
|
|
|
return true;
|
2020-02-06 12:11:24 +00:00
|
|
|
},
|
|
|
|
},
|
2019-12-05 15:20:30 +00:00
|
|
|
);
|
2022-05-03 21:04:37 +00:00
|
|
|
const [keyParams] = await finished;
|
|
|
|
if (!keyParams) {
|
2020-01-31 10:35:05 +00:00
|
|
|
throw new AccessCancelledError();
|
2019-12-05 15:20:30 +00:00
|
|
|
}
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.debug("getSecretStorageKey: got key from user");
|
2022-05-03 21:04:37 +00:00
|
|
|
const key = await inputToKey(keyParams);
|
2019-12-11 14:28:02 +00:00
|
|
|
|
|
|
|
// Save to cache to avoid future prompts in the current session
|
2020-10-09 15:59:56 +00:00
|
|
|
cacheSecretStorageKey(keyId, keyInfo, key);
|
2020-08-28 11:10:17 +00:00
|
|
|
|
|
|
|
return [keyId, key];
|
|
|
|
}
|
|
|
|
|
2020-10-09 15:59:56 +00:00
|
|
|
export async function getDehydrationKey(
|
2024-03-22 12:28:13 +00:00
|
|
|
keyInfo: SecretStorage.SecretStorageKeyDescription,
|
2023-02-13 11:39:16 +00:00
|
|
|
checkFunc: (data: Uint8Array) => void,
|
2020-10-09 15:59:56 +00:00
|
|
|
): Promise<Uint8Array> {
|
2024-04-12 15:15:17 +00:00
|
|
|
const keyFromCustomisations = ModuleRunner.instance.extensions.cryptoSetup.getSecretStorageKey();
|
2020-10-08 15:35:17 +00:00
|
|
|
if (keyFromCustomisations) {
|
2024-04-12 15:15:17 +00:00
|
|
|
logger.log("CryptoSetupExtension: Using key from extension (dehydration)");
|
2020-10-08 15:35:17 +00:00
|
|
|
return keyFromCustomisations;
|
|
|
|
}
|
|
|
|
|
2020-09-30 04:52:47 +00:00
|
|
|
const inputToKey = makeInputToKey(keyInfo);
|
2022-06-14 16:51:51 +00:00
|
|
|
const { finished } = Modal.createDialog(
|
2020-09-30 04:52:47 +00:00
|
|
|
AccessSecretStorageDialog,
|
|
|
|
/* props= */
|
|
|
|
{
|
|
|
|
keyInfo,
|
2023-02-13 11:39:16 +00:00
|
|
|
checkPrivateKey: async (input: KeyParams): Promise<boolean> => {
|
2020-09-30 04:52:47 +00:00
|
|
|
const key = await inputToKey(input);
|
|
|
|
try {
|
|
|
|
checkFunc(key);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2023-02-15 13:36:22 +00:00
|
|
|
/* className= */ undefined,
|
2020-09-30 04:52:47 +00:00
|
|
|
/* isPriorityModal= */ false,
|
|
|
|
/* isStaticModal= */ false,
|
|
|
|
/* options= */ {
|
2023-01-12 13:25:14 +00:00
|
|
|
onBeforeClose: async (reason): Promise<boolean> => {
|
2020-09-30 04:52:47 +00:00
|
|
|
if (reason === "backgroundClick") {
|
|
|
|
return confirmToDismiss();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const [input] = await finished;
|
|
|
|
if (!input) {
|
|
|
|
throw new AccessCancelledError();
|
|
|
|
}
|
|
|
|
const key = await inputToKey(input);
|
2020-10-02 00:23:12 +00:00
|
|
|
|
2020-09-30 04:52:47 +00:00
|
|
|
// need to copy the key because rehydration (unpickling) will clobber it
|
2021-06-29 12:11:58 +00:00
|
|
|
dehydrationCache = { key: new Uint8Array(key), keyInfo };
|
2020-10-02 00:23:12 +00:00
|
|
|
|
2020-09-30 04:52:47 +00:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2024-03-22 12:28:13 +00:00
|
|
|
function cacheSecretStorageKey(
|
|
|
|
keyId: string,
|
|
|
|
keyInfo: SecretStorage.SecretStorageKeyDescription,
|
|
|
|
key: Uint8Array,
|
|
|
|
): void {
|
2024-07-13 10:27:59 +00:00
|
|
|
if (secretStorageBeingAccessed) {
|
2020-08-28 11:10:17 +00:00
|
|
|
secretStorageKeys[keyId] = key;
|
2020-09-30 04:52:47 +00:00
|
|
|
secretStorageKeyInfo[keyId] = keyInfo;
|
2019-12-12 15:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 15:05:03 +00:00
|
|
|
|
2020-10-09 15:59:56 +00:00
|
|
|
async function onSecretRequested(
|
|
|
|
userId: string,
|
|
|
|
deviceId: string,
|
|
|
|
requestId: string,
|
|
|
|
name: string,
|
2024-05-28 07:41:20 +00:00
|
|
|
deviceTrust: Crypto.DeviceVerificationStatus,
|
2023-02-15 13:36:22 +00:00
|
|
|
): Promise<string | undefined> {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log("onSecretRequested", userId, deviceId, requestId, name, deviceTrust);
|
2023-06-21 16:29:44 +00:00
|
|
|
const client = MatrixClientPeg.safeGet();
|
2020-03-02 16:28:10 +00:00
|
|
|
if (userId !== client.getUserId()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-26 10:12:49 +00:00
|
|
|
if (!deviceTrust?.isVerified()) {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log(`Ignoring secret request from untrusted device ${deviceId}`);
|
2020-03-02 16:28:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-07 14:54:05 +00:00
|
|
|
if (
|
|
|
|
name === "m.cross_signing.master" ||
|
|
|
|
name === "m.cross_signing.self_signing" ||
|
|
|
|
name === "m.cross_signing.user_signing"
|
|
|
|
) {
|
2020-03-25 14:06:47 +00:00
|
|
|
const callbacks = client.getCrossSigningCacheCallbacks();
|
2023-02-15 13:36:22 +00:00
|
|
|
if (!callbacks?.getCrossSigningKeyCache) return;
|
2020-08-06 14:10:47 +00:00
|
|
|
const keyId = name.replace("m.cross_signing.", "");
|
|
|
|
const key = await callbacks.getCrossSigningKeyCache(keyId);
|
|
|
|
if (!key) {
|
|
|
|
logger.log(`${keyId} requested by ${deviceId}, but not found in cache`);
|
2020-03-24 15:49:51 +00:00
|
|
|
}
|
2023-02-15 13:36:22 +00:00
|
|
|
return key ? encodeBase64(key) : undefined;
|
2020-03-25 14:06:47 +00:00
|
|
|
} else if (name === "m.megolm_backup.v1") {
|
2023-02-15 13:36:22 +00:00
|
|
|
const key = await client.crypto?.getSessionBackupPrivateKey();
|
2020-03-24 15:49:51 +00:00
|
|
|
if (!key) {
|
2020-03-25 16:08:26 +00:00
|
|
|
logger.log(`session backup key requested by ${deviceId}, but not found in cache`);
|
2020-03-24 15:49:51 +00:00
|
|
|
}
|
2023-02-15 13:36:22 +00:00
|
|
|
return key ? encodeBase64(key) : undefined;
|
2020-03-02 16:28:10 +00:00
|
|
|
}
|
2021-10-15 14:31:29 +00:00
|
|
|
logger.warn("onSecretRequested didn't recognise the secret named ", name);
|
2020-10-09 15:59:56 +00:00
|
|
|
}
|
2020-03-02 16:28:10 +00:00
|
|
|
|
2020-10-09 15:59:56 +00:00
|
|
|
export const crossSigningCallbacks: ICryptoCallbacks = {
|
2019-12-11 15:05:03 +00:00
|
|
|
getSecretStorageKey,
|
2020-08-28 11:10:17 +00:00
|
|
|
cacheSecretStorageKey,
|
2020-03-02 16:28:10 +00:00
|
|
|
onSecretRequested,
|
2020-09-30 04:52:47 +00:00
|
|
|
getDehydrationKey,
|
2019-12-11 15:05:03 +00:00
|
|
|
};
|
|
|
|
|
2023-12-15 14:59:36 +00:00
|
|
|
/**
|
|
|
|
* Carry out an operation that may require multiple accesses to secret storage, caching the key.
|
|
|
|
*
|
|
|
|
* Use this helper to wrap an operation that may require multiple accesses to secret storage; the user will be prompted
|
|
|
|
* to enter the 4S key or passphrase on the first access, and the key will be cached for the rest of the operation.
|
|
|
|
*
|
|
|
|
* @param func - The operation to be wrapped.
|
|
|
|
*/
|
|
|
|
export async function withSecretStorageKeyCache<T>(func: () => Promise<T>): Promise<T> {
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.debug("SecurityManager: enabling 4S key cache");
|
2023-12-15 14:59:36 +00:00
|
|
|
secretStorageBeingAccessed = true;
|
|
|
|
try {
|
|
|
|
return await func();
|
|
|
|
} finally {
|
|
|
|
// Clear secret storage key cache now that work is complete
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.debug("SecurityManager: disabling 4S key cache");
|
2023-12-15 14:59:36 +00:00
|
|
|
secretStorageBeingAccessed = false;
|
2024-07-13 10:27:59 +00:00
|
|
|
secretStorageKeys = {};
|
|
|
|
secretStorageKeyInfo = {};
|
2023-12-15 14:59:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 15:05:03 +00:00
|
|
|
/**
|
|
|
|
* This helper should be used whenever you need to access secret storage. It
|
|
|
|
* ensures that secret storage (and also cross-signing since they each depend on
|
|
|
|
* each other in a cycle of sorts) have been bootstrapped before running the
|
|
|
|
* provided function.
|
|
|
|
*
|
|
|
|
* Bootstrapping secret storage may take one of these paths:
|
|
|
|
* 1. Create secret storage from a passphrase and store cross-signing keys
|
|
|
|
* in secret storage.
|
|
|
|
* 2. Access existing secret storage by requesting passphrase and accessing
|
|
|
|
* cross-signing keys as needed.
|
|
|
|
* 3. All keys are loaded and there's nothing to do.
|
|
|
|
*
|
|
|
|
* Additionally, the secret storage keys are cached during the scope of this function
|
|
|
|
* to ensure the user is prompted only once for their secret storage
|
2020-01-03 13:45:52 +00:00
|
|
|
* passphrase. The cache is then cleared once the provided function completes.
|
2019-12-11 15:05:03 +00:00
|
|
|
*
|
|
|
|
* @param {Function} [func] An operation to perform once secret storage has been
|
|
|
|
* bootstrapped. Optional.
|
2020-06-18 08:35:11 +00:00
|
|
|
* @param {bool} [forceReset] Reset secret storage even if it's already set up
|
2019-12-11 15:05:03 +00:00
|
|
|
*/
|
2024-01-04 13:11:28 +00:00
|
|
|
export async function accessSecretStorage(func = async (): Promise<void> => {}, forceReset = false): Promise<void> {
|
|
|
|
await withSecretStorageKeyCache(() => doAccessSecretStorage(func, forceReset));
|
2023-12-15 14:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper for {@link #accessSecretStorage} */
|
2024-01-10 10:34:03 +00:00
|
|
|
async function doAccessSecretStorage(func: () => Promise<void>, forceReset: boolean): Promise<void> {
|
2019-12-11 15:05:03 +00:00
|
|
|
try {
|
2023-06-21 16:29:44 +00:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2024-07-13 12:36:45 +00:00
|
|
|
const crypto = cli.getCrypto();
|
|
|
|
if (!crypto) {
|
|
|
|
throw new Error("End-to-end encryption is disabled - unable to access secret storage.");
|
|
|
|
}
|
|
|
|
|
|
|
|
let createNew = false;
|
|
|
|
if (forceReset) {
|
|
|
|
logger.debug("accessSecretStorage: resetting 4S");
|
|
|
|
createNew = true;
|
|
|
|
} else if (!(await cli.secretStorage.hasKey())) {
|
|
|
|
logger.debug("accessSecretStorage: no 4S key configured, creating a new one");
|
|
|
|
createNew = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (createNew) {
|
2019-12-11 15:05:03 +00:00
|
|
|
// This dialog calls bootstrap itself after guiding the user through
|
|
|
|
// passphrase creation.
|
2022-06-14 16:51:51 +00:00
|
|
|
const { finished } = Modal.createDialogAsync(
|
2021-10-23 03:32:16 +00:00
|
|
|
import("./async-components/views/dialogs/security/CreateSecretStorageDialog") as unknown as Promise<
|
2023-02-28 10:31:48 +00:00
|
|
|
typeof CreateSecretStorageDialog
|
2021-10-23 03:32:16 +00:00
|
|
|
>,
|
2020-02-07 14:55:01 +00:00
|
|
|
{
|
2020-08-27 12:50:50 +00:00
|
|
|
forceReset,
|
2020-02-07 14:55:01 +00:00
|
|
|
},
|
2023-02-15 13:36:22 +00:00
|
|
|
undefined,
|
2020-08-14 17:06:35 +00:00
|
|
|
/* priority = */ false,
|
|
|
|
/* static = */ true,
|
|
|
|
/* options = */ {
|
2023-01-12 13:25:14 +00:00
|
|
|
onBeforeClose: async (reason): Promise<boolean> => {
|
2020-08-14 17:06:35 +00:00
|
|
|
// If Secure Backup is required, you cannot leave the modal.
|
|
|
|
if (reason === "backgroundClick") {
|
2023-05-23 15:24:12 +00:00
|
|
|
return !isSecureBackupRequired(cli);
|
2020-08-14 17:06:35 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2020-02-07 14:55:01 +00:00
|
|
|
},
|
2019-12-11 15:05:03 +00:00
|
|
|
);
|
|
|
|
const [confirmed] = await finished;
|
|
|
|
if (!confirmed) {
|
|
|
|
throw new Error("Secret storage creation canceled");
|
|
|
|
}
|
|
|
|
} else {
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.debug("accessSecretStorage: bootstrapCrossSigning");
|
2024-01-10 10:34:03 +00:00
|
|
|
await crypto.bootstrapCrossSigning({
|
2023-01-12 13:25:14 +00:00
|
|
|
authUploadDeviceSigningKeys: async (makeRequest): Promise<void> => {
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.debug("accessSecretStorage: performing UIA to upload cross-signing keys");
|
2022-06-14 16:51:51 +00:00
|
|
|
const { finished } = Modal.createDialog(InteractiveAuthDialog, {
|
2023-09-22 15:39:40 +00:00
|
|
|
title: _t("encryption|bootstrap_title"),
|
2022-06-14 16:51:51 +00:00
|
|
|
matrixClient: cli,
|
|
|
|
makeRequest,
|
|
|
|
});
|
2019-12-11 15:05:03 +00:00
|
|
|
const [confirmed] = await finished;
|
|
|
|
if (!confirmed) {
|
|
|
|
throw new Error("Cross-signing key upload auth canceled");
|
|
|
|
}
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.debug("accessSecretStorage: Cross-signing key upload successful");
|
2019-12-11 15:05:03 +00:00
|
|
|
},
|
2020-08-27 12:41:03 +00:00
|
|
|
});
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.debug("accessSecretStorage: bootstrapSecretStorage");
|
2024-07-13 10:27:59 +00:00
|
|
|
await crypto.bootstrapSecretStorage({});
|
2020-09-30 04:52:47 +00:00
|
|
|
|
|
|
|
const keyId = Object.keys(secretStorageKeys)[0];
|
2020-10-02 01:41:03 +00:00
|
|
|
if (keyId && SettingsStore.getValue("feature_dehydration")) {
|
2020-10-09 15:59:56 +00:00
|
|
|
let dehydrationKeyInfo = {};
|
|
|
|
if (secretStorageKeyInfo[keyId] && secretStorageKeyInfo[keyId].passphrase) {
|
|
|
|
dehydrationKeyInfo = { passphrase: secretStorageKeyInfo[keyId].passphrase };
|
|
|
|
}
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.log("accessSecretStorage: Setting dehydration key");
|
2020-10-02 01:41:03 +00:00
|
|
|
await cli.setDehydrationKey(secretStorageKeys[keyId], dehydrationKeyInfo, "Backup device");
|
2020-10-08 15:35:17 +00:00
|
|
|
} else if (!keyId) {
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.warn("accessSecretStorage: Not setting dehydration key: no SSSS key found");
|
2020-09-30 04:52:47 +00:00
|
|
|
} else {
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.log("accessSecretStorage: Not setting dehydration key: feature disabled");
|
2020-09-30 04:52:47 +00:00
|
|
|
}
|
2019-12-11 15:05:03 +00:00
|
|
|
}
|
|
|
|
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.debug("accessSecretStorage: 4S now ready");
|
2019-12-11 15:05:03 +00:00
|
|
|
// `return await` needed here to ensure `finally` block runs after the
|
|
|
|
// inner operation completes.
|
2024-07-13 12:36:45 +00:00
|
|
|
await func();
|
|
|
|
logger.debug("accessSecretStorage: operation complete");
|
2020-10-08 15:35:17 +00:00
|
|
|
} catch (e) {
|
2024-04-12 15:15:17 +00:00
|
|
|
ModuleRunner.instance.extensions.cryptoSetup.catchAccessSecretStorageError(e as Error);
|
2024-07-13 12:36:45 +00:00
|
|
|
logger.error("accessSecretStorage: error during operation", e);
|
2021-03-26 13:01:30 +00:00
|
|
|
// Re-throw so that higher level logic can abort as needed
|
|
|
|
throw e;
|
2019-12-11 15:05:03 +00:00
|
|
|
}
|
|
|
|
}
|