summaryrefslogtreecommitdiffstats
path: root/server/src/http/ext/hmac_openssl.cppm
diff options
context:
space:
mode:
authorRutger Broekhoff2026-11-30 21:27:55 +0100
committerRutger Broekhoff2026-11-30 21:27:55 +0100
commit7bba7176194c4d1d9c7066f8eda7a94491d924e6 (patch)
treea9a171e8608786e09d6342729ee13b8471c0c9c2 /server/src/http/ext/hmac_openssl.cppm
parent02475e05ccd1aa15c6a6163834f28bfebbef796f (diff)
downloadroutemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.tar.gz
routemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.zip
Cleanups and updates
Diffstat (limited to 'server/src/http/ext/hmac_openssl.cppm')
-rw-r--r--server/src/http/ext/hmac_openssl.cppm173
1 files changed, 171 insertions, 2 deletions
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 @@
1module; 1module;
2 2
3#include <boost/asio/ssl/error.hpp>
4#include <boost/beast/core.hpp>
5#include <boost/beast/http.hpp>
6
3#include <openssl/evp.h> 7#include <openssl/evp.h>
4#include <openssl/params.h> 8#include <openssl/params.h>
5 9
6export module routemon:hmac_openssl; 10export module routemon:http.ext.hmac_openssl;
11
12import std;
13import :http.core.common;
14import :util;
15
16namespace {
7 17
8namespace routemon::hmac_openssl { 18namespace net = boost::asio;
9 19
10} 20}
21
22namespace routemon::http::ext::hmac_openssl {
23
24// TODO: constrain T? What are appropriate constraints?
25template <typename T>
26class readable
27{
28 std::string_view key_;
29 std::optional<std::array<std::uint8_t, 32>> mac_;
30 T res_;
31
32 template <bhttp::concepts::body_reader Sub>
33 friend struct body_reader;
34
35public:
36 template <typename... Args>
37 explicit readable(std::string_view key, Args&&... args)
38 : key_{key}, res_{std::forward<Args>(args)...}
39 {
40 }
41
42 template <typename Self>
43 constexpr auto get(this Self&& self) -> auto&&
44 {
45 return std::forward<Self>(self).res_;
46 }
47
48 auto mac() const -> std::optional<std::array<std::uint8_t, 32>>
49 {
50 return mac_;
51 }
52};
53
54template <bhttp::concepts::body_reader Sub>
55class body_reader
56{
57 EVP_MAC* mac_ = nullptr;
58 EVP_MAC_CTX* ctx_ = nullptr;
59
60 util::not_null<readable<typename Sub::value_type>*> v_;
61 Sub::reader subr_;
62 beast::error_code ec_;
63
64public:
65 template <bool isRequest, bhttp::concepts::fields Fields>
66 explicit body_reader(
67 bhttp::header<isRequest, Fields>& h,
68 readable<typename Sub::value_type>& v)
69 : v_{util::not_null{&v}},
70 subr_{h, v.get()} // we are not supposed to use v yet, but this just
71 // computes an address, so it should be fine
72 {
73 }
74
75 ~body_reader()
76 {
77 ::EVP_MAC_CTX_free(ctx_);
78 ::EVP_MAC_free(mac_);
79 }
80
81 // TODO: move to impl. unit.
82 auto init(boost::optional<std::uint64_t> n, beast::error_code& ec) noexcept
83 -> void
84 {
85 // https://docs.openssl.org/master/man3/EVP_MAC/#examples
86 mac_ = ::EVP_MAC_fetch(nullptr, "HMAC", nullptr);
87 if (!mac_)
88 {
89 ec_ = ec = make_error_code(std::errc::not_enough_memory);
90 return;
91 }
92
93 ctx_ = ::EVP_MAC_CTX_new(mac_);
94 if (!ctx_)
95 {
96 ec_ = ec = make_error_code(std::errc::not_enough_memory);
97 return;
98 }
99
100 char sha256[] = "SHA256";
101 ::OSSL_PARAM const params[] = {
102 OSSL_PARAM_utf8_string("digest", sha256, sizeof(sha256) - 1),
103 OSSL_PARAM_END
104 };
105 if (!::EVP_MAC_init(
106 ctx_, reinterpret_cast<unsigned char const*>(v_->key_.data()),
107 v_->key_.size(), params))
108 {
109 ec_ = ec = beast::error_code{
110 static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()
111 };
112 return;
113 }
114
115 subr_.init(n, ec);
116 }
117
118 auto
119 put(beast::concepts::const_buffer_sequence auto b,
120 beast::error_code& ec) noexcept -> std::size_t
121 {
122 if ((ec = ec_))
123 return 0uz;
124
125 auto const total_read = subr_.put(b, ec);
126 if (total_read > 0)
127 {
128 auto remaining = total_read;
129 for (auto const buf : beast::buffers_range(b))
130 {
131 auto const to_hash_from_this_buf = std::min(remaining, buf.size());
132 if (!::EVP_MAC_update(
133 ctx_, static_cast<unsigned char const*>(buf.data()),
134 to_hash_from_this_buf))
135 {
136 ec_ = ec = beast::error_code{
137 static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()
138 };
139 return total_read;
140 }
141 remaining -= to_hash_from_this_buf;
142 }
143 }
144 return total_read;
145 }
146
147 auto finish(beast::error_code& ec) noexcept -> void
148 {
149 auto got_mac_len = 0uz;
150 auto got_mac = std::array<std::uint8_t, 32>{};
151 if (!::EVP_MAC_final(ctx_, got_mac.data(), &got_mac_len, got_mac.size()))
152 {
153 ec = beast::error_code{
154 static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()
155 };
156 return;
157 }
158 assert(got_mac_len == got_mac.size());
159 v_->mac_ = got_mac;
160 subr_.finish(ec);
161 }
162};
163
164template <bhttp::concepts::body Sub>
165struct body
166{
167 using value_type = readable<typename Sub::value_type>;
168};
169
170template <bhttp::concepts::body Sub>
171 requires bhttp::concepts::body_reader<Sub>
172struct body<Sub>
173{
174 using value_type = readable<typename Sub::value_type>;
175
176 using reader = body_reader<Sub>;
177};
178
179} // namespace routemon::http::ext::hmac_openssl