]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/wpa_auth.c
c2cbbe1619603fb820d4fbe3bbf848fc09a483da
[thirdparty/hostap.git] / src / ap / wpa_auth.c
1 /*
2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
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"
29 #include "wpa_auth.h"
30 #include "pmksa_cache_auth.h"
31 #include "wpa_auth_i.h"
32 #include "wpa_auth_ie.h"
33
34 #define STATE_MACHINE_DATA struct wpa_state_machine
35 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
36 #define STATE_MACHINE_ADDR sm->addr
37
38
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);
43 #ifdef CONFIG_FILS
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,
59 struct wpa_ptk *ptk);
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);
67
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 */
72
73 /* TODO: make these configurable */
74 static const int dot11RSNAConfigPMKLifetime = 43200;
75 static const int dot11RSNAConfigPMKReauthThreshold = 70;
76 static const int dot11RSNAConfigSATimeout = 60;
77
78
79 static inline int wpa_auth_mic_failure_report(
80 struct wpa_authenticator *wpa_auth, const u8 *addr)
81 {
82 if (wpa_auth->cb->mic_failure_report)
83 return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr);
84 return 0;
85 }
86
87
88 static inline void wpa_auth_psk_failure_report(
89 struct wpa_authenticator *wpa_auth, const u8 *addr)
90 {
91 if (wpa_auth->cb->psk_failure_report)
92 wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr);
93 }
94
95
96 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
97 const u8 *addr, wpa_eapol_variable var,
98 int value)
99 {
100 if (wpa_auth->cb->set_eapol)
101 wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value);
102 }
103
104
105 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
106 const u8 *addr, wpa_eapol_variable var)
107 {
108 if (wpa_auth->cb->get_eapol == NULL)
109 return -1;
110 return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var);
111 }
112
113
114 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
115 const u8 *addr,
116 const u8 *p2p_dev_addr,
117 const u8 *prev_psk, size_t *psk_len)
118 {
119 if (wpa_auth->cb->get_psk == NULL)
120 return NULL;
121 return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
122 prev_psk, psk_len);
123 }
124
125
126 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
127 const u8 *addr, u8 *msk, size_t *len)
128 {
129 if (wpa_auth->cb->get_msk == NULL)
130 return -1;
131 return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
132 }
133
134
135 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
136 int vlan_id,
137 enum wpa_alg alg, const u8 *addr, int idx,
138 u8 *key, size_t key_len)
139 {
140 if (wpa_auth->cb->set_key == NULL)
141 return -1;
142 return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
143 key, key_len);
144 }
145
146
147 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
148 const u8 *addr, int idx, u8 *seq)
149 {
150 if (wpa_auth->cb->get_seqnum == NULL)
151 return -1;
152 return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
153 }
154
155
156 static inline int
157 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
158 const u8 *data, size_t data_len, int encrypt)
159 {
160 if (wpa_auth->cb->send_eapol == NULL)
161 return -1;
162 return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
163 encrypt);
164 }
165
166
167 #ifdef CONFIG_MESH
168 static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
169 const u8 *addr)
170 {
171 if (wpa_auth->cb->start_ampe == NULL)
172 return -1;
173 return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
174 }
175 #endif /* CONFIG_MESH */
176
177
178 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
179 int (*cb)(struct wpa_state_machine *sm, void *ctx),
180 void *cb_ctx)
181 {
182 if (wpa_auth->cb->for_each_sta == NULL)
183 return 0;
184 return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
185 }
186
187
188 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
189 int (*cb)(struct wpa_authenticator *a, void *ctx),
190 void *cb_ctx)
191 {
192 if (wpa_auth->cb->for_each_auth == NULL)
193 return 0;
194 return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
195 }
196
197
198 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
199 logger_level level, const char *txt)
200 {
201 if (wpa_auth->cb->logger == NULL)
202 return;
203 wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
204 }
205
206
207 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
208 logger_level level, const char *fmt, ...)
209 {
210 char *format;
211 int maxlen;
212 va_list ap;
213
214 if (wpa_auth->cb->logger == NULL)
215 return;
216
217 maxlen = os_strlen(fmt) + 100;
218 format = os_malloc(maxlen);
219 if (!format)
220 return;
221
222 va_start(ap, fmt);
223 vsnprintf(format, maxlen, fmt, ap);
224 va_end(ap);
225
226 wpa_auth_logger(wpa_auth, addr, level, format);
227
228 os_free(format);
229 }
230
231
232 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
233 const u8 *addr, u16 reason)
234 {
235 if (wpa_auth->cb->disconnect == NULL)
236 return;
237 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR " (reason %u)",
238 MAC2STR(addr), reason);
239 wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr, reason);
240 }
241
242
243 #ifdef CONFIG_OCV
244 static int wpa_channel_info(struct wpa_authenticator *wpa_auth,
245 struct wpa_channel_info *ci)
246 {
247 if (!wpa_auth->cb->channel_info)
248 return -1;
249 return wpa_auth->cb->channel_info(wpa_auth->cb_ctx, ci);
250 }
251 #endif /* CONFIG_OCV */
252
253
254 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
255 {
256 struct wpa_authenticator *wpa_auth = eloop_ctx;
257
258 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
259 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
260 "initialization.");
261 } else {
262 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
263 wpa_hexdump_key(MSG_DEBUG, "GMK",
264 wpa_auth->group->GMK, WPA_GMK_LEN);
265 }
266
267 if (wpa_auth->conf.wpa_gmk_rekey) {
268 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
269 wpa_rekey_gmk, wpa_auth, NULL);
270 }
271 }
272
273
274 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
275 {
276 struct wpa_authenticator *wpa_auth = eloop_ctx;
277 struct wpa_group *group, *next;
278
279 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
280 group = wpa_auth->group;
281 while (group) {
282 wpa_group_get(wpa_auth, group);
283
284 group->GTKReKey = TRUE;
285 do {
286 group->changed = FALSE;
287 wpa_group_sm_step(wpa_auth, group);
288 } while (group->changed);
289
290 next = group->next;
291 wpa_group_put(wpa_auth, group);
292 group = next;
293 }
294
295 if (wpa_auth->conf.wpa_group_rekey) {
296 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
297 0, wpa_rekey_gtk, wpa_auth, NULL);
298 }
299 }
300
301
302 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
303 {
304 struct wpa_authenticator *wpa_auth = eloop_ctx;
305 struct wpa_state_machine *sm = timeout_ctx;
306
307 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
308 wpa_request_new_ptk(sm);
309 wpa_sm_step(sm);
310 }
311
312
313 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
314 {
315 if (sm->pmksa == ctx)
316 sm->pmksa = NULL;
317 return 0;
318 }
319
320
321 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
322 void *ctx)
323 {
324 struct wpa_authenticator *wpa_auth = ctx;
325 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
326 }
327
328
329 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
330 struct wpa_group *group)
331 {
332 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
333 u8 rkey[32];
334 unsigned long ptr;
335
336 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
337 return -1;
338 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
339
340 /*
341 * Counter = PRF-256(Random number, "Init Counter",
342 * Local MAC Address || Time)
343 */
344 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
345 wpa_get_ntp_timestamp(buf + ETH_ALEN);
346 ptr = (unsigned long) group;
347 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
348 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
349 return -1;
350
351 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
352 group->Counter, WPA_NONCE_LEN) < 0)
353 return -1;
354 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
355 group->Counter, WPA_NONCE_LEN);
356
357 return 0;
358 }
359
360
361 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
362 int vlan_id, int delay_init)
363 {
364 struct wpa_group *group;
365
366 group = os_zalloc(sizeof(struct wpa_group));
367 if (group == NULL)
368 return NULL;
369
370 group->GTKAuthenticator = TRUE;
371 group->vlan_id = vlan_id;
372 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
373
374 if (random_pool_ready() != 1) {
375 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
376 "for secure operations - update keys later when "
377 "the first station connects");
378 }
379
380 /*
381 * Set initial GMK/Counter value here. The actual values that will be
382 * used in negotiations will be set once the first station tries to
383 * connect. This allows more time for collecting additional randomness
384 * on embedded devices.
385 */
386 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
387 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
388 "initialization.");
389 os_free(group);
390 return NULL;
391 }
392
393 group->GInit = TRUE;
394 if (delay_init) {
395 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
396 "until Beacon frames have been configured");
397 /* Initialization is completed in wpa_init_keys(). */
398 } else {
399 wpa_group_sm_step(wpa_auth, group);
400 group->GInit = FALSE;
401 wpa_group_sm_step(wpa_auth, group);
402 }
403
404 return group;
405 }
406
407
408 /**
409 * wpa_init - Initialize WPA authenticator
410 * @addr: Authenticator address
411 * @conf: Configuration for WPA authenticator
412 * @cb: Callback functions for WPA authenticator
413 * Returns: Pointer to WPA authenticator data or %NULL on failure
414 */
415 struct wpa_authenticator * wpa_init(const u8 *addr,
416 struct wpa_auth_config *conf,
417 const struct wpa_auth_callbacks *cb,
418 void *cb_ctx)
419 {
420 struct wpa_authenticator *wpa_auth;
421
422 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
423 if (wpa_auth == NULL)
424 return NULL;
425 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
426 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
427 wpa_auth->cb = cb;
428 wpa_auth->cb_ctx = cb_ctx;
429
430 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
431 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
432 os_free(wpa_auth);
433 return NULL;
434 }
435
436 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
437 if (wpa_auth->group == NULL) {
438 os_free(wpa_auth->wpa_ie);
439 os_free(wpa_auth);
440 return NULL;
441 }
442
443 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
444 wpa_auth);
445 if (wpa_auth->pmksa == NULL) {
446 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
447 os_free(wpa_auth->group);
448 os_free(wpa_auth->wpa_ie);
449 os_free(wpa_auth);
450 return NULL;
451 }
452
453 #ifdef CONFIG_IEEE80211R_AP
454 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
455 if (wpa_auth->ft_pmk_cache == NULL) {
456 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
457 os_free(wpa_auth->group);
458 os_free(wpa_auth->wpa_ie);
459 pmksa_cache_auth_deinit(wpa_auth->pmksa);
460 os_free(wpa_auth);
461 return NULL;
462 }
463 #endif /* CONFIG_IEEE80211R_AP */
464
465 if (wpa_auth->conf.wpa_gmk_rekey) {
466 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
467 wpa_rekey_gmk, wpa_auth, NULL);
468 }
469
470 if (wpa_auth->conf.wpa_group_rekey) {
471 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
472 wpa_rekey_gtk, wpa_auth, NULL);
473 }
474
475 #ifdef CONFIG_P2P
476 if (WPA_GET_BE32(conf->ip_addr_start)) {
477 int count = WPA_GET_BE32(conf->ip_addr_end) -
478 WPA_GET_BE32(conf->ip_addr_start) + 1;
479 if (count > 1000)
480 count = 1000;
481 if (count > 0)
482 wpa_auth->ip_pool = bitfield_alloc(count);
483 }
484 #endif /* CONFIG_P2P */
485
486 return wpa_auth;
487 }
488
489
490 int wpa_init_keys(struct wpa_authenticator *wpa_auth)
491 {
492 struct wpa_group *group = wpa_auth->group;
493
494 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
495 "keys");
496 wpa_group_sm_step(wpa_auth, group);
497 group->GInit = FALSE;
498 wpa_group_sm_step(wpa_auth, group);
499 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
500 return -1;
501 return 0;
502 }
503
504
505 /**
506 * wpa_deinit - Deinitialize WPA authenticator
507 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
508 */
509 void wpa_deinit(struct wpa_authenticator *wpa_auth)
510 {
511 struct wpa_group *group, *prev;
512
513 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
514 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
515
516 pmksa_cache_auth_deinit(wpa_auth->pmksa);
517
518 #ifdef CONFIG_IEEE80211R_AP
519 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
520 wpa_auth->ft_pmk_cache = NULL;
521 wpa_ft_deinit(wpa_auth);
522 #endif /* CONFIG_IEEE80211R_AP */
523
524 #ifdef CONFIG_P2P
525 bitfield_free(wpa_auth->ip_pool);
526 #endif /* CONFIG_P2P */
527
528
529 os_free(wpa_auth->wpa_ie);
530
531 group = wpa_auth->group;
532 while (group) {
533 prev = group;
534 group = group->next;
535 os_free(prev);
536 }
537
538 os_free(wpa_auth);
539 }
540
541
542 /**
543 * wpa_reconfig - Update WPA authenticator configuration
544 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
545 * @conf: Configuration for WPA authenticator
546 */
547 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
548 struct wpa_auth_config *conf)
549 {
550 struct wpa_group *group;
551 if (wpa_auth == NULL)
552 return 0;
553
554 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
555 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
556 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
557 return -1;
558 }
559
560 /*
561 * Reinitialize GTK to make sure it is suitable for the new
562 * configuration.
563 */
564 group = wpa_auth->group;
565 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
566 group->GInit = TRUE;
567 wpa_group_sm_step(wpa_auth, group);
568 group->GInit = FALSE;
569 wpa_group_sm_step(wpa_auth, group);
570
571 return 0;
572 }
573
574
575 struct wpa_state_machine *
576 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
577 const u8 *p2p_dev_addr)
578 {
579 struct wpa_state_machine *sm;
580
581 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
582 return NULL;
583
584 sm = os_zalloc(sizeof(struct wpa_state_machine));
585 if (sm == NULL)
586 return NULL;
587 os_memcpy(sm->addr, addr, ETH_ALEN);
588 if (p2p_dev_addr)
589 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
590
591 sm->wpa_auth = wpa_auth;
592 sm->group = wpa_auth->group;
593 wpa_group_get(sm->wpa_auth, sm->group);
594
595 return sm;
596 }
597
598
599 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
600 struct wpa_state_machine *sm)
601 {
602 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
603 return -1;
604
605 #ifdef CONFIG_IEEE80211R_AP
606 if (sm->ft_completed) {
607 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
608 "FT authentication already completed - do not "
609 "start 4-way handshake");
610 /* Go to PTKINITDONE state to allow GTK rekeying */
611 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
612 sm->Pair = TRUE;
613 return 0;
614 }
615 #endif /* CONFIG_IEEE80211R_AP */
616
617 #ifdef CONFIG_FILS
618 if (sm->fils_completed) {
619 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
620 "FILS authentication already completed - do not start 4-way handshake");
621 /* Go to PTKINITDONE state to allow GTK rekeying */
622 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
623 sm->Pair = TRUE;
624 return 0;
625 }
626 #endif /* CONFIG_FILS */
627
628 if (sm->started) {
629 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
630 sm->ReAuthenticationRequest = TRUE;
631 return wpa_sm_step(sm);
632 }
633
634 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
635 "start authentication");
636 sm->started = 1;
637
638 sm->Init = TRUE;
639 if (wpa_sm_step(sm) == 1)
640 return 1; /* should not really happen */
641 sm->Init = FALSE;
642 sm->AuthenticationRequest = TRUE;
643 return wpa_sm_step(sm);
644 }
645
646
647 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
648 {
649 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
650 * reassociates back to the same AP while the previous entry for the
651 * STA has not yet been removed. */
652 if (sm == NULL)
653 return;
654
655 sm->wpa_key_mgmt = 0;
656 }
657
658
659 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
660 {
661 #ifdef CONFIG_P2P
662 if (WPA_GET_BE32(sm->ip_addr)) {
663 u32 start;
664 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
665 "address %u.%u.%u.%u from " MACSTR,
666 sm->ip_addr[0], sm->ip_addr[1],
667 sm->ip_addr[2], sm->ip_addr[3],
668 MAC2STR(sm->addr));
669 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
670 bitfield_clear(sm->wpa_auth->ip_pool,
671 WPA_GET_BE32(sm->ip_addr) - start);
672 }
673 #endif /* CONFIG_P2P */
674 if (sm->GUpdateStationKeys) {
675 sm->group->GKeyDoneStations--;
676 sm->GUpdateStationKeys = FALSE;
677 }
678 #ifdef CONFIG_IEEE80211R_AP
679 os_free(sm->assoc_resp_ftie);
680 wpabuf_free(sm->ft_pending_req_ies);
681 #endif /* CONFIG_IEEE80211R_AP */
682 os_free(sm->last_rx_eapol_key);
683 os_free(sm->wpa_ie);
684 wpa_group_put(sm->wpa_auth, sm->group);
685 os_free(sm);
686 }
687
688
689 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
690 {
691 if (sm == NULL)
692 return;
693
694 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
695 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
696 "strict rekeying - force GTK rekey since STA "
697 "is leaving");
698 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk,
699 sm->wpa_auth, NULL) == -1)
700 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
701 NULL);
702 }
703
704 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
705 sm->pending_1_of_4_timeout = 0;
706 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
707 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
708 #ifdef CONFIG_IEEE80211R_AP
709 wpa_ft_sta_deinit(sm);
710 #endif /* CONFIG_IEEE80211R_AP */
711 if (sm->in_step_loop) {
712 /* Must not free state machine while wpa_sm_step() is running.
713 * Freeing will be completed in the end of wpa_sm_step(). */
714 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
715 "machine deinit for " MACSTR, MAC2STR(sm->addr));
716 sm->pending_deinit = 1;
717 } else
718 wpa_free_sta_sm(sm);
719 }
720
721
722 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
723 {
724 if (sm == NULL)
725 return;
726
727 sm->PTKRequest = TRUE;
728 sm->PTK_valid = 0;
729 }
730
731
732 static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
733 const u8 *replay_counter)
734 {
735 int i;
736 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
737 if (!ctr[i].valid)
738 break;
739 if (os_memcmp(replay_counter, ctr[i].counter,
740 WPA_REPLAY_COUNTER_LEN) == 0)
741 return 1;
742 }
743 return 0;
744 }
745
746
747 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
748 const u8 *replay_counter)
749 {
750 int i;
751 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
752 if (ctr[i].valid &&
753 (replay_counter == NULL ||
754 os_memcmp(replay_counter, ctr[i].counter,
755 WPA_REPLAY_COUNTER_LEN) == 0))
756 ctr[i].valid = FALSE;
757 }
758 }
759
760
761 #ifdef CONFIG_IEEE80211R_AP
762 static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
763 struct wpa_state_machine *sm,
764 struct wpa_eapol_ie_parse *kde)
765 {
766 struct wpa_ie_data ie;
767 struct rsn_mdie *mdie;
768
769 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
770 ie.num_pmkid != 1 || ie.pmkid == NULL) {
771 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
772 "FT 4-way handshake message 2/4");
773 return -1;
774 }
775
776 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
777 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
778 sm->sup_pmk_r1_name, PMKID_LEN);
779
780 if (!kde->mdie || !kde->ftie) {
781 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
782 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
783 return -1;
784 }
785
786 mdie = (struct rsn_mdie *) (kde->mdie + 2);
787 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
788 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
789 MOBILITY_DOMAIN_ID_LEN) != 0) {
790 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
791 return -1;
792 }
793
794 if (sm->assoc_resp_ftie &&
795 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
796 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
797 2 + sm->assoc_resp_ftie[1]) != 0)) {
798 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
799 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
800 kde->ftie, kde->ftie_len);
801 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
802 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
803 return -1;
804 }
805
806 return 0;
807 }
808 #endif /* CONFIG_IEEE80211R_AP */
809
810
811 static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
812 struct wpa_state_machine *sm, int group)
813 {
814 /* Supplicant reported a Michael MIC error */
815 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
816 "received EAPOL-Key Error Request "
817 "(STA detected Michael MIC failure (group=%d))",
818 group);
819
820 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
821 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
822 "ignore Michael MIC failure report since "
823 "group cipher is not TKIP");
824 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
825 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
826 "ignore Michael MIC failure report since "
827 "pairwise cipher is not TKIP");
828 } else {
829 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
830 return 1; /* STA entry was removed */
831 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
832 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
833 }
834
835 /*
836 * Error report is not a request for a new key handshake, but since
837 * Authenticator may do it, let's change the keys now anyway.
838 */
839 wpa_request_new_ptk(sm);
840 return 0;
841 }
842
843
844 static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
845 size_t data_len)
846 {
847 struct wpa_ptk PTK;
848 int ok = 0;
849 const u8 *pmk = NULL;
850 size_t pmk_len;
851
852 os_memset(&PTK, 0, sizeof(PTK));
853 for (;;) {
854 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
855 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
856 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
857 sm->p2p_dev_addr, pmk, &pmk_len);
858 if (pmk == NULL)
859 break;
860 #ifdef CONFIG_IEEE80211R_AP
861 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
862 os_memcpy(sm->xxkey, pmk, pmk_len);
863 sm->xxkey_len = pmk_len;
864 }
865 #endif /* CONFIG_IEEE80211R_AP */
866 } else {
867 pmk = sm->PMK;
868 pmk_len = sm->pmk_len;
869 }
870
871 if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK) < 0)
872 break;
873
874 if (wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
875 data, data_len) == 0) {
876 ok = 1;
877 break;
878 }
879
880 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
881 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
882 break;
883 }
884
885 if (!ok) {
886 wpa_printf(MSG_DEBUG,
887 "WPA: Earlier SNonce did not result in matching MIC");
888 return -1;
889 }
890
891 wpa_printf(MSG_DEBUG,
892 "WPA: Earlier SNonce resulted in matching MIC");
893 sm->alt_snonce_valid = 0;
894 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
895 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
896 sm->PTK_valid = TRUE;
897
898 return 0;
899 }
900
901
902 void wpa_receive(struct wpa_authenticator *wpa_auth,
903 struct wpa_state_machine *sm,
904 u8 *data, size_t data_len)
905 {
906 struct ieee802_1x_hdr *hdr;
907 struct wpa_eapol_key *key;
908 u16 key_info, key_data_length;
909 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST } msg;
910 char *msgtxt;
911 struct wpa_eapol_ie_parse kde;
912 const u8 *key_data;
913 size_t keyhdrlen, mic_len;
914 u8 *mic;
915
916 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
917 return;
918 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
919
920 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
921 keyhdrlen = sizeof(*key) + mic_len + 2;
922
923 if (data_len < sizeof(*hdr) + keyhdrlen) {
924 wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
925 return;
926 }
927
928 hdr = (struct ieee802_1x_hdr *) data;
929 key = (struct wpa_eapol_key *) (hdr + 1);
930 mic = (u8 *) (key + 1);
931 key_info = WPA_GET_BE16(key->key_info);
932 key_data = mic + mic_len + 2;
933 key_data_length = WPA_GET_BE16(mic + mic_len);
934 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
935 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
936 MAC2STR(sm->addr), key_info, key->type,
937 (unsigned int) mic_len, key_data_length);
938 wpa_hexdump(MSG_MSGDUMP,
939 "WPA: EAPOL-Key header (ending before Key MIC)",
940 key, sizeof(*key));
941 wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
942 mic, mic_len);
943 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
944 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
945 "key_data overflow (%d > %lu)",
946 key_data_length,
947 (unsigned long) (data_len - sizeof(*hdr) -
948 keyhdrlen));
949 return;
950 }
951
952 if (sm->wpa == WPA_VERSION_WPA2) {
953 if (key->type == EAPOL_KEY_TYPE_WPA) {
954 /*
955 * Some deployed station implementations seem to send
956 * msg 4/4 with incorrect type value in WPA2 mode.
957 */
958 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
959 "with unexpected WPA type in RSN mode");
960 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
961 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
962 "unexpected type %d in RSN mode",
963 key->type);
964 return;
965 }
966 } else {
967 if (key->type != EAPOL_KEY_TYPE_WPA) {
968 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
969 "unexpected type %d in WPA mode",
970 key->type);
971 return;
972 }
973 }
974
975 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
976 WPA_NONCE_LEN);
977 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
978 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
979
980 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
981 * are set */
982
983 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
984 wpa_printf(MSG_DEBUG, "WPA: Ignore SMK message");
985 return;
986 }
987
988 if (key_info & WPA_KEY_INFO_REQUEST) {
989 msg = REQUEST;
990 msgtxt = "Request";
991 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
992 msg = GROUP_2;
993 msgtxt = "2/2 Group";
994 } else if (key_data_length == 0 ||
995 (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
996 key_data_length == AES_BLOCK_SIZE)) {
997 msg = PAIRWISE_4;
998 msgtxt = "4/4 Pairwise";
999 } else {
1000 msg = PAIRWISE_2;
1001 msgtxt = "2/4 Pairwise";
1002 }
1003
1004 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
1005 msg == GROUP_2) {
1006 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1007 if (sm->pairwise == WPA_CIPHER_CCMP ||
1008 sm->pairwise == WPA_CIPHER_GCMP) {
1009 if (wpa_use_cmac(sm->wpa_key_mgmt) &&
1010 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1011 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1012 wpa_auth_logger(wpa_auth, sm->addr,
1013 LOGGER_WARNING,
1014 "advertised support for "
1015 "AES-128-CMAC, but did not "
1016 "use it");
1017 return;
1018 }
1019
1020 if (!wpa_use_cmac(sm->wpa_key_mgmt) &&
1021 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1022 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1023 wpa_auth_logger(wpa_auth, sm->addr,
1024 LOGGER_WARNING,
1025 "did not use HMAC-SHA1-AES "
1026 "with CCMP/GCMP");
1027 return;
1028 }
1029 }
1030
1031 if (wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1032 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1033 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1034 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1035 return;
1036 }
1037 }
1038
1039 if (key_info & WPA_KEY_INFO_REQUEST) {
1040 if (sm->req_replay_counter_used &&
1041 os_memcmp(key->replay_counter, sm->req_replay_counter,
1042 WPA_REPLAY_COUNTER_LEN) <= 0) {
1043 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1044 "received EAPOL-Key request with "
1045 "replayed counter");
1046 return;
1047 }
1048 }
1049
1050 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
1051 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
1052 int i;
1053
1054 if (msg == PAIRWISE_2 &&
1055 wpa_replay_counter_valid(sm->prev_key_replay,
1056 key->replay_counter) &&
1057 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1058 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1059 {
1060 /*
1061 * Some supplicant implementations (e.g., Windows XP
1062 * WZC) update SNonce for each EAPOL-Key 2/4. This
1063 * breaks the workaround on accepting any of the
1064 * pending requests, so allow the SNonce to be updated
1065 * even if we have already sent out EAPOL-Key 3/4.
1066 */
1067 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1068 "Process SNonce update from STA "
1069 "based on retransmitted EAPOL-Key "
1070 "1/4");
1071 sm->update_snonce = 1;
1072 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1073 sm->alt_snonce_valid = TRUE;
1074 os_memcpy(sm->alt_replay_counter,
1075 sm->key_replay[0].counter,
1076 WPA_REPLAY_COUNTER_LEN);
1077 goto continue_processing;
1078 }
1079
1080 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1081 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1082 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1083 WPA_REPLAY_COUNTER_LEN) == 0) {
1084 /*
1085 * Supplicant may still be using the old SNonce since
1086 * there was two EAPOL-Key 2/4 messages and they had
1087 * different SNonce values.
1088 */
1089 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1090 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1091 goto continue_processing;
1092 }
1093
1094 if (msg == PAIRWISE_2 &&
1095 wpa_replay_counter_valid(sm->prev_key_replay,
1096 key->replay_counter) &&
1097 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1098 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1099 "ignore retransmitted EAPOL-Key %s - "
1100 "SNonce did not change", msgtxt);
1101 } else {
1102 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1103 "received EAPOL-Key %s with "
1104 "unexpected replay counter", msgtxt);
1105 }
1106 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1107 if (!sm->key_replay[i].valid)
1108 break;
1109 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1110 sm->key_replay[i].counter,
1111 WPA_REPLAY_COUNTER_LEN);
1112 }
1113 wpa_hexdump(MSG_DEBUG, "received replay counter",
1114 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1115 return;
1116 }
1117
1118 continue_processing:
1119 #ifdef CONFIG_FILS
1120 if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
1121 !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1122 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1123 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1124 return;
1125 }
1126 #endif /* CONFIG_FILS */
1127
1128 switch (msg) {
1129 case PAIRWISE_2:
1130 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
1131 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1132 (!sm->update_snonce ||
1133 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
1134 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1135 "received EAPOL-Key msg 2/4 in "
1136 "invalid state (%d) - dropped",
1137 sm->wpa_ptk_state);
1138 return;
1139 }
1140 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1141 if (sm->group->reject_4way_hs_for_entropy) {
1142 /*
1143 * The system did not have enough entropy to generate
1144 * strong random numbers. Reject the first 4-way
1145 * handshake(s) and collect some entropy based on the
1146 * information from it. Once enough entropy is
1147 * available, the next atempt will trigger GMK/Key
1148 * Counter update and the station will be allowed to
1149 * continue.
1150 */
1151 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1152 "collect more entropy for random number "
1153 "generation");
1154 random_mark_pool_ready();
1155 wpa_sta_disconnect(wpa_auth, sm->addr,
1156 WLAN_REASON_PREV_AUTH_NOT_VALID);
1157 return;
1158 }
1159 break;
1160 case PAIRWISE_4:
1161 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1162 !sm->PTK_valid) {
1163 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1164 "received EAPOL-Key msg 4/4 in "
1165 "invalid state (%d) - dropped",
1166 sm->wpa_ptk_state);
1167 return;
1168 }
1169 break;
1170 case GROUP_2:
1171 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1172 || !sm->PTK_valid) {
1173 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1174 "received EAPOL-Key msg 2/2 in "
1175 "invalid state (%d) - dropped",
1176 sm->wpa_ptk_group_state);
1177 return;
1178 }
1179 break;
1180 case REQUEST:
1181 break;
1182 }
1183
1184 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1185 "received EAPOL-Key frame (%s)", msgtxt);
1186
1187 if (key_info & WPA_KEY_INFO_ACK) {
1188 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1189 "received invalid EAPOL-Key: Key Ack set");
1190 return;
1191 }
1192
1193 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1194 !(key_info & WPA_KEY_INFO_MIC)) {
1195 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1196 "received invalid EAPOL-Key: Key MIC not set");
1197 return;
1198 }
1199
1200 #ifdef CONFIG_FILS
1201 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1202 (key_info & WPA_KEY_INFO_MIC)) {
1203 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1204 "received invalid EAPOL-Key: Key MIC set");
1205 return;
1206 }
1207 #endif /* CONFIG_FILS */
1208
1209 sm->MICVerified = FALSE;
1210 if (sm->PTK_valid && !sm->update_snonce) {
1211 if (mic_len &&
1212 wpa_verify_key_mic(sm->wpa_key_mgmt, sm->pmk_len, &sm->PTK,
1213 data, data_len) &&
1214 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1215 wpa_try_alt_snonce(sm, data, data_len))) {
1216 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1217 "received EAPOL-Key with invalid MIC");
1218 return;
1219 }
1220 #ifdef CONFIG_FILS
1221 if (!mic_len &&
1222 wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
1223 &key_data_length) < 0) {
1224 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1225 "received EAPOL-Key with invalid MIC");
1226 return;
1227 }
1228 #endif /* CONFIG_FILS */
1229 sm->MICVerified = TRUE;
1230 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1231 sm->pending_1_of_4_timeout = 0;
1232 }
1233
1234 if (key_info & WPA_KEY_INFO_REQUEST) {
1235 if (sm->MICVerified) {
1236 sm->req_replay_counter_used = 1;
1237 os_memcpy(sm->req_replay_counter, key->replay_counter,
1238 WPA_REPLAY_COUNTER_LEN);
1239 } else {
1240 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1241 "received EAPOL-Key request with "
1242 "invalid MIC");
1243 return;
1244 }
1245
1246 /*
1247 * TODO: should decrypt key data field if encryption was used;
1248 * even though MAC address KDE is not normally encrypted,
1249 * supplicant is allowed to encrypt it.
1250 */
1251 if (key_info & WPA_KEY_INFO_ERROR) {
1252 if (wpa_receive_error_report(
1253 wpa_auth, sm,
1254 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1255 return; /* STA entry was removed */
1256 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1257 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1258 "received EAPOL-Key Request for new "
1259 "4-Way Handshake");
1260 wpa_request_new_ptk(sm);
1261 } else if (key_data_length > 0 &&
1262 wpa_parse_kde_ies(key_data, key_data_length,
1263 &kde) == 0 &&
1264 kde.mac_addr) {
1265 } else {
1266 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1267 "received EAPOL-Key Request for GTK "
1268 "rekeying");
1269 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1270 wpa_rekey_gtk(wpa_auth, NULL);
1271 }
1272 } else {
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);
1276
1277 if (msg == PAIRWISE_2) {
1278 /*
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.
1284 */
1285 os_memcpy(sm->prev_key_replay, sm->key_replay,
1286 sizeof(sm->key_replay));
1287 } else {
1288 os_memset(sm->prev_key_replay, 0,
1289 sizeof(sm->prev_key_replay));
1290 }
1291
1292 /*
1293 * Make sure old valid counters are not accepted anymore and
1294 * do not get copied again.
1295 */
1296 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
1297 }
1298
1299 os_free(sm->last_rx_eapol_key);
1300 sm->last_rx_eapol_key = os_memdup(data, data_len);
1301 if (sm->last_rx_eapol_key == NULL)
1302 return;
1303 sm->last_rx_eapol_key_len = data_len;
1304
1305 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
1306 sm->EAPOLKeyReceived = TRUE;
1307 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1308 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1309 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1310 wpa_sm_step(sm);
1311 }
1312
1313
1314 static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1315 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1316 {
1317 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + WPA_GTK_MAX_LEN];
1318 u8 *pos;
1319 int ret = 0;
1320
1321 /* GTK = PRF-X(GMK, "Group key expansion",
1322 * AA || GNonce || Time || random data)
1323 * The example described in the IEEE 802.11 standard uses only AA and
1324 * GNonce as inputs here. Add some more entropy since this derivation
1325 * is done only at the Authenticator and as such, does not need to be
1326 * exactly same.
1327 */
1328 os_memset(data, 0, sizeof(data));
1329 os_memcpy(data, addr, ETH_ALEN);
1330 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1331 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1332 wpa_get_ntp_timestamp(pos);
1333 pos += 8;
1334 if (random_get_bytes(pos, gtk_len) < 0)
1335 ret = -1;
1336
1337 #ifdef CONFIG_SHA384
1338 if (sha384_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1339 gtk, gtk_len) < 0)
1340 ret = -1;
1341 #else /* CONFIG_SHA384 */
1342 #ifdef CONFIG_SHA256
1343 if (sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1344 gtk, gtk_len) < 0)
1345 ret = -1;
1346 #else /* CONFIG_SHA256 */
1347 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1348 gtk, gtk_len) < 0)
1349 ret = -1;
1350 #endif /* CONFIG_SHA256 */
1351 #endif /* CONFIG_SHA384 */
1352
1353 return ret;
1354 }
1355
1356
1357 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1358 {
1359 struct wpa_authenticator *wpa_auth = eloop_ctx;
1360 struct wpa_state_machine *sm = timeout_ctx;
1361
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;
1365 wpa_sm_step(sm);
1366 }
1367
1368
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)
1374 {
1375 struct ieee802_1x_hdr *hdr;
1376 struct wpa_eapol_key *key;
1377 size_t len, mic_len, keyhdrlen;
1378 int alg;
1379 int key_data_len, pad_len = 0;
1380 u8 *buf, *pos;
1381 int version, pairwise;
1382 int i;
1383 u8 *key_mic, *key_data;
1384
1385 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
1386 keyhdrlen = sizeof(*key) + mic_len + 2;
1387
1388 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
1389
1390 if (force_version)
1391 version = force_version;
1392 else if (wpa_use_akm_defined(sm->wpa_key_mgmt))
1393 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
1394 else if (wpa_use_cmac(sm->wpa_key_mgmt))
1395 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
1396 else if (sm->pairwise != WPA_CIPHER_TKIP)
1397 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1398 else
1399 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1400
1401 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1402
1403 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1404 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1405 "encr=%d)",
1406 version,
1407 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1408 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1409 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1410 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1411 pairwise, (unsigned long) kde_len, keyidx, encr);
1412
1413 key_data_len = kde_len;
1414
1415 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1416 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
1417 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1418 pad_len = key_data_len % 8;
1419 if (pad_len)
1420 pad_len = 8 - pad_len;
1421 key_data_len += pad_len + 8;
1422 }
1423
1424 len += key_data_len;
1425 if (!mic_len && encr)
1426 len += AES_BLOCK_SIZE;
1427
1428 hdr = os_zalloc(len);
1429 if (hdr == NULL)
1430 return;
1431 hdr->version = wpa_auth->conf.eapol_version;
1432 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1433 hdr->length = host_to_be16(len - sizeof(*hdr));
1434 key = (struct wpa_eapol_key *) (hdr + 1);
1435 key_mic = (u8 *) (key + 1);
1436 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
1437
1438 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1439 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1440 key_info |= version;
1441 if (encr && sm->wpa == WPA_VERSION_WPA2)
1442 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1443 if (sm->wpa != WPA_VERSION_WPA2)
1444 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1445 WPA_PUT_BE16(key->key_info, key_info);
1446
1447 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1448 if (sm->wpa == WPA_VERSION_WPA2 && !pairwise)
1449 WPA_PUT_BE16(key->key_length, 0);
1450 else
1451 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
1452
1453 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1454 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1455 os_memcpy(sm->key_replay[i].counter,
1456 sm->key_replay[i - 1].counter,
1457 WPA_REPLAY_COUNTER_LEN);
1458 }
1459 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1460 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1461 WPA_REPLAY_COUNTER_LEN);
1462 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1463 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1464 sm->key_replay[0].valid = TRUE;
1465
1466 if (nonce)
1467 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1468
1469 if (key_rsc)
1470 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1471
1472 if (kde && !encr) {
1473 os_memcpy(key_data, kde, kde_len);
1474 WPA_PUT_BE16(key_mic + mic_len, kde_len);
1475 #ifdef CONFIG_FILS
1476 } else if (!mic_len && kde) {
1477 const u8 *aad[1];
1478 size_t aad_len[1];
1479
1480 WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
1481 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1482 kde, kde_len);
1483
1484 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
1485 sm->PTK.kek, sm->PTK.kek_len);
1486 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1487 * to Key Data (exclusive). */
1488 aad[0] = (u8 *) hdr;
1489 aad_len[0] = key_mic + 2 - (u8 *) hdr;
1490 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
1491 1, aad, aad_len, key_mic + 2) < 0) {
1492 wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
1493 return;
1494 }
1495
1496 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
1497 key_mic + 2, AES_BLOCK_SIZE + kde_len);
1498 #endif /* CONFIG_FILS */
1499 } else if (encr && kde) {
1500 buf = os_zalloc(key_data_len);
1501 if (buf == NULL) {
1502 os_free(hdr);
1503 return;
1504 }
1505 pos = buf;
1506 os_memcpy(pos, kde, kde_len);
1507 pos += kde_len;
1508
1509 if (pad_len)
1510 *pos++ = 0xdd;
1511
1512 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1513 buf, key_data_len);
1514 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1515 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
1516 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1517 wpa_printf(MSG_DEBUG,
1518 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1519 (unsigned int) sm->PTK.kek_len);
1520 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1521 (key_data_len - 8) / 8, buf, key_data)) {
1522 os_free(hdr);
1523 os_free(buf);
1524 return;
1525 }
1526 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1527 #ifndef CONFIG_NO_RC4
1528 } else if (sm->PTK.kek_len == 16) {
1529 u8 ek[32];
1530
1531 wpa_printf(MSG_DEBUG,
1532 "WPA: Encrypt Key Data using RC4");
1533 os_memcpy(key->key_iv,
1534 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1535 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1536 os_memcpy(ek, key->key_iv, 16);
1537 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1538 os_memcpy(key_data, buf, key_data_len);
1539 rc4_skip(ek, 32, 256, key_data, key_data_len);
1540 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1541 #endif /* CONFIG_NO_RC4 */
1542 } else {
1543 os_free(hdr);
1544 os_free(buf);
1545 return;
1546 }
1547 os_free(buf);
1548 }
1549
1550 if (key_info & WPA_KEY_INFO_MIC) {
1551 if (!sm->PTK_valid || !mic_len) {
1552 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1553 "PTK not valid when sending EAPOL-Key "
1554 "frame");
1555 os_free(hdr);
1556 return;
1557 }
1558
1559 if (wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1560 sm->wpa_key_mgmt, version,
1561 (u8 *) hdr, len, key_mic) < 0) {
1562 os_free(hdr);
1563 return;
1564 }
1565 #ifdef CONFIG_TESTING_OPTIONS
1566 if (!pairwise &&
1567 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
1568 drand48() <
1569 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1570 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1571 "Corrupting group EAPOL-Key Key MIC");
1572 key_mic[0]++;
1573 }
1574 #endif /* CONFIG_TESTING_OPTIONS */
1575 }
1576
1577 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1578 1);
1579 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1580 sm->pairwise_set);
1581 os_free(hdr);
1582 }
1583
1584
1585 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1586 struct wpa_state_machine *sm, int key_info,
1587 const u8 *key_rsc, const u8 *nonce,
1588 const u8 *kde, size_t kde_len,
1589 int keyidx, int encr)
1590 {
1591 int timeout_ms;
1592 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1593 u32 ctr;
1594
1595 if (sm == NULL)
1596 return;
1597
1598 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1599 keyidx, encr, 0);
1600
1601 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1602 if (ctr == 1 && wpa_auth->conf.tx_status)
1603 timeout_ms = pairwise ? eapol_key_timeout_first :
1604 eapol_key_timeout_first_group;
1605 else
1606 timeout_ms = eapol_key_timeout_subseq;
1607 if (wpa_auth->conf.wpa_disable_eapol_key_retries &&
1608 (!pairwise || (key_info & WPA_KEY_INFO_MIC)))
1609 timeout_ms = eapol_key_timeout_no_retrans;
1610 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1611 sm->pending_1_of_4_timeout = 1;
1612 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1613 "counter %u)", timeout_ms, ctr);
1614 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1615 wpa_send_eapol_timeout, wpa_auth, sm);
1616 }
1617
1618
1619 static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
1620 u8 *data, size_t data_len)
1621 {
1622 struct ieee802_1x_hdr *hdr;
1623 struct wpa_eapol_key *key;
1624 u16 key_info;
1625 int ret = 0;
1626 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
1627 size_t mic_len = wpa_mic_len(akmp, pmk_len);
1628
1629 if (data_len < sizeof(*hdr) + sizeof(*key))
1630 return -1;
1631
1632 hdr = (struct ieee802_1x_hdr *) data;
1633 key = (struct wpa_eapol_key *) (hdr + 1);
1634 mic_pos = (u8 *) (key + 1);
1635 key_info = WPA_GET_BE16(key->key_info);
1636 os_memcpy(mic, mic_pos, mic_len);
1637 os_memset(mic_pos, 0, mic_len);
1638 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1639 key_info & WPA_KEY_INFO_TYPE_MASK,
1640 data, data_len, mic_pos) ||
1641 os_memcmp_const(mic, mic_pos, mic_len) != 0)
1642 ret = -1;
1643 os_memcpy(mic_pos, mic, mic_len);
1644 return ret;
1645 }
1646
1647
1648 void wpa_remove_ptk(struct wpa_state_machine *sm)
1649 {
1650 sm->PTK_valid = FALSE;
1651 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1652 if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
1653 0))
1654 wpa_printf(MSG_DEBUG,
1655 "RSN: PTK removal from the driver failed");
1656 sm->pairwise_set = FALSE;
1657 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1658 }
1659
1660
1661 int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
1662 {
1663 int remove_ptk = 1;
1664
1665 if (sm == NULL)
1666 return -1;
1667
1668 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1669 "event %d notification", event);
1670
1671 switch (event) {
1672 case WPA_AUTH:
1673 #ifdef CONFIG_MESH
1674 /* PTKs are derived through AMPE */
1675 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1676 /* not mesh */
1677 break;
1678 }
1679 return 0;
1680 #endif /* CONFIG_MESH */
1681 case WPA_ASSOC:
1682 break;
1683 case WPA_DEAUTH:
1684 case WPA_DISASSOC:
1685 sm->DeauthenticationRequest = TRUE;
1686 break;
1687 case WPA_REAUTH:
1688 case WPA_REAUTH_EAPOL:
1689 if (!sm->started) {
1690 /*
1691 * When using WPS, we may end up here if the STA
1692 * manages to re-associate without the previous STA
1693 * entry getting removed. Consequently, we need to make
1694 * sure that the WPA state machines gets initialized
1695 * properly at this point.
1696 */
1697 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1698 "started - initialize now");
1699 sm->started = 1;
1700 sm->Init = TRUE;
1701 if (wpa_sm_step(sm) == 1)
1702 return 1; /* should not really happen */
1703 sm->Init = FALSE;
1704 sm->AuthenticationRequest = TRUE;
1705 break;
1706 }
1707 if (sm->GUpdateStationKeys) {
1708 /*
1709 * Reauthentication cancels the pending group key
1710 * update for this STA.
1711 */
1712 sm->group->GKeyDoneStations--;
1713 sm->GUpdateStationKeys = FALSE;
1714 sm->PtkGroupInit = TRUE;
1715 }
1716 sm->ReAuthenticationRequest = TRUE;
1717 break;
1718 case WPA_ASSOC_FT:
1719 #ifdef CONFIG_IEEE80211R_AP
1720 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1721 "after association");
1722 wpa_ft_install_ptk(sm);
1723
1724 /* Using FT protocol, not WPA auth state machine */
1725 sm->ft_completed = 1;
1726 return 0;
1727 #else /* CONFIG_IEEE80211R_AP */
1728 break;
1729 #endif /* CONFIG_IEEE80211R_AP */
1730 case WPA_ASSOC_FILS:
1731 #ifdef CONFIG_FILS
1732 wpa_printf(MSG_DEBUG,
1733 "FILS: TK configuration after association");
1734 fils_set_tk(sm);
1735 sm->fils_completed = 1;
1736 return 0;
1737 #else /* CONFIG_FILS */
1738 break;
1739 #endif /* CONFIG_FILS */
1740 case WPA_DRV_STA_REMOVED:
1741 sm->tk_already_set = FALSE;
1742 return 0;
1743 }
1744
1745 #ifdef CONFIG_IEEE80211R_AP
1746 sm->ft_completed = 0;
1747 #endif /* CONFIG_IEEE80211R_AP */
1748
1749 #ifdef CONFIG_IEEE80211W
1750 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1751 remove_ptk = 0;
1752 #endif /* CONFIG_IEEE80211W */
1753 #ifdef CONFIG_FILS
1754 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1755 (event == WPA_AUTH || event == WPA_ASSOC))
1756 remove_ptk = 0;
1757 #endif /* CONFIG_FILS */
1758
1759 if (remove_ptk) {
1760 sm->PTK_valid = FALSE;
1761 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1762
1763 if (event != WPA_REAUTH_EAPOL)
1764 wpa_remove_ptk(sm);
1765 }
1766
1767 if (sm->in_step_loop) {
1768 /*
1769 * wpa_sm_step() is already running - avoid recursive call to
1770 * it by making the existing loop process the new update.
1771 */
1772 sm->changed = TRUE;
1773 return 0;
1774 }
1775 return wpa_sm_step(sm);
1776 }
1777
1778
1779 SM_STATE(WPA_PTK, INITIALIZE)
1780 {
1781 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1782 if (sm->Init) {
1783 /* Init flag is not cleared here, so avoid busy
1784 * loop by claiming nothing changed. */
1785 sm->changed = FALSE;
1786 }
1787
1788 sm->keycount = 0;
1789 if (sm->GUpdateStationKeys)
1790 sm->group->GKeyDoneStations--;
1791 sm->GUpdateStationKeys = FALSE;
1792 if (sm->wpa == WPA_VERSION_WPA)
1793 sm->PInitAKeys = FALSE;
1794 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1795 * Local AA > Remote AA)) */) {
1796 sm->Pair = TRUE;
1797 }
1798 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1799 wpa_remove_ptk(sm);
1800 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1801 sm->TimeoutCtr = 0;
1802 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
1803 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
1804 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
1805 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1806 WPA_EAPOL_authorized, 0);
1807 }
1808 }
1809
1810
1811 SM_STATE(WPA_PTK, DISCONNECT)
1812 {
1813 u16 reason = sm->disconnect_reason;
1814
1815 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1816 sm->Disconnect = FALSE;
1817 sm->disconnect_reason = 0;
1818 if (!reason)
1819 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1820 wpa_sta_disconnect(sm->wpa_auth, sm->addr, reason);
1821 }
1822
1823
1824 SM_STATE(WPA_PTK, DISCONNECTED)
1825 {
1826 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1827 sm->DeauthenticationRequest = FALSE;
1828 }
1829
1830
1831 SM_STATE(WPA_PTK, AUTHENTICATION)
1832 {
1833 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1834 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1835 sm->PTK_valid = FALSE;
1836 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1837 1);
1838 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1839 sm->AuthenticationRequest = FALSE;
1840 }
1841
1842
1843 static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1844 struct wpa_group *group)
1845 {
1846 if (group->first_sta_seen)
1847 return;
1848 /*
1849 * System has run bit further than at the time hostapd was started
1850 * potentially very early during boot up. This provides better chances
1851 * of collecting more randomness on embedded systems. Re-initialize the
1852 * GMK and Counter here to improve their strength if there was not
1853 * enough entropy available immediately after system startup.
1854 */
1855 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1856 "station");
1857 if (random_pool_ready() != 1) {
1858 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1859 "to proceed - reject first 4-way handshake");
1860 group->reject_4way_hs_for_entropy = TRUE;
1861 } else {
1862 group->first_sta_seen = TRUE;
1863 group->reject_4way_hs_for_entropy = FALSE;
1864 }
1865
1866 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
1867 wpa_gtk_update(wpa_auth, group) < 0 ||
1868 wpa_group_config_group_keys(wpa_auth, group) < 0) {
1869 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
1870 group->first_sta_seen = FALSE;
1871 group->reject_4way_hs_for_entropy = TRUE;
1872 }
1873 }
1874
1875
1876 SM_STATE(WPA_PTK, AUTHENTICATION2)
1877 {
1878 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1879
1880 wpa_group_ensure_init(sm->wpa_auth, sm->group);
1881 sm->ReAuthenticationRequest = FALSE;
1882
1883 /*
1884 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1885 * ambiguous. The Authenticator state machine uses a counter that is
1886 * incremented by one for each 4-way handshake. However, the security
1887 * analysis of 4-way handshake points out that unpredictable nonces
1888 * help in preventing precomputation attacks. Instead of the state
1889 * machine definition, use an unpredictable nonce value here to provide
1890 * stronger protection against potential precomputation attacks.
1891 */
1892 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1893 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1894 "ANonce.");
1895 sm->Disconnect = TRUE;
1896 return;
1897 }
1898 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1899 WPA_NONCE_LEN);
1900 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1901 * logical place than INITIALIZE since AUTHENTICATION2 can be
1902 * re-entered on ReAuthenticationRequest without going through
1903 * INITIALIZE. */
1904 sm->TimeoutCtr = 0;
1905 }
1906
1907
1908 static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm)
1909 {
1910 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1911 wpa_printf(MSG_ERROR,
1912 "WPA: Failed to get random data for ANonce");
1913 sm->Disconnect = TRUE;
1914 return -1;
1915 }
1916 wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce,
1917 WPA_NONCE_LEN);
1918 sm->TimeoutCtr = 0;
1919 return 0;
1920 }
1921
1922
1923 SM_STATE(WPA_PTK, INITPMK)
1924 {
1925 u8 msk[2 * PMK_LEN];
1926 size_t len = 2 * PMK_LEN;
1927
1928 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1929 #ifdef CONFIG_IEEE80211R_AP
1930 sm->xxkey_len = 0;
1931 #endif /* CONFIG_IEEE80211R_AP */
1932 if (sm->pmksa) {
1933 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1934 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
1935 sm->pmk_len = sm->pmksa->pmk_len;
1936 #ifdef CONFIG_DPP
1937 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) {
1938 wpa_printf(MSG_DEBUG,
1939 "DPP: No PMKSA cache entry for STA - reject connection");
1940 sm->Disconnect = TRUE;
1941 sm->disconnect_reason = WLAN_REASON_INVALID_PMKID;
1942 return;
1943 #endif /* CONFIG_DPP */
1944 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1945 unsigned int pmk_len;
1946
1947 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
1948 pmk_len = PMK_LEN_SUITE_B_192;
1949 else
1950 pmk_len = PMK_LEN;
1951 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1952 "(MSK len=%lu PMK len=%u)", (unsigned long) len,
1953 pmk_len);
1954 if (len < pmk_len) {
1955 wpa_printf(MSG_DEBUG,
1956 "WPA: MSK not long enough (%u) to create PMK (%u)",
1957 (unsigned int) len, (unsigned int) pmk_len);
1958 sm->Disconnect = TRUE;
1959 return;
1960 }
1961 os_memcpy(sm->PMK, msk, pmk_len);
1962 sm->pmk_len = pmk_len;
1963 #ifdef CONFIG_IEEE80211R_AP
1964 if (len >= 2 * PMK_LEN) {
1965 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
1966 os_memcpy(sm->xxkey, msk, SHA384_MAC_LEN);
1967 sm->xxkey_len = SHA384_MAC_LEN;
1968 } else {
1969 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1970 sm->xxkey_len = PMK_LEN;
1971 }
1972 }
1973 #endif /* CONFIG_IEEE80211R_AP */
1974 } else {
1975 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
1976 sm->wpa_auth->cb->get_msk);
1977 sm->Disconnect = TRUE;
1978 return;
1979 }
1980 os_memset(msk, 0, sizeof(msk));
1981
1982 sm->req_replay_counter_used = 0;
1983 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1984 * will break reauthentication since EAPOL state machines may not be
1985 * get into AUTHENTICATING state that clears keyRun before WPA state
1986 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1987 * state and takes PMK from the previously used AAA Key. This will
1988 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1989 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1990 * be good workaround for this issue. */
1991 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1992 }
1993
1994
1995 SM_STATE(WPA_PTK, INITPSK)
1996 {
1997 const u8 *psk;
1998 size_t psk_len;
1999
2000 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
2001 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL,
2002 &psk_len);
2003 if (psk) {
2004 os_memcpy(sm->PMK, psk, psk_len);
2005 sm->pmk_len = psk_len;
2006 #ifdef CONFIG_IEEE80211R_AP
2007 os_memcpy(sm->xxkey, psk, PMK_LEN);
2008 sm->xxkey_len = PMK_LEN;
2009 #endif /* CONFIG_IEEE80211R_AP */
2010 }
2011 #ifdef CONFIG_SAE
2012 if (wpa_auth_uses_sae(sm) && sm->pmksa) {
2013 wpa_printf(MSG_DEBUG, "SAE: PMK from PMKSA cache");
2014 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2015 sm->pmk_len = sm->pmksa->pmk_len;
2016 }
2017 #endif /* CONFIG_SAE */
2018 sm->req_replay_counter_used = 0;
2019 }
2020
2021
2022 SM_STATE(WPA_PTK, PTKSTART)
2023 {
2024 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
2025 size_t pmkid_len = 0;
2026
2027 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
2028 sm->PTKRequest = FALSE;
2029 sm->TimeoutEvt = FALSE;
2030 sm->alt_snonce_valid = FALSE;
2031
2032 sm->TimeoutCtr++;
2033 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
2034 /* No point in sending the EAPOL-Key - we will disconnect
2035 * immediately following this. */
2036 return;
2037 }
2038
2039 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2040 "sending 1/4 msg of 4-Way Handshake");
2041 /*
2042 * For infrastructure BSS cases, it is better for the AP not to include
2043 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2044 * offline search for the passphrase/PSK without having to be able to
2045 * capture a 4-way handshake from a STA that has access to the network.
2046 *
2047 * For IBSS cases, addition of PMKID KDE could be considered even with
2048 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2049 * possible PSK for this STA. However, this should not be done unless
2050 * there is support for using that information on the supplicant side.
2051 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2052 * cases would also apply here, but at least in the IBSS case, this
2053 * would cover a potential real use case.
2054 */
2055 if (sm->wpa == WPA_VERSION_WPA2 &&
2056 (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) ||
2057 (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && sm->pmksa) ||
2058 wpa_key_mgmt_sae(sm->wpa_key_mgmt)) &&
2059 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
2060 pmkid = buf;
2061 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2062 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
2063 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2064 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
2065 if (sm->pmksa) {
2066 wpa_hexdump(MSG_DEBUG,
2067 "RSN: Message 1/4 PMKID from PMKSA entry",
2068 sm->pmksa->pmkid, PMKID_LEN);
2069 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2070 sm->pmksa->pmkid, PMKID_LEN);
2071 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
2072 /* No KCK available to derive PMKID */
2073 wpa_printf(MSG_DEBUG,
2074 "RSN: No KCK available to derive PMKID for message 1/4");
2075 pmkid = NULL;
2076 #ifdef CONFIG_SAE
2077 } else if (wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
2078 if (sm->pmkid_set) {
2079 wpa_hexdump(MSG_DEBUG,
2080 "RSN: Message 1/4 PMKID from SAE",
2081 sm->pmkid, PMKID_LEN);
2082 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2083 sm->pmkid, PMKID_LEN);
2084 } else {
2085 /* No PMKID available */
2086 wpa_printf(MSG_DEBUG,
2087 "RSN: No SAE PMKID available for message 1/4");
2088 pmkid = NULL;
2089 }
2090 #endif /* CONFIG_SAE */
2091 } else {
2092 /*
2093 * Calculate PMKID since no PMKSA cache entry was
2094 * available with pre-calculated PMKID.
2095 */
2096 rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
2097 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
2098 sm->wpa_key_mgmt);
2099 wpa_hexdump(MSG_DEBUG,
2100 "RSN: Message 1/4 PMKID derived from PMK",
2101 &pmkid[2 + RSN_SELECTOR_LEN], PMKID_LEN);
2102 }
2103 }
2104 wpa_send_eapol(sm->wpa_auth, sm,
2105 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
2106 sm->ANonce, pmkid, pmkid_len, 0, 0);
2107 }
2108
2109
2110 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
2111 const u8 *pmk, unsigned int pmk_len,
2112 struct wpa_ptk *ptk)
2113 {
2114 #ifdef CONFIG_IEEE80211R_AP
2115 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
2116 return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
2117 #endif /* CONFIG_IEEE80211R_AP */
2118
2119 return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
2120 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
2121 ptk, sm->wpa_key_mgmt, sm->pairwise);
2122 }
2123
2124
2125 #ifdef CONFIG_FILS
2126
2127 int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
2128 size_t pmk_len, const u8 *snonce, const u8 *anonce,
2129 const u8 *dhss, size_t dhss_len,
2130 struct wpabuf *g_sta, struct wpabuf *g_ap)
2131 {
2132 u8 ick[FILS_ICK_MAX_LEN];
2133 size_t ick_len;
2134 int res;
2135 u8 fils_ft[FILS_FT_MAX_LEN];
2136 size_t fils_ft_len = 0;
2137
2138 res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr,
2139 snonce, anonce, dhss, dhss_len,
2140 &sm->PTK, ick, &ick_len,
2141 sm->wpa_key_mgmt, sm->pairwise,
2142 fils_ft, &fils_ft_len);
2143 if (res < 0)
2144 return res;
2145 sm->PTK_valid = TRUE;
2146 sm->tk_already_set = FALSE;
2147
2148 #ifdef CONFIG_IEEE80211R_AP
2149 if (fils_ft_len) {
2150 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2151 struct wpa_auth_config *conf = &wpa_auth->conf;
2152 u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
2153 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
2154 size_t pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
2155
2156 if (wpa_derive_pmk_r0(fils_ft, fils_ft_len,
2157 conf->ssid, conf->ssid_len,
2158 conf->mobility_domain,
2159 conf->r0_key_holder,
2160 conf->r0_key_holder_len,
2161 sm->addr, pmk_r0, pmk_r0_name,
2162 use_sha384) < 0)
2163 return -1;
2164
2165 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
2166 pmk_r0, pmk_r0_len);
2167 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
2168 pmk_r0_name, WPA_PMK_NAME_LEN);
2169 wpa_ft_store_pmk_fils(sm, pmk_r0, pmk_r0_name);
2170 os_memset(fils_ft, 0, sizeof(fils_ft));
2171 }
2172 #endif /* CONFIG_IEEE80211R_AP */
2173
2174 res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
2175 sm->addr, sm->wpa_auth->addr,
2176 g_sta ? wpabuf_head(g_sta) : NULL,
2177 g_sta ? wpabuf_len(g_sta) : 0,
2178 g_ap ? wpabuf_head(g_ap) : NULL,
2179 g_ap ? wpabuf_len(g_ap) : 0,
2180 sm->wpa_key_mgmt, sm->fils_key_auth_sta,
2181 sm->fils_key_auth_ap,
2182 &sm->fils_key_auth_len);
2183 os_memset(ick, 0, sizeof(ick));
2184
2185 /* Store nonces for (Re)Association Request/Response frame processing */
2186 os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
2187 os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
2188
2189 return res;
2190 }
2191
2192
2193 static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2194 u8 *buf, size_t buf_len, u16 *_key_data_len)
2195 {
2196 struct ieee802_1x_hdr *hdr;
2197 struct wpa_eapol_key *key;
2198 u8 *pos;
2199 u16 key_data_len;
2200 u8 *tmp;
2201 const u8 *aad[1];
2202 size_t aad_len[1];
2203
2204 hdr = (struct ieee802_1x_hdr *) buf;
2205 key = (struct wpa_eapol_key *) (hdr + 1);
2206 pos = (u8 *) (key + 1);
2207 key_data_len = WPA_GET_BE16(pos);
2208 if (key_data_len < AES_BLOCK_SIZE ||
2209 key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
2210 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2211 "No room for AES-SIV data in the frame");
2212 return -1;
2213 }
2214 pos += 2; /* Pointing at the Encrypted Key Data field */
2215
2216 tmp = os_malloc(key_data_len);
2217 if (!tmp)
2218 return -1;
2219
2220 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2221 * to Key Data (exclusive). */
2222 aad[0] = buf;
2223 aad_len[0] = pos - buf;
2224 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
2225 1, aad, aad_len, tmp) < 0) {
2226 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2227 "Invalid AES-SIV data in the frame");
2228 bin_clear_free(tmp, key_data_len);
2229 return -1;
2230 }
2231
2232 /* AEAD decryption and validation completed successfully */
2233 key_data_len -= AES_BLOCK_SIZE;
2234 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2235 tmp, key_data_len);
2236
2237 /* Replace Key Data field with the decrypted version */
2238 os_memcpy(pos, tmp, key_data_len);
2239 pos -= 2; /* Key Data Length field */
2240 WPA_PUT_BE16(pos, key_data_len);
2241 bin_clear_free(tmp, key_data_len);
2242 if (_key_data_len)
2243 *_key_data_len = key_data_len;
2244 return 0;
2245 }
2246
2247
2248 const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm,
2249 const u8 *ies, size_t ies_len,
2250 const u8 *fils_session)
2251 {
2252 const u8 *ie, *end;
2253 const u8 *session = NULL;
2254
2255 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2256 wpa_printf(MSG_DEBUG,
2257 "FILS: Not a FILS AKM - reject association");
2258 return NULL;
2259 }
2260
2261 /* Verify Session element */
2262 ie = ies;
2263 end = ((const u8 *) ie) + ies_len;
2264 while (ie + 1 < end) {
2265 if (ie + 2 + ie[1] > end)
2266 break;
2267 if (ie[0] == WLAN_EID_EXTENSION &&
2268 ie[1] >= 1 + FILS_SESSION_LEN &&
2269 ie[2] == WLAN_EID_EXT_FILS_SESSION) {
2270 session = ie;
2271 break;
2272 }
2273 ie += 2 + ie[1];
2274 }
2275
2276 if (!session) {
2277 wpa_printf(MSG_DEBUG,
2278 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2279 __func__);
2280 return NULL;
2281 }
2282
2283 if (!fils_session) {
2284 wpa_printf(MSG_DEBUG,
2285 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2286 __func__);
2287 return NULL;
2288 }
2289
2290 if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
2291 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
2292 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
2293 fils_session, FILS_SESSION_LEN);
2294 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
2295 session + 3, FILS_SESSION_LEN);
2296 return NULL;
2297 }
2298 return session;
2299 }
2300
2301
2302 int wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies,
2303 size_t ies_len)
2304 {
2305 struct ieee802_11_elems elems;
2306
2307 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
2308 wpa_printf(MSG_DEBUG,
2309 "FILS: Failed to parse decrypted elements");
2310 return -1;
2311 }
2312
2313 if (!elems.fils_session) {
2314 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
2315 return -1;
2316 }
2317
2318 if (!elems.fils_key_confirm) {
2319 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
2320 return -1;
2321 }
2322
2323 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
2324 wpa_printf(MSG_DEBUG,
2325 "FILS: Unexpected Key-Auth length %d (expected %d)",
2326 elems.fils_key_confirm_len,
2327 (int) sm->fils_key_auth_len);
2328 return -1;
2329 }
2330
2331 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
2332 sm->fils_key_auth_len) != 0) {
2333 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
2334 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
2335 elems.fils_key_confirm, elems.fils_key_confirm_len);
2336 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
2337 sm->fils_key_auth_sta, sm->fils_key_auth_len);
2338 return -1;
2339 }
2340
2341 return 0;
2342 }
2343
2344
2345 int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
2346 const struct ieee80211_mgmt *mgmt, size_t frame_len,
2347 u8 *pos, size_t left)
2348 {
2349 u16 fc, stype;
2350 const u8 *end, *ie_start, *ie, *session, *crypt;
2351 const u8 *aad[5];
2352 size_t aad_len[5];
2353
2354 if (!sm || !sm->PTK_valid) {
2355 wpa_printf(MSG_DEBUG,
2356 "FILS: No KEK to decrypt Assocication Request frame");
2357 return -1;
2358 }
2359
2360 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2361 wpa_printf(MSG_DEBUG,
2362 "FILS: Not a FILS AKM - reject association");
2363 return -1;
2364 }
2365
2366 end = ((const u8 *) mgmt) + frame_len;
2367 fc = le_to_host16(mgmt->frame_control);
2368 stype = WLAN_FC_GET_STYPE(fc);
2369 if (stype == WLAN_FC_STYPE_REASSOC_REQ)
2370 ie_start = mgmt->u.reassoc_req.variable;
2371 else
2372 ie_start = mgmt->u.assoc_req.variable;
2373 ie = ie_start;
2374
2375 /*
2376 * Find FILS Session element which is the last unencrypted element in
2377 * the frame.
2378 */
2379 session = wpa_fils_validate_fils_session(sm, ie, end - ie,
2380 fils_session);
2381 if (!session) {
2382 wpa_printf(MSG_DEBUG, "FILS: Session validation failed");
2383 return -1;
2384 }
2385
2386 crypt = session + 2 + session[1];
2387
2388 if (end - crypt < AES_BLOCK_SIZE) {
2389 wpa_printf(MSG_DEBUG,
2390 "FILS: Too short frame to include AES-SIV data");
2391 return -1;
2392 }
2393
2394 /* AES-SIV AAD vectors */
2395
2396 /* The STA's MAC address */
2397 aad[0] = mgmt->sa;
2398 aad_len[0] = ETH_ALEN;
2399 /* The AP's BSSID */
2400 aad[1] = mgmt->da;
2401 aad_len[1] = ETH_ALEN;
2402 /* The STA's nonce */
2403 aad[2] = sm->SNonce;
2404 aad_len[2] = FILS_NONCE_LEN;
2405 /* The AP's nonce */
2406 aad[3] = sm->ANonce;
2407 aad_len[3] = FILS_NONCE_LEN;
2408 /*
2409 * The (Re)Association Request frame from the Capability Information
2410 * field to the FILS Session element (both inclusive).
2411 */
2412 aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
2413 aad_len[4] = crypt - aad[4];
2414
2415 if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
2416 5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
2417 wpa_printf(MSG_DEBUG,
2418 "FILS: Invalid AES-SIV data in the frame");
2419 return -1;
2420 }
2421 wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
2422 pos, left - AES_BLOCK_SIZE);
2423
2424 if (wpa_fils_validate_key_confirm(sm, pos, left - AES_BLOCK_SIZE) < 0) {
2425 wpa_printf(MSG_DEBUG, "FILS: Key Confirm validation failed");
2426 return -1;
2427 }
2428
2429 return left - AES_BLOCK_SIZE;
2430 }
2431
2432
2433 int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
2434 size_t current_len, size_t max_len,
2435 const struct wpabuf *hlp)
2436 {
2437 u8 *end = buf + max_len;
2438 u8 *pos = buf + current_len;
2439 struct ieee80211_mgmt *mgmt;
2440 struct wpabuf *plain;
2441 const u8 *aad[5];
2442 size_t aad_len[5];
2443
2444 if (!sm || !sm->PTK_valid)
2445 return -1;
2446
2447 wpa_hexdump(MSG_DEBUG,
2448 "FILS: Association Response frame before FILS processing",
2449 buf, current_len);
2450
2451 mgmt = (struct ieee80211_mgmt *) buf;
2452
2453 /* AES-SIV AAD vectors */
2454
2455 /* The AP's BSSID */
2456 aad[0] = mgmt->sa;
2457 aad_len[0] = ETH_ALEN;
2458 /* The STA's MAC address */
2459 aad[1] = mgmt->da;
2460 aad_len[1] = ETH_ALEN;
2461 /* The AP's nonce */
2462 aad[2] = sm->ANonce;
2463 aad_len[2] = FILS_NONCE_LEN;
2464 /* The STA's nonce */
2465 aad[3] = sm->SNonce;
2466 aad_len[3] = FILS_NONCE_LEN;
2467 /*
2468 * The (Re)Association Response frame from the Capability Information
2469 * field (the same offset in both Association and Reassociation
2470 * Response frames) to the FILS Session element (both inclusive).
2471 */
2472 aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
2473 aad_len[4] = pos - aad[4];
2474
2475 /* The following elements will be encrypted with AES-SIV */
2476 plain = fils_prepare_plainbuf(sm, hlp);
2477 if (!plain) {
2478 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2479 return -1;
2480 }
2481
2482 if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
2483 wpa_printf(MSG_DEBUG,
2484 "FILS: Not enough room for FILS elements");
2485 wpabuf_free(plain);
2486 return -1;
2487 }
2488
2489 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
2490 plain);
2491
2492 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
2493 wpabuf_head(plain), wpabuf_len(plain),
2494 5, aad, aad_len, pos) < 0) {
2495 wpabuf_free(plain);
2496 return -1;
2497 }
2498
2499 wpa_hexdump(MSG_DEBUG,
2500 "FILS: Encrypted Association Response elements",
2501 pos, AES_BLOCK_SIZE + wpabuf_len(plain));
2502 current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
2503 wpabuf_free(plain);
2504
2505 sm->fils_completed = 1;
2506
2507 return current_len;
2508 }
2509
2510
2511 static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
2512 const struct wpabuf *hlp)
2513 {
2514 struct wpabuf *plain;
2515 u8 *len, *tmp, *tmp2;
2516 u8 hdr[2];
2517 u8 *gtk, dummy_gtk[32];
2518 size_t gtk_len;
2519 struct wpa_group *gsm;
2520
2521 plain = wpabuf_alloc(1000);
2522 if (!plain)
2523 return NULL;
2524
2525 /* TODO: FILS Public Key */
2526
2527 /* FILS Key Confirmation */
2528 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2529 wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
2530 /* Element ID Extension */
2531 wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
2532 wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
2533
2534 /* FILS HLP Container */
2535 if (hlp)
2536 wpabuf_put_buf(plain, hlp);
2537
2538 /* TODO: FILS IP Address Assignment */
2539
2540 /* Key Delivery */
2541 gsm = sm->group;
2542 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2543 len = wpabuf_put(plain, 1);
2544 wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
2545 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
2546 wpabuf_put(plain, WPA_KEY_RSC_LEN));
2547 /* GTK KDE */
2548 gtk = gsm->GTK[gsm->GN - 1];
2549 gtk_len = gsm->GTK_len;
2550 if (sm->wpa_auth->conf.disable_gtk ||
2551 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
2552 /*
2553 * Provide unique random GTK to each STA to prevent use
2554 * of GTK in the BSS.
2555 */
2556 if (random_get_bytes(dummy_gtk, gtk_len) < 0) {
2557 wpabuf_free(plain);
2558 return NULL;
2559 }
2560 gtk = dummy_gtk;
2561 }
2562 hdr[0] = gsm->GN & 0x03;
2563 hdr[1] = 0;
2564 tmp = wpabuf_put(plain, 0);
2565 tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2566 gtk, gtk_len);
2567 wpabuf_put(plain, tmp2 - tmp);
2568
2569 /* IGTK KDE */
2570 tmp = wpabuf_put(plain, 0);
2571 tmp2 = ieee80211w_kde_add(sm, tmp);
2572 wpabuf_put(plain, tmp2 - tmp);
2573
2574 *len = (u8 *) wpabuf_put(plain, 0) - len - 1;
2575 return plain;
2576 }
2577
2578
2579 int fils_set_tk(struct wpa_state_machine *sm)
2580 {
2581 enum wpa_alg alg;
2582 int klen;
2583
2584 if (!sm || !sm->PTK_valid) {
2585 wpa_printf(MSG_DEBUG, "FILS: No valid PTK available to set TK");
2586 return -1;
2587 }
2588 if (sm->tk_already_set) {
2589 wpa_printf(MSG_DEBUG, "FILS: TK already set to the driver");
2590 return -1;
2591 }
2592
2593 alg = wpa_cipher_to_alg(sm->pairwise);
2594 klen = wpa_cipher_key_len(sm->pairwise);
2595
2596 wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
2597 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2598 sm->PTK.tk, klen)) {
2599 wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
2600 return -1;
2601 }
2602 sm->tk_already_set = TRUE;
2603
2604 return 0;
2605 }
2606
2607
2608 u8 * hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf,
2609 const u8 *fils_session, struct wpabuf *hlp)
2610 {
2611 struct wpabuf *plain;
2612 u8 *pos = buf;
2613
2614 /* FILS Session */
2615 *pos++ = WLAN_EID_EXTENSION; /* Element ID */
2616 *pos++ = 1 + FILS_SESSION_LEN; /* Length */
2617 *pos++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
2618 os_memcpy(pos, fils_session, FILS_SESSION_LEN);
2619 pos += FILS_SESSION_LEN;
2620
2621 plain = fils_prepare_plainbuf(sm, hlp);
2622 if (!plain) {
2623 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2624 return NULL;
2625 }
2626
2627 os_memcpy(pos, wpabuf_head(plain), wpabuf_len(plain));
2628 pos += wpabuf_len(plain);
2629
2630 wpa_printf(MSG_DEBUG, "%s: plain buf_len: %u", __func__,
2631 (unsigned int) wpabuf_len(plain));
2632 wpabuf_free(plain);
2633 sm->fils_completed = 1;
2634 return pos;
2635 }
2636
2637 #endif /* CONFIG_FILS */
2638
2639
2640 #ifdef CONFIG_OCV
2641 int get_sta_tx_parameters(struct wpa_state_machine *sm, int ap_max_chanwidth,
2642 int ap_seg1_idx, int *bandwidth, int *seg1_idx)
2643 {
2644 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2645
2646 if (!wpa_auth->cb->get_sta_tx_params)
2647 return -1;
2648 return wpa_auth->cb->get_sta_tx_params(wpa_auth->cb_ctx, sm->addr,
2649 ap_max_chanwidth, ap_seg1_idx,
2650 bandwidth, seg1_idx);
2651 }
2652 #endif /* CONFIG_OCV */
2653
2654
2655 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2656 {
2657 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2658 struct wpa_ptk PTK;
2659 int ok = 0, psk_found = 0;
2660 const u8 *pmk = NULL;
2661 size_t pmk_len;
2662 int ft;
2663 const u8 *eapol_key_ie, *key_data, *mic;
2664 u16 key_data_length;
2665 size_t mic_len, eapol_key_ie_len;
2666 struct ieee802_1x_hdr *hdr;
2667 struct wpa_eapol_key *key;
2668 struct wpa_eapol_ie_parse kde;
2669
2670 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2671 sm->EAPOLKeyReceived = FALSE;
2672 sm->update_snonce = FALSE;
2673 os_memset(&PTK, 0, sizeof(PTK));
2674
2675 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
2676
2677 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2678 * WPA-PSK: iterate through possible PSKs and select the one matching
2679 * the packet */
2680 for (;;) {
2681 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
2682 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
2683 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
2684 sm->p2p_dev_addr, pmk, &pmk_len);
2685 if (pmk == NULL)
2686 break;
2687 psk_found = 1;
2688 #ifdef CONFIG_IEEE80211R_AP
2689 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
2690 os_memcpy(sm->xxkey, pmk, pmk_len);
2691 sm->xxkey_len = pmk_len;
2692 }
2693 #endif /* CONFIG_IEEE80211R_AP */
2694 } else {
2695 pmk = sm->PMK;
2696 pmk_len = sm->pmk_len;
2697 }
2698
2699 if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK) < 0)
2700 break;
2701
2702 if (mic_len &&
2703 wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
2704 sm->last_rx_eapol_key,
2705 sm->last_rx_eapol_key_len) == 0) {
2706 ok = 1;
2707 break;
2708 }
2709
2710 #ifdef CONFIG_FILS
2711 if (!mic_len &&
2712 wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
2713 sm->last_rx_eapol_key_len, NULL) == 0) {
2714 ok = 1;
2715 break;
2716 }
2717 #endif /* CONFIG_FILS */
2718
2719 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
2720 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
2721 break;
2722 }
2723
2724 if (!ok) {
2725 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2726 "invalid MIC in msg 2/4 of 4-Way Handshake");
2727 if (psk_found)
2728 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
2729 return;
2730 }
2731
2732 /*
2733 * Note: last_rx_eapol_key length fields have already been validated in
2734 * wpa_receive().
2735 */
2736 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
2737 key = (struct wpa_eapol_key *) (hdr + 1);
2738 mic = (u8 *) (key + 1);
2739 key_data = mic + mic_len + 2;
2740 key_data_length = WPA_GET_BE16(mic + mic_len);
2741 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
2742 sizeof(*key) - mic_len - 2)
2743 return;
2744
2745 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
2746 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
2747 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2748 return;
2749 }
2750 if (kde.rsn_ie) {
2751 eapol_key_ie = kde.rsn_ie;
2752 eapol_key_ie_len = kde.rsn_ie_len;
2753 } else if (kde.osen) {
2754 eapol_key_ie = kde.osen;
2755 eapol_key_ie_len = kde.osen_len;
2756 } else {
2757 eapol_key_ie = kde.wpa_ie;
2758 eapol_key_ie_len = kde.wpa_ie_len;
2759 }
2760 ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
2761 if (sm->wpa_ie == NULL ||
2762 wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
2763 eapol_key_ie, eapol_key_ie_len)) {
2764 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2765 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2766 if (sm->wpa_ie) {
2767 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
2768 sm->wpa_ie, sm->wpa_ie_len);
2769 }
2770 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
2771 eapol_key_ie, eapol_key_ie_len);
2772 /* MLME-DEAUTHENTICATE.request */
2773 wpa_sta_disconnect(wpa_auth, sm->addr,
2774 WLAN_REASON_PREV_AUTH_NOT_VALID);
2775 return;
2776 }
2777 #ifdef CONFIG_OCV
2778 if (wpa_auth_uses_ocv(sm)) {
2779 struct wpa_channel_info ci;
2780 int tx_chanwidth;
2781 int tx_seg1_idx;
2782
2783 if (wpa_channel_info(wpa_auth, &ci) != 0) {
2784 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2785 "Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
2786 return;
2787 }
2788
2789 if (get_sta_tx_parameters(sm,
2790 channel_width_to_int(ci.chanwidth),
2791 ci.seg1_idx, &tx_chanwidth,
2792 &tx_seg1_idx) < 0)
2793 return;
2794
2795 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
2796 tx_chanwidth, tx_seg1_idx) != 0) {
2797 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2798 ocv_errorstr);
2799 return;
2800 }
2801 }
2802 #endif /* CONFIG_OCV */
2803 #ifdef CONFIG_IEEE80211R_AP
2804 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
2805 wpa_sta_disconnect(wpa_auth, sm->addr,
2806 WLAN_REASON_PREV_AUTH_NOT_VALID);
2807 return;
2808 }
2809 #endif /* CONFIG_IEEE80211R_AP */
2810 #ifdef CONFIG_P2P
2811 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
2812 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
2813 int idx;
2814 wpa_printf(MSG_DEBUG,
2815 "P2P: IP address requested in EAPOL-Key exchange");
2816 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
2817 if (idx >= 0) {
2818 u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
2819 bitfield_set(wpa_auth->ip_pool, idx);
2820 WPA_PUT_BE32(sm->ip_addr, start + idx);
2821 wpa_printf(MSG_DEBUG,
2822 "P2P: Assigned IP address %u.%u.%u.%u to "
2823 MACSTR, sm->ip_addr[0], sm->ip_addr[1],
2824 sm->ip_addr[2], sm->ip_addr[3],
2825 MAC2STR(sm->addr));
2826 }
2827 }
2828 #endif /* CONFIG_P2P */
2829
2830 #ifdef CONFIG_IEEE80211R_AP
2831 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2832 /*
2833 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2834 * with the value we derived.
2835 */
2836 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
2837 WPA_PMK_NAME_LEN) != 0) {
2838 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2839 "PMKR1Name mismatch in FT 4-way "
2840 "handshake");
2841 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
2842 "Supplicant",
2843 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
2844 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2845 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2846 return;
2847 }
2848 }
2849 #endif /* CONFIG_IEEE80211R_AP */
2850
2851 sm->pending_1_of_4_timeout = 0;
2852 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
2853
2854 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2855 /* PSK may have changed from the previous choice, so update
2856 * state machine data based on whatever PSK was selected here.
2857 */
2858 os_memcpy(sm->PMK, pmk, PMK_LEN);
2859 sm->pmk_len = PMK_LEN;
2860 }
2861
2862 sm->MICVerified = TRUE;
2863
2864 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
2865 sm->PTK_valid = TRUE;
2866 }
2867
2868
2869 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2870 {
2871 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2872 sm->TimeoutCtr = 0;
2873 }
2874
2875
2876 #ifdef CONFIG_IEEE80211W
2877
2878 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2879 {
2880 if (sm->mgmt_frame_prot) {
2881 size_t len;
2882 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2883 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
2884 }
2885
2886 return 0;
2887 }
2888
2889
2890 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2891 {
2892 struct wpa_igtk_kde igtk;
2893 struct wpa_group *gsm = sm->group;
2894 u8 rsc[WPA_KEY_RSC_LEN];
2895 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2896
2897 if (!sm->mgmt_frame_prot)
2898 return pos;
2899
2900 igtk.keyid[0] = gsm->GN_igtk;
2901 igtk.keyid[1] = 0;
2902 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
2903 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
2904 os_memset(igtk.pn, 0, sizeof(igtk.pn));
2905 else
2906 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
2907 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
2908 if (sm->wpa_auth->conf.disable_gtk ||
2909 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
2910 /*
2911 * Provide unique random IGTK to each STA to prevent use of
2912 * IGTK in the BSS.
2913 */
2914 if (random_get_bytes(igtk.igtk, len) < 0)
2915 return pos;
2916 }
2917 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
2918 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
2919 NULL, 0);
2920
2921 return pos;
2922 }
2923
2924 #else /* CONFIG_IEEE80211W */
2925
2926 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2927 {
2928 return 0;
2929 }
2930
2931
2932 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2933 {
2934 return pos;
2935 }
2936
2937 #endif /* CONFIG_IEEE80211W */
2938
2939
2940 static int ocv_oci_len(struct wpa_state_machine *sm)
2941 {
2942 #ifdef CONFIG_OCV
2943 if (wpa_auth_uses_ocv(sm))
2944 return OCV_OCI_KDE_LEN;
2945 #endif /* CONFIG_OCV */
2946 return 0;
2947 }
2948
2949 static int ocv_oci_add(struct wpa_state_machine *sm, u8 **argpos)
2950 {
2951 #ifdef CONFIG_OCV
2952 struct wpa_channel_info ci;
2953
2954 if (!wpa_auth_uses_ocv(sm))
2955 return 0;
2956
2957 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
2958 wpa_printf(MSG_WARNING,
2959 "Failed to get channel info for OCI element");
2960 return -1;
2961 }
2962
2963 return ocv_insert_oci_kde(&ci, argpos);
2964 #else /* CONFIG_OCV */
2965 return 0;
2966 #endif /* CONFIG_OCV */
2967 }
2968
2969
2970 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2971 {
2972 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
2973 size_t gtk_len, kde_len;
2974 struct wpa_group *gsm = sm->group;
2975 u8 *wpa_ie;
2976 int wpa_ie_len, secure, keyidx, encr = 0;
2977
2978 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2979 sm->TimeoutEvt = FALSE;
2980
2981 sm->TimeoutCtr++;
2982 if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
2983 sm->TimeoutCtr > 1) {
2984 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
2985 return;
2986 }
2987 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
2988 /* No point in sending the EAPOL-Key - we will disconnect
2989 * immediately following this. */
2990 return;
2991 }
2992
2993 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2994 GTK[GN], IGTK, [FTIE], [TIE * 2])
2995 */
2996 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2997 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2998 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2999 wpa_ie = sm->wpa_auth->wpa_ie;
3000 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
3001 if (sm->wpa == WPA_VERSION_WPA &&
3002 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
3003 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
3004 /* WPA-only STA, remove RSN IE and possible MDIE */
3005 wpa_ie = wpa_ie + wpa_ie[1] + 2;
3006 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
3007 wpa_ie = wpa_ie + wpa_ie[1] + 2;
3008 wpa_ie_len = wpa_ie[1] + 2;
3009 }
3010 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3011 "sending 3/4 msg of 4-Way Handshake");
3012 if (sm->wpa == WPA_VERSION_WPA2) {
3013 /* WPA2 send GTK in the 4-way handshake */
3014 secure = 1;
3015 gtk = gsm->GTK[gsm->GN - 1];
3016 gtk_len = gsm->GTK_len;
3017 if (sm->wpa_auth->conf.disable_gtk ||
3018 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3019 /*
3020 * Provide unique random GTK to each STA to prevent use
3021 * of GTK in the BSS.
3022 */
3023 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
3024 return;
3025 gtk = dummy_gtk;
3026 }
3027 keyidx = gsm->GN;
3028 _rsc = rsc;
3029 encr = 1;
3030 } else {
3031 /* WPA does not include GTK in msg 3/4 */
3032 secure = 0;
3033 gtk = NULL;
3034 gtk_len = 0;
3035 keyidx = 0;
3036 _rsc = NULL;
3037 if (sm->rx_eapol_key_secure) {
3038 /*
3039 * It looks like Windows 7 supplicant tries to use
3040 * Secure bit in msg 2/4 after having reported Michael
3041 * MIC failure and it then rejects the 4-way handshake
3042 * if msg 3/4 does not set Secure bit. Work around this
3043 * by setting the Secure bit here even in the case of
3044 * WPA if the supplicant used it first.
3045 */
3046 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3047 "STA used Secure bit in WPA msg 2/4 - "
3048 "set Secure for 3/4 as workaround");
3049 secure = 1;
3050 }
3051 }
3052
3053 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
3054 if (gtk)
3055 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
3056 #ifdef CONFIG_IEEE80211R_AP
3057 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3058 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
3059 kde_len += 300; /* FTIE + 2 * TIE */
3060 }
3061 #endif /* CONFIG_IEEE80211R_AP */
3062 #ifdef CONFIG_P2P
3063 if (WPA_GET_BE32(sm->ip_addr) > 0)
3064 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
3065 #endif /* CONFIG_P2P */
3066 kde = os_malloc(kde_len);
3067 if (kde == NULL)
3068 return;
3069
3070 pos = kde;
3071 os_memcpy(pos, wpa_ie, wpa_ie_len);
3072 pos += wpa_ie_len;
3073 #ifdef CONFIG_IEEE80211R_AP
3074 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3075 int res;
3076 size_t elen;
3077
3078 elen = pos - kde;
3079 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
3080 if (res < 0) {
3081 wpa_printf(MSG_ERROR, "FT: Failed to insert "
3082 "PMKR1Name into RSN IE in EAPOL-Key data");
3083 os_free(kde);
3084 return;
3085 }
3086 pos -= wpa_ie_len;
3087 pos += elen;
3088 }
3089 #endif /* CONFIG_IEEE80211R_AP */
3090 if (gtk) {
3091 u8 hdr[2];
3092 hdr[0] = keyidx & 0x03;
3093 hdr[1] = 0;
3094 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3095 gtk, gtk_len);
3096 }
3097 pos = ieee80211w_kde_add(sm, pos);
3098 if (ocv_oci_add(sm, &pos) < 0) {
3099 os_free(kde);
3100 return;
3101 }
3102
3103 #ifdef CONFIG_IEEE80211R_AP
3104 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3105 int res;
3106 struct wpa_auth_config *conf;
3107
3108 conf = &sm->wpa_auth->conf;
3109 if (sm->assoc_resp_ftie &&
3110 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
3111 os_memcpy(pos, sm->assoc_resp_ftie,
3112 2 + sm->assoc_resp_ftie[1]);
3113 res = 2 + sm->assoc_resp_ftie[1];
3114 } else {
3115 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
3116
3117 res = wpa_write_ftie(conf, use_sha384,
3118 conf->r0_key_holder,
3119 conf->r0_key_holder_len,
3120 NULL, NULL, pos,
3121 kde + kde_len - pos,
3122 NULL, 0);
3123 }
3124 if (res < 0) {
3125 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
3126 "into EAPOL-Key Key Data");
3127 os_free(kde);
3128 return;
3129 }
3130 pos += res;
3131
3132 /* TIE[ReassociationDeadline] (TU) */
3133 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3134 *pos++ = 5;
3135 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
3136 WPA_PUT_LE32(pos, conf->reassociation_deadline);
3137 pos += 4;
3138
3139 /* TIE[KeyLifetime] (seconds) */
3140 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3141 *pos++ = 5;
3142 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
3143 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
3144 pos += 4;
3145 }
3146 #endif /* CONFIG_IEEE80211R_AP */
3147 #ifdef CONFIG_P2P
3148 if (WPA_GET_BE32(sm->ip_addr) > 0) {
3149 u8 addr[3 * 4];
3150 os_memcpy(addr, sm->ip_addr, 4);
3151 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
3152 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
3153 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
3154 addr, sizeof(addr), NULL, 0);
3155 }
3156 #endif /* CONFIG_P2P */
3157
3158 wpa_send_eapol(sm->wpa_auth, sm,
3159 (secure ? WPA_KEY_INFO_SECURE : 0) |
3160 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3161 WPA_KEY_INFO_MIC : 0) |
3162 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
3163 WPA_KEY_INFO_KEY_TYPE,
3164 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
3165 os_free(kde);
3166 }
3167
3168
3169 SM_STATE(WPA_PTK, PTKINITDONE)
3170 {
3171 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
3172 sm->EAPOLKeyReceived = FALSE;
3173 if (sm->Pair) {
3174 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
3175 int klen = wpa_cipher_key_len(sm->pairwise);
3176 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
3177 sm->PTK.tk, klen)) {
3178 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
3179 WLAN_REASON_PREV_AUTH_NOT_VALID);
3180 return;
3181 }
3182 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3183 sm->pairwise_set = TRUE;
3184
3185 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
3186 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
3187 eloop_register_timeout(sm->wpa_auth->conf.
3188 wpa_ptk_rekey, 0, wpa_rekey_ptk,
3189 sm->wpa_auth, sm);
3190 }
3191
3192 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3193 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
3194 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
3195 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3196 WPA_EAPOL_authorized, 1);
3197 }
3198 }
3199
3200 if (0 /* IBSS == TRUE */) {
3201 sm->keycount++;
3202 if (sm->keycount == 2) {
3203 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3204 WPA_EAPOL_portValid, 1);
3205 }
3206 } else {
3207 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
3208 1);
3209 }
3210 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
3211 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
3212 if (sm->wpa == WPA_VERSION_WPA)
3213 sm->PInitAKeys = TRUE;
3214 else
3215 sm->has_GTK = TRUE;
3216 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3217 "pairwise key handshake completed (%s)",
3218 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3219
3220 #ifdef CONFIG_IEEE80211R_AP
3221 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
3222 #endif /* CONFIG_IEEE80211R_AP */
3223 }
3224
3225
3226 SM_STEP(WPA_PTK)
3227 {
3228 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3229
3230 if (sm->Init)
3231 SM_ENTER(WPA_PTK, INITIALIZE);
3232 else if (sm->Disconnect
3233 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3234 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
3235 "WPA_PTK: sm->Disconnect");
3236 SM_ENTER(WPA_PTK, DISCONNECT);
3237 }
3238 else if (sm->DeauthenticationRequest)
3239 SM_ENTER(WPA_PTK, DISCONNECTED);
3240 else if (sm->AuthenticationRequest)
3241 SM_ENTER(WPA_PTK, AUTHENTICATION);
3242 else if (sm->ReAuthenticationRequest)
3243 SM_ENTER(WPA_PTK, AUTHENTICATION2);
3244 else if (sm->PTKRequest) {
3245 if (wpa_auth_sm_ptk_update(sm) < 0)
3246 SM_ENTER(WPA_PTK, DISCONNECTED);
3247 else
3248 SM_ENTER(WPA_PTK, PTKSTART);
3249 } else switch (sm->wpa_ptk_state) {
3250 case WPA_PTK_INITIALIZE:
3251 break;
3252 case WPA_PTK_DISCONNECT:
3253 SM_ENTER(WPA_PTK, DISCONNECTED);
3254 break;
3255 case WPA_PTK_DISCONNECTED:
3256 SM_ENTER(WPA_PTK, INITIALIZE);
3257 break;
3258 case WPA_PTK_AUTHENTICATION:
3259 SM_ENTER(WPA_PTK, AUTHENTICATION2);
3260 break;
3261 case WPA_PTK_AUTHENTICATION2:
3262 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
3263 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
3264 WPA_EAPOL_keyRun) > 0)
3265 SM_ENTER(WPA_PTK, INITPMK);
3266 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3267 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
3268 /* FIX: && 802.1X::keyRun */)
3269 SM_ENTER(WPA_PTK, INITPSK);
3270 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
3271 SM_ENTER(WPA_PTK, INITPMK);
3272 break;
3273 case WPA_PTK_INITPMK:
3274 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
3275 WPA_EAPOL_keyAvailable) > 0) {
3276 SM_ENTER(WPA_PTK, PTKSTART);
3277 #ifdef CONFIG_DPP
3278 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->pmksa) {
3279 SM_ENTER(WPA_PTK, PTKSTART);
3280 #endif /* CONFIG_DPP */
3281 } else {
3282 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3283 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3284 "INITPMK - keyAvailable = false");
3285 SM_ENTER(WPA_PTK, DISCONNECT);
3286 }
3287 break;
3288 case WPA_PTK_INITPSK:
3289 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
3290 NULL, NULL)) {
3291 SM_ENTER(WPA_PTK, PTKSTART);
3292 #ifdef CONFIG_SAE
3293 } else if (wpa_auth_uses_sae(sm) && sm->pmksa) {
3294 SM_ENTER(WPA_PTK, PTKSTART);
3295 #endif /* CONFIG_SAE */
3296 } else {
3297 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3298 "no PSK configured for the STA");
3299 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3300 SM_ENTER(WPA_PTK, DISCONNECT);
3301 }
3302 break;
3303 case WPA_PTK_PTKSTART:
3304 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3305 sm->EAPOLKeyPairwise)
3306 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3307 else if (sm->TimeoutCtr >
3308 sm->wpa_auth->conf.wpa_pairwise_update_count) {
3309 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3310 wpa_auth_vlogger(
3311 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3312 "PTKSTART: Retry limit %u reached",
3313 sm->wpa_auth->conf.wpa_pairwise_update_count);
3314 SM_ENTER(WPA_PTK, DISCONNECT);
3315 } else if (sm->TimeoutEvt)
3316 SM_ENTER(WPA_PTK, PTKSTART);
3317 break;
3318 case WPA_PTK_PTKCALCNEGOTIATING:
3319 if (sm->MICVerified)
3320 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
3321 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3322 sm->EAPOLKeyPairwise)
3323 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3324 else if (sm->TimeoutEvt)
3325 SM_ENTER(WPA_PTK, PTKSTART);
3326 break;
3327 case WPA_PTK_PTKCALCNEGOTIATING2:
3328 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3329 break;
3330 case WPA_PTK_PTKINITNEGOTIATING:
3331 if (sm->update_snonce)
3332 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3333 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3334 sm->EAPOLKeyPairwise && sm->MICVerified)
3335 SM_ENTER(WPA_PTK, PTKINITDONE);
3336 else if (sm->TimeoutCtr >
3337 sm->wpa_auth->conf.wpa_pairwise_update_count ||
3338 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3339 sm->TimeoutCtr > 1)) {
3340 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3341 wpa_auth_vlogger(
3342 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3343 "PTKINITNEGOTIATING: Retry limit %u reached",
3344 sm->wpa_auth->conf.wpa_pairwise_update_count);
3345 SM_ENTER(WPA_PTK, DISCONNECT);
3346 } else if (sm->TimeoutEvt)
3347 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3348 break;
3349 case WPA_PTK_PTKINITDONE:
3350 break;
3351 }
3352 }
3353
3354
3355 SM_STATE(WPA_PTK_GROUP, IDLE)
3356 {
3357 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
3358 if (sm->Init) {
3359 /* Init flag is not cleared here, so avoid busy
3360 * loop by claiming nothing changed. */
3361 sm->changed = FALSE;
3362 }
3363 sm->GTimeoutCtr = 0;
3364 }
3365
3366
3367 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
3368 {
3369 u8 rsc[WPA_KEY_RSC_LEN];
3370 struct wpa_group *gsm = sm->group;
3371 const u8 *kde;
3372 u8 *kde_buf = NULL, *pos, hdr[2];
3373 size_t kde_len;
3374 u8 *gtk, dummy_gtk[32];
3375
3376 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
3377
3378 sm->GTimeoutCtr++;
3379 if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3380 sm->GTimeoutCtr > 1) {
3381 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3382 return;
3383 }
3384 if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) {
3385 /* No point in sending the EAPOL-Key - we will disconnect
3386 * immediately following this. */
3387 return;
3388 }
3389
3390 if (sm->wpa == WPA_VERSION_WPA)
3391 sm->PInitAKeys = FALSE;
3392 sm->TimeoutEvt = FALSE;
3393 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3394 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3395 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
3396 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3397 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3398 "sending 1/2 msg of Group Key Handshake");
3399
3400 gtk = gsm->GTK[gsm->GN - 1];
3401 if (sm->wpa_auth->conf.disable_gtk ||
3402 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3403 /*
3404 * Provide unique random GTK to each STA to prevent use
3405 * of GTK in the BSS.
3406 */
3407 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
3408 return;
3409 gtk = dummy_gtk;
3410 }
3411 if (sm->wpa == WPA_VERSION_WPA2) {
3412 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
3413 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
3414 kde_buf = os_malloc(kde_len);
3415 if (kde_buf == NULL)
3416 return;
3417
3418 kde = pos = kde_buf;
3419 hdr[0] = gsm->GN & 0x03;
3420 hdr[1] = 0;
3421 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3422 gtk, gsm->GTK_len);
3423 pos = ieee80211w_kde_add(sm, pos);
3424 if (ocv_oci_add(sm, &pos) < 0) {
3425 os_free(kde_buf);
3426 return;
3427 }
3428 kde_len = pos - kde;
3429 } else {
3430 kde = gtk;
3431 kde_len = gsm->GTK_len;
3432 }
3433
3434 wpa_send_eapol(sm->wpa_auth, sm,
3435 WPA_KEY_INFO_SECURE |
3436 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3437 WPA_KEY_INFO_MIC : 0) |
3438 WPA_KEY_INFO_ACK |
3439 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
3440 rsc, NULL, kde, kde_len, gsm->GN, 1);
3441
3442 os_free(kde_buf);
3443 }
3444
3445
3446 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
3447 {
3448 #ifdef CONFIG_OCV
3449 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3450 const u8 *key_data, *mic;
3451 struct ieee802_1x_hdr *hdr;
3452 struct wpa_eapol_key *key;
3453 struct wpa_eapol_ie_parse kde;
3454 size_t mic_len;
3455 u16 key_data_length;
3456 #endif /* CONFIG_OCV */
3457
3458 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
3459 sm->EAPOLKeyReceived = FALSE;
3460
3461 #ifdef CONFIG_OCV
3462 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
3463
3464 /*
3465 * Note: last_rx_eapol_key length fields have already been validated in
3466 * wpa_receive().
3467 */
3468 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
3469 key = (struct wpa_eapol_key *) (hdr + 1);
3470 mic = (u8 *) (key + 1);
3471 key_data = mic + mic_len + 2;
3472 key_data_length = WPA_GET_BE16(mic + mic_len);
3473 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
3474 sizeof(*key) - mic_len - 2)
3475 return;
3476
3477 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
3478 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
3479 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3480 return;
3481 }
3482
3483 if (wpa_auth_uses_ocv(sm)) {
3484 struct wpa_channel_info ci;
3485 int tx_chanwidth;
3486 int tx_seg1_idx;
3487
3488 if (wpa_channel_info(wpa_auth, &ci) != 0) {
3489 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3490 "Failed to get channel info to validate received OCI in EAPOL-Key group 1/2");
3491 return;
3492 }
3493
3494 if (get_sta_tx_parameters(sm,
3495 channel_width_to_int(ci.chanwidth),
3496 ci.seg1_idx, &tx_chanwidth,
3497 &tx_seg1_idx) < 0)
3498 return;
3499
3500 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
3501 tx_chanwidth, tx_seg1_idx) != 0) {
3502 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3503 ocv_errorstr);
3504 return;
3505 }
3506 }
3507 #endif /* CONFIG_OCV */
3508
3509 if (sm->GUpdateStationKeys)
3510 sm->group->GKeyDoneStations--;
3511 sm->GUpdateStationKeys = FALSE;
3512 sm->GTimeoutCtr = 0;
3513 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3514 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3515 "group key handshake completed (%s)",
3516 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3517 sm->has_GTK = TRUE;
3518 }
3519
3520
3521 SM_STATE(WPA_PTK_GROUP, KEYERROR)
3522 {
3523 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
3524 if (sm->GUpdateStationKeys)
3525 sm->group->GKeyDoneStations--;
3526 sm->GUpdateStationKeys = FALSE;
3527 sm->Disconnect = TRUE;
3528 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3529 "group key handshake failed (%s) after %u tries",
3530 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
3531 sm->wpa_auth->conf.wpa_group_update_count);
3532 }
3533
3534
3535 SM_STEP(WPA_PTK_GROUP)
3536 {
3537 if (sm->Init || sm->PtkGroupInit) {
3538 SM_ENTER(WPA_PTK_GROUP, IDLE);
3539 sm->PtkGroupInit = FALSE;
3540 } else switch (sm->wpa_ptk_group_state) {
3541 case WPA_PTK_GROUP_IDLE:
3542 if (sm->GUpdateStationKeys ||
3543 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
3544 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3545 break;
3546 case WPA_PTK_GROUP_REKEYNEGOTIATING:
3547 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3548 !sm->EAPOLKeyPairwise && sm->MICVerified)
3549 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
3550 else if (sm->GTimeoutCtr >
3551 sm->wpa_auth->conf.wpa_group_update_count ||
3552 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3553 sm->GTimeoutCtr > 1))
3554 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
3555 else if (sm->TimeoutEvt)
3556 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3557 break;
3558 case WPA_PTK_GROUP_KEYERROR:
3559 SM_ENTER(WPA_PTK_GROUP, IDLE);
3560 break;
3561 case WPA_PTK_GROUP_REKEYESTABLISHED:
3562 SM_ENTER(WPA_PTK_GROUP, IDLE);
3563 break;
3564 }
3565 }
3566
3567
3568 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
3569 struct wpa_group *group)
3570 {
3571 int ret = 0;
3572
3573 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3574 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3575 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
3576 wpa_auth->addr, group->GNonce,
3577 group->GTK[group->GN - 1], group->GTK_len) < 0)
3578 ret = -1;
3579 wpa_hexdump_key(MSG_DEBUG, "GTK",
3580 group->GTK[group->GN - 1], group->GTK_len);
3581
3582 #ifdef CONFIG_IEEE80211W
3583 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3584 size_t len;
3585 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3586 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3587 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3588 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
3589 wpa_auth->addr, group->GNonce,
3590 group->IGTK[group->GN_igtk - 4], len) < 0)
3591 ret = -1;
3592 wpa_hexdump_key(MSG_DEBUG, "IGTK",
3593 group->IGTK[group->GN_igtk - 4], len);
3594 }
3595 #endif /* CONFIG_IEEE80211W */
3596
3597 return ret;
3598 }
3599
3600
3601 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
3602 struct wpa_group *group)
3603 {
3604 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3605 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
3606 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
3607 group->wpa_group_state = WPA_GROUP_GTK_INIT;
3608
3609 /* GTK[0..N] = 0 */
3610 os_memset(group->GTK, 0, sizeof(group->GTK));
3611 group->GN = 1;
3612 group->GM = 2;
3613 #ifdef CONFIG_IEEE80211W
3614 group->GN_igtk = 4;
3615 group->GM_igtk = 5;
3616 #endif /* CONFIG_IEEE80211W */
3617 /* GTK[GN] = CalcGTK() */
3618 wpa_gtk_update(wpa_auth, group);
3619 }
3620
3621
3622 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
3623 {
3624 if (ctx != NULL && ctx != sm->group)
3625 return 0;
3626
3627 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
3628 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3629 "Not in PTKINITDONE; skip Group Key update");
3630 sm->GUpdateStationKeys = FALSE;
3631 return 0;
3632 }
3633 if (sm->GUpdateStationKeys) {
3634 /*
3635 * This should not really happen, so add a debug log entry.
3636 * Since we clear the GKeyDoneStations before the loop, the
3637 * station needs to be counted here anyway.
3638 */
3639 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3640 "GUpdateStationKeys was already set when "
3641 "marking station for GTK rekeying");
3642 }
3643
3644 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
3645 if (sm->is_wnmsleep)
3646 return 0;
3647
3648 sm->group->GKeyDoneStations++;
3649 sm->GUpdateStationKeys = TRUE;
3650
3651 wpa_sm_step(sm);
3652 return 0;
3653 }
3654
3655
3656 #ifdef CONFIG_WNM_AP
3657 /* update GTK when exiting WNM-Sleep Mode */
3658 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
3659 {
3660 if (sm == NULL || sm->is_wnmsleep)
3661 return;
3662
3663 wpa_group_update_sta(sm, NULL);
3664 }
3665
3666
3667 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
3668 {
3669 if (sm)
3670 sm->is_wnmsleep = !!flag;
3671 }
3672
3673
3674 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3675 {
3676 struct wpa_group *gsm = sm->group;
3677 u8 *start = pos;
3678
3679 /*
3680 * GTK subelement:
3681 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
3682 * Key[5..32]
3683 */
3684 *pos++ = WNM_SLEEP_SUBELEM_GTK;
3685 *pos++ = 11 + gsm->GTK_len;
3686 /* Key ID in B0-B1 of Key Info */
3687 WPA_PUT_LE16(pos, gsm->GN & 0x03);
3688 pos += 2;
3689 *pos++ = gsm->GTK_len;
3690 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
3691 return 0;
3692 pos += 8;
3693 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3694 pos += gsm->GTK_len;
3695
3696 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3697 gsm->GN);
3698 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
3699 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3700
3701 return pos - start;
3702 }
3703
3704
3705 #ifdef CONFIG_IEEE80211W
3706 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3707 {
3708 struct wpa_group *gsm = sm->group;
3709 u8 *start = pos;
3710 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3711
3712 /*
3713 * IGTK subelement:
3714 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3715 */
3716 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
3717 *pos++ = 2 + 6 + len;
3718 WPA_PUT_LE16(pos, gsm->GN_igtk);
3719 pos += 2;
3720 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
3721 return 0;
3722 pos += 6;
3723
3724 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
3725 pos += len;
3726
3727 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3728 gsm->GN_igtk);
3729 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
3730 gsm->IGTK[gsm->GN_igtk - 4], len);
3731
3732 return pos - start;
3733 }
3734 #endif /* CONFIG_IEEE80211W */
3735 #endif /* CONFIG_WNM_AP */
3736
3737
3738 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
3739 struct wpa_group *group)
3740 {
3741 int tmp;
3742
3743 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3744 "SETKEYS (VLAN-ID %d)", group->vlan_id);
3745 group->changed = TRUE;
3746 group->wpa_group_state = WPA_GROUP_SETKEYS;
3747 group->GTKReKey = FALSE;
3748 tmp = group->GM;
3749 group->GM = group->GN;
3750 group->GN = tmp;
3751 #ifdef CONFIG_IEEE80211W
3752 tmp = group->GM_igtk;
3753 group->GM_igtk = group->GN_igtk;
3754 group->GN_igtk = tmp;
3755 #endif /* CONFIG_IEEE80211W */
3756 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3757 * counting the STAs that are marked with GUpdateStationKeys instead of
3758 * including all STAs that could be in not-yet-completed state. */
3759 wpa_gtk_update(wpa_auth, group);
3760
3761 if (group->GKeyDoneStations) {
3762 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
3763 "GKeyDoneStations=%d when starting new GTK rekey",
3764 group->GKeyDoneStations);
3765 group->GKeyDoneStations = 0;
3766 }
3767 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
3768 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
3769 group->GKeyDoneStations);
3770 }
3771
3772
3773 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
3774 struct wpa_group *group)
3775 {
3776 int ret = 0;
3777
3778 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
3779 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
3780 broadcast_ether_addr, group->GN,
3781 group->GTK[group->GN - 1], group->GTK_len) < 0)
3782 ret = -1;
3783
3784 #ifdef CONFIG_IEEE80211W
3785 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3786 enum wpa_alg alg;
3787 size_t len;
3788
3789 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
3790 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3791
3792 if (ret == 0 &&
3793 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
3794 broadcast_ether_addr, group->GN_igtk,
3795 group->IGTK[group->GN_igtk - 4], len) < 0)
3796 ret = -1;
3797 }
3798 #endif /* CONFIG_IEEE80211W */
3799
3800 return ret;
3801 }
3802
3803
3804 static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
3805 {
3806 if (sm->group == ctx) {
3807 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
3808 " for discconnection due to fatal failure",
3809 MAC2STR(sm->addr));
3810 sm->Disconnect = TRUE;
3811 }
3812
3813 return 0;
3814 }
3815
3816
3817 static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
3818 struct wpa_group *group)
3819 {
3820 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
3821 group->changed = TRUE;
3822 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
3823 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
3824 }
3825
3826
3827 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
3828 struct wpa_group *group)
3829 {
3830 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3831 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
3832 group->changed = TRUE;
3833 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
3834
3835 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
3836 wpa_group_fatal_failure(wpa_auth, group);
3837 return -1;
3838 }
3839
3840 return 0;
3841 }
3842
3843
3844 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
3845 struct wpa_group *group)
3846 {
3847 if (group->GInit) {
3848 wpa_group_gtk_init(wpa_auth, group);
3849 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
3850 /* Do not allow group operations */
3851 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
3852 group->GTKAuthenticator) {
3853 wpa_group_setkeysdone(wpa_auth, group);
3854 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
3855 group->GTKReKey) {
3856 wpa_group_setkeys(wpa_auth, group);
3857 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
3858 if (group->GKeyDoneStations == 0)
3859 wpa_group_setkeysdone(wpa_auth, group);
3860 else if (group->GTKReKey)
3861 wpa_group_setkeys(wpa_auth, group);
3862 }
3863 }
3864
3865
3866 static int wpa_sm_step(struct wpa_state_machine *sm)
3867 {
3868 if (sm == NULL)
3869 return 0;
3870
3871 if (sm->in_step_loop) {
3872 /* This should not happen, but if it does, make sure we do not
3873 * end up freeing the state machine too early by exiting the
3874 * recursive call. */
3875 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
3876 return 0;
3877 }
3878
3879 sm->in_step_loop = 1;
3880 do {
3881 if (sm->pending_deinit)
3882 break;
3883
3884 sm->changed = FALSE;
3885 sm->wpa_auth->group->changed = FALSE;
3886
3887 SM_STEP_RUN(WPA_PTK);
3888 if (sm->pending_deinit)
3889 break;
3890 SM_STEP_RUN(WPA_PTK_GROUP);
3891 if (sm->pending_deinit)
3892 break;
3893 wpa_group_sm_step(sm->wpa_auth, sm->group);
3894 } while (sm->changed || sm->wpa_auth->group->changed);
3895 sm->in_step_loop = 0;
3896
3897 if (sm->pending_deinit) {
3898 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
3899 "machine deinit for " MACSTR, MAC2STR(sm->addr));
3900 wpa_free_sta_sm(sm);
3901 return 1;
3902 }
3903 return 0;
3904 }
3905
3906
3907 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
3908 {
3909 struct wpa_state_machine *sm = eloop_ctx;
3910 wpa_sm_step(sm);
3911 }
3912
3913
3914 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
3915 {
3916 if (sm == NULL)
3917 return;
3918 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
3919 }
3920
3921
3922 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
3923 {
3924 int tmp, i;
3925 struct wpa_group *group;
3926
3927 if (wpa_auth == NULL)
3928 return;
3929
3930 group = wpa_auth->group;
3931
3932 for (i = 0; i < 2; i++) {
3933 tmp = group->GM;
3934 group->GM = group->GN;
3935 group->GN = tmp;
3936 #ifdef CONFIG_IEEE80211W
3937 tmp = group->GM_igtk;
3938 group->GM_igtk = group->GN_igtk;
3939 group->GN_igtk = tmp;
3940 #endif /* CONFIG_IEEE80211W */
3941 wpa_gtk_update(wpa_auth, group);
3942 wpa_group_config_group_keys(wpa_auth, group);
3943 }
3944 }
3945
3946
3947 static const char * wpa_bool_txt(int val)
3948 {
3949 return val ? "TRUE" : "FALSE";
3950 }
3951
3952
3953 #define RSN_SUITE "%02x-%02x-%02x-%d"
3954 #define RSN_SUITE_ARG(s) \
3955 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3956
3957 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
3958 {
3959 int len = 0, ret;
3960 char pmkid_txt[PMKID_LEN * 2 + 1];
3961 #ifdef CONFIG_RSN_PREAUTH
3962 const int preauth = 1;
3963 #else /* CONFIG_RSN_PREAUTH */
3964 const int preauth = 0;
3965 #endif /* CONFIG_RSN_PREAUTH */
3966
3967 if (wpa_auth == NULL)
3968 return len;
3969
3970 ret = os_snprintf(buf + len, buflen - len,
3971 "dot11RSNAOptionImplemented=TRUE\n"
3972 "dot11RSNAPreauthenticationImplemented=%s\n"
3973 "dot11RSNAEnabled=%s\n"
3974 "dot11RSNAPreauthenticationEnabled=%s\n",
3975 wpa_bool_txt(preauth),
3976 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
3977 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
3978 if (os_snprintf_error(buflen - len, ret))
3979 return len;
3980 len += ret;
3981
3982 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
3983 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
3984
3985 ret = os_snprintf(
3986 buf + len, buflen - len,
3987 "dot11RSNAConfigVersion=%u\n"
3988 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
3989 /* FIX: dot11RSNAConfigGroupCipher */
3990 /* FIX: dot11RSNAConfigGroupRekeyMethod */
3991 /* FIX: dot11RSNAConfigGroupRekeyTime */
3992 /* FIX: dot11RSNAConfigGroupRekeyPackets */
3993 "dot11RSNAConfigGroupRekeyStrict=%u\n"
3994 "dot11RSNAConfigGroupUpdateCount=%u\n"
3995 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
3996 "dot11RSNAConfigGroupCipherSize=%u\n"
3997 "dot11RSNAConfigPMKLifetime=%u\n"
3998 "dot11RSNAConfigPMKReauthThreshold=%u\n"
3999 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4000 "dot11RSNAConfigSATimeout=%u\n"
4001 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
4002 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
4003 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
4004 "dot11RSNAPMKIDUsed=%s\n"
4005 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
4006 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
4007 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
4008 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4009 "dot11RSNA4WayHandshakeFailures=%u\n"
4010 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4011 RSN_VERSION,
4012 !!wpa_auth->conf.wpa_strict_rekey,
4013 wpa_auth->conf.wpa_group_update_count,
4014 wpa_auth->conf.wpa_pairwise_update_count,
4015 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
4016 dot11RSNAConfigPMKLifetime,
4017 dot11RSNAConfigPMKReauthThreshold,
4018 dot11RSNAConfigSATimeout,
4019 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
4020 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
4021 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
4022 pmkid_txt,
4023 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
4024 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
4025 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
4026 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
4027 wpa_auth->dot11RSNA4WayHandshakeFailures);
4028 if (os_snprintf_error(buflen - len, ret))
4029 return len;
4030 len += ret;
4031
4032 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
4033 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4034
4035 /* Private MIB */
4036 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
4037 wpa_auth->group->wpa_group_state);
4038 if (os_snprintf_error(buflen - len, ret))
4039 return len;
4040 len += ret;
4041
4042 return len;
4043 }
4044
4045
4046 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
4047 {
4048 int len = 0, ret;
4049 u32 pairwise = 0;
4050
4051 if (sm == NULL)
4052 return 0;
4053
4054 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4055
4056 /* dot11RSNAStatsEntry */
4057
4058 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
4059 WPA_PROTO_RSN : WPA_PROTO_WPA,
4060 sm->pairwise);
4061 if (pairwise == 0)
4062 return 0;
4063
4064 ret = os_snprintf(
4065 buf + len, buflen - len,
4066 /* TODO: dot11RSNAStatsIndex */
4067 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
4068 "dot11RSNAStatsVersion=1\n"
4069 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
4070 /* TODO: dot11RSNAStatsTKIPICVErrors */
4071 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
4072 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
4073 /* TODO: dot11RSNAStatsCCMPReplays */
4074 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
4075 /* TODO: dot11RSNAStatsTKIPReplays */,
4076 MAC2STR(sm->addr),
4077 RSN_SUITE_ARG(pairwise),
4078 sm->dot11RSNAStatsTKIPLocalMICFailures,
4079 sm->dot11RSNAStatsTKIPRemoteMICFailures);
4080 if (os_snprintf_error(buflen - len, ret))
4081 return len;
4082 len += ret;
4083
4084 /* Private MIB */
4085 ret = os_snprintf(buf + len, buflen - len,
4086 "hostapdWPAPTKState=%d\n"
4087 "hostapdWPAPTKGroupState=%d\n",
4088 sm->wpa_ptk_state,
4089 sm->wpa_ptk_group_state);
4090 if (os_snprintf_error(buflen - len, ret))
4091 return len;
4092 len += ret;
4093
4094 return len;
4095 }
4096
4097
4098 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
4099 {
4100 if (wpa_auth)
4101 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
4102 }
4103
4104
4105 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
4106 {
4107 return sm && sm->pairwise_set;
4108 }
4109
4110
4111 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
4112 {
4113 return sm->pairwise;
4114 }
4115
4116
4117 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
4118 {
4119 if (sm == NULL)
4120 return -1;
4121 return sm->wpa_key_mgmt;
4122 }
4123
4124
4125 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
4126 {
4127 if (sm == NULL)
4128 return 0;
4129 return sm->wpa;
4130 }
4131
4132
4133 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
4134 {
4135 if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
4136 return 0;
4137 return sm->tk_already_set;
4138 }
4139
4140
4141 int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm)
4142 {
4143 if (!sm || !wpa_key_mgmt_fils(sm->wpa_key_mgmt))
4144 return 0;
4145 return sm->tk_already_set;
4146 }
4147
4148
4149 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
4150 struct rsn_pmksa_cache_entry *entry)
4151 {
4152 if (sm == NULL || sm->pmksa != entry)
4153 return -1;
4154 sm->pmksa = NULL;
4155 return 0;
4156 }
4157
4158
4159 struct rsn_pmksa_cache_entry *
4160 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
4161 {
4162 return sm ? sm->pmksa : NULL;
4163 }
4164
4165
4166 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
4167 {
4168 if (sm)
4169 sm->dot11RSNAStatsTKIPLocalMICFailures++;
4170 }
4171
4172
4173 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
4174 {
4175 if (wpa_auth == NULL)
4176 return NULL;
4177 *len = wpa_auth->wpa_ie_len;
4178 return wpa_auth->wpa_ie;
4179 }
4180
4181
4182 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
4183 unsigned int pmk_len,
4184 int session_timeout, struct eapol_state_machine *eapol)
4185 {
4186 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
4187 sm->wpa_auth->conf.disable_pmksa_caching)
4188 return -1;
4189
4190 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
4191 if (pmk_len > PMK_LEN_SUITE_B_192)
4192 pmk_len = PMK_LEN_SUITE_B_192;
4193 } else if (pmk_len > PMK_LEN) {
4194 pmk_len = PMK_LEN;
4195 }
4196
4197 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
4198 sm->PTK.kck, sm->PTK.kck_len,
4199 sm->wpa_auth->addr, sm->addr, session_timeout,
4200 eapol, sm->wpa_key_mgmt))
4201 return 0;
4202
4203 return -1;
4204 }
4205
4206
4207 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
4208 const u8 *pmk, size_t len, const u8 *sta_addr,
4209 int session_timeout,
4210 struct eapol_state_machine *eapol)
4211 {
4212 if (wpa_auth == NULL)
4213 return -1;
4214
4215 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
4216 NULL, 0,
4217 wpa_auth->addr,
4218 sta_addr, session_timeout, eapol,
4219 WPA_KEY_MGMT_IEEE8021X))
4220 return 0;
4221
4222 return -1;
4223 }
4224
4225
4226 int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
4227 const u8 *pmk, const u8 *pmkid)
4228 {
4229 if (wpa_auth->conf.disable_pmksa_caching)
4230 return -1;
4231
4232 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
4233 NULL, 0,
4234 wpa_auth->addr, addr, 0, NULL,
4235 WPA_KEY_MGMT_SAE))
4236 return 0;
4237
4238 return -1;
4239 }
4240
4241
4242 void wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid)
4243 {
4244 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
4245 sm->pmkid_set = 1;
4246 }
4247
4248
4249 int wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr,
4250 const u8 *pmk, size_t pmk_len, const u8 *pmkid,
4251 int session_timeout, int akmp)
4252 {
4253 if (wpa_auth->conf.disable_pmksa_caching)
4254 return -1;
4255
4256 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
4257 NULL, 0, wpa_auth->addr, addr, session_timeout,
4258 NULL, akmp))
4259 return 0;
4260
4261 return -1;
4262 }
4263
4264
4265 void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
4266 const u8 *sta_addr)
4267 {
4268 struct rsn_pmksa_cache_entry *pmksa;
4269
4270 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
4271 return;
4272 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
4273 if (pmksa) {
4274 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
4275 MACSTR " based on request", MAC2STR(sta_addr));
4276 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
4277 }
4278 }
4279
4280
4281 int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
4282 size_t len)
4283 {
4284 if (!wpa_auth || !wpa_auth->pmksa)
4285 return 0;
4286 return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
4287 }
4288
4289
4290 void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
4291 {
4292 if (wpa_auth && wpa_auth->pmksa)
4293 pmksa_cache_auth_flush(wpa_auth->pmksa);
4294 }
4295
4296
4297 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4298 #ifdef CONFIG_MESH
4299
4300 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
4301 char *buf, size_t len)
4302 {
4303 if (!wpa_auth || !wpa_auth->pmksa)
4304 return 0;
4305
4306 return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
4307 }
4308
4309
4310 struct rsn_pmksa_cache_entry *
4311 wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
4312 const u8 *pmkid, int expiration)
4313 {
4314 struct rsn_pmksa_cache_entry *entry;
4315 struct os_reltime now;
4316
4317 entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa,
4318 spa, 0, NULL, WPA_KEY_MGMT_SAE);
4319 if (!entry)
4320 return NULL;
4321
4322 os_get_reltime(&now);
4323 entry->expiration = now.sec + expiration;
4324 return entry;
4325 }
4326
4327
4328 int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
4329 struct rsn_pmksa_cache_entry *entry)
4330 {
4331 int ret;
4332
4333 if (!wpa_auth || !wpa_auth->pmksa)
4334 return -1;
4335
4336 ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
4337 if (ret < 0)
4338 wpa_printf(MSG_DEBUG,
4339 "RSN: Failed to store external PMKSA cache for "
4340 MACSTR, MAC2STR(entry->spa));
4341
4342 return ret;
4343 }
4344
4345 #endif /* CONFIG_MESH */
4346 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4347
4348
4349 struct rsn_pmksa_cache_entry *
4350 wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
4351 const u8 *pmkid)
4352 {
4353 if (!wpa_auth || !wpa_auth->pmksa)
4354 return NULL;
4355 return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
4356 }
4357
4358
4359 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
4360 struct wpa_state_machine *sm,
4361 struct wpa_authenticator *wpa_auth,
4362 u8 *pmkid, u8 *pmk)
4363 {
4364 if (!sm)
4365 return;
4366
4367 sm->pmksa = pmksa;
4368 os_memcpy(pmk, pmksa->pmk, PMK_LEN);
4369 os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
4370 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
4371 }
4372
4373
4374 /*
4375 * Remove and free the group from wpa_authenticator. This is triggered by a
4376 * callback to make sure nobody is currently iterating the group list while it
4377 * gets modified.
4378 */
4379 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
4380 struct wpa_group *group)
4381 {
4382 struct wpa_group *prev = wpa_auth->group;
4383
4384 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
4385 group->vlan_id);
4386
4387 while (prev) {
4388 if (prev->next == group) {
4389 /* This never frees the special first group as needed */
4390 prev->next = group->next;
4391 os_free(group);
4392 break;
4393 }
4394 prev = prev->next;
4395 }
4396
4397 }
4398
4399
4400 /* Increase the reference counter for group */
4401 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
4402 struct wpa_group *group)
4403 {
4404 /* Skip the special first group */
4405 if (wpa_auth->group == group)
4406 return;
4407
4408 group->references++;
4409 }
4410
4411
4412 /* Decrease the reference counter and maybe free the group */
4413 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
4414 struct wpa_group *group)
4415 {
4416 /* Skip the special first group */
4417 if (wpa_auth->group == group)
4418 return;
4419
4420 group->references--;
4421 if (group->references)
4422 return;
4423 wpa_group_free(wpa_auth, group);
4424 }
4425
4426
4427 /*
4428 * Add a group that has its references counter set to zero. Caller needs to
4429 * call wpa_group_get() on the return value to mark the entry in use.
4430 */
4431 static struct wpa_group *
4432 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4433 {
4434 struct wpa_group *group;
4435
4436 if (wpa_auth == NULL || wpa_auth->group == NULL)
4437 return NULL;
4438
4439 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
4440 vlan_id);
4441 group = wpa_group_init(wpa_auth, vlan_id, 0);
4442 if (group == NULL)
4443 return NULL;
4444
4445 group->next = wpa_auth->group->next;
4446 wpa_auth->group->next = group;
4447
4448 return group;
4449 }
4450
4451
4452 /*
4453 * Enforce that the group state machine for the VLAN is running, increase
4454 * reference counter as interface is up. References might have been increased
4455 * even if a negative value is returned.
4456 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4457 */
4458 int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4459 {
4460 struct wpa_group *group;
4461
4462 if (wpa_auth == NULL)
4463 return 0;
4464
4465 group = wpa_auth->group;
4466 while (group) {
4467 if (group->vlan_id == vlan_id)
4468 break;
4469 group = group->next;
4470 }
4471
4472 if (group == NULL) {
4473 group = wpa_auth_add_group(wpa_auth, vlan_id);
4474 if (group == NULL)
4475 return -1;
4476 }
4477
4478 wpa_printf(MSG_DEBUG,
4479 "WPA: Ensure group state machine running for VLAN ID %d",
4480 vlan_id);
4481
4482 wpa_group_get(wpa_auth, group);
4483 group->num_setup_iface++;
4484
4485 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4486 return -1;
4487
4488 return 0;
4489 }
4490
4491
4492 /*
4493 * Decrease reference counter, expected to be zero afterwards.
4494 * returns: -1 on error (group not found, group in fail state)
4495 * -2 if wpa_group is still referenced
4496 * 0 else
4497 */
4498 int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4499 {
4500 struct wpa_group *group;
4501 int ret = 0;
4502
4503 if (wpa_auth == NULL)
4504 return 0;
4505
4506 group = wpa_auth->group;
4507 while (group) {
4508 if (group->vlan_id == vlan_id)
4509 break;
4510 group = group->next;
4511 }
4512
4513 if (group == NULL)
4514 return -1;
4515
4516 wpa_printf(MSG_DEBUG,
4517 "WPA: Try stopping group state machine for VLAN ID %d",
4518 vlan_id);
4519
4520 if (group->num_setup_iface <= 0) {
4521 wpa_printf(MSG_ERROR,
4522 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4523 vlan_id);
4524 return -1;
4525 }
4526 group->num_setup_iface--;
4527
4528 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4529 ret = -1;
4530
4531 if (group->references > 1) {
4532 wpa_printf(MSG_DEBUG,
4533 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4534 vlan_id);
4535 ret = -2;
4536 }
4537
4538 wpa_group_put(wpa_auth, group);
4539
4540 return ret;
4541 }
4542
4543
4544 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
4545 {
4546 struct wpa_group *group;
4547
4548 if (sm == NULL || sm->wpa_auth == NULL)
4549 return 0;
4550
4551 group = sm->wpa_auth->group;
4552 while (group) {
4553 if (group->vlan_id == vlan_id)
4554 break;
4555 group = group->next;
4556 }
4557
4558 if (group == NULL) {
4559 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
4560 if (group == NULL)
4561 return -1;
4562 }
4563
4564 if (sm->group == group)
4565 return 0;
4566
4567 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4568 return -1;
4569
4570 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
4571 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
4572
4573 wpa_group_get(sm->wpa_auth, group);
4574 wpa_group_put(sm->wpa_auth, sm->group);
4575 sm->group = group;
4576
4577 return 0;
4578 }
4579
4580
4581 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
4582 struct wpa_state_machine *sm, int ack)
4583 {
4584 if (wpa_auth == NULL || sm == NULL)
4585 return;
4586 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
4587 " ack=%d", MAC2STR(sm->addr), ack);
4588 if (sm->pending_1_of_4_timeout && ack) {
4589 /*
4590 * Some deployed supplicant implementations update their SNonce
4591 * for each EAPOL-Key 2/4 message even within the same 4-way
4592 * handshake and then fail to use the first SNonce when
4593 * deriving the PTK. This results in unsuccessful 4-way
4594 * handshake whenever the relatively short initial timeout is
4595 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4596 * around this by increasing the timeout now that we know that
4597 * the station has received the frame.
4598 */
4599 int timeout_ms = eapol_key_timeout_subseq;
4600 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
4601 "timeout by %u ms because of acknowledged frame",
4602 timeout_ms);
4603 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
4604 eloop_register_timeout(timeout_ms / 1000,
4605 (timeout_ms % 1000) * 1000,
4606 wpa_send_eapol_timeout, wpa_auth, sm);
4607 }
4608
4609 #ifdef CONFIG_TESTING_OPTIONS
4610 if (sm->eapol_status_cb) {
4611 sm->eapol_status_cb(sm->eapol_status_cb_ctx1,
4612 sm->eapol_status_cb_ctx2);
4613 sm->eapol_status_cb = NULL;
4614 }
4615 #endif /* CONFIG_TESTING_OPTIONS */
4616 }
4617
4618
4619 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
4620 {
4621 if (sm == NULL)
4622 return 0;
4623 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
4624 }
4625
4626
4627 int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
4628 {
4629 if (sm == NULL)
4630 return 0;
4631 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
4632 }
4633
4634
4635 #ifdef CONFIG_P2P
4636 int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
4637 {
4638 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
4639 return -1;
4640 os_memcpy(addr, sm->ip_addr, 4);
4641 return 0;
4642 }
4643 #endif /* CONFIG_P2P */
4644
4645
4646 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
4647 struct radius_das_attrs *attr)
4648 {
4649 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
4650 }
4651
4652
4653 void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
4654 {
4655 struct wpa_group *group;
4656
4657 if (!wpa_auth)
4658 return;
4659 for (group = wpa_auth->group; group; group = group->next)
4660 wpa_group_config_group_keys(wpa_auth, group);
4661 }
4662
4663
4664 #ifdef CONFIG_FILS
4665
4666 struct wpa_auth_fils_iter_data {
4667 struct wpa_authenticator *auth;
4668 const u8 *cache_id;
4669 struct rsn_pmksa_cache_entry *pmksa;
4670 const u8 *spa;
4671 const u8 *pmkid;
4672 };
4673
4674
4675 static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
4676 {
4677 struct wpa_auth_fils_iter_data *data = ctx;
4678
4679 if (a == data->auth || !a->conf.fils_cache_id_set ||
4680 os_memcmp(a->conf.fils_cache_id, data->cache_id,
4681 FILS_CACHE_ID_LEN) != 0)
4682 return 0;
4683 data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
4684 return data->pmksa != NULL;
4685 }
4686
4687
4688 struct rsn_pmksa_cache_entry *
4689 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
4690 const u8 *sta_addr, const u8 *pmkid)
4691 {
4692 struct wpa_auth_fils_iter_data idata;
4693
4694 if (!wpa_auth->conf.fils_cache_id_set)
4695 return NULL;
4696 idata.auth = wpa_auth;
4697 idata.cache_id = wpa_auth->conf.fils_cache_id;
4698 idata.pmksa = NULL;
4699 idata.spa = sta_addr;
4700 idata.pmkid = pmkid;
4701 wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
4702 return idata.pmksa;
4703 }
4704
4705
4706 #ifdef CONFIG_IEEE80211R_AP
4707 int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, int use_sha384,
4708 u8 *buf, size_t len)
4709 {
4710 struct wpa_auth_config *conf = &wpa_auth->conf;
4711
4712 return wpa_write_ftie(conf, use_sha384, conf->r0_key_holder,
4713 conf->r0_key_holder_len,
4714 NULL, NULL, buf, len, NULL, 0);
4715 }
4716 #endif /* CONFIG_IEEE80211R_AP */
4717
4718
4719 void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm,
4720 u8 *fils_anonce, u8 *fils_snonce,
4721 u8 *fils_kek, size_t *fils_kek_len)
4722 {
4723 os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN);
4724 os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN);
4725 os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN);
4726 *fils_kek_len = sm->PTK.kek_len;
4727 }
4728
4729 #endif /* CONFIG_FILS */
4730
4731
4732 #ifdef CONFIG_TESTING_OPTIONS
4733
4734 int wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce,
4735 void (*cb)(void *ctx1, void *ctx2),
4736 void *ctx1, void *ctx2)
4737 {
4738 const u8 *anonce = sm->ANonce;
4739 u8 anonce_buf[WPA_NONCE_LEN];
4740
4741 if (change_anonce) {
4742 if (random_get_bytes(anonce_buf, WPA_NONCE_LEN))
4743 return -1;
4744 anonce = anonce_buf;
4745 }
4746
4747 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4748 "sending 1/4 msg of 4-Way Handshake (TESTING)");
4749 wpa_send_eapol(sm->wpa_auth, sm,
4750 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
4751 anonce, NULL, 0, 0, 0);
4752 return 0;
4753 }
4754
4755
4756 int wpa_auth_resend_m3(struct wpa_state_machine *sm,
4757 void (*cb)(void *ctx1, void *ctx2),
4758 void *ctx1, void *ctx2)
4759 {
4760 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
4761 #ifdef CONFIG_IEEE80211W
4762 u8 *opos;
4763 #endif /* CONFIG_IEEE80211W */
4764 size_t gtk_len, kde_len;
4765 struct wpa_group *gsm = sm->group;
4766 u8 *wpa_ie;
4767 int wpa_ie_len, secure, keyidx, encr = 0;
4768
4769 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
4770 GTK[GN], IGTK, [FTIE], [TIE * 2])
4771 */
4772
4773 /* Use 0 RSC */
4774 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
4775 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
4776 wpa_ie = sm->wpa_auth->wpa_ie;
4777 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
4778 if (sm->wpa == WPA_VERSION_WPA &&
4779 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
4780 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
4781 /* WPA-only STA, remove RSN IE and possible MDIE */
4782 wpa_ie = wpa_ie + wpa_ie[1] + 2;
4783 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
4784 wpa_ie = wpa_ie + wpa_ie[1] + 2;
4785 wpa_ie_len = wpa_ie[1] + 2;
4786 }
4787 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4788 "sending 3/4 msg of 4-Way Handshake (TESTING)");
4789 if (sm->wpa == WPA_VERSION_WPA2) {
4790 /* WPA2 send GTK in the 4-way handshake */
4791 secure = 1;
4792 gtk = gsm->GTK[gsm->GN - 1];
4793 gtk_len = gsm->GTK_len;
4794 keyidx = gsm->GN;
4795 _rsc = rsc;
4796 encr = 1;
4797 } else {
4798 /* WPA does not include GTK in msg 3/4 */
4799 secure = 0;
4800 gtk = NULL;
4801 gtk_len = 0;
4802 keyidx = 0;
4803 _rsc = NULL;
4804 if (sm->rx_eapol_key_secure) {
4805 /*
4806 * It looks like Windows 7 supplicant tries to use
4807 * Secure bit in msg 2/4 after having reported Michael
4808 * MIC failure and it then rejects the 4-way handshake
4809 * if msg 3/4 does not set Secure bit. Work around this
4810 * by setting the Secure bit here even in the case of
4811 * WPA if the supplicant used it first.
4812 */
4813 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4814 "STA used Secure bit in WPA msg 2/4 - "
4815 "set Secure for 3/4 as workaround");
4816 secure = 1;
4817 }
4818 }
4819
4820 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
4821 if (gtk)
4822 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
4823 #ifdef CONFIG_IEEE80211R_AP
4824 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4825 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
4826 kde_len += 300; /* FTIE + 2 * TIE */
4827 }
4828 #endif /* CONFIG_IEEE80211R_AP */
4829 kde = os_malloc(kde_len);
4830 if (kde == NULL)
4831 return -1;
4832
4833 pos = kde;
4834 os_memcpy(pos, wpa_ie, wpa_ie_len);
4835 pos += wpa_ie_len;
4836 #ifdef CONFIG_IEEE80211R_AP
4837 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4838 int res;
4839 size_t elen;
4840
4841 elen = pos - kde;
4842 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
4843 if (res < 0) {
4844 wpa_printf(MSG_ERROR, "FT: Failed to insert "
4845 "PMKR1Name into RSN IE in EAPOL-Key data");
4846 os_free(kde);
4847 return -1;
4848 }
4849 pos -= wpa_ie_len;
4850 pos += elen;
4851 }
4852 #endif /* CONFIG_IEEE80211R_AP */
4853 if (gtk) {
4854 u8 hdr[2];
4855 hdr[0] = keyidx & 0x03;
4856 hdr[1] = 0;
4857 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
4858 gtk, gtk_len);
4859 }
4860 #ifdef CONFIG_IEEE80211W
4861 opos = pos;
4862 pos = ieee80211w_kde_add(sm, pos);
4863 if (pos - opos >= 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
4864 /* skip KDE header and keyid */
4865 opos += 2 + RSN_SELECTOR_LEN + 2;
4866 os_memset(opos, 0, 6); /* clear PN */
4867 }
4868 #endif /* CONFIG_IEEE80211W */
4869 if (ocv_oci_add(sm, &pos) < 0) {
4870 os_free(kde);
4871 return -1;
4872 }
4873
4874 #ifdef CONFIG_IEEE80211R_AP
4875 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4876 int res;
4877 struct wpa_auth_config *conf;
4878
4879 conf = &sm->wpa_auth->conf;
4880 if (sm->assoc_resp_ftie &&
4881 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
4882 os_memcpy(pos, sm->assoc_resp_ftie,
4883 2 + sm->assoc_resp_ftie[1]);
4884 res = 2 + sm->assoc_resp_ftie[1];
4885 } else {
4886 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
4887
4888 res = wpa_write_ftie(conf, use_sha384,
4889 conf->r0_key_holder,
4890 conf->r0_key_holder_len,
4891 NULL, NULL, pos,
4892 kde + kde_len - pos,
4893 NULL, 0);
4894 }
4895 if (res < 0) {
4896 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
4897 "into EAPOL-Key Key Data");
4898 os_free(kde);
4899 return -1;
4900 }
4901 pos += res;
4902
4903 /* TIE[ReassociationDeadline] (TU) */
4904 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
4905 *pos++ = 5;
4906 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
4907 WPA_PUT_LE32(pos, conf->reassociation_deadline);
4908 pos += 4;
4909
4910 /* TIE[KeyLifetime] (seconds) */
4911 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
4912 *pos++ = 5;
4913 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
4914 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
4915 pos += 4;
4916 }
4917 #endif /* CONFIG_IEEE80211R_AP */
4918
4919 wpa_send_eapol(sm->wpa_auth, sm,
4920 (secure ? WPA_KEY_INFO_SECURE : 0) |
4921 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
4922 WPA_KEY_INFO_MIC : 0) |
4923 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
4924 WPA_KEY_INFO_KEY_TYPE,
4925 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
4926 os_free(kde);
4927 return 0;
4928 }
4929
4930
4931 int wpa_auth_resend_group_m1(struct wpa_state_machine *sm,
4932 void (*cb)(void *ctx1, void *ctx2),
4933 void *ctx1, void *ctx2)
4934 {
4935 u8 rsc[WPA_KEY_RSC_LEN];
4936 struct wpa_group *gsm = sm->group;
4937 const u8 *kde;
4938 u8 *kde_buf = NULL, *pos, hdr[2];
4939 #ifdef CONFIG_IEEE80211W
4940 u8 *opos;
4941 #endif /* CONFIG_IEEE80211W */
4942 size_t kde_len;
4943 u8 *gtk;
4944
4945 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
4946 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
4947 /* Use 0 RSC */
4948 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4949 "sending 1/2 msg of Group Key Handshake (TESTING)");
4950
4951 gtk = gsm->GTK[gsm->GN - 1];
4952 if (sm->wpa == WPA_VERSION_WPA2) {
4953 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
4954 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
4955 kde_buf = os_malloc(kde_len);
4956 if (kde_buf == NULL)
4957 return -1;
4958
4959 kde = pos = kde_buf;
4960 hdr[0] = gsm->GN & 0x03;
4961 hdr[1] = 0;
4962 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
4963 gtk, gsm->GTK_len);
4964 #ifdef CONFIG_IEEE80211W
4965 opos = pos;
4966 pos = ieee80211w_kde_add(sm, pos);
4967 if (pos - opos >=
4968 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
4969 /* skip KDE header and keyid */
4970 opos += 2 + RSN_SELECTOR_LEN + 2;
4971 os_memset(opos, 0, 6); /* clear PN */
4972 }
4973 #endif /* CONFIG_IEEE80211W */
4974 if (ocv_oci_add(sm, &pos) < 0) {
4975 os_free(kde_buf);
4976 return -1;
4977 }
4978 kde_len = pos - kde;
4979 } else {
4980 kde = gtk;
4981 kde_len = gsm->GTK_len;
4982 }
4983
4984 sm->eapol_status_cb = cb;
4985 sm->eapol_status_cb_ctx1 = ctx1;
4986 sm->eapol_status_cb_ctx2 = ctx2;
4987
4988 wpa_send_eapol(sm->wpa_auth, sm,
4989 WPA_KEY_INFO_SECURE |
4990 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
4991 WPA_KEY_INFO_MIC : 0) |
4992 WPA_KEY_INFO_ACK |
4993 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
4994 rsc, NULL, kde, kde_len, gsm->GN, 1);
4995
4996 os_free(kde_buf);
4997 return 0;
4998 }
4999
5000
5001 int wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth)
5002 {
5003 if (!wpa_auth)
5004 return -1;
5005 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
5006 return eloop_register_timeout(0, 0, wpa_rekey_gtk, wpa_auth, NULL);
5007 }
5008
5009 #endif /* CONFIG_TESTING_OPTIONS */