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 : #ifdef HAVE_CONFIG_H
21 : #include "config.h"
22 : #endif
23 :
24 : #include "contact_list.h"
25 : #include "logger.h"
26 : #if HAVE_RINGNS
27 : #include "namedirectory.h"
28 : #endif
29 :
30 : #include <opendht/crypto.h>
31 : #include <optional>
32 : #include <functional>
33 : #include <map>
34 : #include <string>
35 : #include <filesystem>
36 :
37 : namespace dht {
38 : class DhtRunner;
39 : }
40 :
41 : namespace jami {
42 :
43 : using DeviceId = dht::PkId;
44 : struct AccountArchive;
45 :
46 : struct AccountInfo
47 : {
48 : dht::crypto::Identity identity;
49 : std::unique_ptr<ContactList> contacts;
50 : std::string accountId;
51 : std::string deviceId;
52 : std::shared_ptr<dht::crypto::PublicKey> devicePk;
53 : std::shared_ptr<dht::Value> announce;
54 : std::string ethAccount;
55 : std::string username;
56 : std::string photo;
57 : };
58 :
59 : template<typename To, typename From>
60 : std::unique_ptr<To>
61 583 : dynamic_unique_cast(std::unique_ptr<From>&& p)
62 : {
63 583 : if (auto cast = dynamic_cast<To*>(p.get())) {
64 583 : std::unique_ptr<To> result(cast);
65 583 : p.release();
66 583 : return result;
67 583 : }
68 0 : return {};
69 : }
70 :
71 : class AccountManager: public std::enable_shared_from_this<AccountManager>
72 : {
73 : public:
74 : using OnChangeCallback = ContactList::OnChangeCallback;
75 : using clock = std::chrono::system_clock;
76 : using time_point = clock::time_point;
77 : using OnNewDeviceCb = std::function<void(const std::shared_ptr<dht::crypto::Certificate>&)>;
78 : using OnDeviceAnnouncedCb = std::function<void()>;
79 :
80 596 : AccountManager(const std::filesystem::path& path, const std::string& nameServer)
81 1192 : : path_(path)
82 596 : , nameDir_(NameDirectory::instance(nameServer)) {};
83 :
84 : virtual ~AccountManager();
85 :
86 : constexpr static const char* const DHT_TYPE_NS = "cx.ring";
87 :
88 : // Auth
89 :
90 : enum class AuthError { UNKNOWN, INVALID_ARGUMENTS, SERVER_ERROR, NETWORK };
91 :
92 : using AuthSuccessCallback = std::function<void(const AccountInfo& info,
93 : const std::map<std::string, std::string>& config,
94 : std::string&& receipt,
95 : std::vector<uint8_t>&& receipt_signature)>;
96 :
97 : using AuthFailureCallback = std::function<void(AuthError error, const std::string& message)>;
98 : using DeviceSyncCallback = std::function<void(DeviceSync&& syncData)>;
99 : using CertRequest = std::future<std::unique_ptr<dht::crypto::CertificateRequest>>;
100 : using PrivateKey = std::shared_future<std::shared_ptr<dht::crypto::PrivateKey>>;
101 :
102 : CertRequest buildRequest(PrivateKey fDeviceKey);
103 :
104 : struct AccountCredentials
105 : {
106 : std::string scheme;
107 : std::string uri;
108 : std::string password_scheme;
109 : std::string password;
110 583 : virtual ~AccountCredentials() {};
111 : };
112 :
113 : virtual void initAuthentication(const std::string& accountId,
114 : PrivateKey request,
115 : std::string deviceName,
116 : std::unique_ptr<AccountCredentials> credentials,
117 : AuthSuccessCallback onSuccess,
118 : AuthFailureCallback onFailure,
119 : const OnChangeCallback& onChange)
120 : = 0;
121 :
122 : virtual bool changePassword(const std::string& password_old, const std::string& password_new) = 0;
123 :
124 : virtual void syncDevices() = 0;
125 : virtual void onSyncData(DeviceSync&& device, bool checkDevice = true);
126 :
127 0 : virtual bool isPasswordValid(const std::string& /*password*/) { return false; };
128 0 : virtual std::vector<uint8_t> getPasswordKey(const std::string& /*password*/) { return {}; };
129 :
130 : dht::crypto::Identity loadIdentity(const std::string& accountId,
131 : const std::string& crt_path,
132 : const std::string& key_path,
133 : const std::string& key_pwd) const;
134 :
135 : const AccountInfo* useIdentity(const std::string& accountId,
136 : const dht::crypto::Identity& id,
137 : const std::string& receipt,
138 : const std::vector<uint8_t>& receiptSignature,
139 : const std::string& username,
140 : const OnChangeCallback& onChange);
141 : Json::Value announceFromReceipt(const std::string& receipt);
142 :
143 539 : void setDht(const std::shared_ptr<dht::DhtRunner>& dht) { dht_ = dht; }
144 :
145 : virtual void startSync(const OnNewDeviceCb& cb, const OnDeviceAnnouncedCb& dcb, bool publishPresence = true);
146 :
147 120716 : const AccountInfo* getInfo() const { return info_.get(); }
148 :
149 : // Device management
150 :
151 : enum class AddDeviceResult {
152 : SUCCESS_SHOW_PIN = 0,
153 : ERROR_CREDENTIALS,
154 : ERROR_NETWORK,
155 : };
156 : using AddDeviceCallback = std::function<void(AddDeviceResult, std::string pin)>;
157 :
158 : enum class RevokeDeviceResult {
159 : SUCCESS = 0,
160 : ERROR_CREDENTIALS,
161 : ERROR_NETWORK,
162 : };
163 : using RevokeDeviceCallback = std::function<void(RevokeDeviceResult)>;
164 :
165 0 : virtual void addDevice(const std::string& /*password*/, AddDeviceCallback) {};
166 0 : virtual bool revokeDevice(const std::string& /*device*/,
167 : std::string_view /*scheme*/,
168 : const std::string& /*password*/,
169 : RevokeDeviceCallback)
170 : {
171 0 : return false;
172 : };
173 :
174 : const std::map<dht::PkId, KnownDevice>& getKnownDevices() const;
175 : bool foundAccountDevice(const std::shared_ptr<dht::crypto::Certificate>& crt,
176 : const std::string& name = {},
177 1067 : const time_point& last_sync = time_point::min());
178 : // bool removeAccountDevice(const dht::InfoHash& device);
179 : void setAccountDeviceName(/*const dht::InfoHash& device, */ const std::string& name);
180 : std::string getAccountDeviceName() const;
181 :
182 : void forEachDevice(const dht::InfoHash& to,
183 : std::function<void(const std::shared_ptr<dht::crypto::PublicKey>&)>&& op,
184 : std::function<void(bool)>&& end = {});
185 :
186 : using PeerCertificateCb = std::function<void(const std::shared_ptr<dht::crypto::Certificate>& crt,
187 : const dht::InfoHash& peer_account)>;
188 : void onPeerMessage(const dht::crypto::PublicKey& peer_device,
189 : bool allowPublic,
190 : PeerCertificateCb&& cb);
191 : bool onPeerCertificate(const std::shared_ptr<dht::crypto::Certificate>& crt,
192 : bool allowPublic,
193 : dht::InfoHash& account_id);
194 :
195 : /**
196 : * Inform that a potential peer device have been found.
197 : * Returns true only if the device certificate is a valid device certificate.
198 : * In that case (true is returned) the account_id parameter is set to the peer account ID.
199 : */
200 : static bool foundPeerDevice(const std::shared_ptr<dht::crypto::Certificate>& crt,
201 : dht::InfoHash& account_id);
202 :
203 : // Contact requests
204 :
205 : std::vector<std::map<std::string, std::string>> getTrustRequests() const;
206 : // Note: includeConversation used for compatibility test, do not use if not in test env.
207 : bool acceptTrustRequest(const std::string& from, bool includeConversation = true);
208 : bool discardTrustRequest(const std::string& from);
209 :
210 : void sendTrustRequest(const std::string& to,
211 : const std::string& convId,
212 : const std::vector<uint8_t>& payload);
213 : void sendTrustRequestConfirm(const dht::InfoHash& to,
214 : const std::string& conversationId); // TODO ideally no convId here
215 :
216 : // Contact
217 :
218 : /**
219 : * Add contact to the account contact list.
220 : * Set confirmed if we know the contact also added us.
221 : */
222 : bool addContact(const std::string& uri,
223 : bool confirmed = false,
224 : const std::string& conversationId = "");
225 : void removeContact(const std::string& uri, bool banned = true);
226 : void removeContactConversation(const std::string& uri); // for non swarm contacts
227 : void updateContactConversation(const std::string& uri, const std::string& convId);
228 : std::vector<std::map<std::string, std::string>> getContacts(bool includeRemoved = false) const;
229 :
230 : /** Obtain details about one account contact in serializable form. */
231 : std::map<std::string, std::string> getContactDetails(const std::string& uri) const;
232 :
233 : virtual bool findCertificate(
234 : const dht::InfoHash& h,
235 : std::function<void(const std::shared_ptr<dht::crypto::Certificate>&)>&& cb = {});
236 :
237 : virtual bool findCertificate(
238 : const dht::PkId& h,
239 : std::function<void(const std::shared_ptr<dht::crypto::Certificate>&)>&& cb = {});
240 :
241 : bool setCertificateStatus(const std::string& cert_id, dhtnet::tls::TrustStore::PermissionStatus status);
242 : bool setCertificateStatus(const std::shared_ptr<crypto::Certificate>& cert,
243 : dhtnet::tls::TrustStore::PermissionStatus status,
244 : bool local = true);
245 : std::vector<std::string> getCertificatesByStatus(dhtnet::tls::TrustStore::PermissionStatus status);
246 : dhtnet::tls::TrustStore::PermissionStatus getCertificateStatus(const std::string& cert_id) const;
247 : bool isAllowed(const crypto::Certificate& crt, bool allowPublic = false);
248 :
249 : static std::shared_ptr<dht::Value> parseAnnounce(const std::string& announceBase64,
250 : const std::string& accountId,
251 : const std::string& deviceSha1);
252 :
253 : // Name resolver
254 : using LookupCallback = NameDirectory::LookupCallback;
255 : using SearchResult = NameDirectory::SearchResult;
256 : using SearchCallback = NameDirectory::SearchCallback;
257 : using RegistrationCallback = NameDirectory::RegistrationCallback;
258 : using SearchResponse = NameDirectory::Response;
259 :
260 : virtual void lookupUri(const std::string& name,
261 : const std::string& defaultServer,
262 : LookupCallback cb);
263 : virtual void lookupAddress(const std::string& address, LookupCallback cb);
264 0 : virtual bool searchUser(const std::string& /*query*/, SearchCallback /*cb*/) { return false; }
265 : virtual void registerName(const std::string& name,
266 : std::string_view scheme,
267 : const std::string& password,
268 : RegistrationCallback cb)
269 : = 0;
270 :
271 : dhtnet::tls::CertificateStore& certStore() const;
272 :
273 : protected:
274 : std::filesystem::path path_;
275 : OnChangeCallback onChange_;
276 : std::unique_ptr<AccountInfo> info_;
277 : std::string accountId_;
278 : std::shared_ptr<dht::DhtRunner> dht_;
279 : std::reference_wrapper<NameDirectory> nameDir_;
280 : };
281 :
282 : } // namespace jami
|