From 1d6746e4c97c132214bba374ab22e08711ac4c97 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Wed, 27 Jun 2018 21:24:25 +0300 Subject: [PATCH] Upgrade matrix-structs & mtxclient --- deps/CMakeLists.txt | 4 ++-- include/Olm.hpp | 25 +++++++------------------ src/Olm.cpp | 38 +++++++++++++++++++++++++++----------- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt index 20b356de..4994df38 100644 --- a/deps/CMakeLists.txt +++ b/deps/CMakeLists.txt @@ -37,10 +37,10 @@ set(BOOST_SHA256 5721818253e6a0989583192f96782c4a98eb6204965316df9f5ad75819225ca9) set(MATRIX_STRUCTS_URL https://github.com/mujx/matrix-structs) -set(MATRIX_STRUCTS_TAG eeb7373729a1618e2b3838407863342b88b8a0de) +set(MATRIX_STRUCTS_TAG c24cb9b38312dfa24b33413847e3238600c678cd) set(MTXCLIENT_URL https://github.com/mujx/mtxclient) -set(MTXCLIENT_TAG 96fd35e57d36511b10b7d30de7227c6cd2ffa386) +set(MTXCLIENT_TAG 73491268f94ddeb606284836bb5f512d11b0e249) set(OLM_URL https://git.matrix.org/git/olm.git) set(OLM_TAG 4065c8e11a33ba41133a086ed3de4da94dcb6bae) diff --git a/include/Olm.hpp b/include/Olm.hpp index eb7f9061..58568081 100644 --- a/include/Olm.hpp +++ b/include/Olm.hpp @@ -10,26 +10,13 @@ constexpr auto OLM_ALGO = "m.olm.v1.curve25519-aes-sha2"; namespace olm { -struct OlmCipherContent -{ - std::string body; - uint8_t type; -}; - -inline void -from_json(const nlohmann::json &obj, OlmCipherContent &msg) -{ - msg.body = obj.at("body"); - msg.type = obj.at("type"); -} - struct OlmMessage { std::string sender_key; std::string sender; using RecipientKey = std::string; - std::map ciphertext; + std::map ciphertext; }; inline void @@ -43,8 +30,9 @@ from_json(const nlohmann::json &obj, OlmMessage &msg) msg.sender = obj.at("sender"); msg.sender_key = obj.at("content").at("sender_key"); - msg.ciphertext = - obj.at("content").at("ciphertext").get>(); + msg.ciphertext = obj.at("content") + .at("ciphertext") + .get>(); } mtx::crypto::OlmClient * @@ -54,7 +42,8 @@ void handle_to_device_messages(const std::vector &msgs); boost::optional -try_olm_decryption(const std::string &sender_key, const OlmCipherContent &content); +try_olm_decryption(const std::string &sender_key, + const mtx::events::msg::OlmCipherContent &content); void handle_olm_message(const OlmMessage &msg); @@ -68,7 +57,7 @@ create_inbound_megolm_session(const std::string &sender, void handle_pre_key_olm_message(const std::string &sender, const std::string &sender_key, - const OlmCipherContent &content); + const mtx::events::msg::OlmCipherContent &content); mtx::events::msg::Encrypted encrypt_group_message(const std::string &room_id, diff --git a/src/Olm.cpp b/src/Olm.cpp index 4d144a2d..8a78fd1d 100644 --- a/src/Olm.cpp +++ b/src/Olm.cpp @@ -29,15 +29,31 @@ handle_to_device_messages(const std::vector &msgs) nhlog::crypto()->info("received {} to_device messages", msgs.size()); for (const auto &msg : msgs) { - try { - OlmMessage olm_msg = msg; - handle_olm_message(std::move(olm_msg)); - } catch (const nlohmann::json::exception &e) { - nhlog::crypto()->warn( - "parsing error for olm message: {} {}", e.what(), msg.dump(2)); - } catch (const std::invalid_argument &e) { - nhlog::crypto()->warn( - "validation error for olm message: {} {}", e.what(), msg.dump(2)); + if (msg.count("type") == 0) { + nhlog::crypto()->warn("received message with no type field: {}", + msg.dump(2)); + continue; + } + + std::string msg_type = msg.at("type"); + + if (msg_type == to_string(mtx::events::EventType::RoomEncrypted)) { + try { + OlmMessage olm_msg = msg; + handle_olm_message(std::move(olm_msg)); + } catch (const nlohmann::json::exception &e) { + nhlog::crypto()->warn( + "parsing error for olm message: {} {}", e.what(), msg.dump(2)); + } catch (const std::invalid_argument &e) { + nhlog::crypto()->warn( + "validation error for olm message: {} {}", e.what(), msg.dump(2)); + } + + // TODO: Move this event type into matrix-structs + } else if (msg_type == "m.room_key_request") { + nhlog::crypto()->warn("handling key request event: {}", msg.dump(2)); + } else { + nhlog::crypto()->warn("unhandled event: {}", msg.dump(2)); } } } @@ -79,7 +95,7 @@ handle_olm_message(const OlmMessage &msg) void handle_pre_key_olm_message(const std::string &sender, const std::string &sender_key, - const OlmCipherContent &content) + const mtx::events::msg::OlmCipherContent &content) { nhlog::crypto()->info("opening olm session with {}", sender); @@ -155,7 +171,7 @@ encrypt_group_message(const std::string &room_id, } boost::optional -try_olm_decryption(const std::string &sender_key, const OlmCipherContent &msg) +try_olm_decryption(const std::string &sender_key, const mtx::events::msg::OlmCipherContent &msg) { auto session_ids = cache::client()->getOlmSessions(sender_key);