From ff82452816449eb9ccea872bf192cf153d596858 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sun, 19 Nov 2023 20:09:38 +0100 Subject: Upgrade trust of megolm sessions when receiving RoomKey Before we only did that, when we basically didn't have the key yet. But since we usually get sent a RoomKey when a new message is sent after we sign in, we were discarding, that those messages should usually now be trusted. --- src/Cache.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/Cache.cpp') diff --git a/src/Cache.cpp b/src/Cache.cpp index 58dc689b..35bfe9dd 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -924,9 +924,29 @@ Cache::saveInboundMegolmSession(const MegolmSessionIndex &index, std::string_view value; if (inboundMegolmSessionDb_.get(txn, key, value)) { auto oldSession = unpickle(std::string(value), pickle_secret_); - if (olm_inbound_group_session_first_known_index(session.get()) > - olm_inbound_group_session_first_known_index(oldSession.get())) { - nhlog::crypto()->warn("Not storing inbound session with newer first known index"); + + auto newIndex = olm_inbound_group_session_first_known_index(session.get()); + auto oldIndex = olm_inbound_group_session_first_known_index(oldSession.get()); + + // merge trusted > untrusted + // first known index minimum + if (megolmSessionDataDb_.get(txn, key, value)) { + auto oldData = nlohmann::json::parse(value).get(); + if (oldData.trusted && newIndex >= oldIndex) { + nhlog::crypto()->warn( + "Not storing inbound session of lesser trust or bigger index."); + return; + } + + oldData.trusted = data.trusted || oldData.trusted; + + if (newIndex < oldIndex) { + inboundMegolmSessionDb_.put(txn, key, pickled); + oldData.message_index = newIndex; + } + + megolmSessionDataDb_.put(txn, key, nlohmann::json(oldData).dump()); + txn.commit(); return; } } -- cgit 1.5.1