Merge pull request #198 from Chethan2k1/device-verification-dialogs

Fix nheko build(uses mtxclient device-verification)
This commit is contained in:
DeepBlueV7.X 2020-05-15 14:56:11 +02:00 committed by GitHub
commit ce1c6a7df3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 41 deletions

View file

@ -337,7 +337,7 @@ if(USE_BUNDLED_MTXCLIENT)
FetchContent_Declare( FetchContent_Declare(
MatrixClient MatrixClient
GIT_REPOSITORY https://github.com/Nheko-Reborn/mtxclient.git GIT_REPOSITORY https://github.com/Nheko-Reborn/mtxclient.git
GIT_TAG 71bd56b66cf634341ffef804f07d33f01fd57c25 GIT_TAG dfc39651ac02b48787b9d4e27932ef2feceb162c
) )
FetchContent_MakeAvailable(MatrixClient) FetchContent_MakeAvailable(MatrixClient)
else() else()

View file

@ -146,9 +146,9 @@
"name": "mtxclient", "name": "mtxclient",
"sources": [ "sources": [
{ {
"sha256": "7055f1459a43a12f27f949564624f13cc593ac894e445e6de0e6563ad38ebc3e", "sha256": "4f859cdcb42b052ed07d55ac8890e9051fa49a1e8bc43648106ddce351b6868b",
"type": "archive", "type": "archive",
"url": "https://github.com/Nheko-Reborn/mtxclient/archive/71bd56b66cf634341ffef804f07d33f01fd57c25.tar.gz" "url": "https://github.com/Nheko-Reborn/mtxclient/archive/dfc39651ac02b48787b9d4e27932ef2feceb162c.tar.gz"
} }
] ]
}, },

View file

@ -1011,7 +1011,7 @@ ChatPage::trySync()
// TODO: fine grained error handling // TODO: fine grained error handling
try { try {
cache::saveState(res); cache::saveState(res);
olm::handle_to_device_messages(res.to_device); olm::handle_to_device_messages(res.to_device.events);
auto updates = cache::roomUpdates(res); auto updates = cache::roomUpdates(res);
@ -1234,7 +1234,7 @@ ChatPage::initialSyncHandler(const mtx::responses::Sync &res, mtx::http::Request
try { try {
cache::saveState(res); cache::saveState(res);
olm::handle_to_device_messages(res.to_device); olm::handle_to_device_messages(res.to_device.events);
emit initializeViews(std::move(res.rooms)); emit initializeViews(std::move(res.rooms));
emit initializeRoomList(cache::roomInfo()); emit initializeRoomList(cache::roomInfo());

View file

@ -23,52 +23,55 @@ client()
} }
void void
handle_to_device_messages(const std::vector<nlohmann::json> &msgs) handle_to_device_messages(const std::vector<mtx::events::collections::DeviceEvents> &msgs)
{ {
if (msgs.empty()) if (msgs.empty())
return; return;
nhlog::crypto()->info("received {} to_device messages", msgs.size()); nhlog::crypto()->info("received {} to_device messages", msgs.size());
nlohmann::json j_msg;
for (const auto &msg : msgs) { for (const auto &msg : msgs) {
if (msg.count("type") == 0) { j_msg = std::visit([](auto &e) { return json(e); }, std::move(msg));
if (j_msg.count("type") == 0) {
nhlog::crypto()->warn("received message with no type field: {}", nhlog::crypto()->warn("received message with no type field: {}",
msg.dump(2)); j_msg.dump(2));
continue; continue;
} }
std::string msg_type = msg.at("type"); std::string msg_type = j_msg.at("type");
if (msg_type == to_string(mtx::events::EventType::RoomEncrypted)) { if (msg_type == to_string(mtx::events::EventType::RoomEncrypted)) {
try { try {
OlmMessage olm_msg = msg; OlmMessage olm_msg = j_msg;
handle_olm_message(std::move(olm_msg)); handle_olm_message(std::move(olm_msg));
} catch (const nlohmann::json::exception &e) { } catch (const nlohmann::json::exception &e) {
nhlog::crypto()->warn( nhlog::crypto()->warn(
"parsing error for olm message: {} {}", e.what(), msg.dump(2)); "parsing error for olm message: {} {}", e.what(), j_msg.dump(2));
} catch (const std::invalid_argument &e) { } catch (const std::invalid_argument &e) {
nhlog::crypto()->warn( nhlog::crypto()->warn("validation error for olm message: {} {}",
"validation error for olm message: {} {}", e.what(), msg.dump(2)); e.what(),
j_msg.dump(2));
} }
} else if (msg_type == to_string(mtx::events::EventType::RoomKeyRequest)) { } else if (msg_type == to_string(mtx::events::EventType::RoomKeyRequest)) {
nhlog::crypto()->warn("handling key request event: {}", msg.dump(2)); nhlog::crypto()->warn("handling key request event: {}", j_msg.dump(2));
try { try {
mtx::events::msg::KeyRequest req = msg; mtx::events::DeviceEvent<mtx::events::msg::KeyRequest> req = j_msg;
if (req.action == mtx::events::msg::RequestAction::Request) if (req.content.action == mtx::events::msg::RequestAction::Request)
handle_key_request_message(std::move(req)); handle_key_request_message(req);
else else
nhlog::crypto()->warn( nhlog::crypto()->warn(
"ignore key request (unhandled action): {}", "ignore key request (unhandled action): {}",
req.request_id); req.content.request_id);
} catch (const nlohmann::json::exception &e) { } catch (const nlohmann::json::exception &e) {
nhlog::crypto()->warn( nhlog::crypto()->warn(
"parsing error for key_request message: {} {}", "parsing error for key_request message: {} {}",
e.what(), e.what(),
msg.dump(2)); j_msg.dump(2));
} }
} else { } else {
nhlog::crypto()->warn("unhandled event: {}", msg.dump(2)); nhlog::crypto()->warn("unhandled event: {}", j_msg.dump(2));
} }
} }
} }
@ -341,51 +344,53 @@ send_key_request_for(const std::string &room_id,
} }
void void
handle_key_request_message(const mtx::events::msg::KeyRequest &req) handle_key_request_message(const mtx::events::DeviceEvent<mtx::events::msg::KeyRequest> &req)
{ {
if (req.algorithm != MEGOLM_ALGO) { if (req.content.algorithm != MEGOLM_ALGO) {
nhlog::crypto()->debug("ignoring key request {} with invalid algorithm: {}", nhlog::crypto()->debug("ignoring key request {} with invalid algorithm: {}",
req.request_id, req.content.request_id,
req.algorithm); req.content.algorithm);
return; return;
} }
// Check if we were the sender of the session being requested. // Check if we were the sender of the session being requested.
if (req.sender_key != olm::client()->identity_keys().curve25519) { if (req.content.sender_key != olm::client()->identity_keys().curve25519) {
nhlog::crypto()->debug("ignoring key request {} because we were not the sender: " nhlog::crypto()->debug("ignoring key request {} because we were not the sender: "
"\nrequested({}) ours({})", "\nrequested({}) ours({})",
req.request_id, req.content.request_id,
req.sender_key, req.content.sender_key,
olm::client()->identity_keys().curve25519); olm::client()->identity_keys().curve25519);
return; return;
} }
// Check if we have the keys for the requested session. // Check if we have the keys for the requested session.
if (!cache::outboundMegolmSessionExists(req.room_id)) { if (!cache::outboundMegolmSessionExists(req.content.room_id)) {
nhlog::crypto()->warn("requested session not found in room: {}", req.room_id); nhlog::crypto()->warn("requested session not found in room: {}",
req.content.room_id);
return; return;
} }
// Check that the requested session_id and the one we have saved match. // Check that the requested session_id and the one we have saved match.
const auto session = cache::getOutboundMegolmSession(req.room_id); const auto session = cache::getOutboundMegolmSession(req.content.room_id);
if (req.session_id != session.data.session_id) { if (req.content.session_id != session.data.session_id) {
nhlog::crypto()->warn("session id of retrieved session doesn't match the request: " nhlog::crypto()->warn("session id of retrieved session doesn't match the request: "
"requested({}), ours({})", "requested({}), ours({})",
req.session_id, req.content.session_id,
session.data.session_id); session.data.session_id);
return; return;
} }
if (!cache::isRoomMember(req.sender, req.room_id)) { if (!cache::isRoomMember(req.sender, req.content.room_id)) {
nhlog::crypto()->warn( nhlog::crypto()->warn(
"user {} that requested the session key is not member of the room {}", "user {} that requested the session key is not member of the room {}",
req.sender, req.sender,
req.room_id); req.content.room_id);
return; return;
} }
if (!utils::respondsToKeyRequests(req.room_id)) { if (!utils::respondsToKeyRequests(req.content.room_id)) {
nhlog::crypto()->debug("ignoring all key requests for room {}", req.room_id); nhlog::crypto()->debug("ignoring all key requests for room {}",
req.content.room_id);
return; return;
} }
@ -393,11 +398,11 @@ handle_key_request_message(const mtx::events::msg::KeyRequest &req)
// Prepare the m.room_key event. // Prepare the m.room_key event.
// //
auto payload = json{{"algorithm", "m.megolm.v1.aes-sha2"}, auto payload = json{{"algorithm", "m.megolm.v1.aes-sha2"},
{"room_id", req.room_id}, {"room_id", req.content.room_id},
{"session_id", req.session_id}, {"session_id", req.content.session_id},
{"session_key", session.data.session_key}}; {"session_key", session.data.session_key}};
send_megolm_key_to_device(req.sender, req.requesting_device_id, payload); send_megolm_key_to_device(req.sender, req.content.requesting_device_id, payload);
} }
void void

View file

@ -40,7 +40,7 @@ mtx::crypto::OlmClient *
client(); client();
void void
handle_to_device_messages(const std::vector<nlohmann::json> &msgs); handle_to_device_messages(const std::vector<mtx::events::collections::DeviceEvents> &msgs);
nlohmann::json nlohmann::json
try_olm_decryption(const std::string &sender_key, try_olm_decryption(const std::string &sender_key,
@ -77,7 +77,7 @@ send_key_request_for(const std::string &room_id,
const mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> &); const mtx::events::EncryptedEvent<mtx::events::msg::Encrypted> &);
void void
handle_key_request_message(const mtx::events::msg::KeyRequest &); handle_key_request_message(const mtx::events::DeviceEvent<mtx::events::msg::KeyRequest> &);
void void
send_megolm_key_to_device(const std::string &user_id, send_megolm_key_to_device(const std::string &user_id,