1 /* 2 * WPA Supplicant - WPA state machine and EAPOL-Key processing 3 * Copyright (c) 2003-2018, Jouni Malinen <j (at) w1.fi> 4 * Copyright(c) 2015 Intel Deutschland GmbH 5 * 6 * This software may be distributed under the terms of the BSD license. 7 * See README for more details. 8 */ 9 10 #include "includes.h" 11 12 #include "common.h" 13 #include "crypto/aes.h" 14 #include "crypto/aes_wrap.h" 15 #include "crypto/crypto.h" 16 #include "crypto/random.h" 17 #include "crypto/aes_siv.h" 18 #include "crypto/sha256.h" 19 #include "crypto/sha384.h" 20 #include "crypto/sha512.h" 21 #include "common/ieee802_11_defs.h" 22 #include "common/ieee802_11_common.h" 23 #include "common/ocv.h" 24 #include "eap_common/eap_defs.h" 25 #include "eapol_supp/eapol_supp_sm.h" 26 #include "drivers/driver.h" 27 #include "wpa.h" 28 #include "eloop.h" 29 #include "preauth.h" 30 #include "pmksa_cache.h" 31 #include "wpa_i.h" 32 #include "wpa_ie.h" 33 34 35 static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 36 37 38 /** 39 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message 40 * @sm: Pointer to WPA state machine data from wpa_sm_init() 41 * @ptk: PTK for Key Confirmation/Encryption Key 42 * @ver: Version field from Key Info 43 * @dest: Destination address for the frame 44 * @proto: Ethertype (usually ETH_P_EAPOL) 45 * @msg: EAPOL-Key message 46 * @msg_len: Length of message 47 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written 48 * Returns: >= 0 on success, < 0 on failure 49 */ 50 int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk, 51 int ver, const u8 *dest, u16 proto, 52 u8 *msg, size_t msg_len, u8 *key_mic) 53 { 54 int ret = -1; 55 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); 56 57 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR 58 " ver=%d mic_len=%d key_mgmt=0x%x", 59 MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt); 60 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) { 61 /* 62 * Association event was not yet received; try to fetch 63 * BSSID from the driver. 64 */ 65 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) { 66 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 67 "WPA: Failed to read BSSID for " 68 "EAPOL-Key destination address"); 69 } else { 70 dest = sm->bssid; 71 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 72 "WPA: Use BSSID (" MACSTR 73 ") as the destination for EAPOL-Key", 74 MAC2STR(dest)); 75 } 76 } 77 78 if (mic_len) { 79 if (key_mic && (!ptk || !ptk->kck_len)) 80 goto out; 81 82 if (key_mic && 83 wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver, 84 msg, msg_len, key_mic)) { 85 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, 86 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC", 87 ver, sm->key_mgmt); 88 goto out; 89 } 90 if (ptk) 91 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", 92 ptk->kck, ptk->kck_len); 93 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", 94 key_mic, mic_len); 95 } else { 96 #ifdef CONFIG_FILS 97 /* AEAD cipher - Key MIC field not used */ 98 struct ieee802_1x_hdr *s_hdr, *hdr; 99 struct wpa_eapol_key *s_key, *key; 100 u8 *buf, *s_key_data, *key_data; 101 size_t buf_len = msg_len + AES_BLOCK_SIZE; 102 size_t key_data_len; 103 u16 eapol_len; 104 const u8 *aad[1]; 105 size_t aad_len[1]; 106 107 if (!ptk || !ptk->kek_len) 108 goto out; 109 110 key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) - 111 sizeof(struct wpa_eapol_key) - 2; 112 113 buf = os_malloc(buf_len); 114 if (!buf) 115 goto out; 116 117 os_memcpy(buf, msg, msg_len); 118 hdr = (struct ieee802_1x_hdr *) buf; 119 key = (struct wpa_eapol_key *) (hdr + 1); 120 key_data = ((u8 *) (key + 1)) + 2; 121 122 /* Update EAPOL header to include AES-SIV overhead */ 123 eapol_len = be_to_host16(hdr->length); 124 eapol_len += AES_BLOCK_SIZE; 125 hdr->length = host_to_be16(eapol_len); 126 127 /* Update Key Data Length field to include AES-SIV overhead */ 128 WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len); 129 130 s_hdr = (struct ieee802_1x_hdr *) msg; 131 s_key = (struct wpa_eapol_key *) (s_hdr + 1); 132 s_key_data = ((u8 *) (s_key + 1)) + 2; 133 134 wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data", 135 s_key_data, key_data_len); 136 137 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len); 138 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to 139 * to Key Data (exclusive). */ 140 aad[0] = buf; 141 aad_len[0] = key_data - buf; 142 if (aes_siv_encrypt(ptk->kek, ptk->kek_len, 143 s_key_data, key_data_len, 144 1, aad, aad_len, key_data) < 0) { 145 os_free(buf); 146 goto out; 147 } 148 149 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV", 150 key_data, AES_BLOCK_SIZE + key_data_len); 151 152 os_free(msg); 153 msg = buf; 154 msg_len = buf_len; 155 #else /* CONFIG_FILS */ 156 goto out; 157 #endif /* CONFIG_FILS */ 158 } 159 160 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len); 161 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len); 162 eapol_sm_notify_tx_eapol_key(sm->eapol); 163 out: 164 os_free(msg); 165 return ret; 166 } 167 168 169 /** 170 * wpa_sm_key_request - Send EAPOL-Key Request 171 * @sm: Pointer to WPA state machine data from wpa_sm_init() 172 * @error: Indicate whether this is an Michael MIC error report 173 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet 174 * 175 * Send an EAPOL-Key Request to the current authenticator. This function is 176 * used to request rekeying and it is usually called when a local Michael MIC 177 * failure is detected. 178 */ 179 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise) 180 { 181 size_t mic_len, hdrlen, rlen; 182 struct wpa_eapol_key *reply; 183 int key_info, ver; 184 u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic; 185 186 if (wpa_use_akm_defined(sm->key_mgmt)) 187 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED; 188 else if (wpa_key_mgmt_ft(sm->key_mgmt) || 189 wpa_key_mgmt_sha256(sm->key_mgmt)) 190 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC; 191 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP) 192 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES; 193 else 194 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4; 195 196 if (wpa_sm_get_bssid(sm, bssid) < 0) { 197 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 198 "Failed to read BSSID for EAPOL-Key request"); 199 return; 200 } 201 202 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); 203 hdrlen = sizeof(*reply) + mic_len + 2; 204 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL, 205 hdrlen, &rlen, (void *) &reply); 206 if (rbuf == NULL) 207 return; 208 209 reply->type = (sm->proto == WPA_PROTO_RSN || 210 sm->proto == WPA_PROTO_OSEN) ? 211 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; 212 key_info = WPA_KEY_INFO_REQUEST | ver; 213 if (sm->ptk_set) 214 key_info |= WPA_KEY_INFO_SECURE; 215 if (sm->ptk_set && mic_len) 216 key_info |= WPA_KEY_INFO_MIC; 217 if (error) 218 key_info |= WPA_KEY_INFO_ERROR; 219 if (pairwise) 220 key_info |= WPA_KEY_INFO_KEY_TYPE; 221 WPA_PUT_BE16(reply->key_info, key_info); 222 WPA_PUT_BE16(reply->key_length, 0); 223 os_memcpy(reply->replay_counter, sm->request_counter, 224 WPA_REPLAY_COUNTER_LEN); 225 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN); 226 227 mic = (u8 *) (reply + 1); 228 WPA_PUT_BE16(mic + mic_len, 0); 229 if (!(key_info & WPA_KEY_INFO_MIC)) 230 key_mic = NULL; 231 else 232 key_mic = mic; 233 234 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 235 "WPA: Sending EAPOL-Key Request (error=%d " 236 "pairwise=%d ptk_set=%d len=%lu)", 237 error, pairwise, sm->ptk_set, (unsigned long) rlen); 238 wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen, 239 key_mic); 240 } 241 242 243 static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm) 244 { 245 #ifdef CONFIG_IEEE80211R 246 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) { 247 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len)) 248 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 249 "RSN: Cannot set low order 256 bits of MSK for key management offload"); 250 } else { 251 #endif /* CONFIG_IEEE80211R */ 252 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len)) 253 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 254 "RSN: Cannot set PMK for key management offload"); 255 #ifdef CONFIG_IEEE80211R 256 } 257 #endif /* CONFIG_IEEE80211R */ 258 } 259 260 261 static int wpa_supplicant_get_pmk(struct wpa_sm *sm, 262 const unsigned char *src_addr, 263 const u8 *pmkid) 264 { 265 int abort_cached = 0; 266 267 if (pmkid && !sm->cur_pmksa) { 268 /* When using drivers that generate RSN IE, wpa_supplicant may 269 * not have enough time to get the association information 270 * event before receiving this 1/4 message, so try to find a 271 * matching PMKSA cache entry here. */ 272 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid, 273 NULL, 0); 274 if (sm->cur_pmksa) { 275 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 276 "RSN: found matching PMKID from PMKSA cache"); 277 } else { 278 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 279 "RSN: no matching PMKID found"); 280 abort_cached = 1; 281 } 282 } 283 284 if (pmkid && sm->cur_pmksa && 285 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) { 286 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN); 287 wpa_sm_set_pmk_from_pmksa(sm); 288 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache", 289 sm->pmk, sm->pmk_len); 290 eapol_sm_notify_cached(sm->eapol); 291 #ifdef CONFIG_IEEE80211R 292 sm->xxkey_len = 0; 293 #ifdef CONFIG_SAE 294 if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE && 295 sm->pmk_len == PMK_LEN) { 296 /* Need to allow FT key derivation to proceed with 297 * PMK from SAE being used as the XXKey in cases where 298 * the PMKID in msg 1/4 matches the PMKSA entry that was 299 * just added based on SAE authentication for the 300 * initial mobility domain association. */ 301 os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len); 302 sm->xxkey_len = sm->pmk_len; 303 } 304 #endif /* CONFIG_SAE */ 305 #endif /* CONFIG_IEEE80211R */ 306 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) { 307 int res, pmk_len; 308 309 if (wpa_key_mgmt_sha384(sm->key_mgmt)) 310 pmk_len = PMK_LEN_SUITE_B_192; 311 else 312 pmk_len = PMK_LEN; 313 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len); 314 if (res) { 315 if (pmk_len == PMK_LEN) { 316 /* 317 * EAP-LEAP is an exception from other EAP 318 * methods: it uses only 16-byte PMK. 319 */ 320 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16); 321 pmk_len = 16; 322 } 323 } else { 324 #ifdef CONFIG_IEEE80211R 325 u8 buf[2 * PMK_LEN]; 326 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) 327 { 328 if (wpa_key_mgmt_sha384(sm->key_mgmt)) { 329 os_memcpy(sm->xxkey, buf, 330 SHA384_MAC_LEN); 331 sm->xxkey_len = SHA384_MAC_LEN; 332 } else { 333 os_memcpy(sm->xxkey, buf + PMK_LEN, 334 PMK_LEN); 335 sm->xxkey_len = PMK_LEN; 336 } 337 os_memset(buf, 0, sizeof(buf)); 338 } 339 #endif /* CONFIG_IEEE80211R */ 340 } 341 if (res == 0) { 342 struct rsn_pmksa_cache_entry *sa = NULL; 343 const u8 *fils_cache_id = NULL; 344 345 #ifdef CONFIG_FILS 346 if (sm->fils_cache_id_set) 347 fils_cache_id = sm->fils_cache_id; 348 #endif /* CONFIG_FILS */ 349 350 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state " 351 "machines", sm->pmk, pmk_len); 352 sm->pmk_len = pmk_len; 353 wpa_supplicant_key_mgmt_set_pmk(sm); 354 if (sm->proto == WPA_PROTO_RSN && 355 !wpa_key_mgmt_suite_b(sm->key_mgmt) && 356 !wpa_key_mgmt_ft(sm->key_mgmt)) { 357 sa = pmksa_cache_add(sm->pmksa, 358 sm->pmk, pmk_len, NULL, 359 NULL, 0, 360 src_addr, sm->own_addr, 361 sm->network_ctx, 362 sm->key_mgmt, 363 fils_cache_id); 364 } 365 if (!sm->cur_pmksa && pmkid && 366 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL, 367 0)) { 368 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 369 "RSN: the new PMK matches with the " 370 "PMKID"); 371 abort_cached = 0; 372 } else if (sa && !sm->cur_pmksa && pmkid) { 373 /* 374 * It looks like the authentication server 375 * derived mismatching MSK. This should not 376 * really happen, but bugs happen.. There is not 377 * much we can do here without knowing what 378 * exactly caused the server to misbehave. 379 */ 380 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 381 "RSN: PMKID mismatch - authentication server may have derived different MSK?!"); 382 return -1; 383 } 384 385 if (!sm->cur_pmksa) 386 sm->cur_pmksa = sa; 387 } else { 388 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 389 "WPA: Failed to get master session key from " 390 "EAPOL state machines - key handshake " 391 "aborted"); 392 if (sm->cur_pmksa) { 393 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 394 "RSN: Cancelled PMKSA caching " 395 "attempt"); 396 sm->cur_pmksa = NULL; 397 abort_cached = 1; 398 } else if (!abort_cached) { 399 return -1; 400 } 401 } 402 } 403 404 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && 405 !wpa_key_mgmt_suite_b(sm->key_mgmt) && 406 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN) 407 { 408 /* Send EAPOL-Start to trigger full EAP authentication. */ 409 u8 *buf; 410 size_t buflen; 411 412 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 413 "RSN: no PMKSA entry found - trigger " 414 "full EAP authentication"); 415 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START, 416 NULL, 0, &buflen, NULL); 417 if (buf) { 418 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL, 419 buf, buflen); 420 os_free(buf); 421 return -2; 422 } 423 424 return -1; 425 } 426 427 return 0; 428 } 429 430 431 /** 432 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake 433 * @sm: Pointer to WPA state machine data from wpa_sm_init() 434 * @dst: Destination address for the frame 435 * @key: Pointer to the EAPOL-Key frame header 436 * @ver: Version bits from EAPOL-Key Key Info 437 * @nonce: Nonce value for the EAPOL-Key frame 438 * @wpa_ie: WPA/RSN IE 439 * @wpa_ie_len: Length of the WPA/RSN IE 440 * @ptk: PTK to use for keyed hash and encryption 441 * Returns: >= 0 on success, < 0 on failure 442 */ 443 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst, 444 const struct wpa_eapol_key *key, 445 int ver, const u8 *nonce, 446 const u8 *wpa_ie, size_t wpa_ie_len, 447 struct wpa_ptk *ptk) 448 { 449 size_t mic_len, hdrlen, rlen; 450 struct wpa_eapol_key *reply; 451 u8 *rbuf, *key_mic; 452 u8 *rsn_ie_buf = NULL; 453 u16 key_info; 454 455 if (wpa_ie == NULL) { 456 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - " 457 "cannot generate msg 2/4"); 458 return -1; 459 } 460 461 #ifdef CONFIG_IEEE80211R 462 if (wpa_key_mgmt_ft(sm->key_mgmt)) { 463 int res; 464 465 /* 466 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and 467 * FTIE from (Re)Association Response. 468 */ 469 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN + 470 sm->assoc_resp_ies_len); 471 if (rsn_ie_buf == NULL) 472 return -1; 473 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len); 474 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len, 475 sm->pmk_r1_name); 476 if (res < 0) { 477 os_free(rsn_ie_buf); 478 return -1; 479 } 480 481 if (sm->assoc_resp_ies) { 482 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies, 483 sm->assoc_resp_ies_len); 484 wpa_ie_len += sm->assoc_resp_ies_len; 485 } 486 487 wpa_ie = rsn_ie_buf; 488 } 489 #endif /* CONFIG_IEEE80211R */ 490 491 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len); 492 493 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); 494 hdrlen = sizeof(*reply) + mic_len + 2; 495 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, 496 NULL, hdrlen + wpa_ie_len, 497 &rlen, (void *) &reply); 498 if (rbuf == NULL) { 499 os_free(rsn_ie_buf); 500 return -1; 501 } 502 503 reply->type = (sm->proto == WPA_PROTO_RSN || 504 sm->proto == WPA_PROTO_OSEN) ? 505 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; 506 key_info = ver | WPA_KEY_INFO_KEY_TYPE; 507 if (mic_len) 508 key_info |= WPA_KEY_INFO_MIC; 509 else 510 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; 511 WPA_PUT_BE16(reply->key_info, key_info); 512 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) 513 WPA_PUT_BE16(reply->key_length, 0); 514 else 515 os_memcpy(reply->key_length, key->key_length, 2); 516 os_memcpy(reply->replay_counter, key->replay_counter, 517 WPA_REPLAY_COUNTER_LEN); 518 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter, 519 WPA_REPLAY_COUNTER_LEN); 520 521 key_mic = (u8 *) (reply + 1); 522 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */ 523 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */ 524 os_free(rsn_ie_buf); 525 526 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN); 527 528 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4"); 529 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen, 530 key_mic); 531 } 532 533 534 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr, 535 const struct wpa_eapol_key *key, struct wpa_ptk *ptk) 536 { 537 const u8 *z = NULL; 538 size_t z_len = 0; 539 540 #ifdef CONFIG_IEEE80211R 541 if (wpa_key_mgmt_ft(sm->key_mgmt)) 542 return wpa_derive_ptk_ft(sm, src_addr, key, ptk); 543 #endif /* CONFIG_IEEE80211R */ 544 545 #ifdef CONFIG_DPP2 546 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) { 547 z = wpabuf_head(sm->dpp_z); 548 z_len = wpabuf_len(sm->dpp_z); 549 } 550 #endif /* CONFIG_DPP2 */ 551 552 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion", 553 sm->own_addr, sm->bssid, sm->snonce, 554 key->key_nonce, ptk, sm->key_mgmt, 555 sm->pairwise_cipher, z, z_len); 556 } 557 558 559 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm, 560 const unsigned char *src_addr, 561 const struct wpa_eapol_key *key, 562 u16 ver, const u8 *key_data, 563 size_t key_data_len) 564 { 565 struct wpa_eapol_ie_parse ie; 566 struct wpa_ptk *ptk; 567 int res; 568 u8 *kde, *kde_buf = NULL; 569 size_t kde_len; 570 571 if (wpa_sm_get_network_ctx(sm) == NULL) { 572 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info " 573 "found (msg 1 of 4)"); 574 return; 575 } 576 577 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE); 578 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way " 579 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver); 580 581 os_memset(&ie, 0, sizeof(ie)); 582 583 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) { 584 /* RSN: msg 1/4 should contain PMKID for the selected PMK */ 585 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", 586 key_data, key_data_len); 587 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) 588 goto failed; 589 if (ie.pmkid) { 590 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from " 591 "Authenticator", ie.pmkid, PMKID_LEN); 592 } 593 } 594 595 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid); 596 if (res == -2) { 597 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to " 598 "msg 1/4 - requesting full EAP authentication"); 599 return; 600 } 601 if (res) 602 goto failed; 603 604 if (sm->renew_snonce) { 605 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) { 606 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 607 "WPA: Failed to get random data for SNonce"); 608 goto failed; 609 } 610 sm->renew_snonce = 0; 611 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce", 612 sm->snonce, WPA_NONCE_LEN); 613 } 614 615 /* Calculate PTK which will be stored as a temporary PTK until it has 616 * been verified when processing message 3/4. */ 617 ptk = &sm->tptk; 618 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0) 619 goto failed; 620 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) { 621 u8 buf[8]; 622 /* Supplicant: swap tx/rx Mic keys */ 623 os_memcpy(buf, &ptk->tk[16], 8); 624 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8); 625 os_memcpy(&ptk->tk[24], buf, 8); 626 os_memset(buf, 0, sizeof(buf)); 627 } 628 sm->tptk_set = 1; 629 630 kde = sm->assoc_wpa_ie; 631 kde_len = sm->assoc_wpa_ie_len; 632 633 #ifdef CONFIG_OCV 634 if (wpa_sm_ocv_enabled(sm)) { 635 struct wpa_channel_info ci; 636 u8 *pos; 637 638 if (wpa_sm_channel_info(sm, &ci) != 0) { 639 wpa_printf(MSG_WARNING, 640 "Failed to get channel info for OCI element in EAPOL-Key 2/4"); 641 goto failed; 642 } 643 644 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 3); 645 if (!kde_buf) { 646 wpa_printf(MSG_WARNING, 647 "Failed to allocate memory for KDE with OCI in EAPOL-Key 2/4"); 648 goto failed; 649 } 650 651 os_memcpy(kde_buf, kde, kde_len); 652 kde = kde_buf; 653 pos = kde + kde_len; 654 if (ocv_insert_oci_kde(&ci, &pos) < 0) 655 goto failed; 656 kde_len = pos - kde; 657 } 658 #endif /* CONFIG_OCV */ 659 660 #ifdef CONFIG_P2P 661 if (sm->p2p) { 662 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1); 663 if (kde_buf) { 664 u8 *pos; 665 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE " 666 "into EAPOL-Key 2/4"); 667 os_memcpy(kde_buf, kde, kde_len); 668 kde = kde_buf; 669 pos = kde + kde_len; 670 *pos++ = WLAN_EID_VENDOR_SPECIFIC; 671 *pos++ = RSN_SELECTOR_LEN + 1; 672 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ); 673 pos += RSN_SELECTOR_LEN; 674 *pos++ = 0x01; 675 kde_len = pos - kde; 676 } 677 } 678 #endif /* CONFIG_P2P */ 679 680 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce, 681 kde, kde_len, ptk) < 0) 682 goto failed; 683 684 os_free(kde_buf); 685 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN); 686 return; 687 688 failed: 689 os_free(kde_buf); 690 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED); 691 } 692 693 694 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx) 695 { 696 struct wpa_sm *sm = eloop_ctx; 697 rsn_preauth_candidate_process(sm); 698 } 699 700 701 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm, 702 const u8 *addr, int secure) 703 { 704 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 705 "WPA: Key negotiation completed with " 706 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr), 707 wpa_cipher_txt(sm->pairwise_cipher), 708 wpa_cipher_txt(sm->group_cipher)); 709 wpa_sm_cancel_auth_timeout(sm); 710 wpa_sm_set_state(sm, WPA_COMPLETED); 711 712 if (secure) { 713 wpa_sm_mlme_setprotection( 714 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX, 715 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE); 716 eapol_sm_notify_portValid(sm->eapol, TRUE); 717 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) || 718 sm->key_mgmt == WPA_KEY_MGMT_DPP || 719 sm->key_mgmt == WPA_KEY_MGMT_OWE) 720 eapol_sm_notify_eap_success(sm->eapol, TRUE); 721 /* 722 * Start preauthentication after a short wait to avoid a 723 * possible race condition between the data receive and key 724 * configuration after the 4-Way Handshake. This increases the 725 * likelihood of the first preauth EAPOL-Start frame getting to 726 * the target AP. 727 */ 728 if (!dl_list_empty(&sm->pmksa_candidates)) 729 eloop_register_timeout(1, 0, wpa_sm_start_preauth, 730 sm, NULL); 731 } 732 733 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) { 734 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 735 "RSN: Authenticator accepted " 736 "opportunistic PMKSA entry - marking it valid"); 737 sm->cur_pmksa->opportunistic = 0; 738 } 739 740 #ifdef CONFIG_IEEE80211R 741 if (wpa_key_mgmt_ft(sm->key_mgmt)) { 742 /* Prepare for the next transition */ 743 wpa_ft_prepare_auth_request(sm, NULL); 744 } 745 #endif /* CONFIG_IEEE80211R */ 746 } 747 748 749 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx) 750 { 751 struct wpa_sm *sm = eloop_ctx; 752 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying"); 753 wpa_sm_key_request(sm, 0, 1); 754 } 755 756 757 static int wpa_supplicant_install_ptk(struct wpa_sm *sm, 758 const struct wpa_eapol_key *key) 759 { 760 int keylen, rsclen; 761 enum wpa_alg alg; 762 const u8 *key_rsc; 763 764 if (sm->ptk.installed) { 765 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 766 "WPA: Do not re-install same PTK to the driver"); 767 return 0; 768 } 769 770 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 771 "WPA: Installing PTK to the driver"); 772 773 if (sm->pairwise_cipher == WPA_CIPHER_NONE) { 774 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher " 775 "Suite: NONE - do not use pairwise keys"); 776 return 0; 777 } 778 779 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) { 780 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 781 "WPA: Unsupported pairwise cipher %d", 782 sm->pairwise_cipher); 783 return -1; 784 } 785 786 alg = wpa_cipher_to_alg(sm->pairwise_cipher); 787 keylen = wpa_cipher_key_len(sm->pairwise_cipher); 788 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) { 789 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu", 790 keylen, (long unsigned int) sm->ptk.tk_len); 791 return -1; 792 } 793 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher); 794 795 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) { 796 key_rsc = null_rsc; 797 } else { 798 key_rsc = key->key_rsc; 799 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen); 800 } 801 802 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen, 803 sm->ptk.tk, keylen) < 0) { 804 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 805 "WPA: Failed to set PTK to the " 806 "driver (alg=%d keylen=%d bssid=" MACSTR ")", 807 alg, keylen, MAC2STR(sm->bssid)); 808 return -1; 809 } 810 811 /* TK is not needed anymore in supplicant */ 812 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN); 813 sm->ptk.tk_len = 0; 814 sm->ptk.installed = 1; 815 816 if (sm->wpa_ptk_rekey) { 817 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL); 818 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk, 819 sm, NULL); 820 } 821 822 return 0; 823 } 824 825 826 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm, 827 int group_cipher, 828 int keylen, int maxkeylen, 829 int *key_rsc_len, 830 enum wpa_alg *alg) 831 { 832 int klen; 833 834 *alg = wpa_cipher_to_alg(group_cipher); 835 if (*alg == WPA_ALG_NONE) { 836 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 837 "WPA: Unsupported Group Cipher %d", 838 group_cipher); 839 return -1; 840 } 841 *key_rsc_len = wpa_cipher_rsc_len(group_cipher); 842 843 klen = wpa_cipher_key_len(group_cipher); 844 if (keylen != klen || maxkeylen < klen) { 845 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 846 "WPA: Unsupported %s Group Cipher key length %d (%d)", 847 wpa_cipher_txt(group_cipher), keylen, maxkeylen); 848 return -1; 849 } 850 return 0; 851 } 852 853 854 struct wpa_gtk_data { 855 enum wpa_alg alg; 856 int tx, key_rsc_len, keyidx; 857 u8 gtk[32]; 858 int gtk_len; 859 }; 860 861 862 static int wpa_supplicant_install_gtk(struct wpa_sm *sm, 863 const struct wpa_gtk_data *gd, 864 const u8 *key_rsc, int wnm_sleep) 865 { 866 const u8 *_gtk = gd->gtk; 867 u8 gtk_buf[32]; 868 869 /* Detect possible key reinstallation */ 870 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len && 871 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) || 872 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len && 873 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk, 874 sm->gtk_wnm_sleep.gtk_len) == 0)) { 875 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 876 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)", 877 gd->keyidx, gd->tx, gd->gtk_len); 878 return 0; 879 } 880 881 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len); 882 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 883 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)", 884 gd->keyidx, gd->tx, gd->gtk_len); 885 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len); 886 if (sm->group_cipher == WPA_CIPHER_TKIP) { 887 /* Swap Tx/Rx keys for Michael MIC */ 888 os_memcpy(gtk_buf, gd->gtk, 16); 889 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8); 890 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8); 891 _gtk = gtk_buf; 892 } 893 if (sm->pairwise_cipher == WPA_CIPHER_NONE) { 894 if (wpa_sm_set_key(sm, gd->alg, NULL, 895 gd->keyidx, 1, key_rsc, gd->key_rsc_len, 896 _gtk, gd->gtk_len) < 0) { 897 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 898 "WPA: Failed to set GTK to the driver " 899 "(Group only)"); 900 os_memset(gtk_buf, 0, sizeof(gtk_buf)); 901 return -1; 902 } 903 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr, 904 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len, 905 _gtk, gd->gtk_len) < 0) { 906 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 907 "WPA: Failed to set GTK to " 908 "the driver (alg=%d keylen=%d keyidx=%d)", 909 gd->alg, gd->gtk_len, gd->keyidx); 910 os_memset(gtk_buf, 0, sizeof(gtk_buf)); 911 return -1; 912 } 913 os_memset(gtk_buf, 0, sizeof(gtk_buf)); 914 915 if (wnm_sleep) { 916 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len; 917 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk, 918 sm->gtk_wnm_sleep.gtk_len); 919 } else { 920 sm->gtk.gtk_len = gd->gtk_len; 921 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len); 922 } 923 924 return 0; 925 } 926 927 928 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm, 929 int tx) 930 { 931 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) { 932 /* Ignore Tx bit for GTK if a pairwise key is used. One AP 933 * seemed to set this bit (incorrectly, since Tx is only when 934 * doing Group Key only APs) and without this workaround, the 935 * data connection does not work because wpa_supplicant 936 * configured non-zero keyidx to be used for unicast. */ 937 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 938 "WPA: Tx bit set for GTK, but pairwise " 939 "keys are used - ignore Tx bit"); 940 return 0; 941 } 942 return tx; 943 } 944 945 946 static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm, 947 const u8 *rsc) 948 { 949 int rsclen; 950 951 if (!sm->wpa_rsc_relaxation) 952 return 0; 953 954 rsclen = wpa_cipher_rsc_len(sm->group_cipher); 955 956 /* 957 * Try to detect RSC (endian) corruption issue where the AP sends 958 * the RSC bytes in EAPOL-Key message in the wrong order, both if 959 * it's actually a 6-byte field (as it should be) and if it treats 960 * it as an 8-byte field. 961 * An AP model known to have this bug is the Sapido RB-1632. 962 */ 963 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) { 964 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 965 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0", 966 rsc[0], rsc[1], rsc[2], rsc[3], 967 rsc[4], rsc[5], rsc[6], rsc[7]); 968 969 return 1; 970 } 971 972 return 0; 973 } 974 975 976 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm, 977 const struct wpa_eapol_key *key, 978 const u8 *gtk, size_t gtk_len, 979 int key_info) 980 { 981 struct wpa_gtk_data gd; 982 const u8 *key_rsc; 983 984 /* 985 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x 986 * GTK KDE format: 987 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7] 988 * Reserved [bits 0-7] 989 * GTK 990 */ 991 992 os_memset(&gd, 0, sizeof(gd)); 993 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake", 994 gtk, gtk_len); 995 996 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk)) 997 return -1; 998 999 gd.keyidx = gtk[0] & 0x3; 1000 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm, 1001 !!(gtk[0] & BIT(2))); 1002 gtk += 2; 1003 gtk_len -= 2; 1004 1005 os_memcpy(gd.gtk, gtk, gtk_len); 1006 gd.gtk_len = gtk_len; 1007 1008 key_rsc = key->key_rsc; 1009 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc)) 1010 key_rsc = null_rsc; 1011 1012 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED && 1013 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, 1014 gtk_len, gtk_len, 1015 &gd.key_rsc_len, &gd.alg) || 1016 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) { 1017 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 1018 "RSN: Failed to install GTK"); 1019 os_memset(&gd, 0, sizeof(gd)); 1020 return -1; 1021 } 1022 os_memset(&gd, 0, sizeof(gd)); 1023 1024 return 0; 1025 } 1026 1027 1028 #ifdef CONFIG_IEEE80211W 1029 static int wpa_supplicant_install_igtk(struct wpa_sm *sm, 1030 const struct wpa_igtk_kde *igtk, 1031 int wnm_sleep) 1032 { 1033 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher); 1034 u16 keyidx = WPA_GET_LE16(igtk->keyid); 1035 1036 /* Detect possible key reinstallation */ 1037 if ((sm->igtk.igtk_len == len && 1038 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) || 1039 (sm->igtk_wnm_sleep.igtk_len == len && 1040 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk, 1041 sm->igtk_wnm_sleep.igtk_len) == 0)) { 1042 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 1043 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)", 1044 keyidx); 1045 return 0; 1046 } 1047 1048 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 1049 "WPA: IGTK keyid %d pn " COMPACT_MACSTR, 1050 keyidx, MAC2STR(igtk->pn)); 1051 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len); 1052 if (keyidx > 4095) { 1053 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1054 "WPA: Invalid IGTK KeyID %d", keyidx); 1055 return -1; 1056 } 1057 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher), 1058 broadcast_ether_addr, 1059 keyidx, 0, igtk->pn, sizeof(igtk->pn), 1060 igtk->igtk, len) < 0) { 1061 if (keyidx == 0x0400 || keyidx == 0x0500) { 1062 /* Assume the AP has broken PMF implementation since it 1063 * seems to have swapped the KeyID bytes. The AP cannot 1064 * be trusted to implement BIP correctly or provide a 1065 * valid IGTK, so do not try to configure this key with 1066 * swapped KeyID bytes. Instead, continue without 1067 * configuring the IGTK so that the driver can drop any 1068 * received group-addressed robust management frames due 1069 * to missing keys. 1070 * 1071 * Normally, this error behavior would result in us 1072 * disconnecting, but there are number of deployed APs 1073 * with this broken behavior, so as an interoperability 1074 * workaround, allow the connection to proceed. */ 1075 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 1076 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order"); 1077 } else { 1078 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1079 "WPA: Failed to configure IGTK to the driver"); 1080 return -1; 1081 } 1082 } 1083 1084 if (wnm_sleep) { 1085 sm->igtk_wnm_sleep.igtk_len = len; 1086 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk, 1087 sm->igtk_wnm_sleep.igtk_len); 1088 } else { 1089 sm->igtk.igtk_len = len; 1090 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len); 1091 } 1092 1093 return 0; 1094 } 1095 #endif /* CONFIG_IEEE80211W */ 1096 1097 1098 static int ieee80211w_set_keys(struct wpa_sm *sm, 1099 struct wpa_eapol_ie_parse *ie) 1100 { 1101 #ifdef CONFIG_IEEE80211W 1102 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher)) 1103 return 0; 1104 1105 if (ie->igtk) { 1106 size_t len; 1107 const struct wpa_igtk_kde *igtk; 1108 1109 len = wpa_cipher_key_len(sm->mgmt_group_cipher); 1110 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len) 1111 return -1; 1112 1113 igtk = (const struct wpa_igtk_kde *) ie->igtk; 1114 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0) 1115 return -1; 1116 } 1117 1118 return 0; 1119 #else /* CONFIG_IEEE80211W */ 1120 return 0; 1121 #endif /* CONFIG_IEEE80211W */ 1122 } 1123 1124 1125 static void wpa_report_ie_mismatch(struct wpa_sm *sm, 1126 const char *reason, const u8 *src_addr, 1127 const u8 *wpa_ie, size_t wpa_ie_len, 1128 const u8 *rsn_ie, size_t rsn_ie_len) 1129 { 1130 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")", 1131 reason, MAC2STR(src_addr)); 1132 1133 if (sm->ap_wpa_ie) { 1134 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp", 1135 sm->ap_wpa_ie, sm->ap_wpa_ie_len); 1136 } 1137 if (wpa_ie) { 1138 if (!sm->ap_wpa_ie) { 1139 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 1140 "WPA: No WPA IE in Beacon/ProbeResp"); 1141 } 1142 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg", 1143 wpa_ie, wpa_ie_len); 1144 } 1145 1146 if (sm->ap_rsn_ie) { 1147 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp", 1148 sm->ap_rsn_ie, sm->ap_rsn_ie_len); 1149 } 1150 if (rsn_ie) { 1151 if (!sm->ap_rsn_ie) { 1152 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 1153 "WPA: No RSN IE in Beacon/ProbeResp"); 1154 } 1155 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg", 1156 rsn_ie, rsn_ie_len); 1157 } 1158 1159 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS); 1160 } 1161 1162 1163 #ifdef CONFIG_IEEE80211R 1164 1165 static int ft_validate_mdie(struct wpa_sm *sm, 1166 const unsigned char *src_addr, 1167 struct wpa_eapol_ie_parse *ie, 1168 const u8 *assoc_resp_mdie) 1169 { 1170 struct rsn_mdie *mdie; 1171 1172 mdie = (struct rsn_mdie *) (ie->mdie + 2); 1173 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) || 1174 os_memcmp(mdie->mobility_domain, sm->mobility_domain, 1175 MOBILITY_DOMAIN_ID_LEN) != 0) { 1176 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did " 1177 "not match with the current mobility domain"); 1178 return -1; 1179 } 1180 1181 if (assoc_resp_mdie && 1182 (assoc_resp_mdie[1] != ie->mdie[1] || 1183 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) { 1184 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch"); 1185 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4", 1186 ie->mdie, 2 + ie->mdie[1]); 1187 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response", 1188 assoc_resp_mdie, 2 + assoc_resp_mdie[1]); 1189 return -1; 1190 } 1191 1192 return 0; 1193 } 1194 1195 1196 static int ft_validate_ftie(struct wpa_sm *sm, 1197 const unsigned char *src_addr, 1198 struct wpa_eapol_ie_parse *ie, 1199 const u8 *assoc_resp_ftie) 1200 { 1201 if (ie->ftie == NULL) { 1202 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 1203 "FT: No FTIE in EAPOL-Key msg 3/4"); 1204 return -1; 1205 } 1206 1207 if (assoc_resp_ftie == NULL) 1208 return 0; 1209 1210 if (assoc_resp_ftie[1] != ie->ftie[1] || 1211 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) { 1212 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch"); 1213 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4", 1214 ie->ftie, 2 + ie->ftie[1]); 1215 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response", 1216 assoc_resp_ftie, 2 + assoc_resp_ftie[1]); 1217 return -1; 1218 } 1219 1220 return 0; 1221 } 1222 1223 1224 static int ft_validate_rsnie(struct wpa_sm *sm, 1225 const unsigned char *src_addr, 1226 struct wpa_eapol_ie_parse *ie) 1227 { 1228 struct wpa_ie_data rsn; 1229 1230 if (!ie->rsn_ie) 1231 return 0; 1232 1233 /* 1234 * Verify that PMKR1Name from EAPOL-Key message 3/4 1235 * matches with the value we derived. 1236 */ 1237 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 || 1238 rsn.num_pmkid != 1 || rsn.pmkid == NULL) { 1239 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in " 1240 "FT 4-way handshake message 3/4"); 1241 return -1; 1242 } 1243 1244 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0) 1245 { 1246 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 1247 "FT: PMKR1Name mismatch in " 1248 "FT 4-way handshake message 3/4"); 1249 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator", 1250 rsn.pmkid, WPA_PMK_NAME_LEN); 1251 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name", 1252 sm->pmk_r1_name, WPA_PMK_NAME_LEN); 1253 return -1; 1254 } 1255 1256 return 0; 1257 } 1258 1259 1260 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm, 1261 const unsigned char *src_addr, 1262 struct wpa_eapol_ie_parse *ie) 1263 { 1264 const u8 *pos, *end, *mdie = NULL, *ftie = NULL; 1265 1266 if (sm->assoc_resp_ies) { 1267 pos = sm->assoc_resp_ies; 1268 end = pos + sm->assoc_resp_ies_len; 1269 while (end - pos > 2) { 1270 if (2 + pos[1] > end - pos) 1271 break; 1272 switch (*pos) { 1273 case WLAN_EID_MOBILITY_DOMAIN: 1274 mdie = pos; 1275 break; 1276 case WLAN_EID_FAST_BSS_TRANSITION: 1277 ftie = pos; 1278 break; 1279 } 1280 pos += 2 + pos[1]; 1281 } 1282 } 1283 1284 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 || 1285 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 || 1286 ft_validate_rsnie(sm, src_addr, ie) < 0) 1287 return -1; 1288 1289 return 0; 1290 } 1291 1292 #endif /* CONFIG_IEEE80211R */ 1293 1294 1295 static int wpa_supplicant_validate_ie(struct wpa_sm *sm, 1296 const unsigned char *src_addr, 1297 struct wpa_eapol_ie_parse *ie) 1298 { 1299 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) { 1300 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 1301 "WPA: No WPA/RSN IE for this AP known. " 1302 "Trying to get from scan results"); 1303 if (wpa_sm_get_beacon_ie(sm) < 0) { 1304 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1305 "WPA: Could not find AP from " 1306 "the scan results"); 1307 } else { 1308 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, 1309 "WPA: Found the current AP from " 1310 "updated scan results"); 1311 } 1312 } 1313 1314 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL && 1315 (sm->ap_wpa_ie || sm->ap_rsn_ie)) { 1316 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match " 1317 "with IE in Beacon/ProbeResp (no IE?)", 1318 src_addr, ie->wpa_ie, ie->wpa_ie_len, 1319 ie->rsn_ie, ie->rsn_ie_len); 1320 return -1; 1321 } 1322 1323 if ((ie->wpa_ie && sm->ap_wpa_ie && 1324 (ie->wpa_ie_len != sm->ap_wpa_ie_len || 1325 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) || 1326 (ie->rsn_ie && sm->ap_rsn_ie && 1327 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt), 1328 sm->ap_rsn_ie, sm->ap_rsn_ie_len, 1329 ie->rsn_ie, ie->rsn_ie_len))) { 1330 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match " 1331 "with IE in Beacon/ProbeResp", 1332 src_addr, ie->wpa_ie, ie->wpa_ie_len, 1333 ie->rsn_ie, ie->rsn_ie_len); 1334 return -1; 1335 } 1336 1337 if (sm->proto == WPA_PROTO_WPA && 1338 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) { 1339 wpa_report_ie_mismatch(sm, "Possible downgrade attack " 1340 "detected - RSN was enabled and RSN IE " 1341 "was in msg 3/4, but not in " 1342 "Beacon/ProbeResp", 1343 src_addr, ie->wpa_ie, ie->wpa_ie_len, 1344 ie->rsn_ie, ie->rsn_ie_len); 1345 return -1; 1346 } 1347 1348 #ifdef CONFIG_IEEE80211R 1349 if (wpa_key_mgmt_ft(sm->key_mgmt) && 1350 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0) 1351 return -1; 1352 #endif /* CONFIG_IEEE80211R */ 1353 1354 return 0; 1355 } 1356 1357 1358 /** 1359 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake 1360 * @sm: Pointer to WPA state machine data from wpa_sm_init() 1361 * @dst: Destination address for the frame 1362 * @key: Pointer to the EAPOL-Key frame header 1363 * @ver: Version bits from EAPOL-Key Key Info 1364 * @key_info: Key Info 1365 * @ptk: PTK to use for keyed hash and encryption 1366 * Returns: >= 0 on success, < 0 on failure 1367 */ 1368 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst, 1369 const struct wpa_eapol_key *key, 1370 u16 ver, u16 key_info, 1371 struct wpa_ptk *ptk) 1372 { 1373 size_t mic_len, hdrlen, rlen; 1374 struct wpa_eapol_key *reply; 1375 u8 *rbuf, *key_mic; 1376 1377 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); 1378 hdrlen = sizeof(*reply) + mic_len + 2; 1379 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL, 1380 hdrlen, &rlen, (void *) &reply); 1381 if (rbuf == NULL) 1382 return -1; 1383 1384 reply->type = (sm->proto == WPA_PROTO_RSN || 1385 sm->proto == WPA_PROTO_OSEN) ? 1386 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; 1387 key_info &= WPA_KEY_INFO_SECURE; 1388 key_info |= ver | WPA_KEY_INFO_KEY_TYPE; 1389 if (mic_len) 1390 key_info |= WPA_KEY_INFO_MIC; 1391 else 1392 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; 1393 WPA_PUT_BE16(reply->key_info, key_info); 1394 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) 1395 WPA_PUT_BE16(reply->key_length, 0); 1396 else 1397 os_memcpy(reply->key_length, key->key_length, 2); 1398 os_memcpy(reply->replay_counter, key->replay_counter, 1399 WPA_REPLAY_COUNTER_LEN); 1400 1401 key_mic = (u8 *) (reply + 1); 1402 WPA_PUT_BE16(key_mic + mic_len, 0); 1403 1404 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4"); 1405 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen, 1406 key_mic); 1407 } 1408 1409 1410 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm, 1411 const struct wpa_eapol_key *key, 1412 u16 ver, const u8 *key_data, 1413 size_t key_data_len) 1414 { 1415 u16 key_info, keylen; 1416 struct wpa_eapol_ie_parse ie; 1417 1418 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE); 1419 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 3 of 4-Way " 1420 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver); 1421 1422 key_info = WPA_GET_BE16(key->key_info); 1423 1424 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len); 1425 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) 1426 goto failed; 1427 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { 1428 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1429 "WPA: GTK IE in unencrypted key data"); 1430 goto failed; 1431 } 1432 #ifdef CONFIG_IEEE80211W 1433 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { 1434 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1435 "WPA: IGTK KDE in unencrypted key data"); 1436 goto failed; 1437 } 1438 1439 if (ie.igtk && 1440 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) && 1441 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN + 1442 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) { 1443 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1444 "WPA: Invalid IGTK KDE length %lu", 1445 (unsigned long) ie.igtk_len); 1446 goto failed; 1447 } 1448 #endif /* CONFIG_IEEE80211W */ 1449 1450 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0) 1451 goto failed; 1452 1453 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) { 1454 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1455 "WPA: ANonce from message 1 of 4-Way Handshake " 1456 "differs from 3 of 4-Way Handshake - drop packet (src=" 1457 MACSTR ")", MAC2STR(sm->bssid)); 1458 goto failed; 1459 } 1460 1461 keylen = WPA_GET_BE16(key->key_length); 1462 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) { 1463 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1464 "WPA: Invalid %s key length %d (src=" MACSTR 1465 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen, 1466 MAC2STR(sm->bssid)); 1467 goto failed; 1468 } 1469 1470 #ifdef CONFIG_P2P 1471 if (ie.ip_addr_alloc) { 1472 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4); 1473 wpa_hexdump(MSG_DEBUG, "P2P: IP address info", 1474 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr)); 1475 } 1476 #endif /* CONFIG_P2P */ 1477 1478 #ifdef CONFIG_OCV 1479 if (wpa_sm_ocv_enabled(sm)) { 1480 struct wpa_channel_info ci; 1481 1482 if (wpa_sm_channel_info(sm, &ci) != 0) { 1483 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1484 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4"); 1485 return; 1486 } 1487 1488 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci, 1489 channel_width_to_int(ci.chanwidth), 1490 ci.seg1_idx) != 0) { 1491 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s", 1492 ocv_errorstr); 1493 return; 1494 } 1495 } 1496 #endif /* CONFIG_OCV */ 1497 1498 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info, 1499 &sm->ptk) < 0) { 1500 goto failed; 1501 } 1502 1503 /* SNonce was successfully used in msg 3/4, so mark it to be renewed 1504 * for the next 4-Way Handshake. If msg 3 is received again, the old 1505 * SNonce will still be used to avoid changing PTK. */ 1506 sm->renew_snonce = 1; 1507 1508 if (key_info & WPA_KEY_INFO_INSTALL) { 1509 if (wpa_supplicant_install_ptk(sm, key)) 1510 goto failed; 1511 } 1512 1513 if (key_info & WPA_KEY_INFO_SECURE) { 1514 wpa_sm_mlme_setprotection( 1515 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX, 1516 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE); 1517 eapol_sm_notify_portValid(sm->eapol, TRUE); 1518 } 1519 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE); 1520 1521 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) { 1522 /* No GTK to be set to the driver */ 1523 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) { 1524 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 1525 "RSN: No GTK KDE included in EAPOL-Key msg 3/4"); 1526 goto failed; 1527 } else if (ie.gtk && 1528 wpa_supplicant_pairwise_gtk(sm, key, 1529 ie.gtk, ie.gtk_len, key_info) < 0) { 1530 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 1531 "RSN: Failed to configure GTK"); 1532 goto failed; 1533 } 1534 1535 if (ieee80211w_set_keys(sm, &ie) < 0) { 1536 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 1537 "RSN: Failed to configure IGTK"); 1538 goto failed; 1539 } 1540 1541 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk) 1542 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1543 key_info & WPA_KEY_INFO_SECURE); 1544 1545 if (ie.gtk) 1546 wpa_sm_set_rekey_offload(sm); 1547 1548 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be 1549 * calculated only after KCK has been derived. Though, do not replace an 1550 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID) 1551 * to avoid unnecessary changes of PMKID while continuing to use the 1552 * same PMK. */ 1553 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) && 1554 !sm->cur_pmksa) { 1555 struct rsn_pmksa_cache_entry *sa; 1556 1557 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL, 1558 sm->ptk.kck, sm->ptk.kck_len, 1559 sm->bssid, sm->own_addr, 1560 sm->network_ctx, sm->key_mgmt, NULL); 1561 if (!sm->cur_pmksa) 1562 sm->cur_pmksa = sa; 1563 } 1564 1565 sm->msg_3_of_4_ok = 1; 1566 return; 1567 1568 failed: 1569 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED); 1570 } 1571 1572 1573 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm, 1574 const u8 *keydata, 1575 size_t keydatalen, 1576 u16 key_info, 1577 struct wpa_gtk_data *gd) 1578 { 1579 int maxkeylen; 1580 struct wpa_eapol_ie_parse ie; 1581 1582 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data", 1583 keydata, keydatalen); 1584 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0) 1585 return -1; 1586 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { 1587 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1588 "WPA: GTK IE in unencrypted key data"); 1589 return -1; 1590 } 1591 if (ie.gtk == NULL) { 1592 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 1593 "WPA: No GTK IE in Group Key msg 1/2"); 1594 return -1; 1595 } 1596 maxkeylen = gd->gtk_len = ie.gtk_len - 2; 1597 1598 #ifdef CONFIG_OCV 1599 if (wpa_sm_ocv_enabled(sm)) { 1600 struct wpa_channel_info ci; 1601 1602 if (wpa_sm_channel_info(sm, &ci) != 0) { 1603 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1604 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2"); 1605 return -1; 1606 } 1607 1608 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci, 1609 channel_width_to_int(ci.chanwidth), 1610 ci.seg1_idx) != 0) { 1611 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s", 1612 ocv_errorstr); 1613 return -1; 1614 } 1615 } 1616 #endif /* CONFIG_OCV */ 1617 1618 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, 1619 gd->gtk_len, maxkeylen, 1620 &gd->key_rsc_len, &gd->alg)) 1621 return -1; 1622 1623 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake", 1624 ie.gtk, ie.gtk_len); 1625 gd->keyidx = ie.gtk[0] & 0x3; 1626 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm, 1627 !!(ie.gtk[0] & BIT(2))); 1628 if (ie.gtk_len - 2 > sizeof(gd->gtk)) { 1629 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 1630 "RSN: Too long GTK in GTK IE (len=%lu)", 1631 (unsigned long) ie.gtk_len - 2); 1632 return -1; 1633 } 1634 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2); 1635 1636 if (ieee80211w_set_keys(sm, &ie) < 0) 1637 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 1638 "RSN: Failed to configure IGTK"); 1639 1640 return 0; 1641 } 1642 1643 1644 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm, 1645 const struct wpa_eapol_key *key, 1646 const u8 *key_data, 1647 size_t key_data_len, u16 key_info, 1648 u16 ver, struct wpa_gtk_data *gd) 1649 { 1650 size_t maxkeylen; 1651 u16 gtk_len; 1652 1653 gtk_len = WPA_GET_BE16(key->key_length); 1654 maxkeylen = key_data_len; 1655 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { 1656 if (maxkeylen < 8) { 1657 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 1658 "WPA: Too short maxkeylen (%lu)", 1659 (unsigned long) maxkeylen); 1660 return -1; 1661 } 1662 maxkeylen -= 8; 1663 } 1664 1665 if (gtk_len > maxkeylen || 1666 wpa_supplicant_check_group_cipher(sm, sm->group_cipher, 1667 gtk_len, maxkeylen, 1668 &gd->key_rsc_len, &gd->alg)) 1669 return -1; 1670 1671 gd->gtk_len = gtk_len; 1672 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >> 1673 WPA_KEY_INFO_KEY_INDEX_SHIFT; 1674 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) { 1675 #ifdef CONFIG_NO_RC4 1676 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1677 "WPA: RC4 not supported in the build"); 1678 return -1; 1679 #else /* CONFIG_NO_RC4 */ 1680 u8 ek[32]; 1681 if (key_data_len > sizeof(gd->gtk)) { 1682 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1683 "WPA: RC4 key data too long (%lu)", 1684 (unsigned long) key_data_len); 1685 return -1; 1686 } 1687 os_memcpy(ek, key->key_iv, 16); 1688 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len); 1689 os_memcpy(gd->gtk, key_data, key_data_len); 1690 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) { 1691 os_memset(ek, 0, sizeof(ek)); 1692 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, 1693 "WPA: RC4 failed"); 1694 return -1; 1695 } 1696 os_memset(ek, 0, sizeof(ek)); 1697 #endif /* CONFIG_NO_RC4 */ 1698 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { 1699 if (maxkeylen % 8) { 1700 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1701 "WPA: Unsupported AES-WRAP len %lu", 1702 (unsigned long) maxkeylen); 1703 return -1; 1704 } 1705 if (maxkeylen > sizeof(gd->gtk)) { 1706 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1707 "WPA: AES-WRAP key data " 1708 "too long (keydatalen=%lu maxkeylen=%lu)", 1709 (unsigned long) key_data_len, 1710 (unsigned long) maxkeylen); 1711 return -1; 1712 } 1713 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8, 1714 key_data, gd->gtk)) { 1715 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1716 "WPA: AES unwrap failed - could not decrypt " 1717 "GTK"); 1718 return -1; 1719 } 1720 } else { 1721 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1722 "WPA: Unsupported key_info type %d", ver); 1723 return -1; 1724 } 1725 gd->tx = wpa_supplicant_gtk_tx_bit_workaround( 1726 sm, !!(key_info & WPA_KEY_INFO_TXRX)); 1727 return 0; 1728 } 1729 1730 1731 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm, 1732 const struct wpa_eapol_key *key, 1733 int ver, u16 key_info) 1734 { 1735 size_t mic_len, hdrlen, rlen; 1736 struct wpa_eapol_key *reply; 1737 u8 *rbuf, *key_mic; 1738 size_t kde_len = 0; 1739 1740 #ifdef CONFIG_OCV 1741 if (wpa_sm_ocv_enabled(sm)) 1742 kde_len = OCV_OCI_KDE_LEN; 1743 #endif /* CONFIG_OCV */ 1744 1745 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); 1746 hdrlen = sizeof(*reply) + mic_len + 2; 1747 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL, 1748 hdrlen + kde_len, &rlen, (void *) &reply); 1749 if (rbuf == NULL) 1750 return -1; 1751 1752 reply->type = (sm->proto == WPA_PROTO_RSN || 1753 sm->proto == WPA_PROTO_OSEN) ? 1754 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; 1755 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK; 1756 key_info |= ver | WPA_KEY_INFO_SECURE; 1757 if (mic_len) 1758 key_info |= WPA_KEY_INFO_MIC; 1759 else 1760 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; 1761 WPA_PUT_BE16(reply->key_info, key_info); 1762 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) 1763 WPA_PUT_BE16(reply->key_length, 0); 1764 else 1765 os_memcpy(reply->key_length, key->key_length, 2); 1766 os_memcpy(reply->replay_counter, key->replay_counter, 1767 WPA_REPLAY_COUNTER_LEN); 1768 1769 key_mic = (u8 *) (reply + 1); 1770 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */ 1771 1772 #ifdef CONFIG_OCV 1773 if (wpa_sm_ocv_enabled(sm)) { 1774 struct wpa_channel_info ci; 1775 u8 *pos; 1776 1777 if (wpa_sm_channel_info(sm, &ci) != 0) { 1778 wpa_printf(MSG_WARNING, 1779 "Failed to get channel info for OCI element in EAPOL-Key 2/2"); 1780 os_free(rbuf); 1781 return -1; 1782 } 1783 1784 pos = key_mic + mic_len + 2; /* Key Data */ 1785 if (ocv_insert_oci_kde(&ci, &pos) < 0) { 1786 os_free(rbuf); 1787 return -1; 1788 } 1789 } 1790 #endif /* CONFIG_OCV */ 1791 1792 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2"); 1793 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL, 1794 rbuf, rlen, key_mic); 1795 } 1796 1797 1798 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm, 1799 const unsigned char *src_addr, 1800 const struct wpa_eapol_key *key, 1801 const u8 *key_data, 1802 size_t key_data_len, u16 ver) 1803 { 1804 u16 key_info; 1805 int rekey, ret; 1806 struct wpa_gtk_data gd; 1807 const u8 *key_rsc; 1808 1809 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) { 1810 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 1811 "WPA: Group Key Handshake started prior to completion of 4-way handshake"); 1812 goto failed; 1813 } 1814 1815 os_memset(&gd, 0, sizeof(gd)); 1816 1817 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED; 1818 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key " 1819 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver); 1820 1821 key_info = WPA_GET_BE16(key->key_info); 1822 1823 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) { 1824 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data, 1825 key_data_len, key_info, 1826 &gd); 1827 } else { 1828 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data, 1829 key_data_len, 1830 key_info, ver, &gd); 1831 } 1832 1833 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE); 1834 1835 if (ret) 1836 goto failed; 1837 1838 key_rsc = key->key_rsc; 1839 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc)) 1840 key_rsc = null_rsc; 1841 1842 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) || 1843 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0) 1844 goto failed; 1845 os_memset(&gd, 0, sizeof(gd)); 1846 1847 if (rekey) { 1848 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying " 1849 "completed with " MACSTR " [GTK=%s]", 1850 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher)); 1851 wpa_sm_cancel_auth_timeout(sm); 1852 wpa_sm_set_state(sm, WPA_COMPLETED); 1853 } else { 1854 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1855 key_info & 1856 WPA_KEY_INFO_SECURE); 1857 } 1858 1859 wpa_sm_set_rekey_offload(sm); 1860 1861 return; 1862 1863 failed: 1864 os_memset(&gd, 0, sizeof(gd)); 1865 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED); 1866 } 1867 1868 1869 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm, 1870 struct wpa_eapol_key *key, 1871 u16 ver, 1872 const u8 *buf, size_t len) 1873 { 1874 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN]; 1875 int ok = 0; 1876 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); 1877 1878 os_memcpy(mic, key + 1, mic_len); 1879 if (sm->tptk_set) { 1880 os_memset(key + 1, 0, mic_len); 1881 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, 1882 sm->key_mgmt, 1883 ver, buf, len, (u8 *) (key + 1)) < 0 || 1884 os_memcmp_const(mic, key + 1, mic_len) != 0) { 1885 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1886 "WPA: Invalid EAPOL-Key MIC " 1887 "when using TPTK - ignoring TPTK"); 1888 #ifdef TEST_FUZZ 1889 wpa_printf(MSG_INFO, 1890 "TEST: Ignore Key MIC failure for fuzz testing"); 1891 goto continue_fuzz; 1892 #endif /* TEST_FUZZ */ 1893 } else { 1894 #ifdef TEST_FUZZ 1895 continue_fuzz: 1896 #endif /* TEST_FUZZ */ 1897 ok = 1; 1898 sm->tptk_set = 0; 1899 sm->ptk_set = 1; 1900 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk)); 1901 os_memset(&sm->tptk, 0, sizeof(sm->tptk)); 1902 /* 1903 * This assures the same TPTK in sm->tptk can never be 1904 * copied twice to sm->ptk as the new PTK. In 1905 * combination with the installed flag in the wpa_ptk 1906 * struct, this assures the same PTK is only installed 1907 * once. 1908 */ 1909 sm->renew_snonce = 1; 1910 } 1911 } 1912 1913 if (!ok && sm->ptk_set) { 1914 os_memset(key + 1, 0, mic_len); 1915 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, 1916 sm->key_mgmt, 1917 ver, buf, len, (u8 *) (key + 1)) < 0 || 1918 os_memcmp_const(mic, key + 1, mic_len) != 0) { 1919 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1920 "WPA: Invalid EAPOL-Key MIC - " 1921 "dropping packet"); 1922 #ifdef TEST_FUZZ 1923 wpa_printf(MSG_INFO, 1924 "TEST: Ignore Key MIC failure for fuzz testing"); 1925 goto continue_fuzz2; 1926 #endif /* TEST_FUZZ */ 1927 return -1; 1928 } 1929 #ifdef TEST_FUZZ 1930 continue_fuzz2: 1931 #endif /* TEST_FUZZ */ 1932 ok = 1; 1933 } 1934 1935 if (!ok) { 1936 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1937 "WPA: Could not verify EAPOL-Key MIC - " 1938 "dropping packet"); 1939 return -1; 1940 } 1941 1942 os_memcpy(sm->rx_replay_counter, key->replay_counter, 1943 WPA_REPLAY_COUNTER_LEN); 1944 sm->rx_replay_counter_set = 1; 1945 return 0; 1946 } 1947 1948 1949 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */ 1950 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm, 1951 struct wpa_eapol_key *key, 1952 size_t mic_len, u16 ver, 1953 u8 *key_data, size_t *key_data_len) 1954 { 1955 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data", 1956 key_data, *key_data_len); 1957 if (!sm->ptk_set) { 1958 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1959 "WPA: PTK not available, cannot decrypt EAPOL-Key Key " 1960 "Data"); 1961 return -1; 1962 } 1963 1964 /* Decrypt key data here so that this operation does not need 1965 * to be implemented separately for each message type. */ 1966 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) { 1967 #ifdef CONFIG_NO_RC4 1968 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1969 "WPA: RC4 not supported in the build"); 1970 return -1; 1971 #else /* CONFIG_NO_RC4 */ 1972 u8 ek[32]; 1973 1974 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4"); 1975 os_memcpy(ek, key->key_iv, 16); 1976 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len); 1977 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) { 1978 os_memset(ek, 0, sizeof(ek)); 1979 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, 1980 "WPA: RC4 failed"); 1981 return -1; 1982 } 1983 os_memset(ek, 0, sizeof(ek)); 1984 #endif /* CONFIG_NO_RC4 */ 1985 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || 1986 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC || 1987 wpa_use_aes_key_wrap(sm->key_mgmt)) { 1988 u8 *buf; 1989 1990 wpa_printf(MSG_DEBUG, 1991 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)", 1992 (unsigned int) sm->ptk.kek_len); 1993 if (*key_data_len < 8 || *key_data_len % 8) { 1994 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 1995 "WPA: Unsupported AES-WRAP len %u", 1996 (unsigned int) *key_data_len); 1997 return -1; 1998 } 1999 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */ 2000 buf = os_malloc(*key_data_len); 2001 if (buf == NULL) { 2002 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 2003 "WPA: No memory for AES-UNWRAP buffer"); 2004 return -1; 2005 } 2006 #ifdef TEST_FUZZ 2007 os_memset(buf, 0x11, *key_data_len); 2008 #endif /* TEST_FUZZ */ 2009 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8, 2010 key_data, buf)) { 2011 #ifdef TEST_FUZZ 2012 wpa_printf(MSG_INFO, 2013 "TEST: Ignore AES unwrap failure for fuzz testing"); 2014 goto continue_fuzz; 2015 #endif /* TEST_FUZZ */ 2016 bin_clear_free(buf, *key_data_len); 2017 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 2018 "WPA: AES unwrap failed - " 2019 "could not decrypt EAPOL-Key key data"); 2020 return -1; 2021 } 2022 #ifdef TEST_FUZZ 2023 continue_fuzz: 2024 #endif /* TEST_FUZZ */ 2025 os_memcpy(key_data, buf, *key_data_len); 2026 bin_clear_free(buf, *key_data_len); 2027 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len); 2028 } else { 2029 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 2030 "WPA: Unsupported key_info type %d", ver); 2031 return -1; 2032 } 2033 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data", 2034 key_data, *key_data_len); 2035 return 0; 2036 } 2037 2038 2039 /** 2040 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted 2041 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2042 */ 2043 void wpa_sm_aborted_cached(struct wpa_sm *sm) 2044 { 2045 if (sm && sm->cur_pmksa) { 2046 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 2047 "RSN: Cancelling PMKSA caching attempt"); 2048 sm->cur_pmksa = NULL; 2049 } 2050 } 2051 2052 2053 static void wpa_eapol_key_dump(struct wpa_sm *sm, 2054 const struct wpa_eapol_key *key, 2055 unsigned int key_data_len, 2056 const u8 *mic, unsigned int mic_len) 2057 { 2058 #ifndef CONFIG_NO_STDOUT_DEBUG 2059 u16 key_info = WPA_GET_BE16(key->key_info); 2060 2061 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type); 2062 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 2063 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)", 2064 key_info, key_info & WPA_KEY_INFO_TYPE_MASK, 2065 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >> 2066 WPA_KEY_INFO_KEY_INDEX_SHIFT, 2067 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13, 2068 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group", 2069 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "", 2070 key_info & WPA_KEY_INFO_ACK ? " Ack" : "", 2071 key_info & WPA_KEY_INFO_MIC ? " MIC" : "", 2072 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "", 2073 key_info & WPA_KEY_INFO_ERROR ? " Error" : "", 2074 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "", 2075 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : ""); 2076 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 2077 " key_length=%u key_data_length=%u", 2078 WPA_GET_BE16(key->key_length), key_data_len); 2079 wpa_hexdump(MSG_DEBUG, " replay_counter", 2080 key->replay_counter, WPA_REPLAY_COUNTER_LEN); 2081 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN); 2082 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16); 2083 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8); 2084 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8); 2085 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len); 2086 #endif /* CONFIG_NO_STDOUT_DEBUG */ 2087 } 2088 2089 2090 #ifdef CONFIG_FILS 2091 static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len, 2092 size_t *key_data_len) 2093 { 2094 struct wpa_ptk *ptk; 2095 struct ieee802_1x_hdr *hdr; 2096 struct wpa_eapol_key *key; 2097 u8 *pos, *tmp; 2098 const u8 *aad[1]; 2099 size_t aad_len[1]; 2100 2101 if (*key_data_len < AES_BLOCK_SIZE) { 2102 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame"); 2103 return -1; 2104 } 2105 2106 if (sm->tptk_set) 2107 ptk = &sm->tptk; 2108 else if (sm->ptk_set) 2109 ptk = &sm->ptk; 2110 else 2111 return -1; 2112 2113 hdr = (struct ieee802_1x_hdr *) buf; 2114 key = (struct wpa_eapol_key *) (hdr + 1); 2115 pos = (u8 *) (key + 1); 2116 pos += 2; /* Pointing at the Encrypted Key Data field */ 2117 2118 tmp = os_malloc(*key_data_len); 2119 if (!tmp) 2120 return -1; 2121 2122 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to 2123 * to Key Data (exclusive). */ 2124 aad[0] = buf; 2125 aad_len[0] = pos - buf; 2126 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len, 2127 1, aad, aad_len, tmp) < 0) { 2128 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame"); 2129 bin_clear_free(tmp, *key_data_len); 2130 return -1; 2131 } 2132 2133 /* AEAD decryption and validation completed successfully */ 2134 (*key_data_len) -= AES_BLOCK_SIZE; 2135 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data", 2136 tmp, *key_data_len); 2137 2138 /* Replace Key Data field with the decrypted version */ 2139 os_memcpy(pos, tmp, *key_data_len); 2140 pos -= 2; /* Key Data Length field */ 2141 WPA_PUT_BE16(pos, *key_data_len); 2142 bin_clear_free(tmp, *key_data_len); 2143 2144 if (sm->tptk_set) { 2145 sm->tptk_set = 0; 2146 sm->ptk_set = 1; 2147 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk)); 2148 os_memset(&sm->tptk, 0, sizeof(sm->tptk)); 2149 } 2150 2151 os_memcpy(sm->rx_replay_counter, key->replay_counter, 2152 WPA_REPLAY_COUNTER_LEN); 2153 sm->rx_replay_counter_set = 1; 2154 2155 return 0; 2156 } 2157 #endif /* CONFIG_FILS */ 2158 2159 2160 /** 2161 * wpa_sm_rx_eapol - Process received WPA EAPOL frames 2162 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2163 * @src_addr: Source MAC address of the EAPOL packet 2164 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header) 2165 * @len: Length of the EAPOL frame 2166 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure 2167 * 2168 * This function is called for each received EAPOL frame. Other than EAPOL-Key 2169 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is 2170 * only processing WPA and WPA2 EAPOL-Key frames. 2171 * 2172 * The received EAPOL-Key packets are validated and valid packets are replied 2173 * to. In addition, key material (PTK, GTK) is configured at the end of a 2174 * successful key handshake. 2175 */ 2176 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr, 2177 const u8 *buf, size_t len) 2178 { 2179 size_t plen, data_len, key_data_len; 2180 const struct ieee802_1x_hdr *hdr; 2181 struct wpa_eapol_key *key; 2182 u16 key_info, ver; 2183 u8 *tmp = NULL; 2184 int ret = -1; 2185 u8 *mic, *key_data; 2186 size_t mic_len, keyhdrlen; 2187 2188 #ifdef CONFIG_IEEE80211R 2189 sm->ft_completed = 0; 2190 #endif /* CONFIG_IEEE80211R */ 2191 2192 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); 2193 keyhdrlen = sizeof(*key) + mic_len + 2; 2194 2195 if (len < sizeof(*hdr) + keyhdrlen) { 2196 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 2197 "WPA: EAPOL frame too short to be a WPA " 2198 "EAPOL-Key (len %lu, expecting at least %lu)", 2199 (unsigned long) len, 2200 (unsigned long) sizeof(*hdr) + keyhdrlen); 2201 return 0; 2202 } 2203 2204 hdr = (const struct ieee802_1x_hdr *) buf; 2205 plen = be_to_host16(hdr->length); 2206 data_len = plen + sizeof(*hdr); 2207 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 2208 "IEEE 802.1X RX: version=%d type=%d length=%lu", 2209 hdr->version, hdr->type, (unsigned long) plen); 2210 2211 if (hdr->version < EAPOL_VERSION) { 2212 /* TODO: backwards compatibility */ 2213 } 2214 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) { 2215 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 2216 "WPA: EAPOL frame (type %u) discarded, " 2217 "not a Key frame", hdr->type); 2218 ret = 0; 2219 goto out; 2220 } 2221 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len); 2222 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) { 2223 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 2224 "WPA: EAPOL frame payload size %lu " 2225 "invalid (frame size %lu)", 2226 (unsigned long) plen, (unsigned long) len); 2227 ret = 0; 2228 goto out; 2229 } 2230 if (data_len < len) { 2231 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 2232 "WPA: ignoring %lu bytes after the IEEE 802.1X data", 2233 (unsigned long) len - data_len); 2234 } 2235 2236 /* 2237 * Make a copy of the frame since we need to modify the buffer during 2238 * MAC validation and Key Data decryption. 2239 */ 2240 tmp = os_memdup(buf, data_len); 2241 if (tmp == NULL) 2242 goto out; 2243 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr)); 2244 mic = (u8 *) (key + 1); 2245 key_data = mic + mic_len + 2; 2246 2247 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN) 2248 { 2249 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 2250 "WPA: EAPOL-Key type (%d) unknown, discarded", 2251 key->type); 2252 ret = 0; 2253 goto out; 2254 } 2255 2256 key_data_len = WPA_GET_BE16(mic + mic_len); 2257 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len); 2258 2259 if (key_data_len > plen - keyhdrlen) { 2260 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key " 2261 "frame - key_data overflow (%u > %u)", 2262 (unsigned int) key_data_len, 2263 (unsigned int) (plen - keyhdrlen)); 2264 goto out; 2265 } 2266 2267 eapol_sm_notify_lower_layer_success(sm->eapol, 0); 2268 key_info = WPA_GET_BE16(key->key_info); 2269 ver = key_info & WPA_KEY_INFO_TYPE_MASK; 2270 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && 2271 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W) 2272 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC && 2273 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */ 2274 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES && 2275 !wpa_use_akm_defined(sm->key_mgmt)) { 2276 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 2277 "WPA: Unsupported EAPOL-Key descriptor version %d", 2278 ver); 2279 goto out; 2280 } 2281 2282 if (wpa_use_akm_defined(sm->key_mgmt) && 2283 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) { 2284 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 2285 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)", 2286 ver); 2287 goto out; 2288 } 2289 2290 #ifdef CONFIG_IEEE80211R 2291 if (wpa_key_mgmt_ft(sm->key_mgmt)) { 2292 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */ 2293 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC && 2294 !wpa_use_akm_defined(sm->key_mgmt)) { 2295 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 2296 "FT: AP did not use AES-128-CMAC"); 2297 goto out; 2298 } 2299 } else 2300 #endif /* CONFIG_IEEE80211R */ 2301 #ifdef CONFIG_IEEE80211W 2302 if (wpa_key_mgmt_sha256(sm->key_mgmt)) { 2303 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC && 2304 !wpa_use_akm_defined(sm->key_mgmt)) { 2305 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 2306 "WPA: AP did not use the " 2307 "negotiated AES-128-CMAC"); 2308 goto out; 2309 } 2310 } else 2311 #endif /* CONFIG_IEEE80211W */ 2312 if (sm->pairwise_cipher == WPA_CIPHER_CCMP && 2313 !wpa_use_akm_defined(sm->key_mgmt) && 2314 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { 2315 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 2316 "WPA: CCMP is used, but EAPOL-Key " 2317 "descriptor version (%d) is not 2", ver); 2318 if (sm->group_cipher != WPA_CIPHER_CCMP && 2319 !(key_info & WPA_KEY_INFO_KEY_TYPE)) { 2320 /* Earlier versions of IEEE 802.11i did not explicitly 2321 * require version 2 descriptor for all EAPOL-Key 2322 * packets, so allow group keys to use version 1 if 2323 * CCMP is not used for them. */ 2324 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 2325 "WPA: Backwards compatibility: allow invalid " 2326 "version for non-CCMP group keys"); 2327 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) { 2328 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 2329 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used"); 2330 } else 2331 goto out; 2332 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP && 2333 !wpa_use_akm_defined(sm->key_mgmt) && 2334 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { 2335 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 2336 "WPA: GCMP is used, but EAPOL-Key " 2337 "descriptor version (%d) is not 2", ver); 2338 goto out; 2339 } 2340 2341 if (sm->rx_replay_counter_set && 2342 os_memcmp(key->replay_counter, sm->rx_replay_counter, 2343 WPA_REPLAY_COUNTER_LEN) <= 0) { 2344 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 2345 "WPA: EAPOL-Key Replay Counter did not increase - " 2346 "dropping packet"); 2347 goto out; 2348 } 2349 2350 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) { 2351 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 2352 "WPA: Unsupported SMK bit in key_info"); 2353 goto out; 2354 } 2355 2356 if (!(key_info & WPA_KEY_INFO_ACK)) { 2357 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 2358 "WPA: No Ack bit in key_info"); 2359 goto out; 2360 } 2361 2362 if (key_info & WPA_KEY_INFO_REQUEST) { 2363 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 2364 "WPA: EAPOL-Key with Request bit - dropped"); 2365 goto out; 2366 } 2367 2368 if ((key_info & WPA_KEY_INFO_MIC) && 2369 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len)) 2370 goto out; 2371 2372 #ifdef CONFIG_FILS 2373 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { 2374 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len)) 2375 goto out; 2376 } 2377 #endif /* CONFIG_FILS */ 2378 2379 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) && 2380 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) { 2381 /* 2382 * Only decrypt the Key Data field if the frame's authenticity 2383 * was verified. When using AES-SIV (FILS), the MIC flag is not 2384 * set, so this check should only be performed if mic_len != 0 2385 * which is the case in this code branch. 2386 */ 2387 if (!(key_info & WPA_KEY_INFO_MIC)) { 2388 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 2389 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data"); 2390 goto out; 2391 } 2392 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len, 2393 ver, key_data, 2394 &key_data_len)) 2395 goto out; 2396 } 2397 2398 if (key_info & WPA_KEY_INFO_KEY_TYPE) { 2399 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) { 2400 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 2401 "WPA: Ignored EAPOL-Key (Pairwise) with " 2402 "non-zero key index"); 2403 goto out; 2404 } 2405 if (key_info & (WPA_KEY_INFO_MIC | 2406 WPA_KEY_INFO_ENCR_KEY_DATA)) { 2407 /* 3/4 4-Way Handshake */ 2408 wpa_supplicant_process_3_of_4(sm, key, ver, key_data, 2409 key_data_len); 2410 } else { 2411 /* 1/4 4-Way Handshake */ 2412 wpa_supplicant_process_1_of_4(sm, src_addr, key, 2413 ver, key_data, 2414 key_data_len); 2415 } 2416 } else { 2417 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) || 2418 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) { 2419 /* 1/2 Group Key Handshake */ 2420 wpa_supplicant_process_1_of_2(sm, src_addr, key, 2421 key_data, key_data_len, 2422 ver); 2423 } else { 2424 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 2425 "WPA: EAPOL-Key (Group) without Mic/Encr bit - " 2426 "dropped"); 2427 } 2428 } 2429 2430 ret = 1; 2431 2432 out: 2433 bin_clear_free(tmp, data_len); 2434 return ret; 2435 } 2436 2437 2438 #ifdef CONFIG_CTRL_IFACE 2439 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm) 2440 { 2441 switch (sm->key_mgmt) { 2442 case WPA_KEY_MGMT_IEEE8021X: 2443 return ((sm->proto == WPA_PROTO_RSN || 2444 sm->proto == WPA_PROTO_OSEN) ? 2445 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X : 2446 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X); 2447 case WPA_KEY_MGMT_PSK: 2448 return (sm->proto == WPA_PROTO_RSN ? 2449 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X : 2450 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X); 2451 #ifdef CONFIG_IEEE80211R 2452 case WPA_KEY_MGMT_FT_IEEE8021X: 2453 return RSN_AUTH_KEY_MGMT_FT_802_1X; 2454 case WPA_KEY_MGMT_FT_PSK: 2455 return RSN_AUTH_KEY_MGMT_FT_PSK; 2456 #endif /* CONFIG_IEEE80211R */ 2457 #ifdef CONFIG_IEEE80211W 2458 case WPA_KEY_MGMT_IEEE8021X_SHA256: 2459 return RSN_AUTH_KEY_MGMT_802_1X_SHA256; 2460 case WPA_KEY_MGMT_PSK_SHA256: 2461 return RSN_AUTH_KEY_MGMT_PSK_SHA256; 2462 #endif /* CONFIG_IEEE80211W */ 2463 case WPA_KEY_MGMT_CCKM: 2464 return (sm->proto == WPA_PROTO_RSN ? 2465 RSN_AUTH_KEY_MGMT_CCKM: 2466 WPA_AUTH_KEY_MGMT_CCKM); 2467 case WPA_KEY_MGMT_WPA_NONE: 2468 return WPA_AUTH_KEY_MGMT_NONE; 2469 case WPA_KEY_MGMT_IEEE8021X_SUITE_B: 2470 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B; 2471 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192: 2472 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192; 2473 default: 2474 return 0; 2475 } 2476 } 2477 2478 2479 #define RSN_SUITE "%02x-%02x-%02x-%d" 2480 #define RSN_SUITE_ARG(s) \ 2481 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff 2482 2483 /** 2484 * wpa_sm_get_mib - Dump text list of MIB entries 2485 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2486 * @buf: Buffer for the list 2487 * @buflen: Length of the buffer 2488 * Returns: Number of bytes written to buffer 2489 * 2490 * This function is used fetch dot11 MIB variables. 2491 */ 2492 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen) 2493 { 2494 char pmkid_txt[PMKID_LEN * 2 + 1]; 2495 int rsna, ret; 2496 size_t len; 2497 2498 if (sm->cur_pmksa) { 2499 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt), 2500 sm->cur_pmksa->pmkid, PMKID_LEN); 2501 } else 2502 pmkid_txt[0] = '\0'; 2503 2504 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) || 2505 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) && 2506 sm->proto == WPA_PROTO_RSN) 2507 rsna = 1; 2508 else 2509 rsna = 0; 2510 2511 ret = os_snprintf(buf, buflen, 2512 "dot11RSNAOptionImplemented=TRUE\n" 2513 "dot11RSNAPreauthenticationImplemented=TRUE\n" 2514 "dot11RSNAEnabled=%s\n" 2515 "dot11RSNAPreauthenticationEnabled=%s\n" 2516 "dot11RSNAConfigVersion=%d\n" 2517 "dot11RSNAConfigPairwiseKeysSupported=5\n" 2518 "dot11RSNAConfigGroupCipherSize=%d\n" 2519 "dot11RSNAConfigPMKLifetime=%d\n" 2520 "dot11RSNAConfigPMKReauthThreshold=%d\n" 2521 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n" 2522 "dot11RSNAConfigSATimeout=%d\n", 2523 rsna ? "TRUE" : "FALSE", 2524 rsna ? "TRUE" : "FALSE", 2525 RSN_VERSION, 2526 wpa_cipher_key_len(sm->group_cipher) * 8, 2527 sm->dot11RSNAConfigPMKLifetime, 2528 sm->dot11RSNAConfigPMKReauthThreshold, 2529 sm->dot11RSNAConfigSATimeout); 2530 if (os_snprintf_error(buflen, ret)) 2531 return 0; 2532 len = ret; 2533 2534 ret = os_snprintf( 2535 buf + len, buflen - len, 2536 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n" 2537 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n" 2538 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n" 2539 "dot11RSNAPMKIDUsed=%s\n" 2540 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n" 2541 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n" 2542 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n" 2543 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n" 2544 "dot11RSNA4WayHandshakeFailures=%u\n", 2545 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)), 2546 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto, 2547 sm->pairwise_cipher)), 2548 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto, 2549 sm->group_cipher)), 2550 pmkid_txt, 2551 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)), 2552 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto, 2553 sm->pairwise_cipher)), 2554 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto, 2555 sm->group_cipher)), 2556 sm->dot11RSNA4WayHandshakeFailures); 2557 if (!os_snprintf_error(buflen - len, ret)) 2558 len += ret; 2559 2560 return (int) len; 2561 } 2562 #endif /* CONFIG_CTRL_IFACE */ 2563 2564 2565 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry, 2566 void *ctx, enum pmksa_free_reason reason) 2567 { 2568 struct wpa_sm *sm = ctx; 2569 int deauth = 0; 2570 2571 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: " 2572 MACSTR " reason=%d", MAC2STR(entry->aa), reason); 2573 2574 if (sm->cur_pmksa == entry) { 2575 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 2576 "RSN: %s current PMKSA entry", 2577 reason == PMKSA_REPLACE ? "replaced" : "removed"); 2578 pmksa_cache_clear_current(sm); 2579 2580 /* 2581 * If an entry is simply being replaced, there's no need to 2582 * deauthenticate because it will be immediately re-added. 2583 * This happens when EAP authentication is completed again 2584 * (reauth or failed PMKSA caching attempt). 2585 */ 2586 if (reason != PMKSA_REPLACE) 2587 deauth = 1; 2588 } 2589 2590 if (reason == PMKSA_EXPIRE && 2591 (sm->pmk_len == entry->pmk_len && 2592 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) { 2593 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 2594 "RSN: deauthenticating due to expired PMK"); 2595 pmksa_cache_clear_current(sm); 2596 deauth = 1; 2597 } 2598 2599 if (deauth) { 2600 sm->pmk_len = 0; 2601 os_memset(sm->pmk, 0, sizeof(sm->pmk)); 2602 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED); 2603 } 2604 } 2605 2606 2607 /** 2608 * wpa_sm_init - Initialize WPA state machine 2609 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer 2610 * Returns: Pointer to the allocated WPA state machine data 2611 * 2612 * This function is used to allocate a new WPA state machine and the returned 2613 * value is passed to all WPA state machine calls. 2614 */ 2615 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx) 2616 { 2617 struct wpa_sm *sm; 2618 2619 sm = os_zalloc(sizeof(*sm)); 2620 if (sm == NULL) 2621 return NULL; 2622 dl_list_init(&sm->pmksa_candidates); 2623 sm->renew_snonce = 1; 2624 sm->ctx = ctx; 2625 2626 sm->dot11RSNAConfigPMKLifetime = 43200; 2627 sm->dot11RSNAConfigPMKReauthThreshold = 70; 2628 sm->dot11RSNAConfigSATimeout = 60; 2629 2630 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm); 2631 if (sm->pmksa == NULL) { 2632 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, 2633 "RSN: PMKSA cache initialization failed"); 2634 os_free(sm); 2635 return NULL; 2636 } 2637 2638 return sm; 2639 } 2640 2641 2642 /** 2643 * wpa_sm_deinit - Deinitialize WPA state machine 2644 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2645 */ 2646 void wpa_sm_deinit(struct wpa_sm *sm) 2647 { 2648 if (sm == NULL) 2649 return; 2650 pmksa_cache_deinit(sm->pmksa); 2651 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL); 2652 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL); 2653 os_free(sm->assoc_wpa_ie); 2654 os_free(sm->ap_wpa_ie); 2655 os_free(sm->ap_rsn_ie); 2656 wpa_sm_drop_sa(sm); 2657 os_free(sm->ctx); 2658 #ifdef CONFIG_IEEE80211R 2659 os_free(sm->assoc_resp_ies); 2660 #endif /* CONFIG_IEEE80211R */ 2661 #ifdef CONFIG_TESTING_OPTIONS 2662 wpabuf_free(sm->test_assoc_ie); 2663 #endif /* CONFIG_TESTING_OPTIONS */ 2664 #ifdef CONFIG_FILS_SK_PFS 2665 crypto_ecdh_deinit(sm->fils_ecdh); 2666 #endif /* CONFIG_FILS_SK_PFS */ 2667 #ifdef CONFIG_FILS 2668 wpabuf_free(sm->fils_ft_ies); 2669 #endif /* CONFIG_FILS */ 2670 #ifdef CONFIG_OWE 2671 crypto_ecdh_deinit(sm->owe_ecdh); 2672 #endif /* CONFIG_OWE */ 2673 #ifdef CONFIG_DPP2 2674 wpabuf_clear_free(sm->dpp_z); 2675 #endif /* CONFIG_DPP2 */ 2676 os_free(sm); 2677 } 2678 2679 2680 /** 2681 * wpa_sm_notify_assoc - Notify WPA state machine about association 2682 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2683 * @bssid: The BSSID of the new association 2684 * 2685 * This function is called to let WPA state machine know that the connection 2686 * was established. 2687 */ 2688 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid) 2689 { 2690 int clear_keys = 1; 2691 2692 if (sm == NULL) 2693 return; 2694 2695 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 2696 "WPA: Association event - clear replay counter"); 2697 os_memcpy(sm->bssid, bssid, ETH_ALEN); 2698 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN); 2699 sm->rx_replay_counter_set = 0; 2700 sm->renew_snonce = 1; 2701 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0) 2702 rsn_preauth_deinit(sm); 2703 2704 #ifdef CONFIG_IEEE80211R 2705 if (wpa_ft_is_completed(sm)) { 2706 /* 2707 * Clear portValid to kick EAPOL state machine to re-enter 2708 * AUTHENTICATED state to get the EAPOL port Authorized. 2709 */ 2710 eapol_sm_notify_portValid(sm->eapol, FALSE); 2711 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1); 2712 2713 /* Prepare for the next transition */ 2714 wpa_ft_prepare_auth_request(sm, NULL); 2715 2716 clear_keys = 0; 2717 } 2718 #endif /* CONFIG_IEEE80211R */ 2719 #ifdef CONFIG_FILS 2720 if (sm->fils_completed) { 2721 /* 2722 * Clear portValid to kick EAPOL state machine to re-enter 2723 * AUTHENTICATED state to get the EAPOL port Authorized. 2724 */ 2725 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1); 2726 clear_keys = 0; 2727 } 2728 #endif /* CONFIG_FILS */ 2729 2730 if (clear_keys) { 2731 /* 2732 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if 2733 * this is not part of a Fast BSS Transition. 2734 */ 2735 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK"); 2736 sm->ptk_set = 0; 2737 os_memset(&sm->ptk, 0, sizeof(sm->ptk)); 2738 sm->tptk_set = 0; 2739 os_memset(&sm->tptk, 0, sizeof(sm->tptk)); 2740 os_memset(&sm->gtk, 0, sizeof(sm->gtk)); 2741 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep)); 2742 #ifdef CONFIG_IEEE80211W 2743 os_memset(&sm->igtk, 0, sizeof(sm->igtk)); 2744 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep)); 2745 #endif /* CONFIG_IEEE80211W */ 2746 } 2747 2748 #ifdef CONFIG_TDLS 2749 wpa_tdls_assoc(sm); 2750 #endif /* CONFIG_TDLS */ 2751 2752 #ifdef CONFIG_P2P 2753 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr)); 2754 #endif /* CONFIG_P2P */ 2755 } 2756 2757 2758 /** 2759 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation 2760 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2761 * 2762 * This function is called to let WPA state machine know that the connection 2763 * was lost. This will abort any existing pre-authentication session. 2764 */ 2765 void wpa_sm_notify_disassoc(struct wpa_sm *sm) 2766 { 2767 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL); 2768 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL); 2769 rsn_preauth_deinit(sm); 2770 pmksa_cache_clear_current(sm); 2771 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE) 2772 sm->dot11RSNA4WayHandshakeFailures++; 2773 #ifdef CONFIG_TDLS 2774 wpa_tdls_disassoc(sm); 2775 #endif /* CONFIG_TDLS */ 2776 #ifdef CONFIG_FILS 2777 sm->fils_completed = 0; 2778 #endif /* CONFIG_FILS */ 2779 #ifdef CONFIG_IEEE80211R 2780 sm->ft_reassoc_completed = 0; 2781 #endif /* CONFIG_IEEE80211R */ 2782 2783 /* Keys are not needed in the WPA state machine anymore */ 2784 wpa_sm_drop_sa(sm); 2785 2786 sm->msg_3_of_4_ok = 0; 2787 os_memset(sm->bssid, 0, ETH_ALEN); 2788 } 2789 2790 2791 /** 2792 * wpa_sm_set_pmk - Set PMK 2793 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2794 * @pmk: The new PMK 2795 * @pmk_len: The length of the new PMK in bytes 2796 * @pmkid: Calculated PMKID 2797 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK 2798 * 2799 * Configure the PMK for WPA state machine. 2800 */ 2801 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len, 2802 const u8 *pmkid, const u8 *bssid) 2803 { 2804 if (sm == NULL) 2805 return; 2806 2807 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data", 2808 pmk, pmk_len); 2809 sm->pmk_len = pmk_len; 2810 os_memcpy(sm->pmk, pmk, pmk_len); 2811 2812 #ifdef CONFIG_IEEE80211R 2813 /* Set XXKey to be PSK for FT key derivation */ 2814 sm->xxkey_len = pmk_len; 2815 os_memcpy(sm->xxkey, pmk, pmk_len); 2816 #endif /* CONFIG_IEEE80211R */ 2817 2818 if (bssid) { 2819 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0, 2820 bssid, sm->own_addr, 2821 sm->network_ctx, sm->key_mgmt, NULL); 2822 } 2823 } 2824 2825 2826 /** 2827 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA 2828 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2829 * 2830 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK 2831 * will be cleared. 2832 */ 2833 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm) 2834 { 2835 if (sm == NULL) 2836 return; 2837 2838 if (sm->cur_pmksa) { 2839 wpa_hexdump_key(MSG_DEBUG, 2840 "WPA: Set PMK based on current PMKSA", 2841 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len); 2842 sm->pmk_len = sm->cur_pmksa->pmk_len; 2843 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len); 2844 } else { 2845 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK"); 2846 sm->pmk_len = 0; 2847 os_memset(sm->pmk, 0, PMK_LEN_MAX); 2848 } 2849 } 2850 2851 2852 /** 2853 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled 2854 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2855 * @fast_reauth: Whether fast reauthentication (EAP) is allowed 2856 */ 2857 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth) 2858 { 2859 if (sm) 2860 sm->fast_reauth = fast_reauth; 2861 } 2862 2863 2864 /** 2865 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks 2866 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2867 * @scard_ctx: Context pointer for smartcard related callback functions 2868 */ 2869 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx) 2870 { 2871 if (sm == NULL) 2872 return; 2873 sm->scard_ctx = scard_ctx; 2874 if (sm->preauth_eapol) 2875 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx); 2876 } 2877 2878 2879 /** 2880 * wpa_sm_set_config - Notification of current configration change 2881 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2882 * @config: Pointer to current network configuration 2883 * 2884 * Notify WPA state machine that configuration has changed. config will be 2885 * stored as a backpointer to network configuration. This can be %NULL to clear 2886 * the stored pointed. 2887 */ 2888 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config) 2889 { 2890 if (!sm) 2891 return; 2892 2893 if (config) { 2894 sm->network_ctx = config->network_ctx; 2895 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher; 2896 sm->proactive_key_caching = config->proactive_key_caching; 2897 sm->eap_workaround = config->eap_workaround; 2898 sm->eap_conf_ctx = config->eap_conf_ctx; 2899 if (config->ssid) { 2900 os_memcpy(sm->ssid, config->ssid, config->ssid_len); 2901 sm->ssid_len = config->ssid_len; 2902 } else 2903 sm->ssid_len = 0; 2904 sm->wpa_ptk_rekey = config->wpa_ptk_rekey; 2905 sm->p2p = config->p2p; 2906 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation; 2907 #ifdef CONFIG_FILS 2908 if (config->fils_cache_id) { 2909 sm->fils_cache_id_set = 1; 2910 os_memcpy(sm->fils_cache_id, config->fils_cache_id, 2911 FILS_CACHE_ID_LEN); 2912 } else { 2913 sm->fils_cache_id_set = 0; 2914 } 2915 #endif /* CONFIG_FILS */ 2916 } else { 2917 sm->network_ctx = NULL; 2918 sm->allowed_pairwise_cipher = 0; 2919 sm->proactive_key_caching = 0; 2920 sm->eap_workaround = 0; 2921 sm->eap_conf_ctx = NULL; 2922 sm->ssid_len = 0; 2923 sm->wpa_ptk_rekey = 0; 2924 sm->p2p = 0; 2925 sm->wpa_rsc_relaxation = 0; 2926 } 2927 } 2928 2929 2930 /** 2931 * wpa_sm_set_own_addr - Set own MAC address 2932 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2933 * @addr: Own MAC address 2934 */ 2935 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr) 2936 { 2937 if (sm) 2938 os_memcpy(sm->own_addr, addr, ETH_ALEN); 2939 } 2940 2941 2942 /** 2943 * wpa_sm_set_ifname - Set network interface name 2944 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2945 * @ifname: Interface name 2946 * @bridge_ifname: Optional bridge interface name (for pre-auth) 2947 */ 2948 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname, 2949 const char *bridge_ifname) 2950 { 2951 if (sm) { 2952 sm->ifname = ifname; 2953 sm->bridge_ifname = bridge_ifname; 2954 } 2955 } 2956 2957 2958 /** 2959 * wpa_sm_set_eapol - Set EAPOL state machine pointer 2960 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2961 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init() 2962 */ 2963 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol) 2964 { 2965 if (sm) 2966 sm->eapol = eapol; 2967 } 2968 2969 2970 /** 2971 * wpa_sm_set_param - Set WPA state machine parameters 2972 * @sm: Pointer to WPA state machine data from wpa_sm_init() 2973 * @param: Parameter field 2974 * @value: Parameter value 2975 * Returns: 0 on success, -1 on failure 2976 */ 2977 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param, 2978 unsigned int value) 2979 { 2980 int ret = 0; 2981 2982 if (sm == NULL) 2983 return -1; 2984 2985 switch (param) { 2986 case RSNA_PMK_LIFETIME: 2987 if (value > 0) 2988 sm->dot11RSNAConfigPMKLifetime = value; 2989 else 2990 ret = -1; 2991 break; 2992 case RSNA_PMK_REAUTH_THRESHOLD: 2993 if (value > 0 && value <= 100) 2994 sm->dot11RSNAConfigPMKReauthThreshold = value; 2995 else 2996 ret = -1; 2997 break; 2998 case RSNA_SA_TIMEOUT: 2999 if (value > 0) 3000 sm->dot11RSNAConfigSATimeout = value; 3001 else 3002 ret = -1; 3003 break; 3004 case WPA_PARAM_PROTO: 3005 sm->proto = value; 3006 break; 3007 case WPA_PARAM_PAIRWISE: 3008 sm->pairwise_cipher = value; 3009 break; 3010 case WPA_PARAM_GROUP: 3011 sm->group_cipher = value; 3012 break; 3013 case WPA_PARAM_KEY_MGMT: 3014 sm->key_mgmt = value; 3015 break; 3016 #ifdef CONFIG_IEEE80211W 3017 case WPA_PARAM_MGMT_GROUP: 3018 sm->mgmt_group_cipher = value; 3019 break; 3020 #endif /* CONFIG_IEEE80211W */ 3021 case WPA_PARAM_RSN_ENABLED: 3022 sm->rsn_enabled = value; 3023 break; 3024 case WPA_PARAM_MFP: 3025 sm->mfp = value; 3026 break; 3027 case WPA_PARAM_OCV: 3028 sm->ocv = value; 3029 break; 3030 default: 3031 break; 3032 } 3033 3034 return ret; 3035 } 3036 3037 3038 /** 3039 * wpa_sm_get_status - Get WPA state machine 3040 * @sm: Pointer to WPA state machine data from wpa_sm_init() 3041 * @buf: Buffer for status information 3042 * @buflen: Maximum buffer length 3043 * @verbose: Whether to include verbose status information 3044 * Returns: Number of bytes written to buf. 3045 * 3046 * Query WPA state machine for status information. This function fills in 3047 * a text area with current status information. If the buffer (buf) is not 3048 * large enough, status information will be truncated to fit the buffer. 3049 */ 3050 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen, 3051 int verbose) 3052 { 3053 char *pos = buf, *end = buf + buflen; 3054 int ret; 3055 3056 ret = os_snprintf(pos, end - pos, 3057 "pairwise_cipher=%s\n" 3058 "group_cipher=%s\n" 3059 "key_mgmt=%s\n", 3060 wpa_cipher_txt(sm->pairwise_cipher), 3061 wpa_cipher_txt(sm->group_cipher), 3062 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto)); 3063 if (os_snprintf_error(end - pos, ret)) 3064 return pos - buf; 3065 pos += ret; 3066 3067 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) { 3068 struct wpa_ie_data rsn; 3069 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) 3070 >= 0 && 3071 rsn.capabilities & (WPA_CAPABILITY_MFPR | 3072 WPA_CAPABILITY_MFPC)) { 3073 ret = os_snprintf(pos, end - pos, "pmf=%d\n" 3074 "mgmt_group_cipher=%s\n", 3075 (rsn.capabilities & 3076 WPA_CAPABILITY_MFPR) ? 2 : 1, 3077 wpa_cipher_txt( 3078 sm->mgmt_group_cipher)); 3079 if (os_snprintf_error(end - pos, ret)) 3080 return pos - buf; 3081 pos += ret; 3082 } 3083 } 3084 3085 return pos - buf; 3086 } 3087 3088 3089 int wpa_sm_pmf_enabled(struct wpa_sm *sm) 3090 { 3091 struct wpa_ie_data rsn; 3092 3093 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie) 3094 return 0; 3095 3096 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 && 3097 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC)) 3098 return 1; 3099 3100 return 0; 3101 } 3102 3103 3104 int wpa_sm_ocv_enabled(struct wpa_sm *sm) 3105 { 3106 struct wpa_ie_data rsn; 3107 3108 if (!sm->ocv || !sm->ap_rsn_ie) 3109 return 0; 3110 3111 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, 3112 &rsn) >= 0 && 3113 (rsn.capabilities & WPA_CAPABILITY_OCVC); 3114 } 3115 3116 3117 /** 3118 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration 3119 * @sm: Pointer to WPA state machine data from wpa_sm_init() 3120 * @wpa_ie: Pointer to buffer for WPA/RSN IE 3121 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer 3122 * Returns: 0 on success, -1 on failure 3123 */ 3124 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie, 3125 size_t *wpa_ie_len) 3126 { 3127 int res; 3128 3129 if (sm == NULL) 3130 return -1; 3131 3132 #ifdef CONFIG_TESTING_OPTIONS 3133 if (sm->test_assoc_ie) { 3134 wpa_printf(MSG_DEBUG, 3135 "TESTING: Replace association WPA/RSN IE"); 3136 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie)) 3137 return -1; 3138 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie), 3139 wpabuf_len(sm->test_assoc_ie)); 3140 res = wpabuf_len(sm->test_assoc_ie); 3141 } else 3142 #endif /* CONFIG_TESTING_OPTIONS */ 3143 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len); 3144 if (res < 0) 3145 return -1; 3146 *wpa_ie_len = res; 3147 3148 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default", 3149 wpa_ie, *wpa_ie_len); 3150 3151 if (sm->assoc_wpa_ie == NULL) { 3152 /* 3153 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets 3154 * the correct version of the IE even if PMKSA caching is 3155 * aborted (which would remove PMKID from IE generation). 3156 */ 3157 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len); 3158 if (sm->assoc_wpa_ie == NULL) 3159 return -1; 3160 3161 sm->assoc_wpa_ie_len = *wpa_ie_len; 3162 } else { 3163 wpa_hexdump(MSG_DEBUG, 3164 "WPA: Leave previously set WPA IE default", 3165 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len); 3166 } 3167 3168 return 0; 3169 } 3170 3171 3172 /** 3173 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq 3174 * @sm: Pointer to WPA state machine data from wpa_sm_init() 3175 * @ie: Pointer to IE data (starting from id) 3176 * @len: IE length 3177 * Returns: 0 on success, -1 on failure 3178 * 3179 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association 3180 * Request frame. The IE will be used to override the default value generated 3181 * with wpa_sm_set_assoc_wpa_ie_default(). 3182 */ 3183 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len) 3184 { 3185 if (sm == NULL) 3186 return -1; 3187 3188 os_free(sm->assoc_wpa_ie); 3189 if (ie == NULL || len == 0) { 3190 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 3191 "WPA: clearing own WPA/RSN IE"); 3192 sm->assoc_wpa_ie = NULL; 3193 sm->assoc_wpa_ie_len = 0; 3194 } else { 3195 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len); 3196 sm->assoc_wpa_ie = os_memdup(ie, len); 3197 if (sm->assoc_wpa_ie == NULL) 3198 return -1; 3199 3200 sm->assoc_wpa_ie_len = len; 3201 } 3202 3203 return 0; 3204 } 3205 3206 3207 /** 3208 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp 3209 * @sm: Pointer to WPA state machine data from wpa_sm_init() 3210 * @ie: Pointer to IE data (starting from id) 3211 * @len: IE length 3212 * Returns: 0 on success, -1 on failure 3213 * 3214 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response 3215 * frame. 3216 */ 3217 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len) 3218 { 3219 if (sm == NULL) 3220 return -1; 3221 3222 os_free(sm->ap_wpa_ie); 3223 if (ie == NULL || len == 0) { 3224 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 3225 "WPA: clearing AP WPA IE"); 3226 sm->ap_wpa_ie = NULL; 3227 sm->ap_wpa_ie_len = 0; 3228 } else { 3229 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len); 3230 sm->ap_wpa_ie = os_memdup(ie, len); 3231 if (sm->ap_wpa_ie == NULL) 3232 return -1; 3233 3234 sm->ap_wpa_ie_len = len; 3235 } 3236 3237 return 0; 3238 } 3239 3240 3241 /** 3242 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp 3243 * @sm: Pointer to WPA state machine data from wpa_sm_init() 3244 * @ie: Pointer to IE data (starting from id) 3245 * @len: IE length 3246 * Returns: 0 on success, -1 on failure 3247 * 3248 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response 3249 * frame. 3250 */ 3251 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len) 3252 { 3253 if (sm == NULL) 3254 return -1; 3255 3256 os_free(sm->ap_rsn_ie); 3257 if (ie == NULL || len == 0) { 3258 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 3259 "WPA: clearing AP RSN IE"); 3260 sm->ap_rsn_ie = NULL; 3261 sm->ap_rsn_ie_len = 0; 3262 } else { 3263 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len); 3264 sm->ap_rsn_ie = os_memdup(ie, len); 3265 if (sm->ap_rsn_ie == NULL) 3266 return -1; 3267 3268 sm->ap_rsn_ie_len = len; 3269 } 3270 3271 return 0; 3272 } 3273 3274 3275 /** 3276 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE 3277 * @sm: Pointer to WPA state machine data from wpa_sm_init() 3278 * @data: Pointer to data area for parsing results 3279 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure 3280 * 3281 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the 3282 * parsed data into data. 3283 */ 3284 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data) 3285 { 3286 if (sm == NULL) 3287 return -1; 3288 3289 if (sm->assoc_wpa_ie == NULL) { 3290 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, 3291 "WPA: No WPA/RSN IE available from association info"); 3292 return -1; 3293 } 3294 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data)) 3295 return -2; 3296 return 0; 3297 } 3298 3299 3300 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len) 3301 { 3302 return pmksa_cache_list(sm->pmksa, buf, len); 3303 } 3304 3305 3306 struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm) 3307 { 3308 return pmksa_cache_head(sm->pmksa); 3309 } 3310 3311 3312 struct rsn_pmksa_cache_entry * 3313 wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm, 3314 struct rsn_pmksa_cache_entry * entry) 3315 { 3316 return pmksa_cache_add_entry(sm->pmksa, entry); 3317 } 3318 3319 3320 void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len, 3321 const u8 *pmkid, const u8 *bssid, 3322 const u8 *fils_cache_id) 3323 { 3324 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0, 3325 bssid, sm->own_addr, sm->network_ctx, 3326 sm->key_mgmt, fils_cache_id); 3327 } 3328 3329 3330 int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid, 3331 const void *network_ctx) 3332 { 3333 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL; 3334 } 3335 3336 3337 void wpa_sm_drop_sa(struct wpa_sm *sm) 3338 { 3339 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK"); 3340 sm->ptk_set = 0; 3341 sm->tptk_set = 0; 3342 sm->pmk_len = 0; 3343 os_memset(sm->pmk, 0, sizeof(sm->pmk)); 3344 os_memset(&sm->ptk, 0, sizeof(sm->ptk)); 3345 os_memset(&sm->tptk, 0, sizeof(sm->tptk)); 3346 os_memset(&sm->gtk, 0, sizeof(sm->gtk)); 3347 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep)); 3348 #ifdef CONFIG_IEEE80211W 3349 os_memset(&sm->igtk, 0, sizeof(sm->igtk)); 3350 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep)); 3351 #endif /* CONFIG_IEEE80211W */ 3352 #ifdef CONFIG_IEEE80211R 3353 os_memset(sm->xxkey, 0, sizeof(sm->xxkey)); 3354 sm->xxkey_len = 0; 3355 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0)); 3356 sm->pmk_r0_len = 0; 3357 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1)); 3358 sm->pmk_r1_len = 0; 3359 #endif /* CONFIG_IEEE80211R */ 3360 } 3361 3362 3363 int wpa_sm_has_ptk(struct wpa_sm *sm) 3364 { 3365 if (sm == NULL) 3366 return 0; 3367 return sm->ptk_set; 3368 } 3369 3370 3371 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr) 3372 { 3373 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN); 3374 } 3375 3376 3377 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx) 3378 { 3379 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0); 3380 } 3381 3382 3383 #ifdef CONFIG_WNM 3384 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf) 3385 { 3386 u16 keyinfo; 3387 u8 keylen; /* plaintext key len */ 3388 u8 *key_rsc; 3389 3390 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) { 3391 struct wpa_gtk_data gd; 3392 3393 os_memset(&gd, 0, sizeof(gd)); 3394 keylen = wpa_cipher_key_len(sm->group_cipher); 3395 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher); 3396 gd.alg = wpa_cipher_to_alg(sm->group_cipher); 3397 if (gd.alg == WPA_ALG_NONE) { 3398 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite"); 3399 return -1; 3400 } 3401 3402 key_rsc = buf + 5; 3403 keyinfo = WPA_GET_LE16(buf + 2); 3404 gd.gtk_len = keylen; 3405 if (gd.gtk_len != buf[4]) { 3406 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d", 3407 gd.gtk_len, buf[4]); 3408 return -1; 3409 } 3410 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */ 3411 gd.tx = wpa_supplicant_gtk_tx_bit_workaround( 3412 sm, !!(keyinfo & WPA_KEY_INFO_TXRX)); 3413 3414 os_memcpy(gd.gtk, buf + 13, gd.gtk_len); 3415 3416 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)", 3417 gd.gtk, gd.gtk_len); 3418 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) { 3419 os_memset(&gd, 0, sizeof(gd)); 3420 wpa_printf(MSG_DEBUG, "Failed to install the GTK in " 3421 "WNM mode"); 3422 return -1; 3423 } 3424 os_memset(&gd, 0, sizeof(gd)); 3425 #ifdef CONFIG_IEEE80211W 3426 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) { 3427 const struct wpa_igtk_kde *igtk; 3428 3429 igtk = (const struct wpa_igtk_kde *) (buf + 2); 3430 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0) 3431 return -1; 3432 #endif /* CONFIG_IEEE80211W */ 3433 } else { 3434 wpa_printf(MSG_DEBUG, "Unknown element id"); 3435 return -1; 3436 } 3437 3438 return 0; 3439 } 3440 #endif /* CONFIG_WNM */ 3441 3442 3443 #ifdef CONFIG_P2P 3444 3445 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf) 3446 { 3447 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0) 3448 return -1; 3449 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4); 3450 return 0; 3451 } 3452 3453 #endif /* CONFIG_P2P */ 3454 3455 3456 void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter) 3457 { 3458 if (rx_replay_counter == NULL) 3459 return; 3460 3461 os_memcpy(sm->rx_replay_counter, rx_replay_counter, 3462 WPA_REPLAY_COUNTER_LEN); 3463 sm->rx_replay_counter_set = 1; 3464 wpa_printf(MSG_DEBUG, "Updated key replay counter"); 3465 } 3466 3467 3468 void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm, 3469 const u8 *ptk_kck, size_t ptk_kck_len, 3470 const u8 *ptk_kek, size_t ptk_kek_len) 3471 { 3472 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) { 3473 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len); 3474 sm->ptk.kck_len = ptk_kck_len; 3475 wpa_printf(MSG_DEBUG, "Updated PTK KCK"); 3476 } 3477 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) { 3478 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len); 3479 sm->ptk.kek_len = ptk_kek_len; 3480 wpa_printf(MSG_DEBUG, "Updated PTK KEK"); 3481 } 3482 sm->ptk_set = 1; 3483 } 3484 3485 3486 #ifdef CONFIG_TESTING_OPTIONS 3487 3488 void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf) 3489 { 3490 wpabuf_free(sm->test_assoc_ie); 3491 sm->test_assoc_ie = buf; 3492 } 3493 3494 3495 const u8 * wpa_sm_get_anonce(struct wpa_sm *sm) 3496 { 3497 return sm->anonce; 3498 } 3499 3500 #endif /* CONFIG_TESTING_OPTIONS */ 3501 3502 3503 unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm) 3504 { 3505 return sm->key_mgmt; 3506 } 3507 3508 3509 #ifdef CONFIG_FILS 3510 3511 struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md) 3512 { 3513 struct wpabuf *buf = NULL; 3514 struct wpabuf *erp_msg; 3515 struct wpabuf *pub = NULL; 3516 3517 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol); 3518 if (!erp_msg && !sm->cur_pmksa) { 3519 wpa_printf(MSG_DEBUG, 3520 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS"); 3521 goto fail; 3522 } 3523 3524 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)", 3525 erp_msg != NULL, sm->cur_pmksa != NULL); 3526 3527 sm->fils_completed = 0; 3528 3529 if (!sm->assoc_wpa_ie) { 3530 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS"); 3531 goto fail; 3532 } 3533 3534 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 || 3535 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0) 3536 goto fail; 3537 3538 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce", 3539 sm->fils_nonce, FILS_NONCE_LEN); 3540 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session", 3541 sm->fils_session, FILS_SESSION_LEN); 3542 3543 #ifdef CONFIG_FILS_SK_PFS 3544 sm->fils_dh_group = dh_group; 3545 if (dh_group) { 3546 crypto_ecdh_deinit(sm->fils_ecdh); 3547 sm->fils_ecdh = crypto_ecdh_init(dh_group); 3548 if (!sm->fils_ecdh) { 3549 wpa_printf(MSG_INFO, 3550 "FILS: Could not initialize ECDH with group %d", 3551 dh_group); 3552 goto fail; 3553 } 3554 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1); 3555 if (!pub) 3556 goto fail; 3557 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)", 3558 pub); 3559 sm->fils_dh_elem_len = wpabuf_len(pub); 3560 } 3561 #endif /* CONFIG_FILS_SK_PFS */ 3562 3563 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len + 3564 (pub ? wpabuf_len(pub) : 0)); 3565 if (!buf) 3566 goto fail; 3567 3568 /* Fields following the Authentication algorithm number field */ 3569 3570 /* Authentication Transaction seq# */ 3571 wpabuf_put_le16(buf, 1); 3572 3573 /* Status Code */ 3574 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS); 3575 3576 /* TODO: FILS PK */ 3577 #ifdef CONFIG_FILS_SK_PFS 3578 if (dh_group) { 3579 /* Finite Cyclic Group */ 3580 wpabuf_put_le16(buf, dh_group); 3581 /* Element */ 3582 wpabuf_put_buf(buf, pub); 3583 } 3584 #endif /* CONFIG_FILS_SK_PFS */ 3585 3586 /* RSNE */ 3587 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame", 3588 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len); 3589 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len); 3590 3591 if (md) { 3592 /* MDE when using FILS for FT initial association */ 3593 struct rsn_mdie *mdie; 3594 3595 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN); 3596 wpabuf_put_u8(buf, sizeof(*mdie)); 3597 mdie = wpabuf_put(buf, sizeof(*mdie)); 3598 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN); 3599 mdie->ft_capab = 0; 3600 } 3601 3602 /* FILS Nonce */ 3603 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ 3604 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */ 3605 /* Element ID Extension */ 3606 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE); 3607 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN); 3608 3609 /* FILS Session */ 3610 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ 3611 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */ 3612 /* Element ID Extension */ 3613 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION); 3614 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN); 3615 3616 /* FILS Wrapped Data */ 3617 sm->fils_erp_pmkid_set = 0; 3618 if (erp_msg) { 3619 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ 3620 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */ 3621 /* Element ID Extension */ 3622 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA); 3623 wpabuf_put_buf(buf, erp_msg); 3624 /* Calculate pending PMKID here so that we do not need to 3625 * maintain a copy of the EAP-Initiate/Reauth message. */ 3626 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg), 3627 wpabuf_len(erp_msg), 3628 sm->fils_erp_pmkid) == 0) 3629 sm->fils_erp_pmkid_set = 1; 3630 } 3631 3632 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame", 3633 buf); 3634 3635 fail: 3636 wpabuf_free(erp_msg); 3637 wpabuf_free(pub); 3638 return buf; 3639 } 3640 3641 3642 int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data, 3643 size_t len) 3644 { 3645 const u8 *pos, *end; 3646 struct ieee802_11_elems elems; 3647 struct wpa_ie_data rsn; 3648 int pmkid_match = 0; 3649 u8 ick[FILS_ICK_MAX_LEN]; 3650 size_t ick_len; 3651 int res; 3652 struct wpabuf *dh_ss = NULL; 3653 const u8 *g_sta = NULL; 3654 size_t g_sta_len = 0; 3655 const u8 *g_ap = NULL; 3656 size_t g_ap_len = 0; 3657 struct wpabuf *pub = NULL; 3658 3659 os_memcpy(sm->bssid, bssid, ETH_ALEN); 3660 3661 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields", 3662 data, len); 3663 pos = data; 3664 end = data + len; 3665 3666 /* TODO: FILS PK */ 3667 #ifdef CONFIG_FILS_SK_PFS 3668 if (sm->fils_dh_group) { 3669 u16 group; 3670 3671 /* Using FILS PFS */ 3672 3673 /* Finite Cyclic Group */ 3674 if (end - pos < 2) { 3675 wpa_printf(MSG_DEBUG, 3676 "FILS: No room for Finite Cyclic Group"); 3677 goto fail; 3678 } 3679 group = WPA_GET_LE16(pos); 3680 pos += 2; 3681 if (group != sm->fils_dh_group) { 3682 wpa_printf(MSG_DEBUG, 3683 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)", 3684 group, sm->fils_dh_group); 3685 goto fail; 3686 } 3687 3688 /* Element */ 3689 if ((size_t) (end - pos) < sm->fils_dh_elem_len) { 3690 wpa_printf(MSG_DEBUG, "FILS: No room for Element"); 3691 goto fail; 3692 } 3693 3694 if (!sm->fils_ecdh) { 3695 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available"); 3696 goto fail; 3697 } 3698 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos, 3699 sm->fils_dh_elem_len); 3700 if (!dh_ss) { 3701 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed"); 3702 goto fail; 3703 } 3704 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss); 3705 g_ap = pos; 3706 g_ap_len = sm->fils_dh_elem_len; 3707 pos += sm->fils_dh_elem_len; 3708 } 3709 #endif /* CONFIG_FILS_SK_PFS */ 3710 3711 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos); 3712 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) { 3713 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements"); 3714 goto fail; 3715 } 3716 3717 /* RSNE */ 3718 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie, 3719 elems.rsn_ie_len); 3720 if (!elems.rsn_ie || 3721 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2, 3722 &rsn) < 0) { 3723 wpa_printf(MSG_DEBUG, "FILS: No RSN element"); 3724 goto fail; 3725 } 3726 3727 if (!elems.fils_nonce) { 3728 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field"); 3729 goto fail; 3730 } 3731 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN); 3732 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN); 3733 3734 #ifdef CONFIG_IEEE80211R 3735 if (wpa_key_mgmt_ft(sm->key_mgmt)) { 3736 struct wpa_ft_ies parse; 3737 3738 if (!elems.mdie || !elems.ftie) { 3739 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE"); 3740 goto fail; 3741 } 3742 3743 if (wpa_ft_parse_ies(pos, end - pos, &parse, 3744 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) { 3745 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs"); 3746 goto fail; 3747 } 3748 3749 if (!parse.r0kh_id) { 3750 wpa_printf(MSG_DEBUG, 3751 "FILS+FT: No R0KH-ID subelem in FTE"); 3752 goto fail; 3753 } 3754 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len); 3755 sm->r0kh_id_len = parse.r0kh_id_len; 3756 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID", 3757 sm->r0kh_id, sm->r0kh_id_len); 3758 3759 if (!parse.r1kh_id) { 3760 wpa_printf(MSG_DEBUG, 3761 "FILS+FT: No R1KH-ID subelem in FTE"); 3762 goto fail; 3763 } 3764 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN); 3765 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID", 3766 sm->r1kh_id, FT_R1KH_ID_LEN); 3767 3768 /* TODO: Check MDE and FTE payload */ 3769 3770 wpabuf_free(sm->fils_ft_ies); 3771 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len + 3772 2 + elems.ftie_len); 3773 if (!sm->fils_ft_ies) 3774 goto fail; 3775 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2, 3776 2 + elems.mdie_len); 3777 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2, 3778 2 + elems.ftie_len); 3779 } else { 3780 wpabuf_free(sm->fils_ft_ies); 3781 sm->fils_ft_ies = NULL; 3782 } 3783 #endif /* CONFIG_IEEE80211R */ 3784 3785 /* PMKID List */ 3786 if (rsn.pmkid && rsn.num_pmkid > 0) { 3787 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List", 3788 rsn.pmkid, rsn.num_pmkid * PMKID_LEN); 3789 3790 if (rsn.num_pmkid != 1) { 3791 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection"); 3792 goto fail; 3793 } 3794 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN); 3795 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0) 3796 { 3797 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch"); 3798 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID", 3799 sm->cur_pmksa->pmkid, PMKID_LEN); 3800 goto fail; 3801 } 3802 wpa_printf(MSG_DEBUG, 3803 "FILS: Matching PMKID - continue using PMKSA caching"); 3804 pmkid_match = 1; 3805 } 3806 if (!pmkid_match && sm->cur_pmksa) { 3807 wpa_printf(MSG_DEBUG, 3808 "FILS: No PMKID match - cannot use cached PMKSA entry"); 3809 sm->cur_pmksa = NULL; 3810 } 3811 3812 /* FILS Session */ 3813 if (!elems.fils_session) { 3814 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element"); 3815 goto fail; 3816 } 3817 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session, 3818 FILS_SESSION_LEN); 3819 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN) 3820 != 0) { 3821 wpa_printf(MSG_DEBUG, "FILS: Session mismatch"); 3822 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session", 3823 sm->fils_session, FILS_SESSION_LEN); 3824 goto fail; 3825 } 3826 3827 /* FILS Wrapped Data */ 3828 if (!sm->cur_pmksa && elems.fils_wrapped_data) { 3829 u8 rmsk[ERP_MAX_KEY_LEN]; 3830 size_t rmsk_len; 3831 3832 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data", 3833 elems.fils_wrapped_data, 3834 elems.fils_wrapped_data_len); 3835 eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data, 3836 elems.fils_wrapped_data_len); 3837 if (eapol_sm_failed(sm->eapol)) 3838 goto fail; 3839 3840 rmsk_len = ERP_MAX_KEY_LEN; 3841 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len); 3842 if (res == PMK_LEN) { 3843 rmsk_len = PMK_LEN; 3844 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len); 3845 } 3846 if (res) 3847 goto fail; 3848 3849 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len, 3850 sm->fils_nonce, sm->fils_anonce, 3851 dh_ss ? wpabuf_head(dh_ss) : NULL, 3852 dh_ss ? wpabuf_len(dh_ss) : 0, 3853 sm->pmk, &sm->pmk_len); 3854 os_memset(rmsk, 0, sizeof(rmsk)); 3855 3856 /* Don't use DHss in PTK derivation if PMKSA caching is not 3857 * used. */ 3858 wpabuf_clear_free(dh_ss); 3859 dh_ss = NULL; 3860 3861 if (res) 3862 goto fail; 3863 3864 if (!sm->fils_erp_pmkid_set) { 3865 wpa_printf(MSG_DEBUG, "FILS: PMKID not available"); 3866 goto fail; 3867 } 3868 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid, 3869 PMKID_LEN); 3870 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result"); 3871 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, 3872 sm->fils_erp_pmkid, NULL, 0, 3873 sm->bssid, sm->own_addr, 3874 sm->network_ctx, sm->key_mgmt, 3875 NULL); 3876 } 3877 3878 if (!sm->cur_pmksa) { 3879 wpa_printf(MSG_DEBUG, 3880 "FILS: No remaining options to continue FILS authentication"); 3881 goto fail; 3882 } 3883 3884 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid, 3885 sm->fils_nonce, sm->fils_anonce, 3886 dh_ss ? wpabuf_head(dh_ss) : NULL, 3887 dh_ss ? wpabuf_len(dh_ss) : 0, 3888 &sm->ptk, ick, &ick_len, 3889 sm->key_mgmt, sm->pairwise_cipher, 3890 sm->fils_ft, &sm->fils_ft_len) < 0) { 3891 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK"); 3892 goto fail; 3893 } 3894 3895 wpabuf_clear_free(dh_ss); 3896 dh_ss = NULL; 3897 3898 sm->ptk_set = 1; 3899 sm->tptk_set = 0; 3900 os_memset(&sm->tptk, 0, sizeof(sm->tptk)); 3901 3902 #ifdef CONFIG_FILS_SK_PFS 3903 if (sm->fils_dh_group) { 3904 if (!sm->fils_ecdh) { 3905 wpa_printf(MSG_INFO, "FILS: ECDH not initialized"); 3906 goto fail; 3907 } 3908 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1); 3909 if (!pub) 3910 goto fail; 3911 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub); 3912 g_sta = wpabuf_head(pub); 3913 g_sta_len = wpabuf_len(pub); 3914 if (!g_ap) { 3915 wpa_printf(MSG_INFO, "FILS: gAP not available"); 3916 goto fail; 3917 } 3918 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len); 3919 } 3920 #endif /* CONFIG_FILS_SK_PFS */ 3921 3922 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce, 3923 sm->fils_anonce, sm->own_addr, sm->bssid, 3924 g_sta, g_sta_len, g_ap, g_ap_len, 3925 sm->key_mgmt, sm->fils_key_auth_sta, 3926 sm->fils_key_auth_ap, 3927 &sm->fils_key_auth_len); 3928 wpabuf_free(pub); 3929 os_memset(ick, 0, sizeof(ick)); 3930 return res; 3931 fail: 3932 wpabuf_free(pub); 3933 wpabuf_clear_free(dh_ss); 3934 return -1; 3935 } 3936 3937 3938 #ifdef CONFIG_IEEE80211R 3939 static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf) 3940 { 3941 struct rsn_ie_hdr *rsnie; 3942 u16 capab; 3943 u8 *pos; 3944 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt); 3945 3946 /* RSNIE[PMKR0Name/PMKR1Name] */ 3947 rsnie = wpabuf_put(buf, sizeof(*rsnie)); 3948 rsnie->elem_id = WLAN_EID_RSN; 3949 WPA_PUT_LE16(rsnie->version, RSN_VERSION); 3950 3951 /* Group Suite Selector */ 3952 if (!wpa_cipher_valid_group(sm->group_cipher)) { 3953 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)", 3954 sm->group_cipher); 3955 return -1; 3956 } 3957 pos = wpabuf_put(buf, RSN_SELECTOR_LEN); 3958 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN, 3959 sm->group_cipher)); 3960 3961 /* Pairwise Suite Count */ 3962 wpabuf_put_le16(buf, 1); 3963 3964 /* Pairwise Suite List */ 3965 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) { 3966 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)", 3967 sm->pairwise_cipher); 3968 return -1; 3969 } 3970 pos = wpabuf_put(buf, RSN_SELECTOR_LEN); 3971 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN, 3972 sm->pairwise_cipher)); 3973 3974 /* Authenticated Key Management Suite Count */ 3975 wpabuf_put_le16(buf, 1); 3976 3977 /* Authenticated Key Management Suite List */ 3978 pos = wpabuf_put(buf, RSN_SELECTOR_LEN); 3979 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256) 3980 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256); 3981 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384) 3982 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384); 3983 else { 3984 wpa_printf(MSG_WARNING, 3985 "FILS+FT: Invalid key management type (%d)", 3986 sm->key_mgmt); 3987 return -1; 3988 } 3989 3990 /* RSN Capabilities */ 3991 capab = 0; 3992 #ifdef CONFIG_IEEE80211W 3993 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) 3994 capab |= WPA_CAPABILITY_MFPC; 3995 #endif /* CONFIG_IEEE80211W */ 3996 if (sm->ocv) 3997 capab |= WPA_CAPABILITY_OCVC; 3998 wpabuf_put_le16(buf, capab); 3999 4000 /* PMKID Count */ 4001 wpabuf_put_le16(buf, 1); 4002 4003 /* PMKID List [PMKR1Name] */ 4004 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)", 4005 sm->fils_ft, sm->fils_ft_len); 4006 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len); 4007 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID", 4008 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN); 4009 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID", 4010 sm->r0kh_id, sm->r0kh_id_len); 4011 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid, 4012 sm->ssid_len, sm->mobility_domain, 4013 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr, 4014 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) { 4015 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0"); 4016 return -1; 4017 } 4018 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN; 4019 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0", 4020 sm->pmk_r0, sm->pmk_r0_len); 4021 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name", 4022 sm->pmk_r0_name, WPA_PMK_NAME_LEN); 4023 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR, 4024 MAC2STR(sm->r1kh_id)); 4025 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN); 4026 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr, 4027 sm->pmk_r1_name, use_sha384) < 0) { 4028 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name"); 4029 return -1; 4030 } 4031 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name, 4032 WPA_PMK_NAME_LEN); 4033 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN); 4034 4035 #ifdef CONFIG_IEEE80211W 4036 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) { 4037 /* Management Group Cipher Suite */ 4038 pos = wpabuf_put(buf, RSN_SELECTOR_LEN); 4039 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC); 4040 } 4041 #endif /* CONFIG_IEEE80211W */ 4042 4043 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2; 4044 return 0; 4045 } 4046 #endif /* CONFIG_IEEE80211R */ 4047 4048 4049 struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek, 4050 size_t *kek_len, const u8 **snonce, 4051 const u8 **anonce, 4052 const struct wpabuf **hlp, 4053 unsigned int num_hlp) 4054 { 4055 struct wpabuf *buf; 4056 size_t len; 4057 unsigned int i; 4058 4059 len = 1000; 4060 #ifdef CONFIG_IEEE80211R 4061 if (sm->fils_ft_ies) 4062 len += wpabuf_len(sm->fils_ft_ies); 4063 if (wpa_key_mgmt_ft(sm->key_mgmt)) 4064 len += 256; 4065 #endif /* CONFIG_IEEE80211R */ 4066 for (i = 0; hlp && i < num_hlp; i++) 4067 len += 10 + wpabuf_len(hlp[i]); 4068 buf = wpabuf_alloc(len); 4069 if (!buf) 4070 return NULL; 4071 4072 #ifdef CONFIG_IEEE80211R 4073 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) { 4074 /* MDE and FTE when using FILS+FT */ 4075 wpabuf_put_buf(buf, sm->fils_ft_ies); 4076 /* RSNE with PMKR1Name in PMKID field */ 4077 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) { 4078 wpabuf_free(buf); 4079 return NULL; 4080 } 4081 } 4082 #endif /* CONFIG_IEEE80211R */ 4083 4084 /* FILS Session */ 4085 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ 4086 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */ 4087 /* Element ID Extension */ 4088 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION); 4089 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN); 4090 4091 /* Everything after FILS Session element gets encrypted in the driver 4092 * with KEK. The buffer returned from here is the plaintext version. */ 4093 4094 /* TODO: FILS Public Key */ 4095 4096 /* FILS Key Confirm */ 4097 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ 4098 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */ 4099 /* Element ID Extension */ 4100 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM); 4101 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len); 4102 4103 /* FILS HLP Container */ 4104 for (i = 0; hlp && i < num_hlp; i++) { 4105 const u8 *pos = wpabuf_head(hlp[i]); 4106 size_t left = wpabuf_len(hlp[i]); 4107 4108 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ 4109 if (left <= 254) 4110 len = 1 + left; 4111 else 4112 len = 255; 4113 wpabuf_put_u8(buf, len); /* Length */ 4114 /* Element ID Extension */ 4115 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER); 4116 /* Destination MAC Address, Source MAC Address, HLP Packet. 4117 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP 4118 * header when LPD is used). */ 4119 wpabuf_put_data(buf, pos, len - 1); 4120 pos += len - 1; 4121 left -= len - 1; 4122 while (left) { 4123 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT); 4124 len = left > 255 ? 255 : left; 4125 wpabuf_put_u8(buf, len); 4126 wpabuf_put_data(buf, pos, len); 4127 pos += len; 4128 left -= len; 4129 } 4130 } 4131 4132 /* TODO: FILS IP Address Assignment */ 4133 4134 #ifdef CONFIG_OCV 4135 if (wpa_sm_ocv_enabled(sm)) { 4136 struct wpa_channel_info ci; 4137 u8 *pos; 4138 4139 if (wpa_sm_channel_info(sm, &ci) != 0) { 4140 wpa_printf(MSG_WARNING, 4141 "FILS: Failed to get channel info for OCI element"); 4142 wpabuf_free(buf); 4143 return NULL; 4144 } 4145 4146 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN); 4147 if (ocv_insert_extended_oci(&ci, pos) < 0) { 4148 wpabuf_free(buf); 4149 return NULL; 4150 } 4151 } 4152 #endif /* CONFIG_OCV */ 4153 4154 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf); 4155 4156 *kek = sm->ptk.kek; 4157 *kek_len = sm->ptk.kek_len; 4158 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len); 4159 *snonce = sm->fils_nonce; 4160 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD", 4161 *snonce, FILS_NONCE_LEN); 4162 *anonce = sm->fils_anonce; 4163 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD", 4164 *anonce, FILS_NONCE_LEN); 4165 4166 return buf; 4167 } 4168 4169 4170 static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len) 4171 { 4172 const u8 *pos, *end; 4173 4174 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len); 4175 if (len < 2 * ETH_ALEN) 4176 return; 4177 pos = resp + 2 * ETH_ALEN; 4178 end = resp + len; 4179 if (end - pos >= 6 && 4180 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) 4181 pos += 6; /* Remove SNAP/LLC header */ 4182 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos); 4183 } 4184 4185 4186 static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos, 4187 size_t len) 4188 { 4189 const u8 *end = pos + len; 4190 u8 *tmp, *tmp_pos; 4191 4192 /* Check if there are any FILS HLP Container elements */ 4193 while (end - pos >= 2) { 4194 if (2 + pos[1] > end - pos) 4195 return; 4196 if (pos[0] == WLAN_EID_EXTENSION && 4197 pos[1] >= 1 + 2 * ETH_ALEN && 4198 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER) 4199 break; 4200 pos += 2 + pos[1]; 4201 } 4202 if (end - pos < 2) 4203 return; /* No FILS HLP Container elements */ 4204 4205 tmp = os_malloc(end - pos); 4206 if (!tmp) 4207 return; 4208 4209 while (end - pos >= 2) { 4210 if (2 + pos[1] > end - pos || 4211 pos[0] != WLAN_EID_EXTENSION || 4212 pos[1] < 1 + 2 * ETH_ALEN || 4213 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER) 4214 break; 4215 tmp_pos = tmp; 4216 os_memcpy(tmp_pos, pos + 3, pos[1] - 1); 4217 tmp_pos += pos[1] - 1; 4218 pos += 2 + pos[1]; 4219 4220 /* Add possible fragments */ 4221 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT && 4222 2 + pos[1] <= end - pos) { 4223 os_memcpy(tmp_pos, pos + 2, pos[1]); 4224 tmp_pos += pos[1]; 4225 pos += 2 + pos[1]; 4226 } 4227 4228 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp); 4229 } 4230 4231 os_free(tmp); 4232 } 4233 4234 4235 int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len) 4236 { 4237 const struct ieee80211_mgmt *mgmt; 4238 const u8 *end, *ie_start; 4239 struct ieee802_11_elems elems; 4240 int keylen, rsclen; 4241 enum wpa_alg alg; 4242 struct wpa_gtk_data gd; 4243 int maxkeylen; 4244 struct wpa_eapol_ie_parse kde; 4245 4246 if (!sm || !sm->ptk_set) { 4247 wpa_printf(MSG_DEBUG, "FILS: No KEK available"); 4248 return -1; 4249 } 4250 4251 if (!wpa_key_mgmt_fils(sm->key_mgmt)) { 4252 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM"); 4253 return -1; 4254 } 4255 4256 if (sm->fils_completed) { 4257 wpa_printf(MSG_DEBUG, 4258 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission"); 4259 return -1; 4260 } 4261 4262 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame", 4263 resp, len); 4264 4265 mgmt = (const struct ieee80211_mgmt *) resp; 4266 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp)) 4267 return -1; 4268 4269 end = resp + len; 4270 /* Same offset for Association Response and Reassociation Response */ 4271 ie_start = mgmt->u.assoc_resp.variable; 4272 4273 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) == 4274 ParseFailed) { 4275 wpa_printf(MSG_DEBUG, 4276 "FILS: Failed to parse decrypted elements"); 4277 goto fail; 4278 } 4279 4280 if (!elems.fils_session) { 4281 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element"); 4282 return -1; 4283 } 4284 if (os_memcmp(elems.fils_session, sm->fils_session, 4285 FILS_SESSION_LEN) != 0) { 4286 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch"); 4287 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session", 4288 elems.fils_session, FILS_SESSION_LEN); 4289 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session", 4290 sm->fils_session, FILS_SESSION_LEN); 4291 } 4292 4293 /* TODO: FILS Public Key */ 4294 4295 if (!elems.fils_key_confirm) { 4296 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element"); 4297 goto fail; 4298 } 4299 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) { 4300 wpa_printf(MSG_DEBUG, 4301 "FILS: Unexpected Key-Auth length %d (expected %d)", 4302 elems.fils_key_confirm_len, 4303 (int) sm->fils_key_auth_len); 4304 goto fail; 4305 } 4306 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap, 4307 sm->fils_key_auth_len) != 0) { 4308 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch"); 4309 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth", 4310 elems.fils_key_confirm, 4311 elems.fils_key_confirm_len); 4312 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth", 4313 sm->fils_key_auth_ap, sm->fils_key_auth_len); 4314 goto fail; 4315 } 4316 4317 #ifdef CONFIG_OCV 4318 if (wpa_sm_ocv_enabled(sm)) { 4319 struct wpa_channel_info ci; 4320 4321 if (wpa_sm_channel_info(sm, &ci) != 0) { 4322 wpa_printf(MSG_WARNING, 4323 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame"); 4324 goto fail; 4325 } 4326 4327 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci, 4328 channel_width_to_int(ci.chanwidth), 4329 ci.seg1_idx) != 0) { 4330 wpa_printf(MSG_WARNING, "FILS: %s", ocv_errorstr); 4331 goto fail; 4332 } 4333 } 4334 #endif /* CONFIG_OCV */ 4335 4336 #ifdef CONFIG_IEEE80211R 4337 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) { 4338 struct wpa_ie_data rsn; 4339 4340 /* Check that PMKR1Name derived by the AP matches */ 4341 if (!elems.rsn_ie || 4342 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2, 4343 &rsn) < 0 || 4344 !rsn.pmkid || rsn.num_pmkid != 1 || 4345 os_memcmp(rsn.pmkid, sm->pmk_r1_name, 4346 WPA_PMK_NAME_LEN) != 0) { 4347 wpa_printf(MSG_DEBUG, 4348 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp"); 4349 goto fail; 4350 } 4351 } 4352 #endif /* CONFIG_IEEE80211R */ 4353 4354 /* Key Delivery */ 4355 if (!elems.key_delivery) { 4356 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element"); 4357 goto fail; 4358 } 4359 4360 /* Parse GTK and set the key to the driver */ 4361 os_memset(&gd, 0, sizeof(gd)); 4362 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN, 4363 elems.key_delivery_len - WPA_KEY_RSC_LEN, 4364 &kde) < 0) { 4365 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs"); 4366 goto fail; 4367 } 4368 if (!kde.gtk) { 4369 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE"); 4370 goto fail; 4371 } 4372 maxkeylen = gd.gtk_len = kde.gtk_len - 2; 4373 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, 4374 gd.gtk_len, maxkeylen, 4375 &gd.key_rsc_len, &gd.alg)) 4376 goto fail; 4377 4378 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len); 4379 gd.keyidx = kde.gtk[0] & 0x3; 4380 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm, 4381 !!(kde.gtk[0] & BIT(2))); 4382 if (kde.gtk_len - 2 > sizeof(gd.gtk)) { 4383 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)", 4384 (unsigned long) kde.gtk_len - 2); 4385 goto fail; 4386 } 4387 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2); 4388 4389 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver"); 4390 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) { 4391 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK"); 4392 goto fail; 4393 } 4394 4395 if (ieee80211w_set_keys(sm, &kde) < 0) { 4396 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK"); 4397 goto fail; 4398 } 4399 4400 alg = wpa_cipher_to_alg(sm->pairwise_cipher); 4401 keylen = wpa_cipher_key_len(sm->pairwise_cipher); 4402 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) { 4403 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu", 4404 keylen, (long unsigned int) sm->ptk.tk_len); 4405 goto fail; 4406 } 4407 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher); 4408 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver", 4409 sm->ptk.tk, keylen); 4410 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen, 4411 sm->ptk.tk, keylen) < 0) { 4412 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, 4413 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid=" 4414 MACSTR ")", 4415 alg, keylen, MAC2STR(sm->bssid)); 4416 goto fail; 4417 } 4418 4419 /* TODO: TK could be cleared after auth frame exchange now that driver 4420 * takes care of association frame encryption/decryption. */ 4421 /* TK is not needed anymore in supplicant */ 4422 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN); 4423 sm->ptk.tk_len = 0; 4424 sm->ptk.installed = 1; 4425 4426 /* FILS HLP Container */ 4427 fils_process_hlp_container(sm, ie_start, end - ie_start); 4428 4429 /* TODO: FILS IP Address Assignment */ 4430 4431 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully"); 4432 sm->fils_completed = 1; 4433 4434 return 0; 4435 fail: 4436 return -1; 4437 } 4438 4439 4440 void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set) 4441 { 4442 if (sm) 4443 sm->fils_completed = !!set; 4444 } 4445 4446 #endif /* CONFIG_FILS */ 4447 4448 4449 int wpa_fils_is_completed(struct wpa_sm *sm) 4450 { 4451 #ifdef CONFIG_FILS 4452 return sm && sm->fils_completed; 4453 #else /* CONFIG_FILS */ 4454 return 0; 4455 #endif /* CONFIG_FILS */ 4456 } 4457 4458 4459 #ifdef CONFIG_OWE 4460 4461 struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group) 4462 { 4463 struct wpabuf *ie = NULL, *pub = NULL; 4464 size_t prime_len; 4465 4466 if (group == 19) 4467 prime_len = 32; 4468 else if (group == 20) 4469 prime_len = 48; 4470 else if (group == 21) 4471 prime_len = 66; 4472 else 4473 return NULL; 4474 4475 crypto_ecdh_deinit(sm->owe_ecdh); 4476 sm->owe_ecdh = crypto_ecdh_init(group); 4477 if (!sm->owe_ecdh) 4478 goto fail; 4479 sm->owe_group = group; 4480 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0); 4481 pub = wpabuf_zeropad(pub, prime_len); 4482 if (!pub) 4483 goto fail; 4484 4485 ie = wpabuf_alloc(5 + wpabuf_len(pub)); 4486 if (!ie) 4487 goto fail; 4488 wpabuf_put_u8(ie, WLAN_EID_EXTENSION); 4489 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub)); 4490 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM); 4491 wpabuf_put_le16(ie, group); 4492 wpabuf_put_buf(ie, pub); 4493 wpabuf_free(pub); 4494 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element", 4495 ie); 4496 4497 return ie; 4498 fail: 4499 wpabuf_free(pub); 4500 crypto_ecdh_deinit(sm->owe_ecdh); 4501 sm->owe_ecdh = NULL; 4502 return NULL; 4503 } 4504 4505 4506 int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid, 4507 const u8 *resp_ies, size_t resp_ies_len) 4508 { 4509 struct ieee802_11_elems elems; 4510 u16 group; 4511 struct wpabuf *secret, *pub, *hkey; 4512 int res; 4513 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN]; 4514 const char *info = "OWE Key Generation"; 4515 const u8 *addr[2]; 4516 size_t len[2]; 4517 size_t hash_len, prime_len; 4518 struct wpa_ie_data data; 4519 4520 if (!resp_ies || 4521 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) == 4522 ParseFailed) { 4523 wpa_printf(MSG_INFO, 4524 "OWE: Could not parse Association Response frame elements"); 4525 return -1; 4526 } 4527 4528 if (sm->cur_pmksa && elems.rsn_ie && 4529 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len, 4530 &data) == 0 && 4531 data.num_pmkid == 1 && data.pmkid && 4532 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) { 4533 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching"); 4534 wpa_sm_set_pmk_from_pmksa(sm); 4535 return 0; 4536 } 4537 4538 if (!elems.owe_dh) { 4539 wpa_printf(MSG_INFO, 4540 "OWE: No Diffie-Hellman Parameter element found in Association Response frame"); 4541 return -1; 4542 } 4543 4544 group = WPA_GET_LE16(elems.owe_dh); 4545 if (group != sm->owe_group) { 4546 wpa_printf(MSG_INFO, 4547 "OWE: Unexpected Diffie-Hellman group in response: %u", 4548 group); 4549 return -1; 4550 } 4551 4552 if (!sm->owe_ecdh) { 4553 wpa_printf(MSG_INFO, "OWE: No ECDH state available"); 4554 return -1; 4555 } 4556 4557 if (group == 19) 4558 prime_len = 32; 4559 else if (group == 20) 4560 prime_len = 48; 4561 else if (group == 21) 4562 prime_len = 66; 4563 else 4564 return -1; 4565 4566 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0, 4567 elems.owe_dh + 2, 4568 elems.owe_dh_len - 2); 4569 secret = wpabuf_zeropad(secret, prime_len); 4570 if (!secret) { 4571 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key"); 4572 return -1; 4573 } 4574 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret); 4575 4576 /* prk = HKDF-extract(C | A | group, z) */ 4577 4578 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0); 4579 if (!pub) { 4580 wpabuf_clear_free(secret); 4581 return -1; 4582 } 4583 4584 /* PMKID = Truncate-128(Hash(C | A)) */ 4585 addr[0] = wpabuf_head(pub); 4586 len[0] = wpabuf_len(pub); 4587 addr[1] = elems.owe_dh + 2; 4588 len[1] = elems.owe_dh_len - 2; 4589 if (group == 19) { 4590 res = sha256_vector(2, addr, len, pmkid); 4591 hash_len = SHA256_MAC_LEN; 4592 } else if (group == 20) { 4593 res = sha384_vector(2, addr, len, pmkid); 4594 hash_len = SHA384_MAC_LEN; 4595 } else if (group == 21) { 4596 res = sha512_vector(2, addr, len, pmkid); 4597 hash_len = SHA512_MAC_LEN; 4598 } else { 4599 res = -1; 4600 hash_len = 0; 4601 } 4602 pub = wpabuf_zeropad(pub, prime_len); 4603 if (res < 0 || !pub) { 4604 wpabuf_free(pub); 4605 wpabuf_clear_free(secret); 4606 return -1; 4607 } 4608 4609 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2); 4610 if (!hkey) { 4611 wpabuf_free(pub); 4612 wpabuf_clear_free(secret); 4613 return -1; 4614 } 4615 4616 wpabuf_put_buf(hkey, pub); /* C */ 4617 wpabuf_free(pub); 4618 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */ 4619 wpabuf_put_le16(hkey, sm->owe_group); /* group */ 4620 if (group == 19) 4621 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey), 4622 wpabuf_head(secret), wpabuf_len(secret), prk); 4623 else if (group == 20) 4624 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey), 4625 wpabuf_head(secret), wpabuf_len(secret), prk); 4626 else if (group == 21) 4627 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey), 4628 wpabuf_head(secret), wpabuf_len(secret), prk); 4629 wpabuf_clear_free(hkey); 4630 wpabuf_clear_free(secret); 4631 if (res < 0) 4632 return -1; 4633 4634 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len); 4635 4636 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */ 4637 4638 if (group == 19) 4639 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info, 4640 os_strlen(info), sm->pmk, hash_len); 4641 else if (group == 20) 4642 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info, 4643 os_strlen(info), sm->pmk, hash_len); 4644 else if (group == 21) 4645 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info, 4646 os_strlen(info), sm->pmk, hash_len); 4647 os_memset(prk, 0, SHA512_MAC_LEN); 4648 if (res < 0) { 4649 sm->pmk_len = 0; 4650 return -1; 4651 } 4652 sm->pmk_len = hash_len; 4653 4654 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len); 4655 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN); 4656 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0, 4657 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt, 4658 NULL); 4659 4660 return 0; 4661 } 4662 4663 #endif /* CONFIG_OWE */ 4664 4665 4666 void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id) 4667 { 4668 #ifdef CONFIG_FILS 4669 if (sm && fils_cache_id) { 4670 sm->fils_cache_id_set = 1; 4671 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN); 4672 } 4673 #endif /* CONFIG_FILS */ 4674 } 4675 4676 4677 #ifdef CONFIG_DPP2 4678 void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z) 4679 { 4680 if (sm) { 4681 wpabuf_clear_free(sm->dpp_z); 4682 sm->dpp_z = z ? wpabuf_dup(z) : NULL; 4683 } 4684 } 4685 #endif /* CONFIG_DPP2 */ 4686