From 7bba7176194c4d1d9c7066f8eda7a94491d924e6 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 30 Nov 2026 21:27:55 +0100 Subject: Cleanups and updates --- server/src/http/ext/hmac_openssl.cppm | 173 +++++++++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 2 deletions(-) (limited to 'server/src/http/ext/hmac_openssl.cppm') diff --git a/server/src/http/ext/hmac_openssl.cppm b/server/src/http/ext/hmac_openssl.cppm index 52ccb86..b196094 100644 --- a/server/src/http/ext/hmac_openssl.cppm +++ b/server/src/http/ext/hmac_openssl.cppm @@ -1,10 +1,179 @@ module; +#include +#include +#include + #include #include -export module routemon:hmac_openssl; +export module routemon:http.ext.hmac_openssl; + +import std; +import :http.core.common; +import :util; + +namespace { -namespace routemon::hmac_openssl { +namespace net = boost::asio; } + +namespace routemon::http::ext::hmac_openssl { + +// TODO: constrain T? What are appropriate constraints? +template +class readable +{ + std::string_view key_; + std::optional> mac_; + T res_; + + template + friend struct body_reader; + +public: + template + explicit readable(std::string_view key, Args&&... args) + : key_{key}, res_{std::forward(args)...} + { + } + + template + constexpr auto get(this Self&& self) -> auto&& + { + return std::forward(self).res_; + } + + auto mac() const -> std::optional> + { + return mac_; + } +}; + +template +class body_reader +{ + EVP_MAC* mac_ = nullptr; + EVP_MAC_CTX* ctx_ = nullptr; + + util::not_null*> v_; + Sub::reader subr_; + beast::error_code ec_; + +public: + template + explicit body_reader( + bhttp::header& h, + readable& v) + : v_{util::not_null{&v}}, + subr_{h, v.get()} // we are not supposed to use v yet, but this just + // computes an address, so it should be fine + { + } + + ~body_reader() + { + ::EVP_MAC_CTX_free(ctx_); + ::EVP_MAC_free(mac_); + } + + // TODO: move to impl. unit. + auto init(boost::optional n, beast::error_code& ec) noexcept + -> void + { + // https://docs.openssl.org/master/man3/EVP_MAC/#examples + mac_ = ::EVP_MAC_fetch(nullptr, "HMAC", nullptr); + if (!mac_) + { + ec_ = ec = make_error_code(std::errc::not_enough_memory); + return; + } + + ctx_ = ::EVP_MAC_CTX_new(mac_); + if (!ctx_) + { + ec_ = ec = make_error_code(std::errc::not_enough_memory); + return; + } + + char sha256[] = "SHA256"; + ::OSSL_PARAM const params[] = { + OSSL_PARAM_utf8_string("digest", sha256, sizeof(sha256) - 1), + OSSL_PARAM_END + }; + if (!::EVP_MAC_init( + ctx_, reinterpret_cast(v_->key_.data()), + v_->key_.size(), params)) + { + ec_ = ec = beast::error_code{ + static_cast(::ERR_get_error()), net::error::get_ssl_category() + }; + return; + } + + subr_.init(n, ec); + } + + auto + put(beast::concepts::const_buffer_sequence auto b, + beast::error_code& ec) noexcept -> std::size_t + { + if ((ec = ec_)) + return 0uz; + + auto const total_read = subr_.put(b, ec); + if (total_read > 0) + { + auto remaining = total_read; + for (auto const buf : beast::buffers_range(b)) + { + auto const to_hash_from_this_buf = std::min(remaining, buf.size()); + if (!::EVP_MAC_update( + ctx_, static_cast(buf.data()), + to_hash_from_this_buf)) + { + ec_ = ec = beast::error_code{ + static_cast(::ERR_get_error()), net::error::get_ssl_category() + }; + return total_read; + } + remaining -= to_hash_from_this_buf; + } + } + return total_read; + } + + auto finish(beast::error_code& ec) noexcept -> void + { + auto got_mac_len = 0uz; + auto got_mac = std::array{}; + if (!::EVP_MAC_final(ctx_, got_mac.data(), &got_mac_len, got_mac.size())) + { + ec = beast::error_code{ + static_cast(::ERR_get_error()), net::error::get_ssl_category() + }; + return; + } + assert(got_mac_len == got_mac.size()); + v_->mac_ = got_mac; + subr_.finish(ec); + } +}; + +template +struct body +{ + using value_type = readable; +}; + +template + requires bhttp::concepts::body_reader +struct body +{ + using value_type = readable; + + using reader = body_reader; +}; + +} // namespace routemon::http::ext::hmac_openssl -- cgit v1.3.1