fix move semantics of Ref

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

View File

@ -22,13 +22,13 @@ using namespace std::literals;
static auto start(boost::asio::io_context &io, const Settings &settings) -> void static auto start(boost::asio::io_context &io, const Settings &settings) -> void
{ {
X509_Ref cert; Ref<X509> cert;
if (settings.use_tls && not settings.tls_certfile.empty()) if (settings.use_tls && not settings.tls_certfile.empty())
{ {
cert = cert_from_file(settings.tls_certfile); cert = cert_from_file(settings.tls_certfile);
} }
EVP_PKEY_Ref key; Ref<EVP_PKEY> key;
if (settings.use_tls && not settings.tls_keyfile.empty()) if (settings.use_tls && not settings.tls_keyfile.empty())
{ {
key = key_from_file(settings.tls_keyfile, ""); key = key_from_file(settings.tls_keyfile, "");

View File

@ -12,7 +12,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
Challenge::Challenge(EVP_PKEY_Ref key, Connection & connection) Challenge::Challenge(Ref<EVP_PKEY> key, Connection & connection)
: key_{std::move(key)} : key_{std::move(key)}
, connection_{connection} , connection_{connection}
{} {}
@ -39,7 +39,7 @@ auto Challenge::on_ircmsg(IrcCommand cmd, const IrcMsg &msg) -> void {
auto Challenge::finish_challenge() -> void auto Challenge::finish_challenge() -> void
{ {
EVP_PKEY_CTX_Ref ctx; Ref<EVP_PKEY_CTX> ctx;
unsigned int digestlen = EVP_MAX_MD_SIZE; unsigned int digestlen = EVP_MAX_MD_SIZE;
unsigned char digest[EVP_MAX_MD_SIZE]; unsigned char digest[EVP_MAX_MD_SIZE];
size_t len = mybase64::decoded_size(buffer_.size()); size_t len = mybase64::decoded_size(buffer_.size());
@ -83,7 +83,7 @@ auto Challenge::finish_challenge() -> void
buffer_.clear(); buffer_.clear();
} }
auto Challenge::start(Connection &connection, const std::string_view user, EVP_PKEY_Ref ref) -> std::shared_ptr<Challenge> auto Challenge::start(Connection &connection, const std::string_view user, Ref<EVP_PKEY> ref) -> std::shared_ptr<Challenge>
{ {
auto self = std::make_shared<Challenge>(std::move(ref), connection); auto self = std::make_shared<Challenge>(std::move(ref), connection);
self->slot_ = connection.sig_ircmsg.connect([self](auto cmd, auto &msg) { self->on_ircmsg(cmd, msg); }); self->slot_ = connection.sig_ircmsg.connect([self](auto cmd, auto &msg) { self->on_ircmsg(cmd, msg); });

View File

@ -11,7 +11,7 @@
/// @brief Implements the CHALLENGE command protocol to identify as an operator. /// @brief Implements the CHALLENGE command protocol to identify as an operator.
class Challenge : std::enable_shared_from_this<Challenge> class Challenge : std::enable_shared_from_this<Challenge>
{ {
EVP_PKEY_Ref key_; Ref<EVP_PKEY> key_;
Connection &connection_; Connection &connection_;
boost::signals2::scoped_connection slot_; boost::signals2::scoped_connection slot_;
std::string buffer_; std::string buffer_;
@ -20,12 +20,12 @@ class Challenge : std::enable_shared_from_this<Challenge>
auto finish_challenge() -> void; auto finish_challenge() -> void;
public: public:
Challenge(EVP_PKEY_Ref, Connection &); Challenge(Ref<EVP_PKEY>, Connection &);
/// @brief Starts the CHALLENGE protocol. /// @brief Starts the CHALLENGE protocol.
/// @param connection Registered connection. /// @param connection Registered connection.
/// @param user Operator username /// @param user Operator username
/// @param key Operator private RSA key /// @param key Operator private RSA key
/// @return Handle to the challenge object. /// @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::string host;
std::uint16_t port; std::uint16_t port;
X509_Ref client_cert; Ref<X509> client_cert;
EVP_PKEY_Ref client_key; Ref<EVP_PKEY> client_key;
std::string verify; std::string verify;
std::string sni; std::string sni;

View File

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

View File

@ -5,6 +5,8 @@
#include <memory> #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 <typename> struct RefTraits {};
template <> struct RefTraits<EVP_PKEY> { template <> struct RefTraits<EVP_PKEY> {
@ -19,6 +21,7 @@ template <> struct RefTraits<X509> {
template <> struct RefTraits<EVP_PKEY_CTX> { template <> struct RefTraits<EVP_PKEY_CTX> {
static constexpr void (*Free)(EVP_PKEY_CTX*) = EVP_PKEY_CTX_free; static constexpr void (*Free)(EVP_PKEY_CTX*) = EVP_PKEY_CTX_free;
// this type does not implement UpRef
}; };
template <typename T> template <typename T>
@ -27,20 +30,27 @@ struct FnDeleter {
}; };
template <typename T> template <typename T>
struct Ref : std::unique_ptr<T, FnDeleter<T>> { struct Ref : std::unique_ptr<T, FnDeleter<T>>
using std::unique_ptr<T, FnDeleter<T>>::unique_ptr; {
Ref(const Ref &ref) { 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; *this = ref;
} }
Ref &operator=(const Ref &ref) {
Ref &operator=(Ref&&) noexcept = default;
Ref &operator=(const Ref &ref) noexcept {
if (ref) { if (ref) {
RefTraits<T>::UpRef(ref.get()); RefTraits<T>::UpRef(ref.get());
this->reset(ref.get());
} }
this->reset(ref.get());
return *this; 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_authcid;
std::string sasl_authzid; std::string sasl_authzid;
std::string sasl_password; std::string sasl_password;
EVP_PKEY_Ref sasl_key; Ref<EVP_PKEY> sasl_key;
}; };
private: private:

View File

@ -20,9 +20,9 @@ auto log_openssl_errors(const std::string_view prefix) -> void
ERR_print_errors_cb(CCallback<decltype(err_cb)>::invoke, &err_cb); ERR_print_errors_cb(CCallback<decltype(err_cb)>::invoke, &err_cb);
} }
auto cert_from_file(const std::string &filename) -> X509_Ref auto cert_from_file(const std::string &filename) -> Ref<X509>
{ {
X509_Ref cert; Ref<X509> cert;
if (const auto fp = fopen(filename.c_str(), "r")) if (const auto fp = fopen(filename.c_str(), "r"))
{ {
cert.reset(PEM_read_X509(fp, nullptr, nullptr, nullptr)); 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; 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>
{ {
EVP_PKEY_Ref key; Ref<EVP_PKEY> key;
if (const auto fp = fopen(filename.c_str(), "r")) if (const auto fp = fopen(filename.c_str(), "r"))
{ {
auto cb = [password](char * const buf, int const size, int) -> int { auto cb = [password](char * const buf, int const size, int) -> int {