2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2019, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/state_machine.h"
14 #include "utils/bitfield.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ocv.h"
17 #include "crypto/aes.h"
18 #include "crypto/aes_wrap.h"
19 #include "crypto/aes_siv.h"
20 #include "crypto/crypto.h"
21 #include "crypto/sha1.h"
22 #include "crypto/sha256.h"
23 #include "crypto/sha384.h"
24 #include "crypto/random.h"
25 #include "eapol_auth/eapol_auth_sm.h"
26 #include "drivers/driver.h"
27 #include "ap_config.h"
28 #include "ieee802_11.h"
30 #include "pmksa_cache_auth.h"
31 #include "wpa_auth_i.h"
32 #include "wpa_auth_ie.h"
34 #define STATE_MACHINE_DATA struct wpa_state_machine
35 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
36 #define STATE_MACHINE_ADDR sm->addr
39 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
);
40 static int wpa_sm_step(struct wpa_state_machine
*sm
);
41 static int wpa_verify_key_mic(int akmp
, size_t pmk_len
, struct wpa_ptk
*PTK
,
42 u8
*data
, size_t data_len
);
44 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
45 u8
*buf
, size_t buf_len
, u16
*_key_data_len
);
46 static struct wpabuf
* fils_prepare_plainbuf(struct wpa_state_machine
*sm
,
47 const struct wpabuf
*hlp
);
48 #endif /* CONFIG_FILS */
49 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
);
50 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
51 struct wpa_group
*group
);
52 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
);
53 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
54 struct wpa_group
*group
);
55 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
56 struct wpa_group
*group
);
57 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
58 const u8
*pmk
, unsigned int pmk_len
,
60 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
61 struct wpa_group
*group
);
62 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
63 struct wpa_group
*group
);
64 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
65 struct wpa_group
*group
);
66 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
);
68 static const u32 eapol_key_timeout_first
= 100; /* ms */
69 static const u32 eapol_key_timeout_subseq
= 1000; /* ms */
70 static const u32 eapol_key_timeout_first_group
= 500; /* ms */
71 static const u32 eapol_key_timeout_no_retrans
= 4000; /* ms */
73 /* TODO: make these configurable */
74 static const int dot11RSNAConfigPMKLifetime
= 43200;
75 static const int dot11RSNAConfigPMKReauthThreshold
= 70;
76 static const int dot11RSNAConfigSATimeout
= 60;
79 static inline int wpa_auth_mic_failure_report(
80 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
82 if (wpa_auth
->cb
->mic_failure_report
)
83 return wpa_auth
->cb
->mic_failure_report(wpa_auth
->cb_ctx
, addr
);
88 static inline void wpa_auth_psk_failure_report(
89 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
91 if (wpa_auth
->cb
->psk_failure_report
)
92 wpa_auth
->cb
->psk_failure_report(wpa_auth
->cb_ctx
, addr
);
96 static inline void wpa_auth_set_eapol(struct wpa_authenticator
*wpa_auth
,
97 const u8
*addr
, wpa_eapol_variable var
,
100 if (wpa_auth
->cb
->set_eapol
)
101 wpa_auth
->cb
->set_eapol(wpa_auth
->cb_ctx
, addr
, var
, value
);
105 static inline int wpa_auth_get_eapol(struct wpa_authenticator
*wpa_auth
,
106 const u8
*addr
, wpa_eapol_variable var
)
108 if (wpa_auth
->cb
->get_eapol
== NULL
)
110 return wpa_auth
->cb
->get_eapol(wpa_auth
->cb_ctx
, addr
, var
);
114 static inline const u8
* wpa_auth_get_psk(struct wpa_authenticator
*wpa_auth
,
116 const u8
*p2p_dev_addr
,
117 const u8
*prev_psk
, size_t *psk_len
,
120 if (wpa_auth
->cb
->get_psk
== NULL
)
122 return wpa_auth
->cb
->get_psk(wpa_auth
->cb_ctx
, addr
, p2p_dev_addr
,
123 prev_psk
, psk_len
, vlan_id
);
127 static inline int wpa_auth_get_msk(struct wpa_authenticator
*wpa_auth
,
128 const u8
*addr
, u8
*msk
, size_t *len
)
130 if (wpa_auth
->cb
->get_msk
== NULL
)
132 return wpa_auth
->cb
->get_msk(wpa_auth
->cb_ctx
, addr
, msk
, len
);
136 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
138 enum wpa_alg alg
, const u8
*addr
, int idx
,
139 u8
*key
, size_t key_len
)
141 if (wpa_auth
->cb
->set_key
== NULL
)
143 return wpa_auth
->cb
->set_key(wpa_auth
->cb_ctx
, vlan_id
, alg
, addr
, idx
,
148 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
149 const u8
*addr
, int idx
, u8
*seq
)
153 if (wpa_auth
->cb
->get_seqnum
== NULL
)
155 res
= wpa_auth
->cb
->get_seqnum(wpa_auth
->cb_ctx
, addr
, idx
, seq
);
156 #ifdef CONFIG_TESTING_OPTIONS
157 if (!addr
&& idx
< 4 && wpa_auth
->conf
.gtk_rsc_override_set
) {
158 wpa_printf(MSG_DEBUG
,
159 "TESTING: Override GTK RSC %016llx --> %016llx",
160 (long long unsigned) WPA_GET_LE64(seq
),
162 WPA_GET_LE64(wpa_auth
->conf
.gtk_rsc_override
));
163 os_memcpy(seq
, wpa_auth
->conf
.gtk_rsc_override
,
166 if (!addr
&& idx
>= 4 && idx
<= 5 &&
167 wpa_auth
->conf
.igtk_rsc_override_set
) {
168 wpa_printf(MSG_DEBUG
,
169 "TESTING: Override IGTK RSC %016llx --> %016llx",
170 (long long unsigned) WPA_GET_LE64(seq
),
172 WPA_GET_LE64(wpa_auth
->conf
.igtk_rsc_override
));
173 os_memcpy(seq
, wpa_auth
->conf
.igtk_rsc_override
,
176 #endif /* CONFIG_TESTING_OPTIONS */
182 wpa_auth_send_eapol(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
183 const u8
*data
, size_t data_len
, int encrypt
)
185 if (wpa_auth
->cb
->send_eapol
== NULL
)
187 return wpa_auth
->cb
->send_eapol(wpa_auth
->cb_ctx
, addr
, data
, data_len
,
193 static inline int wpa_auth_start_ampe(struct wpa_authenticator
*wpa_auth
,
196 if (wpa_auth
->cb
->start_ampe
== NULL
)
198 return wpa_auth
->cb
->start_ampe(wpa_auth
->cb_ctx
, addr
);
200 #endif /* CONFIG_MESH */
203 int wpa_auth_for_each_sta(struct wpa_authenticator
*wpa_auth
,
204 int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
207 if (wpa_auth
->cb
->for_each_sta
== NULL
)
209 return wpa_auth
->cb
->for_each_sta(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
213 int wpa_auth_for_each_auth(struct wpa_authenticator
*wpa_auth
,
214 int (*cb
)(struct wpa_authenticator
*a
, void *ctx
),
217 if (wpa_auth
->cb
->for_each_auth
== NULL
)
219 return wpa_auth
->cb
->for_each_auth(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
223 void wpa_auth_logger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
224 logger_level level
, const char *txt
)
226 if (wpa_auth
->cb
->logger
== NULL
)
228 wpa_auth
->cb
->logger(wpa_auth
->cb_ctx
, addr
, level
, txt
);
232 void wpa_auth_vlogger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
233 logger_level level
, const char *fmt
, ...)
239 if (wpa_auth
->cb
->logger
== NULL
)
242 maxlen
= os_strlen(fmt
) + 100;
243 format
= os_malloc(maxlen
);
248 vsnprintf(format
, maxlen
, fmt
, ap
);
251 wpa_auth_logger(wpa_auth
, addr
, level
, format
);
257 static void wpa_sta_disconnect(struct wpa_authenticator
*wpa_auth
,
258 const u8
*addr
, u16 reason
)
260 if (wpa_auth
->cb
->disconnect
== NULL
)
262 wpa_printf(MSG_DEBUG
, "wpa_sta_disconnect STA " MACSTR
" (reason %u)",
263 MAC2STR(addr
), reason
);
264 wpa_auth
->cb
->disconnect(wpa_auth
->cb_ctx
, addr
, reason
);
269 static int wpa_channel_info(struct wpa_authenticator
*wpa_auth
,
270 struct wpa_channel_info
*ci
)
272 if (!wpa_auth
->cb
->channel_info
)
274 return wpa_auth
->cb
->channel_info(wpa_auth
->cb_ctx
, ci
);
276 #endif /* CONFIG_OCV */
279 static int wpa_auth_update_vlan(struct wpa_authenticator
*wpa_auth
,
280 const u8
*addr
, int vlan_id
)
282 if (!wpa_auth
->cb
->update_vlan
)
284 return wpa_auth
->cb
->update_vlan(wpa_auth
->cb_ctx
, addr
, vlan_id
);
288 static void wpa_rekey_gmk(void *eloop_ctx
, void *timeout_ctx
)
290 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
292 if (random_get_bytes(wpa_auth
->group
->GMK
, WPA_GMK_LEN
)) {
293 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
296 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "GMK rekeyd");
297 wpa_hexdump_key(MSG_DEBUG
, "GMK",
298 wpa_auth
->group
->GMK
, WPA_GMK_LEN
);
301 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
302 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
303 wpa_rekey_gmk
, wpa_auth
, NULL
);
308 static void wpa_rekey_gtk(void *eloop_ctx
, void *timeout_ctx
)
310 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
311 struct wpa_group
*group
, *next
;
313 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "rekeying GTK");
314 group
= wpa_auth
->group
;
316 wpa_group_get(wpa_auth
, group
);
318 group
->GTKReKey
= TRUE
;
320 group
->changed
= FALSE
;
321 wpa_group_sm_step(wpa_auth
, group
);
322 } while (group
->changed
);
325 wpa_group_put(wpa_auth
, group
);
329 if (wpa_auth
->conf
.wpa_group_rekey
) {
330 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
,
331 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
336 static void wpa_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
338 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
339 struct wpa_state_machine
*sm
= timeout_ctx
;
341 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "rekeying PTK");
342 wpa_request_new_ptk(sm
);
347 void wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine
*sm
)
349 if (sm
&& sm
->wpa_auth
->conf
.wpa_ptk_rekey
) {
350 wpa_printf(MSG_DEBUG
, "WPA: Start PTK rekeying timer for "
351 MACSTR
" (%d seconds)", MAC2STR(sm
->addr
),
352 sm
->wpa_auth
->conf
.wpa_ptk_rekey
);
353 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
354 eloop_register_timeout(sm
->wpa_auth
->conf
.wpa_ptk_rekey
, 0,
355 wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
360 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine
*sm
, void *ctx
)
362 if (sm
->pmksa
== ctx
)
368 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
371 struct wpa_authenticator
*wpa_auth
= ctx
;
372 wpa_auth_for_each_sta(wpa_auth
, wpa_auth_pmksa_clear_cb
, entry
);
376 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator
*wpa_auth
,
377 struct wpa_group
*group
)
379 u8 buf
[ETH_ALEN
+ 8 + sizeof(unsigned long)];
383 if (random_get_bytes(group
->GMK
, WPA_GMK_LEN
) < 0)
385 wpa_hexdump_key(MSG_DEBUG
, "GMK", group
->GMK
, WPA_GMK_LEN
);
388 * Counter = PRF-256(Random number, "Init Counter",
389 * Local MAC Address || Time)
391 os_memcpy(buf
, wpa_auth
->addr
, ETH_ALEN
);
392 wpa_get_ntp_timestamp(buf
+ ETH_ALEN
);
393 ptr
= (unsigned long) group
;
394 os_memcpy(buf
+ ETH_ALEN
+ 8, &ptr
, sizeof(ptr
));
396 os_memset(buf
+ ETH_ALEN
, 0xab, 8);
397 os_memset(buf
+ ETH_ALEN
+ 8, 0xcd, sizeof(ptr
));
398 #endif /* TEST_FUZZ */
399 if (random_get_bytes(rkey
, sizeof(rkey
)) < 0)
402 if (sha1_prf(rkey
, sizeof(rkey
), "Init Counter", buf
, sizeof(buf
),
403 group
->Counter
, WPA_NONCE_LEN
) < 0)
405 wpa_hexdump_key(MSG_DEBUG
, "Key Counter",
406 group
->Counter
, WPA_NONCE_LEN
);
412 static struct wpa_group
* wpa_group_init(struct wpa_authenticator
*wpa_auth
,
413 int vlan_id
, int delay_init
)
415 struct wpa_group
*group
;
417 group
= os_zalloc(sizeof(struct wpa_group
));
421 group
->GTKAuthenticator
= TRUE
;
422 group
->vlan_id
= vlan_id
;
423 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
425 if (random_pool_ready() != 1) {
426 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
427 "for secure operations - update keys later when "
428 "the first station connects");
432 * Set initial GMK/Counter value here. The actual values that will be
433 * used in negotiations will be set once the first station tries to
434 * connect. This allows more time for collecting additional randomness
435 * on embedded devices.
437 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0) {
438 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
446 wpa_printf(MSG_DEBUG
, "WPA: Delay group state machine start "
447 "until Beacon frames have been configured");
448 /* Initialization is completed in wpa_init_keys(). */
450 wpa_group_sm_step(wpa_auth
, group
);
451 group
->GInit
= FALSE
;
452 wpa_group_sm_step(wpa_auth
, group
);
460 * wpa_init - Initialize WPA authenticator
461 * @addr: Authenticator address
462 * @conf: Configuration for WPA authenticator
463 * @cb: Callback functions for WPA authenticator
464 * Returns: Pointer to WPA authenticator data or %NULL on failure
466 struct wpa_authenticator
* wpa_init(const u8
*addr
,
467 struct wpa_auth_config
*conf
,
468 const struct wpa_auth_callbacks
*cb
,
471 struct wpa_authenticator
*wpa_auth
;
473 wpa_auth
= os_zalloc(sizeof(struct wpa_authenticator
));
474 if (wpa_auth
== NULL
)
476 os_memcpy(wpa_auth
->addr
, addr
, ETH_ALEN
);
477 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
479 wpa_auth
->cb_ctx
= cb_ctx
;
481 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
482 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
487 wpa_auth
->group
= wpa_group_init(wpa_auth
, 0, 1);
488 if (wpa_auth
->group
== NULL
) {
489 os_free(wpa_auth
->wpa_ie
);
494 wpa_auth
->pmksa
= pmksa_cache_auth_init(wpa_auth_pmksa_free_cb
,
496 if (wpa_auth
->pmksa
== NULL
) {
497 wpa_printf(MSG_ERROR
, "PMKSA cache initialization failed.");
498 os_free(wpa_auth
->group
);
499 os_free(wpa_auth
->wpa_ie
);
504 #ifdef CONFIG_IEEE80211R_AP
505 wpa_auth
->ft_pmk_cache
= wpa_ft_pmk_cache_init();
506 if (wpa_auth
->ft_pmk_cache
== NULL
) {
507 wpa_printf(MSG_ERROR
, "FT PMK cache initialization failed.");
508 os_free(wpa_auth
->group
);
509 os_free(wpa_auth
->wpa_ie
);
510 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
514 #endif /* CONFIG_IEEE80211R_AP */
516 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
517 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
518 wpa_rekey_gmk
, wpa_auth
, NULL
);
521 if (wpa_auth
->conf
.wpa_group_rekey
) {
522 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
, 0,
523 wpa_rekey_gtk
, wpa_auth
, NULL
);
527 if (WPA_GET_BE32(conf
->ip_addr_start
)) {
528 int count
= WPA_GET_BE32(conf
->ip_addr_end
) -
529 WPA_GET_BE32(conf
->ip_addr_start
) + 1;
533 wpa_auth
->ip_pool
= bitfield_alloc(count
);
535 #endif /* CONFIG_P2P */
541 int wpa_init_keys(struct wpa_authenticator
*wpa_auth
)
543 struct wpa_group
*group
= wpa_auth
->group
;
545 wpa_printf(MSG_DEBUG
, "WPA: Start group state machine to set initial "
547 wpa_group_sm_step(wpa_auth
, group
);
548 group
->GInit
= FALSE
;
549 wpa_group_sm_step(wpa_auth
, group
);
550 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
557 * wpa_deinit - Deinitialize WPA authenticator
558 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
560 void wpa_deinit(struct wpa_authenticator
*wpa_auth
)
562 struct wpa_group
*group
, *prev
;
564 eloop_cancel_timeout(wpa_rekey_gmk
, wpa_auth
, NULL
);
565 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
567 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
569 #ifdef CONFIG_IEEE80211R_AP
570 wpa_ft_pmk_cache_deinit(wpa_auth
->ft_pmk_cache
);
571 wpa_auth
->ft_pmk_cache
= NULL
;
572 wpa_ft_deinit(wpa_auth
);
573 #endif /* CONFIG_IEEE80211R_AP */
576 bitfield_free(wpa_auth
->ip_pool
);
577 #endif /* CONFIG_P2P */
580 os_free(wpa_auth
->wpa_ie
);
582 group
= wpa_auth
->group
;
594 * wpa_reconfig - Update WPA authenticator configuration
595 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
596 * @conf: Configuration for WPA authenticator
598 int wpa_reconfig(struct wpa_authenticator
*wpa_auth
,
599 struct wpa_auth_config
*conf
)
601 struct wpa_group
*group
;
602 if (wpa_auth
== NULL
)
605 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
606 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
607 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
612 * Reinitialize GTK to make sure it is suitable for the new
615 group
= wpa_auth
->group
;
616 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
618 wpa_group_sm_step(wpa_auth
, group
);
619 group
->GInit
= FALSE
;
620 wpa_group_sm_step(wpa_auth
, group
);
626 struct wpa_state_machine
*
627 wpa_auth_sta_init(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
628 const u8
*p2p_dev_addr
)
630 struct wpa_state_machine
*sm
;
632 if (wpa_auth
->group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
635 sm
= os_zalloc(sizeof(struct wpa_state_machine
));
638 os_memcpy(sm
->addr
, addr
, ETH_ALEN
);
640 os_memcpy(sm
->p2p_dev_addr
, p2p_dev_addr
, ETH_ALEN
);
642 sm
->wpa_auth
= wpa_auth
;
643 sm
->group
= wpa_auth
->group
;
644 wpa_group_get(sm
->wpa_auth
, sm
->group
);
650 int wpa_auth_sta_associated(struct wpa_authenticator
*wpa_auth
,
651 struct wpa_state_machine
*sm
)
653 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
656 #ifdef CONFIG_IEEE80211R_AP
657 if (sm
->ft_completed
) {
658 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
659 "FT authentication already completed - do not "
660 "start 4-way handshake");
661 /* Go to PTKINITDONE state to allow GTK rekeying */
662 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
666 #endif /* CONFIG_IEEE80211R_AP */
669 if (sm
->fils_completed
) {
670 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
671 "FILS authentication already completed - do not start 4-way handshake");
672 /* Go to PTKINITDONE state to allow GTK rekeying */
673 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
677 #endif /* CONFIG_FILS */
680 os_memset(&sm
->key_replay
, 0, sizeof(sm
->key_replay
));
681 sm
->ReAuthenticationRequest
= TRUE
;
682 return wpa_sm_step(sm
);
685 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
686 "start authentication");
690 if (wpa_sm_step(sm
) == 1)
691 return 1; /* should not really happen */
693 sm
->AuthenticationRequest
= TRUE
;
694 return wpa_sm_step(sm
);
698 void wpa_auth_sta_no_wpa(struct wpa_state_machine
*sm
)
700 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
701 * reassociates back to the same AP while the previous entry for the
702 * STA has not yet been removed. */
706 sm
->wpa_key_mgmt
= 0;
710 static void wpa_free_sta_sm(struct wpa_state_machine
*sm
)
713 if (WPA_GET_BE32(sm
->ip_addr
)) {
715 wpa_printf(MSG_DEBUG
, "P2P: Free assigned IP "
716 "address %u.%u.%u.%u from " MACSTR
,
717 sm
->ip_addr
[0], sm
->ip_addr
[1],
718 sm
->ip_addr
[2], sm
->ip_addr
[3],
720 start
= WPA_GET_BE32(sm
->wpa_auth
->conf
.ip_addr_start
);
721 bitfield_clear(sm
->wpa_auth
->ip_pool
,
722 WPA_GET_BE32(sm
->ip_addr
) - start
);
724 #endif /* CONFIG_P2P */
725 if (sm
->GUpdateStationKeys
) {
726 sm
->group
->GKeyDoneStations
--;
727 sm
->GUpdateStationKeys
= FALSE
;
729 #ifdef CONFIG_IEEE80211R_AP
730 os_free(sm
->assoc_resp_ftie
);
731 wpabuf_free(sm
->ft_pending_req_ies
);
732 #endif /* CONFIG_IEEE80211R_AP */
733 os_free(sm
->last_rx_eapol_key
);
736 wpa_group_put(sm
->wpa_auth
, sm
->group
);
738 wpabuf_clear_free(sm
->dpp_z
);
739 #endif /* CONFIG_DPP2 */
740 bin_clear_free(sm
, sizeof(*sm
));
744 void wpa_auth_sta_deinit(struct wpa_state_machine
*sm
)
749 if (sm
->wpa_auth
->conf
.wpa_strict_rekey
&& sm
->has_GTK
) {
750 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
751 "strict rekeying - force GTK rekey since STA "
753 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk
,
754 sm
->wpa_auth
, NULL
) == -1)
755 eloop_register_timeout(0, 500000, wpa_rekey_gtk
, sm
->wpa_auth
,
759 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
760 sm
->pending_1_of_4_timeout
= 0;
761 eloop_cancel_timeout(wpa_sm_call_step
, sm
, NULL
);
762 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
763 #ifdef CONFIG_IEEE80211R_AP
764 wpa_ft_sta_deinit(sm
);
765 #endif /* CONFIG_IEEE80211R_AP */
766 if (sm
->in_step_loop
) {
767 /* Must not free state machine while wpa_sm_step() is running.
768 * Freeing will be completed in the end of wpa_sm_step(). */
769 wpa_printf(MSG_DEBUG
, "WPA: Registering pending STA state "
770 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
771 sm
->pending_deinit
= 1;
777 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
)
782 sm
->PTKRequest
= TRUE
;
787 static int wpa_replay_counter_valid(struct wpa_key_replay_counter
*ctr
,
788 const u8
*replay_counter
)
791 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
794 if (os_memcmp(replay_counter
, ctr
[i
].counter
,
795 WPA_REPLAY_COUNTER_LEN
) == 0)
802 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter
*ctr
,
803 const u8
*replay_counter
)
806 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
808 (replay_counter
== NULL
||
809 os_memcmp(replay_counter
, ctr
[i
].counter
,
810 WPA_REPLAY_COUNTER_LEN
) == 0))
811 ctr
[i
].valid
= FALSE
;
816 #ifdef CONFIG_IEEE80211R_AP
817 static int ft_check_msg_2_of_4(struct wpa_authenticator
*wpa_auth
,
818 struct wpa_state_machine
*sm
,
819 struct wpa_eapol_ie_parse
*kde
)
821 struct wpa_ie_data ie
;
822 struct rsn_mdie
*mdie
;
824 if (wpa_parse_wpa_ie_rsn(kde
->rsn_ie
, kde
->rsn_ie_len
, &ie
) < 0 ||
825 ie
.num_pmkid
!= 1 || ie
.pmkid
== NULL
) {
826 wpa_printf(MSG_DEBUG
, "FT: No PMKR1Name in "
827 "FT 4-way handshake message 2/4");
831 os_memcpy(sm
->sup_pmk_r1_name
, ie
.pmkid
, PMKID_LEN
);
832 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Supplicant",
833 sm
->sup_pmk_r1_name
, PMKID_LEN
);
835 if (!kde
->mdie
|| !kde
->ftie
) {
836 wpa_printf(MSG_DEBUG
, "FT: No %s in FT 4-way handshake "
837 "message 2/4", kde
->mdie
? "FTIE" : "MDIE");
841 mdie
= (struct rsn_mdie
*) (kde
->mdie
+ 2);
842 if (kde
->mdie
[1] < sizeof(struct rsn_mdie
) ||
843 os_memcmp(wpa_auth
->conf
.mobility_domain
, mdie
->mobility_domain
,
844 MOBILITY_DOMAIN_ID_LEN
) != 0) {
845 wpa_printf(MSG_DEBUG
, "FT: MDIE mismatch");
849 if (sm
->assoc_resp_ftie
&&
850 (kde
->ftie
[1] != sm
->assoc_resp_ftie
[1] ||
851 os_memcmp(kde
->ftie
, sm
->assoc_resp_ftie
,
852 2 + sm
->assoc_resp_ftie
[1]) != 0)) {
853 wpa_printf(MSG_DEBUG
, "FT: FTIE mismatch");
854 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 2/4",
855 kde
->ftie
, kde
->ftie_len
);
856 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)AssocResp",
857 sm
->assoc_resp_ftie
, 2 + sm
->assoc_resp_ftie
[1]);
863 #endif /* CONFIG_IEEE80211R_AP */
866 static int wpa_receive_error_report(struct wpa_authenticator
*wpa_auth
,
867 struct wpa_state_machine
*sm
, int group
)
869 /* Supplicant reported a Michael MIC error */
870 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
871 "received EAPOL-Key Error Request "
872 "(STA detected Michael MIC failure (group=%d))",
875 if (group
&& wpa_auth
->conf
.wpa_group
!= WPA_CIPHER_TKIP
) {
876 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
877 "ignore Michael MIC failure report since "
878 "group cipher is not TKIP");
879 } else if (!group
&& sm
->pairwise
!= WPA_CIPHER_TKIP
) {
880 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
881 "ignore Michael MIC failure report since "
882 "pairwise cipher is not TKIP");
884 if (wpa_auth_mic_failure_report(wpa_auth
, sm
->addr
) > 0)
885 return 1; /* STA entry was removed */
886 sm
->dot11RSNAStatsTKIPRemoteMICFailures
++;
887 wpa_auth
->dot11RSNAStatsTKIPRemoteMICFailures
++;
891 * Error report is not a request for a new key handshake, but since
892 * Authenticator may do it, let's change the keys now anyway.
894 wpa_request_new_ptk(sm
);
899 static int wpa_try_alt_snonce(struct wpa_state_machine
*sm
, u8
*data
,
904 const u8
*pmk
= NULL
;
908 os_memset(&PTK
, 0, sizeof(PTK
));
910 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
911 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
912 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
913 sm
->p2p_dev_addr
, pmk
, &pmk_len
,
917 #ifdef CONFIG_IEEE80211R_AP
918 if (wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
919 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
920 sm
->xxkey_len
= pmk_len
;
922 #endif /* CONFIG_IEEE80211R_AP */
925 pmk_len
= sm
->pmk_len
;
928 if (wpa_derive_ptk(sm
, sm
->alt_SNonce
, pmk
, pmk_len
, &PTK
) < 0)
931 if (wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
932 data
, data_len
) == 0) {
933 if (sm
->PMK
!= pmk
) {
934 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
935 sm
->pmk_len
= pmk_len
;
941 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
942 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
))
947 wpa_printf(MSG_DEBUG
,
948 "WPA: Earlier SNonce did not result in matching MIC");
952 wpa_printf(MSG_DEBUG
,
953 "WPA: Earlier SNonce resulted in matching MIC");
954 sm
->alt_snonce_valid
= 0;
956 if (vlan_id
&& wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
957 wpa_auth_update_vlan(sm
->wpa_auth
, sm
->addr
, vlan_id
) < 0)
960 os_memcpy(sm
->SNonce
, sm
->alt_SNonce
, WPA_NONCE_LEN
);
961 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
962 forced_memzero(&PTK
, sizeof(PTK
));
963 sm
->PTK_valid
= TRUE
;
969 void wpa_receive(struct wpa_authenticator
*wpa_auth
,
970 struct wpa_state_machine
*sm
,
971 u8
*data
, size_t data_len
)
973 struct ieee802_1x_hdr
*hdr
;
974 struct wpa_eapol_key
*key
;
975 u16 key_info
, key_data_length
;
976 enum { PAIRWISE_2
, PAIRWISE_4
, GROUP_2
, REQUEST
} msg
;
978 struct wpa_eapol_ie_parse kde
;
980 size_t keyhdrlen
, mic_len
;
983 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
985 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL data", data
, data_len
);
987 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
988 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
990 if (data_len
< sizeof(*hdr
) + keyhdrlen
) {
991 wpa_printf(MSG_DEBUG
, "WPA: Ignore too short EAPOL-Key frame");
995 hdr
= (struct ieee802_1x_hdr
*) data
;
996 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
997 mic
= (u8
*) (key
+ 1);
998 key_info
= WPA_GET_BE16(key
->key_info
);
999 key_data
= mic
+ mic_len
+ 2;
1000 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
1001 wpa_printf(MSG_DEBUG
, "WPA: Received EAPOL-Key from " MACSTR
1002 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
1003 MAC2STR(sm
->addr
), key_info
, key
->type
,
1004 (unsigned int) mic_len
, key_data_length
);
1005 wpa_hexdump(MSG_MSGDUMP
,
1006 "WPA: EAPOL-Key header (ending before Key MIC)",
1008 wpa_hexdump(MSG_MSGDUMP
, "WPA: EAPOL-Key Key MIC",
1010 if (key_data_length
> data_len
- sizeof(*hdr
) - keyhdrlen
) {
1011 wpa_printf(MSG_INFO
, "WPA: Invalid EAPOL-Key frame - "
1012 "key_data overflow (%d > %lu)",
1014 (unsigned long) (data_len
- sizeof(*hdr
) -
1019 if (sm
->wpa
== WPA_VERSION_WPA2
) {
1020 if (key
->type
== EAPOL_KEY_TYPE_WPA
) {
1022 * Some deployed station implementations seem to send
1023 * msg 4/4 with incorrect type value in WPA2 mode.
1025 wpa_printf(MSG_DEBUG
, "Workaround: Allow EAPOL-Key "
1026 "with unexpected WPA type in RSN mode");
1027 } else if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
1028 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
1029 "unexpected type %d in RSN mode",
1034 if (key
->type
!= EAPOL_KEY_TYPE_WPA
) {
1035 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
1036 "unexpected type %d in WPA mode",
1042 wpa_hexdump(MSG_DEBUG
, "WPA: Received Key Nonce", key
->key_nonce
,
1044 wpa_hexdump(MSG_DEBUG
, "WPA: Received Replay Counter",
1045 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1047 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1050 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
1051 wpa_printf(MSG_DEBUG
, "WPA: Ignore SMK message");
1055 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1058 } else if (!(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
1060 msgtxt
= "2/2 Group";
1061 } else if (key_data_length
== 0 ||
1062 (mic_len
== 0 && (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) &&
1063 key_data_length
== AES_BLOCK_SIZE
)) {
1065 msgtxt
= "4/4 Pairwise";
1068 msgtxt
= "2/4 Pairwise";
1071 if (msg
== REQUEST
|| msg
== PAIRWISE_2
|| msg
== PAIRWISE_4
||
1073 u16 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
1074 if (sm
->pairwise
== WPA_CIPHER_CCMP
||
1075 sm
->pairwise
== WPA_CIPHER_GCMP
) {
1076 if (wpa_use_cmac(sm
->wpa_key_mgmt
) &&
1077 !wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1078 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1079 wpa_auth_logger(wpa_auth
, sm
->addr
,
1081 "advertised support for "
1082 "AES-128-CMAC, but did not "
1087 if (!wpa_use_cmac(sm
->wpa_key_mgmt
) &&
1088 !wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1089 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1090 wpa_auth_logger(wpa_auth
, sm
->addr
,
1092 "did not use HMAC-SHA1-AES "
1098 if (wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1099 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
1100 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1101 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1106 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1107 if (sm
->req_replay_counter_used
&&
1108 os_memcmp(key
->replay_counter
, sm
->req_replay_counter
,
1109 WPA_REPLAY_COUNTER_LEN
) <= 0) {
1110 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1111 "received EAPOL-Key request with "
1112 "replayed counter");
1117 if (!(key_info
& WPA_KEY_INFO_REQUEST
) &&
1118 !wpa_replay_counter_valid(sm
->key_replay
, key
->replay_counter
)) {
1121 if (msg
== PAIRWISE_2
&&
1122 wpa_replay_counter_valid(sm
->prev_key_replay
,
1123 key
->replay_counter
) &&
1124 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1125 os_memcmp(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0)
1128 * Some supplicant implementations (e.g., Windows XP
1129 * WZC) update SNonce for each EAPOL-Key 2/4. This
1130 * breaks the workaround on accepting any of the
1131 * pending requests, so allow the SNonce to be updated
1132 * even if we have already sent out EAPOL-Key 3/4.
1134 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1135 "Process SNonce update from STA "
1136 "based on retransmitted EAPOL-Key "
1138 sm
->update_snonce
= 1;
1139 os_memcpy(sm
->alt_SNonce
, sm
->SNonce
, WPA_NONCE_LEN
);
1140 sm
->alt_snonce_valid
= TRUE
;
1141 os_memcpy(sm
->alt_replay_counter
,
1142 sm
->key_replay
[0].counter
,
1143 WPA_REPLAY_COUNTER_LEN
);
1144 goto continue_processing
;
1147 if (msg
== PAIRWISE_4
&& sm
->alt_snonce_valid
&&
1148 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1149 os_memcmp(key
->replay_counter
, sm
->alt_replay_counter
,
1150 WPA_REPLAY_COUNTER_LEN
) == 0) {
1152 * Supplicant may still be using the old SNonce since
1153 * there was two EAPOL-Key 2/4 messages and they had
1154 * different SNonce values.
1156 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1157 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1158 goto continue_processing
;
1161 if (msg
== PAIRWISE_2
&&
1162 wpa_replay_counter_valid(sm
->prev_key_replay
,
1163 key
->replay_counter
) &&
1164 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
) {
1165 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1166 "ignore retransmitted EAPOL-Key %s - "
1167 "SNonce did not change", msgtxt
);
1169 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1170 "received EAPOL-Key %s with "
1171 "unexpected replay counter", msgtxt
);
1173 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
1174 if (!sm
->key_replay
[i
].valid
)
1176 wpa_hexdump(MSG_DEBUG
, "pending replay counter",
1177 sm
->key_replay
[i
].counter
,
1178 WPA_REPLAY_COUNTER_LEN
);
1180 wpa_hexdump(MSG_DEBUG
, "received replay counter",
1181 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1185 continue_processing
:
1187 if (sm
->wpa
== WPA_VERSION_WPA2
&& mic_len
== 0 &&
1188 !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1189 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1190 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1193 #endif /* CONFIG_FILS */
1197 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKSTART
&&
1198 sm
->wpa_ptk_state
!= WPA_PTK_PTKCALCNEGOTIATING
&&
1199 (!sm
->update_snonce
||
1200 sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
)) {
1201 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1202 "received EAPOL-Key msg 2/4 in "
1203 "invalid state (%d) - dropped",
1207 random_add_randomness(key
->key_nonce
, WPA_NONCE_LEN
);
1208 if (sm
->group
->reject_4way_hs_for_entropy
) {
1210 * The system did not have enough entropy to generate
1211 * strong random numbers. Reject the first 4-way
1212 * handshake(s) and collect some entropy based on the
1213 * information from it. Once enough entropy is
1214 * available, the next atempt will trigger GMK/Key
1215 * Counter update and the station will be allowed to
1218 wpa_printf(MSG_DEBUG
, "WPA: Reject 4-way handshake to "
1219 "collect more entropy for random number "
1221 random_mark_pool_ready();
1222 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
1223 WLAN_REASON_PREV_AUTH_NOT_VALID
);
1228 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
||
1230 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1231 "received EAPOL-Key msg 4/4 in "
1232 "invalid state (%d) - dropped",
1238 if (sm
->wpa_ptk_group_state
!= WPA_PTK_GROUP_REKEYNEGOTIATING
1239 || !sm
->PTK_valid
) {
1240 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1241 "received EAPOL-Key msg 2/2 in "
1242 "invalid state (%d) - dropped",
1243 sm
->wpa_ptk_group_state
);
1251 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1252 "received EAPOL-Key frame (%s)", msgtxt
);
1254 if (key_info
& WPA_KEY_INFO_ACK
) {
1255 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1256 "received invalid EAPOL-Key: Key Ack set");
1260 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1261 !(key_info
& WPA_KEY_INFO_MIC
)) {
1262 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1263 "received invalid EAPOL-Key: Key MIC not set");
1268 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1269 (key_info
& WPA_KEY_INFO_MIC
)) {
1270 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1271 "received invalid EAPOL-Key: Key MIC set");
1274 #endif /* CONFIG_FILS */
1276 sm
->MICVerified
= FALSE
;
1277 if (sm
->PTK_valid
&& !sm
->update_snonce
) {
1279 wpa_verify_key_mic(sm
->wpa_key_mgmt
, sm
->pmk_len
, &sm
->PTK
,
1281 (msg
!= PAIRWISE_4
|| !sm
->alt_snonce_valid
||
1282 wpa_try_alt_snonce(sm
, data
, data_len
))) {
1283 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1284 "received EAPOL-Key with invalid MIC");
1286 wpa_printf(MSG_INFO
,
1287 "TEST: Ignore Key MIC failure for fuzz testing");
1289 #endif /* TEST_FUZZ */
1294 wpa_aead_decrypt(sm
, &sm
->PTK
, data
, data_len
,
1295 &key_data_length
) < 0) {
1296 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1297 "received EAPOL-Key with invalid MIC");
1299 wpa_printf(MSG_INFO
,
1300 "TEST: Ignore Key MIC failure for fuzz testing");
1302 #endif /* TEST_FUZZ */
1305 #endif /* CONFIG_FILS */
1308 #endif /* TEST_FUZZ */
1309 sm
->MICVerified
= TRUE
;
1310 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
1311 sm
->pending_1_of_4_timeout
= 0;
1314 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1315 if (sm
->MICVerified
) {
1316 sm
->req_replay_counter_used
= 1;
1317 os_memcpy(sm
->req_replay_counter
, key
->replay_counter
,
1318 WPA_REPLAY_COUNTER_LEN
);
1320 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1321 "received EAPOL-Key request with "
1327 * TODO: should decrypt key data field if encryption was used;
1328 * even though MAC address KDE is not normally encrypted,
1329 * supplicant is allowed to encrypt it.
1331 if (key_info
& WPA_KEY_INFO_ERROR
) {
1332 if (wpa_receive_error_report(
1334 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) > 0)
1335 return; /* STA entry was removed */
1336 } else if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
1337 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1338 "received EAPOL-Key Request for new "
1340 wpa_request_new_ptk(sm
);
1341 } else if (key_data_length
> 0 &&
1342 wpa_parse_kde_ies(key_data
, key_data_length
,
1346 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1347 "received EAPOL-Key Request for GTK "
1349 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
1350 wpa_rekey_gtk(wpa_auth
, NULL
);
1353 /* Do not allow the same key replay counter to be reused. */
1354 wpa_replay_counter_mark_invalid(sm
->key_replay
,
1355 key
->replay_counter
);
1357 if (msg
== PAIRWISE_2
) {
1359 * Maintain a copy of the pending EAPOL-Key frames in
1360 * case the EAPOL-Key frame was retransmitted. This is
1361 * needed to allow EAPOL-Key msg 2/4 reply to another
1362 * pending msg 1/4 to update the SNonce to work around
1363 * unexpected supplicant behavior.
1365 os_memcpy(sm
->prev_key_replay
, sm
->key_replay
,
1366 sizeof(sm
->key_replay
));
1368 os_memset(sm
->prev_key_replay
, 0,
1369 sizeof(sm
->prev_key_replay
));
1373 * Make sure old valid counters are not accepted anymore and
1374 * do not get copied again.
1376 wpa_replay_counter_mark_invalid(sm
->key_replay
, NULL
);
1379 os_free(sm
->last_rx_eapol_key
);
1380 sm
->last_rx_eapol_key
= os_memdup(data
, data_len
);
1381 if (sm
->last_rx_eapol_key
== NULL
)
1383 sm
->last_rx_eapol_key_len
= data_len
;
1385 sm
->rx_eapol_key_secure
= !!(key_info
& WPA_KEY_INFO_SECURE
);
1386 sm
->EAPOLKeyReceived
= TRUE
;
1387 sm
->EAPOLKeyPairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1388 sm
->EAPOLKeyRequest
= !!(key_info
& WPA_KEY_INFO_REQUEST
);
1389 os_memcpy(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
);
1394 static int wpa_gmk_to_gtk(const u8
*gmk
, const char *label
, const u8
*addr
,
1395 const u8
*gnonce
, u8
*gtk
, size_t gtk_len
)
1397 u8 data
[ETH_ALEN
+ WPA_NONCE_LEN
+ 8 + WPA_GTK_MAX_LEN
];
1401 /* GTK = PRF-X(GMK, "Group key expansion",
1402 * AA || GNonce || Time || random data)
1403 * The example described in the IEEE 802.11 standard uses only AA and
1404 * GNonce as inputs here. Add some more entropy since this derivation
1405 * is done only at the Authenticator and as such, does not need to be
1408 os_memset(data
, 0, sizeof(data
));
1409 os_memcpy(data
, addr
, ETH_ALEN
);
1410 os_memcpy(data
+ ETH_ALEN
, gnonce
, WPA_NONCE_LEN
);
1411 pos
= data
+ ETH_ALEN
+ WPA_NONCE_LEN
;
1412 wpa_get_ntp_timestamp(pos
);
1414 os_memset(pos
, 0xef, 8);
1415 #endif /* TEST_FUZZ */
1417 if (random_get_bytes(pos
, gtk_len
) < 0)
1420 #ifdef CONFIG_SHA384
1421 if (sha384_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1424 #else /* CONFIG_SHA384 */
1425 #ifdef CONFIG_SHA256
1426 if (sha256_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1429 #else /* CONFIG_SHA256 */
1430 if (sha1_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1433 #endif /* CONFIG_SHA256 */
1434 #endif /* CONFIG_SHA384 */
1436 forced_memzero(data
, sizeof(data
));
1442 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
)
1444 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1445 struct wpa_state_machine
*sm
= timeout_ctx
;
1447 sm
->pending_1_of_4_timeout
= 0;
1448 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "EAPOL-Key timeout");
1449 sm
->TimeoutEvt
= TRUE
;
1454 void __wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1455 struct wpa_state_machine
*sm
, int key_info
,
1456 const u8
*key_rsc
, const u8
*nonce
,
1457 const u8
*kde
, size_t kde_len
,
1458 int keyidx
, int encr
, int force_version
)
1460 struct ieee802_1x_hdr
*hdr
;
1461 struct wpa_eapol_key
*key
;
1462 size_t len
, mic_len
, keyhdrlen
;
1464 int key_data_len
, pad_len
= 0;
1466 int version
, pairwise
;
1468 u8
*key_mic
, *key_data
;
1470 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
1471 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
1473 len
= sizeof(struct ieee802_1x_hdr
) + keyhdrlen
;
1476 version
= force_version
;
1477 else if (wpa_use_akm_defined(sm
->wpa_key_mgmt
))
1478 version
= WPA_KEY_INFO_TYPE_AKM_DEFINED
;
1479 else if (wpa_use_cmac(sm
->wpa_key_mgmt
))
1480 version
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
1481 else if (sm
->pairwise
!= WPA_CIPHER_TKIP
)
1482 version
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
1484 version
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
1486 pairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1488 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1489 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1492 (key_info
& WPA_KEY_INFO_SECURE
) ? 1 : 0,
1493 (key_info
& WPA_KEY_INFO_MIC
) ? 1 : 0,
1494 (key_info
& WPA_KEY_INFO_ACK
) ? 1 : 0,
1495 (key_info
& WPA_KEY_INFO_INSTALL
) ? 1 : 0,
1496 pairwise
, (unsigned long) kde_len
, keyidx
, encr
);
1498 key_data_len
= kde_len
;
1500 if ((version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1501 wpa_use_aes_key_wrap(sm
->wpa_key_mgmt
) ||
1502 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) && encr
) {
1503 pad_len
= key_data_len
% 8;
1505 pad_len
= 8 - pad_len
;
1506 key_data_len
+= pad_len
+ 8;
1509 len
+= key_data_len
;
1510 if (!mic_len
&& encr
)
1511 len
+= AES_BLOCK_SIZE
;
1513 hdr
= os_zalloc(len
);
1516 hdr
->version
= wpa_auth
->conf
.eapol_version
;
1517 hdr
->type
= IEEE802_1X_TYPE_EAPOL_KEY
;
1518 hdr
->length
= host_to_be16(len
- sizeof(*hdr
));
1519 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1520 key_mic
= (u8
*) (key
+ 1);
1521 key_data
= ((u8
*) (hdr
+ 1)) + keyhdrlen
;
1523 key
->type
= sm
->wpa
== WPA_VERSION_WPA2
?
1524 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1525 key_info
|= version
;
1526 if (encr
&& sm
->wpa
== WPA_VERSION_WPA2
)
1527 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1528 if (sm
->wpa
!= WPA_VERSION_WPA2
)
1529 key_info
|= keyidx
<< WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1530 WPA_PUT_BE16(key
->key_info
, key_info
);
1532 alg
= pairwise
? sm
->pairwise
: wpa_auth
->conf
.wpa_group
;
1533 if (sm
->wpa
== WPA_VERSION_WPA2
&& !pairwise
)
1534 WPA_PUT_BE16(key
->key_length
, 0);
1536 WPA_PUT_BE16(key
->key_length
, wpa_cipher_key_len(alg
));
1538 for (i
= RSNA_MAX_EAPOL_RETRIES
- 1; i
> 0; i
--) {
1539 sm
->key_replay
[i
].valid
= sm
->key_replay
[i
- 1].valid
;
1540 os_memcpy(sm
->key_replay
[i
].counter
,
1541 sm
->key_replay
[i
- 1].counter
,
1542 WPA_REPLAY_COUNTER_LEN
);
1544 inc_byte_array(sm
->key_replay
[0].counter
, WPA_REPLAY_COUNTER_LEN
);
1545 os_memcpy(key
->replay_counter
, sm
->key_replay
[0].counter
,
1546 WPA_REPLAY_COUNTER_LEN
);
1547 wpa_hexdump(MSG_DEBUG
, "WPA: Replay Counter",
1548 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1549 sm
->key_replay
[0].valid
= TRUE
;
1552 os_memcpy(key
->key_nonce
, nonce
, WPA_NONCE_LEN
);
1555 os_memcpy(key
->key_rsc
, key_rsc
, WPA_KEY_RSC_LEN
);
1558 os_memcpy(key_data
, kde
, kde_len
);
1559 WPA_PUT_BE16(key_mic
+ mic_len
, kde_len
);
1561 } else if (!mic_len
&& kde
) {
1565 WPA_PUT_BE16(key_mic
, AES_BLOCK_SIZE
+ kde_len
);
1566 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1569 wpa_hexdump_key(MSG_DEBUG
, "WPA: KEK",
1570 sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1571 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1572 * to Key Data (exclusive). */
1573 aad
[0] = (u8
*) hdr
;
1574 aad_len
[0] = key_mic
+ 2 - (u8
*) hdr
;
1575 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, kde
, kde_len
,
1576 1, aad
, aad_len
, key_mic
+ 2) < 0) {
1577 wpa_printf(MSG_DEBUG
, "WPA: AES-SIV encryption failed");
1581 wpa_hexdump(MSG_DEBUG
, "WPA: Encrypted Key Data from SIV",
1582 key_mic
+ 2, AES_BLOCK_SIZE
+ kde_len
);
1583 #endif /* CONFIG_FILS */
1584 } else if (encr
&& kde
) {
1585 buf
= os_zalloc(key_data_len
);
1591 os_memcpy(pos
, kde
, kde_len
);
1597 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1599 if (version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1600 wpa_use_aes_key_wrap(sm
->wpa_key_mgmt
) ||
1601 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1602 wpa_printf(MSG_DEBUG
,
1603 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1604 (unsigned int) sm
->PTK
.kek_len
);
1605 if (aes_wrap(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
1606 (key_data_len
- 8) / 8, buf
, key_data
)) {
1611 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1612 #ifndef CONFIG_NO_RC4
1613 } else if (sm
->PTK
.kek_len
== 16) {
1616 wpa_printf(MSG_DEBUG
,
1617 "WPA: Encrypt Key Data using RC4");
1618 os_memcpy(key
->key_iv
,
1619 sm
->group
->Counter
+ WPA_NONCE_LEN
- 16, 16);
1620 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1621 os_memcpy(ek
, key
->key_iv
, 16);
1622 os_memcpy(ek
+ 16, sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1623 os_memcpy(key_data
, buf
, key_data_len
);
1624 rc4_skip(ek
, 32, 256, key_data
, key_data_len
);
1625 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1626 #endif /* CONFIG_NO_RC4 */
1635 if (key_info
& WPA_KEY_INFO_MIC
) {
1636 if (!sm
->PTK_valid
|| !mic_len
) {
1637 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1638 "PTK not valid when sending EAPOL-Key "
1644 if (wpa_eapol_key_mic(sm
->PTK
.kck
, sm
->PTK
.kck_len
,
1645 sm
->wpa_key_mgmt
, version
,
1646 (u8
*) hdr
, len
, key_mic
) < 0) {
1650 #ifdef CONFIG_TESTING_OPTIONS
1652 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
> 0.0 &&
1654 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
) {
1655 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1656 "Corrupting group EAPOL-Key Key MIC");
1659 #endif /* CONFIG_TESTING_OPTIONS */
1662 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_inc_EapolFramesTx
,
1664 wpa_auth_send_eapol(wpa_auth
, sm
->addr
, (u8
*) hdr
, len
,
1670 static void wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1671 struct wpa_state_machine
*sm
, int key_info
,
1672 const u8
*key_rsc
, const u8
*nonce
,
1673 const u8
*kde
, size_t kde_len
,
1674 int keyidx
, int encr
)
1677 int pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
1683 __wpa_send_eapol(wpa_auth
, sm
, key_info
, key_rsc
, nonce
, kde
, kde_len
,
1686 ctr
= pairwise
? sm
->TimeoutCtr
: sm
->GTimeoutCtr
;
1687 if (ctr
== 1 && wpa_auth
->conf
.tx_status
)
1688 timeout_ms
= pairwise
? eapol_key_timeout_first
:
1689 eapol_key_timeout_first_group
;
1691 timeout_ms
= eapol_key_timeout_subseq
;
1692 if (wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
1693 (!pairwise
|| (key_info
& WPA_KEY_INFO_MIC
)))
1694 timeout_ms
= eapol_key_timeout_no_retrans
;
1695 if (pairwise
&& ctr
== 1 && !(key_info
& WPA_KEY_INFO_MIC
))
1696 sm
->pending_1_of_4_timeout
= 1;
1699 #endif /* TEST_FUZZ */
1700 wpa_printf(MSG_DEBUG
, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1701 "counter %u)", timeout_ms
, ctr
);
1702 eloop_register_timeout(timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000,
1703 wpa_send_eapol_timeout
, wpa_auth
, sm
);
1707 static int wpa_verify_key_mic(int akmp
, size_t pmk_len
, struct wpa_ptk
*PTK
,
1708 u8
*data
, size_t data_len
)
1710 struct ieee802_1x_hdr
*hdr
;
1711 struct wpa_eapol_key
*key
;
1714 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
], *mic_pos
;
1715 size_t mic_len
= wpa_mic_len(akmp
, pmk_len
);
1717 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
1720 hdr
= (struct ieee802_1x_hdr
*) data
;
1721 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1722 mic_pos
= (u8
*) (key
+ 1);
1723 key_info
= WPA_GET_BE16(key
->key_info
);
1724 os_memcpy(mic
, mic_pos
, mic_len
);
1725 os_memset(mic_pos
, 0, mic_len
);
1726 if (wpa_eapol_key_mic(PTK
->kck
, PTK
->kck_len
, akmp
,
1727 key_info
& WPA_KEY_INFO_TYPE_MASK
,
1728 data
, data_len
, mic_pos
) ||
1729 os_memcmp_const(mic
, mic_pos
, mic_len
) != 0)
1731 os_memcpy(mic_pos
, mic
, mic_len
);
1736 void wpa_remove_ptk(struct wpa_state_machine
*sm
)
1738 sm
->PTK_valid
= FALSE
;
1739 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1740 if (wpa_auth_set_key(sm
->wpa_auth
, 0, WPA_ALG_NONE
, sm
->addr
, 0, NULL
,
1742 wpa_printf(MSG_DEBUG
,
1743 "RSN: PTK removal from the driver failed");
1744 sm
->pairwise_set
= FALSE
;
1745 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1749 int wpa_auth_sm_event(struct wpa_state_machine
*sm
, enum wpa_event event
)
1756 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1757 "event %d notification", event
);
1762 /* PTKs are derived through AMPE */
1763 if (wpa_auth_start_ampe(sm
->wpa_auth
, sm
->addr
)) {
1768 #endif /* CONFIG_MESH */
1773 sm
->DeauthenticationRequest
= TRUE
;
1774 #ifdef CONFIG_IEEE80211R_AP
1775 os_memset(sm
->PMK
, 0, sizeof(sm
->PMK
));
1777 os_memset(sm
->xxkey
, 0, sizeof(sm
->xxkey
));
1779 os_memset(sm
->pmk_r1
, 0, sizeof(sm
->pmk_r1
));
1781 #endif /* CONFIG_IEEE80211R_AP */
1784 case WPA_REAUTH_EAPOL
:
1787 * When using WPS, we may end up here if the STA
1788 * manages to re-associate without the previous STA
1789 * entry getting removed. Consequently, we need to make
1790 * sure that the WPA state machines gets initialized
1791 * properly at this point.
1793 wpa_printf(MSG_DEBUG
, "WPA state machine had not been "
1794 "started - initialize now");
1797 if (wpa_sm_step(sm
) == 1)
1798 return 1; /* should not really happen */
1800 sm
->AuthenticationRequest
= TRUE
;
1803 if (sm
->GUpdateStationKeys
) {
1805 * Reauthentication cancels the pending group key
1806 * update for this STA.
1808 sm
->group
->GKeyDoneStations
--;
1809 sm
->GUpdateStationKeys
= FALSE
;
1810 sm
->PtkGroupInit
= TRUE
;
1812 sm
->ReAuthenticationRequest
= TRUE
;
1815 #ifdef CONFIG_IEEE80211R_AP
1816 wpa_printf(MSG_DEBUG
, "FT: Retry PTK configuration "
1817 "after association");
1818 wpa_ft_install_ptk(sm
);
1820 /* Using FT protocol, not WPA auth state machine */
1821 sm
->ft_completed
= 1;
1822 wpa_auth_set_ptk_rekey_timer(sm
);
1824 #else /* CONFIG_IEEE80211R_AP */
1826 #endif /* CONFIG_IEEE80211R_AP */
1827 case WPA_ASSOC_FILS
:
1829 wpa_printf(MSG_DEBUG
,
1830 "FILS: TK configuration after association");
1832 sm
->fils_completed
= 1;
1834 #else /* CONFIG_FILS */
1836 #endif /* CONFIG_FILS */
1837 case WPA_DRV_STA_REMOVED
:
1838 sm
->tk_already_set
= FALSE
;
1842 #ifdef CONFIG_IEEE80211R_AP
1843 sm
->ft_completed
= 0;
1844 #endif /* CONFIG_IEEE80211R_AP */
1846 if (sm
->mgmt_frame_prot
&& event
== WPA_AUTH
)
1849 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1850 (event
== WPA_AUTH
|| event
== WPA_ASSOC
))
1852 #endif /* CONFIG_FILS */
1855 sm
->PTK_valid
= FALSE
;
1856 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1858 if (event
!= WPA_REAUTH_EAPOL
)
1862 if (sm
->in_step_loop
) {
1864 * wpa_sm_step() is already running - avoid recursive call to
1865 * it by making the existing loop process the new update.
1870 return wpa_sm_step(sm
);
1874 SM_STATE(WPA_PTK
, INITIALIZE
)
1876 SM_ENTRY_MA(WPA_PTK
, INITIALIZE
, wpa_ptk
);
1878 /* Init flag is not cleared here, so avoid busy
1879 * loop by claiming nothing changed. */
1880 sm
->changed
= FALSE
;
1884 if (sm
->GUpdateStationKeys
)
1885 sm
->group
->GKeyDoneStations
--;
1886 sm
->GUpdateStationKeys
= FALSE
;
1887 if (sm
->wpa
== WPA_VERSION_WPA
)
1888 sm
->PInitAKeys
= FALSE
;
1889 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1890 * Local AA > Remote AA)) */) {
1893 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 0);
1895 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
, 0);
1897 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
1898 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1899 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
1900 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1901 WPA_EAPOL_authorized
, 0);
1906 SM_STATE(WPA_PTK
, DISCONNECT
)
1908 u16 reason
= sm
->disconnect_reason
;
1910 SM_ENTRY_MA(WPA_PTK
, DISCONNECT
, wpa_ptk
);
1911 sm
->Disconnect
= FALSE
;
1912 sm
->disconnect_reason
= 0;
1914 reason
= WLAN_REASON_PREV_AUTH_NOT_VALID
;
1915 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
, reason
);
1919 SM_STATE(WPA_PTK
, DISCONNECTED
)
1921 SM_ENTRY_MA(WPA_PTK
, DISCONNECTED
, wpa_ptk
);
1922 sm
->DeauthenticationRequest
= FALSE
;
1926 SM_STATE(WPA_PTK
, AUTHENTICATION
)
1928 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION
, wpa_ptk
);
1929 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1930 sm
->PTK_valid
= FALSE
;
1931 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portControl_Auto
,
1933 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 1);
1934 sm
->AuthenticationRequest
= FALSE
;
1938 static void wpa_group_ensure_init(struct wpa_authenticator
*wpa_auth
,
1939 struct wpa_group
*group
)
1941 if (group
->first_sta_seen
)
1944 * System has run bit further than at the time hostapd was started
1945 * potentially very early during boot up. This provides better chances
1946 * of collecting more randomness on embedded systems. Re-initialize the
1947 * GMK and Counter here to improve their strength if there was not
1948 * enough entropy available immediately after system startup.
1950 wpa_printf(MSG_DEBUG
, "WPA: Re-initialize GMK/Counter on first "
1952 if (random_pool_ready() != 1) {
1953 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
1954 "to proceed - reject first 4-way handshake");
1955 group
->reject_4way_hs_for_entropy
= TRUE
;
1957 group
->first_sta_seen
= TRUE
;
1958 group
->reject_4way_hs_for_entropy
= FALSE
;
1961 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0 ||
1962 wpa_gtk_update(wpa_auth
, group
) < 0 ||
1963 wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
1964 wpa_printf(MSG_INFO
, "WPA: GMK/GTK setup failed");
1965 group
->first_sta_seen
= FALSE
;
1966 group
->reject_4way_hs_for_entropy
= TRUE
;
1971 SM_STATE(WPA_PTK
, AUTHENTICATION2
)
1973 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION2
, wpa_ptk
);
1975 wpa_group_ensure_init(sm
->wpa_auth
, sm
->group
);
1976 sm
->ReAuthenticationRequest
= FALSE
;
1979 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1980 * ambiguous. The Authenticator state machine uses a counter that is
1981 * incremented by one for each 4-way handshake. However, the security
1982 * analysis of 4-way handshake points out that unpredictable nonces
1983 * help in preventing precomputation attacks. Instead of the state
1984 * machine definition, use an unpredictable nonce value here to provide
1985 * stronger protection against potential precomputation attacks.
1987 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
1988 wpa_printf(MSG_ERROR
, "WPA: Failed to get random data for "
1990 sm
->Disconnect
= TRUE
;
1993 wpa_hexdump(MSG_DEBUG
, "WPA: Assign ANonce", sm
->ANonce
,
1995 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1996 * logical place than INITIALIZE since AUTHENTICATION2 can be
1997 * re-entered on ReAuthenticationRequest without going through
2003 static int wpa_auth_sm_ptk_update(struct wpa_state_machine
*sm
)
2005 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
2006 wpa_printf(MSG_ERROR
,
2007 "WPA: Failed to get random data for ANonce");
2008 sm
->Disconnect
= TRUE
;
2011 wpa_hexdump(MSG_DEBUG
, "WPA: Assign new ANonce", sm
->ANonce
,
2018 SM_STATE(WPA_PTK
, INITPMK
)
2020 u8 msk
[2 * PMK_LEN
];
2021 size_t len
= 2 * PMK_LEN
;
2023 SM_ENTRY_MA(WPA_PTK
, INITPMK
, wpa_ptk
);
2024 #ifdef CONFIG_IEEE80211R_AP
2026 #endif /* CONFIG_IEEE80211R_AP */
2028 wpa_printf(MSG_DEBUG
, "WPA: PMK from PMKSA cache");
2029 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2030 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
2032 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
) {
2033 wpa_printf(MSG_DEBUG
,
2034 "DPP: No PMKSA cache entry for STA - reject connection");
2035 sm
->Disconnect
= TRUE
;
2036 sm
->disconnect_reason
= WLAN_REASON_INVALID_PMKID
;
2038 #endif /* CONFIG_DPP */
2039 } else if (wpa_auth_get_msk(sm
->wpa_auth
, sm
->addr
, msk
, &len
) == 0) {
2040 unsigned int pmk_len
;
2042 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
))
2043 pmk_len
= PMK_LEN_SUITE_B_192
;
2046 wpa_printf(MSG_DEBUG
, "WPA: PMK from EAPOL state machine "
2047 "(MSK len=%lu PMK len=%u)", (unsigned long) len
,
2049 if (len
< pmk_len
) {
2050 wpa_printf(MSG_DEBUG
,
2051 "WPA: MSK not long enough (%u) to create PMK (%u)",
2052 (unsigned int) len
, (unsigned int) pmk_len
);
2053 sm
->Disconnect
= TRUE
;
2056 os_memcpy(sm
->PMK
, msk
, pmk_len
);
2057 sm
->pmk_len
= pmk_len
;
2058 #ifdef CONFIG_IEEE80211R_AP
2059 if (len
>= 2 * PMK_LEN
) {
2060 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
2061 os_memcpy(sm
->xxkey
, msk
, SHA384_MAC_LEN
);
2062 sm
->xxkey_len
= SHA384_MAC_LEN
;
2064 os_memcpy(sm
->xxkey
, msk
+ PMK_LEN
, PMK_LEN
);
2065 sm
->xxkey_len
= PMK_LEN
;
2068 #endif /* CONFIG_IEEE80211R_AP */
2070 wpa_printf(MSG_DEBUG
, "WPA: Could not get PMK, get_msk: %p",
2071 sm
->wpa_auth
->cb
->get_msk
);
2072 sm
->Disconnect
= TRUE
;
2075 forced_memzero(msk
, sizeof(msk
));
2077 sm
->req_replay_counter_used
= 0;
2078 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
2079 * will break reauthentication since EAPOL state machines may not be
2080 * get into AUTHENTICATING state that clears keyRun before WPA state
2081 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2082 * state and takes PMK from the previously used AAA Key. This will
2083 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2084 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2085 * be good workaround for this issue. */
2086 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyRun
, 0);
2090 SM_STATE(WPA_PTK
, INITPSK
)
2095 SM_ENTRY_MA(WPA_PTK
, INITPSK
, wpa_ptk
);
2096 psk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
, NULL
,
2099 os_memcpy(sm
->PMK
, psk
, psk_len
);
2100 sm
->pmk_len
= psk_len
;
2101 #ifdef CONFIG_IEEE80211R_AP
2102 os_memcpy(sm
->xxkey
, psk
, PMK_LEN
);
2103 sm
->xxkey_len
= PMK_LEN
;
2104 #endif /* CONFIG_IEEE80211R_AP */
2107 if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
2108 wpa_printf(MSG_DEBUG
, "SAE: PMK from PMKSA cache");
2109 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2110 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
2111 #ifdef CONFIG_IEEE80211R_AP
2112 os_memcpy(sm
->xxkey
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2113 sm
->xxkey_len
= sm
->pmksa
->pmk_len
;
2114 #endif /* CONFIG_IEEE80211R_AP */
2116 #endif /* CONFIG_SAE */
2117 sm
->req_replay_counter_used
= 0;
2121 SM_STATE(WPA_PTK
, PTKSTART
)
2123 u8 buf
[2 + RSN_SELECTOR_LEN
+ PMKID_LEN
], *pmkid
= NULL
;
2124 size_t pmkid_len
= 0;
2126 SM_ENTRY_MA(WPA_PTK
, PTKSTART
, wpa_ptk
);
2127 sm
->PTKRequest
= FALSE
;
2128 sm
->TimeoutEvt
= FALSE
;
2129 sm
->alt_snonce_valid
= FALSE
;
2132 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
2133 /* No point in sending the EAPOL-Key - we will disconnect
2134 * immediately following this. */
2138 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2139 "sending 1/4 msg of 4-Way Handshake");
2141 * For infrastructure BSS cases, it is better for the AP not to include
2142 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2143 * offline search for the passphrase/PSK without having to be able to
2144 * capture a 4-way handshake from a STA that has access to the network.
2146 * For IBSS cases, addition of PMKID KDE could be considered even with
2147 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2148 * possible PSK for this STA. However, this should not be done unless
2149 * there is support for using that information on the supplicant side.
2150 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2151 * cases would also apply here, but at least in the IBSS case, this
2152 * would cover a potential real use case.
2154 if (sm
->wpa
== WPA_VERSION_WPA2
&&
2155 (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) ||
2156 (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
&& sm
->pmksa
) ||
2157 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) &&
2158 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
) {
2160 pmkid_len
= 2 + RSN_SELECTOR_LEN
+ PMKID_LEN
;
2161 pmkid
[0] = WLAN_EID_VENDOR_SPECIFIC
;
2162 pmkid
[1] = RSN_SELECTOR_LEN
+ PMKID_LEN
;
2163 RSN_SELECTOR_PUT(&pmkid
[2], RSN_KEY_DATA_PMKID
);
2165 wpa_hexdump(MSG_DEBUG
,
2166 "RSN: Message 1/4 PMKID from PMKSA entry",
2167 sm
->pmksa
->pmkid
, PMKID_LEN
);
2168 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2169 sm
->pmksa
->pmkid
, PMKID_LEN
);
2170 } else if (wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
)) {
2171 /* No KCK available to derive PMKID */
2172 wpa_printf(MSG_DEBUG
,
2173 "RSN: No KCK available to derive PMKID for message 1/4");
2176 } else if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2177 if (sm
->pmkid_set
) {
2178 wpa_hexdump(MSG_DEBUG
,
2179 "RSN: Message 1/4 PMKID from FILS/ERP",
2180 sm
->pmkid
, PMKID_LEN
);
2181 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2182 sm
->pmkid
, PMKID_LEN
);
2184 /* No PMKID available */
2185 wpa_printf(MSG_DEBUG
,
2186 "RSN: No FILS/ERP PMKID available for message 1/4");
2189 #endif /* CONFIG_FILS */
2190 #ifdef CONFIG_IEEE80211R_AP
2191 } else if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
) &&
2193 wpa_printf(MSG_DEBUG
,
2194 "FT: No PMKID in message 1/4 when using FT protocol");
2196 #endif /* CONFIG_IEEE80211R_AP */
2198 } else if (wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
2199 if (sm
->pmkid_set
) {
2200 wpa_hexdump(MSG_DEBUG
,
2201 "RSN: Message 1/4 PMKID from SAE",
2202 sm
->pmkid
, PMKID_LEN
);
2203 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2204 sm
->pmkid
, PMKID_LEN
);
2206 /* No PMKID available */
2207 wpa_printf(MSG_DEBUG
,
2208 "RSN: No SAE PMKID available for message 1/4");
2211 #endif /* CONFIG_SAE */
2214 * Calculate PMKID since no PMKSA cache entry was
2215 * available with pre-calculated PMKID.
2217 rsn_pmkid(sm
->PMK
, sm
->pmk_len
, sm
->wpa_auth
->addr
,
2218 sm
->addr
, &pmkid
[2 + RSN_SELECTOR_LEN
],
2220 wpa_hexdump(MSG_DEBUG
,
2221 "RSN: Message 1/4 PMKID derived from PMK",
2222 &pmkid
[2 + RSN_SELECTOR_LEN
], PMKID_LEN
);
2227 wpa_send_eapol(sm
->wpa_auth
, sm
,
2228 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
2229 sm
->ANonce
, pmkid
, pmkid_len
, 0, 0);
2233 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
2234 const u8
*pmk
, unsigned int pmk_len
,
2235 struct wpa_ptk
*ptk
)
2240 #ifdef CONFIG_IEEE80211R_AP
2241 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2242 if (sm
->ft_completed
) {
2243 u8 ptk_name
[WPA_PMK_NAME_LEN
];
2245 return wpa_pmk_r1_to_ptk(sm
->pmk_r1
, sm
->pmk_r1_len
,
2246 sm
->SNonce
, sm
->ANonce
,
2247 sm
->addr
, sm
->wpa_auth
->addr
,
2253 return wpa_auth_derive_ptk_ft(sm
, ptk
);
2255 #endif /* CONFIG_IEEE80211R_AP */
2258 if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->dpp_z
) {
2259 z
= wpabuf_head(sm
->dpp_z
);
2260 z_len
= wpabuf_len(sm
->dpp_z
);
2262 #endif /* CONFIG_DPP2 */
2264 return wpa_pmk_to_ptk(pmk
, pmk_len
, "Pairwise key expansion",
2265 sm
->wpa_auth
->addr
, sm
->addr
, sm
->ANonce
, snonce
,
2266 ptk
, sm
->wpa_key_mgmt
, sm
->pairwise
, z
, z_len
);
2272 int fils_auth_pmk_to_ptk(struct wpa_state_machine
*sm
, const u8
*pmk
,
2273 size_t pmk_len
, const u8
*snonce
, const u8
*anonce
,
2274 const u8
*dhss
, size_t dhss_len
,
2275 struct wpabuf
*g_sta
, struct wpabuf
*g_ap
)
2277 u8 ick
[FILS_ICK_MAX_LEN
];
2280 u8 fils_ft
[FILS_FT_MAX_LEN
];
2281 size_t fils_ft_len
= 0;
2283 res
= fils_pmk_to_ptk(pmk
, pmk_len
, sm
->addr
, sm
->wpa_auth
->addr
,
2284 snonce
, anonce
, dhss
, dhss_len
,
2285 &sm
->PTK
, ick
, &ick_len
,
2286 sm
->wpa_key_mgmt
, sm
->pairwise
,
2287 fils_ft
, &fils_ft_len
);
2290 sm
->PTK_valid
= TRUE
;
2291 sm
->tk_already_set
= FALSE
;
2293 #ifdef CONFIG_IEEE80211R_AP
2295 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2296 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
2297 u8 pmk_r0
[PMK_LEN_MAX
], pmk_r0_name
[WPA_PMK_NAME_LEN
];
2298 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
2299 size_t pmk_r0_len
= use_sha384
? SHA384_MAC_LEN
: PMK_LEN
;
2301 if (wpa_derive_pmk_r0(fils_ft
, fils_ft_len
,
2302 conf
->ssid
, conf
->ssid_len
,
2303 conf
->mobility_domain
,
2304 conf
->r0_key_holder
,
2305 conf
->r0_key_holder_len
,
2306 sm
->addr
, pmk_r0
, pmk_r0_name
,
2310 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: PMK-R0",
2311 pmk_r0
, pmk_r0_len
);
2312 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR0Name",
2313 pmk_r0_name
, WPA_PMK_NAME_LEN
);
2314 wpa_ft_store_pmk_fils(sm
, pmk_r0
, pmk_r0_name
);
2315 forced_memzero(fils_ft
, sizeof(fils_ft
));
2317 res
= wpa_derive_pmk_r1_name(pmk_r0_name
, conf
->r1_key_holder
,
2318 sm
->addr
, sm
->pmk_r1_name
,
2320 forced_memzero(pmk_r0
, PMK_LEN_MAX
);
2323 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR1Name", sm
->pmk_r1_name
,
2325 sm
->pmk_r1_name_valid
= 1;
2327 #endif /* CONFIG_IEEE80211R_AP */
2329 res
= fils_key_auth_sk(ick
, ick_len
, snonce
, anonce
,
2330 sm
->addr
, sm
->wpa_auth
->addr
,
2331 g_sta
? wpabuf_head(g_sta
) : NULL
,
2332 g_sta
? wpabuf_len(g_sta
) : 0,
2333 g_ap
? wpabuf_head(g_ap
) : NULL
,
2334 g_ap
? wpabuf_len(g_ap
) : 0,
2335 sm
->wpa_key_mgmt
, sm
->fils_key_auth_sta
,
2336 sm
->fils_key_auth_ap
,
2337 &sm
->fils_key_auth_len
);
2338 forced_memzero(ick
, sizeof(ick
));
2340 /* Store nonces for (Re)Association Request/Response frame processing */
2341 os_memcpy(sm
->SNonce
, snonce
, FILS_NONCE_LEN
);
2342 os_memcpy(sm
->ANonce
, anonce
, FILS_NONCE_LEN
);
2348 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
2349 u8
*buf
, size_t buf_len
, u16
*_key_data_len
)
2351 struct ieee802_1x_hdr
*hdr
;
2352 struct wpa_eapol_key
*key
;
2359 hdr
= (struct ieee802_1x_hdr
*) buf
;
2360 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2361 pos
= (u8
*) (key
+ 1);
2362 key_data_len
= WPA_GET_BE16(pos
);
2363 if (key_data_len
< AES_BLOCK_SIZE
||
2364 key_data_len
> buf_len
- sizeof(*hdr
) - sizeof(*key
) - 2) {
2365 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2366 "No room for AES-SIV data in the frame");
2369 pos
+= 2; /* Pointing at the Encrypted Key Data field */
2371 tmp
= os_malloc(key_data_len
);
2375 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2376 * to Key Data (exclusive). */
2378 aad_len
[0] = pos
- buf
;
2379 if (aes_siv_decrypt(ptk
->kek
, ptk
->kek_len
, pos
, key_data_len
,
2380 1, aad
, aad_len
, tmp
) < 0) {
2381 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2382 "Invalid AES-SIV data in the frame");
2383 bin_clear_free(tmp
, key_data_len
);
2387 /* AEAD decryption and validation completed successfully */
2388 key_data_len
-= AES_BLOCK_SIZE
;
2389 wpa_hexdump_key(MSG_DEBUG
, "WPA: Decrypted Key Data",
2392 /* Replace Key Data field with the decrypted version */
2393 os_memcpy(pos
, tmp
, key_data_len
);
2394 pos
-= 2; /* Key Data Length field */
2395 WPA_PUT_BE16(pos
, key_data_len
);
2396 bin_clear_free(tmp
, key_data_len
);
2398 *_key_data_len
= key_data_len
;
2403 const u8
* wpa_fils_validate_fils_session(struct wpa_state_machine
*sm
,
2404 const u8
*ies
, size_t ies_len
,
2405 const u8
*fils_session
)
2408 const u8
*session
= NULL
;
2410 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2411 wpa_printf(MSG_DEBUG
,
2412 "FILS: Not a FILS AKM - reject association");
2416 /* Verify Session element */
2418 end
= ((const u8
*) ie
) + ies_len
;
2419 while (ie
+ 1 < end
) {
2420 if (ie
+ 2 + ie
[1] > end
)
2422 if (ie
[0] == WLAN_EID_EXTENSION
&&
2423 ie
[1] >= 1 + FILS_SESSION_LEN
&&
2424 ie
[2] == WLAN_EID_EXT_FILS_SESSION
) {
2432 wpa_printf(MSG_DEBUG
,
2433 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2438 if (!fils_session
) {
2439 wpa_printf(MSG_DEBUG
,
2440 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2445 if (os_memcmp(fils_session
, session
+ 3, FILS_SESSION_LEN
) != 0) {
2446 wpa_printf(MSG_DEBUG
, "FILS: Session mismatch");
2447 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
2448 fils_session
, FILS_SESSION_LEN
);
2449 wpa_hexdump(MSG_DEBUG
, "FILS: Received FILS Session",
2450 session
+ 3, FILS_SESSION_LEN
);
2457 int wpa_fils_validate_key_confirm(struct wpa_state_machine
*sm
, const u8
*ies
,
2460 struct ieee802_11_elems elems
;
2462 if (ieee802_11_parse_elems(ies
, ies_len
, &elems
, 1) == ParseFailed
) {
2463 wpa_printf(MSG_DEBUG
,
2464 "FILS: Failed to parse decrypted elements");
2468 if (!elems
.fils_session
) {
2469 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
2473 if (!elems
.fils_key_confirm
) {
2474 wpa_printf(MSG_DEBUG
, "FILS: No FILS Key Confirm element");
2478 if (elems
.fils_key_confirm_len
!= sm
->fils_key_auth_len
) {
2479 wpa_printf(MSG_DEBUG
,
2480 "FILS: Unexpected Key-Auth length %d (expected %d)",
2481 elems
.fils_key_confirm_len
,
2482 (int) sm
->fils_key_auth_len
);
2486 if (os_memcmp(elems
.fils_key_confirm
, sm
->fils_key_auth_sta
,
2487 sm
->fils_key_auth_len
) != 0) {
2488 wpa_printf(MSG_DEBUG
, "FILS: Key-Auth mismatch");
2489 wpa_hexdump(MSG_DEBUG
, "FILS: Received Key-Auth",
2490 elems
.fils_key_confirm
, elems
.fils_key_confirm_len
);
2491 wpa_hexdump(MSG_DEBUG
, "FILS: Expected Key-Auth",
2492 sm
->fils_key_auth_sta
, sm
->fils_key_auth_len
);
2500 int fils_decrypt_assoc(struct wpa_state_machine
*sm
, const u8
*fils_session
,
2501 const struct ieee80211_mgmt
*mgmt
, size_t frame_len
,
2502 u8
*pos
, size_t left
)
2505 const u8
*end
, *ie_start
, *ie
, *session
, *crypt
;
2509 if (!sm
|| !sm
->PTK_valid
) {
2510 wpa_printf(MSG_DEBUG
,
2511 "FILS: No KEK to decrypt Assocication Request frame");
2515 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2516 wpa_printf(MSG_DEBUG
,
2517 "FILS: Not a FILS AKM - reject association");
2521 end
= ((const u8
*) mgmt
) + frame_len
;
2522 fc
= le_to_host16(mgmt
->frame_control
);
2523 stype
= WLAN_FC_GET_STYPE(fc
);
2524 if (stype
== WLAN_FC_STYPE_REASSOC_REQ
)
2525 ie_start
= mgmt
->u
.reassoc_req
.variable
;
2527 ie_start
= mgmt
->u
.assoc_req
.variable
;
2531 * Find FILS Session element which is the last unencrypted element in
2534 session
= wpa_fils_validate_fils_session(sm
, ie
, end
- ie
,
2537 wpa_printf(MSG_DEBUG
, "FILS: Session validation failed");
2541 crypt
= session
+ 2 + session
[1];
2543 if (end
- crypt
< AES_BLOCK_SIZE
) {
2544 wpa_printf(MSG_DEBUG
,
2545 "FILS: Too short frame to include AES-SIV data");
2549 /* AES-SIV AAD vectors */
2551 /* The STA's MAC address */
2553 aad_len
[0] = ETH_ALEN
;
2554 /* The AP's BSSID */
2556 aad_len
[1] = ETH_ALEN
;
2557 /* The STA's nonce */
2558 aad
[2] = sm
->SNonce
;
2559 aad_len
[2] = FILS_NONCE_LEN
;
2560 /* The AP's nonce */
2561 aad
[3] = sm
->ANonce
;
2562 aad_len
[3] = FILS_NONCE_LEN
;
2564 * The (Re)Association Request frame from the Capability Information
2565 * field to the FILS Session element (both inclusive).
2567 aad
[4] = (const u8
*) &mgmt
->u
.assoc_req
.capab_info
;
2568 aad_len
[4] = crypt
- aad
[4];
2570 if (aes_siv_decrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, crypt
, end
- crypt
,
2571 5, aad
, aad_len
, pos
+ (crypt
- ie_start
)) < 0) {
2572 wpa_printf(MSG_DEBUG
,
2573 "FILS: Invalid AES-SIV data in the frame");
2576 wpa_hexdump(MSG_DEBUG
, "FILS: Decrypted Association Request elements",
2577 pos
, left
- AES_BLOCK_SIZE
);
2579 if (wpa_fils_validate_key_confirm(sm
, pos
, left
- AES_BLOCK_SIZE
) < 0) {
2580 wpa_printf(MSG_DEBUG
, "FILS: Key Confirm validation failed");
2584 return left
- AES_BLOCK_SIZE
;
2588 int fils_encrypt_assoc(struct wpa_state_machine
*sm
, u8
*buf
,
2589 size_t current_len
, size_t max_len
,
2590 const struct wpabuf
*hlp
)
2592 u8
*end
= buf
+ max_len
;
2593 u8
*pos
= buf
+ current_len
;
2594 struct ieee80211_mgmt
*mgmt
;
2595 struct wpabuf
*plain
;
2599 if (!sm
|| !sm
->PTK_valid
)
2602 wpa_hexdump(MSG_DEBUG
,
2603 "FILS: Association Response frame before FILS processing",
2606 mgmt
= (struct ieee80211_mgmt
*) buf
;
2608 /* AES-SIV AAD vectors */
2610 /* The AP's BSSID */
2612 aad_len
[0] = ETH_ALEN
;
2613 /* The STA's MAC address */
2615 aad_len
[1] = ETH_ALEN
;
2616 /* The AP's nonce */
2617 aad
[2] = sm
->ANonce
;
2618 aad_len
[2] = FILS_NONCE_LEN
;
2619 /* The STA's nonce */
2620 aad
[3] = sm
->SNonce
;
2621 aad_len
[3] = FILS_NONCE_LEN
;
2623 * The (Re)Association Response frame from the Capability Information
2624 * field (the same offset in both Association and Reassociation
2625 * Response frames) to the FILS Session element (both inclusive).
2627 aad
[4] = (const u8
*) &mgmt
->u
.assoc_resp
.capab_info
;
2628 aad_len
[4] = pos
- aad
[4];
2630 /* The following elements will be encrypted with AES-SIV */
2631 plain
= fils_prepare_plainbuf(sm
, hlp
);
2633 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2637 if (pos
+ wpabuf_len(plain
) + AES_BLOCK_SIZE
> end
) {
2638 wpa_printf(MSG_DEBUG
,
2639 "FILS: Not enough room for FILS elements");
2640 wpabuf_clear_free(plain
);
2644 wpa_hexdump_buf_key(MSG_DEBUG
, "FILS: Association Response plaintext",
2647 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
2648 wpabuf_head(plain
), wpabuf_len(plain
),
2649 5, aad
, aad_len
, pos
) < 0) {
2650 wpabuf_clear_free(plain
);
2654 wpa_hexdump(MSG_DEBUG
,
2655 "FILS: Encrypted Association Response elements",
2656 pos
, AES_BLOCK_SIZE
+ wpabuf_len(plain
));
2657 current_len
+= wpabuf_len(plain
) + AES_BLOCK_SIZE
;
2658 wpabuf_clear_free(plain
);
2660 sm
->fils_completed
= 1;
2666 static struct wpabuf
* fils_prepare_plainbuf(struct wpa_state_machine
*sm
,
2667 const struct wpabuf
*hlp
)
2669 struct wpabuf
*plain
;
2670 u8
*len
, *tmp
, *tmp2
;
2672 u8
*gtk
, dummy_gtk
[32];
2674 struct wpa_group
*gsm
;
2676 plain
= wpabuf_alloc(1000);
2680 /* TODO: FILS Public Key */
2682 /* FILS Key Confirmation */
2683 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2684 wpabuf_put_u8(plain
, 1 + sm
->fils_key_auth_len
); /* Length */
2685 /* Element ID Extension */
2686 wpabuf_put_u8(plain
, WLAN_EID_EXT_FILS_KEY_CONFIRM
);
2687 wpabuf_put_data(plain
, sm
->fils_key_auth_ap
, sm
->fils_key_auth_len
);
2689 /* FILS HLP Container */
2691 wpabuf_put_buf(plain
, hlp
);
2693 /* TODO: FILS IP Address Assignment */
2697 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2698 len
= wpabuf_put(plain
, 1);
2699 wpabuf_put_u8(plain
, WLAN_EID_EXT_KEY_DELIVERY
);
2700 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
,
2701 wpabuf_put(plain
, WPA_KEY_RSC_LEN
));
2703 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2704 gtk_len
= gsm
->GTK_len
;
2705 if (sm
->wpa_auth
->conf
.disable_gtk
||
2706 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
2708 * Provide unique random GTK to each STA to prevent use
2709 * of GTK in the BSS.
2711 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0) {
2712 wpabuf_clear_free(plain
);
2717 hdr
[0] = gsm
->GN
& 0x03;
2719 tmp
= wpabuf_put(plain
, 0);
2720 tmp2
= wpa_add_kde(tmp
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
2722 wpabuf_put(plain
, tmp2
- tmp
);
2725 tmp
= wpabuf_put(plain
, 0);
2726 tmp2
= ieee80211w_kde_add(sm
, tmp
);
2727 wpabuf_put(plain
, tmp2
- tmp
);
2729 *len
= (u8
*) wpabuf_put(plain
, 0) - len
- 1;
2732 if (wpa_auth_uses_ocv(sm
)) {
2733 struct wpa_channel_info ci
;
2736 if (wpa_channel_info(sm
->wpa_auth
, &ci
) != 0) {
2737 wpa_printf(MSG_WARNING
,
2738 "FILS: Failed to get channel info for OCI element");
2739 wpabuf_clear_free(plain
);
2743 pos
= wpabuf_put(plain
, OCV_OCI_EXTENDED_LEN
);
2744 if (ocv_insert_extended_oci(&ci
, pos
) < 0) {
2745 wpabuf_clear_free(plain
);
2749 #endif /* CONFIG_OCV */
2755 int fils_set_tk(struct wpa_state_machine
*sm
)
2760 if (!sm
|| !sm
->PTK_valid
) {
2761 wpa_printf(MSG_DEBUG
, "FILS: No valid PTK available to set TK");
2764 if (sm
->tk_already_set
) {
2765 wpa_printf(MSG_DEBUG
, "FILS: TK already set to the driver");
2769 alg
= wpa_cipher_to_alg(sm
->pairwise
);
2770 klen
= wpa_cipher_key_len(sm
->pairwise
);
2772 wpa_printf(MSG_DEBUG
, "FILS: Configure TK to the driver");
2773 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
2774 sm
->PTK
.tk
, klen
)) {
2775 wpa_printf(MSG_DEBUG
, "FILS: Failed to set TK to the driver");
2778 sm
->tk_already_set
= TRUE
;
2784 u8
* hostapd_eid_assoc_fils_session(struct wpa_state_machine
*sm
, u8
*buf
,
2785 const u8
*fils_session
, struct wpabuf
*hlp
)
2787 struct wpabuf
*plain
;
2791 *pos
++ = WLAN_EID_EXTENSION
; /* Element ID */
2792 *pos
++ = 1 + FILS_SESSION_LEN
; /* Length */
2793 *pos
++ = WLAN_EID_EXT_FILS_SESSION
; /* Element ID Extension */
2794 os_memcpy(pos
, fils_session
, FILS_SESSION_LEN
);
2795 pos
+= FILS_SESSION_LEN
;
2797 plain
= fils_prepare_plainbuf(sm
, hlp
);
2799 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2803 os_memcpy(pos
, wpabuf_head(plain
), wpabuf_len(plain
));
2804 pos
+= wpabuf_len(plain
);
2806 wpa_printf(MSG_DEBUG
, "%s: plain buf_len: %u", __func__
,
2807 (unsigned int) wpabuf_len(plain
));
2808 wpabuf_clear_free(plain
);
2809 sm
->fils_completed
= 1;
2813 #endif /* CONFIG_FILS */
2817 int get_sta_tx_parameters(struct wpa_state_machine
*sm
, int ap_max_chanwidth
,
2818 int ap_seg1_idx
, int *bandwidth
, int *seg1_idx
)
2820 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2822 if (!wpa_auth
->cb
->get_sta_tx_params
)
2824 return wpa_auth
->cb
->get_sta_tx_params(wpa_auth
->cb_ctx
, sm
->addr
,
2825 ap_max_chanwidth
, ap_seg1_idx
,
2826 bandwidth
, seg1_idx
);
2828 #endif /* CONFIG_OCV */
2831 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING
)
2833 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2835 int ok
= 0, psk_found
= 0;
2836 const u8
*pmk
= NULL
;
2839 const u8
*eapol_key_ie
, *key_data
, *mic
;
2840 u16 key_data_length
;
2841 size_t mic_len
, eapol_key_ie_len
;
2842 struct ieee802_1x_hdr
*hdr
;
2843 struct wpa_eapol_key
*key
;
2844 struct wpa_eapol_ie_parse kde
;
2847 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING
, wpa_ptk
);
2848 sm
->EAPOLKeyReceived
= FALSE
;
2849 sm
->update_snonce
= FALSE
;
2850 os_memset(&PTK
, 0, sizeof(PTK
));
2852 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
2854 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2855 * WPA-PSK: iterate through possible PSKs and select the one matching
2858 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
2859 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
2860 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
2861 sm
->p2p_dev_addr
, pmk
, &pmk_len
,
2866 #ifdef CONFIG_IEEE80211R_AP
2867 if (wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
2868 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
2869 sm
->xxkey_len
= pmk_len
;
2871 #endif /* CONFIG_IEEE80211R_AP */
2874 pmk_len
= sm
->pmk_len
;
2877 if ((!pmk
|| !pmk_len
) && sm
->pmksa
) {
2878 wpa_printf(MSG_DEBUG
, "WPA: Use PMK from PMKSA cache");
2879 pmk
= sm
->pmksa
->pmk
;
2880 pmk_len
= sm
->pmksa
->pmk_len
;
2883 if (wpa_derive_ptk(sm
, sm
->SNonce
, pmk
, pmk_len
, &PTK
) < 0)
2887 wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
2888 sm
->last_rx_eapol_key
,
2889 sm
->last_rx_eapol_key_len
) == 0) {
2890 if (sm
->PMK
!= pmk
) {
2891 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
2892 sm
->pmk_len
= pmk_len
;
2900 wpa_aead_decrypt(sm
, &PTK
, sm
->last_rx_eapol_key
,
2901 sm
->last_rx_eapol_key_len
, NULL
) == 0) {
2905 #endif /* CONFIG_FILS */
2907 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
2908 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
))
2913 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2914 "invalid MIC in msg 2/4 of 4-Way Handshake");
2916 wpa_auth_psk_failure_report(sm
->wpa_auth
, sm
->addr
);
2921 * Note: last_rx_eapol_key length fields have already been validated in
2924 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
2925 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2926 mic
= (u8
*) (key
+ 1);
2927 key_data
= mic
+ mic_len
+ 2;
2928 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
2929 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
2930 sizeof(*key
) - mic_len
- 2)
2933 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
2934 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2935 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2939 eapol_key_ie
= kde
.rsn_ie
;
2940 eapol_key_ie_len
= kde
.rsn_ie_len
;
2941 } else if (kde
.osen
) {
2942 eapol_key_ie
= kde
.osen
;
2943 eapol_key_ie_len
= kde
.osen_len
;
2945 eapol_key_ie
= kde
.wpa_ie
;
2946 eapol_key_ie_len
= kde
.wpa_ie_len
;
2948 ft
= sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
);
2949 if (sm
->wpa_ie
== NULL
||
2950 wpa_compare_rsn_ie(ft
, sm
->wpa_ie
, sm
->wpa_ie_len
,
2951 eapol_key_ie
, eapol_key_ie_len
)) {
2952 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2953 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2955 wpa_hexdump(MSG_DEBUG
, "WPA IE in AssocReq",
2956 sm
->wpa_ie
, sm
->wpa_ie_len
);
2958 wpa_hexdump(MSG_DEBUG
, "WPA IE in msg 2/4",
2959 eapol_key_ie
, eapol_key_ie_len
);
2960 /* MLME-DEAUTHENTICATE.request */
2961 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
2962 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2965 if ((!sm
->rsnxe
&& kde
.rsnxe
) ||
2966 (sm
->rsnxe
&& !kde
.rsnxe
) ||
2967 (sm
->rsnxe
&& kde
.rsnxe
&&
2968 (sm
->rsnxe_len
!= kde
.rsnxe_len
||
2969 os_memcmp(sm
->rsnxe
, kde
.rsnxe
, sm
->rsnxe_len
) != 0))) {
2970 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2971 "RSNXE from (Re)AssocReq did not match the one in EAPOL-Key msg 2/4");
2972 wpa_hexdump(MSG_DEBUG
, "RSNXE in AssocReq",
2973 sm
->rsnxe
, sm
->rsnxe_len
);
2974 wpa_hexdump(MSG_DEBUG
, "RSNXE in EAPOL-Key msg 2/4",
2975 kde
.rsnxe
, kde
.rsnxe_len
);
2976 /* MLME-DEAUTHENTICATE.request */
2977 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
2978 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2982 if (wpa_auth_uses_ocv(sm
)) {
2983 struct wpa_channel_info ci
;
2987 if (wpa_channel_info(wpa_auth
, &ci
) != 0) {
2988 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2989 "Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
2993 if (get_sta_tx_parameters(sm
,
2994 channel_width_to_int(ci
.chanwidth
),
2995 ci
.seg1_idx
, &tx_chanwidth
,
2999 if (ocv_verify_tx_params(kde
.oci
, kde
.oci_len
, &ci
,
3000 tx_chanwidth
, tx_seg1_idx
) != 0) {
3001 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3006 #endif /* CONFIG_OCV */
3007 #ifdef CONFIG_IEEE80211R_AP
3008 if (ft
&& ft_check_msg_2_of_4(wpa_auth
, sm
, &kde
) < 0) {
3009 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
3010 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3013 #endif /* CONFIG_IEEE80211R_AP */
3015 if (kde
.ip_addr_req
&& kde
.ip_addr_req
[0] &&
3016 wpa_auth
->ip_pool
&& WPA_GET_BE32(sm
->ip_addr
) == 0) {
3018 wpa_printf(MSG_DEBUG
,
3019 "P2P: IP address requested in EAPOL-Key exchange");
3020 idx
= bitfield_get_first_zero(wpa_auth
->ip_pool
);
3022 u32 start
= WPA_GET_BE32(wpa_auth
->conf
.ip_addr_start
);
3023 bitfield_set(wpa_auth
->ip_pool
, idx
);
3024 WPA_PUT_BE32(sm
->ip_addr
, start
+ idx
);
3025 wpa_printf(MSG_DEBUG
,
3026 "P2P: Assigned IP address %u.%u.%u.%u to "
3027 MACSTR
, sm
->ip_addr
[0], sm
->ip_addr
[1],
3028 sm
->ip_addr
[2], sm
->ip_addr
[3],
3032 #endif /* CONFIG_P2P */
3034 #ifdef CONFIG_IEEE80211R_AP
3035 if (sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3037 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
3038 * with the value we derived.
3040 if (os_memcmp_const(sm
->sup_pmk_r1_name
, sm
->pmk_r1_name
,
3041 WPA_PMK_NAME_LEN
) != 0) {
3042 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3043 "PMKR1Name mismatch in FT 4-way "
3045 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from "
3047 sm
->sup_pmk_r1_name
, WPA_PMK_NAME_LEN
);
3048 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
3049 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
3053 #endif /* CONFIG_IEEE80211R_AP */
3055 if (vlan_id
&& wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
3056 wpa_auth_update_vlan(wpa_auth
, sm
->addr
, vlan_id
) < 0) {
3057 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
3058 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3062 sm
->pending_1_of_4_timeout
= 0;
3063 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
3065 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
3066 /* PSK may have changed from the previous choice, so update
3067 * state machine data based on whatever PSK was selected here.
3069 os_memcpy(sm
->PMK
, pmk
, PMK_LEN
);
3070 sm
->pmk_len
= PMK_LEN
;
3073 sm
->MICVerified
= TRUE
;
3075 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
3076 forced_memzero(&PTK
, sizeof(PTK
));
3077 sm
->PTK_valid
= TRUE
;
3081 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING2
)
3083 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING2
, wpa_ptk
);
3088 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
3090 if (sm
->mgmt_frame_prot
) {
3092 len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3093 return 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
+ len
;
3100 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
3102 struct wpa_igtk_kde igtk
;
3103 struct wpa_group
*gsm
= sm
->group
;
3104 u8 rsc
[WPA_KEY_RSC_LEN
];
3105 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3107 if (!sm
->mgmt_frame_prot
)
3110 igtk
.keyid
[0] = gsm
->GN_igtk
;
3112 if (gsm
->wpa_group_state
!= WPA_GROUP_SETKEYSDONE
||
3113 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, rsc
) < 0)
3114 os_memset(igtk
.pn
, 0, sizeof(igtk
.pn
));
3116 os_memcpy(igtk
.pn
, rsc
, sizeof(igtk
.pn
));
3117 os_memcpy(igtk
.igtk
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3118 if (sm
->wpa_auth
->conf
.disable_gtk
||
3119 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3121 * Provide unique random IGTK to each STA to prevent use of
3124 if (random_get_bytes(igtk
.igtk
, len
) < 0)
3127 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_IGTK
,
3128 (const u8
*) &igtk
, WPA_IGTK_KDE_PREFIX_LEN
+ len
,
3135 static int ocv_oci_len(struct wpa_state_machine
*sm
)
3138 if (wpa_auth_uses_ocv(sm
))
3139 return OCV_OCI_KDE_LEN
;
3140 #endif /* CONFIG_OCV */
3144 static int ocv_oci_add(struct wpa_state_machine
*sm
, u8
**argpos
)
3147 struct wpa_channel_info ci
;
3149 if (!wpa_auth_uses_ocv(sm
))
3152 if (wpa_channel_info(sm
->wpa_auth
, &ci
) != 0) {
3153 wpa_printf(MSG_WARNING
,
3154 "Failed to get channel info for OCI element");
3158 return ocv_insert_oci_kde(&ci
, argpos
);
3159 #else /* CONFIG_OCV */
3161 #endif /* CONFIG_OCV */
3165 SM_STATE(WPA_PTK
, PTKINITNEGOTIATING
)
3167 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
= NULL
, *pos
, dummy_gtk
[32];
3168 size_t gtk_len
, kde_len
;
3169 struct wpa_group
*gsm
= sm
->group
;
3171 int wpa_ie_len
, secure
, gtkidx
, encr
= 0;
3172 u8
*wpa_ie_buf
= NULL
;
3174 SM_ENTRY_MA(WPA_PTK
, PTKINITNEGOTIATING
, wpa_ptk
);
3175 sm
->TimeoutEvt
= FALSE
;
3178 if (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3179 sm
->TimeoutCtr
> 1) {
3180 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
3183 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
3184 /* No point in sending the EAPOL-Key - we will disconnect
3185 * immediately following this. */
3189 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
3190 GTK[GN], IGTK, [FTIE], [TIE * 2])
3192 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3193 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3194 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
3195 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
3196 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
3197 if (sm
->wpa
== WPA_VERSION_WPA
&&
3198 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
3199 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
3200 /* WPA-only STA, remove RSN IE and possible MDIE */
3201 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
3202 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
3203 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
3204 wpa_ie_len
= wpa_ie
[1] + 2;
3206 #ifdef CONFIG_TESTING_OPTIONS
3207 if (sm
->wpa_auth
->conf
.rsnxe_override_eapol_len
) {
3208 u8
*obuf
= sm
->wpa_auth
->conf
.rsnxe_override_eapol
;
3209 size_t olen
= sm
->wpa_auth
->conf
.rsnxe_override_eapol_len
;
3212 wpa_hexdump(MSG_DEBUG
,
3213 "TESTING: wpa_ie before RSNXE EAPOL override",
3214 wpa_ie
, wpa_ie_len
);
3215 wpa_ie_buf
= os_malloc(wpa_ie_len
+ olen
);
3218 os_memcpy(wpa_ie_buf
, wpa_ie
, wpa_ie_len
);
3219 wpa_ie
= wpa_ie_buf
;
3220 rsnxe
= get_ie(wpa_ie
, wpa_ie_len
, WLAN_EID_RSNX
);
3222 u8 rsnxe_len
= 2 + rsnxe
[1];
3224 os_memmove((void *) rsnxe
, rsnxe
+ rsnxe_len
,
3225 wpa_ie_len
- (rsnxe
- wpa_ie
) - rsnxe_len
);
3226 wpa_ie_len
-= rsnxe_len
;
3228 os_memcpy(wpa_ie
+ wpa_ie_len
, obuf
, olen
);
3230 wpa_hexdump(MSG_DEBUG
,
3231 "TESTING: wpa_ie after RSNXE EAPOL override",
3232 wpa_ie
, wpa_ie_len
);
3234 #endif /* CONFIG_TESTING_OPTIONS */
3235 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3236 "sending 3/4 msg of 4-Way Handshake");
3237 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3238 /* WPA2 send GTK in the 4-way handshake */
3240 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3241 gtk_len
= gsm
->GTK_len
;
3242 if (sm
->wpa_auth
->conf
.disable_gtk
||
3243 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3245 * Provide unique random GTK to each STA to prevent use
3246 * of GTK in the BSS.
3248 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0)
3256 /* WPA does not include GTK in msg 3/4 */
3261 if (sm
->rx_eapol_key_secure
) {
3263 * It looks like Windows 7 supplicant tries to use
3264 * Secure bit in msg 2/4 after having reported Michael
3265 * MIC failure and it then rejects the 4-way handshake
3266 * if msg 3/4 does not set Secure bit. Work around this
3267 * by setting the Secure bit here even in the case of
3268 * WPA if the supplicant used it first.
3270 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3271 "STA used Secure bit in WPA msg 2/4 - "
3272 "set Secure for 3/4 as workaround");
3277 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
3279 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
3280 #ifdef CONFIG_IEEE80211R_AP
3281 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3282 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
3283 kde_len
+= 300; /* FTIE + 2 * TIE */
3285 #endif /* CONFIG_IEEE80211R_AP */
3287 if (WPA_GET_BE32(sm
->ip_addr
) > 0)
3288 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 3 * 4;
3289 #endif /* CONFIG_P2P */
3290 kde
= os_malloc(kde_len
);
3295 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
3297 #ifdef CONFIG_IEEE80211R_AP
3298 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3303 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
3305 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
3306 "PMKR1Name into RSN IE in EAPOL-Key data");
3312 #endif /* CONFIG_IEEE80211R_AP */
3315 hdr
[0] = gtkidx
& 0x03;
3317 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3320 pos
= ieee80211w_kde_add(sm
, pos
);
3321 if (ocv_oci_add(sm
, &pos
) < 0)
3324 #ifdef CONFIG_IEEE80211R_AP
3325 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3327 struct wpa_auth_config
*conf
;
3329 conf
= &sm
->wpa_auth
->conf
;
3330 if (sm
->assoc_resp_ftie
&&
3331 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
3332 os_memcpy(pos
, sm
->assoc_resp_ftie
,
3333 2 + sm
->assoc_resp_ftie
[1]);
3334 res
= 2 + sm
->assoc_resp_ftie
[1];
3336 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
3338 res
= wpa_write_ftie(conf
, use_sha384
,
3339 conf
->r0_key_holder
,
3340 conf
->r0_key_holder_len
,
3342 kde
+ kde_len
- pos
,
3346 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
3347 "into EAPOL-Key Key Data");
3352 /* TIE[ReassociationDeadline] (TU) */
3353 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3355 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
3356 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
3359 /* TIE[KeyLifetime] (seconds) */
3360 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3362 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
3363 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
);
3366 #endif /* CONFIG_IEEE80211R_AP */
3368 if (WPA_GET_BE32(sm
->ip_addr
) > 0) {
3370 os_memcpy(addr
, sm
->ip_addr
, 4);
3371 os_memcpy(addr
+ 4, sm
->wpa_auth
->conf
.ip_addr_mask
, 4);
3372 os_memcpy(addr
+ 8, sm
->wpa_auth
->conf
.ip_addr_go
, 4);
3373 pos
= wpa_add_kde(pos
, WFA_KEY_DATA_IP_ADDR_ALLOC
,
3374 addr
, sizeof(addr
), NULL
, 0);
3376 #endif /* CONFIG_P2P */
3378 wpa_send_eapol(sm
->wpa_auth
, sm
,
3379 (secure
? WPA_KEY_INFO_SECURE
: 0) |
3380 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3381 WPA_KEY_INFO_MIC
: 0) |
3382 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
3383 WPA_KEY_INFO_KEY_TYPE
,
3384 _rsc
, sm
->ANonce
, kde
, pos
- kde
, 0, encr
);
3387 os_free(wpa_ie_buf
);
3391 SM_STATE(WPA_PTK
, PTKINITDONE
)
3393 SM_ENTRY_MA(WPA_PTK
, PTKINITDONE
, wpa_ptk
);
3394 sm
->EAPOLKeyReceived
= FALSE
;
3396 enum wpa_alg alg
= wpa_cipher_to_alg(sm
->pairwise
);
3397 int klen
= wpa_cipher_key_len(sm
->pairwise
);
3398 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
3399 sm
->PTK
.tk
, klen
)) {
3400 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
,
3401 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3404 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3405 sm
->pairwise_set
= TRUE
;
3407 wpa_auth_set_ptk_rekey_timer(sm
);
3409 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3410 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
3411 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
3412 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3413 WPA_EAPOL_authorized
, 1);
3417 if (0 /* IBSS == TRUE */) {
3419 if (sm
->keycount
== 2) {
3420 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3421 WPA_EAPOL_portValid
, 1);
3424 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
,
3427 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyAvailable
, 0);
3428 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyDone
, 1);
3429 if (sm
->wpa
== WPA_VERSION_WPA
)
3430 sm
->PInitAKeys
= TRUE
;
3433 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3434 "pairwise key handshake completed (%s)",
3435 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3437 #ifdef CONFIG_IEEE80211R_AP
3438 wpa_ft_push_pmk_r1(sm
->wpa_auth
, sm
->addr
);
3439 #endif /* CONFIG_IEEE80211R_AP */
3445 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3448 SM_ENTER(WPA_PTK
, INITIALIZE
);
3449 else if (sm
->Disconnect
3450 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3451 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3452 "WPA_PTK: sm->Disconnect");
3453 SM_ENTER(WPA_PTK
, DISCONNECT
);
3455 else if (sm
->DeauthenticationRequest
)
3456 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3457 else if (sm
->AuthenticationRequest
)
3458 SM_ENTER(WPA_PTK
, AUTHENTICATION
);
3459 else if (sm
->ReAuthenticationRequest
)
3460 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3461 else if (sm
->PTKRequest
) {
3462 if (wpa_auth_sm_ptk_update(sm
) < 0)
3463 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3465 SM_ENTER(WPA_PTK
, PTKSTART
);
3466 } else switch (sm
->wpa_ptk_state
) {
3467 case WPA_PTK_INITIALIZE
:
3469 case WPA_PTK_DISCONNECT
:
3470 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3472 case WPA_PTK_DISCONNECTED
:
3473 SM_ENTER(WPA_PTK
, INITIALIZE
);
3475 case WPA_PTK_AUTHENTICATION
:
3476 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3478 case WPA_PTK_AUTHENTICATION2
:
3479 if (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
3480 wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
3481 WPA_EAPOL_keyRun
) > 0)
3482 SM_ENTER(WPA_PTK
, INITPMK
);
3483 else if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3484 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
3485 /* FIX: && 802.1X::keyRun */)
3486 SM_ENTER(WPA_PTK
, INITPSK
);
3487 else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
)
3488 SM_ENTER(WPA_PTK
, INITPMK
);
3490 case WPA_PTK_INITPMK
:
3491 if (wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
3492 WPA_EAPOL_keyAvailable
) > 0) {
3493 SM_ENTER(WPA_PTK
, PTKSTART
);
3495 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->pmksa
) {
3496 SM_ENTER(WPA_PTK
, PTKSTART
);
3497 #endif /* CONFIG_DPP */
3499 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3500 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3501 "INITPMK - keyAvailable = false");
3502 SM_ENTER(WPA_PTK
, DISCONNECT
);
3505 case WPA_PTK_INITPSK
:
3506 if (wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
,
3507 NULL
, NULL
, NULL
)) {
3508 SM_ENTER(WPA_PTK
, PTKSTART
);
3510 } else if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
3511 SM_ENTER(WPA_PTK
, PTKSTART
);
3512 #endif /* CONFIG_SAE */
3514 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3515 "no PSK configured for the STA");
3516 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3517 SM_ENTER(WPA_PTK
, DISCONNECT
);
3520 case WPA_PTK_PTKSTART
:
3521 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3522 sm
->EAPOLKeyPairwise
)
3523 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3524 else if (sm
->TimeoutCtr
>
3525 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
3526 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3528 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3529 "PTKSTART: Retry limit %u reached",
3530 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3531 SM_ENTER(WPA_PTK
, DISCONNECT
);
3532 } else if (sm
->TimeoutEvt
)
3533 SM_ENTER(WPA_PTK
, PTKSTART
);
3535 case WPA_PTK_PTKCALCNEGOTIATING
:
3536 if (sm
->MICVerified
)
3537 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING2
);
3538 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3539 sm
->EAPOLKeyPairwise
)
3540 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3541 else if (sm
->TimeoutEvt
)
3542 SM_ENTER(WPA_PTK
, PTKSTART
);
3544 case WPA_PTK_PTKCALCNEGOTIATING2
:
3545 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3547 case WPA_PTK_PTKINITNEGOTIATING
:
3548 if (sm
->update_snonce
)
3549 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3550 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3551 sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3552 SM_ENTER(WPA_PTK
, PTKINITDONE
);
3553 else if (sm
->TimeoutCtr
>
3554 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
||
3555 (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3556 sm
->TimeoutCtr
> 1)) {
3557 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3559 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3560 "PTKINITNEGOTIATING: Retry limit %u reached",
3561 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3562 SM_ENTER(WPA_PTK
, DISCONNECT
);
3563 } else if (sm
->TimeoutEvt
)
3564 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3566 case WPA_PTK_PTKINITDONE
:
3572 SM_STATE(WPA_PTK_GROUP
, IDLE
)
3574 SM_ENTRY_MA(WPA_PTK_GROUP
, IDLE
, wpa_ptk_group
);
3576 /* Init flag is not cleared here, so avoid busy
3577 * loop by claiming nothing changed. */
3578 sm
->changed
= FALSE
;
3580 sm
->GTimeoutCtr
= 0;
3584 SM_STATE(WPA_PTK_GROUP
, REKEYNEGOTIATING
)
3586 u8 rsc
[WPA_KEY_RSC_LEN
];
3587 struct wpa_group
*gsm
= sm
->group
;
3589 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
3591 u8
*gtk
, dummy_gtk
[32];
3593 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYNEGOTIATING
, wpa_ptk_group
);
3596 if (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3597 sm
->GTimeoutCtr
> 1) {
3598 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3601 if (sm
->GTimeoutCtr
> sm
->wpa_auth
->conf
.wpa_group_update_count
) {
3602 /* No point in sending the EAPOL-Key - we will disconnect
3603 * immediately following this. */
3607 if (sm
->wpa
== WPA_VERSION_WPA
)
3608 sm
->PInitAKeys
= FALSE
;
3609 sm
->TimeoutEvt
= FALSE
;
3610 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3611 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3612 if (gsm
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
)
3613 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3614 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3615 "sending 1/2 msg of Group Key Handshake");
3617 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3618 if (sm
->wpa_auth
->conf
.disable_gtk
||
3619 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3621 * Provide unique random GTK to each STA to prevent use
3622 * of GTK in the BSS.
3624 if (random_get_bytes(dummy_gtk
, gsm
->GTK_len
) < 0)
3628 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3629 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
3630 ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
3631 kde_buf
= os_malloc(kde_len
);
3632 if (kde_buf
== NULL
)
3635 kde
= pos
= kde_buf
;
3636 hdr
[0] = gsm
->GN
& 0x03;
3638 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3640 pos
= ieee80211w_kde_add(sm
, pos
);
3641 if (ocv_oci_add(sm
, &pos
) < 0) {
3645 kde_len
= pos
- kde
;
3648 kde_len
= gsm
->GTK_len
;
3651 wpa_send_eapol(sm
->wpa_auth
, sm
,
3652 WPA_KEY_INFO_SECURE
|
3653 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3654 WPA_KEY_INFO_MIC
: 0) |
3656 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
3657 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
3663 SM_STATE(WPA_PTK_GROUP
, REKEYESTABLISHED
)
3666 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3667 const u8
*key_data
, *mic
;
3668 struct ieee802_1x_hdr
*hdr
;
3669 struct wpa_eapol_key
*key
;
3670 struct wpa_eapol_ie_parse kde
;
3672 u16 key_data_length
;
3673 #endif /* CONFIG_OCV */
3675 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYESTABLISHED
, wpa_ptk_group
);
3676 sm
->EAPOLKeyReceived
= FALSE
;
3679 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
3682 * Note: last_rx_eapol_key length fields have already been validated in
3685 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
3686 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
3687 mic
= (u8
*) (key
+ 1);
3688 key_data
= mic
+ mic_len
+ 2;
3689 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
3690 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
3691 sizeof(*key
) - mic_len
- 2)
3694 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
3695 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3696 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3700 if (wpa_auth_uses_ocv(sm
)) {
3701 struct wpa_channel_info ci
;
3705 if (wpa_channel_info(wpa_auth
, &ci
) != 0) {
3706 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3707 "Failed to get channel info to validate received OCI in EAPOL-Key group 1/2");
3711 if (get_sta_tx_parameters(sm
,
3712 channel_width_to_int(ci
.chanwidth
),
3713 ci
.seg1_idx
, &tx_chanwidth
,
3717 if (ocv_verify_tx_params(kde
.oci
, kde
.oci_len
, &ci
,
3718 tx_chanwidth
, tx_seg1_idx
) != 0) {
3719 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3724 #endif /* CONFIG_OCV */
3726 if (sm
->GUpdateStationKeys
)
3727 sm
->group
->GKeyDoneStations
--;
3728 sm
->GUpdateStationKeys
= FALSE
;
3729 sm
->GTimeoutCtr
= 0;
3730 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3731 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3732 "group key handshake completed (%s)",
3733 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3738 SM_STATE(WPA_PTK_GROUP
, KEYERROR
)
3740 SM_ENTRY_MA(WPA_PTK_GROUP
, KEYERROR
, wpa_ptk_group
);
3741 if (sm
->GUpdateStationKeys
)
3742 sm
->group
->GKeyDoneStations
--;
3743 sm
->GUpdateStationKeys
= FALSE
;
3744 sm
->Disconnect
= TRUE
;
3745 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3746 "group key handshake failed (%s) after %u tries",
3747 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN",
3748 sm
->wpa_auth
->conf
.wpa_group_update_count
);
3752 SM_STEP(WPA_PTK_GROUP
)
3754 if (sm
->Init
|| sm
->PtkGroupInit
) {
3755 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3756 sm
->PtkGroupInit
= FALSE
;
3757 } else switch (sm
->wpa_ptk_group_state
) {
3758 case WPA_PTK_GROUP_IDLE
:
3759 if (sm
->GUpdateStationKeys
||
3760 (sm
->wpa
== WPA_VERSION_WPA
&& sm
->PInitAKeys
))
3761 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3763 case WPA_PTK_GROUP_REKEYNEGOTIATING
:
3764 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3765 !sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3766 SM_ENTER(WPA_PTK_GROUP
, REKEYESTABLISHED
);
3767 else if (sm
->GTimeoutCtr
>
3768 sm
->wpa_auth
->conf
.wpa_group_update_count
||
3769 (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3770 sm
->GTimeoutCtr
> 1))
3771 SM_ENTER(WPA_PTK_GROUP
, KEYERROR
);
3772 else if (sm
->TimeoutEvt
)
3773 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3775 case WPA_PTK_GROUP_KEYERROR
:
3776 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3778 case WPA_PTK_GROUP_REKEYESTABLISHED
:
3779 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3785 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
3786 struct wpa_group
*group
)
3790 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3791 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3792 if (wpa_gmk_to_gtk(group
->GMK
, "Group key expansion",
3793 wpa_auth
->addr
, group
->GNonce
,
3794 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3796 wpa_hexdump_key(MSG_DEBUG
, "GTK",
3797 group
->GTK
[group
->GN
- 1], group
->GTK_len
);
3799 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3801 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3802 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3803 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3804 if (wpa_gmk_to_gtk(group
->GMK
, "IGTK key expansion",
3805 wpa_auth
->addr
, group
->GNonce
,
3806 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3808 wpa_hexdump_key(MSG_DEBUG
, "IGTK",
3809 group
->IGTK
[group
->GN_igtk
- 4], len
);
3816 static void wpa_group_gtk_init(struct wpa_authenticator
*wpa_auth
,
3817 struct wpa_group
*group
)
3819 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3820 "GTK_INIT (VLAN-ID %d)", group
->vlan_id
);
3821 group
->changed
= FALSE
; /* GInit is not cleared here; avoid loop */
3822 group
->wpa_group_state
= WPA_GROUP_GTK_INIT
;
3825 os_memset(group
->GTK
, 0, sizeof(group
->GTK
));
3830 /* GTK[GN] = CalcGTK() */
3831 wpa_gtk_update(wpa_auth
, group
);
3835 static int wpa_group_update_sta(struct wpa_state_machine
*sm
, void *ctx
)
3837 if (ctx
!= NULL
&& ctx
!= sm
->group
)
3840 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITDONE
) {
3841 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3842 "Not in PTKINITDONE; skip Group Key update");
3843 sm
->GUpdateStationKeys
= FALSE
;
3846 if (sm
->GUpdateStationKeys
) {
3848 * This should not really happen, so add a debug log entry.
3849 * Since we clear the GKeyDoneStations before the loop, the
3850 * station needs to be counted here anyway.
3852 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3853 "GUpdateStationKeys was already set when "
3854 "marking station for GTK rekeying");
3857 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
3858 if (sm
->is_wnmsleep
)
3861 sm
->group
->GKeyDoneStations
++;
3862 sm
->GUpdateStationKeys
= TRUE
;
3869 #ifdef CONFIG_WNM_AP
3870 /* update GTK when exiting WNM-Sleep Mode */
3871 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine
*sm
)
3873 if (sm
== NULL
|| sm
->is_wnmsleep
)
3876 wpa_group_update_sta(sm
, NULL
);
3880 void wpa_set_wnmsleep(struct wpa_state_machine
*sm
, int flag
)
3883 sm
->is_wnmsleep
= !!flag
;
3887 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3889 struct wpa_group
*gsm
= sm
->group
;
3894 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
3897 *pos
++ = WNM_SLEEP_SUBELEM_GTK
;
3898 *pos
++ = 11 + gsm
->GTK_len
;
3899 /* Key ID in B0-B1 of Key Info */
3900 WPA_PUT_LE16(pos
, gsm
->GN
& 0x03);
3902 *pos
++ = gsm
->GTK_len
;
3903 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, pos
) != 0)
3906 os_memcpy(pos
, gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
3907 pos
+= gsm
->GTK_len
;
3909 wpa_printf(MSG_DEBUG
, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3911 wpa_hexdump_key(MSG_DEBUG
, "WNM: GTK in WNM-Sleep Mode exit",
3912 gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
3918 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3920 struct wpa_group
*gsm
= sm
->group
;
3922 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3926 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3928 *pos
++ = WNM_SLEEP_SUBELEM_IGTK
;
3929 *pos
++ = 2 + 6 + len
;
3930 WPA_PUT_LE16(pos
, gsm
->GN_igtk
);
3932 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, pos
) != 0)
3936 os_memcpy(pos
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3939 wpa_printf(MSG_DEBUG
, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3941 wpa_hexdump_key(MSG_DEBUG
, "WNM: IGTK in WNM-Sleep Mode exit",
3942 gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3947 #endif /* CONFIG_WNM_AP */
3950 static void wpa_group_setkeys(struct wpa_authenticator
*wpa_auth
,
3951 struct wpa_group
*group
)
3955 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3956 "SETKEYS (VLAN-ID %d)", group
->vlan_id
);
3957 group
->changed
= TRUE
;
3958 group
->wpa_group_state
= WPA_GROUP_SETKEYS
;
3959 group
->GTKReKey
= FALSE
;
3961 group
->GM
= group
->GN
;
3963 tmp
= group
->GM_igtk
;
3964 group
->GM_igtk
= group
->GN_igtk
;
3965 group
->GN_igtk
= tmp
;
3966 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3967 * counting the STAs that are marked with GUpdateStationKeys instead of
3968 * including all STAs that could be in not-yet-completed state. */
3969 wpa_gtk_update(wpa_auth
, group
);
3971 if (group
->GKeyDoneStations
) {
3972 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: Unexpected "
3973 "GKeyDoneStations=%d when starting new GTK rekey",
3974 group
->GKeyDoneStations
);
3975 group
->GKeyDoneStations
= 0;
3977 wpa_auth_for_each_sta(wpa_auth
, wpa_group_update_sta
, group
);
3978 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: GKeyDoneStations=%d",
3979 group
->GKeyDoneStations
);
3983 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
3984 struct wpa_group
*group
)
3988 if (wpa_auth_set_key(wpa_auth
, group
->vlan_id
,
3989 wpa_cipher_to_alg(wpa_auth
->conf
.wpa_group
),
3990 broadcast_ether_addr
, group
->GN
,
3991 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3994 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3998 alg
= wpa_cipher_to_alg(wpa_auth
->conf
.group_mgmt_cipher
);
3999 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
4002 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, alg
,
4003 broadcast_ether_addr
, group
->GN_igtk
,
4004 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
4012 static int wpa_group_disconnect_cb(struct wpa_state_machine
*sm
, void *ctx
)
4014 if (sm
->group
== ctx
) {
4015 wpa_printf(MSG_DEBUG
, "WPA: Mark STA " MACSTR
4016 " for discconnection due to fatal failure",
4018 sm
->Disconnect
= TRUE
;
4025 static void wpa_group_fatal_failure(struct wpa_authenticator
*wpa_auth
,
4026 struct wpa_group
*group
)
4028 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state FATAL_FAILURE");
4029 group
->changed
= TRUE
;
4030 group
->wpa_group_state
= WPA_GROUP_FATAL_FAILURE
;
4031 wpa_auth_for_each_sta(wpa_auth
, wpa_group_disconnect_cb
, group
);
4035 static int wpa_group_setkeysdone(struct wpa_authenticator
*wpa_auth
,
4036 struct wpa_group
*group
)
4038 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
4039 "SETKEYSDONE (VLAN-ID %d)", group
->vlan_id
);
4040 group
->changed
= TRUE
;
4041 group
->wpa_group_state
= WPA_GROUP_SETKEYSDONE
;
4043 if (wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
4044 wpa_group_fatal_failure(wpa_auth
, group
);
4052 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
4053 struct wpa_group
*group
)
4056 wpa_group_gtk_init(wpa_auth
, group
);
4057 } else if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
) {
4058 /* Do not allow group operations */
4059 } else if (group
->wpa_group_state
== WPA_GROUP_GTK_INIT
&&
4060 group
->GTKAuthenticator
) {
4061 wpa_group_setkeysdone(wpa_auth
, group
);
4062 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
&&
4064 wpa_group_setkeys(wpa_auth
, group
);
4065 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYS
) {
4066 if (group
->GKeyDoneStations
== 0)
4067 wpa_group_setkeysdone(wpa_auth
, group
);
4068 else if (group
->GTKReKey
)
4069 wpa_group_setkeys(wpa_auth
, group
);
4074 static int wpa_sm_step(struct wpa_state_machine
*sm
)
4079 if (sm
->in_step_loop
) {
4080 /* This should not happen, but if it does, make sure we do not
4081 * end up freeing the state machine too early by exiting the
4082 * recursive call. */
4083 wpa_printf(MSG_ERROR
, "WPA: wpa_sm_step() called recursively");
4087 sm
->in_step_loop
= 1;
4089 if (sm
->pending_deinit
)
4092 sm
->changed
= FALSE
;
4093 sm
->wpa_auth
->group
->changed
= FALSE
;
4095 SM_STEP_RUN(WPA_PTK
);
4096 if (sm
->pending_deinit
)
4098 SM_STEP_RUN(WPA_PTK_GROUP
);
4099 if (sm
->pending_deinit
)
4101 wpa_group_sm_step(sm
->wpa_auth
, sm
->group
);
4102 } while (sm
->changed
|| sm
->wpa_auth
->group
->changed
);
4103 sm
->in_step_loop
= 0;
4105 if (sm
->pending_deinit
) {
4106 wpa_printf(MSG_DEBUG
, "WPA: Completing pending STA state "
4107 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
4108 wpa_free_sta_sm(sm
);
4115 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
)
4117 struct wpa_state_machine
*sm
= eloop_ctx
;
4122 void wpa_auth_sm_notify(struct wpa_state_machine
*sm
)
4126 eloop_register_timeout(0, 0, wpa_sm_call_step
, sm
, NULL
);
4130 void wpa_gtk_rekey(struct wpa_authenticator
*wpa_auth
)
4133 struct wpa_group
*group
;
4135 if (wpa_auth
== NULL
)
4138 group
= wpa_auth
->group
;
4140 for (i
= 0; i
< 2; i
++) {
4142 group
->GM
= group
->GN
;
4144 tmp
= group
->GM_igtk
;
4145 group
->GM_igtk
= group
->GN_igtk
;
4146 group
->GN_igtk
= tmp
;
4147 wpa_gtk_update(wpa_auth
, group
);
4148 wpa_group_config_group_keys(wpa_auth
, group
);
4153 static const char * wpa_bool_txt(int val
)
4155 return val
? "TRUE" : "FALSE";
4159 #define RSN_SUITE "%02x-%02x-%02x-%d"
4160 #define RSN_SUITE_ARG(s) \
4161 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
4163 int wpa_get_mib(struct wpa_authenticator
*wpa_auth
, char *buf
, size_t buflen
)
4166 char pmkid_txt
[PMKID_LEN
* 2 + 1];
4167 #ifdef CONFIG_RSN_PREAUTH
4168 const int preauth
= 1;
4169 #else /* CONFIG_RSN_PREAUTH */
4170 const int preauth
= 0;
4171 #endif /* CONFIG_RSN_PREAUTH */
4173 if (wpa_auth
== NULL
)
4176 ret
= os_snprintf(buf
+ len
, buflen
- len
,
4177 "dot11RSNAOptionImplemented=TRUE\n"
4178 "dot11RSNAPreauthenticationImplemented=%s\n"
4179 "dot11RSNAEnabled=%s\n"
4180 "dot11RSNAPreauthenticationEnabled=%s\n",
4181 wpa_bool_txt(preauth
),
4182 wpa_bool_txt(wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
),
4183 wpa_bool_txt(wpa_auth
->conf
.rsn_preauth
));
4184 if (os_snprintf_error(buflen
- len
, ret
))
4188 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
4189 wpa_auth
->dot11RSNAPMKIDUsed
, PMKID_LEN
);
4192 buf
+ len
, buflen
- len
,
4193 "dot11RSNAConfigVersion=%u\n"
4194 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
4195 /* FIX: dot11RSNAConfigGroupCipher */
4196 /* FIX: dot11RSNAConfigGroupRekeyMethod */
4197 /* FIX: dot11RSNAConfigGroupRekeyTime */
4198 /* FIX: dot11RSNAConfigGroupRekeyPackets */
4199 "dot11RSNAConfigGroupRekeyStrict=%u\n"
4200 "dot11RSNAConfigGroupUpdateCount=%u\n"
4201 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
4202 "dot11RSNAConfigGroupCipherSize=%u\n"
4203 "dot11RSNAConfigPMKLifetime=%u\n"
4204 "dot11RSNAConfigPMKReauthThreshold=%u\n"
4205 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4206 "dot11RSNAConfigSATimeout=%u\n"
4207 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
4208 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
4209 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
4210 "dot11RSNAPMKIDUsed=%s\n"
4211 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
4212 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
4213 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
4214 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4215 "dot11RSNA4WayHandshakeFailures=%u\n"
4216 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4218 !!wpa_auth
->conf
.wpa_strict_rekey
,
4219 wpa_auth
->conf
.wpa_group_update_count
,
4220 wpa_auth
->conf
.wpa_pairwise_update_count
,
4221 wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
) * 8,
4222 dot11RSNAConfigPMKLifetime
,
4223 dot11RSNAConfigPMKReauthThreshold
,
4224 dot11RSNAConfigSATimeout
,
4225 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteSelected
),
4226 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherSelected
),
4227 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherSelected
),
4229 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteRequested
),
4230 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherRequested
),
4231 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherRequested
),
4232 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
,
4233 wpa_auth
->dot11RSNA4WayHandshakeFailures
);
4234 if (os_snprintf_error(buflen
- len
, ret
))
4238 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
4239 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4242 ret
= os_snprintf(buf
+ len
, buflen
- len
, "hostapdWPAGroupState=%d\n",
4243 wpa_auth
->group
->wpa_group_state
);
4244 if (os_snprintf_error(buflen
- len
, ret
))
4252 int wpa_get_mib_sta(struct wpa_state_machine
*sm
, char *buf
, size_t buflen
)
4260 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4262 /* dot11RSNAStatsEntry */
4264 pairwise
= wpa_cipher_to_suite(sm
->wpa
== WPA_VERSION_WPA2
?
4265 WPA_PROTO_RSN
: WPA_PROTO_WPA
,
4271 buf
+ len
, buflen
- len
,
4272 /* TODO: dot11RSNAStatsIndex */
4273 "dot11RSNAStatsSTAAddress=" MACSTR
"\n"
4274 "dot11RSNAStatsVersion=1\n"
4275 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE
"\n"
4276 /* TODO: dot11RSNAStatsTKIPICVErrors */
4277 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
4278 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
4279 /* TODO: dot11RSNAStatsCCMPReplays */
4280 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
4281 /* TODO: dot11RSNAStatsTKIPReplays */,
4283 RSN_SUITE_ARG(pairwise
),
4284 sm
->dot11RSNAStatsTKIPLocalMICFailures
,
4285 sm
->dot11RSNAStatsTKIPRemoteMICFailures
);
4286 if (os_snprintf_error(buflen
- len
, ret
))
4291 ret
= os_snprintf(buf
+ len
, buflen
- len
,
4293 "AKMSuiteSelector=" RSN_SUITE
"\n"
4294 "hostapdWPAPTKState=%d\n"
4295 "hostapdWPAPTKGroupState=%d\n",
4297 RSN_SUITE_ARG(wpa_akm_to_suite(sm
->wpa_key_mgmt
)),
4299 sm
->wpa_ptk_group_state
);
4300 if (os_snprintf_error(buflen
- len
, ret
))
4308 void wpa_auth_countermeasures_start(struct wpa_authenticator
*wpa_auth
)
4311 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
++;
4315 int wpa_auth_pairwise_set(struct wpa_state_machine
*sm
)
4317 return sm
&& sm
->pairwise_set
;
4321 int wpa_auth_get_pairwise(struct wpa_state_machine
*sm
)
4323 return sm
->pairwise
;
4327 const u8
* wpa_auth_get_pmk(struct wpa_state_machine
*sm
, int *len
)
4336 int wpa_auth_sta_key_mgmt(struct wpa_state_machine
*sm
)
4340 return sm
->wpa_key_mgmt
;
4344 int wpa_auth_sta_wpa_version(struct wpa_state_machine
*sm
)
4352 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine
*sm
)
4354 if (!sm
|| !wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
4356 return sm
->tk_already_set
;
4360 int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine
*sm
)
4362 if (!sm
|| !wpa_key_mgmt_fils(sm
->wpa_key_mgmt
))
4364 return sm
->tk_already_set
;
4368 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine
*sm
,
4369 struct rsn_pmksa_cache_entry
*entry
)
4371 if (sm
== NULL
|| sm
->pmksa
!= entry
)
4378 struct rsn_pmksa_cache_entry
*
4379 wpa_auth_sta_get_pmksa(struct wpa_state_machine
*sm
)
4381 return sm
? sm
->pmksa
: NULL
;
4385 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine
*sm
)
4388 sm
->dot11RSNAStatsTKIPLocalMICFailures
++;
4392 const u8
* wpa_auth_get_wpa_ie(struct wpa_authenticator
*wpa_auth
, size_t *len
)
4394 if (wpa_auth
== NULL
)
4396 *len
= wpa_auth
->wpa_ie_len
;
4397 return wpa_auth
->wpa_ie
;
4401 int wpa_auth_pmksa_add(struct wpa_state_machine
*sm
, const u8
*pmk
,
4402 unsigned int pmk_len
,
4403 int session_timeout
, struct eapol_state_machine
*eapol
)
4405 if (sm
== NULL
|| sm
->wpa
!= WPA_VERSION_WPA2
||
4406 sm
->wpa_auth
->conf
.disable_pmksa_caching
)
4409 #ifdef CONFIG_IEEE80211R_AP
4410 if (pmk_len
>= 2 * PMK_LEN
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
) &&
4411 wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
4412 !wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
4413 /* Cache MPMK/XXKey instead of initial part from MSK */
4414 pmk
= pmk
+ PMK_LEN
;
4417 #endif /* CONFIG_IEEE80211R_AP */
4418 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
4419 if (pmk_len
> PMK_LEN_SUITE_B_192
)
4420 pmk_len
= PMK_LEN_SUITE_B_192
;
4421 } else if (pmk_len
> PMK_LEN
) {
4425 wpa_hexdump_key(MSG_DEBUG
, "RSN: Cache PMK", pmk
, pmk_len
);
4426 if (pmksa_cache_auth_add(sm
->wpa_auth
->pmksa
, pmk
, pmk_len
, NULL
,
4427 sm
->PTK
.kck
, sm
->PTK
.kck_len
,
4428 sm
->wpa_auth
->addr
, sm
->addr
, session_timeout
,
4429 eapol
, sm
->wpa_key_mgmt
))
4436 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator
*wpa_auth
,
4437 const u8
*pmk
, size_t len
, const u8
*sta_addr
,
4438 int session_timeout
,
4439 struct eapol_state_machine
*eapol
)
4441 if (wpa_auth
== NULL
)
4444 wpa_hexdump_key(MSG_DEBUG
, "RSN: Cache PMK from preauth", pmk
, len
);
4445 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, len
, NULL
,
4448 sta_addr
, session_timeout
, eapol
,
4449 WPA_KEY_MGMT_IEEE8021X
))
4456 int wpa_auth_pmksa_add_sae(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4457 const u8
*pmk
, const u8
*pmkid
)
4459 if (wpa_auth
->conf
.disable_pmksa_caching
)
4462 wpa_hexdump_key(MSG_DEBUG
, "RSN: Cache PMK from SAE", pmk
, PMK_LEN
);
4463 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, PMK_LEN
, pmkid
,
4465 wpa_auth
->addr
, addr
, 0, NULL
,
4473 void wpa_auth_add_sae_pmkid(struct wpa_state_machine
*sm
, const u8
*pmkid
)
4475 os_memcpy(sm
->pmkid
, pmkid
, PMKID_LEN
);
4480 int wpa_auth_pmksa_add2(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4481 const u8
*pmk
, size_t pmk_len
, const u8
*pmkid
,
4482 int session_timeout
, int akmp
)
4484 if (wpa_auth
->conf
.disable_pmksa_caching
)
4487 wpa_hexdump_key(MSG_DEBUG
, "RSN: Cache PMK (2)", pmk
, PMK_LEN
);
4488 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, pmk_len
, pmkid
,
4489 NULL
, 0, wpa_auth
->addr
, addr
, session_timeout
,
4497 void wpa_auth_pmksa_remove(struct wpa_authenticator
*wpa_auth
,
4500 struct rsn_pmksa_cache_entry
*pmksa
;
4502 if (wpa_auth
== NULL
|| wpa_auth
->pmksa
== NULL
)
4504 pmksa
= pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, NULL
);
4506 wpa_printf(MSG_DEBUG
, "WPA: Remove PMKSA cache entry for "
4507 MACSTR
" based on request", MAC2STR(sta_addr
));
4508 pmksa_cache_free_entry(wpa_auth
->pmksa
, pmksa
);
4513 int wpa_auth_pmksa_list(struct wpa_authenticator
*wpa_auth
, char *buf
,
4516 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4518 return pmksa_cache_auth_list(wpa_auth
->pmksa
, buf
, len
);
4522 void wpa_auth_pmksa_flush(struct wpa_authenticator
*wpa_auth
)
4524 if (wpa_auth
&& wpa_auth
->pmksa
)
4525 pmksa_cache_auth_flush(wpa_auth
->pmksa
);
4529 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4532 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4533 char *buf
, size_t len
)
4535 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4538 return pmksa_cache_auth_list_mesh(wpa_auth
->pmksa
, addr
, buf
, len
);
4542 struct rsn_pmksa_cache_entry
*
4543 wpa_auth_pmksa_create_entry(const u8
*aa
, const u8
*spa
, const u8
*pmk
,
4544 const u8
*pmkid
, int expiration
)
4546 struct rsn_pmksa_cache_entry
*entry
;
4547 struct os_reltime now
;
4549 entry
= pmksa_cache_auth_create_entry(pmk
, PMK_LEN
, pmkid
, NULL
, 0, aa
,
4550 spa
, 0, NULL
, WPA_KEY_MGMT_SAE
);
4554 os_get_reltime(&now
);
4555 entry
->expiration
= now
.sec
+ expiration
;
4560 int wpa_auth_pmksa_add_entry(struct wpa_authenticator
*wpa_auth
,
4561 struct rsn_pmksa_cache_entry
*entry
)
4565 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4568 ret
= pmksa_cache_auth_add_entry(wpa_auth
->pmksa
, entry
);
4570 wpa_printf(MSG_DEBUG
,
4571 "RSN: Failed to store external PMKSA cache for "
4572 MACSTR
, MAC2STR(entry
->spa
));
4577 #endif /* CONFIG_MESH */
4578 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4581 struct rsn_pmksa_cache_entry
*
4582 wpa_auth_pmksa_get(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
,
4585 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4587 return pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, pmkid
);
4591 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry
*pmksa
,
4592 struct wpa_state_machine
*sm
,
4593 struct wpa_authenticator
*wpa_auth
,
4600 os_memcpy(pmk
, pmksa
->pmk
, PMK_LEN
);
4601 os_memcpy(pmkid
, pmksa
->pmkid
, PMKID_LEN
);
4602 os_memcpy(wpa_auth
->dot11RSNAPMKIDUsed
, pmksa
->pmkid
, PMKID_LEN
);
4607 * Remove and free the group from wpa_authenticator. This is triggered by a
4608 * callback to make sure nobody is currently iterating the group list while it
4611 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
4612 struct wpa_group
*group
)
4614 struct wpa_group
*prev
= wpa_auth
->group
;
4616 wpa_printf(MSG_DEBUG
, "WPA: Remove group state machine for VLAN-ID %d",
4620 if (prev
->next
== group
) {
4621 /* This never frees the special first group as needed */
4622 prev
->next
= group
->next
;
4632 /* Increase the reference counter for group */
4633 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
4634 struct wpa_group
*group
)
4636 /* Skip the special first group */
4637 if (wpa_auth
->group
== group
)
4640 group
->references
++;
4644 /* Decrease the reference counter and maybe free the group */
4645 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
4646 struct wpa_group
*group
)
4648 /* Skip the special first group */
4649 if (wpa_auth
->group
== group
)
4652 group
->references
--;
4653 if (group
->references
)
4655 wpa_group_free(wpa_auth
, group
);
4660 * Add a group that has its references counter set to zero. Caller needs to
4661 * call wpa_group_get() on the return value to mark the entry in use.
4663 static struct wpa_group
*
4664 wpa_auth_add_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4666 struct wpa_group
*group
;
4668 if (wpa_auth
== NULL
|| wpa_auth
->group
== NULL
)
4671 wpa_printf(MSG_DEBUG
, "WPA: Add group state machine for VLAN-ID %d",
4673 group
= wpa_group_init(wpa_auth
, vlan_id
, 0);
4677 group
->next
= wpa_auth
->group
->next
;
4678 wpa_auth
->group
->next
= group
;
4685 * Enforce that the group state machine for the VLAN is running, increase
4686 * reference counter as interface is up. References might have been increased
4687 * even if a negative value is returned.
4688 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4690 int wpa_auth_ensure_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4692 struct wpa_group
*group
;
4694 if (wpa_auth
== NULL
)
4697 group
= wpa_auth
->group
;
4699 if (group
->vlan_id
== vlan_id
)
4701 group
= group
->next
;
4704 if (group
== NULL
) {
4705 group
= wpa_auth_add_group(wpa_auth
, vlan_id
);
4710 wpa_printf(MSG_DEBUG
,
4711 "WPA: Ensure group state machine running for VLAN ID %d",
4714 wpa_group_get(wpa_auth
, group
);
4715 group
->num_setup_iface
++;
4717 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4725 * Decrease reference counter, expected to be zero afterwards.
4726 * returns: -1 on error (group not found, group in fail state)
4727 * -2 if wpa_group is still referenced
4730 int wpa_auth_release_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4732 struct wpa_group
*group
;
4735 if (wpa_auth
== NULL
)
4738 group
= wpa_auth
->group
;
4740 if (group
->vlan_id
== vlan_id
)
4742 group
= group
->next
;
4748 wpa_printf(MSG_DEBUG
,
4749 "WPA: Try stopping group state machine for VLAN ID %d",
4752 if (group
->num_setup_iface
<= 0) {
4753 wpa_printf(MSG_ERROR
,
4754 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4758 group
->num_setup_iface
--;
4760 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4763 if (group
->references
> 1) {
4764 wpa_printf(MSG_DEBUG
,
4765 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4770 wpa_group_put(wpa_auth
, group
);
4776 int wpa_auth_sta_set_vlan(struct wpa_state_machine
*sm
, int vlan_id
)
4778 struct wpa_group
*group
;
4780 if (sm
== NULL
|| sm
->wpa_auth
== NULL
)
4783 group
= sm
->wpa_auth
->group
;
4785 if (group
->vlan_id
== vlan_id
)
4787 group
= group
->next
;
4790 if (group
== NULL
) {
4791 group
= wpa_auth_add_group(sm
->wpa_auth
, vlan_id
);
4796 if (sm
->group
== group
)
4799 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4802 wpa_printf(MSG_DEBUG
, "WPA: Moving STA " MACSTR
" to use group state "
4803 "machine for VLAN ID %d", MAC2STR(sm
->addr
), vlan_id
);
4805 wpa_group_get(sm
->wpa_auth
, group
);
4806 wpa_group_put(sm
->wpa_auth
, sm
->group
);
4813 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator
*wpa_auth
,
4814 struct wpa_state_machine
*sm
, int ack
)
4816 if (wpa_auth
== NULL
|| sm
== NULL
)
4818 wpa_printf(MSG_DEBUG
, "WPA: EAPOL-Key TX status for STA " MACSTR
4819 " ack=%d", MAC2STR(sm
->addr
), ack
);
4820 if (sm
->pending_1_of_4_timeout
&& ack
) {
4822 * Some deployed supplicant implementations update their SNonce
4823 * for each EAPOL-Key 2/4 message even within the same 4-way
4824 * handshake and then fail to use the first SNonce when
4825 * deriving the PTK. This results in unsuccessful 4-way
4826 * handshake whenever the relatively short initial timeout is
4827 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4828 * around this by increasing the timeout now that we know that
4829 * the station has received the frame.
4831 int timeout_ms
= eapol_key_timeout_subseq
;
4832 wpa_printf(MSG_DEBUG
, "WPA: Increase initial EAPOL-Key 1/4 "
4833 "timeout by %u ms because of acknowledged frame",
4835 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
4836 eloop_register_timeout(timeout_ms
/ 1000,
4837 (timeout_ms
% 1000) * 1000,
4838 wpa_send_eapol_timeout
, wpa_auth
, sm
);
4841 #ifdef CONFIG_TESTING_OPTIONS
4842 if (sm
->eapol_status_cb
) {
4843 sm
->eapol_status_cb(sm
->eapol_status_cb_ctx1
,
4844 sm
->eapol_status_cb_ctx2
);
4845 sm
->eapol_status_cb
= NULL
;
4847 #endif /* CONFIG_TESTING_OPTIONS */
4851 int wpa_auth_uses_sae(struct wpa_state_machine
*sm
)
4855 return wpa_key_mgmt_sae(sm
->wpa_key_mgmt
);
4859 int wpa_auth_uses_ft_sae(struct wpa_state_machine
*sm
)
4863 return sm
->wpa_key_mgmt
== WPA_KEY_MGMT_FT_SAE
;
4868 int wpa_auth_get_ip_addr(struct wpa_state_machine
*sm
, u8
*addr
)
4870 if (sm
== NULL
|| WPA_GET_BE32(sm
->ip_addr
) == 0)
4872 os_memcpy(addr
, sm
->ip_addr
, 4);
4875 #endif /* CONFIG_P2P */
4878 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator
*wpa_auth
,
4879 struct radius_das_attrs
*attr
)
4881 return pmksa_cache_auth_radius_das_disconnect(wpa_auth
->pmksa
, attr
);
4885 void wpa_auth_reconfig_group_keys(struct wpa_authenticator
*wpa_auth
)
4887 struct wpa_group
*group
;
4891 for (group
= wpa_auth
->group
; group
; group
= group
->next
)
4892 wpa_group_config_group_keys(wpa_auth
, group
);
4898 struct wpa_auth_fils_iter_data
{
4899 struct wpa_authenticator
*auth
;
4901 struct rsn_pmksa_cache_entry
*pmksa
;
4907 static int wpa_auth_fils_iter(struct wpa_authenticator
*a
, void *ctx
)
4909 struct wpa_auth_fils_iter_data
*data
= ctx
;
4911 if (a
== data
->auth
|| !a
->conf
.fils_cache_id_set
||
4912 os_memcmp(a
->conf
.fils_cache_id
, data
->cache_id
,
4913 FILS_CACHE_ID_LEN
) != 0)
4915 data
->pmksa
= pmksa_cache_auth_get(a
->pmksa
, data
->spa
, data
->pmkid
);
4916 return data
->pmksa
!= NULL
;
4920 struct rsn_pmksa_cache_entry
*
4921 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator
*wpa_auth
,
4922 const u8
*sta_addr
, const u8
*pmkid
)
4924 struct wpa_auth_fils_iter_data idata
;
4926 if (!wpa_auth
->conf
.fils_cache_id_set
)
4928 idata
.auth
= wpa_auth
;
4929 idata
.cache_id
= wpa_auth
->conf
.fils_cache_id
;
4931 idata
.spa
= sta_addr
;
4932 idata
.pmkid
= pmkid
;
4933 wpa_auth_for_each_auth(wpa_auth
, wpa_auth_fils_iter
, &idata
);
4938 #ifdef CONFIG_IEEE80211R_AP
4939 int wpa_auth_write_fte(struct wpa_authenticator
*wpa_auth
, int use_sha384
,
4940 u8
*buf
, size_t len
)
4942 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
4944 return wpa_write_ftie(conf
, use_sha384
, conf
->r0_key_holder
,
4945 conf
->r0_key_holder_len
,
4946 NULL
, NULL
, buf
, len
, NULL
, 0);
4948 #endif /* CONFIG_IEEE80211R_AP */
4951 void wpa_auth_get_fils_aead_params(struct wpa_state_machine
*sm
,
4952 u8
*fils_anonce
, u8
*fils_snonce
,
4953 u8
*fils_kek
, size_t *fils_kek_len
)
4955 os_memcpy(fils_anonce
, sm
->ANonce
, WPA_NONCE_LEN
);
4956 os_memcpy(fils_snonce
, sm
->SNonce
, WPA_NONCE_LEN
);
4957 os_memcpy(fils_kek
, sm
->PTK
.kek
, WPA_KEK_MAX_LEN
);
4958 *fils_kek_len
= sm
->PTK
.kek_len
;
4962 void wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine
*sm
, const u8
*pmk
,
4963 size_t pmk_len
, const u8
*pmkid
)
4965 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
4966 sm
->pmk_len
= pmk_len
;
4967 os_memcpy(sm
->pmkid
, pmkid
, PMKID_LEN
);
4971 #endif /* CONFIG_FILS */
4974 void wpa_auth_set_auth_alg(struct wpa_state_machine
*sm
, u16 auth_alg
)
4977 sm
->auth_alg
= auth_alg
;
4982 void wpa_auth_set_dpp_z(struct wpa_state_machine
*sm
, const struct wpabuf
*z
)
4985 wpabuf_clear_free(sm
->dpp_z
);
4986 sm
->dpp_z
= z
? wpabuf_dup(z
) : NULL
;
4989 #endif /* CONFIG_DPP2 */
4992 #ifdef CONFIG_TESTING_OPTIONS
4994 int wpa_auth_resend_m1(struct wpa_state_machine
*sm
, int change_anonce
,
4995 void (*cb
)(void *ctx1
, void *ctx2
),
4996 void *ctx1
, void *ctx2
)
4998 const u8
*anonce
= sm
->ANonce
;
4999 u8 anonce_buf
[WPA_NONCE_LEN
];
5001 if (change_anonce
) {
5002 if (random_get_bytes(anonce_buf
, WPA_NONCE_LEN
))
5004 anonce
= anonce_buf
;
5007 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5008 "sending 1/4 msg of 4-Way Handshake (TESTING)");
5009 wpa_send_eapol(sm
->wpa_auth
, sm
,
5010 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
5011 anonce
, NULL
, 0, 0, 0);
5016 int wpa_auth_resend_m3(struct wpa_state_machine
*sm
,
5017 void (*cb
)(void *ctx1
, void *ctx2
),
5018 void *ctx1
, void *ctx2
)
5020 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
;
5022 size_t gtk_len
, kde_len
;
5023 struct wpa_group
*gsm
= sm
->group
;
5025 int wpa_ie_len
, secure
, gtkidx
, encr
= 0;
5027 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
5028 GTK[GN], IGTK, [FTIE], [TIE * 2])
5032 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
5033 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
5034 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
5035 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
5036 if (sm
->wpa
== WPA_VERSION_WPA
&&
5037 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
5038 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
5039 /* WPA-only STA, remove RSN IE and possible MDIE */
5040 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
5041 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
5042 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
5043 wpa_ie_len
= wpa_ie
[1] + 2;
5045 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5046 "sending 3/4 msg of 4-Way Handshake (TESTING)");
5047 if (sm
->wpa
== WPA_VERSION_WPA2
) {
5048 /* WPA2 send GTK in the 4-way handshake */
5050 gtk
= gsm
->GTK
[gsm
->GN
- 1];
5051 gtk_len
= gsm
->GTK_len
;
5056 /* WPA does not include GTK in msg 3/4 */
5061 if (sm
->rx_eapol_key_secure
) {
5063 * It looks like Windows 7 supplicant tries to use
5064 * Secure bit in msg 2/4 after having reported Michael
5065 * MIC failure and it then rejects the 4-way handshake
5066 * if msg 3/4 does not set Secure bit. Work around this
5067 * by setting the Secure bit here even in the case of
5068 * WPA if the supplicant used it first.
5070 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5071 "STA used Secure bit in WPA msg 2/4 - "
5072 "set Secure for 3/4 as workaround");
5077 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
5079 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
5080 #ifdef CONFIG_IEEE80211R_AP
5081 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
5082 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
5083 kde_len
+= 300; /* FTIE + 2 * TIE */
5085 #endif /* CONFIG_IEEE80211R_AP */
5086 kde
= os_malloc(kde_len
);
5091 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
5093 #ifdef CONFIG_IEEE80211R_AP
5094 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
5099 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
5101 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
5102 "PMKR1Name into RSN IE in EAPOL-Key data");
5109 #endif /* CONFIG_IEEE80211R_AP */
5112 hdr
[0] = gtkidx
& 0x03;
5114 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
5118 pos
= ieee80211w_kde_add(sm
, pos
);
5119 if (pos
- opos
>= 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
) {
5120 /* skip KDE header and keyid */
5121 opos
+= 2 + RSN_SELECTOR_LEN
+ 2;
5122 os_memset(opos
, 0, 6); /* clear PN */
5124 if (ocv_oci_add(sm
, &pos
) < 0) {
5129 #ifdef CONFIG_IEEE80211R_AP
5130 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
5132 struct wpa_auth_config
*conf
;
5134 conf
= &sm
->wpa_auth
->conf
;
5135 if (sm
->assoc_resp_ftie
&&
5136 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
5137 os_memcpy(pos
, sm
->assoc_resp_ftie
,
5138 2 + sm
->assoc_resp_ftie
[1]);
5139 res
= 2 + sm
->assoc_resp_ftie
[1];
5141 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
5143 res
= wpa_write_ftie(conf
, use_sha384
,
5144 conf
->r0_key_holder
,
5145 conf
->r0_key_holder_len
,
5147 kde
+ kde_len
- pos
,
5151 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
5152 "into EAPOL-Key Key Data");
5158 /* TIE[ReassociationDeadline] (TU) */
5159 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
5161 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
5162 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
5165 /* TIE[KeyLifetime] (seconds) */
5166 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
5168 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
5169 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
);
5172 #endif /* CONFIG_IEEE80211R_AP */
5174 wpa_send_eapol(sm
->wpa_auth
, sm
,
5175 (secure
? WPA_KEY_INFO_SECURE
: 0) |
5176 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
5177 WPA_KEY_INFO_MIC
: 0) |
5178 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
5179 WPA_KEY_INFO_KEY_TYPE
,
5180 _rsc
, sm
->ANonce
, kde
, pos
- kde
, 0, encr
);
5186 int wpa_auth_resend_group_m1(struct wpa_state_machine
*sm
,
5187 void (*cb
)(void *ctx1
, void *ctx2
),
5188 void *ctx1
, void *ctx2
)
5190 u8 rsc
[WPA_KEY_RSC_LEN
];
5191 struct wpa_group
*gsm
= sm
->group
;
5193 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
5198 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
5199 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
5201 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5202 "sending 1/2 msg of Group Key Handshake (TESTING)");
5204 gtk
= gsm
->GTK
[gsm
->GN
- 1];
5205 if (sm
->wpa
== WPA_VERSION_WPA2
) {
5206 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
5207 ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
5208 kde_buf
= os_malloc(kde_len
);
5209 if (kde_buf
== NULL
)
5212 kde
= pos
= kde_buf
;
5213 hdr
[0] = gsm
->GN
& 0x03;
5215 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
5218 pos
= ieee80211w_kde_add(sm
, pos
);
5220 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
) {
5221 /* skip KDE header and keyid */
5222 opos
+= 2 + RSN_SELECTOR_LEN
+ 2;
5223 os_memset(opos
, 0, 6); /* clear PN */
5225 if (ocv_oci_add(sm
, &pos
) < 0) {
5229 kde_len
= pos
- kde
;
5232 kde_len
= gsm
->GTK_len
;
5235 sm
->eapol_status_cb
= cb
;
5236 sm
->eapol_status_cb_ctx1
= ctx1
;
5237 sm
->eapol_status_cb_ctx2
= ctx2
;
5239 wpa_send_eapol(sm
->wpa_auth
, sm
,
5240 WPA_KEY_INFO_SECURE
|
5241 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
5242 WPA_KEY_INFO_MIC
: 0) |
5244 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
5245 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
5252 int wpa_auth_rekey_gtk(struct wpa_authenticator
*wpa_auth
)
5256 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
5257 return eloop_register_timeout(0, 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
5260 #endif /* CONFIG_TESTING_OPTIONS */