#include "myirc/openssl_utils.hpp" #include "myirc/c_callback.hpp" #include #include #include #include using namespace std::literals; namespace myirc { auto log_openssl_errors(const std::string_view prefix) -> void { auto err_cb = [prefix](const char *str, size_t len) -> int { BOOST_LOG_TRIVIAL(error) << prefix << std::string_view{str, len}; return 0; }; ERR_print_errors_cb(CCallback::invoke, &err_cb); } auto cert_from_file(const std::string &filename) -> Ref { Ref cert; if (const auto fp = fopen(filename.c_str(), "r")) { cert.reset(PEM_read_X509(fp, nullptr, nullptr, nullptr)); if (cert.get() == nullptr) { log_openssl_errors("Reading certificate: "sv); } fclose(fp); } else { const auto err = strerror(errno); BOOST_LOG_TRIVIAL(error) << "Opening certificate: " << err; } return cert; } auto key_from_file(const std::string &filename, const std::string_view password) -> Ref { Ref key; if (const auto fp = fopen(filename.c_str(), "r")) { auto cb = [password](char * const buf, int const size, int) -> int { if (std::cmp_less(size, password.size())) { return -1; } std::copy(password.begin(), password.end(), buf); return static_cast(password.size()); }; key.reset(PEM_read_PrivateKey(fp, nullptr, CCallback::invoke, &cb)); if (key.get() == nullptr) { log_openssl_errors("Reading private key: "sv); } fclose(fp); } else { const auto err = strerror(errno); BOOST_LOG_TRIVIAL(error) << "Opening private key: " << err; } return key; } } // namespace myirc