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