This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
sae.c
2686 lines (2493 loc) · 80.3 KB
/
sae.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) Dan Harkins, 2008, 2009, 2010
*
* Copyright holder grants permission for redistribution and use in source
* and binary forms, with or without modification, provided that the
* following conditions are met:
* 1. Redistribution of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer
* in all source files.
* 2. Redistribution in binary form must retain the above copyright
* notice, this list of conditions, and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* 3. All advertising materials and documentation mentioning features
* or use of this software must display the following acknowledgement:
*
* "This product includes software written by
* Dan Harkins (dharkins at lounge dot org)"
*
* "DISCLAIMER OF LIABILITY
*
* THIS SOFTWARE IS PROVIDED BY DAN HARKINS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INDUSTRIAL LOUNGE BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE."
*
* This license and distribution terms cannot be changed. In other words,
* this code cannot simply be copied and put under a different distribution
* license (including the GNU public license).
*/
#include "sae.h"
#include <libconfig.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
#include <string.h>
#include "aid.h"
#include "peer_lists.h"
#include "peers.h"
#define COUNTER_INFINITY 65535
#define REAUTH_JITTER 30
#define state_to_string(x) \
(x) == SAE_NOTHING ? "NOTHING" : (x) == SAE_COMMITTED ? "COMMITTED" \
: (x) == SAE_CONFIRMED \
? "CONFIRMED" \
: (x) == SAE_ACCEPTED ? "ACCEPTED" : "unknown"
#define seq_to_string(x) \
(x) == SAE_AUTH_COMMIT ? "COMMIT" : (x) == SAE_AUTH_CONFIRM ? "CONFIRM" \
: "unknown"
#define status_to_string(x) \
(x) == WLAN_STATUS_ANTI_CLOGGING_TOKEN_NEEDED \
? "TOKEN NEEDED" \
: (x) == WLAN_STATUS_NOT_SUPPORTED_GROUP ? "REJECTION" : "unknown"
/*
* the functions H() and CN()
*/
#define H_Init(ctx, x, l) HMAC_Init_ex((ctx), (x), (l), EVP_sha256(), NULL)
#define H_Update(ctx, x, l) HMAC_Update((ctx), (x), (l))
#define H_Final(ctx, x) HMAC_Final((ctx), (x), &function_mdlen)
#define CN_Init(ctx, x, l) HMAC_Init_ex((ctx), (x), (l), EVP_sha256(), NULL)
#define CN_Update(ctx, x, l) HMAC_Update((ctx), (x), (l))
#define CN_Final(ctx, x) HMAC_Final((ctx), (x), &function_mdlen)
/* Pre 1.1.0 compatibility macros */
#ifndef OPENSSL_API_COMPAT
static HMAC_CTX *HMAC_CTX_new(void) {
return (HMAC_CTX *)calloc(sizeof(HMAC_CTX), 1);
}
static void HMAC_CTX_free(HMAC_CTX *ctx) {
if (ctx) {
HMAC_CTX_cleanup(ctx);
free(ctx);
}
}
#endif
struct sae_cb *cb;
/*
* forward declarations
*/
static void reauth(void *data);
/*
* global variables
*/
BN_CTX *bnctx = NULL;
GD *gd; /* group definitions */
BIO *out;
int curr_open, open_threshold, retrans;
unsigned long blacklist_timeout, giveup_threshold, pmk_expiry;
unsigned long token_generator;
char conffile[PATH_MAX], allzero[SHA256_DIGEST_LENGTH];
unsigned int function_mdlen = SHA256_DIGEST_LENGTH;
enum result { NO_ERR, ERR_NOT_FATAL, ERR_FATAL, ERR_BLACKLIST };
static void dump_buffer(unsigned char *buf, int len) {
int i;
for (i = 0; i < len; i++) {
if (i && (i % 4 == 0)) {
printf(" ");
}
if (i && (i % 32 == 0)) {
printf("\n");
}
printf("%02x", buf[i]);
}
printf("\n");
}
static void print_buffer(char *str, unsigned char *buf, int len) {
printf("%s:\n", str);
dump_buffer(buf, len);
printf("\n");
}
static void pp_a_bignum(char *str, BIGNUM *bn) {
unsigned char *buf;
int len;
len = BN_num_bytes(bn);
if ((buf = malloc(len)) == NULL) {
return;
}
BN_bn2bin(bn, buf);
print_buffer(str, buf, len);
free(buf);
}
int prf(
unsigned char *key,
int keylen,
unsigned char *label,
int labellen,
unsigned char *context,
int contextlen,
unsigned char *result,
int resultbitlen) {
HMAC_CTX *ctx;
unsigned char digest[SHA256_DIGEST_LENGTH];
int resultlen, len = 0;
unsigned int mdlen = SHA256_DIGEST_LENGTH;
unsigned char mask = 0xff;
unsigned short reslength;
unsigned short i = 0, i_le;
reslength = ieee_order(resultbitlen);
resultlen = (resultbitlen + 7) / 8;
do {
i++;
ctx = HMAC_CTX_new();
if (ctx == NULL)
return -1;
HMAC_Init_ex(ctx, key, keylen, EVP_sha256(), NULL);
i_le = ieee_order(i);
HMAC_Update(ctx, (unsigned char *)&i_le, sizeof(i_le));
HMAC_Update(ctx, label, labellen);
HMAC_Update(ctx, context, contextlen);
HMAC_Update(ctx, (unsigned char *)&reslength, sizeof(unsigned short));
HMAC_Final(ctx, digest, &mdlen);
HMAC_CTX_free(ctx);
ctx = NULL;
if ((len + mdlen) > resultlen) {
memcpy(result + len, digest, resultlen - len);
} else {
memcpy(result + len, digest, mdlen);
}
len += mdlen;
} while (len < resultlen);
/*
* we're expanding to a bit length, if this is not a
* multiple of 8 bits then mask off the excess.
*/
if (resultbitlen % 8) {
mask <<= (8 - (resultbitlen % 8));
result[resultlen - 1] &= mask;
}
return resultlen;
}
static void remove_from_blacklist(void *data) {
struct candidate *peer, *delme;
delme = (struct candidate *)data;
TAILQ_FOREACH(peer, &blacklist, entry) {
if (memcmp(delme->peer_mac, peer->peer_mac, ETH_ALEN) == 0) {
sae_debug(
SAE_DEBUG_PROTOCOL_MSG,
"removing " MACSTR " from blacklist\n",
MAC2STR(peer->peer_mac));
TAILQ_REMOVE(&blacklist, peer, entry);
free(delme);
return;
}
}
}
static void blacklist_peer(struct candidate *peer) {
struct candidate *fubar;
if ((fubar = (struct candidate *)malloc(sizeof(struct candidate))) != NULL) {
memcpy(fubar->peer_mac, peer->peer_mac, ETH_ALEN);
TAILQ_INSERT_TAIL(&blacklist, fubar, entry);
(void)cb->evl->add_timeout(
SRV_SEC(blacklist_timeout), remove_from_blacklist, fubar);
}
}
static void delete_local_peer_info(struct candidate *delme)
{
struct candidate *peer;
TAILQ_FOREACH(peer, &peers, entry) {
if (delme == peer) {
sae_debug(
SAE_DEBUG_PROTOCOL_MSG,
"deleting peer at " MACSTR " in state %s\n",
MAC2STR(peer->peer_mac),
state_to_string(peer->state));
if ((peer->state == SAE_COMMITTED) || (peer->state == SAE_CONFIRMED)) {
curr_open--;
if (curr_open < 0) {
/*
* one of those "should not happen" kinds of things
*/
sae_debug(
SAE_DEBUG_ERR,
"***ERROR*** we have %d currently open sessions\n",
curr_open);
}
}
cb->evl->rem_timeout(peer->t0); /* no harm if not set */
peer->t0 = 0;
cb->evl->rem_timeout(peer->t1); /* ditto */
peer->t1 = 0;
cb->evl->rem_timeout(peer->t2); /* ditto */
peer->t2 = 0;
cb->evl->rem_timeout(peer->rekey_ping_timer);
peer->rekey_ping_timer = 0;
TAILQ_REMOVE(&peers, peer, entry);
dump_peer_list();
/*
* PWE, the private value, the PMK and KCK are all secret so
* take some special care when deleting them.
*/
EC_POINT_clear_free(peer->pwe);
BN_clear_free(peer->private_val);
memset(peer->pmk, 0, SHA256_DIGEST_LENGTH);
memset(peer->kck, 0, SHA256_DIGEST_LENGTH);
BN_free(peer->peer_scalar);
EC_POINT_free(peer->peer_element);
BN_free(peer->my_scalar);
EC_POINT_free(peer->my_element);
aid_free(peer->association_id);
free(peer->ht_cap);
free(peer->ht_info);
free(peer->vht_cap);
free(peer->vht_info);
free(delme);
return;
}
}
sae_debug(SAE_DEBUG_ERR, "failed to delete peer :-( \n");
}
/*
* Clean up state, remove peer from database, and free up memory.
*
* Note, to remove the station from the kernel, you should use
* finalize() instead, which will normally call this at the
* end.
*/
void delete_peer(struct candidate **delme)
{
delete_local_peer_info(*delme);
*delme = NULL;
}
/*
* a callback-able version of delete peer
*/
static void destroy_peer(void *data) {
delete_local_peer_info((struct candidate *) data);
}
/*
* finalize: call cb->fin, and then remove the local peer info.
* if reason is non-zero, cb->fin is expected to remove the station
* from the kernel, if necessary.
*/
static
void finalize(unsigned short reason, struct candidate *peer,
unsigned char *buf, int len, void *cookie)
{
cb->fin(reason, peer->peer_mac, buf, len, cookie);
if (reason == WLAN_STATUS_SUCCESSFUL)
return;
delete_local_peer_info(peer);
}
static int on_blacklist(unsigned char *mac) {
struct candidate *peer;
TAILQ_FOREACH(peer, &blacklist, entry) {
if (memcmp(peer->peer_mac, mac, ETH_ALEN) == 0) {
return 1;
}
}
return 0;
}
struct candidate *find_peer(unsigned char *mac, int accept) {
struct candidate *peer, *found = NULL;
TAILQ_FOREACH(peer, &peers, entry) {
if (memcmp(peer->peer_mac, mac, ETH_ALEN) == 0) {
/*
* if "accept" then we're only looking for peers in "accepted" state
*/
if (accept) {
if (peer->state == SAE_ACCEPTED) {
return peer;
}
continue;
}
/*
* otherwise we'll take any peer but, if there are 2, give preference
* to the one not in "accepted" state
*/
if (found == NULL) {
found = peer;
} else {
if ((found->state == SAE_ACCEPTED) && (peer->state != SAE_ACCEPTED)) {
found = peer;
}
}
}
}
return found;
}
static int check_dup(
struct candidate *peer,
int check_me,
struct ieee80211_mgmt_frame *frame,
int len) {
unsigned char *ptr;
int itemsize, ret;
BIGNUM *scalar;
if ((scalar = BN_new()) == NULL) {
/*
* this seems kind of serious so return that it is a dupe so we don't
* do anymore processing of this frame
*/
return 0;
}
ptr = frame->authenticate.u.var8 + sizeof(unsigned short);
if (peer->got_token) {
/*
* we know how big the token is because we generated it in the first place!
*/
ptr += SHA256_DIGEST_LENGTH;
}
itemsize = BN_num_bytes(peer->grp_def->order);
BN_bin2bn(ptr, itemsize, scalar);
if (check_me) {
ret = BN_cmp(peer->my_scalar, scalar);
} else {
ret = BN_cmp(peer->peer_scalar, scalar);
}
BN_free(scalar);
return ret;
}
static int check_confirm(
struct candidate *peer,
struct ieee80211_mgmt_frame *frame) {
unsigned short sent_confirm;
sent_confirm = ieee_order(*(frame->authenticate.u.var16));
if ((sent_confirm > peer->rc) && (sent_confirm != COUNTER_INFINITY)) {
return 1;
} else {
return 0;
}
}
static int process_confirm(
struct candidate *peer,
struct ieee80211_mgmt_frame *frame,
int len) {
unsigned char tmp[128];
enum result r = NO_ERR;
BIGNUM *x = NULL;
BIGNUM *y = NULL;
EC_POINT *psum = NULL;
HMAC_CTX *ctx;
int offset;
ctx = HMAC_CTX_new();
if (ctx == NULL) {
r = ERR_FATAL;
goto out;
}
if (len != (IEEE802_11_HDR_LEN + sizeof(frame->authenticate) +
sizeof(unsigned short) + SHA256_DIGEST_LENGTH)) {
sae_debug(SAE_DEBUG_ERR, "bad size of confirm message (%d)\n", len);
r = ERR_NOT_FATAL;
goto out;
}
if (((x = BN_new()) == NULL) || ((y = BN_new()) == NULL) ||
((psum = EC_POINT_new(peer->grp_def->group)) == NULL)) {
sae_debug(SAE_DEBUG_ERR, "unable to construct confirm!\n");
r = ERR_FATAL;
goto out;
}
CN_Init(ctx, peer->kck, SHA256_DIGEST_LENGTH); /* the key */
peer->rc = ieee_order(*(frame->authenticate.u.var16));
sae_debug(SAE_DEBUG_PROTOCOL_MSG, "processing confirm (%d)\n", peer->rc);
/*
* compute the confirm verifier using the over-the-air format of send_conf
*/
CN_Update(ctx, frame->authenticate.u.var8, sizeof(unsigned short));
/* peer's scalar */
offset = BN_num_bytes(peer->grp_def->order) - BN_num_bytes(peer->peer_scalar);
memset(tmp, 0, offset);
BN_bn2bin(peer->peer_scalar, tmp + offset);
CN_Update(ctx, tmp, BN_num_bytes(peer->grp_def->order));
if (!EC_POINT_get_affine_coordinates_GFp(
peer->grp_def->group, peer->peer_element, x, y, bnctx)) {
sae_debug(SAE_DEBUG_ERR, "unable to get x,y of peer's element\n");
r = ERR_NOT_FATAL;
goto out;
}
/* Rarely x can be way too big, e.g. 1348 bytes. Corrupted packet? */
if (BN_num_bytes(peer->grp_def->prime) < BN_num_bytes(x) ||
BN_num_bytes(peer->grp_def->prime) < BN_num_bytes(y)) {
sae_debug(
SAE_DEBUG_ERR,
"coords are too big, x = %d bytes, y = %d bytes\n",
BN_num_bytes(x),
BN_num_bytes(y));
r = ERR_NOT_FATAL;
goto out;
}
/* peer's element */
offset = BN_num_bytes(peer->grp_def->prime) - BN_num_bytes(x);
memset(tmp, 0, offset);
BN_bn2bin(x, tmp + offset);
CN_Update(ctx, tmp, BN_num_bytes(peer->grp_def->prime));
offset = BN_num_bytes(peer->grp_def->prime) - BN_num_bytes(y);
memset(tmp, 0, offset);
BN_bn2bin(y, tmp + offset);
CN_Update(ctx, tmp, BN_num_bytes(peer->grp_def->prime));
/* my scalar */
offset = BN_num_bytes(peer->grp_def->order) - BN_num_bytes(peer->my_scalar);
memset(tmp, 0, offset);
BN_bn2bin(peer->my_scalar, tmp + offset);
CN_Update(ctx, tmp, BN_num_bytes(peer->grp_def->order));
if (!EC_POINT_get_affine_coordinates_GFp(
peer->grp_def->group, peer->my_element, x, y, bnctx)) {
sae_debug(SAE_DEBUG_ERR, "unable to get x,y of my element\n");
r = ERR_NOT_FATAL;
goto out;
}
/* my element */
offset = BN_num_bytes(peer->grp_def->prime) - BN_num_bytes(x);
memset(tmp, 0, offset);
BN_bn2bin(x, tmp + offset);
CN_Update(ctx, tmp, BN_num_bytes(peer->grp_def->prime));
offset = BN_num_bytes(peer->grp_def->prime) - BN_num_bytes(y);
memset(tmp, 0, offset);
BN_bn2bin(y, tmp + offset);
CN_Update(ctx, tmp, BN_num_bytes(peer->grp_def->prime));
CN_Final(ctx, tmp);
HMAC_CTX_free(ctx);
ctx = NULL;
if (sae_debug_mask & SAE_DEBUG_CRYPTO_VERB) {
print_buffer(
"peer's confirm",
frame->authenticate.u.var8,
SHA256_DIGEST_LENGTH + sizeof(unsigned short));
}
if (memcmp(
tmp,
(frame->authenticate.u.var8 + sizeof(unsigned short)),
SHA256_DIGEST_LENGTH)) {
sae_debug(SAE_DEBUG_ERR, "confirm did not verify!\n");
r = ERR_BLACKLIST;
goto out;
}
r = NO_ERR;
out:
HMAC_CTX_free(ctx);
BN_free(x);
BN_free(y);
EC_POINT_free(psum);
return r;
}
static int confirm_to_peer(struct candidate *peer) {
char buf[2048];
unsigned char tmp[128];
struct ieee80211_mgmt_frame *frame;
size_t len = 0;
BIGNUM *x, *y;
HMAC_CTX *ctx;
unsigned short send_conf;
int offset;
int ret = 0;
ctx = HMAC_CTX_new();
if (ctx == NULL) {
return -1;
}
if (((x = BN_new()) == NULL) || ((y = BN_new()) == NULL)) {
sae_debug(SAE_DEBUG_ERR, "unable to construct confirm!\n");
return -1;
}
memset(buf, 0, sizeof(buf));
frame = (struct ieee80211_mgmt_frame *)buf;
frame->frame_control = ieee_order(
(IEEE802_11_FC_TYPE_MGMT << 2 | IEEE802_11_FC_STYPE_AUTH << 4));
memcpy(frame->sa, peer->my_mac, ETH_ALEN);
memcpy(frame->da, peer->peer_mac, ETH_ALEN);
memcpy(frame->bssid, peer->peer_mac, ETH_ALEN);
frame->authenticate.alg = ieee_order(SAE_AUTH_ALG);
frame->authenticate.auth_seq = ieee_order(SAE_AUTH_CONFIRM);
len = IEEE802_11_HDR_LEN + sizeof(frame->authenticate);
if (peer->sc != COUNTER_INFINITY) {
peer->sc++;
}
send_conf = ieee_order(peer->sc);
memcpy(
frame->authenticate.u.var8,
(unsigned char *)&send_conf,
sizeof(unsigned short));
len += sizeof(unsigned short);
CN_Init(ctx, peer->kck, SHA256_DIGEST_LENGTH); /* the key */
/* send_conf is in over-the-air format now */
CN_Update(ctx, (unsigned char *)&send_conf, sizeof(unsigned short));
/* my scalar */
offset = BN_num_bytes(peer->grp_def->order) - BN_num_bytes(peer->my_scalar);
memset(tmp, 0, offset);
BN_bn2bin(peer->my_scalar, tmp + offset);
CN_Update(ctx, tmp, BN_num_bytes(peer->grp_def->order));
if (!EC_POINT_get_affine_coordinates_GFp(
peer->grp_def->group, peer->my_element, x, y, bnctx)) {
sae_debug(SAE_DEBUG_ERR, "unable to get x,y of my element\n");
ret = -1;
goto fail;
}
/* my element */
offset = BN_num_bytes(peer->grp_def->prime) - BN_num_bytes(x);
memset(tmp, 0, offset);
BN_bn2bin(x, tmp + offset);
CN_Update(ctx, tmp, BN_num_bytes(peer->grp_def->prime));
offset = BN_num_bytes(peer->grp_def->prime) - BN_num_bytes(y);
memset(tmp, 0, offset);
BN_bn2bin(y, tmp + offset);
CN_Update(ctx, tmp, BN_num_bytes(peer->grp_def->prime));
/* peer's scalar */
offset = BN_num_bytes(peer->grp_def->order) - BN_num_bytes(peer->peer_scalar);
memset(tmp, 0, offset);
BN_bn2bin(peer->peer_scalar, tmp + offset);
CN_Update(ctx, tmp, BN_num_bytes(peer->grp_def->order));
if (!EC_POINT_get_affine_coordinates_GFp(
peer->grp_def->group, peer->peer_element, x, y, bnctx)) {
sae_debug(SAE_DEBUG_ERR, "unable to get x,y of peer's element\n");
ret = -1;
goto fail;
}
/* peer's element */
offset = BN_num_bytes(peer->grp_def->prime) - BN_num_bytes(x);
memset(tmp, 0, offset);
BN_bn2bin(x, tmp + offset);
CN_Update(ctx, tmp, BN_num_bytes(peer->grp_def->prime));
offset = BN_num_bytes(peer->grp_def->prime) - BN_num_bytes(y);
memset(tmp, 0, offset);
BN_bn2bin(y, tmp + offset);
CN_Update(ctx, tmp, BN_num_bytes(peer->grp_def->prime));
CN_Final(ctx, (frame->authenticate.u.var8 + sizeof(unsigned short)));
if (sae_debug_mask & SAE_DEBUG_CRYPTO_VERB) {
print_buffer(
"local confirm",
frame->authenticate.u.var8,
SHA256_DIGEST_LENGTH + sizeof(unsigned short));
}
len += SHA256_DIGEST_LENGTH;
sae_debug(
SAE_DEBUG_PROTOCOL_MSG,
MACSTR " in %s, sending %s (sc=%d), len %zu\n",
MAC2STR(peer->peer_mac),
state_to_string(peer->state),
seq_to_string(ieee_order(frame->authenticate.auth_seq)),
peer->sc,
len);
if (cb->meshd_write_mgmt(buf, len, peer->cookie) != len) {
sae_debug(
SAE_DEBUG_ERR,
"can't send an authentication frame to " MACSTR "\n",
MAC2STR(peer->peer_mac));
ret = -1;
goto fail;
}
fail:
BN_free(x);
BN_free(y);
HMAC_CTX_free(ctx);
return ret;
}
static int process_commit(
struct candidate *peer,
struct ieee80211_mgmt_frame *frame,
int len) {
BIGNUM *x = NULL, *y = NULL, *k = NULL, *nsum;
int offset, itemsize, ret = 0;
EC_POINT *K = NULL;
unsigned char *ptr, *tmp, keyseed[SHA256_DIGEST_LENGTH],
kckpmk[(SHA256_DIGEST_LENGTH * 2) * 8];
HMAC_CTX *ctx;
ctx = HMAC_CTX_new();
if (ctx == NULL) {
return -1;
}
/*
* check whether the frame is big enough (might be proprietary IEs or cruft
* appended)
*/
if (len < (IEEE802_11_HDR_LEN + sizeof(frame->authenticate) +
(2 * BN_num_bytes(peer->grp_def->prime)) +
BN_num_bytes(peer->grp_def->order))) {
sae_debug(
SAE_DEBUG_ERR,
"invalid size for commit message (%d < %d+%zu+(2*%d)+%d = %zu))\n",
len,
IEEE802_11_HDR_LEN,
sizeof(frame->authenticate),
BN_num_bytes(peer->grp_def->prime),
BN_num_bytes(peer->grp_def->order),
(IEEE802_11_HDR_LEN + sizeof(frame->authenticate) +
(2 * BN_num_bytes(peer->grp_def->prime)) +
BN_num_bytes(peer->grp_def->order)));
goto fail;
}
if (((x = BN_new()) == NULL) || ((y = BN_new()) == NULL) ||
((k = BN_new()) == NULL) ||
((K = EC_POINT_new(peer->grp_def->group)) == NULL)) {
sae_debug(SAE_DEBUG_ERR, "unable to create x,y bignums\n");
goto fail;
}
ptr = frame->authenticate.u.var8;
/*
* first thing in a commit is the finite cyclic group, skip the group
*/
ptr += sizeof(unsigned short);
if (peer->got_token) {
/*
* if we got a token then skip over it. We know the size because we
* created it in the first place!
*/
ptr += SHA256_DIGEST_LENGTH;
}
/*
* first get the peer's scalar
*/
itemsize = BN_num_bytes(peer->grp_def->order);
BN_bin2bn(ptr, itemsize, peer->peer_scalar);
ptr += itemsize;
/*
* then get x and y and turn them into the peer's element
*/
itemsize = BN_num_bytes(peer->grp_def->prime);
BN_bin2bn(ptr, itemsize, x);
ptr += itemsize;
BN_bin2bn(ptr, itemsize, y);
if (!EC_POINT_set_affine_coordinates_GFp(
peer->grp_def->group, peer->peer_element, x, y, bnctx)) {
sae_debug(SAE_DEBUG_ERR, "unable to obtain peer's password element!\n");
goto fail;
}
/*
* validate the scalar...
*/
if ((BN_cmp(peer->peer_scalar, BN_value_one()) < 1) ||
(BN_cmp(peer->peer_scalar, peer->grp_def->order) > 0)) {
sae_debug(SAE_DEBUG_ERR, "peer's scalar is invalid!\n");
goto fail;
}
/*
* ...and the element
*/
if (!EC_POINT_is_on_curve(peer->grp_def->group, peer->peer_element, bnctx)) {
sae_debug(SAE_DEBUG_ERR, "peer's element is invalid!\n");
goto fail;
}
if (sae_debug_mask & SAE_DEBUG_CRYPTO_VERB) {
printf("peer's commit:\n");
pp_a_bignum("peer's scalar", peer->peer_scalar);
printf("peer's element:\n");
pp_a_bignum("x", x);
pp_a_bignum("y", y);
}
/*
* now compute: scalar * PWE...
*/
if (!EC_POINT_mul(
peer->grp_def->group, K, NULL, peer->pwe, peer->peer_scalar, bnctx)) {
sae_debug(SAE_DEBUG_ERR, "unable to multiply peer's scalar and PWE!\n");
goto fail;
}
/*
* ... + element
*/
if (!EC_POINT_add(peer->grp_def->group, K, K, peer->peer_element, bnctx)) {
sae_debug(SAE_DEBUG_ERR, "unable to add element to running point!\n");
goto fail;
}
/*
* ... * private val = our private_val * peer's private_val * pwe
*/
if (!EC_POINT_mul(
peer->grp_def->group, K, NULL, K, peer->private_val, bnctx)) {
sae_debug(
SAE_DEBUG_ERR, "unable to multiple intermediate by private value!\n");
goto fail;
}
if (!EC_POINT_get_affine_coordinates_GFp(
peer->grp_def->group, K, k, NULL, bnctx)) {
sae_debug(SAE_DEBUG_ERR, "unable to get secret key!\n");
goto fail;
}
/*
* compute the KCK and PMK
*/
if ((tmp = (unsigned char *)malloc(BN_num_bytes(peer->grp_def->prime))) ==
NULL) {
sae_debug(
SAE_DEBUG_ERR,
"unable to malloc %d bytes for secret!\n",
BN_num_bytes(k));
goto fail;
}
/*
* first extract the entropy from k into keyseed...
*/
offset = BN_num_bytes(peer->grp_def->prime) - BN_num_bytes(k);
memset(tmp, 0, offset);
BN_bn2bin(k, tmp + offset);
H_Init(ctx, allzero, SHA256_DIGEST_LENGTH);
H_Update(ctx, tmp, BN_num_bytes(peer->grp_def->prime));
H_Final(ctx, keyseed);
free(tmp);
/*
* ...then expand it to create KCK | PMK
*/
if (((tmp = (unsigned char *)malloc(BN_num_bytes(peer->grp_def->order))) ==
NULL) ||
((nsum = BN_new()) == NULL)) {
sae_debug(SAE_DEBUG_ERR, "unable to create buf/bignum to sum scalars!\n");
goto fail;
}
BN_add(nsum, peer->my_scalar, peer->peer_scalar);
BN_mod(nsum, nsum, peer->grp_def->order, bnctx);
offset = BN_num_bytes(peer->grp_def->order) - BN_num_bytes(nsum);
memset(tmp, 0, offset);
BN_bn2bin(nsum, tmp + offset);
memcpy(peer->pmkid, tmp, 16);
prf(keyseed,
SHA256_DIGEST_LENGTH,
(unsigned char *)"SAE KCK and PMK",
strlen("SAE KCK and PMK"),
tmp,
BN_num_bytes(peer->grp_def->order),
kckpmk,
((SHA256_DIGEST_LENGTH * 2) * 8));
free(tmp);
BN_free(nsum);
memcpy(peer->kck, kckpmk, SHA256_DIGEST_LENGTH);
memcpy(peer->pmk, kckpmk + SHA256_DIGEST_LENGTH, SHA256_DIGEST_LENGTH);
if (sae_debug_mask & SAE_DEBUG_CRYPTO_VERB) {
pp_a_bignum("k", k);
print_buffer("keyseed", keyseed, SHA256_DIGEST_LENGTH);
print_buffer("KCK", peer->kck, SHA256_DIGEST_LENGTH);
print_buffer("PMK", peer->pmk, SHA256_DIGEST_LENGTH);
}
if (0) {
fail:
ret = -1;
}
BN_free(x);
BN_free(y);
BN_free(k);
EC_POINT_free(K);
HMAC_CTX_free(ctx);
return ret;
}
static int
commit_to_peer(struct candidate *peer, unsigned char *token, int token_len) {
char buf[2048];
struct ieee80211_mgmt_frame *frame;
int offset1, offset2;
size_t len = 0;
BIGNUM *x, *y, *mask;
unsigned short grp_num;
unsigned char *ptr;
memset(buf, 0, sizeof(buf));
frame = (struct ieee80211_mgmt_frame *)buf;
/*
* fill in authentication frame header...
*/
frame->frame_control = ieee_order(
(IEEE802_11_FC_TYPE_MGMT << 2 | IEEE802_11_FC_STYPE_AUTH << 4));
memcpy(frame->sa, peer->my_mac, ETH_ALEN);
memcpy(frame->da, peer->peer_mac, ETH_ALEN);
memcpy(frame->bssid, peer->peer_mac, ETH_ALEN);
frame->authenticate.alg = ieee_order(SAE_AUTH_ALG);
frame->authenticate.auth_seq = ieee_order(SAE_AUTH_COMMIT);
len = IEEE802_11_HDR_LEN + sizeof(frame->authenticate);
ptr = frame->authenticate.u.var8;
/*
* first, indicate what group we're committing with
*/
grp_num = ieee_order(peer->grp_def->group_num);
memcpy(ptr, &grp_num, sizeof(unsigned short));
ptr += sizeof(unsigned short);
len += sizeof(unsigned short);
/*
* if we've been asked to include a token then include a token
*/
if (token_len && (token != NULL)) {
memcpy(ptr, token, token_len);
ptr += token_len;
len += token_len;
}
if (peer->private_val == NULL) {
if (((mask = BN_new()) == NULL) ||
((peer->private_val = BN_new()) == NULL)) {
sae_debug(SAE_DEBUG_ERR, "unable to commit to peer!\n");
return -1;
}
/*
* generate private values
*/
BN_rand_range(peer->private_val, peer->grp_def->order);
BN_rand_range(mask, peer->grp_def->order);
if (sae_debug_mask & SAE_DEBUG_CRYPTO_VERB) {
pp_a_bignum("local private value", peer->private_val);
pp_a_bignum("local mask value", mask);
}
/*
* generate scalar = (priv + mask) mod order
*/
BN_add(peer->my_scalar, peer->private_val, mask);
BN_mod(peer->my_scalar, peer->my_scalar, peer->grp_def->order, bnctx);
/*
* generate element = -(mask*pwe)
*/
if (!EC_POINT_mul(
peer->grp_def->group,
peer->my_element,
NULL,
peer->pwe,
mask,
bnctx)) {
sae_debug(SAE_DEBUG_ERR, "unable to compute A!\n");
BN_free(mask);
return -1;
}
if (!EC_POINT_invert(peer->grp_def->group, peer->my_element, bnctx)) {
sae_debug(SAE_DEBUG_ERR, "unable to invert A!\n");
BN_free(mask);
return -1;
}
BN_free(mask);
}
if (((x = BN_new()) == NULL) || ((y = BN_new()) == NULL)) {
sae_debug(SAE_DEBUG_ERR, "unable to create x,y bignums\n");
return -1;
}
if (!EC_POINT_get_affine_coordinates_GFp(
peer->grp_def->group, peer->my_element, x, y, bnctx)) {
sae_debug(SAE_DEBUG_ERR, "unable to get secret key!\n");
BN_free(x);
BN_free(y);
return -1;
}
if (sae_debug_mask & SAE_DEBUG_CRYPTO_VERB) {
printf("local commit:\n");
pp_a_bignum("my scalar", peer->my_scalar);
printf("my element:\n");
pp_a_bignum("x", x);
pp_a_bignum("y", y);
}
/*
* fill in the commit, first in the commit message is the scalar
*/
offset1 = BN_num_bytes(peer->grp_def->order) - BN_num_bytes(peer->my_scalar);
BN_bn2bin(peer->my_scalar, ptr + offset1);
ptr += BN_num_bytes(peer->grp_def->order);
len += BN_num_bytes(peer->grp_def->order);
/*
* ...next is the element, x then y
*/
if (!EC_POINT_get_affine_coordinates_GFp(
peer->grp_def->group, peer->my_element, x, y, bnctx)) {
sae_debug(SAE_DEBUG_ERR, "unable to determine u!\n");
exit(1);
}
offset1 = BN_num_bytes(peer->grp_def->prime) - BN_num_bytes(x);
BN_bn2bin(x, ptr + offset1);
ptr += BN_num_bytes(peer->grp_def->prime);
offset2 = BN_num_bytes(peer->grp_def->prime) - BN_num_bytes(y);
BN_bn2bin(y, ptr + offset2);
ptr += BN_num_bytes(peer->grp_def->prime);
len += (2 * BN_num_bytes(peer->grp_def->prime));
sae_debug(
SAE_DEBUG_PROTOCOL_MSG,
"peer " MACSTR " in %s, sending %s (%s token), len %zu, group %d\n",
MAC2STR(peer->peer_mac),
state_to_string(peer->state),