1
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
95
96
97
98
99
100
104
105
106
107
108
109
114
115
116
117
118
119
120
128
129
130
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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
232
233
234
235
236
237
238
242
243
244
245
246
247
248
249
250
251
252
253
259
260
261
262
263
264
265
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
343
344
345
346
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
377
378
386
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
412
413
414
415
418
419
425
426
427
428
429
430
431
437
438
439
440
441
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
482
483
484
485
486
487
488
489
490
491
497
498
499
500
501
502
503
504
505
506
507
508
509
516
517
518
519
520
521
522
523
530
531
532
533
534
535
536
537
538
539
548
549
550
551
554
555
556
557
558
559
560
561
562
563
564
565
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
587
588
589
590
591
592
593
594
595
596
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
628
629
630
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
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
684
685
686
687
688
689
690
693
694
695
696
697
698
699
700
701
702
703
704
705
707
708
709
710
711
712
713
714
715
716
717
718
719
720
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
741
742
743
744
745
746
747
748
749
750
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
812
813
814
815
816
830
831
832
835
838
841
844
847
851
852
853
857
858
859
860
861
862
868
869
870
871
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
922
923
928
929
930
931
932
936
937
938
939
940
941
942
943
944
945
954
966
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1149
1150
1151
1152
1153
1159
1160
1161
1166
1167
1168
1169
1170
1171
1172
1173
1177
1178
1179
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1198
1199
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1233
1234
1235
1236
1241
1242
1243
1244
1245
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1272
1273
1274
1279
1280
1281
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1306
1307
1308
1313
1314
1315
1316
1317
1318
1327
1328
1329
1330
1331
1332
1333
1334
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1401
1402
1403
1404
1405
1410
1411
1417
1418
1419
1420
1421
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1443
1444
1445
1446
1447
1448
1449
1450
1451
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1524
1525
1526
1527
1536
1537
1538
1539
1540
1541
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1577
1578
1579
1580
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1615
1616
1617
1618
1619
1620
1628
1629
1630
1632
1637
1638
1639
1640
1641
1642
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1704
1707
1710
1713
1716
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
/* ... */
#include "utils/includes.h"
#ifdef EAP_TTLS
#include "utils/common.h"
#include "crypto/ms_funcs.h"
#include "crypto/sha1.h"
#include "crypto/tls.h"
#include "eap_peer/chap.h"
#include "eap_peer/eap.h"
#include "eap_peer/eap_ttls.h"
#include "eap_peer/mschapv2.h"
#include "eap_peer/eap_i.h"
#include "eap_peer/eap_tls_common.h"
#include "eap_peer/eap_config.h"
#include "eap_peer/eap_methods.h"12 includes
#define EAP_TTLS_VERSION 0
static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
struct eap_ttls_data {
struct eap_ssl_data ssl;
int ttls_version;
const struct eap_method *phase2_method;
void *phase2_priv;
int phase2_success;
int phase2_start;
enum phase2_types {
EAP_TTLS_PHASE2_EAP,
EAP_TTLS_PHASE2_MSCHAPV2,
EAP_TTLS_PHASE2_MSCHAP,
EAP_TTLS_PHASE2_PAP,
EAP_TTLS_PHASE2_CHAP
}{ ... } phase2_type;
struct eap_method_type phase2_eap_type;
struct eap_method_type *phase2_eap_types;
size_t num_phase2_eap_types;
u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
int auth_response_valid;
u8 master_key[MSCHAPV2_MASTER_KEY_LEN];
u8 ident;
int resuming;
int reauth;
u8 *key_data;
u8 *session_id;
size_t id_len;
struct wpabuf *pending_phase2_req;
struct wpabuf *pending_resp;
#ifdef EAP_TNC
int ready_for_tnc;
int tnc_started;/* ... */
#endif
}{ ... };
static void * eap_ttls_init(struct eap_sm *sm)
{
struct eap_ttls_data *data;
struct eap_peer_config *config = eap_get_config(sm);
data = (struct eap_ttls_data *)os_zalloc(sizeof(*data));
if (data == NULL)
return NULL;
data->ttls_version = EAP_TTLS_VERSION;
data->phase2_type = EAP_TTLS_PHASE2_EAP;
if (config && config->phase2) {
if (os_strstr(config->phase2, "autheap=")) {
data->phase2_type = EAP_TTLS_PHASE2_EAP;
}{...} else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
}{...} else if (os_strstr(config->phase2, "auth=MSCHAP")) {
data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
}{...} else if (os_strstr(config->phase2, "auth=PAP")) {
data->phase2_type = EAP_TTLS_PHASE2_PAP;
}{...} else if (os_strstr(config->phase2, "auth=CHAP")) {
data->phase2_type = EAP_TTLS_PHASE2_CHAP;
}{...}
}{...}
if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
if (eap_peer_select_phase2_methods(config, "autheap=",
&data->phase2_eap_types,
&data->num_phase2_eap_types)
< 0) {
eap_ttls_deinit(sm, data);
return NULL;
}{...}
data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
data->phase2_eap_type.method = EAP_TYPE_NONE;
}{...}
if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_TTLS)) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to initialize SSL.");
eap_ttls_deinit(sm, data);
return NULL;
}{...}
return data;
}{ ... }
static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
struct eap_ttls_data *data)
{
if (data->phase2_priv && data->phase2_method) {
data->phase2_method->deinit(sm, data->phase2_priv);
data->phase2_method = NULL;
data->phase2_priv = NULL;
}{...}
}{ ... }
static void eap_ttls_free_key(struct eap_ttls_data *data)
{
if (data->key_data) {
bin_clear_free(data->key_data, EAP_TLS_KEY_LEN);
data->key_data = NULL;
}{...}
}{ ... }
static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
{
struct eap_ttls_data *data = priv;
if (data == NULL)
return;
eap_ttls_phase2_eap_deinit(sm, data);
os_free(data->phase2_eap_types);
eap_peer_tls_ssl_deinit(sm, &data->ssl);
eap_ttls_free_key(data);
os_free(data->session_id);
wpabuf_clear_free(data->pending_phase2_req);
wpabuf_clear_free(data->pending_resp);
os_free(data);
}{ ... }
static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
int mandatory, size_t len)
{
struct ttls_avp_vendor *avp;
u8 flags;
size_t hdrlen;
avp = (struct ttls_avp_vendor *) avphdr;
flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
if (vendor_id) {
flags |= AVP_FLAGS_VENDOR;
hdrlen = sizeof(*avp);
avp->vendor_id = host_to_be32(vendor_id);
}{...} else {
hdrlen = sizeof(struct ttls_avp);
}{...}
avp->avp_code = host_to_be32(avp_code);
avp->avp_length = host_to_be32((flags << 24) | (u32) (hdrlen + len));
return avphdr + hdrlen;
}{ ... }
static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
u32 vendor_id, int mandatory,
const u8 *data, size_t len)
{
u8 *pos;
pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
os_memcpy(pos, data, len);
pos += len;
AVP_PAD(start, pos);
return pos;
}{ ... }
static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
int mandatory)
{
struct wpabuf *msg;
u8 *avp, *pos;
msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
if (msg == NULL) {
wpabuf_clear_free(*resp);
*resp = NULL;
return -1;
}{...}
avp = wpabuf_mhead(msg);
pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
pos += wpabuf_len(*resp);
AVP_PAD(avp, pos);
wpabuf_clear_free(*resp);
wpabuf_put(msg, pos - avp);
*resp = msg;
return 0;
}{ ... }
static int eap_ttls_v0_derive_key(struct eap_sm *sm,
struct eap_ttls_data *data)
{
const char *label;
const u8 eap_tls13_context[1] = { EAP_TYPE_TTLS };
const u8 *context = NULL;
size_t context_len = 0;
if (data->ssl.tls_v13) {
label = "EXPORTER_EAP_TLS_Key_Material";
context = eap_tls13_context;
context_len = sizeof(eap_tls13_context);
}{...} else {
label = "ttls keying material";
}{...}
eap_ttls_free_key(data);
data->key_data = eap_peer_tls_derive_key(sm, &data->ssl, label,
context, context_len,
EAP_TLS_KEY_LEN +
EAP_EMSK_LEN);
if (!data->key_data) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to derive key");
return -1;
}{...}
wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
data->key_data, EAP_TLS_KEY_LEN);
wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived EMSK",
data->key_data + EAP_TLS_KEY_LEN,
EAP_EMSK_LEN);
os_free(data->session_id);
data->session_id = eap_peer_tls_derive_session_id(sm, &data->ssl,
EAP_TYPE_TTLS,
&data->id_len);
if (data->session_id) {
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Derived Session-Id",
data->session_id, data->id_len);
}{...} else {
wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to derive Session-Id");
}{...}
return 0;
}{ ... }
#ifndef CONFIG_FIPS
static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
struct eap_ttls_data *data, size_t len)
{
return eap_peer_tls_derive_key(sm, &data->ssl, "ttls challenge",
NULL, 0, len);
}{ ... }
/* ... */#endif
static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
int vendor, enum eap_type method)
{
size_t i;
for (i = 0; i < data->num_phase2_eap_types; i++) {
if (data->phase2_eap_types[i].vendor != vendor ||
data->phase2_eap_types[i].method != method)
continue;
data->phase2_eap_type.vendor =
data->phase2_eap_types[i].vendor;
data->phase2_eap_type.method =
data->phase2_eap_types[i].method;
wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
"Phase 2 EAP vendor %d method %d",
data->phase2_eap_type.vendor,
data->phase2_eap_type.method);
break;
}{...}
}{ ... }
static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
struct eap_hdr *hdr, size_t len,
struct wpabuf **resp)
{
struct wpabuf msg;
struct eap_method_ret iret;
os_memset(&iret, 0, sizeof(iret));
wpabuf_set(&msg, hdr, len);
*resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
&msg);
if ((iret.methodState == METHOD_DONE ||
iret.methodState == METHOD_MAY_CONT) &&
(iret.decision == DECISION_UNCOND_SUCC ||
iret.decision == DECISION_COND_SUCC ||
iret.decision == DECISION_FAIL)) {
ret->methodState = iret.methodState;
ret->decision = iret.decision;
}{...}
return 0;
}{ ... }
static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
struct eap_hdr *hdr, size_t len,
int vendor, enum eap_type method,
struct wpabuf **resp)
{
#ifdef EAP_TNC
if (data->tnc_started && data->phase2_method &&
data->phase2_priv &&
vendor == EAP_VENDOR_IETF && method == EAP_TYPE_TNC &&
data->phase2_eap_type.method == EAP_TYPE_TNC)
return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
resp);
if (data->ready_for_tnc && !data->tnc_started &&
vendor == EAP_VENDOR_IETF && method == EAP_TYPE_TNC) {
wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
"EAP method");
data->tnc_started = 1;
}{...}
if (data->tnc_started) {
if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
data->phase2_eap_type.method == EAP_TYPE_TNC) {
wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
"type %d for TNC", method);
return -1;
}{...}
data->phase2_eap_type.vendor = vendor;
data->phase2_eap_type.method = method;
wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
"Phase 2 EAP vendor %d method %d (TNC)",
data->phase2_eap_type.vendor,
data->phase2_eap_type.method);
if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
eap_ttls_phase2_eap_deinit(sm, data);
}{...}
#endif/* ... */
if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
data->phase2_eap_type.method == EAP_TYPE_NONE)
eap_ttls_phase2_select_eap_method(data, vendor, method);
if (vendor != data->phase2_eap_type.vendor ||
method != data->phase2_eap_type.method ||
(vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE)) {
if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
data->num_phase2_eap_types,
hdr, resp))
return -1;
return 0;
}{...}
if (data->phase2_priv == NULL) {
data->phase2_method = eap_peer_get_eap_method(vendor, method);
if (data->phase2_method) {
sm->init_phase2 = 1;
data->phase2_priv = data->phase2_method->init(sm);
sm->init_phase2 = 0;
}{...}
}{...}
if (data->phase2_priv == NULL || data->phase2_method == NULL) {
wpa_printf(MSG_INFO,
"EAP-TTLS: failed to initialize Phase 2 EAP method %u:%u",
vendor, method);
return -1;
}{...}
return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
}{ ... }
static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
struct eap_hdr *hdr,
struct wpabuf **resp)
{
size_t len = be_to_host16(hdr->length);
u8 *pos;
struct eap_peer_config *config = eap_get_config(sm);
if (len <= sizeof(struct eap_hdr)) {
wpa_printf(MSG_INFO, "EAP-TTLS: too short "
"Phase 2 request (len=%lu)", (unsigned long) len);
return -1;
}{...}
pos = (u8 *) (hdr + 1);
wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
switch (*pos) {
case EAP_TYPE_IDENTITY:
*resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
break;...
case EAP_TYPE_EXPANDED:
if (len < sizeof(struct eap_hdr) + 8) {
wpa_printf(MSG_INFO,
"EAP-TTLS: Too short Phase 2 request (expanded header) (len=%lu)",
(unsigned long) len);
return -1;
}{...}
if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
WPA_GET_BE24(pos + 1),
WPA_GET_BE32(pos + 4),
resp) < 0)
return -1;
break;...
default:
if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
EAP_VENDOR_IETF, *pos,
resp) < 0)
return -1;
break;...
}{...}
if (*resp == NULL &&
(config->pending_req_identity || config->pending_req_password ||
config->pending_req_otp)) {
return 0;
}{...}
if (*resp == NULL)
return -1;
wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
*resp);
return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
}{ ... }
static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
struct wpabuf **resp)
{
#ifdef CONFIG_FIPS
wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPV2 not supported in FIPS build");
return -1;/* ... */
#else
#ifdef EAP_MSCHAPv2
struct wpabuf *msg;
u8 *buf, *pos, *challenge, *peer_challenge;
const u8 *identity, *password;
size_t identity_len, password_len;
int pwhash;
wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
identity = eap_get_config_identity(sm, &identity_len);
password = eap_get_config_password2(sm, &password_len, &pwhash);
if (identity == NULL || password == NULL)
return -1;
msg = wpabuf_alloc(identity_len + 1000);
if (msg == NULL) {
wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
return -1;
}{...}
pos = buf = wpabuf_mhead(msg);
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
identity, identity_len);
challenge = eap_ttls_implicit_challenge(
sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
if (challenge == NULL) {
wpabuf_clear_free(msg);
wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
"implicit challenge");
return -1;
}{...}
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
RADIUS_VENDOR_ID_MICROSOFT, 1,
challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
RADIUS_VENDOR_ID_MICROSOFT, 1,
EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
*pos++ = data->ident;
*pos++ = 0;
if (os_get_random(pos, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN) < 0) {
os_free(challenge);
wpabuf_clear_free(msg);
wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to get "
"random data for peer challenge");
return -1;
}{...}
peer_challenge = pos;
pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
os_memset(pos, 0, 8);
pos += 8;
if (mschapv2_derive_response(identity, identity_len, password,
password_len, pwhash, challenge,
peer_challenge, pos, data->auth_response,
data->master_key)) {
os_free(challenge);
wpabuf_clear_free(msg);
wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
"response");
return -1;
}{...}
data->auth_response_valid = 1;
pos += 24;
os_free(challenge);
AVP_PAD(buf, pos);
wpabuf_put(msg, pos - buf);
*resp = msg;
if (sm->workaround) {
/* ... */
wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
"allow success without tunneled response");
ret->methodState = METHOD_MAY_CONT;
ret->decision = DECISION_COND_SUCC;
}{...}
return 0;/* ... */
#else
printf("[Debug] Set EEEEE \n");
wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
return -1;/* ... */
#endif /* ... */
#endif
}{ ... }
static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
struct wpabuf **resp)
{
#ifdef CONFIG_FIPS
wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAP not supported in FIPS build");
return -1;/* ... */
#else
struct wpabuf *msg;
u8 *buf, *pos, *challenge;
const u8 *identity, *password;
size_t identity_len, password_len;
int pwhash;
wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
identity = eap_get_config_identity(sm, &identity_len);
password = eap_get_config_password2(sm, &password_len, &pwhash);
if (identity == NULL || password == NULL)
return -1;
msg = wpabuf_alloc(identity_len + 1000);
if (msg == NULL) {
wpa_printf(MSG_ERROR,
"EAP-TTLS/MSCHAP: Failed to allocate memory");
return -1;
}{...}
pos = buf = wpabuf_mhead(msg);
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
identity, identity_len);
challenge = eap_ttls_implicit_challenge(
sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
if (challenge == NULL) {
wpabuf_clear_free(msg);
wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
"implicit challenge");
return -1;
}{...}
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
RADIUS_VENDOR_ID_MICROSOFT, 1,
challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
RADIUS_VENDOR_ID_MICROSOFT, 1,
EAP_TTLS_MSCHAP_RESPONSE_LEN);
data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
*pos++ = data->ident;
*pos++ = 1;
os_memset(pos, 0, 24);
pos += 24;
if (pwhash) {
if (challenge_response(challenge, password, pos)) {
wpa_printf(MSG_ERROR,
"EAP-TTLS/MSCHAP: Failed derive password hash");
wpabuf_clear_free(msg);
return -1;
}{...}
wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
password, 16);
}{...} else {
if (nt_challenge_response(challenge, password, password_len,
pos)) {
wpa_printf(MSG_ERROR,
"EAP-TTLS/MSCHAP: Failed derive password");
wpabuf_clear_free(msg);
return -1;
}{...}
wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
password, password_len);
}{...}
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
pos += 24;
os_free(challenge);
AVP_PAD(buf, pos);
wpabuf_put(msg, pos - buf);
*resp = msg;
/* ... */
ret->methodState = METHOD_DONE;
ret->decision = DECISION_COND_SUCC;
return 0;/* ... */
#endif
}{ ... }
static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
struct wpabuf **resp)
{
struct wpabuf *msg;
u8 *buf, *pos;
size_t pad;
const u8 *identity, *password;
size_t identity_len, password_len;
wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
identity = eap_get_config_identity(sm, &identity_len);
password = eap_get_config_password(sm, &password_len);
if (identity == NULL || password == NULL)
return -1;
msg = wpabuf_alloc(identity_len + password_len + 100);
if (msg == NULL) {
wpa_printf(MSG_ERROR,
"EAP-TTLS/PAP: Failed to allocate memory");
return -1;
}{...}
pos = buf = wpabuf_mhead(msg);
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
identity, identity_len);
/* ... */
pad = password_len == 0 ? 16 : (16 - (password_len & 15)) & 15;
pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
password_len + pad);
os_memcpy(pos, password, password_len);
pos += password_len;
os_memset(pos, 0, pad);
pos += pad;
AVP_PAD(buf, pos);
wpabuf_put(msg, pos - buf);
*resp = msg;
/* ... */
ret->methodState = METHOD_DONE;
ret->decision = DECISION_COND_SUCC;
return 0;
}{ ... }
static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
struct wpabuf **resp)
{
#ifdef CONFIG_FIPS
wpa_printf(MSG_ERROR, "EAP-TTLS: CHAP not supported in FIPS build");
return -1;/* ... */
#else
struct wpabuf *msg;
u8 *buf, *pos, *challenge;
const u8 *identity, *password;
size_t identity_len, password_len;
wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
identity = eap_get_config_identity(sm, &identity_len);
password = eap_get_config_password(sm, &password_len);
if (identity == NULL || password == NULL)
return -1;
msg = wpabuf_alloc(identity_len + 1000);
if (msg == NULL) {
wpa_printf(MSG_ERROR,
"EAP-TTLS/CHAP: Failed to allocate memory");
return -1;
}{...}
pos = buf = wpabuf_mhead(msg);
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
identity, identity_len);
challenge = eap_ttls_implicit_challenge(
sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
if (challenge == NULL) {
wpabuf_clear_free(msg);
wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
"implicit challenge");
return -1;
}{...}
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
1 + EAP_TTLS_CHAP_PASSWORD_LEN);
data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
*pos++ = data->ident;
chap_md5(data->ident, password, password_len, challenge,
EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
identity, identity_len);
wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
password, password_len);
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
pos, EAP_TTLS_CHAP_PASSWORD_LEN);
pos += EAP_TTLS_CHAP_PASSWORD_LEN;
os_free(challenge);
AVP_PAD(buf, pos);
wpabuf_put(msg, pos - buf);
*resp = msg;
/* ... */
ret->methodState = METHOD_DONE;
ret->decision = DECISION_COND_SUCC;
return 0;/* ... */
#endif
}{ ... }
static int eap_ttls_phase2_request(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
struct eap_hdr *hdr,
struct wpabuf **resp)
{
int res = 0;
size_t len;
enum phase2_types phase2_type = data->phase2_type;
#ifdef EAP_TNC
if (data->tnc_started) {
printf("[debug] set phase2_type \n");
wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
phase2_type = EAP_TTLS_PHASE2_EAP;
}{...}
#endif/* ... */
if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
phase2_type == EAP_TTLS_PHASE2_PAP ||
phase2_type == EAP_TTLS_PHASE2_CHAP) {
if (eap_get_config_identity(sm, &len) == NULL) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Identity not configured");
if (eap_get_config_password(sm, &len) == NULL)
printf("[Debug] Return because no identity EAP_TTLS_PHASE2_MSCHAPV2 EAP_TTLS_PHASE2_MSCHAP\n");
return 0;
}{...}
if (eap_get_config_password(sm, &len) == NULL) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Password not configured");
printf("[Debug] Return because no password EAP_TTLS_PHASE2_MSCHAPV2 EAP_TTLS_PHASE2_MSCHAP\n");
return 0;
}{...}
}{...}
switch (phase2_type) {
case EAP_TTLS_PHASE2_EAP:
res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
break;...
case EAP_TTLS_PHASE2_MSCHAPV2:
res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
break;...
case EAP_TTLS_PHASE2_MSCHAP:
res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
break;...
case EAP_TTLS_PHASE2_PAP:
res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
break;...
case EAP_TTLS_PHASE2_CHAP:
res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
break;...
default:
wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
res = -1;
break;...
}{...}
if (res < 0) {
ret->methodState = METHOD_DONE;
ret->decision = DECISION_FAIL;
}{...}
return res;
}{ ... }
struct ttls_parse_avp {
u8 *mschapv2;
u8 *eapdata;
size_t eap_len;
int mschapv2_error;
}{ ... };
static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
struct ttls_parse_avp *parse)
{
wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
if (parse->eapdata == NULL) {
parse->eapdata = os_malloc(dlen);
if (parse->eapdata == NULL) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to allocate "
"memory for Phase 2 EAP data");
return -1;
}{...}
os_memcpy(parse->eapdata, dpos, dlen);
parse->eap_len = dlen;
}{...} else {
u8 *neweap = (u8 *)os_realloc(parse->eapdata, parse->eap_len + dlen);
if (neweap == NULL) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to allocate "
"memory for Phase 2 EAP data");
return -1;
}{...}
os_memcpy(neweap + parse->eap_len, dpos, dlen);
parse->eapdata = neweap;
parse->eap_len += dlen;
}{...}
return 0;
}{ ... }
static int eap_ttls_parse_avp(u8 *pos, size_t left,
struct ttls_parse_avp *parse)
{
struct ttls_avp *avp;
u32 avp_code, avp_length, vendor_id = 0;
u8 avp_flags, *dpos;
size_t dlen;
avp = (struct ttls_avp *) pos;
avp_code = be_to_host32(avp->avp_code);
avp_length = be_to_host32(avp->avp_length);
avp_flags = (avp_length >> 24) & 0xff;
avp_length &= 0xffffff;
wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
"length=%d", (int) avp_code, avp_flags,
(int) avp_length);
if (avp_length > left) {
wpa_printf(MSG_ERROR, "EAP-TTLS: AVP overflow "
"(len=%d, left=%lu) - dropped",
(int) avp_length, (unsigned long) left);
return -1;
}{...}
if (avp_length < sizeof(*avp)) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Invalid AVP length %d",
avp_length);
return -1;
}{...}
dpos = (u8 *) (avp + 1);
dlen = avp_length - sizeof(*avp);
if (avp_flags & AVP_FLAGS_VENDOR) {
if (dlen < 4) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Vendor AVP underflow");
return -1;
}{...}
vendor_id = WPA_GET_BE32(dpos);
wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
(int) vendor_id);
dpos += 4;
dlen -= 4;
}{...}
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
return -1;
}{...} else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
/* ... */
wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
dpos, dlen);
}{...} else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
dpos, dlen);
if (dlen != 43) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Unexpected "
"MS-CHAP2-Success length "
"(len=%lu, expected 43)",
(unsigned long) dlen);
return -1;
}{...}
parse->mschapv2 = dpos;
}{...} else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
dpos, dlen);
parse->mschapv2_error = 1;
}{...} else if (avp_flags & AVP_FLAGS_MANDATORY) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Unsupported mandatory AVP "
"code %d vendor_id %d - dropped",
(int) avp_code, (int) vendor_id);
return -1;
}{...} else {
wpa_printf(MSG_INFO, "EAP-TTLS: Ignoring unsupported AVP "
"code %d vendor_id %d",
(int) avp_code, (int) vendor_id);
}{...}
return avp_length;
}{ ... }
static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
struct ttls_parse_avp *parse)
{
u8 *pos;
size_t left, pad;
int avp_length;
pos = wpabuf_mhead(in_decrypted);
left = wpabuf_len(in_decrypted);
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
if (left < sizeof(struct ttls_avp)) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Too short Phase 2 AVP frame"
" len=%lu expected %lu or more - dropped",
(unsigned long) left,
(unsigned long) sizeof(struct ttls_avp));
return -1;
}{...}
os_memset(parse, 0, sizeof(*parse));
while (left > 0) {
avp_length = eap_ttls_parse_avp(pos, left, parse);
if (avp_length < 0)
return -1;
pad = (4 - (avp_length & 3)) & 3;
pos += avp_length + pad;
if (left < avp_length + pad)
left = 0;
else
left -= avp_length + pad;
}{...}
return 0;
}{ ... }
static u8 * eap_ttls_fake_identity_request(void)
{
struct eap_hdr *hdr;
u8 *buf;
wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
"Phase 2 - use fake EAP-Request Identity");
buf = os_malloc(sizeof(*hdr) + 1);
if (buf == NULL) {
wpa_printf(MSG_ERROR, "EAP-TTLS: failed to allocate "
"memory for fake EAP-Identity Request");
return NULL;
}{...}
hdr = (struct eap_hdr *) buf;
hdr->code = EAP_CODE_REQUEST;
hdr->identifier = 0;
hdr->length = host_to_be16(sizeof(*hdr) + 1);
buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
return buf;
}{ ... }
static int eap_ttls_encrypt_response(struct eap_sm *sm,
struct eap_ttls_data *data,
struct wpabuf *resp, u8 identifier,
struct wpabuf **out_data)
{
if (resp == NULL)
return 0;
if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
data->ttls_version, identifier,
resp, out_data)) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to encrypt a Phase 2 frame");
wpabuf_clear_free(resp);
return -1;
}{...}
wpabuf_clear_free(resp);
return 0;
}{ ... }
static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
struct ttls_parse_avp *parse,
struct wpabuf **resp)
{
struct eap_hdr *hdr;
size_t len;
if (parse->eapdata == NULL) {
wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
"packet - dropped");
return -1;
}{...}
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
parse->eapdata, parse->eap_len);
hdr = (struct eap_hdr *) parse->eapdata;
if (parse->eap_len < sizeof(*hdr)) {
wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
"frame (len=%lu, expected %lu or more) - dropped",
(unsigned long) parse->eap_len,
(unsigned long) sizeof(*hdr));
return -1;
}{...}
len = be_to_host16(hdr->length);
if (len > parse->eap_len) {
wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
"EAP frame (EAP hdr len=%lu, EAP data len in "
"AVP=%lu)",
(unsigned long) len,
(unsigned long) parse->eap_len);
return -1;
}{...}
wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
"identifier=%d length=%lu",
hdr->code, hdr->identifier, (unsigned long) len);
switch (hdr->code) {
case EAP_CODE_REQUEST:
if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
"processing failed");
return -1;
}{...}
break;...
default:
wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
"Phase 2 EAP header", hdr->code);
return -1;...
}{...}
return 0;
}{ ... }
static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
struct ttls_parse_avp *parse)
{
#ifdef EAP_MSCHAPv2
if (parse->mschapv2_error) {
wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Received "
"MS-CHAP-Error - failed");
ret->methodState = METHOD_DONE;
ret->decision = DECISION_FAIL;
return 1;
}{...}
if (parse->mschapv2 == NULL) {
#ifdef EAP_TNC
if (data->phase2_success && parse->eapdata) {
/* ... */
return 1;
}{...}
#endif/* ... */
wpa_printf(MSG_ERROR, "EAP-TTLS: no MS-CHAP2-Success AVP "
"received for Phase2 MSCHAPV2");
return -1;
}{...}
if (parse->mschapv2[0] != data->ident) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Ident mismatch for Phase 2 "
"MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
parse->mschapv2[0], data->ident);
return -1;
}{...}
if (!data->auth_response_valid ||
mschapv2_verify_auth_response(data->auth_response,
parse->mschapv2 + 1, 42)) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Invalid authenticator "
"response in Phase 2 MSCHAPV2 success request");
return -1;
}{...}
wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 "
"authentication succeeded");
ret->methodState = METHOD_DONE;
ret->decision = DECISION_UNCOND_SUCC;
data->phase2_success = 1;
/* ... */
return 1;/* ... */
#else
wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
return -1;/* ... */
#endif
}{ ... }
#ifdef EAP_TNC
static int eap_ttls_process_tnc_start(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
struct ttls_parse_avp *parse,
struct wpabuf **resp)
{
if (parse->eapdata == NULL) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 received "
"unexpected tunneled data (no EAP)");
return -1;
}{...}
if (!data->ready_for_tnc) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 received "
"EAP after non-EAP, but not ready for TNC");
return -1;
}{...}
wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
"non-EAP method");
data->tnc_started = 1;
if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
return -1;
return 0;
}{...}
/* ... */#endif
static int eap_ttls_process_decrypted(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
u8 identifier,
struct ttls_parse_avp *parse,
struct wpabuf *in_decrypted,
struct wpabuf **out_data)
{
struct wpabuf *resp = NULL;
int res;
enum phase2_types phase2_type = data->phase2_type;
struct eap_peer_config *config = eap_get_config(sm);
#ifdef EAP_TNC
if (data->tnc_started)
phase2_type = EAP_TTLS_PHASE2_EAP;/* ... */
#endif
switch (phase2_type) {
case EAP_TTLS_PHASE2_EAP:
if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
0)
return -1;
break;...
case EAP_TTLS_PHASE2_MSCHAPV2:
res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
#ifdef EAP_TNC
if (res == 1 && parse->eapdata && data->phase2_success) {
/* ... */
ret->methodState = METHOD_MAY_CONT;
data->ready_for_tnc = 1;
if (eap_ttls_process_tnc_start(sm, data, ret, parse,
&resp) == 0)
break;
}{...}
#endif/* ... */
return res;...
case EAP_TTLS_PHASE2_MSCHAP:
case EAP_TTLS_PHASE2_PAP:
case EAP_TTLS_PHASE2_CHAP:
#ifdef EAP_TNC
if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
0)
return -1;
break;/* ... */
#else
/* ... */
wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
"tunneled data");
return -1;/* ... */
#endif ...
}{...}
if (resp) {
if (eap_ttls_encrypt_response(sm, data, resp, identifier,
out_data) < 0)
return -1;
}{...} else if (config->pending_req_identity ||
config->pending_req_password ||
config->pending_req_otp ||
config->pending_req_new_password) {
wpabuf_clear_free(data->pending_phase2_req);
data->pending_phase2_req = wpabuf_dup(in_decrypted);
}{...}
return 0;
}{ ... }
static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
u8 identifier,
struct wpabuf **out_data)
{
int retval = 0;
struct eap_hdr *hdr;
struct wpabuf *resp;
hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
if (hdr == NULL) {
ret->methodState = METHOD_DONE;
ret->decision = DECISION_FAIL;
return -1;
}{...}
resp = NULL;
if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
wpa_printf(MSG_ERROR, "EAP-TTLS: Phase2 Request "
"processing failed");
retval = -1;
}{...} else {
struct eap_peer_config *config = eap_get_config(sm);
if (resp == NULL &&
(config->pending_req_identity ||
config->pending_req_password ||
config->pending_req_otp ||
config->pending_req_new_password)) {
/* ... */
wpabuf_clear_free(data->pending_phase2_req);
data->pending_phase2_req = wpabuf_alloc(0);
}{...}
retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
out_data);
}{...}
os_free(hdr);
if (retval < 0) {
ret->methodState = METHOD_DONE;
ret->decision = DECISION_FAIL;
}{...}
return retval;
}{ ... }
static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
struct eap_method_ret *ret, u8 identifier,
struct wpabuf **out_data)
{
data->phase2_start = 0;
/* ... */
if (data->reauth &&
tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
"skip phase 2");
*out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
data->ttls_version);
ret->methodState = METHOD_DONE;
ret->decision = DECISION_UNCOND_SUCC;
data->phase2_success = 1;
return 0;
}{...}
return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
out_data);
}{ ... }
static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
struct eap_method_ret *ret, u8 identifier,
const struct wpabuf *in_data,
struct wpabuf **out_data)
{
struct wpabuf *in_decrypted = NULL;
int retval = 0;
struct ttls_parse_avp parse;
os_memset(&parse, 0, sizeof(parse));
wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
" Phase 2",
in_data ? (unsigned long) wpabuf_len(in_data) : 0);
if (data->pending_phase2_req) {
wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
"skip decryption and use old data");
eap_peer_tls_reset_input(&data->ssl);
in_decrypted = data->pending_phase2_req;
data->pending_phase2_req = NULL;
if (wpabuf_len(in_decrypted) == 0) {
wpabuf_clear_free(in_decrypted);
return eap_ttls_implicit_identity_request(
sm, data, ret, identifier, out_data);
}{...}
goto continue_req;
}{...}
if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
data->phase2_start) {
start:
return eap_ttls_phase2_start(sm, data, ret, identifier,
out_data);
}{...}
if (in_data == NULL || wpabuf_len(in_data) == 0) {
return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
data->ttls_version,
identifier, NULL, out_data);
}{...}
retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
if (retval)
goto done;
if (wpabuf_len(in_decrypted) == 0) {
wpabuf_free(in_decrypted);
goto start;
}{...}
if (data->ssl.tls_v13 && wpabuf_len(in_decrypted) == 1 &&
*wpabuf_head_u8(in_decrypted) == 0) {
wpa_printf(MSG_DEBUG,
"EAP-TLS: ACKing protected success indication (appl data 0x00)");
eap_peer_tls_reset_output(&data->ssl);
wpabuf_free(in_decrypted);
return 1;
}{...}
continue_req:
data->phase2_start = 0;
if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
retval = -1;
goto done;
}{...}
retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
&parse, in_decrypted, out_data);
done:
wpabuf_clear_free(in_decrypted);
os_free(parse.eapdata);
if (retval < 0) {
ret->methodState = METHOD_DONE;
ret->decision = DECISION_FAIL;
}{...}
return retval;
}{ ... }
static int eap_ttls_process_handshake(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret,
u8 identifier,
const u8 *in_data, size_t in_len,
struct wpabuf **out_data)
{
int res;
if (sm->waiting_ext_cert_check && data->pending_resp) {
struct eap_peer_config *config = eap_get_config(sm);
if (config->pending_ext_cert_check == EXT_CERT_CHECK_GOOD) {
wpa_printf(MSG_DEBUG,
"EAP-TTLS: External certificate check succeeded - continue handshake");
*out_data = data->pending_resp;
data->pending_resp = NULL;
sm->waiting_ext_cert_check = 0;
return 0;
}{...}
if (config->pending_ext_cert_check == EXT_CERT_CHECK_BAD) {
wpa_printf(MSG_DEBUG,
"EAP-TTLS: External certificate check failed - force authentication failure");
ret->methodState = METHOD_DONE;
ret->decision = DECISION_FAIL;
sm->waiting_ext_cert_check = 0;
return 0;
}{...}
wpa_printf(MSG_DEBUG,
"EAP-TTLS: Continuing to wait external server certificate validation");
return 0;
}{...}
res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
data->ttls_version, identifier,
in_data, in_len, out_data);
if (res < 0) {
wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS processing failed");
ret->methodState = METHOD_DONE;
ret->decision = DECISION_FAIL;
return -1;
}{...}
if (sm->waiting_ext_cert_check) {
wpa_printf(MSG_DEBUG,
"EAP-TTLS: Waiting external server certificate validation");
wpabuf_clear_free(data->pending_resp);
data->pending_resp = *out_data;
*out_data = NULL;
return 0;
}{...}
if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
"Phase 2");
if (data->resuming) {
wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
"skip Phase 2");
ret->decision = DECISION_COND_SUCC;
ret->methodState = METHOD_MAY_CONT;
}{...}
data->phase2_start = 1;
eap_ttls_v0_derive_key(sm, data);
if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
if (eap_ttls_decrypt(sm, data, ret, identifier,
NULL, out_data)) {
wpa_printf(MSG_ERROR, "EAP-TTLS: "
"failed to process early "
"start for Phase 2");
}{...}
res = 0;
}{...}
data->resuming = 0;
}{...}
if (res == 2) {
struct wpabuf msg;
/* ... */
wpabuf_clear_free(data->pending_phase2_req);
data->pending_phase2_req = *out_data;
*out_data = NULL;
wpabuf_set(&msg, in_data, in_len);
res = eap_ttls_decrypt(sm, data, ret, identifier, &msg,
out_data);
}{...}
return res;
}{ ... }
static void eap_ttls_check_auth_status(struct eap_sm *sm,
struct eap_ttls_data *data,
struct eap_method_ret *ret)
{
if (ret->methodState == METHOD_DONE) {
ret->allowNotifications = FALSE;
if (ret->decision == DECISION_UNCOND_SUCC ||
ret->decision == DECISION_COND_SUCC) {
wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
"completed successfully");
data->phase2_success = 1;
#ifdef EAP_TNC
if (!data->ready_for_tnc && !data->tnc_started) {
/* ... */
ret->methodState = METHOD_MAY_CONT;
data->ready_for_tnc = 1;
}{...}
#endif/* ... */
}{...}
}{...} else if (ret->methodState == METHOD_MAY_CONT &&
(ret->decision == DECISION_UNCOND_SUCC ||
ret->decision == DECISION_COND_SUCC)) {
wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
"completed successfully (MAY_CONT)");
data->phase2_success = 1;
}{...}
}{ ... }
static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
struct eap_method_ret *ret,
const struct wpabuf *reqData)
{
size_t left;
int res;
u8 flags, id;
struct wpabuf *resp;
const u8 *pos;
struct eap_ttls_data *data = priv;
pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
reqData, &left, &flags);
if (pos == NULL)
return NULL;
id = eap_get_id(reqData);
if (flags & EAP_TLS_FLAGS_START) {
wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own "
"ver=%d)", flags & EAP_TLS_VERSION_MASK,
data->ttls_version);
/* ... */
left = 0;
}{...}
resp = NULL;
if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
!data->resuming) {
struct wpabuf msg;
wpabuf_set(&msg, pos, left);
res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
}{...} else {
res = eap_ttls_process_handshake(sm, data, ret, id,
pos, left, &resp);
}{...}
eap_ttls_check_auth_status(sm, data, ret);
/* ... */
if (res == 1) {
wpabuf_clear_free(resp);
return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
data->ttls_version);
}{...}
return resp;
}{ ... }
static bool eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
{
struct eap_ttls_data *data = priv;
return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
data->phase2_success;
}{ ... }
static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
{
struct eap_ttls_data *data = priv;
wpabuf_clear_free(data->pending_phase2_req);
data->pending_phase2_req = NULL;
wpabuf_clear_free(data->pending_resp);
data->pending_resp = NULL;
#ifdef EAP_TNC
data->ready_for_tnc = 0;
data->tnc_started = 0;/* ... */
#endif
}{ ... }
static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
{
struct eap_ttls_data *data = priv;
eap_ttls_free_key(data);
os_free(data->session_id);
data->session_id = NULL;
if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
os_free(data);
return NULL;
}{...}
if (data->phase2_priv && data->phase2_method &&
data->phase2_method->init_for_reauth)
data->phase2_method->init_for_reauth(sm, data->phase2_priv);
data->phase2_start = 0;
data->phase2_success = 0;
data->resuming = 1;
data->reauth = 1;
return priv;
}{ ... }
static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
size_t buflen, int verbose)
{
struct eap_ttls_data *data = priv;
int len, ret;
len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
ret = snprintf(buf + len, buflen - len,
"EAP-TTLSv%d Phase2 method=",
data->ttls_version);
if (ret < 0 || (size_t) ret >= buflen - len)
return len;
len += ret;
switch (data->phase2_type) {
case EAP_TTLS_PHASE2_EAP:
ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
data->phase2_method ?
data->phase2_method->name : "?");
break;...
case EAP_TTLS_PHASE2_MSCHAPV2:
ret = snprintf(buf + len, buflen - len, "MSCHAPV2\n");
break;...
case EAP_TTLS_PHASE2_MSCHAP:
ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
break;...
case EAP_TTLS_PHASE2_PAP:
ret = os_snprintf(buf + len, buflen - len, "PAP\n");
break;...
case EAP_TTLS_PHASE2_CHAP:
ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
break;...
default:
ret = 0;
break;...
}{...}
if (ret < 0 || (size_t) ret >= buflen - len)
return len;
len += ret;
return len;
}{ ... }
static bool eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
{
struct eap_ttls_data *data = priv;
return data->key_data != NULL && data->phase2_success;
}{ ... }
static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
{
struct eap_ttls_data *data = priv;
u8 *key;
if (data->key_data == NULL || !data->phase2_success)
return NULL;
key = os_malloc(EAP_TLS_KEY_LEN);
if (key == NULL)
return NULL;
*len = EAP_TLS_KEY_LEN;
os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
return key;
}{ ... }
static u8 * eap_ttls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
{
struct eap_ttls_data *data = priv;
u8 *id;
if (data->session_id == NULL || !data->phase2_success)
return NULL;
id = os_malloc(data->id_len);
if (id == NULL)
return NULL;
*len = data->id_len;
os_memcpy(id, data->session_id, data->id_len);
return id;
}{ ... }
static u8 * eap_ttls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
{
struct eap_ttls_data *data = priv;
u8 *key;
if (data->key_data == NULL)
return NULL;
key = os_malloc(EAP_EMSK_LEN);
if (key == NULL)
return NULL;
*len = EAP_EMSK_LEN;
os_memcpy(key, data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
return key;
}{ ... }
int eap_peer_ttls_register(void)
{
struct eap_method *eap;
int ret;
eap = eap_peer_method_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS,
"TTLS");
if (eap == NULL)
return -1;
eap->init = eap_ttls_init;
eap->deinit = eap_ttls_deinit;
eap->process = eap_ttls_process;
eap->isKeyAvailable = eap_ttls_isKeyAvailable;
eap->getKey = eap_ttls_getKey;
eap->getSessionId = eap_ttls_get_session_id;
eap->get_status = eap_ttls_get_status;
eap->has_reauth_data = eap_ttls_has_reauth_data;
eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
eap->init_for_reauth = eap_ttls_init_for_reauth;
eap->get_emsk = eap_ttls_get_emsk;
ret = eap_peer_method_register(eap);
if (ret)
eap_peer_method_free(eap);
return ret;
}{ ... }
/* ... */#endif