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