From 12a52c78ac0428c5cb437a46e31d9acb9898bdbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Tue, 28 Jun 2022 12:06:57 -0400 Subject: [PATCH] x509: make PrivateKey::getPublicKey() thread-safe --- include/opendht/crypto.h | 2 ++ src/crypto.cpp | 1 + 2 files changed, 3 insertions(+) diff --git a/include/opendht/crypto.h b/include/opendht/crypto.h index 64d43092..15aec880 100644 --- a/include/opendht/crypto.h +++ b/include/opendht/crypto.h @@ -33,6 +33,7 @@ extern "C" { #include <vector> #include <memory> #include <atomic> +#include <mutex> #ifdef _WIN32 #include <iso646.h> @@ -207,6 +208,7 @@ private: PrivateKey& operator=(const PrivateKey&) = delete; Blob decryptBloc(const uint8_t* src, size_t src_size) const; + mutable std::mutex publicKeyMutex_ {}; mutable std::shared_ptr<PublicKey> publicKey_ {}; }; diff --git a/src/crypto.cpp b/src/crypto.cpp index f1b18c15..dba34732 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -361,6 +361,7 @@ PrivateKey::getPublicKey() const const std::shared_ptr<PublicKey>& PrivateKey::getSharedPublicKey() const { + std::lock_guard<std::mutex> lock(publicKeyMutex_); if (not publicKey_) { auto pk = std::make_shared<PublicKey>(); if (auto err = gnutls_pubkey_import_privkey(pk->pk, key, GNUTLS_KEY_KEY_CERT_SIGN | GNUTLS_KEY_CRL_SIGN, 0)) -- GitLab