LCOV - code coverage report
Current view: top level - foo/src/jamidht - accountarchive.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 74 82 90.2 %
Date: 2025-12-18 10:07:43 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2025 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  This program is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "accountarchive.h"
      19             : #include "account_const.h"
      20             : #include "configkeys.h"
      21             : #include "base64.h"
      22             : #include "logger.h"
      23             : 
      24             : #include <json_utils.h>
      25             : 
      26             : namespace jami {
      27             : 
      28             : void
      29         102 : AccountArchive::deserialize(std::string_view dat, const std::vector<uint8_t>& salt)
      30             : {
      31         408 :     JAMI_DEBUG("Loading account archive ({:d} bytes)", dat.size());
      32             : 
      33         102 :     password_salt = salt;
      34             : 
      35             :     // Decode string
      36         102 :     auto* char_data = dat.data(); // reinterpret_cast<const char*>(&dat[0]);
      37         102 :     std::string err;
      38         102 :     Json::Value value;
      39         102 :     Json::CharReaderBuilder rbuilder;
      40         102 :     Json::CharReaderBuilder::strictMode(&rbuilder.settings_);
      41         102 :     auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
      42         102 :     if (!reader->parse(char_data, char_data + dat.size(), &value, &err)) {
      43           1 :         JAMI_ERR() << "Archive JSON parsing error: " << err;
      44           1 :         throw std::runtime_error("failed to parse JSON");
      45             :     }
      46             : 
      47             :     // Import content
      48             :     try {
      49        2633 :         for (Json::ValueIterator itr = value.begin(); itr != value.end(); itr++) {
      50             :             try {
      51        2532 :                 const auto key = itr.key().asString();
      52        2532 :                 if (key.empty())
      53           0 :                     continue;
      54        2532 :                 if (key.compare(libjami::Account::ConfProperties::TLS::CA_LIST_FILE) == 0) {
      55        2532 :                 } else if (key.compare(libjami::Account::ConfProperties::TLS::PRIVATE_KEY_FILE) == 0) {
      56        2491 :                 } else if (key.compare(libjami::Account::ConfProperties::TLS::CERTIFICATE_FILE) == 0) {
      57        2450 :                 } else if (key.compare(libjami::Account::ConfProperties::DHT_PROXY_LIST_URL) == 0) {
      58        2450 :                 } else if (key.compare(libjami::Account::ConfProperties::AUTOANSWER) == 0) {
      59        2450 :                 } else if (key.compare(libjami::Account::ConfProperties::PROXY_ENABLED) == 0) {
      60        2450 :                 } else if (key.compare(libjami::Account::ConfProperties::PROXY_SERVER) == 0) {
      61        2450 :                 } else if (key.compare(libjami::Account::ConfProperties::PROXY_PUSH_TOKEN) == 0) {
      62        2450 :                 } else if (key.compare(Conf::RING_CA_KEY) == 0) {
      63         101 :                     ca_key = std::make_shared<dht::crypto::PrivateKey>(base64::decode(itr->asString()));
      64        2349 :                 } else if (key.compare(Conf::RING_ACCOUNT_KEY) == 0) {
      65         101 :                     id.first = std::make_shared<dht::crypto::PrivateKey>(base64::decode(itr->asString()));
      66        2248 :                 } else if (key.compare(Conf::RING_ACCOUNT_CERT) == 0) {
      67         101 :                     id.second = std::make_shared<dht::crypto::Certificate>(base64::decode(itr->asString()));
      68        2147 :                 } else if (key.compare(Conf::RING_ACCOUNT_CONTACTS) == 0) {
      69           6 :                     for (Json::ValueIterator citr = itr->begin(); citr != itr->end(); citr++) {
      70           3 :                         dht::InfoHash h {citr.key().asString()};
      71           3 :                         if (h != dht::InfoHash {})
      72           3 :                             contacts.emplace(h, Contact {*citr});
      73             :                     }
      74        2144 :                 } else if (key.compare(Conf::CONVERSATIONS_KEY) == 0) {
      75          23 :                     for (Json::ValueIterator citr = itr->begin(); citr != itr->end(); citr++) {
      76          13 :                         auto ci = ConvInfo(*citr);
      77          13 :                         conversations[ci.id] = std::move(ci);
      78          13 :                     }
      79        2134 :                 } else if (key.compare(Conf::CONVERSATIONS_REQUESTS_KEY) == 0) {
      80           2 :                     for (Json::ValueIterator citr = itr->begin(); citr != itr->end(); citr++) {
      81           1 :                         conversationsRequests.emplace(citr.key().asString(), ConversationRequest(*citr));
      82             :                     }
      83        2133 :                 } else if (key.compare(Conf::ETH_KEY) == 0) {
      84         101 :                     eth_key = base64::decode(itr->asString());
      85        2032 :                 } else if (key.compare(Conf::RING_ACCOUNT_CRL) == 0) {
      86           0 :                     revoked = std::make_shared<dht::crypto::RevocationList>(base64::decode(itr->asString()));
      87             :                 } else {
      88        2032 :                     config[key] = itr->asString();
      89             :                 }
      90        2532 :             } catch (const std::exception& ex) {
      91           0 :                 JAMI_ERR("Unable to parse JSON entry with value of type %d: %s", (unsigned) itr->type(), ex.what());
      92           0 :             }
      93             :         }
      94           0 :     } catch (const std::exception& ex) {
      95           0 :         JAMI_ERR("Unable to parse JSON: %s", ex.what());
      96           0 :     }
      97             : 
      98         101 :     if (not id.first) {
      99           0 :         throw std::runtime_error("Archive doesn't include account private key");
     100             :     }
     101         105 : }
     102             : 
     103             : std::string
     104         821 : AccountArchive::serialize() const
     105             : {
     106         821 :     Json::Value root;
     107             : 
     108        5452 :     for (const auto& it : config)
     109        4631 :         root[it.first] = it.second;
     110             : 
     111         821 :     if (ca_key and *ca_key)
     112         821 :         root[Conf::RING_CA_KEY] = base64::encode(ca_key->serialize());
     113             : 
     114         821 :     root[Conf::RING_ACCOUNT_KEY] = base64::encode(id.first->serialize());
     115         821 :     root[Conf::RING_ACCOUNT_CERT] = base64::encode(id.second->getPacked());
     116         821 :     root[Conf::ETH_KEY] = base64::encode(eth_key);
     117             : 
     118         821 :     if (revoked)
     119           2 :         root[Conf::RING_ACCOUNT_CRL] = base64::encode(revoked->getPacked());
     120             : 
     121         821 :     if (not contacts.empty()) {
     122           5 :         Json::Value& jsonContacts = root[Conf::RING_ACCOUNT_CONTACTS];
     123          10 :         for (const auto& c : contacts)
     124           5 :             jsonContacts[c.first.toString()] = c.second.toJson();
     125             :     }
     126             : 
     127         821 :     if (not conversations.empty()) {
     128          19 :         Json::Value& jsonConversations = root[Conf::CONVERSATIONS_KEY];
     129          44 :         for (const auto& [key, c] : conversations) {
     130          25 :             jsonConversations[key] = c.toJson();
     131             :         }
     132             :     }
     133             : 
     134         821 :     if (not conversationsRequests.empty()) {
     135           2 :         Json::Value& jsonConversationsReqs = root[Conf::CONVERSATIONS_REQUESTS_KEY];
     136           4 :         for (const auto& [key, value] : conversationsRequests) {
     137           2 :             jsonConversationsReqs[key] = value.toJson();
     138             :         }
     139             :     }
     140             : 
     141        1642 :     return json::toString(root);
     142         821 : }
     143             : 
     144             : } // namespace jami

Generated by: LCOV version 1.14