From 2fbafc3591ca1c3dbff3b43ba05ec05799a30d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Thu, 16 Sep 2021 11:08:02 -0400 Subject: [PATCH] crypto: cache Certificate Id --- include/opendht/crypto.h | 2 ++ src/crypto.cpp | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/include/opendht/crypto.h b/include/opendht/crypto.h index 8960f37a..a3c9aa7b 100644 --- a/include/opendht/crypto.h +++ b/include/opendht/crypto.h @@ -598,6 +598,8 @@ struct OPENDHT_PUBLIC Certificate { private: Certificate(const Certificate&) = delete; Certificate& operator=(const Certificate&) = delete; + InfoHash cachedId_ {}; + PkId cachedLongId_ {}; struct crlNumberCmp { bool operator() (const std::shared_ptr<RevocationList>& lhs, const std::shared_ptr<RevocationList>& rhs) const { diff --git a/src/crypto.cpp b/src/crypto.cpp index 80dbbbdc..e5c7f83d 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -853,12 +853,15 @@ Certificate::getId() const { if (not cert) return {}; + if (cachedId_) + return cachedId_; InfoHash id; size_t sz = id.size(); if (auto err = gnutls_x509_crt_get_key_id(cert, 0, id.data(), &sz)) throw CryptoException(std::string("Can't get certificate public key ID: ") + gnutls_strerror(err)); if (sz != id.size()) throw CryptoException("Can't get certificate public key ID: wrong output length."); + cachedId_ = id; return id; } @@ -867,6 +870,8 @@ Certificate::getLongId() const { if (not cert) return {}; + if (cachedLongId_) + return cachedLongId_; #if GNUTLS_VERSION_NUMBER < 0x030401 throw CryptoException("Can't get certificate 256 bits public key ID: GnuTLS 3.4.1 or higher required."); #else @@ -876,6 +881,7 @@ Certificate::getLongId() const throw CryptoException(std::string("Can't get certificate 256 bits public key ID: ") + gnutls_strerror(err)); if (sz != id.size()) throw CryptoException("Can't get certificate 256 bits public key ID: wrong output length."); + cachedLongId_ = id; return id; #endif } -- GitLab