-
Notifications
You must be signed in to change notification settings - Fork 2
/
eap_fast.c
2091 lines (1849 loc) · 54.2 KB
/
eap_fast.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
/*
* EAP peer method: EAP-FAST (draft-cam-winget-eap-fast-03.txt)
* Copyright (c) 2004-2006, Jouni Malinen <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*/
#include "includes.h"
#include "common.h"
#include "eap_i.h"
#include "eap_tls_common.h"
#include "config_ssid.h"
#include "tls.h"
#include "eap_tlv.h"
#include "sha1.h"
#include "config.h"
/* TODO:
* - encrypt PAC-Key in the PAC file
* - test session resumption and enable it if it interoperates
* - password change (pending mschapv2 packet; replay decrypted packet)
*/
#define EAP_FAST_VERSION 1
#define EAP_FAST_KEY_LEN 64
#define EAP_FAST_PAC_KEY_LEN 32
#define EAP_FAST_SIMCK_LEN 40
#define EAP_FAST_SKS_LEN 40
#define TLS_EXT_PAC_OPAQUE 35
static const char *pac_file_hdr =
"wpa_supplicant EAP-FAST PAC file - version 1";
static void eap_fast_deinit(struct eap_sm *sm, void *priv);
#define PAC_TYPE_PAC_KEY 1
#define PAC_TYPE_PAC_OPAQUE 2
#define PAC_TYPE_CRED_LIFETIME 3
#define PAC_TYPE_A_ID 4
#define PAC_TYPE_I_ID 5
#define PAC_TYPE_SERVER_PROTECTED_DATA 6
#define PAC_TYPE_A_ID_INFO 7
#define PAC_TYPE_PAC_ACKNOWLEDGEMENT 8
#define PAC_TYPE_PAC_INFO 9
struct pac_tlv_hdr {
u16 type;
u16 len;
};
/* draft-cam-winget-eap-fast-provisioning-01.txt:
* 3.4 Key Derivations Used in the EAP-FAST Provisioning Exchange */
struct eap_fast_key_block_provisioning {
/* Extra key material after TLS key_block */
u8 session_key_seed[EAP_FAST_SKS_LEN];
u8 server_challenge[16];
u8 client_challenge[16];
};
struct eap_fast_pac {
struct eap_fast_pac *next;
u8 pac_key[EAP_FAST_PAC_KEY_LEN];
u8 *pac_opaque;
size_t pac_opaque_len;
u8 *pac_info;
size_t pac_info_len;
u8 *a_id;
size_t a_id_len;
u8 *i_id;
size_t i_id_len;
u8 *a_id_info;
size_t a_id_info_len;
};
struct eap_fast_data {
struct eap_ssl_data ssl;
int fast_version;
const struct eap_method *phase2_method;
void *phase2_priv;
int phase2_success;
struct eap_method_type phase2_type;
struct eap_method_type *phase2_types;
size_t num_phase2_types;
int resuming; /* starting a resumed session */
struct eap_fast_key_block_provisioning *key_block_p;
int provisioning_allowed; /* is PAC provisioning allowed */
int provisioning; /* doing PAC provisioning (not the normal auth) */
u8 key_data[EAP_FAST_KEY_LEN];
u8 emsk[EAP_EMSK_LEN];
int success;
struct eap_fast_pac *pac;
struct eap_fast_pac *current_pac;
int tls_master_secret_set;
u8 simck[EAP_FAST_SIMCK_LEN];
int simck_idx;
};
static void eap_fast_free_pac(struct eap_fast_pac *pac)
{
os_free(pac->pac_opaque);
os_free(pac->pac_info);
os_free(pac->a_id);
os_free(pac->i_id);
os_free(pac->a_id_info);
os_free(pac);
}
static struct eap_fast_pac * eap_fast_get_pac(struct eap_fast_data *data,
const u8 *a_id, size_t a_id_len)
{
struct eap_fast_pac *pac = data->pac;
while (pac) {
if (pac->a_id_len == a_id_len &&
os_memcmp(pac->a_id, a_id, a_id_len) == 0) {
return pac;
}
pac = pac->next;
}
return NULL;
}
static int eap_fast_add_pac(struct eap_fast_data *data,
struct eap_fast_pac *entry)
{
struct eap_fast_pac *pac, *prev;
if (entry == NULL || entry->a_id == NULL)
return -1;
/* Remove a possible old entry for the matching A-ID. */
pac = data->pac;
prev = NULL;
while (pac) {
if (pac->a_id_len == entry->a_id_len &&
os_memcmp(pac->a_id, entry->a_id, pac->a_id_len) == 0) {
if (prev == NULL) {
data->pac = pac->next;
} else {
prev->next = pac->next;
}
if (data->current_pac == pac)
data->current_pac = NULL;
eap_fast_free_pac(pac);
break;
}
prev = pac;
pac = pac->next;
}
/* Allocate a new entry and add it to the list of PACs. */
pac = os_zalloc(sizeof(*pac));
if (pac == NULL)
return -1;
os_memcpy(pac->pac_key, entry->pac_key, EAP_FAST_PAC_KEY_LEN);
if (entry->pac_opaque) {
pac->pac_opaque = os_malloc(entry->pac_opaque_len);
if (pac->pac_opaque == NULL) {
eap_fast_free_pac(pac);
return -1;
}
os_memcpy(pac->pac_opaque, entry->pac_opaque,
entry->pac_opaque_len);
pac->pac_opaque_len = entry->pac_opaque_len;
}
if (entry->pac_info) {
pac->pac_info = os_malloc(entry->pac_info_len);
if (pac->pac_info == NULL) {
eap_fast_free_pac(pac);
return -1;
}
os_memcpy(pac->pac_info, entry->pac_info,
entry->pac_info_len);
pac->pac_info_len = entry->pac_info_len;
}
if (entry->a_id) {
pac->a_id = os_malloc(entry->a_id_len);
if (pac->a_id == NULL) {
eap_fast_free_pac(pac);
return -1;
}
os_memcpy(pac->a_id, entry->a_id,
entry->a_id_len);
pac->a_id_len = entry->a_id_len;
}
if (entry->i_id) {
pac->i_id = os_malloc(entry->i_id_len);
if (pac->i_id == NULL) {
eap_fast_free_pac(pac);
return -1;
}
os_memcpy(pac->i_id, entry->i_id,
entry->i_id_len);
pac->i_id_len = entry->i_id_len;
}
if (entry->a_id_info) {
pac->a_id_info = os_malloc(entry->a_id_info_len);
if (pac->a_id_info == NULL) {
eap_fast_free_pac(pac);
return -1;
}
os_memcpy(pac->a_id_info, entry->a_id_info,
entry->a_id_info_len);
pac->a_id_info_len = entry->a_id_info_len;
}
pac->next = data->pac;
data->pac = pac;
return 0;
}
struct eap_fast_read_ctx {
FILE *f;
const char *pos;
const char *end;
};
static int eap_fast_read_line(struct eap_fast_read_ctx *rc, char *buf,
size_t buf_len)
{
char *pos;
if (rc->f) {
if (fgets(buf, buf_len, rc->f) == NULL)
return -1;
} else {
const char *l_end;
size_t len;
if (rc->pos >= rc->end)
return -1;
l_end = rc->pos;
while (l_end < rc->end && *l_end != '\n')
l_end++;
len = l_end - rc->pos;
if (len >= buf_len)
len = buf_len - 1;
os_memcpy(buf, rc->pos, len);
buf[len] = '\0';
rc->pos = l_end + 1;
}
buf[buf_len - 1] = '\0';
pos = buf;
while (*pos != '\0') {
if (*pos == '\n' || *pos == '\r') {
*pos = '\0';
break;
}
pos++;
}
return 0;
}
static u8 * eap_fast_parse_hex(const char *value, size_t *len)
{
int hlen;
u8 *buf;
if (value == NULL)
return NULL;
hlen = os_strlen(value);
if (hlen & 1)
return NULL;
*len = hlen / 2;
buf = os_malloc(*len);
if (buf == NULL)
return NULL;
if (hexstr2bin(value, buf, *len)) {
os_free(buf);
return NULL;
}
return buf;
}
static int eap_fast_load_pac(struct eap_sm *sm, struct eap_fast_data *data,
const char *pac_file)
{
struct eap_fast_read_ctx rc;
struct eap_fast_pac *pac = NULL;
int count = 0;
char *buf, *pos;
const int buf_len = 2048;
int ret = 0, line = 0;
if (pac_file == NULL)
return -1;
os_memset(&rc, 0, sizeof(rc));
if (os_strncmp(pac_file, "blob://", 7) == 0) {
const struct wpa_config_blob *blob;
blob = eap_get_config_blob(sm, pac_file + 7);
if (blob == NULL) {
wpa_printf(MSG_INFO, "EAP-FAST: No PAC blob '%s' - "
"assume no PAC entries have been "
"provisioned", pac_file + 7);
return 0;
}
rc.pos = (char *) blob->data;
rc.end = (char *) blob->data + blob->len;
} else {
rc.f = fopen(pac_file, "r");
if (rc.f == NULL) {
wpa_printf(MSG_INFO, "EAP-FAST: No PAC file '%s' - "
"assume no PAC entries have been "
"provisioned", pac_file);
return 0;
}
}
buf = os_malloc(buf_len);
if (buf == NULL) {
return -1;
}
line++;
if (eap_fast_read_line(&rc, buf, buf_len) < 0 ||
os_strcmp(pac_file_hdr, buf) != 0) {
wpa_printf(MSG_INFO, "EAP-FAST: Unrecognized header line in "
"PAC file '%s'", pac_file);
os_free(buf);
if (rc.f)
fclose(rc.f);
return -1;
}
while (eap_fast_read_line(&rc, buf, buf_len) == 0) {
line++;
pos = os_strchr(buf, '=');
if (pos) {
*pos++ = '\0';
}
if (os_strcmp(buf, "START") == 0) {
if (pac) {
wpa_printf(MSG_INFO, "EAP-FAST: START line "
"without END in '%s:%d'",
pac_file, line);
ret = -1;
break;
}
pac = os_zalloc(sizeof(*pac));
if (pac == NULL) {
wpa_printf(MSG_INFO, "EAP-FAST: No memory for "
"PAC entry");
ret = -1;
break;
}
} else if (os_strcmp(buf, "END") == 0) {
if (pac == NULL) {
wpa_printf(MSG_INFO, "EAP-FAST: END line "
"without START in '%s:%d'",
pac_file, line);
ret = -1;
break;
}
pac->next = data->pac;
data->pac = pac;
pac = NULL;
count++;
} else if (pac && os_strcmp(buf, "PAC-Key") == 0) {
u8 *key;
size_t key_len;
key = eap_fast_parse_hex(pos, &key_len);
if (key == NULL || key_len != EAP_FAST_PAC_KEY_LEN) {
wpa_printf(MSG_INFO, "EAP-FAST: Invalid "
"PAC-Key '%s:%d'", pac_file, line);
ret = -1;
os_free(key);
break;
}
os_memcpy(pac->pac_key, key, EAP_FAST_PAC_KEY_LEN);
os_free(key);
} else if (pac && os_strcmp(buf, "PAC-Opaque") == 0) {
os_free(pac->pac_opaque);
pac->pac_opaque =
eap_fast_parse_hex(pos, &pac->pac_opaque_len);
if (pac->pac_opaque == NULL) {
wpa_printf(MSG_INFO, "EAP-FAST: Invalid "
"PAC-Opaque '%s:%d'",
pac_file, line);
ret = -1;
break;
}
} else if (pac && os_strcmp(buf, "A-ID") == 0) {
os_free(pac->a_id);
pac->a_id = eap_fast_parse_hex(pos, &pac->a_id_len);
if (pac->a_id == NULL) {
wpa_printf(MSG_INFO, "EAP-FAST: Invalid "
"A-ID '%s:%d'", pac_file, line);
ret = -1;
break;
}
} else if (pac && os_strcmp(buf, "I-ID") == 0) {
os_free(pac->i_id);
pac->i_id = eap_fast_parse_hex(pos, &pac->i_id_len);
if (pac->i_id == NULL) {
wpa_printf(MSG_INFO, "EAP-FAST: Invalid "
"I-ID '%s:%d'", pac_file, line);
ret = -1;
break;
}
} else if (pac && os_strcmp(buf, "A-ID-Info") == 0) {
os_free(pac->a_id_info);
pac->a_id_info =
eap_fast_parse_hex(pos, &pac->a_id_info_len);
if (pac->a_id_info == NULL) {
wpa_printf(MSG_INFO, "EAP-FAST: Invalid "
"A-ID-Info '%s:%d'",
pac_file, line);
ret = -1;
break;
}
}
}
if (pac) {
wpa_printf(MSG_INFO, "EAP-FAST: PAC block not terminated with "
"END in '%s'", pac_file);
eap_fast_free_pac(pac);
ret = -1;
}
os_free(buf);
if (rc.f)
fclose(rc.f);
if (ret == 0) {
wpa_printf(MSG_DEBUG, "EAP-FAST: read %d PAC entries from "
"'%s'", count, pac_file);
}
return ret;
}
static void eap_fast_write(char **buf, char **pos, size_t *buf_len,
const char *field, const u8 *data,
size_t len, int txt)
{
size_t i, need;
int ret;
if (data == NULL || *buf == NULL)
return;
need = os_strlen(field) + len * 2 + 30;
if (txt)
need += os_strlen(field) + len + 20;
if (*pos - *buf + need > *buf_len) {
char *nbuf = os_realloc(*buf, *buf_len + need);
if (nbuf == NULL) {
os_free(*buf);
*buf = NULL;
return;
}
*buf = nbuf;
*buf_len += need;
}
ret = os_snprintf(*pos, *buf + *buf_len - *pos, "%s=", field);
if (ret < 0 || ret >= *buf + *buf_len - *pos)
return;
*pos += ret;
*pos += wpa_snprintf_hex(*pos, *buf + *buf_len - *pos, data, len);
ret = os_snprintf(*pos, *buf + *buf_len - *pos, "\n");
if (ret < 0 || ret >= *buf + *buf_len - *pos)
return;
*pos += ret;
if (txt) {
ret = os_snprintf(*pos, *buf + *buf_len - *pos,
"%s-txt=", field);
if (ret < 0 || ret >= *buf + *buf_len - *pos)
return;
*pos += ret;
for (i = 0; i < len; i++) {
ret = os_snprintf(*pos, *buf + *buf_len - *pos,
"%c", data[i]);
if (ret < 0 || ret >= *buf + *buf_len - *pos)
return;
*pos += ret;
}
ret = os_snprintf(*pos, *buf + *buf_len - *pos, "\n");
if (ret < 0 || ret >= *buf + *buf_len - *pos)
return;
*pos += ret;
}
}
static int eap_fast_save_pac(struct eap_sm *sm, struct eap_fast_data *data,
const char *pac_file)
{
FILE *f;
struct eap_fast_pac *pac;
int count = 0, ret;
char *buf, *pos;
size_t buf_len;
if (pac_file == NULL)
return -1;
buf_len = 1024;
pos = buf = os_malloc(buf_len);
if (buf == NULL)
return -1;
ret = os_snprintf(pos, buf + buf_len - pos, "%s\n", pac_file_hdr);
if (ret < 0 || ret >= buf + buf_len - pos) {
os_free(buf);
return -1;
}
pos += ret;
pac = data->pac;
while (pac) {
ret = os_snprintf(pos, buf + buf_len - pos, "START\n");
if (ret < 0 || ret >= buf + buf_len - pos) {
os_free(buf);
return -1;
}
pos += ret;
eap_fast_write(&buf, &pos, &buf_len, "PAC-Key", pac->pac_key,
EAP_FAST_PAC_KEY_LEN, 0);
eap_fast_write(&buf, &pos, &buf_len, "PAC-Opaque",
pac->pac_opaque, pac->pac_opaque_len, 0);
eap_fast_write(&buf, &pos, &buf_len, "PAC-Info", pac->pac_info,
pac->pac_info_len, 0);
eap_fast_write(&buf, &pos, &buf_len, "A-ID", pac->a_id,
pac->a_id_len, 0);
eap_fast_write(&buf, &pos, &buf_len, "I-ID", pac->i_id,
pac->i_id_len, 1);
eap_fast_write(&buf, &pos, &buf_len, "A-ID-Info",
pac->a_id_info, pac->a_id_info_len, 1);
if (buf == NULL) {
wpa_printf(MSG_DEBUG, "EAP-FAST: No memory for PAC "
"data");
return -1;
}
ret = os_snprintf(pos, buf + buf_len - pos, "END\n");
if (ret < 0 || ret >= buf + buf_len - pos) {
os_free(buf);
return -1;
}
pos += ret;
count++;
pac = pac->next;
}
if (os_strncmp(pac_file, "blob://", 7) == 0) {
struct wpa_config_blob *blob;
blob = os_zalloc(sizeof(*blob));
if (blob == NULL) {
os_free(buf);
return -1;
}
blob->data = (u8 *) buf;
blob->len = pos - buf;
buf = NULL;
blob->name = os_strdup(pac_file + 7);
if (blob->name == NULL) {
os_free(blob->data);
os_free(blob);
return -1;
}
eap_set_config_blob(sm, blob);
} else {
f = fopen(pac_file, "w");
if (f == NULL) {
wpa_printf(MSG_INFO, "EAP-FAST: Failed to open PAC "
"file '%s' for writing", pac_file);
os_free(buf);
return -1;
}
fprintf(f, "%s", buf);
os_free(buf);
fclose(f);
}
wpa_printf(MSG_DEBUG, "EAP-FAST: wrote %d PAC entries into '%s'",
count, pac_file);
return 0;
}
static void * eap_fast_init(struct eap_sm *sm)
{
struct eap_fast_data *data;
struct wpa_ssid *config = eap_get_config(sm);
data = os_zalloc(sizeof(*data));
if (data == NULL)
return NULL;
data->fast_version = EAP_FAST_VERSION;
if (config && config->phase1) {
if (os_strstr(config->phase1, "fast_provisioning=1")) {
data->provisioning_allowed = 1;
wpa_printf(MSG_DEBUG, "EAP-FAST: Automatic PAC "
"provisioning is allowed");
}
}
if (config && config->phase2) {
char *start, *pos, *buf;
struct eap_method_type *methods = NULL, *_methods;
u8 method;
size_t num_methods = 0;
start = buf = os_strdup(config->phase2);
if (buf == NULL) {
eap_fast_deinit(sm, data);
return NULL;
}
while (start && *start != '\0') {
int vendor;
pos = os_strstr(start, "auth=");
if (pos == NULL)
break;
if (start != pos && *(pos - 1) != ' ') {
start = pos + 5;
continue;
}
start = pos + 5;
pos = os_strchr(start, ' ');
if (pos)
*pos++ = '\0';
method = eap_get_phase2_type(start, &vendor);
if (vendor == EAP_VENDOR_IETF &&
method == EAP_TYPE_NONE) {
wpa_printf(MSG_ERROR, "EAP-FAST: Unsupported "
"Phase2 method '%s'", start);
} else {
num_methods++;
_methods = os_realloc(
methods,
num_methods * sizeof(*methods));
if (_methods == NULL) {
os_free(methods);
os_free(buf);
eap_fast_deinit(sm, data);
return NULL;
}
methods = _methods;
methods[num_methods - 1].vendor = vendor;
methods[num_methods - 1].method = method;
}
start = pos;
}
os_free(buf);
data->phase2_types = methods;
data->num_phase2_types = num_methods;
}
if (data->phase2_types == NULL) {
data->phase2_types =
eap_get_phase2_types(config, &data->num_phase2_types);
}
if (data->phase2_types == NULL) {
wpa_printf(MSG_ERROR, "EAP-FAST: No Phase2 method available");
eap_fast_deinit(sm, data);
return NULL;
}
wpa_hexdump(MSG_DEBUG, "EAP-FAST: Phase2 EAP types",
(u8 *) data->phase2_types,
data->num_phase2_types * sizeof(struct eap_method_type));
data->phase2_type.vendor = EAP_VENDOR_IETF;
data->phase2_type.method = EAP_TYPE_NONE;
if (eap_tls_ssl_init(sm, &data->ssl, config)) {
wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
eap_fast_deinit(sm, data);
return NULL;
}
/* The local RADIUS server in a Cisco AP does not seem to like empty
* fragments before data, so disable that workaround for CBC.
* TODO: consider making this configurable */
tls_connection_enable_workaround(sm->ssl_ctx, data->ssl.conn);
if (eap_fast_load_pac(sm, data, config->pac_file) < 0) {
eap_fast_deinit(sm, data);
return NULL;
}
if (data->pac == NULL && !data->provisioning_allowed) {
wpa_printf(MSG_INFO, "EAP-FAST: No PAC configured and "
"provisioning disabled");
eap_fast_deinit(sm, data);
return NULL;
}
return data;
}
static void eap_fast_deinit(struct eap_sm *sm, void *priv)
{
struct eap_fast_data *data = priv;
struct eap_fast_pac *pac, *prev;
if (data == NULL)
return;
if (data->phase2_priv && data->phase2_method)
data->phase2_method->deinit(sm, data->phase2_priv);
os_free(data->phase2_types);
os_free(data->key_block_p);
eap_tls_ssl_deinit(sm, &data->ssl);
pac = data->pac;
prev = NULL;
while (pac) {
prev = pac;
pac = pac->next;
eap_fast_free_pac(prev);
}
os_free(data);
}
static int eap_fast_encrypt(struct eap_sm *sm, struct eap_fast_data *data,
int id, const u8 *plain, size_t plain_len,
u8 **out_data, size_t *out_len)
{
int res;
u8 *pos;
struct eap_hdr *resp;
/* TODO: add support for fragmentation, if needed. This will need to
* add TLS Message Length field, if the frame is fragmented. */
resp = os_malloc(sizeof(struct eap_hdr) + 2 + data->ssl.tls_out_limit);
if (resp == NULL)
return 0;
resp->code = EAP_CODE_RESPONSE;
resp->identifier = id;
pos = (u8 *) (resp + 1);
*pos++ = EAP_TYPE_FAST;
*pos++ = data->fast_version;
res = tls_connection_encrypt(sm->ssl_ctx, data->ssl.conn,
plain, plain_len,
pos, data->ssl.tls_out_limit);
if (res < 0) {
wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt Phase 2 "
"data");
os_free(resp);
return 0;
}
*out_len = sizeof(struct eap_hdr) + 2 + res;
resp->length = host_to_be16(*out_len);
*out_data = (u8 *) resp;
return 0;
}
static int eap_fast_phase2_nak(struct eap_fast_data *data,
struct eap_hdr *hdr,
u8 **resp, size_t *resp_len)
{
struct eap_hdr *resp_hdr;
u8 *pos = (u8 *) (hdr + 1);
size_t i;
/* TODO: add support for expanded Nak */
wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: Nak type=%d", *pos);
wpa_hexdump(MSG_DEBUG, "EAP-FAST: Allowed Phase2 EAP types",
(u8 *) data->phase2_types,
data->num_phase2_types * sizeof(struct eap_method_type));
*resp_len = sizeof(struct eap_hdr) + 1;
*resp = os_malloc(*resp_len + data->num_phase2_types);
if (*resp == NULL)
return -1;
resp_hdr = (struct eap_hdr *) (*resp);
resp_hdr->code = EAP_CODE_RESPONSE;
resp_hdr->identifier = hdr->identifier;
pos = (u8 *) (resp_hdr + 1);
*pos++ = EAP_TYPE_NAK;
for (i = 0; i < data->num_phase2_types; i++) {
if (data->phase2_types[i].vendor == EAP_VENDOR_IETF &&
data->phase2_types[i].method < 256) {
(*resp_len)++;
*pos++ = data->phase2_types[i].method;
}
}
resp_hdr->length = host_to_be16(*resp_len);
return 0;
}
static int eap_fast_derive_msk(struct eap_fast_data *data)
{
/* Derive EAP Master Session Keys (section 5.4) */
sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
"Session Key Generating Function", (u8 *) "", 0,
data->key_data, EAP_FAST_KEY_LEN);
wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Derived key (MSK)",
data->key_data, EAP_FAST_KEY_LEN);
sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
"Extended Session Key Generating Function",
(u8 *) "", 0, data->emsk, EAP_EMSK_LEN);
wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Derived key (EMSK)",
data->emsk, EAP_EMSK_LEN);
data->success = 1;
return 0;
}
static int eap_fast_set_tls_master_secret(struct eap_sm *sm,
struct eap_fast_data *data,
const u8 *tls, size_t tls_len)
{
struct tls_keys keys;
u8 master_secret[48], *seed;
const u8 *server_random;
size_t seed_len, server_random_len;
if (data->tls_master_secret_set || !data->current_pac ||
tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
keys.client_random == NULL) {
return 0;
}
wpa_hexdump(MSG_DEBUG, "EAP-FAST: client_random",
keys.client_random, keys.client_random_len);
/* TLS master secret is needed before TLS library has processed this
* message which includes both ServerHello and an encrypted handshake
* message, so we need to parse server_random from this message before
* passing it to TLS library.
*
* Example TLS packet header:
* (16 03 01 00 2a 02 00 00 26 03 01 <32 bytes server_random>)
* Content Type: Handshake: 0x16
* Version: TLS 1.0 (0x0301)
* Lenghth: 42 (0x002a)
* Handshake Type: Server Hello: 0x02
* Length: 38 (0x000026)
* Version TLS 1.0 (0x0301)
* Random: 32 bytes
*/
if (tls_len < 43 || tls[0] != 0x16 ||
tls[1] != 0x03 || tls[2] != 0x01 ||
tls[5] != 0x02 || tls[9] != 0x03 || tls[10] != 0x01) {
wpa_hexdump(MSG_DEBUG, "EAP-FAST: unrecognized TLS "
"ServerHello", tls, tls_len);
return -1;
}
server_random = tls + 11;
server_random_len = 32;
wpa_hexdump(MSG_DEBUG, "EAP-FAST: server_random",
server_random, server_random_len);
seed_len = keys.client_random_len + server_random_len;
seed = os_malloc(seed_len);
if (seed == NULL)
return -1;
os_memcpy(seed, server_random, server_random_len);
os_memcpy(seed + server_random_len,
keys.client_random, keys.client_random_len);
wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: T-PRF seed", seed, seed_len);
wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: PAC-Key",
data->current_pac->pac_key, EAP_FAST_PAC_KEY_LEN);
/* master_secret = T-PRF(PAC-Key, "PAC to master secret label hash",
* server_random + client_random, 48) */
sha1_t_prf(data->current_pac->pac_key, EAP_FAST_PAC_KEY_LEN,
"PAC to master secret label hash",
seed, seed_len, master_secret, sizeof(master_secret));
os_free(seed);
wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: TLS pre-master-secret",
master_secret, sizeof(master_secret));
data->tls_master_secret_set = 1;
return tls_connection_set_master_key(sm->ssl_ctx, data->ssl.conn,
master_secret,
sizeof(master_secret));
}
static u8 * eap_fast_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
char *label, size_t len)
{
struct tls_keys keys;
u8 *rnd = NULL, *out;
int block_size;
block_size = tls_connection_get_keyblock_size(sm->ssl_ctx, data->conn);
if (block_size < 0)
return NULL;
out = os_malloc(block_size + len);
if (out == NULL)
return NULL;
if (tls_connection_prf(sm->ssl_ctx, data->conn, label, 1, out,
block_size + len) == 0) {
os_memmove(out, out + block_size, len);
return out;
}
if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
goto fail;
rnd = os_malloc(keys.client_random_len + keys.server_random_len);
if (rnd == NULL)
goto fail;
os_memcpy(rnd, keys.server_random, keys.server_random_len);
os_memcpy(rnd + keys.server_random_len, keys.client_random,
keys.client_random_len);
wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: master_secret for key "
"expansion", keys.master_key, keys.master_key_len);
if (tls_prf(keys.master_key, keys.master_key_len,
label, rnd, keys.client_random_len +
keys.server_random_len, out, block_size + len))
goto fail;
os_free(rnd);
os_memmove(out, out + block_size, len);
return out;
fail:
os_free(rnd);
os_free(out);
return NULL;
}
static void eap_fast_derive_key_auth(struct eap_sm *sm,
struct eap_fast_data *data)
{
u8 *sks;
/* draft-cam-winget-eap-fast-05.txt:
* 5.1 EAP-FAST Authentication Phase 1: Key Derivations
* Extra key material after TLS key_block: session_ket_seed[40]
*/
sks = eap_fast_derive_key(sm, &data->ssl, "key expansion",
EAP_FAST_SKS_LEN);
if (sks == NULL) {
wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
"session_key_seed");
return;
}
/*
* draft-cam-winget-eap-fast-05.txt, 5.2:
* S-IMCK[0] = session_key_seed
*/
wpa_hexdump_key(MSG_DEBUG,
"EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
sks, EAP_FAST_SKS_LEN);
data->simck_idx = 0;
os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
os_free(sks);
}