diff --git a/src/Olm.cpp b/src/Olm.cpp
index c1598570..78b16be7 100644
--- a/src/Olm.cpp
+++ b/src/Olm.cpp
@@ -1,4 +1,4 @@
-#include <boost/variant.hpp>
+#include <variant>
#include "Olm.h"
@@ -121,7 +121,7 @@ handle_pre_key_olm_message(const std::string &sender,
// We also remove the one time key used to establish that
// session so we'll have to update our copy of the account object.
- cache::client()->saveOlmAccount(olm::client()->save("secret"));
+ cache::saveOlmAccount(olm::client()->save("secret"));
} catch (const mtx::crypto::olm_exception &e) {
nhlog::crypto()->critical(
"failed to create inbound session with {}: {}", sender, e.what());
@@ -149,7 +149,7 @@ handle_pre_key_olm_message(const std::string &sender,
nhlog::crypto()->debug("decrypted message: \n {}", plaintext.dump(2));
try {
- cache::client()->saveOlmSession(sender_key, std::move(inbound_session));
+ cache::saveOlmSession(sender_key, std::move(inbound_session));
} catch (const lmdb::error &e) {
nhlog::db()->warn(
"failed to save inbound olm session from {}: {}", sender, e.what());
@@ -159,15 +159,20 @@ handle_pre_key_olm_message(const std::string &sender,
}
mtx::events::msg::Encrypted
-encrypt_group_message(const std::string &room_id,
- const std::string &device_id,
- const std::string &body)
+encrypt_group_message(const std::string &room_id, const std::string &device_id, nlohmann::json body)
{
using namespace mtx::events;
- // Always chech before for existence.
- auto res = cache::client()->getOutboundMegolmSession(room_id);
- auto payload = olm::client()->encrypt_group_message(res.session, body);
+ // relations shouldn't be encrypted...
+ mtx::common::RelatesTo relation;
+ if (body["content"].count("m.relates_to") != 0) {
+ relation = body["content"]["m.relates_to"];
+ body["content"].erase("m.relates_to");
+ }
+
+ // Always check before for existence.
+ auto res = cache::getOutboundMegolmSession(room_id);
+ auto payload = olm::client()->encrypt_group_message(res.session, body.dump());
// Prepare the m.room.encrypted event.
msg::Encrypted data;
@@ -176,12 +181,13 @@ encrypt_group_message(const std::string &room_id,
data.session_id = res.data.session_id;
data.device_id = device_id;
data.algorithm = MEGOLM_ALGO;
+ data.relates_to = relation;
auto message_index = olm_outbound_group_session_message_index(res.session);
nhlog::crypto()->info("next message_index {}", message_index);
// We need to re-pickle the session after we send a message to save the new message_index.
- cache::client()->updateOutboundMegolmSession(room_id, message_index);
+ cache::updateOutboundMegolmSession(room_id, message_index);
return data;
}
@@ -189,13 +195,13 @@ encrypt_group_message(const std::string &room_id,
nlohmann::json
try_olm_decryption(const std::string &sender_key, const mtx::events::msg::OlmCipherContent &msg)
{
- auto session_ids = cache::client()->getOlmSessions(sender_key);
+ auto session_ids = cache::getOlmSessions(sender_key);
nhlog::crypto()->info("attempt to decrypt message with {} known session_ids",
session_ids.size());
for (const auto &id : session_ids) {
- auto session = cache::client()->getOlmSession(sender_key, id);
+ auto session = cache::getOlmSession(sender_key, id);
if (!session)
continue;
@@ -204,7 +210,7 @@ try_olm_decryption(const std::string &sender_key, const mtx::events::msg::OlmCip
try {
text = olm::client()->decrypt_message(session->get(), msg.type, msg.body);
- cache::client()->saveOlmSession(id, std::move(session.value()));
+ cache::saveOlmSession(id, std::move(session.value()));
} catch (const mtx::crypto::olm_exception &e) {
nhlog::crypto()->debug("failed to decrypt olm message ({}, {}) with {}: {}",
msg.type,
@@ -252,7 +258,7 @@ create_inbound_megolm_session(const std::string &sender,
try {
auto megolm_session = olm::client()->init_inbound_group_session(session_key);
- cache::client()->saveInboundMegolmSession(index, std::move(megolm_session));
+ cache::saveInboundMegolmSession(index, std::move(megolm_session));
} catch (const lmdb::error &e) {
nhlog::crypto()->critical("failed to save inbound megolm session: {}", e.what());
return;
@@ -268,7 +274,7 @@ void
mark_keys_as_published()
{
olm::client()->mark_keys_as_published();
- cache::client()->saveOlmAccount(olm::client()->save(STORAGE_SECRET_KEY));
+ cache::saveOlmAccount(olm::client()->save(STORAGE_SECRET_KEY));
}
void
@@ -289,14 +295,13 @@ request_keys(const std::string &room_id, const std::string &event_id)
return;
}
- if (boost::get<EncryptedEvent<msg::Encrypted>>(&res) == nullptr) {
+ if (!std::holds_alternative<EncryptedEvent<msg::Encrypted>>(res)) {
nhlog::net()->info(
"retrieved event is not encrypted: {} from {}", event_id, room_id);
return;
}
- olm::send_key_request_for(room_id,
- boost::get<EncryptedEvent<msg::Encrypted>>(res));
+ olm::send_key_request_for(room_id, std::get<EncryptedEvent<msg::Encrypted>>(res));
});
}
@@ -356,13 +361,13 @@ handle_key_request_message(const mtx::events::msg::KeyRequest &req)
}
// Check if we have the keys for the requested session.
- if (!cache::client()->outboundMegolmSessionExists(req.room_id)) {
+ if (!cache::outboundMegolmSessionExists(req.room_id)) {
nhlog::crypto()->warn("requested session not found in room: {}", req.room_id);
return;
}
// Check that the requested session_id and the one we have saved match.
- const auto session = cache::client()->getOutboundMegolmSession(req.room_id);
+ const auto session = cache::getOutboundMegolmSession(req.room_id);
if (req.session_id != session.data.session_id) {
nhlog::crypto()->warn("session id of retrieved session doesn't match the request: "
"requested({}), ours({})",
@@ -371,7 +376,7 @@ handle_key_request_message(const mtx::events::msg::KeyRequest &req)
return;
}
- if (!cache::client()->isRoomMember(req.sender, req.room_id)) {
+ if (!cache::isRoomMember(req.sender, req.room_id)) {
nhlog::crypto()->warn(
"user {} that requested the session key is not member of the room {}",
req.sender,
@@ -510,8 +515,7 @@ send_megolm_key_to_device(const std::string &user_id,
device_msg = olm::client()->create_olm_encrypted_content(
olm_session.get(), room_key, pks.curve25519);
- cache::client()->saveOlmSession(pks.curve25519,
- std::move(olm_session));
+ cache::saveOlmSession(pks.curve25519, std::move(olm_session));
} catch (const json::exception &e) {
nhlog::crypto()->warn("creating outbound session: {}",
e.what());
|