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