From c3650ba38ddd4adf969bd2767a821d742d4c6553 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Thu, 30 Jan 2025 11:47:26 -0800 Subject: [PATCH] fix move semantics of Ref --- driver/main.cpp | 4 ++-- myirc/challenge.cpp | 6 +++--- myirc/include/challenge.hpp | 6 +++--- myirc/include/connection.hpp | 4 ++-- myirc/include/openssl_utils.hpp | 4 ++-- myirc/include/ref.hpp | 28 +++++++++++++++++++--------- myirc/include/registration.hpp | 2 +- myirc/openssl_utils.cpp | 8 ++++---- 8 files changed, 36 insertions(+), 26 deletions(-) diff --git a/driver/main.cpp b/driver/main.cpp index ecaa686..569dad8 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -22,13 +22,13 @@ using namespace std::literals; static auto start(boost::asio::io_context &io, const Settings &settings) -> void { - X509_Ref cert; + Ref cert; if (settings.use_tls && not settings.tls_certfile.empty()) { cert = cert_from_file(settings.tls_certfile); } - EVP_PKEY_Ref key; + Ref key; if (settings.use_tls && not settings.tls_keyfile.empty()) { key = key_from_file(settings.tls_keyfile, ""); diff --git a/myirc/challenge.cpp b/myirc/challenge.cpp index 88aef6d..c03f618 100644 --- a/myirc/challenge.cpp +++ b/myirc/challenge.cpp @@ -12,7 +12,7 @@ #include #include -Challenge::Challenge(EVP_PKEY_Ref key, Connection & connection) +Challenge::Challenge(Ref key, Connection & connection) : key_{std::move(key)} , connection_{connection} {} @@ -39,7 +39,7 @@ auto Challenge::on_ircmsg(IrcCommand cmd, const IrcMsg &msg) -> void { auto Challenge::finish_challenge() -> void { - EVP_PKEY_CTX_Ref ctx; + Ref ctx; unsigned int digestlen = EVP_MAX_MD_SIZE; unsigned char digest[EVP_MAX_MD_SIZE]; size_t len = mybase64::decoded_size(buffer_.size()); @@ -83,7 +83,7 @@ auto Challenge::finish_challenge() -> void buffer_.clear(); } -auto Challenge::start(Connection &connection, const std::string_view user, EVP_PKEY_Ref ref) -> std::shared_ptr +auto Challenge::start(Connection &connection, const std::string_view user, Ref ref) -> std::shared_ptr { auto self = std::make_shared(std::move(ref), connection); self->slot_ = connection.sig_ircmsg.connect([self](auto cmd, auto &msg) { self->on_ircmsg(cmd, msg); }); diff --git a/myirc/include/challenge.hpp b/myirc/include/challenge.hpp index 19c7458..8d73ddc 100644 --- a/myirc/include/challenge.hpp +++ b/myirc/include/challenge.hpp @@ -11,7 +11,7 @@ /// @brief Implements the CHALLENGE command protocol to identify as an operator. class Challenge : std::enable_shared_from_this { - EVP_PKEY_Ref key_; + Ref key_; Connection &connection_; boost::signals2::scoped_connection slot_; std::string buffer_; @@ -20,12 +20,12 @@ class Challenge : std::enable_shared_from_this auto finish_challenge() -> void; public: - Challenge(EVP_PKEY_Ref, Connection &); + Challenge(Ref, 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; + static auto start(Connection &, std::string_view user, Ref key) -> std::shared_ptr; }; diff --git a/myirc/include/connection.hpp b/myirc/include/connection.hpp index 3dde320..fb8d35a 100644 --- a/myirc/include/connection.hpp +++ b/myirc/include/connection.hpp @@ -22,8 +22,8 @@ public: std::string host; std::uint16_t port; - X509_Ref client_cert; - EVP_PKEY_Ref client_key; + Ref client_cert; + Ref client_key; std::string verify; std::string sni; diff --git a/myirc/include/openssl_utils.hpp b/myirc/include/openssl_utils.hpp index 65c5d01..35a7551 100644 --- a/myirc/include/openssl_utils.hpp +++ b/myirc/include/openssl_utils.hpp @@ -5,5 +5,5 @@ #include 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; +auto cert_from_file(const std::string &filename) -> Ref; diff --git a/myirc/include/ref.hpp b/myirc/include/ref.hpp index 917fd79..9eeb307 100644 --- a/myirc/include/ref.hpp +++ b/myirc/include/ref.hpp @@ -5,6 +5,8 @@ #include +// Specializations must Free to release a reference +// Specializations can implement UpRef to increase a reference count on copy template struct RefTraits {}; template <> struct RefTraits { @@ -19,6 +21,7 @@ template <> struct RefTraits { template <> struct RefTraits { static constexpr void (*Free)(EVP_PKEY_CTX*) = EVP_PKEY_CTX_free; + // this type does not implement UpRef }; template @@ -27,20 +30,27 @@ struct FnDeleter { }; template -struct Ref : std::unique_ptr> { - using std::unique_ptr>::unique_ptr; - Ref(const Ref &ref) { +struct Ref : std::unique_ptr> +{ + using base = std::unique_ptr>; + + /// 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::UpRef(ref.get()); - this->reset(ref.get()); } + this->reset(ref.get()); return *this; } }; - -using EVP_PKEY_CTX_Ref = Ref; -using X509_Ref = Ref; -using EVP_PKEY_Ref = Ref; diff --git a/myirc/include/registration.hpp b/myirc/include/registration.hpp index 8f8af1f..78935f7 100644 --- a/myirc/include/registration.hpp +++ b/myirc/include/registration.hpp @@ -21,7 +21,7 @@ public: std::string sasl_authcid; std::string sasl_authzid; std::string sasl_password; - EVP_PKEY_Ref sasl_key; + Ref sasl_key; }; private: diff --git a/myirc/openssl_utils.cpp b/myirc/openssl_utils.cpp index 3dee2c9..d656e9a 100644 --- a/myirc/openssl_utils.cpp +++ b/myirc/openssl_utils.cpp @@ -20,9 +20,9 @@ auto log_openssl_errors(const std::string_view prefix) -> void ERR_print_errors_cb(CCallback::invoke, &err_cb); } -auto cert_from_file(const std::string &filename) -> X509_Ref +auto cert_from_file(const std::string &filename) -> Ref { - X509_Ref cert; + Ref cert; if (const auto fp = fopen(filename.c_str(), "r")) { cert.reset(PEM_read_X509(fp, nullptr, nullptr, nullptr)); @@ -40,9 +40,9 @@ auto cert_from_file(const std::string &filename) -> X509_Ref return cert; } -auto key_from_file(const std::string &filename, const std::string_view password) -> EVP_PKEY_Ref +auto key_from_file(const std::string &filename, const std::string_view password) -> Ref { - EVP_PKEY_Ref key; + Ref key; if (const auto fp = fopen(filename.c_str(), "r")) { auto cb = [password](char * const buf, int const size, int) -> int {