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