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