My Project 3.7.1
C++ Distributed Hash Table
Loading...
Searching...
No Matches
securedht.h
1// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "dht.h"
6#include "crypto.h"
7
8#include <map>
9#include <vector>
10#include <memory>
11#include <random>
12
13namespace dht {
14
15class OPENDHT_PUBLIC SecureDht final : public DhtInterface
16{
17public:
18 typedef std::function<void(bool)> SignatureCheckCallback;
19
20 using Config = SecureDhtConfig;
21
22 static dht::Config getConfig(const SecureDht::Config& conf)
23 {
24 auto c = conf.node_config;
25 if (not c.node_id and conf.id.second)
26 c.node_id = InfoHash::get("node:" + conf.id.second->getId().toString());
27 return c;
28 }
29
36 SecureDht(std::unique_ptr<DhtInterface> dht,
37 Config config,
38 IdentityAnnouncedCb iacb = {},
39 const std::shared_ptr<Logger>& l = {});
40
41 virtual ~SecureDht();
42
43 InfoHash getId() const { return key_ ? key_->getPublicKey().getId() : InfoHash(); }
44 PkId getLongId() const { return key_ ? key_->getPublicKey().getLongId() : PkId(); }
45 Sp<crypto::PublicKey> getPublicKey() const { return key_ ? key_->getSharedPublicKey() : Sp<crypto::PublicKey> {}; }
46
47 ValueType secureType(ValueType&& type);
48
49 ValueType secureType(const ValueType& type)
50 {
51 ValueType tmp_type = type;
52 return secureType(std::move(tmp_type));
53 }
54
55 void registerType(const ValueType& type) override
56 {
57 if (dht_)
58 dht_->registerType(secureType(type));
59 }
60 void registerType(ValueType&& type)
61 {
62 if (dht_)
63 dht_->registerType(secureType(std::forward<ValueType>(type)));
64 }
65 void registerInsecureType(const ValueType& type)
66 {
67 if (dht_)
68 dht_->registerType(type);
69 }
70
76 void get(const InfoHash& id, GetCallback cb, DoneCallback donecb = {}, Value::Filter&& = {}, Where&& w = {}) override;
77 void get(const InfoHash& id,
78 GetCallback cb,
79 DoneCallbackSimple donecb = {},
80 Value::Filter&& f = {},
81 Where&& w = {}) override
82 {
83 get(id, cb, bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
84 }
85 void get(const InfoHash& key,
86 GetCallbackSimple cb,
87 DoneCallback donecb = {},
88 Value::Filter&& f = {},
89 Where&& w = {}) override
90 {
91 get(key, bindGetCb(cb), donecb, std::forward<Value::Filter>(f), std::forward<Where>(w));
92 }
93 void get(const InfoHash& key,
94 GetCallbackSimple cb,
95 DoneCallbackSimple donecb,
96 Value::Filter&& f = {},
97 Where&& w = {}) override
98 {
99 get(key, bindGetCb(cb), bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
100 }
101
105 void putSigned(const InfoHash& hash, Sp<Value> val, DoneCallback callback, bool permanent = false);
106 void putSigned(const InfoHash& hash, Value&& v, DoneCallback callback, bool permanent = false)
107 {
108 putSigned(hash, std::make_shared<Value>(std::move(v)), callback, permanent);
109 }
110
117 const InfoHash& hash, const InfoHash& to, Sp<Value> val, DoneCallback callback, bool permanent = false);
118
119 [[deprecated("Use the shared_ptr version instead")]]
120 void putEncrypted(const InfoHash& hash, const InfoHash& to, Value&& v, DoneCallback callback, bool permanent = false)
121 {
122 putEncrypted(hash, to, std::make_shared<Value>(std::move(v)), callback, permanent);
123 }
124 void putEncrypted(const InfoHash& hash,
125 const crypto::PublicKey& to,
126 Sp<Value> val,
127 DoneCallback callback,
128 bool permanent = false);
129
130 [[deprecated("Use the shared_ptr version instead")]]
131 void putEncrypted(
132 const InfoHash& hash, const crypto::PublicKey& to, Value&& v, DoneCallback callback, bool permanent = false)
133 {
134 putEncrypted(hash, to, std::make_shared<Value>(std::move(v)), callback, permanent);
135 }
136
137 void putEncrypted(const InfoHash& hash, const PkId& to, Sp<Value> val, DoneCallback callback, bool permanent = false);
138
142 void sign(Value& v) const;
143
144 Value encrypt(Value& v, const crypto::PublicKey& to) const;
145
146 Value decrypt(const Value& v);
147
148 void findCertificate(const InfoHash& node, const std::function<void(const Sp<crypto::Certificate>)>& cb);
149 void findPublicKey(const InfoHash& node, const std::function<void(const Sp<crypto::PublicKey>)>& cb);
150
151 void findCertificate(const PkId& id, const std::function<void(const Sp<crypto::Certificate>)>& cb);
152 void findPublicKey(const PkId& id, const std::function<void(const Sp<crypto::PublicKey>)>& cb);
153
154 void registerCertificate(const Sp<crypto::Certificate>& cert);
155
156 Sp<crypto::Certificate> getCertificate(const InfoHash& node) const;
157 Sp<crypto::PublicKey> getPublicKey(const InfoHash& node) const;
158
159 Sp<crypto::Certificate> getCertificate(const PkId& node) const;
160 Sp<crypto::PublicKey> getPublicKey(const PkId& node) const;
161
167 void setLocalCertificateStore(CertificateStoreQueryLegacy&& query_method)
168 {
169 localQueryMethodLegacy_ = std::move(query_method);
170 }
171 void setLocalCertificateStore(CertificateStoreQuery&& query_method) { localQueryMethod_ = std::move(query_method); }
172 void setOnPublicAddressChanged(PublicAddressChangedCb cb) override { dht_->setOnPublicAddressChanged(cb); }
173
177 void shutdown(ShutdownCallback cb, bool stop = false) override { dht_->shutdown(cb, stop); }
178 void dumpTables() const override { dht_->dumpTables(); }
179 inline const InfoHash& getNodeId() const override { return dht_->getNodeId(); }
180
181 std::pair<size_t, size_t> getStoreSize() const override { return dht_->getStoreSize(); }
182 std::pair<size_t, size_t> getLocalStoreSize() const override { return dht_->getLocalStoreSize(); }
183 std::string getStorageLog() const override { return dht_->getStorageLog(); }
184 std::string getStorageLog(const InfoHash& h) const override { return dht_->getStorageLog(h); }
185 void setStorageLimit(size_t limit = 0) override { dht_->setStorageLimit(limit); }
186 size_t getStorageLimit() const override { return dht_->getStorageLimit(); }
187 void setLocalStorageLimit(size_t limit = 0) override { dht_->setLocalStorageLimit(limit); }
188 size_t getLocalStorageLimit() const override { return dht_->getLocalStorageLimit(); }
189
190 std::vector<NodeExport> exportNodes() const override { return dht_->exportNodes(); }
191 std::vector<ValuesExport> exportValues() const override { return dht_->exportValues(); }
192 void importValues(const std::vector<ValuesExport>& v) override { dht_->importValues(v); }
193 NodeStats getNodesStats(sa_family_t af) const override { return dht_->getNodesStats(af); }
194
195 NodeInfo getNodeInfo()
196 {
197 NodeInfo info;
198 info.id = getId();
199 info.node_id = getNodeId();
200 info.ipv4 = getNodesStats(AF_INET);
201 info.ipv6 = getNodesStats(AF_INET6);
202 std::tie(info.storage_size, info.storage_values) = getStoreSize();
203 std::tie(info.local_storage_size, info.local_storage_values) = getLocalStoreSize();
204 if (auto sock = getSocket()) {
205 info.bound4 = sock->getBoundRef(AF_INET).getPort();
206 info.bound6 = sock->getBoundRef(AF_INET6).getPort();
207 }
208 return info;
209 }
210
211 std::vector<unsigned> getNodeMessageStats(bool in = false) override { return dht_->getNodeMessageStats(in); }
212 std::string getRoutingTablesLog(sa_family_t af) const override { return dht_->getRoutingTablesLog(af); }
213 std::string getSearchesLog(sa_family_t af) const override { return dht_->getSearchesLog(af); }
214 std::string getSearchLog(const InfoHash& h, sa_family_t af = AF_UNSPEC) const override
215 {
216 return dht_->getSearchLog(h, af);
217 }
218 std::vector<SockAddr> getPublicAddress(sa_family_t family = 0) override { return dht_->getPublicAddress(family); }
219 time_point periodic(const uint8_t* buf, size_t buflen, SockAddr sa, const time_point& now) override
220 {
221 return dht_->periodic(buf, buflen, std::move(sa), now);
222 }
223 time_point periodic(
224 const uint8_t* buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) override
225 {
226 return dht_->periodic(buf, buflen, from, fromlen, now);
227 }
228 NodeStatus updateStatus(sa_family_t af) override { return dht_->updateStatus(af); }
229 NodeStatus getStatus(sa_family_t af) const override { return dht_->getStatus(af); }
230 NodeStatus getStatus() const override { return dht_->getStatus(); }
231 net::DatagramSocket* getSocket() const override { return dht_->getSocket(); };
232 bool isRunning(sa_family_t af = 0) const override { return dht_->isRunning(af); }
233 const ValueType& getType(ValueType::Id type_id) const override { return dht_->getType(type_id); }
234 void addBootstrap(const std::string& host, const std::string& service) override
235 {
236 dht_->addBootstrap(host, service);
237 }
238 void clearBootstrap() override { dht_->clearBootstrap(); }
239 void insertNode(const InfoHash& id, const SockAddr& sa) override { dht_->insertNode(id, sa); }
240 void insertNode(const NodeExport& n) override { dht_->insertNode(n); }
241 void pingNode(SockAddr sa, DoneCallbackSimple&& cb = {}) override { dht_->pingNode(std::move(sa), std::move(cb)); }
242 void query(const InfoHash& key, QueryCallback cb, DoneCallback done_cb = {}, Query&& q = {}) override
243 {
244 dht_->query(key, cb, done_cb, std::move(q));
245 }
246 void query(const InfoHash& key, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query&& q = {}) override
247 {
248 dht_->query(key, cb, done_cb, std::move(q));
249 }
250 std::vector<Sp<Value>> getLocal(const InfoHash& key, const Value::Filter& f = {}) const override
251 {
252 return dht_->getLocal(key, f);
253 }
254 Sp<Value> getLocalById(const InfoHash& key, Value::Id vid) const override { return dht_->getLocalById(key, vid); }
255 void put(const InfoHash& key,
256 Sp<Value> v,
257 DoneCallback cb = nullptr,
258 time_point created = time_point::max(),
259 bool permanent = false) override
260 {
261 dht_->put(key, v, cb, created, permanent);
262 }
263 void put(const InfoHash& key,
264 const Sp<Value>& v,
265 DoneCallbackSimple cb,
266 time_point created = time_point::max(),
267 bool permanent = false) override
268 {
269 dht_->put(key, v, cb, created, permanent);
270 }
271
272 void put(const InfoHash& key,
273 Value&& v,
274 DoneCallback cb = nullptr,
275 time_point created = time_point::max(),
276 bool permanent = false) override
277 {
278 dht_->put(key, std::move(v), cb, created, permanent);
279 }
280 void put(const InfoHash& key,
281 Value&& v,
282 DoneCallbackSimple cb,
283 time_point created = time_point::max(),
284 bool permanent = false) override
285 {
286 dht_->put(key, std::move(v), cb, created, permanent);
287 }
288 std::vector<Sp<Value>> getPut(const InfoHash& h) const override { return dht_->getPut(h); }
289 Sp<Value> getPut(const InfoHash& h, const Value::Id& vid) const override { return dht_->getPut(h, vid); }
290 bool cancelPut(const InfoHash& h, const Value::Id& vid) override { return dht_->cancelPut(h, vid); }
291
292 size_t listen(const InfoHash& key, ValueCallback, Value::Filter = {}, Where = {}) override;
293 size_t listen(const InfoHash& key, GetCallback cb, Value::Filter = {}, Where w = {}) override;
294 size_t listen(const InfoHash& key, GetCallbackSimple cb, Value::Filter f = {}, Where w = {}) override
295 {
296 return listen(key, bindGetCb(cb), f, w);
297 }
298 bool cancelListen(const InfoHash& h, size_t token) override { return dht_->cancelListen(h, token); }
299 void connectivityChanged(sa_family_t af) override { dht_->connectivityChanged(af); }
300 void connectivityChanged() override { dht_->connectivityChanged(); }
301
302 void forwardAllMessages(bool forward) { forward_all_ = forward; }
303
304 void setPushNotificationToken(const std::string& token = "") override { dht_->setPushNotificationToken(token); }
305
310 PushNotificationResult pushNotificationReceived(const std::map<std::string, std::string>& notification) override
311 {
312 return dht_->pushNotificationReceived(notification);
313 }
314
315 void setLogger(const std::shared_ptr<Logger>& logger) override
316 {
317 DhtInterface::setLogger(logger);
318 dht_->setLogger(logger);
319 }
320
324 void setLogFilter(const InfoHash& f) override
325 {
327 dht_->setLogFilter(f);
328 }
329
330private:
331 std::unique_ptr<DhtInterface> dht_;
332 // prevent copy
333 SecureDht(const SecureDht&) = delete;
334 SecureDht& operator=(const SecureDht&) = delete;
335
336 Sp<Value> checkValue(const Sp<Value>& v);
337 ValueCallback getCallbackFilter(const ValueCallback&, Value::Filter&&);
338 GetCallback getCallbackFilter(const GetCallback&, Value::Filter&&);
339
340 Sp<crypto::Certificate> registerCertificate(const InfoHash& node, const Blob& cert);
341 Sp<crypto::Certificate> registerCertificate(const PkId& node, const Blob& cert);
342
343 Sp<crypto::PrivateKey> key_ {};
344 Sp<crypto::Certificate> certificate_ {};
345
346 // method to query the local certificate store
347 CertificateStoreQuery localQueryMethod_ {};
348 CertificateStoreQueryLegacy localQueryMethodLegacy_ {};
349
350 // our certificate cache
351 std::map<InfoHash, Sp<crypto::Certificate>> nodesCertificates_ {};
352 std::map<InfoHash, Sp<crypto::PublicKey>> nodesPubKeys_ {};
353 std::map<PkId, Sp<crypto::Certificate>> nodesCertificatesLong_ {};
354 std::map<PkId, Sp<crypto::PublicKey>> nodesPubKeysLong_ {};
355
356 std::atomic_bool forward_all_ {false};
357 bool enableCache_ {false};
358};
359
360const ValueType CERTIFICATE_TYPE = {8,
361 "Certificate",
362 std::chrono::hours(24 * 7),
363 // A certificate can only be stored at its public key ID.
364 [](InfoHash id, Sp<Value>& v, const InfoHash&, const SockAddr&) {
365 try {
366 crypto::Certificate crt(v->data);
367 // TODO check certificate signature
368 return crt.getPublicKey().getId() == id
369 || InfoHash::get(crt.getPublicKey().getLongId()) == id;
370 } catch (const std::exception& e) {
371 }
372 return false;
373 },
374 [](InfoHash, const Sp<Value>& o, Sp<Value>& n, const InfoHash&, const SockAddr&) {
375 try {
376 return crypto::Certificate(o->data).getPublicKey().getLongId()
377 == crypto::Certificate(n->data).getPublicKey().getLongId();
378 } catch (const std::exception& e) {
379 }
380 return false;
381 }};
382
383} // namespace dht
virtual void setLogFilter(const InfoHash &f)
void putSigned(const InfoHash &hash, Sp< Value > val, DoneCallback callback, bool permanent=false)
size_t listen(const InfoHash &key, GetCallback cb, Value::Filter={}, Where w={}) override
bool isRunning(sa_family_t af=0) const override
Definition securedht.h:232
void insertNode(const InfoHash &id, const SockAddr &sa) override
Definition securedht.h:239
void setLocalStorageLimit(size_t limit=0) override
Definition securedht.h:187
void query(const InfoHash &key, QueryCallback cb, DoneCallback done_cb={}, Query &&q={}) override
Definition securedht.h:242
void get(const InfoHash &id, GetCallback cb, DoneCallback donecb={}, Value::Filter &&={}, Where &&w={}) override
void sign(Value &v) const
SecureDht(std::unique_ptr< DhtInterface > dht, Config config, IdentityAnnouncedCb iacb={}, const std::shared_ptr< Logger > &l={})
void setLogFilter(const InfoHash &f) override
Definition securedht.h:324
void shutdown(ShutdownCallback cb, bool stop=false) override
Definition securedht.h:177
void setStorageLimit(size_t limit=0) override
Definition securedht.h:185
std::vector< NodeExport > exportNodes() const override
Definition securedht.h:190
std::vector< Sp< Value > > getLocal(const InfoHash &key, const Value::Filter &f={}) const override
Definition securedht.h:250
bool cancelPut(const InfoHash &h, const Value::Id &vid) override
Definition securedht.h:290
std::vector< Sp< Value > > getPut(const InfoHash &h) const override
Definition securedht.h:288
void put(const InfoHash &key, Sp< Value > v, DoneCallback cb=nullptr, time_point created=time_point::max(), bool permanent=false) override
Definition securedht.h:255
std::pair< size_t, size_t > getStoreSize() const override
Definition securedht.h:181
Sp< Value > getLocalById(const InfoHash &key, Value::Id vid) const override
Definition securedht.h:254
Sp< Value > getPut(const InfoHash &h, const Value::Id &vid) const override
Definition securedht.h:289
void putEncrypted(const InfoHash &hash, const InfoHash &to, Sp< Value > val, DoneCallback callback, bool permanent=false)
void setLocalCertificateStore(CertificateStoreQueryLegacy &&query_method)
Definition securedht.h:167
const InfoHash & getNodeId() const override
Definition securedht.h:179
PushNotificationResult pushNotificationReceived(const std::map< std::string, std::string > &notification) override
Definition securedht.h:310
NodeStatus updateStatus(sa_family_t af) override
Definition securedht.h:228
void connectivityChanged(sa_family_t af) override
Definition securedht.h:299
std::vector< uint8_t > Blob
Definition utils.h:156
NodeStatus
Definition callbacks.h:24
InfoHash node_id
Definition callbacks.h:101
Describes a query destined to another peer.
Definition value.h:988
Serializable dht::Value filter.
Definition value.h:850
const PkId & getLongId() const