Skip to content
Snippets Groups Projects
Commit 2fbafc35 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

crypto: cache Certificate Id

parent 19fe3441
No related branches found
No related tags found
No related merge requests found
...@@ -598,6 +598,8 @@ struct OPENDHT_PUBLIC Certificate { ...@@ -598,6 +598,8 @@ struct OPENDHT_PUBLIC Certificate {
private: private:
Certificate(const Certificate&) = delete; Certificate(const Certificate&) = delete;
Certificate& operator=(const Certificate&) = delete; Certificate& operator=(const Certificate&) = delete;
InfoHash cachedId_ {};
PkId cachedLongId_ {};
struct crlNumberCmp { struct crlNumberCmp {
bool operator() (const std::shared_ptr<RevocationList>& lhs, const std::shared_ptr<RevocationList>& rhs) const { bool operator() (const std::shared_ptr<RevocationList>& lhs, const std::shared_ptr<RevocationList>& rhs) const {
......
...@@ -853,12 +853,15 @@ Certificate::getId() const ...@@ -853,12 +853,15 @@ Certificate::getId() const
{ {
if (not cert) if (not cert)
return {}; return {};
if (cachedId_)
return cachedId_;
InfoHash id; InfoHash id;
size_t sz = id.size(); size_t sz = id.size();
if (auto err = gnutls_x509_crt_get_key_id(cert, 0, id.data(), &sz)) 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)); throw CryptoException(std::string("Can't get certificate public key ID: ") + gnutls_strerror(err));
if (sz != id.size()) if (sz != id.size())
throw CryptoException("Can't get certificate public key ID: wrong output length."); throw CryptoException("Can't get certificate public key ID: wrong output length.");
cachedId_ = id;
return id; return id;
} }
...@@ -867,6 +870,8 @@ Certificate::getLongId() const ...@@ -867,6 +870,8 @@ Certificate::getLongId() const
{ {
if (not cert) if (not cert)
return {}; return {};
if (cachedLongId_)
return cachedLongId_;
#if GNUTLS_VERSION_NUMBER < 0x030401 #if GNUTLS_VERSION_NUMBER < 0x030401
throw CryptoException("Can't get certificate 256 bits public key ID: GnuTLS 3.4.1 or higher required."); throw CryptoException("Can't get certificate 256 bits public key ID: GnuTLS 3.4.1 or higher required.");
#else #else
...@@ -876,6 +881,7 @@ Certificate::getLongId() const ...@@ -876,6 +881,7 @@ Certificate::getLongId() const
throw CryptoException(std::string("Can't get certificate 256 bits public key ID: ") + gnutls_strerror(err)); throw CryptoException(std::string("Can't get certificate 256 bits public key ID: ") + gnutls_strerror(err));
if (sz != id.size()) if (sz != id.size())
throw CryptoException("Can't get certificate 256 bits public key ID: wrong output length."); throw CryptoException("Can't get certificate 256 bits public key ID: wrong output length.");
cachedLongId_ = id;
return id; return id;
#endif #endif
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment