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 : #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 631 : AccountArchive() = default; 63 : AccountArchive(const std::vector<uint8_t>& data, const std::vector<uint8_t>& password_salt = {}) 64 : { 65 : deserialize(std::string_view((const char*) data.data(), data.size()), password_salt); 66 : } 67 3 : AccountArchive(std::string_view data, const std::vector<uint8_t>& password_salt = {}) 68 3 : { 69 3 : deserialize(data, password_salt); 70 3 : } 71 88 : AccountArchive(const std::filesystem::path& path, 72 : std::string_view scheme = {}, 73 : const std::string& pwd = {}) 74 88 : { 75 88 : load(path, scheme, pwd); 76 128 : } 77 : 78 : /** Serialize structured archive data to memory. */ 79 : std::string serialize() const; 80 : 81 : /** Deserialize archive from memory. */ 82 : void deserialize(std::string_view data, const std::vector<uint8_t>& salt); 83 : 84 : /** Load archive from file, optionally encrypted with provided password. */ 85 88 : void load(const std::filesystem::path& path, std::string_view scheme, const std::string& pwd) 86 : { 87 88 : auto data = fileutils::readArchive(path, scheme, pwd); 88 84 : deserialize(data.data, data.salt); 89 84 : } 90 : 91 : /** Save archive to file, optionally encrypted with provided password. */ 92 669 : bool save(const std::filesystem::path& path, 93 : std::string_view scheme, 94 : const std::string& password) const 95 : { 96 669 : return fileutils::writeArchive(serialize(), path, scheme, password, password_salt); 97 : } 98 : }; 99 : 100 : } // namespace jami