diff --git a/c/opendht.cpp b/c/opendht.cpp index cb239581..bbedef15 100644 --- a/c/opendht.cpp +++ b/c/opendht.cpp @@ -600,7 +600,7 @@ dht_runner_put_signed( void dht_runner_put_encrypted(dht_runner* r, const dht_infohash* h, - const dht_infohash* to, + const dht_pkid* to, const dht_value* v, dht_done_cb done_cb, void* cb_user_data, @@ -608,7 +608,7 @@ dht_runner_put_encrypted(dht_runner* r, { auto runner = reinterpret_cast(r); auto hash = reinterpret_cast(h); - auto toHash = reinterpret_cast(to); + auto toHash = reinterpret_cast(to); auto value = reinterpret_cast(v); runner->putEncrypted( *hash, diff --git a/c/opendht_c.h b/c/opendht_c.h index f9fbc9e6..3c273838 100644 --- a/c/opendht_c.h +++ b/c/opendht_c.h @@ -195,7 +195,7 @@ OPENDHT_C_PUBLIC void dht_runner_put_signed(dht_runner* runner, bool permanent); OPENDHT_C_PUBLIC void dht_runner_put_encrypted(dht_runner* runner, const dht_infohash* hash, - const dht_infohash* to, + const dht_pkid* to, const dht_value* value, dht_done_cb done_cb, void* cb_user_data, diff --git a/include/opendht/callbacks.h b/include/opendht/callbacks.h index b30045f3..ef142fca 100644 --- a/include/opendht/callbacks.h +++ b/include/opendht/callbacks.h @@ -187,8 +187,6 @@ using ShutdownCallback = std::function; using IdentityAnnouncedCb = std::function; using PublicAddressChangedCb = std::function)>; -using CertificateStoreQueryLegacy - = std::function>(const InfoHash& pk_id)>; using CertificateStoreQuery = std::function>(const PkId& pk_id)>; using DoneCallback = std::function>& nodes)>; diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h index 9b104f16..53d1e308 100644 --- a/include/opendht/dhtrunner.h +++ b/include/opendht/dhtrunner.h @@ -61,8 +61,7 @@ public: std::unique_ptr sock; std::shared_ptr peerDiscovery {}; StatusCallback statusChangedCallback {}; - CertificateStoreQueryLegacy certificateStore {}; - CertificateStoreQuery certificateStorePkId {}; + CertificateStoreQuery certificateStore {}; IdentityAnnouncedCb identityAnnouncedCb {}; PublicAddressChangedCb publicAddressChangedCb {}; std::unique_ptr rng {}; @@ -286,21 +285,6 @@ public: } void putSigned(const std::string& key, Value&& value, DoneCallbackSimple cb = {}, bool permanent = false); - void putEncrypted( - InfoHash hash, InfoHash to, std::shared_ptr value, DoneCallback cb = {}, bool permanent = false); - void putEncrypted( - InfoHash hash, InfoHash to, std::shared_ptr value, DoneCallbackSimple cb, bool permanent = false) - { - putEncrypted(hash, to, value, bindDoneCb(cb), permanent); - } - - void putEncrypted(InfoHash hash, InfoHash to, Value&& value, DoneCallback cb = {}, bool permanent = false); - void putEncrypted(InfoHash hash, InfoHash to, Value&& value, DoneCallbackSimple cb, bool permanent = false) - { - putEncrypted(hash, to, std::forward(value), bindDoneCb(cb), permanent); - } - void putEncrypted(const std::string& key, InfoHash to, Value&& value, DoneCallback cb = {}, bool permanent = false); - void putEncrypted(InfoHash hash, const std::shared_ptr& to, std::shared_ptr value, @@ -450,13 +434,9 @@ public: void getPublicAddress(std::function&&)>, sa_family_t af = AF_UNSPEC); // securedht methods - - [[deprecated("Use PkId version")]] void findCertificate( - InfoHash hash, std::function&)>); void findCertificate(PkId hash, std::function&)>); void registerCertificate(const std::shared_ptr& cert); - void setLocalCertificateStore(CertificateStoreQueryLegacy&& query_method); void setLocalCertificateStore(CertificateStoreQuery&& query_method); /** diff --git a/include/opendht/securedht.h b/include/opendht/securedht.h index 6a80c323..72e5ff65 100644 --- a/include/opendht/securedht.h +++ b/include/opendht/securedht.h @@ -113,14 +113,6 @@ public: * and put it in the DHT. * The operation will be immediate if the recipient' public key is known (otherwise it will be retrived first). */ - void putEncrypted( - const InfoHash& hash, const InfoHash& to, Sp val, DoneCallback callback, bool permanent = false); - - [[deprecated("Use the shared_ptr version instead")]] - void putEncrypted(const InfoHash& hash, const InfoHash& to, Value&& v, DoneCallback callback, bool permanent = false) - { - putEncrypted(hash, to, std::make_shared(std::move(v)), callback, permanent); - } void putEncrypted(const InfoHash& hash, const crypto::PublicKey& to, Sp val, @@ -145,19 +137,11 @@ public: Value decrypt(const Value& v); - [[deprecated("Use PkId version")]] void findCertificate( - const InfoHash& node, const std::function)>& cb); - [[deprecated("Use PkId version")]] void findPublicKey(const InfoHash& node, - const std::function)>& cb); - void findCertificate(const PkId& id, const std::function)>& cb); void findPublicKey(const PkId& id, const std::function)>& cb); void registerCertificate(const Sp& cert); - [[deprecated("Use PkId version")]] Sp getCertificate(const InfoHash& node) const; - [[deprecated("Use PkId version")]] Sp getPublicKey(const InfoHash& node) const; - Sp getCertificate(const PkId& node) const; Sp getPublicKey(const PkId& node) const; @@ -166,10 +150,6 @@ public: * The search key used is the public key ID, so there may be multiple certificates retured, signed with * the same private key. */ - void setLocalCertificateStore(CertificateStoreQueryLegacy&& query_method) - { - localQueryMethodLegacy_ = std::move(query_method); - } void setLocalCertificateStore(CertificateStoreQuery&& query_method) { localQueryMethod_ = std::move(query_method); } void setOnPublicAddressChanged(PublicAddressChangedCb cb) override { dht_->setOnPublicAddressChanged(cb); } @@ -347,13 +327,10 @@ private: // method to query the local certificate store CertificateStoreQuery localQueryMethod_ {}; - CertificateStoreQueryLegacy localQueryMethodLegacy_ {}; // our certificate cache - std::map> nodesCertificates_ {}; - std::map> nodesPubKeys_ {}; - std::map> nodesCertificatesLong_ {}; - std::map> nodesPubKeysLong_ {}; + std::map> nodesCertificates_ {}; + std::map> nodesPubKeys_ {}; std::atomic_bool forward_all_ {false}; bool enableCache_ {false}; diff --git a/rust/src/dhtrunner.rs b/rust/src/dhtrunner.rs index 443c501f..e97079f3 100644 --- a/rust/src/dhtrunner.rs +++ b/rust/src/dhtrunner.rs @@ -226,7 +226,7 @@ impl DhtRunner { pub fn put_encrypted<'a>( &mut self, h: &InfoHash, - to: &InfoHash, + to: &PkId, v: Box, done_cb: &'a mut (dyn FnMut(bool)), permanent: bool, diff --git a/rust/src/ffi.rs b/rust/src/ffi.rs index 4e4b50e8..a9ac9eb3 100644 --- a/rust/src/ffi.rs +++ b/rust/src/ffi.rs @@ -190,7 +190,7 @@ extern { cb_user_data: *mut c_void, permanent: bool); pub fn dht_runner_put_encrypted(dht: *mut DhtRunner, h: *const InfoHash, - to: *const InfoHash, v: *const Value, + to: *const PkId, v: *const Value, done_cb: extern fn(bool, *mut c_void), cb_user_data: *mut c_void, permanent: bool); diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp index 8db5a391..c24be31d 100644 --- a/src/dhtrunner.cpp +++ b/src/dhtrunner.cpp @@ -186,9 +186,6 @@ DhtRunner::run(const Config& config, Context&& context) if (context.certificateStore) { dht_->setLocalCertificateStore(std::move(context.certificateStore)); } - if (context.certificateStorePkId) { - dht_->setLocalCertificateStore(std::move(context.certificateStorePkId)); - } if (context.publicAddressChangedCb) { dht_->setOnPublicAddressChanged(std::move(context.publicAddressChangedCb)); } @@ -667,14 +664,6 @@ DhtRunner::registerCertificate(const std::shared_ptr& cert) dht_->registerCertificate(cert); } -void -DhtRunner::setLocalCertificateStore(CertificateStoreQueryLegacy&& query_method) -{ - std::lock_guard lck(dht_mtx); - if (dht_) - dht_->setLocalCertificateStore(std::forward(query_method)); -} - void DhtRunner::setLocalCertificateStore(CertificateStoreQuery&& query_method) { @@ -955,35 +944,6 @@ DhtRunner::putEncrypted(InfoHash hash, const PkId& to, std::shared_ptr va cv.notify_all(); } -void -DhtRunner::putEncrypted(InfoHash hash, InfoHash to, std::shared_ptr value, DoneCallback cb, bool permanent) -{ - std::unique_lock 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, InfoHash to, Value&& value, DoneCallback cb, bool permanent) -{ - putEncrypted(hash, to, std::make_shared(std::move(value)), std::move(cb), permanent); -} - -void -DhtRunner::putEncrypted(const std::string& key, InfoHash to, Value&& value, DoneCallback cb, bool permanent) -{ - putEncrypted(InfoHash::get(key), to, std::forward(value), std::move(cb), permanent); -} - void DhtRunner::putEncrypted(InfoHash hash, const std::shared_ptr& to, @@ -1119,25 +1079,6 @@ DhtRunner::connectivityChanged() cv.notify_all(); } -void -DhtRunner::findCertificate(InfoHash hash, std::function&)> cb) -{ - std::unique_lock lck(storage_mtx); - if (running != State::Running) { - lck.unlock(); - cb({}); - return; - } - ongoing_ops++; - pending_ops.emplace([this, hash, cb = std::move(cb)](SecureDht& dht) { - dht.findCertificate(hash, [this, cb = std::move(cb)](const Sp& crt) { - cb(crt); - opEnded(); - }); - }); - cv.notify_all(); -} - void DhtRunner::findCertificate(PkId hash, std::function&)> cb) { diff --git a/src/securedht.cpp b/src/securedht.cpp index 46a39aa9..001809ed 100644 --- a/src/securedht.cpp +++ b/src/securedht.cpp @@ -96,37 +96,13 @@ SecureDht::secureType(ValueType&& type) return type; } -Sp -SecureDht::getCertificate(const InfoHash& node) const -{ - if (node == getId()) - return certificate_; - auto it = nodesCertificates_.find(node); - if (it == nodesCertificates_.end()) - return nullptr; - else - return it->second; -} - -Sp -SecureDht::getPublicKey(const InfoHash& node) const -{ - if (node == getId()) - return certificate_->getSharedPublicKey(); - auto it = nodesPubKeys_.find(node); - if (it == nodesPubKeys_.end()) - return nullptr; - else - return it->second; -} - Sp SecureDht::getCertificate(const PkId& node) const { if (node == getLongId()) return certificate_; - auto it = nodesCertificatesLong_.find(node); - if (it == nodesCertificatesLong_.end()) + auto it = nodesCertificates_.find(node); + if (it == nodesCertificates_.end()) return nullptr; else return it->second; @@ -137,8 +113,8 @@ SecureDht::getPublicKey(const PkId& node) const { if (node == getLongId()) return certificate_->getSharedPublicKey(); - auto it = nodesPubKeysLong_.find(node); - if (it == nodesPubKeysLong_.end()) + auto it = nodesPubKeys_.find(node); + if (it == nodesPubKeys_.end()) return nullptr; else return it->second; @@ -191,85 +167,10 @@ void SecureDht::registerCertificate(const Sp& cert) { if (cert) { - nodesCertificates_[cert->getId()] = cert; - nodesCertificatesLong_[cert->getLongId()] = cert; + nodesCertificates_[cert->getLongId()] = cert; } } -void -SecureDht::findCertificate(const InfoHash& node, const std::function)>& cb) -{ - Sp b = getCertificate(node); - if (b && *b) { - if (logger_) - logger_->debug("Using certificate from cache for {}", node.toString()); - if (cb) - cb(b); - return; - } - if (localQueryMethodLegacy_) { - auto res = localQueryMethodLegacy_(node); - if (not res.empty()) { - if (logger_) - logger_->debug("Registering certificate from local store for {}", node.toString()); - nodesCertificates_.emplace(node, res.front()); - nodesCertificatesLong_.emplace(res.front()->getLongId(), res.front()); - if (cb) - cb(res.front()); - return; - } - } - - auto found = std::make_shared(false); - dht_->get( - node, - [cb, node, found, this](const std::vector>& vals) { - for (const auto& v : vals) { - if (auto cert = registerCertificate(node, v->data)) { - *found = true; - if (logger_) - logger_->debug("Found certificate for {}", node.toString()); - if (cb) - cb(cert); - return false; - } - } - return !*found; - }, - [cb, found](bool) { - if (!*found and cb) - cb(nullptr); - }, - Value::TypeFilter(CERTIFICATE_TYPE)); -} - -void -SecureDht::findPublicKey(const InfoHash& node, const std::function)>& cb) -{ - auto pk = getPublicKey(node); - if (pk && *pk) { - if (logger_) - logger_->debug("Found public key from cache for {}", node.toString()); - if (cb) - cb(pk); - return; - } - findCertificate(node, [=](const Sp& crt) { - if (crt && *crt) { - auto pk = crt->getSharedPublicKey(); - if (*pk) { - nodesPubKeys_[pk->getId()] = pk; - nodesPubKeysLong_[pk->getLongId()] = pk; - if (cb) - cb(pk); - return; - } - } - if (cb) - cb(nullptr); - }); -} - void SecureDht::findCertificate(const PkId& node, const std::function)>& cb) { @@ -287,8 +188,7 @@ SecureDht::findCertificate(const PkId& node, const std::functiondebug("Registering certificate from local store for {}", node.to_c_str()); - nodesCertificates_.emplace(res.front()->getId(), res.front()); - nodesCertificatesLong_.emplace(node, res.front()); + nodesCertificates_.emplace(node, res.front()); if (cb) cb(res.front()); return; @@ -333,8 +233,7 @@ SecureDht::findPublicKey(const PkId& node, const std::functiongetSharedPublicKey(); if (*pk) { - nodesPubKeys_[pk->getId()] = pk; - nodesPubKeysLong_[pk->getLongId()] = pk; + nodesPubKeys_[pk->getLongId()] = pk; if (cb) cb(pk); return; @@ -362,8 +261,7 @@ SecureDht::checkValue(const Sp& v) if (auto decrypted_val = v->decrypt(*key_)) { auto cacheValue = not isDecrypted and decrypted_val->owner; if (cacheValue) { - nodesPubKeys_[decrypted_val->owner->getId()] = decrypted_val->owner; - nodesPubKeysLong_[decrypted_val->owner->getLongId()] = decrypted_val->owner; + nodesPubKeys_[decrypted_val->owner->getLongId()] = decrypted_val->owner; } return decrypted_val; } @@ -377,8 +275,7 @@ SecureDht::checkValue(const Sp& v) auto cacheValue = not v->isSignatureChecked() and enableCache_ and v->owner; if (v->checkSignature()) { if (cacheValue) { - nodesPubKeys_[v->owner->getId()] = v->owner; - nodesPubKeysLong_[v->owner->getLongId()] = v->owner; + nodesPubKeys_[v->owner->getLongId()] = v->owner; } return v; } else if (logger_) @@ -492,35 +389,6 @@ SecureDht::putSigned(const InfoHash& hash, Sp val, DoneCallback callback, Where().id(val->id)); } -void -SecureDht::putEncrypted(const InfoHash& hash, const InfoHash& to, Sp val, DoneCallback callback, bool permanent) -{ - if (not key_) { - if (callback) - callback(false, {}); - return; - } - findPublicKey(to, - [this, hash, val = std::move(val), callback = std::move(callback), permanent]( - const Sp& pk) { - if (!pk || !*pk) { - if (callback) - callback(false, {}); - return; - } - if (logger_) - logger_->warn("Encrypting data for PK: {}", pk->getId().toString()); - try { - dht_->put(hash, encrypt(*val, *pk), callback, time_point::max(), permanent); - } catch (const std::exception& e) { - if (logger_) - logger_->error("Error putting encrypted data: {}", e.what()); - if (callback) - callback(false, {}); - } - }); -} - void SecureDht::putEncrypted(const InfoHash& hash, const PkId& to, Sp val, DoneCallback callback, bool permanent) { diff --git a/tools/dhtnode.cpp b/tools/dhtnode.cpp index 5221c1be..1b714da3 100644 --- a/tools/dhtnode.cpp +++ b/tools/dhtnode.cpp @@ -481,7 +481,7 @@ cmd_loop(std::shared_ptr& node, auto value = std::make_shared(dht::ValueType::USER_DATA.id, std::vector {v.begin(), v.end()}); value->user_type = "text/plain"; - node->putEncrypted(id, InfoHash(tostr), std::move(value), [start](bool ok) { + node->putEncrypted(id, PkId(tostr), std::move(value), [start](bool ok) { auto end = std::chrono::high_resolution_clock::now(); std::cout << "Put encrypted: " << (ok ? "success" : "failure") << " (took " << print_duration(end - start) << ')' << std::endl;