UNPKG

matrix-js-sdk

Version:
1,125 lines (1,066 loc) 73.7 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.MegolmEncryption = exports.MegolmDecryption = void 0; exports.isRoomSharedHistory = isRoomSharedHistory; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _uuid = require("uuid"); var _logger = require("../../logger"); var olmlib = _interopRequireWildcard(require("../olmlib")); var _base = require("./base"); var _OlmDevice = require("../OlmDevice"); var _event = require("../../@types/event"); var _OutgoingRoomKeyRequestManager = require("../OutgoingRoomKeyRequestManager"); var _utils = require("../../utils"); var _membership = require("../../@types/membership"); var _cryptoApi = require("../../crypto-api"); var _CryptoBackend = require("../../common-crypto/CryptoBackend"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /* Copyright 2015 - 2021, 2023 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. */ /** * Defines m.olm encryption/decryption */ // determine whether the key can be shared with invitees function isRoomSharedHistory(room) { var _room$currentState, _visibilityEvent$getC; const visibilityEvent = room === null || room === void 0 || (_room$currentState = room.currentState) === null || _room$currentState === void 0 ? void 0 : _room$currentState.getStateEvents("m.room.history_visibility", ""); // NOTE: if the room visibility is unset, it would normally default to // "world_readable". // (https://spec.matrix.org/unstable/client-server-api/#server-behaviour-5) // But we will be paranoid here, and treat it as a situation where the room // is not shared-history const visibility = visibilityEvent === null || visibilityEvent === void 0 || (_visibilityEvent$getC = visibilityEvent.getContent()) === null || _visibilityEvent$getC === void 0 ? void 0 : _visibilityEvent$getC.history_visibility; return ["world_readable", "shared"].includes(visibility); } // map user Id → device Id → IBlockedDevice /** * Tests whether an encrypted content has a ciphertext. * Ciphertext can be a string or object depending on the content type {@link IEncryptedContent}. * * @param content - Encrypted content * @returns true: has ciphertext, else false */ const hasCiphertext = content => { return typeof content.ciphertext === "string" ? !!content.ciphertext.length : !!Object.keys(content.ciphertext).length; }; /** The result of parsing the an `m.room_key` or `m.forwarded_room_key` to-device event */ /** * @internal */ class OutboundSessionInfo { /** * @param sharedHistory - whether the session can be freely shared with * other group members, according to the room history visibility settings */ constructor(sessionId, sharedHistory = false) { /** number of times this session has been used */ (0, _defineProperty2.default)(this, "useCount", 0); /** devices with which we have shared the session key `userId -> {deviceId -> SharedWithData}` */ (0, _defineProperty2.default)(this, "sharedWithDevices", new _utils.MapWithDefault(() => new Map())); (0, _defineProperty2.default)(this, "blockedDevicesNotified", new _utils.MapWithDefault(() => new Map())); this.sessionId = sessionId; this.sharedHistory = sharedHistory; this.creationTime = new Date().getTime(); } /** * Check if it's time to rotate the session */ needsRotation(rotationPeriodMsgs, rotationPeriodMs) { const sessionLifetime = new Date().getTime() - this.creationTime; if (this.useCount >= rotationPeriodMsgs || sessionLifetime >= rotationPeriodMs) { _logger.logger.log("Rotating megolm session after " + this.useCount + " messages, " + sessionLifetime + "ms"); return true; } return false; } markSharedWithDevice(userId, deviceId, deviceKey, chainIndex) { this.sharedWithDevices.getOrCreate(userId).set(deviceId, { deviceKey, messageIndex: chainIndex }); } markNotifiedBlockedDevice(userId, deviceId) { this.blockedDevicesNotified.getOrCreate(userId).set(deviceId, true); } /** * Determine if this session has been shared with devices which it shouldn't * have been. * * @param devicesInRoom - `userId -> {deviceId -> object}` * devices we should shared the session with. * * @returns true if we have shared the session with devices which aren't * in devicesInRoom. */ sharedWithTooManyDevices(devicesInRoom) { for (const [userId, devices] of this.sharedWithDevices) { if (!devicesInRoom.has(userId)) { _logger.logger.log("Starting new megolm session because we shared with " + userId); return true; } for (const [deviceId] of devices) { var _devicesInRoom$get; if (!((_devicesInRoom$get = devicesInRoom.get(userId)) !== null && _devicesInRoom$get !== void 0 && _devicesInRoom$get.get(deviceId))) { _logger.logger.log("Starting new megolm session because we shared with " + userId + ":" + deviceId); return true; } } } return false; } } /** * Megolm encryption implementation * * @param params - parameters, as per {@link EncryptionAlgorithm} */ class MegolmEncryption extends _base.EncryptionAlgorithm { constructor(params) { var _params$config$rotati, _params$config, _params$config$rotati2, _params$config2; super(params); // the most recent attempt to set up a session. This is used to serialise // the session setups, so that we have a race-free view of which session we // are using, and which devices we have shared the keys with. It resolves // with an OutboundSessionInfo (or undefined, for the first message in the // room). (0, _defineProperty2.default)(this, "setupPromise", Promise.resolve(null)); // Map of outbound sessions by sessions ID. Used if we need a particular // session (the session we're currently using to send is always obtained // using setupPromise). (0, _defineProperty2.default)(this, "outboundSessions", {}); this.roomId = params.roomId; this.prefixedLogger = _logger.logger.getChild(`[${this.roomId} encryption]`); this.sessionRotationPeriodMsgs = (_params$config$rotati = (_params$config = params.config) === null || _params$config === void 0 ? void 0 : _params$config.rotation_period_msgs) !== null && _params$config$rotati !== void 0 ? _params$config$rotati : 100; this.sessionRotationPeriodMs = (_params$config$rotati2 = (_params$config2 = params.config) === null || _params$config2 === void 0 ? void 0 : _params$config2.rotation_period_ms) !== null && _params$config$rotati2 !== void 0 ? _params$config$rotati2 : 7 * 24 * 3600 * 1000; } /** * @internal * * @param devicesInRoom - The devices in this room, indexed by user ID * @param blocked - The devices that are blocked, indexed by user ID * @param singleOlmCreationPhase - Only perform one round of olm * session creation * * This method updates the setupPromise field of the class by chaining a new * call on top of the existing promise, and then catching and discarding any * errors that might happen while setting up the outbound group session. This * is done to ensure that `setupPromise` always resolves to `null` or the * `OutboundSessionInfo`. * * Using `>>=` to represent the promise chaining operation, it does the * following: * * ``` * setupPromise = previousSetupPromise >>= setup >>= discardErrors * ``` * * The initial value for the `setupPromise` is a promise that resolves to * `null`. The forceDiscardSession() resets setupPromise to this initial * promise. * * @returns Promise which resolves to the * OutboundSessionInfo when setup is complete. */ async ensureOutboundSession(room, devicesInRoom, blocked, singleOlmCreationPhase = false) { // takes the previous OutboundSessionInfo, and considers whether to create // a new one. Also shares the key with any (new) devices in the room. // // returns a promise which resolves once the keyshare is successful. const setup = async oldSession => { const sharedHistory = isRoomSharedHistory(room); const session = await this.prepareSession(devicesInRoom, sharedHistory, oldSession); await this.shareSession(devicesInRoom, sharedHistory, singleOlmCreationPhase, blocked, session); return session; }; // first wait for the previous share to complete const fallible = this.setupPromise.then(setup); // Ensure any failures are logged for debugging and make sure that the // promise chain remains unbroken // // setupPromise resolves to `null` or the `OutboundSessionInfo` whether // or not the share succeeds this.setupPromise = fallible.catch(e => { this.prefixedLogger.error(`Failed to setup outbound session`, e); return null; }); // but we return a promise which only resolves if the share was successful. return fallible; } async prepareSession(devicesInRoom, sharedHistory, session) { var _session, _session2; // history visibility changed if (session && sharedHistory !== session.sharedHistory) { session = null; } // need to make a brand new session? if ((_session = session) !== null && _session !== void 0 && _session.needsRotation(this.sessionRotationPeriodMsgs, this.sessionRotationPeriodMs)) { this.prefixedLogger.debug("Starting new megolm session because we need to rotate."); session = null; } // determine if we have shared with anyone we shouldn't have if ((_session2 = session) !== null && _session2 !== void 0 && _session2.sharedWithTooManyDevices(devicesInRoom)) { session = null; } if (!session) { this.prefixedLogger.debug("Starting new megolm session"); session = await this.prepareNewSession(sharedHistory); this.prefixedLogger.debug(`Started new megolm session ${session.sessionId}`); this.outboundSessions[session.sessionId] = session; } return session; } async shareSession(devicesInRoom, sharedHistory, singleOlmCreationPhase, blocked, session) { // now check if we need to share with any devices const shareMap = {}; for (const [userId, userDevices] of devicesInRoom) { for (const [deviceId, deviceInfo] of userDevices) { var _session$sharedWithDe; const key = deviceInfo.getIdentityKey(); if (key == this.olmDevice.deviceCurve25519Key) { // don't bother sending to ourself continue; } if (!((_session$sharedWithDe = session.sharedWithDevices.get(userId)) !== null && _session$sharedWithDe !== void 0 && _session$sharedWithDe.get(deviceId))) { shareMap[userId] = shareMap[userId] || []; shareMap[userId].push(deviceInfo); } } } const key = this.olmDevice.getOutboundGroupSessionKey(session.sessionId); const payload = { type: "m.room_key", content: { "algorithm": olmlib.MEGOLM_ALGORITHM, "room_id": this.roomId, "session_id": session.sessionId, "session_key": key.key, "chain_index": key.chain_index, "org.matrix.msc3061.shared_history": sharedHistory } }; const [devicesWithoutSession, olmSessions] = await olmlib.getExistingOlmSessions(this.olmDevice, this.baseApis, shareMap); await Promise.all([(async () => { // share keys with devices that we already have a session for const olmSessionList = Array.from(olmSessions.entries()).map(([userId, sessionsByUser]) => Array.from(sessionsByUser.entries()).map(([deviceId, session]) => `${userId}/${deviceId}: ${session.sessionId}`)).flat(1); this.prefixedLogger.debug("Sharing keys with devices with existing Olm sessions:", olmSessionList); await this.shareKeyWithOlmSessions(session, key, payload, olmSessions); this.prefixedLogger.debug("Shared keys with existing Olm sessions"); })(), (async () => { const deviceList = Array.from(devicesWithoutSession.entries()).map(([userId, devicesByUser]) => devicesByUser.map(device => `${userId}/${device.deviceId}`)).flat(1); this.prefixedLogger.debug("Sharing keys (start phase 1) with devices without existing Olm sessions:", deviceList); const errorDevices = []; // meanwhile, establish olm sessions for devices that we don't // already have a session for, and share keys with them. If // we're doing two phases of olm session creation, use a // shorter timeout when fetching one-time keys for the first // phase. const start = Date.now(); const failedServers = []; await this.shareKeyWithDevices(session, key, payload, devicesWithoutSession, errorDevices, singleOlmCreationPhase ? 10000 : 2000, failedServers); this.prefixedLogger.debug("Shared keys (end phase 1) with devices without existing Olm sessions"); if (!singleOlmCreationPhase && Date.now() - start < 10000) { // perform the second phase of olm session creation if requested, // and if the first phase didn't take too long (async () => { // Retry sending keys to devices that we were unable to establish // an olm session for. This time, we use a longer timeout, but we // do this in the background and don't block anything else while we // do this. We only need to retry users from servers that didn't // respond the first time. const retryDevices = new _utils.MapWithDefault(() => []); const failedServerMap = new Set(); for (const server of failedServers) { failedServerMap.add(server); } const failedDevices = []; for (const { userId, deviceInfo } of errorDevices) { const userHS = userId.slice(userId.indexOf(":") + 1); if (failedServerMap.has(userHS)) { retryDevices.getOrCreate(userId).push(deviceInfo); } else { // if we aren't going to retry, then handle it // as a failed device failedDevices.push({ userId, deviceInfo }); } } const retryDeviceList = Array.from(retryDevices.entries()).map(([userId, devicesByUser]) => devicesByUser.map(device => `${userId}/${device.deviceId}`)).flat(1); if (retryDeviceList.length > 0) { this.prefixedLogger.debug("Sharing keys (start phase 2) with devices without existing Olm sessions:", retryDeviceList); await this.shareKeyWithDevices(session, key, payload, retryDevices, failedDevices, 30000); this.prefixedLogger.debug("Shared keys (end phase 2) with devices without existing Olm sessions"); } await this.notifyFailedOlmDevices(session, key, failedDevices); })(); } else { await this.notifyFailedOlmDevices(session, key, errorDevices); } })(), (async () => { this.prefixedLogger.debug(`There are ${blocked.size} blocked devices:`, Array.from(blocked.entries()).map(([userId, blockedByUser]) => Array.from(blockedByUser.entries()).map(([deviceId, _deviceInfo]) => `${userId}/${deviceId}`)).flat(1)); // also, notify newly blocked devices that they're blocked const blockedMap = new _utils.MapWithDefault(() => new Map()); let blockedCount = 0; for (const [userId, userBlockedDevices] of blocked) { for (const [deviceId, device] of userBlockedDevices) { var _session$blockedDevic; if (((_session$blockedDevic = session.blockedDevicesNotified.get(userId)) === null || _session$blockedDevic === void 0 ? void 0 : _session$blockedDevic.get(deviceId)) === undefined) { blockedMap.getOrCreate(userId).set(deviceId, { device }); blockedCount++; } } } if (blockedCount) { this.prefixedLogger.debug(`Notifying ${blockedCount} newly blocked devices:`, Array.from(blockedMap.entries()).map(([userId, blockedByUser]) => Object.entries(blockedByUser).map(([deviceId, _deviceInfo]) => `${userId}/${deviceId}`)).flat(1)); await this.notifyBlockedDevices(session, blockedMap); this.prefixedLogger.debug(`Notified ${blockedCount} newly blocked devices`); } })()]); } /** * @internal * * * @returns session */ async prepareNewSession(sharedHistory) { const sessionId = this.olmDevice.createOutboundGroupSession(); const key = this.olmDevice.getOutboundGroupSessionKey(sessionId); await this.olmDevice.addInboundGroupSession(this.roomId, this.olmDevice.deviceCurve25519Key, [], sessionId, key.key, { ed25519: this.olmDevice.deviceEd25519Key }, false, { sharedHistory }); // don't wait for it to complete this.crypto.backupManager.backupGroupSession(this.olmDevice.deviceCurve25519Key, sessionId); return new OutboundSessionInfo(sessionId, sharedHistory); } /** * Determines what devices in devicesByUser don't have an olm session as given * in devicemap. * * @internal * * @param deviceMap - the devices that have olm sessions, as returned by * olmlib.ensureOlmSessionsForDevices. * @param devicesByUser - a map of user IDs to array of deviceInfo * @param noOlmDevices - an array to fill with devices that don't have * olm sessions * * @returns an array of devices that don't have olm sessions. If * noOlmDevices is specified, then noOlmDevices will be returned. */ getDevicesWithoutSessions(deviceMap, devicesByUser, noOlmDevices = []) { for (const [userId, devicesToShareWith] of devicesByUser) { const sessionResults = deviceMap.get(userId); for (const deviceInfo of devicesToShareWith) { const deviceId = deviceInfo.deviceId; const sessionResult = sessionResults === null || sessionResults === void 0 ? void 0 : sessionResults.get(deviceId); if (!(sessionResult !== null && sessionResult !== void 0 && sessionResult.sessionId)) { // no session with this device, probably because there // were no one-time keys. noOlmDevices.push({ userId, deviceInfo }); sessionResults === null || sessionResults === void 0 || sessionResults.delete(deviceId); // ensureOlmSessionsForUsers has already done the logging, // so just skip it. continue; } } } return noOlmDevices; } /** * Splits the user device map into multiple chunks to reduce the number of * devices we encrypt to per API call. * * @internal * * @param devicesByUser - map from userid to list of devices * * @returns the blocked devices, split into chunks */ splitDevices(devicesByUser) { const maxDevicesPerRequest = 20; // use an array where the slices of a content map gets stored let currentSlice = []; const mapSlices = [currentSlice]; for (const [userId, userDevices] of devicesByUser) { for (const deviceInfo of userDevices.values()) { currentSlice.push({ userId: userId, deviceInfo: deviceInfo.device }); } // We do this in the per-user loop as we prefer that all messages to the // same user end up in the same API call to make it easier for the // server (e.g. only have to send one EDU if a remote user, etc). This // does mean that if a user has many devices we may go over the desired // limit, but its not a hard limit so that is fine. if (currentSlice.length > maxDevicesPerRequest) { // the current slice is filled up. Start inserting into the next slice currentSlice = []; mapSlices.push(currentSlice); } } if (currentSlice.length === 0) { mapSlices.pop(); } return mapSlices; } /** * @internal * * * @param chainIndex - current chain index * * @param userDeviceMap - mapping from userId to deviceInfo * * @param payload - fields to include in the encrypted payload * * @returns Promise which resolves once the key sharing * for the given userDeviceMap is generated and has been sent. */ encryptAndSendKeysToDevices(session, chainIndex, devices, payload) { return this.crypto.encryptAndSendToDevices(devices, payload).then(() => { // store that we successfully uploaded the keys of the current slice for (const device of devices) { session.markSharedWithDevice(device.userId, device.deviceInfo.deviceId, device.deviceInfo.getIdentityKey(), chainIndex); } }).catch(error => { this.prefixedLogger.error("failed to encryptAndSendToDevices", error); throw error; }); } /** * @internal * * * @param userDeviceMap - list of blocked devices to notify * * @param payload - fields to include in the notification payload * * @returns Promise which resolves once the notifications * for the given userDeviceMap is generated and has been sent. */ async sendBlockedNotificationsToDevices(session, userDeviceMap, payload) { const contentMap = new _utils.MapWithDefault(() => new Map()); for (const val of userDeviceMap) { const userId = val.userId; const blockedInfo = val.deviceInfo; const deviceInfo = blockedInfo.deviceInfo; const deviceId = deviceInfo.deviceId; const message = _objectSpread(_objectSpread({}, payload), {}, { code: blockedInfo.code, reason: blockedInfo.reason, [_event.ToDeviceMessageId]: (0, _uuid.v4)() }); if (message.code === "m.no_olm") { delete message.room_id; delete message.session_id; } contentMap.getOrCreate(userId).set(deviceId, message); } await this.baseApis.sendToDevice("m.room_key.withheld", contentMap); // record the fact that we notified these blocked devices for (const [userId, userDeviceMap] of contentMap) { for (const deviceId of userDeviceMap.keys()) { session.markNotifiedBlockedDevice(userId, deviceId); } } } /** * Re-shares a megolm session key with devices if the key has already been * sent to them. * * @param senderKey - The key of the originating device for the session * @param sessionId - ID of the outbound session to share * @param userId - ID of the user who owns the target device * @param device - The target device */ async reshareKeyWithDevice(senderKey, sessionId, userId, device) { var _obSessionInfo$shared; const obSessionInfo = this.outboundSessions[sessionId]; if (!obSessionInfo) { this.prefixedLogger.debug(`megolm session ${senderKey}|${sessionId} not found: not re-sharing keys`); return; } // The chain index of the key we previously sent this device if (!obSessionInfo.sharedWithDevices.has(userId)) { this.prefixedLogger.debug(`megolm session ${senderKey}|${sessionId} never shared with user ${userId}`); return; } const sessionSharedData = (_obSessionInfo$shared = obSessionInfo.sharedWithDevices.get(userId)) === null || _obSessionInfo$shared === void 0 ? void 0 : _obSessionInfo$shared.get(device.deviceId); if (sessionSharedData === undefined) { this.prefixedLogger.debug(`megolm session ${senderKey}|${sessionId} never shared with device ${userId}:${device.deviceId}`); return; } if (sessionSharedData.deviceKey !== device.getIdentityKey()) { this.prefixedLogger.warn(`Megolm session ${senderKey}|${sessionId} has been shared with device ${device.deviceId} but ` + `with identity key ${sessionSharedData.deviceKey}. Key is now ${device.getIdentityKey()}!`); return; } // get the key from the inbound session: the outbound one will already // have been ratcheted to the next chain index. const key = await this.olmDevice.getInboundGroupSessionKey(this.roomId, senderKey, sessionId, sessionSharedData.messageIndex); if (!key) { this.prefixedLogger.warn(`No inbound session key found for megolm session ${senderKey}|${sessionId}: not re-sharing keys`); return; } await olmlib.ensureOlmSessionsForDevices(this.olmDevice, this.baseApis, new Map([[userId, [device]]])); const payload = { type: "m.forwarded_room_key", content: { "algorithm": olmlib.MEGOLM_ALGORITHM, "room_id": this.roomId, "session_id": sessionId, "session_key": key.key, "chain_index": key.chain_index, "sender_key": senderKey, "sender_claimed_ed25519_key": key.sender_claimed_ed25519_key, "forwarding_curve25519_key_chain": key.forwarding_curve25519_key_chain, "org.matrix.msc3061.shared_history": key.shared_history || false } }; const encryptedContent = { algorithm: olmlib.OLM_ALGORITHM, sender_key: this.olmDevice.deviceCurve25519Key, ciphertext: {}, [_event.ToDeviceMessageId]: (0, _uuid.v4)() }; await olmlib.encryptMessageForDevice(encryptedContent.ciphertext, this.userId, this.deviceId, this.olmDevice, userId, device, payload); await this.baseApis.sendToDevice("m.room.encrypted", new Map([[userId, new Map([[device.deviceId, encryptedContent]])]])); this.prefixedLogger.debug(`Re-shared key for megolm session ${senderKey}|${sessionId} with ${userId}:${device.deviceId}`); } /** * @internal * * * @param key - the session key as returned by * OlmDevice.getOutboundGroupSessionKey * * @param payload - the base to-device message payload for sharing keys * * @param devicesByUser - map from userid to list of devices * * @param errorDevices - array that will be populated with the devices that we can't get an * olm session for * * @param otkTimeout - The timeout in milliseconds when requesting * one-time keys for establishing new olm sessions. * * @param failedServers - An array to fill with remote servers that * failed to respond to one-time-key requests. */ async shareKeyWithDevices(session, key, payload, devicesByUser, errorDevices, otkTimeout, failedServers) { const devicemap = await olmlib.ensureOlmSessionsForDevices(this.olmDevice, this.baseApis, devicesByUser, false, otkTimeout, failedServers, this.prefixedLogger); this.getDevicesWithoutSessions(devicemap, devicesByUser, errorDevices); await this.shareKeyWithOlmSessions(session, key, payload, devicemap); } async shareKeyWithOlmSessions(session, key, payload, deviceMap) { const userDeviceMaps = this.splitDevices(deviceMap); for (let i = 0; i < userDeviceMaps.length; i++) { const taskDetail = `megolm keys for ${session.sessionId} (slice ${i + 1}/${userDeviceMaps.length})`; try { this.prefixedLogger.debug(`Sharing ${taskDetail}`, userDeviceMaps[i].map(d => `${d.userId}/${d.deviceInfo.deviceId}`)); await this.encryptAndSendKeysToDevices(session, key.chain_index, userDeviceMaps[i], payload); this.prefixedLogger.debug(`Shared ${taskDetail}`); } catch (e) { this.prefixedLogger.error(`Failed to share ${taskDetail}`); throw e; } } } /** * Notify devices that we weren't able to create olm sessions. * * * * @param failedDevices - the devices that we were unable to * create olm sessions for, as returned by shareKeyWithDevices */ async notifyFailedOlmDevices(session, key, failedDevices) { this.prefixedLogger.debug(`Notifying ${failedDevices.length} devices we failed to create Olm sessions`); // mark the devices that failed as "handled" because we don't want to try // to claim a one-time-key for dead devices on every message. for (const { userId, deviceInfo } of failedDevices) { const deviceId = deviceInfo.deviceId; session.markSharedWithDevice(userId, deviceId, deviceInfo.getIdentityKey(), key.chain_index); } const unnotifiedFailedDevices = await this.olmDevice.filterOutNotifiedErrorDevices(failedDevices); this.prefixedLogger.debug(`Need to notify ${unnotifiedFailedDevices.length} failed devices which haven't been notified before`); const blockedMap = new _utils.MapWithDefault(() => new Map()); for (const { userId, deviceInfo } of unnotifiedFailedDevices) { // we use a similar format to what // olmlib.ensureOlmSessionsForDevices returns, so that // we can use the same function to split blockedMap.getOrCreate(userId).set(deviceInfo.deviceId, { device: { code: "m.no_olm", reason: _OlmDevice.WITHHELD_MESSAGES["m.no_olm"], deviceInfo } }); } // send the notifications await this.notifyBlockedDevices(session, blockedMap); this.prefixedLogger.debug(`Notified ${unnotifiedFailedDevices.length} devices we failed to create Olm sessions`); } /** * Notify blocked devices that they have been blocked. * * * @param devicesByUser - map from userid to device ID to blocked data */ async notifyBlockedDevices(session, devicesByUser) { const payload = { room_id: this.roomId, session_id: session.sessionId, algorithm: olmlib.MEGOLM_ALGORITHM, sender_key: this.olmDevice.deviceCurve25519Key }; const userDeviceMaps = this.splitDevices(devicesByUser); for (let i = 0; i < userDeviceMaps.length; i++) { try { await this.sendBlockedNotificationsToDevices(session, userDeviceMaps[i], payload); this.prefixedLogger.debug(`Completed blacklist notification for ${session.sessionId} ` + `(slice ${i + 1}/${userDeviceMaps.length})`); } catch (e) { this.prefixedLogger.debug(`blacklist notification for ${session.sessionId} ` + `(slice ${i + 1}/${userDeviceMaps.length}) failed`); throw e; } } } /** * Perform any background tasks that can be done before a message is ready to * send, in order to speed up sending of the message. * * @param room - the room the event is in * @returns A function that, when called, will stop the preparation */ prepareToEncrypt(room) { if (room.roomId !== this.roomId) { throw new Error("MegolmEncryption.prepareToEncrypt called on unexpected room"); } if (this.encryptionPreparation != null) { // We're already preparing something, so don't do anything else. const elapsedTime = Date.now() - this.encryptionPreparation.startTime; this.prefixedLogger.debug(`Already started preparing to encrypt for this room ${elapsedTime}ms ago, skipping`); return this.encryptionPreparation.cancel; } this.prefixedLogger.debug("Preparing to encrypt events"); let cancelled = false; const isCancelled = () => cancelled; this.encryptionPreparation = { startTime: Date.now(), promise: (async () => { try { // Attempt to enumerate the devices in room, and gracefully // handle cancellation if it occurs. const getDevicesResult = await this.getDevicesInRoom(room, false, isCancelled); if (getDevicesResult === null) return; const [devicesInRoom, blocked] = getDevicesResult; if (this.crypto.globalErrorOnUnknownDevices) { // Drop unknown devices for now. When the message gets sent, we'll // throw an error, but we'll still be prepared to send to the known // devices. this.removeUnknownDevices(devicesInRoom); } this.prefixedLogger.debug("Ensuring outbound megolm session"); await this.ensureOutboundSession(room, devicesInRoom, blocked, true); this.prefixedLogger.debug("Ready to encrypt events"); } catch (e) { this.prefixedLogger.error("Failed to prepare to encrypt events", e); } finally { delete this.encryptionPreparation; } })(), cancel: () => { // The caller has indicated that the process should be cancelled, // so tell the promise that we'd like to halt, and reset the preparation state. cancelled = true; delete this.encryptionPreparation; } }; return this.encryptionPreparation.cancel; } /** * @param content - plaintext event content * * @returns Promise which resolves to the new event body */ async encryptMessage(room, eventType, content) { this.prefixedLogger.debug("Starting to encrypt event"); if (this.encryptionPreparation != null) { // If we started sending keys, wait for it to be done. // FIXME: check if we need to cancel // (https://github.com/matrix-org/matrix-js-sdk/issues/1255) try { await this.encryptionPreparation.promise; } catch (e) { // ignore any errors -- if the preparation failed, we'll just // restart everything here } } /** * When using in-room messages and the room has encryption enabled, * clients should ensure that encryption does not hinder the verification. */ const forceDistributeToUnverified = this.isVerificationEvent(eventType, content); const [devicesInRoom, blocked] = await this.getDevicesInRoom(room, forceDistributeToUnverified); // check if any of these devices are not yet known to the user. // if so, warn the user so they can verify or ignore. if (this.crypto.globalErrorOnUnknownDevices) { this.checkForUnknownDevices(devicesInRoom); } const session = await this.ensureOutboundSession(room, devicesInRoom, blocked); const payloadJson = { room_id: this.roomId, type: eventType, content: content }; const ciphertext = this.olmDevice.encryptGroupMessage(session.sessionId, JSON.stringify(payloadJson)); const encryptedContent = { algorithm: olmlib.MEGOLM_ALGORITHM, sender_key: this.olmDevice.deviceCurve25519Key, ciphertext: ciphertext, session_id: session.sessionId, // Include our device ID so that recipients can send us a // m.new_device message if they don't have our session key. // XXX: Do we still need this now that m.new_device messages // no longer exist since #483? device_id: this.deviceId }; session.useCount++; return encryptedContent; } isVerificationEvent(eventType, content) { switch (eventType) { case _event.EventType.KeyVerificationCancel: case _event.EventType.KeyVerificationDone: case _event.EventType.KeyVerificationMac: case _event.EventType.KeyVerificationStart: case _event.EventType.KeyVerificationKey: case _event.EventType.KeyVerificationReady: case _event.EventType.KeyVerificationAccept: { return true; } case _event.EventType.RoomMessage: { return content["msgtype"] === _event.MsgType.KeyVerificationRequest; } default: { return false; } } } /** * Forces the current outbound group session to be discarded such * that another one will be created next time an event is sent. * * This should not normally be necessary. */ forceDiscardSession() { this.setupPromise = this.setupPromise.then(() => null); } /** * Checks the devices we're about to send to and see if any are entirely * unknown to the user. If so, warn the user, and mark them as known to * give the user a chance to go verify them before re-sending this message. * * @param devicesInRoom - `userId -> {deviceId -> object}` * devices we should shared the session with. */ checkForUnknownDevices(devicesInRoom) { const unknownDevices = new _utils.MapWithDefault(() => new Map()); for (const [userId, userDevices] of devicesInRoom) { for (const [deviceId, device] of userDevices) { if (device.isUnverified() && !device.isKnown()) { unknownDevices.getOrCreate(userId).set(deviceId, device); } } } if (unknownDevices.size) { // it'd be kind to pass unknownDevices up to the user in this error throw new _base.UnknownDeviceError("This room contains unknown devices which have not been verified. " + "We strongly recommend you verify them before continuing.", unknownDevices); } } /** * Remove unknown devices from a set of devices. The devicesInRoom parameter * will be modified. * * @param devicesInRoom - `userId -> {deviceId -> object}` * devices we should shared the session with. */ removeUnknownDevices(devicesInRoom) { for (const [userId, userDevices] of devicesInRoom) { for (const [deviceId, device] of userDevices) { if (device.isUnverified() && !device.isKnown()) { userDevices.delete(deviceId); } } if (userDevices.size === 0) { devicesInRoom.delete(userId); } } } /** * Get the list of unblocked devices for all users in the room * * @param forceDistributeToUnverified - if set to true will include the unverified devices * even if setting is set to block them (useful for verification) * @param isCancelled - will cause the procedure to abort early if and when it starts * returning `true`. If omitted, cancellation won't happen. * * @returns Promise which resolves to `null`, or an array whose * first element is a {@link DeviceInfoMap} indicating * the devices that messages should be encrypted to, and whose second * element is a map from userId to deviceId to data indicating the devices * that are in the room but that have been blocked. * If `isCancelled` is provided and returns `true` while processing, `null` * will be returned. * If `isCancelled` is not provided, the Promise will never resolve to `null`. */ async getDevicesInRoom(room, forceDistributeToUnverified = false, isCancelled) { const members = await room.getEncryptionTargetMembers(); this.prefixedLogger.debug(`Encrypting for users (shouldEncryptForInvitedMembers: ${room.shouldEncryptForInvitedMembers()}):`, members.map(u => `${u.userId} (${u.membership})`)); const roomMembers = members.map(function (u) { return u.userId; }); // The global value is treated as a default for when rooms don't specify a value. let isBlacklisting = this.crypto.globalBlacklistUnverifiedDevices; const isRoomBlacklisting = room.getBlacklistUnverifiedDevices(); if (typeof isRoomBlacklisting === "boolean") { isBlacklisting = isRoomBlacklisting; } // We are happy to use a cached version here: we assume that if we already // have a list of the user's devices, then we already share an e2e room // with them, which means that they will have announced any new devices via // device_lists in their /sync response. This cache should then be maintained // using all the device_lists changes and left fields. // See https://github.com/vector-im/element-web/issues/2305 for details. const devices = await this.crypto.downloadKeys(roomMembers, false); if ((isCancelled === null || isCancelled === void 0 ? void 0 : isCancelled()) === true) { return null; } const blocked = new _utils.MapWithDefault(() => new Map()); // remove any blocked devices for (const [userId, userDevices] of devices) { for (const [deviceId, userDevice] of userDevices) { // Yield prior to checking each device so that we don't block // updating/rendering for too long. // See https://github.com/vector-im/element-web/issues/21612 if (isCancelled !== undefined) await (0, _utils.immediate)(); if ((isCancelled === null || isCancelled === void 0 ? void 0 : isCancelled()) === true) return null; const deviceTrust = this.crypto.checkDeviceTrust(userId, deviceId); if (userDevice.isBlocked() || !deviceTrust.isVerified() && isBlacklisting && !forceDistributeToUnverified) { const blockedDevices = blocked.getOrCreate(userId); const isBlocked = userDevice.isBlocked(); blockedDevices.set(deviceId, { code: isBlocked ? "m.blacklisted" : "m.unverified", reason: _OlmDevice.WITHHELD_MESSAGES[isBlocked ? "m.blacklisted" : "m.unverified"], deviceInfo: userDevice }); userDevices.delete(deviceId); } } } return [devices, blocked]; } } /** * Megolm decryption implementation * * @param params - parameters, as per {@link DecryptionAlgorithm} */ exports.MegolmEncryption = MegolmEncryption; class MegolmDecryption extends _base.DecryptionAlgorithm { constructor(params) { super(params); // events which we couldn't decrypt due to unknown sessions / // indexes, or which we could only decrypt with untrusted keys: // map from senderKey|sessionId to Set of MatrixEvents (0, _defineProperty2.default)(this, "pendingEvents", new Map()); // this gets stubbed out by the unit tests. (0, _defineProperty2.default)(this, "olmlib", olmlib); this.roomId = params.roomId; this.prefixedLogger = _logger.logger.getChild(`[${this.roomId} decryption]`); } /** * returns a promise which resolves to a * {@link EventDecryptionResult} once we have finished * decrypting, or rejects with an `algorithms.DecryptionError` if there is a * problem decrypting the event. */ async decryptEvent(event) { const content = event.getWireContent(); if (!content.sender_key || !content.session_id || !content.ciphertext) { throw new _CryptoBackend.DecryptionError(_cryptoApi.DecryptionFailureCode.MEGOLM_MISSING_FIELDS, "Missing fields in input"); } // we add the event to the pending list *before* we start decryption. // // then, if the key turns up while decryption is in progress (and // decryption fails), we will schedule a retry. // (fixes https://github.com/vector-im/element-web/issues/5001) this.addEventToPendingList(event); let res; try { res = await this.olmDevice.decryptGroupMessage(event.getRoomId(), content.sender_key, content.session_id, content.ciphertext, event.getId(), event.getTs()); } catch (e) { if (e.name === "DecryptionError") { // re-throw decryption errors as-is throw e; } let errorCode = _cryptoApi.DecryptionFailureCode.OLM_DECRYPT_GROUP_MESSAGE_ERROR; if ((e === null || e === void 0 ? void 0 : e.message) === "OLM.UNKNOWN_MESSAGE_INDEX") { this.requestKeysForEvent(event); errorCode = _cryptoApi.DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX; } throw new _CryptoBackend.DecryptionError(errorCode, e instanceof Error ? e.message : "Unknown Error: Error is undefined", { session: content.sender_key + "|" + content.session_id }); } if (res === null) { // We've got a message for a session we don't have. // try and get the missing key from the backup first this.crypto.backupManager.queryKeyBackupRateLimited(event.getRoomId(), content.session_id).catch(() => {}); // (XXX: We might actually have received this key since we started // decrypting, in which case we'll have scheduled a retry, and this // request will be redundant. We could probably check to see if the // event is still in the pending list; if not, a retry will have been // scheduled, so we needn't send out the request here.) this.requestKeysForEvent(event); // See if there was a problem with the olm session at the time the // event was sent. Use a fuzz factor of 2 minutes. const problem = await this.olmDevice.sessionMayHaveProblems(content.sender_key, event.getTs() - 120000); if (problem) { this.prefixedLogger.info(`When handling UISI from ${event.getSender()} (sender key ${content.sender_key}): ` + `recent session problem with that sender:`, problem); let problemDescription = PROBLEM_DESCRIPTIONS[problem.type] || PROBLEM_DESCRIPTIONS.unknown; if (problem.fixed) { problemDescription += " Trying to create a new secure channel and re-requesting the keys."; } throw new _CryptoBackend.DecryptionError(_cryptoApi.DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID, problemDescription, { session: content.sender_key + "|" + content.session_id }); } throw new _CryptoBackend.DecryptionError(_cryptoApi.DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID, "The sender's device has not sent us the keys for this message.", { session: content.sender_key + "|" + content.session_id }); } // Success. We can remove the event from the pending list, if // that hasn't already happened. However, if the event was // decrypted with an untrusted key, leave it on the pending // list so it will be retried if we find a trusted key later. if (!res.untrusted) { this.removeEventFromPendingList(event); } const payload = JSON.parse(res.result); // belt-and-braces check that the room id matches that indicated by the HS // (this is somewhat redundant, since the megolm session is scoped to the // room, so neither the sender nor a MITM can lie about the room_id). if (payload.room_id !== event.getRoomId()) { throw new _CryptoBackend.DecryptionError(_cryptoApi.DecryptionFailureCode.MEGOLM_BAD_ROOM, "Message intended for room " + payload.room_id); } return { clearEvent: payload, senderCurve25519Key: res.senderKey, claimedEd25519Key: res.keysClaimed.ed25519, forwardingCurve25519KeyChain: res.forwardingCurve25519KeyChain, untrusted: res.untrusted }; } requestKeysForEvent(event) { const wireContent = event.getWireContent(); const recipients = event.getKeyRequestRecipients(this.userId); this.crypto.requestRoomKey({ room_id: event.getRoomId(), algorithm: wireContent.algorithm, sender_key: wireContent.sender_key, session_id: wireContent.session_id }, recipients); } /** * Add an event to the list of those awaiting their session keys. * * @internal * */ addEventToPendingList(event) { var _senderPendingEvents$; const content = event.getWireContent(); const senderKey = content.sender_key; const sessionId = content.session_id; if (!this.pendingEvents.has(senderKey)) { this.pendingEvents.set(senderKey, new Map()); } const senderPendingEvents = this.pendingEvents.get(senderKey); if (!senderPendingEvents.has(sessionId)) { senderPendingEvents.set(sessionId, new Set()); } (_senderPendingEvents$ = senderPendingEvents.get(sessionId)) === null || _senderPendingEvents$ === void 0 || _senderPendingEvents$.add(event); } /** * Remove an event from the list of those awaiting their session keys. * * @internal * */ removeEventFromPendingList(event) { const content = event.getWireContent(); const senderKey = content.sender_key; const sessionId = content.session_id; const senderPendingEvents = this.pendingEvents.get(senderKey); const pendingEvents = senderPendingEvents === null || senderPendingEvents === void 0 ? void 0 : senderPendingEvents.get(sessionId); if (!pendingEvents) { return; } pendingEvents.delete(event); if (pendingEvents.size === 0) { senderPendingEvents.delete(sessionId); } if (senderPendingEvents.size === 0) { this.pendingEvents.delete(senderKey); } } /** * Parse a RoomKey out of an `m.room_key` event. * * @param event - the event containing the room key. * * @returns The `RoomKey` if it could be successfully parsed out of the * event. * * @internal * */ roomKeyFromEvent(event) { const senderKey = event.getSenderKey(); const conten