-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.c
807 lines (674 loc) · 15.3 KB
/
cli.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
*/
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <pwd.h>
#include <unistd.h>
#include <libubox/utils.h>
#include <libubox/uloop.h>
#include <libubox/blobmsg.h>
#include <libubox/blobmsg_json.h>
#include "edsign.h"
#include "ed25519.h"
#include "curve25519.h"
#include "auth-data.h"
#include "pex-msg.h"
static uint8_t peerkey[EDSIGN_PUBLIC_KEY_SIZE];
static uint8_t pubkey[EDSIGN_PUBLIC_KEY_SIZE];
static uint8_t seckey[EDSIGN_PUBLIC_KEY_SIZE];
static void *net_data;
static size_t net_data_len;
static uint64_t net_data_version;
static struct blob_attr *net_data_hosts;
static uint64_t req_id;
static struct blob_buf b;
static FILE *out_file;
static bool quiet;
static bool sync_done;
static bool has_key;
static int password_prompt;
static enum {
CMD_UNKNOWN,
CMD_GENERATE,
CMD_PUBKEY,
CMD_HOST_PUBKEY,
CMD_VERIFY,
CMD_SIGN,
CMD_DOWNLOAD,
CMD_UPLOAD,
CMD_NETDATA,
} cmd;
#define INFO(...) \
do { \
if (quiet) \
break; \
fprintf(stderr, ##__VA_ARGS__); \
} while (0)
static void print_key(const uint8_t *key)
{
char keystr[B64_ENCODE_LEN(EDSIGN_PUBLIC_KEY_SIZE)];
if (b64_encode(key, EDSIGN_PUBLIC_KEY_SIZE, keystr, sizeof(keystr)) < 0)
return;
fprintf(out_file, "%s\n", keystr);
}
static int usage(const char *progname)
{
fprintf(stderr, "Usage: %s [command|options] [<file>]\n"
"Commands:\n"
" -S Sign file\n"
" -V Verify file\n"
" -P Get public signing key from secret key\n"
" -H Get public host key from secret key\n"
" -T Get network data from signed file\n"
" -G Generate new private key\n"
" -D <host>[:<port>] Download network data from unetd\n"
" -U <host>[:<port>] Upload network data to unetd\n"
"\n"
"Options:\n"
" -q: Quiet mode - suppress error/info messages\n"
" -o <file>: Set output file to <file> (defaults to stdout)\n"
" -k <keyfile>|-: Set public key from file or stdin\n"
" -K <keyfile>|-: Set secret key from file or stdin\n"
" -h <keyfile>|- Set peer private key from file or stdin\n"
" (for network data down-/upload)\n"
" -s <n>,<salt> Generating secret key from seed using <n> rounds and <salt>\n"
" (passphrase is read from stdin)\n"
" -p Prompt for seed password\n"
" -b <file>: Read signed network data file\n"
"\n", progname);
return 1;
}
static void pex_timeout(struct uloop_timeout *timeout)
{
uloop_end();
}
static void
pex_recv_update_response(const uint8_t *data, size_t len, enum pex_opcode op)
{
int net_data_len = 0;
void *net_data;
net_data = pex_msg_update_response_recv(data, len, op, &net_data_len, NULL);
if (net_data_len < 0)
goto out;
if (!net_data)
return;
if (cmd == CMD_DOWNLOAD) {
fwrite(net_data, net_data_len, 1, out_file);
sync_done = true;
}
free(net_data);
out:
if (cmd == CMD_DOWNLOAD)
uloop_end();
}
static bool
pex_get_pubkey(uint8_t *pubkey, const uint8_t *id)
{
static const struct blobmsg_policy policy = { "key", BLOBMSG_TYPE_STRING };
struct blob_attr *cur, *key;
int rem;
blobmsg_for_each_attr(cur, net_data_hosts, rem) {
const char *keystr;
blobmsg_parse(&policy, 1, &key, blobmsg_data(cur), blobmsg_len(cur));
if (!key)
continue;
keystr = blobmsg_get_string(key);
if (b64_decode(keystr, pubkey, CURVE25519_KEY_SIZE) != CURVE25519_KEY_SIZE)
continue;
if (!memcmp(pubkey, id, PEX_ID_LEN))
return true;
}
return false;
}
static void
pex_handle_update_request(struct sockaddr_in6 *addr, const uint8_t *id, void *data, size_t len)
{
struct pex_msg_update_send_ctx ctx = {};
static uint8_t empty_key[EDSIGN_PUBLIC_KEY_SIZE] = {};
uint8_t peerpubkey[EDSIGN_PUBLIC_KEY_SIZE];
bool done = false;
if (!pex_get_pubkey(peerpubkey, id)) {
INFO("Could not find public key\n");
return;
}
pex_msg_update_response_init(&ctx, empty_key, pubkey,
peerpubkey, true, data, net_data, net_data_len);
while (!done) {
__pex_msg_send(-1, NULL, NULL, 0);
done = !pex_msg_update_response_continue(&ctx);
}
sync_done = true;
uloop_end();
}
static void pex_recv(void *msg, size_t msg_len, struct sockaddr_in6 *addr)
{
struct pex_hdr *hdr;
struct pex_ext_hdr *ehdr;
uint64_t *msg_req_id;
void *data;
hdr = pex_rx_accept(msg, msg_len, true);
if (!hdr)
return;
ehdr = (void *)(hdr + 1);
data = (void *)(ehdr + 1);
msg_req_id = data;
if (hdr->version != 0)
return;
if (memcmp(ehdr->auth_id, pubkey, sizeof(ehdr->auth_id)) != 0)
return;
*(uint64_t *)hdr->id ^= pex_network_hash(pubkey, ehdr->nonce);
switch (hdr->opcode) {
case PEX_MSG_UPDATE_REQUEST:
if (cmd != CMD_UPLOAD)
break;
pex_handle_update_request(addr, hdr->id, data, hdr->len);
break;
case PEX_MSG_UPDATE_RESPONSE:
case PEX_MSG_UPDATE_RESPONSE_DATA:
case PEX_MSG_UPDATE_RESPONSE_NO_DATA:
if (hdr->len < sizeof(*msg_req_id) || *msg_req_id != req_id)
break;
if (cmd == CMD_DOWNLOAD &&
hdr->opcode == PEX_MSG_UPDATE_RESPONSE_NO_DATA) {
INFO("No network data available\n");
uloop_end();
}
if (cmd == CMD_UPLOAD &&
hdr->opcode != PEX_MSG_UPDATE_RESPONSE_NO_DATA) {
INFO("Server has newer network data\n");
uloop_end();
}
pex_recv_update_response(data, hdr->len, hdr->opcode);
break;
}
}
static int load_network_data(const char *file)
{
static const struct blobmsg_policy policy = { "hosts", BLOBMSG_TYPE_TABLE };
struct unet_auth_hdr *hdr;
struct unet_auth_data *data;
const char *json;
net_data_len = UNETD_NET_DATA_SIZE_MAX;
net_data = unet_read_file(file, &net_data_len);
if (!net_data) {
INFO("failed to read input file %s\n", file);
return 1;
}
if (unet_auth_data_validate(NULL, net_data, net_data_len, &net_data_version, &json) < 0) {
INFO("input data validation failed\n");
return 1;
}
hdr = net_data;
data = (struct unet_auth_data *)(hdr + 1);
memcpy(pubkey, data->pubkey, sizeof(pubkey));
blob_buf_init(&b, 0);
blobmsg_add_json_from_string(&b, json);
blobmsg_parse(&policy, 1, &net_data_hosts, blobmsg_data(b.head), blobmsg_len(b.head));
if (!net_data_hosts) {
INFO("network data is missing the hosts attribute\n");
return 1;
}
return 0;
}
static int cmd_sync(const char *endpoint, int argc, char **argv)
{
uint8_t peerpubkey[EDSIGN_PUBLIC_KEY_SIZE];
struct uloop_timeout timeout = {
.cb = pex_timeout
};
struct pex_update_request *req;
union network_endpoint ep = {};
int len;
if (cmd == CMD_UPLOAD) {
if (argc < 1) {
INFO("missing file argument\n");
return 1;
}
if (load_network_data(argv[0]))
return 1;
}
if (network_get_endpoint(&ep, AF_UNSPEC, endpoint, UNETD_GLOBAL_PEX_PORT, 0) < 0) {
INFO("Invalid hostname/port %s\n", endpoint);
return 1;
}
len = ep.sa.sa_family == AF_INET6 ? sizeof(ep.in6) : sizeof(ep.in);
uloop_init();
if (pex_open(&ep, len, pex_recv, false) < 0)
return 1;
uloop_timeout_set(&timeout, 5000);
curve25519_generate_public(peerpubkey, peerkey);
req = pex_msg_update_request_init(peerpubkey, peerkey, pubkey, &ep,
net_data_version, true);
if (!req)
return 1;
req_id = req->req_id;
if (__pex_msg_send(-1, NULL, NULL, 0) < 0) {
if (!quiet)
perror("send");
return 1;
}
uloop_run();
return !sync_done;
}
static int cmd_sign(int argc, char **argv)
{
struct unet_auth_hdr hdr = {
.magic = cpu_to_be32(UNET_AUTH_MAGIC),
};
struct unet_auth_data *data;
struct timeval tv;
struct stat st;
off_t len;
FILE *f;
if (argc != 1) {
INFO("Missing filename\n");
return 1;
}
if (gettimeofday(&tv, NULL)) {
if (!quiet)
perror("gettimeofday");
return 1;
}
if (stat(argv[0], &st) ||
(f = fopen(argv[0], "r")) == NULL) {
INFO("Input file not found\n");
return 1;
}
data = calloc(1, sizeof(*data) + st.st_size + 1);
data->timestamp = cpu_to_be64(tv.tv_sec);
len = fread(data + 1, 1, st.st_size, f);
fclose(f);
if (len != st.st_size) {
INFO("Error reading from input file\n");
return 1;
}
len += sizeof(*data) + 1;
memcpy(data->pubkey, pubkey, sizeof(pubkey));
edsign_sign(hdr.signature, pubkey, seckey, (const void *)data, len);
fwrite(&hdr, sizeof(hdr), 1, out_file);
fwrite(data, len, 1, out_file);
free(data);
return 0;
}
static int cmd_verify(int argc, char **argv)
{
struct unet_auth_data *data;
struct unet_auth_hdr *hdr;
struct stat st;
off_t len;
FILE *f;
int ret = 1;
if (argc != 1) {
INFO("Missing filename\n");
return 1;
}
if (stat(argv[0], &st) ||
(f = fopen(argv[0], "r")) == NULL) {
INFO("Input file not found\n");
return 1;
}
if (st.st_size <= sizeof(*hdr) + sizeof(*data)) {
INFO("Input file too small\n");
fclose(f);
return 1;
}
hdr = calloc(1, st.st_size);
len = fread(hdr, 1, st.st_size, f);
fclose(f);
if (len != st.st_size) {
INFO("Error reading from input file\n");
return 1;
}
ret = unet_auth_data_validate(pubkey, hdr, len, NULL, NULL);
switch (ret) {
case -1:
INFO("Invalid input data\n");
break;
case -2:
INFO("Public key does not match\n");
break;
case -3:
INFO("Signature verification failed\n");
break;
}
free(hdr);
return ret;
}
static int cmd_host_pubkey(int argc, char **argv)
{
curve25519_generate_public(pubkey, seckey);
print_key(pubkey);
return 0;
}
static int cmd_pubkey(int argc, char **argv)
{
print_key(pubkey);
return 0;
}
static int cmd_generate(int argc, char **argv)
{
FILE *f;
int ret;
if (has_key)
goto out;
f = fopen("/dev/urandom", "r");
if (!f) {
INFO("Can't open /dev/urandom\n");
return 1;
}
ret = fread(seckey, sizeof(seckey), 1, f);
fclose(f);
if (ret != 1) {
INFO("Can't read data from /dev/urandom\n");
return 1;
}
ed25519_prepare(seckey);
out:
print_key(seckey);
return 0;
}
static int cmd_netdata(int argc, char **argv)
{
size_t ofs = sizeof(struct unet_auth_hdr) + sizeof(struct unet_auth_data);
if (!net_data || net_data_len <= ofs) {
INFO("Missing network data\n");
return 1;
}
fputs(net_data + ofs, out_file);
return 0;
}
static bool parse_key(uint8_t *dest, const char *str)
{
char keystr[B64_ENCODE_LEN(EDSIGN_PUBLIC_KEY_SIZE) + 2];
FILE *f;
int len;
if (!strcmp(str, "-"))
f = stdin;
else
f = fopen(str, "r");
if (!f) {
INFO("Can't open key file for reading\n");
return false;
}
len = fread(keystr, 1, sizeof(keystr) - 1, f);
if (f != stdin)
fclose(f);
keystr[len] = 0;
if (b64_decode(keystr, dest, EDSIGN_PUBLIC_KEY_SIZE) != EDSIGN_PUBLIC_KEY_SIZE) {
INFO("Failed to parse key data\n");
return false;
}
return true;
}
static void
pbkdf2_hmac_sha512(uint8_t *dest, const void *key, size_t key_len,
const void *salt, size_t salt_len,
unsigned int rounds)
{
uint8_t hash[SHA512_HASH_SIZE];
hmac_sha512(dest, key, key_len, salt, salt_len);
for (size_t i = 0; i < rounds - 1; i++) {
hmac_sha512(hash, key, key_len, dest, SHA512_HASH_SIZE);
for (size_t k = 0; k < SHA512_HASH_SIZE; k++)
dest[k] ^= hash[k];
}
}
static bool parse_seed(uint8_t *dest, const char *salt)
{
uint8_t hash[SHA512_HASH_SIZE];
char buf[256], *pw = buf;
unsigned long rounds;
size_t len = 0;
char *sep;
rounds = strtoul(salt, &sep, 0);
if (!rounds || *sep != ',') {
INFO("Invalid number of rounds\n");
return false;
}
if (password_prompt) {
char *pw2;
pw = getpass("Password: ");
if (pw)
len = strlen(pw);
if (pw && password_prompt > 1) {
pw = strcpy(buf, pw);
pw2 = getpass("Retype password: ");
if (!pw2 || strcmp(pw, pw2) != 0) {
INFO("Passwords do not match\n");
return false;
}
}
} else {
len = fread(buf, 1, sizeof(buf), stdin);
if (!feof(stdin)) {
INFO("Key data too long\n");
return false;
}
}
if (len < 12) {
INFO("Key data too short\n");
return false;
}
pbkdf2_hmac_sha512(hash, pw, len, salt, strlen(salt), rounds);
memcpy(dest, hash, EDSIGN_PUBLIC_KEY_SIZE);
return true;
}
static bool cmd_needs_peerkey(void)
{
switch (cmd) {
case CMD_DOWNLOAD:
return true;
default:
return false;
}
}
static bool cmd_needs_pubkey(void)
{
switch (cmd) {
case CMD_PUBKEY:
case CMD_DOWNLOAD:
case CMD_VERIFY:
return true;
default:
return false;
}
}
static bool cmd_needs_key(void)
{
switch (cmd) {
case CMD_SIGN:
case CMD_HOST_PUBKEY:
return true;
default:
return false;
}
}
static bool cmd_needs_outfile(void)
{
switch (cmd) {
case CMD_SIGN:
case CMD_PUBKEY:
case CMD_GENERATE:
case CMD_DOWNLOAD:
case CMD_NETDATA:
return true;
default:
return false;
}
}
int main(int argc, char **argv)
{
const char *progname = argv[0];
const char *out_filename = NULL;
const char *cmd_arg = NULL;
bool has_pubkey = false;
bool has_peerkey = false;
int ret, ch;
while ((ch = getopt(argc, argv, "b:h:k:K:o:qD:GHpPs:STU:V")) != -1) {
switch (ch) {
case 'D':
case 'U':
case 'G':
case 'H':
case 'S':
case 'T':
case 'P':
case 'V':
if (cmd != CMD_UNKNOWN)
return usage(progname);
break;
default:
break;
}
switch (ch) {
case 'q':
quiet = true;
break;
case 'o':
out_filename = optarg;
break;
case 'h':
if (has_peerkey)
return usage(progname);
if (!parse_key(peerkey, optarg)) {
return 1;
}
has_peerkey = true;
break;
case 's':
if (has_pubkey)
return usage(progname);
if (!parse_seed(seckey, optarg))
return 1;
has_key = true;
edsign_sec_to_pub(pubkey, seckey);
has_pubkey = true;
break;
case 'k':
if (has_pubkey)
return usage(progname);
if (!parse_key(pubkey, optarg)) {
return 1;
}
has_pubkey = true;
break;
case 'K':
if (has_pubkey)
return usage(progname);
if (!parse_key(seckey, optarg)) {
return 1;
}
has_key = true;
edsign_sec_to_pub(pubkey, seckey);
has_pubkey = true;
break;
case 'p':
password_prompt++;
break;
case 'b':
if (load_network_data(optarg))
return 1;
has_pubkey = true;
break;
case 'U':
cmd = CMD_UPLOAD;
cmd_arg = optarg;
break;
case 'D':
cmd = CMD_DOWNLOAD;
cmd_arg = optarg;
break;
case 'G':
cmd = CMD_GENERATE;
break;
case 'S':
cmd = CMD_SIGN;
break;
case 'T':
cmd = CMD_NETDATA;
break;
case 'P':
cmd = CMD_PUBKEY;
break;
case 'H':
cmd = CMD_HOST_PUBKEY;
break;
case 'V':
cmd = CMD_VERIFY;
break;
default:
return usage(progname);
}
}
if (!has_peerkey && cmd_needs_peerkey()) {
INFO("Missing -h <key> argument\n");
return 1;
}
if (!has_key && cmd_needs_key()) {
INFO("Missing -K <key> argument\n");
return 1;
}
if (!has_pubkey && cmd_needs_pubkey()) {
INFO("Missing -k <key> argument\n");
return 1;
}
argc -= optind;
argv += optind;
if (out_filename && cmd_needs_outfile()) {
out_file = fopen(out_filename, "w");
if (!out_file) {
INFO("Failed to open output file\n");
return 1;
}
} else {
out_file = stdout;
}
ret = -1;
switch (cmd) {
case CMD_UPLOAD:
case CMD_DOWNLOAD:
ret = cmd_sync(cmd_arg, argc, argv);
break;
case CMD_GENERATE:
ret = cmd_generate(argc, argv);
break;
case CMD_SIGN:
ret = cmd_sign(argc, argv);
break;
case CMD_PUBKEY:
ret = cmd_pubkey(argc, argv);
break;
case CMD_HOST_PUBKEY:
ret = cmd_host_pubkey(argc, argv);
break;
case CMD_VERIFY:
ret = cmd_verify(argc, argv);
break;
case CMD_NETDATA:
ret = cmd_netdata(argc, argv);
break;
case CMD_UNKNOWN:
ret = usage(progname);
break;
}
if (net_data)
free(net_data);
blob_buf_free(&b);
if (out_file != stdout) {
fclose(out_file);
if (ret)
unlink(out_filename);
}
return ret;
}