• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA/RSN - Shared functions for supplicant and authenticator
3  * Copyright (c) 2002-2018, 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 "crypto/md5.h"
13 #include "crypto/sha1.h"
14 #include "crypto/sha256.h"
15 #include "crypto/sha384.h"
16 #include "crypto/sha512.h"
17 #include "crypto/aes_wrap.h"
18 #include "crypto/crypto.h"
19 #include "ieee802_11_defs.h"
20 #include "ieee802_11_common.h"
21 #include "defs.h"
22 #include "wpa_common.h"
23 #ifdef CONFIG_WAPI
24 #include "securec.h"
25 #endif
26 
27 
wpa_kck_len(int akmp,size_t pmk_len)28 static unsigned int wpa_kck_len(int akmp, size_t pmk_len)
29 {
30 	switch (akmp) {
31 	case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
32 	case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
33 		return 24;
34 	case WPA_KEY_MGMT_FILS_SHA256:
35 	case WPA_KEY_MGMT_FT_FILS_SHA256:
36 	case WPA_KEY_MGMT_FILS_SHA384:
37 	case WPA_KEY_MGMT_FT_FILS_SHA384:
38 		return 0;
39 	case WPA_KEY_MGMT_DPP:
40 		return pmk_len / 2;
41 	case WPA_KEY_MGMT_OWE:
42 		return pmk_len / 2;
43 	case WPA_KEY_MGMT_SAE_EXT_KEY:
44 		return pmk_len / 2;
45 	default:
46 		return 16;
47 	}
48 }
49 
50 
51 #ifdef CONFIG_IEEE80211R
wpa_kck2_len(int akmp)52 static unsigned int wpa_kck2_len(int akmp)
53 {
54 	switch (akmp) {
55 	case WPA_KEY_MGMT_FT_FILS_SHA256:
56 		return 16;
57 	case WPA_KEY_MGMT_FT_FILS_SHA384:
58 		return 24;
59 	default:
60 		return 0;
61 	}
62 }
63 #endif /* CONFIG_IEEE80211R */
64 
65 
wpa_kek_len(int akmp,size_t pmk_len)66 static unsigned int wpa_kek_len(int akmp, size_t pmk_len)
67 {
68 	switch (akmp) {
69 	case WPA_KEY_MGMT_FILS_SHA384:
70 	case WPA_KEY_MGMT_FT_FILS_SHA384:
71 		return 64;
72 	case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
73 	case WPA_KEY_MGMT_FILS_SHA256:
74 	case WPA_KEY_MGMT_FT_FILS_SHA256:
75 	case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
76 		return 32;
77 	case WPA_KEY_MGMT_DPP:
78 		return pmk_len <= 32 ? 16 : 32;
79 	case WPA_KEY_MGMT_OWE:
80 		return pmk_len <= 32 ? 16 : 32;
81 	case WPA_KEY_MGMT_SAE_EXT_KEY:
82 		return pmk_len <= 32 ? 16 : 32;
83 	default:
84 		return 16;
85 	}
86 }
87 
88 
89 #ifdef CONFIG_IEEE80211R
wpa_kek2_len(int akmp)90 static unsigned int wpa_kek2_len(int akmp)
91 {
92 	switch (akmp) {
93 	case WPA_KEY_MGMT_FT_FILS_SHA256:
94 		return 16;
95 	case WPA_KEY_MGMT_FT_FILS_SHA384:
96 		return 32;
97 	default:
98 		return 0;
99 	}
100 }
101 #endif /* CONFIG_IEEE80211R */
102 
103 
wpa_mic_len(int akmp,size_t pmk_len)104 unsigned int wpa_mic_len(int akmp, size_t pmk_len)
105 {
106 	switch (akmp) {
107 	case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
108 	case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
109 		return 24;
110 	case WPA_KEY_MGMT_FILS_SHA256:
111 	case WPA_KEY_MGMT_FILS_SHA384:
112 	case WPA_KEY_MGMT_FT_FILS_SHA256:
113 	case WPA_KEY_MGMT_FT_FILS_SHA384:
114 		return 0;
115 	case WPA_KEY_MGMT_DPP:
116 		return pmk_len / 2;
117 	case WPA_KEY_MGMT_OWE:
118 		return pmk_len / 2;
119 	case WPA_KEY_MGMT_SAE_EXT_KEY:
120 		return pmk_len / 2;
121 	default:
122 		return 16;
123 	}
124 }
125 
126 
127 /**
128  * wpa_use_akm_defined - Is AKM-defined Key Descriptor Version used
129  * @akmp: WPA_KEY_MGMT_* used in key derivation
130  * Returns: 1 if AKM-defined Key Descriptor Version is used; 0 otherwise
131  */
wpa_use_akm_defined(int akmp)132 int wpa_use_akm_defined(int akmp)
133 {
134 	return akmp == WPA_KEY_MGMT_OSEN ||
135 		akmp == WPA_KEY_MGMT_OWE ||
136 		akmp == WPA_KEY_MGMT_DPP ||
137 		akmp == WPA_KEY_MGMT_FT_IEEE8021X_SHA384 ||
138 		wpa_key_mgmt_sae(akmp) ||
139 		wpa_key_mgmt_suite_b(akmp) ||
140 		wpa_key_mgmt_fils(akmp);
141 }
142 
143 
144 /**
145  * wpa_use_cmac - Is CMAC integrity algorithm used for EAPOL-Key MIC
146  * @akmp: WPA_KEY_MGMT_* used in key derivation
147  * Returns: 1 if CMAC is used; 0 otherwise
148  */
wpa_use_cmac(int akmp)149 int wpa_use_cmac(int akmp)
150 {
151 	return akmp == WPA_KEY_MGMT_OSEN ||
152 		akmp == WPA_KEY_MGMT_OWE ||
153 		akmp == WPA_KEY_MGMT_DPP ||
154 		wpa_key_mgmt_ft(akmp) ||
155 		wpa_key_mgmt_sha256(akmp) ||
156 		(wpa_key_mgmt_sae(akmp) &&
157 		 !wpa_key_mgmt_sae_ext_key(akmp)) ||
158 		wpa_key_mgmt_suite_b(akmp);
159 }
160 
161 
162 /**
163  * wpa_use_aes_key_wrap - Is AES Keywrap algorithm used for EAPOL-Key Key Data
164  * @akmp: WPA_KEY_MGMT_* used in key derivation
165  * Returns: 1 if AES Keywrap is used; 0 otherwise
166  *
167  * Note: AKM 00-0F-AC:1 and 00-0F-AC:2 have special rules for selecting whether
168  * to use AES Keywrap based on the negotiated pairwise cipher. This function
169  * does not cover those special cases.
170  */
wpa_use_aes_key_wrap(int akmp)171 int wpa_use_aes_key_wrap(int akmp)
172 {
173 	return akmp == WPA_KEY_MGMT_OSEN ||
174 		akmp == WPA_KEY_MGMT_OWE ||
175 		akmp == WPA_KEY_MGMT_DPP ||
176 		wpa_key_mgmt_ft(akmp) ||
177 		wpa_key_mgmt_sha256(akmp) ||
178 		wpa_key_mgmt_sae(akmp) ||
179 		wpa_key_mgmt_suite_b(akmp);
180 }
181 
182 
183 /**
184  * wpa_eapol_key_mic - Calculate EAPOL-Key MIC
185  * @key: EAPOL-Key Key Confirmation Key (KCK)
186  * @key_len: KCK length in octets
187  * @akmp: WPA_KEY_MGMT_* used in key derivation
188  * @ver: Key descriptor version (WPA_KEY_INFO_TYPE_*)
189  * @buf: Pointer to the beginning of the EAPOL header (version field)
190  * @len: Length of the EAPOL frame (from EAPOL header to the end of the frame)
191  * @mic: Pointer to the buffer to which the EAPOL-Key MIC is written
192  * Returns: 0 on success, -1 on failure
193  *
194  * Calculate EAPOL-Key MIC for an EAPOL-Key packet. The EAPOL-Key MIC field has
195  * to be cleared (all zeroes) when calling this function.
196  *
197  * Note: 'IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames' has an error in the
198  * description of the Key MIC calculation. It includes packet data from the
199  * beginning of the EAPOL-Key header, not EAPOL header. This incorrect change
200  * happened during final editing of the standard and the correct behavior is
201  * defined in the last draft (IEEE 802.11i/D10).
202  */
wpa_eapol_key_mic(const u8 * key,size_t key_len,int akmp,int ver,const u8 * buf,size_t len,u8 * mic)203 int wpa_eapol_key_mic(const u8 *key, size_t key_len, int akmp, int ver,
204 		      const u8 *buf, size_t len, u8 *mic)
205 {
206 	u8 hash[SHA512_MAC_LEN];
207 
208 	if (key_len == 0) {
209 		wpa_printf(MSG_DEBUG,
210 			   "WPA: KCK not set - cannot calculate MIC");
211 		return -1;
212 	}
213 
214 	switch (ver) {
215 #ifndef CONFIG_FIPS
216 	case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
217 		wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key MIC using HMAC-MD5");
218 		return hmac_md5(key, key_len, buf, len, mic);
219 #endif /* CONFIG_FIPS */
220 	case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
221 		wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key MIC using HMAC-SHA1");
222 		if (hmac_sha1(key, key_len, buf, len, hash))
223 			return -1;
224 		os_memcpy(mic, hash, MD5_MAC_LEN);
225 		break;
226 	case WPA_KEY_INFO_TYPE_AES_128_CMAC:
227 		wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key MIC using AES-CMAC");
228 		return omac1_aes_128(key, buf, len, mic);
229 	case WPA_KEY_INFO_TYPE_AKM_DEFINED:
230 		switch (akmp) {
231 #ifdef CONFIG_SAE
232 		case WPA_KEY_MGMT_SAE:
233 		case WPA_KEY_MGMT_FT_SAE:
234 			wpa_printf(MSG_DEBUG,
235 				   "WPA: EAPOL-Key MIC using AES-CMAC (AKM-defined - SAE)");
236 			return omac1_aes_128(key, buf, len, mic);
237 		case WPA_KEY_MGMT_SAE_EXT_KEY:
238 			wpa_printf(MSG_DEBUG,
239 				   "WPA: EAPOL-Key MIC using HMAC-SHA%u (AKM-defined - SAE-EXT-KEY)",
240 				   (unsigned int) key_len * 8 * 2);
241 			if (key_len == 128 / 8) {
242 				if (hmac_sha256(key, key_len, buf, len, hash))
243 					return -1;
244 #ifdef CONFIG_SHA384
245 			} else if (key_len == 192 / 8) {
246 				if (hmac_sha384(key, key_len, buf, len, hash))
247 					return -1;
248 #endif /* CONFIG_SHA384 */
249 #ifdef CONFIG_SHA512
250 			} else if (key_len == 256 / 8) {
251 				if (hmac_sha512(key, key_len, buf, len, hash))
252 					return -1;
253 #endif /* CONFIG_SHA512 */
254 			} else {
255 				wpa_printf(MSG_INFO,
256 					   "SAE: Unsupported KCK length: %u",
257 					   (unsigned int) key_len);
258 				return -1;
259 			}
260 			os_memcpy(mic, hash, key_len);
261 			break;
262 #endif /* CONFIG_SAE */
263 #ifdef CONFIG_HS20
264 		case WPA_KEY_MGMT_OSEN:
265 			wpa_printf(MSG_DEBUG,
266 				   "WPA: EAPOL-Key MIC using AES-CMAC (AKM-defined - OSEN)");
267 			return omac1_aes_128(key, buf, len, mic);
268 #endif /* CONFIG_HS20 */
269 #ifdef CONFIG_SUITEB
270 		case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
271 			wpa_printf(MSG_DEBUG,
272 				   "WPA: EAPOL-Key MIC using HMAC-SHA256 (AKM-defined - Suite B)");
273 			if (hmac_sha256(key, key_len, buf, len, hash))
274 				return -1;
275 			os_memcpy(mic, hash, MD5_MAC_LEN);
276 			break;
277 #endif /* CONFIG_SUITEB */
278 #ifdef CONFIG_SUITEB192
279 		case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
280 			wpa_printf(MSG_DEBUG,
281 				   "WPA: EAPOL-Key MIC using HMAC-SHA384 (AKM-defined - Suite B 192-bit)");
282 			if (hmac_sha384(key, key_len, buf, len, hash))
283 				return -1;
284 			os_memcpy(mic, hash, 24);
285 			break;
286 #endif /* CONFIG_SUITEB192 */
287 #ifdef CONFIG_OWE
288 		case WPA_KEY_MGMT_OWE:
289 			wpa_printf(MSG_DEBUG,
290 				   "WPA: EAPOL-Key MIC using HMAC-SHA%u (AKM-defined - OWE)",
291 				   (unsigned int) key_len * 8 * 2);
292 			if (key_len == 128 / 8) {
293 				if (hmac_sha256(key, key_len, buf, len, hash))
294 					return -1;
295 			} else if (key_len == 192 / 8) {
296 				if (hmac_sha384(key, key_len, buf, len, hash))
297 					return -1;
298 			} else if (key_len == 256 / 8) {
299 				if (hmac_sha512(key, key_len, buf, len, hash))
300 					return -1;
301 			} else {
302 				wpa_printf(MSG_INFO,
303 					   "OWE: Unsupported KCK length: %u",
304 					   (unsigned int) key_len);
305 				return -1;
306 			}
307 			os_memcpy(mic, hash, key_len);
308 			break;
309 #endif /* CONFIG_OWE */
310 #ifdef CONFIG_DPP
311 		case WPA_KEY_MGMT_DPP:
312 			wpa_printf(MSG_DEBUG,
313 				   "WPA: EAPOL-Key MIC using HMAC-SHA%u (AKM-defined - DPP)",
314 				   (unsigned int) key_len * 8 * 2);
315 			if (key_len == 128 / 8) {
316 				if (hmac_sha256(key, key_len, buf, len, hash))
317 					return -1;
318 			} else if (key_len == 192 / 8) {
319 				if (hmac_sha384(key, key_len, buf, len, hash))
320 					return -1;
321 			} else if (key_len == 256 / 8) {
322 				if (hmac_sha512(key, key_len, buf, len, hash))
323 					return -1;
324 			} else {
325 				wpa_printf(MSG_INFO,
326 					   "DPP: Unsupported KCK length: %u",
327 					   (unsigned int) key_len);
328 				return -1;
329 			}
330 			os_memcpy(mic, hash, key_len);
331 			break;
332 #endif /* CONFIG_DPP */
333 #if defined(CONFIG_IEEE80211R) && defined(CONFIG_SHA384)
334 		case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
335 			wpa_printf(MSG_DEBUG,
336 				   "WPA: EAPOL-Key MIC using HMAC-SHA384 (AKM-defined - FT 802.1X SHA384)");
337 			if (hmac_sha384(key, key_len, buf, len, hash))
338 				return -1;
339 			os_memcpy(mic, hash, 24);
340 			break;
341 #endif /* CONFIG_IEEE80211R && CONFIG_SHA384 */
342 		default:
343 			wpa_printf(MSG_DEBUG,
344 				   "WPA: EAPOL-Key MIC algorithm not known (AKM-defined - akmp=0x%x)",
345 				   akmp);
346 			return -1;
347 		}
348 		break;
349 	default:
350 		wpa_printf(MSG_DEBUG,
351 			   "WPA: EAPOL-Key MIC algorithm not known (ver=%d)",
352 			   ver);
353 		return -1;
354 	}
355 
356 	return 0;
357 }
358 
359 
360 /**
361  * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces
362  * @pmk: Pairwise master key
363  * @pmk_len: Length of PMK
364  * @label: Label to use in derivation
365  * @addr1: AA or SA
366  * @addr2: SA or AA
367  * @nonce1: ANonce or SNonce
368  * @nonce2: SNonce or ANonce
369  * @ptk: Buffer for pairwise transient key
370  * @akmp: Negotiated AKM
371  * @cipher: Negotiated pairwise cipher
372  * @kdk_len: The length in octets that should be derived for KDK
373  * Returns: 0 on success, -1 on failure
374  *
375  * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
376  * PTK = PRF-X(PMK, "Pairwise key expansion",
377  *             Min(AA, SA) || Max(AA, SA) ||
378  *             Min(ANonce, SNonce) || Max(ANonce, SNonce)
379  *             [ || Z.x ])
380  *
381  * The optional Z.x component is used only with DPP and that part is not defined
382  * in IEEE 802.11.
383  */
wpa_pmk_to_ptk(const u8 * pmk,size_t pmk_len,const char * label,const u8 * addr1,const u8 * addr2,const u8 * nonce1,const u8 * nonce2,struct wpa_ptk * ptk,int akmp,int cipher,const u8 * z,size_t z_len,size_t kdk_len)384 int wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label,
385 		   const u8 *addr1, const u8 *addr2,
386 		   const u8 *nonce1, const u8 *nonce2,
387 		   struct wpa_ptk *ptk, int akmp, int cipher,
388 		   const u8 *z, size_t z_len, size_t kdk_len)
389 {
390 #define MAX_Z_LEN 66 /* with NIST P-521 */
391 	u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN + MAX_Z_LEN];
392 	size_t data_len = 2 * ETH_ALEN + 2 * WPA_NONCE_LEN;
393 	u8 tmp[WPA_KCK_MAX_LEN + WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN +
394 		WPA_KDK_MAX_LEN];
395 	size_t ptk_len;
396 #ifdef CONFIG_OWE
397 	int owe_ptk_workaround = 0;
398 
399 	if (akmp == (WPA_KEY_MGMT_OWE | WPA_KEY_MGMT_PSK_SHA256)) {
400 		owe_ptk_workaround = 1;
401 		akmp = WPA_KEY_MGMT_OWE;
402 	}
403 #endif /* CONFIG_OWE */
404 
405 	if (pmk_len == 0) {
406 		wpa_printf(MSG_ERROR, "WPA: No PMK set for PTK derivation");
407 		return -1;
408 	}
409 
410 	if (z_len > MAX_Z_LEN)
411 		return -1;
412 
413 	if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) {
414 		os_memcpy(data, addr1, ETH_ALEN);
415 		os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN);
416 	} else {
417 		os_memcpy(data, addr2, ETH_ALEN);
418 		os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN);
419 	}
420 
421 	if (os_memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) {
422 		os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN);
423 		os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2,
424 			  WPA_NONCE_LEN);
425 	} else {
426 		os_memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN);
427 		os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1,
428 			  WPA_NONCE_LEN);
429 	}
430 
431 	if (z && z_len) {
432 		os_memcpy(data + 2 * ETH_ALEN + 2 * WPA_NONCE_LEN, z, z_len);
433 		data_len += z_len;
434 	}
435 
436 	if (kdk_len > WPA_KDK_MAX_LEN) {
437 		wpa_printf(MSG_ERROR,
438 			   "WPA: KDK len=%zu exceeds max supported len",
439 			   kdk_len);
440 		return -1;
441 	}
442 
443 	ptk->kck_len = wpa_kck_len(akmp, pmk_len);
444 	ptk->kek_len = wpa_kek_len(akmp, pmk_len);
445 	ptk->tk_len = wpa_cipher_key_len(cipher);
446 	ptk->kdk_len = kdk_len;
447 	if (ptk->tk_len == 0) {
448 		wpa_printf(MSG_ERROR,
449 			   "WPA: Unsupported cipher (0x%x) used in PTK derivation",
450 			   cipher);
451 		return -1;
452 	}
453 	ptk_len = ptk->kck_len + ptk->kek_len + ptk->tk_len + ptk->kdk_len;
454 
455 	if (wpa_key_mgmt_sha384(akmp)) {
456 #if defined(CONFIG_SUITEB192) || defined(CONFIG_FILS)
457 		wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA384)");
458 		if (sha384_prf(pmk, pmk_len, label, data, data_len,
459 			       tmp, ptk_len) < 0)
460 			return -1;
461 #else /* CONFIG_SUITEB192 || CONFIG_FILS */
462 		return -1;
463 #endif /* CONFIG_SUITEB192 || CONFIG_FILS */
464 	} else if (wpa_key_mgmt_sha256(akmp)) {
465 		wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA256)");
466 		if (sha256_prf(pmk, pmk_len, label, data, data_len,
467 			       tmp, ptk_len) < 0)
468 			return -1;
469 #ifdef CONFIG_OWE
470 	} else if (akmp == WPA_KEY_MGMT_OWE && (pmk_len == 32 ||
471 						owe_ptk_workaround)) {
472 		wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA256)");
473 		if (sha256_prf(pmk, pmk_len, label, data, data_len,
474 			       tmp, ptk_len) < 0)
475 			return -1;
476 	} else if (akmp == WPA_KEY_MGMT_OWE && pmk_len == 48) {
477 		wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA384)");
478 		if (sha384_prf(pmk, pmk_len, label, data, data_len,
479 			       tmp, ptk_len) < 0)
480 			return -1;
481 	} else if (akmp == WPA_KEY_MGMT_OWE && pmk_len == 64) {
482 		wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA512)");
483 		if (sha512_prf(pmk, pmk_len, label, data, data_len,
484 			       tmp, ptk_len) < 0)
485 			return -1;
486 	} else if (akmp == WPA_KEY_MGMT_OWE) {
487 		wpa_printf(MSG_INFO, "OWE: Unknown PMK length %u",
488 			   (unsigned int) pmk_len);
489 		return -1;
490 #endif /* CONFIG_OWE */
491 #ifdef CONFIG_DPP
492 	} else if (akmp == WPA_KEY_MGMT_DPP && pmk_len == 32) {
493 		wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA256)");
494 		if (sha256_prf(pmk, pmk_len, label, data, data_len,
495 			       tmp, ptk_len) < 0)
496 			return -1;
497 	} else if (akmp == WPA_KEY_MGMT_DPP && pmk_len == 48) {
498 		wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA384)");
499 		if (sha384_prf(pmk, pmk_len, label, data, data_len,
500 			       tmp, ptk_len) < 0)
501 			return -1;
502 	} else if (akmp == WPA_KEY_MGMT_DPP && pmk_len == 64) {
503 		wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA512)");
504 		if (sha512_prf(pmk, pmk_len, label, data, data_len,
505 			       tmp, ptk_len) < 0)
506 			return -1;
507 	} else if (akmp == WPA_KEY_MGMT_DPP) {
508 		wpa_printf(MSG_INFO, "DPP: Unknown PMK length %u",
509 			   (unsigned int) pmk_len);
510 		return -1;
511 #endif /* CONFIG_DPP */
512 #ifdef CONFIG_SAE
513 	} else if (wpa_key_mgmt_sae_ext_key(akmp)) {
514 		if (pmk_len == 32) {
515 			wpa_printf(MSG_DEBUG,
516 				   "SAE: PTK derivation using PRF(SHA256)");
517 			if (sha256_prf(pmk, pmk_len, label, data, data_len,
518 				       tmp, ptk_len) < 0)
519 				return -1;
520 #ifdef CONFIG_SHA384
521 		} else if (pmk_len == 48) {
522 			wpa_printf(MSG_DEBUG,
523 				   "SAE: PTK derivation using PRF(SHA384)");
524 			if (sha384_prf(pmk, pmk_len, label, data, data_len,
525 				       tmp, ptk_len) < 0)
526 				return -1;
527 #endif /* CONFIG_SHA384 */
528 #ifdef CONFIG_SHA512
529 		} else if (pmk_len == 64) {
530 			wpa_printf(MSG_DEBUG,
531 				   "SAE: PTK derivation using PRF(SHA512)");
532 			if (sha512_prf(pmk, pmk_len, label, data, data_len,
533 				       tmp, ptk_len) < 0)
534 				return -1;
535 #endif /* CONFIG_SHA512 */
536 		} else {
537 			wpa_printf(MSG_INFO, "SAE: Unknown PMK length %u",
538 				   (unsigned int) pmk_len);
539 			return -1;
540 		}
541 #endif /* CONFIG_SAE */
542 	} else {
543 		wpa_printf(MSG_DEBUG, "WPA: PTK derivation using PRF(SHA1)");
544 		if (sha1_prf(pmk, pmk_len, label, data, data_len, tmp,
545 			     ptk_len) < 0)
546 			return -1;
547 	}
548 
549 	wpa_printf(MSG_EXCESSIVE, "WPA: PTK derivation - A1=" MACSTR_SEC " A2=" MACSTR_SEC,
550 		   MAC2STR_SEC(addr1), MAC2STR_SEC(addr2));
551 	wpa_hexdump(MSG_DEBUG, "WPA: Nonce1", nonce1, WPA_NONCE_LEN);
552 	wpa_hexdump(MSG_DEBUG, "WPA: Nonce2", nonce2, WPA_NONCE_LEN);
553 	if (z && z_len)
554 		wpa_hexdump_key(MSG_DEBUG, "WPA: Z.x", z, z_len);
555 	wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len);
556 	wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", tmp, ptk_len);
557 
558 	os_memcpy(ptk->kck, tmp, ptk->kck_len);
559 	wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", ptk->kck, ptk->kck_len);
560 
561 	os_memcpy(ptk->kek, tmp + ptk->kck_len, ptk->kek_len);
562 	wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
563 
564 	os_memcpy(ptk->tk, tmp + ptk->kck_len + ptk->kek_len, ptk->tk_len);
565 	wpa_hexdump_key(MSG_DEBUG, "WPA: TK", ptk->tk, ptk->tk_len);
566 
567 	if (kdk_len) {
568 		os_memcpy(ptk->kdk, tmp + ptk->kck_len + ptk->kek_len +
569 			  ptk->tk_len, ptk->kdk_len);
570 		wpa_hexdump_key(MSG_DEBUG, "WPA: KDK", ptk->kdk, ptk->kdk_len);
571 	}
572 
573 	ptk->kek2_len = 0;
574 	ptk->kck2_len = 0;
575 
576 	os_memset(tmp, 0, sizeof(tmp));
577 	os_memset(data, 0, data_len);
578 	return 0;
579 }
580 
581 #ifdef CONFIG_FILS
582 
fils_rmsk_to_pmk(int akmp,const u8 * rmsk,size_t rmsk_len,const u8 * snonce,const u8 * anonce,const u8 * dh_ss,size_t dh_ss_len,u8 * pmk,size_t * pmk_len)583 int fils_rmsk_to_pmk(int akmp, const u8 *rmsk, size_t rmsk_len,
584 		     const u8 *snonce, const u8 *anonce, const u8 *dh_ss,
585 		     size_t dh_ss_len, u8 *pmk, size_t *pmk_len)
586 {
587 	u8 nonces[2 * FILS_NONCE_LEN];
588 	const u8 *addr[2];
589 	size_t len[2];
590 	size_t num_elem;
591 	int res;
592 
593 	/* PMK = HMAC-Hash(SNonce || ANonce, rMSK [ || DHss ]) */
594 	wpa_printf(MSG_DEBUG, "FILS: rMSK to PMK derivation");
595 
596 	if (wpa_key_mgmt_sha384(akmp))
597 		*pmk_len = SHA384_MAC_LEN;
598 	else if (wpa_key_mgmt_sha256(akmp))
599 		*pmk_len = SHA256_MAC_LEN;
600 	else
601 		return -1;
602 
603 	wpa_hexdump_key(MSG_DEBUG, "FILS: rMSK", rmsk, rmsk_len);
604 	wpa_hexdump(MSG_DEBUG, "FILS: SNonce", snonce, FILS_NONCE_LEN);
605 	wpa_hexdump(MSG_DEBUG, "FILS: ANonce", anonce, FILS_NONCE_LEN);
606 	wpa_hexdump(MSG_DEBUG, "FILS: DHss", dh_ss, dh_ss_len);
607 
608 	os_memcpy(nonces, snonce, FILS_NONCE_LEN);
609 	os_memcpy(&nonces[FILS_NONCE_LEN], anonce, FILS_NONCE_LEN);
610 	addr[0] = rmsk;
611 	len[0] = rmsk_len;
612 	num_elem = 1;
613 	if (dh_ss) {
614 		addr[1] = dh_ss;
615 		len[1] = dh_ss_len;
616 		num_elem++;
617 	}
618 	if (wpa_key_mgmt_sha384(akmp))
619 		res = hmac_sha384_vector(nonces, 2 * FILS_NONCE_LEN, num_elem,
620 					 addr, len, pmk);
621 	else
622 		res = hmac_sha256_vector(nonces, 2 * FILS_NONCE_LEN, num_elem,
623 					 addr, len, pmk);
624 	if (res == 0)
625 		wpa_hexdump_key(MSG_DEBUG, "FILS: PMK", pmk, *pmk_len);
626 	else
627 		*pmk_len = 0;
628 	return res;
629 }
630 
631 
fils_pmkid_erp(int akmp,const u8 * reauth,size_t reauth_len,u8 * pmkid)632 int fils_pmkid_erp(int akmp, const u8 *reauth, size_t reauth_len,
633 		   u8 *pmkid)
634 {
635 	const u8 *addr[1];
636 	size_t len[1];
637 	u8 hash[SHA384_MAC_LEN];
638 	int res;
639 
640 	/* PMKID = Truncate-128(Hash(EAP-Initiate/Reauth)) */
641 	addr[0] = reauth;
642 	len[0] = reauth_len;
643 	if (wpa_key_mgmt_sha384(akmp))
644 		res = sha384_vector(1, addr, len, hash);
645 	else if (wpa_key_mgmt_sha256(akmp))
646 		res = sha256_vector(1, addr, len, hash);
647 	else
648 		return -1;
649 	if (res)
650 		return res;
651 	os_memcpy(pmkid, hash, PMKID_LEN);
652 	wpa_hexdump(MSG_DEBUG, "FILS: PMKID", pmkid, PMKID_LEN);
653 	return 0;
654 }
655 
656 
fils_pmk_to_ptk(const u8 * pmk,size_t pmk_len,const u8 * spa,const u8 * aa,const u8 * snonce,const u8 * anonce,const u8 * dhss,size_t dhss_len,struct wpa_ptk * ptk,u8 * ick,size_t * ick_len,int akmp,int cipher,u8 * fils_ft,size_t * fils_ft_len,size_t kdk_len)657 int fils_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const u8 *spa, const u8 *aa,
658 		    const u8 *snonce, const u8 *anonce, const u8 *dhss,
659 		    size_t dhss_len, struct wpa_ptk *ptk,
660 		    u8 *ick, size_t *ick_len, int akmp, int cipher,
661 		    u8 *fils_ft, size_t *fils_ft_len, size_t kdk_len)
662 {
663 	u8 *data, *pos;
664 	size_t data_len;
665 	u8 tmp[FILS_ICK_MAX_LEN + WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN +
666 	       FILS_FT_MAX_LEN + WPA_KDK_MAX_LEN];
667 	size_t key_data_len;
668 	const char *label = "FILS PTK Derivation";
669 	int ret = -1;
670 	size_t offset;
671 
672 	/*
673 	 * FILS-Key-Data = PRF-X(PMK, "FILS PTK Derivation",
674 	 *                       SPA || AA || SNonce || ANonce [ || DHss ])
675 	 * ICK = L(FILS-Key-Data, 0, ICK_bits)
676 	 * KEK = L(FILS-Key-Data, ICK_bits, KEK_bits)
677 	 * TK = L(FILS-Key-Data, ICK_bits + KEK_bits, TK_bits)
678 	 * If doing FT initial mobility domain association:
679 	 * FILS-FT = L(FILS-Key-Data, ICK_bits + KEK_bits + TK_bits,
680 	 *             FILS-FT_bits)
681 	 * When a KDK is derived:
682 	 * KDK = L(FILS-Key-Data, ICK_bits + KEK_bits + TK_bits + FILS-FT_bits,
683 	 *	   KDK_bits)
684 	 */
685 	data_len = 2 * ETH_ALEN + 2 * FILS_NONCE_LEN + dhss_len;
686 	data = os_malloc(data_len);
687 	if (!data)
688 		goto err;
689 	pos = data;
690 	os_memcpy(pos, spa, ETH_ALEN);
691 	pos += ETH_ALEN;
692 	os_memcpy(pos, aa, ETH_ALEN);
693 	pos += ETH_ALEN;
694 	os_memcpy(pos, snonce, FILS_NONCE_LEN);
695 	pos += FILS_NONCE_LEN;
696 	os_memcpy(pos, anonce, FILS_NONCE_LEN);
697 	pos += FILS_NONCE_LEN;
698 	if (dhss)
699 		os_memcpy(pos, dhss, dhss_len);
700 
701 	ptk->kck_len = 0;
702 	ptk->kek_len = wpa_kek_len(akmp, pmk_len);
703 	ptk->tk_len = wpa_cipher_key_len(cipher);
704 	if (wpa_key_mgmt_sha384(akmp))
705 		*ick_len = 48;
706 	else if (wpa_key_mgmt_sha256(akmp))
707 		*ick_len = 32;
708 	else
709 		goto err;
710 	key_data_len = *ick_len + ptk->kek_len + ptk->tk_len;
711 
712 	if (kdk_len) {
713 		if (kdk_len > WPA_KDK_MAX_LEN) {
714 			wpa_printf(MSG_ERROR, "FILS: KDK len=%zu too big",
715 				   kdk_len);
716 			goto err;
717 		}
718 
719 		ptk->kdk_len = kdk_len;
720 		key_data_len += kdk_len;
721 	} else {
722 		ptk->kdk_len = 0;
723 	}
724 
725 	if (fils_ft && fils_ft_len) {
726 		if (akmp == WPA_KEY_MGMT_FT_FILS_SHA256) {
727 			*fils_ft_len = 32;
728 		} else if (akmp == WPA_KEY_MGMT_FT_FILS_SHA384) {
729 			*fils_ft_len = 48;
730 		} else {
731 			*fils_ft_len = 0;
732 			fils_ft = NULL;
733 		}
734 		key_data_len += *fils_ft_len;
735 	}
736 
737 	if (wpa_key_mgmt_sha384(akmp)) {
738 		wpa_printf(MSG_DEBUG, "FILS: PTK derivation using PRF(SHA384)");
739 		if (sha384_prf(pmk, pmk_len, label, data, data_len,
740 			       tmp, key_data_len) < 0)
741 			goto err;
742 	} else {
743 		wpa_printf(MSG_DEBUG, "FILS: PTK derivation using PRF(SHA256)");
744 		if (sha256_prf(pmk, pmk_len, label, data, data_len,
745 			       tmp, key_data_len) < 0)
746 			goto err;
747 	}
748 
749 	wpa_printf(MSG_DEBUG, "FILS: PTK derivation - SPA=" MACSTR_SEC
750 		   " AA=" MACSTR_SEC, MAC2STR_SEC(spa), MAC2STR_SEC(aa));
751 	wpa_hexdump(MSG_DEBUG, "FILS: SNonce", snonce, FILS_NONCE_LEN);
752 	wpa_hexdump(MSG_DEBUG, "FILS: ANonce", anonce, FILS_NONCE_LEN);
753 	if (dhss)
754 		wpa_hexdump_key(MSG_DEBUG, "FILS: DHss", dhss, dhss_len);
755 	wpa_hexdump_key(MSG_DEBUG, "FILS: PMK", pmk, pmk_len);
756 	wpa_hexdump_key(MSG_DEBUG, "FILS: FILS-Key-Data", tmp, key_data_len);
757 
758 	os_memcpy(ick, tmp, *ick_len);
759 	offset = *ick_len;
760 	wpa_hexdump_key(MSG_DEBUG, "FILS: ICK", ick, *ick_len);
761 
762 	os_memcpy(ptk->kek, tmp + offset, ptk->kek_len);
763 	wpa_hexdump_key(MSG_DEBUG, "FILS: KEK", ptk->kek, ptk->kek_len);
764 	offset += ptk->kek_len;
765 
766 	os_memcpy(ptk->tk, tmp + offset, ptk->tk_len);
767 	wpa_hexdump_key(MSG_DEBUG, "FILS: TK", ptk->tk, ptk->tk_len);
768 	offset += ptk->tk_len;
769 
770 	if (fils_ft && fils_ft_len) {
771 		os_memcpy(fils_ft, tmp + offset, *fils_ft_len);
772 		wpa_hexdump_key(MSG_DEBUG, "FILS: FILS-FT",
773 				fils_ft, *fils_ft_len);
774 		offset += *fils_ft_len;
775 	}
776 
777 	if (ptk->kdk_len) {
778 		os_memcpy(ptk->kdk, tmp + offset, ptk->kdk_len);
779 		wpa_hexdump_key(MSG_DEBUG, "FILS: KDK", ptk->kdk, ptk->kdk_len);
780 	}
781 
782 	ptk->kek2_len = 0;
783 	ptk->kck2_len = 0;
784 
785 	os_memset(tmp, 0, sizeof(tmp));
786 	ret = 0;
787 err:
788 	bin_clear_free(data, data_len);
789 	return ret;
790 }
791 
792 
fils_key_auth_sk(const u8 * ick,size_t ick_len,const u8 * snonce,const u8 * anonce,const u8 * sta_addr,const u8 * bssid,const u8 * g_sta,size_t g_sta_len,const u8 * g_ap,size_t g_ap_len,int akmp,u8 * key_auth_sta,u8 * key_auth_ap,size_t * key_auth_len)793 int fils_key_auth_sk(const u8 *ick, size_t ick_len, const u8 *snonce,
794 		     const u8 *anonce, const u8 *sta_addr, const u8 *bssid,
795 		     const u8 *g_sta, size_t g_sta_len,
796 		     const u8 *g_ap, size_t g_ap_len,
797 		     int akmp, u8 *key_auth_sta, u8 *key_auth_ap,
798 		     size_t *key_auth_len)
799 {
800 	const u8 *addr[6];
801 	size_t len[6];
802 	size_t num_elem = 4;
803 	int res;
804 
805 	wpa_printf(MSG_DEBUG, "FILS: Key-Auth derivation: STA-MAC=" MACSTR_SEC
806 		   " AP-BSSID=" MACSTR_SEC, MAC2STR_SEC(sta_addr), MAC2STR_SEC(bssid));
807 	wpa_hexdump_key(MSG_DEBUG, "FILS: ICK", ick, ick_len);
808 	wpa_hexdump(MSG_DEBUG, "FILS: SNonce", snonce, FILS_NONCE_LEN);
809 	wpa_hexdump(MSG_DEBUG, "FILS: ANonce", anonce, FILS_NONCE_LEN);
810 	wpa_hexdump(MSG_DEBUG, "FILS: gSTA", g_sta, g_sta_len);
811 	wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
812 
813 	/*
814 	 * For (Re)Association Request frame (STA->AP):
815 	 * Key-Auth = HMAC-Hash(ICK, SNonce || ANonce || STA-MAC || AP-BSSID
816 	 *                      [ || gSTA || gAP ])
817 	 */
818 	addr[0] = snonce;
819 	len[0] = FILS_NONCE_LEN;
820 	addr[1] = anonce;
821 	len[1] = FILS_NONCE_LEN;
822 	addr[2] = sta_addr;
823 	len[2] = ETH_ALEN;
824 	addr[3] = bssid;
825 	len[3] = ETH_ALEN;
826 	if (g_sta && g_sta_len && g_ap && g_ap_len) {
827 		addr[4] = g_sta;
828 		len[4] = g_sta_len;
829 		addr[5] = g_ap;
830 		len[5] = g_ap_len;
831 		num_elem = 6;
832 	}
833 
834 	if (wpa_key_mgmt_sha384(akmp)) {
835 		*key_auth_len = 48;
836 		res = hmac_sha384_vector(ick, ick_len, num_elem, addr, len,
837 					 key_auth_sta);
838 	} else if (wpa_key_mgmt_sha256(akmp)) {
839 		*key_auth_len = 32;
840 		res = hmac_sha256_vector(ick, ick_len, num_elem, addr, len,
841 					 key_auth_sta);
842 	} else {
843 		return -1;
844 	}
845 	if (res < 0)
846 		return res;
847 
848 	/*
849 	 * For (Re)Association Response frame (AP->STA):
850 	 * Key-Auth = HMAC-Hash(ICK, ANonce || SNonce || AP-BSSID || STA-MAC
851 	 *                      [ || gAP || gSTA ])
852 	 */
853 	addr[0] = anonce;
854 	addr[1] = snonce;
855 	addr[2] = bssid;
856 	addr[3] = sta_addr;
857 	if (g_sta && g_sta_len && g_ap && g_ap_len) {
858 		addr[4] = g_ap;
859 		len[4] = g_ap_len;
860 		addr[5] = g_sta;
861 		len[5] = g_sta_len;
862 	}
863 
864 	if (wpa_key_mgmt_sha384(akmp))
865 		res = hmac_sha384_vector(ick, ick_len, num_elem, addr, len,
866 					 key_auth_ap);
867 	else if (wpa_key_mgmt_sha256(akmp))
868 		res = hmac_sha256_vector(ick, ick_len, num_elem, addr, len,
869 					 key_auth_ap);
870 	if (res < 0)
871 		return res;
872 
873 	wpa_hexdump(MSG_DEBUG, "FILS: Key-Auth (STA)",
874 		    key_auth_sta, *key_auth_len);
875 	wpa_hexdump(MSG_DEBUG, "FILS: Key-Auth (AP)",
876 		    key_auth_ap, *key_auth_len);
877 
878 	return 0;
879 }
880 
881 #endif /* CONFIG_FILS */
882 
883 
884 #ifdef CONFIG_IEEE80211R
wpa_ft_mic(const u8 * kck,size_t kck_len,const u8 * sta_addr,const u8 * ap_addr,u8 transaction_seqnum,const u8 * mdie,size_t mdie_len,const u8 * ftie,size_t ftie_len,const u8 * rsnie,size_t rsnie_len,const u8 * ric,size_t ric_len,const u8 * rsnxe,size_t rsnxe_len,u8 * mic)885 int wpa_ft_mic(const u8 *kck, size_t kck_len, const u8 *sta_addr,
886 	       const u8 *ap_addr, u8 transaction_seqnum,
887 	       const u8 *mdie, size_t mdie_len,
888 	       const u8 *ftie, size_t ftie_len,
889 	       const u8 *rsnie, size_t rsnie_len,
890 	       const u8 *ric, size_t ric_len,
891 	       const u8 *rsnxe, size_t rsnxe_len,
892 	       u8 *mic)
893 {
894 	const u8 *addr[10];
895 	size_t len[10];
896 	size_t i, num_elem = 0;
897 	u8 zero_mic[24];
898 	size_t mic_len, fte_fixed_len;
899 
900 	if (kck_len == 16) {
901 		mic_len = 16;
902 #ifdef CONFIG_SHA384
903 	} else if (kck_len == 24) {
904 		mic_len = 24;
905 #endif /* CONFIG_SHA384 */
906 	} else {
907 		wpa_printf(MSG_WARNING, "FT: Unsupported KCK length %u",
908 			   (unsigned int) kck_len);
909 		return -1;
910 	}
911 
912 	fte_fixed_len = sizeof(struct rsn_ftie) - 16 + mic_len;
913 
914 	addr[num_elem] = sta_addr;
915 	len[num_elem] = ETH_ALEN;
916 	num_elem++;
917 
918 	addr[num_elem] = ap_addr;
919 	len[num_elem] = ETH_ALEN;
920 	num_elem++;
921 
922 	addr[num_elem] = &transaction_seqnum;
923 	len[num_elem] = 1;
924 	num_elem++;
925 
926 	if (rsnie) {
927 		addr[num_elem] = rsnie;
928 		len[num_elem] = rsnie_len;
929 		num_elem++;
930 	}
931 	if (mdie) {
932 		addr[num_elem] = mdie;
933 		len[num_elem] = mdie_len;
934 		num_elem++;
935 	}
936 	if (ftie) {
937 		if (ftie_len < 2 + fte_fixed_len)
938 			return -1;
939 
940 		/* IE hdr and mic_control */
941 		addr[num_elem] = ftie;
942 		len[num_elem] = 2 + 2;
943 		num_elem++;
944 
945 		/* MIC field with all zeros */
946 		os_memset(zero_mic, 0, mic_len);
947 		addr[num_elem] = zero_mic;
948 		len[num_elem] = mic_len;
949 		num_elem++;
950 
951 		/* Rest of FTIE */
952 		addr[num_elem] = ftie + 2 + 2 + mic_len;
953 		len[num_elem] = ftie_len - (2 + 2 + mic_len);
954 		num_elem++;
955 	}
956 	if (ric) {
957 		addr[num_elem] = ric;
958 		len[num_elem] = ric_len;
959 		num_elem++;
960 	}
961 
962 	if (rsnxe) {
963 		addr[num_elem] = rsnxe;
964 		len[num_elem] = rsnxe_len;
965 		num_elem++;
966 	}
967 
968 	for (i = 0; i < num_elem; i++)
969 		wpa_hexdump(MSG_MSGDUMP, "FT: MIC data", addr[i], len[i]);
970 #ifdef CONFIG_SHA384
971 	if (kck_len == 24) {
972 		u8 hash[SHA384_MAC_LEN];
973 
974 		if (hmac_sha384_vector(kck, kck_len, num_elem, addr, len, hash))
975 			return -1;
976 		os_memcpy(mic, hash, 24);
977 	}
978 #endif /* CONFIG_SHA384 */
979 	if (kck_len == 16 &&
980 	    omac1_aes_128_vector(kck, num_elem, addr, len, mic))
981 		return -1;
982 
983 	return 0;
984 }
985 
986 
wpa_ft_parse_ftie(const u8 * ie,size_t ie_len,struct wpa_ft_ies * parse,int use_sha384)987 static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
988 			     struct wpa_ft_ies *parse, int use_sha384)
989 {
990 	const u8 *end, *pos;
991 
992 	parse->ftie = ie;
993 	parse->ftie_len = ie_len;
994 
995 	pos = ie + (use_sha384 ? sizeof(struct rsn_ftie_sha384) :
996 		    sizeof(struct rsn_ftie));
997 	end = ie + ie_len;
998 	wpa_hexdump(MSG_DEBUG, "FT: Parse FTE subelements", pos, end - pos);
999 
1000 	while (end - pos >= 2) {
1001 		u8 id, len;
1002 
1003 		id = *pos++;
1004 		len = *pos++;
1005 		if (len > end - pos) {
1006 			wpa_printf(MSG_DEBUG, "FT: Truncated subelement");
1007 			break;
1008 		}
1009 
1010 		switch (id) {
1011 		case FTIE_SUBELEM_R1KH_ID:
1012 			if (len != FT_R1KH_ID_LEN) {
1013 				wpa_printf(MSG_DEBUG,
1014 					   "FT: Invalid R1KH-ID length in FTIE: %d",
1015 					   len);
1016 				return -1;
1017 			}
1018 			parse->r1kh_id = pos;
1019 			break;
1020 		case FTIE_SUBELEM_GTK:
1021 			parse->gtk = pos;
1022 			parse->gtk_len = len;
1023 			break;
1024 		case FTIE_SUBELEM_R0KH_ID:
1025 			if (len < 1 || len > FT_R0KH_ID_MAX_LEN) {
1026 				wpa_printf(MSG_DEBUG,
1027 					   "FT: Invalid R0KH-ID length in FTIE: %d",
1028 					   len);
1029 				return -1;
1030 			}
1031 			parse->r0kh_id = pos;
1032 			parse->r0kh_id_len = len;
1033 			break;
1034 		case FTIE_SUBELEM_IGTK:
1035 			parse->igtk = pos;
1036 			parse->igtk_len = len;
1037 			break;
1038 #ifdef CONFIG_OCV
1039 		case FTIE_SUBELEM_OCI:
1040 			parse->oci = pos;
1041 			parse->oci_len = len;
1042 			break;
1043 #endif /* CONFIG_OCV */
1044 		case FTIE_SUBELEM_BIGTK:
1045 			parse->bigtk = pos;
1046 			parse->bigtk_len = len;
1047 			break;
1048 		default:
1049 			wpa_printf(MSG_DEBUG, "FT: Unknown subelem id %u", id);
1050 			break;
1051 		}
1052 
1053 		pos += len;
1054 	}
1055 
1056 	return 0;
1057 }
1058 
1059 
wpa_ft_parse_ies(const u8 * ies,size_t ies_len,struct wpa_ft_ies * parse,int use_sha384)1060 int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
1061 		     struct wpa_ft_ies *parse, int use_sha384)
1062 {
1063 	const u8 *end, *pos;
1064 	struct wpa_ie_data data;
1065 	int ret;
1066 	const struct rsn_ftie *ftie;
1067 	int prot_ie_count = 0;
1068 	int update_use_sha384 = 0;
1069 
1070 	if (use_sha384 < 0) {
1071 		use_sha384 = 0;
1072 		update_use_sha384 = 1;
1073 	}
1074 
1075 	os_memset(parse, 0, sizeof(*parse));
1076 	if (ies == NULL)
1077 		return 0;
1078 
1079 	pos = ies;
1080 	end = ies + ies_len;
1081 	while (end - pos >= 2) {
1082 		u8 id, len;
1083 
1084 		id = *pos++;
1085 		len = *pos++;
1086 		if (len > end - pos)
1087 			break;
1088 
1089 		switch (id) {
1090 		case WLAN_EID_RSN:
1091 			wpa_hexdump(MSG_DEBUG, "FT: RSNE", pos, len);
1092 			parse->rsn = pos;
1093 			parse->rsn_len = len;
1094 			ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
1095 						   parse->rsn_len + 2,
1096 						   &data);
1097 			if (ret < 0) {
1098 				wpa_printf(MSG_DEBUG, "FT: Failed to parse "
1099 					   "RSN IE: %d", ret);
1100 				return -1;
1101 			}
1102 			parse->rsn_capab = data.capabilities;
1103 			if (data.num_pmkid == 1 && data.pmkid)
1104 				parse->rsn_pmkid = data.pmkid;
1105 			parse->key_mgmt = data.key_mgmt;
1106 			parse->pairwise_cipher = data.pairwise_cipher;
1107 			if (update_use_sha384) {
1108 				use_sha384 =
1109 					wpa_key_mgmt_sha384(parse->key_mgmt);
1110 				update_use_sha384 = 0;
1111 			}
1112 			break;
1113 		case WLAN_EID_RSNX:
1114 			wpa_hexdump(MSG_DEBUG, "FT: RSNXE", pos, len);
1115 			if (len < 1)
1116 				break;
1117 			parse->rsnxe = pos;
1118 			parse->rsnxe_len = len;
1119 			break;
1120 		case WLAN_EID_MOBILITY_DOMAIN:
1121 			wpa_hexdump(MSG_DEBUG, "FT: MDE", pos, len);
1122 			if (len < sizeof(struct rsn_mdie))
1123 				return -1;
1124 			parse->mdie = pos;
1125 			parse->mdie_len = len;
1126 			break;
1127 		case WLAN_EID_FAST_BSS_TRANSITION:
1128 			wpa_hexdump(MSG_DEBUG, "FT: FTE", pos, len);
1129 			if (use_sha384) {
1130 				const struct rsn_ftie_sha384 *ftie_sha384;
1131 
1132 				if (len < sizeof(*ftie_sha384))
1133 					return -1;
1134 				ftie_sha384 =
1135 					(const struct rsn_ftie_sha384 *) pos;
1136 				wpa_hexdump(MSG_DEBUG, "FT: FTE-MIC Control",
1137 					    ftie_sha384->mic_control, 2);
1138 				wpa_hexdump(MSG_DEBUG, "FT: FTE-MIC",
1139 					    ftie_sha384->mic,
1140 					    sizeof(ftie_sha384->mic));
1141 				parse->fte_anonce = ftie_sha384->anonce;
1142 				wpa_hexdump(MSG_DEBUG, "FT: FTE-ANonce",
1143 					    ftie_sha384->anonce,
1144 					    WPA_NONCE_LEN);
1145 				parse->fte_snonce = ftie_sha384->snonce;
1146 				wpa_hexdump(MSG_DEBUG, "FT: FTE-SNonce",
1147 					    ftie_sha384->snonce,
1148 					    WPA_NONCE_LEN);
1149 				prot_ie_count = ftie_sha384->mic_control[1];
1150 				if (wpa_ft_parse_ftie(pos, len, parse, 1) < 0)
1151 					return -1;
1152 				break;
1153 			}
1154 
1155 			if (len < sizeof(*ftie))
1156 				return -1;
1157 			ftie = (const struct rsn_ftie *) pos;
1158 			wpa_hexdump(MSG_DEBUG, "FT: FTE-MIC Control",
1159 				    ftie->mic_control, 2);
1160 			wpa_hexdump(MSG_DEBUG, "FT: FTE-MIC",
1161 				    ftie->mic, sizeof(ftie->mic));
1162 			parse->fte_anonce = ftie->anonce;
1163 			wpa_hexdump(MSG_DEBUG, "FT: FTE-ANonce",
1164 				    ftie->anonce, WPA_NONCE_LEN);
1165 			parse->fte_snonce = ftie->snonce;
1166 			wpa_hexdump(MSG_DEBUG, "FT: FTE-SNonce",
1167 				    ftie->snonce, WPA_NONCE_LEN);
1168 			prot_ie_count = ftie->mic_control[1];
1169 			if (wpa_ft_parse_ftie(pos, len, parse, 0) < 0)
1170 				return -1;
1171 			break;
1172 		case WLAN_EID_TIMEOUT_INTERVAL:
1173 			wpa_hexdump(MSG_DEBUG, "FT: Timeout Interval",
1174 				    pos, len);
1175 			if (len != 5)
1176 				break;
1177 			parse->tie = pos;
1178 			parse->tie_len = len;
1179 			break;
1180 		case WLAN_EID_RIC_DATA:
1181 			if (parse->ric == NULL)
1182 				parse->ric = pos - 2;
1183 			break;
1184 		}
1185 
1186 		pos += len;
1187 	}
1188 
1189 	if (prot_ie_count == 0)
1190 		return 0; /* no MIC */
1191 
1192 	/*
1193 	 * Check that the protected IE count matches with IEs included in the
1194 	 * frame.
1195 	 */
1196 	if (parse->rsn)
1197 		prot_ie_count--;
1198 	if (parse->mdie)
1199 		prot_ie_count--;
1200 	if (parse->ftie)
1201 		prot_ie_count--;
1202 	if (parse->rsnxe)
1203 		prot_ie_count--;
1204 	if (prot_ie_count < 0) {
1205 		wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in "
1206 			   "the protected IE count");
1207 		return -1;
1208 	}
1209 
1210 	if (prot_ie_count == 0 && parse->ric) {
1211 		wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not "
1212 			   "included in protected IE count");
1213 		return -1;
1214 	}
1215 
1216 	/* Determine the end of the RIC IE(s) */
1217 	if (parse->ric) {
1218 		pos = parse->ric;
1219 		while (end - pos >= 2 && 2 + pos[1] <= end - pos &&
1220 		       prot_ie_count) {
1221 			prot_ie_count--;
1222 			pos += 2 + pos[1];
1223 		}
1224 		parse->ric_len = pos - parse->ric;
1225 	}
1226 	if (prot_ie_count) {
1227 		wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from "
1228 			   "frame", (int) prot_ie_count);
1229 		return -1;
1230 	}
1231 
1232 	return 0;
1233 }
1234 #endif /* CONFIG_IEEE80211R */
1235 
1236 
1237 #ifdef CONFIG_PASN
1238 
1239 /*
1240  * pasn_use_sha384 - Should SHA384 be used or SHA256
1241  *
1242  * @akmp: Authentication and key management protocol
1243  * @cipher: The cipher suite
1244  *
1245  * According to IEEE P802.11az/D2.7, 12.12.7, the hash algorithm to use is the
1246  * hash algorithm defined for the Base AKM (see Table 9-151 (AKM suite
1247  * selectors)). When there is no Base AKM, the hash algorithm is selected based
1248  * on the pairwise cipher suite provided in the RSNE by the AP in the second
1249  * PASN frame. SHA-256 is used as the hash algorithm, except for the ciphers
1250  * 00-0F-AC:9 and 00-0F-AC:10 for which SHA-384 is used.
1251  */
pasn_use_sha384(int akmp,int cipher)1252 static bool pasn_use_sha384(int akmp, int cipher)
1253 {
1254 	return (akmp == WPA_KEY_MGMT_PASN && (cipher == WPA_CIPHER_CCMP_256 ||
1255 					      cipher == WPA_CIPHER_GCMP_256)) ||
1256 		wpa_key_mgmt_sha384(akmp);
1257 }
1258 
1259 
1260 /**
1261  * pasn_pmk_to_ptk - Calculate PASN PTK from PMK, addresses, etc.
1262  * @pmk: Pairwise master key
1263  * @pmk_len: Length of PMK
1264  * @spa: Suppplicant address
1265  * @bssid: AP BSSID
1266  * @dhss: Is the shared secret (DHss) derived from the PASN ephemeral key
1267  *	exchange encoded as an octet string
1268  * @dhss_len: The length of dhss in octets
1269  * @ptk: Buffer for pairwise transient key
1270  * @akmp: Negotiated AKM
1271  * @cipher: Negotiated pairwise cipher
1272  * @kdk_len: the length in octets that should be derived for HTLK. Can be zero.
1273  * Returns: 0 on success, -1 on failure
1274  */
pasn_pmk_to_ptk(const u8 * pmk,size_t pmk_len,const u8 * spa,const u8 * bssid,const u8 * dhss,size_t dhss_len,struct wpa_ptk * ptk,int akmp,int cipher,size_t kdk_len)1275 int pasn_pmk_to_ptk(const u8 *pmk, size_t pmk_len,
1276 		    const u8 *spa, const u8 *bssid,
1277 		    const u8 *dhss, size_t dhss_len,
1278 		    struct wpa_ptk *ptk, int akmp, int cipher,
1279 		    size_t kdk_len)
1280 {
1281 	u8 tmp[WPA_KCK_MAX_LEN + WPA_TK_MAX_LEN + WPA_KDK_MAX_LEN];
1282 	u8 *data;
1283 	size_t data_len, ptk_len;
1284 	int ret = -1;
1285 	const char *label = "PASN PTK Derivation";
1286 
1287 	if (!pmk || !pmk_len) {
1288 		wpa_printf(MSG_ERROR, "PASN: No PMK set for PTK derivation");
1289 		return -1;
1290 	}
1291 
1292 	if (!dhss || !dhss_len) {
1293 		wpa_printf(MSG_ERROR, "PASN: No DHss set for PTK derivation");
1294 		return -1;
1295 	}
1296 
1297 	/*
1298 	 * PASN-PTK = KDF(PMK, “PASN PTK Derivation”, SPA || BSSID || DHss)
1299 	 *
1300 	 * KCK = L(PASN-PTK, 0, 256)
1301 	 * TK = L(PASN-PTK, 256, TK_bits)
1302 	 * KDK = L(PASN-PTK, 256 + TK_bits, kdk_len * 8)
1303 	 */
1304 	data_len = 2 * ETH_ALEN + dhss_len;
1305 	data = os_zalloc(data_len);
1306 	if (!data)
1307 		return -1;
1308 
1309 	os_memcpy(data, spa, ETH_ALEN);
1310 	os_memcpy(data + ETH_ALEN, bssid, ETH_ALEN);
1311 	os_memcpy(data + 2 * ETH_ALEN, dhss, dhss_len);
1312 
1313 	ptk->kck_len = WPA_PASN_KCK_LEN;
1314 	ptk->tk_len = wpa_cipher_key_len(cipher);
1315 	ptk->kdk_len = kdk_len;
1316 	ptk->kek_len = 0;
1317 	ptk->kek2_len = 0;
1318 	ptk->kck2_len = 0;
1319 
1320 	if (ptk->tk_len == 0) {
1321 		wpa_printf(MSG_ERROR,
1322 			   "PASN: Unsupported cipher (0x%x) used in PTK derivation",
1323 			   cipher);
1324 		goto err;
1325 	}
1326 
1327 	ptk_len = ptk->kck_len + ptk->tk_len + ptk->kdk_len;
1328 	if (ptk_len > sizeof(tmp))
1329 		goto err;
1330 
1331 	if (pasn_use_sha384(akmp, cipher)) {
1332 		wpa_printf(MSG_DEBUG, "PASN: PTK derivation using SHA384");
1333 
1334 		if (sha384_prf(pmk, pmk_len, label, data, data_len, tmp,
1335 			       ptk_len) < 0)
1336 			goto err;
1337 	} else {
1338 		wpa_printf(MSG_DEBUG, "PASN: PTK derivation using SHA256");
1339 
1340 		if (sha256_prf(pmk, pmk_len, label, data, data_len, tmp,
1341 			       ptk_len) < 0)
1342 			goto err;
1343 	}
1344 
1345 	wpa_printf(MSG_DEBUG,
1346 		   "PASN: PTK derivation: SPA=" MACSTR_SEC " BSSID=" MACSTR_SEC,
1347 		   MAC2STR_SEC(spa), MAC2STR_SEC(bssid));
1348 
1349 	wpa_hexdump_key(MSG_DEBUG, "PASN: DHss", dhss, dhss_len);
1350 	wpa_hexdump_key(MSG_DEBUG, "PASN: PMK", pmk, pmk_len);
1351 	wpa_hexdump_key(MSG_DEBUG, "PASN: PASN-PTK", tmp, ptk_len);
1352 
1353 	os_memcpy(ptk->kck, tmp, WPA_PASN_KCK_LEN);
1354 	wpa_hexdump_key(MSG_DEBUG, "PASN: KCK:", ptk->kck, WPA_PASN_KCK_LEN);
1355 
1356 	os_memcpy(ptk->tk, tmp + WPA_PASN_KCK_LEN, ptk->tk_len);
1357 	wpa_hexdump_key(MSG_DEBUG, "PASN: TK:", ptk->tk, ptk->tk_len);
1358 
1359 	if (kdk_len) {
1360 		os_memcpy(ptk->kdk, tmp + WPA_PASN_KCK_LEN + ptk->tk_len,
1361 			  ptk->kdk_len);
1362 		wpa_hexdump_key(MSG_DEBUG, "PASN: KDK:",
1363 				ptk->kdk, ptk->kdk_len);
1364 	}
1365 
1366 	forced_memzero(tmp, sizeof(tmp));
1367 	ret = 0;
1368 err:
1369 	bin_clear_free(data, data_len);
1370 	return ret;
1371 }
1372 
1373 
1374 /*
1375  * pasn_mic_len - Returns the MIC length for PASN authentication
1376  */
pasn_mic_len(int akmp,int cipher)1377 u8 pasn_mic_len(int akmp, int cipher)
1378 {
1379 	if (pasn_use_sha384(akmp, cipher))
1380 		return 24;
1381 
1382 	return 16;
1383 }
1384 
1385 
1386 /**
1387  * pasn_mic - Calculate PASN MIC
1388  * @kck: The key confirmation key for the PASN PTKSA
1389  * @akmp: Negotiated AKM
1390  * @cipher: Negotiated pairwise cipher
1391  * @addr1: For the 2nd PASN frame supplicant address; for the 3rd frame the
1392  *	BSSID
1393  * @addr2: For the 2nd PASN frame the BSSID; for the 3rd frame the supplicant
1394  *	address
1395  * @data: For calculating the MIC for the 2nd PASN frame, this should hold the
1396  *	Beacon frame RSNE + RSNXE. For calculating the MIC for the 3rd PASN
1397  *	frame, this should hold the hash of the body of the PASN 1st frame.
1398  * @data_len: The length of data
1399  * @frame: The body of the PASN frame including the MIC element with the octets
1400  *	in the MIC field of the MIC element set to 0.
1401  * @frame_len: The length of frame
1402  * @mic: Buffer to hold the MIC on success. Should be big enough to handle the
1403  *	maximal MIC length
1404  * Returns: 0 on success, -1 on failure
1405  */
pasn_mic(const u8 * kck,int akmp,int cipher,const u8 * addr1,const u8 * addr2,const u8 * data,size_t data_len,const u8 * frame,size_t frame_len,u8 * mic)1406 int pasn_mic(const u8 *kck, int akmp, int cipher,
1407 	     const u8 *addr1, const u8 *addr2,
1408 	     const u8 *data, size_t data_len,
1409 	     const u8 *frame, size_t frame_len, u8 *mic)
1410 {
1411 	u8 *buf;
1412 	u8 hash[SHA384_MAC_LEN];
1413 	size_t buf_len = 2 * ETH_ALEN + data_len + frame_len;
1414 	int ret = -1;
1415 
1416 	if (!kck) {
1417 		wpa_printf(MSG_ERROR, "PASN: No KCK for MIC calculation");
1418 		return -1;
1419 	}
1420 
1421 	if (!data || !data_len) {
1422 		wpa_printf(MSG_ERROR, "PASN: invalid data for MIC calculation");
1423 		return -1;
1424 	}
1425 
1426 	if (!frame || !frame_len) {
1427 		wpa_printf(MSG_ERROR, "PASN: invalid data for MIC calculation");
1428 		return -1;
1429 	}
1430 
1431 	buf = os_zalloc(buf_len);
1432 	if (!buf)
1433 		return -1;
1434 
1435 	os_memcpy(buf, addr1, ETH_ALEN);
1436 	os_memcpy(buf + ETH_ALEN, addr2, ETH_ALEN);
1437 
1438 	wpa_hexdump_key(MSG_DEBUG, "PASN: MIC: data", data, data_len);
1439 	os_memcpy(buf + 2 * ETH_ALEN, data, data_len);
1440 
1441 	wpa_hexdump_key(MSG_DEBUG, "PASN: MIC: frame", frame, frame_len);
1442 	os_memcpy(buf + 2 * ETH_ALEN + data_len, frame, frame_len);
1443 
1444 	wpa_hexdump_key(MSG_DEBUG, "PASN: MIC: KCK", kck, WPA_PASN_KCK_LEN);
1445 	wpa_hexdump_key(MSG_DEBUG, "PASN: MIC: buf", buf, buf_len);
1446 
1447 	if (pasn_use_sha384(akmp, cipher)) {
1448 		wpa_printf(MSG_DEBUG, "PASN: MIC using HMAC-SHA384");
1449 
1450 		if (hmac_sha384(kck, WPA_PASN_KCK_LEN, buf, buf_len, hash))
1451 			goto err;
1452 
1453 		os_memcpy(mic, hash, 24);
1454 		wpa_hexdump_key(MSG_DEBUG, "PASN: MIC: mic: ", mic, 24);
1455 	} else {
1456 		wpa_printf(MSG_DEBUG, "PASN: MIC using HMAC-SHA256");
1457 
1458 		if (hmac_sha256(kck, WPA_PASN_KCK_LEN, buf, buf_len, hash))
1459 			goto err;
1460 
1461 		os_memcpy(mic, hash, 16);
1462 		wpa_hexdump_key(MSG_DEBUG, "PASN: MIC: mic: ", mic, 16);
1463 	}
1464 
1465 	ret = 0;
1466 err:
1467 	bin_clear_free(buf, buf_len);
1468 	return ret;
1469 }
1470 
1471 
1472 /**
1473  * pasn_auth_frame_hash - Computes a hash of an Authentication frame body
1474  * @akmp: Negotiated AKM
1475  * @cipher: Negotiated pairwise cipher
1476  * @data: Pointer to the Authentication frame body
1477  * @len: Length of the Authentication frame body
1478  * @hash: On return would hold the computed hash. Should be big enough to handle
1479  *	SHA384.
1480  * Returns: 0 on success, -1 on failure
1481  */
pasn_auth_frame_hash(int akmp,int cipher,const u8 * data,size_t len,u8 * hash)1482 int pasn_auth_frame_hash(int akmp, int cipher, const u8 *data, size_t len,
1483 			 u8 *hash)
1484 {
1485 	if (pasn_use_sha384(akmp, cipher)) {
1486 		wpa_printf(MSG_DEBUG, "PASN: Frame hash using SHA-384");
1487 		return sha384_vector(1, &data, &len, hash);
1488 	} else {
1489 		wpa_printf(MSG_DEBUG, "PASN: Frame hash using SHA-256");
1490 		return sha256_vector(1, &data, &len, hash);
1491 	}
1492 }
1493 
1494 #endif /* CONFIG_PASN */
1495 
1496 
rsn_selector_to_bitfield(const u8 * s)1497 static int rsn_selector_to_bitfield(const u8 *s)
1498 {
1499 	if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NONE)
1500 		return WPA_CIPHER_NONE;
1501 	if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_TKIP)
1502 		return WPA_CIPHER_TKIP;
1503 	if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP)
1504 		return WPA_CIPHER_CCMP;
1505 	if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_AES_128_CMAC)
1506 		return WPA_CIPHER_AES_128_CMAC;
1507 	if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP)
1508 		return WPA_CIPHER_GCMP;
1509 	if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP_256)
1510 		return WPA_CIPHER_CCMP_256;
1511 	if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP_256)
1512 		return WPA_CIPHER_GCMP_256;
1513 	if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_128)
1514 		return WPA_CIPHER_BIP_GMAC_128;
1515 	if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_256)
1516 		return WPA_CIPHER_BIP_GMAC_256;
1517 	if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_CMAC_256)
1518 		return WPA_CIPHER_BIP_CMAC_256;
1519 	if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED)
1520 		return WPA_CIPHER_GTK_NOT_USED;
1521 	return 0;
1522 }
1523 
1524 
rsn_key_mgmt_to_bitfield(const u8 * s)1525 static int rsn_key_mgmt_to_bitfield(const u8 *s)
1526 {
1527 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_UNSPEC_802_1X)
1528 		return WPA_KEY_MGMT_IEEE8021X;
1529 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X)
1530 		return WPA_KEY_MGMT_PSK;
1531 #ifdef CONFIG_IEEE80211R
1532 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X)
1533 		return WPA_KEY_MGMT_FT_IEEE8021X;
1534 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_PSK)
1535 		return WPA_KEY_MGMT_FT_PSK;
1536 #ifdef CONFIG_SHA384
1537 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384)
1538 		return WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
1539 #endif /* CONFIG_SHA384 */
1540 #endif /* CONFIG_IEEE80211R */
1541 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SHA256)
1542 		return WPA_KEY_MGMT_IEEE8021X_SHA256;
1543 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_SHA256)
1544 		return WPA_KEY_MGMT_PSK_SHA256;
1545 #ifdef CONFIG_SAE
1546 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_SAE)
1547 		return WPA_KEY_MGMT_SAE;
1548 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_SAE_EXT_KEY)
1549 		return WPA_KEY_MGMT_SAE_EXT_KEY;
1550 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_SAE)
1551 		return WPA_KEY_MGMT_FT_SAE;
1552 #endif /* CONFIG_SAE */
1553 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SUITE_B)
1554 		return WPA_KEY_MGMT_IEEE8021X_SUITE_B;
1555 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192)
1556 		return WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
1557 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FILS_SHA256)
1558 		return WPA_KEY_MGMT_FILS_SHA256;
1559 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FILS_SHA384)
1560 		return WPA_KEY_MGMT_FILS_SHA384;
1561 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_FILS_SHA256)
1562 		return WPA_KEY_MGMT_FT_FILS_SHA256;
1563 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_FILS_SHA384)
1564 		return WPA_KEY_MGMT_FT_FILS_SHA384;
1565 #ifdef CONFIG_OWE
1566 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_OWE)
1567 		return WPA_KEY_MGMT_OWE;
1568 #endif /* CONFIG_OWE */
1569 #ifdef CONFIG_DPP
1570 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_DPP)
1571 		return WPA_KEY_MGMT_DPP;
1572 #endif /* CONFIG_DPP */
1573 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_OSEN)
1574 		return WPA_KEY_MGMT_OSEN;
1575 #ifdef CONFIG_PASN
1576 	if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PASN)
1577 		return WPA_KEY_MGMT_PASN;
1578 #endif /* CONFIG_PASN */
1579 	return 0;
1580 }
1581 
1582 
wpa_cipher_valid_group(int cipher)1583 int wpa_cipher_valid_group(int cipher)
1584 {
1585 	return wpa_cipher_valid_pairwise(cipher) ||
1586 		cipher == WPA_CIPHER_GTK_NOT_USED;
1587 }
1588 
1589 
wpa_cipher_valid_mgmt_group(int cipher)1590 int wpa_cipher_valid_mgmt_group(int cipher)
1591 {
1592 	return cipher == WPA_CIPHER_GTK_NOT_USED ||
1593 		cipher == WPA_CIPHER_AES_128_CMAC ||
1594 		cipher == WPA_CIPHER_BIP_GMAC_128 ||
1595 		cipher == WPA_CIPHER_BIP_GMAC_256 ||
1596 		cipher == WPA_CIPHER_BIP_CMAC_256;
1597 }
1598 
1599 
1600 /**
1601  * wpa_parse_wpa_ie_rsn - Parse RSN IE
1602  * @rsn_ie: Buffer containing RSN IE
1603  * @rsn_ie_len: RSN IE buffer length (including IE number and length octets)
1604  * @data: Pointer to structure that will be filled in with parsed data
1605  * Returns: 0 on success, <0 on failure
1606  */
wpa_parse_wpa_ie_rsn(const u8 * rsn_ie,size_t rsn_ie_len,struct wpa_ie_data * data)1607 int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len,
1608 			 struct wpa_ie_data *data)
1609 {
1610 	const u8 *pos;
1611 	int left;
1612 	int i, count;
1613 
1614 	os_memset(data, 0, sizeof(*data));
1615 	data->proto = WPA_PROTO_RSN;
1616 	data->pairwise_cipher = WPA_CIPHER_CCMP;
1617 	data->group_cipher = WPA_CIPHER_CCMP;
1618 	data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1619 	data->capabilities = 0;
1620 	data->pmkid = NULL;
1621 	data->num_pmkid = 0;
1622 	data->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1623 
1624 	if (rsn_ie_len == 0) {
1625 		/* No RSN IE - fail silently */
1626 		return -1;
1627 	}
1628 
1629 	if (rsn_ie_len < sizeof(struct rsn_ie_hdr)) {
1630 		wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
1631 			   __func__, (unsigned long) rsn_ie_len);
1632 		return -1;
1633 	}
1634 
1635 	if (rsn_ie_len >= 6 && rsn_ie[1] >= 4 &&
1636 	    rsn_ie[1] == rsn_ie_len - 2 &&
1637 	    WPA_GET_BE32(&rsn_ie[2]) == OSEN_IE_VENDOR_TYPE) {
1638 		pos = rsn_ie + 6;
1639 		left = rsn_ie_len - 6;
1640 
1641 		data->group_cipher = WPA_CIPHER_GTK_NOT_USED;
1642 		data->has_group = 1;
1643 		data->key_mgmt = WPA_KEY_MGMT_OSEN;
1644 		data->proto = WPA_PROTO_OSEN;
1645 	} else {
1646 		const struct rsn_ie_hdr *hdr;
1647 
1648 		hdr = (const struct rsn_ie_hdr *) rsn_ie;
1649 
1650 		if (hdr->elem_id != WLAN_EID_RSN ||
1651 		    hdr->len != rsn_ie_len - 2 ||
1652 		    WPA_GET_LE16(hdr->version) != RSN_VERSION) {
1653 			wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
1654 				   __func__);
1655 			return -2;
1656 		}
1657 
1658 		pos = (const u8 *) (hdr + 1);
1659 		left = rsn_ie_len - sizeof(*hdr);
1660 	}
1661 
1662 	if (left >= RSN_SELECTOR_LEN) {
1663 		data->group_cipher = rsn_selector_to_bitfield(pos);
1664 		data->has_group = 1;
1665 		if (!wpa_cipher_valid_group(data->group_cipher)) {
1666 			wpa_printf(MSG_DEBUG,
1667 				   "%s: invalid group cipher 0x%x (%08x)",
1668 				   __func__, data->group_cipher,
1669 				   WPA_GET_BE32(pos));
1670 			return -1;
1671 		}
1672 		pos += RSN_SELECTOR_LEN;
1673 		left -= RSN_SELECTOR_LEN;
1674 	} else if (left > 0) {
1675 		wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
1676 			   __func__, left);
1677 		return -3;
1678 	}
1679 
1680 	if (left >= 2) {
1681 		data->pairwise_cipher = 0;
1682 		count = WPA_GET_LE16(pos);
1683 		pos += 2;
1684 		left -= 2;
1685 		if (count == 0 || count > left / RSN_SELECTOR_LEN) {
1686 			wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
1687 				   "count %u left %u", __func__, count, left);
1688 			return -4;
1689 		}
1690 		if (count)
1691 			data->has_pairwise = 1;
1692 		for (i = 0; i < count; i++) {
1693 			data->pairwise_cipher |= rsn_selector_to_bitfield(pos);
1694 			pos += RSN_SELECTOR_LEN;
1695 			left -= RSN_SELECTOR_LEN;
1696 		}
1697 		if (data->pairwise_cipher & WPA_CIPHER_AES_128_CMAC) {
1698 			wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as "
1699 				   "pairwise cipher", __func__);
1700 			return -1;
1701 		}
1702 	} else if (left == 1) {
1703 		wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
1704 			   __func__);
1705 		return -5;
1706 	}
1707 
1708 	if (left >= 2) {
1709 		data->key_mgmt = 0;
1710 		count = WPA_GET_LE16(pos);
1711 		pos += 2;
1712 		left -= 2;
1713 		if (count == 0 || count > left / RSN_SELECTOR_LEN) {
1714 			wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
1715 				   "count %u left %u", __func__, count, left);
1716 			return -6;
1717 		}
1718 		for (i = 0; i < count; i++) {
1719 			data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos);
1720 			pos += RSN_SELECTOR_LEN;
1721 			left -= RSN_SELECTOR_LEN;
1722 		}
1723 	} else if (left == 1) {
1724 		wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
1725 			   __func__);
1726 		return -7;
1727 	}
1728 
1729 	if (left >= 2) {
1730 		data->capabilities = WPA_GET_LE16(pos);
1731 		pos += 2;
1732 		left -= 2;
1733 	}
1734 
1735 	if (left >= 2) {
1736 		u16 num_pmkid = WPA_GET_LE16(pos);
1737 		pos += 2;
1738 		left -= 2;
1739 		if (num_pmkid > (unsigned int) left / PMKID_LEN) {
1740 			wpa_printf(MSG_DEBUG, "%s: PMKID underflow "
1741 				   "(num_pmkid=%u left=%d)",
1742 				   __func__, num_pmkid, left);
1743 			data->num_pmkid = 0;
1744 			return -9;
1745 		} else {
1746 			data->num_pmkid = num_pmkid;
1747 			data->pmkid = pos;
1748 			pos += data->num_pmkid * PMKID_LEN;
1749 			left -= data->num_pmkid * PMKID_LEN;
1750 		}
1751 	}
1752 
1753 	if (left >= 4) {
1754 		data->mgmt_group_cipher = rsn_selector_to_bitfield(pos);
1755 		if (!wpa_cipher_valid_mgmt_group(data->mgmt_group_cipher)) {
1756 			wpa_printf(MSG_DEBUG,
1757 				   "%s: Unsupported management group cipher 0x%x (%08x)",
1758 				   __func__, data->mgmt_group_cipher,
1759 				   WPA_GET_BE32(pos));
1760 			return -10;
1761 		}
1762 		pos += RSN_SELECTOR_LEN;
1763 		left -= RSN_SELECTOR_LEN;
1764 	}
1765 
1766 	if (left > 0) {
1767 		wpa_hexdump(MSG_DEBUG,
1768 			    "wpa_parse_wpa_ie_rsn: ignore trailing bytes",
1769 			    pos, left);
1770 	}
1771 
1772 	return 0;
1773 }
1774 
1775 
wpa_selector_to_bitfield(const u8 * s)1776 static int wpa_selector_to_bitfield(const u8 *s)
1777 {
1778 	if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_NONE)
1779 		return WPA_CIPHER_NONE;
1780 	if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_TKIP)
1781 		return WPA_CIPHER_TKIP;
1782 	if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_CCMP)
1783 		return WPA_CIPHER_CCMP;
1784 	return 0;
1785 }
1786 
1787 
wpa_key_mgmt_to_bitfield(const u8 * s)1788 static int wpa_key_mgmt_to_bitfield(const u8 *s)
1789 {
1790 	if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_UNSPEC_802_1X)
1791 		return WPA_KEY_MGMT_IEEE8021X;
1792 	if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X)
1793 		return WPA_KEY_MGMT_PSK;
1794 	if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_NONE)
1795 		return WPA_KEY_MGMT_WPA_NONE;
1796 	return 0;
1797 }
1798 
1799 
wpa_parse_wpa_ie_wpa(const u8 * wpa_ie,size_t wpa_ie_len,struct wpa_ie_data * data)1800 int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len,
1801 			 struct wpa_ie_data *data)
1802 {
1803 	const struct wpa_ie_hdr *hdr;
1804 	const u8 *pos;
1805 	int left;
1806 	int i, count;
1807 
1808 	os_memset(data, 0, sizeof(*data));
1809 	data->proto = WPA_PROTO_WPA;
1810 	data->pairwise_cipher = WPA_CIPHER_TKIP;
1811 	data->group_cipher = WPA_CIPHER_TKIP;
1812 	data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1813 	data->capabilities = 0;
1814 	data->pmkid = NULL;
1815 	data->num_pmkid = 0;
1816 	data->mgmt_group_cipher = 0;
1817 
1818 	if (wpa_ie_len < sizeof(struct wpa_ie_hdr)) {
1819 		wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
1820 			   __func__, (unsigned long) wpa_ie_len);
1821 		return -1;
1822 	}
1823 
1824 	hdr = (const struct wpa_ie_hdr *) wpa_ie;
1825 
1826 	if (hdr->elem_id != WLAN_EID_VENDOR_SPECIFIC ||
1827 	    hdr->len != wpa_ie_len - 2 ||
1828 	    RSN_SELECTOR_GET(hdr->oui) != WPA_OUI_TYPE ||
1829 	    WPA_GET_LE16(hdr->version) != WPA_VERSION) {
1830 		wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
1831 			   __func__);
1832 		return -2;
1833 	}
1834 
1835 	pos = (const u8 *) (hdr + 1);
1836 	left = wpa_ie_len - sizeof(*hdr);
1837 
1838 	if (left >= WPA_SELECTOR_LEN) {
1839 		data->group_cipher = wpa_selector_to_bitfield(pos);
1840 		pos += WPA_SELECTOR_LEN;
1841 		left -= WPA_SELECTOR_LEN;
1842 	} else if (left > 0) {
1843 		wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
1844 			   __func__, left);
1845 		return -3;
1846 	}
1847 
1848 	if (left >= 2) {
1849 		data->pairwise_cipher = 0;
1850 		count = WPA_GET_LE16(pos);
1851 		pos += 2;
1852 		left -= 2;
1853 		if (count == 0 || count > left / WPA_SELECTOR_LEN) {
1854 			wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
1855 				   "count %u left %u", __func__, count, left);
1856 			return -4;
1857 		}
1858 		for (i = 0; i < count; i++) {
1859 			data->pairwise_cipher |= wpa_selector_to_bitfield(pos);
1860 			pos += WPA_SELECTOR_LEN;
1861 			left -= WPA_SELECTOR_LEN;
1862 		}
1863 	} else if (left == 1) {
1864 		wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
1865 			   __func__);
1866 		return -5;
1867 	}
1868 
1869 	if (left >= 2) {
1870 		data->key_mgmt = 0;
1871 		count = WPA_GET_LE16(pos);
1872 		pos += 2;
1873 		left -= 2;
1874 		if (count == 0 || count > left / WPA_SELECTOR_LEN) {
1875 			wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
1876 				   "count %u left %u", __func__, count, left);
1877 			return -6;
1878 		}
1879 		for (i = 0; i < count; i++) {
1880 			data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos);
1881 			pos += WPA_SELECTOR_LEN;
1882 			left -= WPA_SELECTOR_LEN;
1883 		}
1884 	} else if (left == 1) {
1885 		wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
1886 			   __func__);
1887 		return -7;
1888 	}
1889 
1890 	if (left >= 2) {
1891 		data->capabilities = WPA_GET_LE16(pos);
1892 		pos += 2;
1893 		left -= 2;
1894 	}
1895 
1896 	if (left > 0) {
1897 		wpa_hexdump(MSG_DEBUG,
1898 			    "wpa_parse_wpa_ie_wpa: ignore trailing bytes",
1899 			    pos, left);
1900 	}
1901 
1902 	return 0;
1903 }
1904 
1905 
wpa_default_rsn_cipher(int freq)1906 int wpa_default_rsn_cipher(int freq)
1907 {
1908 	if (freq > 56160)
1909 		return WPA_CIPHER_GCMP; /* DMG */
1910 
1911 	return WPA_CIPHER_CCMP;
1912 }
1913 
1914 
1915 #ifdef CONFIG_IEEE80211R
1916 
1917 /**
1918  * wpa_derive_pmk_r0 - Derive PMK-R0 and PMKR0Name
1919  *
1920  * IEEE Std 802.11r-2008 - 8.5.1.5.3
1921  */
wpa_derive_pmk_r0(const u8 * xxkey,size_t xxkey_len,const u8 * ssid,size_t ssid_len,const u8 * mdid,const u8 * r0kh_id,size_t r0kh_id_len,const u8 * s0kh_id,u8 * pmk_r0,u8 * pmk_r0_name,int use_sha384)1922 int wpa_derive_pmk_r0(const u8 *xxkey, size_t xxkey_len,
1923 		      const u8 *ssid, size_t ssid_len,
1924 		      const u8 *mdid, const u8 *r0kh_id, size_t r0kh_id_len,
1925 		      const u8 *s0kh_id, u8 *pmk_r0, u8 *pmk_r0_name,
1926 		      int use_sha384)
1927 {
1928 	u8 buf[1 + SSID_MAX_LEN + MOBILITY_DOMAIN_ID_LEN + 1 +
1929 	       FT_R0KH_ID_MAX_LEN + ETH_ALEN];
1930 	u8 *pos, r0_key_data[64], hash[48];
1931 	const u8 *addr[2];
1932 	size_t len[2];
1933 	size_t q = use_sha384 ? 48 : 32;
1934 	size_t r0_key_data_len = q + 16;
1935 
1936 	/*
1937 	 * R0-Key-Data = KDF-384(XXKey, "FT-R0",
1938 	 *                       SSIDlength || SSID || MDID || R0KHlength ||
1939 	 *                       R0KH-ID || S0KH-ID)
1940 	 * XXKey is either the second 256 bits of MSK or PSK; or the first
1941 	 * 384 bits of MSK for FT-EAP-SHA384.
1942 	 * PMK-R0 = L(R0-Key-Data, 0, Q)
1943 	 * PMK-R0Name-Salt = L(R0-Key-Data, Q, 128)
1944 	 * Q = 384 for FT-EAP-SHA384; otherwise, 256
1945 	 */
1946 	if (ssid_len > SSID_MAX_LEN || r0kh_id_len > FT_R0KH_ID_MAX_LEN)
1947 		return -1;
1948 	wpa_printf(MSG_DEBUG, "FT: Derive PMK-R0 using KDF-%s",
1949 		   use_sha384 ? "SHA384" : "SHA256");
1950 	wpa_hexdump_key(MSG_DEBUG, "FT: XXKey", xxkey, xxkey_len);
1951 	wpa_hexdump_ascii(MSG_DEBUG, "FT: SSID", ssid, ssid_len);
1952 	wpa_hexdump(MSG_DEBUG, "FT: MDID", mdid, MOBILITY_DOMAIN_ID_LEN);
1953 	wpa_hexdump_ascii(MSG_DEBUG, "FT: R0KH-ID", r0kh_id, r0kh_id_len);
1954 	wpa_printf(MSG_DEBUG, "FT: S0KH-ID: " MACSTR_SEC, MAC2STR_SEC(s0kh_id));
1955 	pos = buf;
1956 	*pos++ = ssid_len;
1957 	os_memcpy(pos, ssid, ssid_len);
1958 	pos += ssid_len;
1959 	os_memcpy(pos, mdid, MOBILITY_DOMAIN_ID_LEN);
1960 	pos += MOBILITY_DOMAIN_ID_LEN;
1961 	*pos++ = r0kh_id_len;
1962 	os_memcpy(pos, r0kh_id, r0kh_id_len);
1963 	pos += r0kh_id_len;
1964 	os_memcpy(pos, s0kh_id, ETH_ALEN);
1965 	pos += ETH_ALEN;
1966 
1967 #ifdef CONFIG_SHA384
1968 	if (use_sha384) {
1969 		if (xxkey_len != SHA384_MAC_LEN) {
1970 			wpa_printf(MSG_ERROR,
1971 				   "FT: Unexpected XXKey length %d (expected %d)",
1972 				   (int) xxkey_len, SHA384_MAC_LEN);
1973 			return -1;
1974 		}
1975 		if (sha384_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf,
1976 			       r0_key_data, r0_key_data_len) < 0)
1977 			return -1;
1978 	}
1979 #endif /* CONFIG_SHA384 */
1980 	if (!use_sha384) {
1981 		if (xxkey_len != PMK_LEN) {
1982 			wpa_printf(MSG_ERROR,
1983 				   "FT: Unexpected XXKey length %d (expected %d)",
1984 				   (int) xxkey_len, PMK_LEN);
1985 			return -1;
1986 		}
1987 		if (sha256_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf,
1988 			       r0_key_data, r0_key_data_len) < 0)
1989 			return -1;
1990 	}
1991 	os_memcpy(pmk_r0, r0_key_data, q);
1992 	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, q);
1993 	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0Name-Salt", &r0_key_data[q], 16);
1994 
1995 	/*
1996 	 * PMKR0Name = Truncate-128(Hash("FT-R0N" || PMK-R0Name-Salt)
1997 	 */
1998 	addr[0] = (const u8 *) "FT-R0N";
1999 	len[0] = 6;
2000 	addr[1] = &r0_key_data[q];
2001 	len[1] = 16;
2002 
2003 #ifdef CONFIG_SHA384
2004 	if (use_sha384 && sha384_vector(2, addr, len, hash) < 0)
2005 		return -1;
2006 #endif /* CONFIG_SHA384 */
2007 	if (!use_sha384 && sha256_vector(2, addr, len, hash) < 0)
2008 		return -1;
2009 	os_memcpy(pmk_r0_name, hash, WPA_PMK_NAME_LEN);
2010 	wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name, WPA_PMK_NAME_LEN);
2011 	forced_memzero(r0_key_data, sizeof(r0_key_data));
2012 	return 0;
2013 }
2014 
2015 
2016 /**
2017  * wpa_derive_pmk_r1_name - Derive PMKR1Name
2018  *
2019  * IEEE Std 802.11r-2008 - 8.5.1.5.4
2020  */
wpa_derive_pmk_r1_name(const u8 * pmk_r0_name,const u8 * r1kh_id,const u8 * s1kh_id,u8 * pmk_r1_name,int use_sha384)2021 int wpa_derive_pmk_r1_name(const u8 *pmk_r0_name, const u8 *r1kh_id,
2022 			   const u8 *s1kh_id, u8 *pmk_r1_name, int use_sha384)
2023 {
2024 	u8 hash[48];
2025 	const u8 *addr[4];
2026 	size_t len[4];
2027 
2028 	/*
2029 	 * PMKR1Name = Truncate-128(Hash("FT-R1N" || PMKR0Name ||
2030 	 *                               R1KH-ID || S1KH-ID))
2031 	 */
2032 	addr[0] = (const u8 *) "FT-R1N";
2033 	len[0] = 6;
2034 	addr[1] = pmk_r0_name;
2035 	len[1] = WPA_PMK_NAME_LEN;
2036 	addr[2] = r1kh_id;
2037 	len[2] = FT_R1KH_ID_LEN;
2038 	addr[3] = s1kh_id;
2039 	len[3] = ETH_ALEN;
2040 
2041 #ifdef CONFIG_SHA384
2042 	if (use_sha384 && sha384_vector(4, addr, len, hash) < 0)
2043 		return -1;
2044 #endif /* CONFIG_SHA384 */
2045 	if (!use_sha384 && sha256_vector(4, addr, len, hash) < 0)
2046 		return -1;
2047 	os_memcpy(pmk_r1_name, hash, WPA_PMK_NAME_LEN);
2048 	wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", pmk_r1_name, WPA_PMK_NAME_LEN);
2049 	return 0;
2050 }
2051 
2052 
2053 /**
2054  * wpa_derive_pmk_r1 - Derive PMK-R1 and PMKR1Name from PMK-R0
2055  *
2056  * IEEE Std 802.11r-2008 - 8.5.1.5.4
2057  */
wpa_derive_pmk_r1(const u8 * pmk_r0,size_t pmk_r0_len,const u8 * pmk_r0_name,const u8 * r1kh_id,const u8 * s1kh_id,u8 * pmk_r1,u8 * pmk_r1_name)2058 int wpa_derive_pmk_r1(const u8 *pmk_r0, size_t pmk_r0_len,
2059 		      const u8 *pmk_r0_name,
2060 		      const u8 *r1kh_id, const u8 *s1kh_id,
2061 		      u8 *pmk_r1, u8 *pmk_r1_name)
2062 {
2063 	u8 buf[FT_R1KH_ID_LEN + ETH_ALEN];
2064 	u8 *pos;
2065 
2066 	/* PMK-R1 = KDF-256(PMK-R0, "FT-R1", R1KH-ID || S1KH-ID) */
2067 	wpa_printf(MSG_DEBUG, "FT: Derive PMK-R1 using KDF-%s",
2068 		   pmk_r0_len == SHA384_MAC_LEN ? "SHA384" : "SHA256");
2069 	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, pmk_r0_len);
2070 	wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", r1kh_id, FT_R1KH_ID_LEN);
2071 	wpa_printf(MSG_DEBUG, "FT: S1KH-ID: " MACSTR_SEC, MAC2STR_SEC(s1kh_id));
2072 	pos = buf;
2073 	os_memcpy(pos, r1kh_id, FT_R1KH_ID_LEN);
2074 	pos += FT_R1KH_ID_LEN;
2075 	os_memcpy(pos, s1kh_id, ETH_ALEN);
2076 	pos += ETH_ALEN;
2077 
2078 #ifdef CONFIG_SHA384
2079 	if (pmk_r0_len == SHA384_MAC_LEN &&
2080 	    sha384_prf(pmk_r0, pmk_r0_len, "FT-R1",
2081 		       buf, pos - buf, pmk_r1, pmk_r0_len) < 0)
2082 		return -1;
2083 #endif /* CONFIG_SHA384 */
2084 	if (pmk_r0_len == PMK_LEN &&
2085 	    sha256_prf(pmk_r0, pmk_r0_len, "FT-R1",
2086 		       buf, pos - buf, pmk_r1, pmk_r0_len) < 0)
2087 		return -1;
2088 	if (pmk_r0_len != SHA384_MAC_LEN && pmk_r0_len != PMK_LEN) {
2089 		wpa_printf(MSG_ERROR, "FT: Unexpected PMK-R0 length %d",
2090 			   (int) pmk_r0_len);
2091 		return -1;
2092 	}
2093 	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, pmk_r0_len);
2094 
2095 	return wpa_derive_pmk_r1_name(pmk_r0_name, r1kh_id, s1kh_id,
2096 				      pmk_r1_name,
2097 				      pmk_r0_len == SHA384_MAC_LEN);
2098 }
2099 
2100 
2101 /**
2102  * wpa_pmk_r1_to_ptk - Derive PTK and PTKName from PMK-R1
2103  *
2104  * IEEE Std 802.11r-2008 - 8.5.1.5.5
2105  */
wpa_pmk_r1_to_ptk(const u8 * pmk_r1,size_t pmk_r1_len,const u8 * snonce,const u8 * anonce,const u8 * sta_addr,const u8 * bssid,const u8 * pmk_r1_name,struct wpa_ptk * ptk,u8 * ptk_name,int akmp,int cipher,size_t kdk_len)2106 int wpa_pmk_r1_to_ptk(const u8 *pmk_r1, size_t pmk_r1_len,
2107 		      const u8 *snonce, const u8 *anonce,
2108 		      const u8 *sta_addr, const u8 *bssid,
2109 		      const u8 *pmk_r1_name,
2110 		      struct wpa_ptk *ptk, u8 *ptk_name, int akmp, int cipher,
2111 		      size_t kdk_len)
2112 {
2113 	u8 buf[2 * WPA_NONCE_LEN + 2 * ETH_ALEN];
2114 	u8 *pos, hash[32];
2115 	const u8 *addr[6];
2116 	size_t len[6];
2117 	u8 tmp[2 * WPA_KCK_MAX_LEN + 2 * WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN +
2118 	       WPA_KDK_MAX_LEN];
2119 	size_t ptk_len, offset;
2120 	int use_sha384 = wpa_key_mgmt_sha384(akmp);
2121 
2122 	if (kdk_len > WPA_KDK_MAX_LEN) {
2123 		wpa_printf(MSG_ERROR,
2124 			   "FT: KDK len=%zu exceeds max supported len",
2125 			   kdk_len);
2126 		return -1;
2127 	}
2128 
2129 	/*
2130 	 * PTK = KDF-PTKLen(PMK-R1, "FT-PTK", SNonce || ANonce ||
2131 	 *                  BSSID || STA-ADDR)
2132 	 */
2133 	wpa_printf(MSG_DEBUG, "FT: Derive PTK using KDF-%s",
2134 		   use_sha384 ? "SHA384" : "SHA256");
2135 	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, pmk_r1_len);
2136 	wpa_hexdump(MSG_DEBUG, "FT: SNonce", snonce, WPA_NONCE_LEN);
2137 	wpa_hexdump(MSG_DEBUG, "FT: ANonce", anonce, WPA_NONCE_LEN);
2138 	wpa_printf(MSG_DEBUG, "FT: BSSID=" MACSTR_SEC " STA-ADDR=" MACSTR_SEC,
2139 		   MAC2STR_SEC(bssid), MAC2STR_SEC(sta_addr));
2140 	pos = buf;
2141 	os_memcpy(pos, snonce, WPA_NONCE_LEN);
2142 	pos += WPA_NONCE_LEN;
2143 	os_memcpy(pos, anonce, WPA_NONCE_LEN);
2144 	pos += WPA_NONCE_LEN;
2145 	os_memcpy(pos, bssid, ETH_ALEN);
2146 	pos += ETH_ALEN;
2147 	os_memcpy(pos, sta_addr, ETH_ALEN);
2148 	pos += ETH_ALEN;
2149 
2150 	ptk->kck_len = wpa_kck_len(akmp, PMK_LEN);
2151 	ptk->kck2_len = wpa_kck2_len(akmp);
2152 	ptk->kek_len = wpa_kek_len(akmp, PMK_LEN);
2153 	ptk->kek2_len = wpa_kek2_len(akmp);
2154 	ptk->tk_len = wpa_cipher_key_len(cipher);
2155 	ptk->kdk_len = kdk_len;
2156 	ptk_len = ptk->kck_len + ptk->kek_len + ptk->tk_len +
2157 		ptk->kck2_len + ptk->kek2_len + ptk->kdk_len;
2158 
2159 #ifdef CONFIG_SHA384
2160 	if (use_sha384) {
2161 		if (pmk_r1_len != SHA384_MAC_LEN) {
2162 			wpa_printf(MSG_ERROR,
2163 				   "FT: Unexpected PMK-R1 length %d (expected %d)",
2164 				   (int) pmk_r1_len, SHA384_MAC_LEN);
2165 			return -1;
2166 		}
2167 		if (sha384_prf(pmk_r1, pmk_r1_len, "FT-PTK",
2168 			       buf, pos - buf, tmp, ptk_len) < 0)
2169 			return -1;
2170 	}
2171 #endif /* CONFIG_SHA384 */
2172 	if (!use_sha384) {
2173 		if (pmk_r1_len != PMK_LEN) {
2174 			wpa_printf(MSG_ERROR,
2175 				   "FT: Unexpected PMK-R1 length %d (expected %d)",
2176 				   (int) pmk_r1_len, PMK_LEN);
2177 			return -1;
2178 		}
2179 		if (sha256_prf(pmk_r1, pmk_r1_len, "FT-PTK",
2180 			       buf, pos - buf, tmp, ptk_len) < 0)
2181 			return -1;
2182 	}
2183 	wpa_hexdump_key(MSG_DEBUG, "FT: PTK", tmp, ptk_len);
2184 
2185 	/*
2186 	 * PTKName = Truncate-128(SHA-256(PMKR1Name || "FT-PTKN" || SNonce ||
2187 	 *                                ANonce || BSSID || STA-ADDR))
2188 	 */
2189 	wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", pmk_r1_name, WPA_PMK_NAME_LEN);
2190 	addr[0] = pmk_r1_name;
2191 	len[0] = WPA_PMK_NAME_LEN;
2192 	addr[1] = (const u8 *) "FT-PTKN";
2193 	len[1] = 7;
2194 	addr[2] = snonce;
2195 	len[2] = WPA_NONCE_LEN;
2196 	addr[3] = anonce;
2197 	len[3] = WPA_NONCE_LEN;
2198 	addr[4] = bssid;
2199 	len[4] = ETH_ALEN;
2200 	addr[5] = sta_addr;
2201 	len[5] = ETH_ALEN;
2202 
2203 	if (sha256_vector(6, addr, len, hash) < 0)
2204 		return -1;
2205 	os_memcpy(ptk_name, hash, WPA_PMK_NAME_LEN);
2206 
2207 	os_memcpy(ptk->kck, tmp, ptk->kck_len);
2208 	offset = ptk->kck_len;
2209 	os_memcpy(ptk->kek, tmp + offset, ptk->kek_len);
2210 	offset += ptk->kek_len;
2211 	os_memcpy(ptk->tk, tmp + offset, ptk->tk_len);
2212 	offset += ptk->tk_len;
2213 	os_memcpy(ptk->kck2, tmp + offset, ptk->kck2_len);
2214 	offset += ptk->kck2_len;
2215 	os_memcpy(ptk->kek2, tmp + offset, ptk->kek2_len);
2216 	offset += ptk->kek2_len;
2217 	os_memcpy(ptk->kdk, tmp + offset, ptk->kdk_len);
2218 
2219 	wpa_hexdump_key(MSG_DEBUG, "FT: KCK", ptk->kck, ptk->kck_len);
2220 	wpa_hexdump_key(MSG_DEBUG, "FT: KEK", ptk->kek, ptk->kek_len);
2221 	if (ptk->kck2_len)
2222 		wpa_hexdump_key(MSG_DEBUG, "FT: KCK2",
2223 				ptk->kck2, ptk->kck2_len);
2224 	if (ptk->kek2_len)
2225 		wpa_hexdump_key(MSG_DEBUG, "FT: KEK2",
2226 				ptk->kek2, ptk->kek2_len);
2227 	if (ptk->kdk_len)
2228 		wpa_hexdump_key(MSG_DEBUG, "FT: KDK", ptk->kdk, ptk->kdk_len);
2229 
2230 	wpa_hexdump_key(MSG_DEBUG, "FT: TK", ptk->tk, ptk->tk_len);
2231 	wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
2232 
2233 	forced_memzero(tmp, sizeof(tmp));
2234 
2235 	return 0;
2236 }
2237 
2238 #endif /* CONFIG_IEEE80211R */
2239 
2240 
2241 /**
2242  * rsn_pmkid - Calculate PMK identifier
2243  * @pmk: Pairwise master key
2244  * @pmk_len: Length of pmk in bytes
2245  * @aa: Authenticator address
2246  * @spa: Supplicant address
2247  * @pmkid: Buffer for PMKID
2248  * @akmp: Negotiated key management protocol
2249  *
2250  * IEEE Std 802.11-2016 - 12.7.1.3 Pairwise key hierarchy
2251  * AKM: 00-0F-AC:5, 00-0F-AC:6, 00-0F-AC:14, 00-0F-AC:16
2252  * PMKID = Truncate-128(HMAC-SHA-256(PMK, "PMK Name" || AA || SPA))
2253  * AKM: 00-0F-AC:11
2254  * See rsn_pmkid_suite_b()
2255  * AKM: 00-0F-AC:12
2256  * See rsn_pmkid_suite_b_192()
2257  * AKM: 00-0F-AC:13, 00-0F-AC:15, 00-0F-AC:17
2258  * PMKID = Truncate-128(HMAC-SHA-384(PMK, "PMK Name" || AA || SPA))
2259  * Otherwise:
2260  * PMKID = Truncate-128(HMAC-SHA-1(PMK, "PMK Name" || AA || SPA))
2261  */
rsn_pmkid(const u8 * pmk,size_t pmk_len,const u8 * aa,const u8 * spa,u8 * pmkid,int akmp)2262 void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
2263 	       u8 *pmkid, int akmp)
2264 {
2265 	char *title = "PMK Name";
2266 	const u8 *addr[3];
2267 	const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
2268 	unsigned char hash[SHA384_MAC_LEN];
2269 
2270 	addr[0] = (u8 *) title;
2271 	addr[1] = aa;
2272 	addr[2] = spa;
2273 
2274 	if (0) {
2275 #if defined(CONFIG_FILS) || defined(CONFIG_SHA384)
2276 	} else if (wpa_key_mgmt_sha384(akmp)) {
2277 		wpa_printf(MSG_DEBUG, "RSN: Derive PMKID using HMAC-SHA-384");
2278 		hmac_sha384_vector(pmk, pmk_len, 3, addr, len, hash);
2279 #endif /* CONFIG_FILS || CONFIG_SHA384 */
2280 	} else if (wpa_key_mgmt_sha256(akmp)) {
2281 		wpa_printf(MSG_DEBUG, "RSN: Derive PMKID using HMAC-SHA-256");
2282 		hmac_sha256_vector(pmk, pmk_len, 3, addr, len, hash);
2283 	} else {
2284 		wpa_printf(MSG_DEBUG, "RSN: Derive PMKID using HMAC-SHA-1");
2285 		hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash);
2286 	}
2287 	wpa_hexdump(MSG_DEBUG, "RSN: Derived PMKID", hash, PMKID_LEN);
2288 	os_memcpy(pmkid, hash, PMKID_LEN);
2289 }
2290 
2291 
2292 #ifdef CONFIG_SUITEB
2293 /**
2294  * rsn_pmkid_suite_b - Calculate PMK identifier for Suite B AKM
2295  * @kck: Key confirmation key
2296  * @kck_len: Length of kck in bytes
2297  * @aa: Authenticator address
2298  * @spa: Supplicant address
2299  * @pmkid: Buffer for PMKID
2300  * Returns: 0 on success, -1 on failure
2301  *
2302  * IEEE Std 802.11ac-2013 - 11.6.1.3 Pairwise key hierarchy
2303  * PMKID = Truncate(HMAC-SHA-256(KCK, "PMK Name" || AA || SPA))
2304  */
rsn_pmkid_suite_b(const u8 * kck,size_t kck_len,const u8 * aa,const u8 * spa,u8 * pmkid)2305 int rsn_pmkid_suite_b(const u8 *kck, size_t kck_len, const u8 *aa,
2306 		      const u8 *spa, u8 *pmkid)
2307 {
2308 	char *title = "PMK Name";
2309 	const u8 *addr[3];
2310 	const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
2311 	unsigned char hash[SHA256_MAC_LEN];
2312 
2313 	addr[0] = (u8 *) title;
2314 	addr[1] = aa;
2315 	addr[2] = spa;
2316 
2317 	if (hmac_sha256_vector(kck, kck_len, 3, addr, len, hash) < 0)
2318 		return -1;
2319 	os_memcpy(pmkid, hash, PMKID_LEN);
2320 	return 0;
2321 }
2322 #endif /* CONFIG_SUITEB */
2323 
2324 
2325 #ifdef CONFIG_SUITEB192
2326 /**
2327  * rsn_pmkid_suite_b_192 - Calculate PMK identifier for Suite B AKM
2328  * @kck: Key confirmation key
2329  * @kck_len: Length of kck in bytes
2330  * @aa: Authenticator address
2331  * @spa: Supplicant address
2332  * @pmkid: Buffer for PMKID
2333  * Returns: 0 on success, -1 on failure
2334  *
2335  * IEEE Std 802.11ac-2013 - 11.6.1.3 Pairwise key hierarchy
2336  * PMKID = Truncate(HMAC-SHA-384(KCK, "PMK Name" || AA || SPA))
2337  */
rsn_pmkid_suite_b_192(const u8 * kck,size_t kck_len,const u8 * aa,const u8 * spa,u8 * pmkid)2338 int rsn_pmkid_suite_b_192(const u8 *kck, size_t kck_len, const u8 *aa,
2339 			  const u8 *spa, u8 *pmkid)
2340 {
2341 	char *title = "PMK Name";
2342 	const u8 *addr[3];
2343 	const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
2344 	unsigned char hash[SHA384_MAC_LEN];
2345 
2346 	addr[0] = (u8 *) title;
2347 	addr[1] = aa;
2348 	addr[2] = spa;
2349 
2350 	if (hmac_sha384_vector(kck, kck_len, 3, addr, len, hash) < 0)
2351 		return -1;
2352 	os_memcpy(pmkid, hash, PMKID_LEN);
2353 	return 0;
2354 }
2355 #endif /* CONFIG_SUITEB192 */
2356 
2357 
2358 /**
2359  * wpa_cipher_txt - Convert cipher suite to a text string
2360  * @cipher: Cipher suite (WPA_CIPHER_* enum)
2361  * Returns: Pointer to a text string of the cipher suite name
2362  */
wpa_cipher_txt(int cipher)2363 const char * wpa_cipher_txt(int cipher)
2364 {
2365 	switch (cipher) {
2366 	case WPA_CIPHER_NONE:
2367 		return "NONE";
2368 #ifdef CONFIG_WEP
2369 	case WPA_CIPHER_WEP40:
2370 		return "WEP-40";
2371 	case WPA_CIPHER_WEP104:
2372 		return "WEP-104";
2373 #endif /* CONFIG_WEP */
2374 	case WPA_CIPHER_TKIP:
2375 		return "TKIP";
2376 	case WPA_CIPHER_CCMP:
2377 		return "CCMP";
2378 	case WPA_CIPHER_CCMP | WPA_CIPHER_TKIP:
2379 		return "CCMP+TKIP";
2380 	case WPA_CIPHER_GCMP:
2381 		return "GCMP";
2382 	case WPA_CIPHER_GCMP_256:
2383 		return "GCMP-256";
2384 	case WPA_CIPHER_CCMP_256:
2385 		return "CCMP-256";
2386 	case WPA_CIPHER_AES_128_CMAC:
2387 		return "BIP";
2388 	case WPA_CIPHER_BIP_GMAC_128:
2389 		return "BIP-GMAC-128";
2390 	case WPA_CIPHER_BIP_GMAC_256:
2391 		return "BIP-GMAC-256";
2392 	case WPA_CIPHER_BIP_CMAC_256:
2393 		return "BIP-CMAC-256";
2394 #ifdef CONFIG_WAPI
2395 	case WPA_CIPHER_SMS4:
2396 		return "SMS4";
2397 #endif
2398 	case WPA_CIPHER_GTK_NOT_USED:
2399 		return "GTK_NOT_USED";
2400 	default:
2401 		return "UNKNOWN";
2402 	}
2403 }
2404 
2405 
2406 /**
2407  * wpa_key_mgmt_txt - Convert key management suite to a text string
2408  * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum)
2409  * @proto: WPA/WPA2 version (WPA_PROTO_*)
2410  * Returns: Pointer to a text string of the key management suite name
2411  */
wpa_key_mgmt_txt(int key_mgmt,int proto)2412 const char * wpa_key_mgmt_txt(int key_mgmt, int proto)
2413 {
2414 	switch (key_mgmt) {
2415 	case WPA_KEY_MGMT_IEEE8021X:
2416 		if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA))
2417 			return "WPA2+WPA/IEEE 802.1X/EAP";
2418 		return proto == WPA_PROTO_RSN ?
2419 			"WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP";
2420 	case WPA_KEY_MGMT_PSK:
2421 		if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA))
2422 			return "WPA2-PSK+WPA-PSK";
2423 		return proto == WPA_PROTO_RSN ?
2424 			"WPA2-PSK" : "WPA-PSK";
2425 	case WPA_KEY_MGMT_NONE:
2426 		return "NONE";
2427 	case WPA_KEY_MGMT_WPA_NONE:
2428 		return "WPA-NONE";
2429 	case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
2430 		return "IEEE 802.1X (no WPA)";
2431 #ifdef CONFIG_IEEE80211R
2432 	case WPA_KEY_MGMT_FT_IEEE8021X:
2433 		return "FT-EAP";
2434 	case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
2435 		return "FT-EAP-SHA384";
2436 	case WPA_KEY_MGMT_FT_PSK:
2437 		return "FT-PSK";
2438 #endif /* CONFIG_IEEE80211R */
2439 	case WPA_KEY_MGMT_IEEE8021X_SHA256:
2440 		return "WPA2-EAP-SHA256";
2441 	case WPA_KEY_MGMT_PSK_SHA256:
2442 		return "WPA2-PSK-SHA256";
2443 	case WPA_KEY_MGMT_WPS:
2444 		return "WPS";
2445 	case WPA_KEY_MGMT_SAE:
2446 		return "SAE";
2447 	case WPA_KEY_MGMT_SAE_EXT_KEY:
2448 		return "SAE-EXT-KEY";
2449 	case WPA_KEY_MGMT_FT_SAE:
2450 		return "FT-SAE";
2451 	case WPA_KEY_MGMT_OSEN:
2452 		return "OSEN";
2453 	case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2454 		return "WPA2-EAP-SUITE-B";
2455 	case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2456 		return "WPA2-EAP-SUITE-B-192";
2457 	case WPA_KEY_MGMT_FILS_SHA256:
2458 		return "FILS-SHA256";
2459 	case WPA_KEY_MGMT_FILS_SHA384:
2460 		return "FILS-SHA384";
2461 	case WPA_KEY_MGMT_FT_FILS_SHA256:
2462 		return "FT-FILS-SHA256";
2463 	case WPA_KEY_MGMT_FT_FILS_SHA384:
2464 		return "FT-FILS-SHA384";
2465 	case WPA_KEY_MGMT_OWE:
2466 		return "OWE";
2467 	case WPA_KEY_MGMT_DPP:
2468 		return "DPP";
2469 	case WPA_KEY_MGMT_PASN:
2470 		return "PASN";
2471 	default:
2472 		return "UNKNOWN";
2473 	}
2474 }
2475 
2476 
wpa_akm_to_suite(int akm)2477 u32 wpa_akm_to_suite(int akm)
2478 {
2479 	if (akm & WPA_KEY_MGMT_FT_IEEE8021X_SHA384)
2480 		return RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384;
2481 	if (akm & WPA_KEY_MGMT_FT_IEEE8021X)
2482 		return RSN_AUTH_KEY_MGMT_FT_802_1X;
2483 	if (akm & WPA_KEY_MGMT_FT_PSK)
2484 		return RSN_AUTH_KEY_MGMT_FT_PSK;
2485 	if (akm & WPA_KEY_MGMT_IEEE8021X_SHA256)
2486 		return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2487 	if (akm & WPA_KEY_MGMT_IEEE8021X)
2488 		return RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
2489 	if (akm & WPA_KEY_MGMT_PSK_SHA256)
2490 		return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2491 	if (akm & WPA_KEY_MGMT_PSK)
2492 		return RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
2493 	if (akm & WPA_KEY_MGMT_CCKM)
2494 		return RSN_AUTH_KEY_MGMT_CCKM;
2495 	if (akm & WPA_KEY_MGMT_OSEN)
2496 		return RSN_AUTH_KEY_MGMT_OSEN;
2497 	if (akm & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
2498 		return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
2499 	if (akm & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
2500 		return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
2501 	if (akm & WPA_KEY_MGMT_FILS_SHA256)
2502 		return RSN_AUTH_KEY_MGMT_FILS_SHA256;
2503 	if (akm & WPA_KEY_MGMT_FILS_SHA384)
2504 		return RSN_AUTH_KEY_MGMT_FILS_SHA384;
2505 	if (akm & WPA_KEY_MGMT_FT_FILS_SHA256)
2506 		return RSN_AUTH_KEY_MGMT_FT_FILS_SHA256;
2507 	if (akm & WPA_KEY_MGMT_FT_FILS_SHA384)
2508 		return RSN_AUTH_KEY_MGMT_FT_FILS_SHA384;
2509 	if (akm & WPA_KEY_MGMT_SAE)
2510 		return RSN_AUTH_KEY_MGMT_SAE;
2511 	if (akm & WPA_KEY_MGMT_SAE_EXT_KEY)
2512 		return RSN_AUTH_KEY_MGMT_SAE_EXT_KEY;
2513 	if (akm & WPA_KEY_MGMT_FT_SAE)
2514 		return RSN_AUTH_KEY_MGMT_FT_SAE;
2515 	if (akm & WPA_KEY_MGMT_OWE)
2516 		return RSN_AUTH_KEY_MGMT_OWE;
2517 	if (akm & WPA_KEY_MGMT_DPP)
2518 		return RSN_AUTH_KEY_MGMT_DPP;
2519 	return 0;
2520 }
2521 
2522 
wpa_compare_rsn_ie(int ft_initial_assoc,const u8 * ie1,size_t ie1len,const u8 * ie2,size_t ie2len)2523 int wpa_compare_rsn_ie(int ft_initial_assoc,
2524 		       const u8 *ie1, size_t ie1len,
2525 		       const u8 *ie2, size_t ie2len)
2526 {
2527 	if (ie1 == NULL || ie2 == NULL)
2528 		return -1;
2529 
2530 	if (ie1len == ie2len && os_memcmp(ie1, ie2, ie1len) == 0)
2531 		return 0; /* identical IEs */
2532 
2533 #ifdef CONFIG_IEEE80211R
2534 	if (ft_initial_assoc) {
2535 		struct wpa_ie_data ie1d, ie2d;
2536 		/*
2537 		 * The PMKID-List in RSN IE is different between Beacon/Probe
2538 		 * Response/(Re)Association Request frames and EAPOL-Key
2539 		 * messages in FT initial mobility domain association. Allow
2540 		 * for this, but verify that other parts of the RSN IEs are
2541 		 * identical.
2542 		 */
2543 		if (wpa_parse_wpa_ie_rsn(ie1, ie1len, &ie1d) < 0 ||
2544 		    wpa_parse_wpa_ie_rsn(ie2, ie2len, &ie2d) < 0)
2545 			return -1;
2546 		if (ie1d.proto == ie2d.proto &&
2547 		    ie1d.pairwise_cipher == ie2d.pairwise_cipher &&
2548 		    ie1d.group_cipher == ie2d.group_cipher &&
2549 		    ie1d.key_mgmt == ie2d.key_mgmt &&
2550 		    ie1d.capabilities == ie2d.capabilities &&
2551 		    ie1d.mgmt_group_cipher == ie2d.mgmt_group_cipher)
2552 			return 0;
2553 	}
2554 #endif /* CONFIG_IEEE80211R */
2555 
2556 	return -1;
2557 }
2558 
2559 
wpa_insert_pmkid(u8 * ies,size_t * ies_len,const u8 * pmkid)2560 int wpa_insert_pmkid(u8 *ies, size_t *ies_len, const u8 *pmkid)
2561 {
2562 	u8 *start, *end, *rpos, *rend;
2563 	int added = 0;
2564 
2565 	start = ies;
2566 	end = ies + *ies_len;
2567 
2568 	while (start < end) {
2569 		if (*start == WLAN_EID_RSN)
2570 			break;
2571 		start += 2 + start[1];
2572 	}
2573 	if (start >= end) {
2574 		wpa_printf(MSG_ERROR, "RSN: Could not find RSNE in IEs data");
2575 		return -1;
2576 	}
2577 	wpa_hexdump(MSG_DEBUG, "RSN: RSNE before modification",
2578 		    start, 2 + start[1]);
2579 
2580 	/* Find start of PMKID-Count */
2581 	rpos = start + 2;
2582 	rend = rpos + start[1];
2583 
2584 	/* Skip Version and Group Data Cipher Suite */
2585 	rpos += 2 + 4;
2586 	/* Skip Pairwise Cipher Suite Count and List */
2587 	rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
2588 	/* Skip AKM Suite Count and List */
2589 	rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
2590 
2591 	if (rpos == rend) {
2592 		/* Add RSN Capabilities */
2593 		os_memmove(rpos + 2, rpos, end - rpos);
2594 		*rpos++ = 0;
2595 		*rpos++ = 0;
2596 		added += 2;
2597 		start[1] += 2;
2598 		rend = rpos;
2599 	} else {
2600 		/* Skip RSN Capabilities */
2601 		rpos += 2;
2602 		if (rpos > rend) {
2603 			wpa_printf(MSG_ERROR,
2604 				   "RSN: Could not parse RSNE in IEs data");
2605 			return -1;
2606 		}
2607 	}
2608 
2609 	if (rpos == rend) {
2610 		/* No PMKID-Count field included; add it */
2611 		os_memmove(rpos + 2 + PMKID_LEN, rpos, end + added - rpos);
2612 		WPA_PUT_LE16(rpos, 1);
2613 		rpos += 2;
2614 		os_memcpy(rpos, pmkid, PMKID_LEN);
2615 		added += 2 + PMKID_LEN;
2616 		start[1] += 2 + PMKID_LEN;
2617 	} else {
2618 		u16 num_pmkid;
2619 
2620 		if (rend - rpos < 2)
2621 			return -1;
2622 		num_pmkid = WPA_GET_LE16(rpos);
2623 		/* PMKID-Count was included; use it */
2624 		if (num_pmkid != 0) {
2625 			u8 *after;
2626 
2627 			if (num_pmkid * PMKID_LEN > rend - rpos - 2)
2628 				return -1;
2629 			/*
2630 			 * PMKID may have been included in RSN IE in
2631 			 * (Re)Association Request frame, so remove the old
2632 			 * PMKID(s) first before adding the new one.
2633 			 */
2634 			wpa_printf(MSG_DEBUG,
2635 				   "RSN: Remove %u old PMKID(s) from RSNE",
2636 				   num_pmkid);
2637 			after = rpos + 2 + num_pmkid * PMKID_LEN;
2638 			os_memmove(rpos + 2, after, end - after);
2639 			start[1] -= num_pmkid * PMKID_LEN;
2640 			added -= num_pmkid * PMKID_LEN;
2641 		}
2642 		WPA_PUT_LE16(rpos, 1);
2643 		rpos += 2;
2644 		os_memmove(rpos + PMKID_LEN, rpos, end + added - rpos);
2645 		os_memcpy(rpos, pmkid, PMKID_LEN);
2646 		added += PMKID_LEN;
2647 		start[1] += PMKID_LEN;
2648 	}
2649 
2650 	wpa_hexdump(MSG_DEBUG, "RSN: RSNE after modification (PMKID inserted)",
2651 		    start, 2 + start[1]);
2652 
2653 	*ies_len += added;
2654 
2655 	return 0;
2656 }
2657 
2658 
wpa_cipher_key_len(int cipher)2659 int wpa_cipher_key_len(int cipher)
2660 {
2661 	switch (cipher) {
2662 	case WPA_CIPHER_CCMP_256:
2663 	case WPA_CIPHER_GCMP_256:
2664 	case WPA_CIPHER_BIP_GMAC_256:
2665 	case WPA_CIPHER_BIP_CMAC_256:
2666 		return 32;
2667 	case WPA_CIPHER_CCMP:
2668 	case WPA_CIPHER_GCMP:
2669 	case WPA_CIPHER_AES_128_CMAC:
2670 	case WPA_CIPHER_BIP_GMAC_128:
2671 		return 16;
2672 	case WPA_CIPHER_TKIP:
2673 		return 32;
2674 	}
2675 
2676 	return 0;
2677 }
2678 
2679 
wpa_cipher_rsc_len(int cipher)2680 int wpa_cipher_rsc_len(int cipher)
2681 {
2682 	switch (cipher) {
2683 	case WPA_CIPHER_CCMP_256:
2684 	case WPA_CIPHER_GCMP_256:
2685 	case WPA_CIPHER_CCMP:
2686 	case WPA_CIPHER_GCMP:
2687 	case WPA_CIPHER_TKIP:
2688 		return 6;
2689 	}
2690 
2691 	return 0;
2692 }
2693 
2694 
wpa_cipher_to_alg(int cipher)2695 enum wpa_alg wpa_cipher_to_alg(int cipher)
2696 {
2697 	switch (cipher) {
2698 	case WPA_CIPHER_CCMP_256:
2699 		return WPA_ALG_CCMP_256;
2700 	case WPA_CIPHER_GCMP_256:
2701 		return WPA_ALG_GCMP_256;
2702 	case WPA_CIPHER_CCMP:
2703 		return WPA_ALG_CCMP;
2704 	case WPA_CIPHER_GCMP:
2705 		return WPA_ALG_GCMP;
2706 	case WPA_CIPHER_TKIP:
2707 		return WPA_ALG_TKIP;
2708 	case WPA_CIPHER_AES_128_CMAC:
2709 		return WPA_ALG_BIP_CMAC_128;
2710 	case WPA_CIPHER_BIP_GMAC_128:
2711 		return WPA_ALG_BIP_GMAC_128;
2712 	case WPA_CIPHER_BIP_GMAC_256:
2713 		return WPA_ALG_BIP_GMAC_256;
2714 	case WPA_CIPHER_BIP_CMAC_256:
2715 		return WPA_ALG_BIP_CMAC_256;
2716 	}
2717 	return WPA_ALG_NONE;
2718 }
2719 
2720 
wpa_cipher_valid_pairwise(int cipher)2721 int wpa_cipher_valid_pairwise(int cipher)
2722 {
2723 #ifdef CONFIG_NO_TKIP
2724 	return cipher == WPA_CIPHER_CCMP_256 ||
2725 		cipher == WPA_CIPHER_GCMP_256 ||
2726 		cipher == WPA_CIPHER_CCMP ||
2727 #ifdef CONFIG_WAPI
2728 		cipher == WPA_CIPHER_SMS4 ||
2729 #endif
2730 		cipher == WPA_CIPHER_GCMP;
2731 #else /* CONFIG_NO_TKIP */
2732 	return cipher == WPA_CIPHER_CCMP_256 ||
2733 		cipher == WPA_CIPHER_GCMP_256 ||
2734 		cipher == WPA_CIPHER_CCMP ||
2735 		cipher == WPA_CIPHER_GCMP ||
2736 #ifdef CONFIG_WAPI
2737 		cipher == WPA_CIPHER_SMS4 ||
2738 #endif
2739 		cipher == WPA_CIPHER_TKIP;
2740 #endif /* CONFIG_NO_TKIP */
2741 }
2742 
2743 
wpa_cipher_to_suite(int proto,int cipher)2744 u32 wpa_cipher_to_suite(int proto, int cipher)
2745 {
2746 	if (cipher & WPA_CIPHER_CCMP_256)
2747 		return RSN_CIPHER_SUITE_CCMP_256;
2748 	if (cipher & WPA_CIPHER_GCMP_256)
2749 		return RSN_CIPHER_SUITE_GCMP_256;
2750 	if (cipher & WPA_CIPHER_CCMP)
2751 		return (proto == WPA_PROTO_RSN ?
2752 			RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
2753 	if (cipher & WPA_CIPHER_GCMP)
2754 		return RSN_CIPHER_SUITE_GCMP;
2755 	if (cipher & WPA_CIPHER_TKIP)
2756 		return (proto == WPA_PROTO_RSN ?
2757 			RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
2758 	if (cipher & WPA_CIPHER_NONE)
2759 		return (proto == WPA_PROTO_RSN ?
2760 			RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
2761 	if (cipher & WPA_CIPHER_GTK_NOT_USED)
2762 		return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
2763 	if (cipher & WPA_CIPHER_AES_128_CMAC)
2764 		return RSN_CIPHER_SUITE_AES_128_CMAC;
2765 	if (cipher & WPA_CIPHER_BIP_GMAC_128)
2766 		return RSN_CIPHER_SUITE_BIP_GMAC_128;
2767 	if (cipher & WPA_CIPHER_BIP_GMAC_256)
2768 		return RSN_CIPHER_SUITE_BIP_GMAC_256;
2769 	if (cipher & WPA_CIPHER_BIP_CMAC_256)
2770 		return RSN_CIPHER_SUITE_BIP_CMAC_256;
2771 #ifdef CONFIG_WAPI
2772 	if (cipher & WPA_CIPHER_SMS4)
2773 		return RSN_CIPHER_SUITE_SMS4;
2774 #endif
2775 	return 0;
2776 }
2777 
2778 
rsn_cipher_put_suites(u8 * start,int ciphers)2779 int rsn_cipher_put_suites(u8 *start, int ciphers)
2780 {
2781 	u8 *pos = start;
2782 
2783 	if (ciphers & WPA_CIPHER_CCMP_256) {
2784 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP_256);
2785 		pos += RSN_SELECTOR_LEN;
2786 	}
2787 	if (ciphers & WPA_CIPHER_GCMP_256) {
2788 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP_256);
2789 		pos += RSN_SELECTOR_LEN;
2790 	}
2791 	if (ciphers & WPA_CIPHER_CCMP) {
2792 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
2793 		pos += RSN_SELECTOR_LEN;
2794 	}
2795 	if (ciphers & WPA_CIPHER_GCMP) {
2796 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP);
2797 		pos += RSN_SELECTOR_LEN;
2798 	}
2799 	if (ciphers & WPA_CIPHER_TKIP) {
2800 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
2801 		pos += RSN_SELECTOR_LEN;
2802 	}
2803 	if (ciphers & WPA_CIPHER_NONE) {
2804 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NONE);
2805 		pos += RSN_SELECTOR_LEN;
2806 	}
2807 
2808 	return (pos - start) / RSN_SELECTOR_LEN;
2809 }
2810 
2811 
wpa_cipher_put_suites(u8 * start,int ciphers)2812 int wpa_cipher_put_suites(u8 *start, int ciphers)
2813 {
2814 	u8 *pos = start;
2815 
2816 	if (ciphers & WPA_CIPHER_CCMP) {
2817 		RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_CCMP);
2818 		pos += WPA_SELECTOR_LEN;
2819 	}
2820 	if (ciphers & WPA_CIPHER_TKIP) {
2821 		RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_TKIP);
2822 		pos += WPA_SELECTOR_LEN;
2823 	}
2824 	if (ciphers & WPA_CIPHER_NONE) {
2825 		RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_NONE);
2826 		pos += WPA_SELECTOR_LEN;
2827 	}
2828 
2829 	return (pos - start) / RSN_SELECTOR_LEN;
2830 }
2831 
2832 
wpa_pick_pairwise_cipher(int ciphers,int none_allowed)2833 int wpa_pick_pairwise_cipher(int ciphers, int none_allowed)
2834 {
2835 	if (ciphers & WPA_CIPHER_CCMP_256)
2836 		return WPA_CIPHER_CCMP_256;
2837 	if (ciphers & WPA_CIPHER_GCMP_256)
2838 		return WPA_CIPHER_GCMP_256;
2839 	if (ciphers & WPA_CIPHER_CCMP)
2840 		return WPA_CIPHER_CCMP;
2841 	if (ciphers & WPA_CIPHER_GCMP)
2842 		return WPA_CIPHER_GCMP;
2843 	if (ciphers & WPA_CIPHER_TKIP)
2844 		return WPA_CIPHER_TKIP;
2845 #ifdef CONFIG_WAPI
2846 	if (ciphers & WPA_CIPHER_SMS4)
2847 		return WPA_CIPHER_SMS4;
2848 #endif
2849 	if (none_allowed && (ciphers & WPA_CIPHER_NONE))
2850 		return WPA_CIPHER_NONE;
2851 	return -1;
2852 }
2853 
2854 
wpa_pick_group_cipher(int ciphers)2855 int wpa_pick_group_cipher(int ciphers)
2856 {
2857 	if (ciphers & WPA_CIPHER_CCMP_256)
2858 		return WPA_CIPHER_CCMP_256;
2859 	if (ciphers & WPA_CIPHER_GCMP_256)
2860 		return WPA_CIPHER_GCMP_256;
2861 	if (ciphers & WPA_CIPHER_CCMP)
2862 		return WPA_CIPHER_CCMP;
2863 	if (ciphers & WPA_CIPHER_GCMP)
2864 		return WPA_CIPHER_GCMP;
2865 	if (ciphers & WPA_CIPHER_GTK_NOT_USED)
2866 		return WPA_CIPHER_GTK_NOT_USED;
2867 	if (ciphers & WPA_CIPHER_TKIP)
2868 		return WPA_CIPHER_TKIP;
2869 #ifdef CONFIG_WAPI
2870 	if (ciphers & WPA_CIPHER_SMS4)
2871 		return WPA_CIPHER_SMS4;
2872 #endif
2873 	return -1;
2874 }
2875 
2876 
wpa_parse_cipher(const char * value)2877 int wpa_parse_cipher(const char *value)
2878 {
2879 	int val = 0, last;
2880 	char *start, *end, *buf;
2881 
2882 	buf = os_strdup(value);
2883 	if (buf == NULL)
2884 		return -1;
2885 	start = buf;
2886 
2887 	while (*start != '\0') {
2888 		while (*start == ' ' || *start == '\t')
2889 			start++;
2890 		if (*start == '\0')
2891 			break;
2892 		end = start;
2893 		while (*end != ' ' && *end != '\t' && *end != '\0')
2894 			end++;
2895 		last = *end == '\0';
2896 		*end = '\0';
2897 		if (os_strcmp(start, "CCMP-256") == 0)
2898 			val |= WPA_CIPHER_CCMP_256;
2899 		else if (os_strcmp(start, "GCMP-256") == 0)
2900 			val |= WPA_CIPHER_GCMP_256;
2901 		else if (os_strcmp(start, "CCMP") == 0)
2902 			val |= WPA_CIPHER_CCMP;
2903 		else if (os_strcmp(start, "GCMP") == 0)
2904 			val |= WPA_CIPHER_GCMP;
2905 #ifndef CONFIG_NO_TKIP
2906 		else if (os_strcmp(start, "TKIP") == 0)
2907 			val |= WPA_CIPHER_TKIP;
2908 #endif /* CONFIG_NO_TKIP */
2909 #ifdef CONFIG_WEP
2910 		else if (os_strcmp(start, "WEP104") == 0)
2911 			val |= WPA_CIPHER_WEP104;
2912 		else if (os_strcmp(start, "WEP40") == 0)
2913 			val |= WPA_CIPHER_WEP40;
2914 #endif /* CONFIG_WEP */
2915 		else if (os_strcmp(start, "NONE") == 0)
2916 			val |= WPA_CIPHER_NONE;
2917 #ifdef CONFIG_WAPI
2918 		else if (os_strcmp(start, "SMS4") == 0)
2919 			val |= WPA_CIPHER_SMS4;
2920 #endif
2921 		else if (os_strcmp(start, "GTK_NOT_USED") == 0)
2922 			val |= WPA_CIPHER_GTK_NOT_USED;
2923 		else if (os_strcmp(start, "AES-128-CMAC") == 0)
2924 			val |= WPA_CIPHER_AES_128_CMAC;
2925 		else if (os_strcmp(start, "BIP-GMAC-128") == 0)
2926 			val |= WPA_CIPHER_BIP_GMAC_128;
2927 		else if (os_strcmp(start, "BIP-GMAC-256") == 0)
2928 			val |= WPA_CIPHER_BIP_GMAC_256;
2929 		else if (os_strcmp(start, "BIP-CMAC-256") == 0)
2930 			val |= WPA_CIPHER_BIP_CMAC_256;
2931 		else {
2932 			os_free(buf);
2933 			return -1;
2934 		}
2935 
2936 		if (last)
2937 			break;
2938 		start = end + 1;
2939 	}
2940 	os_free(buf);
2941 
2942 	return val;
2943 }
2944 
2945 
wpa_write_ciphers(char * start,char * end,int ciphers,const char * delim)2946 int wpa_write_ciphers(char *start, char *end, int ciphers, const char *delim)
2947 {
2948 	char *pos = start;
2949 	int ret;
2950 
2951 	if (ciphers & WPA_CIPHER_CCMP_256) {
2952 		ret = os_snprintf(pos, end - pos, "%sCCMP-256",
2953 				  pos == start ? "" : delim);
2954 		if (os_snprintf_error(end - pos, ret))
2955 			return -1;
2956 		pos += ret;
2957 	}
2958 	if (ciphers & WPA_CIPHER_GCMP_256) {
2959 		ret = os_snprintf(pos, end - pos, "%sGCMP-256",
2960 				  pos == start ? "" : delim);
2961 		if (os_snprintf_error(end - pos, ret))
2962 			return -1;
2963 		pos += ret;
2964 	}
2965 	if (ciphers & WPA_CIPHER_CCMP) {
2966 		ret = os_snprintf(pos, end - pos, "%sCCMP",
2967 				  pos == start ? "" : delim);
2968 		if (os_snprintf_error(end - pos, ret))
2969 			return -1;
2970 		pos += ret;
2971 	}
2972 	if (ciphers & WPA_CIPHER_GCMP) {
2973 		ret = os_snprintf(pos, end - pos, "%sGCMP",
2974 				  pos == start ? "" : delim);
2975 		if (os_snprintf_error(end - pos, ret))
2976 			return -1;
2977 		pos += ret;
2978 	}
2979 	if (ciphers & WPA_CIPHER_TKIP) {
2980 		ret = os_snprintf(pos, end - pos, "%sTKIP",
2981 				  pos == start ? "" : delim);
2982 		if (os_snprintf_error(end - pos, ret))
2983 			return -1;
2984 		pos += ret;
2985 	}
2986 	if (ciphers & WPA_CIPHER_AES_128_CMAC) {
2987 		ret = os_snprintf(pos, end - pos, "%sAES-128-CMAC",
2988 				  pos == start ? "" : delim);
2989 		if (os_snprintf_error(end - pos, ret))
2990 			return -1;
2991 		pos += ret;
2992 	}
2993 	if (ciphers & WPA_CIPHER_BIP_GMAC_128) {
2994 		ret = os_snprintf(pos, end - pos, "%sBIP-GMAC-128",
2995 				  pos == start ? "" : delim);
2996 		if (os_snprintf_error(end - pos, ret))
2997 			return -1;
2998 		pos += ret;
2999 	}
3000 	if (ciphers & WPA_CIPHER_BIP_GMAC_256) {
3001 		ret = os_snprintf(pos, end - pos, "%sBIP-GMAC-256",
3002 				  pos == start ? "" : delim);
3003 		if (os_snprintf_error(end - pos, ret))
3004 			return -1;
3005 		pos += ret;
3006 	}
3007 	if (ciphers & WPA_CIPHER_BIP_CMAC_256) {
3008 		ret = os_snprintf(pos, end - pos, "%sBIP-CMAC-256",
3009 				  pos == start ? "" : delim);
3010 		if (os_snprintf_error(end - pos, ret))
3011 			return -1;
3012 		pos += ret;
3013 	}
3014 	if (ciphers & WPA_CIPHER_NONE) {
3015 		ret = os_snprintf(pos, end - pos, "%sNONE",
3016 				  pos == start ? "" : delim);
3017 		if (os_snprintf_error(end - pos, ret))
3018 			return -1;
3019 		pos += ret;
3020 	}
3021 #ifdef CONFIG_WAPI
3022 	if (ciphers & WPA_CIPHER_SMS4) {
3023 		ret = os_snprintf(pos, end - pos, "%sSMS4",
3024 				  pos == start ? "" : delim);
3025 		if (os_snprintf_error(end - pos, ret)) {
3026 			return -1;
3027 		}
3028 		pos += ret;
3029 	}
3030 #endif
3031 
3032 	return pos - start;
3033 }
3034 
3035 
wpa_select_ap_group_cipher(int wpa,int wpa_pairwise,int rsn_pairwise)3036 int wpa_select_ap_group_cipher(int wpa, int wpa_pairwise, int rsn_pairwise)
3037 {
3038 	int pairwise = 0;
3039 
3040 	/* Select group cipher based on the enabled pairwise cipher suites */
3041 	if (wpa & 1)
3042 		pairwise |= wpa_pairwise;
3043 	if (wpa & 2)
3044 		pairwise |= rsn_pairwise;
3045 
3046 	if (pairwise & WPA_CIPHER_TKIP)
3047 		return WPA_CIPHER_TKIP;
3048 	if ((pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) == WPA_CIPHER_GCMP)
3049 		return WPA_CIPHER_GCMP;
3050 	if ((pairwise & (WPA_CIPHER_GCMP_256 | WPA_CIPHER_CCMP |
3051 			 WPA_CIPHER_GCMP)) == WPA_CIPHER_GCMP_256)
3052 		return WPA_CIPHER_GCMP_256;
3053 	if ((pairwise & (WPA_CIPHER_CCMP_256 | WPA_CIPHER_CCMP |
3054 			 WPA_CIPHER_GCMP)) == WPA_CIPHER_CCMP_256)
3055 		return WPA_CIPHER_CCMP_256;
3056 	return WPA_CIPHER_CCMP;
3057 }
3058 
3059 
3060 #ifdef CONFIG_FILS
fils_domain_name_hash(const char * domain,u8 * hash)3061 int fils_domain_name_hash(const char *domain, u8 *hash)
3062 {
3063 	char buf[255], *wpos = buf;
3064 	const char *pos = domain;
3065 	size_t len;
3066 	const u8 *addr[1];
3067 	u8 mac[SHA256_MAC_LEN];
3068 
3069 	for (len = 0; len < sizeof(buf) && *pos; len++) {
3070 		if (isalpha(*pos) && isupper(*pos))
3071 			*wpos++ = tolower(*pos);
3072 		else
3073 			*wpos++ = *pos;
3074 		pos++;
3075 	}
3076 
3077 	addr[0] = (const u8 *) buf;
3078 	if (sha256_vector(1, addr, &len, mac) < 0)
3079 		return -1;
3080 	os_memcpy(hash, mac, 2);
3081 	return 0;
3082 }
3083 #endif /* CONFIG_FILS */
3084 
3085 
3086 /**
3087  * wpa_parse_vendor_specific - Parse Vendor Specific IEs
3088  * @pos: Pointer to the IE header
3089  * @end: Pointer to the end of the Key Data buffer
3090  * @ie: Pointer to parsed IE data
3091  */
wpa_parse_vendor_specific(const u8 * pos,const u8 * end,struct wpa_eapol_ie_parse * ie)3092 static void wpa_parse_vendor_specific(const u8 *pos, const u8 *end,
3093 				      struct wpa_eapol_ie_parse *ie)
3094 {
3095 	unsigned int oui;
3096 
3097 	if (pos[1] < 4) {
3098 		wpa_printf(MSG_MSGDUMP,
3099 			   "Too short vendor specific IE ignored (len=%u)",
3100 			   pos[1]);
3101 		return;
3102 	}
3103 
3104 	oui = WPA_GET_BE24(&pos[2]);
3105 	if (oui == OUI_MICROSOFT && pos[5] == WMM_OUI_TYPE && pos[1] > 4) {
3106 		if (pos[6] == WMM_OUI_SUBTYPE_INFORMATION_ELEMENT) {
3107 			ie->wmm = &pos[2];
3108 			ie->wmm_len = pos[1];
3109 			wpa_hexdump(MSG_DEBUG, "WPA: WMM IE",
3110 				    ie->wmm, ie->wmm_len);
3111 		} else if (pos[6] == WMM_OUI_SUBTYPE_PARAMETER_ELEMENT) {
3112 			ie->wmm = &pos[2];
3113 			ie->wmm_len = pos[1];
3114 			wpa_hexdump(MSG_DEBUG, "WPA: WMM Parameter Element",
3115 				    ie->wmm, ie->wmm_len);
3116 		}
3117 	}
3118 }
3119 
3120 #ifdef CONFIG_MLD_PATCH
3121 /**
3122  * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
3123  * @pos: Pointer to the IE header
3124  * @ie: Pointer to parsed IE data
3125  * Returns: 0 on success, 1 if end mark is found, 2 if KDE is not recognized
3126  */
wpa_parse_generic(const u8 * pos,struct wpa_eapol_ie_parse * ie)3127 static int wpa_parse_generic(const u8 *pos, struct wpa_eapol_ie_parse *ie)
3128 {
3129 	u8 len = pos[1];
3130 	size_t dlen = 2 + len;
3131 	u32 selector;
3132 	const u8 *p;
3133 	size_t left;
3134 	u8 link_id;
3135 	char title[50];
3136 	int ret;
3137 
3138 	if (len == 0)
3139 		return 1;
3140 
3141 	if (len < RSN_SELECTOR_LEN)
3142 		return 2;
3143 
3144 	p = pos + 2;
3145 	selector = RSN_SELECTOR_GET(p);
3146 	p += RSN_SELECTOR_LEN;
3147 	left = len - RSN_SELECTOR_LEN;
3148 
3149 	if (left >= 2 && selector == WPA_OUI_TYPE && p[0] == 1 && p[1] == 0) {
3150 		ie->wpa_ie = pos;
3151 		ie->wpa_ie_len = dlen;
3152 		wpa_hexdump(MSG_DEBUG, "WPA: WPA IE in EAPOL-Key",
3153 			    ie->wpa_ie, ie->wpa_ie_len);
3154 		return 0;
3155 	}
3156 
3157 	if (selector == OSEN_IE_VENDOR_TYPE) {
3158 		ie->osen = pos;
3159 		ie->osen_len = dlen;
3160 		return 0;
3161 	}
3162 
3163 	if (left >= PMKID_LEN && selector == RSN_KEY_DATA_PMKID) {
3164 		ie->pmkid = p;
3165 		wpa_hexdump(MSG_DEBUG, "WPA: PMKID in EAPOL-Key", pos, dlen);
3166 		return 0;
3167 	}
3168 
3169 	if (left >= 2 && selector == RSN_KEY_DATA_KEYID) {
3170 		ie->key_id = p;
3171 		wpa_hexdump(MSG_DEBUG, "WPA: KeyID in EAPOL-Key", pos, dlen);
3172 		return 0;
3173 	}
3174 
3175 	if (left > 2 && selector == RSN_KEY_DATA_GROUPKEY) {
3176 		ie->gtk = p;
3177 		ie->gtk_len = left;
3178 		wpa_hexdump_key(MSG_DEBUG, "WPA: GTK in EAPOL-Key", pos, dlen);
3179 		return 0;
3180 	}
3181 
3182 	if (left >= ETH_ALEN && selector == RSN_KEY_DATA_MAC_ADDR) {
3183 		ie->mac_addr = p;
3184 		wpa_printf(MSG_DEBUG, "WPA: MAC Address in EAPOL-Key: " MACSTR_SEC,
3185 			   MAC2STR_SEC(ie->mac_addr));
3186 		return 0;
3187 	}
3188 
3189 	if (left > 2 && selector == RSN_KEY_DATA_IGTK) {
3190 		ie->igtk = p;
3191 		ie->igtk_len = left;
3192 		wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK in EAPOL-Key",
3193 				pos, dlen);
3194 		return 0;
3195 	}
3196 
3197 	if (left > 2 && selector == RSN_KEY_DATA_BIGTK) {
3198 		ie->bigtk = p;
3199 		ie->bigtk_len = left;
3200 		wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK in EAPOL-Key",
3201 				pos, dlen);
3202 		return 0;
3203 	}
3204 
3205 	if (left >= 1 && selector == WFA_KEY_DATA_IP_ADDR_REQ) {
3206 		ie->ip_addr_req = p;
3207 		wpa_hexdump(MSG_DEBUG, "WPA: IP Address Request in EAPOL-Key",
3208 			    ie->ip_addr_req, left);
3209 		return 0;
3210 	}
3211 
3212 	if (left >= 3 * 4 && selector == WFA_KEY_DATA_IP_ADDR_ALLOC) {
3213 		ie->ip_addr_alloc = p;
3214 		wpa_hexdump(MSG_DEBUG,
3215 			    "WPA: IP Address Allocation in EAPOL-Key",
3216 			    ie->ip_addr_alloc, left);
3217 		return 0;
3218 	}
3219 
3220 	if (left > 2 && selector == RSN_KEY_DATA_OCI) {
3221 		ie->oci = p;
3222 		ie->oci_len = left;
3223 		wpa_hexdump(MSG_DEBUG, "WPA: OCI KDE in EAPOL-Key",
3224 			    pos, dlen);
3225 		return 0;
3226 	}
3227 
3228 	if (left >= 1 && selector == WFA_KEY_DATA_TRANSITION_DISABLE) {
3229 		ie->transition_disable = p;
3230 		ie->transition_disable_len = left;
3231 		wpa_hexdump(MSG_DEBUG,
3232 			    "WPA: Transition Disable KDE in EAPOL-Key",
3233 			    pos, dlen);
3234 		return 0;
3235 	}
3236 
3237 	if (left >= 2 && selector == WFA_KEY_DATA_DPP) {
3238 		ie->dpp_kde = p;
3239 		ie->dpp_kde_len = left;
3240 		wpa_hexdump(MSG_DEBUG, "WPA: DPP KDE in EAPOL-Key", pos, dlen);
3241 		return 0;
3242 	}
3243 
3244 	if (left >= RSN_MLO_GTK_KDE_PREFIX_LENGTH &&
3245 	    selector == RSN_KEY_DATA_MLO_GTK) {
3246 		link_id = (p[0] & RSN_MLO_GTK_KDE_PREFIX0_LINK_ID_MASK) >>
3247 			RSN_MLO_GTK_KDE_PREFIX0_LINK_ID_SHIFT;
3248 		if (link_id >= MAX_NUM_MLD_LINKS)
3249 			return 2;
3250 
3251 		ie->valid_mlo_gtks |= BIT(link_id);
3252 		ie->mlo_gtk[link_id] = p;
3253 		ie->mlo_gtk_len[link_id] = left;
3254 		ret = os_snprintf(title, sizeof(title),
3255 				  "RSN: Link ID %u - MLO GTK KDE in EAPOL-Key",
3256 				  link_id);
3257 		if (!os_snprintf_error(sizeof(title), ret))
3258 			wpa_hexdump_key(MSG_DEBUG, title, pos, dlen);
3259 		return 0;
3260 	}
3261 
3262 	if (left >= RSN_MLO_IGTK_KDE_PREFIX_LENGTH &&
3263 	    selector == RSN_KEY_DATA_MLO_IGTK) {
3264 		link_id = (p[8] & RSN_MLO_IGTK_KDE_PREFIX8_LINK_ID_MASK) >>
3265 			  RSN_MLO_IGTK_KDE_PREFIX8_LINK_ID_SHIFT;
3266 		if (link_id >= MAX_NUM_MLD_LINKS)
3267 			return 2;
3268 
3269 		ie->valid_mlo_igtks |= BIT(link_id);
3270 		ie->mlo_igtk[link_id] = p;
3271 		ie->mlo_igtk_len[link_id] = left;
3272 		ret = os_snprintf(title, sizeof(title),
3273 				  "RSN: Link ID %u - MLO IGTK KDE in EAPOL-Key",
3274 				  link_id);
3275 		if (!os_snprintf_error(sizeof(title), ret))
3276 			wpa_hexdump_key(MSG_DEBUG, title, pos, dlen);
3277 		return 0;
3278 	}
3279 
3280 	if (left >= RSN_MLO_BIGTK_KDE_PREFIX_LENGTH &&
3281 	    selector == RSN_KEY_DATA_MLO_BIGTK) {
3282 		link_id = (p[8] & RSN_MLO_BIGTK_KDE_PREFIX8_LINK_ID_MASK) >>
3283 			  RSN_MLO_BIGTK_KDE_PREFIX8_LINK_ID_SHIFT;
3284 		if (link_id >= MAX_NUM_MLD_LINKS)
3285 			return 2;
3286 
3287 		ie->valid_mlo_bigtks |= BIT(link_id);
3288 		ie->mlo_bigtk[link_id] = p;
3289 		ie->mlo_bigtk_len[link_id] = left;
3290 		ret = os_snprintf(title, sizeof(title),
3291 				  "RSN: Link ID %u - MLO BIGTK KDE in EAPOL-Key",
3292 				  link_id);
3293 		if (!os_snprintf_error(sizeof(title), ret))
3294 			wpa_hexdump_key(MSG_DEBUG, title, pos, dlen);
3295 		return 0;
3296 	}
3297 
3298 	if (left >= RSN_MLO_LINK_KDE_FIXED_LENGTH &&
3299 	    selector == RSN_KEY_DATA_MLO_LINK) {
3300 		link_id = (p[0] & RSN_MLO_LINK_KDE_LI_LINK_ID_MASK) >>
3301 			  RSN_MLO_LINK_KDE_LI_LINK_ID_SHIFT;
3302 		if (link_id >= MAX_NUM_MLD_LINKS)
3303 			return 2;
3304 
3305 		ie->valid_mlo_links |= BIT(link_id);
3306 		ie->mlo_link[link_id] = p;
3307 		ie->mlo_link_len[link_id] = left;
3308 		ret = os_snprintf(title, sizeof(title),
3309 				  "RSN: Link ID %u - MLO Link KDE in EAPOL-Key",
3310 				  link_id);
3311 		if (!os_snprintf_error(sizeof(title), ret))
3312 			wpa_hexdump(MSG_DEBUG, title, pos, dlen);
3313 		return 0;
3314 	}
3315 
3316 	return 2;
3317 }
3318 #else
3319 /**
3320  * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs
3321  * @pos: Pointer to the IE header
3322  * @ie: Pointer to parsed IE data
3323  * Returns: 0 on success, 1 if end mark is found, 2 if KDE is not recognized
3324  */
wpa_parse_generic(const u8 * pos,struct wpa_eapol_ie_parse * ie)3325 static int wpa_parse_generic(const u8 *pos, struct wpa_eapol_ie_parse *ie)
3326 {
3327 	if (pos[1] == 0)
3328 		return 1;
3329 
3330 	if (pos[1] >= 6 &&
3331 	    RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE &&
3332 	    pos[2 + WPA_SELECTOR_LEN] == 1 &&
3333 	    pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
3334 		ie->wpa_ie = pos;
3335 		ie->wpa_ie_len = pos[1] + 2;
3336 		wpa_hexdump(MSG_DEBUG, "WPA: WPA IE in EAPOL-Key",
3337 			    ie->wpa_ie, ie->wpa_ie_len);
3338 		return 0;
3339 	}
3340 
3341 	if (pos[1] >= 4 && WPA_GET_BE32(pos + 2) == OSEN_IE_VENDOR_TYPE) {
3342 		ie->osen = pos;
3343 		ie->osen_len = pos[1] + 2;
3344 		return 0;
3345 	}
3346 
3347 	if (pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
3348 	    RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) {
3349 		ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
3350 		wpa_hexdump(MSG_DEBUG, "WPA: PMKID in EAPOL-Key",
3351 			    pos, pos[1] + 2);
3352 		return 0;
3353 	}
3354 
3355 	if (pos[1] >= RSN_SELECTOR_LEN + 2 &&
3356 	    RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_KEYID) {
3357 		ie->key_id = pos + 2 + RSN_SELECTOR_LEN;
3358 		wpa_hexdump(MSG_DEBUG, "WPA: KeyID in EAPOL-Key",
3359 			    pos, pos[1] + 2);
3360 		return 0;
3361 	}
3362 
3363 	if (pos[1] > RSN_SELECTOR_LEN + 2 &&
3364 	    RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) {
3365 		ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
3366 		ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
3367 		wpa_hexdump_key(MSG_DEBUG, "WPA: GTK in EAPOL-Key",
3368 				pos, pos[1] + 2);
3369 		return 0;
3370 	}
3371 
3372 	if (pos[1] > RSN_SELECTOR_LEN + 2 &&
3373 	    RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) {
3374 		ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN;
3375 		ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN;
3376 		wpa_hexdump(MSG_DEBUG, "WPA: MAC Address in EAPOL-Key",
3377 			    pos, pos[1] + 2);
3378 		return 0;
3379 	}
3380 
3381 	if (pos[1] > RSN_SELECTOR_LEN + 2 &&
3382 	    RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) {
3383 		ie->igtk = pos + 2 + RSN_SELECTOR_LEN;
3384 		ie->igtk_len = pos[1] - RSN_SELECTOR_LEN;
3385 		wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK in EAPOL-Key",
3386 				pos, pos[1] + 2);
3387 		return 0;
3388 	}
3389 
3390 	if (pos[1] > RSN_SELECTOR_LEN + 2 &&
3391 	    RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_BIGTK) {
3392 		ie->bigtk = pos + 2 + RSN_SELECTOR_LEN;
3393 		ie->bigtk_len = pos[1] - RSN_SELECTOR_LEN;
3394 		wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK in EAPOL-Key",
3395 				pos, pos[1] + 2);
3396 		return 0;
3397 	}
3398 
3399 	if (pos[1] >= RSN_SELECTOR_LEN + 1 &&
3400 	    RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_REQ) {
3401 		ie->ip_addr_req = pos + 2 + RSN_SELECTOR_LEN;
3402 		wpa_hexdump(MSG_DEBUG, "WPA: IP Address Request in EAPOL-Key",
3403 			    ie->ip_addr_req, pos[1] - RSN_SELECTOR_LEN);
3404 		return 0;
3405 	}
3406 
3407 	if (pos[1] >= RSN_SELECTOR_LEN + 3 * 4 &&
3408 	    RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_ALLOC) {
3409 		ie->ip_addr_alloc = pos + 2 + RSN_SELECTOR_LEN;
3410 		wpa_hexdump(MSG_DEBUG,
3411 			    "WPA: IP Address Allocation in EAPOL-Key",
3412 			    ie->ip_addr_alloc, pos[1] - RSN_SELECTOR_LEN);
3413 		return 0;
3414 	}
3415 
3416 	if (pos[1] > RSN_SELECTOR_LEN + 2 &&
3417 	    RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_OCI) {
3418 		ie->oci = pos + 2 + RSN_SELECTOR_LEN;
3419 		ie->oci_len = pos[1] - RSN_SELECTOR_LEN;
3420 		wpa_hexdump(MSG_DEBUG, "WPA: OCI KDE in EAPOL-Key",
3421 			    pos, pos[1] + 2);
3422 		return 0;
3423 	}
3424 
3425 	if (pos[1] >= RSN_SELECTOR_LEN + 1 &&
3426 	    RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_TRANSITION_DISABLE) {
3427 		ie->transition_disable = pos + 2 + RSN_SELECTOR_LEN;
3428 		ie->transition_disable_len = pos[1] - RSN_SELECTOR_LEN;
3429 		wpa_hexdump(MSG_DEBUG,
3430 			    "WPA: Transition Disable KDE in EAPOL-Key",
3431 			    pos, pos[1] + 2);
3432 		return 0;
3433 	}
3434 
3435 	if (pos[1] >= RSN_SELECTOR_LEN + 2 &&
3436 	    RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_DPP) {
3437 		ie->dpp_kde = pos + 2 + RSN_SELECTOR_LEN;
3438 		ie->dpp_kde_len = pos[1] - RSN_SELECTOR_LEN;
3439 		wpa_hexdump(MSG_DEBUG, "WPA: DPP KDE in EAPOL-Key",
3440 			    pos, pos[1] + 2);
3441 		return 0;
3442 	}
3443 
3444 	return 2;
3445 }
3446 #endif
3447 
3448 /**
3449  * wpa_parse_kde_ies - Parse EAPOL-Key Key Data IEs
3450  * @buf: Pointer to the Key Data buffer
3451  * @len: Key Data Length
3452  * @ie: Pointer to parsed IE data
3453  * Returns: 0 on success, -1 on failure
3454  */
wpa_parse_kde_ies(const u8 * buf,size_t len,struct wpa_eapol_ie_parse * ie)3455 int wpa_parse_kde_ies(const u8 *buf, size_t len, struct wpa_eapol_ie_parse *ie)
3456 {
3457 	const u8 *pos, *end;
3458 	int ret = 0;
3459 
3460 	os_memset(ie, 0, sizeof(*ie));
3461 	for (pos = buf, end = pos + len; end - pos > 1; pos += 2 + pos[1]) {
3462 		if (pos[0] == 0xdd &&
3463 		    ((pos == buf + len - 1) || pos[1] == 0)) {
3464 			/* Ignore padding */
3465 			break;
3466 		}
3467 		if (2 + pos[1] > end - pos) {
3468 			wpa_printf(MSG_DEBUG,
3469 				   "WPA: EAPOL-Key Key Data underflow (ie=%d len=%d pos=%d)",
3470 				   pos[0], pos[1], (int) (pos - buf));
3471 			wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data", buf, len);
3472 			ret = -1;
3473 			break;
3474 		}
3475 		if (*pos == WLAN_EID_RSN) {
3476 			ie->rsn_ie = pos;
3477 			ie->rsn_ie_len = pos[1] + 2;
3478 			wpa_hexdump(MSG_DEBUG, "WPA: RSN IE in EAPOL-Key",
3479 				    ie->rsn_ie, ie->rsn_ie_len);
3480 		} else if (*pos == WLAN_EID_RSNX) {
3481 			ie->rsnxe = pos;
3482 			ie->rsnxe_len = pos[1] + 2;
3483 			wpa_hexdump(MSG_DEBUG, "WPA: RSNXE in EAPOL-Key",
3484 				    ie->rsnxe, ie->rsnxe_len);
3485 		} else if (*pos == WLAN_EID_MOBILITY_DOMAIN) {
3486 			ie->mdie = pos;
3487 			ie->mdie_len = pos[1] + 2;
3488 			wpa_hexdump(MSG_DEBUG, "WPA: MDIE in EAPOL-Key",
3489 				    ie->mdie, ie->mdie_len);
3490 		} else if (*pos == WLAN_EID_FAST_BSS_TRANSITION) {
3491 			ie->ftie = pos;
3492 			ie->ftie_len = pos[1] + 2;
3493 			wpa_hexdump(MSG_DEBUG, "WPA: FTIE in EAPOL-Key",
3494 				    ie->ftie, ie->ftie_len);
3495 		} else if (*pos == WLAN_EID_TIMEOUT_INTERVAL && pos[1] >= 5) {
3496 			if (pos[2] == WLAN_TIMEOUT_REASSOC_DEADLINE) {
3497 				ie->reassoc_deadline = pos;
3498 				wpa_hexdump(MSG_DEBUG, "WPA: Reassoc Deadline "
3499 					    "in EAPOL-Key",
3500 					    ie->reassoc_deadline, pos[1] + 2);
3501 			} else if (pos[2] == WLAN_TIMEOUT_KEY_LIFETIME) {
3502 				ie->key_lifetime = pos;
3503 				wpa_hexdump(MSG_DEBUG, "WPA: KeyLifetime "
3504 					    "in EAPOL-Key",
3505 					    ie->key_lifetime, pos[1] + 2);
3506 			} else {
3507 				wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized "
3508 					    "EAPOL-Key Key Data IE",
3509 					    pos, 2 + pos[1]);
3510 			}
3511 		} else if (*pos == WLAN_EID_LINK_ID) {
3512 			if (pos[1] >= 18) {
3513 				ie->lnkid = pos;
3514 				ie->lnkid_len = pos[1] + 2;
3515 			}
3516 		} else if (*pos == WLAN_EID_EXT_CAPAB) {
3517 			ie->ext_capab = pos;
3518 			ie->ext_capab_len = pos[1] + 2;
3519 		} else if (*pos == WLAN_EID_SUPP_RATES) {
3520 			ie->supp_rates = pos;
3521 			ie->supp_rates_len = pos[1] + 2;
3522 		} else if (*pos == WLAN_EID_EXT_SUPP_RATES) {
3523 			ie->ext_supp_rates = pos;
3524 			ie->ext_supp_rates_len = pos[1] + 2;
3525 		} else if (*pos == WLAN_EID_HT_CAP &&
3526 			   pos[1] >= sizeof(struct ieee80211_ht_capabilities)) {
3527 			ie->ht_capabilities = pos + 2;
3528 		} else if (*pos == WLAN_EID_VHT_AID) {
3529 			if (pos[1] >= 2)
3530 				ie->aid = WPA_GET_LE16(pos + 2) & 0x3fff;
3531 		} else if (*pos == WLAN_EID_VHT_CAP &&
3532 			   pos[1] >= sizeof(struct ieee80211_vht_capabilities))
3533 		{
3534 			ie->vht_capabilities = pos + 2;
3535 		} else if (*pos == WLAN_EID_EXTENSION &&
3536 			   pos[1] >= 1 + IEEE80211_HE_CAPAB_MIN_LEN &&
3537 			   pos[2] == WLAN_EID_EXT_HE_CAPABILITIES) {
3538 			ie->he_capabilities = pos + 3;
3539 			ie->he_capab_len = pos[1] - 1;
3540 		} else if (*pos == WLAN_EID_EXTENSION &&
3541 			   pos[1] >= 1 +
3542 			   sizeof(struct ieee80211_he_6ghz_band_cap) &&
3543 			   pos[2] == WLAN_EID_EXT_HE_6GHZ_BAND_CAP) {
3544 			ie->he_6ghz_capabilities = pos + 3;
3545 		} else if (*pos == WLAN_EID_QOS && pos[1] >= 1) {
3546 			ie->qosinfo = pos[2];
3547 		} else if (*pos == WLAN_EID_SUPPORTED_CHANNELS) {
3548 			ie->supp_channels = pos + 2;
3549 			ie->supp_channels_len = pos[1];
3550 		} else if (*pos == WLAN_EID_SUPPORTED_OPERATING_CLASSES) {
3551 			/*
3552 			 * The value of the Length field of the Supported
3553 			 * Operating Classes element is between 2 and 253.
3554 			 * Silently skip invalid elements to avoid interop
3555 			 * issues when trying to use the value.
3556 			 */
3557 			if (pos[1] >= 2 && pos[1] <= 253) {
3558 				ie->supp_oper_classes = pos + 2;
3559 				ie->supp_oper_classes_len = pos[1];
3560 			}
3561 		} else if (*pos == WLAN_EID_VENDOR_SPECIFIC) {
3562 			ret = wpa_parse_generic(pos, ie);
3563 			if (ret == 1) {
3564 				/* end mark found */
3565 				ret = 0;
3566 				break;
3567 			}
3568 
3569 			if (ret == 2) {
3570 				/* not a known KDE */
3571 				wpa_parse_vendor_specific(pos, end, ie);
3572 			}
3573 
3574 			ret = 0;
3575 		} else {
3576 			wpa_hexdump(MSG_DEBUG,
3577 				    "WPA: Unrecognized EAPOL-Key Key Data IE",
3578 				    pos, 2 + pos[1]);
3579 		}
3580 	}
3581 
3582 	return ret;
3583 }
3584 
3585 
3586 #ifdef CONFIG_PASN
3587 
3588 /*
3589  * wpa_pasn_build_auth_header - Add the MAC header and initialize Authentication
3590  * frame for PASN
3591  *
3592  * @buf: Buffer in which the header will be added
3593  * @bssid: The BSSID of the AP
3594  * @src: Source address
3595  * @dst: Destination address
3596  * @trans_seq: Authentication transaction sequence number
3597  * @status: Authentication status
3598  */
wpa_pasn_build_auth_header(struct wpabuf * buf,const u8 * bssid,const u8 * src,const u8 * dst,u8 trans_seq,u16 status)3599 void wpa_pasn_build_auth_header(struct wpabuf *buf, const u8 *bssid,
3600 				const u8 *src, const u8 *dst,
3601 				u8 trans_seq, u16 status)
3602 {
3603 	struct ieee80211_mgmt *auth;
3604 
3605 	wpa_printf(MSG_DEBUG, "PASN: Add authentication header. trans_seq=%u",
3606 		   trans_seq);
3607 
3608 	auth = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
3609 					u.auth.variable));
3610 
3611 	auth->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
3612 					   (WLAN_FC_STYPE_AUTH << 4));
3613 
3614 	os_memcpy(auth->da, dst, ETH_ALEN);
3615 	os_memcpy(auth->sa, src, ETH_ALEN);
3616 	os_memcpy(auth->bssid, bssid, ETH_ALEN);
3617 	auth->seq_ctrl = 0;
3618 
3619 	auth->u.auth.auth_alg = host_to_le16(WLAN_AUTH_PASN);
3620 	auth->u.auth.auth_transaction = host_to_le16(trans_seq);
3621 	auth->u.auth.status_code = host_to_le16(status);
3622 }
3623 
3624 
3625 /*
3626  * wpa_pasn_add_rsne - Add an RSNE for PASN authentication
3627  * @buf: Buffer in which the IE will be added
3628  * @pmkid: Optional PMKID. Can be NULL.
3629  * @akmp: Authentication and key management protocol
3630  * @cipher: The cipher suite
3631  */
wpa_pasn_add_rsne(struct wpabuf * buf,const u8 * pmkid,int akmp,int cipher)3632 int wpa_pasn_add_rsne(struct wpabuf *buf, const u8 *pmkid, int akmp, int cipher)
3633 {
3634 	struct rsn_ie_hdr *hdr;
3635 	u32 suite;
3636 	u16 capab;
3637 	u8 *pos;
3638 	u8 rsne_len;
3639 
3640 	wpa_printf(MSG_DEBUG, "PASN: Add RSNE");
3641 
3642 	rsne_len = sizeof(*hdr) + RSN_SELECTOR_LEN +
3643 		2 + RSN_SELECTOR_LEN + 2 + RSN_SELECTOR_LEN +
3644 		2 + RSN_SELECTOR_LEN + 2 + (pmkid ? PMKID_LEN : 0);
3645 
3646 	if (wpabuf_tailroom(buf) < rsne_len)
3647 		return -1;
3648 	hdr = wpabuf_put(buf, rsne_len);
3649 	hdr->elem_id = WLAN_EID_RSN;
3650 	hdr->len = rsne_len - 2;
3651 	WPA_PUT_LE16(hdr->version, RSN_VERSION);
3652 	pos = (u8 *) (hdr + 1);
3653 
3654 	/* Group addressed data is not allowed */
3655 	RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
3656 	pos += RSN_SELECTOR_LEN;
3657 
3658 	/* Add the pairwise cipher */
3659 	WPA_PUT_LE16(pos, 1);
3660 	pos += 2;
3661 	suite = wpa_cipher_to_suite(WPA_PROTO_RSN, cipher);
3662 	RSN_SELECTOR_PUT(pos, suite);
3663 	pos += RSN_SELECTOR_LEN;
3664 
3665 	/* Add the AKM suite */
3666 	WPA_PUT_LE16(pos, 1);
3667 	pos += 2;
3668 
3669 	switch (akmp) {
3670 	case WPA_KEY_MGMT_PASN:
3671 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PASN);
3672 		break;
3673 #ifdef CONFIG_SAE
3674 	case WPA_KEY_MGMT_SAE:
3675 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE);
3676 		break;
3677 #endif /* CONFIG_SAE */
3678 #ifdef CONFIG_FILS
3679 	case WPA_KEY_MGMT_FILS_SHA256:
3680 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA256);
3681 		break;
3682 	case WPA_KEY_MGMT_FILS_SHA384:
3683 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA384);
3684 		break;
3685 #endif /* CONFIG_FILS */
3686 #ifdef CONFIG_IEEE80211R
3687 	case WPA_KEY_MGMT_FT_PSK:
3688 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
3689 		break;
3690 	case WPA_KEY_MGMT_FT_IEEE8021X:
3691 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
3692 		break;
3693 	case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
3694 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384);
3695 		break;
3696 #endif /* CONFIG_IEEE80211R */
3697 	default:
3698 		wpa_printf(MSG_ERROR, "PASN: Invalid AKMP=0x%x", akmp);
3699 		return -1;
3700 	}
3701 	pos += RSN_SELECTOR_LEN;
3702 
3703 	/* RSN Capabilities: PASN mandates both MFP capable and required */
3704 	capab = WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR;
3705 	WPA_PUT_LE16(pos, capab);
3706 	pos += 2;
3707 
3708 	if (pmkid) {
3709 		wpa_printf(MSG_DEBUG, "PASN: Adding PMKID");
3710 
3711 		WPA_PUT_LE16(pos, 1);
3712 		pos += 2;
3713 		os_memcpy(pos, pmkid, PMKID_LEN);
3714 		pos += PMKID_LEN;
3715 	} else {
3716 		WPA_PUT_LE16(pos, 0);
3717 		pos += 2;
3718 	}
3719 
3720 	/* Group addressed management is not allowed */
3721 	RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED);
3722 
3723 	return 0;
3724 }
3725 
3726 
3727 /*
3728  * wpa_pasn_add_parameter_ie - Add PASN Parameters IE for PASN authentication
3729  * @buf: Buffer in which the IE will be added
3730  * @pasn_group: Finite Cyclic Group ID for PASN authentication
3731  * @wrapped_data_format: Format of the data in the Wrapped Data IE
3732  * @pubkey: A buffer holding the local public key. Can be NULL
3733  * @compressed: In case pubkey is included, indicates if the public key is
3734  *     compressed (only x coordinate is included) or not (both x and y
3735  *     coordinates are included)
3736  * @comeback: A buffer holding the comeback token. Can be NULL
3737  * @after: If comeback is set, defined the comeback time in seconds. -1 to not
3738  *	include the Comeback After field (frames from non-AP STA).
3739  */
wpa_pasn_add_parameter_ie(struct wpabuf * buf,u16 pasn_group,u8 wrapped_data_format,const struct wpabuf * pubkey,bool compressed,const struct wpabuf * comeback,int after)3740 void wpa_pasn_add_parameter_ie(struct wpabuf *buf, u16 pasn_group,
3741 			       u8 wrapped_data_format,
3742 			       const struct wpabuf *pubkey, bool compressed,
3743 			       const struct wpabuf *comeback, int after)
3744 {
3745 	struct pasn_parameter_ie *params;
3746 
3747 	wpa_printf(MSG_DEBUG, "PASN: Add PASN Parameters element");
3748 
3749 	params = wpabuf_put(buf, sizeof(*params));
3750 
3751 	params->id = WLAN_EID_EXTENSION;
3752 	params->len = sizeof(*params) - 2;
3753 	params->id_ext = WLAN_EID_EXT_PASN_PARAMS;
3754 	params->control = 0;
3755 	params->wrapped_data_format = wrapped_data_format;
3756 
3757 	if (comeback) {
3758 		wpa_printf(MSG_DEBUG, "PASN: Adding comeback data");
3759 
3760 		/*
3761 		 * 2 octets for the 'after' field + 1 octet for the length +
3762 		 * actual cookie data
3763 		 */
3764 		if (after >= 0)
3765 			params->len += 2;
3766 		params->len += 1 + wpabuf_len(comeback);
3767 		params->control |= WPA_PASN_CTRL_COMEBACK_INFO_PRESENT;
3768 
3769 		if (after >= 0)
3770 			wpabuf_put_le16(buf, after);
3771 		wpabuf_put_u8(buf, wpabuf_len(comeback));
3772 		wpabuf_put_buf(buf, comeback);
3773 	}
3774 
3775 	if (pubkey) {
3776 		wpa_printf(MSG_DEBUG,
3777 			   "PASN: Adding public key and group ID %u",
3778 			   pasn_group);
3779 
3780 		/*
3781 		 * 2 octets for the finite cyclic group + 2 octets public key
3782 		 * length + 1 octet for the compressed/uncompressed indication +
3783 		 * the actual key.
3784 		 */
3785 		params->len += 2 + 1 + 1 + wpabuf_len(pubkey);
3786 		params->control |= WPA_PASN_CTRL_GROUP_AND_KEY_PRESENT;
3787 
3788 		wpabuf_put_le16(buf, pasn_group);
3789 
3790 		/*
3791 		 * The first octet indicates whether the public key is
3792 		 * compressed, as defined in RFC 5480 section 2.2.
3793 		 */
3794 		wpabuf_put_u8(buf, wpabuf_len(pubkey) + 1);
3795 		wpabuf_put_u8(buf, compressed ? WPA_PASN_PUBKEY_COMPRESSED_0 :
3796 			      WPA_PASN_PUBKEY_UNCOMPRESSED);
3797 
3798 		wpabuf_put_buf(buf, pubkey);
3799 	}
3800 }
3801 
3802 /*
3803  * wpa_pasn_add_wrapped_data - Add a Wrapped Data IE to PASN Authentication
3804  * frame. If needed, the Wrapped Data IE would be fragmented.
3805  *
3806  * @buf: Buffer in which the IE will be added
3807  * @wrapped_data_buf: Buffer holding the wrapped data
3808  */
wpa_pasn_add_wrapped_data(struct wpabuf * buf,struct wpabuf * wrapped_data_buf)3809 int wpa_pasn_add_wrapped_data(struct wpabuf *buf,
3810 			      struct wpabuf *wrapped_data_buf)
3811 {
3812 	const u8 *data;
3813 	size_t data_len;
3814 	u8 len;
3815 
3816 	if (!wrapped_data_buf)
3817 		return 0;
3818 
3819 	wpa_printf(MSG_DEBUG, "PASN: Add wrapped data");
3820 
3821 	data = wpabuf_head_u8(wrapped_data_buf);
3822 	data_len = wpabuf_len(wrapped_data_buf);
3823 
3824 	/* nothing to add */
3825 	if (!data_len)
3826 		return 0;
3827 
3828 	if (data_len <= 254)
3829 		len = 1 + data_len;
3830 	else
3831 		len = 255;
3832 
3833 	if (wpabuf_tailroom(buf) < 3 + data_len)
3834 		return -1;
3835 
3836 	wpabuf_put_u8(buf, WLAN_EID_EXTENSION);
3837 	wpabuf_put_u8(buf, len);
3838 	wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA);
3839 	wpabuf_put_data(buf, data, len - 1);
3840 
3841 	data += len - 1;
3842 	data_len -= len - 1;
3843 
3844 	while (data_len) {
3845 		if (wpabuf_tailroom(buf) < 1 + data_len)
3846 			return -1;
3847 		wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
3848 		len = data_len > 255 ? 255 : data_len;
3849 		wpabuf_put_u8(buf, len);
3850 		wpabuf_put_data(buf, data, len);
3851 		data += len;
3852 		data_len -= len;
3853 	}
3854 
3855 	return 0;
3856 }
3857 
3858 
3859 /*
3860  * wpa_pasn_validate_rsne - Validate PSAN specific data of RSNE
3861  * @data: Parsed representation of an RSNE
3862  * Returns -1 for invalid data; otherwise 0
3863  */
wpa_pasn_validate_rsne(const struct wpa_ie_data * data)3864 int wpa_pasn_validate_rsne(const struct wpa_ie_data *data)
3865 {
3866 	u16 capab = WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR;
3867 
3868 	if (data->proto != WPA_PROTO_RSN)
3869 		return -1;
3870 
3871 	if ((data->capabilities & capab) != capab) {
3872 		wpa_printf(MSG_DEBUG, "PASN: Invalid RSNE capabilities");
3873 		return -1;
3874 	}
3875 
3876 	if (!data->has_group || data->group_cipher != WPA_CIPHER_GTK_NOT_USED) {
3877 		wpa_printf(MSG_DEBUG, "PASN: Invalid group data cipher");
3878 		return -1;
3879 	}
3880 
3881 	if (!data->has_pairwise || !data->pairwise_cipher ||
3882 	    (data->pairwise_cipher & (data->pairwise_cipher - 1))) {
3883 		wpa_printf(MSG_DEBUG, "PASN: No valid pairwise suite");
3884 		return -1;
3885 	}
3886 
3887 	switch (data->key_mgmt) {
3888 #ifdef CONFIG_SAE
3889 	case WPA_KEY_MGMT_SAE:
3890 	/* fall through */
3891 #endif /* CONFIG_SAE */
3892 #ifdef CONFIG_FILS
3893 	case WPA_KEY_MGMT_FILS_SHA256:
3894 	case WPA_KEY_MGMT_FILS_SHA384:
3895 	/* fall through */
3896 #endif /* CONFIG_FILS */
3897 #ifdef CONFIG_IEEE80211R
3898 	case WPA_KEY_MGMT_FT_PSK:
3899 	case WPA_KEY_MGMT_FT_IEEE8021X:
3900 	case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
3901 	/* fall through */
3902 #endif /* CONFIG_IEEE80211R */
3903 	case WPA_KEY_MGMT_PASN:
3904 		break;
3905 	default:
3906 		wpa_printf(MSG_ERROR, "PASN: invalid key_mgmt: 0x%0x",
3907 			   data->key_mgmt);
3908 		return -1;
3909 	}
3910 
3911 	if (data->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED) {
3912 		wpa_printf(MSG_DEBUG, "PASN: Invalid group mgmt cipher");
3913 		return -1;
3914 	}
3915 
3916 	if (data->num_pmkid > 1) {
3917 		wpa_printf(MSG_DEBUG, "PASN: Invalid number of PMKIDs");
3918 		return -1;
3919 	}
3920 
3921 	return 0;
3922 }
3923 
3924 
3925 /*
3926  * wpa_pasn_parse_parameter_ie - Validates PASN Parameters IE
3927  * @data: Pointer to the PASN Parameters IE (starting with the EID).
3928  * @len: Length of the data in the PASN Parameters IE
3929  * @from_ap: Whether this was received from an AP
3930  * @pasn_params: On successful return would hold the parsed PASN parameters.
3931  * Returns: -1 for invalid data; otherwise 0
3932  *
3933  * Note: On successful return, the pointers in &pasn_params point to the data in
3934  * the IE and are not locally allocated (so they should not be freed etc.).
3935  */
wpa_pasn_parse_parameter_ie(const u8 * data,u8 len,bool from_ap,struct wpa_pasn_params_data * pasn_params)3936 int wpa_pasn_parse_parameter_ie(const u8 *data, u8 len, bool from_ap,
3937 				struct wpa_pasn_params_data *pasn_params)
3938 {
3939 	struct pasn_parameter_ie *params = (struct pasn_parameter_ie *) data;
3940 	const u8 *pos = (const u8 *) (params + 1);
3941 
3942 	if (!pasn_params) {
3943 		wpa_printf(MSG_DEBUG, "PASN: Invalid params");
3944 		return -1;
3945 	}
3946 
3947 	if (!params || ((size_t) (params->len + 2) < sizeof(*params)) ||
3948 	    len < sizeof(*params) || params->len + 2 != len) {
3949 		wpa_printf(MSG_DEBUG,
3950 			   "PASN: Invalid parameters IE. len=(%u, %u)",
3951 			   params ? params->len : 0, len);
3952 		return -1;
3953 	}
3954 
3955 	os_memset(pasn_params, 0, sizeof(*pasn_params));
3956 
3957 	switch (params->wrapped_data_format) {
3958 	case WPA_PASN_WRAPPED_DATA_NO:
3959 	case WPA_PASN_WRAPPED_DATA_SAE:
3960 	case WPA_PASN_WRAPPED_DATA_FILS_SK:
3961 	case WPA_PASN_WRAPPED_DATA_FT:
3962 		break;
3963 	default:
3964 		wpa_printf(MSG_DEBUG, "PASN: Invalid wrapped data format");
3965 		return -1;
3966 	}
3967 
3968 	pasn_params->wrapped_data_format = params->wrapped_data_format;
3969 
3970 	len -= sizeof(*params);
3971 
3972 	if (params->control & WPA_PASN_CTRL_COMEBACK_INFO_PRESENT) {
3973 		if (from_ap) {
3974 			if (len < 2) {
3975 				wpa_printf(MSG_DEBUG,
3976 					   "PASN: Invalid Parameters IE: Truncated Comeback After");
3977 				return -1;
3978 			}
3979 			pasn_params->after = WPA_GET_LE16(pos);
3980 			pos += 2;
3981 			len -= 2;
3982 		}
3983 
3984 		if (len < 1 || len < 1 + *pos) {
3985 			wpa_printf(MSG_DEBUG,
3986 				   "PASN: Invalid Parameters IE: comeback len");
3987 			return -1;
3988 		}
3989 
3990 		pasn_params->comeback_len = *pos++;
3991 		len--;
3992 		pasn_params->comeback = pos;
3993 		len -=  pasn_params->comeback_len;
3994 		pos += pasn_params->comeback_len;
3995 	}
3996 
3997 	if (params->control & WPA_PASN_CTRL_GROUP_AND_KEY_PRESENT) {
3998 		if (len < 3 || len < 3 + pos[2]) {
3999 			wpa_printf(MSG_DEBUG,
4000 				   "PASN: Invalid Parameters IE: group and key");
4001 			return -1;
4002 		}
4003 
4004 		pasn_params->group = WPA_GET_LE16(pos);
4005 		pos += 2;
4006 		len -= 2;
4007 		pasn_params->pubkey_len = *pos++;
4008 		len--;
4009 		pasn_params->pubkey = pos;
4010 		len -= pasn_params->pubkey_len;
4011 		pos += pasn_params->pubkey_len;
4012 	}
4013 
4014 	if (len) {
4015 		wpa_printf(MSG_DEBUG,
4016 			   "PASN: Invalid Parameters IE. Bytes left=%u", len);
4017 		return -1;
4018 	}
4019 
4020 	return 0;
4021 }
4022 
4023 
wpa_pasn_add_rsnxe(struct wpabuf * buf,u16 capab)4024 void wpa_pasn_add_rsnxe(struct wpabuf *buf, u16 capab)
4025 {
4026 	size_t flen;
4027 
4028 	flen = (capab & 0xff00) ? 2 : 1;
4029 	if (!capab)
4030 		return; /* no supported extended RSN capabilities */
4031 	if (wpabuf_tailroom(buf) < 2 + flen)
4032 		return;
4033 	capab |= flen - 1; /* bit 0-3 = Field length (n - 1) */
4034 
4035 	wpabuf_put_u8(buf, WLAN_EID_RSNX);
4036 	wpabuf_put_u8(buf, flen);
4037 	wpabuf_put_u8(buf, capab & 0x00ff);
4038 	capab >>= 8;
4039 	if (capab)
4040 		wpabuf_put_u8(buf, capab);
4041 }
4042 
4043 #endif /* CONFIG_PASN */
4044