2020-03-24 17:57:04 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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 EventEmitter from 'events';
|
2021-06-14 22:58:20 +03:00
|
|
|
import { VerificationRequest } from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
|
2021-06-24 23:22:56 +03:00
|
|
|
import { IKeyBackupInfo } from "matrix-js-sdk/src/crypto/keybackup";
|
2021-07-07 22:19:52 +03:00
|
|
|
import { ISecretStorageKeyInfo } from "matrix-js-sdk/src/crypto/api";
|
2021-06-24 23:22:56 +03:00
|
|
|
import { PHASE_DONE as VERIF_PHASE_DONE } from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
|
|
|
|
|
2020-03-24 17:57:04 +03:00
|
|
|
import { MatrixClientPeg } from '../MatrixClientPeg';
|
2020-09-03 16:50:49 +03:00
|
|
|
import { accessSecretStorage, AccessCancelledError } from '../SecurityManager';
|
2020-03-24 17:57:04 +03:00
|
|
|
|
2021-06-18 14:44:15 +03:00
|
|
|
export enum Phase {
|
|
|
|
Loading = 0,
|
|
|
|
Intro = 1,
|
|
|
|
Busy = 2,
|
|
|
|
Done = 3, // final done stage, but still showing UX
|
|
|
|
ConfirmSkip = 4,
|
|
|
|
Finished = 5, // UX can be closed
|
2021-06-14 22:58:20 +03:00
|
|
|
}
|
2020-03-24 17:57:04 +03:00
|
|
|
|
|
|
|
export class SetupEncryptionStore extends EventEmitter {
|
2021-06-14 22:58:20 +03:00
|
|
|
private started: boolean;
|
2021-06-18 14:44:15 +03:00
|
|
|
public phase: Phase;
|
2021-06-14 22:58:20 +03:00
|
|
|
public verificationRequest: VerificationRequest;
|
2021-06-24 23:22:56 +03:00
|
|
|
public backupInfo: IKeyBackupInfo;
|
2021-06-14 22:58:20 +03:00
|
|
|
public keyId: string;
|
|
|
|
public keyInfo: ISecretStorageKeyInfo;
|
|
|
|
public hasDevicesToVerifyAgainst: boolean;
|
|
|
|
|
|
|
|
public static sharedInstance() {
|
|
|
|
if (!window.mxSetupEncryptionStore) window.mxSetupEncryptionStore = new SetupEncryptionStore();
|
|
|
|
return window.mxSetupEncryptionStore;
|
2020-03-24 17:57:04 +03:00
|
|
|
}
|
|
|
|
|
2021-06-14 22:58:20 +03:00
|
|
|
public start(): void {
|
|
|
|
if (this.started) {
|
2020-03-24 17:57:04 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-06-14 22:58:20 +03:00
|
|
|
this.started = true;
|
2021-06-18 14:44:15 +03:00
|
|
|
this.phase = Phase.Loading;
|
2020-03-24 17:57:04 +03:00
|
|
|
this.verificationRequest = null;
|
|
|
|
this.backupInfo = null;
|
2020-06-02 18:32:15 +03:00
|
|
|
|
|
|
|
// ID of the key that the secrets we want are encrypted with
|
|
|
|
this.keyId = null;
|
|
|
|
// Descriptor of the key that the secrets we want are encrypted with
|
|
|
|
this.keyInfo = null;
|
|
|
|
|
2020-06-15 19:41:22 +03:00
|
|
|
const cli = MatrixClientPeg.get();
|
2020-06-19 19:18:48 +03:00
|
|
|
cli.on("crypto.verification.request", this.onVerificationRequest);
|
2021-06-14 22:58:20 +03:00
|
|
|
cli.on('userTrustStatusChanged', this.onUserTrustStatusChanged);
|
2020-06-19 19:18:48 +03:00
|
|
|
|
2020-06-15 19:41:22 +03:00
|
|
|
const requestsInProgress = cli.getVerificationRequestsToDeviceInProgress(cli.getUserId());
|
|
|
|
if (requestsInProgress.length) {
|
2020-06-16 16:53:13 +03:00
|
|
|
// If there are multiple, we take the most recent. Equally if the user sends another request from
|
2020-06-15 19:46:22 +03:00
|
|
|
// another device after this screen has been shown, we'll switch to the new one, so this
|
|
|
|
// generally doesn't support multiple requests.
|
2021-06-14 22:58:20 +03:00
|
|
|
this.setActiveVerificationRequest(requestsInProgress[requestsInProgress.length - 1]);
|
2020-06-15 19:41:22 +03:00
|
|
|
}
|
2020-06-02 18:32:15 +03:00
|
|
|
|
|
|
|
this.fetchKeyInfo();
|
2020-03-24 17:57:04 +03:00
|
|
|
}
|
|
|
|
|
2021-06-14 22:58:20 +03:00
|
|
|
public stop(): void {
|
|
|
|
if (!this.started) {
|
2020-03-24 17:57:04 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-06-14 22:58:20 +03:00
|
|
|
this.started = false;
|
2020-03-24 17:57:04 +03:00
|
|
|
if (this.verificationRequest) {
|
|
|
|
this.verificationRequest.off("change", this.onVerificationRequestChange);
|
|
|
|
}
|
|
|
|
if (MatrixClientPeg.get()) {
|
|
|
|
MatrixClientPeg.get().removeListener("crypto.verification.request", this.onVerificationRequest);
|
2021-06-14 22:58:20 +03:00
|
|
|
MatrixClientPeg.get().removeListener('userTrustStatusChanged', this.onUserTrustStatusChanged);
|
2020-03-24 17:57:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 22:58:20 +03:00
|
|
|
public async fetchKeyInfo(): Promise<void> {
|
2021-03-08 07:49:59 +03:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
const keys = await cli.isSecretStored('m.cross_signing.master', false);
|
2020-06-19 22:07:20 +03:00
|
|
|
if (keys === null || Object.keys(keys).length === 0) {
|
2020-06-02 18:32:15 +03:00
|
|
|
this.keyId = null;
|
|
|
|
this.keyInfo = null;
|
|
|
|
} else {
|
|
|
|
// If the secret is stored under more than one key, we just pick an arbitrary one
|
|
|
|
this.keyId = Object.keys(keys)[0];
|
|
|
|
this.keyInfo = keys[this.keyId];
|
|
|
|
}
|
|
|
|
|
2021-03-08 07:49:59 +03:00
|
|
|
// do we have any other devices which are E2EE which we can verify against?
|
|
|
|
const dehydratedDevice = await cli.getDehydratedDevice();
|
|
|
|
this.hasDevicesToVerifyAgainst = cli.getStoredDevicesForUser(cli.getUserId()).some(
|
|
|
|
device =>
|
|
|
|
device.getIdentityKey() &&
|
2021-03-08 08:08:01 +03:00
|
|
|
(!dehydratedDevice || (device.deviceId != dehydratedDevice.device_id)),
|
2021-03-08 07:49:59 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!this.hasDevicesToVerifyAgainst && !this.keyInfo) {
|
|
|
|
// skip before we can even render anything.
|
2021-06-18 14:44:15 +03:00
|
|
|
this.phase = Phase.Finished;
|
2021-03-08 08:08:01 +03:00
|
|
|
} else {
|
2021-06-18 14:44:15 +03:00
|
|
|
this.phase = Phase.Intro;
|
2021-03-08 07:49:59 +03:00
|
|
|
}
|
2020-06-02 18:32:15 +03:00
|
|
|
this.emit("update");
|
|
|
|
}
|
|
|
|
|
2021-06-14 22:58:20 +03:00
|
|
|
public async usePassPhrase(): Promise<void> {
|
2021-06-18 14:44:15 +03:00
|
|
|
this.phase = Phase.Busy;
|
2020-03-24 17:57:04 +03:00
|
|
|
this.emit("update");
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
try {
|
|
|
|
const backupInfo = await cli.getKeyBackupVersion();
|
|
|
|
this.backupInfo = backupInfo;
|
|
|
|
this.emit("update");
|
|
|
|
// The control flow is fairly twisted here...
|
|
|
|
// For the purposes of completing security, we only wait on getting
|
|
|
|
// as far as the trust check and then show a green shield.
|
|
|
|
// We also begin the key backup restore as well, which we're
|
|
|
|
// awaiting inside `accessSecretStorage` only so that it keeps your
|
|
|
|
// passphase cached for that work. This dialog itself will only wait
|
|
|
|
// on the first trust check, and the key backup restore will happen
|
|
|
|
// in the background.
|
2021-06-14 22:58:20 +03:00
|
|
|
await new Promise((resolve: (value?: unknown) => void, reject: (reason?: any) => void) => {
|
2021-03-26 16:01:30 +03:00
|
|
|
accessSecretStorage(async () => {
|
|
|
|
await cli.checkOwnCrossSigningTrust();
|
|
|
|
resolve();
|
|
|
|
if (backupInfo) {
|
|
|
|
// A complete restore can take many minutes for large
|
|
|
|
// accounts / slow servers, so we allow the dialog
|
|
|
|
// to advance before this.
|
|
|
|
await cli.restoreKeyBackupWithSecretStorage(backupInfo);
|
|
|
|
}
|
|
|
|
}).catch(reject);
|
2020-03-24 17:57:04 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (cli.getCrossSigningId()) {
|
2021-06-18 14:44:15 +03:00
|
|
|
this.phase = Phase.Done;
|
2020-03-24 17:57:04 +03:00
|
|
|
this.emit("update");
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (!(e instanceof AccessCancelledError)) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
// this will throw if the user hits cancel, so ignore
|
2021-06-18 14:44:15 +03:00
|
|
|
this.phase = Phase.Intro;
|
2020-03-24 17:57:04 +03:00
|
|
|
this.emit("update");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 22:58:20 +03:00
|
|
|
private onUserTrustStatusChanged = (userId: string) => {
|
2020-05-01 00:08:00 +03:00
|
|
|
if (userId !== MatrixClientPeg.get().getUserId()) return;
|
2020-09-02 16:13:36 +03:00
|
|
|
const publicKeysTrusted = MatrixClientPeg.get().getCrossSigningId();
|
|
|
|
if (publicKeysTrusted) {
|
2021-06-18 14:44:15 +03:00
|
|
|
this.phase = Phase.Done;
|
2020-05-01 00:08:00 +03:00
|
|
|
this.emit("update");
|
|
|
|
}
|
2021-06-29 15:11:58 +03:00
|
|
|
};
|
2020-05-01 00:08:00 +03:00
|
|
|
|
2021-06-14 22:58:20 +03:00
|
|
|
public onVerificationRequest = (request: VerificationRequest): void => {
|
|
|
|
this.setActiveVerificationRequest(request);
|
2021-06-29 15:11:58 +03:00
|
|
|
};
|
2020-03-24 17:57:04 +03:00
|
|
|
|
2021-06-14 22:58:20 +03:00
|
|
|
public onVerificationRequestChange = (): void => {
|
2020-03-24 17:57:04 +03:00
|
|
|
if (this.verificationRequest.cancelled) {
|
|
|
|
this.verificationRequest.off("change", this.onVerificationRequestChange);
|
|
|
|
this.verificationRequest = null;
|
|
|
|
this.emit("update");
|
2020-05-01 00:08:00 +03:00
|
|
|
} else if (this.verificationRequest.phase === VERIF_PHASE_DONE) {
|
|
|
|
this.verificationRequest.off("change", this.onVerificationRequestChange);
|
|
|
|
this.verificationRequest = null;
|
|
|
|
// At this point, the verification has finished, we just need to wait for
|
|
|
|
// cross signing to be ready to use, so wait for the user trust status to
|
2020-05-01 12:58:00 +03:00
|
|
|
// change (or change to DONE if it's already ready).
|
2020-09-02 16:13:36 +03:00
|
|
|
const publicKeysTrusted = MatrixClientPeg.get().getCrossSigningId();
|
2021-06-18 14:44:15 +03:00
|
|
|
this.phase = publicKeysTrusted ? Phase.Done : Phase.Busy;
|
2020-05-01 00:08:00 +03:00
|
|
|
this.emit("update");
|
2020-03-24 17:57:04 +03:00
|
|
|
}
|
2021-06-29 15:11:58 +03:00
|
|
|
};
|
2020-03-24 17:57:04 +03:00
|
|
|
|
2021-06-14 22:58:20 +03:00
|
|
|
public skip(): void {
|
2021-06-18 14:44:15 +03:00
|
|
|
this.phase = Phase.ConfirmSkip;
|
2020-03-24 17:57:04 +03:00
|
|
|
this.emit("update");
|
|
|
|
}
|
|
|
|
|
2021-06-14 22:58:20 +03:00
|
|
|
public skipConfirm(): void {
|
2021-06-18 14:44:15 +03:00
|
|
|
this.phase = Phase.Finished;
|
2020-03-24 17:57:04 +03:00
|
|
|
this.emit("update");
|
|
|
|
}
|
|
|
|
|
2021-06-14 22:58:20 +03:00
|
|
|
public returnAfterSkip(): void {
|
2021-06-18 14:44:15 +03:00
|
|
|
this.phase = Phase.Intro;
|
2020-03-24 17:57:04 +03:00
|
|
|
this.emit("update");
|
|
|
|
}
|
|
|
|
|
2021-06-14 22:58:20 +03:00
|
|
|
public done(): void {
|
2021-06-18 14:44:15 +03:00
|
|
|
this.phase = Phase.Finished;
|
2020-03-24 17:57:04 +03:00
|
|
|
this.emit("update");
|
2020-04-03 13:49:08 +03:00
|
|
|
// async - ask other clients for keys, if necessary
|
2021-06-02 06:36:28 +03:00
|
|
|
MatrixClientPeg.get().crypto.cancelAndResendAllOutgoingKeyRequests();
|
2020-03-24 17:57:04 +03:00
|
|
|
}
|
2020-06-15 19:41:22 +03:00
|
|
|
|
2021-06-14 22:58:20 +03:00
|
|
|
private async setActiveVerificationRequest(request: VerificationRequest): Promise<void> {
|
2020-06-15 19:41:22 +03:00
|
|
|
if (request.otherUserId !== MatrixClientPeg.get().getUserId()) return;
|
|
|
|
|
|
|
|
if (this.verificationRequest) {
|
|
|
|
this.verificationRequest.off("change", this.onVerificationRequestChange);
|
|
|
|
}
|
|
|
|
this.verificationRequest = request;
|
|
|
|
await request.accept();
|
|
|
|
request.on("change", this.onVerificationRequestChange);
|
|
|
|
this.emit("update");
|
|
|
|
}
|
2020-03-24 17:57:04 +03:00
|
|
|
}
|