From 20ab71aba61e96d21e56636d2a1f569231abc072 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Thu, 5 Apr 2018 14:23:15 -0400
Subject: [PATCH] securedht: check public key

---
 include/opendht/securedht.h |  2 --
 src/securedht.cpp           | 14 +++++++++-----
 2 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/include/opendht/securedht.h b/include/opendht/securedht.h
index 287dca53..f5382708 100644
--- a/include/opendht/securedht.h
+++ b/include/opendht/securedht.h
@@ -346,8 +346,6 @@ private:
     std::map<InfoHash, Sp<crypto::Certificate>> nodesCertificates_ {};
     std::map<InfoHash, Sp<const crypto::PublicKey>> nodesPubKeys_ {};
 
-    std::uniform_int_distribution<Value::Id> rand_id {};
-
     std::atomic_bool forward_all_ {false};
 };
 
diff --git a/src/securedht.cpp b/src/securedht.cpp
index 3b67abdb..2fa3254f 100644
--- a/src/securedht.cpp
+++ b/src/securedht.cpp
@@ -213,11 +213,13 @@ SecureDht::findPublicKey(const InfoHash& node, std::function<void(const Sp<const
     findCertificate(node, [=](const Sp<crypto::Certificate> crt) {
         if (crt && *crt) {
             auto pk = std::make_shared<crypto::PublicKey>(crt->getPublicKey());
-            nodesPubKeys_[pk->getId()] = pk;
-            if (cb) cb(pk);
-        } else {
-            if (cb) cb(nullptr);
+            if (*pk) {
+                nodesPubKeys_[pk->getId()] = pk;
+                if (cb) cb(pk);
+                return;
+            }
         }
+        if (cb) cb(nullptr);
     });
 }
 
@@ -236,7 +238,8 @@ SecureDht::checkValue(const Sp<Value>& v)
         try {
             Value decrypted_val (decrypt(*v));
             if (decrypted_val.recipient == getId()) {
-                nodesPubKeys_[decrypted_val.owner->getId()] = decrypted_val.owner;
+                if (decrypted_val.owner)
+                    nodesPubKeys_[decrypted_val.owner->getId()] = decrypted_val.owner;
                 return std::make_shared<Value>(std::move(decrypted_val));
             }
             // Ignore values belonging to other people
@@ -317,6 +320,7 @@ SecureDht::putSigned(const InfoHash& hash, Sp<Value> val, DoneCallback callback,
 {
     if (val->id == Value::INVALID_ID) {
         crypto::random_device rdev;
+        std::uniform_int_distribution<Value::Id> rand_id;
         val->id = rand_id(rdev);
     }
 
-- 
GitLab