2019-12-05 23:20:30 +08:00
|
|
|
/*
|
2020-09-03 21:50:49 +08:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2019-12-05 23:20:30 +08: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.
|
|
|
|
*/
|
|
|
|
|
2021-07-08 03:19:52 +08:00
|
|
|
import { ICryptoCallbacks } from 'matrix-js-sdk/src/matrix';
|
|
|
|
import { ISecretStorageKeyInfo } from 'matrix-js-sdk/src/crypto/api';
|
2020-10-09 23:59:56 +08:00
|
|
|
import { MatrixClient } from 'matrix-js-sdk/src/client';
|
2021-12-09 17:10:23 +08:00
|
|
|
import { deriveKey } from 'matrix-js-sdk/src/crypto/key_passphrase';
|
|
|
|
import { decodeRecoveryKey } from 'matrix-js-sdk/src/crypto/recoverykey';
|
|
|
|
import { encodeBase64 } from "matrix-js-sdk/src/crypto/olmlib";
|
|
|
|
import { DeviceTrustLevel } from 'matrix-js-sdk/src/crypto/CrossSigning';
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
import { ComponentType } from "react";
|
|
|
|
|
2019-12-05 23:20:30 +08:00
|
|
|
import Modal from './Modal';
|
2021-06-29 20:11:58 +08:00
|
|
|
import { MatrixClientPeg } from './MatrixClientPeg';
|
2019-12-11 23:05:03 +08:00
|
|
|
import { _t } from './languageHandler';
|
2020-08-15 01:06:35 +08:00
|
|
|
import { isSecureBackupRequired } from './utils/WellKnownUtils';
|
2020-09-10 20:56:07 +08:00
|
|
|
import AccessSecretStorageDialog from './components/views/dialogs/security/AccessSecretStorageDialog';
|
|
|
|
import RestoreKeyBackupDialog from './components/views/dialogs/security/RestoreKeyBackupDialog';
|
2020-10-02 09:41:03 +08:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2020-10-08 23:35:17 +08:00
|
|
|
import SecurityCustomisations from "./customisations/Security";
|
2021-11-08 18:27:52 +08:00
|
|
|
import QuestionDialog from "./components/views/dialogs/QuestionDialog";
|
2022-03-03 07:33:40 +08:00
|
|
|
import InteractiveAuthDialog from "./components/views/dialogs/InteractiveAuthDialog";
|
2021-09-21 23:48:09 +08:00
|
|
|
|
2019-12-11 22:28:02 +08: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 23:05:03 +08: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 23:59:56 +08:00
|
|
|
let secretStorageKeys: Record<string, Uint8Array> = {};
|
|
|
|
let secretStorageKeyInfo: Record<string, ISecretStorageKeyInfo> = {};
|
2020-01-30 22:18:12 +08:00
|
|
|
let secretStorageBeingAccessed = false;
|
|
|
|
|
2020-10-02 08:23:12 +08:00
|
|
|
let nonInteractive = false;
|
2020-09-04 04:28:42 +08:00
|
|
|
|
2020-10-09 23:59:56 +08:00
|
|
|
let dehydrationCache: {
|
2021-07-02 06:23:03 +08:00
|
|
|
key?: Uint8Array;
|
|
|
|
keyInfo?: ISecretStorageKeyInfo;
|
2020-10-09 23:59:56 +08:00
|
|
|
} = {};
|
2020-09-04 04:28:42 +08:00
|
|
|
|
2020-10-09 23:59:56 +08:00
|
|
|
function isCachingAllowed(): boolean {
|
2020-06-18 17:42:33 +08:00
|
|
|
return secretStorageBeingAccessed;
|
2020-01-30 22:18:12 +08:00
|
|
|
}
|
2019-12-11 22:28:02 +08:00
|
|
|
|
2020-08-19 20:28:03 +08: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 23:59:56 +08:00
|
|
|
export function isSecretStorageBeingAccessed(): boolean {
|
2020-08-19 20:28:03 +08:00
|
|
|
return secretStorageBeingAccessed;
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:35:05 +08:00
|
|
|
export class AccessCancelledError extends Error {
|
|
|
|
constructor() {
|
|
|
|
super("Secret storage access canceled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 23:59:56 +08:00
|
|
|
async function confirmToDismiss(): Promise<boolean> {
|
2020-02-06 23:51:02 +08:00
|
|
|
const [sure] = await Modal.createDialog(QuestionDialog, {
|
|
|
|
title: _t("Cancel entering passphrase?"),
|
2020-07-06 22:26:40 +08:00
|
|
|
description: _t("Are you sure you want to cancel entering passphrase?"),
|
2020-06-25 21:52:59 +08:00
|
|
|
danger: false,
|
2020-07-06 22:26:40 +08:00
|
|
|
button: _t("Go Back"),
|
2020-06-25 21:52:59 +08:00
|
|
|
cancelButton: _t("Cancel"),
|
2020-02-06 23:51:02 +08:00
|
|
|
}).finished;
|
2020-06-25 21:52:59 +08:00
|
|
|
return !sure;
|
2020-02-06 23:51:02 +08:00
|
|
|
}
|
|
|
|
|
2022-05-04 05:04:37 +08:00
|
|
|
type KeyParams = { passphrase: string, recoveryKey: string };
|
|
|
|
|
2020-10-09 23:59:56 +08:00
|
|
|
function makeInputToKey(
|
|
|
|
keyInfo: ISecretStorageKeyInfo,
|
2022-05-04 05:04:37 +08:00
|
|
|
): (keyParams: KeyParams) => Promise<Uint8Array> {
|
2020-09-30 12:52:47 +08:00
|
|
|
return async ({ passphrase, recoveryKey }) => {
|
|
|
|
if (passphrase) {
|
|
|
|
return deriveKey(
|
|
|
|
passphrase,
|
|
|
|
keyInfo.passphrase.salt,
|
|
|
|
keyInfo.passphrase.iterations,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return decodeRecoveryKey(recoveryKey);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-09 23:59:56 +08:00
|
|
|
async function getSecretStorageKey(
|
|
|
|
{ keys: keyInfos }: { keys: Record<string, ISecretStorageKeyInfo> },
|
|
|
|
): Promise<[string, Uint8Array]> {
|
2021-02-12 05:34:15 +08:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
let keyId = await cli.getDefaultSecretStorageKeyId();
|
2022-05-04 05:04:37 +08:00
|
|
|
let keyInfo: ISecretStorageKeyInfo;
|
2021-02-12 05:34:15 +08:00
|
|
|
if (keyId) {
|
|
|
|
// use the default SSSS key if set
|
|
|
|
keyInfo = keyInfos[keyId];
|
|
|
|
if (!keyInfo) {
|
2021-02-25 06:55:27 +08:00
|
|
|
// if the default key is not available, pretend the default key
|
|
|
|
// isn't set
|
|
|
|
keyId = undefined;
|
2021-02-12 05:34:15 +08:00
|
|
|
}
|
2021-02-25 06:55:27 +08:00
|
|
|
}
|
|
|
|
if (!keyId) {
|
2021-02-12 05:34:15 +08: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 23:20:30 +08:00
|
|
|
}
|
2019-12-11 22:28:02 +08:00
|
|
|
|
|
|
|
// Check the in-memory cache
|
2020-08-28 19:10:17 +08:00
|
|
|
if (isCachingAllowed() && secretStorageKeys[keyId]) {
|
|
|
|
return [keyId, secretStorageKeys[keyId]];
|
2019-12-11 22:28:02 +08:00
|
|
|
}
|
|
|
|
|
2020-10-02 08:23:12 +08:00
|
|
|
if (dehydrationCache.key) {
|
|
|
|
if (await MatrixClientPeg.get().checkSecretStorageKey(dehydrationCache.key, keyInfo)) {
|
2020-10-09 23:59:56 +08:00
|
|
|
cacheSecretStorageKey(keyId, keyInfo, dehydrationCache.key);
|
2020-10-02 08:23:12 +08:00
|
|
|
return [keyId, dehydrationCache.key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 23:35:17 +08:00
|
|
|
const keyFromCustomisations = SecurityCustomisations.getSecretStorageKey?.();
|
|
|
|
if (keyFromCustomisations) {
|
2021-09-21 23:48:09 +08:00
|
|
|
logger.log("Using key from security customisations (secret storage)");
|
2020-10-08 23:35:17 +08:00
|
|
|
cacheSecretStorageKey(keyId, keyInfo, keyFromCustomisations);
|
|
|
|
return [keyId, keyFromCustomisations];
|
|
|
|
}
|
|
|
|
|
2020-10-02 08:23:12 +08:00
|
|
|
if (nonInteractive) {
|
|
|
|
throw new Error("Could not unlock non-interactively");
|
2020-09-04 04:28:42 +08:00
|
|
|
}
|
|
|
|
|
2020-09-30 12:52:47 +08:00
|
|
|
const inputToKey = makeInputToKey(keyInfo);
|
2019-12-05 23:20:30 +08:00
|
|
|
const { finished } = Modal.createTrackedDialog("Access Secret Storage dialog", "",
|
2019-12-07 01:54:00 +08:00
|
|
|
AccessSecretStorageDialog,
|
2020-02-06 20:11:24 +08:00
|
|
|
/* props= */
|
2019-12-07 01:54:00 +08:00
|
|
|
{
|
2020-08-28 19:10:17 +08:00
|
|
|
keyInfo,
|
2022-05-04 05:04:37 +08:00
|
|
|
checkPrivateKey: async (input: KeyParams) => {
|
2019-12-07 01:54:00 +08:00
|
|
|
const key = await inputToKey(input);
|
2022-05-04 05:04:37 +08:00
|
|
|
return MatrixClientPeg.get().checkSecretStorageKey(key, keyInfo);
|
2019-12-07 01:54:00 +08:00
|
|
|
},
|
|
|
|
},
|
2020-02-06 20:11:24 +08:00
|
|
|
/* className= */ null,
|
|
|
|
/* isPriorityModal= */ false,
|
|
|
|
/* isStaticModal= */ false,
|
|
|
|
/* options= */ {
|
|
|
|
onBeforeClose: async (reason) => {
|
2020-02-06 23:51:02 +08:00
|
|
|
if (reason === "backgroundClick") {
|
2020-07-06 22:26:40 +08:00
|
|
|
return confirmToDismiss();
|
2020-02-06 20:11:24 +08:00
|
|
|
}
|
2020-02-06 23:51:02 +08:00
|
|
|
return true;
|
2020-02-06 20:11:24 +08:00
|
|
|
},
|
|
|
|
},
|
2019-12-05 23:20:30 +08:00
|
|
|
);
|
2022-05-04 05:04:37 +08:00
|
|
|
const [keyParams] = await finished;
|
|
|
|
if (!keyParams) {
|
2020-01-31 18:35:05 +08:00
|
|
|
throw new AccessCancelledError();
|
2019-12-05 23:20:30 +08:00
|
|
|
}
|
2022-05-04 05:04:37 +08:00
|
|
|
const key = await inputToKey(keyParams);
|
2019-12-11 22:28:02 +08:00
|
|
|
|
|
|
|
// Save to cache to avoid future prompts in the current session
|
2020-10-09 23:59:56 +08:00
|
|
|
cacheSecretStorageKey(keyId, keyInfo, key);
|
2020-08-28 19:10:17 +08:00
|
|
|
|
|
|
|
return [keyId, key];
|
|
|
|
}
|
|
|
|
|
2020-10-09 23:59:56 +08:00
|
|
|
export async function getDehydrationKey(
|
|
|
|
keyInfo: ISecretStorageKeyInfo,
|
|
|
|
checkFunc: (Uint8Array) => void,
|
|
|
|
): Promise<Uint8Array> {
|
2020-10-08 23:35:17 +08:00
|
|
|
const keyFromCustomisations = SecurityCustomisations.getSecretStorageKey?.();
|
|
|
|
if (keyFromCustomisations) {
|
2021-09-21 23:48:09 +08:00
|
|
|
logger.log("Using key from security customisations (dehydration)");
|
2020-10-08 23:35:17 +08:00
|
|
|
return keyFromCustomisations;
|
|
|
|
}
|
|
|
|
|
2020-09-30 12:52:47 +08:00
|
|
|
const inputToKey = makeInputToKey(keyInfo);
|
|
|
|
const { finished } = Modal.createTrackedDialog("Access Secret Storage dialog", "",
|
|
|
|
AccessSecretStorageDialog,
|
|
|
|
/* props= */
|
|
|
|
{
|
|
|
|
keyInfo,
|
|
|
|
checkPrivateKey: async (input) => {
|
|
|
|
const key = await inputToKey(input);
|
|
|
|
try {
|
|
|
|
checkFunc(key);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
/* className= */ null,
|
|
|
|
/* isPriorityModal= */ false,
|
|
|
|
/* isStaticModal= */ false,
|
|
|
|
/* options= */ {
|
|
|
|
onBeforeClose: async (reason) => {
|
|
|
|
if (reason === "backgroundClick") {
|
|
|
|
return confirmToDismiss();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const [input] = await finished;
|
|
|
|
if (!input) {
|
|
|
|
throw new AccessCancelledError();
|
|
|
|
}
|
|
|
|
const key = await inputToKey(input);
|
2020-10-02 08:23:12 +08:00
|
|
|
|
2020-09-30 12:52:47 +08:00
|
|
|
// need to copy the key because rehydration (unpickling) will clobber it
|
2021-06-29 20:11:58 +08:00
|
|
|
dehydrationCache = { key: new Uint8Array(key), keyInfo };
|
2020-10-02 08:23:12 +08:00
|
|
|
|
2020-09-30 12:52:47 +08:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2020-10-09 23:59:56 +08:00
|
|
|
function cacheSecretStorageKey(
|
|
|
|
keyId: string,
|
|
|
|
keyInfo: ISecretStorageKeyInfo,
|
|
|
|
key: Uint8Array,
|
|
|
|
): void {
|
2020-01-30 22:18:12 +08:00
|
|
|
if (isCachingAllowed()) {
|
2020-08-28 19:10:17 +08:00
|
|
|
secretStorageKeys[keyId] = key;
|
2020-09-30 12:52:47 +08:00
|
|
|
secretStorageKeyInfo[keyId] = keyInfo;
|
2019-12-12 23:34:01 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 23:05:03 +08:00
|
|
|
|
2020-10-09 23:59:56 +08:00
|
|
|
async function onSecretRequested(
|
|
|
|
userId: string,
|
|
|
|
deviceId: string,
|
|
|
|
requestId: string,
|
|
|
|
name: string,
|
2021-06-23 21:47:24 +08:00
|
|
|
deviceTrust: DeviceTrustLevel,
|
2020-10-09 23:59:56 +08:00
|
|
|
): Promise<string> {
|
2021-09-21 23:48:09 +08:00
|
|
|
logger.log("onSecretRequested", userId, deviceId, requestId, name, deviceTrust);
|
2020-03-03 00:28:10 +08:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (userId !== client.getUserId()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!deviceTrust || !deviceTrust.isVerified()) {
|
2021-09-21 23:48:09 +08:00
|
|
|
logger.log(`Ignoring secret request from untrusted device ${deviceId}`);
|
2020-03-03 00:28:10 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-08-07 22:54:05 +08:00
|
|
|
if (
|
|
|
|
name === "m.cross_signing.master" ||
|
|
|
|
name === "m.cross_signing.self_signing" ||
|
|
|
|
name === "m.cross_signing.user_signing"
|
|
|
|
) {
|
2020-03-25 22:06:47 +08:00
|
|
|
const callbacks = client.getCrossSigningCacheCallbacks();
|
|
|
|
if (!callbacks.getCrossSigningKeyCache) return;
|
2020-08-06 22:10:47 +08:00
|
|
|
const keyId = name.replace("m.cross_signing.", "");
|
|
|
|
const key = await callbacks.getCrossSigningKeyCache(keyId);
|
|
|
|
if (!key) {
|
2021-09-21 23:48:09 +08:00
|
|
|
logger.log(
|
2020-08-06 22:10:47 +08:00
|
|
|
`${keyId} requested by ${deviceId}, but not found in cache`,
|
|
|
|
);
|
2020-03-24 23:49:51 +08:00
|
|
|
}
|
2020-08-06 22:10:47 +08:00
|
|
|
return key && encodeBase64(key);
|
2020-03-25 22:06:47 +08:00
|
|
|
} else if (name === "m.megolm_backup.v1") {
|
2021-06-02 11:36:28 +08:00
|
|
|
const key = await client.crypto.getSessionBackupPrivateKey();
|
2020-03-24 23:49:51 +08:00
|
|
|
if (!key) {
|
2021-09-21 23:48:09 +08:00
|
|
|
logger.log(
|
2020-03-26 00:08:26 +08:00
|
|
|
`session backup key requested by ${deviceId}, but not found in cache`,
|
2020-03-25 22:06:47 +08:00
|
|
|
);
|
2020-03-24 23:49:51 +08:00
|
|
|
}
|
2020-03-03 00:28:10 +08:00
|
|
|
return key && encodeBase64(key);
|
|
|
|
}
|
2021-10-15 22:31:29 +08:00
|
|
|
logger.warn("onSecretRequested didn't recognise the secret named ", name);
|
2020-10-09 23:59:56 +08:00
|
|
|
}
|
2020-03-03 00:28:10 +08:00
|
|
|
|
2020-10-09 23:59:56 +08:00
|
|
|
export const crossSigningCallbacks: ICryptoCallbacks = {
|
2019-12-11 23:05:03 +08:00
|
|
|
getSecretStorageKey,
|
2020-08-28 19:10:17 +08:00
|
|
|
cacheSecretStorageKey,
|
2020-03-03 00:28:10 +08:00
|
|
|
onSecretRequested,
|
2020-09-30 12:52:47 +08:00
|
|
|
getDehydrationKey,
|
2019-12-11 23:05:03 +08:00
|
|
|
};
|
|
|
|
|
2020-10-09 23:59:56 +08:00
|
|
|
export async function promptForBackupPassphrase(): Promise<Uint8Array> {
|
2020-03-20 04:42:16 +08:00
|
|
|
let key;
|
|
|
|
|
|
|
|
const { finished } = Modal.createTrackedDialog('Restore Backup', '', RestoreKeyBackupDialog, {
|
2020-04-10 00:30:10 +08:00
|
|
|
showSummary: false, keyCallback: k => key = k,
|
2020-03-24 03:04:59 +08:00
|
|
|
}, null, /* priority = */ false, /* static = */ true);
|
2020-03-20 04:42:16 +08:00
|
|
|
|
|
|
|
const success = await finished;
|
|
|
|
if (!success) throw new Error("Key backup prompt cancelled");
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2019-12-11 23:05:03 +08: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 21:45:52 +08:00
|
|
|
* passphrase. The cache is then cleared once the provided function completes.
|
2019-12-11 23:05:03 +08:00
|
|
|
*
|
|
|
|
* @param {Function} [func] An operation to perform once secret storage has been
|
|
|
|
* bootstrapped. Optional.
|
2020-06-18 16:35:11 +08:00
|
|
|
* @param {bool} [forceReset] Reset secret storage even if it's already set up
|
2019-12-11 23:05:03 +08:00
|
|
|
*/
|
2020-06-18 16:35:11 +08:00
|
|
|
export async function accessSecretStorage(func = async () => { }, forceReset = false) {
|
2019-12-11 23:05:03 +08:00
|
|
|
const cli = MatrixClientPeg.get();
|
2020-01-30 22:18:12 +08:00
|
|
|
secretStorageBeingAccessed = true;
|
2019-12-11 23:05:03 +08:00
|
|
|
try {
|
2021-09-21 23:48:09 +08:00
|
|
|
if (!(await cli.hasSecretStorageKey()) || forceReset) {
|
2019-12-11 23:05:03 +08:00
|
|
|
// This dialog calls bootstrap itself after guiding the user through
|
|
|
|
// passphrase creation.
|
|
|
|
const { finished } = Modal.createTrackedDialogAsync('Create Secret Storage dialog', '',
|
2021-10-23 11:32:16 +08:00
|
|
|
import(
|
|
|
|
"./async-components/views/dialogs/security/CreateSecretStorageDialog"
|
|
|
|
) as unknown as Promise<ComponentType<{}>>,
|
2020-02-07 22:55:01 +08:00
|
|
|
{
|
2020-08-27 20:50:50 +08:00
|
|
|
forceReset,
|
2020-02-07 22:55:01 +08:00
|
|
|
},
|
2020-08-15 01:06:35 +08:00
|
|
|
null,
|
|
|
|
/* priority = */ false,
|
|
|
|
/* static = */ true,
|
|
|
|
/* options = */ {
|
2020-10-09 23:59:56 +08:00
|
|
|
onBeforeClose: async (reason) => {
|
2020-08-15 01:06:35 +08:00
|
|
|
// If Secure Backup is required, you cannot leave the modal.
|
|
|
|
if (reason === "backgroundClick") {
|
|
|
|
return !isSecureBackupRequired();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2020-02-07 22:55:01 +08:00
|
|
|
},
|
2019-12-11 23:05:03 +08:00
|
|
|
);
|
|
|
|
const [confirmed] = await finished;
|
|
|
|
if (!confirmed) {
|
|
|
|
throw new Error("Secret storage creation canceled");
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-27 20:41:03 +08:00
|
|
|
await cli.bootstrapCrossSigning({
|
2019-12-11 23:05:03 +08:00
|
|
|
authUploadDeviceSigningKeys: async (makeRequest) => {
|
|
|
|
const { finished } = Modal.createTrackedDialog(
|
|
|
|
'Cross-signing keys dialog', '', InteractiveAuthDialog,
|
|
|
|
{
|
2020-01-31 23:04:51 +08:00
|
|
|
title: _t("Setting up keys"),
|
2020-09-09 01:01:56 +08:00
|
|
|
matrixClient: cli,
|
2019-12-11 23:05:03 +08:00
|
|
|
makeRequest,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const [confirmed] = await finished;
|
|
|
|
if (!confirmed) {
|
|
|
|
throw new Error("Cross-signing key upload auth canceled");
|
|
|
|
}
|
|
|
|
},
|
2020-08-27 20:41:03 +08:00
|
|
|
});
|
|
|
|
await cli.bootstrapSecretStorage({
|
|
|
|
getKeyBackupPassphrase: promptForBackupPassphrase,
|
2019-12-11 23:05:03 +08:00
|
|
|
});
|
2020-09-30 12:52:47 +08:00
|
|
|
|
|
|
|
const keyId = Object.keys(secretStorageKeys)[0];
|
2020-10-02 09:41:03 +08:00
|
|
|
if (keyId && SettingsStore.getValue("feature_dehydration")) {
|
2020-10-09 23:59:56 +08:00
|
|
|
let dehydrationKeyInfo = {};
|
|
|
|
if (secretStorageKeyInfo[keyId] && secretStorageKeyInfo[keyId].passphrase) {
|
|
|
|
dehydrationKeyInfo = { passphrase: secretStorageKeyInfo[keyId].passphrase };
|
|
|
|
}
|
2021-09-21 23:48:09 +08:00
|
|
|
logger.log("Setting dehydration key");
|
2020-10-02 09:41:03 +08:00
|
|
|
await cli.setDehydrationKey(secretStorageKeys[keyId], dehydrationKeyInfo, "Backup device");
|
2020-10-08 23:35:17 +08:00
|
|
|
} else if (!keyId) {
|
2021-10-15 22:31:29 +08:00
|
|
|
logger.warn("Not setting dehydration key: no SSSS key found");
|
2020-09-30 12:52:47 +08:00
|
|
|
} else {
|
2021-09-21 23:48:09 +08:00
|
|
|
logger.log("Not setting dehydration key: feature disabled");
|
2020-09-30 12:52:47 +08:00
|
|
|
}
|
2019-12-11 23:05:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// `return await` needed here to ensure `finally` block runs after the
|
|
|
|
// inner operation completes.
|
|
|
|
return await func();
|
2020-10-08 23:35:17 +08:00
|
|
|
} catch (e) {
|
|
|
|
SecurityCustomisations.catchAccessSecretStorageError?.(e);
|
2021-10-15 22:30:53 +08:00
|
|
|
logger.error(e);
|
2021-03-26 21:01:30 +08:00
|
|
|
// Re-throw so that higher level logic can abort as needed
|
|
|
|
throw e;
|
2019-12-11 23:05:03 +08:00
|
|
|
} finally {
|
|
|
|
// Clear secret storage key cache now that work is complete
|
2020-01-30 22:18:12 +08:00
|
|
|
secretStorageBeingAccessed = false;
|
|
|
|
if (!isCachingAllowed()) {
|
|
|
|
secretStorageKeys = {};
|
2020-09-30 12:52:47 +08:00
|
|
|
secretStorageKeyInfo = {};
|
2020-01-30 22:18:12 +08:00
|
|
|
}
|
2019-12-11 23:05:03 +08:00
|
|
|
}
|
|
|
|
}
|
2020-10-02 08:23:12 +08:00
|
|
|
|
|
|
|
// FIXME: this function name is a bit of a mouthful
|
2020-10-09 23:59:56 +08:00
|
|
|
export async function tryToUnlockSecretStorageWithDehydrationKey(
|
|
|
|
client: MatrixClient,
|
|
|
|
): Promise<void> {
|
2020-10-02 08:23:12 +08:00
|
|
|
const key = dehydrationCache.key;
|
|
|
|
let restoringBackup = false;
|
2021-09-21 23:48:09 +08:00
|
|
|
if (key && (await client.isSecretStorageReady())) {
|
|
|
|
logger.log("Trying to set up cross-signing using dehydration key");
|
2020-10-02 08:23:12 +08:00
|
|
|
secretStorageBeingAccessed = true;
|
|
|
|
nonInteractive = true;
|
|
|
|
try {
|
|
|
|
await client.checkOwnCrossSigningTrust();
|
|
|
|
|
|
|
|
// we also need to set a new dehydrated device to replace the
|
|
|
|
// device we rehydrated
|
2020-10-09 23:59:56 +08:00
|
|
|
let dehydrationKeyInfo = {};
|
|
|
|
if (dehydrationCache.keyInfo && dehydrationCache.keyInfo.passphrase) {
|
|
|
|
dehydrationKeyInfo = { passphrase: dehydrationCache.keyInfo.passphrase };
|
|
|
|
}
|
2020-10-02 09:41:03 +08:00
|
|
|
await client.setDehydrationKey(key, dehydrationKeyInfo, "Backup device");
|
2020-10-02 08:23:12 +08:00
|
|
|
|
|
|
|
// and restore from backup
|
|
|
|
const backupInfo = await client.getKeyBackupVersion();
|
|
|
|
if (backupInfo) {
|
|
|
|
restoringBackup = true;
|
|
|
|
// don't await, because this can take a long time
|
|
|
|
client.restoreKeyBackupWithSecretStorage(backupInfo)
|
|
|
|
.finally(() => {
|
|
|
|
secretStorageBeingAccessed = false;
|
|
|
|
nonInteractive = false;
|
|
|
|
if (!isCachingAllowed()) {
|
|
|
|
secretStorageKeys = {};
|
|
|
|
secretStorageKeyInfo = {};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
dehydrationCache = {};
|
|
|
|
// the secret storage cache is needed for restoring from backup, so
|
|
|
|
// don't clear it yet if we're restoring from backup
|
|
|
|
if (!restoringBackup) {
|
|
|
|
secretStorageBeingAccessed = false;
|
|
|
|
nonInteractive = false;
|
|
|
|
if (!isCachingAllowed()) {
|
|
|
|
secretStorageKeys = {};
|
|
|
|
secretStorageKeyInfo = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|