From 2ab6eb2a0c799a9c4dc9b34d7c3f2ccb9c8ab5e4 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 20 Feb 2020 15:29:15 +0000 Subject: [PATCH] [Client] Dont create new inbound session when session matches --- lib/src/client.dart | 16 +++++++++++++--- lib/src/room.dart | 5 ++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 0727991..b5ebf7e 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1025,7 +1025,10 @@ class Client { try { toDeviceEvent = decryptToDeviceEvent(toDeviceEvent); } catch (e) { - print("[LibOlm] Could not decrypt to device event: " + e.toString()); + print( + "[LibOlm] Could not decrypt to device event from ${toDeviceEvent.sender}: " + + e.toString()); + print(toDeviceEvent.sender); toDeviceEvent = ToDeviceEvent.fromJson(events[i]); } } @@ -1320,7 +1323,9 @@ class Client { Room room = getRoomById(toDeviceEvent.content["room_id"]); if (room != null && toDeviceEvent.content["session_id"] is String) { final String sessionId = toDeviceEvent.content["session_id"]; - room.setSessionKey(sessionId, toDeviceEvent.content); + if (room != null) { + room.setSessionKey(sessionId, toDeviceEvent.content); + } } break; } @@ -1570,7 +1575,11 @@ class Client { List existingSessions = olmSessions[senderKey]; if (existingSessions != null) { for (olm.Session session in existingSessions) { - if ((type == 0 && session.matches_inbound(body) == 1) || type == 1) { + if (type == 0 && session.matches_inbound(body) != 0) { + plaintext = session.decrypt(type, body); + storeOlmSession(senderKey, session); + break; + } else if (type == 1) { try { plaintext = session.decrypt(type, body); storeOlmSession(senderKey, session); @@ -1679,6 +1688,7 @@ class Client { }; final olm.EncryptResult encryptResult = existingSessions.first.encrypt(json.encode(payload)); + storeOlmSession(device.curve25519Key, existingSessions.first); encryptedMessage["ciphertext"][device.curve25519Key] = { "type": encryptResult.type, "body": encryptResult.body, diff --git a/lib/src/room.dart b/lib/src/room.dart index cfd7c79..176e6ff 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -836,7 +836,7 @@ class Room { .getItem("/clients/${client.deviceID}/rooms/${this.id}/session_keys"); if (sessionKeysPickle?.isNotEmpty ?? false) { final Map map = json.decode(sessionKeysPickle); - this._sessionKeys = {}; + if (this._sessionKeys == null) this._sessionKeys = {}; for (var entry in map.entries) { try { this._sessionKeys[entry.key] = @@ -848,6 +848,9 @@ class Room { } } } + await client.storeAPI?.setItem( + "/clients/${client.deviceID}/rooms/${this.id}/session_keys", + json.encode(sessionKeys)); _fullyRestored = true; }