2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2019, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/state_machine.h"
14 #include "utils/bitfield.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ocv.h"
17 #include "crypto/aes.h"
18 #include "crypto/aes_wrap.h"
19 #include "crypto/aes_siv.h"
20 #include "crypto/crypto.h"
21 #include "crypto/sha1.h"
22 #include "crypto/sha256.h"
23 #include "crypto/sha384.h"
24 #include "crypto/random.h"
25 #include "eapol_auth/eapol_auth_sm.h"
26 #include "drivers/driver.h"
27 #include "ap_config.h"
28 #include "ieee802_11.h"
30 #include "pmksa_cache_auth.h"
31 #include "wpa_auth_i.h"
32 #include "wpa_auth_ie.h"
34 #define STATE_MACHINE_DATA struct wpa_state_machine
35 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
36 #define STATE_MACHINE_ADDR sm->addr
39 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
);
40 static int wpa_sm_step(struct wpa_state_machine
*sm
);
41 static int wpa_verify_key_mic(int akmp
, size_t pmk_len
, struct wpa_ptk
*PTK
,
42 u8
*data
, size_t data_len
);
44 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
45 u8
*buf
, size_t buf_len
, u16
*_key_data_len
);
46 static struct wpabuf
* fils_prepare_plainbuf(struct wpa_state_machine
*sm
,
47 const struct wpabuf
*hlp
);
48 #endif /* CONFIG_FILS */
49 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
);
50 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
51 struct wpa_group
*group
);
52 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
);
53 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
54 struct wpa_group
*group
);
55 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
56 struct wpa_group
*group
);
57 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
58 const u8
*pmk
, unsigned int pmk_len
,
59 struct wpa_ptk
*ptk
, int force_sha256
);
60 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
61 struct wpa_group
*group
);
62 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
63 struct wpa_group
*group
);
64 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
65 struct wpa_group
*group
);
66 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
);
67 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
);
69 static const u32 eapol_key_timeout_first
= 100; /* ms */
70 static const u32 eapol_key_timeout_subseq
= 1000; /* ms */
71 static const u32 eapol_key_timeout_first_group
= 500; /* ms */
72 static const u32 eapol_key_timeout_no_retrans
= 4000; /* ms */
74 /* TODO: make these configurable */
75 static const int dot11RSNAConfigPMKLifetime
= 43200;
76 static const int dot11RSNAConfigPMKReauthThreshold
= 70;
77 static const int dot11RSNAConfigSATimeout
= 60;
80 static inline int wpa_auth_mic_failure_report(
81 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
83 if (wpa_auth
->cb
->mic_failure_report
)
84 return wpa_auth
->cb
->mic_failure_report(wpa_auth
->cb_ctx
, addr
);
89 static inline void wpa_auth_psk_failure_report(
90 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
92 if (wpa_auth
->cb
->psk_failure_report
)
93 wpa_auth
->cb
->psk_failure_report(wpa_auth
->cb_ctx
, addr
);
97 static inline void wpa_auth_set_eapol(struct wpa_authenticator
*wpa_auth
,
98 const u8
*addr
, wpa_eapol_variable var
,
101 if (wpa_auth
->cb
->set_eapol
)
102 wpa_auth
->cb
->set_eapol(wpa_auth
->cb_ctx
, addr
, var
, value
);
106 static inline int wpa_auth_get_eapol(struct wpa_authenticator
*wpa_auth
,
107 const u8
*addr
, wpa_eapol_variable var
)
109 if (wpa_auth
->cb
->get_eapol
== NULL
)
111 return wpa_auth
->cb
->get_eapol(wpa_auth
->cb_ctx
, addr
, var
);
115 static inline const u8
* wpa_auth_get_psk(struct wpa_authenticator
*wpa_auth
,
117 const u8
*p2p_dev_addr
,
118 const u8
*prev_psk
, size_t *psk_len
,
121 if (wpa_auth
->cb
->get_psk
== NULL
)
123 return wpa_auth
->cb
->get_psk(wpa_auth
->cb_ctx
, addr
, p2p_dev_addr
,
124 prev_psk
, psk_len
, vlan_id
);
128 static inline int wpa_auth_get_msk(struct wpa_authenticator
*wpa_auth
,
129 const u8
*addr
, u8
*msk
, size_t *len
)
131 if (wpa_auth
->cb
->get_msk
== NULL
)
133 return wpa_auth
->cb
->get_msk(wpa_auth
->cb_ctx
, addr
, msk
, len
);
137 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
139 enum wpa_alg alg
, const u8
*addr
, int idx
,
140 u8
*key
, size_t key_len
,
141 enum key_flag key_flag
)
143 if (wpa_auth
->cb
->set_key
== NULL
)
145 return wpa_auth
->cb
->set_key(wpa_auth
->cb_ctx
, vlan_id
, alg
, addr
, idx
,
146 key
, key_len
, key_flag
);
150 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
151 const u8
*addr
, int idx
, u8
*seq
)
155 if (wpa_auth
->cb
->get_seqnum
== NULL
)
157 res
= wpa_auth
->cb
->get_seqnum(wpa_auth
->cb_ctx
, addr
, idx
, seq
);
158 #ifdef CONFIG_TESTING_OPTIONS
159 if (!addr
&& idx
< 4 && wpa_auth
->conf
.gtk_rsc_override_set
) {
160 wpa_printf(MSG_DEBUG
,
161 "TESTING: Override GTK RSC %016llx --> %016llx",
162 (long long unsigned) WPA_GET_LE64(seq
),
164 WPA_GET_LE64(wpa_auth
->conf
.gtk_rsc_override
));
165 os_memcpy(seq
, wpa_auth
->conf
.gtk_rsc_override
,
168 if (!addr
&& idx
>= 4 && idx
<= 5 &&
169 wpa_auth
->conf
.igtk_rsc_override_set
) {
170 wpa_printf(MSG_DEBUG
,
171 "TESTING: Override IGTK RSC %016llx --> %016llx",
172 (long long unsigned) WPA_GET_LE64(seq
),
174 WPA_GET_LE64(wpa_auth
->conf
.igtk_rsc_override
));
175 os_memcpy(seq
, wpa_auth
->conf
.igtk_rsc_override
,
178 #endif /* CONFIG_TESTING_OPTIONS */
184 wpa_auth_send_eapol(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
185 const u8
*data
, size_t data_len
, int encrypt
)
187 if (wpa_auth
->cb
->send_eapol
== NULL
)
189 return wpa_auth
->cb
->send_eapol(wpa_auth
->cb_ctx
, addr
, data
, data_len
,
195 static inline int wpa_auth_start_ampe(struct wpa_authenticator
*wpa_auth
,
198 if (wpa_auth
->cb
->start_ampe
== NULL
)
200 return wpa_auth
->cb
->start_ampe(wpa_auth
->cb_ctx
, addr
);
202 #endif /* CONFIG_MESH */
205 int wpa_auth_for_each_sta(struct wpa_authenticator
*wpa_auth
,
206 int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
209 if (wpa_auth
->cb
->for_each_sta
== NULL
)
211 return wpa_auth
->cb
->for_each_sta(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
215 int wpa_auth_for_each_auth(struct wpa_authenticator
*wpa_auth
,
216 int (*cb
)(struct wpa_authenticator
*a
, void *ctx
),
219 if (wpa_auth
->cb
->for_each_auth
== NULL
)
221 return wpa_auth
->cb
->for_each_auth(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
225 void wpa_auth_logger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
226 logger_level level
, const char *txt
)
228 if (wpa_auth
->cb
->logger
== NULL
)
230 wpa_auth
->cb
->logger(wpa_auth
->cb_ctx
, addr
, level
, txt
);
234 void wpa_auth_vlogger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
235 logger_level level
, const char *fmt
, ...)
241 if (wpa_auth
->cb
->logger
== NULL
)
244 maxlen
= os_strlen(fmt
) + 100;
245 format
= os_malloc(maxlen
);
250 vsnprintf(format
, maxlen
, fmt
, ap
);
253 wpa_auth_logger(wpa_auth
, addr
, level
, format
);
259 static void wpa_sta_disconnect(struct wpa_authenticator
*wpa_auth
,
260 const u8
*addr
, u16 reason
)
262 if (wpa_auth
->cb
->disconnect
== NULL
)
264 wpa_printf(MSG_DEBUG
, "wpa_sta_disconnect STA " MACSTR
" (reason %u)",
265 MAC2STR(addr
), reason
);
266 wpa_auth
->cb
->disconnect(wpa_auth
->cb_ctx
, addr
, reason
);
271 static int wpa_channel_info(struct wpa_authenticator
*wpa_auth
,
272 struct wpa_channel_info
*ci
)
274 if (!wpa_auth
->cb
->channel_info
)
276 return wpa_auth
->cb
->channel_info(wpa_auth
->cb_ctx
, ci
);
278 #endif /* CONFIG_OCV */
281 static int wpa_auth_update_vlan(struct wpa_authenticator
*wpa_auth
,
282 const u8
*addr
, int vlan_id
)
284 if (!wpa_auth
->cb
->update_vlan
)
286 return wpa_auth
->cb
->update_vlan(wpa_auth
->cb_ctx
, addr
, vlan_id
);
290 static void wpa_rekey_gmk(void *eloop_ctx
, void *timeout_ctx
)
292 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
294 if (random_get_bytes(wpa_auth
->group
->GMK
, WPA_GMK_LEN
)) {
295 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
298 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "GMK rekeyd");
299 wpa_hexdump_key(MSG_DEBUG
, "GMK",
300 wpa_auth
->group
->GMK
, WPA_GMK_LEN
);
303 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
304 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
305 wpa_rekey_gmk
, wpa_auth
, NULL
);
310 static void wpa_rekey_gtk(void *eloop_ctx
, void *timeout_ctx
)
312 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
313 struct wpa_group
*group
, *next
;
315 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "rekeying GTK");
316 group
= wpa_auth
->group
;
318 wpa_group_get(wpa_auth
, group
);
320 group
->GTKReKey
= TRUE
;
322 group
->changed
= FALSE
;
323 wpa_group_sm_step(wpa_auth
, group
);
324 } while (group
->changed
);
327 wpa_group_put(wpa_auth
, group
);
331 if (wpa_auth
->conf
.wpa_group_rekey
) {
332 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
,
333 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
338 static void wpa_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
340 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
341 struct wpa_state_machine
*sm
= timeout_ctx
;
343 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "rekeying PTK");
344 wpa_request_new_ptk(sm
);
349 void wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine
*sm
)
351 if (sm
&& sm
->wpa_auth
->conf
.wpa_ptk_rekey
) {
352 wpa_printf(MSG_DEBUG
, "WPA: Start PTK rekeying timer for "
353 MACSTR
" (%d seconds)", MAC2STR(sm
->addr
),
354 sm
->wpa_auth
->conf
.wpa_ptk_rekey
);
355 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
356 eloop_register_timeout(sm
->wpa_auth
->conf
.wpa_ptk_rekey
, 0,
357 wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
362 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine
*sm
, void *ctx
)
364 if (sm
->pmksa
== ctx
)
370 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
373 struct wpa_authenticator
*wpa_auth
= ctx
;
374 wpa_auth_for_each_sta(wpa_auth
, wpa_auth_pmksa_clear_cb
, entry
);
378 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator
*wpa_auth
,
379 struct wpa_group
*group
)
381 u8 buf
[ETH_ALEN
+ 8 + sizeof(unsigned long)];
385 if (random_get_bytes(group
->GMK
, WPA_GMK_LEN
) < 0)
387 wpa_hexdump_key(MSG_DEBUG
, "GMK", group
->GMK
, WPA_GMK_LEN
);
390 * Counter = PRF-256(Random number, "Init Counter",
391 * Local MAC Address || Time)
393 os_memcpy(buf
, wpa_auth
->addr
, ETH_ALEN
);
394 wpa_get_ntp_timestamp(buf
+ ETH_ALEN
);
395 ptr
= (unsigned long) group
;
396 os_memcpy(buf
+ ETH_ALEN
+ 8, &ptr
, sizeof(ptr
));
398 os_memset(buf
+ ETH_ALEN
, 0xab, 8);
399 os_memset(buf
+ ETH_ALEN
+ 8, 0xcd, sizeof(ptr
));
400 #endif /* TEST_FUZZ */
401 if (random_get_bytes(rkey
, sizeof(rkey
)) < 0)
404 if (sha1_prf(rkey
, sizeof(rkey
), "Init Counter", buf
, sizeof(buf
),
405 group
->Counter
, WPA_NONCE_LEN
) < 0)
407 wpa_hexdump_key(MSG_DEBUG
, "Key Counter",
408 group
->Counter
, WPA_NONCE_LEN
);
414 static struct wpa_group
* wpa_group_init(struct wpa_authenticator
*wpa_auth
,
415 int vlan_id
, int delay_init
)
417 struct wpa_group
*group
;
419 group
= os_zalloc(sizeof(struct wpa_group
));
423 group
->GTKAuthenticator
= TRUE
;
424 group
->vlan_id
= vlan_id
;
425 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
427 if (random_pool_ready() != 1) {
428 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
429 "for secure operations - update keys later when "
430 "the first station connects");
434 * Set initial GMK/Counter value here. The actual values that will be
435 * used in negotiations will be set once the first station tries to
436 * connect. This allows more time for collecting additional randomness
437 * on embedded devices.
439 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0) {
440 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
448 wpa_printf(MSG_DEBUG
, "WPA: Delay group state machine start "
449 "until Beacon frames have been configured");
450 /* Initialization is completed in wpa_init_keys(). */
452 wpa_group_sm_step(wpa_auth
, group
);
453 group
->GInit
= FALSE
;
454 wpa_group_sm_step(wpa_auth
, group
);
462 * wpa_init - Initialize WPA authenticator
463 * @addr: Authenticator address
464 * @conf: Configuration for WPA authenticator
465 * @cb: Callback functions for WPA authenticator
466 * Returns: Pointer to WPA authenticator data or %NULL on failure
468 struct wpa_authenticator
* wpa_init(const u8
*addr
,
469 struct wpa_auth_config
*conf
,
470 const struct wpa_auth_callbacks
*cb
,
473 struct wpa_authenticator
*wpa_auth
;
475 wpa_auth
= os_zalloc(sizeof(struct wpa_authenticator
));
476 if (wpa_auth
== NULL
)
478 os_memcpy(wpa_auth
->addr
, addr
, ETH_ALEN
);
479 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
481 wpa_auth
->cb_ctx
= cb_ctx
;
483 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
484 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
489 wpa_auth
->group
= wpa_group_init(wpa_auth
, 0, 1);
490 if (wpa_auth
->group
== NULL
) {
491 os_free(wpa_auth
->wpa_ie
);
496 wpa_auth
->pmksa
= pmksa_cache_auth_init(wpa_auth_pmksa_free_cb
,
498 if (wpa_auth
->pmksa
== NULL
) {
499 wpa_printf(MSG_ERROR
, "PMKSA cache initialization failed.");
500 os_free(wpa_auth
->group
);
501 os_free(wpa_auth
->wpa_ie
);
506 #ifdef CONFIG_IEEE80211R_AP
507 wpa_auth
->ft_pmk_cache
= wpa_ft_pmk_cache_init();
508 if (wpa_auth
->ft_pmk_cache
== NULL
) {
509 wpa_printf(MSG_ERROR
, "FT PMK cache initialization failed.");
510 os_free(wpa_auth
->group
);
511 os_free(wpa_auth
->wpa_ie
);
512 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
516 #endif /* CONFIG_IEEE80211R_AP */
518 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
519 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
520 wpa_rekey_gmk
, wpa_auth
, NULL
);
523 if (wpa_auth
->conf
.wpa_group_rekey
) {
524 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
, 0,
525 wpa_rekey_gtk
, wpa_auth
, NULL
);
529 if (WPA_GET_BE32(conf
->ip_addr_start
)) {
530 int count
= WPA_GET_BE32(conf
->ip_addr_end
) -
531 WPA_GET_BE32(conf
->ip_addr_start
) + 1;
535 wpa_auth
->ip_pool
= bitfield_alloc(count
);
537 #endif /* CONFIG_P2P */
543 int wpa_init_keys(struct wpa_authenticator
*wpa_auth
)
545 struct wpa_group
*group
= wpa_auth
->group
;
547 wpa_printf(MSG_DEBUG
, "WPA: Start group state machine to set initial "
549 wpa_group_sm_step(wpa_auth
, group
);
550 group
->GInit
= FALSE
;
551 wpa_group_sm_step(wpa_auth
, group
);
552 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
559 * wpa_deinit - Deinitialize WPA authenticator
560 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
562 void wpa_deinit(struct wpa_authenticator
*wpa_auth
)
564 struct wpa_group
*group
, *prev
;
566 eloop_cancel_timeout(wpa_rekey_gmk
, wpa_auth
, NULL
);
567 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
569 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
571 #ifdef CONFIG_IEEE80211R_AP
572 wpa_ft_pmk_cache_deinit(wpa_auth
->ft_pmk_cache
);
573 wpa_auth
->ft_pmk_cache
= NULL
;
574 wpa_ft_deinit(wpa_auth
);
575 #endif /* CONFIG_IEEE80211R_AP */
578 bitfield_free(wpa_auth
->ip_pool
);
579 #endif /* CONFIG_P2P */
582 os_free(wpa_auth
->wpa_ie
);
584 group
= wpa_auth
->group
;
596 * wpa_reconfig - Update WPA authenticator configuration
597 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
598 * @conf: Configuration for WPA authenticator
600 int wpa_reconfig(struct wpa_authenticator
*wpa_auth
,
601 struct wpa_auth_config
*conf
)
603 struct wpa_group
*group
;
604 if (wpa_auth
== NULL
)
607 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
608 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
609 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
614 * Reinitialize GTK to make sure it is suitable for the new
617 group
= wpa_auth
->group
;
618 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
620 wpa_group_sm_step(wpa_auth
, group
);
621 group
->GInit
= FALSE
;
622 wpa_group_sm_step(wpa_auth
, group
);
628 struct wpa_state_machine
*
629 wpa_auth_sta_init(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
630 const u8
*p2p_dev_addr
)
632 struct wpa_state_machine
*sm
;
634 if (wpa_auth
->group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
637 sm
= os_zalloc(sizeof(struct wpa_state_machine
));
640 os_memcpy(sm
->addr
, addr
, ETH_ALEN
);
642 os_memcpy(sm
->p2p_dev_addr
, p2p_dev_addr
, ETH_ALEN
);
644 sm
->wpa_auth
= wpa_auth
;
645 sm
->group
= wpa_auth
->group
;
646 wpa_group_get(sm
->wpa_auth
, sm
->group
);
652 int wpa_auth_sta_associated(struct wpa_authenticator
*wpa_auth
,
653 struct wpa_state_machine
*sm
)
655 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
658 #ifdef CONFIG_IEEE80211R_AP
659 if (sm
->ft_completed
) {
660 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
661 "FT authentication already completed - do not "
662 "start 4-way handshake");
663 /* Go to PTKINITDONE state to allow GTK rekeying */
664 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
668 #endif /* CONFIG_IEEE80211R_AP */
671 if (sm
->fils_completed
) {
672 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
673 "FILS authentication already completed - do not start 4-way handshake");
674 /* Go to PTKINITDONE state to allow GTK rekeying */
675 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
679 #endif /* CONFIG_FILS */
682 os_memset(&sm
->key_replay
, 0, sizeof(sm
->key_replay
));
683 sm
->ReAuthenticationRequest
= TRUE
;
684 return wpa_sm_step(sm
);
687 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
688 "start authentication");
692 if (wpa_sm_step(sm
) == 1)
693 return 1; /* should not really happen */
695 sm
->AuthenticationRequest
= TRUE
;
696 return wpa_sm_step(sm
);
700 void wpa_auth_sta_no_wpa(struct wpa_state_machine
*sm
)
702 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
703 * reassociates back to the same AP while the previous entry for the
704 * STA has not yet been removed. */
708 sm
->wpa_key_mgmt
= 0;
712 static void wpa_free_sta_sm(struct wpa_state_machine
*sm
)
715 if (WPA_GET_BE32(sm
->ip_addr
)) {
717 wpa_printf(MSG_DEBUG
, "P2P: Free assigned IP "
718 "address %u.%u.%u.%u from " MACSTR
,
719 sm
->ip_addr
[0], sm
->ip_addr
[1],
720 sm
->ip_addr
[2], sm
->ip_addr
[3],
722 start
= WPA_GET_BE32(sm
->wpa_auth
->conf
.ip_addr_start
);
723 bitfield_clear(sm
->wpa_auth
->ip_pool
,
724 WPA_GET_BE32(sm
->ip_addr
) - start
);
726 #endif /* CONFIG_P2P */
727 if (sm
->GUpdateStationKeys
) {
728 sm
->group
->GKeyDoneStations
--;
729 sm
->GUpdateStationKeys
= FALSE
;
731 #ifdef CONFIG_IEEE80211R_AP
732 os_free(sm
->assoc_resp_ftie
);
733 wpabuf_free(sm
->ft_pending_req_ies
);
734 #endif /* CONFIG_IEEE80211R_AP */
735 os_free(sm
->last_rx_eapol_key
);
738 wpa_group_put(sm
->wpa_auth
, sm
->group
);
740 wpabuf_clear_free(sm
->dpp_z
);
741 #endif /* CONFIG_DPP2 */
742 bin_clear_free(sm
, sizeof(*sm
));
746 void wpa_auth_sta_deinit(struct wpa_state_machine
*sm
)
748 struct wpa_authenticator
*wpa_auth
;
753 wpa_auth
= sm
->wpa_auth
;
754 if (wpa_auth
->conf
.wpa_strict_rekey
&& sm
->has_GTK
) {
755 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
756 "strict rekeying - force GTK rekey since STA "
758 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk
,
759 wpa_auth
, NULL
) == -1)
760 eloop_register_timeout(0, 500000, wpa_rekey_gtk
,
764 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
765 sm
->pending_1_of_4_timeout
= 0;
766 eloop_cancel_timeout(wpa_sm_call_step
, sm
, NULL
);
767 eloop_cancel_timeout(wpa_rekey_ptk
, wpa_auth
, sm
);
768 #ifdef CONFIG_IEEE80211R_AP
769 wpa_ft_sta_deinit(sm
);
770 #endif /* CONFIG_IEEE80211R_AP */
771 if (sm
->in_step_loop
) {
772 /* Must not free state machine while wpa_sm_step() is running.
773 * Freeing will be completed in the end of wpa_sm_step(). */
774 wpa_printf(MSG_DEBUG
, "WPA: Registering pending STA state "
775 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
776 sm
->pending_deinit
= 1;
782 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
)
787 if (!sm
->use_ext_key_id
&& sm
->wpa_auth
->conf
.wpa_deny_ptk0_rekey
) {
789 "WPA: PTK0 rekey not allowed, disconnect " MACSTR
,
791 sm
->Disconnect
= TRUE
;
792 /* Try to encourage the STA to reconnect */
793 sm
->disconnect_reason
=
794 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA
;
796 if (sm
->use_ext_key_id
)
797 sm
->keyidx_active
^= 1; /* flip Key ID */
798 sm
->PTKRequest
= TRUE
;
804 static int wpa_replay_counter_valid(struct wpa_key_replay_counter
*ctr
,
805 const u8
*replay_counter
)
808 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
811 if (os_memcmp(replay_counter
, ctr
[i
].counter
,
812 WPA_REPLAY_COUNTER_LEN
) == 0)
819 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter
*ctr
,
820 const u8
*replay_counter
)
823 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
825 (replay_counter
== NULL
||
826 os_memcmp(replay_counter
, ctr
[i
].counter
,
827 WPA_REPLAY_COUNTER_LEN
) == 0))
828 ctr
[i
].valid
= FALSE
;
833 #ifdef CONFIG_IEEE80211R_AP
834 static int ft_check_msg_2_of_4(struct wpa_authenticator
*wpa_auth
,
835 struct wpa_state_machine
*sm
,
836 struct wpa_eapol_ie_parse
*kde
)
838 struct wpa_ie_data ie
;
839 struct rsn_mdie
*mdie
;
841 if (wpa_parse_wpa_ie_rsn(kde
->rsn_ie
, kde
->rsn_ie_len
, &ie
) < 0 ||
842 ie
.num_pmkid
!= 1 || ie
.pmkid
== NULL
) {
843 wpa_printf(MSG_DEBUG
, "FT: No PMKR1Name in "
844 "FT 4-way handshake message 2/4");
848 os_memcpy(sm
->sup_pmk_r1_name
, ie
.pmkid
, PMKID_LEN
);
849 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Supplicant",
850 sm
->sup_pmk_r1_name
, PMKID_LEN
);
852 if (!kde
->mdie
|| !kde
->ftie
) {
853 wpa_printf(MSG_DEBUG
, "FT: No %s in FT 4-way handshake "
854 "message 2/4", kde
->mdie
? "FTIE" : "MDIE");
858 mdie
= (struct rsn_mdie
*) (kde
->mdie
+ 2);
859 if (kde
->mdie
[1] < sizeof(struct rsn_mdie
) ||
860 os_memcmp(wpa_auth
->conf
.mobility_domain
, mdie
->mobility_domain
,
861 MOBILITY_DOMAIN_ID_LEN
) != 0) {
862 wpa_printf(MSG_DEBUG
, "FT: MDIE mismatch");
866 if (sm
->assoc_resp_ftie
&&
867 (kde
->ftie
[1] != sm
->assoc_resp_ftie
[1] ||
868 os_memcmp(kde
->ftie
, sm
->assoc_resp_ftie
,
869 2 + sm
->assoc_resp_ftie
[1]) != 0)) {
870 wpa_printf(MSG_DEBUG
, "FT: FTIE mismatch");
871 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 2/4",
872 kde
->ftie
, kde
->ftie_len
);
873 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)AssocResp",
874 sm
->assoc_resp_ftie
, 2 + sm
->assoc_resp_ftie
[1]);
880 #endif /* CONFIG_IEEE80211R_AP */
883 static int wpa_receive_error_report(struct wpa_authenticator
*wpa_auth
,
884 struct wpa_state_machine
*sm
, int group
)
886 /* Supplicant reported a Michael MIC error */
887 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
888 "received EAPOL-Key Error Request "
889 "(STA detected Michael MIC failure (group=%d))",
892 if (group
&& wpa_auth
->conf
.wpa_group
!= WPA_CIPHER_TKIP
) {
893 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
894 "ignore Michael MIC failure report since "
895 "group cipher is not TKIP");
896 } else if (!group
&& sm
->pairwise
!= WPA_CIPHER_TKIP
) {
897 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
898 "ignore Michael MIC failure report since "
899 "pairwise cipher is not TKIP");
901 if (wpa_auth_mic_failure_report(wpa_auth
, sm
->addr
) > 0)
902 return 1; /* STA entry was removed */
903 sm
->dot11RSNAStatsTKIPRemoteMICFailures
++;
904 wpa_auth
->dot11RSNAStatsTKIPRemoteMICFailures
++;
908 * Error report is not a request for a new key handshake, but since
909 * Authenticator may do it, let's change the keys now anyway.
911 wpa_request_new_ptk(sm
);
916 static int wpa_try_alt_snonce(struct wpa_state_machine
*sm
, u8
*data
,
921 const u8
*pmk
= NULL
;
925 os_memset(&PTK
, 0, sizeof(PTK
));
927 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
928 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
929 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
930 sm
->p2p_dev_addr
, pmk
, &pmk_len
,
934 #ifdef CONFIG_IEEE80211R_AP
935 if (wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
936 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
937 sm
->xxkey_len
= pmk_len
;
939 #endif /* CONFIG_IEEE80211R_AP */
942 pmk_len
= sm
->pmk_len
;
945 if (wpa_derive_ptk(sm
, sm
->alt_SNonce
, pmk
, pmk_len
, &PTK
, 0) <
949 if (wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
950 data
, data_len
) == 0) {
951 if (sm
->PMK
!= pmk
) {
952 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
953 sm
->pmk_len
= pmk_len
;
959 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
960 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
))
965 wpa_printf(MSG_DEBUG
,
966 "WPA: Earlier SNonce did not result in matching MIC");
970 wpa_printf(MSG_DEBUG
,
971 "WPA: Earlier SNonce resulted in matching MIC");
972 sm
->alt_snonce_valid
= 0;
974 if (vlan_id
&& wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
975 wpa_auth_update_vlan(sm
->wpa_auth
, sm
->addr
, vlan_id
) < 0)
978 os_memcpy(sm
->SNonce
, sm
->alt_SNonce
, WPA_NONCE_LEN
);
979 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
980 forced_memzero(&PTK
, sizeof(PTK
));
981 sm
->PTK_valid
= TRUE
;
987 void wpa_receive(struct wpa_authenticator
*wpa_auth
,
988 struct wpa_state_machine
*sm
,
989 u8
*data
, size_t data_len
)
991 struct ieee802_1x_hdr
*hdr
;
992 struct wpa_eapol_key
*key
;
993 u16 key_info
, key_data_length
;
994 enum { PAIRWISE_2
, PAIRWISE_4
, GROUP_2
, REQUEST
} msg
;
996 struct wpa_eapol_ie_parse kde
;
998 size_t keyhdrlen
, mic_len
;
1001 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
1003 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL data", data
, data_len
);
1005 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
1006 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
1008 if (data_len
< sizeof(*hdr
) + keyhdrlen
) {
1009 wpa_printf(MSG_DEBUG
, "WPA: Ignore too short EAPOL-Key frame");
1013 hdr
= (struct ieee802_1x_hdr
*) data
;
1014 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1015 mic
= (u8
*) (key
+ 1);
1016 key_info
= WPA_GET_BE16(key
->key_info
);
1017 key_data
= mic
+ mic_len
+ 2;
1018 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
1019 wpa_printf(MSG_DEBUG
, "WPA: Received EAPOL-Key from " MACSTR
1020 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
1021 MAC2STR(sm
->addr
), key_info
, key
->type
,
1022 (unsigned int) mic_len
, key_data_length
);
1023 wpa_hexdump(MSG_MSGDUMP
,
1024 "WPA: EAPOL-Key header (ending before Key MIC)",
1026 wpa_hexdump(MSG_MSGDUMP
, "WPA: EAPOL-Key Key MIC",
1028 if (key_data_length
> data_len
- sizeof(*hdr
) - keyhdrlen
) {
1029 wpa_printf(MSG_INFO
, "WPA: Invalid EAPOL-Key frame - "
1030 "key_data overflow (%d > %lu)",
1032 (unsigned long) (data_len
- sizeof(*hdr
) -
1037 if (sm
->wpa
== WPA_VERSION_WPA2
) {
1038 if (key
->type
== EAPOL_KEY_TYPE_WPA
) {
1040 * Some deployed station implementations seem to send
1041 * msg 4/4 with incorrect type value in WPA2 mode.
1043 wpa_printf(MSG_DEBUG
, "Workaround: Allow EAPOL-Key "
1044 "with unexpected WPA type in RSN mode");
1045 } else if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
1046 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
1047 "unexpected type %d in RSN mode",
1052 if (key
->type
!= EAPOL_KEY_TYPE_WPA
) {
1053 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
1054 "unexpected type %d in WPA mode",
1060 wpa_hexdump(MSG_DEBUG
, "WPA: Received Key Nonce", key
->key_nonce
,
1062 wpa_hexdump(MSG_DEBUG
, "WPA: Received Replay Counter",
1063 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1065 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1068 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
1069 wpa_printf(MSG_DEBUG
, "WPA: Ignore SMK message");
1073 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1076 } else if (!(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
1078 msgtxt
= "2/2 Group";
1079 } else if (key_data_length
== 0 ||
1080 (mic_len
== 0 && (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) &&
1081 key_data_length
== AES_BLOCK_SIZE
)) {
1083 msgtxt
= "4/4 Pairwise";
1086 msgtxt
= "2/4 Pairwise";
1089 if (msg
== REQUEST
|| msg
== PAIRWISE_2
|| msg
== PAIRWISE_4
||
1091 u16 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
1092 if (sm
->pairwise
== WPA_CIPHER_CCMP
||
1093 sm
->pairwise
== WPA_CIPHER_GCMP
) {
1094 if (wpa_use_cmac(sm
->wpa_key_mgmt
) &&
1095 !wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1096 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1097 wpa_auth_logger(wpa_auth
, sm
->addr
,
1099 "advertised support for "
1100 "AES-128-CMAC, but did not "
1105 if (!wpa_use_cmac(sm
->wpa_key_mgmt
) &&
1106 !wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1107 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1108 wpa_auth_logger(wpa_auth
, sm
->addr
,
1110 "did not use HMAC-SHA1-AES "
1116 if (wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1117 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
1118 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1119 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1124 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1125 if (sm
->req_replay_counter_used
&&
1126 os_memcmp(key
->replay_counter
, sm
->req_replay_counter
,
1127 WPA_REPLAY_COUNTER_LEN
) <= 0) {
1128 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1129 "received EAPOL-Key request with "
1130 "replayed counter");
1135 if (!(key_info
& WPA_KEY_INFO_REQUEST
) &&
1136 !wpa_replay_counter_valid(sm
->key_replay
, key
->replay_counter
)) {
1139 if (msg
== PAIRWISE_2
&&
1140 wpa_replay_counter_valid(sm
->prev_key_replay
,
1141 key
->replay_counter
) &&
1142 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1143 os_memcmp(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0)
1146 * Some supplicant implementations (e.g., Windows XP
1147 * WZC) update SNonce for each EAPOL-Key 2/4. This
1148 * breaks the workaround on accepting any of the
1149 * pending requests, so allow the SNonce to be updated
1150 * even if we have already sent out EAPOL-Key 3/4.
1152 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1153 "Process SNonce update from STA "
1154 "based on retransmitted EAPOL-Key "
1156 sm
->update_snonce
= 1;
1157 os_memcpy(sm
->alt_SNonce
, sm
->SNonce
, WPA_NONCE_LEN
);
1158 sm
->alt_snonce_valid
= TRUE
;
1159 os_memcpy(sm
->alt_replay_counter
,
1160 sm
->key_replay
[0].counter
,
1161 WPA_REPLAY_COUNTER_LEN
);
1162 goto continue_processing
;
1165 if (msg
== PAIRWISE_4
&& sm
->alt_snonce_valid
&&
1166 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1167 os_memcmp(key
->replay_counter
, sm
->alt_replay_counter
,
1168 WPA_REPLAY_COUNTER_LEN
) == 0) {
1170 * Supplicant may still be using the old SNonce since
1171 * there was two EAPOL-Key 2/4 messages and they had
1172 * different SNonce values.
1174 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1175 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1176 goto continue_processing
;
1179 if (msg
== PAIRWISE_2
&&
1180 wpa_replay_counter_valid(sm
->prev_key_replay
,
1181 key
->replay_counter
) &&
1182 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
) {
1183 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1184 "ignore retransmitted EAPOL-Key %s - "
1185 "SNonce did not change", msgtxt
);
1187 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1188 "received EAPOL-Key %s with "
1189 "unexpected replay counter", msgtxt
);
1191 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
1192 if (!sm
->key_replay
[i
].valid
)
1194 wpa_hexdump(MSG_DEBUG
, "pending replay counter",
1195 sm
->key_replay
[i
].counter
,
1196 WPA_REPLAY_COUNTER_LEN
);
1198 wpa_hexdump(MSG_DEBUG
, "received replay counter",
1199 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1203 continue_processing
:
1205 if (sm
->wpa
== WPA_VERSION_WPA2
&& mic_len
== 0 &&
1206 !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1207 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1208 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1211 #endif /* CONFIG_FILS */
1215 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKSTART
&&
1216 sm
->wpa_ptk_state
!= WPA_PTK_PTKCALCNEGOTIATING
&&
1217 (!sm
->update_snonce
||
1218 sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
)) {
1219 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1220 "received EAPOL-Key msg 2/4 in "
1221 "invalid state (%d) - dropped",
1225 random_add_randomness(key
->key_nonce
, WPA_NONCE_LEN
);
1226 if (sm
->group
->reject_4way_hs_for_entropy
) {
1228 * The system did not have enough entropy to generate
1229 * strong random numbers. Reject the first 4-way
1230 * handshake(s) and collect some entropy based on the
1231 * information from it. Once enough entropy is
1232 * available, the next atempt will trigger GMK/Key
1233 * Counter update and the station will be allowed to
1236 wpa_printf(MSG_DEBUG
, "WPA: Reject 4-way handshake to "
1237 "collect more entropy for random number "
1239 random_mark_pool_ready();
1240 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
1241 WLAN_REASON_PREV_AUTH_NOT_VALID
);
1246 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
||
1248 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1249 "received EAPOL-Key msg 4/4 in "
1250 "invalid state (%d) - dropped",
1256 if (sm
->wpa_ptk_group_state
!= WPA_PTK_GROUP_REKEYNEGOTIATING
1257 || !sm
->PTK_valid
) {
1258 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1259 "received EAPOL-Key msg 2/2 in "
1260 "invalid state (%d) - dropped",
1261 sm
->wpa_ptk_group_state
);
1269 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1270 "received EAPOL-Key frame (%s)", msgtxt
);
1272 if (key_info
& WPA_KEY_INFO_ACK
) {
1273 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1274 "received invalid EAPOL-Key: Key Ack set");
1278 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1279 !(key_info
& WPA_KEY_INFO_MIC
)) {
1280 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1281 "received invalid EAPOL-Key: Key MIC not set");
1286 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1287 (key_info
& WPA_KEY_INFO_MIC
)) {
1288 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1289 "received invalid EAPOL-Key: Key MIC set");
1292 #endif /* CONFIG_FILS */
1294 sm
->MICVerified
= FALSE
;
1295 if (sm
->PTK_valid
&& !sm
->update_snonce
) {
1297 wpa_verify_key_mic(sm
->wpa_key_mgmt
, sm
->pmk_len
, &sm
->PTK
,
1299 (msg
!= PAIRWISE_4
|| !sm
->alt_snonce_valid
||
1300 wpa_try_alt_snonce(sm
, data
, data_len
))) {
1301 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1302 "received EAPOL-Key with invalid MIC");
1304 wpa_printf(MSG_INFO
,
1305 "TEST: Ignore Key MIC failure for fuzz testing");
1307 #endif /* TEST_FUZZ */
1312 wpa_aead_decrypt(sm
, &sm
->PTK
, data
, data_len
,
1313 &key_data_length
) < 0) {
1314 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1315 "received EAPOL-Key with invalid MIC");
1317 wpa_printf(MSG_INFO
,
1318 "TEST: Ignore Key MIC failure for fuzz testing");
1320 #endif /* TEST_FUZZ */
1323 #endif /* CONFIG_FILS */
1326 #endif /* TEST_FUZZ */
1327 sm
->MICVerified
= TRUE
;
1328 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
1329 sm
->pending_1_of_4_timeout
= 0;
1332 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1333 if (sm
->MICVerified
) {
1334 sm
->req_replay_counter_used
= 1;
1335 os_memcpy(sm
->req_replay_counter
, key
->replay_counter
,
1336 WPA_REPLAY_COUNTER_LEN
);
1338 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1339 "received EAPOL-Key request with "
1345 * TODO: should decrypt key data field if encryption was used;
1346 * even though MAC address KDE is not normally encrypted,
1347 * supplicant is allowed to encrypt it.
1349 if (key_info
& WPA_KEY_INFO_ERROR
) {
1350 if (wpa_receive_error_report(
1352 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) > 0)
1353 return; /* STA entry was removed */
1354 } else if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
1355 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1356 "received EAPOL-Key Request for new "
1358 wpa_request_new_ptk(sm
);
1359 } else if (key_data_length
> 0 &&
1360 wpa_parse_kde_ies(key_data
, key_data_length
,
1364 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1365 "received EAPOL-Key Request for GTK "
1367 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
1368 wpa_rekey_gtk(wpa_auth
, NULL
);
1371 /* Do not allow the same key replay counter to be reused. */
1372 wpa_replay_counter_mark_invalid(sm
->key_replay
,
1373 key
->replay_counter
);
1375 if (msg
== PAIRWISE_2
) {
1377 * Maintain a copy of the pending EAPOL-Key frames in
1378 * case the EAPOL-Key frame was retransmitted. This is
1379 * needed to allow EAPOL-Key msg 2/4 reply to another
1380 * pending msg 1/4 to update the SNonce to work around
1381 * unexpected supplicant behavior.
1383 os_memcpy(sm
->prev_key_replay
, sm
->key_replay
,
1384 sizeof(sm
->key_replay
));
1386 os_memset(sm
->prev_key_replay
, 0,
1387 sizeof(sm
->prev_key_replay
));
1391 * Make sure old valid counters are not accepted anymore and
1392 * do not get copied again.
1394 wpa_replay_counter_mark_invalid(sm
->key_replay
, NULL
);
1397 os_free(sm
->last_rx_eapol_key
);
1398 sm
->last_rx_eapol_key
= os_memdup(data
, data_len
);
1399 if (sm
->last_rx_eapol_key
== NULL
)
1401 sm
->last_rx_eapol_key_len
= data_len
;
1403 sm
->rx_eapol_key_secure
= !!(key_info
& WPA_KEY_INFO_SECURE
);
1404 sm
->EAPOLKeyReceived
= TRUE
;
1405 sm
->EAPOLKeyPairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1406 sm
->EAPOLKeyRequest
= !!(key_info
& WPA_KEY_INFO_REQUEST
);
1407 os_memcpy(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
);
1412 static int wpa_gmk_to_gtk(const u8
*gmk
, const char *label
, const u8
*addr
,
1413 const u8
*gnonce
, u8
*gtk
, size_t gtk_len
)
1415 u8 data
[ETH_ALEN
+ WPA_NONCE_LEN
+ 8 + WPA_GTK_MAX_LEN
];
1419 /* GTK = PRF-X(GMK, "Group key expansion",
1420 * AA || GNonce || Time || random data)
1421 * The example described in the IEEE 802.11 standard uses only AA and
1422 * GNonce as inputs here. Add some more entropy since this derivation
1423 * is done only at the Authenticator and as such, does not need to be
1426 os_memset(data
, 0, sizeof(data
));
1427 os_memcpy(data
, addr
, ETH_ALEN
);
1428 os_memcpy(data
+ ETH_ALEN
, gnonce
, WPA_NONCE_LEN
);
1429 pos
= data
+ ETH_ALEN
+ WPA_NONCE_LEN
;
1430 wpa_get_ntp_timestamp(pos
);
1432 os_memset(pos
, 0xef, 8);
1433 #endif /* TEST_FUZZ */
1435 if (random_get_bytes(pos
, gtk_len
) < 0)
1438 #ifdef CONFIG_SHA384
1439 if (sha384_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1442 #else /* CONFIG_SHA384 */
1443 #ifdef CONFIG_SHA256
1444 if (sha256_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1447 #else /* CONFIG_SHA256 */
1448 if (sha1_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1451 #endif /* CONFIG_SHA256 */
1452 #endif /* CONFIG_SHA384 */
1454 forced_memzero(data
, sizeof(data
));
1460 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
)
1462 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1463 struct wpa_state_machine
*sm
= timeout_ctx
;
1465 sm
->pending_1_of_4_timeout
= 0;
1466 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "EAPOL-Key timeout");
1467 sm
->TimeoutEvt
= TRUE
;
1472 void __wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1473 struct wpa_state_machine
*sm
, int key_info
,
1474 const u8
*key_rsc
, const u8
*nonce
,
1475 const u8
*kde
, size_t kde_len
,
1476 int keyidx
, int encr
, int force_version
)
1478 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
1479 struct ieee802_1x_hdr
*hdr
;
1480 struct wpa_eapol_key
*key
;
1481 size_t len
, mic_len
, keyhdrlen
;
1483 int key_data_len
, pad_len
= 0;
1485 int version
, pairwise
;
1487 u8
*key_mic
, *key_data
;
1489 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
1490 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
1492 len
= sizeof(struct ieee802_1x_hdr
) + keyhdrlen
;
1495 version
= force_version
;
1496 else if (wpa_use_akm_defined(sm
->wpa_key_mgmt
))
1497 version
= WPA_KEY_INFO_TYPE_AKM_DEFINED
;
1498 else if (wpa_use_cmac(sm
->wpa_key_mgmt
))
1499 version
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
1500 else if (sm
->pairwise
!= WPA_CIPHER_TKIP
)
1501 version
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
1503 version
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
1505 pairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1507 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1508 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1511 (key_info
& WPA_KEY_INFO_SECURE
) ? 1 : 0,
1512 (key_info
& WPA_KEY_INFO_MIC
) ? 1 : 0,
1513 (key_info
& WPA_KEY_INFO_ACK
) ? 1 : 0,
1514 (key_info
& WPA_KEY_INFO_INSTALL
) ? 1 : 0,
1515 pairwise
, (unsigned long) kde_len
, keyidx
, encr
);
1517 key_data_len
= kde_len
;
1519 if ((version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1520 wpa_use_aes_key_wrap(sm
->wpa_key_mgmt
) ||
1521 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) && encr
) {
1522 pad_len
= key_data_len
% 8;
1524 pad_len
= 8 - pad_len
;
1525 key_data_len
+= pad_len
+ 8;
1528 len
+= key_data_len
;
1529 if (!mic_len
&& encr
)
1530 len
+= AES_BLOCK_SIZE
;
1532 hdr
= os_zalloc(len
);
1535 hdr
->version
= conf
->eapol_version
;
1536 hdr
->type
= IEEE802_1X_TYPE_EAPOL_KEY
;
1537 hdr
->length
= host_to_be16(len
- sizeof(*hdr
));
1538 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1539 key_mic
= (u8
*) (key
+ 1);
1540 key_data
= ((u8
*) (hdr
+ 1)) + keyhdrlen
;
1542 key
->type
= sm
->wpa
== WPA_VERSION_WPA2
?
1543 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1544 key_info
|= version
;
1545 if (encr
&& sm
->wpa
== WPA_VERSION_WPA2
)
1546 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1547 if (sm
->wpa
!= WPA_VERSION_WPA2
)
1548 key_info
|= keyidx
<< WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1549 WPA_PUT_BE16(key
->key_info
, key_info
);
1551 alg
= pairwise
? sm
->pairwise
: conf
->wpa_group
;
1552 if (sm
->wpa
== WPA_VERSION_WPA2
&& !pairwise
)
1553 WPA_PUT_BE16(key
->key_length
, 0);
1555 WPA_PUT_BE16(key
->key_length
, wpa_cipher_key_len(alg
));
1557 for (i
= RSNA_MAX_EAPOL_RETRIES
- 1; i
> 0; i
--) {
1558 sm
->key_replay
[i
].valid
= sm
->key_replay
[i
- 1].valid
;
1559 os_memcpy(sm
->key_replay
[i
].counter
,
1560 sm
->key_replay
[i
- 1].counter
,
1561 WPA_REPLAY_COUNTER_LEN
);
1563 inc_byte_array(sm
->key_replay
[0].counter
, WPA_REPLAY_COUNTER_LEN
);
1564 os_memcpy(key
->replay_counter
, sm
->key_replay
[0].counter
,
1565 WPA_REPLAY_COUNTER_LEN
);
1566 wpa_hexdump(MSG_DEBUG
, "WPA: Replay Counter",
1567 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1568 sm
->key_replay
[0].valid
= TRUE
;
1571 os_memcpy(key
->key_nonce
, nonce
, WPA_NONCE_LEN
);
1574 os_memcpy(key
->key_rsc
, key_rsc
, WPA_KEY_RSC_LEN
);
1577 os_memcpy(key_data
, kde
, kde_len
);
1578 WPA_PUT_BE16(key_mic
+ mic_len
, kde_len
);
1580 } else if (!mic_len
&& kde
) {
1584 WPA_PUT_BE16(key_mic
, AES_BLOCK_SIZE
+ kde_len
);
1585 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1588 wpa_hexdump_key(MSG_DEBUG
, "WPA: KEK",
1589 sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1590 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1591 * to Key Data (exclusive). */
1592 aad
[0] = (u8
*) hdr
;
1593 aad_len
[0] = key_mic
+ 2 - (u8
*) hdr
;
1594 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, kde
, kde_len
,
1595 1, aad
, aad_len
, key_mic
+ 2) < 0) {
1596 wpa_printf(MSG_DEBUG
, "WPA: AES-SIV encryption failed");
1600 wpa_hexdump(MSG_DEBUG
, "WPA: Encrypted Key Data from SIV",
1601 key_mic
+ 2, AES_BLOCK_SIZE
+ kde_len
);
1602 #endif /* CONFIG_FILS */
1603 } else if (encr
&& kde
) {
1604 buf
= os_zalloc(key_data_len
);
1610 os_memcpy(pos
, kde
, kde_len
);
1616 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1618 if (version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1619 wpa_use_aes_key_wrap(sm
->wpa_key_mgmt
) ||
1620 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1621 wpa_printf(MSG_DEBUG
,
1622 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1623 (unsigned int) sm
->PTK
.kek_len
);
1624 if (aes_wrap(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
1625 (key_data_len
- 8) / 8, buf
, key_data
)) {
1630 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1631 #ifndef CONFIG_NO_RC4
1632 } else if (sm
->PTK
.kek_len
== 16) {
1635 wpa_printf(MSG_DEBUG
,
1636 "WPA: Encrypt Key Data using RC4");
1637 os_memcpy(key
->key_iv
,
1638 sm
->group
->Counter
+ WPA_NONCE_LEN
- 16, 16);
1639 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1640 os_memcpy(ek
, key
->key_iv
, 16);
1641 os_memcpy(ek
+ 16, sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1642 os_memcpy(key_data
, buf
, key_data_len
);
1643 rc4_skip(ek
, 32, 256, key_data
, key_data_len
);
1644 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1645 #endif /* CONFIG_NO_RC4 */
1654 if (key_info
& WPA_KEY_INFO_MIC
) {
1655 if (!sm
->PTK_valid
|| !mic_len
) {
1656 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1657 "PTK not valid when sending EAPOL-Key "
1663 if (wpa_eapol_key_mic(sm
->PTK
.kck
, sm
->PTK
.kck_len
,
1664 sm
->wpa_key_mgmt
, version
,
1665 (u8
*) hdr
, len
, key_mic
) < 0) {
1669 #ifdef CONFIG_TESTING_OPTIONS
1671 conf
->corrupt_gtk_rekey_mic_probability
> 0.0 &&
1672 drand48() < conf
->corrupt_gtk_rekey_mic_probability
) {
1673 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1674 "Corrupting group EAPOL-Key Key MIC");
1677 #endif /* CONFIG_TESTING_OPTIONS */
1680 wpa_auth_set_eapol(wpa_auth
, sm
->addr
, WPA_EAPOL_inc_EapolFramesTx
, 1);
1681 wpa_auth_send_eapol(wpa_auth
, sm
->addr
, (u8
*) hdr
, len
,
1687 static void wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1688 struct wpa_state_machine
*sm
, int key_info
,
1689 const u8
*key_rsc
, const u8
*nonce
,
1690 const u8
*kde
, size_t kde_len
,
1691 int keyidx
, int encr
)
1694 int pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
1700 __wpa_send_eapol(wpa_auth
, sm
, key_info
, key_rsc
, nonce
, kde
, kde_len
,
1703 ctr
= pairwise
? sm
->TimeoutCtr
: sm
->GTimeoutCtr
;
1704 if (ctr
== 1 && wpa_auth
->conf
.tx_status
)
1705 timeout_ms
= pairwise
? eapol_key_timeout_first
:
1706 eapol_key_timeout_first_group
;
1708 timeout_ms
= eapol_key_timeout_subseq
;
1709 if (wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
1710 (!pairwise
|| (key_info
& WPA_KEY_INFO_MIC
)))
1711 timeout_ms
= eapol_key_timeout_no_retrans
;
1712 if (pairwise
&& ctr
== 1 && !(key_info
& WPA_KEY_INFO_MIC
))
1713 sm
->pending_1_of_4_timeout
= 1;
1716 #endif /* TEST_FUZZ */
1717 wpa_printf(MSG_DEBUG
, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1718 "counter %u)", timeout_ms
, ctr
);
1719 eloop_register_timeout(timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000,
1720 wpa_send_eapol_timeout
, wpa_auth
, sm
);
1724 static int wpa_verify_key_mic(int akmp
, size_t pmk_len
, struct wpa_ptk
*PTK
,
1725 u8
*data
, size_t data_len
)
1727 struct ieee802_1x_hdr
*hdr
;
1728 struct wpa_eapol_key
*key
;
1731 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
], *mic_pos
;
1732 size_t mic_len
= wpa_mic_len(akmp
, pmk_len
);
1734 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
1737 hdr
= (struct ieee802_1x_hdr
*) data
;
1738 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1739 mic_pos
= (u8
*) (key
+ 1);
1740 key_info
= WPA_GET_BE16(key
->key_info
);
1741 os_memcpy(mic
, mic_pos
, mic_len
);
1742 os_memset(mic_pos
, 0, mic_len
);
1743 if (wpa_eapol_key_mic(PTK
->kck
, PTK
->kck_len
, akmp
,
1744 key_info
& WPA_KEY_INFO_TYPE_MASK
,
1745 data
, data_len
, mic_pos
) ||
1746 os_memcmp_const(mic
, mic_pos
, mic_len
) != 0)
1748 os_memcpy(mic_pos
, mic
, mic_len
);
1753 void wpa_remove_ptk(struct wpa_state_machine
*sm
)
1755 sm
->PTK_valid
= FALSE
;
1756 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1757 if (wpa_auth_set_key(sm
->wpa_auth
, 0, WPA_ALG_NONE
, sm
->addr
, 0, NULL
,
1758 0, KEY_FLAG_PAIRWISE
))
1759 wpa_printf(MSG_DEBUG
,
1760 "RSN: PTK removal from the driver failed");
1761 if (sm
->wpa_auth
->conf
.extended_key_id
&& sm
->use_ext_key_id
&&
1762 wpa_auth_set_key(sm
->wpa_auth
, 0, WPA_ALG_NONE
, sm
->addr
, 1, NULL
,
1763 0, KEY_FLAG_PAIRWISE
))
1764 wpa_printf(MSG_DEBUG
,
1765 "RSN: PTK Key ID 1 removal from the driver failed");
1766 sm
->pairwise_set
= FALSE
;
1767 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1771 int wpa_auth_sm_event(struct wpa_state_machine
*sm
, enum wpa_event event
)
1778 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1779 "event %d notification", event
);
1784 /* PTKs are derived through AMPE */
1785 if (wpa_auth_start_ampe(sm
->wpa_auth
, sm
->addr
)) {
1790 #endif /* CONFIG_MESH */
1795 sm
->DeauthenticationRequest
= TRUE
;
1796 #ifdef CONFIG_IEEE80211R_AP
1797 os_memset(sm
->PMK
, 0, sizeof(sm
->PMK
));
1799 os_memset(sm
->xxkey
, 0, sizeof(sm
->xxkey
));
1801 os_memset(sm
->pmk_r1
, 0, sizeof(sm
->pmk_r1
));
1803 #endif /* CONFIG_IEEE80211R_AP */
1806 case WPA_REAUTH_EAPOL
:
1809 * When using WPS, we may end up here if the STA
1810 * manages to re-associate without the previous STA
1811 * entry getting removed. Consequently, we need to make
1812 * sure that the WPA state machines gets initialized
1813 * properly at this point.
1815 wpa_printf(MSG_DEBUG
, "WPA state machine had not been "
1816 "started - initialize now");
1819 if (wpa_sm_step(sm
) == 1)
1820 return 1; /* should not really happen */
1822 sm
->AuthenticationRequest
= TRUE
;
1826 if (!sm
->use_ext_key_id
&&
1827 sm
->wpa_auth
->conf
.wpa_deny_ptk0_rekey
) {
1828 wpa_printf(MSG_INFO
,
1829 "WPA: PTK0 rekey not allowed, disconnect "
1830 MACSTR
, MAC2STR(sm
->addr
));
1831 sm
->Disconnect
= TRUE
;
1832 /* Try to encourage the STA to reconnect */
1833 sm
->disconnect_reason
=
1834 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA
;
1838 if (sm
->use_ext_key_id
)
1839 sm
->keyidx_active
^= 1; /* flip Key ID */
1841 if (sm
->GUpdateStationKeys
) {
1843 * Reauthentication cancels the pending group key
1844 * update for this STA.
1846 sm
->group
->GKeyDoneStations
--;
1847 sm
->GUpdateStationKeys
= FALSE
;
1848 sm
->PtkGroupInit
= TRUE
;
1850 sm
->ReAuthenticationRequest
= TRUE
;
1853 #ifdef CONFIG_IEEE80211R_AP
1854 wpa_printf(MSG_DEBUG
, "FT: Retry PTK configuration "
1855 "after association");
1856 wpa_ft_install_ptk(sm
);
1858 /* Using FT protocol, not WPA auth state machine */
1859 sm
->ft_completed
= 1;
1860 wpa_auth_set_ptk_rekey_timer(sm
);
1862 #else /* CONFIG_IEEE80211R_AP */
1864 #endif /* CONFIG_IEEE80211R_AP */
1865 case WPA_ASSOC_FILS
:
1867 wpa_printf(MSG_DEBUG
,
1868 "FILS: TK configuration after association");
1870 sm
->fils_completed
= 1;
1872 #else /* CONFIG_FILS */
1874 #endif /* CONFIG_FILS */
1875 case WPA_DRV_STA_REMOVED
:
1876 sm
->tk_already_set
= FALSE
;
1880 #ifdef CONFIG_IEEE80211R_AP
1881 sm
->ft_completed
= 0;
1882 #endif /* CONFIG_IEEE80211R_AP */
1884 if (sm
->mgmt_frame_prot
&& event
== WPA_AUTH
)
1887 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1888 (event
== WPA_AUTH
|| event
== WPA_ASSOC
))
1890 #endif /* CONFIG_FILS */
1893 sm
->PTK_valid
= FALSE
;
1894 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1896 if (event
!= WPA_REAUTH_EAPOL
)
1900 if (sm
->in_step_loop
) {
1902 * wpa_sm_step() is already running - avoid recursive call to
1903 * it by making the existing loop process the new update.
1908 return wpa_sm_step(sm
);
1912 SM_STATE(WPA_PTK
, INITIALIZE
)
1914 SM_ENTRY_MA(WPA_PTK
, INITIALIZE
, wpa_ptk
);
1916 /* Init flag is not cleared here, so avoid busy
1917 * loop by claiming nothing changed. */
1918 sm
->changed
= FALSE
;
1922 if (sm
->GUpdateStationKeys
)
1923 sm
->group
->GKeyDoneStations
--;
1924 sm
->GUpdateStationKeys
= FALSE
;
1925 if (sm
->wpa
== WPA_VERSION_WPA
)
1926 sm
->PInitAKeys
= FALSE
;
1927 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1928 * Local AA > Remote AA)) */) {
1931 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 0);
1933 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
, 0);
1935 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
1936 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1937 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
1938 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1939 WPA_EAPOL_authorized
, 0);
1944 SM_STATE(WPA_PTK
, DISCONNECT
)
1946 u16 reason
= sm
->disconnect_reason
;
1948 SM_ENTRY_MA(WPA_PTK
, DISCONNECT
, wpa_ptk
);
1949 sm
->Disconnect
= FALSE
;
1950 sm
->disconnect_reason
= 0;
1952 reason
= WLAN_REASON_PREV_AUTH_NOT_VALID
;
1953 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
, reason
);
1957 SM_STATE(WPA_PTK
, DISCONNECTED
)
1959 SM_ENTRY_MA(WPA_PTK
, DISCONNECTED
, wpa_ptk
);
1960 sm
->DeauthenticationRequest
= FALSE
;
1964 SM_STATE(WPA_PTK
, AUTHENTICATION
)
1966 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION
, wpa_ptk
);
1967 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1968 sm
->PTK_valid
= FALSE
;
1969 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portControl_Auto
,
1971 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 1);
1972 sm
->AuthenticationRequest
= FALSE
;
1976 static void wpa_group_ensure_init(struct wpa_authenticator
*wpa_auth
,
1977 struct wpa_group
*group
)
1979 if (group
->first_sta_seen
)
1982 * System has run bit further than at the time hostapd was started
1983 * potentially very early during boot up. This provides better chances
1984 * of collecting more randomness on embedded systems. Re-initialize the
1985 * GMK and Counter here to improve their strength if there was not
1986 * enough entropy available immediately after system startup.
1988 wpa_printf(MSG_DEBUG
, "WPA: Re-initialize GMK/Counter on first "
1990 if (random_pool_ready() != 1) {
1991 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
1992 "to proceed - reject first 4-way handshake");
1993 group
->reject_4way_hs_for_entropy
= TRUE
;
1995 group
->first_sta_seen
= TRUE
;
1996 group
->reject_4way_hs_for_entropy
= FALSE
;
1999 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0 ||
2000 wpa_gtk_update(wpa_auth
, group
) < 0 ||
2001 wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
2002 wpa_printf(MSG_INFO
, "WPA: GMK/GTK setup failed");
2003 group
->first_sta_seen
= FALSE
;
2004 group
->reject_4way_hs_for_entropy
= TRUE
;
2009 SM_STATE(WPA_PTK
, AUTHENTICATION2
)
2011 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION2
, wpa_ptk
);
2013 wpa_group_ensure_init(sm
->wpa_auth
, sm
->group
);
2014 sm
->ReAuthenticationRequest
= FALSE
;
2017 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
2018 * ambiguous. The Authenticator state machine uses a counter that is
2019 * incremented by one for each 4-way handshake. However, the security
2020 * analysis of 4-way handshake points out that unpredictable nonces
2021 * help in preventing precomputation attacks. Instead of the state
2022 * machine definition, use an unpredictable nonce value here to provide
2023 * stronger protection against potential precomputation attacks.
2025 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
2026 wpa_printf(MSG_ERROR
, "WPA: Failed to get random data for "
2028 sm
->Disconnect
= TRUE
;
2031 wpa_hexdump(MSG_DEBUG
, "WPA: Assign ANonce", sm
->ANonce
,
2033 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
2034 * logical place than INITIALIZE since AUTHENTICATION2 can be
2035 * re-entered on ReAuthenticationRequest without going through
2041 static int wpa_auth_sm_ptk_update(struct wpa_state_machine
*sm
)
2043 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
2044 wpa_printf(MSG_ERROR
,
2045 "WPA: Failed to get random data for ANonce");
2046 sm
->Disconnect
= TRUE
;
2049 wpa_hexdump(MSG_DEBUG
, "WPA: Assign new ANonce", sm
->ANonce
,
2056 SM_STATE(WPA_PTK
, INITPMK
)
2058 u8 msk
[2 * PMK_LEN
];
2059 size_t len
= 2 * PMK_LEN
;
2061 SM_ENTRY_MA(WPA_PTK
, INITPMK
, wpa_ptk
);
2062 #ifdef CONFIG_IEEE80211R_AP
2064 #endif /* CONFIG_IEEE80211R_AP */
2066 wpa_printf(MSG_DEBUG
, "WPA: PMK from PMKSA cache");
2067 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2068 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
2070 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
) {
2071 wpa_printf(MSG_DEBUG
,
2072 "DPP: No PMKSA cache entry for STA - reject connection");
2073 sm
->Disconnect
= TRUE
;
2074 sm
->disconnect_reason
= WLAN_REASON_INVALID_PMKID
;
2076 #endif /* CONFIG_DPP */
2077 } else if (wpa_auth_get_msk(sm
->wpa_auth
, sm
->addr
, msk
, &len
) == 0) {
2078 unsigned int pmk_len
;
2080 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
))
2081 pmk_len
= PMK_LEN_SUITE_B_192
;
2084 wpa_printf(MSG_DEBUG
, "WPA: PMK from EAPOL state machine "
2085 "(MSK len=%lu PMK len=%u)", (unsigned long) len
,
2087 if (len
< pmk_len
) {
2088 wpa_printf(MSG_DEBUG
,
2089 "WPA: MSK not long enough (%u) to create PMK (%u)",
2090 (unsigned int) len
, (unsigned int) pmk_len
);
2091 sm
->Disconnect
= TRUE
;
2094 os_memcpy(sm
->PMK
, msk
, pmk_len
);
2095 sm
->pmk_len
= pmk_len
;
2096 #ifdef CONFIG_IEEE80211R_AP
2097 if (len
>= 2 * PMK_LEN
) {
2098 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
2099 os_memcpy(sm
->xxkey
, msk
, SHA384_MAC_LEN
);
2100 sm
->xxkey_len
= SHA384_MAC_LEN
;
2102 os_memcpy(sm
->xxkey
, msk
+ PMK_LEN
, PMK_LEN
);
2103 sm
->xxkey_len
= PMK_LEN
;
2106 #endif /* CONFIG_IEEE80211R_AP */
2108 wpa_printf(MSG_DEBUG
, "WPA: Could not get PMK, get_msk: %p",
2109 sm
->wpa_auth
->cb
->get_msk
);
2110 sm
->Disconnect
= TRUE
;
2113 forced_memzero(msk
, sizeof(msk
));
2115 sm
->req_replay_counter_used
= 0;
2116 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
2117 * will break reauthentication since EAPOL state machines may not be
2118 * get into AUTHENTICATING state that clears keyRun before WPA state
2119 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2120 * state and takes PMK from the previously used AAA Key. This will
2121 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2122 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2123 * be good workaround for this issue. */
2124 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyRun
, 0);
2128 SM_STATE(WPA_PTK
, INITPSK
)
2133 SM_ENTRY_MA(WPA_PTK
, INITPSK
, wpa_ptk
);
2134 psk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
, NULL
,
2137 os_memcpy(sm
->PMK
, psk
, psk_len
);
2138 sm
->pmk_len
= psk_len
;
2139 #ifdef CONFIG_IEEE80211R_AP
2140 os_memcpy(sm
->xxkey
, psk
, PMK_LEN
);
2141 sm
->xxkey_len
= PMK_LEN
;
2142 #endif /* CONFIG_IEEE80211R_AP */
2145 if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
2146 wpa_printf(MSG_DEBUG
, "SAE: PMK from PMKSA cache");
2147 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2148 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
2149 #ifdef CONFIG_IEEE80211R_AP
2150 os_memcpy(sm
->xxkey
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2151 sm
->xxkey_len
= sm
->pmksa
->pmk_len
;
2152 #endif /* CONFIG_IEEE80211R_AP */
2154 #endif /* CONFIG_SAE */
2155 sm
->req_replay_counter_used
= 0;
2159 SM_STATE(WPA_PTK
, PTKSTART
)
2161 u8 buf
[2 + RSN_SELECTOR_LEN
+ PMKID_LEN
], *pmkid
= NULL
;
2162 size_t pmkid_len
= 0;
2164 SM_ENTRY_MA(WPA_PTK
, PTKSTART
, wpa_ptk
);
2165 sm
->PTKRequest
= FALSE
;
2166 sm
->TimeoutEvt
= FALSE
;
2167 sm
->alt_snonce_valid
= FALSE
;
2170 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
2171 /* No point in sending the EAPOL-Key - we will disconnect
2172 * immediately following this. */
2176 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2177 "sending 1/4 msg of 4-Way Handshake");
2179 * For infrastructure BSS cases, it is better for the AP not to include
2180 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2181 * offline search for the passphrase/PSK without having to be able to
2182 * capture a 4-way handshake from a STA that has access to the network.
2184 * For IBSS cases, addition of PMKID KDE could be considered even with
2185 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2186 * possible PSK for this STA. However, this should not be done unless
2187 * there is support for using that information on the supplicant side.
2188 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2189 * cases would also apply here, but at least in the IBSS case, this
2190 * would cover a potential real use case.
2192 if (sm
->wpa
== WPA_VERSION_WPA2
&&
2193 (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) ||
2194 (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
&& sm
->pmksa
) ||
2195 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) &&
2196 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
) {
2198 pmkid_len
= 2 + RSN_SELECTOR_LEN
+ PMKID_LEN
;
2199 pmkid
[0] = WLAN_EID_VENDOR_SPECIFIC
;
2200 pmkid
[1] = RSN_SELECTOR_LEN
+ PMKID_LEN
;
2201 RSN_SELECTOR_PUT(&pmkid
[2], RSN_KEY_DATA_PMKID
);
2203 wpa_hexdump(MSG_DEBUG
,
2204 "RSN: Message 1/4 PMKID from PMKSA entry",
2205 sm
->pmksa
->pmkid
, PMKID_LEN
);
2206 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2207 sm
->pmksa
->pmkid
, PMKID_LEN
);
2208 } else if (wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
)) {
2209 /* No KCK available to derive PMKID */
2210 wpa_printf(MSG_DEBUG
,
2211 "RSN: No KCK available to derive PMKID for message 1/4");
2214 } else if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2215 if (sm
->pmkid_set
) {
2216 wpa_hexdump(MSG_DEBUG
,
2217 "RSN: Message 1/4 PMKID from FILS/ERP",
2218 sm
->pmkid
, PMKID_LEN
);
2219 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2220 sm
->pmkid
, PMKID_LEN
);
2222 /* No PMKID available */
2223 wpa_printf(MSG_DEBUG
,
2224 "RSN: No FILS/ERP PMKID available for message 1/4");
2227 #endif /* CONFIG_FILS */
2228 #ifdef CONFIG_IEEE80211R_AP
2229 } else if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
) &&
2231 wpa_printf(MSG_DEBUG
,
2232 "FT: No PMKID in message 1/4 when using FT protocol");
2234 #endif /* CONFIG_IEEE80211R_AP */
2236 } else if (wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
2237 if (sm
->pmkid_set
) {
2238 wpa_hexdump(MSG_DEBUG
,
2239 "RSN: Message 1/4 PMKID from SAE",
2240 sm
->pmkid
, PMKID_LEN
);
2241 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2242 sm
->pmkid
, PMKID_LEN
);
2244 /* No PMKID available */
2245 wpa_printf(MSG_DEBUG
,
2246 "RSN: No SAE PMKID available for message 1/4");
2249 #endif /* CONFIG_SAE */
2252 * Calculate PMKID since no PMKSA cache entry was
2253 * available with pre-calculated PMKID.
2255 rsn_pmkid(sm
->PMK
, sm
->pmk_len
, sm
->wpa_auth
->addr
,
2256 sm
->addr
, &pmkid
[2 + RSN_SELECTOR_LEN
],
2258 wpa_hexdump(MSG_DEBUG
,
2259 "RSN: Message 1/4 PMKID derived from PMK",
2260 &pmkid
[2 + RSN_SELECTOR_LEN
], PMKID_LEN
);
2265 wpa_send_eapol(sm
->wpa_auth
, sm
,
2266 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
2267 sm
->ANonce
, pmkid
, pmkid_len
, 0, 0);
2271 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
2272 const u8
*pmk
, unsigned int pmk_len
,
2273 struct wpa_ptk
*ptk
, int force_sha256
)
2279 #ifdef CONFIG_IEEE80211R_AP
2280 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2281 if (sm
->ft_completed
) {
2282 u8 ptk_name
[WPA_PMK_NAME_LEN
];
2284 return wpa_pmk_r1_to_ptk(sm
->pmk_r1
, sm
->pmk_r1_len
,
2285 sm
->SNonce
, sm
->ANonce
,
2286 sm
->addr
, sm
->wpa_auth
->addr
,
2292 return wpa_auth_derive_ptk_ft(sm
, ptk
);
2294 #endif /* CONFIG_IEEE80211R_AP */
2297 if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->dpp_z
) {
2298 z
= wpabuf_head(sm
->dpp_z
);
2299 z_len
= wpabuf_len(sm
->dpp_z
);
2301 #endif /* CONFIG_DPP2 */
2303 akmp
= sm
->wpa_key_mgmt
;
2305 akmp
|= WPA_KEY_MGMT_PSK_SHA256
;
2306 return wpa_pmk_to_ptk(pmk
, pmk_len
, "Pairwise key expansion",
2307 sm
->wpa_auth
->addr
, sm
->addr
, sm
->ANonce
, snonce
,
2308 ptk
, akmp
, sm
->pairwise
, z
, z_len
);
2314 int fils_auth_pmk_to_ptk(struct wpa_state_machine
*sm
, const u8
*pmk
,
2315 size_t pmk_len
, const u8
*snonce
, const u8
*anonce
,
2316 const u8
*dhss
, size_t dhss_len
,
2317 struct wpabuf
*g_sta
, struct wpabuf
*g_ap
)
2319 u8 ick
[FILS_ICK_MAX_LEN
];
2322 u8 fils_ft
[FILS_FT_MAX_LEN
];
2323 size_t fils_ft_len
= 0;
2325 res
= fils_pmk_to_ptk(pmk
, pmk_len
, sm
->addr
, sm
->wpa_auth
->addr
,
2326 snonce
, anonce
, dhss
, dhss_len
,
2327 &sm
->PTK
, ick
, &ick_len
,
2328 sm
->wpa_key_mgmt
, sm
->pairwise
,
2329 fils_ft
, &fils_ft_len
);
2332 sm
->PTK_valid
= TRUE
;
2333 sm
->tk_already_set
= FALSE
;
2335 #ifdef CONFIG_IEEE80211R_AP
2337 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2338 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
2339 u8 pmk_r0
[PMK_LEN_MAX
], pmk_r0_name
[WPA_PMK_NAME_LEN
];
2340 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
2341 size_t pmk_r0_len
= use_sha384
? SHA384_MAC_LEN
: PMK_LEN
;
2343 if (wpa_derive_pmk_r0(fils_ft
, fils_ft_len
,
2344 conf
->ssid
, conf
->ssid_len
,
2345 conf
->mobility_domain
,
2346 conf
->r0_key_holder
,
2347 conf
->r0_key_holder_len
,
2348 sm
->addr
, pmk_r0
, pmk_r0_name
,
2352 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: PMK-R0",
2353 pmk_r0
, pmk_r0_len
);
2354 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR0Name",
2355 pmk_r0_name
, WPA_PMK_NAME_LEN
);
2356 wpa_ft_store_pmk_fils(sm
, pmk_r0
, pmk_r0_name
);
2357 forced_memzero(fils_ft
, sizeof(fils_ft
));
2359 res
= wpa_derive_pmk_r1_name(pmk_r0_name
, conf
->r1_key_holder
,
2360 sm
->addr
, sm
->pmk_r1_name
,
2362 forced_memzero(pmk_r0
, PMK_LEN_MAX
);
2365 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR1Name", sm
->pmk_r1_name
,
2367 sm
->pmk_r1_name_valid
= 1;
2369 #endif /* CONFIG_IEEE80211R_AP */
2371 res
= fils_key_auth_sk(ick
, ick_len
, snonce
, anonce
,
2372 sm
->addr
, sm
->wpa_auth
->addr
,
2373 g_sta
? wpabuf_head(g_sta
) : NULL
,
2374 g_sta
? wpabuf_len(g_sta
) : 0,
2375 g_ap
? wpabuf_head(g_ap
) : NULL
,
2376 g_ap
? wpabuf_len(g_ap
) : 0,
2377 sm
->wpa_key_mgmt
, sm
->fils_key_auth_sta
,
2378 sm
->fils_key_auth_ap
,
2379 &sm
->fils_key_auth_len
);
2380 forced_memzero(ick
, sizeof(ick
));
2382 /* Store nonces for (Re)Association Request/Response frame processing */
2383 os_memcpy(sm
->SNonce
, snonce
, FILS_NONCE_LEN
);
2384 os_memcpy(sm
->ANonce
, anonce
, FILS_NONCE_LEN
);
2390 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
2391 u8
*buf
, size_t buf_len
, u16
*_key_data_len
)
2393 struct ieee802_1x_hdr
*hdr
;
2394 struct wpa_eapol_key
*key
;
2401 hdr
= (struct ieee802_1x_hdr
*) buf
;
2402 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2403 pos
= (u8
*) (key
+ 1);
2404 key_data_len
= WPA_GET_BE16(pos
);
2405 if (key_data_len
< AES_BLOCK_SIZE
||
2406 key_data_len
> buf_len
- sizeof(*hdr
) - sizeof(*key
) - 2) {
2407 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2408 "No room for AES-SIV data in the frame");
2411 pos
+= 2; /* Pointing at the Encrypted Key Data field */
2413 tmp
= os_malloc(key_data_len
);
2417 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2418 * to Key Data (exclusive). */
2420 aad_len
[0] = pos
- buf
;
2421 if (aes_siv_decrypt(ptk
->kek
, ptk
->kek_len
, pos
, key_data_len
,
2422 1, aad
, aad_len
, tmp
) < 0) {
2423 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2424 "Invalid AES-SIV data in the frame");
2425 bin_clear_free(tmp
, key_data_len
);
2429 /* AEAD decryption and validation completed successfully */
2430 key_data_len
-= AES_BLOCK_SIZE
;
2431 wpa_hexdump_key(MSG_DEBUG
, "WPA: Decrypted Key Data",
2434 /* Replace Key Data field with the decrypted version */
2435 os_memcpy(pos
, tmp
, key_data_len
);
2436 pos
-= 2; /* Key Data Length field */
2437 WPA_PUT_BE16(pos
, key_data_len
);
2438 bin_clear_free(tmp
, key_data_len
);
2440 *_key_data_len
= key_data_len
;
2445 const u8
* wpa_fils_validate_fils_session(struct wpa_state_machine
*sm
,
2446 const u8
*ies
, size_t ies_len
,
2447 const u8
*fils_session
)
2450 const u8
*session
= NULL
;
2452 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2453 wpa_printf(MSG_DEBUG
,
2454 "FILS: Not a FILS AKM - reject association");
2458 /* Verify Session element */
2460 end
= ((const u8
*) ie
) + ies_len
;
2461 while (ie
+ 1 < end
) {
2462 if (ie
+ 2 + ie
[1] > end
)
2464 if (ie
[0] == WLAN_EID_EXTENSION
&&
2465 ie
[1] >= 1 + FILS_SESSION_LEN
&&
2466 ie
[2] == WLAN_EID_EXT_FILS_SESSION
) {
2474 wpa_printf(MSG_DEBUG
,
2475 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2480 if (!fils_session
) {
2481 wpa_printf(MSG_DEBUG
,
2482 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2487 if (os_memcmp(fils_session
, session
+ 3, FILS_SESSION_LEN
) != 0) {
2488 wpa_printf(MSG_DEBUG
, "FILS: Session mismatch");
2489 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
2490 fils_session
, FILS_SESSION_LEN
);
2491 wpa_hexdump(MSG_DEBUG
, "FILS: Received FILS Session",
2492 session
+ 3, FILS_SESSION_LEN
);
2499 int wpa_fils_validate_key_confirm(struct wpa_state_machine
*sm
, const u8
*ies
,
2502 struct ieee802_11_elems elems
;
2504 if (ieee802_11_parse_elems(ies
, ies_len
, &elems
, 1) == ParseFailed
) {
2505 wpa_printf(MSG_DEBUG
,
2506 "FILS: Failed to parse decrypted elements");
2510 if (!elems
.fils_session
) {
2511 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
2515 if (!elems
.fils_key_confirm
) {
2516 wpa_printf(MSG_DEBUG
, "FILS: No FILS Key Confirm element");
2520 if (elems
.fils_key_confirm_len
!= sm
->fils_key_auth_len
) {
2521 wpa_printf(MSG_DEBUG
,
2522 "FILS: Unexpected Key-Auth length %d (expected %d)",
2523 elems
.fils_key_confirm_len
,
2524 (int) sm
->fils_key_auth_len
);
2528 if (os_memcmp(elems
.fils_key_confirm
, sm
->fils_key_auth_sta
,
2529 sm
->fils_key_auth_len
) != 0) {
2530 wpa_printf(MSG_DEBUG
, "FILS: Key-Auth mismatch");
2531 wpa_hexdump(MSG_DEBUG
, "FILS: Received Key-Auth",
2532 elems
.fils_key_confirm
, elems
.fils_key_confirm_len
);
2533 wpa_hexdump(MSG_DEBUG
, "FILS: Expected Key-Auth",
2534 sm
->fils_key_auth_sta
, sm
->fils_key_auth_len
);
2542 int fils_decrypt_assoc(struct wpa_state_machine
*sm
, const u8
*fils_session
,
2543 const struct ieee80211_mgmt
*mgmt
, size_t frame_len
,
2544 u8
*pos
, size_t left
)
2547 const u8
*end
, *ie_start
, *ie
, *session
, *crypt
;
2551 if (!sm
|| !sm
->PTK_valid
) {
2552 wpa_printf(MSG_DEBUG
,
2553 "FILS: No KEK to decrypt Assocication Request frame");
2557 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2558 wpa_printf(MSG_DEBUG
,
2559 "FILS: Not a FILS AKM - reject association");
2563 end
= ((const u8
*) mgmt
) + frame_len
;
2564 fc
= le_to_host16(mgmt
->frame_control
);
2565 stype
= WLAN_FC_GET_STYPE(fc
);
2566 if (stype
== WLAN_FC_STYPE_REASSOC_REQ
)
2567 ie_start
= mgmt
->u
.reassoc_req
.variable
;
2569 ie_start
= mgmt
->u
.assoc_req
.variable
;
2573 * Find FILS Session element which is the last unencrypted element in
2576 session
= wpa_fils_validate_fils_session(sm
, ie
, end
- ie
,
2579 wpa_printf(MSG_DEBUG
, "FILS: Session validation failed");
2583 crypt
= session
+ 2 + session
[1];
2585 if (end
- crypt
< AES_BLOCK_SIZE
) {
2586 wpa_printf(MSG_DEBUG
,
2587 "FILS: Too short frame to include AES-SIV data");
2591 /* AES-SIV AAD vectors */
2593 /* The STA's MAC address */
2595 aad_len
[0] = ETH_ALEN
;
2596 /* The AP's BSSID */
2598 aad_len
[1] = ETH_ALEN
;
2599 /* The STA's nonce */
2600 aad
[2] = sm
->SNonce
;
2601 aad_len
[2] = FILS_NONCE_LEN
;
2602 /* The AP's nonce */
2603 aad
[3] = sm
->ANonce
;
2604 aad_len
[3] = FILS_NONCE_LEN
;
2606 * The (Re)Association Request frame from the Capability Information
2607 * field to the FILS Session element (both inclusive).
2609 aad
[4] = (const u8
*) &mgmt
->u
.assoc_req
.capab_info
;
2610 aad_len
[4] = crypt
- aad
[4];
2612 if (aes_siv_decrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, crypt
, end
- crypt
,
2613 5, aad
, aad_len
, pos
+ (crypt
- ie_start
)) < 0) {
2614 wpa_printf(MSG_DEBUG
,
2615 "FILS: Invalid AES-SIV data in the frame");
2618 wpa_hexdump(MSG_DEBUG
, "FILS: Decrypted Association Request elements",
2619 pos
, left
- AES_BLOCK_SIZE
);
2621 if (wpa_fils_validate_key_confirm(sm
, pos
, left
- AES_BLOCK_SIZE
) < 0) {
2622 wpa_printf(MSG_DEBUG
, "FILS: Key Confirm validation failed");
2626 return left
- AES_BLOCK_SIZE
;
2630 int fils_encrypt_assoc(struct wpa_state_machine
*sm
, u8
*buf
,
2631 size_t current_len
, size_t max_len
,
2632 const struct wpabuf
*hlp
)
2634 u8
*end
= buf
+ max_len
;
2635 u8
*pos
= buf
+ current_len
;
2636 struct ieee80211_mgmt
*mgmt
;
2637 struct wpabuf
*plain
;
2641 if (!sm
|| !sm
->PTK_valid
)
2644 wpa_hexdump(MSG_DEBUG
,
2645 "FILS: Association Response frame before FILS processing",
2648 mgmt
= (struct ieee80211_mgmt
*) buf
;
2650 /* AES-SIV AAD vectors */
2652 /* The AP's BSSID */
2654 aad_len
[0] = ETH_ALEN
;
2655 /* The STA's MAC address */
2657 aad_len
[1] = ETH_ALEN
;
2658 /* The AP's nonce */
2659 aad
[2] = sm
->ANonce
;
2660 aad_len
[2] = FILS_NONCE_LEN
;
2661 /* The STA's nonce */
2662 aad
[3] = sm
->SNonce
;
2663 aad_len
[3] = FILS_NONCE_LEN
;
2665 * The (Re)Association Response frame from the Capability Information
2666 * field (the same offset in both Association and Reassociation
2667 * Response frames) to the FILS Session element (both inclusive).
2669 aad
[4] = (const u8
*) &mgmt
->u
.assoc_resp
.capab_info
;
2670 aad_len
[4] = pos
- aad
[4];
2672 /* The following elements will be encrypted with AES-SIV */
2673 plain
= fils_prepare_plainbuf(sm
, hlp
);
2675 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2679 if (pos
+ wpabuf_len(plain
) + AES_BLOCK_SIZE
> end
) {
2680 wpa_printf(MSG_DEBUG
,
2681 "FILS: Not enough room for FILS elements");
2682 wpabuf_clear_free(plain
);
2686 wpa_hexdump_buf_key(MSG_DEBUG
, "FILS: Association Response plaintext",
2689 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
2690 wpabuf_head(plain
), wpabuf_len(plain
),
2691 5, aad
, aad_len
, pos
) < 0) {
2692 wpabuf_clear_free(plain
);
2696 wpa_hexdump(MSG_DEBUG
,
2697 "FILS: Encrypted Association Response elements",
2698 pos
, AES_BLOCK_SIZE
+ wpabuf_len(plain
));
2699 current_len
+= wpabuf_len(plain
) + AES_BLOCK_SIZE
;
2700 wpabuf_clear_free(plain
);
2702 sm
->fils_completed
= 1;
2708 static struct wpabuf
* fils_prepare_plainbuf(struct wpa_state_machine
*sm
,
2709 const struct wpabuf
*hlp
)
2711 struct wpabuf
*plain
;
2712 u8
*len
, *tmp
, *tmp2
;
2714 u8
*gtk
, dummy_gtk
[32];
2716 struct wpa_group
*gsm
;
2718 struct wpa_auth_config
*conf
= &sm
->wpa_auth
->conf
;
2720 plain_len
= 1000 + ieee80211w_kde_len(sm
);
2721 if (conf
->transition_disable
)
2722 plain_len
+= 2 + RSN_SELECTOR_LEN
+ 1;
2723 plain
= wpabuf_alloc(plain_len
);
2727 /* TODO: FILS Public Key */
2729 /* FILS Key Confirmation */
2730 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2731 wpabuf_put_u8(plain
, 1 + sm
->fils_key_auth_len
); /* Length */
2732 /* Element ID Extension */
2733 wpabuf_put_u8(plain
, WLAN_EID_EXT_FILS_KEY_CONFIRM
);
2734 wpabuf_put_data(plain
, sm
->fils_key_auth_ap
, sm
->fils_key_auth_len
);
2736 /* FILS HLP Container */
2738 wpabuf_put_buf(plain
, hlp
);
2740 /* TODO: FILS IP Address Assignment */
2744 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2745 len
= wpabuf_put(plain
, 1);
2746 wpabuf_put_u8(plain
, WLAN_EID_EXT_KEY_DELIVERY
);
2747 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
,
2748 wpabuf_put(plain
, WPA_KEY_RSC_LEN
));
2750 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2751 gtk_len
= gsm
->GTK_len
;
2752 if (conf
->disable_gtk
|| sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
2754 * Provide unique random GTK to each STA to prevent use
2755 * of GTK in the BSS.
2757 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0) {
2758 wpabuf_clear_free(plain
);
2763 hdr
[0] = gsm
->GN
& 0x03;
2765 tmp
= wpabuf_put(plain
, 0);
2766 tmp2
= wpa_add_kde(tmp
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
2768 wpabuf_put(plain
, tmp2
- tmp
);
2770 /* IGTK KDE and BIGTK KDE */
2771 tmp
= wpabuf_put(plain
, 0);
2772 tmp2
= ieee80211w_kde_add(sm
, tmp
);
2773 wpabuf_put(plain
, tmp2
- tmp
);
2775 if (conf
->transition_disable
) {
2776 tmp
= wpabuf_put(plain
, 0);
2777 tmp2
= wpa_add_kde(tmp
, WFA_KEY_DATA_TRANSITION_DISABLE
,
2778 &conf
->transition_disable
, 1, NULL
, 0);
2779 wpabuf_put(plain
, tmp2
- tmp
);
2782 *len
= (u8
*) wpabuf_put(plain
, 0) - len
- 1;
2785 if (wpa_auth_uses_ocv(sm
)) {
2786 struct wpa_channel_info ci
;
2789 if (wpa_channel_info(sm
->wpa_auth
, &ci
) != 0) {
2790 wpa_printf(MSG_WARNING
,
2791 "FILS: Failed to get channel info for OCI element");
2792 wpabuf_clear_free(plain
);
2796 pos
= wpabuf_put(plain
, OCV_OCI_EXTENDED_LEN
);
2797 if (ocv_insert_extended_oci(&ci
, pos
) < 0) {
2798 wpabuf_clear_free(plain
);
2802 #endif /* CONFIG_OCV */
2808 int fils_set_tk(struct wpa_state_machine
*sm
)
2813 if (!sm
|| !sm
->PTK_valid
) {
2814 wpa_printf(MSG_DEBUG
, "FILS: No valid PTK available to set TK");
2817 if (sm
->tk_already_set
) {
2818 wpa_printf(MSG_DEBUG
, "FILS: TK already set to the driver");
2822 alg
= wpa_cipher_to_alg(sm
->pairwise
);
2823 klen
= wpa_cipher_key_len(sm
->pairwise
);
2825 wpa_printf(MSG_DEBUG
, "FILS: Configure TK to the driver");
2826 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
2827 sm
->PTK
.tk
, klen
, KEY_FLAG_PAIRWISE_RX_TX
)) {
2828 wpa_printf(MSG_DEBUG
, "FILS: Failed to set TK to the driver");
2831 sm
->tk_already_set
= TRUE
;
2837 u8
* hostapd_eid_assoc_fils_session(struct wpa_state_machine
*sm
, u8
*buf
,
2838 const u8
*fils_session
, struct wpabuf
*hlp
)
2840 struct wpabuf
*plain
;
2844 *pos
++ = WLAN_EID_EXTENSION
; /* Element ID */
2845 *pos
++ = 1 + FILS_SESSION_LEN
; /* Length */
2846 *pos
++ = WLAN_EID_EXT_FILS_SESSION
; /* Element ID Extension */
2847 os_memcpy(pos
, fils_session
, FILS_SESSION_LEN
);
2848 pos
+= FILS_SESSION_LEN
;
2850 plain
= fils_prepare_plainbuf(sm
, hlp
);
2852 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2856 os_memcpy(pos
, wpabuf_head(plain
), wpabuf_len(plain
));
2857 pos
+= wpabuf_len(plain
);
2859 wpa_printf(MSG_DEBUG
, "%s: plain buf_len: %u", __func__
,
2860 (unsigned int) wpabuf_len(plain
));
2861 wpabuf_clear_free(plain
);
2862 sm
->fils_completed
= 1;
2866 #endif /* CONFIG_FILS */
2870 int get_sta_tx_parameters(struct wpa_state_machine
*sm
, int ap_max_chanwidth
,
2871 int ap_seg1_idx
, int *bandwidth
, int *seg1_idx
)
2873 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2875 if (!wpa_auth
->cb
->get_sta_tx_params
)
2877 return wpa_auth
->cb
->get_sta_tx_params(wpa_auth
->cb_ctx
, sm
->addr
,
2878 ap_max_chanwidth
, ap_seg1_idx
,
2879 bandwidth
, seg1_idx
);
2881 #endif /* CONFIG_OCV */
2884 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING
)
2886 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2888 int ok
= 0, psk_found
= 0;
2889 const u8
*pmk
= NULL
;
2892 const u8
*eapol_key_ie
, *key_data
, *mic
;
2893 u16 key_data_length
;
2894 size_t mic_len
, eapol_key_ie_len
;
2895 struct ieee802_1x_hdr
*hdr
;
2896 struct wpa_eapol_key
*key
;
2897 struct wpa_eapol_ie_parse kde
;
2899 int owe_ptk_workaround
= !!wpa_auth
->conf
.owe_ptk_workaround
;
2901 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING
, wpa_ptk
);
2902 sm
->EAPOLKeyReceived
= FALSE
;
2903 sm
->update_snonce
= FALSE
;
2904 os_memset(&PTK
, 0, sizeof(PTK
));
2906 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
2908 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2909 * WPA-PSK: iterate through possible PSKs and select the one matching
2912 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
2913 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
2914 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
2915 sm
->p2p_dev_addr
, pmk
, &pmk_len
,
2920 #ifdef CONFIG_IEEE80211R_AP
2921 if (wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
2922 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
2923 sm
->xxkey_len
= pmk_len
;
2925 #endif /* CONFIG_IEEE80211R_AP */
2928 pmk_len
= sm
->pmk_len
;
2931 if ((!pmk
|| !pmk_len
) && sm
->pmksa
) {
2932 wpa_printf(MSG_DEBUG
, "WPA: Use PMK from PMKSA cache");
2933 pmk
= sm
->pmksa
->pmk
;
2934 pmk_len
= sm
->pmksa
->pmk_len
;
2937 if (wpa_derive_ptk(sm
, sm
->SNonce
, pmk
, pmk_len
, &PTK
,
2938 owe_ptk_workaround
== 2) < 0)
2942 wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
2943 sm
->last_rx_eapol_key
,
2944 sm
->last_rx_eapol_key_len
) == 0) {
2945 if (sm
->PMK
!= pmk
) {
2946 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
2947 sm
->pmk_len
= pmk_len
;
2955 wpa_aead_decrypt(sm
, &PTK
, sm
->last_rx_eapol_key
,
2956 sm
->last_rx_eapol_key_len
, NULL
) == 0) {
2960 #endif /* CONFIG_FILS */
2963 if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
&& pmk_len
> 32 &&
2964 owe_ptk_workaround
== 1) {
2965 wpa_printf(MSG_DEBUG
,
2966 "OWE: Try PTK derivation workaround with SHA256");
2967 owe_ptk_workaround
= 2;
2970 #endif /* CONFIG_OWE */
2972 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
2973 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
))
2978 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2979 "invalid MIC in msg 2/4 of 4-Way Handshake");
2981 wpa_auth_psk_failure_report(sm
->wpa_auth
, sm
->addr
);
2986 * Note: last_rx_eapol_key length fields have already been validated in
2989 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
2990 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2991 mic
= (u8
*) (key
+ 1);
2992 key_data
= mic
+ mic_len
+ 2;
2993 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
2994 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
2995 sizeof(*key
) - mic_len
- 2)
2998 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
2999 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3000 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
3004 eapol_key_ie
= kde
.rsn_ie
;
3005 eapol_key_ie_len
= kde
.rsn_ie_len
;
3006 } else if (kde
.osen
) {
3007 eapol_key_ie
= kde
.osen
;
3008 eapol_key_ie_len
= kde
.osen_len
;
3010 eapol_key_ie
= kde
.wpa_ie
;
3011 eapol_key_ie_len
= kde
.wpa_ie_len
;
3013 ft
= sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
);
3014 if (sm
->wpa_ie
== NULL
||
3015 wpa_compare_rsn_ie(ft
, sm
->wpa_ie
, sm
->wpa_ie_len
,
3016 eapol_key_ie
, eapol_key_ie_len
)) {
3017 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3018 "WPA IE from (Re)AssocReq did not match with msg 2/4");
3020 wpa_hexdump(MSG_DEBUG
, "WPA IE in AssocReq",
3021 sm
->wpa_ie
, sm
->wpa_ie_len
);
3023 wpa_hexdump(MSG_DEBUG
, "WPA IE in msg 2/4",
3024 eapol_key_ie
, eapol_key_ie_len
);
3025 /* MLME-DEAUTHENTICATE.request */
3026 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
3027 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3030 if ((!sm
->rsnxe
&& kde
.rsnxe
) ||
3031 (sm
->rsnxe
&& !kde
.rsnxe
) ||
3032 (sm
->rsnxe
&& kde
.rsnxe
&&
3033 (sm
->rsnxe_len
!= kde
.rsnxe_len
||
3034 os_memcmp(sm
->rsnxe
, kde
.rsnxe
, sm
->rsnxe_len
) != 0))) {
3035 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3036 "RSNXE from (Re)AssocReq did not match the one in EAPOL-Key msg 2/4");
3037 wpa_hexdump(MSG_DEBUG
, "RSNXE in AssocReq",
3038 sm
->rsnxe
, sm
->rsnxe_len
);
3039 wpa_hexdump(MSG_DEBUG
, "RSNXE in EAPOL-Key msg 2/4",
3040 kde
.rsnxe
, kde
.rsnxe_len
);
3041 /* MLME-DEAUTHENTICATE.request */
3042 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
3043 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3047 if (wpa_auth_uses_ocv(sm
)) {
3048 struct wpa_channel_info ci
;
3052 if (wpa_channel_info(wpa_auth
, &ci
) != 0) {
3053 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3054 "Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
3058 if (get_sta_tx_parameters(sm
,
3059 channel_width_to_int(ci
.chanwidth
),
3060 ci
.seg1_idx
, &tx_chanwidth
,
3064 if (ocv_verify_tx_params(kde
.oci
, kde
.oci_len
, &ci
,
3065 tx_chanwidth
, tx_seg1_idx
) != 0) {
3066 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3071 #endif /* CONFIG_OCV */
3072 #ifdef CONFIG_IEEE80211R_AP
3073 if (ft
&& ft_check_msg_2_of_4(wpa_auth
, sm
, &kde
) < 0) {
3074 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
3075 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3078 #endif /* CONFIG_IEEE80211R_AP */
3080 if (kde
.ip_addr_req
&& kde
.ip_addr_req
[0] &&
3081 wpa_auth
->ip_pool
&& WPA_GET_BE32(sm
->ip_addr
) == 0) {
3083 wpa_printf(MSG_DEBUG
,
3084 "P2P: IP address requested in EAPOL-Key exchange");
3085 idx
= bitfield_get_first_zero(wpa_auth
->ip_pool
);
3087 u32 start
= WPA_GET_BE32(wpa_auth
->conf
.ip_addr_start
);
3088 bitfield_set(wpa_auth
->ip_pool
, idx
);
3089 WPA_PUT_BE32(sm
->ip_addr
, start
+ idx
);
3090 wpa_printf(MSG_DEBUG
,
3091 "P2P: Assigned IP address %u.%u.%u.%u to "
3092 MACSTR
, sm
->ip_addr
[0], sm
->ip_addr
[1],
3093 sm
->ip_addr
[2], sm
->ip_addr
[3],
3097 #endif /* CONFIG_P2P */
3099 #ifdef CONFIG_IEEE80211R_AP
3100 if (sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3102 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
3103 * with the value we derived.
3105 if (os_memcmp_const(sm
->sup_pmk_r1_name
, sm
->pmk_r1_name
,
3106 WPA_PMK_NAME_LEN
) != 0) {
3107 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3108 "PMKR1Name mismatch in FT 4-way "
3110 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from "
3112 sm
->sup_pmk_r1_name
, WPA_PMK_NAME_LEN
);
3113 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
3114 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
3118 #endif /* CONFIG_IEEE80211R_AP */
3120 if (vlan_id
&& wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
3121 wpa_auth_update_vlan(wpa_auth
, sm
->addr
, vlan_id
) < 0) {
3122 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
3123 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3127 sm
->pending_1_of_4_timeout
= 0;
3128 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
3130 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
3131 /* PSK may have changed from the previous choice, so update
3132 * state machine data based on whatever PSK was selected here.
3134 os_memcpy(sm
->PMK
, pmk
, PMK_LEN
);
3135 sm
->pmk_len
= PMK_LEN
;
3138 sm
->MICVerified
= TRUE
;
3140 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
3141 forced_memzero(&PTK
, sizeof(PTK
));
3142 sm
->PTK_valid
= TRUE
;
3146 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING2
)
3148 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING2
, wpa_ptk
);
3153 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
3157 if (sm
->mgmt_frame_prot
) {
3158 len
+= 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
;
3159 len
+= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3161 if (sm
->mgmt_frame_prot
&& sm
->wpa_auth
->conf
.beacon_prot
) {
3162 len
+= 2 + RSN_SELECTOR_LEN
+ WPA_BIGTK_KDE_PREFIX_LEN
;
3163 len
+= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3170 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
3172 struct wpa_igtk_kde igtk
;
3173 struct wpa_bigtk_kde bigtk
;
3174 struct wpa_group
*gsm
= sm
->group
;
3175 u8 rsc
[WPA_KEY_RSC_LEN
];
3176 struct wpa_auth_config
*conf
= &sm
->wpa_auth
->conf
;
3177 size_t len
= wpa_cipher_key_len(conf
->group_mgmt_cipher
);
3179 if (!sm
->mgmt_frame_prot
)
3182 igtk
.keyid
[0] = gsm
->GN_igtk
;
3184 if (gsm
->wpa_group_state
!= WPA_GROUP_SETKEYSDONE
||
3185 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, rsc
) < 0)
3186 os_memset(igtk
.pn
, 0, sizeof(igtk
.pn
));
3188 os_memcpy(igtk
.pn
, rsc
, sizeof(igtk
.pn
));
3189 os_memcpy(igtk
.igtk
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3190 if (conf
->disable_gtk
|| sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3192 * Provide unique random IGTK to each STA to prevent use of
3195 if (random_get_bytes(igtk
.igtk
, len
) < 0)
3198 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_IGTK
,
3199 (const u8
*) &igtk
, WPA_IGTK_KDE_PREFIX_LEN
+ len
,
3202 if (!conf
->beacon_prot
)
3205 bigtk
.keyid
[0] = gsm
->GN_bigtk
;
3207 if (gsm
->wpa_group_state
!= WPA_GROUP_SETKEYSDONE
||
3208 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_bigtk
, rsc
) < 0)
3209 os_memset(bigtk
.pn
, 0, sizeof(bigtk
.pn
));
3211 os_memcpy(bigtk
.pn
, rsc
, sizeof(bigtk
.pn
));
3212 os_memcpy(bigtk
.bigtk
, gsm
->BIGTK
[gsm
->GN_bigtk
- 6], len
);
3213 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_BIGTK
,
3214 (const u8
*) &bigtk
, WPA_BIGTK_KDE_PREFIX_LEN
+ len
,
3221 static int ocv_oci_len(struct wpa_state_machine
*sm
)
3224 if (wpa_auth_uses_ocv(sm
))
3225 return OCV_OCI_KDE_LEN
;
3226 #endif /* CONFIG_OCV */
3230 static int ocv_oci_add(struct wpa_state_machine
*sm
, u8
**argpos
)
3233 struct wpa_channel_info ci
;
3235 if (!wpa_auth_uses_ocv(sm
))
3238 if (wpa_channel_info(sm
->wpa_auth
, &ci
) != 0) {
3239 wpa_printf(MSG_WARNING
,
3240 "Failed to get channel info for OCI element");
3244 return ocv_insert_oci_kde(&ci
, argpos
);
3245 #else /* CONFIG_OCV */
3247 #endif /* CONFIG_OCV */
3251 #ifdef CONFIG_TESTING_OPTIONS
3252 static u8
* replace_ie(const char *name
, const u8
*old_buf
, size_t *len
, u8 eid
,
3253 const u8
*ie
, size_t ie_len
)
3258 wpa_printf(MSG_DEBUG
, "TESTING: %s EAPOL override", name
);
3259 wpa_hexdump(MSG_DEBUG
, "TESTING: wpa_ie before override",
3261 buf
= os_malloc(*len
+ ie_len
);
3264 os_memcpy(buf
, old_buf
, *len
);
3265 elem
= get_ie(buf
, *len
, eid
);
3267 u8 elem_len
= 2 + elem
[1];
3269 os_memmove((void *) elem
, elem
+ elem_len
,
3270 *len
- (elem
- buf
) - elem_len
);
3273 os_memcpy(buf
+ *len
, ie
, ie_len
);
3275 wpa_hexdump(MSG_DEBUG
, "TESTING: wpa_ie after EAPOL override",
3280 #endif /* CONFIG_TESTING_OPTIONS */
3283 SM_STATE(WPA_PTK
, PTKINITNEGOTIATING
)
3285 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
= NULL
, *pos
, dummy_gtk
[32];
3286 size_t gtk_len
, kde_len
, wpa_ie_len
;
3287 struct wpa_group
*gsm
= sm
->group
;
3289 int secure
, gtkidx
, encr
= 0;
3290 u8
*wpa_ie_buf
= NULL
, *wpa_ie_buf2
= NULL
;
3292 struct wpa_auth_config
*conf
= &sm
->wpa_auth
->conf
;
3294 SM_ENTRY_MA(WPA_PTK
, PTKINITNEGOTIATING
, wpa_ptk
);
3295 sm
->TimeoutEvt
= FALSE
;
3298 if (conf
->wpa_disable_eapol_key_retries
&& sm
->TimeoutCtr
> 1) {
3299 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
3302 if (sm
->TimeoutCtr
> conf
->wpa_pairwise_update_count
) {
3303 /* No point in sending the EAPOL-Key - we will disconnect
3304 * immediately following this. */
3308 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
3309 GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2])
3311 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3312 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3313 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
3314 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
3315 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
3316 if (sm
->wpa
== WPA_VERSION_WPA
&& (conf
->wpa
& WPA_PROTO_RSN
) &&
3317 wpa_ie_len
> wpa_ie
[1] + 2U && wpa_ie
[0] == WLAN_EID_RSN
) {
3318 /* WPA-only STA, remove RSN IE and possible MDIE */
3319 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
3320 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
3321 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
3322 wpa_ie_len
= wpa_ie
[1] + 2;
3324 #ifdef CONFIG_TESTING_OPTIONS
3325 if (conf
->rsne_override_eapol_set
) {
3326 wpa_ie_buf2
= replace_ie(
3327 "RSNE", wpa_ie
, &wpa_ie_len
, WLAN_EID_RSN
,
3328 conf
->rsne_override_eapol
,
3329 conf
->rsne_override_eapol_len
);
3332 wpa_ie
= wpa_ie_buf2
;
3334 if (conf
->rsnxe_override_eapol_set
) {
3335 wpa_ie_buf
= replace_ie(
3336 "RSNXE", wpa_ie
, &wpa_ie_len
, WLAN_EID_RSNX
,
3337 conf
->rsnxe_override_eapol
,
3338 conf
->rsnxe_override_eapol_len
);
3341 wpa_ie
= wpa_ie_buf
;
3343 #endif /* CONFIG_TESTING_OPTIONS */
3344 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3345 "sending 3/4 msg of 4-Way Handshake");
3346 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3347 if (sm
->use_ext_key_id
&& sm
->TimeoutCtr
== 1 &&
3348 wpa_auth_set_key(sm
->wpa_auth
, 0,
3349 wpa_cipher_to_alg(sm
->pairwise
),
3351 sm
->keyidx_active
, sm
->PTK
.tk
,
3352 wpa_cipher_key_len(sm
->pairwise
),
3353 KEY_FLAG_PAIRWISE_RX
)) {
3354 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
,
3355 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3359 /* WPA2 send GTK in the 4-way handshake */
3361 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3362 gtk_len
= gsm
->GTK_len
;
3363 if (conf
->disable_gtk
||
3364 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3366 * Provide unique random GTK to each STA to prevent use
3367 * of GTK in the BSS.
3369 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0)
3377 /* WPA does not include GTK in msg 3/4 */
3382 if (sm
->rx_eapol_key_secure
) {
3384 * It looks like Windows 7 supplicant tries to use
3385 * Secure bit in msg 2/4 after having reported Michael
3386 * MIC failure and it then rejects the 4-way handshake
3387 * if msg 3/4 does not set Secure bit. Work around this
3388 * by setting the Secure bit here even in the case of
3389 * WPA if the supplicant used it first.
3391 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3392 "STA used Secure bit in WPA msg 2/4 - "
3393 "set Secure for 3/4 as workaround");
3398 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
3400 if (sm
->use_ext_key_id
)
3401 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2;
3404 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
3405 #ifdef CONFIG_IEEE80211R_AP
3406 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3407 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
3408 kde_len
+= 300; /* FTIE + 2 * TIE */
3410 #endif /* CONFIG_IEEE80211R_AP */
3412 if (WPA_GET_BE32(sm
->ip_addr
) > 0)
3413 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 3 * 4;
3414 #endif /* CONFIG_P2P */
3416 if (conf
->transition_disable
)
3417 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 1;
3419 kde
= os_malloc(kde_len
);
3424 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
3426 #ifdef CONFIG_IEEE80211R_AP
3427 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3432 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
3434 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
3435 "PMKR1Name into RSN IE in EAPOL-Key data");
3441 #endif /* CONFIG_IEEE80211R_AP */
3444 if (sm
->use_ext_key_id
) {
3445 hdr
[0] = sm
->keyidx_active
& 0x01;
3446 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_KEYID
, hdr
, 2, NULL
, 0);
3450 hdr
[0] = gtkidx
& 0x03;
3451 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3454 pos
= ieee80211w_kde_add(sm
, pos
);
3455 if (ocv_oci_add(sm
, &pos
) < 0)
3458 #ifdef CONFIG_IEEE80211R_AP
3459 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3462 if (sm
->assoc_resp_ftie
&&
3463 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
3464 os_memcpy(pos
, sm
->assoc_resp_ftie
,
3465 2 + sm
->assoc_resp_ftie
[1]);
3466 res
= 2 + sm
->assoc_resp_ftie
[1];
3468 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
3470 res
= wpa_write_ftie(conf
, use_sha384
,
3471 conf
->r0_key_holder
,
3472 conf
->r0_key_holder_len
,
3474 kde
+ kde_len
- pos
,
3478 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
3479 "into EAPOL-Key Key Data");
3484 /* TIE[ReassociationDeadline] (TU) */
3485 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3487 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
3488 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
3491 /* TIE[KeyLifetime] (seconds) */
3492 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3494 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
3495 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
);
3498 #endif /* CONFIG_IEEE80211R_AP */
3500 if (WPA_GET_BE32(sm
->ip_addr
) > 0) {
3502 os_memcpy(addr
, sm
->ip_addr
, 4);
3503 os_memcpy(addr
+ 4, conf
->ip_addr_mask
, 4);
3504 os_memcpy(addr
+ 8, conf
->ip_addr_go
, 4);
3505 pos
= wpa_add_kde(pos
, WFA_KEY_DATA_IP_ADDR_ALLOC
,
3506 addr
, sizeof(addr
), NULL
, 0);
3508 #endif /* CONFIG_P2P */
3510 if (conf
->transition_disable
)
3511 pos
= wpa_add_kde(pos
, WFA_KEY_DATA_TRANSITION_DISABLE
,
3512 &conf
->transition_disable
, 1, NULL
, 0);
3514 wpa_send_eapol(sm
->wpa_auth
, sm
,
3515 (secure
? WPA_KEY_INFO_SECURE
: 0) |
3516 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3517 WPA_KEY_INFO_MIC
: 0) |
3518 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
3519 WPA_KEY_INFO_KEY_TYPE
,
3520 _rsc
, sm
->ANonce
, kde
, pos
- kde
, 0, encr
);
3523 os_free(wpa_ie_buf
);
3524 os_free(wpa_ie_buf2
);
3528 SM_STATE(WPA_PTK
, PTKINITDONE
)
3530 SM_ENTRY_MA(WPA_PTK
, PTKINITDONE
, wpa_ptk
);
3531 sm
->EAPOLKeyReceived
= FALSE
;
3533 enum wpa_alg alg
= wpa_cipher_to_alg(sm
->pairwise
);
3534 int klen
= wpa_cipher_key_len(sm
->pairwise
);
3537 if (sm
->use_ext_key_id
)
3538 res
= wpa_auth_set_key(sm
->wpa_auth
, 0, 0, sm
->addr
,
3539 sm
->keyidx_active
, NULL
, 0,
3540 KEY_FLAG_PAIRWISE_RX_TX_MODIFY
);
3542 res
= wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
,
3543 0, sm
->PTK
.tk
, klen
,
3544 KEY_FLAG_PAIRWISE_RX_TX
);
3546 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
,
3547 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3550 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3551 sm
->pairwise_set
= TRUE
;
3553 wpa_auth_set_ptk_rekey_timer(sm
);
3555 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3556 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
3557 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
3558 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3559 WPA_EAPOL_authorized
, 1);
3563 if (0 /* IBSS == TRUE */) {
3565 if (sm
->keycount
== 2) {
3566 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3567 WPA_EAPOL_portValid
, 1);
3570 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
,
3573 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyAvailable
, 0);
3574 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyDone
, 1);
3575 if (sm
->wpa
== WPA_VERSION_WPA
)
3576 sm
->PInitAKeys
= TRUE
;
3579 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3580 "pairwise key handshake completed (%s)",
3581 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3583 #ifdef CONFIG_IEEE80211R_AP
3584 wpa_ft_push_pmk_r1(sm
->wpa_auth
, sm
->addr
);
3585 #endif /* CONFIG_IEEE80211R_AP */
3591 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3592 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
3595 SM_ENTER(WPA_PTK
, INITIALIZE
);
3596 else if (sm
->Disconnect
3597 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3598 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3599 "WPA_PTK: sm->Disconnect");
3600 SM_ENTER(WPA_PTK
, DISCONNECT
);
3602 else if (sm
->DeauthenticationRequest
)
3603 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3604 else if (sm
->AuthenticationRequest
)
3605 SM_ENTER(WPA_PTK
, AUTHENTICATION
);
3606 else if (sm
->ReAuthenticationRequest
)
3607 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3608 else if (sm
->PTKRequest
) {
3609 if (wpa_auth_sm_ptk_update(sm
) < 0)
3610 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3612 SM_ENTER(WPA_PTK
, PTKSTART
);
3613 } else switch (sm
->wpa_ptk_state
) {
3614 case WPA_PTK_INITIALIZE
:
3616 case WPA_PTK_DISCONNECT
:
3617 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3619 case WPA_PTK_DISCONNECTED
:
3620 SM_ENTER(WPA_PTK
, INITIALIZE
);
3622 case WPA_PTK_AUTHENTICATION
:
3623 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3625 case WPA_PTK_AUTHENTICATION2
:
3626 if (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
3627 wpa_auth_get_eapol(wpa_auth
, sm
->addr
,
3628 WPA_EAPOL_keyRun
) > 0)
3629 SM_ENTER(WPA_PTK
, INITPMK
);
3630 else if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3631 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
3632 /* FIX: && 802.1X::keyRun */)
3633 SM_ENTER(WPA_PTK
, INITPSK
);
3634 else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
)
3635 SM_ENTER(WPA_PTK
, INITPMK
);
3637 case WPA_PTK_INITPMK
:
3638 if (wpa_auth_get_eapol(wpa_auth
, sm
->addr
,
3639 WPA_EAPOL_keyAvailable
) > 0) {
3640 SM_ENTER(WPA_PTK
, PTKSTART
);
3642 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->pmksa
) {
3643 SM_ENTER(WPA_PTK
, PTKSTART
);
3644 #endif /* CONFIG_DPP */
3646 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3647 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3648 "INITPMK - keyAvailable = false");
3649 SM_ENTER(WPA_PTK
, DISCONNECT
);
3652 case WPA_PTK_INITPSK
:
3653 if (wpa_auth_get_psk(wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
,
3654 NULL
, NULL
, NULL
)) {
3655 SM_ENTER(WPA_PTK
, PTKSTART
);
3657 } else if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
3658 SM_ENTER(WPA_PTK
, PTKSTART
);
3659 #endif /* CONFIG_SAE */
3661 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3662 "no PSK configured for the STA");
3663 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3664 SM_ENTER(WPA_PTK
, DISCONNECT
);
3667 case WPA_PTK_PTKSTART
:
3668 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3669 sm
->EAPOLKeyPairwise
)
3670 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3671 else if (sm
->TimeoutCtr
> conf
->wpa_pairwise_update_count
) {
3672 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3673 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3674 "PTKSTART: Retry limit %u reached",
3675 conf
->wpa_pairwise_update_count
);
3676 SM_ENTER(WPA_PTK
, DISCONNECT
);
3677 } else if (sm
->TimeoutEvt
)
3678 SM_ENTER(WPA_PTK
, PTKSTART
);
3680 case WPA_PTK_PTKCALCNEGOTIATING
:
3681 if (sm
->MICVerified
)
3682 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING2
);
3683 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3684 sm
->EAPOLKeyPairwise
)
3685 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3686 else if (sm
->TimeoutEvt
)
3687 SM_ENTER(WPA_PTK
, PTKSTART
);
3689 case WPA_PTK_PTKCALCNEGOTIATING2
:
3690 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3692 case WPA_PTK_PTKINITNEGOTIATING
:
3693 if (sm
->update_snonce
)
3694 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3695 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3696 sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3697 SM_ENTER(WPA_PTK
, PTKINITDONE
);
3698 else if (sm
->TimeoutCtr
>
3699 conf
->wpa_pairwise_update_count
||
3700 (conf
->wpa_disable_eapol_key_retries
&&
3701 sm
->TimeoutCtr
> 1)) {
3702 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3703 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3704 "PTKINITNEGOTIATING: Retry limit %u reached",
3705 conf
->wpa_pairwise_update_count
);
3706 SM_ENTER(WPA_PTK
, DISCONNECT
);
3707 } else if (sm
->TimeoutEvt
)
3708 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3710 case WPA_PTK_PTKINITDONE
:
3716 SM_STATE(WPA_PTK_GROUP
, IDLE
)
3718 SM_ENTRY_MA(WPA_PTK_GROUP
, IDLE
, wpa_ptk_group
);
3720 /* Init flag is not cleared here, so avoid busy
3721 * loop by claiming nothing changed. */
3722 sm
->changed
= FALSE
;
3724 sm
->GTimeoutCtr
= 0;
3728 SM_STATE(WPA_PTK_GROUP
, REKEYNEGOTIATING
)
3730 u8 rsc
[WPA_KEY_RSC_LEN
];
3731 struct wpa_group
*gsm
= sm
->group
;
3733 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
3735 u8
*gtk
, dummy_gtk
[32];
3736 struct wpa_auth_config
*conf
= &sm
->wpa_auth
->conf
;
3738 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYNEGOTIATING
, wpa_ptk_group
);
3741 if (conf
->wpa_disable_eapol_key_retries
&& sm
->GTimeoutCtr
> 1) {
3742 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3745 if (sm
->GTimeoutCtr
> conf
->wpa_group_update_count
) {
3746 /* No point in sending the EAPOL-Key - we will disconnect
3747 * immediately following this. */
3751 if (sm
->wpa
== WPA_VERSION_WPA
)
3752 sm
->PInitAKeys
= FALSE
;
3753 sm
->TimeoutEvt
= FALSE
;
3754 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3755 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3756 if (gsm
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
)
3757 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3758 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3759 "sending 1/2 msg of Group Key Handshake");
3761 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3762 if (conf
->disable_gtk
|| sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3764 * Provide unique random GTK to each STA to prevent use
3765 * of GTK in the BSS.
3767 if (random_get_bytes(dummy_gtk
, gsm
->GTK_len
) < 0)
3771 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3772 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
3773 ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
3774 kde_buf
= os_malloc(kde_len
);
3775 if (kde_buf
== NULL
)
3778 kde
= pos
= kde_buf
;
3779 hdr
[0] = gsm
->GN
& 0x03;
3781 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3783 pos
= ieee80211w_kde_add(sm
, pos
);
3784 if (ocv_oci_add(sm
, &pos
) < 0) {
3788 kde_len
= pos
- kde
;
3791 kde_len
= gsm
->GTK_len
;
3794 wpa_send_eapol(sm
->wpa_auth
, sm
,
3795 WPA_KEY_INFO_SECURE
|
3796 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3797 WPA_KEY_INFO_MIC
: 0) |
3799 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
3800 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
3806 SM_STATE(WPA_PTK_GROUP
, REKEYESTABLISHED
)
3808 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3810 const u8
*key_data
, *mic
;
3811 struct ieee802_1x_hdr
*hdr
;
3812 struct wpa_eapol_key
*key
;
3813 struct wpa_eapol_ie_parse kde
;
3815 u16 key_data_length
;
3816 #endif /* CONFIG_OCV */
3818 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYESTABLISHED
, wpa_ptk_group
);
3819 sm
->EAPOLKeyReceived
= FALSE
;
3822 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
3825 * Note: last_rx_eapol_key length fields have already been validated in
3828 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
3829 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
3830 mic
= (u8
*) (key
+ 1);
3831 key_data
= mic
+ mic_len
+ 2;
3832 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
3833 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
3834 sizeof(*key
) - mic_len
- 2)
3837 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
3838 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3839 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3843 if (wpa_auth_uses_ocv(sm
)) {
3844 struct wpa_channel_info ci
;
3848 if (wpa_channel_info(wpa_auth
, &ci
) != 0) {
3849 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3850 "Failed to get channel info to validate received OCI in EAPOL-Key group 1/2");
3854 if (get_sta_tx_parameters(sm
,
3855 channel_width_to_int(ci
.chanwidth
),
3856 ci
.seg1_idx
, &tx_chanwidth
,
3860 if (ocv_verify_tx_params(kde
.oci
, kde
.oci_len
, &ci
,
3861 tx_chanwidth
, tx_seg1_idx
) != 0) {
3862 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3867 #endif /* CONFIG_OCV */
3869 if (sm
->GUpdateStationKeys
)
3870 sm
->group
->GKeyDoneStations
--;
3871 sm
->GUpdateStationKeys
= FALSE
;
3872 sm
->GTimeoutCtr
= 0;
3873 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3874 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3875 "group key handshake completed (%s)",
3876 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3881 SM_STATE(WPA_PTK_GROUP
, KEYERROR
)
3883 SM_ENTRY_MA(WPA_PTK_GROUP
, KEYERROR
, wpa_ptk_group
);
3884 if (sm
->GUpdateStationKeys
)
3885 sm
->group
->GKeyDoneStations
--;
3886 sm
->GUpdateStationKeys
= FALSE
;
3887 sm
->Disconnect
= TRUE
;
3888 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3889 "group key handshake failed (%s) after %u tries",
3890 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN",
3891 sm
->wpa_auth
->conf
.wpa_group_update_count
);
3895 SM_STEP(WPA_PTK_GROUP
)
3897 if (sm
->Init
|| sm
->PtkGroupInit
) {
3898 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3899 sm
->PtkGroupInit
= FALSE
;
3900 } else switch (sm
->wpa_ptk_group_state
) {
3901 case WPA_PTK_GROUP_IDLE
:
3902 if (sm
->GUpdateStationKeys
||
3903 (sm
->wpa
== WPA_VERSION_WPA
&& sm
->PInitAKeys
))
3904 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3906 case WPA_PTK_GROUP_REKEYNEGOTIATING
:
3907 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3908 !sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3909 SM_ENTER(WPA_PTK_GROUP
, REKEYESTABLISHED
);
3910 else if (sm
->GTimeoutCtr
>
3911 sm
->wpa_auth
->conf
.wpa_group_update_count
||
3912 (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3913 sm
->GTimeoutCtr
> 1))
3914 SM_ENTER(WPA_PTK_GROUP
, KEYERROR
);
3915 else if (sm
->TimeoutEvt
)
3916 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3918 case WPA_PTK_GROUP_KEYERROR
:
3919 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3921 case WPA_PTK_GROUP_REKEYESTABLISHED
:
3922 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3928 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
3929 struct wpa_group
*group
)
3931 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
3935 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3936 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3937 if (wpa_gmk_to_gtk(group
->GMK
, "Group key expansion",
3938 wpa_auth
->addr
, group
->GNonce
,
3939 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3941 wpa_hexdump_key(MSG_DEBUG
, "GTK",
3942 group
->GTK
[group
->GN
- 1], group
->GTK_len
);
3944 if (conf
->ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3945 len
= wpa_cipher_key_len(conf
->group_mgmt_cipher
);
3946 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3947 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3948 if (wpa_gmk_to_gtk(group
->GMK
, "IGTK key expansion",
3949 wpa_auth
->addr
, group
->GNonce
,
3950 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3952 wpa_hexdump_key(MSG_DEBUG
, "IGTK",
3953 group
->IGTK
[group
->GN_igtk
- 4], len
);
3956 if (conf
->ieee80211w
!= NO_MGMT_FRAME_PROTECTION
&&
3957 conf
->beacon_prot
) {
3958 len
= wpa_cipher_key_len(conf
->group_mgmt_cipher
);
3959 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3960 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3961 if (wpa_gmk_to_gtk(group
->GMK
, "BIGTK key expansion",
3962 wpa_auth
->addr
, group
->GNonce
,
3963 group
->BIGTK
[group
->GN_bigtk
- 6], len
) < 0)
3965 wpa_hexdump_key(MSG_DEBUG
, "BIGTK",
3966 group
->BIGTK
[group
->GN_bigtk
- 6], len
);
3973 static void wpa_group_gtk_init(struct wpa_authenticator
*wpa_auth
,
3974 struct wpa_group
*group
)
3976 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3977 "GTK_INIT (VLAN-ID %d)", group
->vlan_id
);
3978 group
->changed
= FALSE
; /* GInit is not cleared here; avoid loop */
3979 group
->wpa_group_state
= WPA_GROUP_GTK_INIT
;
3982 os_memset(group
->GTK
, 0, sizeof(group
->GTK
));
3987 group
->GN_bigtk
= 6;
3988 group
->GM_bigtk
= 7;
3989 /* GTK[GN] = CalcGTK() */
3990 wpa_gtk_update(wpa_auth
, group
);
3994 static int wpa_group_update_sta(struct wpa_state_machine
*sm
, void *ctx
)
3996 if (ctx
!= NULL
&& ctx
!= sm
->group
)
3999 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITDONE
) {
4000 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
4001 "Not in PTKINITDONE; skip Group Key update");
4002 sm
->GUpdateStationKeys
= FALSE
;
4005 if (sm
->GUpdateStationKeys
) {
4007 * This should not really happen, so add a debug log entry.
4008 * Since we clear the GKeyDoneStations before the loop, the
4009 * station needs to be counted here anyway.
4011 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
4012 "GUpdateStationKeys was already set when "
4013 "marking station for GTK rekeying");
4016 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
4017 if (sm
->is_wnmsleep
)
4020 sm
->group
->GKeyDoneStations
++;
4021 sm
->GUpdateStationKeys
= TRUE
;
4028 #ifdef CONFIG_WNM_AP
4029 /* update GTK when exiting WNM-Sleep Mode */
4030 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine
*sm
)
4032 if (sm
== NULL
|| sm
->is_wnmsleep
)
4035 wpa_group_update_sta(sm
, NULL
);
4039 void wpa_set_wnmsleep(struct wpa_state_machine
*sm
, int flag
)
4042 sm
->is_wnmsleep
= !!flag
;
4046 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
4048 struct wpa_group
*gsm
= sm
->group
;
4053 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
4056 *pos
++ = WNM_SLEEP_SUBELEM_GTK
;
4057 *pos
++ = 11 + gsm
->GTK_len
;
4058 /* Key ID in B0-B1 of Key Info */
4059 WPA_PUT_LE16(pos
, gsm
->GN
& 0x03);
4061 *pos
++ = gsm
->GTK_len
;
4062 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, pos
) != 0)
4065 os_memcpy(pos
, gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
4066 pos
+= gsm
->GTK_len
;
4068 wpa_printf(MSG_DEBUG
, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
4070 wpa_hexdump_key(MSG_DEBUG
, "WNM: GTK in WNM-Sleep Mode exit",
4071 gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
4077 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
4079 struct wpa_group
*gsm
= sm
->group
;
4081 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
4085 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
4087 *pos
++ = WNM_SLEEP_SUBELEM_IGTK
;
4088 *pos
++ = 2 + 6 + len
;
4089 WPA_PUT_LE16(pos
, gsm
->GN_igtk
);
4091 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, pos
) != 0)
4095 os_memcpy(pos
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
4098 wpa_printf(MSG_DEBUG
, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
4100 wpa_hexdump_key(MSG_DEBUG
, "WNM: IGTK in WNM-Sleep Mode exit",
4101 gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
4107 int wpa_wnmsleep_bigtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
4109 struct wpa_group
*gsm
= sm
->group
;
4111 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
4115 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
4117 *pos
++ = WNM_SLEEP_SUBELEM_BIGTK
;
4118 *pos
++ = 2 + 6 + len
;
4119 WPA_PUT_LE16(pos
, gsm
->GN_bigtk
);
4121 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_bigtk
, pos
) != 0)
4125 os_memcpy(pos
, gsm
->BIGTK
[gsm
->GN_bigtk
- 6], len
);
4128 wpa_printf(MSG_DEBUG
, "WNM: BIGTK Key ID %u in WNM-Sleep Mode exit",
4130 wpa_hexdump_key(MSG_DEBUG
, "WNM: BIGTK in WNM-Sleep Mode exit",
4131 gsm
->IGTK
[gsm
->GN_bigtk
- 6], len
);
4136 #endif /* CONFIG_WNM_AP */
4139 static void wpa_group_setkeys(struct wpa_authenticator
*wpa_auth
,
4140 struct wpa_group
*group
)
4144 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
4145 "SETKEYS (VLAN-ID %d)", group
->vlan_id
);
4146 group
->changed
= TRUE
;
4147 group
->wpa_group_state
= WPA_GROUP_SETKEYS
;
4148 group
->GTKReKey
= FALSE
;
4150 group
->GM
= group
->GN
;
4152 tmp
= group
->GM_igtk
;
4153 group
->GM_igtk
= group
->GN_igtk
;
4154 group
->GN_igtk
= tmp
;
4155 tmp
= group
->GM_bigtk
;
4156 group
->GM_bigtk
= group
->GN_bigtk
;
4157 group
->GN_bigtk
= tmp
;
4158 /* "GKeyDoneStations = GNoStations" is done in more robust way by
4159 * counting the STAs that are marked with GUpdateStationKeys instead of
4160 * including all STAs that could be in not-yet-completed state. */
4161 wpa_gtk_update(wpa_auth
, group
);
4163 if (group
->GKeyDoneStations
) {
4164 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: Unexpected "
4165 "GKeyDoneStations=%d when starting new GTK rekey",
4166 group
->GKeyDoneStations
);
4167 group
->GKeyDoneStations
= 0;
4169 wpa_auth_for_each_sta(wpa_auth
, wpa_group_update_sta
, group
);
4170 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: GKeyDoneStations=%d",
4171 group
->GKeyDoneStations
);
4175 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
4176 struct wpa_group
*group
)
4178 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
4181 if (wpa_auth_set_key(wpa_auth
, group
->vlan_id
,
4182 wpa_cipher_to_alg(conf
->wpa_group
),
4183 broadcast_ether_addr
, group
->GN
,
4184 group
->GTK
[group
->GN
- 1], group
->GTK_len
,
4185 KEY_FLAG_GROUP_TX_DEFAULT
) < 0)
4188 if (conf
->ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
4192 alg
= wpa_cipher_to_alg(conf
->group_mgmt_cipher
);
4193 len
= wpa_cipher_key_len(conf
->group_mgmt_cipher
);
4196 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, alg
,
4197 broadcast_ether_addr
, group
->GN_igtk
,
4198 group
->IGTK
[group
->GN_igtk
- 4], len
,
4199 KEY_FLAG_GROUP_TX_DEFAULT
) < 0)
4202 if (ret
== 0 && conf
->beacon_prot
&&
4203 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, alg
,
4204 broadcast_ether_addr
, group
->GN_bigtk
,
4205 group
->BIGTK
[group
->GN_bigtk
- 6], len
,
4206 KEY_FLAG_GROUP_TX_DEFAULT
) < 0)
4214 static int wpa_group_disconnect_cb(struct wpa_state_machine
*sm
, void *ctx
)
4216 if (sm
->group
== ctx
) {
4217 wpa_printf(MSG_DEBUG
, "WPA: Mark STA " MACSTR
4218 " for discconnection due to fatal failure",
4220 sm
->Disconnect
= TRUE
;
4227 static void wpa_group_fatal_failure(struct wpa_authenticator
*wpa_auth
,
4228 struct wpa_group
*group
)
4230 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state FATAL_FAILURE");
4231 group
->changed
= TRUE
;
4232 group
->wpa_group_state
= WPA_GROUP_FATAL_FAILURE
;
4233 wpa_auth_for_each_sta(wpa_auth
, wpa_group_disconnect_cb
, group
);
4237 static int wpa_group_setkeysdone(struct wpa_authenticator
*wpa_auth
,
4238 struct wpa_group
*group
)
4240 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
4241 "SETKEYSDONE (VLAN-ID %d)", group
->vlan_id
);
4242 group
->changed
= TRUE
;
4243 group
->wpa_group_state
= WPA_GROUP_SETKEYSDONE
;
4245 if (wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
4246 wpa_group_fatal_failure(wpa_auth
, group
);
4254 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
4255 struct wpa_group
*group
)
4258 wpa_group_gtk_init(wpa_auth
, group
);
4259 } else if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
) {
4260 /* Do not allow group operations */
4261 } else if (group
->wpa_group_state
== WPA_GROUP_GTK_INIT
&&
4262 group
->GTKAuthenticator
) {
4263 wpa_group_setkeysdone(wpa_auth
, group
);
4264 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
&&
4266 wpa_group_setkeys(wpa_auth
, group
);
4267 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYS
) {
4268 if (group
->GKeyDoneStations
== 0)
4269 wpa_group_setkeysdone(wpa_auth
, group
);
4270 else if (group
->GTKReKey
)
4271 wpa_group_setkeys(wpa_auth
, group
);
4276 static int wpa_sm_step(struct wpa_state_machine
*sm
)
4281 if (sm
->in_step_loop
) {
4282 /* This should not happen, but if it does, make sure we do not
4283 * end up freeing the state machine too early by exiting the
4284 * recursive call. */
4285 wpa_printf(MSG_ERROR
, "WPA: wpa_sm_step() called recursively");
4289 sm
->in_step_loop
= 1;
4291 if (sm
->pending_deinit
)
4294 sm
->changed
= FALSE
;
4295 sm
->wpa_auth
->group
->changed
= FALSE
;
4297 SM_STEP_RUN(WPA_PTK
);
4298 if (sm
->pending_deinit
)
4300 SM_STEP_RUN(WPA_PTK_GROUP
);
4301 if (sm
->pending_deinit
)
4303 wpa_group_sm_step(sm
->wpa_auth
, sm
->group
);
4304 } while (sm
->changed
|| sm
->wpa_auth
->group
->changed
);
4305 sm
->in_step_loop
= 0;
4307 if (sm
->pending_deinit
) {
4308 wpa_printf(MSG_DEBUG
, "WPA: Completing pending STA state "
4309 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
4310 wpa_free_sta_sm(sm
);
4317 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
)
4319 struct wpa_state_machine
*sm
= eloop_ctx
;
4324 void wpa_auth_sm_notify(struct wpa_state_machine
*sm
)
4328 eloop_register_timeout(0, 0, wpa_sm_call_step
, sm
, NULL
);
4332 void wpa_gtk_rekey(struct wpa_authenticator
*wpa_auth
)
4335 struct wpa_group
*group
;
4337 if (wpa_auth
== NULL
)
4340 group
= wpa_auth
->group
;
4342 for (i
= 0; i
< 2; i
++) {
4344 group
->GM
= group
->GN
;
4346 tmp
= group
->GM_igtk
;
4347 group
->GM_igtk
= group
->GN_igtk
;
4348 group
->GN_igtk
= tmp
;
4349 tmp
= group
->GM_bigtk
;
4350 group
->GM_bigtk
= group
->GN_bigtk
;
4351 group
->GN_bigtk
= tmp
;
4352 wpa_gtk_update(wpa_auth
, group
);
4353 wpa_group_config_group_keys(wpa_auth
, group
);
4358 static const char * wpa_bool_txt(int val
)
4360 return val
? "TRUE" : "FALSE";
4364 #define RSN_SUITE "%02x-%02x-%02x-%d"
4365 #define RSN_SUITE_ARG(s) \
4366 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
4368 int wpa_get_mib(struct wpa_authenticator
*wpa_auth
, char *buf
, size_t buflen
)
4370 struct wpa_auth_config
*conf
;
4372 char pmkid_txt
[PMKID_LEN
* 2 + 1];
4373 #ifdef CONFIG_RSN_PREAUTH
4374 const int preauth
= 1;
4375 #else /* CONFIG_RSN_PREAUTH */
4376 const int preauth
= 0;
4377 #endif /* CONFIG_RSN_PREAUTH */
4379 if (wpa_auth
== NULL
)
4381 conf
= &wpa_auth
->conf
;
4383 ret
= os_snprintf(buf
+ len
, buflen
- len
,
4384 "dot11RSNAOptionImplemented=TRUE\n"
4385 "dot11RSNAPreauthenticationImplemented=%s\n"
4386 "dot11RSNAEnabled=%s\n"
4387 "dot11RSNAPreauthenticationEnabled=%s\n",
4388 wpa_bool_txt(preauth
),
4389 wpa_bool_txt(conf
->wpa
& WPA_PROTO_RSN
),
4390 wpa_bool_txt(conf
->rsn_preauth
));
4391 if (os_snprintf_error(buflen
- len
, ret
))
4395 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
4396 wpa_auth
->dot11RSNAPMKIDUsed
, PMKID_LEN
);
4399 buf
+ len
, buflen
- len
,
4400 "dot11RSNAConfigVersion=%u\n"
4401 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
4402 /* FIX: dot11RSNAConfigGroupCipher */
4403 /* FIX: dot11RSNAConfigGroupRekeyMethod */
4404 /* FIX: dot11RSNAConfigGroupRekeyTime */
4405 /* FIX: dot11RSNAConfigGroupRekeyPackets */
4406 "dot11RSNAConfigGroupRekeyStrict=%u\n"
4407 "dot11RSNAConfigGroupUpdateCount=%u\n"
4408 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
4409 "dot11RSNAConfigGroupCipherSize=%u\n"
4410 "dot11RSNAConfigPMKLifetime=%u\n"
4411 "dot11RSNAConfigPMKReauthThreshold=%u\n"
4412 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4413 "dot11RSNAConfigSATimeout=%u\n"
4414 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
4415 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
4416 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
4417 "dot11RSNAPMKIDUsed=%s\n"
4418 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
4419 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
4420 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
4421 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4422 "dot11RSNA4WayHandshakeFailures=%u\n"
4423 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4425 !!conf
->wpa_strict_rekey
,
4426 conf
->wpa_group_update_count
,
4427 conf
->wpa_pairwise_update_count
,
4428 wpa_cipher_key_len(conf
->wpa_group
) * 8,
4429 dot11RSNAConfigPMKLifetime
,
4430 dot11RSNAConfigPMKReauthThreshold
,
4431 dot11RSNAConfigSATimeout
,
4432 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteSelected
),
4433 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherSelected
),
4434 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherSelected
),
4436 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteRequested
),
4437 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherRequested
),
4438 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherRequested
),
4439 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
,
4440 wpa_auth
->dot11RSNA4WayHandshakeFailures
);
4441 if (os_snprintf_error(buflen
- len
, ret
))
4445 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
4446 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4449 ret
= os_snprintf(buf
+ len
, buflen
- len
, "hostapdWPAGroupState=%d\n",
4450 wpa_auth
->group
->wpa_group_state
);
4451 if (os_snprintf_error(buflen
- len
, ret
))
4459 int wpa_get_mib_sta(struct wpa_state_machine
*sm
, char *buf
, size_t buflen
)
4467 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4469 /* dot11RSNAStatsEntry */
4471 pairwise
= wpa_cipher_to_suite(sm
->wpa
== WPA_VERSION_WPA2
?
4472 WPA_PROTO_RSN
: WPA_PROTO_WPA
,
4478 buf
+ len
, buflen
- len
,
4479 /* TODO: dot11RSNAStatsIndex */
4480 "dot11RSNAStatsSTAAddress=" MACSTR
"\n"
4481 "dot11RSNAStatsVersion=1\n"
4482 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE
"\n"
4483 /* TODO: dot11RSNAStatsTKIPICVErrors */
4484 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
4485 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
4486 /* TODO: dot11RSNAStatsCCMPReplays */
4487 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
4488 /* TODO: dot11RSNAStatsTKIPReplays */,
4490 RSN_SUITE_ARG(pairwise
),
4491 sm
->dot11RSNAStatsTKIPLocalMICFailures
,
4492 sm
->dot11RSNAStatsTKIPRemoteMICFailures
);
4493 if (os_snprintf_error(buflen
- len
, ret
))
4498 ret
= os_snprintf(buf
+ len
, buflen
- len
,
4500 "AKMSuiteSelector=" RSN_SUITE
"\n"
4501 "hostapdWPAPTKState=%d\n"
4502 "hostapdWPAPTKGroupState=%d\n",
4504 RSN_SUITE_ARG(wpa_akm_to_suite(sm
->wpa_key_mgmt
)),
4506 sm
->wpa_ptk_group_state
);
4507 if (os_snprintf_error(buflen
- len
, ret
))
4515 void wpa_auth_countermeasures_start(struct wpa_authenticator
*wpa_auth
)
4518 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
++;
4522 int wpa_auth_pairwise_set(struct wpa_state_machine
*sm
)
4524 return sm
&& sm
->pairwise_set
;
4528 int wpa_auth_get_pairwise(struct wpa_state_machine
*sm
)
4530 return sm
->pairwise
;
4534 const u8
* wpa_auth_get_pmk(struct wpa_state_machine
*sm
, int *len
)
4543 int wpa_auth_sta_key_mgmt(struct wpa_state_machine
*sm
)
4547 return sm
->wpa_key_mgmt
;
4551 int wpa_auth_sta_wpa_version(struct wpa_state_machine
*sm
)
4559 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine
*sm
)
4561 if (!sm
|| !wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
4563 return sm
->tk_already_set
;
4567 int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine
*sm
)
4569 if (!sm
|| !wpa_key_mgmt_fils(sm
->wpa_key_mgmt
))
4571 return sm
->tk_already_set
;
4575 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine
*sm
,
4576 struct rsn_pmksa_cache_entry
*entry
)
4578 if (sm
== NULL
|| sm
->pmksa
!= entry
)
4585 struct rsn_pmksa_cache_entry
*
4586 wpa_auth_sta_get_pmksa(struct wpa_state_machine
*sm
)
4588 return sm
? sm
->pmksa
: NULL
;
4592 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine
*sm
)
4595 sm
->dot11RSNAStatsTKIPLocalMICFailures
++;
4599 const u8
* wpa_auth_get_wpa_ie(struct wpa_authenticator
*wpa_auth
, size_t *len
)
4601 if (wpa_auth
== NULL
)
4603 *len
= wpa_auth
->wpa_ie_len
;
4604 return wpa_auth
->wpa_ie
;
4608 int wpa_auth_pmksa_add(struct wpa_state_machine
*sm
, const u8
*pmk
,
4609 unsigned int pmk_len
,
4610 int session_timeout
, struct eapol_state_machine
*eapol
)
4612 if (sm
== NULL
|| sm
->wpa
!= WPA_VERSION_WPA2
||
4613 sm
->wpa_auth
->conf
.disable_pmksa_caching
)
4616 #ifdef CONFIG_IEEE80211R_AP
4617 if (pmk_len
>= 2 * PMK_LEN
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
) &&
4618 wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
4619 !wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
4620 /* Cache MPMK/XXKey instead of initial part from MSK */
4621 pmk
= pmk
+ PMK_LEN
;
4624 #endif /* CONFIG_IEEE80211R_AP */
4625 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
4626 if (pmk_len
> PMK_LEN_SUITE_B_192
)
4627 pmk_len
= PMK_LEN_SUITE_B_192
;
4628 } else if (pmk_len
> PMK_LEN
) {
4632 wpa_hexdump_key(MSG_DEBUG
, "RSN: Cache PMK", pmk
, pmk_len
);
4633 if (pmksa_cache_auth_add(sm
->wpa_auth
->pmksa
, pmk
, pmk_len
, NULL
,
4634 sm
->PTK
.kck
, sm
->PTK
.kck_len
,
4635 sm
->wpa_auth
->addr
, sm
->addr
, session_timeout
,
4636 eapol
, sm
->wpa_key_mgmt
))
4643 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator
*wpa_auth
,
4644 const u8
*pmk
, size_t len
, const u8
*sta_addr
,
4645 int session_timeout
,
4646 struct eapol_state_machine
*eapol
)
4648 if (wpa_auth
== NULL
)
4651 wpa_hexdump_key(MSG_DEBUG
, "RSN: Cache PMK from preauth", pmk
, len
);
4652 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, len
, NULL
,
4655 sta_addr
, session_timeout
, eapol
,
4656 WPA_KEY_MGMT_IEEE8021X
))
4663 int wpa_auth_pmksa_add_sae(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4664 const u8
*pmk
, const u8
*pmkid
)
4666 if (wpa_auth
->conf
.disable_pmksa_caching
)
4669 wpa_hexdump_key(MSG_DEBUG
, "RSN: Cache PMK from SAE", pmk
, PMK_LEN
);
4670 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, PMK_LEN
, pmkid
,
4672 wpa_auth
->addr
, addr
, 0, NULL
,
4680 void wpa_auth_add_sae_pmkid(struct wpa_state_machine
*sm
, const u8
*pmkid
)
4682 os_memcpy(sm
->pmkid
, pmkid
, PMKID_LEN
);
4687 int wpa_auth_pmksa_add2(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4688 const u8
*pmk
, size_t pmk_len
, const u8
*pmkid
,
4689 int session_timeout
, int akmp
)
4691 if (wpa_auth
->conf
.disable_pmksa_caching
)
4694 wpa_hexdump_key(MSG_DEBUG
, "RSN: Cache PMK (2)", pmk
, PMK_LEN
);
4695 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, pmk_len
, pmkid
,
4696 NULL
, 0, wpa_auth
->addr
, addr
, session_timeout
,
4704 void wpa_auth_pmksa_remove(struct wpa_authenticator
*wpa_auth
,
4707 struct rsn_pmksa_cache_entry
*pmksa
;
4709 if (wpa_auth
== NULL
|| wpa_auth
->pmksa
== NULL
)
4711 pmksa
= pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, NULL
);
4713 wpa_printf(MSG_DEBUG
, "WPA: Remove PMKSA cache entry for "
4714 MACSTR
" based on request", MAC2STR(sta_addr
));
4715 pmksa_cache_free_entry(wpa_auth
->pmksa
, pmksa
);
4720 int wpa_auth_pmksa_list(struct wpa_authenticator
*wpa_auth
, char *buf
,
4723 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4725 return pmksa_cache_auth_list(wpa_auth
->pmksa
, buf
, len
);
4729 void wpa_auth_pmksa_flush(struct wpa_authenticator
*wpa_auth
)
4731 if (wpa_auth
&& wpa_auth
->pmksa
)
4732 pmksa_cache_auth_flush(wpa_auth
->pmksa
);
4736 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4739 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4740 char *buf
, size_t len
)
4742 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4745 return pmksa_cache_auth_list_mesh(wpa_auth
->pmksa
, addr
, buf
, len
);
4749 struct rsn_pmksa_cache_entry
*
4750 wpa_auth_pmksa_create_entry(const u8
*aa
, const u8
*spa
, const u8
*pmk
,
4751 const u8
*pmkid
, int expiration
)
4753 struct rsn_pmksa_cache_entry
*entry
;
4754 struct os_reltime now
;
4756 entry
= pmksa_cache_auth_create_entry(pmk
, PMK_LEN
, pmkid
, NULL
, 0, aa
,
4757 spa
, 0, NULL
, WPA_KEY_MGMT_SAE
);
4761 os_get_reltime(&now
);
4762 entry
->expiration
= now
.sec
+ expiration
;
4767 int wpa_auth_pmksa_add_entry(struct wpa_authenticator
*wpa_auth
,
4768 struct rsn_pmksa_cache_entry
*entry
)
4772 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4775 ret
= pmksa_cache_auth_add_entry(wpa_auth
->pmksa
, entry
);
4777 wpa_printf(MSG_DEBUG
,
4778 "RSN: Failed to store external PMKSA cache for "
4779 MACSTR
, MAC2STR(entry
->spa
));
4784 #endif /* CONFIG_MESH */
4785 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4788 struct rsn_pmksa_cache_entry
*
4789 wpa_auth_pmksa_get(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
,
4792 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4794 return pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, pmkid
);
4798 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry
*pmksa
,
4799 struct wpa_state_machine
*sm
,
4800 struct wpa_authenticator
*wpa_auth
,
4807 os_memcpy(pmk
, pmksa
->pmk
, PMK_LEN
);
4808 os_memcpy(pmkid
, pmksa
->pmkid
, PMKID_LEN
);
4809 os_memcpy(wpa_auth
->dot11RSNAPMKIDUsed
, pmksa
->pmkid
, PMKID_LEN
);
4814 * Remove and free the group from wpa_authenticator. This is triggered by a
4815 * callback to make sure nobody is currently iterating the group list while it
4818 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
4819 struct wpa_group
*group
)
4821 struct wpa_group
*prev
= wpa_auth
->group
;
4823 wpa_printf(MSG_DEBUG
, "WPA: Remove group state machine for VLAN-ID %d",
4827 if (prev
->next
== group
) {
4828 /* This never frees the special first group as needed */
4829 prev
->next
= group
->next
;
4839 /* Increase the reference counter for group */
4840 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
4841 struct wpa_group
*group
)
4843 /* Skip the special first group */
4844 if (wpa_auth
->group
== group
)
4847 group
->references
++;
4851 /* Decrease the reference counter and maybe free the group */
4852 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
4853 struct wpa_group
*group
)
4855 /* Skip the special first group */
4856 if (wpa_auth
->group
== group
)
4859 group
->references
--;
4860 if (group
->references
)
4862 wpa_group_free(wpa_auth
, group
);
4867 * Add a group that has its references counter set to zero. Caller needs to
4868 * call wpa_group_get() on the return value to mark the entry in use.
4870 static struct wpa_group
*
4871 wpa_auth_add_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4873 struct wpa_group
*group
;
4875 if (wpa_auth
== NULL
|| wpa_auth
->group
== NULL
)
4878 wpa_printf(MSG_DEBUG
, "WPA: Add group state machine for VLAN-ID %d",
4880 group
= wpa_group_init(wpa_auth
, vlan_id
, 0);
4884 group
->next
= wpa_auth
->group
->next
;
4885 wpa_auth
->group
->next
= group
;
4892 * Enforce that the group state machine for the VLAN is running, increase
4893 * reference counter as interface is up. References might have been increased
4894 * even if a negative value is returned.
4895 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4897 int wpa_auth_ensure_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4899 struct wpa_group
*group
;
4901 if (wpa_auth
== NULL
)
4904 group
= wpa_auth
->group
;
4906 if (group
->vlan_id
== vlan_id
)
4908 group
= group
->next
;
4911 if (group
== NULL
) {
4912 group
= wpa_auth_add_group(wpa_auth
, vlan_id
);
4917 wpa_printf(MSG_DEBUG
,
4918 "WPA: Ensure group state machine running for VLAN ID %d",
4921 wpa_group_get(wpa_auth
, group
);
4922 group
->num_setup_iface
++;
4924 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4932 * Decrease reference counter, expected to be zero afterwards.
4933 * returns: -1 on error (group not found, group in fail state)
4934 * -2 if wpa_group is still referenced
4937 int wpa_auth_release_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4939 struct wpa_group
*group
;
4942 if (wpa_auth
== NULL
)
4945 group
= wpa_auth
->group
;
4947 if (group
->vlan_id
== vlan_id
)
4949 group
= group
->next
;
4955 wpa_printf(MSG_DEBUG
,
4956 "WPA: Try stopping group state machine for VLAN ID %d",
4959 if (group
->num_setup_iface
<= 0) {
4960 wpa_printf(MSG_ERROR
,
4961 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4965 group
->num_setup_iface
--;
4967 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4970 if (group
->references
> 1) {
4971 wpa_printf(MSG_DEBUG
,
4972 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4977 wpa_group_put(wpa_auth
, group
);
4983 int wpa_auth_sta_set_vlan(struct wpa_state_machine
*sm
, int vlan_id
)
4985 struct wpa_group
*group
;
4987 if (sm
== NULL
|| sm
->wpa_auth
== NULL
)
4990 group
= sm
->wpa_auth
->group
;
4992 if (group
->vlan_id
== vlan_id
)
4994 group
= group
->next
;
4997 if (group
== NULL
) {
4998 group
= wpa_auth_add_group(sm
->wpa_auth
, vlan_id
);
5003 if (sm
->group
== group
)
5006 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
5009 wpa_printf(MSG_DEBUG
, "WPA: Moving STA " MACSTR
" to use group state "
5010 "machine for VLAN ID %d", MAC2STR(sm
->addr
), vlan_id
);
5012 wpa_group_get(sm
->wpa_auth
, group
);
5013 wpa_group_put(sm
->wpa_auth
, sm
->group
);
5020 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator
*wpa_auth
,
5021 struct wpa_state_machine
*sm
, int ack
)
5023 if (wpa_auth
== NULL
|| sm
== NULL
)
5025 wpa_printf(MSG_DEBUG
, "WPA: EAPOL-Key TX status for STA " MACSTR
5026 " ack=%d", MAC2STR(sm
->addr
), ack
);
5027 if (sm
->pending_1_of_4_timeout
&& ack
) {
5029 * Some deployed supplicant implementations update their SNonce
5030 * for each EAPOL-Key 2/4 message even within the same 4-way
5031 * handshake and then fail to use the first SNonce when
5032 * deriving the PTK. This results in unsuccessful 4-way
5033 * handshake whenever the relatively short initial timeout is
5034 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
5035 * around this by increasing the timeout now that we know that
5036 * the station has received the frame.
5038 int timeout_ms
= eapol_key_timeout_subseq
;
5039 wpa_printf(MSG_DEBUG
, "WPA: Increase initial EAPOL-Key 1/4 "
5040 "timeout by %u ms because of acknowledged frame",
5042 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
5043 eloop_register_timeout(timeout_ms
/ 1000,
5044 (timeout_ms
% 1000) * 1000,
5045 wpa_send_eapol_timeout
, wpa_auth
, sm
);
5048 #ifdef CONFIG_TESTING_OPTIONS
5049 if (sm
->eapol_status_cb
) {
5050 sm
->eapol_status_cb(sm
->eapol_status_cb_ctx1
,
5051 sm
->eapol_status_cb_ctx2
);
5052 sm
->eapol_status_cb
= NULL
;
5054 #endif /* CONFIG_TESTING_OPTIONS */
5058 int wpa_auth_uses_sae(struct wpa_state_machine
*sm
)
5062 return wpa_key_mgmt_sae(sm
->wpa_key_mgmt
);
5066 int wpa_auth_uses_ft_sae(struct wpa_state_machine
*sm
)
5070 return sm
->wpa_key_mgmt
== WPA_KEY_MGMT_FT_SAE
;
5075 int wpa_auth_get_ip_addr(struct wpa_state_machine
*sm
, u8
*addr
)
5077 if (sm
== NULL
|| WPA_GET_BE32(sm
->ip_addr
) == 0)
5079 os_memcpy(addr
, sm
->ip_addr
, 4);
5082 #endif /* CONFIG_P2P */
5085 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator
*wpa_auth
,
5086 struct radius_das_attrs
*attr
)
5088 return pmksa_cache_auth_radius_das_disconnect(wpa_auth
->pmksa
, attr
);
5092 void wpa_auth_reconfig_group_keys(struct wpa_authenticator
*wpa_auth
)
5094 struct wpa_group
*group
;
5098 for (group
= wpa_auth
->group
; group
; group
= group
->next
)
5099 wpa_group_config_group_keys(wpa_auth
, group
);
5105 struct wpa_auth_fils_iter_data
{
5106 struct wpa_authenticator
*auth
;
5108 struct rsn_pmksa_cache_entry
*pmksa
;
5114 static int wpa_auth_fils_iter(struct wpa_authenticator
*a
, void *ctx
)
5116 struct wpa_auth_fils_iter_data
*data
= ctx
;
5118 if (a
== data
->auth
|| !a
->conf
.fils_cache_id_set
||
5119 os_memcmp(a
->conf
.fils_cache_id
, data
->cache_id
,
5120 FILS_CACHE_ID_LEN
) != 0)
5122 data
->pmksa
= pmksa_cache_auth_get(a
->pmksa
, data
->spa
, data
->pmkid
);
5123 return data
->pmksa
!= NULL
;
5127 struct rsn_pmksa_cache_entry
*
5128 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator
*wpa_auth
,
5129 const u8
*sta_addr
, const u8
*pmkid
)
5131 struct wpa_auth_fils_iter_data idata
;
5133 if (!wpa_auth
->conf
.fils_cache_id_set
)
5135 idata
.auth
= wpa_auth
;
5136 idata
.cache_id
= wpa_auth
->conf
.fils_cache_id
;
5138 idata
.spa
= sta_addr
;
5139 idata
.pmkid
= pmkid
;
5140 wpa_auth_for_each_auth(wpa_auth
, wpa_auth_fils_iter
, &idata
);
5145 #ifdef CONFIG_IEEE80211R_AP
5146 int wpa_auth_write_fte(struct wpa_authenticator
*wpa_auth
, int use_sha384
,
5147 u8
*buf
, size_t len
)
5149 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
5151 return wpa_write_ftie(conf
, use_sha384
, conf
->r0_key_holder
,
5152 conf
->r0_key_holder_len
,
5153 NULL
, NULL
, buf
, len
, NULL
, 0, 0);
5155 #endif /* CONFIG_IEEE80211R_AP */
5158 void wpa_auth_get_fils_aead_params(struct wpa_state_machine
*sm
,
5159 u8
*fils_anonce
, u8
*fils_snonce
,
5160 u8
*fils_kek
, size_t *fils_kek_len
)
5162 os_memcpy(fils_anonce
, sm
->ANonce
, WPA_NONCE_LEN
);
5163 os_memcpy(fils_snonce
, sm
->SNonce
, WPA_NONCE_LEN
);
5164 os_memcpy(fils_kek
, sm
->PTK
.kek
, WPA_KEK_MAX_LEN
);
5165 *fils_kek_len
= sm
->PTK
.kek_len
;
5169 void wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine
*sm
, const u8
*pmk
,
5170 size_t pmk_len
, const u8
*pmkid
)
5172 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
5173 sm
->pmk_len
= pmk_len
;
5174 os_memcpy(sm
->pmkid
, pmkid
, PMKID_LEN
);
5178 #endif /* CONFIG_FILS */
5181 void wpa_auth_set_auth_alg(struct wpa_state_machine
*sm
, u16 auth_alg
)
5184 sm
->auth_alg
= auth_alg
;
5189 void wpa_auth_set_dpp_z(struct wpa_state_machine
*sm
, const struct wpabuf
*z
)
5192 wpabuf_clear_free(sm
->dpp_z
);
5193 sm
->dpp_z
= z
? wpabuf_dup(z
) : NULL
;
5196 #endif /* CONFIG_DPP2 */
5199 #ifdef CONFIG_TESTING_OPTIONS
5201 int wpa_auth_resend_m1(struct wpa_state_machine
*sm
, int change_anonce
,
5202 void (*cb
)(void *ctx1
, void *ctx2
),
5203 void *ctx1
, void *ctx2
)
5205 const u8
*anonce
= sm
->ANonce
;
5206 u8 anonce_buf
[WPA_NONCE_LEN
];
5208 if (change_anonce
) {
5209 if (random_get_bytes(anonce_buf
, WPA_NONCE_LEN
))
5211 anonce
= anonce_buf
;
5214 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5215 "sending 1/4 msg of 4-Way Handshake (TESTING)");
5216 wpa_send_eapol(sm
->wpa_auth
, sm
,
5217 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
5218 anonce
, NULL
, 0, 0, 0);
5223 int wpa_auth_resend_m3(struct wpa_state_machine
*sm
,
5224 void (*cb
)(void *ctx1
, void *ctx2
),
5225 void *ctx1
, void *ctx2
)
5227 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
;
5229 size_t gtk_len
, kde_len
;
5230 struct wpa_group
*gsm
= sm
->group
;
5232 int wpa_ie_len
, secure
, gtkidx
, encr
= 0;
5235 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
5236 GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2])
5240 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
5241 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
5242 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
5243 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
5244 if (sm
->wpa
== WPA_VERSION_WPA
&&
5245 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
5246 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
5247 /* WPA-only STA, remove RSN IE and possible MDIE */
5248 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
5249 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
5250 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
5251 wpa_ie_len
= wpa_ie
[1] + 2;
5253 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5254 "sending 3/4 msg of 4-Way Handshake (TESTING)");
5255 if (sm
->wpa
== WPA_VERSION_WPA2
) {
5256 /* WPA2 send GTK in the 4-way handshake */
5258 gtk
= gsm
->GTK
[gsm
->GN
- 1];
5259 gtk_len
= gsm
->GTK_len
;
5264 /* WPA does not include GTK in msg 3/4 */
5269 if (sm
->rx_eapol_key_secure
) {
5271 * It looks like Windows 7 supplicant tries to use
5272 * Secure bit in msg 2/4 after having reported Michael
5273 * MIC failure and it then rejects the 4-way handshake
5274 * if msg 3/4 does not set Secure bit. Work around this
5275 * by setting the Secure bit here even in the case of
5276 * WPA if the supplicant used it first.
5278 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5279 "STA used Secure bit in WPA msg 2/4 - "
5280 "set Secure for 3/4 as workaround");
5285 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
5287 if (sm
->use_ext_key_id
)
5288 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2;
5291 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
5292 #ifdef CONFIG_IEEE80211R_AP
5293 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
5294 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
5295 kde_len
+= 300; /* FTIE + 2 * TIE */
5297 #endif /* CONFIG_IEEE80211R_AP */
5298 kde
= os_malloc(kde_len
);
5303 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
5305 #ifdef CONFIG_IEEE80211R_AP
5306 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
5311 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
5313 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
5314 "PMKR1Name into RSN IE in EAPOL-Key data");
5321 #endif /* CONFIG_IEEE80211R_AP */
5324 if (sm
->use_ext_key_id
) {
5325 hdr
[0] = sm
->keyidx_active
& 0x01;
5326 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_KEYID
, hdr
, 2, NULL
, 0);
5330 hdr
[0] = gtkidx
& 0x03;
5331 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
5335 pos
= ieee80211w_kde_add(sm
, pos
);
5336 if (pos
- opos
>= 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
) {
5337 /* skip KDE header and keyid */
5338 opos
+= 2 + RSN_SELECTOR_LEN
+ 2;
5339 os_memset(opos
, 0, 6); /* clear PN */
5341 if (ocv_oci_add(sm
, &pos
) < 0) {
5346 #ifdef CONFIG_IEEE80211R_AP
5347 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
5349 struct wpa_auth_config
*conf
;
5351 conf
= &sm
->wpa_auth
->conf
;
5352 if (sm
->assoc_resp_ftie
&&
5353 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
5354 os_memcpy(pos
, sm
->assoc_resp_ftie
,
5355 2 + sm
->assoc_resp_ftie
[1]);
5356 res
= 2 + sm
->assoc_resp_ftie
[1];
5358 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
5360 res
= wpa_write_ftie(conf
, use_sha384
,
5361 conf
->r0_key_holder
,
5362 conf
->r0_key_holder_len
,
5364 kde
+ kde_len
- pos
,
5368 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
5369 "into EAPOL-Key Key Data");
5375 /* TIE[ReassociationDeadline] (TU) */
5376 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
5378 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
5379 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
5382 /* TIE[KeyLifetime] (seconds) */
5383 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
5385 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
5386 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
);
5389 #endif /* CONFIG_IEEE80211R_AP */
5391 wpa_send_eapol(sm
->wpa_auth
, sm
,
5392 (secure
? WPA_KEY_INFO_SECURE
: 0) |
5393 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
5394 WPA_KEY_INFO_MIC
: 0) |
5395 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
5396 WPA_KEY_INFO_KEY_TYPE
,
5397 _rsc
, sm
->ANonce
, kde
, pos
- kde
, 0, encr
);
5403 int wpa_auth_resend_group_m1(struct wpa_state_machine
*sm
,
5404 void (*cb
)(void *ctx1
, void *ctx2
),
5405 void *ctx1
, void *ctx2
)
5407 u8 rsc
[WPA_KEY_RSC_LEN
];
5408 struct wpa_group
*gsm
= sm
->group
;
5410 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
5415 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
5416 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
5418 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5419 "sending 1/2 msg of Group Key Handshake (TESTING)");
5421 gtk
= gsm
->GTK
[gsm
->GN
- 1];
5422 if (sm
->wpa
== WPA_VERSION_WPA2
) {
5423 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
5424 ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
5425 kde_buf
= os_malloc(kde_len
);
5426 if (kde_buf
== NULL
)
5429 kde
= pos
= kde_buf
;
5430 hdr
[0] = gsm
->GN
& 0x03;
5432 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
5435 pos
= ieee80211w_kde_add(sm
, pos
);
5437 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
) {
5438 /* skip KDE header and keyid */
5439 opos
+= 2 + RSN_SELECTOR_LEN
+ 2;
5440 os_memset(opos
, 0, 6); /* clear PN */
5442 if (ocv_oci_add(sm
, &pos
) < 0) {
5446 kde_len
= pos
- kde
;
5449 kde_len
= gsm
->GTK_len
;
5452 sm
->eapol_status_cb
= cb
;
5453 sm
->eapol_status_cb_ctx1
= ctx1
;
5454 sm
->eapol_status_cb_ctx2
= ctx2
;
5456 wpa_send_eapol(sm
->wpa_auth
, sm
,
5457 WPA_KEY_INFO_SECURE
|
5458 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
5459 WPA_KEY_INFO_MIC
: 0) |
5461 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
5462 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
5469 int wpa_auth_rekey_gtk(struct wpa_authenticator
*wpa_auth
)
5473 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
5474 return eloop_register_timeout(0, 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
5477 #endif /* CONFIG_TESTING_OPTIONS */