2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/state_machine.h"
14 #include "utils/bitfield.h"
15 #include "common/ieee802_11_defs.h"
16 #include "crypto/aes.h"
17 #include "crypto/aes_wrap.h"
18 #include "crypto/aes_siv.h"
19 #include "crypto/crypto.h"
20 #include "crypto/sha1.h"
21 #include "crypto/sha256.h"
22 #include "crypto/sha384.h"
23 #include "crypto/random.h"
24 #include "eapol_auth/eapol_auth_sm.h"
25 #include "ap_config.h"
26 #include "ieee802_11.h"
28 #include "pmksa_cache_auth.h"
29 #include "wpa_auth_i.h"
30 #include "wpa_auth_ie.h"
32 #define STATE_MACHINE_DATA struct wpa_state_machine
33 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
34 #define STATE_MACHINE_ADDR sm->addr
37 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
);
38 static int wpa_sm_step(struct wpa_state_machine
*sm
);
39 static int wpa_verify_key_mic(int akmp
, size_t pmk_len
, struct wpa_ptk
*PTK
,
40 u8
*data
, size_t data_len
);
42 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
43 u8
*buf
, size_t buf_len
, u16
*_key_data_len
);
44 static struct wpabuf
* fils_prepare_plainbuf(struct wpa_state_machine
*sm
,
45 const struct wpabuf
*hlp
);
46 #endif /* CONFIG_FILS */
47 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
);
48 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
49 struct wpa_group
*group
);
50 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
);
51 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
52 struct wpa_group
*group
);
53 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
54 struct wpa_group
*group
);
55 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
56 const u8
*pmk
, unsigned int pmk_len
,
58 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
59 struct wpa_group
*group
);
60 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
61 struct wpa_group
*group
);
62 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
63 struct wpa_group
*group
);
64 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
);
66 static const u32 eapol_key_timeout_first
= 100; /* ms */
67 static const u32 eapol_key_timeout_subseq
= 1000; /* ms */
68 static const u32 eapol_key_timeout_first_group
= 500; /* ms */
69 static const u32 eapol_key_timeout_no_retrans
= 4000; /* ms */
71 /* TODO: make these configurable */
72 static const int dot11RSNAConfigPMKLifetime
= 43200;
73 static const int dot11RSNAConfigPMKReauthThreshold
= 70;
74 static const int dot11RSNAConfigSATimeout
= 60;
77 static inline int wpa_auth_mic_failure_report(
78 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
80 if (wpa_auth
->cb
->mic_failure_report
)
81 return wpa_auth
->cb
->mic_failure_report(wpa_auth
->cb_ctx
, addr
);
86 static inline void wpa_auth_psk_failure_report(
87 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
89 if (wpa_auth
->cb
->psk_failure_report
)
90 wpa_auth
->cb
->psk_failure_report(wpa_auth
->cb_ctx
, addr
);
94 static inline void wpa_auth_set_eapol(struct wpa_authenticator
*wpa_auth
,
95 const u8
*addr
, wpa_eapol_variable var
,
98 if (wpa_auth
->cb
->set_eapol
)
99 wpa_auth
->cb
->set_eapol(wpa_auth
->cb_ctx
, addr
, var
, value
);
103 static inline int wpa_auth_get_eapol(struct wpa_authenticator
*wpa_auth
,
104 const u8
*addr
, wpa_eapol_variable var
)
106 if (wpa_auth
->cb
->get_eapol
== NULL
)
108 return wpa_auth
->cb
->get_eapol(wpa_auth
->cb_ctx
, addr
, var
);
112 static inline const u8
* wpa_auth_get_psk(struct wpa_authenticator
*wpa_auth
,
114 const u8
*p2p_dev_addr
,
115 const u8
*prev_psk
, size_t *psk_len
)
117 if (wpa_auth
->cb
->get_psk
== NULL
)
119 return wpa_auth
->cb
->get_psk(wpa_auth
->cb_ctx
, addr
, p2p_dev_addr
,
124 static inline int wpa_auth_get_msk(struct wpa_authenticator
*wpa_auth
,
125 const u8
*addr
, u8
*msk
, size_t *len
)
127 if (wpa_auth
->cb
->get_msk
== NULL
)
129 return wpa_auth
->cb
->get_msk(wpa_auth
->cb_ctx
, addr
, msk
, len
);
133 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
135 enum wpa_alg alg
, const u8
*addr
, int idx
,
136 u8
*key
, size_t key_len
)
138 if (wpa_auth
->cb
->set_key
== NULL
)
140 return wpa_auth
->cb
->set_key(wpa_auth
->cb_ctx
, vlan_id
, alg
, addr
, idx
,
145 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
146 const u8
*addr
, int idx
, u8
*seq
)
148 if (wpa_auth
->cb
->get_seqnum
== NULL
)
150 return wpa_auth
->cb
->get_seqnum(wpa_auth
->cb_ctx
, addr
, idx
, seq
);
155 wpa_auth_send_eapol(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
156 const u8
*data
, size_t data_len
, int encrypt
)
158 if (wpa_auth
->cb
->send_eapol
== NULL
)
160 return wpa_auth
->cb
->send_eapol(wpa_auth
->cb_ctx
, addr
, data
, data_len
,
166 static inline int wpa_auth_start_ampe(struct wpa_authenticator
*wpa_auth
,
169 if (wpa_auth
->cb
->start_ampe
== NULL
)
171 return wpa_auth
->cb
->start_ampe(wpa_auth
->cb_ctx
, addr
);
173 #endif /* CONFIG_MESH */
176 int wpa_auth_for_each_sta(struct wpa_authenticator
*wpa_auth
,
177 int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
180 if (wpa_auth
->cb
->for_each_sta
== NULL
)
182 return wpa_auth
->cb
->for_each_sta(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
186 int wpa_auth_for_each_auth(struct wpa_authenticator
*wpa_auth
,
187 int (*cb
)(struct wpa_authenticator
*a
, void *ctx
),
190 if (wpa_auth
->cb
->for_each_auth
== NULL
)
192 return wpa_auth
->cb
->for_each_auth(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
196 void wpa_auth_logger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
197 logger_level level
, const char *txt
)
199 if (wpa_auth
->cb
->logger
== NULL
)
201 wpa_auth
->cb
->logger(wpa_auth
->cb_ctx
, addr
, level
, txt
);
205 void wpa_auth_vlogger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
206 logger_level level
, const char *fmt
, ...)
212 if (wpa_auth
->cb
->logger
== NULL
)
215 maxlen
= os_strlen(fmt
) + 100;
216 format
= os_malloc(maxlen
);
221 vsnprintf(format
, maxlen
, fmt
, ap
);
224 wpa_auth_logger(wpa_auth
, addr
, level
, format
);
230 static void wpa_sta_disconnect(struct wpa_authenticator
*wpa_auth
,
231 const u8
*addr
, u16 reason
)
233 if (wpa_auth
->cb
->disconnect
== NULL
)
235 wpa_printf(MSG_DEBUG
, "wpa_sta_disconnect STA " MACSTR
" (reason %u)",
236 MAC2STR(addr
), reason
);
237 wpa_auth
->cb
->disconnect(wpa_auth
->cb_ctx
, addr
, reason
);
241 static int wpa_use_aes_cmac(struct wpa_state_machine
*sm
)
244 #ifdef CONFIG_IEEE80211R_AP
245 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
247 #endif /* CONFIG_IEEE80211R_AP */
248 #ifdef CONFIG_IEEE80211W
249 if (wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
))
251 #endif /* CONFIG_IEEE80211W */
252 if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
)
258 static void wpa_rekey_gmk(void *eloop_ctx
, void *timeout_ctx
)
260 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
262 if (random_get_bytes(wpa_auth
->group
->GMK
, WPA_GMK_LEN
)) {
263 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
266 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "GMK rekeyd");
267 wpa_hexdump_key(MSG_DEBUG
, "GMK",
268 wpa_auth
->group
->GMK
, WPA_GMK_LEN
);
271 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
272 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
273 wpa_rekey_gmk
, wpa_auth
, NULL
);
278 static void wpa_rekey_gtk(void *eloop_ctx
, void *timeout_ctx
)
280 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
281 struct wpa_group
*group
, *next
;
283 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "rekeying GTK");
284 group
= wpa_auth
->group
;
286 wpa_group_get(wpa_auth
, group
);
288 group
->GTKReKey
= TRUE
;
290 group
->changed
= FALSE
;
291 wpa_group_sm_step(wpa_auth
, group
);
292 } while (group
->changed
);
295 wpa_group_put(wpa_auth
, group
);
299 if (wpa_auth
->conf
.wpa_group_rekey
) {
300 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
,
301 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
306 static void wpa_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
308 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
309 struct wpa_state_machine
*sm
= timeout_ctx
;
311 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "rekeying PTK");
312 wpa_request_new_ptk(sm
);
317 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine
*sm
, void *ctx
)
319 if (sm
->pmksa
== ctx
)
325 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
328 struct wpa_authenticator
*wpa_auth
= ctx
;
329 wpa_auth_for_each_sta(wpa_auth
, wpa_auth_pmksa_clear_cb
, entry
);
333 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator
*wpa_auth
,
334 struct wpa_group
*group
)
336 u8 buf
[ETH_ALEN
+ 8 + sizeof(unsigned long)];
340 if (random_get_bytes(group
->GMK
, WPA_GMK_LEN
) < 0)
342 wpa_hexdump_key(MSG_DEBUG
, "GMK", group
->GMK
, WPA_GMK_LEN
);
345 * Counter = PRF-256(Random number, "Init Counter",
346 * Local MAC Address || Time)
348 os_memcpy(buf
, wpa_auth
->addr
, ETH_ALEN
);
349 wpa_get_ntp_timestamp(buf
+ ETH_ALEN
);
350 ptr
= (unsigned long) group
;
351 os_memcpy(buf
+ ETH_ALEN
+ 8, &ptr
, sizeof(ptr
));
352 if (random_get_bytes(rkey
, sizeof(rkey
)) < 0)
355 if (sha1_prf(rkey
, sizeof(rkey
), "Init Counter", buf
, sizeof(buf
),
356 group
->Counter
, WPA_NONCE_LEN
) < 0)
358 wpa_hexdump_key(MSG_DEBUG
, "Key Counter",
359 group
->Counter
, WPA_NONCE_LEN
);
365 static struct wpa_group
* wpa_group_init(struct wpa_authenticator
*wpa_auth
,
366 int vlan_id
, int delay_init
)
368 struct wpa_group
*group
;
370 group
= os_zalloc(sizeof(struct wpa_group
));
374 group
->GTKAuthenticator
= TRUE
;
375 group
->vlan_id
= vlan_id
;
376 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
378 if (random_pool_ready() != 1) {
379 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
380 "for secure operations - update keys later when "
381 "the first station connects");
385 * Set initial GMK/Counter value here. The actual values that will be
386 * used in negotiations will be set once the first station tries to
387 * connect. This allows more time for collecting additional randomness
388 * on embedded devices.
390 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0) {
391 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
399 wpa_printf(MSG_DEBUG
, "WPA: Delay group state machine start "
400 "until Beacon frames have been configured");
401 /* Initialization is completed in wpa_init_keys(). */
403 wpa_group_sm_step(wpa_auth
, group
);
404 group
->GInit
= FALSE
;
405 wpa_group_sm_step(wpa_auth
, group
);
413 * wpa_init - Initialize WPA authenticator
414 * @addr: Authenticator address
415 * @conf: Configuration for WPA authenticator
416 * @cb: Callback functions for WPA authenticator
417 * Returns: Pointer to WPA authenticator data or %NULL on failure
419 struct wpa_authenticator
* wpa_init(const u8
*addr
,
420 struct wpa_auth_config
*conf
,
421 const struct wpa_auth_callbacks
*cb
,
424 struct wpa_authenticator
*wpa_auth
;
426 wpa_auth
= os_zalloc(sizeof(struct wpa_authenticator
));
427 if (wpa_auth
== NULL
)
429 os_memcpy(wpa_auth
->addr
, addr
, ETH_ALEN
);
430 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
432 wpa_auth
->cb_ctx
= cb_ctx
;
434 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
435 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
440 wpa_auth
->group
= wpa_group_init(wpa_auth
, 0, 1);
441 if (wpa_auth
->group
== NULL
) {
442 os_free(wpa_auth
->wpa_ie
);
447 wpa_auth
->pmksa
= pmksa_cache_auth_init(wpa_auth_pmksa_free_cb
,
449 if (wpa_auth
->pmksa
== NULL
) {
450 wpa_printf(MSG_ERROR
, "PMKSA cache initialization failed.");
451 os_free(wpa_auth
->group
);
452 os_free(wpa_auth
->wpa_ie
);
457 #ifdef CONFIG_IEEE80211R_AP
458 wpa_auth
->ft_pmk_cache
= wpa_ft_pmk_cache_init();
459 if (wpa_auth
->ft_pmk_cache
== NULL
) {
460 wpa_printf(MSG_ERROR
, "FT PMK cache initialization failed.");
461 os_free(wpa_auth
->group
);
462 os_free(wpa_auth
->wpa_ie
);
463 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
467 #endif /* CONFIG_IEEE80211R_AP */
469 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
470 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
471 wpa_rekey_gmk
, wpa_auth
, NULL
);
474 if (wpa_auth
->conf
.wpa_group_rekey
) {
475 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
, 0,
476 wpa_rekey_gtk
, wpa_auth
, NULL
);
480 if (WPA_GET_BE32(conf
->ip_addr_start
)) {
481 int count
= WPA_GET_BE32(conf
->ip_addr_end
) -
482 WPA_GET_BE32(conf
->ip_addr_start
) + 1;
486 wpa_auth
->ip_pool
= bitfield_alloc(count
);
488 #endif /* CONFIG_P2P */
494 int wpa_init_keys(struct wpa_authenticator
*wpa_auth
)
496 struct wpa_group
*group
= wpa_auth
->group
;
498 wpa_printf(MSG_DEBUG
, "WPA: Start group state machine to set initial "
500 wpa_group_sm_step(wpa_auth
, group
);
501 group
->GInit
= FALSE
;
502 wpa_group_sm_step(wpa_auth
, group
);
503 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
510 * wpa_deinit - Deinitialize WPA authenticator
511 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
513 void wpa_deinit(struct wpa_authenticator
*wpa_auth
)
515 struct wpa_group
*group
, *prev
;
517 eloop_cancel_timeout(wpa_rekey_gmk
, wpa_auth
, NULL
);
518 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
520 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
522 #ifdef CONFIG_IEEE80211R_AP
523 wpa_ft_pmk_cache_deinit(wpa_auth
->ft_pmk_cache
);
524 wpa_auth
->ft_pmk_cache
= NULL
;
525 wpa_ft_deinit(wpa_auth
);
526 #endif /* CONFIG_IEEE80211R_AP */
529 bitfield_free(wpa_auth
->ip_pool
);
530 #endif /* CONFIG_P2P */
533 os_free(wpa_auth
->wpa_ie
);
535 group
= wpa_auth
->group
;
547 * wpa_reconfig - Update WPA authenticator configuration
548 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
549 * @conf: Configuration for WPA authenticator
551 int wpa_reconfig(struct wpa_authenticator
*wpa_auth
,
552 struct wpa_auth_config
*conf
)
554 struct wpa_group
*group
;
555 if (wpa_auth
== NULL
)
558 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
559 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
560 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
565 * Reinitialize GTK to make sure it is suitable for the new
568 group
= wpa_auth
->group
;
569 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
571 wpa_group_sm_step(wpa_auth
, group
);
572 group
->GInit
= FALSE
;
573 wpa_group_sm_step(wpa_auth
, group
);
579 struct wpa_state_machine
*
580 wpa_auth_sta_init(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
581 const u8
*p2p_dev_addr
)
583 struct wpa_state_machine
*sm
;
585 if (wpa_auth
->group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
588 sm
= os_zalloc(sizeof(struct wpa_state_machine
));
591 os_memcpy(sm
->addr
, addr
, ETH_ALEN
);
593 os_memcpy(sm
->p2p_dev_addr
, p2p_dev_addr
, ETH_ALEN
);
595 sm
->wpa_auth
= wpa_auth
;
596 sm
->group
= wpa_auth
->group
;
597 wpa_group_get(sm
->wpa_auth
, sm
->group
);
603 int wpa_auth_sta_associated(struct wpa_authenticator
*wpa_auth
,
604 struct wpa_state_machine
*sm
)
606 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
609 #ifdef CONFIG_IEEE80211R_AP
610 if (sm
->ft_completed
) {
611 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
612 "FT authentication already completed - do not "
613 "start 4-way handshake");
614 /* Go to PTKINITDONE state to allow GTK rekeying */
615 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
619 #endif /* CONFIG_IEEE80211R_AP */
622 if (sm
->fils_completed
) {
623 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
624 "FILS authentication already completed - do not start 4-way handshake");
625 /* Go to PTKINITDONE state to allow GTK rekeying */
626 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
630 #endif /* CONFIG_FILS */
633 os_memset(&sm
->key_replay
, 0, sizeof(sm
->key_replay
));
634 sm
->ReAuthenticationRequest
= TRUE
;
635 return wpa_sm_step(sm
);
638 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
639 "start authentication");
643 if (wpa_sm_step(sm
) == 1)
644 return 1; /* should not really happen */
646 sm
->AuthenticationRequest
= TRUE
;
647 return wpa_sm_step(sm
);
651 void wpa_auth_sta_no_wpa(struct wpa_state_machine
*sm
)
653 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
654 * reassociates back to the same AP while the previous entry for the
655 * STA has not yet been removed. */
659 sm
->wpa_key_mgmt
= 0;
663 static void wpa_free_sta_sm(struct wpa_state_machine
*sm
)
666 if (WPA_GET_BE32(sm
->ip_addr
)) {
668 wpa_printf(MSG_DEBUG
, "P2P: Free assigned IP "
669 "address %u.%u.%u.%u from " MACSTR
,
670 sm
->ip_addr
[0], sm
->ip_addr
[1],
671 sm
->ip_addr
[2], sm
->ip_addr
[3],
673 start
= WPA_GET_BE32(sm
->wpa_auth
->conf
.ip_addr_start
);
674 bitfield_clear(sm
->wpa_auth
->ip_pool
,
675 WPA_GET_BE32(sm
->ip_addr
) - start
);
677 #endif /* CONFIG_P2P */
678 if (sm
->GUpdateStationKeys
) {
679 sm
->group
->GKeyDoneStations
--;
680 sm
->GUpdateStationKeys
= FALSE
;
682 #ifdef CONFIG_IEEE80211R_AP
683 os_free(sm
->assoc_resp_ftie
);
684 wpabuf_free(sm
->ft_pending_req_ies
);
685 #endif /* CONFIG_IEEE80211R_AP */
686 os_free(sm
->last_rx_eapol_key
);
688 wpa_group_put(sm
->wpa_auth
, sm
->group
);
693 void wpa_auth_sta_deinit(struct wpa_state_machine
*sm
)
698 if (sm
->wpa_auth
->conf
.wpa_strict_rekey
&& sm
->has_GTK
) {
699 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
700 "strict rekeying - force GTK rekey since STA "
702 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk
,
703 sm
->wpa_auth
, NULL
) == -1)
704 eloop_register_timeout(0, 500000, wpa_rekey_gtk
, sm
->wpa_auth
,
708 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
709 sm
->pending_1_of_4_timeout
= 0;
710 eloop_cancel_timeout(wpa_sm_call_step
, sm
, NULL
);
711 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
712 #ifdef CONFIG_IEEE80211R_AP
713 wpa_ft_sta_deinit(sm
);
714 #endif /* CONFIG_IEEE80211R_AP */
715 if (sm
->in_step_loop
) {
716 /* Must not free state machine while wpa_sm_step() is running.
717 * Freeing will be completed in the end of wpa_sm_step(). */
718 wpa_printf(MSG_DEBUG
, "WPA: Registering pending STA state "
719 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
720 sm
->pending_deinit
= 1;
726 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
)
731 sm
->PTKRequest
= TRUE
;
736 static int wpa_replay_counter_valid(struct wpa_key_replay_counter
*ctr
,
737 const u8
*replay_counter
)
740 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
743 if (os_memcmp(replay_counter
, ctr
[i
].counter
,
744 WPA_REPLAY_COUNTER_LEN
) == 0)
751 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter
*ctr
,
752 const u8
*replay_counter
)
755 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
757 (replay_counter
== NULL
||
758 os_memcmp(replay_counter
, ctr
[i
].counter
,
759 WPA_REPLAY_COUNTER_LEN
) == 0))
760 ctr
[i
].valid
= FALSE
;
765 #ifdef CONFIG_IEEE80211R_AP
766 static int ft_check_msg_2_of_4(struct wpa_authenticator
*wpa_auth
,
767 struct wpa_state_machine
*sm
,
768 struct wpa_eapol_ie_parse
*kde
)
770 struct wpa_ie_data ie
;
771 struct rsn_mdie
*mdie
;
773 if (wpa_parse_wpa_ie_rsn(kde
->rsn_ie
, kde
->rsn_ie_len
, &ie
) < 0 ||
774 ie
.num_pmkid
!= 1 || ie
.pmkid
== NULL
) {
775 wpa_printf(MSG_DEBUG
, "FT: No PMKR1Name in "
776 "FT 4-way handshake message 2/4");
780 os_memcpy(sm
->sup_pmk_r1_name
, ie
.pmkid
, PMKID_LEN
);
781 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Supplicant",
782 sm
->sup_pmk_r1_name
, PMKID_LEN
);
784 if (!kde
->mdie
|| !kde
->ftie
) {
785 wpa_printf(MSG_DEBUG
, "FT: No %s in FT 4-way handshake "
786 "message 2/4", kde
->mdie
? "FTIE" : "MDIE");
790 mdie
= (struct rsn_mdie
*) (kde
->mdie
+ 2);
791 if (kde
->mdie
[1] < sizeof(struct rsn_mdie
) ||
792 os_memcmp(wpa_auth
->conf
.mobility_domain
, mdie
->mobility_domain
,
793 MOBILITY_DOMAIN_ID_LEN
) != 0) {
794 wpa_printf(MSG_DEBUG
, "FT: MDIE mismatch");
798 if (sm
->assoc_resp_ftie
&&
799 (kde
->ftie
[1] != sm
->assoc_resp_ftie
[1] ||
800 os_memcmp(kde
->ftie
, sm
->assoc_resp_ftie
,
801 2 + sm
->assoc_resp_ftie
[1]) != 0)) {
802 wpa_printf(MSG_DEBUG
, "FT: FTIE mismatch");
803 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 2/4",
804 kde
->ftie
, kde
->ftie_len
);
805 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)AssocResp",
806 sm
->assoc_resp_ftie
, 2 + sm
->assoc_resp_ftie
[1]);
812 #endif /* CONFIG_IEEE80211R_AP */
815 static int wpa_receive_error_report(struct wpa_authenticator
*wpa_auth
,
816 struct wpa_state_machine
*sm
, int group
)
818 /* Supplicant reported a Michael MIC error */
819 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
820 "received EAPOL-Key Error Request "
821 "(STA detected Michael MIC failure (group=%d))",
824 if (group
&& wpa_auth
->conf
.wpa_group
!= WPA_CIPHER_TKIP
) {
825 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
826 "ignore Michael MIC failure report since "
827 "group cipher is not TKIP");
828 } else if (!group
&& sm
->pairwise
!= WPA_CIPHER_TKIP
) {
829 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
830 "ignore Michael MIC failure report since "
831 "pairwise cipher is not TKIP");
833 if (wpa_auth_mic_failure_report(wpa_auth
, sm
->addr
) > 0)
834 return 1; /* STA entry was removed */
835 sm
->dot11RSNAStatsTKIPRemoteMICFailures
++;
836 wpa_auth
->dot11RSNAStatsTKIPRemoteMICFailures
++;
840 * Error report is not a request for a new key handshake, but since
841 * Authenticator may do it, let's change the keys now anyway.
843 wpa_request_new_ptk(sm
);
848 static int wpa_try_alt_snonce(struct wpa_state_machine
*sm
, u8
*data
,
853 const u8
*pmk
= NULL
;
856 os_memset(&PTK
, 0, sizeof(PTK
));
858 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
859 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
860 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
861 sm
->p2p_dev_addr
, pmk
, &pmk_len
);
864 #ifdef CONFIG_IEEE80211R_AP
865 if (wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
866 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
867 sm
->xxkey_len
= pmk_len
;
869 #endif /* CONFIG_IEEE80211R_AP */
872 pmk_len
= sm
->pmk_len
;
875 if (wpa_derive_ptk(sm
, sm
->alt_SNonce
, pmk
, pmk_len
, &PTK
) < 0)
878 if (wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
879 data
, data_len
) == 0) {
884 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
885 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
))
890 wpa_printf(MSG_DEBUG
,
891 "WPA: Earlier SNonce did not result in matching MIC");
895 wpa_printf(MSG_DEBUG
,
896 "WPA: Earlier SNonce resulted in matching MIC");
897 sm
->alt_snonce_valid
= 0;
898 os_memcpy(sm
->SNonce
, sm
->alt_SNonce
, WPA_NONCE_LEN
);
899 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
900 sm
->PTK_valid
= TRUE
;
906 void wpa_receive(struct wpa_authenticator
*wpa_auth
,
907 struct wpa_state_machine
*sm
,
908 u8
*data
, size_t data_len
)
910 struct ieee802_1x_hdr
*hdr
;
911 struct wpa_eapol_key
*key
;
912 u16 key_info
, key_data_length
;
913 enum { PAIRWISE_2
, PAIRWISE_4
, GROUP_2
, REQUEST
} msg
;
915 struct wpa_eapol_ie_parse kde
;
917 size_t keyhdrlen
, mic_len
;
920 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
922 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL data", data
, data_len
);
924 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
925 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
927 if (data_len
< sizeof(*hdr
) + keyhdrlen
) {
928 wpa_printf(MSG_DEBUG
, "WPA: Ignore too short EAPOL-Key frame");
932 hdr
= (struct ieee802_1x_hdr
*) data
;
933 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
934 mic
= (u8
*) (key
+ 1);
935 key_info
= WPA_GET_BE16(key
->key_info
);
936 key_data
= mic
+ mic_len
+ 2;
937 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
938 wpa_printf(MSG_DEBUG
, "WPA: Received EAPOL-Key from " MACSTR
939 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
940 MAC2STR(sm
->addr
), key_info
, key
->type
,
941 (unsigned int) mic_len
, key_data_length
);
942 wpa_hexdump(MSG_MSGDUMP
,
943 "WPA: EAPOL-Key header (ending before Key MIC)",
945 wpa_hexdump(MSG_MSGDUMP
, "WPA: EAPOL-Key Key MIC",
947 if (key_data_length
> data_len
- sizeof(*hdr
) - keyhdrlen
) {
948 wpa_printf(MSG_INFO
, "WPA: Invalid EAPOL-Key frame - "
949 "key_data overflow (%d > %lu)",
951 (unsigned long) (data_len
- sizeof(*hdr
) -
956 if (sm
->wpa
== WPA_VERSION_WPA2
) {
957 if (key
->type
== EAPOL_KEY_TYPE_WPA
) {
959 * Some deployed station implementations seem to send
960 * msg 4/4 with incorrect type value in WPA2 mode.
962 wpa_printf(MSG_DEBUG
, "Workaround: Allow EAPOL-Key "
963 "with unexpected WPA type in RSN mode");
964 } else if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
965 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
966 "unexpected type %d in RSN mode",
971 if (key
->type
!= EAPOL_KEY_TYPE_WPA
) {
972 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
973 "unexpected type %d in WPA mode",
979 wpa_hexdump(MSG_DEBUG
, "WPA: Received Key Nonce", key
->key_nonce
,
981 wpa_hexdump(MSG_DEBUG
, "WPA: Received Replay Counter",
982 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
984 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
987 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
988 wpa_printf(MSG_DEBUG
, "WPA: Ignore SMK message");
992 if (key_info
& WPA_KEY_INFO_REQUEST
) {
995 } else if (!(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
997 msgtxt
= "2/2 Group";
998 } else if (key_data_length
== 0 ||
999 (mic_len
== 0 && (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) &&
1000 key_data_length
== AES_BLOCK_SIZE
)) {
1002 msgtxt
= "4/4 Pairwise";
1005 msgtxt
= "2/4 Pairwise";
1008 if (msg
== REQUEST
|| msg
== PAIRWISE_2
|| msg
== PAIRWISE_4
||
1010 u16 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
1011 if (sm
->pairwise
== WPA_CIPHER_CCMP
||
1012 sm
->pairwise
== WPA_CIPHER_GCMP
) {
1013 if (wpa_use_aes_cmac(sm
) &&
1014 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
&&
1015 !wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) &&
1016 !wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1017 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1018 wpa_auth_logger(wpa_auth
, sm
->addr
,
1020 "advertised support for "
1021 "AES-128-CMAC, but did not "
1026 if (!wpa_use_aes_cmac(sm
) &&
1027 !wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) &&
1028 !wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1029 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OWE
&&
1030 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_DPP
&&
1031 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1032 wpa_auth_logger(wpa_auth
, sm
->addr
,
1034 "did not use HMAC-SHA1-AES "
1040 if ((wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) ||
1041 wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) ||
1042 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1043 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) &&
1044 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
1045 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1046 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1051 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1052 if (sm
->req_replay_counter_used
&&
1053 os_memcmp(key
->replay_counter
, sm
->req_replay_counter
,
1054 WPA_REPLAY_COUNTER_LEN
) <= 0) {
1055 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1056 "received EAPOL-Key request with "
1057 "replayed counter");
1062 if (!(key_info
& WPA_KEY_INFO_REQUEST
) &&
1063 !wpa_replay_counter_valid(sm
->key_replay
, key
->replay_counter
)) {
1066 if (msg
== PAIRWISE_2
&&
1067 wpa_replay_counter_valid(sm
->prev_key_replay
,
1068 key
->replay_counter
) &&
1069 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1070 os_memcmp(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0)
1073 * Some supplicant implementations (e.g., Windows XP
1074 * WZC) update SNonce for each EAPOL-Key 2/4. This
1075 * breaks the workaround on accepting any of the
1076 * pending requests, so allow the SNonce to be updated
1077 * even if we have already sent out EAPOL-Key 3/4.
1079 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1080 "Process SNonce update from STA "
1081 "based on retransmitted EAPOL-Key "
1083 sm
->update_snonce
= 1;
1084 os_memcpy(sm
->alt_SNonce
, sm
->SNonce
, WPA_NONCE_LEN
);
1085 sm
->alt_snonce_valid
= TRUE
;
1086 os_memcpy(sm
->alt_replay_counter
,
1087 sm
->key_replay
[0].counter
,
1088 WPA_REPLAY_COUNTER_LEN
);
1089 goto continue_processing
;
1092 if (msg
== PAIRWISE_4
&& sm
->alt_snonce_valid
&&
1093 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1094 os_memcmp(key
->replay_counter
, sm
->alt_replay_counter
,
1095 WPA_REPLAY_COUNTER_LEN
) == 0) {
1097 * Supplicant may still be using the old SNonce since
1098 * there was two EAPOL-Key 2/4 messages and they had
1099 * different SNonce values.
1101 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1102 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1103 goto continue_processing
;
1106 if (msg
== PAIRWISE_2
&&
1107 wpa_replay_counter_valid(sm
->prev_key_replay
,
1108 key
->replay_counter
) &&
1109 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
) {
1110 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1111 "ignore retransmitted EAPOL-Key %s - "
1112 "SNonce did not change", msgtxt
);
1114 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1115 "received EAPOL-Key %s with "
1116 "unexpected replay counter", msgtxt
);
1118 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
1119 if (!sm
->key_replay
[i
].valid
)
1121 wpa_hexdump(MSG_DEBUG
, "pending replay counter",
1122 sm
->key_replay
[i
].counter
,
1123 WPA_REPLAY_COUNTER_LEN
);
1125 wpa_hexdump(MSG_DEBUG
, "received replay counter",
1126 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1130 continue_processing
:
1132 if (sm
->wpa
== WPA_VERSION_WPA2
&& mic_len
== 0 &&
1133 !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1134 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1135 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1138 #endif /* CONFIG_FILS */
1142 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKSTART
&&
1143 sm
->wpa_ptk_state
!= WPA_PTK_PTKCALCNEGOTIATING
&&
1144 (!sm
->update_snonce
||
1145 sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
)) {
1146 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1147 "received EAPOL-Key msg 2/4 in "
1148 "invalid state (%d) - dropped",
1152 random_add_randomness(key
->key_nonce
, WPA_NONCE_LEN
);
1153 if (sm
->group
->reject_4way_hs_for_entropy
) {
1155 * The system did not have enough entropy to generate
1156 * strong random numbers. Reject the first 4-way
1157 * handshake(s) and collect some entropy based on the
1158 * information from it. Once enough entropy is
1159 * available, the next atempt will trigger GMK/Key
1160 * Counter update and the station will be allowed to
1163 wpa_printf(MSG_DEBUG
, "WPA: Reject 4-way handshake to "
1164 "collect more entropy for random number "
1166 random_mark_pool_ready();
1167 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
1168 WLAN_REASON_PREV_AUTH_NOT_VALID
);
1173 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
||
1175 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1176 "received EAPOL-Key msg 4/4 in "
1177 "invalid state (%d) - dropped",
1183 if (sm
->wpa_ptk_group_state
!= WPA_PTK_GROUP_REKEYNEGOTIATING
1184 || !sm
->PTK_valid
) {
1185 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1186 "received EAPOL-Key msg 2/2 in "
1187 "invalid state (%d) - dropped",
1188 sm
->wpa_ptk_group_state
);
1196 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1197 "received EAPOL-Key frame (%s)", msgtxt
);
1199 if (key_info
& WPA_KEY_INFO_ACK
) {
1200 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1201 "received invalid EAPOL-Key: Key Ack set");
1205 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1206 !(key_info
& WPA_KEY_INFO_MIC
)) {
1207 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1208 "received invalid EAPOL-Key: Key MIC not set");
1213 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1214 (key_info
& WPA_KEY_INFO_MIC
)) {
1215 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1216 "received invalid EAPOL-Key: Key MIC set");
1219 #endif /* CONFIG_FILS */
1221 sm
->MICVerified
= FALSE
;
1222 if (sm
->PTK_valid
&& !sm
->update_snonce
) {
1224 wpa_verify_key_mic(sm
->wpa_key_mgmt
, sm
->pmk_len
, &sm
->PTK
,
1226 (msg
!= PAIRWISE_4
|| !sm
->alt_snonce_valid
||
1227 wpa_try_alt_snonce(sm
, data
, data_len
))) {
1228 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1229 "received EAPOL-Key with invalid MIC");
1234 wpa_aead_decrypt(sm
, &sm
->PTK
, data
, data_len
,
1235 &key_data_length
) < 0) {
1236 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1237 "received EAPOL-Key with invalid MIC");
1240 #endif /* CONFIG_FILS */
1241 sm
->MICVerified
= TRUE
;
1242 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
1243 sm
->pending_1_of_4_timeout
= 0;
1246 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1247 if (sm
->MICVerified
) {
1248 sm
->req_replay_counter_used
= 1;
1249 os_memcpy(sm
->req_replay_counter
, key
->replay_counter
,
1250 WPA_REPLAY_COUNTER_LEN
);
1252 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1253 "received EAPOL-Key request with "
1259 * TODO: should decrypt key data field if encryption was used;
1260 * even though MAC address KDE is not normally encrypted,
1261 * supplicant is allowed to encrypt it.
1263 if (key_info
& WPA_KEY_INFO_ERROR
) {
1264 if (wpa_receive_error_report(
1266 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) > 0)
1267 return; /* STA entry was removed */
1268 } else if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
1269 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1270 "received EAPOL-Key Request for new "
1272 wpa_request_new_ptk(sm
);
1273 } else if (key_data_length
> 0 &&
1274 wpa_parse_kde_ies(key_data
, key_data_length
,
1278 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1279 "received EAPOL-Key Request for GTK "
1281 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
1282 wpa_rekey_gtk(wpa_auth
, NULL
);
1285 /* Do not allow the same key replay counter to be reused. */
1286 wpa_replay_counter_mark_invalid(sm
->key_replay
,
1287 key
->replay_counter
);
1289 if (msg
== PAIRWISE_2
) {
1291 * Maintain a copy of the pending EAPOL-Key frames in
1292 * case the EAPOL-Key frame was retransmitted. This is
1293 * needed to allow EAPOL-Key msg 2/4 reply to another
1294 * pending msg 1/4 to update the SNonce to work around
1295 * unexpected supplicant behavior.
1297 os_memcpy(sm
->prev_key_replay
, sm
->key_replay
,
1298 sizeof(sm
->key_replay
));
1300 os_memset(sm
->prev_key_replay
, 0,
1301 sizeof(sm
->prev_key_replay
));
1305 * Make sure old valid counters are not accepted anymore and
1306 * do not get copied again.
1308 wpa_replay_counter_mark_invalid(sm
->key_replay
, NULL
);
1311 os_free(sm
->last_rx_eapol_key
);
1312 sm
->last_rx_eapol_key
= os_memdup(data
, data_len
);
1313 if (sm
->last_rx_eapol_key
== NULL
)
1315 sm
->last_rx_eapol_key_len
= data_len
;
1317 sm
->rx_eapol_key_secure
= !!(key_info
& WPA_KEY_INFO_SECURE
);
1318 sm
->EAPOLKeyReceived
= TRUE
;
1319 sm
->EAPOLKeyPairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1320 sm
->EAPOLKeyRequest
= !!(key_info
& WPA_KEY_INFO_REQUEST
);
1321 os_memcpy(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
);
1326 static int wpa_gmk_to_gtk(const u8
*gmk
, const char *label
, const u8
*addr
,
1327 const u8
*gnonce
, u8
*gtk
, size_t gtk_len
)
1329 u8 data
[ETH_ALEN
+ WPA_NONCE_LEN
+ 8 + WPA_GTK_MAX_LEN
];
1333 /* GTK = PRF-X(GMK, "Group key expansion",
1334 * AA || GNonce || Time || random data)
1335 * The example described in the IEEE 802.11 standard uses only AA and
1336 * GNonce as inputs here. Add some more entropy since this derivation
1337 * is done only at the Authenticator and as such, does not need to be
1340 os_memset(data
, 0, sizeof(data
));
1341 os_memcpy(data
, addr
, ETH_ALEN
);
1342 os_memcpy(data
+ ETH_ALEN
, gnonce
, WPA_NONCE_LEN
);
1343 pos
= data
+ ETH_ALEN
+ WPA_NONCE_LEN
;
1344 wpa_get_ntp_timestamp(pos
);
1346 if (random_get_bytes(pos
, gtk_len
) < 0)
1349 #ifdef CONFIG_SHA384
1350 if (sha384_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1353 #else /* CONFIG_SHA384 */
1354 #ifdef CONFIG_SHA256
1355 if (sha256_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1358 #else /* CONFIG_SHA256 */
1359 if (sha1_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1362 #endif /* CONFIG_SHA256 */
1363 #endif /* CONFIG_SHA384 */
1369 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
)
1371 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1372 struct wpa_state_machine
*sm
= timeout_ctx
;
1374 sm
->pending_1_of_4_timeout
= 0;
1375 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "EAPOL-Key timeout");
1376 sm
->TimeoutEvt
= TRUE
;
1381 void __wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1382 struct wpa_state_machine
*sm
, int key_info
,
1383 const u8
*key_rsc
, const u8
*nonce
,
1384 const u8
*kde
, size_t kde_len
,
1385 int keyidx
, int encr
, int force_version
)
1387 struct ieee802_1x_hdr
*hdr
;
1388 struct wpa_eapol_key
*key
;
1389 size_t len
, mic_len
, keyhdrlen
;
1391 int key_data_len
, pad_len
= 0;
1393 int version
, pairwise
;
1395 u8
*key_mic
, *key_data
;
1397 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
1398 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
1400 len
= sizeof(struct ieee802_1x_hdr
) + keyhdrlen
;
1403 version
= force_version
;
1404 else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
||
1405 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
||
1406 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1407 wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) ||
1408 wpa_key_mgmt_fils(sm
->wpa_key_mgmt
))
1409 version
= WPA_KEY_INFO_TYPE_AKM_DEFINED
;
1410 else if (wpa_use_aes_cmac(sm
))
1411 version
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
1412 else if (sm
->pairwise
!= WPA_CIPHER_TKIP
)
1413 version
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
1415 version
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
1417 pairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1419 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1420 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1423 (key_info
& WPA_KEY_INFO_SECURE
) ? 1 : 0,
1424 (key_info
& WPA_KEY_INFO_MIC
) ? 1 : 0,
1425 (key_info
& WPA_KEY_INFO_ACK
) ? 1 : 0,
1426 (key_info
& WPA_KEY_INFO_INSTALL
) ? 1 : 0,
1427 pairwise
, (unsigned long) kde_len
, keyidx
, encr
);
1429 key_data_len
= kde_len
;
1431 if ((version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1432 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
||
1433 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1434 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
||
1435 wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) ||
1436 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) && encr
) {
1437 pad_len
= key_data_len
% 8;
1439 pad_len
= 8 - pad_len
;
1440 key_data_len
+= pad_len
+ 8;
1443 len
+= key_data_len
;
1444 if (!mic_len
&& encr
)
1445 len
+= AES_BLOCK_SIZE
;
1447 hdr
= os_zalloc(len
);
1450 hdr
->version
= wpa_auth
->conf
.eapol_version
;
1451 hdr
->type
= IEEE802_1X_TYPE_EAPOL_KEY
;
1452 hdr
->length
= host_to_be16(len
- sizeof(*hdr
));
1453 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1454 key_mic
= (u8
*) (key
+ 1);
1455 key_data
= ((u8
*) (hdr
+ 1)) + keyhdrlen
;
1457 key
->type
= sm
->wpa
== WPA_VERSION_WPA2
?
1458 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1459 key_info
|= version
;
1460 if (encr
&& sm
->wpa
== WPA_VERSION_WPA2
)
1461 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1462 if (sm
->wpa
!= WPA_VERSION_WPA2
)
1463 key_info
|= keyidx
<< WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1464 WPA_PUT_BE16(key
->key_info
, key_info
);
1466 alg
= pairwise
? sm
->pairwise
: wpa_auth
->conf
.wpa_group
;
1467 if (sm
->wpa
== WPA_VERSION_WPA2
&& !pairwise
)
1468 WPA_PUT_BE16(key
->key_length
, 0);
1470 WPA_PUT_BE16(key
->key_length
, wpa_cipher_key_len(alg
));
1472 for (i
= RSNA_MAX_EAPOL_RETRIES
- 1; i
> 0; i
--) {
1473 sm
->key_replay
[i
].valid
= sm
->key_replay
[i
- 1].valid
;
1474 os_memcpy(sm
->key_replay
[i
].counter
,
1475 sm
->key_replay
[i
- 1].counter
,
1476 WPA_REPLAY_COUNTER_LEN
);
1478 inc_byte_array(sm
->key_replay
[0].counter
, WPA_REPLAY_COUNTER_LEN
);
1479 os_memcpy(key
->replay_counter
, sm
->key_replay
[0].counter
,
1480 WPA_REPLAY_COUNTER_LEN
);
1481 wpa_hexdump(MSG_DEBUG
, "WPA: Replay Counter",
1482 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1483 sm
->key_replay
[0].valid
= TRUE
;
1486 os_memcpy(key
->key_nonce
, nonce
, WPA_NONCE_LEN
);
1489 os_memcpy(key
->key_rsc
, key_rsc
, WPA_KEY_RSC_LEN
);
1492 os_memcpy(key_data
, kde
, kde_len
);
1493 WPA_PUT_BE16(key_mic
+ mic_len
, kde_len
);
1495 } else if (!mic_len
&& kde
) {
1499 WPA_PUT_BE16(key_mic
, AES_BLOCK_SIZE
+ kde_len
);
1500 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1503 wpa_hexdump_key(MSG_DEBUG
, "WPA: KEK",
1504 sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1505 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1506 * to Key Data (exclusive). */
1507 aad
[0] = (u8
*) hdr
;
1508 aad_len
[0] = key_mic
+ 2 - (u8
*) hdr
;
1509 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, kde
, kde_len
,
1510 1, aad
, aad_len
, key_mic
+ 2) < 0) {
1511 wpa_printf(MSG_DEBUG
, "WPA: AES-SIV encryption failed");
1515 wpa_hexdump(MSG_DEBUG
, "WPA: Encrypted Key Data from SIV",
1516 key_mic
+ 2, AES_BLOCK_SIZE
+ kde_len
);
1517 #endif /* CONFIG_FILS */
1518 } else if (encr
&& kde
) {
1519 buf
= os_zalloc(key_data_len
);
1525 os_memcpy(pos
, kde
, kde_len
);
1531 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1533 if (version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1534 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
||
1535 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1536 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
||
1537 wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) ||
1538 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1539 wpa_printf(MSG_DEBUG
,
1540 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1541 (unsigned int) sm
->PTK
.kek_len
);
1542 if (aes_wrap(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
1543 (key_data_len
- 8) / 8, buf
, key_data
)) {
1548 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1549 #ifndef CONFIG_NO_RC4
1550 } else if (sm
->PTK
.kek_len
== 16) {
1553 wpa_printf(MSG_DEBUG
,
1554 "WPA: Encrypt Key Data using RC4");
1555 os_memcpy(key
->key_iv
,
1556 sm
->group
->Counter
+ WPA_NONCE_LEN
- 16, 16);
1557 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1558 os_memcpy(ek
, key
->key_iv
, 16);
1559 os_memcpy(ek
+ 16, sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1560 os_memcpy(key_data
, buf
, key_data_len
);
1561 rc4_skip(ek
, 32, 256, key_data
, key_data_len
);
1562 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1563 #endif /* CONFIG_NO_RC4 */
1572 if (key_info
& WPA_KEY_INFO_MIC
) {
1573 if (!sm
->PTK_valid
|| !mic_len
) {
1574 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1575 "PTK not valid when sending EAPOL-Key "
1581 if (wpa_eapol_key_mic(sm
->PTK
.kck
, sm
->PTK
.kck_len
,
1582 sm
->wpa_key_mgmt
, version
,
1583 (u8
*) hdr
, len
, key_mic
) < 0) {
1587 #ifdef CONFIG_TESTING_OPTIONS
1589 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
> 0.0 &&
1591 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
) {
1592 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1593 "Corrupting group EAPOL-Key Key MIC");
1596 #endif /* CONFIG_TESTING_OPTIONS */
1599 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_inc_EapolFramesTx
,
1601 wpa_auth_send_eapol(wpa_auth
, sm
->addr
, (u8
*) hdr
, len
,
1607 static void wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1608 struct wpa_state_machine
*sm
, int key_info
,
1609 const u8
*key_rsc
, const u8
*nonce
,
1610 const u8
*kde
, size_t kde_len
,
1611 int keyidx
, int encr
)
1614 int pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
1620 __wpa_send_eapol(wpa_auth
, sm
, key_info
, key_rsc
, nonce
, kde
, kde_len
,
1623 ctr
= pairwise
? sm
->TimeoutCtr
: sm
->GTimeoutCtr
;
1624 if (ctr
== 1 && wpa_auth
->conf
.tx_status
)
1625 timeout_ms
= pairwise
? eapol_key_timeout_first
:
1626 eapol_key_timeout_first_group
;
1628 timeout_ms
= eapol_key_timeout_subseq
;
1629 if (wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
1630 (!pairwise
|| (key_info
& WPA_KEY_INFO_MIC
)))
1631 timeout_ms
= eapol_key_timeout_no_retrans
;
1632 if (pairwise
&& ctr
== 1 && !(key_info
& WPA_KEY_INFO_MIC
))
1633 sm
->pending_1_of_4_timeout
= 1;
1634 wpa_printf(MSG_DEBUG
, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1635 "counter %u)", timeout_ms
, ctr
);
1636 eloop_register_timeout(timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000,
1637 wpa_send_eapol_timeout
, wpa_auth
, sm
);
1641 static int wpa_verify_key_mic(int akmp
, size_t pmk_len
, struct wpa_ptk
*PTK
,
1642 u8
*data
, size_t data_len
)
1644 struct ieee802_1x_hdr
*hdr
;
1645 struct wpa_eapol_key
*key
;
1648 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
], *mic_pos
;
1649 size_t mic_len
= wpa_mic_len(akmp
, pmk_len
);
1651 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
1654 hdr
= (struct ieee802_1x_hdr
*) data
;
1655 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1656 mic_pos
= (u8
*) (key
+ 1);
1657 key_info
= WPA_GET_BE16(key
->key_info
);
1658 os_memcpy(mic
, mic_pos
, mic_len
);
1659 os_memset(mic_pos
, 0, mic_len
);
1660 if (wpa_eapol_key_mic(PTK
->kck
, PTK
->kck_len
, akmp
,
1661 key_info
& WPA_KEY_INFO_TYPE_MASK
,
1662 data
, data_len
, mic_pos
) ||
1663 os_memcmp_const(mic
, mic_pos
, mic_len
) != 0)
1665 os_memcpy(mic_pos
, mic
, mic_len
);
1670 void wpa_remove_ptk(struct wpa_state_machine
*sm
)
1672 sm
->PTK_valid
= FALSE
;
1673 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1674 if (wpa_auth_set_key(sm
->wpa_auth
, 0, WPA_ALG_NONE
, sm
->addr
, 0, NULL
,
1676 wpa_printf(MSG_DEBUG
,
1677 "RSN: PTK removal from the driver failed");
1678 sm
->pairwise_set
= FALSE
;
1679 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1683 int wpa_auth_sm_event(struct wpa_state_machine
*sm
, enum wpa_event event
)
1690 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1691 "event %d notification", event
);
1696 /* PTKs are derived through AMPE */
1697 if (wpa_auth_start_ampe(sm
->wpa_auth
, sm
->addr
)) {
1702 #endif /* CONFIG_MESH */
1707 sm
->DeauthenticationRequest
= TRUE
;
1710 case WPA_REAUTH_EAPOL
:
1713 * When using WPS, we may end up here if the STA
1714 * manages to re-associate without the previous STA
1715 * entry getting removed. Consequently, we need to make
1716 * sure that the WPA state machines gets initialized
1717 * properly at this point.
1719 wpa_printf(MSG_DEBUG
, "WPA state machine had not been "
1720 "started - initialize now");
1723 if (wpa_sm_step(sm
) == 1)
1724 return 1; /* should not really happen */
1726 sm
->AuthenticationRequest
= TRUE
;
1729 if (sm
->GUpdateStationKeys
) {
1731 * Reauthentication cancels the pending group key
1732 * update for this STA.
1734 sm
->group
->GKeyDoneStations
--;
1735 sm
->GUpdateStationKeys
= FALSE
;
1736 sm
->PtkGroupInit
= TRUE
;
1738 sm
->ReAuthenticationRequest
= TRUE
;
1741 #ifdef CONFIG_IEEE80211R_AP
1742 wpa_printf(MSG_DEBUG
, "FT: Retry PTK configuration "
1743 "after association");
1744 wpa_ft_install_ptk(sm
);
1746 /* Using FT protocol, not WPA auth state machine */
1747 sm
->ft_completed
= 1;
1749 #else /* CONFIG_IEEE80211R_AP */
1751 #endif /* CONFIG_IEEE80211R_AP */
1752 case WPA_ASSOC_FILS
:
1754 wpa_printf(MSG_DEBUG
,
1755 "FILS: TK configuration after association");
1757 sm
->fils_completed
= 1;
1759 #else /* CONFIG_FILS */
1761 #endif /* CONFIG_FILS */
1762 case WPA_DRV_STA_REMOVED
:
1763 sm
->tk_already_set
= FALSE
;
1767 #ifdef CONFIG_IEEE80211R_AP
1768 sm
->ft_completed
= 0;
1769 #endif /* CONFIG_IEEE80211R_AP */
1771 #ifdef CONFIG_IEEE80211W
1772 if (sm
->mgmt_frame_prot
&& event
== WPA_AUTH
)
1774 #endif /* CONFIG_IEEE80211W */
1776 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1777 (event
== WPA_AUTH
|| event
== WPA_ASSOC
))
1779 #endif /* CONFIG_FILS */
1782 sm
->PTK_valid
= FALSE
;
1783 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1785 if (event
!= WPA_REAUTH_EAPOL
)
1789 if (sm
->in_step_loop
) {
1791 * wpa_sm_step() is already running - avoid recursive call to
1792 * it by making the existing loop process the new update.
1797 return wpa_sm_step(sm
);
1801 SM_STATE(WPA_PTK
, INITIALIZE
)
1803 SM_ENTRY_MA(WPA_PTK
, INITIALIZE
, wpa_ptk
);
1805 /* Init flag is not cleared here, so avoid busy
1806 * loop by claiming nothing changed. */
1807 sm
->changed
= FALSE
;
1811 if (sm
->GUpdateStationKeys
)
1812 sm
->group
->GKeyDoneStations
--;
1813 sm
->GUpdateStationKeys
= FALSE
;
1814 if (sm
->wpa
== WPA_VERSION_WPA
)
1815 sm
->PInitAKeys
= FALSE
;
1816 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1817 * Local AA > Remote AA)) */) {
1820 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 0);
1822 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
, 0);
1824 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
1825 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1826 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
1827 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1828 WPA_EAPOL_authorized
, 0);
1833 SM_STATE(WPA_PTK
, DISCONNECT
)
1835 u16 reason
= sm
->disconnect_reason
;
1837 SM_ENTRY_MA(WPA_PTK
, DISCONNECT
, wpa_ptk
);
1838 sm
->Disconnect
= FALSE
;
1839 sm
->disconnect_reason
= 0;
1841 reason
= WLAN_REASON_PREV_AUTH_NOT_VALID
;
1842 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
, reason
);
1846 SM_STATE(WPA_PTK
, DISCONNECTED
)
1848 SM_ENTRY_MA(WPA_PTK
, DISCONNECTED
, wpa_ptk
);
1849 sm
->DeauthenticationRequest
= FALSE
;
1853 SM_STATE(WPA_PTK
, AUTHENTICATION
)
1855 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION
, wpa_ptk
);
1856 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1857 sm
->PTK_valid
= FALSE
;
1858 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portControl_Auto
,
1860 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 1);
1861 sm
->AuthenticationRequest
= FALSE
;
1865 static void wpa_group_ensure_init(struct wpa_authenticator
*wpa_auth
,
1866 struct wpa_group
*group
)
1868 if (group
->first_sta_seen
)
1871 * System has run bit further than at the time hostapd was started
1872 * potentially very early during boot up. This provides better chances
1873 * of collecting more randomness on embedded systems. Re-initialize the
1874 * GMK and Counter here to improve their strength if there was not
1875 * enough entropy available immediately after system startup.
1877 wpa_printf(MSG_DEBUG
, "WPA: Re-initialize GMK/Counter on first "
1879 if (random_pool_ready() != 1) {
1880 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
1881 "to proceed - reject first 4-way handshake");
1882 group
->reject_4way_hs_for_entropy
= TRUE
;
1884 group
->first_sta_seen
= TRUE
;
1885 group
->reject_4way_hs_for_entropy
= FALSE
;
1888 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0 ||
1889 wpa_gtk_update(wpa_auth
, group
) < 0 ||
1890 wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
1891 wpa_printf(MSG_INFO
, "WPA: GMK/GTK setup failed");
1892 group
->first_sta_seen
= FALSE
;
1893 group
->reject_4way_hs_for_entropy
= TRUE
;
1898 SM_STATE(WPA_PTK
, AUTHENTICATION2
)
1900 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION2
, wpa_ptk
);
1902 wpa_group_ensure_init(sm
->wpa_auth
, sm
->group
);
1903 sm
->ReAuthenticationRequest
= FALSE
;
1906 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1907 * ambiguous. The Authenticator state machine uses a counter that is
1908 * incremented by one for each 4-way handshake. However, the security
1909 * analysis of 4-way handshake points out that unpredictable nonces
1910 * help in preventing precomputation attacks. Instead of the state
1911 * machine definition, use an unpredictable nonce value here to provide
1912 * stronger protection against potential precomputation attacks.
1914 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
1915 wpa_printf(MSG_ERROR
, "WPA: Failed to get random data for "
1917 sm
->Disconnect
= TRUE
;
1920 wpa_hexdump(MSG_DEBUG
, "WPA: Assign ANonce", sm
->ANonce
,
1922 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1923 * logical place than INITIALIZE since AUTHENTICATION2 can be
1924 * re-entered on ReAuthenticationRequest without going through
1930 static int wpa_auth_sm_ptk_update(struct wpa_state_machine
*sm
)
1932 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
1933 wpa_printf(MSG_ERROR
,
1934 "WPA: Failed to get random data for ANonce");
1935 sm
->Disconnect
= TRUE
;
1938 wpa_hexdump(MSG_DEBUG
, "WPA: Assign new ANonce", sm
->ANonce
,
1945 SM_STATE(WPA_PTK
, INITPMK
)
1947 u8 msk
[2 * PMK_LEN
];
1948 size_t len
= 2 * PMK_LEN
;
1950 SM_ENTRY_MA(WPA_PTK
, INITPMK
, wpa_ptk
);
1951 #ifdef CONFIG_IEEE80211R_AP
1953 #endif /* CONFIG_IEEE80211R_AP */
1955 wpa_printf(MSG_DEBUG
, "WPA: PMK from PMKSA cache");
1956 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
1957 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
1959 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
) {
1960 wpa_printf(MSG_DEBUG
,
1961 "DPP: No PMKSA cache entry for STA - reject connection");
1962 sm
->Disconnect
= TRUE
;
1963 sm
->disconnect_reason
= WLAN_REASON_INVALID_PMKID
;
1965 #endif /* CONFIG_DPP */
1966 } else if (wpa_auth_get_msk(sm
->wpa_auth
, sm
->addr
, msk
, &len
) == 0) {
1967 unsigned int pmk_len
;
1969 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
))
1970 pmk_len
= PMK_LEN_SUITE_B_192
;
1973 wpa_printf(MSG_DEBUG
, "WPA: PMK from EAPOL state machine "
1974 "(MSK len=%lu PMK len=%u)", (unsigned long) len
,
1976 if (len
< pmk_len
) {
1977 wpa_printf(MSG_DEBUG
,
1978 "WPA: MSK not long enough (%u) to create PMK (%u)",
1979 (unsigned int) len
, (unsigned int) pmk_len
);
1980 sm
->Disconnect
= TRUE
;
1983 os_memcpy(sm
->PMK
, msk
, pmk_len
);
1984 sm
->pmk_len
= pmk_len
;
1985 #ifdef CONFIG_IEEE80211R_AP
1986 if (len
>= 2 * PMK_LEN
) {
1987 os_memcpy(sm
->xxkey
, msk
+ PMK_LEN
, PMK_LEN
);
1988 sm
->xxkey_len
= PMK_LEN
;
1990 #endif /* CONFIG_IEEE80211R_AP */
1992 wpa_printf(MSG_DEBUG
, "WPA: Could not get PMK, get_msk: %p",
1993 sm
->wpa_auth
->cb
->get_msk
);
1994 sm
->Disconnect
= TRUE
;
1997 os_memset(msk
, 0, sizeof(msk
));
1999 sm
->req_replay_counter_used
= 0;
2000 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
2001 * will break reauthentication since EAPOL state machines may not be
2002 * get into AUTHENTICATING state that clears keyRun before WPA state
2003 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2004 * state and takes PMK from the previously used AAA Key. This will
2005 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2006 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2007 * be good workaround for this issue. */
2008 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyRun
, 0);
2012 SM_STATE(WPA_PTK
, INITPSK
)
2017 SM_ENTRY_MA(WPA_PTK
, INITPSK
, wpa_ptk
);
2018 psk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
, NULL
,
2021 os_memcpy(sm
->PMK
, psk
, psk_len
);
2022 sm
->pmk_len
= psk_len
;
2023 #ifdef CONFIG_IEEE80211R_AP
2024 os_memcpy(sm
->xxkey
, psk
, PMK_LEN
);
2025 sm
->xxkey_len
= PMK_LEN
;
2026 #endif /* CONFIG_IEEE80211R_AP */
2029 if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
2030 wpa_printf(MSG_DEBUG
, "SAE: PMK from PMKSA cache");
2031 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2032 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
2034 #endif /* CONFIG_SAE */
2035 sm
->req_replay_counter_used
= 0;
2039 SM_STATE(WPA_PTK
, PTKSTART
)
2041 u8 buf
[2 + RSN_SELECTOR_LEN
+ PMKID_LEN
], *pmkid
= NULL
;
2042 size_t pmkid_len
= 0;
2044 SM_ENTRY_MA(WPA_PTK
, PTKSTART
, wpa_ptk
);
2045 sm
->PTKRequest
= FALSE
;
2046 sm
->TimeoutEvt
= FALSE
;
2047 sm
->alt_snonce_valid
= FALSE
;
2050 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
2051 /* No point in sending the EAPOL-Key - we will disconnect
2052 * immediately following this. */
2056 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2057 "sending 1/4 msg of 4-Way Handshake");
2059 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
2060 * one possible PSK for this STA.
2062 if (sm
->wpa
== WPA_VERSION_WPA2
&&
2063 (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) ||
2064 (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
&& sm
->pmksa
) ||
2065 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) &&
2066 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
) {
2068 pmkid_len
= 2 + RSN_SELECTOR_LEN
+ PMKID_LEN
;
2069 pmkid
[0] = WLAN_EID_VENDOR_SPECIFIC
;
2070 pmkid
[1] = RSN_SELECTOR_LEN
+ PMKID_LEN
;
2071 RSN_SELECTOR_PUT(&pmkid
[2], RSN_KEY_DATA_PMKID
);
2073 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2074 sm
->pmksa
->pmkid
, PMKID_LEN
);
2075 } else if (wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
)) {
2076 /* No KCK available to derive PMKID */
2080 * Calculate PMKID since no PMKSA cache entry was
2081 * available with pre-calculated PMKID.
2083 rsn_pmkid(sm
->PMK
, sm
->pmk_len
, sm
->wpa_auth
->addr
,
2084 sm
->addr
, &pmkid
[2 + RSN_SELECTOR_LEN
],
2088 wpa_send_eapol(sm
->wpa_auth
, sm
,
2089 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
2090 sm
->ANonce
, pmkid
, pmkid_len
, 0, 0);
2094 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
2095 const u8
*pmk
, unsigned int pmk_len
,
2096 struct wpa_ptk
*ptk
)
2098 #ifdef CONFIG_IEEE80211R_AP
2099 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
2100 return wpa_auth_derive_ptk_ft(sm
, pmk
, ptk
);
2101 #endif /* CONFIG_IEEE80211R_AP */
2103 return wpa_pmk_to_ptk(pmk
, pmk_len
, "Pairwise key expansion",
2104 sm
->wpa_auth
->addr
, sm
->addr
, sm
->ANonce
, snonce
,
2105 ptk
, sm
->wpa_key_mgmt
, sm
->pairwise
);
2111 int fils_auth_pmk_to_ptk(struct wpa_state_machine
*sm
, const u8
*pmk
,
2112 size_t pmk_len
, const u8
*snonce
, const u8
*anonce
,
2113 const u8
*dhss
, size_t dhss_len
,
2114 struct wpabuf
*g_sta
, struct wpabuf
*g_ap
)
2116 u8 ick
[FILS_ICK_MAX_LEN
];
2119 u8 fils_ft
[FILS_FT_MAX_LEN
];
2120 size_t fils_ft_len
= 0;
2122 res
= fils_pmk_to_ptk(pmk
, pmk_len
, sm
->addr
, sm
->wpa_auth
->addr
,
2123 snonce
, anonce
, dhss
, dhss_len
,
2124 &sm
->PTK
, ick
, &ick_len
,
2125 sm
->wpa_key_mgmt
, sm
->pairwise
,
2126 fils_ft
, &fils_ft_len
);
2129 sm
->PTK_valid
= TRUE
;
2130 sm
->tk_already_set
= FALSE
;
2132 #ifdef CONFIG_IEEE80211R_AP
2134 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2135 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
2136 u8 pmk_r0
[PMK_LEN
], pmk_r0_name
[WPA_PMK_NAME_LEN
];
2138 if (wpa_derive_pmk_r0(fils_ft
, fils_ft_len
,
2139 conf
->ssid
, conf
->ssid_len
,
2140 conf
->mobility_domain
,
2141 conf
->r0_key_holder
,
2142 conf
->r0_key_holder_len
,
2143 sm
->addr
, pmk_r0
, pmk_r0_name
) < 0)
2146 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: PMK-R0", pmk_r0
, PMK_LEN
);
2147 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR0Name",
2148 pmk_r0_name
, WPA_PMK_NAME_LEN
);
2149 wpa_ft_store_pmk_r0(wpa_auth
, sm
->addr
, pmk_r0
, pmk_r0_name
,
2151 os_memset(fils_ft
, 0, sizeof(fils_ft
));
2153 #endif /* CONFIG_IEEE80211R_AP */
2155 res
= fils_key_auth_sk(ick
, ick_len
, snonce
, anonce
,
2156 sm
->addr
, sm
->wpa_auth
->addr
,
2157 g_sta
? wpabuf_head(g_sta
) : NULL
,
2158 g_sta
? wpabuf_len(g_sta
) : 0,
2159 g_ap
? wpabuf_head(g_ap
) : NULL
,
2160 g_ap
? wpabuf_len(g_ap
) : 0,
2161 sm
->wpa_key_mgmt
, sm
->fils_key_auth_sta
,
2162 sm
->fils_key_auth_ap
,
2163 &sm
->fils_key_auth_len
);
2164 os_memset(ick
, 0, sizeof(ick
));
2166 /* Store nonces for (Re)Association Request/Response frame processing */
2167 os_memcpy(sm
->SNonce
, snonce
, FILS_NONCE_LEN
);
2168 os_memcpy(sm
->ANonce
, anonce
, FILS_NONCE_LEN
);
2174 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
2175 u8
*buf
, size_t buf_len
, u16
*_key_data_len
)
2177 struct ieee802_1x_hdr
*hdr
;
2178 struct wpa_eapol_key
*key
;
2185 hdr
= (struct ieee802_1x_hdr
*) buf
;
2186 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2187 pos
= (u8
*) (key
+ 1);
2188 key_data_len
= WPA_GET_BE16(pos
);
2189 if (key_data_len
< AES_BLOCK_SIZE
||
2190 key_data_len
> buf_len
- sizeof(*hdr
) - sizeof(*key
) - 2) {
2191 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2192 "No room for AES-SIV data in the frame");
2195 pos
+= 2; /* Pointing at the Encrypted Key Data field */
2197 tmp
= os_malloc(key_data_len
);
2201 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2202 * to Key Data (exclusive). */
2204 aad_len
[0] = pos
- buf
;
2205 if (aes_siv_decrypt(ptk
->kek
, ptk
->kek_len
, pos
, key_data_len
,
2206 1, aad
, aad_len
, tmp
) < 0) {
2207 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2208 "Invalid AES-SIV data in the frame");
2209 bin_clear_free(tmp
, key_data_len
);
2213 /* AEAD decryption and validation completed successfully */
2214 key_data_len
-= AES_BLOCK_SIZE
;
2215 wpa_hexdump_key(MSG_DEBUG
, "WPA: Decrypted Key Data",
2218 /* Replace Key Data field with the decrypted version */
2219 os_memcpy(pos
, tmp
, key_data_len
);
2220 pos
-= 2; /* Key Data Length field */
2221 WPA_PUT_BE16(pos
, key_data_len
);
2222 bin_clear_free(tmp
, key_data_len
);
2224 *_key_data_len
= key_data_len
;
2229 const u8
* wpa_fils_validate_fils_session(struct wpa_state_machine
*sm
,
2230 const u8
*ies
, size_t ies_len
,
2231 const u8
*fils_session
)
2234 const u8
*session
= NULL
;
2236 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2237 wpa_printf(MSG_DEBUG
,
2238 "FILS: Not a FILS AKM - reject association");
2242 /* Verify Session element */
2244 end
= ((const u8
*) ie
) + ies_len
;
2245 while (ie
+ 1 < end
) {
2246 if (ie
+ 2 + ie
[1] > end
)
2248 if (ie
[0] == WLAN_EID_EXTENSION
&&
2249 ie
[1] >= 1 + FILS_SESSION_LEN
&&
2250 ie
[2] == WLAN_EID_EXT_FILS_SESSION
) {
2258 wpa_printf(MSG_DEBUG
,
2259 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2264 if (!fils_session
) {
2265 wpa_printf(MSG_DEBUG
,
2266 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2271 if (os_memcmp(fils_session
, session
+ 3, FILS_SESSION_LEN
) != 0) {
2272 wpa_printf(MSG_DEBUG
, "FILS: Session mismatch");
2273 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
2274 fils_session
, FILS_SESSION_LEN
);
2275 wpa_hexdump(MSG_DEBUG
, "FILS: Received FILS Session",
2276 session
+ 3, FILS_SESSION_LEN
);
2283 int wpa_fils_validate_key_confirm(struct wpa_state_machine
*sm
, const u8
*ies
,
2286 struct ieee802_11_elems elems
;
2288 if (ieee802_11_parse_elems(ies
, ies_len
, &elems
, 1) == ParseFailed
) {
2289 wpa_printf(MSG_DEBUG
,
2290 "FILS: Failed to parse decrypted elements");
2294 if (!elems
.fils_session
) {
2295 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
2299 if (!elems
.fils_key_confirm
) {
2300 wpa_printf(MSG_DEBUG
, "FILS: No FILS Key Confirm element");
2304 if (elems
.fils_key_confirm_len
!= sm
->fils_key_auth_len
) {
2305 wpa_printf(MSG_DEBUG
,
2306 "FILS: Unexpected Key-Auth length %d (expected %d)",
2307 elems
.fils_key_confirm_len
,
2308 (int) sm
->fils_key_auth_len
);
2312 if (os_memcmp(elems
.fils_key_confirm
, sm
->fils_key_auth_sta
,
2313 sm
->fils_key_auth_len
) != 0) {
2314 wpa_printf(MSG_DEBUG
, "FILS: Key-Auth mismatch");
2315 wpa_hexdump(MSG_DEBUG
, "FILS: Received Key-Auth",
2316 elems
.fils_key_confirm
, elems
.fils_key_confirm_len
);
2317 wpa_hexdump(MSG_DEBUG
, "FILS: Expected Key-Auth",
2318 sm
->fils_key_auth_sta
, sm
->fils_key_auth_len
);
2326 int fils_decrypt_assoc(struct wpa_state_machine
*sm
, const u8
*fils_session
,
2327 const struct ieee80211_mgmt
*mgmt
, size_t frame_len
,
2328 u8
*pos
, size_t left
)
2331 const u8
*end
, *ie_start
, *ie
, *session
, *crypt
;
2335 if (!sm
|| !sm
->PTK_valid
) {
2336 wpa_printf(MSG_DEBUG
,
2337 "FILS: No KEK to decrypt Assocication Request frame");
2341 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2342 wpa_printf(MSG_DEBUG
,
2343 "FILS: Not a FILS AKM - reject association");
2347 end
= ((const u8
*) mgmt
) + frame_len
;
2348 fc
= le_to_host16(mgmt
->frame_control
);
2349 stype
= WLAN_FC_GET_STYPE(fc
);
2350 if (stype
== WLAN_FC_STYPE_REASSOC_REQ
)
2351 ie_start
= mgmt
->u
.reassoc_req
.variable
;
2353 ie_start
= mgmt
->u
.assoc_req
.variable
;
2357 * Find FILS Session element which is the last unencrypted element in
2360 session
= wpa_fils_validate_fils_session(sm
, ie
, end
- ie
,
2363 wpa_printf(MSG_DEBUG
, "FILS: Session validation failed");
2367 crypt
= session
+ 2 + session
[1];
2369 if (end
- crypt
< AES_BLOCK_SIZE
) {
2370 wpa_printf(MSG_DEBUG
,
2371 "FILS: Too short frame to include AES-SIV data");
2375 /* AES-SIV AAD vectors */
2377 /* The STA's MAC address */
2379 aad_len
[0] = ETH_ALEN
;
2380 /* The AP's BSSID */
2382 aad_len
[1] = ETH_ALEN
;
2383 /* The STA's nonce */
2384 aad
[2] = sm
->SNonce
;
2385 aad_len
[2] = FILS_NONCE_LEN
;
2386 /* The AP's nonce */
2387 aad
[3] = sm
->ANonce
;
2388 aad_len
[3] = FILS_NONCE_LEN
;
2390 * The (Re)Association Request frame from the Capability Information
2391 * field to the FILS Session element (both inclusive).
2393 aad
[4] = (const u8
*) &mgmt
->u
.assoc_req
.capab_info
;
2394 aad_len
[4] = crypt
- aad
[4];
2396 if (aes_siv_decrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, crypt
, end
- crypt
,
2397 5, aad
, aad_len
, pos
+ (crypt
- ie_start
)) < 0) {
2398 wpa_printf(MSG_DEBUG
,
2399 "FILS: Invalid AES-SIV data in the frame");
2402 wpa_hexdump(MSG_DEBUG
, "FILS: Decrypted Association Request elements",
2403 pos
, left
- AES_BLOCK_SIZE
);
2405 if (wpa_fils_validate_key_confirm(sm
, pos
, left
- AES_BLOCK_SIZE
) < 0) {
2406 wpa_printf(MSG_DEBUG
, "FILS: Key Confirm validation failed");
2410 return left
- AES_BLOCK_SIZE
;
2414 int fils_encrypt_assoc(struct wpa_state_machine
*sm
, u8
*buf
,
2415 size_t current_len
, size_t max_len
,
2416 const struct wpabuf
*hlp
)
2418 u8
*end
= buf
+ max_len
;
2419 u8
*pos
= buf
+ current_len
;
2420 struct ieee80211_mgmt
*mgmt
;
2421 struct wpabuf
*plain
;
2425 if (!sm
|| !sm
->PTK_valid
)
2428 wpa_hexdump(MSG_DEBUG
,
2429 "FILS: Association Response frame before FILS processing",
2432 mgmt
= (struct ieee80211_mgmt
*) buf
;
2434 /* AES-SIV AAD vectors */
2436 /* The AP's BSSID */
2438 aad_len
[0] = ETH_ALEN
;
2439 /* The STA's MAC address */
2441 aad_len
[1] = ETH_ALEN
;
2442 /* The AP's nonce */
2443 aad
[2] = sm
->ANonce
;
2444 aad_len
[2] = FILS_NONCE_LEN
;
2445 /* The STA's nonce */
2446 aad
[3] = sm
->SNonce
;
2447 aad_len
[3] = FILS_NONCE_LEN
;
2449 * The (Re)Association Response frame from the Capability Information
2450 * field (the same offset in both Association and Reassociation
2451 * Response frames) to the FILS Session element (both inclusive).
2453 aad
[4] = (const u8
*) &mgmt
->u
.assoc_resp
.capab_info
;
2454 aad_len
[4] = pos
- aad
[4];
2456 /* The following elements will be encrypted with AES-SIV */
2457 plain
= fils_prepare_plainbuf(sm
, hlp
);
2459 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2463 if (pos
+ wpabuf_len(plain
) + AES_BLOCK_SIZE
> end
) {
2464 wpa_printf(MSG_DEBUG
,
2465 "FILS: Not enough room for FILS elements");
2470 wpa_hexdump_buf_key(MSG_DEBUG
, "FILS: Association Response plaintext",
2473 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
2474 wpabuf_head(plain
), wpabuf_len(plain
),
2475 5, aad
, aad_len
, pos
) < 0) {
2480 wpa_hexdump(MSG_DEBUG
,
2481 "FILS: Encrypted Association Response elements",
2482 pos
, AES_BLOCK_SIZE
+ wpabuf_len(plain
));
2483 current_len
+= wpabuf_len(plain
) + AES_BLOCK_SIZE
;
2486 sm
->fils_completed
= 1;
2492 static struct wpabuf
* fils_prepare_plainbuf(struct wpa_state_machine
*sm
,
2493 const struct wpabuf
*hlp
)
2495 struct wpabuf
*plain
;
2496 u8
*len
, *tmp
, *tmp2
;
2498 u8
*gtk
, dummy_gtk
[32];
2500 struct wpa_group
*gsm
;
2502 plain
= wpabuf_alloc(1000);
2506 /* TODO: FILS Public Key */
2508 /* FILS Key Confirmation */
2509 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2510 wpabuf_put_u8(plain
, 1 + sm
->fils_key_auth_len
); /* Length */
2511 /* Element ID Extension */
2512 wpabuf_put_u8(plain
, WLAN_EID_EXT_FILS_KEY_CONFIRM
);
2513 wpabuf_put_data(plain
, sm
->fils_key_auth_ap
, sm
->fils_key_auth_len
);
2515 /* FILS HLP Container */
2517 wpabuf_put_buf(plain
, hlp
);
2519 /* TODO: FILS IP Address Assignment */
2523 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2524 len
= wpabuf_put(plain
, 1);
2525 wpabuf_put_u8(plain
, WLAN_EID_EXT_KEY_DELIVERY
);
2526 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
,
2527 wpabuf_put(plain
, WPA_KEY_RSC_LEN
));
2529 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2530 gtk_len
= gsm
->GTK_len
;
2531 if (sm
->wpa_auth
->conf
.disable_gtk
) {
2533 * Provide unique random GTK to each STA to prevent use
2534 * of GTK in the BSS.
2536 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0) {
2542 hdr
[0] = gsm
->GN
& 0x03;
2544 tmp
= wpabuf_put(plain
, 0);
2545 tmp2
= wpa_add_kde(tmp
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
2547 wpabuf_put(plain
, tmp2
- tmp
);
2550 tmp
= wpabuf_put(plain
, 0);
2551 tmp2
= ieee80211w_kde_add(sm
, tmp
);
2552 wpabuf_put(plain
, tmp2
- tmp
);
2554 *len
= (u8
*) wpabuf_put(plain
, 0) - len
- 1;
2559 int fils_set_tk(struct wpa_state_machine
*sm
)
2564 if (!sm
|| !sm
->PTK_valid
) {
2565 wpa_printf(MSG_DEBUG
, "FILS: No valid PTK available to set TK");
2568 if (sm
->tk_already_set
) {
2569 wpa_printf(MSG_DEBUG
, "FILS: TK already set to the driver");
2573 alg
= wpa_cipher_to_alg(sm
->pairwise
);
2574 klen
= wpa_cipher_key_len(sm
->pairwise
);
2576 wpa_printf(MSG_DEBUG
, "FILS: Configure TK to the driver");
2577 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
2578 sm
->PTK
.tk
, klen
)) {
2579 wpa_printf(MSG_DEBUG
, "FILS: Failed to set TK to the driver");
2582 sm
->tk_already_set
= TRUE
;
2588 u8
* hostapd_eid_assoc_fils_session(struct wpa_state_machine
*sm
, u8
*buf
,
2589 const u8
*fils_session
, struct wpabuf
*hlp
)
2591 struct wpabuf
*plain
;
2595 *pos
++ = WLAN_EID_EXTENSION
; /* Element ID */
2596 *pos
++ = 1 + FILS_SESSION_LEN
; /* Length */
2597 *pos
++ = WLAN_EID_EXT_FILS_SESSION
; /* Element ID Extension */
2598 os_memcpy(pos
, fils_session
, FILS_SESSION_LEN
);
2599 pos
+= FILS_SESSION_LEN
;
2601 plain
= fils_prepare_plainbuf(sm
, hlp
);
2603 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2607 os_memcpy(pos
, wpabuf_head(plain
), wpabuf_len(plain
));
2608 pos
+= wpabuf_len(plain
);
2610 wpa_printf(MSG_DEBUG
, "%s: plain buf_len: %u", __func__
,
2611 (unsigned int) wpabuf_len(plain
));
2613 sm
->fils_completed
= 1;
2617 #endif /* CONFIG_FILS */
2620 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING
)
2622 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2624 int ok
= 0, psk_found
= 0;
2625 const u8
*pmk
= NULL
;
2628 const u8
*eapol_key_ie
, *key_data
, *mic
;
2629 u16 key_data_length
;
2630 size_t mic_len
, eapol_key_ie_len
;
2631 struct ieee802_1x_hdr
*hdr
;
2632 struct wpa_eapol_key
*key
;
2633 struct wpa_eapol_ie_parse kde
;
2635 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING
, wpa_ptk
);
2636 sm
->EAPOLKeyReceived
= FALSE
;
2637 sm
->update_snonce
= FALSE
;
2638 os_memset(&PTK
, 0, sizeof(PTK
));
2640 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
2642 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2643 * WPA-PSK: iterate through possible PSKs and select the one matching
2646 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
2647 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
2648 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
2649 sm
->p2p_dev_addr
, pmk
, &pmk_len
);
2653 #ifdef CONFIG_IEEE80211R_AP
2654 if (wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
2655 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
2656 sm
->xxkey_len
= pmk_len
;
2658 #endif /* CONFIG_IEEE80211R_AP */
2661 pmk_len
= sm
->pmk_len
;
2664 if (wpa_derive_ptk(sm
, sm
->SNonce
, pmk
, pmk_len
, &PTK
) < 0)
2668 wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
2669 sm
->last_rx_eapol_key
,
2670 sm
->last_rx_eapol_key_len
) == 0) {
2677 wpa_aead_decrypt(sm
, &PTK
, sm
->last_rx_eapol_key
,
2678 sm
->last_rx_eapol_key_len
, NULL
) == 0) {
2682 #endif /* CONFIG_FILS */
2684 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
2685 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
))
2690 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2691 "invalid MIC in msg 2/4 of 4-Way Handshake");
2693 wpa_auth_psk_failure_report(sm
->wpa_auth
, sm
->addr
);
2698 * Note: last_rx_eapol_key length fields have already been validated in
2701 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
2702 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2703 mic
= (u8
*) (key
+ 1);
2704 key_data
= mic
+ mic_len
+ 2;
2705 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
2706 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
2707 sizeof(*key
) - mic_len
- 2)
2710 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
2711 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2712 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2716 eapol_key_ie
= kde
.rsn_ie
;
2717 eapol_key_ie_len
= kde
.rsn_ie_len
;
2718 } else if (kde
.osen
) {
2719 eapol_key_ie
= kde
.osen
;
2720 eapol_key_ie_len
= kde
.osen_len
;
2722 eapol_key_ie
= kde
.wpa_ie
;
2723 eapol_key_ie_len
= kde
.wpa_ie_len
;
2725 ft
= sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
);
2726 if (sm
->wpa_ie
== NULL
||
2727 wpa_compare_rsn_ie(ft
, sm
->wpa_ie
, sm
->wpa_ie_len
,
2728 eapol_key_ie
, eapol_key_ie_len
)) {
2729 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2730 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2732 wpa_hexdump(MSG_DEBUG
, "WPA IE in AssocReq",
2733 sm
->wpa_ie
, sm
->wpa_ie_len
);
2735 wpa_hexdump(MSG_DEBUG
, "WPA IE in msg 2/4",
2736 eapol_key_ie
, eapol_key_ie_len
);
2737 /* MLME-DEAUTHENTICATE.request */
2738 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
2739 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2742 #ifdef CONFIG_IEEE80211R_AP
2743 if (ft
&& ft_check_msg_2_of_4(wpa_auth
, sm
, &kde
) < 0) {
2744 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
2745 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2748 #endif /* CONFIG_IEEE80211R_AP */
2750 if (kde
.ip_addr_req
&& kde
.ip_addr_req
[0] &&
2751 wpa_auth
->ip_pool
&& WPA_GET_BE32(sm
->ip_addr
) == 0) {
2753 wpa_printf(MSG_DEBUG
,
2754 "P2P: IP address requested in EAPOL-Key exchange");
2755 idx
= bitfield_get_first_zero(wpa_auth
->ip_pool
);
2757 u32 start
= WPA_GET_BE32(wpa_auth
->conf
.ip_addr_start
);
2758 bitfield_set(wpa_auth
->ip_pool
, idx
);
2759 WPA_PUT_BE32(sm
->ip_addr
, start
+ idx
);
2760 wpa_printf(MSG_DEBUG
,
2761 "P2P: Assigned IP address %u.%u.%u.%u to "
2762 MACSTR
, sm
->ip_addr
[0], sm
->ip_addr
[1],
2763 sm
->ip_addr
[2], sm
->ip_addr
[3],
2767 #endif /* CONFIG_P2P */
2769 #ifdef CONFIG_IEEE80211R_AP
2770 if (sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2772 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2773 * with the value we derived.
2775 if (os_memcmp_const(sm
->sup_pmk_r1_name
, sm
->pmk_r1_name
,
2776 WPA_PMK_NAME_LEN
) != 0) {
2777 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2778 "PMKR1Name mismatch in FT 4-way "
2780 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from "
2782 sm
->sup_pmk_r1_name
, WPA_PMK_NAME_LEN
);
2783 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
2784 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
2788 #endif /* CONFIG_IEEE80211R_AP */
2790 sm
->pending_1_of_4_timeout
= 0;
2791 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
2793 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
2794 /* PSK may have changed from the previous choice, so update
2795 * state machine data based on whatever PSK was selected here.
2797 os_memcpy(sm
->PMK
, pmk
, PMK_LEN
);
2798 sm
->pmk_len
= PMK_LEN
;
2801 sm
->MICVerified
= TRUE
;
2803 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
2804 sm
->PTK_valid
= TRUE
;
2808 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING2
)
2810 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING2
, wpa_ptk
);
2815 #ifdef CONFIG_IEEE80211W
2817 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
2819 if (sm
->mgmt_frame_prot
) {
2821 len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
2822 return 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
+ len
;
2829 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
2831 struct wpa_igtk_kde igtk
;
2832 struct wpa_group
*gsm
= sm
->group
;
2833 u8 rsc
[WPA_KEY_RSC_LEN
];
2834 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
2836 if (!sm
->mgmt_frame_prot
)
2839 igtk
.keyid
[0] = gsm
->GN_igtk
;
2841 if (gsm
->wpa_group_state
!= WPA_GROUP_SETKEYSDONE
||
2842 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, rsc
) < 0)
2843 os_memset(igtk
.pn
, 0, sizeof(igtk
.pn
));
2845 os_memcpy(igtk
.pn
, rsc
, sizeof(igtk
.pn
));
2846 os_memcpy(igtk
.igtk
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
2847 if (sm
->wpa_auth
->conf
.disable_gtk
) {
2849 * Provide unique random IGTK to each STA to prevent use of
2852 if (random_get_bytes(igtk
.igtk
, len
) < 0)
2855 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_IGTK
,
2856 (const u8
*) &igtk
, WPA_IGTK_KDE_PREFIX_LEN
+ len
,
2862 #else /* CONFIG_IEEE80211W */
2864 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
2870 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
2875 #endif /* CONFIG_IEEE80211W */
2878 SM_STATE(WPA_PTK
, PTKINITNEGOTIATING
)
2880 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
, dummy_gtk
[32];
2881 size_t gtk_len
, kde_len
;
2882 struct wpa_group
*gsm
= sm
->group
;
2884 int wpa_ie_len
, secure
, keyidx
, encr
= 0;
2886 SM_ENTRY_MA(WPA_PTK
, PTKINITNEGOTIATING
, wpa_ptk
);
2887 sm
->TimeoutEvt
= FALSE
;
2890 if (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
2891 sm
->TimeoutCtr
> 1) {
2892 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
2895 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
2896 /* No point in sending the EAPOL-Key - we will disconnect
2897 * immediately following this. */
2901 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2902 GTK[GN], IGTK, [FTIE], [TIE * 2])
2904 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
2905 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
2906 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2907 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
2908 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
2909 if (sm
->wpa
== WPA_VERSION_WPA
&&
2910 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
2911 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
2912 /* WPA-only STA, remove RSN IE and possible MDIE */
2913 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
2914 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
2915 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
2916 wpa_ie_len
= wpa_ie
[1] + 2;
2918 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2919 "sending 3/4 msg of 4-Way Handshake");
2920 if (sm
->wpa
== WPA_VERSION_WPA2
) {
2921 /* WPA2 send GTK in the 4-way handshake */
2923 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2924 gtk_len
= gsm
->GTK_len
;
2925 if (sm
->wpa_auth
->conf
.disable_gtk
) {
2927 * Provide unique random GTK to each STA to prevent use
2928 * of GTK in the BSS.
2930 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0)
2938 /* WPA does not include GTK in msg 3/4 */
2944 if (sm
->rx_eapol_key_secure
) {
2946 * It looks like Windows 7 supplicant tries to use
2947 * Secure bit in msg 2/4 after having reported Michael
2948 * MIC failure and it then rejects the 4-way handshake
2949 * if msg 3/4 does not set Secure bit. Work around this
2950 * by setting the Secure bit here even in the case of
2951 * WPA if the supplicant used it first.
2953 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2954 "STA used Secure bit in WPA msg 2/4 - "
2955 "set Secure for 3/4 as workaround");
2960 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
);
2962 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
2963 #ifdef CONFIG_IEEE80211R_AP
2964 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2965 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
2966 kde_len
+= 300; /* FTIE + 2 * TIE */
2968 #endif /* CONFIG_IEEE80211R_AP */
2970 if (WPA_GET_BE32(sm
->ip_addr
) > 0)
2971 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 3 * 4;
2972 #endif /* CONFIG_P2P */
2973 kde
= os_malloc(kde_len
);
2978 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
2980 #ifdef CONFIG_IEEE80211R_AP
2981 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2986 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
2988 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
2989 "PMKR1Name into RSN IE in EAPOL-Key data");
2996 #endif /* CONFIG_IEEE80211R_AP */
2999 hdr
[0] = keyidx
& 0x03;
3001 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3004 pos
= ieee80211w_kde_add(sm
, pos
);
3006 #ifdef CONFIG_IEEE80211R_AP
3007 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3009 struct wpa_auth_config
*conf
;
3011 conf
= &sm
->wpa_auth
->conf
;
3012 if (sm
->assoc_resp_ftie
&&
3013 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
3014 os_memcpy(pos
, sm
->assoc_resp_ftie
,
3015 2 + sm
->assoc_resp_ftie
[1]);
3016 res
= 2 + sm
->assoc_resp_ftie
[1];
3018 res
= wpa_write_ftie(conf
, conf
->r0_key_holder
,
3019 conf
->r0_key_holder_len
,
3021 kde
+ kde_len
- pos
,
3025 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
3026 "into EAPOL-Key Key Data");
3032 /* TIE[ReassociationDeadline] (TU) */
3033 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3035 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
3036 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
3039 /* TIE[KeyLifetime] (seconds) */
3040 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3042 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
3043 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
* 60);
3046 #endif /* CONFIG_IEEE80211R_AP */
3048 if (WPA_GET_BE32(sm
->ip_addr
) > 0) {
3050 os_memcpy(addr
, sm
->ip_addr
, 4);
3051 os_memcpy(addr
+ 4, sm
->wpa_auth
->conf
.ip_addr_mask
, 4);
3052 os_memcpy(addr
+ 8, sm
->wpa_auth
->conf
.ip_addr_go
, 4);
3053 pos
= wpa_add_kde(pos
, WFA_KEY_DATA_IP_ADDR_ALLOC
,
3054 addr
, sizeof(addr
), NULL
, 0);
3056 #endif /* CONFIG_P2P */
3058 wpa_send_eapol(sm
->wpa_auth
, sm
,
3059 (secure
? WPA_KEY_INFO_SECURE
: 0) |
3060 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3061 WPA_KEY_INFO_MIC
: 0) |
3062 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
3063 WPA_KEY_INFO_KEY_TYPE
,
3064 _rsc
, sm
->ANonce
, kde
, pos
- kde
, keyidx
, encr
);
3069 SM_STATE(WPA_PTK
, PTKINITDONE
)
3071 SM_ENTRY_MA(WPA_PTK
, PTKINITDONE
, wpa_ptk
);
3072 sm
->EAPOLKeyReceived
= FALSE
;
3074 enum wpa_alg alg
= wpa_cipher_to_alg(sm
->pairwise
);
3075 int klen
= wpa_cipher_key_len(sm
->pairwise
);
3076 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
3077 sm
->PTK
.tk
, klen
)) {
3078 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
,
3079 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3082 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3083 sm
->pairwise_set
= TRUE
;
3085 if (sm
->wpa_auth
->conf
.wpa_ptk_rekey
) {
3086 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
3087 eloop_register_timeout(sm
->wpa_auth
->conf
.
3088 wpa_ptk_rekey
, 0, wpa_rekey_ptk
,
3092 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3093 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
3094 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
3095 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3096 WPA_EAPOL_authorized
, 1);
3100 if (0 /* IBSS == TRUE */) {
3102 if (sm
->keycount
== 2) {
3103 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3104 WPA_EAPOL_portValid
, 1);
3107 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
,
3110 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyAvailable
, 0);
3111 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyDone
, 1);
3112 if (sm
->wpa
== WPA_VERSION_WPA
)
3113 sm
->PInitAKeys
= TRUE
;
3116 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3117 "pairwise key handshake completed (%s)",
3118 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3120 #ifdef CONFIG_IEEE80211R_AP
3121 wpa_ft_push_pmk_r1(sm
->wpa_auth
, sm
->addr
);
3122 #endif /* CONFIG_IEEE80211R_AP */
3128 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3131 SM_ENTER(WPA_PTK
, INITIALIZE
);
3132 else if (sm
->Disconnect
3133 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3134 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3135 "WPA_PTK: sm->Disconnect");
3136 SM_ENTER(WPA_PTK
, DISCONNECT
);
3138 else if (sm
->DeauthenticationRequest
)
3139 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3140 else if (sm
->AuthenticationRequest
)
3141 SM_ENTER(WPA_PTK
, AUTHENTICATION
);
3142 else if (sm
->ReAuthenticationRequest
)
3143 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3144 else if (sm
->PTKRequest
) {
3145 if (wpa_auth_sm_ptk_update(sm
) < 0)
3146 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3148 SM_ENTER(WPA_PTK
, PTKSTART
);
3149 } else switch (sm
->wpa_ptk_state
) {
3150 case WPA_PTK_INITIALIZE
:
3152 case WPA_PTK_DISCONNECT
:
3153 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3155 case WPA_PTK_DISCONNECTED
:
3156 SM_ENTER(WPA_PTK
, INITIALIZE
);
3158 case WPA_PTK_AUTHENTICATION
:
3159 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3161 case WPA_PTK_AUTHENTICATION2
:
3162 if (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
3163 wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
3164 WPA_EAPOL_keyRun
) > 0)
3165 SM_ENTER(WPA_PTK
, INITPMK
);
3166 else if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3167 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
3168 /* FIX: && 802.1X::keyRun */)
3169 SM_ENTER(WPA_PTK
, INITPSK
);
3170 else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
)
3171 SM_ENTER(WPA_PTK
, INITPMK
);
3173 case WPA_PTK_INITPMK
:
3174 if (wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
3175 WPA_EAPOL_keyAvailable
) > 0) {
3176 SM_ENTER(WPA_PTK
, PTKSTART
);
3178 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->pmksa
) {
3179 SM_ENTER(WPA_PTK
, PTKSTART
);
3180 #endif /* CONFIG_DPP */
3182 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3183 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3184 "INITPMK - keyAvailable = false");
3185 SM_ENTER(WPA_PTK
, DISCONNECT
);
3188 case WPA_PTK_INITPSK
:
3189 if (wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
,
3191 SM_ENTER(WPA_PTK
, PTKSTART
);
3193 } else if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
3194 SM_ENTER(WPA_PTK
, PTKSTART
);
3195 #endif /* CONFIG_SAE */
3197 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3198 "no PSK configured for the STA");
3199 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3200 SM_ENTER(WPA_PTK
, DISCONNECT
);
3203 case WPA_PTK_PTKSTART
:
3204 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3205 sm
->EAPOLKeyPairwise
)
3206 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3207 else if (sm
->TimeoutCtr
>
3208 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
3209 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3211 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3212 "PTKSTART: Retry limit %u reached",
3213 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3214 SM_ENTER(WPA_PTK
, DISCONNECT
);
3215 } else if (sm
->TimeoutEvt
)
3216 SM_ENTER(WPA_PTK
, PTKSTART
);
3218 case WPA_PTK_PTKCALCNEGOTIATING
:
3219 if (sm
->MICVerified
)
3220 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING2
);
3221 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3222 sm
->EAPOLKeyPairwise
)
3223 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3224 else if (sm
->TimeoutEvt
)
3225 SM_ENTER(WPA_PTK
, PTKSTART
);
3227 case WPA_PTK_PTKCALCNEGOTIATING2
:
3228 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3230 case WPA_PTK_PTKINITNEGOTIATING
:
3231 if (sm
->update_snonce
)
3232 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3233 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3234 sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3235 SM_ENTER(WPA_PTK
, PTKINITDONE
);
3236 else if (sm
->TimeoutCtr
>
3237 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
||
3238 (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3239 sm
->TimeoutCtr
> 1)) {
3240 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3242 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3243 "PTKINITNEGOTIATING: Retry limit %u reached",
3244 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3245 SM_ENTER(WPA_PTK
, DISCONNECT
);
3246 } else if (sm
->TimeoutEvt
)
3247 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3249 case WPA_PTK_PTKINITDONE
:
3255 SM_STATE(WPA_PTK_GROUP
, IDLE
)
3257 SM_ENTRY_MA(WPA_PTK_GROUP
, IDLE
, wpa_ptk_group
);
3259 /* Init flag is not cleared here, so avoid busy
3260 * loop by claiming nothing changed. */
3261 sm
->changed
= FALSE
;
3263 sm
->GTimeoutCtr
= 0;
3267 SM_STATE(WPA_PTK_GROUP
, REKEYNEGOTIATING
)
3269 u8 rsc
[WPA_KEY_RSC_LEN
];
3270 struct wpa_group
*gsm
= sm
->group
;
3272 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
3274 u8
*gtk
, dummy_gtk
[32];
3276 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYNEGOTIATING
, wpa_ptk_group
);
3279 if (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3280 sm
->GTimeoutCtr
> 1) {
3281 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3284 if (sm
->GTimeoutCtr
> sm
->wpa_auth
->conf
.wpa_group_update_count
) {
3285 /* No point in sending the EAPOL-Key - we will disconnect
3286 * immediately following this. */
3290 if (sm
->wpa
== WPA_VERSION_WPA
)
3291 sm
->PInitAKeys
= FALSE
;
3292 sm
->TimeoutEvt
= FALSE
;
3293 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3294 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3295 if (gsm
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
)
3296 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3297 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3298 "sending 1/2 msg of Group Key Handshake");
3300 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3301 if (sm
->wpa_auth
->conf
.disable_gtk
) {
3303 * Provide unique random GTK to each STA to prevent use
3304 * of GTK in the BSS.
3306 if (random_get_bytes(dummy_gtk
, gsm
->GTK_len
) < 0)
3310 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3311 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
3312 ieee80211w_kde_len(sm
);
3313 kde_buf
= os_malloc(kde_len
);
3314 if (kde_buf
== NULL
)
3317 kde
= pos
= kde_buf
;
3318 hdr
[0] = gsm
->GN
& 0x03;
3320 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3322 pos
= ieee80211w_kde_add(sm
, pos
);
3323 kde_len
= pos
- kde
;
3326 kde_len
= gsm
->GTK_len
;
3329 wpa_send_eapol(sm
->wpa_auth
, sm
,
3330 WPA_KEY_INFO_SECURE
|
3331 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3332 WPA_KEY_INFO_MIC
: 0) |
3334 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
3335 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
3341 SM_STATE(WPA_PTK_GROUP
, REKEYESTABLISHED
)
3343 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYESTABLISHED
, wpa_ptk_group
);
3344 sm
->EAPOLKeyReceived
= FALSE
;
3345 if (sm
->GUpdateStationKeys
)
3346 sm
->group
->GKeyDoneStations
--;
3347 sm
->GUpdateStationKeys
= FALSE
;
3348 sm
->GTimeoutCtr
= 0;
3349 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3350 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3351 "group key handshake completed (%s)",
3352 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3357 SM_STATE(WPA_PTK_GROUP
, KEYERROR
)
3359 SM_ENTRY_MA(WPA_PTK_GROUP
, KEYERROR
, wpa_ptk_group
);
3360 if (sm
->GUpdateStationKeys
)
3361 sm
->group
->GKeyDoneStations
--;
3362 sm
->GUpdateStationKeys
= FALSE
;
3363 sm
->Disconnect
= TRUE
;
3364 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3365 "group key handshake failed (%s) after %u tries",
3366 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN",
3367 sm
->wpa_auth
->conf
.wpa_group_update_count
);
3371 SM_STEP(WPA_PTK_GROUP
)
3373 if (sm
->Init
|| sm
->PtkGroupInit
) {
3374 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3375 sm
->PtkGroupInit
= FALSE
;
3376 } else switch (sm
->wpa_ptk_group_state
) {
3377 case WPA_PTK_GROUP_IDLE
:
3378 if (sm
->GUpdateStationKeys
||
3379 (sm
->wpa
== WPA_VERSION_WPA
&& sm
->PInitAKeys
))
3380 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3382 case WPA_PTK_GROUP_REKEYNEGOTIATING
:
3383 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3384 !sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3385 SM_ENTER(WPA_PTK_GROUP
, REKEYESTABLISHED
);
3386 else if (sm
->GTimeoutCtr
>
3387 sm
->wpa_auth
->conf
.wpa_group_update_count
||
3388 (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3389 sm
->GTimeoutCtr
> 1))
3390 SM_ENTER(WPA_PTK_GROUP
, KEYERROR
);
3391 else if (sm
->TimeoutEvt
)
3392 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3394 case WPA_PTK_GROUP_KEYERROR
:
3395 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3397 case WPA_PTK_GROUP_REKEYESTABLISHED
:
3398 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3404 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
3405 struct wpa_group
*group
)
3409 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3410 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3411 if (wpa_gmk_to_gtk(group
->GMK
, "Group key expansion",
3412 wpa_auth
->addr
, group
->GNonce
,
3413 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3415 wpa_hexdump_key(MSG_DEBUG
, "GTK",
3416 group
->GTK
[group
->GN
- 1], group
->GTK_len
);
3418 #ifdef CONFIG_IEEE80211W
3419 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3421 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3422 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3423 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3424 if (wpa_gmk_to_gtk(group
->GMK
, "IGTK key expansion",
3425 wpa_auth
->addr
, group
->GNonce
,
3426 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3428 wpa_hexdump_key(MSG_DEBUG
, "IGTK",
3429 group
->IGTK
[group
->GN_igtk
- 4], len
);
3431 #endif /* CONFIG_IEEE80211W */
3437 static void wpa_group_gtk_init(struct wpa_authenticator
*wpa_auth
,
3438 struct wpa_group
*group
)
3440 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3441 "GTK_INIT (VLAN-ID %d)", group
->vlan_id
);
3442 group
->changed
= FALSE
; /* GInit is not cleared here; avoid loop */
3443 group
->wpa_group_state
= WPA_GROUP_GTK_INIT
;
3446 os_memset(group
->GTK
, 0, sizeof(group
->GTK
));
3449 #ifdef CONFIG_IEEE80211W
3452 #endif /* CONFIG_IEEE80211W */
3453 /* GTK[GN] = CalcGTK() */
3454 wpa_gtk_update(wpa_auth
, group
);
3458 static int wpa_group_update_sta(struct wpa_state_machine
*sm
, void *ctx
)
3460 if (ctx
!= NULL
&& ctx
!= sm
->group
)
3463 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITDONE
) {
3464 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3465 "Not in PTKINITDONE; skip Group Key update");
3466 sm
->GUpdateStationKeys
= FALSE
;
3469 if (sm
->GUpdateStationKeys
) {
3471 * This should not really happen, so add a debug log entry.
3472 * Since we clear the GKeyDoneStations before the loop, the
3473 * station needs to be counted here anyway.
3475 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3476 "GUpdateStationKeys was already set when "
3477 "marking station for GTK rekeying");
3480 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
3481 if (sm
->is_wnmsleep
)
3484 sm
->group
->GKeyDoneStations
++;
3485 sm
->GUpdateStationKeys
= TRUE
;
3492 #ifdef CONFIG_WNM_AP
3493 /* update GTK when exiting WNM-Sleep Mode */
3494 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine
*sm
)
3496 if (sm
== NULL
|| sm
->is_wnmsleep
)
3499 wpa_group_update_sta(sm
, NULL
);
3503 void wpa_set_wnmsleep(struct wpa_state_machine
*sm
, int flag
)
3506 sm
->is_wnmsleep
= !!flag
;
3510 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3512 struct wpa_group
*gsm
= sm
->group
;
3517 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
3520 *pos
++ = WNM_SLEEP_SUBELEM_GTK
;
3521 *pos
++ = 11 + gsm
->GTK_len
;
3522 /* Key ID in B0-B1 of Key Info */
3523 WPA_PUT_LE16(pos
, gsm
->GN
& 0x03);
3525 *pos
++ = gsm
->GTK_len
;
3526 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, pos
) != 0)
3529 os_memcpy(pos
, gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
3530 pos
+= gsm
->GTK_len
;
3532 wpa_printf(MSG_DEBUG
, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3534 wpa_hexdump_key(MSG_DEBUG
, "WNM: GTK in WNM-Sleep Mode exit",
3535 gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
3541 #ifdef CONFIG_IEEE80211W
3542 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3544 struct wpa_group
*gsm
= sm
->group
;
3546 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3550 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3552 *pos
++ = WNM_SLEEP_SUBELEM_IGTK
;
3553 *pos
++ = 2 + 6 + len
;
3554 WPA_PUT_LE16(pos
, gsm
->GN_igtk
);
3556 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, pos
) != 0)
3560 os_memcpy(pos
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3563 wpa_printf(MSG_DEBUG
, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3565 wpa_hexdump_key(MSG_DEBUG
, "WNM: IGTK in WNM-Sleep Mode exit",
3566 gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3570 #endif /* CONFIG_IEEE80211W */
3571 #endif /* CONFIG_WNM_AP */
3574 static void wpa_group_setkeys(struct wpa_authenticator
*wpa_auth
,
3575 struct wpa_group
*group
)
3579 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3580 "SETKEYS (VLAN-ID %d)", group
->vlan_id
);
3581 group
->changed
= TRUE
;
3582 group
->wpa_group_state
= WPA_GROUP_SETKEYS
;
3583 group
->GTKReKey
= FALSE
;
3585 group
->GM
= group
->GN
;
3587 #ifdef CONFIG_IEEE80211W
3588 tmp
= group
->GM_igtk
;
3589 group
->GM_igtk
= group
->GN_igtk
;
3590 group
->GN_igtk
= tmp
;
3591 #endif /* CONFIG_IEEE80211W */
3592 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3593 * counting the STAs that are marked with GUpdateStationKeys instead of
3594 * including all STAs that could be in not-yet-completed state. */
3595 wpa_gtk_update(wpa_auth
, group
);
3597 if (group
->GKeyDoneStations
) {
3598 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: Unexpected "
3599 "GKeyDoneStations=%d when starting new GTK rekey",
3600 group
->GKeyDoneStations
);
3601 group
->GKeyDoneStations
= 0;
3603 wpa_auth_for_each_sta(wpa_auth
, wpa_group_update_sta
, group
);
3604 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: GKeyDoneStations=%d",
3605 group
->GKeyDoneStations
);
3609 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
3610 struct wpa_group
*group
)
3614 if (wpa_auth_set_key(wpa_auth
, group
->vlan_id
,
3615 wpa_cipher_to_alg(wpa_auth
->conf
.wpa_group
),
3616 broadcast_ether_addr
, group
->GN
,
3617 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3620 #ifdef CONFIG_IEEE80211W
3621 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3625 alg
= wpa_cipher_to_alg(wpa_auth
->conf
.group_mgmt_cipher
);
3626 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3629 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, alg
,
3630 broadcast_ether_addr
, group
->GN_igtk
,
3631 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3634 #endif /* CONFIG_IEEE80211W */
3640 static int wpa_group_disconnect_cb(struct wpa_state_machine
*sm
, void *ctx
)
3642 if (sm
->group
== ctx
) {
3643 wpa_printf(MSG_DEBUG
, "WPA: Mark STA " MACSTR
3644 " for discconnection due to fatal failure",
3646 sm
->Disconnect
= TRUE
;
3653 static void wpa_group_fatal_failure(struct wpa_authenticator
*wpa_auth
,
3654 struct wpa_group
*group
)
3656 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state FATAL_FAILURE");
3657 group
->changed
= TRUE
;
3658 group
->wpa_group_state
= WPA_GROUP_FATAL_FAILURE
;
3659 wpa_auth_for_each_sta(wpa_auth
, wpa_group_disconnect_cb
, group
);
3663 static int wpa_group_setkeysdone(struct wpa_authenticator
*wpa_auth
,
3664 struct wpa_group
*group
)
3666 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3667 "SETKEYSDONE (VLAN-ID %d)", group
->vlan_id
);
3668 group
->changed
= TRUE
;
3669 group
->wpa_group_state
= WPA_GROUP_SETKEYSDONE
;
3671 if (wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
3672 wpa_group_fatal_failure(wpa_auth
, group
);
3680 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
3681 struct wpa_group
*group
)
3684 wpa_group_gtk_init(wpa_auth
, group
);
3685 } else if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
) {
3686 /* Do not allow group operations */
3687 } else if (group
->wpa_group_state
== WPA_GROUP_GTK_INIT
&&
3688 group
->GTKAuthenticator
) {
3689 wpa_group_setkeysdone(wpa_auth
, group
);
3690 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
&&
3692 wpa_group_setkeys(wpa_auth
, group
);
3693 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYS
) {
3694 if (group
->GKeyDoneStations
== 0)
3695 wpa_group_setkeysdone(wpa_auth
, group
);
3696 else if (group
->GTKReKey
)
3697 wpa_group_setkeys(wpa_auth
, group
);
3702 static int wpa_sm_step(struct wpa_state_machine
*sm
)
3707 if (sm
->in_step_loop
) {
3708 /* This should not happen, but if it does, make sure we do not
3709 * end up freeing the state machine too early by exiting the
3710 * recursive call. */
3711 wpa_printf(MSG_ERROR
, "WPA: wpa_sm_step() called recursively");
3715 sm
->in_step_loop
= 1;
3717 if (sm
->pending_deinit
)
3720 sm
->changed
= FALSE
;
3721 sm
->wpa_auth
->group
->changed
= FALSE
;
3723 SM_STEP_RUN(WPA_PTK
);
3724 if (sm
->pending_deinit
)
3726 SM_STEP_RUN(WPA_PTK_GROUP
);
3727 if (sm
->pending_deinit
)
3729 wpa_group_sm_step(sm
->wpa_auth
, sm
->group
);
3730 } while (sm
->changed
|| sm
->wpa_auth
->group
->changed
);
3731 sm
->in_step_loop
= 0;
3733 if (sm
->pending_deinit
) {
3734 wpa_printf(MSG_DEBUG
, "WPA: Completing pending STA state "
3735 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
3736 wpa_free_sta_sm(sm
);
3743 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
)
3745 struct wpa_state_machine
*sm
= eloop_ctx
;
3750 void wpa_auth_sm_notify(struct wpa_state_machine
*sm
)
3754 eloop_register_timeout(0, 0, wpa_sm_call_step
, sm
, NULL
);
3758 void wpa_gtk_rekey(struct wpa_authenticator
*wpa_auth
)
3761 struct wpa_group
*group
;
3763 if (wpa_auth
== NULL
)
3766 group
= wpa_auth
->group
;
3768 for (i
= 0; i
< 2; i
++) {
3770 group
->GM
= group
->GN
;
3772 #ifdef CONFIG_IEEE80211W
3773 tmp
= group
->GM_igtk
;
3774 group
->GM_igtk
= group
->GN_igtk
;
3775 group
->GN_igtk
= tmp
;
3776 #endif /* CONFIG_IEEE80211W */
3777 wpa_gtk_update(wpa_auth
, group
);
3778 wpa_group_config_group_keys(wpa_auth
, group
);
3783 static const char * wpa_bool_txt(int val
)
3785 return val
? "TRUE" : "FALSE";
3789 #define RSN_SUITE "%02x-%02x-%02x-%d"
3790 #define RSN_SUITE_ARG(s) \
3791 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3793 int wpa_get_mib(struct wpa_authenticator
*wpa_auth
, char *buf
, size_t buflen
)
3796 char pmkid_txt
[PMKID_LEN
* 2 + 1];
3797 #ifdef CONFIG_RSN_PREAUTH
3798 const int preauth
= 1;
3799 #else /* CONFIG_RSN_PREAUTH */
3800 const int preauth
= 0;
3801 #endif /* CONFIG_RSN_PREAUTH */
3803 if (wpa_auth
== NULL
)
3806 ret
= os_snprintf(buf
+ len
, buflen
- len
,
3807 "dot11RSNAOptionImplemented=TRUE\n"
3808 "dot11RSNAPreauthenticationImplemented=%s\n"
3809 "dot11RSNAEnabled=%s\n"
3810 "dot11RSNAPreauthenticationEnabled=%s\n",
3811 wpa_bool_txt(preauth
),
3812 wpa_bool_txt(wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
),
3813 wpa_bool_txt(wpa_auth
->conf
.rsn_preauth
));
3814 if (os_snprintf_error(buflen
- len
, ret
))
3818 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
3819 wpa_auth
->dot11RSNAPMKIDUsed
, PMKID_LEN
);
3822 buf
+ len
, buflen
- len
,
3823 "dot11RSNAConfigVersion=%u\n"
3824 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
3825 /* FIX: dot11RSNAConfigGroupCipher */
3826 /* FIX: dot11RSNAConfigGroupRekeyMethod */
3827 /* FIX: dot11RSNAConfigGroupRekeyTime */
3828 /* FIX: dot11RSNAConfigGroupRekeyPackets */
3829 "dot11RSNAConfigGroupRekeyStrict=%u\n"
3830 "dot11RSNAConfigGroupUpdateCount=%u\n"
3831 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
3832 "dot11RSNAConfigGroupCipherSize=%u\n"
3833 "dot11RSNAConfigPMKLifetime=%u\n"
3834 "dot11RSNAConfigPMKReauthThreshold=%u\n"
3835 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
3836 "dot11RSNAConfigSATimeout=%u\n"
3837 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
3838 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
3839 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
3840 "dot11RSNAPMKIDUsed=%s\n"
3841 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
3842 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
3843 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
3844 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
3845 "dot11RSNA4WayHandshakeFailures=%u\n"
3846 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
3848 !!wpa_auth
->conf
.wpa_strict_rekey
,
3849 wpa_auth
->conf
.wpa_group_update_count
,
3850 wpa_auth
->conf
.wpa_pairwise_update_count
,
3851 wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
) * 8,
3852 dot11RSNAConfigPMKLifetime
,
3853 dot11RSNAConfigPMKReauthThreshold
,
3854 dot11RSNAConfigSATimeout
,
3855 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteSelected
),
3856 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherSelected
),
3857 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherSelected
),
3859 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteRequested
),
3860 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherRequested
),
3861 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherRequested
),
3862 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
,
3863 wpa_auth
->dot11RSNA4WayHandshakeFailures
);
3864 if (os_snprintf_error(buflen
- len
, ret
))
3868 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
3869 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
3872 ret
= os_snprintf(buf
+ len
, buflen
- len
, "hostapdWPAGroupState=%d\n",
3873 wpa_auth
->group
->wpa_group_state
);
3874 if (os_snprintf_error(buflen
- len
, ret
))
3882 int wpa_get_mib_sta(struct wpa_state_machine
*sm
, char *buf
, size_t buflen
)
3890 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
3892 /* dot11RSNAStatsEntry */
3894 pairwise
= wpa_cipher_to_suite(sm
->wpa
== WPA_VERSION_WPA2
?
3895 WPA_PROTO_RSN
: WPA_PROTO_WPA
,
3901 buf
+ len
, buflen
- len
,
3902 /* TODO: dot11RSNAStatsIndex */
3903 "dot11RSNAStatsSTAAddress=" MACSTR
"\n"
3904 "dot11RSNAStatsVersion=1\n"
3905 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE
"\n"
3906 /* TODO: dot11RSNAStatsTKIPICVErrors */
3907 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
3908 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
3909 /* TODO: dot11RSNAStatsCCMPReplays */
3910 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
3911 /* TODO: dot11RSNAStatsTKIPReplays */,
3913 RSN_SUITE_ARG(pairwise
),
3914 sm
->dot11RSNAStatsTKIPLocalMICFailures
,
3915 sm
->dot11RSNAStatsTKIPRemoteMICFailures
);
3916 if (os_snprintf_error(buflen
- len
, ret
))
3921 ret
= os_snprintf(buf
+ len
, buflen
- len
,
3922 "hostapdWPAPTKState=%d\n"
3923 "hostapdWPAPTKGroupState=%d\n",
3925 sm
->wpa_ptk_group_state
);
3926 if (os_snprintf_error(buflen
- len
, ret
))
3934 void wpa_auth_countermeasures_start(struct wpa_authenticator
*wpa_auth
)
3937 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
++;
3941 int wpa_auth_pairwise_set(struct wpa_state_machine
*sm
)
3943 return sm
&& sm
->pairwise_set
;
3947 int wpa_auth_get_pairwise(struct wpa_state_machine
*sm
)
3949 return sm
->pairwise
;
3953 int wpa_auth_sta_key_mgmt(struct wpa_state_machine
*sm
)
3957 return sm
->wpa_key_mgmt
;
3961 int wpa_auth_sta_wpa_version(struct wpa_state_machine
*sm
)
3969 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine
*sm
)
3971 if (!sm
|| !wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
3973 return sm
->tk_already_set
;
3977 int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine
*sm
)
3979 if (!sm
|| !wpa_key_mgmt_fils(sm
->wpa_key_mgmt
))
3981 return sm
->tk_already_set
;
3985 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine
*sm
,
3986 struct rsn_pmksa_cache_entry
*entry
)
3988 if (sm
== NULL
|| sm
->pmksa
!= entry
)
3995 struct rsn_pmksa_cache_entry
*
3996 wpa_auth_sta_get_pmksa(struct wpa_state_machine
*sm
)
3998 return sm
? sm
->pmksa
: NULL
;
4002 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine
*sm
)
4005 sm
->dot11RSNAStatsTKIPLocalMICFailures
++;
4009 const u8
* wpa_auth_get_wpa_ie(struct wpa_authenticator
*wpa_auth
, size_t *len
)
4011 if (wpa_auth
== NULL
)
4013 *len
= wpa_auth
->wpa_ie_len
;
4014 return wpa_auth
->wpa_ie
;
4018 int wpa_auth_pmksa_add(struct wpa_state_machine
*sm
, const u8
*pmk
,
4019 unsigned int pmk_len
,
4020 int session_timeout
, struct eapol_state_machine
*eapol
)
4022 if (sm
== NULL
|| sm
->wpa
!= WPA_VERSION_WPA2
||
4023 sm
->wpa_auth
->conf
.disable_pmksa_caching
)
4026 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
4027 if (pmk_len
> PMK_LEN_SUITE_B_192
)
4028 pmk_len
= PMK_LEN_SUITE_B_192
;
4029 } else if (pmk_len
> PMK_LEN
) {
4033 if (pmksa_cache_auth_add(sm
->wpa_auth
->pmksa
, pmk
, pmk_len
, NULL
,
4034 sm
->PTK
.kck
, sm
->PTK
.kck_len
,
4035 sm
->wpa_auth
->addr
, sm
->addr
, session_timeout
,
4036 eapol
, sm
->wpa_key_mgmt
))
4043 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator
*wpa_auth
,
4044 const u8
*pmk
, size_t len
, const u8
*sta_addr
,
4045 int session_timeout
,
4046 struct eapol_state_machine
*eapol
)
4048 if (wpa_auth
== NULL
)
4051 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, len
, NULL
,
4054 sta_addr
, session_timeout
, eapol
,
4055 WPA_KEY_MGMT_IEEE8021X
))
4062 int wpa_auth_pmksa_add_sae(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4063 const u8
*pmk
, const u8
*pmkid
)
4065 if (wpa_auth
->conf
.disable_pmksa_caching
)
4068 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, PMK_LEN
, pmkid
,
4070 wpa_auth
->addr
, addr
, 0, NULL
,
4078 int wpa_auth_pmksa_add2(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4079 const u8
*pmk
, size_t pmk_len
, const u8
*pmkid
,
4080 int session_timeout
, int akmp
)
4082 if (wpa_auth
->conf
.disable_pmksa_caching
)
4085 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, pmk_len
, pmkid
,
4086 NULL
, 0, wpa_auth
->addr
, addr
, session_timeout
,
4094 void wpa_auth_pmksa_remove(struct wpa_authenticator
*wpa_auth
,
4097 struct rsn_pmksa_cache_entry
*pmksa
;
4099 if (wpa_auth
== NULL
|| wpa_auth
->pmksa
== NULL
)
4101 pmksa
= pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, NULL
);
4103 wpa_printf(MSG_DEBUG
, "WPA: Remove PMKSA cache entry for "
4104 MACSTR
" based on request", MAC2STR(sta_addr
));
4105 pmksa_cache_free_entry(wpa_auth
->pmksa
, pmksa
);
4110 int wpa_auth_pmksa_list(struct wpa_authenticator
*wpa_auth
, char *buf
,
4113 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4115 return pmksa_cache_auth_list(wpa_auth
->pmksa
, buf
, len
);
4119 void wpa_auth_pmksa_flush(struct wpa_authenticator
*wpa_auth
)
4121 if (wpa_auth
&& wpa_auth
->pmksa
)
4122 pmksa_cache_auth_flush(wpa_auth
->pmksa
);
4126 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4129 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4130 char *buf
, size_t len
)
4132 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4135 return pmksa_cache_auth_list_mesh(wpa_auth
->pmksa
, addr
, buf
, len
);
4139 struct rsn_pmksa_cache_entry
*
4140 wpa_auth_pmksa_create_entry(const u8
*aa
, const u8
*spa
, const u8
*pmk
,
4141 const u8
*pmkid
, int expiration
)
4143 struct rsn_pmksa_cache_entry
*entry
;
4144 struct os_reltime now
;
4146 entry
= pmksa_cache_auth_create_entry(pmk
, PMK_LEN
, pmkid
, NULL
, 0, aa
,
4147 spa
, 0, NULL
, WPA_KEY_MGMT_SAE
);
4151 os_get_reltime(&now
);
4152 entry
->expiration
= now
.sec
+ expiration
;
4157 int wpa_auth_pmksa_add_entry(struct wpa_authenticator
*wpa_auth
,
4158 struct rsn_pmksa_cache_entry
*entry
)
4162 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4165 ret
= pmksa_cache_auth_add_entry(wpa_auth
->pmksa
, entry
);
4167 wpa_printf(MSG_DEBUG
,
4168 "RSN: Failed to store external PMKSA cache for "
4169 MACSTR
, MAC2STR(entry
->spa
));
4174 #endif /* CONFIG_MESH */
4175 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4178 struct rsn_pmksa_cache_entry
*
4179 wpa_auth_pmksa_get(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
,
4182 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4184 return pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, pmkid
);
4188 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry
*pmksa
,
4189 struct wpa_state_machine
*sm
,
4190 struct wpa_authenticator
*wpa_auth
,
4197 os_memcpy(pmk
, pmksa
->pmk
, PMK_LEN
);
4198 os_memcpy(pmkid
, pmksa
->pmkid
, PMKID_LEN
);
4199 os_memcpy(wpa_auth
->dot11RSNAPMKIDUsed
, pmksa
->pmkid
, PMKID_LEN
);
4204 * Remove and free the group from wpa_authenticator. This is triggered by a
4205 * callback to make sure nobody is currently iterating the group list while it
4208 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
4209 struct wpa_group
*group
)
4211 struct wpa_group
*prev
= wpa_auth
->group
;
4213 wpa_printf(MSG_DEBUG
, "WPA: Remove group state machine for VLAN-ID %d",
4217 if (prev
->next
== group
) {
4218 /* This never frees the special first group as needed */
4219 prev
->next
= group
->next
;
4229 /* Increase the reference counter for group */
4230 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
4231 struct wpa_group
*group
)
4233 /* Skip the special first group */
4234 if (wpa_auth
->group
== group
)
4237 group
->references
++;
4241 /* Decrease the reference counter and maybe free the group */
4242 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
4243 struct wpa_group
*group
)
4245 /* Skip the special first group */
4246 if (wpa_auth
->group
== group
)
4249 group
->references
--;
4250 if (group
->references
)
4252 wpa_group_free(wpa_auth
, group
);
4257 * Add a group that has its references counter set to zero. Caller needs to
4258 * call wpa_group_get() on the return value to mark the entry in use.
4260 static struct wpa_group
*
4261 wpa_auth_add_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4263 struct wpa_group
*group
;
4265 if (wpa_auth
== NULL
|| wpa_auth
->group
== NULL
)
4268 wpa_printf(MSG_DEBUG
, "WPA: Add group state machine for VLAN-ID %d",
4270 group
= wpa_group_init(wpa_auth
, vlan_id
, 0);
4274 group
->next
= wpa_auth
->group
->next
;
4275 wpa_auth
->group
->next
= group
;
4282 * Enforce that the group state machine for the VLAN is running, increase
4283 * reference counter as interface is up. References might have been increased
4284 * even if a negative value is returned.
4285 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4287 int wpa_auth_ensure_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4289 struct wpa_group
*group
;
4291 if (wpa_auth
== NULL
)
4294 group
= wpa_auth
->group
;
4296 if (group
->vlan_id
== vlan_id
)
4298 group
= group
->next
;
4301 if (group
== NULL
) {
4302 group
= wpa_auth_add_group(wpa_auth
, vlan_id
);
4307 wpa_printf(MSG_DEBUG
,
4308 "WPA: Ensure group state machine running for VLAN ID %d",
4311 wpa_group_get(wpa_auth
, group
);
4312 group
->num_setup_iface
++;
4314 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4322 * Decrease reference counter, expected to be zero afterwards.
4323 * returns: -1 on error (group not found, group in fail state)
4324 * -2 if wpa_group is still referenced
4327 int wpa_auth_release_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4329 struct wpa_group
*group
;
4332 if (wpa_auth
== NULL
)
4335 group
= wpa_auth
->group
;
4337 if (group
->vlan_id
== vlan_id
)
4339 group
= group
->next
;
4345 wpa_printf(MSG_DEBUG
,
4346 "WPA: Try stopping group state machine for VLAN ID %d",
4349 if (group
->num_setup_iface
<= 0) {
4350 wpa_printf(MSG_ERROR
,
4351 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4355 group
->num_setup_iface
--;
4357 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4360 if (group
->references
> 1) {
4361 wpa_printf(MSG_DEBUG
,
4362 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4367 wpa_group_put(wpa_auth
, group
);
4373 int wpa_auth_sta_set_vlan(struct wpa_state_machine
*sm
, int vlan_id
)
4375 struct wpa_group
*group
;
4377 if (sm
== NULL
|| sm
->wpa_auth
== NULL
)
4380 group
= sm
->wpa_auth
->group
;
4382 if (group
->vlan_id
== vlan_id
)
4384 group
= group
->next
;
4387 if (group
== NULL
) {
4388 group
= wpa_auth_add_group(sm
->wpa_auth
, vlan_id
);
4393 if (sm
->group
== group
)
4396 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4399 wpa_printf(MSG_DEBUG
, "WPA: Moving STA " MACSTR
" to use group state "
4400 "machine for VLAN ID %d", MAC2STR(sm
->addr
), vlan_id
);
4402 wpa_group_get(sm
->wpa_auth
, group
);
4403 wpa_group_put(sm
->wpa_auth
, sm
->group
);
4410 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator
*wpa_auth
,
4411 struct wpa_state_machine
*sm
, int ack
)
4413 if (wpa_auth
== NULL
|| sm
== NULL
)
4415 wpa_printf(MSG_DEBUG
, "WPA: EAPOL-Key TX status for STA " MACSTR
4416 " ack=%d", MAC2STR(sm
->addr
), ack
);
4417 if (sm
->pending_1_of_4_timeout
&& ack
) {
4419 * Some deployed supplicant implementations update their SNonce
4420 * for each EAPOL-Key 2/4 message even within the same 4-way
4421 * handshake and then fail to use the first SNonce when
4422 * deriving the PTK. This results in unsuccessful 4-way
4423 * handshake whenever the relatively short initial timeout is
4424 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4425 * around this by increasing the timeout now that we know that
4426 * the station has received the frame.
4428 int timeout_ms
= eapol_key_timeout_subseq
;
4429 wpa_printf(MSG_DEBUG
, "WPA: Increase initial EAPOL-Key 1/4 "
4430 "timeout by %u ms because of acknowledged frame",
4432 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
4433 eloop_register_timeout(timeout_ms
/ 1000,
4434 (timeout_ms
% 1000) * 1000,
4435 wpa_send_eapol_timeout
, wpa_auth
, sm
);
4438 #ifdef CONFIG_TESTING_OPTIONS
4439 if (sm
->eapol_status_cb
) {
4440 sm
->eapol_status_cb(sm
->eapol_status_cb_ctx1
,
4441 sm
->eapol_status_cb_ctx2
);
4442 sm
->eapol_status_cb
= NULL
;
4444 #endif /* CONFIG_TESTING_OPTIONS */
4448 int wpa_auth_uses_sae(struct wpa_state_machine
*sm
)
4452 return wpa_key_mgmt_sae(sm
->wpa_key_mgmt
);
4456 int wpa_auth_uses_ft_sae(struct wpa_state_machine
*sm
)
4460 return sm
->wpa_key_mgmt
== WPA_KEY_MGMT_FT_SAE
;
4465 int wpa_auth_get_ip_addr(struct wpa_state_machine
*sm
, u8
*addr
)
4467 if (sm
== NULL
|| WPA_GET_BE32(sm
->ip_addr
) == 0)
4469 os_memcpy(addr
, sm
->ip_addr
, 4);
4472 #endif /* CONFIG_P2P */
4475 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator
*wpa_auth
,
4476 struct radius_das_attrs
*attr
)
4478 return pmksa_cache_auth_radius_das_disconnect(wpa_auth
->pmksa
, attr
);
4482 void wpa_auth_reconfig_group_keys(struct wpa_authenticator
*wpa_auth
)
4484 struct wpa_group
*group
;
4488 for (group
= wpa_auth
->group
; group
; group
= group
->next
)
4489 wpa_group_config_group_keys(wpa_auth
, group
);
4495 struct wpa_auth_fils_iter_data
{
4496 struct wpa_authenticator
*auth
;
4498 struct rsn_pmksa_cache_entry
*pmksa
;
4504 static int wpa_auth_fils_iter(struct wpa_authenticator
*a
, void *ctx
)
4506 struct wpa_auth_fils_iter_data
*data
= ctx
;
4508 if (a
== data
->auth
|| !a
->conf
.fils_cache_id_set
||
4509 os_memcmp(a
->conf
.fils_cache_id
, data
->cache_id
,
4510 FILS_CACHE_ID_LEN
) != 0)
4512 data
->pmksa
= pmksa_cache_auth_get(a
->pmksa
, data
->spa
, data
->pmkid
);
4513 return data
->pmksa
!= NULL
;
4517 struct rsn_pmksa_cache_entry
*
4518 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator
*wpa_auth
,
4519 const u8
*sta_addr
, const u8
*pmkid
)
4521 struct wpa_auth_fils_iter_data idata
;
4523 if (!wpa_auth
->conf
.fils_cache_id_set
)
4525 idata
.auth
= wpa_auth
;
4526 idata
.cache_id
= wpa_auth
->conf
.fils_cache_id
;
4528 idata
.spa
= sta_addr
;
4529 idata
.pmkid
= pmkid
;
4530 wpa_auth_for_each_auth(wpa_auth
, wpa_auth_fils_iter
, &idata
);
4535 #ifdef CONFIG_IEEE80211R_AP
4536 int wpa_auth_write_fte(struct wpa_authenticator
*wpa_auth
, u8
*buf
, size_t len
)
4538 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
4540 return wpa_write_ftie(conf
, conf
->r0_key_holder
,
4541 conf
->r0_key_holder_len
,
4542 NULL
, NULL
, buf
, len
, NULL
, 0);
4544 #endif /* CONFIG_IEEE80211R_AP */
4547 void wpa_auth_get_fils_aead_params(struct wpa_state_machine
*sm
,
4548 u8
*fils_anonce
, u8
*fils_snonce
,
4549 u8
*fils_kek
, size_t *fils_kek_len
)
4551 os_memcpy(fils_anonce
, sm
->ANonce
, WPA_NONCE_LEN
);
4552 os_memcpy(fils_snonce
, sm
->SNonce
, WPA_NONCE_LEN
);
4553 os_memcpy(fils_kek
, sm
->PTK
.kek
, WPA_KEK_MAX_LEN
);
4554 *fils_kek_len
= sm
->PTK
.kek_len
;
4557 #endif /* CONFIG_FILS */
4560 #ifdef CONFIG_TESTING_OPTIONS
4562 int wpa_auth_resend_m1(struct wpa_state_machine
*sm
, int change_anonce
,
4563 void (*cb
)(void *ctx1
, void *ctx2
),
4564 void *ctx1
, void *ctx2
)
4566 const u8
*anonce
= sm
->ANonce
;
4567 u8 anonce_buf
[WPA_NONCE_LEN
];
4569 if (change_anonce
) {
4570 if (random_get_bytes(anonce_buf
, WPA_NONCE_LEN
))
4572 anonce
= anonce_buf
;
4575 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
4576 "sending 1/4 msg of 4-Way Handshake (TESTING)");
4577 wpa_send_eapol(sm
->wpa_auth
, sm
,
4578 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
4579 anonce
, NULL
, 0, 0, 0);
4584 int wpa_auth_resend_m3(struct wpa_state_machine
*sm
,
4585 void (*cb
)(void *ctx1
, void *ctx2
),
4586 void *ctx1
, void *ctx2
)
4588 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
;
4589 #ifdef CONFIG_IEEE80211W
4591 #endif /* CONFIG_IEEE80211W */
4592 size_t gtk_len
, kde_len
;
4593 struct wpa_group
*gsm
= sm
->group
;
4595 int wpa_ie_len
, secure
, keyidx
, encr
= 0;
4597 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
4598 GTK[GN], IGTK, [FTIE], [TIE * 2])
4602 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
4603 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
4604 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
4605 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
4606 if (sm
->wpa
== WPA_VERSION_WPA
&&
4607 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
4608 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
4609 /* WPA-only STA, remove RSN IE and possible MDIE */
4610 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
4611 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
4612 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
4613 wpa_ie_len
= wpa_ie
[1] + 2;
4615 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
4616 "sending 3/4 msg of 4-Way Handshake (TESTING)");
4617 if (sm
->wpa
== WPA_VERSION_WPA2
) {
4618 /* WPA2 send GTK in the 4-way handshake */
4620 gtk
= gsm
->GTK
[gsm
->GN
- 1];
4621 gtk_len
= gsm
->GTK_len
;
4626 /* WPA does not include GTK in msg 3/4 */
4632 if (sm
->rx_eapol_key_secure
) {
4634 * It looks like Windows 7 supplicant tries to use
4635 * Secure bit in msg 2/4 after having reported Michael
4636 * MIC failure and it then rejects the 4-way handshake
4637 * if msg 3/4 does not set Secure bit. Work around this
4638 * by setting the Secure bit here even in the case of
4639 * WPA if the supplicant used it first.
4641 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
4642 "STA used Secure bit in WPA msg 2/4 - "
4643 "set Secure for 3/4 as workaround");
4648 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
);
4650 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
4651 #ifdef CONFIG_IEEE80211R_AP
4652 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
4653 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
4654 kde_len
+= 300; /* FTIE + 2 * TIE */
4656 #endif /* CONFIG_IEEE80211R_AP */
4657 kde
= os_malloc(kde_len
);
4662 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
4664 #ifdef CONFIG_IEEE80211R_AP
4665 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
4670 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
4672 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
4673 "PMKR1Name into RSN IE in EAPOL-Key data");
4680 #endif /* CONFIG_IEEE80211R_AP */
4683 hdr
[0] = keyidx
& 0x03;
4685 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
4688 #ifdef CONFIG_IEEE80211W
4690 pos
= ieee80211w_kde_add(sm
, pos
);
4691 if (pos
- opos
>= WPA_IGTK_KDE_PREFIX_LEN
) {
4692 opos
+= 2; /* skip keyid */
4693 os_memset(opos
, 0, 6); /* clear PN */
4695 #endif /* CONFIG_IEEE80211W */
4697 #ifdef CONFIG_IEEE80211R_AP
4698 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
4700 struct wpa_auth_config
*conf
;
4702 conf
= &sm
->wpa_auth
->conf
;
4703 if (sm
->assoc_resp_ftie
&&
4704 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
4705 os_memcpy(pos
, sm
->assoc_resp_ftie
,
4706 2 + sm
->assoc_resp_ftie
[1]);
4707 res
= 2 + sm
->assoc_resp_ftie
[1];
4709 res
= wpa_write_ftie(conf
, conf
->r0_key_holder
,
4710 conf
->r0_key_holder_len
,
4712 kde
+ kde_len
- pos
,
4716 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
4717 "into EAPOL-Key Key Data");
4723 /* TIE[ReassociationDeadline] (TU) */
4724 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
4726 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
4727 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
4730 /* TIE[KeyLifetime] (seconds) */
4731 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
4733 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
4734 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
* 60);
4737 #endif /* CONFIG_IEEE80211R_AP */
4739 wpa_send_eapol(sm
->wpa_auth
, sm
,
4740 (secure
? WPA_KEY_INFO_SECURE
: 0) |
4741 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
4742 WPA_KEY_INFO_MIC
: 0) |
4743 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
4744 WPA_KEY_INFO_KEY_TYPE
,
4745 _rsc
, sm
->ANonce
, kde
, pos
- kde
, keyidx
, encr
);
4751 int wpa_auth_resend_group_m1(struct wpa_state_machine
*sm
,
4752 void (*cb
)(void *ctx1
, void *ctx2
),
4753 void *ctx1
, void *ctx2
)
4755 u8 rsc
[WPA_KEY_RSC_LEN
];
4756 struct wpa_group
*gsm
= sm
->group
;
4758 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
4759 #ifdef CONFIG_IEEE80211W
4761 #endif /* CONFIG_IEEE80211W */
4765 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
4766 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
4768 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
4769 "sending 1/2 msg of Group Key Handshake (TESTING)");
4771 gtk
= gsm
->GTK
[gsm
->GN
- 1];
4772 if (sm
->wpa
== WPA_VERSION_WPA2
) {
4773 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
4774 ieee80211w_kde_len(sm
);
4775 kde_buf
= os_malloc(kde_len
);
4776 if (kde_buf
== NULL
)
4779 kde
= pos
= kde_buf
;
4780 hdr
[0] = gsm
->GN
& 0x03;
4782 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
4784 #ifdef CONFIG_IEEE80211W
4786 pos
= ieee80211w_kde_add(sm
, pos
);
4787 if (pos
- opos
>= WPA_IGTK_KDE_PREFIX_LEN
) {
4788 opos
+= 2; /* skip keyid */
4789 os_memset(opos
, 0, 6); /* clear PN */
4791 #endif /* CONFIG_IEEE80211W */
4792 kde_len
= pos
- kde
;
4795 kde_len
= gsm
->GTK_len
;
4798 sm
->eapol_status_cb
= cb
;
4799 sm
->eapol_status_cb_ctx1
= ctx1
;
4800 sm
->eapol_status_cb_ctx2
= ctx2
;
4802 wpa_send_eapol(sm
->wpa_auth
, sm
,
4803 WPA_KEY_INFO_SECURE
|
4804 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
4805 WPA_KEY_INFO_MIC
: 0) |
4807 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
4808 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
4815 int wpa_auth_rekey_gtk(struct wpa_authenticator
*wpa_auth
)
4819 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
4820 return eloop_register_timeout(0, 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
4823 #endif /* CONFIG_TESTING_OPTIONS */