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