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