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 : #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 774 : 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 106 : AccountArchive(const std::filesystem::path& path, std::string_view scheme = {}, const std::string& pwd = {})
72 106 : {
73 106 : load(path, scheme, pwd);
74 146 : }
75 :
76 : /** Serialize structured archive data to memory. */
77 : std::string serialize() const;
78 :
79 : /** Deserialize archive from memory. */
80 : void deserialize(std::string_view data, const std::vector<uint8_t>& salt);
81 :
82 : /** Load archive from file, optionally encrypted with provided password. */
83 106 : void load(const std::filesystem::path& path, std::string_view scheme, const std::string& pwd)
84 : {
85 106 : auto data = fileutils::readArchive(path, scheme, pwd);
86 102 : deserialize(data.data, data.salt);
87 102 : }
88 :
89 : /** Save archive to file, optionally encrypted with provided password. */
90 822 : bool save(const std::filesystem::path& path, std::string_view scheme, const std::string& password) const
91 : {
92 822 : return fileutils::writeArchive(serialize(), path, scheme, password, password_salt);
93 : }
94 : };
95 :
96 : } // namespace jami
|