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