2 * hostapd - WPA/RSN IE and KDE definitions
3 * Copyright (c) 2004-2015, 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
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;
173 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
174 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
175 pos += RSN_SELECTOR_LEN;
178 #endif /* CONFIG_IEEE80211R */
179 #ifdef CONFIG_IEEE80211W
180 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
181 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SHA256);
182 pos += RSN_SELECTOR_LEN;
185 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
186 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_SHA256);
187 pos += RSN_SELECTOR_LEN;
190 #endif /* CONFIG_IEEE80211W */
192 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
193 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE);
194 pos += RSN_SELECTOR_LEN;
197 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
198 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
199 pos += RSN_SELECTOR_LEN;
202 #endif /* CONFIG_SAE */
203 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
204 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B);
205 pos += RSN_SELECTOR_LEN;
208 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
209 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192);
210 pos += RSN_SELECTOR_LEN;
214 #ifdef CONFIG_RSN_TESTING
216 RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 2));
217 pos += RSN_SELECTOR_LEN;
220 #endif /* CONFIG_RSN_TESTING */
222 if (num_suites == 0) {
223 wpa_printf(MSG_DEBUG, "Invalid key management type (%d).",
227 WPA_PUT_LE16(count, num_suites);
229 /* RSN Capabilities */
231 if (conf->rsn_preauth)
232 capab |= WPA_CAPABILITY_PREAUTH;
234 capab |= WPA_CAPABILITY_PEERKEY_ENABLED;
235 if (conf->wmm_enabled) {
236 /* 4 PTKSA replay counters when using WMM */
237 capab |= (RSN_NUM_REPLAY_COUNTERS_16 << 2);
239 #ifdef CONFIG_IEEE80211W
240 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
241 capab |= WPA_CAPABILITY_MFPC;
242 if (conf->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED)
243 capab |= WPA_CAPABILITY_MFPR;
245 #endif /* CONFIG_IEEE80211W */
246 #ifdef CONFIG_RSN_TESTING
248 capab |= BIT(8) | BIT(14) | BIT(15);
249 #endif /* CONFIG_RSN_TESTING */
250 WPA_PUT_LE16(pos, capab);
254 if (pos + 2 + PMKID_LEN > buf + len)
257 WPA_PUT_LE16(pos, 1);
259 os_memcpy(pos, pmkid, PMKID_LEN);
263 #ifdef CONFIG_IEEE80211W
264 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION &&
265 conf->group_mgmt_cipher != WPA_CIPHER_AES_128_CMAC) {
266 if (pos + 2 + 4 > buf + len)
270 WPA_PUT_LE16(pos, 0);
274 /* Management Group Cipher Suite */
275 switch (conf->group_mgmt_cipher) {
276 case WPA_CIPHER_AES_128_CMAC:
277 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
279 case WPA_CIPHER_BIP_GMAC_128:
280 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_128);
282 case WPA_CIPHER_BIP_GMAC_256:
283 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_256);
285 case WPA_CIPHER_BIP_CMAC_256:
286 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_CMAC_256);
289 wpa_printf(MSG_DEBUG,
290 "Invalid group management cipher (0x%x)",
291 conf->group_mgmt_cipher);
294 pos += RSN_SELECTOR_LEN;
296 #endif /* CONFIG_IEEE80211W */
298 #ifdef CONFIG_RSN_TESTING
301 * Fill in any defined fields and add extra data to the end of
304 int pmkid_count_set = pmkid != NULL;
305 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION)
308 WPA_PUT_LE16(pos, 0);
310 if (conf->ieee80211w == NO_MGMT_FRAME_PROTECTION) {
311 /* Management Group Cipher Suite */
312 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
313 pos += RSN_SELECTOR_LEN;
316 os_memset(pos, 0x12, 17);
319 #endif /* CONFIG_RSN_TESTING */
321 hdr->len = (pos - buf) - 2;
327 static u8 * wpa_write_osen(struct wpa_auth_config *conf, u8 *eid)
332 *eid++ = WLAN_EID_VENDOR_SPECIFIC;
333 len = eid++; /* to be filled */
334 WPA_PUT_BE24(eid, OUI_WFA);
336 *eid++ = HS20_OSEN_OUI_TYPE;
338 /* Group Data Cipher Suite */
339 RSN_SELECTOR_PUT(eid, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
340 eid += RSN_SELECTOR_LEN;
342 /* Pairwise Cipher Suite Count and List */
343 WPA_PUT_LE16(eid, 1);
345 RSN_SELECTOR_PUT(eid, RSN_CIPHER_SUITE_CCMP);
346 eid += RSN_SELECTOR_LEN;
348 /* AKM Suite Count and List */
349 WPA_PUT_LE16(eid, 1);
351 RSN_SELECTOR_PUT(eid, RSN_AUTH_KEY_MGMT_OSEN);
352 eid += RSN_SELECTOR_LEN;
354 /* RSN Capabilities */
356 if (conf->wmm_enabled) {
357 /* 4 PTKSA replay counters when using WMM */
358 capab |= (RSN_NUM_REPLAY_COUNTERS_16 << 2);
360 #ifdef CONFIG_IEEE80211W
361 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
362 capab |= WPA_CAPABILITY_MFPC;
363 if (conf->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED)
364 capab |= WPA_CAPABILITY_MFPR;
366 #endif /* CONFIG_IEEE80211W */
367 WPA_PUT_LE16(eid, capab);
370 *len = eid - len - 1;
376 int wpa_auth_gen_wpa_ie(struct wpa_authenticator *wpa_auth)
381 #ifdef CONFIG_TESTING_OPTIONS
382 if (wpa_auth->conf.own_ie_override_len) {
383 wpa_hexdump(MSG_DEBUG, "WPA: Forced own IE(s) for testing",
384 wpa_auth->conf.own_ie_override,
385 wpa_auth->conf.own_ie_override_len);
386 os_free(wpa_auth->wpa_ie);
388 os_malloc(wpa_auth->conf.own_ie_override_len);
389 if (wpa_auth->wpa_ie == NULL)
391 os_memcpy(wpa_auth->wpa_ie, wpa_auth->conf.own_ie_override,
392 wpa_auth->conf.own_ie_override_len);
393 wpa_auth->wpa_ie_len = wpa_auth->conf.own_ie_override_len;
396 #endif /* CONFIG_TESTING_OPTIONS */
400 if (wpa_auth->conf.wpa == WPA_PROTO_OSEN) {
401 pos = wpa_write_osen(&wpa_auth->conf, pos);
403 if (wpa_auth->conf.wpa & WPA_PROTO_RSN) {
404 res = wpa_write_rsn_ie(&wpa_auth->conf,
405 pos, buf + sizeof(buf) - pos, NULL);
410 #ifdef CONFIG_IEEE80211R
411 if (wpa_key_mgmt_ft(wpa_auth->conf.wpa_key_mgmt)) {
412 res = wpa_write_mdie(&wpa_auth->conf, pos,
413 buf + sizeof(buf) - pos);
418 #endif /* CONFIG_IEEE80211R */
419 if (wpa_auth->conf.wpa & WPA_PROTO_WPA) {
420 res = wpa_write_wpa_ie(&wpa_auth->conf,
421 pos, buf + sizeof(buf) - pos);
427 os_free(wpa_auth->wpa_ie);
428 wpa_auth->wpa_ie = os_malloc(pos - buf);
429 if (wpa_auth->wpa_ie == NULL)
431 os_memcpy(wpa_auth->wpa_ie, buf, pos - buf);
432 wpa_auth->wpa_ie_len = pos - buf;
438 u8 * wpa_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len,
439 const u8 *data2, size_t data2_len)
441 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
442 *pos++ = RSN_SELECTOR_LEN + data_len + data2_len;
443 RSN_SELECTOR_PUT(pos, kde);
444 pos += RSN_SELECTOR_LEN;
445 os_memcpy(pos, data, data_len);
448 os_memcpy(pos, data2, data2_len);
455 struct wpa_auth_okc_iter_data {
456 struct rsn_pmksa_cache_entry *pmksa;
463 static int wpa_auth_okc_iter(struct wpa_authenticator *a, void *ctx)
465 struct wpa_auth_okc_iter_data *data = ctx;
466 data->pmksa = pmksa_cache_get_okc(a->pmksa, data->aa, data->spa,
474 int wpa_validate_wpa_ie(struct wpa_authenticator *wpa_auth,
475 struct wpa_state_machine *sm,
476 const u8 *wpa_ie, size_t wpa_ie_len,
477 const u8 *mdie, size_t mdie_len)
479 struct wpa_ie_data data;
480 int ciphers, key_mgmt, res, version;
483 const u8 *pmkid = NULL;
485 if (wpa_auth == NULL || sm == NULL)
486 return WPA_NOT_ENABLED;
488 if (wpa_ie == NULL || wpa_ie_len < 1)
489 return WPA_INVALID_IE;
491 if (wpa_ie[0] == WLAN_EID_RSN)
492 version = WPA_PROTO_RSN;
494 version = WPA_PROTO_WPA;
496 if (!(wpa_auth->conf.wpa & version)) {
497 wpa_printf(MSG_DEBUG, "Invalid WPA proto (%d) from " MACSTR,
498 version, MAC2STR(sm->addr));
499 return WPA_INVALID_PROTO;
502 if (version == WPA_PROTO_RSN) {
503 res = wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, &data);
505 selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
508 else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
509 selector = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
510 else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
511 selector = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
512 #ifdef CONFIG_IEEE80211R
513 else if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
514 selector = RSN_AUTH_KEY_MGMT_FT_802_1X;
515 else if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK)
516 selector = RSN_AUTH_KEY_MGMT_FT_PSK;
517 #endif /* CONFIG_IEEE80211R */
518 #ifdef CONFIG_IEEE80211W
519 else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
520 selector = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
521 else if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
522 selector = RSN_AUTH_KEY_MGMT_PSK_SHA256;
523 #endif /* CONFIG_IEEE80211W */
525 else if (data.key_mgmt & WPA_KEY_MGMT_SAE)
526 selector = RSN_AUTH_KEY_MGMT_SAE;
527 else if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE)
528 selector = RSN_AUTH_KEY_MGMT_FT_SAE;
529 #endif /* CONFIG_SAE */
530 else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
531 selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
532 else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
533 selector = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
534 wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
536 selector = wpa_cipher_to_suite(WPA_PROTO_RSN,
537 data.pairwise_cipher);
539 selector = RSN_CIPHER_SUITE_CCMP;
540 wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
542 selector = wpa_cipher_to_suite(WPA_PROTO_RSN,
545 selector = RSN_CIPHER_SUITE_CCMP;
546 wpa_auth->dot11RSNAGroupCipherSelected = selector;
548 res = wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, &data);
550 selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
551 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
552 selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
553 else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
554 selector = WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X;
555 wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector;
557 selector = wpa_cipher_to_suite(WPA_PROTO_WPA,
558 data.pairwise_cipher);
560 selector = RSN_CIPHER_SUITE_TKIP;
561 wpa_auth->dot11RSNAPairwiseCipherSelected = selector;
563 selector = wpa_cipher_to_suite(WPA_PROTO_WPA,
566 selector = WPA_CIPHER_SUITE_TKIP;
567 wpa_auth->dot11RSNAGroupCipherSelected = selector;
570 wpa_printf(MSG_DEBUG, "Failed to parse WPA/RSN IE from "
571 MACSTR " (res=%d)", MAC2STR(sm->addr), res);
572 wpa_hexdump(MSG_DEBUG, "WPA/RSN IE", wpa_ie, wpa_ie_len);
573 return WPA_INVALID_IE;
576 if (data.group_cipher != wpa_auth->conf.wpa_group) {
577 wpa_printf(MSG_DEBUG, "Invalid WPA group cipher (0x%x) from "
578 MACSTR, data.group_cipher, MAC2STR(sm->addr));
579 return WPA_INVALID_GROUP;
582 key_mgmt = data.key_mgmt & wpa_auth->conf.wpa_key_mgmt;
584 wpa_printf(MSG_DEBUG, "Invalid WPA key mgmt (0x%x) from "
585 MACSTR, data.key_mgmt, MAC2STR(sm->addr));
586 return WPA_INVALID_AKMP;
590 else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
591 sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
592 else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
593 sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
594 #ifdef CONFIG_IEEE80211R
595 else if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
596 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
597 else if (key_mgmt & WPA_KEY_MGMT_FT_PSK)
598 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_PSK;
599 #endif /* CONFIG_IEEE80211R */
600 #ifdef CONFIG_IEEE80211W
601 else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
602 sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
603 else if (key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
604 sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
605 #endif /* CONFIG_IEEE80211W */
607 else if (key_mgmt & WPA_KEY_MGMT_SAE)
608 sm->wpa_key_mgmt = WPA_KEY_MGMT_SAE;
609 else if (key_mgmt & WPA_KEY_MGMT_FT_SAE)
610 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_SAE;
611 #endif /* CONFIG_SAE */
612 else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X)
613 sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
615 sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
617 if (version == WPA_PROTO_RSN)
618 ciphers = data.pairwise_cipher & wpa_auth->conf.rsn_pairwise;
620 ciphers = data.pairwise_cipher & wpa_auth->conf.wpa_pairwise;
622 wpa_printf(MSG_DEBUG, "Invalid %s pairwise cipher (0x%x) "
624 version == WPA_PROTO_RSN ? "RSN" : "WPA",
625 data.pairwise_cipher, MAC2STR(sm->addr));
626 return WPA_INVALID_PAIRWISE;
629 #ifdef CONFIG_IEEE80211W
630 if (wpa_auth->conf.ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
631 if (!(data.capabilities & WPA_CAPABILITY_MFPC)) {
632 wpa_printf(MSG_DEBUG, "Management frame protection "
633 "required, but client did not enable it");
634 return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
637 if (ciphers & WPA_CIPHER_TKIP) {
638 wpa_printf(MSG_DEBUG, "Management frame protection "
640 return WPA_MGMT_FRAME_PROTECTION_VIOLATION;
643 if (data.mgmt_group_cipher != wpa_auth->conf.group_mgmt_cipher)
645 wpa_printf(MSG_DEBUG, "Unsupported management group "
646 "cipher %d", data.mgmt_group_cipher);
647 return WPA_INVALID_MGMT_GROUP_CIPHER;
651 if (wpa_auth->conf.ieee80211w == NO_MGMT_FRAME_PROTECTION ||
652 !(data.capabilities & WPA_CAPABILITY_MFPC))
653 sm->mgmt_frame_prot = 0;
655 sm->mgmt_frame_prot = 1;
656 #endif /* CONFIG_IEEE80211W */
658 #ifdef CONFIG_IEEE80211R
659 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
660 if (mdie == NULL || mdie_len < MOBILITY_DOMAIN_ID_LEN + 1) {
661 wpa_printf(MSG_DEBUG, "RSN: Trying to use FT, but "
662 "MDIE not included");
663 return WPA_INVALID_MDIE;
665 if (os_memcmp(mdie, wpa_auth->conf.mobility_domain,
666 MOBILITY_DOMAIN_ID_LEN) != 0) {
667 wpa_hexdump(MSG_DEBUG, "RSN: Attempted to use unknown "
668 "MDIE", mdie, MOBILITY_DOMAIN_ID_LEN);
669 return WPA_INVALID_MDIE;
672 #endif /* CONFIG_IEEE80211R */
674 sm->pairwise = wpa_pick_pairwise_cipher(ciphers, 0);
675 if (sm->pairwise < 0)
676 return WPA_INVALID_PAIRWISE;
678 /* TODO: clear WPA/WPA2 state if STA changes from one to another */
679 if (wpa_ie[0] == WLAN_EID_RSN)
680 sm->wpa = WPA_VERSION_WPA2;
682 sm->wpa = WPA_VERSION_WPA;
685 for (i = 0; i < data.num_pmkid; i++) {
686 wpa_hexdump(MSG_DEBUG, "RSN IE: STA PMKID",
687 &data.pmkid[i * PMKID_LEN], PMKID_LEN);
688 sm->pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sm->addr,
689 &data.pmkid[i * PMKID_LEN]);
691 pmkid = sm->pmksa->pmkid;
695 for (i = 0; sm->pmksa == NULL && wpa_auth->conf.okc &&
696 i < data.num_pmkid; i++) {
697 struct wpa_auth_okc_iter_data idata;
699 idata.aa = wpa_auth->addr;
700 idata.spa = sm->addr;
701 idata.pmkid = &data.pmkid[i * PMKID_LEN];
702 wpa_auth_for_each_auth(wpa_auth, wpa_auth_okc_iter, &idata);
704 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
705 "OKC match for PMKID");
706 sm->pmksa = pmksa_cache_add_okc(wpa_auth->pmksa,
714 if (sm->pmksa && pmkid) {
715 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
716 "PMKID found from PMKSA cache "
717 "eap_type=%d vlan_id=%d",
718 sm->pmksa->eap_type_authsrv,
720 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmkid, PMKID_LEN);
723 if (sm->wpa_ie == NULL || sm->wpa_ie_len < wpa_ie_len) {
725 sm->wpa_ie = os_malloc(wpa_ie_len);
726 if (sm->wpa_ie == NULL)
727 return WPA_ALLOC_FAIL;
729 os_memcpy(sm->wpa_ie, wpa_ie, wpa_ie_len);
730 sm->wpa_ie_len = wpa_ie_len;
737 int wpa_validate_osen(struct wpa_authenticator *wpa_auth,
738 struct wpa_state_machine *sm,
739 const u8 *osen_ie, size_t osen_ie_len)
741 if (wpa_auth == NULL || sm == NULL)
744 /* TODO: parse OSEN element */
745 sm->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
746 sm->mgmt_frame_prot = 1;
747 sm->pairwise = WPA_CIPHER_CCMP;
748 sm->wpa = WPA_VERSION_WPA2;
750 if (sm->wpa_ie == NULL || sm->wpa_ie_len < osen_ie_len) {
752 sm->wpa_ie = os_malloc(osen_ie_len);
753 if (sm->wpa_ie == NULL)
757 os_memcpy(sm->wpa_ie, osen_ie, osen_ie_len);
758 sm->wpa_ie_len = osen_ie_len;
763 #endif /* CONFIG_HS20 */
767 * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
768 * @pos: Pointer to the IE header
769 * @end: Pointer to the end of the Key Data buffer
770 * @ie: Pointer to parsed IE data
771 * Returns: 0 on success, 1 if end mark is found, -1 on failure
773 static int wpa_parse_generic(const u8 *pos, const u8 *end,
774 struct wpa_eapol_ie_parse *ie)
780 RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE &&
781 pos[2 + WPA_SELECTOR_LEN] == 1 &&
782 pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
784 ie->wpa_ie_len = pos[1] + 2;
788 if (pos[1] >= 4 && WPA_GET_BE32(pos + 2) == OSEN_IE_VENDOR_TYPE) {
790 ie->osen_len = pos[1] + 2;
794 if (pos + 1 + RSN_SELECTOR_LEN < end &&
795 pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
796 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) {
797 ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
801 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
802 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) {
803 ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
804 ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
808 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
809 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) {
810 ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN;
811 ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN;
815 #ifdef CONFIG_PEERKEY
816 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
817 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_SMK) {
818 ie->smk = pos + 2 + RSN_SELECTOR_LEN;
819 ie->smk_len = pos[1] - RSN_SELECTOR_LEN;
823 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
824 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_NONCE) {
825 ie->nonce = pos + 2 + RSN_SELECTOR_LEN;
826 ie->nonce_len = pos[1] - RSN_SELECTOR_LEN;
830 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
831 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_LIFETIME) {
832 ie->lifetime = pos + 2 + RSN_SELECTOR_LEN;
833 ie->lifetime_len = pos[1] - RSN_SELECTOR_LEN;
837 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
838 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_ERROR) {
839 ie->error = pos + 2 + RSN_SELECTOR_LEN;
840 ie->error_len = pos[1] - RSN_SELECTOR_LEN;
843 #endif /* CONFIG_PEERKEY */
845 #ifdef CONFIG_IEEE80211W
846 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
847 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) {
848 ie->igtk = pos + 2 + RSN_SELECTOR_LEN;
849 ie->igtk_len = pos[1] - RSN_SELECTOR_LEN;
852 #endif /* CONFIG_IEEE80211W */
855 if (pos[1] >= RSN_SELECTOR_LEN + 1 &&
856 RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_REQ) {
857 ie->ip_addr_req = pos + 2 + RSN_SELECTOR_LEN;
858 wpa_hexdump(MSG_DEBUG, "WPA: IP Address Request in EAPOL-Key",
859 ie->ip_addr_req, pos[1] - RSN_SELECTOR_LEN);
863 if (pos[1] >= RSN_SELECTOR_LEN + 3 * 4 &&
864 RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_ALLOC) {
865 ie->ip_addr_alloc = pos + 2 + RSN_SELECTOR_LEN;
866 wpa_hexdump(MSG_DEBUG,
867 "WPA: IP Address Allocation in EAPOL-Key",
868 ie->ip_addr_alloc, pos[1] - RSN_SELECTOR_LEN);
871 #endif /* CONFIG_P2P */
878 * wpa_parse_kde_ies - Parse EAPOL-Key Key Data IEs
879 * @buf: Pointer to the Key Data buffer
880 * @len: Key Data Length
881 * @ie: Pointer to parsed IE data
882 * Returns: 0 on success, -1 on failure
884 int wpa_parse_kde_ies(const u8 *buf, size_t len, struct wpa_eapol_ie_parse *ie)
889 os_memset(ie, 0, sizeof(*ie));
890 for (pos = buf, end = pos + len; pos + 1 < end; pos += 2 + pos[1]) {
891 if (pos[0] == 0xdd &&
892 ((pos == buf + len - 1) || pos[1] == 0)) {
896 if (pos + 2 + pos[1] > end) {
897 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data "
898 "underflow (ie=%d len=%d pos=%d)",
899 pos[0], pos[1], (int) (pos - buf));
900 wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data",
905 if (*pos == WLAN_EID_RSN) {
907 ie->rsn_ie_len = pos[1] + 2;
908 #ifdef CONFIG_IEEE80211R
909 } else if (*pos == WLAN_EID_MOBILITY_DOMAIN) {
911 ie->mdie_len = pos[1] + 2;
912 } else if (*pos == WLAN_EID_FAST_BSS_TRANSITION) {
914 ie->ftie_len = pos[1] + 2;
915 #endif /* CONFIG_IEEE80211R */
916 } else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
917 ret = wpa_parse_generic(pos, end, ie);
925 wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key "
926 "Key Data IE", pos, 2 + pos[1]);
934 int wpa_auth_uses_mfp(struct wpa_state_machine *sm)
936 return sm ? sm->mgmt_frame_prot : 0;