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