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?!");
388 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
389 "WPA: Failed to get master session key from "
390 "EAPOL state machines - key handshake "
393 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
394 "RSN: Cancelled PMKSA caching "
396 sm
->cur_pmksa
= NULL
;
398 } else if (!abort_cached
) {
404 if (abort_cached
&& wpa_key_mgmt_wpa_ieee8021x(sm
->key_mgmt
) &&
405 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
406 !wpa_key_mgmt_ft(sm
->key_mgmt
) && sm
->key_mgmt
!= WPA_KEY_MGMT_OSEN
)
408 /* Send EAPOL-Start to trigger full EAP authentication. */
412 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
413 "RSN: no PMKSA entry found - trigger "
414 "full EAP authentication");
415 buf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_START
,
416 NULL
, 0, &buflen
, NULL
);
418 wpa_sm_ether_send(sm
, sm
->bssid
, ETH_P_EAPOL
,
432 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
433 * @sm: Pointer to WPA state machine data from wpa_sm_init()
434 * @dst: Destination address for the frame
435 * @key: Pointer to the EAPOL-Key frame header
436 * @ver: Version bits from EAPOL-Key Key Info
437 * @nonce: Nonce value for the EAPOL-Key frame
438 * @wpa_ie: WPA/RSN IE
439 * @wpa_ie_len: Length of the WPA/RSN IE
440 * @ptk: PTK to use for keyed hash and encryption
441 * Returns: >= 0 on success, < 0 on failure
443 int wpa_supplicant_send_2_of_4(struct wpa_sm
*sm
, const unsigned char *dst
,
444 const struct wpa_eapol_key
*key
,
445 int ver
, const u8
*nonce
,
446 const u8
*wpa_ie
, size_t wpa_ie_len
,
449 size_t mic_len
, hdrlen
, rlen
;
450 struct wpa_eapol_key
*reply
;
452 u8
*rsn_ie_buf
= NULL
;
455 if (wpa_ie
== NULL
) {
456 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "WPA: No wpa_ie set - "
457 "cannot generate msg 2/4");
461 #ifdef CONFIG_IEEE80211R
462 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
466 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
467 * FTIE from (Re)Association Response.
469 rsn_ie_buf
= os_malloc(wpa_ie_len
+ 2 + 2 + PMKID_LEN
+
470 sm
->assoc_resp_ies_len
);
471 if (rsn_ie_buf
== NULL
)
473 os_memcpy(rsn_ie_buf
, wpa_ie
, wpa_ie_len
);
474 res
= wpa_insert_pmkid(rsn_ie_buf
, &wpa_ie_len
,
481 if (sm
->assoc_resp_ies
) {
482 os_memcpy(rsn_ie_buf
+ wpa_ie_len
, sm
->assoc_resp_ies
,
483 sm
->assoc_resp_ies_len
);
484 wpa_ie_len
+= sm
->assoc_resp_ies_len
;
489 #endif /* CONFIG_IEEE80211R */
491 wpa_hexdump(MSG_DEBUG
, "WPA: WPA IE for msg 2/4", wpa_ie
, wpa_ie_len
);
493 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
494 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
495 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
,
496 NULL
, hdrlen
+ wpa_ie_len
,
497 &rlen
, (void *) &reply
);
503 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
504 sm
->proto
== WPA_PROTO_OSEN
) ?
505 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
506 key_info
= ver
| WPA_KEY_INFO_KEY_TYPE
;
508 key_info
|= WPA_KEY_INFO_MIC
;
510 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
511 WPA_PUT_BE16(reply
->key_info
, key_info
);
512 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
)
513 WPA_PUT_BE16(reply
->key_length
, 0);
515 os_memcpy(reply
->key_length
, key
->key_length
, 2);
516 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
517 WPA_REPLAY_COUNTER_LEN
);
518 wpa_hexdump(MSG_DEBUG
, "WPA: Replay Counter", reply
->replay_counter
,
519 WPA_REPLAY_COUNTER_LEN
);
521 key_mic
= (u8
*) (reply
+ 1);
522 WPA_PUT_BE16(key_mic
+ mic_len
, wpa_ie_len
); /* Key Data Length */
523 os_memcpy(key_mic
+ mic_len
+ 2, wpa_ie
, wpa_ie_len
); /* Key Data */
526 os_memcpy(reply
->key_nonce
, nonce
, WPA_NONCE_LEN
);
528 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Sending EAPOL-Key 2/4");
529 return wpa_eapol_key_send(sm
, ptk
, ver
, dst
, ETH_P_EAPOL
, rbuf
, rlen
,
534 static int wpa_derive_ptk(struct wpa_sm
*sm
, const unsigned char *src_addr
,
535 const struct wpa_eapol_key
*key
, struct wpa_ptk
*ptk
)
540 #ifdef CONFIG_IEEE80211R
541 if (wpa_key_mgmt_ft(sm
->key_mgmt
))
542 return wpa_derive_ptk_ft(sm
, src_addr
, key
, ptk
);
543 #endif /* CONFIG_IEEE80211R */
546 if (sm
->key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->dpp_z
) {
547 z
= wpabuf_head(sm
->dpp_z
);
548 z_len
= wpabuf_len(sm
->dpp_z
);
550 #endif /* CONFIG_DPP2 */
552 return wpa_pmk_to_ptk(sm
->pmk
, sm
->pmk_len
, "Pairwise key expansion",
553 sm
->own_addr
, sm
->bssid
, sm
->snonce
,
554 key
->key_nonce
, ptk
, sm
->key_mgmt
,
555 sm
->pairwise_cipher
, z
, z_len
);
559 static void wpa_supplicant_process_1_of_4(struct wpa_sm
*sm
,
560 const unsigned char *src_addr
,
561 const struct wpa_eapol_key
*key
,
562 u16 ver
, const u8
*key_data
,
565 struct wpa_eapol_ie_parse ie
;
568 u8
*kde
, *kde_buf
= NULL
;
571 if (wpa_sm_get_network_ctx(sm
) == NULL
) {
572 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "WPA: No SSID info "
573 "found (msg 1 of 4)");
577 wpa_sm_set_state(sm
, WPA_4WAY_HANDSHAKE
);
578 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: RX message 1 of 4-Way "
579 "Handshake from " MACSTR
" (ver=%d)", MAC2STR(src_addr
), ver
);
581 os_memset(&ie
, 0, sizeof(ie
));
583 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) {
584 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
585 wpa_hexdump(MSG_DEBUG
, "RSN: msg 1/4 key data",
586 key_data
, key_data_len
);
587 if (wpa_supplicant_parse_ies(key_data
, key_data_len
, &ie
) < 0)
590 wpa_hexdump(MSG_DEBUG
, "RSN: PMKID from "
591 "Authenticator", ie
.pmkid
, PMKID_LEN
);
595 res
= wpa_supplicant_get_pmk(sm
, src_addr
, ie
.pmkid
);
597 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: Do not reply to "
598 "msg 1/4 - requesting full EAP authentication");
604 if (sm
->renew_snonce
) {
605 if (random_get_bytes(sm
->snonce
, WPA_NONCE_LEN
)) {
606 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
607 "WPA: Failed to get random data for SNonce");
610 sm
->renew_snonce
= 0;
611 wpa_hexdump(MSG_DEBUG
, "WPA: Renewed SNonce",
612 sm
->snonce
, WPA_NONCE_LEN
);
615 /* Calculate PTK which will be stored as a temporary PTK until it has
616 * been verified when processing message 3/4. */
618 if (wpa_derive_ptk(sm
, src_addr
, key
, ptk
) < 0)
620 if (sm
->pairwise_cipher
== WPA_CIPHER_TKIP
) {
622 /* Supplicant: swap tx/rx Mic keys */
623 os_memcpy(buf
, &ptk
->tk
[16], 8);
624 os_memcpy(&ptk
->tk
[16], &ptk
->tk
[24], 8);
625 os_memcpy(&ptk
->tk
[24], buf
, 8);
626 os_memset(buf
, 0, sizeof(buf
));
630 kde
= sm
->assoc_wpa_ie
;
631 kde_len
= sm
->assoc_wpa_ie_len
;
634 if (wpa_sm_ocv_enabled(sm
)) {
635 struct wpa_channel_info ci
;
638 if (wpa_sm_channel_info(sm
, &ci
) != 0) {
639 wpa_printf(MSG_WARNING
,
640 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
644 kde_buf
= os_malloc(kde_len
+ 2 + RSN_SELECTOR_LEN
+ 3);
646 wpa_printf(MSG_WARNING
,
647 "Failed to allocate memory for KDE with OCI in EAPOL-Key 2/4");
651 os_memcpy(kde_buf
, kde
, kde_len
);
654 if (ocv_insert_oci_kde(&ci
, &pos
) < 0)
658 #endif /* CONFIG_OCV */
662 kde_buf
= os_malloc(kde_len
+ 2 + RSN_SELECTOR_LEN
+ 1);
665 wpa_printf(MSG_DEBUG
, "P2P: Add IP Address Request KDE "
666 "into EAPOL-Key 2/4");
667 os_memcpy(kde_buf
, kde
, kde_len
);
670 *pos
++ = WLAN_EID_VENDOR_SPECIFIC
;
671 *pos
++ = RSN_SELECTOR_LEN
+ 1;
672 RSN_SELECTOR_PUT(pos
, WFA_KEY_DATA_IP_ADDR_REQ
);
673 pos
+= RSN_SELECTOR_LEN
;
678 #endif /* CONFIG_P2P */
680 if (wpa_supplicant_send_2_of_4(sm
, sm
->bssid
, key
, ver
, sm
->snonce
,
681 kde
, kde_len
, ptk
) < 0)
685 os_memcpy(sm
->anonce
, key
->key_nonce
, WPA_NONCE_LEN
);
690 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
694 static void wpa_sm_start_preauth(void *eloop_ctx
, void *timeout_ctx
)
696 struct wpa_sm
*sm
= eloop_ctx
;
697 rsn_preauth_candidate_process(sm
);
701 static void wpa_supplicant_key_neg_complete(struct wpa_sm
*sm
,
702 const u8
*addr
, int secure
)
704 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
705 "WPA: Key negotiation completed with "
706 MACSTR
" [PTK=%s GTK=%s]", MAC2STR(addr
),
707 wpa_cipher_txt(sm
->pairwise_cipher
),
708 wpa_cipher_txt(sm
->group_cipher
));
709 wpa_sm_cancel_auth_timeout(sm
);
710 wpa_sm_set_state(sm
, WPA_COMPLETED
);
713 wpa_sm_mlme_setprotection(
714 sm
, addr
, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX
,
715 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE
);
716 eapol_sm_notify_portValid(sm
->eapol
, TRUE
);
717 if (wpa_key_mgmt_wpa_psk(sm
->key_mgmt
) ||
718 sm
->key_mgmt
== WPA_KEY_MGMT_DPP
||
719 sm
->key_mgmt
== WPA_KEY_MGMT_OWE
)
720 eapol_sm_notify_eap_success(sm
->eapol
, TRUE
);
722 * Start preauthentication after a short wait to avoid a
723 * possible race condition between the data receive and key
724 * configuration after the 4-Way Handshake. This increases the
725 * likelihood of the first preauth EAPOL-Start frame getting to
728 if (!dl_list_empty(&sm
->pmksa_candidates
))
729 eloop_register_timeout(1, 0, wpa_sm_start_preauth
,
733 if (sm
->cur_pmksa
&& sm
->cur_pmksa
->opportunistic
) {
734 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
735 "RSN: Authenticator accepted "
736 "opportunistic PMKSA entry - marking it valid");
737 sm
->cur_pmksa
->opportunistic
= 0;
740 #ifdef CONFIG_IEEE80211R
741 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
742 /* Prepare for the next transition */
743 wpa_ft_prepare_auth_request(sm
, NULL
);
745 #endif /* CONFIG_IEEE80211R */
749 static void wpa_sm_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
751 struct wpa_sm
*sm
= eloop_ctx
;
752 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Request PTK rekeying");
753 wpa_sm_key_request(sm
, 0, 1);
757 static int wpa_supplicant_install_ptk(struct wpa_sm
*sm
,
758 const struct wpa_eapol_key
*key
)
764 if (sm
->ptk
.installed
) {
765 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
766 "WPA: Do not re-install same PTK to the driver");
770 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
771 "WPA: Installing PTK to the driver");
773 if (sm
->pairwise_cipher
== WPA_CIPHER_NONE
) {
774 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Pairwise Cipher "
775 "Suite: NONE - do not use pairwise keys");
779 if (!wpa_cipher_valid_pairwise(sm
->pairwise_cipher
)) {
780 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
781 "WPA: Unsupported pairwise cipher %d",
782 sm
->pairwise_cipher
);
786 alg
= wpa_cipher_to_alg(sm
->pairwise_cipher
);
787 keylen
= wpa_cipher_key_len(sm
->pairwise_cipher
);
788 if (keylen
<= 0 || (unsigned int) keylen
!= sm
->ptk
.tk_len
) {
789 wpa_printf(MSG_DEBUG
, "WPA: TK length mismatch: %d != %lu",
790 keylen
, (long unsigned int) sm
->ptk
.tk_len
);
793 rsclen
= wpa_cipher_rsc_len(sm
->pairwise_cipher
);
795 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) {
798 key_rsc
= key
->key_rsc
;
799 wpa_hexdump(MSG_DEBUG
, "WPA: RSC", key_rsc
, rsclen
);
802 if (wpa_sm_set_key(sm
, alg
, sm
->bssid
, 0, 1, key_rsc
, rsclen
,
803 sm
->ptk
.tk
, keylen
) < 0) {
804 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
805 "WPA: Failed to set PTK to the "
806 "driver (alg=%d keylen=%d bssid=" MACSTR
")",
807 alg
, keylen
, MAC2STR(sm
->bssid
));
811 /* TK is not needed anymore in supplicant */
812 os_memset(sm
->ptk
.tk
, 0, WPA_TK_MAX_LEN
);
814 sm
->ptk
.installed
= 1;
816 if (sm
->wpa_ptk_rekey
) {
817 eloop_cancel_timeout(wpa_sm_rekey_ptk
, sm
, NULL
);
818 eloop_register_timeout(sm
->wpa_ptk_rekey
, 0, wpa_sm_rekey_ptk
,
826 static int wpa_supplicant_check_group_cipher(struct wpa_sm
*sm
,
828 int keylen
, int maxkeylen
,
834 *alg
= wpa_cipher_to_alg(group_cipher
);
835 if (*alg
== WPA_ALG_NONE
) {
836 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
837 "WPA: Unsupported Group Cipher %d",
841 *key_rsc_len
= wpa_cipher_rsc_len(group_cipher
);
843 klen
= wpa_cipher_key_len(group_cipher
);
844 if (keylen
!= klen
|| maxkeylen
< klen
) {
845 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
846 "WPA: Unsupported %s Group Cipher key length %d (%d)",
847 wpa_cipher_txt(group_cipher
), keylen
, maxkeylen
);
854 struct wpa_gtk_data
{
856 int tx
, key_rsc_len
, keyidx
;
862 static int wpa_supplicant_install_gtk(struct wpa_sm
*sm
,
863 const struct wpa_gtk_data
*gd
,
864 const u8
*key_rsc
, int wnm_sleep
)
866 const u8
*_gtk
= gd
->gtk
;
869 /* Detect possible key reinstallation */
870 if ((sm
->gtk
.gtk_len
== (size_t) gd
->gtk_len
&&
871 os_memcmp(sm
->gtk
.gtk
, gd
->gtk
, sm
->gtk
.gtk_len
) == 0) ||
872 (sm
->gtk_wnm_sleep
.gtk_len
== (size_t) gd
->gtk_len
&&
873 os_memcmp(sm
->gtk_wnm_sleep
.gtk
, gd
->gtk
,
874 sm
->gtk_wnm_sleep
.gtk_len
) == 0)) {
875 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
876 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
877 gd
->keyidx
, gd
->tx
, gd
->gtk_len
);
881 wpa_hexdump_key(MSG_DEBUG
, "WPA: Group Key", gd
->gtk
, gd
->gtk_len
);
882 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
883 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
884 gd
->keyidx
, gd
->tx
, gd
->gtk_len
);
885 wpa_hexdump(MSG_DEBUG
, "WPA: RSC", key_rsc
, gd
->key_rsc_len
);
886 if (sm
->group_cipher
== WPA_CIPHER_TKIP
) {
887 /* Swap Tx/Rx keys for Michael MIC */
888 os_memcpy(gtk_buf
, gd
->gtk
, 16);
889 os_memcpy(gtk_buf
+ 16, gd
->gtk
+ 24, 8);
890 os_memcpy(gtk_buf
+ 24, gd
->gtk
+ 16, 8);
893 if (sm
->pairwise_cipher
== WPA_CIPHER_NONE
) {
894 if (wpa_sm_set_key(sm
, gd
->alg
, NULL
,
895 gd
->keyidx
, 1, key_rsc
, gd
->key_rsc_len
,
896 _gtk
, gd
->gtk_len
) < 0) {
897 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
898 "WPA: Failed to set GTK to the driver "
900 os_memset(gtk_buf
, 0, sizeof(gtk_buf
));
903 } else if (wpa_sm_set_key(sm
, gd
->alg
, broadcast_ether_addr
,
904 gd
->keyidx
, gd
->tx
, key_rsc
, gd
->key_rsc_len
,
905 _gtk
, gd
->gtk_len
) < 0) {
906 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
907 "WPA: Failed to set GTK to "
908 "the driver (alg=%d keylen=%d keyidx=%d)",
909 gd
->alg
, gd
->gtk_len
, gd
->keyidx
);
910 os_memset(gtk_buf
, 0, sizeof(gtk_buf
));
913 os_memset(gtk_buf
, 0, sizeof(gtk_buf
));
916 sm
->gtk_wnm_sleep
.gtk_len
= gd
->gtk_len
;
917 os_memcpy(sm
->gtk_wnm_sleep
.gtk
, gd
->gtk
,
918 sm
->gtk_wnm_sleep
.gtk_len
);
920 sm
->gtk
.gtk_len
= gd
->gtk_len
;
921 os_memcpy(sm
->gtk
.gtk
, gd
->gtk
, sm
->gtk
.gtk_len
);
928 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm
*sm
,
931 if (tx
&& sm
->pairwise_cipher
!= WPA_CIPHER_NONE
) {
932 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
933 * seemed to set this bit (incorrectly, since Tx is only when
934 * doing Group Key only APs) and without this workaround, the
935 * data connection does not work because wpa_supplicant
936 * configured non-zero keyidx to be used for unicast. */
937 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
938 "WPA: Tx bit set for GTK, but pairwise "
939 "keys are used - ignore Tx bit");
946 static int wpa_supplicant_rsc_relaxation(const struct wpa_sm
*sm
,
951 if (!sm
->wpa_rsc_relaxation
)
954 rsclen
= wpa_cipher_rsc_len(sm
->group_cipher
);
957 * Try to detect RSC (endian) corruption issue where the AP sends
958 * the RSC bytes in EAPOL-Key message in the wrong order, both if
959 * it's actually a 6-byte field (as it should be) and if it treats
960 * it as an 8-byte field.
961 * An AP model known to have this bug is the Sapido RB-1632.
963 if (rsclen
== 6 && ((rsc
[5] && !rsc
[0]) || rsc
[6] || rsc
[7])) {
964 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
965 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
966 rsc
[0], rsc
[1], rsc
[2], rsc
[3],
967 rsc
[4], rsc
[5], rsc
[6], rsc
[7]);
976 static int wpa_supplicant_pairwise_gtk(struct wpa_sm
*sm
,
977 const struct wpa_eapol_key
*key
,
978 const u8
*gtk
, size_t gtk_len
,
981 struct wpa_gtk_data gd
;
985 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
987 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
988 * Reserved [bits 0-7]
992 os_memset(&gd
, 0, sizeof(gd
));
993 wpa_hexdump_key(MSG_DEBUG
, "RSN: received GTK in pairwise handshake",
996 if (gtk_len
< 2 || gtk_len
- 2 > sizeof(gd
.gtk
))
999 gd
.keyidx
= gtk
[0] & 0x3;
1000 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
1001 !!(gtk
[0] & BIT(2)));
1005 os_memcpy(gd
.gtk
, gtk
, gtk_len
);
1006 gd
.gtk_len
= gtk_len
;
1008 key_rsc
= key
->key_rsc
;
1009 if (wpa_supplicant_rsc_relaxation(sm
, key
->key_rsc
))
1012 if (sm
->group_cipher
!= WPA_CIPHER_GTK_NOT_USED
&&
1013 (wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
1015 &gd
.key_rsc_len
, &gd
.alg
) ||
1016 wpa_supplicant_install_gtk(sm
, &gd
, key_rsc
, 0))) {
1017 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1018 "RSN: Failed to install GTK");
1019 os_memset(&gd
, 0, sizeof(gd
));
1022 os_memset(&gd
, 0, sizeof(gd
));
1028 #ifdef CONFIG_IEEE80211W
1029 static int wpa_supplicant_install_igtk(struct wpa_sm
*sm
,
1030 const struct wpa_igtk_kde
*igtk
,
1033 size_t len
= wpa_cipher_key_len(sm
->mgmt_group_cipher
);
1034 u16 keyidx
= WPA_GET_LE16(igtk
->keyid
);
1036 /* Detect possible key reinstallation */
1037 if ((sm
->igtk
.igtk_len
== len
&&
1038 os_memcmp(sm
->igtk
.igtk
, igtk
->igtk
, sm
->igtk
.igtk_len
) == 0) ||
1039 (sm
->igtk_wnm_sleep
.igtk_len
== len
&&
1040 os_memcmp(sm
->igtk_wnm_sleep
.igtk
, igtk
->igtk
,
1041 sm
->igtk_wnm_sleep
.igtk_len
) == 0)) {
1042 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1043 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1048 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1049 "WPA: IGTK keyid %d pn " COMPACT_MACSTR
,
1050 keyidx
, MAC2STR(igtk
->pn
));
1051 wpa_hexdump_key(MSG_DEBUG
, "WPA: IGTK", igtk
->igtk
, len
);
1052 if (keyidx
> 4095) {
1053 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1054 "WPA: Invalid IGTK KeyID %d", keyidx
);
1057 if (wpa_sm_set_key(sm
, wpa_cipher_to_alg(sm
->mgmt_group_cipher
),
1058 broadcast_ether_addr
,
1059 keyidx
, 0, igtk
->pn
, sizeof(igtk
->pn
),
1060 igtk
->igtk
, len
) < 0) {
1061 if (keyidx
== 0x0400 || keyidx
== 0x0500) {
1062 /* Assume the AP has broken PMF implementation since it
1063 * seems to have swapped the KeyID bytes. The AP cannot
1064 * be trusted to implement BIP correctly or provide a
1065 * valid IGTK, so do not try to configure this key with
1066 * swapped KeyID bytes. Instead, continue without
1067 * configuring the IGTK so that the driver can drop any
1068 * received group-addressed robust management frames due
1071 * Normally, this error behavior would result in us
1072 * disconnecting, but there are number of deployed APs
1073 * with this broken behavior, so as an interoperability
1074 * workaround, allow the connection to proceed. */
1075 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1076 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1078 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1079 "WPA: Failed to configure IGTK to the driver");
1085 sm
->igtk_wnm_sleep
.igtk_len
= len
;
1086 os_memcpy(sm
->igtk_wnm_sleep
.igtk
, igtk
->igtk
,
1087 sm
->igtk_wnm_sleep
.igtk_len
);
1089 sm
->igtk
.igtk_len
= len
;
1090 os_memcpy(sm
->igtk
.igtk
, igtk
->igtk
, sm
->igtk
.igtk_len
);
1095 #endif /* CONFIG_IEEE80211W */
1098 static int ieee80211w_set_keys(struct wpa_sm
*sm
,
1099 struct wpa_eapol_ie_parse
*ie
)
1101 #ifdef CONFIG_IEEE80211W
1102 if (!wpa_cipher_valid_mgmt_group(sm
->mgmt_group_cipher
))
1107 const struct wpa_igtk_kde
*igtk
;
1109 len
= wpa_cipher_key_len(sm
->mgmt_group_cipher
);
1110 if (ie
->igtk_len
!= WPA_IGTK_KDE_PREFIX_LEN
+ len
)
1113 igtk
= (const struct wpa_igtk_kde
*) ie
->igtk
;
1114 if (wpa_supplicant_install_igtk(sm
, igtk
, 0) < 0)
1119 #else /* CONFIG_IEEE80211W */
1121 #endif /* CONFIG_IEEE80211W */
1125 static void wpa_report_ie_mismatch(struct wpa_sm
*sm
,
1126 const char *reason
, const u8
*src_addr
,
1127 const u8
*wpa_ie
, size_t wpa_ie_len
,
1128 const u8
*rsn_ie
, size_t rsn_ie_len
)
1130 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "WPA: %s (src=" MACSTR
")",
1131 reason
, MAC2STR(src_addr
));
1133 if (sm
->ap_wpa_ie
) {
1134 wpa_hexdump(MSG_INFO
, "WPA: WPA IE in Beacon/ProbeResp",
1135 sm
->ap_wpa_ie
, sm
->ap_wpa_ie_len
);
1138 if (!sm
->ap_wpa_ie
) {
1139 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1140 "WPA: No WPA IE in Beacon/ProbeResp");
1142 wpa_hexdump(MSG_INFO
, "WPA: WPA IE in 3/4 msg",
1143 wpa_ie
, wpa_ie_len
);
1146 if (sm
->ap_rsn_ie
) {
1147 wpa_hexdump(MSG_INFO
, "WPA: RSN IE in Beacon/ProbeResp",
1148 sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
);
1151 if (!sm
->ap_rsn_ie
) {
1152 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1153 "WPA: No RSN IE in Beacon/ProbeResp");
1155 wpa_hexdump(MSG_INFO
, "WPA: RSN IE in 3/4 msg",
1156 rsn_ie
, rsn_ie_len
);
1159 wpa_sm_deauthenticate(sm
, WLAN_REASON_IE_IN_4WAY_DIFFERS
);
1163 #ifdef CONFIG_IEEE80211R
1165 static int ft_validate_mdie(struct wpa_sm
*sm
,
1166 const unsigned char *src_addr
,
1167 struct wpa_eapol_ie_parse
*ie
,
1168 const u8
*assoc_resp_mdie
)
1170 struct rsn_mdie
*mdie
;
1172 mdie
= (struct rsn_mdie
*) (ie
->mdie
+ 2);
1173 if (ie
->mdie
== NULL
|| ie
->mdie_len
< 2 + sizeof(*mdie
) ||
1174 os_memcmp(mdie
->mobility_domain
, sm
->mobility_domain
,
1175 MOBILITY_DOMAIN_ID_LEN
) != 0) {
1176 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: MDIE in msg 3/4 did "
1177 "not match with the current mobility domain");
1181 if (assoc_resp_mdie
&&
1182 (assoc_resp_mdie
[1] != ie
->mdie
[1] ||
1183 os_memcmp(assoc_resp_mdie
, ie
->mdie
, 2 + ie
->mdie
[1]) != 0)) {
1184 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: MDIE mismatch");
1185 wpa_hexdump(MSG_DEBUG
, "FT: MDIE in EAPOL-Key msg 3/4",
1186 ie
->mdie
, 2 + ie
->mdie
[1]);
1187 wpa_hexdump(MSG_DEBUG
, "FT: MDIE in (Re)Association Response",
1188 assoc_resp_mdie
, 2 + assoc_resp_mdie
[1]);
1196 static int ft_validate_ftie(struct wpa_sm
*sm
,
1197 const unsigned char *src_addr
,
1198 struct wpa_eapol_ie_parse
*ie
,
1199 const u8
*assoc_resp_ftie
)
1201 if (ie
->ftie
== NULL
) {
1202 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1203 "FT: No FTIE in EAPOL-Key msg 3/4");
1207 if (assoc_resp_ftie
== NULL
)
1210 if (assoc_resp_ftie
[1] != ie
->ftie
[1] ||
1211 os_memcmp(assoc_resp_ftie
, ie
->ftie
, 2 + ie
->ftie
[1]) != 0) {
1212 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: FTIE mismatch");
1213 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 3/4",
1214 ie
->ftie
, 2 + ie
->ftie
[1]);
1215 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)Association Response",
1216 assoc_resp_ftie
, 2 + assoc_resp_ftie
[1]);
1224 static int ft_validate_rsnie(struct wpa_sm
*sm
,
1225 const unsigned char *src_addr
,
1226 struct wpa_eapol_ie_parse
*ie
)
1228 struct wpa_ie_data rsn
;
1234 * Verify that PMKR1Name from EAPOL-Key message 3/4
1235 * matches with the value we derived.
1237 if (wpa_parse_wpa_ie_rsn(ie
->rsn_ie
, ie
->rsn_ie_len
, &rsn
) < 0 ||
1238 rsn
.num_pmkid
!= 1 || rsn
.pmkid
== NULL
) {
1239 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: No PMKR1Name in "
1240 "FT 4-way handshake message 3/4");
1244 if (os_memcmp_const(rsn
.pmkid
, sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
) != 0)
1246 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1247 "FT: PMKR1Name mismatch in "
1248 "FT 4-way handshake message 3/4");
1249 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Authenticator",
1250 rsn
.pmkid
, WPA_PMK_NAME_LEN
);
1251 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
1252 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
1260 static int wpa_supplicant_validate_ie_ft(struct wpa_sm
*sm
,
1261 const unsigned char *src_addr
,
1262 struct wpa_eapol_ie_parse
*ie
)
1264 const u8
*pos
, *end
, *mdie
= NULL
, *ftie
= NULL
;
1266 if (sm
->assoc_resp_ies
) {
1267 pos
= sm
->assoc_resp_ies
;
1268 end
= pos
+ sm
->assoc_resp_ies_len
;
1269 while (end
- pos
> 2) {
1270 if (2 + pos
[1] > end
- pos
)
1273 case WLAN_EID_MOBILITY_DOMAIN
:
1276 case WLAN_EID_FAST_BSS_TRANSITION
:
1284 if (ft_validate_mdie(sm
, src_addr
, ie
, mdie
) < 0 ||
1285 ft_validate_ftie(sm
, src_addr
, ie
, ftie
) < 0 ||
1286 ft_validate_rsnie(sm
, src_addr
, ie
) < 0)
1292 #endif /* CONFIG_IEEE80211R */
1295 static int wpa_supplicant_validate_ie(struct wpa_sm
*sm
,
1296 const unsigned char *src_addr
,
1297 struct wpa_eapol_ie_parse
*ie
)
1299 if (sm
->ap_wpa_ie
== NULL
&& sm
->ap_rsn_ie
== NULL
) {
1300 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1301 "WPA: No WPA/RSN IE for this AP known. "
1302 "Trying to get from scan results");
1303 if (wpa_sm_get_beacon_ie(sm
) < 0) {
1304 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1305 "WPA: Could not find AP from "
1306 "the scan results");
1308 wpa_msg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1309 "WPA: Found the current AP from "
1310 "updated scan results");
1314 if (ie
->wpa_ie
== NULL
&& ie
->rsn_ie
== NULL
&&
1315 (sm
->ap_wpa_ie
|| sm
->ap_rsn_ie
)) {
1316 wpa_report_ie_mismatch(sm
, "IE in 3/4 msg does not match "
1317 "with IE in Beacon/ProbeResp (no IE?)",
1318 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
1319 ie
->rsn_ie
, ie
->rsn_ie_len
);
1323 if ((ie
->wpa_ie
&& sm
->ap_wpa_ie
&&
1324 (ie
->wpa_ie_len
!= sm
->ap_wpa_ie_len
||
1325 os_memcmp(ie
->wpa_ie
, sm
->ap_wpa_ie
, ie
->wpa_ie_len
) != 0)) ||
1326 (ie
->rsn_ie
&& sm
->ap_rsn_ie
&&
1327 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm
->key_mgmt
),
1328 sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
,
1329 ie
->rsn_ie
, ie
->rsn_ie_len
))) {
1330 wpa_report_ie_mismatch(sm
, "IE in 3/4 msg does not match "
1331 "with IE in Beacon/ProbeResp",
1332 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
1333 ie
->rsn_ie
, ie
->rsn_ie_len
);
1337 if (sm
->proto
== WPA_PROTO_WPA
&&
1338 ie
->rsn_ie
&& sm
->ap_rsn_ie
== NULL
&& sm
->rsn_enabled
) {
1339 wpa_report_ie_mismatch(sm
, "Possible downgrade attack "
1340 "detected - RSN was enabled and RSN IE "
1341 "was in msg 3/4, but not in "
1343 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
1344 ie
->rsn_ie
, ie
->rsn_ie_len
);
1348 #ifdef CONFIG_IEEE80211R
1349 if (wpa_key_mgmt_ft(sm
->key_mgmt
) &&
1350 wpa_supplicant_validate_ie_ft(sm
, src_addr
, ie
) < 0)
1352 #endif /* CONFIG_IEEE80211R */
1359 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1360 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1361 * @dst: Destination address for the frame
1362 * @key: Pointer to the EAPOL-Key frame header
1363 * @ver: Version bits from EAPOL-Key Key Info
1364 * @key_info: Key Info
1365 * @ptk: PTK to use for keyed hash and encryption
1366 * Returns: >= 0 on success, < 0 on failure
1368 int wpa_supplicant_send_4_of_4(struct wpa_sm
*sm
, const unsigned char *dst
,
1369 const struct wpa_eapol_key
*key
,
1370 u16 ver
, u16 key_info
,
1371 struct wpa_ptk
*ptk
)
1373 size_t mic_len
, hdrlen
, rlen
;
1374 struct wpa_eapol_key
*reply
;
1377 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
1378 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
1379 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
1380 hdrlen
, &rlen
, (void *) &reply
);
1384 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
1385 sm
->proto
== WPA_PROTO_OSEN
) ?
1386 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1387 key_info
&= WPA_KEY_INFO_SECURE
;
1388 key_info
|= ver
| WPA_KEY_INFO_KEY_TYPE
;
1390 key_info
|= WPA_KEY_INFO_MIC
;
1392 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1393 WPA_PUT_BE16(reply
->key_info
, key_info
);
1394 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
)
1395 WPA_PUT_BE16(reply
->key_length
, 0);
1397 os_memcpy(reply
->key_length
, key
->key_length
, 2);
1398 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
1399 WPA_REPLAY_COUNTER_LEN
);
1401 key_mic
= (u8
*) (reply
+ 1);
1402 WPA_PUT_BE16(key_mic
+ mic_len
, 0);
1404 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Sending EAPOL-Key 4/4");
1405 return wpa_eapol_key_send(sm
, ptk
, ver
, dst
, ETH_P_EAPOL
, rbuf
, rlen
,
1410 static void wpa_supplicant_process_3_of_4(struct wpa_sm
*sm
,
1411 const struct wpa_eapol_key
*key
,
1412 u16 ver
, const u8
*key_data
,
1413 size_t key_data_len
)
1415 u16 key_info
, keylen
;
1416 struct wpa_eapol_ie_parse ie
;
1418 wpa_sm_set_state(sm
, WPA_4WAY_HANDSHAKE
);
1419 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: RX message 3 of 4-Way "
1420 "Handshake from " MACSTR
" (ver=%d)", MAC2STR(sm
->bssid
), ver
);
1422 key_info
= WPA_GET_BE16(key
->key_info
);
1424 wpa_hexdump(MSG_DEBUG
, "WPA: IE KeyData", key_data
, key_data_len
);
1425 if (wpa_supplicant_parse_ies(key_data
, key_data_len
, &ie
) < 0)
1427 if (ie
.gtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1428 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1429 "WPA: GTK IE in unencrypted key data");
1432 #ifdef CONFIG_IEEE80211W
1433 if (ie
.igtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1434 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1435 "WPA: IGTK KDE in unencrypted key data");
1440 wpa_cipher_valid_mgmt_group(sm
->mgmt_group_cipher
) &&
1441 ie
.igtk_len
!= WPA_IGTK_KDE_PREFIX_LEN
+
1442 (unsigned int) wpa_cipher_key_len(sm
->mgmt_group_cipher
)) {
1443 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1444 "WPA: Invalid IGTK KDE length %lu",
1445 (unsigned long) ie
.igtk_len
);
1448 #endif /* CONFIG_IEEE80211W */
1450 if (wpa_supplicant_validate_ie(sm
, sm
->bssid
, &ie
) < 0)
1453 if (os_memcmp(sm
->anonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0) {
1454 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1455 "WPA: ANonce from message 1 of 4-Way Handshake "
1456 "differs from 3 of 4-Way Handshake - drop packet (src="
1457 MACSTR
")", MAC2STR(sm
->bssid
));
1461 keylen
= WPA_GET_BE16(key
->key_length
);
1462 if (keylen
!= wpa_cipher_key_len(sm
->pairwise_cipher
)) {
1463 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1464 "WPA: Invalid %s key length %d (src=" MACSTR
1465 ")", wpa_cipher_txt(sm
->pairwise_cipher
), keylen
,
1466 MAC2STR(sm
->bssid
));
1471 if (ie
.ip_addr_alloc
) {
1472 os_memcpy(sm
->p2p_ip_addr
, ie
.ip_addr_alloc
, 3 * 4);
1473 wpa_hexdump(MSG_DEBUG
, "P2P: IP address info",
1474 sm
->p2p_ip_addr
, sizeof(sm
->p2p_ip_addr
));
1476 #endif /* CONFIG_P2P */
1479 if (wpa_sm_ocv_enabled(sm
)) {
1480 struct wpa_channel_info ci
;
1482 if (wpa_sm_channel_info(sm
, &ci
) != 0) {
1483 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1484 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1488 if (ocv_verify_tx_params(ie
.oci
, ie
.oci_len
, &ci
,
1489 channel_width_to_int(ci
.chanwidth
),
1490 ci
.seg1_idx
) != 0) {
1491 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "%s",
1496 #endif /* CONFIG_OCV */
1498 if (wpa_supplicant_send_4_of_4(sm
, sm
->bssid
, key
, ver
, key_info
,
1503 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1504 * for the next 4-Way Handshake. If msg 3 is received again, the old
1505 * SNonce will still be used to avoid changing PTK. */
1506 sm
->renew_snonce
= 1;
1508 if (key_info
& WPA_KEY_INFO_INSTALL
) {
1509 if (wpa_supplicant_install_ptk(sm
, key
))
1513 if (key_info
& WPA_KEY_INFO_SECURE
) {
1514 wpa_sm_mlme_setprotection(
1515 sm
, sm
->bssid
, MLME_SETPROTECTION_PROTECT_TYPE_RX
,
1516 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE
);
1517 eapol_sm_notify_portValid(sm
->eapol
, TRUE
);
1519 wpa_sm_set_state(sm
, WPA_GROUP_HANDSHAKE
);
1521 if (sm
->group_cipher
== WPA_CIPHER_GTK_NOT_USED
) {
1522 /* No GTK to be set to the driver */
1523 } else if (!ie
.gtk
&& sm
->proto
== WPA_PROTO_RSN
) {
1524 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1525 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
1527 } else if (ie
.gtk
&&
1528 wpa_supplicant_pairwise_gtk(sm
, key
,
1529 ie
.gtk
, ie
.gtk_len
, key_info
) < 0) {
1530 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1531 "RSN: Failed to configure GTK");
1535 if (ieee80211w_set_keys(sm
, &ie
) < 0) {
1536 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1537 "RSN: Failed to configure IGTK");
1541 if (sm
->group_cipher
== WPA_CIPHER_GTK_NOT_USED
|| ie
.gtk
)
1542 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
,
1543 key_info
& WPA_KEY_INFO_SECURE
);
1546 wpa_sm_set_rekey_offload(sm
);
1548 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1549 * calculated only after KCK has been derived. Though, do not replace an
1550 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1551 * to avoid unnecessary changes of PMKID while continuing to use the
1553 if (sm
->proto
== WPA_PROTO_RSN
&& wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
1555 struct rsn_pmksa_cache_entry
*sa
;
1557 sa
= pmksa_cache_add(sm
->pmksa
, sm
->pmk
, sm
->pmk_len
, NULL
,
1558 sm
->ptk
.kck
, sm
->ptk
.kck_len
,
1559 sm
->bssid
, sm
->own_addr
,
1560 sm
->network_ctx
, sm
->key_mgmt
, NULL
);
1565 sm
->msg_3_of_4_ok
= 1;
1569 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
1573 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm
*sm
,
1577 struct wpa_gtk_data
*gd
)
1580 struct wpa_eapol_ie_parse ie
;
1582 wpa_hexdump_key(MSG_DEBUG
, "RSN: msg 1/2 key data",
1583 keydata
, keydatalen
);
1584 if (wpa_supplicant_parse_ies(keydata
, keydatalen
, &ie
) < 0)
1586 if (ie
.gtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1587 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1588 "WPA: GTK IE in unencrypted key data");
1591 if (ie
.gtk
== NULL
) {
1592 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1593 "WPA: No GTK IE in Group Key msg 1/2");
1596 maxkeylen
= gd
->gtk_len
= ie
.gtk_len
- 2;
1599 if (wpa_sm_ocv_enabled(sm
)) {
1600 struct wpa_channel_info ci
;
1602 if (wpa_sm_channel_info(sm
, &ci
) != 0) {
1603 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1604 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1608 if (ocv_verify_tx_params(ie
.oci
, ie
.oci_len
, &ci
,
1609 channel_width_to_int(ci
.chanwidth
),
1610 ci
.seg1_idx
) != 0) {
1611 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "%s",
1616 #endif /* CONFIG_OCV */
1618 if (wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
1619 gd
->gtk_len
, maxkeylen
,
1620 &gd
->key_rsc_len
, &gd
->alg
))
1623 wpa_hexdump_key(MSG_DEBUG
, "RSN: received GTK in group key handshake",
1624 ie
.gtk
, ie
.gtk_len
);
1625 gd
->keyidx
= ie
.gtk
[0] & 0x3;
1626 gd
->tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
1627 !!(ie
.gtk
[0] & BIT(2)));
1628 if (ie
.gtk_len
- 2 > sizeof(gd
->gtk
)) {
1629 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1630 "RSN: Too long GTK in GTK IE (len=%lu)",
1631 (unsigned long) ie
.gtk_len
- 2);
1634 os_memcpy(gd
->gtk
, ie
.gtk
+ 2, ie
.gtk_len
- 2);
1636 if (ieee80211w_set_keys(sm
, &ie
) < 0)
1637 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1638 "RSN: Failed to configure IGTK");
1644 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm
*sm
,
1645 const struct wpa_eapol_key
*key
,
1647 size_t key_data_len
, u16 key_info
,
1648 u16 ver
, struct wpa_gtk_data
*gd
)
1653 gtk_len
= WPA_GET_BE16(key
->key_length
);
1654 maxkeylen
= key_data_len
;
1655 if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1656 if (maxkeylen
< 8) {
1657 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1658 "WPA: Too short maxkeylen (%lu)",
1659 (unsigned long) maxkeylen
);
1665 if (gtk_len
> maxkeylen
||
1666 wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
1668 &gd
->key_rsc_len
, &gd
->alg
))
1671 gd
->gtk_len
= gtk_len
;
1672 gd
->keyidx
= (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) >>
1673 WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1674 if (ver
== WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&& sm
->ptk
.kek_len
== 16) {
1675 #ifdef CONFIG_NO_RC4
1676 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1677 "WPA: RC4 not supported in the build");
1679 #else /* CONFIG_NO_RC4 */
1681 if (key_data_len
> sizeof(gd
->gtk
)) {
1682 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1683 "WPA: RC4 key data too long (%lu)",
1684 (unsigned long) key_data_len
);
1687 os_memcpy(ek
, key
->key_iv
, 16);
1688 os_memcpy(ek
+ 16, sm
->ptk
.kek
, sm
->ptk
.kek_len
);
1689 os_memcpy(gd
->gtk
, key_data
, key_data_len
);
1690 if (rc4_skip(ek
, 32, 256, gd
->gtk
, key_data_len
)) {
1691 os_memset(ek
, 0, sizeof(ek
));
1692 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
1696 os_memset(ek
, 0, sizeof(ek
));
1697 #endif /* CONFIG_NO_RC4 */
1698 } else if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1699 if (maxkeylen
% 8) {
1700 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1701 "WPA: Unsupported AES-WRAP len %lu",
1702 (unsigned long) maxkeylen
);
1705 if (maxkeylen
> sizeof(gd
->gtk
)) {
1706 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1707 "WPA: AES-WRAP key data "
1708 "too long (keydatalen=%lu maxkeylen=%lu)",
1709 (unsigned long) key_data_len
,
1710 (unsigned long) maxkeylen
);
1713 if (aes_unwrap(sm
->ptk
.kek
, sm
->ptk
.kek_len
, maxkeylen
/ 8,
1714 key_data
, gd
->gtk
)) {
1715 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1716 "WPA: AES unwrap failed - could not decrypt "
1721 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1722 "WPA: Unsupported key_info type %d", ver
);
1725 gd
->tx
= wpa_supplicant_gtk_tx_bit_workaround(
1726 sm
, !!(key_info
& WPA_KEY_INFO_TXRX
));
1731 static int wpa_supplicant_send_2_of_2(struct wpa_sm
*sm
,
1732 const struct wpa_eapol_key
*key
,
1733 int ver
, u16 key_info
)
1735 size_t mic_len
, hdrlen
, rlen
;
1736 struct wpa_eapol_key
*reply
;
1741 if (wpa_sm_ocv_enabled(sm
))
1742 kde_len
= OCV_OCI_KDE_LEN
;
1743 #endif /* CONFIG_OCV */
1745 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
1746 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
1747 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
1748 hdrlen
+ kde_len
, &rlen
, (void *) &reply
);
1752 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
1753 sm
->proto
== WPA_PROTO_OSEN
) ?
1754 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1755 key_info
&= WPA_KEY_INFO_KEY_INDEX_MASK
;
1756 key_info
|= ver
| WPA_KEY_INFO_SECURE
;
1758 key_info
|= WPA_KEY_INFO_MIC
;
1760 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1761 WPA_PUT_BE16(reply
->key_info
, key_info
);
1762 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
)
1763 WPA_PUT_BE16(reply
->key_length
, 0);
1765 os_memcpy(reply
->key_length
, key
->key_length
, 2);
1766 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
1767 WPA_REPLAY_COUNTER_LEN
);
1769 key_mic
= (u8
*) (reply
+ 1);
1770 WPA_PUT_BE16(key_mic
+ mic_len
, kde_len
); /* Key Data Length */
1773 if (wpa_sm_ocv_enabled(sm
)) {
1774 struct wpa_channel_info ci
;
1777 if (wpa_sm_channel_info(sm
, &ci
) != 0) {
1778 wpa_printf(MSG_WARNING
,
1779 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
1784 pos
= key_mic
+ mic_len
+ 2; /* Key Data */
1785 if (ocv_insert_oci_kde(&ci
, &pos
) < 0) {
1790 #endif /* CONFIG_OCV */
1792 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Sending EAPOL-Key 2/2");
1793 return wpa_eapol_key_send(sm
, &sm
->ptk
, ver
, sm
->bssid
, ETH_P_EAPOL
,
1794 rbuf
, rlen
, key_mic
);
1798 static void wpa_supplicant_process_1_of_2(struct wpa_sm
*sm
,
1799 const unsigned char *src_addr
,
1800 const struct wpa_eapol_key
*key
,
1802 size_t key_data_len
, u16 ver
)
1806 struct wpa_gtk_data gd
;
1809 if (!sm
->msg_3_of_4_ok
&& !wpa_fils_is_completed(sm
)) {
1810 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1811 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1815 os_memset(&gd
, 0, sizeof(gd
));
1817 rekey
= wpa_sm_get_state(sm
) == WPA_COMPLETED
;
1818 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: RX message 1 of Group Key "
1819 "Handshake from " MACSTR
" (ver=%d)", MAC2STR(src_addr
), ver
);
1821 key_info
= WPA_GET_BE16(key
->key_info
);
1823 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) {
1824 ret
= wpa_supplicant_process_1_of_2_rsn(sm
, key_data
,
1825 key_data_len
, key_info
,
1828 ret
= wpa_supplicant_process_1_of_2_wpa(sm
, key
, key_data
,
1830 key_info
, ver
, &gd
);
1833 wpa_sm_set_state(sm
, WPA_GROUP_HANDSHAKE
);
1838 key_rsc
= key
->key_rsc
;
1839 if (wpa_supplicant_rsc_relaxation(sm
, key
->key_rsc
))
1842 if (wpa_supplicant_install_gtk(sm
, &gd
, key_rsc
, 0) ||
1843 wpa_supplicant_send_2_of_2(sm
, key
, ver
, key_info
) < 0)
1845 os_memset(&gd
, 0, sizeof(gd
));
1848 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
, "WPA: Group rekeying "
1849 "completed with " MACSTR
" [GTK=%s]",
1850 MAC2STR(sm
->bssid
), wpa_cipher_txt(sm
->group_cipher
));
1851 wpa_sm_cancel_auth_timeout(sm
);
1852 wpa_sm_set_state(sm
, WPA_COMPLETED
);
1854 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
,
1856 WPA_KEY_INFO_SECURE
);
1859 wpa_sm_set_rekey_offload(sm
);
1864 os_memset(&gd
, 0, sizeof(gd
));
1865 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
1869 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm
*sm
,
1870 struct wpa_eapol_key
*key
,
1872 const u8
*buf
, size_t len
)
1874 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
];
1876 size_t mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
1878 os_memcpy(mic
, key
+ 1, mic_len
);
1880 os_memset(key
+ 1, 0, mic_len
);
1881 if (wpa_eapol_key_mic(sm
->tptk
.kck
, sm
->tptk
.kck_len
,
1883 ver
, buf
, len
, (u8
*) (key
+ 1)) < 0 ||
1884 os_memcmp_const(mic
, key
+ 1, mic_len
) != 0) {
1885 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1886 "WPA: Invalid EAPOL-Key MIC "
1887 "when using TPTK - ignoring TPTK");
1889 wpa_printf(MSG_INFO
,
1890 "TEST: Ignore Key MIC failure for fuzz testing");
1892 #endif /* TEST_FUZZ */
1896 #endif /* TEST_FUZZ */
1900 os_memcpy(&sm
->ptk
, &sm
->tptk
, sizeof(sm
->ptk
));
1901 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
1903 * This assures the same TPTK in sm->tptk can never be
1904 * copied twice to sm->ptk as the new PTK. In
1905 * combination with the installed flag in the wpa_ptk
1906 * struct, this assures the same PTK is only installed
1909 sm
->renew_snonce
= 1;
1913 if (!ok
&& sm
->ptk_set
) {
1914 os_memset(key
+ 1, 0, mic_len
);
1915 if (wpa_eapol_key_mic(sm
->ptk
.kck
, sm
->ptk
.kck_len
,
1917 ver
, buf
, len
, (u8
*) (key
+ 1)) < 0 ||
1918 os_memcmp_const(mic
, key
+ 1, mic_len
) != 0) {
1919 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1920 "WPA: Invalid EAPOL-Key MIC - "
1923 wpa_printf(MSG_INFO
,
1924 "TEST: Ignore Key MIC failure for fuzz testing");
1925 goto continue_fuzz2
;
1926 #endif /* TEST_FUZZ */
1931 #endif /* TEST_FUZZ */
1936 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1937 "WPA: Could not verify EAPOL-Key MIC - "
1942 os_memcpy(sm
->rx_replay_counter
, key
->replay_counter
,
1943 WPA_REPLAY_COUNTER_LEN
);
1944 sm
->rx_replay_counter_set
= 1;
1949 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1950 static int wpa_supplicant_decrypt_key_data(struct wpa_sm
*sm
,
1951 struct wpa_eapol_key
*key
,
1952 size_t mic_len
, u16 ver
,
1953 u8
*key_data
, size_t *key_data_len
)
1955 wpa_hexdump(MSG_DEBUG
, "RSN: encrypted key data",
1956 key_data
, *key_data_len
);
1958 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1959 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1964 /* Decrypt key data here so that this operation does not need
1965 * to be implemented separately for each message type. */
1966 if (ver
== WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&& sm
->ptk
.kek_len
== 16) {
1967 #ifdef CONFIG_NO_RC4
1968 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1969 "WPA: RC4 not supported in the build");
1971 #else /* CONFIG_NO_RC4 */
1974 wpa_printf(MSG_DEBUG
, "WPA: Decrypt Key Data using RC4");
1975 os_memcpy(ek
, key
->key_iv
, 16);
1976 os_memcpy(ek
+ 16, sm
->ptk
.kek
, sm
->ptk
.kek_len
);
1977 if (rc4_skip(ek
, 32, 256, key_data
, *key_data_len
)) {
1978 os_memset(ek
, 0, sizeof(ek
));
1979 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
1983 os_memset(ek
, 0, sizeof(ek
));
1984 #endif /* CONFIG_NO_RC4 */
1985 } else if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1986 ver
== WPA_KEY_INFO_TYPE_AES_128_CMAC
||
1987 wpa_use_aes_key_wrap(sm
->key_mgmt
)) {
1990 wpa_printf(MSG_DEBUG
,
1991 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
1992 (unsigned int) sm
->ptk
.kek_len
);
1993 if (*key_data_len
< 8 || *key_data_len
% 8) {
1994 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1995 "WPA: Unsupported AES-WRAP len %u",
1996 (unsigned int) *key_data_len
);
1999 *key_data_len
-= 8; /* AES-WRAP adds 8 bytes */
2000 buf
= os_malloc(*key_data_len
);
2002 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2003 "WPA: No memory for AES-UNWRAP buffer");
2007 os_memset(buf
, 0x11, *key_data_len
);
2008 #endif /* TEST_FUZZ */
2009 if (aes_unwrap(sm
->ptk
.kek
, sm
->ptk
.kek_len
, *key_data_len
/ 8,
2012 wpa_printf(MSG_INFO
,
2013 "TEST: Ignore AES unwrap failure for fuzz testing");
2015 #endif /* TEST_FUZZ */
2016 bin_clear_free(buf
, *key_data_len
);
2017 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2018 "WPA: AES unwrap failed - "
2019 "could not decrypt EAPOL-Key key data");
2024 #endif /* TEST_FUZZ */
2025 os_memcpy(key_data
, buf
, *key_data_len
);
2026 bin_clear_free(buf
, *key_data_len
);
2027 WPA_PUT_BE16(((u8
*) (key
+ 1)) + mic_len
, *key_data_len
);
2029 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2030 "WPA: Unsupported key_info type %d", ver
);
2033 wpa_hexdump_key(MSG_DEBUG
, "WPA: decrypted EAPOL-Key key data",
2034 key_data
, *key_data_len
);
2040 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2041 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2043 void wpa_sm_aborted_cached(struct wpa_sm
*sm
)
2045 if (sm
&& sm
->cur_pmksa
) {
2046 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2047 "RSN: Cancelling PMKSA caching attempt");
2048 sm
->cur_pmksa
= NULL
;
2053 static void wpa_eapol_key_dump(struct wpa_sm
*sm
,
2054 const struct wpa_eapol_key
*key
,
2055 unsigned int key_data_len
,
2056 const u8
*mic
, unsigned int mic_len
)
2058 #ifndef CONFIG_NO_STDOUT_DEBUG
2059 u16 key_info
= WPA_GET_BE16(key
->key_info
);
2061 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, " EAPOL-Key type=%d", key
->type
);
2062 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2063 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2064 key_info
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
2065 (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) >>
2066 WPA_KEY_INFO_KEY_INDEX_SHIFT
,
2067 (key_info
& (BIT(13) | BIT(14) | BIT(15))) >> 13,
2068 key_info
& WPA_KEY_INFO_KEY_TYPE
? "Pairwise" : "Group",
2069 key_info
& WPA_KEY_INFO_INSTALL
? " Install" : "",
2070 key_info
& WPA_KEY_INFO_ACK
? " Ack" : "",
2071 key_info
& WPA_KEY_INFO_MIC
? " MIC" : "",
2072 key_info
& WPA_KEY_INFO_SECURE
? " Secure" : "",
2073 key_info
& WPA_KEY_INFO_ERROR
? " Error" : "",
2074 key_info
& WPA_KEY_INFO_REQUEST
? " Request" : "",
2075 key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
? " Encr" : "");
2076 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2077 " key_length=%u key_data_length=%u",
2078 WPA_GET_BE16(key
->key_length
), key_data_len
);
2079 wpa_hexdump(MSG_DEBUG
, " replay_counter",
2080 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
2081 wpa_hexdump(MSG_DEBUG
, " key_nonce", key
->key_nonce
, WPA_NONCE_LEN
);
2082 wpa_hexdump(MSG_DEBUG
, " key_iv", key
->key_iv
, 16);
2083 wpa_hexdump(MSG_DEBUG
, " key_rsc", key
->key_rsc
, 8);
2084 wpa_hexdump(MSG_DEBUG
, " key_id (reserved)", key
->key_id
, 8);
2085 wpa_hexdump(MSG_DEBUG
, " key_mic", mic
, mic_len
);
2086 #endif /* CONFIG_NO_STDOUT_DEBUG */
2091 static int wpa_supp_aead_decrypt(struct wpa_sm
*sm
, u8
*buf
, size_t buf_len
,
2092 size_t *key_data_len
)
2094 struct wpa_ptk
*ptk
;
2095 struct ieee802_1x_hdr
*hdr
;
2096 struct wpa_eapol_key
*key
;
2101 if (*key_data_len
< AES_BLOCK_SIZE
) {
2102 wpa_printf(MSG_INFO
, "No room for AES-SIV data in the frame");
2108 else if (sm
->ptk_set
)
2113 hdr
= (struct ieee802_1x_hdr
*) buf
;
2114 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2115 pos
= (u8
*) (key
+ 1);
2116 pos
+= 2; /* Pointing at the Encrypted Key Data field */
2118 tmp
= os_malloc(*key_data_len
);
2122 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2123 * to Key Data (exclusive). */
2125 aad_len
[0] = pos
- buf
;
2126 if (aes_siv_decrypt(ptk
->kek
, ptk
->kek_len
, pos
, *key_data_len
,
2127 1, aad
, aad_len
, tmp
) < 0) {
2128 wpa_printf(MSG_INFO
, "Invalid AES-SIV data in the frame");
2129 bin_clear_free(tmp
, *key_data_len
);
2133 /* AEAD decryption and validation completed successfully */
2134 (*key_data_len
) -= AES_BLOCK_SIZE
;
2135 wpa_hexdump_key(MSG_DEBUG
, "WPA: Decrypted Key Data",
2136 tmp
, *key_data_len
);
2138 /* Replace Key Data field with the decrypted version */
2139 os_memcpy(pos
, tmp
, *key_data_len
);
2140 pos
-= 2; /* Key Data Length field */
2141 WPA_PUT_BE16(pos
, *key_data_len
);
2142 bin_clear_free(tmp
, *key_data_len
);
2147 os_memcpy(&sm
->ptk
, &sm
->tptk
, sizeof(sm
->ptk
));
2148 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
2151 os_memcpy(sm
->rx_replay_counter
, key
->replay_counter
,
2152 WPA_REPLAY_COUNTER_LEN
);
2153 sm
->rx_replay_counter_set
= 1;
2157 #endif /* CONFIG_FILS */
2161 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2162 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2163 * @src_addr: Source MAC address of the EAPOL packet
2164 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2165 * @len: Length of the EAPOL frame
2166 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2168 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2169 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2170 * only processing WPA and WPA2 EAPOL-Key frames.
2172 * The received EAPOL-Key packets are validated and valid packets are replied
2173 * to. In addition, key material (PTK, GTK) is configured at the end of a
2174 * successful key handshake.
2176 int wpa_sm_rx_eapol(struct wpa_sm
*sm
, const u8
*src_addr
,
2177 const u8
*buf
, size_t len
)
2179 size_t plen
, data_len
, key_data_len
;
2180 const struct ieee802_1x_hdr
*hdr
;
2181 struct wpa_eapol_key
*key
;
2186 size_t mic_len
, keyhdrlen
;
2188 #ifdef CONFIG_IEEE80211R
2189 sm
->ft_completed
= 0;
2190 #endif /* CONFIG_IEEE80211R */
2192 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
2193 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
2195 if (len
< sizeof(*hdr
) + keyhdrlen
) {
2196 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2197 "WPA: EAPOL frame too short to be a WPA "
2198 "EAPOL-Key (len %lu, expecting at least %lu)",
2199 (unsigned long) len
,
2200 (unsigned long) sizeof(*hdr
) + keyhdrlen
);
2204 hdr
= (const struct ieee802_1x_hdr
*) buf
;
2205 plen
= be_to_host16(hdr
->length
);
2206 data_len
= plen
+ sizeof(*hdr
);
2207 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2208 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2209 hdr
->version
, hdr
->type
, (unsigned long) plen
);
2211 if (hdr
->version
< EAPOL_VERSION
) {
2212 /* TODO: backwards compatibility */
2214 if (hdr
->type
!= IEEE802_1X_TYPE_EAPOL_KEY
) {
2215 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2216 "WPA: EAPOL frame (type %u) discarded, "
2217 "not a Key frame", hdr
->type
);
2221 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL-Key", buf
, len
);
2222 if (plen
> len
- sizeof(*hdr
) || plen
< keyhdrlen
) {
2223 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2224 "WPA: EAPOL frame payload size %lu "
2225 "invalid (frame size %lu)",
2226 (unsigned long) plen
, (unsigned long) len
);
2230 if (data_len
< len
) {
2231 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2232 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2233 (unsigned long) len
- data_len
);
2237 * Make a copy of the frame since we need to modify the buffer during
2238 * MAC validation and Key Data decryption.
2240 tmp
= os_memdup(buf
, data_len
);
2243 key
= (struct wpa_eapol_key
*) (tmp
+ sizeof(struct ieee802_1x_hdr
));
2244 mic
= (u8
*) (key
+ 1);
2245 key_data
= mic
+ mic_len
+ 2;
2247 if (key
->type
!= EAPOL_KEY_TYPE_WPA
&& key
->type
!= EAPOL_KEY_TYPE_RSN
)
2249 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2250 "WPA: EAPOL-Key type (%d) unknown, discarded",
2256 key_data_len
= WPA_GET_BE16(mic
+ mic_len
);
2257 wpa_eapol_key_dump(sm
, key
, key_data_len
, mic
, mic_len
);
2259 if (key_data_len
> plen
- keyhdrlen
) {
2260 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
, "WPA: Invalid EAPOL-Key "
2261 "frame - key_data overflow (%u > %u)",
2262 (unsigned int) key_data_len
,
2263 (unsigned int) (plen
- keyhdrlen
));
2267 eapol_sm_notify_lower_layer_success(sm
->eapol
, 0);
2268 key_info
= WPA_GET_BE16(key
->key_info
);
2269 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
2270 if (ver
!= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&&
2271 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
2272 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
&&
2273 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
2274 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
&&
2275 !wpa_use_akm_defined(sm
->key_mgmt
)) {
2276 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2277 "WPA: Unsupported EAPOL-Key descriptor version %d",
2282 if (wpa_use_akm_defined(sm
->key_mgmt
) &&
2283 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
2284 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2285 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2290 #ifdef CONFIG_IEEE80211R
2291 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
2292 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
2293 if (ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
&&
2294 !wpa_use_akm_defined(sm
->key_mgmt
)) {
2295 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2296 "FT: AP did not use AES-128-CMAC");
2300 #endif /* CONFIG_IEEE80211R */
2301 #ifdef CONFIG_IEEE80211W
2302 if (wpa_key_mgmt_sha256(sm
->key_mgmt
)) {
2303 if (ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
&&
2304 !wpa_use_akm_defined(sm
->key_mgmt
)) {
2305 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2306 "WPA: AP did not use the "
2307 "negotiated AES-128-CMAC");
2311 #endif /* CONFIG_IEEE80211W */
2312 if (sm
->pairwise_cipher
== WPA_CIPHER_CCMP
&&
2313 !wpa_use_akm_defined(sm
->key_mgmt
) &&
2314 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
2315 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2316 "WPA: CCMP is used, but EAPOL-Key "
2317 "descriptor version (%d) is not 2", ver
);
2318 if (sm
->group_cipher
!= WPA_CIPHER_CCMP
&&
2319 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
2320 /* Earlier versions of IEEE 802.11i did not explicitly
2321 * require version 2 descriptor for all EAPOL-Key
2322 * packets, so allow group keys to use version 1 if
2323 * CCMP is not used for them. */
2324 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2325 "WPA: Backwards compatibility: allow invalid "
2326 "version for non-CCMP group keys");
2327 } else if (ver
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
2328 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2329 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
2332 } else if (sm
->pairwise_cipher
== WPA_CIPHER_GCMP
&&
2333 !wpa_use_akm_defined(sm
->key_mgmt
) &&
2334 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
2335 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2336 "WPA: GCMP is used, but EAPOL-Key "
2337 "descriptor version (%d) is not 2", ver
);
2341 if (sm
->rx_replay_counter_set
&&
2342 os_memcmp(key
->replay_counter
, sm
->rx_replay_counter
,
2343 WPA_REPLAY_COUNTER_LEN
) <= 0) {
2344 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2345 "WPA: EAPOL-Key Replay Counter did not increase - "
2350 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
2351 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2352 "WPA: Unsupported SMK bit in key_info");
2356 if (!(key_info
& WPA_KEY_INFO_ACK
)) {
2357 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2358 "WPA: No Ack bit in key_info");
2362 if (key_info
& WPA_KEY_INFO_REQUEST
) {
2363 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2364 "WPA: EAPOL-Key with Request bit - dropped");
2368 if ((key_info
& WPA_KEY_INFO_MIC
) &&
2369 wpa_supplicant_verify_eapol_key_mic(sm
, key
, ver
, tmp
, data_len
))
2373 if (!mic_len
&& (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
2374 if (wpa_supp_aead_decrypt(sm
, tmp
, data_len
, &key_data_len
))
2377 #endif /* CONFIG_FILS */
2379 if ((sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) &&
2380 (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) && mic_len
) {
2382 * Only decrypt the Key Data field if the frame's authenticity
2383 * was verified. When using AES-SIV (FILS), the MIC flag is not
2384 * set, so this check should only be performed if mic_len != 0
2385 * which is the case in this code branch.
2387 if (!(key_info
& WPA_KEY_INFO_MIC
)) {
2388 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2389 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2392 if (wpa_supplicant_decrypt_key_data(sm
, key
, mic_len
,
2398 if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
2399 if (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) {
2400 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2401 "WPA: Ignored EAPOL-Key (Pairwise) with "
2402 "non-zero key index");
2405 if (key_info
& (WPA_KEY_INFO_MIC
|
2406 WPA_KEY_INFO_ENCR_KEY_DATA
)) {
2407 /* 3/4 4-Way Handshake */
2408 wpa_supplicant_process_3_of_4(sm
, key
, ver
, key_data
,
2411 /* 1/4 4-Way Handshake */
2412 wpa_supplicant_process_1_of_4(sm
, src_addr
, key
,
2417 if ((mic_len
&& (key_info
& WPA_KEY_INFO_MIC
)) ||
2418 (!mic_len
&& (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
))) {
2419 /* 1/2 Group Key Handshake */
2420 wpa_supplicant_process_1_of_2(sm
, src_addr
, key
,
2421 key_data
, key_data_len
,
2424 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2425 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
2433 bin_clear_free(tmp
, data_len
);
2438 #ifdef CONFIG_CTRL_IFACE
2439 static u32
wpa_key_mgmt_suite(struct wpa_sm
*sm
)
2441 switch (sm
->key_mgmt
) {
2442 case WPA_KEY_MGMT_IEEE8021X
:
2443 return ((sm
->proto
== WPA_PROTO_RSN
||
2444 sm
->proto
== WPA_PROTO_OSEN
) ?
2445 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X
:
2446 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X
);
2447 case WPA_KEY_MGMT_PSK
:
2448 return (sm
->proto
== WPA_PROTO_RSN
?
2449 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X
:
2450 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X
);
2451 #ifdef CONFIG_IEEE80211R
2452 case WPA_KEY_MGMT_FT_IEEE8021X
:
2453 return RSN_AUTH_KEY_MGMT_FT_802_1X
;
2454 case WPA_KEY_MGMT_FT_PSK
:
2455 return RSN_AUTH_KEY_MGMT_FT_PSK
;
2456 #endif /* CONFIG_IEEE80211R */
2457 #ifdef CONFIG_IEEE80211W
2458 case WPA_KEY_MGMT_IEEE8021X_SHA256
:
2459 return RSN_AUTH_KEY_MGMT_802_1X_SHA256
;
2460 case WPA_KEY_MGMT_PSK_SHA256
:
2461 return RSN_AUTH_KEY_MGMT_PSK_SHA256
;
2462 #endif /* CONFIG_IEEE80211W */
2463 case WPA_KEY_MGMT_CCKM
:
2464 return (sm
->proto
== WPA_PROTO_RSN
?
2465 RSN_AUTH_KEY_MGMT_CCKM
:
2466 WPA_AUTH_KEY_MGMT_CCKM
);
2467 case WPA_KEY_MGMT_WPA_NONE
:
2468 return WPA_AUTH_KEY_MGMT_NONE
;
2469 case WPA_KEY_MGMT_IEEE8021X_SUITE_B
:
2470 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B
;
2471 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192
:
2472 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192
;
2479 #define RSN_SUITE "%02x-%02x-%02x-%d"
2480 #define RSN_SUITE_ARG(s) \
2481 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2484 * wpa_sm_get_mib - Dump text list of MIB entries
2485 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2486 * @buf: Buffer for the list
2487 * @buflen: Length of the buffer
2488 * Returns: Number of bytes written to buffer
2490 * This function is used fetch dot11 MIB variables.
2492 int wpa_sm_get_mib(struct wpa_sm
*sm
, char *buf
, size_t buflen
)
2494 char pmkid_txt
[PMKID_LEN
* 2 + 1];
2498 if (sm
->cur_pmksa
) {
2499 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
2500 sm
->cur_pmksa
->pmkid
, PMKID_LEN
);
2502 pmkid_txt
[0] = '\0';
2504 if ((wpa_key_mgmt_wpa_psk(sm
->key_mgmt
) ||
2505 wpa_key_mgmt_wpa_ieee8021x(sm
->key_mgmt
)) &&
2506 sm
->proto
== WPA_PROTO_RSN
)
2511 ret
= os_snprintf(buf
, buflen
,
2512 "dot11RSNAOptionImplemented=TRUE\n"
2513 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2514 "dot11RSNAEnabled=%s\n"
2515 "dot11RSNAPreauthenticationEnabled=%s\n"
2516 "dot11RSNAConfigVersion=%d\n"
2517 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2518 "dot11RSNAConfigGroupCipherSize=%d\n"
2519 "dot11RSNAConfigPMKLifetime=%d\n"
2520 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2521 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2522 "dot11RSNAConfigSATimeout=%d\n",
2523 rsna
? "TRUE" : "FALSE",
2524 rsna
? "TRUE" : "FALSE",
2526 wpa_cipher_key_len(sm
->group_cipher
) * 8,
2527 sm
->dot11RSNAConfigPMKLifetime
,
2528 sm
->dot11RSNAConfigPMKReauthThreshold
,
2529 sm
->dot11RSNAConfigSATimeout
);
2530 if (os_snprintf_error(buflen
, ret
))
2535 buf
+ len
, buflen
- len
,
2536 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
2537 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
2538 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
2539 "dot11RSNAPMKIDUsed=%s\n"
2540 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
2541 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
2542 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
2543 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2544 "dot11RSNA4WayHandshakeFailures=%u\n",
2545 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm
)),
2546 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2547 sm
->pairwise_cipher
)),
2548 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2551 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm
)),
2552 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2553 sm
->pairwise_cipher
)),
2554 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2556 sm
->dot11RSNA4WayHandshakeFailures
);
2557 if (!os_snprintf_error(buflen
- len
, ret
))
2562 #endif /* CONFIG_CTRL_IFACE */
2565 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
2566 void *ctx
, enum pmksa_free_reason reason
)
2568 struct wpa_sm
*sm
= ctx
;
2571 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: PMKSA cache entry free_cb: "
2572 MACSTR
" reason=%d", MAC2STR(entry
->aa
), reason
);
2574 if (sm
->cur_pmksa
== entry
) {
2575 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2576 "RSN: %s current PMKSA entry",
2577 reason
== PMKSA_REPLACE
? "replaced" : "removed");
2578 pmksa_cache_clear_current(sm
);
2581 * If an entry is simply being replaced, there's no need to
2582 * deauthenticate because it will be immediately re-added.
2583 * This happens when EAP authentication is completed again
2584 * (reauth or failed PMKSA caching attempt).
2586 if (reason
!= PMKSA_REPLACE
)
2590 if (reason
== PMKSA_EXPIRE
&&
2591 (sm
->pmk_len
== entry
->pmk_len
&&
2592 os_memcmp(sm
->pmk
, entry
->pmk
, sm
->pmk_len
) == 0)) {
2593 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2594 "RSN: deauthenticating due to expired PMK");
2595 pmksa_cache_clear_current(sm
);
2601 os_memset(sm
->pmk
, 0, sizeof(sm
->pmk
));
2602 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
2608 * wpa_sm_init - Initialize WPA state machine
2609 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2610 * Returns: Pointer to the allocated WPA state machine data
2612 * This function is used to allocate a new WPA state machine and the returned
2613 * value is passed to all WPA state machine calls.
2615 struct wpa_sm
* wpa_sm_init(struct wpa_sm_ctx
*ctx
)
2619 sm
= os_zalloc(sizeof(*sm
));
2622 dl_list_init(&sm
->pmksa_candidates
);
2623 sm
->renew_snonce
= 1;
2626 sm
->dot11RSNAConfigPMKLifetime
= 43200;
2627 sm
->dot11RSNAConfigPMKReauthThreshold
= 70;
2628 sm
->dot11RSNAConfigSATimeout
= 60;
2630 sm
->pmksa
= pmksa_cache_init(wpa_sm_pmksa_free_cb
, sm
, sm
);
2631 if (sm
->pmksa
== NULL
) {
2632 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
2633 "RSN: PMKSA cache initialization failed");
2643 * wpa_sm_deinit - Deinitialize WPA state machine
2644 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2646 void wpa_sm_deinit(struct wpa_sm
*sm
)
2650 pmksa_cache_deinit(sm
->pmksa
);
2651 eloop_cancel_timeout(wpa_sm_start_preauth
, sm
, NULL
);
2652 eloop_cancel_timeout(wpa_sm_rekey_ptk
, sm
, NULL
);
2653 os_free(sm
->assoc_wpa_ie
);
2654 os_free(sm
->ap_wpa_ie
);
2655 os_free(sm
->ap_rsn_ie
);
2658 #ifdef CONFIG_IEEE80211R
2659 os_free(sm
->assoc_resp_ies
);
2660 #endif /* CONFIG_IEEE80211R */
2661 #ifdef CONFIG_TESTING_OPTIONS
2662 wpabuf_free(sm
->test_assoc_ie
);
2663 #endif /* CONFIG_TESTING_OPTIONS */
2664 #ifdef CONFIG_FILS_SK_PFS
2665 crypto_ecdh_deinit(sm
->fils_ecdh
);
2666 #endif /* CONFIG_FILS_SK_PFS */
2668 wpabuf_free(sm
->fils_ft_ies
);
2669 #endif /* CONFIG_FILS */
2671 crypto_ecdh_deinit(sm
->owe_ecdh
);
2672 #endif /* CONFIG_OWE */
2674 wpabuf_clear_free(sm
->dpp_z
);
2675 #endif /* CONFIG_DPP2 */
2681 * wpa_sm_notify_assoc - Notify WPA state machine about association
2682 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2683 * @bssid: The BSSID of the new association
2685 * This function is called to let WPA state machine know that the connection
2688 void wpa_sm_notify_assoc(struct wpa_sm
*sm
, const u8
*bssid
)
2695 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2696 "WPA: Association event - clear replay counter");
2697 os_memcpy(sm
->bssid
, bssid
, ETH_ALEN
);
2698 os_memset(sm
->rx_replay_counter
, 0, WPA_REPLAY_COUNTER_LEN
);
2699 sm
->rx_replay_counter_set
= 0;
2700 sm
->renew_snonce
= 1;
2701 if (os_memcmp(sm
->preauth_bssid
, bssid
, ETH_ALEN
) == 0)
2702 rsn_preauth_deinit(sm
);
2704 #ifdef CONFIG_IEEE80211R
2705 if (wpa_ft_is_completed(sm
)) {
2707 * Clear portValid to kick EAPOL state machine to re-enter
2708 * AUTHENTICATED state to get the EAPOL port Authorized.
2710 eapol_sm_notify_portValid(sm
->eapol
, FALSE
);
2711 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
, 1);
2713 /* Prepare for the next transition */
2714 wpa_ft_prepare_auth_request(sm
, NULL
);
2718 #endif /* CONFIG_IEEE80211R */
2720 if (sm
->fils_completed
) {
2722 * Clear portValid to kick EAPOL state machine to re-enter
2723 * AUTHENTICATED state to get the EAPOL port Authorized.
2725 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
, 1);
2728 #endif /* CONFIG_FILS */
2732 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2733 * this is not part of a Fast BSS Transition.
2735 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Clear old PTK");
2737 os_memset(&sm
->ptk
, 0, sizeof(sm
->ptk
));
2739 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
2740 os_memset(&sm
->gtk
, 0, sizeof(sm
->gtk
));
2741 os_memset(&sm
->gtk_wnm_sleep
, 0, sizeof(sm
->gtk_wnm_sleep
));
2742 #ifdef CONFIG_IEEE80211W
2743 os_memset(&sm
->igtk
, 0, sizeof(sm
->igtk
));
2744 os_memset(&sm
->igtk_wnm_sleep
, 0, sizeof(sm
->igtk_wnm_sleep
));
2745 #endif /* CONFIG_IEEE80211W */
2750 #endif /* CONFIG_TDLS */
2753 os_memset(sm
->p2p_ip_addr
, 0, sizeof(sm
->p2p_ip_addr
));
2754 #endif /* CONFIG_P2P */
2759 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2760 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2762 * This function is called to let WPA state machine know that the connection
2763 * was lost. This will abort any existing pre-authentication session.
2765 void wpa_sm_notify_disassoc(struct wpa_sm
*sm
)
2767 eloop_cancel_timeout(wpa_sm_start_preauth
, sm
, NULL
);
2768 eloop_cancel_timeout(wpa_sm_rekey_ptk
, sm
, NULL
);
2769 rsn_preauth_deinit(sm
);
2770 pmksa_cache_clear_current(sm
);
2771 if (wpa_sm_get_state(sm
) == WPA_4WAY_HANDSHAKE
)
2772 sm
->dot11RSNA4WayHandshakeFailures
++;
2774 wpa_tdls_disassoc(sm
);
2775 #endif /* CONFIG_TDLS */
2777 sm
->fils_completed
= 0;
2778 #endif /* CONFIG_FILS */
2779 #ifdef CONFIG_IEEE80211R
2780 sm
->ft_reassoc_completed
= 0;
2781 #endif /* CONFIG_IEEE80211R */
2783 /* Keys are not needed in the WPA state machine anymore */
2786 sm
->msg_3_of_4_ok
= 0;
2787 os_memset(sm
->bssid
, 0, ETH_ALEN
);
2792 * wpa_sm_set_pmk - Set PMK
2793 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2795 * @pmk_len: The length of the new PMK in bytes
2796 * @pmkid: Calculated PMKID
2797 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
2799 * Configure the PMK for WPA state machine.
2801 void wpa_sm_set_pmk(struct wpa_sm
*sm
, const u8
*pmk
, size_t pmk_len
,
2802 const u8
*pmkid
, const u8
*bssid
)
2807 wpa_hexdump_key(MSG_DEBUG
, "WPA: Set PMK based on external data",
2809 sm
->pmk_len
= pmk_len
;
2810 os_memcpy(sm
->pmk
, pmk
, pmk_len
);
2812 #ifdef CONFIG_IEEE80211R
2813 /* Set XXKey to be PSK for FT key derivation */
2814 sm
->xxkey_len
= pmk_len
;
2815 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
2816 #endif /* CONFIG_IEEE80211R */
2819 pmksa_cache_add(sm
->pmksa
, pmk
, pmk_len
, pmkid
, NULL
, 0,
2820 bssid
, sm
->own_addr
,
2821 sm
->network_ctx
, sm
->key_mgmt
, NULL
);
2827 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2828 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2830 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2833 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm
*sm
)
2838 if (sm
->cur_pmksa
) {
2839 wpa_hexdump_key(MSG_DEBUG
,
2840 "WPA: Set PMK based on current PMKSA",
2841 sm
->cur_pmksa
->pmk
, sm
->cur_pmksa
->pmk_len
);
2842 sm
->pmk_len
= sm
->cur_pmksa
->pmk_len
;
2843 os_memcpy(sm
->pmk
, sm
->cur_pmksa
->pmk
, sm
->pmk_len
);
2845 wpa_printf(MSG_DEBUG
, "WPA: No current PMKSA - clear PMK");
2847 os_memset(sm
->pmk
, 0, PMK_LEN_MAX
);
2853 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2854 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2855 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2857 void wpa_sm_set_fast_reauth(struct wpa_sm
*sm
, int fast_reauth
)
2860 sm
->fast_reauth
= fast_reauth
;
2865 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2866 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2867 * @scard_ctx: Context pointer for smartcard related callback functions
2869 void wpa_sm_set_scard_ctx(struct wpa_sm
*sm
, void *scard_ctx
)
2873 sm
->scard_ctx
= scard_ctx
;
2874 if (sm
->preauth_eapol
)
2875 eapol_sm_register_scard_ctx(sm
->preauth_eapol
, scard_ctx
);
2880 * wpa_sm_set_config - Notification of current configration change
2881 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2882 * @config: Pointer to current network configuration
2884 * Notify WPA state machine that configuration has changed. config will be
2885 * stored as a backpointer to network configuration. This can be %NULL to clear
2886 * the stored pointed.
2888 void wpa_sm_set_config(struct wpa_sm
*sm
, struct rsn_supp_config
*config
)
2894 sm
->network_ctx
= config
->network_ctx
;
2895 sm
->allowed_pairwise_cipher
= config
->allowed_pairwise_cipher
;
2896 sm
->proactive_key_caching
= config
->proactive_key_caching
;
2897 sm
->eap_workaround
= config
->eap_workaround
;
2898 sm
->eap_conf_ctx
= config
->eap_conf_ctx
;
2900 os_memcpy(sm
->ssid
, config
->ssid
, config
->ssid_len
);
2901 sm
->ssid_len
= config
->ssid_len
;
2904 sm
->wpa_ptk_rekey
= config
->wpa_ptk_rekey
;
2905 sm
->p2p
= config
->p2p
;
2906 sm
->wpa_rsc_relaxation
= config
->wpa_rsc_relaxation
;
2908 if (config
->fils_cache_id
) {
2909 sm
->fils_cache_id_set
= 1;
2910 os_memcpy(sm
->fils_cache_id
, config
->fils_cache_id
,
2913 sm
->fils_cache_id_set
= 0;
2915 #endif /* CONFIG_FILS */
2917 sm
->network_ctx
= NULL
;
2918 sm
->allowed_pairwise_cipher
= 0;
2919 sm
->proactive_key_caching
= 0;
2920 sm
->eap_workaround
= 0;
2921 sm
->eap_conf_ctx
= NULL
;
2923 sm
->wpa_ptk_rekey
= 0;
2925 sm
->wpa_rsc_relaxation
= 0;
2931 * wpa_sm_set_own_addr - Set own MAC address
2932 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2933 * @addr: Own MAC address
2935 void wpa_sm_set_own_addr(struct wpa_sm
*sm
, const u8
*addr
)
2938 os_memcpy(sm
->own_addr
, addr
, ETH_ALEN
);
2943 * wpa_sm_set_ifname - Set network interface name
2944 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2945 * @ifname: Interface name
2946 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2948 void wpa_sm_set_ifname(struct wpa_sm
*sm
, const char *ifname
,
2949 const char *bridge_ifname
)
2952 sm
->ifname
= ifname
;
2953 sm
->bridge_ifname
= bridge_ifname
;
2959 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2960 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2961 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2963 void wpa_sm_set_eapol(struct wpa_sm
*sm
, struct eapol_sm
*eapol
)
2971 * wpa_sm_set_param - Set WPA state machine parameters
2972 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2973 * @param: Parameter field
2974 * @value: Parameter value
2975 * Returns: 0 on success, -1 on failure
2977 int wpa_sm_set_param(struct wpa_sm
*sm
, enum wpa_sm_conf_params param
,
2986 case RSNA_PMK_LIFETIME
:
2988 sm
->dot11RSNAConfigPMKLifetime
= value
;
2992 case RSNA_PMK_REAUTH_THRESHOLD
:
2993 if (value
> 0 && value
<= 100)
2994 sm
->dot11RSNAConfigPMKReauthThreshold
= value
;
2998 case RSNA_SA_TIMEOUT
:
3000 sm
->dot11RSNAConfigSATimeout
= value
;
3004 case WPA_PARAM_PROTO
:
3007 case WPA_PARAM_PAIRWISE
:
3008 sm
->pairwise_cipher
= value
;
3010 case WPA_PARAM_GROUP
:
3011 sm
->group_cipher
= value
;
3013 case WPA_PARAM_KEY_MGMT
:
3014 sm
->key_mgmt
= value
;
3016 #ifdef CONFIG_IEEE80211W
3017 case WPA_PARAM_MGMT_GROUP
:
3018 sm
->mgmt_group_cipher
= value
;
3020 #endif /* CONFIG_IEEE80211W */
3021 case WPA_PARAM_RSN_ENABLED
:
3022 sm
->rsn_enabled
= value
;
3039 * wpa_sm_get_status - Get WPA state machine
3040 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3041 * @buf: Buffer for status information
3042 * @buflen: Maximum buffer length
3043 * @verbose: Whether to include verbose status information
3044 * Returns: Number of bytes written to buf.
3046 * Query WPA state machine for status information. This function fills in
3047 * a text area with current status information. If the buffer (buf) is not
3048 * large enough, status information will be truncated to fit the buffer.
3050 int wpa_sm_get_status(struct wpa_sm
*sm
, char *buf
, size_t buflen
,
3053 char *pos
= buf
, *end
= buf
+ buflen
;
3056 ret
= os_snprintf(pos
, end
- pos
,
3057 "pairwise_cipher=%s\n"
3060 wpa_cipher_txt(sm
->pairwise_cipher
),
3061 wpa_cipher_txt(sm
->group_cipher
),
3062 wpa_key_mgmt_txt(sm
->key_mgmt
, sm
->proto
));
3063 if (os_snprintf_error(end
- pos
, ret
))
3067 if (sm
->mfp
!= NO_MGMT_FRAME_PROTECTION
&& sm
->ap_rsn_ie
) {
3068 struct wpa_ie_data rsn
;
3069 if (wpa_parse_wpa_ie_rsn(sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
, &rsn
)
3071 rsn
.capabilities
& (WPA_CAPABILITY_MFPR
|
3072 WPA_CAPABILITY_MFPC
)) {
3073 ret
= os_snprintf(pos
, end
- pos
, "pmf=%d\n"
3074 "mgmt_group_cipher=%s\n",
3076 WPA_CAPABILITY_MFPR
) ? 2 : 1,
3078 sm
->mgmt_group_cipher
));
3079 if (os_snprintf_error(end
- pos
, ret
))
3089 int wpa_sm_pmf_enabled(struct wpa_sm
*sm
)
3091 struct wpa_ie_data rsn
;
3093 if (sm
->mfp
== NO_MGMT_FRAME_PROTECTION
|| !sm
->ap_rsn_ie
)
3096 if (wpa_parse_wpa_ie_rsn(sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
, &rsn
) >= 0 &&
3097 rsn
.capabilities
& (WPA_CAPABILITY_MFPR
| WPA_CAPABILITY_MFPC
))
3104 int wpa_sm_ocv_enabled(struct wpa_sm
*sm
)
3106 struct wpa_ie_data rsn
;
3108 if (!sm
->ocv
|| !sm
->ap_rsn_ie
)
3111 return wpa_parse_wpa_ie_rsn(sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
,
3113 (rsn
.capabilities
& WPA_CAPABILITY_OCVC
);
3118 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3119 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3120 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3121 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3122 * Returns: 0 on success, -1 on failure
3124 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm
*sm
, u8
*wpa_ie
,
3132 #ifdef CONFIG_TESTING_OPTIONS
3133 if (sm
->test_assoc_ie
) {
3134 wpa_printf(MSG_DEBUG
,
3135 "TESTING: Replace association WPA/RSN IE");
3136 if (*wpa_ie_len
< wpabuf_len(sm
->test_assoc_ie
))
3138 os_memcpy(wpa_ie
, wpabuf_head(sm
->test_assoc_ie
),
3139 wpabuf_len(sm
->test_assoc_ie
));
3140 res
= wpabuf_len(sm
->test_assoc_ie
);
3142 #endif /* CONFIG_TESTING_OPTIONS */
3143 res
= wpa_gen_wpa_ie(sm
, wpa_ie
, *wpa_ie_len
);
3148 wpa_hexdump(MSG_DEBUG
, "WPA: Set own WPA IE default",
3149 wpa_ie
, *wpa_ie_len
);
3151 if (sm
->assoc_wpa_ie
== NULL
) {
3153 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3154 * the correct version of the IE even if PMKSA caching is
3155 * aborted (which would remove PMKID from IE generation).
3157 sm
->assoc_wpa_ie
= os_memdup(wpa_ie
, *wpa_ie_len
);
3158 if (sm
->assoc_wpa_ie
== NULL
)
3161 sm
->assoc_wpa_ie_len
= *wpa_ie_len
;
3163 wpa_hexdump(MSG_DEBUG
,
3164 "WPA: Leave previously set WPA IE default",
3165 sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
);
3173 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3174 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3175 * @ie: Pointer to IE data (starting from id)
3177 * Returns: 0 on success, -1 on failure
3179 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3180 * Request frame. The IE will be used to override the default value generated
3181 * with wpa_sm_set_assoc_wpa_ie_default().
3183 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
3188 os_free(sm
->assoc_wpa_ie
);
3189 if (ie
== NULL
|| len
== 0) {
3190 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3191 "WPA: clearing own WPA/RSN IE");
3192 sm
->assoc_wpa_ie
= NULL
;
3193 sm
->assoc_wpa_ie_len
= 0;
3195 wpa_hexdump(MSG_DEBUG
, "WPA: set own WPA/RSN IE", ie
, len
);
3196 sm
->assoc_wpa_ie
= os_memdup(ie
, len
);
3197 if (sm
->assoc_wpa_ie
== NULL
)
3200 sm
->assoc_wpa_ie_len
= len
;
3208 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3209 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3210 * @ie: Pointer to IE data (starting from id)
3212 * Returns: 0 on success, -1 on failure
3214 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3217 int wpa_sm_set_ap_wpa_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
3222 os_free(sm
->ap_wpa_ie
);
3223 if (ie
== NULL
|| len
== 0) {
3224 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3225 "WPA: clearing AP WPA IE");
3226 sm
->ap_wpa_ie
= NULL
;
3227 sm
->ap_wpa_ie_len
= 0;
3229 wpa_hexdump(MSG_DEBUG
, "WPA: set AP WPA IE", ie
, len
);
3230 sm
->ap_wpa_ie
= os_memdup(ie
, len
);
3231 if (sm
->ap_wpa_ie
== NULL
)
3234 sm
->ap_wpa_ie_len
= len
;
3242 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3243 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3244 * @ie: Pointer to IE data (starting from id)
3246 * Returns: 0 on success, -1 on failure
3248 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3251 int wpa_sm_set_ap_rsn_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
3256 os_free(sm
->ap_rsn_ie
);
3257 if (ie
== NULL
|| len
== 0) {
3258 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3259 "WPA: clearing AP RSN IE");
3260 sm
->ap_rsn_ie
= NULL
;
3261 sm
->ap_rsn_ie_len
= 0;
3263 wpa_hexdump(MSG_DEBUG
, "WPA: set AP RSN IE", ie
, len
);
3264 sm
->ap_rsn_ie
= os_memdup(ie
, len
);
3265 if (sm
->ap_rsn_ie
== NULL
)
3268 sm
->ap_rsn_ie_len
= len
;
3276 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3277 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3278 * @data: Pointer to data area for parsing results
3279 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3281 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3282 * parsed data into data.
3284 int wpa_sm_parse_own_wpa_ie(struct wpa_sm
*sm
, struct wpa_ie_data
*data
)
3289 if (sm
->assoc_wpa_ie
== NULL
) {
3290 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3291 "WPA: No WPA/RSN IE available from association info");
3294 if (wpa_parse_wpa_ie(sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
, data
))
3300 int wpa_sm_pmksa_cache_list(struct wpa_sm
*sm
, char *buf
, size_t len
)
3302 return pmksa_cache_list(sm
->pmksa
, buf
, len
);
3306 struct rsn_pmksa_cache_entry
* wpa_sm_pmksa_cache_head(struct wpa_sm
*sm
)
3308 return pmksa_cache_head(sm
->pmksa
);
3312 struct rsn_pmksa_cache_entry
*
3313 wpa_sm_pmksa_cache_add_entry(struct wpa_sm
*sm
,
3314 struct rsn_pmksa_cache_entry
* entry
)
3316 return pmksa_cache_add_entry(sm
->pmksa
, entry
);
3320 void wpa_sm_pmksa_cache_add(struct wpa_sm
*sm
, const u8
*pmk
, size_t pmk_len
,
3321 const u8
*pmkid
, const u8
*bssid
,
3322 const u8
*fils_cache_id
)
3324 sm
->cur_pmksa
= pmksa_cache_add(sm
->pmksa
, pmk
, pmk_len
, pmkid
, NULL
, 0,
3325 bssid
, sm
->own_addr
, sm
->network_ctx
,
3326 sm
->key_mgmt
, fils_cache_id
);
3330 int wpa_sm_pmksa_exists(struct wpa_sm
*sm
, const u8
*bssid
,
3331 const void *network_ctx
)
3333 return pmksa_cache_get(sm
->pmksa
, bssid
, NULL
, network_ctx
, 0) != NULL
;
3337 void wpa_sm_drop_sa(struct wpa_sm
*sm
)
3339 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Clear old PMK and PTK");
3343 os_memset(sm
->pmk
, 0, sizeof(sm
->pmk
));
3344 os_memset(&sm
->ptk
, 0, sizeof(sm
->ptk
));
3345 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
3346 os_memset(&sm
->gtk
, 0, sizeof(sm
->gtk
));
3347 os_memset(&sm
->gtk_wnm_sleep
, 0, sizeof(sm
->gtk_wnm_sleep
));
3348 #ifdef CONFIG_IEEE80211W
3349 os_memset(&sm
->igtk
, 0, sizeof(sm
->igtk
));
3350 os_memset(&sm
->igtk_wnm_sleep
, 0, sizeof(sm
->igtk_wnm_sleep
));
3351 #endif /* CONFIG_IEEE80211W */
3352 #ifdef CONFIG_IEEE80211R
3353 os_memset(sm
->xxkey
, 0, sizeof(sm
->xxkey
));
3355 os_memset(sm
->pmk_r0
, 0, sizeof(sm
->pmk_r0
));
3357 os_memset(sm
->pmk_r1
, 0, sizeof(sm
->pmk_r1
));
3359 #endif /* CONFIG_IEEE80211R */
3363 int wpa_sm_has_ptk(struct wpa_sm
*sm
)
3371 void wpa_sm_update_replay_ctr(struct wpa_sm
*sm
, const u8
*replay_ctr
)
3373 os_memcpy(sm
->rx_replay_counter
, replay_ctr
, WPA_REPLAY_COUNTER_LEN
);
3377 void wpa_sm_pmksa_cache_flush(struct wpa_sm
*sm
, void *network_ctx
)
3379 pmksa_cache_flush(sm
->pmksa
, network_ctx
, NULL
, 0);
3384 int wpa_wnmsleep_install_key(struct wpa_sm
*sm
, u8 subelem_id
, u8
*buf
)
3387 u8 keylen
; /* plaintext key len */
3390 if (subelem_id
== WNM_SLEEP_SUBELEM_GTK
) {
3391 struct wpa_gtk_data gd
;
3393 os_memset(&gd
, 0, sizeof(gd
));
3394 keylen
= wpa_cipher_key_len(sm
->group_cipher
);
3395 gd
.key_rsc_len
= wpa_cipher_rsc_len(sm
->group_cipher
);
3396 gd
.alg
= wpa_cipher_to_alg(sm
->group_cipher
);
3397 if (gd
.alg
== WPA_ALG_NONE
) {
3398 wpa_printf(MSG_DEBUG
, "Unsupported group cipher suite");
3403 keyinfo
= WPA_GET_LE16(buf
+ 2);
3404 gd
.gtk_len
= keylen
;
3405 if (gd
.gtk_len
!= buf
[4]) {
3406 wpa_printf(MSG_DEBUG
, "GTK len mismatch len %d vs %d",
3407 gd
.gtk_len
, buf
[4]);
3410 gd
.keyidx
= keyinfo
& 0x03; /* B0 - B1 */
3411 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(
3412 sm
, !!(keyinfo
& WPA_KEY_INFO_TXRX
));
3414 os_memcpy(gd
.gtk
, buf
+ 13, gd
.gtk_len
);
3416 wpa_hexdump_key(MSG_DEBUG
, "Install GTK (WNM SLEEP)",
3417 gd
.gtk
, gd
.gtk_len
);
3418 if (wpa_supplicant_install_gtk(sm
, &gd
, key_rsc
, 1)) {
3419 os_memset(&gd
, 0, sizeof(gd
));
3420 wpa_printf(MSG_DEBUG
, "Failed to install the GTK in "
3424 os_memset(&gd
, 0, sizeof(gd
));
3425 #ifdef CONFIG_IEEE80211W
3426 } else if (subelem_id
== WNM_SLEEP_SUBELEM_IGTK
) {
3427 const struct wpa_igtk_kde
*igtk
;
3429 igtk
= (const struct wpa_igtk_kde
*) (buf
+ 2);
3430 if (wpa_supplicant_install_igtk(sm
, igtk
, 1) < 0)
3432 #endif /* CONFIG_IEEE80211W */
3434 wpa_printf(MSG_DEBUG
, "Unknown element id");
3440 #endif /* CONFIG_WNM */
3445 int wpa_sm_get_p2p_ip_addr(struct wpa_sm
*sm
, u8
*buf
)
3447 if (sm
== NULL
|| WPA_GET_BE32(sm
->p2p_ip_addr
) == 0)
3449 os_memcpy(buf
, sm
->p2p_ip_addr
, 3 * 4);
3453 #endif /* CONFIG_P2P */
3456 void wpa_sm_set_rx_replay_ctr(struct wpa_sm
*sm
, const u8
*rx_replay_counter
)
3458 if (rx_replay_counter
== NULL
)
3461 os_memcpy(sm
->rx_replay_counter
, rx_replay_counter
,
3462 WPA_REPLAY_COUNTER_LEN
);
3463 sm
->rx_replay_counter_set
= 1;
3464 wpa_printf(MSG_DEBUG
, "Updated key replay counter");
3468 void wpa_sm_set_ptk_kck_kek(struct wpa_sm
*sm
,
3469 const u8
*ptk_kck
, size_t ptk_kck_len
,
3470 const u8
*ptk_kek
, size_t ptk_kek_len
)
3472 if (ptk_kck
&& ptk_kck_len
<= WPA_KCK_MAX_LEN
) {
3473 os_memcpy(sm
->ptk
.kck
, ptk_kck
, ptk_kck_len
);
3474 sm
->ptk
.kck_len
= ptk_kck_len
;
3475 wpa_printf(MSG_DEBUG
, "Updated PTK KCK");
3477 if (ptk_kek
&& ptk_kek_len
<= WPA_KEK_MAX_LEN
) {
3478 os_memcpy(sm
->ptk
.kek
, ptk_kek
, ptk_kek_len
);
3479 sm
->ptk
.kek_len
= ptk_kek_len
;
3480 wpa_printf(MSG_DEBUG
, "Updated PTK KEK");
3486 #ifdef CONFIG_TESTING_OPTIONS
3488 void wpa_sm_set_test_assoc_ie(struct wpa_sm
*sm
, struct wpabuf
*buf
)
3490 wpabuf_free(sm
->test_assoc_ie
);
3491 sm
->test_assoc_ie
= buf
;
3495 const u8
* wpa_sm_get_anonce(struct wpa_sm
*sm
)
3500 #endif /* CONFIG_TESTING_OPTIONS */
3503 unsigned int wpa_sm_get_key_mgmt(struct wpa_sm
*sm
)
3505 return sm
->key_mgmt
;
3511 struct wpabuf
* fils_build_auth(struct wpa_sm
*sm
, int dh_group
, const u8
*md
)
3513 struct wpabuf
*buf
= NULL
;
3514 struct wpabuf
*erp_msg
;
3515 struct wpabuf
*pub
= NULL
;
3517 erp_msg
= eapol_sm_build_erp_reauth_start(sm
->eapol
);
3518 if (!erp_msg
&& !sm
->cur_pmksa
) {
3519 wpa_printf(MSG_DEBUG
,
3520 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3524 wpa_printf(MSG_DEBUG
, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3525 erp_msg
!= NULL
, sm
->cur_pmksa
!= NULL
);
3527 sm
->fils_completed
= 0;
3529 if (!sm
->assoc_wpa_ie
) {
3530 wpa_printf(MSG_INFO
, "FILS: No own RSN IE set for FILS");
3534 if (random_get_bytes(sm
->fils_nonce
, FILS_NONCE_LEN
) < 0 ||
3535 random_get_bytes(sm
->fils_session
, FILS_SESSION_LEN
) < 0)
3538 wpa_hexdump(MSG_DEBUG
, "FILS: Generated FILS Nonce",
3539 sm
->fils_nonce
, FILS_NONCE_LEN
);
3540 wpa_hexdump(MSG_DEBUG
, "FILS: Generated FILS Session",
3541 sm
->fils_session
, FILS_SESSION_LEN
);
3543 #ifdef CONFIG_FILS_SK_PFS
3544 sm
->fils_dh_group
= dh_group
;
3546 crypto_ecdh_deinit(sm
->fils_ecdh
);
3547 sm
->fils_ecdh
= crypto_ecdh_init(dh_group
);
3548 if (!sm
->fils_ecdh
) {
3549 wpa_printf(MSG_INFO
,
3550 "FILS: Could not initialize ECDH with group %d",
3554 pub
= crypto_ecdh_get_pubkey(sm
->fils_ecdh
, 1);
3557 wpa_hexdump_buf(MSG_DEBUG
, "FILS: Element (DH public key)",
3559 sm
->fils_dh_elem_len
= wpabuf_len(pub
);
3561 #endif /* CONFIG_FILS_SK_PFS */
3563 buf
= wpabuf_alloc(1000 + sm
->assoc_wpa_ie_len
+
3564 (pub
? wpabuf_len(pub
) : 0));
3568 /* Fields following the Authentication algorithm number field */
3570 /* Authentication Transaction seq# */
3571 wpabuf_put_le16(buf
, 1);
3574 wpabuf_put_le16(buf
, WLAN_STATUS_SUCCESS
);
3577 #ifdef CONFIG_FILS_SK_PFS
3579 /* Finite Cyclic Group */
3580 wpabuf_put_le16(buf
, dh_group
);
3582 wpabuf_put_buf(buf
, pub
);
3584 #endif /* CONFIG_FILS_SK_PFS */
3587 wpa_hexdump(MSG_DEBUG
, "FILS: RSNE in FILS Authentication frame",
3588 sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
);
3589 wpabuf_put_data(buf
, sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
);
3592 /* MDE when using FILS for FT initial association */
3593 struct rsn_mdie
*mdie
;
3595 wpabuf_put_u8(buf
, WLAN_EID_MOBILITY_DOMAIN
);
3596 wpabuf_put_u8(buf
, sizeof(*mdie
));
3597 mdie
= wpabuf_put(buf
, sizeof(*mdie
));
3598 os_memcpy(mdie
->mobility_domain
, md
, MOBILITY_DOMAIN_ID_LEN
);
3603 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3604 wpabuf_put_u8(buf
, 1 + FILS_NONCE_LEN
); /* Length */
3605 /* Element ID Extension */
3606 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_NONCE
);
3607 wpabuf_put_data(buf
, sm
->fils_nonce
, FILS_NONCE_LEN
);
3610 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3611 wpabuf_put_u8(buf
, 1 + FILS_SESSION_LEN
); /* Length */
3612 /* Element ID Extension */
3613 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_SESSION
);
3614 wpabuf_put_data(buf
, sm
->fils_session
, FILS_SESSION_LEN
);
3616 /* FILS Wrapped Data */
3617 sm
->fils_erp_pmkid_set
= 0;
3619 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3620 wpabuf_put_u8(buf
, 1 + wpabuf_len(erp_msg
)); /* Length */
3621 /* Element ID Extension */
3622 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_WRAPPED_DATA
);
3623 wpabuf_put_buf(buf
, erp_msg
);
3624 /* Calculate pending PMKID here so that we do not need to
3625 * maintain a copy of the EAP-Initiate/Reauth message. */
3626 if (fils_pmkid_erp(sm
->key_mgmt
, wpabuf_head(erp_msg
),
3627 wpabuf_len(erp_msg
),
3628 sm
->fils_erp_pmkid
) == 0)
3629 sm
->fils_erp_pmkid_set
= 1;
3632 wpa_hexdump_buf(MSG_DEBUG
, "RSN: FILS fields for Authentication frame",
3636 wpabuf_free(erp_msg
);
3642 int fils_process_auth(struct wpa_sm
*sm
, const u8
*bssid
, const u8
*data
,
3645 const u8
*pos
, *end
;
3646 struct ieee802_11_elems elems
;
3647 struct wpa_ie_data rsn
;
3648 int pmkid_match
= 0;
3649 u8 ick
[FILS_ICK_MAX_LEN
];
3652 struct wpabuf
*dh_ss
= NULL
;
3653 const u8
*g_sta
= NULL
;
3654 size_t g_sta_len
= 0;
3655 const u8
*g_ap
= NULL
;
3656 size_t g_ap_len
= 0;
3657 struct wpabuf
*pub
= NULL
;
3659 os_memcpy(sm
->bssid
, bssid
, ETH_ALEN
);
3661 wpa_hexdump(MSG_DEBUG
, "FILS: Authentication frame fields",
3667 #ifdef CONFIG_FILS_SK_PFS
3668 if (sm
->fils_dh_group
) {
3671 /* Using FILS PFS */
3673 /* Finite Cyclic Group */
3674 if (end
- pos
< 2) {
3675 wpa_printf(MSG_DEBUG
,
3676 "FILS: No room for Finite Cyclic Group");
3679 group
= WPA_GET_LE16(pos
);
3681 if (group
!= sm
->fils_dh_group
) {
3682 wpa_printf(MSG_DEBUG
,
3683 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
3684 group
, sm
->fils_dh_group
);
3689 if ((size_t) (end
- pos
) < sm
->fils_dh_elem_len
) {
3690 wpa_printf(MSG_DEBUG
, "FILS: No room for Element");
3694 if (!sm
->fils_ecdh
) {
3695 wpa_printf(MSG_DEBUG
, "FILS: No ECDH state available");
3698 dh_ss
= crypto_ecdh_set_peerkey(sm
->fils_ecdh
, 1, pos
,
3699 sm
->fils_dh_elem_len
);
3701 wpa_printf(MSG_DEBUG
, "FILS: ECDH operation failed");
3704 wpa_hexdump_buf_key(MSG_DEBUG
, "FILS: DH_SS", dh_ss
);
3706 g_ap_len
= sm
->fils_dh_elem_len
;
3707 pos
+= sm
->fils_dh_elem_len
;
3709 #endif /* CONFIG_FILS_SK_PFS */
3711 wpa_hexdump(MSG_DEBUG
, "FILS: Remaining IEs", pos
, end
- pos
);
3712 if (ieee802_11_parse_elems(pos
, end
- pos
, &elems
, 1) == ParseFailed
) {
3713 wpa_printf(MSG_DEBUG
, "FILS: Could not parse elements");
3718 wpa_hexdump(MSG_DEBUG
, "FILS: RSN element", elems
.rsn_ie
,
3720 if (!elems
.rsn_ie
||
3721 wpa_parse_wpa_ie_rsn(elems
.rsn_ie
- 2, elems
.rsn_ie_len
+ 2,
3723 wpa_printf(MSG_DEBUG
, "FILS: No RSN element");
3727 if (!elems
.fils_nonce
) {
3728 wpa_printf(MSG_DEBUG
, "FILS: No FILS Nonce field");
3731 os_memcpy(sm
->fils_anonce
, elems
.fils_nonce
, FILS_NONCE_LEN
);
3732 wpa_hexdump(MSG_DEBUG
, "FILS: ANonce", sm
->fils_anonce
, FILS_NONCE_LEN
);
3734 #ifdef CONFIG_IEEE80211R
3735 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
3736 struct wpa_ft_ies parse
;
3738 if (!elems
.mdie
|| !elems
.ftie
) {
3739 wpa_printf(MSG_DEBUG
, "FILS+FT: No MDE or FTE");
3743 if (wpa_ft_parse_ies(pos
, end
- pos
, &parse
,
3744 wpa_key_mgmt_sha384(sm
->key_mgmt
)) < 0) {
3745 wpa_printf(MSG_DEBUG
, "FILS+FT: Failed to parse IEs");
3749 if (!parse
.r0kh_id
) {
3750 wpa_printf(MSG_DEBUG
,
3751 "FILS+FT: No R0KH-ID subelem in FTE");
3754 os_memcpy(sm
->r0kh_id
, parse
.r0kh_id
, parse
.r0kh_id_len
);
3755 sm
->r0kh_id_len
= parse
.r0kh_id_len
;
3756 wpa_hexdump_ascii(MSG_DEBUG
, "FILS+FT: R0KH-ID",
3757 sm
->r0kh_id
, sm
->r0kh_id_len
);
3759 if (!parse
.r1kh_id
) {
3760 wpa_printf(MSG_DEBUG
,
3761 "FILS+FT: No R1KH-ID subelem in FTE");
3764 os_memcpy(sm
->r1kh_id
, parse
.r1kh_id
, FT_R1KH_ID_LEN
);
3765 wpa_hexdump(MSG_DEBUG
, "FILS+FT: R1KH-ID",
3766 sm
->r1kh_id
, FT_R1KH_ID_LEN
);
3768 /* TODO: Check MDE and FTE payload */
3770 wpabuf_free(sm
->fils_ft_ies
);
3771 sm
->fils_ft_ies
= wpabuf_alloc(2 + elems
.mdie_len
+
3772 2 + elems
.ftie_len
);
3773 if (!sm
->fils_ft_ies
)
3775 wpabuf_put_data(sm
->fils_ft_ies
, elems
.mdie
- 2,
3776 2 + elems
.mdie_len
);
3777 wpabuf_put_data(sm
->fils_ft_ies
, elems
.ftie
- 2,
3778 2 + elems
.ftie_len
);
3780 wpabuf_free(sm
->fils_ft_ies
);
3781 sm
->fils_ft_ies
= NULL
;
3783 #endif /* CONFIG_IEEE80211R */
3786 if (rsn
.pmkid
&& rsn
.num_pmkid
> 0) {
3787 wpa_hexdump(MSG_DEBUG
, "FILS: PMKID List",
3788 rsn
.pmkid
, rsn
.num_pmkid
* PMKID_LEN
);
3790 if (rsn
.num_pmkid
!= 1) {
3791 wpa_printf(MSG_DEBUG
, "FILS: Invalid PMKID selection");
3794 wpa_hexdump(MSG_DEBUG
, "FILS: PMKID", rsn
.pmkid
, PMKID_LEN
);
3795 if (os_memcmp(sm
->cur_pmksa
->pmkid
, rsn
.pmkid
, PMKID_LEN
) != 0)
3797 wpa_printf(MSG_DEBUG
, "FILS: PMKID mismatch");
3798 wpa_hexdump(MSG_DEBUG
, "FILS: Expected PMKID",
3799 sm
->cur_pmksa
->pmkid
, PMKID_LEN
);
3802 wpa_printf(MSG_DEBUG
,
3803 "FILS: Matching PMKID - continue using PMKSA caching");
3806 if (!pmkid_match
&& sm
->cur_pmksa
) {
3807 wpa_printf(MSG_DEBUG
,
3808 "FILS: No PMKID match - cannot use cached PMKSA entry");
3809 sm
->cur_pmksa
= NULL
;
3813 if (!elems
.fils_session
) {
3814 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
3817 wpa_hexdump(MSG_DEBUG
, "FILS: FILS Session", elems
.fils_session
,
3819 if (os_memcmp(sm
->fils_session
, elems
.fils_session
, FILS_SESSION_LEN
)
3821 wpa_printf(MSG_DEBUG
, "FILS: Session mismatch");
3822 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
3823 sm
->fils_session
, FILS_SESSION_LEN
);
3827 /* FILS Wrapped Data */
3828 if (!sm
->cur_pmksa
&& elems
.fils_wrapped_data
) {
3829 u8 rmsk
[ERP_MAX_KEY_LEN
];
3832 wpa_hexdump(MSG_DEBUG
, "FILS: Wrapped Data",
3833 elems
.fils_wrapped_data
,
3834 elems
.fils_wrapped_data_len
);
3835 eapol_sm_process_erp_finish(sm
->eapol
, elems
.fils_wrapped_data
,
3836 elems
.fils_wrapped_data_len
);
3837 if (eapol_sm_failed(sm
->eapol
))
3840 rmsk_len
= ERP_MAX_KEY_LEN
;
3841 res
= eapol_sm_get_key(sm
->eapol
, rmsk
, rmsk_len
);
3842 if (res
== PMK_LEN
) {
3844 res
= eapol_sm_get_key(sm
->eapol
, rmsk
, rmsk_len
);
3849 res
= fils_rmsk_to_pmk(sm
->key_mgmt
, rmsk
, rmsk_len
,
3850 sm
->fils_nonce
, sm
->fils_anonce
,
3851 dh_ss
? wpabuf_head(dh_ss
) : NULL
,
3852 dh_ss
? wpabuf_len(dh_ss
) : 0,
3853 sm
->pmk
, &sm
->pmk_len
);
3854 os_memset(rmsk
, 0, sizeof(rmsk
));
3856 /* Don't use DHss in PTK derivation if PMKSA caching is not
3858 wpabuf_clear_free(dh_ss
);
3864 if (!sm
->fils_erp_pmkid_set
) {
3865 wpa_printf(MSG_DEBUG
, "FILS: PMKID not available");
3868 wpa_hexdump(MSG_DEBUG
, "FILS: PMKID", sm
->fils_erp_pmkid
,
3870 wpa_printf(MSG_DEBUG
, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
3871 sm
->cur_pmksa
= pmksa_cache_add(sm
->pmksa
, sm
->pmk
, sm
->pmk_len
,
3872 sm
->fils_erp_pmkid
, NULL
, 0,
3873 sm
->bssid
, sm
->own_addr
,
3874 sm
->network_ctx
, sm
->key_mgmt
,
3878 if (!sm
->cur_pmksa
) {
3879 wpa_printf(MSG_DEBUG
,
3880 "FILS: No remaining options to continue FILS authentication");
3884 if (fils_pmk_to_ptk(sm
->pmk
, sm
->pmk_len
, sm
->own_addr
, sm
->bssid
,
3885 sm
->fils_nonce
, sm
->fils_anonce
,
3886 dh_ss
? wpabuf_head(dh_ss
) : NULL
,
3887 dh_ss
? wpabuf_len(dh_ss
) : 0,
3888 &sm
->ptk
, ick
, &ick_len
,
3889 sm
->key_mgmt
, sm
->pairwise_cipher
,
3890 sm
->fils_ft
, &sm
->fils_ft_len
) < 0) {
3891 wpa_printf(MSG_DEBUG
, "FILS: Failed to derive PTK");
3895 wpabuf_clear_free(dh_ss
);
3900 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
3902 #ifdef CONFIG_FILS_SK_PFS
3903 if (sm
->fils_dh_group
) {
3904 if (!sm
->fils_ecdh
) {
3905 wpa_printf(MSG_INFO
, "FILS: ECDH not initialized");
3908 pub
= crypto_ecdh_get_pubkey(sm
->fils_ecdh
, 1);
3911 wpa_hexdump_buf(MSG_DEBUG
, "FILS: gSTA", pub
);
3912 g_sta
= wpabuf_head(pub
);
3913 g_sta_len
= wpabuf_len(pub
);
3915 wpa_printf(MSG_INFO
, "FILS: gAP not available");
3918 wpa_hexdump(MSG_DEBUG
, "FILS: gAP", g_ap
, g_ap_len
);
3920 #endif /* CONFIG_FILS_SK_PFS */
3922 res
= fils_key_auth_sk(ick
, ick_len
, sm
->fils_nonce
,
3923 sm
->fils_anonce
, sm
->own_addr
, sm
->bssid
,
3924 g_sta
, g_sta_len
, g_ap
, g_ap_len
,
3925 sm
->key_mgmt
, sm
->fils_key_auth_sta
,
3926 sm
->fils_key_auth_ap
,
3927 &sm
->fils_key_auth_len
);
3929 os_memset(ick
, 0, sizeof(ick
));
3933 wpabuf_clear_free(dh_ss
);
3938 #ifdef CONFIG_IEEE80211R
3939 static int fils_ft_build_assoc_req_rsne(struct wpa_sm
*sm
, struct wpabuf
*buf
)
3941 struct rsn_ie_hdr
*rsnie
;
3944 int use_sha384
= wpa_key_mgmt_sha384(sm
->key_mgmt
);
3946 /* RSNIE[PMKR0Name/PMKR1Name] */
3947 rsnie
= wpabuf_put(buf
, sizeof(*rsnie
));
3948 rsnie
->elem_id
= WLAN_EID_RSN
;
3949 WPA_PUT_LE16(rsnie
->version
, RSN_VERSION
);
3951 /* Group Suite Selector */
3952 if (!wpa_cipher_valid_group(sm
->group_cipher
)) {
3953 wpa_printf(MSG_WARNING
, "FT: Invalid group cipher (%d)",
3957 pos
= wpabuf_put(buf
, RSN_SELECTOR_LEN
);
3958 RSN_SELECTOR_PUT(pos
, wpa_cipher_to_suite(WPA_PROTO_RSN
,
3961 /* Pairwise Suite Count */
3962 wpabuf_put_le16(buf
, 1);
3964 /* Pairwise Suite List */
3965 if (!wpa_cipher_valid_pairwise(sm
->pairwise_cipher
)) {
3966 wpa_printf(MSG_WARNING
, "FT: Invalid pairwise cipher (%d)",
3967 sm
->pairwise_cipher
);
3970 pos
= wpabuf_put(buf
, RSN_SELECTOR_LEN
);
3971 RSN_SELECTOR_PUT(pos
, wpa_cipher_to_suite(WPA_PROTO_RSN
,
3972 sm
->pairwise_cipher
));
3974 /* Authenticated Key Management Suite Count */
3975 wpabuf_put_le16(buf
, 1);
3977 /* Authenticated Key Management Suite List */
3978 pos
= wpabuf_put(buf
, RSN_SELECTOR_LEN
);
3979 if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_FILS_SHA256
)
3980 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256
);
3981 else if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_FILS_SHA384
)
3982 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384
);
3984 wpa_printf(MSG_WARNING
,
3985 "FILS+FT: Invalid key management type (%d)",
3990 /* RSN Capabilities */
3992 #ifdef CONFIG_IEEE80211W
3993 if (sm
->mgmt_group_cipher
== WPA_CIPHER_AES_128_CMAC
)
3994 capab
|= WPA_CAPABILITY_MFPC
;
3995 #endif /* CONFIG_IEEE80211W */
3997 capab
|= WPA_CAPABILITY_OCVC
;
3998 wpabuf_put_le16(buf
, capab
);
4001 wpabuf_put_le16(buf
, 1);
4003 /* PMKID List [PMKR1Name] */
4004 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: XXKey (FILS-FT)",
4005 sm
->fils_ft
, sm
->fils_ft_len
);
4006 wpa_hexdump_ascii(MSG_DEBUG
, "FILS+FT: SSID", sm
->ssid
, sm
->ssid_len
);
4007 wpa_hexdump(MSG_DEBUG
, "FILS+FT: MDID",
4008 sm
->mobility_domain
, MOBILITY_DOMAIN_ID_LEN
);
4009 wpa_hexdump_ascii(MSG_DEBUG
, "FILS+FT: R0KH-ID",
4010 sm
->r0kh_id
, sm
->r0kh_id_len
);
4011 if (wpa_derive_pmk_r0(sm
->fils_ft
, sm
->fils_ft_len
, sm
->ssid
,
4012 sm
->ssid_len
, sm
->mobility_domain
,
4013 sm
->r0kh_id
, sm
->r0kh_id_len
, sm
->own_addr
,
4014 sm
->pmk_r0
, sm
->pmk_r0_name
, use_sha384
) < 0) {
4015 wpa_printf(MSG_WARNING
, "FILS+FT: Could not derive PMK-R0");
4018 sm
->pmk_r0_len
= use_sha384
? SHA384_MAC_LEN
: PMK_LEN
;
4019 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: PMK-R0",
4020 sm
->pmk_r0
, sm
->pmk_r0_len
);
4021 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR0Name",
4022 sm
->pmk_r0_name
, WPA_PMK_NAME_LEN
);
4023 wpa_printf(MSG_DEBUG
, "FILS+FT: R1KH-ID: " MACSTR
,
4024 MAC2STR(sm
->r1kh_id
));
4025 pos
= wpabuf_put(buf
, WPA_PMK_NAME_LEN
);
4026 if (wpa_derive_pmk_r1_name(sm
->pmk_r0_name
, sm
->r1kh_id
, sm
->own_addr
,
4027 sm
->pmk_r1_name
, use_sha384
) < 0) {
4028 wpa_printf(MSG_WARNING
, "FILS+FT: Could not derive PMKR1Name");
4031 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR1Name", sm
->pmk_r1_name
,
4033 os_memcpy(pos
, sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
4035 #ifdef CONFIG_IEEE80211W
4036 if (sm
->mgmt_group_cipher
== WPA_CIPHER_AES_128_CMAC
) {
4037 /* Management Group Cipher Suite */
4038 pos
= wpabuf_put(buf
, RSN_SELECTOR_LEN
);
4039 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_AES_128_CMAC
);
4041 #endif /* CONFIG_IEEE80211W */
4043 rsnie
->len
= ((u8
*) wpabuf_put(buf
, 0) - (u8
*) rsnie
) - 2;
4046 #endif /* CONFIG_IEEE80211R */
4049 struct wpabuf
* fils_build_assoc_req(struct wpa_sm
*sm
, const u8
**kek
,
4050 size_t *kek_len
, const u8
**snonce
,
4052 const struct wpabuf
**hlp
,
4053 unsigned int num_hlp
)
4060 #ifdef CONFIG_IEEE80211R
4061 if (sm
->fils_ft_ies
)
4062 len
+= wpabuf_len(sm
->fils_ft_ies
);
4063 if (wpa_key_mgmt_ft(sm
->key_mgmt
))
4065 #endif /* CONFIG_IEEE80211R */
4066 for (i
= 0; hlp
&& i
< num_hlp
; i
++)
4067 len
+= 10 + wpabuf_len(hlp
[i
]);
4068 buf
= wpabuf_alloc(len
);
4072 #ifdef CONFIG_IEEE80211R
4073 if (wpa_key_mgmt_ft(sm
->key_mgmt
) && sm
->fils_ft_ies
) {
4074 /* MDE and FTE when using FILS+FT */
4075 wpabuf_put_buf(buf
, sm
->fils_ft_ies
);
4076 /* RSNE with PMKR1Name in PMKID field */
4077 if (fils_ft_build_assoc_req_rsne(sm
, buf
) < 0) {
4082 #endif /* CONFIG_IEEE80211R */
4085 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
4086 wpabuf_put_u8(buf
, 1 + FILS_SESSION_LEN
); /* Length */
4087 /* Element ID Extension */
4088 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_SESSION
);
4089 wpabuf_put_data(buf
, sm
->fils_session
, FILS_SESSION_LEN
);
4091 /* Everything after FILS Session element gets encrypted in the driver
4092 * with KEK. The buffer returned from here is the plaintext version. */
4094 /* TODO: FILS Public Key */
4096 /* FILS Key Confirm */
4097 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
4098 wpabuf_put_u8(buf
, 1 + sm
->fils_key_auth_len
); /* Length */
4099 /* Element ID Extension */
4100 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_KEY_CONFIRM
);
4101 wpabuf_put_data(buf
, sm
->fils_key_auth_sta
, sm
->fils_key_auth_len
);
4103 /* FILS HLP Container */
4104 for (i
= 0; hlp
&& i
< num_hlp
; i
++) {
4105 const u8
*pos
= wpabuf_head(hlp
[i
]);
4106 size_t left
= wpabuf_len(hlp
[i
]);
4108 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
4113 wpabuf_put_u8(buf
, len
); /* Length */
4114 /* Element ID Extension */
4115 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_HLP_CONTAINER
);
4116 /* Destination MAC Address, Source MAC Address, HLP Packet.
4117 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4118 * header when LPD is used). */
4119 wpabuf_put_data(buf
, pos
, len
- 1);
4123 wpabuf_put_u8(buf
, WLAN_EID_FRAGMENT
);
4124 len
= left
> 255 ? 255 : left
;
4125 wpabuf_put_u8(buf
, len
);
4126 wpabuf_put_data(buf
, pos
, len
);
4132 /* TODO: FILS IP Address Assignment */
4135 if (wpa_sm_ocv_enabled(sm
)) {
4136 struct wpa_channel_info ci
;
4139 if (wpa_sm_channel_info(sm
, &ci
) != 0) {
4140 wpa_printf(MSG_WARNING
,
4141 "FILS: Failed to get channel info for OCI element");
4146 pos
= wpabuf_put(buf
, OCV_OCI_EXTENDED_LEN
);
4147 if (ocv_insert_extended_oci(&ci
, pos
) < 0) {
4152 #endif /* CONFIG_OCV */
4154 wpa_hexdump_buf(MSG_DEBUG
, "FILS: Association Request plaintext", buf
);
4157 *kek_len
= sm
->ptk
.kek_len
;
4158 wpa_hexdump_key(MSG_DEBUG
, "FILS: KEK for AEAD", *kek
, *kek_len
);
4159 *snonce
= sm
->fils_nonce
;
4160 wpa_hexdump(MSG_DEBUG
, "FILS: SNonce for AEAD AAD",
4161 *snonce
, FILS_NONCE_LEN
);
4162 *anonce
= sm
->fils_anonce
;
4163 wpa_hexdump(MSG_DEBUG
, "FILS: ANonce for AEAD AAD",
4164 *anonce
, FILS_NONCE_LEN
);
4170 static void fils_process_hlp_resp(struct wpa_sm
*sm
, const u8
*resp
, size_t len
)
4172 const u8
*pos
, *end
;
4174 wpa_hexdump(MSG_MSGDUMP
, "FILS: HLP response", resp
, len
);
4175 if (len
< 2 * ETH_ALEN
)
4177 pos
= resp
+ 2 * ETH_ALEN
;
4179 if (end
- pos
>= 6 &&
4180 os_memcmp(pos
, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4181 pos
+= 6; /* Remove SNAP/LLC header */
4182 wpa_sm_fils_hlp_rx(sm
, resp
, resp
+ ETH_ALEN
, pos
, end
- pos
);
4186 static void fils_process_hlp_container(struct wpa_sm
*sm
, const u8
*pos
,
4189 const u8
*end
= pos
+ len
;
4192 /* Check if there are any FILS HLP Container elements */
4193 while (end
- pos
>= 2) {
4194 if (2 + pos
[1] > end
- pos
)
4196 if (pos
[0] == WLAN_EID_EXTENSION
&&
4197 pos
[1] >= 1 + 2 * ETH_ALEN
&&
4198 pos
[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER
)
4203 return; /* No FILS HLP Container elements */
4205 tmp
= os_malloc(end
- pos
);
4209 while (end
- pos
>= 2) {
4210 if (2 + pos
[1] > end
- pos
||
4211 pos
[0] != WLAN_EID_EXTENSION
||
4212 pos
[1] < 1 + 2 * ETH_ALEN
||
4213 pos
[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER
)
4216 os_memcpy(tmp_pos
, pos
+ 3, pos
[1] - 1);
4217 tmp_pos
+= pos
[1] - 1;
4220 /* Add possible fragments */
4221 while (end
- pos
>= 2 && pos
[0] == WLAN_EID_FRAGMENT
&&
4222 2 + pos
[1] <= end
- pos
) {
4223 os_memcpy(tmp_pos
, pos
+ 2, pos
[1]);
4228 fils_process_hlp_resp(sm
, tmp
, tmp_pos
- tmp
);
4235 int fils_process_assoc_resp(struct wpa_sm
*sm
, const u8
*resp
, size_t len
)
4237 const struct ieee80211_mgmt
*mgmt
;
4238 const u8
*end
, *ie_start
;
4239 struct ieee802_11_elems elems
;
4242 struct wpa_gtk_data gd
;
4244 struct wpa_eapol_ie_parse kde
;
4246 if (!sm
|| !sm
->ptk_set
) {
4247 wpa_printf(MSG_DEBUG
, "FILS: No KEK available");
4251 if (!wpa_key_mgmt_fils(sm
->key_mgmt
)) {
4252 wpa_printf(MSG_DEBUG
, "FILS: Not a FILS AKM");
4256 if (sm
->fils_completed
) {
4257 wpa_printf(MSG_DEBUG
,
4258 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4262 wpa_hexdump(MSG_DEBUG
, "FILS: (Re)Association Response frame",
4265 mgmt
= (const struct ieee80211_mgmt
*) resp
;
4266 if (len
< IEEE80211_HDRLEN
+ sizeof(mgmt
->u
.assoc_resp
))
4270 /* Same offset for Association Response and Reassociation Response */
4271 ie_start
= mgmt
->u
.assoc_resp
.variable
;
4273 if (ieee802_11_parse_elems(ie_start
, end
- ie_start
, &elems
, 1) ==
4275 wpa_printf(MSG_DEBUG
,
4276 "FILS: Failed to parse decrypted elements");
4280 if (!elems
.fils_session
) {
4281 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
4284 if (os_memcmp(elems
.fils_session
, sm
->fils_session
,
4285 FILS_SESSION_LEN
) != 0) {
4286 wpa_printf(MSG_DEBUG
, "FILS: FILS Session mismatch");
4287 wpa_hexdump(MSG_DEBUG
, "FILS: Received FILS Session",
4288 elems
.fils_session
, FILS_SESSION_LEN
);
4289 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
4290 sm
->fils_session
, FILS_SESSION_LEN
);
4293 /* TODO: FILS Public Key */
4295 if (!elems
.fils_key_confirm
) {
4296 wpa_printf(MSG_DEBUG
, "FILS: No FILS Key Confirm element");
4299 if (elems
.fils_key_confirm_len
!= sm
->fils_key_auth_len
) {
4300 wpa_printf(MSG_DEBUG
,
4301 "FILS: Unexpected Key-Auth length %d (expected %d)",
4302 elems
.fils_key_confirm_len
,
4303 (int) sm
->fils_key_auth_len
);
4306 if (os_memcmp(elems
.fils_key_confirm
, sm
->fils_key_auth_ap
,
4307 sm
->fils_key_auth_len
) != 0) {
4308 wpa_printf(MSG_DEBUG
, "FILS: Key-Auth mismatch");
4309 wpa_hexdump(MSG_DEBUG
, "FILS: Received Key-Auth",
4310 elems
.fils_key_confirm
,
4311 elems
.fils_key_confirm_len
);
4312 wpa_hexdump(MSG_DEBUG
, "FILS: Expected Key-Auth",
4313 sm
->fils_key_auth_ap
, sm
->fils_key_auth_len
);
4318 if (wpa_sm_ocv_enabled(sm
)) {
4319 struct wpa_channel_info ci
;
4321 if (wpa_sm_channel_info(sm
, &ci
) != 0) {
4322 wpa_printf(MSG_WARNING
,
4323 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4327 if (ocv_verify_tx_params(elems
.oci
, elems
.oci_len
, &ci
,
4328 channel_width_to_int(ci
.chanwidth
),
4329 ci
.seg1_idx
) != 0) {
4330 wpa_printf(MSG_WARNING
, "FILS: %s", ocv_errorstr
);
4334 #endif /* CONFIG_OCV */
4336 #ifdef CONFIG_IEEE80211R
4337 if (wpa_key_mgmt_ft(sm
->key_mgmt
) && sm
->fils_ft_ies
) {
4338 struct wpa_ie_data rsn
;
4340 /* Check that PMKR1Name derived by the AP matches */
4341 if (!elems
.rsn_ie
||
4342 wpa_parse_wpa_ie_rsn(elems
.rsn_ie
- 2, elems
.rsn_ie_len
+ 2,
4344 !rsn
.pmkid
|| rsn
.num_pmkid
!= 1 ||
4345 os_memcmp(rsn
.pmkid
, sm
->pmk_r1_name
,
4346 WPA_PMK_NAME_LEN
) != 0) {
4347 wpa_printf(MSG_DEBUG
,
4348 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
4352 #endif /* CONFIG_IEEE80211R */
4355 if (!elems
.key_delivery
) {
4356 wpa_printf(MSG_DEBUG
, "FILS: No Key Delivery element");
4360 /* Parse GTK and set the key to the driver */
4361 os_memset(&gd
, 0, sizeof(gd
));
4362 if (wpa_supplicant_parse_ies(elems
.key_delivery
+ WPA_KEY_RSC_LEN
,
4363 elems
.key_delivery_len
- WPA_KEY_RSC_LEN
,
4365 wpa_printf(MSG_DEBUG
, "FILS: Failed to parse KDEs");
4369 wpa_printf(MSG_DEBUG
, "FILS: No GTK KDE");
4372 maxkeylen
= gd
.gtk_len
= kde
.gtk_len
- 2;
4373 if (wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
4374 gd
.gtk_len
, maxkeylen
,
4375 &gd
.key_rsc_len
, &gd
.alg
))
4378 wpa_hexdump_key(MSG_DEBUG
, "FILS: Received GTK", kde
.gtk
, kde
.gtk_len
);
4379 gd
.keyidx
= kde
.gtk
[0] & 0x3;
4380 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
4381 !!(kde
.gtk
[0] & BIT(2)));
4382 if (kde
.gtk_len
- 2 > sizeof(gd
.gtk
)) {
4383 wpa_printf(MSG_DEBUG
, "FILS: Too long GTK in GTK KDE (len=%lu)",
4384 (unsigned long) kde
.gtk_len
- 2);
4387 os_memcpy(gd
.gtk
, kde
.gtk
+ 2, kde
.gtk_len
- 2);
4389 wpa_printf(MSG_DEBUG
, "FILS: Set GTK to driver");
4390 if (wpa_supplicant_install_gtk(sm
, &gd
, elems
.key_delivery
, 0) < 0) {
4391 wpa_printf(MSG_DEBUG
, "FILS: Failed to set GTK");
4395 if (ieee80211w_set_keys(sm
, &kde
) < 0) {
4396 wpa_printf(MSG_DEBUG
, "FILS: Failed to set IGTK");
4400 alg
= wpa_cipher_to_alg(sm
->pairwise_cipher
);
4401 keylen
= wpa_cipher_key_len(sm
->pairwise_cipher
);
4402 if (keylen
<= 0 || (unsigned int) keylen
!= sm
->ptk
.tk_len
) {
4403 wpa_printf(MSG_DEBUG
, "FILS: TK length mismatch: %u != %lu",
4404 keylen
, (long unsigned int) sm
->ptk
.tk_len
);
4407 rsclen
= wpa_cipher_rsc_len(sm
->pairwise_cipher
);
4408 wpa_hexdump_key(MSG_DEBUG
, "FILS: Set TK to driver",
4409 sm
->ptk
.tk
, keylen
);
4410 if (wpa_sm_set_key(sm
, alg
, sm
->bssid
, 0, 1, null_rsc
, rsclen
,
4411 sm
->ptk
.tk
, keylen
) < 0) {
4412 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
4413 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4415 alg
, keylen
, MAC2STR(sm
->bssid
));
4419 /* TODO: TK could be cleared after auth frame exchange now that driver
4420 * takes care of association frame encryption/decryption. */
4421 /* TK is not needed anymore in supplicant */
4422 os_memset(sm
->ptk
.tk
, 0, WPA_TK_MAX_LEN
);
4424 sm
->ptk
.installed
= 1;
4426 /* FILS HLP Container */
4427 fils_process_hlp_container(sm
, ie_start
, end
- ie_start
);
4429 /* TODO: FILS IP Address Assignment */
4431 wpa_printf(MSG_DEBUG
, "FILS: Auth+Assoc completed successfully");
4432 sm
->fils_completed
= 1;
4440 void wpa_sm_set_reset_fils_completed(struct wpa_sm
*sm
, int set
)
4443 sm
->fils_completed
= !!set
;
4446 #endif /* CONFIG_FILS */
4449 int wpa_fils_is_completed(struct wpa_sm
*sm
)
4452 return sm
&& sm
->fils_completed
;
4453 #else /* CONFIG_FILS */
4455 #endif /* CONFIG_FILS */
4461 struct wpabuf
* owe_build_assoc_req(struct wpa_sm
*sm
, u16 group
)
4463 struct wpabuf
*ie
= NULL
, *pub
= NULL
;
4468 else if (group
== 20)
4470 else if (group
== 21)
4475 crypto_ecdh_deinit(sm
->owe_ecdh
);
4476 sm
->owe_ecdh
= crypto_ecdh_init(group
);
4479 sm
->owe_group
= group
;
4480 pub
= crypto_ecdh_get_pubkey(sm
->owe_ecdh
, 0);
4481 pub
= wpabuf_zeropad(pub
, prime_len
);
4485 ie
= wpabuf_alloc(5 + wpabuf_len(pub
));
4488 wpabuf_put_u8(ie
, WLAN_EID_EXTENSION
);
4489 wpabuf_put_u8(ie
, 1 + 2 + wpabuf_len(pub
));
4490 wpabuf_put_u8(ie
, WLAN_EID_EXT_OWE_DH_PARAM
);
4491 wpabuf_put_le16(ie
, group
);
4492 wpabuf_put_buf(ie
, pub
);
4494 wpa_hexdump_buf(MSG_DEBUG
, "OWE: Diffie-Hellman Parameter element",
4500 crypto_ecdh_deinit(sm
->owe_ecdh
);
4501 sm
->owe_ecdh
= NULL
;
4506 int owe_process_assoc_resp(struct wpa_sm
*sm
, const u8
*bssid
,
4507 const u8
*resp_ies
, size_t resp_ies_len
)
4509 struct ieee802_11_elems elems
;
4511 struct wpabuf
*secret
, *pub
, *hkey
;
4513 u8 prk
[SHA512_MAC_LEN
], pmkid
[SHA512_MAC_LEN
];
4514 const char *info
= "OWE Key Generation";
4517 size_t hash_len
, prime_len
;
4518 struct wpa_ie_data data
;
4521 ieee802_11_parse_elems(resp_ies
, resp_ies_len
, &elems
, 1) ==
4523 wpa_printf(MSG_INFO
,
4524 "OWE: Could not parse Association Response frame elements");
4528 if (sm
->cur_pmksa
&& elems
.rsn_ie
&&
4529 wpa_parse_wpa_ie_rsn(elems
.rsn_ie
- 2, 2 + elems
.rsn_ie_len
,
4531 data
.num_pmkid
== 1 && data
.pmkid
&&
4532 os_memcmp(sm
->cur_pmksa
->pmkid
, data
.pmkid
, PMKID_LEN
) == 0) {
4533 wpa_printf(MSG_DEBUG
, "OWE: Use PMKSA caching");
4534 wpa_sm_set_pmk_from_pmksa(sm
);
4538 if (!elems
.owe_dh
) {
4539 wpa_printf(MSG_INFO
,
4540 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
4544 group
= WPA_GET_LE16(elems
.owe_dh
);
4545 if (group
!= sm
->owe_group
) {
4546 wpa_printf(MSG_INFO
,
4547 "OWE: Unexpected Diffie-Hellman group in response: %u",
4552 if (!sm
->owe_ecdh
) {
4553 wpa_printf(MSG_INFO
, "OWE: No ECDH state available");
4559 else if (group
== 20)
4561 else if (group
== 21)
4566 secret
= crypto_ecdh_set_peerkey(sm
->owe_ecdh
, 0,
4568 elems
.owe_dh_len
- 2);
4569 secret
= wpabuf_zeropad(secret
, prime_len
);
4571 wpa_printf(MSG_DEBUG
, "OWE: Invalid peer DH public key");
4574 wpa_hexdump_buf_key(MSG_DEBUG
, "OWE: DH shared secret", secret
);
4576 /* prk = HKDF-extract(C | A | group, z) */
4578 pub
= crypto_ecdh_get_pubkey(sm
->owe_ecdh
, 0);
4580 wpabuf_clear_free(secret
);
4584 /* PMKID = Truncate-128(Hash(C | A)) */
4585 addr
[0] = wpabuf_head(pub
);
4586 len
[0] = wpabuf_len(pub
);
4587 addr
[1] = elems
.owe_dh
+ 2;
4588 len
[1] = elems
.owe_dh_len
- 2;
4590 res
= sha256_vector(2, addr
, len
, pmkid
);
4591 hash_len
= SHA256_MAC_LEN
;
4592 } else if (group
== 20) {
4593 res
= sha384_vector(2, addr
, len
, pmkid
);
4594 hash_len
= SHA384_MAC_LEN
;
4595 } else if (group
== 21) {
4596 res
= sha512_vector(2, addr
, len
, pmkid
);
4597 hash_len
= SHA512_MAC_LEN
;
4602 pub
= wpabuf_zeropad(pub
, prime_len
);
4603 if (res
< 0 || !pub
) {
4605 wpabuf_clear_free(secret
);
4609 hkey
= wpabuf_alloc(wpabuf_len(pub
) + elems
.owe_dh_len
- 2 + 2);
4612 wpabuf_clear_free(secret
);
4616 wpabuf_put_buf(hkey
, pub
); /* C */
4618 wpabuf_put_data(hkey
, elems
.owe_dh
+ 2, elems
.owe_dh_len
- 2); /* A */
4619 wpabuf_put_le16(hkey
, sm
->owe_group
); /* group */
4621 res
= hmac_sha256(wpabuf_head(hkey
), wpabuf_len(hkey
),
4622 wpabuf_head(secret
), wpabuf_len(secret
), prk
);
4623 else if (group
== 20)
4624 res
= hmac_sha384(wpabuf_head(hkey
), wpabuf_len(hkey
),
4625 wpabuf_head(secret
), wpabuf_len(secret
), prk
);
4626 else if (group
== 21)
4627 res
= hmac_sha512(wpabuf_head(hkey
), wpabuf_len(hkey
),
4628 wpabuf_head(secret
), wpabuf_len(secret
), prk
);
4629 wpabuf_clear_free(hkey
);
4630 wpabuf_clear_free(secret
);
4634 wpa_hexdump_key(MSG_DEBUG
, "OWE: prk", prk
, hash_len
);
4636 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
4639 res
= hmac_sha256_kdf(prk
, hash_len
, NULL
, (const u8
*) info
,
4640 os_strlen(info
), sm
->pmk
, hash_len
);
4641 else if (group
== 20)
4642 res
= hmac_sha384_kdf(prk
, hash_len
, NULL
, (const u8
*) info
,
4643 os_strlen(info
), sm
->pmk
, hash_len
);
4644 else if (group
== 21)
4645 res
= hmac_sha512_kdf(prk
, hash_len
, NULL
, (const u8
*) info
,
4646 os_strlen(info
), sm
->pmk
, hash_len
);
4647 os_memset(prk
, 0, SHA512_MAC_LEN
);
4652 sm
->pmk_len
= hash_len
;
4654 wpa_hexdump_key(MSG_DEBUG
, "OWE: PMK", sm
->pmk
, sm
->pmk_len
);
4655 wpa_hexdump(MSG_DEBUG
, "OWE: PMKID", pmkid
, PMKID_LEN
);
4656 pmksa_cache_add(sm
->pmksa
, sm
->pmk
, sm
->pmk_len
, pmkid
, NULL
, 0,
4657 bssid
, sm
->own_addr
, sm
->network_ctx
, sm
->key_mgmt
,
4663 #endif /* CONFIG_OWE */
4666 void wpa_sm_set_fils_cache_id(struct wpa_sm
*sm
, const u8
*fils_cache_id
)
4669 if (sm
&& fils_cache_id
) {
4670 sm
->fils_cache_id_set
= 1;
4671 os_memcpy(sm
->fils_cache_id
, fils_cache_id
, FILS_CACHE_ID_LEN
);
4673 #endif /* CONFIG_FILS */
4678 void wpa_sm_set_dpp_z(struct wpa_sm
*sm
, const struct wpabuf
*z
)
4681 wpabuf_clear_free(sm
->dpp_z
);
4682 sm
->dpp_z
= z
? wpabuf_dup(z
) : NULL
;
4685 #endif /* CONFIG_DPP2 */