Share historical keys

We share all keys with our devices and ones created by us to other
users.
This commit is contained in:
Nicolas Werner 2021-07-17 02:14:44 +02:00
parent 9fadd14871
commit 3f0aa13cb6
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
4 changed files with 56 additions and 23 deletions

View file

@ -582,6 +582,25 @@ Cache::getOutboundMegolmSession(const std::string &room_id)
} }
} }
std::optional<GroupSessionData>
Cache::getMegolmSessionData(const MegolmSessionIndex &index)
{
try {
using namespace mtx::crypto;
auto txn = ro_txn(env_);
std::string_view value;
if (megolmSessionDataDb_.get(txn, json(index).dump(), value)) {
return nlohmann::json::parse(value).get<GroupSessionData>();
}
return std::nullopt;
} catch (std::exception &e) {
nhlog::db()->error("Failed to retrieve Megolm Session Data: {}", e.what());
return std::nullopt;
}
}
// //
// OLM sessions. // OLM sessions.
// //
@ -4622,6 +4641,11 @@ inboundMegolmSessionExists(const MegolmSessionIndex &index)
{ {
return instance_->inboundMegolmSessionExists(index); return instance_->inboundMegolmSessionExists(index);
} }
std::optional<GroupSessionData>
getMegolmSessionData(const MegolmSessionIndex &index)
{
return instance_->getMegolmSessionData(index);
}
// //
// Olm Sessions // Olm Sessions

View file

@ -229,6 +229,8 @@ mtx::crypto::InboundGroupSessionPtr
getInboundMegolmSession(const MegolmSessionIndex &index); getInboundMegolmSession(const MegolmSessionIndex &index);
bool bool
inboundMegolmSessionExists(const MegolmSessionIndex &index); inboundMegolmSessionExists(const MegolmSessionIndex &index);
std::optional<GroupSessionData>
getMegolmSessionData(const MegolmSessionIndex &index);
// //
// Olm Sessions // Olm Sessions

View file

@ -259,6 +259,7 @@ public:
mtx::crypto::InboundGroupSessionPtr getInboundMegolmSession( mtx::crypto::InboundGroupSessionPtr getInboundMegolmSession(
const MegolmSessionIndex &index); const MegolmSessionIndex &index);
bool inboundMegolmSessionExists(const MegolmSessionIndex &index); bool inboundMegolmSessionExists(const MegolmSessionIndex &index);
std::optional<GroupSessionData> getMegolmSessionData(const MegolmSessionIndex &index);
// //
// Olm Sessions // Olm Sessions

View file

@ -631,8 +631,9 @@ encrypt_group_message(const std::string &room_id, const std::string &device_id,
// Saving the new megolm session. // Saving the new megolm session.
GroupSessionData session_data{}; GroupSessionData session_data{};
session_data.message_index = 0; session_data.message_index = 0;
session_data.timestamp = QDateTime::currentMSecsSinceEpoch(); session_data.timestamp = QDateTime::currentMSecsSinceEpoch();
session_data.sender_claimed_ed25519_key = olm::client()->identity_keys().ed25519;
sendSessionTo.clear(); sendSessionTo.clear();
@ -886,21 +887,16 @@ handle_key_request_message(const mtx::events::DeviceEvent<mtx::events::msg::KeyR
return; return;
} }
// Check if we were the sender of the session being requested. // Check if we were the sender of the session being requested (unless it is actually us
if (req.content.sender_key != olm::client()->identity_keys().curve25519) { // requesting the session).
nhlog::crypto()->debug("ignoring key request {} because we were not the sender: " if (req.sender != http::client()->user_id().to_string() &&
"\nrequested({}) ours({})", req.content.sender_key != olm::client()->identity_keys().curve25519) {
req.content.request_id, nhlog::crypto()->debug(
req.content.sender_key, "ignoring key request {} because we did not create the requested session: "
olm::client()->identity_keys().curve25519); "\nrequested({}) ours({})",
return; req.content.request_id,
} req.content.sender_key,
olm::client()->identity_keys().curve25519);
// Check if we have the keys for the requested session.
auto outboundSession = cache::getOutboundMegolmSession(req.content.room_id);
if (!outboundSession.session) {
nhlog::crypto()->warn("requested session not found in room: {}",
req.content.room_id);
return; return;
} }
@ -908,7 +904,15 @@ handle_key_request_message(const mtx::events::DeviceEvent<mtx::events::msg::KeyR
MegolmSessionIndex index{}; MegolmSessionIndex index{};
index.room_id = req.content.room_id; index.room_id = req.content.room_id;
index.session_id = req.content.session_id; index.session_id = req.content.session_id;
index.sender_key = olm::client()->identity_keys().curve25519; index.sender_key = req.content.sender_key;
// Check if we have the keys for the requested session.
auto sessionData = cache::getMegolmSessionData(index);
if (!sessionData) {
nhlog::crypto()->warn("requested session not found in room: {}",
req.content.room_id);
return;
}
const auto session = cache::getInboundMegolmSession(index); const auto session = cache::getInboundMegolmSession(index);
if (!session) { if (!session) {
@ -942,11 +946,11 @@ handle_key_request_message(const mtx::events::DeviceEvent<mtx::events::msg::KeyR
bool shouldSeeKeys = false; bool shouldSeeKeys = false;
uint64_t minimumIndex = -1; uint64_t minimumIndex = -1;
if (outboundSession.data.currently.keys.count(req.sender)) { if (sessionData->currently.keys.count(req.sender)) {
if (outboundSession.data.currently.keys.at(req.sender) if (sessionData->currently.keys.at(req.sender)
.deviceids.count(req.content.requesting_device_id)) { .deviceids.count(req.content.requesting_device_id)) {
shouldSeeKeys = true; shouldSeeKeys = true;
minimumIndex = outboundSession.data.currently.keys.at(req.sender) minimumIndex = sessionData->currently.keys.at(req.sender)
.deviceids.at(req.content.requesting_device_id); .deviceids.at(req.content.requesting_device_id);
} }
} }
@ -976,8 +980,9 @@ handle_key_request_message(const mtx::events::DeviceEvent<mtx::events::msg::KeyR
forward_key.sender_key = index.sender_key; forward_key.sender_key = index.sender_key;
// TODO(Nico): Figure out if this is correct // TODO(Nico): Figure out if this is correct
forward_key.sender_claimed_ed25519_key = olm::client()->identity_keys().ed25519; forward_key.sender_claimed_ed25519_key = sessionData->sender_claimed_ed25519_key;
forward_key.forwarding_curve25519_key_chain = {}; forward_key.forwarding_curve25519_key_chain =
sessionData->forwarding_curve25519_key_chain;
send_megolm_key_to_device( send_megolm_key_to_device(
req.sender, req.content.requesting_device_id, forward_key); req.sender, req.content.requesting_device_id, forward_key);
@ -998,6 +1003,7 @@ send_megolm_key_to_device(const std::string &user_id,
std::map<std::string, std::vector<std::string>> targets; std::map<std::string, std::vector<std::string>> targets;
targets[user_id] = {device_id}; targets[user_id] = {device_id};
send_encrypted_to_device_messages(targets, room_key); send_encrypted_to_device_messages(targets, room_key);
nhlog::crypto()->debug("Forwarded key to {}:{}", user_id, device_id);
} }
DecryptionResult DecryptionResult