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