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