-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeys.cpp
More file actions
322 lines (249 loc) · 8.43 KB
/
Copy pathkeys.cpp
File metadata and controls
322 lines (249 loc) · 8.43 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "keys.hxx"
#include "base64.h"
#include "cryptlib.h"
#include "files.h"
#include <filesystem>
#include <format>
#include <iostream>
#include <stdexcept>
#include "hex.h"
#include "nlohmann/json.hpp"
#include "queue.h"
#include "spdlog/spdlog.h"
#include "xed25519.h"
using namespace CryptoPP;
KeyManager::KeyManager(const std::filesystem::path &keys_dir,
const std::string &keyfile_prefix)
: keyfile_prefix(keyfile_prefix), keys_dir(keys_dir),
logger(spdlog::get("audpipe")) {
this->pubkey_file = {};
this->privkey_file = {};
}
void KeyManager::ensure_file_open(const std::filesystem::path &file_path,
std::fstream &fs,
std::ios_base::openmode mode) {
std::string err_msg;
auto die = [&] {
this->logger->error(err_msg);
throw std::runtime_error(err_msg);
};
try {
fs.exceptions(std::ios::failbit | std::ios::badbit);
if (!file_path.parent_path().empty()) {
// Creates if didn't exist already. Will only shit the bed if
// file_path/../../ does not exist, or we get another error.
std::filesystem::create_directory(file_path.parent_path());
}
fs.open(file_path, std::ios::app);
fs.close();
fs.open(file_path, mode);
} catch (const std::ios_base::failure &e) {
if (e.code().value() == 0) {
throw;
}
this->logger->error("Could not open file at path \'{}\' because: {}",
file_path.native(), e.what());
throw std::system_error{
e.code().value(), std::generic_category(),
std::format("Opening file at path \'{}\' failed because: {}",
file_path.native(), e.what())};
} catch (const std::exception &e) {
// Don't presume this is logged upstream here just in case.
this->logger->error(e.what());
throw;
}
if (!fs.is_open()) {
logger->error("Could not open file at \'{}\' for unknown reasons.",
file_path.native());
}
}
void KeyManager::load_or_create() {
// Guard in case I accidentally call from elsewhere.
if (this->loaded)
return;
std::string err_msg;
auto die = [&] {
this->logger->error(err_msg);
throw std::runtime_error(err_msg);
};
// Open `keys_dir/`keyfile_prefix`_ed25519` and .pub.
this->pubkey_path =
this->keys_dir / std::format("{}_ed25519.pub", this->keyfile_prefix);
this->privkey_path =
this->keys_dir / std::format("{}_ed25519", this->keyfile_prefix);
this->trusted_peers_path = this->keys_dir / "trusted_peers.json";
// These should be open for r/w if they don't throw.
ensure_file_open(pubkey_path, this->pubkey_file);
ensure_file_open(privkey_path, this->privkey_file);
ensure_file_open(trusted_peers_path, this->trusted_peers_file);
// If empty then we want to create a keypair.
this->pubkey_file.seekg(0, std::ios::end);
size_t pubkey_file_size = this->pubkey_file.tellg();
this->pubkey_file.seekg(0, std::ios::beg);
this->privkey_file.seekg(0, std::ios::end);
size_t privkey_file_size = this->privkey_file.tellg();
this->privkey_file.seekg(0, std::ios::beg);
if (privkey_file_size == 0 || pubkey_file_size == 0) {
create();
logger->info("Created ed25519 keypair in {}.", this->keys_dir.native());
return;
}
load();
logger->info("Loaded ed25519 keypair from {}.", this->keys_dir.native());
}
void KeyManager::sign() {}
void KeyManager::verify() {}
void load_from_file(const std::string &filename, BufferedTransformation &bt) {
FileSource file(filename.c_str(), true);
file.TransferTo(bt);
bt.MessageEnd();
}
void KeyManager::load() {
this->privkey_file.close();
this->pubkey_file.close();
Base64Decoder dec(new ByteQueue());
::load_from_file(this->pubkey_path, dec);
this->pubkey.Load(dec);
auto queue = dynamic_cast<ByteQueue *>(dec.AttachedTransformation());
queue->Clear();
::load_from_file(this->privkey_path, dec);
this->privkey.Load(dec);
}
// You can safely call .value() on signer, verifier after this, provided it does
// not throw an exception.
void KeyManager::ensure_loaded() {
if (this->loaded && this->signer.has_value() && this->verifier.has_value()) {
return;
}
this->load_or_create();
}
// We may cache the result but this is called on `create()`.
void KeyManager::create_pubkey() {
std::string err_msg;
auto die = [&] {
this->logger->error(err_msg);
throw std::runtime_error(err_msg);
};
if (!signer.has_value()) {
err_msg = "KeyManager->signer not set but called `KeyManager::pubkey`! "
"This is an internal invariant. Probably file an issue if you "
"see this.";
die();
}
this->verifier = ed25519::Verifier(signer.value());
auto file_sink = new FileSink(this->pubkey_file);
Base64Encoder encoder(file_sink);
const ed25519PublicKey &pubkey =
dynamic_cast<const ed25519PublicKey &>(this->verifier->GetPublicKey());
pubkey.Save(encoder);
encoder.MessageEnd();
this->pubkey = pubkey;
}
KeyManager::~KeyManager() {
if (this->privkey_file.is_open()) {
this->privkey_file.close();
}
if (this->pubkey_file.is_open()) {
this->pubkey_file.close();
}
}
// Creates a new keypair from scratch.
void KeyManager::create() {
Base64Encoder encoder(new FileSink(this->privkey_file));
AutoSeededRandomPool prng;
this->signer = ed25519::Signer();
this->signer->AccessPrivateKey().GenerateRandom(prng);
const ed25519PrivateKey &privkey =
dynamic_cast<const ed25519PrivateKey &>(this->signer->GetPrivateKey());
privkey.Save(encoder);
encoder.MessageEnd();
this->create_pubkey();
this->privkey = privkey;
}
std::vector<TrustedPeer> KeyManager::get_trusted_peers() {
using json = nlohmann::json;
json data;
std::string err_msg;
auto die = [&] {
this->logger->error(err_msg);
throw std::runtime_error(err_msg);
};
this->trusted_peers_file >> data;
auto json_peers = data.get<std::vector<schema::TrustedPeer>>();
std::vector<TrustedPeer> peers;
peers.reserve(json_peers.size());
std::ranges::transform(
json_peers, std::back_inserter(peers),
[](const auto &peer_json) { return TrustedPeer(peer_json); });
return peers;
}
TrustedPeer::TrustedPeer(schema::TrustedPeer json_model) {
std::string err_msg;
auto logger = spdlog::get("audpipe");
auto die = [&] {
logger->error(err_msg);
throw std::runtime_error(err_msg);
};
if (json_model.signer_id.empty()) {
err_msg =
std::format("Empty `signer_id` field when reading from JSON file!");
die();
}
if (json_model.pubkey_base64.empty()) {
err_msg =
std::format("Empty `pubkey_base64` field when reading from JSON file!");
die();
}
// Expect 64 characters for `signer_id` as it is hex encoded.
if (json_model.signer_id.length() != 64) {
err_msg =
std::format("Incorrect `signer_id` length as this is hex encoded!");
die();
}
// Decode fields and create TrustedPeer.
this->signer_id = decode_hex_id(json_model.signer_id);
this->pubkey = decode_pubkey_base64(json_model.pubkey_base64);
this->label = std::move(json_model.label);
this->revoked = json_model.revoked;
}
std::array<uint8_t, 32> TrustedPeer::decode_hex_id(const std::string &hex) {
std::array<uint8_t, 32> out{};
auto ss =
CryptoPP::StringSource(hex, true,
new CryptoPP::HexDecoder(new CryptoPP::ArraySink(
out.data(), out.size())));
return out;
}
CryptoPP::ed25519PublicKey
TrustedPeer::decode_pubkey_base64(const std::string &b64) {
CryptoPP::ByteQueue q;
auto ss = CryptoPP::StringSource(
b64, true, new CryptoPP::Base64Decoder(new CryptoPP::Redirector(q)));
CryptoPP::ed25519PublicKey pubkey;
pubkey.Load(q);
return pubkey;
}
std::string
TrustedPeer::encode_hex_id(const std::array<uint8_t, 32> &id) const {
std::string out;
CryptoPP::StringSource ss(
id.data(), id.size(), true,
new CryptoPP::HexEncoder(new CryptoPP::StringSink(out), false));
return out;
}
std::string TrustedPeer::encode_pubkey_base64(
const CryptoPP::ed25519PublicKey &pubkey) const {
std::string out;
CryptoPP::Base64Encoder encoder(new CryptoPP::StringSink(out), false);
pubkey.Save(encoder);
encoder.MessageEnd();
return out;
}
schema::TrustedPeer TrustedPeer::to_json() const {
return schema::TrustedPeer{
.signer_id = encode_hex_id(this->signer_id),
.pubkey_base64 = encode_pubkey_base64(this->pubkey),
.label = this->label,
.revoked = this->revoked,
};
}