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