LCOV - code coverage report
Current view: top level - foo/src/jamidht - accountarchive.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 80 89 89.9 %
Date: 2025-10-16 08:11: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         100 : AccountArchive::deserialize(std::string_view dat, const std::vector<uint8_t>& salt)
      30             : {
      31         300 :     JAMI_DEBUG("Loading account archive ({:d} bytes)", dat.size());
      32             : 
      33         100 :     password_salt = salt;
      34             : 
      35             :     // Decode string
      36         100 :     auto* char_data = dat.data(); // reinterpret_cast<const char*>(&dat[0]);
      37         100 :     std::string err;
      38         100 :     Json::Value value;
      39         100 :     Json::CharReaderBuilder rbuilder;
      40         100 :     Json::CharReaderBuilder::strictMode(&rbuilder.settings_);
      41         100 :     auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
      42         100 :     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        2621 :         for (Json::ValueIterator itr = value.begin(); itr != value.end(); itr++) {
      50             :             try {
      51        2522 :                 const auto key = itr.key().asString();
      52        2522 :                 if (key.empty())
      53           0 :                     continue;
      54        2522 :                 if (key.compare(libjami::Account::ConfProperties::TLS::CA_LIST_FILE) == 0) {
      55        2522 :                 } else if (key.compare(libjami::Account::ConfProperties::TLS::PRIVATE_KEY_FILE)
      56        2522 :                            == 0) {
      57        2481 :                 } else if (key.compare(libjami::Account::ConfProperties::TLS::CERTIFICATE_FILE)
      58        2481 :                            == 0) {
      59        2440 :                 } else if (key.compare(libjami::Account::ConfProperties::DHT_PROXY_LIST_URL) == 0) {
      60        2440 :                 } else if (key.compare(libjami::Account::ConfProperties::AUTOANSWER) == 0) {
      61        2440 :                 } else if (key.compare(libjami::Account::ConfProperties::PROXY_ENABLED) == 0) {
      62        2440 :                 } else if (key.compare(libjami::Account::ConfProperties::PROXY_SERVER) == 0) {
      63        2440 :                 } else if (key.compare(libjami::Account::ConfProperties::PROXY_PUSH_TOKEN) == 0) {
      64        2440 :                 } else if (key.compare(Conf::RING_CA_KEY) == 0) {
      65         198 :                     ca_key = std::make_shared<dht::crypto::PrivateKey>(
      66         297 :                         base64::decode(itr->asString()));
      67        2341 :                 } else if (key.compare(Conf::RING_ACCOUNT_KEY) == 0) {
      68         198 :                     id.first = std::make_shared<dht::crypto::PrivateKey>(
      69         297 :                         base64::decode(itr->asString()));
      70        2242 :                 } else if (key.compare(Conf::RING_ACCOUNT_CERT) == 0) {
      71         198 :                     id.second = std::make_shared<dht::crypto::Certificate>(
      72         297 :                         base64::decode(itr->asString()));
      73        2143 :                 } else if (key.compare(Conf::RING_ACCOUNT_CONTACTS) == 0) {
      74           6 :                     for (Json::ValueIterator citr = itr->begin(); citr != itr->end(); citr++) {
      75           3 :                         dht::InfoHash h {citr.key().asString()};
      76           3 :                         if (h != dht::InfoHash {})
      77           3 :                             contacts.emplace(h, Contact {*citr});
      78             :                     }
      79        2140 :                 } else if (key.compare(Conf::CONVERSATIONS_KEY) == 0) {
      80          23 :                     for (Json::ValueIterator citr = itr->begin(); citr != itr->end(); citr++) {
      81          13 :                         auto ci = ConvInfo(*citr);
      82          13 :                         conversations[ci.id] = std::move(ci);
      83          13 :                     }
      84        2130 :                 } else if (key.compare(Conf::CONVERSATIONS_REQUESTS_KEY) == 0) {
      85           2 :                     for (Json::ValueIterator citr = itr->begin(); citr != itr->end(); citr++) {
      86           1 :                         conversationsRequests.emplace(citr.key().asString(),
      87           2 :                                                       ConversationRequest(*citr));
      88             :                     }
      89        2129 :                 } else if (key.compare(Conf::ETH_KEY) == 0) {
      90          99 :                     eth_key = base64::decode(itr->asString());
      91        2030 :                 } else if (key.compare(Conf::RING_ACCOUNT_CRL) == 0) {
      92           0 :                     revoked = std::make_shared<dht::crypto::RevocationList>(
      93           0 :                         base64::decode(itr->asString()));
      94             :                 } else {
      95        2030 :                     config[key] = itr->asString();
      96             :                 }
      97        2522 :             } catch (const std::exception& ex) {
      98           0 :                 JAMI_ERR("Unable to parse JSON entry with value of type %d: %s",
      99             :                          (unsigned) itr->type(),
     100             :                          ex.what());
     101           0 :             }
     102             :         }
     103           0 :     } catch (const std::exception& ex) {
     104           0 :         JAMI_ERR("Unable to parse JSON: %s", ex.what());
     105           0 :     }
     106             : 
     107          99 :     if (not id.first) {
     108           0 :         throw std::runtime_error("Archive doesn't include account private key");
     109             :     }
     110         103 : }
     111             : 
     112             : std::string
     113         849 : AccountArchive::serialize() const
     114             : {
     115         849 :     Json::Value root;
     116             : 
     117        5508 :     for (const auto& it : config)
     118        4659 :         root[it.first] = it.second;
     119             : 
     120         849 :     if (ca_key and *ca_key)
     121         849 :         root[Conf::RING_CA_KEY] = base64::encode(ca_key->serialize());
     122             : 
     123         849 :     root[Conf::RING_ACCOUNT_KEY] = base64::encode(id.first->serialize());
     124         849 :     root[Conf::RING_ACCOUNT_CERT] = base64::encode(id.second->getPacked());
     125         849 :     root[Conf::ETH_KEY] = base64::encode(eth_key);
     126             : 
     127         849 :     if (revoked)
     128           2 :         root[Conf::RING_ACCOUNT_CRL] = base64::encode(revoked->getPacked());
     129             : 
     130         849 :     if (not contacts.empty()) {
     131           5 :         Json::Value& jsonContacts = root[Conf::RING_ACCOUNT_CONTACTS];
     132          10 :         for (const auto& c : contacts)
     133           5 :             jsonContacts[c.first.toString()] = c.second.toJson();
     134             :     }
     135             : 
     136         849 :     if (not conversations.empty()) {
     137          19 :         Json::Value& jsonConversations = root[Conf::CONVERSATIONS_KEY];
     138          44 :         for (const auto& [key, c] : conversations) {
     139          25 :             jsonConversations[key] = c.toJson();
     140             :         }
     141             :     }
     142             : 
     143         849 :     if (not conversationsRequests.empty()) {
     144           2 :         Json::Value& jsonConversationsReqs = root[Conf::CONVERSATIONS_REQUESTS_KEY];
     145           4 :         for (const auto& [key, value] : conversationsRequests) {
     146           2 :             jsonConversationsReqs[key] = value.toJson();
     147             :         }
     148             :     }
     149             : 
     150        1698 :     return json::toString(root);
     151         849 : }
     152             : 
     153             : } // namespace jami

Generated by: LCOV version 1.14