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