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