Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * Author : Adrien BĂ©raud <adrien.beraud@savoirfairelinux.com> 4 : * 5 : * This program is free software; you can redistribute it and/or modify 6 : * it under the terms of the GNU General Public License as published by 7 : * the Free Software Foundation; either version 3 of the License, or 8 : * (at your option) any later version. 9 : * 10 : * This program is distributed in the hope that it will be useful, 11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 : * GNU General Public License for more details. 14 : * 15 : * You should have received a copy of the GNU General Public License 16 : * along with this program. If not, see <https://www.gnu.org/licenses/>. 17 : */ 18 : #pragma once 19 : 20 : #include "jami_contact.h" 21 : #include "jamidht/jamiaccount.h" 22 : #include "fileutils.h" 23 : 24 : #include <opendht/crypto.h> 25 : #include <memory> 26 : #include <vector> 27 : #include <map> 28 : #include <string> 29 : 30 : namespace jami { 31 : 32 : /** 33 : * Crypto material contained in the archive, 34 : * not persisted in the account configuration 35 : */ 36 : struct AccountArchive 37 : { 38 : /** Account main private key and certificate chain */ 39 : dht::crypto::Identity id; 40 : 41 : /** Generated CA key (for self-signed certificates) */ 42 : std::shared_ptr<dht::crypto::PrivateKey> ca_key; 43 : 44 : /** Revoked devices */ 45 : std::shared_ptr<dht::crypto::RevocationList> revoked; 46 : 47 : /** Ethereum private key */ 48 : std::vector<uint8_t> eth_key; 49 : 50 : /** Contacts */ 51 : std::map<dht::InfoHash, Contact> contacts; 52 : 53 : // Conversations 54 : std::map<std::string, ConvInfo> conversations; 55 : std::map<std::string, ConversationRequest> conversationsRequests; 56 : 57 : /** Account configuration */ 58 : std::map<std::string, std::string> config; 59 : 60 : /** Salt for the archive encryption password. */ 61 : std::vector<uint8_t> password_salt; 62 : 63 777 : AccountArchive() = default; 64 1 : AccountArchive(const std::vector<uint8_t>& data, const std::vector<uint8_t>& password_salt = {}) { deserialize(data, password_salt); } 65 120 : AccountArchive(const std::filesystem::path& path, std::string_view scheme = {}, const std::string& pwd = {}) { load(path, scheme, pwd); } 66 : 67 : /** Serialize structured archive data to memory. */ 68 : std::string serialize() const; 69 : 70 : /** Deserialize archive from memory. */ 71 : void deserialize(const std::vector<uint8_t>& data, const std::vector<uint8_t>& salt); 72 : 73 : /** Load archive from file, optionally encrypted with provided password. */ 74 96 : void load(const std::filesystem::path& path, std::string_view scheme, const std::string& pwd) { 75 96 : auto data = fileutils::readArchive(path, scheme, pwd); 76 94 : deserialize(data.data, data.salt); 77 94 : } 78 : 79 : /** Save archive to file, optionally encrypted with provided password. */ 80 818 : void save(const std::filesystem::path& path, std::string_view scheme, const std::string& password) const { 81 818 : fileutils::writeArchive(serialize(), path, scheme, password, password_salt); 82 818 : } 83 : }; 84 : 85 : } // namespace jami