2 * wpa_supplicant - WPA/RSN IE and KDE processing
3 * Copyright (c) 2003-2007, 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 "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 */
320 wpa_printf(MSG_WARNING
, "Invalid key management type (%d).",
324 pos
+= RSN_SELECTOR_LEN
;
326 /* RSN Capabilities */
328 #ifdef CONFIG_IEEE80211W
329 if (mgmt_group_cipher
== WPA_CIPHER_AES_128_CMAC
)
330 capab
|= WPA_CAPABILITY_MGMT_FRAME_PROTECTION
;
331 #endif /* CONFIG_IEEE80211W */
332 WPA_PUT_LE16(pos
, capab
);
336 /* PMKID Count (2 octets, little endian) */
340 os_memcpy(pos
, sm
->cur_pmksa
->pmkid
, PMKID_LEN
);
344 #ifdef CONFIG_IEEE80211W
345 if (mgmt_group_cipher
== WPA_CIPHER_AES_128_CMAC
) {
346 if (!sm
->cur_pmksa
) {
348 WPA_PUT_LE16(pos
, 0);
352 /* Management Group Cipher Suite */
353 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_AES_128_CMAC
);
354 pos
+= RSN_SELECTOR_LEN
;
356 #endif /* CONFIG_IEEE80211W */
358 hdr
->len
= (pos
- rsn_ie
) - 2;
360 WPA_ASSERT((size_t) (pos
- rsn_ie
) <= rsn_ie_len
);
363 #else /* CONFIG_NO_WPA2 */
365 #endif /* CONFIG_NO_WPA2 */
370 * wpa_gen_wpa_ie - Generate WPA/RSN IE based on current security policy
371 * @sm: Pointer to WPA state machine data from wpa_sm_init()
372 * @wpa_ie: Pointer to memory area for the generated WPA/RSN IE
373 * @wpa_ie_len: Maximum length of the generated WPA/RSN IE
374 * Returns: Length of the generated WPA/RSN IE or -1 on failure
376 int wpa_gen_wpa_ie(struct wpa_sm
*sm
, u8
*wpa_ie
, size_t wpa_ie_len
)
378 if (sm
->proto
== WPA_PROTO_RSN
)
379 return wpa_gen_wpa_ie_rsn(wpa_ie
, wpa_ie_len
,
382 sm
->key_mgmt
, sm
->mgmt_group_cipher
,
385 return wpa_gen_wpa_ie_wpa(wpa_ie
, wpa_ie_len
,
393 * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
394 * @pos: Pointer to the IE header
395 * @end: Pointer to the end of the Key Data buffer
396 * @ie: Pointer to parsed IE data
397 * Returns: 0 on success, 1 if end mark is found, -1 on failure
399 static int wpa_parse_generic(const u8
*pos
, const u8
*end
,
400 struct wpa_eapol_ie_parse
*ie
)
406 RSN_SELECTOR_GET(pos
+ 2) == WPA_OUI_TYPE
&&
407 pos
[2 + WPA_SELECTOR_LEN
] == 1 &&
408 pos
[2 + WPA_SELECTOR_LEN
+ 1] == 0) {
410 ie
->wpa_ie_len
= pos
[1] + 2;
414 if (pos
+ 1 + RSN_SELECTOR_LEN
< end
&&
415 pos
[1] >= RSN_SELECTOR_LEN
+ PMKID_LEN
&&
416 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_PMKID
) {
417 ie
->pmkid
= pos
+ 2 + RSN_SELECTOR_LEN
;
421 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
422 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_GROUPKEY
) {
423 ie
->gtk
= pos
+ 2 + RSN_SELECTOR_LEN
;
424 ie
->gtk_len
= pos
[1] - RSN_SELECTOR_LEN
;
428 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
429 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_MAC_ADDR
) {
430 ie
->mac_addr
= pos
+ 2 + RSN_SELECTOR_LEN
;
431 ie
->mac_addr_len
= pos
[1] - RSN_SELECTOR_LEN
;
435 #ifdef CONFIG_PEERKEY
436 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
437 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_SMK
) {
438 ie
->smk
= pos
+ 2 + RSN_SELECTOR_LEN
;
439 ie
->smk_len
= pos
[1] - RSN_SELECTOR_LEN
;
443 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
444 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_NONCE
) {
445 ie
->nonce
= pos
+ 2 + RSN_SELECTOR_LEN
;
446 ie
->nonce_len
= pos
[1] - RSN_SELECTOR_LEN
;
450 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
451 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_LIFETIME
) {
452 ie
->lifetime
= pos
+ 2 + RSN_SELECTOR_LEN
;
453 ie
->lifetime_len
= pos
[1] - RSN_SELECTOR_LEN
;
457 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
458 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_ERROR
) {
459 ie
->error
= pos
+ 2 + RSN_SELECTOR_LEN
;
460 ie
->error_len
= pos
[1] - RSN_SELECTOR_LEN
;
463 #endif /* CONFIG_PEERKEY */
465 #ifdef CONFIG_IEEE80211W
466 if (pos
[1] > RSN_SELECTOR_LEN
+ 2 &&
467 RSN_SELECTOR_GET(pos
+ 2) == RSN_KEY_DATA_IGTK
) {
468 ie
->igtk
= pos
+ 2 + RSN_SELECTOR_LEN
;
469 ie
->igtk_len
= pos
[1] - RSN_SELECTOR_LEN
;
472 #endif /* CONFIG_IEEE80211W */
479 * wpa_supplicant_parse_ies - Parse EAPOL-Key Key Data IEs
480 * @buf: Pointer to the Key Data buffer
481 * @len: Key Data Length
482 * @ie: Pointer to parsed IE data
483 * Returns: 0 on success, -1 on failure
485 int wpa_supplicant_parse_ies(const u8
*buf
, size_t len
,
486 struct wpa_eapol_ie_parse
*ie
)
491 os_memset(ie
, 0, sizeof(*ie
));
492 for (pos
= buf
, end
= pos
+ len
; pos
+ 1 < end
; pos
+= 2 + pos
[1]) {
493 if (pos
[0] == 0xdd &&
494 ((pos
== buf
+ len
- 1) || pos
[1] == 0)) {
498 if (pos
+ 2 + pos
[1] > end
) {
499 wpa_printf(MSG_DEBUG
, "WPA: EAPOL-Key Key Data "
500 "underflow (ie=%d len=%d pos=%d)",
501 pos
[0], pos
[1], (int) (pos
- buf
));
502 wpa_hexdump_key(MSG_DEBUG
, "WPA: Key Data",
507 if (*pos
== WLAN_EID_RSN
) {
509 ie
->rsn_ie_len
= pos
[1] + 2;
510 #ifdef CONFIG_IEEE80211R
511 } else if (*pos
== WLAN_EID_MOBILITY_DOMAIN
) {
513 ie
->mdie_len
= pos
[1] + 2;
514 #endif /* CONFIG_IEEE80211R */
515 } else if (*pos
== WLAN_EID_VENDOR_SPECIFIC
) {
516 ret
= wpa_parse_generic(pos
, end
, ie
);
524 wpa_hexdump(MSG_DEBUG
, "WPA: Unrecognized EAPOL-Key "
525 "Key Data IE", pos
, 2 + pos
[1]);