matrix-js-sdk
Version:
Matrix Client-Server SDK for Javascript
1,161 lines (1,032 loc) • 182 kB
text/typescript
/*
Copyright 2016 OpenMarket Ltd
Copyright 2017 Vector Creations Ltd
Copyright 2018-2019 New Vector Ltd
Copyright 2019-2021 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 anotherjson from "another-json";
import { v4 as uuidv4 } from "uuid";
import type { IDeviceKeys, IEventDecryptionResult, IMegolmSessionData, IOneTimeKey } from "../@types/crypto";
import type { PkDecryption, PkSigning } from "@matrix-org/olm";
import { EventType, ToDeviceMessageId } from "../@types/event";
import { TypedReEmitter } from "../ReEmitter";
import { logger } from "../logger";
import { IExportedDevice, OlmDevice } from "./OlmDevice";
import { IOlmDevice } from "./algorithms/megolm";
import * as olmlib from "./olmlib";
import { DeviceInfoMap, DeviceList } from "./DeviceList";
import { DeviceInfo, IDevice } from "./deviceinfo";
import type { DecryptionAlgorithm, EncryptionAlgorithm } from "./algorithms";
import * as algorithms from "./algorithms";
import { createCryptoStoreCacheCallbacks, CrossSigningInfo, DeviceTrustLevel, UserTrustLevel } from "./CrossSigning";
import { EncryptionSetupBuilder } from "./EncryptionSetup";
import { SecretStorage as LegacySecretStorage } from "./SecretStorage";
import { CrossSigningKey, ICreateSecretStorageOpts, IEncryptedEventInfo, IRecoveryKey } from "./api";
import { OutgoingRoomKeyRequestManager } from "./OutgoingRoomKeyRequestManager";
import { IndexedDBCryptoStore } from "./store/indexeddb-crypto-store";
import { VerificationBase } from "./verification/Base";
import { ReciprocateQRCode, SCAN_QR_CODE_METHOD, SHOW_QR_CODE_METHOD } from "./verification/QRCode";
import { SAS as SASVerification } from "./verification/SAS";
import { keyFromPassphrase } from "./key_passphrase";
import { decodeRecoveryKey, encodeRecoveryKey } from "./recoverykey";
import { VerificationRequest } from "./verification/request/VerificationRequest";
import { InRoomChannel, InRoomRequests } from "./verification/request/InRoomChannel";
import { Request, ToDeviceChannel, ToDeviceRequests } from "./verification/request/ToDeviceChannel";
import { IllegalMethod } from "./verification/IllegalMethod";
import { KeySignatureUploadError } from "../errors";
import { calculateKeyCheck, decryptAES, encryptAES, IEncryptedPayload } from "./aes";
import { DehydrationManager } from "./dehydration";
import { BackupManager, LibOlmBackupDecryptor, backupTrustInfoFromLegacyTrustInfo } from "./backup";
import { IStore } from "../store";
import { Room, RoomEvent } from "../models/room";
import { RoomMember, RoomMemberEvent } from "../models/room-member";
import { EventStatus, IContent, IEvent, MatrixEvent, MatrixEventEvent } from "../models/event";
import { ToDeviceBatch } from "../models/ToDeviceMessage";
import {
ClientEvent,
ICrossSigningKey,
IKeysUploadResponse,
ISignedKey,
IUploadKeySignaturesResponse,
MatrixClient,
} from "../client";
import { IRoomEncryption, RoomList } from "./RoomList";
import { IKeyBackupInfo } from "./keybackup";
import { ISyncStateData } from "../sync";
import { CryptoStore } from "./store/base";
import { IVerificationChannel } from "./verification/request/Channel";
import { TypedEventEmitter } from "../models/typed-event-emitter";
import { IDeviceLists, ISyncResponse, IToDeviceEvent } from "../sync-accumulator";
import { ISignatures } from "../@types/signed";
import { IMessage } from "./algorithms/olm";
import { BackupDecryptor, CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend";
import { RoomState, RoomStateEvent } from "../models/room-state";
import { MapWithDefault, recursiveMapToObject } from "../utils";
import {
AccountDataClient,
AddSecretStorageKeyOpts,
SECRET_STORAGE_ALGORITHM_V1_AES,
SecretStorageKeyDescription,
SecretStorageKeyObject,
SecretStorageKeyTuple,
ServerSideSecretStorageImpl,
} from "../secret-storage";
import { ISecretRequest } from "./SecretSharing";
import {
BackupTrustInfo,
BootstrapCrossSigningOpts,
CrossSigningStatus,
DeviceVerificationStatus,
EventEncryptionInfo,
EventShieldColour,
EventShieldReason,
ImportRoomKeysOpts,
KeyBackupCheck,
KeyBackupInfo,
VerificationRequest as CryptoApiVerificationRequest,
OwnDeviceKeys,
} from "../crypto-api";
import { Device, DeviceMap } from "../models/device";
import { deviceInfoToDevice } from "./device-converter";
import { ClientPrefix, MatrixError, Method } from "../http-api";
import { decodeBase64, encodeBase64 } from "../base64";
/* re-exports for backwards compatibility */
export type {
BootstrapCrossSigningOpts as IBootstrapCrossSigningOpts,
CryptoCallbacks as ICryptoCallbacks,
} from "../crypto-api";
const DeviceVerification = DeviceInfo.DeviceVerification;
const defaultVerificationMethods = {
[ReciprocateQRCode.NAME]: ReciprocateQRCode,
[SASVerification.NAME]: SASVerification,
// These two can't be used for actual verification, but we do
// need to be able to define them here for the verification flows
// to start.
[SHOW_QR_CODE_METHOD]: IllegalMethod,
[SCAN_QR_CODE_METHOD]: IllegalMethod,
} as const;
/**
* verification method names
*/
// legacy export identifier
export const verificationMethods = {
RECIPROCATE_QR_CODE: ReciprocateQRCode.NAME,
SAS: SASVerification.NAME,
} as const;
export type VerificationMethod = keyof typeof verificationMethods | string;
export function isCryptoAvailable(): boolean {
return Boolean(globalThis.Olm);
}
// minimum time between attempting to unwedge an Olm session, if we succeeded
// in creating a new session
const MIN_FORCE_SESSION_INTERVAL_MS = 60 * 60 * 1000; // 1 hour
// minimum time between attempting to unwedge an Olm session, if we failed
// to create a new session
const FORCE_SESSION_RETRY_INTERVAL_MS = 5 * 60 * 1000; // 5 minutes
interface IInitOpts {
exportedOlmDevice?: IExportedDevice;
pickleKey?: string;
}
/* eslint-disable camelcase */
interface IRoomKey {
room_id: string;
algorithm: string;
}
/**
* The parameters of a room key request. The details of the request may
* vary with the crypto algorithm, but the management and storage layers for
* outgoing requests expect it to have 'room_id' and 'session_id' properties.
*/
export interface IRoomKeyRequestBody extends IRoomKey {
session_id: string;
sender_key: string;
}
/* eslint-enable camelcase */
interface IDeviceVerificationUpgrade {
devices: DeviceInfo[];
crossSigningInfo: CrossSigningInfo;
}
export interface ICheckOwnCrossSigningTrustOpts {
allowPrivateKeyRequests?: boolean;
}
interface IUserOlmSession {
deviceIdKey: string;
sessions: {
sessionId: string;
hasReceivedMessage: boolean;
}[];
}
export interface IRoomKeyRequestRecipient {
userId: string;
deviceId: string;
}
interface ISignableObject {
signatures?: ISignatures;
unsigned?: object;
}
export interface IRequestsMap {
getRequest(event: MatrixEvent): VerificationRequest | undefined;
getRequestByChannel(channel: IVerificationChannel): VerificationRequest | undefined;
setRequest(event: MatrixEvent, request: VerificationRequest): void;
setRequestByChannel(channel: IVerificationChannel, request: VerificationRequest): void;
}
/* eslint-disable camelcase */
export interface IOlmEncryptedContent {
algorithm: typeof olmlib.OLM_ALGORITHM;
sender_key: string;
ciphertext: Record<string, IMessage>;
[ToDeviceMessageId]?: string;
}
export interface IMegolmEncryptedContent {
algorithm: typeof olmlib.MEGOLM_ALGORITHM;
sender_key: string;
session_id: string;
device_id: string;
ciphertext: string;
[ToDeviceMessageId]?: string;
}
/* eslint-enable camelcase */
export type IEncryptedContent = IOlmEncryptedContent | IMegolmEncryptedContent;
export enum CryptoEvent {
DeviceVerificationChanged = "deviceVerificationChanged",
UserTrustStatusChanged = "userTrustStatusChanged",
UserCrossSigningUpdated = "userCrossSigningUpdated",
RoomKeyRequest = "crypto.roomKeyRequest",
RoomKeyRequestCancellation = "crypto.roomKeyRequestCancellation",
KeyBackupStatus = "crypto.keyBackupStatus",
KeyBackupFailed = "crypto.keyBackupFailed",
KeyBackupSessionsRemaining = "crypto.keyBackupSessionsRemaining",
/**
* Fires when a new valid backup decryption key is in cache.
* This will happen when a secret is received from another session, from secret storage,
* or when a new backup is created from this session.
*
* The payload is the version of the backup for which we have the key for.
*
* This event is only fired by the rust crypto backend.
*/
KeyBackupDecryptionKeyCached = "crypto.keyBackupDecryptionKeyCached",
KeySignatureUploadFailure = "crypto.keySignatureUploadFailure",
/** @deprecated Use `VerificationRequestReceived`. */
VerificationRequest = "crypto.verification.request",
/**
* Fires when a key verification request is received.
*
* The payload is a {@link Crypto.VerificationRequest}.
*/
VerificationRequestReceived = "crypto.verificationRequestReceived",
Warning = "crypto.warning",
WillUpdateDevices = "crypto.willUpdateDevices",
DevicesUpdated = "crypto.devicesUpdated",
KeysChanged = "crossSigning.keysChanged",
/**
* Fires when data is being migrated from legacy crypto to rust crypto.
*
* The payload is a pair `(progress, total)`, where `progress` is the number of steps completed so far, and
* `total` is the total number of steps. When migration is complete, a final instance of the event is emitted, with
* `progress === total === -1`.
*/
LegacyCryptoStoreMigrationProgress = "crypto.legacyCryptoStoreMigrationProgress",
}
export type CryptoEventHandlerMap = {
/**
* Fires when a device is marked as verified/unverified/blocked/unblocked by
* {@link MatrixClient#setDeviceVerified | MatrixClient.setDeviceVerified} or
* {@link MatrixClient#setDeviceBlocked | MatrixClient.setDeviceBlocked}.
*
* @param userId - the owner of the verified device
* @param deviceId - the id of the verified device
* @param deviceInfo - updated device information
*/
[CryptoEvent.DeviceVerificationChanged]: (userId: string, deviceId: string, device: DeviceInfo) => void;
/**
* Fires when the trust status of a user changes
* If userId is the userId of the logged-in user, this indicated a change
* in the trust status of the cross-signing data on the account.
*
* The cross-signing API is currently UNSTABLE and may change without notice.
* @experimental
*
* @param userId - the userId of the user in question
* @param trustLevel - The new trust level of the user
*/
[CryptoEvent.UserTrustStatusChanged]: (userId: string, trustLevel: UserTrustLevel) => void;
/**
* Fires when we receive a room key request
*
* @param req - request details
*/
[CryptoEvent.RoomKeyRequest]: (request: IncomingRoomKeyRequest) => void;
/**
* Fires when we receive a room key request cancellation
*/
[CryptoEvent.RoomKeyRequestCancellation]: (request: IncomingRoomKeyRequestCancellation) => void;
/**
* Fires whenever the status of e2e key backup changes, as returned by getKeyBackupEnabled()
* @param enabled - true if key backup has been enabled, otherwise false
* @example
* ```
* matrixClient.on("crypto.keyBackupStatus", function(enabled){
* if (enabled) {
* [...]
* }
* });
* ```
*/
[CryptoEvent.KeyBackupStatus]: (enabled: boolean) => void;
[CryptoEvent.KeyBackupFailed]: (errcode: string) => void;
[CryptoEvent.KeyBackupSessionsRemaining]: (remaining: number) => void;
/**
* Fires when the backup decryption key is received and cached.
*
* @param version - The version of the backup for which we have the key for.
*/
[CryptoEvent.KeyBackupDecryptionKeyCached]: (version: string) => void;
[CryptoEvent.KeySignatureUploadFailure]: (
failures: IUploadKeySignaturesResponse["failures"],
source: "checkOwnCrossSigningTrust" | "afterCrossSigningLocalKeyChange" | "setDeviceVerification",
upload: (opts: { shouldEmit: boolean }) => Promise<void>,
) => void;
/**
* Fires when a key verification is requested.
*
* Deprecated: use `CryptoEvent.VerificationRequestReceived`.
*/
[CryptoEvent.VerificationRequest]: (request: VerificationRequest<any>) => void;
/**
* Fires when a key verification request is received.
*/
[CryptoEvent.VerificationRequestReceived]: (request: CryptoApiVerificationRequest) => void;
/**
* Fires when the app may wish to warn the user about something related
* the end-to-end crypto.
*
* @param type - One of the strings listed above
*/
[CryptoEvent.Warning]: (type: string) => void;
/**
* Fires when the user's cross-signing keys have changed or cross-signing
* has been enabled/disabled. The client can use getStoredCrossSigningForUser
* with the user ID of the logged in user to check if cross-signing is
* enabled on the account. If enabled, it can test whether the current key
* is trusted using with checkUserTrust with the user ID of the logged
* in user. The checkOwnCrossSigningTrust function may be used to reconcile
* the trust in the account key.
*
* The cross-signing API is currently UNSTABLE and may change without notice.
* @experimental
*/
[CryptoEvent.KeysChanged]: (data: {}) => void;
/**
* Fires whenever the stored devices for a user will be updated
* @param users - A list of user IDs that will be updated
* @param initialFetch - If true, the store is empty (apart
* from our own device) and is being seeded.
*/
[CryptoEvent.WillUpdateDevices]: (users: string[], initialFetch: boolean) => void;
/**
* Fires whenever the stored devices for a user have changed
* @param users - A list of user IDs that were updated
* @param initialFetch - If true, the store was empty (apart
* from our own device) and has been seeded.
*/
[CryptoEvent.DevicesUpdated]: (users: string[], initialFetch: boolean) => void;
[CryptoEvent.UserCrossSigningUpdated]: (userId: string) => void;
[CryptoEvent.LegacyCryptoStoreMigrationProgress]: (progress: number, total: number) => void;
};
export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap> implements CryptoBackend {
/**
* @returns The version of Olm.
*/
public static getOlmVersion(): [number, number, number] {
return OlmDevice.getOlmVersion();
}
public readonly backupManager: BackupManager;
public readonly crossSigningInfo: CrossSigningInfo;
public readonly olmDevice: OlmDevice;
public readonly deviceList: DeviceList;
public readonly dehydrationManager: DehydrationManager;
public readonly secretStorage: LegacySecretStorage;
private readonly roomList: RoomList;
private readonly reEmitter: TypedReEmitter<CryptoEvent, CryptoEventHandlerMap>;
private readonly verificationMethods: Map<VerificationMethod, typeof VerificationBase>;
public readonly supportedAlgorithms: string[];
private readonly outgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager;
private readonly toDeviceVerificationRequests: ToDeviceRequests;
public readonly inRoomVerificationRequests: InRoomRequests;
private trustCrossSignedDevices = true;
// the last time we did a check for the number of one-time-keys on the server.
private lastOneTimeKeyCheck: number | null = null;
private oneTimeKeyCheckInProgress = false;
// EncryptionAlgorithm instance for each room
private roomEncryptors = new Map<string, EncryptionAlgorithm>();
// map from algorithm to DecryptionAlgorithm instance, for each room
private roomDecryptors = new Map<string, Map<string, DecryptionAlgorithm>>();
private deviceKeys: Record<string, string> = {}; // type: key
public globalBlacklistUnverifiedDevices = false;
public globalErrorOnUnknownDevices = true;
// list of IncomingRoomKeyRequests/IncomingRoomKeyRequestCancellations
// we received in the current sync.
private receivedRoomKeyRequests: IncomingRoomKeyRequest[] = [];
private receivedRoomKeyRequestCancellations: IncomingRoomKeyRequestCancellation[] = [];
// true if we are currently processing received room key requests
private processingRoomKeyRequests = false;
// controls whether device tracking is delayed
// until calling encryptEvent or trackRoomDevices,
// or done immediately upon enabling room encryption.
private lazyLoadMembers = false;
// in case lazyLoadMembers is true,
// track if an initial tracking of all the room members
// has happened for a given room. This is delayed
// to avoid loading room members as long as possible.
private roomDeviceTrackingState: { [roomId: string]: Promise<void> } = {};
// The timestamp of the minimum time at which we will retry forcing establishment
// of a new session for each device, in milliseconds.
// {
// userId: {
// deviceId: 1234567890000,
// },
// }
// Map: user Id → device Id → timestamp
private forceNewSessionRetryTime: MapWithDefault<string, MapWithDefault<string, number>> = new MapWithDefault(
() => new MapWithDefault(() => 0),
);
// This flag will be unset whilst the client processes a sync response
// so that we don't start requesting keys until we've actually finished
// processing the response.
private sendKeyRequestsImmediately = false;
private oneTimeKeyCount?: number;
private needsNewFallback?: boolean;
private fallbackCleanup?: ReturnType<typeof setTimeout>;
/**
* Cryptography bits
*
* This module is internal to the js-sdk; the public API is via MatrixClient.
*
* @internal
*
* @param baseApis - base matrix api interface
*
* @param userId - The user ID for the local user
*
* @param deviceId - The identifier for this device.
*
* @param clientStore - the MatrixClient data store.
*
* @param cryptoStore - storage for the crypto layer.
*
* @param roomList - An initialised RoomList object
*
* @param verificationMethods - Array of verification methods to use.
* Each element can either be a string from MatrixClient.verificationMethods
* or a class that implements a verification method.
*/
public constructor(
public readonly baseApis: MatrixClient,
public readonly userId: string,
private readonly deviceId: string,
private readonly clientStore: IStore,
public readonly cryptoStore: CryptoStore,
verificationMethods: Array<VerificationMethod | (typeof VerificationBase & { NAME: string })>,
) {
super();
logger.debug("Crypto: initialising roomlist...");
this.roomList = new RoomList(cryptoStore);
this.reEmitter = new TypedReEmitter(this);
if (verificationMethods) {
this.verificationMethods = new Map();
for (const method of verificationMethods) {
if (typeof method === "string") {
if (defaultVerificationMethods[method]) {
this.verificationMethods.set(
method,
<typeof VerificationBase>defaultVerificationMethods[method],
);
}
} else if (method["NAME"]) {
this.verificationMethods.set(method["NAME"], method as typeof VerificationBase);
} else {
logger.warn(`Excluding unknown verification method ${method}`);
}
}
} else {
this.verificationMethods = new Map(Object.entries(defaultVerificationMethods)) as Map<
VerificationMethod,
typeof VerificationBase
>;
}
this.backupManager = new BackupManager(baseApis, async () => {
// try to get key from cache
const cachedKey = await this.getSessionBackupPrivateKey();
if (cachedKey) {
return cachedKey;
}
// try to get key from secret storage
const storedKey = await this.secretStorage.get("m.megolm_backup.v1");
if (storedKey) {
// ensure that the key is in the right format. If not, fix the key and
// store the fixed version
const fixedKey = fixBackupKey(storedKey);
if (fixedKey) {
const keys = await this.secretStorage.getKey();
await this.secretStorage.store("m.megolm_backup.v1", fixedKey, [keys![0]]);
}
return decodeBase64(fixedKey || storedKey);
}
// try to get key from app
if (this.baseApis.cryptoCallbacks && this.baseApis.cryptoCallbacks.getBackupKey) {
return this.baseApis.cryptoCallbacks.getBackupKey();
}
throw new Error("Unable to get private key");
});
this.olmDevice = new OlmDevice(cryptoStore);
this.deviceList = new DeviceList(baseApis, cryptoStore, this.olmDevice);
// XXX: This isn't removed at any point, but then none of the event listeners
// this class sets seem to be removed at any point... :/
this.deviceList.on(CryptoEvent.UserCrossSigningUpdated, this.onDeviceListUserCrossSigningUpdated);
this.reEmitter.reEmit(this.deviceList, [CryptoEvent.DevicesUpdated, CryptoEvent.WillUpdateDevices]);
this.supportedAlgorithms = Array.from(algorithms.DECRYPTION_CLASSES.keys());
this.outgoingRoomKeyRequestManager = new OutgoingRoomKeyRequestManager(
baseApis,
this.deviceId,
this.cryptoStore,
);
this.toDeviceVerificationRequests = new ToDeviceRequests();
this.inRoomVerificationRequests = new InRoomRequests();
const cryptoCallbacks = this.baseApis.cryptoCallbacks || {};
const cacheCallbacks = createCryptoStoreCacheCallbacks(cryptoStore, this.olmDevice);
this.crossSigningInfo = new CrossSigningInfo(userId, cryptoCallbacks, cacheCallbacks);
// Yes, we pass the client twice here: see SecretStorage
this.secretStorage = new LegacySecretStorage(baseApis as AccountDataClient, cryptoCallbacks, baseApis);
this.dehydrationManager = new DehydrationManager(this);
// Assuming no app-supplied callback, default to getting from SSSS.
if (!cryptoCallbacks.getCrossSigningKey && cryptoCallbacks.getSecretStorageKey) {
cryptoCallbacks.getCrossSigningKey = async (type): Promise<Uint8Array | null> => {
return CrossSigningInfo.getFromSecretStorage(type, this.secretStorage);
};
}
}
/**
* Initialise the crypto module so that it is ready for use
*
* Returns a promise which resolves once the crypto module is ready for use.
*
* @param exportedOlmDevice - (Optional) data from exported device
* that must be re-created.
*/
public async init({ exportedOlmDevice, pickleKey }: IInitOpts = {}): Promise<void> {
logger.log("Crypto: initialising Olm...");
await global.Olm.init();
logger.log(
exportedOlmDevice
? "Crypto: initialising Olm device from exported device..."
: "Crypto: initialising Olm device...",
);
await this.olmDevice.init({ fromExportedDevice: exportedOlmDevice, pickleKey });
logger.log("Crypto: loading device list...");
await this.deviceList.load();
// build our device keys: these will later be uploaded
this.deviceKeys["ed25519:" + this.deviceId] = this.olmDevice.deviceEd25519Key!;
this.deviceKeys["curve25519:" + this.deviceId] = this.olmDevice.deviceCurve25519Key!;
logger.log("Crypto: fetching own devices...");
let myDevices = this.deviceList.getRawStoredDevicesForUser(this.userId);
if (!myDevices) {
myDevices = {};
}
if (!myDevices[this.deviceId]) {
// add our own deviceinfo to the cryptoStore
logger.log("Crypto: adding this device to the store...");
const deviceInfo = {
keys: this.deviceKeys,
algorithms: this.supportedAlgorithms,
verified: DeviceVerification.VERIFIED,
known: true,
};
myDevices[this.deviceId] = deviceInfo;
this.deviceList.storeDevicesForUser(this.userId, myDevices);
this.deviceList.saveIfDirty();
}
await this.cryptoStore.doTxn("readonly", [IndexedDBCryptoStore.STORE_ACCOUNT], (txn) => {
this.cryptoStore.getCrossSigningKeys(txn, (keys) => {
// can be an empty object after resetting cross-signing keys, see storeTrustedSelfKeys
if (keys && Object.keys(keys).length !== 0) {
logger.log("Loaded cross-signing public keys from crypto store");
this.crossSigningInfo.setKeys(keys);
}
});
});
// make sure we are keeping track of our own devices
// (this is important for key backups & things)
this.deviceList.startTrackingDeviceList(this.userId);
logger.debug("Crypto: initialising roomlist...");
await this.roomList.init();
logger.log("Crypto: checking for key backup...");
this.backupManager.checkAndStart();
}
/**
* Implementation of {@link CryptoApi#getVersion}.
*/
public getVersion(): string {
const olmVersionTuple = Crypto.getOlmVersion();
return `Olm ${olmVersionTuple[0]}.${olmVersionTuple[1]}.${olmVersionTuple[2]}`;
}
/**
* Whether to trust a others users signatures of their devices.
* If false, devices will only be considered 'verified' if we have
* verified that device individually (effectively disabling cross-signing).
*
* Default: true
*
* @returns True if trusting cross-signed devices
*/
public getTrustCrossSignedDevices(): boolean {
return this.trustCrossSignedDevices;
}
/**
* @deprecated Use {@link Crypto.CryptoApi#getTrustCrossSignedDevices}.
*/
public getCryptoTrustCrossSignedDevices(): boolean {
return this.trustCrossSignedDevices;
}
/**
* See getCryptoTrustCrossSignedDevices
*
* @param val - True to trust cross-signed devices
*/
public setTrustCrossSignedDevices(val: boolean): void {
this.trustCrossSignedDevices = val;
for (const userId of this.deviceList.getKnownUserIds()) {
const devices = this.deviceList.getRawStoredDevicesForUser(userId);
for (const deviceId of Object.keys(devices)) {
const deviceTrust = this.checkDeviceTrust(userId, deviceId);
// If the device is locally verified then isVerified() is always true,
// so this will only have caused the value to change if the device is
// cross-signing verified but not locally verified
if (!deviceTrust.isLocallyVerified() && deviceTrust.isCrossSigningVerified()) {
const deviceObj = this.deviceList.getStoredDevice(userId, deviceId)!;
this.emit(CryptoEvent.DeviceVerificationChanged, userId, deviceId, deviceObj);
}
}
}
}
/**
* @deprecated Use {@link Crypto.CryptoApi#setTrustCrossSignedDevices}.
*/
public setCryptoTrustCrossSignedDevices(val: boolean): void {
this.setTrustCrossSignedDevices(val);
}
/**
* Create a recovery key from a user-supplied passphrase.
*
* @param password - Passphrase string that can be entered by the user
* when restoring the backup as an alternative to entering the recovery key.
* Optional.
* @returns Object with public key metadata, encoded private
* recovery key which should be disposed of after displaying to the user,
* and raw private key to avoid round tripping if needed.
*/
public async createRecoveryKeyFromPassphrase(password?: string): Promise<IRecoveryKey> {
const decryption = new global.Olm.PkDecryption();
try {
if (password) {
const derivation = await keyFromPassphrase(password);
decryption.init_with_private_key(derivation.key);
const privateKey = decryption.get_private_key();
return {
keyInfo: {
passphrase: {
algorithm: "m.pbkdf2",
iterations: derivation.iterations,
salt: derivation.salt,
},
},
privateKey: privateKey,
encodedPrivateKey: encodeRecoveryKey(privateKey),
};
} else {
decryption.generate_key();
const privateKey = decryption.get_private_key();
return {
privateKey: privateKey,
encodedPrivateKey: encodeRecoveryKey(privateKey),
};
}
} finally {
decryption?.free();
}
}
/**
* Checks if the user has previously published cross-signing keys
*
* This means downloading the devicelist for the user and checking if the list includes
* the cross-signing pseudo-device.
*
* @internal
*/
public async userHasCrossSigningKeys(userId = this.userId): Promise<boolean> {
await this.downloadKeys([userId]);
return this.deviceList.getStoredCrossSigningForUser(userId) !== null;
}
/**
* Checks whether cross signing:
* - is enabled on this account and trusted by this device
* - has private keys either cached locally or stored in secret storage
*
* If this function returns false, bootstrapCrossSigning() can be used
* to fix things such that it returns true. That is to say, after
* bootstrapCrossSigning() completes successfully, this function should
* return true.
*
* The cross-signing API is currently UNSTABLE and may change without notice.
*
* @returns True if cross-signing is ready to be used on this device
*/
public async isCrossSigningReady(): Promise<boolean> {
const publicKeysOnDevice = this.crossSigningInfo.getId();
const privateKeysExistSomewhere =
(await this.crossSigningInfo.isStoredInKeyCache()) ||
(await this.crossSigningInfo.isStoredInSecretStorage(this.secretStorage));
return !!(publicKeysOnDevice && privateKeysExistSomewhere);
}
/**
* Checks whether secret storage:
* - is enabled on this account
* - is storing cross-signing private keys
* - is storing session backup key (if enabled)
*
* If this function returns false, bootstrapSecretStorage() can be used
* to fix things such that it returns true. That is to say, after
* bootstrapSecretStorage() completes successfully, this function should
* return true.
*
* The Secure Secret Storage API is currently UNSTABLE and may change without notice.
*
* @returns True if secret storage is ready to be used on this device
*/
public async isSecretStorageReady(): Promise<boolean> {
const secretStorageKeyInAccount = await this.secretStorage.hasKey();
const privateKeysInStorage = await this.crossSigningInfo.isStoredInSecretStorage(this.secretStorage);
const sessionBackupInStorage =
!this.backupManager.getKeyBackupEnabled() || (await this.baseApis.isKeyBackupKeyStored());
return !!(secretStorageKeyInAccount && privateKeysInStorage && sessionBackupInStorage);
}
/**
* Implementation of {@link CryptoApi#getCrossSigningStatus}
*/
public async getCrossSigningStatus(): Promise<CrossSigningStatus> {
const publicKeysOnDevice = Boolean(this.crossSigningInfo.getId());
const privateKeysInSecretStorage = Boolean(
await this.crossSigningInfo.isStoredInSecretStorage(this.secretStorage),
);
const cacheCallbacks = this.crossSigningInfo.getCacheCallbacks();
const masterKey = Boolean(await cacheCallbacks.getCrossSigningKeyCache?.("master"));
const selfSigningKey = Boolean(await cacheCallbacks.getCrossSigningKeyCache?.("self_signing"));
const userSigningKey = Boolean(await cacheCallbacks.getCrossSigningKeyCache?.("user_signing"));
return {
publicKeysOnDevice,
privateKeysInSecretStorage,
privateKeysCachedLocally: {
masterKey,
selfSigningKey,
userSigningKey,
},
};
}
/**
* Bootstrap cross-signing by creating keys if needed. If everything is already
* set up, then no changes are made, so this is safe to run to ensure
* cross-signing is ready for use.
*
* This function:
* - creates new cross-signing keys if they are not found locally cached nor in
* secret storage (if it has been setup)
*
* The cross-signing API is currently UNSTABLE and may change without notice.
*
* @param authUploadDeviceSigningKeys - Function
* called to await an interactive auth flow when uploading device signing keys.
* @param setupNewCrossSigning - Optional. Reset even if keys
* already exist.
* Args:
* A function that makes the request requiring auth. Receives the
* auth data as an object. Can be called multiple times, first with an empty
* authDict, to obtain the flows.
*/
public async bootstrapCrossSigning({
authUploadDeviceSigningKeys,
setupNewCrossSigning,
}: BootstrapCrossSigningOpts = {}): Promise<void> {
logger.log("Bootstrapping cross-signing");
const delegateCryptoCallbacks = this.baseApis.cryptoCallbacks;
const builder = new EncryptionSetupBuilder(this.baseApis.store.accountData, delegateCryptoCallbacks);
const crossSigningInfo = new CrossSigningInfo(
this.userId,
builder.crossSigningCallbacks,
builder.crossSigningCallbacks,
);
// Reset the cross-signing keys
const resetCrossSigning = async (): Promise<void> => {
crossSigningInfo.resetKeys();
// Sign master key with device key
await this.signObject(crossSigningInfo.keys.master);
// Store auth flow helper function, as we need to call it when uploading
// to ensure we handle auth errors properly.
builder.addCrossSigningKeys(authUploadDeviceSigningKeys, crossSigningInfo.keys);
// Cross-sign own device
const device = this.deviceList.getStoredDevice(this.userId, this.deviceId)!;
const deviceSignature = await crossSigningInfo.signDevice(this.userId, device);
builder.addKeySignature(this.userId, this.deviceId, deviceSignature!);
// Sign message key backup with cross-signing master key
if (this.backupManager.backupInfo) {
await crossSigningInfo.signObject(this.backupManager.backupInfo.auth_data, "master");
builder.addSessionBackup(this.backupManager.backupInfo);
}
};
const publicKeysOnDevice = this.crossSigningInfo.getId();
const privateKeysInCache = await this.crossSigningInfo.isStoredInKeyCache();
const privateKeysInStorage = await this.crossSigningInfo.isStoredInSecretStorage(this.secretStorage);
const privateKeysExistSomewhere = privateKeysInCache || privateKeysInStorage;
// Log all relevant state for easier parsing of debug logs.
logger.log({
setupNewCrossSigning,
publicKeysOnDevice,
privateKeysInCache,
privateKeysInStorage,
privateKeysExistSomewhere,
});
if (!privateKeysExistSomewhere || setupNewCrossSigning) {
logger.log("Cross-signing private keys not found locally or in secret storage, " + "creating new keys");
// If a user has multiple devices, it important to only call bootstrap
// as part of some UI flow (and not silently during startup), as they
// may have setup cross-signing on a platform which has not saved keys
// to secret storage, and this would reset them. In such a case, you
// should prompt the user to verify any existing devices first (and
// request private keys from those devices) before calling bootstrap.
await resetCrossSigning();
} else if (publicKeysOnDevice && privateKeysInCache) {
logger.log("Cross-signing public keys trusted and private keys found locally");
} else if (privateKeysInStorage) {
logger.log(
"Cross-signing private keys not found locally, but they are available " +
"in secret storage, reading storage and caching locally",
);
await this.checkOwnCrossSigningTrust({
allowPrivateKeyRequests: true,
});
}
// Assuming no app-supplied callback, default to storing new private keys in
// secret storage if it exists. If it does not, it is assumed this will be
// done as part of setting up secret storage later.
const crossSigningPrivateKeys = builder.crossSigningCallbacks.privateKeys;
if (crossSigningPrivateKeys.size && !this.baseApis.cryptoCallbacks.saveCrossSigningKeys) {
const secretStorage = new ServerSideSecretStorageImpl(
builder.accountDataClientAdapter,
builder.ssssCryptoCallbacks,
);
if (await secretStorage.hasKey()) {
logger.log("Storing new cross-signing private keys in secret storage");
// This is writing to in-memory account data in
// builder.accountDataClientAdapter so won't fail
await CrossSigningInfo.storeInSecretStorage(crossSigningPrivateKeys, secretStorage);
}
}
const operation = builder.buildOperation();
await operation.apply(this);
// This persists private keys and public keys as trusted,
// only do this if apply succeeded for now as retry isn't in place yet
await builder.persist(this);
logger.log("Cross-signing ready");
}
/**
* Bootstrap Secure Secret Storage if needed by creating a default key. If everything is
* already set up, then no changes are made, so this is safe to run to ensure secret
* storage is ready for use.
*
* This function
* - creates a new Secure Secret Storage key if no default key exists
* - if a key backup exists, it is migrated to store the key in the Secret
* Storage
* - creates a backup if none exists, and one is requested
* - migrates Secure Secret Storage to use the latest algorithm, if an outdated
* algorithm is found
*
* The Secure Secret Storage API is currently UNSTABLE and may change without notice.
*
* @param createSecretStorageKey - Optional. Function
* called to await a secret storage key creation flow.
* Returns a Promise which resolves to an object with public key metadata, encoded private
* recovery key which should be disposed of after displaying to the user,
* and raw private key to avoid round tripping if needed.
* @param keyBackupInfo - The current key backup object. If passed,
* the passphrase and recovery key from this backup will be used.
* @param setupNewKeyBackup - If true, a new key backup version will be
* created and the private key stored in the new SSSS store. Ignored if keyBackupInfo
* is supplied.
* @param setupNewSecretStorage - Optional. Reset even if keys already exist.
* @param getKeyBackupPassphrase - Optional. Function called to get the user's
* current key backup passphrase. Should return a promise that resolves with a Buffer
* containing the key, or rejects if the key cannot be obtained.
* Returns:
* A promise which resolves to key creation data for
* SecretStorage#addKey: an object with `passphrase` etc fields.
*/
// TODO this does not resolve with what it says it does
public async bootstrapSecretStorage({
createSecretStorageKey = async (): Promise<IRecoveryKey> => ({}) as IRecoveryKey,
keyBackupInfo,
setupNewKeyBackup,
setupNewSecretStorage,
getKeyBackupPassphrase,
}: ICreateSecretStorageOpts = {}): Promise<void> {
logger.log("Bootstrapping Secure Secret Storage");
const delegateCryptoCallbacks = this.baseApis.cryptoCallbacks;
const builder = new EncryptionSetupBuilder(this.baseApis.store.accountData, delegateCryptoCallbacks);
const secretStorage = new ServerSideSecretStorageImpl(
builder.accountDataClientAdapter,
builder.ssssCryptoCallbacks,
);
// the ID of the new SSSS key, if we create one
let newKeyId: string | null = null;
// create a new SSSS key and set it as default
const createSSSS = async (opts: AddSecretStorageKeyOpts): Promise<string> => {
const { keyId, keyInfo } = await secretStorage.addKey(SECRET_STORAGE_ALGORITHM_V1_AES, opts);
// make the private key available to encrypt 4S secrets
builder.ssssCryptoCallbacks.addPrivateKey(keyId, keyInfo, opts.key);
await secretStorage.setDefaultKeyId(keyId);
return keyId;
};
const ensureCanCheckPassphrase = async (keyId: string, keyInfo: SecretStorageKeyDescription): Promise<void> => {
if (!keyInfo.mac) {
const key = await this.baseApis.cryptoCallbacks.getSecretStorageKey?.(
{ keys: { [keyId]: keyInfo } },
"",
);
if (key) {
const privateKey = key[1];
builder.ssssCryptoCallbacks.addPrivateKey(keyId, keyInfo, privateKey);
const { iv, mac } = await calculateKeyCheck(privateKey);
keyInfo.iv = iv;
keyInfo.mac = mac;
await builder.setAccountData(`m.secret_storage.key.${keyId}`, keyInfo);
}
}
};
const signKeyBackupWithCrossSigning = async (keyBackupAuthData: IKeyBackupInfo["auth_data"]): Promise<void> => {
if (this.crossSigningInfo.getId() && (await this.crossSigningInfo.isStoredInKeyCache("master"))) {
try {
logger.log("Adding cross-signing signature to key backup");
await this.crossSigningInfo.signObject(keyBackupAuthData, "master");
} catch (e) {
// This step is not critical (just helpful), so we catch here
// and continue if it fails.
logger.error("Signing key backup with cross-signing keys failed", e);
}
} else {
logger.warn("Cross-signing keys not available, skipping signature on key backup");
}
};
const oldSSSSKey = await this.secretStorage.getKey();
const [oldKeyId, oldKeyInfo] = oldSSSSKey || [null, null];
const storageExists =
!setupNewSecretStorage && oldKeyInfo && oldKeyInfo.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES;
// Log all relevant state for easier parsing of debug logs.
logger.log({
keyBackupInfo,
setupNewKeyBackup,
setupNewSecretStorage,
storageExists,
oldKeyInfo,
});
if (!storageExists && !keyBackupInfo) {
// either we don't have anything, or we've been asked to restart
// from scratch
logger.log("Secret storage does not exist, creating new storage key");
// if we already have a usable default SSSS key and aren't resetting
// SSSS just use it. otherwise, create a new one
// Note: we leave the old SSSS key in place: there could be other
// secrets using it, in theory. We could move them to the new key but a)
// that would mean we'd need to prompt for the old passphrase, and b)
// it's not clear that would be the right thing to do anyway.
const { keyInfo, privateKey } = await createSecretStorageKey();
newKeyId = await createSSSS({ passphrase: keyInfo?.passphrase, key: privateKey, name: keyInfo?.name });
} else if (!storageExists && keyBackupInfo) {
// we have an existing backup, but no SSSS
logger.log("Secret storage does not exist, using key backup key");
// if we have the backup key already cached, use it; otherwise use the
// callback to prompt for the key
const backupKey = (await this.getSessionBackupPrivateKey()) || (await getKeyBackupPassphrase?.());
// create a new SSSS key and use the backup key as the new SSSS key
const opts = { key: backupKey } as AddSecretStorageKeyOpts;
if (keyBackupInfo.auth_data.private_key_salt && keyBackupInfo.auth_data.private_key_iterations) {
// FIXME: ???
opts.passphrase = {
algorithm: "m.pbkdf2",
iterations: keyBackupInfo.auth_data.private_key_iterations,
salt: keyBackupInfo.auth_data.private_key_salt,
bits: 256,
};
}
newKeyId = await createSSSS(opts);
// store the backup key in secret storage
await secretStorage.store("m.megolm_backup.v1", encodeBase64(backupKey!), [newKeyId]);
// The backup is trusted because the user provided the private key.
// Sign the backup with the cross-signing key so the key backup can
// be trusted via cross-signing.
await signKeyBackupWithCrossSigning(keyBackupInfo.auth_data);
builder.addSessionBackup(keyBackupInfo);
} else {
// 4S is already set up
logger.log("Secret storage exists");
if (oldKeyInfo && oldKeyInfo.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
// make sure that the default key has the information needed to
// check the passphrase
await ensureCanCheckPassphrase(oldKeyId, oldKeyInfo);
}
}
// If we have cross-signing private keys cached, store them in secret
// storage if they are not there already.
if (
!this.baseApis.cryptoCallbacks.saveCrossSigningKeys &&
(await this.isCrossSigningReady()) &&
(newKeyId || !(await this.crossSigningInfo.isStoredInSecretStorage(secretStorage)))
) {
logger.log("Copying cross-signing private keys from cache to secret storage");
const crossSigningPrivateKeys = await this.crossSigningInfo.getCrossSigningKeysFromCache();
// This is writing to in-memory account data in
// builder.accountDataClientAdapter so won't fail
await CrossSigningInfo.storeInSecretStorage(crossSigningPrivateKeys, secretStorage);
}
if (setupNewKeyBackup && !keyBackupInfo) {
logger.log("Creating new message key backup version");
const info = await this.baseApis.prepareKeyBackupVersion(
null /* random key */,
// don't write to secret storage, as it will write to this.secretStorage.
// Here, we want to capture all the side-effects of bootstrapping,
// and want to write to the local secretStorage object
{ secureSecretStorage: false },
);
// write the key to 4S
const privateKey = decodeRecoveryKey(info.recovery_key);
await secretStorage.store("m.megolm_backup.v1", encodeBase64(privateKey));
// create keyBackupInfo object to add to builder
const data: IKeyBackupInfo = {
algorithm: info.algorithm,
auth_data: info.auth_data,
};
// Sign with cross-signing master key
await signKeyBackupWithCrossSigning(data.auth_data);
// sign with the device fingerprint
await this.signObject(data.auth_data);
builder.addSessionBackup(data);
}
// Cache the session backup key
const sessi