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