Request keys when opening a room for the first time

This commit is contained in:
Nicolas Werner 2021-08-14 02:41:34 +02:00
parent 13633c7644
commit 110fef5c68
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
2 changed files with 28 additions and 11 deletions

View file

@ -3587,24 +3587,33 @@ Cache::roomVerificationStatus(const std::string &room_id)
crypto::Trust trust = crypto::Verified; crypto::Trust trust = crypto::Verified;
try { try {
auto txn = ro_txn(env_); auto txn = lmdb::txn::begin(env_);
auto db = getMembersDb(txn, room_id); auto db = getMembersDb(txn, room_id);
auto keysDb = getUserKeysDb(txn); auto keysDb = getUserKeysDb(txn);
std::vector<std::string> keysToRequest;
std::string_view user_id, unused; std::string_view user_id, unused;
auto cursor = lmdb::cursor::open(txn, db); auto cursor = lmdb::cursor::open(txn, db);
while (cursor.get(user_id, unused, MDB_NEXT)) { while (cursor.get(user_id, unused, MDB_NEXT)) {
auto verif = verificationStatus_(std::string(user_id), txn); auto verif = verificationStatus_(std::string(user_id), txn);
if (verif.unverified_device_count) if (verif.unverified_device_count) {
return crypto::Unverified; trust = crypto::Unverified;
else if (verif.user_verified == crypto::TOFU) if (verif.verified_devices.empty() && verif.no_keys) {
// we probably don't have the keys yet, so query them
keysToRequest.push_back(std::string(user_id));
}
} else if (verif.user_verified == crypto::TOFU && trust == crypto::Verified)
trust = crypto::TOFU; trust = crypto::TOFU;
} }
if (!keysToRequest.empty())
markUserKeysOutOfDate(txn, keysDb, keysToRequest, "");
} catch (std::exception &e) { } catch (std::exception &e) {
nhlog::db()->error( nhlog::db()->error(
"Failed to calculate verification status for {}: {}", room_id, e.what()); "Failed to calculate verification status for {}: {}", room_id, e.what());
return crypto::Unverified; trust = crypto::Unverified;
} }
return trust; return trust;
@ -4000,14 +4009,16 @@ Cache::markUserKeysOutOfDate(lmdb::txn &txn,
nhlog::db()->debug("Marking user keys out of date: {}", user); nhlog::db()->debug("Marking user keys out of date: {}", user);
std::string_view oldKeys; std::string_view oldKeys;
UserKeyCache cacheEntry;
auto res = db.get(txn, user, oldKeys); auto res = db.get(txn, user, oldKeys);
if (res) {
if (!res)
continue;
auto cacheEntry = auto cacheEntry =
json::parse(std::string_view(oldKeys.data(), oldKeys.size())).get<UserKeyCache>(); json::parse(std::string_view(oldKeys.data(), oldKeys.size()))
.get<UserKeyCache>();
}
cacheEntry.last_changed = sync_token; cacheEntry.last_changed = sync_token;
db.put(txn, user, json(cacheEntry).dump()); db.put(txn, user, json(cacheEntry).dump());
query.device_keys[user] = {}; query.device_keys[user] = {};
@ -4233,6 +4244,7 @@ Cache::verificationStatus_(const std::string &user_id, lmdb::txn &txn)
// assume there is at least one unverified device until we have checked we have the device // assume there is at least one unverified device until we have checked we have the device
// list for that user. // list for that user.
status.unverified_device_count = 1; status.unverified_device_count = 1;
status.no_keys = true;
if (auto verifCache = verificationCache(user_id, txn)) { if (auto verifCache = verificationCache(user_id, txn)) {
status.verified_devices = verifCache->device_verified; status.verified_devices = verifCache->device_verified;
@ -4284,6 +4296,9 @@ Cache::verificationStatus_(const std::string &user_id, lmdb::txn &txn)
// key and their master key // key and their master key
auto ourKeys = userKeys_(local_user, txn); auto ourKeys = userKeys_(local_user, txn);
auto theirKeys = userKeys_(user_id, txn); auto theirKeys = userKeys_(user_id, txn);
if (theirKeys)
status.no_keys = false;
if (!ourKeys || !theirKeys) { if (!ourKeys || !theirKeys) {
verification_storage.status[user_id] = status; verification_storage.status[user_id] = status;
return status; return status;

View file

@ -117,6 +117,8 @@ struct VerificationStatus
std::map<std::string, crypto::Trust> verified_device_keys; std::map<std::string, crypto::Trust> verified_device_keys;
//! Count of unverified devices //! Count of unverified devices
int unverified_device_count = 0; int unverified_device_count = 0;
// if the keys are not in cache
bool no_keys = false;
}; };
//! In memory cache of verification status //! In memory cache of verification status