2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2019, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/state_machine.h"
14 #include "utils/bitfield.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ocv.h"
17 #include "crypto/aes.h"
18 #include "crypto/aes_wrap.h"
19 #include "crypto/aes_siv.h"
20 #include "crypto/crypto.h"
21 #include "crypto/sha1.h"
22 #include "crypto/sha256.h"
23 #include "crypto/sha384.h"
24 #include "crypto/random.h"
25 #include "eapol_auth/eapol_auth_sm.h"
26 #include "drivers/driver.h"
27 #include "ap_config.h"
28 #include "ieee802_11.h"
30 #include "pmksa_cache_auth.h"
31 #include "wpa_auth_i.h"
32 #include "wpa_auth_ie.h"
34 #define STATE_MACHINE_DATA struct wpa_state_machine
35 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
36 #define STATE_MACHINE_ADDR sm->addr
39 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
);
40 static int wpa_sm_step(struct wpa_state_machine
*sm
);
41 static int wpa_verify_key_mic(int akmp
, size_t pmk_len
, struct wpa_ptk
*PTK
,
42 u8
*data
, size_t data_len
);
44 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
45 u8
*buf
, size_t buf_len
, u16
*_key_data_len
);
46 static struct wpabuf
* fils_prepare_plainbuf(struct wpa_state_machine
*sm
,
47 const struct wpabuf
*hlp
);
48 #endif /* CONFIG_FILS */
49 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
);
50 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
51 struct wpa_group
*group
);
52 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
);
53 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
54 struct wpa_group
*group
);
55 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
56 struct wpa_group
*group
);
57 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
58 const u8
*pmk
, unsigned int pmk_len
,
59 struct wpa_ptk
*ptk
, int force_sha256
);
60 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
61 struct wpa_group
*group
);
62 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
63 struct wpa_group
*group
);
64 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
65 struct wpa_group
*group
);
66 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
);
67 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
);
69 static const u32 eapol_key_timeout_first
= 100; /* ms */
70 static const u32 eapol_key_timeout_subseq
= 1000; /* ms */
71 static const u32 eapol_key_timeout_first_group
= 500; /* ms */
72 static const u32 eapol_key_timeout_no_retrans
= 4000; /* ms */
74 /* TODO: make these configurable */
75 static const int dot11RSNAConfigPMKLifetime
= 43200;
76 static const int dot11RSNAConfigPMKReauthThreshold
= 70;
77 static const int dot11RSNAConfigSATimeout
= 60;
80 static inline int wpa_auth_mic_failure_report(
81 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
83 if (wpa_auth
->cb
->mic_failure_report
)
84 return wpa_auth
->cb
->mic_failure_report(wpa_auth
->cb_ctx
, addr
);
89 static inline void wpa_auth_psk_failure_report(
90 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
92 if (wpa_auth
->cb
->psk_failure_report
)
93 wpa_auth
->cb
->psk_failure_report(wpa_auth
->cb_ctx
, addr
);
97 static inline void wpa_auth_set_eapol(struct wpa_authenticator
*wpa_auth
,
98 const u8
*addr
, wpa_eapol_variable var
,
101 if (wpa_auth
->cb
->set_eapol
)
102 wpa_auth
->cb
->set_eapol(wpa_auth
->cb_ctx
, addr
, var
, value
);
106 static inline int wpa_auth_get_eapol(struct wpa_authenticator
*wpa_auth
,
107 const u8
*addr
, wpa_eapol_variable var
)
109 if (wpa_auth
->cb
->get_eapol
== NULL
)
111 return wpa_auth
->cb
->get_eapol(wpa_auth
->cb_ctx
, addr
, var
);
115 static inline const u8
* wpa_auth_get_psk(struct wpa_authenticator
*wpa_auth
,
117 const u8
*p2p_dev_addr
,
118 const u8
*prev_psk
, size_t *psk_len
,
121 if (wpa_auth
->cb
->get_psk
== NULL
)
123 return wpa_auth
->cb
->get_psk(wpa_auth
->cb_ctx
, addr
, p2p_dev_addr
,
124 prev_psk
, psk_len
, vlan_id
);
128 static inline int wpa_auth_get_msk(struct wpa_authenticator
*wpa_auth
,
129 const u8
*addr
, u8
*msk
, size_t *len
)
131 if (wpa_auth
->cb
->get_msk
== NULL
)
133 return wpa_auth
->cb
->get_msk(wpa_auth
->cb_ctx
, addr
, msk
, len
);
137 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
139 enum wpa_alg alg
, const u8
*addr
, int idx
,
140 u8
*key
, size_t key_len
,
141 enum key_flag key_flag
)
143 if (wpa_auth
->cb
->set_key
== NULL
)
145 return wpa_auth
->cb
->set_key(wpa_auth
->cb_ctx
, vlan_id
, alg
, addr
, idx
,
146 key
, key_len
, key_flag
);
150 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
151 const u8
*addr
, int idx
, u8
*seq
)
155 if (wpa_auth
->cb
->get_seqnum
== NULL
)
157 res
= wpa_auth
->cb
->get_seqnum(wpa_auth
->cb_ctx
, addr
, idx
, seq
);
158 #ifdef CONFIG_TESTING_OPTIONS
159 if (!addr
&& idx
< 4 && wpa_auth
->conf
.gtk_rsc_override_set
) {
160 wpa_printf(MSG_DEBUG
,
161 "TESTING: Override GTK RSC %016llx --> %016llx",
162 (long long unsigned) WPA_GET_LE64(seq
),
164 WPA_GET_LE64(wpa_auth
->conf
.gtk_rsc_override
));
165 os_memcpy(seq
, wpa_auth
->conf
.gtk_rsc_override
,
168 if (!addr
&& idx
>= 4 && idx
<= 5 &&
169 wpa_auth
->conf
.igtk_rsc_override_set
) {
170 wpa_printf(MSG_DEBUG
,
171 "TESTING: Override IGTK RSC %016llx --> %016llx",
172 (long long unsigned) WPA_GET_LE64(seq
),
174 WPA_GET_LE64(wpa_auth
->conf
.igtk_rsc_override
));
175 os_memcpy(seq
, wpa_auth
->conf
.igtk_rsc_override
,
178 #endif /* CONFIG_TESTING_OPTIONS */
184 wpa_auth_send_eapol(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
185 const u8
*data
, size_t data_len
, int encrypt
)
187 if (wpa_auth
->cb
->send_eapol
== NULL
)
189 return wpa_auth
->cb
->send_eapol(wpa_auth
->cb_ctx
, addr
, data
, data_len
,
195 static inline int wpa_auth_start_ampe(struct wpa_authenticator
*wpa_auth
,
198 if (wpa_auth
->cb
->start_ampe
== NULL
)
200 return wpa_auth
->cb
->start_ampe(wpa_auth
->cb_ctx
, addr
);
202 #endif /* CONFIG_MESH */
205 int wpa_auth_for_each_sta(struct wpa_authenticator
*wpa_auth
,
206 int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
209 if (wpa_auth
->cb
->for_each_sta
== NULL
)
211 return wpa_auth
->cb
->for_each_sta(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
215 int wpa_auth_for_each_auth(struct wpa_authenticator
*wpa_auth
,
216 int (*cb
)(struct wpa_authenticator
*a
, void *ctx
),
219 if (wpa_auth
->cb
->for_each_auth
== NULL
)
221 return wpa_auth
->cb
->for_each_auth(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
225 void wpa_auth_logger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
226 logger_level level
, const char *txt
)
228 if (wpa_auth
->cb
->logger
== NULL
)
230 wpa_auth
->cb
->logger(wpa_auth
->cb_ctx
, addr
, level
, txt
);
234 void wpa_auth_vlogger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
235 logger_level level
, const char *fmt
, ...)
241 if (wpa_auth
->cb
->logger
== NULL
)
244 maxlen
= os_strlen(fmt
) + 100;
245 format
= os_malloc(maxlen
);
250 vsnprintf(format
, maxlen
, fmt
, ap
);
253 wpa_auth_logger(wpa_auth
, addr
, level
, format
);
259 static void wpa_sta_disconnect(struct wpa_authenticator
*wpa_auth
,
260 const u8
*addr
, u16 reason
)
262 if (wpa_auth
->cb
->disconnect
== NULL
)
264 wpa_printf(MSG_DEBUG
, "wpa_sta_disconnect STA " MACSTR
" (reason %u)",
265 MAC2STR(addr
), reason
);
266 wpa_auth
->cb
->disconnect(wpa_auth
->cb_ctx
, addr
, reason
);
271 static int wpa_channel_info(struct wpa_authenticator
*wpa_auth
,
272 struct wpa_channel_info
*ci
)
274 if (!wpa_auth
->cb
->channel_info
)
276 return wpa_auth
->cb
->channel_info(wpa_auth
->cb_ctx
, ci
);
278 #endif /* CONFIG_OCV */
281 static int wpa_auth_update_vlan(struct wpa_authenticator
*wpa_auth
,
282 const u8
*addr
, int vlan_id
)
284 if (!wpa_auth
->cb
->update_vlan
)
286 return wpa_auth
->cb
->update_vlan(wpa_auth
->cb_ctx
, addr
, vlan_id
);
290 static void wpa_rekey_gmk(void *eloop_ctx
, void *timeout_ctx
)
292 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
294 if (random_get_bytes(wpa_auth
->group
->GMK
, WPA_GMK_LEN
)) {
295 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
298 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "GMK rekeyd");
299 wpa_hexdump_key(MSG_DEBUG
, "GMK",
300 wpa_auth
->group
->GMK
, WPA_GMK_LEN
);
303 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
304 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
305 wpa_rekey_gmk
, wpa_auth
, NULL
);
310 static void wpa_rekey_gtk(void *eloop_ctx
, void *timeout_ctx
)
312 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
313 struct wpa_group
*group
, *next
;
315 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "rekeying GTK");
316 group
= wpa_auth
->group
;
318 wpa_group_get(wpa_auth
, group
);
320 group
->GTKReKey
= TRUE
;
322 group
->changed
= FALSE
;
323 wpa_group_sm_step(wpa_auth
, group
);
324 } while (group
->changed
);
327 wpa_group_put(wpa_auth
, group
);
331 if (wpa_auth
->conf
.wpa_group_rekey
) {
332 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
,
333 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
338 static void wpa_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
340 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
341 struct wpa_state_machine
*sm
= timeout_ctx
;
343 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "rekeying PTK");
344 wpa_request_new_ptk(sm
);
349 void wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine
*sm
)
351 if (sm
&& sm
->wpa_auth
->conf
.wpa_ptk_rekey
) {
352 wpa_printf(MSG_DEBUG
, "WPA: Start PTK rekeying timer for "
353 MACSTR
" (%d seconds)", MAC2STR(sm
->addr
),
354 sm
->wpa_auth
->conf
.wpa_ptk_rekey
);
355 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
356 eloop_register_timeout(sm
->wpa_auth
->conf
.wpa_ptk_rekey
, 0,
357 wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
362 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine
*sm
, void *ctx
)
364 if (sm
->pmksa
== ctx
)
370 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
373 struct wpa_authenticator
*wpa_auth
= ctx
;
374 wpa_auth_for_each_sta(wpa_auth
, wpa_auth_pmksa_clear_cb
, entry
);
378 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator
*wpa_auth
,
379 struct wpa_group
*group
)
381 u8 buf
[ETH_ALEN
+ 8 + sizeof(unsigned long)];
385 if (random_get_bytes(group
->GMK
, WPA_GMK_LEN
) < 0)
387 wpa_hexdump_key(MSG_DEBUG
, "GMK", group
->GMK
, WPA_GMK_LEN
);
390 * Counter = PRF-256(Random number, "Init Counter",
391 * Local MAC Address || Time)
393 os_memcpy(buf
, wpa_auth
->addr
, ETH_ALEN
);
394 wpa_get_ntp_timestamp(buf
+ ETH_ALEN
);
395 ptr
= (unsigned long) group
;
396 os_memcpy(buf
+ ETH_ALEN
+ 8, &ptr
, sizeof(ptr
));
398 os_memset(buf
+ ETH_ALEN
, 0xab, 8);
399 os_memset(buf
+ ETH_ALEN
+ 8, 0xcd, sizeof(ptr
));
400 #endif /* TEST_FUZZ */
401 if (random_get_bytes(rkey
, sizeof(rkey
)) < 0)
404 if (sha1_prf(rkey
, sizeof(rkey
), "Init Counter", buf
, sizeof(buf
),
405 group
->Counter
, WPA_NONCE_LEN
) < 0)
407 wpa_hexdump_key(MSG_DEBUG
, "Key Counter",
408 group
->Counter
, WPA_NONCE_LEN
);
414 static struct wpa_group
* wpa_group_init(struct wpa_authenticator
*wpa_auth
,
415 int vlan_id
, int delay_init
)
417 struct wpa_group
*group
;
419 group
= os_zalloc(sizeof(struct wpa_group
));
423 group
->GTKAuthenticator
= TRUE
;
424 group
->vlan_id
= vlan_id
;
425 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
427 if (random_pool_ready() != 1) {
428 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
429 "for secure operations - update keys later when "
430 "the first station connects");
434 * Set initial GMK/Counter value here. The actual values that will be
435 * used in negotiations will be set once the first station tries to
436 * connect. This allows more time for collecting additional randomness
437 * on embedded devices.
439 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0) {
440 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
448 wpa_printf(MSG_DEBUG
, "WPA: Delay group state machine start "
449 "until Beacon frames have been configured");
450 /* Initialization is completed in wpa_init_keys(). */
452 wpa_group_sm_step(wpa_auth
, group
);
453 group
->GInit
= FALSE
;
454 wpa_group_sm_step(wpa_auth
, group
);
462 * wpa_init - Initialize WPA authenticator
463 * @addr: Authenticator address
464 * @conf: Configuration for WPA authenticator
465 * @cb: Callback functions for WPA authenticator
466 * Returns: Pointer to WPA authenticator data or %NULL on failure
468 struct wpa_authenticator
* wpa_init(const u8
*addr
,
469 struct wpa_auth_config
*conf
,
470 const struct wpa_auth_callbacks
*cb
,
473 struct wpa_authenticator
*wpa_auth
;
475 wpa_auth
= os_zalloc(sizeof(struct wpa_authenticator
));
476 if (wpa_auth
== NULL
)
478 os_memcpy(wpa_auth
->addr
, addr
, ETH_ALEN
);
479 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
481 wpa_auth
->cb_ctx
= cb_ctx
;
483 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
484 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
489 wpa_auth
->group
= wpa_group_init(wpa_auth
, 0, 1);
490 if (wpa_auth
->group
== NULL
) {
491 os_free(wpa_auth
->wpa_ie
);
496 wpa_auth
->pmksa
= pmksa_cache_auth_init(wpa_auth_pmksa_free_cb
,
498 if (wpa_auth
->pmksa
== NULL
) {
499 wpa_printf(MSG_ERROR
, "PMKSA cache initialization failed.");
500 os_free(wpa_auth
->group
);
501 os_free(wpa_auth
->wpa_ie
);
506 #ifdef CONFIG_IEEE80211R_AP
507 wpa_auth
->ft_pmk_cache
= wpa_ft_pmk_cache_init();
508 if (wpa_auth
->ft_pmk_cache
== NULL
) {
509 wpa_printf(MSG_ERROR
, "FT PMK cache initialization failed.");
510 os_free(wpa_auth
->group
);
511 os_free(wpa_auth
->wpa_ie
);
512 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
516 #endif /* CONFIG_IEEE80211R_AP */
518 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
519 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
520 wpa_rekey_gmk
, wpa_auth
, NULL
);
523 if (wpa_auth
->conf
.wpa_group_rekey
) {
524 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
, 0,
525 wpa_rekey_gtk
, wpa_auth
, NULL
);
529 if (WPA_GET_BE32(conf
->ip_addr_start
)) {
530 int count
= WPA_GET_BE32(conf
->ip_addr_end
) -
531 WPA_GET_BE32(conf
->ip_addr_start
) + 1;
535 wpa_auth
->ip_pool
= bitfield_alloc(count
);
537 #endif /* CONFIG_P2P */
543 int wpa_init_keys(struct wpa_authenticator
*wpa_auth
)
545 struct wpa_group
*group
= wpa_auth
->group
;
547 wpa_printf(MSG_DEBUG
, "WPA: Start group state machine to set initial "
549 wpa_group_sm_step(wpa_auth
, group
);
550 group
->GInit
= FALSE
;
551 wpa_group_sm_step(wpa_auth
, group
);
552 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
559 * wpa_deinit - Deinitialize WPA authenticator
560 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
562 void wpa_deinit(struct wpa_authenticator
*wpa_auth
)
564 struct wpa_group
*group
, *prev
;
566 eloop_cancel_timeout(wpa_rekey_gmk
, wpa_auth
, NULL
);
567 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
569 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
571 #ifdef CONFIG_IEEE80211R_AP
572 wpa_ft_pmk_cache_deinit(wpa_auth
->ft_pmk_cache
);
573 wpa_auth
->ft_pmk_cache
= NULL
;
574 wpa_ft_deinit(wpa_auth
);
575 #endif /* CONFIG_IEEE80211R_AP */
578 bitfield_free(wpa_auth
->ip_pool
);
579 #endif /* CONFIG_P2P */
582 os_free(wpa_auth
->wpa_ie
);
584 group
= wpa_auth
->group
;
596 * wpa_reconfig - Update WPA authenticator configuration
597 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
598 * @conf: Configuration for WPA authenticator
600 int wpa_reconfig(struct wpa_authenticator
*wpa_auth
,
601 struct wpa_auth_config
*conf
)
603 struct wpa_group
*group
;
604 if (wpa_auth
== NULL
)
607 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
608 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
609 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
614 * Reinitialize GTK to make sure it is suitable for the new
617 group
= wpa_auth
->group
;
618 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
620 wpa_group_sm_step(wpa_auth
, group
);
621 group
->GInit
= FALSE
;
622 wpa_group_sm_step(wpa_auth
, group
);
628 struct wpa_state_machine
*
629 wpa_auth_sta_init(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
630 const u8
*p2p_dev_addr
)
632 struct wpa_state_machine
*sm
;
634 if (wpa_auth
->group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
637 sm
= os_zalloc(sizeof(struct wpa_state_machine
));
640 os_memcpy(sm
->addr
, addr
, ETH_ALEN
);
642 os_memcpy(sm
->p2p_dev_addr
, p2p_dev_addr
, ETH_ALEN
);
644 sm
->wpa_auth
= wpa_auth
;
645 sm
->group
= wpa_auth
->group
;
646 wpa_group_get(sm
->wpa_auth
, sm
->group
);
652 int wpa_auth_sta_associated(struct wpa_authenticator
*wpa_auth
,
653 struct wpa_state_machine
*sm
)
655 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
658 #ifdef CONFIG_IEEE80211R_AP
659 if (sm
->ft_completed
) {
660 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
661 "FT authentication already completed - do not "
662 "start 4-way handshake");
663 /* Go to PTKINITDONE state to allow GTK rekeying */
664 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
668 #endif /* CONFIG_IEEE80211R_AP */
671 if (sm
->fils_completed
) {
672 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
673 "FILS authentication already completed - do not start 4-way handshake");
674 /* Go to PTKINITDONE state to allow GTK rekeying */
675 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
679 #endif /* CONFIG_FILS */
682 os_memset(&sm
->key_replay
, 0, sizeof(sm
->key_replay
));
683 sm
->ReAuthenticationRequest
= TRUE
;
684 return wpa_sm_step(sm
);
687 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
688 "start authentication");
692 if (wpa_sm_step(sm
) == 1)
693 return 1; /* should not really happen */
695 sm
->AuthenticationRequest
= TRUE
;
696 return wpa_sm_step(sm
);
700 void wpa_auth_sta_no_wpa(struct wpa_state_machine
*sm
)
702 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
703 * reassociates back to the same AP while the previous entry for the
704 * STA has not yet been removed. */
708 sm
->wpa_key_mgmt
= 0;
712 static void wpa_free_sta_sm(struct wpa_state_machine
*sm
)
715 if (WPA_GET_BE32(sm
->ip_addr
)) {
717 wpa_printf(MSG_DEBUG
, "P2P: Free assigned IP "
718 "address %u.%u.%u.%u from " MACSTR
,
719 sm
->ip_addr
[0], sm
->ip_addr
[1],
720 sm
->ip_addr
[2], sm
->ip_addr
[3],
722 start
= WPA_GET_BE32(sm
->wpa_auth
->conf
.ip_addr_start
);
723 bitfield_clear(sm
->wpa_auth
->ip_pool
,
724 WPA_GET_BE32(sm
->ip_addr
) - start
);
726 #endif /* CONFIG_P2P */
727 if (sm
->GUpdateStationKeys
) {
728 sm
->group
->GKeyDoneStations
--;
729 sm
->GUpdateStationKeys
= FALSE
;
731 #ifdef CONFIG_IEEE80211R_AP
732 os_free(sm
->assoc_resp_ftie
);
733 wpabuf_free(sm
->ft_pending_req_ies
);
734 #endif /* CONFIG_IEEE80211R_AP */
735 os_free(sm
->last_rx_eapol_key
);
738 wpa_group_put(sm
->wpa_auth
, sm
->group
);
740 wpabuf_clear_free(sm
->dpp_z
);
741 #endif /* CONFIG_DPP2 */
742 bin_clear_free(sm
, sizeof(*sm
));
746 void wpa_auth_sta_deinit(struct wpa_state_machine
*sm
)
751 if (sm
->wpa_auth
->conf
.wpa_strict_rekey
&& sm
->has_GTK
) {
752 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
753 "strict rekeying - force GTK rekey since STA "
755 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk
,
756 sm
->wpa_auth
, NULL
) == -1)
757 eloop_register_timeout(0, 500000, wpa_rekey_gtk
, sm
->wpa_auth
,
761 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
762 sm
->pending_1_of_4_timeout
= 0;
763 eloop_cancel_timeout(wpa_sm_call_step
, sm
, NULL
);
764 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
765 #ifdef CONFIG_IEEE80211R_AP
766 wpa_ft_sta_deinit(sm
);
767 #endif /* CONFIG_IEEE80211R_AP */
768 if (sm
->in_step_loop
) {
769 /* Must not free state machine while wpa_sm_step() is running.
770 * Freeing will be completed in the end of wpa_sm_step(). */
771 wpa_printf(MSG_DEBUG
, "WPA: Registering pending STA state "
772 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
773 sm
->pending_deinit
= 1;
779 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
)
784 if (sm
->wpa_auth
->conf
.wpa_deny_ptk0_rekey
) {
786 "WPA: PTK0 rekey not allowed, disconnect " MACSTR
,
788 sm
->Disconnect
= TRUE
;
789 /* Try to encourage the STA to reconnect */
790 sm
->disconnect_reason
=
791 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA
;
793 sm
->PTKRequest
= TRUE
;
799 static int wpa_replay_counter_valid(struct wpa_key_replay_counter
*ctr
,
800 const u8
*replay_counter
)
803 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
806 if (os_memcmp(replay_counter
, ctr
[i
].counter
,
807 WPA_REPLAY_COUNTER_LEN
) == 0)
814 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter
*ctr
,
815 const u8
*replay_counter
)
818 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
820 (replay_counter
== NULL
||
821 os_memcmp(replay_counter
, ctr
[i
].counter
,
822 WPA_REPLAY_COUNTER_LEN
) == 0))
823 ctr
[i
].valid
= FALSE
;
828 #ifdef CONFIG_IEEE80211R_AP
829 static int ft_check_msg_2_of_4(struct wpa_authenticator
*wpa_auth
,
830 struct wpa_state_machine
*sm
,
831 struct wpa_eapol_ie_parse
*kde
)
833 struct wpa_ie_data ie
;
834 struct rsn_mdie
*mdie
;
836 if (wpa_parse_wpa_ie_rsn(kde
->rsn_ie
, kde
->rsn_ie_len
, &ie
) < 0 ||
837 ie
.num_pmkid
!= 1 || ie
.pmkid
== NULL
) {
838 wpa_printf(MSG_DEBUG
, "FT: No PMKR1Name in "
839 "FT 4-way handshake message 2/4");
843 os_memcpy(sm
->sup_pmk_r1_name
, ie
.pmkid
, PMKID_LEN
);
844 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Supplicant",
845 sm
->sup_pmk_r1_name
, PMKID_LEN
);
847 if (!kde
->mdie
|| !kde
->ftie
) {
848 wpa_printf(MSG_DEBUG
, "FT: No %s in FT 4-way handshake "
849 "message 2/4", kde
->mdie
? "FTIE" : "MDIE");
853 mdie
= (struct rsn_mdie
*) (kde
->mdie
+ 2);
854 if (kde
->mdie
[1] < sizeof(struct rsn_mdie
) ||
855 os_memcmp(wpa_auth
->conf
.mobility_domain
, mdie
->mobility_domain
,
856 MOBILITY_DOMAIN_ID_LEN
) != 0) {
857 wpa_printf(MSG_DEBUG
, "FT: MDIE mismatch");
861 if (sm
->assoc_resp_ftie
&&
862 (kde
->ftie
[1] != sm
->assoc_resp_ftie
[1] ||
863 os_memcmp(kde
->ftie
, sm
->assoc_resp_ftie
,
864 2 + sm
->assoc_resp_ftie
[1]) != 0)) {
865 wpa_printf(MSG_DEBUG
, "FT: FTIE mismatch");
866 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 2/4",
867 kde
->ftie
, kde
->ftie_len
);
868 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)AssocResp",
869 sm
->assoc_resp_ftie
, 2 + sm
->assoc_resp_ftie
[1]);
875 #endif /* CONFIG_IEEE80211R_AP */
878 static int wpa_receive_error_report(struct wpa_authenticator
*wpa_auth
,
879 struct wpa_state_machine
*sm
, int group
)
881 /* Supplicant reported a Michael MIC error */
882 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
883 "received EAPOL-Key Error Request "
884 "(STA detected Michael MIC failure (group=%d))",
887 if (group
&& wpa_auth
->conf
.wpa_group
!= WPA_CIPHER_TKIP
) {
888 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
889 "ignore Michael MIC failure report since "
890 "group cipher is not TKIP");
891 } else if (!group
&& sm
->pairwise
!= WPA_CIPHER_TKIP
) {
892 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
893 "ignore Michael MIC failure report since "
894 "pairwise cipher is not TKIP");
896 if (wpa_auth_mic_failure_report(wpa_auth
, sm
->addr
) > 0)
897 return 1; /* STA entry was removed */
898 sm
->dot11RSNAStatsTKIPRemoteMICFailures
++;
899 wpa_auth
->dot11RSNAStatsTKIPRemoteMICFailures
++;
903 * Error report is not a request for a new key handshake, but since
904 * Authenticator may do it, let's change the keys now anyway.
906 wpa_request_new_ptk(sm
);
911 static int wpa_try_alt_snonce(struct wpa_state_machine
*sm
, u8
*data
,
916 const u8
*pmk
= NULL
;
920 os_memset(&PTK
, 0, sizeof(PTK
));
922 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
923 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
924 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
925 sm
->p2p_dev_addr
, pmk
, &pmk_len
,
929 #ifdef CONFIG_IEEE80211R_AP
930 if (wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
931 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
932 sm
->xxkey_len
= pmk_len
;
934 #endif /* CONFIG_IEEE80211R_AP */
937 pmk_len
= sm
->pmk_len
;
940 if (wpa_derive_ptk(sm
, sm
->alt_SNonce
, pmk
, pmk_len
, &PTK
, 0) <
944 if (wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
945 data
, data_len
) == 0) {
946 if (sm
->PMK
!= pmk
) {
947 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
948 sm
->pmk_len
= pmk_len
;
954 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
955 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
))
960 wpa_printf(MSG_DEBUG
,
961 "WPA: Earlier SNonce did not result in matching MIC");
965 wpa_printf(MSG_DEBUG
,
966 "WPA: Earlier SNonce resulted in matching MIC");
967 sm
->alt_snonce_valid
= 0;
969 if (vlan_id
&& wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
970 wpa_auth_update_vlan(sm
->wpa_auth
, sm
->addr
, vlan_id
) < 0)
973 os_memcpy(sm
->SNonce
, sm
->alt_SNonce
, WPA_NONCE_LEN
);
974 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
975 forced_memzero(&PTK
, sizeof(PTK
));
976 sm
->PTK_valid
= TRUE
;
982 void wpa_receive(struct wpa_authenticator
*wpa_auth
,
983 struct wpa_state_machine
*sm
,
984 u8
*data
, size_t data_len
)
986 struct ieee802_1x_hdr
*hdr
;
987 struct wpa_eapol_key
*key
;
988 u16 key_info
, key_data_length
;
989 enum { PAIRWISE_2
, PAIRWISE_4
, GROUP_2
, REQUEST
} msg
;
991 struct wpa_eapol_ie_parse kde
;
993 size_t keyhdrlen
, mic_len
;
996 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
998 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL data", data
, data_len
);
1000 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
1001 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
1003 if (data_len
< sizeof(*hdr
) + keyhdrlen
) {
1004 wpa_printf(MSG_DEBUG
, "WPA: Ignore too short EAPOL-Key frame");
1008 hdr
= (struct ieee802_1x_hdr
*) data
;
1009 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1010 mic
= (u8
*) (key
+ 1);
1011 key_info
= WPA_GET_BE16(key
->key_info
);
1012 key_data
= mic
+ mic_len
+ 2;
1013 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
1014 wpa_printf(MSG_DEBUG
, "WPA: Received EAPOL-Key from " MACSTR
1015 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
1016 MAC2STR(sm
->addr
), key_info
, key
->type
,
1017 (unsigned int) mic_len
, key_data_length
);
1018 wpa_hexdump(MSG_MSGDUMP
,
1019 "WPA: EAPOL-Key header (ending before Key MIC)",
1021 wpa_hexdump(MSG_MSGDUMP
, "WPA: EAPOL-Key Key MIC",
1023 if (key_data_length
> data_len
- sizeof(*hdr
) - keyhdrlen
) {
1024 wpa_printf(MSG_INFO
, "WPA: Invalid EAPOL-Key frame - "
1025 "key_data overflow (%d > %lu)",
1027 (unsigned long) (data_len
- sizeof(*hdr
) -
1032 if (sm
->wpa
== WPA_VERSION_WPA2
) {
1033 if (key
->type
== EAPOL_KEY_TYPE_WPA
) {
1035 * Some deployed station implementations seem to send
1036 * msg 4/4 with incorrect type value in WPA2 mode.
1038 wpa_printf(MSG_DEBUG
, "Workaround: Allow EAPOL-Key "
1039 "with unexpected WPA type in RSN mode");
1040 } else if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
1041 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
1042 "unexpected type %d in RSN mode",
1047 if (key
->type
!= EAPOL_KEY_TYPE_WPA
) {
1048 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
1049 "unexpected type %d in WPA mode",
1055 wpa_hexdump(MSG_DEBUG
, "WPA: Received Key Nonce", key
->key_nonce
,
1057 wpa_hexdump(MSG_DEBUG
, "WPA: Received Replay Counter",
1058 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1060 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1063 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
1064 wpa_printf(MSG_DEBUG
, "WPA: Ignore SMK message");
1068 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1071 } else if (!(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
1073 msgtxt
= "2/2 Group";
1074 } else if (key_data_length
== 0 ||
1075 (mic_len
== 0 && (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) &&
1076 key_data_length
== AES_BLOCK_SIZE
)) {
1078 msgtxt
= "4/4 Pairwise";
1081 msgtxt
= "2/4 Pairwise";
1084 if (msg
== REQUEST
|| msg
== PAIRWISE_2
|| msg
== PAIRWISE_4
||
1086 u16 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
1087 if (sm
->pairwise
== WPA_CIPHER_CCMP
||
1088 sm
->pairwise
== WPA_CIPHER_GCMP
) {
1089 if (wpa_use_cmac(sm
->wpa_key_mgmt
) &&
1090 !wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1091 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1092 wpa_auth_logger(wpa_auth
, sm
->addr
,
1094 "advertised support for "
1095 "AES-128-CMAC, but did not "
1100 if (!wpa_use_cmac(sm
->wpa_key_mgmt
) &&
1101 !wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1102 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1103 wpa_auth_logger(wpa_auth
, sm
->addr
,
1105 "did not use HMAC-SHA1-AES "
1111 if (wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1112 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
1113 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1114 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1119 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1120 if (sm
->req_replay_counter_used
&&
1121 os_memcmp(key
->replay_counter
, sm
->req_replay_counter
,
1122 WPA_REPLAY_COUNTER_LEN
) <= 0) {
1123 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1124 "received EAPOL-Key request with "
1125 "replayed counter");
1130 if (!(key_info
& WPA_KEY_INFO_REQUEST
) &&
1131 !wpa_replay_counter_valid(sm
->key_replay
, key
->replay_counter
)) {
1134 if (msg
== PAIRWISE_2
&&
1135 wpa_replay_counter_valid(sm
->prev_key_replay
,
1136 key
->replay_counter
) &&
1137 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1138 os_memcmp(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0)
1141 * Some supplicant implementations (e.g., Windows XP
1142 * WZC) update SNonce for each EAPOL-Key 2/4. This
1143 * breaks the workaround on accepting any of the
1144 * pending requests, so allow the SNonce to be updated
1145 * even if we have already sent out EAPOL-Key 3/4.
1147 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1148 "Process SNonce update from STA "
1149 "based on retransmitted EAPOL-Key "
1151 sm
->update_snonce
= 1;
1152 os_memcpy(sm
->alt_SNonce
, sm
->SNonce
, WPA_NONCE_LEN
);
1153 sm
->alt_snonce_valid
= TRUE
;
1154 os_memcpy(sm
->alt_replay_counter
,
1155 sm
->key_replay
[0].counter
,
1156 WPA_REPLAY_COUNTER_LEN
);
1157 goto continue_processing
;
1160 if (msg
== PAIRWISE_4
&& sm
->alt_snonce_valid
&&
1161 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1162 os_memcmp(key
->replay_counter
, sm
->alt_replay_counter
,
1163 WPA_REPLAY_COUNTER_LEN
) == 0) {
1165 * Supplicant may still be using the old SNonce since
1166 * there was two EAPOL-Key 2/4 messages and they had
1167 * different SNonce values.
1169 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1170 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1171 goto continue_processing
;
1174 if (msg
== PAIRWISE_2
&&
1175 wpa_replay_counter_valid(sm
->prev_key_replay
,
1176 key
->replay_counter
) &&
1177 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
) {
1178 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1179 "ignore retransmitted EAPOL-Key %s - "
1180 "SNonce did not change", msgtxt
);
1182 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1183 "received EAPOL-Key %s with "
1184 "unexpected replay counter", msgtxt
);
1186 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
1187 if (!sm
->key_replay
[i
].valid
)
1189 wpa_hexdump(MSG_DEBUG
, "pending replay counter",
1190 sm
->key_replay
[i
].counter
,
1191 WPA_REPLAY_COUNTER_LEN
);
1193 wpa_hexdump(MSG_DEBUG
, "received replay counter",
1194 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1198 continue_processing
:
1200 if (sm
->wpa
== WPA_VERSION_WPA2
&& mic_len
== 0 &&
1201 !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1202 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1203 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1206 #endif /* CONFIG_FILS */
1210 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKSTART
&&
1211 sm
->wpa_ptk_state
!= WPA_PTK_PTKCALCNEGOTIATING
&&
1212 (!sm
->update_snonce
||
1213 sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
)) {
1214 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1215 "received EAPOL-Key msg 2/4 in "
1216 "invalid state (%d) - dropped",
1220 random_add_randomness(key
->key_nonce
, WPA_NONCE_LEN
);
1221 if (sm
->group
->reject_4way_hs_for_entropy
) {
1223 * The system did not have enough entropy to generate
1224 * strong random numbers. Reject the first 4-way
1225 * handshake(s) and collect some entropy based on the
1226 * information from it. Once enough entropy is
1227 * available, the next atempt will trigger GMK/Key
1228 * Counter update and the station will be allowed to
1231 wpa_printf(MSG_DEBUG
, "WPA: Reject 4-way handshake to "
1232 "collect more entropy for random number "
1234 random_mark_pool_ready();
1235 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
1236 WLAN_REASON_PREV_AUTH_NOT_VALID
);
1241 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
||
1243 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1244 "received EAPOL-Key msg 4/4 in "
1245 "invalid state (%d) - dropped",
1251 if (sm
->wpa_ptk_group_state
!= WPA_PTK_GROUP_REKEYNEGOTIATING
1252 || !sm
->PTK_valid
) {
1253 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1254 "received EAPOL-Key msg 2/2 in "
1255 "invalid state (%d) - dropped",
1256 sm
->wpa_ptk_group_state
);
1264 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1265 "received EAPOL-Key frame (%s)", msgtxt
);
1267 if (key_info
& WPA_KEY_INFO_ACK
) {
1268 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1269 "received invalid EAPOL-Key: Key Ack set");
1273 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1274 !(key_info
& WPA_KEY_INFO_MIC
)) {
1275 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1276 "received invalid EAPOL-Key: Key MIC not set");
1281 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1282 (key_info
& WPA_KEY_INFO_MIC
)) {
1283 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1284 "received invalid EAPOL-Key: Key MIC set");
1287 #endif /* CONFIG_FILS */
1289 sm
->MICVerified
= FALSE
;
1290 if (sm
->PTK_valid
&& !sm
->update_snonce
) {
1292 wpa_verify_key_mic(sm
->wpa_key_mgmt
, sm
->pmk_len
, &sm
->PTK
,
1294 (msg
!= PAIRWISE_4
|| !sm
->alt_snonce_valid
||
1295 wpa_try_alt_snonce(sm
, data
, data_len
))) {
1296 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1297 "received EAPOL-Key with invalid MIC");
1299 wpa_printf(MSG_INFO
,
1300 "TEST: Ignore Key MIC failure for fuzz testing");
1302 #endif /* TEST_FUZZ */
1307 wpa_aead_decrypt(sm
, &sm
->PTK
, data
, data_len
,
1308 &key_data_length
) < 0) {
1309 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1310 "received EAPOL-Key with invalid MIC");
1312 wpa_printf(MSG_INFO
,
1313 "TEST: Ignore Key MIC failure for fuzz testing");
1315 #endif /* TEST_FUZZ */
1318 #endif /* CONFIG_FILS */
1321 #endif /* TEST_FUZZ */
1322 sm
->MICVerified
= TRUE
;
1323 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
1324 sm
->pending_1_of_4_timeout
= 0;
1327 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1328 if (sm
->MICVerified
) {
1329 sm
->req_replay_counter_used
= 1;
1330 os_memcpy(sm
->req_replay_counter
, key
->replay_counter
,
1331 WPA_REPLAY_COUNTER_LEN
);
1333 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1334 "received EAPOL-Key request with "
1340 * TODO: should decrypt key data field if encryption was used;
1341 * even though MAC address KDE is not normally encrypted,
1342 * supplicant is allowed to encrypt it.
1344 if (key_info
& WPA_KEY_INFO_ERROR
) {
1345 if (wpa_receive_error_report(
1347 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) > 0)
1348 return; /* STA entry was removed */
1349 } else if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
1350 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1351 "received EAPOL-Key Request for new "
1353 wpa_request_new_ptk(sm
);
1354 } else if (key_data_length
> 0 &&
1355 wpa_parse_kde_ies(key_data
, key_data_length
,
1359 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1360 "received EAPOL-Key Request for GTK "
1362 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
1363 wpa_rekey_gtk(wpa_auth
, NULL
);
1366 /* Do not allow the same key replay counter to be reused. */
1367 wpa_replay_counter_mark_invalid(sm
->key_replay
,
1368 key
->replay_counter
);
1370 if (msg
== PAIRWISE_2
) {
1372 * Maintain a copy of the pending EAPOL-Key frames in
1373 * case the EAPOL-Key frame was retransmitted. This is
1374 * needed to allow EAPOL-Key msg 2/4 reply to another
1375 * pending msg 1/4 to update the SNonce to work around
1376 * unexpected supplicant behavior.
1378 os_memcpy(sm
->prev_key_replay
, sm
->key_replay
,
1379 sizeof(sm
->key_replay
));
1381 os_memset(sm
->prev_key_replay
, 0,
1382 sizeof(sm
->prev_key_replay
));
1386 * Make sure old valid counters are not accepted anymore and
1387 * do not get copied again.
1389 wpa_replay_counter_mark_invalid(sm
->key_replay
, NULL
);
1392 os_free(sm
->last_rx_eapol_key
);
1393 sm
->last_rx_eapol_key
= os_memdup(data
, data_len
);
1394 if (sm
->last_rx_eapol_key
== NULL
)
1396 sm
->last_rx_eapol_key_len
= data_len
;
1398 sm
->rx_eapol_key_secure
= !!(key_info
& WPA_KEY_INFO_SECURE
);
1399 sm
->EAPOLKeyReceived
= TRUE
;
1400 sm
->EAPOLKeyPairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1401 sm
->EAPOLKeyRequest
= !!(key_info
& WPA_KEY_INFO_REQUEST
);
1402 os_memcpy(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
);
1407 static int wpa_gmk_to_gtk(const u8
*gmk
, const char *label
, const u8
*addr
,
1408 const u8
*gnonce
, u8
*gtk
, size_t gtk_len
)
1410 u8 data
[ETH_ALEN
+ WPA_NONCE_LEN
+ 8 + WPA_GTK_MAX_LEN
];
1414 /* GTK = PRF-X(GMK, "Group key expansion",
1415 * AA || GNonce || Time || random data)
1416 * The example described in the IEEE 802.11 standard uses only AA and
1417 * GNonce as inputs here. Add some more entropy since this derivation
1418 * is done only at the Authenticator and as such, does not need to be
1421 os_memset(data
, 0, sizeof(data
));
1422 os_memcpy(data
, addr
, ETH_ALEN
);
1423 os_memcpy(data
+ ETH_ALEN
, gnonce
, WPA_NONCE_LEN
);
1424 pos
= data
+ ETH_ALEN
+ WPA_NONCE_LEN
;
1425 wpa_get_ntp_timestamp(pos
);
1427 os_memset(pos
, 0xef, 8);
1428 #endif /* TEST_FUZZ */
1430 if (random_get_bytes(pos
, gtk_len
) < 0)
1433 #ifdef CONFIG_SHA384
1434 if (sha384_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1437 #else /* CONFIG_SHA384 */
1438 #ifdef CONFIG_SHA256
1439 if (sha256_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1442 #else /* CONFIG_SHA256 */
1443 if (sha1_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1446 #endif /* CONFIG_SHA256 */
1447 #endif /* CONFIG_SHA384 */
1449 forced_memzero(data
, sizeof(data
));
1455 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
)
1457 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1458 struct wpa_state_machine
*sm
= timeout_ctx
;
1460 sm
->pending_1_of_4_timeout
= 0;
1461 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "EAPOL-Key timeout");
1462 sm
->TimeoutEvt
= TRUE
;
1467 void __wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1468 struct wpa_state_machine
*sm
, int key_info
,
1469 const u8
*key_rsc
, const u8
*nonce
,
1470 const u8
*kde
, size_t kde_len
,
1471 int keyidx
, int encr
, int force_version
)
1473 struct ieee802_1x_hdr
*hdr
;
1474 struct wpa_eapol_key
*key
;
1475 size_t len
, mic_len
, keyhdrlen
;
1477 int key_data_len
, pad_len
= 0;
1479 int version
, pairwise
;
1481 u8
*key_mic
, *key_data
;
1483 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
1484 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
1486 len
= sizeof(struct ieee802_1x_hdr
) + keyhdrlen
;
1489 version
= force_version
;
1490 else if (wpa_use_akm_defined(sm
->wpa_key_mgmt
))
1491 version
= WPA_KEY_INFO_TYPE_AKM_DEFINED
;
1492 else if (wpa_use_cmac(sm
->wpa_key_mgmt
))
1493 version
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
1494 else if (sm
->pairwise
!= WPA_CIPHER_TKIP
)
1495 version
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
1497 version
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
1499 pairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1501 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1502 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1505 (key_info
& WPA_KEY_INFO_SECURE
) ? 1 : 0,
1506 (key_info
& WPA_KEY_INFO_MIC
) ? 1 : 0,
1507 (key_info
& WPA_KEY_INFO_ACK
) ? 1 : 0,
1508 (key_info
& WPA_KEY_INFO_INSTALL
) ? 1 : 0,
1509 pairwise
, (unsigned long) kde_len
, keyidx
, encr
);
1511 key_data_len
= kde_len
;
1513 if ((version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1514 wpa_use_aes_key_wrap(sm
->wpa_key_mgmt
) ||
1515 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) && encr
) {
1516 pad_len
= key_data_len
% 8;
1518 pad_len
= 8 - pad_len
;
1519 key_data_len
+= pad_len
+ 8;
1522 len
+= key_data_len
;
1523 if (!mic_len
&& encr
)
1524 len
+= AES_BLOCK_SIZE
;
1526 hdr
= os_zalloc(len
);
1529 hdr
->version
= wpa_auth
->conf
.eapol_version
;
1530 hdr
->type
= IEEE802_1X_TYPE_EAPOL_KEY
;
1531 hdr
->length
= host_to_be16(len
- sizeof(*hdr
));
1532 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1533 key_mic
= (u8
*) (key
+ 1);
1534 key_data
= ((u8
*) (hdr
+ 1)) + keyhdrlen
;
1536 key
->type
= sm
->wpa
== WPA_VERSION_WPA2
?
1537 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1538 key_info
|= version
;
1539 if (encr
&& sm
->wpa
== WPA_VERSION_WPA2
)
1540 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1541 if (sm
->wpa
!= WPA_VERSION_WPA2
)
1542 key_info
|= keyidx
<< WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1543 WPA_PUT_BE16(key
->key_info
, key_info
);
1545 alg
= pairwise
? sm
->pairwise
: wpa_auth
->conf
.wpa_group
;
1546 if (sm
->wpa
== WPA_VERSION_WPA2
&& !pairwise
)
1547 WPA_PUT_BE16(key
->key_length
, 0);
1549 WPA_PUT_BE16(key
->key_length
, wpa_cipher_key_len(alg
));
1551 for (i
= RSNA_MAX_EAPOL_RETRIES
- 1; i
> 0; i
--) {
1552 sm
->key_replay
[i
].valid
= sm
->key_replay
[i
- 1].valid
;
1553 os_memcpy(sm
->key_replay
[i
].counter
,
1554 sm
->key_replay
[i
- 1].counter
,
1555 WPA_REPLAY_COUNTER_LEN
);
1557 inc_byte_array(sm
->key_replay
[0].counter
, WPA_REPLAY_COUNTER_LEN
);
1558 os_memcpy(key
->replay_counter
, sm
->key_replay
[0].counter
,
1559 WPA_REPLAY_COUNTER_LEN
);
1560 wpa_hexdump(MSG_DEBUG
, "WPA: Replay Counter",
1561 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1562 sm
->key_replay
[0].valid
= TRUE
;
1565 os_memcpy(key
->key_nonce
, nonce
, WPA_NONCE_LEN
);
1568 os_memcpy(key
->key_rsc
, key_rsc
, WPA_KEY_RSC_LEN
);
1571 os_memcpy(key_data
, kde
, kde_len
);
1572 WPA_PUT_BE16(key_mic
+ mic_len
, kde_len
);
1574 } else if (!mic_len
&& kde
) {
1578 WPA_PUT_BE16(key_mic
, AES_BLOCK_SIZE
+ kde_len
);
1579 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1582 wpa_hexdump_key(MSG_DEBUG
, "WPA: KEK",
1583 sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1584 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1585 * to Key Data (exclusive). */
1586 aad
[0] = (u8
*) hdr
;
1587 aad_len
[0] = key_mic
+ 2 - (u8
*) hdr
;
1588 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, kde
, kde_len
,
1589 1, aad
, aad_len
, key_mic
+ 2) < 0) {
1590 wpa_printf(MSG_DEBUG
, "WPA: AES-SIV encryption failed");
1594 wpa_hexdump(MSG_DEBUG
, "WPA: Encrypted Key Data from SIV",
1595 key_mic
+ 2, AES_BLOCK_SIZE
+ kde_len
);
1596 #endif /* CONFIG_FILS */
1597 } else if (encr
&& kde
) {
1598 buf
= os_zalloc(key_data_len
);
1604 os_memcpy(pos
, kde
, kde_len
);
1610 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1612 if (version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1613 wpa_use_aes_key_wrap(sm
->wpa_key_mgmt
) ||
1614 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1615 wpa_printf(MSG_DEBUG
,
1616 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1617 (unsigned int) sm
->PTK
.kek_len
);
1618 if (aes_wrap(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
1619 (key_data_len
- 8) / 8, buf
, key_data
)) {
1624 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1625 #ifndef CONFIG_NO_RC4
1626 } else if (sm
->PTK
.kek_len
== 16) {
1629 wpa_printf(MSG_DEBUG
,
1630 "WPA: Encrypt Key Data using RC4");
1631 os_memcpy(key
->key_iv
,
1632 sm
->group
->Counter
+ WPA_NONCE_LEN
- 16, 16);
1633 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1634 os_memcpy(ek
, key
->key_iv
, 16);
1635 os_memcpy(ek
+ 16, sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1636 os_memcpy(key_data
, buf
, key_data_len
);
1637 rc4_skip(ek
, 32, 256, key_data
, key_data_len
);
1638 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1639 #endif /* CONFIG_NO_RC4 */
1648 if (key_info
& WPA_KEY_INFO_MIC
) {
1649 if (!sm
->PTK_valid
|| !mic_len
) {
1650 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1651 "PTK not valid when sending EAPOL-Key "
1657 if (wpa_eapol_key_mic(sm
->PTK
.kck
, sm
->PTK
.kck_len
,
1658 sm
->wpa_key_mgmt
, version
,
1659 (u8
*) hdr
, len
, key_mic
) < 0) {
1663 #ifdef CONFIG_TESTING_OPTIONS
1665 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
> 0.0 &&
1667 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
) {
1668 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1669 "Corrupting group EAPOL-Key Key MIC");
1672 #endif /* CONFIG_TESTING_OPTIONS */
1675 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_inc_EapolFramesTx
,
1677 wpa_auth_send_eapol(wpa_auth
, sm
->addr
, (u8
*) hdr
, len
,
1683 static void wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1684 struct wpa_state_machine
*sm
, int key_info
,
1685 const u8
*key_rsc
, const u8
*nonce
,
1686 const u8
*kde
, size_t kde_len
,
1687 int keyidx
, int encr
)
1690 int pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
1696 __wpa_send_eapol(wpa_auth
, sm
, key_info
, key_rsc
, nonce
, kde
, kde_len
,
1699 ctr
= pairwise
? sm
->TimeoutCtr
: sm
->GTimeoutCtr
;
1700 if (ctr
== 1 && wpa_auth
->conf
.tx_status
)
1701 timeout_ms
= pairwise
? eapol_key_timeout_first
:
1702 eapol_key_timeout_first_group
;
1704 timeout_ms
= eapol_key_timeout_subseq
;
1705 if (wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
1706 (!pairwise
|| (key_info
& WPA_KEY_INFO_MIC
)))
1707 timeout_ms
= eapol_key_timeout_no_retrans
;
1708 if (pairwise
&& ctr
== 1 && !(key_info
& WPA_KEY_INFO_MIC
))
1709 sm
->pending_1_of_4_timeout
= 1;
1712 #endif /* TEST_FUZZ */
1713 wpa_printf(MSG_DEBUG
, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1714 "counter %u)", timeout_ms
, ctr
);
1715 eloop_register_timeout(timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000,
1716 wpa_send_eapol_timeout
, wpa_auth
, sm
);
1720 static int wpa_verify_key_mic(int akmp
, size_t pmk_len
, struct wpa_ptk
*PTK
,
1721 u8
*data
, size_t data_len
)
1723 struct ieee802_1x_hdr
*hdr
;
1724 struct wpa_eapol_key
*key
;
1727 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
], *mic_pos
;
1728 size_t mic_len
= wpa_mic_len(akmp
, pmk_len
);
1730 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
1733 hdr
= (struct ieee802_1x_hdr
*) data
;
1734 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1735 mic_pos
= (u8
*) (key
+ 1);
1736 key_info
= WPA_GET_BE16(key
->key_info
);
1737 os_memcpy(mic
, mic_pos
, mic_len
);
1738 os_memset(mic_pos
, 0, mic_len
);
1739 if (wpa_eapol_key_mic(PTK
->kck
, PTK
->kck_len
, akmp
,
1740 key_info
& WPA_KEY_INFO_TYPE_MASK
,
1741 data
, data_len
, mic_pos
) ||
1742 os_memcmp_const(mic
, mic_pos
, mic_len
) != 0)
1744 os_memcpy(mic_pos
, mic
, mic_len
);
1749 void wpa_remove_ptk(struct wpa_state_machine
*sm
)
1751 sm
->PTK_valid
= FALSE
;
1752 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1753 if (wpa_auth_set_key(sm
->wpa_auth
, 0, WPA_ALG_NONE
, sm
->addr
, 0, NULL
,
1754 0, KEY_FLAG_PAIRWISE
))
1755 wpa_printf(MSG_DEBUG
,
1756 "RSN: PTK removal from the driver failed");
1757 sm
->pairwise_set
= FALSE
;
1758 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1762 int wpa_auth_sm_event(struct wpa_state_machine
*sm
, enum wpa_event event
)
1769 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1770 "event %d notification", event
);
1775 /* PTKs are derived through AMPE */
1776 if (wpa_auth_start_ampe(sm
->wpa_auth
, sm
->addr
)) {
1781 #endif /* CONFIG_MESH */
1786 sm
->DeauthenticationRequest
= TRUE
;
1787 #ifdef CONFIG_IEEE80211R_AP
1788 os_memset(sm
->PMK
, 0, sizeof(sm
->PMK
));
1790 os_memset(sm
->xxkey
, 0, sizeof(sm
->xxkey
));
1792 os_memset(sm
->pmk_r1
, 0, sizeof(sm
->pmk_r1
));
1794 #endif /* CONFIG_IEEE80211R_AP */
1797 case WPA_REAUTH_EAPOL
:
1800 * When using WPS, we may end up here if the STA
1801 * manages to re-associate without the previous STA
1802 * entry getting removed. Consequently, we need to make
1803 * sure that the WPA state machines gets initialized
1804 * properly at this point.
1806 wpa_printf(MSG_DEBUG
, "WPA state machine had not been "
1807 "started - initialize now");
1810 if (wpa_sm_step(sm
) == 1)
1811 return 1; /* should not really happen */
1813 sm
->AuthenticationRequest
= TRUE
;
1815 } else if (sm
->wpa_auth
->conf
.wpa_deny_ptk0_rekey
) {
1816 wpa_printf(MSG_INFO
,
1817 "WPA: PTK0 rekey not allowed, disconnect "
1818 MACSTR
, MAC2STR(sm
->addr
));
1819 sm
->Disconnect
= TRUE
;
1820 /* Try to encourage the STA reconnect */
1821 sm
->disconnect_reason
=
1822 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA
;
1825 if (sm
->GUpdateStationKeys
) {
1827 * Reauthentication cancels the pending group key
1828 * update for this STA.
1830 sm
->group
->GKeyDoneStations
--;
1831 sm
->GUpdateStationKeys
= FALSE
;
1832 sm
->PtkGroupInit
= TRUE
;
1834 sm
->ReAuthenticationRequest
= TRUE
;
1837 #ifdef CONFIG_IEEE80211R_AP
1838 wpa_printf(MSG_DEBUG
, "FT: Retry PTK configuration "
1839 "after association");
1840 wpa_ft_install_ptk(sm
);
1842 /* Using FT protocol, not WPA auth state machine */
1843 sm
->ft_completed
= 1;
1844 wpa_auth_set_ptk_rekey_timer(sm
);
1846 #else /* CONFIG_IEEE80211R_AP */
1848 #endif /* CONFIG_IEEE80211R_AP */
1849 case WPA_ASSOC_FILS
:
1851 wpa_printf(MSG_DEBUG
,
1852 "FILS: TK configuration after association");
1854 sm
->fils_completed
= 1;
1856 #else /* CONFIG_FILS */
1858 #endif /* CONFIG_FILS */
1859 case WPA_DRV_STA_REMOVED
:
1860 sm
->tk_already_set
= FALSE
;
1864 #ifdef CONFIG_IEEE80211R_AP
1865 sm
->ft_completed
= 0;
1866 #endif /* CONFIG_IEEE80211R_AP */
1868 if (sm
->mgmt_frame_prot
&& event
== WPA_AUTH
)
1871 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1872 (event
== WPA_AUTH
|| event
== WPA_ASSOC
))
1874 #endif /* CONFIG_FILS */
1877 sm
->PTK_valid
= FALSE
;
1878 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1880 if (event
!= WPA_REAUTH_EAPOL
)
1884 if (sm
->in_step_loop
) {
1886 * wpa_sm_step() is already running - avoid recursive call to
1887 * it by making the existing loop process the new update.
1892 return wpa_sm_step(sm
);
1896 SM_STATE(WPA_PTK
, INITIALIZE
)
1898 SM_ENTRY_MA(WPA_PTK
, INITIALIZE
, wpa_ptk
);
1900 /* Init flag is not cleared here, so avoid busy
1901 * loop by claiming nothing changed. */
1902 sm
->changed
= FALSE
;
1906 if (sm
->GUpdateStationKeys
)
1907 sm
->group
->GKeyDoneStations
--;
1908 sm
->GUpdateStationKeys
= FALSE
;
1909 if (sm
->wpa
== WPA_VERSION_WPA
)
1910 sm
->PInitAKeys
= FALSE
;
1911 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1912 * Local AA > Remote AA)) */) {
1915 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 0);
1917 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
, 0);
1919 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
1920 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1921 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
1922 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1923 WPA_EAPOL_authorized
, 0);
1928 SM_STATE(WPA_PTK
, DISCONNECT
)
1930 u16 reason
= sm
->disconnect_reason
;
1932 SM_ENTRY_MA(WPA_PTK
, DISCONNECT
, wpa_ptk
);
1933 sm
->Disconnect
= FALSE
;
1934 sm
->disconnect_reason
= 0;
1936 reason
= WLAN_REASON_PREV_AUTH_NOT_VALID
;
1937 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
, reason
);
1941 SM_STATE(WPA_PTK
, DISCONNECTED
)
1943 SM_ENTRY_MA(WPA_PTK
, DISCONNECTED
, wpa_ptk
);
1944 sm
->DeauthenticationRequest
= FALSE
;
1948 SM_STATE(WPA_PTK
, AUTHENTICATION
)
1950 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION
, wpa_ptk
);
1951 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1952 sm
->PTK_valid
= FALSE
;
1953 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portControl_Auto
,
1955 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 1);
1956 sm
->AuthenticationRequest
= FALSE
;
1960 static void wpa_group_ensure_init(struct wpa_authenticator
*wpa_auth
,
1961 struct wpa_group
*group
)
1963 if (group
->first_sta_seen
)
1966 * System has run bit further than at the time hostapd was started
1967 * potentially very early during boot up. This provides better chances
1968 * of collecting more randomness on embedded systems. Re-initialize the
1969 * GMK and Counter here to improve their strength if there was not
1970 * enough entropy available immediately after system startup.
1972 wpa_printf(MSG_DEBUG
, "WPA: Re-initialize GMK/Counter on first "
1974 if (random_pool_ready() != 1) {
1975 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
1976 "to proceed - reject first 4-way handshake");
1977 group
->reject_4way_hs_for_entropy
= TRUE
;
1979 group
->first_sta_seen
= TRUE
;
1980 group
->reject_4way_hs_for_entropy
= FALSE
;
1983 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0 ||
1984 wpa_gtk_update(wpa_auth
, group
) < 0 ||
1985 wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
1986 wpa_printf(MSG_INFO
, "WPA: GMK/GTK setup failed");
1987 group
->first_sta_seen
= FALSE
;
1988 group
->reject_4way_hs_for_entropy
= TRUE
;
1993 SM_STATE(WPA_PTK
, AUTHENTICATION2
)
1995 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION2
, wpa_ptk
);
1997 wpa_group_ensure_init(sm
->wpa_auth
, sm
->group
);
1998 sm
->ReAuthenticationRequest
= FALSE
;
2001 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
2002 * ambiguous. The Authenticator state machine uses a counter that is
2003 * incremented by one for each 4-way handshake. However, the security
2004 * analysis of 4-way handshake points out that unpredictable nonces
2005 * help in preventing precomputation attacks. Instead of the state
2006 * machine definition, use an unpredictable nonce value here to provide
2007 * stronger protection against potential precomputation attacks.
2009 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
2010 wpa_printf(MSG_ERROR
, "WPA: Failed to get random data for "
2012 sm
->Disconnect
= TRUE
;
2015 wpa_hexdump(MSG_DEBUG
, "WPA: Assign ANonce", sm
->ANonce
,
2017 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
2018 * logical place than INITIALIZE since AUTHENTICATION2 can be
2019 * re-entered on ReAuthenticationRequest without going through
2025 static int wpa_auth_sm_ptk_update(struct wpa_state_machine
*sm
)
2027 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
2028 wpa_printf(MSG_ERROR
,
2029 "WPA: Failed to get random data for ANonce");
2030 sm
->Disconnect
= TRUE
;
2033 wpa_hexdump(MSG_DEBUG
, "WPA: Assign new ANonce", sm
->ANonce
,
2040 SM_STATE(WPA_PTK
, INITPMK
)
2042 u8 msk
[2 * PMK_LEN
];
2043 size_t len
= 2 * PMK_LEN
;
2045 SM_ENTRY_MA(WPA_PTK
, INITPMK
, wpa_ptk
);
2046 #ifdef CONFIG_IEEE80211R_AP
2048 #endif /* CONFIG_IEEE80211R_AP */
2050 wpa_printf(MSG_DEBUG
, "WPA: PMK from PMKSA cache");
2051 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2052 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
2054 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
) {
2055 wpa_printf(MSG_DEBUG
,
2056 "DPP: No PMKSA cache entry for STA - reject connection");
2057 sm
->Disconnect
= TRUE
;
2058 sm
->disconnect_reason
= WLAN_REASON_INVALID_PMKID
;
2060 #endif /* CONFIG_DPP */
2061 } else if (wpa_auth_get_msk(sm
->wpa_auth
, sm
->addr
, msk
, &len
) == 0) {
2062 unsigned int pmk_len
;
2064 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
))
2065 pmk_len
= PMK_LEN_SUITE_B_192
;
2068 wpa_printf(MSG_DEBUG
, "WPA: PMK from EAPOL state machine "
2069 "(MSK len=%lu PMK len=%u)", (unsigned long) len
,
2071 if (len
< pmk_len
) {
2072 wpa_printf(MSG_DEBUG
,
2073 "WPA: MSK not long enough (%u) to create PMK (%u)",
2074 (unsigned int) len
, (unsigned int) pmk_len
);
2075 sm
->Disconnect
= TRUE
;
2078 os_memcpy(sm
->PMK
, msk
, pmk_len
);
2079 sm
->pmk_len
= pmk_len
;
2080 #ifdef CONFIG_IEEE80211R_AP
2081 if (len
>= 2 * PMK_LEN
) {
2082 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
2083 os_memcpy(sm
->xxkey
, msk
, SHA384_MAC_LEN
);
2084 sm
->xxkey_len
= SHA384_MAC_LEN
;
2086 os_memcpy(sm
->xxkey
, msk
+ PMK_LEN
, PMK_LEN
);
2087 sm
->xxkey_len
= PMK_LEN
;
2090 #endif /* CONFIG_IEEE80211R_AP */
2092 wpa_printf(MSG_DEBUG
, "WPA: Could not get PMK, get_msk: %p",
2093 sm
->wpa_auth
->cb
->get_msk
);
2094 sm
->Disconnect
= TRUE
;
2097 forced_memzero(msk
, sizeof(msk
));
2099 sm
->req_replay_counter_used
= 0;
2100 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
2101 * will break reauthentication since EAPOL state machines may not be
2102 * get into AUTHENTICATING state that clears keyRun before WPA state
2103 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2104 * state and takes PMK from the previously used AAA Key. This will
2105 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2106 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2107 * be good workaround for this issue. */
2108 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyRun
, 0);
2112 SM_STATE(WPA_PTK
, INITPSK
)
2117 SM_ENTRY_MA(WPA_PTK
, INITPSK
, wpa_ptk
);
2118 psk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
, NULL
,
2121 os_memcpy(sm
->PMK
, psk
, psk_len
);
2122 sm
->pmk_len
= psk_len
;
2123 #ifdef CONFIG_IEEE80211R_AP
2124 os_memcpy(sm
->xxkey
, psk
, PMK_LEN
);
2125 sm
->xxkey_len
= PMK_LEN
;
2126 #endif /* CONFIG_IEEE80211R_AP */
2129 if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
2130 wpa_printf(MSG_DEBUG
, "SAE: PMK from PMKSA cache");
2131 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2132 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
2133 #ifdef CONFIG_IEEE80211R_AP
2134 os_memcpy(sm
->xxkey
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2135 sm
->xxkey_len
= sm
->pmksa
->pmk_len
;
2136 #endif /* CONFIG_IEEE80211R_AP */
2138 #endif /* CONFIG_SAE */
2139 sm
->req_replay_counter_used
= 0;
2143 SM_STATE(WPA_PTK
, PTKSTART
)
2145 u8 buf
[2 + RSN_SELECTOR_LEN
+ PMKID_LEN
], *pmkid
= NULL
;
2146 size_t pmkid_len
= 0;
2148 SM_ENTRY_MA(WPA_PTK
, PTKSTART
, wpa_ptk
);
2149 sm
->PTKRequest
= FALSE
;
2150 sm
->TimeoutEvt
= FALSE
;
2151 sm
->alt_snonce_valid
= FALSE
;
2154 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
2155 /* No point in sending the EAPOL-Key - we will disconnect
2156 * immediately following this. */
2160 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2161 "sending 1/4 msg of 4-Way Handshake");
2163 * For infrastructure BSS cases, it is better for the AP not to include
2164 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2165 * offline search for the passphrase/PSK without having to be able to
2166 * capture a 4-way handshake from a STA that has access to the network.
2168 * For IBSS cases, addition of PMKID KDE could be considered even with
2169 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2170 * possible PSK for this STA. However, this should not be done unless
2171 * there is support for using that information on the supplicant side.
2172 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2173 * cases would also apply here, but at least in the IBSS case, this
2174 * would cover a potential real use case.
2176 if (sm
->wpa
== WPA_VERSION_WPA2
&&
2177 (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) ||
2178 (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
&& sm
->pmksa
) ||
2179 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) &&
2180 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
) {
2182 pmkid_len
= 2 + RSN_SELECTOR_LEN
+ PMKID_LEN
;
2183 pmkid
[0] = WLAN_EID_VENDOR_SPECIFIC
;
2184 pmkid
[1] = RSN_SELECTOR_LEN
+ PMKID_LEN
;
2185 RSN_SELECTOR_PUT(&pmkid
[2], RSN_KEY_DATA_PMKID
);
2187 wpa_hexdump(MSG_DEBUG
,
2188 "RSN: Message 1/4 PMKID from PMKSA entry",
2189 sm
->pmksa
->pmkid
, PMKID_LEN
);
2190 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2191 sm
->pmksa
->pmkid
, PMKID_LEN
);
2192 } else if (wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
)) {
2193 /* No KCK available to derive PMKID */
2194 wpa_printf(MSG_DEBUG
,
2195 "RSN: No KCK available to derive PMKID for message 1/4");
2198 } else if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2199 if (sm
->pmkid_set
) {
2200 wpa_hexdump(MSG_DEBUG
,
2201 "RSN: Message 1/4 PMKID from FILS/ERP",
2202 sm
->pmkid
, PMKID_LEN
);
2203 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2204 sm
->pmkid
, PMKID_LEN
);
2206 /* No PMKID available */
2207 wpa_printf(MSG_DEBUG
,
2208 "RSN: No FILS/ERP PMKID available for message 1/4");
2211 #endif /* CONFIG_FILS */
2212 #ifdef CONFIG_IEEE80211R_AP
2213 } else if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
) &&
2215 wpa_printf(MSG_DEBUG
,
2216 "FT: No PMKID in message 1/4 when using FT protocol");
2218 #endif /* CONFIG_IEEE80211R_AP */
2220 } else if (wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
2221 if (sm
->pmkid_set
) {
2222 wpa_hexdump(MSG_DEBUG
,
2223 "RSN: Message 1/4 PMKID from SAE",
2224 sm
->pmkid
, PMKID_LEN
);
2225 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2226 sm
->pmkid
, PMKID_LEN
);
2228 /* No PMKID available */
2229 wpa_printf(MSG_DEBUG
,
2230 "RSN: No SAE PMKID available for message 1/4");
2233 #endif /* CONFIG_SAE */
2236 * Calculate PMKID since no PMKSA cache entry was
2237 * available with pre-calculated PMKID.
2239 rsn_pmkid(sm
->PMK
, sm
->pmk_len
, sm
->wpa_auth
->addr
,
2240 sm
->addr
, &pmkid
[2 + RSN_SELECTOR_LEN
],
2242 wpa_hexdump(MSG_DEBUG
,
2243 "RSN: Message 1/4 PMKID derived from PMK",
2244 &pmkid
[2 + RSN_SELECTOR_LEN
], PMKID_LEN
);
2249 wpa_send_eapol(sm
->wpa_auth
, sm
,
2250 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
2251 sm
->ANonce
, pmkid
, pmkid_len
, 0, 0);
2255 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
2256 const u8
*pmk
, unsigned int pmk_len
,
2257 struct wpa_ptk
*ptk
, int force_sha256
)
2263 #ifdef CONFIG_IEEE80211R_AP
2264 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2265 if (sm
->ft_completed
) {
2266 u8 ptk_name
[WPA_PMK_NAME_LEN
];
2268 return wpa_pmk_r1_to_ptk(sm
->pmk_r1
, sm
->pmk_r1_len
,
2269 sm
->SNonce
, sm
->ANonce
,
2270 sm
->addr
, sm
->wpa_auth
->addr
,
2276 return wpa_auth_derive_ptk_ft(sm
, ptk
);
2278 #endif /* CONFIG_IEEE80211R_AP */
2281 if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->dpp_z
) {
2282 z
= wpabuf_head(sm
->dpp_z
);
2283 z_len
= wpabuf_len(sm
->dpp_z
);
2285 #endif /* CONFIG_DPP2 */
2287 akmp
= sm
->wpa_key_mgmt
;
2289 akmp
|= WPA_KEY_MGMT_PSK_SHA256
;
2290 return wpa_pmk_to_ptk(pmk
, pmk_len
, "Pairwise key expansion",
2291 sm
->wpa_auth
->addr
, sm
->addr
, sm
->ANonce
, snonce
,
2292 ptk
, akmp
, sm
->pairwise
, z
, z_len
);
2298 int fils_auth_pmk_to_ptk(struct wpa_state_machine
*sm
, const u8
*pmk
,
2299 size_t pmk_len
, const u8
*snonce
, const u8
*anonce
,
2300 const u8
*dhss
, size_t dhss_len
,
2301 struct wpabuf
*g_sta
, struct wpabuf
*g_ap
)
2303 u8 ick
[FILS_ICK_MAX_LEN
];
2306 u8 fils_ft
[FILS_FT_MAX_LEN
];
2307 size_t fils_ft_len
= 0;
2309 res
= fils_pmk_to_ptk(pmk
, pmk_len
, sm
->addr
, sm
->wpa_auth
->addr
,
2310 snonce
, anonce
, dhss
, dhss_len
,
2311 &sm
->PTK
, ick
, &ick_len
,
2312 sm
->wpa_key_mgmt
, sm
->pairwise
,
2313 fils_ft
, &fils_ft_len
);
2316 sm
->PTK_valid
= TRUE
;
2317 sm
->tk_already_set
= FALSE
;
2319 #ifdef CONFIG_IEEE80211R_AP
2321 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2322 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
2323 u8 pmk_r0
[PMK_LEN_MAX
], pmk_r0_name
[WPA_PMK_NAME_LEN
];
2324 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
2325 size_t pmk_r0_len
= use_sha384
? SHA384_MAC_LEN
: PMK_LEN
;
2327 if (wpa_derive_pmk_r0(fils_ft
, fils_ft_len
,
2328 conf
->ssid
, conf
->ssid_len
,
2329 conf
->mobility_domain
,
2330 conf
->r0_key_holder
,
2331 conf
->r0_key_holder_len
,
2332 sm
->addr
, pmk_r0
, pmk_r0_name
,
2336 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: PMK-R0",
2337 pmk_r0
, pmk_r0_len
);
2338 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR0Name",
2339 pmk_r0_name
, WPA_PMK_NAME_LEN
);
2340 wpa_ft_store_pmk_fils(sm
, pmk_r0
, pmk_r0_name
);
2341 forced_memzero(fils_ft
, sizeof(fils_ft
));
2343 res
= wpa_derive_pmk_r1_name(pmk_r0_name
, conf
->r1_key_holder
,
2344 sm
->addr
, sm
->pmk_r1_name
,
2346 forced_memzero(pmk_r0
, PMK_LEN_MAX
);
2349 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR1Name", sm
->pmk_r1_name
,
2351 sm
->pmk_r1_name_valid
= 1;
2353 #endif /* CONFIG_IEEE80211R_AP */
2355 res
= fils_key_auth_sk(ick
, ick_len
, snonce
, anonce
,
2356 sm
->addr
, sm
->wpa_auth
->addr
,
2357 g_sta
? wpabuf_head(g_sta
) : NULL
,
2358 g_sta
? wpabuf_len(g_sta
) : 0,
2359 g_ap
? wpabuf_head(g_ap
) : NULL
,
2360 g_ap
? wpabuf_len(g_ap
) : 0,
2361 sm
->wpa_key_mgmt
, sm
->fils_key_auth_sta
,
2362 sm
->fils_key_auth_ap
,
2363 &sm
->fils_key_auth_len
);
2364 forced_memzero(ick
, sizeof(ick
));
2366 /* Store nonces for (Re)Association Request/Response frame processing */
2367 os_memcpy(sm
->SNonce
, snonce
, FILS_NONCE_LEN
);
2368 os_memcpy(sm
->ANonce
, anonce
, FILS_NONCE_LEN
);
2374 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
2375 u8
*buf
, size_t buf_len
, u16
*_key_data_len
)
2377 struct ieee802_1x_hdr
*hdr
;
2378 struct wpa_eapol_key
*key
;
2385 hdr
= (struct ieee802_1x_hdr
*) buf
;
2386 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2387 pos
= (u8
*) (key
+ 1);
2388 key_data_len
= WPA_GET_BE16(pos
);
2389 if (key_data_len
< AES_BLOCK_SIZE
||
2390 key_data_len
> buf_len
- sizeof(*hdr
) - sizeof(*key
) - 2) {
2391 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2392 "No room for AES-SIV data in the frame");
2395 pos
+= 2; /* Pointing at the Encrypted Key Data field */
2397 tmp
= os_malloc(key_data_len
);
2401 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2402 * to Key Data (exclusive). */
2404 aad_len
[0] = pos
- buf
;
2405 if (aes_siv_decrypt(ptk
->kek
, ptk
->kek_len
, pos
, key_data_len
,
2406 1, aad
, aad_len
, tmp
) < 0) {
2407 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2408 "Invalid AES-SIV data in the frame");
2409 bin_clear_free(tmp
, key_data_len
);
2413 /* AEAD decryption and validation completed successfully */
2414 key_data_len
-= AES_BLOCK_SIZE
;
2415 wpa_hexdump_key(MSG_DEBUG
, "WPA: Decrypted Key Data",
2418 /* Replace Key Data field with the decrypted version */
2419 os_memcpy(pos
, tmp
, key_data_len
);
2420 pos
-= 2; /* Key Data Length field */
2421 WPA_PUT_BE16(pos
, key_data_len
);
2422 bin_clear_free(tmp
, key_data_len
);
2424 *_key_data_len
= key_data_len
;
2429 const u8
* wpa_fils_validate_fils_session(struct wpa_state_machine
*sm
,
2430 const u8
*ies
, size_t ies_len
,
2431 const u8
*fils_session
)
2434 const u8
*session
= NULL
;
2436 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2437 wpa_printf(MSG_DEBUG
,
2438 "FILS: Not a FILS AKM - reject association");
2442 /* Verify Session element */
2444 end
= ((const u8
*) ie
) + ies_len
;
2445 while (ie
+ 1 < end
) {
2446 if (ie
+ 2 + ie
[1] > end
)
2448 if (ie
[0] == WLAN_EID_EXTENSION
&&
2449 ie
[1] >= 1 + FILS_SESSION_LEN
&&
2450 ie
[2] == WLAN_EID_EXT_FILS_SESSION
) {
2458 wpa_printf(MSG_DEBUG
,
2459 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2464 if (!fils_session
) {
2465 wpa_printf(MSG_DEBUG
,
2466 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2471 if (os_memcmp(fils_session
, session
+ 3, FILS_SESSION_LEN
) != 0) {
2472 wpa_printf(MSG_DEBUG
, "FILS: Session mismatch");
2473 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
2474 fils_session
, FILS_SESSION_LEN
);
2475 wpa_hexdump(MSG_DEBUG
, "FILS: Received FILS Session",
2476 session
+ 3, FILS_SESSION_LEN
);
2483 int wpa_fils_validate_key_confirm(struct wpa_state_machine
*sm
, const u8
*ies
,
2486 struct ieee802_11_elems elems
;
2488 if (ieee802_11_parse_elems(ies
, ies_len
, &elems
, 1) == ParseFailed
) {
2489 wpa_printf(MSG_DEBUG
,
2490 "FILS: Failed to parse decrypted elements");
2494 if (!elems
.fils_session
) {
2495 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
2499 if (!elems
.fils_key_confirm
) {
2500 wpa_printf(MSG_DEBUG
, "FILS: No FILS Key Confirm element");
2504 if (elems
.fils_key_confirm_len
!= sm
->fils_key_auth_len
) {
2505 wpa_printf(MSG_DEBUG
,
2506 "FILS: Unexpected Key-Auth length %d (expected %d)",
2507 elems
.fils_key_confirm_len
,
2508 (int) sm
->fils_key_auth_len
);
2512 if (os_memcmp(elems
.fils_key_confirm
, sm
->fils_key_auth_sta
,
2513 sm
->fils_key_auth_len
) != 0) {
2514 wpa_printf(MSG_DEBUG
, "FILS: Key-Auth mismatch");
2515 wpa_hexdump(MSG_DEBUG
, "FILS: Received Key-Auth",
2516 elems
.fils_key_confirm
, elems
.fils_key_confirm_len
);
2517 wpa_hexdump(MSG_DEBUG
, "FILS: Expected Key-Auth",
2518 sm
->fils_key_auth_sta
, sm
->fils_key_auth_len
);
2526 int fils_decrypt_assoc(struct wpa_state_machine
*sm
, const u8
*fils_session
,
2527 const struct ieee80211_mgmt
*mgmt
, size_t frame_len
,
2528 u8
*pos
, size_t left
)
2531 const u8
*end
, *ie_start
, *ie
, *session
, *crypt
;
2535 if (!sm
|| !sm
->PTK_valid
) {
2536 wpa_printf(MSG_DEBUG
,
2537 "FILS: No KEK to decrypt Assocication Request frame");
2541 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2542 wpa_printf(MSG_DEBUG
,
2543 "FILS: Not a FILS AKM - reject association");
2547 end
= ((const u8
*) mgmt
) + frame_len
;
2548 fc
= le_to_host16(mgmt
->frame_control
);
2549 stype
= WLAN_FC_GET_STYPE(fc
);
2550 if (stype
== WLAN_FC_STYPE_REASSOC_REQ
)
2551 ie_start
= mgmt
->u
.reassoc_req
.variable
;
2553 ie_start
= mgmt
->u
.assoc_req
.variable
;
2557 * Find FILS Session element which is the last unencrypted element in
2560 session
= wpa_fils_validate_fils_session(sm
, ie
, end
- ie
,
2563 wpa_printf(MSG_DEBUG
, "FILS: Session validation failed");
2567 crypt
= session
+ 2 + session
[1];
2569 if (end
- crypt
< AES_BLOCK_SIZE
) {
2570 wpa_printf(MSG_DEBUG
,
2571 "FILS: Too short frame to include AES-SIV data");
2575 /* AES-SIV AAD vectors */
2577 /* The STA's MAC address */
2579 aad_len
[0] = ETH_ALEN
;
2580 /* The AP's BSSID */
2582 aad_len
[1] = ETH_ALEN
;
2583 /* The STA's nonce */
2584 aad
[2] = sm
->SNonce
;
2585 aad_len
[2] = FILS_NONCE_LEN
;
2586 /* The AP's nonce */
2587 aad
[3] = sm
->ANonce
;
2588 aad_len
[3] = FILS_NONCE_LEN
;
2590 * The (Re)Association Request frame from the Capability Information
2591 * field to the FILS Session element (both inclusive).
2593 aad
[4] = (const u8
*) &mgmt
->u
.assoc_req
.capab_info
;
2594 aad_len
[4] = crypt
- aad
[4];
2596 if (aes_siv_decrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, crypt
, end
- crypt
,
2597 5, aad
, aad_len
, pos
+ (crypt
- ie_start
)) < 0) {
2598 wpa_printf(MSG_DEBUG
,
2599 "FILS: Invalid AES-SIV data in the frame");
2602 wpa_hexdump(MSG_DEBUG
, "FILS: Decrypted Association Request elements",
2603 pos
, left
- AES_BLOCK_SIZE
);
2605 if (wpa_fils_validate_key_confirm(sm
, pos
, left
- AES_BLOCK_SIZE
) < 0) {
2606 wpa_printf(MSG_DEBUG
, "FILS: Key Confirm validation failed");
2610 return left
- AES_BLOCK_SIZE
;
2614 int fils_encrypt_assoc(struct wpa_state_machine
*sm
, u8
*buf
,
2615 size_t current_len
, size_t max_len
,
2616 const struct wpabuf
*hlp
)
2618 u8
*end
= buf
+ max_len
;
2619 u8
*pos
= buf
+ current_len
;
2620 struct ieee80211_mgmt
*mgmt
;
2621 struct wpabuf
*plain
;
2625 if (!sm
|| !sm
->PTK_valid
)
2628 wpa_hexdump(MSG_DEBUG
,
2629 "FILS: Association Response frame before FILS processing",
2632 mgmt
= (struct ieee80211_mgmt
*) buf
;
2634 /* AES-SIV AAD vectors */
2636 /* The AP's BSSID */
2638 aad_len
[0] = ETH_ALEN
;
2639 /* The STA's MAC address */
2641 aad_len
[1] = ETH_ALEN
;
2642 /* The AP's nonce */
2643 aad
[2] = sm
->ANonce
;
2644 aad_len
[2] = FILS_NONCE_LEN
;
2645 /* The STA's nonce */
2646 aad
[3] = sm
->SNonce
;
2647 aad_len
[3] = FILS_NONCE_LEN
;
2649 * The (Re)Association Response frame from the Capability Information
2650 * field (the same offset in both Association and Reassociation
2651 * Response frames) to the FILS Session element (both inclusive).
2653 aad
[4] = (const u8
*) &mgmt
->u
.assoc_resp
.capab_info
;
2654 aad_len
[4] = pos
- aad
[4];
2656 /* The following elements will be encrypted with AES-SIV */
2657 plain
= fils_prepare_plainbuf(sm
, hlp
);
2659 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2663 if (pos
+ wpabuf_len(plain
) + AES_BLOCK_SIZE
> end
) {
2664 wpa_printf(MSG_DEBUG
,
2665 "FILS: Not enough room for FILS elements");
2666 wpabuf_clear_free(plain
);
2670 wpa_hexdump_buf_key(MSG_DEBUG
, "FILS: Association Response plaintext",
2673 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
2674 wpabuf_head(plain
), wpabuf_len(plain
),
2675 5, aad
, aad_len
, pos
) < 0) {
2676 wpabuf_clear_free(plain
);
2680 wpa_hexdump(MSG_DEBUG
,
2681 "FILS: Encrypted Association Response elements",
2682 pos
, AES_BLOCK_SIZE
+ wpabuf_len(plain
));
2683 current_len
+= wpabuf_len(plain
) + AES_BLOCK_SIZE
;
2684 wpabuf_clear_free(plain
);
2686 sm
->fils_completed
= 1;
2692 static struct wpabuf
* fils_prepare_plainbuf(struct wpa_state_machine
*sm
,
2693 const struct wpabuf
*hlp
)
2695 struct wpabuf
*plain
;
2696 u8
*len
, *tmp
, *tmp2
;
2698 u8
*gtk
, dummy_gtk
[32];
2700 struct wpa_group
*gsm
;
2702 plain
= wpabuf_alloc(1000 + ieee80211w_kde_len(sm
));
2706 /* TODO: FILS Public Key */
2708 /* FILS Key Confirmation */
2709 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2710 wpabuf_put_u8(plain
, 1 + sm
->fils_key_auth_len
); /* Length */
2711 /* Element ID Extension */
2712 wpabuf_put_u8(plain
, WLAN_EID_EXT_FILS_KEY_CONFIRM
);
2713 wpabuf_put_data(plain
, sm
->fils_key_auth_ap
, sm
->fils_key_auth_len
);
2715 /* FILS HLP Container */
2717 wpabuf_put_buf(plain
, hlp
);
2719 /* TODO: FILS IP Address Assignment */
2723 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2724 len
= wpabuf_put(plain
, 1);
2725 wpabuf_put_u8(plain
, WLAN_EID_EXT_KEY_DELIVERY
);
2726 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
,
2727 wpabuf_put(plain
, WPA_KEY_RSC_LEN
));
2729 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2730 gtk_len
= gsm
->GTK_len
;
2731 if (sm
->wpa_auth
->conf
.disable_gtk
||
2732 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
2734 * Provide unique random GTK to each STA to prevent use
2735 * of GTK in the BSS.
2737 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0) {
2738 wpabuf_clear_free(plain
);
2743 hdr
[0] = gsm
->GN
& 0x03;
2745 tmp
= wpabuf_put(plain
, 0);
2746 tmp2
= wpa_add_kde(tmp
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
2748 wpabuf_put(plain
, tmp2
- tmp
);
2750 /* IGTK KDE and BIGTK KDE */
2751 tmp
= wpabuf_put(plain
, 0);
2752 tmp2
= ieee80211w_kde_add(sm
, tmp
);
2753 wpabuf_put(plain
, tmp2
- tmp
);
2755 *len
= (u8
*) wpabuf_put(plain
, 0) - len
- 1;
2758 if (wpa_auth_uses_ocv(sm
)) {
2759 struct wpa_channel_info ci
;
2762 if (wpa_channel_info(sm
->wpa_auth
, &ci
) != 0) {
2763 wpa_printf(MSG_WARNING
,
2764 "FILS: Failed to get channel info for OCI element");
2765 wpabuf_clear_free(plain
);
2769 pos
= wpabuf_put(plain
, OCV_OCI_EXTENDED_LEN
);
2770 if (ocv_insert_extended_oci(&ci
, pos
) < 0) {
2771 wpabuf_clear_free(plain
);
2775 #endif /* CONFIG_OCV */
2781 int fils_set_tk(struct wpa_state_machine
*sm
)
2786 if (!sm
|| !sm
->PTK_valid
) {
2787 wpa_printf(MSG_DEBUG
, "FILS: No valid PTK available to set TK");
2790 if (sm
->tk_already_set
) {
2791 wpa_printf(MSG_DEBUG
, "FILS: TK already set to the driver");
2795 alg
= wpa_cipher_to_alg(sm
->pairwise
);
2796 klen
= wpa_cipher_key_len(sm
->pairwise
);
2798 wpa_printf(MSG_DEBUG
, "FILS: Configure TK to the driver");
2799 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
2800 sm
->PTK
.tk
, klen
, KEY_FLAG_PAIRWISE_RX_TX
)) {
2801 wpa_printf(MSG_DEBUG
, "FILS: Failed to set TK to the driver");
2804 sm
->tk_already_set
= TRUE
;
2810 u8
* hostapd_eid_assoc_fils_session(struct wpa_state_machine
*sm
, u8
*buf
,
2811 const u8
*fils_session
, struct wpabuf
*hlp
)
2813 struct wpabuf
*plain
;
2817 *pos
++ = WLAN_EID_EXTENSION
; /* Element ID */
2818 *pos
++ = 1 + FILS_SESSION_LEN
; /* Length */
2819 *pos
++ = WLAN_EID_EXT_FILS_SESSION
; /* Element ID Extension */
2820 os_memcpy(pos
, fils_session
, FILS_SESSION_LEN
);
2821 pos
+= FILS_SESSION_LEN
;
2823 plain
= fils_prepare_plainbuf(sm
, hlp
);
2825 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2829 os_memcpy(pos
, wpabuf_head(plain
), wpabuf_len(plain
));
2830 pos
+= wpabuf_len(plain
);
2832 wpa_printf(MSG_DEBUG
, "%s: plain buf_len: %u", __func__
,
2833 (unsigned int) wpabuf_len(plain
));
2834 wpabuf_clear_free(plain
);
2835 sm
->fils_completed
= 1;
2839 #endif /* CONFIG_FILS */
2843 int get_sta_tx_parameters(struct wpa_state_machine
*sm
, int ap_max_chanwidth
,
2844 int ap_seg1_idx
, int *bandwidth
, int *seg1_idx
)
2846 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2848 if (!wpa_auth
->cb
->get_sta_tx_params
)
2850 return wpa_auth
->cb
->get_sta_tx_params(wpa_auth
->cb_ctx
, sm
->addr
,
2851 ap_max_chanwidth
, ap_seg1_idx
,
2852 bandwidth
, seg1_idx
);
2854 #endif /* CONFIG_OCV */
2857 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING
)
2859 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2861 int ok
= 0, psk_found
= 0;
2862 const u8
*pmk
= NULL
;
2865 const u8
*eapol_key_ie
, *key_data
, *mic
;
2866 u16 key_data_length
;
2867 size_t mic_len
, eapol_key_ie_len
;
2868 struct ieee802_1x_hdr
*hdr
;
2869 struct wpa_eapol_key
*key
;
2870 struct wpa_eapol_ie_parse kde
;
2872 int owe_ptk_workaround
= !!wpa_auth
->conf
.owe_ptk_workaround
;
2874 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING
, wpa_ptk
);
2875 sm
->EAPOLKeyReceived
= FALSE
;
2876 sm
->update_snonce
= FALSE
;
2877 os_memset(&PTK
, 0, sizeof(PTK
));
2879 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
2881 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2882 * WPA-PSK: iterate through possible PSKs and select the one matching
2885 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
2886 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
2887 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
2888 sm
->p2p_dev_addr
, pmk
, &pmk_len
,
2893 #ifdef CONFIG_IEEE80211R_AP
2894 if (wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
2895 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
2896 sm
->xxkey_len
= pmk_len
;
2898 #endif /* CONFIG_IEEE80211R_AP */
2901 pmk_len
= sm
->pmk_len
;
2904 if ((!pmk
|| !pmk_len
) && sm
->pmksa
) {
2905 wpa_printf(MSG_DEBUG
, "WPA: Use PMK from PMKSA cache");
2906 pmk
= sm
->pmksa
->pmk
;
2907 pmk_len
= sm
->pmksa
->pmk_len
;
2910 if (wpa_derive_ptk(sm
, sm
->SNonce
, pmk
, pmk_len
, &PTK
,
2911 owe_ptk_workaround
== 2) < 0)
2915 wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
2916 sm
->last_rx_eapol_key
,
2917 sm
->last_rx_eapol_key_len
) == 0) {
2918 if (sm
->PMK
!= pmk
) {
2919 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
2920 sm
->pmk_len
= pmk_len
;
2928 wpa_aead_decrypt(sm
, &PTK
, sm
->last_rx_eapol_key
,
2929 sm
->last_rx_eapol_key_len
, NULL
) == 0) {
2933 #endif /* CONFIG_FILS */
2936 if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
&& pmk_len
> 32 &&
2937 owe_ptk_workaround
== 1) {
2938 wpa_printf(MSG_DEBUG
,
2939 "OWE: Try PTK derivation workaround with SHA256");
2940 owe_ptk_workaround
= 2;
2943 #endif /* CONFIG_OWE */
2945 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
2946 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
))
2951 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2952 "invalid MIC in msg 2/4 of 4-Way Handshake");
2954 wpa_auth_psk_failure_report(sm
->wpa_auth
, sm
->addr
);
2959 * Note: last_rx_eapol_key length fields have already been validated in
2962 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
2963 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2964 mic
= (u8
*) (key
+ 1);
2965 key_data
= mic
+ mic_len
+ 2;
2966 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
2967 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
2968 sizeof(*key
) - mic_len
- 2)
2971 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
2972 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2973 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2977 eapol_key_ie
= kde
.rsn_ie
;
2978 eapol_key_ie_len
= kde
.rsn_ie_len
;
2979 } else if (kde
.osen
) {
2980 eapol_key_ie
= kde
.osen
;
2981 eapol_key_ie_len
= kde
.osen_len
;
2983 eapol_key_ie
= kde
.wpa_ie
;
2984 eapol_key_ie_len
= kde
.wpa_ie_len
;
2986 ft
= sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
);
2987 if (sm
->wpa_ie
== NULL
||
2988 wpa_compare_rsn_ie(ft
, sm
->wpa_ie
, sm
->wpa_ie_len
,
2989 eapol_key_ie
, eapol_key_ie_len
)) {
2990 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2991 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2993 wpa_hexdump(MSG_DEBUG
, "WPA IE in AssocReq",
2994 sm
->wpa_ie
, sm
->wpa_ie_len
);
2996 wpa_hexdump(MSG_DEBUG
, "WPA IE in msg 2/4",
2997 eapol_key_ie
, eapol_key_ie_len
);
2998 /* MLME-DEAUTHENTICATE.request */
2999 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
3000 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3003 if ((!sm
->rsnxe
&& kde
.rsnxe
) ||
3004 (sm
->rsnxe
&& !kde
.rsnxe
) ||
3005 (sm
->rsnxe
&& kde
.rsnxe
&&
3006 (sm
->rsnxe_len
!= kde
.rsnxe_len
||
3007 os_memcmp(sm
->rsnxe
, kde
.rsnxe
, sm
->rsnxe_len
) != 0))) {
3008 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3009 "RSNXE from (Re)AssocReq did not match the one in EAPOL-Key msg 2/4");
3010 wpa_hexdump(MSG_DEBUG
, "RSNXE in AssocReq",
3011 sm
->rsnxe
, sm
->rsnxe_len
);
3012 wpa_hexdump(MSG_DEBUG
, "RSNXE in EAPOL-Key msg 2/4",
3013 kde
.rsnxe
, kde
.rsnxe_len
);
3014 /* MLME-DEAUTHENTICATE.request */
3015 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
3016 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3020 if (wpa_auth_uses_ocv(sm
)) {
3021 struct wpa_channel_info ci
;
3025 if (wpa_channel_info(wpa_auth
, &ci
) != 0) {
3026 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3027 "Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
3031 if (get_sta_tx_parameters(sm
,
3032 channel_width_to_int(ci
.chanwidth
),
3033 ci
.seg1_idx
, &tx_chanwidth
,
3037 if (ocv_verify_tx_params(kde
.oci
, kde
.oci_len
, &ci
,
3038 tx_chanwidth
, tx_seg1_idx
) != 0) {
3039 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3044 #endif /* CONFIG_OCV */
3045 #ifdef CONFIG_IEEE80211R_AP
3046 if (ft
&& ft_check_msg_2_of_4(wpa_auth
, sm
, &kde
) < 0) {
3047 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
3048 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3051 #endif /* CONFIG_IEEE80211R_AP */
3053 if (kde
.ip_addr_req
&& kde
.ip_addr_req
[0] &&
3054 wpa_auth
->ip_pool
&& WPA_GET_BE32(sm
->ip_addr
) == 0) {
3056 wpa_printf(MSG_DEBUG
,
3057 "P2P: IP address requested in EAPOL-Key exchange");
3058 idx
= bitfield_get_first_zero(wpa_auth
->ip_pool
);
3060 u32 start
= WPA_GET_BE32(wpa_auth
->conf
.ip_addr_start
);
3061 bitfield_set(wpa_auth
->ip_pool
, idx
);
3062 WPA_PUT_BE32(sm
->ip_addr
, start
+ idx
);
3063 wpa_printf(MSG_DEBUG
,
3064 "P2P: Assigned IP address %u.%u.%u.%u to "
3065 MACSTR
, sm
->ip_addr
[0], sm
->ip_addr
[1],
3066 sm
->ip_addr
[2], sm
->ip_addr
[3],
3070 #endif /* CONFIG_P2P */
3072 #ifdef CONFIG_IEEE80211R_AP
3073 if (sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3075 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
3076 * with the value we derived.
3078 if (os_memcmp_const(sm
->sup_pmk_r1_name
, sm
->pmk_r1_name
,
3079 WPA_PMK_NAME_LEN
) != 0) {
3080 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3081 "PMKR1Name mismatch in FT 4-way "
3083 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from "
3085 sm
->sup_pmk_r1_name
, WPA_PMK_NAME_LEN
);
3086 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
3087 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
3091 #endif /* CONFIG_IEEE80211R_AP */
3093 if (vlan_id
&& wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
3094 wpa_auth_update_vlan(wpa_auth
, sm
->addr
, vlan_id
) < 0) {
3095 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
3096 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3100 sm
->pending_1_of_4_timeout
= 0;
3101 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
3103 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
3104 /* PSK may have changed from the previous choice, so update
3105 * state machine data based on whatever PSK was selected here.
3107 os_memcpy(sm
->PMK
, pmk
, PMK_LEN
);
3108 sm
->pmk_len
= PMK_LEN
;
3111 sm
->MICVerified
= TRUE
;
3113 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
3114 forced_memzero(&PTK
, sizeof(PTK
));
3115 sm
->PTK_valid
= TRUE
;
3119 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING2
)
3121 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING2
, wpa_ptk
);
3126 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
3130 if (sm
->mgmt_frame_prot
) {
3131 len
+= 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
;
3132 len
+= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3134 if (sm
->mgmt_frame_prot
&& sm
->wpa_auth
->conf
.beacon_prot
) {
3135 len
+= 2 + RSN_SELECTOR_LEN
+ WPA_BIGTK_KDE_PREFIX_LEN
;
3136 len
+= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3143 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
3145 struct wpa_igtk_kde igtk
;
3146 struct wpa_bigtk_kde bigtk
;
3147 struct wpa_group
*gsm
= sm
->group
;
3148 u8 rsc
[WPA_KEY_RSC_LEN
];
3149 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3151 if (!sm
->mgmt_frame_prot
)
3154 igtk
.keyid
[0] = gsm
->GN_igtk
;
3156 if (gsm
->wpa_group_state
!= WPA_GROUP_SETKEYSDONE
||
3157 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, rsc
) < 0)
3158 os_memset(igtk
.pn
, 0, sizeof(igtk
.pn
));
3160 os_memcpy(igtk
.pn
, rsc
, sizeof(igtk
.pn
));
3161 os_memcpy(igtk
.igtk
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3162 if (sm
->wpa_auth
->conf
.disable_gtk
||
3163 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3165 * Provide unique random IGTK to each STA to prevent use of
3168 if (random_get_bytes(igtk
.igtk
, len
) < 0)
3171 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_IGTK
,
3172 (const u8
*) &igtk
, WPA_IGTK_KDE_PREFIX_LEN
+ len
,
3175 if (!sm
->wpa_auth
->conf
.beacon_prot
)
3178 bigtk
.keyid
[0] = gsm
->GN_bigtk
;
3180 if (gsm
->wpa_group_state
!= WPA_GROUP_SETKEYSDONE
||
3181 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_bigtk
, rsc
) < 0)
3182 os_memset(bigtk
.pn
, 0, sizeof(bigtk
.pn
));
3184 os_memcpy(bigtk
.pn
, rsc
, sizeof(bigtk
.pn
));
3185 os_memcpy(bigtk
.bigtk
, gsm
->BIGTK
[gsm
->GN_bigtk
- 6], len
);
3186 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_BIGTK
,
3187 (const u8
*) &bigtk
, WPA_BIGTK_KDE_PREFIX_LEN
+ len
,
3194 static int ocv_oci_len(struct wpa_state_machine
*sm
)
3197 if (wpa_auth_uses_ocv(sm
))
3198 return OCV_OCI_KDE_LEN
;
3199 #endif /* CONFIG_OCV */
3203 static int ocv_oci_add(struct wpa_state_machine
*sm
, u8
**argpos
)
3206 struct wpa_channel_info ci
;
3208 if (!wpa_auth_uses_ocv(sm
))
3211 if (wpa_channel_info(sm
->wpa_auth
, &ci
) != 0) {
3212 wpa_printf(MSG_WARNING
,
3213 "Failed to get channel info for OCI element");
3217 return ocv_insert_oci_kde(&ci
, argpos
);
3218 #else /* CONFIG_OCV */
3220 #endif /* CONFIG_OCV */
3224 #ifdef CONFIG_TESTING_OPTIONS
3225 static u8
* replace_ie(const char *name
, const u8
*old_buf
, size_t *len
, u8 eid
,
3226 const u8
*ie
, size_t ie_len
)
3231 wpa_printf(MSG_DEBUG
, "TESTING: %s EAPOL override", name
);
3232 wpa_hexdump(MSG_DEBUG
, "TESTING: wpa_ie before override",
3234 buf
= os_malloc(*len
+ ie_len
);
3237 os_memcpy(buf
, old_buf
, *len
);
3238 elem
= get_ie(buf
, *len
, eid
);
3240 u8 elem_len
= 2 + elem
[1];
3242 os_memmove((void *) elem
, elem
+ elem_len
,
3243 *len
- (elem
- buf
) - elem_len
);
3246 os_memcpy(buf
+ *len
, ie
, ie_len
);
3248 wpa_hexdump(MSG_DEBUG
, "TESTING: wpa_ie after EAPOL override",
3253 #endif /* CONFIG_TESTING_OPTIONS */
3256 SM_STATE(WPA_PTK
, PTKINITNEGOTIATING
)
3258 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
= NULL
, *pos
, dummy_gtk
[32];
3259 size_t gtk_len
, kde_len
, wpa_ie_len
;
3260 struct wpa_group
*gsm
= sm
->group
;
3262 int secure
, gtkidx
, encr
= 0;
3263 u8
*wpa_ie_buf
= NULL
, *wpa_ie_buf2
= NULL
;
3265 SM_ENTRY_MA(WPA_PTK
, PTKINITNEGOTIATING
, wpa_ptk
);
3266 sm
->TimeoutEvt
= FALSE
;
3269 if (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3270 sm
->TimeoutCtr
> 1) {
3271 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
3274 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
3275 /* No point in sending the EAPOL-Key - we will disconnect
3276 * immediately following this. */
3280 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
3281 GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2])
3283 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3284 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3285 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
3286 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
3287 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
3288 if (sm
->wpa
== WPA_VERSION_WPA
&&
3289 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
3290 wpa_ie_len
> wpa_ie
[1] + 2U && wpa_ie
[0] == WLAN_EID_RSN
) {
3291 /* WPA-only STA, remove RSN IE and possible MDIE */
3292 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
3293 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
3294 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
3295 wpa_ie_len
= wpa_ie
[1] + 2;
3297 #ifdef CONFIG_TESTING_OPTIONS
3298 if (sm
->wpa_auth
->conf
.rsne_override_eapol_set
) {
3299 wpa_ie_buf2
= replace_ie(
3300 "RSNE", wpa_ie
, &wpa_ie_len
, WLAN_EID_RSN
,
3301 sm
->wpa_auth
->conf
.rsne_override_eapol
,
3302 sm
->wpa_auth
->conf
.rsne_override_eapol_len
);
3305 wpa_ie
= wpa_ie_buf2
;
3307 if (sm
->wpa_auth
->conf
.rsnxe_override_eapol_set
) {
3308 wpa_ie_buf
= replace_ie(
3309 "RSNXE", wpa_ie
, &wpa_ie_len
, WLAN_EID_RSNX
,
3310 sm
->wpa_auth
->conf
.rsnxe_override_eapol
,
3311 sm
->wpa_auth
->conf
.rsnxe_override_eapol_len
);
3314 wpa_ie
= wpa_ie_buf
;
3316 #endif /* CONFIG_TESTING_OPTIONS */
3317 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3318 "sending 3/4 msg of 4-Way Handshake");
3319 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3320 /* WPA2 send GTK in the 4-way handshake */
3322 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3323 gtk_len
= gsm
->GTK_len
;
3324 if (sm
->wpa_auth
->conf
.disable_gtk
||
3325 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3327 * Provide unique random GTK to each STA to prevent use
3328 * of GTK in the BSS.
3330 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0)
3338 /* WPA does not include GTK in msg 3/4 */
3343 if (sm
->rx_eapol_key_secure
) {
3345 * It looks like Windows 7 supplicant tries to use
3346 * Secure bit in msg 2/4 after having reported Michael
3347 * MIC failure and it then rejects the 4-way handshake
3348 * if msg 3/4 does not set Secure bit. Work around this
3349 * by setting the Secure bit here even in the case of
3350 * WPA if the supplicant used it first.
3352 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3353 "STA used Secure bit in WPA msg 2/4 - "
3354 "set Secure for 3/4 as workaround");
3359 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
3361 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
3362 #ifdef CONFIG_IEEE80211R_AP
3363 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3364 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
3365 kde_len
+= 300; /* FTIE + 2 * TIE */
3367 #endif /* CONFIG_IEEE80211R_AP */
3369 if (WPA_GET_BE32(sm
->ip_addr
) > 0)
3370 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 3 * 4;
3371 #endif /* CONFIG_P2P */
3372 kde
= os_malloc(kde_len
);
3377 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
3379 #ifdef CONFIG_IEEE80211R_AP
3380 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3385 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
3387 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
3388 "PMKR1Name into RSN IE in EAPOL-Key data");
3394 #endif /* CONFIG_IEEE80211R_AP */
3397 hdr
[0] = gtkidx
& 0x03;
3399 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3402 pos
= ieee80211w_kde_add(sm
, pos
);
3403 if (ocv_oci_add(sm
, &pos
) < 0)
3406 #ifdef CONFIG_IEEE80211R_AP
3407 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3409 struct wpa_auth_config
*conf
;
3411 conf
= &sm
->wpa_auth
->conf
;
3412 if (sm
->assoc_resp_ftie
&&
3413 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
3414 os_memcpy(pos
, sm
->assoc_resp_ftie
,
3415 2 + sm
->assoc_resp_ftie
[1]);
3416 res
= 2 + sm
->assoc_resp_ftie
[1];
3418 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
3420 res
= wpa_write_ftie(conf
, use_sha384
,
3421 conf
->r0_key_holder
,
3422 conf
->r0_key_holder_len
,
3424 kde
+ kde_len
- pos
,
3428 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
3429 "into EAPOL-Key Key Data");
3434 /* TIE[ReassociationDeadline] (TU) */
3435 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3437 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
3438 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
3441 /* TIE[KeyLifetime] (seconds) */
3442 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3444 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
3445 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
);
3448 #endif /* CONFIG_IEEE80211R_AP */
3450 if (WPA_GET_BE32(sm
->ip_addr
) > 0) {
3452 os_memcpy(addr
, sm
->ip_addr
, 4);
3453 os_memcpy(addr
+ 4, sm
->wpa_auth
->conf
.ip_addr_mask
, 4);
3454 os_memcpy(addr
+ 8, sm
->wpa_auth
->conf
.ip_addr_go
, 4);
3455 pos
= wpa_add_kde(pos
, WFA_KEY_DATA_IP_ADDR_ALLOC
,
3456 addr
, sizeof(addr
), NULL
, 0);
3458 #endif /* CONFIG_P2P */
3460 wpa_send_eapol(sm
->wpa_auth
, sm
,
3461 (secure
? WPA_KEY_INFO_SECURE
: 0) |
3462 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3463 WPA_KEY_INFO_MIC
: 0) |
3464 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
3465 WPA_KEY_INFO_KEY_TYPE
,
3466 _rsc
, sm
->ANonce
, kde
, pos
- kde
, 0, encr
);
3469 os_free(wpa_ie_buf
);
3470 os_free(wpa_ie_buf2
);
3474 SM_STATE(WPA_PTK
, PTKINITDONE
)
3476 SM_ENTRY_MA(WPA_PTK
, PTKINITDONE
, wpa_ptk
);
3477 sm
->EAPOLKeyReceived
= FALSE
;
3479 enum wpa_alg alg
= wpa_cipher_to_alg(sm
->pairwise
);
3480 int klen
= wpa_cipher_key_len(sm
->pairwise
);
3481 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
3483 KEY_FLAG_PAIRWISE_RX_TX
)) {
3484 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
,
3485 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3488 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3489 sm
->pairwise_set
= TRUE
;
3491 wpa_auth_set_ptk_rekey_timer(sm
);
3493 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3494 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
3495 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
3496 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3497 WPA_EAPOL_authorized
, 1);
3501 if (0 /* IBSS == TRUE */) {
3503 if (sm
->keycount
== 2) {
3504 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3505 WPA_EAPOL_portValid
, 1);
3508 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
,
3511 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyAvailable
, 0);
3512 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyDone
, 1);
3513 if (sm
->wpa
== WPA_VERSION_WPA
)
3514 sm
->PInitAKeys
= TRUE
;
3517 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3518 "pairwise key handshake completed (%s)",
3519 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3521 #ifdef CONFIG_IEEE80211R_AP
3522 wpa_ft_push_pmk_r1(sm
->wpa_auth
, sm
->addr
);
3523 #endif /* CONFIG_IEEE80211R_AP */
3529 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3532 SM_ENTER(WPA_PTK
, INITIALIZE
);
3533 else if (sm
->Disconnect
3534 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3535 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3536 "WPA_PTK: sm->Disconnect");
3537 SM_ENTER(WPA_PTK
, DISCONNECT
);
3539 else if (sm
->DeauthenticationRequest
)
3540 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3541 else if (sm
->AuthenticationRequest
)
3542 SM_ENTER(WPA_PTK
, AUTHENTICATION
);
3543 else if (sm
->ReAuthenticationRequest
)
3544 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3545 else if (sm
->PTKRequest
) {
3546 if (wpa_auth_sm_ptk_update(sm
) < 0)
3547 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3549 SM_ENTER(WPA_PTK
, PTKSTART
);
3550 } else switch (sm
->wpa_ptk_state
) {
3551 case WPA_PTK_INITIALIZE
:
3553 case WPA_PTK_DISCONNECT
:
3554 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3556 case WPA_PTK_DISCONNECTED
:
3557 SM_ENTER(WPA_PTK
, INITIALIZE
);
3559 case WPA_PTK_AUTHENTICATION
:
3560 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3562 case WPA_PTK_AUTHENTICATION2
:
3563 if (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
3564 wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
3565 WPA_EAPOL_keyRun
) > 0)
3566 SM_ENTER(WPA_PTK
, INITPMK
);
3567 else if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3568 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
3569 /* FIX: && 802.1X::keyRun */)
3570 SM_ENTER(WPA_PTK
, INITPSK
);
3571 else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
)
3572 SM_ENTER(WPA_PTK
, INITPMK
);
3574 case WPA_PTK_INITPMK
:
3575 if (wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
3576 WPA_EAPOL_keyAvailable
) > 0) {
3577 SM_ENTER(WPA_PTK
, PTKSTART
);
3579 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->pmksa
) {
3580 SM_ENTER(WPA_PTK
, PTKSTART
);
3581 #endif /* CONFIG_DPP */
3583 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3584 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3585 "INITPMK - keyAvailable = false");
3586 SM_ENTER(WPA_PTK
, DISCONNECT
);
3589 case WPA_PTK_INITPSK
:
3590 if (wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
,
3591 NULL
, NULL
, NULL
)) {
3592 SM_ENTER(WPA_PTK
, PTKSTART
);
3594 } else if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
3595 SM_ENTER(WPA_PTK
, PTKSTART
);
3596 #endif /* CONFIG_SAE */
3598 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3599 "no PSK configured for the STA");
3600 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3601 SM_ENTER(WPA_PTK
, DISCONNECT
);
3604 case WPA_PTK_PTKSTART
:
3605 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3606 sm
->EAPOLKeyPairwise
)
3607 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3608 else if (sm
->TimeoutCtr
>
3609 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
3610 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3612 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3613 "PTKSTART: Retry limit %u reached",
3614 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3615 SM_ENTER(WPA_PTK
, DISCONNECT
);
3616 } else if (sm
->TimeoutEvt
)
3617 SM_ENTER(WPA_PTK
, PTKSTART
);
3619 case WPA_PTK_PTKCALCNEGOTIATING
:
3620 if (sm
->MICVerified
)
3621 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING2
);
3622 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3623 sm
->EAPOLKeyPairwise
)
3624 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3625 else if (sm
->TimeoutEvt
)
3626 SM_ENTER(WPA_PTK
, PTKSTART
);
3628 case WPA_PTK_PTKCALCNEGOTIATING2
:
3629 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3631 case WPA_PTK_PTKINITNEGOTIATING
:
3632 if (sm
->update_snonce
)
3633 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3634 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3635 sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3636 SM_ENTER(WPA_PTK
, PTKINITDONE
);
3637 else if (sm
->TimeoutCtr
>
3638 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
||
3639 (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3640 sm
->TimeoutCtr
> 1)) {
3641 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3643 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3644 "PTKINITNEGOTIATING: Retry limit %u reached",
3645 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3646 SM_ENTER(WPA_PTK
, DISCONNECT
);
3647 } else if (sm
->TimeoutEvt
)
3648 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3650 case WPA_PTK_PTKINITDONE
:
3656 SM_STATE(WPA_PTK_GROUP
, IDLE
)
3658 SM_ENTRY_MA(WPA_PTK_GROUP
, IDLE
, wpa_ptk_group
);
3660 /* Init flag is not cleared here, so avoid busy
3661 * loop by claiming nothing changed. */
3662 sm
->changed
= FALSE
;
3664 sm
->GTimeoutCtr
= 0;
3668 SM_STATE(WPA_PTK_GROUP
, REKEYNEGOTIATING
)
3670 u8 rsc
[WPA_KEY_RSC_LEN
];
3671 struct wpa_group
*gsm
= sm
->group
;
3673 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
3675 u8
*gtk
, dummy_gtk
[32];
3677 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYNEGOTIATING
, wpa_ptk_group
);
3680 if (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3681 sm
->GTimeoutCtr
> 1) {
3682 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3685 if (sm
->GTimeoutCtr
> sm
->wpa_auth
->conf
.wpa_group_update_count
) {
3686 /* No point in sending the EAPOL-Key - we will disconnect
3687 * immediately following this. */
3691 if (sm
->wpa
== WPA_VERSION_WPA
)
3692 sm
->PInitAKeys
= FALSE
;
3693 sm
->TimeoutEvt
= FALSE
;
3694 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3695 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3696 if (gsm
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
)
3697 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3698 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3699 "sending 1/2 msg of Group Key Handshake");
3701 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3702 if (sm
->wpa_auth
->conf
.disable_gtk
||
3703 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3705 * Provide unique random GTK to each STA to prevent use
3706 * of GTK in the BSS.
3708 if (random_get_bytes(dummy_gtk
, gsm
->GTK_len
) < 0)
3712 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3713 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
3714 ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
3715 kde_buf
= os_malloc(kde_len
);
3716 if (kde_buf
== NULL
)
3719 kde
= pos
= kde_buf
;
3720 hdr
[0] = gsm
->GN
& 0x03;
3722 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3724 pos
= ieee80211w_kde_add(sm
, pos
);
3725 if (ocv_oci_add(sm
, &pos
) < 0) {
3729 kde_len
= pos
- kde
;
3732 kde_len
= gsm
->GTK_len
;
3735 wpa_send_eapol(sm
->wpa_auth
, sm
,
3736 WPA_KEY_INFO_SECURE
|
3737 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3738 WPA_KEY_INFO_MIC
: 0) |
3740 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
3741 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
3747 SM_STATE(WPA_PTK_GROUP
, REKEYESTABLISHED
)
3750 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3751 const u8
*key_data
, *mic
;
3752 struct ieee802_1x_hdr
*hdr
;
3753 struct wpa_eapol_key
*key
;
3754 struct wpa_eapol_ie_parse kde
;
3756 u16 key_data_length
;
3757 #endif /* CONFIG_OCV */
3759 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYESTABLISHED
, wpa_ptk_group
);
3760 sm
->EAPOLKeyReceived
= FALSE
;
3763 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
3766 * Note: last_rx_eapol_key length fields have already been validated in
3769 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
3770 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
3771 mic
= (u8
*) (key
+ 1);
3772 key_data
= mic
+ mic_len
+ 2;
3773 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
3774 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
3775 sizeof(*key
) - mic_len
- 2)
3778 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
3779 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3780 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3784 if (wpa_auth_uses_ocv(sm
)) {
3785 struct wpa_channel_info ci
;
3789 if (wpa_channel_info(wpa_auth
, &ci
) != 0) {
3790 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3791 "Failed to get channel info to validate received OCI in EAPOL-Key group 1/2");
3795 if (get_sta_tx_parameters(sm
,
3796 channel_width_to_int(ci
.chanwidth
),
3797 ci
.seg1_idx
, &tx_chanwidth
,
3801 if (ocv_verify_tx_params(kde
.oci
, kde
.oci_len
, &ci
,
3802 tx_chanwidth
, tx_seg1_idx
) != 0) {
3803 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3808 #endif /* CONFIG_OCV */
3810 if (sm
->GUpdateStationKeys
)
3811 sm
->group
->GKeyDoneStations
--;
3812 sm
->GUpdateStationKeys
= FALSE
;
3813 sm
->GTimeoutCtr
= 0;
3814 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3815 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3816 "group key handshake completed (%s)",
3817 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3822 SM_STATE(WPA_PTK_GROUP
, KEYERROR
)
3824 SM_ENTRY_MA(WPA_PTK_GROUP
, KEYERROR
, wpa_ptk_group
);
3825 if (sm
->GUpdateStationKeys
)
3826 sm
->group
->GKeyDoneStations
--;
3827 sm
->GUpdateStationKeys
= FALSE
;
3828 sm
->Disconnect
= TRUE
;
3829 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3830 "group key handshake failed (%s) after %u tries",
3831 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN",
3832 sm
->wpa_auth
->conf
.wpa_group_update_count
);
3836 SM_STEP(WPA_PTK_GROUP
)
3838 if (sm
->Init
|| sm
->PtkGroupInit
) {
3839 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3840 sm
->PtkGroupInit
= FALSE
;
3841 } else switch (sm
->wpa_ptk_group_state
) {
3842 case WPA_PTK_GROUP_IDLE
:
3843 if (sm
->GUpdateStationKeys
||
3844 (sm
->wpa
== WPA_VERSION_WPA
&& sm
->PInitAKeys
))
3845 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3847 case WPA_PTK_GROUP_REKEYNEGOTIATING
:
3848 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3849 !sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3850 SM_ENTER(WPA_PTK_GROUP
, REKEYESTABLISHED
);
3851 else if (sm
->GTimeoutCtr
>
3852 sm
->wpa_auth
->conf
.wpa_group_update_count
||
3853 (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3854 sm
->GTimeoutCtr
> 1))
3855 SM_ENTER(WPA_PTK_GROUP
, KEYERROR
);
3856 else if (sm
->TimeoutEvt
)
3857 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3859 case WPA_PTK_GROUP_KEYERROR
:
3860 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3862 case WPA_PTK_GROUP_REKEYESTABLISHED
:
3863 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3869 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
3870 struct wpa_group
*group
)
3875 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3876 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3877 if (wpa_gmk_to_gtk(group
->GMK
, "Group key expansion",
3878 wpa_auth
->addr
, group
->GNonce
,
3879 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3881 wpa_hexdump_key(MSG_DEBUG
, "GTK",
3882 group
->GTK
[group
->GN
- 1], group
->GTK_len
);
3884 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3885 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3886 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3887 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3888 if (wpa_gmk_to_gtk(group
->GMK
, "IGTK key expansion",
3889 wpa_auth
->addr
, group
->GNonce
,
3890 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3892 wpa_hexdump_key(MSG_DEBUG
, "IGTK",
3893 group
->IGTK
[group
->GN_igtk
- 4], len
);
3896 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
&&
3897 wpa_auth
->conf
.beacon_prot
) {
3898 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3899 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3900 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3901 if (wpa_gmk_to_gtk(group
->GMK
, "BIGTK key expansion",
3902 wpa_auth
->addr
, group
->GNonce
,
3903 group
->BIGTK
[group
->GN_bigtk
- 6], len
) < 0)
3905 wpa_hexdump_key(MSG_DEBUG
, "BIGTK",
3906 group
->BIGTK
[group
->GN_bigtk
- 6], len
);
3913 static void wpa_group_gtk_init(struct wpa_authenticator
*wpa_auth
,
3914 struct wpa_group
*group
)
3916 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3917 "GTK_INIT (VLAN-ID %d)", group
->vlan_id
);
3918 group
->changed
= FALSE
; /* GInit is not cleared here; avoid loop */
3919 group
->wpa_group_state
= WPA_GROUP_GTK_INIT
;
3922 os_memset(group
->GTK
, 0, sizeof(group
->GTK
));
3927 group
->GN_bigtk
= 6;
3928 group
->GM_bigtk
= 7;
3929 /* GTK[GN] = CalcGTK() */
3930 wpa_gtk_update(wpa_auth
, group
);
3934 static int wpa_group_update_sta(struct wpa_state_machine
*sm
, void *ctx
)
3936 if (ctx
!= NULL
&& ctx
!= sm
->group
)
3939 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITDONE
) {
3940 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3941 "Not in PTKINITDONE; skip Group Key update");
3942 sm
->GUpdateStationKeys
= FALSE
;
3945 if (sm
->GUpdateStationKeys
) {
3947 * This should not really happen, so add a debug log entry.
3948 * Since we clear the GKeyDoneStations before the loop, the
3949 * station needs to be counted here anyway.
3951 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3952 "GUpdateStationKeys was already set when "
3953 "marking station for GTK rekeying");
3956 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
3957 if (sm
->is_wnmsleep
)
3960 sm
->group
->GKeyDoneStations
++;
3961 sm
->GUpdateStationKeys
= TRUE
;
3968 #ifdef CONFIG_WNM_AP
3969 /* update GTK when exiting WNM-Sleep Mode */
3970 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine
*sm
)
3972 if (sm
== NULL
|| sm
->is_wnmsleep
)
3975 wpa_group_update_sta(sm
, NULL
);
3979 void wpa_set_wnmsleep(struct wpa_state_machine
*sm
, int flag
)
3982 sm
->is_wnmsleep
= !!flag
;
3986 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3988 struct wpa_group
*gsm
= sm
->group
;
3993 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
3996 *pos
++ = WNM_SLEEP_SUBELEM_GTK
;
3997 *pos
++ = 11 + gsm
->GTK_len
;
3998 /* Key ID in B0-B1 of Key Info */
3999 WPA_PUT_LE16(pos
, gsm
->GN
& 0x03);
4001 *pos
++ = gsm
->GTK_len
;
4002 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, pos
) != 0)
4005 os_memcpy(pos
, gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
4006 pos
+= gsm
->GTK_len
;
4008 wpa_printf(MSG_DEBUG
, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
4010 wpa_hexdump_key(MSG_DEBUG
, "WNM: GTK in WNM-Sleep Mode exit",
4011 gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
4017 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
4019 struct wpa_group
*gsm
= sm
->group
;
4021 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
4025 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
4027 *pos
++ = WNM_SLEEP_SUBELEM_IGTK
;
4028 *pos
++ = 2 + 6 + len
;
4029 WPA_PUT_LE16(pos
, gsm
->GN_igtk
);
4031 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, pos
) != 0)
4035 os_memcpy(pos
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
4038 wpa_printf(MSG_DEBUG
, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
4040 wpa_hexdump_key(MSG_DEBUG
, "WNM: IGTK in WNM-Sleep Mode exit",
4041 gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
4047 int wpa_wnmsleep_bigtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
4049 struct wpa_group
*gsm
= sm
->group
;
4051 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
4055 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
4057 *pos
++ = WNM_SLEEP_SUBELEM_BIGTK
;
4058 *pos
++ = 2 + 6 + len
;
4059 WPA_PUT_LE16(pos
, gsm
->GN_bigtk
);
4061 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_bigtk
, pos
) != 0)
4065 os_memcpy(pos
, gsm
->BIGTK
[gsm
->GN_bigtk
- 6], len
);
4068 wpa_printf(MSG_DEBUG
, "WNM: BIGTK Key ID %u in WNM-Sleep Mode exit",
4070 wpa_hexdump_key(MSG_DEBUG
, "WNM: BIGTK in WNM-Sleep Mode exit",
4071 gsm
->IGTK
[gsm
->GN_bigtk
- 6], len
);
4076 #endif /* CONFIG_WNM_AP */
4079 static void wpa_group_setkeys(struct wpa_authenticator
*wpa_auth
,
4080 struct wpa_group
*group
)
4084 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
4085 "SETKEYS (VLAN-ID %d)", group
->vlan_id
);
4086 group
->changed
= TRUE
;
4087 group
->wpa_group_state
= WPA_GROUP_SETKEYS
;
4088 group
->GTKReKey
= FALSE
;
4090 group
->GM
= group
->GN
;
4092 tmp
= group
->GM_igtk
;
4093 group
->GM_igtk
= group
->GN_igtk
;
4094 group
->GN_igtk
= tmp
;
4095 tmp
= group
->GM_bigtk
;
4096 group
->GM_bigtk
= group
->GN_bigtk
;
4097 group
->GN_bigtk
= tmp
;
4098 /* "GKeyDoneStations = GNoStations" is done in more robust way by
4099 * counting the STAs that are marked with GUpdateStationKeys instead of
4100 * including all STAs that could be in not-yet-completed state. */
4101 wpa_gtk_update(wpa_auth
, group
);
4103 if (group
->GKeyDoneStations
) {
4104 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: Unexpected "
4105 "GKeyDoneStations=%d when starting new GTK rekey",
4106 group
->GKeyDoneStations
);
4107 group
->GKeyDoneStations
= 0;
4109 wpa_auth_for_each_sta(wpa_auth
, wpa_group_update_sta
, group
);
4110 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: GKeyDoneStations=%d",
4111 group
->GKeyDoneStations
);
4115 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
4116 struct wpa_group
*group
)
4120 if (wpa_auth_set_key(wpa_auth
, group
->vlan_id
,
4121 wpa_cipher_to_alg(wpa_auth
->conf
.wpa_group
),
4122 broadcast_ether_addr
, group
->GN
,
4123 group
->GTK
[group
->GN
- 1], group
->GTK_len
,
4124 KEY_FLAG_GROUP_TX_DEFAULT
) < 0)
4127 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
4131 alg
= wpa_cipher_to_alg(wpa_auth
->conf
.group_mgmt_cipher
);
4132 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
4135 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, alg
,
4136 broadcast_ether_addr
, group
->GN_igtk
,
4137 group
->IGTK
[group
->GN_igtk
- 4], len
,
4138 KEY_FLAG_GROUP_TX_DEFAULT
) < 0)
4141 if (ret
== 0 && wpa_auth
->conf
.beacon_prot
&&
4142 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, alg
,
4143 broadcast_ether_addr
, group
->GN_bigtk
,
4144 group
->BIGTK
[group
->GN_bigtk
- 6], len
,
4145 KEY_FLAG_GROUP_TX_DEFAULT
) < 0)
4153 static int wpa_group_disconnect_cb(struct wpa_state_machine
*sm
, void *ctx
)
4155 if (sm
->group
== ctx
) {
4156 wpa_printf(MSG_DEBUG
, "WPA: Mark STA " MACSTR
4157 " for discconnection due to fatal failure",
4159 sm
->Disconnect
= TRUE
;
4166 static void wpa_group_fatal_failure(struct wpa_authenticator
*wpa_auth
,
4167 struct wpa_group
*group
)
4169 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state FATAL_FAILURE");
4170 group
->changed
= TRUE
;
4171 group
->wpa_group_state
= WPA_GROUP_FATAL_FAILURE
;
4172 wpa_auth_for_each_sta(wpa_auth
, wpa_group_disconnect_cb
, group
);
4176 static int wpa_group_setkeysdone(struct wpa_authenticator
*wpa_auth
,
4177 struct wpa_group
*group
)
4179 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
4180 "SETKEYSDONE (VLAN-ID %d)", group
->vlan_id
);
4181 group
->changed
= TRUE
;
4182 group
->wpa_group_state
= WPA_GROUP_SETKEYSDONE
;
4184 if (wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
4185 wpa_group_fatal_failure(wpa_auth
, group
);
4193 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
4194 struct wpa_group
*group
)
4197 wpa_group_gtk_init(wpa_auth
, group
);
4198 } else if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
) {
4199 /* Do not allow group operations */
4200 } else if (group
->wpa_group_state
== WPA_GROUP_GTK_INIT
&&
4201 group
->GTKAuthenticator
) {
4202 wpa_group_setkeysdone(wpa_auth
, group
);
4203 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
&&
4205 wpa_group_setkeys(wpa_auth
, group
);
4206 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYS
) {
4207 if (group
->GKeyDoneStations
== 0)
4208 wpa_group_setkeysdone(wpa_auth
, group
);
4209 else if (group
->GTKReKey
)
4210 wpa_group_setkeys(wpa_auth
, group
);
4215 static int wpa_sm_step(struct wpa_state_machine
*sm
)
4220 if (sm
->in_step_loop
) {
4221 /* This should not happen, but if it does, make sure we do not
4222 * end up freeing the state machine too early by exiting the
4223 * recursive call. */
4224 wpa_printf(MSG_ERROR
, "WPA: wpa_sm_step() called recursively");
4228 sm
->in_step_loop
= 1;
4230 if (sm
->pending_deinit
)
4233 sm
->changed
= FALSE
;
4234 sm
->wpa_auth
->group
->changed
= FALSE
;
4236 SM_STEP_RUN(WPA_PTK
);
4237 if (sm
->pending_deinit
)
4239 SM_STEP_RUN(WPA_PTK_GROUP
);
4240 if (sm
->pending_deinit
)
4242 wpa_group_sm_step(sm
->wpa_auth
, sm
->group
);
4243 } while (sm
->changed
|| sm
->wpa_auth
->group
->changed
);
4244 sm
->in_step_loop
= 0;
4246 if (sm
->pending_deinit
) {
4247 wpa_printf(MSG_DEBUG
, "WPA: Completing pending STA state "
4248 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
4249 wpa_free_sta_sm(sm
);
4256 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
)
4258 struct wpa_state_machine
*sm
= eloop_ctx
;
4263 void wpa_auth_sm_notify(struct wpa_state_machine
*sm
)
4267 eloop_register_timeout(0, 0, wpa_sm_call_step
, sm
, NULL
);
4271 void wpa_gtk_rekey(struct wpa_authenticator
*wpa_auth
)
4274 struct wpa_group
*group
;
4276 if (wpa_auth
== NULL
)
4279 group
= wpa_auth
->group
;
4281 for (i
= 0; i
< 2; i
++) {
4283 group
->GM
= group
->GN
;
4285 tmp
= group
->GM_igtk
;
4286 group
->GM_igtk
= group
->GN_igtk
;
4287 group
->GN_igtk
= tmp
;
4288 tmp
= group
->GM_bigtk
;
4289 group
->GM_bigtk
= group
->GN_bigtk
;
4290 group
->GN_bigtk
= tmp
;
4291 wpa_gtk_update(wpa_auth
, group
);
4292 wpa_group_config_group_keys(wpa_auth
, group
);
4297 static const char * wpa_bool_txt(int val
)
4299 return val
? "TRUE" : "FALSE";
4303 #define RSN_SUITE "%02x-%02x-%02x-%d"
4304 #define RSN_SUITE_ARG(s) \
4305 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
4307 int wpa_get_mib(struct wpa_authenticator
*wpa_auth
, char *buf
, size_t buflen
)
4310 char pmkid_txt
[PMKID_LEN
* 2 + 1];
4311 #ifdef CONFIG_RSN_PREAUTH
4312 const int preauth
= 1;
4313 #else /* CONFIG_RSN_PREAUTH */
4314 const int preauth
= 0;
4315 #endif /* CONFIG_RSN_PREAUTH */
4317 if (wpa_auth
== NULL
)
4320 ret
= os_snprintf(buf
+ len
, buflen
- len
,
4321 "dot11RSNAOptionImplemented=TRUE\n"
4322 "dot11RSNAPreauthenticationImplemented=%s\n"
4323 "dot11RSNAEnabled=%s\n"
4324 "dot11RSNAPreauthenticationEnabled=%s\n",
4325 wpa_bool_txt(preauth
),
4326 wpa_bool_txt(wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
),
4327 wpa_bool_txt(wpa_auth
->conf
.rsn_preauth
));
4328 if (os_snprintf_error(buflen
- len
, ret
))
4332 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
4333 wpa_auth
->dot11RSNAPMKIDUsed
, PMKID_LEN
);
4336 buf
+ len
, buflen
- len
,
4337 "dot11RSNAConfigVersion=%u\n"
4338 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
4339 /* FIX: dot11RSNAConfigGroupCipher */
4340 /* FIX: dot11RSNAConfigGroupRekeyMethod */
4341 /* FIX: dot11RSNAConfigGroupRekeyTime */
4342 /* FIX: dot11RSNAConfigGroupRekeyPackets */
4343 "dot11RSNAConfigGroupRekeyStrict=%u\n"
4344 "dot11RSNAConfigGroupUpdateCount=%u\n"
4345 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
4346 "dot11RSNAConfigGroupCipherSize=%u\n"
4347 "dot11RSNAConfigPMKLifetime=%u\n"
4348 "dot11RSNAConfigPMKReauthThreshold=%u\n"
4349 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4350 "dot11RSNAConfigSATimeout=%u\n"
4351 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
4352 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
4353 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
4354 "dot11RSNAPMKIDUsed=%s\n"
4355 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
4356 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
4357 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
4358 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4359 "dot11RSNA4WayHandshakeFailures=%u\n"
4360 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4362 !!wpa_auth
->conf
.wpa_strict_rekey
,
4363 wpa_auth
->conf
.wpa_group_update_count
,
4364 wpa_auth
->conf
.wpa_pairwise_update_count
,
4365 wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
) * 8,
4366 dot11RSNAConfigPMKLifetime
,
4367 dot11RSNAConfigPMKReauthThreshold
,
4368 dot11RSNAConfigSATimeout
,
4369 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteSelected
),
4370 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherSelected
),
4371 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherSelected
),
4373 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteRequested
),
4374 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherRequested
),
4375 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherRequested
),
4376 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
,
4377 wpa_auth
->dot11RSNA4WayHandshakeFailures
);
4378 if (os_snprintf_error(buflen
- len
, ret
))
4382 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
4383 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4386 ret
= os_snprintf(buf
+ len
, buflen
- len
, "hostapdWPAGroupState=%d\n",
4387 wpa_auth
->group
->wpa_group_state
);
4388 if (os_snprintf_error(buflen
- len
, ret
))
4396 int wpa_get_mib_sta(struct wpa_state_machine
*sm
, char *buf
, size_t buflen
)
4404 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4406 /* dot11RSNAStatsEntry */
4408 pairwise
= wpa_cipher_to_suite(sm
->wpa
== WPA_VERSION_WPA2
?
4409 WPA_PROTO_RSN
: WPA_PROTO_WPA
,
4415 buf
+ len
, buflen
- len
,
4416 /* TODO: dot11RSNAStatsIndex */
4417 "dot11RSNAStatsSTAAddress=" MACSTR
"\n"
4418 "dot11RSNAStatsVersion=1\n"
4419 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE
"\n"
4420 /* TODO: dot11RSNAStatsTKIPICVErrors */
4421 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
4422 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
4423 /* TODO: dot11RSNAStatsCCMPReplays */
4424 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
4425 /* TODO: dot11RSNAStatsTKIPReplays */,
4427 RSN_SUITE_ARG(pairwise
),
4428 sm
->dot11RSNAStatsTKIPLocalMICFailures
,
4429 sm
->dot11RSNAStatsTKIPRemoteMICFailures
);
4430 if (os_snprintf_error(buflen
- len
, ret
))
4435 ret
= os_snprintf(buf
+ len
, buflen
- len
,
4437 "AKMSuiteSelector=" RSN_SUITE
"\n"
4438 "hostapdWPAPTKState=%d\n"
4439 "hostapdWPAPTKGroupState=%d\n",
4441 RSN_SUITE_ARG(wpa_akm_to_suite(sm
->wpa_key_mgmt
)),
4443 sm
->wpa_ptk_group_state
);
4444 if (os_snprintf_error(buflen
- len
, ret
))
4452 void wpa_auth_countermeasures_start(struct wpa_authenticator
*wpa_auth
)
4455 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
++;
4459 int wpa_auth_pairwise_set(struct wpa_state_machine
*sm
)
4461 return sm
&& sm
->pairwise_set
;
4465 int wpa_auth_get_pairwise(struct wpa_state_machine
*sm
)
4467 return sm
->pairwise
;
4471 const u8
* wpa_auth_get_pmk(struct wpa_state_machine
*sm
, int *len
)
4480 int wpa_auth_sta_key_mgmt(struct wpa_state_machine
*sm
)
4484 return sm
->wpa_key_mgmt
;
4488 int wpa_auth_sta_wpa_version(struct wpa_state_machine
*sm
)
4496 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine
*sm
)
4498 if (!sm
|| !wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
4500 return sm
->tk_already_set
;
4504 int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine
*sm
)
4506 if (!sm
|| !wpa_key_mgmt_fils(sm
->wpa_key_mgmt
))
4508 return sm
->tk_already_set
;
4512 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine
*sm
,
4513 struct rsn_pmksa_cache_entry
*entry
)
4515 if (sm
== NULL
|| sm
->pmksa
!= entry
)
4522 struct rsn_pmksa_cache_entry
*
4523 wpa_auth_sta_get_pmksa(struct wpa_state_machine
*sm
)
4525 return sm
? sm
->pmksa
: NULL
;
4529 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine
*sm
)
4532 sm
->dot11RSNAStatsTKIPLocalMICFailures
++;
4536 const u8
* wpa_auth_get_wpa_ie(struct wpa_authenticator
*wpa_auth
, size_t *len
)
4538 if (wpa_auth
== NULL
)
4540 *len
= wpa_auth
->wpa_ie_len
;
4541 return wpa_auth
->wpa_ie
;
4545 int wpa_auth_pmksa_add(struct wpa_state_machine
*sm
, const u8
*pmk
,
4546 unsigned int pmk_len
,
4547 int session_timeout
, struct eapol_state_machine
*eapol
)
4549 if (sm
== NULL
|| sm
->wpa
!= WPA_VERSION_WPA2
||
4550 sm
->wpa_auth
->conf
.disable_pmksa_caching
)
4553 #ifdef CONFIG_IEEE80211R_AP
4554 if (pmk_len
>= 2 * PMK_LEN
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
) &&
4555 wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
4556 !wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
4557 /* Cache MPMK/XXKey instead of initial part from MSK */
4558 pmk
= pmk
+ PMK_LEN
;
4561 #endif /* CONFIG_IEEE80211R_AP */
4562 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
4563 if (pmk_len
> PMK_LEN_SUITE_B_192
)
4564 pmk_len
= PMK_LEN_SUITE_B_192
;
4565 } else if (pmk_len
> PMK_LEN
) {
4569 wpa_hexdump_key(MSG_DEBUG
, "RSN: Cache PMK", pmk
, pmk_len
);
4570 if (pmksa_cache_auth_add(sm
->wpa_auth
->pmksa
, pmk
, pmk_len
, NULL
,
4571 sm
->PTK
.kck
, sm
->PTK
.kck_len
,
4572 sm
->wpa_auth
->addr
, sm
->addr
, session_timeout
,
4573 eapol
, sm
->wpa_key_mgmt
))
4580 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator
*wpa_auth
,
4581 const u8
*pmk
, size_t len
, const u8
*sta_addr
,
4582 int session_timeout
,
4583 struct eapol_state_machine
*eapol
)
4585 if (wpa_auth
== NULL
)
4588 wpa_hexdump_key(MSG_DEBUG
, "RSN: Cache PMK from preauth", pmk
, len
);
4589 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, len
, NULL
,
4592 sta_addr
, session_timeout
, eapol
,
4593 WPA_KEY_MGMT_IEEE8021X
))
4600 int wpa_auth_pmksa_add_sae(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4601 const u8
*pmk
, const u8
*pmkid
)
4603 if (wpa_auth
->conf
.disable_pmksa_caching
)
4606 wpa_hexdump_key(MSG_DEBUG
, "RSN: Cache PMK from SAE", pmk
, PMK_LEN
);
4607 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, PMK_LEN
, pmkid
,
4609 wpa_auth
->addr
, addr
, 0, NULL
,
4617 void wpa_auth_add_sae_pmkid(struct wpa_state_machine
*sm
, const u8
*pmkid
)
4619 os_memcpy(sm
->pmkid
, pmkid
, PMKID_LEN
);
4624 int wpa_auth_pmksa_add2(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4625 const u8
*pmk
, size_t pmk_len
, const u8
*pmkid
,
4626 int session_timeout
, int akmp
)
4628 if (wpa_auth
->conf
.disable_pmksa_caching
)
4631 wpa_hexdump_key(MSG_DEBUG
, "RSN: Cache PMK (2)", pmk
, PMK_LEN
);
4632 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, pmk_len
, pmkid
,
4633 NULL
, 0, wpa_auth
->addr
, addr
, session_timeout
,
4641 void wpa_auth_pmksa_remove(struct wpa_authenticator
*wpa_auth
,
4644 struct rsn_pmksa_cache_entry
*pmksa
;
4646 if (wpa_auth
== NULL
|| wpa_auth
->pmksa
== NULL
)
4648 pmksa
= pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, NULL
);
4650 wpa_printf(MSG_DEBUG
, "WPA: Remove PMKSA cache entry for "
4651 MACSTR
" based on request", MAC2STR(sta_addr
));
4652 pmksa_cache_free_entry(wpa_auth
->pmksa
, pmksa
);
4657 int wpa_auth_pmksa_list(struct wpa_authenticator
*wpa_auth
, char *buf
,
4660 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4662 return pmksa_cache_auth_list(wpa_auth
->pmksa
, buf
, len
);
4666 void wpa_auth_pmksa_flush(struct wpa_authenticator
*wpa_auth
)
4668 if (wpa_auth
&& wpa_auth
->pmksa
)
4669 pmksa_cache_auth_flush(wpa_auth
->pmksa
);
4673 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4676 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4677 char *buf
, size_t len
)
4679 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4682 return pmksa_cache_auth_list_mesh(wpa_auth
->pmksa
, addr
, buf
, len
);
4686 struct rsn_pmksa_cache_entry
*
4687 wpa_auth_pmksa_create_entry(const u8
*aa
, const u8
*spa
, const u8
*pmk
,
4688 const u8
*pmkid
, int expiration
)
4690 struct rsn_pmksa_cache_entry
*entry
;
4691 struct os_reltime now
;
4693 entry
= pmksa_cache_auth_create_entry(pmk
, PMK_LEN
, pmkid
, NULL
, 0, aa
,
4694 spa
, 0, NULL
, WPA_KEY_MGMT_SAE
);
4698 os_get_reltime(&now
);
4699 entry
->expiration
= now
.sec
+ expiration
;
4704 int wpa_auth_pmksa_add_entry(struct wpa_authenticator
*wpa_auth
,
4705 struct rsn_pmksa_cache_entry
*entry
)
4709 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4712 ret
= pmksa_cache_auth_add_entry(wpa_auth
->pmksa
, entry
);
4714 wpa_printf(MSG_DEBUG
,
4715 "RSN: Failed to store external PMKSA cache for "
4716 MACSTR
, MAC2STR(entry
->spa
));
4721 #endif /* CONFIG_MESH */
4722 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4725 struct rsn_pmksa_cache_entry
*
4726 wpa_auth_pmksa_get(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
,
4729 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4731 return pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, pmkid
);
4735 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry
*pmksa
,
4736 struct wpa_state_machine
*sm
,
4737 struct wpa_authenticator
*wpa_auth
,
4744 os_memcpy(pmk
, pmksa
->pmk
, PMK_LEN
);
4745 os_memcpy(pmkid
, pmksa
->pmkid
, PMKID_LEN
);
4746 os_memcpy(wpa_auth
->dot11RSNAPMKIDUsed
, pmksa
->pmkid
, PMKID_LEN
);
4751 * Remove and free the group from wpa_authenticator. This is triggered by a
4752 * callback to make sure nobody is currently iterating the group list while it
4755 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
4756 struct wpa_group
*group
)
4758 struct wpa_group
*prev
= wpa_auth
->group
;
4760 wpa_printf(MSG_DEBUG
, "WPA: Remove group state machine for VLAN-ID %d",
4764 if (prev
->next
== group
) {
4765 /* This never frees the special first group as needed */
4766 prev
->next
= group
->next
;
4776 /* Increase the reference counter for group */
4777 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
4778 struct wpa_group
*group
)
4780 /* Skip the special first group */
4781 if (wpa_auth
->group
== group
)
4784 group
->references
++;
4788 /* Decrease the reference counter and maybe free the group */
4789 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
4790 struct wpa_group
*group
)
4792 /* Skip the special first group */
4793 if (wpa_auth
->group
== group
)
4796 group
->references
--;
4797 if (group
->references
)
4799 wpa_group_free(wpa_auth
, group
);
4804 * Add a group that has its references counter set to zero. Caller needs to
4805 * call wpa_group_get() on the return value to mark the entry in use.
4807 static struct wpa_group
*
4808 wpa_auth_add_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4810 struct wpa_group
*group
;
4812 if (wpa_auth
== NULL
|| wpa_auth
->group
== NULL
)
4815 wpa_printf(MSG_DEBUG
, "WPA: Add group state machine for VLAN-ID %d",
4817 group
= wpa_group_init(wpa_auth
, vlan_id
, 0);
4821 group
->next
= wpa_auth
->group
->next
;
4822 wpa_auth
->group
->next
= group
;
4829 * Enforce that the group state machine for the VLAN is running, increase
4830 * reference counter as interface is up. References might have been increased
4831 * even if a negative value is returned.
4832 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4834 int wpa_auth_ensure_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4836 struct wpa_group
*group
;
4838 if (wpa_auth
== NULL
)
4841 group
= wpa_auth
->group
;
4843 if (group
->vlan_id
== vlan_id
)
4845 group
= group
->next
;
4848 if (group
== NULL
) {
4849 group
= wpa_auth_add_group(wpa_auth
, vlan_id
);
4854 wpa_printf(MSG_DEBUG
,
4855 "WPA: Ensure group state machine running for VLAN ID %d",
4858 wpa_group_get(wpa_auth
, group
);
4859 group
->num_setup_iface
++;
4861 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4869 * Decrease reference counter, expected to be zero afterwards.
4870 * returns: -1 on error (group not found, group in fail state)
4871 * -2 if wpa_group is still referenced
4874 int wpa_auth_release_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4876 struct wpa_group
*group
;
4879 if (wpa_auth
== NULL
)
4882 group
= wpa_auth
->group
;
4884 if (group
->vlan_id
== vlan_id
)
4886 group
= group
->next
;
4892 wpa_printf(MSG_DEBUG
,
4893 "WPA: Try stopping group state machine for VLAN ID %d",
4896 if (group
->num_setup_iface
<= 0) {
4897 wpa_printf(MSG_ERROR
,
4898 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4902 group
->num_setup_iface
--;
4904 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4907 if (group
->references
> 1) {
4908 wpa_printf(MSG_DEBUG
,
4909 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4914 wpa_group_put(wpa_auth
, group
);
4920 int wpa_auth_sta_set_vlan(struct wpa_state_machine
*sm
, int vlan_id
)
4922 struct wpa_group
*group
;
4924 if (sm
== NULL
|| sm
->wpa_auth
== NULL
)
4927 group
= sm
->wpa_auth
->group
;
4929 if (group
->vlan_id
== vlan_id
)
4931 group
= group
->next
;
4934 if (group
== NULL
) {
4935 group
= wpa_auth_add_group(sm
->wpa_auth
, vlan_id
);
4940 if (sm
->group
== group
)
4943 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4946 wpa_printf(MSG_DEBUG
, "WPA: Moving STA " MACSTR
" to use group state "
4947 "machine for VLAN ID %d", MAC2STR(sm
->addr
), vlan_id
);
4949 wpa_group_get(sm
->wpa_auth
, group
);
4950 wpa_group_put(sm
->wpa_auth
, sm
->group
);
4957 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator
*wpa_auth
,
4958 struct wpa_state_machine
*sm
, int ack
)
4960 if (wpa_auth
== NULL
|| sm
== NULL
)
4962 wpa_printf(MSG_DEBUG
, "WPA: EAPOL-Key TX status for STA " MACSTR
4963 " ack=%d", MAC2STR(sm
->addr
), ack
);
4964 if (sm
->pending_1_of_4_timeout
&& ack
) {
4966 * Some deployed supplicant implementations update their SNonce
4967 * for each EAPOL-Key 2/4 message even within the same 4-way
4968 * handshake and then fail to use the first SNonce when
4969 * deriving the PTK. This results in unsuccessful 4-way
4970 * handshake whenever the relatively short initial timeout is
4971 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4972 * around this by increasing the timeout now that we know that
4973 * the station has received the frame.
4975 int timeout_ms
= eapol_key_timeout_subseq
;
4976 wpa_printf(MSG_DEBUG
, "WPA: Increase initial EAPOL-Key 1/4 "
4977 "timeout by %u ms because of acknowledged frame",
4979 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
4980 eloop_register_timeout(timeout_ms
/ 1000,
4981 (timeout_ms
% 1000) * 1000,
4982 wpa_send_eapol_timeout
, wpa_auth
, sm
);
4985 #ifdef CONFIG_TESTING_OPTIONS
4986 if (sm
->eapol_status_cb
) {
4987 sm
->eapol_status_cb(sm
->eapol_status_cb_ctx1
,
4988 sm
->eapol_status_cb_ctx2
);
4989 sm
->eapol_status_cb
= NULL
;
4991 #endif /* CONFIG_TESTING_OPTIONS */
4995 int wpa_auth_uses_sae(struct wpa_state_machine
*sm
)
4999 return wpa_key_mgmt_sae(sm
->wpa_key_mgmt
);
5003 int wpa_auth_uses_ft_sae(struct wpa_state_machine
*sm
)
5007 return sm
->wpa_key_mgmt
== WPA_KEY_MGMT_FT_SAE
;
5012 int wpa_auth_get_ip_addr(struct wpa_state_machine
*sm
, u8
*addr
)
5014 if (sm
== NULL
|| WPA_GET_BE32(sm
->ip_addr
) == 0)
5016 os_memcpy(addr
, sm
->ip_addr
, 4);
5019 #endif /* CONFIG_P2P */
5022 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator
*wpa_auth
,
5023 struct radius_das_attrs
*attr
)
5025 return pmksa_cache_auth_radius_das_disconnect(wpa_auth
->pmksa
, attr
);
5029 void wpa_auth_reconfig_group_keys(struct wpa_authenticator
*wpa_auth
)
5031 struct wpa_group
*group
;
5035 for (group
= wpa_auth
->group
; group
; group
= group
->next
)
5036 wpa_group_config_group_keys(wpa_auth
, group
);
5042 struct wpa_auth_fils_iter_data
{
5043 struct wpa_authenticator
*auth
;
5045 struct rsn_pmksa_cache_entry
*pmksa
;
5051 static int wpa_auth_fils_iter(struct wpa_authenticator
*a
, void *ctx
)
5053 struct wpa_auth_fils_iter_data
*data
= ctx
;
5055 if (a
== data
->auth
|| !a
->conf
.fils_cache_id_set
||
5056 os_memcmp(a
->conf
.fils_cache_id
, data
->cache_id
,
5057 FILS_CACHE_ID_LEN
) != 0)
5059 data
->pmksa
= pmksa_cache_auth_get(a
->pmksa
, data
->spa
, data
->pmkid
);
5060 return data
->pmksa
!= NULL
;
5064 struct rsn_pmksa_cache_entry
*
5065 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator
*wpa_auth
,
5066 const u8
*sta_addr
, const u8
*pmkid
)
5068 struct wpa_auth_fils_iter_data idata
;
5070 if (!wpa_auth
->conf
.fils_cache_id_set
)
5072 idata
.auth
= wpa_auth
;
5073 idata
.cache_id
= wpa_auth
->conf
.fils_cache_id
;
5075 idata
.spa
= sta_addr
;
5076 idata
.pmkid
= pmkid
;
5077 wpa_auth_for_each_auth(wpa_auth
, wpa_auth_fils_iter
, &idata
);
5082 #ifdef CONFIG_IEEE80211R_AP
5083 int wpa_auth_write_fte(struct wpa_authenticator
*wpa_auth
, int use_sha384
,
5084 u8
*buf
, size_t len
)
5086 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
5088 return wpa_write_ftie(conf
, use_sha384
, conf
->r0_key_holder
,
5089 conf
->r0_key_holder_len
,
5090 NULL
, NULL
, buf
, len
, NULL
, 0);
5092 #endif /* CONFIG_IEEE80211R_AP */
5095 void wpa_auth_get_fils_aead_params(struct wpa_state_machine
*sm
,
5096 u8
*fils_anonce
, u8
*fils_snonce
,
5097 u8
*fils_kek
, size_t *fils_kek_len
)
5099 os_memcpy(fils_anonce
, sm
->ANonce
, WPA_NONCE_LEN
);
5100 os_memcpy(fils_snonce
, sm
->SNonce
, WPA_NONCE_LEN
);
5101 os_memcpy(fils_kek
, sm
->PTK
.kek
, WPA_KEK_MAX_LEN
);
5102 *fils_kek_len
= sm
->PTK
.kek_len
;
5106 void wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine
*sm
, const u8
*pmk
,
5107 size_t pmk_len
, const u8
*pmkid
)
5109 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
5110 sm
->pmk_len
= pmk_len
;
5111 os_memcpy(sm
->pmkid
, pmkid
, PMKID_LEN
);
5115 #endif /* CONFIG_FILS */
5118 void wpa_auth_set_auth_alg(struct wpa_state_machine
*sm
, u16 auth_alg
)
5121 sm
->auth_alg
= auth_alg
;
5126 void wpa_auth_set_dpp_z(struct wpa_state_machine
*sm
, const struct wpabuf
*z
)
5129 wpabuf_clear_free(sm
->dpp_z
);
5130 sm
->dpp_z
= z
? wpabuf_dup(z
) : NULL
;
5133 #endif /* CONFIG_DPP2 */
5136 #ifdef CONFIG_TESTING_OPTIONS
5138 int wpa_auth_resend_m1(struct wpa_state_machine
*sm
, int change_anonce
,
5139 void (*cb
)(void *ctx1
, void *ctx2
),
5140 void *ctx1
, void *ctx2
)
5142 const u8
*anonce
= sm
->ANonce
;
5143 u8 anonce_buf
[WPA_NONCE_LEN
];
5145 if (change_anonce
) {
5146 if (random_get_bytes(anonce_buf
, WPA_NONCE_LEN
))
5148 anonce
= anonce_buf
;
5151 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5152 "sending 1/4 msg of 4-Way Handshake (TESTING)");
5153 wpa_send_eapol(sm
->wpa_auth
, sm
,
5154 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
5155 anonce
, NULL
, 0, 0, 0);
5160 int wpa_auth_resend_m3(struct wpa_state_machine
*sm
,
5161 void (*cb
)(void *ctx1
, void *ctx2
),
5162 void *ctx1
, void *ctx2
)
5164 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
;
5166 size_t gtk_len
, kde_len
;
5167 struct wpa_group
*gsm
= sm
->group
;
5169 int wpa_ie_len
, secure
, gtkidx
, encr
= 0;
5171 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
5172 GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2])
5176 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
5177 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
5178 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
5179 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
5180 if (sm
->wpa
== WPA_VERSION_WPA
&&
5181 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
5182 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
5183 /* WPA-only STA, remove RSN IE and possible MDIE */
5184 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
5185 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
5186 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
5187 wpa_ie_len
= wpa_ie
[1] + 2;
5189 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5190 "sending 3/4 msg of 4-Way Handshake (TESTING)");
5191 if (sm
->wpa
== WPA_VERSION_WPA2
) {
5192 /* WPA2 send GTK in the 4-way handshake */
5194 gtk
= gsm
->GTK
[gsm
->GN
- 1];
5195 gtk_len
= gsm
->GTK_len
;
5200 /* WPA does not include GTK in msg 3/4 */
5205 if (sm
->rx_eapol_key_secure
) {
5207 * It looks like Windows 7 supplicant tries to use
5208 * Secure bit in msg 2/4 after having reported Michael
5209 * MIC failure and it then rejects the 4-way handshake
5210 * if msg 3/4 does not set Secure bit. Work around this
5211 * by setting the Secure bit here even in the case of
5212 * WPA if the supplicant used it first.
5214 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5215 "STA used Secure bit in WPA msg 2/4 - "
5216 "set Secure for 3/4 as workaround");
5221 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
5223 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
5224 #ifdef CONFIG_IEEE80211R_AP
5225 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
5226 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
5227 kde_len
+= 300; /* FTIE + 2 * TIE */
5229 #endif /* CONFIG_IEEE80211R_AP */
5230 kde
= os_malloc(kde_len
);
5235 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
5237 #ifdef CONFIG_IEEE80211R_AP
5238 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
5243 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
5245 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
5246 "PMKR1Name into RSN IE in EAPOL-Key data");
5253 #endif /* CONFIG_IEEE80211R_AP */
5256 hdr
[0] = gtkidx
& 0x03;
5258 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
5262 pos
= ieee80211w_kde_add(sm
, pos
);
5263 if (pos
- opos
>= 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
) {
5264 /* skip KDE header and keyid */
5265 opos
+= 2 + RSN_SELECTOR_LEN
+ 2;
5266 os_memset(opos
, 0, 6); /* clear PN */
5268 if (ocv_oci_add(sm
, &pos
) < 0) {
5273 #ifdef CONFIG_IEEE80211R_AP
5274 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
5276 struct wpa_auth_config
*conf
;
5278 conf
= &sm
->wpa_auth
->conf
;
5279 if (sm
->assoc_resp_ftie
&&
5280 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
5281 os_memcpy(pos
, sm
->assoc_resp_ftie
,
5282 2 + sm
->assoc_resp_ftie
[1]);
5283 res
= 2 + sm
->assoc_resp_ftie
[1];
5285 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
5287 res
= wpa_write_ftie(conf
, use_sha384
,
5288 conf
->r0_key_holder
,
5289 conf
->r0_key_holder_len
,
5291 kde
+ kde_len
- pos
,
5295 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
5296 "into EAPOL-Key Key Data");
5302 /* TIE[ReassociationDeadline] (TU) */
5303 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
5305 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
5306 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
5309 /* TIE[KeyLifetime] (seconds) */
5310 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
5312 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
5313 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
);
5316 #endif /* CONFIG_IEEE80211R_AP */
5318 wpa_send_eapol(sm
->wpa_auth
, sm
,
5319 (secure
? WPA_KEY_INFO_SECURE
: 0) |
5320 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
5321 WPA_KEY_INFO_MIC
: 0) |
5322 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
5323 WPA_KEY_INFO_KEY_TYPE
,
5324 _rsc
, sm
->ANonce
, kde
, pos
- kde
, 0, encr
);
5330 int wpa_auth_resend_group_m1(struct wpa_state_machine
*sm
,
5331 void (*cb
)(void *ctx1
, void *ctx2
),
5332 void *ctx1
, void *ctx2
)
5334 u8 rsc
[WPA_KEY_RSC_LEN
];
5335 struct wpa_group
*gsm
= sm
->group
;
5337 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
5342 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
5343 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
5345 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5346 "sending 1/2 msg of Group Key Handshake (TESTING)");
5348 gtk
= gsm
->GTK
[gsm
->GN
- 1];
5349 if (sm
->wpa
== WPA_VERSION_WPA2
) {
5350 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
5351 ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
5352 kde_buf
= os_malloc(kde_len
);
5353 if (kde_buf
== NULL
)
5356 kde
= pos
= kde_buf
;
5357 hdr
[0] = gsm
->GN
& 0x03;
5359 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
5362 pos
= ieee80211w_kde_add(sm
, pos
);
5364 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
) {
5365 /* skip KDE header and keyid */
5366 opos
+= 2 + RSN_SELECTOR_LEN
+ 2;
5367 os_memset(opos
, 0, 6); /* clear PN */
5369 if (ocv_oci_add(sm
, &pos
) < 0) {
5373 kde_len
= pos
- kde
;
5376 kde_len
= gsm
->GTK_len
;
5379 sm
->eapol_status_cb
= cb
;
5380 sm
->eapol_status_cb_ctx1
= ctx1
;
5381 sm
->eapol_status_cb_ctx2
= ctx2
;
5383 wpa_send_eapol(sm
->wpa_auth
, sm
,
5384 WPA_KEY_INFO_SECURE
|
5385 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
5386 WPA_KEY_INFO_MIC
: 0) |
5388 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
5389 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
5396 int wpa_auth_rekey_gtk(struct wpa_authenticator
*wpa_auth
)
5400 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
5401 return eloop_register_timeout(0, 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
5404 #endif /* CONFIG_TESTING_OPTIONS */