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