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
)
151 if (wpa_auth
->cb
->get_seqnum
== NULL
)
153 return wpa_auth
->cb
->get_seqnum(wpa_auth
->cb_ctx
, addr
, idx
, seq
);
158 wpa_auth_send_eapol(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
159 const u8
*data
, size_t data_len
, int encrypt
)
161 if (wpa_auth
->cb
->send_eapol
== NULL
)
163 return wpa_auth
->cb
->send_eapol(wpa_auth
->cb_ctx
, addr
, data
, data_len
,
169 static inline int wpa_auth_start_ampe(struct wpa_authenticator
*wpa_auth
,
172 if (wpa_auth
->cb
->start_ampe
== NULL
)
174 return wpa_auth
->cb
->start_ampe(wpa_auth
->cb_ctx
, addr
);
176 #endif /* CONFIG_MESH */
179 int wpa_auth_for_each_sta(struct wpa_authenticator
*wpa_auth
,
180 int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
183 if (wpa_auth
->cb
->for_each_sta
== NULL
)
185 return wpa_auth
->cb
->for_each_sta(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
189 int wpa_auth_for_each_auth(struct wpa_authenticator
*wpa_auth
,
190 int (*cb
)(struct wpa_authenticator
*a
, void *ctx
),
193 if (wpa_auth
->cb
->for_each_auth
== NULL
)
195 return wpa_auth
->cb
->for_each_auth(wpa_auth
->cb_ctx
, cb
, cb_ctx
);
199 void wpa_auth_logger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
200 logger_level level
, const char *txt
)
202 if (wpa_auth
->cb
->logger
== NULL
)
204 wpa_auth
->cb
->logger(wpa_auth
->cb_ctx
, addr
, level
, txt
);
208 void wpa_auth_vlogger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
209 logger_level level
, const char *fmt
, ...)
215 if (wpa_auth
->cb
->logger
== NULL
)
218 maxlen
= os_strlen(fmt
) + 100;
219 format
= os_malloc(maxlen
);
224 vsnprintf(format
, maxlen
, fmt
, ap
);
227 wpa_auth_logger(wpa_auth
, addr
, level
, format
);
233 static void wpa_sta_disconnect(struct wpa_authenticator
*wpa_auth
,
234 const u8
*addr
, u16 reason
)
236 if (wpa_auth
->cb
->disconnect
== NULL
)
238 wpa_printf(MSG_DEBUG
, "wpa_sta_disconnect STA " MACSTR
" (reason %u)",
239 MAC2STR(addr
), reason
);
240 wpa_auth
->cb
->disconnect(wpa_auth
->cb_ctx
, addr
, reason
);
245 static int wpa_channel_info(struct wpa_authenticator
*wpa_auth
,
246 struct wpa_channel_info
*ci
)
248 if (!wpa_auth
->cb
->channel_info
)
250 return wpa_auth
->cb
->channel_info(wpa_auth
->cb_ctx
, ci
);
252 #endif /* CONFIG_OCV */
255 static int wpa_auth_update_vlan(struct wpa_authenticator
*wpa_auth
,
256 const u8
*addr
, int vlan_id
)
258 if (!wpa_auth
->cb
->update_vlan
)
260 return wpa_auth
->cb
->update_vlan(wpa_auth
->cb_ctx
, addr
, vlan_id
);
264 static void wpa_rekey_gmk(void *eloop_ctx
, void *timeout_ctx
)
266 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
268 if (random_get_bytes(wpa_auth
->group
->GMK
, WPA_GMK_LEN
)) {
269 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
272 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "GMK rekeyd");
273 wpa_hexdump_key(MSG_DEBUG
, "GMK",
274 wpa_auth
->group
->GMK
, WPA_GMK_LEN
);
277 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
278 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
279 wpa_rekey_gmk
, wpa_auth
, NULL
);
284 static void wpa_rekey_gtk(void *eloop_ctx
, void *timeout_ctx
)
286 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
287 struct wpa_group
*group
, *next
;
289 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "rekeying GTK");
290 group
= wpa_auth
->group
;
292 wpa_group_get(wpa_auth
, group
);
294 group
->GTKReKey
= TRUE
;
296 group
->changed
= FALSE
;
297 wpa_group_sm_step(wpa_auth
, group
);
298 } while (group
->changed
);
301 wpa_group_put(wpa_auth
, group
);
305 if (wpa_auth
->conf
.wpa_group_rekey
) {
306 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
,
307 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
312 static void wpa_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
314 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
315 struct wpa_state_machine
*sm
= timeout_ctx
;
317 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "rekeying PTK");
318 wpa_request_new_ptk(sm
);
323 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine
*sm
, void *ctx
)
325 if (sm
->pmksa
== ctx
)
331 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
334 struct wpa_authenticator
*wpa_auth
= ctx
;
335 wpa_auth_for_each_sta(wpa_auth
, wpa_auth_pmksa_clear_cb
, entry
);
339 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator
*wpa_auth
,
340 struct wpa_group
*group
)
342 u8 buf
[ETH_ALEN
+ 8 + sizeof(unsigned long)];
346 if (random_get_bytes(group
->GMK
, WPA_GMK_LEN
) < 0)
348 wpa_hexdump_key(MSG_DEBUG
, "GMK", group
->GMK
, WPA_GMK_LEN
);
351 * Counter = PRF-256(Random number, "Init Counter",
352 * Local MAC Address || Time)
354 os_memcpy(buf
, wpa_auth
->addr
, ETH_ALEN
);
355 wpa_get_ntp_timestamp(buf
+ ETH_ALEN
);
356 ptr
= (unsigned long) group
;
357 os_memcpy(buf
+ ETH_ALEN
+ 8, &ptr
, sizeof(ptr
));
359 os_memset(buf
+ ETH_ALEN
, 0xab, 8);
360 os_memset(buf
+ ETH_ALEN
+ 8, 0xcd, sizeof(ptr
));
361 #endif /* TEST_FUZZ */
362 if (random_get_bytes(rkey
, sizeof(rkey
)) < 0)
365 if (sha1_prf(rkey
, sizeof(rkey
), "Init Counter", buf
, sizeof(buf
),
366 group
->Counter
, WPA_NONCE_LEN
) < 0)
368 wpa_hexdump_key(MSG_DEBUG
, "Key Counter",
369 group
->Counter
, WPA_NONCE_LEN
);
375 static struct wpa_group
* wpa_group_init(struct wpa_authenticator
*wpa_auth
,
376 int vlan_id
, int delay_init
)
378 struct wpa_group
*group
;
380 group
= os_zalloc(sizeof(struct wpa_group
));
384 group
->GTKAuthenticator
= TRUE
;
385 group
->vlan_id
= vlan_id
;
386 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
388 if (random_pool_ready() != 1) {
389 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
390 "for secure operations - update keys later when "
391 "the first station connects");
395 * Set initial GMK/Counter value here. The actual values that will be
396 * used in negotiations will be set once the first station tries to
397 * connect. This allows more time for collecting additional randomness
398 * on embedded devices.
400 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0) {
401 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
409 wpa_printf(MSG_DEBUG
, "WPA: Delay group state machine start "
410 "until Beacon frames have been configured");
411 /* Initialization is completed in wpa_init_keys(). */
413 wpa_group_sm_step(wpa_auth
, group
);
414 group
->GInit
= FALSE
;
415 wpa_group_sm_step(wpa_auth
, group
);
423 * wpa_init - Initialize WPA authenticator
424 * @addr: Authenticator address
425 * @conf: Configuration for WPA authenticator
426 * @cb: Callback functions for WPA authenticator
427 * Returns: Pointer to WPA authenticator data or %NULL on failure
429 struct wpa_authenticator
* wpa_init(const u8
*addr
,
430 struct wpa_auth_config
*conf
,
431 const struct wpa_auth_callbacks
*cb
,
434 struct wpa_authenticator
*wpa_auth
;
436 wpa_auth
= os_zalloc(sizeof(struct wpa_authenticator
));
437 if (wpa_auth
== NULL
)
439 os_memcpy(wpa_auth
->addr
, addr
, ETH_ALEN
);
440 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
442 wpa_auth
->cb_ctx
= cb_ctx
;
444 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
445 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
450 wpa_auth
->group
= wpa_group_init(wpa_auth
, 0, 1);
451 if (wpa_auth
->group
== NULL
) {
452 os_free(wpa_auth
->wpa_ie
);
457 wpa_auth
->pmksa
= pmksa_cache_auth_init(wpa_auth_pmksa_free_cb
,
459 if (wpa_auth
->pmksa
== NULL
) {
460 wpa_printf(MSG_ERROR
, "PMKSA cache initialization failed.");
461 os_free(wpa_auth
->group
);
462 os_free(wpa_auth
->wpa_ie
);
467 #ifdef CONFIG_IEEE80211R_AP
468 wpa_auth
->ft_pmk_cache
= wpa_ft_pmk_cache_init();
469 if (wpa_auth
->ft_pmk_cache
== NULL
) {
470 wpa_printf(MSG_ERROR
, "FT PMK cache initialization failed.");
471 os_free(wpa_auth
->group
);
472 os_free(wpa_auth
->wpa_ie
);
473 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
477 #endif /* CONFIG_IEEE80211R_AP */
479 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
480 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
481 wpa_rekey_gmk
, wpa_auth
, NULL
);
484 if (wpa_auth
->conf
.wpa_group_rekey
) {
485 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
, 0,
486 wpa_rekey_gtk
, wpa_auth
, NULL
);
490 if (WPA_GET_BE32(conf
->ip_addr_start
)) {
491 int count
= WPA_GET_BE32(conf
->ip_addr_end
) -
492 WPA_GET_BE32(conf
->ip_addr_start
) + 1;
496 wpa_auth
->ip_pool
= bitfield_alloc(count
);
498 #endif /* CONFIG_P2P */
504 int wpa_init_keys(struct wpa_authenticator
*wpa_auth
)
506 struct wpa_group
*group
= wpa_auth
->group
;
508 wpa_printf(MSG_DEBUG
, "WPA: Start group state machine to set initial "
510 wpa_group_sm_step(wpa_auth
, group
);
511 group
->GInit
= FALSE
;
512 wpa_group_sm_step(wpa_auth
, group
);
513 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
520 * wpa_deinit - Deinitialize WPA authenticator
521 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
523 void wpa_deinit(struct wpa_authenticator
*wpa_auth
)
525 struct wpa_group
*group
, *prev
;
527 eloop_cancel_timeout(wpa_rekey_gmk
, wpa_auth
, NULL
);
528 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
530 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
532 #ifdef CONFIG_IEEE80211R_AP
533 wpa_ft_pmk_cache_deinit(wpa_auth
->ft_pmk_cache
);
534 wpa_auth
->ft_pmk_cache
= NULL
;
535 wpa_ft_deinit(wpa_auth
);
536 #endif /* CONFIG_IEEE80211R_AP */
539 bitfield_free(wpa_auth
->ip_pool
);
540 #endif /* CONFIG_P2P */
543 os_free(wpa_auth
->wpa_ie
);
545 group
= wpa_auth
->group
;
557 * wpa_reconfig - Update WPA authenticator configuration
558 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
559 * @conf: Configuration for WPA authenticator
561 int wpa_reconfig(struct wpa_authenticator
*wpa_auth
,
562 struct wpa_auth_config
*conf
)
564 struct wpa_group
*group
;
565 if (wpa_auth
== NULL
)
568 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
569 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
570 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
575 * Reinitialize GTK to make sure it is suitable for the new
578 group
= wpa_auth
->group
;
579 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
581 wpa_group_sm_step(wpa_auth
, group
);
582 group
->GInit
= FALSE
;
583 wpa_group_sm_step(wpa_auth
, group
);
589 struct wpa_state_machine
*
590 wpa_auth_sta_init(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
591 const u8
*p2p_dev_addr
)
593 struct wpa_state_machine
*sm
;
595 if (wpa_auth
->group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
598 sm
= os_zalloc(sizeof(struct wpa_state_machine
));
601 os_memcpy(sm
->addr
, addr
, ETH_ALEN
);
603 os_memcpy(sm
->p2p_dev_addr
, p2p_dev_addr
, ETH_ALEN
);
605 sm
->wpa_auth
= wpa_auth
;
606 sm
->group
= wpa_auth
->group
;
607 wpa_group_get(sm
->wpa_auth
, sm
->group
);
613 int wpa_auth_sta_associated(struct wpa_authenticator
*wpa_auth
,
614 struct wpa_state_machine
*sm
)
616 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
619 #ifdef CONFIG_IEEE80211R_AP
620 if (sm
->ft_completed
) {
621 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
622 "FT authentication already completed - do not "
623 "start 4-way handshake");
624 /* Go to PTKINITDONE state to allow GTK rekeying */
625 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
629 #endif /* CONFIG_IEEE80211R_AP */
632 if (sm
->fils_completed
) {
633 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
634 "FILS authentication already completed - do not start 4-way handshake");
635 /* Go to PTKINITDONE state to allow GTK rekeying */
636 sm
->wpa_ptk_state
= WPA_PTK_PTKINITDONE
;
640 #endif /* CONFIG_FILS */
643 os_memset(&sm
->key_replay
, 0, sizeof(sm
->key_replay
));
644 sm
->ReAuthenticationRequest
= TRUE
;
645 return wpa_sm_step(sm
);
648 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
649 "start authentication");
653 if (wpa_sm_step(sm
) == 1)
654 return 1; /* should not really happen */
656 sm
->AuthenticationRequest
= TRUE
;
657 return wpa_sm_step(sm
);
661 void wpa_auth_sta_no_wpa(struct wpa_state_machine
*sm
)
663 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
664 * reassociates back to the same AP while the previous entry for the
665 * STA has not yet been removed. */
669 sm
->wpa_key_mgmt
= 0;
673 static void wpa_free_sta_sm(struct wpa_state_machine
*sm
)
676 if (WPA_GET_BE32(sm
->ip_addr
)) {
678 wpa_printf(MSG_DEBUG
, "P2P: Free assigned IP "
679 "address %u.%u.%u.%u from " MACSTR
,
680 sm
->ip_addr
[0], sm
->ip_addr
[1],
681 sm
->ip_addr
[2], sm
->ip_addr
[3],
683 start
= WPA_GET_BE32(sm
->wpa_auth
->conf
.ip_addr_start
);
684 bitfield_clear(sm
->wpa_auth
->ip_pool
,
685 WPA_GET_BE32(sm
->ip_addr
) - start
);
687 #endif /* CONFIG_P2P */
688 if (sm
->GUpdateStationKeys
) {
689 sm
->group
->GKeyDoneStations
--;
690 sm
->GUpdateStationKeys
= FALSE
;
692 #ifdef CONFIG_IEEE80211R_AP
693 os_free(sm
->assoc_resp_ftie
);
694 wpabuf_free(sm
->ft_pending_req_ies
);
695 #endif /* CONFIG_IEEE80211R_AP */
696 os_free(sm
->last_rx_eapol_key
);
698 wpa_group_put(sm
->wpa_auth
, sm
->group
);
700 wpabuf_clear_free(sm
->dpp_z
);
701 #endif /* CONFIG_DPP2 */
706 void wpa_auth_sta_deinit(struct wpa_state_machine
*sm
)
711 if (sm
->wpa_auth
->conf
.wpa_strict_rekey
&& sm
->has_GTK
) {
712 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
713 "strict rekeying - force GTK rekey since STA "
715 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk
,
716 sm
->wpa_auth
, NULL
) == -1)
717 eloop_register_timeout(0, 500000, wpa_rekey_gtk
, sm
->wpa_auth
,
721 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
722 sm
->pending_1_of_4_timeout
= 0;
723 eloop_cancel_timeout(wpa_sm_call_step
, sm
, NULL
);
724 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
725 #ifdef CONFIG_IEEE80211R_AP
726 wpa_ft_sta_deinit(sm
);
727 #endif /* CONFIG_IEEE80211R_AP */
728 if (sm
->in_step_loop
) {
729 /* Must not free state machine while wpa_sm_step() is running.
730 * Freeing will be completed in the end of wpa_sm_step(). */
731 wpa_printf(MSG_DEBUG
, "WPA: Registering pending STA state "
732 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
733 sm
->pending_deinit
= 1;
739 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
)
744 sm
->PTKRequest
= TRUE
;
749 static int wpa_replay_counter_valid(struct wpa_key_replay_counter
*ctr
,
750 const u8
*replay_counter
)
753 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
756 if (os_memcmp(replay_counter
, ctr
[i
].counter
,
757 WPA_REPLAY_COUNTER_LEN
) == 0)
764 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter
*ctr
,
765 const u8
*replay_counter
)
768 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
770 (replay_counter
== NULL
||
771 os_memcmp(replay_counter
, ctr
[i
].counter
,
772 WPA_REPLAY_COUNTER_LEN
) == 0))
773 ctr
[i
].valid
= FALSE
;
778 #ifdef CONFIG_IEEE80211R_AP
779 static int ft_check_msg_2_of_4(struct wpa_authenticator
*wpa_auth
,
780 struct wpa_state_machine
*sm
,
781 struct wpa_eapol_ie_parse
*kde
)
783 struct wpa_ie_data ie
;
784 struct rsn_mdie
*mdie
;
786 if (wpa_parse_wpa_ie_rsn(kde
->rsn_ie
, kde
->rsn_ie_len
, &ie
) < 0 ||
787 ie
.num_pmkid
!= 1 || ie
.pmkid
== NULL
) {
788 wpa_printf(MSG_DEBUG
, "FT: No PMKR1Name in "
789 "FT 4-way handshake message 2/4");
793 os_memcpy(sm
->sup_pmk_r1_name
, ie
.pmkid
, PMKID_LEN
);
794 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Supplicant",
795 sm
->sup_pmk_r1_name
, PMKID_LEN
);
797 if (!kde
->mdie
|| !kde
->ftie
) {
798 wpa_printf(MSG_DEBUG
, "FT: No %s in FT 4-way handshake "
799 "message 2/4", kde
->mdie
? "FTIE" : "MDIE");
803 mdie
= (struct rsn_mdie
*) (kde
->mdie
+ 2);
804 if (kde
->mdie
[1] < sizeof(struct rsn_mdie
) ||
805 os_memcmp(wpa_auth
->conf
.mobility_domain
, mdie
->mobility_domain
,
806 MOBILITY_DOMAIN_ID_LEN
) != 0) {
807 wpa_printf(MSG_DEBUG
, "FT: MDIE mismatch");
811 if (sm
->assoc_resp_ftie
&&
812 (kde
->ftie
[1] != sm
->assoc_resp_ftie
[1] ||
813 os_memcmp(kde
->ftie
, sm
->assoc_resp_ftie
,
814 2 + sm
->assoc_resp_ftie
[1]) != 0)) {
815 wpa_printf(MSG_DEBUG
, "FT: FTIE mismatch");
816 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 2/4",
817 kde
->ftie
, kde
->ftie_len
);
818 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)AssocResp",
819 sm
->assoc_resp_ftie
, 2 + sm
->assoc_resp_ftie
[1]);
825 #endif /* CONFIG_IEEE80211R_AP */
828 static int wpa_receive_error_report(struct wpa_authenticator
*wpa_auth
,
829 struct wpa_state_machine
*sm
, int group
)
831 /* Supplicant reported a Michael MIC error */
832 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
833 "received EAPOL-Key Error Request "
834 "(STA detected Michael MIC failure (group=%d))",
837 if (group
&& wpa_auth
->conf
.wpa_group
!= WPA_CIPHER_TKIP
) {
838 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
839 "ignore Michael MIC failure report since "
840 "group cipher is not TKIP");
841 } else if (!group
&& sm
->pairwise
!= WPA_CIPHER_TKIP
) {
842 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
843 "ignore Michael MIC failure report since "
844 "pairwise cipher is not TKIP");
846 if (wpa_auth_mic_failure_report(wpa_auth
, sm
->addr
) > 0)
847 return 1; /* STA entry was removed */
848 sm
->dot11RSNAStatsTKIPRemoteMICFailures
++;
849 wpa_auth
->dot11RSNAStatsTKIPRemoteMICFailures
++;
853 * Error report is not a request for a new key handshake, but since
854 * Authenticator may do it, let's change the keys now anyway.
856 wpa_request_new_ptk(sm
);
861 static int wpa_try_alt_snonce(struct wpa_state_machine
*sm
, u8
*data
,
866 const u8
*pmk
= NULL
;
870 os_memset(&PTK
, 0, sizeof(PTK
));
872 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
873 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
874 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
875 sm
->p2p_dev_addr
, pmk
, &pmk_len
,
879 #ifdef CONFIG_IEEE80211R_AP
880 if (wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
881 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
882 sm
->xxkey_len
= pmk_len
;
884 #endif /* CONFIG_IEEE80211R_AP */
887 pmk_len
= sm
->pmk_len
;
890 if (wpa_derive_ptk(sm
, sm
->alt_SNonce
, pmk
, pmk_len
, &PTK
) < 0)
893 if (wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
894 data
, data_len
) == 0) {
895 if (sm
->PMK
!= pmk
) {
896 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
897 sm
->pmk_len
= pmk_len
;
903 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
904 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
))
909 wpa_printf(MSG_DEBUG
,
910 "WPA: Earlier SNonce did not result in matching MIC");
914 wpa_printf(MSG_DEBUG
,
915 "WPA: Earlier SNonce resulted in matching MIC");
916 sm
->alt_snonce_valid
= 0;
918 if (vlan_id
&& wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
919 wpa_auth_update_vlan(sm
->wpa_auth
, sm
->addr
, vlan_id
) < 0)
922 os_memcpy(sm
->SNonce
, sm
->alt_SNonce
, WPA_NONCE_LEN
);
923 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
924 sm
->PTK_valid
= TRUE
;
930 void wpa_receive(struct wpa_authenticator
*wpa_auth
,
931 struct wpa_state_machine
*sm
,
932 u8
*data
, size_t data_len
)
934 struct ieee802_1x_hdr
*hdr
;
935 struct wpa_eapol_key
*key
;
936 u16 key_info
, key_data_length
;
937 enum { PAIRWISE_2
, PAIRWISE_4
, GROUP_2
, REQUEST
} msg
;
939 struct wpa_eapol_ie_parse kde
;
941 size_t keyhdrlen
, mic_len
;
944 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
946 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL data", data
, data_len
);
948 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
949 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
951 if (data_len
< sizeof(*hdr
) + keyhdrlen
) {
952 wpa_printf(MSG_DEBUG
, "WPA: Ignore too short EAPOL-Key frame");
956 hdr
= (struct ieee802_1x_hdr
*) data
;
957 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
958 mic
= (u8
*) (key
+ 1);
959 key_info
= WPA_GET_BE16(key
->key_info
);
960 key_data
= mic
+ mic_len
+ 2;
961 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
962 wpa_printf(MSG_DEBUG
, "WPA: Received EAPOL-Key from " MACSTR
963 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
964 MAC2STR(sm
->addr
), key_info
, key
->type
,
965 (unsigned int) mic_len
, key_data_length
);
966 wpa_hexdump(MSG_MSGDUMP
,
967 "WPA: EAPOL-Key header (ending before Key MIC)",
969 wpa_hexdump(MSG_MSGDUMP
, "WPA: EAPOL-Key Key MIC",
971 if (key_data_length
> data_len
- sizeof(*hdr
) - keyhdrlen
) {
972 wpa_printf(MSG_INFO
, "WPA: Invalid EAPOL-Key frame - "
973 "key_data overflow (%d > %lu)",
975 (unsigned long) (data_len
- sizeof(*hdr
) -
980 if (sm
->wpa
== WPA_VERSION_WPA2
) {
981 if (key
->type
== EAPOL_KEY_TYPE_WPA
) {
983 * Some deployed station implementations seem to send
984 * msg 4/4 with incorrect type value in WPA2 mode.
986 wpa_printf(MSG_DEBUG
, "Workaround: Allow EAPOL-Key "
987 "with unexpected WPA type in RSN mode");
988 } else if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
989 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
990 "unexpected type %d in RSN mode",
995 if (key
->type
!= EAPOL_KEY_TYPE_WPA
) {
996 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
997 "unexpected type %d in WPA mode",
1003 wpa_hexdump(MSG_DEBUG
, "WPA: Received Key Nonce", key
->key_nonce
,
1005 wpa_hexdump(MSG_DEBUG
, "WPA: Received Replay Counter",
1006 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1008 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1011 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
1012 wpa_printf(MSG_DEBUG
, "WPA: Ignore SMK message");
1016 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1019 } else if (!(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
1021 msgtxt
= "2/2 Group";
1022 } else if (key_data_length
== 0 ||
1023 (mic_len
== 0 && (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) &&
1024 key_data_length
== AES_BLOCK_SIZE
)) {
1026 msgtxt
= "4/4 Pairwise";
1029 msgtxt
= "2/4 Pairwise";
1032 if (msg
== REQUEST
|| msg
== PAIRWISE_2
|| msg
== PAIRWISE_4
||
1034 u16 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
1035 if (sm
->pairwise
== WPA_CIPHER_CCMP
||
1036 sm
->pairwise
== WPA_CIPHER_GCMP
) {
1037 if (wpa_use_cmac(sm
->wpa_key_mgmt
) &&
1038 !wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1039 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1040 wpa_auth_logger(wpa_auth
, sm
->addr
,
1042 "advertised support for "
1043 "AES-128-CMAC, but did not "
1048 if (!wpa_use_cmac(sm
->wpa_key_mgmt
) &&
1049 !wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1050 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1051 wpa_auth_logger(wpa_auth
, sm
->addr
,
1053 "did not use HMAC-SHA1-AES "
1059 if (wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1060 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
1061 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1062 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1067 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1068 if (sm
->req_replay_counter_used
&&
1069 os_memcmp(key
->replay_counter
, sm
->req_replay_counter
,
1070 WPA_REPLAY_COUNTER_LEN
) <= 0) {
1071 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1072 "received EAPOL-Key request with "
1073 "replayed counter");
1078 if (!(key_info
& WPA_KEY_INFO_REQUEST
) &&
1079 !wpa_replay_counter_valid(sm
->key_replay
, key
->replay_counter
)) {
1082 if (msg
== PAIRWISE_2
&&
1083 wpa_replay_counter_valid(sm
->prev_key_replay
,
1084 key
->replay_counter
) &&
1085 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1086 os_memcmp(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0)
1089 * Some supplicant implementations (e.g., Windows XP
1090 * WZC) update SNonce for each EAPOL-Key 2/4. This
1091 * breaks the workaround on accepting any of the
1092 * pending requests, so allow the SNonce to be updated
1093 * even if we have already sent out EAPOL-Key 3/4.
1095 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1096 "Process SNonce update from STA "
1097 "based on retransmitted EAPOL-Key "
1099 sm
->update_snonce
= 1;
1100 os_memcpy(sm
->alt_SNonce
, sm
->SNonce
, WPA_NONCE_LEN
);
1101 sm
->alt_snonce_valid
= TRUE
;
1102 os_memcpy(sm
->alt_replay_counter
,
1103 sm
->key_replay
[0].counter
,
1104 WPA_REPLAY_COUNTER_LEN
);
1105 goto continue_processing
;
1108 if (msg
== PAIRWISE_4
&& sm
->alt_snonce_valid
&&
1109 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1110 os_memcmp(key
->replay_counter
, sm
->alt_replay_counter
,
1111 WPA_REPLAY_COUNTER_LEN
) == 0) {
1113 * Supplicant may still be using the old SNonce since
1114 * there was two EAPOL-Key 2/4 messages and they had
1115 * different SNonce values.
1117 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1118 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1119 goto continue_processing
;
1122 if (msg
== PAIRWISE_2
&&
1123 wpa_replay_counter_valid(sm
->prev_key_replay
,
1124 key
->replay_counter
) &&
1125 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
) {
1126 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1127 "ignore retransmitted EAPOL-Key %s - "
1128 "SNonce did not change", msgtxt
);
1130 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1131 "received EAPOL-Key %s with "
1132 "unexpected replay counter", msgtxt
);
1134 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
1135 if (!sm
->key_replay
[i
].valid
)
1137 wpa_hexdump(MSG_DEBUG
, "pending replay counter",
1138 sm
->key_replay
[i
].counter
,
1139 WPA_REPLAY_COUNTER_LEN
);
1141 wpa_hexdump(MSG_DEBUG
, "received replay counter",
1142 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1146 continue_processing
:
1148 if (sm
->wpa
== WPA_VERSION_WPA2
&& mic_len
== 0 &&
1149 !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1150 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1151 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1154 #endif /* CONFIG_FILS */
1158 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKSTART
&&
1159 sm
->wpa_ptk_state
!= WPA_PTK_PTKCALCNEGOTIATING
&&
1160 (!sm
->update_snonce
||
1161 sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
)) {
1162 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1163 "received EAPOL-Key msg 2/4 in "
1164 "invalid state (%d) - dropped",
1168 random_add_randomness(key
->key_nonce
, WPA_NONCE_LEN
);
1169 if (sm
->group
->reject_4way_hs_for_entropy
) {
1171 * The system did not have enough entropy to generate
1172 * strong random numbers. Reject the first 4-way
1173 * handshake(s) and collect some entropy based on the
1174 * information from it. Once enough entropy is
1175 * available, the next atempt will trigger GMK/Key
1176 * Counter update and the station will be allowed to
1179 wpa_printf(MSG_DEBUG
, "WPA: Reject 4-way handshake to "
1180 "collect more entropy for random number "
1182 random_mark_pool_ready();
1183 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
1184 WLAN_REASON_PREV_AUTH_NOT_VALID
);
1189 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
||
1191 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1192 "received EAPOL-Key msg 4/4 in "
1193 "invalid state (%d) - dropped",
1199 if (sm
->wpa_ptk_group_state
!= WPA_PTK_GROUP_REKEYNEGOTIATING
1200 || !sm
->PTK_valid
) {
1201 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1202 "received EAPOL-Key msg 2/2 in "
1203 "invalid state (%d) - dropped",
1204 sm
->wpa_ptk_group_state
);
1212 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1213 "received EAPOL-Key frame (%s)", msgtxt
);
1215 if (key_info
& WPA_KEY_INFO_ACK
) {
1216 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1217 "received invalid EAPOL-Key: Key Ack set");
1221 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1222 !(key_info
& WPA_KEY_INFO_MIC
)) {
1223 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1224 "received invalid EAPOL-Key: Key MIC not set");
1229 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1230 (key_info
& WPA_KEY_INFO_MIC
)) {
1231 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1232 "received invalid EAPOL-Key: Key MIC set");
1235 #endif /* CONFIG_FILS */
1237 sm
->MICVerified
= FALSE
;
1238 if (sm
->PTK_valid
&& !sm
->update_snonce
) {
1240 wpa_verify_key_mic(sm
->wpa_key_mgmt
, sm
->pmk_len
, &sm
->PTK
,
1242 (msg
!= PAIRWISE_4
|| !sm
->alt_snonce_valid
||
1243 wpa_try_alt_snonce(sm
, data
, data_len
))) {
1244 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1245 "received EAPOL-Key with invalid MIC");
1247 wpa_printf(MSG_INFO
,
1248 "TEST: Ignore Key MIC failure for fuzz testing");
1250 #endif /* TEST_FUZZ */
1255 wpa_aead_decrypt(sm
, &sm
->PTK
, data
, data_len
,
1256 &key_data_length
) < 0) {
1257 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1258 "received EAPOL-Key with invalid MIC");
1260 wpa_printf(MSG_INFO
,
1261 "TEST: Ignore Key MIC failure for fuzz testing");
1263 #endif /* TEST_FUZZ */
1266 #endif /* CONFIG_FILS */
1269 #endif /* TEST_FUZZ */
1270 sm
->MICVerified
= TRUE
;
1271 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
1272 sm
->pending_1_of_4_timeout
= 0;
1275 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1276 if (sm
->MICVerified
) {
1277 sm
->req_replay_counter_used
= 1;
1278 os_memcpy(sm
->req_replay_counter
, key
->replay_counter
,
1279 WPA_REPLAY_COUNTER_LEN
);
1281 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1282 "received EAPOL-Key request with "
1288 * TODO: should decrypt key data field if encryption was used;
1289 * even though MAC address KDE is not normally encrypted,
1290 * supplicant is allowed to encrypt it.
1292 if (key_info
& WPA_KEY_INFO_ERROR
) {
1293 if (wpa_receive_error_report(
1295 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) > 0)
1296 return; /* STA entry was removed */
1297 } else if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
1298 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1299 "received EAPOL-Key Request for new "
1301 wpa_request_new_ptk(sm
);
1302 } else if (key_data_length
> 0 &&
1303 wpa_parse_kde_ies(key_data
, key_data_length
,
1307 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1308 "received EAPOL-Key Request for GTK "
1310 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
1311 wpa_rekey_gtk(wpa_auth
, NULL
);
1314 /* Do not allow the same key replay counter to be reused. */
1315 wpa_replay_counter_mark_invalid(sm
->key_replay
,
1316 key
->replay_counter
);
1318 if (msg
== PAIRWISE_2
) {
1320 * Maintain a copy of the pending EAPOL-Key frames in
1321 * case the EAPOL-Key frame was retransmitted. This is
1322 * needed to allow EAPOL-Key msg 2/4 reply to another
1323 * pending msg 1/4 to update the SNonce to work around
1324 * unexpected supplicant behavior.
1326 os_memcpy(sm
->prev_key_replay
, sm
->key_replay
,
1327 sizeof(sm
->key_replay
));
1329 os_memset(sm
->prev_key_replay
, 0,
1330 sizeof(sm
->prev_key_replay
));
1334 * Make sure old valid counters are not accepted anymore and
1335 * do not get copied again.
1337 wpa_replay_counter_mark_invalid(sm
->key_replay
, NULL
);
1340 os_free(sm
->last_rx_eapol_key
);
1341 sm
->last_rx_eapol_key
= os_memdup(data
, data_len
);
1342 if (sm
->last_rx_eapol_key
== NULL
)
1344 sm
->last_rx_eapol_key_len
= data_len
;
1346 sm
->rx_eapol_key_secure
= !!(key_info
& WPA_KEY_INFO_SECURE
);
1347 sm
->EAPOLKeyReceived
= TRUE
;
1348 sm
->EAPOLKeyPairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1349 sm
->EAPOLKeyRequest
= !!(key_info
& WPA_KEY_INFO_REQUEST
);
1350 os_memcpy(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
);
1355 static int wpa_gmk_to_gtk(const u8
*gmk
, const char *label
, const u8
*addr
,
1356 const u8
*gnonce
, u8
*gtk
, size_t gtk_len
)
1358 u8 data
[ETH_ALEN
+ WPA_NONCE_LEN
+ 8 + WPA_GTK_MAX_LEN
];
1362 /* GTK = PRF-X(GMK, "Group key expansion",
1363 * AA || GNonce || Time || random data)
1364 * The example described in the IEEE 802.11 standard uses only AA and
1365 * GNonce as inputs here. Add some more entropy since this derivation
1366 * is done only at the Authenticator and as such, does not need to be
1369 os_memset(data
, 0, sizeof(data
));
1370 os_memcpy(data
, addr
, ETH_ALEN
);
1371 os_memcpy(data
+ ETH_ALEN
, gnonce
, WPA_NONCE_LEN
);
1372 pos
= data
+ ETH_ALEN
+ WPA_NONCE_LEN
;
1373 wpa_get_ntp_timestamp(pos
);
1375 os_memset(pos
, 0xef, 8);
1376 #endif /* TEST_FUZZ */
1378 if (random_get_bytes(pos
, gtk_len
) < 0)
1381 #ifdef CONFIG_SHA384
1382 if (sha384_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1385 #else /* CONFIG_SHA384 */
1386 #ifdef CONFIG_SHA256
1387 if (sha256_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1390 #else /* CONFIG_SHA256 */
1391 if (sha1_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1394 #endif /* CONFIG_SHA256 */
1395 #endif /* CONFIG_SHA384 */
1401 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
)
1403 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1404 struct wpa_state_machine
*sm
= timeout_ctx
;
1406 sm
->pending_1_of_4_timeout
= 0;
1407 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "EAPOL-Key timeout");
1408 sm
->TimeoutEvt
= TRUE
;
1413 void __wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1414 struct wpa_state_machine
*sm
, int key_info
,
1415 const u8
*key_rsc
, const u8
*nonce
,
1416 const u8
*kde
, size_t kde_len
,
1417 int keyidx
, int encr
, int force_version
)
1419 struct ieee802_1x_hdr
*hdr
;
1420 struct wpa_eapol_key
*key
;
1421 size_t len
, mic_len
, keyhdrlen
;
1423 int key_data_len
, pad_len
= 0;
1425 int version
, pairwise
;
1427 u8
*key_mic
, *key_data
;
1429 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
1430 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
1432 len
= sizeof(struct ieee802_1x_hdr
) + keyhdrlen
;
1435 version
= force_version
;
1436 else if (wpa_use_akm_defined(sm
->wpa_key_mgmt
))
1437 version
= WPA_KEY_INFO_TYPE_AKM_DEFINED
;
1438 else if (wpa_use_cmac(sm
->wpa_key_mgmt
))
1439 version
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
1440 else if (sm
->pairwise
!= WPA_CIPHER_TKIP
)
1441 version
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
1443 version
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
1445 pairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1447 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1448 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1451 (key_info
& WPA_KEY_INFO_SECURE
) ? 1 : 0,
1452 (key_info
& WPA_KEY_INFO_MIC
) ? 1 : 0,
1453 (key_info
& WPA_KEY_INFO_ACK
) ? 1 : 0,
1454 (key_info
& WPA_KEY_INFO_INSTALL
) ? 1 : 0,
1455 pairwise
, (unsigned long) kde_len
, keyidx
, encr
);
1457 key_data_len
= kde_len
;
1459 if ((version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1460 wpa_use_aes_key_wrap(sm
->wpa_key_mgmt
) ||
1461 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) && encr
) {
1462 pad_len
= key_data_len
% 8;
1464 pad_len
= 8 - pad_len
;
1465 key_data_len
+= pad_len
+ 8;
1468 len
+= key_data_len
;
1469 if (!mic_len
&& encr
)
1470 len
+= AES_BLOCK_SIZE
;
1472 hdr
= os_zalloc(len
);
1475 hdr
->version
= wpa_auth
->conf
.eapol_version
;
1476 hdr
->type
= IEEE802_1X_TYPE_EAPOL_KEY
;
1477 hdr
->length
= host_to_be16(len
- sizeof(*hdr
));
1478 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1479 key_mic
= (u8
*) (key
+ 1);
1480 key_data
= ((u8
*) (hdr
+ 1)) + keyhdrlen
;
1482 key
->type
= sm
->wpa
== WPA_VERSION_WPA2
?
1483 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1484 key_info
|= version
;
1485 if (encr
&& sm
->wpa
== WPA_VERSION_WPA2
)
1486 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1487 if (sm
->wpa
!= WPA_VERSION_WPA2
)
1488 key_info
|= keyidx
<< WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1489 WPA_PUT_BE16(key
->key_info
, key_info
);
1491 alg
= pairwise
? sm
->pairwise
: wpa_auth
->conf
.wpa_group
;
1492 if (sm
->wpa
== WPA_VERSION_WPA2
&& !pairwise
)
1493 WPA_PUT_BE16(key
->key_length
, 0);
1495 WPA_PUT_BE16(key
->key_length
, wpa_cipher_key_len(alg
));
1497 for (i
= RSNA_MAX_EAPOL_RETRIES
- 1; i
> 0; i
--) {
1498 sm
->key_replay
[i
].valid
= sm
->key_replay
[i
- 1].valid
;
1499 os_memcpy(sm
->key_replay
[i
].counter
,
1500 sm
->key_replay
[i
- 1].counter
,
1501 WPA_REPLAY_COUNTER_LEN
);
1503 inc_byte_array(sm
->key_replay
[0].counter
, WPA_REPLAY_COUNTER_LEN
);
1504 os_memcpy(key
->replay_counter
, sm
->key_replay
[0].counter
,
1505 WPA_REPLAY_COUNTER_LEN
);
1506 wpa_hexdump(MSG_DEBUG
, "WPA: Replay Counter",
1507 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1508 sm
->key_replay
[0].valid
= TRUE
;
1511 os_memcpy(key
->key_nonce
, nonce
, WPA_NONCE_LEN
);
1514 os_memcpy(key
->key_rsc
, key_rsc
, WPA_KEY_RSC_LEN
);
1517 os_memcpy(key_data
, kde
, kde_len
);
1518 WPA_PUT_BE16(key_mic
+ mic_len
, kde_len
);
1520 } else if (!mic_len
&& kde
) {
1524 WPA_PUT_BE16(key_mic
, AES_BLOCK_SIZE
+ kde_len
);
1525 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1528 wpa_hexdump_key(MSG_DEBUG
, "WPA: KEK",
1529 sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1530 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1531 * to Key Data (exclusive). */
1532 aad
[0] = (u8
*) hdr
;
1533 aad_len
[0] = key_mic
+ 2 - (u8
*) hdr
;
1534 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, kde
, kde_len
,
1535 1, aad
, aad_len
, key_mic
+ 2) < 0) {
1536 wpa_printf(MSG_DEBUG
, "WPA: AES-SIV encryption failed");
1540 wpa_hexdump(MSG_DEBUG
, "WPA: Encrypted Key Data from SIV",
1541 key_mic
+ 2, AES_BLOCK_SIZE
+ kde_len
);
1542 #endif /* CONFIG_FILS */
1543 } else if (encr
&& kde
) {
1544 buf
= os_zalloc(key_data_len
);
1550 os_memcpy(pos
, kde
, kde_len
);
1556 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1558 if (version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1559 wpa_use_aes_key_wrap(sm
->wpa_key_mgmt
) ||
1560 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1561 wpa_printf(MSG_DEBUG
,
1562 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1563 (unsigned int) sm
->PTK
.kek_len
);
1564 if (aes_wrap(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
1565 (key_data_len
- 8) / 8, buf
, key_data
)) {
1570 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1571 #ifndef CONFIG_NO_RC4
1572 } else if (sm
->PTK
.kek_len
== 16) {
1575 wpa_printf(MSG_DEBUG
,
1576 "WPA: Encrypt Key Data using RC4");
1577 os_memcpy(key
->key_iv
,
1578 sm
->group
->Counter
+ WPA_NONCE_LEN
- 16, 16);
1579 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1580 os_memcpy(ek
, key
->key_iv
, 16);
1581 os_memcpy(ek
+ 16, sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1582 os_memcpy(key_data
, buf
, key_data_len
);
1583 rc4_skip(ek
, 32, 256, key_data
, key_data_len
);
1584 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1585 #endif /* CONFIG_NO_RC4 */
1594 if (key_info
& WPA_KEY_INFO_MIC
) {
1595 if (!sm
->PTK_valid
|| !mic_len
) {
1596 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1597 "PTK not valid when sending EAPOL-Key "
1603 if (wpa_eapol_key_mic(sm
->PTK
.kck
, sm
->PTK
.kck_len
,
1604 sm
->wpa_key_mgmt
, version
,
1605 (u8
*) hdr
, len
, key_mic
) < 0) {
1609 #ifdef CONFIG_TESTING_OPTIONS
1611 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
> 0.0 &&
1613 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
) {
1614 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1615 "Corrupting group EAPOL-Key Key MIC");
1618 #endif /* CONFIG_TESTING_OPTIONS */
1621 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_inc_EapolFramesTx
,
1623 wpa_auth_send_eapol(wpa_auth
, sm
->addr
, (u8
*) hdr
, len
,
1629 static void wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1630 struct wpa_state_machine
*sm
, int key_info
,
1631 const u8
*key_rsc
, const u8
*nonce
,
1632 const u8
*kde
, size_t kde_len
,
1633 int keyidx
, int encr
)
1636 int pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
1642 __wpa_send_eapol(wpa_auth
, sm
, key_info
, key_rsc
, nonce
, kde
, kde_len
,
1645 ctr
= pairwise
? sm
->TimeoutCtr
: sm
->GTimeoutCtr
;
1646 if (ctr
== 1 && wpa_auth
->conf
.tx_status
)
1647 timeout_ms
= pairwise
? eapol_key_timeout_first
:
1648 eapol_key_timeout_first_group
;
1650 timeout_ms
= eapol_key_timeout_subseq
;
1651 if (wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
1652 (!pairwise
|| (key_info
& WPA_KEY_INFO_MIC
)))
1653 timeout_ms
= eapol_key_timeout_no_retrans
;
1654 if (pairwise
&& ctr
== 1 && !(key_info
& WPA_KEY_INFO_MIC
))
1655 sm
->pending_1_of_4_timeout
= 1;
1658 #endif /* TEST_FUZZ */
1659 wpa_printf(MSG_DEBUG
, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1660 "counter %u)", timeout_ms
, ctr
);
1661 eloop_register_timeout(timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000,
1662 wpa_send_eapol_timeout
, wpa_auth
, sm
);
1666 static int wpa_verify_key_mic(int akmp
, size_t pmk_len
, struct wpa_ptk
*PTK
,
1667 u8
*data
, size_t data_len
)
1669 struct ieee802_1x_hdr
*hdr
;
1670 struct wpa_eapol_key
*key
;
1673 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
], *mic_pos
;
1674 size_t mic_len
= wpa_mic_len(akmp
, pmk_len
);
1676 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
1679 hdr
= (struct ieee802_1x_hdr
*) data
;
1680 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1681 mic_pos
= (u8
*) (key
+ 1);
1682 key_info
= WPA_GET_BE16(key
->key_info
);
1683 os_memcpy(mic
, mic_pos
, mic_len
);
1684 os_memset(mic_pos
, 0, mic_len
);
1685 if (wpa_eapol_key_mic(PTK
->kck
, PTK
->kck_len
, akmp
,
1686 key_info
& WPA_KEY_INFO_TYPE_MASK
,
1687 data
, data_len
, mic_pos
) ||
1688 os_memcmp_const(mic
, mic_pos
, mic_len
) != 0)
1690 os_memcpy(mic_pos
, mic
, mic_len
);
1695 void wpa_remove_ptk(struct wpa_state_machine
*sm
)
1697 sm
->PTK_valid
= FALSE
;
1698 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1699 if (wpa_auth_set_key(sm
->wpa_auth
, 0, WPA_ALG_NONE
, sm
->addr
, 0, NULL
,
1701 wpa_printf(MSG_DEBUG
,
1702 "RSN: PTK removal from the driver failed");
1703 sm
->pairwise_set
= FALSE
;
1704 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1708 int wpa_auth_sm_event(struct wpa_state_machine
*sm
, enum wpa_event event
)
1715 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1716 "event %d notification", event
);
1721 /* PTKs are derived through AMPE */
1722 if (wpa_auth_start_ampe(sm
->wpa_auth
, sm
->addr
)) {
1727 #endif /* CONFIG_MESH */
1732 sm
->DeauthenticationRequest
= TRUE
;
1735 case WPA_REAUTH_EAPOL
:
1738 * When using WPS, we may end up here if the STA
1739 * manages to re-associate without the previous STA
1740 * entry getting removed. Consequently, we need to make
1741 * sure that the WPA state machines gets initialized
1742 * properly at this point.
1744 wpa_printf(MSG_DEBUG
, "WPA state machine had not been "
1745 "started - initialize now");
1748 if (wpa_sm_step(sm
) == 1)
1749 return 1; /* should not really happen */
1751 sm
->AuthenticationRequest
= TRUE
;
1754 if (sm
->GUpdateStationKeys
) {
1756 * Reauthentication cancels the pending group key
1757 * update for this STA.
1759 sm
->group
->GKeyDoneStations
--;
1760 sm
->GUpdateStationKeys
= FALSE
;
1761 sm
->PtkGroupInit
= TRUE
;
1763 sm
->ReAuthenticationRequest
= TRUE
;
1766 #ifdef CONFIG_IEEE80211R_AP
1767 wpa_printf(MSG_DEBUG
, "FT: Retry PTK configuration "
1768 "after association");
1769 wpa_ft_install_ptk(sm
);
1771 /* Using FT protocol, not WPA auth state machine */
1772 sm
->ft_completed
= 1;
1774 #else /* CONFIG_IEEE80211R_AP */
1776 #endif /* CONFIG_IEEE80211R_AP */
1777 case WPA_ASSOC_FILS
:
1779 wpa_printf(MSG_DEBUG
,
1780 "FILS: TK configuration after association");
1782 sm
->fils_completed
= 1;
1784 #else /* CONFIG_FILS */
1786 #endif /* CONFIG_FILS */
1787 case WPA_DRV_STA_REMOVED
:
1788 sm
->tk_already_set
= FALSE
;
1792 #ifdef CONFIG_IEEE80211R_AP
1793 sm
->ft_completed
= 0;
1794 #endif /* CONFIG_IEEE80211R_AP */
1796 #ifdef CONFIG_IEEE80211W
1797 if (sm
->mgmt_frame_prot
&& event
== WPA_AUTH
)
1799 #endif /* CONFIG_IEEE80211W */
1801 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1802 (event
== WPA_AUTH
|| event
== WPA_ASSOC
))
1804 #endif /* CONFIG_FILS */
1807 sm
->PTK_valid
= FALSE
;
1808 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1810 if (event
!= WPA_REAUTH_EAPOL
)
1814 if (sm
->in_step_loop
) {
1816 * wpa_sm_step() is already running - avoid recursive call to
1817 * it by making the existing loop process the new update.
1822 return wpa_sm_step(sm
);
1826 SM_STATE(WPA_PTK
, INITIALIZE
)
1828 SM_ENTRY_MA(WPA_PTK
, INITIALIZE
, wpa_ptk
);
1830 /* Init flag is not cleared here, so avoid busy
1831 * loop by claiming nothing changed. */
1832 sm
->changed
= FALSE
;
1836 if (sm
->GUpdateStationKeys
)
1837 sm
->group
->GKeyDoneStations
--;
1838 sm
->GUpdateStationKeys
= FALSE
;
1839 if (sm
->wpa
== WPA_VERSION_WPA
)
1840 sm
->PInitAKeys
= FALSE
;
1841 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1842 * Local AA > Remote AA)) */) {
1845 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 0);
1847 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
, 0);
1849 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
1850 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1851 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
1852 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1853 WPA_EAPOL_authorized
, 0);
1858 SM_STATE(WPA_PTK
, DISCONNECT
)
1860 u16 reason
= sm
->disconnect_reason
;
1862 SM_ENTRY_MA(WPA_PTK
, DISCONNECT
, wpa_ptk
);
1863 sm
->Disconnect
= FALSE
;
1864 sm
->disconnect_reason
= 0;
1866 reason
= WLAN_REASON_PREV_AUTH_NOT_VALID
;
1867 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
, reason
);
1871 SM_STATE(WPA_PTK
, DISCONNECTED
)
1873 SM_ENTRY_MA(WPA_PTK
, DISCONNECTED
, wpa_ptk
);
1874 sm
->DeauthenticationRequest
= FALSE
;
1878 SM_STATE(WPA_PTK
, AUTHENTICATION
)
1880 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION
, wpa_ptk
);
1881 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1882 sm
->PTK_valid
= FALSE
;
1883 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portControl_Auto
,
1885 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 1);
1886 sm
->AuthenticationRequest
= FALSE
;
1890 static void wpa_group_ensure_init(struct wpa_authenticator
*wpa_auth
,
1891 struct wpa_group
*group
)
1893 if (group
->first_sta_seen
)
1896 * System has run bit further than at the time hostapd was started
1897 * potentially very early during boot up. This provides better chances
1898 * of collecting more randomness on embedded systems. Re-initialize the
1899 * GMK and Counter here to improve their strength if there was not
1900 * enough entropy available immediately after system startup.
1902 wpa_printf(MSG_DEBUG
, "WPA: Re-initialize GMK/Counter on first "
1904 if (random_pool_ready() != 1) {
1905 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
1906 "to proceed - reject first 4-way handshake");
1907 group
->reject_4way_hs_for_entropy
= TRUE
;
1909 group
->first_sta_seen
= TRUE
;
1910 group
->reject_4way_hs_for_entropy
= FALSE
;
1913 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0 ||
1914 wpa_gtk_update(wpa_auth
, group
) < 0 ||
1915 wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
1916 wpa_printf(MSG_INFO
, "WPA: GMK/GTK setup failed");
1917 group
->first_sta_seen
= FALSE
;
1918 group
->reject_4way_hs_for_entropy
= TRUE
;
1923 SM_STATE(WPA_PTK
, AUTHENTICATION2
)
1925 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION2
, wpa_ptk
);
1927 wpa_group_ensure_init(sm
->wpa_auth
, sm
->group
);
1928 sm
->ReAuthenticationRequest
= FALSE
;
1931 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1932 * ambiguous. The Authenticator state machine uses a counter that is
1933 * incremented by one for each 4-way handshake. However, the security
1934 * analysis of 4-way handshake points out that unpredictable nonces
1935 * help in preventing precomputation attacks. Instead of the state
1936 * machine definition, use an unpredictable nonce value here to provide
1937 * stronger protection against potential precomputation attacks.
1939 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
1940 wpa_printf(MSG_ERROR
, "WPA: Failed to get random data for "
1942 sm
->Disconnect
= TRUE
;
1945 wpa_hexdump(MSG_DEBUG
, "WPA: Assign ANonce", sm
->ANonce
,
1947 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1948 * logical place than INITIALIZE since AUTHENTICATION2 can be
1949 * re-entered on ReAuthenticationRequest without going through
1955 static int wpa_auth_sm_ptk_update(struct wpa_state_machine
*sm
)
1957 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
1958 wpa_printf(MSG_ERROR
,
1959 "WPA: Failed to get random data for ANonce");
1960 sm
->Disconnect
= TRUE
;
1963 wpa_hexdump(MSG_DEBUG
, "WPA: Assign new ANonce", sm
->ANonce
,
1970 SM_STATE(WPA_PTK
, INITPMK
)
1972 u8 msk
[2 * PMK_LEN
];
1973 size_t len
= 2 * PMK_LEN
;
1975 SM_ENTRY_MA(WPA_PTK
, INITPMK
, wpa_ptk
);
1976 #ifdef CONFIG_IEEE80211R_AP
1978 #endif /* CONFIG_IEEE80211R_AP */
1980 wpa_printf(MSG_DEBUG
, "WPA: PMK from PMKSA cache");
1981 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
1982 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
1984 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
) {
1985 wpa_printf(MSG_DEBUG
,
1986 "DPP: No PMKSA cache entry for STA - reject connection");
1987 sm
->Disconnect
= TRUE
;
1988 sm
->disconnect_reason
= WLAN_REASON_INVALID_PMKID
;
1990 #endif /* CONFIG_DPP */
1991 } else if (wpa_auth_get_msk(sm
->wpa_auth
, sm
->addr
, msk
, &len
) == 0) {
1992 unsigned int pmk_len
;
1994 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
))
1995 pmk_len
= PMK_LEN_SUITE_B_192
;
1998 wpa_printf(MSG_DEBUG
, "WPA: PMK from EAPOL state machine "
1999 "(MSK len=%lu PMK len=%u)", (unsigned long) len
,
2001 if (len
< pmk_len
) {
2002 wpa_printf(MSG_DEBUG
,
2003 "WPA: MSK not long enough (%u) to create PMK (%u)",
2004 (unsigned int) len
, (unsigned int) pmk_len
);
2005 sm
->Disconnect
= TRUE
;
2008 os_memcpy(sm
->PMK
, msk
, pmk_len
);
2009 sm
->pmk_len
= pmk_len
;
2010 #ifdef CONFIG_IEEE80211R_AP
2011 if (len
>= 2 * PMK_LEN
) {
2012 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
2013 os_memcpy(sm
->xxkey
, msk
, SHA384_MAC_LEN
);
2014 sm
->xxkey_len
= SHA384_MAC_LEN
;
2016 os_memcpy(sm
->xxkey
, msk
+ PMK_LEN
, PMK_LEN
);
2017 sm
->xxkey_len
= PMK_LEN
;
2020 #endif /* CONFIG_IEEE80211R_AP */
2022 wpa_printf(MSG_DEBUG
, "WPA: Could not get PMK, get_msk: %p",
2023 sm
->wpa_auth
->cb
->get_msk
);
2024 sm
->Disconnect
= TRUE
;
2027 os_memset(msk
, 0, sizeof(msk
));
2029 sm
->req_replay_counter_used
= 0;
2030 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
2031 * will break reauthentication since EAPOL state machines may not be
2032 * get into AUTHENTICATING state that clears keyRun before WPA state
2033 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2034 * state and takes PMK from the previously used AAA Key. This will
2035 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2036 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2037 * be good workaround for this issue. */
2038 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyRun
, 0);
2042 SM_STATE(WPA_PTK
, INITPSK
)
2047 SM_ENTRY_MA(WPA_PTK
, INITPSK
, wpa_ptk
);
2048 psk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
, NULL
,
2051 os_memcpy(sm
->PMK
, psk
, psk_len
);
2052 sm
->pmk_len
= psk_len
;
2053 #ifdef CONFIG_IEEE80211R_AP
2054 os_memcpy(sm
->xxkey
, psk
, PMK_LEN
);
2055 sm
->xxkey_len
= PMK_LEN
;
2056 #endif /* CONFIG_IEEE80211R_AP */
2059 if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
2060 wpa_printf(MSG_DEBUG
, "SAE: PMK from PMKSA cache");
2061 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2062 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
2064 #endif /* CONFIG_SAE */
2065 sm
->req_replay_counter_used
= 0;
2069 SM_STATE(WPA_PTK
, PTKSTART
)
2071 u8 buf
[2 + RSN_SELECTOR_LEN
+ PMKID_LEN
], *pmkid
= NULL
;
2072 size_t pmkid_len
= 0;
2074 SM_ENTRY_MA(WPA_PTK
, PTKSTART
, wpa_ptk
);
2075 sm
->PTKRequest
= FALSE
;
2076 sm
->TimeoutEvt
= FALSE
;
2077 sm
->alt_snonce_valid
= FALSE
;
2080 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
2081 /* No point in sending the EAPOL-Key - we will disconnect
2082 * immediately following this. */
2086 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2087 "sending 1/4 msg of 4-Way Handshake");
2089 * For infrastructure BSS cases, it is better for the AP not to include
2090 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2091 * offline search for the passphrase/PSK without having to be able to
2092 * capture a 4-way handshake from a STA that has access to the network.
2094 * For IBSS cases, addition of PMKID KDE could be considered even with
2095 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2096 * possible PSK for this STA. However, this should not be done unless
2097 * there is support for using that information on the supplicant side.
2098 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2099 * cases would also apply here, but at least in the IBSS case, this
2100 * would cover a potential real use case.
2102 if (sm
->wpa
== WPA_VERSION_WPA2
&&
2103 (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) ||
2104 (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
&& sm
->pmksa
) ||
2105 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) &&
2106 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
) {
2108 pmkid_len
= 2 + RSN_SELECTOR_LEN
+ PMKID_LEN
;
2109 pmkid
[0] = WLAN_EID_VENDOR_SPECIFIC
;
2110 pmkid
[1] = RSN_SELECTOR_LEN
+ PMKID_LEN
;
2111 RSN_SELECTOR_PUT(&pmkid
[2], RSN_KEY_DATA_PMKID
);
2113 wpa_hexdump(MSG_DEBUG
,
2114 "RSN: Message 1/4 PMKID from PMKSA entry",
2115 sm
->pmksa
->pmkid
, PMKID_LEN
);
2116 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2117 sm
->pmksa
->pmkid
, PMKID_LEN
);
2118 } else if (wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
)) {
2119 /* No KCK available to derive PMKID */
2120 wpa_printf(MSG_DEBUG
,
2121 "RSN: No KCK available to derive PMKID for message 1/4");
2124 } else if (wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
2125 if (sm
->pmkid_set
) {
2126 wpa_hexdump(MSG_DEBUG
,
2127 "RSN: Message 1/4 PMKID from SAE",
2128 sm
->pmkid
, PMKID_LEN
);
2129 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2130 sm
->pmkid
, PMKID_LEN
);
2132 /* No PMKID available */
2133 wpa_printf(MSG_DEBUG
,
2134 "RSN: No SAE PMKID available for message 1/4");
2137 #endif /* CONFIG_SAE */
2140 * Calculate PMKID since no PMKSA cache entry was
2141 * available with pre-calculated PMKID.
2143 rsn_pmkid(sm
->PMK
, sm
->pmk_len
, sm
->wpa_auth
->addr
,
2144 sm
->addr
, &pmkid
[2 + RSN_SELECTOR_LEN
],
2146 wpa_hexdump(MSG_DEBUG
,
2147 "RSN: Message 1/4 PMKID derived from PMK",
2148 &pmkid
[2 + RSN_SELECTOR_LEN
], PMKID_LEN
);
2151 wpa_send_eapol(sm
->wpa_auth
, sm
,
2152 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
2153 sm
->ANonce
, pmkid
, pmkid_len
, 0, 0);
2157 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
2158 const u8
*pmk
, unsigned int pmk_len
,
2159 struct wpa_ptk
*ptk
)
2164 #ifdef CONFIG_IEEE80211R_AP
2165 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
2166 return wpa_auth_derive_ptk_ft(sm
, pmk
, ptk
);
2167 #endif /* CONFIG_IEEE80211R_AP */
2170 if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->dpp_z
) {
2171 z
= wpabuf_head(sm
->dpp_z
);
2172 z_len
= wpabuf_len(sm
->dpp_z
);
2174 #endif /* CONFIG_DPP2 */
2176 return wpa_pmk_to_ptk(pmk
, pmk_len
, "Pairwise key expansion",
2177 sm
->wpa_auth
->addr
, sm
->addr
, sm
->ANonce
, snonce
,
2178 ptk
, sm
->wpa_key_mgmt
, sm
->pairwise
, z
, z_len
);
2184 int fils_auth_pmk_to_ptk(struct wpa_state_machine
*sm
, const u8
*pmk
,
2185 size_t pmk_len
, const u8
*snonce
, const u8
*anonce
,
2186 const u8
*dhss
, size_t dhss_len
,
2187 struct wpabuf
*g_sta
, struct wpabuf
*g_ap
)
2189 u8 ick
[FILS_ICK_MAX_LEN
];
2192 u8 fils_ft
[FILS_FT_MAX_LEN
];
2193 size_t fils_ft_len
= 0;
2195 res
= fils_pmk_to_ptk(pmk
, pmk_len
, sm
->addr
, sm
->wpa_auth
->addr
,
2196 snonce
, anonce
, dhss
, dhss_len
,
2197 &sm
->PTK
, ick
, &ick_len
,
2198 sm
->wpa_key_mgmt
, sm
->pairwise
,
2199 fils_ft
, &fils_ft_len
);
2202 sm
->PTK_valid
= TRUE
;
2203 sm
->tk_already_set
= FALSE
;
2205 #ifdef CONFIG_IEEE80211R_AP
2207 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2208 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
2209 u8 pmk_r0
[PMK_LEN_MAX
], pmk_r0_name
[WPA_PMK_NAME_LEN
];
2210 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
2211 size_t pmk_r0_len
= use_sha384
? SHA384_MAC_LEN
: PMK_LEN
;
2213 if (wpa_derive_pmk_r0(fils_ft
, fils_ft_len
,
2214 conf
->ssid
, conf
->ssid_len
,
2215 conf
->mobility_domain
,
2216 conf
->r0_key_holder
,
2217 conf
->r0_key_holder_len
,
2218 sm
->addr
, pmk_r0
, pmk_r0_name
,
2222 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: PMK-R0",
2223 pmk_r0
, pmk_r0_len
);
2224 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR0Name",
2225 pmk_r0_name
, WPA_PMK_NAME_LEN
);
2226 wpa_ft_store_pmk_fils(sm
, pmk_r0
, pmk_r0_name
);
2227 os_memset(fils_ft
, 0, sizeof(fils_ft
));
2229 res
= wpa_derive_pmk_r1_name(pmk_r0_name
, conf
->r1_key_holder
,
2230 sm
->addr
, sm
->pmk_r1_name
,
2232 os_memset(pmk_r0
, 0, PMK_LEN_MAX
);
2235 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR1Name", sm
->pmk_r1_name
,
2237 sm
->pmk_r1_name_valid
= 1;
2239 #endif /* CONFIG_IEEE80211R_AP */
2241 res
= fils_key_auth_sk(ick
, ick_len
, snonce
, anonce
,
2242 sm
->addr
, sm
->wpa_auth
->addr
,
2243 g_sta
? wpabuf_head(g_sta
) : NULL
,
2244 g_sta
? wpabuf_len(g_sta
) : 0,
2245 g_ap
? wpabuf_head(g_ap
) : NULL
,
2246 g_ap
? wpabuf_len(g_ap
) : 0,
2247 sm
->wpa_key_mgmt
, sm
->fils_key_auth_sta
,
2248 sm
->fils_key_auth_ap
,
2249 &sm
->fils_key_auth_len
);
2250 os_memset(ick
, 0, sizeof(ick
));
2252 /* Store nonces for (Re)Association Request/Response frame processing */
2253 os_memcpy(sm
->SNonce
, snonce
, FILS_NONCE_LEN
);
2254 os_memcpy(sm
->ANonce
, anonce
, FILS_NONCE_LEN
);
2260 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
2261 u8
*buf
, size_t buf_len
, u16
*_key_data_len
)
2263 struct ieee802_1x_hdr
*hdr
;
2264 struct wpa_eapol_key
*key
;
2271 hdr
= (struct ieee802_1x_hdr
*) buf
;
2272 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2273 pos
= (u8
*) (key
+ 1);
2274 key_data_len
= WPA_GET_BE16(pos
);
2275 if (key_data_len
< AES_BLOCK_SIZE
||
2276 key_data_len
> buf_len
- sizeof(*hdr
) - sizeof(*key
) - 2) {
2277 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2278 "No room for AES-SIV data in the frame");
2281 pos
+= 2; /* Pointing at the Encrypted Key Data field */
2283 tmp
= os_malloc(key_data_len
);
2287 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2288 * to Key Data (exclusive). */
2290 aad_len
[0] = pos
- buf
;
2291 if (aes_siv_decrypt(ptk
->kek
, ptk
->kek_len
, pos
, key_data_len
,
2292 1, aad
, aad_len
, tmp
) < 0) {
2293 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2294 "Invalid AES-SIV data in the frame");
2295 bin_clear_free(tmp
, key_data_len
);
2299 /* AEAD decryption and validation completed successfully */
2300 key_data_len
-= AES_BLOCK_SIZE
;
2301 wpa_hexdump_key(MSG_DEBUG
, "WPA: Decrypted Key Data",
2304 /* Replace Key Data field with the decrypted version */
2305 os_memcpy(pos
, tmp
, key_data_len
);
2306 pos
-= 2; /* Key Data Length field */
2307 WPA_PUT_BE16(pos
, key_data_len
);
2308 bin_clear_free(tmp
, key_data_len
);
2310 *_key_data_len
= key_data_len
;
2315 const u8
* wpa_fils_validate_fils_session(struct wpa_state_machine
*sm
,
2316 const u8
*ies
, size_t ies_len
,
2317 const u8
*fils_session
)
2320 const u8
*session
= NULL
;
2322 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2323 wpa_printf(MSG_DEBUG
,
2324 "FILS: Not a FILS AKM - reject association");
2328 /* Verify Session element */
2330 end
= ((const u8
*) ie
) + ies_len
;
2331 while (ie
+ 1 < end
) {
2332 if (ie
+ 2 + ie
[1] > end
)
2334 if (ie
[0] == WLAN_EID_EXTENSION
&&
2335 ie
[1] >= 1 + FILS_SESSION_LEN
&&
2336 ie
[2] == WLAN_EID_EXT_FILS_SESSION
) {
2344 wpa_printf(MSG_DEBUG
,
2345 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2350 if (!fils_session
) {
2351 wpa_printf(MSG_DEBUG
,
2352 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2357 if (os_memcmp(fils_session
, session
+ 3, FILS_SESSION_LEN
) != 0) {
2358 wpa_printf(MSG_DEBUG
, "FILS: Session mismatch");
2359 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
2360 fils_session
, FILS_SESSION_LEN
);
2361 wpa_hexdump(MSG_DEBUG
, "FILS: Received FILS Session",
2362 session
+ 3, FILS_SESSION_LEN
);
2369 int wpa_fils_validate_key_confirm(struct wpa_state_machine
*sm
, const u8
*ies
,
2372 struct ieee802_11_elems elems
;
2374 if (ieee802_11_parse_elems(ies
, ies_len
, &elems
, 1) == ParseFailed
) {
2375 wpa_printf(MSG_DEBUG
,
2376 "FILS: Failed to parse decrypted elements");
2380 if (!elems
.fils_session
) {
2381 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
2385 if (!elems
.fils_key_confirm
) {
2386 wpa_printf(MSG_DEBUG
, "FILS: No FILS Key Confirm element");
2390 if (elems
.fils_key_confirm_len
!= sm
->fils_key_auth_len
) {
2391 wpa_printf(MSG_DEBUG
,
2392 "FILS: Unexpected Key-Auth length %d (expected %d)",
2393 elems
.fils_key_confirm_len
,
2394 (int) sm
->fils_key_auth_len
);
2398 if (os_memcmp(elems
.fils_key_confirm
, sm
->fils_key_auth_sta
,
2399 sm
->fils_key_auth_len
) != 0) {
2400 wpa_printf(MSG_DEBUG
, "FILS: Key-Auth mismatch");
2401 wpa_hexdump(MSG_DEBUG
, "FILS: Received Key-Auth",
2402 elems
.fils_key_confirm
, elems
.fils_key_confirm_len
);
2403 wpa_hexdump(MSG_DEBUG
, "FILS: Expected Key-Auth",
2404 sm
->fils_key_auth_sta
, sm
->fils_key_auth_len
);
2412 int fils_decrypt_assoc(struct wpa_state_machine
*sm
, const u8
*fils_session
,
2413 const struct ieee80211_mgmt
*mgmt
, size_t frame_len
,
2414 u8
*pos
, size_t left
)
2417 const u8
*end
, *ie_start
, *ie
, *session
, *crypt
;
2421 if (!sm
|| !sm
->PTK_valid
) {
2422 wpa_printf(MSG_DEBUG
,
2423 "FILS: No KEK to decrypt Assocication Request frame");
2427 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2428 wpa_printf(MSG_DEBUG
,
2429 "FILS: Not a FILS AKM - reject association");
2433 end
= ((const u8
*) mgmt
) + frame_len
;
2434 fc
= le_to_host16(mgmt
->frame_control
);
2435 stype
= WLAN_FC_GET_STYPE(fc
);
2436 if (stype
== WLAN_FC_STYPE_REASSOC_REQ
)
2437 ie_start
= mgmt
->u
.reassoc_req
.variable
;
2439 ie_start
= mgmt
->u
.assoc_req
.variable
;
2443 * Find FILS Session element which is the last unencrypted element in
2446 session
= wpa_fils_validate_fils_session(sm
, ie
, end
- ie
,
2449 wpa_printf(MSG_DEBUG
, "FILS: Session validation failed");
2453 crypt
= session
+ 2 + session
[1];
2455 if (end
- crypt
< AES_BLOCK_SIZE
) {
2456 wpa_printf(MSG_DEBUG
,
2457 "FILS: Too short frame to include AES-SIV data");
2461 /* AES-SIV AAD vectors */
2463 /* The STA's MAC address */
2465 aad_len
[0] = ETH_ALEN
;
2466 /* The AP's BSSID */
2468 aad_len
[1] = ETH_ALEN
;
2469 /* The STA's nonce */
2470 aad
[2] = sm
->SNonce
;
2471 aad_len
[2] = FILS_NONCE_LEN
;
2472 /* The AP's nonce */
2473 aad
[3] = sm
->ANonce
;
2474 aad_len
[3] = FILS_NONCE_LEN
;
2476 * The (Re)Association Request frame from the Capability Information
2477 * field to the FILS Session element (both inclusive).
2479 aad
[4] = (const u8
*) &mgmt
->u
.assoc_req
.capab_info
;
2480 aad_len
[4] = crypt
- aad
[4];
2482 if (aes_siv_decrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, crypt
, end
- crypt
,
2483 5, aad
, aad_len
, pos
+ (crypt
- ie_start
)) < 0) {
2484 wpa_printf(MSG_DEBUG
,
2485 "FILS: Invalid AES-SIV data in the frame");
2488 wpa_hexdump(MSG_DEBUG
, "FILS: Decrypted Association Request elements",
2489 pos
, left
- AES_BLOCK_SIZE
);
2491 if (wpa_fils_validate_key_confirm(sm
, pos
, left
- AES_BLOCK_SIZE
) < 0) {
2492 wpa_printf(MSG_DEBUG
, "FILS: Key Confirm validation failed");
2496 return left
- AES_BLOCK_SIZE
;
2500 int fils_encrypt_assoc(struct wpa_state_machine
*sm
, u8
*buf
,
2501 size_t current_len
, size_t max_len
,
2502 const struct wpabuf
*hlp
)
2504 u8
*end
= buf
+ max_len
;
2505 u8
*pos
= buf
+ current_len
;
2506 struct ieee80211_mgmt
*mgmt
;
2507 struct wpabuf
*plain
;
2511 if (!sm
|| !sm
->PTK_valid
)
2514 wpa_hexdump(MSG_DEBUG
,
2515 "FILS: Association Response frame before FILS processing",
2518 mgmt
= (struct ieee80211_mgmt
*) buf
;
2520 /* AES-SIV AAD vectors */
2522 /* The AP's BSSID */
2524 aad_len
[0] = ETH_ALEN
;
2525 /* The STA's MAC address */
2527 aad_len
[1] = ETH_ALEN
;
2528 /* The AP's nonce */
2529 aad
[2] = sm
->ANonce
;
2530 aad_len
[2] = FILS_NONCE_LEN
;
2531 /* The STA's nonce */
2532 aad
[3] = sm
->SNonce
;
2533 aad_len
[3] = FILS_NONCE_LEN
;
2535 * The (Re)Association Response frame from the Capability Information
2536 * field (the same offset in both Association and Reassociation
2537 * Response frames) to the FILS Session element (both inclusive).
2539 aad
[4] = (const u8
*) &mgmt
->u
.assoc_resp
.capab_info
;
2540 aad_len
[4] = pos
- aad
[4];
2542 /* The following elements will be encrypted with AES-SIV */
2543 plain
= fils_prepare_plainbuf(sm
, hlp
);
2545 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2549 if (pos
+ wpabuf_len(plain
) + AES_BLOCK_SIZE
> end
) {
2550 wpa_printf(MSG_DEBUG
,
2551 "FILS: Not enough room for FILS elements");
2556 wpa_hexdump_buf_key(MSG_DEBUG
, "FILS: Association Response plaintext",
2559 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
2560 wpabuf_head(plain
), wpabuf_len(plain
),
2561 5, aad
, aad_len
, pos
) < 0) {
2566 wpa_hexdump(MSG_DEBUG
,
2567 "FILS: Encrypted Association Response elements",
2568 pos
, AES_BLOCK_SIZE
+ wpabuf_len(plain
));
2569 current_len
+= wpabuf_len(plain
) + AES_BLOCK_SIZE
;
2572 sm
->fils_completed
= 1;
2578 static struct wpabuf
* fils_prepare_plainbuf(struct wpa_state_machine
*sm
,
2579 const struct wpabuf
*hlp
)
2581 struct wpabuf
*plain
;
2582 u8
*len
, *tmp
, *tmp2
;
2584 u8
*gtk
, dummy_gtk
[32];
2586 struct wpa_group
*gsm
;
2588 plain
= wpabuf_alloc(1000);
2592 /* TODO: FILS Public Key */
2594 /* FILS Key Confirmation */
2595 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2596 wpabuf_put_u8(plain
, 1 + sm
->fils_key_auth_len
); /* Length */
2597 /* Element ID Extension */
2598 wpabuf_put_u8(plain
, WLAN_EID_EXT_FILS_KEY_CONFIRM
);
2599 wpabuf_put_data(plain
, sm
->fils_key_auth_ap
, sm
->fils_key_auth_len
);
2601 /* FILS HLP Container */
2603 wpabuf_put_buf(plain
, hlp
);
2605 /* TODO: FILS IP Address Assignment */
2609 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2610 len
= wpabuf_put(plain
, 1);
2611 wpabuf_put_u8(plain
, WLAN_EID_EXT_KEY_DELIVERY
);
2612 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
,
2613 wpabuf_put(plain
, WPA_KEY_RSC_LEN
));
2615 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2616 gtk_len
= gsm
->GTK_len
;
2617 if (sm
->wpa_auth
->conf
.disable_gtk
||
2618 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
2620 * Provide unique random GTK to each STA to prevent use
2621 * of GTK in the BSS.
2623 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0) {
2629 hdr
[0] = gsm
->GN
& 0x03;
2631 tmp
= wpabuf_put(plain
, 0);
2632 tmp2
= wpa_add_kde(tmp
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
2634 wpabuf_put(plain
, tmp2
- tmp
);
2637 tmp
= wpabuf_put(plain
, 0);
2638 tmp2
= ieee80211w_kde_add(sm
, tmp
);
2639 wpabuf_put(plain
, tmp2
- tmp
);
2641 *len
= (u8
*) wpabuf_put(plain
, 0) - len
- 1;
2644 if (wpa_auth_uses_ocv(sm
)) {
2645 struct wpa_channel_info ci
;
2648 if (wpa_channel_info(sm
->wpa_auth
, &ci
) != 0) {
2649 wpa_printf(MSG_WARNING
,
2650 "FILS: Failed to get channel info for OCI element");
2655 pos
= wpabuf_put(plain
, OCV_OCI_EXTENDED_LEN
);
2656 if (ocv_insert_extended_oci(&ci
, pos
) < 0) {
2661 #endif /* CONFIG_OCV */
2667 int fils_set_tk(struct wpa_state_machine
*sm
)
2672 if (!sm
|| !sm
->PTK_valid
) {
2673 wpa_printf(MSG_DEBUG
, "FILS: No valid PTK available to set TK");
2676 if (sm
->tk_already_set
) {
2677 wpa_printf(MSG_DEBUG
, "FILS: TK already set to the driver");
2681 alg
= wpa_cipher_to_alg(sm
->pairwise
);
2682 klen
= wpa_cipher_key_len(sm
->pairwise
);
2684 wpa_printf(MSG_DEBUG
, "FILS: Configure TK to the driver");
2685 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
2686 sm
->PTK
.tk
, klen
)) {
2687 wpa_printf(MSG_DEBUG
, "FILS: Failed to set TK to the driver");
2690 sm
->tk_already_set
= TRUE
;
2696 u8
* hostapd_eid_assoc_fils_session(struct wpa_state_machine
*sm
, u8
*buf
,
2697 const u8
*fils_session
, struct wpabuf
*hlp
)
2699 struct wpabuf
*plain
;
2703 *pos
++ = WLAN_EID_EXTENSION
; /* Element ID */
2704 *pos
++ = 1 + FILS_SESSION_LEN
; /* Length */
2705 *pos
++ = WLAN_EID_EXT_FILS_SESSION
; /* Element ID Extension */
2706 os_memcpy(pos
, fils_session
, FILS_SESSION_LEN
);
2707 pos
+= FILS_SESSION_LEN
;
2709 plain
= fils_prepare_plainbuf(sm
, hlp
);
2711 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2715 os_memcpy(pos
, wpabuf_head(plain
), wpabuf_len(plain
));
2716 pos
+= wpabuf_len(plain
);
2718 wpa_printf(MSG_DEBUG
, "%s: plain buf_len: %u", __func__
,
2719 (unsigned int) wpabuf_len(plain
));
2721 sm
->fils_completed
= 1;
2725 #endif /* CONFIG_FILS */
2729 int get_sta_tx_parameters(struct wpa_state_machine
*sm
, int ap_max_chanwidth
,
2730 int ap_seg1_idx
, int *bandwidth
, int *seg1_idx
)
2732 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2734 if (!wpa_auth
->cb
->get_sta_tx_params
)
2736 return wpa_auth
->cb
->get_sta_tx_params(wpa_auth
->cb_ctx
, sm
->addr
,
2737 ap_max_chanwidth
, ap_seg1_idx
,
2738 bandwidth
, seg1_idx
);
2740 #endif /* CONFIG_OCV */
2743 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING
)
2745 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2747 int ok
= 0, psk_found
= 0;
2748 const u8
*pmk
= NULL
;
2751 const u8
*eapol_key_ie
, *key_data
, *mic
;
2752 u16 key_data_length
;
2753 size_t mic_len
, eapol_key_ie_len
;
2754 struct ieee802_1x_hdr
*hdr
;
2755 struct wpa_eapol_key
*key
;
2756 struct wpa_eapol_ie_parse kde
;
2759 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING
, wpa_ptk
);
2760 sm
->EAPOLKeyReceived
= FALSE
;
2761 sm
->update_snonce
= FALSE
;
2762 os_memset(&PTK
, 0, sizeof(PTK
));
2764 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
2766 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2767 * WPA-PSK: iterate through possible PSKs and select the one matching
2770 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
2771 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
2772 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
2773 sm
->p2p_dev_addr
, pmk
, &pmk_len
,
2778 #ifdef CONFIG_IEEE80211R_AP
2779 if (wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
2780 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
2781 sm
->xxkey_len
= pmk_len
;
2783 #endif /* CONFIG_IEEE80211R_AP */
2786 pmk_len
= sm
->pmk_len
;
2789 if (wpa_derive_ptk(sm
, sm
->SNonce
, pmk
, pmk_len
, &PTK
) < 0)
2793 wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
2794 sm
->last_rx_eapol_key
,
2795 sm
->last_rx_eapol_key_len
) == 0) {
2796 if (sm
->PMK
!= pmk
) {
2797 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
2798 sm
->pmk_len
= pmk_len
;
2806 wpa_aead_decrypt(sm
, &PTK
, sm
->last_rx_eapol_key
,
2807 sm
->last_rx_eapol_key_len
, NULL
) == 0) {
2811 #endif /* CONFIG_FILS */
2813 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
2814 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
))
2819 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2820 "invalid MIC in msg 2/4 of 4-Way Handshake");
2822 wpa_auth_psk_failure_report(sm
->wpa_auth
, sm
->addr
);
2827 * Note: last_rx_eapol_key length fields have already been validated in
2830 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
2831 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2832 mic
= (u8
*) (key
+ 1);
2833 key_data
= mic
+ mic_len
+ 2;
2834 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
2835 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
2836 sizeof(*key
) - mic_len
- 2)
2839 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
2840 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2841 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2845 eapol_key_ie
= kde
.rsn_ie
;
2846 eapol_key_ie_len
= kde
.rsn_ie_len
;
2847 } else if (kde
.osen
) {
2848 eapol_key_ie
= kde
.osen
;
2849 eapol_key_ie_len
= kde
.osen_len
;
2851 eapol_key_ie
= kde
.wpa_ie
;
2852 eapol_key_ie_len
= kde
.wpa_ie_len
;
2854 ft
= sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
);
2855 if (sm
->wpa_ie
== NULL
||
2856 wpa_compare_rsn_ie(ft
, sm
->wpa_ie
, sm
->wpa_ie_len
,
2857 eapol_key_ie
, eapol_key_ie_len
)) {
2858 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2859 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2861 wpa_hexdump(MSG_DEBUG
, "WPA IE in AssocReq",
2862 sm
->wpa_ie
, sm
->wpa_ie_len
);
2864 wpa_hexdump(MSG_DEBUG
, "WPA IE in msg 2/4",
2865 eapol_key_ie
, eapol_key_ie_len
);
2866 /* MLME-DEAUTHENTICATE.request */
2867 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
2868 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2872 if (wpa_auth_uses_ocv(sm
)) {
2873 struct wpa_channel_info ci
;
2877 if (wpa_channel_info(wpa_auth
, &ci
) != 0) {
2878 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2879 "Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
2883 if (get_sta_tx_parameters(sm
,
2884 channel_width_to_int(ci
.chanwidth
),
2885 ci
.seg1_idx
, &tx_chanwidth
,
2889 if (ocv_verify_tx_params(kde
.oci
, kde
.oci_len
, &ci
,
2890 tx_chanwidth
, tx_seg1_idx
) != 0) {
2891 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2896 #endif /* CONFIG_OCV */
2897 #ifdef CONFIG_IEEE80211R_AP
2898 if (ft
&& ft_check_msg_2_of_4(wpa_auth
, sm
, &kde
) < 0) {
2899 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
2900 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2903 #endif /* CONFIG_IEEE80211R_AP */
2905 if (kde
.ip_addr_req
&& kde
.ip_addr_req
[0] &&
2906 wpa_auth
->ip_pool
&& WPA_GET_BE32(sm
->ip_addr
) == 0) {
2908 wpa_printf(MSG_DEBUG
,
2909 "P2P: IP address requested in EAPOL-Key exchange");
2910 idx
= bitfield_get_first_zero(wpa_auth
->ip_pool
);
2912 u32 start
= WPA_GET_BE32(wpa_auth
->conf
.ip_addr_start
);
2913 bitfield_set(wpa_auth
->ip_pool
, idx
);
2914 WPA_PUT_BE32(sm
->ip_addr
, start
+ idx
);
2915 wpa_printf(MSG_DEBUG
,
2916 "P2P: Assigned IP address %u.%u.%u.%u to "
2917 MACSTR
, sm
->ip_addr
[0], sm
->ip_addr
[1],
2918 sm
->ip_addr
[2], sm
->ip_addr
[3],
2922 #endif /* CONFIG_P2P */
2924 #ifdef CONFIG_IEEE80211R_AP
2925 if (sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2927 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2928 * with the value we derived.
2930 if (os_memcmp_const(sm
->sup_pmk_r1_name
, sm
->pmk_r1_name
,
2931 WPA_PMK_NAME_LEN
) != 0) {
2932 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2933 "PMKR1Name mismatch in FT 4-way "
2935 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from "
2937 sm
->sup_pmk_r1_name
, WPA_PMK_NAME_LEN
);
2938 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
2939 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
2943 #endif /* CONFIG_IEEE80211R_AP */
2945 if (vlan_id
&& wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
2946 wpa_auth_update_vlan(wpa_auth
, sm
->addr
, vlan_id
) < 0) {
2947 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
2948 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2952 sm
->pending_1_of_4_timeout
= 0;
2953 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
2955 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
2956 /* PSK may have changed from the previous choice, so update
2957 * state machine data based on whatever PSK was selected here.
2959 os_memcpy(sm
->PMK
, pmk
, PMK_LEN
);
2960 sm
->pmk_len
= PMK_LEN
;
2963 sm
->MICVerified
= TRUE
;
2965 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
2966 sm
->PTK_valid
= TRUE
;
2970 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING2
)
2972 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING2
, wpa_ptk
);
2977 #ifdef CONFIG_IEEE80211W
2979 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
2981 if (sm
->mgmt_frame_prot
) {
2983 len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
2984 return 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
+ len
;
2991 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
2993 struct wpa_igtk_kde igtk
;
2994 struct wpa_group
*gsm
= sm
->group
;
2995 u8 rsc
[WPA_KEY_RSC_LEN
];
2996 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
2998 if (!sm
->mgmt_frame_prot
)
3001 igtk
.keyid
[0] = gsm
->GN_igtk
;
3003 if (gsm
->wpa_group_state
!= WPA_GROUP_SETKEYSDONE
||
3004 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, rsc
) < 0)
3005 os_memset(igtk
.pn
, 0, sizeof(igtk
.pn
));
3007 os_memcpy(igtk
.pn
, rsc
, sizeof(igtk
.pn
));
3008 os_memcpy(igtk
.igtk
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3009 if (sm
->wpa_auth
->conf
.disable_gtk
||
3010 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3012 * Provide unique random IGTK to each STA to prevent use of
3015 if (random_get_bytes(igtk
.igtk
, len
) < 0)
3018 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_IGTK
,
3019 (const u8
*) &igtk
, WPA_IGTK_KDE_PREFIX_LEN
+ len
,
3025 #else /* CONFIG_IEEE80211W */
3027 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
3033 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
3038 #endif /* CONFIG_IEEE80211W */
3041 static int ocv_oci_len(struct wpa_state_machine
*sm
)
3044 if (wpa_auth_uses_ocv(sm
))
3045 return OCV_OCI_KDE_LEN
;
3046 #endif /* CONFIG_OCV */
3050 static int ocv_oci_add(struct wpa_state_machine
*sm
, u8
**argpos
)
3053 struct wpa_channel_info ci
;
3055 if (!wpa_auth_uses_ocv(sm
))
3058 if (wpa_channel_info(sm
->wpa_auth
, &ci
) != 0) {
3059 wpa_printf(MSG_WARNING
,
3060 "Failed to get channel info for OCI element");
3064 return ocv_insert_oci_kde(&ci
, argpos
);
3065 #else /* CONFIG_OCV */
3067 #endif /* CONFIG_OCV */
3071 SM_STATE(WPA_PTK
, PTKINITNEGOTIATING
)
3073 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
, dummy_gtk
[32];
3074 size_t gtk_len
, kde_len
;
3075 struct wpa_group
*gsm
= sm
->group
;
3077 int wpa_ie_len
, secure
, keyidx
, encr
= 0;
3079 SM_ENTRY_MA(WPA_PTK
, PTKINITNEGOTIATING
, wpa_ptk
);
3080 sm
->TimeoutEvt
= FALSE
;
3083 if (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3084 sm
->TimeoutCtr
> 1) {
3085 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
3088 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
3089 /* No point in sending the EAPOL-Key - we will disconnect
3090 * immediately following this. */
3094 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
3095 GTK[GN], IGTK, [FTIE], [TIE * 2])
3097 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3098 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3099 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
3100 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
3101 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
3102 if (sm
->wpa
== WPA_VERSION_WPA
&&
3103 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
3104 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
3105 /* WPA-only STA, remove RSN IE and possible MDIE */
3106 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
3107 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
3108 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
3109 wpa_ie_len
= wpa_ie
[1] + 2;
3111 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3112 "sending 3/4 msg of 4-Way Handshake");
3113 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3114 /* WPA2 send GTK in the 4-way handshake */
3116 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3117 gtk_len
= gsm
->GTK_len
;
3118 if (sm
->wpa_auth
->conf
.disable_gtk
||
3119 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3121 * Provide unique random GTK to each STA to prevent use
3122 * of GTK in the BSS.
3124 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0)
3132 /* WPA does not include GTK in msg 3/4 */
3138 if (sm
->rx_eapol_key_secure
) {
3140 * It looks like Windows 7 supplicant tries to use
3141 * Secure bit in msg 2/4 after having reported Michael
3142 * MIC failure and it then rejects the 4-way handshake
3143 * if msg 3/4 does not set Secure bit. Work around this
3144 * by setting the Secure bit here even in the case of
3145 * WPA if the supplicant used it first.
3147 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3148 "STA used Secure bit in WPA msg 2/4 - "
3149 "set Secure for 3/4 as workaround");
3154 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
3156 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
3157 #ifdef CONFIG_IEEE80211R_AP
3158 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3159 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
3160 kde_len
+= 300; /* FTIE + 2 * TIE */
3162 #endif /* CONFIG_IEEE80211R_AP */
3164 if (WPA_GET_BE32(sm
->ip_addr
) > 0)
3165 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 3 * 4;
3166 #endif /* CONFIG_P2P */
3167 kde
= os_malloc(kde_len
);
3172 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
3174 #ifdef CONFIG_IEEE80211R_AP
3175 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3180 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
3182 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
3183 "PMKR1Name into RSN IE in EAPOL-Key data");
3190 #endif /* CONFIG_IEEE80211R_AP */
3193 hdr
[0] = keyidx
& 0x03;
3195 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3198 pos
= ieee80211w_kde_add(sm
, pos
);
3199 if (ocv_oci_add(sm
, &pos
) < 0) {
3204 #ifdef CONFIG_IEEE80211R_AP
3205 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3207 struct wpa_auth_config
*conf
;
3209 conf
= &sm
->wpa_auth
->conf
;
3210 if (sm
->assoc_resp_ftie
&&
3211 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
3212 os_memcpy(pos
, sm
->assoc_resp_ftie
,
3213 2 + sm
->assoc_resp_ftie
[1]);
3214 res
= 2 + sm
->assoc_resp_ftie
[1];
3216 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
3218 res
= wpa_write_ftie(conf
, use_sha384
,
3219 conf
->r0_key_holder
,
3220 conf
->r0_key_holder_len
,
3222 kde
+ kde_len
- pos
,
3226 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
3227 "into EAPOL-Key Key Data");
3233 /* TIE[ReassociationDeadline] (TU) */
3234 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3236 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
3237 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
3240 /* TIE[KeyLifetime] (seconds) */
3241 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3243 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
3244 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
);
3247 #endif /* CONFIG_IEEE80211R_AP */
3249 if (WPA_GET_BE32(sm
->ip_addr
) > 0) {
3251 os_memcpy(addr
, sm
->ip_addr
, 4);
3252 os_memcpy(addr
+ 4, sm
->wpa_auth
->conf
.ip_addr_mask
, 4);
3253 os_memcpy(addr
+ 8, sm
->wpa_auth
->conf
.ip_addr_go
, 4);
3254 pos
= wpa_add_kde(pos
, WFA_KEY_DATA_IP_ADDR_ALLOC
,
3255 addr
, sizeof(addr
), NULL
, 0);
3257 #endif /* CONFIG_P2P */
3259 wpa_send_eapol(sm
->wpa_auth
, sm
,
3260 (secure
? WPA_KEY_INFO_SECURE
: 0) |
3261 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3262 WPA_KEY_INFO_MIC
: 0) |
3263 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
3264 WPA_KEY_INFO_KEY_TYPE
,
3265 _rsc
, sm
->ANonce
, kde
, pos
- kde
, keyidx
, encr
);
3270 SM_STATE(WPA_PTK
, PTKINITDONE
)
3272 SM_ENTRY_MA(WPA_PTK
, PTKINITDONE
, wpa_ptk
);
3273 sm
->EAPOLKeyReceived
= FALSE
;
3275 enum wpa_alg alg
= wpa_cipher_to_alg(sm
->pairwise
);
3276 int klen
= wpa_cipher_key_len(sm
->pairwise
);
3277 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
3278 sm
->PTK
.tk
, klen
)) {
3279 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
,
3280 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3283 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3284 sm
->pairwise_set
= TRUE
;
3286 if (sm
->wpa_auth
->conf
.wpa_ptk_rekey
) {
3287 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
3288 eloop_register_timeout(sm
->wpa_auth
->conf
.
3289 wpa_ptk_rekey
, 0, wpa_rekey_ptk
,
3293 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3294 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
3295 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
3296 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3297 WPA_EAPOL_authorized
, 1);
3301 if (0 /* IBSS == TRUE */) {
3303 if (sm
->keycount
== 2) {
3304 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3305 WPA_EAPOL_portValid
, 1);
3308 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
,
3311 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyAvailable
, 0);
3312 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyDone
, 1);
3313 if (sm
->wpa
== WPA_VERSION_WPA
)
3314 sm
->PInitAKeys
= TRUE
;
3317 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3318 "pairwise key handshake completed (%s)",
3319 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3321 #ifdef CONFIG_IEEE80211R_AP
3322 wpa_ft_push_pmk_r1(sm
->wpa_auth
, sm
->addr
);
3323 #endif /* CONFIG_IEEE80211R_AP */
3329 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3332 SM_ENTER(WPA_PTK
, INITIALIZE
);
3333 else if (sm
->Disconnect
3334 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3335 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3336 "WPA_PTK: sm->Disconnect");
3337 SM_ENTER(WPA_PTK
, DISCONNECT
);
3339 else if (sm
->DeauthenticationRequest
)
3340 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3341 else if (sm
->AuthenticationRequest
)
3342 SM_ENTER(WPA_PTK
, AUTHENTICATION
);
3343 else if (sm
->ReAuthenticationRequest
)
3344 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3345 else if (sm
->PTKRequest
) {
3346 if (wpa_auth_sm_ptk_update(sm
) < 0)
3347 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3349 SM_ENTER(WPA_PTK
, PTKSTART
);
3350 } else switch (sm
->wpa_ptk_state
) {
3351 case WPA_PTK_INITIALIZE
:
3353 case WPA_PTK_DISCONNECT
:
3354 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3356 case WPA_PTK_DISCONNECTED
:
3357 SM_ENTER(WPA_PTK
, INITIALIZE
);
3359 case WPA_PTK_AUTHENTICATION
:
3360 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3362 case WPA_PTK_AUTHENTICATION2
:
3363 if (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
3364 wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
3365 WPA_EAPOL_keyRun
) > 0)
3366 SM_ENTER(WPA_PTK
, INITPMK
);
3367 else if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3368 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
3369 /* FIX: && 802.1X::keyRun */)
3370 SM_ENTER(WPA_PTK
, INITPSK
);
3371 else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
)
3372 SM_ENTER(WPA_PTK
, INITPMK
);
3374 case WPA_PTK_INITPMK
:
3375 if (wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
3376 WPA_EAPOL_keyAvailable
) > 0) {
3377 SM_ENTER(WPA_PTK
, PTKSTART
);
3379 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->pmksa
) {
3380 SM_ENTER(WPA_PTK
, PTKSTART
);
3381 #endif /* CONFIG_DPP */
3383 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3384 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3385 "INITPMK - keyAvailable = false");
3386 SM_ENTER(WPA_PTK
, DISCONNECT
);
3389 case WPA_PTK_INITPSK
:
3390 if (wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
,
3391 NULL
, NULL
, NULL
)) {
3392 SM_ENTER(WPA_PTK
, PTKSTART
);
3394 } else if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
3395 SM_ENTER(WPA_PTK
, PTKSTART
);
3396 #endif /* CONFIG_SAE */
3398 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3399 "no PSK configured for the STA");
3400 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3401 SM_ENTER(WPA_PTK
, DISCONNECT
);
3404 case WPA_PTK_PTKSTART
:
3405 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3406 sm
->EAPOLKeyPairwise
)
3407 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3408 else if (sm
->TimeoutCtr
>
3409 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
3410 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3412 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3413 "PTKSTART: Retry limit %u reached",
3414 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3415 SM_ENTER(WPA_PTK
, DISCONNECT
);
3416 } else if (sm
->TimeoutEvt
)
3417 SM_ENTER(WPA_PTK
, PTKSTART
);
3419 case WPA_PTK_PTKCALCNEGOTIATING
:
3420 if (sm
->MICVerified
)
3421 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING2
);
3422 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3423 sm
->EAPOLKeyPairwise
)
3424 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3425 else if (sm
->TimeoutEvt
)
3426 SM_ENTER(WPA_PTK
, PTKSTART
);
3428 case WPA_PTK_PTKCALCNEGOTIATING2
:
3429 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3431 case WPA_PTK_PTKINITNEGOTIATING
:
3432 if (sm
->update_snonce
)
3433 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3434 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3435 sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3436 SM_ENTER(WPA_PTK
, PTKINITDONE
);
3437 else if (sm
->TimeoutCtr
>
3438 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
||
3439 (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3440 sm
->TimeoutCtr
> 1)) {
3441 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3443 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3444 "PTKINITNEGOTIATING: Retry limit %u reached",
3445 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3446 SM_ENTER(WPA_PTK
, DISCONNECT
);
3447 } else if (sm
->TimeoutEvt
)
3448 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3450 case WPA_PTK_PTKINITDONE
:
3456 SM_STATE(WPA_PTK_GROUP
, IDLE
)
3458 SM_ENTRY_MA(WPA_PTK_GROUP
, IDLE
, wpa_ptk_group
);
3460 /* Init flag is not cleared here, so avoid busy
3461 * loop by claiming nothing changed. */
3462 sm
->changed
= FALSE
;
3464 sm
->GTimeoutCtr
= 0;
3468 SM_STATE(WPA_PTK_GROUP
, REKEYNEGOTIATING
)
3470 u8 rsc
[WPA_KEY_RSC_LEN
];
3471 struct wpa_group
*gsm
= sm
->group
;
3473 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
3475 u8
*gtk
, dummy_gtk
[32];
3477 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYNEGOTIATING
, wpa_ptk_group
);
3480 if (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3481 sm
->GTimeoutCtr
> 1) {
3482 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3485 if (sm
->GTimeoutCtr
> sm
->wpa_auth
->conf
.wpa_group_update_count
) {
3486 /* No point in sending the EAPOL-Key - we will disconnect
3487 * immediately following this. */
3491 if (sm
->wpa
== WPA_VERSION_WPA
)
3492 sm
->PInitAKeys
= FALSE
;
3493 sm
->TimeoutEvt
= FALSE
;
3494 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3495 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3496 if (gsm
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
)
3497 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3498 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3499 "sending 1/2 msg of Group Key Handshake");
3501 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3502 if (sm
->wpa_auth
->conf
.disable_gtk
||
3503 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3505 * Provide unique random GTK to each STA to prevent use
3506 * of GTK in the BSS.
3508 if (random_get_bytes(dummy_gtk
, gsm
->GTK_len
) < 0)
3512 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3513 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
3514 ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
3515 kde_buf
= os_malloc(kde_len
);
3516 if (kde_buf
== NULL
)
3519 kde
= pos
= kde_buf
;
3520 hdr
[0] = gsm
->GN
& 0x03;
3522 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3524 pos
= ieee80211w_kde_add(sm
, pos
);
3525 if (ocv_oci_add(sm
, &pos
) < 0) {
3529 kde_len
= pos
- kde
;
3532 kde_len
= gsm
->GTK_len
;
3535 wpa_send_eapol(sm
->wpa_auth
, sm
,
3536 WPA_KEY_INFO_SECURE
|
3537 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3538 WPA_KEY_INFO_MIC
: 0) |
3540 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
3541 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
3547 SM_STATE(WPA_PTK_GROUP
, REKEYESTABLISHED
)
3550 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3551 const u8
*key_data
, *mic
;
3552 struct ieee802_1x_hdr
*hdr
;
3553 struct wpa_eapol_key
*key
;
3554 struct wpa_eapol_ie_parse kde
;
3556 u16 key_data_length
;
3557 #endif /* CONFIG_OCV */
3559 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYESTABLISHED
, wpa_ptk_group
);
3560 sm
->EAPOLKeyReceived
= FALSE
;
3563 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
3566 * Note: last_rx_eapol_key length fields have already been validated in
3569 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
3570 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
3571 mic
= (u8
*) (key
+ 1);
3572 key_data
= mic
+ mic_len
+ 2;
3573 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
3574 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
3575 sizeof(*key
) - mic_len
- 2)
3578 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
3579 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3580 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3584 if (wpa_auth_uses_ocv(sm
)) {
3585 struct wpa_channel_info ci
;
3589 if (wpa_channel_info(wpa_auth
, &ci
) != 0) {
3590 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3591 "Failed to get channel info to validate received OCI in EAPOL-Key group 1/2");
3595 if (get_sta_tx_parameters(sm
,
3596 channel_width_to_int(ci
.chanwidth
),
3597 ci
.seg1_idx
, &tx_chanwidth
,
3601 if (ocv_verify_tx_params(kde
.oci
, kde
.oci_len
, &ci
,
3602 tx_chanwidth
, tx_seg1_idx
) != 0) {
3603 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3608 #endif /* CONFIG_OCV */
3610 if (sm
->GUpdateStationKeys
)
3611 sm
->group
->GKeyDoneStations
--;
3612 sm
->GUpdateStationKeys
= FALSE
;
3613 sm
->GTimeoutCtr
= 0;
3614 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3615 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3616 "group key handshake completed (%s)",
3617 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3622 SM_STATE(WPA_PTK_GROUP
, KEYERROR
)
3624 SM_ENTRY_MA(WPA_PTK_GROUP
, KEYERROR
, wpa_ptk_group
);
3625 if (sm
->GUpdateStationKeys
)
3626 sm
->group
->GKeyDoneStations
--;
3627 sm
->GUpdateStationKeys
= FALSE
;
3628 sm
->Disconnect
= TRUE
;
3629 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3630 "group key handshake failed (%s) after %u tries",
3631 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN",
3632 sm
->wpa_auth
->conf
.wpa_group_update_count
);
3636 SM_STEP(WPA_PTK_GROUP
)
3638 if (sm
->Init
|| sm
->PtkGroupInit
) {
3639 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3640 sm
->PtkGroupInit
= FALSE
;
3641 } else switch (sm
->wpa_ptk_group_state
) {
3642 case WPA_PTK_GROUP_IDLE
:
3643 if (sm
->GUpdateStationKeys
||
3644 (sm
->wpa
== WPA_VERSION_WPA
&& sm
->PInitAKeys
))
3645 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3647 case WPA_PTK_GROUP_REKEYNEGOTIATING
:
3648 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3649 !sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3650 SM_ENTER(WPA_PTK_GROUP
, REKEYESTABLISHED
);
3651 else if (sm
->GTimeoutCtr
>
3652 sm
->wpa_auth
->conf
.wpa_group_update_count
||
3653 (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3654 sm
->GTimeoutCtr
> 1))
3655 SM_ENTER(WPA_PTK_GROUP
, KEYERROR
);
3656 else if (sm
->TimeoutEvt
)
3657 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3659 case WPA_PTK_GROUP_KEYERROR
:
3660 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3662 case WPA_PTK_GROUP_REKEYESTABLISHED
:
3663 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3669 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
3670 struct wpa_group
*group
)
3674 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3675 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3676 if (wpa_gmk_to_gtk(group
->GMK
, "Group key expansion",
3677 wpa_auth
->addr
, group
->GNonce
,
3678 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3680 wpa_hexdump_key(MSG_DEBUG
, "GTK",
3681 group
->GTK
[group
->GN
- 1], group
->GTK_len
);
3683 #ifdef CONFIG_IEEE80211W
3684 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3686 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3687 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3688 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3689 if (wpa_gmk_to_gtk(group
->GMK
, "IGTK key expansion",
3690 wpa_auth
->addr
, group
->GNonce
,
3691 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3693 wpa_hexdump_key(MSG_DEBUG
, "IGTK",
3694 group
->IGTK
[group
->GN_igtk
- 4], len
);
3696 #endif /* CONFIG_IEEE80211W */
3702 static void wpa_group_gtk_init(struct wpa_authenticator
*wpa_auth
,
3703 struct wpa_group
*group
)
3705 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3706 "GTK_INIT (VLAN-ID %d)", group
->vlan_id
);
3707 group
->changed
= FALSE
; /* GInit is not cleared here; avoid loop */
3708 group
->wpa_group_state
= WPA_GROUP_GTK_INIT
;
3711 os_memset(group
->GTK
, 0, sizeof(group
->GTK
));
3714 #ifdef CONFIG_IEEE80211W
3717 #endif /* CONFIG_IEEE80211W */
3718 /* GTK[GN] = CalcGTK() */
3719 wpa_gtk_update(wpa_auth
, group
);
3723 static int wpa_group_update_sta(struct wpa_state_machine
*sm
, void *ctx
)
3725 if (ctx
!= NULL
&& ctx
!= sm
->group
)
3728 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITDONE
) {
3729 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3730 "Not in PTKINITDONE; skip Group Key update");
3731 sm
->GUpdateStationKeys
= FALSE
;
3734 if (sm
->GUpdateStationKeys
) {
3736 * This should not really happen, so add a debug log entry.
3737 * Since we clear the GKeyDoneStations before the loop, the
3738 * station needs to be counted here anyway.
3740 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3741 "GUpdateStationKeys was already set when "
3742 "marking station for GTK rekeying");
3745 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
3746 if (sm
->is_wnmsleep
)
3749 sm
->group
->GKeyDoneStations
++;
3750 sm
->GUpdateStationKeys
= TRUE
;
3757 #ifdef CONFIG_WNM_AP
3758 /* update GTK when exiting WNM-Sleep Mode */
3759 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine
*sm
)
3761 if (sm
== NULL
|| sm
->is_wnmsleep
)
3764 wpa_group_update_sta(sm
, NULL
);
3768 void wpa_set_wnmsleep(struct wpa_state_machine
*sm
, int flag
)
3771 sm
->is_wnmsleep
= !!flag
;
3775 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3777 struct wpa_group
*gsm
= sm
->group
;
3782 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
3785 *pos
++ = WNM_SLEEP_SUBELEM_GTK
;
3786 *pos
++ = 11 + gsm
->GTK_len
;
3787 /* Key ID in B0-B1 of Key Info */
3788 WPA_PUT_LE16(pos
, gsm
->GN
& 0x03);
3790 *pos
++ = gsm
->GTK_len
;
3791 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, pos
) != 0)
3794 os_memcpy(pos
, gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
3795 pos
+= gsm
->GTK_len
;
3797 wpa_printf(MSG_DEBUG
, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3799 wpa_hexdump_key(MSG_DEBUG
, "WNM: GTK in WNM-Sleep Mode exit",
3800 gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
3806 #ifdef CONFIG_IEEE80211W
3807 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3809 struct wpa_group
*gsm
= sm
->group
;
3811 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3815 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3817 *pos
++ = WNM_SLEEP_SUBELEM_IGTK
;
3818 *pos
++ = 2 + 6 + len
;
3819 WPA_PUT_LE16(pos
, gsm
->GN_igtk
);
3821 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, pos
) != 0)
3825 os_memcpy(pos
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3828 wpa_printf(MSG_DEBUG
, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3830 wpa_hexdump_key(MSG_DEBUG
, "WNM: IGTK in WNM-Sleep Mode exit",
3831 gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3835 #endif /* CONFIG_IEEE80211W */
3836 #endif /* CONFIG_WNM_AP */
3839 static void wpa_group_setkeys(struct wpa_authenticator
*wpa_auth
,
3840 struct wpa_group
*group
)
3844 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3845 "SETKEYS (VLAN-ID %d)", group
->vlan_id
);
3846 group
->changed
= TRUE
;
3847 group
->wpa_group_state
= WPA_GROUP_SETKEYS
;
3848 group
->GTKReKey
= FALSE
;
3850 group
->GM
= group
->GN
;
3852 #ifdef CONFIG_IEEE80211W
3853 tmp
= group
->GM_igtk
;
3854 group
->GM_igtk
= group
->GN_igtk
;
3855 group
->GN_igtk
= tmp
;
3856 #endif /* CONFIG_IEEE80211W */
3857 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3858 * counting the STAs that are marked with GUpdateStationKeys instead of
3859 * including all STAs that could be in not-yet-completed state. */
3860 wpa_gtk_update(wpa_auth
, group
);
3862 if (group
->GKeyDoneStations
) {
3863 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: Unexpected "
3864 "GKeyDoneStations=%d when starting new GTK rekey",
3865 group
->GKeyDoneStations
);
3866 group
->GKeyDoneStations
= 0;
3868 wpa_auth_for_each_sta(wpa_auth
, wpa_group_update_sta
, group
);
3869 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: GKeyDoneStations=%d",
3870 group
->GKeyDoneStations
);
3874 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
3875 struct wpa_group
*group
)
3879 if (wpa_auth_set_key(wpa_auth
, group
->vlan_id
,
3880 wpa_cipher_to_alg(wpa_auth
->conf
.wpa_group
),
3881 broadcast_ether_addr
, group
->GN
,
3882 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3885 #ifdef CONFIG_IEEE80211W
3886 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3890 alg
= wpa_cipher_to_alg(wpa_auth
->conf
.group_mgmt_cipher
);
3891 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3894 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, alg
,
3895 broadcast_ether_addr
, group
->GN_igtk
,
3896 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3899 #endif /* CONFIG_IEEE80211W */
3905 static int wpa_group_disconnect_cb(struct wpa_state_machine
*sm
, void *ctx
)
3907 if (sm
->group
== ctx
) {
3908 wpa_printf(MSG_DEBUG
, "WPA: Mark STA " MACSTR
3909 " for discconnection due to fatal failure",
3911 sm
->Disconnect
= TRUE
;
3918 static void wpa_group_fatal_failure(struct wpa_authenticator
*wpa_auth
,
3919 struct wpa_group
*group
)
3921 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state FATAL_FAILURE");
3922 group
->changed
= TRUE
;
3923 group
->wpa_group_state
= WPA_GROUP_FATAL_FAILURE
;
3924 wpa_auth_for_each_sta(wpa_auth
, wpa_group_disconnect_cb
, group
);
3928 static int wpa_group_setkeysdone(struct wpa_authenticator
*wpa_auth
,
3929 struct wpa_group
*group
)
3931 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3932 "SETKEYSDONE (VLAN-ID %d)", group
->vlan_id
);
3933 group
->changed
= TRUE
;
3934 group
->wpa_group_state
= WPA_GROUP_SETKEYSDONE
;
3936 if (wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
3937 wpa_group_fatal_failure(wpa_auth
, group
);
3945 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
3946 struct wpa_group
*group
)
3949 wpa_group_gtk_init(wpa_auth
, group
);
3950 } else if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
) {
3951 /* Do not allow group operations */
3952 } else if (group
->wpa_group_state
== WPA_GROUP_GTK_INIT
&&
3953 group
->GTKAuthenticator
) {
3954 wpa_group_setkeysdone(wpa_auth
, group
);
3955 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
&&
3957 wpa_group_setkeys(wpa_auth
, group
);
3958 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYS
) {
3959 if (group
->GKeyDoneStations
== 0)
3960 wpa_group_setkeysdone(wpa_auth
, group
);
3961 else if (group
->GTKReKey
)
3962 wpa_group_setkeys(wpa_auth
, group
);
3967 static int wpa_sm_step(struct wpa_state_machine
*sm
)
3972 if (sm
->in_step_loop
) {
3973 /* This should not happen, but if it does, make sure we do not
3974 * end up freeing the state machine too early by exiting the
3975 * recursive call. */
3976 wpa_printf(MSG_ERROR
, "WPA: wpa_sm_step() called recursively");
3980 sm
->in_step_loop
= 1;
3982 if (sm
->pending_deinit
)
3985 sm
->changed
= FALSE
;
3986 sm
->wpa_auth
->group
->changed
= FALSE
;
3988 SM_STEP_RUN(WPA_PTK
);
3989 if (sm
->pending_deinit
)
3991 SM_STEP_RUN(WPA_PTK_GROUP
);
3992 if (sm
->pending_deinit
)
3994 wpa_group_sm_step(sm
->wpa_auth
, sm
->group
);
3995 } while (sm
->changed
|| sm
->wpa_auth
->group
->changed
);
3996 sm
->in_step_loop
= 0;
3998 if (sm
->pending_deinit
) {
3999 wpa_printf(MSG_DEBUG
, "WPA: Completing pending STA state "
4000 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
4001 wpa_free_sta_sm(sm
);
4008 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
)
4010 struct wpa_state_machine
*sm
= eloop_ctx
;
4015 void wpa_auth_sm_notify(struct wpa_state_machine
*sm
)
4019 eloop_register_timeout(0, 0, wpa_sm_call_step
, sm
, NULL
);
4023 void wpa_gtk_rekey(struct wpa_authenticator
*wpa_auth
)
4026 struct wpa_group
*group
;
4028 if (wpa_auth
== NULL
)
4031 group
= wpa_auth
->group
;
4033 for (i
= 0; i
< 2; i
++) {
4035 group
->GM
= group
->GN
;
4037 #ifdef CONFIG_IEEE80211W
4038 tmp
= group
->GM_igtk
;
4039 group
->GM_igtk
= group
->GN_igtk
;
4040 group
->GN_igtk
= tmp
;
4041 #endif /* CONFIG_IEEE80211W */
4042 wpa_gtk_update(wpa_auth
, group
);
4043 wpa_group_config_group_keys(wpa_auth
, group
);
4048 static const char * wpa_bool_txt(int val
)
4050 return val
? "TRUE" : "FALSE";
4054 #define RSN_SUITE "%02x-%02x-%02x-%d"
4055 #define RSN_SUITE_ARG(s) \
4056 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
4058 int wpa_get_mib(struct wpa_authenticator
*wpa_auth
, char *buf
, size_t buflen
)
4061 char pmkid_txt
[PMKID_LEN
* 2 + 1];
4062 #ifdef CONFIG_RSN_PREAUTH
4063 const int preauth
= 1;
4064 #else /* CONFIG_RSN_PREAUTH */
4065 const int preauth
= 0;
4066 #endif /* CONFIG_RSN_PREAUTH */
4068 if (wpa_auth
== NULL
)
4071 ret
= os_snprintf(buf
+ len
, buflen
- len
,
4072 "dot11RSNAOptionImplemented=TRUE\n"
4073 "dot11RSNAPreauthenticationImplemented=%s\n"
4074 "dot11RSNAEnabled=%s\n"
4075 "dot11RSNAPreauthenticationEnabled=%s\n",
4076 wpa_bool_txt(preauth
),
4077 wpa_bool_txt(wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
),
4078 wpa_bool_txt(wpa_auth
->conf
.rsn_preauth
));
4079 if (os_snprintf_error(buflen
- len
, ret
))
4083 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
4084 wpa_auth
->dot11RSNAPMKIDUsed
, PMKID_LEN
);
4087 buf
+ len
, buflen
- len
,
4088 "dot11RSNAConfigVersion=%u\n"
4089 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
4090 /* FIX: dot11RSNAConfigGroupCipher */
4091 /* FIX: dot11RSNAConfigGroupRekeyMethod */
4092 /* FIX: dot11RSNAConfigGroupRekeyTime */
4093 /* FIX: dot11RSNAConfigGroupRekeyPackets */
4094 "dot11RSNAConfigGroupRekeyStrict=%u\n"
4095 "dot11RSNAConfigGroupUpdateCount=%u\n"
4096 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
4097 "dot11RSNAConfigGroupCipherSize=%u\n"
4098 "dot11RSNAConfigPMKLifetime=%u\n"
4099 "dot11RSNAConfigPMKReauthThreshold=%u\n"
4100 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4101 "dot11RSNAConfigSATimeout=%u\n"
4102 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
4103 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
4104 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
4105 "dot11RSNAPMKIDUsed=%s\n"
4106 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
4107 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
4108 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
4109 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4110 "dot11RSNA4WayHandshakeFailures=%u\n"
4111 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4113 !!wpa_auth
->conf
.wpa_strict_rekey
,
4114 wpa_auth
->conf
.wpa_group_update_count
,
4115 wpa_auth
->conf
.wpa_pairwise_update_count
,
4116 wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
) * 8,
4117 dot11RSNAConfigPMKLifetime
,
4118 dot11RSNAConfigPMKReauthThreshold
,
4119 dot11RSNAConfigSATimeout
,
4120 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteSelected
),
4121 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherSelected
),
4122 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherSelected
),
4124 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteRequested
),
4125 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherRequested
),
4126 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherRequested
),
4127 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
,
4128 wpa_auth
->dot11RSNA4WayHandshakeFailures
);
4129 if (os_snprintf_error(buflen
- len
, ret
))
4133 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
4134 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4137 ret
= os_snprintf(buf
+ len
, buflen
- len
, "hostapdWPAGroupState=%d\n",
4138 wpa_auth
->group
->wpa_group_state
);
4139 if (os_snprintf_error(buflen
- len
, ret
))
4147 int wpa_get_mib_sta(struct wpa_state_machine
*sm
, char *buf
, size_t buflen
)
4155 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4157 /* dot11RSNAStatsEntry */
4159 pairwise
= wpa_cipher_to_suite(sm
->wpa
== WPA_VERSION_WPA2
?
4160 WPA_PROTO_RSN
: WPA_PROTO_WPA
,
4166 buf
+ len
, buflen
- len
,
4167 /* TODO: dot11RSNAStatsIndex */
4168 "dot11RSNAStatsSTAAddress=" MACSTR
"\n"
4169 "dot11RSNAStatsVersion=1\n"
4170 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE
"\n"
4171 /* TODO: dot11RSNAStatsTKIPICVErrors */
4172 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
4173 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
4174 /* TODO: dot11RSNAStatsCCMPReplays */
4175 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
4176 /* TODO: dot11RSNAStatsTKIPReplays */,
4178 RSN_SUITE_ARG(pairwise
),
4179 sm
->dot11RSNAStatsTKIPLocalMICFailures
,
4180 sm
->dot11RSNAStatsTKIPRemoteMICFailures
);
4181 if (os_snprintf_error(buflen
- len
, ret
))
4186 ret
= os_snprintf(buf
+ len
, buflen
- len
,
4187 "hostapdWPAPTKState=%d\n"
4188 "hostapdWPAPTKGroupState=%d\n",
4190 sm
->wpa_ptk_group_state
);
4191 if (os_snprintf_error(buflen
- len
, ret
))
4199 void wpa_auth_countermeasures_start(struct wpa_authenticator
*wpa_auth
)
4202 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
++;
4206 int wpa_auth_pairwise_set(struct wpa_state_machine
*sm
)
4208 return sm
&& sm
->pairwise_set
;
4212 int wpa_auth_get_pairwise(struct wpa_state_machine
*sm
)
4214 return sm
->pairwise
;
4218 const u8
* wpa_auth_get_pmk(struct wpa_state_machine
*sm
, int *len
)
4227 int wpa_auth_sta_key_mgmt(struct wpa_state_machine
*sm
)
4231 return sm
->wpa_key_mgmt
;
4235 int wpa_auth_sta_wpa_version(struct wpa_state_machine
*sm
)
4243 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine
*sm
)
4245 if (!sm
|| !wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
4247 return sm
->tk_already_set
;
4251 int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine
*sm
)
4253 if (!sm
|| !wpa_key_mgmt_fils(sm
->wpa_key_mgmt
))
4255 return sm
->tk_already_set
;
4259 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine
*sm
,
4260 struct rsn_pmksa_cache_entry
*entry
)
4262 if (sm
== NULL
|| sm
->pmksa
!= entry
)
4269 struct rsn_pmksa_cache_entry
*
4270 wpa_auth_sta_get_pmksa(struct wpa_state_machine
*sm
)
4272 return sm
? sm
->pmksa
: NULL
;
4276 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine
*sm
)
4279 sm
->dot11RSNAStatsTKIPLocalMICFailures
++;
4283 const u8
* wpa_auth_get_wpa_ie(struct wpa_authenticator
*wpa_auth
, size_t *len
)
4285 if (wpa_auth
== NULL
)
4287 *len
= wpa_auth
->wpa_ie_len
;
4288 return wpa_auth
->wpa_ie
;
4292 int wpa_auth_pmksa_add(struct wpa_state_machine
*sm
, const u8
*pmk
,
4293 unsigned int pmk_len
,
4294 int session_timeout
, struct eapol_state_machine
*eapol
)
4296 if (sm
== NULL
|| sm
->wpa
!= WPA_VERSION_WPA2
||
4297 sm
->wpa_auth
->conf
.disable_pmksa_caching
)
4300 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
4301 if (pmk_len
> PMK_LEN_SUITE_B_192
)
4302 pmk_len
= PMK_LEN_SUITE_B_192
;
4303 } else if (pmk_len
> PMK_LEN
) {
4307 if (pmksa_cache_auth_add(sm
->wpa_auth
->pmksa
, pmk
, pmk_len
, NULL
,
4308 sm
->PTK
.kck
, sm
->PTK
.kck_len
,
4309 sm
->wpa_auth
->addr
, sm
->addr
, session_timeout
,
4310 eapol
, sm
->wpa_key_mgmt
))
4317 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator
*wpa_auth
,
4318 const u8
*pmk
, size_t len
, const u8
*sta_addr
,
4319 int session_timeout
,
4320 struct eapol_state_machine
*eapol
)
4322 if (wpa_auth
== NULL
)
4325 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, len
, NULL
,
4328 sta_addr
, session_timeout
, eapol
,
4329 WPA_KEY_MGMT_IEEE8021X
))
4336 int wpa_auth_pmksa_add_sae(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4337 const u8
*pmk
, const u8
*pmkid
)
4339 if (wpa_auth
->conf
.disable_pmksa_caching
)
4342 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, PMK_LEN
, pmkid
,
4344 wpa_auth
->addr
, addr
, 0, NULL
,
4352 void wpa_auth_add_sae_pmkid(struct wpa_state_machine
*sm
, const u8
*pmkid
)
4354 os_memcpy(sm
->pmkid
, pmkid
, PMKID_LEN
);
4359 int wpa_auth_pmksa_add2(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4360 const u8
*pmk
, size_t pmk_len
, const u8
*pmkid
,
4361 int session_timeout
, int akmp
)
4363 if (wpa_auth
->conf
.disable_pmksa_caching
)
4366 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, pmk_len
, pmkid
,
4367 NULL
, 0, wpa_auth
->addr
, addr
, session_timeout
,
4375 void wpa_auth_pmksa_remove(struct wpa_authenticator
*wpa_auth
,
4378 struct rsn_pmksa_cache_entry
*pmksa
;
4380 if (wpa_auth
== NULL
|| wpa_auth
->pmksa
== NULL
)
4382 pmksa
= pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, NULL
);
4384 wpa_printf(MSG_DEBUG
, "WPA: Remove PMKSA cache entry for "
4385 MACSTR
" based on request", MAC2STR(sta_addr
));
4386 pmksa_cache_free_entry(wpa_auth
->pmksa
, pmksa
);
4391 int wpa_auth_pmksa_list(struct wpa_authenticator
*wpa_auth
, char *buf
,
4394 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4396 return pmksa_cache_auth_list(wpa_auth
->pmksa
, buf
, len
);
4400 void wpa_auth_pmksa_flush(struct wpa_authenticator
*wpa_auth
)
4402 if (wpa_auth
&& wpa_auth
->pmksa
)
4403 pmksa_cache_auth_flush(wpa_auth
->pmksa
);
4407 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4410 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4411 char *buf
, size_t len
)
4413 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4416 return pmksa_cache_auth_list_mesh(wpa_auth
->pmksa
, addr
, buf
, len
);
4420 struct rsn_pmksa_cache_entry
*
4421 wpa_auth_pmksa_create_entry(const u8
*aa
, const u8
*spa
, const u8
*pmk
,
4422 const u8
*pmkid
, int expiration
)
4424 struct rsn_pmksa_cache_entry
*entry
;
4425 struct os_reltime now
;
4427 entry
= pmksa_cache_auth_create_entry(pmk
, PMK_LEN
, pmkid
, NULL
, 0, aa
,
4428 spa
, 0, NULL
, WPA_KEY_MGMT_SAE
);
4432 os_get_reltime(&now
);
4433 entry
->expiration
= now
.sec
+ expiration
;
4438 int wpa_auth_pmksa_add_entry(struct wpa_authenticator
*wpa_auth
,
4439 struct rsn_pmksa_cache_entry
*entry
)
4443 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4446 ret
= pmksa_cache_auth_add_entry(wpa_auth
->pmksa
, entry
);
4448 wpa_printf(MSG_DEBUG
,
4449 "RSN: Failed to store external PMKSA cache for "
4450 MACSTR
, MAC2STR(entry
->spa
));
4455 #endif /* CONFIG_MESH */
4456 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4459 struct rsn_pmksa_cache_entry
*
4460 wpa_auth_pmksa_get(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
,
4463 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4465 return pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, pmkid
);
4469 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry
*pmksa
,
4470 struct wpa_state_machine
*sm
,
4471 struct wpa_authenticator
*wpa_auth
,
4478 os_memcpy(pmk
, pmksa
->pmk
, PMK_LEN
);
4479 os_memcpy(pmkid
, pmksa
->pmkid
, PMKID_LEN
);
4480 os_memcpy(wpa_auth
->dot11RSNAPMKIDUsed
, pmksa
->pmkid
, PMKID_LEN
);
4485 * Remove and free the group from wpa_authenticator. This is triggered by a
4486 * callback to make sure nobody is currently iterating the group list while it
4489 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
4490 struct wpa_group
*group
)
4492 struct wpa_group
*prev
= wpa_auth
->group
;
4494 wpa_printf(MSG_DEBUG
, "WPA: Remove group state machine for VLAN-ID %d",
4498 if (prev
->next
== group
) {
4499 /* This never frees the special first group as needed */
4500 prev
->next
= group
->next
;
4510 /* Increase the reference counter for group */
4511 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
4512 struct wpa_group
*group
)
4514 /* Skip the special first group */
4515 if (wpa_auth
->group
== group
)
4518 group
->references
++;
4522 /* Decrease the reference counter and maybe free the group */
4523 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
4524 struct wpa_group
*group
)
4526 /* Skip the special first group */
4527 if (wpa_auth
->group
== group
)
4530 group
->references
--;
4531 if (group
->references
)
4533 wpa_group_free(wpa_auth
, group
);
4538 * Add a group that has its references counter set to zero. Caller needs to
4539 * call wpa_group_get() on the return value to mark the entry in use.
4541 static struct wpa_group
*
4542 wpa_auth_add_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4544 struct wpa_group
*group
;
4546 if (wpa_auth
== NULL
|| wpa_auth
->group
== NULL
)
4549 wpa_printf(MSG_DEBUG
, "WPA: Add group state machine for VLAN-ID %d",
4551 group
= wpa_group_init(wpa_auth
, vlan_id
, 0);
4555 group
->next
= wpa_auth
->group
->next
;
4556 wpa_auth
->group
->next
= group
;
4563 * Enforce that the group state machine for the VLAN is running, increase
4564 * reference counter as interface is up. References might have been increased
4565 * even if a negative value is returned.
4566 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4568 int wpa_auth_ensure_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4570 struct wpa_group
*group
;
4572 if (wpa_auth
== NULL
)
4575 group
= wpa_auth
->group
;
4577 if (group
->vlan_id
== vlan_id
)
4579 group
= group
->next
;
4582 if (group
== NULL
) {
4583 group
= wpa_auth_add_group(wpa_auth
, vlan_id
);
4588 wpa_printf(MSG_DEBUG
,
4589 "WPA: Ensure group state machine running for VLAN ID %d",
4592 wpa_group_get(wpa_auth
, group
);
4593 group
->num_setup_iface
++;
4595 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4603 * Decrease reference counter, expected to be zero afterwards.
4604 * returns: -1 on error (group not found, group in fail state)
4605 * -2 if wpa_group is still referenced
4608 int wpa_auth_release_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4610 struct wpa_group
*group
;
4613 if (wpa_auth
== NULL
)
4616 group
= wpa_auth
->group
;
4618 if (group
->vlan_id
== vlan_id
)
4620 group
= group
->next
;
4626 wpa_printf(MSG_DEBUG
,
4627 "WPA: Try stopping group state machine for VLAN ID %d",
4630 if (group
->num_setup_iface
<= 0) {
4631 wpa_printf(MSG_ERROR
,
4632 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4636 group
->num_setup_iface
--;
4638 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4641 if (group
->references
> 1) {
4642 wpa_printf(MSG_DEBUG
,
4643 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4648 wpa_group_put(wpa_auth
, group
);
4654 int wpa_auth_sta_set_vlan(struct wpa_state_machine
*sm
, int vlan_id
)
4656 struct wpa_group
*group
;
4658 if (sm
== NULL
|| sm
->wpa_auth
== NULL
)
4661 group
= sm
->wpa_auth
->group
;
4663 if (group
->vlan_id
== vlan_id
)
4665 group
= group
->next
;
4668 if (group
== NULL
) {
4669 group
= wpa_auth_add_group(sm
->wpa_auth
, vlan_id
);
4674 if (sm
->group
== group
)
4677 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4680 wpa_printf(MSG_DEBUG
, "WPA: Moving STA " MACSTR
" to use group state "
4681 "machine for VLAN ID %d", MAC2STR(sm
->addr
), vlan_id
);
4683 wpa_group_get(sm
->wpa_auth
, group
);
4684 wpa_group_put(sm
->wpa_auth
, sm
->group
);
4691 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator
*wpa_auth
,
4692 struct wpa_state_machine
*sm
, int ack
)
4694 if (wpa_auth
== NULL
|| sm
== NULL
)
4696 wpa_printf(MSG_DEBUG
, "WPA: EAPOL-Key TX status for STA " MACSTR
4697 " ack=%d", MAC2STR(sm
->addr
), ack
);
4698 if (sm
->pending_1_of_4_timeout
&& ack
) {
4700 * Some deployed supplicant implementations update their SNonce
4701 * for each EAPOL-Key 2/4 message even within the same 4-way
4702 * handshake and then fail to use the first SNonce when
4703 * deriving the PTK. This results in unsuccessful 4-way
4704 * handshake whenever the relatively short initial timeout is
4705 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4706 * around this by increasing the timeout now that we know that
4707 * the station has received the frame.
4709 int timeout_ms
= eapol_key_timeout_subseq
;
4710 wpa_printf(MSG_DEBUG
, "WPA: Increase initial EAPOL-Key 1/4 "
4711 "timeout by %u ms because of acknowledged frame",
4713 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
4714 eloop_register_timeout(timeout_ms
/ 1000,
4715 (timeout_ms
% 1000) * 1000,
4716 wpa_send_eapol_timeout
, wpa_auth
, sm
);
4719 #ifdef CONFIG_TESTING_OPTIONS
4720 if (sm
->eapol_status_cb
) {
4721 sm
->eapol_status_cb(sm
->eapol_status_cb_ctx1
,
4722 sm
->eapol_status_cb_ctx2
);
4723 sm
->eapol_status_cb
= NULL
;
4725 #endif /* CONFIG_TESTING_OPTIONS */
4729 int wpa_auth_uses_sae(struct wpa_state_machine
*sm
)
4733 return wpa_key_mgmt_sae(sm
->wpa_key_mgmt
);
4737 int wpa_auth_uses_ft_sae(struct wpa_state_machine
*sm
)
4741 return sm
->wpa_key_mgmt
== WPA_KEY_MGMT_FT_SAE
;
4746 int wpa_auth_get_ip_addr(struct wpa_state_machine
*sm
, u8
*addr
)
4748 if (sm
== NULL
|| WPA_GET_BE32(sm
->ip_addr
) == 0)
4750 os_memcpy(addr
, sm
->ip_addr
, 4);
4753 #endif /* CONFIG_P2P */
4756 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator
*wpa_auth
,
4757 struct radius_das_attrs
*attr
)
4759 return pmksa_cache_auth_radius_das_disconnect(wpa_auth
->pmksa
, attr
);
4763 void wpa_auth_reconfig_group_keys(struct wpa_authenticator
*wpa_auth
)
4765 struct wpa_group
*group
;
4769 for (group
= wpa_auth
->group
; group
; group
= group
->next
)
4770 wpa_group_config_group_keys(wpa_auth
, group
);
4776 struct wpa_auth_fils_iter_data
{
4777 struct wpa_authenticator
*auth
;
4779 struct rsn_pmksa_cache_entry
*pmksa
;
4785 static int wpa_auth_fils_iter(struct wpa_authenticator
*a
, void *ctx
)
4787 struct wpa_auth_fils_iter_data
*data
= ctx
;
4789 if (a
== data
->auth
|| !a
->conf
.fils_cache_id_set
||
4790 os_memcmp(a
->conf
.fils_cache_id
, data
->cache_id
,
4791 FILS_CACHE_ID_LEN
) != 0)
4793 data
->pmksa
= pmksa_cache_auth_get(a
->pmksa
, data
->spa
, data
->pmkid
);
4794 return data
->pmksa
!= NULL
;
4798 struct rsn_pmksa_cache_entry
*
4799 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator
*wpa_auth
,
4800 const u8
*sta_addr
, const u8
*pmkid
)
4802 struct wpa_auth_fils_iter_data idata
;
4804 if (!wpa_auth
->conf
.fils_cache_id_set
)
4806 idata
.auth
= wpa_auth
;
4807 idata
.cache_id
= wpa_auth
->conf
.fils_cache_id
;
4809 idata
.spa
= sta_addr
;
4810 idata
.pmkid
= pmkid
;
4811 wpa_auth_for_each_auth(wpa_auth
, wpa_auth_fils_iter
, &idata
);
4816 #ifdef CONFIG_IEEE80211R_AP
4817 int wpa_auth_write_fte(struct wpa_authenticator
*wpa_auth
, int use_sha384
,
4818 u8
*buf
, size_t len
)
4820 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
4822 return wpa_write_ftie(conf
, use_sha384
, conf
->r0_key_holder
,
4823 conf
->r0_key_holder_len
,
4824 NULL
, NULL
, buf
, len
, NULL
, 0);
4826 #endif /* CONFIG_IEEE80211R_AP */
4829 void wpa_auth_get_fils_aead_params(struct wpa_state_machine
*sm
,
4830 u8
*fils_anonce
, u8
*fils_snonce
,
4831 u8
*fils_kek
, size_t *fils_kek_len
)
4833 os_memcpy(fils_anonce
, sm
->ANonce
, WPA_NONCE_LEN
);
4834 os_memcpy(fils_snonce
, sm
->SNonce
, WPA_NONCE_LEN
);
4835 os_memcpy(fils_kek
, sm
->PTK
.kek
, WPA_KEK_MAX_LEN
);
4836 *fils_kek_len
= sm
->PTK
.kek_len
;
4839 #endif /* CONFIG_FILS */
4842 void wpa_auth_set_auth_alg(struct wpa_state_machine
*sm
, u16 auth_alg
)
4845 sm
->auth_alg
= auth_alg
;
4850 void wpa_auth_set_dpp_z(struct wpa_state_machine
*sm
, const struct wpabuf
*z
)
4853 wpabuf_clear_free(sm
->dpp_z
);
4854 sm
->dpp_z
= z
? wpabuf_dup(z
) : NULL
;
4857 #endif /* CONFIG_DPP2 */
4860 #ifdef CONFIG_TESTING_OPTIONS
4862 int wpa_auth_resend_m1(struct wpa_state_machine
*sm
, int change_anonce
,
4863 void (*cb
)(void *ctx1
, void *ctx2
),
4864 void *ctx1
, void *ctx2
)
4866 const u8
*anonce
= sm
->ANonce
;
4867 u8 anonce_buf
[WPA_NONCE_LEN
];
4869 if (change_anonce
) {
4870 if (random_get_bytes(anonce_buf
, WPA_NONCE_LEN
))
4872 anonce
= anonce_buf
;
4875 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
4876 "sending 1/4 msg of 4-Way Handshake (TESTING)");
4877 wpa_send_eapol(sm
->wpa_auth
, sm
,
4878 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
4879 anonce
, NULL
, 0, 0, 0);
4884 int wpa_auth_resend_m3(struct wpa_state_machine
*sm
,
4885 void (*cb
)(void *ctx1
, void *ctx2
),
4886 void *ctx1
, void *ctx2
)
4888 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
;
4889 #ifdef CONFIG_IEEE80211W
4891 #endif /* CONFIG_IEEE80211W */
4892 size_t gtk_len
, kde_len
;
4893 struct wpa_group
*gsm
= sm
->group
;
4895 int wpa_ie_len
, secure
, keyidx
, encr
= 0;
4897 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
4898 GTK[GN], IGTK, [FTIE], [TIE * 2])
4902 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
4903 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
4904 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
4905 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
4906 if (sm
->wpa
== WPA_VERSION_WPA
&&
4907 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
4908 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
4909 /* WPA-only STA, remove RSN IE and possible MDIE */
4910 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
4911 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
4912 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
4913 wpa_ie_len
= wpa_ie
[1] + 2;
4915 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
4916 "sending 3/4 msg of 4-Way Handshake (TESTING)");
4917 if (sm
->wpa
== WPA_VERSION_WPA2
) {
4918 /* WPA2 send GTK in the 4-way handshake */
4920 gtk
= gsm
->GTK
[gsm
->GN
- 1];
4921 gtk_len
= gsm
->GTK_len
;
4926 /* WPA does not include GTK in msg 3/4 */
4932 if (sm
->rx_eapol_key_secure
) {
4934 * It looks like Windows 7 supplicant tries to use
4935 * Secure bit in msg 2/4 after having reported Michael
4936 * MIC failure and it then rejects the 4-way handshake
4937 * if msg 3/4 does not set Secure bit. Work around this
4938 * by setting the Secure bit here even in the case of
4939 * WPA if the supplicant used it first.
4941 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
4942 "STA used Secure bit in WPA msg 2/4 - "
4943 "set Secure for 3/4 as workaround");
4948 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
4950 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
4951 #ifdef CONFIG_IEEE80211R_AP
4952 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
4953 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
4954 kde_len
+= 300; /* FTIE + 2 * TIE */
4956 #endif /* CONFIG_IEEE80211R_AP */
4957 kde
= os_malloc(kde_len
);
4962 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
4964 #ifdef CONFIG_IEEE80211R_AP
4965 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
4970 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
4972 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
4973 "PMKR1Name into RSN IE in EAPOL-Key data");
4980 #endif /* CONFIG_IEEE80211R_AP */
4983 hdr
[0] = keyidx
& 0x03;
4985 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
4988 #ifdef CONFIG_IEEE80211W
4990 pos
= ieee80211w_kde_add(sm
, pos
);
4991 if (pos
- opos
>= 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
) {
4992 /* skip KDE header and keyid */
4993 opos
+= 2 + RSN_SELECTOR_LEN
+ 2;
4994 os_memset(opos
, 0, 6); /* clear PN */
4996 #endif /* CONFIG_IEEE80211W */
4997 if (ocv_oci_add(sm
, &pos
) < 0) {
5002 #ifdef CONFIG_IEEE80211R_AP
5003 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
5005 struct wpa_auth_config
*conf
;
5007 conf
= &sm
->wpa_auth
->conf
;
5008 if (sm
->assoc_resp_ftie
&&
5009 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
5010 os_memcpy(pos
, sm
->assoc_resp_ftie
,
5011 2 + sm
->assoc_resp_ftie
[1]);
5012 res
= 2 + sm
->assoc_resp_ftie
[1];
5014 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
5016 res
= wpa_write_ftie(conf
, use_sha384
,
5017 conf
->r0_key_holder
,
5018 conf
->r0_key_holder_len
,
5020 kde
+ kde_len
- pos
,
5024 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
5025 "into EAPOL-Key Key Data");
5031 /* TIE[ReassociationDeadline] (TU) */
5032 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
5034 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
5035 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
5038 /* TIE[KeyLifetime] (seconds) */
5039 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
5041 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
5042 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
);
5045 #endif /* CONFIG_IEEE80211R_AP */
5047 wpa_send_eapol(sm
->wpa_auth
, sm
,
5048 (secure
? WPA_KEY_INFO_SECURE
: 0) |
5049 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
5050 WPA_KEY_INFO_MIC
: 0) |
5051 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
5052 WPA_KEY_INFO_KEY_TYPE
,
5053 _rsc
, sm
->ANonce
, kde
, pos
- kde
, keyidx
, encr
);
5059 int wpa_auth_resend_group_m1(struct wpa_state_machine
*sm
,
5060 void (*cb
)(void *ctx1
, void *ctx2
),
5061 void *ctx1
, void *ctx2
)
5063 u8 rsc
[WPA_KEY_RSC_LEN
];
5064 struct wpa_group
*gsm
= sm
->group
;
5066 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
5067 #ifdef CONFIG_IEEE80211W
5069 #endif /* CONFIG_IEEE80211W */
5073 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
5074 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
5076 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5077 "sending 1/2 msg of Group Key Handshake (TESTING)");
5079 gtk
= gsm
->GTK
[gsm
->GN
- 1];
5080 if (sm
->wpa
== WPA_VERSION_WPA2
) {
5081 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
5082 ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
5083 kde_buf
= os_malloc(kde_len
);
5084 if (kde_buf
== NULL
)
5087 kde
= pos
= kde_buf
;
5088 hdr
[0] = gsm
->GN
& 0x03;
5090 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
5092 #ifdef CONFIG_IEEE80211W
5094 pos
= ieee80211w_kde_add(sm
, pos
);
5096 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
) {
5097 /* skip KDE header and keyid */
5098 opos
+= 2 + RSN_SELECTOR_LEN
+ 2;
5099 os_memset(opos
, 0, 6); /* clear PN */
5101 #endif /* CONFIG_IEEE80211W */
5102 if (ocv_oci_add(sm
, &pos
) < 0) {
5106 kde_len
= pos
- kde
;
5109 kde_len
= gsm
->GTK_len
;
5112 sm
->eapol_status_cb
= cb
;
5113 sm
->eapol_status_cb_ctx1
= ctx1
;
5114 sm
->eapol_status_cb_ctx2
= ctx2
;
5116 wpa_send_eapol(sm
->wpa_auth
, sm
,
5117 WPA_KEY_INFO_SECURE
|
5118 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
5119 WPA_KEY_INFO_MIC
: 0) |
5121 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
5122 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
5129 int wpa_auth_rekey_gtk(struct wpa_authenticator
*wpa_auth
)
5133 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
5134 return eloop_register_timeout(0, 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
5137 #endif /* CONFIG_TESTING_OPTIONS */