2 * wpa_supplicant - WPA/RSN IE and KDE processing
3 * Copyright (c) 2003-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.
13 #include "pmksa_cache.h"
14 #include "common/ieee802_11_defs.h"
20 * wpa_parse_wpa_ie - Parse WPA/RSN IE
21 * @wpa_ie: Pointer to WPA or RSN IE
22 * @wpa_ie_len: Length of the WPA/RSN IE
23 * @data: Pointer to data area for parsing results
24 * Returns: 0 on success, -1 on failure
26 * Parse the contents of WPA or RSN IE and write the parsed data into data.
28 int wpa_parse_wpa_ie(const u8 *wpa_ie, size_t wpa_ie_len,
29 struct wpa_ie_data *data)
31 if (wpa_ie_len >= 1 && wpa_ie[0] == WLAN_EID_RSN)
32 return wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, data);
34 return wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, data);
38 static int wpa_gen_wpa_ie_wpa(u8 *wpa_ie, size_t wpa_ie_len,
39 int pairwise_cipher, int group_cipher,
43 struct wpa_ie_hdr *hdr;
46 if (wpa_ie_len < sizeof(*hdr) + WPA_SELECTOR_LEN +
47 2 + WPA_SELECTOR_LEN + 2 + WPA_SELECTOR_LEN)
50 hdr = (struct wpa_ie_hdr *) wpa_ie;
51 hdr->elem_id = WLAN_EID_VENDOR_SPECIFIC;
52 RSN_SELECTOR_PUT(hdr->oui, WPA_OUI_TYPE);
53 WPA_PUT_LE16(hdr->version, WPA_VERSION);
54 pos = (u8 *) (hdr + 1);
56 suite = wpa_cipher_to_suite(WPA_PROTO_WPA, group_cipher);
58 wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
62 RSN_SELECTOR_PUT(pos, suite);
63 pos += WPA_SELECTOR_LEN;
67 suite = wpa_cipher_to_suite(WPA_PROTO_WPA, pairwise_cipher);
69 (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
70 pairwise_cipher != WPA_CIPHER_NONE)) {
71 wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
75 RSN_SELECTOR_PUT(pos, suite);
76 pos += WPA_SELECTOR_LEN;
80 if (key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
81 RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
82 } else if (key_mgmt == WPA_KEY_MGMT_PSK) {
83 RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
84 } else if (key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
85 RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_NONE);
86 } else if (key_mgmt == WPA_KEY_MGMT_CCKM) {
87 RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_CCKM);
89 wpa_printf(MSG_WARNING, "Invalid key management type (%d).",
93 pos += WPA_SELECTOR_LEN;
95 /* WPA Capabilities; use defaults, so no need to include it */
97 hdr->len = (pos - wpa_ie) - 2;
99 WPA_ASSERT((size_t) (pos - wpa_ie) <= wpa_ie_len);
105 static int wpa_gen_wpa_ie_rsn(u8 *rsn_ie, size_t rsn_ie_len,
106 int pairwise_cipher, int group_cipher,
107 int key_mgmt, int mgmt_group_cipher,
111 struct rsn_ie_hdr *hdr;
115 if (rsn_ie_len < sizeof(*hdr) + RSN_SELECTOR_LEN +
116 2 + RSN_SELECTOR_LEN + 2 + RSN_SELECTOR_LEN + 2 +
117 (sm->cur_pmksa ? 2 + PMKID_LEN : 0)) {
118 wpa_printf(MSG_DEBUG, "RSN: Too short IE buffer (%lu bytes)",
119 (unsigned long) rsn_ie_len);
123 hdr = (struct rsn_ie_hdr *) rsn_ie;
124 hdr->elem_id = WLAN_EID_RSN;
125 WPA_PUT_LE16(hdr->version, RSN_VERSION);
126 pos = (u8 *) (hdr + 1);
128 suite = wpa_cipher_to_suite(WPA_PROTO_RSN, group_cipher);
130 wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
134 RSN_SELECTOR_PUT(pos, suite);
135 pos += RSN_SELECTOR_LEN;
139 suite = wpa_cipher_to_suite(WPA_PROTO_RSN, pairwise_cipher);
141 (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
142 pairwise_cipher != WPA_CIPHER_NONE)) {
143 wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
147 RSN_SELECTOR_PUT(pos, suite);
148 pos += RSN_SELECTOR_LEN;
152 if (key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
153 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X);
154 } else if (key_mgmt == WPA_KEY_MGMT_PSK) {
155 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X);
156 } else if (key_mgmt == WPA_KEY_MGMT_CCKM) {
157 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_CCKM);
158 #ifdef CONFIG_IEEE80211R
159 } else if (key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
160 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
161 } else if (key_mgmt == WPA_KEY_MGMT_FT_PSK) {
162 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
163 #endif /* CONFIG_IEEE80211R */
164 #ifdef CONFIG_IEEE80211W
165 } else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SHA256) {
166 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SHA256);
167 } else if (key_mgmt == WPA_KEY_MGMT_PSK_SHA256) {
168 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_SHA256);
169 #endif /* CONFIG_IEEE80211W */
171 } else if (key_mgmt == WPA_KEY_MGMT_SAE) {
172 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE);
173 } else if (key_mgmt == WPA_KEY_MGMT_FT_SAE) {
174 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
175 #endif /* CONFIG_SAE */
176 } else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
177 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192);
178 } else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
179 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B);
181 wpa_printf(MSG_WARNING, "Invalid key management type (%d).",
185 pos += RSN_SELECTOR_LEN;
187 /* RSN Capabilities */
189 #ifdef CONFIG_IEEE80211W
191 capab |= WPA_CAPABILITY_MFPC;
193 capab |= WPA_CAPABILITY_MFPR;
194 #endif /* CONFIG_IEEE80211W */
195 WPA_PUT_LE16(pos, capab);
199 /* PMKID Count (2 octets, little endian) */
203 os_memcpy(pos, sm->cur_pmksa->pmkid, PMKID_LEN);
207 #ifdef CONFIG_IEEE80211W
208 if (wpa_cipher_valid_mgmt_group(mgmt_group_cipher)) {
209 if (!sm->cur_pmksa) {
211 WPA_PUT_LE16(pos, 0);
215 /* Management Group Cipher Suite */
216 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
218 pos += RSN_SELECTOR_LEN;
220 #endif /* CONFIG_IEEE80211W */
222 hdr->len = (pos - rsn_ie) - 2;
224 WPA_ASSERT((size_t) (pos - rsn_ie) <= rsn_ie_len);
231 static int wpa_gen_wpa_ie_osen(u8 *wpa_ie, size_t wpa_ie_len,
232 int pairwise_cipher, int group_cipher,
238 if (wpa_ie_len < 2 + 4 + RSN_SELECTOR_LEN +
239 2 + RSN_SELECTOR_LEN + 2 + RSN_SELECTOR_LEN)
243 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
244 len = pos++; /* to be filled */
245 WPA_PUT_BE24(pos, OUI_WFA);
247 *pos++ = HS20_OSEN_OUI_TYPE;
249 /* Group Data Cipher Suite */
250 suite = wpa_cipher_to_suite(WPA_PROTO_RSN, group_cipher);
252 wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
256 RSN_SELECTOR_PUT(pos, suite);
257 pos += RSN_SELECTOR_LEN;
259 /* Pairwise Cipher Suite Count and List */
260 WPA_PUT_LE16(pos, 1);
262 suite = wpa_cipher_to_suite(WPA_PROTO_RSN, pairwise_cipher);
264 (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
265 pairwise_cipher != WPA_CIPHER_NONE)) {
266 wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
270 RSN_SELECTOR_PUT(pos, suite);
271 pos += RSN_SELECTOR_LEN;
273 /* AKM Suite Count and List */
274 WPA_PUT_LE16(pos, 1);
276 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_OSEN);
277 pos += RSN_SELECTOR_LEN;
279 *len = pos - len - 1;
281 WPA_ASSERT((size_t) (pos - wpa_ie) <= wpa_ie_len);
285 #endif /* CONFIG_HS20 */
289 * wpa_gen_wpa_ie - Generate WPA/RSN IE based on current security policy
290 * @sm: Pointer to WPA state machine data from wpa_sm_init()
291 * @wpa_ie: Pointer to memory area for the generated WPA/RSN IE
292 * @wpa_ie_len: Maximum length of the generated WPA/RSN IE
293 * Returns: Length of the generated WPA/RSN IE or -1 on failure
295 int wpa_gen_wpa_ie(struct wpa_sm *sm, u8 *wpa_ie, size_t wpa_ie_len)
297 if (sm->proto == WPA_PROTO_RSN)
298 return wpa_gen_wpa_ie_rsn(wpa_ie, wpa_ie_len,
301 sm->key_mgmt, sm->mgmt_group_cipher,
304 else if (sm->proto == WPA_PROTO_OSEN)
305 return wpa_gen_wpa_ie_osen(wpa_ie, wpa_ie_len,
309 #endif /* CONFIG_HS20 */
311 return wpa_gen_wpa_ie_wpa(wpa_ie, wpa_ie_len,
319 * wpa_parse_vendor_specific - Parse Vendor Specific IEs
320 * @pos: Pointer to the IE header
321 * @end: Pointer to the end of the Key Data buffer
322 * @ie: Pointer to parsed IE data
323 * Returns: 0 on success, 1 if end mark is found, -1 on failure
325 static int wpa_parse_vendor_specific(const u8 *pos, const u8 *end,
326 struct wpa_eapol_ie_parse *ie)
331 wpa_printf(MSG_MSGDUMP, "Too short vendor specific IE ignored (len=%u)",
336 oui = WPA_GET_BE24(&pos[2]);
337 if (oui == OUI_MICROSOFT && pos[5] == WMM_OUI_TYPE && pos[1] > 4) {
338 if (pos[6] == WMM_OUI_SUBTYPE_INFORMATION_ELEMENT) {
340 ie->wmm_len = pos[1];
341 wpa_hexdump(MSG_DEBUG, "WPA: WMM IE",
342 ie->wmm, ie->wmm_len);
343 } else if (pos[6] == WMM_OUI_SUBTYPE_PARAMETER_ELEMENT) {
345 ie->wmm_len = pos[1];
346 wpa_hexdump(MSG_DEBUG, "WPA: WMM Parameter Element",
347 ie->wmm, ie->wmm_len);
355 * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
356 * @pos: Pointer to the IE header
357 * @end: Pointer to the end of the Key Data buffer
358 * @ie: Pointer to parsed IE data
359 * Returns: 0 on success, 1 if end mark is found, -1 on failure
361 static int wpa_parse_generic(const u8 *pos, const u8 *end,
362 struct wpa_eapol_ie_parse *ie)
368 RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE &&
369 pos[2 + WPA_SELECTOR_LEN] == 1 &&
370 pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
372 ie->wpa_ie_len = pos[1] + 2;
373 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE in EAPOL-Key",
374 ie->wpa_ie, ie->wpa_ie_len);
378 if (pos + 1 + RSN_SELECTOR_LEN < end &&
379 pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
380 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) {
381 ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
382 wpa_hexdump(MSG_DEBUG, "WPA: PMKID in EAPOL-Key",
387 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
388 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) {
389 ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
390 ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
391 wpa_hexdump_key(MSG_DEBUG, "WPA: GTK in EAPOL-Key",
396 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
397 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) {
398 ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN;
399 ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN;
400 wpa_hexdump(MSG_DEBUG, "WPA: MAC Address in EAPOL-Key",
405 #ifdef CONFIG_PEERKEY
406 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
407 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_SMK) {
408 ie->smk = pos + 2 + RSN_SELECTOR_LEN;
409 ie->smk_len = pos[1] - RSN_SELECTOR_LEN;
410 wpa_hexdump_key(MSG_DEBUG, "WPA: SMK in EAPOL-Key",
415 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
416 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_NONCE) {
417 ie->nonce = pos + 2 + RSN_SELECTOR_LEN;
418 ie->nonce_len = pos[1] - RSN_SELECTOR_LEN;
419 wpa_hexdump(MSG_DEBUG, "WPA: Nonce in EAPOL-Key",
424 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
425 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_LIFETIME) {
426 ie->lifetime = pos + 2 + RSN_SELECTOR_LEN;
427 ie->lifetime_len = pos[1] - RSN_SELECTOR_LEN;
428 wpa_hexdump(MSG_DEBUG, "WPA: Lifetime in EAPOL-Key",
433 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
434 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_ERROR) {
435 ie->error = pos + 2 + RSN_SELECTOR_LEN;
436 ie->error_len = pos[1] - RSN_SELECTOR_LEN;
437 wpa_hexdump(MSG_DEBUG, "WPA: Error in EAPOL-Key",
441 #endif /* CONFIG_PEERKEY */
443 #ifdef CONFIG_IEEE80211W
444 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
445 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) {
446 ie->igtk = pos + 2 + RSN_SELECTOR_LEN;
447 ie->igtk_len = pos[1] - RSN_SELECTOR_LEN;
448 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK in EAPOL-Key",
452 #endif /* CONFIG_IEEE80211W */
455 if (pos[1] >= RSN_SELECTOR_LEN + 1 &&
456 RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_REQ) {
457 ie->ip_addr_req = pos + 2 + RSN_SELECTOR_LEN;
458 wpa_hexdump(MSG_DEBUG, "WPA: IP Address Request in EAPOL-Key",
459 ie->ip_addr_req, pos[1] - RSN_SELECTOR_LEN);
463 if (pos[1] >= RSN_SELECTOR_LEN + 3 * 4 &&
464 RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_ALLOC) {
465 ie->ip_addr_alloc = pos + 2 + RSN_SELECTOR_LEN;
466 wpa_hexdump(MSG_DEBUG,
467 "WPA: IP Address Allocation in EAPOL-Key",
468 ie->ip_addr_alloc, pos[1] - RSN_SELECTOR_LEN);
471 #endif /* CONFIG_P2P */
478 * wpa_supplicant_parse_ies - Parse EAPOL-Key Key Data IEs
479 * @buf: Pointer to the Key Data buffer
480 * @len: Key Data Length
481 * @ie: Pointer to parsed IE data
482 * Returns: 0 on success, -1 on failure
484 int wpa_supplicant_parse_ies(const u8 *buf, size_t len,
485 struct wpa_eapol_ie_parse *ie)
490 os_memset(ie, 0, sizeof(*ie));
491 for (pos = buf, end = pos + len; pos + 1 < end; pos += 2 + pos[1]) {
492 if (pos[0] == 0xdd &&
493 ((pos == buf + len - 1) || pos[1] == 0)) {
497 if (pos + 2 + pos[1] > end) {
498 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data "
499 "underflow (ie=%d len=%d pos=%d)",
500 pos[0], pos[1], (int) (pos - buf));
501 wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data",
506 if (*pos == WLAN_EID_RSN) {
508 ie->rsn_ie_len = pos[1] + 2;
509 wpa_hexdump(MSG_DEBUG, "WPA: RSN IE in EAPOL-Key",
510 ie->rsn_ie, ie->rsn_ie_len);
511 } else if (*pos == WLAN_EID_MOBILITY_DOMAIN) {
513 ie->mdie_len = pos[1] + 2;
514 wpa_hexdump(MSG_DEBUG, "WPA: MDIE in EAPOL-Key",
515 ie->mdie, ie->mdie_len);
516 } else if (*pos == WLAN_EID_FAST_BSS_TRANSITION) {
518 ie->ftie_len = pos[1] + 2;
519 wpa_hexdump(MSG_DEBUG, "WPA: FTIE in EAPOL-Key",
520 ie->ftie, ie->ftie_len);
521 } else if (*pos == WLAN_EID_TIMEOUT_INTERVAL && pos[1] >= 5) {
522 if (pos[2] == WLAN_TIMEOUT_REASSOC_DEADLINE) {
523 ie->reassoc_deadline = pos;
524 wpa_hexdump(MSG_DEBUG, "WPA: Reassoc Deadline "
526 ie->reassoc_deadline, pos[1] + 2);
527 } else if (pos[2] == WLAN_TIMEOUT_KEY_LIFETIME) {
528 ie->key_lifetime = pos;
529 wpa_hexdump(MSG_DEBUG, "WPA: KeyLifetime "
531 ie->key_lifetime, pos[1] + 2);
533 wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized "
534 "EAPOL-Key Key Data IE",
537 } else if (*pos == WLAN_EID_LINK_ID) {
540 ie->lnkid_len = pos[1] + 2;
542 } else if (*pos == WLAN_EID_EXT_CAPAB) {
544 ie->ext_capab_len = pos[1] + 2;
545 } else if (*pos == WLAN_EID_SUPP_RATES) {
546 ie->supp_rates = pos;
547 ie->supp_rates_len = pos[1] + 2;
548 } else if (*pos == WLAN_EID_EXT_SUPP_RATES) {
549 ie->ext_supp_rates = pos;
550 ie->ext_supp_rates_len = pos[1] + 2;
551 } else if (*pos == WLAN_EID_HT_CAP) {
552 ie->ht_capabilities = pos + 2;
553 ie->ht_capabilities_len = pos[1];
554 } else if (*pos == WLAN_EID_VHT_AID) {
556 ie->aid = WPA_GET_LE16(pos + 2) & 0x3fff;
557 } else if (*pos == WLAN_EID_VHT_CAP) {
558 ie->vht_capabilities = pos + 2;
559 ie->vht_capabilities_len = pos[1];
560 } else if (*pos == WLAN_EID_QOS && pos[1] >= 1) {
561 ie->qosinfo = pos[2];
562 } else if (*pos == WLAN_EID_SUPPORTED_CHANNELS) {
563 ie->supp_channels = pos + 2;
564 ie->supp_channels_len = pos[1];
565 } else if (*pos == WLAN_EID_SUPPORTED_OPERATING_CLASSES) {
567 * The value of the Length field of the Supported
568 * Operating Classes element is between 2 and 253.
569 * Silently skip invalid elements to avoid interop
570 * issues when trying to use the value.
572 if (pos[1] >= 2 && pos[1] <= 253) {
573 ie->supp_oper_classes = pos + 2;
574 ie->supp_oper_classes_len = pos[1];
576 } else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
577 ret = wpa_parse_generic(pos, end, ie);
585 ret = wpa_parse_vendor_specific(pos, end, ie);
593 wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key "
594 "Key Data IE", pos, 2 + pos[1]);