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 (pairwise
&& sm
->wpa_deny_ptk0_rekey
&& !sm
->use_ext_key_id
&&
187 wpa_sm_get_state(sm
) == WPA_COMPLETED
) {
188 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
189 "WPA: PTK0 rekey not allowed, reconnecting");
190 wpa_sm_reconnect(sm
);
194 if (wpa_use_akm_defined(sm
->key_mgmt
))
195 ver
= WPA_KEY_INFO_TYPE_AKM_DEFINED
;
196 else if (wpa_key_mgmt_ft(sm
->key_mgmt
) ||
197 wpa_key_mgmt_sha256(sm
->key_mgmt
))
198 ver
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
199 else if (sm
->pairwise_cipher
!= WPA_CIPHER_TKIP
)
200 ver
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
202 ver
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
204 if (wpa_sm_get_bssid(sm
, bssid
) < 0) {
205 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
206 "Failed to read BSSID for EAPOL-Key request");
210 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
211 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
212 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
213 hdrlen
, &rlen
, (void *) &reply
);
217 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
218 sm
->proto
== WPA_PROTO_OSEN
) ?
219 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
220 key_info
= WPA_KEY_INFO_REQUEST
| ver
;
222 key_info
|= WPA_KEY_INFO_SECURE
;
223 if (sm
->ptk_set
&& mic_len
)
224 key_info
|= WPA_KEY_INFO_MIC
;
226 key_info
|= WPA_KEY_INFO_ERROR
;
228 key_info
|= WPA_KEY_INFO_KEY_TYPE
;
229 WPA_PUT_BE16(reply
->key_info
, key_info
);
230 WPA_PUT_BE16(reply
->key_length
, 0);
231 os_memcpy(reply
->replay_counter
, sm
->request_counter
,
232 WPA_REPLAY_COUNTER_LEN
);
233 inc_byte_array(sm
->request_counter
, WPA_REPLAY_COUNTER_LEN
);
235 mic
= (u8
*) (reply
+ 1);
236 WPA_PUT_BE16(mic
+ mic_len
, 0);
237 if (!(key_info
& WPA_KEY_INFO_MIC
))
242 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
243 "WPA: Sending EAPOL-Key Request (error=%d "
244 "pairwise=%d ptk_set=%d len=%lu)",
245 error
, pairwise
, sm
->ptk_set
, (unsigned long) rlen
);
246 wpa_eapol_key_send(sm
, &sm
->ptk
, ver
, bssid
, ETH_P_EAPOL
, rbuf
, rlen
,
251 static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm
*sm
)
253 #ifdef CONFIG_IEEE80211R
254 if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
) {
255 if (wpa_sm_key_mgmt_set_pmk(sm
, sm
->xxkey
, sm
->xxkey_len
))
256 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
257 "RSN: Cannot set low order 256 bits of MSK for key management offload");
259 #endif /* CONFIG_IEEE80211R */
260 if (wpa_sm_key_mgmt_set_pmk(sm
, sm
->pmk
, sm
->pmk_len
))
261 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
262 "RSN: Cannot set PMK for key management offload");
263 #ifdef CONFIG_IEEE80211R
265 #endif /* CONFIG_IEEE80211R */
269 static int wpa_supplicant_get_pmk(struct wpa_sm
*sm
,
270 const unsigned char *src_addr
,
273 int abort_cached
= 0;
275 if (pmkid
&& !sm
->cur_pmksa
) {
276 /* When using drivers that generate RSN IE, wpa_supplicant may
277 * not have enough time to get the association information
278 * event before receiving this 1/4 message, so try to find a
279 * matching PMKSA cache entry here. */
280 sm
->cur_pmksa
= pmksa_cache_get(sm
->pmksa
, src_addr
, pmkid
,
283 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
284 "RSN: found matching PMKID from PMKSA cache");
286 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
287 "RSN: no matching PMKID found");
292 if (pmkid
&& sm
->cur_pmksa
&&
293 os_memcmp_const(pmkid
, sm
->cur_pmksa
->pmkid
, PMKID_LEN
) == 0) {
294 wpa_hexdump(MSG_DEBUG
, "RSN: matched PMKID", pmkid
, PMKID_LEN
);
295 wpa_sm_set_pmk_from_pmksa(sm
);
296 wpa_hexdump_key(MSG_DEBUG
, "RSN: PMK from PMKSA cache",
297 sm
->pmk
, sm
->pmk_len
);
298 eapol_sm_notify_cached(sm
->eapol
);
299 #ifdef CONFIG_IEEE80211R
302 if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_SAE
&&
303 sm
->pmk_len
== PMK_LEN
) {
304 /* Need to allow FT key derivation to proceed with
305 * PMK from SAE being used as the XXKey in cases where
306 * the PMKID in msg 1/4 matches the PMKSA entry that was
307 * just added based on SAE authentication for the
308 * initial mobility domain association. */
309 os_memcpy(sm
->xxkey
, sm
->pmk
, sm
->pmk_len
);
310 sm
->xxkey_len
= sm
->pmk_len
;
312 #endif /* CONFIG_SAE */
313 #endif /* CONFIG_IEEE80211R */
314 } else if (wpa_key_mgmt_wpa_ieee8021x(sm
->key_mgmt
) && sm
->eapol
) {
316 #ifdef CONFIG_IEEE80211R
318 #endif /* CONFIG_IEEE80211R */
320 if (wpa_key_mgmt_sha384(sm
->key_mgmt
))
321 pmk_len
= PMK_LEN_SUITE_B_192
;
324 res
= eapol_sm_get_key(sm
->eapol
, sm
->pmk
, pmk_len
);
326 if (pmk_len
== PMK_LEN
) {
328 * EAP-LEAP is an exception from other EAP
329 * methods: it uses only 16-byte PMK.
331 res
= eapol_sm_get_key(sm
->eapol
, sm
->pmk
, 16);
335 #ifdef CONFIG_IEEE80211R
337 eapol_sm_get_key(sm
->eapol
, buf
, 2 * PMK_LEN
) == 0) {
338 if (wpa_key_mgmt_sha384(sm
->key_mgmt
)) {
339 os_memcpy(sm
->xxkey
, buf
, SHA384_MAC_LEN
);
340 sm
->xxkey_len
= SHA384_MAC_LEN
;
342 os_memcpy(sm
->xxkey
, buf
+ PMK_LEN
, PMK_LEN
);
343 sm
->xxkey_len
= PMK_LEN
;
345 forced_memzero(buf
, sizeof(buf
));
346 if (sm
->proto
== WPA_PROTO_RSN
&&
347 wpa_key_mgmt_ft(sm
->key_mgmt
)) {
348 struct rsn_pmksa_cache_entry
*sa
= NULL
;
349 const u8
*fils_cache_id
= NULL
;
352 if (sm
->fils_cache_id_set
)
353 fils_cache_id
= sm
->fils_cache_id
;
354 #endif /* CONFIG_FILS */
355 wpa_hexdump_key(MSG_DEBUG
,
356 "FT: Cache XXKey/MPMK",
357 sm
->xxkey
, sm
->xxkey_len
);
358 sa
= pmksa_cache_add(sm
->pmksa
,
359 sm
->xxkey
, sm
->xxkey_len
,
361 src_addr
, sm
->own_addr
,
369 #endif /* CONFIG_IEEE80211R */
371 struct rsn_pmksa_cache_entry
*sa
= NULL
;
372 const u8
*fils_cache_id
= NULL
;
375 if (sm
->fils_cache_id_set
)
376 fils_cache_id
= sm
->fils_cache_id
;
377 #endif /* CONFIG_FILS */
379 wpa_hexdump_key(MSG_DEBUG
, "WPA: PMK from EAPOL state "
380 "machines", sm
->pmk
, pmk_len
);
381 sm
->pmk_len
= pmk_len
;
382 wpa_supplicant_key_mgmt_set_pmk(sm
);
383 if (sm
->proto
== WPA_PROTO_RSN
&&
384 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
385 !wpa_key_mgmt_ft(sm
->key_mgmt
)) {
386 sa
= pmksa_cache_add(sm
->pmksa
,
387 sm
->pmk
, pmk_len
, NULL
,
389 src_addr
, sm
->own_addr
,
394 if (!sm
->cur_pmksa
&& pmkid
&&
395 pmksa_cache_get(sm
->pmksa
, src_addr
, pmkid
, NULL
,
397 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
398 "RSN: the new PMK matches with the "
401 } else if (sa
&& !sm
->cur_pmksa
&& pmkid
) {
403 * It looks like the authentication server
404 * derived mismatching MSK. This should not
405 * really happen, but bugs happen.. There is not
406 * much we can do here without knowing what
407 * exactly caused the server to misbehave.
409 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
410 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
416 #ifdef CONFIG_IEEE80211R
417 } else if (wpa_key_mgmt_ft(sm
->key_mgmt
) && sm
->ft_protocol
) {
418 wpa_printf(MSG_DEBUG
,
419 "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
420 #endif /* CONFIG_IEEE80211R */
422 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
423 "WPA: Failed to get master session key from "
424 "EAPOL state machines - key handshake "
427 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
428 "RSN: Cancelled PMKSA caching "
430 sm
->cur_pmksa
= NULL
;
432 } else if (!abort_cached
) {
438 if (abort_cached
&& wpa_key_mgmt_wpa_ieee8021x(sm
->key_mgmt
) &&
439 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
440 !wpa_key_mgmt_ft(sm
->key_mgmt
) && sm
->key_mgmt
!= WPA_KEY_MGMT_OSEN
)
442 /* Send EAPOL-Start to trigger full EAP authentication. */
446 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
447 "RSN: no PMKSA entry found - trigger "
448 "full EAP authentication");
449 buf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_START
,
450 NULL
, 0, &buflen
, NULL
);
452 wpa_sm_ether_send(sm
, sm
->bssid
, ETH_P_EAPOL
,
466 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
467 * @sm: Pointer to WPA state machine data from wpa_sm_init()
468 * @dst: Destination address for the frame
469 * @key: Pointer to the EAPOL-Key frame header
470 * @ver: Version bits from EAPOL-Key Key Info
471 * @nonce: Nonce value for the EAPOL-Key frame
472 * @wpa_ie: WPA/RSN IE
473 * @wpa_ie_len: Length of the WPA/RSN IE
474 * @ptk: PTK to use for keyed hash and encryption
475 * Returns: >= 0 on success, < 0 on failure
477 int wpa_supplicant_send_2_of_4(struct wpa_sm
*sm
, const unsigned char *dst
,
478 const struct wpa_eapol_key
*key
,
479 int ver
, const u8
*nonce
,
480 const u8
*wpa_ie
, size_t wpa_ie_len
,
483 size_t mic_len
, hdrlen
, rlen
;
484 struct wpa_eapol_key
*reply
;
486 u8
*rsn_ie_buf
= NULL
;
489 if (wpa_ie
== NULL
) {
490 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "WPA: No wpa_ie set - "
491 "cannot generate msg 2/4");
495 #ifdef CONFIG_IEEE80211R
496 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
499 wpa_hexdump(MSG_DEBUG
, "WPA: WPA IE before FT processing",
502 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
503 * FTIE from (Re)Association Response.
505 rsn_ie_buf
= os_malloc(wpa_ie_len
+ 2 + 2 + PMKID_LEN
+
506 sm
->assoc_resp_ies_len
);
507 if (rsn_ie_buf
== NULL
)
509 os_memcpy(rsn_ie_buf
, wpa_ie
, wpa_ie_len
);
510 res
= wpa_insert_pmkid(rsn_ie_buf
, &wpa_ie_len
,
516 wpa_hexdump(MSG_DEBUG
,
517 "WPA: WPA IE after PMKID[PMKR1Name] addition into RSNE",
518 rsn_ie_buf
, wpa_ie_len
);
520 if (sm
->assoc_resp_ies
) {
521 wpa_hexdump(MSG_DEBUG
, "WPA: Add assoc_resp_ies",
523 sm
->assoc_resp_ies_len
);
524 os_memcpy(rsn_ie_buf
+ wpa_ie_len
, sm
->assoc_resp_ies
,
525 sm
->assoc_resp_ies_len
);
526 wpa_ie_len
+= sm
->assoc_resp_ies_len
;
531 #endif /* CONFIG_IEEE80211R */
533 wpa_hexdump(MSG_DEBUG
, "WPA: WPA IE for msg 2/4", wpa_ie
, wpa_ie_len
);
535 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
536 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
537 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
,
538 NULL
, hdrlen
+ wpa_ie_len
,
539 &rlen
, (void *) &reply
);
545 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
546 sm
->proto
== WPA_PROTO_OSEN
) ?
547 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
548 key_info
= ver
| WPA_KEY_INFO_KEY_TYPE
;
550 key_info
|= WPA_KEY_INFO_MIC
;
552 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
553 WPA_PUT_BE16(reply
->key_info
, key_info
);
554 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
)
555 WPA_PUT_BE16(reply
->key_length
, 0);
557 os_memcpy(reply
->key_length
, key
->key_length
, 2);
558 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
559 WPA_REPLAY_COUNTER_LEN
);
560 wpa_hexdump(MSG_DEBUG
, "WPA: Replay Counter", reply
->replay_counter
,
561 WPA_REPLAY_COUNTER_LEN
);
563 key_mic
= (u8
*) (reply
+ 1);
564 WPA_PUT_BE16(key_mic
+ mic_len
, wpa_ie_len
); /* Key Data Length */
565 os_memcpy(key_mic
+ mic_len
+ 2, wpa_ie
, wpa_ie_len
); /* Key Data */
568 os_memcpy(reply
->key_nonce
, nonce
, WPA_NONCE_LEN
);
570 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Sending EAPOL-Key 2/4");
571 return wpa_eapol_key_send(sm
, ptk
, ver
, dst
, ETH_P_EAPOL
, rbuf
, rlen
,
576 static int wpa_derive_ptk(struct wpa_sm
*sm
, const unsigned char *src_addr
,
577 const struct wpa_eapol_key
*key
, struct wpa_ptk
*ptk
)
583 #ifdef CONFIG_IEEE80211R
584 if (wpa_key_mgmt_ft(sm
->key_mgmt
))
585 return wpa_derive_ptk_ft(sm
, src_addr
, key
, ptk
);
586 #endif /* CONFIG_IEEE80211R */
589 if (sm
->key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->dpp_z
) {
590 z
= wpabuf_head(sm
->dpp_z
);
591 z_len
= wpabuf_len(sm
->dpp_z
);
593 #endif /* CONFIG_DPP2 */
597 if (sm
->owe_ptk_workaround
&& akmp
== WPA_KEY_MGMT_OWE
&&
599 wpa_printf(MSG_DEBUG
,
600 "OWE: Force SHA256 for PTK derivation");
601 akmp
|= WPA_KEY_MGMT_PSK_SHA256
;
603 #endif /* CONFIG_OWE */
604 return wpa_pmk_to_ptk(sm
->pmk
, sm
->pmk_len
, "Pairwise key expansion",
605 sm
->own_addr
, sm
->bssid
, sm
->snonce
,
606 key
->key_nonce
, ptk
, akmp
,
607 sm
->pairwise_cipher
, z
, z_len
);
611 static int wpa_handle_ext_key_id(struct wpa_sm
*sm
,
612 struct wpa_eapol_ie_parse
*kde
)
614 if (sm
->ext_key_id
) {
618 wpa_msg(sm
->ctx
->msg_ctx
,
619 sm
->use_ext_key_id
? MSG_INFO
: MSG_DEBUG
,
620 "RSN: No Key ID in Extended Key ID handshake");
621 sm
->keyidx_active
= 0;
622 return sm
->use_ext_key_id
? -1 : 0;
625 key_id
= kde
->key_id
[0] & 0x03;
627 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
628 "RSN: Invalid Extended Key ID: %d", key_id
);
631 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
632 "RSN: Using Extended Key ID %d", key_id
);
633 sm
->keyidx_active
= key_id
;
634 sm
->use_ext_key_id
= 1;
636 if (kde
->key_id
&& (kde
->key_id
[0] & 0x03)) {
637 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
638 "RSN: Non-zero Extended Key ID Key ID in PTK0 handshake");
643 /* This is not supposed to be included here, but ignore
644 * the case of matching Key ID 0 just in case. */
645 wpa_msg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
646 "RSN: Extended Key ID Key ID 0 in PTK0 handshake");
648 sm
->keyidx_active
= 0;
649 sm
->use_ext_key_id
= 0;
656 static void wpa_supplicant_process_1_of_4(struct wpa_sm
*sm
,
657 const unsigned char *src_addr
,
658 const struct wpa_eapol_key
*key
,
659 u16 ver
, const u8
*key_data
,
662 struct wpa_eapol_ie_parse ie
;
665 u8
*kde
, *kde_buf
= NULL
;
668 if (wpa_sm_get_network_ctx(sm
) == NULL
) {
669 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "WPA: No SSID info "
670 "found (msg 1 of 4)");
674 if (sm
->wpa_deny_ptk0_rekey
&& !sm
->use_ext_key_id
&&
675 wpa_sm_get_state(sm
) == WPA_COMPLETED
) {
676 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
677 "WPA: PTK0 rekey not allowed, reconnecting");
678 wpa_sm_reconnect(sm
);
682 wpa_sm_set_state(sm
, WPA_4WAY_HANDSHAKE
);
683 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: RX message 1 of 4-Way "
684 "Handshake from " MACSTR
" (ver=%d)", MAC2STR(src_addr
), ver
);
686 os_memset(&ie
, 0, sizeof(ie
));
688 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) {
689 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
690 wpa_hexdump(MSG_DEBUG
, "RSN: msg 1/4 key data",
691 key_data
, key_data_len
);
692 if (wpa_supplicant_parse_ies(key_data
, key_data_len
, &ie
) < 0)
695 wpa_hexdump(MSG_DEBUG
, "RSN: PMKID from "
696 "Authenticator", ie
.pmkid
, PMKID_LEN
);
700 res
= wpa_supplicant_get_pmk(sm
, src_addr
, ie
.pmkid
);
702 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: Do not reply to "
703 "msg 1/4 - requesting full EAP authentication");
709 if (sm
->renew_snonce
) {
710 if (random_get_bytes(sm
->snonce
, WPA_NONCE_LEN
)) {
711 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
712 "WPA: Failed to get random data for SNonce");
715 sm
->renew_snonce
= 0;
716 wpa_hexdump(MSG_DEBUG
, "WPA: Renewed SNonce",
717 sm
->snonce
, WPA_NONCE_LEN
);
720 /* Calculate PTK which will be stored as a temporary PTK until it has
721 * been verified when processing message 3/4. */
723 if (wpa_derive_ptk(sm
, src_addr
, key
, ptk
) < 0)
725 if (sm
->pairwise_cipher
== WPA_CIPHER_TKIP
) {
727 /* Supplicant: swap tx/rx Mic keys */
728 os_memcpy(buf
, &ptk
->tk
[16], 8);
729 os_memcpy(&ptk
->tk
[16], &ptk
->tk
[24], 8);
730 os_memcpy(&ptk
->tk
[24], buf
, 8);
731 forced_memzero(buf
, sizeof(buf
));
735 kde
= sm
->assoc_wpa_ie
;
736 kde_len
= sm
->assoc_wpa_ie_len
;
737 kde_buf
= os_malloc(kde_len
+
738 2 + RSN_SELECTOR_LEN
+ 3 +
739 sm
->assoc_rsnxe_len
+
740 2 + RSN_SELECTOR_LEN
+ 1);
743 os_memcpy(kde_buf
, kde
, kde_len
);
747 if (wpa_sm_ocv_enabled(sm
)) {
748 struct wpa_channel_info ci
;
752 if (wpa_sm_channel_info(sm
, &ci
) != 0) {
753 wpa_printf(MSG_WARNING
,
754 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
758 if (ocv_insert_oci_kde(&ci
, &pos
) < 0)
762 #endif /* CONFIG_OCV */
764 if (sm
->assoc_rsnxe
&& sm
->assoc_rsnxe_len
) {
765 os_memcpy(kde
+ kde_len
, sm
->assoc_rsnxe
, sm
->assoc_rsnxe_len
);
766 kde_len
+= sm
->assoc_rsnxe_len
;
773 wpa_printf(MSG_DEBUG
,
774 "P2P: Add IP Address Request KDE into EAPOL-Key 2/4");
776 *pos
++ = WLAN_EID_VENDOR_SPECIFIC
;
777 *pos
++ = RSN_SELECTOR_LEN
+ 1;
778 RSN_SELECTOR_PUT(pos
, WFA_KEY_DATA_IP_ADDR_REQ
);
779 pos
+= RSN_SELECTOR_LEN
;
783 #endif /* CONFIG_P2P */
785 if (wpa_supplicant_send_2_of_4(sm
, sm
->bssid
, key
, ver
, sm
->snonce
,
786 kde
, kde_len
, ptk
) < 0)
790 os_memcpy(sm
->anonce
, key
->key_nonce
, WPA_NONCE_LEN
);
795 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
799 static void wpa_sm_start_preauth(void *eloop_ctx
, void *timeout_ctx
)
801 struct wpa_sm
*sm
= eloop_ctx
;
802 rsn_preauth_candidate_process(sm
);
806 static void wpa_supplicant_key_neg_complete(struct wpa_sm
*sm
,
807 const u8
*addr
, int secure
)
809 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
810 "WPA: Key negotiation completed with "
811 MACSTR
" [PTK=%s GTK=%s]", MAC2STR(addr
),
812 wpa_cipher_txt(sm
->pairwise_cipher
),
813 wpa_cipher_txt(sm
->group_cipher
));
814 wpa_sm_cancel_auth_timeout(sm
);
815 wpa_sm_set_state(sm
, WPA_COMPLETED
);
818 wpa_sm_mlme_setprotection(
819 sm
, addr
, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX
,
820 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE
);
821 eapol_sm_notify_portValid(sm
->eapol
, TRUE
);
822 if (wpa_key_mgmt_wpa_psk(sm
->key_mgmt
) ||
823 sm
->key_mgmt
== WPA_KEY_MGMT_DPP
||
824 sm
->key_mgmt
== WPA_KEY_MGMT_OWE
)
825 eapol_sm_notify_eap_success(sm
->eapol
, TRUE
);
827 * Start preauthentication after a short wait to avoid a
828 * possible race condition between the data receive and key
829 * configuration after the 4-Way Handshake. This increases the
830 * likelihood of the first preauth EAPOL-Start frame getting to
833 if (!dl_list_empty(&sm
->pmksa_candidates
))
834 eloop_register_timeout(1, 0, wpa_sm_start_preauth
,
838 if (sm
->cur_pmksa
&& sm
->cur_pmksa
->opportunistic
) {
839 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
840 "RSN: Authenticator accepted "
841 "opportunistic PMKSA entry - marking it valid");
842 sm
->cur_pmksa
->opportunistic
= 0;
845 #ifdef CONFIG_IEEE80211R
846 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
847 /* Prepare for the next transition */
848 wpa_ft_prepare_auth_request(sm
, NULL
);
850 #endif /* CONFIG_IEEE80211R */
854 static void wpa_sm_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
856 struct wpa_sm
*sm
= eloop_ctx
;
857 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Request PTK rekeying");
858 wpa_sm_key_request(sm
, 0, 1);
862 static int wpa_supplicant_install_ptk(struct wpa_sm
*sm
,
863 const struct wpa_eapol_key
*key
,
864 enum key_flag key_flag
)
870 if (sm
->ptk
.installed
) {
871 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
872 "WPA: Do not re-install same PTK to the driver");
876 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
877 "WPA: Installing PTK to the driver");
879 if (sm
->pairwise_cipher
== WPA_CIPHER_NONE
) {
880 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Pairwise Cipher "
881 "Suite: NONE - do not use pairwise keys");
885 if (!wpa_cipher_valid_pairwise(sm
->pairwise_cipher
)) {
886 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
887 "WPA: Unsupported pairwise cipher %d",
888 sm
->pairwise_cipher
);
892 alg
= wpa_cipher_to_alg(sm
->pairwise_cipher
);
893 keylen
= wpa_cipher_key_len(sm
->pairwise_cipher
);
894 if (keylen
<= 0 || (unsigned int) keylen
!= sm
->ptk
.tk_len
) {
895 wpa_printf(MSG_DEBUG
, "WPA: TK length mismatch: %d != %lu",
896 keylen
, (long unsigned int) sm
->ptk
.tk_len
);
899 rsclen
= wpa_cipher_rsc_len(sm
->pairwise_cipher
);
901 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) {
904 key_rsc
= key
->key_rsc
;
905 wpa_hexdump(MSG_DEBUG
, "WPA: RSC", key_rsc
, rsclen
);
908 if (wpa_sm_set_key(sm
, alg
, sm
->bssid
, sm
->keyidx_active
, 1, key_rsc
,
909 rsclen
, sm
->ptk
.tk
, keylen
,
910 KEY_FLAG_PAIRWISE
| key_flag
) < 0) {
911 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
912 "WPA: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
913 MACSTR
" idx=%d key_flag=0x%x)",
914 alg
, keylen
, MAC2STR(sm
->bssid
),
915 sm
->keyidx_active
, key_flag
);
919 /* TK is not needed anymore in supplicant */
920 os_memset(sm
->ptk
.tk
, 0, WPA_TK_MAX_LEN
);
922 sm
->ptk
.installed
= 1;
924 if (sm
->wpa_ptk_rekey
) {
925 eloop_cancel_timeout(wpa_sm_rekey_ptk
, sm
, NULL
);
926 eloop_register_timeout(sm
->wpa_ptk_rekey
, 0, wpa_sm_rekey_ptk
,
933 static int wpa_supplicant_activate_ptk(struct wpa_sm
*sm
)
935 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
936 "WPA: Activate PTK (idx=%d bssid=" MACSTR
")",
937 sm
->keyidx_active
, MAC2STR(sm
->bssid
));
939 if (wpa_sm_set_key(sm
, 0, sm
->bssid
, sm
->keyidx_active
, 0, NULL
, 0,
940 NULL
, 0, KEY_FLAG_PAIRWISE_RX_TX_MODIFY
) < 0) {
941 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
942 "WPA: Failed to activate PTK for TX (idx=%d bssid="
943 MACSTR
")", sm
->keyidx_active
, MAC2STR(sm
->bssid
));
950 static int wpa_supplicant_check_group_cipher(struct wpa_sm
*sm
,
952 int keylen
, int maxkeylen
,
958 *alg
= wpa_cipher_to_alg(group_cipher
);
959 if (*alg
== WPA_ALG_NONE
) {
960 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
961 "WPA: Unsupported Group Cipher %d",
965 *key_rsc_len
= wpa_cipher_rsc_len(group_cipher
);
967 klen
= wpa_cipher_key_len(group_cipher
);
968 if (keylen
!= klen
|| maxkeylen
< klen
) {
969 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
970 "WPA: Unsupported %s Group Cipher key length %d (%d)",
971 wpa_cipher_txt(group_cipher
), keylen
, maxkeylen
);
978 struct wpa_gtk_data
{
980 int tx
, key_rsc_len
, keyidx
;
986 static int wpa_supplicant_install_gtk(struct wpa_sm
*sm
,
987 const struct wpa_gtk_data
*gd
,
988 const u8
*key_rsc
, int wnm_sleep
)
990 const u8
*_gtk
= gd
->gtk
;
993 /* Detect possible key reinstallation */
994 if ((sm
->gtk
.gtk_len
== (size_t) gd
->gtk_len
&&
995 os_memcmp(sm
->gtk
.gtk
, gd
->gtk
, sm
->gtk
.gtk_len
) == 0) ||
996 (sm
->gtk_wnm_sleep
.gtk_len
== (size_t) gd
->gtk_len
&&
997 os_memcmp(sm
->gtk_wnm_sleep
.gtk
, gd
->gtk
,
998 sm
->gtk_wnm_sleep
.gtk_len
) == 0)) {
999 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1000 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
1001 gd
->keyidx
, gd
->tx
, gd
->gtk_len
);
1005 wpa_hexdump_key(MSG_DEBUG
, "WPA: Group Key", gd
->gtk
, gd
->gtk_len
);
1006 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1007 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
1008 gd
->keyidx
, gd
->tx
, gd
->gtk_len
);
1009 wpa_hexdump(MSG_DEBUG
, "WPA: RSC", key_rsc
, gd
->key_rsc_len
);
1010 if (sm
->group_cipher
== WPA_CIPHER_TKIP
) {
1011 /* Swap Tx/Rx keys for Michael MIC */
1012 os_memcpy(gtk_buf
, gd
->gtk
, 16);
1013 os_memcpy(gtk_buf
+ 16, gd
->gtk
+ 24, 8);
1014 os_memcpy(gtk_buf
+ 24, gd
->gtk
+ 16, 8);
1017 if (sm
->pairwise_cipher
== WPA_CIPHER_NONE
) {
1018 if (wpa_sm_set_key(sm
, gd
->alg
, NULL
,
1019 gd
->keyidx
, 1, key_rsc
, gd
->key_rsc_len
,
1021 KEY_FLAG_GROUP_RX_TX_DEFAULT
) < 0) {
1022 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1023 "WPA: Failed to set GTK to the driver "
1025 forced_memzero(gtk_buf
, sizeof(gtk_buf
));
1028 } else if (wpa_sm_set_key(sm
, gd
->alg
, broadcast_ether_addr
,
1029 gd
->keyidx
, gd
->tx
, key_rsc
, gd
->key_rsc_len
,
1030 _gtk
, gd
->gtk_len
, KEY_FLAG_GROUP_RX
) < 0) {
1031 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1032 "WPA: Failed to set GTK to "
1033 "the driver (alg=%d keylen=%d keyidx=%d)",
1034 gd
->alg
, gd
->gtk_len
, gd
->keyidx
);
1035 forced_memzero(gtk_buf
, sizeof(gtk_buf
));
1038 forced_memzero(gtk_buf
, sizeof(gtk_buf
));
1041 sm
->gtk_wnm_sleep
.gtk_len
= gd
->gtk_len
;
1042 os_memcpy(sm
->gtk_wnm_sleep
.gtk
, gd
->gtk
,
1043 sm
->gtk_wnm_sleep
.gtk_len
);
1045 sm
->gtk
.gtk_len
= gd
->gtk_len
;
1046 os_memcpy(sm
->gtk
.gtk
, gd
->gtk
, sm
->gtk
.gtk_len
);
1053 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm
*sm
,
1056 if (tx
&& sm
->pairwise_cipher
!= WPA_CIPHER_NONE
) {
1057 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
1058 * seemed to set this bit (incorrectly, since Tx is only when
1059 * doing Group Key only APs) and without this workaround, the
1060 * data connection does not work because wpa_supplicant
1061 * configured non-zero keyidx to be used for unicast. */
1062 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1063 "WPA: Tx bit set for GTK, but pairwise "
1064 "keys are used - ignore Tx bit");
1071 static int wpa_supplicant_rsc_relaxation(const struct wpa_sm
*sm
,
1076 if (!sm
->wpa_rsc_relaxation
)
1079 rsclen
= wpa_cipher_rsc_len(sm
->group_cipher
);
1082 * Try to detect RSC (endian) corruption issue where the AP sends
1083 * the RSC bytes in EAPOL-Key message in the wrong order, both if
1084 * it's actually a 6-byte field (as it should be) and if it treats
1085 * it as an 8-byte field.
1086 * An AP model known to have this bug is the Sapido RB-1632.
1088 if (rsclen
== 6 && ((rsc
[5] && !rsc
[0]) || rsc
[6] || rsc
[7])) {
1089 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1090 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
1091 rsc
[0], rsc
[1], rsc
[2], rsc
[3],
1092 rsc
[4], rsc
[5], rsc
[6], rsc
[7]);
1101 static int wpa_supplicant_pairwise_gtk(struct wpa_sm
*sm
,
1102 const struct wpa_eapol_key
*key
,
1103 const u8
*gtk
, size_t gtk_len
,
1106 struct wpa_gtk_data gd
;
1110 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1112 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1113 * Reserved [bits 0-7]
1117 os_memset(&gd
, 0, sizeof(gd
));
1118 wpa_hexdump_key(MSG_DEBUG
, "RSN: received GTK in pairwise handshake",
1121 if (gtk_len
< 2 || gtk_len
- 2 > sizeof(gd
.gtk
))
1124 gd
.keyidx
= gtk
[0] & 0x3;
1125 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
1126 !!(gtk
[0] & BIT(2)));
1130 os_memcpy(gd
.gtk
, gtk
, gtk_len
);
1131 gd
.gtk_len
= gtk_len
;
1133 key_rsc
= key
->key_rsc
;
1134 if (wpa_supplicant_rsc_relaxation(sm
, key
->key_rsc
))
1137 if (sm
->group_cipher
!= WPA_CIPHER_GTK_NOT_USED
&&
1138 (wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
1140 &gd
.key_rsc_len
, &gd
.alg
) ||
1141 wpa_supplicant_install_gtk(sm
, &gd
, key_rsc
, 0))) {
1142 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1143 "RSN: Failed to install GTK");
1144 forced_memzero(&gd
, sizeof(gd
));
1147 forced_memzero(&gd
, sizeof(gd
));
1153 static int wpa_supplicant_install_igtk(struct wpa_sm
*sm
,
1154 const struct wpa_igtk_kde
*igtk
,
1157 size_t len
= wpa_cipher_key_len(sm
->mgmt_group_cipher
);
1158 u16 keyidx
= WPA_GET_LE16(igtk
->keyid
);
1160 /* Detect possible key reinstallation */
1161 if ((sm
->igtk
.igtk_len
== len
&&
1162 os_memcmp(sm
->igtk
.igtk
, igtk
->igtk
, sm
->igtk
.igtk_len
) == 0) ||
1163 (sm
->igtk_wnm_sleep
.igtk_len
== len
&&
1164 os_memcmp(sm
->igtk_wnm_sleep
.igtk
, igtk
->igtk
,
1165 sm
->igtk_wnm_sleep
.igtk_len
) == 0)) {
1166 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1167 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1172 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1173 "WPA: IGTK keyid %d pn " COMPACT_MACSTR
,
1174 keyidx
, MAC2STR(igtk
->pn
));
1175 wpa_hexdump_key(MSG_DEBUG
, "WPA: IGTK", igtk
->igtk
, len
);
1176 if (keyidx
> 4095) {
1177 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1178 "WPA: Invalid IGTK KeyID %d", keyidx
);
1181 if (wpa_sm_set_key(sm
, wpa_cipher_to_alg(sm
->mgmt_group_cipher
),
1182 broadcast_ether_addr
,
1183 keyidx
, 0, igtk
->pn
, sizeof(igtk
->pn
),
1184 igtk
->igtk
, len
, KEY_FLAG_GROUP_RX
) < 0) {
1185 if (keyidx
== 0x0400 || keyidx
== 0x0500) {
1186 /* Assume the AP has broken PMF implementation since it
1187 * seems to have swapped the KeyID bytes. The AP cannot
1188 * be trusted to implement BIP correctly or provide a
1189 * valid IGTK, so do not try to configure this key with
1190 * swapped KeyID bytes. Instead, continue without
1191 * configuring the IGTK so that the driver can drop any
1192 * received group-addressed robust management frames due
1195 * Normally, this error behavior would result in us
1196 * disconnecting, but there are number of deployed APs
1197 * with this broken behavior, so as an interoperability
1198 * workaround, allow the connection to proceed. */
1199 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1200 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1202 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1203 "WPA: Failed to configure IGTK to the driver");
1209 sm
->igtk_wnm_sleep
.igtk_len
= len
;
1210 os_memcpy(sm
->igtk_wnm_sleep
.igtk
, igtk
->igtk
,
1211 sm
->igtk_wnm_sleep
.igtk_len
);
1213 sm
->igtk
.igtk_len
= len
;
1214 os_memcpy(sm
->igtk
.igtk
, igtk
->igtk
, sm
->igtk
.igtk_len
);
1221 static int wpa_supplicant_install_bigtk(struct wpa_sm
*sm
,
1222 const struct wpa_bigtk_kde
*bigtk
,
1225 size_t len
= wpa_cipher_key_len(sm
->mgmt_group_cipher
);
1226 u16 keyidx
= WPA_GET_LE16(bigtk
->keyid
);
1228 /* Detect possible key reinstallation */
1229 if ((sm
->bigtk
.bigtk_len
== len
&&
1230 os_memcmp(sm
->bigtk
.bigtk
, bigtk
->bigtk
,
1231 sm
->bigtk
.bigtk_len
) == 0) ||
1232 (sm
->bigtk_wnm_sleep
.bigtk_len
== len
&&
1233 os_memcmp(sm
->bigtk_wnm_sleep
.bigtk
, bigtk
->bigtk
,
1234 sm
->bigtk_wnm_sleep
.bigtk_len
) == 0)) {
1235 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1236 "WPA: Not reinstalling already in-use BIGTK to the driver (keyidx=%d)",
1241 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1242 "WPA: BIGTK keyid %d pn " COMPACT_MACSTR
,
1243 keyidx
, MAC2STR(bigtk
->pn
));
1244 wpa_hexdump_key(MSG_DEBUG
, "WPA: BIGTK", bigtk
->bigtk
, len
);
1245 if (keyidx
< 6 || keyidx
> 7) {
1246 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1247 "WPA: Invalid BIGTK KeyID %d", keyidx
);
1250 if (wpa_sm_set_key(sm
, wpa_cipher_to_alg(sm
->mgmt_group_cipher
),
1251 broadcast_ether_addr
,
1252 keyidx
, 0, bigtk
->pn
, sizeof(bigtk
->pn
),
1253 bigtk
->bigtk
, len
, KEY_FLAG_GROUP_RX
) < 0) {
1254 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1255 "WPA: Failed to configure BIGTK to the driver");
1260 sm
->bigtk_wnm_sleep
.bigtk_len
= len
;
1261 os_memcpy(sm
->bigtk_wnm_sleep
.bigtk
, bigtk
->bigtk
,
1262 sm
->bigtk_wnm_sleep
.bigtk_len
);
1264 sm
->bigtk
.bigtk_len
= len
;
1265 os_memcpy(sm
->bigtk
.bigtk
, bigtk
->bigtk
, sm
->bigtk
.bigtk_len
);
1272 static int ieee80211w_set_keys(struct wpa_sm
*sm
,
1273 struct wpa_eapol_ie_parse
*ie
)
1277 if (!wpa_cipher_valid_mgmt_group(sm
->mgmt_group_cipher
))
1281 const struct wpa_igtk_kde
*igtk
;
1283 len
= wpa_cipher_key_len(sm
->mgmt_group_cipher
);
1284 if (ie
->igtk_len
!= WPA_IGTK_KDE_PREFIX_LEN
+ len
)
1287 igtk
= (const struct wpa_igtk_kde
*) ie
->igtk
;
1288 if (wpa_supplicant_install_igtk(sm
, igtk
, 0) < 0)
1292 if (ie
->bigtk
&& sm
->beacon_prot
) {
1293 const struct wpa_bigtk_kde
*bigtk
;
1295 len
= wpa_cipher_key_len(sm
->mgmt_group_cipher
);
1296 if (ie
->bigtk_len
!= WPA_BIGTK_KDE_PREFIX_LEN
+ len
)
1299 bigtk
= (const struct wpa_bigtk_kde
*) ie
->bigtk
;
1300 if (wpa_supplicant_install_bigtk(sm
, bigtk
, 0) < 0)
1308 static void wpa_report_ie_mismatch(struct wpa_sm
*sm
,
1309 const char *reason
, const u8
*src_addr
,
1310 const u8
*wpa_ie
, size_t wpa_ie_len
,
1311 const u8
*rsn_ie
, size_t rsn_ie_len
)
1313 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "WPA: %s (src=" MACSTR
")",
1314 reason
, MAC2STR(src_addr
));
1316 if (sm
->ap_wpa_ie
) {
1317 wpa_hexdump(MSG_INFO
, "WPA: WPA IE in Beacon/ProbeResp",
1318 sm
->ap_wpa_ie
, sm
->ap_wpa_ie_len
);
1321 if (!sm
->ap_wpa_ie
) {
1322 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1323 "WPA: No WPA IE in Beacon/ProbeResp");
1325 wpa_hexdump(MSG_INFO
, "WPA: WPA IE in 3/4 msg",
1326 wpa_ie
, wpa_ie_len
);
1329 if (sm
->ap_rsn_ie
) {
1330 wpa_hexdump(MSG_INFO
, "WPA: RSN IE in Beacon/ProbeResp",
1331 sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
);
1334 if (!sm
->ap_rsn_ie
) {
1335 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1336 "WPA: No RSN IE in Beacon/ProbeResp");
1338 wpa_hexdump(MSG_INFO
, "WPA: RSN IE in 3/4 msg",
1339 rsn_ie
, rsn_ie_len
);
1342 wpa_sm_deauthenticate(sm
, WLAN_REASON_IE_IN_4WAY_DIFFERS
);
1346 #ifdef CONFIG_IEEE80211R
1348 static int ft_validate_mdie(struct wpa_sm
*sm
,
1349 const unsigned char *src_addr
,
1350 struct wpa_eapol_ie_parse
*ie
,
1351 const u8
*assoc_resp_mdie
)
1353 struct rsn_mdie
*mdie
;
1355 mdie
= (struct rsn_mdie
*) (ie
->mdie
+ 2);
1356 if (ie
->mdie
== NULL
|| ie
->mdie_len
< 2 + sizeof(*mdie
) ||
1357 os_memcmp(mdie
->mobility_domain
, sm
->mobility_domain
,
1358 MOBILITY_DOMAIN_ID_LEN
) != 0) {
1359 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: MDIE in msg 3/4 did "
1360 "not match with the current mobility domain");
1364 if (assoc_resp_mdie
&&
1365 (assoc_resp_mdie
[1] != ie
->mdie
[1] ||
1366 os_memcmp(assoc_resp_mdie
, ie
->mdie
, 2 + ie
->mdie
[1]) != 0)) {
1367 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: MDIE mismatch");
1368 wpa_hexdump(MSG_DEBUG
, "FT: MDIE in EAPOL-Key msg 3/4",
1369 ie
->mdie
, 2 + ie
->mdie
[1]);
1370 wpa_hexdump(MSG_DEBUG
, "FT: MDIE in (Re)Association Response",
1371 assoc_resp_mdie
, 2 + assoc_resp_mdie
[1]);
1379 static int ft_validate_ftie(struct wpa_sm
*sm
,
1380 const unsigned char *src_addr
,
1381 struct wpa_eapol_ie_parse
*ie
,
1382 const u8
*assoc_resp_ftie
)
1384 if (ie
->ftie
== NULL
) {
1385 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1386 "FT: No FTIE in EAPOL-Key msg 3/4");
1390 if (assoc_resp_ftie
== NULL
)
1393 if (assoc_resp_ftie
[1] != ie
->ftie
[1] ||
1394 os_memcmp(assoc_resp_ftie
, ie
->ftie
, 2 + ie
->ftie
[1]) != 0) {
1395 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: FTIE mismatch");
1396 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 3/4",
1397 ie
->ftie
, 2 + ie
->ftie
[1]);
1398 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)Association Response",
1399 assoc_resp_ftie
, 2 + assoc_resp_ftie
[1]);
1407 static int ft_validate_rsnie(struct wpa_sm
*sm
,
1408 const unsigned char *src_addr
,
1409 struct wpa_eapol_ie_parse
*ie
)
1411 struct wpa_ie_data rsn
;
1417 * Verify that PMKR1Name from EAPOL-Key message 3/4
1418 * matches with the value we derived.
1420 if (wpa_parse_wpa_ie_rsn(ie
->rsn_ie
, ie
->rsn_ie_len
, &rsn
) < 0 ||
1421 rsn
.num_pmkid
!= 1 || rsn
.pmkid
== NULL
) {
1422 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: No PMKR1Name in "
1423 "FT 4-way handshake message 3/4");
1427 if (os_memcmp_const(rsn
.pmkid
, sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
) != 0)
1429 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1430 "FT: PMKR1Name mismatch in "
1431 "FT 4-way handshake message 3/4");
1432 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Authenticator",
1433 rsn
.pmkid
, WPA_PMK_NAME_LEN
);
1434 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
1435 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
1443 static int wpa_supplicant_validate_ie_ft(struct wpa_sm
*sm
,
1444 const unsigned char *src_addr
,
1445 struct wpa_eapol_ie_parse
*ie
)
1447 const u8
*pos
, *end
, *mdie
= NULL
, *ftie
= NULL
;
1449 if (sm
->assoc_resp_ies
) {
1450 pos
= sm
->assoc_resp_ies
;
1451 end
= pos
+ sm
->assoc_resp_ies_len
;
1452 while (end
- pos
> 2) {
1453 if (2 + pos
[1] > end
- pos
)
1456 case WLAN_EID_MOBILITY_DOMAIN
:
1459 case WLAN_EID_FAST_BSS_TRANSITION
:
1467 if (ft_validate_mdie(sm
, src_addr
, ie
, mdie
) < 0 ||
1468 ft_validate_ftie(sm
, src_addr
, ie
, ftie
) < 0 ||
1469 ft_validate_rsnie(sm
, src_addr
, ie
) < 0)
1475 #endif /* CONFIG_IEEE80211R */
1478 static int wpa_supplicant_validate_ie(struct wpa_sm
*sm
,
1479 const unsigned char *src_addr
,
1480 struct wpa_eapol_ie_parse
*ie
)
1482 if (sm
->ap_wpa_ie
== NULL
&& sm
->ap_rsn_ie
== NULL
) {
1483 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1484 "WPA: No WPA/RSN IE for this AP known. "
1485 "Trying to get from scan results");
1486 if (wpa_sm_get_beacon_ie(sm
) < 0) {
1487 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1488 "WPA: Could not find AP from "
1489 "the scan results");
1492 wpa_msg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1493 "WPA: Found the current AP from updated scan results");
1496 if (ie
->wpa_ie
== NULL
&& ie
->rsn_ie
== NULL
&&
1497 (sm
->ap_wpa_ie
|| sm
->ap_rsn_ie
)) {
1498 wpa_report_ie_mismatch(sm
, "IE in 3/4 msg does not match "
1499 "with IE in Beacon/ProbeResp (no IE?)",
1500 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
1501 ie
->rsn_ie
, ie
->rsn_ie_len
);
1505 if ((ie
->wpa_ie
&& sm
->ap_wpa_ie
&&
1506 (ie
->wpa_ie_len
!= sm
->ap_wpa_ie_len
||
1507 os_memcmp(ie
->wpa_ie
, sm
->ap_wpa_ie
, ie
->wpa_ie_len
) != 0)) ||
1508 (ie
->rsn_ie
&& sm
->ap_rsn_ie
&&
1509 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm
->key_mgmt
),
1510 sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
,
1511 ie
->rsn_ie
, ie
->rsn_ie_len
))) {
1512 wpa_report_ie_mismatch(sm
, "IE in 3/4 msg does not match "
1513 "with IE in Beacon/ProbeResp",
1514 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
1515 ie
->rsn_ie
, ie
->rsn_ie_len
);
1519 if (sm
->proto
== WPA_PROTO_WPA
&&
1520 ie
->rsn_ie
&& sm
->ap_rsn_ie
== NULL
&& sm
->rsn_enabled
) {
1521 wpa_report_ie_mismatch(sm
, "Possible downgrade attack "
1522 "detected - RSN was enabled and RSN IE "
1523 "was in msg 3/4, but not in "
1525 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
1526 ie
->rsn_ie
, ie
->rsn_ie_len
);
1530 if ((sm
->ap_rsnxe
&& !ie
->rsnxe
) ||
1531 (!sm
->ap_rsnxe
&& ie
->rsnxe
) ||
1532 (sm
->ap_rsnxe
&& ie
->rsnxe
&&
1533 (sm
->ap_rsnxe_len
!= ie
->rsnxe_len
||
1534 os_memcmp(sm
->ap_rsnxe
, ie
->rsnxe
, sm
->ap_rsnxe_len
) != 0))) {
1535 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1536 "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
1537 wpa_hexdump(MSG_INFO
, "RSNXE in Beacon/ProbeResp",
1538 sm
->ap_rsnxe
, sm
->ap_rsnxe_len
);
1539 wpa_hexdump(MSG_INFO
, "RSNXE in EAPOL-Key msg 3/4",
1540 ie
->rsnxe
, ie
->rsnxe_len
);
1541 wpa_sm_deauthenticate(sm
, WLAN_REASON_IE_IN_4WAY_DIFFERS
);
1545 #ifdef CONFIG_IEEE80211R
1546 if (wpa_key_mgmt_ft(sm
->key_mgmt
) &&
1547 wpa_supplicant_validate_ie_ft(sm
, src_addr
, ie
) < 0)
1549 #endif /* CONFIG_IEEE80211R */
1556 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1557 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1558 * @dst: Destination address for the frame
1559 * @key: Pointer to the EAPOL-Key frame header
1560 * @ver: Version bits from EAPOL-Key Key Info
1561 * @key_info: Key Info
1562 * @ptk: PTK to use for keyed hash and encryption
1563 * Returns: >= 0 on success, < 0 on failure
1565 int wpa_supplicant_send_4_of_4(struct wpa_sm
*sm
, const unsigned char *dst
,
1566 const struct wpa_eapol_key
*key
,
1567 u16 ver
, u16 key_info
,
1568 struct wpa_ptk
*ptk
)
1570 size_t mic_len
, hdrlen
, rlen
;
1571 struct wpa_eapol_key
*reply
;
1574 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
1575 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
1576 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
1577 hdrlen
, &rlen
, (void *) &reply
);
1581 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
1582 sm
->proto
== WPA_PROTO_OSEN
) ?
1583 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1584 key_info
&= WPA_KEY_INFO_SECURE
;
1585 key_info
|= ver
| WPA_KEY_INFO_KEY_TYPE
;
1587 key_info
|= WPA_KEY_INFO_MIC
;
1589 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1590 WPA_PUT_BE16(reply
->key_info
, key_info
);
1591 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
)
1592 WPA_PUT_BE16(reply
->key_length
, 0);
1594 os_memcpy(reply
->key_length
, key
->key_length
, 2);
1595 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
1596 WPA_REPLAY_COUNTER_LEN
);
1598 key_mic
= (u8
*) (reply
+ 1);
1599 WPA_PUT_BE16(key_mic
+ mic_len
, 0);
1601 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Sending EAPOL-Key 4/4");
1602 return wpa_eapol_key_send(sm
, ptk
, ver
, dst
, ETH_P_EAPOL
, rbuf
, rlen
,
1607 static void wpa_supplicant_process_3_of_4(struct wpa_sm
*sm
,
1608 const struct wpa_eapol_key
*key
,
1609 u16 ver
, const u8
*key_data
,
1610 size_t key_data_len
)
1612 u16 key_info
, keylen
;
1613 struct wpa_eapol_ie_parse ie
;
1615 wpa_sm_set_state(sm
, WPA_4WAY_HANDSHAKE
);
1616 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: RX message 3 of 4-Way "
1617 "Handshake from " MACSTR
" (ver=%d)", MAC2STR(sm
->bssid
), ver
);
1619 key_info
= WPA_GET_BE16(key
->key_info
);
1621 wpa_hexdump(MSG_DEBUG
, "WPA: IE KeyData", key_data
, key_data_len
);
1622 if (wpa_supplicant_parse_ies(key_data
, key_data_len
, &ie
) < 0)
1624 if (ie
.gtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1625 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1626 "WPA: GTK IE in unencrypted key data");
1629 if (ie
.igtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1630 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1631 "WPA: IGTK KDE in unencrypted key data");
1636 wpa_cipher_valid_mgmt_group(sm
->mgmt_group_cipher
) &&
1637 ie
.igtk_len
!= WPA_IGTK_KDE_PREFIX_LEN
+
1638 (unsigned int) wpa_cipher_key_len(sm
->mgmt_group_cipher
)) {
1639 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1640 "WPA: Invalid IGTK KDE length %lu",
1641 (unsigned long) ie
.igtk_len
);
1645 if (wpa_supplicant_validate_ie(sm
, sm
->bssid
, &ie
) < 0)
1648 if (wpa_handle_ext_key_id(sm
, &ie
))
1651 if (os_memcmp(sm
->anonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0) {
1652 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1653 "WPA: ANonce from message 1 of 4-Way Handshake "
1654 "differs from 3 of 4-Way Handshake - drop packet (src="
1655 MACSTR
")", MAC2STR(sm
->bssid
));
1659 keylen
= WPA_GET_BE16(key
->key_length
);
1660 if (keylen
!= wpa_cipher_key_len(sm
->pairwise_cipher
)) {
1661 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1662 "WPA: Invalid %s key length %d (src=" MACSTR
1663 ")", wpa_cipher_txt(sm
->pairwise_cipher
), keylen
,
1664 MAC2STR(sm
->bssid
));
1669 if (ie
.ip_addr_alloc
) {
1670 os_memcpy(sm
->p2p_ip_addr
, ie
.ip_addr_alloc
, 3 * 4);
1671 wpa_hexdump(MSG_DEBUG
, "P2P: IP address info",
1672 sm
->p2p_ip_addr
, sizeof(sm
->p2p_ip_addr
));
1674 #endif /* CONFIG_P2P */
1677 if (wpa_sm_ocv_enabled(sm
)) {
1678 struct wpa_channel_info ci
;
1680 if (wpa_sm_channel_info(sm
, &ci
) != 0) {
1681 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1682 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1686 if (ocv_verify_tx_params(ie
.oci
, ie
.oci_len
, &ci
,
1687 channel_width_to_int(ci
.chanwidth
),
1688 ci
.seg1_idx
) != 0) {
1689 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "%s",
1694 #endif /* CONFIG_OCV */
1696 if (sm
->use_ext_key_id
&&
1697 wpa_supplicant_install_ptk(sm
, key
, KEY_FLAG_RX
))
1700 if (wpa_supplicant_send_4_of_4(sm
, sm
->bssid
, key
, ver
, key_info
,
1705 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1706 * for the next 4-Way Handshake. If msg 3 is received again, the old
1707 * SNonce will still be used to avoid changing PTK. */
1708 sm
->renew_snonce
= 1;
1710 if (key_info
& WPA_KEY_INFO_INSTALL
) {
1713 if (sm
->use_ext_key_id
)
1714 res
= wpa_supplicant_activate_ptk(sm
);
1716 res
= wpa_supplicant_install_ptk(sm
, key
,
1722 if (key_info
& WPA_KEY_INFO_SECURE
) {
1723 wpa_sm_mlme_setprotection(
1724 sm
, sm
->bssid
, MLME_SETPROTECTION_PROTECT_TYPE_RX
,
1725 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE
);
1726 eapol_sm_notify_portValid(sm
->eapol
, TRUE
);
1728 wpa_sm_set_state(sm
, WPA_GROUP_HANDSHAKE
);
1730 if (sm
->group_cipher
== WPA_CIPHER_GTK_NOT_USED
) {
1731 /* No GTK to be set to the driver */
1732 } else if (!ie
.gtk
&& sm
->proto
== WPA_PROTO_RSN
) {
1733 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1734 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
1736 } else if (ie
.gtk
&&
1737 wpa_supplicant_pairwise_gtk(sm
, key
,
1738 ie
.gtk
, ie
.gtk_len
, key_info
) < 0) {
1739 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1740 "RSN: Failed to configure GTK");
1744 if (ieee80211w_set_keys(sm
, &ie
) < 0) {
1745 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1746 "RSN: Failed to configure IGTK");
1750 if (sm
->group_cipher
== WPA_CIPHER_GTK_NOT_USED
|| ie
.gtk
)
1751 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
,
1752 key_info
& WPA_KEY_INFO_SECURE
);
1755 wpa_sm_set_rekey_offload(sm
);
1757 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1758 * calculated only after KCK has been derived. Though, do not replace an
1759 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1760 * to avoid unnecessary changes of PMKID while continuing to use the
1762 if (sm
->proto
== WPA_PROTO_RSN
&& wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
1764 struct rsn_pmksa_cache_entry
*sa
;
1766 sa
= pmksa_cache_add(sm
->pmksa
, sm
->pmk
, sm
->pmk_len
, NULL
,
1767 sm
->ptk
.kck
, sm
->ptk
.kck_len
,
1768 sm
->bssid
, sm
->own_addr
,
1769 sm
->network_ctx
, sm
->key_mgmt
, NULL
);
1774 sm
->msg_3_of_4_ok
= 1;
1778 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
1782 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm
*sm
,
1786 struct wpa_gtk_data
*gd
)
1789 struct wpa_eapol_ie_parse ie
;
1791 wpa_hexdump_key(MSG_DEBUG
, "RSN: msg 1/2 key data",
1792 keydata
, keydatalen
);
1793 if (wpa_supplicant_parse_ies(keydata
, keydatalen
, &ie
) < 0)
1795 if (ie
.gtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1796 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1797 "WPA: GTK IE in unencrypted key data");
1800 if (ie
.gtk
== NULL
) {
1801 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1802 "WPA: No GTK IE in Group Key msg 1/2");
1805 maxkeylen
= gd
->gtk_len
= ie
.gtk_len
- 2;
1808 if (wpa_sm_ocv_enabled(sm
)) {
1809 struct wpa_channel_info ci
;
1811 if (wpa_sm_channel_info(sm
, &ci
) != 0) {
1812 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1813 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1817 if (ocv_verify_tx_params(ie
.oci
, ie
.oci_len
, &ci
,
1818 channel_width_to_int(ci
.chanwidth
),
1819 ci
.seg1_idx
) != 0) {
1820 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "%s",
1825 #endif /* CONFIG_OCV */
1827 if (wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
1828 gd
->gtk_len
, maxkeylen
,
1829 &gd
->key_rsc_len
, &gd
->alg
))
1832 wpa_hexdump_key(MSG_DEBUG
, "RSN: received GTK in group key handshake",
1833 ie
.gtk
, ie
.gtk_len
);
1834 gd
->keyidx
= ie
.gtk
[0] & 0x3;
1835 gd
->tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
1836 !!(ie
.gtk
[0] & BIT(2)));
1837 if (ie
.gtk_len
- 2 > sizeof(gd
->gtk
)) {
1838 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1839 "RSN: Too long GTK in GTK IE (len=%lu)",
1840 (unsigned long) ie
.gtk_len
- 2);
1843 os_memcpy(gd
->gtk
, ie
.gtk
+ 2, ie
.gtk_len
- 2);
1845 if (ieee80211w_set_keys(sm
, &ie
) < 0)
1846 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1847 "RSN: Failed to configure IGTK");
1853 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm
*sm
,
1854 const struct wpa_eapol_key
*key
,
1856 size_t key_data_len
, u16 key_info
,
1857 u16 ver
, struct wpa_gtk_data
*gd
)
1862 gtk_len
= WPA_GET_BE16(key
->key_length
);
1863 maxkeylen
= key_data_len
;
1864 if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1865 if (maxkeylen
< 8) {
1866 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1867 "WPA: Too short maxkeylen (%lu)",
1868 (unsigned long) maxkeylen
);
1874 if (gtk_len
> maxkeylen
||
1875 wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
1877 &gd
->key_rsc_len
, &gd
->alg
))
1880 gd
->gtk_len
= gtk_len
;
1881 gd
->keyidx
= (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) >>
1882 WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1883 if (ver
== WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&& sm
->ptk
.kek_len
== 16) {
1884 #ifdef CONFIG_NO_RC4
1885 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1886 "WPA: RC4 not supported in the build");
1888 #else /* CONFIG_NO_RC4 */
1890 if (key_data_len
> sizeof(gd
->gtk
)) {
1891 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1892 "WPA: RC4 key data too long (%lu)",
1893 (unsigned long) key_data_len
);
1896 os_memcpy(ek
, key
->key_iv
, 16);
1897 os_memcpy(ek
+ 16, sm
->ptk
.kek
, sm
->ptk
.kek_len
);
1898 os_memcpy(gd
->gtk
, key_data
, key_data_len
);
1899 if (rc4_skip(ek
, 32, 256, gd
->gtk
, key_data_len
)) {
1900 forced_memzero(ek
, sizeof(ek
));
1901 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
1905 forced_memzero(ek
, sizeof(ek
));
1906 #endif /* CONFIG_NO_RC4 */
1907 } else if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1908 if (maxkeylen
% 8) {
1909 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1910 "WPA: Unsupported AES-WRAP len %lu",
1911 (unsigned long) maxkeylen
);
1914 if (maxkeylen
> sizeof(gd
->gtk
)) {
1915 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1916 "WPA: AES-WRAP key data "
1917 "too long (keydatalen=%lu maxkeylen=%lu)",
1918 (unsigned long) key_data_len
,
1919 (unsigned long) maxkeylen
);
1922 if (aes_unwrap(sm
->ptk
.kek
, sm
->ptk
.kek_len
, maxkeylen
/ 8,
1923 key_data
, gd
->gtk
)) {
1924 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1925 "WPA: AES unwrap failed - could not decrypt "
1930 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1931 "WPA: Unsupported key_info type %d", ver
);
1934 gd
->tx
= wpa_supplicant_gtk_tx_bit_workaround(
1935 sm
, !!(key_info
& WPA_KEY_INFO_TXRX
));
1940 static int wpa_supplicant_send_2_of_2(struct wpa_sm
*sm
,
1941 const struct wpa_eapol_key
*key
,
1942 int ver
, u16 key_info
)
1944 size_t mic_len
, hdrlen
, rlen
;
1945 struct wpa_eapol_key
*reply
;
1950 if (wpa_sm_ocv_enabled(sm
))
1951 kde_len
= OCV_OCI_KDE_LEN
;
1952 #endif /* CONFIG_OCV */
1954 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
1955 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
1956 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
1957 hdrlen
+ kde_len
, &rlen
, (void *) &reply
);
1961 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
1962 sm
->proto
== WPA_PROTO_OSEN
) ?
1963 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1964 key_info
&= WPA_KEY_INFO_KEY_INDEX_MASK
;
1965 key_info
|= ver
| WPA_KEY_INFO_SECURE
;
1967 key_info
|= WPA_KEY_INFO_MIC
;
1969 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1970 WPA_PUT_BE16(reply
->key_info
, key_info
);
1971 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
)
1972 WPA_PUT_BE16(reply
->key_length
, 0);
1974 os_memcpy(reply
->key_length
, key
->key_length
, 2);
1975 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
1976 WPA_REPLAY_COUNTER_LEN
);
1978 key_mic
= (u8
*) (reply
+ 1);
1979 WPA_PUT_BE16(key_mic
+ mic_len
, kde_len
); /* Key Data Length */
1982 if (wpa_sm_ocv_enabled(sm
)) {
1983 struct wpa_channel_info ci
;
1986 if (wpa_sm_channel_info(sm
, &ci
) != 0) {
1987 wpa_printf(MSG_WARNING
,
1988 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
1993 pos
= key_mic
+ mic_len
+ 2; /* Key Data */
1994 if (ocv_insert_oci_kde(&ci
, &pos
) < 0) {
1999 #endif /* CONFIG_OCV */
2001 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Sending EAPOL-Key 2/2");
2002 return wpa_eapol_key_send(sm
, &sm
->ptk
, ver
, sm
->bssid
, ETH_P_EAPOL
,
2003 rbuf
, rlen
, key_mic
);
2007 static void wpa_supplicant_process_1_of_2(struct wpa_sm
*sm
,
2008 const unsigned char *src_addr
,
2009 const struct wpa_eapol_key
*key
,
2011 size_t key_data_len
, u16 ver
)
2015 struct wpa_gtk_data gd
;
2018 if (!sm
->msg_3_of_4_ok
&& !wpa_fils_is_completed(sm
)) {
2019 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2020 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
2024 os_memset(&gd
, 0, sizeof(gd
));
2026 rekey
= wpa_sm_get_state(sm
) == WPA_COMPLETED
;
2027 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: RX message 1 of Group Key "
2028 "Handshake from " MACSTR
" (ver=%d)", MAC2STR(src_addr
), ver
);
2030 key_info
= WPA_GET_BE16(key
->key_info
);
2032 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) {
2033 ret
= wpa_supplicant_process_1_of_2_rsn(sm
, key_data
,
2034 key_data_len
, key_info
,
2037 ret
= wpa_supplicant_process_1_of_2_wpa(sm
, key
, key_data
,
2039 key_info
, ver
, &gd
);
2042 wpa_sm_set_state(sm
, WPA_GROUP_HANDSHAKE
);
2047 key_rsc
= key
->key_rsc
;
2048 if (wpa_supplicant_rsc_relaxation(sm
, key
->key_rsc
))
2051 if (wpa_supplicant_install_gtk(sm
, &gd
, key_rsc
, 0) ||
2052 wpa_supplicant_send_2_of_2(sm
, key
, ver
, key_info
) < 0)
2054 forced_memzero(&gd
, sizeof(gd
));
2057 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
, "WPA: Group rekeying "
2058 "completed with " MACSTR
" [GTK=%s]",
2059 MAC2STR(sm
->bssid
), wpa_cipher_txt(sm
->group_cipher
));
2060 wpa_sm_cancel_auth_timeout(sm
);
2061 wpa_sm_set_state(sm
, WPA_COMPLETED
);
2063 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
,
2065 WPA_KEY_INFO_SECURE
);
2068 wpa_sm_set_rekey_offload(sm
);
2073 forced_memzero(&gd
, sizeof(gd
));
2074 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
2078 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm
*sm
,
2079 struct wpa_eapol_key
*key
,
2081 const u8
*buf
, size_t len
)
2083 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
];
2085 size_t mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
2087 os_memcpy(mic
, key
+ 1, mic_len
);
2089 os_memset(key
+ 1, 0, mic_len
);
2090 if (wpa_eapol_key_mic(sm
->tptk
.kck
, sm
->tptk
.kck_len
,
2092 ver
, buf
, len
, (u8
*) (key
+ 1)) < 0 ||
2093 os_memcmp_const(mic
, key
+ 1, mic_len
) != 0) {
2094 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2095 "WPA: Invalid EAPOL-Key MIC "
2096 "when using TPTK - ignoring TPTK");
2098 wpa_printf(MSG_INFO
,
2099 "TEST: Ignore Key MIC failure for fuzz testing");
2101 #endif /* TEST_FUZZ */
2105 #endif /* TEST_FUZZ */
2109 os_memcpy(&sm
->ptk
, &sm
->tptk
, sizeof(sm
->ptk
));
2110 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
2112 * This assures the same TPTK in sm->tptk can never be
2113 * copied twice to sm->ptk as the new PTK. In
2114 * combination with the installed flag in the wpa_ptk
2115 * struct, this assures the same PTK is only installed
2118 sm
->renew_snonce
= 1;
2122 if (!ok
&& sm
->ptk_set
) {
2123 os_memset(key
+ 1, 0, mic_len
);
2124 if (wpa_eapol_key_mic(sm
->ptk
.kck
, sm
->ptk
.kck_len
,
2126 ver
, buf
, len
, (u8
*) (key
+ 1)) < 0 ||
2127 os_memcmp_const(mic
, key
+ 1, mic_len
) != 0) {
2128 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2129 "WPA: Invalid EAPOL-Key MIC - "
2132 wpa_printf(MSG_INFO
,
2133 "TEST: Ignore Key MIC failure for fuzz testing");
2134 goto continue_fuzz2
;
2135 #endif /* TEST_FUZZ */
2140 #endif /* TEST_FUZZ */
2145 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2146 "WPA: Could not verify EAPOL-Key MIC - "
2151 os_memcpy(sm
->rx_replay_counter
, key
->replay_counter
,
2152 WPA_REPLAY_COUNTER_LEN
);
2153 sm
->rx_replay_counter_set
= 1;
2158 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
2159 static int wpa_supplicant_decrypt_key_data(struct wpa_sm
*sm
,
2160 struct wpa_eapol_key
*key
,
2161 size_t mic_len
, u16 ver
,
2162 u8
*key_data
, size_t *key_data_len
)
2164 wpa_hexdump(MSG_DEBUG
, "RSN: encrypted key data",
2165 key_data
, *key_data_len
);
2167 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2168 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
2173 /* Decrypt key data here so that this operation does not need
2174 * to be implemented separately for each message type. */
2175 if (ver
== WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&& sm
->ptk
.kek_len
== 16) {
2176 #ifdef CONFIG_NO_RC4
2177 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2178 "WPA: RC4 not supported in the build");
2180 #else /* CONFIG_NO_RC4 */
2183 wpa_printf(MSG_DEBUG
, "WPA: Decrypt Key Data using RC4");
2184 os_memcpy(ek
, key
->key_iv
, 16);
2185 os_memcpy(ek
+ 16, sm
->ptk
.kek
, sm
->ptk
.kek_len
);
2186 if (rc4_skip(ek
, 32, 256, key_data
, *key_data_len
)) {
2187 forced_memzero(ek
, sizeof(ek
));
2188 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
2192 forced_memzero(ek
, sizeof(ek
));
2193 #endif /* CONFIG_NO_RC4 */
2194 } else if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
2195 ver
== WPA_KEY_INFO_TYPE_AES_128_CMAC
||
2196 wpa_use_aes_key_wrap(sm
->key_mgmt
)) {
2199 wpa_printf(MSG_DEBUG
,
2200 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
2201 (unsigned int) sm
->ptk
.kek_len
);
2202 if (*key_data_len
< 8 || *key_data_len
% 8) {
2203 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2204 "WPA: Unsupported AES-WRAP len %u",
2205 (unsigned int) *key_data_len
);
2208 *key_data_len
-= 8; /* AES-WRAP adds 8 bytes */
2209 buf
= os_malloc(*key_data_len
);
2211 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2212 "WPA: No memory for AES-UNWRAP buffer");
2216 os_memset(buf
, 0x11, *key_data_len
);
2217 #endif /* TEST_FUZZ */
2218 if (aes_unwrap(sm
->ptk
.kek
, sm
->ptk
.kek_len
, *key_data_len
/ 8,
2221 wpa_printf(MSG_INFO
,
2222 "TEST: Ignore AES unwrap failure for fuzz testing");
2224 #endif /* TEST_FUZZ */
2225 bin_clear_free(buf
, *key_data_len
);
2226 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2227 "WPA: AES unwrap failed - "
2228 "could not decrypt EAPOL-Key key data");
2233 #endif /* TEST_FUZZ */
2234 os_memcpy(key_data
, buf
, *key_data_len
);
2235 bin_clear_free(buf
, *key_data_len
);
2236 WPA_PUT_BE16(((u8
*) (key
+ 1)) + mic_len
, *key_data_len
);
2238 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2239 "WPA: Unsupported key_info type %d", ver
);
2242 wpa_hexdump_key(MSG_DEBUG
, "WPA: decrypted EAPOL-Key key data",
2243 key_data
, *key_data_len
);
2249 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2250 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2252 void wpa_sm_aborted_cached(struct wpa_sm
*sm
)
2254 if (sm
&& sm
->cur_pmksa
) {
2255 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2256 "RSN: Cancelling PMKSA caching attempt");
2257 sm
->cur_pmksa
= NULL
;
2262 static void wpa_eapol_key_dump(struct wpa_sm
*sm
,
2263 const struct wpa_eapol_key
*key
,
2264 unsigned int key_data_len
,
2265 const u8
*mic
, unsigned int mic_len
)
2267 #ifndef CONFIG_NO_STDOUT_DEBUG
2268 u16 key_info
= WPA_GET_BE16(key
->key_info
);
2270 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, " EAPOL-Key type=%d", key
->type
);
2271 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2272 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2273 key_info
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
2274 (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) >>
2275 WPA_KEY_INFO_KEY_INDEX_SHIFT
,
2276 (key_info
& (BIT(13) | BIT(14) | BIT(15))) >> 13,
2277 key_info
& WPA_KEY_INFO_KEY_TYPE
? "Pairwise" : "Group",
2278 key_info
& WPA_KEY_INFO_INSTALL
? " Install" : "",
2279 key_info
& WPA_KEY_INFO_ACK
? " Ack" : "",
2280 key_info
& WPA_KEY_INFO_MIC
? " MIC" : "",
2281 key_info
& WPA_KEY_INFO_SECURE
? " Secure" : "",
2282 key_info
& WPA_KEY_INFO_ERROR
? " Error" : "",
2283 key_info
& WPA_KEY_INFO_REQUEST
? " Request" : "",
2284 key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
? " Encr" : "");
2285 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2286 " key_length=%u key_data_length=%u",
2287 WPA_GET_BE16(key
->key_length
), key_data_len
);
2288 wpa_hexdump(MSG_DEBUG
, " replay_counter",
2289 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
2290 wpa_hexdump(MSG_DEBUG
, " key_nonce", key
->key_nonce
, WPA_NONCE_LEN
);
2291 wpa_hexdump(MSG_DEBUG
, " key_iv", key
->key_iv
, 16);
2292 wpa_hexdump(MSG_DEBUG
, " key_rsc", key
->key_rsc
, 8);
2293 wpa_hexdump(MSG_DEBUG
, " key_id (reserved)", key
->key_id
, 8);
2294 wpa_hexdump(MSG_DEBUG
, " key_mic", mic
, mic_len
);
2295 #endif /* CONFIG_NO_STDOUT_DEBUG */
2300 static int wpa_supp_aead_decrypt(struct wpa_sm
*sm
, u8
*buf
, size_t buf_len
,
2301 size_t *key_data_len
)
2303 struct wpa_ptk
*ptk
;
2304 struct ieee802_1x_hdr
*hdr
;
2305 struct wpa_eapol_key
*key
;
2310 if (*key_data_len
< AES_BLOCK_SIZE
) {
2311 wpa_printf(MSG_INFO
, "No room for AES-SIV data in the frame");
2317 else if (sm
->ptk_set
)
2322 hdr
= (struct ieee802_1x_hdr
*) buf
;
2323 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2324 pos
= (u8
*) (key
+ 1);
2325 pos
+= 2; /* Pointing at the Encrypted Key Data field */
2327 tmp
= os_malloc(*key_data_len
);
2331 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2332 * to Key Data (exclusive). */
2334 aad_len
[0] = pos
- buf
;
2335 if (aes_siv_decrypt(ptk
->kek
, ptk
->kek_len
, pos
, *key_data_len
,
2336 1, aad
, aad_len
, tmp
) < 0) {
2337 wpa_printf(MSG_INFO
, "Invalid AES-SIV data in the frame");
2338 bin_clear_free(tmp
, *key_data_len
);
2342 /* AEAD decryption and validation completed successfully */
2343 (*key_data_len
) -= AES_BLOCK_SIZE
;
2344 wpa_hexdump_key(MSG_DEBUG
, "WPA: Decrypted Key Data",
2345 tmp
, *key_data_len
);
2347 /* Replace Key Data field with the decrypted version */
2348 os_memcpy(pos
, tmp
, *key_data_len
);
2349 pos
-= 2; /* Key Data Length field */
2350 WPA_PUT_BE16(pos
, *key_data_len
);
2351 bin_clear_free(tmp
, *key_data_len
);
2356 os_memcpy(&sm
->ptk
, &sm
->tptk
, sizeof(sm
->ptk
));
2357 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
2360 os_memcpy(sm
->rx_replay_counter
, key
->replay_counter
,
2361 WPA_REPLAY_COUNTER_LEN
);
2362 sm
->rx_replay_counter_set
= 1;
2366 #endif /* CONFIG_FILS */
2370 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2371 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2372 * @src_addr: Source MAC address of the EAPOL packet
2373 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2374 * @len: Length of the EAPOL frame
2375 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2377 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2378 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2379 * only processing WPA and WPA2 EAPOL-Key frames.
2381 * The received EAPOL-Key packets are validated and valid packets are replied
2382 * to. In addition, key material (PTK, GTK) is configured at the end of a
2383 * successful key handshake.
2385 int wpa_sm_rx_eapol(struct wpa_sm
*sm
, const u8
*src_addr
,
2386 const u8
*buf
, size_t len
)
2388 size_t plen
, data_len
, key_data_len
;
2389 const struct ieee802_1x_hdr
*hdr
;
2390 struct wpa_eapol_key
*key
;
2395 size_t mic_len
, keyhdrlen
;
2397 #ifdef CONFIG_IEEE80211R
2398 sm
->ft_completed
= 0;
2399 #endif /* CONFIG_IEEE80211R */
2401 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
2402 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
2404 if (len
< sizeof(*hdr
) + keyhdrlen
) {
2405 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2406 "WPA: EAPOL frame too short to be a WPA "
2407 "EAPOL-Key (len %lu, expecting at least %lu)",
2408 (unsigned long) len
,
2409 (unsigned long) sizeof(*hdr
) + keyhdrlen
);
2413 hdr
= (const struct ieee802_1x_hdr
*) buf
;
2414 plen
= be_to_host16(hdr
->length
);
2415 data_len
= plen
+ sizeof(*hdr
);
2416 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2417 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2418 hdr
->version
, hdr
->type
, (unsigned long) plen
);
2420 if (hdr
->version
< EAPOL_VERSION
) {
2421 /* TODO: backwards compatibility */
2423 if (hdr
->type
!= IEEE802_1X_TYPE_EAPOL_KEY
) {
2424 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2425 "WPA: EAPOL frame (type %u) discarded, "
2426 "not a Key frame", hdr
->type
);
2430 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL-Key", buf
, len
);
2431 if (plen
> len
- sizeof(*hdr
) || plen
< keyhdrlen
) {
2432 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2433 "WPA: EAPOL frame payload size %lu "
2434 "invalid (frame size %lu)",
2435 (unsigned long) plen
, (unsigned long) len
);
2439 if (data_len
< len
) {
2440 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2441 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2442 (unsigned long) len
- data_len
);
2446 * Make a copy of the frame since we need to modify the buffer during
2447 * MAC validation and Key Data decryption.
2449 tmp
= os_memdup(buf
, data_len
);
2452 key
= (struct wpa_eapol_key
*) (tmp
+ sizeof(struct ieee802_1x_hdr
));
2453 mic
= (u8
*) (key
+ 1);
2454 key_data
= mic
+ mic_len
+ 2;
2456 if (key
->type
!= EAPOL_KEY_TYPE_WPA
&& key
->type
!= EAPOL_KEY_TYPE_RSN
)
2458 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2459 "WPA: EAPOL-Key type (%d) unknown, discarded",
2465 key_data_len
= WPA_GET_BE16(mic
+ mic_len
);
2466 wpa_eapol_key_dump(sm
, key
, key_data_len
, mic
, mic_len
);
2468 if (key_data_len
> plen
- keyhdrlen
) {
2469 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
, "WPA: Invalid EAPOL-Key "
2470 "frame - key_data overflow (%u > %u)",
2471 (unsigned int) key_data_len
,
2472 (unsigned int) (plen
- keyhdrlen
));
2476 eapol_sm_notify_lower_layer_success(sm
->eapol
, 0);
2477 key_info
= WPA_GET_BE16(key
->key_info
);
2478 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
2479 if (ver
!= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&&
2480 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
&&
2481 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
&&
2482 !wpa_use_akm_defined(sm
->key_mgmt
)) {
2483 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2484 "WPA: Unsupported EAPOL-Key descriptor version %d",
2489 if (wpa_use_akm_defined(sm
->key_mgmt
) &&
2490 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
2491 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2492 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2497 #ifdef CONFIG_IEEE80211R
2498 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
2499 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
2500 if (ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
&&
2501 !wpa_use_akm_defined(sm
->key_mgmt
)) {
2502 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2503 "FT: AP did not use AES-128-CMAC");
2507 #endif /* CONFIG_IEEE80211R */
2508 if (wpa_key_mgmt_sha256(sm
->key_mgmt
)) {
2509 if (ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
&&
2510 !wpa_use_akm_defined(sm
->key_mgmt
)) {
2511 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2512 "WPA: AP did not use the "
2513 "negotiated AES-128-CMAC");
2516 } else if (sm
->pairwise_cipher
== WPA_CIPHER_CCMP
&&
2517 !wpa_use_akm_defined(sm
->key_mgmt
) &&
2518 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
2519 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2520 "WPA: CCMP is used, but EAPOL-Key "
2521 "descriptor version (%d) is not 2", ver
);
2522 if (sm
->group_cipher
!= WPA_CIPHER_CCMP
&&
2523 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
2524 /* Earlier versions of IEEE 802.11i did not explicitly
2525 * require version 2 descriptor for all EAPOL-Key
2526 * packets, so allow group keys to use version 1 if
2527 * CCMP is not used for them. */
2528 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2529 "WPA: Backwards compatibility: allow invalid "
2530 "version for non-CCMP group keys");
2531 } else if (ver
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
2532 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2533 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
2536 } else if (sm
->pairwise_cipher
== WPA_CIPHER_GCMP
&&
2537 !wpa_use_akm_defined(sm
->key_mgmt
) &&
2538 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
2539 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2540 "WPA: GCMP is used, but EAPOL-Key "
2541 "descriptor version (%d) is not 2", ver
);
2545 if (sm
->rx_replay_counter_set
&&
2546 os_memcmp(key
->replay_counter
, sm
->rx_replay_counter
,
2547 WPA_REPLAY_COUNTER_LEN
) <= 0) {
2548 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2549 "WPA: EAPOL-Key Replay Counter did not increase - "
2554 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
2555 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2556 "WPA: Unsupported SMK bit in key_info");
2560 if (!(key_info
& WPA_KEY_INFO_ACK
)) {
2561 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2562 "WPA: No Ack bit in key_info");
2566 if (key_info
& WPA_KEY_INFO_REQUEST
) {
2567 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2568 "WPA: EAPOL-Key with Request bit - dropped");
2572 if ((key_info
& WPA_KEY_INFO_MIC
) &&
2573 wpa_supplicant_verify_eapol_key_mic(sm
, key
, ver
, tmp
, data_len
))
2577 if (!mic_len
&& (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
2578 if (wpa_supp_aead_decrypt(sm
, tmp
, data_len
, &key_data_len
))
2581 #endif /* CONFIG_FILS */
2583 if ((sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) &&
2584 (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) && mic_len
) {
2586 * Only decrypt the Key Data field if the frame's authenticity
2587 * was verified. When using AES-SIV (FILS), the MIC flag is not
2588 * set, so this check should only be performed if mic_len != 0
2589 * which is the case in this code branch.
2591 if (!(key_info
& WPA_KEY_INFO_MIC
)) {
2592 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2593 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2596 if (wpa_supplicant_decrypt_key_data(sm
, key
, mic_len
,
2602 if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
2603 if (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) {
2604 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2605 "WPA: Ignored EAPOL-Key (Pairwise) with "
2606 "non-zero key index");
2609 if (key_info
& (WPA_KEY_INFO_MIC
|
2610 WPA_KEY_INFO_ENCR_KEY_DATA
)) {
2611 /* 3/4 4-Way Handshake */
2612 wpa_supplicant_process_3_of_4(sm
, key
, ver
, key_data
,
2615 /* 1/4 4-Way Handshake */
2616 wpa_supplicant_process_1_of_4(sm
, src_addr
, key
,
2621 if ((mic_len
&& (key_info
& WPA_KEY_INFO_MIC
)) ||
2622 (!mic_len
&& (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
))) {
2623 /* 1/2 Group Key Handshake */
2624 wpa_supplicant_process_1_of_2(sm
, src_addr
, key
,
2625 key_data
, key_data_len
,
2628 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2629 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
2637 bin_clear_free(tmp
, data_len
);
2642 #ifdef CONFIG_CTRL_IFACE
2643 static u32
wpa_key_mgmt_suite(struct wpa_sm
*sm
)
2645 switch (sm
->key_mgmt
) {
2646 case WPA_KEY_MGMT_IEEE8021X
:
2647 return ((sm
->proto
== WPA_PROTO_RSN
||
2648 sm
->proto
== WPA_PROTO_OSEN
) ?
2649 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X
:
2650 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X
);
2651 case WPA_KEY_MGMT_PSK
:
2652 return (sm
->proto
== WPA_PROTO_RSN
?
2653 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X
:
2654 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X
);
2655 #ifdef CONFIG_IEEE80211R
2656 case WPA_KEY_MGMT_FT_IEEE8021X
:
2657 return RSN_AUTH_KEY_MGMT_FT_802_1X
;
2658 case WPA_KEY_MGMT_FT_PSK
:
2659 return RSN_AUTH_KEY_MGMT_FT_PSK
;
2660 #endif /* CONFIG_IEEE80211R */
2661 case WPA_KEY_MGMT_IEEE8021X_SHA256
:
2662 return RSN_AUTH_KEY_MGMT_802_1X_SHA256
;
2663 case WPA_KEY_MGMT_PSK_SHA256
:
2664 return RSN_AUTH_KEY_MGMT_PSK_SHA256
;
2665 case WPA_KEY_MGMT_CCKM
:
2666 return (sm
->proto
== WPA_PROTO_RSN
?
2667 RSN_AUTH_KEY_MGMT_CCKM
:
2668 WPA_AUTH_KEY_MGMT_CCKM
);
2669 case WPA_KEY_MGMT_WPA_NONE
:
2670 return WPA_AUTH_KEY_MGMT_NONE
;
2671 case WPA_KEY_MGMT_IEEE8021X_SUITE_B
:
2672 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B
;
2673 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192
:
2674 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192
;
2681 #define RSN_SUITE "%02x-%02x-%02x-%d"
2682 #define RSN_SUITE_ARG(s) \
2683 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2686 * wpa_sm_get_mib - Dump text list of MIB entries
2687 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2688 * @buf: Buffer for the list
2689 * @buflen: Length of the buffer
2690 * Returns: Number of bytes written to buffer
2692 * This function is used fetch dot11 MIB variables.
2694 int wpa_sm_get_mib(struct wpa_sm
*sm
, char *buf
, size_t buflen
)
2696 char pmkid_txt
[PMKID_LEN
* 2 + 1];
2700 if (sm
->cur_pmksa
) {
2701 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
2702 sm
->cur_pmksa
->pmkid
, PMKID_LEN
);
2704 pmkid_txt
[0] = '\0';
2706 if ((wpa_key_mgmt_wpa_psk(sm
->key_mgmt
) ||
2707 wpa_key_mgmt_wpa_ieee8021x(sm
->key_mgmt
)) &&
2708 sm
->proto
== WPA_PROTO_RSN
)
2713 ret
= os_snprintf(buf
, buflen
,
2714 "dot11RSNAOptionImplemented=TRUE\n"
2715 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2716 "dot11RSNAEnabled=%s\n"
2717 "dot11RSNAPreauthenticationEnabled=%s\n"
2718 "dot11RSNAConfigVersion=%d\n"
2719 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2720 "dot11RSNAConfigGroupCipherSize=%d\n"
2721 "dot11RSNAConfigPMKLifetime=%d\n"
2722 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2723 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2724 "dot11RSNAConfigSATimeout=%d\n",
2725 rsna
? "TRUE" : "FALSE",
2726 rsna
? "TRUE" : "FALSE",
2728 wpa_cipher_key_len(sm
->group_cipher
) * 8,
2729 sm
->dot11RSNAConfigPMKLifetime
,
2730 sm
->dot11RSNAConfigPMKReauthThreshold
,
2731 sm
->dot11RSNAConfigSATimeout
);
2732 if (os_snprintf_error(buflen
, ret
))
2737 buf
+ len
, buflen
- len
,
2738 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
2739 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
2740 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
2741 "dot11RSNAPMKIDUsed=%s\n"
2742 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
2743 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
2744 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
2745 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2746 "dot11RSNA4WayHandshakeFailures=%u\n",
2747 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm
)),
2748 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2749 sm
->pairwise_cipher
)),
2750 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2753 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm
)),
2754 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2755 sm
->pairwise_cipher
)),
2756 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2758 sm
->dot11RSNA4WayHandshakeFailures
);
2759 if (!os_snprintf_error(buflen
- len
, ret
))
2764 #endif /* CONFIG_CTRL_IFACE */
2767 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
2768 void *ctx
, enum pmksa_free_reason reason
)
2770 struct wpa_sm
*sm
= ctx
;
2773 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: PMKSA cache entry free_cb: "
2774 MACSTR
" reason=%d", MAC2STR(entry
->aa
), reason
);
2776 if (sm
->cur_pmksa
== entry
) {
2777 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2778 "RSN: %s current PMKSA entry",
2779 reason
== PMKSA_REPLACE
? "replaced" : "removed");
2780 pmksa_cache_clear_current(sm
);
2783 * If an entry is simply being replaced, there's no need to
2784 * deauthenticate because it will be immediately re-added.
2785 * This happens when EAP authentication is completed again
2786 * (reauth or failed PMKSA caching attempt).
2788 if (reason
!= PMKSA_REPLACE
)
2792 if (reason
== PMKSA_EXPIRE
&&
2793 (sm
->pmk_len
== entry
->pmk_len
&&
2794 os_memcmp(sm
->pmk
, entry
->pmk
, sm
->pmk_len
) == 0)) {
2795 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2796 "RSN: deauthenticating due to expired PMK");
2797 pmksa_cache_clear_current(sm
);
2803 os_memset(sm
->pmk
, 0, sizeof(sm
->pmk
));
2804 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
2810 * wpa_sm_init - Initialize WPA state machine
2811 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2812 * Returns: Pointer to the allocated WPA state machine data
2814 * This function is used to allocate a new WPA state machine and the returned
2815 * value is passed to all WPA state machine calls.
2817 struct wpa_sm
* wpa_sm_init(struct wpa_sm_ctx
*ctx
)
2821 sm
= os_zalloc(sizeof(*sm
));
2824 dl_list_init(&sm
->pmksa_candidates
);
2825 sm
->renew_snonce
= 1;
2828 sm
->dot11RSNAConfigPMKLifetime
= 43200;
2829 sm
->dot11RSNAConfigPMKReauthThreshold
= 70;
2830 sm
->dot11RSNAConfigSATimeout
= 60;
2832 sm
->pmksa
= pmksa_cache_init(wpa_sm_pmksa_free_cb
, sm
, sm
);
2833 if (sm
->pmksa
== NULL
) {
2834 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
2835 "RSN: PMKSA cache initialization failed");
2845 * wpa_sm_deinit - Deinitialize WPA state machine
2846 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2848 void wpa_sm_deinit(struct wpa_sm
*sm
)
2852 pmksa_cache_deinit(sm
->pmksa
);
2853 eloop_cancel_timeout(wpa_sm_start_preauth
, sm
, NULL
);
2854 eloop_cancel_timeout(wpa_sm_rekey_ptk
, sm
, NULL
);
2855 os_free(sm
->assoc_wpa_ie
);
2856 os_free(sm
->assoc_rsnxe
);
2857 os_free(sm
->ap_wpa_ie
);
2858 os_free(sm
->ap_rsn_ie
);
2859 os_free(sm
->ap_rsnxe
);
2862 #ifdef CONFIG_IEEE80211R
2863 os_free(sm
->assoc_resp_ies
);
2864 #endif /* CONFIG_IEEE80211R */
2865 #ifdef CONFIG_TESTING_OPTIONS
2866 wpabuf_free(sm
->test_assoc_ie
);
2867 #endif /* CONFIG_TESTING_OPTIONS */
2868 #ifdef CONFIG_FILS_SK_PFS
2869 crypto_ecdh_deinit(sm
->fils_ecdh
);
2870 #endif /* CONFIG_FILS_SK_PFS */
2872 wpabuf_free(sm
->fils_ft_ies
);
2873 #endif /* CONFIG_FILS */
2875 crypto_ecdh_deinit(sm
->owe_ecdh
);
2876 #endif /* CONFIG_OWE */
2878 wpabuf_clear_free(sm
->dpp_z
);
2879 #endif /* CONFIG_DPP2 */
2885 * wpa_sm_notify_assoc - Notify WPA state machine about association
2886 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2887 * @bssid: The BSSID of the new association
2889 * This function is called to let WPA state machine know that the connection
2892 void wpa_sm_notify_assoc(struct wpa_sm
*sm
, const u8
*bssid
)
2899 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2900 "WPA: Association event - clear replay counter");
2901 os_memcpy(sm
->bssid
, bssid
, ETH_ALEN
);
2902 os_memset(sm
->rx_replay_counter
, 0, WPA_REPLAY_COUNTER_LEN
);
2903 sm
->rx_replay_counter_set
= 0;
2904 sm
->renew_snonce
= 1;
2905 if (os_memcmp(sm
->preauth_bssid
, bssid
, ETH_ALEN
) == 0)
2906 rsn_preauth_deinit(sm
);
2908 #ifdef CONFIG_IEEE80211R
2909 if (wpa_ft_is_completed(sm
)) {
2911 * Clear portValid to kick EAPOL state machine to re-enter
2912 * AUTHENTICATED state to get the EAPOL port Authorized.
2914 eapol_sm_notify_portValid(sm
->eapol
, FALSE
);
2915 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
, 1);
2917 /* Prepare for the next transition */
2918 wpa_ft_prepare_auth_request(sm
, NULL
);
2921 sm
->ft_protocol
= 1;
2923 sm
->ft_protocol
= 0;
2925 #endif /* CONFIG_IEEE80211R */
2927 if (sm
->fils_completed
) {
2929 * Clear portValid to kick EAPOL state machine to re-enter
2930 * AUTHENTICATED state to get the EAPOL port Authorized.
2932 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
, 1);
2935 #endif /* CONFIG_FILS */
2939 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2940 * this is not part of a Fast BSS Transition.
2942 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Clear old PTK");
2944 os_memset(&sm
->ptk
, 0, sizeof(sm
->ptk
));
2946 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
2947 os_memset(&sm
->gtk
, 0, sizeof(sm
->gtk
));
2948 os_memset(&sm
->gtk_wnm_sleep
, 0, sizeof(sm
->gtk_wnm_sleep
));
2949 os_memset(&sm
->igtk
, 0, sizeof(sm
->igtk
));
2950 os_memset(&sm
->igtk_wnm_sleep
, 0, sizeof(sm
->igtk_wnm_sleep
));
2955 #endif /* CONFIG_TDLS */
2958 os_memset(sm
->p2p_ip_addr
, 0, sizeof(sm
->p2p_ip_addr
));
2959 #endif /* CONFIG_P2P */
2961 sm
->keyidx_active
= 0;
2966 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2967 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2969 * This function is called to let WPA state machine know that the connection
2970 * was lost. This will abort any existing pre-authentication session.
2972 void wpa_sm_notify_disassoc(struct wpa_sm
*sm
)
2974 eloop_cancel_timeout(wpa_sm_start_preauth
, sm
, NULL
);
2975 eloop_cancel_timeout(wpa_sm_rekey_ptk
, sm
, NULL
);
2976 rsn_preauth_deinit(sm
);
2977 pmksa_cache_clear_current(sm
);
2978 if (wpa_sm_get_state(sm
) == WPA_4WAY_HANDSHAKE
)
2979 sm
->dot11RSNA4WayHandshakeFailures
++;
2981 wpa_tdls_disassoc(sm
);
2982 #endif /* CONFIG_TDLS */
2984 sm
->fils_completed
= 0;
2985 #endif /* CONFIG_FILS */
2986 #ifdef CONFIG_IEEE80211R
2987 sm
->ft_reassoc_completed
= 0;
2988 sm
->ft_protocol
= 0;
2989 #endif /* CONFIG_IEEE80211R */
2991 /* Keys are not needed in the WPA state machine anymore */
2993 sm
->keyidx_active
= 0;
2995 sm
->msg_3_of_4_ok
= 0;
2996 os_memset(sm
->bssid
, 0, ETH_ALEN
);
3001 * wpa_sm_set_pmk - Set PMK
3002 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3004 * @pmk_len: The length of the new PMK in bytes
3005 * @pmkid: Calculated PMKID
3006 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
3008 * Configure the PMK for WPA state machine.
3010 void wpa_sm_set_pmk(struct wpa_sm
*sm
, const u8
*pmk
, size_t pmk_len
,
3011 const u8
*pmkid
, const u8
*bssid
)
3016 wpa_hexdump_key(MSG_DEBUG
, "WPA: Set PMK based on external data",
3018 sm
->pmk_len
= pmk_len
;
3019 os_memcpy(sm
->pmk
, pmk
, pmk_len
);
3021 #ifdef CONFIG_IEEE80211R
3022 /* Set XXKey to be PSK for FT key derivation */
3023 sm
->xxkey_len
= pmk_len
;
3024 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
3025 #endif /* CONFIG_IEEE80211R */
3028 pmksa_cache_add(sm
->pmksa
, pmk
, pmk_len
, pmkid
, NULL
, 0,
3029 bssid
, sm
->own_addr
,
3030 sm
->network_ctx
, sm
->key_mgmt
, NULL
);
3036 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
3037 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3039 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
3042 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm
*sm
)
3047 if (sm
->cur_pmksa
) {
3048 wpa_hexdump_key(MSG_DEBUG
,
3049 "WPA: Set PMK based on current PMKSA",
3050 sm
->cur_pmksa
->pmk
, sm
->cur_pmksa
->pmk_len
);
3051 sm
->pmk_len
= sm
->cur_pmksa
->pmk_len
;
3052 os_memcpy(sm
->pmk
, sm
->cur_pmksa
->pmk
, sm
->pmk_len
);
3054 wpa_printf(MSG_DEBUG
, "WPA: No current PMKSA - clear PMK");
3056 os_memset(sm
->pmk
, 0, PMK_LEN_MAX
);
3062 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
3063 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3064 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
3066 void wpa_sm_set_fast_reauth(struct wpa_sm
*sm
, int fast_reauth
)
3069 sm
->fast_reauth
= fast_reauth
;
3074 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
3075 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3076 * @scard_ctx: Context pointer for smartcard related callback functions
3078 void wpa_sm_set_scard_ctx(struct wpa_sm
*sm
, void *scard_ctx
)
3082 sm
->scard_ctx
= scard_ctx
;
3083 if (sm
->preauth_eapol
)
3084 eapol_sm_register_scard_ctx(sm
->preauth_eapol
, scard_ctx
);
3089 * wpa_sm_set_config - Notification of current configuration change
3090 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3091 * @config: Pointer to current network configuration
3093 * Notify WPA state machine that configuration has changed. config will be
3094 * stored as a backpointer to network configuration. This can be %NULL to clear
3095 * the stored pointed.
3097 void wpa_sm_set_config(struct wpa_sm
*sm
, struct rsn_supp_config
*config
)
3103 sm
->network_ctx
= config
->network_ctx
;
3104 sm
->allowed_pairwise_cipher
= config
->allowed_pairwise_cipher
;
3105 sm
->proactive_key_caching
= config
->proactive_key_caching
;
3106 sm
->eap_workaround
= config
->eap_workaround
;
3107 sm
->eap_conf_ctx
= config
->eap_conf_ctx
;
3109 os_memcpy(sm
->ssid
, config
->ssid
, config
->ssid_len
);
3110 sm
->ssid_len
= config
->ssid_len
;
3113 sm
->wpa_ptk_rekey
= config
->wpa_ptk_rekey
;
3114 sm
->p2p
= config
->p2p
;
3115 sm
->wpa_rsc_relaxation
= config
->wpa_rsc_relaxation
;
3116 sm
->owe_ptk_workaround
= config
->owe_ptk_workaround
;
3118 if (config
->fils_cache_id
) {
3119 sm
->fils_cache_id_set
= 1;
3120 os_memcpy(sm
->fils_cache_id
, config
->fils_cache_id
,
3123 sm
->fils_cache_id_set
= 0;
3125 #endif /* CONFIG_FILS */
3126 sm
->beacon_prot
= config
->beacon_prot
;
3128 sm
->network_ctx
= NULL
;
3129 sm
->allowed_pairwise_cipher
= 0;
3130 sm
->proactive_key_caching
= 0;
3131 sm
->eap_workaround
= 0;
3132 sm
->eap_conf_ctx
= NULL
;
3134 sm
->wpa_ptk_rekey
= 0;
3136 sm
->wpa_rsc_relaxation
= 0;
3137 sm
->owe_ptk_workaround
= 0;
3138 sm
->beacon_prot
= 0;
3144 * wpa_sm_set_own_addr - Set own MAC address
3145 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3146 * @addr: Own MAC address
3148 void wpa_sm_set_own_addr(struct wpa_sm
*sm
, const u8
*addr
)
3151 os_memcpy(sm
->own_addr
, addr
, ETH_ALEN
);
3156 * wpa_sm_set_ifname - Set network interface name
3157 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3158 * @ifname: Interface name
3159 * @bridge_ifname: Optional bridge interface name (for pre-auth)
3161 void wpa_sm_set_ifname(struct wpa_sm
*sm
, const char *ifname
,
3162 const char *bridge_ifname
)
3165 sm
->ifname
= ifname
;
3166 sm
->bridge_ifname
= bridge_ifname
;
3172 * wpa_sm_set_eapol - Set EAPOL state machine pointer
3173 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3174 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
3176 void wpa_sm_set_eapol(struct wpa_sm
*sm
, struct eapol_sm
*eapol
)
3184 * wpa_sm_set_param - Set WPA state machine parameters
3185 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3186 * @param: Parameter field
3187 * @value: Parameter value
3188 * Returns: 0 on success, -1 on failure
3190 int wpa_sm_set_param(struct wpa_sm
*sm
, enum wpa_sm_conf_params param
,
3199 case RSNA_PMK_LIFETIME
:
3201 sm
->dot11RSNAConfigPMKLifetime
= value
;
3205 case RSNA_PMK_REAUTH_THRESHOLD
:
3206 if (value
> 0 && value
<= 100)
3207 sm
->dot11RSNAConfigPMKReauthThreshold
= value
;
3211 case RSNA_SA_TIMEOUT
:
3213 sm
->dot11RSNAConfigSATimeout
= value
;
3217 case WPA_PARAM_PROTO
:
3220 case WPA_PARAM_PAIRWISE
:
3221 sm
->pairwise_cipher
= value
;
3223 case WPA_PARAM_GROUP
:
3224 sm
->group_cipher
= value
;
3226 case WPA_PARAM_KEY_MGMT
:
3227 sm
->key_mgmt
= value
;
3229 case WPA_PARAM_MGMT_GROUP
:
3230 sm
->mgmt_group_cipher
= value
;
3232 case WPA_PARAM_RSN_ENABLED
:
3233 sm
->rsn_enabled
= value
;
3241 case WPA_PARAM_SAE_PWE
:
3242 sm
->sae_pwe
= value
;
3244 case WPA_PARAM_DENY_PTK0_REKEY
:
3245 sm
->wpa_deny_ptk0_rekey
= value
;
3247 case WPA_PARAM_EXT_KEY_ID
:
3248 sm
->ext_key_id
= value
;
3250 case WPA_PARAM_USE_EXT_KEY_ID
:
3251 sm
->use_ext_key_id
= value
;
3262 * wpa_sm_get_status - Get WPA state machine
3263 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3264 * @buf: Buffer for status information
3265 * @buflen: Maximum buffer length
3266 * @verbose: Whether to include verbose status information
3267 * Returns: Number of bytes written to buf.
3269 * Query WPA state machine for status information. This function fills in
3270 * a text area with current status information. If the buffer (buf) is not
3271 * large enough, status information will be truncated to fit the buffer.
3273 int wpa_sm_get_status(struct wpa_sm
*sm
, char *buf
, size_t buflen
,
3276 char *pos
= buf
, *end
= buf
+ buflen
;
3279 ret
= os_snprintf(pos
, end
- pos
,
3280 "pairwise_cipher=%s\n"
3283 wpa_cipher_txt(sm
->pairwise_cipher
),
3284 wpa_cipher_txt(sm
->group_cipher
),
3285 wpa_key_mgmt_txt(sm
->key_mgmt
, sm
->proto
));
3286 if (os_snprintf_error(end
- pos
, ret
))
3290 if (sm
->mfp
!= NO_MGMT_FRAME_PROTECTION
&& sm
->ap_rsn_ie
) {
3291 struct wpa_ie_data rsn
;
3292 if (wpa_parse_wpa_ie_rsn(sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
, &rsn
)
3294 rsn
.capabilities
& (WPA_CAPABILITY_MFPR
|
3295 WPA_CAPABILITY_MFPC
)) {
3296 ret
= os_snprintf(pos
, end
- pos
, "pmf=%d\n"
3297 "mgmt_group_cipher=%s\n",
3299 WPA_CAPABILITY_MFPR
) ? 2 : 1,
3301 sm
->mgmt_group_cipher
));
3302 if (os_snprintf_error(end
- pos
, ret
))
3312 int wpa_sm_pmf_enabled(struct wpa_sm
*sm
)
3314 struct wpa_ie_data rsn
;
3316 if (sm
->mfp
== NO_MGMT_FRAME_PROTECTION
|| !sm
->ap_rsn_ie
)
3319 if (wpa_parse_wpa_ie_rsn(sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
, &rsn
) >= 0 &&
3320 rsn
.capabilities
& (WPA_CAPABILITY_MFPR
| WPA_CAPABILITY_MFPC
))
3327 int wpa_sm_ext_key_id(struct wpa_sm
*sm
)
3329 return sm
? sm
->ext_key_id
: 0;
3333 int wpa_sm_ext_key_id_active(struct wpa_sm
*sm
)
3335 return sm
? sm
->use_ext_key_id
: 0;
3339 int wpa_sm_ocv_enabled(struct wpa_sm
*sm
)
3341 struct wpa_ie_data rsn
;
3343 if (!sm
->ocv
|| !sm
->ap_rsn_ie
)
3346 return wpa_parse_wpa_ie_rsn(sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
,
3348 (rsn
.capabilities
& WPA_CAPABILITY_OCVC
);
3353 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3354 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3355 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3356 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3357 * Returns: 0 on success, -1 on failure
3359 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm
*sm
, u8
*wpa_ie
,
3367 #ifdef CONFIG_TESTING_OPTIONS
3368 if (sm
->test_assoc_ie
) {
3369 wpa_printf(MSG_DEBUG
,
3370 "TESTING: Replace association WPA/RSN IE");
3371 if (*wpa_ie_len
< wpabuf_len(sm
->test_assoc_ie
))
3373 os_memcpy(wpa_ie
, wpabuf_head(sm
->test_assoc_ie
),
3374 wpabuf_len(sm
->test_assoc_ie
));
3375 res
= wpabuf_len(sm
->test_assoc_ie
);
3377 #endif /* CONFIG_TESTING_OPTIONS */
3378 res
= wpa_gen_wpa_ie(sm
, wpa_ie
, *wpa_ie_len
);
3383 wpa_hexdump(MSG_DEBUG
, "WPA: Set own WPA IE default",
3384 wpa_ie
, *wpa_ie_len
);
3386 if (sm
->assoc_wpa_ie
== NULL
) {
3388 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3389 * the correct version of the IE even if PMKSA caching is
3390 * aborted (which would remove PMKID from IE generation).
3392 sm
->assoc_wpa_ie
= os_memdup(wpa_ie
, *wpa_ie_len
);
3393 if (sm
->assoc_wpa_ie
== NULL
)
3396 sm
->assoc_wpa_ie_len
= *wpa_ie_len
;
3398 wpa_hexdump(MSG_DEBUG
,
3399 "WPA: Leave previously set WPA IE default",
3400 sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
);
3408 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3409 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3410 * @ie: Pointer to IE data (starting from id)
3412 * Returns: 0 on success, -1 on failure
3414 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3415 * Request frame. The IE will be used to override the default value generated
3416 * with wpa_sm_set_assoc_wpa_ie_default().
3418 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
3423 os_free(sm
->assoc_wpa_ie
);
3424 if (ie
== NULL
|| len
== 0) {
3425 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3426 "WPA: clearing own WPA/RSN IE");
3427 sm
->assoc_wpa_ie
= NULL
;
3428 sm
->assoc_wpa_ie_len
= 0;
3430 wpa_hexdump(MSG_DEBUG
, "WPA: set own WPA/RSN IE", ie
, len
);
3431 sm
->assoc_wpa_ie
= os_memdup(ie
, len
);
3432 if (sm
->assoc_wpa_ie
== NULL
)
3435 sm
->assoc_wpa_ie_len
= len
;
3443 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
3444 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3445 * @rsnxe: Pointer to buffer for RSNXE
3446 * @rsnxe_len: Pointer to the length of the rsne buffer
3447 * Returns: 0 on success, -1 on failure
3449 int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm
*sm
, u8
*rsnxe
,
3457 res
= wpa_gen_rsnxe(sm
, rsnxe
, *rsnxe_len
);
3462 wpa_hexdump(MSG_DEBUG
, "RSN: Set own RSNXE default", rsnxe
, *rsnxe_len
);
3464 if (sm
->assoc_rsnxe
) {
3465 wpa_hexdump(MSG_DEBUG
,
3466 "RSN: Leave previously set RSNXE default",
3467 sm
->assoc_rsnxe
, sm
->assoc_rsnxe_len
);
3468 } else if (*rsnxe_len
> 0) {
3470 * Make a copy of the RSNXE so that 4-Way Handshake gets the
3471 * correct version of the IE even if it gets changed.
3473 sm
->assoc_rsnxe
= os_memdup(rsnxe
, *rsnxe_len
);
3474 if (!sm
->assoc_rsnxe
)
3477 sm
->assoc_rsnxe_len
= *rsnxe_len
;
3485 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
3486 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3487 * @ie: Pointer to IE data (starting from id)
3489 * Returns: 0 on success, -1 on failure
3491 * Inform WPA state machine about the RSNXE used in (Re)Association Request
3492 * frame. The IE will be used to override the default value generated
3493 * with wpa_sm_set_assoc_rsnxe_default().
3495 int wpa_sm_set_assoc_rsnxe(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
3500 os_free(sm
->assoc_rsnxe
);
3501 if (!ie
|| len
== 0) {
3502 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3503 "RSN: clearing own RSNXE");
3504 sm
->assoc_rsnxe
= NULL
;
3505 sm
->assoc_rsnxe_len
= 0;
3507 wpa_hexdump(MSG_DEBUG
, "RSN: set own RSNXE", ie
, len
);
3508 sm
->assoc_rsnxe
= os_memdup(ie
, len
);
3509 if (!sm
->assoc_rsnxe
)
3512 sm
->assoc_rsnxe_len
= len
;
3520 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3521 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3522 * @ie: Pointer to IE data (starting from id)
3524 * Returns: 0 on success, -1 on failure
3526 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3529 int wpa_sm_set_ap_wpa_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
3534 os_free(sm
->ap_wpa_ie
);
3535 if (ie
== NULL
|| len
== 0) {
3536 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3537 "WPA: clearing AP WPA IE");
3538 sm
->ap_wpa_ie
= NULL
;
3539 sm
->ap_wpa_ie_len
= 0;
3541 wpa_hexdump(MSG_DEBUG
, "WPA: set AP WPA IE", ie
, len
);
3542 sm
->ap_wpa_ie
= os_memdup(ie
, len
);
3543 if (sm
->ap_wpa_ie
== NULL
)
3546 sm
->ap_wpa_ie_len
= len
;
3554 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3555 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3556 * @ie: Pointer to IE data (starting from id)
3558 * Returns: 0 on success, -1 on failure
3560 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3563 int wpa_sm_set_ap_rsn_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
3568 os_free(sm
->ap_rsn_ie
);
3569 if (ie
== NULL
|| len
== 0) {
3570 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3571 "WPA: clearing AP RSN IE");
3572 sm
->ap_rsn_ie
= NULL
;
3573 sm
->ap_rsn_ie_len
= 0;
3575 wpa_hexdump(MSG_DEBUG
, "WPA: set AP RSN IE", ie
, len
);
3576 sm
->ap_rsn_ie
= os_memdup(ie
, len
);
3577 if (sm
->ap_rsn_ie
== NULL
)
3580 sm
->ap_rsn_ie_len
= len
;
3588 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
3589 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3590 * @ie: Pointer to IE data (starting from id)
3592 * Returns: 0 on success, -1 on failure
3594 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
3597 int wpa_sm_set_ap_rsnxe(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
3602 os_free(sm
->ap_rsnxe
);
3603 if (!ie
|| len
== 0) {
3604 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: clearing AP RSNXE");
3605 sm
->ap_rsnxe
= NULL
;
3606 sm
->ap_rsnxe_len
= 0;
3608 wpa_hexdump(MSG_DEBUG
, "WPA: set AP RSNXE", ie
, len
);
3609 sm
->ap_rsnxe
= os_memdup(ie
, len
);
3613 sm
->ap_rsnxe_len
= len
;
3621 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3622 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3623 * @data: Pointer to data area for parsing results
3624 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3626 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3627 * parsed data into data.
3629 int wpa_sm_parse_own_wpa_ie(struct wpa_sm
*sm
, struct wpa_ie_data
*data
)
3634 if (sm
->assoc_wpa_ie
== NULL
) {
3635 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3636 "WPA: No WPA/RSN IE available from association info");
3639 if (wpa_parse_wpa_ie(sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
, data
))
3645 int wpa_sm_pmksa_cache_list(struct wpa_sm
*sm
, char *buf
, size_t len
)
3647 return pmksa_cache_list(sm
->pmksa
, buf
, len
);
3651 struct rsn_pmksa_cache_entry
* wpa_sm_pmksa_cache_head(struct wpa_sm
*sm
)
3653 return pmksa_cache_head(sm
->pmksa
);
3657 struct rsn_pmksa_cache_entry
*
3658 wpa_sm_pmksa_cache_add_entry(struct wpa_sm
*sm
,
3659 struct rsn_pmksa_cache_entry
* entry
)
3661 return pmksa_cache_add_entry(sm
->pmksa
, entry
);
3665 void wpa_sm_pmksa_cache_add(struct wpa_sm
*sm
, const u8
*pmk
, size_t pmk_len
,
3666 const u8
*pmkid
, const u8
*bssid
,
3667 const u8
*fils_cache_id
)
3669 sm
->cur_pmksa
= pmksa_cache_add(sm
->pmksa
, pmk
, pmk_len
, pmkid
, NULL
, 0,
3670 bssid
, sm
->own_addr
, sm
->network_ctx
,
3671 sm
->key_mgmt
, fils_cache_id
);
3675 int wpa_sm_pmksa_exists(struct wpa_sm
*sm
, const u8
*bssid
,
3676 const void *network_ctx
)
3678 return pmksa_cache_get(sm
->pmksa
, bssid
, NULL
, network_ctx
, 0) != NULL
;
3682 void wpa_sm_drop_sa(struct wpa_sm
*sm
)
3684 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Clear old PMK and PTK");
3688 os_memset(sm
->pmk
, 0, sizeof(sm
->pmk
));
3689 os_memset(&sm
->ptk
, 0, sizeof(sm
->ptk
));
3690 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
3691 os_memset(&sm
->gtk
, 0, sizeof(sm
->gtk
));
3692 os_memset(&sm
->gtk_wnm_sleep
, 0, sizeof(sm
->gtk_wnm_sleep
));
3693 os_memset(&sm
->igtk
, 0, sizeof(sm
->igtk
));
3694 os_memset(&sm
->igtk_wnm_sleep
, 0, sizeof(sm
->igtk_wnm_sleep
));
3695 #ifdef CONFIG_IEEE80211R
3696 os_memset(sm
->xxkey
, 0, sizeof(sm
->xxkey
));
3698 os_memset(sm
->pmk_r0
, 0, sizeof(sm
->pmk_r0
));
3700 os_memset(sm
->pmk_r1
, 0, sizeof(sm
->pmk_r1
));
3702 #endif /* CONFIG_IEEE80211R */
3706 int wpa_sm_has_ptk(struct wpa_sm
*sm
)
3714 int wpa_sm_has_ptk_installed(struct wpa_sm
*sm
)
3718 return sm
->ptk
.installed
;
3722 void wpa_sm_update_replay_ctr(struct wpa_sm
*sm
, const u8
*replay_ctr
)
3724 os_memcpy(sm
->rx_replay_counter
, replay_ctr
, WPA_REPLAY_COUNTER_LEN
);
3728 void wpa_sm_pmksa_cache_flush(struct wpa_sm
*sm
, void *network_ctx
)
3730 pmksa_cache_flush(sm
->pmksa
, network_ctx
, NULL
, 0);
3735 int wpa_wnmsleep_install_key(struct wpa_sm
*sm
, u8 subelem_id
, u8
*buf
)
3738 u8 keylen
; /* plaintext key len */
3741 if (subelem_id
== WNM_SLEEP_SUBELEM_GTK
) {
3742 struct wpa_gtk_data gd
;
3744 os_memset(&gd
, 0, sizeof(gd
));
3745 keylen
= wpa_cipher_key_len(sm
->group_cipher
);
3746 gd
.key_rsc_len
= wpa_cipher_rsc_len(sm
->group_cipher
);
3747 gd
.alg
= wpa_cipher_to_alg(sm
->group_cipher
);
3748 if (gd
.alg
== WPA_ALG_NONE
) {
3749 wpa_printf(MSG_DEBUG
, "Unsupported group cipher suite");
3754 keyinfo
= WPA_GET_LE16(buf
+ 2);
3755 gd
.gtk_len
= keylen
;
3756 if (gd
.gtk_len
!= buf
[4]) {
3757 wpa_printf(MSG_DEBUG
, "GTK len mismatch len %d vs %d",
3758 gd
.gtk_len
, buf
[4]);
3761 gd
.keyidx
= keyinfo
& 0x03; /* B0 - B1 */
3762 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(
3763 sm
, !!(keyinfo
& WPA_KEY_INFO_TXRX
));
3765 os_memcpy(gd
.gtk
, buf
+ 13, gd
.gtk_len
);
3767 wpa_hexdump_key(MSG_DEBUG
, "Install GTK (WNM SLEEP)",
3768 gd
.gtk
, gd
.gtk_len
);
3769 if (wpa_supplicant_install_gtk(sm
, &gd
, key_rsc
, 1)) {
3770 forced_memzero(&gd
, sizeof(gd
));
3771 wpa_printf(MSG_DEBUG
, "Failed to install the GTK in "
3775 forced_memzero(&gd
, sizeof(gd
));
3776 } else if (subelem_id
== WNM_SLEEP_SUBELEM_IGTK
) {
3777 const struct wpa_igtk_kde
*igtk
;
3779 igtk
= (const struct wpa_igtk_kde
*) (buf
+ 2);
3780 if (wpa_supplicant_install_igtk(sm
, igtk
, 1) < 0)
3782 } else if (subelem_id
== WNM_SLEEP_SUBELEM_BIGTK
) {
3783 const struct wpa_bigtk_kde
*bigtk
;
3785 bigtk
= (const struct wpa_bigtk_kde
*) (buf
+ 2);
3786 if (sm
->beacon_prot
&&
3787 wpa_supplicant_install_bigtk(sm
, bigtk
, 1) < 0)
3790 wpa_printf(MSG_DEBUG
, "Unknown element id");
3796 #endif /* CONFIG_WNM */
3801 int wpa_sm_get_p2p_ip_addr(struct wpa_sm
*sm
, u8
*buf
)
3803 if (sm
== NULL
|| WPA_GET_BE32(sm
->p2p_ip_addr
) == 0)
3805 os_memcpy(buf
, sm
->p2p_ip_addr
, 3 * 4);
3809 #endif /* CONFIG_P2P */
3812 void wpa_sm_set_rx_replay_ctr(struct wpa_sm
*sm
, const u8
*rx_replay_counter
)
3814 if (rx_replay_counter
== NULL
)
3817 os_memcpy(sm
->rx_replay_counter
, rx_replay_counter
,
3818 WPA_REPLAY_COUNTER_LEN
);
3819 sm
->rx_replay_counter_set
= 1;
3820 wpa_printf(MSG_DEBUG
, "Updated key replay counter");
3824 void wpa_sm_set_ptk_kck_kek(struct wpa_sm
*sm
,
3825 const u8
*ptk_kck
, size_t ptk_kck_len
,
3826 const u8
*ptk_kek
, size_t ptk_kek_len
)
3828 if (ptk_kck
&& ptk_kck_len
<= WPA_KCK_MAX_LEN
) {
3829 os_memcpy(sm
->ptk
.kck
, ptk_kck
, ptk_kck_len
);
3830 sm
->ptk
.kck_len
= ptk_kck_len
;
3831 wpa_printf(MSG_DEBUG
, "Updated PTK KCK");
3833 if (ptk_kek
&& ptk_kek_len
<= WPA_KEK_MAX_LEN
) {
3834 os_memcpy(sm
->ptk
.kek
, ptk_kek
, ptk_kek_len
);
3835 sm
->ptk
.kek_len
= ptk_kek_len
;
3836 wpa_printf(MSG_DEBUG
, "Updated PTK KEK");
3842 #ifdef CONFIG_TESTING_OPTIONS
3844 void wpa_sm_set_test_assoc_ie(struct wpa_sm
*sm
, struct wpabuf
*buf
)
3846 wpabuf_free(sm
->test_assoc_ie
);
3847 sm
->test_assoc_ie
= buf
;
3851 const u8
* wpa_sm_get_anonce(struct wpa_sm
*sm
)
3856 #endif /* CONFIG_TESTING_OPTIONS */
3859 unsigned int wpa_sm_get_key_mgmt(struct wpa_sm
*sm
)
3861 return sm
->key_mgmt
;
3867 struct wpabuf
* fils_build_auth(struct wpa_sm
*sm
, int dh_group
, const u8
*md
)
3869 struct wpabuf
*buf
= NULL
;
3870 struct wpabuf
*erp_msg
;
3871 struct wpabuf
*pub
= NULL
;
3873 erp_msg
= eapol_sm_build_erp_reauth_start(sm
->eapol
);
3874 if (!erp_msg
&& !sm
->cur_pmksa
) {
3875 wpa_printf(MSG_DEBUG
,
3876 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3880 wpa_printf(MSG_DEBUG
, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3881 erp_msg
!= NULL
, sm
->cur_pmksa
!= NULL
);
3883 sm
->fils_completed
= 0;
3885 if (!sm
->assoc_wpa_ie
) {
3886 wpa_printf(MSG_INFO
, "FILS: No own RSN IE set for FILS");
3890 if (random_get_bytes(sm
->fils_nonce
, FILS_NONCE_LEN
) < 0 ||
3891 random_get_bytes(sm
->fils_session
, FILS_SESSION_LEN
) < 0)
3894 wpa_hexdump(MSG_DEBUG
, "FILS: Generated FILS Nonce",
3895 sm
->fils_nonce
, FILS_NONCE_LEN
);
3896 wpa_hexdump(MSG_DEBUG
, "FILS: Generated FILS Session",
3897 sm
->fils_session
, FILS_SESSION_LEN
);
3899 #ifdef CONFIG_FILS_SK_PFS
3900 sm
->fils_dh_group
= dh_group
;
3902 crypto_ecdh_deinit(sm
->fils_ecdh
);
3903 sm
->fils_ecdh
= crypto_ecdh_init(dh_group
);
3904 if (!sm
->fils_ecdh
) {
3905 wpa_printf(MSG_INFO
,
3906 "FILS: Could not initialize ECDH with group %d",
3910 pub
= crypto_ecdh_get_pubkey(sm
->fils_ecdh
, 1);
3913 wpa_hexdump_buf(MSG_DEBUG
, "FILS: Element (DH public key)",
3915 sm
->fils_dh_elem_len
= wpabuf_len(pub
);
3917 #endif /* CONFIG_FILS_SK_PFS */
3919 buf
= wpabuf_alloc(1000 + sm
->assoc_wpa_ie_len
+
3920 (pub
? wpabuf_len(pub
) : 0));
3924 /* Fields following the Authentication algorithm number field */
3926 /* Authentication Transaction seq# */
3927 wpabuf_put_le16(buf
, 1);
3930 wpabuf_put_le16(buf
, WLAN_STATUS_SUCCESS
);
3933 #ifdef CONFIG_FILS_SK_PFS
3935 /* Finite Cyclic Group */
3936 wpabuf_put_le16(buf
, dh_group
);
3938 wpabuf_put_buf(buf
, pub
);
3940 #endif /* CONFIG_FILS_SK_PFS */
3943 wpa_hexdump(MSG_DEBUG
, "FILS: RSNE in FILS Authentication frame",
3944 sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
);
3945 wpabuf_put_data(buf
, sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
);
3948 /* MDE when using FILS for FT initial association */
3949 struct rsn_mdie
*mdie
;
3951 wpabuf_put_u8(buf
, WLAN_EID_MOBILITY_DOMAIN
);
3952 wpabuf_put_u8(buf
, sizeof(*mdie
));
3953 mdie
= wpabuf_put(buf
, sizeof(*mdie
));
3954 os_memcpy(mdie
->mobility_domain
, md
, MOBILITY_DOMAIN_ID_LEN
);
3959 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3960 wpabuf_put_u8(buf
, 1 + FILS_NONCE_LEN
); /* Length */
3961 /* Element ID Extension */
3962 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_NONCE
);
3963 wpabuf_put_data(buf
, sm
->fils_nonce
, FILS_NONCE_LEN
);
3966 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3967 wpabuf_put_u8(buf
, 1 + FILS_SESSION_LEN
); /* Length */
3968 /* Element ID Extension */
3969 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_SESSION
);
3970 wpabuf_put_data(buf
, sm
->fils_session
, FILS_SESSION_LEN
);
3973 sm
->fils_erp_pmkid_set
= 0;
3975 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3976 wpabuf_put_u8(buf
, 1 + wpabuf_len(erp_msg
)); /* Length */
3977 /* Element ID Extension */
3978 wpabuf_put_u8(buf
, WLAN_EID_EXT_WRAPPED_DATA
);
3979 wpabuf_put_buf(buf
, erp_msg
);
3980 /* Calculate pending PMKID here so that we do not need to
3981 * maintain a copy of the EAP-Initiate/Reauth message. */
3982 if (fils_pmkid_erp(sm
->key_mgmt
, wpabuf_head(erp_msg
),
3983 wpabuf_len(erp_msg
),
3984 sm
->fils_erp_pmkid
) == 0)
3985 sm
->fils_erp_pmkid_set
= 1;
3988 wpa_hexdump_buf(MSG_DEBUG
, "RSN: FILS fields for Authentication frame",
3992 wpabuf_free(erp_msg
);
3998 int fils_process_auth(struct wpa_sm
*sm
, const u8
*bssid
, const u8
*data
,
4001 const u8
*pos
, *end
;
4002 struct ieee802_11_elems elems
;
4003 struct wpa_ie_data rsn
;
4004 int pmkid_match
= 0;
4005 u8 ick
[FILS_ICK_MAX_LEN
];
4008 struct wpabuf
*dh_ss
= NULL
;
4009 const u8
*g_sta
= NULL
;
4010 size_t g_sta_len
= 0;
4011 const u8
*g_ap
= NULL
;
4012 size_t g_ap_len
= 0;
4013 struct wpabuf
*pub
= NULL
;
4015 os_memcpy(sm
->bssid
, bssid
, ETH_ALEN
);
4017 wpa_hexdump(MSG_DEBUG
, "FILS: Authentication frame fields",
4023 #ifdef CONFIG_FILS_SK_PFS
4024 if (sm
->fils_dh_group
) {
4027 /* Using FILS PFS */
4029 /* Finite Cyclic Group */
4030 if (end
- pos
< 2) {
4031 wpa_printf(MSG_DEBUG
,
4032 "FILS: No room for Finite Cyclic Group");
4035 group
= WPA_GET_LE16(pos
);
4037 if (group
!= sm
->fils_dh_group
) {
4038 wpa_printf(MSG_DEBUG
,
4039 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
4040 group
, sm
->fils_dh_group
);
4045 if ((size_t) (end
- pos
) < sm
->fils_dh_elem_len
) {
4046 wpa_printf(MSG_DEBUG
, "FILS: No room for Element");
4050 if (!sm
->fils_ecdh
) {
4051 wpa_printf(MSG_DEBUG
, "FILS: No ECDH state available");
4054 dh_ss
= crypto_ecdh_set_peerkey(sm
->fils_ecdh
, 1, pos
,
4055 sm
->fils_dh_elem_len
);
4057 wpa_printf(MSG_DEBUG
, "FILS: ECDH operation failed");
4060 wpa_hexdump_buf_key(MSG_DEBUG
, "FILS: DH_SS", dh_ss
);
4062 g_ap_len
= sm
->fils_dh_elem_len
;
4063 pos
+= sm
->fils_dh_elem_len
;
4065 #endif /* CONFIG_FILS_SK_PFS */
4067 wpa_hexdump(MSG_DEBUG
, "FILS: Remaining IEs", pos
, end
- pos
);
4068 if (ieee802_11_parse_elems(pos
, end
- pos
, &elems
, 1) == ParseFailed
) {
4069 wpa_printf(MSG_DEBUG
, "FILS: Could not parse elements");
4074 wpa_hexdump(MSG_DEBUG
, "FILS: RSN element", elems
.rsn_ie
,
4076 if (!elems
.rsn_ie
||
4077 wpa_parse_wpa_ie_rsn(elems
.rsn_ie
- 2, elems
.rsn_ie_len
+ 2,
4079 wpa_printf(MSG_DEBUG
, "FILS: No RSN element");
4083 if (!elems
.fils_nonce
) {
4084 wpa_printf(MSG_DEBUG
, "FILS: No FILS Nonce field");
4087 os_memcpy(sm
->fils_anonce
, elems
.fils_nonce
, FILS_NONCE_LEN
);
4088 wpa_hexdump(MSG_DEBUG
, "FILS: ANonce", sm
->fils_anonce
, FILS_NONCE_LEN
);
4090 #ifdef CONFIG_IEEE80211R
4091 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
4092 struct wpa_ft_ies parse
;
4094 if (!elems
.mdie
|| !elems
.ftie
) {
4095 wpa_printf(MSG_DEBUG
, "FILS+FT: No MDE or FTE");
4099 if (wpa_ft_parse_ies(pos
, end
- pos
, &parse
,
4100 wpa_key_mgmt_sha384(sm
->key_mgmt
)) < 0) {
4101 wpa_printf(MSG_DEBUG
, "FILS+FT: Failed to parse IEs");
4105 if (!parse
.r0kh_id
) {
4106 wpa_printf(MSG_DEBUG
,
4107 "FILS+FT: No R0KH-ID subelem in FTE");
4110 os_memcpy(sm
->r0kh_id
, parse
.r0kh_id
, parse
.r0kh_id_len
);
4111 sm
->r0kh_id_len
= parse
.r0kh_id_len
;
4112 wpa_hexdump_ascii(MSG_DEBUG
, "FILS+FT: R0KH-ID",
4113 sm
->r0kh_id
, sm
->r0kh_id_len
);
4115 if (!parse
.r1kh_id
) {
4116 wpa_printf(MSG_DEBUG
,
4117 "FILS+FT: No R1KH-ID subelem in FTE");
4120 os_memcpy(sm
->r1kh_id
, parse
.r1kh_id
, FT_R1KH_ID_LEN
);
4121 wpa_hexdump(MSG_DEBUG
, "FILS+FT: R1KH-ID",
4122 sm
->r1kh_id
, FT_R1KH_ID_LEN
);
4124 /* TODO: Check MDE and FTE payload */
4126 wpabuf_free(sm
->fils_ft_ies
);
4127 sm
->fils_ft_ies
= wpabuf_alloc(2 + elems
.mdie_len
+
4128 2 + elems
.ftie_len
);
4129 if (!sm
->fils_ft_ies
)
4131 wpabuf_put_data(sm
->fils_ft_ies
, elems
.mdie
- 2,
4132 2 + elems
.mdie_len
);
4133 wpabuf_put_data(sm
->fils_ft_ies
, elems
.ftie
- 2,
4134 2 + elems
.ftie_len
);
4136 wpabuf_free(sm
->fils_ft_ies
);
4137 sm
->fils_ft_ies
= NULL
;
4139 #endif /* CONFIG_IEEE80211R */
4142 if (rsn
.pmkid
&& rsn
.num_pmkid
> 0) {
4143 wpa_hexdump(MSG_DEBUG
, "FILS: PMKID List",
4144 rsn
.pmkid
, rsn
.num_pmkid
* PMKID_LEN
);
4146 if (rsn
.num_pmkid
!= 1) {
4147 wpa_printf(MSG_DEBUG
, "FILS: Invalid PMKID selection");
4150 wpa_hexdump(MSG_DEBUG
, "FILS: PMKID", rsn
.pmkid
, PMKID_LEN
);
4151 if (os_memcmp(sm
->cur_pmksa
->pmkid
, rsn
.pmkid
, PMKID_LEN
) != 0)
4153 wpa_printf(MSG_DEBUG
, "FILS: PMKID mismatch");
4154 wpa_hexdump(MSG_DEBUG
, "FILS: Expected PMKID",
4155 sm
->cur_pmksa
->pmkid
, PMKID_LEN
);
4158 wpa_printf(MSG_DEBUG
,
4159 "FILS: Matching PMKID - continue using PMKSA caching");
4162 if (!pmkid_match
&& sm
->cur_pmksa
) {
4163 wpa_printf(MSG_DEBUG
,
4164 "FILS: No PMKID match - cannot use cached PMKSA entry");
4165 sm
->cur_pmksa
= NULL
;
4169 if (!elems
.fils_session
) {
4170 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
4173 wpa_hexdump(MSG_DEBUG
, "FILS: FILS Session", elems
.fils_session
,
4175 if (os_memcmp(sm
->fils_session
, elems
.fils_session
, FILS_SESSION_LEN
)
4177 wpa_printf(MSG_DEBUG
, "FILS: Session mismatch");
4178 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
4179 sm
->fils_session
, FILS_SESSION_LEN
);
4184 if (!sm
->cur_pmksa
&& elems
.wrapped_data
) {
4185 u8 rmsk
[ERP_MAX_KEY_LEN
];
4188 wpa_hexdump(MSG_DEBUG
, "FILS: Wrapped Data",
4190 elems
.wrapped_data_len
);
4191 eapol_sm_process_erp_finish(sm
->eapol
, elems
.wrapped_data
,
4192 elems
.wrapped_data_len
);
4193 if (eapol_sm_failed(sm
->eapol
))
4196 rmsk_len
= ERP_MAX_KEY_LEN
;
4197 res
= eapol_sm_get_key(sm
->eapol
, rmsk
, rmsk_len
);
4198 if (res
== PMK_LEN
) {
4200 res
= eapol_sm_get_key(sm
->eapol
, rmsk
, rmsk_len
);
4205 res
= fils_rmsk_to_pmk(sm
->key_mgmt
, rmsk
, rmsk_len
,
4206 sm
->fils_nonce
, sm
->fils_anonce
,
4207 dh_ss
? wpabuf_head(dh_ss
) : NULL
,
4208 dh_ss
? wpabuf_len(dh_ss
) : 0,
4209 sm
->pmk
, &sm
->pmk_len
);
4210 forced_memzero(rmsk
, sizeof(rmsk
));
4212 /* Don't use DHss in PTK derivation if PMKSA caching is not
4214 wpabuf_clear_free(dh_ss
);
4220 if (!sm
->fils_erp_pmkid_set
) {
4221 wpa_printf(MSG_DEBUG
, "FILS: PMKID not available");
4224 wpa_hexdump(MSG_DEBUG
, "FILS: PMKID", sm
->fils_erp_pmkid
,
4226 wpa_printf(MSG_DEBUG
, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
4227 sm
->cur_pmksa
= pmksa_cache_add(sm
->pmksa
, sm
->pmk
, sm
->pmk_len
,
4228 sm
->fils_erp_pmkid
, NULL
, 0,
4229 sm
->bssid
, sm
->own_addr
,
4230 sm
->network_ctx
, sm
->key_mgmt
,
4234 if (!sm
->cur_pmksa
) {
4235 wpa_printf(MSG_DEBUG
,
4236 "FILS: No remaining options to continue FILS authentication");
4240 if (fils_pmk_to_ptk(sm
->pmk
, sm
->pmk_len
, sm
->own_addr
, sm
->bssid
,
4241 sm
->fils_nonce
, sm
->fils_anonce
,
4242 dh_ss
? wpabuf_head(dh_ss
) : NULL
,
4243 dh_ss
? wpabuf_len(dh_ss
) : 0,
4244 &sm
->ptk
, ick
, &ick_len
,
4245 sm
->key_mgmt
, sm
->pairwise_cipher
,
4246 sm
->fils_ft
, &sm
->fils_ft_len
) < 0) {
4247 wpa_printf(MSG_DEBUG
, "FILS: Failed to derive PTK");
4251 wpabuf_clear_free(dh_ss
);
4256 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
4258 #ifdef CONFIG_FILS_SK_PFS
4259 if (sm
->fils_dh_group
) {
4260 if (!sm
->fils_ecdh
) {
4261 wpa_printf(MSG_INFO
, "FILS: ECDH not initialized");
4264 pub
= crypto_ecdh_get_pubkey(sm
->fils_ecdh
, 1);
4267 wpa_hexdump_buf(MSG_DEBUG
, "FILS: gSTA", pub
);
4268 g_sta
= wpabuf_head(pub
);
4269 g_sta_len
= wpabuf_len(pub
);
4271 wpa_printf(MSG_INFO
, "FILS: gAP not available");
4274 wpa_hexdump(MSG_DEBUG
, "FILS: gAP", g_ap
, g_ap_len
);
4276 #endif /* CONFIG_FILS_SK_PFS */
4278 res
= fils_key_auth_sk(ick
, ick_len
, sm
->fils_nonce
,
4279 sm
->fils_anonce
, sm
->own_addr
, sm
->bssid
,
4280 g_sta
, g_sta_len
, g_ap
, g_ap_len
,
4281 sm
->key_mgmt
, sm
->fils_key_auth_sta
,
4282 sm
->fils_key_auth_ap
,
4283 &sm
->fils_key_auth_len
);
4285 forced_memzero(ick
, sizeof(ick
));
4289 wpabuf_clear_free(dh_ss
);
4294 #ifdef CONFIG_IEEE80211R
4295 static int fils_ft_build_assoc_req_rsne(struct wpa_sm
*sm
, struct wpabuf
*buf
)
4297 struct rsn_ie_hdr
*rsnie
;
4300 int use_sha384
= wpa_key_mgmt_sha384(sm
->key_mgmt
);
4302 /* RSNIE[PMKR0Name/PMKR1Name] */
4303 rsnie
= wpabuf_put(buf
, sizeof(*rsnie
));
4304 rsnie
->elem_id
= WLAN_EID_RSN
;
4305 WPA_PUT_LE16(rsnie
->version
, RSN_VERSION
);
4307 /* Group Suite Selector */
4308 if (!wpa_cipher_valid_group(sm
->group_cipher
)) {
4309 wpa_printf(MSG_WARNING
, "FT: Invalid group cipher (%d)",
4313 pos
= wpabuf_put(buf
, RSN_SELECTOR_LEN
);
4314 RSN_SELECTOR_PUT(pos
, wpa_cipher_to_suite(WPA_PROTO_RSN
,
4317 /* Pairwise Suite Count */
4318 wpabuf_put_le16(buf
, 1);
4320 /* Pairwise Suite List */
4321 if (!wpa_cipher_valid_pairwise(sm
->pairwise_cipher
)) {
4322 wpa_printf(MSG_WARNING
, "FT: Invalid pairwise cipher (%d)",
4323 sm
->pairwise_cipher
);
4326 pos
= wpabuf_put(buf
, RSN_SELECTOR_LEN
);
4327 RSN_SELECTOR_PUT(pos
, wpa_cipher_to_suite(WPA_PROTO_RSN
,
4328 sm
->pairwise_cipher
));
4330 /* Authenticated Key Management Suite Count */
4331 wpabuf_put_le16(buf
, 1);
4333 /* Authenticated Key Management Suite List */
4334 pos
= wpabuf_put(buf
, RSN_SELECTOR_LEN
);
4335 if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_FILS_SHA256
)
4336 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256
);
4337 else if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_FILS_SHA384
)
4338 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384
);
4340 wpa_printf(MSG_WARNING
,
4341 "FILS+FT: Invalid key management type (%d)",
4346 /* RSN Capabilities */
4349 capab
|= WPA_CAPABILITY_MFPC
;
4351 capab
|= WPA_CAPABILITY_MFPR
;
4353 capab
|= WPA_CAPABILITY_OCVC
;
4355 capab
|= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST
;
4356 wpabuf_put_le16(buf
, capab
);
4359 wpabuf_put_le16(buf
, 1);
4361 /* PMKID List [PMKR1Name] */
4362 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: XXKey (FILS-FT)",
4363 sm
->fils_ft
, sm
->fils_ft_len
);
4364 wpa_hexdump_ascii(MSG_DEBUG
, "FILS+FT: SSID", sm
->ssid
, sm
->ssid_len
);
4365 wpa_hexdump(MSG_DEBUG
, "FILS+FT: MDID",
4366 sm
->mobility_domain
, MOBILITY_DOMAIN_ID_LEN
);
4367 wpa_hexdump_ascii(MSG_DEBUG
, "FILS+FT: R0KH-ID",
4368 sm
->r0kh_id
, sm
->r0kh_id_len
);
4369 if (wpa_derive_pmk_r0(sm
->fils_ft
, sm
->fils_ft_len
, sm
->ssid
,
4370 sm
->ssid_len
, sm
->mobility_domain
,
4371 sm
->r0kh_id
, sm
->r0kh_id_len
, sm
->own_addr
,
4372 sm
->pmk_r0
, sm
->pmk_r0_name
, use_sha384
) < 0) {
4373 wpa_printf(MSG_WARNING
, "FILS+FT: Could not derive PMK-R0");
4376 sm
->pmk_r0_len
= use_sha384
? SHA384_MAC_LEN
: PMK_LEN
;
4377 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: PMK-R0",
4378 sm
->pmk_r0
, sm
->pmk_r0_len
);
4379 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR0Name",
4380 sm
->pmk_r0_name
, WPA_PMK_NAME_LEN
);
4381 wpa_printf(MSG_DEBUG
, "FILS+FT: R1KH-ID: " MACSTR
,
4382 MAC2STR(sm
->r1kh_id
));
4383 pos
= wpabuf_put(buf
, WPA_PMK_NAME_LEN
);
4384 if (wpa_derive_pmk_r1_name(sm
->pmk_r0_name
, sm
->r1kh_id
, sm
->own_addr
,
4385 sm
->pmk_r1_name
, use_sha384
) < 0) {
4386 wpa_printf(MSG_WARNING
, "FILS+FT: Could not derive PMKR1Name");
4389 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR1Name", sm
->pmk_r1_name
,
4391 os_memcpy(pos
, sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
4393 if (sm
->mgmt_group_cipher
== WPA_CIPHER_AES_128_CMAC
) {
4394 /* Management Group Cipher Suite */
4395 pos
= wpabuf_put(buf
, RSN_SELECTOR_LEN
);
4396 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_AES_128_CMAC
);
4399 rsnie
->len
= ((u8
*) wpabuf_put(buf
, 0) - (u8
*) rsnie
) - 2;
4402 #endif /* CONFIG_IEEE80211R */
4405 struct wpabuf
* fils_build_assoc_req(struct wpa_sm
*sm
, const u8
**kek
,
4406 size_t *kek_len
, const u8
**snonce
,
4408 const struct wpabuf
**hlp
,
4409 unsigned int num_hlp
)
4416 #ifdef CONFIG_IEEE80211R
4417 if (sm
->fils_ft_ies
)
4418 len
+= wpabuf_len(sm
->fils_ft_ies
);
4419 if (wpa_key_mgmt_ft(sm
->key_mgmt
))
4421 #endif /* CONFIG_IEEE80211R */
4422 for (i
= 0; hlp
&& i
< num_hlp
; i
++)
4423 len
+= 10 + wpabuf_len(hlp
[i
]);
4424 buf
= wpabuf_alloc(len
);
4428 #ifdef CONFIG_IEEE80211R
4429 if (wpa_key_mgmt_ft(sm
->key_mgmt
) && sm
->fils_ft_ies
) {
4430 /* MDE and FTE when using FILS+FT */
4431 wpabuf_put_buf(buf
, sm
->fils_ft_ies
);
4432 /* RSNE with PMKR1Name in PMKID field */
4433 if (fils_ft_build_assoc_req_rsne(sm
, buf
) < 0) {
4438 #endif /* CONFIG_IEEE80211R */
4441 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
4442 wpabuf_put_u8(buf
, 1 + FILS_SESSION_LEN
); /* Length */
4443 /* Element ID Extension */
4444 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_SESSION
);
4445 wpabuf_put_data(buf
, sm
->fils_session
, FILS_SESSION_LEN
);
4447 /* Everything after FILS Session element gets encrypted in the driver
4448 * with KEK. The buffer returned from here is the plaintext version. */
4450 /* TODO: FILS Public Key */
4452 /* FILS Key Confirm */
4453 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
4454 wpabuf_put_u8(buf
, 1 + sm
->fils_key_auth_len
); /* Length */
4455 /* Element ID Extension */
4456 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_KEY_CONFIRM
);
4457 wpabuf_put_data(buf
, sm
->fils_key_auth_sta
, sm
->fils_key_auth_len
);
4459 /* FILS HLP Container */
4460 for (i
= 0; hlp
&& i
< num_hlp
; i
++) {
4461 const u8
*pos
= wpabuf_head(hlp
[i
]);
4462 size_t left
= wpabuf_len(hlp
[i
]);
4464 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
4469 wpabuf_put_u8(buf
, len
); /* Length */
4470 /* Element ID Extension */
4471 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_HLP_CONTAINER
);
4472 /* Destination MAC Address, Source MAC Address, HLP Packet.
4473 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4474 * header when LPD is used). */
4475 wpabuf_put_data(buf
, pos
, len
- 1);
4479 wpabuf_put_u8(buf
, WLAN_EID_FRAGMENT
);
4480 len
= left
> 255 ? 255 : left
;
4481 wpabuf_put_u8(buf
, len
);
4482 wpabuf_put_data(buf
, pos
, len
);
4488 /* TODO: FILS IP Address Assignment */
4491 if (wpa_sm_ocv_enabled(sm
)) {
4492 struct wpa_channel_info ci
;
4495 if (wpa_sm_channel_info(sm
, &ci
) != 0) {
4496 wpa_printf(MSG_WARNING
,
4497 "FILS: Failed to get channel info for OCI element");
4502 pos
= wpabuf_put(buf
, OCV_OCI_EXTENDED_LEN
);
4503 if (ocv_insert_extended_oci(&ci
, pos
) < 0) {
4508 #endif /* CONFIG_OCV */
4510 wpa_hexdump_buf(MSG_DEBUG
, "FILS: Association Request plaintext", buf
);
4513 *kek_len
= sm
->ptk
.kek_len
;
4514 wpa_hexdump_key(MSG_DEBUG
, "FILS: KEK for AEAD", *kek
, *kek_len
);
4515 *snonce
= sm
->fils_nonce
;
4516 wpa_hexdump(MSG_DEBUG
, "FILS: SNonce for AEAD AAD",
4517 *snonce
, FILS_NONCE_LEN
);
4518 *anonce
= sm
->fils_anonce
;
4519 wpa_hexdump(MSG_DEBUG
, "FILS: ANonce for AEAD AAD",
4520 *anonce
, FILS_NONCE_LEN
);
4526 static void fils_process_hlp_resp(struct wpa_sm
*sm
, const u8
*resp
, size_t len
)
4528 const u8
*pos
, *end
;
4530 wpa_hexdump(MSG_MSGDUMP
, "FILS: HLP response", resp
, len
);
4531 if (len
< 2 * ETH_ALEN
)
4533 pos
= resp
+ 2 * ETH_ALEN
;
4535 if (end
- pos
>= 6 &&
4536 os_memcmp(pos
, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4537 pos
+= 6; /* Remove SNAP/LLC header */
4538 wpa_sm_fils_hlp_rx(sm
, resp
, resp
+ ETH_ALEN
, pos
, end
- pos
);
4542 static void fils_process_hlp_container(struct wpa_sm
*sm
, const u8
*pos
,
4545 const u8
*end
= pos
+ len
;
4548 /* Check if there are any FILS HLP Container elements */
4549 while (end
- pos
>= 2) {
4550 if (2 + pos
[1] > end
- pos
)
4552 if (pos
[0] == WLAN_EID_EXTENSION
&&
4553 pos
[1] >= 1 + 2 * ETH_ALEN
&&
4554 pos
[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER
)
4559 return; /* No FILS HLP Container elements */
4561 tmp
= os_malloc(end
- pos
);
4565 while (end
- pos
>= 2) {
4566 if (2 + pos
[1] > end
- pos
||
4567 pos
[0] != WLAN_EID_EXTENSION
||
4568 pos
[1] < 1 + 2 * ETH_ALEN
||
4569 pos
[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER
)
4572 os_memcpy(tmp_pos
, pos
+ 3, pos
[1] - 1);
4573 tmp_pos
+= pos
[1] - 1;
4576 /* Add possible fragments */
4577 while (end
- pos
>= 2 && pos
[0] == WLAN_EID_FRAGMENT
&&
4578 2 + pos
[1] <= end
- pos
) {
4579 os_memcpy(tmp_pos
, pos
+ 2, pos
[1]);
4584 fils_process_hlp_resp(sm
, tmp
, tmp_pos
- tmp
);
4591 int fils_process_assoc_resp(struct wpa_sm
*sm
, const u8
*resp
, size_t len
)
4593 const struct ieee80211_mgmt
*mgmt
;
4594 const u8
*end
, *ie_start
;
4595 struct ieee802_11_elems elems
;
4598 struct wpa_gtk_data gd
;
4600 struct wpa_eapol_ie_parse kde
;
4602 if (!sm
|| !sm
->ptk_set
) {
4603 wpa_printf(MSG_DEBUG
, "FILS: No KEK available");
4607 if (!wpa_key_mgmt_fils(sm
->key_mgmt
)) {
4608 wpa_printf(MSG_DEBUG
, "FILS: Not a FILS AKM");
4612 if (sm
->fils_completed
) {
4613 wpa_printf(MSG_DEBUG
,
4614 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4618 wpa_hexdump(MSG_DEBUG
, "FILS: (Re)Association Response frame",
4621 mgmt
= (const struct ieee80211_mgmt
*) resp
;
4622 if (len
< IEEE80211_HDRLEN
+ sizeof(mgmt
->u
.assoc_resp
))
4626 /* Same offset for Association Response and Reassociation Response */
4627 ie_start
= mgmt
->u
.assoc_resp
.variable
;
4629 if (ieee802_11_parse_elems(ie_start
, end
- ie_start
, &elems
, 1) ==
4631 wpa_printf(MSG_DEBUG
,
4632 "FILS: Failed to parse decrypted elements");
4636 if (!elems
.fils_session
) {
4637 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
4640 if (os_memcmp(elems
.fils_session
, sm
->fils_session
,
4641 FILS_SESSION_LEN
) != 0) {
4642 wpa_printf(MSG_DEBUG
, "FILS: FILS Session mismatch");
4643 wpa_hexdump(MSG_DEBUG
, "FILS: Received FILS Session",
4644 elems
.fils_session
, FILS_SESSION_LEN
);
4645 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
4646 sm
->fils_session
, FILS_SESSION_LEN
);
4649 if (!elems
.rsn_ie
) {
4650 wpa_printf(MSG_DEBUG
,
4651 "FILS: No RSNE in (Re)Association Response");
4652 /* As an interop workaround, allow this for now since IEEE Std
4653 * 802.11ai-2016 did not include all the needed changes to make
4654 * a FILS AP include RSNE in the frame. This workaround might
4655 * eventually be removed and replaced with rejection (goto fail)
4656 * to follow a strict interpretation of the standard. */
4657 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm
->key_mgmt
),
4658 sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
,
4659 elems
.rsn_ie
- 2, elems
.rsn_ie_len
+ 2)) {
4660 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
4661 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
4662 wpa_hexdump(MSG_DEBUG
, "FILS: RSNE in Beacon/Probe Response",
4663 sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
);
4664 wpa_hexdump(MSG_DEBUG
, "FILS: RSNE in (Re)Association Response",
4665 elems
.rsn_ie
, elems
.rsn_ie_len
);
4669 /* TODO: FILS Public Key */
4671 if (!elems
.fils_key_confirm
) {
4672 wpa_printf(MSG_DEBUG
, "FILS: No FILS Key Confirm element");
4675 if (elems
.fils_key_confirm_len
!= sm
->fils_key_auth_len
) {
4676 wpa_printf(MSG_DEBUG
,
4677 "FILS: Unexpected Key-Auth length %d (expected %d)",
4678 elems
.fils_key_confirm_len
,
4679 (int) sm
->fils_key_auth_len
);
4682 if (os_memcmp(elems
.fils_key_confirm
, sm
->fils_key_auth_ap
,
4683 sm
->fils_key_auth_len
) != 0) {
4684 wpa_printf(MSG_DEBUG
, "FILS: Key-Auth mismatch");
4685 wpa_hexdump(MSG_DEBUG
, "FILS: Received Key-Auth",
4686 elems
.fils_key_confirm
,
4687 elems
.fils_key_confirm_len
);
4688 wpa_hexdump(MSG_DEBUG
, "FILS: Expected Key-Auth",
4689 sm
->fils_key_auth_ap
, sm
->fils_key_auth_len
);
4694 if (wpa_sm_ocv_enabled(sm
)) {
4695 struct wpa_channel_info ci
;
4697 if (wpa_sm_channel_info(sm
, &ci
) != 0) {
4698 wpa_printf(MSG_WARNING
,
4699 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4703 if (ocv_verify_tx_params(elems
.oci
, elems
.oci_len
, &ci
,
4704 channel_width_to_int(ci
.chanwidth
),
4705 ci
.seg1_idx
) != 0) {
4706 wpa_printf(MSG_WARNING
, "FILS: %s", ocv_errorstr
);
4710 #endif /* CONFIG_OCV */
4712 #ifdef CONFIG_IEEE80211R
4713 if (wpa_key_mgmt_ft(sm
->key_mgmt
) && sm
->fils_ft_ies
) {
4714 struct wpa_ie_data rsn
;
4716 /* Check that PMKR1Name derived by the AP matches */
4717 if (!elems
.rsn_ie
||
4718 wpa_parse_wpa_ie_rsn(elems
.rsn_ie
- 2, elems
.rsn_ie_len
+ 2,
4720 !rsn
.pmkid
|| rsn
.num_pmkid
!= 1 ||
4721 os_memcmp(rsn
.pmkid
, sm
->pmk_r1_name
,
4722 WPA_PMK_NAME_LEN
) != 0) {
4723 wpa_printf(MSG_DEBUG
,
4724 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
4728 #endif /* CONFIG_IEEE80211R */
4731 if (!elems
.key_delivery
) {
4732 wpa_printf(MSG_DEBUG
, "FILS: No Key Delivery element");
4736 /* Parse GTK and set the key to the driver */
4737 os_memset(&gd
, 0, sizeof(gd
));
4738 if (wpa_supplicant_parse_ies(elems
.key_delivery
+ WPA_KEY_RSC_LEN
,
4739 elems
.key_delivery_len
- WPA_KEY_RSC_LEN
,
4741 wpa_printf(MSG_DEBUG
, "FILS: Failed to parse KDEs");
4745 wpa_printf(MSG_DEBUG
, "FILS: No GTK KDE");
4748 maxkeylen
= gd
.gtk_len
= kde
.gtk_len
- 2;
4749 if (wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
4750 gd
.gtk_len
, maxkeylen
,
4751 &gd
.key_rsc_len
, &gd
.alg
))
4754 wpa_hexdump_key(MSG_DEBUG
, "FILS: Received GTK", kde
.gtk
, kde
.gtk_len
);
4755 gd
.keyidx
= kde
.gtk
[0] & 0x3;
4756 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
4757 !!(kde
.gtk
[0] & BIT(2)));
4758 if (kde
.gtk_len
- 2 > sizeof(gd
.gtk
)) {
4759 wpa_printf(MSG_DEBUG
, "FILS: Too long GTK in GTK KDE (len=%lu)",
4760 (unsigned long) kde
.gtk_len
- 2);
4763 os_memcpy(gd
.gtk
, kde
.gtk
+ 2, kde
.gtk_len
- 2);
4765 wpa_printf(MSG_DEBUG
, "FILS: Set GTK to driver");
4766 if (wpa_supplicant_install_gtk(sm
, &gd
, elems
.key_delivery
, 0) < 0) {
4767 wpa_printf(MSG_DEBUG
, "FILS: Failed to set GTK");
4771 if (ieee80211w_set_keys(sm
, &kde
) < 0) {
4772 wpa_printf(MSG_DEBUG
, "FILS: Failed to set IGTK");
4776 alg
= wpa_cipher_to_alg(sm
->pairwise_cipher
);
4777 keylen
= wpa_cipher_key_len(sm
->pairwise_cipher
);
4778 if (keylen
<= 0 || (unsigned int) keylen
!= sm
->ptk
.tk_len
) {
4779 wpa_printf(MSG_DEBUG
, "FILS: TK length mismatch: %u != %lu",
4780 keylen
, (long unsigned int) sm
->ptk
.tk_len
);
4784 rsclen
= wpa_cipher_rsc_len(sm
->pairwise_cipher
);
4785 wpa_hexdump_key(MSG_DEBUG
, "FILS: Set TK to driver",
4786 sm
->ptk
.tk
, keylen
);
4787 if (wpa_sm_set_key(sm
, alg
, sm
->bssid
, 0, 1, null_rsc
, rsclen
,
4788 sm
->ptk
.tk
, keylen
, KEY_FLAG_PAIRWISE_RX_TX
) < 0) {
4789 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
4790 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4792 alg
, keylen
, MAC2STR(sm
->bssid
));
4796 /* TODO: TK could be cleared after auth frame exchange now that driver
4797 * takes care of association frame encryption/decryption. */
4798 /* TK is not needed anymore in supplicant */
4799 os_memset(sm
->ptk
.tk
, 0, WPA_TK_MAX_LEN
);
4801 sm
->ptk
.installed
= 1;
4803 /* FILS HLP Container */
4804 fils_process_hlp_container(sm
, ie_start
, end
- ie_start
);
4806 /* TODO: FILS IP Address Assignment */
4808 wpa_printf(MSG_DEBUG
, "FILS: Auth+Assoc completed successfully");
4809 sm
->fils_completed
= 1;
4810 forced_memzero(&gd
, sizeof(gd
));
4814 forced_memzero(&gd
, sizeof(gd
));
4819 void wpa_sm_set_reset_fils_completed(struct wpa_sm
*sm
, int set
)
4822 sm
->fils_completed
= !!set
;
4825 #endif /* CONFIG_FILS */
4828 int wpa_fils_is_completed(struct wpa_sm
*sm
)
4831 return sm
&& sm
->fils_completed
;
4832 #else /* CONFIG_FILS */
4834 #endif /* CONFIG_FILS */
4840 struct wpabuf
* owe_build_assoc_req(struct wpa_sm
*sm
, u16 group
)
4842 struct wpabuf
*ie
= NULL
, *pub
= NULL
;
4847 else if (group
== 20)
4849 else if (group
== 21)
4854 crypto_ecdh_deinit(sm
->owe_ecdh
);
4855 sm
->owe_ecdh
= crypto_ecdh_init(group
);
4858 sm
->owe_group
= group
;
4859 pub
= crypto_ecdh_get_pubkey(sm
->owe_ecdh
, 0);
4860 pub
= wpabuf_zeropad(pub
, prime_len
);
4864 ie
= wpabuf_alloc(5 + wpabuf_len(pub
));
4867 wpabuf_put_u8(ie
, WLAN_EID_EXTENSION
);
4868 wpabuf_put_u8(ie
, 1 + 2 + wpabuf_len(pub
));
4869 wpabuf_put_u8(ie
, WLAN_EID_EXT_OWE_DH_PARAM
);
4870 wpabuf_put_le16(ie
, group
);
4871 wpabuf_put_buf(ie
, pub
);
4873 wpa_hexdump_buf(MSG_DEBUG
, "OWE: Diffie-Hellman Parameter element",
4879 crypto_ecdh_deinit(sm
->owe_ecdh
);
4880 sm
->owe_ecdh
= NULL
;
4885 int owe_process_assoc_resp(struct wpa_sm
*sm
, const u8
*bssid
,
4886 const u8
*resp_ies
, size_t resp_ies_len
)
4888 struct ieee802_11_elems elems
;
4890 struct wpabuf
*secret
, *pub
, *hkey
;
4892 u8 prk
[SHA512_MAC_LEN
], pmkid
[SHA512_MAC_LEN
];
4893 const char *info
= "OWE Key Generation";
4896 size_t hash_len
, prime_len
;
4897 struct wpa_ie_data data
;
4900 ieee802_11_parse_elems(resp_ies
, resp_ies_len
, &elems
, 1) ==
4902 wpa_printf(MSG_INFO
,
4903 "OWE: Could not parse Association Response frame elements");
4907 if (sm
->cur_pmksa
&& elems
.rsn_ie
&&
4908 wpa_parse_wpa_ie_rsn(elems
.rsn_ie
- 2, 2 + elems
.rsn_ie_len
,
4910 data
.num_pmkid
== 1 && data
.pmkid
&&
4911 os_memcmp(sm
->cur_pmksa
->pmkid
, data
.pmkid
, PMKID_LEN
) == 0) {
4912 wpa_printf(MSG_DEBUG
, "OWE: Use PMKSA caching");
4913 wpa_sm_set_pmk_from_pmksa(sm
);
4917 if (!elems
.owe_dh
) {
4918 wpa_printf(MSG_INFO
,
4919 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
4923 group
= WPA_GET_LE16(elems
.owe_dh
);
4924 if (group
!= sm
->owe_group
) {
4925 wpa_printf(MSG_INFO
,
4926 "OWE: Unexpected Diffie-Hellman group in response: %u",
4931 if (!sm
->owe_ecdh
) {
4932 wpa_printf(MSG_INFO
, "OWE: No ECDH state available");
4938 else if (group
== 20)
4940 else if (group
== 21)
4945 secret
= crypto_ecdh_set_peerkey(sm
->owe_ecdh
, 0,
4947 elems
.owe_dh_len
- 2);
4948 secret
= wpabuf_zeropad(secret
, prime_len
);
4950 wpa_printf(MSG_DEBUG
, "OWE: Invalid peer DH public key");
4953 wpa_hexdump_buf_key(MSG_DEBUG
, "OWE: DH shared secret", secret
);
4955 /* prk = HKDF-extract(C | A | group, z) */
4957 pub
= crypto_ecdh_get_pubkey(sm
->owe_ecdh
, 0);
4959 wpabuf_clear_free(secret
);
4963 /* PMKID = Truncate-128(Hash(C | A)) */
4964 addr
[0] = wpabuf_head(pub
);
4965 len
[0] = wpabuf_len(pub
);
4966 addr
[1] = elems
.owe_dh
+ 2;
4967 len
[1] = elems
.owe_dh_len
- 2;
4969 res
= sha256_vector(2, addr
, len
, pmkid
);
4970 hash_len
= SHA256_MAC_LEN
;
4971 } else if (group
== 20) {
4972 res
= sha384_vector(2, addr
, len
, pmkid
);
4973 hash_len
= SHA384_MAC_LEN
;
4974 } else if (group
== 21) {
4975 res
= sha512_vector(2, addr
, len
, pmkid
);
4976 hash_len
= SHA512_MAC_LEN
;
4981 pub
= wpabuf_zeropad(pub
, prime_len
);
4982 if (res
< 0 || !pub
) {
4984 wpabuf_clear_free(secret
);
4988 hkey
= wpabuf_alloc(wpabuf_len(pub
) + elems
.owe_dh_len
- 2 + 2);
4991 wpabuf_clear_free(secret
);
4995 wpabuf_put_buf(hkey
, pub
); /* C */
4997 wpabuf_put_data(hkey
, elems
.owe_dh
+ 2, elems
.owe_dh_len
- 2); /* A */
4998 wpabuf_put_le16(hkey
, sm
->owe_group
); /* group */
5000 res
= hmac_sha256(wpabuf_head(hkey
), wpabuf_len(hkey
),
5001 wpabuf_head(secret
), wpabuf_len(secret
), prk
);
5002 else if (group
== 20)
5003 res
= hmac_sha384(wpabuf_head(hkey
), wpabuf_len(hkey
),
5004 wpabuf_head(secret
), wpabuf_len(secret
), prk
);
5005 else if (group
== 21)
5006 res
= hmac_sha512(wpabuf_head(hkey
), wpabuf_len(hkey
),
5007 wpabuf_head(secret
), wpabuf_len(secret
), prk
);
5008 wpabuf_clear_free(hkey
);
5009 wpabuf_clear_free(secret
);
5013 wpa_hexdump_key(MSG_DEBUG
, "OWE: prk", prk
, hash_len
);
5015 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
5018 res
= hmac_sha256_kdf(prk
, hash_len
, NULL
, (const u8
*) info
,
5019 os_strlen(info
), sm
->pmk
, hash_len
);
5020 else if (group
== 20)
5021 res
= hmac_sha384_kdf(prk
, hash_len
, NULL
, (const u8
*) info
,
5022 os_strlen(info
), sm
->pmk
, hash_len
);
5023 else if (group
== 21)
5024 res
= hmac_sha512_kdf(prk
, hash_len
, NULL
, (const u8
*) info
,
5025 os_strlen(info
), sm
->pmk
, hash_len
);
5026 forced_memzero(prk
, SHA512_MAC_LEN
);
5031 sm
->pmk_len
= hash_len
;
5033 wpa_hexdump_key(MSG_DEBUG
, "OWE: PMK", sm
->pmk
, sm
->pmk_len
);
5034 wpa_hexdump(MSG_DEBUG
, "OWE: PMKID", pmkid
, PMKID_LEN
);
5035 pmksa_cache_add(sm
->pmksa
, sm
->pmk
, sm
->pmk_len
, pmkid
, NULL
, 0,
5036 bssid
, sm
->own_addr
, sm
->network_ctx
, sm
->key_mgmt
,
5042 #endif /* CONFIG_OWE */
5045 void wpa_sm_set_fils_cache_id(struct wpa_sm
*sm
, const u8
*fils_cache_id
)
5048 if (sm
&& fils_cache_id
) {
5049 sm
->fils_cache_id_set
= 1;
5050 os_memcpy(sm
->fils_cache_id
, fils_cache_id
, FILS_CACHE_ID_LEN
);
5052 #endif /* CONFIG_FILS */
5057 void wpa_sm_set_dpp_z(struct wpa_sm
*sm
, const struct wpabuf
*z
)
5060 wpabuf_clear_free(sm
->dpp_z
);
5061 sm
->dpp_z
= z
? wpabuf_dup(z
) : NULL
;
5064 #endif /* CONFIG_DPP2 */