Simplify outbound session setup

Don't send inbound session to self and claim and send all keys at once.
This commit is contained in:
Nicolas Werner 2020-09-20 23:04:14 +02:00
parent 8eb74daf76
commit 54db9c89ed
9 changed files with 209 additions and 214 deletions

View file

@ -342,7 +342,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 0665c8baf4af0ce192adb8ca97761b63b681d569 GIT_TAG f84611f129b46746a4b586acaba54fc31a303bc6
) )
FetchContent_MakeAvailable(MatrixClient) FetchContent_MakeAvailable(MatrixClient)
else() else()

View file

@ -146,7 +146,7 @@
"name": "mtxclient", "name": "mtxclient",
"sources": [ "sources": [
{ {
"commit": "0665c8baf4af0ce192adb8ca97761b63b681d569", "commit": "f84611f129b46746a4b586acaba54fc31a303bc6",
"type": "git", "type": "git",
"url": "https://github.com/Nheko-Reborn/mtxclient.git" "url": "https://github.com/Nheko-Reborn/mtxclient.git"
} }

View file

@ -139,7 +139,8 @@ Cache::Cache(const QString &userId, QObject *parent)
, localUserId_{userId} , localUserId_{userId}
{ {
setup(); setup();
connect(this, connect(
this,
&Cache::updateUserCacheFlag, &Cache::updateUserCacheFlag,
this, this,
[this](const std::string &user_id) { [this](const std::string &user_id) {
@ -152,7 +153,8 @@ Cache::Cache(const QString &userId, QObject *parent)
} }
}, },
Qt::QueuedConnection); Qt::QueuedConnection);
connect(this, connect(
this,
&Cache::deleteLeftUsers, &Cache::deleteLeftUsers,
this, this,
[this](const std::string &user_id) { deleteUserCache(user_id); }, [this](const std::string &user_id) { deleteUserCache(user_id); },

View file

@ -606,7 +606,8 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
connect( connect(
this, &ChatPage::tryInitialSyncCb, this, &ChatPage::tryInitialSync, Qt::QueuedConnection); this, &ChatPage::tryInitialSyncCb, this, &ChatPage::tryInitialSync, Qt::QueuedConnection);
connect(this, &ChatPage::trySyncCb, this, &ChatPage::trySync, Qt::QueuedConnection); connect(this, &ChatPage::trySyncCb, this, &ChatPage::trySync, Qt::QueuedConnection);
connect(this, connect(
this,
&ChatPage::tryDelayedSyncCb, &ChatPage::tryDelayedSyncCb,
this, this,
[this]() { QTimer::singleShot(RETRY_TIMEOUT, this, &ChatPage::trySync); }, [this]() { QTimer::singleShot(RETRY_TIMEOUT, this, &ChatPage::trySync); },

View file

@ -581,9 +581,11 @@ send_megolm_key_to_device(const std::string &user_id,
->create_room_key_event(UserId(user_id), pks.ed25519, payload) ->create_room_key_event(UserId(user_id), pks.ed25519, payload)
.dump(); .dump();
mtx::requests::ClaimKeys claim_keys;
claim_keys.one_time_keys[user_id][device_id] = mtx::crypto::SIGNED_CURVE25519;
http::client()->claim_keys( http::client()->claim_keys(
user_id, claim_keys,
{device_id},
[room_key, user_id, device_id, pks](const mtx::responses::ClaimKeys &res, [room_key, user_id, device_id, pks](const mtx::responses::ClaimKeys &res,
mtx::http::RequestErr err) { mtx::http::RequestErr err) {
if (err) { if (err) {

Binary file not shown.

View file

@ -32,7 +32,8 @@ EventStore::EventStore(std::string room_id, QObject *)
this->last = range->last; this->last = range->last;
} }
connect(this, connect(
this,
&EventStore::eventFetched, &EventStore::eventFetched,
this, this,
[this](std::string id, [this](std::string id,
@ -48,7 +49,8 @@ EventStore::EventStore(std::string room_id, QObject *)
}, },
Qt::QueuedConnection); Qt::QueuedConnection);
connect(this, connect(
this,
&EventStore::oldMessagesRetrieved, &EventStore::oldMessagesRetrieved,
this, this,
[this](const mtx::responses::Messages &res) { [this](const mtx::responses::Messages &res) {
@ -128,7 +130,8 @@ EventStore::EventStore(std::string room_id, QObject *)
event->data); event->data);
}); });
connect(this, connect(
this,
&EventStore::messageFailed, &EventStore::messageFailed,
this, this,
[this](std::string txn_id) { [this](std::string txn_id) {
@ -148,7 +151,8 @@ EventStore::EventStore(std::string room_id, QObject *)
}, },
Qt::QueuedConnection); Qt::QueuedConnection);
connect(this, connect(
this,
&EventStore::messageSent, &EventStore::messageSent,
this, this,
[this](std::string txn_id, std::string event_id) { [this](std::string txn_id, std::string event_id) {

View file

@ -204,7 +204,8 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj
, room_id_(room_id) , room_id_(room_id)
, manager_(manager) , manager_(manager)
{ {
connect(this, connect(
this,
&TimelineModel::redactionFailed, &TimelineModel::redactionFailed,
this, this,
[](const QString &msg) { emit ChatPage::instance()->showNotification(msg); }, [](const QString &msg) { emit ChatPage::instance()->showNotification(msg); },
@ -217,13 +218,13 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj
Qt::QueuedConnection); Qt::QueuedConnection);
connect(this, &TimelineModel::addPendingMessageToStore, &events, &EventStore::addPending); connect(this, &TimelineModel::addPendingMessageToStore, &events, &EventStore::addPending);
connect(&events, connect(
&events,
&EventStore::dataChanged, &EventStore::dataChanged,
this, this,
[this](int from, int to) { [this](int from, int to) {
nhlog::ui()->debug("data changed {} to {}", nhlog::ui()->debug(
events.size() - to - 1, "data changed {} to {}", events.size() - to - 1, events.size() - from - 1);
events.size() - from - 1);
emit dataChanged(index(events.size() - to - 1, 0), emit dataChanged(index(events.size() - to - 1, 0),
index(events.size() - from - 1, 0)); index(events.size() - from - 1, 0));
}, },
@ -916,10 +917,20 @@ TimelineModel::sendEncryptedMessage(mtx::events::RoomEvent<T> msg, mtx::events::
OutboundGroupSessionData session_data; OutboundGroupSessionData session_data;
session_data.session_id = session_id; session_data.session_id = session_id;
session_data.session_key = session_key; session_data.session_key = session_key;
session_data.message_index = 0; // TODO Update me session_data.message_index = 0;
cache::saveOutboundMegolmSession( cache::saveOutboundMegolmSession(
room_id, session_data, std::move(outbound_session)); room_id, session_data, std::move(outbound_session));
{
MegolmSessionIndex index;
index.room_id = room_id;
index.session_id = session_id;
index.sender_key = olm::client()->identity_keys().curve25519;
auto megolm_session =
olm::client()->init_inbound_group_session(session_key);
cache::saveInboundMegolmSession(index, std::move(megolm_session));
}
const auto members = cache::roomMembers(room_id); const auto members = cache::roomMembers(room_id);
nhlog::ui()->info("retrieved {} members for {}", members.size(), room_id); nhlog::ui()->info("retrieved {} members for {}", members.size(), room_id);
@ -961,19 +972,23 @@ TimelineModel::sendEncryptedMessage(mtx::events::RoomEvent<T> msg, mtx::events::
return; return;
} }
for (const auto &user : res.device_keys) { mtx::requests::ClaimKeys claim_keys;
// Mapping from a device_id with valid identity keys to the
// Mapping from user id to a device_id with valid identity keys to the
// generated room_key event used for sharing the megolm session. // generated room_key event used for sharing the megolm session.
std::map<std::string, std::string> room_key_msgs; std::map<std::string, std::map<std::string, std::string>> room_key_msgs;
std::map<std::string, DevicePublicKeys> deviceKeys; std::map<std::string, std::map<std::string, DevicePublicKeys>> deviceKeys;
room_key_msgs.clear();
deviceKeys.clear();
for (const auto &user : res.device_keys) {
for (const auto &dev : user.second) { for (const auto &dev : user.second) {
const auto user_id = ::UserId(dev.second.user_id); const auto user_id = ::UserId(dev.second.user_id);
const auto device_id = DeviceId(dev.second.device_id); const auto device_id = DeviceId(dev.second.device_id);
if (user_id.get() ==
http::client()->user_id().to_string() &&
device_id.get() == http::client()->device_id())
continue;
const auto device_keys = dev.second.keys; const auto device_keys = dev.second.keys;
const auto curveKey = "curve25519:" + device_id.get(); const auto curveKey = "curve25519:" + device_id.get();
const auto edKey = "ed25519:" + device_id.get(); const auto edKey = "ed25519:" + device_id.get();
@ -1015,42 +1030,25 @@ TimelineModel::sendEncryptedMessage(mtx::events::RoomEvent<T> msg, mtx::events::
user_id, pks.ed25519, megolm_payload) user_id, pks.ed25519, megolm_payload)
.dump(); .dump();
room_key_msgs.emplace(device_id, room_key); room_key_msgs[user_id].emplace(device_id, room_key);
deviceKeys.emplace(device_id, pks); deviceKeys[user_id].emplace(device_id, pks);
claim_keys.one_time_keys[user.first][device_id] =
mtx::crypto::SIGNED_CURVE25519;
nhlog::net()->info("{}", device_id.get());
nhlog::net()->info(" curve25519 {}", pks.curve25519);
nhlog::net()->info(" ed25519 {}", pks.ed25519);
}
} }
std::vector<std::string> valid_devices; http::client()->claim_keys(claim_keys,
valid_devices.reserve(room_key_msgs.size());
for (auto const &d : room_key_msgs) {
valid_devices.push_back(d.first);
nhlog::net()->info("{}", d.first);
nhlog::net()->info(" curve25519 {}",
deviceKeys.at(d.first).curve25519);
nhlog::net()->info(" ed25519 {}",
deviceKeys.at(d.first).ed25519);
}
nhlog::net()->info(
"sending claim request for user {} with {} devices",
user.first,
valid_devices.size());
http::client()->claim_keys(
user.first,
valid_devices,
std::bind(&TimelineModel::handleClaimedKeys, std::bind(&TimelineModel::handleClaimedKeys,
this, this,
keeper, keeper,
room_key_msgs, room_key_msgs,
deviceKeys, deviceKeys,
user.first,
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2)); std::placeholders::_2));
// TODO: Wait before sending the next batch of requests.
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}); });
// TODO: Let the user know about the errors. // TODO: Let the user know about the errors.
@ -1068,10 +1066,10 @@ TimelineModel::sendEncryptedMessage(mtx::events::RoomEvent<T> msg, mtx::events::
} }
void void
TimelineModel::handleClaimedKeys(std::shared_ptr<StateKeeper> keeper, TimelineModel::handleClaimedKeys(
const std::map<std::string, std::string> &room_keys, std::shared_ptr<StateKeeper> keeper,
const std::map<std::string, DevicePublicKeys> &pks, const std::map<std::string, std::map<std::string, std::string>> &room_keys,
const std::string &user_id, const std::map<std::string, std::map<std::string, DevicePublicKeys>> &pks,
const mtx::responses::ClaimKeys &res, const mtx::responses::ClaimKeys &res,
mtx::http::RequestErr err) mtx::http::RequestErr err)
{ {
@ -1083,65 +1081,53 @@ TimelineModel::handleClaimedKeys(std::shared_ptr<StateKeeper> keeper,
return; return;
} }
nhlog::net()->debug("claimed keys for {}", user_id);
if (res.one_time_keys.size() == 0) {
nhlog::net()->debug("no one-time keys found for user_id: {}", user_id);
return;
}
if (res.one_time_keys.find(user_id) == res.one_time_keys.end()) {
nhlog::net()->debug("no one-time keys found for user_id: {}", user_id);
return;
}
auto retrieved_devices = res.one_time_keys.at(user_id);
// Payload with all the to_device message to be sent. // Payload with all the to_device message to be sent.
json body; nlohmann::json body;
body["messages"][user_id] = json::object();
for (const auto &[user_id, retrieved_devices] : res.one_time_keys) {
nhlog::net()->debug("claimed keys for {}", user_id);
if (retrieved_devices.size() == 0) {
nhlog::net()->debug("no one-time keys found for user_id: {}", user_id);
return;
}
for (const auto &rd : retrieved_devices) { for (const auto &rd : retrieved_devices) {
const auto device_id = rd.first; const auto device_id = rd.first;
nhlog::net()->debug("{} : \n {}", device_id, rd.second.dump(2)); nhlog::net()->debug("{} : \n {}", device_id, rd.second.dump(2));
// TODO: Verify signatures // TODO: Verify signatures
auto otk = rd.second.begin()->at("key"); auto otk = rd.second.begin()->at("key");
if (pks.find(device_id) == pks.end()) { auto id_key = pks.at(user_id).at(device_id).curve25519;
nhlog::net()->critical("couldn't find public key for device: {}",
device_id);
continue;
}
auto id_key = pks.at(device_id).curve25519;
auto s = olm::client()->create_outbound_session(id_key, otk); auto s = olm::client()->create_outbound_session(id_key, otk);
if (room_keys.find(device_id) == room_keys.end()) {
nhlog::net()->critical("couldn't find m.room_key for device: {}",
device_id);
continue;
}
auto device_msg = olm::client()->create_olm_encrypted_content( auto device_msg = olm::client()->create_olm_encrypted_content(
s.get(), room_keys.at(device_id), pks.at(device_id).curve25519); s.get(),
room_keys.at(user_id).at(device_id),
pks.at(user_id).at(device_id).curve25519);
try { try {
cache::saveOlmSession(id_key, std::move(s)); cache::saveOlmSession(id_key, std::move(s));
} catch (const lmdb::error &e) { } catch (const lmdb::error &e) {
nhlog::db()->critical("failed to save outbound olm session: {}", e.what()); nhlog::db()->critical("failed to save outbound olm session: {}",
} catch (const mtx::crypto::olm_exception &e) {
nhlog::crypto()->critical("failed to pickle outbound olm session: {}",
e.what()); e.what());
} catch (const mtx::crypto::olm_exception &e) {
nhlog::crypto()->critical(
"failed to pickle outbound olm session: {}", e.what());
} }
body["messages"][user_id][device_id] = device_msg; body["messages"][user_id][device_id] = device_msg;
} }
nhlog::net()->info("send_to_device: {}", user_id); nhlog::net()->info("send_to_device: {}", user_id);
}
http::client()->send_to_device( http::client()->send_to_device(
"m.room.encrypted", body, [keeper](mtx::http::RequestErr err) { mtx::events::to_string(mtx::events::EventType::RoomEncrypted),
http::client()->generate_txn_id(),
body,
[keeper](mtx::http::RequestErr err) {
if (err) { if (err) {
nhlog::net()->warn("failed to send " nhlog::net()->warn("failed to send "
"send_to_device " "send_to_device "

View file

@ -285,10 +285,10 @@ signals:
private: private:
template<typename T> template<typename T>
void sendEncryptedMessage(mtx::events::RoomEvent<T> msg, mtx::events::EventType eventType); void sendEncryptedMessage(mtx::events::RoomEvent<T> msg, mtx::events::EventType eventType);
void handleClaimedKeys(std::shared_ptr<StateKeeper> keeper, void handleClaimedKeys(
const std::map<std::string, std::string> &room_key, std::shared_ptr<StateKeeper> keeper,
const std::map<std::string, DevicePublicKeys> &pks, const std::map<std::string, std::map<std::string, std::string>> &room_keys,
const std::string &user_id, const std::map<std::string, std::map<std::string, DevicePublicKeys>> &pks,
const mtx::responses::ClaimKeys &res, const mtx::responses::ClaimKeys &res,
mtx::http::RequestErr err); mtx::http::RequestErr err);
void readEvent(const std::string &id); void readEvent(const std::string &id);