-
Notifications
You must be signed in to change notification settings - Fork 3
/
OpenARC.cpp
197 lines (160 loc) · 5.59 KB
/
OpenARC.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#define _Bool bool
#include "OpenARC.hpp"
#include <regex>
#include <stdbool.h> // needs to be above <openarc/arc.h>
#include <openarc/arc.h>
#include "iobuffer.hpp"
#include <fmt/format.h>
#include <fmt/ostream.h>
#include <glog/logging.h>
namespace {
u_char* uc(char const* cp)
{
return reinterpret_cast<u_char*>(const_cast<char*>(cp));
}
char const* c(u_char* ucp) { return reinterpret_cast<char const*>(ucp); }
} // namespace
OpenARC::lib::lib()
{
arc_ = arc_init();
CHECK_NOTNULL(arc_);
uint32_t arcl_flags;
get_option(ARC_OPTS_FLAGS, &arcl_flags, sizeof(arcl_flags));
arcl_flags |= ARC_LIBFLAGS_FIXCRLF;
set_option(ARC_OPTS_FLAGS, &arcl_flags, sizeof(arcl_flags));
char const* signhdrs[] = {"cc",
"content-language",
"content-transfer-encoding",
"content-type",
"date",
"feedback-id",
"from",
"in-reply-to",
"list-archive",
"list-help",
"list-id",
"list-owner",
"list-post",
"list-subscribe",
"list-unsubscribe",
"message-id",
"mime-version",
"precedence",
"references",
"reply-to",
"resent-cc",
"resent-date",
"resent-from",
"resent-to",
"subject",
"to",
nullptr};
set_option(ARC_OPTS_SIGNHDRS, signhdrs, sizeof(char**));
char const* oversignhdrs[] = {"from", nullptr};
set_option(ARC_OPTS_OVERSIGNHDRS, oversignhdrs, sizeof(char**));
}
OpenARC::lib::~lib()
{
set_option(ARC_OPTS_SIGNHDRS, nullptr, sizeof(char**));
set_option(ARC_OPTS_OVERSIGNHDRS, nullptr, sizeof(char**));
arc_close(arc_);
}
void OpenARC::lib::get_option(int arg, void* val, size_t valsz)
{
CHECK_EQ(arc_options(arc_, ARC_OP_GETOPT, arg, val, valsz), ARC_STAT_OK);
}
void OpenARC::lib::set_option(int arg, void* val, size_t valsz)
{
CHECK_EQ(arc_options(arc_, ARC_OP_SETOPT, arg, val, valsz), ARC_STAT_OK);
}
void OpenARC::lib::set_cv_unkn() { arc_set_cv(msg_, ARC_CHAIN_UNKNOWN); }
void OpenARC::lib::set_cv_none() { arc_set_cv(msg_, ARC_CHAIN_NONE); }
void OpenARC::lib::set_cv_fail() { arc_set_cv(msg_, ARC_CHAIN_FAIL); }
void OpenARC::lib::set_cv_pass() { arc_set_cv(msg_, ARC_CHAIN_PASS); }
void OpenARC::lib::header(std::string_view header)
{
CHECK_EQ(arc_header_field(msg_, uc(header.data()), header.length()),
ARC_STAT_OK);
}
void OpenARC::lib::eoh() { CHECK_EQ(arc_eoh(msg_), ARC_STAT_OK); }
void OpenARC::lib::body(std::string_view body)
{
CHECK_EQ(arc_body(msg_, uc(body.data()), body.length()), ARC_STAT_OK);
}
void OpenARC::lib::eom() { CHECK_EQ(arc_eom(msg_), ARC_STAT_OK); }
OpenARC::sign::sign()
{
u_char const* error;
msg_ = arc_message(arc_, ARC_CANON_RELAXED, ARC_CANON_RELAXED,
ARC_SIGN_RSASHA256, ARC_MODE_SIGN, &error);
CHECK_NOTNULL(msg_);
}
OpenARC::sign::~sign() { arc_free(msg_); }
bool OpenARC::sign::seal(char const* authservid,
char const* selector,
char const* domain,
char const* key,
size_t keylen,
char const* ar)
{
// clang-format off
auto const stat = arc_getseal(msg_,
&seal_,
const_cast<char*>(authservid),
const_cast<char*>(selector),
const_cast<char*>(domain),
uc(key),
keylen,
uc(ar));
// clang-format on
return stat == ARC_STAT_OK;
}
static std::string get_name(arc_hdrfield* hdr)
{
CHECK_NOTNULL(hdr);
size_t len = 0;
auto const p = c(arc_hdr_name(hdr, &len));
return std::string(p, len);
}
static std::string get_value(arc_hdrfield* hdr)
{
CHECK_NOTNULL(hdr);
auto const p = c(arc_hdr_value(hdr));
return std::string(p, strlen(p));
}
std::string OpenARC::sign::name() const { return get_name(seal_); }
std::string OpenARC::sign::value() const { return get_value(seal_); }
std::vector<std::string> OpenARC::sign::whole_seal() const
{
std::vector<std::string> hdrs;
auto const re = std::regex("(?:\\r\\n|\\n|\\r)");
for (auto sealhdr = seal_; sealhdr; sealhdr = arc_hdr_next(sealhdr)) {
auto const hdr =
fmt::format("{}:{}", get_name(sealhdr), get_value(sealhdr));
hdrs.emplace_back(std::regex_replace(hdr, re, "\r\n"));
}
return hdrs;
}
OpenARC::verify::verify()
{
u_char const* error;
msg_ = arc_message(arc_, ARC_CANON_RELAXED, ARC_CANON_RELAXED,
ARC_SIGN_RSASHA256, ARC_MODE_VERIFY, &error);
CHECK_NOTNULL(msg_);
}
OpenARC::verify::~verify() { arc_free(msg_); }
char const* OpenARC::verify::chain_status_str() const
{
return arc_chain_status_str(msg_);
}
std::string OpenARC::verify::chain_custody_str() const
{
for (iobuffer<char> buf{256}; buf.size() < 10 * 1024 * 1024;
buf.resize(buf.size() * 2)) {
size_t len = arc_chain_custody_str(msg_, uc(buf.data()), buf.size());
if (len < buf.size())
return std::string(buf.data(), len);
}
LOG(FATAL) << "custody chain way too large...";
return "";
}