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