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