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