2 * hostapd - WPA/RSN IE and KDE definitions
3 * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "eapol_auth/eapol_auth_sm.h"
14 #include "ap_config.h"
15 #include "ieee802_11.h"
17 #include "pmksa_cache_auth.h"
18 #include "wpa_auth_ie.h"
19 #include "wpa_auth_i.h"
22 #ifdef CONFIG_RSN_TESTING
24 #endif /* CONFIG_RSN_TESTING */
27 static int wpa_write_wpa_ie(struct wpa_auth_config *conf, u8 *buf, size_t len)
29 struct wpa_ie_hdr *hdr;
34 hdr = (struct wpa_ie_hdr *) buf;
35 hdr->elem_id = WLAN_EID_VENDOR_SPECIFIC;
36 RSN_SELECTOR_PUT(hdr->oui, WPA_OUI_TYPE);
37 WPA_PUT_LE16(hdr->version, WPA_VERSION);
38 pos = (u8 *) (hdr + 1);
40 suite = wpa_cipher_to_suite(WPA_PROTO_WPA, conf->wpa_group);
42 wpa_printf(MSG_DEBUG, "Invalid group cipher (%d).",
46 RSN_SELECTOR_PUT(pos, suite);
47 pos += WPA_SELECTOR_LEN;
52 num_suites = wpa_cipher_put_suites(pos, conf->wpa_pairwise);
53 if (num_suites == 0) {
54 wpa_printf(MSG_DEBUG, "Invalid pairwise cipher (%d).",
58 pos += num_suites * WPA_SELECTOR_LEN;
59 WPA_PUT_LE16(count, num_suites);
65 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
66 RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
67 pos += WPA_SELECTOR_LEN;
70 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
71 RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
72 pos += WPA_SELECTOR_LEN;
76 if (num_suites == 0) {
77 wpa_printf(MSG_DEBUG, "Invalid key management type (%d).",
81 WPA_PUT_LE16(count, num_suites);
83 /* WPA Capabilities; use defaults, so no need to include it */
85 hdr->len = (pos - buf) - 2;
91 int wpa_write_rsn_ie(struct wpa_auth_config *conf, u8 *buf, size_t len,
94 struct rsn_ie_hdr *hdr;
100 hdr = (struct rsn_ie_hdr *) buf;
101 hdr->elem_id = WLAN_EID_RSN;
102 WPA_PUT_LE16(hdr->version, RSN_VERSION);
103 pos = (u8 *) (hdr + 1);
105 suite = wpa_cipher_to_suite(WPA_PROTO_RSN, conf->wpa_group);
107 wpa_printf(MSG_DEBUG, "Invalid group cipher (%d).",
111 RSN_SELECTOR_PUT(pos, suite);
112 pos += RSN_SELECTOR_LEN;
118 #ifdef CONFIG_RSN_TESTING
120 RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 1));
121 pos += RSN_SELECTOR_LEN;
124 #endif /* CONFIG_RSN_TESTING */
126 res = rsn_cipher_put_suites(pos, conf->rsn_pairwise);
128 pos += res * RSN_SELECTOR_LEN;
130 #ifdef CONFIG_RSN_TESTING
132 RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 2));
133 pos += RSN_SELECTOR_LEN;
136 #endif /* CONFIG_RSN_TESTING */
138 if (num_suites == 0) {
139 wpa_printf(MSG_DEBUG, "Invalid pairwise cipher (%d).",
143 WPA_PUT_LE16(count, num_suites);
149 #ifdef CONFIG_RSN_TESTING
151 RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 1));
152 pos += RSN_SELECTOR_LEN;
155 #endif /* CONFIG_RSN_TESTING */
157 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
158 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X);
159 pos += RSN_SELECTOR_LEN;
162 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
163 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X);
164 pos += RSN_SELECTOR_LEN;
167 #ifdef CONFIG_IEEE80211R_AP
168 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
169 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
170 pos += RSN_SELECTOR_LEN;
174 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
175 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384);
176 pos += RSN_SELECTOR_LEN;
179 #endif /* CONFIG_SHA384 */
180 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
181 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
182 pos += RSN_SELECTOR_LEN;
185 #endif /* CONFIG_IEEE80211R_AP */
186 #ifdef CONFIG_IEEE80211W
187 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
188 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SHA256);
189 pos += RSN_SELECTOR_LEN;
192 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
193 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_SHA256);
194 pos += RSN_SELECTOR_LEN;
197 #endif /* CONFIG_IEEE80211W */
199 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
200 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE);
201 pos += RSN_SELECTOR_LEN;
204 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
205 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
206 pos += RSN_SELECTOR_LEN;
209 #endif /* CONFIG_SAE */
210 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
211 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B);
212 pos += RSN_SELECTOR_LEN;
215 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
216 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192);
217 pos += RSN_SELECTOR_LEN;
221 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
222 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA256);
223 pos += RSN_SELECTOR_LEN;
226 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
227 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA384);
228 pos += RSN_SELECTOR_LEN;
231 #ifdef CONFIG_IEEE80211R_AP
232 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
233 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
234 pos += RSN_SELECTOR_LEN;
237 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
238 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
239 pos += RSN_SELECTOR_LEN;
242 #endif /* CONFIG_IEEE80211R_AP */
243 #endif /* CONFIG_FILS */
245 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
246 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_OWE);
247 pos += RSN_SELECTOR_LEN;
250 #endif /* CONFIG_OWE */
252 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
253 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_DPP);
254 pos += RSN_SELECTOR_LEN;
257 #endif /* CONFIG_DPP */
259 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_OSEN) {
260 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_OSEN);
261 pos += RSN_SELECTOR_LEN;
264 #endif /* CONFIG_HS20 */
266 #ifdef CONFIG_RSN_TESTING
268 RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 2));
269 pos += RSN_SELECTOR_LEN;
272 #endif /* CONFIG_RSN_TESTING */
274 if (num_suites == 0) {
275 wpa_printf(MSG_DEBUG, "Invalid key management type (%d).",
279 WPA_PUT_LE16(count, num_suites);
281 /* RSN Capabilities */
283 if (conf->rsn_preauth)
284 capab |= WPA_CAPABILITY_PREAUTH;
285 if (conf->wmm_enabled) {
286 /* 4 PTKSA replay counters when using WMM */
287 capab |= (RSN_NUM_REPLAY_COUNTERS_16 << 2);
289 #ifdef CONFIG_IEEE80211W
290 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
291 capab |= WPA_CAPABILITY_MFPC;
292 if (conf->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED)
293 capab |= WPA_CAPABILITY_MFPR;
295 #endif /* CONFIG_IEEE80211W */
298 capab |= WPA_CAPABILITY_OCVC;
299 #endif /* CONFIG_OCV */
300 #ifdef CONFIG_RSN_TESTING
302 capab |= BIT(8) | BIT(15);
303 #endif /* CONFIG_RSN_TESTING */
304 WPA_PUT_LE16(pos, capab);
308 if (2 + PMKID_LEN > buf + len - pos)
311 WPA_PUT_LE16(pos, 1);
313 os_memcpy(pos, pmkid, PMKID_LEN);
317 #ifdef CONFIG_IEEE80211W
318 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION &&
319 conf->group_mgmt_cipher != WPA_CIPHER_AES_128_CMAC) {
320 if (2 + 4 > buf + len - pos)
324 WPA_PUT_LE16(pos, 0);
328 /* Management Group Cipher Suite */
329 switch (conf->group_mgmt_cipher) {
330 case WPA_CIPHER_AES_128_CMAC:
331 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
333 case WPA_CIPHER_BIP_GMAC_128:
334 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_128);
336 case WPA_CIPHER_BIP_GMAC_256:
337 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_256);
339 case WPA_CIPHER_BIP_CMAC_256:
340 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_CMAC_256);
343 wpa_printf(MSG_DEBUG,
344 "Invalid group management cipher (0x%x)",
345 conf->group_mgmt_cipher);
348 pos += RSN_SELECTOR_LEN;
350 #endif /* CONFIG_IEEE80211W */
352 #ifdef CONFIG_RSN_TESTING
355 * Fill in any defined fields and add extra data to the end of
358 int pmkid_count_set = pmkid != NULL;
359 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION)
362 WPA_PUT_LE16(pos, 0);
364 if (conf->ieee80211w == NO_MGMT_FRAME_PROTECTION) {
365 /* Management Group Cipher Suite */
366 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
367 pos += RSN_SELECTOR_LEN;
370 os_memset(pos, 0x12, 17);
373 #endif /* CONFIG_RSN_TESTING */
375 hdr->len = (pos - buf) - 2;
381 static u8 * wpa_write_osen(struct wpa_auth_config *conf, u8 *eid)
386 *eid++ = WLAN_EID_VENDOR_SPECIFIC;
387 len = eid++; /* to be filled */
388 WPA_PUT_BE24(eid, OUI_WFA);
390 *eid++ = HS20_OSEN_OUI_TYPE;
392 /* Group Data Cipher Suite */
393 RSN_SELECTOR_PUT(eid, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
394 eid += RSN_SELECTOR_LEN;
396 /* Pairwise Cipher Suite Count and List */
397 WPA_PUT_LE16(eid, 1);
399 RSN_SELECTOR_PUT(eid, RSN_CIPHER_SUITE_CCMP);
400 eid += RSN_SELECTOR_LEN;
402 /* AKM Suite Count and List */
403 WPA_PUT_LE16(eid, 1);
405 RSN_SELECTOR_PUT(eid, RSN_AUTH_KEY_MGMT_OSEN);
406 eid += RSN_SELECTOR_LEN;
408 /* RSN Capabilities */
410 if (conf->wmm_enabled) {
411 /* 4 PTKSA replay counters when using WMM */
412 capab |= (RSN_NUM_REPLAY_COUNTERS_16 << 2);
414 #ifdef CONFIG_IEEE80211W
415 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
416 capab |= WPA_CAPABILITY_MFPC;
417 if (conf->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED)
418 capab |= WPA_CAPABILITY_MFPR;
420 #endif /* CONFIG_IEEE80211W */
423 capab |= WPA_CAPABILITY_OCVC;
424 #endif /* CONFIG_OCV */
425 WPA_PUT_LE16(eid, capab);
428 *len = eid - len - 1;
434 int wpa_auth_gen_wpa_ie(struct wpa_authenticator *wpa_auth)
439 #ifdef CONFIG_TESTING_OPTIONS
440 if (wpa_auth->conf.own_ie_override_len) {
441 wpa_hexdump(MSG_DEBUG, "WPA: Forced own IE(s) for testing",
442 wpa_auth->conf.own_ie_override,
443 wpa_auth->conf.own_ie_override_len);
444 os_free(wpa_auth->wpa_ie);
446 os_malloc(wpa_auth->conf.own_ie_override_len);
447 if (wpa_auth->wpa_ie == NULL)
449 os_memcpy(wpa_auth->wpa_ie, wpa_auth->conf.own_ie_override,
450 wpa_auth->conf.own_ie_override_len);
451 wpa_auth->wpa_ie_len = wpa_auth->conf.own_ie_override_len;
454 #endif /* CONFIG_TESTING_OPTIONS */
458 if (wpa_auth->conf.wpa == WPA_PROTO_OSEN) {
459 pos = wpa_write_osen(&wpa_auth->conf, pos);
461 if (wpa_auth->conf.wpa & WPA_PROTO_RSN) {
462 res = wpa_write_rsn_ie(&wpa_auth->conf,
463 pos, buf + sizeof(buf) - pos, NULL);
468 #ifdef CONFIG_IEEE80211R_AP
469 if (wpa_key_mgmt_ft(wpa_auth->conf.wpa_key_mgmt)) {
470 res = wpa_write_mdie(&wpa_auth->conf, pos,
471 buf + sizeof(buf) - pos);
476 #endif /* CONFIG_IEEE80211R_AP */
477 if (wpa_auth->conf.wpa & WPA_PROTO_WPA) {
478 res = wpa_write_wpa_ie(&wpa_auth->conf,
479 pos, buf + sizeof(buf) - pos);
485 os_free(wpa_auth->wpa_ie);
486 wpa_auth->wpa_ie = os_malloc(pos - buf);
487 if (wpa_auth->wpa_ie == NULL)
489 os_memcpy(wpa_auth->wpa_ie, buf, pos - buf);
490 wpa_auth->wpa_ie_len = pos - buf;
496 u8 * wpa_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len,
497 const u8 *data2, size_t data2_len)
499 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
500 *pos++ = RSN_SELECTOR_LEN + data_len + data2_len;
501 RSN_SELECTOR_PUT(pos, kde);
502 pos += RSN_SELECTOR_LEN;
503 os_memcpy(pos, data, data_len);
506 os_memcpy(pos, data2, data2_len);
513 struct wpa_auth_okc_iter_data {
514 struct rsn_pmksa_cache_entry *pmksa;
521 static int wpa_auth_okc_iter(struct wpa_authenticator *a, void *ctx)
523 struct wpa_auth_okc_iter_data *data = ctx;
524 data->pmksa = pmksa_cache_get_okc(a->pmksa, data->aa, data->spa,
532 int wpa_validate_wpa_ie(struct wpa_authenticator *wpa_auth,
533 struct wpa_state_machine *sm, int freq,
534 const u8 *wpa_ie, size_t wpa_ie_len,
535 const u8 *mdie, size_t mdie_len,
536 const u8 *owe_dh, size_t owe_dh_len)
538 struct wpa_ie_data data;
539 int ciphers, key_mgmt, res, version;
542 const u8 *pmkid = NULL;
544 if (wpa_auth == NULL || sm == NULL)
545 return WPA_NOT_ENABLED;
547 if (wpa_ie == NULL || wpa_ie_len < 1)
548 return WPA_INVALID_IE;
550 if (wpa_ie[0] == WLAN_EID_RSN)
551 version = WPA_PROTO_RSN;
553 version = WPA_PROTO_WPA;
555 if (!(wpa_auth->conf.wpa & version)) {
556 wpa_printf(MSG_DEBUG, "Invalid WPA proto (%d) from " MACSTR,
557 version, MAC2STR(sm->addr));
558 return WPA_INVALID_PROTO;
561 if (version == WPA_PROTO_RSN) {
562 res = wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, &data);
563 if (!data.has_pairwise)
564 data.pairwise_cipher = wpa_default_rsn_cipher(freq);
566 data.group_cipher = wpa_default_rsn_cipher(freq);
568 if (wpa_key_mgmt_ft(data.key_mgmt) && !mdie &&
569 !wpa_key_mgmt_only_ft(data.key_mgmt)) {
570 /* Workaround for some HP and Epson printers that seem
571 * to incorrectly copy the FT-PSK + WPA-PSK AKMs from AP
572 * advertised RSNE to Association Request frame. */
573 wpa_printf(MSG_DEBUG,
574 "RSN: FT set in RSNE AKM but MDE is missing from "
576 " - ignore FT AKM(s) because there's also a non-FT AKM",
578 data.key_mgmt &= ~WPA_KEY_MGMT_FT;
581 selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
584 else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
585 selector = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
586 else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
587 selector = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
589 #ifdef CONFIG_IEEE80211R_AP
590 else if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384)
591 selector = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
592 else if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256)
593 selector = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
594 #endif /* CONFIG_IEEE80211R_AP */
595 else if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384)
596 selector = RSN_AUTH_KEY_MGMT_FILS_SHA384;
597 else if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256)
598 selector = RSN_AUTH_KEY_MGMT_FILS_SHA256;
599 #endif /* CONFIG_FILS */
600 #ifdef CONFIG_IEEE80211R_AP
602 else if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384)
603 selector = RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384;
604 #endif /* CONFIG_SHA384 */
605 else if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
606 selector = RSN_AUTH_KEY_MGMT_FT_802_1X;
607 else if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK)
608 selector = RSN_AUTH_KEY_MGMT_FT_PSK;
609 #endif /* CONFIG_IEEE80211R_AP */
610 #ifdef CONFIG_IEEE80211W
611 else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
612 selector = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
613 else if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
614 selector = RSN_AUTH_KEY_MGMT_PSK_SHA256;
615 #endif /* CONFIG_IEEE80211W */
617 else if (data.key_mgmt & WPA_KEY_MGMT_SAE)
618 selector = RSN_AUTH_KEY_MGMT_SAE;
619 else if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE)
620 selector = RSN_AUTH_KEY_MGMT_FT_SAE;
621 #endif /* CONFIG_SAE */
622 else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
623 selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
624 else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
625 selector = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
627 else if (data.key_mgmt & WPA_KEY_MGMT_OWE)
628 selector = RSN_AUTH_KEY_MGMT_OWE;
629 #endif /* CONFIG_OWE */
631 else if (data.key_mgmt & WPA_KEY_MGMT_DPP)
632 selector = RSN_AUTH_KEY_MGMT_DPP;
633 #endif /* CONFIG_DPP */
635 else if (data.key_mgmt & WPA_KEY_MGMT_OSEN)
636 selector = RSN_AUTH_KEY_MGMT_OSEN;
637 #endif /* CONFIG_HS20 */
638 wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
640 selector = wpa_cipher_to_suite(WPA_PROTO_RSN,
641 data.pairwise_cipher);
643 selector = RSN_CIPHER_SUITE_CCMP;
644 wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
646 selector = wpa_cipher_to_suite(WPA_PROTO_RSN,
649 selector = RSN_CIPHER_SUITE_CCMP;
650 wpa_auth->dot11RSNAGroupCipherSelected = selector;
652 res = wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, &data);
654 selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
655 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
656 selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
657 else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
658 selector = WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X;
659 wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
661 selector = wpa_cipher_to_suite(WPA_PROTO_WPA,
662 data.pairwise_cipher);
664 selector = RSN_CIPHER_SUITE_TKIP;
665 wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
667 selector = wpa_cipher_to_suite(WPA_PROTO_WPA,
670 selector = WPA_CIPHER_SUITE_TKIP;
671 wpa_auth->dot11RSNAGroupCipherSelected = selector;
674 wpa_printf(MSG_DEBUG, "Failed to parse WPA/RSN IE from "
675 MACSTR " (res=%d)", MAC2STR(sm->addr), res);
676 wpa_hexdump(MSG_DEBUG, "WPA/RSN IE", wpa_ie, wpa_ie_len);
677 return WPA_INVALID_IE;
680 if (data.group_cipher != wpa_auth->conf.wpa_group) {
681 wpa_printf(MSG_DEBUG, "Invalid WPA group cipher (0x%x) from "
682 MACSTR, data.group_cipher, MAC2STR(sm->addr));
683 return WPA_INVALID_GROUP;
686 key_mgmt = data.key_mgmt & wpa_auth->conf.wpa_key_mgmt;
688 wpa_printf(MSG_DEBUG, "Invalid WPA key mgmt (0x%x) from "
689 MACSTR, data.key_mgmt, MAC2STR(sm->addr));
690 return WPA_INVALID_AKMP;
694 else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
695 sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
696 else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
697 sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
699 #ifdef CONFIG_IEEE80211R_AP
700 else if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384)
701 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA384;
702 else if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256)
703 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA256;
704 #endif /* CONFIG_IEEE80211R_AP */
705 else if (key_mgmt & WPA_KEY_MGMT_FILS_SHA384)
706 sm->wpa_key_mgmt = WPA_KEY_MGMT_FILS_SHA384;
707 else if (key_mgmt & WPA_KEY_MGMT_FILS_SHA256)
708 sm->wpa_key_mgmt = WPA_KEY_MGMT_FILS_SHA256;
709 #endif /* CONFIG_FILS */
710 #ifdef CONFIG_IEEE80211R_AP
712 else if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384)
713 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
714 #endif /* CONFIG_SHA384 */
715 else if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
716 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
717 else if (key_mgmt & WPA_KEY_MGMT_FT_PSK)
718 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_PSK;
719 #endif /* CONFIG_IEEE80211R_AP */
720 #ifdef CONFIG_IEEE80211W
721 else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
722 sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
723 else if (key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
724 sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
725 #endif /* CONFIG_IEEE80211W */
727 else if (key_mgmt & WPA_KEY_MGMT_SAE)
728 sm->wpa_key_mgmt = WPA_KEY_MGMT_SAE;
729 else if (key_mgmt & WPA_KEY_MGMT_FT_SAE)
730 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_SAE;
731 #endif /* CONFIG_SAE */
732 else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X)
733 sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
735 else if (key_mgmt & WPA_KEY_MGMT_OWE)
736 sm->wpa_key_mgmt = WPA_KEY_MGMT_OWE;
737 #endif /* CONFIG_OWE */
739 else if (key_mgmt & WPA_KEY_MGMT_DPP)
740 sm->wpa_key_mgmt = WPA_KEY_MGMT_DPP;
741 #endif /* CONFIG_DPP */
743 else if (key_mgmt & WPA_KEY_MGMT_OSEN)
744 sm->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
745 #endif /* CONFIG_HS20 */
747 sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
749 if (version == WPA_PROTO_RSN)
750 ciphers = data.pairwise_cipher & wpa_auth->conf.rsn_pairwise;
752 ciphers = data.pairwise_cipher & wpa_auth->conf.wpa_pairwise;
754 wpa_printf(MSG_DEBUG, "Invalid %s pairwise cipher (0x%x) "
756 version == WPA_PROTO_RSN ? "RSN" : "WPA",
757 data.pairwise_cipher, MAC2STR(sm->addr));
758 return WPA_INVALID_PAIRWISE;
761 #ifdef CONFIG_IEEE80211W
762 if (wpa_auth->conf.ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
763 if (!(data.capabilities & WPA_CAPABILITY_MFPC)) {
764 wpa_printf(MSG_DEBUG, "Management frame protection "
765 "required, but client did not enable it");
766 return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
769 if (data.mgmt_group_cipher != wpa_auth->conf.group_mgmt_cipher)
771 wpa_printf(MSG_DEBUG, "Unsupported management group "
772 "cipher %d", data.mgmt_group_cipher);
773 return WPA_INVALID_MGMT_GROUP_CIPHER;
778 if (wpa_auth->conf.ieee80211w == MGMT_FRAME_PROTECTION_OPTIONAL &&
779 wpa_auth->conf.sae_require_mfp &&
780 wpa_key_mgmt_sae(sm->wpa_key_mgmt) &&
781 !(data.capabilities & WPA_CAPABILITY_MFPC)) {
782 wpa_printf(MSG_DEBUG,
783 "Management frame protection required with SAE, but client did not enable it");
784 return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
786 #endif /* CONFIG_SAE */
789 if ((data.capabilities & WPA_CAPABILITY_OCVC) &&
790 !(data.capabilities & WPA_CAPABILITY_MFPC)) {
791 wpa_printf(MSG_DEBUG,
792 "Management frame protection required with OCV, but client did not enable it");
793 return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
795 wpa_auth_set_ocv(sm, wpa_auth->conf.ocv &&
796 (data.capabilities & WPA_CAPABILITY_OCVC));
797 #endif /* CONFIG_OCV */
799 if (wpa_auth->conf.ieee80211w == NO_MGMT_FRAME_PROTECTION ||
800 !(data.capabilities & WPA_CAPABILITY_MFPC))
801 sm->mgmt_frame_prot = 0;
803 sm->mgmt_frame_prot = 1;
805 if (sm->mgmt_frame_prot && (ciphers & WPA_CIPHER_TKIP)) {
806 wpa_printf(MSG_DEBUG,
807 "Management frame protection cannot use TKIP");
808 return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
810 #endif /* CONFIG_IEEE80211W */
812 #ifdef CONFIG_IEEE80211R_AP
813 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
814 if (mdie == NULL || mdie_len < MOBILITY_DOMAIN_ID_LEN + 1) {
815 wpa_printf(MSG_DEBUG, "RSN: Trying to use FT, but "
816 "MDIE not included");
817 return WPA_INVALID_MDIE;
819 if (os_memcmp(mdie, wpa_auth->conf.mobility_domain,
820 MOBILITY_DOMAIN_ID_LEN) != 0) {
821 wpa_hexdump(MSG_DEBUG, "RSN: Attempted to use unknown "
822 "MDIE", mdie, MOBILITY_DOMAIN_ID_LEN);
823 return WPA_INVALID_MDIE;
825 } else if (mdie != NULL) {
826 wpa_printf(MSG_DEBUG,
827 "RSN: Trying to use non-FT AKM suite, but MDIE included");
828 return WPA_INVALID_AKMP;
830 #endif /* CONFIG_IEEE80211R_AP */
833 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && !owe_dh) {
834 wpa_printf(MSG_DEBUG,
835 "OWE: No Diffie-Hellman Parameter element");
836 return WPA_INVALID_AKMP;
839 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && owe_dh) {
840 /* Diffie-Hellman Parameter element can be used with DPP as
841 * well, so allow this to proceed. */
843 #endif /* CONFIG_DPP */
844 if (sm->wpa_key_mgmt != WPA_KEY_MGMT_OWE && owe_dh) {
845 wpa_printf(MSG_DEBUG,
846 "OWE: Unexpected Diffie-Hellman Parameter element with non-OWE AKM");
847 return WPA_INVALID_AKMP;
849 #endif /* CONFIG_OWE */
851 sm->pairwise = wpa_pick_pairwise_cipher(ciphers, 0);
852 if (sm->pairwise < 0)
853 return WPA_INVALID_PAIRWISE;
855 /* TODO: clear WPA/WPA2 state if STA changes from one to another */
856 if (wpa_ie[0] == WLAN_EID_RSN)
857 sm->wpa = WPA_VERSION_WPA2;
859 sm->wpa = WPA_VERSION_WPA;
861 #if defined(CONFIG_IEEE80211R_AP) && defined(CONFIG_FILS)
862 if ((sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256 ||
863 sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384) &&
864 (sm->auth_alg == WLAN_AUTH_FILS_SK ||
865 sm->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
866 sm->auth_alg == WLAN_AUTH_FILS_PK) &&
867 (data.num_pmkid != 1 || !data.pmkid || !sm->pmk_r1_name_valid ||
868 os_memcmp_const(data.pmkid, sm->pmk_r1_name,
869 WPA_PMK_NAME_LEN) != 0)) {
870 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
871 "No PMKR1Name match for FILS+FT");
872 return WPA_INVALID_PMKID;
874 #endif /* CONFIG_IEEE80211R_AP && CONFIG_FILS */
877 for (i = 0; i < data.num_pmkid; i++) {
878 wpa_hexdump(MSG_DEBUG, "RSN IE: STA PMKID",
879 &data.pmkid[i * PMKID_LEN], PMKID_LEN);
880 sm->pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sm->addr,
881 &data.pmkid[i * PMKID_LEN]);
883 pmkid = sm->pmksa->pmkid;
887 for (i = 0; sm->pmksa == NULL && wpa_auth->conf.okc &&
888 i < data.num_pmkid; i++) {
889 struct wpa_auth_okc_iter_data idata;
891 idata.aa = wpa_auth->addr;
892 idata.spa = sm->addr;
893 idata.pmkid = &data.pmkid[i * PMKID_LEN];
894 wpa_auth_for_each_auth(wpa_auth, wpa_auth_okc_iter, &idata);
896 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
897 "OKC match for PMKID");
898 sm->pmksa = pmksa_cache_add_okc(wpa_auth->pmksa,
906 if (sm->pmksa && pmkid) {
907 struct vlan_description *vlan;
909 vlan = sm->pmksa->vlan_desc;
910 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
911 "PMKID found from PMKSA cache eap_type=%d vlan=%d%s",
912 sm->pmksa->eap_type_authsrv,
913 vlan ? vlan->untagged : 0,
914 (vlan && vlan->tagged[0]) ? "+" : "");
915 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmkid, PMKID_LEN);
919 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_SAE && data.num_pmkid &&
921 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
922 "No PMKSA cache entry found for SAE");
923 return WPA_INVALID_PMKID;
925 #endif /* CONFIG_SAE */
928 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && !sm->pmksa) {
929 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
930 "No PMKSA cache entry found for DPP");
931 return WPA_INVALID_PMKID;
933 #endif /* CONFIG_DPP */
935 if (sm->wpa_ie == NULL || sm->wpa_ie_len < wpa_ie_len) {
937 sm->wpa_ie = os_malloc(wpa_ie_len);
938 if (sm->wpa_ie == NULL)
939 return WPA_ALLOC_FAIL;
941 os_memcpy(sm->wpa_ie, wpa_ie, wpa_ie_len);
942 sm->wpa_ie_len = wpa_ie_len;
949 int wpa_validate_osen(struct wpa_authenticator *wpa_auth,
950 struct wpa_state_machine *sm,
951 const u8 *osen_ie, size_t osen_ie_len)
953 if (wpa_auth == NULL || sm == NULL)
956 /* TODO: parse OSEN element */
957 sm->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
958 sm->mgmt_frame_prot = 1;
959 sm->pairwise = WPA_CIPHER_CCMP;
960 sm->wpa = WPA_VERSION_WPA2;
962 if (sm->wpa_ie == NULL || sm->wpa_ie_len < osen_ie_len) {
964 sm->wpa_ie = os_malloc(osen_ie_len);
965 if (sm->wpa_ie == NULL)
969 os_memcpy(sm->wpa_ie, osen_ie, osen_ie_len);
970 sm->wpa_ie_len = osen_ie_len;
975 #endif /* CONFIG_HS20 */
979 * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
980 * @pos: Pointer to the IE header
981 * @end: Pointer to the end of the Key Data buffer
982 * @ie: Pointer to parsed IE data
983 * Returns: 0 on success, 1 if end mark is found, -1 on failure
985 static int wpa_parse_generic(const u8 *pos, const u8 *end,
986 struct wpa_eapol_ie_parse *ie)
992 RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE &&
993 pos[2 + WPA_SELECTOR_LEN] == 1 &&
994 pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
996 ie->wpa_ie_len = pos[1] + 2;
1000 if (pos[1] >= 4 && WPA_GET_BE32(pos + 2) == OSEN_IE_VENDOR_TYPE) {
1002 ie->osen_len = pos[1] + 2;
1006 if (1 + RSN_SELECTOR_LEN < end - pos &&
1007 pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
1008 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) {
1009 ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
1013 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
1014 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) {
1015 ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
1016 ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
1020 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
1021 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) {
1022 ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN;
1023 ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN;
1027 #ifdef CONFIG_IEEE80211W
1028 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
1029 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) {
1030 ie->igtk = pos + 2 + RSN_SELECTOR_LEN;
1031 ie->igtk_len = pos[1] - RSN_SELECTOR_LEN;
1034 #endif /* CONFIG_IEEE80211W */
1037 if (pos[1] >= RSN_SELECTOR_LEN + 1 &&
1038 RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_REQ) {
1039 ie->ip_addr_req = pos + 2 + RSN_SELECTOR_LEN;
1040 wpa_hexdump(MSG_DEBUG, "WPA: IP Address Request in EAPOL-Key",
1041 ie->ip_addr_req, pos[1] - RSN_SELECTOR_LEN);
1045 if (pos[1] >= RSN_SELECTOR_LEN + 3 * 4 &&
1046 RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_ALLOC) {
1047 ie->ip_addr_alloc = pos + 2 + RSN_SELECTOR_LEN;
1048 wpa_hexdump(MSG_DEBUG,
1049 "WPA: IP Address Allocation in EAPOL-Key",
1050 ie->ip_addr_alloc, pos[1] - RSN_SELECTOR_LEN);
1053 #endif /* CONFIG_P2P */
1056 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
1057 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_OCI) {
1058 ie->oci = pos + 2 + RSN_SELECTOR_LEN;
1059 ie->oci_len = pos[1] - RSN_SELECTOR_LEN;
1062 #endif /* CONFIG_OCV */
1069 * wpa_parse_kde_ies - Parse EAPOL-Key Key Data IEs
1070 * @buf: Pointer to the Key Data buffer
1071 * @len: Key Data Length
1072 * @ie: Pointer to parsed IE data
1073 * Returns: 0 on success, -1 on failure
1075 int wpa_parse_kde_ies(const u8 *buf, size_t len, struct wpa_eapol_ie_parse *ie)
1077 const u8 *pos, *end;
1080 os_memset(ie, 0, sizeof(*ie));
1081 for (pos = buf, end = pos + len; end - pos > 1; pos += 2 + pos[1]) {
1082 if (pos[0] == 0xdd &&
1083 ((pos == buf + len - 1) || pos[1] == 0)) {
1084 /* Ignore padding */
1087 if (2 + pos[1] > end - pos) {
1088 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data "
1089 "underflow (ie=%d len=%d pos=%d)",
1090 pos[0], pos[1], (int) (pos - buf));
1091 wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data",
1096 if (*pos == WLAN_EID_RSN) {
1098 ie->rsn_ie_len = pos[1] + 2;
1099 #ifdef CONFIG_IEEE80211R_AP
1100 } else if (*pos == WLAN_EID_MOBILITY_DOMAIN) {
1102 ie->mdie_len = pos[1] + 2;
1103 } else if (*pos == WLAN_EID_FAST_BSS_TRANSITION) {
1105 ie->ftie_len = pos[1] + 2;
1106 #endif /* CONFIG_IEEE80211R_AP */
1107 } else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
1108 ret = wpa_parse_generic(pos, end, ie);
1116 wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key "
1117 "Key Data IE", pos, 2 + pos[1]);
1125 int wpa_auth_uses_mfp(struct wpa_state_machine *sm)
1127 return sm ? sm->mgmt_frame_prot : 0;
1133 void wpa_auth_set_ocv(struct wpa_state_machine *sm, int ocv)
1136 sm->ocv_enabled = ocv;
1140 int wpa_auth_uses_ocv(struct wpa_state_machine *sm)
1142 return sm ? sm->ocv_enabled : 0;
1145 #endif /* CONFIG_OCV */
1149 u8 * wpa_auth_write_assoc_resp_owe(struct wpa_state_machine *sm,
1150 u8 *pos, size_t max_len,
1151 const u8 *req_ies, size_t req_ies_len)
1154 struct wpa_auth_config *conf;
1158 conf = &sm->wpa_auth->conf;
1160 #ifdef CONFIG_TESTING_OPTIONS
1161 if (conf->own_ie_override_len) {
1162 if (max_len < conf->own_ie_override_len)
1164 wpa_hexdump(MSG_DEBUG, "WPA: Forced own IE(s) for testing",
1165 conf->own_ie_override, conf->own_ie_override_len);
1166 os_memcpy(pos, conf->own_ie_override,
1167 conf->own_ie_override_len);
1168 return pos + conf->own_ie_override_len;
1170 #endif /* CONFIG_TESTING_OPTIONS */
1172 res = wpa_write_rsn_ie(conf, pos, max_len,
1173 sm->pmksa ? sm->pmksa->pmkid : NULL);
1178 #endif /* CONFIG_OWE */