This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enclave.c
1481 lines (1271 loc) · 34.4 KB
/
enclave.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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018, 2019, 2020 Tim Kuijsten
*
* Permission to use, copy, modify, and distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright
* notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/event.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <openssl/curve25519.h>
#include <openssl/evp.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "base64.h"
#include "blake2.h"
#include "tai64n.h"
#include "util.h"
#include "wireprot.h"
#include "wiresep.h"
#define TAGLEN 16
#define MINDATA (1 << 21) /* malloc(3) and mmap(2) without ifn or peers */
#define MAXSTACK (1 << 15) /* 32 KB should be enough */
#ifdef DEBUG
#define MAXCORE (1024 * 1024 * 10)
#else
#define MAXCORE 0
#endif
void enclave_printinfo(FILE *);
extern int background, verbose;
/*
* Both session ids in the hs structure are in 32 bits in wire format (little-
* endian).
*/
/* handshake state */
struct hs {
uint32_t sessid; /* wire format, little-endian */
uint32_t peersessid; /* wire format, little-endian */
wskey epriv;
wskey epubi;
wskey c;
wshash h;
struct peer *peer;
};
/*
* pubkey = Spubm'
* pubkeyhash = Hash(Hash(Hash(Construction) || Identifier) || Spubm')
* mac1key = Hash(Label-Mac1 || Spubm')
* dhsecret = DH(Sprivm, Spubm')
*/
struct peer {
uint32_t id;
wskey pubkey;
wshash pubkeyhash;
wskey mac1key;
wskey dhsecret;
wskey psk;
struct ifn *ifn;
struct hs *hs;
uint8_t recvts[12]; /* last received authenticated timestamp */
};
/*
* psk = optional symmetric pre-shared secret, Q
* pubkey = Spubm
* pubkeyhash = Hash(Hash(Hash(Construction) || Identifier) || Spubm)
* mac1key = Hash(Label-Mac1 || Spubm)
* cookiekey = Hash(Label-Cookie || Spubm)
*/
struct ifn {
uint32_t id;
int port;
char *ifname; /* null terminated name of the interface */
size_t ifnamesize;
wskey privkey;
wskey pubkey;
wshash pubkeyhash;
wskey mac1key;
wskey cookiekey;
struct peer **peers;
size_t peerssize;
};
static uid_t uid;
static gid_t gid;
static int kq, pport, doterm, logstats;
static uint8_t msg[MAXSCRATCH];
static struct ifn **ifnv;
static size_t ifnvsize;
static struct tai64n ts;
static struct iovec iov[3];
static wshash conshash, considhash, tmph;
static wskey k, t0, tau, tmpc;
static uint8_t tmpempty[0 + TAGLEN], tmpts[12 + TAGLEN],
tmpstat[KEYLEN + TAGLEN];
/*
* Copyright (c) 2019 Matt Dunwoodie
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Hmac(key, input) Hmac-Blake2s(key, input, 32), the ordinary BLAKE2s hash
* function used in an HMAC construction, returning 32 bytes of output.
*/
static void
hmac(wskey out, const struct iovec *iovin, size_t iovinlen, const wskey key)
{
/* struct */ blake2s_state state;
u_int8_t x_key[BLAKE2S_BLOCKBYTES] = {0};
u_int8_t i_hash[BLAKE2S_OUTBYTES];
size_t n;
int i;
memcpy(x_key, key, BLAKE2S_KEYBYTES);
for (i = 0; i < BLAKE2S_BLOCKBYTES; i++)
x_key[i] ^= 0x36;
blake2s_init(&state, BLAKE2S_OUTBYTES);
blake2s_update(&state, x_key, BLAKE2S_BLOCKBYTES);
for (n = 0; n < iovinlen; n++)
blake2s_update(&state, iovin[n].iov_base, iovin[n].iov_len);
blake2s_final(&state, i_hash, BLAKE2S_OUTBYTES);
for (i = 0; i < BLAKE2S_BLOCKBYTES; i++)
x_key[i] ^= 0x5c ^ 0x36;
blake2s_init(&state, BLAKE2S_OUTBYTES);
blake2s_update(&state, x_key, BLAKE2S_BLOCKBYTES);
blake2s_update(&state, i_hash, BLAKE2S_OUTBYTES);
blake2s_final(&state, i_hash, BLAKE2S_OUTBYTES);
memcpy(out, i_hash, BLAKE2S_OUTBYTES);
}
static void
handlesig(int signo)
{
switch (signo) {
case SIGUSR1:
logstats = 1;
break;
case SIGINT:
/* FALLTHROUGH */
case SIGTERM:
doterm = 1;
break;
default:
logwarnx("enclave unexpected signal %d %s", signo,
strsignal(signo));
break;
}
}
/*
* Derive "iovlen" new keys (HMAC).
*
* 0 < "iovlen" < 256
*
* Each "iov" entry must be at least KEYLEN bytes long
*
* Return 0 on success, -1 on failure.
*/
static int
kdfn(struct iovec *iovout, size_t iovoutlen, const wskey in, const wskey key)
{
struct iovec iov[2];
uint8_t t; /* must be one byte */
if (iovoutlen < 1 || iovoutlen > 255)
return -1;
if (in) {
iov[0].iov_base = (uint8_t *)in;
iov[0].iov_len = KEYLEN;
hmac(t0, iov, 1, key);
} else {
hmac(t0, NULL, 0, key);
}
t = 1;
iov[0].iov_base = &t;
iov[0].iov_len = 1;
hmac(iovout[0].iov_base, iov, 1, t0);
for (t = 2; t <= iovoutlen; t++) {
iov[0].iov_base = iovout[t - 2].iov_base;
iov[0].iov_len = iovout[t - 2].iov_len;
iov[1].iov_base = &t;
iov[1].iov_len = 1;
hmac(iovout[t - 1].iov_base, iov, 2, t0);
}
explicit_bzero(t0, KEYLEN);
return 0;
}
/*
* Derive one new key (HMAC).
*
* Return 0 on success, -1 on failure.
*/
static int
kdf1(wskey out, const wskey in, const wskey key)
{
struct iovec iov[1];
iov[0].iov_base = out;
iov[0].iov_len = KEYLEN;
if (kdfn(iov, 1, in, key) == -1)
return -1;
return 0;
}
/*
* Generate new transport data keys based on the chaining key.
* Make a MSGSESSKEYS message, updates "msk".
*
* Return 0 on success, -1 on failure.
*/
static int
makemsgsesskeys(struct msgsesskeys *msk, struct hs *hs, int responder)
{
if (msk == NULL || hs == 0)
return -1;
iov[0].iov_len = iov[1].iov_len = KEYLEN;
if (responder) {
iov[0].iov_base = msk->recvkey;
iov[1].iov_base = msk->sendkey;
} else {
iov[0].iov_base = msk->sendkey;
iov[1].iov_base = msk->recvkey;
}
if (kdfn(iov, 2, NULL, hs->c) == -1)
return -1;
msk->sessid = hs->sessid;
msk->peersessid = hs->peersessid;
return 0;
}
/*
* Do the DH.
*
* Return 0 on success, -1 on failure.
*/
static int
dh(wskey sharedsecret, const wskey privkey, const wskey peerkey)
{
if (X25519(sharedsecret, privkey, peerkey) == 0)
return -1;
return 0;
}
static void
inithash2(wshash out, const void *in1, size_t in1size, const void *in2,
size_t in2size)
{
iov[0].iov_base = (uint8_t *)in1;
iov[0].iov_len = in1size;
iov[1].iov_base = (uint8_t *)in2;
iov[1].iov_len = in2size;
ws_hash(out, iov, 2);
}
static void
appendhash(wshash h, const uint8_t *in, size_t insize)
{
iov[0].iov_base = h;
iov[0].iov_len = HASHLEN;
iov[1].iov_base = (uint8_t *)in;
iov[1].iov_len = insize;
ws_hash(h, iov, 2);
}
/*
* Find a peer by public key and interface. Return 1 if found and updates "p" to
* point to it. 0 if not found and updates "p" to NULL.
*
* XXX log(n)
*/
static int
findifnpeerbypubkey(struct peer **p, const struct ifn *ifn, wskey pubkey)
{
size_t n;
*p = NULL;
for (n = 0; n < ifn->peerssize; n++) {
if (memcmp(ifn->peers[n]->pubkey, pubkey, sizeof(wskey)) == 0) {
*p = ifn->peers[n];
return 1;
}
}
return 0;
}
/*
* Find a peer by session id and interface. Return 1 if found and updates "p" to
* point to it. 0 if not found and updates "p" to NULL.
*
* XXX log(n)
*/
static int
findifnpeerbysessid(struct peer **p, const struct ifn *ifn, uint32_t sessid)
{
size_t n;
*p = NULL;
for (n = 0; n < ifn->peerssize; n++) {
if (ifn->peers[n]->hs->sessid == sessid) {
*p = ifn->peers[n];
return 1;
}
}
return 0;
}
/*
* Find a peer by id and interface. Return 1 if found and updates "p" to point
* to it. 0 if not found and updates "p" to NULL.
*/
static int
findifnpeerbyid(struct peer **p, const struct ifn *ifn, uint32_t peerid)
{
*p = NULL;
if (peerid >= ifn->peerssize)
return 0;
*p = ifn->peers[peerid];
return 1;
}
static void
prinths(FILE *fp, const struct hs *hs)
{
fprintf(fp, "sessid %x\n", le32toh(hs->sessid));
fprintf(fp, "peersessid %x\n", le32toh(hs->peersessid));
fprintf(fp, "chaining key\n");
hexdump(fp, hs->c, sizeof(hs->c), sizeof(hs->c));
fprintf(fp, "hash\n");
hexdump(fp, hs->h, sizeof(hs->h), sizeof(hs->h));
}
static void
printpeer(FILE *fp, const struct peer *peer)
{
fprintf(fp, "id %u\n", peer->id);
fprintf(fp, "pubkey\n");
hexdump(fp, peer->pubkey, sizeof(peer->pubkey), sizeof(peer->pubkey));
fprintf(fp, "pubkeyhash\n");
hexdump(fp, peer->pubkeyhash, sizeof(peer->pubkeyhash),
sizeof(peer->pubkeyhash));
fprintf(fp, "mac1key\n");
hexdump(fp, peer->mac1key, sizeof(peer->mac1key),
sizeof(peer->mac1key));
fprintf(fp, "recvts\n");
hexdump(fp, peer->recvts, sizeof(peer->recvts), sizeof(peer->recvts));
}
/*
* Authenticated encryption and decryption.
*
* Return 0 on success, -1 on failure. "outlen" is a value/result parameter.
*/
static int
aead(uint8_t *out, size_t *outlen, const uint8_t *in, size_t inlen,
const wskey key, const wshash h, int open)
{
const EVP_AEAD *aead = EVP_aead_chacha20_poly1305();
EVP_AEAD_CTX ctx;
static const uint8_t nonce[12] = { 0 };
assert(EVP_AEAD_nonce_length(aead) == sizeof(nonce));
assert(EVP_AEAD_max_tag_len(aead) == TAGLEN);
if (EVP_AEAD_CTX_init(&ctx, aead, key, KEYLEN, TAGLEN, NULL) == 0)
return -1;
if (open) {
if (EVP_AEAD_CTX_open(&ctx, out, outlen, *outlen, nonce,
sizeof(nonce), in, inlen, h, HASHLEN) == 0)
goto err;
} else {
if (EVP_AEAD_CTX_seal(&ctx, out, outlen, *outlen, nonce,
sizeof(nonce), in, inlen, h, HASHLEN) == 0)
goto err;
}
EVP_AEAD_CTX_cleanup(&ctx);
return 0;
err:
EVP_AEAD_CTX_cleanup(&ctx);
return -1;
}
/*
* Upgrade handshake initialization state.
*
* The following fields are updated on success:
* peer->hs->c
* peer->hs->h
*
* In case we're the responder the peer is first searched for and mwi->stat and
* mwi->timestamp are verified. The following extra fields are updated:
* peer is set if found
* peer->recvts
* peer->hs->epubi
*
* If we're the initiator ("peer" is not NULL) mwi->stat and mwi->timestamp are
* set.
*
* Return 0 on success, -1 on failure.
*/
static int
upgradehsinit(struct ifn *ifn, struct msgwginit *mwi, struct peer **peer,
int responder)
{
struct hs *hs;
struct peer *p;
size_t n;
if (ifn == NULL || mwi == NULL || peer == NULL)
return -1;
if (responder) {
memcpy(tmph, ifn->pubkeyhash, HASHLEN);
} else {
memcpy(tmph, (*peer)->pubkeyhash, HASHLEN);
}
appendhash(tmph, mwi->ephemeral, sizeof(mwi->ephemeral));
if (kdf1(tmpc, mwi->ephemeral, conshash) == -1)
return -1;
if (responder) {
if (dh(k, ifn->privkey, mwi->ephemeral) == -1)
return -1;
} else {
if (dh(k, (*peer)->hs->epriv, (*peer)->pubkey) == -1)
return -1;
}
iov[0].iov_base = tmpc;
iov[0].iov_len = KEYLEN;
iov[1].iov_base = k;
iov[1].iov_len = KEYLEN;
if (kdfn(iov, 2, k, tmpc) == -1)
return -1;
if (responder) {
n = sizeof(tmpstat);
if (aead(tmpstat, &n, mwi->stat, sizeof(mwi->stat), k, tmph, 1)
== -1)
return -1;
if (!findifnpeerbypubkey(&p, ifn, tmpstat))
return -1;
/* extra verification on connected sockets */
if (*peer && p != *peer)
return -1;
*peer = p;
} else {
n = sizeof(mwi->stat);
if (aead(mwi->stat, &n, ifn->pubkey, sizeof(ifn->pubkey), k,
tmph, 0) == -1)
return -1;
}
hs = (*peer)->hs;
appendhash(tmph, mwi->stat, sizeof(mwi->stat));
iov[0].iov_base = tmpc;
iov[0].iov_len = KEYLEN;
iov[1].iov_base = k;
iov[1].iov_len = KEYLEN;
if (kdfn(iov, 2, (*peer)->dhsecret, tmpc) == -1)
return -1;
if (responder) {
n = sizeof(tmpts);
if (aead(tmpts, &n, mwi->timestamp, sizeof(mwi->timestamp), k,
tmph, 1))
return -1;
if (memcmp(tmpts, (*peer)->recvts, sizeof(tmpts)) <= 0) {
logwarnx("enclave %s %x received init message is "
"replayed", ifn->ifname, le32toh(hs->sessid));
return -1;
}
/* Successfully authenticated */
memcpy((*peer)->recvts, tmpts,
MIN(sizeof (*peer)->recvts, sizeof tmpts));
memcpy(hs->epubi, mwi->ephemeral, KEYLEN);
} else {
n = sizeof(mwi->timestamp);
if (aead(mwi->timestamp, &n, mwi->timestamp,
sizeof(mwi->timestamp) - TAGLEN, k, tmph, 0))
return -1;
}
appendhash(tmph, mwi->timestamp, sizeof(mwi->timestamp));
memcpy(hs->c, tmpc, KEYLEN);
memcpy(hs->h, tmph, HASHLEN);
return 0;
}
/*
* Create new handshake initialization state and a message.
*
* Return 0 on success if "hs" and "mwi" are initialized, -1 on failure.
*/
static int
createhsinit(struct peer *peer, struct msgwginit *mwi)
{
struct hs *hs;
if (peer == NULL || mwi == NULL)
return -1;
hs = peer->hs;
hs->sessid = arc4random();
mwi->type = htole32(1);
mwi->sender = hs->sessid;
X25519_keypair(mwi->ephemeral, hs->epriv);
if (externaltai64n(mwi->timestamp, sizeof(mwi->timestamp) - TAGLEN,
nowtai64n(&ts)) == -1)
return -1;
if (upgradehsinit(peer->ifn, mwi, &peer, 0) == -1) {
logwarnx("enclave %s %x could not upgrade new init message",
peer->ifn->ifname, le32toh(hs->sessid));
return -1;
}
/*
* Calculate MAC of message.
*
* msga = everything up to mac1 field, depends on structure of mwi.
*
* Mac1:
* 14. msg.mac1 := Mac(Hash(Label-Mac1 || Spubr), msga)
* -> mac(msg.mac1, msga, Hash(Label-Mac1 || Spubr))
*/
if (ws_mac(mwi->mac1, sizeof(mwi->mac1), mwi, MAC1OFFSETINIT,
hs->peer->mac1key) == -1)
return -1;
/*
* Cookies are handled outside the enclave.
*/
memset(mwi->mac2, 0, sizeof(mwi->mac2));
return 0;
}
/*
* Upgrade handshake response state.
*
* In case we're the initiator the following fields are updated only if
* successfully verified:
* hs->c
*
* If we're the responder these fields are always updated, including mwr->empty.
*
* Return 0 on success, -1 on failure.
*/
static int
upgradehsresp(struct hs *hs, struct msgwgresp *mwr, int responder)
{
size_t n;
if (kdf1(tmpc, mwr->ephemeral, hs->c) == -1)
return -1;
inithash2(tmph, hs->h, HASHLEN, mwr->ephemeral, KEYLEN);
if (responder) {
if (dh(k, hs->epriv, hs->epubi) == -1)
return -1;
} else {
if (dh(k, hs->epriv, mwr->ephemeral) == -1)
return -1;
}
if (kdf1(tmpc, k, tmpc) == -1)
return -1;
if (responder) {
if (dh(k, hs->epriv, hs->peer->pubkey) == -1)
return -1;
} else {
if (dh(k, hs->peer->ifn->privkey, mwr->ephemeral) == -1)
return -1;
}
if (kdf1(tmpc, k, tmpc) == -1)
return -1;
iov[0].iov_base = tmpc;
iov[0].iov_len = KEYLEN;
iov[1].iov_base = tau;
iov[1].iov_len = KEYLEN;
iov[2].iov_base = k;
iov[2].iov_len = KEYLEN;
if (kdfn(iov, 3, hs->peer->psk, tmpc) == -1)
return -1;
appendhash(tmph, tau, KEYLEN);
if (responder) {
n = sizeof(mwr->empty);
if (aead(mwr->empty, &n, NULL, 0, k, tmph, 0))
return -1;
} else {
n = sizeof(tmpempty);
if (aead(tmpempty, &n, mwr->empty, sizeof(mwr->empty), k, tmph,
1))
return -1;
}
/* skip hashing msg.empty */
/* Successfully authenticated */
memcpy(hs->c, tmpc, KEYLEN);
return 0;
}
/*
* Create new handshake response state and a message.
*
* Return 0 on success if "hs" and "mwr" are initialized, -1 on failure.
*/
static int
createhsresp(struct peer *peer, struct msgwgresp *mwr,
const struct msgwginit *mwi)
{
struct hs *hs;
if (peer == NULL || mwr == NULL || mwi == NULL)
return -1;
hs = peer->hs;
/*
* Be careful to get everything from mwi before we write into mwr as it
* might point to the same memory.
*/
hs->sessid = arc4random();
hs->peersessid = mwi->sender;
mwr->type = htole32(2);
mwr->sender = hs->sessid;
mwr->receiver = hs->peersessid;
X25519_keypair(mwr->ephemeral, hs->epriv);
if (upgradehsresp(hs, mwr, 1) == -1) {
logwarnx("enclave %s %x could not upgrade response message",
peer->ifn->ifname, le32toh(hs->sessid));
return -1;
}
if (ws_mac(mwr->mac1, sizeof(mwr->mac1), mwr, MAC1OFFSETRESP,
hs->peer->mac1key) == -1)
return -1;
memset(mwr->mac2, 0, sizeof(mwr->mac2));
return 0;
}
/*
* Handle an incoming MSGWGINIT, make sure it authenticates and determine or
* verify the peer. Then write the appropriate response. Reads and writes to
* "msg".
*
* Return 0 on success, -1 if data did not authenticate or another error
* occurred.
*
* MSGWGINIT
* if data authenticates
* determine peer if it's not given, verify if it's given
* if fsn and lsn are not null send MSGCONNREQ
* send MSGSESSKEYS
* create and send MSGWGRESP
*/
static int
handlewginit(struct ifn *ifn, struct peer *peer,
union sockaddr_inet *fsn, union sockaddr_inet *lsn)
{
struct msgconnreq mcr;
struct msgsesskeys msk;
struct msgwgresp *mwr;
struct msgwginit *mwi;
uint32_t initsess, respsess;
mwi = (struct msgwginit *)msg;
initsess = le32toh(mwi->sender);
if (!ws_validmac(mwi->mac1, sizeof(mwi->mac1), mwi, MAC1OFFSETINIT,
ifn->mac1key)) {
if (peer) {
logwarnx("enclave %s I:%x init message with invalid mac"
" received from peer %u", ifn->ifname, initsess,
peer->id);
} else {
logwarnx("enclave %s I:%x init message with invalid mac"
" received from peer", ifn->ifname, initsess);
}
return -1;
}
if (upgradehsinit(ifn, mwi, &peer, 1) == -1) {
if (peer) {
logwarnx("enclave %s I:%x could not authenticate init "
"message from peer %u", ifn->ifname, initsess,
peer->id);
} else {
logwarnx("enclave %s I:%x could not authenticate init "
"message from peer", ifn->ifname, initsess);
}
return -1;
}
/* Overwrite msg mwi with mwr */
mwr = (struct msgwgresp *)msg;
if (createhsresp(peer, mwr, mwi) == -1) {
logwarnx("enclave %s I:%x could not create response message for"
" peer %u", ifn->ifname, initsess, peer->id);
return -1;
}
respsess = le32toh(mwr->sender);
if (fsn && lsn) {
if (makemsgconnreq(&mcr, fsn, lsn) == -1) {
logwarnx("enclave %s (%x) I:%x makemsgconnreq error for"
" peer %u", ifn->ifname, respsess, initsess,
peer->id);
exit(1);
}
if (wire_sendpeeridmsg(ifn->port, peer->id, MSGCONNREQ, &mcr,
sizeof(mcr)) == -1) {
logwarnx("enclave %s (%x) I:%x error sending connect "
"request for peer %u to ifn", ifn->ifname,
respsess, initsess, peer->id);
return -1;
}
}
if (makemsgsesskeys(&msk, peer->hs, 1) == -1) {
logwarnx("enclave %s (%x) I:%x makemsgsesskeys error for peer "
"%u", ifn->ifname, respsess, initsess, peer->id);
exit(1);
}
if (wire_sendpeeridmsg(ifn->port, peer->id, MSGSESSKEYS, &msk,
sizeof(msk)) == -1) {
logwarnx("enclave %s (%x) I:%x error sending keys for peer %u "
"to ifn", ifn->ifname, respsess, initsess, peer->id);
return -1;
}
explicit_bzero(&msk, sizeof(msk));
if (wire_sendpeeridmsg(ifn->port, peer->id, MSGWGRESP, mwr,
sizeof(*mwr)) == -1) {
logwarnx("enclave %s (%x) I:%x error sending response message "
"for peer %u to ifn", ifn->ifname, respsess, initsess,
peer->id);
return -1;
}
if (verbose > 1)
loginfox("enclave %s (%x) I:%x sent response message for peer "
"%u to ifn", ifn->ifname, respsess, initsess, peer->id);
return 0;
}
/*
* Handle an incoming MSGWGRESP, make sure it authenticates and determine or
* verify the peer. Then write the appropriate response. Reads and writes to
* "msg".
*
* Return 0 on success, -1 if data did not authenticate or another error
* occurred.
*
* MSGWGRESP
* if data authenticates
* determine peer if it's not given, verify if it's given
* if fsn and lsn are not null send MSGCONNREQ
* send MSGSESSKEYS
*
* Return 0 on success, -1 on error.
*/
static int
handlewgresp(struct ifn *ifn, struct peer *peer,
union sockaddr_inet *fsn, union sockaddr_inet *lsn)
{
struct msgconnreq mcr;
struct msgsesskeys msk;
struct msgwgresp *mwr;
struct peer *p;
uint32_t initsess, respsess;
mwr = (struct msgwgresp *)msg;
initsess = le32toh(mwr->receiver);
respsess = le32toh(mwr->sender);
if (!ws_validmac(mwr->mac1, sizeof(mwr->mac1), mwr, MAC1OFFSETRESP,
ifn->mac1key)) {
if (peer) {
logwarnx("enclave %s /%x/ R:%x response message with "
"invalid mac received from peer %u", ifn->ifname,
initsess, respsess, peer->id);
} else {
logwarnx("enclave %s /%x/ R:%x response message with "
"invalid mac received from peer", ifn->ifname,
initsess, respsess);
}
return -1;
}
if (!findifnpeerbysessid(&p, ifn, mwr->receiver)) {
if (peer) {
logwarnx("enclave %s /%x/ R:%x receiver in response "
"message from peer %u is unknown", ifn->ifname,
initsess, respsess, peer->id);
} else {
logwarnx("enclave %s /%x/ R:%x receiver in response "
"message from peer is unknown", ifn->ifname,
initsess, respsess);
}
return -1;
}
/* verify the authenticated packet came in on the right socket */
if (peer && peer->id != p->id) {
logwarnx("enclave %s /%x/ R:%x response message received from "
"peer %u designated for peer %u, discarding", ifn->ifname,
initsess, respsess, p->id, peer->id);
return -1;
}
peer = p;
if (upgradehsresp(peer->hs, mwr, 0) == -1) {
logwarnx("enclave %s /%x/ R:%x response message received from "
"peer %u could not be authenticated", ifn->ifname, initsess,
respsess, p->id);
return -1;
}
peer->hs->peersessid = mwr->sender;
if (fsn && lsn) {
if (makemsgconnreq(&mcr, fsn, lsn) == -1) {
logwarnx("enclave %s %x R:%x makemsgconnreq error for "
"peer %u", ifn->ifname, initsess, respsess,
peer->id);
exit(1);
}
if (wire_sendpeeridmsg(ifn->port, peer->id, MSGCONNREQ, &mcr,
sizeof(mcr)) == -1) {
logwarnx("enclave %s %x R:%x error sending connect "
"request for peer %u to ifn", ifn->ifname, initsess,
respsess, peer->id);
return -1;
}
}
if (makemsgsesskeys(&msk, peer->hs, 0) == -1) {
logwarnx("enclave %s %x R:%x makemsgsesskeys error for peer %u",
ifn->ifname, initsess, respsess, peer->id);
exit(1);
}
if (wire_sendpeeridmsg(ifn->port, peer->id, MSGSESSKEYS, &msk,
sizeof(msk)) == -1) {
logwarnx("enclave %s %x R:%x error sending keys for peer %u to "
"ifn", ifn->ifname, initsess, respsess, peer->id);
return -1;
}
if (verbose > 1)
loginfox("enclave %s %x R:%x sent new session keys for peer %u "
"to ifn", ifn->ifname, initsess, respsess, peer->id);
explicit_bzero(&msk, sizeof(msk));
return 0;
}
/*
* Receive and handle a message from an IFN.
*
* MSGWGINIT
* if data authenticates:
* create and send MSGWGRESP
* send MSGSESSKEYS
* MSGWGRESP
* if data authenticates:
* send MSGSESSKEYS
* MSGREQWGINIT
* create and send MSGWGINIT
*
* Return 0 on success, -1 on error.
*/
static int
handleifnmsg(const struct ifn *ifn)
{
struct msgwginit *mwi;
struct peer *peer;
size_t msgsize;
uint32_t peerid;
unsigned char mtcode;
msgsize = sizeof(msg);
if (wire_recvpeeridmsg(ifn->port, &peerid, &mtcode, msg, &msgsize)
== -1) {
logwarnx("enclave %s read error", ifn->ifname);
return -1;
}
if (!findifnpeerbyid(&peer, ifn, peerid)) {
logwarnx("enclave %s unknown peer id %u", ifn->ifname, peerid);
return -1;
}
switch (mtcode) {
case MSGWGINIT:
return handlewginit(peer->ifn, peer, NULL, NULL);
case MSGWGRESP:
return handlewgresp(peer->ifn, peer, NULL, NULL);
case MSGREQWGINIT:
mwi = (struct msgwginit *)msg;