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