-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtoken.c
206 lines (163 loc) · 4.6 KB
/
token.c
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2025 Felix Fietkau <[email protected]>
*/
#include <time.h>
#include "unetd.h"
#include "sha512.h"
static uint8_t salt[8];
static uint64_t nonce;
struct token_hdr {
uint8_t src[PEX_ID_LEN];
uint8_t salt[8];
uint64_t nonce;
uint8_t hmac[SHA512_HASH_SIZE / 2];
};
static bool token_init(void)
{
static bool init_done;
FILE *f;
if (init_done)
return true;
f = fopen("/dev/urandom", "r");
if (!f)
return false;
init_done = fread(salt, sizeof(salt), 1, f) == 1;
fclose(f);
return init_done;
}
static bool
token_verify_service(struct network *net, const char *name,
struct network_host *local_host,
struct network_host *target)
{
struct network_service *s;
bool dest_found = false;
bool src_found = false;
s = vlist_find(&net->services, name, s, node);
if (!s)
return false;
for (size_t i = 0; i < s->n_members; i++) {
if (s->members[i] == local_host)
src_found = true;
if (s->members[i] == target)
dest_found = true;
}
if (!src_found || !dest_found)
return false;
return true;
}
void *token_create(struct network *net, struct network_host *target,
const char *service, struct blob_attr *info, size_t *len)
{
struct network_host *local_host = net->net_config.local_host;
size_t data_len = blob_pad_len(info);
uint8_t dh_key[CURVE25519_KEY_SIZE];
uint8_t hmac[SHA512_HASH_SIZE];
struct sha512_state s;
struct token_hdr *hdr;
const void *key;
void *data;
if (!local_host || !token_init() || target == local_host)
return NULL;
if (service && !token_verify_service(net, service, local_host, target))
return NULL;
hdr = data = malloc(sizeof(*hdr) + data_len);
data += sizeof(*hdr);
memcpy(hdr->src, local_host->peer.key, sizeof(hdr->src));
memcpy(hdr->salt, salt, sizeof(hdr->salt));
hdr->nonce = nonce++;
curve25519(dh_key, net->config.key, target->peer.key);
sha512_init(&s);
sha512_add(&s, dh_key, sizeof(dh_key));
sha512_add(&s, salt, sizeof(salt));
key = sha512_final_get(&s);
memcpy(data, info, data_len);
chacha20_encrypt_msg(data, data_len, &hdr->nonce, key);
hmac_sha512(hmac, key, SHA512_HASH_SIZE, data, data_len);
memcpy(hdr->hmac, hmac, sizeof(hdr->hmac));
*len = data_len + sizeof(*hdr);
return hdr;
}
static bool
token_decrypt(struct network *net, struct token_hdr *hdr, size_t len,
struct network_host **host)
{
struct network_host *local_host = net->net_config.local_host;
uint8_t dh_key[CURVE25519_KEY_SIZE];
uint8_t pubkey[WG_KEY_LEN] = {};
uint8_t hmac[SHA512_HASH_SIZE];
struct network_peer *peer;
struct sha512_state s;
const void *key;
void *data;
data = hdr + 1;
memcpy(pubkey, hdr->src, sizeof(hdr->src));
peer = avl_find_ge_element(&net->peers.avl, pubkey, peer, node.avl);
if (!peer || peer == &local_host->peer)
return false;
if (memcmp(peer->key, pubkey, sizeof(hdr->src)) != 0)
return false;
memcpy(pubkey, peer->key, sizeof(pubkey));
curve25519(dh_key, net->config.key, pubkey);
sha512_init(&s);
sha512_add(&s, dh_key, sizeof(dh_key));
sha512_add(&s, hdr->salt, sizeof(hdr->salt));
key = sha512_final_get(&s);
hmac_sha512(hmac, key, SHA512_HASH_SIZE, data, len);
if (memcmp(hdr->hmac, hmac, sizeof(hdr->hmac)) != 0)
return false;
chacha20_encrypt_msg(data, len, &hdr->nonce, key);
*host = container_of(peer, struct network_host, peer);
return true;
}
bool token_parse(struct blob_buf *buf, const char *token)
{
enum {
TOKEN_ATTR_SERVICE,
__TOKEN_ATTR_MAX,
};
struct blobmsg_policy policy[__TOKEN_ATTR_MAX] = {
[TOKEN_ATTR_SERVICE] = { "service", BLOBMSG_TYPE_STRING },
};
struct blob_attr *tb[__TOKEN_ATTR_MAX], *cur;
struct network_host *host;
struct token_hdr *hdr;
struct network *net;
bool ret = false;
size_t len;
void *data;
len = B64_DECODE_LEN(strlen(token));
hdr = malloc(len);
len = b64_decode(token, hdr, len);
if (len <= sizeof(*hdr) + sizeof(struct blob_attr))
goto out;
data = hdr + 1;
len -= sizeof(*hdr);
avl_for_each_element(&networks, net, node) {
struct network_host *local_host = net->net_config.local_host;
if (!local_host)
continue;
ret = token_decrypt(net, hdr, len, &host);
if (!ret)
continue;
blobmsg_add_string(buf, "network", network_name(net));
blobmsg_add_string(buf, "host", network_host_name(host));
if (blob_pad_len(data) != len) {
ret = false;
break;
}
blobmsg_parse_attr(policy, __TOKEN_ATTR_MAX, tb, data);
cur = tb[TOKEN_ATTR_SERVICE];
if (cur && !token_verify_service(net, blobmsg_get_string(cur),
local_host, host))
ret = false;
break;
}
if (!ret)
goto out;
blob_put_raw(buf, blobmsg_data(data), blobmsg_len(data));
out:
free(hdr);
return ret;
}