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
77
78
82
83
87
88
89
90
91
94
97
100
103
106
109
112
115
118
121
123
124
125
126
127
128
129
132
135
138
141
145
150
151
152
155
159
164
165
166
169
171
172
173
174
175
176
177
178
179
187
188
189
190
191
192
196
197
198
199
200
201
202
203
204
205
206
207
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
248
249
250
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
358
359
360
361
362
363
364
370
371
375
376
377
378
379
380
381
382
383
384
385
386
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
426
427
428
429
430
431
432
433
434
435
440
441
459
460
461
462
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
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
535
536
537
538
539
540
541
542
546
547
548
549
550
551
552
553
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
654
655
656
657
658
659
660
661
662
663
664
665
666
667
671
672
673
674
679
680
681
682
683
687
688
698
699
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
741
742
743
744
745
746
747
748
752
753
754
758
759
763
764
765
766
767
772
773
779
780
781
782
787
788
789
790
791
792
793
794
795
796
797
798
799
800
806
807
808
809
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
846
847
848
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
874
875
876
877
878
879
886
896
897
898
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
929
930
937
938
939
940
941
942
956
957
958
959
960
961
962
963
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1011
1012
1013
1014
1015
1016
1017
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1039
1040
1043
1044
1045
1046
1047
1051
1059
1060
1064
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1092
1093
1094
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1113
1114
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1133
1134
1137
1138
1139
1140
1141
1142
1143
1144
1150
1151
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
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
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1276
1277
1278
1284
1285
1286
1304
1305
1306
1309
1310
1311
1312
1313
1314
1315
1323
1324
1328
1329
1332
1333
1336
1337
1338
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1361
1362
1363
1364
1365
1366
1367
1368
1374
1375
1376
1377
1378
1379
1380
1381
1390
1393
1394
1395
1396
1407
1408
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1456
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1477
1478
1479
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1522
1523
1527
1528
1534
1535
1536
1537
1538
1539
1540
1541
1546
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
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
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1645
1646
1647
1648
1649
1650
1651
1654
1655
1656
1657
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1692
1693
1694
1695
1696
1697
1702
1703
1704
1705
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1733
1734
1736
1745
1746
1747
1748
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1771
1772
1773
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
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1880
1886
1887
1888
1889
1890
1891
1892
1893
1894
1900
1901
1902
1903
1907
1908
1909
1910
1911
1912
1913
1914
1915
1920
1921
1927
1928
1929
1935
1936
1937
1938
1939
1940
1941
1942
1943
1955
1956
1957
1958
1959
1968
1969
1970
1971
1976
1977
1981
1982
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2016
2021
2022
2023
2024
2025
2026
2027
2032
2033
2038
2039
2040
2045
2046
2047
2048
2049
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2107
2108
2109
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2160
2161
2162
2163
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2207
2208
2209
2210
2211
2212
2213
2214
2217
2218
2219
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2321
2322
2323
2324
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2347
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2369
2370
2390
2394
2395
2396
2397
2398
2399
2402
2403
2404
2405
2411
2412
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2444
2445
2446
2447
2448
2449
2450
2451
2452
2455
2456
2457
2458
2459
2460
2461
2462
2463
2467
2468
2469
2470
2471
2472
2473
2474
2487
2488
2494
2495
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2547
2548
2554
2555
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2583
2584
2585
2586
2587
2588
2589
2590
2594
2595
2596
2597
2598
2599
2600
2601
2602
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2620
2621
2622
2623
2624
2625
2626
2627
2631
2632
2636
2640
2641
2642
2643
2644
2664
2671
2673
2674
2675
2676
2679
2680
2681
2682
2683
2684
2685
2688
2689
2690
2691
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2717
2718
2719
2720
2721
2722
2723
2728
2729
2730
2731
2732
2733
2734
2735
2736
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2795
2796
2797
2798
2802
2803
2804
2805
2806
2807
2808
2812
2813
2814
2815
2818
2819
2820
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2866
2867
2870
2871
2875
2876
2877
2887
2888
2892
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2909
2910
2911
2912
2913
2914
2915
2919
2920
2921
2922
2926
2927
2928
2929
2930
2931
2932
2933
2934
2937
2938
2939
2940
2941
2944
2945
2946
2947
2950
2951
2952
2953
2954
2955
2956
2957
2958
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
/* ... */
#include "utils/includes.h"
#include "utils/common.h"
#include "utils/eloop.h"
#include "rsn_supp/wpa.h"
#include "rsn_supp/pmksa_cache.h"
#include "rsn_supp/wpa_i.h"
#include "common/eapol_common.h"
#include "common/ieee802_11_defs.h"
#include "rsn_supp/wpa_ie.h"
#include "esp_wpas_glue.h"
#include "esp_wifi_driver.h"
#include "crypto/crypto.h"
#include "crypto/sha1.h"
#include "crypto/aes_wrap.h"
#include "crypto/ccmp.h"
#include "crypto/sha256.h"
#include "esp_rom_sys.h"
#include "common/bss.h"
#include "esp_common_i.h"
#include "esp_owe_i.h"
#include "common/sae.h"
#include "esp_eap_client_i.h"
#include "esp_wpa3_i.h"23 includes
/* ... */
#define WPA_4_4_HANDSHAKE_BIT (1<<13)
#define WPA_GROUP_HANDSHAKE_BIT (1<<14)
struct wpa_sm gWpaSm;
#define WPA_TX_MSG_BUFF_MAXLEN 200
#define MIN_DH_LEN(len) (len < 4)
#define ASSOC_IE_LEN 24 + 2 + PMKID_LEN + RSN_SELECTOR_LEN
#define MAX_EAPOL_RETRIES 3
u8 assoc_ie_buf[ASSOC_IE_LEN+2];
void set_assoc_ie(u8 * assoc_buf);
static int wpa_sm_get_key(uint8_t *ifx, int *alg, u8 *addr, int *key_idx, u8 *key, size_t key_len, enum key_flag key_flag);
void wpa_set_passphrase(char * passphrase, u8 *ssid, size_t ssid_len);
void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm);
static bool wpa_supplicant_gtk_in_use(struct wpa_sm *sm, struct wpa_gtk_data *gd);
void wpa_supplicant_stop_countermeasures(void *data, void *user_ctx);
static inline enum wpa_states wpa_sm_get_state(struct wpa_sm *sm)
{
return sm->wpa_state;;
}{...}
static inline void wpa_sm_cancel_auth_timeout(struct wpa_sm *sm)
{
}{ ... }
void eapol_sm_notify_eap_success(Boolean success)
{
}{ ... }
wifi_cipher_type_t cipher_type_map_supp_to_public(unsigned wpa_cipher)
{
switch (wpa_cipher) {
case WPA_CIPHER_NONE:
return WIFI_CIPHER_TYPE_NONE;
...
case WPA_CIPHER_WEP40:
return WIFI_CIPHER_TYPE_WEP40;
...
case WPA_CIPHER_WEP104:
return WIFI_CIPHER_TYPE_WEP104;
...
case WPA_CIPHER_TKIP:
return WIFI_CIPHER_TYPE_TKIP;
...
case WPA_CIPHER_CCMP:
return WIFI_CIPHER_TYPE_CCMP;
...
case WPA_CIPHER_CCMP|WPA_CIPHER_TKIP:
return WIFI_CIPHER_TYPE_TKIP_CCMP;
...
case WPA_CIPHER_AES_128_CMAC:
return WIFI_CIPHER_TYPE_AES_CMAC128;
...
case WPA_CIPHER_SMS4:
return WIFI_CIPHER_TYPE_SMS4;
...
case WPA_CIPHER_GCMP:
return WIFI_CIPHER_TYPE_GCMP;
...
case WPA_CIPHER_GCMP_256:
return WIFI_CIPHER_TYPE_GCMP256;
...
default:
return WIFI_CIPHER_TYPE_UNKNOWN;...
}{...}
}{ ... }
unsigned cipher_type_map_public_to_supp(wifi_cipher_type_t cipher)
{
switch (cipher) {
case WIFI_CIPHER_TYPE_NONE:
return WPA_CIPHER_NONE;
...
case WIFI_CIPHER_TYPE_WEP40:
return WPA_CIPHER_WEP40;
...
case WIFI_CIPHER_TYPE_WEP104:
return WPA_CIPHER_WEP104;
...
case WIFI_CIPHER_TYPE_TKIP:
return WPA_CIPHER_TKIP;
...
case WIFI_CIPHER_TYPE_CCMP:
return WPA_CIPHER_CCMP;
#ifdef CONFIG_GCMP...
case WIFI_CIPHER_TYPE_GCMP:
return WPA_CIPHER_GCMP;
...
case WIFI_CIPHER_TYPE_GCMP256:
return WPA_CIPHER_GCMP_256;/* ... */
#endif
case WIFI_CIPHER_TYPE_TKIP_CCMP:
return WPA_CIPHER_CCMP|WPA_CIPHER_TKIP;
...
case WIFI_CIPHER_TYPE_AES_CMAC128:
return WPA_CIPHER_AES_128_CMAC;
#ifdef CONFIG_GMAC...
case WIFI_CIPHER_TYPE_AES_GMAC128:
return WPA_CIPHER_BIP_GMAC_128;
...
case WIFI_CIPHER_TYPE_AES_GMAC256:
return WPA_CIPHER_BIP_GMAC_256;/* ... */
#endif
case WIFI_CIPHER_TYPE_SMS4:
return WPA_CIPHER_SMS4;
...
default:
return WPA_CIPHER_NONE;...
}{...}
}{ ... }
#ifdef CONFIG_ESP_WIFI_ENTERPRISE_SUPPORT
static bool is_wpa2_enterprise_connection(void)
{
uint8_t authmode;
if (esp_wifi_sta_prof_is_wpa2_internal()) {
authmode = esp_wifi_sta_get_prof_authmode_internal();
if ((authmode == WPA2_AUTH_ENT) ||
(authmode == WPA2_AUTH_ENT_SHA256) ||
(authmode == WPA2_AUTH_ENT_SHA384_SUITE_B)) {
return true;
}{...}
}{...}
return false;
}{ ... }
/* ... */#endif
const u8 * wpa_sm_get_auth_addr(struct wpa_sm *sm)
{
return sm->bssid;
}{ ... }
/* ... */
static inline int wpa_sm_ether_send(struct wpa_sm *sm, const u8 *dest, u16 proto,
const u8 *data, size_t data_len)
{
return wpa_ether_send(sm, dest, proto, data, data_len);
}{ ... }
/* ... */
int wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck, size_t kck_len,
int ver, const u8 *dest, u16 proto,
u8 *msg, size_t msg_len, u8 *key_mic)
{
int ret = -1;
if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
#ifndef ESP_SUPPLICANT
/* ... */
if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
wpa_printf(MSG_DEBUG, "WPA: Failed to read BSSID for "
"EAPOL-Key destination address");
}{...} else {
dest = sm->bssid;
wpa_printf(MSG_DEBUG, "WPA: Use BSSID (" MACSTR
") as the destination for EAPOL-Key",
MAC2STR(dest));
}{...}
#else/* ... */
return ret;
#endif
}{...}
if (key_mic &&
wpa_eapol_key_mic(kck, kck_len, sm->key_mgmt, ver, msg, msg_len,
key_mic)) {
wpa_msg(NULL, MSG_ERROR,
"WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
ver, sm->key_mgmt);
goto out;
}{...}
wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", kck, kck_len);
wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", key_mic, wpa_mic_len(sm->key_mgmt, sm->pmk_len));
wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
return wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
out:
return ret;
}{ ... }
/* ... */
static void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
{
size_t mic_len, hdrlen, rlen;
struct wpa_eapol_key *reply;
struct wpa_eapol_key_192 *reply192;
int key_info, ver;
u8 *rbuf, *key_mic;
if (wpa_use_akm_defined(sm->key_mgmt))
ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
else if (wpa_key_mgmt_ft(sm->key_mgmt) || wpa_key_mgmt_sha256(sm->key_mgmt))
ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
else
ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
hdrlen, &rlen, (void *) &reply);
if (rbuf == NULL)
return;
reply192 = (struct wpa_eapol_key_192 *) reply;
reply->type = sm->proto == WPA_PROTO_RSN ?
EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
key_info = WPA_KEY_INFO_REQUEST | ver;
if (sm->ptk_set)
key_info |= WPA_KEY_INFO_SECURE;
if (sm->ptk_set && mic_len)
key_info |= WPA_KEY_INFO_MIC;
if (error)
key_info |= WPA_KEY_INFO_ERROR;
if (pairwise)
key_info |= WPA_KEY_INFO_KEY_TYPE;
WPA_PUT_BE16(reply->key_info, key_info);
WPA_PUT_BE16(reply->key_length, 0);
os_memcpy(reply->replay_counter, sm->request_counter,
WPA_REPLAY_COUNTER_LEN);
inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
if (mic_len == 24)
WPA_PUT_BE16(reply192->key_data_length, 0);
else
WPA_PUT_BE16(reply->key_data_length, 0);
if (!(key_info & WPA_KEY_INFO_MIC))
key_mic = NULL;
else
key_mic = reply192->key_mic;
wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key Request (error=%d "
"pairwise=%d ptk_set=%d len=%lu)",
error, pairwise, sm->ptk_set, (unsigned long) rlen);
wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver, wpa_sm_get_auth_addr(sm),
ETH_P_EAPOL, rbuf, rlen, key_mic);
wpa_sm_free_eapol(rbuf);
}{ ... }
static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
void *ctx, enum pmksa_free_reason reason)
{
struct wpa_sm *sm = ctx;
int deauth = 0;
wpa_printf( MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
MACSTR " reason=%d", MAC2STR(entry->aa), reason);
if (sm->cur_pmksa == entry) {
wpa_printf( MSG_DEBUG,
"RSN: %s current PMKSA entry",
reason == PMKSA_REPLACE ? "replaced" : "removed");
pmksa_cache_clear_current(sm);
/* ... */
if (reason != PMKSA_REPLACE)
deauth = 1;
}{...}
if (reason == PMKSA_EXPIRE &&
(sm->pmk_len == entry->pmk_len &&
os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
wpa_printf( MSG_DEBUG,
"RSN: deauthenticating due to expired PMK");
pmksa_cache_clear_current(sm);
deauth = 1;
}{...}
if (deauth) {
os_memset(sm->pmk, 0, sizeof(sm->pmk));
wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
}{...}
}{ ... }
static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
const unsigned char *src_addr,
const u8 *pmkid)
{
int abort_cached = 0;
if (pmkid && !sm->cur_pmksa) {
/* ... */
sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
NULL);
if (sm->cur_pmksa) {
wpa_printf(MSG_DEBUG,
"RSN: found matching PMKID from PMKSA cache");
}{...} else {
wpa_printf( MSG_DEBUG,
"RSN: no matching PMKID found");
abort_cached = 1;
}{...}
}{...}
if (pmkid && sm->cur_pmksa &&
os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
wpa_sm_set_pmk_from_pmksa(sm);
wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
sm->pmk, sm->pmk_len);
#ifdef CONFIG_IEEE80211R
sm->xxkey_len = 0;
#endif
}{...} else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) {
int res = 0, pmk_len;
if (wpa_key_mgmt_sha384(sm->key_mgmt))
pmk_len = PMK_LEN_SUITE_B_192;
else
pmk_len = PMK_LEN;
if(!sm->pmk_len) {
res = -1;
}{...}
if (res == 0) {
struct rsn_pmksa_cache_entry *sa = NULL;
wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
"machines", sm->pmk, pmk_len);
sm->pmk_len = pmk_len;
if (sm->proto == WPA_PROTO_RSN &&
!wpa_key_mgmt_suite_b(sm->key_mgmt) &&
!wpa_key_mgmt_ft(sm->key_mgmt)) {
sa = pmksa_cache_add(sm->pmksa, sm->pmk, pmk_len,
NULL, NULL, 0, src_addr, sm->own_addr,
sm->network_ctx, sm->key_mgmt);
}{...}
if (!sm->cur_pmksa && pmkid &&
pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
{
wpa_printf( MSG_DEBUG,
"RSN: the new PMK matches with the "
"PMKID");
abort_cached = 0;
}{...} else if (sa && !sm->cur_pmksa && pmkid) {
/* ... */
wpa_printf( MSG_INFO,
"RSN: PMKID mismatch - authentication server may have derived different MSK?!");
return -1;
}{...}
if (!sm->cur_pmksa)
sm->cur_pmksa = sa;
}{...} else {
wpa_printf( MSG_WARNING,
"WPA: Failed to get master session key from "
"EAPOL state machines - key handshake "
"aborted");
if (sm->cur_pmksa) {
wpa_printf( MSG_DEBUG,
"RSN: Cancelled PMKSA caching "
"attempt");
sm->cur_pmksa = NULL;
abort_cached = 1;
}{...} else if (!abort_cached) {
return -1;
}{...}
}{...}
}{...}
if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
!wpa_key_mgmt_suite_b(sm->key_mgmt) &&
!wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
{
u8 *buf;
size_t buflen;
wpa_printf( MSG_DEBUG,
"RSN: no PMKSA entry found - trigger "
"full EAP authentication");
buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
NULL, 0, &buflen, NULL);
if (buf) {
wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
buf, buflen);
wpa_sm_free_eapol(buf);
return -2;
}{...}
return -1;
}{...}
return 0;
}{ ... }
/* ... */
int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
const struct wpa_eapol_key *key,
int ver, const u8 *nonce,
const u8 *wpa_ie, size_t wpa_ie_len,
struct wpa_ptk *ptk)
{
size_t mic_len, hdrlen, rlen;
struct wpa_eapol_key *reply;
struct wpa_eapol_key_192 *reply192;
u8 *rsn_ie_buf = NULL;
u8 *rbuf, *key_mic;
if (wpa_ie == NULL) {
wpa_printf(MSG_ERROR, "WPA: No wpa_ie set - cannot "
"generate msg 2/4");
return -1;
}{...}
#ifdef CONFIG_IEEE80211R
if (wpa_key_mgmt_ft(sm->key_mgmt)) {
int res;
wpa_hexdump(MSG_DEBUG, "WPA: WPA IE before FT processing",
wpa_ie, wpa_ie_len);
/* ... */
rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
sm->assoc_resp_ies_len);
if (rsn_ie_buf == NULL)
return -1;
os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
sm->pmk_r1_name);
if (res < 0) {
os_free(rsn_ie_buf);
return -1;
}{...}
wpa_hexdump(MSG_DEBUG,
"WPA: WPA IE after PMKID[PMKR1Name] addition into RSNE",
rsn_ie_buf, wpa_ie_len);
if (sm->assoc_resp_ies) {
wpa_hexdump(MSG_DEBUG, "WPA: Add assoc_resp_ies",
sm->assoc_resp_ies,
sm->assoc_resp_ies_len);
os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
sm->assoc_resp_ies_len);
wpa_ie_len += sm->assoc_resp_ies_len;
}{...}
wpa_ie = rsn_ie_buf;
}{...}
#endif/* ... */
wpa_hexdump(MSG_MSGDUMP, "WPA: WPA IE for msg 2/4\n", wpa_ie, wpa_ie_len);
mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
NULL, hdrlen + wpa_ie_len,
&rlen, (void *) &reply);
if (rbuf == NULL) {
os_free(rsn_ie_buf);
return -1;
}{...}
reply192 = (struct wpa_eapol_key_192 *) reply;
reply->type = sm->proto == WPA_PROTO_RSN ?
EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
WPA_PUT_BE16(reply->key_info,
ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
if (sm->proto == WPA_PROTO_RSN)
WPA_PUT_BE16(reply->key_length, 0);
else
memcpy(reply->key_length, key->key_length, 2);
memcpy(reply->replay_counter, key->replay_counter,
WPA_REPLAY_COUNTER_LEN);
key_mic = reply192->key_mic;
if (mic_len == 24) {
WPA_PUT_BE16(reply192->key_data_length, wpa_ie_len);
os_memcpy(reply192 + 1, wpa_ie, wpa_ie_len);
}{...} else {
WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
}{...}
os_free(rsn_ie_buf);
os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
wpa_printf(MSG_DEBUG, "WPA Send EAPOL-Key 2/4");
wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst, ETH_P_EAPOL,
rbuf, rlen, key_mic);
wpa_sm_free_eapol(rbuf);
return 0;
}{ ... }
static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
{
#ifdef CONFIG_IEEE80211R
if (wpa_key_mgmt_ft(sm->key_mgmt))
return wpa_derive_ptk_ft(sm, src_addr, key, ptk);/* ... */
#endif
return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
sm->own_addr, sm->bssid, sm->snonce,
key->key_nonce, ptk, sm->key_mgmt,
sm->pairwise_cipher);
}{ ... }
void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
const unsigned char *src_addr,
const struct wpa_eapol_key *key,
u16 ver, const u8 *key_data,
size_t key_data_len)
{
struct wpa_eapol_ie_parse ie;
struct wpa_ptk *ptk;
int res;
u8 *kde, *kde_buf = NULL;
size_t kde_len;
#ifdef CONFIG_ESP_WIFI_ENTERPRISE_SUPPORT
if (is_wpa2_enterprise_connection()) {
wpa2_ent_eap_state_t state = eap_client_get_eap_state();
if (state == WPA2_ENT_EAP_STATE_IN_PROGRESS) {
wpa_printf(MSG_INFO, "EAP Success has not been processed yet."
" Drop EAPOL message.");
return;
}{...}
}{...}
#endif/* ... */
wpa_sm_set_state(WPA_FIRST_HALF_4WAY_HANDSHAKE);
wpa_printf(MSG_DEBUG, "WPA 1/4-Way Handshake");
memset(&ie, 0, sizeof(ie));
if (sm->proto == WPA_PROTO_RSN) {
wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
key_data, key_data_len);
if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
goto failed;
if (ie.pmkid) {
wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
"Authenticator", ie.pmkid, PMKID_LEN);
}{...}
}{...}
res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
if (res == -2) {
wpa_printf(MSG_DEBUG, "RSN: Do not reply to msg 1/4 - "
"requesting full EAP authentication");
return;
}{...}
if (res)
goto failed;
#ifdef CONFIG_ESP_WIFI_ENTERPRISE_SUPPORT
if (is_wpa2_enterprise_connection()) {
pmksa_cache_set_current(sm, NULL, sm->bssid, 0, 0);
}{...}
#endif/* ... */
if (sm->renew_snonce) {
if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
wpa_printf(MSG_DEBUG, "WPA: Failed to get random data for SNonce");
goto failed;
}{...}
sm->renew_snonce = 0;
wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
sm->snonce, WPA_NONCE_LEN);
}{...}
/* ... */
ptk = &sm->tptk;
wpa_derive_ptk(sm, src_addr, key, ptk);
sm->tptk_set = 1;
sm->ptk_set = 0;
sm->key_install = true;
kde = sm->assoc_wpa_ie;
kde_len = sm->assoc_wpa_ie_len;
kde_buf = os_malloc(kde_len +
sm->assoc_rsnxe_len);
if (!kde_buf)
goto failed;
os_memcpy(kde_buf, kde, kde_len);
kde = kde_buf;
if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) {
os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len);
kde_len += sm->assoc_rsnxe_len;
}{...}
if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
kde, kde_len, ptk))
goto failed;
os_free(kde_buf);
memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
return;
failed:
os_free(kde_buf);
wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
}{ ... }
static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
{
struct wpa_sm *sm = eloop_ctx;
wpa_printf(MSG_DEBUG, "WPA: Request PTK rekeying");
wpa_sm_key_request(sm, 0, 1);
}{ ... }
static int wpa_supplicant_install_ptk(struct wpa_sm *sm, enum key_flag key_flag)
{
int keylen;
enum wpa_alg alg;
if (sm->ptk.installed) {
wpa_printf(MSG_DEBUG, "WPA: Do not re-install same PTK to the driver");
return 0;
}{...}
wpa_printf(MSG_DEBUG, "WPA: Installing PTK to the driver.");
if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
wpa_printf(MSG_DEBUG, "WPA: Pairwise Cipher Suite: NONE - do not use pairwise keys");
return 0;
}{...}
if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
wpa_printf(MSG_DEBUG, "WPA: Unsupported pairwise cipher %d", sm->pairwise_cipher);
return -1;
}{...}
alg = wpa_cipher_to_alg(sm->pairwise_cipher);
keylen = wpa_cipher_key_len(sm->pairwise_cipher);
if (alg == WIFI_WPA_ALG_NONE) {
wpa_printf(MSG_DEBUG, "WPA: Pairwise Cipher Suite: "
"NONE - do not use pairwise keys");
return 0;
}{...}
if (wpa_sm_set_key(&(sm->install_ptk), alg, sm->bssid, 0, 1, (sm->install_ptk).seq, WPA_KEY_RSC_LEN,
sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE | key_flag) < 0) {
wpa_printf(MSG_DEBUG, "WPA: Failed to set PTK to the "
"driver (alg=%d keylen=%d bssid=" MACSTR ")",
alg, keylen, MAC2STR(sm->bssid));
return -1;
}{...}
sm->ptk.installed = 1;
if (sm->wpa_ptk_rekey) {
eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
sm, NULL);
}{...}
return 0;
}{ ... }
static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
int group_cipher,
int keylen, int maxkeylen,
int *key_rsc_len,
enum wpa_alg *alg)
{
int klen;
*alg = wpa_cipher_to_alg(group_cipher);
if (*alg == WIFI_WPA_ALG_NONE) {
wpa_printf(MSG_WARNING,
"WPA: Unsupported Group Cipher %d",
group_cipher);
return -1;
}{...}
*key_rsc_len = 6;
klen = wpa_cipher_key_len(group_cipher);
if (keylen != klen || maxkeylen < klen) {
wpa_printf(MSG_WARNING,
"WPA: Unsupported %s Group Cipher key length %d (%d)",
wpa_cipher_txt(group_cipher), keylen, maxkeylen);
return -1;
}{...}
return 0;
}{ ... }
void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
const u8 *addr, int secure)
{
wpa_printf(MSG_DEBUG, "WPA: Key negotiation completed with "
MACSTR " [PTK=%s GTK=%s]\n", MAC2STR(addr),
wpa_cipher_txt(sm->pairwise_cipher),
wpa_cipher_txt(sm->group_cipher));
wpa_sm_cancel_auth_timeout(sm);
wpa_sm_set_state(WPA_COMPLETED);
wpa_neg_complete();
sm->eapol1_count = 0;
sm->use_ext_key_id = 0;
if (secure) {
wpa_sm_mlme_setprotection(
sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) || sm->key_mgmt == WPA_KEY_MGMT_OWE || sm->key_mgmt == WPA_KEY_MGMT_DPP)
eapol_sm_notify_eap_success(TRUE);
/* ... */
}{...}
#ifdef CONFIG_IEEE80211R
if (wpa_key_mgmt_ft(sm->key_mgmt)) {
wpa_ft_prepare_auth_request(sm, NULL);
sm->ft_protocol = 1;
}{...}
#endif/* ... */
}{ ... }
static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
struct wpa_gtk_data *gd)
{
u8 *_gtk = gd->gtk;
u8 gtk_buf[32];
u8 *key_rsc=(sm->install_gtk).seq;
wpa_hexdump(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
wpa_supplicant_gtk_in_use(sm, &(sm->gd))) {
wpa_printf(MSG_DEBUG,
"WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
gd->keyidx, gd->tx, gd->gtk_len);
return 0;
}{...}
wpa_printf(MSG_DEBUG, "WPA: Installing GTK to the driver "
"(keyidx=%d tx=%d len=%d).", gd->keyidx, gd->tx,
gd->gtk_len);
wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
if (sm->group_cipher == WPA_CIPHER_TKIP) {
memcpy(gtk_buf, gd->gtk, 16);
memcpy(gtk_buf + 16, gd->gtk + 16, 8);
memcpy(gtk_buf + 24, gd->gtk + 24, 8);
_gtk = gtk_buf;
}{...}
if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
if (wpa_sm_set_key(&(sm->install_gtk), gd->alg,
sm->bssid,
gd->keyidx, 1, key_rsc, gd->key_rsc_len,
_gtk, gd->gtk_len, (KEY_FLAG_GROUP | KEY_FLAG_RX | KEY_FLAG_TX)) < 0) {
wpa_printf(MSG_DEBUG, "WPA: Failed to set "
"GTK to the driver (Group only).");
return -1;
}{...}
}{...} else if (wpa_sm_set_key(&(sm->install_gtk), gd->alg,
sm->bssid,
gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
_gtk, gd->gtk_len, KEY_FLAG_GROUP | KEY_FLAG_RX) < 0) {
wpa_printf(MSG_DEBUG, "WPA: Failed to set GTK to "
"the driver (alg=%d keylen=%d keyidx=%d)",
gd->alg, gd->gtk_len, gd->keyidx);
return -1;
}{...}
sm->gtk.gtk_len = gd->gtk_len;
os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
return 0;
}{ ... }
static bool wpa_supplicant_gtk_in_use(struct wpa_sm *sm, struct wpa_gtk_data *gd)
{
u8 *_gtk = gd->gtk;
u8 gtk_buf[32];
u8 gtk_get[32] = {0};
u8 ifx;
int alg;
u8 bssid[6];
int keyidx = gd->keyidx;
wpa_printf(MSG_DEBUG, "WPA: Judge GTK: (keyidx=%d len=%d).", gd->keyidx, gd->gtk_len);
if (sm->group_cipher == WPA_CIPHER_TKIP) {
memcpy(gtk_buf, gd->gtk, 16);
memcpy(gtk_buf + 16, gd->gtk + 16, 8);
memcpy(gtk_buf + 24, gd->gtk + 24, 8);
_gtk = gtk_buf;
}{...}
if (wpa_sm_get_key(&ifx, &alg, bssid, &keyidx, gtk_get, gd->gtk_len, KEY_FLAG_GROUP) == 0) {
if (ifx == 0 && alg == gd->alg && memcmp(bssid, sm->bssid, ETH_ALEN) == 0 &&
memcmp(_gtk, gtk_get, gd->gtk_len) == 0) {
wpa_printf(MSG_DEBUG, "GTK %d is already in use, it may be an attack, ignore it.", gd->keyidx);
return true;
}{...}
}{...}
return false;
}{ ... }
int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
int tx)
{
if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
/* ... */
wpa_printf(MSG_DEBUG, "WPA: Tx bit set for GTK, but pairwise "
"keys are used - ignore Tx bit");
return 0;
}{...}
return tx;
}{ ... }
int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
const u8 *gtk, size_t gtk_len,
int key_info)
{
struct wpa_gtk_data *gd=&(sm->gd);
/* ... */
memset(gd, 0, sizeof(struct wpa_gtk_data));
wpa_hexdump(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
gtk, gtk_len);
if (gtk_len < 2 || gtk_len - 2 > sizeof(gd->gtk))
return -1;
gd->keyidx = gtk[0] & 0x3;
gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
!!(gtk[0] & BIT(2)));
gtk += 2;
gtk_len -= 2;
memcpy(gd->gtk, gtk, gtk_len);
gd->gtk_len = gtk_len;
if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
gtk_len, gtk_len,
&(gd->key_rsc_len), &(gd->alg))) {
wpa_printf(MSG_DEBUG, "RSN: Failed to install GTK");
return -1;
}{...}
return 0;
}{ ... }
#ifdef CONFIG_IEEE80211W
static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
const wifi_wpa_igtk_t *igtk)
{
size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
u16 keyidx = WPA_GET_LE16(igtk->keyid);
if (sm->igtk.igtk_len == len &&
os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) {
wpa_printf(MSG_DEBUG,
"WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
keyidx);
return 0;
}{...}
wpa_printf(MSG_DEBUG,
"WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
keyidx, MAC2STR(igtk->pn));
wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
if (esp_wifi_set_igtk_internal(WIFI_IF_STA, igtk) < 0) {
if (keyidx > 4095) {
wpa_printf(MSG_WARNING,
"WPA: Invalid IGTK KeyID %d", keyidx);
}{...}
wpa_printf(MSG_WARNING,
"WPA: Failed to configure IGTK to the driver");
return -1;
}{...}
sm->igtk.igtk_len = len;
os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
return 0;
}{ ... }
/* ... */#endif
#ifdef DEBUG_PRINT
void wpa_report_ie_mismatch(struct wpa_sm *sm,
const char *reason, const u8 *src_addr,
const u8 *wpa_ie, size_t wpa_ie_len,
const u8 *rsn_ie, size_t rsn_ie_len)/* ... */
#else
void wpa_report_ie_mismatch(struct wpa_sm *sm, const u8 *src_addr,
const u8 *wpa_ie, size_t wpa_ie_len,
const u8 *rsn_ie, size_t rsn_ie_len)/* ... */
#endif
{
wpa_printf(MSG_DEBUG, "WPA: %s (src=" MACSTR ")",
reason, MAC2STR(src_addr));
if (sm->ap_wpa_ie) {
wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
sm->ap_wpa_ie, sm->ap_wpa_ie_len);
}{...}
if (wpa_ie) {
if (!sm->ap_wpa_ie) {
wpa_printf(MSG_DEBUG, "WPA: No WPA IE in "
"Beacon/ProbeResp");
}{...}
wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
wpa_ie, wpa_ie_len);
}{...}
if (sm->ap_rsn_ie) {
wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
sm->ap_rsn_ie, sm->ap_rsn_ie_len);
}{...}
if (rsn_ie) {
if (!sm->ap_rsn_ie) {
wpa_printf(MSG_DEBUG, "WPA: No RSN IE in "
"Beacon/ProbeResp");
}{...}
wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
rsn_ie, rsn_ie_len);
}{...}
wpa_sm_disassociate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
}{...}
static int ieee80211w_set_keys(struct wpa_sm *sm,
struct wpa_eapol_ie_parse *ie)
{
#ifdef CONFIG_IEEE80211W
size_t len;
if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
return 0;
if (ie->igtk) {
const wifi_wpa_igtk_t *igtk;
#define WPA_IGTK_KDE_PREFIX_LEN (2 + 6)
len = wpa_cipher_key_len(sm->mgmt_group_cipher);
if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len) {
return -1;
}{...}
igtk = (const wifi_wpa_igtk_t*)ie->igtk;
if (wpa_supplicant_install_igtk(sm, igtk) < 0) {
return -1;
}{...}
}{...}
#endif/* ... */
return 0;
}{ ... }
static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
const unsigned char *src_addr,
struct wpa_eapol_ie_parse *ie)
{
if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
(sm->ap_wpa_ie || sm->ap_rsn_ie)) {
#ifdef DEBUG_PRINT
wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
"with IE in Beacon/ProbeResp (no IE?)",
src_addr, ie->wpa_ie, ie->wpa_ie_len,
ie->rsn_ie, ie->rsn_ie_len);/* ... */
#else
wpa_report_ie_mismatch(sm,
src_addr, ie->wpa_ie, ie->wpa_ie_len,
ie->rsn_ie, ie->rsn_ie_len);/* ... */
#endif
return -1;
}{...}
if ((ie->wpa_ie && sm->ap_wpa_ie &&
(ie->wpa_ie_len != sm->ap_wpa_ie_len ||
memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
(ie->rsn_ie && sm->ap_rsn_ie &&
wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
sm->ap_rsn_ie, sm->ap_rsn_ie_len,
ie->rsn_ie, ie->rsn_ie_len))) {
#ifdef DEBUG_PRINT
wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
"with IE in Beacon/ProbeResp",
src_addr, ie->wpa_ie, ie->wpa_ie_len,
ie->rsn_ie, ie->rsn_ie_len);/* ... */
#else
wpa_report_ie_mismatch(sm,
src_addr, ie->wpa_ie, ie->wpa_ie_len,
ie->rsn_ie, ie->rsn_ie_len);/* ... */
#endif
return -1;
}{...}
if (sm->proto == WPA_PROTO_WPA &&
ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
#ifdef DEBUG_PRINT
wpa_report_ie_mismatch(sm, "Possible downgrade attack "
"detected - RSN was enabled and RSN IE "
"was in msg 3/4, but not in "
"Beacon/ProbeResp",
src_addr, ie->wpa_ie, ie->wpa_ie_len,
ie->rsn_ie, ie->rsn_ie_len);/* ... */
#else
wpa_report_ie_mismatch(sm,
src_addr, ie->wpa_ie, ie->wpa_ie_len,
ie->rsn_ie, ie->rsn_ie_len);/* ... */
#endif
return -1;
}{...}
if (sm->proto == WPA_PROTO_RSN &&
((sm->ap_rsnxe && !ie->rsnxe) ||
(!sm->ap_rsnxe && ie->rsnxe) ||
(sm->ap_rsnxe && ie->rsnxe &&
(sm->ap_rsnxe_len != ie->rsnxe_len ||
os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0)))) {
wpa_printf(MSG_INFO,
"WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
sm->ap_rsnxe, sm->ap_rsnxe_len);
wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
ie->rsnxe, ie->rsnxe_len);
wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
return -1;
}{...}
return 0;
}{ ... }
/* ... */
static int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
const struct wpa_eapol_key *key,
u16 ver, u16 key_info,
struct wpa_ptk *ptk)
{
size_t mic_len, hdrlen, rlen;
struct wpa_eapol_key *reply;
struct wpa_eapol_key_192 *reply192;
u8 *rbuf, *key_mic;
mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
hdrlen,
&rlen, (void *) &reply);
if (rbuf == NULL)
return -1;
sm->txcb_flags |= WPA_4_4_HANDSHAKE_BIT;
wpa_printf(MSG_DEBUG, "tx 4/4 txcb_flags=%d", sm->txcb_flags);
reply192 = (struct wpa_eapol_key_192 *) reply;
reply->type = sm->proto == WPA_PROTO_RSN ?
EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
key_info &= WPA_KEY_INFO_SECURE;
key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
WPA_PUT_BE16(reply->key_info, key_info);
if (sm->proto == WPA_PROTO_RSN)
WPA_PUT_BE16(reply->key_length, 0);
else
memcpy(reply->key_length, key->key_length, 2);
memcpy(reply->replay_counter, key->replay_counter,
WPA_REPLAY_COUNTER_LEN);
key_mic = reply192->key_mic;
if (mic_len == 24)
WPA_PUT_BE16(reply192->key_data_length, 0);
else
WPA_PUT_BE16(reply->key_data_length, 0);
wpa_printf(MSG_DEBUG, "WPA Send EAPOL-Key 4/4");
wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst, ETH_P_EAPOL,
rbuf, rlen, key_mic);
wpa_sm_free_eapol(rbuf);
return 0;
}{ ... }
static void wpa_sm_set_seq(struct wpa_sm *sm, struct wpa_eapol_key *key, u8 isptk)
{
u8 *key_rsc, *seq;
u8 null_rsc[WPA_KEY_RSC_LEN];
os_bzero(null_rsc, WPA_KEY_RSC_LEN);
if (sm->proto == WPA_PROTO_RSN && isptk) {
key_rsc = null_rsc;
}{...} else {
key_rsc = key->key_rsc;
wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, WPA_KEY_RSC_LEN);
}{...}
seq=(isptk) ? (sm->install_ptk).seq : (sm->install_gtk).seq;
memcpy(seq, key_rsc, WPA_KEY_RSC_LEN);
}{ ... }
static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
struct wpa_eapol_key *key,
u16 ver, const u8 *key_data,
size_t key_data_len)
{
u16 key_info, keylen;
struct wpa_eapol_ie_parse ie;
wpa_sm_set_state(WPA_LAST_HALF_4WAY_HANDSHAKE);
wpa_printf(MSG_DEBUG, "WPA 3/4-Way Handshake");
key_info = WPA_GET_BE16(key->key_info);
if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
goto failed;
if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
goto failed;
if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
wpa_printf(MSG_DEBUG, "WPA: GTK IE in unencrypted key data");
goto failed;
}{...}
wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
if (memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
wpa_printf(MSG_DEBUG, "WPA: ANonce from message 1 of 4-Way "
"Handshake differs from 3 of 4-Way Handshake - drop"
" packet (src=" MACSTR ")", MAC2STR(sm->bssid));
goto failed;
}{...}
keylen = WPA_GET_BE16(key->key_length);
switch (sm->pairwise_cipher) {
case WPA_CIPHER_CCMP:
if (keylen != 16) {
wpa_printf(MSG_DEBUG, "WPA: Invalid CCMP key length "
"%d (src=" MACSTR ")",
keylen, MAC2STR(sm->bssid));
goto failed;
}{...}
break;...
case WPA_CIPHER_TKIP:
if (keylen != 32) {
wpa_printf(MSG_DEBUG, "WPA: Invalid TKIP key length "
"%d (src=" MACSTR ")",
keylen, MAC2STR(sm->bssid));
goto failed;
}{...}
break;...
}{...}
/* ... */
sm->renew_snonce = 1;
wpa_sm_set_seq(sm, key, 1);
sm->key_info=key_info;
(sm->gd).gtk_len=0;
if (ie.gtk) {
wpa_sm_set_seq(sm, key, 0);
if (wpa_supplicant_pairwise_gtk(sm,
ie.gtk, ie.gtk_len, key_info) < 0) {
wpa_printf(MSG_DEBUG, "RSN: Failed to configure GTK");
goto failed;
}{...}
}{...}
if (sm->pmf_cfg.capable && ieee80211w_set_keys(sm, &ie) < 0) {
wpa_printf(MSG_DEBUG, "RSN: Failed to configure IGTK");
goto failed;
}{...}
if (ie.transition_disable) {
wpa_supplicant_transition_disable(sm, ie.transition_disable[0]);
}{...}
if (sm->key_install && sm->key_info & WPA_KEY_INFO_INSTALL && sm->use_ext_key_id) {
wpa_supplicant_install_ptk(sm, KEY_FLAG_RX);
}{...}
if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
&sm->ptk)) {
goto failed;
}{...}
return;
failed:
wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
}{ ... }
static int wpa_supplicant_activate_ptk(struct wpa_sm *sm)
{
int keylen;
enum wpa_alg alg;
alg = wpa_cipher_to_alg(sm->pairwise_cipher);
keylen = wpa_cipher_key_len(sm->pairwise_cipher);
if (alg == WIFI_WPA_ALG_NONE) {
wpa_printf(MSG_DEBUG, "WPA: Pairwise Cipher Suite: "
"NONE - do not use pairwise keys");
return 0;
}{...}
wpa_printf(MSG_DEBUG,
"WPA: Activate PTK bssid=" MACSTR ")",
MAC2STR(sm->bssid));
if (wpa_sm_set_key(&(sm->install_ptk), alg, sm->bssid, 0, 1, (sm->install_ptk).seq,
WPA_KEY_RSC_LEN, sm->ptk.tk, keylen,
(KEY_FLAG_MODIFY | KEY_FLAG_PAIRWISE | KEY_FLAG_RX | KEY_FLAG_TX)) < 0) {
wpa_printf(MSG_WARNING,
"WPA: Failed to activate PTK for TX (idx=%d bssid="
MACSTR ")", 0, MAC2STR(sm->bssid));
return -1;
}{...}
return 0;
}{ ... }
static int wpa_supplicant_send_4_of_4_txcallback(struct wpa_sm *sm)
{
u16 key_info=sm->key_info;
if (sm->key_install && key_info & WPA_KEY_INFO_INSTALL) {
if (sm->use_ext_key_id) {
if (wpa_supplicant_activate_ptk(sm))
goto failed;
}{...} else {
if (wpa_supplicant_install_ptk(sm, KEY_FLAG_TX | KEY_FLAG_RX))
goto failed;
}{...}
}{...}
else if (sm->key_install == false) {
wpa_printf(MSG_DEBUG, "PTK has been installed, it may be an attack, ignor it.");
}{...}
wpa_sm_set_state(WPA_GROUP_HANDSHAKE);
if ((sm->gd).gtk_len) {
if (sm->key_install) {
if (wpa_supplicant_install_gtk(sm, &(sm->gd)))
goto failed;
}{...}
else {
wpa_printf(MSG_DEBUG, "GTK has been installed, it may be an attack, ignor it.");
}{...}
wpa_supplicant_key_neg_complete(sm, sm->bssid,
key_info & WPA_KEY_INFO_SECURE);
}{...}
if (key_info & WPA_KEY_INFO_SECURE) {
wpa_sm_mlme_setprotection(
sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
}{...}
sm->key_install = false;
#if defined(CONFIG_SUITEB192) || defined (CONFIG_SUITEB)
/* ... */
if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
!sm->cur_pmksa) {
struct rsn_pmksa_cache_entry *sa;
sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
sm->ptk.kck, sm->ptk.kck_len,
sm->bssid, sm->own_addr,
sm->network_ctx, sm->key_mgmt);
if (!sm->cur_pmksa)
sm->cur_pmksa = sa;
}{...}
#endif/* ... */
return 0;
failed:
return WLAN_REASON_UNSPECIFIED;
}{ ... }
static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
const u8 *keydata,
size_t keydatalen,
u16 key_info,
struct wpa_gtk_data *gd)
{
int maxkeylen;
struct wpa_eapol_ie_parse ie;
wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
wpa_supplicant_parse_ies(keydata, keydatalen, &ie);
if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
wpa_printf(MSG_DEBUG, "WPA: GTK IE in unencrypted key data");
return -1;
}{...}
if (ie.gtk == NULL) {
wpa_printf(MSG_DEBUG, "WPA: No GTK IE in Group Key msg 1/2");
return -1;
}{...}
maxkeylen = gd->gtk_len = ie.gtk_len - 2;
if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
gd->gtk_len, maxkeylen,
&gd->key_rsc_len, &gd->alg))
return -1;
wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
ie.gtk, ie.gtk_len);
gd->keyidx = ie.gtk[0] & 0x3;
gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
!!(ie.gtk[0] & BIT(2)));
if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
wpa_printf(MSG_DEBUG, "RSN: Too long GTK in GTK IE "
"(len=%lu)", (unsigned long) ie.gtk_len - 2);
return -1;
}{...}
memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
if (ieee80211w_set_keys(sm, &ie) < 0)
{
wpa_printf(MSG_DEBUG, "RSN: Failed to configure IGTK");
}{...}
return 0;
}{ ... }
static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
const struct wpa_eapol_key *key,
const u8 *key_data,
size_t key_data_len, u16 key_info,
u16 ver, struct wpa_gtk_data *gd)
{
size_t maxkeylen;
u8 ek[32];
gd->gtk_len = WPA_GET_BE16(key->key_length);
maxkeylen = key_data_len;
if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
if (maxkeylen < 8) {
wpa_printf(MSG_DEBUG, "WPA: Too short maxkeylen (%lu)",
(unsigned long) maxkeylen);
return -1;
}{...}
maxkeylen -= 8;
}{...}
if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
gd->gtk_len, maxkeylen,
&gd->key_rsc_len, &gd->alg))
return -1;
gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
WPA_KEY_INFO_KEY_INDEX_SHIFT;
if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
os_memcpy(ek, key->key_iv, 16);
os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
if (key_data_len > sizeof(gd->gtk)) {
wpa_printf(MSG_DEBUG, "WPA: RC4 key data "
"too long (%lu)",
(unsigned long) key_data_len);
return -1;
}{...}
os_memcpy(gd->gtk, key_data, key_data_len);
if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
wpa_printf(MSG_DEBUG, "WPA: RC4 failed");
return -1;
}{...}
}{...} else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
if (maxkeylen % 8) {
wpa_printf(MSG_DEBUG,
"WPA: Unsupported AES-WRAP len %lu",
(unsigned long) maxkeylen);
return -1;
}{...}
if (maxkeylen > sizeof(gd->gtk)) {
wpa_printf(MSG_DEBUG, "WPA: AES-WRAP key data "
"too long (keydatalen=%lu maxkeylen=%lu)",
(unsigned long) key_data_len,
(unsigned long) maxkeylen);
return -1;
}{...}
if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8, key_data, gd->gtk)) {
wpa_printf(MSG_DEBUG, "WPA: AES unwrap "
"failed - could not decrypt GTK");
return -1;
}{...}
}{...} else {
wpa_printf(MSG_DEBUG, "WPA: Unsupported key_info type %d",
ver);
return -1;
}{...}
gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
sm, !!(key_info & WPA_KEY_INFO_TXRX));
return 0;
}{ ... }
static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
const struct wpa_eapol_key *key,
int ver, u16 key_info)
{
size_t mic_len, hdrlen, rlen;
struct wpa_eapol_key *reply;
struct wpa_eapol_key_192 *reply192;
u8 *rbuf, *key_mic;
mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
hdrlen, &rlen, (void *) &reply);
if (rbuf == NULL)
return -1;
sm->txcb_flags |= WPA_GROUP_HANDSHAKE_BIT;
wpa_printf(MSG_DEBUG, "2/2 txcb_flags=%d", sm->txcb_flags);
reply192 = (struct wpa_eapol_key_192 *) reply;
reply->type = sm->proto == WPA_PROTO_RSN ?
EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
WPA_PUT_BE16(reply->key_info, key_info);
if (sm->proto == WPA_PROTO_RSN)
WPA_PUT_BE16(reply->key_length, 0);
else
memcpy(reply->key_length, key->key_length, 2);
memcpy(reply->replay_counter, key->replay_counter,
WPA_REPLAY_COUNTER_LEN);
key_mic = reply192->key_mic;
if (mic_len == 24)
WPA_PUT_BE16(reply192->key_data_length, 0);
else
WPA_PUT_BE16(reply->key_data_length, 0);
wpa_printf(MSG_DEBUG, "WPA Send 2/2 Group key");
wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver, sm->bssid, ETH_P_EAPOL,
rbuf, rlen, key_mic);
wpa_sm_free_eapol(rbuf);
return 0;
}{ ... }
static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
const unsigned char *src_addr,
struct wpa_eapol_key *key,
const u8 *key_data,
size_t key_data_len, u16 ver)
{
u16 key_info;
int ret;
struct wpa_gtk_data *gd=&(sm->gd);
memset(gd, 0, sizeof(struct wpa_gtk_data));
wpa_printf(MSG_DEBUG, "WPA 1/2 Group Key Handshake");
key_info = WPA_GET_BE16(key->key_info);
if (sm->proto == WPA_PROTO_RSN) {
ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
key_data_len, key_info, gd);
}{...} else {
ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
key_data_len, key_info, ver, gd);
}{...}
wpa_sm_set_state(WPA_GROUP_HANDSHAKE);
if (ret)
goto failed;
wpa_sm_set_seq(sm, key, 0);
sm->key_info=key_info;
if((sm->gd).gtk_len) {
if (wpa_supplicant_install_gtk(sm, &(sm->gd)))
goto failed;
}{...} else {
goto failed;
}{...}
if (wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
goto failed;
if (WPA_SM_STATE(sm) == WPA_COMPLETED) {
#ifdef MSG_PRINT
wpa_printf(MSG_DEBUG, "WPA: Group rekeying "
"completed with " MACSTR " [GTK=%s]",
MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));/* ... */
#endif
wpa_sm_cancel_auth_timeout(sm);
wpa_sm_set_state(WPA_COMPLETED);
}{...} else {
wpa_supplicant_key_neg_complete(sm, sm->bssid,
sm->key_info &WPA_KEY_INFO_SECURE);
}{...}
return;
failed:
wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
}{ ... }
static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
struct wpa_eapol_key_192 *key,
u16 ver,
const u8 *buf, size_t len)
{
u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
int ok = 0;
size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
os_memcpy(mic, key->key_mic, mic_len);
if (sm->tptk_set) {
os_memset(key->key_mic, 0, mic_len);
wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
ver, buf, len, key->key_mic);
if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
wpa_printf(MSG_DEBUG, "WPA: Invalid EAPOL-Key MIC "
"when using TPTK - ignoring TPTK");
}{...} else {
ok = 1;
sm->tptk_set = 0;
sm->ptk_set = 1;
memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
}{...}
}{...}
if (!ok && sm->ptk_set) {
os_memset(key->key_mic, 0, mic_len);
wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
ver, buf, len, key->key_mic);
if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
wpa_printf(MSG_DEBUG, "WPA: Invalid EAPOL-Key MIC "
"- dropping packet");
return -1;
}{...}
ok = 1;
}{...}
if (!ok) {
wpa_printf(MSG_DEBUG, "WPA: Could not verify EAPOL-Key MIC "
"- dropping packet");
return -1;
}{...}
memcpy(sm->rx_replay_counter, key->replay_counter,
WPA_REPLAY_COUNTER_LEN);
sm->rx_replay_counter_set = 1;
memcpy(sm->request_counter, key->replay_counter,
WPA_REPLAY_COUNTER_LEN);
return 0;
}{ ... }
static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
struct wpa_eapol_key *key, u16 ver,
u8 *key_data, size_t *key_data_len)
{
wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
key_data, *key_data_len);
if (!sm->ptk_set) {
wpa_printf(MSG_DEBUG, "WPA: PTK not available, "
"cannot decrypt EAPOL-Key key data.");
return -1;
}{...}
/* ... */
if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
u8 ek[32];
os_memcpy(ek, key->key_iv, 16);
os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
wpa_printf(MSG_DEBUG, "WPA: RC4 failed");
return -1;
}{...}
}{...} else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
wpa_use_aes_key_wrap(sm->key_mgmt)) {
u8 *buf;
if (*key_data_len < 8 || *key_data_len % 8) {
wpa_printf(MSG_DEBUG, "WPA: Unsupported "
"AES-WRAP len %u", (unsigned int) *key_data_len);
return -1;
}{...}
*key_data_len -= 8;
buf = ((u8 *) (key+1))+ 8;
/* ... */
if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
key_data, buf)) {
wpa_printf(MSG_DEBUG, "WPA: AES unwrap failed - "
"could not decrypt EAPOL-Key key data");
return -1;
}{...}
os_memcpy(key_data, buf, *key_data_len);
WPA_PUT_BE16(key->key_data_length, *key_data_len);
}{...} else {
wpa_printf(MSG_DEBUG, "WPA: Unsupported key_info type %d",
ver);
return -1;
}{...}
wpa_hexdump(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
key_data, *key_data_len);
return 0;
}{ ... }
static void wpa_eapol_key_dump(struct wpa_sm *sm,
const struct wpa_eapol_key *key,
unsigned int key_data_len,
const u8 *mic, unsigned int mic_len)
{
#ifdef DEBUG_PRINT
u16 key_info = WPA_GET_BE16(key->key_info);
wpa_printf(MSG_DEBUG, " EAPOL-Key type=%d", key->type);
wpa_printf(MSG_DEBUG, " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s"
"%s%s%s%s%s%s%s)",
key_info, (u32)(key_info & WPA_KEY_INFO_TYPE_MASK),
(u32)((key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
WPA_KEY_INFO_KEY_INDEX_SHIFT),
(u32)((key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13),
key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
wpa_printf(MSG_DEBUG, " key_length=%u key_data_length=%u",
WPA_GET_BE16(key->key_length), key_data_len);
wpa_hexdump(MSG_DEBUG, " replay_counter",
key->replay_counter, WPA_REPLAY_COUNTER_LEN);
wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);/* ... */
#endif
}{ ... }
/* ... */
int wpa_sm_rx_eapol(u8 *src_addr, u8 *buf, u32 len)
{
struct wpa_sm *sm = &gWpaSm;
size_t plen, data_len, key_data_len;
struct ieee802_1x_hdr *hdr;
struct wpa_eapol_key *key;
struct wpa_eapol_key_192 *key192;
u16 key_info, ver;
u8 *tmp;
int ret = -1;
size_t mic_len, keyhdrlen;
u8 *key_data;
mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
if (len < sizeof(*hdr) + keyhdrlen) {
wpa_printf(MSG_DEBUG, "WPA: EAPOL frame too short to be a WPA "
"EAPOL-Key (len %lu, expecting at least %lu)",
(unsigned long) len,
(unsigned long) sizeof(*hdr) + sizeof(*key));
return 0;
}{...}
tmp = buf;
hdr = (struct ieee802_1x_hdr *) tmp;
key = (struct wpa_eapol_key *) (hdr + 1);
key192 = (struct wpa_eapol_key_192 *)
(tmp + sizeof(struct ieee802_1x_hdr));
if (mic_len == 24)
key_data = (u8 *) (key192 + 1);
else
key_data = (u8 *) (key + 1);
plen = be_to_host16(hdr->length);
data_len = plen + sizeof(*hdr);
wpa_printf(MSG_DEBUG, "IEEE 802.1X RX: version=%d type=%d length=%d",
hdr->version, hdr->type, plen);
if (hdr->version < EAPOL_VERSION) {
}{...}
if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
wpa_printf(MSG_DEBUG, "WPA: EAPOL frame (type %u) discarded, "
"not a Key frame", hdr->type);
ret = 0;
goto out;
}{...}
if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
wpa_printf(MSG_DEBUG, "WPA: EAPOL frame payload size %lu "
"invalid (frame size %lu)",
(unsigned long) plen, (unsigned long) len);
ret = 0;
goto out;
}{...}
if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN) {
wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key type (%d) unknown, "
"discarded", key->type);
ret = 0;
goto out;
}{...}
wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
if (data_len < len) {
wpa_printf(MSG_DEBUG, "WPA: ignoring %lu bytes after the IEEE "
"802.1X data", (unsigned long) len - data_len);
}{...}
key_info = WPA_GET_BE16(key->key_info);
ver = key_info & WPA_KEY_INFO_TYPE_MASK;
if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
#ifdef CONFIG_IEEE80211W
ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
#endif
ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
!wpa_use_akm_defined(sm->key_mgmt)) {
wpa_printf(MSG_DEBUG, "WPA: Unsupported EAPOL-Key descriptor "
"version %d.", ver);
goto out;
}{...}
if (wpa_use_akm_defined(sm->key_mgmt) &&
ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
wpa_msg(NULL, MSG_INFO,
"RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
ver);
goto out;
}{...}
#ifdef CONFIG_IEEE80211W
if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
!wpa_use_akm_defined(sm->key_mgmt)) {
goto out;
}{...}
}{...} else/* ... */
#endif
if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
!wpa_use_akm_defined(sm->key_mgmt) &&
ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ) {
wpa_printf(MSG_DEBUG, "WPA: CCMP is used, but EAPOL-Key "
"descriptor version (%d) is not 2.", ver);
if (sm->group_cipher != WPA_CIPHER_CCMP &&
!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
/* ... */
wpa_printf(MSG_DEBUG, "WPA: Backwards compatibility: "
"allow invalid version for non-CCMP group "
"keys");
}{...} else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
wpa_printf(MSG_DEBUG,
"WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
}{...} else
goto out;
}{...}
#ifdef CONFIG_GCMP
if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
!wpa_use_akm_defined(sm->key_mgmt) &&
ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
wpa_msg(NULL, MSG_INFO,
"WPA: GCMP is used, but EAPOL-Key "
"descriptor version (%d) is not 2", ver);
goto out;
}{...}
#endif/* ... */
if (sm->rx_replay_counter_set &&
os_memcmp(key->replay_counter, sm->rx_replay_counter,
WPA_REPLAY_COUNTER_LEN) <= 0) {
wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Replay Counter did not"
" increase - dropping packet");
goto out;
}{...}
if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))) {
wpa_printf(MSG_DEBUG, "WPA: No Ack bit in key_info");
goto out;
}{...}
if (key_info & WPA_KEY_INFO_REQUEST) {
wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key with Request bit - dropped");
goto out;
}{...}
if ((key_info & WPA_KEY_INFO_MIC) &&
wpa_supplicant_verify_eapol_key_mic(sm, key192, ver, tmp, data_len))
goto out;
if (mic_len == 24)
key_data_len = WPA_GET_BE16(key192->key_data_length);
else
key_data_len = WPA_GET_BE16(key->key_data_length);
wpa_eapol_key_dump(sm, key, key_data_len, key192->key_mic, mic_len);
if (key_data_len > plen - keyhdrlen) {
wpa_printf(MSG_DEBUG, "WPA: Invalid EAPOL-Key "
"frame - key_data overflow (%d > %u)",
(unsigned int) key_data_len,
(unsigned int) (plen - keyhdrlen));
goto out;
}{...}
if (sm->proto == WPA_PROTO_RSN &&
(key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
/* ... */
if (!(key_info & WPA_KEY_INFO_MIC)) {
wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
"WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
goto out;
}{...}
if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
&key_data_len))
goto out;
}{...}
if (key_info & WPA_KEY_INFO_KEY_TYPE) {
if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
wpa_printf(MSG_DEBUG, "WPA: Ignored EAPOL-Key "
"(Pairwise) with non-zero key index");
goto out;
}{...}
if (key_info & WPA_KEY_INFO_MIC) {
wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
key_data_len);
}{...} else {
sm->eapol1_count++;
if (sm->eapol1_count > MAX_EAPOL_RETRIES) {
wpa_printf(MSG_INFO, "EAPOL1 received for %d times, sending deauth", sm->eapol1_count);
esp_wifi_internal_issue_disconnect(WLAN_REASON_4WAY_HANDSHAKE_TIMEOUT);
goto out;
}{...}
wpa_supplicant_process_1_of_4(sm, src_addr, key,
ver, key_data,
key_data_len);
}{...}
}{...} else {
if (key_info & WPA_KEY_INFO_MIC) {
wpa_supplicant_process_1_of_2(sm, src_addr, key,
key_data, key_data_len,
ver);
}{...} else {
wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key (Group) "
"without Mic bit - dropped");
}{...}
}{...}
ret = 1;
out:
return ret;
}{ ... }
/* ... */
void wpa_sm_set_state(enum wpa_states state)
{
struct wpa_sm *sm = &gWpaSm;
if(WPA_MIC_FAILURE==WPA_SM_STATE(sm))
eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, NULL, NULL);
sm->wpa_state= state;
}{ ... }
/* ... */
void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
const u8 *pmkid, const u8 *bssid)
{
if (sm == NULL)
return;
sm->pmk_len = pmk_len;
os_memcpy(sm->pmk, pmk, pmk_len);
#ifdef CONFIG_IEEE80211R
sm->xxkey_len = pmk_len;
os_memcpy(sm->xxkey, pmk, pmk_len);/* ... */
#endif
if (bssid) {
pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
bssid, sm->own_addr,
sm->network_ctx, sm->key_mgmt);
}{...}
}{ ... }
/* ... */
void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
{
if (sm == NULL)
return;
if (sm->cur_pmksa) {
sm->pmk_len = sm->cur_pmksa->pmk_len;
os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
}{...} else {
sm->pmk_len = PMK_LEN_MAX;
os_memset(sm->pmk, 0, PMK_LEN_MAX);
}{...}
}{ ... }
bool wpa_sm_init(void)
{
struct wpa_sm *sm = &gWpaSm;
u16 spp_attrubute = 0;
os_memset(sm, 0, sizeof(struct wpa_sm));
sm->eapol_version = DEFAULT_EAPOL_VERSION;
spp_attrubute = esp_wifi_get_spp_attrubute_internal(WIFI_IF_STA);
sm->spp_sup.capable = ((spp_attrubute & WPA_CAPABILITY_SPP_CAPABLE) ? SPP_AMSDU_CAP_ENABLE : SPP_AMSDU_CAP_DISABLE);
sm->spp_sup.require = ((spp_attrubute & WPA_CAPABILITY_SPP_REQUIRED) ? SPP_AMSDU_REQ_ENABLE : SPP_AMSDU_REQ_DISABLE);
wpa_sm_set_state(WPA_INACTIVE);
sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
if (sm->pmksa == NULL) {
wpa_printf(MSG_ERROR,
"RSN: PMKSA cache initialization failed");
return false;
}{...}
return true;
}{ ... }
/* ... */
void wpa_sm_deinit(void)
{
struct wpa_sm *sm = &gWpaSm;
pmksa_cache_deinit(sm->pmksa);
os_free(sm->ap_rsnxe);
sm->ap_rsnxe = NULL;
os_free(sm->assoc_rsnxe);
wpa_sm_drop_sa(sm);
sm->assoc_rsnxe = NULL;
}{ ... }
#ifdef ESP_SUPPLICANT
/* ... */
void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
{
int clear_keys = 1;
if (sm == NULL)
return;
wpa_printf(MSG_DEBUG,
"WPA: Association event - clear replay counter");
os_memcpy(sm->bssid, bssid, ETH_ALEN);
os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
sm->rx_replay_counter_set = 0;
sm->renew_snonce = 1;
#ifdef CONFIG_IEEE80211R
if (wpa_ft_is_completed(sm)) {
/* ... */
wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
wpa_ft_prepare_auth_request(sm, NULL);
clear_keys = 0;
sm->ft_protocol = 1;
}{...} else {
sm->ft_protocol = 0;
}{...}
#endif/* ... */
if (clear_keys) {
/* ... */
wpa_printf(MSG_DEBUG, "WPA: Clear old PTK");
sm->ptk_set = 0;
os_memset(&sm->ptk, 0, sizeof(sm->ptk));
sm->tptk_set = 0;
os_memset(&sm->tptk, 0, sizeof(sm->tptk));
os_memset(&sm->gtk, 0, sizeof(sm->gtk));
os_memset(&sm->igtk, 0, sizeof(sm->igtk));
}{...}
}{ ... }
/* ... */
void wpa_sm_notify_disassoc(struct wpa_sm *sm)
{
eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
pmksa_cache_clear_current(sm);
#ifdef CONFIG_IEEE80211R
sm->ft_reassoc_completed = 0;
sm->ft_protocol = 0;/* ... */
#endif
wpa_sm_drop_sa(sm);
os_memset(sm->bssid, 0, ETH_ALEN);
}{ ... }
void wpa_set_profile(u32 wpa_proto, u8 auth_mode)
{
struct wpa_sm *sm = &gWpaSm;
sm->proto = wpa_proto;
if (auth_mode == WPA2_AUTH_ENT) {
sm->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
}{...} else if (auth_mode == WPA2_AUTH_ENT_SHA256) {
sm->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
}{...} else if (auth_mode == WPA2_AUTH_PSK_SHA256) {
sm->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
}{...} else if (auth_mode == WPA3_AUTH_PSK) {
sm->key_mgmt = WPA_KEY_MGMT_SAE;
}{...} else if (auth_mode == WAPI_AUTH_PSK) {
sm->key_mgmt = WPA_KEY_MGMT_WAPI_PSK;
}{...} else if (auth_mode == WPA2_AUTH_ENT_SHA384_SUITE_B) {
sm->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
}{...} else if (auth_mode == WPA2_AUTH_FT_PSK) {
sm->key_mgmt = WPA_KEY_MGMT_FT_PSK;
}{...} else if (auth_mode == WPA3_AUTH_OWE) {
sm->key_mgmt = WPA_KEY_MGMT_OWE;
}{...} else if (auth_mode == WPA3_AUTH_PSK_EXT_KEY) {
sm->key_mgmt = WPA_KEY_MGMT_SAE_EXT_KEY;
}{...} else if (auth_mode == WPA3_AUTH_DPP) {
sm->key_mgmt = WPA_KEY_MGMT_DPP;
}{...} else {
sm->key_mgmt = WPA_KEY_MGMT_PSK;
}{...}
}{ ... }
void wpa_set_pmk(uint8_t *pmk, size_t pmk_length, const u8 *pmkid, bool cache_pmksa)
{
struct wpa_sm *sm = &gWpaSm;
int pmk_len;
if (wpa_key_mgmt_sha384(sm->key_mgmt))
pmk_len = PMK_LEN_SUITE_B_192;
else if (wpa_key_mgmt_sae(sm->key_mgmt))
pmk_len = pmk_length;
else
pmk_len = PMK_LEN;
memcpy(sm->pmk, pmk, pmk_len);
sm->pmk_len = pmk_len;
if (cache_pmksa) {
pmksa_cache_add(sm->pmksa, pmk, PMK_LEN, pmkid, NULL, 0,
sm->bssid, sm->own_addr,
sm->network_ctx, sm->key_mgmt);
}{...}
}{ ... }
int wpa_set_bss(char *macddr, char * bssid, u8 pairwise_cipher, u8 group_cipher, char *passphrase, u8 *ssid, size_t ssid_len)
{
int res = 0;
struct wpa_sm *sm = &gWpaSm;
bool use_pmk_cache = !esp_wifi_skip_supp_pmkcaching();
u8 assoc_rsnxe[20];
size_t assoc_rsnxe_len = sizeof(assoc_rsnxe);
/* ... */
if ((sm->key_mgmt == WPA_KEY_MGMT_SAE ||
sm->key_mgmt == WPA_KEY_MGMT_SAE_EXT_KEY) &&
(os_memcmp(sm->bssid, bssid, ETH_ALEN) == 0) &&
(os_memcmp(sm->ssid, ssid, ssid_len) != 0)) {
use_pmk_cache = false;
}{...}
sm->pairwise_cipher = BIT(pairwise_cipher);
sm->group_cipher = BIT(group_cipher);
sm->rx_replay_counter_set = 0;
memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
sm->wpa_ptk_rekey = 0;
sm->renew_snonce = 1;
memcpy(sm->own_addr, macddr, ETH_ALEN);
memcpy(sm->bssid, bssid, ETH_ALEN);
sm->ap_notify_completed_rsne = esp_wifi_sta_is_ap_notify_completed_rsne_internal();
sm->use_ext_key_id = (sm->proto == WPA_PROTO_WPA);
pmksa_cache_clear_current(sm);
sm->sae_pwe = esp_wifi_get_config_sae_pwe_h2e_internal(WIFI_IF_STA);
struct rsn_pmksa_cache_entry *pmksa = NULL;
if (use_pmk_cache) {
pmksa = pmksa_cache_get(sm->pmksa, (const u8 *)bssid, NULL, NULL);
if (pmksa && (pmksa->akmp != sm->key_mgmt)) {
use_pmk_cache = false;
}{...}
}{...}
if (wpa_key_mgmt_supports_caching(sm->key_mgmt) && use_pmk_cache) {
pmksa_cache_set_current(sm, NULL, (const u8*) bssid, 0, 0);
wpa_sm_set_pmk_from_pmksa(sm);
}{...} else {
if (pmksa) {
pmksa_cache_flush(sm->pmksa, NULL, pmksa->pmk, pmksa->pmk_len);
}{...}
}{...}
sm->eapol1_count = 0;
#ifdef CONFIG_IEEE80211W
if (esp_wifi_sta_pmf_enabled()) {
wifi_config_t wifi_cfg;
wifi_cipher_type_t mgmt_cipher = esp_wifi_sta_get_mgmt_group_cipher();
esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg);
sm->pmf_cfg = wifi_cfg.sta.pmf_cfg;
sm->mgmt_group_cipher = cipher_type_map_public_to_supp(mgmt_cipher);
if (sm->mgmt_group_cipher == WPA_CIPHER_NONE) {
wpa_printf(MSG_ERROR, "mgmt_cipher %d not supported", mgmt_cipher);
return -1;
}{...}
#ifdef CONFIG_SUITEB192
extern bool g_wpa_suiteb_certification;
if (g_wpa_suiteb_certification) {
if (sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_256) {
wpa_printf(MSG_ERROR, "suite-b 192bit certification, only GMAC256 is supported");
return -1;
}{...}
if (sm->group_cipher != WPA_CIPHER_GCMP_256) {
wpa_printf(MSG_ERROR, "suite-b 192bit certification, only group GCMP256 is supported for group data cipher.");
return -1;
}{...}
if (sm->pairwise_cipher != WPA_CIPHER_GCMP_256) {
wpa_printf(MSG_ERROR,"suite-b 192bit certification, only group GCMP256 is supported for pairwise cipher");
return -1;
}{...}
if (sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
wpa_printf(MSG_ERROR, "suite-b 192bit certification, 192bit akm supported");
return -1;
}{...}
}{...}
#endif/* ... */
}{...} else {
memset(&sm->pmf_cfg, 0, sizeof(sm->pmf_cfg));
sm->mgmt_group_cipher = WPA_CIPHER_NONE;
}{...}
/* ... */#endif
#ifdef CONFIG_IEEE80211R
if (sm->key_mgmt == WPA_KEY_MGMT_FT_PSK) {
const u8 *ie, *md = NULL;
struct wpa_bss *bss = wpa_bss_get_bssid(&g_wpa_supp, (uint8_t *)bssid);
if (!bss) {
return -1;
}{...}
ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
md = ie + 2;
if (os_memcmp(md, sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN) != 0) {
esp_wifi_unset_appie_internal(WIFI_APPIE_RAM_STA_AUTH);
esp_wifi_unset_appie_internal(WIFI_APPIE_ASSOC_REQ);
sm->ft_protocol = 0;
}{...}
wpa_sm_set_ft_params(sm, ie, ie ? 2 + ie[1] : 0);
}{...} else {
wpa_sm_set_ft_params(sm, NULL, 0);
esp_wifi_unset_appie_internal(WIFI_APPIE_RAM_STA_AUTH);
esp_wifi_unset_appie_internal(WIFI_APPIE_ASSOC_REQ);
}{...}
#endif/* ... */
set_assoc_ie(assoc_ie_buf);
res = wpa_gen_wpa_ie(sm, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
if (res < 0)
return -1;
sm->assoc_wpa_ie_len = res;
const u8 *rsnxe;
rsnxe = esp_wifi_sta_get_rsnxe((u8*)bssid);
wpa_sm_set_ap_rsnxe(rsnxe, rsnxe ? (rsnxe[1] + 2) : 0);
res = wpa_gen_rsnxe(sm, assoc_rsnxe, assoc_rsnxe_len);
if (res < 0)
return -1;
assoc_rsnxe_len = res;
res = wpa_sm_set_assoc_rsnxe(sm, assoc_rsnxe, assoc_rsnxe_len);
if (res < 0)
return -1;
esp_set_assoc_ie((uint8_t *)bssid, assoc_rsnxe, assoc_rsnxe_len, true);
os_memset(sm->ssid, 0, sizeof(sm->ssid));
os_memcpy(sm->ssid, ssid, ssid_len);
sm->ssid_len = ssid_len;
wpa_set_passphrase(passphrase, ssid, ssid_len);
#ifdef CONFIG_MBO
if (!mbo_bss_profile_match((u8 *)bssid))
return -1;/* ... */
#endif
#ifndef CONFIG_SAE_PK
esp_wifi_sta_disable_sae_pk_internal();
#endif
return 0;
}{ ... }
/* ... */
void
wpa_set_passphrase(char * passphrase, u8 *ssid, size_t ssid_len)
{
struct wifi_ssid *sta_ssid = esp_wifi_sta_get_prof_ssid_internal();
struct wpa_sm *sm = &gWpaSm;
if (passphrase == NULL) return;
/* ... */
if (sm->key_mgmt == WPA_KEY_MGMT_SAE ||
sm->key_mgmt == WPA_KEY_MGMT_OWE ||
sm->key_mgmt == WPA_KEY_MGMT_SAE_EXT_KEY ||
sm->key_mgmt == WPA_KEY_MGMT_DPP)
return;
if (esp_wifi_sta_get_reset_param_internal() != 0) {
if (strlen((char *)esp_wifi_sta_get_prof_password_internal()) == 64) {
if (hexstr2bin((char *)esp_wifi_sta_get_prof_password_internal(),
esp_wifi_sta_get_ap_info_prof_pmk_internal(), PMK_LEN) != 0)
return;
}{...} else {
pbkdf2_sha1((char *)esp_wifi_sta_get_prof_password_internal(), sta_ssid->ssid, (size_t)sta_ssid->len,
4096, esp_wifi_sta_get_ap_info_prof_pmk_internal(), PMK_LEN);
}{...}
esp_wifi_sta_update_ap_info_internal();
esp_wifi_sta_set_reset_param_internal(0);
}{...}
if (sm->key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
}{...} else {
memcpy(sm->pmk, esp_wifi_sta_get_ap_info_prof_pmk_internal(), PMK_LEN);
sm->pmk_len = PMK_LEN;
}{...}
#ifdef CONFIG_IEEE80211R
sm->xxkey_len = PMK_LEN;
os_memcpy(sm->xxkey, sm->pmk, PMK_LEN);/* ... */
#endif
}{ ... }
void
set_assoc_ie(u8 * assoc_buf)
{
struct wpa_sm *sm = &gWpaSm;
sm->assoc_wpa_ie = assoc_buf + 2;
if ( sm->proto == WPA_PROTO_WPA)
sm->assoc_wpa_ie_len = ASSOC_IE_LEN;
else
sm->assoc_wpa_ie_len = ASSOC_IE_LEN - 2;
wpa_config_assoc_ie(sm->proto, assoc_buf, sm->assoc_wpa_ie_len);
}{ ... }
int wpa_sm_set_key(struct install_key *key_sm, enum wpa_alg alg,
u8 *addr, int key_idx, int set_tx,
u8 *seq, size_t seq_len,
u8 *key, size_t key_len,
enum key_flag key_flag)
{
struct wpa_sm *sm = &gWpaSm;
if (alg == WIFI_WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
sm->mic_errors_seen = 0;
}{...}
key_sm->keys_cleared = 0;
key_sm->alg = alg;
memcpy(key_sm->addr, addr, ETH_ALEN);
key_sm->key_idx = key_idx;
key_sm->set_tx = set_tx;
memcpy(key_sm->key, key, key_len);
wpa_install_key(alg, addr, key_idx, set_tx, seq, seq_len, key, key_len, key_flag);
return 0;
}{ ... }
static int
wpa_sm_get_key(uint8_t *ifx, int *alg, u8 *addr, int *key_idx, u8 *key, size_t key_len, enum key_flag key_flag)
{
return wpa_get_key(ifx, alg, addr, key_idx, key, key_len, key_flag);
}{ ... }
void wpa_supplicant_clr_countermeasures(u16 *pisunicast)
{
struct wpa_sm *sm = &gWpaSm;
sm->mic_errors_seen = 0;
wpa_printf(MSG_DEBUG, "WPA: TKIP countermeasures clean");
}{ ... }
/* ... */
void wpa_supplicant_stop_countermeasures(void *data, void *user_ctx)
{
struct wpa_sm *sm = &gWpaSm;
if (sm->countermeasures) {
sm->countermeasures = 0;
wpa_supplicant_clr_countermeasures(NULL);
eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, NULL, NULL);
wpa_printf(MSG_DEBUG, "WPA: TKIP countermeasures stopped");
}{...}
wpa_sm_set_state(WPA_DISCONNECTED);
}{ ... }
int wpa_michael_mic_failure(u16 isunicast)
{
struct wpa_sm *sm = &gWpaSm;
wpa_printf(MSG_DEBUG, "TKIP MIC failure occur");
if (sm->mic_errors_seen) {
/* ... */
wpa_sm_set_state(WPA_TKIP_COUNTERMEASURES);
wpa_sm_key_request(sm, 1, isunicast);
sm->countermeasures = 1;
wpa_printf(MSG_DEBUG, "TKIP countermeasures started");
/* ... */
os_sleep(0, 10000);
eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, NULL, NULL);
eloop_register_timeout(60, 0, wpa_supplicant_stop_countermeasures, NULL, NULL);
/* ... */
}{...} else {
sm->mic_errors_seen++;
wpa_sm_set_state(WPA_MIC_FAILURE);
wpa_sm_key_request(sm, 1, isunicast);
eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, NULL, NULL);
eloop_register_timeout(60, 0, wpa_supplicant_stop_countermeasures, NULL, NULL);
}{...}
return 0;
}{ ... }
/* ... */
void eapol_txcb(uint8_t *eapol_payload, size_t len, bool tx_failure)
{
struct ieee802_1x_hdr *hdr;
struct wpa_eapol_key *key;
struct wpa_sm *sm = &gWpaSm;
u8 isdeauth = 0;
if (len < sizeof(struct ieee802_1x_hdr)) {
return;
}{...}
hdr = (struct ieee802_1x_hdr *) eapol_payload;
if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
return;
}{...}
if (len < (sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key))) {
wpa_printf(MSG_ERROR, "EAPOL TxDone with invalid payload len! (len - %zu)", len);
return;
}{...}
key = (struct wpa_eapol_key *) (hdr + 1);
switch(WPA_SM_STATE(sm)) {
case WPA_FIRST_HALF_4WAY_HANDSHAKE:
case WPA_LAST_HALF_4WAY_HANDSHAKE:
if (WPA_GET_BE16(key->key_data_length) == 0 ||
(WPA_GET_BE16(key->key_info) & WPA_KEY_INFO_SECURE)) {
if (tx_failure) {
wpa_printf(MSG_ERROR, "Eapol message 4/4 tx failure, not installing keys");
return;
}{...}
if (sm->txcb_flags & WPA_4_4_HANDSHAKE_BIT) {
sm->txcb_flags &= ~WPA_4_4_HANDSHAKE_BIT;
isdeauth = wpa_supplicant_send_4_of_4_txcallback(sm);
}{...} else {
wpa_printf(MSG_DEBUG, "4/4 txcb, flags=%d", sm->txcb_flags);
}{...}
}{...} else {
wpa_printf(MSG_DEBUG, "2/4 txcb, flags=%d, txfail %d", sm->txcb_flags, tx_failure);
}{...}
break;...
case WPA_GROUP_HANDSHAKE:
if (sm->txcb_flags & WPA_GROUP_HANDSHAKE_BIT) {
sm->txcb_flags &= ~WPA_GROUP_HANDSHAKE_BIT;
}{...} else {
wpa_printf(MSG_DEBUG, "2/2 txcb, flags=%d", sm->txcb_flags);
}{...}
break;...
case WPA_TKIP_COUNTERMEASURES: isdeauth=WLAN_REASON_MICHAEL_MIC_FAILURE;
break;...
default: break;
}{...}
if(isdeauth) {
wpa_sm_deauthenticate(sm, isdeauth);
}{...}
}{ ... }
bool wpa_sta_in_4way_handshake(void)
{
struct wpa_sm *sm = &gWpaSm;
if ( WPA_SM_STATE(sm) == WPA_MIC_FAILURE || WPA_SM_STATE(sm) == WPA_FIRST_HALF_4WAY_HANDSHAKE
|| WPA_SM_STATE(sm) == WPA_LAST_HALF_4WAY_HANDSHAKE) {
return true;
}{...}
return false;
}{ ... }
bool wpa_sta_is_cur_pmksa_set(void) {
struct wpa_sm *sm = &gWpaSm;
return (pmksa_cache_get_current(sm) != NULL);
}{ ... }
bool wpa_sta_cur_pmksa_matches_akm(void) {
struct wpa_sm *sm = &gWpaSm;
struct rsn_pmksa_cache_entry *pmksa;
pmksa = pmksa_cache_get_current(sm);
return (pmksa != NULL &&
sm->key_mgmt == pmksa->akmp);
}{ ... }
void wpa_sta_clear_curr_pmksa(void) {
struct wpa_sm *sm = &gWpaSm;
if (sm->pmksa)
pmksa_cache_flush(sm->pmksa, NULL, sm->pmk, sm->pmk_len);
pmksa_cache_clear_current(sm);
}{ ... }
struct wpa_sm * get_wpa_sm(void)
{
return &gWpaSm;
}{ ... }
int wpa_sm_set_ap_rsnxe(const u8 *ie, size_t len)
{
struct wpa_sm *sm = &gWpaSm;
os_free(sm->ap_rsnxe);
if (!ie || len == 0) {
wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
sm->ap_rsnxe = NULL;
sm->ap_rsnxe_len = 0;
}{...} else {
wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
sm->ap_rsnxe = os_memdup(ie, len);
if (!sm->ap_rsnxe)
return -1;
sm->ap_rsnxe_len = len;
}{...}
if (sm->ap_rsnxe != NULL) {
sm->sae_pwe = esp_wifi_get_config_sae_pwe_h2e_internal(WIFI_IF_STA);
#ifdef CONFIG_SAE_PK
const u8 *pw = (const u8 *)esp_wifi_sta_get_prof_password_internal();
if (esp_wifi_sta_get_config_sae_pk_internal() != WPA3_SAE_PK_MODE_DISABLED &&
sae_pk_valid_password((const char*)pw)) {
sm->sae_pk = true;
}{...}
/* ... */#endif
}{...}
return 0;
}{ ... }
int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
{
if (!sm)
return -1;
os_free(sm->assoc_rsnxe);
if (!ie || len == 0) {
sm->assoc_rsnxe = NULL;
sm->assoc_rsnxe_len = 0;
}{...} else {
wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
sm->assoc_rsnxe = os_memdup(ie, len);
if (!sm->assoc_rsnxe)
return -1;
sm->assoc_rsnxe_len = len;
}{...}
return 0;
}{ ... }
void wpa_sm_drop_sa(struct wpa_sm *sm)
{
wpa_printf(MSG_DEBUG, "WPA: Clear old PMK and PTK");
sm->ptk_set = 0;
sm->tptk_set = 0;
sm->pmk_len = 0;
os_memset(&sm->ptk, 0, sizeof(sm->ptk));
os_memset(&sm->tptk, 0, sizeof(sm->tptk));
os_memset(&sm->gtk, 0, sizeof(sm->gtk));
os_memset(&sm->igtk, 0, sizeof(sm->igtk));
}{ ... }
#ifdef CONFIG_OWE_STA
struct wpabuf *owe_build_assoc_req(struct wpa_sm *sm, u16 group)
{
struct wpabuf *pub = NULL;
size_t prime_len;
if (group == OWE_DH_GRP19) {
prime_len = OWE_PRIME_LEN;
}{...} else {
wpa_printf(MSG_ERROR, "OWE: Unsupported Diffie-Hellman group");
return NULL;
}{...}
sm->owe_ecdh = crypto_ecdh_init(group);
if (!sm->owe_ecdh) {
wpa_printf(MSG_ERROR, "Initialization of ecdh failed");
return NULL;
}{...}
sm->owe_group = group;
pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
pub = wpabuf_zeropad(pub, prime_len);
if (!pub) {
wpa_printf(MSG_ERROR, "Own public key is NULL");
goto fail;
}{...}
wpa_hexdump_buf(MSG_DEBUG, "Own public key", pub);
if (sm->owe_ie) {
wpabuf_free(sm->owe_ie);
}{...}
sm->owe_ie = wpabuf_alloc(5 + wpabuf_len(pub));
if (!sm->owe_ie) {
wpa_printf(MSG_ERROR, "OWE IE allocation failed");
goto fail;
}{...}
wpabuf_put_u8(sm->owe_ie, WLAN_EID_EXTENSION);
wpabuf_put_u8(sm->owe_ie, 1 + 2 + wpabuf_len(pub));
wpabuf_put_u8(sm->owe_ie, WLAN_EID_EXT_OWE_DH_PARAM);
wpabuf_put_le16(sm->owe_ie, group);
wpabuf_put_buf(sm->owe_ie, pub);
wpabuf_free(pub);
wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element", sm->owe_ie);
return (struct wpabuf *)wpabuf_head(sm->owe_ie);
fail:
wpabuf_free(pub);
crypto_ecdh_deinit(sm->owe_ecdh);
return NULL;
}{ ... }
int owe_process_assoc_resp(const u8 *rsn_ie, size_t rsn_len, const uint8_t *dh_ie, size_t dh_len)
{
size_t prime_len=0,hash_len=0;
struct wpabuf * sh_secret = NULL, *pub = NULL, *hkey = NULL;
const char *info = "OWE Key Generation";
u8 pmkid[SHA256_MAC_LEN], prk[SHA256_MAC_LEN], pmk[SHA256_MAC_LEN];
const u8 *addr[2];
size_t len[2];
u16 group;
struct wpa_sm *sm;
sm = get_wpa_sm();
wpabuf_free(sm->owe_ie);
sm->owe_ie = NULL;
struct wpa_ie_data *parsed_rsn_data;
parsed_rsn_data = os_zalloc(sizeof(struct wpa_ie_data));
if (!parsed_rsn_data) {
wpa_printf(MSG_ERROR, "Memory allocation failed");
return -1;
}{...}
if (rsn_ie && rsn_len && wpa_parse_wpa_ie_rsn(rsn_ie, rsn_len + 2, parsed_rsn_data) != 0) {
goto fail;
}{...}
if (!dh_ie && parsed_rsn_data->num_pmkid == 0) {
wpa_printf(MSG_ERROR, "OWE: Assoc response should either have pmkid or DH IE");
goto fail;
}{...}
if (sm->cur_pmksa && parsed_rsn_data && parsed_rsn_data->num_pmkid == 1 && parsed_rsn_data->pmkid) {
if (os_memcmp(parsed_rsn_data->pmkid, sm->cur_pmksa->pmkid, OWE_PMKID_LEN) == 0) {
wpa_printf(MSG_DEBUG, "OWE: Using PMK caching");
wpa_sm_set_pmk_from_pmksa(sm);
goto done;
}{...} else {
wpa_printf(MSG_DEBUG, "OWE : Invalid PMKID in response");
}{...}
}{...}
if (dh_ie == NULL) {
wpa_printf(MSG_ERROR, "OWE: No Diffie Hellman IE in association response");
goto fail;
}{...}
if (dh_ie && MIN_DH_LEN(dh_len)) {
wpa_printf(MSG_ERROR, "OWE: Invalid Diffie Hellman IE");
goto fail;
}{...}
dh_len += 2;
dh_ie += 3;
dh_len -=3;
group = WPA_GET_LE16(dh_ie);
if ((group != sm->owe_group) || (group != OWE_DH_GRP19)) {
wpa_printf(MSG_ERROR, "OWE: Unexpected Diffie-Hellman group in response");
goto fail;
}{...}
prime_len = OWE_PRIME_LEN;
sh_secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0, dh_ie+2, dh_len-2);
sh_secret = wpabuf_zeropad(sh_secret, prime_len);
if (!sh_secret) {
wpa_printf(MSG_ERROR, "OWE: Invalid peer DH public key");
goto fail;
}{...}
wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", sh_secret);
pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
if (!pub) {
wpa_printf(MSG_ERROR, "No own public key");
goto fail;
}{...}
addr[0] = wpabuf_head(pub);
len[0] = wpabuf_len(pub);
addr[1] = dh_ie + 2;
len[1] = dh_len - 2;
int res = sha256_vector(2, addr, len, pmkid);
if (res < 0 ) {
goto fail;
}{...}
hash_len = SHA256_MAC_LEN;
pub = wpabuf_zeropad(pub, prime_len);
if (!pub) {
goto fail;
}{...}
hkey = wpabuf_alloc(wpabuf_len(pub) + dh_len - 2 + 2);
if (!hkey) {
goto fail;
}{...}
wpabuf_put_buf(hkey, pub);
wpabuf_free(pub);
wpabuf_put_data(hkey, dh_ie + 2, dh_len - 2);
wpabuf_put_le16(hkey, sm->owe_group);
res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey), wpabuf_head(sh_secret), wpabuf_len(sh_secret), prk);
if (res < 0 ) {
goto fail;
}{...}
hash_len = SHA256_MAC_LEN;
wpabuf_free(hkey);
wpabuf_clear_free(sh_secret);
wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *)info,
os_strlen(info), pmk, hash_len);
if (res < 0 ) {
goto fail;
}{...}
forced_memzero(prk, SHA256_MAC_LEN);
wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, OWE_PMKID_LEN);
os_memcpy(sm->pmk,pmk,hash_len);
sm->pmk_len = hash_len;
wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
sm->bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt);
done:
os_free(parsed_rsn_data);
return 0;
fail:
os_free(parsed_rsn_data);
wpabuf_free(pub);
wpabuf_free(hkey);
wpabuf_clear_free(sh_secret);
return -1;
}{ ... }
#endif/* ... */ /* ... */
#endif