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