From 8e20139079c44504bae30b9b9013f199ebdd788d Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sun, 27 Feb 2022 06:41:48 +0100 Subject: Allow properly editing pending encrypted messages --- src/encryption/Olm.cpp | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'src/encryption/Olm.cpp') diff --git a/src/encryption/Olm.cpp b/src/encryption/Olm.cpp index 5de18fa3..e6426658 100644 --- a/src/encryption/Olm.cpp +++ b/src/encryption/Olm.cpp @@ -470,6 +470,30 @@ handle_pre_key_olm_message(const std::string &sender, return plaintext; } +mtx::events::msg::Encrypted +encrypt_group_message_with_session(mtx::crypto::OutboundGroupSessionPtr &session, + const std::string &device_id, + nlohmann::json body) +{ + using namespace mtx::events; + + // relations shouldn't be encrypted... + mtx::common::Relations relations = mtx::common::parse_relations(body["content"]); + + auto payload = olm::client()->encrypt_group_message(session.get(), body.dump()); + + // Prepare the m.room.encrypted event. + msg::Encrypted data; + data.ciphertext = std::string((char *)payload.data(), payload.size()); + data.sender_key = olm::client()->identity_keys().curve25519; + data.session_id = mtx::crypto::session_id(session.get()); + data.device_id = device_id; + data.algorithm = MEGOLM_ALGO; + data.relations = relations; + + return data; +} + mtx::events::msg::Encrypted encrypt_group_message(const std::string &room_id, const std::string &device_id, nlohmann::json body) { @@ -631,19 +655,7 @@ encrypt_group_message(const std::string &room_id, const std::string &device_id, if (!sendSessionTo.empty()) olm::send_encrypted_to_device_messages(sendSessionTo, megolm_payload); - // relations shouldn't be encrypted... - mtx::common::Relations relations = mtx::common::parse_relations(body["content"]); - - auto payload = olm::client()->encrypt_group_message(session.get(), body.dump()); - - // Prepare the m.room.encrypted event. - msg::Encrypted data; - data.ciphertext = std::string((char *)payload.data(), payload.size()); - data.sender_key = olm::client()->identity_keys().curve25519; - data.session_id = mtx::crypto::session_id(session.get()); - data.device_id = device_id; - data.algorithm = MEGOLM_ALGO; - data.relations = relations; + auto data = encrypt_group_message_with_session(session, device_id, body); group_session_data.message_index = olm_outbound_group_session_message_index(session.get()); nhlog::crypto()->debug("next message_index {}", group_session_data.message_index); -- cgit 1.5.1