|
|
@ -431,12 +431,20 @@ encrypt_group_message(const std::string &room_id, const std::string &device_id, |
|
|
|
|
|
|
|
|
|
|
|
if (cache::outboundMegolmSessionExists(room_id)) { |
|
|
|
if (cache::outboundMegolmSessionExists(room_id)) { |
|
|
|
auto res = cache::getOutboundMegolmSession(room_id); |
|
|
|
auto res = cache::getOutboundMegolmSession(room_id); |
|
|
|
|
|
|
|
auto encryptionSettings = cache::client()->roomEncryptionSettings(room_id); |
|
|
|
|
|
|
|
mtx::events::state::Encryption defaultSettings; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// rotate if we crossed the limits for this key
|
|
|
|
|
|
|
|
if (res.data.message_index < |
|
|
|
|
|
|
|
encryptionSettings.value_or(defaultSettings).rotation_period_msgs && |
|
|
|
|
|
|
|
(QDateTime::currentMSecsSinceEpoch() - res.data.timestamp) < |
|
|
|
|
|
|
|
encryptionSettings.value_or(defaultSettings).rotation_period_ms) { |
|
|
|
auto member_it = members.begin(); |
|
|
|
auto member_it = members.begin(); |
|
|
|
auto session_member_it = res.data.currently.keys.begin(); |
|
|
|
auto session_member_it = res.data.currently.keys.begin(); |
|
|
|
auto session_member_it_end = res.data.currently.keys.end(); |
|
|
|
auto session_member_it_end = res.data.currently.keys.end(); |
|
|
|
|
|
|
|
|
|
|
|
while (member_it != members.end() || session_member_it != session_member_it_end) { |
|
|
|
while (member_it != members.end() || |
|
|
|
|
|
|
|
session_member_it != session_member_it_end) { |
|
|
|
if (member_it == members.end()) { |
|
|
|
if (member_it == members.end()) { |
|
|
|
// a member left, purge session!
|
|
|
|
// a member left, purge session!
|
|
|
|
nhlog::crypto()->debug( |
|
|
|
nhlog::crypto()->debug( |
|
|
@ -452,9 +460,11 @@ encrypt_group_message(const std::string &room_id, const std::string &device_id, |
|
|
|
if (member_it->second) |
|
|
|
if (member_it->second) |
|
|
|
for (const auto &dev : |
|
|
|
for (const auto &dev : |
|
|
|
member_it->second->device_keys) |
|
|
|
member_it->second->device_keys) |
|
|
|
if (member_it->first != own_user_id || |
|
|
|
if (member_it->first != |
|
|
|
|
|
|
|
own_user_id || |
|
|
|
dev.first != device_id) |
|
|
|
dev.first != device_id) |
|
|
|
sendSessionTo[member_it->first] |
|
|
|
sendSessionTo[member_it |
|
|
|
|
|
|
|
->first] |
|
|
|
.push_back(dev.first); |
|
|
|
.push_back(dev.first); |
|
|
|
|
|
|
|
|
|
|
|
++member_it; |
|
|
|
++member_it; |
|
|
@ -474,11 +484,12 @@ encrypt_group_message(const std::string &room_id, const std::string &device_id, |
|
|
|
sendSessionTo[member_it->first] = {}; |
|
|
|
sendSessionTo[member_it->first] = {}; |
|
|
|
|
|
|
|
|
|
|
|
if (member_it->second) { |
|
|
|
if (member_it->second) { |
|
|
|
for (const auto &dev : member_it->second->device_keys) |
|
|
|
for (const auto &dev : |
|
|
|
|
|
|
|
member_it->second->device_keys) |
|
|
|
if (member_it->first != own_user_id || |
|
|
|
if (member_it->first != own_user_id || |
|
|
|
dev.first != device_id) |
|
|
|
dev.first != device_id) |
|
|
|
sendSessionTo[member_it->first].push_back( |
|
|
|
sendSessionTo[member_it->first] |
|
|
|
dev.first); |
|
|
|
.push_back(dev.first); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
++member_it; |
|
|
|
++member_it; |
|
|
@ -487,7 +498,8 @@ encrypt_group_message(const std::string &room_id, const std::string &device_id, |
|
|
|
bool device_removed = false; |
|
|
|
bool device_removed = false; |
|
|
|
for (const auto &dev : session_member_it->second.devices) { |
|
|
|
for (const auto &dev : session_member_it->second.devices) { |
|
|
|
if (!member_it->second || |
|
|
|
if (!member_it->second || |
|
|
|
!member_it->second->device_keys.count(dev.first)) { |
|
|
|
!member_it->second->device_keys.count( |
|
|
|
|
|
|
|
dev.first)) { |
|
|
|
device_removed = true; |
|
|
|
device_removed = true; |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
@ -496,20 +508,22 @@ encrypt_group_message(const std::string &room_id, const std::string &device_id, |
|
|
|
if (device_removed) { |
|
|
|
if (device_removed) { |
|
|
|
// device removed, rotate session!
|
|
|
|
// device removed, rotate session!
|
|
|
|
nhlog::crypto()->debug( |
|
|
|
nhlog::crypto()->debug( |
|
|
|
"Rotating megolm session because of removed device of {}", |
|
|
|
"Rotating megolm session because of removed " |
|
|
|
|
|
|
|
"device of {}", |
|
|
|
member_it->first); |
|
|
|
member_it->first); |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// check for new devices to share with
|
|
|
|
// check for new devices to share with
|
|
|
|
if (member_it->second) |
|
|
|
if (member_it->second) |
|
|
|
for (const auto &dev : member_it->second->device_keys) |
|
|
|
for (const auto &dev : |
|
|
|
if (!session_member_it->second.devices.count( |
|
|
|
member_it->second->device_keys) |
|
|
|
dev.first) && |
|
|
|
if (!session_member_it->second.devices |
|
|
|
|
|
|
|
.count(dev.first) && |
|
|
|
(member_it->first != own_user_id || |
|
|
|
(member_it->first != own_user_id || |
|
|
|
dev.first != device_id)) |
|
|
|
dev.first != device_id)) |
|
|
|
sendSessionTo[member_it->first].push_back( |
|
|
|
sendSessionTo[member_it->first] |
|
|
|
dev.first); |
|
|
|
.push_back(dev.first); |
|
|
|
|
|
|
|
|
|
|
|
++member_it; |
|
|
|
++member_it; |
|
|
|
++session_member_it; |
|
|
|
++session_member_it; |
|
|
@ -520,6 +534,7 @@ encrypt_group_message(const std::string &room_id, const std::string &device_id, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
group_session_data = std::move(res.data); |
|
|
|
group_session_data = std::move(res.data); |
|
|
|
} |
|
|
|
} |
|
|
@ -537,6 +552,7 @@ encrypt_group_message(const std::string &room_id, const std::string &device_id, |
|
|
|
session_data.session_id = mtx::crypto::session_id(session.get()); |
|
|
|
session_data.session_id = mtx::crypto::session_id(session.get()); |
|
|
|
session_data.session_key = mtx::crypto::session_key(session.get()); |
|
|
|
session_data.session_key = mtx::crypto::session_key(session.get()); |
|
|
|
session_data.message_index = 0; |
|
|
|
session_data.message_index = 0; |
|
|
|
|
|
|
|
session_data.timestamp = QDateTime::currentMSecsSinceEpoch(); |
|
|
|
|
|
|
|
|
|
|
|
sendSessionTo.clear(); |
|
|
|
sendSessionTo.clear(); |
|
|
|
|
|
|
|
|
|
|
|