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