From 56db0dbc7d0878ce1621a4dc998024d470d200a5 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 17 Aug 2021 03:23:51 +0200 Subject: Allow downloading keys from key backup --- src/Cache.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src/Cache.cpp') diff --git a/src/Cache.cpp b/src/Cache.cpp index 8b8b2985..6b28ca91 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -42,6 +42,7 @@ static const std::string SECRET("secret"); static const std::string_view NEXT_BATCH_KEY("next_batch"); static const std::string_view OLM_ACCOUNT_KEY("olm_account"); static const std::string_view CACHE_FORMAT_VERSION_KEY("cache_format_version"); +static const std::string_view CURRENT_ONLINE_BACKUP_VERSION("current_online_backup_version"); constexpr size_t MAX_RESTORED_MESSAGES = 30'000; @@ -723,6 +724,36 @@ Cache::restoreOlmAccount() return std::string(pickled.data(), pickled.size()); } +void +Cache::saveBackupVersion(const OnlineBackupVersion &data) +{ + auto txn = lmdb::txn::begin(env_); + syncStateDb_.put(txn, CURRENT_ONLINE_BACKUP_VERSION, nlohmann::json(data).dump()); + txn.commit(); +} + +void +Cache::deleteBackupVersion() +{ + auto txn = lmdb::txn::begin(env_); + syncStateDb_.del(txn, CURRENT_ONLINE_BACKUP_VERSION); + txn.commit(); +} + +std::optional +Cache::backupVersion() +{ + try { + auto txn = ro_txn(env_); + std::string_view v; + syncStateDb_.get(txn, CURRENT_ONLINE_BACKUP_VERSION, v); + + return nlohmann::json::parse(v).get(); + } catch (...) { + return std::nullopt; + } +} + void Cache::storeSecret(const std::string name, const std::string secret) { @@ -4114,6 +4145,20 @@ from_json(const json &j, VerificationCache &info) info.device_blocked = j.at("device_blocked").get>(); } +void +to_json(json &j, const OnlineBackupVersion &info) +{ + j["v"] = info.version; + j["a"] = info.algorithm; +} + +void +from_json(const json &j, OnlineBackupVersion &info) +{ + info.version = j.at("v").get(); + info.algorithm = j.at("a").get(); +} + std::optional Cache::verificationCache(const std::string &user_id, lmdb::txn &txn) { @@ -4461,6 +4506,7 @@ to_json(nlohmann::json &obj, const GroupSessionData &msg) { obj["message_index"] = msg.message_index; obj["ts"] = msg.timestamp; + obj["trust"] = msg.trusted; obj["sender_claimed_ed25519_key"] = msg.sender_claimed_ed25519_key; obj["forwarding_curve25519_key_chain"] = msg.forwarding_curve25519_key_chain; @@ -4475,6 +4521,7 @@ from_json(const nlohmann::json &obj, GroupSessionData &msg) { msg.message_index = obj.at("message_index"); msg.timestamp = obj.value("ts", 0ULL); + msg.trusted = obj.value("trust", true); msg.sender_claimed_ed25519_key = obj.value("sender_claimed_ed25519_key", ""); msg.forwarding_curve25519_key_chain = -- cgit 1.5.1