LCOV - code coverage report
Current view: top level - src/jamidht - accountarchive.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 10 10 100.0 %
Date: 2024-11-13 09:04:27 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 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             : #pragma once
      18             : 
      19             : #include "jami_contact.h"
      20             : #include "jamidht/jamiaccount.h"
      21             : #include "fileutils.h"
      22             : 
      23             : #include <opendht/crypto.h>
      24             : #include <memory>
      25             : #include <vector>
      26             : #include <map>
      27             : #include <string>
      28             : 
      29             : namespace jami {
      30             : 
      31             : /**
      32             :  * Crypto material contained in the archive,
      33             :  * not persisted in the account configuration
      34             :  */
      35             : struct AccountArchive
      36             : {
      37             :     /** Account main private key and certificate chain */
      38             :     dht::crypto::Identity id;
      39             : 
      40             :     /** Generated CA key (for self-signed certificates) */
      41             :     std::shared_ptr<dht::crypto::PrivateKey> ca_key;
      42             : 
      43             :     /** Revoked devices */
      44             :     std::shared_ptr<dht::crypto::RevocationList> revoked;
      45             : 
      46             :     /** Ethereum private key */
      47             :     std::vector<uint8_t> eth_key;
      48             : 
      49             :     /** Contacts */
      50             :     std::map<dht::InfoHash, Contact> contacts;
      51             : 
      52             :     // Conversations
      53             :     std::map<std::string, ConvInfo> conversations;
      54             :     std::map<std::string, ConversationRequest> conversationsRequests;
      55             : 
      56             :     /** Account configuration */
      57             :     std::map<std::string, std::string> config;
      58             : 
      59             :     /** Salt for the archive encryption password.  */
      60             :     std::vector<uint8_t> password_salt;
      61             : 
      62         786 :     AccountArchive() = default;
      63           1 :     AccountArchive(const std::vector<uint8_t>& data, const std::vector<uint8_t>& password_salt = {}) { deserialize(data, password_salt); }
      64         120 :     AccountArchive(const std::filesystem::path& path, std::string_view scheme = {}, const std::string& pwd = {}) { load(path, scheme, pwd); }
      65             : 
      66             :     /** Serialize structured archive data to memory. */
      67             :     std::string serialize() const;
      68             : 
      69             :     /** Deserialize archive from memory. */
      70             :     void deserialize(const std::vector<uint8_t>& data, const std::vector<uint8_t>& salt);
      71             : 
      72             :     /** Load archive from file, optionally encrypted with provided password. */
      73          96 :     void load(const std::filesystem::path& path, std::string_view scheme, const std::string& pwd) {
      74          96 :         auto data = fileutils::readArchive(path, scheme, pwd);
      75          94 :         deserialize(data.data, data.salt);
      76          94 :     }
      77             : 
      78             :     /** Save archive to file, optionally encrypted with provided password. */
      79         827 :     void save(const std::filesystem::path& path, std::string_view scheme, const std::string& password) const {
      80         827 :         fileutils::writeArchive(serialize(), path, scheme, password, password_salt);
      81         827 :     }
      82             : };
      83             : 
      84             : } // namespace jami

Generated by: LCOV version 1.14