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