-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.cpp
More file actions
241 lines (212 loc) · 7.15 KB
/
auth.cpp
File metadata and controls
241 lines (212 loc) · 7.15 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
/*
* YUME - Yume Universal Multiprotocol Engine
* Copyright (C) 2026 FixCraft Inc.
* Licensed under the GNU General Public License v3.0.
*/
#include "server/auth.hpp"
#include <openssl/pem.h>
#include <openssl/sha.h>
#include <mutex>
#include <fstream>
#include <ctime>
#include <sstream>
#include <stdexcept>
#include <nlohmann/json.hpp>
namespace yume::server {
namespace {
std::optional<bool> read_policy_bool(const nlohmann::json& entry, const char* key) {
if (entry.contains("permissions") && entry["permissions"].is_object()) {
const auto& permissions = entry["permissions"];
if (permissions.contains(key) && permissions[key].is_boolean()) {
return permissions[key].get<bool>();
}
}
if (entry.contains(key) && entry[key].is_boolean()) {
return entry[key].get<bool>();
}
return std::nullopt;
}
} // namespace
bool AuthKeyPolicy::empty() const {
return !allow_exec.has_value() &&
!allow_local_ip.has_value() &&
!control_full.has_value() &&
!allow_inbound_admin.has_value() &&
!allow_outbound_admin.has_value() &&
!allow_chat.has_value() &&
!allow_file.has_value() &&
!allow_bytes.has_value() &&
federation_peer_id.empty();
}
std::vector<crypto::Bytes> load_authorized_keys(const std::string& path) {
std::vector<crypto::Bytes> keys;
if (path.empty()) {
return keys;
}
BIO* bio = BIO_new_file(path.c_str(), "r");
if (!bio) {
throw std::runtime_error("failed to open authorized_keys");
}
while (true) {
EVP_PKEY* key = PEM_read_bio_PUBKEY(bio, nullptr, nullptr, nullptr);
if (!key) {
break;
}
int len = i2d_PUBKEY(key, nullptr);
if (len > 0) {
crypto::Bytes der(static_cast<size_t>(len));
unsigned char* p = der.data();
i2d_PUBKEY(key, &p);
keys.push_back(std::move(der));
}
EVP_PKEY_free(key);
}
BIO_free(bio);
return keys;
}
AuthKeyPolicyMap load_auth_policies(const std::string& meta_path) {
AuthKeyPolicyMap policies;
if (meta_path.empty()) {
return policies;
}
std::ifstream in(meta_path);
if (!in) {
return policies;
}
nlohmann::json meta = nlohmann::json::object();
try {
in >> meta;
} catch (const std::exception& ex) {
throw std::runtime_error(std::string("failed to parse auth_keys_meta: ") + ex.what());
}
if (!meta.is_object()) {
throw std::runtime_error("auth_keys_meta root must be an object");
}
for (auto it = meta.begin(); it != meta.end(); ++it) {
if (!it.value().is_object()) {
continue;
}
AuthKeyPolicy policy;
policy.allow_exec = read_policy_bool(it.value(), "allow_exec");
policy.allow_local_ip = read_policy_bool(it.value(), "allow_local_ip");
policy.control_full = read_policy_bool(it.value(), "control_full");
policy.allow_inbound_admin = read_policy_bool(it.value(), "allow_inbound_admin");
policy.allow_outbound_admin = read_policy_bool(it.value(), "allow_outbound_admin");
policy.allow_chat = read_policy_bool(it.value(), "allow_chat");
policy.allow_file = read_policy_bool(it.value(), "allow_file");
policy.allow_bytes = read_policy_bool(it.value(), "allow_bytes");
if (it.value().contains("federation_peer_id") && it.value()["federation_peer_id"].is_string()) {
policy.federation_peer_id = it.value()["federation_peer_id"].get<std::string>();
}
if (!policy.empty()) {
policies[it.key()] = std::move(policy);
}
}
return policies;
}
bool is_authorized(EVP_PKEY* pubkey, const std::vector<crypto::Bytes>& authorized) {
if (!pubkey) {
return false;
}
int len = i2d_PUBKEY(pubkey, nullptr);
if (len <= 0) {
return false;
}
crypto::Bytes der(static_cast<size_t>(len));
unsigned char* p = der.data();
i2d_PUBKEY(pubkey, &p);
for (const auto& allowed : authorized) {
if (allowed == der) {
return true;
}
}
return false;
}
crypto::Bytes read_field(const crypto::Bytes& payload, size_t& offset) {
if (offset + 2 > payload.size()) {
throw std::runtime_error("auth payload truncated");
}
uint16_t len = static_cast<uint16_t>((payload[offset] << 8) | payload[offset + 1]);
offset += 2;
if (offset + len > payload.size()) {
throw std::runtime_error("auth payload truncated");
}
crypto::Bytes out(payload.begin() + offset, payload.begin() + offset + len);
offset += len;
return out;
}
std::string fingerprint_pubkey(EVP_PKEY* pubkey) {
if (!pubkey) {
return "";
}
int len = i2d_PUBKEY(pubkey, nullptr);
if (len <= 0) {
return "";
}
crypto::Bytes der(static_cast<size_t>(len));
unsigned char* p = der.data();
i2d_PUBKEY(pubkey, &p);
unsigned char hash[SHA256_DIGEST_LENGTH] = {0};
SHA256(der.data(), der.size(), hash);
static const char* kHex = "0123456789abcdef";
std::string out;
out.reserve(SHA256_DIGEST_LENGTH * 2);
for (size_t i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
out.push_back(kHex[(hash[i] >> 4) & 0xF]);
out.push_back(kHex[hash[i] & 0xF]);
}
return out;
}
std::string summarize_auth_policy(const AuthKeyPolicy& policy) {
std::vector<std::string> parts;
auto append = [&](const char* key, const std::optional<bool>& value) {
if (!value.has_value()) {
return;
}
parts.emplace_back(std::string(key) + "=" + (*value ? "true" : "false"));
};
append("allow_exec", policy.allow_exec);
append("allow_local_ip", policy.allow_local_ip);
append("control_full", policy.control_full);
append("allow_inbound_admin", policy.allow_inbound_admin);
append("allow_outbound_admin", policy.allow_outbound_admin);
append("allow_chat", policy.allow_chat);
append("allow_file", policy.allow_file);
append("allow_bytes", policy.allow_bytes);
if (!policy.federation_peer_id.empty()) {
parts.emplace_back("federation_peer_id=" + policy.federation_peer_id);
}
std::ostringstream out;
for (std::size_t i = 0; i < parts.size(); ++i) {
if (i > 0) {
out << ",";
}
out << parts[i];
}
return out.str();
}
void update_auth_meta(const std::string& meta_path, const std::string& fingerprint, const std::string& alias) {
if (meta_path.empty() || fingerprint.empty()) {
return;
}
static std::mutex meta_mutex;
std::lock_guard<std::mutex> lock(meta_mutex);
nlohmann::json meta = nlohmann::json::object();
std::ifstream in(meta_path);
if (in) {
try {
in >> meta;
} catch (...) {
meta = nlohmann::json::object();
}
}
nlohmann::json entry = meta.value(fingerprint, nlohmann::json::object());
if (!alias.empty()) {
entry["alias"] = alias;
}
entry["last_seen"] = static_cast<long long>(std::time(nullptr));
meta[fingerprint] = entry;
std::ofstream out(meta_path);
out << meta.dump(2);
}
} // namespace yume::server