]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/wpa_auth.c
Print the algorithms used for EAPOL-Key professing in log
[thirdparty/hostap.git] / src / ap / wpa_auth.c
1 /*
2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2015, 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 "crypto/aes.h"
17 #include "crypto/aes_wrap.h"
18 #include "crypto/aes_siv.h"
19 #include "crypto/crypto.h"
20 #include "crypto/sha1.h"
21 #include "crypto/sha256.h"
22 #include "crypto/random.h"
23 #include "eapol_auth/eapol_auth_sm.h"
24 #include "ap_config.h"
25 #include "ieee802_11.h"
26 #include "wpa_auth.h"
27 #include "pmksa_cache_auth.h"
28 #include "wpa_auth_i.h"
29 #include "wpa_auth_ie.h"
30
31 #define STATE_MACHINE_DATA struct wpa_state_machine
32 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
33 #define STATE_MACHINE_ADDR sm->addr
34
35
36 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
37 static int wpa_sm_step(struct wpa_state_machine *sm);
38 static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
39 size_t data_len);
40 #ifdef CONFIG_FILS
41 static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
42 u8 *buf, size_t buf_len, u16 *_key_data_len);
43 #endif /* CONFIG_FILS */
44 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
45 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
46 struct wpa_group *group);
47 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
48 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
49 struct wpa_group *group);
50 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
51 struct wpa_group *group);
52 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
53 const u8 *pmk, unsigned int pmk_len,
54 struct wpa_ptk *ptk);
55 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
56 struct wpa_group *group);
57 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
58 struct wpa_group *group);
59 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
60 struct wpa_group *group);
61 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos);
62
63 static const u32 eapol_key_timeout_first = 100; /* ms */
64 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
65 static const u32 eapol_key_timeout_first_group = 500; /* ms */
66
67 /* TODO: make these configurable */
68 static const int dot11RSNAConfigPMKLifetime = 43200;
69 static const int dot11RSNAConfigPMKReauthThreshold = 70;
70 static const int dot11RSNAConfigSATimeout = 60;
71
72
73 static inline int wpa_auth_mic_failure_report(
74 struct wpa_authenticator *wpa_auth, const u8 *addr)
75 {
76 if (wpa_auth->cb->mic_failure_report)
77 return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr);
78 return 0;
79 }
80
81
82 static inline void wpa_auth_psk_failure_report(
83 struct wpa_authenticator *wpa_auth, const u8 *addr)
84 {
85 if (wpa_auth->cb->psk_failure_report)
86 wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr);
87 }
88
89
90 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
91 const u8 *addr, wpa_eapol_variable var,
92 int value)
93 {
94 if (wpa_auth->cb->set_eapol)
95 wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value);
96 }
97
98
99 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
100 const u8 *addr, wpa_eapol_variable var)
101 {
102 if (wpa_auth->cb->get_eapol == NULL)
103 return -1;
104 return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var);
105 }
106
107
108 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
109 const u8 *addr,
110 const u8 *p2p_dev_addr,
111 const u8 *prev_psk)
112 {
113 if (wpa_auth->cb->get_psk == NULL)
114 return NULL;
115 return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
116 prev_psk);
117 }
118
119
120 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
121 const u8 *addr, u8 *msk, size_t *len)
122 {
123 if (wpa_auth->cb->get_msk == NULL)
124 return -1;
125 return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
126 }
127
128
129 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
130 int vlan_id,
131 enum wpa_alg alg, const u8 *addr, int idx,
132 u8 *key, size_t key_len)
133 {
134 if (wpa_auth->cb->set_key == NULL)
135 return -1;
136 return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
137 key, key_len);
138 }
139
140
141 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
142 const u8 *addr, int idx, u8 *seq)
143 {
144 if (wpa_auth->cb->get_seqnum == NULL)
145 return -1;
146 return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
147 }
148
149
150 static inline int
151 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
152 const u8 *data, size_t data_len, int encrypt)
153 {
154 if (wpa_auth->cb->send_eapol == NULL)
155 return -1;
156 return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
157 encrypt);
158 }
159
160
161 #ifdef CONFIG_MESH
162 static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
163 const u8 *addr)
164 {
165 if (wpa_auth->cb->start_ampe == NULL)
166 return -1;
167 return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
168 }
169 #endif /* CONFIG_MESH */
170
171
172 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
173 int (*cb)(struct wpa_state_machine *sm, void *ctx),
174 void *cb_ctx)
175 {
176 if (wpa_auth->cb->for_each_sta == NULL)
177 return 0;
178 return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
179 }
180
181
182 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
183 int (*cb)(struct wpa_authenticator *a, void *ctx),
184 void *cb_ctx)
185 {
186 if (wpa_auth->cb->for_each_auth == NULL)
187 return 0;
188 return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
189 }
190
191
192 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
193 logger_level level, const char *txt)
194 {
195 if (wpa_auth->cb->logger == NULL)
196 return;
197 wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
198 }
199
200
201 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
202 logger_level level, const char *fmt, ...)
203 {
204 char *format;
205 int maxlen;
206 va_list ap;
207
208 if (wpa_auth->cb->logger == NULL)
209 return;
210
211 maxlen = os_strlen(fmt) + 100;
212 format = os_malloc(maxlen);
213 if (!format)
214 return;
215
216 va_start(ap, fmt);
217 vsnprintf(format, maxlen, fmt, ap);
218 va_end(ap);
219
220 wpa_auth_logger(wpa_auth, addr, level, format);
221
222 os_free(format);
223 }
224
225
226 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
227 const u8 *addr)
228 {
229 if (wpa_auth->cb->disconnect == NULL)
230 return;
231 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
232 wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr,
233 WLAN_REASON_PREV_AUTH_NOT_VALID);
234 }
235
236
237 static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
238 {
239 int ret = 0;
240 #ifdef CONFIG_IEEE80211R_AP
241 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
242 ret = 1;
243 #endif /* CONFIG_IEEE80211R_AP */
244 #ifdef CONFIG_IEEE80211W
245 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
246 ret = 1;
247 #endif /* CONFIG_IEEE80211W */
248 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
249 ret = 1;
250 return ret;
251 }
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 #endif /* CONFIG_IEEE80211R_AP */
522
523 #ifdef CONFIG_P2P
524 bitfield_free(wpa_auth->ip_pool);
525 #endif /* CONFIG_P2P */
526
527
528 os_free(wpa_auth->wpa_ie);
529
530 group = wpa_auth->group;
531 while (group) {
532 prev = group;
533 group = group->next;
534 os_free(prev);
535 }
536
537 os_free(wpa_auth);
538 }
539
540
541 /**
542 * wpa_reconfig - Update WPA authenticator configuration
543 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
544 * @conf: Configuration for WPA authenticator
545 */
546 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
547 struct wpa_auth_config *conf)
548 {
549 struct wpa_group *group;
550 if (wpa_auth == NULL)
551 return 0;
552
553 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
554 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
555 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
556 return -1;
557 }
558
559 /*
560 * Reinitialize GTK to make sure it is suitable for the new
561 * configuration.
562 */
563 group = wpa_auth->group;
564 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
565 group->GInit = TRUE;
566 wpa_group_sm_step(wpa_auth, group);
567 group->GInit = FALSE;
568 wpa_group_sm_step(wpa_auth, group);
569
570 return 0;
571 }
572
573
574 struct wpa_state_machine *
575 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
576 const u8 *p2p_dev_addr)
577 {
578 struct wpa_state_machine *sm;
579
580 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
581 return NULL;
582
583 sm = os_zalloc(sizeof(struct wpa_state_machine));
584 if (sm == NULL)
585 return NULL;
586 os_memcpy(sm->addr, addr, ETH_ALEN);
587 if (p2p_dev_addr)
588 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
589
590 sm->wpa_auth = wpa_auth;
591 sm->group = wpa_auth->group;
592 wpa_group_get(sm->wpa_auth, sm->group);
593
594 return sm;
595 }
596
597
598 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
599 struct wpa_state_machine *sm)
600 {
601 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
602 return -1;
603
604 #ifdef CONFIG_IEEE80211R_AP
605 if (sm->ft_completed) {
606 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
607 "FT authentication already completed - do not "
608 "start 4-way handshake");
609 /* Go to PTKINITDONE state to allow GTK rekeying */
610 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
611 sm->Pair = TRUE;
612 return 0;
613 }
614 #endif /* CONFIG_IEEE80211R_AP */
615
616 #ifdef CONFIG_FILS
617 if (sm->fils_completed) {
618 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
619 "FILS authentication already completed - do not start 4-way handshake");
620 /* Go to PTKINITDONE state to allow GTK rekeying */
621 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
622 sm->Pair = TRUE;
623 return 0;
624 }
625 #endif /* CONFIG_FILS */
626
627 if (sm->started) {
628 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
629 sm->ReAuthenticationRequest = TRUE;
630 return wpa_sm_step(sm);
631 }
632
633 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
634 "start authentication");
635 sm->started = 1;
636
637 sm->Init = TRUE;
638 if (wpa_sm_step(sm) == 1)
639 return 1; /* should not really happen */
640 sm->Init = FALSE;
641 sm->AuthenticationRequest = TRUE;
642 return wpa_sm_step(sm);
643 }
644
645
646 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
647 {
648 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
649 * reassociates back to the same AP while the previous entry for the
650 * STA has not yet been removed. */
651 if (sm == NULL)
652 return;
653
654 sm->wpa_key_mgmt = 0;
655 }
656
657
658 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
659 {
660 #ifdef CONFIG_P2P
661 if (WPA_GET_BE32(sm->ip_addr)) {
662 u32 start;
663 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
664 "address %u.%u.%u.%u from " MACSTR,
665 sm->ip_addr[0], sm->ip_addr[1],
666 sm->ip_addr[2], sm->ip_addr[3],
667 MAC2STR(sm->addr));
668 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
669 bitfield_clear(sm->wpa_auth->ip_pool,
670 WPA_GET_BE32(sm->ip_addr) - start);
671 }
672 #endif /* CONFIG_P2P */
673 if (sm->GUpdateStationKeys) {
674 sm->group->GKeyDoneStations--;
675 sm->GUpdateStationKeys = FALSE;
676 }
677 #ifdef CONFIG_IEEE80211R_AP
678 os_free(sm->assoc_resp_ftie);
679 wpabuf_free(sm->ft_pending_req_ies);
680 #endif /* CONFIG_IEEE80211R_AP */
681 os_free(sm->last_rx_eapol_key);
682 os_free(sm->wpa_ie);
683 wpa_group_put(sm->wpa_auth, sm->group);
684 os_free(sm);
685 }
686
687
688 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
689 {
690 if (sm == NULL)
691 return;
692
693 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
694 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
695 "strict rekeying - force GTK rekey since STA "
696 "is leaving");
697 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
698 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
699 NULL);
700 }
701
702 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
703 sm->pending_1_of_4_timeout = 0;
704 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
705 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
706 if (sm->in_step_loop) {
707 /* Must not free state machine while wpa_sm_step() is running.
708 * Freeing will be completed in the end of wpa_sm_step(). */
709 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
710 "machine deinit for " MACSTR, MAC2STR(sm->addr));
711 sm->pending_deinit = 1;
712 } else
713 wpa_free_sta_sm(sm);
714 }
715
716
717 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
718 {
719 if (sm == NULL)
720 return;
721
722 sm->PTKRequest = TRUE;
723 sm->PTK_valid = 0;
724 }
725
726
727 static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
728 const u8 *replay_counter)
729 {
730 int i;
731 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
732 if (!ctr[i].valid)
733 break;
734 if (os_memcmp(replay_counter, ctr[i].counter,
735 WPA_REPLAY_COUNTER_LEN) == 0)
736 return 1;
737 }
738 return 0;
739 }
740
741
742 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
743 const u8 *replay_counter)
744 {
745 int i;
746 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
747 if (ctr[i].valid &&
748 (replay_counter == NULL ||
749 os_memcmp(replay_counter, ctr[i].counter,
750 WPA_REPLAY_COUNTER_LEN) == 0))
751 ctr[i].valid = FALSE;
752 }
753 }
754
755
756 #ifdef CONFIG_IEEE80211R_AP
757 static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
758 struct wpa_state_machine *sm,
759 struct wpa_eapol_ie_parse *kde)
760 {
761 struct wpa_ie_data ie;
762 struct rsn_mdie *mdie;
763
764 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
765 ie.num_pmkid != 1 || ie.pmkid == NULL) {
766 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
767 "FT 4-way handshake message 2/4");
768 return -1;
769 }
770
771 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
772 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
773 sm->sup_pmk_r1_name, PMKID_LEN);
774
775 if (!kde->mdie || !kde->ftie) {
776 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
777 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
778 return -1;
779 }
780
781 mdie = (struct rsn_mdie *) (kde->mdie + 2);
782 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
783 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
784 MOBILITY_DOMAIN_ID_LEN) != 0) {
785 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
786 return -1;
787 }
788
789 if (sm->assoc_resp_ftie &&
790 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
791 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
792 2 + sm->assoc_resp_ftie[1]) != 0)) {
793 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
794 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
795 kde->ftie, kde->ftie_len);
796 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
797 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
798 return -1;
799 }
800
801 return 0;
802 }
803 #endif /* CONFIG_IEEE80211R_AP */
804
805
806 static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
807 struct wpa_state_machine *sm, int group)
808 {
809 /* Supplicant reported a Michael MIC error */
810 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
811 "received EAPOL-Key Error Request "
812 "(STA detected Michael MIC failure (group=%d))",
813 group);
814
815 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
816 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
817 "ignore Michael MIC failure report since "
818 "group cipher is not TKIP");
819 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
820 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
821 "ignore Michael MIC failure report since "
822 "pairwise cipher is not TKIP");
823 } else {
824 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
825 return 1; /* STA entry was removed */
826 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
827 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
828 }
829
830 /*
831 * Error report is not a request for a new key handshake, but since
832 * Authenticator may do it, let's change the keys now anyway.
833 */
834 wpa_request_new_ptk(sm);
835 return 0;
836 }
837
838
839 static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
840 size_t data_len)
841 {
842 struct wpa_ptk PTK;
843 int ok = 0;
844 const u8 *pmk = NULL;
845 unsigned int pmk_len;
846
847 os_memset(&PTK, 0, sizeof(PTK));
848 for (;;) {
849 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
850 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
851 sm->p2p_dev_addr, pmk);
852 if (pmk == NULL)
853 break;
854 pmk_len = PMK_LEN;
855 } else {
856 pmk = sm->PMK;
857 pmk_len = sm->pmk_len;
858 }
859
860 if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK) < 0)
861 break;
862
863 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len)
864 == 0) {
865 ok = 1;
866 break;
867 }
868
869 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
870 break;
871 }
872
873 if (!ok) {
874 wpa_printf(MSG_DEBUG,
875 "WPA: Earlier SNonce did not result in matching MIC");
876 return -1;
877 }
878
879 wpa_printf(MSG_DEBUG,
880 "WPA: Earlier SNonce resulted in matching MIC");
881 sm->alt_snonce_valid = 0;
882 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
883 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
884 sm->PTK_valid = TRUE;
885
886 return 0;
887 }
888
889
890 void wpa_receive(struct wpa_authenticator *wpa_auth,
891 struct wpa_state_machine *sm,
892 u8 *data, size_t data_len)
893 {
894 struct ieee802_1x_hdr *hdr;
895 struct wpa_eapol_key *key;
896 u16 key_info, key_data_length;
897 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
898 SMK_M1, SMK_M3, SMK_ERROR } msg;
899 char *msgtxt;
900 struct wpa_eapol_ie_parse kde;
901 const u8 *key_data;
902 size_t keyhdrlen, mic_len;
903 u8 *mic;
904
905 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
906 return;
907 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
908
909 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
910 keyhdrlen = sizeof(*key) + mic_len + 2;
911
912 if (data_len < sizeof(*hdr) + keyhdrlen) {
913 wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
914 return;
915 }
916
917 hdr = (struct ieee802_1x_hdr *) data;
918 key = (struct wpa_eapol_key *) (hdr + 1);
919 mic = (u8 *) (key + 1);
920 key_info = WPA_GET_BE16(key->key_info);
921 key_data = mic + mic_len + 2;
922 key_data_length = WPA_GET_BE16(mic + mic_len);
923 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
924 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
925 MAC2STR(sm->addr), key_info, key->type,
926 (unsigned int) mic_len, key_data_length);
927 wpa_hexdump(MSG_MSGDUMP,
928 "WPA: EAPOL-Key header (ending before Key MIC)",
929 key, sizeof(*key));
930 wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
931 mic, mic_len);
932 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
933 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
934 "key_data overflow (%d > %lu)",
935 key_data_length,
936 (unsigned long) (data_len - sizeof(*hdr) -
937 keyhdrlen));
938 return;
939 }
940
941 if (sm->wpa == WPA_VERSION_WPA2) {
942 if (key->type == EAPOL_KEY_TYPE_WPA) {
943 /*
944 * Some deployed station implementations seem to send
945 * msg 4/4 with incorrect type value in WPA2 mode.
946 */
947 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
948 "with unexpected WPA type in RSN mode");
949 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
950 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
951 "unexpected type %d in RSN mode",
952 key->type);
953 return;
954 }
955 } else {
956 if (key->type != EAPOL_KEY_TYPE_WPA) {
957 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
958 "unexpected type %d in WPA mode",
959 key->type);
960 return;
961 }
962 }
963
964 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
965 WPA_NONCE_LEN);
966 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
967 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
968
969 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
970 * are set */
971
972 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
973 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
974 if (key_info & WPA_KEY_INFO_ERROR) {
975 msg = SMK_ERROR;
976 msgtxt = "SMK Error";
977 } else {
978 msg = SMK_M1;
979 msgtxt = "SMK M1";
980 }
981 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
982 msg = SMK_M3;
983 msgtxt = "SMK M3";
984 } else if (key_info & WPA_KEY_INFO_REQUEST) {
985 msg = REQUEST;
986 msgtxt = "Request";
987 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
988 msg = GROUP_2;
989 msgtxt = "2/2 Group";
990 } else if (key_data_length == 0 ||
991 (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
992 key_data_length == AES_BLOCK_SIZE)) {
993 msg = PAIRWISE_4;
994 msgtxt = "4/4 Pairwise";
995 } else {
996 msg = PAIRWISE_2;
997 msgtxt = "2/4 Pairwise";
998 }
999
1000 /* TODO: key_info type validation for PeerKey */
1001 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
1002 msg == GROUP_2) {
1003 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1004 if (sm->pairwise == WPA_CIPHER_CCMP ||
1005 sm->pairwise == WPA_CIPHER_GCMP) {
1006 if (wpa_use_aes_cmac(sm) &&
1007 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
1008 !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
1009 !wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1010 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1011 wpa_auth_logger(wpa_auth, sm->addr,
1012 LOGGER_WARNING,
1013 "advertised support for "
1014 "AES-128-CMAC, but did not "
1015 "use it");
1016 return;
1017 }
1018
1019 if (!wpa_use_aes_cmac(sm) &&
1020 !wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1021 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1022 wpa_auth_logger(wpa_auth, sm->addr,
1023 LOGGER_WARNING,
1024 "did not use HMAC-SHA1-AES "
1025 "with CCMP/GCMP");
1026 return;
1027 }
1028 }
1029
1030 if ((wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1031 wpa_key_mgmt_fils(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 return;
1157 }
1158 break;
1159 case PAIRWISE_4:
1160 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1161 !sm->PTK_valid) {
1162 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1163 "received EAPOL-Key msg 4/4 in "
1164 "invalid state (%d) - dropped",
1165 sm->wpa_ptk_state);
1166 return;
1167 }
1168 break;
1169 case GROUP_2:
1170 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1171 || !sm->PTK_valid) {
1172 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1173 "received EAPOL-Key msg 2/2 in "
1174 "invalid state (%d) - dropped",
1175 sm->wpa_ptk_group_state);
1176 return;
1177 }
1178 break;
1179 #ifdef CONFIG_PEERKEY
1180 case SMK_M1:
1181 case SMK_M3:
1182 case SMK_ERROR:
1183 if (!wpa_auth->conf.peerkey) {
1184 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1185 "PeerKey use disabled - ignoring message");
1186 return;
1187 }
1188 if (!sm->PTK_valid) {
1189 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1190 "received EAPOL-Key msg SMK in "
1191 "invalid state - dropped");
1192 return;
1193 }
1194 break;
1195 #else /* CONFIG_PEERKEY */
1196 case SMK_M1:
1197 case SMK_M3:
1198 case SMK_ERROR:
1199 return; /* STSL disabled - ignore SMK messages */
1200 #endif /* CONFIG_PEERKEY */
1201 case REQUEST:
1202 break;
1203 }
1204
1205 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1206 "received EAPOL-Key frame (%s)", msgtxt);
1207
1208 if (key_info & WPA_KEY_INFO_ACK) {
1209 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1210 "received invalid EAPOL-Key: Key Ack set");
1211 return;
1212 }
1213
1214 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1215 !(key_info & WPA_KEY_INFO_MIC)) {
1216 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1217 "received invalid EAPOL-Key: Key MIC not set");
1218 return;
1219 }
1220
1221 #ifdef CONFIG_FILS
1222 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1223 (key_info & WPA_KEY_INFO_MIC)) {
1224 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1225 "received invalid EAPOL-Key: Key MIC set");
1226 return;
1227 }
1228 #endif /* CONFIG_FILS */
1229
1230 sm->MICVerified = FALSE;
1231 if (sm->PTK_valid && !sm->update_snonce) {
1232 if (mic_len &&
1233 wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
1234 data_len) &&
1235 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1236 wpa_try_alt_snonce(sm, data, data_len))) {
1237 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1238 "received EAPOL-Key with invalid MIC");
1239 return;
1240 }
1241 #ifdef CONFIG_FILS
1242 if (!mic_len &&
1243 wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
1244 &key_data_length) < 0) {
1245 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1246 "received EAPOL-Key with invalid MIC");
1247 return;
1248 }
1249 #endif /* CONFIG_FILS */
1250 sm->MICVerified = TRUE;
1251 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1252 sm->pending_1_of_4_timeout = 0;
1253 }
1254
1255 if (key_info & WPA_KEY_INFO_REQUEST) {
1256 if (sm->MICVerified) {
1257 sm->req_replay_counter_used = 1;
1258 os_memcpy(sm->req_replay_counter, key->replay_counter,
1259 WPA_REPLAY_COUNTER_LEN);
1260 } else {
1261 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1262 "received EAPOL-Key request with "
1263 "invalid MIC");
1264 return;
1265 }
1266
1267 /*
1268 * TODO: should decrypt key data field if encryption was used;
1269 * even though MAC address KDE is not normally encrypted,
1270 * supplicant is allowed to encrypt it.
1271 */
1272 if (msg == SMK_ERROR) {
1273 #ifdef CONFIG_PEERKEY
1274 wpa_smk_error(wpa_auth, sm, key_data, key_data_length);
1275 #endif /* CONFIG_PEERKEY */
1276 return;
1277 } else if (key_info & WPA_KEY_INFO_ERROR) {
1278 if (wpa_receive_error_report(
1279 wpa_auth, sm,
1280 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1281 return; /* STA entry was removed */
1282 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1283 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1284 "received EAPOL-Key Request for new "
1285 "4-Way Handshake");
1286 wpa_request_new_ptk(sm);
1287 #ifdef CONFIG_PEERKEY
1288 } else if (msg == SMK_M1) {
1289 wpa_smk_m1(wpa_auth, sm, key, key_data,
1290 key_data_length);
1291 #endif /* CONFIG_PEERKEY */
1292 } else if (key_data_length > 0 &&
1293 wpa_parse_kde_ies(key_data, key_data_length,
1294 &kde) == 0 &&
1295 kde.mac_addr) {
1296 } else {
1297 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1298 "received EAPOL-Key Request for GTK "
1299 "rekeying");
1300 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1301 wpa_rekey_gtk(wpa_auth, NULL);
1302 }
1303 } else {
1304 /* Do not allow the same key replay counter to be reused. */
1305 wpa_replay_counter_mark_invalid(sm->key_replay,
1306 key->replay_counter);
1307
1308 if (msg == PAIRWISE_2) {
1309 /*
1310 * Maintain a copy of the pending EAPOL-Key frames in
1311 * case the EAPOL-Key frame was retransmitted. This is
1312 * needed to allow EAPOL-Key msg 2/4 reply to another
1313 * pending msg 1/4 to update the SNonce to work around
1314 * unexpected supplicant behavior.
1315 */
1316 os_memcpy(sm->prev_key_replay, sm->key_replay,
1317 sizeof(sm->key_replay));
1318 } else {
1319 os_memset(sm->prev_key_replay, 0,
1320 sizeof(sm->prev_key_replay));
1321 }
1322
1323 /*
1324 * Make sure old valid counters are not accepted anymore and
1325 * do not get copied again.
1326 */
1327 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
1328 }
1329
1330 #ifdef CONFIG_PEERKEY
1331 if (msg == SMK_M3) {
1332 wpa_smk_m3(wpa_auth, sm, key, key_data, key_data_length);
1333 return;
1334 }
1335 #endif /* CONFIG_PEERKEY */
1336
1337 os_free(sm->last_rx_eapol_key);
1338 sm->last_rx_eapol_key = os_memdup(data, data_len);
1339 if (sm->last_rx_eapol_key == NULL)
1340 return;
1341 sm->last_rx_eapol_key_len = data_len;
1342
1343 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
1344 sm->EAPOLKeyReceived = TRUE;
1345 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1346 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1347 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1348 wpa_sm_step(sm);
1349 }
1350
1351
1352 static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1353 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1354 {
1355 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
1356 u8 *pos;
1357 int ret = 0;
1358
1359 /* GTK = PRF-X(GMK, "Group key expansion",
1360 * AA || GNonce || Time || random data)
1361 * The example described in the IEEE 802.11 standard uses only AA and
1362 * GNonce as inputs here. Add some more entropy since this derivation
1363 * is done only at the Authenticator and as such, does not need to be
1364 * exactly same.
1365 */
1366 os_memcpy(data, addr, ETH_ALEN);
1367 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1368 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1369 wpa_get_ntp_timestamp(pos);
1370 pos += 8;
1371 if (random_get_bytes(pos, 16) < 0)
1372 ret = -1;
1373
1374 #ifdef CONFIG_IEEE80211W
1375 sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
1376 #else /* CONFIG_IEEE80211W */
1377 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
1378 < 0)
1379 ret = -1;
1380 #endif /* CONFIG_IEEE80211W */
1381
1382 return ret;
1383 }
1384
1385
1386 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1387 {
1388 struct wpa_authenticator *wpa_auth = eloop_ctx;
1389 struct wpa_state_machine *sm = timeout_ctx;
1390
1391 sm->pending_1_of_4_timeout = 0;
1392 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1393 sm->TimeoutEvt = TRUE;
1394 wpa_sm_step(sm);
1395 }
1396
1397
1398 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1399 struct wpa_state_machine *sm, int key_info,
1400 const u8 *key_rsc, const u8 *nonce,
1401 const u8 *kde, size_t kde_len,
1402 int keyidx, int encr, int force_version)
1403 {
1404 struct ieee802_1x_hdr *hdr;
1405 struct wpa_eapol_key *key;
1406 size_t len, mic_len, keyhdrlen;
1407 int alg;
1408 int key_data_len, pad_len = 0;
1409 u8 *buf, *pos;
1410 int version, pairwise;
1411 int i;
1412 u8 *key_mic, *key_data;
1413
1414 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
1415 keyhdrlen = sizeof(*key) + mic_len + 2;
1416
1417 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
1418
1419 if (force_version)
1420 version = force_version;
1421 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1422 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1423 wpa_key_mgmt_fils(sm->wpa_key_mgmt))
1424 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
1425 else if (wpa_use_aes_cmac(sm))
1426 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
1427 else if (sm->pairwise != WPA_CIPHER_TKIP)
1428 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1429 else
1430 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1431
1432 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1433
1434 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1435 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1436 "encr=%d)",
1437 version,
1438 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1439 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1440 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1441 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1442 pairwise, (unsigned long) kde_len, keyidx, encr);
1443
1444 key_data_len = kde_len;
1445
1446 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1447 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1448 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1449 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1450 pad_len = key_data_len % 8;
1451 if (pad_len)
1452 pad_len = 8 - pad_len;
1453 key_data_len += pad_len + 8;
1454 }
1455
1456 len += key_data_len;
1457 if (!mic_len && encr)
1458 len += AES_BLOCK_SIZE;
1459
1460 hdr = os_zalloc(len);
1461 if (hdr == NULL)
1462 return;
1463 hdr->version = wpa_auth->conf.eapol_version;
1464 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1465 hdr->length = host_to_be16(len - sizeof(*hdr));
1466 key = (struct wpa_eapol_key *) (hdr + 1);
1467 key_mic = (u8 *) (key + 1);
1468 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
1469
1470 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1471 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1472 key_info |= version;
1473 if (encr && sm->wpa == WPA_VERSION_WPA2)
1474 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1475 if (sm->wpa != WPA_VERSION_WPA2)
1476 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1477 WPA_PUT_BE16(key->key_info, key_info);
1478
1479 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1480 if ((key_info & WPA_KEY_INFO_SMK_MESSAGE) ||
1481 (sm->wpa == WPA_VERSION_WPA2 && !pairwise))
1482 WPA_PUT_BE16(key->key_length, 0);
1483 else
1484 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
1485
1486 /* FIX: STSL: what to use as key_replay_counter? */
1487 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1488 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1489 os_memcpy(sm->key_replay[i].counter,
1490 sm->key_replay[i - 1].counter,
1491 WPA_REPLAY_COUNTER_LEN);
1492 }
1493 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1494 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1495 WPA_REPLAY_COUNTER_LEN);
1496 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1497 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1498 sm->key_replay[0].valid = TRUE;
1499
1500 if (nonce)
1501 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1502
1503 if (key_rsc)
1504 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1505
1506 if (kde && !encr) {
1507 os_memcpy(key_data, kde, kde_len);
1508 WPA_PUT_BE16(key_mic + mic_len, kde_len);
1509 #ifdef CONFIG_FILS
1510 } else if (!mic_len) {
1511 const u8 *aad[1];
1512 size_t aad_len[1];
1513
1514 WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
1515 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1516 kde, kde_len);
1517
1518 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
1519 sm->PTK.kek, sm->PTK.kek_len);
1520 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1521 * to Key Data (exclusive). */
1522 aad[0] = (u8 *) hdr;
1523 aad_len[0] = key_mic + 2 - (u8 *) hdr;
1524 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
1525 1, aad, aad_len, key_mic + 2) < 0) {
1526 wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
1527 return;
1528 }
1529
1530 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
1531 key_mic + 2, AES_BLOCK_SIZE + kde_len);
1532 #endif /* CONFIG_FILS */
1533 } else if (encr && kde) {
1534 buf = os_zalloc(key_data_len);
1535 if (buf == NULL) {
1536 os_free(hdr);
1537 return;
1538 }
1539 pos = buf;
1540 os_memcpy(pos, kde, kde_len);
1541 pos += kde_len;
1542
1543 if (pad_len)
1544 *pos++ = 0xdd;
1545
1546 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1547 buf, key_data_len);
1548 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1549 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1550 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1551 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1552 wpa_printf(MSG_DEBUG,
1553 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1554 (unsigned int) sm->PTK.kek_len);
1555 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1556 (key_data_len - 8) / 8, buf, key_data)) {
1557 os_free(hdr);
1558 os_free(buf);
1559 return;
1560 }
1561 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1562 #ifndef CONFIG_NO_RC4
1563 } else if (sm->PTK.kek_len == 16) {
1564 u8 ek[32];
1565
1566 wpa_printf(MSG_DEBUG,
1567 "WPA: Encrypt Key Data using RC4");
1568 os_memcpy(key->key_iv,
1569 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1570 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1571 os_memcpy(ek, key->key_iv, 16);
1572 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1573 os_memcpy(key_data, buf, key_data_len);
1574 rc4_skip(ek, 32, 256, key_data, key_data_len);
1575 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1576 #endif /* CONFIG_NO_RC4 */
1577 } else {
1578 os_free(hdr);
1579 os_free(buf);
1580 return;
1581 }
1582 os_free(buf);
1583 }
1584
1585 if (key_info & WPA_KEY_INFO_MIC) {
1586 if (!sm->PTK_valid || !mic_len) {
1587 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1588 "PTK not valid when sending EAPOL-Key "
1589 "frame");
1590 os_free(hdr);
1591 return;
1592 }
1593
1594 wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1595 sm->wpa_key_mgmt, version,
1596 (u8 *) hdr, len, key_mic);
1597 #ifdef CONFIG_TESTING_OPTIONS
1598 if (!pairwise &&
1599 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
1600 drand48() <
1601 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1602 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1603 "Corrupting group EAPOL-Key Key MIC");
1604 key_mic[0]++;
1605 }
1606 #endif /* CONFIG_TESTING_OPTIONS */
1607 }
1608
1609 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1610 1);
1611 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1612 sm->pairwise_set);
1613 os_free(hdr);
1614 }
1615
1616
1617 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1618 struct wpa_state_machine *sm, int key_info,
1619 const u8 *key_rsc, const u8 *nonce,
1620 const u8 *kde, size_t kde_len,
1621 int keyidx, int encr)
1622 {
1623 int timeout_ms;
1624 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1625 u32 ctr;
1626
1627 if (sm == NULL)
1628 return;
1629
1630 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1631 keyidx, encr, 0);
1632
1633 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1634 if (ctr == 1 && wpa_auth->conf.tx_status)
1635 timeout_ms = pairwise ? eapol_key_timeout_first :
1636 eapol_key_timeout_first_group;
1637 else
1638 timeout_ms = eapol_key_timeout_subseq;
1639 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1640 sm->pending_1_of_4_timeout = 1;
1641 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1642 "counter %u)", timeout_ms, ctr);
1643 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1644 wpa_send_eapol_timeout, wpa_auth, sm);
1645 }
1646
1647
1648 static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
1649 size_t data_len)
1650 {
1651 struct ieee802_1x_hdr *hdr;
1652 struct wpa_eapol_key *key;
1653 u16 key_info;
1654 int ret = 0;
1655 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
1656 size_t mic_len = wpa_mic_len(akmp);
1657
1658 if (data_len < sizeof(*hdr) + sizeof(*key))
1659 return -1;
1660
1661 hdr = (struct ieee802_1x_hdr *) data;
1662 key = (struct wpa_eapol_key *) (hdr + 1);
1663 mic_pos = (u8 *) (key + 1);
1664 key_info = WPA_GET_BE16(key->key_info);
1665 os_memcpy(mic, mic_pos, mic_len);
1666 os_memset(mic_pos, 0, mic_len);
1667 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1668 key_info & WPA_KEY_INFO_TYPE_MASK,
1669 data, data_len, mic_pos) ||
1670 os_memcmp_const(mic, mic_pos, mic_len) != 0)
1671 ret = -1;
1672 os_memcpy(mic_pos, mic, mic_len);
1673 return ret;
1674 }
1675
1676
1677 void wpa_remove_ptk(struct wpa_state_machine *sm)
1678 {
1679 sm->PTK_valid = FALSE;
1680 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1681 if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
1682 0))
1683 wpa_printf(MSG_DEBUG,
1684 "RSN: PTK removal from the driver failed");
1685 sm->pairwise_set = FALSE;
1686 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1687 }
1688
1689
1690 int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
1691 {
1692 int remove_ptk = 1;
1693
1694 if (sm == NULL)
1695 return -1;
1696
1697 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1698 "event %d notification", event);
1699
1700 switch (event) {
1701 case WPA_AUTH:
1702 #ifdef CONFIG_MESH
1703 /* PTKs are derived through AMPE */
1704 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1705 /* not mesh */
1706 break;
1707 }
1708 return 0;
1709 #endif /* CONFIG_MESH */
1710 case WPA_ASSOC:
1711 break;
1712 case WPA_DEAUTH:
1713 case WPA_DISASSOC:
1714 sm->DeauthenticationRequest = TRUE;
1715 break;
1716 case WPA_REAUTH:
1717 case WPA_REAUTH_EAPOL:
1718 if (!sm->started) {
1719 /*
1720 * When using WPS, we may end up here if the STA
1721 * manages to re-associate without the previous STA
1722 * entry getting removed. Consequently, we need to make
1723 * sure that the WPA state machines gets initialized
1724 * properly at this point.
1725 */
1726 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1727 "started - initialize now");
1728 sm->started = 1;
1729 sm->Init = TRUE;
1730 if (wpa_sm_step(sm) == 1)
1731 return 1; /* should not really happen */
1732 sm->Init = FALSE;
1733 sm->AuthenticationRequest = TRUE;
1734 break;
1735 }
1736 if (sm->GUpdateStationKeys) {
1737 /*
1738 * Reauthentication cancels the pending group key
1739 * update for this STA.
1740 */
1741 sm->group->GKeyDoneStations--;
1742 sm->GUpdateStationKeys = FALSE;
1743 sm->PtkGroupInit = TRUE;
1744 }
1745 sm->ReAuthenticationRequest = TRUE;
1746 break;
1747 case WPA_ASSOC_FT:
1748 #ifdef CONFIG_IEEE80211R_AP
1749 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1750 "after association");
1751 wpa_ft_install_ptk(sm);
1752
1753 /* Using FT protocol, not WPA auth state machine */
1754 sm->ft_completed = 1;
1755 return 0;
1756 #else /* CONFIG_IEEE80211R_AP */
1757 break;
1758 #endif /* CONFIG_IEEE80211R_AP */
1759 }
1760
1761 #ifdef CONFIG_IEEE80211R_AP
1762 sm->ft_completed = 0;
1763 #endif /* CONFIG_IEEE80211R_AP */
1764
1765 #ifdef CONFIG_IEEE80211W
1766 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1767 remove_ptk = 0;
1768 #endif /* CONFIG_IEEE80211W */
1769 #ifdef CONFIG_FILS
1770 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1771 (event == WPA_AUTH || event == WPA_ASSOC))
1772 remove_ptk = 0;
1773 #endif /* CONFIG_FILS */
1774
1775 if (remove_ptk) {
1776 sm->PTK_valid = FALSE;
1777 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1778
1779 if (event != WPA_REAUTH_EAPOL)
1780 wpa_remove_ptk(sm);
1781 }
1782
1783 if (sm->in_step_loop) {
1784 /*
1785 * wpa_sm_step() is already running - avoid recursive call to
1786 * it by making the existing loop process the new update.
1787 */
1788 sm->changed = TRUE;
1789 return 0;
1790 }
1791 return wpa_sm_step(sm);
1792 }
1793
1794
1795 SM_STATE(WPA_PTK, INITIALIZE)
1796 {
1797 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1798 if (sm->Init) {
1799 /* Init flag is not cleared here, so avoid busy
1800 * loop by claiming nothing changed. */
1801 sm->changed = FALSE;
1802 }
1803
1804 sm->keycount = 0;
1805 if (sm->GUpdateStationKeys)
1806 sm->group->GKeyDoneStations--;
1807 sm->GUpdateStationKeys = FALSE;
1808 if (sm->wpa == WPA_VERSION_WPA)
1809 sm->PInitAKeys = FALSE;
1810 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1811 * Local AA > Remote AA)) */) {
1812 sm->Pair = TRUE;
1813 }
1814 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1815 wpa_remove_ptk(sm);
1816 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1817 sm->TimeoutCtr = 0;
1818 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
1819 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
1820 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1821 WPA_EAPOL_authorized, 0);
1822 }
1823 }
1824
1825
1826 SM_STATE(WPA_PTK, DISCONNECT)
1827 {
1828 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1829 sm->Disconnect = FALSE;
1830 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1831 }
1832
1833
1834 SM_STATE(WPA_PTK, DISCONNECTED)
1835 {
1836 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1837 sm->DeauthenticationRequest = FALSE;
1838 }
1839
1840
1841 SM_STATE(WPA_PTK, AUTHENTICATION)
1842 {
1843 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1844 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1845 sm->PTK_valid = FALSE;
1846 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1847 1);
1848 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1849 sm->AuthenticationRequest = FALSE;
1850 }
1851
1852
1853 static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1854 struct wpa_group *group)
1855 {
1856 if (group->first_sta_seen)
1857 return;
1858 /*
1859 * System has run bit further than at the time hostapd was started
1860 * potentially very early during boot up. This provides better chances
1861 * of collecting more randomness on embedded systems. Re-initialize the
1862 * GMK and Counter here to improve their strength if there was not
1863 * enough entropy available immediately after system startup.
1864 */
1865 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1866 "station");
1867 if (random_pool_ready() != 1) {
1868 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1869 "to proceed - reject first 4-way handshake");
1870 group->reject_4way_hs_for_entropy = TRUE;
1871 } else {
1872 group->first_sta_seen = TRUE;
1873 group->reject_4way_hs_for_entropy = FALSE;
1874 }
1875
1876 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
1877 wpa_gtk_update(wpa_auth, group) < 0 ||
1878 wpa_group_config_group_keys(wpa_auth, group) < 0) {
1879 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
1880 group->first_sta_seen = FALSE;
1881 group->reject_4way_hs_for_entropy = TRUE;
1882 }
1883 }
1884
1885
1886 SM_STATE(WPA_PTK, AUTHENTICATION2)
1887 {
1888 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1889
1890 wpa_group_ensure_init(sm->wpa_auth, sm->group);
1891 sm->ReAuthenticationRequest = FALSE;
1892
1893 /*
1894 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1895 * ambiguous. The Authenticator state machine uses a counter that is
1896 * incremented by one for each 4-way handshake. However, the security
1897 * analysis of 4-way handshake points out that unpredictable nonces
1898 * help in preventing precomputation attacks. Instead of the state
1899 * machine definition, use an unpredictable nonce value here to provide
1900 * stronger protection against potential precomputation attacks.
1901 */
1902 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1903 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1904 "ANonce.");
1905 sm->Disconnect = TRUE;
1906 return;
1907 }
1908 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1909 WPA_NONCE_LEN);
1910 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1911 * logical place than INITIALIZE since AUTHENTICATION2 can be
1912 * re-entered on ReAuthenticationRequest without going through
1913 * INITIALIZE. */
1914 sm->TimeoutCtr = 0;
1915 }
1916
1917
1918 SM_STATE(WPA_PTK, INITPMK)
1919 {
1920 u8 msk[2 * PMK_LEN];
1921 size_t len = 2 * PMK_LEN;
1922
1923 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1924 #ifdef CONFIG_IEEE80211R_AP
1925 sm->xxkey_len = 0;
1926 #endif /* CONFIG_IEEE80211R_AP */
1927 if (sm->pmksa) {
1928 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1929 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
1930 sm->pmk_len = sm->pmksa->pmk_len;
1931 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1932 unsigned int pmk_len;
1933
1934 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
1935 pmk_len = PMK_LEN_SUITE_B_192;
1936 else
1937 pmk_len = PMK_LEN;
1938 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1939 "(MSK len=%lu PMK len=%u)", (unsigned long) len,
1940 pmk_len);
1941 if (len < pmk_len) {
1942 wpa_printf(MSG_DEBUG,
1943 "WPA: MSK not long enough (%u) to create PMK (%u)",
1944 (unsigned int) len, (unsigned int) pmk_len);
1945 sm->Disconnect = TRUE;
1946 return;
1947 }
1948 os_memcpy(sm->PMK, msk, pmk_len);
1949 sm->pmk_len = pmk_len;
1950 #ifdef CONFIG_IEEE80211R_AP
1951 if (len >= 2 * PMK_LEN) {
1952 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1953 sm->xxkey_len = PMK_LEN;
1954 }
1955 #endif /* CONFIG_IEEE80211R_AP */
1956 } else {
1957 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
1958 sm->wpa_auth->cb->get_msk);
1959 sm->Disconnect = TRUE;
1960 return;
1961 }
1962 os_memset(msk, 0, sizeof(msk));
1963
1964 sm->req_replay_counter_used = 0;
1965 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1966 * will break reauthentication since EAPOL state machines may not be
1967 * get into AUTHENTICATING state that clears keyRun before WPA state
1968 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1969 * state and takes PMK from the previously used AAA Key. This will
1970 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1971 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1972 * be good workaround for this issue. */
1973 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1974 }
1975
1976
1977 SM_STATE(WPA_PTK, INITPSK)
1978 {
1979 const u8 *psk;
1980 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1981 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
1982 if (psk) {
1983 os_memcpy(sm->PMK, psk, PMK_LEN);
1984 sm->pmk_len = PMK_LEN;
1985 #ifdef CONFIG_IEEE80211R_AP
1986 os_memcpy(sm->xxkey, psk, PMK_LEN);
1987 sm->xxkey_len = PMK_LEN;
1988 #endif /* CONFIG_IEEE80211R_AP */
1989 }
1990 sm->req_replay_counter_used = 0;
1991 }
1992
1993
1994 SM_STATE(WPA_PTK, PTKSTART)
1995 {
1996 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1997 size_t pmkid_len = 0;
1998
1999 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
2000 sm->PTKRequest = FALSE;
2001 sm->TimeoutEvt = FALSE;
2002 sm->alt_snonce_valid = FALSE;
2003
2004 sm->TimeoutCtr++;
2005 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
2006 /* No point in sending the EAPOL-Key - we will disconnect
2007 * immediately following this. */
2008 return;
2009 }
2010
2011 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2012 "sending 1/4 msg of 4-Way Handshake");
2013 /*
2014 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
2015 * one possible PSK for this STA.
2016 */
2017 if (sm->wpa == WPA_VERSION_WPA2 &&
2018 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2019 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
2020 pmkid = buf;
2021 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2022 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
2023 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2024 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
2025 if (sm->pmksa) {
2026 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2027 sm->pmksa->pmkid, PMKID_LEN);
2028 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
2029 /* No KCK available to derive PMKID */
2030 pmkid = NULL;
2031 } else {
2032 /*
2033 * Calculate PMKID since no PMKSA cache entry was
2034 * available with pre-calculated PMKID.
2035 */
2036 rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
2037 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
2038 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
2039 }
2040 }
2041 wpa_send_eapol(sm->wpa_auth, sm,
2042 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
2043 sm->ANonce, pmkid, pmkid_len, 0, 0);
2044 }
2045
2046
2047 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
2048 const u8 *pmk, unsigned int pmk_len,
2049 struct wpa_ptk *ptk)
2050 {
2051 #ifdef CONFIG_IEEE80211R_AP
2052 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
2053 return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
2054 #endif /* CONFIG_IEEE80211R_AP */
2055
2056 return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
2057 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
2058 ptk, sm->wpa_key_mgmt, sm->pairwise);
2059 }
2060
2061
2062 #ifdef CONFIG_FILS
2063
2064 int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
2065 size_t pmk_len, const u8 *snonce, const u8 *anonce)
2066 {
2067 u8 ick[FILS_ICK_MAX_LEN];
2068 size_t ick_len;
2069 int res;
2070
2071 res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr,
2072 snonce, anonce, &sm->PTK, ick, &ick_len,
2073 sm->wpa_key_mgmt, sm->pairwise);
2074 if (res < 0)
2075 return res;
2076 sm->PTK_valid = TRUE;
2077
2078 res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
2079 sm->addr, sm->wpa_auth->addr,
2080 NULL, 0, NULL, 0, /* TODO: SK+PFS */
2081 sm->wpa_key_mgmt, sm->fils_key_auth_sta,
2082 sm->fils_key_auth_ap,
2083 &sm->fils_key_auth_len);
2084 os_memset(ick, 0, sizeof(ick));
2085
2086 /* Store nonces for (Re)Association Request/Response frame processing */
2087 os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
2088 os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
2089
2090 return res;
2091 }
2092
2093
2094 static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2095 u8 *buf, size_t buf_len, u16 *_key_data_len)
2096 {
2097 struct ieee802_1x_hdr *hdr;
2098 struct wpa_eapol_key *key;
2099 u8 *pos;
2100 u16 key_data_len;
2101 u8 *tmp;
2102 const u8 *aad[1];
2103 size_t aad_len[1];
2104
2105 hdr = (struct ieee802_1x_hdr *) buf;
2106 key = (struct wpa_eapol_key *) (hdr + 1);
2107 pos = (u8 *) (key + 1);
2108 key_data_len = WPA_GET_BE16(pos);
2109 if (key_data_len < AES_BLOCK_SIZE ||
2110 key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
2111 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2112 "No room for AES-SIV data in the frame");
2113 return -1;
2114 }
2115 pos += 2; /* Pointing at the Encrypted Key Data field */
2116
2117 tmp = os_malloc(key_data_len);
2118 if (!tmp)
2119 return -1;
2120
2121 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2122 * to Key Data (exclusive). */
2123 aad[0] = buf;
2124 aad_len[0] = pos - buf;
2125 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
2126 1, aad, aad_len, tmp) < 0) {
2127 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2128 "Invalid AES-SIV data in the frame");
2129 bin_clear_free(tmp, key_data_len);
2130 return -1;
2131 }
2132
2133 /* AEAD decryption and validation completed successfully */
2134 key_data_len -= AES_BLOCK_SIZE;
2135 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2136 tmp, key_data_len);
2137
2138 /* Replace Key Data field with the decrypted version */
2139 os_memcpy(pos, tmp, key_data_len);
2140 pos -= 2; /* Key Data Length field */
2141 WPA_PUT_BE16(pos, key_data_len);
2142 bin_clear_free(tmp, key_data_len);
2143 if (_key_data_len)
2144 *_key_data_len = key_data_len;
2145 return 0;
2146 }
2147
2148
2149 int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
2150 const struct ieee80211_mgmt *mgmt, size_t frame_len,
2151 u8 *pos, size_t left)
2152 {
2153 u16 fc, stype;
2154 const u8 *end, *ie_start, *ie, *session, *crypt;
2155 struct ieee802_11_elems elems;
2156 const u8 *aad[5];
2157 size_t aad_len[5];
2158
2159 if (!sm || !sm->PTK_valid) {
2160 wpa_printf(MSG_DEBUG,
2161 "FILS: No KEK to decrypt Assocication Request frame");
2162 return -1;
2163 }
2164
2165 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2166 wpa_printf(MSG_DEBUG,
2167 "FILS: Not a FILS AKM - reject association");
2168 return -1;
2169 }
2170
2171 end = ((const u8 *) mgmt) + frame_len;
2172 fc = le_to_host16(mgmt->frame_control);
2173 stype = WLAN_FC_GET_STYPE(fc);
2174 if (stype == WLAN_FC_STYPE_REASSOC_REQ)
2175 ie_start = mgmt->u.reassoc_req.variable;
2176 else
2177 ie_start = mgmt->u.assoc_req.variable;
2178 ie = ie_start;
2179
2180 /*
2181 * Find FILS Session element which is the last unencrypted element in
2182 * the frame.
2183 */
2184 session = NULL;
2185 while (ie + 1 < end) {
2186 if (ie + 2 + ie[1] > end)
2187 break;
2188 if (ie[0] == WLAN_EID_EXTENSION &&
2189 ie[1] >= 1 + FILS_SESSION_LEN &&
2190 ie[2] == WLAN_EID_EXT_FILS_SESSION) {
2191 session = ie;
2192 break;
2193 }
2194 ie += 2 + ie[1];
2195 }
2196
2197 if (!session) {
2198 wpa_printf(MSG_DEBUG,
2199 "FILS: Could not find FILS Session element in Association Request frame - reject");
2200 return -1;
2201 }
2202 if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
2203 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
2204 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
2205 fils_session, FILS_SESSION_LEN);
2206 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
2207 session + 3, FILS_SESSION_LEN);
2208 return -1;
2209 }
2210 crypt = session + 2 + session[1];
2211
2212 if (end - crypt < AES_BLOCK_SIZE) {
2213 wpa_printf(MSG_DEBUG,
2214 "FILS: Too short frame to include AES-SIV data");
2215 return -1;
2216 }
2217
2218 /* AES-SIV AAD vectors */
2219
2220 /* The STA's MAC address */
2221 aad[0] = mgmt->sa;
2222 aad_len[0] = ETH_ALEN;
2223 /* The AP's BSSID */
2224 aad[1] = mgmt->da;
2225 aad_len[1] = ETH_ALEN;
2226 /* The STA's nonce */
2227 aad[2] = sm->SNonce;
2228 aad_len[2] = FILS_NONCE_LEN;
2229 /* The AP's nonce */
2230 aad[3] = sm->ANonce;
2231 aad_len[3] = FILS_NONCE_LEN;
2232 /*
2233 * The (Re)Association Request frame from the Capability Information
2234 * field to the FILS Session element (both inclusive).
2235 */
2236 aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
2237 aad_len[4] = crypt - aad[4];
2238
2239 if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
2240 5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
2241 wpa_printf(MSG_DEBUG,
2242 "FILS: Invalid AES-SIV data in the frame");
2243 return -1;
2244 }
2245 wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
2246 pos, left - AES_BLOCK_SIZE);
2247
2248 if (ieee802_11_parse_elems(pos, left - AES_BLOCK_SIZE, &elems, 1) ==
2249 ParseFailed) {
2250 wpa_printf(MSG_DEBUG,
2251 "FILS: Failed to parse decrypted elements");
2252 return -1;
2253 }
2254 if (!elems.fils_key_confirm) {
2255 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
2256 return -1;
2257 }
2258 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
2259 wpa_printf(MSG_DEBUG,
2260 "FILS: Unexpected Key-Auth length %d (expected %d)",
2261 elems.fils_key_confirm_len,
2262 (int) sm->fils_key_auth_len);
2263 return -1;
2264 }
2265 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
2266 sm->fils_key_auth_len) != 0) {
2267 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
2268 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
2269 elems.fils_key_confirm,
2270 elems.fils_key_confirm_len);
2271 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
2272 sm->fils_key_auth_sta, sm->fils_key_auth_len);
2273 return -1;
2274 }
2275
2276 return left - AES_BLOCK_SIZE;
2277 }
2278
2279
2280 int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
2281 size_t current_len, size_t max_len,
2282 const struct wpabuf *hlp)
2283 {
2284 u8 *end = buf + max_len;
2285 u8 *pos = buf + current_len;
2286 struct ieee80211_mgmt *mgmt;
2287 struct wpabuf *plain;
2288 u8 *len, *tmp, *tmp2;
2289 u8 hdr[2];
2290 u8 *gtk, dummy_gtk[32];
2291 size_t gtk_len;
2292 struct wpa_group *gsm;
2293 const u8 *aad[5];
2294 size_t aad_len[5];
2295
2296 if (!sm || !sm->PTK_valid)
2297 return -1;
2298
2299 wpa_hexdump(MSG_DEBUG,
2300 "FILS: Association Response frame before FILS processing",
2301 buf, current_len);
2302
2303 mgmt = (struct ieee80211_mgmt *) buf;
2304
2305 /* AES-SIV AAD vectors */
2306
2307 /* The AP's BSSID */
2308 aad[0] = mgmt->sa;
2309 aad_len[0] = ETH_ALEN;
2310 /* The STA's MAC address */
2311 aad[1] = mgmt->da;
2312 aad_len[1] = ETH_ALEN;
2313 /* The AP's nonce */
2314 aad[2] = sm->ANonce;
2315 aad_len[2] = FILS_NONCE_LEN;
2316 /* The STA's nonce */
2317 aad[3] = sm->SNonce;
2318 aad_len[3] = FILS_NONCE_LEN;
2319 /*
2320 * The (Re)Association Response frame from the Capability Information
2321 * field (the same offset in both Association and Reassociation
2322 * Response frames) to the FILS Session element (both inclusive).
2323 */
2324 aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
2325 aad_len[4] = pos - aad[4];
2326
2327 /* The following elements will be encrypted with AES-SIV */
2328
2329 plain = wpabuf_alloc(1000);
2330 if (!plain)
2331 return -1;
2332
2333 /* TODO: FILS Public Key */
2334
2335 /* FILS Key Confirmation */
2336 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2337 wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
2338 /* Element ID Extension */
2339 wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
2340 wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
2341
2342 /* FILS HLP Container */
2343 if (hlp)
2344 wpabuf_put_buf(plain, hlp);
2345
2346 /* TODO: FILS IP Address Assignment */
2347
2348 /* Key Delivery */
2349 gsm = sm->group;
2350 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2351 len = wpabuf_put(plain, 1);
2352 wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
2353 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
2354 wpabuf_put(plain, WPA_KEY_RSC_LEN));
2355 /* GTK KDE */
2356 gtk = gsm->GTK[gsm->GN - 1];
2357 gtk_len = gsm->GTK_len;
2358 if (sm->wpa_auth->conf.disable_gtk) {
2359 /*
2360 * Provide unique random GTK to each STA to prevent use
2361 * of GTK in the BSS.
2362 */
2363 if (random_get_bytes(dummy_gtk, gtk_len) < 0) {
2364 wpabuf_free(plain);
2365 return -1;
2366 }
2367 gtk = dummy_gtk;
2368 }
2369 hdr[0] = gsm->GN & 0x03;
2370 hdr[1] = 0;
2371 tmp = wpabuf_put(plain, 0);
2372 tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2373 gtk, gtk_len);
2374 wpabuf_put(plain, tmp2 - tmp);
2375
2376 /* IGTK KDE */
2377 tmp = wpabuf_put(plain, 0);
2378 tmp2 = ieee80211w_kde_add(sm, tmp);
2379 wpabuf_put(plain, tmp2 - tmp);
2380
2381 *len = (u8 *) wpabuf_put(plain, 0) - len - 1;
2382
2383 if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
2384 wpa_printf(MSG_DEBUG,
2385 "FILS: Not enough room for FILS elements");
2386 wpabuf_free(plain);
2387 return -1;
2388 }
2389
2390 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
2391 plain);
2392
2393 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
2394 wpabuf_head(plain), wpabuf_len(plain),
2395 5, aad, aad_len, pos) < 0) {
2396 wpabuf_free(plain);
2397 return -1;
2398 }
2399
2400 wpa_hexdump(MSG_DEBUG,
2401 "FILS: Encrypted Association Response elements",
2402 pos, AES_BLOCK_SIZE + wpabuf_len(plain));
2403 current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
2404 wpabuf_free(plain);
2405
2406 sm->fils_completed = 1;
2407
2408 return current_len;
2409 }
2410
2411
2412 int fils_set_tk(struct wpa_state_machine *sm)
2413 {
2414 enum wpa_alg alg;
2415 int klen;
2416
2417 if (!sm || !sm->PTK_valid)
2418 return -1;
2419
2420 alg = wpa_cipher_to_alg(sm->pairwise);
2421 klen = wpa_cipher_key_len(sm->pairwise);
2422
2423 wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
2424 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2425 sm->PTK.tk, klen)) {
2426 wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
2427 return -1;
2428 }
2429
2430 return 0;
2431 }
2432
2433 #endif /* CONFIG_FILS */
2434
2435
2436 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2437 {
2438 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2439 struct wpa_ptk PTK;
2440 int ok = 0, psk_found = 0;
2441 const u8 *pmk = NULL;
2442 unsigned int pmk_len;
2443 int ft;
2444 const u8 *eapol_key_ie, *key_data, *mic;
2445 u16 key_data_length;
2446 size_t mic_len, eapol_key_ie_len;
2447 struct ieee802_1x_hdr *hdr;
2448 struct wpa_eapol_key *key;
2449 struct wpa_eapol_ie_parse kde;
2450
2451 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2452 sm->EAPOLKeyReceived = FALSE;
2453 sm->update_snonce = FALSE;
2454 os_memset(&PTK, 0, sizeof(PTK));
2455
2456 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
2457
2458 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2459 * WPA-PSK: iterate through possible PSKs and select the one matching
2460 * the packet */
2461 for (;;) {
2462 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2463 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
2464 sm->p2p_dev_addr, pmk);
2465 if (pmk == NULL)
2466 break;
2467 psk_found = 1;
2468 pmk_len = PMK_LEN;
2469 } else {
2470 pmk = sm->PMK;
2471 pmk_len = sm->pmk_len;
2472 }
2473
2474 if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK) < 0)
2475 break;
2476
2477 if (mic_len &&
2478 wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
2479 sm->last_rx_eapol_key,
2480 sm->last_rx_eapol_key_len) == 0) {
2481 ok = 1;
2482 break;
2483 }
2484
2485 #ifdef CONFIG_FILS
2486 if (!mic_len &&
2487 wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
2488 sm->last_rx_eapol_key_len, NULL) == 0) {
2489 ok = 1;
2490 break;
2491 }
2492 #endif /* CONFIG_FILS */
2493
2494 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
2495 break;
2496 }
2497
2498 if (!ok) {
2499 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2500 "invalid MIC in msg 2/4 of 4-Way Handshake");
2501 if (psk_found)
2502 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
2503 return;
2504 }
2505
2506 /*
2507 * Note: last_rx_eapol_key length fields have already been validated in
2508 * wpa_receive().
2509 */
2510 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
2511 key = (struct wpa_eapol_key *) (hdr + 1);
2512 mic = (u8 *) (key + 1);
2513 key_data = mic + mic_len + 2;
2514 key_data_length = WPA_GET_BE16(mic + mic_len);
2515 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
2516 sizeof(*key) - mic_len - 2)
2517 return;
2518
2519 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
2520 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
2521 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2522 return;
2523 }
2524 if (kde.rsn_ie) {
2525 eapol_key_ie = kde.rsn_ie;
2526 eapol_key_ie_len = kde.rsn_ie_len;
2527 } else if (kde.osen) {
2528 eapol_key_ie = kde.osen;
2529 eapol_key_ie_len = kde.osen_len;
2530 } else {
2531 eapol_key_ie = kde.wpa_ie;
2532 eapol_key_ie_len = kde.wpa_ie_len;
2533 }
2534 ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
2535 if (sm->wpa_ie == NULL ||
2536 wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
2537 eapol_key_ie, eapol_key_ie_len)) {
2538 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2539 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2540 if (sm->wpa_ie) {
2541 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
2542 sm->wpa_ie, sm->wpa_ie_len);
2543 }
2544 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
2545 eapol_key_ie, eapol_key_ie_len);
2546 /* MLME-DEAUTHENTICATE.request */
2547 wpa_sta_disconnect(wpa_auth, sm->addr);
2548 return;
2549 }
2550 #ifdef CONFIG_IEEE80211R_AP
2551 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
2552 wpa_sta_disconnect(wpa_auth, sm->addr);
2553 return;
2554 }
2555 #endif /* CONFIG_IEEE80211R_AP */
2556 #ifdef CONFIG_P2P
2557 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
2558 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
2559 int idx;
2560 wpa_printf(MSG_DEBUG,
2561 "P2P: IP address requested in EAPOL-Key exchange");
2562 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
2563 if (idx >= 0) {
2564 u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
2565 bitfield_set(wpa_auth->ip_pool, idx);
2566 WPA_PUT_BE32(sm->ip_addr, start + idx);
2567 wpa_printf(MSG_DEBUG,
2568 "P2P: Assigned IP address %u.%u.%u.%u to "
2569 MACSTR, sm->ip_addr[0], sm->ip_addr[1],
2570 sm->ip_addr[2], sm->ip_addr[3],
2571 MAC2STR(sm->addr));
2572 }
2573 }
2574 #endif /* CONFIG_P2P */
2575
2576 #ifdef CONFIG_IEEE80211R_AP
2577 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2578 /*
2579 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2580 * with the value we derived.
2581 */
2582 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
2583 WPA_PMK_NAME_LEN) != 0) {
2584 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2585 "PMKR1Name mismatch in FT 4-way "
2586 "handshake");
2587 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
2588 "Supplicant",
2589 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
2590 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2591 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2592 return;
2593 }
2594 }
2595 #endif /* CONFIG_IEEE80211R_AP */
2596
2597 sm->pending_1_of_4_timeout = 0;
2598 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
2599
2600 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2601 /* PSK may have changed from the previous choice, so update
2602 * state machine data based on whatever PSK was selected here.
2603 */
2604 os_memcpy(sm->PMK, pmk, PMK_LEN);
2605 sm->pmk_len = PMK_LEN;
2606 }
2607
2608 sm->MICVerified = TRUE;
2609
2610 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
2611 sm->PTK_valid = TRUE;
2612 }
2613
2614
2615 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2616 {
2617 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2618 sm->TimeoutCtr = 0;
2619 }
2620
2621
2622 #ifdef CONFIG_IEEE80211W
2623
2624 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2625 {
2626 if (sm->mgmt_frame_prot) {
2627 size_t len;
2628 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2629 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
2630 }
2631
2632 return 0;
2633 }
2634
2635
2636 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2637 {
2638 struct wpa_igtk_kde igtk;
2639 struct wpa_group *gsm = sm->group;
2640 u8 rsc[WPA_KEY_RSC_LEN];
2641 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2642
2643 if (!sm->mgmt_frame_prot)
2644 return pos;
2645
2646 igtk.keyid[0] = gsm->GN_igtk;
2647 igtk.keyid[1] = 0;
2648 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
2649 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
2650 os_memset(igtk.pn, 0, sizeof(igtk.pn));
2651 else
2652 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
2653 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
2654 if (sm->wpa_auth->conf.disable_gtk) {
2655 /*
2656 * Provide unique random IGTK to each STA to prevent use of
2657 * IGTK in the BSS.
2658 */
2659 if (random_get_bytes(igtk.igtk, len) < 0)
2660 return pos;
2661 }
2662 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
2663 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
2664 NULL, 0);
2665
2666 return pos;
2667 }
2668
2669 #else /* CONFIG_IEEE80211W */
2670
2671 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2672 {
2673 return 0;
2674 }
2675
2676
2677 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2678 {
2679 return pos;
2680 }
2681
2682 #endif /* CONFIG_IEEE80211W */
2683
2684
2685 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2686 {
2687 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
2688 size_t gtk_len, kde_len;
2689 struct wpa_group *gsm = sm->group;
2690 u8 *wpa_ie;
2691 int wpa_ie_len, secure, keyidx, encr = 0;
2692
2693 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2694 sm->TimeoutEvt = FALSE;
2695
2696 sm->TimeoutCtr++;
2697 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
2698 /* No point in sending the EAPOL-Key - we will disconnect
2699 * immediately following this. */
2700 return;
2701 }
2702
2703 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2704 GTK[GN], IGTK, [FTIE], [TIE * 2])
2705 */
2706 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2707 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2708 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2709 wpa_ie = sm->wpa_auth->wpa_ie;
2710 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
2711 if (sm->wpa == WPA_VERSION_WPA &&
2712 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
2713 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
2714 /* WPA-only STA, remove RSN IE and possible MDIE */
2715 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2716 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
2717 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2718 wpa_ie_len = wpa_ie[1] + 2;
2719 }
2720 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2721 "sending 3/4 msg of 4-Way Handshake");
2722 if (sm->wpa == WPA_VERSION_WPA2) {
2723 /* WPA2 send GTK in the 4-way handshake */
2724 secure = 1;
2725 gtk = gsm->GTK[gsm->GN - 1];
2726 gtk_len = gsm->GTK_len;
2727 if (sm->wpa_auth->conf.disable_gtk) {
2728 /*
2729 * Provide unique random GTK to each STA to prevent use
2730 * of GTK in the BSS.
2731 */
2732 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
2733 return;
2734 gtk = dummy_gtk;
2735 }
2736 keyidx = gsm->GN;
2737 _rsc = rsc;
2738 encr = 1;
2739 } else {
2740 /* WPA does not include GTK in msg 3/4 */
2741 secure = 0;
2742 gtk = NULL;
2743 gtk_len = 0;
2744 keyidx = 0;
2745 _rsc = NULL;
2746 if (sm->rx_eapol_key_secure) {
2747 /*
2748 * It looks like Windows 7 supplicant tries to use
2749 * Secure bit in msg 2/4 after having reported Michael
2750 * MIC failure and it then rejects the 4-way handshake
2751 * if msg 3/4 does not set Secure bit. Work around this
2752 * by setting the Secure bit here even in the case of
2753 * WPA if the supplicant used it first.
2754 */
2755 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2756 "STA used Secure bit in WPA msg 2/4 - "
2757 "set Secure for 3/4 as workaround");
2758 secure = 1;
2759 }
2760 }
2761
2762 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
2763 if (gtk)
2764 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
2765 #ifdef CONFIG_IEEE80211R_AP
2766 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2767 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
2768 kde_len += 300; /* FTIE + 2 * TIE */
2769 }
2770 #endif /* CONFIG_IEEE80211R_AP */
2771 #ifdef CONFIG_P2P
2772 if (WPA_GET_BE32(sm->ip_addr) > 0)
2773 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
2774 #endif /* CONFIG_P2P */
2775 kde = os_malloc(kde_len);
2776 if (kde == NULL)
2777 return;
2778
2779 pos = kde;
2780 os_memcpy(pos, wpa_ie, wpa_ie_len);
2781 pos += wpa_ie_len;
2782 #ifdef CONFIG_IEEE80211R_AP
2783 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2784 int res;
2785 size_t elen;
2786
2787 elen = pos - kde;
2788 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
2789 if (res < 0) {
2790 wpa_printf(MSG_ERROR, "FT: Failed to insert "
2791 "PMKR1Name into RSN IE in EAPOL-Key data");
2792 os_free(kde);
2793 return;
2794 }
2795 pos -= wpa_ie_len;
2796 pos += elen;
2797 }
2798 #endif /* CONFIG_IEEE80211R_AP */
2799 if (gtk) {
2800 u8 hdr[2];
2801 hdr[0] = keyidx & 0x03;
2802 hdr[1] = 0;
2803 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2804 gtk, gtk_len);
2805 }
2806 pos = ieee80211w_kde_add(sm, pos);
2807
2808 #ifdef CONFIG_IEEE80211R_AP
2809 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2810 int res;
2811 struct wpa_auth_config *conf;
2812
2813 conf = &sm->wpa_auth->conf;
2814 if (sm->assoc_resp_ftie &&
2815 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
2816 os_memcpy(pos, sm->assoc_resp_ftie,
2817 2 + sm->assoc_resp_ftie[1]);
2818 res = 2 + sm->assoc_resp_ftie[1];
2819 } else {
2820 res = wpa_write_ftie(conf, conf->r0_key_holder,
2821 conf->r0_key_holder_len,
2822 NULL, NULL, pos,
2823 kde + kde_len - pos,
2824 NULL, 0);
2825 }
2826 if (res < 0) {
2827 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2828 "into EAPOL-Key Key Data");
2829 os_free(kde);
2830 return;
2831 }
2832 pos += res;
2833
2834 /* TIE[ReassociationDeadline] (TU) */
2835 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2836 *pos++ = 5;
2837 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2838 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2839 pos += 4;
2840
2841 /* TIE[KeyLifetime] (seconds) */
2842 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2843 *pos++ = 5;
2844 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2845 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2846 pos += 4;
2847 }
2848 #endif /* CONFIG_IEEE80211R_AP */
2849 #ifdef CONFIG_P2P
2850 if (WPA_GET_BE32(sm->ip_addr) > 0) {
2851 u8 addr[3 * 4];
2852 os_memcpy(addr, sm->ip_addr, 4);
2853 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
2854 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
2855 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
2856 addr, sizeof(addr), NULL, 0);
2857 }
2858 #endif /* CONFIG_P2P */
2859
2860 wpa_send_eapol(sm->wpa_auth, sm,
2861 (secure ? WPA_KEY_INFO_SECURE : 0) |
2862 (wpa_mic_len(sm->wpa_key_mgmt) ? WPA_KEY_INFO_MIC : 0) |
2863 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2864 WPA_KEY_INFO_KEY_TYPE,
2865 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2866 os_free(kde);
2867 }
2868
2869
2870 SM_STATE(WPA_PTK, PTKINITDONE)
2871 {
2872 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2873 sm->EAPOLKeyReceived = FALSE;
2874 if (sm->Pair) {
2875 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2876 int klen = wpa_cipher_key_len(sm->pairwise);
2877 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2878 sm->PTK.tk, klen)) {
2879 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2880 return;
2881 }
2882 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2883 sm->pairwise_set = TRUE;
2884
2885 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2886 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2887 eloop_register_timeout(sm->wpa_auth->conf.
2888 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2889 sm->wpa_auth, sm);
2890 }
2891
2892 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
2893 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
2894 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2895 WPA_EAPOL_authorized, 1);
2896 }
2897 }
2898
2899 if (0 /* IBSS == TRUE */) {
2900 sm->keycount++;
2901 if (sm->keycount == 2) {
2902 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2903 WPA_EAPOL_portValid, 1);
2904 }
2905 } else {
2906 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2907 1);
2908 }
2909 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2910 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2911 if (sm->wpa == WPA_VERSION_WPA)
2912 sm->PInitAKeys = TRUE;
2913 else
2914 sm->has_GTK = TRUE;
2915 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2916 "pairwise key handshake completed (%s)",
2917 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2918
2919 #ifdef CONFIG_IEEE80211R_AP
2920 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2921 #endif /* CONFIG_IEEE80211R_AP */
2922 }
2923
2924
2925 SM_STEP(WPA_PTK)
2926 {
2927 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2928
2929 if (sm->Init)
2930 SM_ENTER(WPA_PTK, INITIALIZE);
2931 else if (sm->Disconnect
2932 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2933 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2934 "WPA_PTK: sm->Disconnect");
2935 SM_ENTER(WPA_PTK, DISCONNECT);
2936 }
2937 else if (sm->DeauthenticationRequest)
2938 SM_ENTER(WPA_PTK, DISCONNECTED);
2939 else if (sm->AuthenticationRequest)
2940 SM_ENTER(WPA_PTK, AUTHENTICATION);
2941 else if (sm->ReAuthenticationRequest)
2942 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2943 else if (sm->PTKRequest)
2944 SM_ENTER(WPA_PTK, PTKSTART);
2945 else switch (sm->wpa_ptk_state) {
2946 case WPA_PTK_INITIALIZE:
2947 break;
2948 case WPA_PTK_DISCONNECT:
2949 SM_ENTER(WPA_PTK, DISCONNECTED);
2950 break;
2951 case WPA_PTK_DISCONNECTED:
2952 SM_ENTER(WPA_PTK, INITIALIZE);
2953 break;
2954 case WPA_PTK_AUTHENTICATION:
2955 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2956 break;
2957 case WPA_PTK_AUTHENTICATION2:
2958 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2959 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2960 WPA_EAPOL_keyRun) > 0)
2961 SM_ENTER(WPA_PTK, INITPMK);
2962 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
2963 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
2964 /* FIX: && 802.1X::keyRun */)
2965 SM_ENTER(WPA_PTK, INITPSK);
2966 break;
2967 case WPA_PTK_INITPMK:
2968 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2969 WPA_EAPOL_keyAvailable) > 0)
2970 SM_ENTER(WPA_PTK, PTKSTART);
2971 else {
2972 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2973 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2974 "INITPMK - keyAvailable = false");
2975 SM_ENTER(WPA_PTK, DISCONNECT);
2976 }
2977 break;
2978 case WPA_PTK_INITPSK:
2979 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
2980 NULL))
2981 SM_ENTER(WPA_PTK, PTKSTART);
2982 else {
2983 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2984 "no PSK configured for the STA");
2985 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2986 SM_ENTER(WPA_PTK, DISCONNECT);
2987 }
2988 break;
2989 case WPA_PTK_PTKSTART:
2990 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2991 sm->EAPOLKeyPairwise)
2992 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2993 else if (sm->TimeoutCtr >
2994 sm->wpa_auth->conf.wpa_pairwise_update_count) {
2995 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2996 wpa_auth_vlogger(
2997 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2998 "PTKSTART: Retry limit %u reached",
2999 sm->wpa_auth->conf.wpa_pairwise_update_count);
3000 SM_ENTER(WPA_PTK, DISCONNECT);
3001 } else if (sm->TimeoutEvt)
3002 SM_ENTER(WPA_PTK, PTKSTART);
3003 break;
3004 case WPA_PTK_PTKCALCNEGOTIATING:
3005 if (sm->MICVerified)
3006 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
3007 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3008 sm->EAPOLKeyPairwise)
3009 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3010 else if (sm->TimeoutEvt)
3011 SM_ENTER(WPA_PTK, PTKSTART);
3012 break;
3013 case WPA_PTK_PTKCALCNEGOTIATING2:
3014 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3015 break;
3016 case WPA_PTK_PTKINITNEGOTIATING:
3017 if (sm->update_snonce)
3018 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3019 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3020 sm->EAPOLKeyPairwise && sm->MICVerified)
3021 SM_ENTER(WPA_PTK, PTKINITDONE);
3022 else if (sm->TimeoutCtr >
3023 sm->wpa_auth->conf.wpa_pairwise_update_count) {
3024 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3025 wpa_auth_vlogger(
3026 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3027 "PTKINITNEGOTIATING: Retry limit %u reached",
3028 sm->wpa_auth->conf.wpa_pairwise_update_count);
3029 SM_ENTER(WPA_PTK, DISCONNECT);
3030 } else if (sm->TimeoutEvt)
3031 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3032 break;
3033 case WPA_PTK_PTKINITDONE:
3034 break;
3035 }
3036 }
3037
3038
3039 SM_STATE(WPA_PTK_GROUP, IDLE)
3040 {
3041 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
3042 if (sm->Init) {
3043 /* Init flag is not cleared here, so avoid busy
3044 * loop by claiming nothing changed. */
3045 sm->changed = FALSE;
3046 }
3047 sm->GTimeoutCtr = 0;
3048 }
3049
3050
3051 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
3052 {
3053 u8 rsc[WPA_KEY_RSC_LEN];
3054 struct wpa_group *gsm = sm->group;
3055 const u8 *kde;
3056 u8 *kde_buf = NULL, *pos, hdr[2];
3057 size_t kde_len;
3058 u8 *gtk, dummy_gtk[32];
3059
3060 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
3061
3062 sm->GTimeoutCtr++;
3063 if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) {
3064 /* No point in sending the EAPOL-Key - we will disconnect
3065 * immediately following this. */
3066 return;
3067 }
3068
3069 if (sm->wpa == WPA_VERSION_WPA)
3070 sm->PInitAKeys = FALSE;
3071 sm->TimeoutEvt = FALSE;
3072 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3073 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3074 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
3075 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3076 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3077 "sending 1/2 msg of Group Key Handshake");
3078
3079 gtk = gsm->GTK[gsm->GN - 1];
3080 if (sm->wpa_auth->conf.disable_gtk) {
3081 /*
3082 * Provide unique random GTK to each STA to prevent use
3083 * of GTK in the BSS.
3084 */
3085 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
3086 return;
3087 gtk = dummy_gtk;
3088 }
3089 if (sm->wpa == WPA_VERSION_WPA2) {
3090 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
3091 ieee80211w_kde_len(sm);
3092 kde_buf = os_malloc(kde_len);
3093 if (kde_buf == NULL)
3094 return;
3095
3096 kde = pos = kde_buf;
3097 hdr[0] = gsm->GN & 0x03;
3098 hdr[1] = 0;
3099 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3100 gtk, gsm->GTK_len);
3101 pos = ieee80211w_kde_add(sm, pos);
3102 kde_len = pos - kde;
3103 } else {
3104 kde = gtk;
3105 kde_len = gsm->GTK_len;
3106 }
3107
3108 wpa_send_eapol(sm->wpa_auth, sm,
3109 WPA_KEY_INFO_SECURE |
3110 (wpa_mic_len(sm->wpa_key_mgmt) ? WPA_KEY_INFO_MIC : 0) |
3111 WPA_KEY_INFO_ACK |
3112 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
3113 rsc, NULL, kde, kde_len, gsm->GN, 1);
3114
3115 os_free(kde_buf);
3116 }
3117
3118
3119 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
3120 {
3121 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
3122 sm->EAPOLKeyReceived = FALSE;
3123 if (sm->GUpdateStationKeys)
3124 sm->group->GKeyDoneStations--;
3125 sm->GUpdateStationKeys = FALSE;
3126 sm->GTimeoutCtr = 0;
3127 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3128 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3129 "group key handshake completed (%s)",
3130 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3131 sm->has_GTK = TRUE;
3132 }
3133
3134
3135 SM_STATE(WPA_PTK_GROUP, KEYERROR)
3136 {
3137 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
3138 if (sm->GUpdateStationKeys)
3139 sm->group->GKeyDoneStations--;
3140 sm->GUpdateStationKeys = FALSE;
3141 sm->Disconnect = TRUE;
3142 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3143 "group key handshake failed (%s) after %u tries",
3144 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
3145 sm->wpa_auth->conf.wpa_group_update_count);
3146 }
3147
3148
3149 SM_STEP(WPA_PTK_GROUP)
3150 {
3151 if (sm->Init || sm->PtkGroupInit) {
3152 SM_ENTER(WPA_PTK_GROUP, IDLE);
3153 sm->PtkGroupInit = FALSE;
3154 } else switch (sm->wpa_ptk_group_state) {
3155 case WPA_PTK_GROUP_IDLE:
3156 if (sm->GUpdateStationKeys ||
3157 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
3158 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3159 break;
3160 case WPA_PTK_GROUP_REKEYNEGOTIATING:
3161 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3162 !sm->EAPOLKeyPairwise && sm->MICVerified)
3163 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
3164 else if (sm->GTimeoutCtr >
3165 sm->wpa_auth->conf.wpa_group_update_count)
3166 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
3167 else if (sm->TimeoutEvt)
3168 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3169 break;
3170 case WPA_PTK_GROUP_KEYERROR:
3171 SM_ENTER(WPA_PTK_GROUP, IDLE);
3172 break;
3173 case WPA_PTK_GROUP_REKEYESTABLISHED:
3174 SM_ENTER(WPA_PTK_GROUP, IDLE);
3175 break;
3176 }
3177 }
3178
3179
3180 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
3181 struct wpa_group *group)
3182 {
3183 int ret = 0;
3184
3185 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3186 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3187 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
3188 wpa_auth->addr, group->GNonce,
3189 group->GTK[group->GN - 1], group->GTK_len) < 0)
3190 ret = -1;
3191 wpa_hexdump_key(MSG_DEBUG, "GTK",
3192 group->GTK[group->GN - 1], group->GTK_len);
3193
3194 #ifdef CONFIG_IEEE80211W
3195 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3196 size_t len;
3197 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3198 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3199 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3200 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
3201 wpa_auth->addr, group->GNonce,
3202 group->IGTK[group->GN_igtk - 4], len) < 0)
3203 ret = -1;
3204 wpa_hexdump_key(MSG_DEBUG, "IGTK",
3205 group->IGTK[group->GN_igtk - 4], len);
3206 }
3207 #endif /* CONFIG_IEEE80211W */
3208
3209 return ret;
3210 }
3211
3212
3213 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
3214 struct wpa_group *group)
3215 {
3216 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3217 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
3218 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
3219 group->wpa_group_state = WPA_GROUP_GTK_INIT;
3220
3221 /* GTK[0..N] = 0 */
3222 os_memset(group->GTK, 0, sizeof(group->GTK));
3223 group->GN = 1;
3224 group->GM = 2;
3225 #ifdef CONFIG_IEEE80211W
3226 group->GN_igtk = 4;
3227 group->GM_igtk = 5;
3228 #endif /* CONFIG_IEEE80211W */
3229 /* GTK[GN] = CalcGTK() */
3230 wpa_gtk_update(wpa_auth, group);
3231 }
3232
3233
3234 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
3235 {
3236 if (ctx != NULL && ctx != sm->group)
3237 return 0;
3238
3239 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
3240 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3241 "Not in PTKINITDONE; skip Group Key update");
3242 sm->GUpdateStationKeys = FALSE;
3243 return 0;
3244 }
3245 if (sm->GUpdateStationKeys) {
3246 /*
3247 * This should not really happen, so add a debug log entry.
3248 * Since we clear the GKeyDoneStations before the loop, the
3249 * station needs to be counted here anyway.
3250 */
3251 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3252 "GUpdateStationKeys was already set when "
3253 "marking station for GTK rekeying");
3254 }
3255
3256 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
3257 if (sm->is_wnmsleep)
3258 return 0;
3259
3260 sm->group->GKeyDoneStations++;
3261 sm->GUpdateStationKeys = TRUE;
3262
3263 wpa_sm_step(sm);
3264 return 0;
3265 }
3266
3267
3268 #ifdef CONFIG_WNM
3269 /* update GTK when exiting WNM-Sleep Mode */
3270 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
3271 {
3272 if (sm == NULL || sm->is_wnmsleep)
3273 return;
3274
3275 wpa_group_update_sta(sm, NULL);
3276 }
3277
3278
3279 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
3280 {
3281 if (sm)
3282 sm->is_wnmsleep = !!flag;
3283 }
3284
3285
3286 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3287 {
3288 struct wpa_group *gsm = sm->group;
3289 u8 *start = pos;
3290
3291 /*
3292 * GTK subelement:
3293 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
3294 * Key[5..32]
3295 */
3296 *pos++ = WNM_SLEEP_SUBELEM_GTK;
3297 *pos++ = 11 + gsm->GTK_len;
3298 /* Key ID in B0-B1 of Key Info */
3299 WPA_PUT_LE16(pos, gsm->GN & 0x03);
3300 pos += 2;
3301 *pos++ = gsm->GTK_len;
3302 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
3303 return 0;
3304 pos += 8;
3305 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3306 pos += gsm->GTK_len;
3307
3308 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3309 gsm->GN);
3310 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
3311 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3312
3313 return pos - start;
3314 }
3315
3316
3317 #ifdef CONFIG_IEEE80211W
3318 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3319 {
3320 struct wpa_group *gsm = sm->group;
3321 u8 *start = pos;
3322 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3323
3324 /*
3325 * IGTK subelement:
3326 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3327 */
3328 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
3329 *pos++ = 2 + 6 + len;
3330 WPA_PUT_LE16(pos, gsm->GN_igtk);
3331 pos += 2;
3332 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
3333 return 0;
3334 pos += 6;
3335
3336 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
3337 pos += len;
3338
3339 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3340 gsm->GN_igtk);
3341 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
3342 gsm->IGTK[gsm->GN_igtk - 4], len);
3343
3344 return pos - start;
3345 }
3346 #endif /* CONFIG_IEEE80211W */
3347 #endif /* CONFIG_WNM */
3348
3349
3350 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
3351 struct wpa_group *group)
3352 {
3353 int tmp;
3354
3355 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3356 "SETKEYS (VLAN-ID %d)", group->vlan_id);
3357 group->changed = TRUE;
3358 group->wpa_group_state = WPA_GROUP_SETKEYS;
3359 group->GTKReKey = FALSE;
3360 tmp = group->GM;
3361 group->GM = group->GN;
3362 group->GN = tmp;
3363 #ifdef CONFIG_IEEE80211W
3364 tmp = group->GM_igtk;
3365 group->GM_igtk = group->GN_igtk;
3366 group->GN_igtk = tmp;
3367 #endif /* CONFIG_IEEE80211W */
3368 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3369 * counting the STAs that are marked with GUpdateStationKeys instead of
3370 * including all STAs that could be in not-yet-completed state. */
3371 wpa_gtk_update(wpa_auth, group);
3372
3373 if (group->GKeyDoneStations) {
3374 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
3375 "GKeyDoneStations=%d when starting new GTK rekey",
3376 group->GKeyDoneStations);
3377 group->GKeyDoneStations = 0;
3378 }
3379 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
3380 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
3381 group->GKeyDoneStations);
3382 }
3383
3384
3385 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
3386 struct wpa_group *group)
3387 {
3388 int ret = 0;
3389
3390 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
3391 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
3392 broadcast_ether_addr, group->GN,
3393 group->GTK[group->GN - 1], group->GTK_len) < 0)
3394 ret = -1;
3395
3396 #ifdef CONFIG_IEEE80211W
3397 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3398 enum wpa_alg alg;
3399 size_t len;
3400
3401 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
3402 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3403
3404 if (ret == 0 &&
3405 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
3406 broadcast_ether_addr, group->GN_igtk,
3407 group->IGTK[group->GN_igtk - 4], len) < 0)
3408 ret = -1;
3409 }
3410 #endif /* CONFIG_IEEE80211W */
3411
3412 return ret;
3413 }
3414
3415
3416 static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
3417 {
3418 if (sm->group == ctx) {
3419 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
3420 " for discconnection due to fatal failure",
3421 MAC2STR(sm->addr));
3422 sm->Disconnect = TRUE;
3423 }
3424
3425 return 0;
3426 }
3427
3428
3429 static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
3430 struct wpa_group *group)
3431 {
3432 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
3433 group->changed = TRUE;
3434 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
3435 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
3436 }
3437
3438
3439 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
3440 struct wpa_group *group)
3441 {
3442 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3443 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
3444 group->changed = TRUE;
3445 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
3446
3447 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
3448 wpa_group_fatal_failure(wpa_auth, group);
3449 return -1;
3450 }
3451
3452 return 0;
3453 }
3454
3455
3456 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
3457 struct wpa_group *group)
3458 {
3459 if (group->GInit) {
3460 wpa_group_gtk_init(wpa_auth, group);
3461 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
3462 /* Do not allow group operations */
3463 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
3464 group->GTKAuthenticator) {
3465 wpa_group_setkeysdone(wpa_auth, group);
3466 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
3467 group->GTKReKey) {
3468 wpa_group_setkeys(wpa_auth, group);
3469 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
3470 if (group->GKeyDoneStations == 0)
3471 wpa_group_setkeysdone(wpa_auth, group);
3472 else if (group->GTKReKey)
3473 wpa_group_setkeys(wpa_auth, group);
3474 }
3475 }
3476
3477
3478 static int wpa_sm_step(struct wpa_state_machine *sm)
3479 {
3480 if (sm == NULL)
3481 return 0;
3482
3483 if (sm->in_step_loop) {
3484 /* This should not happen, but if it does, make sure we do not
3485 * end up freeing the state machine too early by exiting the
3486 * recursive call. */
3487 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
3488 return 0;
3489 }
3490
3491 sm->in_step_loop = 1;
3492 do {
3493 if (sm->pending_deinit)
3494 break;
3495
3496 sm->changed = FALSE;
3497 sm->wpa_auth->group->changed = FALSE;
3498
3499 SM_STEP_RUN(WPA_PTK);
3500 if (sm->pending_deinit)
3501 break;
3502 SM_STEP_RUN(WPA_PTK_GROUP);
3503 if (sm->pending_deinit)
3504 break;
3505 wpa_group_sm_step(sm->wpa_auth, sm->group);
3506 } while (sm->changed || sm->wpa_auth->group->changed);
3507 sm->in_step_loop = 0;
3508
3509 if (sm->pending_deinit) {
3510 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
3511 "machine deinit for " MACSTR, MAC2STR(sm->addr));
3512 wpa_free_sta_sm(sm);
3513 return 1;
3514 }
3515 return 0;
3516 }
3517
3518
3519 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
3520 {
3521 struct wpa_state_machine *sm = eloop_ctx;
3522 wpa_sm_step(sm);
3523 }
3524
3525
3526 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
3527 {
3528 if (sm == NULL)
3529 return;
3530 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
3531 }
3532
3533
3534 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
3535 {
3536 int tmp, i;
3537 struct wpa_group *group;
3538
3539 if (wpa_auth == NULL)
3540 return;
3541
3542 group = wpa_auth->group;
3543
3544 for (i = 0; i < 2; i++) {
3545 tmp = group->GM;
3546 group->GM = group->GN;
3547 group->GN = tmp;
3548 #ifdef CONFIG_IEEE80211W
3549 tmp = group->GM_igtk;
3550 group->GM_igtk = group->GN_igtk;
3551 group->GN_igtk = tmp;
3552 #endif /* CONFIG_IEEE80211W */
3553 wpa_gtk_update(wpa_auth, group);
3554 wpa_group_config_group_keys(wpa_auth, group);
3555 }
3556 }
3557
3558
3559 static const char * wpa_bool_txt(int val)
3560 {
3561 return val ? "TRUE" : "FALSE";
3562 }
3563
3564
3565 #define RSN_SUITE "%02x-%02x-%02x-%d"
3566 #define RSN_SUITE_ARG(s) \
3567 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3568
3569 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
3570 {
3571 int len = 0, ret;
3572 char pmkid_txt[PMKID_LEN * 2 + 1];
3573 #ifdef CONFIG_RSN_PREAUTH
3574 const int preauth = 1;
3575 #else /* CONFIG_RSN_PREAUTH */
3576 const int preauth = 0;
3577 #endif /* CONFIG_RSN_PREAUTH */
3578
3579 if (wpa_auth == NULL)
3580 return len;
3581
3582 ret = os_snprintf(buf + len, buflen - len,
3583 "dot11RSNAOptionImplemented=TRUE\n"
3584 "dot11RSNAPreauthenticationImplemented=%s\n"
3585 "dot11RSNAEnabled=%s\n"
3586 "dot11RSNAPreauthenticationEnabled=%s\n",
3587 wpa_bool_txt(preauth),
3588 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
3589 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
3590 if (os_snprintf_error(buflen - len, ret))
3591 return len;
3592 len += ret;
3593
3594 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
3595 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
3596
3597 ret = os_snprintf(
3598 buf + len, buflen - len,
3599 "dot11RSNAConfigVersion=%u\n"
3600 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
3601 /* FIX: dot11RSNAConfigGroupCipher */
3602 /* FIX: dot11RSNAConfigGroupRekeyMethod */
3603 /* FIX: dot11RSNAConfigGroupRekeyTime */
3604 /* FIX: dot11RSNAConfigGroupRekeyPackets */
3605 "dot11RSNAConfigGroupRekeyStrict=%u\n"
3606 "dot11RSNAConfigGroupUpdateCount=%u\n"
3607 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
3608 "dot11RSNAConfigGroupCipherSize=%u\n"
3609 "dot11RSNAConfigPMKLifetime=%u\n"
3610 "dot11RSNAConfigPMKReauthThreshold=%u\n"
3611 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
3612 "dot11RSNAConfigSATimeout=%u\n"
3613 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
3614 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
3615 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
3616 "dot11RSNAPMKIDUsed=%s\n"
3617 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
3618 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
3619 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
3620 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
3621 "dot11RSNA4WayHandshakeFailures=%u\n"
3622 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
3623 RSN_VERSION,
3624 !!wpa_auth->conf.wpa_strict_rekey,
3625 wpa_auth->conf.wpa_group_update_count,
3626 wpa_auth->conf.wpa_pairwise_update_count,
3627 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
3628 dot11RSNAConfigPMKLifetime,
3629 dot11RSNAConfigPMKReauthThreshold,
3630 dot11RSNAConfigSATimeout,
3631 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
3632 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
3633 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
3634 pmkid_txt,
3635 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
3636 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
3637 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
3638 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
3639 wpa_auth->dot11RSNA4WayHandshakeFailures);
3640 if (os_snprintf_error(buflen - len, ret))
3641 return len;
3642 len += ret;
3643
3644 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
3645 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
3646
3647 /* Private MIB */
3648 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
3649 wpa_auth->group->wpa_group_state);
3650 if (os_snprintf_error(buflen - len, ret))
3651 return len;
3652 len += ret;
3653
3654 return len;
3655 }
3656
3657
3658 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
3659 {
3660 int len = 0, ret;
3661 u32 pairwise = 0;
3662
3663 if (sm == NULL)
3664 return 0;
3665
3666 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
3667
3668 /* dot11RSNAStatsEntry */
3669
3670 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
3671 WPA_PROTO_RSN : WPA_PROTO_WPA,
3672 sm->pairwise);
3673 if (pairwise == 0)
3674 return 0;
3675
3676 ret = os_snprintf(
3677 buf + len, buflen - len,
3678 /* TODO: dot11RSNAStatsIndex */
3679 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
3680 "dot11RSNAStatsVersion=1\n"
3681 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
3682 /* TODO: dot11RSNAStatsTKIPICVErrors */
3683 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
3684 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
3685 /* TODO: dot11RSNAStatsCCMPReplays */
3686 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
3687 /* TODO: dot11RSNAStatsTKIPReplays */,
3688 MAC2STR(sm->addr),
3689 RSN_SUITE_ARG(pairwise),
3690 sm->dot11RSNAStatsTKIPLocalMICFailures,
3691 sm->dot11RSNAStatsTKIPRemoteMICFailures);
3692 if (os_snprintf_error(buflen - len, ret))
3693 return len;
3694 len += ret;
3695
3696 /* Private MIB */
3697 ret = os_snprintf(buf + len, buflen - len,
3698 "hostapdWPAPTKState=%d\n"
3699 "hostapdWPAPTKGroupState=%d\n",
3700 sm->wpa_ptk_state,
3701 sm->wpa_ptk_group_state);
3702 if (os_snprintf_error(buflen - len, ret))
3703 return len;
3704 len += ret;
3705
3706 return len;
3707 }
3708
3709
3710 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
3711 {
3712 if (wpa_auth)
3713 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
3714 }
3715
3716
3717 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
3718 {
3719 return sm && sm->pairwise_set;
3720 }
3721
3722
3723 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
3724 {
3725 return sm->pairwise;
3726 }
3727
3728
3729 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
3730 {
3731 if (sm == NULL)
3732 return -1;
3733 return sm->wpa_key_mgmt;
3734 }
3735
3736
3737 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
3738 {
3739 if (sm == NULL)
3740 return 0;
3741 return sm->wpa;
3742 }
3743
3744
3745 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
3746 struct rsn_pmksa_cache_entry *entry)
3747 {
3748 if (sm == NULL || sm->pmksa != entry)
3749 return -1;
3750 sm->pmksa = NULL;
3751 return 0;
3752 }
3753
3754
3755 struct rsn_pmksa_cache_entry *
3756 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
3757 {
3758 return sm ? sm->pmksa : NULL;
3759 }
3760
3761
3762 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
3763 {
3764 if (sm)
3765 sm->dot11RSNAStatsTKIPLocalMICFailures++;
3766 }
3767
3768
3769 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
3770 {
3771 if (wpa_auth == NULL)
3772 return NULL;
3773 *len = wpa_auth->wpa_ie_len;
3774 return wpa_auth->wpa_ie;
3775 }
3776
3777
3778 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
3779 unsigned int pmk_len,
3780 int session_timeout, struct eapol_state_machine *eapol)
3781 {
3782 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
3783 sm->wpa_auth->conf.disable_pmksa_caching)
3784 return -1;
3785
3786 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
3787 if (pmk_len > PMK_LEN_SUITE_B_192)
3788 pmk_len = PMK_LEN_SUITE_B_192;
3789 } else if (pmk_len > PMK_LEN) {
3790 pmk_len = PMK_LEN;
3791 }
3792
3793 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
3794 sm->PTK.kck, sm->PTK.kck_len,
3795 sm->wpa_auth->addr, sm->addr, session_timeout,
3796 eapol, sm->wpa_key_mgmt))
3797 return 0;
3798
3799 return -1;
3800 }
3801
3802
3803 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
3804 const u8 *pmk, size_t len, const u8 *sta_addr,
3805 int session_timeout,
3806 struct eapol_state_machine *eapol)
3807 {
3808 if (wpa_auth == NULL)
3809 return -1;
3810
3811 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
3812 NULL, 0,
3813 wpa_auth->addr,
3814 sta_addr, session_timeout, eapol,
3815 WPA_KEY_MGMT_IEEE8021X))
3816 return 0;
3817
3818 return -1;
3819 }
3820
3821
3822 int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
3823 const u8 *pmk, const u8 *pmkid)
3824 {
3825 if (wpa_auth->conf.disable_pmksa_caching)
3826 return -1;
3827
3828 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
3829 NULL, 0,
3830 wpa_auth->addr, addr, 0, NULL,
3831 WPA_KEY_MGMT_SAE))
3832 return 0;
3833
3834 return -1;
3835 }
3836
3837
3838 void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
3839 const u8 *sta_addr)
3840 {
3841 struct rsn_pmksa_cache_entry *pmksa;
3842
3843 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
3844 return;
3845 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3846 if (pmksa) {
3847 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
3848 MACSTR " based on request", MAC2STR(sta_addr));
3849 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
3850 }
3851 }
3852
3853
3854 int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
3855 size_t len)
3856 {
3857 if (!wpa_auth || !wpa_auth->pmksa)
3858 return 0;
3859 return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
3860 }
3861
3862
3863 void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
3864 {
3865 if (wpa_auth && wpa_auth->pmksa)
3866 pmksa_cache_auth_flush(wpa_auth->pmksa);
3867 }
3868
3869
3870 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
3871 #ifdef CONFIG_MESH
3872
3873 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
3874 char *buf, size_t len)
3875 {
3876 if (!wpa_auth || !wpa_auth->pmksa)
3877 return 0;
3878
3879 return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
3880 }
3881
3882
3883 struct rsn_pmksa_cache_entry *
3884 wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
3885 const u8 *pmkid, int expiration)
3886 {
3887 struct rsn_pmksa_cache_entry *entry;
3888 struct os_reltime now;
3889
3890 entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa,
3891 spa, 0, NULL, WPA_KEY_MGMT_SAE);
3892 if (!entry)
3893 return NULL;
3894
3895 os_get_reltime(&now);
3896 entry->expiration = now.sec + expiration;
3897 return entry;
3898 }
3899
3900
3901 int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
3902 struct rsn_pmksa_cache_entry *entry)
3903 {
3904 int ret;
3905
3906 if (!wpa_auth || !wpa_auth->pmksa)
3907 return -1;
3908
3909 ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
3910 if (ret < 0)
3911 wpa_printf(MSG_DEBUG,
3912 "RSN: Failed to store external PMKSA cache for "
3913 MACSTR, MAC2STR(entry->spa));
3914
3915 return ret;
3916 }
3917
3918 #endif /* CONFIG_MESH */
3919 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
3920
3921
3922 struct rsn_pmksa_cache_entry *
3923 wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
3924 const u8 *pmkid)
3925 {
3926 if (!wpa_auth || !wpa_auth->pmksa)
3927 return NULL;
3928 return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
3929 }
3930
3931
3932 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
3933 struct wpa_state_machine *sm,
3934 struct wpa_authenticator *wpa_auth,
3935 u8 *pmkid, u8 *pmk)
3936 {
3937 if (!sm)
3938 return;
3939
3940 sm->pmksa = pmksa;
3941 os_memcpy(pmk, pmksa->pmk, PMK_LEN);
3942 os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
3943 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
3944 }
3945
3946
3947 /*
3948 * Remove and free the group from wpa_authenticator. This is triggered by a
3949 * callback to make sure nobody is currently iterating the group list while it
3950 * gets modified.
3951 */
3952 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
3953 struct wpa_group *group)
3954 {
3955 struct wpa_group *prev = wpa_auth->group;
3956
3957 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
3958 group->vlan_id);
3959
3960 while (prev) {
3961 if (prev->next == group) {
3962 /* This never frees the special first group as needed */
3963 prev->next = group->next;
3964 os_free(group);
3965 break;
3966 }
3967 prev = prev->next;
3968 }
3969
3970 }
3971
3972
3973 /* Increase the reference counter for group */
3974 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
3975 struct wpa_group *group)
3976 {
3977 /* Skip the special first group */
3978 if (wpa_auth->group == group)
3979 return;
3980
3981 group->references++;
3982 }
3983
3984
3985 /* Decrease the reference counter and maybe free the group */
3986 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
3987 struct wpa_group *group)
3988 {
3989 /* Skip the special first group */
3990 if (wpa_auth->group == group)
3991 return;
3992
3993 group->references--;
3994 if (group->references)
3995 return;
3996 wpa_group_free(wpa_auth, group);
3997 }
3998
3999
4000 /*
4001 * Add a group that has its references counter set to zero. Caller needs to
4002 * call wpa_group_get() on the return value to mark the entry in use.
4003 */
4004 static struct wpa_group *
4005 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4006 {
4007 struct wpa_group *group;
4008
4009 if (wpa_auth == NULL || wpa_auth->group == NULL)
4010 return NULL;
4011
4012 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
4013 vlan_id);
4014 group = wpa_group_init(wpa_auth, vlan_id, 0);
4015 if (group == NULL)
4016 return NULL;
4017
4018 group->next = wpa_auth->group->next;
4019 wpa_auth->group->next = group;
4020
4021 return group;
4022 }
4023
4024
4025 /*
4026 * Enforce that the group state machine for the VLAN is running, increase
4027 * reference counter as interface is up. References might have been increased
4028 * even if a negative value is returned.
4029 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4030 */
4031 int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4032 {
4033 struct wpa_group *group;
4034
4035 if (wpa_auth == NULL)
4036 return 0;
4037
4038 group = wpa_auth->group;
4039 while (group) {
4040 if (group->vlan_id == vlan_id)
4041 break;
4042 group = group->next;
4043 }
4044
4045 if (group == NULL) {
4046 group = wpa_auth_add_group(wpa_auth, vlan_id);
4047 if (group == NULL)
4048 return -1;
4049 }
4050
4051 wpa_printf(MSG_DEBUG,
4052 "WPA: Ensure group state machine running for VLAN ID %d",
4053 vlan_id);
4054
4055 wpa_group_get(wpa_auth, group);
4056 group->num_setup_iface++;
4057
4058 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4059 return -1;
4060
4061 return 0;
4062 }
4063
4064
4065 /*
4066 * Decrease reference counter, expected to be zero afterwards.
4067 * returns: -1 on error (group not found, group in fail state)
4068 * -2 if wpa_group is still referenced
4069 * 0 else
4070 */
4071 int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4072 {
4073 struct wpa_group *group;
4074 int ret = 0;
4075
4076 if (wpa_auth == NULL)
4077 return 0;
4078
4079 group = wpa_auth->group;
4080 while (group) {
4081 if (group->vlan_id == vlan_id)
4082 break;
4083 group = group->next;
4084 }
4085
4086 if (group == NULL)
4087 return -1;
4088
4089 wpa_printf(MSG_DEBUG,
4090 "WPA: Try stopping group state machine for VLAN ID %d",
4091 vlan_id);
4092
4093 if (group->num_setup_iface <= 0) {
4094 wpa_printf(MSG_ERROR,
4095 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4096 vlan_id);
4097 return -1;
4098 }
4099 group->num_setup_iface--;
4100
4101 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4102 ret = -1;
4103
4104 if (group->references > 1) {
4105 wpa_printf(MSG_DEBUG,
4106 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4107 vlan_id);
4108 ret = -2;
4109 }
4110
4111 wpa_group_put(wpa_auth, group);
4112
4113 return ret;
4114 }
4115
4116
4117 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
4118 {
4119 struct wpa_group *group;
4120
4121 if (sm == NULL || sm->wpa_auth == NULL)
4122 return 0;
4123
4124 group = sm->wpa_auth->group;
4125 while (group) {
4126 if (group->vlan_id == vlan_id)
4127 break;
4128 group = group->next;
4129 }
4130
4131 if (group == NULL) {
4132 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
4133 if (group == NULL)
4134 return -1;
4135 }
4136
4137 if (sm->group == group)
4138 return 0;
4139
4140 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4141 return -1;
4142
4143 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
4144 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
4145
4146 wpa_group_get(sm->wpa_auth, group);
4147 wpa_group_put(sm->wpa_auth, sm->group);
4148 sm->group = group;
4149
4150 return 0;
4151 }
4152
4153
4154 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
4155 struct wpa_state_machine *sm, int ack)
4156 {
4157 if (wpa_auth == NULL || sm == NULL)
4158 return;
4159 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
4160 " ack=%d", MAC2STR(sm->addr), ack);
4161 if (sm->pending_1_of_4_timeout && ack) {
4162 /*
4163 * Some deployed supplicant implementations update their SNonce
4164 * for each EAPOL-Key 2/4 message even within the same 4-way
4165 * handshake and then fail to use the first SNonce when
4166 * deriving the PTK. This results in unsuccessful 4-way
4167 * handshake whenever the relatively short initial timeout is
4168 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4169 * around this by increasing the timeout now that we know that
4170 * the station has received the frame.
4171 */
4172 int timeout_ms = eapol_key_timeout_subseq;
4173 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
4174 "timeout by %u ms because of acknowledged frame",
4175 timeout_ms);
4176 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
4177 eloop_register_timeout(timeout_ms / 1000,
4178 (timeout_ms % 1000) * 1000,
4179 wpa_send_eapol_timeout, wpa_auth, sm);
4180 }
4181 }
4182
4183
4184 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
4185 {
4186 if (sm == NULL)
4187 return 0;
4188 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
4189 }
4190
4191
4192 int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
4193 {
4194 if (sm == NULL)
4195 return 0;
4196 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
4197 }
4198
4199
4200 #ifdef CONFIG_P2P
4201 int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
4202 {
4203 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
4204 return -1;
4205 os_memcpy(addr, sm->ip_addr, 4);
4206 return 0;
4207 }
4208 #endif /* CONFIG_P2P */
4209
4210
4211 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
4212 struct radius_das_attrs *attr)
4213 {
4214 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
4215 }
4216
4217
4218 void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
4219 {
4220 struct wpa_group *group;
4221
4222 if (!wpa_auth)
4223 return;
4224 for (group = wpa_auth->group; group; group = group->next)
4225 wpa_group_config_group_keys(wpa_auth, group);
4226 }
4227
4228
4229 #ifdef CONFIG_FILS
4230
4231 struct wpa_auth_fils_iter_data {
4232 struct wpa_authenticator *auth;
4233 const u8 *cache_id;
4234 struct rsn_pmksa_cache_entry *pmksa;
4235 const u8 *spa;
4236 const u8 *pmkid;
4237 };
4238
4239
4240 static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
4241 {
4242 struct wpa_auth_fils_iter_data *data = ctx;
4243
4244 if (a == data->auth || !a->conf.fils_cache_id ||
4245 os_memcmp(a->conf.fils_cache_id, data->cache_id,
4246 FILS_CACHE_ID_LEN) != 0)
4247 return 0;
4248 data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
4249 return data->pmksa != NULL;
4250 }
4251
4252
4253 struct rsn_pmksa_cache_entry *
4254 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
4255 const u8 *sta_addr, const u8 *pmkid)
4256 {
4257 struct wpa_auth_fils_iter_data idata;
4258
4259 if (!wpa_auth->conf.fils_cache_id_set)
4260 return NULL;
4261 idata.auth = wpa_auth;
4262 idata.cache_id = wpa_auth->conf.fils_cache_id;
4263 idata.pmksa = NULL;
4264 idata.spa = sta_addr;
4265 idata.pmkid = pmkid;
4266 wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
4267 return idata.pmksa;
4268 }
4269
4270 #endif /* CONFIG_FILS */