adapt Session and RoomEncryption to megolm/Decryption API changes

This commit is contained in:
Bruno Windels 2021-10-22 17:48:53 +02:00
parent ac23119838
commit 66a93ee108
2 changed files with 20 additions and 54 deletions

View file

@ -26,7 +26,7 @@ import {DeviceMessageHandler} from "./DeviceMessageHandler.js";
import {Account as E2EEAccount} from "./e2ee/Account.js"; import {Account as E2EEAccount} from "./e2ee/Account.js";
import {Decryption as OlmDecryption} from "./e2ee/olm/Decryption.js"; import {Decryption as OlmDecryption} from "./e2ee/olm/Decryption.js";
import {Encryption as OlmEncryption} from "./e2ee/olm/Encryption.js"; import {Encryption as OlmEncryption} from "./e2ee/olm/Encryption.js";
import {Decryption as MegOlmDecryption} from "./e2ee/megolm/Decryption.js"; import {Decryption as MegOlmDecryption} from "./e2ee/megolm/Decryption";
import {SessionBackup} from "./e2ee/megolm/SessionBackup.js"; import {SessionBackup} from "./e2ee/megolm/SessionBackup.js";
import {Encryption as MegOlmEncryption} from "./e2ee/megolm/Encryption.js"; import {Encryption as MegOlmEncryption} from "./e2ee/megolm/Encryption.js";
import {MEGOLM_ALGORITHM} from "./e2ee/common.js"; import {MEGOLM_ALGORITHM} from "./e2ee/common.js";
@ -137,11 +137,8 @@ export class Session {
now: this._platform.clock.now, now: this._platform.clock.now,
ownDeviceId: this._sessionInfo.deviceId, ownDeviceId: this._sessionInfo.deviceId,
}); });
this._megolmDecryption = new MegOlmDecryption({ const keyLoader = new KeyLoader(this._olm, PICKLE_KEY, 20);
pickleKey: PICKLE_KEY, this._megolmDecryption = new MegOlmDecryption(keyLoader, this._olmWorker);
olm: this._olm,
olmWorker: this._olmWorker,
});
this._deviceMessageHandler.enableEncryption({olmDecryption, megolmDecryption: this._megolmDecryption}); this._deviceMessageHandler.enableEncryption({olmDecryption, megolmDecryption: this._megolmDecryption});
} }
@ -319,6 +316,7 @@ export class Session {
dispose() { dispose() {
this._olmWorker?.dispose(); this._olmWorker?.dispose();
this._sessionBackup?.dispose(); this._sessionBackup?.dispose();
this._megolmDecryption.dispose();
for (const room of this._rooms.values()) { for (const room of this._rooms.values()) {
room.dispose(); room.dispose();
} }

View file

@ -36,8 +36,6 @@ export class RoomEncryption {
this._megolmDecryption = megolmDecryption; this._megolmDecryption = megolmDecryption;
// content of the m.room.encryption event // content of the m.room.encryption event
this._encryptionParams = encryptionParams; this._encryptionParams = encryptionParams;
this._megolmBackfillCache = this._megolmDecryption.createSessionCache();
this._megolmSyncCache = this._megolmDecryption.createSessionCache(1);
// caches devices to verify events // caches devices to verify events
this._senderDeviceCache = new Map(); this._senderDeviceCache = new Map();
this._storage = storage; this._storage = storage;
@ -76,9 +74,6 @@ export class RoomEncryption {
} }
notifyTimelineClosed() { notifyTimelineClosed() {
// empty the backfill cache when closing the timeline
this._megolmBackfillCache.dispose();
this._megolmBackfillCache = this._megolmDecryption.createSessionCache();
this._senderDeviceCache = new Map(); // purge the sender device cache this._senderDeviceCache = new Map(); // purge the sender device cache
} }
@ -112,27 +107,8 @@ export class RoomEncryption {
} }
validEvents.push(event); validEvents.push(event);
} }
let customCache;
let sessionCache;
// we have different caches so we can keep them small but still
// have backfill and sync not invalidate each other
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.
customCache = this._megolmDecryption.createSessionCache();
sessionCache = customCache;
} else {
throw new Error("Unknown source: " + source);
}
const preparation = await this._megolmDecryption.prepareDecryptAll( const preparation = await this._megolmDecryption.prepareDecryptAll(
this._room.id, validEvents, newKeys, sessionCache, txn); this._room.id, validEvents, newKeys, txn);
if (customCache) {
customCache.dispose();
}
return new DecryptionPreparation(preparation, errors, source, this, events); return new DecryptionPreparation(preparation, errors, source, this, events);
} }
@ -208,16 +184,15 @@ export class RoomEncryption {
try { try {
const session = await this._sessionBackup.getSession(this._room.id, sessionId, log); const session = await this._sessionBackup.getSession(this._room.id, sessionId, log);
if (session?.algorithm === MEGOLM_ALGORITHM) { if (session?.algorithm === MEGOLM_ALGORITHM) {
if (session["sender_key"] !== senderKey) { let roomKey = this._megolmDecryption.roomKeyFromBackup(this._room.id, sessionId, session);
log.set("wrong_sender_key", session["sender_key"]); if (roomKey) {
if (roomKey.senderKey !== senderKey) {
log.set("wrong_sender_key", roomKey.senderKey);
log.logLevel = log.level.Warn; log.logLevel = log.level.Warn;
return; return;
} }
let roomKey = this._megolmDecryption.roomKeyFromBackup(this._room.id, sessionId, session);
if (roomKey) {
let keyIsBestOne = false; let keyIsBestOne = false;
let retryEventIds; let retryEventIds;
try {
const txn = await this._storage.readWriteTxn([this._storage.storeNames.inboundGroupSessions]); const txn = await this._storage.readWriteTxn([this._storage.storeNames.inboundGroupSessions]);
try { try {
keyIsBestOne = await this._megolmDecryption.writeRoomKey(roomKey, txn); keyIsBestOne = await this._megolmDecryption.writeRoomKey(roomKey, txn);
@ -230,11 +205,6 @@ export class RoomEncryption {
throw err; throw err;
} }
await txn.complete(); await txn.complete();
} finally {
// can still access properties on it afterwards
// this is just clearing the internal sessionInfo
roomKey.dispose();
}
if (keyIsBestOne) { if (keyIsBestOne) {
await log.wrap("retryDecryption", log => this._room.notifyRoomKey(roomKey, retryEventIds || [], log)); await log.wrap("retryDecryption", log => this._room.notifyRoomKey(roomKey, retryEventIds || [], log));
} }
@ -466,8 +436,6 @@ export class RoomEncryption {
dispose() { dispose() {
this._disposed = true; this._disposed = true;
this._megolmBackfillCache.dispose();
this._megolmSyncCache.dispose();
} }
} }