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