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
);
703 void wpa_auth_sta_deinit(struct wpa_state_machine
*sm
)
708 if (sm
->wpa_auth
->conf
.wpa_strict_rekey
&& sm
->has_GTK
) {
709 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
710 "strict rekeying - force GTK rekey since STA "
712 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk
,
713 sm
->wpa_auth
, NULL
) == -1)
714 eloop_register_timeout(0, 500000, wpa_rekey_gtk
, sm
->wpa_auth
,
718 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
719 sm
->pending_1_of_4_timeout
= 0;
720 eloop_cancel_timeout(wpa_sm_call_step
, sm
, NULL
);
721 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
722 #ifdef CONFIG_IEEE80211R_AP
723 wpa_ft_sta_deinit(sm
);
724 #endif /* CONFIG_IEEE80211R_AP */
725 if (sm
->in_step_loop
) {
726 /* Must not free state machine while wpa_sm_step() is running.
727 * Freeing will be completed in the end of wpa_sm_step(). */
728 wpa_printf(MSG_DEBUG
, "WPA: Registering pending STA state "
729 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
730 sm
->pending_deinit
= 1;
736 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
)
741 sm
->PTKRequest
= TRUE
;
746 static int wpa_replay_counter_valid(struct wpa_key_replay_counter
*ctr
,
747 const u8
*replay_counter
)
750 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
753 if (os_memcmp(replay_counter
, ctr
[i
].counter
,
754 WPA_REPLAY_COUNTER_LEN
) == 0)
761 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter
*ctr
,
762 const u8
*replay_counter
)
765 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
767 (replay_counter
== NULL
||
768 os_memcmp(replay_counter
, ctr
[i
].counter
,
769 WPA_REPLAY_COUNTER_LEN
) == 0))
770 ctr
[i
].valid
= FALSE
;
775 #ifdef CONFIG_IEEE80211R_AP
776 static int ft_check_msg_2_of_4(struct wpa_authenticator
*wpa_auth
,
777 struct wpa_state_machine
*sm
,
778 struct wpa_eapol_ie_parse
*kde
)
780 struct wpa_ie_data ie
;
781 struct rsn_mdie
*mdie
;
783 if (wpa_parse_wpa_ie_rsn(kde
->rsn_ie
, kde
->rsn_ie_len
, &ie
) < 0 ||
784 ie
.num_pmkid
!= 1 || ie
.pmkid
== NULL
) {
785 wpa_printf(MSG_DEBUG
, "FT: No PMKR1Name in "
786 "FT 4-way handshake message 2/4");
790 os_memcpy(sm
->sup_pmk_r1_name
, ie
.pmkid
, PMKID_LEN
);
791 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Supplicant",
792 sm
->sup_pmk_r1_name
, PMKID_LEN
);
794 if (!kde
->mdie
|| !kde
->ftie
) {
795 wpa_printf(MSG_DEBUG
, "FT: No %s in FT 4-way handshake "
796 "message 2/4", kde
->mdie
? "FTIE" : "MDIE");
800 mdie
= (struct rsn_mdie
*) (kde
->mdie
+ 2);
801 if (kde
->mdie
[1] < sizeof(struct rsn_mdie
) ||
802 os_memcmp(wpa_auth
->conf
.mobility_domain
, mdie
->mobility_domain
,
803 MOBILITY_DOMAIN_ID_LEN
) != 0) {
804 wpa_printf(MSG_DEBUG
, "FT: MDIE mismatch");
808 if (sm
->assoc_resp_ftie
&&
809 (kde
->ftie
[1] != sm
->assoc_resp_ftie
[1] ||
810 os_memcmp(kde
->ftie
, sm
->assoc_resp_ftie
,
811 2 + sm
->assoc_resp_ftie
[1]) != 0)) {
812 wpa_printf(MSG_DEBUG
, "FT: FTIE mismatch");
813 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 2/4",
814 kde
->ftie
, kde
->ftie_len
);
815 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)AssocResp",
816 sm
->assoc_resp_ftie
, 2 + sm
->assoc_resp_ftie
[1]);
822 #endif /* CONFIG_IEEE80211R_AP */
825 static int wpa_receive_error_report(struct wpa_authenticator
*wpa_auth
,
826 struct wpa_state_machine
*sm
, int group
)
828 /* Supplicant reported a Michael MIC error */
829 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
830 "received EAPOL-Key Error Request "
831 "(STA detected Michael MIC failure (group=%d))",
834 if (group
&& wpa_auth
->conf
.wpa_group
!= WPA_CIPHER_TKIP
) {
835 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
836 "ignore Michael MIC failure report since "
837 "group cipher is not TKIP");
838 } else if (!group
&& sm
->pairwise
!= WPA_CIPHER_TKIP
) {
839 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
840 "ignore Michael MIC failure report since "
841 "pairwise cipher is not TKIP");
843 if (wpa_auth_mic_failure_report(wpa_auth
, sm
->addr
) > 0)
844 return 1; /* STA entry was removed */
845 sm
->dot11RSNAStatsTKIPRemoteMICFailures
++;
846 wpa_auth
->dot11RSNAStatsTKIPRemoteMICFailures
++;
850 * Error report is not a request for a new key handshake, but since
851 * Authenticator may do it, let's change the keys now anyway.
853 wpa_request_new_ptk(sm
);
858 static int wpa_try_alt_snonce(struct wpa_state_machine
*sm
, u8
*data
,
863 const u8
*pmk
= NULL
;
867 os_memset(&PTK
, 0, sizeof(PTK
));
869 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
870 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
871 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
872 sm
->p2p_dev_addr
, pmk
, &pmk_len
,
876 #ifdef CONFIG_IEEE80211R_AP
877 if (wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
878 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
879 sm
->xxkey_len
= pmk_len
;
881 #endif /* CONFIG_IEEE80211R_AP */
884 pmk_len
= sm
->pmk_len
;
887 if (wpa_derive_ptk(sm
, sm
->alt_SNonce
, pmk
, pmk_len
, &PTK
) < 0)
890 if (wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
891 data
, data_len
) == 0) {
892 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
893 sm
->pmk_len
= pmk_len
;
898 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
899 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
))
904 wpa_printf(MSG_DEBUG
,
905 "WPA: Earlier SNonce did not result in matching MIC");
909 wpa_printf(MSG_DEBUG
,
910 "WPA: Earlier SNonce resulted in matching MIC");
911 sm
->alt_snonce_valid
= 0;
913 if (vlan_id
&& wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
914 wpa_auth_update_vlan(sm
->wpa_auth
, sm
->addr
, vlan_id
) < 0)
917 os_memcpy(sm
->SNonce
, sm
->alt_SNonce
, WPA_NONCE_LEN
);
918 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
919 sm
->PTK_valid
= TRUE
;
925 void wpa_receive(struct wpa_authenticator
*wpa_auth
,
926 struct wpa_state_machine
*sm
,
927 u8
*data
, size_t data_len
)
929 struct ieee802_1x_hdr
*hdr
;
930 struct wpa_eapol_key
*key
;
931 u16 key_info
, key_data_length
;
932 enum { PAIRWISE_2
, PAIRWISE_4
, GROUP_2
, REQUEST
} msg
;
934 struct wpa_eapol_ie_parse kde
;
936 size_t keyhdrlen
, mic_len
;
939 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
941 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL data", data
, data_len
);
943 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
944 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
946 if (data_len
< sizeof(*hdr
) + keyhdrlen
) {
947 wpa_printf(MSG_DEBUG
, "WPA: Ignore too short EAPOL-Key frame");
951 hdr
= (struct ieee802_1x_hdr
*) data
;
952 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
953 mic
= (u8
*) (key
+ 1);
954 key_info
= WPA_GET_BE16(key
->key_info
);
955 key_data
= mic
+ mic_len
+ 2;
956 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
957 wpa_printf(MSG_DEBUG
, "WPA: Received EAPOL-Key from " MACSTR
958 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
959 MAC2STR(sm
->addr
), key_info
, key
->type
,
960 (unsigned int) mic_len
, key_data_length
);
961 wpa_hexdump(MSG_MSGDUMP
,
962 "WPA: EAPOL-Key header (ending before Key MIC)",
964 wpa_hexdump(MSG_MSGDUMP
, "WPA: EAPOL-Key Key MIC",
966 if (key_data_length
> data_len
- sizeof(*hdr
) - keyhdrlen
) {
967 wpa_printf(MSG_INFO
, "WPA: Invalid EAPOL-Key frame - "
968 "key_data overflow (%d > %lu)",
970 (unsigned long) (data_len
- sizeof(*hdr
) -
975 if (sm
->wpa
== WPA_VERSION_WPA2
) {
976 if (key
->type
== EAPOL_KEY_TYPE_WPA
) {
978 * Some deployed station implementations seem to send
979 * msg 4/4 with incorrect type value in WPA2 mode.
981 wpa_printf(MSG_DEBUG
, "Workaround: Allow EAPOL-Key "
982 "with unexpected WPA type in RSN mode");
983 } else if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
984 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
985 "unexpected type %d in RSN mode",
990 if (key
->type
!= EAPOL_KEY_TYPE_WPA
) {
991 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
992 "unexpected type %d in WPA mode",
998 wpa_hexdump(MSG_DEBUG
, "WPA: Received Key Nonce", key
->key_nonce
,
1000 wpa_hexdump(MSG_DEBUG
, "WPA: Received Replay Counter",
1001 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1003 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1006 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
1007 wpa_printf(MSG_DEBUG
, "WPA: Ignore SMK message");
1011 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1014 } else if (!(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
1016 msgtxt
= "2/2 Group";
1017 } else if (key_data_length
== 0 ||
1018 (mic_len
== 0 && (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) &&
1019 key_data_length
== AES_BLOCK_SIZE
)) {
1021 msgtxt
= "4/4 Pairwise";
1024 msgtxt
= "2/4 Pairwise";
1027 if (msg
== REQUEST
|| msg
== PAIRWISE_2
|| msg
== PAIRWISE_4
||
1029 u16 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
1030 if (sm
->pairwise
== WPA_CIPHER_CCMP
||
1031 sm
->pairwise
== WPA_CIPHER_GCMP
) {
1032 if (wpa_use_cmac(sm
->wpa_key_mgmt
) &&
1033 !wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1034 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1035 wpa_auth_logger(wpa_auth
, sm
->addr
,
1037 "advertised support for "
1038 "AES-128-CMAC, but did not "
1043 if (!wpa_use_cmac(sm
->wpa_key_mgmt
) &&
1044 !wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1045 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1046 wpa_auth_logger(wpa_auth
, sm
->addr
,
1048 "did not use HMAC-SHA1-AES "
1054 if (wpa_use_akm_defined(sm
->wpa_key_mgmt
) &&
1055 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
1056 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1057 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1062 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1063 if (sm
->req_replay_counter_used
&&
1064 os_memcmp(key
->replay_counter
, sm
->req_replay_counter
,
1065 WPA_REPLAY_COUNTER_LEN
) <= 0) {
1066 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
1067 "received EAPOL-Key request with "
1068 "replayed counter");
1073 if (!(key_info
& WPA_KEY_INFO_REQUEST
) &&
1074 !wpa_replay_counter_valid(sm
->key_replay
, key
->replay_counter
)) {
1077 if (msg
== PAIRWISE_2
&&
1078 wpa_replay_counter_valid(sm
->prev_key_replay
,
1079 key
->replay_counter
) &&
1080 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1081 os_memcmp(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0)
1084 * Some supplicant implementations (e.g., Windows XP
1085 * WZC) update SNonce for each EAPOL-Key 2/4. This
1086 * breaks the workaround on accepting any of the
1087 * pending requests, so allow the SNonce to be updated
1088 * even if we have already sent out EAPOL-Key 3/4.
1090 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1091 "Process SNonce update from STA "
1092 "based on retransmitted EAPOL-Key "
1094 sm
->update_snonce
= 1;
1095 os_memcpy(sm
->alt_SNonce
, sm
->SNonce
, WPA_NONCE_LEN
);
1096 sm
->alt_snonce_valid
= TRUE
;
1097 os_memcpy(sm
->alt_replay_counter
,
1098 sm
->key_replay
[0].counter
,
1099 WPA_REPLAY_COUNTER_LEN
);
1100 goto continue_processing
;
1103 if (msg
== PAIRWISE_4
&& sm
->alt_snonce_valid
&&
1104 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
1105 os_memcmp(key
->replay_counter
, sm
->alt_replay_counter
,
1106 WPA_REPLAY_COUNTER_LEN
) == 0) {
1108 * Supplicant may still be using the old SNonce since
1109 * there was two EAPOL-Key 2/4 messages and they had
1110 * different SNonce values.
1112 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1113 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1114 goto continue_processing
;
1117 if (msg
== PAIRWISE_2
&&
1118 wpa_replay_counter_valid(sm
->prev_key_replay
,
1119 key
->replay_counter
) &&
1120 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
) {
1121 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1122 "ignore retransmitted EAPOL-Key %s - "
1123 "SNonce did not change", msgtxt
);
1125 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1126 "received EAPOL-Key %s with "
1127 "unexpected replay counter", msgtxt
);
1129 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
1130 if (!sm
->key_replay
[i
].valid
)
1132 wpa_hexdump(MSG_DEBUG
, "pending replay counter",
1133 sm
->key_replay
[i
].counter
,
1134 WPA_REPLAY_COUNTER_LEN
);
1136 wpa_hexdump(MSG_DEBUG
, "received replay counter",
1137 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1141 continue_processing
:
1143 if (sm
->wpa
== WPA_VERSION_WPA2
&& mic_len
== 0 &&
1144 !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1145 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1146 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1149 #endif /* CONFIG_FILS */
1153 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKSTART
&&
1154 sm
->wpa_ptk_state
!= WPA_PTK_PTKCALCNEGOTIATING
&&
1155 (!sm
->update_snonce
||
1156 sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
)) {
1157 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1158 "received EAPOL-Key msg 2/4 in "
1159 "invalid state (%d) - dropped",
1163 random_add_randomness(key
->key_nonce
, WPA_NONCE_LEN
);
1164 if (sm
->group
->reject_4way_hs_for_entropy
) {
1166 * The system did not have enough entropy to generate
1167 * strong random numbers. Reject the first 4-way
1168 * handshake(s) and collect some entropy based on the
1169 * information from it. Once enough entropy is
1170 * available, the next atempt will trigger GMK/Key
1171 * Counter update and the station will be allowed to
1174 wpa_printf(MSG_DEBUG
, "WPA: Reject 4-way handshake to "
1175 "collect more entropy for random number "
1177 random_mark_pool_ready();
1178 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
1179 WLAN_REASON_PREV_AUTH_NOT_VALID
);
1184 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
||
1186 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1187 "received EAPOL-Key msg 4/4 in "
1188 "invalid state (%d) - dropped",
1194 if (sm
->wpa_ptk_group_state
!= WPA_PTK_GROUP_REKEYNEGOTIATING
1195 || !sm
->PTK_valid
) {
1196 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1197 "received EAPOL-Key msg 2/2 in "
1198 "invalid state (%d) - dropped",
1199 sm
->wpa_ptk_group_state
);
1207 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1208 "received EAPOL-Key frame (%s)", msgtxt
);
1210 if (key_info
& WPA_KEY_INFO_ACK
) {
1211 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1212 "received invalid EAPOL-Key: Key Ack set");
1216 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1217 !(key_info
& WPA_KEY_INFO_MIC
)) {
1218 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1219 "received invalid EAPOL-Key: Key MIC not set");
1224 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1225 (key_info
& WPA_KEY_INFO_MIC
)) {
1226 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1227 "received invalid EAPOL-Key: Key MIC set");
1230 #endif /* CONFIG_FILS */
1232 sm
->MICVerified
= FALSE
;
1233 if (sm
->PTK_valid
&& !sm
->update_snonce
) {
1235 wpa_verify_key_mic(sm
->wpa_key_mgmt
, sm
->pmk_len
, &sm
->PTK
,
1237 (msg
!= PAIRWISE_4
|| !sm
->alt_snonce_valid
||
1238 wpa_try_alt_snonce(sm
, data
, data_len
))) {
1239 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1240 "received EAPOL-Key with invalid MIC");
1242 wpa_printf(MSG_INFO
,
1243 "TEST: Ignore Key MIC failure for fuzz testing");
1245 #endif /* TEST_FUZZ */
1250 wpa_aead_decrypt(sm
, &sm
->PTK
, data
, data_len
,
1251 &key_data_length
) < 0) {
1252 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1253 "received EAPOL-Key with invalid MIC");
1255 wpa_printf(MSG_INFO
,
1256 "TEST: Ignore Key MIC failure for fuzz testing");
1258 #endif /* TEST_FUZZ */
1261 #endif /* CONFIG_FILS */
1264 #endif /* TEST_FUZZ */
1265 sm
->MICVerified
= TRUE
;
1266 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
1267 sm
->pending_1_of_4_timeout
= 0;
1270 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1271 if (sm
->MICVerified
) {
1272 sm
->req_replay_counter_used
= 1;
1273 os_memcpy(sm
->req_replay_counter
, key
->replay_counter
,
1274 WPA_REPLAY_COUNTER_LEN
);
1276 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1277 "received EAPOL-Key request with "
1283 * TODO: should decrypt key data field if encryption was used;
1284 * even though MAC address KDE is not normally encrypted,
1285 * supplicant is allowed to encrypt it.
1287 if (key_info
& WPA_KEY_INFO_ERROR
) {
1288 if (wpa_receive_error_report(
1290 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) > 0)
1291 return; /* STA entry was removed */
1292 } else if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
1293 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1294 "received EAPOL-Key Request for new "
1296 wpa_request_new_ptk(sm
);
1297 } else if (key_data_length
> 0 &&
1298 wpa_parse_kde_ies(key_data
, key_data_length
,
1302 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1303 "received EAPOL-Key Request for GTK "
1305 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
1306 wpa_rekey_gtk(wpa_auth
, NULL
);
1309 /* Do not allow the same key replay counter to be reused. */
1310 wpa_replay_counter_mark_invalid(sm
->key_replay
,
1311 key
->replay_counter
);
1313 if (msg
== PAIRWISE_2
) {
1315 * Maintain a copy of the pending EAPOL-Key frames in
1316 * case the EAPOL-Key frame was retransmitted. This is
1317 * needed to allow EAPOL-Key msg 2/4 reply to another
1318 * pending msg 1/4 to update the SNonce to work around
1319 * unexpected supplicant behavior.
1321 os_memcpy(sm
->prev_key_replay
, sm
->key_replay
,
1322 sizeof(sm
->key_replay
));
1324 os_memset(sm
->prev_key_replay
, 0,
1325 sizeof(sm
->prev_key_replay
));
1329 * Make sure old valid counters are not accepted anymore and
1330 * do not get copied again.
1332 wpa_replay_counter_mark_invalid(sm
->key_replay
, NULL
);
1335 os_free(sm
->last_rx_eapol_key
);
1336 sm
->last_rx_eapol_key
= os_memdup(data
, data_len
);
1337 if (sm
->last_rx_eapol_key
== NULL
)
1339 sm
->last_rx_eapol_key_len
= data_len
;
1341 sm
->rx_eapol_key_secure
= !!(key_info
& WPA_KEY_INFO_SECURE
);
1342 sm
->EAPOLKeyReceived
= TRUE
;
1343 sm
->EAPOLKeyPairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1344 sm
->EAPOLKeyRequest
= !!(key_info
& WPA_KEY_INFO_REQUEST
);
1345 os_memcpy(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
);
1350 static int wpa_gmk_to_gtk(const u8
*gmk
, const char *label
, const u8
*addr
,
1351 const u8
*gnonce
, u8
*gtk
, size_t gtk_len
)
1353 u8 data
[ETH_ALEN
+ WPA_NONCE_LEN
+ 8 + WPA_GTK_MAX_LEN
];
1357 /* GTK = PRF-X(GMK, "Group key expansion",
1358 * AA || GNonce || Time || random data)
1359 * The example described in the IEEE 802.11 standard uses only AA and
1360 * GNonce as inputs here. Add some more entropy since this derivation
1361 * is done only at the Authenticator and as such, does not need to be
1364 os_memset(data
, 0, sizeof(data
));
1365 os_memcpy(data
, addr
, ETH_ALEN
);
1366 os_memcpy(data
+ ETH_ALEN
, gnonce
, WPA_NONCE_LEN
);
1367 pos
= data
+ ETH_ALEN
+ WPA_NONCE_LEN
;
1368 wpa_get_ntp_timestamp(pos
);
1370 os_memset(pos
, 0xef, 8);
1371 #endif /* TEST_FUZZ */
1373 if (random_get_bytes(pos
, gtk_len
) < 0)
1376 #ifdef CONFIG_SHA384
1377 if (sha384_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1380 #else /* CONFIG_SHA384 */
1381 #ifdef CONFIG_SHA256
1382 if (sha256_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1385 #else /* CONFIG_SHA256 */
1386 if (sha1_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
),
1389 #endif /* CONFIG_SHA256 */
1390 #endif /* CONFIG_SHA384 */
1396 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
)
1398 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1399 struct wpa_state_machine
*sm
= timeout_ctx
;
1401 sm
->pending_1_of_4_timeout
= 0;
1402 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "EAPOL-Key timeout");
1403 sm
->TimeoutEvt
= TRUE
;
1408 void __wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1409 struct wpa_state_machine
*sm
, int key_info
,
1410 const u8
*key_rsc
, const u8
*nonce
,
1411 const u8
*kde
, size_t kde_len
,
1412 int keyidx
, int encr
, int force_version
)
1414 struct ieee802_1x_hdr
*hdr
;
1415 struct wpa_eapol_key
*key
;
1416 size_t len
, mic_len
, keyhdrlen
;
1418 int key_data_len
, pad_len
= 0;
1420 int version
, pairwise
;
1422 u8
*key_mic
, *key_data
;
1424 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
1425 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
1427 len
= sizeof(struct ieee802_1x_hdr
) + keyhdrlen
;
1430 version
= force_version
;
1431 else if (wpa_use_akm_defined(sm
->wpa_key_mgmt
))
1432 version
= WPA_KEY_INFO_TYPE_AKM_DEFINED
;
1433 else if (wpa_use_cmac(sm
->wpa_key_mgmt
))
1434 version
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
1435 else if (sm
->pairwise
!= WPA_CIPHER_TKIP
)
1436 version
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
1438 version
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
1440 pairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1442 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1443 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1446 (key_info
& WPA_KEY_INFO_SECURE
) ? 1 : 0,
1447 (key_info
& WPA_KEY_INFO_MIC
) ? 1 : 0,
1448 (key_info
& WPA_KEY_INFO_ACK
) ? 1 : 0,
1449 (key_info
& WPA_KEY_INFO_INSTALL
) ? 1 : 0,
1450 pairwise
, (unsigned long) kde_len
, keyidx
, encr
);
1452 key_data_len
= kde_len
;
1454 if ((version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1455 wpa_use_aes_key_wrap(sm
->wpa_key_mgmt
) ||
1456 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) && encr
) {
1457 pad_len
= key_data_len
% 8;
1459 pad_len
= 8 - pad_len
;
1460 key_data_len
+= pad_len
+ 8;
1463 len
+= key_data_len
;
1464 if (!mic_len
&& encr
)
1465 len
+= AES_BLOCK_SIZE
;
1467 hdr
= os_zalloc(len
);
1470 hdr
->version
= wpa_auth
->conf
.eapol_version
;
1471 hdr
->type
= IEEE802_1X_TYPE_EAPOL_KEY
;
1472 hdr
->length
= host_to_be16(len
- sizeof(*hdr
));
1473 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1474 key_mic
= (u8
*) (key
+ 1);
1475 key_data
= ((u8
*) (hdr
+ 1)) + keyhdrlen
;
1477 key
->type
= sm
->wpa
== WPA_VERSION_WPA2
?
1478 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1479 key_info
|= version
;
1480 if (encr
&& sm
->wpa
== WPA_VERSION_WPA2
)
1481 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1482 if (sm
->wpa
!= WPA_VERSION_WPA2
)
1483 key_info
|= keyidx
<< WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1484 WPA_PUT_BE16(key
->key_info
, key_info
);
1486 alg
= pairwise
? sm
->pairwise
: wpa_auth
->conf
.wpa_group
;
1487 if (sm
->wpa
== WPA_VERSION_WPA2
&& !pairwise
)
1488 WPA_PUT_BE16(key
->key_length
, 0);
1490 WPA_PUT_BE16(key
->key_length
, wpa_cipher_key_len(alg
));
1492 for (i
= RSNA_MAX_EAPOL_RETRIES
- 1; i
> 0; i
--) {
1493 sm
->key_replay
[i
].valid
= sm
->key_replay
[i
- 1].valid
;
1494 os_memcpy(sm
->key_replay
[i
].counter
,
1495 sm
->key_replay
[i
- 1].counter
,
1496 WPA_REPLAY_COUNTER_LEN
);
1498 inc_byte_array(sm
->key_replay
[0].counter
, WPA_REPLAY_COUNTER_LEN
);
1499 os_memcpy(key
->replay_counter
, sm
->key_replay
[0].counter
,
1500 WPA_REPLAY_COUNTER_LEN
);
1501 wpa_hexdump(MSG_DEBUG
, "WPA: Replay Counter",
1502 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1503 sm
->key_replay
[0].valid
= TRUE
;
1506 os_memcpy(key
->key_nonce
, nonce
, WPA_NONCE_LEN
);
1509 os_memcpy(key
->key_rsc
, key_rsc
, WPA_KEY_RSC_LEN
);
1512 os_memcpy(key_data
, kde
, kde_len
);
1513 WPA_PUT_BE16(key_mic
+ mic_len
, kde_len
);
1515 } else if (!mic_len
&& kde
) {
1519 WPA_PUT_BE16(key_mic
, AES_BLOCK_SIZE
+ kde_len
);
1520 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1523 wpa_hexdump_key(MSG_DEBUG
, "WPA: KEK",
1524 sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1525 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1526 * to Key Data (exclusive). */
1527 aad
[0] = (u8
*) hdr
;
1528 aad_len
[0] = key_mic
+ 2 - (u8
*) hdr
;
1529 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, kde
, kde_len
,
1530 1, aad
, aad_len
, key_mic
+ 2) < 0) {
1531 wpa_printf(MSG_DEBUG
, "WPA: AES-SIV encryption failed");
1535 wpa_hexdump(MSG_DEBUG
, "WPA: Encrypted Key Data from SIV",
1536 key_mic
+ 2, AES_BLOCK_SIZE
+ kde_len
);
1537 #endif /* CONFIG_FILS */
1538 } else if (encr
&& kde
) {
1539 buf
= os_zalloc(key_data_len
);
1545 os_memcpy(pos
, kde
, kde_len
);
1551 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1553 if (version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1554 wpa_use_aes_key_wrap(sm
->wpa_key_mgmt
) ||
1555 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1556 wpa_printf(MSG_DEBUG
,
1557 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1558 (unsigned int) sm
->PTK
.kek_len
);
1559 if (aes_wrap(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
1560 (key_data_len
- 8) / 8, buf
, key_data
)) {
1565 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1566 #ifndef CONFIG_NO_RC4
1567 } else if (sm
->PTK
.kek_len
== 16) {
1570 wpa_printf(MSG_DEBUG
,
1571 "WPA: Encrypt Key Data using RC4");
1572 os_memcpy(key
->key_iv
,
1573 sm
->group
->Counter
+ WPA_NONCE_LEN
- 16, 16);
1574 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1575 os_memcpy(ek
, key
->key_iv
, 16);
1576 os_memcpy(ek
+ 16, sm
->PTK
.kek
, sm
->PTK
.kek_len
);
1577 os_memcpy(key_data
, buf
, key_data_len
);
1578 rc4_skip(ek
, 32, 256, key_data
, key_data_len
);
1579 WPA_PUT_BE16(key_mic
+ mic_len
, key_data_len
);
1580 #endif /* CONFIG_NO_RC4 */
1589 if (key_info
& WPA_KEY_INFO_MIC
) {
1590 if (!sm
->PTK_valid
|| !mic_len
) {
1591 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1592 "PTK not valid when sending EAPOL-Key "
1598 if (wpa_eapol_key_mic(sm
->PTK
.kck
, sm
->PTK
.kck_len
,
1599 sm
->wpa_key_mgmt
, version
,
1600 (u8
*) hdr
, len
, key_mic
) < 0) {
1604 #ifdef CONFIG_TESTING_OPTIONS
1606 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
> 0.0 &&
1608 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
) {
1609 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1610 "Corrupting group EAPOL-Key Key MIC");
1613 #endif /* CONFIG_TESTING_OPTIONS */
1616 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_inc_EapolFramesTx
,
1618 wpa_auth_send_eapol(wpa_auth
, sm
->addr
, (u8
*) hdr
, len
,
1624 static void wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1625 struct wpa_state_machine
*sm
, int key_info
,
1626 const u8
*key_rsc
, const u8
*nonce
,
1627 const u8
*kde
, size_t kde_len
,
1628 int keyidx
, int encr
)
1631 int pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
1637 __wpa_send_eapol(wpa_auth
, sm
, key_info
, key_rsc
, nonce
, kde
, kde_len
,
1640 ctr
= pairwise
? sm
->TimeoutCtr
: sm
->GTimeoutCtr
;
1641 if (ctr
== 1 && wpa_auth
->conf
.tx_status
)
1642 timeout_ms
= pairwise
? eapol_key_timeout_first
:
1643 eapol_key_timeout_first_group
;
1645 timeout_ms
= eapol_key_timeout_subseq
;
1646 if (wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
1647 (!pairwise
|| (key_info
& WPA_KEY_INFO_MIC
)))
1648 timeout_ms
= eapol_key_timeout_no_retrans
;
1649 if (pairwise
&& ctr
== 1 && !(key_info
& WPA_KEY_INFO_MIC
))
1650 sm
->pending_1_of_4_timeout
= 1;
1653 #endif /* TEST_FUZZ */
1654 wpa_printf(MSG_DEBUG
, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1655 "counter %u)", timeout_ms
, ctr
);
1656 eloop_register_timeout(timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000,
1657 wpa_send_eapol_timeout
, wpa_auth
, sm
);
1661 static int wpa_verify_key_mic(int akmp
, size_t pmk_len
, struct wpa_ptk
*PTK
,
1662 u8
*data
, size_t data_len
)
1664 struct ieee802_1x_hdr
*hdr
;
1665 struct wpa_eapol_key
*key
;
1668 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
], *mic_pos
;
1669 size_t mic_len
= wpa_mic_len(akmp
, pmk_len
);
1671 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
1674 hdr
= (struct ieee802_1x_hdr
*) data
;
1675 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1676 mic_pos
= (u8
*) (key
+ 1);
1677 key_info
= WPA_GET_BE16(key
->key_info
);
1678 os_memcpy(mic
, mic_pos
, mic_len
);
1679 os_memset(mic_pos
, 0, mic_len
);
1680 if (wpa_eapol_key_mic(PTK
->kck
, PTK
->kck_len
, akmp
,
1681 key_info
& WPA_KEY_INFO_TYPE_MASK
,
1682 data
, data_len
, mic_pos
) ||
1683 os_memcmp_const(mic
, mic_pos
, mic_len
) != 0)
1685 os_memcpy(mic_pos
, mic
, mic_len
);
1690 void wpa_remove_ptk(struct wpa_state_machine
*sm
)
1692 sm
->PTK_valid
= FALSE
;
1693 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1694 if (wpa_auth_set_key(sm
->wpa_auth
, 0, WPA_ALG_NONE
, sm
->addr
, 0, NULL
,
1696 wpa_printf(MSG_DEBUG
,
1697 "RSN: PTK removal from the driver failed");
1698 sm
->pairwise_set
= FALSE
;
1699 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1703 int wpa_auth_sm_event(struct wpa_state_machine
*sm
, enum wpa_event event
)
1710 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1711 "event %d notification", event
);
1716 /* PTKs are derived through AMPE */
1717 if (wpa_auth_start_ampe(sm
->wpa_auth
, sm
->addr
)) {
1722 #endif /* CONFIG_MESH */
1727 sm
->DeauthenticationRequest
= TRUE
;
1730 case WPA_REAUTH_EAPOL
:
1733 * When using WPS, we may end up here if the STA
1734 * manages to re-associate without the previous STA
1735 * entry getting removed. Consequently, we need to make
1736 * sure that the WPA state machines gets initialized
1737 * properly at this point.
1739 wpa_printf(MSG_DEBUG
, "WPA state machine had not been "
1740 "started - initialize now");
1743 if (wpa_sm_step(sm
) == 1)
1744 return 1; /* should not really happen */
1746 sm
->AuthenticationRequest
= TRUE
;
1749 if (sm
->GUpdateStationKeys
) {
1751 * Reauthentication cancels the pending group key
1752 * update for this STA.
1754 sm
->group
->GKeyDoneStations
--;
1755 sm
->GUpdateStationKeys
= FALSE
;
1756 sm
->PtkGroupInit
= TRUE
;
1758 sm
->ReAuthenticationRequest
= TRUE
;
1761 #ifdef CONFIG_IEEE80211R_AP
1762 wpa_printf(MSG_DEBUG
, "FT: Retry PTK configuration "
1763 "after association");
1764 wpa_ft_install_ptk(sm
);
1766 /* Using FT protocol, not WPA auth state machine */
1767 sm
->ft_completed
= 1;
1769 #else /* CONFIG_IEEE80211R_AP */
1771 #endif /* CONFIG_IEEE80211R_AP */
1772 case WPA_ASSOC_FILS
:
1774 wpa_printf(MSG_DEBUG
,
1775 "FILS: TK configuration after association");
1777 sm
->fils_completed
= 1;
1779 #else /* CONFIG_FILS */
1781 #endif /* CONFIG_FILS */
1782 case WPA_DRV_STA_REMOVED
:
1783 sm
->tk_already_set
= FALSE
;
1787 #ifdef CONFIG_IEEE80211R_AP
1788 sm
->ft_completed
= 0;
1789 #endif /* CONFIG_IEEE80211R_AP */
1791 #ifdef CONFIG_IEEE80211W
1792 if (sm
->mgmt_frame_prot
&& event
== WPA_AUTH
)
1794 #endif /* CONFIG_IEEE80211W */
1796 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
) &&
1797 (event
== WPA_AUTH
|| event
== WPA_ASSOC
))
1799 #endif /* CONFIG_FILS */
1802 sm
->PTK_valid
= FALSE
;
1803 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1805 if (event
!= WPA_REAUTH_EAPOL
)
1809 if (sm
->in_step_loop
) {
1811 * wpa_sm_step() is already running - avoid recursive call to
1812 * it by making the existing loop process the new update.
1817 return wpa_sm_step(sm
);
1821 SM_STATE(WPA_PTK
, INITIALIZE
)
1823 SM_ENTRY_MA(WPA_PTK
, INITIALIZE
, wpa_ptk
);
1825 /* Init flag is not cleared here, so avoid busy
1826 * loop by claiming nothing changed. */
1827 sm
->changed
= FALSE
;
1831 if (sm
->GUpdateStationKeys
)
1832 sm
->group
->GKeyDoneStations
--;
1833 sm
->GUpdateStationKeys
= FALSE
;
1834 if (sm
->wpa
== WPA_VERSION_WPA
)
1835 sm
->PInitAKeys
= FALSE
;
1836 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1837 * Local AA > Remote AA)) */) {
1840 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 0);
1842 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
, 0);
1844 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
1845 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
1846 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
1847 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1848 WPA_EAPOL_authorized
, 0);
1853 SM_STATE(WPA_PTK
, DISCONNECT
)
1855 u16 reason
= sm
->disconnect_reason
;
1857 SM_ENTRY_MA(WPA_PTK
, DISCONNECT
, wpa_ptk
);
1858 sm
->Disconnect
= FALSE
;
1859 sm
->disconnect_reason
= 0;
1861 reason
= WLAN_REASON_PREV_AUTH_NOT_VALID
;
1862 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
, reason
);
1866 SM_STATE(WPA_PTK
, DISCONNECTED
)
1868 SM_ENTRY_MA(WPA_PTK
, DISCONNECTED
, wpa_ptk
);
1869 sm
->DeauthenticationRequest
= FALSE
;
1873 SM_STATE(WPA_PTK
, AUTHENTICATION
)
1875 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION
, wpa_ptk
);
1876 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1877 sm
->PTK_valid
= FALSE
;
1878 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portControl_Auto
,
1880 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 1);
1881 sm
->AuthenticationRequest
= FALSE
;
1885 static void wpa_group_ensure_init(struct wpa_authenticator
*wpa_auth
,
1886 struct wpa_group
*group
)
1888 if (group
->first_sta_seen
)
1891 * System has run bit further than at the time hostapd was started
1892 * potentially very early during boot up. This provides better chances
1893 * of collecting more randomness on embedded systems. Re-initialize the
1894 * GMK and Counter here to improve their strength if there was not
1895 * enough entropy available immediately after system startup.
1897 wpa_printf(MSG_DEBUG
, "WPA: Re-initialize GMK/Counter on first "
1899 if (random_pool_ready() != 1) {
1900 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
1901 "to proceed - reject first 4-way handshake");
1902 group
->reject_4way_hs_for_entropy
= TRUE
;
1904 group
->first_sta_seen
= TRUE
;
1905 group
->reject_4way_hs_for_entropy
= FALSE
;
1908 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0 ||
1909 wpa_gtk_update(wpa_auth
, group
) < 0 ||
1910 wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
1911 wpa_printf(MSG_INFO
, "WPA: GMK/GTK setup failed");
1912 group
->first_sta_seen
= FALSE
;
1913 group
->reject_4way_hs_for_entropy
= TRUE
;
1918 SM_STATE(WPA_PTK
, AUTHENTICATION2
)
1920 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION2
, wpa_ptk
);
1922 wpa_group_ensure_init(sm
->wpa_auth
, sm
->group
);
1923 sm
->ReAuthenticationRequest
= FALSE
;
1926 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1927 * ambiguous. The Authenticator state machine uses a counter that is
1928 * incremented by one for each 4-way handshake. However, the security
1929 * analysis of 4-way handshake points out that unpredictable nonces
1930 * help in preventing precomputation attacks. Instead of the state
1931 * machine definition, use an unpredictable nonce value here to provide
1932 * stronger protection against potential precomputation attacks.
1934 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
1935 wpa_printf(MSG_ERROR
, "WPA: Failed to get random data for "
1937 sm
->Disconnect
= TRUE
;
1940 wpa_hexdump(MSG_DEBUG
, "WPA: Assign ANonce", sm
->ANonce
,
1942 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1943 * logical place than INITIALIZE since AUTHENTICATION2 can be
1944 * re-entered on ReAuthenticationRequest without going through
1950 static int wpa_auth_sm_ptk_update(struct wpa_state_machine
*sm
)
1952 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
1953 wpa_printf(MSG_ERROR
,
1954 "WPA: Failed to get random data for ANonce");
1955 sm
->Disconnect
= TRUE
;
1958 wpa_hexdump(MSG_DEBUG
, "WPA: Assign new ANonce", sm
->ANonce
,
1965 SM_STATE(WPA_PTK
, INITPMK
)
1967 u8 msk
[2 * PMK_LEN
];
1968 size_t len
= 2 * PMK_LEN
;
1970 SM_ENTRY_MA(WPA_PTK
, INITPMK
, wpa_ptk
);
1971 #ifdef CONFIG_IEEE80211R_AP
1973 #endif /* CONFIG_IEEE80211R_AP */
1975 wpa_printf(MSG_DEBUG
, "WPA: PMK from PMKSA cache");
1976 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
1977 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
1979 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
) {
1980 wpa_printf(MSG_DEBUG
,
1981 "DPP: No PMKSA cache entry for STA - reject connection");
1982 sm
->Disconnect
= TRUE
;
1983 sm
->disconnect_reason
= WLAN_REASON_INVALID_PMKID
;
1985 #endif /* CONFIG_DPP */
1986 } else if (wpa_auth_get_msk(sm
->wpa_auth
, sm
->addr
, msk
, &len
) == 0) {
1987 unsigned int pmk_len
;
1989 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
))
1990 pmk_len
= PMK_LEN_SUITE_B_192
;
1993 wpa_printf(MSG_DEBUG
, "WPA: PMK from EAPOL state machine "
1994 "(MSK len=%lu PMK len=%u)", (unsigned long) len
,
1996 if (len
< pmk_len
) {
1997 wpa_printf(MSG_DEBUG
,
1998 "WPA: MSK not long enough (%u) to create PMK (%u)",
1999 (unsigned int) len
, (unsigned int) pmk_len
);
2000 sm
->Disconnect
= TRUE
;
2003 os_memcpy(sm
->PMK
, msk
, pmk_len
);
2004 sm
->pmk_len
= pmk_len
;
2005 #ifdef CONFIG_IEEE80211R_AP
2006 if (len
>= 2 * PMK_LEN
) {
2007 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
2008 os_memcpy(sm
->xxkey
, msk
, SHA384_MAC_LEN
);
2009 sm
->xxkey_len
= SHA384_MAC_LEN
;
2011 os_memcpy(sm
->xxkey
, msk
+ PMK_LEN
, PMK_LEN
);
2012 sm
->xxkey_len
= PMK_LEN
;
2015 #endif /* CONFIG_IEEE80211R_AP */
2017 wpa_printf(MSG_DEBUG
, "WPA: Could not get PMK, get_msk: %p",
2018 sm
->wpa_auth
->cb
->get_msk
);
2019 sm
->Disconnect
= TRUE
;
2022 os_memset(msk
, 0, sizeof(msk
));
2024 sm
->req_replay_counter_used
= 0;
2025 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
2026 * will break reauthentication since EAPOL state machines may not be
2027 * get into AUTHENTICATING state that clears keyRun before WPA state
2028 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2029 * state and takes PMK from the previously used AAA Key. This will
2030 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2031 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2032 * be good workaround for this issue. */
2033 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyRun
, 0);
2037 SM_STATE(WPA_PTK
, INITPSK
)
2042 SM_ENTRY_MA(WPA_PTK
, INITPSK
, wpa_ptk
);
2043 psk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
, NULL
,
2046 os_memcpy(sm
->PMK
, psk
, psk_len
);
2047 sm
->pmk_len
= psk_len
;
2048 #ifdef CONFIG_IEEE80211R_AP
2049 os_memcpy(sm
->xxkey
, psk
, PMK_LEN
);
2050 sm
->xxkey_len
= PMK_LEN
;
2051 #endif /* CONFIG_IEEE80211R_AP */
2054 if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
2055 wpa_printf(MSG_DEBUG
, "SAE: PMK from PMKSA cache");
2056 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, sm
->pmksa
->pmk_len
);
2057 sm
->pmk_len
= sm
->pmksa
->pmk_len
;
2059 #endif /* CONFIG_SAE */
2060 sm
->req_replay_counter_used
= 0;
2064 SM_STATE(WPA_PTK
, PTKSTART
)
2066 u8 buf
[2 + RSN_SELECTOR_LEN
+ PMKID_LEN
], *pmkid
= NULL
;
2067 size_t pmkid_len
= 0;
2069 SM_ENTRY_MA(WPA_PTK
, PTKSTART
, wpa_ptk
);
2070 sm
->PTKRequest
= FALSE
;
2071 sm
->TimeoutEvt
= FALSE
;
2072 sm
->alt_snonce_valid
= FALSE
;
2075 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
2076 /* No point in sending the EAPOL-Key - we will disconnect
2077 * immediately following this. */
2081 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2082 "sending 1/4 msg of 4-Way Handshake");
2084 * For infrastructure BSS cases, it is better for the AP not to include
2085 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2086 * offline search for the passphrase/PSK without having to be able to
2087 * capture a 4-way handshake from a STA that has access to the network.
2089 * For IBSS cases, addition of PMKID KDE could be considered even with
2090 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2091 * possible PSK for this STA. However, this should not be done unless
2092 * there is support for using that information on the supplicant side.
2093 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2094 * cases would also apply here, but at least in the IBSS case, this
2095 * would cover a potential real use case.
2097 if (sm
->wpa
== WPA_VERSION_WPA2
&&
2098 (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) ||
2099 (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
&& sm
->pmksa
) ||
2100 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) &&
2101 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
) {
2103 pmkid_len
= 2 + RSN_SELECTOR_LEN
+ PMKID_LEN
;
2104 pmkid
[0] = WLAN_EID_VENDOR_SPECIFIC
;
2105 pmkid
[1] = RSN_SELECTOR_LEN
+ PMKID_LEN
;
2106 RSN_SELECTOR_PUT(&pmkid
[2], RSN_KEY_DATA_PMKID
);
2108 wpa_hexdump(MSG_DEBUG
,
2109 "RSN: Message 1/4 PMKID from PMKSA entry",
2110 sm
->pmksa
->pmkid
, PMKID_LEN
);
2111 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2112 sm
->pmksa
->pmkid
, PMKID_LEN
);
2113 } else if (wpa_key_mgmt_suite_b(sm
->wpa_key_mgmt
)) {
2114 /* No KCK available to derive PMKID */
2115 wpa_printf(MSG_DEBUG
,
2116 "RSN: No KCK available to derive PMKID for message 1/4");
2119 } else if (wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
2120 if (sm
->pmkid_set
) {
2121 wpa_hexdump(MSG_DEBUG
,
2122 "RSN: Message 1/4 PMKID from SAE",
2123 sm
->pmkid
, PMKID_LEN
);
2124 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
2125 sm
->pmkid
, PMKID_LEN
);
2127 /* No PMKID available */
2128 wpa_printf(MSG_DEBUG
,
2129 "RSN: No SAE PMKID available for message 1/4");
2132 #endif /* CONFIG_SAE */
2135 * Calculate PMKID since no PMKSA cache entry was
2136 * available with pre-calculated PMKID.
2138 rsn_pmkid(sm
->PMK
, sm
->pmk_len
, sm
->wpa_auth
->addr
,
2139 sm
->addr
, &pmkid
[2 + RSN_SELECTOR_LEN
],
2141 wpa_hexdump(MSG_DEBUG
,
2142 "RSN: Message 1/4 PMKID derived from PMK",
2143 &pmkid
[2 + RSN_SELECTOR_LEN
], PMKID_LEN
);
2146 wpa_send_eapol(sm
->wpa_auth
, sm
,
2147 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
2148 sm
->ANonce
, pmkid
, pmkid_len
, 0, 0);
2152 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*snonce
,
2153 const u8
*pmk
, unsigned int pmk_len
,
2154 struct wpa_ptk
*ptk
)
2156 #ifdef CONFIG_IEEE80211R_AP
2157 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
2158 return wpa_auth_derive_ptk_ft(sm
, pmk
, ptk
);
2159 #endif /* CONFIG_IEEE80211R_AP */
2161 return wpa_pmk_to_ptk(pmk
, pmk_len
, "Pairwise key expansion",
2162 sm
->wpa_auth
->addr
, sm
->addr
, sm
->ANonce
, snonce
,
2163 ptk
, sm
->wpa_key_mgmt
, sm
->pairwise
);
2169 int fils_auth_pmk_to_ptk(struct wpa_state_machine
*sm
, const u8
*pmk
,
2170 size_t pmk_len
, const u8
*snonce
, const u8
*anonce
,
2171 const u8
*dhss
, size_t dhss_len
,
2172 struct wpabuf
*g_sta
, struct wpabuf
*g_ap
)
2174 u8 ick
[FILS_ICK_MAX_LEN
];
2177 u8 fils_ft
[FILS_FT_MAX_LEN
];
2178 size_t fils_ft_len
= 0;
2180 res
= fils_pmk_to_ptk(pmk
, pmk_len
, sm
->addr
, sm
->wpa_auth
->addr
,
2181 snonce
, anonce
, dhss
, dhss_len
,
2182 &sm
->PTK
, ick
, &ick_len
,
2183 sm
->wpa_key_mgmt
, sm
->pairwise
,
2184 fils_ft
, &fils_ft_len
);
2187 sm
->PTK_valid
= TRUE
;
2188 sm
->tk_already_set
= FALSE
;
2190 #ifdef CONFIG_IEEE80211R_AP
2192 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2193 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
2194 u8 pmk_r0
[PMK_LEN_MAX
], pmk_r0_name
[WPA_PMK_NAME_LEN
];
2195 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
2196 size_t pmk_r0_len
= use_sha384
? SHA384_MAC_LEN
: PMK_LEN
;
2198 if (wpa_derive_pmk_r0(fils_ft
, fils_ft_len
,
2199 conf
->ssid
, conf
->ssid_len
,
2200 conf
->mobility_domain
,
2201 conf
->r0_key_holder
,
2202 conf
->r0_key_holder_len
,
2203 sm
->addr
, pmk_r0
, pmk_r0_name
,
2207 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: PMK-R0",
2208 pmk_r0
, pmk_r0_len
);
2209 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR0Name",
2210 pmk_r0_name
, WPA_PMK_NAME_LEN
);
2211 wpa_ft_store_pmk_fils(sm
, pmk_r0
, pmk_r0_name
);
2212 os_memset(fils_ft
, 0, sizeof(fils_ft
));
2214 #endif /* CONFIG_IEEE80211R_AP */
2216 res
= fils_key_auth_sk(ick
, ick_len
, snonce
, anonce
,
2217 sm
->addr
, sm
->wpa_auth
->addr
,
2218 g_sta
? wpabuf_head(g_sta
) : NULL
,
2219 g_sta
? wpabuf_len(g_sta
) : 0,
2220 g_ap
? wpabuf_head(g_ap
) : NULL
,
2221 g_ap
? wpabuf_len(g_ap
) : 0,
2222 sm
->wpa_key_mgmt
, sm
->fils_key_auth_sta
,
2223 sm
->fils_key_auth_ap
,
2224 &sm
->fils_key_auth_len
);
2225 os_memset(ick
, 0, sizeof(ick
));
2227 /* Store nonces for (Re)Association Request/Response frame processing */
2228 os_memcpy(sm
->SNonce
, snonce
, FILS_NONCE_LEN
);
2229 os_memcpy(sm
->ANonce
, anonce
, FILS_NONCE_LEN
);
2235 static int wpa_aead_decrypt(struct wpa_state_machine
*sm
, struct wpa_ptk
*ptk
,
2236 u8
*buf
, size_t buf_len
, u16
*_key_data_len
)
2238 struct ieee802_1x_hdr
*hdr
;
2239 struct wpa_eapol_key
*key
;
2246 hdr
= (struct ieee802_1x_hdr
*) buf
;
2247 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2248 pos
= (u8
*) (key
+ 1);
2249 key_data_len
= WPA_GET_BE16(pos
);
2250 if (key_data_len
< AES_BLOCK_SIZE
||
2251 key_data_len
> buf_len
- sizeof(*hdr
) - sizeof(*key
) - 2) {
2252 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2253 "No room for AES-SIV data in the frame");
2256 pos
+= 2; /* Pointing at the Encrypted Key Data field */
2258 tmp
= os_malloc(key_data_len
);
2262 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2263 * to Key Data (exclusive). */
2265 aad_len
[0] = pos
- buf
;
2266 if (aes_siv_decrypt(ptk
->kek
, ptk
->kek_len
, pos
, key_data_len
,
2267 1, aad
, aad_len
, tmp
) < 0) {
2268 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2269 "Invalid AES-SIV data in the frame");
2270 bin_clear_free(tmp
, key_data_len
);
2274 /* AEAD decryption and validation completed successfully */
2275 key_data_len
-= AES_BLOCK_SIZE
;
2276 wpa_hexdump_key(MSG_DEBUG
, "WPA: Decrypted Key Data",
2279 /* Replace Key Data field with the decrypted version */
2280 os_memcpy(pos
, tmp
, key_data_len
);
2281 pos
-= 2; /* Key Data Length field */
2282 WPA_PUT_BE16(pos
, key_data_len
);
2283 bin_clear_free(tmp
, key_data_len
);
2285 *_key_data_len
= key_data_len
;
2290 const u8
* wpa_fils_validate_fils_session(struct wpa_state_machine
*sm
,
2291 const u8
*ies
, size_t ies_len
,
2292 const u8
*fils_session
)
2295 const u8
*session
= NULL
;
2297 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2298 wpa_printf(MSG_DEBUG
,
2299 "FILS: Not a FILS AKM - reject association");
2303 /* Verify Session element */
2305 end
= ((const u8
*) ie
) + ies_len
;
2306 while (ie
+ 1 < end
) {
2307 if (ie
+ 2 + ie
[1] > end
)
2309 if (ie
[0] == WLAN_EID_EXTENSION
&&
2310 ie
[1] >= 1 + FILS_SESSION_LEN
&&
2311 ie
[2] == WLAN_EID_EXT_FILS_SESSION
) {
2319 wpa_printf(MSG_DEBUG
,
2320 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2325 if (!fils_session
) {
2326 wpa_printf(MSG_DEBUG
,
2327 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2332 if (os_memcmp(fils_session
, session
+ 3, FILS_SESSION_LEN
) != 0) {
2333 wpa_printf(MSG_DEBUG
, "FILS: Session mismatch");
2334 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
2335 fils_session
, FILS_SESSION_LEN
);
2336 wpa_hexdump(MSG_DEBUG
, "FILS: Received FILS Session",
2337 session
+ 3, FILS_SESSION_LEN
);
2344 int wpa_fils_validate_key_confirm(struct wpa_state_machine
*sm
, const u8
*ies
,
2347 struct ieee802_11_elems elems
;
2349 if (ieee802_11_parse_elems(ies
, ies_len
, &elems
, 1) == ParseFailed
) {
2350 wpa_printf(MSG_DEBUG
,
2351 "FILS: Failed to parse decrypted elements");
2355 if (!elems
.fils_session
) {
2356 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
2360 if (!elems
.fils_key_confirm
) {
2361 wpa_printf(MSG_DEBUG
, "FILS: No FILS Key Confirm element");
2365 if (elems
.fils_key_confirm_len
!= sm
->fils_key_auth_len
) {
2366 wpa_printf(MSG_DEBUG
,
2367 "FILS: Unexpected Key-Auth length %d (expected %d)",
2368 elems
.fils_key_confirm_len
,
2369 (int) sm
->fils_key_auth_len
);
2373 if (os_memcmp(elems
.fils_key_confirm
, sm
->fils_key_auth_sta
,
2374 sm
->fils_key_auth_len
) != 0) {
2375 wpa_printf(MSG_DEBUG
, "FILS: Key-Auth mismatch");
2376 wpa_hexdump(MSG_DEBUG
, "FILS: Received Key-Auth",
2377 elems
.fils_key_confirm
, elems
.fils_key_confirm_len
);
2378 wpa_hexdump(MSG_DEBUG
, "FILS: Expected Key-Auth",
2379 sm
->fils_key_auth_sta
, sm
->fils_key_auth_len
);
2387 int fils_decrypt_assoc(struct wpa_state_machine
*sm
, const u8
*fils_session
,
2388 const struct ieee80211_mgmt
*mgmt
, size_t frame_len
,
2389 u8
*pos
, size_t left
)
2392 const u8
*end
, *ie_start
, *ie
, *session
, *crypt
;
2396 if (!sm
|| !sm
->PTK_valid
) {
2397 wpa_printf(MSG_DEBUG
,
2398 "FILS: No KEK to decrypt Assocication Request frame");
2402 if (!wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2403 wpa_printf(MSG_DEBUG
,
2404 "FILS: Not a FILS AKM - reject association");
2408 end
= ((const u8
*) mgmt
) + frame_len
;
2409 fc
= le_to_host16(mgmt
->frame_control
);
2410 stype
= WLAN_FC_GET_STYPE(fc
);
2411 if (stype
== WLAN_FC_STYPE_REASSOC_REQ
)
2412 ie_start
= mgmt
->u
.reassoc_req
.variable
;
2414 ie_start
= mgmt
->u
.assoc_req
.variable
;
2418 * Find FILS Session element which is the last unencrypted element in
2421 session
= wpa_fils_validate_fils_session(sm
, ie
, end
- ie
,
2424 wpa_printf(MSG_DEBUG
, "FILS: Session validation failed");
2428 crypt
= session
+ 2 + session
[1];
2430 if (end
- crypt
< AES_BLOCK_SIZE
) {
2431 wpa_printf(MSG_DEBUG
,
2432 "FILS: Too short frame to include AES-SIV data");
2436 /* AES-SIV AAD vectors */
2438 /* The STA's MAC address */
2440 aad_len
[0] = ETH_ALEN
;
2441 /* The AP's BSSID */
2443 aad_len
[1] = ETH_ALEN
;
2444 /* The STA's nonce */
2445 aad
[2] = sm
->SNonce
;
2446 aad_len
[2] = FILS_NONCE_LEN
;
2447 /* The AP's nonce */
2448 aad
[3] = sm
->ANonce
;
2449 aad_len
[3] = FILS_NONCE_LEN
;
2451 * The (Re)Association Request frame from the Capability Information
2452 * field to the FILS Session element (both inclusive).
2454 aad
[4] = (const u8
*) &mgmt
->u
.assoc_req
.capab_info
;
2455 aad_len
[4] = crypt
- aad
[4];
2457 if (aes_siv_decrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
, crypt
, end
- crypt
,
2458 5, aad
, aad_len
, pos
+ (crypt
- ie_start
)) < 0) {
2459 wpa_printf(MSG_DEBUG
,
2460 "FILS: Invalid AES-SIV data in the frame");
2463 wpa_hexdump(MSG_DEBUG
, "FILS: Decrypted Association Request elements",
2464 pos
, left
- AES_BLOCK_SIZE
);
2466 if (wpa_fils_validate_key_confirm(sm
, pos
, left
- AES_BLOCK_SIZE
) < 0) {
2467 wpa_printf(MSG_DEBUG
, "FILS: Key Confirm validation failed");
2471 return left
- AES_BLOCK_SIZE
;
2475 int fils_encrypt_assoc(struct wpa_state_machine
*sm
, u8
*buf
,
2476 size_t current_len
, size_t max_len
,
2477 const struct wpabuf
*hlp
)
2479 u8
*end
= buf
+ max_len
;
2480 u8
*pos
= buf
+ current_len
;
2481 struct ieee80211_mgmt
*mgmt
;
2482 struct wpabuf
*plain
;
2486 if (!sm
|| !sm
->PTK_valid
)
2489 wpa_hexdump(MSG_DEBUG
,
2490 "FILS: Association Response frame before FILS processing",
2493 mgmt
= (struct ieee80211_mgmt
*) buf
;
2495 /* AES-SIV AAD vectors */
2497 /* The AP's BSSID */
2499 aad_len
[0] = ETH_ALEN
;
2500 /* The STA's MAC address */
2502 aad_len
[1] = ETH_ALEN
;
2503 /* The AP's nonce */
2504 aad
[2] = sm
->ANonce
;
2505 aad_len
[2] = FILS_NONCE_LEN
;
2506 /* The STA's nonce */
2507 aad
[3] = sm
->SNonce
;
2508 aad_len
[3] = FILS_NONCE_LEN
;
2510 * The (Re)Association Response frame from the Capability Information
2511 * field (the same offset in both Association and Reassociation
2512 * Response frames) to the FILS Session element (both inclusive).
2514 aad
[4] = (const u8
*) &mgmt
->u
.assoc_resp
.capab_info
;
2515 aad_len
[4] = pos
- aad
[4];
2517 /* The following elements will be encrypted with AES-SIV */
2518 plain
= fils_prepare_plainbuf(sm
, hlp
);
2520 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2524 if (pos
+ wpabuf_len(plain
) + AES_BLOCK_SIZE
> end
) {
2525 wpa_printf(MSG_DEBUG
,
2526 "FILS: Not enough room for FILS elements");
2531 wpa_hexdump_buf_key(MSG_DEBUG
, "FILS: Association Response plaintext",
2534 if (aes_siv_encrypt(sm
->PTK
.kek
, sm
->PTK
.kek_len
,
2535 wpabuf_head(plain
), wpabuf_len(plain
),
2536 5, aad
, aad_len
, pos
) < 0) {
2541 wpa_hexdump(MSG_DEBUG
,
2542 "FILS: Encrypted Association Response elements",
2543 pos
, AES_BLOCK_SIZE
+ wpabuf_len(plain
));
2544 current_len
+= wpabuf_len(plain
) + AES_BLOCK_SIZE
;
2547 sm
->fils_completed
= 1;
2553 static struct wpabuf
* fils_prepare_plainbuf(struct wpa_state_machine
*sm
,
2554 const struct wpabuf
*hlp
)
2556 struct wpabuf
*plain
;
2557 u8
*len
, *tmp
, *tmp2
;
2559 u8
*gtk
, dummy_gtk
[32];
2561 struct wpa_group
*gsm
;
2563 plain
= wpabuf_alloc(1000);
2567 /* TODO: FILS Public Key */
2569 /* FILS Key Confirmation */
2570 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2571 wpabuf_put_u8(plain
, 1 + sm
->fils_key_auth_len
); /* Length */
2572 /* Element ID Extension */
2573 wpabuf_put_u8(plain
, WLAN_EID_EXT_FILS_KEY_CONFIRM
);
2574 wpabuf_put_data(plain
, sm
->fils_key_auth_ap
, sm
->fils_key_auth_len
);
2576 /* FILS HLP Container */
2578 wpabuf_put_buf(plain
, hlp
);
2580 /* TODO: FILS IP Address Assignment */
2584 wpabuf_put_u8(plain
, WLAN_EID_EXTENSION
); /* Element ID */
2585 len
= wpabuf_put(plain
, 1);
2586 wpabuf_put_u8(plain
, WLAN_EID_EXT_KEY_DELIVERY
);
2587 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
,
2588 wpabuf_put(plain
, WPA_KEY_RSC_LEN
));
2590 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2591 gtk_len
= gsm
->GTK_len
;
2592 if (sm
->wpa_auth
->conf
.disable_gtk
||
2593 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
2595 * Provide unique random GTK to each STA to prevent use
2596 * of GTK in the BSS.
2598 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0) {
2604 hdr
[0] = gsm
->GN
& 0x03;
2606 tmp
= wpabuf_put(plain
, 0);
2607 tmp2
= wpa_add_kde(tmp
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
2609 wpabuf_put(plain
, tmp2
- tmp
);
2612 tmp
= wpabuf_put(plain
, 0);
2613 tmp2
= ieee80211w_kde_add(sm
, tmp
);
2614 wpabuf_put(plain
, tmp2
- tmp
);
2616 *len
= (u8
*) wpabuf_put(plain
, 0) - len
- 1;
2619 if (wpa_auth_uses_ocv(sm
)) {
2620 struct wpa_channel_info ci
;
2623 if (wpa_channel_info(sm
->wpa_auth
, &ci
) != 0) {
2624 wpa_printf(MSG_WARNING
,
2625 "FILS: Failed to get channel info for OCI element");
2630 pos
= wpabuf_put(plain
, OCV_OCI_EXTENDED_LEN
);
2631 if (ocv_insert_extended_oci(&ci
, pos
) < 0) {
2636 #endif /* CONFIG_OCV */
2642 int fils_set_tk(struct wpa_state_machine
*sm
)
2647 if (!sm
|| !sm
->PTK_valid
) {
2648 wpa_printf(MSG_DEBUG
, "FILS: No valid PTK available to set TK");
2651 if (sm
->tk_already_set
) {
2652 wpa_printf(MSG_DEBUG
, "FILS: TK already set to the driver");
2656 alg
= wpa_cipher_to_alg(sm
->pairwise
);
2657 klen
= wpa_cipher_key_len(sm
->pairwise
);
2659 wpa_printf(MSG_DEBUG
, "FILS: Configure TK to the driver");
2660 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
2661 sm
->PTK
.tk
, klen
)) {
2662 wpa_printf(MSG_DEBUG
, "FILS: Failed to set TK to the driver");
2665 sm
->tk_already_set
= TRUE
;
2671 u8
* hostapd_eid_assoc_fils_session(struct wpa_state_machine
*sm
, u8
*buf
,
2672 const u8
*fils_session
, struct wpabuf
*hlp
)
2674 struct wpabuf
*plain
;
2678 *pos
++ = WLAN_EID_EXTENSION
; /* Element ID */
2679 *pos
++ = 1 + FILS_SESSION_LEN
; /* Length */
2680 *pos
++ = WLAN_EID_EXT_FILS_SESSION
; /* Element ID Extension */
2681 os_memcpy(pos
, fils_session
, FILS_SESSION_LEN
);
2682 pos
+= FILS_SESSION_LEN
;
2684 plain
= fils_prepare_plainbuf(sm
, hlp
);
2686 wpa_printf(MSG_DEBUG
, "FILS: Plain buffer prep failed");
2690 os_memcpy(pos
, wpabuf_head(plain
), wpabuf_len(plain
));
2691 pos
+= wpabuf_len(plain
);
2693 wpa_printf(MSG_DEBUG
, "%s: plain buf_len: %u", __func__
,
2694 (unsigned int) wpabuf_len(plain
));
2696 sm
->fils_completed
= 1;
2700 #endif /* CONFIG_FILS */
2704 int get_sta_tx_parameters(struct wpa_state_machine
*sm
, int ap_max_chanwidth
,
2705 int ap_seg1_idx
, int *bandwidth
, int *seg1_idx
)
2707 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2709 if (!wpa_auth
->cb
->get_sta_tx_params
)
2711 return wpa_auth
->cb
->get_sta_tx_params(wpa_auth
->cb_ctx
, sm
->addr
,
2712 ap_max_chanwidth
, ap_seg1_idx
,
2713 bandwidth
, seg1_idx
);
2715 #endif /* CONFIG_OCV */
2718 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING
)
2720 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2722 int ok
= 0, psk_found
= 0;
2723 const u8
*pmk
= NULL
;
2726 const u8
*eapol_key_ie
, *key_data
, *mic
;
2727 u16 key_data_length
;
2728 size_t mic_len
, eapol_key_ie_len
;
2729 struct ieee802_1x_hdr
*hdr
;
2730 struct wpa_eapol_key
*key
;
2731 struct wpa_eapol_ie_parse kde
;
2734 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING
, wpa_ptk
);
2735 sm
->EAPOLKeyReceived
= FALSE
;
2736 sm
->update_snonce
= FALSE
;
2737 os_memset(&PTK
, 0, sizeof(PTK
));
2739 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
2741 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2742 * WPA-PSK: iterate through possible PSKs and select the one matching
2745 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
2746 !wpa_key_mgmt_sae(sm
->wpa_key_mgmt
)) {
2747 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
2748 sm
->p2p_dev_addr
, pmk
, &pmk_len
,
2753 #ifdef CONFIG_IEEE80211R_AP
2754 if (wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
2755 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
2756 sm
->xxkey_len
= pmk_len
;
2758 #endif /* CONFIG_IEEE80211R_AP */
2761 pmk_len
= sm
->pmk_len
;
2764 if (wpa_derive_ptk(sm
, sm
->SNonce
, pmk
, pmk_len
, &PTK
) < 0)
2768 wpa_verify_key_mic(sm
->wpa_key_mgmt
, pmk_len
, &PTK
,
2769 sm
->last_rx_eapol_key
,
2770 sm
->last_rx_eapol_key_len
) == 0) {
2771 os_memcpy(sm
->PMK
, pmk
, pmk_len
);
2772 sm
->pmk_len
= pmk_len
;
2779 wpa_aead_decrypt(sm
, &PTK
, sm
->last_rx_eapol_key
,
2780 sm
->last_rx_eapol_key_len
, NULL
) == 0) {
2784 #endif /* CONFIG_FILS */
2786 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
2787 wpa_key_mgmt_sae(sm
->wpa_key_mgmt
))
2792 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2793 "invalid MIC in msg 2/4 of 4-Way Handshake");
2795 wpa_auth_psk_failure_report(sm
->wpa_auth
, sm
->addr
);
2800 * Note: last_rx_eapol_key length fields have already been validated in
2803 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
2804 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
2805 mic
= (u8
*) (key
+ 1);
2806 key_data
= mic
+ mic_len
+ 2;
2807 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
2808 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
2809 sizeof(*key
) - mic_len
- 2)
2812 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
2813 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2814 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2818 eapol_key_ie
= kde
.rsn_ie
;
2819 eapol_key_ie_len
= kde
.rsn_ie_len
;
2820 } else if (kde
.osen
) {
2821 eapol_key_ie
= kde
.osen
;
2822 eapol_key_ie_len
= kde
.osen_len
;
2824 eapol_key_ie
= kde
.wpa_ie
;
2825 eapol_key_ie_len
= kde
.wpa_ie_len
;
2827 ft
= sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
);
2828 if (sm
->wpa_ie
== NULL
||
2829 wpa_compare_rsn_ie(ft
, sm
->wpa_ie
, sm
->wpa_ie_len
,
2830 eapol_key_ie
, eapol_key_ie_len
)) {
2831 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2832 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2834 wpa_hexdump(MSG_DEBUG
, "WPA IE in AssocReq",
2835 sm
->wpa_ie
, sm
->wpa_ie_len
);
2837 wpa_hexdump(MSG_DEBUG
, "WPA IE in msg 2/4",
2838 eapol_key_ie
, eapol_key_ie_len
);
2839 /* MLME-DEAUTHENTICATE.request */
2840 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
2841 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2845 if (wpa_auth_uses_ocv(sm
)) {
2846 struct wpa_channel_info ci
;
2850 if (wpa_channel_info(wpa_auth
, &ci
) != 0) {
2851 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2852 "Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
2856 if (get_sta_tx_parameters(sm
,
2857 channel_width_to_int(ci
.chanwidth
),
2858 ci
.seg1_idx
, &tx_chanwidth
,
2862 if (ocv_verify_tx_params(kde
.oci
, kde
.oci_len
, &ci
,
2863 tx_chanwidth
, tx_seg1_idx
) != 0) {
2864 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
2869 #endif /* CONFIG_OCV */
2870 #ifdef CONFIG_IEEE80211R_AP
2871 if (ft
&& ft_check_msg_2_of_4(wpa_auth
, sm
, &kde
) < 0) {
2872 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
2873 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2876 #endif /* CONFIG_IEEE80211R_AP */
2878 if (kde
.ip_addr_req
&& kde
.ip_addr_req
[0] &&
2879 wpa_auth
->ip_pool
&& WPA_GET_BE32(sm
->ip_addr
) == 0) {
2881 wpa_printf(MSG_DEBUG
,
2882 "P2P: IP address requested in EAPOL-Key exchange");
2883 idx
= bitfield_get_first_zero(wpa_auth
->ip_pool
);
2885 u32 start
= WPA_GET_BE32(wpa_auth
->conf
.ip_addr_start
);
2886 bitfield_set(wpa_auth
->ip_pool
, idx
);
2887 WPA_PUT_BE32(sm
->ip_addr
, start
+ idx
);
2888 wpa_printf(MSG_DEBUG
,
2889 "P2P: Assigned IP address %u.%u.%u.%u to "
2890 MACSTR
, sm
->ip_addr
[0], sm
->ip_addr
[1],
2891 sm
->ip_addr
[2], sm
->ip_addr
[3],
2895 #endif /* CONFIG_P2P */
2897 #ifdef CONFIG_IEEE80211R_AP
2898 if (sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2900 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2901 * with the value we derived.
2903 if (os_memcmp_const(sm
->sup_pmk_r1_name
, sm
->pmk_r1_name
,
2904 WPA_PMK_NAME_LEN
) != 0) {
2905 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2906 "PMKR1Name mismatch in FT 4-way "
2908 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from "
2910 sm
->sup_pmk_r1_name
, WPA_PMK_NAME_LEN
);
2911 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
2912 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
2916 #endif /* CONFIG_IEEE80211R_AP */
2918 if (vlan_id
&& wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) &&
2919 wpa_auth_update_vlan(wpa_auth
, sm
->addr
, vlan_id
) < 0) {
2920 wpa_sta_disconnect(wpa_auth
, sm
->addr
,
2921 WLAN_REASON_PREV_AUTH_NOT_VALID
);
2925 sm
->pending_1_of_4_timeout
= 0;
2926 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
2928 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
2929 /* PSK may have changed from the previous choice, so update
2930 * state machine data based on whatever PSK was selected here.
2932 os_memcpy(sm
->PMK
, pmk
, PMK_LEN
);
2933 sm
->pmk_len
= PMK_LEN
;
2936 sm
->MICVerified
= TRUE
;
2938 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
2939 sm
->PTK_valid
= TRUE
;
2943 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING2
)
2945 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING2
, wpa_ptk
);
2950 #ifdef CONFIG_IEEE80211W
2952 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
2954 if (sm
->mgmt_frame_prot
) {
2956 len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
2957 return 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
+ len
;
2964 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
2966 struct wpa_igtk_kde igtk
;
2967 struct wpa_group
*gsm
= sm
->group
;
2968 u8 rsc
[WPA_KEY_RSC_LEN
];
2969 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
2971 if (!sm
->mgmt_frame_prot
)
2974 igtk
.keyid
[0] = gsm
->GN_igtk
;
2976 if (gsm
->wpa_group_state
!= WPA_GROUP_SETKEYSDONE
||
2977 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, rsc
) < 0)
2978 os_memset(igtk
.pn
, 0, sizeof(igtk
.pn
));
2980 os_memcpy(igtk
.pn
, rsc
, sizeof(igtk
.pn
));
2981 os_memcpy(igtk
.igtk
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
2982 if (sm
->wpa_auth
->conf
.disable_gtk
||
2983 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
2985 * Provide unique random IGTK to each STA to prevent use of
2988 if (random_get_bytes(igtk
.igtk
, len
) < 0)
2991 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_IGTK
,
2992 (const u8
*) &igtk
, WPA_IGTK_KDE_PREFIX_LEN
+ len
,
2998 #else /* CONFIG_IEEE80211W */
3000 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
3006 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
3011 #endif /* CONFIG_IEEE80211W */
3014 static int ocv_oci_len(struct wpa_state_machine
*sm
)
3017 if (wpa_auth_uses_ocv(sm
))
3018 return OCV_OCI_KDE_LEN
;
3019 #endif /* CONFIG_OCV */
3023 static int ocv_oci_add(struct wpa_state_machine
*sm
, u8
**argpos
)
3026 struct wpa_channel_info ci
;
3028 if (!wpa_auth_uses_ocv(sm
))
3031 if (wpa_channel_info(sm
->wpa_auth
, &ci
) != 0) {
3032 wpa_printf(MSG_WARNING
,
3033 "Failed to get channel info for OCI element");
3037 return ocv_insert_oci_kde(&ci
, argpos
);
3038 #else /* CONFIG_OCV */
3040 #endif /* CONFIG_OCV */
3044 SM_STATE(WPA_PTK
, PTKINITNEGOTIATING
)
3046 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
, dummy_gtk
[32];
3047 size_t gtk_len
, kde_len
;
3048 struct wpa_group
*gsm
= sm
->group
;
3050 int wpa_ie_len
, secure
, keyidx
, encr
= 0;
3052 SM_ENTRY_MA(WPA_PTK
, PTKINITNEGOTIATING
, wpa_ptk
);
3053 sm
->TimeoutEvt
= FALSE
;
3056 if (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3057 sm
->TimeoutCtr
> 1) {
3058 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
3061 if (sm
->TimeoutCtr
> sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
3062 /* No point in sending the EAPOL-Key - we will disconnect
3063 * immediately following this. */
3067 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
3068 GTK[GN], IGTK, [FTIE], [TIE * 2])
3070 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3071 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3072 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
3073 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
3074 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
3075 if (sm
->wpa
== WPA_VERSION_WPA
&&
3076 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
3077 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
3078 /* WPA-only STA, remove RSN IE and possible MDIE */
3079 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
3080 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
3081 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
3082 wpa_ie_len
= wpa_ie
[1] + 2;
3084 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3085 "sending 3/4 msg of 4-Way Handshake");
3086 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3087 /* WPA2 send GTK in the 4-way handshake */
3089 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3090 gtk_len
= gsm
->GTK_len
;
3091 if (sm
->wpa_auth
->conf
.disable_gtk
||
3092 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3094 * Provide unique random GTK to each STA to prevent use
3095 * of GTK in the BSS.
3097 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0)
3105 /* WPA does not include GTK in msg 3/4 */
3111 if (sm
->rx_eapol_key_secure
) {
3113 * It looks like Windows 7 supplicant tries to use
3114 * Secure bit in msg 2/4 after having reported Michael
3115 * MIC failure and it then rejects the 4-way handshake
3116 * if msg 3/4 does not set Secure bit. Work around this
3117 * by setting the Secure bit here even in the case of
3118 * WPA if the supplicant used it first.
3120 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3121 "STA used Secure bit in WPA msg 2/4 - "
3122 "set Secure for 3/4 as workaround");
3127 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
3129 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
3130 #ifdef CONFIG_IEEE80211R_AP
3131 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3132 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
3133 kde_len
+= 300; /* FTIE + 2 * TIE */
3135 #endif /* CONFIG_IEEE80211R_AP */
3137 if (WPA_GET_BE32(sm
->ip_addr
) > 0)
3138 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 3 * 4;
3139 #endif /* CONFIG_P2P */
3140 kde
= os_malloc(kde_len
);
3145 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
3147 #ifdef CONFIG_IEEE80211R_AP
3148 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3153 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
3155 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
3156 "PMKR1Name into RSN IE in EAPOL-Key data");
3163 #endif /* CONFIG_IEEE80211R_AP */
3166 hdr
[0] = keyidx
& 0x03;
3168 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3171 pos
= ieee80211w_kde_add(sm
, pos
);
3172 if (ocv_oci_add(sm
, &pos
) < 0) {
3177 #ifdef CONFIG_IEEE80211R_AP
3178 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
3180 struct wpa_auth_config
*conf
;
3182 conf
= &sm
->wpa_auth
->conf
;
3183 if (sm
->assoc_resp_ftie
&&
3184 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
3185 os_memcpy(pos
, sm
->assoc_resp_ftie
,
3186 2 + sm
->assoc_resp_ftie
[1]);
3187 res
= 2 + sm
->assoc_resp_ftie
[1];
3189 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
3191 res
= wpa_write_ftie(conf
, use_sha384
,
3192 conf
->r0_key_holder
,
3193 conf
->r0_key_holder_len
,
3195 kde
+ kde_len
- pos
,
3199 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
3200 "into EAPOL-Key Key Data");
3206 /* TIE[ReassociationDeadline] (TU) */
3207 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3209 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
3210 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
3213 /* TIE[KeyLifetime] (seconds) */
3214 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
3216 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
3217 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
);
3220 #endif /* CONFIG_IEEE80211R_AP */
3222 if (WPA_GET_BE32(sm
->ip_addr
) > 0) {
3224 os_memcpy(addr
, sm
->ip_addr
, 4);
3225 os_memcpy(addr
+ 4, sm
->wpa_auth
->conf
.ip_addr_mask
, 4);
3226 os_memcpy(addr
+ 8, sm
->wpa_auth
->conf
.ip_addr_go
, 4);
3227 pos
= wpa_add_kde(pos
, WFA_KEY_DATA_IP_ADDR_ALLOC
,
3228 addr
, sizeof(addr
), NULL
, 0);
3230 #endif /* CONFIG_P2P */
3232 wpa_send_eapol(sm
->wpa_auth
, sm
,
3233 (secure
? WPA_KEY_INFO_SECURE
: 0) |
3234 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3235 WPA_KEY_INFO_MIC
: 0) |
3236 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
3237 WPA_KEY_INFO_KEY_TYPE
,
3238 _rsc
, sm
->ANonce
, kde
, pos
- kde
, keyidx
, encr
);
3243 SM_STATE(WPA_PTK
, PTKINITDONE
)
3245 SM_ENTRY_MA(WPA_PTK
, PTKINITDONE
, wpa_ptk
);
3246 sm
->EAPOLKeyReceived
= FALSE
;
3248 enum wpa_alg alg
= wpa_cipher_to_alg(sm
->pairwise
);
3249 int klen
= wpa_cipher_key_len(sm
->pairwise
);
3250 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
3251 sm
->PTK
.tk
, klen
)) {
3252 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
,
3253 WLAN_REASON_PREV_AUTH_NOT_VALID
);
3256 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3257 sm
->pairwise_set
= TRUE
;
3259 if (sm
->wpa_auth
->conf
.wpa_ptk_rekey
) {
3260 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
3261 eloop_register_timeout(sm
->wpa_auth
->conf
.
3262 wpa_ptk_rekey
, 0, wpa_rekey_ptk
,
3266 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3267 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
||
3268 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
) {
3269 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3270 WPA_EAPOL_authorized
, 1);
3274 if (0 /* IBSS == TRUE */) {
3276 if (sm
->keycount
== 2) {
3277 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
3278 WPA_EAPOL_portValid
, 1);
3281 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
,
3284 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyAvailable
, 0);
3285 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyDone
, 1);
3286 if (sm
->wpa
== WPA_VERSION_WPA
)
3287 sm
->PInitAKeys
= TRUE
;
3290 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3291 "pairwise key handshake completed (%s)",
3292 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3294 #ifdef CONFIG_IEEE80211R_AP
3295 wpa_ft_push_pmk_r1(sm
->wpa_auth
, sm
->addr
);
3296 #endif /* CONFIG_IEEE80211R_AP */
3302 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3305 SM_ENTER(WPA_PTK
, INITIALIZE
);
3306 else if (sm
->Disconnect
3307 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3308 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3309 "WPA_PTK: sm->Disconnect");
3310 SM_ENTER(WPA_PTK
, DISCONNECT
);
3312 else if (sm
->DeauthenticationRequest
)
3313 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3314 else if (sm
->AuthenticationRequest
)
3315 SM_ENTER(WPA_PTK
, AUTHENTICATION
);
3316 else if (sm
->ReAuthenticationRequest
)
3317 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3318 else if (sm
->PTKRequest
) {
3319 if (wpa_auth_sm_ptk_update(sm
) < 0)
3320 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3322 SM_ENTER(WPA_PTK
, PTKSTART
);
3323 } else switch (sm
->wpa_ptk_state
) {
3324 case WPA_PTK_INITIALIZE
:
3326 case WPA_PTK_DISCONNECT
:
3327 SM_ENTER(WPA_PTK
, DISCONNECTED
);
3329 case WPA_PTK_DISCONNECTED
:
3330 SM_ENTER(WPA_PTK
, INITIALIZE
);
3332 case WPA_PTK_AUTHENTICATION
:
3333 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
3335 case WPA_PTK_AUTHENTICATION2
:
3336 if (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
3337 wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
3338 WPA_EAPOL_keyRun
) > 0)
3339 SM_ENTER(WPA_PTK
, INITPMK
);
3340 else if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
) ||
3341 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OWE
3342 /* FIX: && 802.1X::keyRun */)
3343 SM_ENTER(WPA_PTK
, INITPSK
);
3344 else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
)
3345 SM_ENTER(WPA_PTK
, INITPMK
);
3347 case WPA_PTK_INITPMK
:
3348 if (wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
3349 WPA_EAPOL_keyAvailable
) > 0) {
3350 SM_ENTER(WPA_PTK
, PTKSTART
);
3352 } else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_DPP
&& sm
->pmksa
) {
3353 SM_ENTER(WPA_PTK
, PTKSTART
);
3354 #endif /* CONFIG_DPP */
3356 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3357 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3358 "INITPMK - keyAvailable = false");
3359 SM_ENTER(WPA_PTK
, DISCONNECT
);
3362 case WPA_PTK_INITPSK
:
3363 if (wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
,
3364 NULL
, NULL
, NULL
)) {
3365 SM_ENTER(WPA_PTK
, PTKSTART
);
3367 } else if (wpa_auth_uses_sae(sm
) && sm
->pmksa
) {
3368 SM_ENTER(WPA_PTK
, PTKSTART
);
3369 #endif /* CONFIG_SAE */
3371 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3372 "no PSK configured for the STA");
3373 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3374 SM_ENTER(WPA_PTK
, DISCONNECT
);
3377 case WPA_PTK_PTKSTART
:
3378 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3379 sm
->EAPOLKeyPairwise
)
3380 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3381 else if (sm
->TimeoutCtr
>
3382 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
) {
3383 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3385 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3386 "PTKSTART: Retry limit %u reached",
3387 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3388 SM_ENTER(WPA_PTK
, DISCONNECT
);
3389 } else if (sm
->TimeoutEvt
)
3390 SM_ENTER(WPA_PTK
, PTKSTART
);
3392 case WPA_PTK_PTKCALCNEGOTIATING
:
3393 if (sm
->MICVerified
)
3394 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING2
);
3395 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3396 sm
->EAPOLKeyPairwise
)
3397 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3398 else if (sm
->TimeoutEvt
)
3399 SM_ENTER(WPA_PTK
, PTKSTART
);
3401 case WPA_PTK_PTKCALCNEGOTIATING2
:
3402 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3404 case WPA_PTK_PTKINITNEGOTIATING
:
3405 if (sm
->update_snonce
)
3406 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
3407 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3408 sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3409 SM_ENTER(WPA_PTK
, PTKINITDONE
);
3410 else if (sm
->TimeoutCtr
>
3411 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
||
3412 (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3413 sm
->TimeoutCtr
> 1)) {
3414 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
3416 sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3417 "PTKINITNEGOTIATING: Retry limit %u reached",
3418 sm
->wpa_auth
->conf
.wpa_pairwise_update_count
);
3419 SM_ENTER(WPA_PTK
, DISCONNECT
);
3420 } else if (sm
->TimeoutEvt
)
3421 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
3423 case WPA_PTK_PTKINITDONE
:
3429 SM_STATE(WPA_PTK_GROUP
, IDLE
)
3431 SM_ENTRY_MA(WPA_PTK_GROUP
, IDLE
, wpa_ptk_group
);
3433 /* Init flag is not cleared here, so avoid busy
3434 * loop by claiming nothing changed. */
3435 sm
->changed
= FALSE
;
3437 sm
->GTimeoutCtr
= 0;
3441 SM_STATE(WPA_PTK_GROUP
, REKEYNEGOTIATING
)
3443 u8 rsc
[WPA_KEY_RSC_LEN
];
3444 struct wpa_group
*gsm
= sm
->group
;
3446 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
3448 u8
*gtk
, dummy_gtk
[32];
3450 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYNEGOTIATING
, wpa_ptk_group
);
3453 if (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3454 sm
->GTimeoutCtr
> 1) {
3455 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3458 if (sm
->GTimeoutCtr
> sm
->wpa_auth
->conf
.wpa_group_update_count
) {
3459 /* No point in sending the EAPOL-Key - we will disconnect
3460 * immediately following this. */
3464 if (sm
->wpa
== WPA_VERSION_WPA
)
3465 sm
->PInitAKeys
= FALSE
;
3466 sm
->TimeoutEvt
= FALSE
;
3467 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3468 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
3469 if (gsm
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
)
3470 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
3471 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3472 "sending 1/2 msg of Group Key Handshake");
3474 gtk
= gsm
->GTK
[gsm
->GN
- 1];
3475 if (sm
->wpa_auth
->conf
.disable_gtk
||
3476 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
) {
3478 * Provide unique random GTK to each STA to prevent use
3479 * of GTK in the BSS.
3481 if (random_get_bytes(dummy_gtk
, gsm
->GTK_len
) < 0)
3485 if (sm
->wpa
== WPA_VERSION_WPA2
) {
3486 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
3487 ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
3488 kde_buf
= os_malloc(kde_len
);
3489 if (kde_buf
== NULL
)
3492 kde
= pos
= kde_buf
;
3493 hdr
[0] = gsm
->GN
& 0x03;
3495 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
3497 pos
= ieee80211w_kde_add(sm
, pos
);
3498 if (ocv_oci_add(sm
, &pos
) < 0) {
3502 kde_len
= pos
- kde
;
3505 kde_len
= gsm
->GTK_len
;
3508 wpa_send_eapol(sm
->wpa_auth
, sm
,
3509 WPA_KEY_INFO_SECURE
|
3510 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
3511 WPA_KEY_INFO_MIC
: 0) |
3513 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
3514 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
3520 SM_STATE(WPA_PTK_GROUP
, REKEYESTABLISHED
)
3523 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3524 const u8
*key_data
, *mic
;
3525 struct ieee802_1x_hdr
*hdr
;
3526 struct wpa_eapol_key
*key
;
3527 struct wpa_eapol_ie_parse kde
;
3529 u16 key_data_length
;
3530 #endif /* CONFIG_OCV */
3532 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYESTABLISHED
, wpa_ptk_group
);
3533 sm
->EAPOLKeyReceived
= FALSE
;
3536 mic_len
= wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
);
3539 * Note: last_rx_eapol_key length fields have already been validated in
3542 hdr
= (struct ieee802_1x_hdr
*) sm
->last_rx_eapol_key
;
3543 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
3544 mic
= (u8
*) (key
+ 1);
3545 key_data
= mic
+ mic_len
+ 2;
3546 key_data_length
= WPA_GET_BE16(mic
+ mic_len
);
3547 if (key_data_length
> sm
->last_rx_eapol_key_len
- sizeof(*hdr
) -
3548 sizeof(*key
) - mic_len
- 2)
3551 if (wpa_parse_kde_ies(key_data
, key_data_length
, &kde
) < 0) {
3552 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3553 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3557 if (wpa_auth_uses_ocv(sm
)) {
3558 struct wpa_channel_info ci
;
3562 if (wpa_channel_info(wpa_auth
, &ci
) != 0) {
3563 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3564 "Failed to get channel info to validate received OCI in EAPOL-Key group 1/2");
3568 if (get_sta_tx_parameters(sm
,
3569 channel_width_to_int(ci
.chanwidth
),
3570 ci
.seg1_idx
, &tx_chanwidth
,
3574 if (ocv_verify_tx_params(kde
.oci
, kde
.oci_len
, &ci
,
3575 tx_chanwidth
, tx_seg1_idx
) != 0) {
3576 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
3581 #endif /* CONFIG_OCV */
3583 if (sm
->GUpdateStationKeys
)
3584 sm
->group
->GKeyDoneStations
--;
3585 sm
->GUpdateStationKeys
= FALSE
;
3586 sm
->GTimeoutCtr
= 0;
3587 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3588 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3589 "group key handshake completed (%s)",
3590 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
3595 SM_STATE(WPA_PTK_GROUP
, KEYERROR
)
3597 SM_ENTRY_MA(WPA_PTK_GROUP
, KEYERROR
, wpa_ptk_group
);
3598 if (sm
->GUpdateStationKeys
)
3599 sm
->group
->GKeyDoneStations
--;
3600 sm
->GUpdateStationKeys
= FALSE
;
3601 sm
->Disconnect
= TRUE
;
3602 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
3603 "group key handshake failed (%s) after %u tries",
3604 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN",
3605 sm
->wpa_auth
->conf
.wpa_group_update_count
);
3609 SM_STEP(WPA_PTK_GROUP
)
3611 if (sm
->Init
|| sm
->PtkGroupInit
) {
3612 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3613 sm
->PtkGroupInit
= FALSE
;
3614 } else switch (sm
->wpa_ptk_group_state
) {
3615 case WPA_PTK_GROUP_IDLE
:
3616 if (sm
->GUpdateStationKeys
||
3617 (sm
->wpa
== WPA_VERSION_WPA
&& sm
->PInitAKeys
))
3618 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3620 case WPA_PTK_GROUP_REKEYNEGOTIATING
:
3621 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
3622 !sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
3623 SM_ENTER(WPA_PTK_GROUP
, REKEYESTABLISHED
);
3624 else if (sm
->GTimeoutCtr
>
3625 sm
->wpa_auth
->conf
.wpa_group_update_count
||
3626 (sm
->wpa_auth
->conf
.wpa_disable_eapol_key_retries
&&
3627 sm
->GTimeoutCtr
> 1))
3628 SM_ENTER(WPA_PTK_GROUP
, KEYERROR
);
3629 else if (sm
->TimeoutEvt
)
3630 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
3632 case WPA_PTK_GROUP_KEYERROR
:
3633 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3635 case WPA_PTK_GROUP_REKEYESTABLISHED
:
3636 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
3642 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
3643 struct wpa_group
*group
)
3647 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3648 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3649 if (wpa_gmk_to_gtk(group
->GMK
, "Group key expansion",
3650 wpa_auth
->addr
, group
->GNonce
,
3651 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3653 wpa_hexdump_key(MSG_DEBUG
, "GTK",
3654 group
->GTK
[group
->GN
- 1], group
->GTK_len
);
3656 #ifdef CONFIG_IEEE80211W
3657 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3659 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3660 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
3661 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
3662 if (wpa_gmk_to_gtk(group
->GMK
, "IGTK key expansion",
3663 wpa_auth
->addr
, group
->GNonce
,
3664 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3666 wpa_hexdump_key(MSG_DEBUG
, "IGTK",
3667 group
->IGTK
[group
->GN_igtk
- 4], len
);
3669 #endif /* CONFIG_IEEE80211W */
3675 static void wpa_group_gtk_init(struct wpa_authenticator
*wpa_auth
,
3676 struct wpa_group
*group
)
3678 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3679 "GTK_INIT (VLAN-ID %d)", group
->vlan_id
);
3680 group
->changed
= FALSE
; /* GInit is not cleared here; avoid loop */
3681 group
->wpa_group_state
= WPA_GROUP_GTK_INIT
;
3684 os_memset(group
->GTK
, 0, sizeof(group
->GTK
));
3687 #ifdef CONFIG_IEEE80211W
3690 #endif /* CONFIG_IEEE80211W */
3691 /* GTK[GN] = CalcGTK() */
3692 wpa_gtk_update(wpa_auth
, group
);
3696 static int wpa_group_update_sta(struct wpa_state_machine
*sm
, void *ctx
)
3698 if (ctx
!= NULL
&& ctx
!= sm
->group
)
3701 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITDONE
) {
3702 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3703 "Not in PTKINITDONE; skip Group Key update");
3704 sm
->GUpdateStationKeys
= FALSE
;
3707 if (sm
->GUpdateStationKeys
) {
3709 * This should not really happen, so add a debug log entry.
3710 * Since we clear the GKeyDoneStations before the loop, the
3711 * station needs to be counted here anyway.
3713 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
3714 "GUpdateStationKeys was already set when "
3715 "marking station for GTK rekeying");
3718 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
3719 if (sm
->is_wnmsleep
)
3722 sm
->group
->GKeyDoneStations
++;
3723 sm
->GUpdateStationKeys
= TRUE
;
3730 #ifdef CONFIG_WNM_AP
3731 /* update GTK when exiting WNM-Sleep Mode */
3732 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine
*sm
)
3734 if (sm
== NULL
|| sm
->is_wnmsleep
)
3737 wpa_group_update_sta(sm
, NULL
);
3741 void wpa_set_wnmsleep(struct wpa_state_machine
*sm
, int flag
)
3744 sm
->is_wnmsleep
= !!flag
;
3748 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3750 struct wpa_group
*gsm
= sm
->group
;
3755 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
3758 *pos
++ = WNM_SLEEP_SUBELEM_GTK
;
3759 *pos
++ = 11 + gsm
->GTK_len
;
3760 /* Key ID in B0-B1 of Key Info */
3761 WPA_PUT_LE16(pos
, gsm
->GN
& 0x03);
3763 *pos
++ = gsm
->GTK_len
;
3764 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, pos
) != 0)
3767 os_memcpy(pos
, gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
3768 pos
+= gsm
->GTK_len
;
3770 wpa_printf(MSG_DEBUG
, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3772 wpa_hexdump_key(MSG_DEBUG
, "WNM: GTK in WNM-Sleep Mode exit",
3773 gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
3779 #ifdef CONFIG_IEEE80211W
3780 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
3782 struct wpa_group
*gsm
= sm
->group
;
3784 size_t len
= wpa_cipher_key_len(sm
->wpa_auth
->conf
.group_mgmt_cipher
);
3788 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3790 *pos
++ = WNM_SLEEP_SUBELEM_IGTK
;
3791 *pos
++ = 2 + 6 + len
;
3792 WPA_PUT_LE16(pos
, gsm
->GN_igtk
);
3794 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, pos
) != 0)
3798 os_memcpy(pos
, gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3801 wpa_printf(MSG_DEBUG
, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3803 wpa_hexdump_key(MSG_DEBUG
, "WNM: IGTK in WNM-Sleep Mode exit",
3804 gsm
->IGTK
[gsm
->GN_igtk
- 4], len
);
3808 #endif /* CONFIG_IEEE80211W */
3809 #endif /* CONFIG_WNM_AP */
3812 static void wpa_group_setkeys(struct wpa_authenticator
*wpa_auth
,
3813 struct wpa_group
*group
)
3817 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3818 "SETKEYS (VLAN-ID %d)", group
->vlan_id
);
3819 group
->changed
= TRUE
;
3820 group
->wpa_group_state
= WPA_GROUP_SETKEYS
;
3821 group
->GTKReKey
= FALSE
;
3823 group
->GM
= group
->GN
;
3825 #ifdef CONFIG_IEEE80211W
3826 tmp
= group
->GM_igtk
;
3827 group
->GM_igtk
= group
->GN_igtk
;
3828 group
->GN_igtk
= tmp
;
3829 #endif /* CONFIG_IEEE80211W */
3830 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3831 * counting the STAs that are marked with GUpdateStationKeys instead of
3832 * including all STAs that could be in not-yet-completed state. */
3833 wpa_gtk_update(wpa_auth
, group
);
3835 if (group
->GKeyDoneStations
) {
3836 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: Unexpected "
3837 "GKeyDoneStations=%d when starting new GTK rekey",
3838 group
->GKeyDoneStations
);
3839 group
->GKeyDoneStations
= 0;
3841 wpa_auth_for_each_sta(wpa_auth
, wpa_group_update_sta
, group
);
3842 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: GKeyDoneStations=%d",
3843 group
->GKeyDoneStations
);
3847 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
3848 struct wpa_group
*group
)
3852 if (wpa_auth_set_key(wpa_auth
, group
->vlan_id
,
3853 wpa_cipher_to_alg(wpa_auth
->conf
.wpa_group
),
3854 broadcast_ether_addr
, group
->GN
,
3855 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
3858 #ifdef CONFIG_IEEE80211W
3859 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
3863 alg
= wpa_cipher_to_alg(wpa_auth
->conf
.group_mgmt_cipher
);
3864 len
= wpa_cipher_key_len(wpa_auth
->conf
.group_mgmt_cipher
);
3867 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, alg
,
3868 broadcast_ether_addr
, group
->GN_igtk
,
3869 group
->IGTK
[group
->GN_igtk
- 4], len
) < 0)
3872 #endif /* CONFIG_IEEE80211W */
3878 static int wpa_group_disconnect_cb(struct wpa_state_machine
*sm
, void *ctx
)
3880 if (sm
->group
== ctx
) {
3881 wpa_printf(MSG_DEBUG
, "WPA: Mark STA " MACSTR
3882 " for discconnection due to fatal failure",
3884 sm
->Disconnect
= TRUE
;
3891 static void wpa_group_fatal_failure(struct wpa_authenticator
*wpa_auth
,
3892 struct wpa_group
*group
)
3894 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state FATAL_FAILURE");
3895 group
->changed
= TRUE
;
3896 group
->wpa_group_state
= WPA_GROUP_FATAL_FAILURE
;
3897 wpa_auth_for_each_sta(wpa_auth
, wpa_group_disconnect_cb
, group
);
3901 static int wpa_group_setkeysdone(struct wpa_authenticator
*wpa_auth
,
3902 struct wpa_group
*group
)
3904 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
3905 "SETKEYSDONE (VLAN-ID %d)", group
->vlan_id
);
3906 group
->changed
= TRUE
;
3907 group
->wpa_group_state
= WPA_GROUP_SETKEYSDONE
;
3909 if (wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
3910 wpa_group_fatal_failure(wpa_auth
, group
);
3918 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
3919 struct wpa_group
*group
)
3922 wpa_group_gtk_init(wpa_auth
, group
);
3923 } else if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
) {
3924 /* Do not allow group operations */
3925 } else if (group
->wpa_group_state
== WPA_GROUP_GTK_INIT
&&
3926 group
->GTKAuthenticator
) {
3927 wpa_group_setkeysdone(wpa_auth
, group
);
3928 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
&&
3930 wpa_group_setkeys(wpa_auth
, group
);
3931 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYS
) {
3932 if (group
->GKeyDoneStations
== 0)
3933 wpa_group_setkeysdone(wpa_auth
, group
);
3934 else if (group
->GTKReKey
)
3935 wpa_group_setkeys(wpa_auth
, group
);
3940 static int wpa_sm_step(struct wpa_state_machine
*sm
)
3945 if (sm
->in_step_loop
) {
3946 /* This should not happen, but if it does, make sure we do not
3947 * end up freeing the state machine too early by exiting the
3948 * recursive call. */
3949 wpa_printf(MSG_ERROR
, "WPA: wpa_sm_step() called recursively");
3953 sm
->in_step_loop
= 1;
3955 if (sm
->pending_deinit
)
3958 sm
->changed
= FALSE
;
3959 sm
->wpa_auth
->group
->changed
= FALSE
;
3961 SM_STEP_RUN(WPA_PTK
);
3962 if (sm
->pending_deinit
)
3964 SM_STEP_RUN(WPA_PTK_GROUP
);
3965 if (sm
->pending_deinit
)
3967 wpa_group_sm_step(sm
->wpa_auth
, sm
->group
);
3968 } while (sm
->changed
|| sm
->wpa_auth
->group
->changed
);
3969 sm
->in_step_loop
= 0;
3971 if (sm
->pending_deinit
) {
3972 wpa_printf(MSG_DEBUG
, "WPA: Completing pending STA state "
3973 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
3974 wpa_free_sta_sm(sm
);
3981 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
)
3983 struct wpa_state_machine
*sm
= eloop_ctx
;
3988 void wpa_auth_sm_notify(struct wpa_state_machine
*sm
)
3992 eloop_register_timeout(0, 0, wpa_sm_call_step
, sm
, NULL
);
3996 void wpa_gtk_rekey(struct wpa_authenticator
*wpa_auth
)
3999 struct wpa_group
*group
;
4001 if (wpa_auth
== NULL
)
4004 group
= wpa_auth
->group
;
4006 for (i
= 0; i
< 2; i
++) {
4008 group
->GM
= group
->GN
;
4010 #ifdef CONFIG_IEEE80211W
4011 tmp
= group
->GM_igtk
;
4012 group
->GM_igtk
= group
->GN_igtk
;
4013 group
->GN_igtk
= tmp
;
4014 #endif /* CONFIG_IEEE80211W */
4015 wpa_gtk_update(wpa_auth
, group
);
4016 wpa_group_config_group_keys(wpa_auth
, group
);
4021 static const char * wpa_bool_txt(int val
)
4023 return val
? "TRUE" : "FALSE";
4027 #define RSN_SUITE "%02x-%02x-%02x-%d"
4028 #define RSN_SUITE_ARG(s) \
4029 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
4031 int wpa_get_mib(struct wpa_authenticator
*wpa_auth
, char *buf
, size_t buflen
)
4034 char pmkid_txt
[PMKID_LEN
* 2 + 1];
4035 #ifdef CONFIG_RSN_PREAUTH
4036 const int preauth
= 1;
4037 #else /* CONFIG_RSN_PREAUTH */
4038 const int preauth
= 0;
4039 #endif /* CONFIG_RSN_PREAUTH */
4041 if (wpa_auth
== NULL
)
4044 ret
= os_snprintf(buf
+ len
, buflen
- len
,
4045 "dot11RSNAOptionImplemented=TRUE\n"
4046 "dot11RSNAPreauthenticationImplemented=%s\n"
4047 "dot11RSNAEnabled=%s\n"
4048 "dot11RSNAPreauthenticationEnabled=%s\n",
4049 wpa_bool_txt(preauth
),
4050 wpa_bool_txt(wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
),
4051 wpa_bool_txt(wpa_auth
->conf
.rsn_preauth
));
4052 if (os_snprintf_error(buflen
- len
, ret
))
4056 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
4057 wpa_auth
->dot11RSNAPMKIDUsed
, PMKID_LEN
);
4060 buf
+ len
, buflen
- len
,
4061 "dot11RSNAConfigVersion=%u\n"
4062 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
4063 /* FIX: dot11RSNAConfigGroupCipher */
4064 /* FIX: dot11RSNAConfigGroupRekeyMethod */
4065 /* FIX: dot11RSNAConfigGroupRekeyTime */
4066 /* FIX: dot11RSNAConfigGroupRekeyPackets */
4067 "dot11RSNAConfigGroupRekeyStrict=%u\n"
4068 "dot11RSNAConfigGroupUpdateCount=%u\n"
4069 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
4070 "dot11RSNAConfigGroupCipherSize=%u\n"
4071 "dot11RSNAConfigPMKLifetime=%u\n"
4072 "dot11RSNAConfigPMKReauthThreshold=%u\n"
4073 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4074 "dot11RSNAConfigSATimeout=%u\n"
4075 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
4076 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
4077 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
4078 "dot11RSNAPMKIDUsed=%s\n"
4079 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
4080 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
4081 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
4082 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4083 "dot11RSNA4WayHandshakeFailures=%u\n"
4084 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4086 !!wpa_auth
->conf
.wpa_strict_rekey
,
4087 wpa_auth
->conf
.wpa_group_update_count
,
4088 wpa_auth
->conf
.wpa_pairwise_update_count
,
4089 wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
) * 8,
4090 dot11RSNAConfigPMKLifetime
,
4091 dot11RSNAConfigPMKReauthThreshold
,
4092 dot11RSNAConfigSATimeout
,
4093 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteSelected
),
4094 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherSelected
),
4095 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherSelected
),
4097 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteRequested
),
4098 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherRequested
),
4099 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherRequested
),
4100 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
,
4101 wpa_auth
->dot11RSNA4WayHandshakeFailures
);
4102 if (os_snprintf_error(buflen
- len
, ret
))
4106 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
4107 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4110 ret
= os_snprintf(buf
+ len
, buflen
- len
, "hostapdWPAGroupState=%d\n",
4111 wpa_auth
->group
->wpa_group_state
);
4112 if (os_snprintf_error(buflen
- len
, ret
))
4120 int wpa_get_mib_sta(struct wpa_state_machine
*sm
, char *buf
, size_t buflen
)
4128 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4130 /* dot11RSNAStatsEntry */
4132 pairwise
= wpa_cipher_to_suite(sm
->wpa
== WPA_VERSION_WPA2
?
4133 WPA_PROTO_RSN
: WPA_PROTO_WPA
,
4139 buf
+ len
, buflen
- len
,
4140 /* TODO: dot11RSNAStatsIndex */
4141 "dot11RSNAStatsSTAAddress=" MACSTR
"\n"
4142 "dot11RSNAStatsVersion=1\n"
4143 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE
"\n"
4144 /* TODO: dot11RSNAStatsTKIPICVErrors */
4145 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
4146 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
4147 /* TODO: dot11RSNAStatsCCMPReplays */
4148 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
4149 /* TODO: dot11RSNAStatsTKIPReplays */,
4151 RSN_SUITE_ARG(pairwise
),
4152 sm
->dot11RSNAStatsTKIPLocalMICFailures
,
4153 sm
->dot11RSNAStatsTKIPRemoteMICFailures
);
4154 if (os_snprintf_error(buflen
- len
, ret
))
4159 ret
= os_snprintf(buf
+ len
, buflen
- len
,
4160 "hostapdWPAPTKState=%d\n"
4161 "hostapdWPAPTKGroupState=%d\n",
4163 sm
->wpa_ptk_group_state
);
4164 if (os_snprintf_error(buflen
- len
, ret
))
4172 void wpa_auth_countermeasures_start(struct wpa_authenticator
*wpa_auth
)
4175 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
++;
4179 int wpa_auth_pairwise_set(struct wpa_state_machine
*sm
)
4181 return sm
&& sm
->pairwise_set
;
4185 int wpa_auth_get_pairwise(struct wpa_state_machine
*sm
)
4187 return sm
->pairwise
;
4191 const u8
* wpa_auth_get_pmk(struct wpa_state_machine
*sm
, int *len
)
4200 int wpa_auth_sta_key_mgmt(struct wpa_state_machine
*sm
)
4204 return sm
->wpa_key_mgmt
;
4208 int wpa_auth_sta_wpa_version(struct wpa_state_machine
*sm
)
4216 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine
*sm
)
4218 if (!sm
|| !wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
4220 return sm
->tk_already_set
;
4224 int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine
*sm
)
4226 if (!sm
|| !wpa_key_mgmt_fils(sm
->wpa_key_mgmt
))
4228 return sm
->tk_already_set
;
4232 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine
*sm
,
4233 struct rsn_pmksa_cache_entry
*entry
)
4235 if (sm
== NULL
|| sm
->pmksa
!= entry
)
4242 struct rsn_pmksa_cache_entry
*
4243 wpa_auth_sta_get_pmksa(struct wpa_state_machine
*sm
)
4245 return sm
? sm
->pmksa
: NULL
;
4249 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine
*sm
)
4252 sm
->dot11RSNAStatsTKIPLocalMICFailures
++;
4256 const u8
* wpa_auth_get_wpa_ie(struct wpa_authenticator
*wpa_auth
, size_t *len
)
4258 if (wpa_auth
== NULL
)
4260 *len
= wpa_auth
->wpa_ie_len
;
4261 return wpa_auth
->wpa_ie
;
4265 int wpa_auth_pmksa_add(struct wpa_state_machine
*sm
, const u8
*pmk
,
4266 unsigned int pmk_len
,
4267 int session_timeout
, struct eapol_state_machine
*eapol
)
4269 if (sm
== NULL
|| sm
->wpa
!= WPA_VERSION_WPA2
||
4270 sm
->wpa_auth
->conf
.disable_pmksa_caching
)
4273 if (wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) {
4274 if (pmk_len
> PMK_LEN_SUITE_B_192
)
4275 pmk_len
= PMK_LEN_SUITE_B_192
;
4276 } else if (pmk_len
> PMK_LEN
) {
4280 if (pmksa_cache_auth_add(sm
->wpa_auth
->pmksa
, pmk
, pmk_len
, NULL
,
4281 sm
->PTK
.kck
, sm
->PTK
.kck_len
,
4282 sm
->wpa_auth
->addr
, sm
->addr
, session_timeout
,
4283 eapol
, sm
->wpa_key_mgmt
))
4290 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator
*wpa_auth
,
4291 const u8
*pmk
, size_t len
, const u8
*sta_addr
,
4292 int session_timeout
,
4293 struct eapol_state_machine
*eapol
)
4295 if (wpa_auth
== NULL
)
4298 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, len
, NULL
,
4301 sta_addr
, session_timeout
, eapol
,
4302 WPA_KEY_MGMT_IEEE8021X
))
4309 int wpa_auth_pmksa_add_sae(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4310 const u8
*pmk
, const u8
*pmkid
)
4312 if (wpa_auth
->conf
.disable_pmksa_caching
)
4315 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, PMK_LEN
, pmkid
,
4317 wpa_auth
->addr
, addr
, 0, NULL
,
4325 void wpa_auth_add_sae_pmkid(struct wpa_state_machine
*sm
, const u8
*pmkid
)
4327 os_memcpy(sm
->pmkid
, pmkid
, PMKID_LEN
);
4332 int wpa_auth_pmksa_add2(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4333 const u8
*pmk
, size_t pmk_len
, const u8
*pmkid
,
4334 int session_timeout
, int akmp
)
4336 if (wpa_auth
->conf
.disable_pmksa_caching
)
4339 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, pmk_len
, pmkid
,
4340 NULL
, 0, wpa_auth
->addr
, addr
, session_timeout
,
4348 void wpa_auth_pmksa_remove(struct wpa_authenticator
*wpa_auth
,
4351 struct rsn_pmksa_cache_entry
*pmksa
;
4353 if (wpa_auth
== NULL
|| wpa_auth
->pmksa
== NULL
)
4355 pmksa
= pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, NULL
);
4357 wpa_printf(MSG_DEBUG
, "WPA: Remove PMKSA cache entry for "
4358 MACSTR
" based on request", MAC2STR(sta_addr
));
4359 pmksa_cache_free_entry(wpa_auth
->pmksa
, pmksa
);
4364 int wpa_auth_pmksa_list(struct wpa_authenticator
*wpa_auth
, char *buf
,
4367 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4369 return pmksa_cache_auth_list(wpa_auth
->pmksa
, buf
, len
);
4373 void wpa_auth_pmksa_flush(struct wpa_authenticator
*wpa_auth
)
4375 if (wpa_auth
&& wpa_auth
->pmksa
)
4376 pmksa_cache_auth_flush(wpa_auth
->pmksa
);
4380 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4383 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
4384 char *buf
, size_t len
)
4386 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4389 return pmksa_cache_auth_list_mesh(wpa_auth
->pmksa
, addr
, buf
, len
);
4393 struct rsn_pmksa_cache_entry
*
4394 wpa_auth_pmksa_create_entry(const u8
*aa
, const u8
*spa
, const u8
*pmk
,
4395 const u8
*pmkid
, int expiration
)
4397 struct rsn_pmksa_cache_entry
*entry
;
4398 struct os_reltime now
;
4400 entry
= pmksa_cache_auth_create_entry(pmk
, PMK_LEN
, pmkid
, NULL
, 0, aa
,
4401 spa
, 0, NULL
, WPA_KEY_MGMT_SAE
);
4405 os_get_reltime(&now
);
4406 entry
->expiration
= now
.sec
+ expiration
;
4411 int wpa_auth_pmksa_add_entry(struct wpa_authenticator
*wpa_auth
,
4412 struct rsn_pmksa_cache_entry
*entry
)
4416 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4419 ret
= pmksa_cache_auth_add_entry(wpa_auth
->pmksa
, entry
);
4421 wpa_printf(MSG_DEBUG
,
4422 "RSN: Failed to store external PMKSA cache for "
4423 MACSTR
, MAC2STR(entry
->spa
));
4428 #endif /* CONFIG_MESH */
4429 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4432 struct rsn_pmksa_cache_entry
*
4433 wpa_auth_pmksa_get(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
,
4436 if (!wpa_auth
|| !wpa_auth
->pmksa
)
4438 return pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, pmkid
);
4442 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry
*pmksa
,
4443 struct wpa_state_machine
*sm
,
4444 struct wpa_authenticator
*wpa_auth
,
4451 os_memcpy(pmk
, pmksa
->pmk
, PMK_LEN
);
4452 os_memcpy(pmkid
, pmksa
->pmkid
, PMKID_LEN
);
4453 os_memcpy(wpa_auth
->dot11RSNAPMKIDUsed
, pmksa
->pmkid
, PMKID_LEN
);
4458 * Remove and free the group from wpa_authenticator. This is triggered by a
4459 * callback to make sure nobody is currently iterating the group list while it
4462 static void wpa_group_free(struct wpa_authenticator
*wpa_auth
,
4463 struct wpa_group
*group
)
4465 struct wpa_group
*prev
= wpa_auth
->group
;
4467 wpa_printf(MSG_DEBUG
, "WPA: Remove group state machine for VLAN-ID %d",
4471 if (prev
->next
== group
) {
4472 /* This never frees the special first group as needed */
4473 prev
->next
= group
->next
;
4483 /* Increase the reference counter for group */
4484 static void wpa_group_get(struct wpa_authenticator
*wpa_auth
,
4485 struct wpa_group
*group
)
4487 /* Skip the special first group */
4488 if (wpa_auth
->group
== group
)
4491 group
->references
++;
4495 /* Decrease the reference counter and maybe free the group */
4496 static void wpa_group_put(struct wpa_authenticator
*wpa_auth
,
4497 struct wpa_group
*group
)
4499 /* Skip the special first group */
4500 if (wpa_auth
->group
== group
)
4503 group
->references
--;
4504 if (group
->references
)
4506 wpa_group_free(wpa_auth
, group
);
4511 * Add a group that has its references counter set to zero. Caller needs to
4512 * call wpa_group_get() on the return value to mark the entry in use.
4514 static struct wpa_group
*
4515 wpa_auth_add_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4517 struct wpa_group
*group
;
4519 if (wpa_auth
== NULL
|| wpa_auth
->group
== NULL
)
4522 wpa_printf(MSG_DEBUG
, "WPA: Add group state machine for VLAN-ID %d",
4524 group
= wpa_group_init(wpa_auth
, vlan_id
, 0);
4528 group
->next
= wpa_auth
->group
->next
;
4529 wpa_auth
->group
->next
= group
;
4536 * Enforce that the group state machine for the VLAN is running, increase
4537 * reference counter as interface is up. References might have been increased
4538 * even if a negative value is returned.
4539 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4541 int wpa_auth_ensure_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4543 struct wpa_group
*group
;
4545 if (wpa_auth
== NULL
)
4548 group
= wpa_auth
->group
;
4550 if (group
->vlan_id
== vlan_id
)
4552 group
= group
->next
;
4555 if (group
== NULL
) {
4556 group
= wpa_auth_add_group(wpa_auth
, vlan_id
);
4561 wpa_printf(MSG_DEBUG
,
4562 "WPA: Ensure group state machine running for VLAN ID %d",
4565 wpa_group_get(wpa_auth
, group
);
4566 group
->num_setup_iface
++;
4568 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4576 * Decrease reference counter, expected to be zero afterwards.
4577 * returns: -1 on error (group not found, group in fail state)
4578 * -2 if wpa_group is still referenced
4581 int wpa_auth_release_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
4583 struct wpa_group
*group
;
4586 if (wpa_auth
== NULL
)
4589 group
= wpa_auth
->group
;
4591 if (group
->vlan_id
== vlan_id
)
4593 group
= group
->next
;
4599 wpa_printf(MSG_DEBUG
,
4600 "WPA: Try stopping group state machine for VLAN ID %d",
4603 if (group
->num_setup_iface
<= 0) {
4604 wpa_printf(MSG_ERROR
,
4605 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4609 group
->num_setup_iface
--;
4611 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4614 if (group
->references
> 1) {
4615 wpa_printf(MSG_DEBUG
,
4616 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4621 wpa_group_put(wpa_auth
, group
);
4627 int wpa_auth_sta_set_vlan(struct wpa_state_machine
*sm
, int vlan_id
)
4629 struct wpa_group
*group
;
4631 if (sm
== NULL
|| sm
->wpa_auth
== NULL
)
4634 group
= sm
->wpa_auth
->group
;
4636 if (group
->vlan_id
== vlan_id
)
4638 group
= group
->next
;
4641 if (group
== NULL
) {
4642 group
= wpa_auth_add_group(sm
->wpa_auth
, vlan_id
);
4647 if (sm
->group
== group
)
4650 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
4653 wpa_printf(MSG_DEBUG
, "WPA: Moving STA " MACSTR
" to use group state "
4654 "machine for VLAN ID %d", MAC2STR(sm
->addr
), vlan_id
);
4656 wpa_group_get(sm
->wpa_auth
, group
);
4657 wpa_group_put(sm
->wpa_auth
, sm
->group
);
4664 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator
*wpa_auth
,
4665 struct wpa_state_machine
*sm
, int ack
)
4667 if (wpa_auth
== NULL
|| sm
== NULL
)
4669 wpa_printf(MSG_DEBUG
, "WPA: EAPOL-Key TX status for STA " MACSTR
4670 " ack=%d", MAC2STR(sm
->addr
), ack
);
4671 if (sm
->pending_1_of_4_timeout
&& ack
) {
4673 * Some deployed supplicant implementations update their SNonce
4674 * for each EAPOL-Key 2/4 message even within the same 4-way
4675 * handshake and then fail to use the first SNonce when
4676 * deriving the PTK. This results in unsuccessful 4-way
4677 * handshake whenever the relatively short initial timeout is
4678 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4679 * around this by increasing the timeout now that we know that
4680 * the station has received the frame.
4682 int timeout_ms
= eapol_key_timeout_subseq
;
4683 wpa_printf(MSG_DEBUG
, "WPA: Increase initial EAPOL-Key 1/4 "
4684 "timeout by %u ms because of acknowledged frame",
4686 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
4687 eloop_register_timeout(timeout_ms
/ 1000,
4688 (timeout_ms
% 1000) * 1000,
4689 wpa_send_eapol_timeout
, wpa_auth
, sm
);
4692 #ifdef CONFIG_TESTING_OPTIONS
4693 if (sm
->eapol_status_cb
) {
4694 sm
->eapol_status_cb(sm
->eapol_status_cb_ctx1
,
4695 sm
->eapol_status_cb_ctx2
);
4696 sm
->eapol_status_cb
= NULL
;
4698 #endif /* CONFIG_TESTING_OPTIONS */
4702 int wpa_auth_uses_sae(struct wpa_state_machine
*sm
)
4706 return wpa_key_mgmt_sae(sm
->wpa_key_mgmt
);
4710 int wpa_auth_uses_ft_sae(struct wpa_state_machine
*sm
)
4714 return sm
->wpa_key_mgmt
== WPA_KEY_MGMT_FT_SAE
;
4719 int wpa_auth_get_ip_addr(struct wpa_state_machine
*sm
, u8
*addr
)
4721 if (sm
== NULL
|| WPA_GET_BE32(sm
->ip_addr
) == 0)
4723 os_memcpy(addr
, sm
->ip_addr
, 4);
4726 #endif /* CONFIG_P2P */
4729 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator
*wpa_auth
,
4730 struct radius_das_attrs
*attr
)
4732 return pmksa_cache_auth_radius_das_disconnect(wpa_auth
->pmksa
, attr
);
4736 void wpa_auth_reconfig_group_keys(struct wpa_authenticator
*wpa_auth
)
4738 struct wpa_group
*group
;
4742 for (group
= wpa_auth
->group
; group
; group
= group
->next
)
4743 wpa_group_config_group_keys(wpa_auth
, group
);
4749 struct wpa_auth_fils_iter_data
{
4750 struct wpa_authenticator
*auth
;
4752 struct rsn_pmksa_cache_entry
*pmksa
;
4758 static int wpa_auth_fils_iter(struct wpa_authenticator
*a
, void *ctx
)
4760 struct wpa_auth_fils_iter_data
*data
= ctx
;
4762 if (a
== data
->auth
|| !a
->conf
.fils_cache_id_set
||
4763 os_memcmp(a
->conf
.fils_cache_id
, data
->cache_id
,
4764 FILS_CACHE_ID_LEN
) != 0)
4766 data
->pmksa
= pmksa_cache_auth_get(a
->pmksa
, data
->spa
, data
->pmkid
);
4767 return data
->pmksa
!= NULL
;
4771 struct rsn_pmksa_cache_entry
*
4772 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator
*wpa_auth
,
4773 const u8
*sta_addr
, const u8
*pmkid
)
4775 struct wpa_auth_fils_iter_data idata
;
4777 if (!wpa_auth
->conf
.fils_cache_id_set
)
4779 idata
.auth
= wpa_auth
;
4780 idata
.cache_id
= wpa_auth
->conf
.fils_cache_id
;
4782 idata
.spa
= sta_addr
;
4783 idata
.pmkid
= pmkid
;
4784 wpa_auth_for_each_auth(wpa_auth
, wpa_auth_fils_iter
, &idata
);
4789 #ifdef CONFIG_IEEE80211R_AP
4790 int wpa_auth_write_fte(struct wpa_authenticator
*wpa_auth
, int use_sha384
,
4791 u8
*buf
, size_t len
)
4793 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
4795 return wpa_write_ftie(conf
, use_sha384
, conf
->r0_key_holder
,
4796 conf
->r0_key_holder_len
,
4797 NULL
, NULL
, buf
, len
, NULL
, 0);
4799 #endif /* CONFIG_IEEE80211R_AP */
4802 void wpa_auth_get_fils_aead_params(struct wpa_state_machine
*sm
,
4803 u8
*fils_anonce
, u8
*fils_snonce
,
4804 u8
*fils_kek
, size_t *fils_kek_len
)
4806 os_memcpy(fils_anonce
, sm
->ANonce
, WPA_NONCE_LEN
);
4807 os_memcpy(fils_snonce
, sm
->SNonce
, WPA_NONCE_LEN
);
4808 os_memcpy(fils_kek
, sm
->PTK
.kek
, WPA_KEK_MAX_LEN
);
4809 *fils_kek_len
= sm
->PTK
.kek_len
;
4812 #endif /* CONFIG_FILS */
4815 #ifdef CONFIG_TESTING_OPTIONS
4817 int wpa_auth_resend_m1(struct wpa_state_machine
*sm
, int change_anonce
,
4818 void (*cb
)(void *ctx1
, void *ctx2
),
4819 void *ctx1
, void *ctx2
)
4821 const u8
*anonce
= sm
->ANonce
;
4822 u8 anonce_buf
[WPA_NONCE_LEN
];
4824 if (change_anonce
) {
4825 if (random_get_bytes(anonce_buf
, WPA_NONCE_LEN
))
4827 anonce
= anonce_buf
;
4830 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
4831 "sending 1/4 msg of 4-Way Handshake (TESTING)");
4832 wpa_send_eapol(sm
->wpa_auth
, sm
,
4833 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
4834 anonce
, NULL
, 0, 0, 0);
4839 int wpa_auth_resend_m3(struct wpa_state_machine
*sm
,
4840 void (*cb
)(void *ctx1
, void *ctx2
),
4841 void *ctx1
, void *ctx2
)
4843 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
;
4844 #ifdef CONFIG_IEEE80211W
4846 #endif /* CONFIG_IEEE80211W */
4847 size_t gtk_len
, kde_len
;
4848 struct wpa_group
*gsm
= sm
->group
;
4850 int wpa_ie_len
, secure
, keyidx
, encr
= 0;
4852 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
4853 GTK[GN], IGTK, [FTIE], [TIE * 2])
4857 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
4858 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
4859 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
4860 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
4861 if (sm
->wpa
== WPA_VERSION_WPA
&&
4862 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
4863 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
4864 /* WPA-only STA, remove RSN IE and possible MDIE */
4865 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
4866 if (wpa_ie
[0] == WLAN_EID_MOBILITY_DOMAIN
)
4867 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
4868 wpa_ie_len
= wpa_ie
[1] + 2;
4870 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
4871 "sending 3/4 msg of 4-Way Handshake (TESTING)");
4872 if (sm
->wpa
== WPA_VERSION_WPA2
) {
4873 /* WPA2 send GTK in the 4-way handshake */
4875 gtk
= gsm
->GTK
[gsm
->GN
- 1];
4876 gtk_len
= gsm
->GTK_len
;
4881 /* WPA does not include GTK in msg 3/4 */
4887 if (sm
->rx_eapol_key_secure
) {
4889 * It looks like Windows 7 supplicant tries to use
4890 * Secure bit in msg 2/4 after having reported Michael
4891 * MIC failure and it then rejects the 4-way handshake
4892 * if msg 3/4 does not set Secure bit. Work around this
4893 * by setting the Secure bit here even in the case of
4894 * WPA if the supplicant used it first.
4896 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
4897 "STA used Secure bit in WPA msg 2/4 - "
4898 "set Secure for 3/4 as workaround");
4903 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
4905 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
4906 #ifdef CONFIG_IEEE80211R_AP
4907 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
4908 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
4909 kde_len
+= 300; /* FTIE + 2 * TIE */
4911 #endif /* CONFIG_IEEE80211R_AP */
4912 kde
= os_malloc(kde_len
);
4917 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
4919 #ifdef CONFIG_IEEE80211R_AP
4920 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
4925 res
= wpa_insert_pmkid(kde
, &elen
, sm
->pmk_r1_name
);
4927 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
4928 "PMKR1Name into RSN IE in EAPOL-Key data");
4935 #endif /* CONFIG_IEEE80211R_AP */
4938 hdr
[0] = keyidx
& 0x03;
4940 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
4943 #ifdef CONFIG_IEEE80211W
4945 pos
= ieee80211w_kde_add(sm
, pos
);
4946 if (pos
- opos
>= 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
) {
4947 /* skip KDE header and keyid */
4948 opos
+= 2 + RSN_SELECTOR_LEN
+ 2;
4949 os_memset(opos
, 0, 6); /* clear PN */
4951 #endif /* CONFIG_IEEE80211W */
4952 if (ocv_oci_add(sm
, &pos
) < 0) {
4957 #ifdef CONFIG_IEEE80211R_AP
4958 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
4960 struct wpa_auth_config
*conf
;
4962 conf
= &sm
->wpa_auth
->conf
;
4963 if (sm
->assoc_resp_ftie
&&
4964 kde
+ kde_len
- pos
>= 2 + sm
->assoc_resp_ftie
[1]) {
4965 os_memcpy(pos
, sm
->assoc_resp_ftie
,
4966 2 + sm
->assoc_resp_ftie
[1]);
4967 res
= 2 + sm
->assoc_resp_ftie
[1];
4969 int use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
4971 res
= wpa_write_ftie(conf
, use_sha384
,
4972 conf
->r0_key_holder
,
4973 conf
->r0_key_holder_len
,
4975 kde
+ kde_len
- pos
,
4979 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
4980 "into EAPOL-Key Key Data");
4986 /* TIE[ReassociationDeadline] (TU) */
4987 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
4989 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
4990 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
4993 /* TIE[KeyLifetime] (seconds) */
4994 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
4996 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
4997 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
);
5000 #endif /* CONFIG_IEEE80211R_AP */
5002 wpa_send_eapol(sm
->wpa_auth
, sm
,
5003 (secure
? WPA_KEY_INFO_SECURE
: 0) |
5004 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
5005 WPA_KEY_INFO_MIC
: 0) |
5006 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
5007 WPA_KEY_INFO_KEY_TYPE
,
5008 _rsc
, sm
->ANonce
, kde
, pos
- kde
, keyidx
, encr
);
5014 int wpa_auth_resend_group_m1(struct wpa_state_machine
*sm
,
5015 void (*cb
)(void *ctx1
, void *ctx2
),
5016 void *ctx1
, void *ctx2
)
5018 u8 rsc
[WPA_KEY_RSC_LEN
];
5019 struct wpa_group
*gsm
= sm
->group
;
5021 u8
*kde_buf
= NULL
, *pos
, hdr
[2];
5022 #ifdef CONFIG_IEEE80211W
5024 #endif /* CONFIG_IEEE80211W */
5028 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
5029 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
5031 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
5032 "sending 1/2 msg of Group Key Handshake (TESTING)");
5034 gtk
= gsm
->GTK
[gsm
->GN
- 1];
5035 if (sm
->wpa
== WPA_VERSION_WPA2
) {
5036 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
5037 ieee80211w_kde_len(sm
) + ocv_oci_len(sm
);
5038 kde_buf
= os_malloc(kde_len
);
5039 if (kde_buf
== NULL
)
5042 kde
= pos
= kde_buf
;
5043 hdr
[0] = gsm
->GN
& 0x03;
5045 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
5047 #ifdef CONFIG_IEEE80211W
5049 pos
= ieee80211w_kde_add(sm
, pos
);
5051 2 + RSN_SELECTOR_LEN
+ WPA_IGTK_KDE_PREFIX_LEN
) {
5052 /* skip KDE header and keyid */
5053 opos
+= 2 + RSN_SELECTOR_LEN
+ 2;
5054 os_memset(opos
, 0, 6); /* clear PN */
5056 #endif /* CONFIG_IEEE80211W */
5057 if (ocv_oci_add(sm
, &pos
) < 0) {
5061 kde_len
= pos
- kde
;
5064 kde_len
= gsm
->GTK_len
;
5067 sm
->eapol_status_cb
= cb
;
5068 sm
->eapol_status_cb_ctx1
= ctx1
;
5069 sm
->eapol_status_cb_ctx2
= ctx2
;
5071 wpa_send_eapol(sm
->wpa_auth
, sm
,
5072 WPA_KEY_INFO_SECURE
|
5073 (wpa_mic_len(sm
->wpa_key_mgmt
, sm
->pmk_len
) ?
5074 WPA_KEY_INFO_MIC
: 0) |
5076 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
5077 rsc
, NULL
, kde
, kde_len
, gsm
->GN
, 1);
5084 int wpa_auth_rekey_gtk(struct wpa_authenticator
*wpa_auth
)
5088 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
5089 return eloop_register_timeout(0, 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
5092 #endif /* CONFIG_TESTING_OPTIONS */