2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/state_machine.h"
14 #include "utils/bitfield.h"
15 #include "common/ieee802_11_defs.h"
16 #include "crypto/aes.h"
17 #include "crypto/aes_wrap.h"
18 #include "crypto/aes_siv.h"
19 #include "crypto/crypto.h"
20 #include "crypto/sha1.h"
21 #include "crypto/sha256.h"
22 #include "crypto/random.h"
23 #include "eapol_auth/eapol_auth_sm.h"
24 #include "ap_config.h"
25 #include "ieee802_11.h"
27 #include "pmksa_cache_auth.h"
28 #include "wpa_auth_i.h"
29 #include "wpa_auth_ie.h"
31 #define STATE_MACHINE_DATA struct wpa_state_machine
32 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
33 #define STATE_MACHINE_ADDR sm->addr
36 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
);
37 static int wpa_sm_step(struct wpa_state_machine
*sm
);
38 static int wpa_verify_key_mic(int akmp
, struct wpa_ptk
*PTK
, u8
*data
,
41 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
42 u8
*buf
, size_t buf_len
, u16
*_key_data_len
);
43 #endif /* CONFIG_FILS */
44 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
);
45 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
46 struct wpa_group
*group
);
47 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
);
48 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
49 struct wpa_group
*group
);
50 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
51 struct wpa_group
*group
);
52 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
53 const u8
*pmk
, unsigned int pmk_len
,
55 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
56 struct wpa_group
*group
);
57 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
58 struct wpa_group
*group
);
59 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
60 struct wpa_group
*group
);
61 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
);
63 static const u32 eapol_key_timeout_first
= 100; /* ms */
64 static const u32 eapol_key_timeout_subseq
= 1000; /* ms */
65 static const u32 eapol_key_timeout_first_group
= 500; /* ms */
67 /* TODO: make these configurable */
68 static const int dot11RSNAConfigPMKLifetime
= 43200;
69 static const int dot11RSNAConfigPMKReauthThreshold
= 70;
70 static const int dot11RSNAConfigSATimeout
= 60;
73 static inline int wpa_auth_mic_failure_report(
74 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
76 if (wpa_auth
->cb
->mic_failure_report
)
77 return wpa_auth
->cb
->mic_failure_report(wpa_auth
->cb_ctx
, addr
);
82 static inline void wpa_auth_psk_failure_report(
83 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
85 if (wpa_auth
->cb
->psk_failure_report
)
86 wpa_auth
->cb
->psk_failure_report(wpa_auth
->cb_ctx
, addr
);
90 static inline void wpa_auth_set_eapol(struct wpa_authenticator
*wpa_auth
,
91 const u8
*addr
, wpa_eapol_variable var
,
94 if (wpa_auth
->cb
->set_eapol
)
95 wpa_auth
->cb
->set_eapol(wpa_auth
->cb_ctx
, addr
, var
, value
);
99 static inline int wpa_auth_get_eapol(struct wpa_authenticator
*wpa_auth
,
100 const u8
*addr
, wpa_eapol_variable var
)
102 if (wpa_auth
->cb
->get_eapol
== NULL
)
104 return wpa_auth
->cb
->get_eapol(wpa_auth
->cb_ctx
, addr
, var
);
108 static inline const u8
* wpa_auth_get_psk(struct wpa_authenticator
*wpa_auth
,
110 const u8
*p2p_dev_addr
,
113 if (wpa_auth
->cb
->get_psk
== NULL
)
115 return wpa_auth
->cb
->get_psk(wpa_auth
->cb_ctx
, addr
, p2p_dev_addr
,
120 static inline int wpa_auth_get_msk(struct wpa_authenticator
*wpa_auth
,
121 const u8
*addr
, u8
*msk
, size_t *len
)
123 if (wpa_auth
->cb
->get_msk
== NULL
)
125 return wpa_auth
->cb
->get_msk(wpa_auth
->cb_ctx
, addr
, msk
, len
);
129 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
131 enum wpa_alg alg
, const u8
*addr
, int idx
,
132 u8
*key
, size_t key_len
)
134 if (wpa_auth
->cb
->set_key
== NULL
)
136 return wpa_auth
->cb
->set_key(wpa_auth
->cb_ctx
, vlan_id
, alg
, addr
, idx
,
141 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
142 const u8
*addr
, int idx
, u8
*seq
)
144 if (wpa_auth
->cb
->get_seqnum
== NULL
)
146 return wpa_auth
->cb
->get_seqnum(wpa_auth
->cb_ctx
, addr
, idx
, seq
);
151 wpa_auth_send_eapol(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
152 const u8
*data
, size_t data_len
, int encrypt
)
154 if (wpa_auth
->cb
->send_eapol
== NULL
)
156 return wpa_auth
->cb
->send_eapol(wpa_auth
->cb_ctx
, addr
, data
, data_len
,
162 static inline int wpa_auth_start_ampe(struct wpa_authenticator
*wpa_auth
,
165 if (wpa_auth
->cb
->start_ampe
== NULL
)
167 return wpa_auth
->cb
->start_ampe(wpa_auth
->cb_ctx
, addr
);
169 #endif /* CONFIG_MESH */
172 int wpa_auth_for_each_sta(struct wpa_authenticator
*wpa_auth
,
173 int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
176 if (wpa_auth
->cb
->for_each_sta
== NULL
)
178 return wpa_auth
->cb
->for_each_sta(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
182 int wpa_auth_for_each_auth(struct wpa_authenticator
*wpa_auth
,
183 int (*cb
)(struct wpa_authenticator
*a
, void *ctx
),
186 if (wpa_auth
->cb
->for_each_auth
== NULL
)
188 return wpa_auth
->cb
->for_each_auth(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
192 void wpa_auth_logger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
193 logger_level level
, const char *txt
)
195 if (wpa_auth
->cb
->logger
== NULL
)
197 wpa_auth
->cb
->logger(wpa_auth
->cb_ctx
, addr
, level
, txt
);
201 void wpa_auth_vlogger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
202 logger_level level
, const char *fmt
, ...)
208 if (wpa_auth
->cb
->logger
== NULL
)
211 maxlen
= os_strlen(fmt
) + 100;
212 format
= os_malloc(maxlen
);
217 vsnprintf(format
, maxlen
, fmt
, ap
);
220 wpa_auth_logger(wpa_auth
, addr
, level
, format
);
226 static void wpa_sta_disconnect(struct wpa_authenticator
*wpa_auth
,
229 if (wpa_auth
->cb
->disconnect
== NULL
)
231 wpa_printf(MSG_DEBUG
, "wpa_sta_disconnect STA " MACSTR
, MAC2STR(addr
));
232 wpa_auth
->cb
->disconnect(wpa_auth
->cb_ctx
, addr
,
233 WLAN_REASON_PREV_AUTH_NOT_VALID
);
237 static int wpa_use_aes_cmac(struct wpa_state_machine
*sm
)
240 #ifdef CONFIG_IEEE80211R_AP
241 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
243 #endif /* CONFIG_IEEE80211R_AP */
244 #ifdef CONFIG_IEEE80211W
245 if (wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
))
247 #endif /* CONFIG_IEEE80211W */
248 if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
)
254 static void wpa_rekey_gmk(void *eloop_ctx
, void *timeout_ctx
)
256 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
258 if (random_get_bytes(wpa_auth
->group
->GMK
, WPA_GMK_LEN
)) {
259 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
262 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "GMK rekeyd");
263 wpa_hexdump_key(MSG_DEBUG
, "GMK",
264 wpa_auth
->group
->GMK
, WPA_GMK_LEN
);
267 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
268 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
269 wpa_rekey_gmk
, wpa_auth
, NULL
);
274 static void wpa_rekey_gtk(void *eloop_ctx
, void *timeout_ctx
)
276 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
277 struct wpa_group
*group
, *next
;
279 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "rekeying GTK");
280 group
= wpa_auth
->group
;
282 wpa_group_get(wpa_auth
, group
);
284 group
->GTKReKey
= TRUE
;
286 group
->changed
= FALSE
;
287 wpa_group_sm_step(wpa_auth
, group
);
288 } while (group
->changed
);
291 wpa_group_put(wpa_auth
, group
);
295 if (wpa_auth
->conf
.wpa_group_rekey
) {
296 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
,
297 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
302 static void wpa_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
304 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
305 struct wpa_state_machine
*sm
= timeout_ctx
;
307 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "rekeying PTK");
308 wpa_request_new_ptk(sm
);
313 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine
*sm
, void *ctx
)
315 if (sm
->pmksa
== ctx
)
321 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
324 struct wpa_authenticator
*wpa_auth
= ctx
;
325 wpa_auth_for_each_sta(wpa_auth
, wpa_auth_pmksa_clear_cb
, entry
);
329 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator
*wpa_auth
,
330 struct wpa_group
*group
)
332 u8 buf
[ETH_ALEN
+ 8 + sizeof(unsigned long)];
336 if (random_get_bytes(group
->GMK
, WPA_GMK_LEN
) < 0)
338 wpa_hexdump_key(MSG_DEBUG
, "GMK", group
->GMK
, WPA_GMK_LEN
);
341 * Counter = PRF-256(Random number, "Init Counter",
342 * Local MAC Address || Time)
344 os_memcpy(buf
, wpa_auth
->addr
, ETH_ALEN
);
345 wpa_get_ntp_timestamp(buf
+ ETH_ALEN
);
346 ptr
= (unsigned long) group
;
347 os_memcpy(buf
+ ETH_ALEN
+ 8, &ptr
, sizeof(ptr
));
348 if (random_get_bytes(rkey
, sizeof(rkey
)) < 0)
351 if (sha1_prf(rkey
, sizeof(rkey
), "Init Counter", buf
, sizeof(buf
),
352 group
->Counter
, WPA_NONCE_LEN
) < 0)
354 wpa_hexdump_key(MSG_DEBUG
, "Key Counter",
355 group
->Counter
, WPA_NONCE_LEN
);
361 static struct wpa_group
* wpa_group_init(struct wpa_authenticator
*wpa_auth
,
362 int vlan_id
, int delay_init
)
364 struct wpa_group
*group
;
366 group
= os_zalloc(sizeof(struct wpa_group
));
370 group
->GTKAuthenticator
= TRUE
;
371 group
->vlan_id
= vlan_id
;
372 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
374 if (random_pool_ready() != 1) {
375 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
376 "for secure operations - update keys later when "
377 "the first station connects");
381 * Set initial GMK/Counter value here. The actual values that will be
382 * used in negotiations will be set once the first station tries to
383 * connect. This allows more time for collecting additional randomness
384 * on embedded devices.
386 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0) {
387 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
395 wpa_printf(MSG_DEBUG
, "WPA: Delay group state machine start "
396 "until Beacon frames have been configured");
397 /* Initialization is completed in wpa_init_keys(). */
399 wpa_group_sm_step(wpa_auth
, group
);
400 group
->GInit
= FALSE
;
401 wpa_group_sm_step(wpa_auth
, group
);
409 * wpa_init - Initialize WPA authenticator
410 * @addr: Authenticator address
411 * @conf: Configuration for WPA authenticator
412 * @cb: Callback functions for WPA authenticator
413 * Returns: Pointer to WPA authenticator data or %NULL on failure
415 struct wpa_authenticator
* wpa_init(const u8
*addr
,
416 struct wpa_auth_config
*conf
,
417 const struct wpa_auth_callbacks
*cb
,
420 struct wpa_authenticator
*wpa_auth
;
422 wpa_auth
= os_zalloc(sizeof(struct wpa_authenticator
));
423 if (wpa_auth
== NULL
)
425 os_memcpy(wpa_auth
->addr
, addr
, ETH_ALEN
);
426 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
428 wpa_auth
->cb_ctx
= cb_ctx
;
430 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
431 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
436 wpa_auth
->group
= wpa_group_init(wpa_auth
, 0, 1);
437 if (wpa_auth
->group
== NULL
) {
438 os_free(wpa_auth
->wpa_ie
);
443 wpa_auth
->pmksa
= pmksa_cache_auth_init(wpa_auth_pmksa_free_cb
,
445 if (wpa_auth
->pmksa
== NULL
) {
446 wpa_printf(MSG_ERROR
, "PMKSA cache initialization failed.");
447 os_free(wpa_auth
->group
);
448 os_free(wpa_auth
->wpa_ie
);
453 #ifdef CONFIG_IEEE80211R_AP
454 wpa_auth
->ft_pmk_cache
= wpa_ft_pmk_cache_init();
455 if (wpa_auth
->ft_pmk_cache
== NULL
) {
456 wpa_printf(MSG_ERROR
, "FT PMK cache initialization failed.");
457 os_free(wpa_auth
->group
);
458 os_free(wpa_auth
->wpa_ie
);
459 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
463 #endif /* CONFIG_IEEE80211R_AP */
465 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
466 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
467 wpa_rekey_gmk
, wpa_auth
, NULL
);
470 if (wpa_auth
->conf
.wpa_group_rekey
) {
471 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
, 0,
472 wpa_rekey_gtk
, wpa_auth
, NULL
);
476 if (WPA_GET_BE32(conf
->ip_addr_start
)) {
477 int count
= WPA_GET_BE32(conf
->ip_addr_end
) -
478 WPA_GET_BE32(conf
->ip_addr_start
) + 1;
482 wpa_auth
->ip_pool
= bitfield_alloc(count
);
484 #endif /* CONFIG_P2P */
490 int wpa_init_keys(struct wpa_authenticator
*wpa_auth
)
492 struct wpa_group
*group
= wpa_auth
->group
;
494 wpa_printf(MSG_DEBUG
, "WPA: Start group state machine to set initial "
496 wpa_group_sm_step(wpa_auth
, group
);
497 group
->GInit
= FALSE
;
498 wpa_group_sm_step(wpa_auth
, group
);
499 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
506 * wpa_deinit - Deinitialize WPA authenticator
507 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
509 void wpa_deinit(struct wpa_authenticator
*wpa_auth
)
511 struct wpa_group
*group
, *prev
;
513 eloop_cancel_timeout(wpa_rekey_gmk
, wpa_auth
, NULL
);
514 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
516 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
518 #ifdef CONFIG_IEEE80211R_AP
519 wpa_ft_pmk_cache_deinit(wpa_auth
->ft_pmk_cache
);
520 wpa_auth
->ft_pmk_cache
= NULL
;
521 #endif /* CONFIG_IEEE80211R_AP */
524 bitfield_free(wpa_auth
->ip_pool
);
525 #endif /* CONFIG_P2P */
528 os_free(wpa_auth
->wpa_ie
);
530 group
= wpa_auth
->group
;
542 * wpa_reconfig - Update WPA authenticator configuration
543 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
544 * @conf: Configuration for WPA authenticator
546 int wpa_reconfig(struct wpa_authenticator
*wpa_auth
,
547 struct wpa_auth_config
*conf
)
549 struct wpa_group
*group
;
550 if (wpa_auth
== NULL
)
553 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
554 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
555 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
560 * Reinitialize GTK to make sure it is suitable for the new
563 group
= wpa_auth
->group
;
564 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
566 wpa_group_sm_step(wpa_auth
, group
);
567 group
->GInit
= FALSE
;
568 wpa_group_sm_step(wpa_auth
, group
);
574 struct wpa_state_machine
*
575 wpa_auth_sta_init(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
576 const u8
*p2p_dev_addr
)
578 struct wpa_state_machine
*sm
;
580 if (wpa_auth
->group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
583 sm
= os_zalloc(sizeof(struct wpa_state_machine
));
586 os_memcpy(sm
->addr
, addr
, ETH_ALEN
);
588 os_memcpy(sm
->p2p_dev_addr
, p2p_dev_addr
, ETH_ALEN
);
590 sm
->wpa_auth
= wpa_auth
;
591 sm
->group
= wpa_auth
->group
;
592 wpa_group_get(sm
->wpa_auth
, sm
->group
);
598 int wpa_auth_sta_associated(struct wpa_authenticator
*wpa_auth
,
599 struct wpa_state_machine
*sm
)
601 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
604 #ifdef CONFIG_IEEE80211R_AP
605 if (sm
->ft_completed
) {
606 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
607 "FT authentication already completed - do not "
608 "start 4-way handshake");
609 /* Go to PTKINITDONE state to allow GTK rekeying */
610 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
614 #endif /* CONFIG_IEEE80211R_AP */
617 if (sm
->fils_completed
) {
618 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
619 "FILS authentication already completed - do not start 4-way handshake");
620 /* Go to PTKINITDONE state to allow GTK rekeying */
621 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
625 #endif /* CONFIG_FILS */
628 os_memset(&sm
->key_replay
, 0, sizeof(sm
->key_replay
));
629 sm
->ReAuthenticationRequest
= TRUE
;
630 return wpa_sm_step(sm
);
633 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
634 "start authentication");
638 if (wpa_sm_step(sm
) == 1)
639 return 1; /* should not really happen */
641 sm
->AuthenticationRequest
= TRUE
;
642 return wpa_sm_step(sm
);
646 void wpa_auth_sta_no_wpa(struct wpa_state_machine
*sm
)
648 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
649 * reassociates back to the same AP while the previous entry for the
650 * STA has not yet been removed. */
654 sm
->wpa_key_mgmt
= 0;
658 static void wpa_free_sta_sm(struct wpa_state_machine
*sm
)
661 if (WPA_GET_BE32(sm
->ip_addr
)) {
663 wpa_printf(MSG_DEBUG
, "P2P: Free assigned IP "
664 "address %u.%u.%u.%u from " MACSTR
,
665 sm
->ip_addr
[0], sm
->ip_addr
[1],
666 sm
->ip_addr
[2], sm
->ip_addr
[3],
668 start
= WPA_GET_BE32(sm
->wpa_auth
->conf
.ip_addr_start
);
669 bitfield_clear(sm
->wpa_auth
->ip_pool
,
670 WPA_GET_BE32(sm
->ip_addr
) - start
);
672 #endif /* CONFIG_P2P */
673 if (sm
->GUpdateStationKeys
) {
674 sm
->group
->GKeyDoneStations
--;
675 sm
->GUpdateStationKeys
= FALSE
;
677 #ifdef CONFIG_IEEE80211R_AP
678 os_free(sm
->assoc_resp_ftie
);
679 wpabuf_free(sm
->ft_pending_req_ies
);
680 #endif /* CONFIG_IEEE80211R_AP */
681 os_free(sm
->last_rx_eapol_key
);
683 wpa_group_put(sm
->wpa_auth
, sm
->group
);
688 void wpa_auth_sta_deinit(struct wpa_state_machine
*sm
)
693 if (sm
->wpa_auth
->conf
.wpa_strict_rekey
&& sm
->has_GTK
) {
694 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
695 "strict rekeying - force GTK rekey since STA "
697 eloop_cancel_timeout(wpa_rekey_gtk
, sm
->wpa_auth
, NULL
);
698 eloop_register_timeout(0, 500000, wpa_rekey_gtk
, sm
->wpa_auth
,
702 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
703 sm
->pending_1_of_4_timeout
= 0;
704 eloop_cancel_timeout(wpa_sm_call_step
, sm
, NULL
);
705 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
706 if (sm
->in_step_loop
) {
707 /* Must not free state machine while wpa_sm_step() is running.
708 * Freeing will be completed in the end of wpa_sm_step(). */
709 wpa_printf(MSG_DEBUG
, "WPA: Registering pending STA state "
710 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
711 sm
->pending_deinit
= 1;
717 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
)
722 sm
->PTKRequest
= TRUE
;
727 static int wpa_replay_counter_valid(struct wpa_key_replay_counter
*ctr
,
728 const u8
*replay_counter
)
731 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
734 if (os_memcmp(replay_counter
, ctr
[i
].counter
,
735 WPA_REPLAY_COUNTER_LEN
) == 0)
742 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter
*ctr
,
743 const u8
*replay_counter
)
746 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
748 (replay_counter
== NULL
||
749 os_memcmp(replay_counter
, ctr
[i
].counter
,
750 WPA_REPLAY_COUNTER_LEN
) == 0))
751 ctr
[i
].valid
= FALSE
;
756 #ifdef CONFIG_IEEE80211R_AP
757 static int ft_check_msg_2_of_4(struct wpa_authenticator
*wpa_auth
,
758 struct wpa_state_machine
*sm
,
759 struct wpa_eapol_ie_parse
*kde
)
761 struct wpa_ie_data ie
;
762 struct rsn_mdie
*mdie
;
764 if (wpa_parse_wpa_ie_rsn(kde
->rsn_ie
, kde
->rsn_ie_len
, &ie
) < 0 ||
765 ie
.num_pmkid
!= 1 || ie
.pmkid
== NULL
) {
766 wpa_printf(MSG_DEBUG
, "FT: No PMKR1Name in "
767 "FT 4-way handshake message 2/4");
771 os_memcpy(sm
->sup_pmk_r1_name
, ie
.pmkid
, PMKID_LEN
);
772 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Supplicant",
773 sm
->sup_pmk_r1_name
, PMKID_LEN
);
775 if (!kde
->mdie
|| !kde
->ftie
) {
776 wpa_printf(MSG_DEBUG
, "FT: No %s in FT 4-way handshake "
777 "message 2/4", kde
->mdie
? "FTIE" : "MDIE");
781 mdie
= (struct rsn_mdie
*) (kde
->mdie
+ 2);
782 if (kde
->mdie
[1] < sizeof(struct rsn_mdie
) ||
783 os_memcmp(wpa_auth
->conf
.mobility_domain
, mdie
->mobility_domain
,
784 MOBILITY_DOMAIN_ID_LEN
) != 0) {
785 wpa_printf(MSG_DEBUG
, "FT: MDIE mismatch");
789 if (sm
->assoc_resp_ftie
&&
790 (kde
->ftie
[1] != sm
->assoc_resp_ftie
[1] ||
791 os_memcmp(kde
->ftie
, sm
->assoc_resp_ftie
,
792 2 + sm
->assoc_resp_ftie
[1]) != 0)) {
793 wpa_printf(MSG_DEBUG
, "FT: FTIE mismatch");
794 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 2/4",
795 kde
->ftie
, kde
->ftie_len
);
796 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)AssocResp",
797 sm
->assoc_resp_ftie
, 2 + sm
->assoc_resp_ftie
[1]);
803 #endif /* CONFIG_IEEE80211R_AP */
806 static int wpa_receive_error_report(struct wpa_authenticator
*wpa_auth
,
807 struct wpa_state_machine
*sm
, int group
)
809 /* Supplicant reported a Michael MIC error */
810 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
811 "received EAPOL-Key Error Request "
812 "(STA detected Michael MIC failure (group=%d))",
815 if (group
&& wpa_auth
->conf
.wpa_group
!= WPA_CIPHER_TKIP
) {
816 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
817 "ignore Michael MIC failure report since "
818 "group cipher is not TKIP");
819 } else if (!group
&& sm
->pairwise
!= WPA_CIPHER_TKIP
) {
820 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
821 "ignore Michael MIC failure report since "
822 "pairwise cipher is not TKIP");
824 if (wpa_auth_mic_failure_report(wpa_auth
, sm
->addr
) > 0)
825 return 1; /* STA entry was removed */
826 sm
->dot11RSNAStatsTKIPRemoteMICFailures
++;
827 wpa_auth
->dot11RSNAStatsTKIPRemoteMICFailures
++;
831 * Error report is not a request for a new key handshake, but since
832 * Authenticator may do it, let's change the keys now anyway.
834 wpa_request_new_ptk(sm
);
839 static int wpa_try_alt_snonce(struct wpa_state_machine
*sm
, u8
*data
,
844 const u8
*pmk
= NULL
;
845 unsigned int pmk_len
;
847 os_memset(&PTK
, 0, sizeof(PTK
));
849 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
850 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
851 sm
->p2p_dev_addr
, pmk
);
857 pmk_len
= sm
->pmk_len
;
860 if (wpa_derive_ptk(sm
, sm
->alt_SNonce
, pmk
, pmk_len
, &PTK
) < 0)
863 if (wpa_verify_key_mic(sm
->wpa_key_mgmt
, &PTK
, data
, data_len
)
869 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
))
874 wpa_printf(MSG_DEBUG
,
875 "WPA: Earlier SNonce did not result in matching MIC");
879 wpa_printf(MSG_DEBUG
,
880 "WPA: Earlier SNonce resulted in matching MIC");
881 sm
->alt_snonce_valid
= 0;
882 os_memcpy(sm
->SNonce
, sm
->alt_SNonce
, WPA_NONCE_LEN
);
883 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
884 sm
->PTK_valid
= TRUE
;
890 void wpa_receive(struct wpa_authenticator
*wpa_auth
,
891 struct wpa_state_machine
*sm
,
892 u8
*data
, size_t data_len
)
894 struct ieee802_1x_hdr
*hdr
;
895 struct wpa_eapol_key
*key
;
896 u16 key_info
, key_data_length
;
897 enum { PAIRWISE_2
, PAIRWISE_4
, GROUP_2
, REQUEST
,
898 SMK_M1
, SMK_M3
, SMK_ERROR
} msg
;
900 struct wpa_eapol_ie_parse kde
;
902 size_t keyhdrlen
, mic_len
;
905 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
907 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL data", data
, data_len
);
909 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
);
910 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
912 if (data_len
< sizeof(*hdr
) + keyhdrlen
) {
913 wpa_printf(MSG_DEBUG
, "WPA: Ignore too short EAPOL-Key frame");
917 hdr
= (struct ieee802_1x_hdr
*) data
;
918 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
919 mic
= (u8
*) (key
+ 1);
920 key_info
= WPA_GET_BE16(key
->key_info
);
921 key_data
= mic
+ mic_len
+ 2;
922 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
923 wpa_printf(MSG_DEBUG
, "WPA: Received EAPOL-Key from " MACSTR
924 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
925 MAC2STR(sm
->addr
), key_info
, key
->type
,
926 (unsigned int) mic_len
, key_data_length
);
927 wpa_hexdump(MSG_MSGDUMP
,
928 "WPA: EAPOL-Key header (ending before Key MIC)",
930 wpa_hexdump(MSG_MSGDUMP
, "WPA: EAPOL-Key Key MIC",
932 if (key_data_length
> data_len
- sizeof(*hdr
) - keyhdrlen
) {
933 wpa_printf(MSG_INFO
, "WPA: Invalid EAPOL-Key frame - "
934 "key_data overflow (%d > %lu)",
936 (unsigned long) (data_len
- sizeof(*hdr
) -
941 if (sm
->wpa
== WPA_VERSION_WPA2
) {
942 if (key
->type
== EAPOL_KEY_TYPE_WPA
) {
944 * Some deployed station implementations seem to send
945 * msg 4/4 with incorrect type value in WPA2 mode.
947 wpa_printf(MSG_DEBUG
, "Workaround: Allow EAPOL-Key "
948 "with unexpected WPA type in RSN mode");
949 } else if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
950 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
951 "unexpected type %d in RSN mode",
956 if (key
->type
!= EAPOL_KEY_TYPE_WPA
) {
957 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
958 "unexpected type %d in WPA mode",
964 wpa_hexdump(MSG_DEBUG
, "WPA: Received Key Nonce", key
->key_nonce
,
966 wpa_hexdump(MSG_DEBUG
, "WPA: Received Replay Counter",
967 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
969 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
972 if ((key_info
& (WPA_KEY_INFO_SMK_MESSAGE
| WPA_KEY_INFO_REQUEST
)) ==
973 (WPA_KEY_INFO_SMK_MESSAGE
| WPA_KEY_INFO_REQUEST
)) {
974 if (key_info
& WPA_KEY_INFO_ERROR
) {
976 msgtxt
= "SMK Error";
981 } else if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
984 } else if (key_info
& WPA_KEY_INFO_REQUEST
) {
987 } else if (!(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
989 msgtxt
= "2/2 Group";
990 } else if (key_data_length
== 0 ||
991 (mic_len
== 0 && (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) &&
992 key_data_length
== AES_BLOCK_SIZE
)) {
994 msgtxt
= "4/4 Pairwise";
997 msgtxt
= "2/4 Pairwise";
1000 /* TODO: key_info type validation for PeerKey */
1001 if (msg
== REQUEST
|| msg
== PAIRWISE_2
|| msg
== PAIRWISE_4
||
1003 u16 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
1004 if (sm
->pairwise
== WPA_CIPHER_CCMP
||
1005 sm
->pairwise
== WPA_CIPHER_GCMP
) {
1006 if (wpa_use_aes_cmac(sm
) &&
1007 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
&&
1008 !wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) &&
1009 !wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1010 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1011 wpa_auth_logger(wpa_auth
, sm
->addr
,
1013 "advertised support for "
1014 "AES-128-CMAC, but did not "
1019 if (!wpa_use_aes_cmac(sm
) &&
1020 !wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1021 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1022 wpa_auth_logger(wpa_auth
, sm
->addr
,
1024 "did not use HMAC-SHA1-AES "
1030 if ((wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) ||
1031 wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) &&
1032 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
1033 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1034 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1039 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1040 if (sm
->req_replay_counter_used
&&
1041 os_memcmp(key
->replay_counter
, sm
->req_replay_counter
,
1042 WPA_REPLAY_COUNTER_LEN
) <= 0) {
1043 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1044 "received EAPOL-Key request with "
1045 "replayed counter");
1050 if (!(key_info
& WPA_KEY_INFO_REQUEST
) &&
1051 !wpa_replay_counter_valid(sm
->key_replay
, key
->replay_counter
)) {
1054 if (msg
== PAIRWISE_2
&&
1055 wpa_replay_counter_valid(sm
->prev_key_replay
,
1056 key
->replay_counter
) &&
1057 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1058 os_memcmp(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0)
1061 * Some supplicant implementations (e.g., Windows XP
1062 * WZC) update SNonce for each EAPOL-Key 2/4. This
1063 * breaks the workaround on accepting any of the
1064 * pending requests, so allow the SNonce to be updated
1065 * even if we have already sent out EAPOL-Key 3/4.
1067 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1068 "Process SNonce update from STA "
1069 "based on retransmitted EAPOL-Key "
1071 sm
->update_snonce
= 1;
1072 os_memcpy(sm
->alt_SNonce
, sm
->SNonce
, WPA_NONCE_LEN
);
1073 sm
->alt_snonce_valid
= TRUE
;
1074 os_memcpy(sm
->alt_replay_counter
,
1075 sm
->key_replay
[0].counter
,
1076 WPA_REPLAY_COUNTER_LEN
);
1077 goto continue_processing
;
1080 if (msg
== PAIRWISE_4
&& sm
->alt_snonce_valid
&&
1081 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1082 os_memcmp(key
->replay_counter
, sm
->alt_replay_counter
,
1083 WPA_REPLAY_COUNTER_LEN
) == 0) {
1085 * Supplicant may still be using the old SNonce since
1086 * there was two EAPOL-Key 2/4 messages and they had
1087 * different SNonce values.
1089 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1090 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1091 goto continue_processing
;
1094 if (msg
== PAIRWISE_2
&&
1095 wpa_replay_counter_valid(sm
->prev_key_replay
,
1096 key
->replay_counter
) &&
1097 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
) {
1098 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1099 "ignore retransmitted EAPOL-Key %s - "
1100 "SNonce did not change", msgtxt
);
1102 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1103 "received EAPOL-Key %s with "
1104 "unexpected replay counter", msgtxt
);
1106 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
1107 if (!sm
->key_replay
[i
].valid
)
1109 wpa_hexdump(MSG_DEBUG
, "pending replay counter",
1110 sm
->key_replay
[i
].counter
,
1111 WPA_REPLAY_COUNTER_LEN
);
1113 wpa_hexdump(MSG_DEBUG
, "received replay counter",
1114 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1118 continue_processing
:
1120 if (sm
->wpa
== WPA_VERSION_WPA2
&& mic_len
== 0 &&
1121 !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1122 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1123 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1126 #endif /* CONFIG_FILS */
1130 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKSTART
&&
1131 sm
->wpa_ptk_state
!= WPA_PTK_PTKCALCNEGOTIATING
&&
1132 (!sm
->update_snonce
||
1133 sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
)) {
1134 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1135 "received EAPOL-Key msg 2/4 in "
1136 "invalid state (%d) - dropped",
1140 random_add_randomness(key
->key_nonce
, WPA_NONCE_LEN
);
1141 if (sm
->group
->reject_4way_hs_for_entropy
) {
1143 * The system did not have enough entropy to generate
1144 * strong random numbers. Reject the first 4-way
1145 * handshake(s) and collect some entropy based on the
1146 * information from it. Once enough entropy is
1147 * available, the next atempt will trigger GMK/Key
1148 * Counter update and the station will be allowed to
1151 wpa_printf(MSG_DEBUG
, "WPA: Reject 4-way handshake to "
1152 "collect more entropy for random number "
1154 random_mark_pool_ready();
1155 wpa_sta_disconnect(wpa_auth
, sm
->addr
);
1160 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
||
1162 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1163 "received EAPOL-Key msg 4/4 in "
1164 "invalid state (%d) - dropped",
1170 if (sm
->wpa_ptk_group_state
!= WPA_PTK_GROUP_REKEYNEGOTIATING
1171 || !sm
->PTK_valid
) {
1172 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1173 "received EAPOL-Key msg 2/2 in "
1174 "invalid state (%d) - dropped",
1175 sm
->wpa_ptk_group_state
);
1179 #ifdef CONFIG_PEERKEY
1183 if (!wpa_auth
->conf
.peerkey
) {
1184 wpa_printf(MSG_DEBUG
, "RSN: SMK M1/M3/Error, but "
1185 "PeerKey use disabled - ignoring message");
1188 if (!sm
->PTK_valid
) {
1189 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1190 "received EAPOL-Key msg SMK in "
1191 "invalid state - dropped");
1195 #else /* CONFIG_PEERKEY */
1199 return; /* STSL disabled - ignore SMK messages */
1200 #endif /* CONFIG_PEERKEY */
1205 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1206 "received EAPOL-Key frame (%s)", msgtxt
);
1208 if (key_info
& WPA_KEY_INFO_ACK
) {
1209 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1210 "received invalid EAPOL-Key: Key Ack set");
1214 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1215 !(key_info
& WPA_KEY_INFO_MIC
)) {
1216 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1217 "received invalid EAPOL-Key: Key MIC not set");
1222 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1223 (key_info
& WPA_KEY_INFO_MIC
)) {
1224 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1225 "received invalid EAPOL-Key: Key MIC set");
1228 #endif /* CONFIG_FILS */
1230 sm
->MICVerified
= FALSE
;
1231 if (sm
->PTK_valid
&& !sm
->update_snonce
) {
1233 wpa_verify_key_mic(sm
->wpa_key_mgmt
, &sm
->PTK
, data
,
1235 (msg
!= PAIRWISE_4
|| !sm
->alt_snonce_valid
||
1236 wpa_try_alt_snonce(sm
, data
, data_len
))) {
1237 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1238 "received EAPOL-Key with invalid MIC");
1243 wpa_aead_decrypt(sm
, &sm
->PTK
, data
, data_len
,
1244 &key_data_length
) < 0) {
1245 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1246 "received EAPOL-Key with invalid MIC");
1249 #endif /* CONFIG_FILS */
1250 sm
->MICVerified
= TRUE
;
1251 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
1252 sm
->pending_1_of_4_timeout
= 0;
1255 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1256 if (sm
->MICVerified
) {
1257 sm
->req_replay_counter_used
= 1;
1258 os_memcpy(sm
->req_replay_counter
, key
->replay_counter
,
1259 WPA_REPLAY_COUNTER_LEN
);
1261 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1262 "received EAPOL-Key request with "
1268 * TODO: should decrypt key data field if encryption was used;
1269 * even though MAC address KDE is not normally encrypted,
1270 * supplicant is allowed to encrypt it.
1272 if (msg
== SMK_ERROR
) {
1273 #ifdef CONFIG_PEERKEY
1274 wpa_smk_error(wpa_auth
, sm
, key_data
, key_data_length
);
1275 #endif /* CONFIG_PEERKEY */
1277 } else if (key_info
& WPA_KEY_INFO_ERROR
) {
1278 if (wpa_receive_error_report(
1280 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) > 0)
1281 return; /* STA entry was removed */
1282 } else if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
1283 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1284 "received EAPOL-Key Request for new "
1286 wpa_request_new_ptk(sm
);
1287 #ifdef CONFIG_PEERKEY
1288 } else if (msg
== SMK_M1
) {
1289 wpa_smk_m1(wpa_auth
, sm
, key
, key_data
,
1291 #endif /* CONFIG_PEERKEY */
1292 } else if (key_data_length
> 0 &&
1293 wpa_parse_kde_ies(key_data
, key_data_length
,
1297 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1298 "received EAPOL-Key Request for GTK "
1300 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
1301 wpa_rekey_gtk(wpa_auth
, NULL
);
1304 /* Do not allow the same key replay counter to be reused. */
1305 wpa_replay_counter_mark_invalid(sm
->key_replay
,
1306 key
->replay_counter
);
1308 if (msg
== PAIRWISE_2
) {
1310 * Maintain a copy of the pending EAPOL-Key frames in
1311 * case the EAPOL-Key frame was retransmitted. This is
1312 * needed to allow EAPOL-Key msg 2/4 reply to another
1313 * pending msg 1/4 to update the SNonce to work around
1314 * unexpected supplicant behavior.
1316 os_memcpy(sm
->prev_key_replay
, sm
->key_replay
,
1317 sizeof(sm
->key_replay
));
1319 os_memset(sm
->prev_key_replay
, 0,
1320 sizeof(sm
->prev_key_replay
));
1324 * Make sure old valid counters are not accepted anymore and
1325 * do not get copied again.
1327 wpa_replay_counter_mark_invalid(sm
->key_replay
, NULL
);
1330 #ifdef CONFIG_PEERKEY
1331 if (msg
== SMK_M3
) {
1332 wpa_smk_m3(wpa_auth
, sm
, key
, key_data
, key_data_length
);
1335 #endif /* CONFIG_PEERKEY */
1337 os_free(sm
->last_rx_eapol_key
);
1338 sm
->last_rx_eapol_key
= os_memdup(data
, data_len
);
1339 if (sm
->last_rx_eapol_key
== NULL
)
1341 sm
->last_rx_eapol_key_len
= data_len
;
1343 sm
->rx_eapol_key_secure
= !!(key_info
& WPA_KEY_INFO_SECURE
);
1344 sm
->EAPOLKeyReceived
= TRUE
;
1345 sm
->EAPOLKeyPairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1346 sm
->EAPOLKeyRequest
= !!(key_info
& WPA_KEY_INFO_REQUEST
);
1347 os_memcpy(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
);
1352 static int wpa_gmk_to_gtk(const u8
*gmk
, const char *label
, const u8
*addr
,
1353 const u8
*gnonce
, u8
*gtk
, size_t gtk_len
)
1355 u8 data
[ETH_ALEN
+ WPA_NONCE_LEN
+ 8 + 16];
1359 /* GTK = PRF-X(GMK, "Group key expansion",
1360 * AA || GNonce || Time || random data)
1361 * The example described in the IEEE 802.11 standard uses only AA and
1362 * GNonce as inputs here. Add some more entropy since this derivation
1363 * is done only at the Authenticator and as such, does not need to be
1366 os_memcpy(data
, addr
, ETH_ALEN
);
1367 os_memcpy(data
+ ETH_ALEN
, gnonce
, WPA_NONCE_LEN
);
1368 pos
= data
+ ETH_ALEN
+ WPA_NONCE_LEN
;
1369 wpa_get_ntp_timestamp(pos
);
1371 if (random_get_bytes(pos
, 16) < 0)
1374 #ifdef CONFIG_IEEE80211W
1375 sha256_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
), gtk
, gtk_len
);
1376 #else /* CONFIG_IEEE80211W */
1377 if (sha1_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
), gtk
, gtk_len
)
1380 #endif /* CONFIG_IEEE80211W */
1386 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
)
1388 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1389 struct wpa_state_machine
*sm
= timeout_ctx
;
1391 sm
->pending_1_of_4_timeout
= 0;
1392 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "EAPOL-Key timeout");
1393 sm
->TimeoutEvt
= TRUE
;
1398 void __wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1399 struct wpa_state_machine
*sm
, int key_info
,
1400 const u8
*key_rsc
, const u8
*nonce
,
1401 const u8
*kde
, size_t kde_len
,
1402 int keyidx
, int encr
, int force_version
)
1404 struct ieee802_1x_hdr
*hdr
;
1405 struct wpa_eapol_key
*key
;
1406 size_t len
, mic_len
, keyhdrlen
;
1408 int key_data_len
, pad_len
= 0;
1410 int version
, pairwise
;
1412 u8
*key_mic
, *key_data
;
1414 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
);
1415 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
1417 len
= sizeof(struct ieee802_1x_hdr
) + keyhdrlen
;
1420 version
= force_version
;
1421 else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
||
1422 wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) ||
1423 wpa_key_mgmt_fils(sm
->wpa_key_mgmt
))
1424 version
= WPA_KEY_INFO_TYPE_AKM_DEFINED
;
1425 else if (wpa_use_aes_cmac(sm
))
1426 version
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
1427 else if (sm
->pairwise
!= WPA_CIPHER_TKIP
)
1428 version
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
1430 version
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
1432 pairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1434 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1435 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1438 (key_info
& WPA_KEY_INFO_SECURE
) ? 1 : 0,
1439 (key_info
& WPA_KEY_INFO_MIC
) ? 1 : 0,
1440 (key_info
& WPA_KEY_INFO_ACK
) ? 1 : 0,
1441 (key_info
& WPA_KEY_INFO_INSTALL
) ? 1 : 0,
1442 pairwise
, (unsigned long) kde_len
, keyidx
, encr
);
1444 key_data_len
= kde_len
;
1446 if ((version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1447 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
||
1448 wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) ||
1449 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) && encr
) {
1450 pad_len
= key_data_len
% 8;
1452 pad_len
= 8 - pad_len
;
1453 key_data_len
+= pad_len
+ 8;
1456 len
+= key_data_len
;
1457 if (!mic_len
&& encr
)
1458 len
+= AES_BLOCK_SIZE
;
1460 hdr
= os_zalloc(len
);
1463 hdr
->version
= wpa_auth
->conf
.eapol_version
;
1464 hdr
->type
= IEEE802_1X_TYPE_EAPOL_KEY
;
1465 hdr
->length
= host_to_be16(len
- sizeof(*hdr
));
1466 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1467 key_mic
= (u8
*) (key
+ 1);
1468 key_data
= ((u8
*) (hdr
+ 1)) + keyhdrlen
;
1470 key
->type
= sm
->wpa
== WPA_VERSION_WPA2
?
1471 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1472 key_info
|= version
;
1473 if (encr
&& sm
->wpa
== WPA_VERSION_WPA2
)
1474 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1475 if (sm
->wpa
!= WPA_VERSION_WPA2
)
1476 key_info
|= keyidx
<< WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1477 WPA_PUT_BE16(key
->key_info
, key_info
);
1479 alg
= pairwise
? sm
->pairwise
: wpa_auth
->conf
.wpa_group
;
1480 if ((key_info
& WPA_KEY_INFO_SMK_MESSAGE
) ||
1481 (sm
->wpa
== WPA_VERSION_WPA2
&& !pairwise
))
1482 WPA_PUT_BE16(key
->key_length
, 0);
1484 WPA_PUT_BE16(key
->key_length
, wpa_cipher_key_len(alg
));
1486 /* FIX: STSL: what to use as key_replay_counter? */
1487 for (i
= RSNA_MAX_EAPOL_RETRIES
- 1; i
> 0; i
--) {
1488 sm
->key_replay
[i
].valid
= sm
->key_replay
[i
- 1].valid
;
1489 os_memcpy(sm
->key_replay
[i
].counter
,
1490 sm
->key_replay
[i
- 1].counter
,
1491 WPA_REPLAY_COUNTER_LEN
);
1493 inc_byte_array(sm
->key_replay
[0].counter
, WPA_REPLAY_COUNTER_LEN
);
1494 os_memcpy(key
->replay_counter
, sm
->key_replay
[0].counter
,
1495 WPA_REPLAY_COUNTER_LEN
);
1496 wpa_hexdump(MSG_DEBUG
, "WPA: Replay Counter",
1497 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1498 sm
->key_replay
[0].valid
= TRUE
;
1501 os_memcpy(key
->key_nonce
, nonce
, WPA_NONCE_LEN
);
1504 os_memcpy(key
->key_rsc
, key_rsc
, WPA_KEY_RSC_LEN
);
1507 os_memcpy(key_data
, kde
, kde_len
);
1508 WPA_PUT_BE16(key_mic
+ mic_len
, kde_len
);
1510 } else if (!mic_len
) {
1514 WPA_PUT_BE16(key_mic
, AES_BLOCK_SIZE
+ kde_len
);
1515 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1518 wpa_hexdump_key(MSG_DEBUG
, "WPA: KEK",
1519 sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1520 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1521 * to Key Data (exclusive). */
1522 aad
[0] = (u8
*) hdr
;
1523 aad_len
[0] = key_mic
+ 2 - (u8
*) hdr
;
1524 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, kde
, kde_len
,
1525 1, aad
, aad_len
, key_mic
+ 2) < 0) {
1526 wpa_printf(MSG_DEBUG
, "WPA: AES-SIV encryption failed");
1530 wpa_hexdump(MSG_DEBUG
, "WPA: Encrypted Key Data from SIV",
1531 key_mic
+ 2, AES_BLOCK_SIZE
+ kde_len
);
1532 #endif /* CONFIG_FILS */
1533 } else if (encr
&& kde
) {
1534 buf
= os_zalloc(key_data_len
);
1540 os_memcpy(pos
, kde
, kde_len
);
1546 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1548 if (version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1549 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
||
1550 wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
) ||
1551 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1552 if (aes_wrap(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
1553 (key_data_len
- 8) / 8, buf
, key_data
)) {
1558 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1559 #ifndef CONFIG_NO_RC4
1560 } else if (sm
->PTK
.kek_len
== 16) {
1562 os_memcpy(key
->key_iv
,
1563 sm
->group
->Counter
+ WPA_NONCE_LEN
- 16, 16);
1564 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1565 os_memcpy(ek
, key
->key_iv
, 16);
1566 os_memcpy(ek
+ 16, sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1567 os_memcpy(key_data
, buf
, key_data_len
);
1568 rc4_skip(ek
, 32, 256, key_data
, key_data_len
);
1569 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1570 #endif /* CONFIG_NO_RC4 */
1579 if (key_info
& WPA_KEY_INFO_MIC
) {
1580 if (!sm
->PTK_valid
|| !mic_len
) {
1581 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1582 "PTK not valid when sending EAPOL-Key "
1588 wpa_eapol_key_mic(sm
->PTK
.kck
, sm
->PTK
.kck_len
,
1589 sm
->wpa_key_mgmt
, version
,
1590 (u8
*) hdr
, len
, key_mic
);
1591 #ifdef CONFIG_TESTING_OPTIONS
1593 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
> 0.0 &&
1595 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
) {
1596 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1597 "Corrupting group EAPOL-Key Key MIC");
1600 #endif /* CONFIG_TESTING_OPTIONS */
1603 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_inc_EapolFramesTx
,
1605 wpa_auth_send_eapol(wpa_auth
, sm
->addr
, (u8
*) hdr
, len
,
1611 static void wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1612 struct wpa_state_machine
*sm
, int key_info
,
1613 const u8
*key_rsc
, const u8
*nonce
,
1614 const u8
*kde
, size_t kde_len
,
1615 int keyidx
, int encr
)
1618 int pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
1624 __wpa_send_eapol(wpa_auth
, sm
, key_info
, key_rsc
, nonce
, kde
, kde_len
,
1627 ctr
= pairwise
? sm
->TimeoutCtr
: sm
->GTimeoutCtr
;
1628 if (ctr
== 1 && wpa_auth
->conf
.tx_status
)
1629 timeout_ms
= pairwise
? eapol_key_timeout_first
:
1630 eapol_key_timeout_first_group
;
1632 timeout_ms
= eapol_key_timeout_subseq
;
1633 if (pairwise
&& ctr
== 1 && !(key_info
& WPA_KEY_INFO_MIC
))
1634 sm
->pending_1_of_4_timeout
= 1;
1635 wpa_printf(MSG_DEBUG
, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1636 "counter %u)", timeout_ms
, ctr
);
1637 eloop_register_timeout(timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000,
1638 wpa_send_eapol_timeout
, wpa_auth
, sm
);
1642 static int wpa_verify_key_mic(int akmp
, struct wpa_ptk
*PTK
, u8
*data
,
1645 struct ieee802_1x_hdr
*hdr
;
1646 struct wpa_eapol_key
*key
;
1649 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
], *mic_pos
;
1650 size_t mic_len
= wpa_mic_len(akmp
);
1652 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
1655 hdr
= (struct ieee802_1x_hdr
*) data
;
1656 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1657 mic_pos
= (u8
*) (key
+ 1);
1658 key_info
= WPA_GET_BE16(key
->key_info
);
1659 os_memcpy(mic
, mic_pos
, mic_len
);
1660 os_memset(mic_pos
, 0, mic_len
);
1661 if (wpa_eapol_key_mic(PTK
->kck
, PTK
->kck_len
, akmp
,
1662 key_info
& WPA_KEY_INFO_TYPE_MASK
,
1663 data
, data_len
, mic_pos
) ||
1664 os_memcmp_const(mic
, mic_pos
, mic_len
) != 0)
1666 os_memcpy(mic_pos
, mic
, mic_len
);
1671 void wpa_remove_ptk(struct wpa_state_machine
*sm
)
1673 sm
->PTK_valid
= FALSE
;
1674 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1675 if (wpa_auth_set_key(sm
->wpa_auth
, 0, WPA_ALG_NONE
, sm
->addr
, 0, NULL
,
1677 wpa_printf(MSG_DEBUG
,
1678 "RSN: PTK removal from the driver failed");
1679 sm
->pairwise_set
= FALSE
;
1680 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1684 int wpa_auth_sm_event(struct wpa_state_machine
*sm
, enum wpa_event event
)
1691 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1692 "event %d notification", event
);
1697 /* PTKs are derived through AMPE */
1698 if (wpa_auth_start_ampe(sm
->wpa_auth
, sm
->addr
)) {
1703 #endif /* CONFIG_MESH */
1708 sm
->DeauthenticationRequest
= TRUE
;
1711 case WPA_REAUTH_EAPOL
:
1714 * When using WPS, we may end up here if the STA
1715 * manages to re-associate without the previous STA
1716 * entry getting removed. Consequently, we need to make
1717 * sure that the WPA state machines gets initialized
1718 * properly at this point.
1720 wpa_printf(MSG_DEBUG
, "WPA state machine had not been "
1721 "started - initialize now");
1724 if (wpa_sm_step(sm
) == 1)
1725 return 1; /* should not really happen */
1727 sm
->AuthenticationRequest
= TRUE
;
1730 if (sm
->GUpdateStationKeys
) {
1732 * Reauthentication cancels the pending group key
1733 * update for this STA.
1735 sm
->group
->GKeyDoneStations
--;
1736 sm
->GUpdateStationKeys
= FALSE
;
1737 sm
->PtkGroupInit
= TRUE
;
1739 sm
->ReAuthenticationRequest
= TRUE
;
1742 #ifdef CONFIG_IEEE80211R_AP
1743 wpa_printf(MSG_DEBUG
, "FT: Retry PTK configuration "
1744 "after association");
1745 wpa_ft_install_ptk(sm
);
1747 /* Using FT protocol, not WPA auth state machine */
1748 sm
->ft_completed
= 1;
1750 #else /* CONFIG_IEEE80211R_AP */
1752 #endif /* CONFIG_IEEE80211R_AP */
1755 #ifdef CONFIG_IEEE80211R_AP
1756 sm
->ft_completed
= 0;
1757 #endif /* CONFIG_IEEE80211R_AP */
1759 #ifdef CONFIG_IEEE80211W
1760 if (sm
->mgmt_frame_prot
&& event
== WPA_AUTH
)
1762 #endif /* CONFIG_IEEE80211W */
1764 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1765 (event
== WPA_AUTH
|| event
== WPA_ASSOC
))
1767 #endif /* CONFIG_FILS */
1770 sm
->PTK_valid
= FALSE
;
1771 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1773 if (event
!= WPA_REAUTH_EAPOL
)
1777 if (sm
->in_step_loop
) {
1779 * wpa_sm_step() is already running - avoid recursive call to
1780 * it by making the existing loop process the new update.
1785 return wpa_sm_step(sm
);
1789 SM_STATE(WPA_PTK
, INITIALIZE
)
1791 SM_ENTRY_MA(WPA_PTK
, INITIALIZE
, wpa_ptk
);
1793 /* Init flag is not cleared here, so avoid busy
1794 * loop by claiming nothing changed. */
1795 sm
->changed
= FALSE
;
1799 if (sm
->GUpdateStationKeys
)
1800 sm
->group
->GKeyDoneStations
--;
1801 sm
->GUpdateStationKeys
= FALSE
;
1802 if (sm
->wpa
== WPA_VERSION_WPA
)
1803 sm
->PInitAKeys
= FALSE
;
1804 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1805 * Local AA > Remote AA)) */) {
1808 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 0);
1810 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
, 0);
1812 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1813 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1814 WPA_EAPOL_authorized
, 0);
1819 SM_STATE(WPA_PTK
, DISCONNECT
)
1821 SM_ENTRY_MA(WPA_PTK
, DISCONNECT
, wpa_ptk
);
1822 sm
->Disconnect
= FALSE
;
1823 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
);
1827 SM_STATE(WPA_PTK
, DISCONNECTED
)
1829 SM_ENTRY_MA(WPA_PTK
, DISCONNECTED
, wpa_ptk
);
1830 sm
->DeauthenticationRequest
= FALSE
;
1834 SM_STATE(WPA_PTK
, AUTHENTICATION
)
1836 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION
, wpa_ptk
);
1837 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1838 sm
->PTK_valid
= FALSE
;
1839 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portControl_Auto
,
1841 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 1);
1842 sm
->AuthenticationRequest
= FALSE
;
1846 static void wpa_group_ensure_init(struct wpa_authenticator
*wpa_auth
,
1847 struct wpa_group
*group
)
1849 if (group
->first_sta_seen
)
1852 * System has run bit further than at the time hostapd was started
1853 * potentially very early during boot up. This provides better chances
1854 * of collecting more randomness on embedded systems. Re-initialize the
1855 * GMK and Counter here to improve their strength if there was not
1856 * enough entropy available immediately after system startup.
1858 wpa_printf(MSG_DEBUG
, "WPA: Re-initialize GMK/Counter on first "
1860 if (random_pool_ready() != 1) {
1861 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
1862 "to proceed - reject first 4-way handshake");
1863 group
->reject_4way_hs_for_entropy
= TRUE
;
1865 group
->first_sta_seen
= TRUE
;
1866 group
->reject_4way_hs_for_entropy
= FALSE
;
1869 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0 ||
1870 wpa_gtk_update(wpa_auth
, group
) < 0 ||
1871 wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
1872 wpa_printf(MSG_INFO
, "WPA: GMK/GTK setup failed");
1873 group
->first_sta_seen
= FALSE
;
1874 group
->reject_4way_hs_for_entropy
= TRUE
;
1879 SM_STATE(WPA_PTK
, AUTHENTICATION2
)
1881 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION2
, wpa_ptk
);
1883 wpa_group_ensure_init(sm
->wpa_auth
, sm
->group
);
1884 sm
->ReAuthenticationRequest
= FALSE
;
1887 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1888 * ambiguous. The Authenticator state machine uses a counter that is
1889 * incremented by one for each 4-way handshake. However, the security
1890 * analysis of 4-way handshake points out that unpredictable nonces
1891 * help in preventing precomputation attacks. Instead of the state
1892 * machine definition, use an unpredictable nonce value here to provide
1893 * stronger protection against potential precomputation attacks.
1895 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
1896 wpa_printf(MSG_ERROR
, "WPA: Failed to get random data for "
1898 sm
->Disconnect
= TRUE
;
1901 wpa_hexdump(MSG_DEBUG
, "WPA: Assign ANonce", sm
->ANonce
,
1903 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1904 * logical place than INITIALIZE since AUTHENTICATION2 can be
1905 * re-entered on ReAuthenticationRequest without going through
1911 SM_STATE(WPA_PTK
, INITPMK
)
1913 u8 msk
[2 * PMK_LEN
];
1914 size_t len
= 2 * PMK_LEN
;
1916 SM_ENTRY_MA(WPA_PTK
, INITPMK
, wpa_ptk
);
1917 #ifdef CONFIG_IEEE80211R_AP
1919 #endif /* CONFIG_IEEE80211R_AP */
1921 wpa_printf(MSG_DEBUG
, "WPA: PMK from PMKSA cache");
1922 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
1923 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
1924 } else if (wpa_auth_get_msk(sm
->wpa_auth
, sm
->addr
, msk
, &len
) == 0) {
1925 unsigned int pmk_len
;
1927 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
))
1928 pmk_len
= PMK_LEN_SUITE_B_192
;
1931 wpa_printf(MSG_DEBUG
, "WPA: PMK from EAPOL state machine "
1932 "(MSK len=%lu PMK len=%u)", (unsigned long) len
,
1934 if (len
< pmk_len
) {
1935 wpa_printf(MSG_DEBUG
,
1936 "WPA: MSK not long enough (%u) to create PMK (%u)",
1937 (unsigned int) len
, (unsigned int) pmk_len
);
1938 sm
->Disconnect
= TRUE
;
1941 os_memcpy(sm
->PMK
, msk
, pmk_len
);
1942 sm
->pmk_len
= pmk_len
;
1943 #ifdef CONFIG_IEEE80211R_AP
1944 if (len
>= 2 * PMK_LEN
) {
1945 os_memcpy(sm
->xxkey
, msk
+ PMK_LEN
, PMK_LEN
);
1946 sm
->xxkey_len
= PMK_LEN
;
1948 #endif /* CONFIG_IEEE80211R_AP */
1950 wpa_printf(MSG_DEBUG
, "WPA: Could not get PMK, get_msk: %p",
1951 sm
->wpa_auth
->cb
->get_msk
);
1952 sm
->Disconnect
= TRUE
;
1955 os_memset(msk
, 0, sizeof(msk
));
1957 sm
->req_replay_counter_used
= 0;
1958 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1959 * will break reauthentication since EAPOL state machines may not be
1960 * get into AUTHENTICATING state that clears keyRun before WPA state
1961 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1962 * state and takes PMK from the previously used AAA Key. This will
1963 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1964 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1965 * be good workaround for this issue. */
1966 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyRun
, 0);
1970 SM_STATE(WPA_PTK
, INITPSK
)
1973 SM_ENTRY_MA(WPA_PTK
, INITPSK
, wpa_ptk
);
1974 psk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
, NULL
);
1976 os_memcpy(sm
->PMK
, psk
, PMK_LEN
);
1977 sm
->pmk_len
= PMK_LEN
;
1978 #ifdef CONFIG_IEEE80211R_AP
1979 os_memcpy(sm
->xxkey
, psk
, PMK_LEN
);
1980 sm
->xxkey_len
= PMK_LEN
;
1981 #endif /* CONFIG_IEEE80211R_AP */
1983 sm
->req_replay_counter_used
= 0;
1987 SM_STATE(WPA_PTK
, PTKSTART
)
1989 u8 buf
[2 + RSN_SELECTOR_LEN
+ PMKID_LEN
], *pmkid
= NULL
;
1990 size_t pmkid_len
= 0;
1992 SM_ENTRY_MA(WPA_PTK
, PTKSTART
, wpa_ptk
);
1993 sm
->PTKRequest
= FALSE
;
1994 sm
->TimeoutEvt
= FALSE
;
1995 sm
->alt_snonce_valid
= FALSE
;
1998 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
1999 /* No point in sending the EAPOL-Key - we will disconnect
2000 * immediately following this. */
2004 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2005 "sending 1/4 msg of 4-Way Handshake");
2007 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
2008 * one possible PSK for this STA.
2010 if (sm
->wpa
== WPA_VERSION_WPA2
&&
2011 wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
2012 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
) {
2014 pmkid_len
= 2 + RSN_SELECTOR_LEN
+ PMKID_LEN
;
2015 pmkid
[0] = WLAN_EID_VENDOR_SPECIFIC
;
2016 pmkid
[1] = RSN_SELECTOR_LEN
+ PMKID_LEN
;
2017 RSN_SELECTOR_PUT(&pmkid
[2], RSN_KEY_DATA_PMKID
);
2019 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2020 sm
->pmksa
->pmkid
, PMKID_LEN
);
2021 } else if (wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
)) {
2022 /* No KCK available to derive PMKID */
2026 * Calculate PMKID since no PMKSA cache entry was
2027 * available with pre-calculated PMKID.
2029 rsn_pmkid(sm
->PMK
, sm
->pmk_len
, sm
->wpa_auth
->addr
,
2030 sm
->addr
, &pmkid
[2 + RSN_SELECTOR_LEN
],
2031 wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
));
2034 wpa_send_eapol(sm
->wpa_auth
, sm
,
2035 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
2036 sm
->ANonce
, pmkid
, pmkid_len
, 0, 0);
2040 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
2041 const u8
*pmk
, unsigned int pmk_len
,
2042 struct wpa_ptk
*ptk
)
2044 #ifdef CONFIG_IEEE80211R_AP
2045 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
2046 return wpa_auth_derive_ptk_ft(sm
, pmk
, ptk
);
2047 #endif /* CONFIG_IEEE80211R_AP */
2049 return wpa_pmk_to_ptk(pmk
, pmk_len
, "Pairwise key expansion",
2050 sm
->wpa_auth
->addr
, sm
->addr
, sm
->ANonce
, snonce
,
2051 ptk
, sm
->wpa_key_mgmt
, sm
->pairwise
);
2057 int fils_auth_pmk_to_ptk(struct wpa_state_machine
*sm
, const u8
*pmk
,
2058 size_t pmk_len
, const u8
*snonce
, const u8
*anonce
)
2060 u8 ick
[FILS_ICK_MAX_LEN
];
2064 res
= fils_pmk_to_ptk(pmk
, pmk_len
, sm
->addr
, sm
->wpa_auth
->addr
,
2065 snonce
, anonce
, &sm
->PTK
, ick
, &ick_len
,
2066 sm
->wpa_key_mgmt
, sm
->pairwise
);
2069 sm
->PTK_valid
= TRUE
;
2071 res
= fils_key_auth_sk(ick
, ick_len
, snonce
, anonce
,
2072 sm
->addr
, sm
->wpa_auth
->addr
,
2073 NULL
, 0, NULL
, 0, /* TODO: SK+PFS */
2074 sm
->wpa_key_mgmt
, sm
->fils_key_auth_sta
,
2075 sm
->fils_key_auth_ap
,
2076 &sm
->fils_key_auth_len
);
2077 os_memset(ick
, 0, sizeof(ick
));
2079 /* Store nonces for (Re)Association Request/Response frame processing */
2080 os_memcpy(sm
->SNonce
, snonce
, FILS_NONCE_LEN
);
2081 os_memcpy(sm
->ANonce
, anonce
, FILS_NONCE_LEN
);
2087 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
2088 u8
*buf
, size_t buf_len
, u16
*_key_data_len
)
2090 struct ieee802_1x_hdr
*hdr
;
2091 struct wpa_eapol_key
*key
;
2098 hdr
= (struct ieee802_1x_hdr
*) buf
;
2099 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2100 pos
= (u8
*) (key
+ 1);
2101 key_data_len
= WPA_GET_BE16(pos
);
2102 if (key_data_len
< AES_BLOCK_SIZE
||
2103 key_data_len
> buf_len
- sizeof(*hdr
) - sizeof(*key
) - 2) {
2104 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2105 "No room for AES-SIV data in the frame");
2108 pos
+= 2; /* Pointing at the Encrypted Key Data field */
2110 tmp
= os_malloc(key_data_len
);
2114 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2115 * to Key Data (exclusive). */
2117 aad_len
[0] = pos
- buf
;
2118 if (aes_siv_decrypt(ptk
->kek
, ptk
->kek_len
, pos
, key_data_len
,
2119 1, aad
, aad_len
, tmp
) < 0) {
2120 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2121 "Invalid AES-SIV data in the frame");
2122 bin_clear_free(tmp
, key_data_len
);
2126 /* AEAD decryption and validation completed successfully */
2127 key_data_len
-= AES_BLOCK_SIZE
;
2128 wpa_hexdump_key(MSG_DEBUG
, "WPA: Decrypted Key Data",
2131 /* Replace Key Data field with the decrypted version */
2132 os_memcpy(pos
, tmp
, key_data_len
);
2133 pos
-= 2; /* Key Data Length field */
2134 WPA_PUT_BE16(pos
, key_data_len
);
2135 bin_clear_free(tmp
, key_data_len
);
2137 *_key_data_len
= key_data_len
;
2142 int fils_decrypt_assoc(struct wpa_state_machine
*sm
, const u8
*fils_session
,
2143 const struct ieee80211_mgmt
*mgmt
, size_t frame_len
,
2144 u8
*pos
, size_t left
)
2147 const u8
*end
, *ie_start
, *ie
, *session
, *crypt
;
2148 struct ieee802_11_elems elems
;
2152 if (!sm
|| !sm
->PTK_valid
) {
2153 wpa_printf(MSG_DEBUG
,
2154 "FILS: No KEK to decrypt Assocication Request frame");
2158 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2159 wpa_printf(MSG_DEBUG
,
2160 "FILS: Not a FILS AKM - reject association");
2164 end
= ((const u8
*) mgmt
) + frame_len
;
2165 fc
= le_to_host16(mgmt
->frame_control
);
2166 stype
= WLAN_FC_GET_STYPE(fc
);
2167 if (stype
== WLAN_FC_STYPE_REASSOC_REQ
)
2168 ie_start
= mgmt
->u
.reassoc_req
.variable
;
2170 ie_start
= mgmt
->u
.assoc_req
.variable
;
2174 * Find FILS Session element which is the last unencrypted element in
2178 while (ie
+ 1 < end
) {
2179 if (ie
+ 2 + ie
[1] > end
)
2181 if (ie
[0] == WLAN_EID_EXTENSION
&&
2182 ie
[1] >= 1 + FILS_SESSION_LEN
&&
2183 ie
[2] == WLAN_EID_EXT_FILS_SESSION
) {
2191 wpa_printf(MSG_DEBUG
,
2192 "FILS: Could not find FILS Session element in Association Request frame - reject");
2195 if (os_memcmp(fils_session
, session
+ 3, FILS_SESSION_LEN
) != 0) {
2196 wpa_printf(MSG_DEBUG
, "FILS: Session mismatch");
2197 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
2198 fils_session
, FILS_SESSION_LEN
);
2199 wpa_hexdump(MSG_DEBUG
, "FILS: Received FILS Session",
2200 session
+ 3, FILS_SESSION_LEN
);
2203 crypt
= session
+ 2 + session
[1];
2205 if (end
- crypt
< AES_BLOCK_SIZE
) {
2206 wpa_printf(MSG_DEBUG
,
2207 "FILS: Too short frame to include AES-SIV data");
2211 /* AES-SIV AAD vectors */
2213 /* The STA's MAC address */
2215 aad_len
[0] = ETH_ALEN
;
2216 /* The AP's BSSID */
2218 aad_len
[1] = ETH_ALEN
;
2219 /* The STA's nonce */
2220 aad
[2] = sm
->SNonce
;
2221 aad_len
[2] = FILS_NONCE_LEN
;
2222 /* The AP's nonce */
2223 aad
[3] = sm
->ANonce
;
2224 aad_len
[3] = FILS_NONCE_LEN
;
2226 * The (Re)Association Request frame from the Capability Information
2227 * field to the FILS Session element (both inclusive).
2229 aad
[4] = (const u8
*) &mgmt
->u
.assoc_req
.capab_info
;
2230 aad_len
[4] = crypt
- aad
[4];
2232 if (aes_siv_decrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, crypt
, end
- crypt
,
2233 5, aad
, aad_len
, pos
+ (crypt
- ie_start
)) < 0) {
2234 wpa_printf(MSG_DEBUG
,
2235 "FILS: Invalid AES-SIV data in the frame");
2238 wpa_hexdump(MSG_DEBUG
, "FILS: Decrypted Association Request elements",
2239 pos
, left
- AES_BLOCK_SIZE
);
2241 if (ieee802_11_parse_elems(pos
, left
- AES_BLOCK_SIZE
, &elems
, 1) ==
2243 wpa_printf(MSG_DEBUG
,
2244 "FILS: Failed to parse decrypted elements");
2247 if (!elems
.fils_key_confirm
) {
2248 wpa_printf(MSG_DEBUG
, "FILS: No FILS Key Confirm element");
2251 if (elems
.fils_key_confirm_len
!= sm
->fils_key_auth_len
) {
2252 wpa_printf(MSG_DEBUG
,
2253 "FILS: Unexpected Key-Auth length %d (expected %d)",
2254 elems
.fils_key_confirm_len
,
2255 (int) sm
->fils_key_auth_len
);
2258 if (os_memcmp(elems
.fils_key_confirm
, sm
->fils_key_auth_sta
,
2259 sm
->fils_key_auth_len
) != 0) {
2260 wpa_printf(MSG_DEBUG
, "FILS: Key-Auth mismatch");
2261 wpa_hexdump(MSG_DEBUG
, "FILS: Received Key-Auth",
2262 elems
.fils_key_confirm
,
2263 elems
.fils_key_confirm_len
);
2264 wpa_hexdump(MSG_DEBUG
, "FILS: Expected Key-Auth",
2265 sm
->fils_key_auth_sta
, sm
->fils_key_auth_len
);
2269 return left
- AES_BLOCK_SIZE
;
2273 int fils_encrypt_assoc(struct wpa_state_machine
*sm
, u8
*buf
,
2274 size_t current_len
, size_t max_len
,
2275 const struct wpabuf
*hlp
)
2277 u8
*end
= buf
+ max_len
;
2278 u8
*pos
= buf
+ current_len
;
2279 struct ieee80211_mgmt
*mgmt
;
2280 struct wpabuf
*plain
;
2281 u8
*len
, *tmp
, *tmp2
;
2283 u8
*gtk
, dummy_gtk
[32];
2285 struct wpa_group
*gsm
;
2289 if (!sm
|| !sm
->PTK_valid
)
2292 wpa_hexdump(MSG_DEBUG
,
2293 "FILS: Association Response frame before FILS processing",
2296 mgmt
= (struct ieee80211_mgmt
*) buf
;
2298 /* AES-SIV AAD vectors */
2300 /* The AP's BSSID */
2302 aad_len
[0] = ETH_ALEN
;
2303 /* The STA's MAC address */
2305 aad_len
[1] = ETH_ALEN
;
2306 /* The AP's nonce */
2307 aad
[2] = sm
->ANonce
;
2308 aad_len
[2] = FILS_NONCE_LEN
;
2309 /* The STA's nonce */
2310 aad
[3] = sm
->SNonce
;
2311 aad_len
[3] = FILS_NONCE_LEN
;
2313 * The (Re)Association Response frame from the Capability Information
2314 * field (the same offset in both Association and Reassociation
2315 * Response frames) to the FILS Session element (both inclusive).
2317 aad
[4] = (const u8
*) &mgmt
->u
.assoc_resp
.capab_info
;
2318 aad_len
[4] = pos
- aad
[4];
2320 /* The following elements will be encrypted with AES-SIV */
2322 plain
= wpabuf_alloc(1000);
2326 /* TODO: FILS Public Key */
2328 /* FILS Key Confirmation */
2329 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2330 wpabuf_put_u8(plain
, 1 + sm
->fils_key_auth_len
); /* Length */
2331 /* Element ID Extension */
2332 wpabuf_put_u8(plain
, WLAN_EID_EXT_FILS_KEY_CONFIRM
);
2333 wpabuf_put_data(plain
, sm
->fils_key_auth_ap
, sm
->fils_key_auth_len
);
2335 /* FILS HLP Container */
2337 wpabuf_put_buf(plain
, hlp
);
2339 /* TODO: FILS IP Address Assignment */
2343 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2344 len
= wpabuf_put(plain
, 1);
2345 wpabuf_put_u8(plain
, WLAN_EID_EXT_KEY_DELIVERY
);
2346 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
,
2347 wpabuf_put(plain
, WPA_KEY_RSC_LEN
));
2349 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2350 gtk_len
= gsm
->GTK_len
;
2351 if (sm
->wpa_auth
->conf
.disable_gtk
) {
2353 * Provide unique random GTK to each STA to prevent use
2354 * of GTK in the BSS.
2356 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0) {
2362 hdr
[0] = gsm
->GN
& 0x03;
2364 tmp
= wpabuf_put(plain
, 0);
2365 tmp2
= wpa_add_kde(tmp
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
2367 wpabuf_put(plain
, tmp2
- tmp
);
2370 tmp
= wpabuf_put(plain
, 0);
2371 tmp2
= ieee80211w_kde_add(sm
, tmp
);
2372 wpabuf_put(plain
, tmp2
- tmp
);
2374 *len
= (u8
*) wpabuf_put(plain
, 0) - len
- 1;
2376 if (pos
+ wpabuf_len(plain
) + AES_BLOCK_SIZE
> end
) {
2377 wpa_printf(MSG_DEBUG
,
2378 "FILS: Not enough room for FILS elements");
2383 wpa_hexdump_buf_key(MSG_DEBUG
, "FILS: Association Response plaintext",
2386 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
2387 wpabuf_head(plain
), wpabuf_len(plain
),
2388 5, aad
, aad_len
, pos
) < 0) {
2393 wpa_hexdump(MSG_DEBUG
,
2394 "FILS: Encrypted Association Response elements",
2395 pos
, AES_BLOCK_SIZE
+ wpabuf_len(plain
));
2396 current_len
+= wpabuf_len(plain
) + AES_BLOCK_SIZE
;
2399 sm
->fils_completed
= 1;
2405 int fils_set_tk(struct wpa_state_machine
*sm
)
2410 if (!sm
|| !sm
->PTK_valid
)
2413 alg
= wpa_cipher_to_alg(sm
->pairwise
);
2414 klen
= wpa_cipher_key_len(sm
->pairwise
);
2416 wpa_printf(MSG_DEBUG
, "FILS: Configure TK to the driver");
2417 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
2418 sm
->PTK
.tk
, klen
)) {
2419 wpa_printf(MSG_DEBUG
, "FILS: Failed to set TK to the driver");
2426 #endif /* CONFIG_FILS */
2429 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING
)
2431 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2433 int ok
= 0, psk_found
= 0;
2434 const u8
*pmk
= NULL
;
2435 unsigned int pmk_len
;
2437 const u8
*eapol_key_ie
, *key_data
, *mic
;
2438 u16 key_data_length
;
2439 size_t mic_len
, eapol_key_ie_len
;
2440 struct ieee802_1x_hdr
*hdr
;
2441 struct wpa_eapol_key
*key
;
2442 struct wpa_eapol_ie_parse kde
;
2444 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING
, wpa_ptk
);
2445 sm
->EAPOLKeyReceived
= FALSE
;
2446 sm
->update_snonce
= FALSE
;
2447 os_memset(&PTK
, 0, sizeof(PTK
));
2449 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
);
2451 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2452 * WPA-PSK: iterate through possible PSKs and select the one matching
2455 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
2456 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
2457 sm
->p2p_dev_addr
, pmk
);
2464 pmk_len
= sm
->pmk_len
;
2467 if (wpa_derive_ptk(sm
, sm
->SNonce
, pmk
, pmk_len
, &PTK
) < 0)
2471 wpa_verify_key_mic(sm
->wpa_key_mgmt
, &PTK
,
2472 sm
->last_rx_eapol_key
,
2473 sm
->last_rx_eapol_key_len
) == 0) {
2480 wpa_aead_decrypt(sm
, &PTK
, sm
->last_rx_eapol_key
,
2481 sm
->last_rx_eapol_key_len
, NULL
) == 0) {
2485 #endif /* CONFIG_FILS */
2487 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
))
2492 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2493 "invalid MIC in msg 2/4 of 4-Way Handshake");
2495 wpa_auth_psk_failure_report(sm
->wpa_auth
, sm
->addr
);
2500 * Note: last_rx_eapol_key length fields have already been validated in
2503 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
2504 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2505 mic
= (u8
*) (key
+ 1);
2506 key_data
= mic
+ mic_len
+ 2;
2507 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
2508 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
2509 sizeof(*key
) - mic_len
- 2)
2512 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
2513 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2514 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2518 eapol_key_ie
= kde
.rsn_ie
;
2519 eapol_key_ie_len
= kde
.rsn_ie_len
;
2520 } else if (kde
.osen
) {
2521 eapol_key_ie
= kde
.osen
;
2522 eapol_key_ie_len
= kde
.osen_len
;
2524 eapol_key_ie
= kde
.wpa_ie
;
2525 eapol_key_ie_len
= kde
.wpa_ie_len
;
2527 ft
= sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
);
2528 if (sm
->wpa_ie
== NULL
||
2529 wpa_compare_rsn_ie(ft
, sm
->wpa_ie
, sm
->wpa_ie_len
,
2530 eapol_key_ie
, eapol_key_ie_len
)) {
2531 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2532 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2534 wpa_hexdump(MSG_DEBUG
, "WPA IE in AssocReq",
2535 sm
->wpa_ie
, sm
->wpa_ie_len
);
2537 wpa_hexdump(MSG_DEBUG
, "WPA IE in msg 2/4",
2538 eapol_key_ie
, eapol_key_ie_len
);
2539 /* MLME-DEAUTHENTICATE.request */
2540 wpa_sta_disconnect(wpa_auth
, sm
->addr
);
2543 #ifdef CONFIG_IEEE80211R_AP
2544 if (ft
&& ft_check_msg_2_of_4(wpa_auth
, sm
, &kde
) < 0) {
2545 wpa_sta_disconnect(wpa_auth
, sm
->addr
);
2548 #endif /* CONFIG_IEEE80211R_AP */
2550 if (kde
.ip_addr_req
&& kde
.ip_addr_req
[0] &&
2551 wpa_auth
->ip_pool
&& WPA_GET_BE32(sm
->ip_addr
) == 0) {
2553 wpa_printf(MSG_DEBUG
,
2554 "P2P: IP address requested in EAPOL-Key exchange");
2555 idx
= bitfield_get_first_zero(wpa_auth
->ip_pool
);
2557 u32 start
= WPA_GET_BE32(wpa_auth
->conf
.ip_addr_start
);
2558 bitfield_set(wpa_auth
->ip_pool
, idx
);
2559 WPA_PUT_BE32(sm
->ip_addr
, start
+ idx
);
2560 wpa_printf(MSG_DEBUG
,
2561 "P2P: Assigned IP address %u.%u.%u.%u to "
2562 MACSTR
, sm
->ip_addr
[0], sm
->ip_addr
[1],
2563 sm
->ip_addr
[2], sm
->ip_addr
[3],
2567 #endif /* CONFIG_P2P */
2569 #ifdef CONFIG_IEEE80211R_AP
2570 if (sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2572 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2573 * with the value we derived.
2575 if (os_memcmp_const(sm
->sup_pmk_r1_name
, sm
->pmk_r1_name
,
2576 WPA_PMK_NAME_LEN
) != 0) {
2577 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2578 "PMKR1Name mismatch in FT 4-way "
2580 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from "
2582 sm
->sup_pmk_r1_name
, WPA_PMK_NAME_LEN
);
2583 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
2584 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
2588 #endif /* CONFIG_IEEE80211R_AP */
2590 sm
->pending_1_of_4_timeout
= 0;
2591 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
2593 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
2594 /* PSK may have changed from the previous choice, so update
2595 * state machine data based on whatever PSK was selected here.
2597 os_memcpy(sm
->PMK
, pmk
, PMK_LEN
);
2598 sm
->pmk_len
= PMK_LEN
;
2601 sm
->MICVerified
= TRUE
;
2603 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
2604 sm
->PTK_valid
= TRUE
;
2608 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING2
)
2610 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING2
, wpa_ptk
);
2615 #ifdef CONFIG_IEEE80211W
2617 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
2619 if (sm
->mgmt_frame_prot
) {
2621 len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
2622 return 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
+ len
;
2629 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
2631 struct wpa_igtk_kde igtk
;
2632 struct wpa_group
*gsm
= sm
->group
;
2633 u8 rsc
[WPA_KEY_RSC_LEN
];
2634 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
2636 if (!sm
->mgmt_frame_prot
)
2639 igtk
.keyid
[0] = gsm
->GN_igtk
;
2641 if (gsm
->wpa_group_state
!= WPA_GROUP_SETKEYSDONE
||
2642 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, rsc
) < 0)
2643 os_memset(igtk
.pn
, 0, sizeof(igtk
.pn
));
2645 os_memcpy(igtk
.pn
, rsc
, sizeof(igtk
.pn
));
2646 os_memcpy(igtk
.igtk
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
2647 if (sm
->wpa_auth
->conf
.disable_gtk
) {
2649 * Provide unique random IGTK to each STA to prevent use of
2652 if (random_get_bytes(igtk
.igtk
, len
) < 0)
2655 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_IGTK
,
2656 (const u8
*) &igtk
, WPA_IGTK_KDE_PREFIX_LEN
+ len
,
2662 #else /* CONFIG_IEEE80211W */
2664 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
2670 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
2675 #endif /* CONFIG_IEEE80211W */
2678 SM_STATE(WPA_PTK
, PTKINITNEGOTIATING
)
2680 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
, dummy_gtk
[32];
2681 size_t gtk_len
, kde_len
;
2682 struct wpa_group
*gsm
= sm
->group
;
2684 int wpa_ie_len
, secure
, keyidx
, encr
= 0;
2686 SM_ENTRY_MA(WPA_PTK
, PTKINITNEGOTIATING
, wpa_ptk
);
2687 sm
->TimeoutEvt
= FALSE
;
2690 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
2691 /* No point in sending the EAPOL-Key - we will disconnect
2692 * immediately following this. */
2696 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2697 GTK[GN], IGTK, [FTIE], [TIE * 2])
2699 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
2700 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
2701 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2702 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
2703 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
2704 if (sm
->wpa
== WPA_VERSION_WPA
&&
2705 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
2706 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
2707 /* WPA-only STA, remove RSN IE and possible MDIE */
2708 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
2709 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
2710 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
2711 wpa_ie_len
= wpa_ie
[1] + 2;
2713 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2714 "sending 3/4 msg of 4-Way Handshake");
2715 if (sm
->wpa
== WPA_VERSION_WPA2
) {
2716 /* WPA2 send GTK in the 4-way handshake */
2718 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2719 gtk_len
= gsm
->GTK_len
;
2720 if (sm
->wpa_auth
->conf
.disable_gtk
) {
2722 * Provide unique random GTK to each STA to prevent use
2723 * of GTK in the BSS.
2725 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0)
2733 /* WPA does not include GTK in msg 3/4 */
2739 if (sm
->rx_eapol_key_secure
) {
2741 * It looks like Windows 7 supplicant tries to use
2742 * Secure bit in msg 2/4 after having reported Michael
2743 * MIC failure and it then rejects the 4-way handshake
2744 * if msg 3/4 does not set Secure bit. Work around this
2745 * by setting the Secure bit here even in the case of
2746 * WPA if the supplicant used it first.
2748 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2749 "STA used Secure bit in WPA msg 2/4 - "
2750 "set Secure for 3/4 as workaround");
2755 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
);
2757 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
2758 #ifdef CONFIG_IEEE80211R_AP
2759 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2760 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
2761 kde_len
+= 300; /* FTIE + 2 * TIE */
2763 #endif /* CONFIG_IEEE80211R_AP */
2765 if (WPA_GET_BE32(sm
->ip_addr
) > 0)
2766 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 3 * 4;
2767 #endif /* CONFIG_P2P */
2768 kde
= os_malloc(kde_len
);
2773 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
2775 #ifdef CONFIG_IEEE80211R_AP
2776 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2781 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
2783 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
2784 "PMKR1Name into RSN IE in EAPOL-Key data");
2791 #endif /* CONFIG_IEEE80211R_AP */
2794 hdr
[0] = keyidx
& 0x03;
2796 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
2799 pos
= ieee80211w_kde_add(sm
, pos
);
2801 #ifdef CONFIG_IEEE80211R_AP
2802 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2804 struct wpa_auth_config
*conf
;
2806 conf
= &sm
->wpa_auth
->conf
;
2807 if (sm
->assoc_resp_ftie
&&
2808 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
2809 os_memcpy(pos
, sm
->assoc_resp_ftie
,
2810 2 + sm
->assoc_resp_ftie
[1]);
2811 res
= 2 + sm
->assoc_resp_ftie
[1];
2813 res
= wpa_write_ftie(conf
, conf
->r0_key_holder
,
2814 conf
->r0_key_holder_len
,
2816 kde
+ kde_len
- pos
,
2820 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
2821 "into EAPOL-Key Key Data");
2827 /* TIE[ReassociationDeadline] (TU) */
2828 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
2830 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
2831 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
2834 /* TIE[KeyLifetime] (seconds) */
2835 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
2837 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
2838 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
* 60);
2841 #endif /* CONFIG_IEEE80211R_AP */
2843 if (WPA_GET_BE32(sm
->ip_addr
) > 0) {
2845 os_memcpy(addr
, sm
->ip_addr
, 4);
2846 os_memcpy(addr
+ 4, sm
->wpa_auth
->conf
.ip_addr_mask
, 4);
2847 os_memcpy(addr
+ 8, sm
->wpa_auth
->conf
.ip_addr_go
, 4);
2848 pos
= wpa_add_kde(pos
, WFA_KEY_DATA_IP_ADDR_ALLOC
,
2849 addr
, sizeof(addr
), NULL
, 0);
2851 #endif /* CONFIG_P2P */
2853 wpa_send_eapol(sm
->wpa_auth
, sm
,
2854 (secure
? WPA_KEY_INFO_SECURE
: 0) |
2855 (wpa_mic_len(sm
->wpa_key_mgmt
) ? WPA_KEY_INFO_MIC
: 0) |
2856 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
2857 WPA_KEY_INFO_KEY_TYPE
,
2858 _rsc
, sm
->ANonce
, kde
, pos
- kde
, keyidx
, encr
);
2863 SM_STATE(WPA_PTK
, PTKINITDONE
)
2865 SM_ENTRY_MA(WPA_PTK
, PTKINITDONE
, wpa_ptk
);
2866 sm
->EAPOLKeyReceived
= FALSE
;
2868 enum wpa_alg alg
= wpa_cipher_to_alg(sm
->pairwise
);
2869 int klen
= wpa_cipher_key_len(sm
->pairwise
);
2870 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
2871 sm
->PTK
.tk
, klen
)) {
2872 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
);
2875 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2876 sm
->pairwise_set
= TRUE
;
2878 if (sm
->wpa_auth
->conf
.wpa_ptk_rekey
) {
2879 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
2880 eloop_register_timeout(sm
->wpa_auth
->conf
.
2881 wpa_ptk_rekey
, 0, wpa_rekey_ptk
,
2885 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
2886 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
2887 WPA_EAPOL_authorized
, 1);
2891 if (0 /* IBSS == TRUE */) {
2893 if (sm
->keycount
== 2) {
2894 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
2895 WPA_EAPOL_portValid
, 1);
2898 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
,
2901 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyAvailable
, 0);
2902 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyDone
, 1);
2903 if (sm
->wpa
== WPA_VERSION_WPA
)
2904 sm
->PInitAKeys
= TRUE
;
2907 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2908 "pairwise key handshake completed (%s)",
2909 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
2911 #ifdef CONFIG_IEEE80211R_AP
2912 wpa_ft_push_pmk_r1(sm
->wpa_auth
, sm
->addr
);
2913 #endif /* CONFIG_IEEE80211R_AP */
2919 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2922 SM_ENTER(WPA_PTK
, INITIALIZE
);
2923 else if (sm
->Disconnect
2924 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2925 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2926 "WPA_PTK: sm->Disconnect");
2927 SM_ENTER(WPA_PTK
, DISCONNECT
);
2929 else if (sm
->DeauthenticationRequest
)
2930 SM_ENTER(WPA_PTK
, DISCONNECTED
);
2931 else if (sm
->AuthenticationRequest
)
2932 SM_ENTER(WPA_PTK
, AUTHENTICATION
);
2933 else if (sm
->ReAuthenticationRequest
)
2934 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
2935 else if (sm
->PTKRequest
)
2936 SM_ENTER(WPA_PTK
, PTKSTART
);
2937 else switch (sm
->wpa_ptk_state
) {
2938 case WPA_PTK_INITIALIZE
:
2940 case WPA_PTK_DISCONNECT
:
2941 SM_ENTER(WPA_PTK
, DISCONNECTED
);
2943 case WPA_PTK_DISCONNECTED
:
2944 SM_ENTER(WPA_PTK
, INITIALIZE
);
2946 case WPA_PTK_AUTHENTICATION
:
2947 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
2949 case WPA_PTK_AUTHENTICATION2
:
2950 if (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
2951 wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
2952 WPA_EAPOL_keyRun
) > 0)
2953 SM_ENTER(WPA_PTK
, INITPMK
);
2954 else if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)
2955 /* FIX: && 802.1X::keyRun */)
2956 SM_ENTER(WPA_PTK
, INITPSK
);
2958 case WPA_PTK_INITPMK
:
2959 if (wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
2960 WPA_EAPOL_keyAvailable
) > 0)
2961 SM_ENTER(WPA_PTK
, PTKSTART
);
2963 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
2964 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2965 "INITPMK - keyAvailable = false");
2966 SM_ENTER(WPA_PTK
, DISCONNECT
);
2969 case WPA_PTK_INITPSK
:
2970 if (wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
,
2972 SM_ENTER(WPA_PTK
, PTKSTART
);
2974 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2975 "no PSK configured for the STA");
2976 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
2977 SM_ENTER(WPA_PTK
, DISCONNECT
);
2980 case WPA_PTK_PTKSTART
:
2981 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
2982 sm
->EAPOLKeyPairwise
)
2983 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
2984 else if (sm
->TimeoutCtr
>
2985 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
2986 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
2988 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2989 "PTKSTART: Retry limit %u reached",
2990 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
2991 SM_ENTER(WPA_PTK
, DISCONNECT
);
2992 } else if (sm
->TimeoutEvt
)
2993 SM_ENTER(WPA_PTK
, PTKSTART
);
2995 case WPA_PTK_PTKCALCNEGOTIATING
:
2996 if (sm
->MICVerified
)
2997 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING2
);
2998 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
2999 sm
->EAPOLKeyPairwise
)
3000 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3001 else if (sm
->TimeoutEvt
)
3002 SM_ENTER(WPA_PTK
, PTKSTART
);
3004 case WPA_PTK_PTKCALCNEGOTIATING2
:
3005 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3007 case WPA_PTK_PTKINITNEGOTIATING
:
3008 if (sm
->update_snonce
)
3009 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3010 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3011 sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3012 SM_ENTER(WPA_PTK
, PTKINITDONE
);
3013 else if (sm
->TimeoutCtr
>
3014 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
3015 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3017 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3018 "PTKINITNEGOTIATING: Retry limit %u reached",
3019 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3020 SM_ENTER(WPA_PTK
, DISCONNECT
);
3021 } else if (sm
->TimeoutEvt
)
3022 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3024 case WPA_PTK_PTKINITDONE
:
3030 SM_STATE(WPA_PTK_GROUP
, IDLE
)
3032 SM_ENTRY_MA(WPA_PTK_GROUP
, IDLE
, wpa_ptk_group
);
3034 /* Init flag is not cleared here, so avoid busy
3035 * loop by claiming nothing changed. */
3036 sm
->changed
= FALSE
;
3038 sm
->GTimeoutCtr
= 0;
3042 SM_STATE(WPA_PTK_GROUP
, REKEYNEGOTIATING
)
3044 u8 rsc
[WPA_KEY_RSC_LEN
];
3045 struct wpa_group
*gsm
= sm
->group
;
3047 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
3049 u8
*gtk
, dummy_gtk
[32];
3051 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYNEGOTIATING
, wpa_ptk_group
);
3054 if (sm
->GTimeoutCtr
> sm
->wpa_auth
->conf
.wpa_group_update_count
) {
3055 /* No point in sending the EAPOL-Key - we will disconnect
3056 * immediately following this. */
3060 if (sm
->wpa
== WPA_VERSION_WPA
)
3061 sm
->PInitAKeys
= FALSE
;
3062 sm
->TimeoutEvt
= FALSE
;
3063 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3064 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3065 if (gsm
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
)
3066 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3067 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3068 "sending 1/2 msg of Group Key Handshake");
3070 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3071 if (sm
->wpa_auth
->conf
.disable_gtk
) {
3073 * Provide unique random GTK to each STA to prevent use
3074 * of GTK in the BSS.
3076 if (random_get_bytes(dummy_gtk
, gsm
->GTK_len
) < 0)
3080 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3081 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
3082 ieee80211w_kde_len(sm
);
3083 kde_buf
= os_malloc(kde_len
);
3084 if (kde_buf
== NULL
)
3087 kde
= pos
= kde_buf
;
3088 hdr
[0] = gsm
->GN
& 0x03;
3090 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3092 pos
= ieee80211w_kde_add(sm
, pos
);
3093 kde_len
= pos
- kde
;
3096 kde_len
= gsm
->GTK_len
;
3099 wpa_send_eapol(sm
->wpa_auth
, sm
,
3100 WPA_KEY_INFO_SECURE
|
3101 (wpa_mic_len(sm
->wpa_key_mgmt
) ? WPA_KEY_INFO_MIC
: 0) |
3103 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
3104 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
3110 SM_STATE(WPA_PTK_GROUP
, REKEYESTABLISHED
)
3112 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYESTABLISHED
, wpa_ptk_group
);
3113 sm
->EAPOLKeyReceived
= FALSE
;
3114 if (sm
->GUpdateStationKeys
)
3115 sm
->group
->GKeyDoneStations
--;
3116 sm
->GUpdateStationKeys
= FALSE
;
3117 sm
->GTimeoutCtr
= 0;
3118 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3119 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3120 "group key handshake completed (%s)",
3121 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3126 SM_STATE(WPA_PTK_GROUP
, KEYERROR
)
3128 SM_ENTRY_MA(WPA_PTK_GROUP
, KEYERROR
, wpa_ptk_group
);
3129 if (sm
->GUpdateStationKeys
)
3130 sm
->group
->GKeyDoneStations
--;
3131 sm
->GUpdateStationKeys
= FALSE
;
3132 sm
->Disconnect
= TRUE
;
3133 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3134 "group key handshake failed (%s) after %u tries",
3135 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN",
3136 sm
->wpa_auth
->conf
.wpa_group_update_count
);
3140 SM_STEP(WPA_PTK_GROUP
)
3142 if (sm
->Init
|| sm
->PtkGroupInit
) {
3143 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3144 sm
->PtkGroupInit
= FALSE
;
3145 } else switch (sm
->wpa_ptk_group_state
) {
3146 case WPA_PTK_GROUP_IDLE
:
3147 if (sm
->GUpdateStationKeys
||
3148 (sm
->wpa
== WPA_VERSION_WPA
&& sm
->PInitAKeys
))
3149 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3151 case WPA_PTK_GROUP_REKEYNEGOTIATING
:
3152 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3153 !sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3154 SM_ENTER(WPA_PTK_GROUP
, REKEYESTABLISHED
);
3155 else if (sm
->GTimeoutCtr
>
3156 sm
->wpa_auth
->conf
.wpa_group_update_count
)
3157 SM_ENTER(WPA_PTK_GROUP
, KEYERROR
);
3158 else if (sm
->TimeoutEvt
)
3159 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3161 case WPA_PTK_GROUP_KEYERROR
:
3162 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3164 case WPA_PTK_GROUP_REKEYESTABLISHED
:
3165 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3171 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
3172 struct wpa_group
*group
)
3176 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3177 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3178 if (wpa_gmk_to_gtk(group
->GMK
, "Group key expansion",
3179 wpa_auth
->addr
, group
->GNonce
,
3180 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3182 wpa_hexdump_key(MSG_DEBUG
, "GTK",
3183 group
->GTK
[group
->GN
- 1], group
->GTK_len
);
3185 #ifdef CONFIG_IEEE80211W
3186 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3188 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3189 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3190 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3191 if (wpa_gmk_to_gtk(group
->GMK
, "IGTK key expansion",
3192 wpa_auth
->addr
, group
->GNonce
,
3193 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3195 wpa_hexdump_key(MSG_DEBUG
, "IGTK",
3196 group
->IGTK
[group
->GN_igtk
- 4], len
);
3198 #endif /* CONFIG_IEEE80211W */
3204 static void wpa_group_gtk_init(struct wpa_authenticator
*wpa_auth
,
3205 struct wpa_group
*group
)
3207 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3208 "GTK_INIT (VLAN-ID %d)", group
->vlan_id
);
3209 group
->changed
= FALSE
; /* GInit is not cleared here; avoid loop */
3210 group
->wpa_group_state
= WPA_GROUP_GTK_INIT
;
3213 os_memset(group
->GTK
, 0, sizeof(group
->GTK
));
3216 #ifdef CONFIG_IEEE80211W
3219 #endif /* CONFIG_IEEE80211W */
3220 /* GTK[GN] = CalcGTK() */
3221 wpa_gtk_update(wpa_auth
, group
);
3225 static int wpa_group_update_sta(struct wpa_state_machine
*sm
, void *ctx
)
3227 if (ctx
!= NULL
&& ctx
!= sm
->group
)
3230 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITDONE
) {
3231 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3232 "Not in PTKINITDONE; skip Group Key update");
3233 sm
->GUpdateStationKeys
= FALSE
;
3236 if (sm
->GUpdateStationKeys
) {
3238 * This should not really happen, so add a debug log entry.
3239 * Since we clear the GKeyDoneStations before the loop, the
3240 * station needs to be counted here anyway.
3242 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3243 "GUpdateStationKeys was already set when "
3244 "marking station for GTK rekeying");
3247 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
3248 if (sm
->is_wnmsleep
)
3251 sm
->group
->GKeyDoneStations
++;
3252 sm
->GUpdateStationKeys
= TRUE
;
3260 /* update GTK when exiting WNM-Sleep Mode */
3261 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine
*sm
)
3263 if (sm
== NULL
|| sm
->is_wnmsleep
)
3266 wpa_group_update_sta(sm
, NULL
);
3270 void wpa_set_wnmsleep(struct wpa_state_machine
*sm
, int flag
)
3273 sm
->is_wnmsleep
= !!flag
;
3277 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3279 struct wpa_group
*gsm
= sm
->group
;
3284 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
3287 *pos
++ = WNM_SLEEP_SUBELEM_GTK
;
3288 *pos
++ = 11 + gsm
->GTK_len
;
3289 /* Key ID in B0-B1 of Key Info */
3290 WPA_PUT_LE16(pos
, gsm
->GN
& 0x03);
3292 *pos
++ = gsm
->GTK_len
;
3293 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, pos
) != 0)
3296 os_memcpy(pos
, gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
3297 pos
+= gsm
->GTK_len
;
3299 wpa_printf(MSG_DEBUG
, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3301 wpa_hexdump_key(MSG_DEBUG
, "WNM: GTK in WNM-Sleep Mode exit",
3302 gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
3308 #ifdef CONFIG_IEEE80211W
3309 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3311 struct wpa_group
*gsm
= sm
->group
;
3313 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3317 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3319 *pos
++ = WNM_SLEEP_SUBELEM_IGTK
;
3320 *pos
++ = 2 + 6 + len
;
3321 WPA_PUT_LE16(pos
, gsm
->GN_igtk
);
3323 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, pos
) != 0)
3327 os_memcpy(pos
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3330 wpa_printf(MSG_DEBUG
, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3332 wpa_hexdump_key(MSG_DEBUG
, "WNM: IGTK in WNM-Sleep Mode exit",
3333 gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3337 #endif /* CONFIG_IEEE80211W */
3338 #endif /* CONFIG_WNM */
3341 static void wpa_group_setkeys(struct wpa_authenticator
*wpa_auth
,
3342 struct wpa_group
*group
)
3346 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3347 "SETKEYS (VLAN-ID %d)", group
->vlan_id
);
3348 group
->changed
= TRUE
;
3349 group
->wpa_group_state
= WPA_GROUP_SETKEYS
;
3350 group
->GTKReKey
= FALSE
;
3352 group
->GM
= group
->GN
;
3354 #ifdef CONFIG_IEEE80211W
3355 tmp
= group
->GM_igtk
;
3356 group
->GM_igtk
= group
->GN_igtk
;
3357 group
->GN_igtk
= tmp
;
3358 #endif /* CONFIG_IEEE80211W */
3359 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3360 * counting the STAs that are marked with GUpdateStationKeys instead of
3361 * including all STAs that could be in not-yet-completed state. */
3362 wpa_gtk_update(wpa_auth
, group
);
3364 if (group
->GKeyDoneStations
) {
3365 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: Unexpected "
3366 "GKeyDoneStations=%d when starting new GTK rekey",
3367 group
->GKeyDoneStations
);
3368 group
->GKeyDoneStations
= 0;
3370 wpa_auth_for_each_sta(wpa_auth
, wpa_group_update_sta
, group
);
3371 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: GKeyDoneStations=%d",
3372 group
->GKeyDoneStations
);
3376 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
3377 struct wpa_group
*group
)
3381 if (wpa_auth_set_key(wpa_auth
, group
->vlan_id
,
3382 wpa_cipher_to_alg(wpa_auth
->conf
.wpa_group
),
3383 broadcast_ether_addr
, group
->GN
,
3384 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3387 #ifdef CONFIG_IEEE80211W
3388 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3392 alg
= wpa_cipher_to_alg(wpa_auth
->conf
.group_mgmt_cipher
);
3393 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3396 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, alg
,
3397 broadcast_ether_addr
, group
->GN_igtk
,
3398 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3401 #endif /* CONFIG_IEEE80211W */
3407 static int wpa_group_disconnect_cb(struct wpa_state_machine
*sm
, void *ctx
)
3409 if (sm
->group
== ctx
) {
3410 wpa_printf(MSG_DEBUG
, "WPA: Mark STA " MACSTR
3411 " for discconnection due to fatal failure",
3413 sm
->Disconnect
= TRUE
;
3420 static void wpa_group_fatal_failure(struct wpa_authenticator
*wpa_auth
,
3421 struct wpa_group
*group
)
3423 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state FATAL_FAILURE");
3424 group
->changed
= TRUE
;
3425 group
->wpa_group_state
= WPA_GROUP_FATAL_FAILURE
;
3426 wpa_auth_for_each_sta(wpa_auth
, wpa_group_disconnect_cb
, group
);
3430 static int wpa_group_setkeysdone(struct wpa_authenticator
*wpa_auth
,
3431 struct wpa_group
*group
)
3433 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3434 "SETKEYSDONE (VLAN-ID %d)", group
->vlan_id
);
3435 group
->changed
= TRUE
;
3436 group
->wpa_group_state
= WPA_GROUP_SETKEYSDONE
;
3438 if (wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
3439 wpa_group_fatal_failure(wpa_auth
, group
);
3447 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
3448 struct wpa_group
*group
)
3451 wpa_group_gtk_init(wpa_auth
, group
);
3452 } else if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
) {
3453 /* Do not allow group operations */
3454 } else if (group
->wpa_group_state
== WPA_GROUP_GTK_INIT
&&
3455 group
->GTKAuthenticator
) {
3456 wpa_group_setkeysdone(wpa_auth
, group
);
3457 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
&&
3459 wpa_group_setkeys(wpa_auth
, group
);
3460 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYS
) {
3461 if (group
->GKeyDoneStations
== 0)
3462 wpa_group_setkeysdone(wpa_auth
, group
);
3463 else if (group
->GTKReKey
)
3464 wpa_group_setkeys(wpa_auth
, group
);
3469 static int wpa_sm_step(struct wpa_state_machine
*sm
)
3474 if (sm
->in_step_loop
) {
3475 /* This should not happen, but if it does, make sure we do not
3476 * end up freeing the state machine too early by exiting the
3477 * recursive call. */
3478 wpa_printf(MSG_ERROR
, "WPA: wpa_sm_step() called recursively");
3482 sm
->in_step_loop
= 1;
3484 if (sm
->pending_deinit
)
3487 sm
->changed
= FALSE
;
3488 sm
->wpa_auth
->group
->changed
= FALSE
;
3490 SM_STEP_RUN(WPA_PTK
);
3491 if (sm
->pending_deinit
)
3493 SM_STEP_RUN(WPA_PTK_GROUP
);
3494 if (sm
->pending_deinit
)
3496 wpa_group_sm_step(sm
->wpa_auth
, sm
->group
);
3497 } while (sm
->changed
|| sm
->wpa_auth
->group
->changed
);
3498 sm
->in_step_loop
= 0;
3500 if (sm
->pending_deinit
) {
3501 wpa_printf(MSG_DEBUG
, "WPA: Completing pending STA state "
3502 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
3503 wpa_free_sta_sm(sm
);
3510 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
)
3512 struct wpa_state_machine
*sm
= eloop_ctx
;
3517 void wpa_auth_sm_notify(struct wpa_state_machine
*sm
)
3521 eloop_register_timeout(0, 0, wpa_sm_call_step
, sm
, NULL
);
3525 void wpa_gtk_rekey(struct wpa_authenticator
*wpa_auth
)
3528 struct wpa_group
*group
;
3530 if (wpa_auth
== NULL
)
3533 group
= wpa_auth
->group
;
3535 for (i
= 0; i
< 2; i
++) {
3537 group
->GM
= group
->GN
;
3539 #ifdef CONFIG_IEEE80211W
3540 tmp
= group
->GM_igtk
;
3541 group
->GM_igtk
= group
->GN_igtk
;
3542 group
->GN_igtk
= tmp
;
3543 #endif /* CONFIG_IEEE80211W */
3544 wpa_gtk_update(wpa_auth
, group
);
3545 wpa_group_config_group_keys(wpa_auth
, group
);
3550 static const char * wpa_bool_txt(int val
)
3552 return val
? "TRUE" : "FALSE";
3556 #define RSN_SUITE "%02x-%02x-%02x-%d"
3557 #define RSN_SUITE_ARG(s) \
3558 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3560 int wpa_get_mib(struct wpa_authenticator
*wpa_auth
, char *buf
, size_t buflen
)
3563 char pmkid_txt
[PMKID_LEN
* 2 + 1];
3564 #ifdef CONFIG_RSN_PREAUTH
3565 const int preauth
= 1;
3566 #else /* CONFIG_RSN_PREAUTH */
3567 const int preauth
= 0;
3568 #endif /* CONFIG_RSN_PREAUTH */
3570 if (wpa_auth
== NULL
)
3573 ret
= os_snprintf(buf
+ len
, buflen
- len
,
3574 "dot11RSNAOptionImplemented=TRUE\n"
3575 "dot11RSNAPreauthenticationImplemented=%s\n"
3576 "dot11RSNAEnabled=%s\n"
3577 "dot11RSNAPreauthenticationEnabled=%s\n",
3578 wpa_bool_txt(preauth
),
3579 wpa_bool_txt(wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
),
3580 wpa_bool_txt(wpa_auth
->conf
.rsn_preauth
));
3581 if (os_snprintf_error(buflen
- len
, ret
))
3585 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
3586 wpa_auth
->dot11RSNAPMKIDUsed
, PMKID_LEN
);
3589 buf
+ len
, buflen
- len
,
3590 "dot11RSNAConfigVersion=%u\n"
3591 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
3592 /* FIX: dot11RSNAConfigGroupCipher */
3593 /* FIX: dot11RSNAConfigGroupRekeyMethod */
3594 /* FIX: dot11RSNAConfigGroupRekeyTime */
3595 /* FIX: dot11RSNAConfigGroupRekeyPackets */
3596 "dot11RSNAConfigGroupRekeyStrict=%u\n"
3597 "dot11RSNAConfigGroupUpdateCount=%u\n"
3598 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
3599 "dot11RSNAConfigGroupCipherSize=%u\n"
3600 "dot11RSNAConfigPMKLifetime=%u\n"
3601 "dot11RSNAConfigPMKReauthThreshold=%u\n"
3602 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
3603 "dot11RSNAConfigSATimeout=%u\n"
3604 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
3605 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
3606 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
3607 "dot11RSNAPMKIDUsed=%s\n"
3608 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
3609 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
3610 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
3611 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
3612 "dot11RSNA4WayHandshakeFailures=%u\n"
3613 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
3615 !!wpa_auth
->conf
.wpa_strict_rekey
,
3616 wpa_auth
->conf
.wpa_group_update_count
,
3617 wpa_auth
->conf
.wpa_pairwise_update_count
,
3618 wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
) * 8,
3619 dot11RSNAConfigPMKLifetime
,
3620 dot11RSNAConfigPMKReauthThreshold
,
3621 dot11RSNAConfigSATimeout
,
3622 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteSelected
),
3623 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherSelected
),
3624 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherSelected
),
3626 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteRequested
),
3627 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherRequested
),
3628 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherRequested
),
3629 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
,
3630 wpa_auth
->dot11RSNA4WayHandshakeFailures
);
3631 if (os_snprintf_error(buflen
- len
, ret
))
3635 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
3636 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
3639 ret
= os_snprintf(buf
+ len
, buflen
- len
, "hostapdWPAGroupState=%d\n",
3640 wpa_auth
->group
->wpa_group_state
);
3641 if (os_snprintf_error(buflen
- len
, ret
))
3649 int wpa_get_mib_sta(struct wpa_state_machine
*sm
, char *buf
, size_t buflen
)
3657 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
3659 /* dot11RSNAStatsEntry */
3661 pairwise
= wpa_cipher_to_suite(sm
->wpa
== WPA_VERSION_WPA2
?
3662 WPA_PROTO_RSN
: WPA_PROTO_WPA
,
3668 buf
+ len
, buflen
- len
,
3669 /* TODO: dot11RSNAStatsIndex */
3670 "dot11RSNAStatsSTAAddress=" MACSTR
"\n"
3671 "dot11RSNAStatsVersion=1\n"
3672 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE
"\n"
3673 /* TODO: dot11RSNAStatsTKIPICVErrors */
3674 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
3675 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
3676 /* TODO: dot11RSNAStatsCCMPReplays */
3677 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
3678 /* TODO: dot11RSNAStatsTKIPReplays */,
3680 RSN_SUITE_ARG(pairwise
),
3681 sm
->dot11RSNAStatsTKIPLocalMICFailures
,
3682 sm
->dot11RSNAStatsTKIPRemoteMICFailures
);
3683 if (os_snprintf_error(buflen
- len
, ret
))
3688 ret
= os_snprintf(buf
+ len
, buflen
- len
,
3689 "hostapdWPAPTKState=%d\n"
3690 "hostapdWPAPTKGroupState=%d\n",
3692 sm
->wpa_ptk_group_state
);
3693 if (os_snprintf_error(buflen
- len
, ret
))
3701 void wpa_auth_countermeasures_start(struct wpa_authenticator
*wpa_auth
)
3704 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
++;
3708 int wpa_auth_pairwise_set(struct wpa_state_machine
*sm
)
3710 return sm
&& sm
->pairwise_set
;
3714 int wpa_auth_get_pairwise(struct wpa_state_machine
*sm
)
3716 return sm
->pairwise
;
3720 int wpa_auth_sta_key_mgmt(struct wpa_state_machine
*sm
)
3724 return sm
->wpa_key_mgmt
;
3728 int wpa_auth_sta_wpa_version(struct wpa_state_machine
*sm
)
3736 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine
*sm
,
3737 struct rsn_pmksa_cache_entry
*entry
)
3739 if (sm
== NULL
|| sm
->pmksa
!= entry
)
3746 struct rsn_pmksa_cache_entry
*
3747 wpa_auth_sta_get_pmksa(struct wpa_state_machine
*sm
)
3749 return sm
? sm
->pmksa
: NULL
;
3753 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine
*sm
)
3756 sm
->dot11RSNAStatsTKIPLocalMICFailures
++;
3760 const u8
* wpa_auth_get_wpa_ie(struct wpa_authenticator
*wpa_auth
, size_t *len
)
3762 if (wpa_auth
== NULL
)
3764 *len
= wpa_auth
->wpa_ie_len
;
3765 return wpa_auth
->wpa_ie
;
3769 int wpa_auth_pmksa_add(struct wpa_state_machine
*sm
, const u8
*pmk
,
3770 unsigned int pmk_len
,
3771 int session_timeout
, struct eapol_state_machine
*eapol
)
3773 if (sm
== NULL
|| sm
->wpa
!= WPA_VERSION_WPA2
||
3774 sm
->wpa_auth
->conf
.disable_pmksa_caching
)
3777 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
3778 if (pmk_len
> PMK_LEN_SUITE_B_192
)
3779 pmk_len
= PMK_LEN_SUITE_B_192
;
3780 } else if (pmk_len
> PMK_LEN
) {
3784 if (pmksa_cache_auth_add(sm
->wpa_auth
->pmksa
, pmk
, pmk_len
, NULL
,
3785 sm
->PTK
.kck
, sm
->PTK
.kck_len
,
3786 sm
->wpa_auth
->addr
, sm
->addr
, session_timeout
,
3787 eapol
, sm
->wpa_key_mgmt
))
3794 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator
*wpa_auth
,
3795 const u8
*pmk
, size_t len
, const u8
*sta_addr
,
3796 int session_timeout
,
3797 struct eapol_state_machine
*eapol
)
3799 if (wpa_auth
== NULL
)
3802 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, len
, NULL
,
3805 sta_addr
, session_timeout
, eapol
,
3806 WPA_KEY_MGMT_IEEE8021X
))
3813 int wpa_auth_pmksa_add_sae(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
3814 const u8
*pmk
, const u8
*pmkid
)
3816 if (wpa_auth
->conf
.disable_pmksa_caching
)
3819 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, PMK_LEN
, pmkid
,
3821 wpa_auth
->addr
, addr
, 0, NULL
,
3829 void wpa_auth_pmksa_remove(struct wpa_authenticator
*wpa_auth
,
3832 struct rsn_pmksa_cache_entry
*pmksa
;
3834 if (wpa_auth
== NULL
|| wpa_auth
->pmksa
== NULL
)
3836 pmksa
= pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, NULL
);
3838 wpa_printf(MSG_DEBUG
, "WPA: Remove PMKSA cache entry for "
3839 MACSTR
" based on request", MAC2STR(sta_addr
));
3840 pmksa_cache_free_entry(wpa_auth
->pmksa
, pmksa
);
3845 int wpa_auth_pmksa_list(struct wpa_authenticator
*wpa_auth
, char *buf
,
3848 if (!wpa_auth
|| !wpa_auth
->pmksa
)
3850 return pmksa_cache_auth_list(wpa_auth
->pmksa
, buf
, len
);
3854 void wpa_auth_pmksa_flush(struct wpa_authenticator
*wpa_auth
)
3856 if (wpa_auth
&& wpa_auth
->pmksa
)
3857 pmksa_cache_auth_flush(wpa_auth
->pmksa
);
3861 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
3864 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
3865 char *buf
, size_t len
)
3867 if (!wpa_auth
|| !wpa_auth
->pmksa
)
3870 return pmksa_cache_auth_list_mesh(wpa_auth
->pmksa
, addr
, buf
, len
);
3874 struct rsn_pmksa_cache_entry
*
3875 wpa_auth_pmksa_create_entry(const u8
*aa
, const u8
*spa
, const u8
*pmk
,
3876 const u8
*pmkid
, int expiration
)
3878 struct rsn_pmksa_cache_entry
*entry
;
3879 struct os_reltime now
;
3881 entry
= pmksa_cache_auth_create_entry(pmk
, PMK_LEN
, pmkid
, NULL
, 0, aa
,
3882 spa
, 0, NULL
, WPA_KEY_MGMT_SAE
);
3886 os_get_reltime(&now
);
3887 entry
->expiration
= now
.sec
+ expiration
;
3892 int wpa_auth_pmksa_add_entry(struct wpa_authenticator
*wpa_auth
,
3893 struct rsn_pmksa_cache_entry
*entry
)
3897 if (!wpa_auth
|| !wpa_auth
->pmksa
)
3900 ret
= pmksa_cache_auth_add_entry(wpa_auth
->pmksa
, entry
);
3902 wpa_printf(MSG_DEBUG
,
3903 "RSN: Failed to store external PMKSA cache for "
3904 MACSTR
, MAC2STR(entry
->spa
));
3909 #endif /* CONFIG_MESH */
3910 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
3913 struct rsn_pmksa_cache_entry
*
3914 wpa_auth_pmksa_get(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
,
3917 if (!wpa_auth
|| !wpa_auth
->pmksa
)
3919 return pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, pmkid
);
3923 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry
*pmksa
,
3924 struct wpa_state_machine
*sm
,
3925 struct wpa_authenticator
*wpa_auth
,
3932 os_memcpy(pmk
, pmksa
->pmk
, PMK_LEN
);
3933 os_memcpy(pmkid
, pmksa
->pmkid
, PMKID_LEN
);
3934 os_memcpy(wpa_auth
->dot11RSNAPMKIDUsed
, pmksa
->pmkid
, PMKID_LEN
);
3939 * Remove and free the group from wpa_authenticator. This is triggered by a
3940 * callback to make sure nobody is currently iterating the group list while it
3943 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
3944 struct wpa_group
*group
)
3946 struct wpa_group
*prev
= wpa_auth
->group
;
3948 wpa_printf(MSG_DEBUG
, "WPA: Remove group state machine for VLAN-ID %d",
3952 if (prev
->next
== group
) {
3953 /* This never frees the special first group as needed */
3954 prev
->next
= group
->next
;
3964 /* Increase the reference counter for group */
3965 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
3966 struct wpa_group
*group
)
3968 /* Skip the special first group */
3969 if (wpa_auth
->group
== group
)
3972 group
->references
++;
3976 /* Decrease the reference counter and maybe free the group */
3977 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
3978 struct wpa_group
*group
)
3980 /* Skip the special first group */
3981 if (wpa_auth
->group
== group
)
3984 group
->references
--;
3985 if (group
->references
)
3987 wpa_group_free(wpa_auth
, group
);
3992 * Add a group that has its references counter set to zero. Caller needs to
3993 * call wpa_group_get() on the return value to mark the entry in use.
3995 static struct wpa_group
*
3996 wpa_auth_add_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
3998 struct wpa_group
*group
;
4000 if (wpa_auth
== NULL
|| wpa_auth
->group
== NULL
)
4003 wpa_printf(MSG_DEBUG
, "WPA: Add group state machine for VLAN-ID %d",
4005 group
= wpa_group_init(wpa_auth
, vlan_id
, 0);
4009 group
->next
= wpa_auth
->group
->next
;
4010 wpa_auth
->group
->next
= group
;
4017 * Enforce that the group state machine for the VLAN is running, increase
4018 * reference counter as interface is up. References might have been increased
4019 * even if a negative value is returned.
4020 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4022 int wpa_auth_ensure_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4024 struct wpa_group
*group
;
4026 if (wpa_auth
== NULL
)
4029 group
= wpa_auth
->group
;
4031 if (group
->vlan_id
== vlan_id
)
4033 group
= group
->next
;
4036 if (group
== NULL
) {
4037 group
= wpa_auth_add_group(wpa_auth
, vlan_id
);
4042 wpa_printf(MSG_DEBUG
,
4043 "WPA: Ensure group state machine running for VLAN ID %d",
4046 wpa_group_get(wpa_auth
, group
);
4047 group
->num_setup_iface
++;
4049 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4057 * Decrease reference counter, expected to be zero afterwards.
4058 * returns: -1 on error (group not found, group in fail state)
4059 * -2 if wpa_group is still referenced
4062 int wpa_auth_release_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4064 struct wpa_group
*group
;
4067 if (wpa_auth
== NULL
)
4070 group
= wpa_auth
->group
;
4072 if (group
->vlan_id
== vlan_id
)
4074 group
= group
->next
;
4080 wpa_printf(MSG_DEBUG
,
4081 "WPA: Try stopping group state machine for VLAN ID %d",
4084 if (group
->num_setup_iface
<= 0) {
4085 wpa_printf(MSG_ERROR
,
4086 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4090 group
->num_setup_iface
--;
4092 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4095 if (group
->references
> 1) {
4096 wpa_printf(MSG_DEBUG
,
4097 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4102 wpa_group_put(wpa_auth
, group
);
4108 int wpa_auth_sta_set_vlan(struct wpa_state_machine
*sm
, int vlan_id
)
4110 struct wpa_group
*group
;
4112 if (sm
== NULL
|| sm
->wpa_auth
== NULL
)
4115 group
= sm
->wpa_auth
->group
;
4117 if (group
->vlan_id
== vlan_id
)
4119 group
= group
->next
;
4122 if (group
== NULL
) {
4123 group
= wpa_auth_add_group(sm
->wpa_auth
, vlan_id
);
4128 if (sm
->group
== group
)
4131 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4134 wpa_printf(MSG_DEBUG
, "WPA: Moving STA " MACSTR
" to use group state "
4135 "machine for VLAN ID %d", MAC2STR(sm
->addr
), vlan_id
);
4137 wpa_group_get(sm
->wpa_auth
, group
);
4138 wpa_group_put(sm
->wpa_auth
, sm
->group
);
4145 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator
*wpa_auth
,
4146 struct wpa_state_machine
*sm
, int ack
)
4148 if (wpa_auth
== NULL
|| sm
== NULL
)
4150 wpa_printf(MSG_DEBUG
, "WPA: EAPOL-Key TX status for STA " MACSTR
4151 " ack=%d", MAC2STR(sm
->addr
), ack
);
4152 if (sm
->pending_1_of_4_timeout
&& ack
) {
4154 * Some deployed supplicant implementations update their SNonce
4155 * for each EAPOL-Key 2/4 message even within the same 4-way
4156 * handshake and then fail to use the first SNonce when
4157 * deriving the PTK. This results in unsuccessful 4-way
4158 * handshake whenever the relatively short initial timeout is
4159 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4160 * around this by increasing the timeout now that we know that
4161 * the station has received the frame.
4163 int timeout_ms
= eapol_key_timeout_subseq
;
4164 wpa_printf(MSG_DEBUG
, "WPA: Increase initial EAPOL-Key 1/4 "
4165 "timeout by %u ms because of acknowledged frame",
4167 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
4168 eloop_register_timeout(timeout_ms
/ 1000,
4169 (timeout_ms
% 1000) * 1000,
4170 wpa_send_eapol_timeout
, wpa_auth
, sm
);
4175 int wpa_auth_uses_sae(struct wpa_state_machine
*sm
)
4179 return wpa_key_mgmt_sae(sm
->wpa_key_mgmt
);
4183 int wpa_auth_uses_ft_sae(struct wpa_state_machine
*sm
)
4187 return sm
->wpa_key_mgmt
== WPA_KEY_MGMT_FT_SAE
;
4192 int wpa_auth_get_ip_addr(struct wpa_state_machine
*sm
, u8
*addr
)
4194 if (sm
== NULL
|| WPA_GET_BE32(sm
->ip_addr
) == 0)
4196 os_memcpy(addr
, sm
->ip_addr
, 4);
4199 #endif /* CONFIG_P2P */
4202 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator
*wpa_auth
,
4203 struct radius_das_attrs
*attr
)
4205 return pmksa_cache_auth_radius_das_disconnect(wpa_auth
->pmksa
, attr
);
4209 void wpa_auth_reconfig_group_keys(struct wpa_authenticator
*wpa_auth
)
4211 struct wpa_group
*group
;
4215 for (group
= wpa_auth
->group
; group
; group
= group
->next
)
4216 wpa_group_config_group_keys(wpa_auth
, group
);
4222 struct wpa_auth_fils_iter_data
{
4223 struct wpa_authenticator
*auth
;
4225 struct rsn_pmksa_cache_entry
*pmksa
;
4231 static int wpa_auth_fils_iter(struct wpa_authenticator
*a
, void *ctx
)
4233 struct wpa_auth_fils_iter_data
*data
= ctx
;
4235 if (a
== data
->auth
|| !a
->conf
.fils_cache_id
||
4236 os_memcmp(a
->conf
.fils_cache_id
, data
->cache_id
,
4237 FILS_CACHE_ID_LEN
) != 0)
4239 data
->pmksa
= pmksa_cache_auth_get(a
->pmksa
, data
->spa
, data
->pmkid
);
4240 return data
->pmksa
!= NULL
;
4244 struct rsn_pmksa_cache_entry
*
4245 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator
*wpa_auth
,
4246 const u8
*sta_addr
, const u8
*pmkid
)
4248 struct wpa_auth_fils_iter_data idata
;
4250 if (!wpa_auth
->conf
.fils_cache_id_set
)
4252 idata
.auth
= wpa_auth
;
4253 idata
.cache_id
= wpa_auth
->conf
.fils_cache_id
;
4255 idata
.spa
= sta_addr
;
4256 idata
.pmkid
= pmkid
;
4257 wpa_auth_for_each_auth(wpa_auth
, wpa_auth_fils_iter
, &idata
);
4261 #endif /* CONFIG_FILS */