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.
This commit is contained in:
Nicolas Werner 2023-11-19 20:09:38 +01:00
parent 5ddc11d9b4
commit ff82452816
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
6 changed files with 72 additions and 29 deletions

View file

@ -924,9 +924,29 @@ Cache::saveInboundMegolmSession(const MegolmSessionIndex &index,
std::string_view value;
if (inboundMegolmSessionDb_.get(txn, key, value)) {
auto oldSession = unpickle<InboundSessionObject>(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<GroupSessionData>();
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;
}
}