]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/wpa_auth.c
PeerKey: Remove dead code related to STSL negotiation state
[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 wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK);
861
862 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len)
863 == 0) {
864 ok = 1;
865 break;
866 }
867
868 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
869 break;
870 }
871
872 if (!ok) {
873 wpa_printf(MSG_DEBUG,
874 "WPA: Earlier SNonce did not result in matching MIC");
875 return -1;
876 }
877
878 wpa_printf(MSG_DEBUG,
879 "WPA: Earlier SNonce resulted in matching MIC");
880 sm->alt_snonce_valid = 0;
881 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
882 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
883 sm->PTK_valid = TRUE;
884
885 return 0;
886 }
887
888
889 void wpa_receive(struct wpa_authenticator *wpa_auth,
890 struct wpa_state_machine *sm,
891 u8 *data, size_t data_len)
892 {
893 struct ieee802_1x_hdr *hdr;
894 struct wpa_eapol_key *key;
895 u16 key_info, key_data_length;
896 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
897 SMK_M1, SMK_M3, SMK_ERROR } msg;
898 char *msgtxt;
899 struct wpa_eapol_ie_parse kde;
900 const u8 *key_data;
901 size_t keyhdrlen, mic_len;
902 u8 *mic;
903
904 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
905 return;
906 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
907
908 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
909 keyhdrlen = sizeof(*key) + mic_len + 2;
910
911 if (data_len < sizeof(*hdr) + keyhdrlen) {
912 wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
913 return;
914 }
915
916 hdr = (struct ieee802_1x_hdr *) data;
917 key = (struct wpa_eapol_key *) (hdr + 1);
918 mic = (u8 *) (key + 1);
919 key_info = WPA_GET_BE16(key->key_info);
920 key_data = mic + mic_len + 2;
921 key_data_length = WPA_GET_BE16(mic + mic_len);
922 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
923 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
924 MAC2STR(sm->addr), key_info, key->type,
925 (unsigned int) mic_len, key_data_length);
926 wpa_hexdump(MSG_MSGDUMP,
927 "WPA: EAPOL-Key header (ending before Key MIC)",
928 key, sizeof(*key));
929 wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
930 mic, mic_len);
931 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
932 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
933 "key_data overflow (%d > %lu)",
934 key_data_length,
935 (unsigned long) (data_len - sizeof(*hdr) -
936 keyhdrlen));
937 return;
938 }
939
940 if (sm->wpa == WPA_VERSION_WPA2) {
941 if (key->type == EAPOL_KEY_TYPE_WPA) {
942 /*
943 * Some deployed station implementations seem to send
944 * msg 4/4 with incorrect type value in WPA2 mode.
945 */
946 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
947 "with unexpected WPA type in RSN mode");
948 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
949 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
950 "unexpected type %d in RSN mode",
951 key->type);
952 return;
953 }
954 } else {
955 if (key->type != EAPOL_KEY_TYPE_WPA) {
956 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
957 "unexpected type %d in WPA mode",
958 key->type);
959 return;
960 }
961 }
962
963 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
964 WPA_NONCE_LEN);
965 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
966 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
967
968 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
969 * are set */
970
971 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
972 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
973 if (key_info & WPA_KEY_INFO_ERROR) {
974 msg = SMK_ERROR;
975 msgtxt = "SMK Error";
976 } else {
977 msg = SMK_M1;
978 msgtxt = "SMK M1";
979 }
980 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
981 msg = SMK_M3;
982 msgtxt = "SMK M3";
983 } else if (key_info & WPA_KEY_INFO_REQUEST) {
984 msg = REQUEST;
985 msgtxt = "Request";
986 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
987 msg = GROUP_2;
988 msgtxt = "2/2 Group";
989 } else if (key_data_length == 0 ||
990 (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
991 key_data_length == AES_BLOCK_SIZE)) {
992 msg = PAIRWISE_4;
993 msgtxt = "4/4 Pairwise";
994 } else {
995 msg = PAIRWISE_2;
996 msgtxt = "2/4 Pairwise";
997 }
998
999 /* TODO: key_info type validation for PeerKey */
1000 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
1001 msg == GROUP_2) {
1002 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1003 if (sm->pairwise == WPA_CIPHER_CCMP ||
1004 sm->pairwise == WPA_CIPHER_GCMP) {
1005 if (wpa_use_aes_cmac(sm) &&
1006 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
1007 !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
1008 !wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1009 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1010 wpa_auth_logger(wpa_auth, sm->addr,
1011 LOGGER_WARNING,
1012 "advertised support for "
1013 "AES-128-CMAC, but did not "
1014 "use it");
1015 return;
1016 }
1017
1018 if (!wpa_use_aes_cmac(sm) &&
1019 !wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1020 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1021 wpa_auth_logger(wpa_auth, sm->addr,
1022 LOGGER_WARNING,
1023 "did not use HMAC-SHA1-AES "
1024 "with CCMP/GCMP");
1025 return;
1026 }
1027 }
1028
1029 if ((wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1030 wpa_key_mgmt_fils(sm->wpa_key_mgmt)) &&
1031 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1032 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1033 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1034 return;
1035 }
1036 }
1037
1038 if (key_info & WPA_KEY_INFO_REQUEST) {
1039 if (sm->req_replay_counter_used &&
1040 os_memcmp(key->replay_counter, sm->req_replay_counter,
1041 WPA_REPLAY_COUNTER_LEN) <= 0) {
1042 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1043 "received EAPOL-Key request with "
1044 "replayed counter");
1045 return;
1046 }
1047 }
1048
1049 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
1050 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
1051 int i;
1052
1053 if (msg == PAIRWISE_2 &&
1054 wpa_replay_counter_valid(sm->prev_key_replay,
1055 key->replay_counter) &&
1056 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1057 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1058 {
1059 /*
1060 * Some supplicant implementations (e.g., Windows XP
1061 * WZC) update SNonce for each EAPOL-Key 2/4. This
1062 * breaks the workaround on accepting any of the
1063 * pending requests, so allow the SNonce to be updated
1064 * even if we have already sent out EAPOL-Key 3/4.
1065 */
1066 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1067 "Process SNonce update from STA "
1068 "based on retransmitted EAPOL-Key "
1069 "1/4");
1070 sm->update_snonce = 1;
1071 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1072 sm->alt_snonce_valid = TRUE;
1073 os_memcpy(sm->alt_replay_counter,
1074 sm->key_replay[0].counter,
1075 WPA_REPLAY_COUNTER_LEN);
1076 goto continue_processing;
1077 }
1078
1079 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1080 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1081 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1082 WPA_REPLAY_COUNTER_LEN) == 0) {
1083 /*
1084 * Supplicant may still be using the old SNonce since
1085 * there was two EAPOL-Key 2/4 messages and they had
1086 * different SNonce values.
1087 */
1088 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1089 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1090 goto continue_processing;
1091 }
1092
1093 if (msg == PAIRWISE_2 &&
1094 wpa_replay_counter_valid(sm->prev_key_replay,
1095 key->replay_counter) &&
1096 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1097 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1098 "ignore retransmitted EAPOL-Key %s - "
1099 "SNonce did not change", msgtxt);
1100 } else {
1101 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1102 "received EAPOL-Key %s with "
1103 "unexpected replay counter", msgtxt);
1104 }
1105 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1106 if (!sm->key_replay[i].valid)
1107 break;
1108 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1109 sm->key_replay[i].counter,
1110 WPA_REPLAY_COUNTER_LEN);
1111 }
1112 wpa_hexdump(MSG_DEBUG, "received replay counter",
1113 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1114 return;
1115 }
1116
1117 continue_processing:
1118 #ifdef CONFIG_FILS
1119 if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
1120 !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1121 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1122 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1123 return;
1124 }
1125 #endif /* CONFIG_FILS */
1126
1127 switch (msg) {
1128 case PAIRWISE_2:
1129 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
1130 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1131 (!sm->update_snonce ||
1132 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
1133 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1134 "received EAPOL-Key msg 2/4 in "
1135 "invalid state (%d) - dropped",
1136 sm->wpa_ptk_state);
1137 return;
1138 }
1139 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1140 if (sm->group->reject_4way_hs_for_entropy) {
1141 /*
1142 * The system did not have enough entropy to generate
1143 * strong random numbers. Reject the first 4-way
1144 * handshake(s) and collect some entropy based on the
1145 * information from it. Once enough entropy is
1146 * available, the next atempt will trigger GMK/Key
1147 * Counter update and the station will be allowed to
1148 * continue.
1149 */
1150 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1151 "collect more entropy for random number "
1152 "generation");
1153 random_mark_pool_ready();
1154 wpa_sta_disconnect(wpa_auth, sm->addr);
1155 return;
1156 }
1157 break;
1158 case PAIRWISE_4:
1159 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1160 !sm->PTK_valid) {
1161 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1162 "received EAPOL-Key msg 4/4 in "
1163 "invalid state (%d) - dropped",
1164 sm->wpa_ptk_state);
1165 return;
1166 }
1167 break;
1168 case GROUP_2:
1169 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1170 || !sm->PTK_valid) {
1171 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1172 "received EAPOL-Key msg 2/2 in "
1173 "invalid state (%d) - dropped",
1174 sm->wpa_ptk_group_state);
1175 return;
1176 }
1177 break;
1178 #ifdef CONFIG_PEERKEY
1179 case SMK_M1:
1180 case SMK_M3:
1181 case SMK_ERROR:
1182 if (!wpa_auth->conf.peerkey) {
1183 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1184 "PeerKey use disabled - ignoring message");
1185 return;
1186 }
1187 if (!sm->PTK_valid) {
1188 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1189 "received EAPOL-Key msg SMK in "
1190 "invalid state - dropped");
1191 return;
1192 }
1193 break;
1194 #else /* CONFIG_PEERKEY */
1195 case SMK_M1:
1196 case SMK_M3:
1197 case SMK_ERROR:
1198 return; /* STSL disabled - ignore SMK messages */
1199 #endif /* CONFIG_PEERKEY */
1200 case REQUEST:
1201 break;
1202 }
1203
1204 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1205 "received EAPOL-Key frame (%s)", msgtxt);
1206
1207 if (key_info & WPA_KEY_INFO_ACK) {
1208 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1209 "received invalid EAPOL-Key: Key Ack set");
1210 return;
1211 }
1212
1213 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1214 !(key_info & WPA_KEY_INFO_MIC)) {
1215 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1216 "received invalid EAPOL-Key: Key MIC not set");
1217 return;
1218 }
1219
1220 #ifdef CONFIG_FILS
1221 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1222 (key_info & WPA_KEY_INFO_MIC)) {
1223 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1224 "received invalid EAPOL-Key: Key MIC set");
1225 return;
1226 }
1227 #endif /* CONFIG_FILS */
1228
1229 sm->MICVerified = FALSE;
1230 if (sm->PTK_valid && !sm->update_snonce) {
1231 if (mic_len &&
1232 wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
1233 data_len) &&
1234 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1235 wpa_try_alt_snonce(sm, data, data_len))) {
1236 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1237 "received EAPOL-Key with invalid MIC");
1238 return;
1239 }
1240 #ifdef CONFIG_FILS
1241 if (!mic_len &&
1242 wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
1243 &key_data_length) < 0) {
1244 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1245 "received EAPOL-Key with invalid MIC");
1246 return;
1247 }
1248 #endif /* CONFIG_FILS */
1249 sm->MICVerified = TRUE;
1250 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1251 sm->pending_1_of_4_timeout = 0;
1252 }
1253
1254 if (key_info & WPA_KEY_INFO_REQUEST) {
1255 if (sm->MICVerified) {
1256 sm->req_replay_counter_used = 1;
1257 os_memcpy(sm->req_replay_counter, key->replay_counter,
1258 WPA_REPLAY_COUNTER_LEN);
1259 } else {
1260 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1261 "received EAPOL-Key request with "
1262 "invalid MIC");
1263 return;
1264 }
1265
1266 /*
1267 * TODO: should decrypt key data field if encryption was used;
1268 * even though MAC address KDE is not normally encrypted,
1269 * supplicant is allowed to encrypt it.
1270 */
1271 if (msg == SMK_ERROR) {
1272 #ifdef CONFIG_PEERKEY
1273 wpa_smk_error(wpa_auth, sm, key_data, key_data_length);
1274 #endif /* CONFIG_PEERKEY */
1275 return;
1276 } else if (key_info & WPA_KEY_INFO_ERROR) {
1277 if (wpa_receive_error_report(
1278 wpa_auth, sm,
1279 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1280 return; /* STA entry was removed */
1281 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1282 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1283 "received EAPOL-Key Request for new "
1284 "4-Way Handshake");
1285 wpa_request_new_ptk(sm);
1286 #ifdef CONFIG_PEERKEY
1287 } else if (msg == SMK_M1) {
1288 wpa_smk_m1(wpa_auth, sm, key, key_data,
1289 key_data_length);
1290 #endif /* CONFIG_PEERKEY */
1291 } else if (key_data_length > 0 &&
1292 wpa_parse_kde_ies(key_data, key_data_length,
1293 &kde) == 0 &&
1294 kde.mac_addr) {
1295 } else {
1296 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1297 "received EAPOL-Key Request for GTK "
1298 "rekeying");
1299 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1300 wpa_rekey_gtk(wpa_auth, NULL);
1301 }
1302 } else {
1303 /* Do not allow the same key replay counter to be reused. */
1304 wpa_replay_counter_mark_invalid(sm->key_replay,
1305 key->replay_counter);
1306
1307 if (msg == PAIRWISE_2) {
1308 /*
1309 * Maintain a copy of the pending EAPOL-Key frames in
1310 * case the EAPOL-Key frame was retransmitted. This is
1311 * needed to allow EAPOL-Key msg 2/4 reply to another
1312 * pending msg 1/4 to update the SNonce to work around
1313 * unexpected supplicant behavior.
1314 */
1315 os_memcpy(sm->prev_key_replay, sm->key_replay,
1316 sizeof(sm->key_replay));
1317 } else {
1318 os_memset(sm->prev_key_replay, 0,
1319 sizeof(sm->prev_key_replay));
1320 }
1321
1322 /*
1323 * Make sure old valid counters are not accepted anymore and
1324 * do not get copied again.
1325 */
1326 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
1327 }
1328
1329 #ifdef CONFIG_PEERKEY
1330 if (msg == SMK_M3) {
1331 wpa_smk_m3(wpa_auth, sm, key, key_data, key_data_length);
1332 return;
1333 }
1334 #endif /* CONFIG_PEERKEY */
1335
1336 os_free(sm->last_rx_eapol_key);
1337 sm->last_rx_eapol_key = os_malloc(data_len);
1338 if (sm->last_rx_eapol_key == NULL)
1339 return;
1340 os_memcpy(sm->last_rx_eapol_key, data, data_len);
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 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1553 (key_data_len - 8) / 8, buf, key_data)) {
1554 os_free(hdr);
1555 os_free(buf);
1556 return;
1557 }
1558 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1559 #ifndef CONFIG_NO_RC4
1560 } else if (sm->PTK.kek_len == 16) {
1561 u8 ek[32];
1562 os_memcpy(key->key_iv,
1563 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1564 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1565 os_memcpy(ek, key->key_iv, 16);
1566 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1567 os_memcpy(key_data, buf, key_data_len);
1568 rc4_skip(ek, 32, 256, key_data, key_data_len);
1569 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1570 #endif /* CONFIG_NO_RC4 */
1571 } else {
1572 os_free(hdr);
1573 os_free(buf);
1574 return;
1575 }
1576 os_free(buf);
1577 }
1578
1579 if (key_info & WPA_KEY_INFO_MIC) {
1580 if (!sm->PTK_valid || !mic_len) {
1581 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1582 "PTK not valid when sending EAPOL-Key "
1583 "frame");
1584 os_free(hdr);
1585 return;
1586 }
1587
1588 wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1589 sm->wpa_key_mgmt, version,
1590 (u8 *) hdr, len, key_mic);
1591 #ifdef CONFIG_TESTING_OPTIONS
1592 if (!pairwise &&
1593 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
1594 drand48() <
1595 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1596 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1597 "Corrupting group EAPOL-Key Key MIC");
1598 key_mic[0]++;
1599 }
1600 #endif /* CONFIG_TESTING_OPTIONS */
1601 }
1602
1603 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1604 1);
1605 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1606 sm->pairwise_set);
1607 os_free(hdr);
1608 }
1609
1610
1611 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1612 struct wpa_state_machine *sm, int key_info,
1613 const u8 *key_rsc, const u8 *nonce,
1614 const u8 *kde, size_t kde_len,
1615 int keyidx, int encr)
1616 {
1617 int timeout_ms;
1618 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1619 u32 ctr;
1620
1621 if (sm == NULL)
1622 return;
1623
1624 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1625 keyidx, encr, 0);
1626
1627 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1628 if (ctr == 1 && wpa_auth->conf.tx_status)
1629 timeout_ms = pairwise ? eapol_key_timeout_first :
1630 eapol_key_timeout_first_group;
1631 else
1632 timeout_ms = eapol_key_timeout_subseq;
1633 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1634 sm->pending_1_of_4_timeout = 1;
1635 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1636 "counter %u)", timeout_ms, ctr);
1637 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1638 wpa_send_eapol_timeout, wpa_auth, sm);
1639 }
1640
1641
1642 static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
1643 size_t data_len)
1644 {
1645 struct ieee802_1x_hdr *hdr;
1646 struct wpa_eapol_key *key;
1647 u16 key_info;
1648 int ret = 0;
1649 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
1650 size_t mic_len = wpa_mic_len(akmp);
1651
1652 if (data_len < sizeof(*hdr) + sizeof(*key))
1653 return -1;
1654
1655 hdr = (struct ieee802_1x_hdr *) data;
1656 key = (struct wpa_eapol_key *) (hdr + 1);
1657 mic_pos = (u8 *) (key + 1);
1658 key_info = WPA_GET_BE16(key->key_info);
1659 os_memcpy(mic, mic_pos, mic_len);
1660 os_memset(mic_pos, 0, mic_len);
1661 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1662 key_info & WPA_KEY_INFO_TYPE_MASK,
1663 data, data_len, mic_pos) ||
1664 os_memcmp_const(mic, mic_pos, mic_len) != 0)
1665 ret = -1;
1666 os_memcpy(mic_pos, mic, mic_len);
1667 return ret;
1668 }
1669
1670
1671 void wpa_remove_ptk(struct wpa_state_machine *sm)
1672 {
1673 sm->PTK_valid = FALSE;
1674 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1675 if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
1676 0))
1677 wpa_printf(MSG_DEBUG,
1678 "RSN: PTK removal from the driver failed");
1679 sm->pairwise_set = FALSE;
1680 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1681 }
1682
1683
1684 int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
1685 {
1686 int remove_ptk = 1;
1687
1688 if (sm == NULL)
1689 return -1;
1690
1691 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1692 "event %d notification", event);
1693
1694 switch (event) {
1695 case WPA_AUTH:
1696 #ifdef CONFIG_MESH
1697 /* PTKs are derived through AMPE */
1698 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1699 /* not mesh */
1700 break;
1701 }
1702 return 0;
1703 #endif /* CONFIG_MESH */
1704 case WPA_ASSOC:
1705 break;
1706 case WPA_DEAUTH:
1707 case WPA_DISASSOC:
1708 sm->DeauthenticationRequest = TRUE;
1709 break;
1710 case WPA_REAUTH:
1711 case WPA_REAUTH_EAPOL:
1712 if (!sm->started) {
1713 /*
1714 * When using WPS, we may end up here if the STA
1715 * manages to re-associate without the previous STA
1716 * entry getting removed. Consequently, we need to make
1717 * sure that the WPA state machines gets initialized
1718 * properly at this point.
1719 */
1720 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1721 "started - initialize now");
1722 sm->started = 1;
1723 sm->Init = TRUE;
1724 if (wpa_sm_step(sm) == 1)
1725 return 1; /* should not really happen */
1726 sm->Init = FALSE;
1727 sm->AuthenticationRequest = TRUE;
1728 break;
1729 }
1730 if (sm->GUpdateStationKeys) {
1731 /*
1732 * Reauthentication cancels the pending group key
1733 * update for this STA.
1734 */
1735 sm->group->GKeyDoneStations--;
1736 sm->GUpdateStationKeys = FALSE;
1737 sm->PtkGroupInit = TRUE;
1738 }
1739 sm->ReAuthenticationRequest = TRUE;
1740 break;
1741 case WPA_ASSOC_FT:
1742 #ifdef CONFIG_IEEE80211R_AP
1743 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1744 "after association");
1745 wpa_ft_install_ptk(sm);
1746
1747 /* Using FT protocol, not WPA auth state machine */
1748 sm->ft_completed = 1;
1749 return 0;
1750 #else /* CONFIG_IEEE80211R_AP */
1751 break;
1752 #endif /* CONFIG_IEEE80211R_AP */
1753 }
1754
1755 #ifdef CONFIG_IEEE80211R_AP
1756 sm->ft_completed = 0;
1757 #endif /* CONFIG_IEEE80211R_AP */
1758
1759 #ifdef CONFIG_IEEE80211W
1760 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1761 remove_ptk = 0;
1762 #endif /* CONFIG_IEEE80211W */
1763 #ifdef CONFIG_FILS
1764 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1765 (event == WPA_AUTH || event == WPA_ASSOC))
1766 remove_ptk = 0;
1767 #endif /* CONFIG_FILS */
1768
1769 if (remove_ptk) {
1770 sm->PTK_valid = FALSE;
1771 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1772
1773 if (event != WPA_REAUTH_EAPOL)
1774 wpa_remove_ptk(sm);
1775 }
1776
1777 if (sm->in_step_loop) {
1778 /*
1779 * wpa_sm_step() is already running - avoid recursive call to
1780 * it by making the existing loop process the new update.
1781 */
1782 sm->changed = TRUE;
1783 return 0;
1784 }
1785 return wpa_sm_step(sm);
1786 }
1787
1788
1789 SM_STATE(WPA_PTK, INITIALIZE)
1790 {
1791 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1792 if (sm->Init) {
1793 /* Init flag is not cleared here, so avoid busy
1794 * loop by claiming nothing changed. */
1795 sm->changed = FALSE;
1796 }
1797
1798 sm->keycount = 0;
1799 if (sm->GUpdateStationKeys)
1800 sm->group->GKeyDoneStations--;
1801 sm->GUpdateStationKeys = FALSE;
1802 if (sm->wpa == WPA_VERSION_WPA)
1803 sm->PInitAKeys = FALSE;
1804 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1805 * Local AA > Remote AA)) */) {
1806 sm->Pair = TRUE;
1807 }
1808 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1809 wpa_remove_ptk(sm);
1810 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1811 sm->TimeoutCtr = 0;
1812 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1813 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1814 WPA_EAPOL_authorized, 0);
1815 }
1816 }
1817
1818
1819 SM_STATE(WPA_PTK, DISCONNECT)
1820 {
1821 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1822 sm->Disconnect = FALSE;
1823 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1824 }
1825
1826
1827 SM_STATE(WPA_PTK, DISCONNECTED)
1828 {
1829 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1830 sm->DeauthenticationRequest = FALSE;
1831 }
1832
1833
1834 SM_STATE(WPA_PTK, AUTHENTICATION)
1835 {
1836 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1837 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1838 sm->PTK_valid = FALSE;
1839 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1840 1);
1841 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1842 sm->AuthenticationRequest = FALSE;
1843 }
1844
1845
1846 static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1847 struct wpa_group *group)
1848 {
1849 if (group->first_sta_seen)
1850 return;
1851 /*
1852 * System has run bit further than at the time hostapd was started
1853 * potentially very early during boot up. This provides better chances
1854 * of collecting more randomness on embedded systems. Re-initialize the
1855 * GMK and Counter here to improve their strength if there was not
1856 * enough entropy available immediately after system startup.
1857 */
1858 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1859 "station");
1860 if (random_pool_ready() != 1) {
1861 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1862 "to proceed - reject first 4-way handshake");
1863 group->reject_4way_hs_for_entropy = TRUE;
1864 } else {
1865 group->first_sta_seen = TRUE;
1866 group->reject_4way_hs_for_entropy = FALSE;
1867 }
1868
1869 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
1870 wpa_gtk_update(wpa_auth, group) < 0 ||
1871 wpa_group_config_group_keys(wpa_auth, group) < 0) {
1872 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
1873 group->first_sta_seen = FALSE;
1874 group->reject_4way_hs_for_entropy = TRUE;
1875 }
1876 }
1877
1878
1879 SM_STATE(WPA_PTK, AUTHENTICATION2)
1880 {
1881 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1882
1883 wpa_group_ensure_init(sm->wpa_auth, sm->group);
1884 sm->ReAuthenticationRequest = FALSE;
1885
1886 /*
1887 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1888 * ambiguous. The Authenticator state machine uses a counter that is
1889 * incremented by one for each 4-way handshake. However, the security
1890 * analysis of 4-way handshake points out that unpredictable nonces
1891 * help in preventing precomputation attacks. Instead of the state
1892 * machine definition, use an unpredictable nonce value here to provide
1893 * stronger protection against potential precomputation attacks.
1894 */
1895 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1896 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1897 "ANonce.");
1898 sm->Disconnect = TRUE;
1899 return;
1900 }
1901 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1902 WPA_NONCE_LEN);
1903 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1904 * logical place than INITIALIZE since AUTHENTICATION2 can be
1905 * re-entered on ReAuthenticationRequest without going through
1906 * INITIALIZE. */
1907 sm->TimeoutCtr = 0;
1908 }
1909
1910
1911 SM_STATE(WPA_PTK, INITPMK)
1912 {
1913 u8 msk[2 * PMK_LEN];
1914 size_t len = 2 * PMK_LEN;
1915
1916 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1917 #ifdef CONFIG_IEEE80211R_AP
1918 sm->xxkey_len = 0;
1919 #endif /* CONFIG_IEEE80211R_AP */
1920 if (sm->pmksa) {
1921 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1922 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
1923 sm->pmk_len = sm->pmksa->pmk_len;
1924 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1925 unsigned int pmk_len;
1926
1927 if (sm->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
1928 pmk_len = PMK_LEN_SUITE_B_192;
1929 else
1930 pmk_len = PMK_LEN;
1931 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1932 "(MSK len=%lu PMK len=%u)", (unsigned long) len,
1933 pmk_len);
1934 if (len < pmk_len) {
1935 wpa_printf(MSG_DEBUG,
1936 "WPA: MSK not long enough (%u) to create PMK (%u)",
1937 (unsigned int) len, (unsigned int) pmk_len);
1938 sm->Disconnect = TRUE;
1939 return;
1940 }
1941 os_memcpy(sm->PMK, msk, pmk_len);
1942 sm->pmk_len = pmk_len;
1943 #ifdef CONFIG_IEEE80211R_AP
1944 if (len >= 2 * PMK_LEN) {
1945 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1946 sm->xxkey_len = PMK_LEN;
1947 }
1948 #endif /* CONFIG_IEEE80211R_AP */
1949 } else {
1950 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
1951 sm->wpa_auth->cb->get_msk);
1952 sm->Disconnect = TRUE;
1953 return;
1954 }
1955 os_memset(msk, 0, sizeof(msk));
1956
1957 sm->req_replay_counter_used = 0;
1958 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1959 * will break reauthentication since EAPOL state machines may not be
1960 * get into AUTHENTICATING state that clears keyRun before WPA state
1961 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1962 * state and takes PMK from the previously used AAA Key. This will
1963 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1964 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1965 * be good workaround for this issue. */
1966 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1967 }
1968
1969
1970 SM_STATE(WPA_PTK, INITPSK)
1971 {
1972 const u8 *psk;
1973 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1974 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
1975 if (psk) {
1976 os_memcpy(sm->PMK, psk, PMK_LEN);
1977 sm->pmk_len = PMK_LEN;
1978 #ifdef CONFIG_IEEE80211R_AP
1979 os_memcpy(sm->xxkey, psk, PMK_LEN);
1980 sm->xxkey_len = PMK_LEN;
1981 #endif /* CONFIG_IEEE80211R_AP */
1982 }
1983 sm->req_replay_counter_used = 0;
1984 }
1985
1986
1987 SM_STATE(WPA_PTK, PTKSTART)
1988 {
1989 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1990 size_t pmkid_len = 0;
1991
1992 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1993 sm->PTKRequest = FALSE;
1994 sm->TimeoutEvt = FALSE;
1995 sm->alt_snonce_valid = FALSE;
1996
1997 sm->TimeoutCtr++;
1998 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
1999 /* No point in sending the EAPOL-Key - we will disconnect
2000 * immediately following this. */
2001 return;
2002 }
2003
2004 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2005 "sending 1/4 msg of 4-Way Handshake");
2006 /*
2007 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
2008 * one possible PSK for this STA.
2009 */
2010 if (sm->wpa == WPA_VERSION_WPA2 &&
2011 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2012 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
2013 pmkid = buf;
2014 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2015 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
2016 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2017 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
2018 if (sm->pmksa) {
2019 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2020 sm->pmksa->pmkid, PMKID_LEN);
2021 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
2022 /* No KCK available to derive PMKID */
2023 pmkid = NULL;
2024 } else {
2025 /*
2026 * Calculate PMKID since no PMKSA cache entry was
2027 * available with pre-calculated PMKID.
2028 */
2029 rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
2030 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
2031 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
2032 }
2033 }
2034 wpa_send_eapol(sm->wpa_auth, sm,
2035 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
2036 sm->ANonce, pmkid, pmkid_len, 0, 0);
2037 }
2038
2039
2040 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
2041 const u8 *pmk, unsigned int pmk_len,
2042 struct wpa_ptk *ptk)
2043 {
2044 #ifdef CONFIG_IEEE80211R_AP
2045 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
2046 return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
2047 #endif /* CONFIG_IEEE80211R_AP */
2048
2049 return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
2050 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
2051 ptk, sm->wpa_key_mgmt, sm->pairwise);
2052 }
2053
2054
2055 #ifdef CONFIG_FILS
2056
2057 int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
2058 size_t pmk_len, const u8 *snonce, const u8 *anonce)
2059 {
2060 u8 ick[FILS_ICK_MAX_LEN];
2061 size_t ick_len;
2062 int res;
2063
2064 res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr,
2065 snonce, anonce, &sm->PTK, ick, &ick_len,
2066 sm->wpa_key_mgmt, sm->pairwise);
2067 if (res < 0)
2068 return res;
2069 sm->PTK_valid = TRUE;
2070
2071 res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
2072 sm->addr, sm->wpa_auth->addr,
2073 NULL, 0, NULL, 0, /* TODO: SK+PFS */
2074 sm->wpa_key_mgmt, sm->fils_key_auth_sta,
2075 sm->fils_key_auth_ap,
2076 &sm->fils_key_auth_len);
2077 os_memset(ick, 0, sizeof(ick));
2078
2079 /* Store nonces for (Re)Association Request/Response frame processing */
2080 os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
2081 os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
2082
2083 return res;
2084 }
2085
2086
2087 static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2088 u8 *buf, size_t buf_len, u16 *_key_data_len)
2089 {
2090 struct ieee802_1x_hdr *hdr;
2091 struct wpa_eapol_key *key;
2092 u8 *pos;
2093 u16 key_data_len;
2094 u8 *tmp;
2095 const u8 *aad[1];
2096 size_t aad_len[1];
2097
2098 hdr = (struct ieee802_1x_hdr *) buf;
2099 key = (struct wpa_eapol_key *) (hdr + 1);
2100 pos = (u8 *) (key + 1);
2101 key_data_len = WPA_GET_BE16(pos);
2102 if (key_data_len < AES_BLOCK_SIZE ||
2103 key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
2104 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2105 "No room for AES-SIV data in the frame");
2106 return -1;
2107 }
2108 pos += 2; /* Pointing at the Encrypted Key Data field */
2109
2110 tmp = os_malloc(key_data_len);
2111 if (!tmp)
2112 return -1;
2113
2114 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2115 * to Key Data (exclusive). */
2116 aad[0] = buf;
2117 aad_len[0] = pos - buf;
2118 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
2119 1, aad, aad_len, tmp) < 0) {
2120 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2121 "Invalid AES-SIV data in the frame");
2122 bin_clear_free(tmp, key_data_len);
2123 return -1;
2124 }
2125
2126 /* AEAD decryption and validation completed successfully */
2127 key_data_len -= AES_BLOCK_SIZE;
2128 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2129 tmp, key_data_len);
2130
2131 /* Replace Key Data field with the decrypted version */
2132 os_memcpy(pos, tmp, key_data_len);
2133 pos -= 2; /* Key Data Length field */
2134 WPA_PUT_BE16(pos, key_data_len);
2135 bin_clear_free(tmp, key_data_len);
2136 if (_key_data_len)
2137 *_key_data_len = key_data_len;
2138 return 0;
2139 }
2140
2141
2142 int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
2143 const struct ieee80211_mgmt *mgmt, size_t frame_len,
2144 u8 *pos, size_t left)
2145 {
2146 u16 fc, stype;
2147 const u8 *end, *ie_start, *ie, *session, *crypt;
2148 struct ieee802_11_elems elems;
2149 const u8 *aad[5];
2150 size_t aad_len[5];
2151
2152 if (!sm || !sm->PTK_valid) {
2153 wpa_printf(MSG_DEBUG,
2154 "FILS: No KEK to decrypt Assocication Request frame");
2155 return -1;
2156 }
2157
2158 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2159 wpa_printf(MSG_DEBUG,
2160 "FILS: Not a FILS AKM - reject association");
2161 return -1;
2162 }
2163
2164 end = ((const u8 *) mgmt) + frame_len;
2165 fc = le_to_host16(mgmt->frame_control);
2166 stype = WLAN_FC_GET_STYPE(fc);
2167 if (stype == WLAN_FC_STYPE_REASSOC_REQ)
2168 ie_start = mgmt->u.reassoc_req.variable;
2169 else
2170 ie_start = mgmt->u.assoc_req.variable;
2171 ie = ie_start;
2172
2173 /*
2174 * Find FILS Session element which is the last unencrypted element in
2175 * the frame.
2176 */
2177 session = NULL;
2178 while (ie + 1 < end) {
2179 if (ie + 2 + ie[1] > end)
2180 break;
2181 if (ie[0] == WLAN_EID_EXTENSION &&
2182 ie[1] >= 1 + FILS_SESSION_LEN &&
2183 ie[2] == WLAN_EID_EXT_FILS_SESSION) {
2184 session = ie;
2185 break;
2186 }
2187 ie += 2 + ie[1];
2188 }
2189
2190 if (!session) {
2191 wpa_printf(MSG_DEBUG,
2192 "FILS: Could not find FILS Session element in Association Request frame - reject");
2193 return -1;
2194 }
2195 if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
2196 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
2197 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
2198 fils_session, FILS_SESSION_LEN);
2199 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
2200 session + 3, FILS_SESSION_LEN);
2201 return -1;
2202 }
2203 crypt = session + 2 + session[1];
2204
2205 if (end - crypt < AES_BLOCK_SIZE) {
2206 wpa_printf(MSG_DEBUG,
2207 "FILS: Too short frame to include AES-SIV data");
2208 return -1;
2209 }
2210
2211 /* AES-SIV AAD vectors */
2212
2213 /* The STA's MAC address */
2214 aad[0] = mgmt->sa;
2215 aad_len[0] = ETH_ALEN;
2216 /* The AP's BSSID */
2217 aad[1] = mgmt->da;
2218 aad_len[1] = ETH_ALEN;
2219 /* The STA's nonce */
2220 aad[2] = sm->SNonce;
2221 aad_len[2] = FILS_NONCE_LEN;
2222 /* The AP's nonce */
2223 aad[3] = sm->ANonce;
2224 aad_len[3] = FILS_NONCE_LEN;
2225 /*
2226 * The (Re)Association Request frame from the Capability Information
2227 * field to the FILS Session element (both inclusive).
2228 */
2229 aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
2230 aad_len[4] = crypt - aad[4];
2231
2232 if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
2233 5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
2234 wpa_printf(MSG_DEBUG,
2235 "FILS: Invalid AES-SIV data in the frame");
2236 return -1;
2237 }
2238 wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
2239 pos, left - AES_BLOCK_SIZE);
2240
2241 if (ieee802_11_parse_elems(pos, left - AES_BLOCK_SIZE, &elems, 1) ==
2242 ParseFailed) {
2243 wpa_printf(MSG_DEBUG,
2244 "FILS: Failed to parse decrypted elements");
2245 return -1;
2246 }
2247 if (!elems.fils_key_confirm) {
2248 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
2249 return -1;
2250 }
2251 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
2252 wpa_printf(MSG_DEBUG,
2253 "FILS: Unexpected Key-Auth length %d (expected %d)",
2254 elems.fils_key_confirm_len,
2255 (int) sm->fils_key_auth_len);
2256 return -1;
2257 }
2258 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
2259 sm->fils_key_auth_len) != 0) {
2260 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
2261 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
2262 elems.fils_key_confirm,
2263 elems.fils_key_confirm_len);
2264 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
2265 sm->fils_key_auth_sta, sm->fils_key_auth_len);
2266 return -1;
2267 }
2268
2269 return left - AES_BLOCK_SIZE;
2270 }
2271
2272
2273 int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
2274 size_t current_len, size_t max_len,
2275 const struct wpabuf *hlp)
2276 {
2277 u8 *end = buf + max_len;
2278 u8 *pos = buf + current_len;
2279 struct ieee80211_mgmt *mgmt;
2280 struct wpabuf *plain;
2281 u8 *len, *tmp, *tmp2;
2282 u8 hdr[2];
2283 u8 *gtk, dummy_gtk[32];
2284 size_t gtk_len;
2285 struct wpa_group *gsm;
2286 const u8 *aad[5];
2287 size_t aad_len[5];
2288
2289 if (!sm || !sm->PTK_valid)
2290 return -1;
2291
2292 wpa_hexdump(MSG_DEBUG,
2293 "FILS: Association Response frame before FILS processing",
2294 buf, current_len);
2295
2296 mgmt = (struct ieee80211_mgmt *) buf;
2297
2298 /* AES-SIV AAD vectors */
2299
2300 /* The AP's BSSID */
2301 aad[0] = mgmt->sa;
2302 aad_len[0] = ETH_ALEN;
2303 /* The STA's MAC address */
2304 aad[1] = mgmt->da;
2305 aad_len[1] = ETH_ALEN;
2306 /* The AP's nonce */
2307 aad[2] = sm->ANonce;
2308 aad_len[2] = FILS_NONCE_LEN;
2309 /* The STA's nonce */
2310 aad[3] = sm->SNonce;
2311 aad_len[3] = FILS_NONCE_LEN;
2312 /*
2313 * The (Re)Association Response frame from the Capability Information
2314 * field (the same offset in both Association and Reassociation
2315 * Response frames) to the FILS Session element (both inclusive).
2316 */
2317 aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
2318 aad_len[4] = pos - aad[4];
2319
2320 /* The following elements will be encrypted with AES-SIV */
2321
2322 plain = wpabuf_alloc(1000);
2323 if (!plain)
2324 return -1;
2325
2326 /* TODO: FILS Public Key */
2327
2328 /* FILS Key Confirmation */
2329 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2330 wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
2331 /* Element ID Extension */
2332 wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
2333 wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
2334
2335 /* FILS HLP Container */
2336 if (hlp)
2337 wpabuf_put_buf(plain, hlp);
2338
2339 /* TODO: FILS IP Address Assignment */
2340
2341 /* Key Delivery */
2342 gsm = sm->group;
2343 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2344 len = wpabuf_put(plain, 1);
2345 wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
2346 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
2347 wpabuf_put(plain, WPA_KEY_RSC_LEN));
2348 /* GTK KDE */
2349 gtk = gsm->GTK[gsm->GN - 1];
2350 gtk_len = gsm->GTK_len;
2351 if (sm->wpa_auth->conf.disable_gtk) {
2352 /*
2353 * Provide unique random GTK to each STA to prevent use
2354 * of GTK in the BSS.
2355 */
2356 if (random_get_bytes(dummy_gtk, gtk_len) < 0) {
2357 wpabuf_free(plain);
2358 return -1;
2359 }
2360 gtk = dummy_gtk;
2361 }
2362 hdr[0] = gsm->GN & 0x03;
2363 hdr[1] = 0;
2364 tmp = wpabuf_put(plain, 0);
2365 tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2366 gtk, gtk_len);
2367 wpabuf_put(plain, tmp2 - tmp);
2368
2369 /* IGTK KDE */
2370 tmp = wpabuf_put(plain, 0);
2371 tmp2 = ieee80211w_kde_add(sm, tmp);
2372 wpabuf_put(plain, tmp2 - tmp);
2373
2374 *len = (u8 *) wpabuf_put(plain, 0) - len - 1;
2375
2376 if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
2377 wpa_printf(MSG_DEBUG,
2378 "FILS: Not enough room for FILS elements");
2379 wpabuf_free(plain);
2380 return -1;
2381 }
2382
2383 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
2384 plain);
2385
2386 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
2387 wpabuf_head(plain), wpabuf_len(plain),
2388 5, aad, aad_len, pos) < 0) {
2389 wpabuf_free(plain);
2390 return -1;
2391 }
2392
2393 wpa_hexdump(MSG_DEBUG,
2394 "FILS: Encrypted Association Response elements",
2395 pos, AES_BLOCK_SIZE + wpabuf_len(plain));
2396 current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
2397 wpabuf_free(plain);
2398
2399 sm->fils_completed = 1;
2400
2401 return current_len;
2402 }
2403
2404
2405 int fils_set_tk(struct wpa_state_machine *sm)
2406 {
2407 enum wpa_alg alg;
2408 int klen;
2409
2410 if (!sm || !sm->PTK_valid)
2411 return -1;
2412
2413 alg = wpa_cipher_to_alg(sm->pairwise);
2414 klen = wpa_cipher_key_len(sm->pairwise);
2415
2416 wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
2417 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2418 sm->PTK.tk, klen)) {
2419 wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
2420 return -1;
2421 }
2422
2423 return 0;
2424 }
2425
2426 #endif /* CONFIG_FILS */
2427
2428
2429 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2430 {
2431 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2432 struct wpa_ptk PTK;
2433 int ok = 0, psk_found = 0;
2434 const u8 *pmk = NULL;
2435 unsigned int pmk_len;
2436 int ft;
2437 const u8 *eapol_key_ie, *key_data, *mic;
2438 u16 key_data_length;
2439 size_t mic_len, eapol_key_ie_len;
2440 struct ieee802_1x_hdr *hdr;
2441 struct wpa_eapol_key *key;
2442 struct wpa_eapol_ie_parse kde;
2443
2444 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2445 sm->EAPOLKeyReceived = FALSE;
2446 sm->update_snonce = FALSE;
2447 os_memset(&PTK, 0, sizeof(PTK));
2448
2449 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
2450
2451 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2452 * WPA-PSK: iterate through possible PSKs and select the one matching
2453 * the packet */
2454 for (;;) {
2455 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2456 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
2457 sm->p2p_dev_addr, pmk);
2458 if (pmk == NULL)
2459 break;
2460 psk_found = 1;
2461 pmk_len = PMK_LEN;
2462 } else {
2463 pmk = sm->PMK;
2464 pmk_len = sm->pmk_len;
2465 }
2466
2467 wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK);
2468
2469 if (mic_len &&
2470 wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
2471 sm->last_rx_eapol_key,
2472 sm->last_rx_eapol_key_len) == 0) {
2473 ok = 1;
2474 break;
2475 }
2476
2477 #ifdef CONFIG_FILS
2478 if (!mic_len &&
2479 wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
2480 sm->last_rx_eapol_key_len, NULL) == 0) {
2481 ok = 1;
2482 break;
2483 }
2484 #endif /* CONFIG_FILS */
2485
2486 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
2487 break;
2488 }
2489
2490 if (!ok) {
2491 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2492 "invalid MIC in msg 2/4 of 4-Way Handshake");
2493 if (psk_found)
2494 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
2495 return;
2496 }
2497
2498 /*
2499 * Note: last_rx_eapol_key length fields have already been validated in
2500 * wpa_receive().
2501 */
2502 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
2503 key = (struct wpa_eapol_key *) (hdr + 1);
2504 mic = (u8 *) (key + 1);
2505 key_data = mic + mic_len + 2;
2506 key_data_length = WPA_GET_BE16(mic + mic_len);
2507 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
2508 sizeof(*key) - mic_len - 2)
2509 return;
2510
2511 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
2512 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
2513 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2514 return;
2515 }
2516 if (kde.rsn_ie) {
2517 eapol_key_ie = kde.rsn_ie;
2518 eapol_key_ie_len = kde.rsn_ie_len;
2519 } else if (kde.osen) {
2520 eapol_key_ie = kde.osen;
2521 eapol_key_ie_len = kde.osen_len;
2522 } else {
2523 eapol_key_ie = kde.wpa_ie;
2524 eapol_key_ie_len = kde.wpa_ie_len;
2525 }
2526 ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
2527 if (sm->wpa_ie == NULL ||
2528 wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
2529 eapol_key_ie, eapol_key_ie_len)) {
2530 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2531 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2532 if (sm->wpa_ie) {
2533 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
2534 sm->wpa_ie, sm->wpa_ie_len);
2535 }
2536 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
2537 eapol_key_ie, eapol_key_ie_len);
2538 /* MLME-DEAUTHENTICATE.request */
2539 wpa_sta_disconnect(wpa_auth, sm->addr);
2540 return;
2541 }
2542 #ifdef CONFIG_IEEE80211R_AP
2543 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
2544 wpa_sta_disconnect(wpa_auth, sm->addr);
2545 return;
2546 }
2547 #endif /* CONFIG_IEEE80211R_AP */
2548 #ifdef CONFIG_P2P
2549 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
2550 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
2551 int idx;
2552 wpa_printf(MSG_DEBUG,
2553 "P2P: IP address requested in EAPOL-Key exchange");
2554 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
2555 if (idx >= 0) {
2556 u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
2557 bitfield_set(wpa_auth->ip_pool, idx);
2558 WPA_PUT_BE32(sm->ip_addr, start + idx);
2559 wpa_printf(MSG_DEBUG,
2560 "P2P: Assigned IP address %u.%u.%u.%u to "
2561 MACSTR, sm->ip_addr[0], sm->ip_addr[1],
2562 sm->ip_addr[2], sm->ip_addr[3],
2563 MAC2STR(sm->addr));
2564 }
2565 }
2566 #endif /* CONFIG_P2P */
2567
2568 #ifdef CONFIG_IEEE80211R_AP
2569 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2570 /*
2571 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2572 * with the value we derived.
2573 */
2574 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
2575 WPA_PMK_NAME_LEN) != 0) {
2576 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2577 "PMKR1Name mismatch in FT 4-way "
2578 "handshake");
2579 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
2580 "Supplicant",
2581 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
2582 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2583 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2584 return;
2585 }
2586 }
2587 #endif /* CONFIG_IEEE80211R_AP */
2588
2589 sm->pending_1_of_4_timeout = 0;
2590 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
2591
2592 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2593 /* PSK may have changed from the previous choice, so update
2594 * state machine data based on whatever PSK was selected here.
2595 */
2596 os_memcpy(sm->PMK, pmk, PMK_LEN);
2597 sm->pmk_len = PMK_LEN;
2598 }
2599
2600 sm->MICVerified = TRUE;
2601
2602 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
2603 sm->PTK_valid = TRUE;
2604 }
2605
2606
2607 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2608 {
2609 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2610 sm->TimeoutCtr = 0;
2611 }
2612
2613
2614 #ifdef CONFIG_IEEE80211W
2615
2616 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2617 {
2618 if (sm->mgmt_frame_prot) {
2619 size_t len;
2620 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2621 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
2622 }
2623
2624 return 0;
2625 }
2626
2627
2628 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2629 {
2630 struct wpa_igtk_kde igtk;
2631 struct wpa_group *gsm = sm->group;
2632 u8 rsc[WPA_KEY_RSC_LEN];
2633 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2634
2635 if (!sm->mgmt_frame_prot)
2636 return pos;
2637
2638 igtk.keyid[0] = gsm->GN_igtk;
2639 igtk.keyid[1] = 0;
2640 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
2641 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
2642 os_memset(igtk.pn, 0, sizeof(igtk.pn));
2643 else
2644 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
2645 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
2646 if (sm->wpa_auth->conf.disable_gtk) {
2647 /*
2648 * Provide unique random IGTK to each STA to prevent use of
2649 * IGTK in the BSS.
2650 */
2651 if (random_get_bytes(igtk.igtk, len) < 0)
2652 return pos;
2653 }
2654 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
2655 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
2656 NULL, 0);
2657
2658 return pos;
2659 }
2660
2661 #else /* CONFIG_IEEE80211W */
2662
2663 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2664 {
2665 return 0;
2666 }
2667
2668
2669 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2670 {
2671 return pos;
2672 }
2673
2674 #endif /* CONFIG_IEEE80211W */
2675
2676
2677 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2678 {
2679 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
2680 size_t gtk_len, kde_len;
2681 struct wpa_group *gsm = sm->group;
2682 u8 *wpa_ie;
2683 int wpa_ie_len, secure, keyidx, encr = 0;
2684
2685 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2686 sm->TimeoutEvt = FALSE;
2687
2688 sm->TimeoutCtr++;
2689 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
2690 /* No point in sending the EAPOL-Key - we will disconnect
2691 * immediately following this. */
2692 return;
2693 }
2694
2695 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2696 GTK[GN], IGTK, [FTIE], [TIE * 2])
2697 */
2698 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2699 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2700 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2701 wpa_ie = sm->wpa_auth->wpa_ie;
2702 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
2703 if (sm->wpa == WPA_VERSION_WPA &&
2704 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
2705 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
2706 /* WPA-only STA, remove RSN IE and possible MDIE */
2707 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2708 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
2709 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2710 wpa_ie_len = wpa_ie[1] + 2;
2711 }
2712 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2713 "sending 3/4 msg of 4-Way Handshake");
2714 if (sm->wpa == WPA_VERSION_WPA2) {
2715 /* WPA2 send GTK in the 4-way handshake */
2716 secure = 1;
2717 gtk = gsm->GTK[gsm->GN - 1];
2718 gtk_len = gsm->GTK_len;
2719 if (sm->wpa_auth->conf.disable_gtk) {
2720 /*
2721 * Provide unique random GTK to each STA to prevent use
2722 * of GTK in the BSS.
2723 */
2724 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
2725 return;
2726 gtk = dummy_gtk;
2727 }
2728 keyidx = gsm->GN;
2729 _rsc = rsc;
2730 encr = 1;
2731 } else {
2732 /* WPA does not include GTK in msg 3/4 */
2733 secure = 0;
2734 gtk = NULL;
2735 gtk_len = 0;
2736 keyidx = 0;
2737 _rsc = NULL;
2738 if (sm->rx_eapol_key_secure) {
2739 /*
2740 * It looks like Windows 7 supplicant tries to use
2741 * Secure bit in msg 2/4 after having reported Michael
2742 * MIC failure and it then rejects the 4-way handshake
2743 * if msg 3/4 does not set Secure bit. Work around this
2744 * by setting the Secure bit here even in the case of
2745 * WPA if the supplicant used it first.
2746 */
2747 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2748 "STA used Secure bit in WPA msg 2/4 - "
2749 "set Secure for 3/4 as workaround");
2750 secure = 1;
2751 }
2752 }
2753
2754 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
2755 if (gtk)
2756 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
2757 #ifdef CONFIG_IEEE80211R_AP
2758 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2759 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
2760 kde_len += 300; /* FTIE + 2 * TIE */
2761 }
2762 #endif /* CONFIG_IEEE80211R_AP */
2763 #ifdef CONFIG_P2P
2764 if (WPA_GET_BE32(sm->ip_addr) > 0)
2765 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
2766 #endif /* CONFIG_P2P */
2767 kde = os_malloc(kde_len);
2768 if (kde == NULL)
2769 return;
2770
2771 pos = kde;
2772 os_memcpy(pos, wpa_ie, wpa_ie_len);
2773 pos += wpa_ie_len;
2774 #ifdef CONFIG_IEEE80211R_AP
2775 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2776 int res;
2777 size_t elen;
2778
2779 elen = pos - kde;
2780 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
2781 if (res < 0) {
2782 wpa_printf(MSG_ERROR, "FT: Failed to insert "
2783 "PMKR1Name into RSN IE in EAPOL-Key data");
2784 os_free(kde);
2785 return;
2786 }
2787 pos -= wpa_ie_len;
2788 pos += elen;
2789 }
2790 #endif /* CONFIG_IEEE80211R_AP */
2791 if (gtk) {
2792 u8 hdr[2];
2793 hdr[0] = keyidx & 0x03;
2794 hdr[1] = 0;
2795 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2796 gtk, gtk_len);
2797 }
2798 pos = ieee80211w_kde_add(sm, pos);
2799
2800 #ifdef CONFIG_IEEE80211R_AP
2801 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2802 int res;
2803 struct wpa_auth_config *conf;
2804
2805 conf = &sm->wpa_auth->conf;
2806 if (sm->assoc_resp_ftie &&
2807 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
2808 os_memcpy(pos, sm->assoc_resp_ftie,
2809 2 + sm->assoc_resp_ftie[1]);
2810 res = 2 + sm->assoc_resp_ftie[1];
2811 } else {
2812 res = wpa_write_ftie(conf, conf->r0_key_holder,
2813 conf->r0_key_holder_len,
2814 NULL, NULL, pos,
2815 kde + kde_len - pos,
2816 NULL, 0);
2817 }
2818 if (res < 0) {
2819 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2820 "into EAPOL-Key Key Data");
2821 os_free(kde);
2822 return;
2823 }
2824 pos += res;
2825
2826 /* TIE[ReassociationDeadline] (TU) */
2827 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2828 *pos++ = 5;
2829 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2830 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2831 pos += 4;
2832
2833 /* TIE[KeyLifetime] (seconds) */
2834 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2835 *pos++ = 5;
2836 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2837 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2838 pos += 4;
2839 }
2840 #endif /* CONFIG_IEEE80211R_AP */
2841 #ifdef CONFIG_P2P
2842 if (WPA_GET_BE32(sm->ip_addr) > 0) {
2843 u8 addr[3 * 4];
2844 os_memcpy(addr, sm->ip_addr, 4);
2845 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
2846 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
2847 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
2848 addr, sizeof(addr), NULL, 0);
2849 }
2850 #endif /* CONFIG_P2P */
2851
2852 wpa_send_eapol(sm->wpa_auth, sm,
2853 (secure ? WPA_KEY_INFO_SECURE : 0) |
2854 (wpa_mic_len(sm->wpa_key_mgmt) ? WPA_KEY_INFO_MIC : 0) |
2855 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2856 WPA_KEY_INFO_KEY_TYPE,
2857 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2858 os_free(kde);
2859 }
2860
2861
2862 SM_STATE(WPA_PTK, PTKINITDONE)
2863 {
2864 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2865 sm->EAPOLKeyReceived = FALSE;
2866 if (sm->Pair) {
2867 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2868 int klen = wpa_cipher_key_len(sm->pairwise);
2869 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2870 sm->PTK.tk, klen)) {
2871 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2872 return;
2873 }
2874 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2875 sm->pairwise_set = TRUE;
2876
2877 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2878 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2879 eloop_register_timeout(sm->wpa_auth->conf.
2880 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2881 sm->wpa_auth, sm);
2882 }
2883
2884 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2885 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2886 WPA_EAPOL_authorized, 1);
2887 }
2888 }
2889
2890 if (0 /* IBSS == TRUE */) {
2891 sm->keycount++;
2892 if (sm->keycount == 2) {
2893 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2894 WPA_EAPOL_portValid, 1);
2895 }
2896 } else {
2897 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2898 1);
2899 }
2900 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2901 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2902 if (sm->wpa == WPA_VERSION_WPA)
2903 sm->PInitAKeys = TRUE;
2904 else
2905 sm->has_GTK = TRUE;
2906 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2907 "pairwise key handshake completed (%s)",
2908 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2909
2910 #ifdef CONFIG_IEEE80211R_AP
2911 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2912 #endif /* CONFIG_IEEE80211R_AP */
2913 }
2914
2915
2916 SM_STEP(WPA_PTK)
2917 {
2918 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2919
2920 if (sm->Init)
2921 SM_ENTER(WPA_PTK, INITIALIZE);
2922 else if (sm->Disconnect
2923 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2924 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2925 "WPA_PTK: sm->Disconnect");
2926 SM_ENTER(WPA_PTK, DISCONNECT);
2927 }
2928 else if (sm->DeauthenticationRequest)
2929 SM_ENTER(WPA_PTK, DISCONNECTED);
2930 else if (sm->AuthenticationRequest)
2931 SM_ENTER(WPA_PTK, AUTHENTICATION);
2932 else if (sm->ReAuthenticationRequest)
2933 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2934 else if (sm->PTKRequest)
2935 SM_ENTER(WPA_PTK, PTKSTART);
2936 else switch (sm->wpa_ptk_state) {
2937 case WPA_PTK_INITIALIZE:
2938 break;
2939 case WPA_PTK_DISCONNECT:
2940 SM_ENTER(WPA_PTK, DISCONNECTED);
2941 break;
2942 case WPA_PTK_DISCONNECTED:
2943 SM_ENTER(WPA_PTK, INITIALIZE);
2944 break;
2945 case WPA_PTK_AUTHENTICATION:
2946 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2947 break;
2948 case WPA_PTK_AUTHENTICATION2:
2949 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2950 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2951 WPA_EAPOL_keyRun) > 0)
2952 SM_ENTER(WPA_PTK, INITPMK);
2953 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
2954 /* FIX: && 802.1X::keyRun */)
2955 SM_ENTER(WPA_PTK, INITPSK);
2956 break;
2957 case WPA_PTK_INITPMK:
2958 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2959 WPA_EAPOL_keyAvailable) > 0)
2960 SM_ENTER(WPA_PTK, PTKSTART);
2961 else {
2962 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2963 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2964 "INITPMK - keyAvailable = false");
2965 SM_ENTER(WPA_PTK, DISCONNECT);
2966 }
2967 break;
2968 case WPA_PTK_INITPSK:
2969 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
2970 NULL))
2971 SM_ENTER(WPA_PTK, PTKSTART);
2972 else {
2973 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2974 "no PSK configured for the STA");
2975 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2976 SM_ENTER(WPA_PTK, DISCONNECT);
2977 }
2978 break;
2979 case WPA_PTK_PTKSTART:
2980 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2981 sm->EAPOLKeyPairwise)
2982 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2983 else if (sm->TimeoutCtr >
2984 sm->wpa_auth->conf.wpa_pairwise_update_count) {
2985 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2986 wpa_auth_vlogger(
2987 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2988 "PTKSTART: Retry limit %u reached",
2989 sm->wpa_auth->conf.wpa_pairwise_update_count);
2990 SM_ENTER(WPA_PTK, DISCONNECT);
2991 } else if (sm->TimeoutEvt)
2992 SM_ENTER(WPA_PTK, PTKSTART);
2993 break;
2994 case WPA_PTK_PTKCALCNEGOTIATING:
2995 if (sm->MICVerified)
2996 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2997 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2998 sm->EAPOLKeyPairwise)
2999 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3000 else if (sm->TimeoutEvt)
3001 SM_ENTER(WPA_PTK, PTKSTART);
3002 break;
3003 case WPA_PTK_PTKCALCNEGOTIATING2:
3004 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3005 break;
3006 case WPA_PTK_PTKINITNEGOTIATING:
3007 if (sm->update_snonce)
3008 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3009 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3010 sm->EAPOLKeyPairwise && sm->MICVerified)
3011 SM_ENTER(WPA_PTK, PTKINITDONE);
3012 else if (sm->TimeoutCtr >
3013 sm->wpa_auth->conf.wpa_pairwise_update_count) {
3014 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3015 wpa_auth_vlogger(
3016 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3017 "PTKINITNEGOTIATING: Retry limit %u reached",
3018 sm->wpa_auth->conf.wpa_pairwise_update_count);
3019 SM_ENTER(WPA_PTK, DISCONNECT);
3020 } else if (sm->TimeoutEvt)
3021 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3022 break;
3023 case WPA_PTK_PTKINITDONE:
3024 break;
3025 }
3026 }
3027
3028
3029 SM_STATE(WPA_PTK_GROUP, IDLE)
3030 {
3031 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
3032 if (sm->Init) {
3033 /* Init flag is not cleared here, so avoid busy
3034 * loop by claiming nothing changed. */
3035 sm->changed = FALSE;
3036 }
3037 sm->GTimeoutCtr = 0;
3038 }
3039
3040
3041 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
3042 {
3043 u8 rsc[WPA_KEY_RSC_LEN];
3044 struct wpa_group *gsm = sm->group;
3045 const u8 *kde;
3046 u8 *kde_buf = NULL, *pos, hdr[2];
3047 size_t kde_len;
3048 u8 *gtk, dummy_gtk[32];
3049
3050 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
3051
3052 sm->GTimeoutCtr++;
3053 if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) {
3054 /* No point in sending the EAPOL-Key - we will disconnect
3055 * immediately following this. */
3056 return;
3057 }
3058
3059 if (sm->wpa == WPA_VERSION_WPA)
3060 sm->PInitAKeys = FALSE;
3061 sm->TimeoutEvt = FALSE;
3062 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3063 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3064 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
3065 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3066 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3067 "sending 1/2 msg of Group Key Handshake");
3068
3069 gtk = gsm->GTK[gsm->GN - 1];
3070 if (sm->wpa_auth->conf.disable_gtk) {
3071 /*
3072 * Provide unique random GTK to each STA to prevent use
3073 * of GTK in the BSS.
3074 */
3075 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
3076 return;
3077 gtk = dummy_gtk;
3078 }
3079 if (sm->wpa == WPA_VERSION_WPA2) {
3080 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
3081 ieee80211w_kde_len(sm);
3082 kde_buf = os_malloc(kde_len);
3083 if (kde_buf == NULL)
3084 return;
3085
3086 kde = pos = kde_buf;
3087 hdr[0] = gsm->GN & 0x03;
3088 hdr[1] = 0;
3089 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3090 gtk, gsm->GTK_len);
3091 pos = ieee80211w_kde_add(sm, pos);
3092 kde_len = pos - kde;
3093 } else {
3094 kde = gtk;
3095 kde_len = gsm->GTK_len;
3096 }
3097
3098 wpa_send_eapol(sm->wpa_auth, sm,
3099 WPA_KEY_INFO_SECURE |
3100 (wpa_mic_len(sm->wpa_key_mgmt) ? WPA_KEY_INFO_MIC : 0) |
3101 WPA_KEY_INFO_ACK |
3102 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
3103 rsc, NULL, kde, kde_len, gsm->GN, 1);
3104
3105 os_free(kde_buf);
3106 }
3107
3108
3109 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
3110 {
3111 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
3112 sm->EAPOLKeyReceived = FALSE;
3113 if (sm->GUpdateStationKeys)
3114 sm->group->GKeyDoneStations--;
3115 sm->GUpdateStationKeys = FALSE;
3116 sm->GTimeoutCtr = 0;
3117 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3118 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3119 "group key handshake completed (%s)",
3120 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3121 sm->has_GTK = TRUE;
3122 }
3123
3124
3125 SM_STATE(WPA_PTK_GROUP, KEYERROR)
3126 {
3127 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
3128 if (sm->GUpdateStationKeys)
3129 sm->group->GKeyDoneStations--;
3130 sm->GUpdateStationKeys = FALSE;
3131 sm->Disconnect = TRUE;
3132 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3133 "group key handshake failed (%s) after %u tries",
3134 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
3135 sm->wpa_auth->conf.wpa_group_update_count);
3136 }
3137
3138
3139 SM_STEP(WPA_PTK_GROUP)
3140 {
3141 if (sm->Init || sm->PtkGroupInit) {
3142 SM_ENTER(WPA_PTK_GROUP, IDLE);
3143 sm->PtkGroupInit = FALSE;
3144 } else switch (sm->wpa_ptk_group_state) {
3145 case WPA_PTK_GROUP_IDLE:
3146 if (sm->GUpdateStationKeys ||
3147 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
3148 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3149 break;
3150 case WPA_PTK_GROUP_REKEYNEGOTIATING:
3151 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3152 !sm->EAPOLKeyPairwise && sm->MICVerified)
3153 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
3154 else if (sm->GTimeoutCtr >
3155 sm->wpa_auth->conf.wpa_group_update_count)
3156 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
3157 else if (sm->TimeoutEvt)
3158 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3159 break;
3160 case WPA_PTK_GROUP_KEYERROR:
3161 SM_ENTER(WPA_PTK_GROUP, IDLE);
3162 break;
3163 case WPA_PTK_GROUP_REKEYESTABLISHED:
3164 SM_ENTER(WPA_PTK_GROUP, IDLE);
3165 break;
3166 }
3167 }
3168
3169
3170 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
3171 struct wpa_group *group)
3172 {
3173 int ret = 0;
3174
3175 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3176 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3177 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
3178 wpa_auth->addr, group->GNonce,
3179 group->GTK[group->GN - 1], group->GTK_len) < 0)
3180 ret = -1;
3181 wpa_hexdump_key(MSG_DEBUG, "GTK",
3182 group->GTK[group->GN - 1], group->GTK_len);
3183
3184 #ifdef CONFIG_IEEE80211W
3185 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3186 size_t len;
3187 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3188 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3189 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3190 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
3191 wpa_auth->addr, group->GNonce,
3192 group->IGTK[group->GN_igtk - 4], len) < 0)
3193 ret = -1;
3194 wpa_hexdump_key(MSG_DEBUG, "IGTK",
3195 group->IGTK[group->GN_igtk - 4], len);
3196 }
3197 #endif /* CONFIG_IEEE80211W */
3198
3199 return ret;
3200 }
3201
3202
3203 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
3204 struct wpa_group *group)
3205 {
3206 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3207 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
3208 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
3209 group->wpa_group_state = WPA_GROUP_GTK_INIT;
3210
3211 /* GTK[0..N] = 0 */
3212 os_memset(group->GTK, 0, sizeof(group->GTK));
3213 group->GN = 1;
3214 group->GM = 2;
3215 #ifdef CONFIG_IEEE80211W
3216 group->GN_igtk = 4;
3217 group->GM_igtk = 5;
3218 #endif /* CONFIG_IEEE80211W */
3219 /* GTK[GN] = CalcGTK() */
3220 wpa_gtk_update(wpa_auth, group);
3221 }
3222
3223
3224 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
3225 {
3226 if (ctx != NULL && ctx != sm->group)
3227 return 0;
3228
3229 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
3230 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3231 "Not in PTKINITDONE; skip Group Key update");
3232 sm->GUpdateStationKeys = FALSE;
3233 return 0;
3234 }
3235 if (sm->GUpdateStationKeys) {
3236 /*
3237 * This should not really happen, so add a debug log entry.
3238 * Since we clear the GKeyDoneStations before the loop, the
3239 * station needs to be counted here anyway.
3240 */
3241 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3242 "GUpdateStationKeys was already set when "
3243 "marking station for GTK rekeying");
3244 }
3245
3246 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
3247 if (sm->is_wnmsleep)
3248 return 0;
3249
3250 sm->group->GKeyDoneStations++;
3251 sm->GUpdateStationKeys = TRUE;
3252
3253 wpa_sm_step(sm);
3254 return 0;
3255 }
3256
3257
3258 #ifdef CONFIG_WNM
3259 /* update GTK when exiting WNM-Sleep Mode */
3260 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
3261 {
3262 if (sm == NULL || sm->is_wnmsleep)
3263 return;
3264
3265 wpa_group_update_sta(sm, NULL);
3266 }
3267
3268
3269 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
3270 {
3271 if (sm)
3272 sm->is_wnmsleep = !!flag;
3273 }
3274
3275
3276 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3277 {
3278 struct wpa_group *gsm = sm->group;
3279 u8 *start = pos;
3280
3281 /*
3282 * GTK subelement:
3283 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
3284 * Key[5..32]
3285 */
3286 *pos++ = WNM_SLEEP_SUBELEM_GTK;
3287 *pos++ = 11 + gsm->GTK_len;
3288 /* Key ID in B0-B1 of Key Info */
3289 WPA_PUT_LE16(pos, gsm->GN & 0x03);
3290 pos += 2;
3291 *pos++ = gsm->GTK_len;
3292 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
3293 return 0;
3294 pos += 8;
3295 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3296 pos += gsm->GTK_len;
3297
3298 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3299 gsm->GN);
3300 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
3301 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3302
3303 return pos - start;
3304 }
3305
3306
3307 #ifdef CONFIG_IEEE80211W
3308 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3309 {
3310 struct wpa_group *gsm = sm->group;
3311 u8 *start = pos;
3312 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3313
3314 /*
3315 * IGTK subelement:
3316 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3317 */
3318 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
3319 *pos++ = 2 + 6 + len;
3320 WPA_PUT_LE16(pos, gsm->GN_igtk);
3321 pos += 2;
3322 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
3323 return 0;
3324 pos += 6;
3325
3326 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
3327 pos += len;
3328
3329 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3330 gsm->GN_igtk);
3331 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
3332 gsm->IGTK[gsm->GN_igtk - 4], len);
3333
3334 return pos - start;
3335 }
3336 #endif /* CONFIG_IEEE80211W */
3337 #endif /* CONFIG_WNM */
3338
3339
3340 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
3341 struct wpa_group *group)
3342 {
3343 int tmp;
3344
3345 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3346 "SETKEYS (VLAN-ID %d)", group->vlan_id);
3347 group->changed = TRUE;
3348 group->wpa_group_state = WPA_GROUP_SETKEYS;
3349 group->GTKReKey = FALSE;
3350 tmp = group->GM;
3351 group->GM = group->GN;
3352 group->GN = tmp;
3353 #ifdef CONFIG_IEEE80211W
3354 tmp = group->GM_igtk;
3355 group->GM_igtk = group->GN_igtk;
3356 group->GN_igtk = tmp;
3357 #endif /* CONFIG_IEEE80211W */
3358 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3359 * counting the STAs that are marked with GUpdateStationKeys instead of
3360 * including all STAs that could be in not-yet-completed state. */
3361 wpa_gtk_update(wpa_auth, group);
3362
3363 if (group->GKeyDoneStations) {
3364 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
3365 "GKeyDoneStations=%d when starting new GTK rekey",
3366 group->GKeyDoneStations);
3367 group->GKeyDoneStations = 0;
3368 }
3369 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
3370 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
3371 group->GKeyDoneStations);
3372 }
3373
3374
3375 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
3376 struct wpa_group *group)
3377 {
3378 int ret = 0;
3379
3380 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
3381 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
3382 broadcast_ether_addr, group->GN,
3383 group->GTK[group->GN - 1], group->GTK_len) < 0)
3384 ret = -1;
3385
3386 #ifdef CONFIG_IEEE80211W
3387 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3388 enum wpa_alg alg;
3389 size_t len;
3390
3391 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
3392 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3393
3394 if (ret == 0 &&
3395 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
3396 broadcast_ether_addr, group->GN_igtk,
3397 group->IGTK[group->GN_igtk - 4], len) < 0)
3398 ret = -1;
3399 }
3400 #endif /* CONFIG_IEEE80211W */
3401
3402 return ret;
3403 }
3404
3405
3406 static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
3407 {
3408 if (sm->group == ctx) {
3409 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
3410 " for discconnection due to fatal failure",
3411 MAC2STR(sm->addr));
3412 sm->Disconnect = TRUE;
3413 }
3414
3415 return 0;
3416 }
3417
3418
3419 static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
3420 struct wpa_group *group)
3421 {
3422 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
3423 group->changed = TRUE;
3424 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
3425 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
3426 }
3427
3428
3429 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
3430 struct wpa_group *group)
3431 {
3432 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3433 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
3434 group->changed = TRUE;
3435 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
3436
3437 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
3438 wpa_group_fatal_failure(wpa_auth, group);
3439 return -1;
3440 }
3441
3442 return 0;
3443 }
3444
3445
3446 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
3447 struct wpa_group *group)
3448 {
3449 if (group->GInit) {
3450 wpa_group_gtk_init(wpa_auth, group);
3451 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
3452 /* Do not allow group operations */
3453 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
3454 group->GTKAuthenticator) {
3455 wpa_group_setkeysdone(wpa_auth, group);
3456 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
3457 group->GTKReKey) {
3458 wpa_group_setkeys(wpa_auth, group);
3459 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
3460 if (group->GKeyDoneStations == 0)
3461 wpa_group_setkeysdone(wpa_auth, group);
3462 else if (group->GTKReKey)
3463 wpa_group_setkeys(wpa_auth, group);
3464 }
3465 }
3466
3467
3468 static int wpa_sm_step(struct wpa_state_machine *sm)
3469 {
3470 if (sm == NULL)
3471 return 0;
3472
3473 if (sm->in_step_loop) {
3474 /* This should not happen, but if it does, make sure we do not
3475 * end up freeing the state machine too early by exiting the
3476 * recursive call. */
3477 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
3478 return 0;
3479 }
3480
3481 sm->in_step_loop = 1;
3482 do {
3483 if (sm->pending_deinit)
3484 break;
3485
3486 sm->changed = FALSE;
3487 sm->wpa_auth->group->changed = FALSE;
3488
3489 SM_STEP_RUN(WPA_PTK);
3490 if (sm->pending_deinit)
3491 break;
3492 SM_STEP_RUN(WPA_PTK_GROUP);
3493 if (sm->pending_deinit)
3494 break;
3495 wpa_group_sm_step(sm->wpa_auth, sm->group);
3496 } while (sm->changed || sm->wpa_auth->group->changed);
3497 sm->in_step_loop = 0;
3498
3499 if (sm->pending_deinit) {
3500 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
3501 "machine deinit for " MACSTR, MAC2STR(sm->addr));
3502 wpa_free_sta_sm(sm);
3503 return 1;
3504 }
3505 return 0;
3506 }
3507
3508
3509 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
3510 {
3511 struct wpa_state_machine *sm = eloop_ctx;
3512 wpa_sm_step(sm);
3513 }
3514
3515
3516 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
3517 {
3518 if (sm == NULL)
3519 return;
3520 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
3521 }
3522
3523
3524 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
3525 {
3526 int tmp, i;
3527 struct wpa_group *group;
3528
3529 if (wpa_auth == NULL)
3530 return;
3531
3532 group = wpa_auth->group;
3533
3534 for (i = 0; i < 2; i++) {
3535 tmp = group->GM;
3536 group->GM = group->GN;
3537 group->GN = tmp;
3538 #ifdef CONFIG_IEEE80211W
3539 tmp = group->GM_igtk;
3540 group->GM_igtk = group->GN_igtk;
3541 group->GN_igtk = tmp;
3542 #endif /* CONFIG_IEEE80211W */
3543 wpa_gtk_update(wpa_auth, group);
3544 wpa_group_config_group_keys(wpa_auth, group);
3545 }
3546 }
3547
3548
3549 static const char * wpa_bool_txt(int val)
3550 {
3551 return val ? "TRUE" : "FALSE";
3552 }
3553
3554
3555 #define RSN_SUITE "%02x-%02x-%02x-%d"
3556 #define RSN_SUITE_ARG(s) \
3557 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3558
3559 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
3560 {
3561 int len = 0, ret;
3562 char pmkid_txt[PMKID_LEN * 2 + 1];
3563 #ifdef CONFIG_RSN_PREAUTH
3564 const int preauth = 1;
3565 #else /* CONFIG_RSN_PREAUTH */
3566 const int preauth = 0;
3567 #endif /* CONFIG_RSN_PREAUTH */
3568
3569 if (wpa_auth == NULL)
3570 return len;
3571
3572 ret = os_snprintf(buf + len, buflen - len,
3573 "dot11RSNAOptionImplemented=TRUE\n"
3574 "dot11RSNAPreauthenticationImplemented=%s\n"
3575 "dot11RSNAEnabled=%s\n"
3576 "dot11RSNAPreauthenticationEnabled=%s\n",
3577 wpa_bool_txt(preauth),
3578 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
3579 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
3580 if (os_snprintf_error(buflen - len, ret))
3581 return len;
3582 len += ret;
3583
3584 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
3585 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
3586
3587 ret = os_snprintf(
3588 buf + len, buflen - len,
3589 "dot11RSNAConfigVersion=%u\n"
3590 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
3591 /* FIX: dot11RSNAConfigGroupCipher */
3592 /* FIX: dot11RSNAConfigGroupRekeyMethod */
3593 /* FIX: dot11RSNAConfigGroupRekeyTime */
3594 /* FIX: dot11RSNAConfigGroupRekeyPackets */
3595 "dot11RSNAConfigGroupRekeyStrict=%u\n"
3596 "dot11RSNAConfigGroupUpdateCount=%u\n"
3597 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
3598 "dot11RSNAConfigGroupCipherSize=%u\n"
3599 "dot11RSNAConfigPMKLifetime=%u\n"
3600 "dot11RSNAConfigPMKReauthThreshold=%u\n"
3601 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
3602 "dot11RSNAConfigSATimeout=%u\n"
3603 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
3604 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
3605 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
3606 "dot11RSNAPMKIDUsed=%s\n"
3607 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
3608 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
3609 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
3610 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
3611 "dot11RSNA4WayHandshakeFailures=%u\n"
3612 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
3613 RSN_VERSION,
3614 !!wpa_auth->conf.wpa_strict_rekey,
3615 wpa_auth->conf.wpa_group_update_count,
3616 wpa_auth->conf.wpa_pairwise_update_count,
3617 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
3618 dot11RSNAConfigPMKLifetime,
3619 dot11RSNAConfigPMKReauthThreshold,
3620 dot11RSNAConfigSATimeout,
3621 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
3622 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
3623 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
3624 pmkid_txt,
3625 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
3626 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
3627 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
3628 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
3629 wpa_auth->dot11RSNA4WayHandshakeFailures);
3630 if (os_snprintf_error(buflen - len, ret))
3631 return len;
3632 len += ret;
3633
3634 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
3635 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
3636
3637 /* Private MIB */
3638 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
3639 wpa_auth->group->wpa_group_state);
3640 if (os_snprintf_error(buflen - len, ret))
3641 return len;
3642 len += ret;
3643
3644 return len;
3645 }
3646
3647
3648 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
3649 {
3650 int len = 0, ret;
3651 u32 pairwise = 0;
3652
3653 if (sm == NULL)
3654 return 0;
3655
3656 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
3657
3658 /* dot11RSNAStatsEntry */
3659
3660 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
3661 WPA_PROTO_RSN : WPA_PROTO_WPA,
3662 sm->pairwise);
3663 if (pairwise == 0)
3664 return 0;
3665
3666 ret = os_snprintf(
3667 buf + len, buflen - len,
3668 /* TODO: dot11RSNAStatsIndex */
3669 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
3670 "dot11RSNAStatsVersion=1\n"
3671 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
3672 /* TODO: dot11RSNAStatsTKIPICVErrors */
3673 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
3674 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
3675 /* TODO: dot11RSNAStatsCCMPReplays */
3676 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
3677 /* TODO: dot11RSNAStatsTKIPReplays */,
3678 MAC2STR(sm->addr),
3679 RSN_SUITE_ARG(pairwise),
3680 sm->dot11RSNAStatsTKIPLocalMICFailures,
3681 sm->dot11RSNAStatsTKIPRemoteMICFailures);
3682 if (os_snprintf_error(buflen - len, ret))
3683 return len;
3684 len += ret;
3685
3686 /* Private MIB */
3687 ret = os_snprintf(buf + len, buflen - len,
3688 "hostapdWPAPTKState=%d\n"
3689 "hostapdWPAPTKGroupState=%d\n",
3690 sm->wpa_ptk_state,
3691 sm->wpa_ptk_group_state);
3692 if (os_snprintf_error(buflen - len, ret))
3693 return len;
3694 len += ret;
3695
3696 return len;
3697 }
3698
3699
3700 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
3701 {
3702 if (wpa_auth)
3703 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
3704 }
3705
3706
3707 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
3708 {
3709 return sm && sm->pairwise_set;
3710 }
3711
3712
3713 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
3714 {
3715 return sm->pairwise;
3716 }
3717
3718
3719 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
3720 {
3721 if (sm == NULL)
3722 return -1;
3723 return sm->wpa_key_mgmt;
3724 }
3725
3726
3727 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
3728 {
3729 if (sm == NULL)
3730 return 0;
3731 return sm->wpa;
3732 }
3733
3734
3735 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
3736 struct rsn_pmksa_cache_entry *entry)
3737 {
3738 if (sm == NULL || sm->pmksa != entry)
3739 return -1;
3740 sm->pmksa = NULL;
3741 return 0;
3742 }
3743
3744
3745 struct rsn_pmksa_cache_entry *
3746 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
3747 {
3748 return sm ? sm->pmksa : NULL;
3749 }
3750
3751
3752 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
3753 {
3754 if (sm)
3755 sm->dot11RSNAStatsTKIPLocalMICFailures++;
3756 }
3757
3758
3759 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
3760 {
3761 if (wpa_auth == NULL)
3762 return NULL;
3763 *len = wpa_auth->wpa_ie_len;
3764 return wpa_auth->wpa_ie;
3765 }
3766
3767
3768 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
3769 unsigned int pmk_len,
3770 int session_timeout, struct eapol_state_machine *eapol)
3771 {
3772 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
3773 sm->wpa_auth->conf.disable_pmksa_caching)
3774 return -1;
3775
3776 if (sm->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
3777 if (pmk_len > PMK_LEN_SUITE_B_192)
3778 pmk_len = PMK_LEN_SUITE_B_192;
3779 } else if (pmk_len > PMK_LEN) {
3780 pmk_len = PMK_LEN;
3781 }
3782
3783 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
3784 sm->PTK.kck, sm->PTK.kck_len,
3785 sm->wpa_auth->addr, sm->addr, session_timeout,
3786 eapol, sm->wpa_key_mgmt))
3787 return 0;
3788
3789 return -1;
3790 }
3791
3792
3793 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
3794 const u8 *pmk, size_t len, const u8 *sta_addr,
3795 int session_timeout,
3796 struct eapol_state_machine *eapol)
3797 {
3798 if (wpa_auth == NULL)
3799 return -1;
3800
3801 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
3802 NULL, 0,
3803 wpa_auth->addr,
3804 sta_addr, session_timeout, eapol,
3805 WPA_KEY_MGMT_IEEE8021X))
3806 return 0;
3807
3808 return -1;
3809 }
3810
3811
3812 int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
3813 const u8 *pmk, const u8 *pmkid)
3814 {
3815 if (wpa_auth->conf.disable_pmksa_caching)
3816 return -1;
3817
3818 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
3819 NULL, 0,
3820 wpa_auth->addr, addr, 0, NULL,
3821 WPA_KEY_MGMT_SAE))
3822 return 0;
3823
3824 return -1;
3825 }
3826
3827
3828 void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
3829 const u8 *sta_addr)
3830 {
3831 struct rsn_pmksa_cache_entry *pmksa;
3832
3833 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
3834 return;
3835 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3836 if (pmksa) {
3837 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
3838 MACSTR " based on request", MAC2STR(sta_addr));
3839 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
3840 }
3841 }
3842
3843
3844 int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
3845 size_t len)
3846 {
3847 if (!wpa_auth || !wpa_auth->pmksa)
3848 return 0;
3849 return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
3850 }
3851
3852
3853 void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
3854 {
3855 if (wpa_auth && wpa_auth->pmksa)
3856 pmksa_cache_auth_flush(wpa_auth->pmksa);
3857 }
3858
3859
3860 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
3861 #ifdef CONFIG_MESH
3862
3863 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
3864 char *buf, size_t len)
3865 {
3866 if (!wpa_auth || !wpa_auth->pmksa)
3867 return 0;
3868
3869 return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
3870 }
3871
3872
3873 struct rsn_pmksa_cache_entry *
3874 wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
3875 const u8 *pmkid, int expiration)
3876 {
3877 struct rsn_pmksa_cache_entry *entry;
3878 struct os_reltime now;
3879
3880 entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa,
3881 spa, 0, NULL, WPA_KEY_MGMT_SAE);
3882 if (!entry)
3883 return NULL;
3884
3885 os_get_reltime(&now);
3886 entry->expiration = now.sec + expiration;
3887 return entry;
3888 }
3889
3890
3891 int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
3892 struct rsn_pmksa_cache_entry *entry)
3893 {
3894 int ret;
3895
3896 if (!wpa_auth || !wpa_auth->pmksa)
3897 return -1;
3898
3899 ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
3900 if (ret < 0)
3901 wpa_printf(MSG_DEBUG,
3902 "RSN: Failed to store external PMKSA cache for "
3903 MACSTR, MAC2STR(entry->spa));
3904
3905 return ret;
3906 }
3907
3908 #endif /* CONFIG_MESH */
3909 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
3910
3911
3912 struct rsn_pmksa_cache_entry *
3913 wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
3914 const u8 *pmkid)
3915 {
3916 if (!wpa_auth || !wpa_auth->pmksa)
3917 return NULL;
3918 return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
3919 }
3920
3921
3922 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
3923 struct wpa_state_machine *sm,
3924 struct wpa_authenticator *wpa_auth,
3925 u8 *pmkid, u8 *pmk)
3926 {
3927 if (!sm)
3928 return;
3929
3930 sm->pmksa = pmksa;
3931 os_memcpy(pmk, pmksa->pmk, PMK_LEN);
3932 os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
3933 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
3934 }
3935
3936
3937 /*
3938 * Remove and free the group from wpa_authenticator. This is triggered by a
3939 * callback to make sure nobody is currently iterating the group list while it
3940 * gets modified.
3941 */
3942 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
3943 struct wpa_group *group)
3944 {
3945 struct wpa_group *prev = wpa_auth->group;
3946
3947 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
3948 group->vlan_id);
3949
3950 while (prev) {
3951 if (prev->next == group) {
3952 /* This never frees the special first group as needed */
3953 prev->next = group->next;
3954 os_free(group);
3955 break;
3956 }
3957 prev = prev->next;
3958 }
3959
3960 }
3961
3962
3963 /* Increase the reference counter for group */
3964 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
3965 struct wpa_group *group)
3966 {
3967 /* Skip the special first group */
3968 if (wpa_auth->group == group)
3969 return;
3970
3971 group->references++;
3972 }
3973
3974
3975 /* Decrease the reference counter and maybe free the group */
3976 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
3977 struct wpa_group *group)
3978 {
3979 /* Skip the special first group */
3980 if (wpa_auth->group == group)
3981 return;
3982
3983 group->references--;
3984 if (group->references)
3985 return;
3986 wpa_group_free(wpa_auth, group);
3987 }
3988
3989
3990 /*
3991 * Add a group that has its references counter set to zero. Caller needs to
3992 * call wpa_group_get() on the return value to mark the entry in use.
3993 */
3994 static struct wpa_group *
3995 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3996 {
3997 struct wpa_group *group;
3998
3999 if (wpa_auth == NULL || wpa_auth->group == NULL)
4000 return NULL;
4001
4002 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
4003 vlan_id);
4004 group = wpa_group_init(wpa_auth, vlan_id, 0);
4005 if (group == NULL)
4006 return NULL;
4007
4008 group->next = wpa_auth->group->next;
4009 wpa_auth->group->next = group;
4010
4011 return group;
4012 }
4013
4014
4015 /*
4016 * Enforce that the group state machine for the VLAN is running, increase
4017 * reference counter as interface is up. References might have been increased
4018 * even if a negative value is returned.
4019 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4020 */
4021 int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4022 {
4023 struct wpa_group *group;
4024
4025 if (wpa_auth == NULL)
4026 return 0;
4027
4028 group = wpa_auth->group;
4029 while (group) {
4030 if (group->vlan_id == vlan_id)
4031 break;
4032 group = group->next;
4033 }
4034
4035 if (group == NULL) {
4036 group = wpa_auth_add_group(wpa_auth, vlan_id);
4037 if (group == NULL)
4038 return -1;
4039 }
4040
4041 wpa_printf(MSG_DEBUG,
4042 "WPA: Ensure group state machine running for VLAN ID %d",
4043 vlan_id);
4044
4045 wpa_group_get(wpa_auth, group);
4046 group->num_setup_iface++;
4047
4048 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4049 return -1;
4050
4051 return 0;
4052 }
4053
4054
4055 /*
4056 * Decrease reference counter, expected to be zero afterwards.
4057 * returns: -1 on error (group not found, group in fail state)
4058 * -2 if wpa_group is still referenced
4059 * 0 else
4060 */
4061 int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4062 {
4063 struct wpa_group *group;
4064 int ret = 0;
4065
4066 if (wpa_auth == NULL)
4067 return 0;
4068
4069 group = wpa_auth->group;
4070 while (group) {
4071 if (group->vlan_id == vlan_id)
4072 break;
4073 group = group->next;
4074 }
4075
4076 if (group == NULL)
4077 return -1;
4078
4079 wpa_printf(MSG_DEBUG,
4080 "WPA: Try stopping group state machine for VLAN ID %d",
4081 vlan_id);
4082
4083 if (group->num_setup_iface <= 0) {
4084 wpa_printf(MSG_ERROR,
4085 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4086 vlan_id);
4087 return -1;
4088 }
4089 group->num_setup_iface--;
4090
4091 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4092 ret = -1;
4093
4094 if (group->references > 1) {
4095 wpa_printf(MSG_DEBUG,
4096 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4097 vlan_id);
4098 ret = -2;
4099 }
4100
4101 wpa_group_put(wpa_auth, group);
4102
4103 return ret;
4104 }
4105
4106
4107 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
4108 {
4109 struct wpa_group *group;
4110
4111 if (sm == NULL || sm->wpa_auth == NULL)
4112 return 0;
4113
4114 group = sm->wpa_auth->group;
4115 while (group) {
4116 if (group->vlan_id == vlan_id)
4117 break;
4118 group = group->next;
4119 }
4120
4121 if (group == NULL) {
4122 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
4123 if (group == NULL)
4124 return -1;
4125 }
4126
4127 if (sm->group == group)
4128 return 0;
4129
4130 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4131 return -1;
4132
4133 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
4134 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
4135
4136 wpa_group_get(sm->wpa_auth, group);
4137 wpa_group_put(sm->wpa_auth, sm->group);
4138 sm->group = group;
4139
4140 return 0;
4141 }
4142
4143
4144 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
4145 struct wpa_state_machine *sm, int ack)
4146 {
4147 if (wpa_auth == NULL || sm == NULL)
4148 return;
4149 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
4150 " ack=%d", MAC2STR(sm->addr), ack);
4151 if (sm->pending_1_of_4_timeout && ack) {
4152 /*
4153 * Some deployed supplicant implementations update their SNonce
4154 * for each EAPOL-Key 2/4 message even within the same 4-way
4155 * handshake and then fail to use the first SNonce when
4156 * deriving the PTK. This results in unsuccessful 4-way
4157 * handshake whenever the relatively short initial timeout is
4158 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4159 * around this by increasing the timeout now that we know that
4160 * the station has received the frame.
4161 */
4162 int timeout_ms = eapol_key_timeout_subseq;
4163 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
4164 "timeout by %u ms because of acknowledged frame",
4165 timeout_ms);
4166 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
4167 eloop_register_timeout(timeout_ms / 1000,
4168 (timeout_ms % 1000) * 1000,
4169 wpa_send_eapol_timeout, wpa_auth, sm);
4170 }
4171 }
4172
4173
4174 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
4175 {
4176 if (sm == NULL)
4177 return 0;
4178 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
4179 }
4180
4181
4182 int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
4183 {
4184 if (sm == NULL)
4185 return 0;
4186 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
4187 }
4188
4189
4190 #ifdef CONFIG_P2P
4191 int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
4192 {
4193 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
4194 return -1;
4195 os_memcpy(addr, sm->ip_addr, 4);
4196 return 0;
4197 }
4198 #endif /* CONFIG_P2P */
4199
4200
4201 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
4202 struct radius_das_attrs *attr)
4203 {
4204 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
4205 }
4206
4207
4208 void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
4209 {
4210 struct wpa_group *group;
4211
4212 if (!wpa_auth)
4213 return;
4214 for (group = wpa_auth->group; group; group = group->next)
4215 wpa_group_config_group_keys(wpa_auth, group);
4216 }