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) {
309 if (wpa_key_mgmt_sha384(sm->key_mgmt))
310 pmk_len = PMK_LEN_SUITE_B_192;
313 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
315 if (pmk_len == PMK_LEN) {
317 * EAP-LEAP is an exception from other EAP
318 * methods: it uses only 16-byte PMK.
320 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
324 #ifdef CONFIG_IEEE80211R
326 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
328 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
329 os_memcpy(sm->xxkey, buf,
331 sm->xxkey_len = SHA384_MAC_LEN;
333 os_memcpy(sm->xxkey, buf + PMK_LEN,
335 sm->xxkey_len = PMK_LEN;
337 os_memset(buf, 0, sizeof(buf));
339 #endif /* CONFIG_IEEE80211R */
342 struct rsn_pmksa_cache_entry *sa = NULL;
343 const u8 *fils_cache_id = NULL;
346 if (sm->fils_cache_id_set)
347 fils_cache_id = sm->fils_cache_id;
348 #endif /* CONFIG_FILS */
350 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
351 "machines", sm->pmk, pmk_len);
352 sm->pmk_len = pmk_len;
353 wpa_supplicant_key_mgmt_set_pmk(sm);
354 if (sm->proto == WPA_PROTO_RSN &&
355 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
356 !wpa_key_mgmt_ft(sm->key_mgmt)) {
357 sa = pmksa_cache_add(sm->pmksa,
358 sm->pmk, pmk_len, NULL,
360 src_addr, sm->own_addr,
365 if (!sm->cur_pmksa && pmkid &&
366 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL,
368 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
369 "RSN: the new PMK matches with the "
372 } else if (sa && !sm->cur_pmksa && pmkid) {
374 * It looks like the authentication server
375 * derived mismatching MSK. This should not
376 * really happen, but bugs happen.. There is not
377 * much we can do here without knowing what
378 * exactly caused the server to misbehave.
380 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
381 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
387 #ifdef CONFIG_IEEE80211R
388 } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) {
389 wpa_printf(MSG_DEBUG,
390 "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
391 #endif /* CONFIG_IEEE80211R */
393 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
394 "WPA: Failed to get master session key from "
395 "EAPOL state machines - key handshake "
398 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
399 "RSN: Cancelled PMKSA caching "
401 sm->cur_pmksa = NULL;
403 } else if (!abort_cached) {
409 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
410 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
411 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
413 /* Send EAPOL-Start to trigger full EAP authentication. */
417 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
418 "RSN: no PMKSA entry found - trigger "
419 "full EAP authentication");
420 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
421 NULL, 0, &buflen, NULL);
423 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
437 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
438 * @sm: Pointer to WPA state machine data from wpa_sm_init()
439 * @dst: Destination address for the frame
440 * @key: Pointer to the EAPOL-Key frame header
441 * @ver: Version bits from EAPOL-Key Key Info
442 * @nonce: Nonce value for the EAPOL-Key frame
443 * @wpa_ie: WPA/RSN IE
444 * @wpa_ie_len: Length of the WPA/RSN IE
445 * @ptk: PTK to use for keyed hash and encryption
446 * Returns: >= 0 on success, < 0 on failure
448 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
449 const struct wpa_eapol_key *key,
450 int ver, const u8 *nonce,
451 const u8 *wpa_ie, size_t wpa_ie_len,
454 size_t mic_len, hdrlen, rlen;
455 struct wpa_eapol_key *reply;
457 u8 *rsn_ie_buf = NULL;
460 if (wpa_ie == NULL) {
461 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
462 "cannot generate msg 2/4");
466 #ifdef CONFIG_IEEE80211R
467 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
471 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
472 * FTIE from (Re)Association Response.
474 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
475 sm->assoc_resp_ies_len);
476 if (rsn_ie_buf == NULL)
478 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
479 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
486 if (sm->assoc_resp_ies) {
487 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
488 sm->assoc_resp_ies_len);
489 wpa_ie_len += sm->assoc_resp_ies_len;
494 #endif /* CONFIG_IEEE80211R */
496 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
498 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
499 hdrlen = sizeof(*reply) + mic_len + 2;
500 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
501 NULL, hdrlen + wpa_ie_len,
502 &rlen, (void *) &reply);
508 reply->type = (sm->proto == WPA_PROTO_RSN ||
509 sm->proto == WPA_PROTO_OSEN) ?
510 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
511 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
513 key_info |= WPA_KEY_INFO_MIC;
515 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
516 WPA_PUT_BE16(reply->key_info, key_info);
517 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
518 WPA_PUT_BE16(reply->key_length, 0);
520 os_memcpy(reply->key_length, key->key_length, 2);
521 os_memcpy(reply->replay_counter, key->replay_counter,
522 WPA_REPLAY_COUNTER_LEN);
523 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
524 WPA_REPLAY_COUNTER_LEN);
526 key_mic = (u8 *) (reply + 1);
527 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
528 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
531 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
533 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
534 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
539 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
540 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
545 #ifdef CONFIG_IEEE80211R
546 if (wpa_key_mgmt_ft(sm->key_mgmt))
547 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
548 #endif /* CONFIG_IEEE80211R */
551 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
552 z = wpabuf_head(sm->dpp_z);
553 z_len = wpabuf_len(sm->dpp_z);
555 #endif /* CONFIG_DPP2 */
557 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
558 sm->own_addr, sm->bssid, sm->snonce,
559 key->key_nonce, ptk, sm->key_mgmt,
560 sm->pairwise_cipher, z, z_len);
564 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
565 const unsigned char *src_addr,
566 const struct wpa_eapol_key *key,
567 u16 ver, const u8 *key_data,
570 struct wpa_eapol_ie_parse ie;
573 u8 *kde, *kde_buf = NULL;
576 if (wpa_sm_get_network_ctx(sm) == NULL) {
577 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
578 "found (msg 1 of 4)");
582 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
583 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
584 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
586 os_memset(&ie, 0, sizeof(ie));
588 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
589 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
590 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
591 key_data, key_data_len);
592 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
595 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
596 "Authenticator", ie.pmkid, PMKID_LEN);
600 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
602 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
603 "msg 1/4 - requesting full EAP authentication");
609 if (sm->renew_snonce) {
610 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
611 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
612 "WPA: Failed to get random data for SNonce");
615 sm->renew_snonce = 0;
616 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
617 sm->snonce, WPA_NONCE_LEN);
620 /* Calculate PTK which will be stored as a temporary PTK until it has
621 * been verified when processing message 3/4. */
623 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
625 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
627 /* Supplicant: swap tx/rx Mic keys */
628 os_memcpy(buf, &ptk->tk[16], 8);
629 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
630 os_memcpy(&ptk->tk[24], buf, 8);
631 os_memset(buf, 0, sizeof(buf));
635 kde = sm->assoc_wpa_ie;
636 kde_len = sm->assoc_wpa_ie_len;
639 if (wpa_sm_ocv_enabled(sm)) {
640 struct wpa_channel_info ci;
643 if (wpa_sm_channel_info(sm, &ci) != 0) {
644 wpa_printf(MSG_WARNING,
645 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
649 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 3);
651 wpa_printf(MSG_WARNING,
652 "Failed to allocate memory for KDE with OCI in EAPOL-Key 2/4");
656 os_memcpy(kde_buf, kde, kde_len);
659 if (ocv_insert_oci_kde(&ci, &pos) < 0)
663 #endif /* CONFIG_OCV */
667 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
670 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
671 "into EAPOL-Key 2/4");
672 os_memcpy(kde_buf, kde, kde_len);
675 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
676 *pos++ = RSN_SELECTOR_LEN + 1;
677 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
678 pos += RSN_SELECTOR_LEN;
683 #endif /* CONFIG_P2P */
685 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
686 kde, kde_len, ptk) < 0)
690 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
695 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
699 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
701 struct wpa_sm *sm = eloop_ctx;
702 rsn_preauth_candidate_process(sm);
706 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
707 const u8 *addr, int secure)
709 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
710 "WPA: Key negotiation completed with "
711 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
712 wpa_cipher_txt(sm->pairwise_cipher),
713 wpa_cipher_txt(sm->group_cipher));
714 wpa_sm_cancel_auth_timeout(sm);
715 wpa_sm_set_state(sm, WPA_COMPLETED);
718 wpa_sm_mlme_setprotection(
719 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
720 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
721 eapol_sm_notify_portValid(sm->eapol, TRUE);
722 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
723 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
724 sm->key_mgmt == WPA_KEY_MGMT_OWE)
725 eapol_sm_notify_eap_success(sm->eapol, TRUE);
727 * Start preauthentication after a short wait to avoid a
728 * possible race condition between the data receive and key
729 * configuration after the 4-Way Handshake. This increases the
730 * likelihood of the first preauth EAPOL-Start frame getting to
733 if (!dl_list_empty(&sm->pmksa_candidates))
734 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
738 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
739 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
740 "RSN: Authenticator accepted "
741 "opportunistic PMKSA entry - marking it valid");
742 sm->cur_pmksa->opportunistic = 0;
745 #ifdef CONFIG_IEEE80211R
746 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
747 /* Prepare for the next transition */
748 wpa_ft_prepare_auth_request(sm, NULL);
750 #endif /* CONFIG_IEEE80211R */
754 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
756 struct wpa_sm *sm = eloop_ctx;
757 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
758 wpa_sm_key_request(sm, 0, 1);
762 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
763 const struct wpa_eapol_key *key)
769 if (sm->ptk.installed) {
770 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
771 "WPA: Do not re-install same PTK to the driver");
775 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
776 "WPA: Installing PTK to the driver");
778 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
779 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
780 "Suite: NONE - do not use pairwise keys");
784 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
785 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
786 "WPA: Unsupported pairwise cipher %d",
787 sm->pairwise_cipher);
791 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
792 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
793 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
794 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
795 keylen, (long unsigned int) sm->ptk.tk_len);
798 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
800 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
803 key_rsc = key->key_rsc;
804 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
807 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
808 sm->ptk.tk, keylen) < 0) {
809 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
810 "WPA: Failed to set PTK to the "
811 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
812 alg, keylen, MAC2STR(sm->bssid));
816 /* TK is not needed anymore in supplicant */
817 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
819 sm->ptk.installed = 1;
821 if (sm->wpa_ptk_rekey) {
822 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
823 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
831 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
833 int keylen, int maxkeylen,
839 *alg = wpa_cipher_to_alg(group_cipher);
840 if (*alg == WPA_ALG_NONE) {
841 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
842 "WPA: Unsupported Group Cipher %d",
846 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
848 klen = wpa_cipher_key_len(group_cipher);
849 if (keylen != klen || maxkeylen < klen) {
850 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
851 "WPA: Unsupported %s Group Cipher key length %d (%d)",
852 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
859 struct wpa_gtk_data {
861 int tx, key_rsc_len, keyidx;
867 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
868 const struct wpa_gtk_data *gd,
869 const u8 *key_rsc, int wnm_sleep)
871 const u8 *_gtk = gd->gtk;
874 /* Detect possible key reinstallation */
875 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
876 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
877 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
878 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
879 sm->gtk_wnm_sleep.gtk_len) == 0)) {
880 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
881 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
882 gd->keyidx, gd->tx, gd->gtk_len);
886 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
887 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
888 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
889 gd->keyidx, gd->tx, gd->gtk_len);
890 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
891 if (sm->group_cipher == WPA_CIPHER_TKIP) {
892 /* Swap Tx/Rx keys for Michael MIC */
893 os_memcpy(gtk_buf, gd->gtk, 16);
894 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
895 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
898 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
899 if (wpa_sm_set_key(sm, gd->alg, NULL,
900 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
901 _gtk, gd->gtk_len) < 0) {
902 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
903 "WPA: Failed to set GTK to the driver "
905 os_memset(gtk_buf, 0, sizeof(gtk_buf));
908 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
909 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
910 _gtk, gd->gtk_len) < 0) {
911 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
912 "WPA: Failed to set GTK to "
913 "the driver (alg=%d keylen=%d keyidx=%d)",
914 gd->alg, gd->gtk_len, gd->keyidx);
915 os_memset(gtk_buf, 0, sizeof(gtk_buf));
918 os_memset(gtk_buf, 0, sizeof(gtk_buf));
921 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
922 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
923 sm->gtk_wnm_sleep.gtk_len);
925 sm->gtk.gtk_len = gd->gtk_len;
926 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
933 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
936 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
937 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
938 * seemed to set this bit (incorrectly, since Tx is only when
939 * doing Group Key only APs) and without this workaround, the
940 * data connection does not work because wpa_supplicant
941 * configured non-zero keyidx to be used for unicast. */
942 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
943 "WPA: Tx bit set for GTK, but pairwise "
944 "keys are used - ignore Tx bit");
951 static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
956 if (!sm->wpa_rsc_relaxation)
959 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
962 * Try to detect RSC (endian) corruption issue where the AP sends
963 * the RSC bytes in EAPOL-Key message in the wrong order, both if
964 * it's actually a 6-byte field (as it should be) and if it treats
965 * it as an 8-byte field.
966 * An AP model known to have this bug is the Sapido RB-1632.
968 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
969 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
970 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
971 rsc[0], rsc[1], rsc[2], rsc[3],
972 rsc[4], rsc[5], rsc[6], rsc[7]);
981 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
982 const struct wpa_eapol_key *key,
983 const u8 *gtk, size_t gtk_len,
986 struct wpa_gtk_data gd;
990 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
992 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
993 * Reserved [bits 0-7]
997 os_memset(&gd, 0, sizeof(gd));
998 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1001 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1004 gd.keyidx = gtk[0] & 0x3;
1005 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1006 !!(gtk[0] & BIT(2)));
1010 os_memcpy(gd.gtk, gtk, gtk_len);
1011 gd.gtk_len = gtk_len;
1013 key_rsc = key->key_rsc;
1014 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1017 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1018 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1020 &gd.key_rsc_len, &gd.alg) ||
1021 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
1022 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1023 "RSN: Failed to install GTK");
1024 os_memset(&gd, 0, sizeof(gd));
1027 os_memset(&gd, 0, sizeof(gd));
1033 #ifdef CONFIG_IEEE80211W
1034 static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
1035 const struct wpa_igtk_kde *igtk,
1038 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1039 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1041 /* Detect possible key reinstallation */
1042 if ((sm->igtk.igtk_len == len &&
1043 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1044 (sm->igtk_wnm_sleep.igtk_len == len &&
1045 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1046 sm->igtk_wnm_sleep.igtk_len) == 0)) {
1047 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1048 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1053 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1054 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
1055 keyidx, MAC2STR(igtk->pn));
1056 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1057 if (keyidx > 4095) {
1058 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1059 "WPA: Invalid IGTK KeyID %d", keyidx);
1062 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1063 broadcast_ether_addr,
1064 keyidx, 0, igtk->pn, sizeof(igtk->pn),
1065 igtk->igtk, len) < 0) {
1066 if (keyidx == 0x0400 || keyidx == 0x0500) {
1067 /* Assume the AP has broken PMF implementation since it
1068 * seems to have swapped the KeyID bytes. The AP cannot
1069 * be trusted to implement BIP correctly or provide a
1070 * valid IGTK, so do not try to configure this key with
1071 * swapped KeyID bytes. Instead, continue without
1072 * configuring the IGTK so that the driver can drop any
1073 * received group-addressed robust management frames due
1076 * Normally, this error behavior would result in us
1077 * disconnecting, but there are number of deployed APs
1078 * with this broken behavior, so as an interoperability
1079 * workaround, allow the connection to proceed. */
1080 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1081 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1083 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1084 "WPA: Failed to configure IGTK to the driver");
1090 sm->igtk_wnm_sleep.igtk_len = len;
1091 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1092 sm->igtk_wnm_sleep.igtk_len);
1094 sm->igtk.igtk_len = len;
1095 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1100 #endif /* CONFIG_IEEE80211W */
1103 static int ieee80211w_set_keys(struct wpa_sm *sm,
1104 struct wpa_eapol_ie_parse *ie)
1106 #ifdef CONFIG_IEEE80211W
1107 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
1112 const struct wpa_igtk_kde *igtk;
1114 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1115 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
1118 igtk = (const struct wpa_igtk_kde *) ie->igtk;
1119 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
1124 #else /* CONFIG_IEEE80211W */
1126 #endif /* CONFIG_IEEE80211W */
1130 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1131 const char *reason, const u8 *src_addr,
1132 const u8 *wpa_ie, size_t wpa_ie_len,
1133 const u8 *rsn_ie, size_t rsn_ie_len)
1135 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1136 reason, MAC2STR(src_addr));
1138 if (sm->ap_wpa_ie) {
1139 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1140 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1143 if (!sm->ap_wpa_ie) {
1144 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1145 "WPA: No WPA IE in Beacon/ProbeResp");
1147 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1148 wpa_ie, wpa_ie_len);
1151 if (sm->ap_rsn_ie) {
1152 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1153 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1156 if (!sm->ap_rsn_ie) {
1157 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1158 "WPA: No RSN IE in Beacon/ProbeResp");
1160 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1161 rsn_ie, rsn_ie_len);
1164 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
1168 #ifdef CONFIG_IEEE80211R
1170 static int ft_validate_mdie(struct wpa_sm *sm,
1171 const unsigned char *src_addr,
1172 struct wpa_eapol_ie_parse *ie,
1173 const u8 *assoc_resp_mdie)
1175 struct rsn_mdie *mdie;
1177 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1178 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1179 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1180 MOBILITY_DOMAIN_ID_LEN) != 0) {
1181 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1182 "not match with the current mobility domain");
1186 if (assoc_resp_mdie &&
1187 (assoc_resp_mdie[1] != ie->mdie[1] ||
1188 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1189 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1190 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1191 ie->mdie, 2 + ie->mdie[1]);
1192 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1193 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1201 static int ft_validate_ftie(struct wpa_sm *sm,
1202 const unsigned char *src_addr,
1203 struct wpa_eapol_ie_parse *ie,
1204 const u8 *assoc_resp_ftie)
1206 if (ie->ftie == NULL) {
1207 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1208 "FT: No FTIE in EAPOL-Key msg 3/4");
1212 if (assoc_resp_ftie == NULL)
1215 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1216 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1217 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1218 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1219 ie->ftie, 2 + ie->ftie[1]);
1220 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1221 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1229 static int ft_validate_rsnie(struct wpa_sm *sm,
1230 const unsigned char *src_addr,
1231 struct wpa_eapol_ie_parse *ie)
1233 struct wpa_ie_data rsn;
1239 * Verify that PMKR1Name from EAPOL-Key message 3/4
1240 * matches with the value we derived.
1242 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1243 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1244 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1245 "FT 4-way handshake message 3/4");
1249 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1251 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1252 "FT: PMKR1Name mismatch in "
1253 "FT 4-way handshake message 3/4");
1254 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1255 rsn.pmkid, WPA_PMK_NAME_LEN);
1256 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1257 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1265 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1266 const unsigned char *src_addr,
1267 struct wpa_eapol_ie_parse *ie)
1269 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1271 if (sm->assoc_resp_ies) {
1272 pos = sm->assoc_resp_ies;
1273 end = pos + sm->assoc_resp_ies_len;
1274 while (end - pos > 2) {
1275 if (2 + pos[1] > end - pos)
1278 case WLAN_EID_MOBILITY_DOMAIN:
1281 case WLAN_EID_FAST_BSS_TRANSITION:
1289 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1290 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1291 ft_validate_rsnie(sm, src_addr, ie) < 0)
1297 #endif /* CONFIG_IEEE80211R */
1300 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1301 const unsigned char *src_addr,
1302 struct wpa_eapol_ie_parse *ie)
1304 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1305 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1306 "WPA: No WPA/RSN IE for this AP known. "
1307 "Trying to get from scan results");
1308 if (wpa_sm_get_beacon_ie(sm) < 0) {
1309 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1310 "WPA: Could not find AP from "
1311 "the scan results");
1313 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1314 "WPA: Found the current AP from "
1315 "updated scan results");
1319 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1320 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1321 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1322 "with IE in Beacon/ProbeResp (no IE?)",
1323 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1324 ie->rsn_ie, ie->rsn_ie_len);
1328 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1329 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1330 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1331 (ie->rsn_ie && sm->ap_rsn_ie &&
1332 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1333 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1334 ie->rsn_ie, ie->rsn_ie_len))) {
1335 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1336 "with IE in Beacon/ProbeResp",
1337 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1338 ie->rsn_ie, ie->rsn_ie_len);
1342 if (sm->proto == WPA_PROTO_WPA &&
1343 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1344 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1345 "detected - RSN was enabled and RSN IE "
1346 "was in msg 3/4, but not in "
1348 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1349 ie->rsn_ie, ie->rsn_ie_len);
1353 #ifdef CONFIG_IEEE80211R
1354 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1355 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1357 #endif /* CONFIG_IEEE80211R */
1364 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1365 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1366 * @dst: Destination address for the frame
1367 * @key: Pointer to the EAPOL-Key frame header
1368 * @ver: Version bits from EAPOL-Key Key Info
1369 * @key_info: Key Info
1370 * @ptk: PTK to use for keyed hash and encryption
1371 * Returns: >= 0 on success, < 0 on failure
1373 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1374 const struct wpa_eapol_key *key,
1375 u16 ver, u16 key_info,
1376 struct wpa_ptk *ptk)
1378 size_t mic_len, hdrlen, rlen;
1379 struct wpa_eapol_key *reply;
1382 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
1383 hdrlen = sizeof(*reply) + mic_len + 2;
1384 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1385 hdrlen, &rlen, (void *) &reply);
1389 reply->type = (sm->proto == WPA_PROTO_RSN ||
1390 sm->proto == WPA_PROTO_OSEN) ?
1391 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1392 key_info &= WPA_KEY_INFO_SECURE;
1393 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1395 key_info |= WPA_KEY_INFO_MIC;
1397 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1398 WPA_PUT_BE16(reply->key_info, key_info);
1399 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1400 WPA_PUT_BE16(reply->key_length, 0);
1402 os_memcpy(reply->key_length, key->key_length, 2);
1403 os_memcpy(reply->replay_counter, key->replay_counter,
1404 WPA_REPLAY_COUNTER_LEN);
1406 key_mic = (u8 *) (reply + 1);
1407 WPA_PUT_BE16(key_mic + mic_len, 0);
1409 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1410 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1415 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1416 const struct wpa_eapol_key *key,
1417 u16 ver, const u8 *key_data,
1418 size_t key_data_len)
1420 u16 key_info, keylen;
1421 struct wpa_eapol_ie_parse ie;
1423 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1424 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1425 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1427 key_info = WPA_GET_BE16(key->key_info);
1429 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1430 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
1432 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1433 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1434 "WPA: GTK IE in unencrypted key data");
1437 #ifdef CONFIG_IEEE80211W
1438 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1439 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1440 "WPA: IGTK KDE in unencrypted key data");
1445 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1446 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1447 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
1448 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1449 "WPA: Invalid IGTK KDE length %lu",
1450 (unsigned long) ie.igtk_len);
1453 #endif /* CONFIG_IEEE80211W */
1455 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1458 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1459 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1460 "WPA: ANonce from message 1 of 4-Way Handshake "
1461 "differs from 3 of 4-Way Handshake - drop packet (src="
1462 MACSTR ")", MAC2STR(sm->bssid));
1466 keylen = WPA_GET_BE16(key->key_length);
1467 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1468 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1469 "WPA: Invalid %s key length %d (src=" MACSTR
1470 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1471 MAC2STR(sm->bssid));
1476 if (ie.ip_addr_alloc) {
1477 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1478 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1479 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1481 #endif /* CONFIG_P2P */
1484 if (wpa_sm_ocv_enabled(sm)) {
1485 struct wpa_channel_info ci;
1487 if (wpa_sm_channel_info(sm, &ci) != 0) {
1488 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1489 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1493 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1494 channel_width_to_int(ci.chanwidth),
1495 ci.seg1_idx) != 0) {
1496 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1501 #endif /* CONFIG_OCV */
1503 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1508 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1509 * for the next 4-Way Handshake. If msg 3 is received again, the old
1510 * SNonce will still be used to avoid changing PTK. */
1511 sm->renew_snonce = 1;
1513 if (key_info & WPA_KEY_INFO_INSTALL) {
1514 if (wpa_supplicant_install_ptk(sm, key))
1518 if (key_info & WPA_KEY_INFO_SECURE) {
1519 wpa_sm_mlme_setprotection(
1520 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1521 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1522 eapol_sm_notify_portValid(sm->eapol, TRUE);
1524 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1526 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1527 /* No GTK to be set to the driver */
1528 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
1529 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1530 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
1532 } else if (ie.gtk &&
1533 wpa_supplicant_pairwise_gtk(sm, key,
1534 ie.gtk, ie.gtk_len, key_info) < 0) {
1535 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1536 "RSN: Failed to configure GTK");
1540 if (ieee80211w_set_keys(sm, &ie) < 0) {
1541 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1542 "RSN: Failed to configure IGTK");
1546 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
1547 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1548 key_info & WPA_KEY_INFO_SECURE);
1551 wpa_sm_set_rekey_offload(sm);
1553 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1554 * calculated only after KCK has been derived. Though, do not replace an
1555 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1556 * to avoid unnecessary changes of PMKID while continuing to use the
1558 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1560 struct rsn_pmksa_cache_entry *sa;
1562 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
1563 sm->ptk.kck, sm->ptk.kck_len,
1564 sm->bssid, sm->own_addr,
1565 sm->network_ctx, sm->key_mgmt, NULL);
1570 sm->msg_3_of_4_ok = 1;
1574 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1578 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1582 struct wpa_gtk_data *gd)
1585 struct wpa_eapol_ie_parse ie;
1587 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1588 keydata, keydatalen);
1589 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1591 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1592 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1593 "WPA: GTK IE in unencrypted key data");
1596 if (ie.gtk == NULL) {
1597 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1598 "WPA: No GTK IE in Group Key msg 1/2");
1601 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1604 if (wpa_sm_ocv_enabled(sm)) {
1605 struct wpa_channel_info ci;
1607 if (wpa_sm_channel_info(sm, &ci) != 0) {
1608 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1609 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1613 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1614 channel_width_to_int(ci.chanwidth),
1615 ci.seg1_idx) != 0) {
1616 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1621 #endif /* CONFIG_OCV */
1623 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1624 gd->gtk_len, maxkeylen,
1625 &gd->key_rsc_len, &gd->alg))
1628 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1629 ie.gtk, ie.gtk_len);
1630 gd->keyidx = ie.gtk[0] & 0x3;
1631 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1632 !!(ie.gtk[0] & BIT(2)));
1633 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1634 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1635 "RSN: Too long GTK in GTK IE (len=%lu)",
1636 (unsigned long) ie.gtk_len - 2);
1639 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1641 if (ieee80211w_set_keys(sm, &ie) < 0)
1642 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1643 "RSN: Failed to configure IGTK");
1649 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1650 const struct wpa_eapol_key *key,
1652 size_t key_data_len, u16 key_info,
1653 u16 ver, struct wpa_gtk_data *gd)
1658 gtk_len = WPA_GET_BE16(key->key_length);
1659 maxkeylen = key_data_len;
1660 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1661 if (maxkeylen < 8) {
1662 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1663 "WPA: Too short maxkeylen (%lu)",
1664 (unsigned long) maxkeylen);
1670 if (gtk_len > maxkeylen ||
1671 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1673 &gd->key_rsc_len, &gd->alg))
1676 gd->gtk_len = gtk_len;
1677 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1678 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1679 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1680 #ifdef CONFIG_NO_RC4
1681 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1682 "WPA: RC4 not supported in the build");
1684 #else /* CONFIG_NO_RC4 */
1686 if (key_data_len > sizeof(gd->gtk)) {
1687 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1688 "WPA: RC4 key data too long (%lu)",
1689 (unsigned long) key_data_len);
1692 os_memcpy(ek, key->key_iv, 16);
1693 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1694 os_memcpy(gd->gtk, key_data, key_data_len);
1695 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
1696 os_memset(ek, 0, sizeof(ek));
1697 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1701 os_memset(ek, 0, sizeof(ek));
1702 #endif /* CONFIG_NO_RC4 */
1703 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1704 if (maxkeylen % 8) {
1705 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1706 "WPA: Unsupported AES-WRAP len %lu",
1707 (unsigned long) maxkeylen);
1710 if (maxkeylen > sizeof(gd->gtk)) {
1711 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1712 "WPA: AES-WRAP key data "
1713 "too long (keydatalen=%lu maxkeylen=%lu)",
1714 (unsigned long) key_data_len,
1715 (unsigned long) maxkeylen);
1718 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1719 key_data, gd->gtk)) {
1720 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1721 "WPA: AES unwrap failed - could not decrypt "
1726 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1727 "WPA: Unsupported key_info type %d", ver);
1730 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1731 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1736 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1737 const struct wpa_eapol_key *key,
1738 int ver, u16 key_info)
1740 size_t mic_len, hdrlen, rlen;
1741 struct wpa_eapol_key *reply;
1746 if (wpa_sm_ocv_enabled(sm))
1747 kde_len = OCV_OCI_KDE_LEN;
1748 #endif /* CONFIG_OCV */
1750 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
1751 hdrlen = sizeof(*reply) + mic_len + 2;
1752 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1753 hdrlen + kde_len, &rlen, (void *) &reply);
1757 reply->type = (sm->proto == WPA_PROTO_RSN ||
1758 sm->proto == WPA_PROTO_OSEN) ?
1759 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1760 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1761 key_info |= ver | WPA_KEY_INFO_SECURE;
1763 key_info |= WPA_KEY_INFO_MIC;
1765 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1766 WPA_PUT_BE16(reply->key_info, key_info);
1767 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1768 WPA_PUT_BE16(reply->key_length, 0);
1770 os_memcpy(reply->key_length, key->key_length, 2);
1771 os_memcpy(reply->replay_counter, key->replay_counter,
1772 WPA_REPLAY_COUNTER_LEN);
1774 key_mic = (u8 *) (reply + 1);
1775 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
1778 if (wpa_sm_ocv_enabled(sm)) {
1779 struct wpa_channel_info ci;
1782 if (wpa_sm_channel_info(sm, &ci) != 0) {
1783 wpa_printf(MSG_WARNING,
1784 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
1789 pos = key_mic + mic_len + 2; /* Key Data */
1790 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
1795 #endif /* CONFIG_OCV */
1797 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1798 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
1799 rbuf, rlen, key_mic);
1803 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1804 const unsigned char *src_addr,
1805 const struct wpa_eapol_key *key,
1807 size_t key_data_len, u16 ver)
1811 struct wpa_gtk_data gd;
1814 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
1815 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1816 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1820 os_memset(&gd, 0, sizeof(gd));
1822 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1823 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1824 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1826 key_info = WPA_GET_BE16(key->key_info);
1828 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
1829 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1830 key_data_len, key_info,
1833 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1835 key_info, ver, &gd);
1838 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1843 key_rsc = key->key_rsc;
1844 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1847 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
1848 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
1850 os_memset(&gd, 0, sizeof(gd));
1853 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1854 "completed with " MACSTR " [GTK=%s]",
1855 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1856 wpa_sm_cancel_auth_timeout(sm);
1857 wpa_sm_set_state(sm, WPA_COMPLETED);
1859 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1861 WPA_KEY_INFO_SECURE);
1864 wpa_sm_set_rekey_offload(sm);
1869 os_memset(&gd, 0, sizeof(gd));
1870 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1874 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1875 struct wpa_eapol_key *key,
1877 const u8 *buf, size_t len)
1879 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
1881 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
1883 os_memcpy(mic, key + 1, mic_len);
1885 os_memset(key + 1, 0, mic_len);
1886 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
1888 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1889 os_memcmp_const(mic, key + 1, mic_len) != 0) {
1890 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1891 "WPA: Invalid EAPOL-Key MIC "
1892 "when using TPTK - ignoring TPTK");
1894 wpa_printf(MSG_INFO,
1895 "TEST: Ignore Key MIC failure for fuzz testing");
1897 #endif /* TEST_FUZZ */
1901 #endif /* TEST_FUZZ */
1905 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1906 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1908 * This assures the same TPTK in sm->tptk can never be
1909 * copied twice to sm->ptk as the new PTK. In
1910 * combination with the installed flag in the wpa_ptk
1911 * struct, this assures the same PTK is only installed
1914 sm->renew_snonce = 1;
1918 if (!ok && sm->ptk_set) {
1919 os_memset(key + 1, 0, mic_len);
1920 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
1922 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1923 os_memcmp_const(mic, key + 1, mic_len) != 0) {
1924 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1925 "WPA: Invalid EAPOL-Key MIC - "
1928 wpa_printf(MSG_INFO,
1929 "TEST: Ignore Key MIC failure for fuzz testing");
1930 goto continue_fuzz2;
1931 #endif /* TEST_FUZZ */
1936 #endif /* TEST_FUZZ */
1941 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1942 "WPA: Could not verify EAPOL-Key MIC - "
1947 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1948 WPA_REPLAY_COUNTER_LEN);
1949 sm->rx_replay_counter_set = 1;
1954 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1955 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1956 struct wpa_eapol_key *key,
1957 size_t mic_len, u16 ver,
1958 u8 *key_data, size_t *key_data_len)
1960 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1961 key_data, *key_data_len);
1963 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1964 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1969 /* Decrypt key data here so that this operation does not need
1970 * to be implemented separately for each message type. */
1971 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1972 #ifdef CONFIG_NO_RC4
1973 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1974 "WPA: RC4 not supported in the build");
1976 #else /* CONFIG_NO_RC4 */
1979 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
1980 os_memcpy(ek, key->key_iv, 16);
1981 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1982 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
1983 os_memset(ek, 0, sizeof(ek));
1984 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1988 os_memset(ek, 0, sizeof(ek));
1989 #endif /* CONFIG_NO_RC4 */
1990 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1991 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
1992 wpa_use_aes_key_wrap(sm->key_mgmt)) {
1995 wpa_printf(MSG_DEBUG,
1996 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
1997 (unsigned int) sm->ptk.kek_len);
1998 if (*key_data_len < 8 || *key_data_len % 8) {
1999 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2000 "WPA: Unsupported AES-WRAP len %u",
2001 (unsigned int) *key_data_len);
2004 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
2005 buf = os_malloc(*key_data_len);
2007 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2008 "WPA: No memory for AES-UNWRAP buffer");
2012 os_memset(buf, 0x11, *key_data_len);
2013 #endif /* TEST_FUZZ */
2014 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
2017 wpa_printf(MSG_INFO,
2018 "TEST: Ignore AES unwrap failure for fuzz testing");
2020 #endif /* TEST_FUZZ */
2021 bin_clear_free(buf, *key_data_len);
2022 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2023 "WPA: AES unwrap failed - "
2024 "could not decrypt EAPOL-Key key data");
2029 #endif /* TEST_FUZZ */
2030 os_memcpy(key_data, buf, *key_data_len);
2031 bin_clear_free(buf, *key_data_len);
2032 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
2034 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2035 "WPA: Unsupported key_info type %d", ver);
2038 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
2039 key_data, *key_data_len);
2045 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2046 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2048 void wpa_sm_aborted_cached(struct wpa_sm *sm)
2050 if (sm && sm->cur_pmksa) {
2051 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2052 "RSN: Cancelling PMKSA caching attempt");
2053 sm->cur_pmksa = NULL;
2058 static void wpa_eapol_key_dump(struct wpa_sm *sm,
2059 const struct wpa_eapol_key *key,
2060 unsigned int key_data_len,
2061 const u8 *mic, unsigned int mic_len)
2063 #ifndef CONFIG_NO_STDOUT_DEBUG
2064 u16 key_info = WPA_GET_BE16(key->key_info);
2066 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
2067 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2068 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2069 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
2070 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2071 WPA_KEY_INFO_KEY_INDEX_SHIFT,
2072 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
2073 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
2074 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
2075 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
2076 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
2077 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
2078 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
2079 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
2080 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
2081 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2082 " key_length=%u key_data_length=%u",
2083 WPA_GET_BE16(key->key_length), key_data_len);
2084 wpa_hexdump(MSG_DEBUG, " replay_counter",
2085 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2086 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
2087 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
2088 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
2089 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
2090 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
2091 #endif /* CONFIG_NO_STDOUT_DEBUG */
2096 static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
2097 size_t *key_data_len)
2099 struct wpa_ptk *ptk;
2100 struct ieee802_1x_hdr *hdr;
2101 struct wpa_eapol_key *key;
2106 if (*key_data_len < AES_BLOCK_SIZE) {
2107 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
2113 else if (sm->ptk_set)
2118 hdr = (struct ieee802_1x_hdr *) buf;
2119 key = (struct wpa_eapol_key *) (hdr + 1);
2120 pos = (u8 *) (key + 1);
2121 pos += 2; /* Pointing at the Encrypted Key Data field */
2123 tmp = os_malloc(*key_data_len);
2127 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2128 * to Key Data (exclusive). */
2130 aad_len[0] = pos - buf;
2131 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
2132 1, aad, aad_len, tmp) < 0) {
2133 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
2134 bin_clear_free(tmp, *key_data_len);
2138 /* AEAD decryption and validation completed successfully */
2139 (*key_data_len) -= AES_BLOCK_SIZE;
2140 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2141 tmp, *key_data_len);
2143 /* Replace Key Data field with the decrypted version */
2144 os_memcpy(pos, tmp, *key_data_len);
2145 pos -= 2; /* Key Data Length field */
2146 WPA_PUT_BE16(pos, *key_data_len);
2147 bin_clear_free(tmp, *key_data_len);
2152 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
2153 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2156 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2157 WPA_REPLAY_COUNTER_LEN);
2158 sm->rx_replay_counter_set = 1;
2162 #endif /* CONFIG_FILS */
2166 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2167 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2168 * @src_addr: Source MAC address of the EAPOL packet
2169 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2170 * @len: Length of the EAPOL frame
2171 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2173 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2174 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2175 * only processing WPA and WPA2 EAPOL-Key frames.
2177 * The received EAPOL-Key packets are validated and valid packets are replied
2178 * to. In addition, key material (PTK, GTK) is configured at the end of a
2179 * successful key handshake.
2181 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
2182 const u8 *buf, size_t len)
2184 size_t plen, data_len, key_data_len;
2185 const struct ieee802_1x_hdr *hdr;
2186 struct wpa_eapol_key *key;
2191 size_t mic_len, keyhdrlen;
2193 #ifdef CONFIG_IEEE80211R
2194 sm->ft_completed = 0;
2195 #endif /* CONFIG_IEEE80211R */
2197 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
2198 keyhdrlen = sizeof(*key) + mic_len + 2;
2200 if (len < sizeof(*hdr) + keyhdrlen) {
2201 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2202 "WPA: EAPOL frame too short to be a WPA "
2203 "EAPOL-Key (len %lu, expecting at least %lu)",
2204 (unsigned long) len,
2205 (unsigned long) sizeof(*hdr) + keyhdrlen);
2209 hdr = (const struct ieee802_1x_hdr *) buf;
2210 plen = be_to_host16(hdr->length);
2211 data_len = plen + sizeof(*hdr);
2212 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2213 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2214 hdr->version, hdr->type, (unsigned long) plen);
2216 if (hdr->version < EAPOL_VERSION) {
2217 /* TODO: backwards compatibility */
2219 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2220 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2221 "WPA: EAPOL frame (type %u) discarded, "
2222 "not a Key frame", hdr->type);
2226 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
2227 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
2228 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2229 "WPA: EAPOL frame payload size %lu "
2230 "invalid (frame size %lu)",
2231 (unsigned long) plen, (unsigned long) len);
2235 if (data_len < len) {
2236 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2237 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2238 (unsigned long) len - data_len);
2242 * Make a copy of the frame since we need to modify the buffer during
2243 * MAC validation and Key Data decryption.
2245 tmp = os_memdup(buf, data_len);
2248 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
2249 mic = (u8 *) (key + 1);
2250 key_data = mic + mic_len + 2;
2252 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2254 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2255 "WPA: EAPOL-Key type (%d) unknown, discarded",
2261 key_data_len = WPA_GET_BE16(mic + mic_len);
2262 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
2264 if (key_data_len > plen - keyhdrlen) {
2265 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2266 "frame - key_data overflow (%u > %u)",
2267 (unsigned int) key_data_len,
2268 (unsigned int) (plen - keyhdrlen));
2272 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
2273 key_info = WPA_GET_BE16(key->key_info);
2274 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2275 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
2276 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
2277 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2278 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
2279 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
2280 !wpa_use_akm_defined(sm->key_mgmt)) {
2281 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2282 "WPA: Unsupported EAPOL-Key descriptor version %d",
2287 if (wpa_use_akm_defined(sm->key_mgmt) &&
2288 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2289 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2290 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2295 #ifdef CONFIG_IEEE80211R
2296 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2297 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
2298 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2299 !wpa_use_akm_defined(sm->key_mgmt)) {
2300 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2301 "FT: AP did not use AES-128-CMAC");
2305 #endif /* CONFIG_IEEE80211R */
2306 #ifdef CONFIG_IEEE80211W
2307 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
2308 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2309 !wpa_use_akm_defined(sm->key_mgmt)) {
2310 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2311 "WPA: AP did not use the "
2312 "negotiated AES-128-CMAC");
2316 #endif /* CONFIG_IEEE80211W */
2317 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
2318 !wpa_use_akm_defined(sm->key_mgmt) &&
2319 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2320 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2321 "WPA: CCMP is used, but EAPOL-Key "
2322 "descriptor version (%d) is not 2", ver);
2323 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2324 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2325 /* Earlier versions of IEEE 802.11i did not explicitly
2326 * require version 2 descriptor for all EAPOL-Key
2327 * packets, so allow group keys to use version 1 if
2328 * CCMP is not used for them. */
2329 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2330 "WPA: Backwards compatibility: allow invalid "
2331 "version for non-CCMP group keys");
2332 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2333 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2334 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
2337 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
2338 !wpa_use_akm_defined(sm->key_mgmt) &&
2339 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2340 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2341 "WPA: GCMP is used, but EAPOL-Key "
2342 "descriptor version (%d) is not 2", ver);
2346 if (sm->rx_replay_counter_set &&
2347 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2348 WPA_REPLAY_COUNTER_LEN) <= 0) {
2349 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2350 "WPA: EAPOL-Key Replay Counter did not increase - "
2355 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2356 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2357 "WPA: Unsupported SMK bit in key_info");
2361 if (!(key_info & WPA_KEY_INFO_ACK)) {
2362 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2363 "WPA: No Ack bit in key_info");
2367 if (key_info & WPA_KEY_INFO_REQUEST) {
2368 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2369 "WPA: EAPOL-Key with Request bit - dropped");
2373 if ((key_info & WPA_KEY_INFO_MIC) &&
2374 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
2378 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2379 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2382 #endif /* CONFIG_FILS */
2384 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
2385 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
2387 * Only decrypt the Key Data field if the frame's authenticity
2388 * was verified. When using AES-SIV (FILS), the MIC flag is not
2389 * set, so this check should only be performed if mic_len != 0
2390 * which is the case in this code branch.
2392 if (!(key_info & WPA_KEY_INFO_MIC)) {
2393 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2394 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2397 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2403 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2404 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2405 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2406 "WPA: Ignored EAPOL-Key (Pairwise) with "
2407 "non-zero key index");
2410 if (key_info & (WPA_KEY_INFO_MIC |
2411 WPA_KEY_INFO_ENCR_KEY_DATA)) {
2412 /* 3/4 4-Way Handshake */
2413 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2416 /* 1/4 4-Way Handshake */
2417 wpa_supplicant_process_1_of_4(sm, src_addr, key,
2422 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2423 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
2424 /* 1/2 Group Key Handshake */
2425 wpa_supplicant_process_1_of_2(sm, src_addr, key,
2426 key_data, key_data_len,
2429 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2430 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
2438 bin_clear_free(tmp, data_len);
2443 #ifdef CONFIG_CTRL_IFACE
2444 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2446 switch (sm->key_mgmt) {
2447 case WPA_KEY_MGMT_IEEE8021X:
2448 return ((sm->proto == WPA_PROTO_RSN ||
2449 sm->proto == WPA_PROTO_OSEN) ?
2450 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2451 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2452 case WPA_KEY_MGMT_PSK:
2453 return (sm->proto == WPA_PROTO_RSN ?
2454 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2455 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2456 #ifdef CONFIG_IEEE80211R
2457 case WPA_KEY_MGMT_FT_IEEE8021X:
2458 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2459 case WPA_KEY_MGMT_FT_PSK:
2460 return RSN_AUTH_KEY_MGMT_FT_PSK;
2461 #endif /* CONFIG_IEEE80211R */
2462 #ifdef CONFIG_IEEE80211W
2463 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2464 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2465 case WPA_KEY_MGMT_PSK_SHA256:
2466 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2467 #endif /* CONFIG_IEEE80211W */
2468 case WPA_KEY_MGMT_CCKM:
2469 return (sm->proto == WPA_PROTO_RSN ?
2470 RSN_AUTH_KEY_MGMT_CCKM:
2471 WPA_AUTH_KEY_MGMT_CCKM);
2472 case WPA_KEY_MGMT_WPA_NONE:
2473 return WPA_AUTH_KEY_MGMT_NONE;
2474 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2475 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
2476 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2477 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
2484 #define RSN_SUITE "%02x-%02x-%02x-%d"
2485 #define RSN_SUITE_ARG(s) \
2486 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2489 * wpa_sm_get_mib - Dump text list of MIB entries
2490 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2491 * @buf: Buffer for the list
2492 * @buflen: Length of the buffer
2493 * Returns: Number of bytes written to buffer
2495 * This function is used fetch dot11 MIB variables.
2497 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2499 char pmkid_txt[PMKID_LEN * 2 + 1];
2503 if (sm->cur_pmksa) {
2504 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2505 sm->cur_pmksa->pmkid, PMKID_LEN);
2507 pmkid_txt[0] = '\0';
2509 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2510 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2511 sm->proto == WPA_PROTO_RSN)
2516 ret = os_snprintf(buf, buflen,
2517 "dot11RSNAOptionImplemented=TRUE\n"
2518 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2519 "dot11RSNAEnabled=%s\n"
2520 "dot11RSNAPreauthenticationEnabled=%s\n"
2521 "dot11RSNAConfigVersion=%d\n"
2522 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2523 "dot11RSNAConfigGroupCipherSize=%d\n"
2524 "dot11RSNAConfigPMKLifetime=%d\n"
2525 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2526 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2527 "dot11RSNAConfigSATimeout=%d\n",
2528 rsna ? "TRUE" : "FALSE",
2529 rsna ? "TRUE" : "FALSE",
2531 wpa_cipher_key_len(sm->group_cipher) * 8,
2532 sm->dot11RSNAConfigPMKLifetime,
2533 sm->dot11RSNAConfigPMKReauthThreshold,
2534 sm->dot11RSNAConfigSATimeout);
2535 if (os_snprintf_error(buflen, ret))
2540 buf + len, buflen - len,
2541 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2542 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2543 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2544 "dot11RSNAPMKIDUsed=%s\n"
2545 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2546 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2547 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2548 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2549 "dot11RSNA4WayHandshakeFailures=%u\n",
2550 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2551 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2552 sm->pairwise_cipher)),
2553 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2556 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2557 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2558 sm->pairwise_cipher)),
2559 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2561 sm->dot11RSNA4WayHandshakeFailures);
2562 if (!os_snprintf_error(buflen - len, ret))
2567 #endif /* CONFIG_CTRL_IFACE */
2570 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
2571 void *ctx, enum pmksa_free_reason reason)
2573 struct wpa_sm *sm = ctx;
2576 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2577 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2579 if (sm->cur_pmksa == entry) {
2580 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2581 "RSN: %s current PMKSA entry",
2582 reason == PMKSA_REPLACE ? "replaced" : "removed");
2583 pmksa_cache_clear_current(sm);
2586 * If an entry is simply being replaced, there's no need to
2587 * deauthenticate because it will be immediately re-added.
2588 * This happens when EAP authentication is completed again
2589 * (reauth or failed PMKSA caching attempt).
2591 if (reason != PMKSA_REPLACE)
2595 if (reason == PMKSA_EXPIRE &&
2596 (sm->pmk_len == entry->pmk_len &&
2597 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2598 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2599 "RSN: deauthenticating due to expired PMK");
2600 pmksa_cache_clear_current(sm);
2606 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2607 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2613 * wpa_sm_init - Initialize WPA state machine
2614 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2615 * Returns: Pointer to the allocated WPA state machine data
2617 * This function is used to allocate a new WPA state machine and the returned
2618 * value is passed to all WPA state machine calls.
2620 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2624 sm = os_zalloc(sizeof(*sm));
2627 dl_list_init(&sm->pmksa_candidates);
2628 sm->renew_snonce = 1;
2631 sm->dot11RSNAConfigPMKLifetime = 43200;
2632 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2633 sm->dot11RSNAConfigSATimeout = 60;
2635 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2636 if (sm->pmksa == NULL) {
2637 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2638 "RSN: PMKSA cache initialization failed");
2648 * wpa_sm_deinit - Deinitialize WPA state machine
2649 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2651 void wpa_sm_deinit(struct wpa_sm *sm)
2655 pmksa_cache_deinit(sm->pmksa);
2656 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2657 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2658 os_free(sm->assoc_wpa_ie);
2659 os_free(sm->ap_wpa_ie);
2660 os_free(sm->ap_rsn_ie);
2663 #ifdef CONFIG_IEEE80211R
2664 os_free(sm->assoc_resp_ies);
2665 #endif /* CONFIG_IEEE80211R */
2666 #ifdef CONFIG_TESTING_OPTIONS
2667 wpabuf_free(sm->test_assoc_ie);
2668 #endif /* CONFIG_TESTING_OPTIONS */
2669 #ifdef CONFIG_FILS_SK_PFS
2670 crypto_ecdh_deinit(sm->fils_ecdh);
2671 #endif /* CONFIG_FILS_SK_PFS */
2673 wpabuf_free(sm->fils_ft_ies);
2674 #endif /* CONFIG_FILS */
2676 crypto_ecdh_deinit(sm->owe_ecdh);
2677 #endif /* CONFIG_OWE */
2679 wpabuf_clear_free(sm->dpp_z);
2680 #endif /* CONFIG_DPP2 */
2686 * wpa_sm_notify_assoc - Notify WPA state machine about association
2687 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2688 * @bssid: The BSSID of the new association
2690 * This function is called to let WPA state machine know that the connection
2693 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2700 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2701 "WPA: Association event - clear replay counter");
2702 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2703 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2704 sm->rx_replay_counter_set = 0;
2705 sm->renew_snonce = 1;
2706 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2707 rsn_preauth_deinit(sm);
2709 #ifdef CONFIG_IEEE80211R
2710 if (wpa_ft_is_completed(sm)) {
2712 * Clear portValid to kick EAPOL state machine to re-enter
2713 * AUTHENTICATED state to get the EAPOL port Authorized.
2715 eapol_sm_notify_portValid(sm->eapol, FALSE);
2716 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2718 /* Prepare for the next transition */
2719 wpa_ft_prepare_auth_request(sm, NULL);
2722 sm->ft_protocol = 1;
2724 sm->ft_protocol = 0;
2726 #endif /* CONFIG_IEEE80211R */
2728 if (sm->fils_completed) {
2730 * Clear portValid to kick EAPOL state machine to re-enter
2731 * AUTHENTICATED state to get the EAPOL port Authorized.
2733 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2736 #endif /* CONFIG_FILS */
2740 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2741 * this is not part of a Fast BSS Transition.
2743 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2745 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2747 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2748 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2749 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
2750 #ifdef CONFIG_IEEE80211W
2751 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2752 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
2753 #endif /* CONFIG_IEEE80211W */
2758 #endif /* CONFIG_TDLS */
2761 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2762 #endif /* CONFIG_P2P */
2767 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2768 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2770 * This function is called to let WPA state machine know that the connection
2771 * was lost. This will abort any existing pre-authentication session.
2773 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2775 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2776 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2777 rsn_preauth_deinit(sm);
2778 pmksa_cache_clear_current(sm);
2779 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2780 sm->dot11RSNA4WayHandshakeFailures++;
2782 wpa_tdls_disassoc(sm);
2783 #endif /* CONFIG_TDLS */
2785 sm->fils_completed = 0;
2786 #endif /* CONFIG_FILS */
2787 #ifdef CONFIG_IEEE80211R
2788 sm->ft_reassoc_completed = 0;
2789 sm->ft_protocol = 0;
2790 #endif /* CONFIG_IEEE80211R */
2792 /* Keys are not needed in the WPA state machine anymore */
2795 sm->msg_3_of_4_ok = 0;
2796 os_memset(sm->bssid, 0, ETH_ALEN);
2801 * wpa_sm_set_pmk - Set PMK
2802 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2804 * @pmk_len: The length of the new PMK in bytes
2805 * @pmkid: Calculated PMKID
2806 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
2808 * Configure the PMK for WPA state machine.
2810 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2811 const u8 *pmkid, const u8 *bssid)
2816 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
2818 sm->pmk_len = pmk_len;
2819 os_memcpy(sm->pmk, pmk, pmk_len);
2821 #ifdef CONFIG_IEEE80211R
2822 /* Set XXKey to be PSK for FT key derivation */
2823 sm->xxkey_len = pmk_len;
2824 os_memcpy(sm->xxkey, pmk, pmk_len);
2825 #endif /* CONFIG_IEEE80211R */
2828 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
2829 bssid, sm->own_addr,
2830 sm->network_ctx, sm->key_mgmt, NULL);
2836 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2837 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2839 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2842 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2847 if (sm->cur_pmksa) {
2848 wpa_hexdump_key(MSG_DEBUG,
2849 "WPA: Set PMK based on current PMKSA",
2850 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
2851 sm->pmk_len = sm->cur_pmksa->pmk_len;
2852 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2854 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
2856 os_memset(sm->pmk, 0, PMK_LEN_MAX);
2862 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2863 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2864 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2866 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2869 sm->fast_reauth = fast_reauth;
2874 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2875 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2876 * @scard_ctx: Context pointer for smartcard related callback functions
2878 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2882 sm->scard_ctx = scard_ctx;
2883 if (sm->preauth_eapol)
2884 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2889 * wpa_sm_set_config - Notification of current configration change
2890 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2891 * @config: Pointer to current network configuration
2893 * Notify WPA state machine that configuration has changed. config will be
2894 * stored as a backpointer to network configuration. This can be %NULL to clear
2895 * the stored pointed.
2897 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2903 sm->network_ctx = config->network_ctx;
2904 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2905 sm->proactive_key_caching = config->proactive_key_caching;
2906 sm->eap_workaround = config->eap_workaround;
2907 sm->eap_conf_ctx = config->eap_conf_ctx;
2909 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2910 sm->ssid_len = config->ssid_len;
2913 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2914 sm->p2p = config->p2p;
2915 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
2917 if (config->fils_cache_id) {
2918 sm->fils_cache_id_set = 1;
2919 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
2922 sm->fils_cache_id_set = 0;
2924 #endif /* CONFIG_FILS */
2926 sm->network_ctx = NULL;
2927 sm->allowed_pairwise_cipher = 0;
2928 sm->proactive_key_caching = 0;
2929 sm->eap_workaround = 0;
2930 sm->eap_conf_ctx = NULL;
2932 sm->wpa_ptk_rekey = 0;
2934 sm->wpa_rsc_relaxation = 0;
2940 * wpa_sm_set_own_addr - Set own MAC address
2941 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2942 * @addr: Own MAC address
2944 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2947 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2952 * wpa_sm_set_ifname - Set network interface name
2953 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2954 * @ifname: Interface name
2955 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2957 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2958 const char *bridge_ifname)
2961 sm->ifname = ifname;
2962 sm->bridge_ifname = bridge_ifname;
2968 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2969 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2970 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2972 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2980 * wpa_sm_set_param - Set WPA state machine parameters
2981 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2982 * @param: Parameter field
2983 * @value: Parameter value
2984 * Returns: 0 on success, -1 on failure
2986 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2995 case RSNA_PMK_LIFETIME:
2997 sm->dot11RSNAConfigPMKLifetime = value;
3001 case RSNA_PMK_REAUTH_THRESHOLD:
3002 if (value > 0 && value <= 100)
3003 sm->dot11RSNAConfigPMKReauthThreshold = value;
3007 case RSNA_SA_TIMEOUT:
3009 sm->dot11RSNAConfigSATimeout = value;
3013 case WPA_PARAM_PROTO:
3016 case WPA_PARAM_PAIRWISE:
3017 sm->pairwise_cipher = value;
3019 case WPA_PARAM_GROUP:
3020 sm->group_cipher = value;
3022 case WPA_PARAM_KEY_MGMT:
3023 sm->key_mgmt = value;
3025 #ifdef CONFIG_IEEE80211W
3026 case WPA_PARAM_MGMT_GROUP:
3027 sm->mgmt_group_cipher = value;
3029 #endif /* CONFIG_IEEE80211W */
3030 case WPA_PARAM_RSN_ENABLED:
3031 sm->rsn_enabled = value;
3048 * wpa_sm_get_status - Get WPA state machine
3049 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3050 * @buf: Buffer for status information
3051 * @buflen: Maximum buffer length
3052 * @verbose: Whether to include verbose status information
3053 * Returns: Number of bytes written to buf.
3055 * Query WPA state machine for status information. This function fills in
3056 * a text area with current status information. If the buffer (buf) is not
3057 * large enough, status information will be truncated to fit the buffer.
3059 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
3062 char *pos = buf, *end = buf + buflen;
3065 ret = os_snprintf(pos, end - pos,
3066 "pairwise_cipher=%s\n"
3069 wpa_cipher_txt(sm->pairwise_cipher),
3070 wpa_cipher_txt(sm->group_cipher),
3071 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
3072 if (os_snprintf_error(end - pos, ret))
3076 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
3077 struct wpa_ie_data rsn;
3078 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
3080 rsn.capabilities & (WPA_CAPABILITY_MFPR |
3081 WPA_CAPABILITY_MFPC)) {
3082 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
3083 "mgmt_group_cipher=%s\n",
3085 WPA_CAPABILITY_MFPR) ? 2 : 1,
3087 sm->mgmt_group_cipher));
3088 if (os_snprintf_error(end - pos, ret))
3098 int wpa_sm_pmf_enabled(struct wpa_sm *sm)
3100 struct wpa_ie_data rsn;
3102 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
3105 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
3106 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
3113 int wpa_sm_ocv_enabled(struct wpa_sm *sm)
3115 struct wpa_ie_data rsn;
3117 if (!sm->ocv || !sm->ap_rsn_ie)
3120 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
3122 (rsn.capabilities & WPA_CAPABILITY_OCVC);
3127 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3128 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3129 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3130 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3131 * Returns: 0 on success, -1 on failure
3133 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
3141 #ifdef CONFIG_TESTING_OPTIONS
3142 if (sm->test_assoc_ie) {
3143 wpa_printf(MSG_DEBUG,
3144 "TESTING: Replace association WPA/RSN IE");
3145 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
3147 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
3148 wpabuf_len(sm->test_assoc_ie));
3149 res = wpabuf_len(sm->test_assoc_ie);
3151 #endif /* CONFIG_TESTING_OPTIONS */
3152 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
3157 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
3158 wpa_ie, *wpa_ie_len);
3160 if (sm->assoc_wpa_ie == NULL) {
3162 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3163 * the correct version of the IE even if PMKSA caching is
3164 * aborted (which would remove PMKID from IE generation).
3166 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
3167 if (sm->assoc_wpa_ie == NULL)
3170 sm->assoc_wpa_ie_len = *wpa_ie_len;
3172 wpa_hexdump(MSG_DEBUG,
3173 "WPA: Leave previously set WPA IE default",
3174 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3182 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3183 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3184 * @ie: Pointer to IE data (starting from id)
3186 * Returns: 0 on success, -1 on failure
3188 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3189 * Request frame. The IE will be used to override the default value generated
3190 * with wpa_sm_set_assoc_wpa_ie_default().
3192 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3197 os_free(sm->assoc_wpa_ie);
3198 if (ie == NULL || len == 0) {
3199 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3200 "WPA: clearing own WPA/RSN IE");
3201 sm->assoc_wpa_ie = NULL;
3202 sm->assoc_wpa_ie_len = 0;
3204 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
3205 sm->assoc_wpa_ie = os_memdup(ie, len);
3206 if (sm->assoc_wpa_ie == NULL)
3209 sm->assoc_wpa_ie_len = len;
3217 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3218 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3219 * @ie: Pointer to IE data (starting from id)
3221 * Returns: 0 on success, -1 on failure
3223 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3226 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3231 os_free(sm->ap_wpa_ie);
3232 if (ie == NULL || len == 0) {
3233 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3234 "WPA: clearing AP WPA IE");
3235 sm->ap_wpa_ie = NULL;
3236 sm->ap_wpa_ie_len = 0;
3238 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
3239 sm->ap_wpa_ie = os_memdup(ie, len);
3240 if (sm->ap_wpa_ie == NULL)
3243 sm->ap_wpa_ie_len = len;
3251 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3252 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3253 * @ie: Pointer to IE data (starting from id)
3255 * Returns: 0 on success, -1 on failure
3257 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3260 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3265 os_free(sm->ap_rsn_ie);
3266 if (ie == NULL || len == 0) {
3267 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3268 "WPA: clearing AP RSN IE");
3269 sm->ap_rsn_ie = NULL;
3270 sm->ap_rsn_ie_len = 0;
3272 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
3273 sm->ap_rsn_ie = os_memdup(ie, len);
3274 if (sm->ap_rsn_ie == NULL)
3277 sm->ap_rsn_ie_len = len;
3285 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3286 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3287 * @data: Pointer to data area for parsing results
3288 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3290 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3291 * parsed data into data.
3293 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3298 if (sm->assoc_wpa_ie == NULL) {
3299 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3300 "WPA: No WPA/RSN IE available from association info");
3303 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3309 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3311 return pmksa_cache_list(sm->pmksa, buf, len);
3315 struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3317 return pmksa_cache_head(sm->pmksa);
3321 struct rsn_pmksa_cache_entry *
3322 wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3323 struct rsn_pmksa_cache_entry * entry)
3325 return pmksa_cache_add_entry(sm->pmksa, entry);
3329 void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3330 const u8 *pmkid, const u8 *bssid,
3331 const u8 *fils_cache_id)
3333 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3334 bssid, sm->own_addr, sm->network_ctx,
3335 sm->key_mgmt, fils_cache_id);
3339 int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3340 const void *network_ctx)
3342 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
3346 void wpa_sm_drop_sa(struct wpa_sm *sm)
3348 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3352 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3353 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3354 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3355 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
3356 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
3357 #ifdef CONFIG_IEEE80211W
3358 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
3359 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
3360 #endif /* CONFIG_IEEE80211W */
3361 #ifdef CONFIG_IEEE80211R
3362 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
3364 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
3366 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
3368 #endif /* CONFIG_IEEE80211R */
3372 int wpa_sm_has_ptk(struct wpa_sm *sm)
3380 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3382 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3386 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3388 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
3393 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3396 u8 keylen; /* plaintext key len */
3399 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
3400 struct wpa_gtk_data gd;
3402 os_memset(&gd, 0, sizeof(gd));
3403 keylen = wpa_cipher_key_len(sm->group_cipher);
3404 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3405 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3406 if (gd.alg == WPA_ALG_NONE) {
3407 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3412 keyinfo = WPA_GET_LE16(buf + 2);
3413 gd.gtk_len = keylen;
3414 if (gd.gtk_len != buf[4]) {
3415 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3416 gd.gtk_len, buf[4]);
3419 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3420 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3421 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3423 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
3425 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3426 gd.gtk, gd.gtk_len);
3427 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
3428 os_memset(&gd, 0, sizeof(gd));
3429 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3433 os_memset(&gd, 0, sizeof(gd));
3434 #ifdef CONFIG_IEEE80211W
3435 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
3436 const struct wpa_igtk_kde *igtk;
3438 igtk = (const struct wpa_igtk_kde *) (buf + 2);
3439 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
3441 #endif /* CONFIG_IEEE80211W */
3443 wpa_printf(MSG_DEBUG, "Unknown element id");
3449 #endif /* CONFIG_WNM */
3454 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3456 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3458 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3462 #endif /* CONFIG_P2P */
3465 void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3467 if (rx_replay_counter == NULL)
3470 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3471 WPA_REPLAY_COUNTER_LEN);
3472 sm->rx_replay_counter_set = 1;
3473 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3477 void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3478 const u8 *ptk_kck, size_t ptk_kck_len,
3479 const u8 *ptk_kek, size_t ptk_kek_len)
3481 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3482 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3483 sm->ptk.kck_len = ptk_kck_len;
3484 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3486 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3487 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3488 sm->ptk.kek_len = ptk_kek_len;
3489 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3495 #ifdef CONFIG_TESTING_OPTIONS
3497 void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3499 wpabuf_free(sm->test_assoc_ie);
3500 sm->test_assoc_ie = buf;
3504 const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
3509 #endif /* CONFIG_TESTING_OPTIONS */
3512 unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
3514 return sm->key_mgmt;
3520 struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
3522 struct wpabuf *buf = NULL;
3523 struct wpabuf *erp_msg;
3524 struct wpabuf *pub = NULL;
3526 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
3527 if (!erp_msg && !sm->cur_pmksa) {
3528 wpa_printf(MSG_DEBUG,
3529 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3533 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3534 erp_msg != NULL, sm->cur_pmksa != NULL);
3536 sm->fils_completed = 0;
3538 if (!sm->assoc_wpa_ie) {
3539 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
3543 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
3544 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
3547 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
3548 sm->fils_nonce, FILS_NONCE_LEN);
3549 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
3550 sm->fils_session, FILS_SESSION_LEN);
3552 #ifdef CONFIG_FILS_SK_PFS
3553 sm->fils_dh_group = dh_group;
3555 crypto_ecdh_deinit(sm->fils_ecdh);
3556 sm->fils_ecdh = crypto_ecdh_init(dh_group);
3557 if (!sm->fils_ecdh) {
3558 wpa_printf(MSG_INFO,
3559 "FILS: Could not initialize ECDH with group %d",
3563 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3566 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
3568 sm->fils_dh_elem_len = wpabuf_len(pub);
3570 #endif /* CONFIG_FILS_SK_PFS */
3572 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
3573 (pub ? wpabuf_len(pub) : 0));
3577 /* Fields following the Authentication algorithm number field */
3579 /* Authentication Transaction seq# */
3580 wpabuf_put_le16(buf, 1);
3583 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
3586 #ifdef CONFIG_FILS_SK_PFS
3588 /* Finite Cyclic Group */
3589 wpabuf_put_le16(buf, dh_group);
3591 wpabuf_put_buf(buf, pub);
3593 #endif /* CONFIG_FILS_SK_PFS */
3596 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
3597 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3598 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3601 /* MDE when using FILS for FT initial association */
3602 struct rsn_mdie *mdie;
3604 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
3605 wpabuf_put_u8(buf, sizeof(*mdie));
3606 mdie = wpabuf_put(buf, sizeof(*mdie));
3607 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
3612 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3613 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
3614 /* Element ID Extension */
3615 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
3616 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
3619 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3620 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3621 /* Element ID Extension */
3622 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3623 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3625 /* FILS Wrapped Data */
3626 sm->fils_erp_pmkid_set = 0;
3628 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3629 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
3630 /* Element ID Extension */
3631 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA);
3632 wpabuf_put_buf(buf, erp_msg);
3633 /* Calculate pending PMKID here so that we do not need to
3634 * maintain a copy of the EAP-Initiate/Reauth message. */
3635 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
3636 wpabuf_len(erp_msg),
3637 sm->fils_erp_pmkid) == 0)
3638 sm->fils_erp_pmkid_set = 1;
3641 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
3645 wpabuf_free(erp_msg);
3651 int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
3654 const u8 *pos, *end;
3655 struct ieee802_11_elems elems;
3656 struct wpa_ie_data rsn;
3657 int pmkid_match = 0;
3658 u8 ick[FILS_ICK_MAX_LEN];
3661 struct wpabuf *dh_ss = NULL;
3662 const u8 *g_sta = NULL;
3663 size_t g_sta_len = 0;
3664 const u8 *g_ap = NULL;
3665 size_t g_ap_len = 0;
3666 struct wpabuf *pub = NULL;
3668 os_memcpy(sm->bssid, bssid, ETH_ALEN);
3670 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
3676 #ifdef CONFIG_FILS_SK_PFS
3677 if (sm->fils_dh_group) {
3680 /* Using FILS PFS */
3682 /* Finite Cyclic Group */
3683 if (end - pos < 2) {
3684 wpa_printf(MSG_DEBUG,
3685 "FILS: No room for Finite Cyclic Group");
3688 group = WPA_GET_LE16(pos);
3690 if (group != sm->fils_dh_group) {
3691 wpa_printf(MSG_DEBUG,
3692 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
3693 group, sm->fils_dh_group);
3698 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
3699 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
3703 if (!sm->fils_ecdh) {
3704 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
3707 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
3708 sm->fils_dh_elem_len);
3710 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
3713 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
3715 g_ap_len = sm->fils_dh_elem_len;
3716 pos += sm->fils_dh_elem_len;
3718 #endif /* CONFIG_FILS_SK_PFS */
3720 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
3721 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
3722 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
3727 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
3729 if (!elems.rsn_ie ||
3730 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3732 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
3736 if (!elems.fils_nonce) {
3737 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
3740 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
3741 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
3743 #ifdef CONFIG_IEEE80211R
3744 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3745 struct wpa_ft_ies parse;
3747 if (!elems.mdie || !elems.ftie) {
3748 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
3752 if (wpa_ft_parse_ies(pos, end - pos, &parse,
3753 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
3754 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
3758 if (!parse.r0kh_id) {
3759 wpa_printf(MSG_DEBUG,
3760 "FILS+FT: No R0KH-ID subelem in FTE");
3763 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
3764 sm->r0kh_id_len = parse.r0kh_id_len;
3765 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3766 sm->r0kh_id, sm->r0kh_id_len);
3768 if (!parse.r1kh_id) {
3769 wpa_printf(MSG_DEBUG,
3770 "FILS+FT: No R1KH-ID subelem in FTE");
3773 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
3774 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
3775 sm->r1kh_id, FT_R1KH_ID_LEN);
3777 /* TODO: Check MDE and FTE payload */
3779 wpabuf_free(sm->fils_ft_ies);
3780 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
3781 2 + elems.ftie_len);
3782 if (!sm->fils_ft_ies)
3784 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
3785 2 + elems.mdie_len);
3786 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
3787 2 + elems.ftie_len);
3789 wpabuf_free(sm->fils_ft_ies);
3790 sm->fils_ft_ies = NULL;
3792 #endif /* CONFIG_IEEE80211R */
3795 if (rsn.pmkid && rsn.num_pmkid > 0) {
3796 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
3797 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
3799 if (rsn.num_pmkid != 1) {
3800 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
3803 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
3804 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
3806 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
3807 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
3808 sm->cur_pmksa->pmkid, PMKID_LEN);
3811 wpa_printf(MSG_DEBUG,
3812 "FILS: Matching PMKID - continue using PMKSA caching");
3815 if (!pmkid_match && sm->cur_pmksa) {
3816 wpa_printf(MSG_DEBUG,
3817 "FILS: No PMKID match - cannot use cached PMKSA entry");
3818 sm->cur_pmksa = NULL;
3822 if (!elems.fils_session) {
3823 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
3826 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
3828 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
3830 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
3831 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
3832 sm->fils_session, FILS_SESSION_LEN);
3836 /* FILS Wrapped Data */
3837 if (!sm->cur_pmksa && elems.fils_wrapped_data) {
3838 u8 rmsk[ERP_MAX_KEY_LEN];
3841 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
3842 elems.fils_wrapped_data,
3843 elems.fils_wrapped_data_len);
3844 eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data,
3845 elems.fils_wrapped_data_len);
3846 if (eapol_sm_failed(sm->eapol))
3849 rmsk_len = ERP_MAX_KEY_LEN;
3850 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3851 if (res == PMK_LEN) {
3853 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3858 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
3859 sm->fils_nonce, sm->fils_anonce,
3860 dh_ss ? wpabuf_head(dh_ss) : NULL,
3861 dh_ss ? wpabuf_len(dh_ss) : 0,
3862 sm->pmk, &sm->pmk_len);
3863 os_memset(rmsk, 0, sizeof(rmsk));
3865 /* Don't use DHss in PTK derivation if PMKSA caching is not
3867 wpabuf_clear_free(dh_ss);
3873 if (!sm->fils_erp_pmkid_set) {
3874 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
3877 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
3879 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
3880 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
3881 sm->fils_erp_pmkid, NULL, 0,
3882 sm->bssid, sm->own_addr,
3883 sm->network_ctx, sm->key_mgmt,
3887 if (!sm->cur_pmksa) {
3888 wpa_printf(MSG_DEBUG,
3889 "FILS: No remaining options to continue FILS authentication");
3893 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
3894 sm->fils_nonce, sm->fils_anonce,
3895 dh_ss ? wpabuf_head(dh_ss) : NULL,
3896 dh_ss ? wpabuf_len(dh_ss) : 0,
3897 &sm->ptk, ick, &ick_len,
3898 sm->key_mgmt, sm->pairwise_cipher,
3899 sm->fils_ft, &sm->fils_ft_len) < 0) {
3900 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
3904 wpabuf_clear_free(dh_ss);
3909 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3911 #ifdef CONFIG_FILS_SK_PFS
3912 if (sm->fils_dh_group) {
3913 if (!sm->fils_ecdh) {
3914 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
3917 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3920 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
3921 g_sta = wpabuf_head(pub);
3922 g_sta_len = wpabuf_len(pub);
3924 wpa_printf(MSG_INFO, "FILS: gAP not available");
3927 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
3929 #endif /* CONFIG_FILS_SK_PFS */
3931 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
3932 sm->fils_anonce, sm->own_addr, sm->bssid,
3933 g_sta, g_sta_len, g_ap, g_ap_len,
3934 sm->key_mgmt, sm->fils_key_auth_sta,
3935 sm->fils_key_auth_ap,
3936 &sm->fils_key_auth_len);
3938 os_memset(ick, 0, sizeof(ick));
3942 wpabuf_clear_free(dh_ss);
3947 #ifdef CONFIG_IEEE80211R
3948 static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
3950 struct rsn_ie_hdr *rsnie;
3953 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
3955 /* RSNIE[PMKR0Name/PMKR1Name] */
3956 rsnie = wpabuf_put(buf, sizeof(*rsnie));
3957 rsnie->elem_id = WLAN_EID_RSN;
3958 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
3960 /* Group Suite Selector */
3961 if (!wpa_cipher_valid_group(sm->group_cipher)) {
3962 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
3966 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3967 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3970 /* Pairwise Suite Count */
3971 wpabuf_put_le16(buf, 1);
3973 /* Pairwise Suite List */
3974 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
3975 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
3976 sm->pairwise_cipher);
3979 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3980 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3981 sm->pairwise_cipher));
3983 /* Authenticated Key Management Suite Count */
3984 wpabuf_put_le16(buf, 1);
3986 /* Authenticated Key Management Suite List */
3987 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3988 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
3989 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
3990 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
3991 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
3993 wpa_printf(MSG_WARNING,
3994 "FILS+FT: Invalid key management type (%d)",
3999 /* RSN Capabilities */
4001 #ifdef CONFIG_IEEE80211W
4002 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
4003 capab |= WPA_CAPABILITY_MFPC;
4004 #endif /* CONFIG_IEEE80211W */
4006 capab |= WPA_CAPABILITY_OCVC;
4007 wpabuf_put_le16(buf, capab);
4010 wpabuf_put_le16(buf, 1);
4012 /* PMKID List [PMKR1Name] */
4013 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
4014 sm->fils_ft, sm->fils_ft_len);
4015 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
4016 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
4017 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
4018 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4019 sm->r0kh_id, sm->r0kh_id_len);
4020 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
4021 sm->ssid_len, sm->mobility_domain,
4022 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
4023 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
4024 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
4027 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
4028 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
4029 sm->pmk_r0, sm->pmk_r0_len);
4030 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
4031 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
4032 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
4033 MAC2STR(sm->r1kh_id));
4034 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
4035 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
4036 sm->pmk_r1_name, use_sha384) < 0) {
4037 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
4040 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name,
4042 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
4044 #ifdef CONFIG_IEEE80211W
4045 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
4046 /* Management Group Cipher Suite */
4047 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4048 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
4050 #endif /* CONFIG_IEEE80211W */
4052 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
4055 #endif /* CONFIG_IEEE80211R */
4058 struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
4059 size_t *kek_len, const u8 **snonce,
4061 const struct wpabuf **hlp,
4062 unsigned int num_hlp)
4069 #ifdef CONFIG_IEEE80211R
4070 if (sm->fils_ft_ies)
4071 len += wpabuf_len(sm->fils_ft_ies);
4072 if (wpa_key_mgmt_ft(sm->key_mgmt))
4074 #endif /* CONFIG_IEEE80211R */
4075 for (i = 0; hlp && i < num_hlp; i++)
4076 len += 10 + wpabuf_len(hlp[i]);
4077 buf = wpabuf_alloc(len);
4081 #ifdef CONFIG_IEEE80211R
4082 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4083 /* MDE and FTE when using FILS+FT */
4084 wpabuf_put_buf(buf, sm->fils_ft_ies);
4085 /* RSNE with PMKR1Name in PMKID field */
4086 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
4091 #endif /* CONFIG_IEEE80211R */
4094 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4095 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4096 /* Element ID Extension */
4097 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4098 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4100 /* Everything after FILS Session element gets encrypted in the driver
4101 * with KEK. The buffer returned from here is the plaintext version. */
4103 /* TODO: FILS Public Key */
4105 /* FILS Key Confirm */
4106 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4107 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
4108 /* Element ID Extension */
4109 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
4110 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
4112 /* FILS HLP Container */
4113 for (i = 0; hlp && i < num_hlp; i++) {
4114 const u8 *pos = wpabuf_head(hlp[i]);
4115 size_t left = wpabuf_len(hlp[i]);
4117 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4122 wpabuf_put_u8(buf, len); /* Length */
4123 /* Element ID Extension */
4124 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
4125 /* Destination MAC Address, Source MAC Address, HLP Packet.
4126 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4127 * header when LPD is used). */
4128 wpabuf_put_data(buf, pos, len - 1);
4132 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4133 len = left > 255 ? 255 : left;
4134 wpabuf_put_u8(buf, len);
4135 wpabuf_put_data(buf, pos, len);
4141 /* TODO: FILS IP Address Assignment */
4144 if (wpa_sm_ocv_enabled(sm)) {
4145 struct wpa_channel_info ci;
4148 if (wpa_sm_channel_info(sm, &ci) != 0) {
4149 wpa_printf(MSG_WARNING,
4150 "FILS: Failed to get channel info for OCI element");
4155 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4156 if (ocv_insert_extended_oci(&ci, pos) < 0) {
4161 #endif /* CONFIG_OCV */
4163 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
4166 *kek_len = sm->ptk.kek_len;
4167 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
4168 *snonce = sm->fils_nonce;
4169 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
4170 *snonce, FILS_NONCE_LEN);
4171 *anonce = sm->fils_anonce;
4172 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
4173 *anonce, FILS_NONCE_LEN);
4179 static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4181 const u8 *pos, *end;
4183 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
4184 if (len < 2 * ETH_ALEN)
4186 pos = resp + 2 * ETH_ALEN;
4188 if (end - pos >= 6 &&
4189 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4190 pos += 6; /* Remove SNAP/LLC header */
4191 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
4195 static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
4198 const u8 *end = pos + len;
4201 /* Check if there are any FILS HLP Container elements */
4202 while (end - pos >= 2) {
4203 if (2 + pos[1] > end - pos)
4205 if (pos[0] == WLAN_EID_EXTENSION &&
4206 pos[1] >= 1 + 2 * ETH_ALEN &&
4207 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
4212 return; /* No FILS HLP Container elements */
4214 tmp = os_malloc(end - pos);
4218 while (end - pos >= 2) {
4219 if (2 + pos[1] > end - pos ||
4220 pos[0] != WLAN_EID_EXTENSION ||
4221 pos[1] < 1 + 2 * ETH_ALEN ||
4222 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4225 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4226 tmp_pos += pos[1] - 1;
4229 /* Add possible fragments */
4230 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4231 2 + pos[1] <= end - pos) {
4232 os_memcpy(tmp_pos, pos + 2, pos[1]);
4237 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4244 int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4246 const struct ieee80211_mgmt *mgmt;
4247 const u8 *end, *ie_start;
4248 struct ieee802_11_elems elems;
4251 struct wpa_gtk_data gd;
4253 struct wpa_eapol_ie_parse kde;
4255 if (!sm || !sm->ptk_set) {
4256 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4260 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4261 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4265 if (sm->fils_completed) {
4266 wpa_printf(MSG_DEBUG,
4267 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4271 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4274 mgmt = (const struct ieee80211_mgmt *) resp;
4275 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4279 /* Same offset for Association Response and Reassociation Response */
4280 ie_start = mgmt->u.assoc_resp.variable;
4282 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4284 wpa_printf(MSG_DEBUG,
4285 "FILS: Failed to parse decrypted elements");
4289 if (!elems.fils_session) {
4290 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4293 if (os_memcmp(elems.fils_session, sm->fils_session,
4294 FILS_SESSION_LEN) != 0) {
4295 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4296 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4297 elems.fils_session, FILS_SESSION_LEN);
4298 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4299 sm->fils_session, FILS_SESSION_LEN);
4302 /* TODO: FILS Public Key */
4304 if (!elems.fils_key_confirm) {
4305 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4308 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4309 wpa_printf(MSG_DEBUG,
4310 "FILS: Unexpected Key-Auth length %d (expected %d)",
4311 elems.fils_key_confirm_len,
4312 (int) sm->fils_key_auth_len);
4315 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4316 sm->fils_key_auth_len) != 0) {
4317 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4318 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4319 elems.fils_key_confirm,
4320 elems.fils_key_confirm_len);
4321 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4322 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4327 if (wpa_sm_ocv_enabled(sm)) {
4328 struct wpa_channel_info ci;
4330 if (wpa_sm_channel_info(sm, &ci) != 0) {
4331 wpa_printf(MSG_WARNING,
4332 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4336 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
4337 channel_width_to_int(ci.chanwidth),
4338 ci.seg1_idx) != 0) {
4339 wpa_printf(MSG_WARNING, "FILS: %s", ocv_errorstr);
4343 #endif /* CONFIG_OCV */
4345 #ifdef CONFIG_IEEE80211R
4346 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4347 struct wpa_ie_data rsn;
4349 /* Check that PMKR1Name derived by the AP matches */
4350 if (!elems.rsn_ie ||
4351 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4353 !rsn.pmkid || rsn.num_pmkid != 1 ||
4354 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
4355 WPA_PMK_NAME_LEN) != 0) {
4356 wpa_printf(MSG_DEBUG,
4357 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
4361 #endif /* CONFIG_IEEE80211R */
4364 if (!elems.key_delivery) {
4365 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4369 /* Parse GTK and set the key to the driver */
4370 os_memset(&gd, 0, sizeof(gd));
4371 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4372 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4374 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4378 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4381 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4382 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
4383 gd.gtk_len, maxkeylen,
4384 &gd.key_rsc_len, &gd.alg))
4387 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
4388 gd.keyidx = kde.gtk[0] & 0x3;
4389 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
4390 !!(kde.gtk[0] & BIT(2)));
4391 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
4392 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
4393 (unsigned long) kde.gtk_len - 2);
4396 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
4398 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
4399 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
4400 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
4404 if (ieee80211w_set_keys(sm, &kde) < 0) {
4405 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
4409 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
4410 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
4411 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
4412 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
4413 keylen, (long unsigned int) sm->ptk.tk_len);
4416 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
4417 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
4418 sm->ptk.tk, keylen);
4419 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
4420 sm->ptk.tk, keylen) < 0) {
4421 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4422 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4424 alg, keylen, MAC2STR(sm->bssid));
4428 /* TODO: TK could be cleared after auth frame exchange now that driver
4429 * takes care of association frame encryption/decryption. */
4430 /* TK is not needed anymore in supplicant */
4431 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
4433 sm->ptk.installed = 1;
4435 /* FILS HLP Container */
4436 fils_process_hlp_container(sm, ie_start, end - ie_start);
4438 /* TODO: FILS IP Address Assignment */
4440 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
4441 sm->fils_completed = 1;
4449 void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
4452 sm->fils_completed = !!set;
4455 #endif /* CONFIG_FILS */
4458 int wpa_fils_is_completed(struct wpa_sm *sm)
4461 return sm && sm->fils_completed;
4462 #else /* CONFIG_FILS */
4464 #endif /* CONFIG_FILS */
4470 struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
4472 struct wpabuf *ie = NULL, *pub = NULL;
4477 else if (group == 20)
4479 else if (group == 21)
4484 crypto_ecdh_deinit(sm->owe_ecdh);
4485 sm->owe_ecdh = crypto_ecdh_init(group);
4488 sm->owe_group = group;
4489 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4490 pub = wpabuf_zeropad(pub, prime_len);
4494 ie = wpabuf_alloc(5 + wpabuf_len(pub));
4497 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
4498 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
4499 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
4500 wpabuf_put_le16(ie, group);
4501 wpabuf_put_buf(ie, pub);
4503 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
4509 crypto_ecdh_deinit(sm->owe_ecdh);
4510 sm->owe_ecdh = NULL;
4515 int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
4516 const u8 *resp_ies, size_t resp_ies_len)
4518 struct ieee802_11_elems elems;
4520 struct wpabuf *secret, *pub, *hkey;
4522 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
4523 const char *info = "OWE Key Generation";
4526 size_t hash_len, prime_len;
4527 struct wpa_ie_data data;
4530 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
4532 wpa_printf(MSG_INFO,
4533 "OWE: Could not parse Association Response frame elements");
4537 if (sm->cur_pmksa && elems.rsn_ie &&
4538 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
4540 data.num_pmkid == 1 && data.pmkid &&
4541 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
4542 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
4543 wpa_sm_set_pmk_from_pmksa(sm);
4547 if (!elems.owe_dh) {
4548 wpa_printf(MSG_INFO,
4549 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
4553 group = WPA_GET_LE16(elems.owe_dh);
4554 if (group != sm->owe_group) {
4555 wpa_printf(MSG_INFO,
4556 "OWE: Unexpected Diffie-Hellman group in response: %u",
4561 if (!sm->owe_ecdh) {
4562 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
4568 else if (group == 20)
4570 else if (group == 21)
4575 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
4577 elems.owe_dh_len - 2);
4578 secret = wpabuf_zeropad(secret, prime_len);
4580 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
4583 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
4585 /* prk = HKDF-extract(C | A | group, z) */
4587 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4589 wpabuf_clear_free(secret);
4593 /* PMKID = Truncate-128(Hash(C | A)) */
4594 addr[0] = wpabuf_head(pub);
4595 len[0] = wpabuf_len(pub);
4596 addr[1] = elems.owe_dh + 2;
4597 len[1] = elems.owe_dh_len - 2;
4599 res = sha256_vector(2, addr, len, pmkid);
4600 hash_len = SHA256_MAC_LEN;
4601 } else if (group == 20) {
4602 res = sha384_vector(2, addr, len, pmkid);
4603 hash_len = SHA384_MAC_LEN;
4604 } else if (group == 21) {
4605 res = sha512_vector(2, addr, len, pmkid);
4606 hash_len = SHA512_MAC_LEN;
4611 pub = wpabuf_zeropad(pub, prime_len);
4612 if (res < 0 || !pub) {
4614 wpabuf_clear_free(secret);
4618 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
4621 wpabuf_clear_free(secret);
4625 wpabuf_put_buf(hkey, pub); /* C */
4627 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
4628 wpabuf_put_le16(hkey, sm->owe_group); /* group */
4630 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
4631 wpabuf_head(secret), wpabuf_len(secret), prk);
4632 else if (group == 20)
4633 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
4634 wpabuf_head(secret), wpabuf_len(secret), prk);
4635 else if (group == 21)
4636 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
4637 wpabuf_head(secret), wpabuf_len(secret), prk);
4638 wpabuf_clear_free(hkey);
4639 wpabuf_clear_free(secret);
4643 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
4645 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
4648 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
4649 os_strlen(info), sm->pmk, hash_len);
4650 else if (group == 20)
4651 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
4652 os_strlen(info), sm->pmk, hash_len);
4653 else if (group == 21)
4654 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
4655 os_strlen(info), sm->pmk, hash_len);
4656 os_memset(prk, 0, SHA512_MAC_LEN);
4661 sm->pmk_len = hash_len;
4663 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
4664 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
4665 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
4666 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
4672 #endif /* CONFIG_OWE */
4675 void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
4678 if (sm && fils_cache_id) {
4679 sm->fils_cache_id_set = 1;
4680 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
4682 #endif /* CONFIG_FILS */
4687 void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
4690 wpabuf_clear_free(sm->dpp_z);
4691 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
4694 #endif /* CONFIG_DPP2 */