2020-09-03 15:36:17 +02: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.
|
|
|
|
*/
|
|
|
|
|
2020-09-10 12:09:17 +02:00
|
|
|
import {MEGOLM_ALGORITHM, DecryptionSource} from "./common.js";
|
2020-09-03 15:36:17 +02:00
|
|
|
import {groupBy} from "../../utils/groupBy.js";
|
2020-09-10 12:09:17 +02:00
|
|
|
import {mergeMap} from "../../utils/mergeMap.js";
|
2020-09-03 15:36:17 +02:00
|
|
|
import {makeTxnId} from "../common.js";
|
|
|
|
|
2020-09-03 17:50:28 +02:00
|
|
|
const ENCRYPTED_TYPE = "m.room.encrypted";
|
2020-09-03 15:36:17 +02:00
|
|
|
|
2020-09-18 13:10:41 +02:00
|
|
|
function encodeMissingSessionKey(senderKey, sessionId) {
|
|
|
|
return `${senderKey}|${sessionId}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeMissingSessionKey(key) {
|
|
|
|
const [senderKey, sessionId] = key.split("|");
|
|
|
|
return {senderKey, sessionId};
|
|
|
|
}
|
|
|
|
|
2020-09-03 15:36:17 +02:00
|
|
|
export class RoomEncryption {
|
2020-09-18 13:10:41 +02:00
|
|
|
constructor({room, deviceTracker, olmEncryption, megolmEncryption, megolmDecryption, encryptionParams, storage, sessionBackup, notifyMissingMegolmSession, clock}) {
|
2020-09-03 15:36:17 +02:00
|
|
|
this._room = room;
|
|
|
|
this._deviceTracker = deviceTracker;
|
|
|
|
this._olmEncryption = olmEncryption;
|
2020-09-03 17:50:28 +02:00
|
|
|
this._megolmEncryption = megolmEncryption;
|
2020-09-04 16:27:14 +02:00
|
|
|
this._megolmDecryption = megolmDecryption;
|
2020-09-03 15:36:17 +02:00
|
|
|
// content of the m.room.encryption event
|
2020-09-03 17:50:28 +02:00
|
|
|
this._encryptionParams = encryptionParams;
|
2020-09-04 12:09:19 +02:00
|
|
|
|
|
|
|
this._megolmBackfillCache = this._megolmDecryption.createSessionCache();
|
|
|
|
this._megolmSyncCache = this._megolmDecryption.createSessionCache();
|
2020-09-23 17:34:25 +02:00
|
|
|
// session => event ids of messages we tried to decrypt and the session was missing
|
|
|
|
this._missingSessions = new SessionToEventIdsMap();
|
|
|
|
// sessions that may or may not be missing, but that while
|
|
|
|
// looking for a particular session came up as a candidate and were
|
|
|
|
// added to the cache to prevent further lookups from storage
|
|
|
|
this._missingSessionCandidates = new SessionToEventIdsMap();
|
2020-09-08 10:50:39 +02:00
|
|
|
this._senderDeviceCache = new Map();
|
2020-09-08 14:24:48 +02:00
|
|
|
this._storage = storage;
|
2020-09-17 15:58:46 +02:00
|
|
|
this._sessionBackup = sessionBackup;
|
2020-09-17 18:00:00 +02:00
|
|
|
this._notifyMissingMegolmSession = notifyMissingMegolmSession;
|
2020-09-18 13:10:41 +02:00
|
|
|
this._clock = clock;
|
2020-09-18 13:08:18 +02:00
|
|
|
this._disposed = false;
|
2020-09-25 10:45:00 +02:00
|
|
|
this._isFlushingRoomKeyShares = false;
|
2020-09-17 14:20:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-17 18:00:00 +02:00
|
|
|
async enableSessionBackup(sessionBackup) {
|
2020-09-17 15:58:46 +02:00
|
|
|
if (this._sessionBackup) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-17 14:20:15 +02:00
|
|
|
this._sessionBackup = sessionBackup;
|
2020-09-23 17:34:25 +02:00
|
|
|
for(const {senderKey, sessionId} of this._missingSessions.getSessions()) {
|
2020-09-18 13:10:41 +02:00
|
|
|
await this._requestMissingSessionFromBackup(senderKey, sessionId, null);
|
2020-09-17 18:00:00 +02:00
|
|
|
}
|
2020-09-04 12:09:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
notifyTimelineClosed() {
|
|
|
|
// empty the backfill cache when closing the timeline
|
|
|
|
this._megolmBackfillCache.dispose();
|
|
|
|
this._megolmBackfillCache = this._megolmDecryption.createSessionCache();
|
2020-09-08 10:50:39 +02:00
|
|
|
this._senderDeviceCache = new Map(); // purge the sender device cache
|
2020-09-03 15:36:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async writeMemberChanges(memberChanges, txn) {
|
2020-09-11 14:41:12 +02:00
|
|
|
const memberChangesArray = Array.from(memberChanges.values());
|
|
|
|
if (memberChangesArray.some(m => m.hasLeft)) {
|
|
|
|
this._megolmEncryption.discardOutboundSession(this._room.id, txn);
|
2020-09-08 11:09:09 +02:00
|
|
|
}
|
2020-09-11 14:41:12 +02:00
|
|
|
if (memberChangesArray.some(m => m.hasJoined)) {
|
|
|
|
await this._addShareRoomKeyOperationForNewMembers(memberChangesArray, txn);
|
|
|
|
}
|
|
|
|
await this._deviceTracker.writeMemberChanges(this._room, memberChanges, txn);
|
2020-09-03 15:36:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 12:09:17 +02:00
|
|
|
// this happens before entries exists, as they are created by the syncwriter
|
|
|
|
// but we want to be able to map it back to something in the timeline easily
|
|
|
|
// when retrying decryption.
|
|
|
|
async prepareDecryptAll(events, source, isTimelineOpen, txn) {
|
|
|
|
const errors = [];
|
|
|
|
const validEvents = [];
|
|
|
|
for (const event of events) {
|
|
|
|
if (event.redacted_because || event.unsigned?.redacted_because) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (event.content?.algorithm !== MEGOLM_ALGORITHM) {
|
|
|
|
errors.set(event.event_id, new Error("Unsupported algorithm: " + event.content?.algorithm));
|
|
|
|
}
|
|
|
|
validEvents.push(event);
|
2020-09-08 17:16:01 +02:00
|
|
|
}
|
2020-09-10 12:09:17 +02:00
|
|
|
let customCache;
|
|
|
|
let sessionCache;
|
|
|
|
if (source === DecryptionSource.Sync) {
|
|
|
|
sessionCache = this._megolmSyncCache;
|
|
|
|
} else if (source === DecryptionSource.Timeline) {
|
|
|
|
sessionCache = this._megolmBackfillCache;
|
|
|
|
} else if (source === DecryptionSource.Retry) {
|
|
|
|
// when retrying, we could have mixed events from at the bottom of the timeline (sync)
|
|
|
|
// and somewhere else, so create a custom cache we use just for this operation.
|
2020-09-18 13:10:41 +02:00
|
|
|
customCache = this._megolmDecryption.createSessionCache();
|
2020-09-10 12:09:17 +02:00
|
|
|
sessionCache = customCache;
|
|
|
|
} else {
|
|
|
|
throw new Error("Unknown source: " + source);
|
2020-09-04 12:10:12 +02:00
|
|
|
}
|
2020-09-10 12:09:17 +02:00
|
|
|
const preparation = await this._megolmDecryption.prepareDecryptAll(
|
|
|
|
this._room.id, validEvents, sessionCache, txn);
|
|
|
|
if (customCache) {
|
|
|
|
customCache.dispose();
|
2020-09-04 12:10:12 +02:00
|
|
|
}
|
2020-09-23 17:34:25 +02:00
|
|
|
return new DecryptionPreparation(preparation, errors, {isTimelineOpen, source}, this, events);
|
2020-09-10 12:09:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 17:34:25 +02:00
|
|
|
async _processDecryptionResults(events, results, errors, flags, txn) {
|
|
|
|
for (const event of events) {
|
|
|
|
const error = errors.get(event.event_id);
|
|
|
|
if (error?.code === "MEGOLM_NO_SESSION") {
|
|
|
|
this._addMissingSessionEvent(event, flags.source);
|
|
|
|
} else {
|
|
|
|
this._missingSessions.removeEvent(event);
|
|
|
|
this._missingSessionCandidates.removeEvent(event);
|
2020-09-10 12:09:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags.isTimelineOpen) {
|
|
|
|
for (const result of results.values()) {
|
|
|
|
await this._verifyDecryptionResult(result, txn);
|
|
|
|
}
|
2020-09-08 10:50:39 +02:00
|
|
|
}
|
2020-09-04 12:09:19 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 10:50:39 +02:00
|
|
|
async _verifyDecryptionResult(result, txn) {
|
|
|
|
let device = this._senderDeviceCache.get(result.senderCurve25519Key);
|
|
|
|
if (!device) {
|
|
|
|
device = await this._deviceTracker.getDeviceByCurve25519Key(result.senderCurve25519Key, txn);
|
|
|
|
this._senderDeviceCache.set(result.senderCurve25519Key, device);
|
|
|
|
}
|
|
|
|
if (device) {
|
|
|
|
result.setDevice(device);
|
|
|
|
} else if (!this._room.isTrackingMembers) {
|
|
|
|
result.setRoomNotTrackedYet();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:10:41 +02:00
|
|
|
_addMissingSessionEvent(event, source) {
|
2020-09-23 17:34:25 +02:00
|
|
|
const isNewSession = this._missingSessions.addEvent(event);
|
|
|
|
if (isNewSession) {
|
|
|
|
const senderKey = event.content?.["sender_key"];
|
|
|
|
const sessionId = event.content?.["session_id"];
|
2020-09-18 13:10:41 +02:00
|
|
|
this._requestMissingSessionFromBackup(senderKey, sessionId, source);
|
2020-09-04 12:10:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:10:41 +02:00
|
|
|
async _requestMissingSessionFromBackup(senderKey, sessionId, source) {
|
|
|
|
// if the message came from sync, wait 10s to see if the room key arrives,
|
|
|
|
// and only after that proceed to request from backup
|
|
|
|
if (source === DecryptionSource.Sync) {
|
|
|
|
await this._clock.createTimeout(10000).elapsed();
|
2020-09-23 17:34:25 +02:00
|
|
|
if (this._disposed || !this._missingSessions.hasSession(senderKey, sessionId)) {
|
2020-09-18 13:10:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 09:30:25 +02:00
|
|
|
// show prompt to enable secret storage
|
|
|
|
if (!this._sessionBackup) {
|
|
|
|
this._notifyMissingMegolmSession();
|
|
|
|
return;
|
|
|
|
}
|
2020-09-18 13:10:41 +02:00
|
|
|
|
2020-09-17 17:57:52 +02:00
|
|
|
try {
|
|
|
|
const session = await this._sessionBackup.getSession(this._room.id, sessionId);
|
|
|
|
if (session?.algorithm === MEGOLM_ALGORITHM) {
|
|
|
|
if (session["sender_key"] !== senderKey) {
|
|
|
|
console.warn("Got session key back from backup with different sender key, ignoring", {session, senderKey});
|
|
|
|
return;
|
|
|
|
}
|
2020-09-25 16:42:41 +02:00
|
|
|
const txn = this._storage.readWriteTxn([this._storage.storeNames.inboundGroupSessions]);
|
2020-09-17 17:57:52 +02:00
|
|
|
let roomKey;
|
|
|
|
try {
|
|
|
|
roomKey = await this._megolmDecryption.addRoomKeyFromBackup(
|
|
|
|
this._room.id, sessionId, session, txn);
|
|
|
|
} catch (err) {
|
|
|
|
txn.abort();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await txn.complete();
|
|
|
|
|
|
|
|
if (roomKey) {
|
2020-09-23 17:34:25 +02:00
|
|
|
// this will reattempt decryption
|
|
|
|
await this._room.notifyRoomKey(roomKey);
|
2020-09-17 17:57:52 +02:00
|
|
|
}
|
|
|
|
} else if (session?.algorithm) {
|
|
|
|
console.info(`Backed-up session of unknown algorithm: ${session.algorithm}`);
|
2020-09-17 14:20:15 +02:00
|
|
|
}
|
2020-09-17 17:57:52 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`Could not get session ${sessionId} from backup`, err);
|
2020-09-17 14:20:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {RoomKeyDescription}
|
|
|
|
* @property {RoomKeyDescription} senderKey the curve25519 key of the sender
|
|
|
|
* @property {RoomKeyDescription} sessionId
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {Array<RoomKeyDescription>} roomKeys
|
|
|
|
* @return {Array<string>} the event ids that should be retried to decrypt
|
|
|
|
*/
|
2020-09-23 17:34:25 +02:00
|
|
|
getEventIdsForRoomKey(roomKey) {
|
2020-09-24 10:35:09 +02:00
|
|
|
// TODO: we could concat both results here, and only put stuff in
|
|
|
|
// candidates if it is not in missing sessions to use a bit less memory
|
2020-09-23 17:34:25 +02:00
|
|
|
let eventIds = this._missingSessions.getEventIds(roomKey.senderKey, roomKey.sessionId);
|
|
|
|
if (!eventIds) {
|
|
|
|
eventIds = this._missingSessionCandidates.getEventIds(roomKey.senderKey, roomKey.sessionId);
|
2020-09-04 12:10:12 +02:00
|
|
|
}
|
2020-09-23 17:34:25 +02:00
|
|
|
return eventIds;
|
|
|
|
}
|
|
|
|
|
2020-09-23 17:59:42 +02:00
|
|
|
/**
|
|
|
|
* caches mapping of session to event id of all encrypted candidates
|
|
|
|
* and filters to return only the candidates for the given room key
|
|
|
|
*/
|
2020-09-23 17:34:25 +02:00
|
|
|
findAndCacheEntriesForRoomKey(roomKey, candidateEntries) {
|
2020-09-23 17:59:42 +02:00
|
|
|
const matches = [];
|
|
|
|
|
|
|
|
for (const entry of candidateEntries) {
|
|
|
|
if (entry.eventType === ENCRYPTED_TYPE) {
|
|
|
|
this._missingSessionCandidates.addEvent(entry.event);
|
|
|
|
const senderKey = entry.event?.content?.["sender_key"];
|
|
|
|
const sessionId = entry.event?.content?.["session_id"];
|
|
|
|
if (senderKey === roomKey.senderKey && sessionId === roomKey.sessionId) {
|
|
|
|
matches.push(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
|
2020-09-04 12:10:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 15:36:17 +02:00
|
|
|
async encrypt(type, content, hsApi) {
|
2020-09-11 14:41:12 +02:00
|
|
|
await this._deviceTracker.trackRoom(this._room);
|
2020-09-03 17:50:28 +02:00
|
|
|
const megolmResult = await this._megolmEncryption.encrypt(this._room.id, type, content, this._encryptionParams);
|
|
|
|
if (megolmResult.roomKeyMessage) {
|
2020-09-26 09:12:34 +02:00
|
|
|
// TODO: should we await this??
|
2020-09-11 14:41:12 +02:00
|
|
|
this._shareNewRoomKey(megolmResult.roomKeyMessage, hsApi);
|
2020-09-03 17:50:28 +02:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: ENCRYPTED_TYPE,
|
|
|
|
content: megolmResult.content
|
|
|
|
};
|
2020-09-03 15:36:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:38:27 +02:00
|
|
|
needsToShareKeys(memberChanges) {
|
|
|
|
for (const m of memberChanges.values()) {
|
2020-09-11 14:41:12 +02:00
|
|
|
if (m.hasJoined) {
|
2020-09-08 14:38:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:41:12 +02:00
|
|
|
async _shareNewRoomKey(roomKeyMessage, hsApi) {
|
|
|
|
const devices = await this._deviceTracker.devicesForTrackedRoom(this._room.id, hsApi);
|
|
|
|
const userIds = Array.from(devices.reduce((set, device) => set.add(device.userId), new Set()));
|
2020-09-08 14:38:27 +02:00
|
|
|
|
2020-09-11 14:41:12 +02:00
|
|
|
// store operation for room key share, in case we don't finish here
|
2020-09-25 16:42:41 +02:00
|
|
|
const writeOpTxn = this._storage.readWriteTxn([this._storage.storeNames.operations]);
|
2020-09-11 14:41:12 +02:00
|
|
|
let operationId;
|
|
|
|
try {
|
|
|
|
operationId = this._writeRoomKeyShareOperation(roomKeyMessage, userIds, writeOpTxn);
|
|
|
|
} catch (err) {
|
|
|
|
writeOpTxn.abort();
|
|
|
|
throw err;
|
2020-09-08 14:24:48 +02:00
|
|
|
}
|
2020-09-11 14:41:12 +02:00
|
|
|
await writeOpTxn.complete();
|
|
|
|
// TODO: at this point we have the room key stored, and the rest is sort of optional
|
|
|
|
// it would be nice if we could signal SendQueue that any error from here on is non-fatal and
|
|
|
|
// return the encrypted payload.
|
2020-09-08 14:24:48 +02:00
|
|
|
|
2020-09-11 14:41:12 +02:00
|
|
|
// send the room key
|
|
|
|
await this._sendRoomKey(roomKeyMessage, devices, hsApi);
|
|
|
|
|
|
|
|
// remove the operation
|
2020-09-25 16:42:41 +02:00
|
|
|
const removeOpTxn = this._storage.readWriteTxn([this._storage.storeNames.operations]);
|
2020-09-11 14:41:12 +02:00
|
|
|
try {
|
|
|
|
removeOpTxn.operations.remove(operationId);
|
|
|
|
} catch (err) {
|
|
|
|
removeOpTxn.abort();
|
|
|
|
throw err;
|
2020-09-08 14:24:48 +02:00
|
|
|
}
|
2020-09-11 14:41:12 +02:00
|
|
|
await removeOpTxn.complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _addShareRoomKeyOperationForNewMembers(memberChangesArray, txn) {
|
|
|
|
const userIds = memberChangesArray.filter(m => m.hasJoined).map(m => m.userId);
|
|
|
|
const roomKeyMessage = await this._megolmEncryption.createRoomKeyMessage(
|
|
|
|
this._room.id, txn);
|
2020-09-08 14:24:48 +02:00
|
|
|
if (roomKeyMessage) {
|
2020-09-11 14:41:12 +02:00
|
|
|
this._writeRoomKeyShareOperation(roomKeyMessage, userIds, txn);
|
2020-09-08 14:24:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:41:12 +02:00
|
|
|
_writeRoomKeyShareOperation(roomKeyMessage, userIds, txn) {
|
|
|
|
const id = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString();
|
|
|
|
txn.operations.add({
|
|
|
|
id,
|
|
|
|
type: "share_room_key",
|
|
|
|
scope: this._room.id,
|
|
|
|
userIds,
|
|
|
|
roomKeyMessage,
|
|
|
|
});
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
async flushPendingRoomKeyShares(hsApi, operations = null) {
|
2020-09-25 10:45:00 +02:00
|
|
|
// this has to be reentrant as it can be called from Room.start while still running
|
|
|
|
if (this._isFlushingRoomKeyShares) {
|
|
|
|
return;
|
2020-09-11 14:41:12 +02:00
|
|
|
}
|
2020-09-25 10:45:00 +02:00
|
|
|
this._isFlushingRoomKeyShares = true;
|
|
|
|
try {
|
|
|
|
if (!operations) {
|
2020-09-25 16:42:41 +02:00
|
|
|
const txn = this._storage.readTxn([this._storage.storeNames.operations]);
|
2020-09-25 10:45:00 +02:00
|
|
|
operations = await txn.operations.getAllByTypeAndScope("share_room_key", this._room.id);
|
2020-09-11 14:41:12 +02:00
|
|
|
}
|
2020-09-25 10:45:00 +02:00
|
|
|
for (const operation of operations) {
|
|
|
|
// just to be sure
|
|
|
|
if (operation.type !== "share_room_key") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const devices = await this._deviceTracker.devicesForRoomMembers(this._room.id, operation.userIds, hsApi);
|
|
|
|
await this._sendRoomKey(operation.roomKeyMessage, devices, hsApi);
|
2020-09-25 16:42:41 +02:00
|
|
|
const removeTxn = this._storage.readWriteTxn([this._storage.storeNames.operations]);
|
2020-09-25 10:45:00 +02:00
|
|
|
try {
|
|
|
|
removeTxn.operations.remove(operation.id);
|
|
|
|
} catch (err) {
|
|
|
|
removeTxn.abort();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await removeTxn.complete();
|
2020-09-11 14:41:12 +02:00
|
|
|
}
|
2020-09-25 10:45:00 +02:00
|
|
|
} finally {
|
|
|
|
this._isFlushingRoomKeyShares = false;
|
2020-09-08 14:24:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _sendRoomKey(roomKeyMessage, devices, hsApi) {
|
|
|
|
const messages = await this._olmEncryption.encrypt(
|
|
|
|
"m.room_key", roomKeyMessage, devices, hsApi);
|
|
|
|
await this._sendMessagesToDevices(ENCRYPTED_TYPE, messages, hsApi);
|
|
|
|
}
|
|
|
|
|
2020-09-03 15:36:17 +02:00
|
|
|
async _sendMessagesToDevices(type, messages, hsApi) {
|
|
|
|
const messagesByUser = groupBy(messages, message => message.device.userId);
|
|
|
|
const payload = {
|
|
|
|
messages: Array.from(messagesByUser.entries()).reduce((userMap, [userId, messages]) => {
|
|
|
|
userMap[userId] = messages.reduce((deviceMap, message) => {
|
|
|
|
deviceMap[message.device.deviceId] = message.content;
|
|
|
|
return deviceMap;
|
|
|
|
}, {});
|
|
|
|
return userMap;
|
|
|
|
}, {})
|
|
|
|
};
|
|
|
|
const txnId = makeTxnId();
|
|
|
|
await hsApi.sendToDevice(type, payload, txnId).response();
|
|
|
|
}
|
2020-09-18 13:08:18 +02:00
|
|
|
|
|
|
|
dispose() {
|
|
|
|
this._disposed = true;
|
|
|
|
}
|
2020-09-03 15:36:17 +02:00
|
|
|
}
|
2020-09-10 12:09:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* wrappers around megolm decryption classes to be able to post-process
|
|
|
|
* the decryption results before turning them
|
|
|
|
*/
|
|
|
|
class DecryptionPreparation {
|
2020-09-23 17:34:25 +02:00
|
|
|
constructor(megolmDecryptionPreparation, extraErrors, flags, roomEncryption, events) {
|
2020-09-10 12:09:17 +02:00
|
|
|
this._megolmDecryptionPreparation = megolmDecryptionPreparation;
|
|
|
|
this._extraErrors = extraErrors;
|
|
|
|
this._flags = flags;
|
|
|
|
this._roomEncryption = roomEncryption;
|
2020-09-23 17:34:25 +02:00
|
|
|
this._events = events;
|
2020-09-10 12:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async decrypt() {
|
|
|
|
return new DecryptionChanges(
|
|
|
|
await this._megolmDecryptionPreparation.decrypt(),
|
|
|
|
this._extraErrors,
|
|
|
|
this._flags,
|
2020-09-23 17:34:25 +02:00
|
|
|
this._roomEncryption,
|
|
|
|
this._events);
|
2020-09-10 12:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
2020-09-10 16:40:30 +02:00
|
|
|
this._megolmDecryptionPreparation.dispose();
|
2020-09-10 12:09:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DecryptionChanges {
|
2020-09-23 17:34:25 +02:00
|
|
|
constructor(megolmDecryptionChanges, extraErrors, flags, roomEncryption, events) {
|
2020-09-10 12:09:17 +02:00
|
|
|
this._megolmDecryptionChanges = megolmDecryptionChanges;
|
|
|
|
this._extraErrors = extraErrors;
|
|
|
|
this._flags = flags;
|
|
|
|
this._roomEncryption = roomEncryption;
|
2020-09-23 17:34:25 +02:00
|
|
|
this._events = events;
|
2020-09-10 12:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async write(txn) {
|
|
|
|
const {results, errors} = await this._megolmDecryptionChanges.write(txn);
|
|
|
|
mergeMap(this._extraErrors, errors);
|
2020-09-23 17:34:25 +02:00
|
|
|
await this._roomEncryption._processDecryptionResults(this._events, results, errors, this._flags, txn);
|
2020-09-10 12:09:17 +02:00
|
|
|
return new BatchDecryptionResult(results, errors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BatchDecryptionResult {
|
|
|
|
constructor(results, errors) {
|
|
|
|
this.results = results;
|
|
|
|
this.errors = errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
applyToEntries(entries) {
|
|
|
|
for (const entry of entries) {
|
|
|
|
const result = this.results.get(entry.id);
|
|
|
|
if (result) {
|
|
|
|
entry.setDecryptionResult(result);
|
|
|
|
} else {
|
|
|
|
const error = this.errors.get(entry.id);
|
|
|
|
if (error) {
|
|
|
|
entry.setDecryptionError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 17:34:25 +02:00
|
|
|
|
|
|
|
class SessionToEventIdsMap {
|
|
|
|
constructor() {
|
|
|
|
this._eventIdsBySession = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
addEvent(event) {
|
|
|
|
let isNewSession = false;
|
|
|
|
const senderKey = event.content?.["sender_key"];
|
|
|
|
const sessionId = event.content?.["session_id"];
|
|
|
|
const key = encodeMissingSessionKey(senderKey, sessionId);
|
|
|
|
let eventIds = this._eventIdsBySession.get(key);
|
|
|
|
// new missing session
|
|
|
|
if (!eventIds) {
|
|
|
|
eventIds = new Set();
|
|
|
|
this._eventIdsBySession.set(key, eventIds);
|
|
|
|
isNewSession = true;
|
|
|
|
}
|
|
|
|
eventIds.add(event.event_id);
|
|
|
|
return isNewSession;
|
|
|
|
}
|
|
|
|
|
|
|
|
getEventIds(senderKey, sessionId) {
|
|
|
|
const key = encodeMissingSessionKey(senderKey, sessionId);
|
|
|
|
const entriesForSession = this._eventIdsBySession.get(key);
|
|
|
|
if (entriesForSession) {
|
|
|
|
return [...entriesForSession];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getSessions() {
|
|
|
|
return Array.from(this._eventIdsBySession.keys()).map(decodeMissingSessionKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
hasSession(senderKey, sessionId) {
|
|
|
|
return this._eventIdsBySession.has(encodeMissingSessionKey(senderKey, sessionId));
|
|
|
|
}
|
|
|
|
|
|
|
|
removeEvent(event) {
|
|
|
|
let hasRemovedSession = false;
|
|
|
|
const senderKey = event.content?.["sender_key"];
|
|
|
|
const sessionId = event.content?.["session_id"];
|
|
|
|
const key = encodeMissingSessionKey(senderKey, sessionId);
|
|
|
|
let eventIds = this._eventIdsBySession.get(key);
|
|
|
|
if (eventIds) {
|
|
|
|
if (eventIds.delete(event.event_id)) {
|
|
|
|
if (!eventIds.length) {
|
|
|
|
this._eventIdsBySession.delete(key);
|
|
|
|
hasRemovedSession = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasRemovedSession;
|
|
|
|
}
|
|
|
|
}
|