2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2018, Jouni Malinen <j@w1.fi>
4 * Copyright(c) 2015 Intel Deutschland GmbH
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
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"
30 #include "pmksa_cache.h"
35 static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
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
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)
55 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
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)) {
62 * Association event was not yet received; try to fetch
63 * BSSID from the driver.
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");
71 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
72 "WPA: Use BSSID (" MACSTR
73 ") as the destination for EAPOL-Key",
79 if (key_mic && (!ptk || !ptk->kck_len))
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",
91 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
92 ptk->kck, ptk->kck_len);
93 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
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;
107 if (!ptk || !ptk->kek_len)
110 key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
111 sizeof(struct wpa_eapol_key) - 2;
113 buf = os_malloc(buf_len);
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;
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);
127 /* Update Key Data Length field to include AES-SIV overhead */
128 WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
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;
134 wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
135 s_key_data, key_data_len);
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). */
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) {
149 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
150 key_data, AES_BLOCK_SIZE + key_data_len);
155 #else /* CONFIG_FILS */
157 #endif /* CONFIG_FILS */
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);
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
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.
179 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
181 size_t mic_len, hdrlen, rlen;
182 struct wpa_eapol_key *reply;
184 u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
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;
194 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
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");
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);
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;
214 key_info |= WPA_KEY_INFO_SECURE;
215 if (sm->ptk_set && mic_len)
216 key_info |= WPA_KEY_INFO_MIC;
218 key_info |= WPA_KEY_INFO_ERROR;
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);
227 mic = (u8 *) (reply + 1);
228 WPA_PUT_BE16(mic + mic_len, 0);
229 if (!(key_info & WPA_KEY_INFO_MIC))
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,
243 static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
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");
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
257 #endif /* CONFIG_IEEE80211R */
261 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
262 const unsigned char *src_addr,
265 int abort_cached = 0;
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,
275 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
276 "RSN: found matching PMKID from PMKSA cache");
278 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
279 "RSN: no matching PMKID found");
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
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;
304 #endif /* CONFIG_SAE */
305 #endif /* CONFIG_IEEE80211R */
306 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
308 #ifdef CONFIG_IEEE80211R
310 #endif /* CONFIG_IEEE80211R */
312 if (wpa_key_mgmt_sha384(sm->key_mgmt))
313 pmk_len = PMK_LEN_SUITE_B_192;
316 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
318 if (pmk_len == PMK_LEN) {
320 * EAP-LEAP is an exception from other EAP
321 * methods: it uses only 16-byte PMK.
323 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
327 #ifdef CONFIG_IEEE80211R
329 eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) {
330 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
331 os_memcpy(sm->xxkey, buf, SHA384_MAC_LEN);
332 sm->xxkey_len = SHA384_MAC_LEN;
334 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
335 sm->xxkey_len = PMK_LEN;
337 forced_memzero(buf, sizeof(buf));
338 if (sm->proto == WPA_PROTO_RSN &&
339 wpa_key_mgmt_ft(sm->key_mgmt)) {
340 struct rsn_pmksa_cache_entry *sa = NULL;
341 const u8 *fils_cache_id = NULL;
344 if (sm->fils_cache_id_set)
345 fils_cache_id = sm->fils_cache_id;
346 #endif /* CONFIG_FILS */
347 wpa_hexdump_key(MSG_DEBUG,
348 "FT: Cache XXKey/MPMK",
349 sm->xxkey, sm->xxkey_len);
350 sa = pmksa_cache_add(sm->pmksa,
351 sm->xxkey, sm->xxkey_len,
353 src_addr, sm->own_addr,
361 #endif /* CONFIG_IEEE80211R */
363 struct rsn_pmksa_cache_entry *sa = NULL;
364 const u8 *fils_cache_id = NULL;
367 if (sm->fils_cache_id_set)
368 fils_cache_id = sm->fils_cache_id;
369 #endif /* CONFIG_FILS */
371 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
372 "machines", sm->pmk, pmk_len);
373 sm->pmk_len = pmk_len;
374 wpa_supplicant_key_mgmt_set_pmk(sm);
375 if (sm->proto == WPA_PROTO_RSN &&
376 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
377 !wpa_key_mgmt_ft(sm->key_mgmt)) {
378 sa = pmksa_cache_add(sm->pmksa,
379 sm->pmk, pmk_len, NULL,
381 src_addr, sm->own_addr,
386 if (!sm->cur_pmksa && pmkid &&
387 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL,
389 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
390 "RSN: the new PMK matches with the "
393 } else if (sa && !sm->cur_pmksa && pmkid) {
395 * It looks like the authentication server
396 * derived mismatching MSK. This should not
397 * really happen, but bugs happen.. There is not
398 * much we can do here without knowing what
399 * exactly caused the server to misbehave.
401 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
402 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
408 #ifdef CONFIG_IEEE80211R
409 } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) {
410 wpa_printf(MSG_DEBUG,
411 "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
412 #endif /* CONFIG_IEEE80211R */
414 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
415 "WPA: Failed to get master session key from "
416 "EAPOL state machines - key handshake "
419 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
420 "RSN: Cancelled PMKSA caching "
422 sm->cur_pmksa = NULL;
424 } else if (!abort_cached) {
430 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
431 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
432 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
434 /* Send EAPOL-Start to trigger full EAP authentication. */
438 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
439 "RSN: no PMKSA entry found - trigger "
440 "full EAP authentication");
441 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
442 NULL, 0, &buflen, NULL);
444 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
458 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
459 * @sm: Pointer to WPA state machine data from wpa_sm_init()
460 * @dst: Destination address for the frame
461 * @key: Pointer to the EAPOL-Key frame header
462 * @ver: Version bits from EAPOL-Key Key Info
463 * @nonce: Nonce value for the EAPOL-Key frame
464 * @wpa_ie: WPA/RSN IE
465 * @wpa_ie_len: Length of the WPA/RSN IE
466 * @ptk: PTK to use for keyed hash and encryption
467 * Returns: >= 0 on success, < 0 on failure
469 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
470 const struct wpa_eapol_key *key,
471 int ver, const u8 *nonce,
472 const u8 *wpa_ie, size_t wpa_ie_len,
475 size_t mic_len, hdrlen, rlen;
476 struct wpa_eapol_key *reply;
478 u8 *rsn_ie_buf = NULL;
481 if (wpa_ie == NULL) {
482 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
483 "cannot generate msg 2/4");
487 #ifdef CONFIG_IEEE80211R
488 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
492 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
493 * FTIE from (Re)Association Response.
495 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
496 sm->assoc_resp_ies_len);
497 if (rsn_ie_buf == NULL)
499 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
500 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
507 if (sm->assoc_resp_ies) {
508 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
509 sm->assoc_resp_ies_len);
510 wpa_ie_len += sm->assoc_resp_ies_len;
515 #endif /* CONFIG_IEEE80211R */
517 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
519 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
520 hdrlen = sizeof(*reply) + mic_len + 2;
521 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
522 NULL, hdrlen + wpa_ie_len,
523 &rlen, (void *) &reply);
529 reply->type = (sm->proto == WPA_PROTO_RSN ||
530 sm->proto == WPA_PROTO_OSEN) ?
531 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
532 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
534 key_info |= WPA_KEY_INFO_MIC;
536 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
537 WPA_PUT_BE16(reply->key_info, key_info);
538 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
539 WPA_PUT_BE16(reply->key_length, 0);
541 os_memcpy(reply->key_length, key->key_length, 2);
542 os_memcpy(reply->replay_counter, key->replay_counter,
543 WPA_REPLAY_COUNTER_LEN);
544 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
545 WPA_REPLAY_COUNTER_LEN);
547 key_mic = (u8 *) (reply + 1);
548 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
549 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
552 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
554 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
555 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
560 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
561 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
566 #ifdef CONFIG_IEEE80211R
567 if (wpa_key_mgmt_ft(sm->key_mgmt))
568 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
569 #endif /* CONFIG_IEEE80211R */
572 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
573 z = wpabuf_head(sm->dpp_z);
574 z_len = wpabuf_len(sm->dpp_z);
576 #endif /* CONFIG_DPP2 */
578 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
579 sm->own_addr, sm->bssid, sm->snonce,
580 key->key_nonce, ptk, sm->key_mgmt,
581 sm->pairwise_cipher, z, z_len);
585 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
586 const unsigned char *src_addr,
587 const struct wpa_eapol_key *key,
588 u16 ver, const u8 *key_data,
591 struct wpa_eapol_ie_parse ie;
594 u8 *kde, *kde_buf = NULL;
597 if (wpa_sm_get_network_ctx(sm) == NULL) {
598 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
599 "found (msg 1 of 4)");
603 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
604 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
605 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
607 os_memset(&ie, 0, sizeof(ie));
609 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
610 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
611 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
612 key_data, key_data_len);
613 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
616 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
617 "Authenticator", ie.pmkid, PMKID_LEN);
621 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
623 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
624 "msg 1/4 - requesting full EAP authentication");
630 if (sm->renew_snonce) {
631 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
632 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
633 "WPA: Failed to get random data for SNonce");
636 sm->renew_snonce = 0;
637 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
638 sm->snonce, WPA_NONCE_LEN);
641 /* Calculate PTK which will be stored as a temporary PTK until it has
642 * been verified when processing message 3/4. */
644 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
646 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
648 /* Supplicant: swap tx/rx Mic keys */
649 os_memcpy(buf, &ptk->tk[16], 8);
650 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
651 os_memcpy(&ptk->tk[24], buf, 8);
652 forced_memzero(buf, sizeof(buf));
656 kde = sm->assoc_wpa_ie;
657 kde_len = sm->assoc_wpa_ie_len;
660 if (wpa_sm_ocv_enabled(sm)) {
661 struct wpa_channel_info ci;
664 if (wpa_sm_channel_info(sm, &ci) != 0) {
665 wpa_printf(MSG_WARNING,
666 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
670 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 3);
672 wpa_printf(MSG_WARNING,
673 "Failed to allocate memory for KDE with OCI in EAPOL-Key 2/4");
677 os_memcpy(kde_buf, kde, kde_len);
680 if (ocv_insert_oci_kde(&ci, &pos) < 0)
684 #endif /* CONFIG_OCV */
688 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
691 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
692 "into EAPOL-Key 2/4");
693 os_memcpy(kde_buf, kde, kde_len);
696 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
697 *pos++ = RSN_SELECTOR_LEN + 1;
698 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
699 pos += RSN_SELECTOR_LEN;
704 #endif /* CONFIG_P2P */
706 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
707 kde, kde_len, ptk) < 0)
711 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
716 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
720 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
722 struct wpa_sm *sm = eloop_ctx;
723 rsn_preauth_candidate_process(sm);
727 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
728 const u8 *addr, int secure)
730 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
731 "WPA: Key negotiation completed with "
732 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
733 wpa_cipher_txt(sm->pairwise_cipher),
734 wpa_cipher_txt(sm->group_cipher));
735 wpa_sm_cancel_auth_timeout(sm);
736 wpa_sm_set_state(sm, WPA_COMPLETED);
739 wpa_sm_mlme_setprotection(
740 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
741 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
742 eapol_sm_notify_portValid(sm->eapol, TRUE);
743 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
744 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
745 sm->key_mgmt == WPA_KEY_MGMT_OWE)
746 eapol_sm_notify_eap_success(sm->eapol, TRUE);
748 * Start preauthentication after a short wait to avoid a
749 * possible race condition between the data receive and key
750 * configuration after the 4-Way Handshake. This increases the
751 * likelihood of the first preauth EAPOL-Start frame getting to
754 if (!dl_list_empty(&sm->pmksa_candidates))
755 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
759 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
760 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
761 "RSN: Authenticator accepted "
762 "opportunistic PMKSA entry - marking it valid");
763 sm->cur_pmksa->opportunistic = 0;
766 #ifdef CONFIG_IEEE80211R
767 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
768 /* Prepare for the next transition */
769 wpa_ft_prepare_auth_request(sm, NULL);
771 #endif /* CONFIG_IEEE80211R */
775 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
777 struct wpa_sm *sm = eloop_ctx;
778 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
779 wpa_sm_key_request(sm, 0, 1);
783 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
784 const struct wpa_eapol_key *key)
790 if (sm->ptk.installed) {
791 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
792 "WPA: Do not re-install same PTK to the driver");
796 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
797 "WPA: Installing PTK to the driver");
799 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
800 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
801 "Suite: NONE - do not use pairwise keys");
805 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
806 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
807 "WPA: Unsupported pairwise cipher %d",
808 sm->pairwise_cipher);
812 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
813 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
814 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
815 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
816 keylen, (long unsigned int) sm->ptk.tk_len);
819 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
821 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
824 key_rsc = key->key_rsc;
825 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
828 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
829 sm->ptk.tk, keylen) < 0) {
830 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
831 "WPA: Failed to set PTK to the "
832 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
833 alg, keylen, MAC2STR(sm->bssid));
837 /* TK is not needed anymore in supplicant */
838 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
840 sm->ptk.installed = 1;
842 if (sm->wpa_ptk_rekey) {
843 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
844 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
852 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
854 int keylen, int maxkeylen,
860 *alg = wpa_cipher_to_alg(group_cipher);
861 if (*alg == WPA_ALG_NONE) {
862 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
863 "WPA: Unsupported Group Cipher %d",
867 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
869 klen = wpa_cipher_key_len(group_cipher);
870 if (keylen != klen || maxkeylen < klen) {
871 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
872 "WPA: Unsupported %s Group Cipher key length %d (%d)",
873 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
880 struct wpa_gtk_data {
882 int tx, key_rsc_len, keyidx;
888 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
889 const struct wpa_gtk_data *gd,
890 const u8 *key_rsc, int wnm_sleep)
892 const u8 *_gtk = gd->gtk;
895 /* Detect possible key reinstallation */
896 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
897 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
898 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
899 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
900 sm->gtk_wnm_sleep.gtk_len) == 0)) {
901 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
902 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
903 gd->keyidx, gd->tx, gd->gtk_len);
907 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
908 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
909 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
910 gd->keyidx, gd->tx, gd->gtk_len);
911 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
912 if (sm->group_cipher == WPA_CIPHER_TKIP) {
913 /* Swap Tx/Rx keys for Michael MIC */
914 os_memcpy(gtk_buf, gd->gtk, 16);
915 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
916 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
919 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
920 if (wpa_sm_set_key(sm, gd->alg, NULL,
921 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
922 _gtk, gd->gtk_len) < 0) {
923 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
924 "WPA: Failed to set GTK to the driver "
926 forced_memzero(gtk_buf, sizeof(gtk_buf));
929 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
930 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
931 _gtk, gd->gtk_len) < 0) {
932 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
933 "WPA: Failed to set GTK to "
934 "the driver (alg=%d keylen=%d keyidx=%d)",
935 gd->alg, gd->gtk_len, gd->keyidx);
936 forced_memzero(gtk_buf, sizeof(gtk_buf));
939 forced_memzero(gtk_buf, sizeof(gtk_buf));
942 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
943 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
944 sm->gtk_wnm_sleep.gtk_len);
946 sm->gtk.gtk_len = gd->gtk_len;
947 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
954 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
957 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
958 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
959 * seemed to set this bit (incorrectly, since Tx is only when
960 * doing Group Key only APs) and without this workaround, the
961 * data connection does not work because wpa_supplicant
962 * configured non-zero keyidx to be used for unicast. */
963 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
964 "WPA: Tx bit set for GTK, but pairwise "
965 "keys are used - ignore Tx bit");
972 static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
977 if (!sm->wpa_rsc_relaxation)
980 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
983 * Try to detect RSC (endian) corruption issue where the AP sends
984 * the RSC bytes in EAPOL-Key message in the wrong order, both if
985 * it's actually a 6-byte field (as it should be) and if it treats
986 * it as an 8-byte field.
987 * An AP model known to have this bug is the Sapido RB-1632.
989 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
990 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
991 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
992 rsc[0], rsc[1], rsc[2], rsc[3],
993 rsc[4], rsc[5], rsc[6], rsc[7]);
1002 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1003 const struct wpa_eapol_key *key,
1004 const u8 *gtk, size_t gtk_len,
1007 struct wpa_gtk_data gd;
1011 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1013 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1014 * Reserved [bits 0-7]
1018 os_memset(&gd, 0, sizeof(gd));
1019 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1022 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1025 gd.keyidx = gtk[0] & 0x3;
1026 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1027 !!(gtk[0] & BIT(2)));
1031 os_memcpy(gd.gtk, gtk, gtk_len);
1032 gd.gtk_len = gtk_len;
1034 key_rsc = key->key_rsc;
1035 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1038 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1039 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1041 &gd.key_rsc_len, &gd.alg) ||
1042 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
1043 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1044 "RSN: Failed to install GTK");
1045 forced_memzero(&gd, sizeof(gd));
1048 forced_memzero(&gd, sizeof(gd));
1054 #ifdef CONFIG_IEEE80211W
1055 static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
1056 const struct wpa_igtk_kde *igtk,
1059 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1060 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1062 /* Detect possible key reinstallation */
1063 if ((sm->igtk.igtk_len == len &&
1064 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1065 (sm->igtk_wnm_sleep.igtk_len == len &&
1066 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1067 sm->igtk_wnm_sleep.igtk_len) == 0)) {
1068 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1069 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1074 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1075 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
1076 keyidx, MAC2STR(igtk->pn));
1077 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1078 if (keyidx > 4095) {
1079 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1080 "WPA: Invalid IGTK KeyID %d", keyidx);
1083 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1084 broadcast_ether_addr,
1085 keyidx, 0, igtk->pn, sizeof(igtk->pn),
1086 igtk->igtk, len) < 0) {
1087 if (keyidx == 0x0400 || keyidx == 0x0500) {
1088 /* Assume the AP has broken PMF implementation since it
1089 * seems to have swapped the KeyID bytes. The AP cannot
1090 * be trusted to implement BIP correctly or provide a
1091 * valid IGTK, so do not try to configure this key with
1092 * swapped KeyID bytes. Instead, continue without
1093 * configuring the IGTK so that the driver can drop any
1094 * received group-addressed robust management frames due
1097 * Normally, this error behavior would result in us
1098 * disconnecting, but there are number of deployed APs
1099 * with this broken behavior, so as an interoperability
1100 * workaround, allow the connection to proceed. */
1101 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1102 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1104 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1105 "WPA: Failed to configure IGTK to the driver");
1111 sm->igtk_wnm_sleep.igtk_len = len;
1112 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1113 sm->igtk_wnm_sleep.igtk_len);
1115 sm->igtk.igtk_len = len;
1116 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1121 #endif /* CONFIG_IEEE80211W */
1124 static int ieee80211w_set_keys(struct wpa_sm *sm,
1125 struct wpa_eapol_ie_parse *ie)
1127 #ifdef CONFIG_IEEE80211W
1128 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
1133 const struct wpa_igtk_kde *igtk;
1135 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1136 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
1139 igtk = (const struct wpa_igtk_kde *) ie->igtk;
1140 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
1145 #else /* CONFIG_IEEE80211W */
1147 #endif /* CONFIG_IEEE80211W */
1151 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1152 const char *reason, const u8 *src_addr,
1153 const u8 *wpa_ie, size_t wpa_ie_len,
1154 const u8 *rsn_ie, size_t rsn_ie_len)
1156 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1157 reason, MAC2STR(src_addr));
1159 if (sm->ap_wpa_ie) {
1160 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1161 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1164 if (!sm->ap_wpa_ie) {
1165 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1166 "WPA: No WPA IE in Beacon/ProbeResp");
1168 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1169 wpa_ie, wpa_ie_len);
1172 if (sm->ap_rsn_ie) {
1173 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1174 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1177 if (!sm->ap_rsn_ie) {
1178 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1179 "WPA: No RSN IE in Beacon/ProbeResp");
1181 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1182 rsn_ie, rsn_ie_len);
1185 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
1189 #ifdef CONFIG_IEEE80211R
1191 static int ft_validate_mdie(struct wpa_sm *sm,
1192 const unsigned char *src_addr,
1193 struct wpa_eapol_ie_parse *ie,
1194 const u8 *assoc_resp_mdie)
1196 struct rsn_mdie *mdie;
1198 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1199 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1200 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1201 MOBILITY_DOMAIN_ID_LEN) != 0) {
1202 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1203 "not match with the current mobility domain");
1207 if (assoc_resp_mdie &&
1208 (assoc_resp_mdie[1] != ie->mdie[1] ||
1209 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1210 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1211 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1212 ie->mdie, 2 + ie->mdie[1]);
1213 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1214 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1222 static int ft_validate_ftie(struct wpa_sm *sm,
1223 const unsigned char *src_addr,
1224 struct wpa_eapol_ie_parse *ie,
1225 const u8 *assoc_resp_ftie)
1227 if (ie->ftie == NULL) {
1228 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1229 "FT: No FTIE in EAPOL-Key msg 3/4");
1233 if (assoc_resp_ftie == NULL)
1236 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1237 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1238 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1239 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1240 ie->ftie, 2 + ie->ftie[1]);
1241 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1242 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1250 static int ft_validate_rsnie(struct wpa_sm *sm,
1251 const unsigned char *src_addr,
1252 struct wpa_eapol_ie_parse *ie)
1254 struct wpa_ie_data rsn;
1260 * Verify that PMKR1Name from EAPOL-Key message 3/4
1261 * matches with the value we derived.
1263 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1264 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1265 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1266 "FT 4-way handshake message 3/4");
1270 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1272 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1273 "FT: PMKR1Name mismatch in "
1274 "FT 4-way handshake message 3/4");
1275 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1276 rsn.pmkid, WPA_PMK_NAME_LEN);
1277 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1278 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1286 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1287 const unsigned char *src_addr,
1288 struct wpa_eapol_ie_parse *ie)
1290 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1292 if (sm->assoc_resp_ies) {
1293 pos = sm->assoc_resp_ies;
1294 end = pos + sm->assoc_resp_ies_len;
1295 while (end - pos > 2) {
1296 if (2 + pos[1] > end - pos)
1299 case WLAN_EID_MOBILITY_DOMAIN:
1302 case WLAN_EID_FAST_BSS_TRANSITION:
1310 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1311 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1312 ft_validate_rsnie(sm, src_addr, ie) < 0)
1318 #endif /* CONFIG_IEEE80211R */
1321 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1322 const unsigned char *src_addr,
1323 struct wpa_eapol_ie_parse *ie)
1325 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1326 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1327 "WPA: No WPA/RSN IE for this AP known. "
1328 "Trying to get from scan results");
1329 if (wpa_sm_get_beacon_ie(sm) < 0) {
1330 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1331 "WPA: Could not find AP from "
1332 "the scan results");
1334 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1335 "WPA: Found the current AP from "
1336 "updated scan results");
1340 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1341 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1342 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1343 "with IE in Beacon/ProbeResp (no IE?)",
1344 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1345 ie->rsn_ie, ie->rsn_ie_len);
1349 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1350 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1351 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1352 (ie->rsn_ie && sm->ap_rsn_ie &&
1353 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1354 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1355 ie->rsn_ie, ie->rsn_ie_len))) {
1356 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1357 "with IE in Beacon/ProbeResp",
1358 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1359 ie->rsn_ie, ie->rsn_ie_len);
1363 if (sm->proto == WPA_PROTO_WPA &&
1364 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1365 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1366 "detected - RSN was enabled and RSN IE "
1367 "was in msg 3/4, but not in "
1369 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1370 ie->rsn_ie, ie->rsn_ie_len);
1374 #ifdef CONFIG_IEEE80211R
1375 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1376 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1378 #endif /* CONFIG_IEEE80211R */
1385 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1386 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1387 * @dst: Destination address for the frame
1388 * @key: Pointer to the EAPOL-Key frame header
1389 * @ver: Version bits from EAPOL-Key Key Info
1390 * @key_info: Key Info
1391 * @ptk: PTK to use for keyed hash and encryption
1392 * Returns: >= 0 on success, < 0 on failure
1394 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1395 const struct wpa_eapol_key *key,
1396 u16 ver, u16 key_info,
1397 struct wpa_ptk *ptk)
1399 size_t mic_len, hdrlen, rlen;
1400 struct wpa_eapol_key *reply;
1403 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
1404 hdrlen = sizeof(*reply) + mic_len + 2;
1405 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1406 hdrlen, &rlen, (void *) &reply);
1410 reply->type = (sm->proto == WPA_PROTO_RSN ||
1411 sm->proto == WPA_PROTO_OSEN) ?
1412 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1413 key_info &= WPA_KEY_INFO_SECURE;
1414 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1416 key_info |= WPA_KEY_INFO_MIC;
1418 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1419 WPA_PUT_BE16(reply->key_info, key_info);
1420 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1421 WPA_PUT_BE16(reply->key_length, 0);
1423 os_memcpy(reply->key_length, key->key_length, 2);
1424 os_memcpy(reply->replay_counter, key->replay_counter,
1425 WPA_REPLAY_COUNTER_LEN);
1427 key_mic = (u8 *) (reply + 1);
1428 WPA_PUT_BE16(key_mic + mic_len, 0);
1430 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1431 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1436 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1437 const struct wpa_eapol_key *key,
1438 u16 ver, const u8 *key_data,
1439 size_t key_data_len)
1441 u16 key_info, keylen;
1442 struct wpa_eapol_ie_parse ie;
1444 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1445 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1446 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1448 key_info = WPA_GET_BE16(key->key_info);
1450 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1451 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
1453 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1454 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1455 "WPA: GTK IE in unencrypted key data");
1458 #ifdef CONFIG_IEEE80211W
1459 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1460 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1461 "WPA: IGTK KDE in unencrypted key data");
1466 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1467 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1468 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
1469 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1470 "WPA: Invalid IGTK KDE length %lu",
1471 (unsigned long) ie.igtk_len);
1474 #endif /* CONFIG_IEEE80211W */
1476 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1479 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1480 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1481 "WPA: ANonce from message 1 of 4-Way Handshake "
1482 "differs from 3 of 4-Way Handshake - drop packet (src="
1483 MACSTR ")", MAC2STR(sm->bssid));
1487 keylen = WPA_GET_BE16(key->key_length);
1488 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1489 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1490 "WPA: Invalid %s key length %d (src=" MACSTR
1491 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1492 MAC2STR(sm->bssid));
1497 if (ie.ip_addr_alloc) {
1498 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1499 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1500 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1502 #endif /* CONFIG_P2P */
1505 if (wpa_sm_ocv_enabled(sm)) {
1506 struct wpa_channel_info ci;
1508 if (wpa_sm_channel_info(sm, &ci) != 0) {
1509 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1510 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1514 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1515 channel_width_to_int(ci.chanwidth),
1516 ci.seg1_idx) != 0) {
1517 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1522 #endif /* CONFIG_OCV */
1524 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1529 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1530 * for the next 4-Way Handshake. If msg 3 is received again, the old
1531 * SNonce will still be used to avoid changing PTK. */
1532 sm->renew_snonce = 1;
1534 if (key_info & WPA_KEY_INFO_INSTALL) {
1535 if (wpa_supplicant_install_ptk(sm, key))
1539 if (key_info & WPA_KEY_INFO_SECURE) {
1540 wpa_sm_mlme_setprotection(
1541 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1542 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1543 eapol_sm_notify_portValid(sm->eapol, TRUE);
1545 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1547 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1548 /* No GTK to be set to the driver */
1549 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
1550 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1551 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
1553 } else if (ie.gtk &&
1554 wpa_supplicant_pairwise_gtk(sm, key,
1555 ie.gtk, ie.gtk_len, key_info) < 0) {
1556 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1557 "RSN: Failed to configure GTK");
1561 if (ieee80211w_set_keys(sm, &ie) < 0) {
1562 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1563 "RSN: Failed to configure IGTK");
1567 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
1568 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1569 key_info & WPA_KEY_INFO_SECURE);
1572 wpa_sm_set_rekey_offload(sm);
1574 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1575 * calculated only after KCK has been derived. Though, do not replace an
1576 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1577 * to avoid unnecessary changes of PMKID while continuing to use the
1579 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1581 struct rsn_pmksa_cache_entry *sa;
1583 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
1584 sm->ptk.kck, sm->ptk.kck_len,
1585 sm->bssid, sm->own_addr,
1586 sm->network_ctx, sm->key_mgmt, NULL);
1591 sm->msg_3_of_4_ok = 1;
1595 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1599 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1603 struct wpa_gtk_data *gd)
1606 struct wpa_eapol_ie_parse ie;
1608 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1609 keydata, keydatalen);
1610 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1612 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1613 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1614 "WPA: GTK IE in unencrypted key data");
1617 if (ie.gtk == NULL) {
1618 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1619 "WPA: No GTK IE in Group Key msg 1/2");
1622 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1625 if (wpa_sm_ocv_enabled(sm)) {
1626 struct wpa_channel_info ci;
1628 if (wpa_sm_channel_info(sm, &ci) != 0) {
1629 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1630 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1634 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1635 channel_width_to_int(ci.chanwidth),
1636 ci.seg1_idx) != 0) {
1637 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1642 #endif /* CONFIG_OCV */
1644 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1645 gd->gtk_len, maxkeylen,
1646 &gd->key_rsc_len, &gd->alg))
1649 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1650 ie.gtk, ie.gtk_len);
1651 gd->keyidx = ie.gtk[0] & 0x3;
1652 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1653 !!(ie.gtk[0] & BIT(2)));
1654 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1655 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1656 "RSN: Too long GTK in GTK IE (len=%lu)",
1657 (unsigned long) ie.gtk_len - 2);
1660 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1662 if (ieee80211w_set_keys(sm, &ie) < 0)
1663 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1664 "RSN: Failed to configure IGTK");
1670 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1671 const struct wpa_eapol_key *key,
1673 size_t key_data_len, u16 key_info,
1674 u16 ver, struct wpa_gtk_data *gd)
1679 gtk_len = WPA_GET_BE16(key->key_length);
1680 maxkeylen = key_data_len;
1681 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1682 if (maxkeylen < 8) {
1683 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1684 "WPA: Too short maxkeylen (%lu)",
1685 (unsigned long) maxkeylen);
1691 if (gtk_len > maxkeylen ||
1692 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1694 &gd->key_rsc_len, &gd->alg))
1697 gd->gtk_len = gtk_len;
1698 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1699 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1700 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1701 #ifdef CONFIG_NO_RC4
1702 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1703 "WPA: RC4 not supported in the build");
1705 #else /* CONFIG_NO_RC4 */
1707 if (key_data_len > sizeof(gd->gtk)) {
1708 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1709 "WPA: RC4 key data too long (%lu)",
1710 (unsigned long) key_data_len);
1713 os_memcpy(ek, key->key_iv, 16);
1714 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1715 os_memcpy(gd->gtk, key_data, key_data_len);
1716 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
1717 forced_memzero(ek, sizeof(ek));
1718 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1722 forced_memzero(ek, sizeof(ek));
1723 #endif /* CONFIG_NO_RC4 */
1724 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1725 if (maxkeylen % 8) {
1726 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1727 "WPA: Unsupported AES-WRAP len %lu",
1728 (unsigned long) maxkeylen);
1731 if (maxkeylen > sizeof(gd->gtk)) {
1732 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1733 "WPA: AES-WRAP key data "
1734 "too long (keydatalen=%lu maxkeylen=%lu)",
1735 (unsigned long) key_data_len,
1736 (unsigned long) maxkeylen);
1739 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1740 key_data, gd->gtk)) {
1741 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1742 "WPA: AES unwrap failed - could not decrypt "
1747 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1748 "WPA: Unsupported key_info type %d", ver);
1751 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1752 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1757 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1758 const struct wpa_eapol_key *key,
1759 int ver, u16 key_info)
1761 size_t mic_len, hdrlen, rlen;
1762 struct wpa_eapol_key *reply;
1767 if (wpa_sm_ocv_enabled(sm))
1768 kde_len = OCV_OCI_KDE_LEN;
1769 #endif /* CONFIG_OCV */
1771 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
1772 hdrlen = sizeof(*reply) + mic_len + 2;
1773 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1774 hdrlen + kde_len, &rlen, (void *) &reply);
1778 reply->type = (sm->proto == WPA_PROTO_RSN ||
1779 sm->proto == WPA_PROTO_OSEN) ?
1780 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1781 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1782 key_info |= ver | WPA_KEY_INFO_SECURE;
1784 key_info |= WPA_KEY_INFO_MIC;
1786 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1787 WPA_PUT_BE16(reply->key_info, key_info);
1788 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1789 WPA_PUT_BE16(reply->key_length, 0);
1791 os_memcpy(reply->key_length, key->key_length, 2);
1792 os_memcpy(reply->replay_counter, key->replay_counter,
1793 WPA_REPLAY_COUNTER_LEN);
1795 key_mic = (u8 *) (reply + 1);
1796 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
1799 if (wpa_sm_ocv_enabled(sm)) {
1800 struct wpa_channel_info ci;
1803 if (wpa_sm_channel_info(sm, &ci) != 0) {
1804 wpa_printf(MSG_WARNING,
1805 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
1810 pos = key_mic + mic_len + 2; /* Key Data */
1811 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
1816 #endif /* CONFIG_OCV */
1818 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1819 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
1820 rbuf, rlen, key_mic);
1824 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1825 const unsigned char *src_addr,
1826 const struct wpa_eapol_key *key,
1828 size_t key_data_len, u16 ver)
1832 struct wpa_gtk_data gd;
1835 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
1836 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1837 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1841 os_memset(&gd, 0, sizeof(gd));
1843 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1844 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1845 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1847 key_info = WPA_GET_BE16(key->key_info);
1849 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
1850 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1851 key_data_len, key_info,
1854 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1856 key_info, ver, &gd);
1859 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1864 key_rsc = key->key_rsc;
1865 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1868 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
1869 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
1871 forced_memzero(&gd, sizeof(gd));
1874 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1875 "completed with " MACSTR " [GTK=%s]",
1876 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1877 wpa_sm_cancel_auth_timeout(sm);
1878 wpa_sm_set_state(sm, WPA_COMPLETED);
1880 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1882 WPA_KEY_INFO_SECURE);
1885 wpa_sm_set_rekey_offload(sm);
1890 forced_memzero(&gd, sizeof(gd));
1891 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1895 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1896 struct wpa_eapol_key *key,
1898 const u8 *buf, size_t len)
1900 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
1902 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
1904 os_memcpy(mic, key + 1, mic_len);
1906 os_memset(key + 1, 0, mic_len);
1907 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
1909 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1910 os_memcmp_const(mic, key + 1, mic_len) != 0) {
1911 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1912 "WPA: Invalid EAPOL-Key MIC "
1913 "when using TPTK - ignoring TPTK");
1915 wpa_printf(MSG_INFO,
1916 "TEST: Ignore Key MIC failure for fuzz testing");
1918 #endif /* TEST_FUZZ */
1922 #endif /* TEST_FUZZ */
1926 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1927 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1929 * This assures the same TPTK in sm->tptk can never be
1930 * copied twice to sm->ptk as the new PTK. In
1931 * combination with the installed flag in the wpa_ptk
1932 * struct, this assures the same PTK is only installed
1935 sm->renew_snonce = 1;
1939 if (!ok && sm->ptk_set) {
1940 os_memset(key + 1, 0, mic_len);
1941 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
1943 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1944 os_memcmp_const(mic, key + 1, mic_len) != 0) {
1945 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1946 "WPA: Invalid EAPOL-Key MIC - "
1949 wpa_printf(MSG_INFO,
1950 "TEST: Ignore Key MIC failure for fuzz testing");
1951 goto continue_fuzz2;
1952 #endif /* TEST_FUZZ */
1957 #endif /* TEST_FUZZ */
1962 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1963 "WPA: Could not verify EAPOL-Key MIC - "
1968 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1969 WPA_REPLAY_COUNTER_LEN);
1970 sm->rx_replay_counter_set = 1;
1975 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1976 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1977 struct wpa_eapol_key *key,
1978 size_t mic_len, u16 ver,
1979 u8 *key_data, size_t *key_data_len)
1981 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1982 key_data, *key_data_len);
1984 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1985 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1990 /* Decrypt key data here so that this operation does not need
1991 * to be implemented separately for each message type. */
1992 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1993 #ifdef CONFIG_NO_RC4
1994 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1995 "WPA: RC4 not supported in the build");
1997 #else /* CONFIG_NO_RC4 */
2000 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
2001 os_memcpy(ek, key->key_iv, 16);
2002 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
2003 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
2004 forced_memzero(ek, sizeof(ek));
2005 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2009 forced_memzero(ek, sizeof(ek));
2010 #endif /* CONFIG_NO_RC4 */
2011 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
2012 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
2013 wpa_use_aes_key_wrap(sm->key_mgmt)) {
2016 wpa_printf(MSG_DEBUG,
2017 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
2018 (unsigned int) sm->ptk.kek_len);
2019 if (*key_data_len < 8 || *key_data_len % 8) {
2020 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2021 "WPA: Unsupported AES-WRAP len %u",
2022 (unsigned int) *key_data_len);
2025 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
2026 buf = os_malloc(*key_data_len);
2028 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2029 "WPA: No memory for AES-UNWRAP buffer");
2033 os_memset(buf, 0x11, *key_data_len);
2034 #endif /* TEST_FUZZ */
2035 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
2038 wpa_printf(MSG_INFO,
2039 "TEST: Ignore AES unwrap failure for fuzz testing");
2041 #endif /* TEST_FUZZ */
2042 bin_clear_free(buf, *key_data_len);
2043 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2044 "WPA: AES unwrap failed - "
2045 "could not decrypt EAPOL-Key key data");
2050 #endif /* TEST_FUZZ */
2051 os_memcpy(key_data, buf, *key_data_len);
2052 bin_clear_free(buf, *key_data_len);
2053 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
2055 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2056 "WPA: Unsupported key_info type %d", ver);
2059 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
2060 key_data, *key_data_len);
2066 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2067 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2069 void wpa_sm_aborted_cached(struct wpa_sm *sm)
2071 if (sm && sm->cur_pmksa) {
2072 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2073 "RSN: Cancelling PMKSA caching attempt");
2074 sm->cur_pmksa = NULL;
2079 static void wpa_eapol_key_dump(struct wpa_sm *sm,
2080 const struct wpa_eapol_key *key,
2081 unsigned int key_data_len,
2082 const u8 *mic, unsigned int mic_len)
2084 #ifndef CONFIG_NO_STDOUT_DEBUG
2085 u16 key_info = WPA_GET_BE16(key->key_info);
2087 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
2088 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2089 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2090 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
2091 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2092 WPA_KEY_INFO_KEY_INDEX_SHIFT,
2093 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
2094 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
2095 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
2096 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
2097 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
2098 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
2099 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
2100 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
2101 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
2102 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2103 " key_length=%u key_data_length=%u",
2104 WPA_GET_BE16(key->key_length), key_data_len);
2105 wpa_hexdump(MSG_DEBUG, " replay_counter",
2106 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2107 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
2108 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
2109 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
2110 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
2111 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
2112 #endif /* CONFIG_NO_STDOUT_DEBUG */
2117 static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
2118 size_t *key_data_len)
2120 struct wpa_ptk *ptk;
2121 struct ieee802_1x_hdr *hdr;
2122 struct wpa_eapol_key *key;
2127 if (*key_data_len < AES_BLOCK_SIZE) {
2128 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
2134 else if (sm->ptk_set)
2139 hdr = (struct ieee802_1x_hdr *) buf;
2140 key = (struct wpa_eapol_key *) (hdr + 1);
2141 pos = (u8 *) (key + 1);
2142 pos += 2; /* Pointing at the Encrypted Key Data field */
2144 tmp = os_malloc(*key_data_len);
2148 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2149 * to Key Data (exclusive). */
2151 aad_len[0] = pos - buf;
2152 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
2153 1, aad, aad_len, tmp) < 0) {
2154 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
2155 bin_clear_free(tmp, *key_data_len);
2159 /* AEAD decryption and validation completed successfully */
2160 (*key_data_len) -= AES_BLOCK_SIZE;
2161 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2162 tmp, *key_data_len);
2164 /* Replace Key Data field with the decrypted version */
2165 os_memcpy(pos, tmp, *key_data_len);
2166 pos -= 2; /* Key Data Length field */
2167 WPA_PUT_BE16(pos, *key_data_len);
2168 bin_clear_free(tmp, *key_data_len);
2173 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
2174 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2177 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2178 WPA_REPLAY_COUNTER_LEN);
2179 sm->rx_replay_counter_set = 1;
2183 #endif /* CONFIG_FILS */
2187 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2188 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2189 * @src_addr: Source MAC address of the EAPOL packet
2190 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2191 * @len: Length of the EAPOL frame
2192 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2194 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2195 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2196 * only processing WPA and WPA2 EAPOL-Key frames.
2198 * The received EAPOL-Key packets are validated and valid packets are replied
2199 * to. In addition, key material (PTK, GTK) is configured at the end of a
2200 * successful key handshake.
2202 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
2203 const u8 *buf, size_t len)
2205 size_t plen, data_len, key_data_len;
2206 const struct ieee802_1x_hdr *hdr;
2207 struct wpa_eapol_key *key;
2212 size_t mic_len, keyhdrlen;
2214 #ifdef CONFIG_IEEE80211R
2215 sm->ft_completed = 0;
2216 #endif /* CONFIG_IEEE80211R */
2218 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
2219 keyhdrlen = sizeof(*key) + mic_len + 2;
2221 if (len < sizeof(*hdr) + keyhdrlen) {
2222 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2223 "WPA: EAPOL frame too short to be a WPA "
2224 "EAPOL-Key (len %lu, expecting at least %lu)",
2225 (unsigned long) len,
2226 (unsigned long) sizeof(*hdr) + keyhdrlen);
2230 hdr = (const struct ieee802_1x_hdr *) buf;
2231 plen = be_to_host16(hdr->length);
2232 data_len = plen + sizeof(*hdr);
2233 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2234 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2235 hdr->version, hdr->type, (unsigned long) plen);
2237 if (hdr->version < EAPOL_VERSION) {
2238 /* TODO: backwards compatibility */
2240 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2241 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2242 "WPA: EAPOL frame (type %u) discarded, "
2243 "not a Key frame", hdr->type);
2247 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
2248 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
2249 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2250 "WPA: EAPOL frame payload size %lu "
2251 "invalid (frame size %lu)",
2252 (unsigned long) plen, (unsigned long) len);
2256 if (data_len < len) {
2257 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2258 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2259 (unsigned long) len - data_len);
2263 * Make a copy of the frame since we need to modify the buffer during
2264 * MAC validation and Key Data decryption.
2266 tmp = os_memdup(buf, data_len);
2269 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
2270 mic = (u8 *) (key + 1);
2271 key_data = mic + mic_len + 2;
2273 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2275 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2276 "WPA: EAPOL-Key type (%d) unknown, discarded",
2282 key_data_len = WPA_GET_BE16(mic + mic_len);
2283 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
2285 if (key_data_len > plen - keyhdrlen) {
2286 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2287 "frame - key_data overflow (%u > %u)",
2288 (unsigned int) key_data_len,
2289 (unsigned int) (plen - keyhdrlen));
2293 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
2294 key_info = WPA_GET_BE16(key->key_info);
2295 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2296 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
2297 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
2298 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2299 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
2300 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
2301 !wpa_use_akm_defined(sm->key_mgmt)) {
2302 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2303 "WPA: Unsupported EAPOL-Key descriptor version %d",
2308 if (wpa_use_akm_defined(sm->key_mgmt) &&
2309 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2310 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2311 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2316 #ifdef CONFIG_IEEE80211R
2317 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2318 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
2319 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2320 !wpa_use_akm_defined(sm->key_mgmt)) {
2321 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2322 "FT: AP did not use AES-128-CMAC");
2326 #endif /* CONFIG_IEEE80211R */
2327 #ifdef CONFIG_IEEE80211W
2328 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
2329 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2330 !wpa_use_akm_defined(sm->key_mgmt)) {
2331 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2332 "WPA: AP did not use the "
2333 "negotiated AES-128-CMAC");
2337 #endif /* CONFIG_IEEE80211W */
2338 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
2339 !wpa_use_akm_defined(sm->key_mgmt) &&
2340 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2341 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2342 "WPA: CCMP is used, but EAPOL-Key "
2343 "descriptor version (%d) is not 2", ver);
2344 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2345 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2346 /* Earlier versions of IEEE 802.11i did not explicitly
2347 * require version 2 descriptor for all EAPOL-Key
2348 * packets, so allow group keys to use version 1 if
2349 * CCMP is not used for them. */
2350 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2351 "WPA: Backwards compatibility: allow invalid "
2352 "version for non-CCMP group keys");
2353 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2354 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2355 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
2358 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
2359 !wpa_use_akm_defined(sm->key_mgmt) &&
2360 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2361 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2362 "WPA: GCMP is used, but EAPOL-Key "
2363 "descriptor version (%d) is not 2", ver);
2367 if (sm->rx_replay_counter_set &&
2368 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2369 WPA_REPLAY_COUNTER_LEN) <= 0) {
2370 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2371 "WPA: EAPOL-Key Replay Counter did not increase - "
2376 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2377 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2378 "WPA: Unsupported SMK bit in key_info");
2382 if (!(key_info & WPA_KEY_INFO_ACK)) {
2383 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2384 "WPA: No Ack bit in key_info");
2388 if (key_info & WPA_KEY_INFO_REQUEST) {
2389 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2390 "WPA: EAPOL-Key with Request bit - dropped");
2394 if ((key_info & WPA_KEY_INFO_MIC) &&
2395 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
2399 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2400 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2403 #endif /* CONFIG_FILS */
2405 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
2406 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
2408 * Only decrypt the Key Data field if the frame's authenticity
2409 * was verified. When using AES-SIV (FILS), the MIC flag is not
2410 * set, so this check should only be performed if mic_len != 0
2411 * which is the case in this code branch.
2413 if (!(key_info & WPA_KEY_INFO_MIC)) {
2414 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2415 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2418 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2424 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2425 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2426 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2427 "WPA: Ignored EAPOL-Key (Pairwise) with "
2428 "non-zero key index");
2431 if (key_info & (WPA_KEY_INFO_MIC |
2432 WPA_KEY_INFO_ENCR_KEY_DATA)) {
2433 /* 3/4 4-Way Handshake */
2434 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2437 /* 1/4 4-Way Handshake */
2438 wpa_supplicant_process_1_of_4(sm, src_addr, key,
2443 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2444 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
2445 /* 1/2 Group Key Handshake */
2446 wpa_supplicant_process_1_of_2(sm, src_addr, key,
2447 key_data, key_data_len,
2450 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2451 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
2459 bin_clear_free(tmp, data_len);
2464 #ifdef CONFIG_CTRL_IFACE
2465 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2467 switch (sm->key_mgmt) {
2468 case WPA_KEY_MGMT_IEEE8021X:
2469 return ((sm->proto == WPA_PROTO_RSN ||
2470 sm->proto == WPA_PROTO_OSEN) ?
2471 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2472 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2473 case WPA_KEY_MGMT_PSK:
2474 return (sm->proto == WPA_PROTO_RSN ?
2475 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2476 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2477 #ifdef CONFIG_IEEE80211R
2478 case WPA_KEY_MGMT_FT_IEEE8021X:
2479 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2480 case WPA_KEY_MGMT_FT_PSK:
2481 return RSN_AUTH_KEY_MGMT_FT_PSK;
2482 #endif /* CONFIG_IEEE80211R */
2483 #ifdef CONFIG_IEEE80211W
2484 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2485 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2486 case WPA_KEY_MGMT_PSK_SHA256:
2487 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2488 #endif /* CONFIG_IEEE80211W */
2489 case WPA_KEY_MGMT_CCKM:
2490 return (sm->proto == WPA_PROTO_RSN ?
2491 RSN_AUTH_KEY_MGMT_CCKM:
2492 WPA_AUTH_KEY_MGMT_CCKM);
2493 case WPA_KEY_MGMT_WPA_NONE:
2494 return WPA_AUTH_KEY_MGMT_NONE;
2495 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2496 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
2497 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2498 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
2505 #define RSN_SUITE "%02x-%02x-%02x-%d"
2506 #define RSN_SUITE_ARG(s) \
2507 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2510 * wpa_sm_get_mib - Dump text list of MIB entries
2511 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2512 * @buf: Buffer for the list
2513 * @buflen: Length of the buffer
2514 * Returns: Number of bytes written to buffer
2516 * This function is used fetch dot11 MIB variables.
2518 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2520 char pmkid_txt[PMKID_LEN * 2 + 1];
2524 if (sm->cur_pmksa) {
2525 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2526 sm->cur_pmksa->pmkid, PMKID_LEN);
2528 pmkid_txt[0] = '\0';
2530 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2531 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2532 sm->proto == WPA_PROTO_RSN)
2537 ret = os_snprintf(buf, buflen,
2538 "dot11RSNAOptionImplemented=TRUE\n"
2539 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2540 "dot11RSNAEnabled=%s\n"
2541 "dot11RSNAPreauthenticationEnabled=%s\n"
2542 "dot11RSNAConfigVersion=%d\n"
2543 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2544 "dot11RSNAConfigGroupCipherSize=%d\n"
2545 "dot11RSNAConfigPMKLifetime=%d\n"
2546 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2547 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2548 "dot11RSNAConfigSATimeout=%d\n",
2549 rsna ? "TRUE" : "FALSE",
2550 rsna ? "TRUE" : "FALSE",
2552 wpa_cipher_key_len(sm->group_cipher) * 8,
2553 sm->dot11RSNAConfigPMKLifetime,
2554 sm->dot11RSNAConfigPMKReauthThreshold,
2555 sm->dot11RSNAConfigSATimeout);
2556 if (os_snprintf_error(buflen, ret))
2561 buf + len, buflen - len,
2562 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2563 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2564 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2565 "dot11RSNAPMKIDUsed=%s\n"
2566 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2567 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2568 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2569 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2570 "dot11RSNA4WayHandshakeFailures=%u\n",
2571 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2572 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2573 sm->pairwise_cipher)),
2574 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2577 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2578 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2579 sm->pairwise_cipher)),
2580 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2582 sm->dot11RSNA4WayHandshakeFailures);
2583 if (!os_snprintf_error(buflen - len, ret))
2588 #endif /* CONFIG_CTRL_IFACE */
2591 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
2592 void *ctx, enum pmksa_free_reason reason)
2594 struct wpa_sm *sm = ctx;
2597 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2598 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2600 if (sm->cur_pmksa == entry) {
2601 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2602 "RSN: %s current PMKSA entry",
2603 reason == PMKSA_REPLACE ? "replaced" : "removed");
2604 pmksa_cache_clear_current(sm);
2607 * If an entry is simply being replaced, there's no need to
2608 * deauthenticate because it will be immediately re-added.
2609 * This happens when EAP authentication is completed again
2610 * (reauth or failed PMKSA caching attempt).
2612 if (reason != PMKSA_REPLACE)
2616 if (reason == PMKSA_EXPIRE &&
2617 (sm->pmk_len == entry->pmk_len &&
2618 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2619 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2620 "RSN: deauthenticating due to expired PMK");
2621 pmksa_cache_clear_current(sm);
2627 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2628 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2634 * wpa_sm_init - Initialize WPA state machine
2635 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2636 * Returns: Pointer to the allocated WPA state machine data
2638 * This function is used to allocate a new WPA state machine and the returned
2639 * value is passed to all WPA state machine calls.
2641 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2645 sm = os_zalloc(sizeof(*sm));
2648 dl_list_init(&sm->pmksa_candidates);
2649 sm->renew_snonce = 1;
2652 sm->dot11RSNAConfigPMKLifetime = 43200;
2653 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2654 sm->dot11RSNAConfigSATimeout = 60;
2656 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2657 if (sm->pmksa == NULL) {
2658 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2659 "RSN: PMKSA cache initialization failed");
2669 * wpa_sm_deinit - Deinitialize WPA state machine
2670 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2672 void wpa_sm_deinit(struct wpa_sm *sm)
2676 pmksa_cache_deinit(sm->pmksa);
2677 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2678 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2679 os_free(sm->assoc_wpa_ie);
2680 os_free(sm->ap_wpa_ie);
2681 os_free(sm->ap_rsn_ie);
2684 #ifdef CONFIG_IEEE80211R
2685 os_free(sm->assoc_resp_ies);
2686 #endif /* CONFIG_IEEE80211R */
2687 #ifdef CONFIG_TESTING_OPTIONS
2688 wpabuf_free(sm->test_assoc_ie);
2689 #endif /* CONFIG_TESTING_OPTIONS */
2690 #ifdef CONFIG_FILS_SK_PFS
2691 crypto_ecdh_deinit(sm->fils_ecdh);
2692 #endif /* CONFIG_FILS_SK_PFS */
2694 wpabuf_free(sm->fils_ft_ies);
2695 #endif /* CONFIG_FILS */
2697 crypto_ecdh_deinit(sm->owe_ecdh);
2698 #endif /* CONFIG_OWE */
2700 wpabuf_clear_free(sm->dpp_z);
2701 #endif /* CONFIG_DPP2 */
2707 * wpa_sm_notify_assoc - Notify WPA state machine about association
2708 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2709 * @bssid: The BSSID of the new association
2711 * This function is called to let WPA state machine know that the connection
2714 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2721 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2722 "WPA: Association event - clear replay counter");
2723 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2724 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2725 sm->rx_replay_counter_set = 0;
2726 sm->renew_snonce = 1;
2727 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2728 rsn_preauth_deinit(sm);
2730 #ifdef CONFIG_IEEE80211R
2731 if (wpa_ft_is_completed(sm)) {
2733 * Clear portValid to kick EAPOL state machine to re-enter
2734 * AUTHENTICATED state to get the EAPOL port Authorized.
2736 eapol_sm_notify_portValid(sm->eapol, FALSE);
2737 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2739 /* Prepare for the next transition */
2740 wpa_ft_prepare_auth_request(sm, NULL);
2743 sm->ft_protocol = 1;
2745 sm->ft_protocol = 0;
2747 #endif /* CONFIG_IEEE80211R */
2749 if (sm->fils_completed) {
2751 * Clear portValid to kick EAPOL state machine to re-enter
2752 * AUTHENTICATED state to get the EAPOL port Authorized.
2754 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2757 #endif /* CONFIG_FILS */
2761 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2762 * this is not part of a Fast BSS Transition.
2764 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2766 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2768 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2769 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2770 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
2771 #ifdef CONFIG_IEEE80211W
2772 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2773 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
2774 #endif /* CONFIG_IEEE80211W */
2779 #endif /* CONFIG_TDLS */
2782 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2783 #endif /* CONFIG_P2P */
2788 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2789 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2791 * This function is called to let WPA state machine know that the connection
2792 * was lost. This will abort any existing pre-authentication session.
2794 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2796 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2797 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2798 rsn_preauth_deinit(sm);
2799 pmksa_cache_clear_current(sm);
2800 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2801 sm->dot11RSNA4WayHandshakeFailures++;
2803 wpa_tdls_disassoc(sm);
2804 #endif /* CONFIG_TDLS */
2806 sm->fils_completed = 0;
2807 #endif /* CONFIG_FILS */
2808 #ifdef CONFIG_IEEE80211R
2809 sm->ft_reassoc_completed = 0;
2810 sm->ft_protocol = 0;
2811 #endif /* CONFIG_IEEE80211R */
2813 /* Keys are not needed in the WPA state machine anymore */
2816 sm->msg_3_of_4_ok = 0;
2817 os_memset(sm->bssid, 0, ETH_ALEN);
2822 * wpa_sm_set_pmk - Set PMK
2823 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2825 * @pmk_len: The length of the new PMK in bytes
2826 * @pmkid: Calculated PMKID
2827 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
2829 * Configure the PMK for WPA state machine.
2831 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2832 const u8 *pmkid, const u8 *bssid)
2837 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
2839 sm->pmk_len = pmk_len;
2840 os_memcpy(sm->pmk, pmk, pmk_len);
2842 #ifdef CONFIG_IEEE80211R
2843 /* Set XXKey to be PSK for FT key derivation */
2844 sm->xxkey_len = pmk_len;
2845 os_memcpy(sm->xxkey, pmk, pmk_len);
2846 #endif /* CONFIG_IEEE80211R */
2849 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
2850 bssid, sm->own_addr,
2851 sm->network_ctx, sm->key_mgmt, NULL);
2857 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2858 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2860 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2863 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2868 if (sm->cur_pmksa) {
2869 wpa_hexdump_key(MSG_DEBUG,
2870 "WPA: Set PMK based on current PMKSA",
2871 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
2872 sm->pmk_len = sm->cur_pmksa->pmk_len;
2873 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2875 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
2877 os_memset(sm->pmk, 0, PMK_LEN_MAX);
2883 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2884 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2885 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2887 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2890 sm->fast_reauth = fast_reauth;
2895 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2896 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2897 * @scard_ctx: Context pointer for smartcard related callback functions
2899 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2903 sm->scard_ctx = scard_ctx;
2904 if (sm->preauth_eapol)
2905 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2910 * wpa_sm_set_config - Notification of current configration change
2911 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2912 * @config: Pointer to current network configuration
2914 * Notify WPA state machine that configuration has changed. config will be
2915 * stored as a backpointer to network configuration. This can be %NULL to clear
2916 * the stored pointed.
2918 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2924 sm->network_ctx = config->network_ctx;
2925 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2926 sm->proactive_key_caching = config->proactive_key_caching;
2927 sm->eap_workaround = config->eap_workaround;
2928 sm->eap_conf_ctx = config->eap_conf_ctx;
2930 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2931 sm->ssid_len = config->ssid_len;
2934 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2935 sm->p2p = config->p2p;
2936 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
2938 if (config->fils_cache_id) {
2939 sm->fils_cache_id_set = 1;
2940 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
2943 sm->fils_cache_id_set = 0;
2945 #endif /* CONFIG_FILS */
2947 sm->network_ctx = NULL;
2948 sm->allowed_pairwise_cipher = 0;
2949 sm->proactive_key_caching = 0;
2950 sm->eap_workaround = 0;
2951 sm->eap_conf_ctx = NULL;
2953 sm->wpa_ptk_rekey = 0;
2955 sm->wpa_rsc_relaxation = 0;
2961 * wpa_sm_set_own_addr - Set own MAC address
2962 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2963 * @addr: Own MAC address
2965 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2968 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2973 * wpa_sm_set_ifname - Set network interface name
2974 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2975 * @ifname: Interface name
2976 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2978 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2979 const char *bridge_ifname)
2982 sm->ifname = ifname;
2983 sm->bridge_ifname = bridge_ifname;
2989 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2990 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2991 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2993 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
3001 * wpa_sm_set_param - Set WPA state machine parameters
3002 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3003 * @param: Parameter field
3004 * @value: Parameter value
3005 * Returns: 0 on success, -1 on failure
3007 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
3016 case RSNA_PMK_LIFETIME:
3018 sm->dot11RSNAConfigPMKLifetime = value;
3022 case RSNA_PMK_REAUTH_THRESHOLD:
3023 if (value > 0 && value <= 100)
3024 sm->dot11RSNAConfigPMKReauthThreshold = value;
3028 case RSNA_SA_TIMEOUT:
3030 sm->dot11RSNAConfigSATimeout = value;
3034 case WPA_PARAM_PROTO:
3037 case WPA_PARAM_PAIRWISE:
3038 sm->pairwise_cipher = value;
3040 case WPA_PARAM_GROUP:
3041 sm->group_cipher = value;
3043 case WPA_PARAM_KEY_MGMT:
3044 sm->key_mgmt = value;
3046 #ifdef CONFIG_IEEE80211W
3047 case WPA_PARAM_MGMT_GROUP:
3048 sm->mgmt_group_cipher = value;
3050 #endif /* CONFIG_IEEE80211W */
3051 case WPA_PARAM_RSN_ENABLED:
3052 sm->rsn_enabled = value;
3069 * wpa_sm_get_status - Get WPA state machine
3070 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3071 * @buf: Buffer for status information
3072 * @buflen: Maximum buffer length
3073 * @verbose: Whether to include verbose status information
3074 * Returns: Number of bytes written to buf.
3076 * Query WPA state machine for status information. This function fills in
3077 * a text area with current status information. If the buffer (buf) is not
3078 * large enough, status information will be truncated to fit the buffer.
3080 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
3083 char *pos = buf, *end = buf + buflen;
3086 ret = os_snprintf(pos, end - pos,
3087 "pairwise_cipher=%s\n"
3090 wpa_cipher_txt(sm->pairwise_cipher),
3091 wpa_cipher_txt(sm->group_cipher),
3092 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
3093 if (os_snprintf_error(end - pos, ret))
3097 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
3098 struct wpa_ie_data rsn;
3099 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
3101 rsn.capabilities & (WPA_CAPABILITY_MFPR |
3102 WPA_CAPABILITY_MFPC)) {
3103 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
3104 "mgmt_group_cipher=%s\n",
3106 WPA_CAPABILITY_MFPR) ? 2 : 1,
3108 sm->mgmt_group_cipher));
3109 if (os_snprintf_error(end - pos, ret))
3119 int wpa_sm_pmf_enabled(struct wpa_sm *sm)
3121 struct wpa_ie_data rsn;
3123 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
3126 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
3127 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
3134 int wpa_sm_ocv_enabled(struct wpa_sm *sm)
3136 struct wpa_ie_data rsn;
3138 if (!sm->ocv || !sm->ap_rsn_ie)
3141 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
3143 (rsn.capabilities & WPA_CAPABILITY_OCVC);
3148 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3149 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3150 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3151 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3152 * Returns: 0 on success, -1 on failure
3154 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
3162 #ifdef CONFIG_TESTING_OPTIONS
3163 if (sm->test_assoc_ie) {
3164 wpa_printf(MSG_DEBUG,
3165 "TESTING: Replace association WPA/RSN IE");
3166 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
3168 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
3169 wpabuf_len(sm->test_assoc_ie));
3170 res = wpabuf_len(sm->test_assoc_ie);
3172 #endif /* CONFIG_TESTING_OPTIONS */
3173 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
3178 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
3179 wpa_ie, *wpa_ie_len);
3181 if (sm->assoc_wpa_ie == NULL) {
3183 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3184 * the correct version of the IE even if PMKSA caching is
3185 * aborted (which would remove PMKID from IE generation).
3187 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
3188 if (sm->assoc_wpa_ie == NULL)
3191 sm->assoc_wpa_ie_len = *wpa_ie_len;
3193 wpa_hexdump(MSG_DEBUG,
3194 "WPA: Leave previously set WPA IE default",
3195 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3203 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3204 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3205 * @ie: Pointer to IE data (starting from id)
3207 * Returns: 0 on success, -1 on failure
3209 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3210 * Request frame. The IE will be used to override the default value generated
3211 * with wpa_sm_set_assoc_wpa_ie_default().
3213 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3218 os_free(sm->assoc_wpa_ie);
3219 if (ie == NULL || len == 0) {
3220 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3221 "WPA: clearing own WPA/RSN IE");
3222 sm->assoc_wpa_ie = NULL;
3223 sm->assoc_wpa_ie_len = 0;
3225 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
3226 sm->assoc_wpa_ie = os_memdup(ie, len);
3227 if (sm->assoc_wpa_ie == NULL)
3230 sm->assoc_wpa_ie_len = len;
3238 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3239 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3240 * @ie: Pointer to IE data (starting from id)
3242 * Returns: 0 on success, -1 on failure
3244 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3247 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3252 os_free(sm->ap_wpa_ie);
3253 if (ie == NULL || len == 0) {
3254 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3255 "WPA: clearing AP WPA IE");
3256 sm->ap_wpa_ie = NULL;
3257 sm->ap_wpa_ie_len = 0;
3259 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
3260 sm->ap_wpa_ie = os_memdup(ie, len);
3261 if (sm->ap_wpa_ie == NULL)
3264 sm->ap_wpa_ie_len = len;
3272 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3273 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3274 * @ie: Pointer to IE data (starting from id)
3276 * Returns: 0 on success, -1 on failure
3278 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3281 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3286 os_free(sm->ap_rsn_ie);
3287 if (ie == NULL || len == 0) {
3288 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3289 "WPA: clearing AP RSN IE");
3290 sm->ap_rsn_ie = NULL;
3291 sm->ap_rsn_ie_len = 0;
3293 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
3294 sm->ap_rsn_ie = os_memdup(ie, len);
3295 if (sm->ap_rsn_ie == NULL)
3298 sm->ap_rsn_ie_len = len;
3306 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3307 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3308 * @data: Pointer to data area for parsing results
3309 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3311 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3312 * parsed data into data.
3314 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3319 if (sm->assoc_wpa_ie == NULL) {
3320 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3321 "WPA: No WPA/RSN IE available from association info");
3324 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3330 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3332 return pmksa_cache_list(sm->pmksa, buf, len);
3336 struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3338 return pmksa_cache_head(sm->pmksa);
3342 struct rsn_pmksa_cache_entry *
3343 wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3344 struct rsn_pmksa_cache_entry * entry)
3346 return pmksa_cache_add_entry(sm->pmksa, entry);
3350 void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3351 const u8 *pmkid, const u8 *bssid,
3352 const u8 *fils_cache_id)
3354 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3355 bssid, sm->own_addr, sm->network_ctx,
3356 sm->key_mgmt, fils_cache_id);
3360 int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3361 const void *network_ctx)
3363 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
3367 void wpa_sm_drop_sa(struct wpa_sm *sm)
3369 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3373 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3374 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3375 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3376 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
3377 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
3378 #ifdef CONFIG_IEEE80211W
3379 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
3380 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
3381 #endif /* CONFIG_IEEE80211W */
3382 #ifdef CONFIG_IEEE80211R
3383 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
3385 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
3387 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
3389 #endif /* CONFIG_IEEE80211R */
3393 int wpa_sm_has_ptk(struct wpa_sm *sm)
3401 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3403 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3407 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3409 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
3414 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3417 u8 keylen; /* plaintext key len */
3420 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
3421 struct wpa_gtk_data gd;
3423 os_memset(&gd, 0, sizeof(gd));
3424 keylen = wpa_cipher_key_len(sm->group_cipher);
3425 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3426 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3427 if (gd.alg == WPA_ALG_NONE) {
3428 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3433 keyinfo = WPA_GET_LE16(buf + 2);
3434 gd.gtk_len = keylen;
3435 if (gd.gtk_len != buf[4]) {
3436 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3437 gd.gtk_len, buf[4]);
3440 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3441 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3442 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3444 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
3446 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3447 gd.gtk, gd.gtk_len);
3448 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
3449 forced_memzero(&gd, sizeof(gd));
3450 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3454 forced_memzero(&gd, sizeof(gd));
3455 #ifdef CONFIG_IEEE80211W
3456 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
3457 const struct wpa_igtk_kde *igtk;
3459 igtk = (const struct wpa_igtk_kde *) (buf + 2);
3460 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
3462 #endif /* CONFIG_IEEE80211W */
3464 wpa_printf(MSG_DEBUG, "Unknown element id");
3470 #endif /* CONFIG_WNM */
3475 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3477 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3479 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3483 #endif /* CONFIG_P2P */
3486 void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3488 if (rx_replay_counter == NULL)
3491 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3492 WPA_REPLAY_COUNTER_LEN);
3493 sm->rx_replay_counter_set = 1;
3494 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3498 void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3499 const u8 *ptk_kck, size_t ptk_kck_len,
3500 const u8 *ptk_kek, size_t ptk_kek_len)
3502 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3503 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3504 sm->ptk.kck_len = ptk_kck_len;
3505 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3507 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3508 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3509 sm->ptk.kek_len = ptk_kek_len;
3510 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3516 #ifdef CONFIG_TESTING_OPTIONS
3518 void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3520 wpabuf_free(sm->test_assoc_ie);
3521 sm->test_assoc_ie = buf;
3525 const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
3530 #endif /* CONFIG_TESTING_OPTIONS */
3533 unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
3535 return sm->key_mgmt;
3541 struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
3543 struct wpabuf *buf = NULL;
3544 struct wpabuf *erp_msg;
3545 struct wpabuf *pub = NULL;
3547 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
3548 if (!erp_msg && !sm->cur_pmksa) {
3549 wpa_printf(MSG_DEBUG,
3550 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3554 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3555 erp_msg != NULL, sm->cur_pmksa != NULL);
3557 sm->fils_completed = 0;
3559 if (!sm->assoc_wpa_ie) {
3560 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
3564 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
3565 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
3568 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
3569 sm->fils_nonce, FILS_NONCE_LEN);
3570 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
3571 sm->fils_session, FILS_SESSION_LEN);
3573 #ifdef CONFIG_FILS_SK_PFS
3574 sm->fils_dh_group = dh_group;
3576 crypto_ecdh_deinit(sm->fils_ecdh);
3577 sm->fils_ecdh = crypto_ecdh_init(dh_group);
3578 if (!sm->fils_ecdh) {
3579 wpa_printf(MSG_INFO,
3580 "FILS: Could not initialize ECDH with group %d",
3584 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3587 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
3589 sm->fils_dh_elem_len = wpabuf_len(pub);
3591 #endif /* CONFIG_FILS_SK_PFS */
3593 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
3594 (pub ? wpabuf_len(pub) : 0));
3598 /* Fields following the Authentication algorithm number field */
3600 /* Authentication Transaction seq# */
3601 wpabuf_put_le16(buf, 1);
3604 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
3607 #ifdef CONFIG_FILS_SK_PFS
3609 /* Finite Cyclic Group */
3610 wpabuf_put_le16(buf, dh_group);
3612 wpabuf_put_buf(buf, pub);
3614 #endif /* CONFIG_FILS_SK_PFS */
3617 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
3618 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3619 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3622 /* MDE when using FILS for FT initial association */
3623 struct rsn_mdie *mdie;
3625 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
3626 wpabuf_put_u8(buf, sizeof(*mdie));
3627 mdie = wpabuf_put(buf, sizeof(*mdie));
3628 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
3633 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3634 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
3635 /* Element ID Extension */
3636 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
3637 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
3640 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3641 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3642 /* Element ID Extension */
3643 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3644 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3646 /* FILS Wrapped Data */
3647 sm->fils_erp_pmkid_set = 0;
3649 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3650 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
3651 /* Element ID Extension */
3652 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA);
3653 wpabuf_put_buf(buf, erp_msg);
3654 /* Calculate pending PMKID here so that we do not need to
3655 * maintain a copy of the EAP-Initiate/Reauth message. */
3656 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
3657 wpabuf_len(erp_msg),
3658 sm->fils_erp_pmkid) == 0)
3659 sm->fils_erp_pmkid_set = 1;
3662 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
3666 wpabuf_free(erp_msg);
3672 int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
3675 const u8 *pos, *end;
3676 struct ieee802_11_elems elems;
3677 struct wpa_ie_data rsn;
3678 int pmkid_match = 0;
3679 u8 ick[FILS_ICK_MAX_LEN];
3682 struct wpabuf *dh_ss = NULL;
3683 const u8 *g_sta = NULL;
3684 size_t g_sta_len = 0;
3685 const u8 *g_ap = NULL;
3686 size_t g_ap_len = 0;
3687 struct wpabuf *pub = NULL;
3689 os_memcpy(sm->bssid, bssid, ETH_ALEN);
3691 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
3697 #ifdef CONFIG_FILS_SK_PFS
3698 if (sm->fils_dh_group) {
3701 /* Using FILS PFS */
3703 /* Finite Cyclic Group */
3704 if (end - pos < 2) {
3705 wpa_printf(MSG_DEBUG,
3706 "FILS: No room for Finite Cyclic Group");
3709 group = WPA_GET_LE16(pos);
3711 if (group != sm->fils_dh_group) {
3712 wpa_printf(MSG_DEBUG,
3713 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
3714 group, sm->fils_dh_group);
3719 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
3720 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
3724 if (!sm->fils_ecdh) {
3725 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
3728 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
3729 sm->fils_dh_elem_len);
3731 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
3734 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
3736 g_ap_len = sm->fils_dh_elem_len;
3737 pos += sm->fils_dh_elem_len;
3739 #endif /* CONFIG_FILS_SK_PFS */
3741 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
3742 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
3743 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
3748 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
3750 if (!elems.rsn_ie ||
3751 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3753 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
3757 if (!elems.fils_nonce) {
3758 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
3761 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
3762 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
3764 #ifdef CONFIG_IEEE80211R
3765 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3766 struct wpa_ft_ies parse;
3768 if (!elems.mdie || !elems.ftie) {
3769 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
3773 if (wpa_ft_parse_ies(pos, end - pos, &parse,
3774 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
3775 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
3779 if (!parse.r0kh_id) {
3780 wpa_printf(MSG_DEBUG,
3781 "FILS+FT: No R0KH-ID subelem in FTE");
3784 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
3785 sm->r0kh_id_len = parse.r0kh_id_len;
3786 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3787 sm->r0kh_id, sm->r0kh_id_len);
3789 if (!parse.r1kh_id) {
3790 wpa_printf(MSG_DEBUG,
3791 "FILS+FT: No R1KH-ID subelem in FTE");
3794 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
3795 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
3796 sm->r1kh_id, FT_R1KH_ID_LEN);
3798 /* TODO: Check MDE and FTE payload */
3800 wpabuf_free(sm->fils_ft_ies);
3801 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
3802 2 + elems.ftie_len);
3803 if (!sm->fils_ft_ies)
3805 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
3806 2 + elems.mdie_len);
3807 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
3808 2 + elems.ftie_len);
3810 wpabuf_free(sm->fils_ft_ies);
3811 sm->fils_ft_ies = NULL;
3813 #endif /* CONFIG_IEEE80211R */
3816 if (rsn.pmkid && rsn.num_pmkid > 0) {
3817 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
3818 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
3820 if (rsn.num_pmkid != 1) {
3821 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
3824 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
3825 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
3827 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
3828 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
3829 sm->cur_pmksa->pmkid, PMKID_LEN);
3832 wpa_printf(MSG_DEBUG,
3833 "FILS: Matching PMKID - continue using PMKSA caching");
3836 if (!pmkid_match && sm->cur_pmksa) {
3837 wpa_printf(MSG_DEBUG,
3838 "FILS: No PMKID match - cannot use cached PMKSA entry");
3839 sm->cur_pmksa = NULL;
3843 if (!elems.fils_session) {
3844 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
3847 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
3849 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
3851 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
3852 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
3853 sm->fils_session, FILS_SESSION_LEN);
3857 /* FILS Wrapped Data */
3858 if (!sm->cur_pmksa && elems.fils_wrapped_data) {
3859 u8 rmsk[ERP_MAX_KEY_LEN];
3862 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
3863 elems.fils_wrapped_data,
3864 elems.fils_wrapped_data_len);
3865 eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data,
3866 elems.fils_wrapped_data_len);
3867 if (eapol_sm_failed(sm->eapol))
3870 rmsk_len = ERP_MAX_KEY_LEN;
3871 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3872 if (res == PMK_LEN) {
3874 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3879 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
3880 sm->fils_nonce, sm->fils_anonce,
3881 dh_ss ? wpabuf_head(dh_ss) : NULL,
3882 dh_ss ? wpabuf_len(dh_ss) : 0,
3883 sm->pmk, &sm->pmk_len);
3884 forced_memzero(rmsk, sizeof(rmsk));
3886 /* Don't use DHss in PTK derivation if PMKSA caching is not
3888 wpabuf_clear_free(dh_ss);
3894 if (!sm->fils_erp_pmkid_set) {
3895 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
3898 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
3900 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
3901 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
3902 sm->fils_erp_pmkid, NULL, 0,
3903 sm->bssid, sm->own_addr,
3904 sm->network_ctx, sm->key_mgmt,
3908 if (!sm->cur_pmksa) {
3909 wpa_printf(MSG_DEBUG,
3910 "FILS: No remaining options to continue FILS authentication");
3914 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
3915 sm->fils_nonce, sm->fils_anonce,
3916 dh_ss ? wpabuf_head(dh_ss) : NULL,
3917 dh_ss ? wpabuf_len(dh_ss) : 0,
3918 &sm->ptk, ick, &ick_len,
3919 sm->key_mgmt, sm->pairwise_cipher,
3920 sm->fils_ft, &sm->fils_ft_len) < 0) {
3921 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
3925 wpabuf_clear_free(dh_ss);
3930 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3932 #ifdef CONFIG_FILS_SK_PFS
3933 if (sm->fils_dh_group) {
3934 if (!sm->fils_ecdh) {
3935 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
3938 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3941 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
3942 g_sta = wpabuf_head(pub);
3943 g_sta_len = wpabuf_len(pub);
3945 wpa_printf(MSG_INFO, "FILS: gAP not available");
3948 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
3950 #endif /* CONFIG_FILS_SK_PFS */
3952 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
3953 sm->fils_anonce, sm->own_addr, sm->bssid,
3954 g_sta, g_sta_len, g_ap, g_ap_len,
3955 sm->key_mgmt, sm->fils_key_auth_sta,
3956 sm->fils_key_auth_ap,
3957 &sm->fils_key_auth_len);
3959 forced_memzero(ick, sizeof(ick));
3963 wpabuf_clear_free(dh_ss);
3968 #ifdef CONFIG_IEEE80211R
3969 static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
3971 struct rsn_ie_hdr *rsnie;
3974 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
3976 /* RSNIE[PMKR0Name/PMKR1Name] */
3977 rsnie = wpabuf_put(buf, sizeof(*rsnie));
3978 rsnie->elem_id = WLAN_EID_RSN;
3979 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
3981 /* Group Suite Selector */
3982 if (!wpa_cipher_valid_group(sm->group_cipher)) {
3983 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
3987 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3988 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3991 /* Pairwise Suite Count */
3992 wpabuf_put_le16(buf, 1);
3994 /* Pairwise Suite List */
3995 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
3996 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
3997 sm->pairwise_cipher);
4000 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4001 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4002 sm->pairwise_cipher));
4004 /* Authenticated Key Management Suite Count */
4005 wpabuf_put_le16(buf, 1);
4007 /* Authenticated Key Management Suite List */
4008 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4009 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
4010 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
4011 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
4012 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
4014 wpa_printf(MSG_WARNING,
4015 "FILS+FT: Invalid key management type (%d)",
4020 /* RSN Capabilities */
4022 #ifdef CONFIG_IEEE80211W
4023 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
4024 capab |= WPA_CAPABILITY_MFPC;
4025 #endif /* CONFIG_IEEE80211W */
4027 capab |= WPA_CAPABILITY_OCVC;
4028 wpabuf_put_le16(buf, capab);
4031 wpabuf_put_le16(buf, 1);
4033 /* PMKID List [PMKR1Name] */
4034 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
4035 sm->fils_ft, sm->fils_ft_len);
4036 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
4037 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
4038 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
4039 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4040 sm->r0kh_id, sm->r0kh_id_len);
4041 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
4042 sm->ssid_len, sm->mobility_domain,
4043 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
4044 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
4045 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
4048 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
4049 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
4050 sm->pmk_r0, sm->pmk_r0_len);
4051 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
4052 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
4053 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
4054 MAC2STR(sm->r1kh_id));
4055 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
4056 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
4057 sm->pmk_r1_name, use_sha384) < 0) {
4058 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
4061 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name,
4063 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
4065 #ifdef CONFIG_IEEE80211W
4066 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
4067 /* Management Group Cipher Suite */
4068 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4069 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
4071 #endif /* CONFIG_IEEE80211W */
4073 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
4076 #endif /* CONFIG_IEEE80211R */
4079 struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
4080 size_t *kek_len, const u8 **snonce,
4082 const struct wpabuf **hlp,
4083 unsigned int num_hlp)
4090 #ifdef CONFIG_IEEE80211R
4091 if (sm->fils_ft_ies)
4092 len += wpabuf_len(sm->fils_ft_ies);
4093 if (wpa_key_mgmt_ft(sm->key_mgmt))
4095 #endif /* CONFIG_IEEE80211R */
4096 for (i = 0; hlp && i < num_hlp; i++)
4097 len += 10 + wpabuf_len(hlp[i]);
4098 buf = wpabuf_alloc(len);
4102 #ifdef CONFIG_IEEE80211R
4103 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4104 /* MDE and FTE when using FILS+FT */
4105 wpabuf_put_buf(buf, sm->fils_ft_ies);
4106 /* RSNE with PMKR1Name in PMKID field */
4107 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
4112 #endif /* CONFIG_IEEE80211R */
4115 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4116 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4117 /* Element ID Extension */
4118 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4119 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4121 /* Everything after FILS Session element gets encrypted in the driver
4122 * with KEK. The buffer returned from here is the plaintext version. */
4124 /* TODO: FILS Public Key */
4126 /* FILS Key Confirm */
4127 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4128 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
4129 /* Element ID Extension */
4130 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
4131 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
4133 /* FILS HLP Container */
4134 for (i = 0; hlp && i < num_hlp; i++) {
4135 const u8 *pos = wpabuf_head(hlp[i]);
4136 size_t left = wpabuf_len(hlp[i]);
4138 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4143 wpabuf_put_u8(buf, len); /* Length */
4144 /* Element ID Extension */
4145 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
4146 /* Destination MAC Address, Source MAC Address, HLP Packet.
4147 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4148 * header when LPD is used). */
4149 wpabuf_put_data(buf, pos, len - 1);
4153 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4154 len = left > 255 ? 255 : left;
4155 wpabuf_put_u8(buf, len);
4156 wpabuf_put_data(buf, pos, len);
4162 /* TODO: FILS IP Address Assignment */
4165 if (wpa_sm_ocv_enabled(sm)) {
4166 struct wpa_channel_info ci;
4169 if (wpa_sm_channel_info(sm, &ci) != 0) {
4170 wpa_printf(MSG_WARNING,
4171 "FILS: Failed to get channel info for OCI element");
4176 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4177 if (ocv_insert_extended_oci(&ci, pos) < 0) {
4182 #endif /* CONFIG_OCV */
4184 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
4187 *kek_len = sm->ptk.kek_len;
4188 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
4189 *snonce = sm->fils_nonce;
4190 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
4191 *snonce, FILS_NONCE_LEN);
4192 *anonce = sm->fils_anonce;
4193 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
4194 *anonce, FILS_NONCE_LEN);
4200 static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4202 const u8 *pos, *end;
4204 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
4205 if (len < 2 * ETH_ALEN)
4207 pos = resp + 2 * ETH_ALEN;
4209 if (end - pos >= 6 &&
4210 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4211 pos += 6; /* Remove SNAP/LLC header */
4212 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
4216 static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
4219 const u8 *end = pos + len;
4222 /* Check if there are any FILS HLP Container elements */
4223 while (end - pos >= 2) {
4224 if (2 + pos[1] > end - pos)
4226 if (pos[0] == WLAN_EID_EXTENSION &&
4227 pos[1] >= 1 + 2 * ETH_ALEN &&
4228 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
4233 return; /* No FILS HLP Container elements */
4235 tmp = os_malloc(end - pos);
4239 while (end - pos >= 2) {
4240 if (2 + pos[1] > end - pos ||
4241 pos[0] != WLAN_EID_EXTENSION ||
4242 pos[1] < 1 + 2 * ETH_ALEN ||
4243 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4246 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4247 tmp_pos += pos[1] - 1;
4250 /* Add possible fragments */
4251 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4252 2 + pos[1] <= end - pos) {
4253 os_memcpy(tmp_pos, pos + 2, pos[1]);
4258 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4265 int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4267 const struct ieee80211_mgmt *mgmt;
4268 const u8 *end, *ie_start;
4269 struct ieee802_11_elems elems;
4272 struct wpa_gtk_data gd;
4274 struct wpa_eapol_ie_parse kde;
4276 if (!sm || !sm->ptk_set) {
4277 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4281 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4282 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4286 if (sm->fils_completed) {
4287 wpa_printf(MSG_DEBUG,
4288 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4292 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4295 mgmt = (const struct ieee80211_mgmt *) resp;
4296 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4300 /* Same offset for Association Response and Reassociation Response */
4301 ie_start = mgmt->u.assoc_resp.variable;
4303 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4305 wpa_printf(MSG_DEBUG,
4306 "FILS: Failed to parse decrypted elements");
4310 if (!elems.fils_session) {
4311 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4314 if (os_memcmp(elems.fils_session, sm->fils_session,
4315 FILS_SESSION_LEN) != 0) {
4316 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4317 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4318 elems.fils_session, FILS_SESSION_LEN);
4319 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4320 sm->fils_session, FILS_SESSION_LEN);
4323 if (!elems.rsn_ie) {
4324 wpa_printf(MSG_DEBUG,
4325 "FILS: No RSNE in (Re)Association Response");
4326 /* As an interop workaround, allow this for now since IEEE Std
4327 * 802.11ai-2016 did not include all the needed changes to make
4328 * a FILS AP include RSNE in the frame. This workaround might
4329 * eventually be removed and replaced with rejection (goto fail)
4330 * to follow a strict interpretation of the standard. */
4331 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
4332 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
4333 elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
4334 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4335 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
4336 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
4337 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
4338 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
4339 elems.rsn_ie, elems.rsn_ie_len);
4343 /* TODO: FILS Public Key */
4345 if (!elems.fils_key_confirm) {
4346 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4349 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4350 wpa_printf(MSG_DEBUG,
4351 "FILS: Unexpected Key-Auth length %d (expected %d)",
4352 elems.fils_key_confirm_len,
4353 (int) sm->fils_key_auth_len);
4356 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4357 sm->fils_key_auth_len) != 0) {
4358 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4359 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4360 elems.fils_key_confirm,
4361 elems.fils_key_confirm_len);
4362 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4363 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4368 if (wpa_sm_ocv_enabled(sm)) {
4369 struct wpa_channel_info ci;
4371 if (wpa_sm_channel_info(sm, &ci) != 0) {
4372 wpa_printf(MSG_WARNING,
4373 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4377 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
4378 channel_width_to_int(ci.chanwidth),
4379 ci.seg1_idx) != 0) {
4380 wpa_printf(MSG_WARNING, "FILS: %s", ocv_errorstr);
4384 #endif /* CONFIG_OCV */
4386 #ifdef CONFIG_IEEE80211R
4387 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4388 struct wpa_ie_data rsn;
4390 /* Check that PMKR1Name derived by the AP matches */
4391 if (!elems.rsn_ie ||
4392 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4394 !rsn.pmkid || rsn.num_pmkid != 1 ||
4395 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
4396 WPA_PMK_NAME_LEN) != 0) {
4397 wpa_printf(MSG_DEBUG,
4398 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
4402 #endif /* CONFIG_IEEE80211R */
4405 if (!elems.key_delivery) {
4406 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4410 /* Parse GTK and set the key to the driver */
4411 os_memset(&gd, 0, sizeof(gd));
4412 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4413 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4415 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4419 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4422 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4423 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
4424 gd.gtk_len, maxkeylen,
4425 &gd.key_rsc_len, &gd.alg))
4428 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
4429 gd.keyidx = kde.gtk[0] & 0x3;
4430 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
4431 !!(kde.gtk[0] & BIT(2)));
4432 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
4433 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
4434 (unsigned long) kde.gtk_len - 2);
4437 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
4439 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
4440 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
4441 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
4445 if (ieee80211w_set_keys(sm, &kde) < 0) {
4446 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
4450 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
4451 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
4452 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
4453 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
4454 keylen, (long unsigned int) sm->ptk.tk_len);
4457 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
4458 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
4459 sm->ptk.tk, keylen);
4460 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
4461 sm->ptk.tk, keylen) < 0) {
4462 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4463 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4465 alg, keylen, MAC2STR(sm->bssid));
4469 /* TODO: TK could be cleared after auth frame exchange now that driver
4470 * takes care of association frame encryption/decryption. */
4471 /* TK is not needed anymore in supplicant */
4472 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
4474 sm->ptk.installed = 1;
4476 /* FILS HLP Container */
4477 fils_process_hlp_container(sm, ie_start, end - ie_start);
4479 /* TODO: FILS IP Address Assignment */
4481 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
4482 sm->fils_completed = 1;
4483 forced_memzero(&gd, sizeof(gd));
4487 forced_memzero(&gd, sizeof(gd));
4492 void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
4495 sm->fils_completed = !!set;
4498 #endif /* CONFIG_FILS */
4501 int wpa_fils_is_completed(struct wpa_sm *sm)
4504 return sm && sm->fils_completed;
4505 #else /* CONFIG_FILS */
4507 #endif /* CONFIG_FILS */
4513 struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
4515 struct wpabuf *ie = NULL, *pub = NULL;
4520 else if (group == 20)
4522 else if (group == 21)
4527 crypto_ecdh_deinit(sm->owe_ecdh);
4528 sm->owe_ecdh = crypto_ecdh_init(group);
4531 sm->owe_group = group;
4532 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4533 pub = wpabuf_zeropad(pub, prime_len);
4537 ie = wpabuf_alloc(5 + wpabuf_len(pub));
4540 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
4541 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
4542 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
4543 wpabuf_put_le16(ie, group);
4544 wpabuf_put_buf(ie, pub);
4546 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
4552 crypto_ecdh_deinit(sm->owe_ecdh);
4553 sm->owe_ecdh = NULL;
4558 int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
4559 const u8 *resp_ies, size_t resp_ies_len)
4561 struct ieee802_11_elems elems;
4563 struct wpabuf *secret, *pub, *hkey;
4565 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
4566 const char *info = "OWE Key Generation";
4569 size_t hash_len, prime_len;
4570 struct wpa_ie_data data;
4573 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
4575 wpa_printf(MSG_INFO,
4576 "OWE: Could not parse Association Response frame elements");
4580 if (sm->cur_pmksa && elems.rsn_ie &&
4581 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
4583 data.num_pmkid == 1 && data.pmkid &&
4584 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
4585 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
4586 wpa_sm_set_pmk_from_pmksa(sm);
4590 if (!elems.owe_dh) {
4591 wpa_printf(MSG_INFO,
4592 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
4596 group = WPA_GET_LE16(elems.owe_dh);
4597 if (group != sm->owe_group) {
4598 wpa_printf(MSG_INFO,
4599 "OWE: Unexpected Diffie-Hellman group in response: %u",
4604 if (!sm->owe_ecdh) {
4605 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
4611 else if (group == 20)
4613 else if (group == 21)
4618 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
4620 elems.owe_dh_len - 2);
4621 secret = wpabuf_zeropad(secret, prime_len);
4623 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
4626 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
4628 /* prk = HKDF-extract(C | A | group, z) */
4630 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4632 wpabuf_clear_free(secret);
4636 /* PMKID = Truncate-128(Hash(C | A)) */
4637 addr[0] = wpabuf_head(pub);
4638 len[0] = wpabuf_len(pub);
4639 addr[1] = elems.owe_dh + 2;
4640 len[1] = elems.owe_dh_len - 2;
4642 res = sha256_vector(2, addr, len, pmkid);
4643 hash_len = SHA256_MAC_LEN;
4644 } else if (group == 20) {
4645 res = sha384_vector(2, addr, len, pmkid);
4646 hash_len = SHA384_MAC_LEN;
4647 } else if (group == 21) {
4648 res = sha512_vector(2, addr, len, pmkid);
4649 hash_len = SHA512_MAC_LEN;
4654 pub = wpabuf_zeropad(pub, prime_len);
4655 if (res < 0 || !pub) {
4657 wpabuf_clear_free(secret);
4661 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
4664 wpabuf_clear_free(secret);
4668 wpabuf_put_buf(hkey, pub); /* C */
4670 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
4671 wpabuf_put_le16(hkey, sm->owe_group); /* group */
4673 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
4674 wpabuf_head(secret), wpabuf_len(secret), prk);
4675 else if (group == 20)
4676 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
4677 wpabuf_head(secret), wpabuf_len(secret), prk);
4678 else if (group == 21)
4679 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
4680 wpabuf_head(secret), wpabuf_len(secret), prk);
4681 wpabuf_clear_free(hkey);
4682 wpabuf_clear_free(secret);
4686 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
4688 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
4691 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
4692 os_strlen(info), sm->pmk, hash_len);
4693 else if (group == 20)
4694 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
4695 os_strlen(info), sm->pmk, hash_len);
4696 else if (group == 21)
4697 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
4698 os_strlen(info), sm->pmk, hash_len);
4699 forced_memzero(prk, SHA512_MAC_LEN);
4704 sm->pmk_len = hash_len;
4706 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
4707 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
4708 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
4709 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
4715 #endif /* CONFIG_OWE */
4718 void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
4721 if (sm && fils_cache_id) {
4722 sm->fils_cache_id_set = 1;
4723 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
4725 #endif /* CONFIG_FILS */
4730 void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
4733 wpabuf_clear_free(sm->dpp_z);
4734 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
4737 #endif /* CONFIG_DPP2 */