2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2015, 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 "crypto/aes.h"
17 #include "crypto/aes_wrap.h"
18 #include "crypto/aes_siv.h"
19 #include "crypto/crypto.h"
20 #include "crypto/sha1.h"
21 #include "crypto/sha256.h"
22 #include "crypto/random.h"
23 #include "eapol_auth/eapol_auth_sm.h"
24 #include "ap_config.h"
25 #include "ieee802_11.h"
27 #include "pmksa_cache_auth.h"
28 #include "wpa_auth_i.h"
29 #include "wpa_auth_ie.h"
31 #define STATE_MACHINE_DATA struct wpa_state_machine
32 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
33 #define STATE_MACHINE_ADDR sm->addr
36 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
);
37 static int wpa_sm_step(struct wpa_state_machine
*sm
);
38 static int wpa_verify_key_mic(int akmp
, size_t pmk_len
, struct wpa_ptk
*PTK
,
39 u8
*data
, size_t data_len
);
41 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
42 u8
*buf
, size_t buf_len
, u16
*_key_data_len
);
43 static struct wpabuf
* fils_prepare_plainbuf(struct wpa_state_machine
*sm
,
44 const struct wpabuf
*hlp
);
45 #endif /* CONFIG_FILS */
46 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
);
47 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
48 struct wpa_group
*group
);
49 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
);
50 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
51 struct wpa_group
*group
);
52 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
53 struct wpa_group
*group
);
54 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
55 const u8
*pmk
, unsigned int pmk_len
,
57 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
58 struct wpa_group
*group
);
59 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
60 struct wpa_group
*group
);
61 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
62 struct wpa_group
*group
);
63 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
);
65 static const u32 eapol_key_timeout_first
= 100; /* ms */
66 static const u32 eapol_key_timeout_subseq
= 1000; /* ms */
67 static const u32 eapol_key_timeout_first_group
= 500; /* ms */
69 /* TODO: make these configurable */
70 static const int dot11RSNAConfigPMKLifetime
= 43200;
71 static const int dot11RSNAConfigPMKReauthThreshold
= 70;
72 static const int dot11RSNAConfigSATimeout
= 60;
75 static inline int wpa_auth_mic_failure_report(
76 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
78 if (wpa_auth
->cb
->mic_failure_report
)
79 return wpa_auth
->cb
->mic_failure_report(wpa_auth
->cb_ctx
, addr
);
84 static inline void wpa_auth_psk_failure_report(
85 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
87 if (wpa_auth
->cb
->psk_failure_report
)
88 wpa_auth
->cb
->psk_failure_report(wpa_auth
->cb_ctx
, addr
);
92 static inline void wpa_auth_set_eapol(struct wpa_authenticator
*wpa_auth
,
93 const u8
*addr
, wpa_eapol_variable var
,
96 if (wpa_auth
->cb
->set_eapol
)
97 wpa_auth
->cb
->set_eapol(wpa_auth
->cb_ctx
, addr
, var
, value
);
101 static inline int wpa_auth_get_eapol(struct wpa_authenticator
*wpa_auth
,
102 const u8
*addr
, wpa_eapol_variable var
)
104 if (wpa_auth
->cb
->get_eapol
== NULL
)
106 return wpa_auth
->cb
->get_eapol(wpa_auth
->cb_ctx
, addr
, var
);
110 static inline const u8
* wpa_auth_get_psk(struct wpa_authenticator
*wpa_auth
,
112 const u8
*p2p_dev_addr
,
113 const u8
*prev_psk
, size_t *psk_len
)
115 if (wpa_auth
->cb
->get_psk
== NULL
)
117 return wpa_auth
->cb
->get_psk(wpa_auth
->cb_ctx
, addr
, p2p_dev_addr
,
122 static inline int wpa_auth_get_msk(struct wpa_authenticator
*wpa_auth
,
123 const u8
*addr
, u8
*msk
, size_t *len
)
125 if (wpa_auth
->cb
->get_msk
== NULL
)
127 return wpa_auth
->cb
->get_msk(wpa_auth
->cb_ctx
, addr
, msk
, len
);
131 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
133 enum wpa_alg alg
, const u8
*addr
, int idx
,
134 u8
*key
, size_t key_len
)
136 if (wpa_auth
->cb
->set_key
== NULL
)
138 return wpa_auth
->cb
->set_key(wpa_auth
->cb_ctx
, vlan_id
, alg
, addr
, idx
,
143 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
144 const u8
*addr
, int idx
, u8
*seq
)
146 if (wpa_auth
->cb
->get_seqnum
== NULL
)
148 return wpa_auth
->cb
->get_seqnum(wpa_auth
->cb_ctx
, addr
, idx
, seq
);
153 wpa_auth_send_eapol(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
154 const u8
*data
, size_t data_len
, int encrypt
)
156 if (wpa_auth
->cb
->send_eapol
== NULL
)
158 return wpa_auth
->cb
->send_eapol(wpa_auth
->cb_ctx
, addr
, data
, data_len
,
164 static inline int wpa_auth_start_ampe(struct wpa_authenticator
*wpa_auth
,
167 if (wpa_auth
->cb
->start_ampe
== NULL
)
169 return wpa_auth
->cb
->start_ampe(wpa_auth
->cb_ctx
, addr
);
171 #endif /* CONFIG_MESH */
174 int wpa_auth_for_each_sta(struct wpa_authenticator
*wpa_auth
,
175 int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
178 if (wpa_auth
->cb
->for_each_sta
== NULL
)
180 return wpa_auth
->cb
->for_each_sta(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
184 int wpa_auth_for_each_auth(struct wpa_authenticator
*wpa_auth
,
185 int (*cb
)(struct wpa_authenticator
*a
, void *ctx
),
188 if (wpa_auth
->cb
->for_each_auth
== NULL
)
190 return wpa_auth
->cb
->for_each_auth(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
194 void wpa_auth_logger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
195 logger_level level
, const char *txt
)
197 if (wpa_auth
->cb
->logger
== NULL
)
199 wpa_auth
->cb
->logger(wpa_auth
->cb_ctx
, addr
, level
, txt
);
203 void wpa_auth_vlogger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
204 logger_level level
, const char *fmt
, ...)
210 if (wpa_auth
->cb
->logger
== NULL
)
213 maxlen
= os_strlen(fmt
) + 100;
214 format
= os_malloc(maxlen
);
219 vsnprintf(format
, maxlen
, fmt
, ap
);
222 wpa_auth_logger(wpa_auth
, addr
, level
, format
);
228 static void wpa_sta_disconnect(struct wpa_authenticator
*wpa_auth
,
229 const u8
*addr
, u16 reason
)
231 if (wpa_auth
->cb
->disconnect
== NULL
)
233 wpa_printf(MSG_DEBUG
, "wpa_sta_disconnect STA " MACSTR
" (reason %u)",
234 MAC2STR(addr
), reason
);
235 wpa_auth
->cb
->disconnect(wpa_auth
->cb_ctx
, addr
, reason
);
239 static int wpa_use_aes_cmac(struct wpa_state_machine
*sm
)
242 #ifdef CONFIG_IEEE80211R_AP
243 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
245 #endif /* CONFIG_IEEE80211R_AP */
246 #ifdef CONFIG_IEEE80211W
247 if (wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
))
249 #endif /* CONFIG_IEEE80211W */
250 if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
)
256 static void wpa_rekey_gmk(void *eloop_ctx
, void *timeout_ctx
)
258 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
260 if (random_get_bytes(wpa_auth
->group
->GMK
, WPA_GMK_LEN
)) {
261 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
264 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "GMK rekeyd");
265 wpa_hexdump_key(MSG_DEBUG
, "GMK",
266 wpa_auth
->group
->GMK
, WPA_GMK_LEN
);
269 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
270 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
271 wpa_rekey_gmk
, wpa_auth
, NULL
);
276 static void wpa_rekey_gtk(void *eloop_ctx
, void *timeout_ctx
)
278 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
279 struct wpa_group
*group
, *next
;
281 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "rekeying GTK");
282 group
= wpa_auth
->group
;
284 wpa_group_get(wpa_auth
, group
);
286 group
->GTKReKey
= TRUE
;
288 group
->changed
= FALSE
;
289 wpa_group_sm_step(wpa_auth
, group
);
290 } while (group
->changed
);
293 wpa_group_put(wpa_auth
, group
);
297 if (wpa_auth
->conf
.wpa_group_rekey
) {
298 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
,
299 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
304 static void wpa_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
306 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
307 struct wpa_state_machine
*sm
= timeout_ctx
;
309 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "rekeying PTK");
310 wpa_request_new_ptk(sm
);
315 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine
*sm
, void *ctx
)
317 if (sm
->pmksa
== ctx
)
323 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
326 struct wpa_authenticator
*wpa_auth
= ctx
;
327 wpa_auth_for_each_sta(wpa_auth
, wpa_auth_pmksa_clear_cb
, entry
);
331 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator
*wpa_auth
,
332 struct wpa_group
*group
)
334 u8 buf
[ETH_ALEN
+ 8 + sizeof(unsigned long)];
338 if (random_get_bytes(group
->GMK
, WPA_GMK_LEN
) < 0)
340 wpa_hexdump_key(MSG_DEBUG
, "GMK", group
->GMK
, WPA_GMK_LEN
);
343 * Counter = PRF-256(Random number, "Init Counter",
344 * Local MAC Address || Time)
346 os_memcpy(buf
, wpa_auth
->addr
, ETH_ALEN
);
347 wpa_get_ntp_timestamp(buf
+ ETH_ALEN
);
348 ptr
= (unsigned long) group
;
349 os_memcpy(buf
+ ETH_ALEN
+ 8, &ptr
, sizeof(ptr
));
350 if (random_get_bytes(rkey
, sizeof(rkey
)) < 0)
353 if (sha1_prf(rkey
, sizeof(rkey
), "Init Counter", buf
, sizeof(buf
),
354 group
->Counter
, WPA_NONCE_LEN
) < 0)
356 wpa_hexdump_key(MSG_DEBUG
, "Key Counter",
357 group
->Counter
, WPA_NONCE_LEN
);
363 static struct wpa_group
* wpa_group_init(struct wpa_authenticator
*wpa_auth
,
364 int vlan_id
, int delay_init
)
366 struct wpa_group
*group
;
368 group
= os_zalloc(sizeof(struct wpa_group
));
372 group
->GTKAuthenticator
= TRUE
;
373 group
->vlan_id
= vlan_id
;
374 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
376 if (random_pool_ready() != 1) {
377 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
378 "for secure operations - update keys later when "
379 "the first station connects");
383 * Set initial GMK/Counter value here. The actual values that will be
384 * used in negotiations will be set once the first station tries to
385 * connect. This allows more time for collecting additional randomness
386 * on embedded devices.
388 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0) {
389 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
397 wpa_printf(MSG_DEBUG
, "WPA: Delay group state machine start "
398 "until Beacon frames have been configured");
399 /* Initialization is completed in wpa_init_keys(). */
401 wpa_group_sm_step(wpa_auth
, group
);
402 group
->GInit
= FALSE
;
403 wpa_group_sm_step(wpa_auth
, group
);
411 * wpa_init - Initialize WPA authenticator
412 * @addr: Authenticator address
413 * @conf: Configuration for WPA authenticator
414 * @cb: Callback functions for WPA authenticator
415 * Returns: Pointer to WPA authenticator data or %NULL on failure
417 struct wpa_authenticator
* wpa_init(const u8
*addr
,
418 struct wpa_auth_config
*conf
,
419 const struct wpa_auth_callbacks
*cb
,
422 struct wpa_authenticator
*wpa_auth
;
424 wpa_auth
= os_zalloc(sizeof(struct wpa_authenticator
));
425 if (wpa_auth
== NULL
)
427 os_memcpy(wpa_auth
->addr
, addr
, ETH_ALEN
);
428 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
430 wpa_auth
->cb_ctx
= cb_ctx
;
432 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
433 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
438 wpa_auth
->group
= wpa_group_init(wpa_auth
, 0, 1);
439 if (wpa_auth
->group
== NULL
) {
440 os_free(wpa_auth
->wpa_ie
);
445 wpa_auth
->pmksa
= pmksa_cache_auth_init(wpa_auth_pmksa_free_cb
,
447 if (wpa_auth
->pmksa
== NULL
) {
448 wpa_printf(MSG_ERROR
, "PMKSA cache initialization failed.");
449 os_free(wpa_auth
->group
);
450 os_free(wpa_auth
->wpa_ie
);
455 #ifdef CONFIG_IEEE80211R_AP
456 wpa_auth
->ft_pmk_cache
= wpa_ft_pmk_cache_init();
457 if (wpa_auth
->ft_pmk_cache
== NULL
) {
458 wpa_printf(MSG_ERROR
, "FT PMK cache initialization failed.");
459 os_free(wpa_auth
->group
);
460 os_free(wpa_auth
->wpa_ie
);
461 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
465 #endif /* CONFIG_IEEE80211R_AP */
467 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
468 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
469 wpa_rekey_gmk
, wpa_auth
, NULL
);
472 if (wpa_auth
->conf
.wpa_group_rekey
) {
473 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
, 0,
474 wpa_rekey_gtk
, wpa_auth
, NULL
);
478 if (WPA_GET_BE32(conf
->ip_addr_start
)) {
479 int count
= WPA_GET_BE32(conf
->ip_addr_end
) -
480 WPA_GET_BE32(conf
->ip_addr_start
) + 1;
484 wpa_auth
->ip_pool
= bitfield_alloc(count
);
486 #endif /* CONFIG_P2P */
492 int wpa_init_keys(struct wpa_authenticator
*wpa_auth
)
494 struct wpa_group
*group
= wpa_auth
->group
;
496 wpa_printf(MSG_DEBUG
, "WPA: Start group state machine to set initial "
498 wpa_group_sm_step(wpa_auth
, group
);
499 group
->GInit
= FALSE
;
500 wpa_group_sm_step(wpa_auth
, group
);
501 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
508 * wpa_deinit - Deinitialize WPA authenticator
509 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
511 void wpa_deinit(struct wpa_authenticator
*wpa_auth
)
513 struct wpa_group
*group
, *prev
;
515 eloop_cancel_timeout(wpa_rekey_gmk
, wpa_auth
, NULL
);
516 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
518 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
520 #ifdef CONFIG_IEEE80211R_AP
521 wpa_ft_pmk_cache_deinit(wpa_auth
->ft_pmk_cache
);
522 wpa_auth
->ft_pmk_cache
= NULL
;
523 wpa_ft_deinit(wpa_auth
);
524 #endif /* CONFIG_IEEE80211R_AP */
527 bitfield_free(wpa_auth
->ip_pool
);
528 #endif /* CONFIG_P2P */
531 os_free(wpa_auth
->wpa_ie
);
533 group
= wpa_auth
->group
;
545 * wpa_reconfig - Update WPA authenticator configuration
546 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
547 * @conf: Configuration for WPA authenticator
549 int wpa_reconfig(struct wpa_authenticator
*wpa_auth
,
550 struct wpa_auth_config
*conf
)
552 struct wpa_group
*group
;
553 if (wpa_auth
== NULL
)
556 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
557 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
558 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
563 * Reinitialize GTK to make sure it is suitable for the new
566 group
= wpa_auth
->group
;
567 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
569 wpa_group_sm_step(wpa_auth
, group
);
570 group
->GInit
= FALSE
;
571 wpa_group_sm_step(wpa_auth
, group
);
577 struct wpa_state_machine
*
578 wpa_auth_sta_init(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
579 const u8
*p2p_dev_addr
)
581 struct wpa_state_machine
*sm
;
583 if (wpa_auth
->group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
586 sm
= os_zalloc(sizeof(struct wpa_state_machine
));
589 os_memcpy(sm
->addr
, addr
, ETH_ALEN
);
591 os_memcpy(sm
->p2p_dev_addr
, p2p_dev_addr
, ETH_ALEN
);
593 sm
->wpa_auth
= wpa_auth
;
594 sm
->group
= wpa_auth
->group
;
595 wpa_group_get(sm
->wpa_auth
, sm
->group
);
601 int wpa_auth_sta_associated(struct wpa_authenticator
*wpa_auth
,
602 struct wpa_state_machine
*sm
)
604 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
607 #ifdef CONFIG_IEEE80211R_AP
608 if (sm
->ft_completed
) {
609 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
610 "FT authentication already completed - do not "
611 "start 4-way handshake");
612 /* Go to PTKINITDONE state to allow GTK rekeying */
613 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
617 #endif /* CONFIG_IEEE80211R_AP */
620 if (sm
->fils_completed
) {
621 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
622 "FILS authentication already completed - do not start 4-way handshake");
623 /* Go to PTKINITDONE state to allow GTK rekeying */
624 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
628 #endif /* CONFIG_FILS */
631 os_memset(&sm
->key_replay
, 0, sizeof(sm
->key_replay
));
632 sm
->ReAuthenticationRequest
= TRUE
;
633 return wpa_sm_step(sm
);
636 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
637 "start authentication");
641 if (wpa_sm_step(sm
) == 1)
642 return 1; /* should not really happen */
644 sm
->AuthenticationRequest
= TRUE
;
645 return wpa_sm_step(sm
);
649 void wpa_auth_sta_no_wpa(struct wpa_state_machine
*sm
)
651 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
652 * reassociates back to the same AP while the previous entry for the
653 * STA has not yet been removed. */
657 sm
->wpa_key_mgmt
= 0;
661 static void wpa_free_sta_sm(struct wpa_state_machine
*sm
)
664 if (WPA_GET_BE32(sm
->ip_addr
)) {
666 wpa_printf(MSG_DEBUG
, "P2P: Free assigned IP "
667 "address %u.%u.%u.%u from " MACSTR
,
668 sm
->ip_addr
[0], sm
->ip_addr
[1],
669 sm
->ip_addr
[2], sm
->ip_addr
[3],
671 start
= WPA_GET_BE32(sm
->wpa_auth
->conf
.ip_addr_start
);
672 bitfield_clear(sm
->wpa_auth
->ip_pool
,
673 WPA_GET_BE32(sm
->ip_addr
) - start
);
675 #endif /* CONFIG_P2P */
676 if (sm
->GUpdateStationKeys
) {
677 sm
->group
->GKeyDoneStations
--;
678 sm
->GUpdateStationKeys
= FALSE
;
680 #ifdef CONFIG_IEEE80211R_AP
681 os_free(sm
->assoc_resp_ftie
);
682 wpabuf_free(sm
->ft_pending_req_ies
);
683 #endif /* CONFIG_IEEE80211R_AP */
684 os_free(sm
->last_rx_eapol_key
);
686 wpa_group_put(sm
->wpa_auth
, sm
->group
);
691 void wpa_auth_sta_deinit(struct wpa_state_machine
*sm
)
696 if (sm
->wpa_auth
->conf
.wpa_strict_rekey
&& sm
->has_GTK
) {
697 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
698 "strict rekeying - force GTK rekey since STA "
700 eloop_cancel_timeout(wpa_rekey_gtk
, sm
->wpa_auth
, NULL
);
701 eloop_register_timeout(0, 500000, wpa_rekey_gtk
, sm
->wpa_auth
,
705 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
706 sm
->pending_1_of_4_timeout
= 0;
707 eloop_cancel_timeout(wpa_sm_call_step
, sm
, NULL
);
708 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
709 #ifdef CONFIG_IEEE80211R_AP
710 wpa_ft_sta_deinit(sm
);
711 #endif /* CONFIG_IEEE80211R_AP */
712 if (sm
->in_step_loop
) {
713 /* Must not free state machine while wpa_sm_step() is running.
714 * Freeing will be completed in the end of wpa_sm_step(). */
715 wpa_printf(MSG_DEBUG
, "WPA: Registering pending STA state "
716 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
717 sm
->pending_deinit
= 1;
723 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
)
728 sm
->PTKRequest
= TRUE
;
733 static int wpa_replay_counter_valid(struct wpa_key_replay_counter
*ctr
,
734 const u8
*replay_counter
)
737 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
740 if (os_memcmp(replay_counter
, ctr
[i
].counter
,
741 WPA_REPLAY_COUNTER_LEN
) == 0)
748 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter
*ctr
,
749 const u8
*replay_counter
)
752 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
754 (replay_counter
== NULL
||
755 os_memcmp(replay_counter
, ctr
[i
].counter
,
756 WPA_REPLAY_COUNTER_LEN
) == 0))
757 ctr
[i
].valid
= FALSE
;
762 #ifdef CONFIG_IEEE80211R_AP
763 static int ft_check_msg_2_of_4(struct wpa_authenticator
*wpa_auth
,
764 struct wpa_state_machine
*sm
,
765 struct wpa_eapol_ie_parse
*kde
)
767 struct wpa_ie_data ie
;
768 struct rsn_mdie
*mdie
;
770 if (wpa_parse_wpa_ie_rsn(kde
->rsn_ie
, kde
->rsn_ie_len
, &ie
) < 0 ||
771 ie
.num_pmkid
!= 1 || ie
.pmkid
== NULL
) {
772 wpa_printf(MSG_DEBUG
, "FT: No PMKR1Name in "
773 "FT 4-way handshake message 2/4");
777 os_memcpy(sm
->sup_pmk_r1_name
, ie
.pmkid
, PMKID_LEN
);
778 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Supplicant",
779 sm
->sup_pmk_r1_name
, PMKID_LEN
);
781 if (!kde
->mdie
|| !kde
->ftie
) {
782 wpa_printf(MSG_DEBUG
, "FT: No %s in FT 4-way handshake "
783 "message 2/4", kde
->mdie
? "FTIE" : "MDIE");
787 mdie
= (struct rsn_mdie
*) (kde
->mdie
+ 2);
788 if (kde
->mdie
[1] < sizeof(struct rsn_mdie
) ||
789 os_memcmp(wpa_auth
->conf
.mobility_domain
, mdie
->mobility_domain
,
790 MOBILITY_DOMAIN_ID_LEN
) != 0) {
791 wpa_printf(MSG_DEBUG
, "FT: MDIE mismatch");
795 if (sm
->assoc_resp_ftie
&&
796 (kde
->ftie
[1] != sm
->assoc_resp_ftie
[1] ||
797 os_memcmp(kde
->ftie
, sm
->assoc_resp_ftie
,
798 2 + sm
->assoc_resp_ftie
[1]) != 0)) {
799 wpa_printf(MSG_DEBUG
, "FT: FTIE mismatch");
800 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 2/4",
801 kde
->ftie
, kde
->ftie_len
);
802 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)AssocResp",
803 sm
->assoc_resp_ftie
, 2 + sm
->assoc_resp_ftie
[1]);
809 #endif /* CONFIG_IEEE80211R_AP */
812 static int wpa_receive_error_report(struct wpa_authenticator
*wpa_auth
,
813 struct wpa_state_machine
*sm
, int group
)
815 /* Supplicant reported a Michael MIC error */
816 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
817 "received EAPOL-Key Error Request "
818 "(STA detected Michael MIC failure (group=%d))",
821 if (group
&& wpa_auth
->conf
.wpa_group
!= WPA_CIPHER_TKIP
) {
822 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
823 "ignore Michael MIC failure report since "
824 "group cipher is not TKIP");
825 } else if (!group
&& sm
->pairwise
!= WPA_CIPHER_TKIP
) {
826 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
827 "ignore Michael MIC failure report since "
828 "pairwise cipher is not TKIP");
830 if (wpa_auth_mic_failure_report(wpa_auth
, sm
->addr
) > 0)
831 return 1; /* STA entry was removed */
832 sm
->dot11RSNAStatsTKIPRemoteMICFailures
++;
833 wpa_auth
->dot11RSNAStatsTKIPRemoteMICFailures
++;
837 * Error report is not a request for a new key handshake, but since
838 * Authenticator may do it, let's change the keys now anyway.
840 wpa_request_new_ptk(sm
);
845 static int wpa_try_alt_snonce(struct wpa_state_machine
*sm
, u8
*data
,
850 const u8
*pmk
= NULL
;
853 os_memset(&PTK
, 0, sizeof(PTK
));
855 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
856 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
857 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
858 sm
->p2p_dev_addr
, pmk
, &pmk_len
);
863 pmk_len
= sm
->pmk_len
;
866 if (wpa_derive_ptk(sm
, sm
->alt_SNonce
, pmk
, pmk_len
, &PTK
) < 0)
869 if (wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
870 data
, data_len
) == 0) {
875 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
))
880 wpa_printf(MSG_DEBUG
,
881 "WPA: Earlier SNonce did not result in matching MIC");
885 wpa_printf(MSG_DEBUG
,
886 "WPA: Earlier SNonce resulted in matching MIC");
887 sm
->alt_snonce_valid
= 0;
888 os_memcpy(sm
->SNonce
, sm
->alt_SNonce
, WPA_NONCE_LEN
);
889 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
890 sm
->PTK_valid
= TRUE
;
896 void wpa_receive(struct wpa_authenticator
*wpa_auth
,
897 struct wpa_state_machine
*sm
,
898 u8
*data
, size_t data_len
)
900 struct ieee802_1x_hdr
*hdr
;
901 struct wpa_eapol_key
*key
;
902 u16 key_info
, key_data_length
;
903 enum { PAIRWISE_2
, PAIRWISE_4
, GROUP_2
, REQUEST
,
904 SMK_M1
, SMK_M3
, SMK_ERROR
} msg
;
906 struct wpa_eapol_ie_parse kde
;
908 size_t keyhdrlen
, mic_len
;
911 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
913 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL data", data
, data_len
);
915 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
916 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
918 if (data_len
< sizeof(*hdr
) + keyhdrlen
) {
919 wpa_printf(MSG_DEBUG
, "WPA: Ignore too short EAPOL-Key frame");
923 hdr
= (struct ieee802_1x_hdr
*) data
;
924 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
925 mic
= (u8
*) (key
+ 1);
926 key_info
= WPA_GET_BE16(key
->key_info
);
927 key_data
= mic
+ mic_len
+ 2;
928 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
929 wpa_printf(MSG_DEBUG
, "WPA: Received EAPOL-Key from " MACSTR
930 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
931 MAC2STR(sm
->addr
), key_info
, key
->type
,
932 (unsigned int) mic_len
, key_data_length
);
933 wpa_hexdump(MSG_MSGDUMP
,
934 "WPA: EAPOL-Key header (ending before Key MIC)",
936 wpa_hexdump(MSG_MSGDUMP
, "WPA: EAPOL-Key Key MIC",
938 if (key_data_length
> data_len
- sizeof(*hdr
) - keyhdrlen
) {
939 wpa_printf(MSG_INFO
, "WPA: Invalid EAPOL-Key frame - "
940 "key_data overflow (%d > %lu)",
942 (unsigned long) (data_len
- sizeof(*hdr
) -
947 if (sm
->wpa
== WPA_VERSION_WPA2
) {
948 if (key
->type
== EAPOL_KEY_TYPE_WPA
) {
950 * Some deployed station implementations seem to send
951 * msg 4/4 with incorrect type value in WPA2 mode.
953 wpa_printf(MSG_DEBUG
, "Workaround: Allow EAPOL-Key "
954 "with unexpected WPA type in RSN mode");
955 } else if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
956 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
957 "unexpected type %d in RSN mode",
962 if (key
->type
!= EAPOL_KEY_TYPE_WPA
) {
963 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
964 "unexpected type %d in WPA mode",
970 wpa_hexdump(MSG_DEBUG
, "WPA: Received Key Nonce", key
->key_nonce
,
972 wpa_hexdump(MSG_DEBUG
, "WPA: Received Replay Counter",
973 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
975 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
978 if ((key_info
& (WPA_KEY_INFO_SMK_MESSAGE
| WPA_KEY_INFO_REQUEST
)) ==
979 (WPA_KEY_INFO_SMK_MESSAGE
| WPA_KEY_INFO_REQUEST
)) {
980 if (key_info
& WPA_KEY_INFO_ERROR
) {
982 msgtxt
= "SMK Error";
987 } else if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
990 } else if (key_info
& WPA_KEY_INFO_REQUEST
) {
993 } else if (!(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
995 msgtxt
= "2/2 Group";
996 } else if (key_data_length
== 0 ||
997 (mic_len
== 0 && (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) &&
998 key_data_length
== AES_BLOCK_SIZE
)) {
1000 msgtxt
= "4/4 Pairwise";
1003 msgtxt
= "2/4 Pairwise";
1006 /* TODO: key_info type validation for PeerKey */
1007 if (msg
== REQUEST
|| msg
== PAIRWISE_2
|| msg
== PAIRWISE_4
||
1009 u16 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
1010 if (sm
->pairwise
== WPA_CIPHER_CCMP
||
1011 sm
->pairwise
== WPA_CIPHER_GCMP
) {
1012 if (wpa_use_aes_cmac(sm
) &&
1013 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
&&
1014 !wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) &&
1015 !wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1016 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1017 wpa_auth_logger(wpa_auth
, sm
->addr
,
1019 "advertised support for "
1020 "AES-128-CMAC, but did not "
1025 if (!wpa_use_aes_cmac(sm
) &&
1026 !wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) &&
1027 !wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1028 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OWE
&&
1029 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_DPP
&&
1030 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1031 wpa_auth_logger(wpa_auth
, sm
->addr
,
1033 "did not use HMAC-SHA1-AES "
1039 if ((wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) ||
1040 wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) ||
1041 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1042 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) &&
1043 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
1044 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1045 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1050 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1051 if (sm
->req_replay_counter_used
&&
1052 os_memcmp(key
->replay_counter
, sm
->req_replay_counter
,
1053 WPA_REPLAY_COUNTER_LEN
) <= 0) {
1054 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1055 "received EAPOL-Key request with "
1056 "replayed counter");
1061 if (!(key_info
& WPA_KEY_INFO_REQUEST
) &&
1062 !wpa_replay_counter_valid(sm
->key_replay
, key
->replay_counter
)) {
1065 if (msg
== PAIRWISE_2
&&
1066 wpa_replay_counter_valid(sm
->prev_key_replay
,
1067 key
->replay_counter
) &&
1068 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1069 os_memcmp(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0)
1072 * Some supplicant implementations (e.g., Windows XP
1073 * WZC) update SNonce for each EAPOL-Key 2/4. This
1074 * breaks the workaround on accepting any of the
1075 * pending requests, so allow the SNonce to be updated
1076 * even if we have already sent out EAPOL-Key 3/4.
1078 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1079 "Process SNonce update from STA "
1080 "based on retransmitted EAPOL-Key "
1082 sm
->update_snonce
= 1;
1083 os_memcpy(sm
->alt_SNonce
, sm
->SNonce
, WPA_NONCE_LEN
);
1084 sm
->alt_snonce_valid
= TRUE
;
1085 os_memcpy(sm
->alt_replay_counter
,
1086 sm
->key_replay
[0].counter
,
1087 WPA_REPLAY_COUNTER_LEN
);
1088 goto continue_processing
;
1091 if (msg
== PAIRWISE_4
&& sm
->alt_snonce_valid
&&
1092 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1093 os_memcmp(key
->replay_counter
, sm
->alt_replay_counter
,
1094 WPA_REPLAY_COUNTER_LEN
) == 0) {
1096 * Supplicant may still be using the old SNonce since
1097 * there was two EAPOL-Key 2/4 messages and they had
1098 * different SNonce values.
1100 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1101 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1102 goto continue_processing
;
1105 if (msg
== PAIRWISE_2
&&
1106 wpa_replay_counter_valid(sm
->prev_key_replay
,
1107 key
->replay_counter
) &&
1108 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
) {
1109 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1110 "ignore retransmitted EAPOL-Key %s - "
1111 "SNonce did not change", msgtxt
);
1113 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1114 "received EAPOL-Key %s with "
1115 "unexpected replay counter", msgtxt
);
1117 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
1118 if (!sm
->key_replay
[i
].valid
)
1120 wpa_hexdump(MSG_DEBUG
, "pending replay counter",
1121 sm
->key_replay
[i
].counter
,
1122 WPA_REPLAY_COUNTER_LEN
);
1124 wpa_hexdump(MSG_DEBUG
, "received replay counter",
1125 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1129 continue_processing
:
1131 if (sm
->wpa
== WPA_VERSION_WPA2
&& mic_len
== 0 &&
1132 !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1133 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1134 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1137 #endif /* CONFIG_FILS */
1141 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKSTART
&&
1142 sm
->wpa_ptk_state
!= WPA_PTK_PTKCALCNEGOTIATING
&&
1143 (!sm
->update_snonce
||
1144 sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
)) {
1145 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1146 "received EAPOL-Key msg 2/4 in "
1147 "invalid state (%d) - dropped",
1151 random_add_randomness(key
->key_nonce
, WPA_NONCE_LEN
);
1152 if (sm
->group
->reject_4way_hs_for_entropy
) {
1154 * The system did not have enough entropy to generate
1155 * strong random numbers. Reject the first 4-way
1156 * handshake(s) and collect some entropy based on the
1157 * information from it. Once enough entropy is
1158 * available, the next atempt will trigger GMK/Key
1159 * Counter update and the station will be allowed to
1162 wpa_printf(MSG_DEBUG
, "WPA: Reject 4-way handshake to "
1163 "collect more entropy for random number "
1165 random_mark_pool_ready();
1166 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
1167 WLAN_REASON_PREV_AUTH_NOT_VALID
);
1172 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
||
1174 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1175 "received EAPOL-Key msg 4/4 in "
1176 "invalid state (%d) - dropped",
1182 if (sm
->wpa_ptk_group_state
!= WPA_PTK_GROUP_REKEYNEGOTIATING
1183 || !sm
->PTK_valid
) {
1184 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1185 "received EAPOL-Key msg 2/2 in "
1186 "invalid state (%d) - dropped",
1187 sm
->wpa_ptk_group_state
);
1191 #ifdef CONFIG_PEERKEY
1195 if (!wpa_auth
->conf
.peerkey
) {
1196 wpa_printf(MSG_DEBUG
, "RSN: SMK M1/M3/Error, but "
1197 "PeerKey use disabled - ignoring message");
1200 if (!sm
->PTK_valid
) {
1201 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1202 "received EAPOL-Key msg SMK in "
1203 "invalid state - dropped");
1207 #else /* CONFIG_PEERKEY */
1211 return; /* STSL disabled - ignore SMK messages */
1212 #endif /* CONFIG_PEERKEY */
1217 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1218 "received EAPOL-Key frame (%s)", msgtxt
);
1220 if (key_info
& WPA_KEY_INFO_ACK
) {
1221 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1222 "received invalid EAPOL-Key: Key Ack set");
1226 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1227 !(key_info
& WPA_KEY_INFO_MIC
)) {
1228 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1229 "received invalid EAPOL-Key: Key MIC not set");
1234 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1235 (key_info
& WPA_KEY_INFO_MIC
)) {
1236 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1237 "received invalid EAPOL-Key: Key MIC set");
1240 #endif /* CONFIG_FILS */
1242 sm
->MICVerified
= FALSE
;
1243 if (sm
->PTK_valid
&& !sm
->update_snonce
) {
1245 wpa_verify_key_mic(sm
->wpa_key_mgmt
, sm
->pmk_len
, &sm
->PTK
,
1247 (msg
!= PAIRWISE_4
|| !sm
->alt_snonce_valid
||
1248 wpa_try_alt_snonce(sm
, data
, data_len
))) {
1249 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1250 "received EAPOL-Key with invalid MIC");
1255 wpa_aead_decrypt(sm
, &sm
->PTK
, data
, data_len
,
1256 &key_data_length
) < 0) {
1257 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1258 "received EAPOL-Key with invalid MIC");
1261 #endif /* CONFIG_FILS */
1262 sm
->MICVerified
= TRUE
;
1263 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
1264 sm
->pending_1_of_4_timeout
= 0;
1267 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1268 if (sm
->MICVerified
) {
1269 sm
->req_replay_counter_used
= 1;
1270 os_memcpy(sm
->req_replay_counter
, key
->replay_counter
,
1271 WPA_REPLAY_COUNTER_LEN
);
1273 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1274 "received EAPOL-Key request with "
1280 * TODO: should decrypt key data field if encryption was used;
1281 * even though MAC address KDE is not normally encrypted,
1282 * supplicant is allowed to encrypt it.
1284 if (msg
== SMK_ERROR
) {
1285 #ifdef CONFIG_PEERKEY
1286 wpa_smk_error(wpa_auth
, sm
, key_data
, key_data_length
);
1287 #endif /* CONFIG_PEERKEY */
1289 } else if (key_info
& WPA_KEY_INFO_ERROR
) {
1290 if (wpa_receive_error_report(
1292 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) > 0)
1293 return; /* STA entry was removed */
1294 } else if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
1295 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1296 "received EAPOL-Key Request for new "
1298 wpa_request_new_ptk(sm
);
1299 #ifdef CONFIG_PEERKEY
1300 } else if (msg
== SMK_M1
) {
1301 wpa_smk_m1(wpa_auth
, sm
, key
, key_data
,
1303 #endif /* CONFIG_PEERKEY */
1304 } else if (key_data_length
> 0 &&
1305 wpa_parse_kde_ies(key_data
, key_data_length
,
1309 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1310 "received EAPOL-Key Request for GTK "
1312 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
1313 wpa_rekey_gtk(wpa_auth
, NULL
);
1316 /* Do not allow the same key replay counter to be reused. */
1317 wpa_replay_counter_mark_invalid(sm
->key_replay
,
1318 key
->replay_counter
);
1320 if (msg
== PAIRWISE_2
) {
1322 * Maintain a copy of the pending EAPOL-Key frames in
1323 * case the EAPOL-Key frame was retransmitted. This is
1324 * needed to allow EAPOL-Key msg 2/4 reply to another
1325 * pending msg 1/4 to update the SNonce to work around
1326 * unexpected supplicant behavior.
1328 os_memcpy(sm
->prev_key_replay
, sm
->key_replay
,
1329 sizeof(sm
->key_replay
));
1331 os_memset(sm
->prev_key_replay
, 0,
1332 sizeof(sm
->prev_key_replay
));
1336 * Make sure old valid counters are not accepted anymore and
1337 * do not get copied again.
1339 wpa_replay_counter_mark_invalid(sm
->key_replay
, NULL
);
1342 #ifdef CONFIG_PEERKEY
1343 if (msg
== SMK_M3
) {
1344 wpa_smk_m3(wpa_auth
, sm
, key
, key_data
, key_data_length
);
1347 #endif /* CONFIG_PEERKEY */
1349 os_free(sm
->last_rx_eapol_key
);
1350 sm
->last_rx_eapol_key
= os_memdup(data
, data_len
);
1351 if (sm
->last_rx_eapol_key
== NULL
)
1353 sm
->last_rx_eapol_key_len
= data_len
;
1355 sm
->rx_eapol_key_secure
= !!(key_info
& WPA_KEY_INFO_SECURE
);
1356 sm
->EAPOLKeyReceived
= TRUE
;
1357 sm
->EAPOLKeyPairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1358 sm
->EAPOLKeyRequest
= !!(key_info
& WPA_KEY_INFO_REQUEST
);
1359 os_memcpy(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
);
1364 static int wpa_gmk_to_gtk(const u8
*gmk
, const char *label
, const u8
*addr
,
1365 const u8
*gnonce
, u8
*gtk
, size_t gtk_len
)
1367 u8 data
[ETH_ALEN
+ WPA_NONCE_LEN
+ 8 + 16];
1371 /* GTK = PRF-X(GMK, "Group key expansion",
1372 * AA || GNonce || Time || random data)
1373 * The example described in the IEEE 802.11 standard uses only AA and
1374 * GNonce as inputs here. Add some more entropy since this derivation
1375 * is done only at the Authenticator and as such, does not need to be
1378 os_memcpy(data
, addr
, ETH_ALEN
);
1379 os_memcpy(data
+ ETH_ALEN
, gnonce
, WPA_NONCE_LEN
);
1380 pos
= data
+ ETH_ALEN
+ WPA_NONCE_LEN
;
1381 wpa_get_ntp_timestamp(pos
);
1383 if (random_get_bytes(pos
, 16) < 0)
1386 #ifdef CONFIG_IEEE80211W
1387 sha256_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
), gtk
, gtk_len
);
1388 #else /* CONFIG_IEEE80211W */
1389 if (sha1_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
), gtk
, gtk_len
)
1392 #endif /* CONFIG_IEEE80211W */
1398 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
)
1400 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1401 struct wpa_state_machine
*sm
= timeout_ctx
;
1403 sm
->pending_1_of_4_timeout
= 0;
1404 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "EAPOL-Key timeout");
1405 sm
->TimeoutEvt
= TRUE
;
1410 void __wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1411 struct wpa_state_machine
*sm
, int key_info
,
1412 const u8
*key_rsc
, const u8
*nonce
,
1413 const u8
*kde
, size_t kde_len
,
1414 int keyidx
, int encr
, int force_version
)
1416 struct ieee802_1x_hdr
*hdr
;
1417 struct wpa_eapol_key
*key
;
1418 size_t len
, mic_len
, keyhdrlen
;
1420 int key_data_len
, pad_len
= 0;
1422 int version
, pairwise
;
1424 u8
*key_mic
, *key_data
;
1426 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
1427 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
1429 len
= sizeof(struct ieee802_1x_hdr
) + keyhdrlen
;
1432 version
= force_version
;
1433 else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
||
1434 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
||
1435 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1436 wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) ||
1437 wpa_key_mgmt_fils(sm
->wpa_key_mgmt
))
1438 version
= WPA_KEY_INFO_TYPE_AKM_DEFINED
;
1439 else if (wpa_use_aes_cmac(sm
))
1440 version
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
1441 else if (sm
->pairwise
!= WPA_CIPHER_TKIP
)
1442 version
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
1444 version
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
1446 pairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1448 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1449 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1452 (key_info
& WPA_KEY_INFO_SECURE
) ? 1 : 0,
1453 (key_info
& WPA_KEY_INFO_MIC
) ? 1 : 0,
1454 (key_info
& WPA_KEY_INFO_ACK
) ? 1 : 0,
1455 (key_info
& WPA_KEY_INFO_INSTALL
) ? 1 : 0,
1456 pairwise
, (unsigned long) kde_len
, keyidx
, encr
);
1458 key_data_len
= kde_len
;
1460 if ((version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1461 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
||
1462 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1463 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
||
1464 wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) ||
1465 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) && encr
) {
1466 pad_len
= key_data_len
% 8;
1468 pad_len
= 8 - pad_len
;
1469 key_data_len
+= pad_len
+ 8;
1472 len
+= key_data_len
;
1473 if (!mic_len
&& encr
)
1474 len
+= AES_BLOCK_SIZE
;
1476 hdr
= os_zalloc(len
);
1479 hdr
->version
= wpa_auth
->conf
.eapol_version
;
1480 hdr
->type
= IEEE802_1X_TYPE_EAPOL_KEY
;
1481 hdr
->length
= host_to_be16(len
- sizeof(*hdr
));
1482 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1483 key_mic
= (u8
*) (key
+ 1);
1484 key_data
= ((u8
*) (hdr
+ 1)) + keyhdrlen
;
1486 key
->type
= sm
->wpa
== WPA_VERSION_WPA2
?
1487 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1488 key_info
|= version
;
1489 if (encr
&& sm
->wpa
== WPA_VERSION_WPA2
)
1490 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1491 if (sm
->wpa
!= WPA_VERSION_WPA2
)
1492 key_info
|= keyidx
<< WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1493 WPA_PUT_BE16(key
->key_info
, key_info
);
1495 alg
= pairwise
? sm
->pairwise
: wpa_auth
->conf
.wpa_group
;
1496 if ((key_info
& WPA_KEY_INFO_SMK_MESSAGE
) ||
1497 (sm
->wpa
== WPA_VERSION_WPA2
&& !pairwise
))
1498 WPA_PUT_BE16(key
->key_length
, 0);
1500 WPA_PUT_BE16(key
->key_length
, wpa_cipher_key_len(alg
));
1502 /* FIX: STSL: what to use as key_replay_counter? */
1503 for (i
= RSNA_MAX_EAPOL_RETRIES
- 1; i
> 0; i
--) {
1504 sm
->key_replay
[i
].valid
= sm
->key_replay
[i
- 1].valid
;
1505 os_memcpy(sm
->key_replay
[i
].counter
,
1506 sm
->key_replay
[i
- 1].counter
,
1507 WPA_REPLAY_COUNTER_LEN
);
1509 inc_byte_array(sm
->key_replay
[0].counter
, WPA_REPLAY_COUNTER_LEN
);
1510 os_memcpy(key
->replay_counter
, sm
->key_replay
[0].counter
,
1511 WPA_REPLAY_COUNTER_LEN
);
1512 wpa_hexdump(MSG_DEBUG
, "WPA: Replay Counter",
1513 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1514 sm
->key_replay
[0].valid
= TRUE
;
1517 os_memcpy(key
->key_nonce
, nonce
, WPA_NONCE_LEN
);
1520 os_memcpy(key
->key_rsc
, key_rsc
, WPA_KEY_RSC_LEN
);
1523 os_memcpy(key_data
, kde
, kde_len
);
1524 WPA_PUT_BE16(key_mic
+ mic_len
, kde_len
);
1526 } else if (!mic_len
) {
1530 WPA_PUT_BE16(key_mic
, AES_BLOCK_SIZE
+ kde_len
);
1531 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1534 wpa_hexdump_key(MSG_DEBUG
, "WPA: KEK",
1535 sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1536 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1537 * to Key Data (exclusive). */
1538 aad
[0] = (u8
*) hdr
;
1539 aad_len
[0] = key_mic
+ 2 - (u8
*) hdr
;
1540 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, kde
, kde_len
,
1541 1, aad
, aad_len
, key_mic
+ 2) < 0) {
1542 wpa_printf(MSG_DEBUG
, "WPA: AES-SIV encryption failed");
1546 wpa_hexdump(MSG_DEBUG
, "WPA: Encrypted Key Data from SIV",
1547 key_mic
+ 2, AES_BLOCK_SIZE
+ kde_len
);
1548 #endif /* CONFIG_FILS */
1549 } else if (encr
&& kde
) {
1550 buf
= os_zalloc(key_data_len
);
1556 os_memcpy(pos
, kde
, kde_len
);
1562 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1564 if (version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1565 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
||
1566 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1567 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
||
1568 wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) ||
1569 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1570 wpa_printf(MSG_DEBUG
,
1571 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1572 (unsigned int) sm
->PTK
.kek_len
);
1573 if (aes_wrap(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
1574 (key_data_len
- 8) / 8, buf
, key_data
)) {
1579 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1580 #ifndef CONFIG_NO_RC4
1581 } else if (sm
->PTK
.kek_len
== 16) {
1584 wpa_printf(MSG_DEBUG
,
1585 "WPA: Encrypt Key Data using RC4");
1586 os_memcpy(key
->key_iv
,
1587 sm
->group
->Counter
+ WPA_NONCE_LEN
- 16, 16);
1588 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1589 os_memcpy(ek
, key
->key_iv
, 16);
1590 os_memcpy(ek
+ 16, sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1591 os_memcpy(key_data
, buf
, key_data_len
);
1592 rc4_skip(ek
, 32, 256, key_data
, key_data_len
);
1593 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1594 #endif /* CONFIG_NO_RC4 */
1603 if (key_info
& WPA_KEY_INFO_MIC
) {
1604 if (!sm
->PTK_valid
|| !mic_len
) {
1605 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1606 "PTK not valid when sending EAPOL-Key "
1612 wpa_eapol_key_mic(sm
->PTK
.kck
, sm
->PTK
.kck_len
,
1613 sm
->wpa_key_mgmt
, version
,
1614 (u8
*) hdr
, len
, key_mic
);
1615 #ifdef CONFIG_TESTING_OPTIONS
1617 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
> 0.0 &&
1619 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
) {
1620 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1621 "Corrupting group EAPOL-Key Key MIC");
1624 #endif /* CONFIG_TESTING_OPTIONS */
1627 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_inc_EapolFramesTx
,
1629 wpa_auth_send_eapol(wpa_auth
, sm
->addr
, (u8
*) hdr
, len
,
1635 static void wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1636 struct wpa_state_machine
*sm
, int key_info
,
1637 const u8
*key_rsc
, const u8
*nonce
,
1638 const u8
*kde
, size_t kde_len
,
1639 int keyidx
, int encr
)
1642 int pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
1648 __wpa_send_eapol(wpa_auth
, sm
, key_info
, key_rsc
, nonce
, kde
, kde_len
,
1651 ctr
= pairwise
? sm
->TimeoutCtr
: sm
->GTimeoutCtr
;
1652 if (ctr
== 1 && wpa_auth
->conf
.tx_status
)
1653 timeout_ms
= pairwise
? eapol_key_timeout_first
:
1654 eapol_key_timeout_first_group
;
1656 timeout_ms
= eapol_key_timeout_subseq
;
1657 if (pairwise
&& ctr
== 1 && !(key_info
& WPA_KEY_INFO_MIC
))
1658 sm
->pending_1_of_4_timeout
= 1;
1659 wpa_printf(MSG_DEBUG
, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1660 "counter %u)", timeout_ms
, ctr
);
1661 eloop_register_timeout(timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000,
1662 wpa_send_eapol_timeout
, wpa_auth
, sm
);
1666 static int wpa_verify_key_mic(int akmp
, size_t pmk_len
, struct wpa_ptk
*PTK
,
1667 u8
*data
, size_t data_len
)
1669 struct ieee802_1x_hdr
*hdr
;
1670 struct wpa_eapol_key
*key
;
1673 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
], *mic_pos
;
1674 size_t mic_len
= wpa_mic_len(akmp
, pmk_len
);
1676 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
1679 hdr
= (struct ieee802_1x_hdr
*) data
;
1680 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1681 mic_pos
= (u8
*) (key
+ 1);
1682 key_info
= WPA_GET_BE16(key
->key_info
);
1683 os_memcpy(mic
, mic_pos
, mic_len
);
1684 os_memset(mic_pos
, 0, mic_len
);
1685 if (wpa_eapol_key_mic(PTK
->kck
, PTK
->kck_len
, akmp
,
1686 key_info
& WPA_KEY_INFO_TYPE_MASK
,
1687 data
, data_len
, mic_pos
) ||
1688 os_memcmp_const(mic
, mic_pos
, mic_len
) != 0)
1690 os_memcpy(mic_pos
, mic
, mic_len
);
1695 void wpa_remove_ptk(struct wpa_state_machine
*sm
)
1697 sm
->PTK_valid
= FALSE
;
1698 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1699 if (wpa_auth_set_key(sm
->wpa_auth
, 0, WPA_ALG_NONE
, sm
->addr
, 0, NULL
,
1701 wpa_printf(MSG_DEBUG
,
1702 "RSN: PTK removal from the driver failed");
1703 sm
->pairwise_set
= FALSE
;
1704 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1708 int wpa_auth_sm_event(struct wpa_state_machine
*sm
, enum wpa_event event
)
1715 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1716 "event %d notification", event
);
1721 /* PTKs are derived through AMPE */
1722 if (wpa_auth_start_ampe(sm
->wpa_auth
, sm
->addr
)) {
1727 #endif /* CONFIG_MESH */
1732 sm
->DeauthenticationRequest
= TRUE
;
1735 case WPA_REAUTH_EAPOL
:
1738 * When using WPS, we may end up here if the STA
1739 * manages to re-associate without the previous STA
1740 * entry getting removed. Consequently, we need to make
1741 * sure that the WPA state machines gets initialized
1742 * properly at this point.
1744 wpa_printf(MSG_DEBUG
, "WPA state machine had not been "
1745 "started - initialize now");
1748 if (wpa_sm_step(sm
) == 1)
1749 return 1; /* should not really happen */
1751 sm
->AuthenticationRequest
= TRUE
;
1754 if (sm
->GUpdateStationKeys
) {
1756 * Reauthentication cancels the pending group key
1757 * update for this STA.
1759 sm
->group
->GKeyDoneStations
--;
1760 sm
->GUpdateStationKeys
= FALSE
;
1761 sm
->PtkGroupInit
= TRUE
;
1763 sm
->ReAuthenticationRequest
= TRUE
;
1766 #ifdef CONFIG_IEEE80211R_AP
1767 wpa_printf(MSG_DEBUG
, "FT: Retry PTK configuration "
1768 "after association");
1769 wpa_ft_install_ptk(sm
);
1771 /* Using FT protocol, not WPA auth state machine */
1772 sm
->ft_completed
= 1;
1774 #else /* CONFIG_IEEE80211R_AP */
1776 #endif /* CONFIG_IEEE80211R_AP */
1777 case WPA_ASSOC_FILS
:
1779 wpa_printf(MSG_DEBUG
,
1780 "FILS: TK configuration after association");
1782 sm
->fils_completed
= 1;
1784 #else /* CONFIG_FILS */
1786 #endif /* CONFIG_FILS */
1787 case WPA_DRV_STA_REMOVED
:
1788 sm
->tk_already_set
= FALSE
;
1792 #ifdef CONFIG_IEEE80211R_AP
1793 sm
->ft_completed
= 0;
1794 #endif /* CONFIG_IEEE80211R_AP */
1796 #ifdef CONFIG_IEEE80211W
1797 if (sm
->mgmt_frame_prot
&& event
== WPA_AUTH
)
1799 #endif /* CONFIG_IEEE80211W */
1801 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1802 (event
== WPA_AUTH
|| event
== WPA_ASSOC
))
1804 #endif /* CONFIG_FILS */
1807 sm
->PTK_valid
= FALSE
;
1808 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1810 if (event
!= WPA_REAUTH_EAPOL
)
1814 if (sm
->in_step_loop
) {
1816 * wpa_sm_step() is already running - avoid recursive call to
1817 * it by making the existing loop process the new update.
1822 return wpa_sm_step(sm
);
1826 SM_STATE(WPA_PTK
, INITIALIZE
)
1828 SM_ENTRY_MA(WPA_PTK
, INITIALIZE
, wpa_ptk
);
1830 /* Init flag is not cleared here, so avoid busy
1831 * loop by claiming nothing changed. */
1832 sm
->changed
= FALSE
;
1836 if (sm
->GUpdateStationKeys
)
1837 sm
->group
->GKeyDoneStations
--;
1838 sm
->GUpdateStationKeys
= FALSE
;
1839 if (sm
->wpa
== WPA_VERSION_WPA
)
1840 sm
->PInitAKeys
= FALSE
;
1841 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1842 * Local AA > Remote AA)) */) {
1845 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 0);
1847 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
, 0);
1849 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
1850 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1851 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
1852 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1853 WPA_EAPOL_authorized
, 0);
1858 SM_STATE(WPA_PTK
, DISCONNECT
)
1860 u16 reason
= sm
->disconnect_reason
;
1862 SM_ENTRY_MA(WPA_PTK
, DISCONNECT
, wpa_ptk
);
1863 sm
->Disconnect
= FALSE
;
1864 sm
->disconnect_reason
= 0;
1866 reason
= WLAN_REASON_PREV_AUTH_NOT_VALID
;
1867 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
, reason
);
1871 SM_STATE(WPA_PTK
, DISCONNECTED
)
1873 SM_ENTRY_MA(WPA_PTK
, DISCONNECTED
, wpa_ptk
);
1874 sm
->DeauthenticationRequest
= FALSE
;
1878 SM_STATE(WPA_PTK
, AUTHENTICATION
)
1880 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION
, wpa_ptk
);
1881 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1882 sm
->PTK_valid
= FALSE
;
1883 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portControl_Auto
,
1885 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 1);
1886 sm
->AuthenticationRequest
= FALSE
;
1890 static void wpa_group_ensure_init(struct wpa_authenticator
*wpa_auth
,
1891 struct wpa_group
*group
)
1893 if (group
->first_sta_seen
)
1896 * System has run bit further than at the time hostapd was started
1897 * potentially very early during boot up. This provides better chances
1898 * of collecting more randomness on embedded systems. Re-initialize the
1899 * GMK and Counter here to improve their strength if there was not
1900 * enough entropy available immediately after system startup.
1902 wpa_printf(MSG_DEBUG
, "WPA: Re-initialize GMK/Counter on first "
1904 if (random_pool_ready() != 1) {
1905 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
1906 "to proceed - reject first 4-way handshake");
1907 group
->reject_4way_hs_for_entropy
= TRUE
;
1909 group
->first_sta_seen
= TRUE
;
1910 group
->reject_4way_hs_for_entropy
= FALSE
;
1913 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0 ||
1914 wpa_gtk_update(wpa_auth
, group
) < 0 ||
1915 wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
1916 wpa_printf(MSG_INFO
, "WPA: GMK/GTK setup failed");
1917 group
->first_sta_seen
= FALSE
;
1918 group
->reject_4way_hs_for_entropy
= TRUE
;
1923 SM_STATE(WPA_PTK
, AUTHENTICATION2
)
1925 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION2
, wpa_ptk
);
1927 wpa_group_ensure_init(sm
->wpa_auth
, sm
->group
);
1928 sm
->ReAuthenticationRequest
= FALSE
;
1931 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1932 * ambiguous. The Authenticator state machine uses a counter that is
1933 * incremented by one for each 4-way handshake. However, the security
1934 * analysis of 4-way handshake points out that unpredictable nonces
1935 * help in preventing precomputation attacks. Instead of the state
1936 * machine definition, use an unpredictable nonce value here to provide
1937 * stronger protection against potential precomputation attacks.
1939 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
1940 wpa_printf(MSG_ERROR
, "WPA: Failed to get random data for "
1942 sm
->Disconnect
= TRUE
;
1945 wpa_hexdump(MSG_DEBUG
, "WPA: Assign ANonce", sm
->ANonce
,
1947 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1948 * logical place than INITIALIZE since AUTHENTICATION2 can be
1949 * re-entered on ReAuthenticationRequest without going through
1955 SM_STATE(WPA_PTK
, INITPMK
)
1957 u8 msk
[2 * PMK_LEN
];
1958 size_t len
= 2 * PMK_LEN
;
1960 SM_ENTRY_MA(WPA_PTK
, INITPMK
, wpa_ptk
);
1961 #ifdef CONFIG_IEEE80211R_AP
1963 #endif /* CONFIG_IEEE80211R_AP */
1965 wpa_printf(MSG_DEBUG
, "WPA: PMK from PMKSA cache");
1966 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
1967 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
1969 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
) {
1970 wpa_printf(MSG_DEBUG
,
1971 "DPP: No PMKSA cache entry for STA - reject connection");
1972 sm
->Disconnect
= TRUE
;
1973 sm
->disconnect_reason
= WLAN_REASON_INVALID_PMKID
;
1975 #endif /* CONFIG_DPP */
1976 } else if (wpa_auth_get_msk(sm
->wpa_auth
, sm
->addr
, msk
, &len
) == 0) {
1977 unsigned int pmk_len
;
1979 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
))
1980 pmk_len
= PMK_LEN_SUITE_B_192
;
1983 wpa_printf(MSG_DEBUG
, "WPA: PMK from EAPOL state machine "
1984 "(MSK len=%lu PMK len=%u)", (unsigned long) len
,
1986 if (len
< pmk_len
) {
1987 wpa_printf(MSG_DEBUG
,
1988 "WPA: MSK not long enough (%u) to create PMK (%u)",
1989 (unsigned int) len
, (unsigned int) pmk_len
);
1990 sm
->Disconnect
= TRUE
;
1993 os_memcpy(sm
->PMK
, msk
, pmk_len
);
1994 sm
->pmk_len
= pmk_len
;
1995 #ifdef CONFIG_IEEE80211R_AP
1996 if (len
>= 2 * PMK_LEN
) {
1997 os_memcpy(sm
->xxkey
, msk
+ PMK_LEN
, PMK_LEN
);
1998 sm
->xxkey_len
= PMK_LEN
;
2000 #endif /* CONFIG_IEEE80211R_AP */
2002 wpa_printf(MSG_DEBUG
, "WPA: Could not get PMK, get_msk: %p",
2003 sm
->wpa_auth
->cb
->get_msk
);
2004 sm
->Disconnect
= TRUE
;
2007 os_memset(msk
, 0, sizeof(msk
));
2009 sm
->req_replay_counter_used
= 0;
2010 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
2011 * will break reauthentication since EAPOL state machines may not be
2012 * get into AUTHENTICATING state that clears keyRun before WPA state
2013 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2014 * state and takes PMK from the previously used AAA Key. This will
2015 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2016 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2017 * be good workaround for this issue. */
2018 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyRun
, 0);
2022 SM_STATE(WPA_PTK
, INITPSK
)
2027 SM_ENTRY_MA(WPA_PTK
, INITPSK
, wpa_ptk
);
2028 psk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
, NULL
,
2031 os_memcpy(sm
->PMK
, psk
, psk_len
);
2032 sm
->pmk_len
= psk_len
;
2033 #ifdef CONFIG_IEEE80211R_AP
2034 os_memcpy(sm
->xxkey
, psk
, PMK_LEN
);
2035 sm
->xxkey_len
= PMK_LEN
;
2036 #endif /* CONFIG_IEEE80211R_AP */
2039 if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
2040 wpa_printf(MSG_DEBUG
, "SAE: PMK from PMKSA cache");
2041 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2042 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
2044 #endif /* CONFIG_SAE */
2045 sm
->req_replay_counter_used
= 0;
2049 SM_STATE(WPA_PTK
, PTKSTART
)
2051 u8 buf
[2 + RSN_SELECTOR_LEN
+ PMKID_LEN
], *pmkid
= NULL
;
2052 size_t pmkid_len
= 0;
2054 SM_ENTRY_MA(WPA_PTK
, PTKSTART
, wpa_ptk
);
2055 sm
->PTKRequest
= FALSE
;
2056 sm
->TimeoutEvt
= FALSE
;
2057 sm
->alt_snonce_valid
= FALSE
;
2060 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
2061 /* No point in sending the EAPOL-Key - we will disconnect
2062 * immediately following this. */
2066 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2067 "sending 1/4 msg of 4-Way Handshake");
2069 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
2070 * one possible PSK for this STA.
2072 if (sm
->wpa
== WPA_VERSION_WPA2
&&
2073 (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) ||
2074 (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
&& sm
->pmksa
) ||
2075 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) &&
2076 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
) {
2078 pmkid_len
= 2 + RSN_SELECTOR_LEN
+ PMKID_LEN
;
2079 pmkid
[0] = WLAN_EID_VENDOR_SPECIFIC
;
2080 pmkid
[1] = RSN_SELECTOR_LEN
+ PMKID_LEN
;
2081 RSN_SELECTOR_PUT(&pmkid
[2], RSN_KEY_DATA_PMKID
);
2083 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2084 sm
->pmksa
->pmkid
, PMKID_LEN
);
2085 } else if (wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
)) {
2086 /* No KCK available to derive PMKID */
2090 * Calculate PMKID since no PMKSA cache entry was
2091 * available with pre-calculated PMKID.
2093 rsn_pmkid(sm
->PMK
, sm
->pmk_len
, sm
->wpa_auth
->addr
,
2094 sm
->addr
, &pmkid
[2 + RSN_SELECTOR_LEN
],
2098 wpa_send_eapol(sm
->wpa_auth
, sm
,
2099 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
2100 sm
->ANonce
, pmkid
, pmkid_len
, 0, 0);
2104 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
2105 const u8
*pmk
, unsigned int pmk_len
,
2106 struct wpa_ptk
*ptk
)
2108 #ifdef CONFIG_IEEE80211R_AP
2109 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
2110 return wpa_auth_derive_ptk_ft(sm
, pmk
, ptk
);
2111 #endif /* CONFIG_IEEE80211R_AP */
2113 return wpa_pmk_to_ptk(pmk
, pmk_len
, "Pairwise key expansion",
2114 sm
->wpa_auth
->addr
, sm
->addr
, sm
->ANonce
, snonce
,
2115 ptk
, sm
->wpa_key_mgmt
, sm
->pairwise
);
2121 int fils_auth_pmk_to_ptk(struct wpa_state_machine
*sm
, const u8
*pmk
,
2122 size_t pmk_len
, const u8
*snonce
, const u8
*anonce
,
2123 const u8
*dhss
, size_t dhss_len
,
2124 struct wpabuf
*g_sta
, struct wpabuf
*g_ap
)
2126 u8 ick
[FILS_ICK_MAX_LEN
];
2129 u8 fils_ft
[FILS_FT_MAX_LEN
];
2130 size_t fils_ft_len
= 0;
2132 res
= fils_pmk_to_ptk(pmk
, pmk_len
, sm
->addr
, sm
->wpa_auth
->addr
,
2133 snonce
, anonce
, dhss
, dhss_len
,
2134 &sm
->PTK
, ick
, &ick_len
,
2135 sm
->wpa_key_mgmt
, sm
->pairwise
,
2136 fils_ft
, &fils_ft_len
);
2139 sm
->PTK_valid
= TRUE
;
2141 #ifdef CONFIG_IEEE80211R_AP
2143 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2144 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
2145 u8 pmk_r0
[PMK_LEN
], pmk_r0_name
[WPA_PMK_NAME_LEN
];
2147 if (wpa_derive_pmk_r0(fils_ft
, fils_ft_len
,
2148 conf
->ssid
, conf
->ssid_len
,
2149 conf
->mobility_domain
,
2150 conf
->r0_key_holder
,
2151 conf
->r0_key_holder_len
,
2152 sm
->addr
, pmk_r0
, pmk_r0_name
) < 0)
2155 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: PMK-R0", pmk_r0
, PMK_LEN
);
2156 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR0Name",
2157 pmk_r0_name
, WPA_PMK_NAME_LEN
);
2158 wpa_ft_store_pmk_r0(wpa_auth
, sm
->addr
, pmk_r0
, pmk_r0_name
,
2160 os_memset(fils_ft
, 0, sizeof(fils_ft
));
2162 #endif /* CONFIG_IEEE80211R_AP */
2164 res
= fils_key_auth_sk(ick
, ick_len
, snonce
, anonce
,
2165 sm
->addr
, sm
->wpa_auth
->addr
,
2166 g_sta
? wpabuf_head(g_sta
) : NULL
,
2167 g_sta
? wpabuf_len(g_sta
) : 0,
2168 g_ap
? wpabuf_head(g_ap
) : NULL
,
2169 g_ap
? wpabuf_len(g_ap
) : 0,
2170 sm
->wpa_key_mgmt
, sm
->fils_key_auth_sta
,
2171 sm
->fils_key_auth_ap
,
2172 &sm
->fils_key_auth_len
);
2173 os_memset(ick
, 0, sizeof(ick
));
2175 /* Store nonces for (Re)Association Request/Response frame processing */
2176 os_memcpy(sm
->SNonce
, snonce
, FILS_NONCE_LEN
);
2177 os_memcpy(sm
->ANonce
, anonce
, FILS_NONCE_LEN
);
2183 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
2184 u8
*buf
, size_t buf_len
, u16
*_key_data_len
)
2186 struct ieee802_1x_hdr
*hdr
;
2187 struct wpa_eapol_key
*key
;
2194 hdr
= (struct ieee802_1x_hdr
*) buf
;
2195 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2196 pos
= (u8
*) (key
+ 1);
2197 key_data_len
= WPA_GET_BE16(pos
);
2198 if (key_data_len
< AES_BLOCK_SIZE
||
2199 key_data_len
> buf_len
- sizeof(*hdr
) - sizeof(*key
) - 2) {
2200 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2201 "No room for AES-SIV data in the frame");
2204 pos
+= 2; /* Pointing at the Encrypted Key Data field */
2206 tmp
= os_malloc(key_data_len
);
2210 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2211 * to Key Data (exclusive). */
2213 aad_len
[0] = pos
- buf
;
2214 if (aes_siv_decrypt(ptk
->kek
, ptk
->kek_len
, pos
, key_data_len
,
2215 1, aad
, aad_len
, tmp
) < 0) {
2216 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2217 "Invalid AES-SIV data in the frame");
2218 bin_clear_free(tmp
, key_data_len
);
2222 /* AEAD decryption and validation completed successfully */
2223 key_data_len
-= AES_BLOCK_SIZE
;
2224 wpa_hexdump_key(MSG_DEBUG
, "WPA: Decrypted Key Data",
2227 /* Replace Key Data field with the decrypted version */
2228 os_memcpy(pos
, tmp
, key_data_len
);
2229 pos
-= 2; /* Key Data Length field */
2230 WPA_PUT_BE16(pos
, key_data_len
);
2231 bin_clear_free(tmp
, key_data_len
);
2233 *_key_data_len
= key_data_len
;
2238 const u8
* wpa_fils_validate_fils_session(struct wpa_state_machine
*sm
,
2239 const u8
*ies
, size_t ies_len
,
2240 const u8
*fils_session
)
2243 const u8
*session
= NULL
;
2245 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2246 wpa_printf(MSG_DEBUG
,
2247 "FILS: Not a FILS AKM - reject association");
2251 /* Verify Session element */
2253 end
= ((const u8
*) ie
) + ies_len
;
2254 while (ie
+ 1 < end
) {
2255 if (ie
+ 2 + ie
[1] > end
)
2257 if (ie
[0] == WLAN_EID_EXTENSION
&&
2258 ie
[1] >= 1 + FILS_SESSION_LEN
&&
2259 ie
[2] == WLAN_EID_EXT_FILS_SESSION
) {
2267 wpa_printf(MSG_DEBUG
,
2268 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2273 if (!fils_session
) {
2274 wpa_printf(MSG_DEBUG
,
2275 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2280 if (os_memcmp(fils_session
, session
+ 3, FILS_SESSION_LEN
) != 0) {
2281 wpa_printf(MSG_DEBUG
, "FILS: Session mismatch");
2282 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
2283 fils_session
, FILS_SESSION_LEN
);
2284 wpa_hexdump(MSG_DEBUG
, "FILS: Received FILS Session",
2285 session
+ 3, FILS_SESSION_LEN
);
2292 int wpa_fils_validate_key_confirm(struct wpa_state_machine
*sm
, const u8
*ies
,
2295 struct ieee802_11_elems elems
;
2297 if (ieee802_11_parse_elems(ies
, ies_len
, &elems
, 1) == ParseFailed
) {
2298 wpa_printf(MSG_DEBUG
,
2299 "FILS: Failed to parse decrypted elements");
2303 if (!elems
.fils_session
) {
2304 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
2308 if (!elems
.fils_key_confirm
) {
2309 wpa_printf(MSG_DEBUG
, "FILS: No FILS Key Confirm element");
2313 if (elems
.fils_key_confirm_len
!= sm
->fils_key_auth_len
) {
2314 wpa_printf(MSG_DEBUG
,
2315 "FILS: Unexpected Key-Auth length %d (expected %d)",
2316 elems
.fils_key_confirm_len
,
2317 (int) sm
->fils_key_auth_len
);
2321 if (os_memcmp(elems
.fils_key_confirm
, sm
->fils_key_auth_sta
,
2322 sm
->fils_key_auth_len
) != 0) {
2323 wpa_printf(MSG_DEBUG
, "FILS: Key-Auth mismatch");
2324 wpa_hexdump(MSG_DEBUG
, "FILS: Received Key-Auth",
2325 elems
.fils_key_confirm
, elems
.fils_key_confirm_len
);
2326 wpa_hexdump(MSG_DEBUG
, "FILS: Expected Key-Auth",
2327 sm
->fils_key_auth_sta
, sm
->fils_key_auth_len
);
2335 int fils_decrypt_assoc(struct wpa_state_machine
*sm
, const u8
*fils_session
,
2336 const struct ieee80211_mgmt
*mgmt
, size_t frame_len
,
2337 u8
*pos
, size_t left
)
2340 const u8
*end
, *ie_start
, *ie
, *session
, *crypt
;
2344 if (!sm
|| !sm
->PTK_valid
) {
2345 wpa_printf(MSG_DEBUG
,
2346 "FILS: No KEK to decrypt Assocication Request frame");
2350 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2351 wpa_printf(MSG_DEBUG
,
2352 "FILS: Not a FILS AKM - reject association");
2356 end
= ((const u8
*) mgmt
) + frame_len
;
2357 fc
= le_to_host16(mgmt
->frame_control
);
2358 stype
= WLAN_FC_GET_STYPE(fc
);
2359 if (stype
== WLAN_FC_STYPE_REASSOC_REQ
)
2360 ie_start
= mgmt
->u
.reassoc_req
.variable
;
2362 ie_start
= mgmt
->u
.assoc_req
.variable
;
2366 * Find FILS Session element which is the last unencrypted element in
2369 session
= wpa_fils_validate_fils_session(sm
, ie
, end
- ie
,
2372 wpa_printf(MSG_DEBUG
, "FILS: Session validation failed");
2376 crypt
= session
+ 2 + session
[1];
2378 if (end
- crypt
< AES_BLOCK_SIZE
) {
2379 wpa_printf(MSG_DEBUG
,
2380 "FILS: Too short frame to include AES-SIV data");
2384 /* AES-SIV AAD vectors */
2386 /* The STA's MAC address */
2388 aad_len
[0] = ETH_ALEN
;
2389 /* The AP's BSSID */
2391 aad_len
[1] = ETH_ALEN
;
2392 /* The STA's nonce */
2393 aad
[2] = sm
->SNonce
;
2394 aad_len
[2] = FILS_NONCE_LEN
;
2395 /* The AP's nonce */
2396 aad
[3] = sm
->ANonce
;
2397 aad_len
[3] = FILS_NONCE_LEN
;
2399 * The (Re)Association Request frame from the Capability Information
2400 * field to the FILS Session element (both inclusive).
2402 aad
[4] = (const u8
*) &mgmt
->u
.assoc_req
.capab_info
;
2403 aad_len
[4] = crypt
- aad
[4];
2405 if (aes_siv_decrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, crypt
, end
- crypt
,
2406 5, aad
, aad_len
, pos
+ (crypt
- ie_start
)) < 0) {
2407 wpa_printf(MSG_DEBUG
,
2408 "FILS: Invalid AES-SIV data in the frame");
2411 wpa_hexdump(MSG_DEBUG
, "FILS: Decrypted Association Request elements",
2412 pos
, left
- AES_BLOCK_SIZE
);
2414 if (wpa_fils_validate_key_confirm(sm
, pos
, left
- AES_BLOCK_SIZE
) < 0) {
2415 wpa_printf(MSG_DEBUG
, "FILS: Key Confirm validation failed");
2419 return left
- AES_BLOCK_SIZE
;
2423 int fils_encrypt_assoc(struct wpa_state_machine
*sm
, u8
*buf
,
2424 size_t current_len
, size_t max_len
,
2425 const struct wpabuf
*hlp
)
2427 u8
*end
= buf
+ max_len
;
2428 u8
*pos
= buf
+ current_len
;
2429 struct ieee80211_mgmt
*mgmt
;
2430 struct wpabuf
*plain
;
2434 if (!sm
|| !sm
->PTK_valid
)
2437 wpa_hexdump(MSG_DEBUG
,
2438 "FILS: Association Response frame before FILS processing",
2441 mgmt
= (struct ieee80211_mgmt
*) buf
;
2443 /* AES-SIV AAD vectors */
2445 /* The AP's BSSID */
2447 aad_len
[0] = ETH_ALEN
;
2448 /* The STA's MAC address */
2450 aad_len
[1] = ETH_ALEN
;
2451 /* The AP's nonce */
2452 aad
[2] = sm
->ANonce
;
2453 aad_len
[2] = FILS_NONCE_LEN
;
2454 /* The STA's nonce */
2455 aad
[3] = sm
->SNonce
;
2456 aad_len
[3] = FILS_NONCE_LEN
;
2458 * The (Re)Association Response frame from the Capability Information
2459 * field (the same offset in both Association and Reassociation
2460 * Response frames) to the FILS Session element (both inclusive).
2462 aad
[4] = (const u8
*) &mgmt
->u
.assoc_resp
.capab_info
;
2463 aad_len
[4] = pos
- aad
[4];
2465 /* The following elements will be encrypted with AES-SIV */
2466 plain
= fils_prepare_plainbuf(sm
, hlp
);
2468 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2472 if (pos
+ wpabuf_len(plain
) + AES_BLOCK_SIZE
> end
) {
2473 wpa_printf(MSG_DEBUG
,
2474 "FILS: Not enough room for FILS elements");
2479 wpa_hexdump_buf_key(MSG_DEBUG
, "FILS: Association Response plaintext",
2482 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
2483 wpabuf_head(plain
), wpabuf_len(plain
),
2484 5, aad
, aad_len
, pos
) < 0) {
2489 wpa_hexdump(MSG_DEBUG
,
2490 "FILS: Encrypted Association Response elements",
2491 pos
, AES_BLOCK_SIZE
+ wpabuf_len(plain
));
2492 current_len
+= wpabuf_len(plain
) + AES_BLOCK_SIZE
;
2495 sm
->fils_completed
= 1;
2501 static struct wpabuf
* fils_prepare_plainbuf(struct wpa_state_machine
*sm
,
2502 const struct wpabuf
*hlp
)
2504 struct wpabuf
*plain
;
2505 u8
*len
, *tmp
, *tmp2
;
2507 u8
*gtk
, dummy_gtk
[32];
2509 struct wpa_group
*gsm
;
2511 plain
= wpabuf_alloc(1000);
2515 /* TODO: FILS Public Key */
2517 /* FILS Key Confirmation */
2518 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2519 wpabuf_put_u8(plain
, 1 + sm
->fils_key_auth_len
); /* Length */
2520 /* Element ID Extension */
2521 wpabuf_put_u8(plain
, WLAN_EID_EXT_FILS_KEY_CONFIRM
);
2522 wpabuf_put_data(plain
, sm
->fils_key_auth_ap
, sm
->fils_key_auth_len
);
2524 /* FILS HLP Container */
2526 wpabuf_put_buf(plain
, hlp
);
2528 /* TODO: FILS IP Address Assignment */
2532 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2533 len
= wpabuf_put(plain
, 1);
2534 wpabuf_put_u8(plain
, WLAN_EID_EXT_KEY_DELIVERY
);
2535 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
,
2536 wpabuf_put(plain
, WPA_KEY_RSC_LEN
));
2538 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2539 gtk_len
= gsm
->GTK_len
;
2540 if (sm
->wpa_auth
->conf
.disable_gtk
) {
2542 * Provide unique random GTK to each STA to prevent use
2543 * of GTK in the BSS.
2545 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0) {
2551 hdr
[0] = gsm
->GN
& 0x03;
2553 tmp
= wpabuf_put(plain
, 0);
2554 tmp2
= wpa_add_kde(tmp
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
2556 wpabuf_put(plain
, tmp2
- tmp
);
2559 tmp
= wpabuf_put(plain
, 0);
2560 tmp2
= ieee80211w_kde_add(sm
, tmp
);
2561 wpabuf_put(plain
, tmp2
- tmp
);
2563 *len
= (u8
*) wpabuf_put(plain
, 0) - len
- 1;
2568 int fils_set_tk(struct wpa_state_machine
*sm
)
2573 if (!sm
|| !sm
->PTK_valid
)
2576 alg
= wpa_cipher_to_alg(sm
->pairwise
);
2577 klen
= wpa_cipher_key_len(sm
->pairwise
);
2579 wpa_printf(MSG_DEBUG
, "FILS: Configure TK to the driver");
2580 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
2581 sm
->PTK
.tk
, klen
)) {
2582 wpa_printf(MSG_DEBUG
, "FILS: Failed to set TK to the driver");
2590 u8
* hostapd_eid_assoc_fils_session(struct wpa_state_machine
*sm
, u8
*buf
,
2591 const u8
*fils_session
, struct wpabuf
*hlp
)
2593 struct wpabuf
*plain
;
2597 *pos
++ = WLAN_EID_EXTENSION
; /* Element ID */
2598 *pos
++ = 1 + FILS_SESSION_LEN
; /* Length */
2599 *pos
++ = WLAN_EID_EXT_FILS_SESSION
; /* Element ID Extension */
2600 os_memcpy(pos
, fils_session
, FILS_SESSION_LEN
);
2601 pos
+= FILS_SESSION_LEN
;
2603 plain
= fils_prepare_plainbuf(sm
, hlp
);
2605 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2609 os_memcpy(pos
, wpabuf_head(plain
), wpabuf_len(plain
));
2610 pos
+= wpabuf_len(plain
);
2612 wpa_printf(MSG_DEBUG
, "%s: plain buf_len: %u", __func__
,
2613 (unsigned int) wpabuf_len(plain
));
2615 sm
->fils_completed
= 1;
2619 #endif /* CONFIG_FILS */
2622 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING
)
2624 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2626 int ok
= 0, psk_found
= 0;
2627 const u8
*pmk
= NULL
;
2630 const u8
*eapol_key_ie
, *key_data
, *mic
;
2631 u16 key_data_length
;
2632 size_t mic_len
, eapol_key_ie_len
;
2633 struct ieee802_1x_hdr
*hdr
;
2634 struct wpa_eapol_key
*key
;
2635 struct wpa_eapol_ie_parse kde
;
2637 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING
, wpa_ptk
);
2638 sm
->EAPOLKeyReceived
= FALSE
;
2639 sm
->update_snonce
= FALSE
;
2640 os_memset(&PTK
, 0, sizeof(PTK
));
2642 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
2644 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2645 * WPA-PSK: iterate through possible PSKs and select the one matching
2648 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
2649 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
2650 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
2651 sm
->p2p_dev_addr
, pmk
, &pmk_len
);
2657 pmk_len
= sm
->pmk_len
;
2660 if (wpa_derive_ptk(sm
, sm
->SNonce
, pmk
, pmk_len
, &PTK
) < 0)
2664 wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
2665 sm
->last_rx_eapol_key
,
2666 sm
->last_rx_eapol_key_len
) == 0) {
2673 wpa_aead_decrypt(sm
, &PTK
, sm
->last_rx_eapol_key
,
2674 sm
->last_rx_eapol_key_len
, NULL
) == 0) {
2678 #endif /* CONFIG_FILS */
2680 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
))
2685 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2686 "invalid MIC in msg 2/4 of 4-Way Handshake");
2688 wpa_auth_psk_failure_report(sm
->wpa_auth
, sm
->addr
);
2693 * Note: last_rx_eapol_key length fields have already been validated in
2696 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
2697 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2698 mic
= (u8
*) (key
+ 1);
2699 key_data
= mic
+ mic_len
+ 2;
2700 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
2701 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
2702 sizeof(*key
) - mic_len
- 2)
2705 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
2706 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2707 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2711 eapol_key_ie
= kde
.rsn_ie
;
2712 eapol_key_ie_len
= kde
.rsn_ie_len
;
2713 } else if (kde
.osen
) {
2714 eapol_key_ie
= kde
.osen
;
2715 eapol_key_ie_len
= kde
.osen_len
;
2717 eapol_key_ie
= kde
.wpa_ie
;
2718 eapol_key_ie_len
= kde
.wpa_ie_len
;
2720 ft
= sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
);
2721 if (sm
->wpa_ie
== NULL
||
2722 wpa_compare_rsn_ie(ft
, sm
->wpa_ie
, sm
->wpa_ie_len
,
2723 eapol_key_ie
, eapol_key_ie_len
)) {
2724 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2725 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2727 wpa_hexdump(MSG_DEBUG
, "WPA IE in AssocReq",
2728 sm
->wpa_ie
, sm
->wpa_ie_len
);
2730 wpa_hexdump(MSG_DEBUG
, "WPA IE in msg 2/4",
2731 eapol_key_ie
, eapol_key_ie_len
);
2732 /* MLME-DEAUTHENTICATE.request */
2733 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
2734 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2737 #ifdef CONFIG_IEEE80211R_AP
2738 if (ft
&& ft_check_msg_2_of_4(wpa_auth
, sm
, &kde
) < 0) {
2739 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
2740 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2743 #endif /* CONFIG_IEEE80211R_AP */
2745 if (kde
.ip_addr_req
&& kde
.ip_addr_req
[0] &&
2746 wpa_auth
->ip_pool
&& WPA_GET_BE32(sm
->ip_addr
) == 0) {
2748 wpa_printf(MSG_DEBUG
,
2749 "P2P: IP address requested in EAPOL-Key exchange");
2750 idx
= bitfield_get_first_zero(wpa_auth
->ip_pool
);
2752 u32 start
= WPA_GET_BE32(wpa_auth
->conf
.ip_addr_start
);
2753 bitfield_set(wpa_auth
->ip_pool
, idx
);
2754 WPA_PUT_BE32(sm
->ip_addr
, start
+ idx
);
2755 wpa_printf(MSG_DEBUG
,
2756 "P2P: Assigned IP address %u.%u.%u.%u to "
2757 MACSTR
, sm
->ip_addr
[0], sm
->ip_addr
[1],
2758 sm
->ip_addr
[2], sm
->ip_addr
[3],
2762 #endif /* CONFIG_P2P */
2764 #ifdef CONFIG_IEEE80211R_AP
2765 if (sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2767 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2768 * with the value we derived.
2770 if (os_memcmp_const(sm
->sup_pmk_r1_name
, sm
->pmk_r1_name
,
2771 WPA_PMK_NAME_LEN
) != 0) {
2772 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2773 "PMKR1Name mismatch in FT 4-way "
2775 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from "
2777 sm
->sup_pmk_r1_name
, WPA_PMK_NAME_LEN
);
2778 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
2779 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
2783 #endif /* CONFIG_IEEE80211R_AP */
2785 sm
->pending_1_of_4_timeout
= 0;
2786 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
2788 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
2789 /* PSK may have changed from the previous choice, so update
2790 * state machine data based on whatever PSK was selected here.
2792 os_memcpy(sm
->PMK
, pmk
, PMK_LEN
);
2793 sm
->pmk_len
= PMK_LEN
;
2796 sm
->MICVerified
= TRUE
;
2798 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
2799 sm
->PTK_valid
= TRUE
;
2803 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING2
)
2805 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING2
, wpa_ptk
);
2810 #ifdef CONFIG_IEEE80211W
2812 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
2814 if (sm
->mgmt_frame_prot
) {
2816 len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
2817 return 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
+ len
;
2824 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
2826 struct wpa_igtk_kde igtk
;
2827 struct wpa_group
*gsm
= sm
->group
;
2828 u8 rsc
[WPA_KEY_RSC_LEN
];
2829 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
2831 if (!sm
->mgmt_frame_prot
)
2834 igtk
.keyid
[0] = gsm
->GN_igtk
;
2836 if (gsm
->wpa_group_state
!= WPA_GROUP_SETKEYSDONE
||
2837 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, rsc
) < 0)
2838 os_memset(igtk
.pn
, 0, sizeof(igtk
.pn
));
2840 os_memcpy(igtk
.pn
, rsc
, sizeof(igtk
.pn
));
2841 os_memcpy(igtk
.igtk
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
2842 if (sm
->wpa_auth
->conf
.disable_gtk
) {
2844 * Provide unique random IGTK to each STA to prevent use of
2847 if (random_get_bytes(igtk
.igtk
, len
) < 0)
2850 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_IGTK
,
2851 (const u8
*) &igtk
, WPA_IGTK_KDE_PREFIX_LEN
+ len
,
2857 #else /* CONFIG_IEEE80211W */
2859 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
2865 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
2870 #endif /* CONFIG_IEEE80211W */
2873 SM_STATE(WPA_PTK
, PTKINITNEGOTIATING
)
2875 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
, dummy_gtk
[32];
2876 size_t gtk_len
, kde_len
;
2877 struct wpa_group
*gsm
= sm
->group
;
2879 int wpa_ie_len
, secure
, keyidx
, encr
= 0;
2881 SM_ENTRY_MA(WPA_PTK
, PTKINITNEGOTIATING
, wpa_ptk
);
2882 sm
->TimeoutEvt
= FALSE
;
2885 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
2886 /* No point in sending the EAPOL-Key - we will disconnect
2887 * immediately following this. */
2891 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2892 GTK[GN], IGTK, [FTIE], [TIE * 2])
2894 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
2895 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
2896 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2897 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
2898 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
2899 if (sm
->wpa
== WPA_VERSION_WPA
&&
2900 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
2901 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
2902 /* WPA-only STA, remove RSN IE and possible MDIE */
2903 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
2904 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
2905 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
2906 wpa_ie_len
= wpa_ie
[1] + 2;
2908 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2909 "sending 3/4 msg of 4-Way Handshake");
2910 if (sm
->wpa
== WPA_VERSION_WPA2
) {
2911 /* WPA2 send GTK in the 4-way handshake */
2913 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2914 gtk_len
= gsm
->GTK_len
;
2915 if (sm
->wpa_auth
->conf
.disable_gtk
) {
2917 * Provide unique random GTK to each STA to prevent use
2918 * of GTK in the BSS.
2920 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0)
2928 /* WPA does not include GTK in msg 3/4 */
2934 if (sm
->rx_eapol_key_secure
) {
2936 * It looks like Windows 7 supplicant tries to use
2937 * Secure bit in msg 2/4 after having reported Michael
2938 * MIC failure and it then rejects the 4-way handshake
2939 * if msg 3/4 does not set Secure bit. Work around this
2940 * by setting the Secure bit here even in the case of
2941 * WPA if the supplicant used it first.
2943 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2944 "STA used Secure bit in WPA msg 2/4 - "
2945 "set Secure for 3/4 as workaround");
2950 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
);
2952 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
2953 #ifdef CONFIG_IEEE80211R_AP
2954 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2955 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
2956 kde_len
+= 300; /* FTIE + 2 * TIE */
2958 #endif /* CONFIG_IEEE80211R_AP */
2960 if (WPA_GET_BE32(sm
->ip_addr
) > 0)
2961 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 3 * 4;
2962 #endif /* CONFIG_P2P */
2963 kde
= os_malloc(kde_len
);
2968 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
2970 #ifdef CONFIG_IEEE80211R_AP
2971 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2976 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
2978 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
2979 "PMKR1Name into RSN IE in EAPOL-Key data");
2986 #endif /* CONFIG_IEEE80211R_AP */
2989 hdr
[0] = keyidx
& 0x03;
2991 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
2994 pos
= ieee80211w_kde_add(sm
, pos
);
2996 #ifdef CONFIG_IEEE80211R_AP
2997 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2999 struct wpa_auth_config
*conf
;
3001 conf
= &sm
->wpa_auth
->conf
;
3002 if (sm
->assoc_resp_ftie
&&
3003 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
3004 os_memcpy(pos
, sm
->assoc_resp_ftie
,
3005 2 + sm
->assoc_resp_ftie
[1]);
3006 res
= 2 + sm
->assoc_resp_ftie
[1];
3008 res
= wpa_write_ftie(conf
, conf
->r0_key_holder
,
3009 conf
->r0_key_holder_len
,
3011 kde
+ kde_len
- pos
,
3015 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
3016 "into EAPOL-Key Key Data");
3022 /* TIE[ReassociationDeadline] (TU) */
3023 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3025 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
3026 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
3029 /* TIE[KeyLifetime] (seconds) */
3030 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3032 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
3033 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
* 60);
3036 #endif /* CONFIG_IEEE80211R_AP */
3038 if (WPA_GET_BE32(sm
->ip_addr
) > 0) {
3040 os_memcpy(addr
, sm
->ip_addr
, 4);
3041 os_memcpy(addr
+ 4, sm
->wpa_auth
->conf
.ip_addr_mask
, 4);
3042 os_memcpy(addr
+ 8, sm
->wpa_auth
->conf
.ip_addr_go
, 4);
3043 pos
= wpa_add_kde(pos
, WFA_KEY_DATA_IP_ADDR_ALLOC
,
3044 addr
, sizeof(addr
), NULL
, 0);
3046 #endif /* CONFIG_P2P */
3048 wpa_send_eapol(sm
->wpa_auth
, sm
,
3049 (secure
? WPA_KEY_INFO_SECURE
: 0) |
3050 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3051 WPA_KEY_INFO_MIC
: 0) |
3052 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
3053 WPA_KEY_INFO_KEY_TYPE
,
3054 _rsc
, sm
->ANonce
, kde
, pos
- kde
, keyidx
, encr
);
3059 SM_STATE(WPA_PTK
, PTKINITDONE
)
3061 SM_ENTRY_MA(WPA_PTK
, PTKINITDONE
, wpa_ptk
);
3062 sm
->EAPOLKeyReceived
= FALSE
;
3064 enum wpa_alg alg
= wpa_cipher_to_alg(sm
->pairwise
);
3065 int klen
= wpa_cipher_key_len(sm
->pairwise
);
3066 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
3067 sm
->PTK
.tk
, klen
)) {
3068 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
,
3069 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3072 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3073 sm
->pairwise_set
= TRUE
;
3075 if (sm
->wpa_auth
->conf
.wpa_ptk_rekey
) {
3076 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
3077 eloop_register_timeout(sm
->wpa_auth
->conf
.
3078 wpa_ptk_rekey
, 0, wpa_rekey_ptk
,
3082 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3083 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
3084 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
3085 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3086 WPA_EAPOL_authorized
, 1);
3090 if (0 /* IBSS == TRUE */) {
3092 if (sm
->keycount
== 2) {
3093 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3094 WPA_EAPOL_portValid
, 1);
3097 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
,
3100 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyAvailable
, 0);
3101 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyDone
, 1);
3102 if (sm
->wpa
== WPA_VERSION_WPA
)
3103 sm
->PInitAKeys
= TRUE
;
3106 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3107 "pairwise key handshake completed (%s)",
3108 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3110 #ifdef CONFIG_IEEE80211R_AP
3111 wpa_ft_push_pmk_r1(sm
->wpa_auth
, sm
->addr
);
3112 #endif /* CONFIG_IEEE80211R_AP */
3118 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3121 SM_ENTER(WPA_PTK
, INITIALIZE
);
3122 else if (sm
->Disconnect
3123 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3124 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3125 "WPA_PTK: sm->Disconnect");
3126 SM_ENTER(WPA_PTK
, DISCONNECT
);
3128 else if (sm
->DeauthenticationRequest
)
3129 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3130 else if (sm
->AuthenticationRequest
)
3131 SM_ENTER(WPA_PTK
, AUTHENTICATION
);
3132 else if (sm
->ReAuthenticationRequest
)
3133 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3134 else if (sm
->PTKRequest
)
3135 SM_ENTER(WPA_PTK
, PTKSTART
);
3136 else switch (sm
->wpa_ptk_state
) {
3137 case WPA_PTK_INITIALIZE
:
3139 case WPA_PTK_DISCONNECT
:
3140 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3142 case WPA_PTK_DISCONNECTED
:
3143 SM_ENTER(WPA_PTK
, INITIALIZE
);
3145 case WPA_PTK_AUTHENTICATION
:
3146 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3148 case WPA_PTK_AUTHENTICATION2
:
3149 if (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
3150 wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
3151 WPA_EAPOL_keyRun
) > 0)
3152 SM_ENTER(WPA_PTK
, INITPMK
);
3153 else if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3154 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
3155 /* FIX: && 802.1X::keyRun */)
3156 SM_ENTER(WPA_PTK
, INITPSK
);
3157 else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
)
3158 SM_ENTER(WPA_PTK
, INITPMK
);
3160 case WPA_PTK_INITPMK
:
3161 if (wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
3162 WPA_EAPOL_keyAvailable
) > 0) {
3163 SM_ENTER(WPA_PTK
, PTKSTART
);
3165 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->pmksa
) {
3166 SM_ENTER(WPA_PTK
, PTKSTART
);
3167 #endif /* CONFIG_DPP */
3169 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3170 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3171 "INITPMK - keyAvailable = false");
3172 SM_ENTER(WPA_PTK
, DISCONNECT
);
3175 case WPA_PTK_INITPSK
:
3176 if (wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
,
3178 SM_ENTER(WPA_PTK
, PTKSTART
);
3180 } else if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
3181 SM_ENTER(WPA_PTK
, PTKSTART
);
3182 #endif /* CONFIG_SAE */
3184 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3185 "no PSK configured for the STA");
3186 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3187 SM_ENTER(WPA_PTK
, DISCONNECT
);
3190 case WPA_PTK_PTKSTART
:
3191 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3192 sm
->EAPOLKeyPairwise
)
3193 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3194 else if (sm
->TimeoutCtr
>
3195 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
3196 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3198 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3199 "PTKSTART: Retry limit %u reached",
3200 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3201 SM_ENTER(WPA_PTK
, DISCONNECT
);
3202 } else if (sm
->TimeoutEvt
)
3203 SM_ENTER(WPA_PTK
, PTKSTART
);
3205 case WPA_PTK_PTKCALCNEGOTIATING
:
3206 if (sm
->MICVerified
)
3207 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING2
);
3208 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3209 sm
->EAPOLKeyPairwise
)
3210 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3211 else if (sm
->TimeoutEvt
)
3212 SM_ENTER(WPA_PTK
, PTKSTART
);
3214 case WPA_PTK_PTKCALCNEGOTIATING2
:
3215 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3217 case WPA_PTK_PTKINITNEGOTIATING
:
3218 if (sm
->update_snonce
)
3219 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3220 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3221 sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3222 SM_ENTER(WPA_PTK
, PTKINITDONE
);
3223 else if (sm
->TimeoutCtr
>
3224 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
3225 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3227 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3228 "PTKINITNEGOTIATING: Retry limit %u reached",
3229 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3230 SM_ENTER(WPA_PTK
, DISCONNECT
);
3231 } else if (sm
->TimeoutEvt
)
3232 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3234 case WPA_PTK_PTKINITDONE
:
3240 SM_STATE(WPA_PTK_GROUP
, IDLE
)
3242 SM_ENTRY_MA(WPA_PTK_GROUP
, IDLE
, wpa_ptk_group
);
3244 /* Init flag is not cleared here, so avoid busy
3245 * loop by claiming nothing changed. */
3246 sm
->changed
= FALSE
;
3248 sm
->GTimeoutCtr
= 0;
3252 SM_STATE(WPA_PTK_GROUP
, REKEYNEGOTIATING
)
3254 u8 rsc
[WPA_KEY_RSC_LEN
];
3255 struct wpa_group
*gsm
= sm
->group
;
3257 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
3259 u8
*gtk
, dummy_gtk
[32];
3261 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYNEGOTIATING
, wpa_ptk_group
);
3264 if (sm
->GTimeoutCtr
> sm
->wpa_auth
->conf
.wpa_group_update_count
) {
3265 /* No point in sending the EAPOL-Key - we will disconnect
3266 * immediately following this. */
3270 if (sm
->wpa
== WPA_VERSION_WPA
)
3271 sm
->PInitAKeys
= FALSE
;
3272 sm
->TimeoutEvt
= FALSE
;
3273 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3274 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3275 if (gsm
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
)
3276 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3277 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3278 "sending 1/2 msg of Group Key Handshake");
3280 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3281 if (sm
->wpa_auth
->conf
.disable_gtk
) {
3283 * Provide unique random GTK to each STA to prevent use
3284 * of GTK in the BSS.
3286 if (random_get_bytes(dummy_gtk
, gsm
->GTK_len
) < 0)
3290 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3291 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
3292 ieee80211w_kde_len(sm
);
3293 kde_buf
= os_malloc(kde_len
);
3294 if (kde_buf
== NULL
)
3297 kde
= pos
= kde_buf
;
3298 hdr
[0] = gsm
->GN
& 0x03;
3300 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3302 pos
= ieee80211w_kde_add(sm
, pos
);
3303 kde_len
= pos
- kde
;
3306 kde_len
= gsm
->GTK_len
;
3309 wpa_send_eapol(sm
->wpa_auth
, sm
,
3310 WPA_KEY_INFO_SECURE
|
3311 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3312 WPA_KEY_INFO_MIC
: 0) |
3314 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
3315 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
3321 SM_STATE(WPA_PTK_GROUP
, REKEYESTABLISHED
)
3323 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYESTABLISHED
, wpa_ptk_group
);
3324 sm
->EAPOLKeyReceived
= FALSE
;
3325 if (sm
->GUpdateStationKeys
)
3326 sm
->group
->GKeyDoneStations
--;
3327 sm
->GUpdateStationKeys
= FALSE
;
3328 sm
->GTimeoutCtr
= 0;
3329 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3330 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3331 "group key handshake completed (%s)",
3332 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3337 SM_STATE(WPA_PTK_GROUP
, KEYERROR
)
3339 SM_ENTRY_MA(WPA_PTK_GROUP
, KEYERROR
, wpa_ptk_group
);
3340 if (sm
->GUpdateStationKeys
)
3341 sm
->group
->GKeyDoneStations
--;
3342 sm
->GUpdateStationKeys
= FALSE
;
3343 sm
->Disconnect
= TRUE
;
3344 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3345 "group key handshake failed (%s) after %u tries",
3346 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN",
3347 sm
->wpa_auth
->conf
.wpa_group_update_count
);
3351 SM_STEP(WPA_PTK_GROUP
)
3353 if (sm
->Init
|| sm
->PtkGroupInit
) {
3354 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3355 sm
->PtkGroupInit
= FALSE
;
3356 } else switch (sm
->wpa_ptk_group_state
) {
3357 case WPA_PTK_GROUP_IDLE
:
3358 if (sm
->GUpdateStationKeys
||
3359 (sm
->wpa
== WPA_VERSION_WPA
&& sm
->PInitAKeys
))
3360 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3362 case WPA_PTK_GROUP_REKEYNEGOTIATING
:
3363 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3364 !sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3365 SM_ENTER(WPA_PTK_GROUP
, REKEYESTABLISHED
);
3366 else if (sm
->GTimeoutCtr
>
3367 sm
->wpa_auth
->conf
.wpa_group_update_count
)
3368 SM_ENTER(WPA_PTK_GROUP
, KEYERROR
);
3369 else if (sm
->TimeoutEvt
)
3370 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3372 case WPA_PTK_GROUP_KEYERROR
:
3373 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3375 case WPA_PTK_GROUP_REKEYESTABLISHED
:
3376 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3382 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
3383 struct wpa_group
*group
)
3387 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3388 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3389 if (wpa_gmk_to_gtk(group
->GMK
, "Group key expansion",
3390 wpa_auth
->addr
, group
->GNonce
,
3391 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3393 wpa_hexdump_key(MSG_DEBUG
, "GTK",
3394 group
->GTK
[group
->GN
- 1], group
->GTK_len
);
3396 #ifdef CONFIG_IEEE80211W
3397 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3399 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3400 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3401 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3402 if (wpa_gmk_to_gtk(group
->GMK
, "IGTK key expansion",
3403 wpa_auth
->addr
, group
->GNonce
,
3404 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3406 wpa_hexdump_key(MSG_DEBUG
, "IGTK",
3407 group
->IGTK
[group
->GN_igtk
- 4], len
);
3409 #endif /* CONFIG_IEEE80211W */
3415 static void wpa_group_gtk_init(struct wpa_authenticator
*wpa_auth
,
3416 struct wpa_group
*group
)
3418 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3419 "GTK_INIT (VLAN-ID %d)", group
->vlan_id
);
3420 group
->changed
= FALSE
; /* GInit is not cleared here; avoid loop */
3421 group
->wpa_group_state
= WPA_GROUP_GTK_INIT
;
3424 os_memset(group
->GTK
, 0, sizeof(group
->GTK
));
3427 #ifdef CONFIG_IEEE80211W
3430 #endif /* CONFIG_IEEE80211W */
3431 /* GTK[GN] = CalcGTK() */
3432 wpa_gtk_update(wpa_auth
, group
);
3436 static int wpa_group_update_sta(struct wpa_state_machine
*sm
, void *ctx
)
3438 if (ctx
!= NULL
&& ctx
!= sm
->group
)
3441 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITDONE
) {
3442 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3443 "Not in PTKINITDONE; skip Group Key update");
3444 sm
->GUpdateStationKeys
= FALSE
;
3447 if (sm
->GUpdateStationKeys
) {
3449 * This should not really happen, so add a debug log entry.
3450 * Since we clear the GKeyDoneStations before the loop, the
3451 * station needs to be counted here anyway.
3453 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3454 "GUpdateStationKeys was already set when "
3455 "marking station for GTK rekeying");
3458 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
3459 if (sm
->is_wnmsleep
)
3462 sm
->group
->GKeyDoneStations
++;
3463 sm
->GUpdateStationKeys
= TRUE
;
3470 #ifdef CONFIG_WNM_AP
3471 /* update GTK when exiting WNM-Sleep Mode */
3472 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine
*sm
)
3474 if (sm
== NULL
|| sm
->is_wnmsleep
)
3477 wpa_group_update_sta(sm
, NULL
);
3481 void wpa_set_wnmsleep(struct wpa_state_machine
*sm
, int flag
)
3484 sm
->is_wnmsleep
= !!flag
;
3488 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3490 struct wpa_group
*gsm
= sm
->group
;
3495 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
3498 *pos
++ = WNM_SLEEP_SUBELEM_GTK
;
3499 *pos
++ = 11 + gsm
->GTK_len
;
3500 /* Key ID in B0-B1 of Key Info */
3501 WPA_PUT_LE16(pos
, gsm
->GN
& 0x03);
3503 *pos
++ = gsm
->GTK_len
;
3504 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, pos
) != 0)
3507 os_memcpy(pos
, gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
3508 pos
+= gsm
->GTK_len
;
3510 wpa_printf(MSG_DEBUG
, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3512 wpa_hexdump_key(MSG_DEBUG
, "WNM: GTK in WNM-Sleep Mode exit",
3513 gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
3519 #ifdef CONFIG_IEEE80211W
3520 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3522 struct wpa_group
*gsm
= sm
->group
;
3524 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3528 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3530 *pos
++ = WNM_SLEEP_SUBELEM_IGTK
;
3531 *pos
++ = 2 + 6 + len
;
3532 WPA_PUT_LE16(pos
, gsm
->GN_igtk
);
3534 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, pos
) != 0)
3538 os_memcpy(pos
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3541 wpa_printf(MSG_DEBUG
, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3543 wpa_hexdump_key(MSG_DEBUG
, "WNM: IGTK in WNM-Sleep Mode exit",
3544 gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3548 #endif /* CONFIG_IEEE80211W */
3549 #endif /* CONFIG_WNM_AP */
3552 static void wpa_group_setkeys(struct wpa_authenticator
*wpa_auth
,
3553 struct wpa_group
*group
)
3557 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3558 "SETKEYS (VLAN-ID %d)", group
->vlan_id
);
3559 group
->changed
= TRUE
;
3560 group
->wpa_group_state
= WPA_GROUP_SETKEYS
;
3561 group
->GTKReKey
= FALSE
;
3563 group
->GM
= group
->GN
;
3565 #ifdef CONFIG_IEEE80211W
3566 tmp
= group
->GM_igtk
;
3567 group
->GM_igtk
= group
->GN_igtk
;
3568 group
->GN_igtk
= tmp
;
3569 #endif /* CONFIG_IEEE80211W */
3570 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3571 * counting the STAs that are marked with GUpdateStationKeys instead of
3572 * including all STAs that could be in not-yet-completed state. */
3573 wpa_gtk_update(wpa_auth
, group
);
3575 if (group
->GKeyDoneStations
) {
3576 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: Unexpected "
3577 "GKeyDoneStations=%d when starting new GTK rekey",
3578 group
->GKeyDoneStations
);
3579 group
->GKeyDoneStations
= 0;
3581 wpa_auth_for_each_sta(wpa_auth
, wpa_group_update_sta
, group
);
3582 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: GKeyDoneStations=%d",
3583 group
->GKeyDoneStations
);
3587 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
3588 struct wpa_group
*group
)
3592 if (wpa_auth_set_key(wpa_auth
, group
->vlan_id
,
3593 wpa_cipher_to_alg(wpa_auth
->conf
.wpa_group
),
3594 broadcast_ether_addr
, group
->GN
,
3595 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3598 #ifdef CONFIG_IEEE80211W
3599 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3603 alg
= wpa_cipher_to_alg(wpa_auth
->conf
.group_mgmt_cipher
);
3604 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3607 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, alg
,
3608 broadcast_ether_addr
, group
->GN_igtk
,
3609 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3612 #endif /* CONFIG_IEEE80211W */
3618 static int wpa_group_disconnect_cb(struct wpa_state_machine
*sm
, void *ctx
)
3620 if (sm
->group
== ctx
) {
3621 wpa_printf(MSG_DEBUG
, "WPA: Mark STA " MACSTR
3622 " for discconnection due to fatal failure",
3624 sm
->Disconnect
= TRUE
;
3631 static void wpa_group_fatal_failure(struct wpa_authenticator
*wpa_auth
,
3632 struct wpa_group
*group
)
3634 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state FATAL_FAILURE");
3635 group
->changed
= TRUE
;
3636 group
->wpa_group_state
= WPA_GROUP_FATAL_FAILURE
;
3637 wpa_auth_for_each_sta(wpa_auth
, wpa_group_disconnect_cb
, group
);
3641 static int wpa_group_setkeysdone(struct wpa_authenticator
*wpa_auth
,
3642 struct wpa_group
*group
)
3644 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3645 "SETKEYSDONE (VLAN-ID %d)", group
->vlan_id
);
3646 group
->changed
= TRUE
;
3647 group
->wpa_group_state
= WPA_GROUP_SETKEYSDONE
;
3649 if (wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
3650 wpa_group_fatal_failure(wpa_auth
, group
);
3658 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
3659 struct wpa_group
*group
)
3662 wpa_group_gtk_init(wpa_auth
, group
);
3663 } else if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
) {
3664 /* Do not allow group operations */
3665 } else if (group
->wpa_group_state
== WPA_GROUP_GTK_INIT
&&
3666 group
->GTKAuthenticator
) {
3667 wpa_group_setkeysdone(wpa_auth
, group
);
3668 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
&&
3670 wpa_group_setkeys(wpa_auth
, group
);
3671 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYS
) {
3672 if (group
->GKeyDoneStations
== 0)
3673 wpa_group_setkeysdone(wpa_auth
, group
);
3674 else if (group
->GTKReKey
)
3675 wpa_group_setkeys(wpa_auth
, group
);
3680 static int wpa_sm_step(struct wpa_state_machine
*sm
)
3685 if (sm
->in_step_loop
) {
3686 /* This should not happen, but if it does, make sure we do not
3687 * end up freeing the state machine too early by exiting the
3688 * recursive call. */
3689 wpa_printf(MSG_ERROR
, "WPA: wpa_sm_step() called recursively");
3693 sm
->in_step_loop
= 1;
3695 if (sm
->pending_deinit
)
3698 sm
->changed
= FALSE
;
3699 sm
->wpa_auth
->group
->changed
= FALSE
;
3701 SM_STEP_RUN(WPA_PTK
);
3702 if (sm
->pending_deinit
)
3704 SM_STEP_RUN(WPA_PTK_GROUP
);
3705 if (sm
->pending_deinit
)
3707 wpa_group_sm_step(sm
->wpa_auth
, sm
->group
);
3708 } while (sm
->changed
|| sm
->wpa_auth
->group
->changed
);
3709 sm
->in_step_loop
= 0;
3711 if (sm
->pending_deinit
) {
3712 wpa_printf(MSG_DEBUG
, "WPA: Completing pending STA state "
3713 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
3714 wpa_free_sta_sm(sm
);
3721 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
)
3723 struct wpa_state_machine
*sm
= eloop_ctx
;
3728 void wpa_auth_sm_notify(struct wpa_state_machine
*sm
)
3732 eloop_register_timeout(0, 0, wpa_sm_call_step
, sm
, NULL
);
3736 void wpa_gtk_rekey(struct wpa_authenticator
*wpa_auth
)
3739 struct wpa_group
*group
;
3741 if (wpa_auth
== NULL
)
3744 group
= wpa_auth
->group
;
3746 for (i
= 0; i
< 2; i
++) {
3748 group
->GM
= group
->GN
;
3750 #ifdef CONFIG_IEEE80211W
3751 tmp
= group
->GM_igtk
;
3752 group
->GM_igtk
= group
->GN_igtk
;
3753 group
->GN_igtk
= tmp
;
3754 #endif /* CONFIG_IEEE80211W */
3755 wpa_gtk_update(wpa_auth
, group
);
3756 wpa_group_config_group_keys(wpa_auth
, group
);
3761 static const char * wpa_bool_txt(int val
)
3763 return val
? "TRUE" : "FALSE";
3767 #define RSN_SUITE "%02x-%02x-%02x-%d"
3768 #define RSN_SUITE_ARG(s) \
3769 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3771 int wpa_get_mib(struct wpa_authenticator
*wpa_auth
, char *buf
, size_t buflen
)
3774 char pmkid_txt
[PMKID_LEN
* 2 + 1];
3775 #ifdef CONFIG_RSN_PREAUTH
3776 const int preauth
= 1;
3777 #else /* CONFIG_RSN_PREAUTH */
3778 const int preauth
= 0;
3779 #endif /* CONFIG_RSN_PREAUTH */
3781 if (wpa_auth
== NULL
)
3784 ret
= os_snprintf(buf
+ len
, buflen
- len
,
3785 "dot11RSNAOptionImplemented=TRUE\n"
3786 "dot11RSNAPreauthenticationImplemented=%s\n"
3787 "dot11RSNAEnabled=%s\n"
3788 "dot11RSNAPreauthenticationEnabled=%s\n",
3789 wpa_bool_txt(preauth
),
3790 wpa_bool_txt(wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
),
3791 wpa_bool_txt(wpa_auth
->conf
.rsn_preauth
));
3792 if (os_snprintf_error(buflen
- len
, ret
))
3796 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
3797 wpa_auth
->dot11RSNAPMKIDUsed
, PMKID_LEN
);
3800 buf
+ len
, buflen
- len
,
3801 "dot11RSNAConfigVersion=%u\n"
3802 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
3803 /* FIX: dot11RSNAConfigGroupCipher */
3804 /* FIX: dot11RSNAConfigGroupRekeyMethod */
3805 /* FIX: dot11RSNAConfigGroupRekeyTime */
3806 /* FIX: dot11RSNAConfigGroupRekeyPackets */
3807 "dot11RSNAConfigGroupRekeyStrict=%u\n"
3808 "dot11RSNAConfigGroupUpdateCount=%u\n"
3809 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
3810 "dot11RSNAConfigGroupCipherSize=%u\n"
3811 "dot11RSNAConfigPMKLifetime=%u\n"
3812 "dot11RSNAConfigPMKReauthThreshold=%u\n"
3813 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
3814 "dot11RSNAConfigSATimeout=%u\n"
3815 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
3816 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
3817 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
3818 "dot11RSNAPMKIDUsed=%s\n"
3819 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
3820 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
3821 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
3822 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
3823 "dot11RSNA4WayHandshakeFailures=%u\n"
3824 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
3826 !!wpa_auth
->conf
.wpa_strict_rekey
,
3827 wpa_auth
->conf
.wpa_group_update_count
,
3828 wpa_auth
->conf
.wpa_pairwise_update_count
,
3829 wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
) * 8,
3830 dot11RSNAConfigPMKLifetime
,
3831 dot11RSNAConfigPMKReauthThreshold
,
3832 dot11RSNAConfigSATimeout
,
3833 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteSelected
),
3834 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherSelected
),
3835 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherSelected
),
3837 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteRequested
),
3838 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherRequested
),
3839 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherRequested
),
3840 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
,
3841 wpa_auth
->dot11RSNA4WayHandshakeFailures
);
3842 if (os_snprintf_error(buflen
- len
, ret
))
3846 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
3847 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
3850 ret
= os_snprintf(buf
+ len
, buflen
- len
, "hostapdWPAGroupState=%d\n",
3851 wpa_auth
->group
->wpa_group_state
);
3852 if (os_snprintf_error(buflen
- len
, ret
))
3860 int wpa_get_mib_sta(struct wpa_state_machine
*sm
, char *buf
, size_t buflen
)
3868 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
3870 /* dot11RSNAStatsEntry */
3872 pairwise
= wpa_cipher_to_suite(sm
->wpa
== WPA_VERSION_WPA2
?
3873 WPA_PROTO_RSN
: WPA_PROTO_WPA
,
3879 buf
+ len
, buflen
- len
,
3880 /* TODO: dot11RSNAStatsIndex */
3881 "dot11RSNAStatsSTAAddress=" MACSTR
"\n"
3882 "dot11RSNAStatsVersion=1\n"
3883 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE
"\n"
3884 /* TODO: dot11RSNAStatsTKIPICVErrors */
3885 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
3886 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
3887 /* TODO: dot11RSNAStatsCCMPReplays */
3888 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
3889 /* TODO: dot11RSNAStatsTKIPReplays */,
3891 RSN_SUITE_ARG(pairwise
),
3892 sm
->dot11RSNAStatsTKIPLocalMICFailures
,
3893 sm
->dot11RSNAStatsTKIPRemoteMICFailures
);
3894 if (os_snprintf_error(buflen
- len
, ret
))
3899 ret
= os_snprintf(buf
+ len
, buflen
- len
,
3900 "hostapdWPAPTKState=%d\n"
3901 "hostapdWPAPTKGroupState=%d\n",
3903 sm
->wpa_ptk_group_state
);
3904 if (os_snprintf_error(buflen
- len
, ret
))
3912 void wpa_auth_countermeasures_start(struct wpa_authenticator
*wpa_auth
)
3915 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
++;
3919 int wpa_auth_pairwise_set(struct wpa_state_machine
*sm
)
3921 return sm
&& sm
->pairwise_set
;
3925 int wpa_auth_get_pairwise(struct wpa_state_machine
*sm
)
3927 return sm
->pairwise
;
3931 int wpa_auth_sta_key_mgmt(struct wpa_state_machine
*sm
)
3935 return sm
->wpa_key_mgmt
;
3939 int wpa_auth_sta_wpa_version(struct wpa_state_machine
*sm
)
3947 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine
*sm
)
3949 if (!sm
|| !wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
3951 return sm
->tk_already_set
;
3955 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine
*sm
,
3956 struct rsn_pmksa_cache_entry
*entry
)
3958 if (sm
== NULL
|| sm
->pmksa
!= entry
)
3965 struct rsn_pmksa_cache_entry
*
3966 wpa_auth_sta_get_pmksa(struct wpa_state_machine
*sm
)
3968 return sm
? sm
->pmksa
: NULL
;
3972 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine
*sm
)
3975 sm
->dot11RSNAStatsTKIPLocalMICFailures
++;
3979 const u8
* wpa_auth_get_wpa_ie(struct wpa_authenticator
*wpa_auth
, size_t *len
)
3981 if (wpa_auth
== NULL
)
3983 *len
= wpa_auth
->wpa_ie_len
;
3984 return wpa_auth
->wpa_ie
;
3988 int wpa_auth_pmksa_add(struct wpa_state_machine
*sm
, const u8
*pmk
,
3989 unsigned int pmk_len
,
3990 int session_timeout
, struct eapol_state_machine
*eapol
)
3992 if (sm
== NULL
|| sm
->wpa
!= WPA_VERSION_WPA2
||
3993 sm
->wpa_auth
->conf
.disable_pmksa_caching
)
3996 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
3997 if (pmk_len
> PMK_LEN_SUITE_B_192
)
3998 pmk_len
= PMK_LEN_SUITE_B_192
;
3999 } else if (pmk_len
> PMK_LEN
) {
4003 if (pmksa_cache_auth_add(sm
->wpa_auth
->pmksa
, pmk
, pmk_len
, NULL
,
4004 sm
->PTK
.kck
, sm
->PTK
.kck_len
,
4005 sm
->wpa_auth
->addr
, sm
->addr
, session_timeout
,
4006 eapol
, sm
->wpa_key_mgmt
))
4013 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator
*wpa_auth
,
4014 const u8
*pmk
, size_t len
, const u8
*sta_addr
,
4015 int session_timeout
,
4016 struct eapol_state_machine
*eapol
)
4018 if (wpa_auth
== NULL
)
4021 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, len
, NULL
,
4024 sta_addr
, session_timeout
, eapol
,
4025 WPA_KEY_MGMT_IEEE8021X
))
4032 int wpa_auth_pmksa_add_sae(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4033 const u8
*pmk
, const u8
*pmkid
)
4035 if (wpa_auth
->conf
.disable_pmksa_caching
)
4038 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, PMK_LEN
, pmkid
,
4040 wpa_auth
->addr
, addr
, 0, NULL
,
4048 int wpa_auth_pmksa_add2(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4049 const u8
*pmk
, size_t pmk_len
, const u8
*pmkid
,
4050 int session_timeout
, int akmp
)
4052 if (wpa_auth
->conf
.disable_pmksa_caching
)
4055 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, pmk_len
, pmkid
,
4056 NULL
, 0, wpa_auth
->addr
, addr
, session_timeout
,
4064 void wpa_auth_pmksa_remove(struct wpa_authenticator
*wpa_auth
,
4067 struct rsn_pmksa_cache_entry
*pmksa
;
4069 if (wpa_auth
== NULL
|| wpa_auth
->pmksa
== NULL
)
4071 pmksa
= pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, NULL
);
4073 wpa_printf(MSG_DEBUG
, "WPA: Remove PMKSA cache entry for "
4074 MACSTR
" based on request", MAC2STR(sta_addr
));
4075 pmksa_cache_free_entry(wpa_auth
->pmksa
, pmksa
);
4080 int wpa_auth_pmksa_list(struct wpa_authenticator
*wpa_auth
, char *buf
,
4083 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4085 return pmksa_cache_auth_list(wpa_auth
->pmksa
, buf
, len
);
4089 void wpa_auth_pmksa_flush(struct wpa_authenticator
*wpa_auth
)
4091 if (wpa_auth
&& wpa_auth
->pmksa
)
4092 pmksa_cache_auth_flush(wpa_auth
->pmksa
);
4096 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4099 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4100 char *buf
, size_t len
)
4102 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4105 return pmksa_cache_auth_list_mesh(wpa_auth
->pmksa
, addr
, buf
, len
);
4109 struct rsn_pmksa_cache_entry
*
4110 wpa_auth_pmksa_create_entry(const u8
*aa
, const u8
*spa
, const u8
*pmk
,
4111 const u8
*pmkid
, int expiration
)
4113 struct rsn_pmksa_cache_entry
*entry
;
4114 struct os_reltime now
;
4116 entry
= pmksa_cache_auth_create_entry(pmk
, PMK_LEN
, pmkid
, NULL
, 0, aa
,
4117 spa
, 0, NULL
, WPA_KEY_MGMT_SAE
);
4121 os_get_reltime(&now
);
4122 entry
->expiration
= now
.sec
+ expiration
;
4127 int wpa_auth_pmksa_add_entry(struct wpa_authenticator
*wpa_auth
,
4128 struct rsn_pmksa_cache_entry
*entry
)
4132 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4135 ret
= pmksa_cache_auth_add_entry(wpa_auth
->pmksa
, entry
);
4137 wpa_printf(MSG_DEBUG
,
4138 "RSN: Failed to store external PMKSA cache for "
4139 MACSTR
, MAC2STR(entry
->spa
));
4144 #endif /* CONFIG_MESH */
4145 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4148 struct rsn_pmksa_cache_entry
*
4149 wpa_auth_pmksa_get(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
,
4152 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4154 return pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, pmkid
);
4158 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry
*pmksa
,
4159 struct wpa_state_machine
*sm
,
4160 struct wpa_authenticator
*wpa_auth
,
4167 os_memcpy(pmk
, pmksa
->pmk
, PMK_LEN
);
4168 os_memcpy(pmkid
, pmksa
->pmkid
, PMKID_LEN
);
4169 os_memcpy(wpa_auth
->dot11RSNAPMKIDUsed
, pmksa
->pmkid
, PMKID_LEN
);
4174 * Remove and free the group from wpa_authenticator. This is triggered by a
4175 * callback to make sure nobody is currently iterating the group list while it
4178 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
4179 struct wpa_group
*group
)
4181 struct wpa_group
*prev
= wpa_auth
->group
;
4183 wpa_printf(MSG_DEBUG
, "WPA: Remove group state machine for VLAN-ID %d",
4187 if (prev
->next
== group
) {
4188 /* This never frees the special first group as needed */
4189 prev
->next
= group
->next
;
4199 /* Increase the reference counter for group */
4200 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
4201 struct wpa_group
*group
)
4203 /* Skip the special first group */
4204 if (wpa_auth
->group
== group
)
4207 group
->references
++;
4211 /* Decrease the reference counter and maybe free the group */
4212 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
4213 struct wpa_group
*group
)
4215 /* Skip the special first group */
4216 if (wpa_auth
->group
== group
)
4219 group
->references
--;
4220 if (group
->references
)
4222 wpa_group_free(wpa_auth
, group
);
4227 * Add a group that has its references counter set to zero. Caller needs to
4228 * call wpa_group_get() on the return value to mark the entry in use.
4230 static struct wpa_group
*
4231 wpa_auth_add_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4233 struct wpa_group
*group
;
4235 if (wpa_auth
== NULL
|| wpa_auth
->group
== NULL
)
4238 wpa_printf(MSG_DEBUG
, "WPA: Add group state machine for VLAN-ID %d",
4240 group
= wpa_group_init(wpa_auth
, vlan_id
, 0);
4244 group
->next
= wpa_auth
->group
->next
;
4245 wpa_auth
->group
->next
= group
;
4252 * Enforce that the group state machine for the VLAN is running, increase
4253 * reference counter as interface is up. References might have been increased
4254 * even if a negative value is returned.
4255 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4257 int wpa_auth_ensure_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4259 struct wpa_group
*group
;
4261 if (wpa_auth
== NULL
)
4264 group
= wpa_auth
->group
;
4266 if (group
->vlan_id
== vlan_id
)
4268 group
= group
->next
;
4271 if (group
== NULL
) {
4272 group
= wpa_auth_add_group(wpa_auth
, vlan_id
);
4277 wpa_printf(MSG_DEBUG
,
4278 "WPA: Ensure group state machine running for VLAN ID %d",
4281 wpa_group_get(wpa_auth
, group
);
4282 group
->num_setup_iface
++;
4284 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4292 * Decrease reference counter, expected to be zero afterwards.
4293 * returns: -1 on error (group not found, group in fail state)
4294 * -2 if wpa_group is still referenced
4297 int wpa_auth_release_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4299 struct wpa_group
*group
;
4302 if (wpa_auth
== NULL
)
4305 group
= wpa_auth
->group
;
4307 if (group
->vlan_id
== vlan_id
)
4309 group
= group
->next
;
4315 wpa_printf(MSG_DEBUG
,
4316 "WPA: Try stopping group state machine for VLAN ID %d",
4319 if (group
->num_setup_iface
<= 0) {
4320 wpa_printf(MSG_ERROR
,
4321 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4325 group
->num_setup_iface
--;
4327 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4330 if (group
->references
> 1) {
4331 wpa_printf(MSG_DEBUG
,
4332 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4337 wpa_group_put(wpa_auth
, group
);
4343 int wpa_auth_sta_set_vlan(struct wpa_state_machine
*sm
, int vlan_id
)
4345 struct wpa_group
*group
;
4347 if (sm
== NULL
|| sm
->wpa_auth
== NULL
)
4350 group
= sm
->wpa_auth
->group
;
4352 if (group
->vlan_id
== vlan_id
)
4354 group
= group
->next
;
4357 if (group
== NULL
) {
4358 group
= wpa_auth_add_group(sm
->wpa_auth
, vlan_id
);
4363 if (sm
->group
== group
)
4366 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4369 wpa_printf(MSG_DEBUG
, "WPA: Moving STA " MACSTR
" to use group state "
4370 "machine for VLAN ID %d", MAC2STR(sm
->addr
), vlan_id
);
4372 wpa_group_get(sm
->wpa_auth
, group
);
4373 wpa_group_put(sm
->wpa_auth
, sm
->group
);
4380 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator
*wpa_auth
,
4381 struct wpa_state_machine
*sm
, int ack
)
4383 if (wpa_auth
== NULL
|| sm
== NULL
)
4385 wpa_printf(MSG_DEBUG
, "WPA: EAPOL-Key TX status for STA " MACSTR
4386 " ack=%d", MAC2STR(sm
->addr
), ack
);
4387 if (sm
->pending_1_of_4_timeout
&& ack
) {
4389 * Some deployed supplicant implementations update their SNonce
4390 * for each EAPOL-Key 2/4 message even within the same 4-way
4391 * handshake and then fail to use the first SNonce when
4392 * deriving the PTK. This results in unsuccessful 4-way
4393 * handshake whenever the relatively short initial timeout is
4394 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4395 * around this by increasing the timeout now that we know that
4396 * the station has received the frame.
4398 int timeout_ms
= eapol_key_timeout_subseq
;
4399 wpa_printf(MSG_DEBUG
, "WPA: Increase initial EAPOL-Key 1/4 "
4400 "timeout by %u ms because of acknowledged frame",
4402 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
4403 eloop_register_timeout(timeout_ms
/ 1000,
4404 (timeout_ms
% 1000) * 1000,
4405 wpa_send_eapol_timeout
, wpa_auth
, sm
);
4410 int wpa_auth_uses_sae(struct wpa_state_machine
*sm
)
4414 return wpa_key_mgmt_sae(sm
->wpa_key_mgmt
);
4418 int wpa_auth_uses_ft_sae(struct wpa_state_machine
*sm
)
4422 return sm
->wpa_key_mgmt
== WPA_KEY_MGMT_FT_SAE
;
4427 int wpa_auth_get_ip_addr(struct wpa_state_machine
*sm
, u8
*addr
)
4429 if (sm
== NULL
|| WPA_GET_BE32(sm
->ip_addr
) == 0)
4431 os_memcpy(addr
, sm
->ip_addr
, 4);
4434 #endif /* CONFIG_P2P */
4437 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator
*wpa_auth
,
4438 struct radius_das_attrs
*attr
)
4440 return pmksa_cache_auth_radius_das_disconnect(wpa_auth
->pmksa
, attr
);
4444 void wpa_auth_reconfig_group_keys(struct wpa_authenticator
*wpa_auth
)
4446 struct wpa_group
*group
;
4450 for (group
= wpa_auth
->group
; group
; group
= group
->next
)
4451 wpa_group_config_group_keys(wpa_auth
, group
);
4457 struct wpa_auth_fils_iter_data
{
4458 struct wpa_authenticator
*auth
;
4460 struct rsn_pmksa_cache_entry
*pmksa
;
4466 static int wpa_auth_fils_iter(struct wpa_authenticator
*a
, void *ctx
)
4468 struct wpa_auth_fils_iter_data
*data
= ctx
;
4470 if (a
== data
->auth
|| !a
->conf
.fils_cache_id_set
||
4471 os_memcmp(a
->conf
.fils_cache_id
, data
->cache_id
,
4472 FILS_CACHE_ID_LEN
) != 0)
4474 data
->pmksa
= pmksa_cache_auth_get(a
->pmksa
, data
->spa
, data
->pmkid
);
4475 return data
->pmksa
!= NULL
;
4479 struct rsn_pmksa_cache_entry
*
4480 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator
*wpa_auth
,
4481 const u8
*sta_addr
, const u8
*pmkid
)
4483 struct wpa_auth_fils_iter_data idata
;
4485 if (!wpa_auth
->conf
.fils_cache_id_set
)
4487 idata
.auth
= wpa_auth
;
4488 idata
.cache_id
= wpa_auth
->conf
.fils_cache_id
;
4490 idata
.spa
= sta_addr
;
4491 idata
.pmkid
= pmkid
;
4492 wpa_auth_for_each_auth(wpa_auth
, wpa_auth_fils_iter
, &idata
);
4497 #ifdef CONFIG_IEEE80211R_AP
4498 int wpa_auth_write_fte(struct wpa_authenticator
*wpa_auth
, u8
*buf
, size_t len
)
4500 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
4502 return wpa_write_ftie(conf
, conf
->r0_key_holder
,
4503 conf
->r0_key_holder_len
,
4504 NULL
, NULL
, buf
, len
, NULL
, 0);
4506 #endif /* CONFIG_IEEE80211R_AP */
4509 void wpa_auth_get_fils_aead_params(struct wpa_state_machine
*sm
,
4510 u8
*fils_anonce
, u8
*fils_snonce
,
4511 u8
*fils_kek
, size_t *fils_kek_len
)
4513 os_memcpy(fils_anonce
, sm
->ANonce
, WPA_NONCE_LEN
);
4514 os_memcpy(fils_snonce
, sm
->SNonce
, WPA_NONCE_LEN
);
4515 os_memcpy(fils_kek
, sm
->PTK
.kek
, WPA_KEK_MAX_LEN
);
4516 *fils_kek_len
= sm
->PTK
.kek_len
;
4519 #endif /* CONFIG_FILS */