• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * wpa_supplicant - WPA/RSN IE and KDE processing
3  * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "wpa.h"
13 #include "pmksa_cache.h"
14 #include "common/ieee802_11_defs.h"
15 #include "wpa_i.h"
16 #include "wpa_ie.h"
17 
18 
19 /**
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
25  *
26  * Parse the contents of WPA or RSN IE and write the parsed data into data.
27  */
wpa_parse_wpa_ie(const u8 * wpa_ie,size_t wpa_ie_len,struct wpa_ie_data * data)28 int wpa_parse_wpa_ie(const u8 *wpa_ie, size_t wpa_ie_len,
29 		     struct wpa_ie_data *data)
30 {
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);
33 	if (wpa_ie_len >= 6 && wpa_ie[0] == WLAN_EID_VENDOR_SPECIFIC &&
34 	    wpa_ie[1] >= 4 && WPA_GET_BE32(&wpa_ie[2]) == OSEN_IE_VENDOR_TYPE)
35 		return wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, data);
36 	else
37 		return wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, data);
38 }
39 
40 
wpa_gen_wpa_ie_wpa(u8 * wpa_ie,size_t wpa_ie_len,int pairwise_cipher,int group_cipher,int key_mgmt)41 static int wpa_gen_wpa_ie_wpa(u8 *wpa_ie, size_t wpa_ie_len,
42 			      int pairwise_cipher, int group_cipher,
43 			      int key_mgmt)
44 {
45 	u8 *pos;
46 	struct wpa_ie_hdr *hdr;
47 	u32 suite;
48 
49 	if (wpa_ie_len < sizeof(*hdr) + WPA_SELECTOR_LEN +
50 	    2 + WPA_SELECTOR_LEN + 2 + WPA_SELECTOR_LEN)
51 		return -1;
52 
53 	hdr = (struct wpa_ie_hdr *) wpa_ie;
54 	hdr->elem_id = WLAN_EID_VENDOR_SPECIFIC;
55 	RSN_SELECTOR_PUT(hdr->oui, WPA_OUI_TYPE);
56 	WPA_PUT_LE16(hdr->version, WPA_VERSION);
57 	pos = (u8 *) (hdr + 1);
58 
59 	suite = wpa_cipher_to_suite(WPA_PROTO_WPA, group_cipher);
60 	if (suite == 0) {
61 		wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
62 			   group_cipher);
63 		return -1;
64 	}
65 	RSN_SELECTOR_PUT(pos, suite);
66 	pos += WPA_SELECTOR_LEN;
67 
68 	*pos++ = 1;
69 	*pos++ = 0;
70 	suite = wpa_cipher_to_suite(WPA_PROTO_WPA, pairwise_cipher);
71 	if (suite == 0 ||
72 	    (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
73 	     pairwise_cipher != WPA_CIPHER_NONE)) {
74 		wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
75 			   pairwise_cipher);
76 		return -1;
77 	}
78 	RSN_SELECTOR_PUT(pos, suite);
79 	pos += WPA_SELECTOR_LEN;
80 
81 	*pos++ = 1;
82 	*pos++ = 0;
83 	if (key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
84 		RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
85 	} else if (key_mgmt == WPA_KEY_MGMT_PSK) {
86 		RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
87 	} else if (key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
88 		RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_NONE);
89 	} else if (key_mgmt == WPA_KEY_MGMT_CCKM) {
90 		RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_CCKM);
91 	} else {
92 		wpa_printf(MSG_WARNING, "Invalid key management type (%d).",
93 			   key_mgmt);
94 		return -1;
95 	}
96 	pos += WPA_SELECTOR_LEN;
97 
98 	/* WPA Capabilities; use defaults, so no need to include it */
99 
100 	hdr->len = (pos - wpa_ie) - 2;
101 
102 	WPA_ASSERT((size_t) (pos - wpa_ie) <= wpa_ie_len);
103 
104 	return pos - wpa_ie;
105 }
106 
107 
wpa_gen_wpa_ie_rsn(u8 * rsn_ie,size_t rsn_ie_len,int pairwise_cipher,int group_cipher,int key_mgmt,int mgmt_group_cipher,struct wpa_sm * sm)108 static int wpa_gen_wpa_ie_rsn(u8 *rsn_ie, size_t rsn_ie_len,
109 			      int pairwise_cipher, int group_cipher,
110 			      int key_mgmt, int mgmt_group_cipher,
111 			      struct wpa_sm *sm)
112 {
113 	u8 *pos;
114 	struct rsn_ie_hdr *hdr;
115 	u16 capab;
116 	u32 suite;
117 
118 	if (rsn_ie_len < sizeof(*hdr) + RSN_SELECTOR_LEN +
119 	    2 + RSN_SELECTOR_LEN + 2 + RSN_SELECTOR_LEN + 2 +
120 	    (sm->cur_pmksa ? 2 + PMKID_LEN : 0)) {
121 		wpa_printf(MSG_DEBUG, "RSN: Too short IE buffer (%lu bytes)",
122 			   (unsigned long) rsn_ie_len);
123 		return -1;
124 	}
125 
126 	hdr = (struct rsn_ie_hdr *) rsn_ie;
127 	hdr->elem_id = WLAN_EID_RSN;
128 	WPA_PUT_LE16(hdr->version, RSN_VERSION);
129 	pos = (u8 *) (hdr + 1);
130 
131 	suite = wpa_cipher_to_suite(WPA_PROTO_RSN, group_cipher);
132 	if (suite == 0) {
133 		wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
134 			   group_cipher);
135 		return -1;
136 	}
137 	RSN_SELECTOR_PUT(pos, suite);
138 	pos += RSN_SELECTOR_LEN;
139 
140 	*pos++ = 1;
141 	*pos++ = 0;
142 	suite = wpa_cipher_to_suite(WPA_PROTO_RSN, pairwise_cipher);
143 	if (suite == 0 ||
144 	    (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
145 	     pairwise_cipher != WPA_CIPHER_NONE)) {
146 		wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
147 			   pairwise_cipher);
148 		return -1;
149 	}
150 	RSN_SELECTOR_PUT(pos, suite);
151 	pos += RSN_SELECTOR_LEN;
152 
153 	*pos++ = 1;
154 	*pos++ = 0;
155 	if (key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
156 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X);
157 	} else if (key_mgmt == WPA_KEY_MGMT_PSK) {
158 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X);
159 	} else if (key_mgmt == WPA_KEY_MGMT_CCKM) {
160 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_CCKM);
161 #ifdef CONFIG_IEEE80211R
162 	} else if (key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
163 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
164 	} else if (key_mgmt == WPA_KEY_MGMT_FT_PSK) {
165 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
166 #endif /* CONFIG_IEEE80211R */
167 #ifdef CONFIG_IEEE80211W
168 	} else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SHA256) {
169 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SHA256);
170 	} else if (key_mgmt == WPA_KEY_MGMT_PSK_SHA256) {
171 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_SHA256);
172 #endif /* CONFIG_IEEE80211W */
173 #ifdef CONFIG_SAE
174 	} else if (key_mgmt == WPA_KEY_MGMT_SAE) {
175 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE);
176 	} else if (key_mgmt == WPA_KEY_MGMT_FT_SAE) {
177 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
178 #endif /* CONFIG_SAE */
179 	} else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
180 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192);
181 	} else if (key_mgmt == WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
182 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B);
183 #ifdef CONFIG_FILS
184 	} else if (key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
185 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA256);
186 	} else if (key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
187 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA384);
188 #ifdef CONFIG_IEEE80211R
189 	} else if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
190 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
191 	} else if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
192 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
193 #endif /* CONFIG_IEEE80211R */
194 #endif /* CONFIG_FILS */
195 #ifdef CONFIG_OWE
196 	} else if (key_mgmt & WPA_KEY_MGMT_OWE) {
197 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_OWE);
198 #endif /* CONFIG_OWE */
199 #ifdef CONFIG_DPP
200 	} else if (key_mgmt & WPA_KEY_MGMT_DPP) {
201 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_DPP);
202 #endif /* CONFIG_DPP */
203 	} else {
204 		wpa_printf(MSG_WARNING, "Invalid key management type (%d).",
205 			   key_mgmt);
206 		return -1;
207 	}
208 	pos += RSN_SELECTOR_LEN;
209 
210 	/* RSN Capabilities */
211 	capab = 0;
212 #ifdef CONFIG_IEEE80211W
213 	if (sm->mfp)
214 		capab |= WPA_CAPABILITY_MFPC;
215 	if (sm->mfp == 2)
216 		capab |= WPA_CAPABILITY_MFPR;
217 #endif /* CONFIG_IEEE80211W */
218 	WPA_PUT_LE16(pos, capab);
219 	pos += 2;
220 
221 	if (sm->cur_pmksa) {
222 		/* PMKID Count (2 octets, little endian) */
223 		*pos++ = 1;
224 		*pos++ = 0;
225 		/* PMKID */
226 		os_memcpy(pos, sm->cur_pmksa->pmkid, PMKID_LEN);
227 		pos += PMKID_LEN;
228 	}
229 
230 #ifdef CONFIG_IEEE80211W
231 	if (wpa_cipher_valid_mgmt_group(mgmt_group_cipher)) {
232 		if (!sm->cur_pmksa) {
233 			/* PMKID Count */
234 			WPA_PUT_LE16(pos, 0);
235 			pos += 2;
236 		}
237 
238 		/* Management Group Cipher Suite */
239 		RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
240 							  mgmt_group_cipher));
241 		pos += RSN_SELECTOR_LEN;
242 	}
243 #endif /* CONFIG_IEEE80211W */
244 
245 	hdr->len = (pos - rsn_ie) - 2;
246 
247 	WPA_ASSERT((size_t) (pos - rsn_ie) <= rsn_ie_len);
248 
249 	return pos - rsn_ie;
250 }
251 
252 
253 #ifdef CONFIG_HS20
wpa_gen_wpa_ie_osen(u8 * wpa_ie,size_t wpa_ie_len,int pairwise_cipher,int group_cipher,int key_mgmt)254 static int wpa_gen_wpa_ie_osen(u8 *wpa_ie, size_t wpa_ie_len,
255 			       int pairwise_cipher, int group_cipher,
256 			       int key_mgmt)
257 {
258 	u8 *pos, *len;
259 	u32 suite;
260 
261 	if (wpa_ie_len < 2 + 4 + RSN_SELECTOR_LEN +
262 	    2 + RSN_SELECTOR_LEN + 2 + RSN_SELECTOR_LEN)
263 		return -1;
264 
265 	pos = wpa_ie;
266 	*pos++ = WLAN_EID_VENDOR_SPECIFIC;
267 	len = pos++; /* to be filled */
268 	WPA_PUT_BE24(pos, OUI_WFA);
269 	pos += 3;
270 	*pos++ = HS20_OSEN_OUI_TYPE;
271 
272 	/* Group Data Cipher Suite */
273 	suite = wpa_cipher_to_suite(WPA_PROTO_RSN, group_cipher);
274 	if (suite == 0) {
275 		wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
276 			   group_cipher);
277 		return -1;
278 	}
279 	RSN_SELECTOR_PUT(pos, suite);
280 	pos += RSN_SELECTOR_LEN;
281 
282 	/* Pairwise Cipher Suite Count and List */
283 	WPA_PUT_LE16(pos, 1);
284 	pos += 2;
285 	suite = wpa_cipher_to_suite(WPA_PROTO_RSN, pairwise_cipher);
286 	if (suite == 0 ||
287 	    (!wpa_cipher_valid_pairwise(pairwise_cipher) &&
288 	     pairwise_cipher != WPA_CIPHER_NONE)) {
289 		wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
290 			   pairwise_cipher);
291 		return -1;
292 	}
293 	RSN_SELECTOR_PUT(pos, suite);
294 	pos += RSN_SELECTOR_LEN;
295 
296 	/* AKM Suite Count and List */
297 	WPA_PUT_LE16(pos, 1);
298 	pos += 2;
299 	RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_OSEN);
300 	pos += RSN_SELECTOR_LEN;
301 
302 	*len = pos - len - 1;
303 
304 	WPA_ASSERT((size_t) (pos - wpa_ie) <= wpa_ie_len);
305 
306 	return pos - wpa_ie;
307 }
308 #endif /* CONFIG_HS20 */
309 
310 
311 /**
312  * wpa_gen_wpa_ie - Generate WPA/RSN IE based on current security policy
313  * @sm: Pointer to WPA state machine data from wpa_sm_init()
314  * @wpa_ie: Pointer to memory area for the generated WPA/RSN IE
315  * @wpa_ie_len: Maximum length of the generated WPA/RSN IE
316  * Returns: Length of the generated WPA/RSN IE or -1 on failure
317  */
wpa_gen_wpa_ie(struct wpa_sm * sm,u8 * wpa_ie,size_t wpa_ie_len)318 int wpa_gen_wpa_ie(struct wpa_sm *sm, u8 *wpa_ie, size_t wpa_ie_len)
319 {
320 	if (sm->proto == WPA_PROTO_RSN)
321 		return wpa_gen_wpa_ie_rsn(wpa_ie, wpa_ie_len,
322 					  sm->pairwise_cipher,
323 					  sm->group_cipher,
324 					  sm->key_mgmt, sm->mgmt_group_cipher,
325 					  sm);
326 #ifdef CONFIG_HS20
327 	else if (sm->proto == WPA_PROTO_OSEN)
328 		return wpa_gen_wpa_ie_osen(wpa_ie, wpa_ie_len,
329 					   sm->pairwise_cipher,
330 					   sm->group_cipher,
331 					   sm->key_mgmt);
332 #endif /* CONFIG_HS20 */
333 	else
334 		return wpa_gen_wpa_ie_wpa(wpa_ie, wpa_ie_len,
335 					  sm->pairwise_cipher,
336 					  sm->group_cipher,
337 					  sm->key_mgmt);
338 }
339 
340 
341 /**
342  * wpa_parse_vendor_specific - Parse Vendor Specific IEs
343  * @pos: Pointer to the IE header
344  * @end: Pointer to the end of the Key Data buffer
345  * @ie: Pointer to parsed IE data
346  * Returns: 0 on success, 1 if end mark is found, -1 on failure
347  */
wpa_parse_vendor_specific(const u8 * pos,const u8 * end,struct wpa_eapol_ie_parse * ie)348 static int wpa_parse_vendor_specific(const u8 *pos, const u8 *end,
349 				     struct wpa_eapol_ie_parse *ie)
350 {
351 	unsigned int oui;
352 
353 	if (pos[1] < 4) {
354 		wpa_printf(MSG_MSGDUMP, "Too short vendor specific IE ignored (len=%u)",
355 			   pos[1]);
356 		return 1;
357 	}
358 
359 	oui = WPA_GET_BE24(&pos[2]);
360 	if (oui == OUI_MICROSOFT && pos[5] == WMM_OUI_TYPE && pos[1] > 4) {
361 		if (pos[6] == WMM_OUI_SUBTYPE_INFORMATION_ELEMENT) {
362 			ie->wmm = &pos[2];
363 			ie->wmm_len = pos[1];
364 			wpa_hexdump(MSG_DEBUG, "WPA: WMM IE",
365 				    ie->wmm, ie->wmm_len);
366 		} else if (pos[6] == WMM_OUI_SUBTYPE_PARAMETER_ELEMENT) {
367 			ie->wmm = &pos[2];
368 			ie->wmm_len = pos[1];
369 			wpa_hexdump(MSG_DEBUG, "WPA: WMM Parameter Element",
370 				    ie->wmm, ie->wmm_len);
371 		}
372 	}
373 	return 0;
374 }
375 
376 
377 /**
378  * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
379  * @pos: Pointer to the IE header
380  * @end: Pointer to the end of the Key Data buffer
381  * @ie: Pointer to parsed IE data
382  * Returns: 0 on success, 1 if end mark is found, -1 on failure
383  */
wpa_parse_generic(const u8 * pos,const u8 * end,struct wpa_eapol_ie_parse * ie)384 static int wpa_parse_generic(const u8 *pos, const u8 *end,
385 			     struct wpa_eapol_ie_parse *ie)
386 {
387 	if (pos[1] == 0)
388 		return 1;
389 
390 	if (pos[1] >= 6 &&
391 	    RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE &&
392 	    pos[2 + WPA_SELECTOR_LEN] == 1 &&
393 	    pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
394 		ie->wpa_ie = pos;
395 		ie->wpa_ie_len = pos[1] + 2;
396 		wpa_hexdump(MSG_DEBUG, "WPA: WPA IE in EAPOL-Key",
397 			    ie->wpa_ie, ie->wpa_ie_len);
398 		return 0;
399 	}
400 
401 	if (1 + RSN_SELECTOR_LEN < end - pos &&
402 	    pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
403 	    RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) {
404 		ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
405 		wpa_hexdump(MSG_DEBUG, "WPA: PMKID in EAPOL-Key",
406 			    pos, pos[1] + 2);
407 		return 0;
408 	}
409 
410 	if (pos[1] > RSN_SELECTOR_LEN + 2 &&
411 	    RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) {
412 		ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
413 		ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
414 		wpa_hexdump_key(MSG_DEBUG, "WPA: GTK in EAPOL-Key",
415 				pos, pos[1] + 2);
416 		return 0;
417 	}
418 
419 	if (pos[1] > RSN_SELECTOR_LEN + 2 &&
420 	    RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) {
421 		ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN;
422 		ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN;
423 		wpa_hexdump(MSG_DEBUG, "WPA: MAC Address in EAPOL-Key",
424 			    pos, pos[1] + 2);
425 		return 0;
426 	}
427 
428 #ifdef CONFIG_IEEE80211W
429 	if (pos[1] > RSN_SELECTOR_LEN + 2 &&
430 	    RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) {
431 		ie->igtk = pos + 2 + RSN_SELECTOR_LEN;
432 		ie->igtk_len = pos[1] - RSN_SELECTOR_LEN;
433 		wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK in EAPOL-Key",
434 				pos, pos[1] + 2);
435 		return 0;
436 	}
437 #endif /* CONFIG_IEEE80211W */
438 
439 #ifdef CONFIG_P2P
440 	if (pos[1] >= RSN_SELECTOR_LEN + 1 &&
441 	    RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_REQ) {
442 		ie->ip_addr_req = pos + 2 + RSN_SELECTOR_LEN;
443 		wpa_hexdump(MSG_DEBUG, "WPA: IP Address Request in EAPOL-Key",
444 			    ie->ip_addr_req, pos[1] - RSN_SELECTOR_LEN);
445 		return 0;
446 	}
447 
448 	if (pos[1] >= RSN_SELECTOR_LEN + 3 * 4 &&
449 	    RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_ALLOC) {
450 		ie->ip_addr_alloc = pos + 2 + RSN_SELECTOR_LEN;
451 		wpa_hexdump(MSG_DEBUG,
452 			    "WPA: IP Address Allocation in EAPOL-Key",
453 			    ie->ip_addr_alloc, pos[1] - RSN_SELECTOR_LEN);
454 		return 0;
455 	}
456 #endif /* CONFIG_P2P */
457 
458 	return 0;
459 }
460 
461 
462 /**
463  * wpa_supplicant_parse_ies - Parse EAPOL-Key Key Data IEs
464  * @buf: Pointer to the Key Data buffer
465  * @len: Key Data Length
466  * @ie: Pointer to parsed IE data
467  * Returns: 0 on success, -1 on failure
468  */
wpa_supplicant_parse_ies(const u8 * buf,size_t len,struct wpa_eapol_ie_parse * ie)469 int wpa_supplicant_parse_ies(const u8 *buf, size_t len,
470 			     struct wpa_eapol_ie_parse *ie)
471 {
472 	const u8 *pos, *end;
473 	int ret = 0;
474 
475 	os_memset(ie, 0, sizeof(*ie));
476 	for (pos = buf, end = pos + len; end - pos > 1; pos += 2 + pos[1]) {
477 		if (pos[0] == 0xdd &&
478 		    ((pos == buf + len - 1) || pos[1] == 0)) {
479 			/* Ignore padding */
480 			break;
481 		}
482 		if (2 + pos[1] > end - pos) {
483 			wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data "
484 				   "underflow (ie=%d len=%d pos=%d)",
485 				   pos[0], pos[1], (int) (pos - buf));
486 			wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data",
487 					buf, len);
488 			ret = -1;
489 			break;
490 		}
491 		if (*pos == WLAN_EID_RSN) {
492 			ie->rsn_ie = pos;
493 			ie->rsn_ie_len = pos[1] + 2;
494 			wpa_hexdump(MSG_DEBUG, "WPA: RSN IE in EAPOL-Key",
495 				    ie->rsn_ie, ie->rsn_ie_len);
496 		} else if (*pos == WLAN_EID_MOBILITY_DOMAIN &&
497 			   pos[1] >= sizeof(struct rsn_mdie)) {
498 			ie->mdie = pos;
499 			ie->mdie_len = pos[1] + 2;
500 			wpa_hexdump(MSG_DEBUG, "WPA: MDIE in EAPOL-Key",
501 				    ie->mdie, ie->mdie_len);
502 		} else if (*pos == WLAN_EID_FAST_BSS_TRANSITION &&
503 			   pos[1] >= sizeof(struct rsn_ftie)) {
504 			ie->ftie = pos;
505 			ie->ftie_len = pos[1] + 2;
506 			wpa_hexdump(MSG_DEBUG, "WPA: FTIE in EAPOL-Key",
507 				    ie->ftie, ie->ftie_len);
508 		} else if (*pos == WLAN_EID_TIMEOUT_INTERVAL && pos[1] >= 5) {
509 			if (pos[2] == WLAN_TIMEOUT_REASSOC_DEADLINE) {
510 				ie->reassoc_deadline = pos;
511 				wpa_hexdump(MSG_DEBUG, "WPA: Reassoc Deadline "
512 					    "in EAPOL-Key",
513 					    ie->reassoc_deadline, pos[1] + 2);
514 			} else if (pos[2] == WLAN_TIMEOUT_KEY_LIFETIME) {
515 				ie->key_lifetime = pos;
516 				wpa_hexdump(MSG_DEBUG, "WPA: KeyLifetime "
517 					    "in EAPOL-Key",
518 					    ie->key_lifetime, pos[1] + 2);
519 			} else {
520 				wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized "
521 					    "EAPOL-Key Key Data IE",
522 					    pos, 2 + pos[1]);
523 			}
524 		} else if (*pos == WLAN_EID_LINK_ID) {
525 			if (pos[1] >= 18) {
526 				ie->lnkid = pos;
527 				ie->lnkid_len = pos[1] + 2;
528 			}
529 		} else if (*pos == WLAN_EID_EXT_CAPAB) {
530 			ie->ext_capab = pos;
531 			ie->ext_capab_len = pos[1] + 2;
532 		} else if (*pos == WLAN_EID_SUPP_RATES) {
533 			ie->supp_rates = pos;
534 			ie->supp_rates_len = pos[1] + 2;
535 		} else if (*pos == WLAN_EID_EXT_SUPP_RATES) {
536 			ie->ext_supp_rates = pos;
537 			ie->ext_supp_rates_len = pos[1] + 2;
538 		} else if (*pos == WLAN_EID_HT_CAP &&
539 			   pos[1] >= sizeof(struct ieee80211_ht_capabilities)) {
540 			ie->ht_capabilities = pos + 2;
541 		} else if (*pos == WLAN_EID_VHT_AID) {
542 			if (pos[1] >= 2)
543 				ie->aid = WPA_GET_LE16(pos + 2) & 0x3fff;
544 		} else if (*pos == WLAN_EID_VHT_CAP &&
545 			   pos[1] >= sizeof(struct ieee80211_vht_capabilities))
546 		{
547 			ie->vht_capabilities = pos + 2;
548 		} else if (*pos == WLAN_EID_QOS && pos[1] >= 1) {
549 			ie->qosinfo = pos[2];
550 		} else if (*pos == WLAN_EID_SUPPORTED_CHANNELS) {
551 			ie->supp_channels = pos + 2;
552 			ie->supp_channels_len = pos[1];
553 		} else if (*pos == WLAN_EID_SUPPORTED_OPERATING_CLASSES) {
554 			/*
555 			 * The value of the Length field of the Supported
556 			 * Operating Classes element is between 2 and 253.
557 			 * Silently skip invalid elements to avoid interop
558 			 * issues when trying to use the value.
559 			 */
560 			if (pos[1] >= 2 && pos[1] <= 253) {
561 				ie->supp_oper_classes = pos + 2;
562 				ie->supp_oper_classes_len = pos[1];
563 			}
564 		} else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
565 			ret = wpa_parse_generic(pos, end, ie);
566 			if (ret < 0)
567 				break;
568 			if (ret > 0) {
569 				ret = 0;
570 				break;
571 			}
572 
573 			ret = wpa_parse_vendor_specific(pos, end, ie);
574 			if (ret < 0)
575 				break;
576 			if (ret > 0) {
577 				ret = 0;
578 				break;
579 			}
580 		} else {
581 			wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key "
582 				    "Key Data IE", pos, 2 + pos[1]);
583 		}
584 	}
585 
586 	return ret;
587 }
588