From 1b8e481559586f2816e9e93e4833909b538cfc97 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 17 Sep 2020 17:59:02 +0200 Subject: [PATCH] keys from backup need to be imported with import_session, not create --- src/matrix/e2ee/megolm/Decryption.js | 73 +++++++++++++++------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/src/matrix/e2ee/megolm/Decryption.js b/src/matrix/e2ee/megolm/Decryption.js index 1723351a..09662e96 100644 --- a/src/matrix/e2ee/megolm/Decryption.js +++ b/src/matrix/e2ee/megolm/Decryption.js @@ -138,10 +138,16 @@ export class Decryption { return; } - const sessionEntry = await this._writeInboundSession( - roomId, senderKey, claimedEd25519Key, sessionId, sessionKey, txn); - if (sessionEntry) { - newSessions.push(sessionEntry); + const session = new this._olm.InboundGroupSession(); + try { + session.create(sessionKey); + const sessionEntry = await this._writeInboundSession( + session, roomId, senderKey, claimedEd25519Key, sessionId, txn); + if (sessionEntry) { + newSessions.push(sessionEntry); + } + } finally { + session.free(); } } // this will be passed to the Room in notifyRoomKeys @@ -171,41 +177,40 @@ export class Decryption { ) { return; } - return await this._writeInboundSession( - roomId, senderKey, claimedEd25519Key, sessionId, sessionKey, txn); - } - - async _writeInboundSession(roomId, senderKey, claimedEd25519Key, sessionId, sessionKey, txn) { const session = new this._olm.InboundGroupSession(); try { - session.create(sessionKey); - - let incomingSessionIsBetter = true; - const existingSessionEntry = await txn.inboundGroupSessions.get(roomId, senderKey, sessionId); - if (existingSessionEntry) { - const existingSession = new this._olm.InboundGroupSession(); - try { - existingSession.unpickle(this._pickleKey, existingSessionEntry.session); - incomingSessionIsBetter = session.first_known_index() < existingSession.first_known_index(); - } finally { - existingSession.free(); - } - } - - if (incomingSessionIsBetter) { - const sessionEntry = { - roomId, - senderKey, - sessionId, - session: session.pickle(this._pickleKey), - claimedKeys: {ed25519: claimedEd25519Key}, - }; - txn.inboundGroupSessions.set(sessionEntry); - return sessionEntry; - } + session.import_session(sessionKey); + return await this._writeInboundSession( + session, roomId, senderKey, claimedEd25519Key, sessionId, txn); } finally { session.free(); } } + + async _writeInboundSession(session, roomId, senderKey, claimedEd25519Key, sessionId, txn) { + let incomingSessionIsBetter = true; + const existingSessionEntry = await txn.inboundGroupSessions.get(roomId, senderKey, sessionId); + if (existingSessionEntry) { + const existingSession = new this._olm.InboundGroupSession(); + try { + existingSession.unpickle(this._pickleKey, existingSessionEntry.session); + incomingSessionIsBetter = session.first_known_index() < existingSession.first_known_index(); + } finally { + existingSession.free(); + } + } + + if (incomingSessionIsBetter) { + const sessionEntry = { + roomId, + senderKey, + sessionId, + session: session.pickle(this._pickleKey), + claimedKeys: {ed25519: claimedEd25519Key}, + }; + txn.inboundGroupSessions.set(sessionEntry); + return sessionEntry; + } + } }