fix move semantics of Ref

This commit is contained in:
2025-01-30 11:47:26 -08:00
parent 281937e2c5
commit c3650ba38d
8 changed files with 36 additions and 26 deletions

View File

@@ -11,7 +11,7 @@
/// @brief Implements the CHALLENGE command protocol to identify as an operator.
class Challenge : std::enable_shared_from_this<Challenge>
{
EVP_PKEY_Ref key_;
Ref<EVP_PKEY> key_;
Connection &connection_;
boost::signals2::scoped_connection slot_;
std::string buffer_;
@@ -20,12 +20,12 @@ class Challenge : std::enable_shared_from_this<Challenge>
auto finish_challenge() -> void;
public:
Challenge(EVP_PKEY_Ref, Connection &);
Challenge(Ref<EVP_PKEY>, Connection &);
/// @brief Starts the CHALLENGE protocol.
/// @param connection Registered connection.
/// @param user Operator username
/// @param key Operator private RSA key
/// @return Handle to the challenge object.
static auto start(Connection &, std::string_view user, EVP_PKEY_Ref key) -> std::shared_ptr<Challenge>;
static auto start(Connection &, std::string_view user, Ref<EVP_PKEY> key) -> std::shared_ptr<Challenge>;
};

View File

@@ -22,8 +22,8 @@ public:
std::string host;
std::uint16_t port;
X509_Ref client_cert;
EVP_PKEY_Ref client_key;
Ref<X509> client_cert;
Ref<EVP_PKEY> client_key;
std::string verify;
std::string sni;

View File

@@ -5,5 +5,5 @@
#include <string_view>
auto log_openssl_errors(const std::string_view prefix) -> void;
auto key_from_file(const std::string &filename, const std::string_view password) -> EVP_PKEY_Ref;
auto cert_from_file(const std::string &filename) -> X509_Ref;
auto key_from_file(const std::string &filename, const std::string_view password) -> Ref<EVP_PKEY>;
auto cert_from_file(const std::string &filename) -> Ref<X509>;

View File

@@ -5,6 +5,8 @@
#include <memory>
// Specializations must Free to release a reference
// Specializations can implement UpRef to increase a reference count on copy
template <typename> struct RefTraits {};
template <> struct RefTraits<EVP_PKEY> {
@@ -19,6 +21,7 @@ template <> struct RefTraits<X509> {
template <> struct RefTraits<EVP_PKEY_CTX> {
static constexpr void (*Free)(EVP_PKEY_CTX*) = EVP_PKEY_CTX_free;
// this type does not implement UpRef
};
template <typename T>
@@ -27,20 +30,27 @@ struct FnDeleter {
};
template <typename T>
struct Ref : std::unique_ptr<T, FnDeleter<T>> {
using std::unique_ptr<T, FnDeleter<T>>::unique_ptr;
Ref(const Ref &ref) {
struct Ref : std::unique_ptr<T, FnDeleter<T>>
{
using base = std::unique_ptr<T, FnDeleter<T>>;
/// Owns nothing
Ref() noexcept = default;
/// Takes ownership of the pointer
explicit Ref(T *x) noexcept : base{x} {}
Ref(Ref &&ref) noexcept = default;
Ref(const Ref &ref) noexcept {
*this = ref;
}
Ref &operator=(const Ref &ref) {
Ref &operator=(Ref&&) noexcept = default;
Ref &operator=(const Ref &ref) noexcept {
if (ref) {
RefTraits<T>::UpRef(ref.get());
this->reset(ref.get());
}
this->reset(ref.get());
return *this;
}
};
using EVP_PKEY_CTX_Ref = Ref<EVP_PKEY_CTX>;
using X509_Ref = Ref<X509>;
using EVP_PKEY_Ref = Ref<EVP_PKEY>;

View File

@@ -21,7 +21,7 @@ public:
std::string sasl_authcid;
std::string sasl_authzid;
std::string sasl_password;
EVP_PKEY_Ref sasl_key;
Ref<EVP_PKEY> sasl_key;
};
private: