• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA Supplicant - IEEE 802.11r - Fast BSS Transition
3  * Copyright (c) 2006-2007, 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/random.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/ieee802_11_common.h"
16 #include "wpa.h"
17 #include "wpa_i.h"
18 
19 #ifdef CONFIG_IEEE80211R
20 
wpa_derive_ptk_ft(struct wpa_sm * sm,const unsigned char * src_addr,const struct wpa_eapol_key * key,struct wpa_ptk * ptk,size_t ptk_len)21 int wpa_derive_ptk_ft(struct wpa_sm *sm, const unsigned char *src_addr,
22 		      const struct wpa_eapol_key *key,
23 		      struct wpa_ptk *ptk, size_t ptk_len)
24 {
25 	u8 ptk_name[WPA_PMK_NAME_LEN];
26 	const u8 *anonce = key->key_nonce;
27 
28 	if (sm->xxkey_len == 0) {
29 		wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
30 			   "derivation");
31 		return -1;
32 	}
33 
34 	wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, sm->ssid,
35 			  sm->ssid_len, sm->mobility_domain,
36 			  sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
37 			  sm->pmk_r0, sm->pmk_r0_name);
38 	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", sm->pmk_r0, PMK_LEN);
39 	wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name",
40 		    sm->pmk_r0_name, WPA_PMK_NAME_LEN);
41 	wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
42 			  sm->own_addr, sm->pmk_r1, sm->pmk_r1_name);
43 	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
44 	wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name,
45 		    WPA_PMK_NAME_LEN);
46 	wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, anonce, sm->own_addr,
47 			  sm->bssid, sm->pmk_r1_name,
48 			  (u8 *) ptk, ptk_len, ptk_name);
49 	wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, ptk_len);
50 	wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
51 
52 	return 0;
53 }
54 
55 
56 /**
57  * wpa_sm_set_ft_params - Set FT (IEEE 802.11r) parameters
58  * @sm: Pointer to WPA state machine data from wpa_sm_init()
59  * @ies: Association Response IEs or %NULL to clear FT parameters
60  * @ies_len: Length of ies buffer in octets
61  * Returns: 0 on success, -1 on failure
62  */
wpa_sm_set_ft_params(struct wpa_sm * sm,const u8 * ies,size_t ies_len)63 int wpa_sm_set_ft_params(struct wpa_sm *sm, const u8 *ies, size_t ies_len)
64 {
65 	struct wpa_ft_ies ft;
66 
67 	if (sm == NULL)
68 		return 0;
69 
70 	if (wpa_ft_parse_ies(ies, ies_len, &ft) < 0)
71 		return -1;
72 
73 	if (ft.mdie && ft.mdie_len < MOBILITY_DOMAIN_ID_LEN + 1)
74 		return -1;
75 
76 	if (ft.mdie) {
77 		wpa_hexdump(MSG_DEBUG, "FT: Mobility domain",
78 			    ft.mdie, MOBILITY_DOMAIN_ID_LEN);
79 		os_memcpy(sm->mobility_domain, ft.mdie,
80 			  MOBILITY_DOMAIN_ID_LEN);
81 		sm->mdie_ft_capab = ft.mdie[MOBILITY_DOMAIN_ID_LEN];
82 		wpa_printf(MSG_DEBUG, "FT: Capability and Policy: 0x%02x",
83 			   sm->mdie_ft_capab);
84 	} else
85 		os_memset(sm->mobility_domain, 0, MOBILITY_DOMAIN_ID_LEN);
86 
87 	if (ft.r0kh_id) {
88 		wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID",
89 			    ft.r0kh_id, ft.r0kh_id_len);
90 		os_memcpy(sm->r0kh_id, ft.r0kh_id, ft.r0kh_id_len);
91 		sm->r0kh_id_len = ft.r0kh_id_len;
92 	} else {
93 		/* FIX: When should R0KH-ID be cleared? We need to keep the
94 		 * old R0KH-ID in order to be able to use this during FT. */
95 		/*
96 		 * os_memset(sm->r0kh_id, 0, FT_R0KH_ID_LEN);
97 		 * sm->r0kh_id_len = 0;
98 		 */
99 	}
100 
101 	if (ft.r1kh_id) {
102 		wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID",
103 			    ft.r1kh_id, FT_R1KH_ID_LEN);
104 		os_memcpy(sm->r1kh_id, ft.r1kh_id, FT_R1KH_ID_LEN);
105 	} else
106 		os_memset(sm->r1kh_id, 0, FT_R1KH_ID_LEN);
107 
108 	os_free(sm->assoc_resp_ies);
109 	sm->assoc_resp_ies = os_malloc(ft.mdie_len + 2 + ft.ftie_len + 2);
110 	if (sm->assoc_resp_ies) {
111 		u8 *pos = sm->assoc_resp_ies;
112 		if (ft.mdie) {
113 			os_memcpy(pos, ft.mdie - 2, ft.mdie_len + 2);
114 			pos += ft.mdie_len + 2;
115 		}
116 		if (ft.ftie) {
117 			os_memcpy(pos, ft.ftie - 2, ft.ftie_len + 2);
118 			pos += ft.ftie_len + 2;
119 		}
120 		sm->assoc_resp_ies_len = pos - sm->assoc_resp_ies;
121 		wpa_hexdump(MSG_DEBUG, "FT: Stored MDIE and FTIE from "
122 			    "(Re)Association Response",
123 			    sm->assoc_resp_ies, sm->assoc_resp_ies_len);
124 	}
125 
126 	return 0;
127 }
128 
129 
130 /**
131  * wpa_ft_gen_req_ies - Generate FT (IEEE 802.11r) IEs for Auth/ReAssoc Request
132  * @sm: Pointer to WPA state machine data from wpa_sm_init()
133  * @len: Buffer for returning the length of the IEs
134  * @anonce: ANonce or %NULL if not yet available
135  * @pmk_name: PMKR0Name or PMKR1Name to be added into the RSN IE PMKID List
136  * @kck: 128-bit KCK for MIC or %NULL if no MIC is used
137  * @target_ap: Target AP address
138  * @ric_ies: Optional IE(s), e.g., WMM TSPEC(s), for RIC-Request or %NULL
139  * @ric_ies_len: Length of ric_ies buffer in octets
140  * @ap_mdie: Mobility Domain IE from the target AP
141  * Returns: Pointer to buffer with IEs or %NULL on failure
142  *
143  * Caller is responsible for freeing the returned buffer with os_free();
144  */
wpa_ft_gen_req_ies(struct wpa_sm * sm,size_t * len,const u8 * anonce,const u8 * pmk_name,const u8 * kck,const u8 * target_ap,const u8 * ric_ies,size_t ric_ies_len,const u8 * ap_mdie)145 static u8 * wpa_ft_gen_req_ies(struct wpa_sm *sm, size_t *len,
146 			       const u8 *anonce, const u8 *pmk_name,
147 			       const u8 *kck, const u8 *target_ap,
148 			       const u8 *ric_ies, size_t ric_ies_len,
149 			       const u8 *ap_mdie)
150 {
151 	size_t buf_len;
152 	u8 *buf, *pos, *ftie_len, *ftie_pos;
153 	struct rsn_mdie *mdie;
154 	struct rsn_ftie *ftie;
155 	struct rsn_ie_hdr *rsnie;
156 	u16 capab;
157 
158 	sm->ft_completed = 0;
159 
160 	buf_len = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
161 		2 + sm->r0kh_id_len + ric_ies_len + 100;
162 	buf = os_zalloc(buf_len);
163 	if (buf == NULL)
164 		return NULL;
165 	pos = buf;
166 
167 	/* RSNIE[PMKR0Name/PMKR1Name] */
168 	rsnie = (struct rsn_ie_hdr *) pos;
169 	rsnie->elem_id = WLAN_EID_RSN;
170 	WPA_PUT_LE16(rsnie->version, RSN_VERSION);
171 	pos = (u8 *) (rsnie + 1);
172 
173 	/* Group Suite Selector */
174 	if (sm->group_cipher == WPA_CIPHER_CCMP)
175 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
176 	else if (sm->group_cipher == WPA_CIPHER_TKIP)
177 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
178 	else {
179 		wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
180 			   sm->group_cipher);
181 		os_free(buf);
182 		return NULL;
183 	}
184 	pos += RSN_SELECTOR_LEN;
185 
186 	/* Pairwise Suite Count */
187 	WPA_PUT_LE16(pos, 1);
188 	pos += 2;
189 
190 	/* Pairwise Suite List */
191 	if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
192 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
193 	else if (sm->pairwise_cipher == WPA_CIPHER_TKIP)
194 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
195 	else {
196 		wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
197 			   sm->pairwise_cipher);
198 		os_free(buf);
199 		return NULL;
200 	}
201 	pos += RSN_SELECTOR_LEN;
202 
203 	/* Authenticated Key Management Suite Count */
204 	WPA_PUT_LE16(pos, 1);
205 	pos += 2;
206 
207 	/* Authenticated Key Management Suite List */
208 	if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X)
209 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
210 	else if (sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
211 		RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
212 	else {
213 		wpa_printf(MSG_WARNING, "FT: Invalid key management type (%d)",
214 			   sm->key_mgmt);
215 		os_free(buf);
216 		return NULL;
217 	}
218 	pos += RSN_SELECTOR_LEN;
219 
220 	/* RSN Capabilities */
221 	capab = 0;
222 #ifdef CONFIG_IEEE80211W
223 	if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
224 		capab |= WPA_CAPABILITY_MFPC;
225 #endif /* CONFIG_IEEE80211W */
226 	WPA_PUT_LE16(pos, capab);
227 	pos += 2;
228 
229 	/* PMKID Count */
230 	WPA_PUT_LE16(pos, 1);
231 	pos += 2;
232 
233 	/* PMKID List [PMKR0Name/PMKR1Name] */
234 	os_memcpy(pos, pmk_name, WPA_PMK_NAME_LEN);
235 	pos += WPA_PMK_NAME_LEN;
236 
237 #ifdef CONFIG_IEEE80211W
238 	if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
239 		/* Management Group Cipher Suite */
240 		RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
241 		pos += RSN_SELECTOR_LEN;
242 	}
243 #endif /* CONFIG_IEEE80211W */
244 
245 	rsnie->len = (pos - (u8 *) rsnie) - 2;
246 
247 	/* MDIE */
248 	*pos++ = WLAN_EID_MOBILITY_DOMAIN;
249 	*pos++ = sizeof(*mdie);
250 	mdie = (struct rsn_mdie *) pos;
251 	pos += sizeof(*mdie);
252 	os_memcpy(mdie->mobility_domain, sm->mobility_domain,
253 		  MOBILITY_DOMAIN_ID_LEN);
254 	mdie->ft_capab = ap_mdie && ap_mdie[1] >= 3 ? ap_mdie[4] :
255 		sm->mdie_ft_capab;
256 
257 	/* FTIE[SNonce, [R1KH-ID,] R0KH-ID ] */
258 	ftie_pos = pos;
259 	*pos++ = WLAN_EID_FAST_BSS_TRANSITION;
260 	ftie_len = pos++;
261 	ftie = (struct rsn_ftie *) pos;
262 	pos += sizeof(*ftie);
263 	os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
264 	if (anonce)
265 		os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
266 	if (kck) {
267 		/* R1KH-ID sub-element in third FT message */
268 		*pos++ = FTIE_SUBELEM_R1KH_ID;
269 		*pos++ = FT_R1KH_ID_LEN;
270 		os_memcpy(pos, sm->r1kh_id, FT_R1KH_ID_LEN);
271 		pos += FT_R1KH_ID_LEN;
272 	}
273 	/* R0KH-ID sub-element */
274 	*pos++ = FTIE_SUBELEM_R0KH_ID;
275 	*pos++ = sm->r0kh_id_len;
276 	os_memcpy(pos, sm->r0kh_id, sm->r0kh_id_len);
277 	pos += sm->r0kh_id_len;
278 	*ftie_len = pos - ftie_len - 1;
279 
280 	if (ric_ies) {
281 		/* RIC Request */
282 		os_memcpy(pos, ric_ies, ric_ies_len);
283 		pos += ric_ies_len;
284 	}
285 
286 	if (kck) {
287 		/*
288 		 * IEEE Std 802.11r-2008, 11A.8.4
289 		 * MIC shall be calculated over:
290 		 * non-AP STA MAC address
291 		 * Target AP MAC address
292 		 * Transaction seq number (5 for ReassocReq, 3 otherwise)
293 		 * RSN IE
294 		 * MDIE
295 		 * FTIE (with MIC field set to 0)
296 		 * RIC-Request (if present)
297 		 */
298 		/* Information element count */
299 		ftie->mic_control[1] = 3 + ieee802_11_ie_count(ric_ies,
300 							       ric_ies_len);
301 		if (wpa_ft_mic(kck, sm->own_addr, target_ap, 5,
302 			       ((u8 *) mdie) - 2, 2 + sizeof(*mdie),
303 			       ftie_pos, 2 + *ftie_len,
304 			       (u8 *) rsnie, 2 + rsnie->len, ric_ies,
305 			       ric_ies_len, ftie->mic) < 0) {
306 			wpa_printf(MSG_INFO, "FT: Failed to calculate MIC");
307 			os_free(buf);
308 			return NULL;
309 		}
310 	}
311 
312 	*len = pos - buf;
313 
314 	return buf;
315 }
316 
317 
wpa_ft_install_ptk(struct wpa_sm * sm,const u8 * bssid)318 static int wpa_ft_install_ptk(struct wpa_sm *sm, const u8 *bssid)
319 {
320 	int keylen;
321 	enum wpa_alg alg;
322 	u8 null_rsc[6] = { 0, 0, 0, 0, 0, 0 };
323 
324 	wpa_printf(MSG_DEBUG, "FT: Installing PTK to the driver.");
325 
326 	switch (sm->pairwise_cipher) {
327 	case WPA_CIPHER_CCMP:
328 		alg = WPA_ALG_CCMP;
329 		keylen = 16;
330 		break;
331 	case WPA_CIPHER_TKIP:
332 		alg = WPA_ALG_TKIP;
333 		keylen = 32;
334 		break;
335 	default:
336 		wpa_printf(MSG_WARNING, "FT: Unsupported pairwise cipher %d",
337 			   sm->pairwise_cipher);
338 		return -1;
339 	}
340 
341 	if (wpa_sm_set_key(sm, alg, bssid, 0, 1, null_rsc,
342 			   sizeof(null_rsc), (u8 *) sm->ptk.tk1, keylen) < 0) {
343 		wpa_printf(MSG_WARNING, "FT: Failed to set PTK to the driver");
344 		return -1;
345 	}
346 
347 	return 0;
348 }
349 
350 
351 /**
352  * wpa_ft_prepare_auth_request - Generate over-the-air auth request
353  * @sm: Pointer to WPA state machine data from wpa_sm_init()
354  * @mdie: Target AP MDIE
355  * Returns: 0 on success, -1 on failure
356  */
wpa_ft_prepare_auth_request(struct wpa_sm * sm,const u8 * mdie)357 int wpa_ft_prepare_auth_request(struct wpa_sm *sm, const u8 *mdie)
358 {
359 	u8 *ft_ies;
360 	size_t ft_ies_len;
361 
362 	/* Generate a new SNonce */
363 	if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
364 		wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
365 		return -1;
366 	}
367 
368 	ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
369 				    NULL, sm->bssid, NULL, 0, mdie);
370 	if (ft_ies) {
371 		wpa_sm_update_ft_ies(sm, sm->mobility_domain,
372 				     ft_ies, ft_ies_len);
373 		os_free(ft_ies);
374 	}
375 
376 	return 0;
377 }
378 
379 
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)380 int wpa_ft_process_response(struct wpa_sm *sm, const u8 *ies, size_t ies_len,
381 			    int ft_action, const u8 *target_ap,
382 			    const u8 *ric_ies, size_t ric_ies_len)
383 {
384 	u8 *ft_ies;
385 	size_t ft_ies_len, ptk_len;
386 	struct wpa_ft_ies parse;
387 	struct rsn_mdie *mdie;
388 	struct rsn_ftie *ftie;
389 	u8 ptk_name[WPA_PMK_NAME_LEN];
390 	int ret;
391 	const u8 *bssid;
392 
393 	wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
394 	wpa_hexdump(MSG_DEBUG, "FT: RIC IEs", ric_ies, ric_ies_len);
395 
396 	if (ft_action) {
397 		if (!sm->over_the_ds_in_progress) {
398 			wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
399 				   "- drop FT Action Response");
400 			return -1;
401 		}
402 
403 		if (os_memcmp(target_ap, sm->target_ap, ETH_ALEN) != 0) {
404 			wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
405 				   "with this Target AP - drop FT Action "
406 				   "Response");
407 			return -1;
408 		}
409 	}
410 
411 	if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
412 	    sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
413 		wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
414 			   "enabled for this connection");
415 		return -1;
416 	}
417 
418 	if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
419 		wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
420 		return -1;
421 	}
422 
423 	mdie = (struct rsn_mdie *) parse.mdie;
424 	if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
425 	    os_memcmp(mdie->mobility_domain, sm->mobility_domain,
426 		      MOBILITY_DOMAIN_ID_LEN) != 0) {
427 		wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
428 		return -1;
429 	}
430 
431 	ftie = (struct rsn_ftie *) parse.ftie;
432 	if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
433 		wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
434 		return -1;
435 	}
436 
437 	if (os_memcmp(ftie->snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
438 		wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
439 		wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
440 			    ftie->snonce, WPA_NONCE_LEN);
441 		wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
442 			    sm->snonce, WPA_NONCE_LEN);
443 		return -1;
444 	}
445 
446 	if (parse.r0kh_id == NULL) {
447 		wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
448 		return -1;
449 	}
450 
451 	if (parse.r0kh_id_len != sm->r0kh_id_len ||
452 	    os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
453 		wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
454 			   "the current R0KH-ID");
455 		wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
456 			    parse.r0kh_id, parse.r0kh_id_len);
457 		wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
458 			    sm->r0kh_id, sm->r0kh_id_len);
459 		return -1;
460 	}
461 
462 	if (parse.r1kh_id == NULL) {
463 		wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
464 		return -1;
465 	}
466 
467 	if (parse.rsn_pmkid == NULL ||
468 	    os_memcmp(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN)) {
469 		wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name (PMKID) in "
470 			   "RSNIE");
471 		return -1;
472 	}
473 
474 	os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
475 	wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", sm->r1kh_id, FT_R1KH_ID_LEN);
476 	wpa_hexdump(MSG_DEBUG, "FT: SNonce", sm->snonce, WPA_NONCE_LEN);
477 	wpa_hexdump(MSG_DEBUG, "FT: ANonce", ftie->anonce, WPA_NONCE_LEN);
478 	os_memcpy(sm->anonce, ftie->anonce, WPA_NONCE_LEN);
479 	wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
480 			  sm->own_addr, sm->pmk_r1, sm->pmk_r1_name);
481 	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
482 	wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name",
483 		    sm->pmk_r1_name, WPA_PMK_NAME_LEN);
484 
485 	bssid = target_ap;
486 	ptk_len = sm->pairwise_cipher == WPA_CIPHER_CCMP ? 48 : 64;
487 	wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, ftie->anonce, sm->own_addr,
488 			  bssid, sm->pmk_r1_name,
489 			  (u8 *) &sm->ptk, ptk_len, ptk_name);
490 	wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
491 			(u8 *) &sm->ptk, ptk_len);
492 	wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
493 
494 	ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, ftie->anonce,
495 				    sm->pmk_r1_name, sm->ptk.kck, bssid,
496 				    ric_ies, ric_ies_len,
497 				    parse.mdie ? parse.mdie - 2 : NULL);
498 	if (ft_ies) {
499 		wpa_sm_update_ft_ies(sm, sm->mobility_domain,
500 				     ft_ies, ft_ies_len);
501 		os_free(ft_ies);
502 	}
503 
504 	wpa_sm_mark_authenticated(sm, bssid);
505 	ret = wpa_ft_install_ptk(sm, bssid);
506 	if (ret) {
507 		/*
508 		 * Some drivers do not support key configuration when we are
509 		 * not associated with the target AP. Work around this by
510 		 * trying again after the following reassociation gets
511 		 * completed.
512 		 */
513 		wpa_printf(MSG_DEBUG, "FT: Failed to set PTK prior to "
514 			   "association - try again after reassociation");
515 		sm->set_ptk_after_assoc = 1;
516 	} else
517 		sm->set_ptk_after_assoc = 0;
518 
519 	sm->ft_completed = 1;
520 	if (ft_action) {
521 		/*
522 		 * The caller is expected trigger re-association with the
523 		 * Target AP.
524 		 */
525 		os_memcpy(sm->bssid, target_ap, ETH_ALEN);
526 	}
527 
528 	return 0;
529 }
530 
531 
wpa_ft_is_completed(struct wpa_sm * sm)532 int wpa_ft_is_completed(struct wpa_sm *sm)
533 {
534 	if (sm == NULL)
535 		return 0;
536 
537 	if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
538 	    sm->key_mgmt != WPA_KEY_MGMT_FT_PSK)
539 		return 0;
540 
541 	return sm->ft_completed;
542 }
543 
544 
wpa_ft_process_gtk_subelem(struct wpa_sm * sm,const u8 * gtk_elem,size_t gtk_elem_len)545 static int wpa_ft_process_gtk_subelem(struct wpa_sm *sm, const u8 *gtk_elem,
546 				      size_t gtk_elem_len)
547 {
548 	u8 gtk[32];
549 	int keyidx;
550 	enum wpa_alg alg;
551 	size_t gtk_len, keylen, rsc_len;
552 
553 	if (gtk_elem == NULL) {
554 		wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
555 		return 0;
556 	}
557 
558 	wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
559 			gtk_elem, gtk_elem_len);
560 
561 	if (gtk_elem_len < 11 + 24 || (gtk_elem_len - 11) % 8 ||
562 	    gtk_elem_len - 19 > sizeof(gtk)) {
563 		wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
564 			   "length %lu", (unsigned long) gtk_elem_len);
565 		return -1;
566 	}
567 	gtk_len = gtk_elem_len - 19;
568 	if (aes_unwrap(sm->ptk.kek, gtk_len / 8, gtk_elem + 11, gtk)) {
569 		wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
570 			   "decrypt GTK");
571 		return -1;
572 	}
573 
574 	switch (sm->group_cipher) {
575 	case WPA_CIPHER_CCMP:
576 		keylen = 16;
577 		rsc_len = 6;
578 		alg = WPA_ALG_CCMP;
579 		break;
580 	case WPA_CIPHER_TKIP:
581 		keylen = 32;
582 		rsc_len = 6;
583 		alg = WPA_ALG_TKIP;
584 		break;
585 	case WPA_CIPHER_WEP104:
586 		keylen = 13;
587 		rsc_len = 0;
588 		alg = WPA_ALG_WEP;
589 		break;
590 	case WPA_CIPHER_WEP40:
591 		keylen = 5;
592 		rsc_len = 0;
593 		alg = WPA_ALG_WEP;
594 		break;
595 	default:
596 		wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
597 			   sm->group_cipher);
598 		return -1;
599 	}
600 
601 	if (gtk_len < keylen) {
602 		wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
603 		return -1;
604 	}
605 
606 	/* Key Info[2] | Key Length[1] | RSC[8] | Key[5..32]. */
607 
608 	keyidx = WPA_GET_LE16(gtk_elem) & 0x03;
609 
610 	if (gtk_elem[2] != keylen) {
611 		wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
612 			   "negotiated %lu",
613 			   gtk_elem[2], (unsigned long) keylen);
614 		return -1;
615 	}
616 
617 	wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
618 	if (wpa_sm_set_key(sm, alg, broadcast_ether_addr, keyidx, 0,
619 			   gtk_elem + 3, rsc_len, gtk, keylen) < 0) {
620 		wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
621 			   "driver.");
622 		return -1;
623 	}
624 
625 	return 0;
626 }
627 
628 
629 #ifdef CONFIG_IEEE80211W
wpa_ft_process_igtk_subelem(struct wpa_sm * sm,const u8 * igtk_elem,size_t igtk_elem_len)630 static int wpa_ft_process_igtk_subelem(struct wpa_sm *sm, const u8 *igtk_elem,
631 				       size_t igtk_elem_len)
632 {
633 	u8 igtk[WPA_IGTK_LEN];
634 	u16 keyidx;
635 
636 	if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
637 		return 0;
638 
639 	if (igtk_elem == NULL) {
640 		wpa_printf(MSG_DEBUG, "FT: No IGTK included in FTIE");
641 		return 0;
642 	}
643 
644 	wpa_hexdump_key(MSG_DEBUG, "FT: Received IGTK in Reassoc Resp",
645 			igtk_elem, igtk_elem_len);
646 
647 	if (igtk_elem_len != 2 + 6 + 1 + WPA_IGTK_LEN + 8) {
648 		wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem "
649 			   "length %lu", (unsigned long) igtk_elem_len);
650 		return -1;
651 	}
652 	if (igtk_elem[8] != WPA_IGTK_LEN) {
653 		wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem Key Length "
654 			   "%d", igtk_elem[8]);
655 		return -1;
656 	}
657 
658 	if (aes_unwrap(sm->ptk.kek, WPA_IGTK_LEN / 8, igtk_elem + 9, igtk)) {
659 		wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
660 			   "decrypt IGTK");
661 		return -1;
662 	}
663 
664 	/* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
665 
666 	keyidx = WPA_GET_LE16(igtk_elem);
667 
668 	wpa_hexdump_key(MSG_DEBUG, "FT: IGTK from Reassoc Resp", igtk,
669 			WPA_IGTK_LEN);
670 	if (wpa_sm_set_key(sm, WPA_ALG_IGTK, broadcast_ether_addr, keyidx, 0,
671 			   igtk_elem + 2, 6, igtk, WPA_IGTK_LEN) < 0) {
672 		wpa_printf(MSG_WARNING, "WPA: Failed to set IGTK to the "
673 			   "driver.");
674 		return -1;
675 	}
676 
677 	return 0;
678 }
679 #endif /* CONFIG_IEEE80211W */
680 
681 
wpa_ft_validate_reassoc_resp(struct wpa_sm * sm,const u8 * ies,size_t ies_len,const u8 * src_addr)682 int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
683 				 size_t ies_len, const u8 *src_addr)
684 {
685 	struct wpa_ft_ies parse;
686 	struct rsn_mdie *mdie;
687 	struct rsn_ftie *ftie;
688 	unsigned int count;
689 	u8 mic[16];
690 
691 	wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
692 
693 	if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
694 	    sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
695 		wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
696 			   "enabled for this connection");
697 		return -1;
698 	}
699 
700 	if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
701 		wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
702 		return -1;
703 	}
704 
705 	mdie = (struct rsn_mdie *) parse.mdie;
706 	if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
707 	    os_memcmp(mdie->mobility_domain, sm->mobility_domain,
708 		      MOBILITY_DOMAIN_ID_LEN) != 0) {
709 		wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
710 		return -1;
711 	}
712 
713 	ftie = (struct rsn_ftie *) parse.ftie;
714 	if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
715 		wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
716 		return -1;
717 	}
718 
719 	if (os_memcmp(ftie->snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
720 		wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
721 		wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
722 			    ftie->snonce, WPA_NONCE_LEN);
723 		wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
724 			    sm->snonce, WPA_NONCE_LEN);
725 		return -1;
726 	}
727 
728 	if (os_memcmp(ftie->anonce, sm->anonce, WPA_NONCE_LEN) != 0) {
729 		wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
730 		wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
731 			    ftie->anonce, WPA_NONCE_LEN);
732 		wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
733 			    sm->anonce, WPA_NONCE_LEN);
734 		return -1;
735 	}
736 
737 	if (parse.r0kh_id == NULL) {
738 		wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
739 		return -1;
740 	}
741 
742 	if (parse.r0kh_id_len != sm->r0kh_id_len ||
743 	    os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
744 		wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
745 			   "the current R0KH-ID");
746 		wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
747 			    parse.r0kh_id, parse.r0kh_id_len);
748 		wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
749 			    sm->r0kh_id, sm->r0kh_id_len);
750 		return -1;
751 	}
752 
753 	if (parse.r1kh_id == NULL) {
754 		wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
755 		return -1;
756 	}
757 
758 	if (os_memcmp(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
759 		wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
760 			   "ReassocResp");
761 		return -1;
762 	}
763 
764 	if (parse.rsn_pmkid == NULL ||
765 	    os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) {
766 		wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
767 			   "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
768 		return -1;
769 	}
770 
771 	count = 3;
772 	if (parse.ric)
773 		count += ieee802_11_ie_count(parse.ric, parse.ric_len);
774 	if (ftie->mic_control[1] != count) {
775 		wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
776 			   "Control: received %u expected %u",
777 			   ftie->mic_control[1], count);
778 		return -1;
779 	}
780 
781 	if (wpa_ft_mic(sm->ptk.kck, sm->own_addr, src_addr, 6,
782 		       parse.mdie - 2, parse.mdie_len + 2,
783 		       parse.ftie - 2, parse.ftie_len + 2,
784 		       parse.rsn - 2, parse.rsn_len + 2,
785 		       parse.ric, parse.ric_len,
786 		       mic) < 0) {
787 		wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
788 		return -1;
789 	}
790 
791 	if (os_memcmp(mic, ftie->mic, 16) != 0) {
792 		wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
793 		wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
794 		wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
795 		return -1;
796 	}
797 
798 	if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0)
799 		return -1;
800 
801 #ifdef CONFIG_IEEE80211W
802 	if (wpa_ft_process_igtk_subelem(sm, parse.igtk, parse.igtk_len) < 0)
803 		return -1;
804 #endif /* CONFIG_IEEE80211W */
805 
806 	if (sm->set_ptk_after_assoc) {
807 		wpa_printf(MSG_DEBUG, "FT: Try to set PTK again now that we "
808 			   "are associated");
809 		if (wpa_ft_install_ptk(sm, src_addr) < 0)
810 			return -1;
811 		sm->set_ptk_after_assoc = 0;
812 	}
813 
814 	if (parse.ric) {
815 		wpa_hexdump(MSG_MSGDUMP, "FT: RIC Response",
816 			    parse.ric, parse.ric_len);
817 		/* TODO: parse response and inform driver about results */
818 	}
819 
820 	return 0;
821 }
822 
823 
824 /**
825  * wpa_ft_start_over_ds - Generate over-the-DS auth request
826  * @sm: Pointer to WPA state machine data from wpa_sm_init()
827  * @target_ap: Target AP Address
828  * @mdie: Mobility Domain IE from the target AP
829  * Returns: 0 on success, -1 on failure
830  */
wpa_ft_start_over_ds(struct wpa_sm * sm,const u8 * target_ap,const u8 * mdie)831 int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap,
832 			 const u8 *mdie)
833 {
834 	u8 *ft_ies;
835 	size_t ft_ies_len;
836 
837 	wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
838 		   MAC2STR(target_ap));
839 
840 	/* Generate a new SNonce */
841 	if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
842 		wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
843 		return -1;
844 	}
845 
846 	ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
847 				    NULL, target_ap, NULL, 0, mdie);
848 	if (ft_ies) {
849 		sm->over_the_ds_in_progress = 1;
850 		os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
851 		wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
852 		os_free(ft_ies);
853 	}
854 
855 	return 0;
856 }
857 
858 #endif /* CONFIG_IEEE80211R */
859