diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h index 476953814b8d774bfb64065c98889f4ab82246e2..5618d4db0e6631c783f48cea81a3cd4702473694 100644 --- a/include/opendht/dhtrunner.h +++ b/include/opendht/dhtrunner.h @@ -257,6 +257,17 @@ public: } void putEncrypted(const std::string& key, InfoHash to, Value&& value, DoneCallback cb={}, bool permanent = false); + void putEncrypted(InfoHash hash, const std::shared_ptr<crypto::PublicKey>& to, std::shared_ptr<Value> value, DoneCallback cb={}, bool permanent = false); + void putEncrypted(InfoHash hash, const std::shared_ptr<crypto::PublicKey>& to, std::shared_ptr<Value> value, DoneCallbackSimple cb, bool permanent = false) { + putEncrypted(hash, to, value, bindDoneCb(cb), permanent); + } + + void putEncrypted(InfoHash hash, const std::shared_ptr<crypto::PublicKey>& to, Value&& value, DoneCallback cb={}, bool permanent = false); + void putEncrypted(InfoHash hash, const std::shared_ptr<crypto::PublicKey>& to, Value&& value, DoneCallbackSimple cb, bool permanent = false) { + putEncrypted(hash, to, std::forward<Value>(value), bindDoneCb(cb), permanent); + } + + /** * Insert known nodes to the routing table, without necessarly ping them. * Usefull to restart a node and get things running fast without putting load on the network. diff --git a/include/opendht/securedht.h b/include/opendht/securedht.h index e1d046606404c3b4701867960cdc47d7715f8c71..82ac84ac6f2b2cead57f4f33d800ba8c3a5fc0d3 100644 --- a/include/opendht/securedht.h +++ b/include/opendht/securedht.h @@ -118,6 +118,10 @@ public: void putEncrypted(const InfoHash& hash, const InfoHash& to, Value&& v, DoneCallback callback, bool permanent = false) { putEncrypted(hash, to, std::make_shared<Value>(std::move(v)), callback, permanent); } + void putEncrypted(const InfoHash& hash, const crypto::PublicKey& to, Sp<Value> val, DoneCallback callback, bool permanent = false); + void putEncrypted(const InfoHash& hash, const crypto::PublicKey& to, Value&& v, DoneCallback callback, bool permanent = false) { + putEncrypted(hash, to, std::make_shared<Value>(std::move(v)), callback, permanent); + } /** * Take ownership of the value and sign it using our private key. diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp index a145d4f3313e2afa4322d34279d5fd5bd16e6006..f5a4aced247cb95f5c51ddc0cc29b1514fee5a11 100644 --- a/src/dhtrunner.cpp +++ b/src/dhtrunner.cpp @@ -966,6 +966,31 @@ DhtRunner::putEncrypted(const std::string& key, InfoHash to, Value&& value, Done putEncrypted(InfoHash::get(key), to, std::forward<Value>(value), std::move(cb), permanent); } +void +DhtRunner::putEncrypted(InfoHash hash, const std::shared_ptr<crypto::PublicKey>& to, std::shared_ptr<Value> value, DoneCallback cb, bool permanent) +{ + std::unique_lock<std::mutex> lck(storage_mtx); + if (running != State::Running) { + lck.unlock(); + if (cb) cb(false, {}); + return; + } + ongoing_ops++; + pending_ops.emplace([=, + cb = std::move(cb), + value = std::move(value) + ] (SecureDht& dht) mutable { + dht.putEncrypted(hash, *to, value, bindOpDoneCallback(std::move(cb)), permanent); + }); + cv.notify_all(); +} + +void +DhtRunner::putEncrypted(InfoHash hash, const std::shared_ptr<crypto::PublicKey>& to, Value&& value, DoneCallback cb, bool permanent) +{ + putEncrypted(hash, to, std::make_shared<Value>(std::move(value)), std::move(cb), permanent); +} + void DhtRunner::bootstrap(const std::string& host, const std::string& service) { diff --git a/src/securedht.cpp b/src/securedht.cpp index 407c86b2b3bbf4e4d66897d3e023749c2d4f9f21..2c16d6938661437d75ad82fca791fcc4f8070edd 100644 --- a/src/securedht.cpp +++ b/src/securedht.cpp @@ -200,8 +200,6 @@ SecureDht::findCertificate(const InfoHash& node, const std::function<void(const auto found = std::make_shared<bool>(false); dht_->get(node, [cb,node,found,this](const std::vector<Sp<Value>>& vals) { - if (*found) - return false; for (const auto& v : vals) { if (auto cert = registerCertificate(node, v->data)) { *found = true; @@ -212,7 +210,7 @@ SecureDht::findCertificate(const InfoHash& node, const std::function<void(const return false; } } - return true; + return !*found; }, [cb,found](bool) { if (!*found and cb) cb(nullptr); @@ -424,6 +422,26 @@ SecureDht::putEncrypted(const InfoHash& hash, const InfoHash& to, Sp<Value> val, }); } +void +SecureDht::putEncrypted(const InfoHash& hash, const crypto::PublicKey& pk, Sp<Value> val, DoneCallback callback, bool permanent) +{ + if (not key_) { + if (callback) + callback(false, {}); + return; + } + if (logger_) + logger_->w("Encrypting data for PK: %s", pk.getLongId().to_c_str()); + try { + dht_->put(hash, encrypt(*val, pk), callback, time_point::max(), permanent); + } catch (const std::exception& e) { + if (logger_) + logger_->e("Error putting encrypted data: %s", e.what()); + if (callback) + callback(false, {}); + } +} + void SecureDht::sign(Value& v) const {