From 74f17bdc60c5fa2c12b12fee1cf519a97e670fe3 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 15 Dec 2020 16:09:47 +0100 Subject: [PATCH] Clean up encrypted message handling --- src/Olm.cpp | 104 ++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/src/Olm.cpp b/src/Olm.cpp index 2b7382c9..b05737a5 100644 --- a/src/Olm.cpp +++ b/src/Olm.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include "Cache.h" @@ -169,59 +170,60 @@ handle_olm_message(const OlmMessage &msg) } if (!payload.is_null()) { - std::string msg_type = payload["type"]; + mtx::events::collections::DeviceEvents device_event; - if (msg_type == to_string(mtx::events::EventType::KeyVerificationAccept)) { - ChatPage::instance()->receivedDeviceVerificationAccept( - payload["content"]); - return; - } else if (msg_type == - to_string(mtx::events::EventType::KeyVerificationRequest)) { - ChatPage::instance()->receivedDeviceVerificationRequest( - payload["content"], payload["sender"]); - return; - } else if (msg_type == - to_string(mtx::events::EventType::KeyVerificationCancel)) { - ChatPage::instance()->receivedDeviceVerificationCancel( - payload["content"]); - return; - } else if (msg_type == - to_string(mtx::events::EventType::KeyVerificationKey)) { - ChatPage::instance()->receivedDeviceVerificationKey( - payload["content"]); - return; - } else if (msg_type == - to_string(mtx::events::EventType::KeyVerificationMac)) { - ChatPage::instance()->receivedDeviceVerificationMac( - payload["content"]); - return; - } else if (msg_type == - to_string(mtx::events::EventType::KeyVerificationStart)) { - ChatPage::instance()->receivedDeviceVerificationStart( - payload["content"], payload["sender"]); - return; - } else if (msg_type == - to_string(mtx::events::EventType::KeyVerificationReady)) { - ChatPage::instance()->receivedDeviceVerificationReady( - payload["content"]); - return; - } else if (msg_type == - to_string(mtx::events::EventType::KeyVerificationDone)) { - ChatPage::instance()->receivedDeviceVerificationDone( - payload["content"]); - return; - } else if (msg_type == to_string(mtx::events::EventType::RoomKey)) { - mtx::events::DeviceEvent roomKey = - payload; - create_inbound_megolm_session(roomKey, msg.sender_key); - return; - } else if (msg_type == - to_string(mtx::events::EventType::ForwardedRoomKey)) { - mtx::events::DeviceEvent - roomKey = payload; - import_inbound_megolm_session(roomKey); - return; + { + std::string msg_type = payload["type"]; + json event_array = json::array(); + event_array.push_back(payload); + + std::vector temp_events; + mtx::responses::utils::parse_device_events(event_array, + temp_events); + if (temp_events.empty()) { + nhlog::crypto()->warn("Decrypted unknown event: {}", + payload.dump()); + continue; + } + device_event = temp_events.at(0); } + + using namespace mtx::events; + if (auto e = + std::get_if>(&device_event)) { + ChatPage::instance()->receivedDeviceVerificationAccept(e->content); + } else if (auto e = std::get_if>( + &device_event)) { + ChatPage::instance()->receivedDeviceVerificationRequest(e->content, + e->sender); + } else if (auto e = std::get_if>( + &device_event)) { + ChatPage::instance()->receivedDeviceVerificationCancel(e->content); + } else if (auto e = std::get_if>( + &device_event)) { + ChatPage::instance()->receivedDeviceVerificationKey(e->content); + } else if (auto e = std::get_if>( + &device_event)) { + ChatPage::instance()->receivedDeviceVerificationMac(e->content); + } else if (auto e = std::get_if>( + &device_event)) { + ChatPage::instance()->receivedDeviceVerificationStart(e->content, + e->sender); + } else if (auto e = std::get_if>( + &device_event)) { + ChatPage::instance()->receivedDeviceVerificationReady(e->content); + } else if (auto e = std::get_if>( + &device_event)) { + ChatPage::instance()->receivedDeviceVerificationDone(e->content); + } else if (auto roomKey = + std::get_if>(&device_event)) { + create_inbound_megolm_session(*roomKey, msg.sender_key); + } else if (auto roomKey = std::get_if>( + &device_event)) { + import_inbound_megolm_session(*roomKey); + } + + return; } } }