xref: /aosp_15_r20/external/wpa_supplicant_8/src/rsn_supp/wpa_ft.c (revision 03f9172ca588f91df233974f4258bab95191f931)
1 /*
2  * WPA Supplicant - IEEE 802.11r - Fast BSS Transition
3  * Copyright (c) 2006-2018, Jouni Malinen <[email protected]>
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/aes_wrap.h"
13 #include "crypto/sha384.h"
14 #include "crypto/sha512.h"
15 #include "crypto/random.h"
16 #include "common/ieee802_11_defs.h"
17 #include "common/ieee802_11_common.h"
18 #include "common/ocv.h"
19 #include "common/wpa_ctrl.h"
20 #include "drivers/driver.h"
21 #include "wpa.h"
22 #include "wpa_i.h"
23 #include "wpa_ie.h"
24 #include "pmksa_cache.h"
25 
26 #ifdef CONFIG_IEEE80211R
27 
28 #ifdef CONFIG_PASN
29 static void wpa_ft_pasn_store_r1kh(struct wpa_sm *sm, const u8 *bssid);
30 #else /* CONFIG_PASN */
wpa_ft_pasn_store_r1kh(struct wpa_sm * sm,const u8 * bssid)31 static void wpa_ft_pasn_store_r1kh(struct wpa_sm *sm, const u8 *bssid)
32 {
33 }
34 #endif /* CONFIG_PASN */
35 
36 
wpa_derive_ptk_ft(struct wpa_sm * sm,const unsigned char * src_addr,const struct wpa_eapol_key * key,struct wpa_ptk * ptk)37 int wpa_derive_ptk_ft(struct wpa_sm *sm, const unsigned char *src_addr,
38 		      const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
39 {
40 	u8 ptk_name[WPA_PMK_NAME_LEN];
41 	const u8 *anonce = key->key_nonce;
42 	int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
43 	const u8 *mpmk;
44 	size_t mpmk_len, kdk_len;
45 	int ret = 0;
46 
47 	if (sm->xxkey_len > 0) {
48 		mpmk = sm->xxkey;
49 		mpmk_len = sm->xxkey_len;
50 	} else if (sm->cur_pmksa) {
51 		mpmk = sm->cur_pmksa->pmk;
52 		mpmk_len = sm->cur_pmksa->pmk_len;
53 	} else {
54 		wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
55 			   "derivation");
56 		return -1;
57 	}
58 
59 	if (wpa_key_mgmt_sae_ext_key(sm->key_mgmt))
60 		sm->pmk_r0_len = mpmk_len;
61 	else
62 		sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
63 	if (wpa_derive_pmk_r0(mpmk, mpmk_len, sm->ssid,
64 			      sm->ssid_len, sm->mobility_domain,
65 			      sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
66 			      sm->pmk_r0, sm->pmk_r0_name, sm->key_mgmt) < 0)
67 		return -1;
68 	sm->pmk_r1_len = sm->pmk_r0_len;
69 	if (wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_len, sm->pmk_r0_name,
70 			      sm->r1kh_id, sm->own_addr, sm->pmk_r1,
71 			      sm->pmk_r1_name) < 0)
72 		return -1;
73 
74 	wpa_ft_pasn_store_r1kh(sm, src_addr);
75 
76 	if (sm->force_kdk_derivation ||
77 	    (sm->secure_ltf &&
78 	     ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
79 		kdk_len = WPA_KDK_MAX_LEN;
80 	else
81 		kdk_len = 0;
82 
83 	ret = wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len, sm->snonce,
84 				anonce, sm->own_addr, wpa_sm_get_auth_addr(sm),
85 				sm->pmk_r1_name, ptk, ptk_name, sm->key_mgmt,
86 				sm->pairwise_cipher, kdk_len);
87 	if (ret) {
88 		wpa_printf(MSG_ERROR, "FT: PTK derivation failed");
89 		return ret;
90 	}
91 
92 	os_memcpy(sm->key_mobility_domain, sm->mobility_domain,
93 		  MOBILITY_DOMAIN_ID_LEN);
94 
95 #ifdef CONFIG_PASN
96 	if (sm->secure_ltf &&
97 	    ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF))
98 		ret = wpa_ltf_keyseed(ptk, sm->key_mgmt, sm->pairwise_cipher);
99 #endif /* CONFIG_PASN */
100 
101 	return ret;
102 }
103 
104 
105 /**
106  * wpa_sm_set_ft_params - Set FT (IEEE 802.11r) parameters
107  * @sm: Pointer to WPA state machine data from wpa_sm_init()
108  * @ies: Association Response IEs or %NULL to clear FT parameters
109  * @ies_len: Length of ies buffer in octets
110  * Returns: 0 on success, -1 on failure
111  */
wpa_sm_set_ft_params(struct wpa_sm * sm,const u8 * ies,size_t ies_len)112 int wpa_sm_set_ft_params(struct wpa_sm *sm, const u8 *ies, size_t ies_len)
113 {
114 	struct wpa_ft_ies ft;
115 
116 	if (sm == NULL)
117 		return 0;
118 
119 	if (!get_ie(ies, ies_len, WLAN_EID_MOBILITY_DOMAIN)) {
120 		os_free(sm->assoc_resp_ies);
121 		sm->assoc_resp_ies = NULL;
122 		sm->assoc_resp_ies_len = 0;
123 		os_memset(sm->mobility_domain, 0, MOBILITY_DOMAIN_ID_LEN);
124 		os_memset(sm->r0kh_id, 0, FT_R0KH_ID_MAX_LEN);
125 		sm->r0kh_id_len = 0;
126 		os_memset(sm->r1kh_id, 0, FT_R1KH_ID_LEN);
127 		return 0;
128 	}
129 
130 	if (wpa_ft_parse_ies(ies, ies_len, &ft, sm->key_mgmt, false) < 0)
131 		return -1;
132 
133 	if (ft.mdie_len < MOBILITY_DOMAIN_ID_LEN + 1) {
134 		wpa_ft_parse_ies_free(&ft);
135 		return -1;
136 	}
137 
138 	wpa_hexdump(MSG_DEBUG, "FT: Mobility domain",
139 		    ft.mdie, MOBILITY_DOMAIN_ID_LEN);
140 	os_memcpy(sm->mobility_domain, ft.mdie, MOBILITY_DOMAIN_ID_LEN);
141 	sm->mdie_ft_capab = ft.mdie[MOBILITY_DOMAIN_ID_LEN];
142 	wpa_printf(MSG_DEBUG, "FT: Capability and Policy: 0x%02x",
143 		   sm->mdie_ft_capab);
144 
145 	if (ft.r0kh_id) {
146 		wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID",
147 			    ft.r0kh_id, ft.r0kh_id_len);
148 		os_memcpy(sm->r0kh_id, ft.r0kh_id, ft.r0kh_id_len);
149 		sm->r0kh_id_len = ft.r0kh_id_len;
150 	} else {
151 		/* FIX: When should R0KH-ID be cleared? We need to keep the
152 		 * old R0KH-ID in order to be able to use this during FT. */
153 		/*
154 		 * os_memset(sm->r0kh_id, 0, FT_R0KH_ID_LEN);
155 		 * sm->r0kh_id_len = 0;
156 		 */
157 	}
158 
159 	if (ft.r1kh_id) {
160 		wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID",
161 			    ft.r1kh_id, FT_R1KH_ID_LEN);
162 		os_memcpy(sm->r1kh_id, ft.r1kh_id, FT_R1KH_ID_LEN);
163 	} else
164 		os_memset(sm->r1kh_id, 0, FT_R1KH_ID_LEN);
165 
166 	os_free(sm->assoc_resp_ies);
167 	sm->assoc_resp_ies = os_malloc(ft.mdie_len + 2 + ft.ftie_len + 2);
168 	if (sm->assoc_resp_ies) {
169 		u8 *pos = sm->assoc_resp_ies;
170 
171 		os_memcpy(pos, ft.mdie - 2, ft.mdie_len + 2);
172 		pos += ft.mdie_len + 2;
173 
174 		if (ft.ftie) {
175 			os_memcpy(pos, ft.ftie - 2, ft.ftie_len + 2);
176 			pos += ft.ftie_len + 2;
177 		}
178 		sm->assoc_resp_ies_len = pos - sm->assoc_resp_ies;
179 		wpa_hexdump(MSG_DEBUG, "FT: Stored MDIE and FTIE from "
180 			    "(Re)Association Response",
181 			    sm->assoc_resp_ies, sm->assoc_resp_ies_len);
182 	}
183 
184 	wpa_ft_parse_ies_free(&ft);
185 	return 0;
186 }
187 
188 
189 /**
190  * wpa_ft_gen_req_ies - Generate FT (IEEE 802.11r) IEs for Auth/ReAssoc Request
191  * @sm: Pointer to WPA state machine data from wpa_sm_init()
192  * @len: Buffer for returning the length of the IEs
193  * @anonce: ANonce or %NULL if not yet available
194  * @pmk_name: PMKR0Name or PMKR1Name to be added into the RSN IE PMKID List
195  * @kck: KCK for MIC or %NULL if no MIC is used
196  * @kck_len: KCK length in octets
197  * @target_ap: Target AP address
198  * @ric_ies: Optional IE(s), e.g., WMM TSPEC(s), for RIC-Request or %NULL
199  * @ric_ies_len: Length of ric_ies buffer in octets
200  * @ap_mdie: Mobility Domain IE from the target AP
201  * @omit_rsnxe: Whether RSNXE is omitted from Reassociation Request frame
202  * Returns: Pointer to buffer with IEs or %NULL on failure
203  *
204  * Caller is responsible for freeing the returned buffer with os_free();
205  */
wpa_ft_gen_req_ies(struct wpa_sm * sm,size_t * len,const u8 * anonce,const u8 * pmk_name,const u8 * kck,size_t kck_len,const u8 * target_ap,const u8 * ric_ies,size_t ric_ies_len,const u8 * ap_mdie,int omit_rsnxe)206 static u8 * wpa_ft_gen_req_ies(struct wpa_sm *sm, size_t *len,
207 			       const u8 *anonce, const u8 *pmk_name,
208 			       const u8 *kck, size_t kck_len,
209 			       const u8 *target_ap,
210 			       const u8 *ric_ies, size_t ric_ies_len,
211 			       const u8 *ap_mdie, int omit_rsnxe)
212 {
213 	size_t buf_len;
214 	u8 *buf, *pos, *ftie_len, *ftie_pos, *fte_mic, *elem_count;
215 	struct rsn_mdie *mdie;
216 	struct rsn_ie_hdr *rsnie;
217 	int mdie_len;
218 	u8 rsnxe[10];
219 	size_t rsnxe_len;
220 	int rsnxe_used;
221 	int res;
222 	u8 mic_control;
223 
224 	sm->ft_completed = 0;
225 	sm->ft_reassoc_completed = 0;
226 
227 	buf_len = 2 + sizeof(struct rsn_mdie) + 2 +
228 		sizeof(struct rsn_ftie_sha512) +
229 		2 + sm->r0kh_id_len + ric_ies_len + 100;
230 	buf = os_zalloc(buf_len);
231 	if (buf == NULL)
232 		return NULL;
233 	pos = buf;
234 
235 	/* RSNIE[PMKR0Name/PMKR1Name] */
236 	rsnie = (struct rsn_ie_hdr *) pos;
237 	rsnie->elem_id = WLAN_EID_RSN;
238 	WPA_PUT_LE16(rsnie->version, RSN_VERSION);
239 	pos = (u8 *) (rsnie + 1);
240 
241 	/* Group Suite Selector */
242 	if (!wpa_cipher_valid_group(sm->group_cipher)) {
243 		wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
244 			   sm->group_cipher);
245 		os_free(buf);
246 		return NULL;
247 	}
248 	RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
249 						  sm->group_cipher));
250 	pos += RSN_SELECTOR_LEN;
251 
252 	/* Pairwise Suite Count */
253 	WPA_PUT_LE16(pos, 1);
254 	pos += 2;
255 
256 	/* Pairwise Suite List */
257 	if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
258 		wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
259 			   sm->pairwise_cipher);
260 		os_free(buf);
261 		return NULL;
262 	}
263 	RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
264 						  sm->pairwise_cipher));
265 	pos += RSN_SELECTOR_LEN;
266 
267 	/* Authenticated Key Management Suite Count */
268 	WPA_PUT_LE16(pos, 1);
269 	pos += 2;
270 
271 	/* Authenticated Key Management Suite List */
272 	if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X)
273 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
274 #ifdef CONFIG_SHA384
275 	else if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X_SHA384)
276 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384);
277 #endif /* CONFIG_SHA384 */
278 	else if (sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
279 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
280 	else if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE)
281 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
282 	else if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY)
283 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY);
284 #ifdef CONFIG_FILS
285 	else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
286 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
287 	else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
288 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
289 #endif /* CONFIG_FILS */
290 	else {
291 		wpa_printf(MSG_WARNING, "FT: Invalid key management type (%d)",
292 			   sm->key_mgmt);
293 		os_free(buf);
294 		return NULL;
295 	}
296 	pos += RSN_SELECTOR_LEN;
297 
298 	/* RSN Capabilities */
299 	WPA_PUT_LE16(pos, rsn_supp_capab(sm));
300 	pos += 2;
301 
302 	/* PMKID Count */
303 	WPA_PUT_LE16(pos, 1);
304 	pos += 2;
305 
306 	/* PMKID List [PMKR0Name/PMKR1Name] */
307 	os_memcpy(pos, pmk_name, WPA_PMK_NAME_LEN);
308 	pos += WPA_PMK_NAME_LEN;
309 
310 	/* Management Group Cipher Suite */
311 	switch (sm->mgmt_group_cipher) {
312 	case WPA_CIPHER_AES_128_CMAC:
313 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
314 		pos += RSN_SELECTOR_LEN;
315 		break;
316 	case WPA_CIPHER_BIP_GMAC_128:
317 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_128);
318 		pos += RSN_SELECTOR_LEN;
319 		break;
320 	case WPA_CIPHER_BIP_GMAC_256:
321 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_256);
322 		pos += RSN_SELECTOR_LEN;
323 		break;
324 	case WPA_CIPHER_BIP_CMAC_256:
325 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_CMAC_256);
326 		pos += RSN_SELECTOR_LEN;
327 		break;
328 	}
329 
330 	rsnie->len = (pos - (u8 *) rsnie) - 2;
331 
332 	/* MDIE */
333 	mdie_len = wpa_ft_add_mdie(sm, pos, buf_len - (pos - buf), ap_mdie);
334 	if (mdie_len <= 0) {
335 		os_free(buf);
336 		return NULL;
337 	}
338 	mdie = (struct rsn_mdie *) (pos + 2);
339 	pos += mdie_len;
340 
341 	/* FTIE[SNonce, [R1KH-ID,] R0KH-ID ] */
342 	ftie_pos = pos;
343 	*pos++ = WLAN_EID_FAST_BSS_TRANSITION;
344 	ftie_len = pos++;
345 	rsnxe_used = wpa_key_mgmt_sae(sm->key_mgmt) && anonce &&
346 		(sm->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
347 		 sm->sae_pwe == SAE_PWE_BOTH);
348 #ifdef CONFIG_TESTING_OPTIONS
349 	if (anonce && sm->ft_rsnxe_used) {
350 		rsnxe_used = sm->ft_rsnxe_used == 1;
351 		wpa_printf(MSG_DEBUG, "TESTING: FT: Force RSNXE Used %d",
352 			   rsnxe_used);
353 	}
354 #endif /* CONFIG_TESTING_OPTIONS */
355 	mic_control = rsnxe_used ? FTE_MIC_CTRL_RSNXE_USED : 0;
356 	if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
357 	    sm->pmk_r0_len == SHA512_MAC_LEN) {
358 		struct rsn_ftie_sha512 *ftie;
359 
360 		ftie = (struct rsn_ftie_sha512 *) pos;
361 		mic_control |= FTE_MIC_LEN_32 << FTE_MIC_CTRL_MIC_LEN_SHIFT;
362 		ftie->mic_control[0] = mic_control;
363 		fte_mic = ftie->mic;
364 		elem_count = &ftie->mic_control[1];
365 		pos += sizeof(*ftie);
366 		os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
367 		if (anonce)
368 			os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
369 	} else if ((sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
370 		    sm->pmk_r0_len == SHA384_MAC_LEN) ||
371 		   wpa_key_mgmt_sha384(sm->key_mgmt)) {
372 		struct rsn_ftie_sha384 *ftie;
373 
374 		ftie = (struct rsn_ftie_sha384 *) pos;
375 		mic_control |= FTE_MIC_LEN_24 << FTE_MIC_CTRL_MIC_LEN_SHIFT;
376 		ftie->mic_control[0] = mic_control;
377 		fte_mic = ftie->mic;
378 		elem_count = &ftie->mic_control[1];
379 		pos += sizeof(*ftie);
380 		os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
381 		if (anonce)
382 			os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
383 	} else {
384 		struct rsn_ftie *ftie;
385 
386 		ftie = (struct rsn_ftie *) pos;
387 		mic_control |= FTE_MIC_LEN_16 << FTE_MIC_CTRL_MIC_LEN_SHIFT;
388 		ftie->mic_control[0] = mic_control;
389 		fte_mic = ftie->mic;
390 		elem_count = &ftie->mic_control[1];
391 		pos += sizeof(*ftie);
392 		os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
393 		if (anonce)
394 			os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
395 	}
396 	if (kck) {
397 		/* R1KH-ID sub-element in third FT message */
398 		*pos++ = FTIE_SUBELEM_R1KH_ID;
399 		*pos++ = FT_R1KH_ID_LEN;
400 		os_memcpy(pos, sm->r1kh_id, FT_R1KH_ID_LEN);
401 		pos += FT_R1KH_ID_LEN;
402 	}
403 	/* R0KH-ID sub-element */
404 	*pos++ = FTIE_SUBELEM_R0KH_ID;
405 	*pos++ = sm->r0kh_id_len;
406 	os_memcpy(pos, sm->r0kh_id, sm->r0kh_id_len);
407 	pos += sm->r0kh_id_len;
408 #ifdef CONFIG_OCV
409 	if (kck && wpa_sm_ocv_enabled(sm)) {
410 		/* OCI sub-element in the third FT message */
411 		struct wpa_channel_info ci;
412 
413 		if (wpa_sm_channel_info(sm, &ci) != 0) {
414 			wpa_printf(MSG_WARNING,
415 				   "Failed to get channel info for OCI element in FTE");
416 			os_free(buf);
417 			return NULL;
418 		}
419 #ifdef CONFIG_TESTING_OPTIONS
420 		if (sm->oci_freq_override_ft_assoc) {
421 			wpa_printf(MSG_INFO,
422 				   "TEST: Override OCI KDE frequency %d -> %d MHz",
423 				   ci.frequency, sm->oci_freq_override_ft_assoc);
424 			ci.frequency = sm->oci_freq_override_ft_assoc;
425 		}
426 #endif /* CONFIG_TESTING_OPTIONS */
427 
428 		*pos++ = FTIE_SUBELEM_OCI;
429 		*pos++ = OCV_OCI_LEN;
430 		if (ocv_insert_oci(&ci, &pos) < 0) {
431 			os_free(buf);
432 			return NULL;
433 		}
434 	}
435 #endif /* CONFIG_OCV */
436 	*ftie_len = pos - ftie_len - 1;
437 
438 	if (ric_ies) {
439 		/* RIC Request */
440 		os_memcpy(pos, ric_ies, ric_ies_len);
441 		pos += ric_ies_len;
442 	}
443 
444 	if (omit_rsnxe) {
445 		rsnxe_len = 0;
446 	} else {
447 		res = wpa_gen_rsnxe(sm, rsnxe, sizeof(rsnxe));
448 		if (res < 0) {
449 			os_free(buf);
450 			return NULL;
451 		}
452 		rsnxe_len = res;
453 	}
454 
455 	if (kck) {
456 		/*
457 		 * IEEE Std 802.11r-2008, 11A.8.4
458 		 * MIC shall be calculated over:
459 		 * non-AP STA MAC address
460 		 * Target AP MAC address
461 		 * Transaction seq number (5 for ReassocReq, 3 otherwise)
462 		 * RSN IE
463 		 * MDIE
464 		 * FTIE (with MIC field set to 0)
465 		 * RIC-Request (if present)
466 		 * RSNXE (if present)
467 		 */
468 		/* Information element count */
469 		*elem_count = 3 + ieee802_11_ie_count(ric_ies, ric_ies_len);
470 		if (rsnxe_len)
471 			*elem_count += 1;
472 		if (wpa_ft_mic(sm->key_mgmt, kck, kck_len,
473 			       sm->own_addr, target_ap, 5,
474 			       ((u8 *) mdie) - 2, 2 + sizeof(*mdie),
475 			       ftie_pos, 2 + *ftie_len,
476 			       (u8 *) rsnie, 2 + rsnie->len, ric_ies,
477 			       ric_ies_len, rsnxe_len ? rsnxe : NULL, rsnxe_len,
478 			       NULL,
479 			       fte_mic) < 0) {
480 			wpa_printf(MSG_INFO, "FT: Failed to calculate MIC");
481 			os_free(buf);
482 			return NULL;
483 		}
484 	}
485 
486 	*len = pos - buf;
487 
488 	return buf;
489 }
490 
491 
wpa_ft_install_ptk(struct wpa_sm * sm,const u8 * bssid)492 static int wpa_ft_install_ptk(struct wpa_sm *sm, const u8 *bssid)
493 {
494 	int keylen;
495 	enum wpa_alg alg;
496 	u8 null_rsc[6] = { 0, 0, 0, 0, 0, 0 };
497 
498 	wpa_printf(MSG_DEBUG, "FT: Installing PTK to the driver.");
499 
500 	if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
501 		wpa_printf(MSG_WARNING, "FT: Unsupported pairwise cipher %d",
502 			   sm->pairwise_cipher);
503 		return -1;
504 	}
505 
506 	alg = wpa_cipher_to_alg(sm->pairwise_cipher);
507 	keylen = wpa_cipher_key_len(sm->pairwise_cipher);
508 
509 	/* TODO: AP MLD address for MLO */
510 	if (wpa_sm_set_key(sm, -1, alg, bssid, 0, 1, null_rsc, sizeof(null_rsc),
511 			   (u8 *) sm->ptk.tk, keylen,
512 			   KEY_FLAG_PAIRWISE_RX_TX) < 0) {
513 		wpa_printf(MSG_WARNING, "FT: Failed to set PTK to the driver");
514 		return -1;
515 	}
516 	sm->tk_set = true;
517 
518 	wpa_sm_store_ptk(sm, bssid, sm->pairwise_cipher,
519 			 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
520 	return 0;
521 }
522 
523 
524 /**
525  * wpa_ft_prepare_auth_request - Generate over-the-air auth request
526  * @sm: Pointer to WPA state machine data from wpa_sm_init()
527  * @mdie: Target AP MDIE
528  * Returns: 0 on success, -1 on failure
529  */
wpa_ft_prepare_auth_request(struct wpa_sm * sm,const u8 * mdie)530 int wpa_ft_prepare_auth_request(struct wpa_sm *sm, const u8 *mdie)
531 {
532 	u8 *ft_ies;
533 	size_t ft_ies_len;
534 
535 	/* Generate a new SNonce */
536 	if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
537 		wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
538 		return -1;
539 	}
540 
541 	ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
542 				    NULL, 0, sm->bssid, NULL, 0, mdie, 0);
543 	if (ft_ies) {
544 		wpa_sm_update_ft_ies(sm, sm->mobility_domain,
545 				     ft_ies, ft_ies_len);
546 		os_free(ft_ies);
547 	}
548 
549 	return 0;
550 }
551 
552 
wpa_ft_add_mdie(struct wpa_sm * sm,u8 * buf,size_t buf_len,const u8 * ap_mdie)553 int wpa_ft_add_mdie(struct wpa_sm *sm, u8 *buf, size_t buf_len,
554 		    const u8 *ap_mdie)
555 {
556 	u8 *pos = buf;
557 	struct rsn_mdie *mdie;
558 
559 	if (buf_len < 2 + sizeof(*mdie)) {
560 		wpa_printf(MSG_INFO,
561 			   "FT: Failed to add MDIE: short buffer, length=%zu",
562 			   buf_len);
563 		return 0;
564 	}
565 
566 	*pos++ = WLAN_EID_MOBILITY_DOMAIN;
567 	*pos++ = sizeof(*mdie);
568 	mdie = (struct rsn_mdie *) pos;
569 	os_memcpy(mdie->mobility_domain, sm->mobility_domain,
570 		  MOBILITY_DOMAIN_ID_LEN);
571 	mdie->ft_capab = ap_mdie && ap_mdie[1] >= 3 ? ap_mdie[4] :
572 		sm->mdie_ft_capab;
573 
574 	return 2 + sizeof(*mdie);
575 }
576 
577 
wpa_sm_get_ft_md(struct wpa_sm * sm)578 const u8 * wpa_sm_get_ft_md(struct wpa_sm *sm)
579 {
580 	return sm->mobility_domain;
581 }
582 
583 
wpa_ft_process_response(struct wpa_sm * sm,const u8 * ies,size_t ies_len,int ft_action,const u8 * target_ap,const u8 * ric_ies,size_t ric_ies_len)584 int wpa_ft_process_response(struct wpa_sm *sm, const u8 *ies, size_t ies_len,
585 			    int ft_action, const u8 *target_ap,
586 			    const u8 *ric_ies, size_t ric_ies_len)
587 {
588 	u8 *ft_ies;
589 	size_t ft_ies_len;
590 	struct wpa_ft_ies parse;
591 	struct rsn_mdie *mdie;
592 	u8 ptk_name[WPA_PMK_NAME_LEN];
593 	int ret = -1, res;
594 	const u8 *bssid;
595 	const u8 *kck;
596 	size_t kck_len, kdk_len;
597 
598 	os_memset(&parse, 0, sizeof(parse));
599 
600 	wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
601 	wpa_hexdump(MSG_DEBUG, "FT: RIC IEs", ric_ies, ric_ies_len);
602 
603 	if (ft_action) {
604 		if (!sm->over_the_ds_in_progress) {
605 			wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
606 				   "- drop FT Action Response");
607 			goto fail;
608 		}
609 
610 		if (!ether_addr_equal(target_ap, sm->target_ap)) {
611 			wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
612 				   "with this Target AP - drop FT Action "
613 				   "Response");
614 			goto fail;
615 		}
616 	}
617 
618 	if (!wpa_key_mgmt_ft(sm->key_mgmt)) {
619 		wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
620 			   "enabled for this connection");
621 		goto fail;
622 	}
623 
624 	if (wpa_ft_parse_ies(ies, ies_len, &parse, sm->key_mgmt,
625 			     !ft_action) < 0) {
626 		wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
627 		goto fail;
628 	}
629 
630 	mdie = (struct rsn_mdie *) parse.mdie;
631 	if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
632 	    os_memcmp(mdie->mobility_domain, sm->mobility_domain,
633 		      MOBILITY_DOMAIN_ID_LEN) != 0) {
634 		wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
635 		goto fail;
636 	}
637 
638 	if (!parse.ftie || !parse.fte_anonce || !parse.fte_snonce) {
639 		wpa_printf(MSG_DEBUG, "FT: Invalid FTE");
640 		goto fail;
641 	}
642 
643 	if (os_memcmp(parse.fte_snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
644 		wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
645 		wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
646 			    parse.fte_snonce, WPA_NONCE_LEN);
647 		wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
648 			    sm->snonce, WPA_NONCE_LEN);
649 		goto fail;
650 	}
651 
652 	if (parse.r0kh_id == NULL) {
653 		wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
654 		goto fail;
655 	}
656 
657 	if (parse.r0kh_id_len != sm->r0kh_id_len ||
658 	    os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
659 	{
660 		wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
661 			   "the current R0KH-ID");
662 		wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
663 			    parse.r0kh_id, parse.r0kh_id_len);
664 		wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
665 			    sm->r0kh_id, sm->r0kh_id_len);
666 		goto fail;
667 	}
668 
669 	if (parse.r1kh_id == NULL) {
670 		wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
671 		goto fail;
672 	}
673 
674 	if (parse.rsn_pmkid == NULL ||
675 	    os_memcmp_const(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN))
676 	{
677 		wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name (PMKID) in "
678 			   "RSNIE");
679 		goto fail;
680 	}
681 
682 	if (sm->mfp == 2 && !(parse.rsn_capab & WPA_CAPABILITY_MFPC)) {
683 		wpa_printf(MSG_INFO,
684 			   "FT: Target AP does not support PMF, but local configuration requires that");
685 		goto fail;
686 	}
687 
688 	os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
689 	wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", sm->r1kh_id, FT_R1KH_ID_LEN);
690 	wpa_hexdump(MSG_DEBUG, "FT: SNonce", sm->snonce, WPA_NONCE_LEN);
691 	wpa_hexdump(MSG_DEBUG, "FT: ANonce", parse.fte_anonce, WPA_NONCE_LEN);
692 	os_memcpy(sm->anonce, parse.fte_anonce, WPA_NONCE_LEN);
693 	if (wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_len, sm->pmk_r0_name,
694 			      sm->r1kh_id, sm->own_addr, sm->pmk_r1,
695 			      sm->pmk_r1_name) < 0)
696 		goto fail;
697 	sm->pmk_r1_len = sm->pmk_r0_len;
698 
699 	bssid = target_ap;
700 
701 	wpa_ft_pasn_store_r1kh(sm, bssid);
702 
703 	if (sm->force_kdk_derivation ||
704 	    (sm->secure_ltf &&
705 	     ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
706 		kdk_len = WPA_KDK_MAX_LEN;
707 	else
708 		kdk_len = 0;
709 
710 	/* TODO: AP MLD address for MLO */
711 	if (wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len, sm->snonce,
712 			      parse.fte_anonce, sm->own_addr, bssid,
713 			      sm->pmk_r1_name, &sm->ptk, ptk_name, sm->key_mgmt,
714 			      sm->pairwise_cipher,
715 			      kdk_len) < 0)
716 		goto fail;
717 
718 	os_memcpy(sm->key_mobility_domain, sm->mobility_domain,
719 		  MOBILITY_DOMAIN_ID_LEN);
720 
721 #ifdef CONFIG_PASN
722 	if (sm->secure_ltf &&
723 	    ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
724 	    wpa_ltf_keyseed(&sm->ptk, sm->key_mgmt, sm->pairwise_cipher)) {
725 		wpa_printf(MSG_DEBUG, "FT: Failed to derive LTF keyseed");
726 		goto fail;
727 	}
728 #endif /* CONFIG_PASN */
729 
730 	if (wpa_key_mgmt_fils(sm->key_mgmt)) {
731 		kck = sm->ptk.kck2;
732 		kck_len = sm->ptk.kck2_len;
733 	} else {
734 		kck = sm->ptk.kck;
735 		kck_len = sm->ptk.kck_len;
736 	}
737 	ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, parse.fte_anonce,
738 				    sm->pmk_r1_name,
739 				    kck, kck_len, bssid,
740 				    ric_ies, ric_ies_len,
741 				    parse.mdie ? parse.mdie - 2 : NULL,
742 				    !sm->ap_rsnxe);
743 	if (ft_ies) {
744 		wpa_sm_update_ft_ies(sm, sm->mobility_domain,
745 				     ft_ies, ft_ies_len);
746 		os_free(ft_ies);
747 	}
748 
749 	wpa_sm_mark_authenticated(sm, bssid);
750 	res = wpa_ft_install_ptk(sm, bssid);
751 	if (res) {
752 		/*
753 		 * Some drivers do not support key configuration when we are
754 		 * not associated with the target AP. Work around this by
755 		 * trying again after the following reassociation gets
756 		 * completed.
757 		 */
758 		wpa_printf(MSG_DEBUG, "FT: Failed to set PTK prior to "
759 			   "association - try again after reassociation");
760 		sm->set_ptk_after_assoc = 1;
761 	} else
762 		sm->set_ptk_after_assoc = 0;
763 
764 	sm->ft_completed = 1;
765 	if (ft_action) {
766 		/*
767 		 * The caller is expected trigger re-association with the
768 		 * Target AP.
769 		 */
770 		os_memcpy(sm->bssid, target_ap, ETH_ALEN);
771 	}
772 
773 	ret = 0;
774 fail:
775 	wpa_ft_parse_ies_free(&parse);
776 	return ret;
777 }
778 
779 
wpa_ft_is_completed(struct wpa_sm * sm)780 int wpa_ft_is_completed(struct wpa_sm *sm)
781 {
782 	if (sm == NULL)
783 		return 0;
784 
785 	if (!wpa_key_mgmt_ft(sm->key_mgmt))
786 		return 0;
787 
788 	return sm->ft_completed;
789 }
790 
791 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
wpa_ft_is_ft_protocol(struct wpa_sm * sm)792 int wpa_ft_is_ft_protocol(struct wpa_sm *sm)
793 {
794 	if (sm == NULL)
795 		return 0;
796 
797 	if (!wpa_key_mgmt_ft(sm->key_mgmt))
798 		return 0;
799 
800 	return sm->ft_protocol;
801 }
802 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
803 
wpa_reset_ft_completed(struct wpa_sm * sm)804 void wpa_reset_ft_completed(struct wpa_sm *sm)
805 {
806 	if (sm != NULL)
807 		sm->ft_completed = 0;
808 }
809 
810 
wpa_set_ft_completed(struct wpa_sm * sm)811 void wpa_set_ft_completed(struct wpa_sm *sm)
812 {
813 	if (sm != NULL)
814 		sm->ft_completed = 1;
815 }
816 
wpa_ft_process_gtk_subelem(struct wpa_sm * sm,const u8 * gtk_elem,size_t gtk_elem_len)817 static int wpa_ft_process_gtk_subelem(struct wpa_sm *sm, const u8 *gtk_elem,
818 				      size_t gtk_elem_len)
819 {
820 	u8 gtk[32];
821 	int keyidx;
822 	enum wpa_alg alg;
823 	size_t gtk_len, keylen, rsc_len;
824 	const u8 *kek;
825 	size_t kek_len;
826 
827 	if (wpa_key_mgmt_fils(sm->key_mgmt)) {
828 		kek = sm->ptk.kek2;
829 		kek_len = sm->ptk.kek2_len;
830 	} else {
831 		kek = sm->ptk.kek;
832 		kek_len = sm->ptk.kek_len;
833 	}
834 
835 	if (gtk_elem == NULL) {
836 		wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
837 		return 0;
838 	}
839 
840 	wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
841 			gtk_elem, gtk_elem_len);
842 
843 	if (gtk_elem_len < 11 + 24 || (gtk_elem_len - 11) % 8 ||
844 	    gtk_elem_len - 19 > sizeof(gtk)) {
845 		wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
846 			   "length %lu", (unsigned long) gtk_elem_len);
847 		return -1;
848 	}
849 	gtk_len = gtk_elem_len - 19;
850 	if (aes_unwrap(kek, kek_len, gtk_len / 8, gtk_elem + 11, gtk)) {
851 		wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
852 			   "decrypt GTK");
853 		return -1;
854 	}
855 
856 	keylen = wpa_cipher_key_len(sm->group_cipher);
857 	rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
858 	alg = wpa_cipher_to_alg(sm->group_cipher);
859 	if (alg == WPA_ALG_NONE) {
860 		wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
861 			   sm->group_cipher);
862 		return -1;
863 	}
864 
865 	if (gtk_len < keylen) {
866 		wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
867 		return -1;
868 	}
869 
870 	/* Key Info[2] | Key Length[1] | RSC[8] | Key[5..32]. */
871 
872 	keyidx = WPA_GET_LE16(gtk_elem) & 0x03;
873 
874 	if (gtk_elem[2] != keylen) {
875 		wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
876 			   "negotiated %lu",
877 			   gtk_elem[2], (unsigned long) keylen);
878 		return -1;
879 	}
880 
881 	wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
882 	if (sm->group_cipher == WPA_CIPHER_TKIP) {
883 		/* Swap Tx/Rx keys for Michael MIC */
884 		u8 tmp[8];
885 		os_memcpy(tmp, gtk + 16, 8);
886 		os_memcpy(gtk + 16, gtk + 24, 8);
887 		os_memcpy(gtk + 24, tmp, 8);
888 	}
889 	if (wpa_sm_set_key(sm, -1, alg, broadcast_ether_addr, keyidx, 0,
890 			   gtk_elem + 3, rsc_len, gtk, keylen,
891 			   KEY_FLAG_GROUP_RX) < 0) {
892 		wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
893 			   "driver.");
894 		return -1;
895 	}
896 
897 	return 0;
898 }
899 
900 
wpa_ft_process_igtk_subelem(struct wpa_sm * sm,const u8 * igtk_elem,size_t igtk_elem_len)901 static int wpa_ft_process_igtk_subelem(struct wpa_sm *sm, const u8 *igtk_elem,
902 				       size_t igtk_elem_len)
903 {
904 	u8 igtk[WPA_IGTK_MAX_LEN];
905 	size_t igtk_len;
906 	u16 keyidx;
907 	const u8 *kek;
908 	size_t kek_len;
909 
910 	if (wpa_key_mgmt_fils(sm->key_mgmt)) {
911 		kek = sm->ptk.kek2;
912 		kek_len = sm->ptk.kek2_len;
913 	} else {
914 		kek = sm->ptk.kek;
915 		kek_len = sm->ptk.kek_len;
916 	}
917 
918 	if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC &&
919 	    sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_128 &&
920 	    sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_256 &&
921 	    sm->mgmt_group_cipher != WPA_CIPHER_BIP_CMAC_256)
922 		return 0;
923 
924 	if (igtk_elem == NULL) {
925 		wpa_printf(MSG_DEBUG, "FT: No IGTK included in FTIE");
926 		return 0;
927 	}
928 
929 	wpa_hexdump_key(MSG_DEBUG, "FT: Received IGTK in Reassoc Resp",
930 			igtk_elem, igtk_elem_len);
931 
932 	igtk_len = wpa_cipher_key_len(sm->mgmt_group_cipher);
933 	if (igtk_elem_len != 2 + 6 + 1 + igtk_len + 8) {
934 		wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem "
935 			   "length %lu", (unsigned long) igtk_elem_len);
936 		return -1;
937 	}
938 	if (igtk_elem[8] != igtk_len) {
939 		wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem Key Length "
940 			   "%d", igtk_elem[8]);
941 		return -1;
942 	}
943 
944 	if (aes_unwrap(kek, kek_len, igtk_len / 8, igtk_elem + 9, igtk)) {
945 		wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
946 			   "decrypt IGTK");
947 		return -1;
948 	}
949 
950 	/* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
951 
952 	keyidx = WPA_GET_LE16(igtk_elem);
953 
954 	wpa_hexdump_key(MSG_DEBUG, "FT: IGTK from Reassoc Resp", igtk,
955 			igtk_len);
956 	if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
957 			   broadcast_ether_addr, keyidx, 0,
958 			   igtk_elem + 2, 6, igtk, igtk_len,
959 			   KEY_FLAG_GROUP_RX) < 0) {
960 		wpa_printf(MSG_WARNING, "WPA: Failed to set IGTK to the "
961 			   "driver.");
962 		forced_memzero(igtk, sizeof(igtk));
963 		return -1;
964 	}
965 	forced_memzero(igtk, sizeof(igtk));
966 
967 	return 0;
968 }
969 
970 
wpa_ft_process_bigtk_subelem(struct wpa_sm * sm,const u8 * bigtk_elem,size_t bigtk_elem_len)971 static int wpa_ft_process_bigtk_subelem(struct wpa_sm *sm, const u8 *bigtk_elem,
972 				       size_t bigtk_elem_len)
973 {
974 	u8 bigtk[WPA_BIGTK_MAX_LEN];
975 	size_t bigtk_len;
976 	u16 keyidx;
977 	const u8 *kek;
978 	size_t kek_len;
979 
980 	if (!sm->beacon_prot || !bigtk_elem ||
981 	    (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC &&
982 	     sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_128 &&
983 	     sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_256 &&
984 	     sm->mgmt_group_cipher != WPA_CIPHER_BIP_CMAC_256))
985 		return 0;
986 
987 	if (wpa_key_mgmt_fils(sm->key_mgmt)) {
988 		kek = sm->ptk.kek2;
989 		kek_len = sm->ptk.kek2_len;
990 	} else {
991 		kek = sm->ptk.kek;
992 		kek_len = sm->ptk.kek_len;
993 	}
994 
995 	wpa_hexdump_key(MSG_DEBUG, "FT: Received BIGTK in Reassoc Resp",
996 			bigtk_elem, bigtk_elem_len);
997 
998 	bigtk_len = wpa_cipher_key_len(sm->mgmt_group_cipher);
999 	if (bigtk_elem_len != 2 + 6 + 1 + bigtk_len + 8) {
1000 		wpa_printf(MSG_DEBUG,
1001 			   "FT: Invalid BIGTK sub-elem length %lu",
1002 			   (unsigned long) bigtk_elem_len);
1003 		return -1;
1004 	}
1005 	if (bigtk_elem[8] != bigtk_len) {
1006 		wpa_printf(MSG_DEBUG,
1007 			   "FT: Invalid BIGTK sub-elem Key Length %d",
1008 			   bigtk_elem[8]);
1009 		return -1;
1010 	}
1011 
1012 	if (aes_unwrap(kek, kek_len, bigtk_len / 8, bigtk_elem + 9, bigtk)) {
1013 		wpa_printf(MSG_WARNING,
1014 			   "FT: AES unwrap failed - could not decrypt BIGTK");
1015 		return -1;
1016 	}
1017 
1018 	/* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
1019 
1020 	keyidx = WPA_GET_LE16(bigtk_elem);
1021 
1022 	wpa_hexdump_key(MSG_DEBUG, "FT: BIGTK from Reassoc Resp", bigtk,
1023 			bigtk_len);
1024 	if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1025 			   broadcast_ether_addr, keyidx, 0,
1026 			   bigtk_elem + 2, 6, bigtk, bigtk_len,
1027 			   KEY_FLAG_GROUP_RX) < 0) {
1028 		wpa_printf(MSG_WARNING,
1029 			   "WPA: Failed to set BIGTK to the driver");
1030 		forced_memzero(bigtk, sizeof(bigtk));
1031 		return -1;
1032 	}
1033 	forced_memzero(bigtk, sizeof(bigtk));
1034 
1035 	return 0;
1036 }
1037 
1038 
wpa_ft_validate_reassoc_resp(struct wpa_sm * sm,const u8 * ies,size_t ies_len,const u8 * src_addr)1039 int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
1040 				 size_t ies_len, const u8 *src_addr)
1041 {
1042 	struct wpa_ft_ies parse;
1043 	struct rsn_mdie *mdie;
1044 	unsigned int count;
1045 	u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
1046 	const u8 *kck;
1047 	size_t kck_len;
1048 	int own_rsnxe_used;
1049 	size_t mic_len;
1050 	int ret = -1;
1051 
1052 	os_memset(&parse, 0, sizeof(parse));
1053 
1054 	wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
1055 
1056 	if (!wpa_key_mgmt_ft(sm->key_mgmt)) {
1057 		wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
1058 			   "enabled for this connection");
1059 		goto fail;
1060 	}
1061 
1062 	if (sm->ft_reassoc_completed) {
1063 		wpa_printf(MSG_DEBUG, "FT: Reassociation has already been completed for this FT protocol instance - ignore unexpected retransmission");
1064 		return 0;
1065 	}
1066 
1067 	if (wpa_ft_parse_ies(ies, ies_len, &parse, sm->key_mgmt, true) < 0) {
1068 		wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
1069 		goto fail;
1070 	}
1071 
1072 	mdie = (struct rsn_mdie *) parse.mdie;
1073 	if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
1074 	    os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1075 		      MOBILITY_DOMAIN_ID_LEN) != 0) {
1076 		wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
1077 		goto fail;
1078 	}
1079 
1080 	if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
1081 	    sm->pmk_r1_len == SHA512_MAC_LEN)
1082 		mic_len = 32;
1083 	else if ((sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
1084 		  sm->pmk_r1_len == SHA384_MAC_LEN) ||
1085 		 wpa_key_mgmt_sha384(sm->key_mgmt))
1086 		mic_len = 24;
1087 	else
1088 		mic_len = 16;
1089 
1090 	if (!parse.ftie || !parse.fte_anonce || !parse.fte_snonce ||
1091 	    parse.fte_mic_len != mic_len) {
1092 		wpa_printf(MSG_DEBUG,
1093 			   "FT: Invalid FTE (fte_mic_len=%zu mic_len=%zu)",
1094 			   parse.fte_mic_len, mic_len);
1095 		goto fail;
1096 	}
1097 
1098 	if (os_memcmp(parse.fte_snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
1099 		wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
1100 		wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
1101 			    parse.fte_snonce, WPA_NONCE_LEN);
1102 		wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
1103 			    sm->snonce, WPA_NONCE_LEN);
1104 		goto fail;
1105 	}
1106 
1107 	if (os_memcmp(parse.fte_anonce, sm->anonce, WPA_NONCE_LEN) != 0) {
1108 		wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
1109 		wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
1110 			    parse.fte_anonce, WPA_NONCE_LEN);
1111 		wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
1112 			    sm->anonce, WPA_NONCE_LEN);
1113 		goto fail;
1114 	}
1115 
1116 	if (parse.r0kh_id == NULL) {
1117 		wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
1118 		goto fail;
1119 	}
1120 
1121 	if (parse.r0kh_id_len != sm->r0kh_id_len ||
1122 	    os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
1123 	{
1124 		wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
1125 			   "the current R0KH-ID");
1126 		wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
1127 			    parse.r0kh_id, parse.r0kh_id_len);
1128 		wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
1129 			    sm->r0kh_id, sm->r0kh_id_len);
1130 		goto fail;
1131 	}
1132 
1133 	if (parse.r1kh_id == NULL) {
1134 		wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
1135 		goto fail;
1136 	}
1137 
1138 	if (os_memcmp_const(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
1139 		wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
1140 			   "ReassocResp");
1141 		goto fail;
1142 	}
1143 
1144 	if (parse.rsn_pmkid == NULL ||
1145 	    os_memcmp_const(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN))
1146 	{
1147 		wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
1148 			   "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
1149 		goto fail;
1150 	}
1151 
1152 	count = 3;
1153 	if (parse.ric)
1154 		count += ieee802_11_ie_count(parse.ric, parse.ric_len);
1155 	if (parse.rsnxe)
1156 		count++;
1157 	if (parse.fte_elem_count != count) {
1158 		wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
1159 			   "Control: received %u expected %u",
1160 			   parse.fte_elem_count, count);
1161 		goto fail;
1162 	}
1163 
1164 	if (wpa_key_mgmt_fils(sm->key_mgmt)) {
1165 		kck = sm->ptk.kck2;
1166 		kck_len = sm->ptk.kck2_len;
1167 	} else {
1168 		kck = sm->ptk.kck;
1169 		kck_len = sm->ptk.kck_len;
1170 	}
1171 
1172 	if (wpa_ft_mic(sm->key_mgmt, kck, kck_len, sm->own_addr, src_addr, 6,
1173 		       parse.mdie - 2, parse.mdie_len + 2,
1174 		       parse.ftie - 2, parse.ftie_len + 2,
1175 		       parse.rsn - 2, parse.rsn_len + 2,
1176 		       parse.ric, parse.ric_len,
1177 		       parse.rsnxe ? parse.rsnxe - 2 : NULL,
1178 		       parse.rsnxe ? parse.rsnxe_len + 2 : 0,
1179 		       NULL,
1180 		       mic) < 0) {
1181 		wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
1182 		goto fail;
1183 	}
1184 
1185 	if (os_memcmp_const(mic, parse.fte_mic, mic_len) != 0) {
1186 		wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
1187 		wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC",
1188 			    parse.fte_mic, mic_len);
1189 		wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, mic_len);
1190 		goto fail;
1191 	}
1192 
1193 	if (parse.fte_rsnxe_used && !sm->ap_rsnxe) {
1194 		wpa_printf(MSG_INFO,
1195 			   "FT: FTE indicated that AP uses RSNXE, but RSNXE was not included in Beacon/Probe Response frames");
1196 		goto fail;
1197 	}
1198 
1199 	if (!sm->ap_rsn_ie) {
1200 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1201 			"FT: No RSNE for this AP known - trying to get from scan results");
1202 		if (wpa_sm_get_beacon_ie(sm) < 0) {
1203 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1204 				"FT: Could not find AP from the scan results");
1205 			goto fail;
1206 		}
1207 		wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1208 			"FT: Found the current AP from updated scan results");
1209 	}
1210 
1211 	if (sm->ap_rsn_ie &&
1212 	    wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1213 			       sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1214 			       parse.rsn - 2, parse.rsn_len + 2)) {
1215 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1216 			"FT: RSNE mismatch between Beacon/ProbeResp and FT protocol Reassociation Response frame");
1217 		wpa_hexdump(MSG_INFO, "RSNE in Beacon/ProbeResp",
1218 			    sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1219 		wpa_hexdump(MSG_INFO,
1220 			    "RSNE in FT protocol Reassociation Response frame",
1221 			    parse.rsn ? parse.rsn - 2 : NULL,
1222 			    parse.rsn ? parse.rsn_len + 2 : 0);
1223 		goto fail;
1224 	}
1225 
1226 	own_rsnxe_used = wpa_key_mgmt_sae(sm->key_mgmt) &&
1227 		(sm->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
1228 		 sm->sae_pwe == SAE_PWE_BOTH);
1229 	if ((sm->ap_rsnxe && !parse.rsnxe && own_rsnxe_used) ||
1230 	    (!sm->ap_rsnxe && parse.rsnxe) ||
1231 	    (sm->ap_rsnxe && parse.rsnxe &&
1232 	     (sm->ap_rsnxe_len != 2 + parse.rsnxe_len ||
1233 	      os_memcmp(sm->ap_rsnxe, parse.rsnxe - 2,
1234 			sm->ap_rsnxe_len) != 0))) {
1235 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1236 			"FT: RSNXE mismatch between Beacon/ProbeResp and FT protocol Reassociation Response frame");
1237 		wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
1238 			    sm->ap_rsnxe, sm->ap_rsnxe_len);
1239 		wpa_hexdump(MSG_INFO,
1240 			    "RSNXE in FT protocol Reassociation Response frame",
1241 			    parse.rsnxe ? parse.rsnxe - 2 : NULL,
1242 			    parse.rsnxe ? parse.rsnxe_len + 2 : 0);
1243 		goto fail;
1244 	}
1245 
1246 #ifdef CONFIG_OCV
1247 	if (wpa_sm_ocv_enabled(sm)) {
1248 		struct wpa_channel_info ci;
1249 
1250 		if (wpa_sm_channel_info(sm, &ci) != 0) {
1251 			wpa_printf(MSG_WARNING,
1252 				   "Failed to get channel info to validate received OCI in (Re)Assoc Response");
1253 			goto fail;
1254 		}
1255 
1256 		if (ocv_verify_tx_params(parse.oci, parse.oci_len, &ci,
1257 					 channel_width_to_int(ci.chanwidth),
1258 					 ci.seg1_idx) != OCI_SUCCESS) {
1259 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
1260 				"addr=" MACSTR " frame=ft-assoc error=%s",
1261 				MAC2STR(src_addr), ocv_errorstr);
1262 			goto fail;
1263 		}
1264 	}
1265 #endif /* CONFIG_OCV */
1266 
1267 	sm->ft_reassoc_completed = 1;
1268 
1269 	if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0 ||
1270 	    wpa_ft_process_igtk_subelem(sm, parse.igtk, parse.igtk_len) < 0 ||
1271 	    wpa_ft_process_bigtk_subelem(sm, parse.bigtk, parse.bigtk_len) < 0)
1272 		goto fail;
1273 
1274 	if (sm->set_ptk_after_assoc) {
1275 		wpa_printf(MSG_DEBUG, "FT: Try to set PTK again now that we "
1276 			   "are associated");
1277 		if (wpa_ft_install_ptk(sm, src_addr) < 0)
1278 			goto fail;
1279 		sm->set_ptk_after_assoc = 0;
1280 	}
1281 
1282 	if (parse.ric) {
1283 		wpa_hexdump(MSG_MSGDUMP, "FT: RIC Response",
1284 			    parse.ric, parse.ric_len);
1285 		/* TODO: parse response and inform driver about results when
1286 		 * using wpa_supplicant SME */
1287 	}
1288 
1289 	wpa_printf(MSG_DEBUG, "FT: Completed successfully");
1290 
1291 	ret = 0;
1292 fail:
1293 	wpa_ft_parse_ies_free(&parse);
1294 	return ret;
1295 }
1296 
1297 
1298 /**
1299  * wpa_ft_start_over_ds - Generate over-the-DS auth request
1300  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1301  * @target_ap: Target AP Address
1302  * @mdie: Mobility Domain IE from the target AP
1303  * @force: Whether to force the request and ignore mobility domain differences
1304  *	(only for testing purposes)
1305  * Returns: 0 on success, -1 on failure
1306  */
wpa_ft_start_over_ds(struct wpa_sm * sm,const u8 * target_ap,const u8 * mdie,bool force)1307 int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap,
1308 			 const u8 *mdie, bool force)
1309 {
1310 	u8 *ft_ies;
1311 	size_t ft_ies_len;
1312 
1313 	if (!force &&
1314 	    (!mdie || mdie[1] < 3 || !wpa_sm_has_ft_keys(sm, mdie + 2))) {
1315 		wpa_printf(MSG_DEBUG, "FT: Cannot use over-the DS with " MACSTR
1316 			   " - no keys matching the mobility domain",
1317 			   MAC2STR(target_ap));
1318 		return -1;
1319 	}
1320 
1321 	wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
1322 		   MAC2STR(target_ap));
1323 
1324 	/* Generate a new SNonce */
1325 	if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
1326 		wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
1327 		return -1;
1328 	}
1329 
1330 	ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
1331 				    NULL, 0, target_ap, NULL, 0, mdie, 0);
1332 	if (ft_ies) {
1333 		sm->over_the_ds_in_progress = 1;
1334 		os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
1335 		wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
1336 		os_free(ft_ies);
1337 	}
1338 
1339 	return 0;
1340 }
1341 
1342 
1343 #ifdef CONFIG_PASN
1344 
wpa_ft_pasn_get_r1kh(struct wpa_sm * sm,const u8 * bssid)1345 static struct pasn_ft_r1kh * wpa_ft_pasn_get_r1kh(struct wpa_sm *sm,
1346 						  const u8 *bssid)
1347 {
1348 	size_t i;
1349 
1350 	for (i = 0; i < sm->n_pasn_r1kh; i++)
1351 		if (ether_addr_equal(sm->pasn_r1kh[i].bssid, bssid))
1352 			return &sm->pasn_r1kh[i];
1353 
1354 	return NULL;
1355 }
1356 
1357 
wpa_ft_pasn_store_r1kh(struct wpa_sm * sm,const u8 * bssid)1358 static void wpa_ft_pasn_store_r1kh(struct wpa_sm *sm, const u8 *bssid)
1359 {
1360 	struct pasn_ft_r1kh *tmp = wpa_ft_pasn_get_r1kh(sm, bssid);
1361 
1362 	if (tmp)
1363 		return;
1364 
1365 	tmp = os_realloc_array(sm->pasn_r1kh, sm->n_pasn_r1kh + 1,
1366 			       sizeof(*tmp));
1367 	if (!tmp) {
1368 		wpa_printf(MSG_DEBUG, "PASN: FT: Failed to store R1KH");
1369 		return;
1370 	}
1371 
1372 	sm->pasn_r1kh = tmp;
1373 	tmp = &sm->pasn_r1kh[sm->n_pasn_r1kh];
1374 
1375 	wpa_printf(MSG_DEBUG, "PASN: FT: Store R1KH for " MACSTR,
1376 		   MAC2STR(bssid));
1377 
1378 	os_memcpy(tmp->bssid, bssid, ETH_ALEN);
1379 	os_memcpy(tmp->r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN);
1380 
1381 	sm->n_pasn_r1kh++;
1382 }
1383 
1384 
wpa_pasn_ft_derive_pmk_r1(struct wpa_sm * sm,int akmp,const u8 * bssid,u8 * pmk_r1,size_t * pmk_r1_len,u8 * pmk_r1_name)1385 int wpa_pasn_ft_derive_pmk_r1(struct wpa_sm *sm, int akmp, const u8 *bssid,
1386 			      u8 *pmk_r1, size_t *pmk_r1_len, u8 *pmk_r1_name)
1387 {
1388 	struct pasn_ft_r1kh *r1kh_entry;
1389 
1390 	if (sm->key_mgmt != (unsigned int) akmp) {
1391 		wpa_printf(MSG_DEBUG,
1392 			   "PASN: FT: Key management mismatch: %u != %u",
1393 			   sm->key_mgmt, akmp);
1394 		return -1;
1395 	}
1396 
1397 	r1kh_entry = wpa_ft_pasn_get_r1kh(sm, bssid);
1398 	if (!r1kh_entry) {
1399 		wpa_printf(MSG_DEBUG,
1400 			   "PASN: FT: Cannot find R1KH-ID for " MACSTR,
1401 			   MAC2STR(bssid));
1402 		return -1;
1403 	}
1404 
1405 	/*
1406 	 * Note: PMK R0 etc. were already derived and are maintained by the
1407 	 * state machine, and as the same key hierarchy is used, there is no
1408 	 * need to derive them again, so only derive PMK R1 etc.
1409 	 */
1410 	if (wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_len, sm->pmk_r0_name,
1411 			      r1kh_entry->r1kh_id, sm->own_addr, pmk_r1,
1412 			      pmk_r1_name) < 0)
1413 		return -1;
1414 
1415 	*pmk_r1_len = sm->pmk_r0_len;
1416 
1417 	wpa_hexdump_key(MSG_DEBUG, "PASN: FT: PMK-R1", pmk_r1, sm->pmk_r0_len);
1418 	wpa_hexdump(MSG_DEBUG, "PASN: FT: PMKR1Name", pmk_r1_name,
1419 		    WPA_PMK_NAME_LEN);
1420 
1421 	return 0;
1422 }
1423 
1424 #endif /* CONFIG_PASN */
1425 
1426 #endif /* CONFIG_IEEE80211R */
1427