Actually forward keys using m.forwared_room_key

This commit is contained in:
Nicolas Werner 2020-10-18 02:02:14 +02:00
parent c25fd947a8
commit 1f68bc60b5
2 changed files with 27 additions and 22 deletions

View file

@ -1,8 +1,8 @@
#include "Olm.h"
#include <QObject> #include <QObject>
#include <variant> #include <variant>
#include "Olm.h"
#include "Cache.h" #include "Cache.h"
#include "Cache_p.h" #include "Cache_p.h"
#include "ChatPage.h" #include "ChatPage.h"
@ -494,9 +494,6 @@ handle_key_request_message(const mtx::events::DeviceEvent<mtx::events::msg::KeyR
nhlog::crypto()->warn("requested session not found in room: {}", nhlog::crypto()->warn("requested session not found in room: {}",
req.content.room_id); req.content.room_id);
nhlog::crypto()->warn("requested session not found in room: {}",
req.content.room_id);
return; return;
} }
@ -528,6 +525,7 @@ handle_key_request_message(const mtx::events::DeviceEvent<mtx::events::msg::KeyR
for (const auto &dev : verificationStatus->verified_devices) { for (const auto &dev : verificationStatus->verified_devices) {
if (dev == req.content.requesting_device_id) { if (dev == req.content.requesting_device_id) {
verifiedDevice = true; verifiedDevice = true;
nhlog::crypto()->debug("Verified device: {}", dev);
break; break;
} }
} }
@ -543,18 +541,24 @@ handle_key_request_message(const mtx::events::DeviceEvent<mtx::events::msg::KeyR
// //
// Prepare the m.room_key event. // Prepare the m.room_key event.
// //
auto payload = json{{"algorithm", "m.megolm.v1.aes-sha2"}, mtx::events::msg::ForwardedRoomKey forward_key{};
{"room_id", req.content.room_id}, forward_key.algorithm = MEGOLM_ALGO;
{"session_id", req.content.session_id}, forward_key.room_id = index.room_id;
{"session_key", session_key}}; forward_key.session_id = index.session_id;
forward_key.session_key = session_key;
forward_key.sender_key = index.sender_key;
send_megolm_key_to_device(req.sender, req.content.requesting_device_id, payload); // TODO(Nico): Figure out if this is correct
forward_key.sender_claimed_ed25519_key = olm::client()->identity_keys().ed25519;
forward_key.forwarding_curve25519_key_chain = {};
send_megolm_key_to_device(req.sender, req.content.requesting_device_id, forward_key);
} }
void void
send_megolm_key_to_device(const std::string &user_id, send_megolm_key_to_device(const std::string &user_id,
const std::string &device_id, const std::string &device_id,
const json &payload) const mtx::events::msg::ForwardedRoomKey &payload)
{ {
mtx::requests::QueryKeys req; mtx::requests::QueryKeys req;
req.device_keys[user_id] = {device_id}; req.device_keys[user_id] = {device_id};
@ -613,17 +617,13 @@ send_megolm_key_to_device(const std::string &user_id,
return; return;
} }
auto room_key = olm::client()
->create_room_key_event(UserId(user_id), pks.ed25519, payload)
.dump();
mtx::requests::ClaimKeys claim_keys; mtx::requests::ClaimKeys claim_keys;
claim_keys.one_time_keys[user_id][device_id] = mtx::crypto::SIGNED_CURVE25519; claim_keys.one_time_keys[user_id][device_id] = mtx::crypto::SIGNED_CURVE25519;
http::client()->claim_keys( http::client()->claim_keys(
claim_keys, claim_keys,
[room_key, user_id, device_id, pks](const mtx::responses::ClaimKeys &res, [payload, user_id, device_id, pks](const mtx::responses::ClaimKeys &res,
mtx::http::RequestErr err) { mtx::http::RequestErr err) {
if (err) { if (err) {
nhlog::net()->warn("claim keys error: {} {} {}", nhlog::net()->warn("claim keys error: {} {} {}",
err->matrix_error.error, err->matrix_error.error,
@ -665,8 +665,12 @@ send_megolm_key_to_device(const std::string &user_id,
auto olm_session = olm::client()->create_outbound_session( auto olm_session = olm::client()->create_outbound_session(
pks.curve25519, device.begin()->at("key")); pks.curve25519, device.begin()->at("key"));
device_msg = olm::client()->create_olm_encrypted_content( mtx::events::DeviceEvent<mtx::events::msg::ForwardedRoomKey>
olm_session.get(), room_key, pks.curve25519); room_key;
room_key.content = payload;
room_key.type = mtx::events::EventType::ForwardedRoomKey;
device_msg = olm::client()->create_olm_encrypted_content(
olm_session.get(), json(room_key).dump(), pks.curve25519);
cache::saveOlmSession(pks.curve25519, std::move(olm_session)); cache::saveOlmSession(pks.curve25519, std::move(olm_session));
} catch (const json::exception &e) { } catch (const json::exception &e) {
@ -682,7 +686,7 @@ send_megolm_key_to_device(const std::string &user_id,
body["messages"][user_id][device_id] = device_msg; body["messages"][user_id][device_id] = device_msg;
nhlog::net()->info( nhlog::net()->info(
"sending m.room_key event to {}:{}", user_id, device_id); "sending m.forwarded_room_key event to {}:{}", user_id, device_id);
http::client()->send_to_device( http::client()->send_to_device(
"m.room.encrypted", body, [user_id](mtx::http::RequestErr err) { "m.room.encrypted", body, [user_id](mtx::http::RequestErr err) {
if (err) { if (err) {
@ -692,7 +696,8 @@ send_megolm_key_to_device(const std::string &user_id,
err->matrix_error.error); err->matrix_error.error);
} }
nhlog::net()->info("m.room_key send to {}", user_id); nhlog::net()->info("m.forwarded_room_key send to {}",
user_id);
}); });
}); });
}); });

View file

@ -108,6 +108,6 @@ handle_key_request_message(const mtx::events::DeviceEvent<mtx::events::msg::KeyR
void void
send_megolm_key_to_device(const std::string &user_id, send_megolm_key_to_device(const std::string &user_id,
const std::string &device_id, const std::string &device_id,
const json &payload); const mtx::events::msg::ForwardedRoomKey &payload);
} // namespace olm } // namespace olm