2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/state_machine.h"
14 #include "utils/bitfield.h"
15 #include "common/ieee802_11_defs.h"
16 #include "crypto/aes_wrap.h"
17 #include "crypto/crypto.h"
18 #include "crypto/sha1.h"
19 #include "crypto/sha256.h"
20 #include "crypto/random.h"
21 #include "eapol_auth/eapol_auth_sm.h"
22 #include "ap_config.h"
23 #include "ieee802_11.h"
25 #include "pmksa_cache_auth.h"
26 #include "wpa_auth_i.h"
27 #include "wpa_auth_ie.h"
29 #define STATE_MACHINE_DATA struct wpa_state_machine
30 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
31 #define STATE_MACHINE_ADDR sm->addr
34 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
35 static int wpa_sm_step(struct wpa_state_machine *sm);
36 static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
38 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
39 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
40 struct wpa_group *group);
41 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
42 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
43 struct wpa_group *group);
44 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
45 struct wpa_group *group);
46 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
47 const u8 *pmk, struct wpa_ptk *ptk);
49 static const u32 dot11RSNAConfigGroupUpdateCount = 4;
50 static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
51 static const u32 eapol_key_timeout_first = 100; /* ms */
52 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
53 static const u32 eapol_key_timeout_first_group = 500; /* ms */
55 /* TODO: make these configurable */
56 static const int dot11RSNAConfigPMKLifetime = 43200;
57 static const int dot11RSNAConfigPMKReauthThreshold = 70;
58 static const int dot11RSNAConfigSATimeout = 60;
61 static inline int wpa_auth_mic_failure_report(
62 struct wpa_authenticator *wpa_auth, const u8 *addr)
64 if (wpa_auth->cb.mic_failure_report)
65 return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
70 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
71 const u8 *addr, wpa_eapol_variable var,
74 if (wpa_auth->cb.set_eapol)
75 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
79 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
80 const u8 *addr, wpa_eapol_variable var)
82 if (wpa_auth->cb.get_eapol == NULL)
84 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
88 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
90 const u8 *p2p_dev_addr,
93 if (wpa_auth->cb.get_psk == NULL)
95 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, p2p_dev_addr,
100 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
101 const u8 *addr, u8 *msk, size_t *len)
103 if (wpa_auth->cb.get_msk == NULL)
105 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
109 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
111 enum wpa_alg alg, const u8 *addr, int idx,
112 u8 *key, size_t key_len)
114 if (wpa_auth->cb.set_key == NULL)
116 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
121 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
122 const u8 *addr, int idx, u8 *seq)
124 if (wpa_auth->cb.get_seqnum == NULL)
126 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
131 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
132 const u8 *data, size_t data_len, int encrypt)
134 if (wpa_auth->cb.send_eapol == NULL)
136 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
142 static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
145 if (wpa_auth->cb.start_ampe == NULL)
147 return wpa_auth->cb.start_ampe(wpa_auth->cb.ctx, addr);
149 #endif /* CONFIG_MESH */
152 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
153 int (*cb)(struct wpa_state_machine *sm, void *ctx),
156 if (wpa_auth->cb.for_each_sta == NULL)
158 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
162 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
163 int (*cb)(struct wpa_authenticator *a, void *ctx),
166 if (wpa_auth->cb.for_each_auth == NULL)
168 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
172 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
173 logger_level level, const char *txt)
175 if (wpa_auth->cb.logger == NULL)
177 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
181 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
182 logger_level level, const char *fmt, ...)
188 if (wpa_auth->cb.logger == NULL)
191 maxlen = os_strlen(fmt) + 100;
192 format = os_malloc(maxlen);
197 vsnprintf(format, maxlen, fmt, ap);
200 wpa_auth_logger(wpa_auth, addr, level, format);
206 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
209 if (wpa_auth->cb.disconnect == NULL)
211 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
212 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
213 WLAN_REASON_PREV_AUTH_NOT_VALID);
217 static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
220 #ifdef CONFIG_IEEE80211R
221 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
223 #endif /* CONFIG_IEEE80211R */
224 #ifdef CONFIG_IEEE80211W
225 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
227 #endif /* CONFIG_IEEE80211W */
228 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
234 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
236 struct wpa_authenticator *wpa_auth = eloop_ctx;
238 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
239 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
242 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
243 wpa_hexdump_key(MSG_DEBUG, "GMK",
244 wpa_auth->group->GMK, WPA_GMK_LEN);
247 if (wpa_auth->conf.wpa_gmk_rekey) {
248 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
249 wpa_rekey_gmk, wpa_auth, NULL);
254 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
256 struct wpa_authenticator *wpa_auth = eloop_ctx;
257 struct wpa_group *group;
259 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
260 for (group = wpa_auth->group; group; group = group->next) {
261 group->GTKReKey = TRUE;
263 group->changed = FALSE;
264 wpa_group_sm_step(wpa_auth, group);
265 } while (group->changed);
268 if (wpa_auth->conf.wpa_group_rekey) {
269 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
270 0, wpa_rekey_gtk, wpa_auth, NULL);
275 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
277 struct wpa_authenticator *wpa_auth = eloop_ctx;
278 struct wpa_state_machine *sm = timeout_ctx;
280 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
281 wpa_request_new_ptk(sm);
286 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
288 if (sm->pmksa == ctx)
294 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
297 struct wpa_authenticator *wpa_auth = ctx;
298 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
302 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
303 struct wpa_group *group)
305 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
309 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
311 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
314 * Counter = PRF-256(Random number, "Init Counter",
315 * Local MAC Address || Time)
317 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
318 wpa_get_ntp_timestamp(buf + ETH_ALEN);
319 ptr = (unsigned long) group;
320 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
321 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
324 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
325 group->Counter, WPA_NONCE_LEN) < 0)
327 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
328 group->Counter, WPA_NONCE_LEN);
334 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
335 int vlan_id, int delay_init)
337 struct wpa_group *group;
339 group = os_zalloc(sizeof(struct wpa_group));
343 group->GTKAuthenticator = TRUE;
344 group->vlan_id = vlan_id;
345 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
347 if (random_pool_ready() != 1) {
348 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
349 "for secure operations - update keys later when "
350 "the first station connects");
354 * Set initial GMK/Counter value here. The actual values that will be
355 * used in negotiations will be set once the first station tries to
356 * connect. This allows more time for collecting additional randomness
357 * on embedded devices.
359 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
360 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
368 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
369 "until Beacon frames have been configured");
370 /* Initialization is completed in wpa_init_keys(). */
372 wpa_group_sm_step(wpa_auth, group);
373 group->GInit = FALSE;
374 wpa_group_sm_step(wpa_auth, group);
382 * wpa_init - Initialize WPA authenticator
383 * @addr: Authenticator address
384 * @conf: Configuration for WPA authenticator
385 * @cb: Callback functions for WPA authenticator
386 * Returns: Pointer to WPA authenticator data or %NULL on failure
388 struct wpa_authenticator * wpa_init(const u8 *addr,
389 struct wpa_auth_config *conf,
390 struct wpa_auth_callbacks *cb)
392 struct wpa_authenticator *wpa_auth;
394 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
395 if (wpa_auth == NULL)
397 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
398 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
399 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
401 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
402 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
407 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
408 if (wpa_auth->group == NULL) {
409 os_free(wpa_auth->wpa_ie);
414 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
416 if (wpa_auth->pmksa == NULL) {
417 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
418 os_free(wpa_auth->wpa_ie);
423 #ifdef CONFIG_IEEE80211R
424 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
425 if (wpa_auth->ft_pmk_cache == NULL) {
426 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
427 os_free(wpa_auth->wpa_ie);
428 pmksa_cache_auth_deinit(wpa_auth->pmksa);
432 #endif /* CONFIG_IEEE80211R */
434 if (wpa_auth->conf.wpa_gmk_rekey) {
435 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
436 wpa_rekey_gmk, wpa_auth, NULL);
439 if (wpa_auth->conf.wpa_group_rekey) {
440 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
441 wpa_rekey_gtk, wpa_auth, NULL);
445 if (WPA_GET_BE32(conf->ip_addr_start)) {
446 int count = WPA_GET_BE32(conf->ip_addr_end) -
447 WPA_GET_BE32(conf->ip_addr_start) + 1;
451 wpa_auth->ip_pool = bitfield_alloc(count);
453 #endif /* CONFIG_P2P */
459 int wpa_init_keys(struct wpa_authenticator *wpa_auth)
461 struct wpa_group *group = wpa_auth->group;
463 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
465 wpa_group_sm_step(wpa_auth, group);
466 group->GInit = FALSE;
467 wpa_group_sm_step(wpa_auth, group);
468 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
475 * wpa_deinit - Deinitialize WPA authenticator
476 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
478 void wpa_deinit(struct wpa_authenticator *wpa_auth)
480 struct wpa_group *group, *prev;
482 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
483 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
485 #ifdef CONFIG_PEERKEY
486 while (wpa_auth->stsl_negotiations)
487 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
488 #endif /* CONFIG_PEERKEY */
490 pmksa_cache_auth_deinit(wpa_auth->pmksa);
492 #ifdef CONFIG_IEEE80211R
493 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
494 wpa_auth->ft_pmk_cache = NULL;
495 #endif /* CONFIG_IEEE80211R */
498 bitfield_free(wpa_auth->ip_pool);
499 #endif /* CONFIG_P2P */
502 os_free(wpa_auth->wpa_ie);
504 group = wpa_auth->group;
516 * wpa_reconfig - Update WPA authenticator configuration
517 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
518 * @conf: Configuration for WPA authenticator
520 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
521 struct wpa_auth_config *conf)
523 struct wpa_group *group;
524 if (wpa_auth == NULL)
527 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
528 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
529 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
534 * Reinitialize GTK to make sure it is suitable for the new
537 group = wpa_auth->group;
538 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
540 wpa_group_sm_step(wpa_auth, group);
541 group->GInit = FALSE;
542 wpa_group_sm_step(wpa_auth, group);
548 struct wpa_state_machine *
549 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
550 const u8 *p2p_dev_addr)
552 struct wpa_state_machine *sm;
554 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
557 sm = os_zalloc(sizeof(struct wpa_state_machine));
560 os_memcpy(sm->addr, addr, ETH_ALEN);
562 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
564 sm->wpa_auth = wpa_auth;
565 sm->group = wpa_auth->group;
571 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
572 struct wpa_state_machine *sm)
574 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
577 #ifdef CONFIG_IEEE80211R
578 if (sm->ft_completed) {
579 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
580 "FT authentication already completed - do not "
581 "start 4-way handshake");
582 /* Go to PTKINITDONE state to allow GTK rekeying */
583 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
586 #endif /* CONFIG_IEEE80211R */
589 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
590 sm->ReAuthenticationRequest = TRUE;
591 return wpa_sm_step(sm);
594 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
595 "start authentication");
599 if (wpa_sm_step(sm) == 1)
600 return 1; /* should not really happen */
602 sm->AuthenticationRequest = TRUE;
603 return wpa_sm_step(sm);
607 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
609 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
610 * reassociates back to the same AP while the previous entry for the
611 * STA has not yet been removed. */
615 sm->wpa_key_mgmt = 0;
619 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
622 if (WPA_GET_BE32(sm->ip_addr)) {
624 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
625 "address %u.%u.%u.%u from " MACSTR,
626 sm->ip_addr[0], sm->ip_addr[1],
627 sm->ip_addr[2], sm->ip_addr[3],
629 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
630 bitfield_clear(sm->wpa_auth->ip_pool,
631 WPA_GET_BE32(sm->ip_addr) - start);
633 #endif /* CONFIG_P2P */
634 if (sm->GUpdateStationKeys) {
635 sm->group->GKeyDoneStations--;
636 sm->GUpdateStationKeys = FALSE;
638 #ifdef CONFIG_IEEE80211R
639 os_free(sm->assoc_resp_ftie);
640 wpabuf_free(sm->ft_pending_req_ies);
641 #endif /* CONFIG_IEEE80211R */
642 os_free(sm->last_rx_eapol_key);
648 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
653 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
654 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
655 "strict rekeying - force GTK rekey since STA "
657 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
658 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
662 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
663 sm->pending_1_of_4_timeout = 0;
664 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
665 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
666 if (sm->in_step_loop) {
667 /* Must not free state machine while wpa_sm_step() is running.
668 * Freeing will be completed in the end of wpa_sm_step(). */
669 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
670 "machine deinit for " MACSTR, MAC2STR(sm->addr));
671 sm->pending_deinit = 1;
677 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
682 sm->PTKRequest = TRUE;
687 static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
688 const u8 *replay_counter)
691 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
694 if (os_memcmp(replay_counter, ctr[i].counter,
695 WPA_REPLAY_COUNTER_LEN) == 0)
702 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
703 const u8 *replay_counter)
706 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
708 (replay_counter == NULL ||
709 os_memcmp(replay_counter, ctr[i].counter,
710 WPA_REPLAY_COUNTER_LEN) == 0))
711 ctr[i].valid = FALSE;
716 #ifdef CONFIG_IEEE80211R
717 static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
718 struct wpa_state_machine *sm,
719 struct wpa_eapol_ie_parse *kde)
721 struct wpa_ie_data ie;
722 struct rsn_mdie *mdie;
724 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
725 ie.num_pmkid != 1 || ie.pmkid == NULL) {
726 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
727 "FT 4-way handshake message 2/4");
731 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
732 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
733 sm->sup_pmk_r1_name, PMKID_LEN);
735 if (!kde->mdie || !kde->ftie) {
736 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
737 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
741 mdie = (struct rsn_mdie *) (kde->mdie + 2);
742 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
743 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
744 MOBILITY_DOMAIN_ID_LEN) != 0) {
745 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
749 if (sm->assoc_resp_ftie &&
750 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
751 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
752 2 + sm->assoc_resp_ftie[1]) != 0)) {
753 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
754 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
755 kde->ftie, kde->ftie_len);
756 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
757 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
763 #endif /* CONFIG_IEEE80211R */
766 static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
767 struct wpa_state_machine *sm, int group)
769 /* Supplicant reported a Michael MIC error */
770 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
771 "received EAPOL-Key Error Request "
772 "(STA detected Michael MIC failure (group=%d))",
775 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
776 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
777 "ignore Michael MIC failure report since "
778 "group cipher is not TKIP");
779 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
780 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
781 "ignore Michael MIC failure report since "
782 "pairwise cipher is not TKIP");
784 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
785 return 1; /* STA entry was removed */
786 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
787 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
791 * Error report is not a request for a new key handshake, but since
792 * Authenticator may do it, let's change the keys now anyway.
794 wpa_request_new_ptk(sm);
799 static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
804 const u8 *pmk = NULL;
807 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
808 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
809 sm->p2p_dev_addr, pmk);
815 wpa_derive_ptk(sm, sm->alt_SNonce, pmk, &PTK);
817 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len)
823 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
828 wpa_printf(MSG_DEBUG,
829 "WPA: Earlier SNonce did not result in matching MIC");
833 wpa_printf(MSG_DEBUG,
834 "WPA: Earlier SNonce resulted in matching MIC");
835 sm->alt_snonce_valid = 0;
836 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
837 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
838 sm->PTK_valid = TRUE;
844 void wpa_receive(struct wpa_authenticator *wpa_auth,
845 struct wpa_state_machine *sm,
846 u8 *data, size_t data_len)
848 struct ieee802_1x_hdr *hdr;
849 struct wpa_eapol_key *key;
850 u16 key_info, key_data_length;
851 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
852 SMK_M1, SMK_M3, SMK_ERROR } msg;
854 struct wpa_eapol_ie_parse kde;
856 const u8 *eapol_key_ie;
857 size_t eapol_key_ie_len;
859 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
862 if (data_len < sizeof(*hdr) + sizeof(*key))
865 hdr = (struct ieee802_1x_hdr *) data;
866 key = (struct wpa_eapol_key *) (hdr + 1);
867 key_info = WPA_GET_BE16(key->key_info);
868 key_data_length = WPA_GET_BE16(key->key_data_length);
869 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
870 " key_info=0x%x type=%u key_data_length=%u",
871 MAC2STR(sm->addr), key_info, key->type, key_data_length);
872 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
873 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
874 "key_data overflow (%d > %lu)",
876 (unsigned long) (data_len - sizeof(*hdr) -
881 if (sm->wpa == WPA_VERSION_WPA2) {
882 if (key->type == EAPOL_KEY_TYPE_WPA) {
884 * Some deployed station implementations seem to send
885 * msg 4/4 with incorrect type value in WPA2 mode.
887 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
888 "with unexpected WPA type in RSN mode");
889 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
890 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
891 "unexpected type %d in RSN mode",
896 if (key->type != EAPOL_KEY_TYPE_WPA) {
897 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
898 "unexpected type %d in WPA mode",
904 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
906 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
907 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
909 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
912 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
913 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
914 if (key_info & WPA_KEY_INFO_ERROR) {
916 msgtxt = "SMK Error";
921 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
924 } else if (key_info & WPA_KEY_INFO_REQUEST) {
927 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
929 msgtxt = "2/2 Group";
930 } else if (key_data_length == 0) {
932 msgtxt = "4/4 Pairwise";
935 msgtxt = "2/4 Pairwise";
938 /* TODO: key_info type validation for PeerKey */
939 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
941 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
942 if (sm->pairwise == WPA_CIPHER_CCMP ||
943 sm->pairwise == WPA_CIPHER_GCMP) {
944 if (wpa_use_aes_cmac(sm) &&
945 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
946 !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
947 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
948 wpa_auth_logger(wpa_auth, sm->addr,
950 "advertised support for "
951 "AES-128-CMAC, but did not "
956 if (!wpa_use_aes_cmac(sm) &&
957 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
958 wpa_auth_logger(wpa_auth, sm->addr,
960 "did not use HMAC-SHA1-AES "
966 if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
967 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
968 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
969 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
974 if (key_info & WPA_KEY_INFO_REQUEST) {
975 if (sm->req_replay_counter_used &&
976 os_memcmp(key->replay_counter, sm->req_replay_counter,
977 WPA_REPLAY_COUNTER_LEN) <= 0) {
978 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
979 "received EAPOL-Key request with "
985 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
986 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
989 if (msg == PAIRWISE_2 &&
990 wpa_replay_counter_valid(sm->prev_key_replay,
991 key->replay_counter) &&
992 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
993 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
996 * Some supplicant implementations (e.g., Windows XP
997 * WZC) update SNonce for each EAPOL-Key 2/4. This
998 * breaks the workaround on accepting any of the
999 * pending requests, so allow the SNonce to be updated
1000 * even if we have already sent out EAPOL-Key 3/4.
1002 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1003 "Process SNonce update from STA "
1004 "based on retransmitted EAPOL-Key "
1006 sm->update_snonce = 1;
1007 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1008 sm->alt_snonce_valid = TRUE;
1009 os_memcpy(sm->alt_replay_counter,
1010 sm->key_replay[0].counter,
1011 WPA_REPLAY_COUNTER_LEN);
1012 goto continue_processing;
1015 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1016 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1017 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1018 WPA_REPLAY_COUNTER_LEN) == 0) {
1020 * Supplicant may still be using the old SNonce since
1021 * there was two EAPOL-Key 2/4 messages and they had
1022 * different SNonce values.
1024 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1025 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1026 goto continue_processing;
1029 if (msg == PAIRWISE_2 &&
1030 wpa_replay_counter_valid(sm->prev_key_replay,
1031 key->replay_counter) &&
1032 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1033 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1034 "ignore retransmitted EAPOL-Key %s - "
1035 "SNonce did not change", msgtxt);
1037 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1038 "received EAPOL-Key %s with "
1039 "unexpected replay counter", msgtxt);
1041 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1042 if (!sm->key_replay[i].valid)
1044 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1045 sm->key_replay[i].counter,
1046 WPA_REPLAY_COUNTER_LEN);
1048 wpa_hexdump(MSG_DEBUG, "received replay counter",
1049 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1053 continue_processing:
1056 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
1057 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1058 (!sm->update_snonce ||
1059 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
1060 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1061 "received EAPOL-Key msg 2/4 in "
1062 "invalid state (%d) - dropped",
1066 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1067 if (sm->group->reject_4way_hs_for_entropy) {
1069 * The system did not have enough entropy to generate
1070 * strong random numbers. Reject the first 4-way
1071 * handshake(s) and collect some entropy based on the
1072 * information from it. Once enough entropy is
1073 * available, the next atempt will trigger GMK/Key
1074 * Counter update and the station will be allowed to
1077 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1078 "collect more entropy for random number "
1080 random_mark_pool_ready();
1081 wpa_sta_disconnect(wpa_auth, sm->addr);
1084 if (wpa_parse_kde_ies((u8 *) (key + 1), key_data_length,
1086 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1087 "received EAPOL-Key msg 2/4 with "
1088 "invalid Key Data contents");
1092 eapol_key_ie = kde.rsn_ie;
1093 eapol_key_ie_len = kde.rsn_ie_len;
1094 } else if (kde.osen) {
1095 eapol_key_ie = kde.osen;
1096 eapol_key_ie_len = kde.osen_len;
1098 eapol_key_ie = kde.wpa_ie;
1099 eapol_key_ie_len = kde.wpa_ie_len;
1101 ft = sm->wpa == WPA_VERSION_WPA2 &&
1102 wpa_key_mgmt_ft(sm->wpa_key_mgmt);
1103 if (sm->wpa_ie == NULL ||
1104 wpa_compare_rsn_ie(ft,
1105 sm->wpa_ie, sm->wpa_ie_len,
1106 eapol_key_ie, eapol_key_ie_len)) {
1107 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1108 "WPA IE from (Re)AssocReq did not "
1109 "match with msg 2/4");
1111 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
1112 sm->wpa_ie, sm->wpa_ie_len);
1114 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
1115 eapol_key_ie, eapol_key_ie_len);
1116 /* MLME-DEAUTHENTICATE.request */
1117 wpa_sta_disconnect(wpa_auth, sm->addr);
1120 #ifdef CONFIG_IEEE80211R
1121 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
1122 wpa_sta_disconnect(wpa_auth, sm->addr);
1125 #endif /* CONFIG_IEEE80211R */
1127 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
1128 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
1130 wpa_printf(MSG_DEBUG, "P2P: IP address requested in "
1131 "EAPOL-Key exchange");
1132 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
1134 u32 start = WPA_GET_BE32(wpa_auth->conf.
1136 bitfield_set(wpa_auth->ip_pool, idx);
1137 WPA_PUT_BE32(sm->ip_addr, start + idx);
1138 wpa_printf(MSG_DEBUG, "P2P: Assigned IP "
1139 "address %u.%u.%u.%u to " MACSTR,
1140 sm->ip_addr[0], sm->ip_addr[1],
1141 sm->ip_addr[2], sm->ip_addr[3],
1145 #endif /* CONFIG_P2P */
1148 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1150 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1151 "received EAPOL-Key msg 4/4 in "
1152 "invalid state (%d) - dropped",
1158 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1159 || !sm->PTK_valid) {
1160 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1161 "received EAPOL-Key msg 2/2 in "
1162 "invalid state (%d) - dropped",
1163 sm->wpa_ptk_group_state);
1167 #ifdef CONFIG_PEERKEY
1171 if (!wpa_auth->conf.peerkey) {
1172 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1173 "PeerKey use disabled - ignoring message");
1176 if (!sm->PTK_valid) {
1177 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1178 "received EAPOL-Key msg SMK in "
1179 "invalid state - dropped");
1183 #else /* CONFIG_PEERKEY */
1187 return; /* STSL disabled - ignore SMK messages */
1188 #endif /* CONFIG_PEERKEY */
1193 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1194 "received EAPOL-Key frame (%s)", msgtxt);
1196 if (key_info & WPA_KEY_INFO_ACK) {
1197 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1198 "received invalid EAPOL-Key: Key Ack set");
1202 if (!(key_info & WPA_KEY_INFO_MIC)) {
1203 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1204 "received invalid EAPOL-Key: Key MIC not set");
1208 sm->MICVerified = FALSE;
1209 if (sm->PTK_valid && !sm->update_snonce) {
1210 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
1212 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1213 wpa_try_alt_snonce(sm, data, data_len))) {
1214 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1215 "received EAPOL-Key with invalid MIC");
1218 sm->MICVerified = TRUE;
1219 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1220 sm->pending_1_of_4_timeout = 0;
1223 if (key_info & WPA_KEY_INFO_REQUEST) {
1224 if (sm->MICVerified) {
1225 sm->req_replay_counter_used = 1;
1226 os_memcpy(sm->req_replay_counter, key->replay_counter,
1227 WPA_REPLAY_COUNTER_LEN);
1229 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1230 "received EAPOL-Key request with "
1236 * TODO: should decrypt key data field if encryption was used;
1237 * even though MAC address KDE is not normally encrypted,
1238 * supplicant is allowed to encrypt it.
1240 if (msg == SMK_ERROR) {
1241 #ifdef CONFIG_PEERKEY
1242 wpa_smk_error(wpa_auth, sm, (const u8 *) (key + 1),
1244 #endif /* CONFIG_PEERKEY */
1246 } else if (key_info & WPA_KEY_INFO_ERROR) {
1247 if (wpa_receive_error_report(
1249 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1250 return; /* STA entry was removed */
1251 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1252 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1253 "received EAPOL-Key Request for new "
1255 wpa_request_new_ptk(sm);
1256 #ifdef CONFIG_PEERKEY
1257 } else if (msg == SMK_M1) {
1258 wpa_smk_m1(wpa_auth, sm, key, (const u8 *) (key + 1),
1260 #endif /* CONFIG_PEERKEY */
1261 } else if (key_data_length > 0 &&
1262 wpa_parse_kde_ies((const u8 *) (key + 1),
1263 key_data_length, &kde) == 0 &&
1266 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1267 "received EAPOL-Key Request for GTK "
1269 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1270 wpa_rekey_gtk(wpa_auth, NULL);
1273 /* Do not allow the same key replay counter to be reused. */
1274 wpa_replay_counter_mark_invalid(sm->key_replay,
1275 key->replay_counter);
1277 if (msg == PAIRWISE_2) {
1279 * Maintain a copy of the pending EAPOL-Key frames in
1280 * case the EAPOL-Key frame was retransmitted. This is
1281 * needed to allow EAPOL-Key msg 2/4 reply to another
1282 * pending msg 1/4 to update the SNonce to work around
1283 * unexpected supplicant behavior.
1285 os_memcpy(sm->prev_key_replay, sm->key_replay,
1286 sizeof(sm->key_replay));
1288 os_memset(sm->prev_key_replay, 0,
1289 sizeof(sm->prev_key_replay));
1293 * Make sure old valid counters are not accepted anymore and
1294 * do not get copied again.
1296 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
1299 #ifdef CONFIG_PEERKEY
1300 if (msg == SMK_M3) {
1301 wpa_smk_m3(wpa_auth, sm, key, (const u8 *) (key + 1),
1305 #endif /* CONFIG_PEERKEY */
1307 os_free(sm->last_rx_eapol_key);
1308 sm->last_rx_eapol_key = os_malloc(data_len);
1309 if (sm->last_rx_eapol_key == NULL)
1311 os_memcpy(sm->last_rx_eapol_key, data, data_len);
1312 sm->last_rx_eapol_key_len = data_len;
1314 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
1315 sm->EAPOLKeyReceived = TRUE;
1316 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1317 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1318 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1323 static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1324 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1326 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
1330 /* GTK = PRF-X(GMK, "Group key expansion",
1331 * AA || GNonce || Time || random data)
1332 * The example described in the IEEE 802.11 standard uses only AA and
1333 * GNonce as inputs here. Add some more entropy since this derivation
1334 * is done only at the Authenticator and as such, does not need to be
1337 os_memcpy(data, addr, ETH_ALEN);
1338 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1339 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1340 wpa_get_ntp_timestamp(pos);
1342 if (random_get_bytes(pos, 16) < 0)
1345 #ifdef CONFIG_IEEE80211W
1346 sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
1347 #else /* CONFIG_IEEE80211W */
1348 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
1351 #endif /* CONFIG_IEEE80211W */
1357 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1359 struct wpa_authenticator *wpa_auth = eloop_ctx;
1360 struct wpa_state_machine *sm = timeout_ctx;
1362 sm->pending_1_of_4_timeout = 0;
1363 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1364 sm->TimeoutEvt = TRUE;
1369 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1370 struct wpa_state_machine *sm, int key_info,
1371 const u8 *key_rsc, const u8 *nonce,
1372 const u8 *kde, size_t kde_len,
1373 int keyidx, int encr, int force_version)
1375 struct ieee802_1x_hdr *hdr;
1376 struct wpa_eapol_key *key;
1379 int key_data_len, pad_len = 0;
1381 int version, pairwise;
1384 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
1387 version = force_version;
1388 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1389 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt))
1390 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
1391 else if (wpa_use_aes_cmac(sm))
1392 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
1393 else if (sm->pairwise != WPA_CIPHER_TKIP)
1394 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1396 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1398 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1400 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1401 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1404 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1405 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1406 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1407 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1408 pairwise, (unsigned long) kde_len, keyidx, encr);
1410 key_data_len = kde_len;
1412 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1413 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1414 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1415 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1416 pad_len = key_data_len % 8;
1418 pad_len = 8 - pad_len;
1419 key_data_len += pad_len + 8;
1422 len += key_data_len;
1424 hdr = os_zalloc(len);
1427 hdr->version = wpa_auth->conf.eapol_version;
1428 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1429 hdr->length = host_to_be16(len - sizeof(*hdr));
1430 key = (struct wpa_eapol_key *) (hdr + 1);
1432 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1433 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1434 key_info |= version;
1435 if (encr && sm->wpa == WPA_VERSION_WPA2)
1436 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1437 if (sm->wpa != WPA_VERSION_WPA2)
1438 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1439 WPA_PUT_BE16(key->key_info, key_info);
1441 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1442 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
1443 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1444 WPA_PUT_BE16(key->key_length, 0);
1446 /* FIX: STSL: what to use as key_replay_counter? */
1447 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1448 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1449 os_memcpy(sm->key_replay[i].counter,
1450 sm->key_replay[i - 1].counter,
1451 WPA_REPLAY_COUNTER_LEN);
1453 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1454 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1455 WPA_REPLAY_COUNTER_LEN);
1456 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1457 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1458 sm->key_replay[0].valid = TRUE;
1461 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1464 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1467 os_memcpy(key + 1, kde, kde_len);
1468 WPA_PUT_BE16(key->key_data_length, kde_len);
1469 } else if (encr && kde) {
1470 buf = os_zalloc(key_data_len);
1476 os_memcpy(pos, kde, kde_len);
1482 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1484 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1485 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1486 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1487 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1488 if (aes_wrap(sm->PTK.kek, 16,
1489 (key_data_len - 8) / 8, buf,
1490 (u8 *) (key + 1))) {
1495 WPA_PUT_BE16(key->key_data_length, key_data_len);
1498 os_memcpy(key->key_iv,
1499 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1500 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1501 os_memcpy(ek, key->key_iv, 16);
1502 os_memcpy(ek + 16, sm->PTK.kek, 16);
1503 os_memcpy(key + 1, buf, key_data_len);
1504 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1505 WPA_PUT_BE16(key->key_data_length, key_data_len);
1510 if (key_info & WPA_KEY_INFO_MIC) {
1511 if (!sm->PTK_valid) {
1512 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1513 "PTK not valid when sending EAPOL-Key "
1518 wpa_eapol_key_mic(sm->PTK.kck, sm->wpa_key_mgmt, version,
1519 (u8 *) hdr, len, key->key_mic);
1520 #ifdef CONFIG_TESTING_OPTIONS
1522 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
1524 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1525 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1526 "Corrupting group EAPOL-Key Key MIC");
1529 #endif /* CONFIG_TESTING_OPTIONS */
1532 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1534 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1540 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1541 struct wpa_state_machine *sm, int key_info,
1542 const u8 *key_rsc, const u8 *nonce,
1543 const u8 *kde, size_t kde_len,
1544 int keyidx, int encr)
1547 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1553 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1556 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1557 if (ctr == 1 && wpa_auth->conf.tx_status)
1558 timeout_ms = pairwise ? eapol_key_timeout_first :
1559 eapol_key_timeout_first_group;
1561 timeout_ms = eapol_key_timeout_subseq;
1562 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1563 sm->pending_1_of_4_timeout = 1;
1564 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1565 "counter %d)", timeout_ms, ctr);
1566 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1567 wpa_send_eapol_timeout, wpa_auth, sm);
1571 static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
1574 struct ieee802_1x_hdr *hdr;
1575 struct wpa_eapol_key *key;
1580 if (data_len < sizeof(*hdr) + sizeof(*key))
1583 hdr = (struct ieee802_1x_hdr *) data;
1584 key = (struct wpa_eapol_key *) (hdr + 1);
1585 key_info = WPA_GET_BE16(key->key_info);
1586 os_memcpy(mic, key->key_mic, 16);
1587 os_memset(key->key_mic, 0, 16);
1588 if (wpa_eapol_key_mic(PTK->kck, akmp, key_info & WPA_KEY_INFO_TYPE_MASK,
1589 data, data_len, key->key_mic) ||
1590 os_memcmp_const(mic, key->key_mic, 16) != 0)
1592 os_memcpy(key->key_mic, mic, 16);
1597 void wpa_remove_ptk(struct wpa_state_machine *sm)
1599 sm->PTK_valid = FALSE;
1600 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1601 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
1602 sm->pairwise_set = FALSE;
1603 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1607 int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1614 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1615 "event %d notification", event);
1620 /* PTKs are derived through AMPE */
1621 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1626 #endif /* CONFIG_MESH */
1631 sm->DeauthenticationRequest = TRUE;
1634 case WPA_REAUTH_EAPOL:
1637 * When using WPS, we may end up here if the STA
1638 * manages to re-associate without the previous STA
1639 * entry getting removed. Consequently, we need to make
1640 * sure that the WPA state machines gets initialized
1641 * properly at this point.
1643 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1644 "started - initialize now");
1647 if (wpa_sm_step(sm) == 1)
1648 return 1; /* should not really happen */
1650 sm->AuthenticationRequest = TRUE;
1653 if (sm->GUpdateStationKeys) {
1655 * Reauthentication cancels the pending group key
1656 * update for this STA.
1658 sm->group->GKeyDoneStations--;
1659 sm->GUpdateStationKeys = FALSE;
1660 sm->PtkGroupInit = TRUE;
1662 sm->ReAuthenticationRequest = TRUE;
1665 #ifdef CONFIG_IEEE80211R
1666 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1667 "after association");
1668 wpa_ft_install_ptk(sm);
1670 /* Using FT protocol, not WPA auth state machine */
1671 sm->ft_completed = 1;
1673 #else /* CONFIG_IEEE80211R */
1675 #endif /* CONFIG_IEEE80211R */
1678 #ifdef CONFIG_IEEE80211R
1679 sm->ft_completed = 0;
1680 #endif /* CONFIG_IEEE80211R */
1682 #ifdef CONFIG_IEEE80211W
1683 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1685 #endif /* CONFIG_IEEE80211W */
1688 sm->PTK_valid = FALSE;
1689 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1691 if (event != WPA_REAUTH_EAPOL)
1695 return wpa_sm_step(sm);
1699 SM_STATE(WPA_PTK, INITIALIZE)
1701 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1703 /* Init flag is not cleared here, so avoid busy
1704 * loop by claiming nothing changed. */
1705 sm->changed = FALSE;
1709 if (sm->GUpdateStationKeys)
1710 sm->group->GKeyDoneStations--;
1711 sm->GUpdateStationKeys = FALSE;
1712 if (sm->wpa == WPA_VERSION_WPA)
1713 sm->PInitAKeys = FALSE;
1714 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1715 * Local AA > Remote AA)) */) {
1718 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1720 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1722 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1723 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1724 WPA_EAPOL_authorized, 0);
1729 SM_STATE(WPA_PTK, DISCONNECT)
1731 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1732 sm->Disconnect = FALSE;
1733 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1737 SM_STATE(WPA_PTK, DISCONNECTED)
1739 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1740 sm->DeauthenticationRequest = FALSE;
1744 SM_STATE(WPA_PTK, AUTHENTICATION)
1746 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1747 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1748 sm->PTK_valid = FALSE;
1749 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1751 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1752 sm->AuthenticationRequest = FALSE;
1756 static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1757 struct wpa_group *group)
1759 if (group->first_sta_seen)
1762 * System has run bit further than at the time hostapd was started
1763 * potentially very early during boot up. This provides better chances
1764 * of collecting more randomness on embedded systems. Re-initialize the
1765 * GMK and Counter here to improve their strength if there was not
1766 * enough entropy available immediately after system startup.
1768 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1770 if (random_pool_ready() != 1) {
1771 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1772 "to proceed - reject first 4-way handshake");
1773 group->reject_4way_hs_for_entropy = TRUE;
1775 group->first_sta_seen = TRUE;
1776 group->reject_4way_hs_for_entropy = FALSE;
1779 wpa_group_init_gmk_and_counter(wpa_auth, group);
1780 wpa_gtk_update(wpa_auth, group);
1781 wpa_group_config_group_keys(wpa_auth, group);
1785 SM_STATE(WPA_PTK, AUTHENTICATION2)
1787 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1789 wpa_group_ensure_init(sm->wpa_auth, sm->group);
1790 sm->ReAuthenticationRequest = FALSE;
1793 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1794 * ambiguous. The Authenticator state machine uses a counter that is
1795 * incremented by one for each 4-way handshake. However, the security
1796 * analysis of 4-way handshake points out that unpredictable nonces
1797 * help in preventing precomputation attacks. Instead of the state
1798 * machine definition, use an unpredictable nonce value here to provide
1799 * stronger protection against potential precomputation attacks.
1801 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1802 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1804 sm->Disconnect = TRUE;
1807 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1809 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1810 * logical place than INITIALIZE since AUTHENTICATION2 can be
1811 * re-entered on ReAuthenticationRequest without going through
1817 SM_STATE(WPA_PTK, INITPMK)
1819 u8 msk[2 * PMK_LEN];
1820 size_t len = 2 * PMK_LEN;
1822 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1823 #ifdef CONFIG_IEEE80211R
1825 #endif /* CONFIG_IEEE80211R */
1827 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1828 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1829 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1830 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1831 "(len=%lu)", (unsigned long) len);
1832 os_memcpy(sm->PMK, msk, PMK_LEN);
1833 #ifdef CONFIG_IEEE80211R
1834 if (len >= 2 * PMK_LEN) {
1835 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1836 sm->xxkey_len = PMK_LEN;
1838 #endif /* CONFIG_IEEE80211R */
1840 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1843 sm->req_replay_counter_used = 0;
1844 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1845 * will break reauthentication since EAPOL state machines may not be
1846 * get into AUTHENTICATING state that clears keyRun before WPA state
1847 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1848 * state and takes PMK from the previously used AAA Key. This will
1849 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1850 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1851 * be good workaround for this issue. */
1852 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1856 SM_STATE(WPA_PTK, INITPSK)
1859 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1860 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
1862 os_memcpy(sm->PMK, psk, PMK_LEN);
1863 #ifdef CONFIG_IEEE80211R
1864 os_memcpy(sm->xxkey, psk, PMK_LEN);
1865 sm->xxkey_len = PMK_LEN;
1866 #endif /* CONFIG_IEEE80211R */
1868 sm->req_replay_counter_used = 0;
1872 SM_STATE(WPA_PTK, PTKSTART)
1874 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1875 size_t pmkid_len = 0;
1877 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1878 sm->PTKRequest = FALSE;
1879 sm->TimeoutEvt = FALSE;
1880 sm->alt_snonce_valid = FALSE;
1883 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1884 /* No point in sending the EAPOL-Key - we will disconnect
1885 * immediately following this. */
1889 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1890 "sending 1/4 msg of 4-Way Handshake");
1892 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1893 * one possible PSK for this STA.
1895 if (sm->wpa == WPA_VERSION_WPA2 &&
1896 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1897 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
1899 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1900 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1901 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1902 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1904 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1905 sm->pmksa->pmkid, PMKID_LEN);
1906 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
1907 /* No KCK available to derive PMKID */
1911 * Calculate PMKID since no PMKSA cache entry was
1912 * available with pre-calculated PMKID.
1914 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1915 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1916 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1919 wpa_send_eapol(sm->wpa_auth, sm,
1920 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1921 sm->ANonce, pmkid, pmkid_len, 0, 0);
1925 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
1926 const u8 *pmk, struct wpa_ptk *ptk)
1928 size_t ptk_len = wpa_cipher_key_len(sm->pairwise) + 32;
1929 #ifdef CONFIG_IEEE80211R
1930 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1931 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1932 #endif /* CONFIG_IEEE80211R */
1934 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1935 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
1936 (u8 *) ptk, ptk_len,
1937 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1943 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1947 const u8 *pmk = NULL;
1949 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1950 sm->EAPOLKeyReceived = FALSE;
1951 sm->update_snonce = FALSE;
1953 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1954 * WPA-PSK: iterate through possible PSKs and select the one matching
1957 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1958 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
1959 sm->p2p_dev_addr, pmk);
1965 wpa_derive_ptk(sm, sm->SNonce, pmk, &PTK);
1967 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
1968 sm->last_rx_eapol_key,
1969 sm->last_rx_eapol_key_len) == 0) {
1974 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1979 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1980 "invalid MIC in msg 2/4 of 4-Way Handshake");
1984 #ifdef CONFIG_IEEE80211R
1985 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1987 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
1988 * with the value we derived.
1990 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
1991 WPA_PMK_NAME_LEN) != 0) {
1992 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1993 "PMKR1Name mismatch in FT 4-way "
1995 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
1997 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
1998 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1999 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2003 #endif /* CONFIG_IEEE80211R */
2005 sm->pending_1_of_4_timeout = 0;
2006 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
2008 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2009 /* PSK may have changed from the previous choice, so update
2010 * state machine data based on whatever PSK was selected here.
2012 os_memcpy(sm->PMK, pmk, PMK_LEN);
2015 sm->MICVerified = TRUE;
2017 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
2018 sm->PTK_valid = TRUE;
2022 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2024 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2029 #ifdef CONFIG_IEEE80211W
2031 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2033 if (sm->mgmt_frame_prot) {
2035 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2036 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
2043 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2045 struct wpa_igtk_kde igtk;
2046 struct wpa_group *gsm = sm->group;
2047 u8 rsc[WPA_KEY_RSC_LEN];
2048 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2050 if (!sm->mgmt_frame_prot)
2053 igtk.keyid[0] = gsm->GN_igtk;
2055 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
2056 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
2057 os_memset(igtk.pn, 0, sizeof(igtk.pn));
2059 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
2060 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
2061 if (sm->wpa_auth->conf.disable_gtk) {
2063 * Provide unique random IGTK to each STA to prevent use of
2066 if (random_get_bytes(igtk.igtk, len) < 0)
2069 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
2070 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
2076 #else /* CONFIG_IEEE80211W */
2078 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2084 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2089 #endif /* CONFIG_IEEE80211W */
2092 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2094 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
2095 size_t gtk_len, kde_len;
2096 struct wpa_group *gsm = sm->group;
2098 int wpa_ie_len, secure, keyidx, encr = 0;
2100 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2101 sm->TimeoutEvt = FALSE;
2104 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
2105 /* No point in sending the EAPOL-Key - we will disconnect
2106 * immediately following this. */
2110 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2111 GTK[GN], IGTK, [FTIE], [TIE * 2])
2113 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2114 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2115 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2116 wpa_ie = sm->wpa_auth->wpa_ie;
2117 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
2118 if (sm->wpa == WPA_VERSION_WPA &&
2119 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
2120 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
2121 /* WPA-only STA, remove RSN IE */
2122 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2123 wpa_ie_len = wpa_ie[1] + 2;
2125 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2126 "sending 3/4 msg of 4-Way Handshake");
2127 if (sm->wpa == WPA_VERSION_WPA2) {
2128 /* WPA2 send GTK in the 4-way handshake */
2130 gtk = gsm->GTK[gsm->GN - 1];
2131 gtk_len = gsm->GTK_len;
2132 if (sm->wpa_auth->conf.disable_gtk) {
2134 * Provide unique random GTK to each STA to prevent use
2135 * of GTK in the BSS.
2137 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
2145 /* WPA does not include GTK in msg 3/4 */
2151 if (sm->rx_eapol_key_secure) {
2153 * It looks like Windows 7 supplicant tries to use
2154 * Secure bit in msg 2/4 after having reported Michael
2155 * MIC failure and it then rejects the 4-way handshake
2156 * if msg 3/4 does not set Secure bit. Work around this
2157 * by setting the Secure bit here even in the case of
2158 * WPA if the supplicant used it first.
2160 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2161 "STA used Secure bit in WPA msg 2/4 - "
2162 "set Secure for 3/4 as workaround");
2167 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
2169 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
2170 #ifdef CONFIG_IEEE80211R
2171 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2172 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
2173 kde_len += 300; /* FTIE + 2 * TIE */
2175 #endif /* CONFIG_IEEE80211R */
2177 if (WPA_GET_BE32(sm->ip_addr) > 0)
2178 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
2179 #endif /* CONFIG_P2P */
2180 kde = os_malloc(kde_len);
2185 os_memcpy(pos, wpa_ie, wpa_ie_len);
2187 #ifdef CONFIG_IEEE80211R
2188 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2189 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
2191 wpa_printf(MSG_ERROR, "FT: Failed to insert "
2192 "PMKR1Name into RSN IE in EAPOL-Key data");
2198 #endif /* CONFIG_IEEE80211R */
2201 hdr[0] = keyidx & 0x03;
2203 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2206 pos = ieee80211w_kde_add(sm, pos);
2208 #ifdef CONFIG_IEEE80211R
2209 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2211 struct wpa_auth_config *conf;
2213 conf = &sm->wpa_auth->conf;
2214 res = wpa_write_ftie(conf, conf->r0_key_holder,
2215 conf->r0_key_holder_len,
2216 NULL, NULL, pos, kde + kde_len - pos,
2219 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2220 "into EAPOL-Key Key Data");
2226 /* TIE[ReassociationDeadline] (TU) */
2227 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2229 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2230 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2233 /* TIE[KeyLifetime] (seconds) */
2234 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2236 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2237 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2240 #endif /* CONFIG_IEEE80211R */
2242 if (WPA_GET_BE32(sm->ip_addr) > 0) {
2244 os_memcpy(addr, sm->ip_addr, 4);
2245 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
2246 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
2247 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
2248 addr, sizeof(addr), NULL, 0);
2250 #endif /* CONFIG_P2P */
2252 wpa_send_eapol(sm->wpa_auth, sm,
2253 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
2254 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2255 WPA_KEY_INFO_KEY_TYPE,
2256 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2261 SM_STATE(WPA_PTK, PTKINITDONE)
2263 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2264 sm->EAPOLKeyReceived = FALSE;
2266 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2267 int klen = wpa_cipher_key_len(sm->pairwise);
2268 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2269 sm->PTK.tk1, klen)) {
2270 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2273 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2274 sm->pairwise_set = TRUE;
2276 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2277 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2278 eloop_register_timeout(sm->wpa_auth->conf.
2279 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2283 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2284 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2285 WPA_EAPOL_authorized, 1);
2289 if (0 /* IBSS == TRUE */) {
2291 if (sm->keycount == 2) {
2292 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2293 WPA_EAPOL_portValid, 1);
2296 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2299 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2300 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2301 if (sm->wpa == WPA_VERSION_WPA)
2302 sm->PInitAKeys = TRUE;
2305 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2306 "pairwise key handshake completed (%s)",
2307 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2309 #ifdef CONFIG_IEEE80211R
2310 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2311 #endif /* CONFIG_IEEE80211R */
2317 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2320 SM_ENTER(WPA_PTK, INITIALIZE);
2321 else if (sm->Disconnect
2322 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2323 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2324 "WPA_PTK: sm->Disconnect");
2325 SM_ENTER(WPA_PTK, DISCONNECT);
2327 else if (sm->DeauthenticationRequest)
2328 SM_ENTER(WPA_PTK, DISCONNECTED);
2329 else if (sm->AuthenticationRequest)
2330 SM_ENTER(WPA_PTK, AUTHENTICATION);
2331 else if (sm->ReAuthenticationRequest)
2332 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2333 else if (sm->PTKRequest)
2334 SM_ENTER(WPA_PTK, PTKSTART);
2335 else switch (sm->wpa_ptk_state) {
2336 case WPA_PTK_INITIALIZE:
2338 case WPA_PTK_DISCONNECT:
2339 SM_ENTER(WPA_PTK, DISCONNECTED);
2341 case WPA_PTK_DISCONNECTED:
2342 SM_ENTER(WPA_PTK, INITIALIZE);
2344 case WPA_PTK_AUTHENTICATION:
2345 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2347 case WPA_PTK_AUTHENTICATION2:
2348 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2349 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2350 WPA_EAPOL_keyRun) > 0)
2351 SM_ENTER(WPA_PTK, INITPMK);
2352 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
2353 /* FIX: && 802.1X::keyRun */)
2354 SM_ENTER(WPA_PTK, INITPSK);
2356 case WPA_PTK_INITPMK:
2357 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2358 WPA_EAPOL_keyAvailable) > 0)
2359 SM_ENTER(WPA_PTK, PTKSTART);
2361 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2362 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2363 "INITPMK - keyAvailable = false");
2364 SM_ENTER(WPA_PTK, DISCONNECT);
2367 case WPA_PTK_INITPSK:
2368 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
2370 SM_ENTER(WPA_PTK, PTKSTART);
2372 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2373 "no PSK configured for the STA");
2374 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2375 SM_ENTER(WPA_PTK, DISCONNECT);
2378 case WPA_PTK_PTKSTART:
2379 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2380 sm->EAPOLKeyPairwise)
2381 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2382 else if (sm->TimeoutCtr >
2383 (int) dot11RSNAConfigPairwiseUpdateCount) {
2384 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2385 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2386 "PTKSTART: Retry limit %d reached",
2387 dot11RSNAConfigPairwiseUpdateCount);
2388 SM_ENTER(WPA_PTK, DISCONNECT);
2389 } else if (sm->TimeoutEvt)
2390 SM_ENTER(WPA_PTK, PTKSTART);
2392 case WPA_PTK_PTKCALCNEGOTIATING:
2393 if (sm->MICVerified)
2394 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2395 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2396 sm->EAPOLKeyPairwise)
2397 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2398 else if (sm->TimeoutEvt)
2399 SM_ENTER(WPA_PTK, PTKSTART);
2401 case WPA_PTK_PTKCALCNEGOTIATING2:
2402 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2404 case WPA_PTK_PTKINITNEGOTIATING:
2405 if (sm->update_snonce)
2406 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2407 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2408 sm->EAPOLKeyPairwise && sm->MICVerified)
2409 SM_ENTER(WPA_PTK, PTKINITDONE);
2410 else if (sm->TimeoutCtr >
2411 (int) dot11RSNAConfigPairwiseUpdateCount) {
2412 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2413 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2414 "PTKINITNEGOTIATING: Retry limit %d "
2416 dot11RSNAConfigPairwiseUpdateCount);
2417 SM_ENTER(WPA_PTK, DISCONNECT);
2418 } else if (sm->TimeoutEvt)
2419 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2421 case WPA_PTK_PTKINITDONE:
2427 SM_STATE(WPA_PTK_GROUP, IDLE)
2429 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2431 /* Init flag is not cleared here, so avoid busy
2432 * loop by claiming nothing changed. */
2433 sm->changed = FALSE;
2435 sm->GTimeoutCtr = 0;
2439 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2441 u8 rsc[WPA_KEY_RSC_LEN];
2442 struct wpa_group *gsm = sm->group;
2444 u8 *kde_buf = NULL, *pos, hdr[2];
2446 u8 *gtk, dummy_gtk[32];
2448 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
2451 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
2452 /* No point in sending the EAPOL-Key - we will disconnect
2453 * immediately following this. */
2457 if (sm->wpa == WPA_VERSION_WPA)
2458 sm->PInitAKeys = FALSE;
2459 sm->TimeoutEvt = FALSE;
2460 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2461 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2462 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2463 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2464 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2465 "sending 1/2 msg of Group Key Handshake");
2467 gtk = gsm->GTK[gsm->GN - 1];
2468 if (sm->wpa_auth->conf.disable_gtk) {
2470 * Provide unique random GTK to each STA to prevent use
2471 * of GTK in the BSS.
2473 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
2477 if (sm->wpa == WPA_VERSION_WPA2) {
2478 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
2479 ieee80211w_kde_len(sm);
2480 kde_buf = os_malloc(kde_len);
2481 if (kde_buf == NULL)
2484 kde = pos = kde_buf;
2485 hdr[0] = gsm->GN & 0x03;
2487 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2489 pos = ieee80211w_kde_add(sm, pos);
2490 kde_len = pos - kde;
2493 kde_len = gsm->GTK_len;
2496 wpa_send_eapol(sm->wpa_auth, sm,
2497 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
2499 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
2500 rsc, gsm->GNonce, kde, kde_len, gsm->GN, 1);
2506 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2508 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2509 sm->EAPOLKeyReceived = FALSE;
2510 if (sm->GUpdateStationKeys)
2511 sm->group->GKeyDoneStations--;
2512 sm->GUpdateStationKeys = FALSE;
2513 sm->GTimeoutCtr = 0;
2514 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2515 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2516 "group key handshake completed (%s)",
2517 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2522 SM_STATE(WPA_PTK_GROUP, KEYERROR)
2524 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2525 if (sm->GUpdateStationKeys)
2526 sm->group->GKeyDoneStations--;
2527 sm->GUpdateStationKeys = FALSE;
2528 sm->Disconnect = TRUE;
2532 SM_STEP(WPA_PTK_GROUP)
2534 if (sm->Init || sm->PtkGroupInit) {
2535 SM_ENTER(WPA_PTK_GROUP, IDLE);
2536 sm->PtkGroupInit = FALSE;
2537 } else switch (sm->wpa_ptk_group_state) {
2538 case WPA_PTK_GROUP_IDLE:
2539 if (sm->GUpdateStationKeys ||
2540 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2541 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2543 case WPA_PTK_GROUP_REKEYNEGOTIATING:
2544 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2545 !sm->EAPOLKeyPairwise && sm->MICVerified)
2546 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2547 else if (sm->GTimeoutCtr >
2548 (int) dot11RSNAConfigGroupUpdateCount)
2549 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2550 else if (sm->TimeoutEvt)
2551 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2553 case WPA_PTK_GROUP_KEYERROR:
2554 SM_ENTER(WPA_PTK_GROUP, IDLE);
2556 case WPA_PTK_GROUP_REKEYESTABLISHED:
2557 SM_ENTER(WPA_PTK_GROUP, IDLE);
2563 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
2564 struct wpa_group *group)
2568 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2569 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2570 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
2571 wpa_auth->addr, group->GNonce,
2572 group->GTK[group->GN - 1], group->GTK_len) < 0)
2574 wpa_hexdump_key(MSG_DEBUG, "GTK",
2575 group->GTK[group->GN - 1], group->GTK_len);
2577 #ifdef CONFIG_IEEE80211W
2578 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2580 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2581 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2582 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2583 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
2584 wpa_auth->addr, group->GNonce,
2585 group->IGTK[group->GN_igtk - 4], len) < 0)
2587 wpa_hexdump_key(MSG_DEBUG, "IGTK",
2588 group->IGTK[group->GN_igtk - 4], len);
2590 #endif /* CONFIG_IEEE80211W */
2596 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2597 struct wpa_group *group)
2599 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2600 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2601 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2602 group->wpa_group_state = WPA_GROUP_GTK_INIT;
2605 os_memset(group->GTK, 0, sizeof(group->GTK));
2608 #ifdef CONFIG_IEEE80211W
2611 #endif /* CONFIG_IEEE80211W */
2612 /* GTK[GN] = CalcGTK() */
2613 wpa_gtk_update(wpa_auth, group);
2617 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2619 if (ctx != NULL && ctx != sm->group)
2622 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2623 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2624 "Not in PTKINITDONE; skip Group Key update");
2625 sm->GUpdateStationKeys = FALSE;
2628 if (sm->GUpdateStationKeys) {
2630 * This should not really happen, so add a debug log entry.
2631 * Since we clear the GKeyDoneStations before the loop, the
2632 * station needs to be counted here anyway.
2634 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2635 "GUpdateStationKeys was already set when "
2636 "marking station for GTK rekeying");
2639 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
2640 if (sm->is_wnmsleep)
2643 sm->group->GKeyDoneStations++;
2644 sm->GUpdateStationKeys = TRUE;
2652 /* update GTK when exiting WNM-Sleep Mode */
2653 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
2655 if (sm == NULL || sm->is_wnmsleep)
2658 wpa_group_update_sta(sm, NULL);
2662 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
2665 sm->is_wnmsleep = !!flag;
2669 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2671 struct wpa_group *gsm = sm->group;
2676 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
2679 *pos++ = WNM_SLEEP_SUBELEM_GTK;
2680 *pos++ = 11 + gsm->GTK_len;
2681 /* Key ID in B0-B1 of Key Info */
2682 WPA_PUT_LE16(pos, gsm->GN & 0x03);
2684 *pos++ = gsm->GTK_len;
2685 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
2688 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2689 pos += gsm->GTK_len;
2691 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
2693 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
2694 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2700 #ifdef CONFIG_IEEE80211W
2701 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2703 struct wpa_group *gsm = sm->group;
2705 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2709 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
2711 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
2712 *pos++ = 2 + 6 + len;
2713 WPA_PUT_LE16(pos, gsm->GN_igtk);
2715 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
2719 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
2722 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
2724 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
2725 gsm->IGTK[gsm->GN_igtk - 4], len);
2729 #endif /* CONFIG_IEEE80211W */
2730 #endif /* CONFIG_WNM */
2733 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2734 struct wpa_group *group)
2738 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2739 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2740 group->changed = TRUE;
2741 group->wpa_group_state = WPA_GROUP_SETKEYS;
2742 group->GTKReKey = FALSE;
2744 group->GM = group->GN;
2746 #ifdef CONFIG_IEEE80211W
2747 tmp = group->GM_igtk;
2748 group->GM_igtk = group->GN_igtk;
2749 group->GN_igtk = tmp;
2750 #endif /* CONFIG_IEEE80211W */
2751 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2752 * counting the STAs that are marked with GUpdateStationKeys instead of
2753 * including all STAs that could be in not-yet-completed state. */
2754 wpa_gtk_update(wpa_auth, group);
2756 if (group->GKeyDoneStations) {
2757 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
2758 "GKeyDoneStations=%d when starting new GTK rekey",
2759 group->GKeyDoneStations);
2760 group->GKeyDoneStations = 0;
2762 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
2763 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2764 group->GKeyDoneStations);
2768 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
2769 struct wpa_group *group)
2773 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
2774 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
2775 broadcast_ether_addr, group->GN,
2776 group->GTK[group->GN - 1], group->GTK_len) < 0)
2779 #ifdef CONFIG_IEEE80211W
2780 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2784 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
2785 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2788 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
2789 broadcast_ether_addr, group->GN_igtk,
2790 group->IGTK[group->GN_igtk - 4], len) < 0)
2793 #endif /* CONFIG_IEEE80211W */
2799 static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
2801 if (sm->group == ctx) {
2802 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
2803 " for discconnection due to fatal failure",
2805 sm->Disconnect = TRUE;
2812 static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
2813 struct wpa_group *group)
2815 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
2816 group->changed = TRUE;
2817 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
2818 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
2822 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2823 struct wpa_group *group)
2825 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2826 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2827 group->changed = TRUE;
2828 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2830 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
2831 wpa_group_fatal_failure(wpa_auth, group);
2839 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2840 struct wpa_group *group)
2843 wpa_group_gtk_init(wpa_auth, group);
2844 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
2845 /* Do not allow group operations */
2846 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2847 group->GTKAuthenticator) {
2848 wpa_group_setkeysdone(wpa_auth, group);
2849 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2851 wpa_group_setkeys(wpa_auth, group);
2852 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2853 if (group->GKeyDoneStations == 0)
2854 wpa_group_setkeysdone(wpa_auth, group);
2855 else if (group->GTKReKey)
2856 wpa_group_setkeys(wpa_auth, group);
2861 static int wpa_sm_step(struct wpa_state_machine *sm)
2866 if (sm->in_step_loop) {
2867 /* This should not happen, but if it does, make sure we do not
2868 * end up freeing the state machine too early by exiting the
2869 * recursive call. */
2870 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2874 sm->in_step_loop = 1;
2876 if (sm->pending_deinit)
2879 sm->changed = FALSE;
2880 sm->wpa_auth->group->changed = FALSE;
2882 SM_STEP_RUN(WPA_PTK);
2883 if (sm->pending_deinit)
2885 SM_STEP_RUN(WPA_PTK_GROUP);
2886 if (sm->pending_deinit)
2888 wpa_group_sm_step(sm->wpa_auth, sm->group);
2889 } while (sm->changed || sm->wpa_auth->group->changed);
2890 sm->in_step_loop = 0;
2892 if (sm->pending_deinit) {
2893 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2894 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2895 wpa_free_sta_sm(sm);
2902 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2904 struct wpa_state_machine *sm = eloop_ctx;
2909 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2913 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2917 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2920 struct wpa_group *group;
2922 if (wpa_auth == NULL)
2925 group = wpa_auth->group;
2927 for (i = 0; i < 2; i++) {
2929 group->GM = group->GN;
2931 #ifdef CONFIG_IEEE80211W
2932 tmp = group->GM_igtk;
2933 group->GM_igtk = group->GN_igtk;
2934 group->GN_igtk = tmp;
2935 #endif /* CONFIG_IEEE80211W */
2936 wpa_gtk_update(wpa_auth, group);
2937 wpa_group_config_group_keys(wpa_auth, group);
2942 static const char * wpa_bool_txt(int bool)
2944 return bool ? "TRUE" : "FALSE";
2948 #define RSN_SUITE "%02x-%02x-%02x-%d"
2949 #define RSN_SUITE_ARG(s) \
2950 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2952 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2955 char pmkid_txt[PMKID_LEN * 2 + 1];
2956 #ifdef CONFIG_RSN_PREAUTH
2957 const int preauth = 1;
2958 #else /* CONFIG_RSN_PREAUTH */
2959 const int preauth = 0;
2960 #endif /* CONFIG_RSN_PREAUTH */
2962 if (wpa_auth == NULL)
2965 ret = os_snprintf(buf + len, buflen - len,
2966 "dot11RSNAOptionImplemented=TRUE\n"
2967 "dot11RSNAPreauthenticationImplemented=%s\n"
2968 "dot11RSNAEnabled=%s\n"
2969 "dot11RSNAPreauthenticationEnabled=%s\n",
2970 wpa_bool_txt(preauth),
2971 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2972 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2973 if (ret < 0 || (size_t) ret >= buflen - len)
2977 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2978 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2981 buf + len, buflen - len,
2982 "dot11RSNAConfigVersion=%u\n"
2983 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2984 /* FIX: dot11RSNAConfigGroupCipher */
2985 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2986 /* FIX: dot11RSNAConfigGroupRekeyTime */
2987 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2988 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2989 "dot11RSNAConfigGroupUpdateCount=%u\n"
2990 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2991 "dot11RSNAConfigGroupCipherSize=%u\n"
2992 "dot11RSNAConfigPMKLifetime=%u\n"
2993 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2994 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2995 "dot11RSNAConfigSATimeout=%u\n"
2996 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2997 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2998 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2999 "dot11RSNAPMKIDUsed=%s\n"
3000 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
3001 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
3002 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
3003 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
3004 "dot11RSNA4WayHandshakeFailures=%u\n"
3005 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
3007 !!wpa_auth->conf.wpa_strict_rekey,
3008 dot11RSNAConfigGroupUpdateCount,
3009 dot11RSNAConfigPairwiseUpdateCount,
3010 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
3011 dot11RSNAConfigPMKLifetime,
3012 dot11RSNAConfigPMKReauthThreshold,
3013 dot11RSNAConfigSATimeout,
3014 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
3015 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
3016 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
3018 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
3019 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
3020 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
3021 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
3022 wpa_auth->dot11RSNA4WayHandshakeFailures);
3023 if (ret < 0 || (size_t) ret >= buflen - len)
3027 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
3028 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
3031 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
3032 wpa_auth->group->wpa_group_state);
3033 if (ret < 0 || (size_t) ret >= buflen - len)
3041 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
3049 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
3051 /* dot11RSNAStatsEntry */
3053 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
3054 WPA_PROTO_RSN : WPA_PROTO_WPA,
3060 buf + len, buflen - len,
3061 /* TODO: dot11RSNAStatsIndex */
3062 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
3063 "dot11RSNAStatsVersion=1\n"
3064 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
3065 /* TODO: dot11RSNAStatsTKIPICVErrors */
3066 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
3067 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
3068 /* TODO: dot11RSNAStatsCCMPReplays */
3069 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
3070 /* TODO: dot11RSNAStatsTKIPReplays */,
3072 RSN_SUITE_ARG(pairwise),
3073 sm->dot11RSNAStatsTKIPLocalMICFailures,
3074 sm->dot11RSNAStatsTKIPRemoteMICFailures);
3075 if (ret < 0 || (size_t) ret >= buflen - len)
3080 ret = os_snprintf(buf + len, buflen - len,
3081 "hostapdWPAPTKState=%d\n"
3082 "hostapdWPAPTKGroupState=%d\n",
3084 sm->wpa_ptk_group_state);
3085 if (ret < 0 || (size_t) ret >= buflen - len)
3093 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
3096 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
3100 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
3102 return sm && sm->pairwise_set;
3106 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
3108 return sm->pairwise;
3112 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
3116 return sm->wpa_key_mgmt;
3120 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
3128 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
3129 struct rsn_pmksa_cache_entry *entry)
3131 if (sm == NULL || sm->pmksa != entry)
3138 struct rsn_pmksa_cache_entry *
3139 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
3141 return sm ? sm->pmksa : NULL;
3145 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
3148 sm->dot11RSNAStatsTKIPLocalMICFailures++;
3152 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
3154 if (wpa_auth == NULL)
3156 *len = wpa_auth->wpa_ie_len;
3157 return wpa_auth->wpa_ie;
3161 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
3162 int session_timeout, struct eapol_state_machine *eapol)
3164 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
3165 sm->wpa_auth->conf.disable_pmksa_caching)
3168 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
3169 sm->PTK.kck, sizeof(sm->PTK.kck),
3170 sm->wpa_auth->addr, sm->addr, session_timeout,
3171 eapol, sm->wpa_key_mgmt))
3178 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
3179 const u8 *pmk, size_t len, const u8 *sta_addr,
3180 int session_timeout,
3181 struct eapol_state_machine *eapol)
3183 if (wpa_auth == NULL)
3186 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len,
3189 sta_addr, session_timeout, eapol,
3190 WPA_KEY_MGMT_IEEE8021X))
3197 int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
3200 if (wpa_auth->conf.disable_pmksa_caching)
3203 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN,
3205 wpa_auth->addr, addr, 0, NULL,
3213 void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
3216 struct rsn_pmksa_cache_entry *pmksa;
3218 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
3220 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3222 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
3223 MACSTR " based on request", MAC2STR(sta_addr));
3224 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
3229 static struct wpa_group *
3230 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3232 struct wpa_group *group;
3234 if (wpa_auth == NULL || wpa_auth->group == NULL)
3237 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
3239 group = wpa_group_init(wpa_auth, vlan_id, 0);
3243 group->next = wpa_auth->group->next;
3244 wpa_auth->group->next = group;
3250 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
3252 struct wpa_group *group;
3254 if (sm == NULL || sm->wpa_auth == NULL)
3257 group = sm->wpa_auth->group;
3259 if (group->vlan_id == vlan_id)
3261 group = group->next;
3264 if (group == NULL) {
3265 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
3270 if (sm->group == group)
3273 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
3276 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
3277 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
3284 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
3285 struct wpa_state_machine *sm, int ack)
3287 if (wpa_auth == NULL || sm == NULL)
3289 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
3290 " ack=%d", MAC2STR(sm->addr), ack);
3291 if (sm->pending_1_of_4_timeout && ack) {
3293 * Some deployed supplicant implementations update their SNonce
3294 * for each EAPOL-Key 2/4 message even within the same 4-way
3295 * handshake and then fail to use the first SNonce when
3296 * deriving the PTK. This results in unsuccessful 4-way
3297 * handshake whenever the relatively short initial timeout is
3298 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
3299 * around this by increasing the timeout now that we know that
3300 * the station has received the frame.
3302 int timeout_ms = eapol_key_timeout_subseq;
3303 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
3304 "timeout by %u ms because of acknowledged frame",
3306 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
3307 eloop_register_timeout(timeout_ms / 1000,
3308 (timeout_ms % 1000) * 1000,
3309 wpa_send_eapol_timeout, wpa_auth, sm);
3314 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
3318 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
3322 int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
3326 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
3331 int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
3333 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
3335 os_memcpy(addr, sm->ip_addr, 4);
3338 #endif /* CONFIG_P2P */