1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
module;
#include <boost/asio/ssl/error.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <openssl/evp.h>
#include <openssl/params.h>
export module routemon:http.ext.hmac_openssl;
import std;
import :http.core.common;
import :util;
namespace {
namespace net = boost::asio;
}
namespace routemon::http::ext::hmac_openssl {
// TODO: constrain T? What are appropriate constraints?
template <typename T>
class readable
{
std::string_view key_;
std::optional<std::array<std::uint8_t, 32>> mac_;
T res_;
template <bhttp::concepts::body_reader Sub>
friend struct body_reader;
public:
template <typename... Args>
explicit readable(std::string_view key, Args&&... args)
: key_{key}, res_{std::forward<Args>(args)...}
{
}
template <typename Self>
constexpr auto get(this Self&& self) -> auto&&
{
return std::forward<Self>(self).res_;
}
auto mac() const -> std::optional<std::array<std::uint8_t, 32>>
{
return mac_;
}
};
template <bhttp::concepts::body_reader Sub>
class body_reader
{
EVP_MAC* mac_ = nullptr;
EVP_MAC_CTX* ctx_ = nullptr;
util::not_null<readable<typename Sub::value_type>*> v_;
Sub::reader subr_;
beast::error_code ec_;
public:
template <bool isRequest, bhttp::concepts::fields Fields>
explicit body_reader(
bhttp::header<isRequest, Fields>& h,
readable<typename Sub::value_type>& 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<std::uint64_t> 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<unsigned char const*>(v_->key_.data()),
v_->key_.size(), params))
{
ec_ = ec = beast::error_code{
static_cast<int>(::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<unsigned char const*>(buf.data()),
to_hash_from_this_buf))
{
ec_ = ec = beast::error_code{
static_cast<int>(::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<std::uint8_t, 32>{};
if (!::EVP_MAC_final(ctx_, got_mac.data(), &got_mac_len, got_mac.size()))
{
ec = beast::error_code{
static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()
};
return;
}
assert(got_mac_len == got_mac.size());
v_->mac_ = got_mac;
subr_.finish(ec);
}
};
template <bhttp::concepts::body Sub>
struct body
{
using value_type = readable<typename Sub::value_type>;
};
template <bhttp::concepts::body Sub>
requires bhttp::concepts::body_reader<Sub>
struct body<Sub>
{
using value_type = readable<typename Sub::value_type>;
using reader = body_reader<Sub>;
};
} // namespace routemon::http::ext::hmac_openssl
|