From d16260eca8ef529d843f58d5e683df339d5ff6aa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Sat, 4 Jul 2015 19:12:42 -0400
Subject: [PATCH] crypto: unpack certificate list from blob vector

---
 include/opendht/crypto.h | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/include/opendht/crypto.h b/include/opendht/crypto.h
index 067f28fa..9736cfe7 100644
--- a/include/opendht/crypto.h
+++ b/include/opendht/crypto.h
@@ -169,6 +169,23 @@ struct Certificate : public Serializable {
     void pack(Blob& b) const override;
     void unpack(Blob::const_iterator& begin, Blob::const_iterator& end) override;
 
+    template<typename Iterator>
+    void unpack(const Iterator& begin, const Iterator& end)
+    {
+        std::shared_ptr<Certificate> tmp_subject {};
+        std::shared_ptr<Certificate> first {};
+        for (Iterator icrt = begin; icrt < end; ++icrt) {
+            auto tmp_crt = std::make_shared<Certificate>(*icrt);
+            if (tmp_subject)
+                tmp_subject->issuer = tmp_crt;
+            tmp_subject = std::move(tmp_crt);
+            if (!first)
+                first = tmp_subject;
+        }
+        *this = first ? std::move(*first) : Certificate();
+    }
+
+
     /**
      * Import certificate chain (PEM or DER),
      * ordered from subject to issuer
@@ -177,6 +194,7 @@ struct Certificate : public Serializable {
     void unpack(const std::vector<std::pair<Iterator, Iterator>>& certs)
     {
         std::shared_ptr<Certificate> tmp_issuer;
+        // reverse iteration
         for (auto li = certs.rbegin(); li != certs.rend(); ++li) {
             Certificate tmp_crt;
             gnutls_x509_crt_init(&tmp_crt.cert);
@@ -189,8 +207,7 @@ struct Certificate : public Serializable {
             tmp_crt.issuer = tmp_issuer;
             tmp_issuer = std::make_shared<Certificate>(std::move(tmp_crt));
         }
-        if (tmp_issuer)
-            *this = std::move(*tmp_issuer);
+        *this = tmp_issuer ? std::move(*tmp_issuer) : Certificate();
     }
 
     operator bool() const { return cert; }
-- 
GitLab