-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauth_common.cpp
More file actions
59 lines (47 loc) · 1.57 KB
/
Copy pathauth_common.cpp
File metadata and controls
59 lines (47 loc) · 1.57 KB
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
#include "auth_common.hpp"
#include <oxenc/base64.h>
#include <oxen/log/format.hpp>
namespace spns::notifier {
using namespace oxen::log::literals;
void privkey_deleter::operator()(gnutls_privkey_t priv) const noexcept {
gnutls_privkey_deinit(priv);
}
privkey_ptr load_privkey(std::string_view content) {
gnutls_global_init();
gnutls_datum_t pem_data{
.data = const_cast<unsigned char*>(
reinterpret_cast<const unsigned char*>(content.data())),
.size = static_cast<unsigned int>(content.size())};
gnutls_x509_privkey_t x_priv;
gnutls_x509_privkey_init(&x_priv);
int ret = gnutls_x509_privkey_import(x_priv, &pem_data, GNUTLS_X509_FMT_PEM);
if (ret < 0) {
gnutls_x509_privkey_deinit(x_priv);
throw std::invalid_argument{
"Invalid auth data: did not find a valid PEM private key: {}"_format(
gnutls_strerror(ret))};
}
privkey_ptr priv;
{
gnutls_privkey_t p;
gnutls_privkey_init(&p);
priv.reset(p);
}
ret = gnutls_privkey_import_x509(priv.get(), x_priv, 0);
if (ret < 0) {
gnutls_x509_privkey_deinit(x_priv);
throw std::invalid_argument{
"Invalid private key: import failed: {}"_format(gnutls_strerror(ret))};
}
return priv;
}
std::string b64_url(std::string_view in) {
auto out = oxenc::to_base64_unpadded(in);
for (auto& c : out)
if (c == '+')
c = '-';
else if (c == '/')
c = '_';
return out;
}
} // namespace spns::notifier