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: 2026-02-28 10:41:24 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2026 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          96 : AccountArchive::deserialize(std::string_view dat, const std::vector<uint8_t>& salt)
      30             : {
      31         384 :     JAMI_DEBUG("Loading account archive ({:d} bytes)", dat.size());
      32             : 
      33          96 :     password_salt = salt;
      34             : 
      35             :     // Decode string
      36          96 :     auto* char_data = dat.data(); // reinterpret_cast<const char*>(&dat[0]);
      37          96 :     std::string err;
      38          96 :     Json::Value value;
      39          96 :     Json::CharReaderBuilder rbuilder;
      40          96 :     Json::CharReaderBuilder::strictMode(&rbuilder.settings_);
      41          96 :     auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
      42          96 :     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        2597 :         for (Json::ValueIterator itr = value.begin(); itr != value.end(); itr++) {
      50             :             try {
      51        2502 :                 const auto key = itr.key().asString();
      52        2502 :                 if (key.empty())
      53           0 :                     continue;
      54        2502 :                 if (key.compare(libjami::Account::ConfProperties::TLS::CA_LIST_FILE) == 0) {
      55        2502 :                 } else if (key.compare(libjami::Account::ConfProperties::TLS::PRIVATE_KEY_FILE) == 0) {
      56        2461 :                 } else if (key.compare(libjami::Account::ConfProperties::TLS::CERTIFICATE_FILE) == 0) {
      57        2420 :                 } else if (key.compare(libjami::Account::ConfProperties::DHT_PROXY_LIST_URL) == 0) {
      58        2420 :                 } else if (key.compare(libjami::Account::ConfProperties::AUTOANSWER) == 0) {
      59        2420 :                 } else if (key.compare(libjami::Account::ConfProperties::PROXY_ENABLED) == 0) {
      60        2420 :                 } else if (key.compare(libjami::Account::ConfProperties::PROXY_SERVER) == 0) {
      61        2420 :                 } else if (key.compare(libjami::Account::ConfProperties::PROXY_PUSH_TOKEN) == 0) {
      62        2420 :                 } else if (key.compare(Conf::RING_CA_KEY) == 0) {
      63          95 :                     ca_key = std::make_shared<dht::crypto::PrivateKey>(base64::decode(itr->asString()));
      64        2325 :                 } else if (key.compare(Conf::RING_ACCOUNT_KEY) == 0) {
      65          95 :                     id.first = std::make_shared<dht::crypto::PrivateKey>(base64::decode(itr->asString()));
      66        2230 :                 } else if (key.compare(Conf::RING_ACCOUNT_CERT) == 0) {
      67          95 :                     id.second = std::make_shared<dht::crypto::Certificate>(base64::decode(itr->asString()));
      68        2135 :                 } 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        2132 :                 } 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        2122 :                 } 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        2121 :                 } else if (key.compare(Conf::ETH_KEY) == 0) {
      84          95 :                     eth_key = base64::decode(itr->asString());
      85        2026 :                 } 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        2026 :                     config[key] = itr->asString();
      89             :                 }
      90        2502 :             } 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          95 :     if (not id.first) {
      99           0 :         throw std::runtime_error("Archive doesn't include account private key");
     100             :     }
     101          99 : }
     102             : 
     103             : std::string
     104         815 : AccountArchive::serialize() const
     105             : {
     106         815 :     Json::Value root;
     107             : 
     108        5440 :     for (const auto& it : config)
     109        4625 :         root[it.first] = it.second;
     110             : 
     111         815 :     if (ca_key and *ca_key)
     112         815 :         root[Conf::RING_CA_KEY] = base64::encode(ca_key->serialize());
     113             : 
     114         815 :     root[Conf::RING_ACCOUNT_KEY] = base64::encode(id.first->serialize());
     115         815 :     root[Conf::RING_ACCOUNT_CERT] = base64::encode(id.second->getPacked());
     116         815 :     root[Conf::ETH_KEY] = base64::encode(eth_key);
     117             : 
     118         815 :     if (revoked)
     119           2 :         root[Conf::RING_ACCOUNT_CRL] = base64::encode(revoked->getPacked());
     120             : 
     121         815 :     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         815 :     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         815 :     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        1630 :     return json::toString(root);
     142         815 : }
     143             : 
     144             : } // namespace jami

Generated by: LCOV version 1.14