2 * wpa_supplicant - WPA/RSN IE and KDE processing
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "pmksa_cache.h"
20 #include "common/ieee802_11_defs.h"
25 static int wpa_selector_to_bitfield(const u8
*s
)
27 if (RSN_SELECTOR_GET(s
) == WPA_CIPHER_SUITE_NONE
)
28 return WPA_CIPHER_NONE
;
29 if (RSN_SELECTOR_GET(s
) == WPA_CIPHER_SUITE_WEP40
)
30 return WPA_CIPHER_WEP40
;
31 if (RSN_SELECTOR_GET(s
) == WPA_CIPHER_SUITE_TKIP
)
32 return WPA_CIPHER_TKIP
;
33 if (RSN_SELECTOR_GET(s
) == WPA_CIPHER_SUITE_CCMP
)
34 return WPA_CIPHER_CCMP
;
35 if (RSN_SELECTOR_GET(s
) == WPA_CIPHER_SUITE_WEP104
)
36 return WPA_CIPHER_WEP104
;
41 static int wpa_key_mgmt_to_bitfield(const u8
*s
)
43 if (RSN_SELECTOR_GET(s
) == WPA_AUTH_KEY_MGMT_UNSPEC_802_1X
)
44 return WPA_KEY_MGMT_IEEE8021X
;
45 if (RSN_SELECTOR_GET(s
) == WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X
)
46 return WPA_KEY_MGMT_PSK
;
47 if (RSN_SELECTOR_GET(s
) == WPA_AUTH_KEY_MGMT_NONE
)
48 return WPA_KEY_MGMT_WPA_NONE
;
53 static int wpa_parse_wpa_ie_wpa(const u8
*wpa_ie
, size_t wpa_ie_len
,
54 struct wpa_ie_data
*data
)
56 const struct wpa_ie_hdr
*hdr
;
61 os_memset(data
, 0, sizeof(*data
));
62 data
->proto
= WPA_PROTO_WPA
;
63 data
->pairwise_cipher
= WPA_CIPHER_TKIP
;
64 data
->group_cipher
= WPA_CIPHER_TKIP
;
65 data
->key_mgmt
= WPA_KEY_MGMT_IEEE8021X
;
66 data
->capabilities
= 0;
69 data
->mgmt_group_cipher
= 0;
71 if (wpa_ie_len
== 0) {
72 /* No WPA IE - fail silently */
76 if (wpa_ie_len
< sizeof(struct wpa_ie_hdr
)) {
77 wpa_printf(MSG_DEBUG
, "%s: ie len too short %lu",
78 __func__
, (unsigned long) wpa_ie_len
);
82 hdr
= (const struct wpa_ie_hdr
*) wpa_ie
;
84 if (hdr
->elem_id
!= WLAN_EID_VENDOR_SPECIFIC
||
85 hdr
->len
!= wpa_ie_len
- 2 ||
86 RSN_SELECTOR_GET(hdr
->oui
) != WPA_OUI_TYPE
||
87 WPA_GET_LE16(hdr
->version
) != WPA_VERSION
) {
88 wpa_printf(MSG_DEBUG
, "%s: malformed ie or unknown version",
93 pos
= (const u8
*) (hdr
+ 1);
94 left
= wpa_ie_len
- sizeof(*hdr
);
96 if (left
>= WPA_SELECTOR_LEN
) {
97 data
->group_cipher
= wpa_selector_to_bitfield(pos
);
98 pos
+= WPA_SELECTOR_LEN
;
99 left
-= WPA_SELECTOR_LEN
;
100 } else if (left
> 0) {
101 wpa_printf(MSG_DEBUG
, "%s: ie length mismatch, %u too much",
107 data
->pairwise_cipher
= 0;
108 count
= WPA_GET_LE16(pos
);
111 if (count
== 0 || left
< count
* WPA_SELECTOR_LEN
) {
112 wpa_printf(MSG_DEBUG
, "%s: ie count botch (pairwise), "
113 "count %u left %u", __func__
, count
, left
);
116 for (i
= 0; i
< count
; i
++) {
117 data
->pairwise_cipher
|= wpa_selector_to_bitfield(pos
);
118 pos
+= WPA_SELECTOR_LEN
;
119 left
-= WPA_SELECTOR_LEN
;
121 } else if (left
== 1) {
122 wpa_printf(MSG_DEBUG
, "%s: ie too short (for key mgmt)",
129 count
= WPA_GET_LE16(pos
);
132 if (count
== 0 || left
< count
* WPA_SELECTOR_LEN
) {
133 wpa_printf(MSG_DEBUG
, "%s: ie count botch (key mgmt), "
134 "count %u left %u", __func__
, count
, left
);
137 for (i
= 0; i
< count
; i
++) {
138 data
->key_mgmt
|= wpa_key_mgmt_to_bitfield(pos
);
139 pos
+= WPA_SELECTOR_LEN
;
140 left
-= WPA_SELECTOR_LEN
;
142 } else if (left
== 1) {
143 wpa_printf(MSG_DEBUG
, "%s: ie too short (for capabilities)",
149 data
->capabilities
= WPA_GET_LE16(pos
);
155 wpa_printf(MSG_DEBUG
, "%s: ie has %u trailing bytes - ignored",
164 * wpa_parse_wpa_ie - Parse WPA/RSN IE
165 * @wpa_ie: Pointer to WPA or RSN IE
166 * @wpa_ie_len: Length of the WPA/RSN IE
167 * @data: Pointer to data area for parsing results
168 * Returns: 0 on success, -1 on failure
170 * Parse the contents of WPA or RSN IE and write the parsed data into data.
172 int wpa_parse_wpa_ie(const u8
*wpa_ie
, size_t wpa_ie_len
,
173 struct wpa_ie_data
*data
)
175 if (wpa_ie_len
>= 1 && wpa_ie
[0] == WLAN_EID_RSN
)
176 return wpa_parse_wpa_ie_rsn(wpa_ie
, wpa_ie_len
, data
);
178 return wpa_parse_wpa_ie_wpa(wpa_ie
, wpa_ie_len
, data
);
182 static int wpa_gen_wpa_ie_wpa(u8
*wpa_ie
, size_t wpa_ie_len
,
183 int pairwise_cipher
, int group_cipher
,
187 struct wpa_ie_hdr
*hdr
;
189 if (wpa_ie_len
< sizeof(*hdr
) + WPA_SELECTOR_LEN
+
190 2 + WPA_SELECTOR_LEN
+ 2 + WPA_SELECTOR_LEN
)
193 hdr
= (struct wpa_ie_hdr
*) wpa_ie
;
194 hdr
->elem_id
= WLAN_EID_VENDOR_SPECIFIC
;
195 RSN_SELECTOR_PUT(hdr
->oui
, WPA_OUI_TYPE
);
196 WPA_PUT_LE16(hdr
->version
, WPA_VERSION
);
197 pos
= (u8
*) (hdr
+ 1);
199 if (group_cipher
== WPA_CIPHER_CCMP
) {
200 RSN_SELECTOR_PUT(pos
, WPA_CIPHER_SUITE_CCMP
);
201 } else if (group_cipher
== WPA_CIPHER_TKIP
) {
202 RSN_SELECTOR_PUT(pos
, WPA_CIPHER_SUITE_TKIP
);
203 } else if (group_cipher
== WPA_CIPHER_WEP104
) {
204 RSN_SELECTOR_PUT(pos
, WPA_CIPHER_SUITE_WEP104
);
205 } else if (group_cipher
== WPA_CIPHER_WEP40
) {
206 RSN_SELECTOR_PUT(pos
, WPA_CIPHER_SUITE_WEP40
);
208 wpa_printf(MSG_WARNING
, "Invalid group cipher (%d).",
212 pos
+= WPA_SELECTOR_LEN
;
216 if (pairwise_cipher
== WPA_CIPHER_CCMP
) {
217 RSN_SELECTOR_PUT(pos
, WPA_CIPHER_SUITE_CCMP
);
218 } else if (pairwise_cipher
== WPA_CIPHER_TKIP
) {
219 RSN_SELECTOR_PUT(pos
, WPA_CIPHER_SUITE_TKIP
);
220 } else if (pairwise_cipher
== WPA_CIPHER_NONE
) {
221 RSN_SELECTOR_PUT(pos
, WPA_CIPHER_SUITE_NONE
);
223 wpa_printf(MSG_WARNING
, "Invalid pairwise cipher (%d).",
227 pos
+= WPA_SELECTOR_LEN
;
231 if (key_mgmt
== WPA_KEY_MGMT_IEEE8021X
) {
232 RSN_SELECTOR_PUT(pos
, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X
);
233 } else if (key_mgmt
== WPA_KEY_MGMT_PSK
) {
234 RSN_SELECTOR_PUT(pos
, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X
);
235 } else if (key_mgmt
== WPA_KEY_MGMT_WPA_NONE
) {
236 RSN_SELECTOR_PUT(pos
, WPA_AUTH_KEY_MGMT_NONE
);
238 wpa_printf(MSG_WARNING
, "Invalid key management type (%d).",
242 pos
+= WPA_SELECTOR_LEN
;
244 /* WPA Capabilities; use defaults, so no need to include it */
246 hdr
->len
= (pos
- wpa_ie
) - 2;
248 WPA_ASSERT((size_t) (pos
- wpa_ie
) <= wpa_ie_len
);
254 static int wpa_gen_wpa_ie_rsn(u8
*rsn_ie
, size_t rsn_ie_len
,
255 int pairwise_cipher
, int group_cipher
,
256 int key_mgmt
, int mgmt_group_cipher
,
259 #ifndef CONFIG_NO_WPA2
261 struct rsn_ie_hdr
*hdr
;
264 if (rsn_ie_len
< sizeof(*hdr
) + RSN_SELECTOR_LEN
+
265 2 + RSN_SELECTOR_LEN
+ 2 + RSN_SELECTOR_LEN
+ 2 +
266 (sm
->cur_pmksa
? 2 + PMKID_LEN
: 0)) {
267 wpa_printf(MSG_DEBUG
, "RSN: Too short IE buffer (%lu bytes)",
268 (unsigned long) rsn_ie_len
);
272 hdr
= (struct rsn_ie_hdr
*) rsn_ie
;
273 hdr
->elem_id
= WLAN_EID_RSN
;
274 WPA_PUT_LE16(hdr
->version
, RSN_VERSION
);
275 pos
= (u8
*) (hdr
+ 1);
277 if (group_cipher
== WPA_CIPHER_CCMP
) {
278 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_CCMP
);
279 } else if (group_cipher
== WPA_CIPHER_TKIP
) {
280 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_TKIP
);
281 } else if (group_cipher
== WPA_CIPHER_WEP104
) {
282 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_WEP104
);
283 } else if (group_cipher
== WPA_CIPHER_WEP40
) {
284 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_WEP40
);
286 wpa_printf(MSG_WARNING
, "Invalid group cipher (%d).",
290 pos
+= RSN_SELECTOR_LEN
;
294 if (pairwise_cipher
== WPA_CIPHER_CCMP
) {
295 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_CCMP
);
296 } else if (pairwise_cipher
== WPA_CIPHER_TKIP
) {
297 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_TKIP
);
298 } else if (pairwise_cipher
== WPA_CIPHER_NONE
) {
299 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_NONE
);
301 wpa_printf(MSG_WARNING
, "Invalid pairwise cipher (%d).",
305 pos
+= RSN_SELECTOR_LEN
;
309 if (key_mgmt
== WPA_KEY_MGMT_IEEE8021X
) {
310 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X
);
311 } else if (key_mgmt
== WPA_KEY_MGMT_PSK
) {
312 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X
);
313 #ifdef CONFIG_IEEE80211R
314 } else if (key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
) {
315 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_FT_802_1X
);
316 } else if (key_mgmt
== WPA_KEY_MGMT_FT_PSK
) {
317 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_FT_PSK
);
318 #endif /* CONFIG_IEEE80211R */
319 #ifdef CONFIG_IEEE80211W
320 } else if (key_mgmt
== WPA_KEY_MGMT_IEEE8021X_SHA256
) {
321 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_802_1X_SHA256
);
322 } else if (key_mgmt
== WPA_KEY_MGMT_PSK_SHA256
) {
323 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_PSK_SHA256
);
324 #endif /* CONFIG_IEEE80211W */
326 wpa_printf(MSG_WARNING
, "Invalid key management type (%d).",
330 pos
+= RSN_SELECTOR_LEN
;
332 /* RSN Capabilities */
334 #ifdef CONFIG_IEEE80211W
336 capab
|= WPA_CAPABILITY_MFPC
;
338 capab
|= WPA_CAPABILITY_MFPR
;
339 #endif /* CONFIG_IEEE80211W */
340 WPA_PUT_LE16(pos
, capab
);
344 /* PMKID Count (2 octets, little endian) */
348 os_memcpy(pos
, sm
->cur_pmksa
->pmkid
, PMKID_LEN
);
352 #ifdef CONFIG_IEEE80211W
353 if (mgmt_group_cipher
== WPA_CIPHER_AES_128_CMAC
) {
354 if (!sm
->cur_pmksa
) {
356 WPA_PUT_LE16(pos
, 0);
360 /* Management Group Cipher Suite */
361 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_AES_128_CMAC
);
362 pos
+= RSN_SELECTOR_LEN
;
364 #endif /* CONFIG_IEEE80211W */
366 hdr
->len
= (pos
- rsn_ie
) - 2;
368 WPA_ASSERT((size_t) (pos
- rsn_ie
) <= rsn_ie_len
);
371 #else /* CONFIG_NO_WPA2 */
373 #endif /* CONFIG_NO_WPA2 */
378 * wpa_gen_wpa_ie - Generate WPA/RSN IE based on current security policy
379 * @sm: Pointer to WPA state machine data from wpa_sm_init()
380 * @wpa_ie: Pointer to memory area for the generated WPA/RSN IE
381 * @wpa_ie_len: Maximum length of the generated WPA/RSN IE
382 * Returns: Length of the generated WPA/RSN IE or -1 on failure
384 int wpa_gen_wpa_ie(struct wpa_sm
*sm
, u8
*wpa_ie
, size_t wpa_ie_len
)
386 if (sm
->proto
== WPA_PROTO_RSN
)
387 return wpa_gen_wpa_ie_rsn(wpa_ie
, wpa_ie_len
,
390 sm
->key_mgmt
, sm
->mgmt_group_cipher
,
393 return wpa_gen_wpa_ie_wpa(wpa_ie
, wpa_ie_len
,
401 * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
402 * @pos: Pointer to the IE header
403 * @end: Pointer to the end of the Key Data buffer
404 * @ie: Pointer to parsed IE data
405 * Returns: 0 on success, 1 if end mark is found, -1 on failure
407 static int wpa_parse_generic(const u8
*pos
, const u8
*end
,
408 struct wpa_eapol_ie_parse
*ie
)
414 RSN_SELECTOR_GET(pos
+ 2) == WPA_OUI_TYPE
&&
415 pos
[2 + WPA_SELECTOR_LEN
] == 1 &&
416 pos
[2 + WPA_SELECTOR_LEN
+ 1] == 0) {
418 ie
->wpa_ie_len
= pos
[1] + 2;
419 wpa_hexdump(MSG_DEBUG
, "WPA: WPA IE in EAPOL-Key",
420 ie
->wpa_ie
, ie
->wpa_ie_len
);
424 if (pos
+ 1 + RSN_SELECTOR_LEN
< end
&&
425 pos
[1] >= RSN_SELECTOR_LEN
+ PMKID_LEN
&&
426 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_PMKID
) {
427 ie
->pmkid
= pos
+ 2 + RSN_SELECTOR_LEN
;
428 wpa_hexdump(MSG_DEBUG
, "WPA: PMKID in EAPOL-Key",
433 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
434 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_GROUPKEY
) {
435 ie
->gtk
= pos
+ 2 + RSN_SELECTOR_LEN
;
436 ie
->gtk_len
= pos
[1] - RSN_SELECTOR_LEN
;
437 wpa_hexdump_key(MSG_DEBUG
, "WPA: GTK in EAPOL-Key",
442 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
443 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_MAC_ADDR
) {
444 ie
->mac_addr
= pos
+ 2 + RSN_SELECTOR_LEN
;
445 ie
->mac_addr_len
= pos
[1] - RSN_SELECTOR_LEN
;
446 wpa_hexdump(MSG_DEBUG
, "WPA: MAC Address in EAPOL-Key",
451 #ifdef CONFIG_PEERKEY
452 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
453 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_SMK
) {
454 ie
->smk
= pos
+ 2 + RSN_SELECTOR_LEN
;
455 ie
->smk_len
= pos
[1] - RSN_SELECTOR_LEN
;
456 wpa_hexdump_key(MSG_DEBUG
, "WPA: SMK in EAPOL-Key",
461 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
462 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_NONCE
) {
463 ie
->nonce
= pos
+ 2 + RSN_SELECTOR_LEN
;
464 ie
->nonce_len
= pos
[1] - RSN_SELECTOR_LEN
;
465 wpa_hexdump(MSG_DEBUG
, "WPA: Nonce in EAPOL-Key",
470 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
471 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_LIFETIME
) {
472 ie
->lifetime
= pos
+ 2 + RSN_SELECTOR_LEN
;
473 ie
->lifetime_len
= pos
[1] - RSN_SELECTOR_LEN
;
474 wpa_hexdump(MSG_DEBUG
, "WPA: Lifetime in EAPOL-Key",
479 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
480 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_ERROR
) {
481 ie
->error
= pos
+ 2 + RSN_SELECTOR_LEN
;
482 ie
->error_len
= pos
[1] - RSN_SELECTOR_LEN
;
483 wpa_hexdump(MSG_DEBUG
, "WPA: Error in EAPOL-Key",
487 #endif /* CONFIG_PEERKEY */
489 #ifdef CONFIG_IEEE80211W
490 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
491 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_IGTK
) {
492 ie
->igtk
= pos
+ 2 + RSN_SELECTOR_LEN
;
493 ie
->igtk_len
= pos
[1] - RSN_SELECTOR_LEN
;
494 wpa_hexdump_key(MSG_DEBUG
, "WPA: IGTK in EAPOL-Key",
498 #endif /* CONFIG_IEEE80211W */
505 * wpa_supplicant_parse_ies - Parse EAPOL-Key Key Data IEs
506 * @buf: Pointer to the Key Data buffer
507 * @len: Key Data Length
508 * @ie: Pointer to parsed IE data
509 * Returns: 0 on success, -1 on failure
511 int wpa_supplicant_parse_ies(const u8
*buf
, size_t len
,
512 struct wpa_eapol_ie_parse
*ie
)
517 os_memset(ie
, 0, sizeof(*ie
));
518 for (pos
= buf
, end
= pos
+ len
; pos
+ 1 < end
; pos
+= 2 + pos
[1]) {
519 if (pos
[0] == 0xdd &&
520 ((pos
== buf
+ len
- 1) || pos
[1] == 0)) {
524 if (pos
+ 2 + pos
[1] > end
) {
525 wpa_printf(MSG_DEBUG
, "WPA: EAPOL-Key Key Data "
526 "underflow (ie=%d len=%d pos=%d)",
527 pos
[0], pos
[1], (int) (pos
- buf
));
528 wpa_hexdump_key(MSG_DEBUG
, "WPA: Key Data",
533 if (*pos
== WLAN_EID_RSN
) {
535 ie
->rsn_ie_len
= pos
[1] + 2;
536 wpa_hexdump(MSG_DEBUG
, "WPA: RSN IE in EAPOL-Key",
537 ie
->rsn_ie
, ie
->rsn_ie_len
);
538 #ifdef CONFIG_IEEE80211R
539 } else if (*pos
== WLAN_EID_MOBILITY_DOMAIN
) {
541 ie
->mdie_len
= pos
[1] + 2;
542 wpa_hexdump(MSG_DEBUG
, "WPA: MDIE in EAPOL-Key",
543 ie
->mdie
, ie
->mdie_len
);
544 } else if (*pos
== WLAN_EID_FAST_BSS_TRANSITION
) {
546 ie
->ftie_len
= pos
[1] + 2;
547 wpa_hexdump(MSG_DEBUG
, "WPA: FTIE in EAPOL-Key",
548 ie
->ftie
, ie
->ftie_len
);
549 } else if (*pos
== WLAN_EID_TIMEOUT_INTERVAL
&& pos
[1] >= 5) {
550 if (pos
[2] == WLAN_TIMEOUT_REASSOC_DEADLINE
) {
551 ie
->reassoc_deadline
= pos
;
552 wpa_hexdump(MSG_DEBUG
, "WPA: Reassoc Deadline "
554 ie
->reassoc_deadline
, pos
[1] + 2);
555 } else if (pos
[2] == WLAN_TIMEOUT_KEY_LIFETIME
) {
556 ie
->key_lifetime
= pos
;
557 wpa_hexdump(MSG_DEBUG
, "WPA: KeyLifetime "
559 ie
->key_lifetime
, pos
[1] + 2);
561 wpa_hexdump(MSG_DEBUG
, "WPA: Unrecognized "
562 "EAPOL-Key Key Data IE",
565 #endif /* CONFIG_IEEE80211R */
566 } else if (*pos
== WLAN_EID_VENDOR_SPECIFIC
) {
567 ret
= wpa_parse_generic(pos
, end
, ie
);
575 wpa_hexdump(MSG_DEBUG
, "WPA: Unrecognized EAPOL-Key "
576 "Key Data IE", pos
, 2 + pos
[1]);