]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/wpa_auth.c
AP: Allow PTK rekeying without Ext KeyID to be disabled as a workaround
[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)
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
6fc6879b
JM
3224SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
3225{
f2c4b44b 3226 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde = NULL, *pos, dummy_gtk[32];
6fc6879b
JM
3227 size_t gtk_len, kde_len;
3228 struct wpa_group *gsm = sm->group;
3229 u8 *wpa_ie;
7a4b01c8 3230 int wpa_ie_len, secure, gtkidx, encr = 0;
f2c4b44b 3231 u8 *wpa_ie_buf = NULL;
6fc6879b
JM
3232
3233 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
3234 sm->TimeoutEvt = FALSE;
bae61562
JM
3235
3236 sm->TimeoutCtr++;
6f234c1e
JM
3237 if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3238 sm->TimeoutCtr > 1) {
3239 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
3240 return;
3241 }
41f140d3 3242 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
bae61562
JM
3243 /* No point in sending the EAPOL-Key - we will disconnect
3244 * immediately following this. */
3245 return;
3246 }
3247
86dfabb8 3248 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
16889aff 3249 GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2])
6fc6879b
JM
3250 */
3251 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3252 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
86dfabb8 3253 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
6fc6879b
JM
3254 wpa_ie = sm->wpa_auth->wpa_ie;
3255 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
3256 if (sm->wpa == WPA_VERSION_WPA &&
3257 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
3258 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
774d4145 3259 /* WPA-only STA, remove RSN IE and possible MDIE */
6fc6879b 3260 wpa_ie = wpa_ie + wpa_ie[1] + 2;
774d4145
JM
3261 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
3262 wpa_ie = wpa_ie + wpa_ie[1] + 2;
6fc6879b
JM
3263 wpa_ie_len = wpa_ie[1] + 2;
3264 }
f2c4b44b
JM
3265#ifdef CONFIG_TESTING_OPTIONS
3266 if (sm->wpa_auth->conf.rsnxe_override_eapol_len) {
3267 u8 *obuf = sm->wpa_auth->conf.rsnxe_override_eapol;
3268 size_t olen = sm->wpa_auth->conf.rsnxe_override_eapol_len;
3269 const u8 *rsnxe;
3270
3271 wpa_hexdump(MSG_DEBUG,
3272 "TESTING: wpa_ie before RSNXE EAPOL override",
3273 wpa_ie, wpa_ie_len);
3274 wpa_ie_buf = os_malloc(wpa_ie_len + olen);
3275 if (!wpa_ie_buf)
3276 return;
3277 os_memcpy(wpa_ie_buf, wpa_ie, wpa_ie_len);
3278 wpa_ie = wpa_ie_buf;
3279 rsnxe = get_ie(wpa_ie, wpa_ie_len, WLAN_EID_RSNX);
3280 if (rsnxe) {
3281 u8 rsnxe_len = 2 + rsnxe[1];
3282
3283 os_memmove((void *) rsnxe, rsnxe + rsnxe_len,
3284 wpa_ie_len - (rsnxe - wpa_ie) - rsnxe_len);
3285 wpa_ie_len -= rsnxe_len;
3286 }
3287 os_memcpy(wpa_ie + wpa_ie_len, obuf, olen);
3288 wpa_ie_len += olen;
3289 wpa_hexdump(MSG_DEBUG,
3290 "TESTING: wpa_ie after RSNXE EAPOL override",
3291 wpa_ie, wpa_ie_len);
3292 }
3293#endif /* CONFIG_TESTING_OPTIONS */
6fc6879b
JM
3294 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3295 "sending 3/4 msg of 4-Way Handshake");
3296 if (sm->wpa == WPA_VERSION_WPA2) {
3297 /* WPA2 send GTK in the 4-way handshake */
3298 secure = 1;
3299 gtk = gsm->GTK[gsm->GN - 1];
3300 gtk_len = gsm->GTK_len;
8d660a4b
JM
3301 if (sm->wpa_auth->conf.disable_gtk ||
3302 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
83421850
JM
3303 /*
3304 * Provide unique random GTK to each STA to prevent use
3305 * of GTK in the BSS.
3306 */
3307 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
f2c4b44b 3308 goto done;
83421850
JM
3309 gtk = dummy_gtk;
3310 }
7a4b01c8 3311 gtkidx = gsm->GN;
6fc6879b
JM
3312 _rsc = rsc;
3313 encr = 1;
3314 } else {
3315 /* WPA does not include GTK in msg 3/4 */
3316 secure = 0;
3317 gtk = NULL;
3318 gtk_len = 0;
6fc6879b 3319 _rsc = NULL;
0d442aff
JM
3320 if (sm->rx_eapol_key_secure) {
3321 /*
3322 * It looks like Windows 7 supplicant tries to use
3323 * Secure bit in msg 2/4 after having reported Michael
3324 * MIC failure and it then rejects the 4-way handshake
3325 * if msg 3/4 does not set Secure bit. Work around this
3326 * by setting the Secure bit here even in the case of
3327 * WPA if the supplicant used it first.
3328 */
3329 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4740d5b9
JM
3330 "STA used Secure bit in WPA msg 2/4 - "
3331 "set Secure for 3/4 as workaround");
0d442aff
JM
3332 secure = 1;
3333 }
6fc6879b
JM
3334 }
3335
17c2559c 3336 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
6fc6879b
JM
3337 if (gtk)
3338 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
4ec1fd8e 3339#ifdef CONFIG_IEEE80211R_AP
86dfabb8
JM
3340 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3341 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
3342 kde_len += 300; /* FTIE + 2 * TIE */
3343 }
4ec1fd8e 3344#endif /* CONFIG_IEEE80211R_AP */
25ef8529
JM
3345#ifdef CONFIG_P2P
3346 if (WPA_GET_BE32(sm->ip_addr) > 0)
3347 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
3348#endif /* CONFIG_P2P */
6fc6879b
JM
3349 kde = os_malloc(kde_len);
3350 if (kde == NULL)
f2c4b44b 3351 goto done;
6fc6879b
JM
3352
3353 pos = kde;
3354 os_memcpy(pos, wpa_ie, wpa_ie_len);
3355 pos += wpa_ie_len;
4ec1fd8e 3356#ifdef CONFIG_IEEE80211R_AP
26e23750 3357 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
59e78c24
JM
3358 int res;
3359 size_t elen;
3360
3361 elen = pos - kde;
3362 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
26e23750
JM
3363 if (res < 0) {
3364 wpa_printf(MSG_ERROR, "FT: Failed to insert "
3365 "PMKR1Name into RSN IE in EAPOL-Key data");
f2c4b44b 3366 goto done;
26e23750 3367 }
59e78c24
JM
3368 pos -= wpa_ie_len;
3369 pos += elen;
26e23750 3370 }
4ec1fd8e 3371#endif /* CONFIG_IEEE80211R_AP */
6fc6879b
JM
3372 if (gtk) {
3373 u8 hdr[2];
7a4b01c8 3374 hdr[0] = gtkidx & 0x03;
6fc6879b
JM
3375 hdr[1] = 0;
3376 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3377 gtk, gtk_len);
3378 }
3379 pos = ieee80211w_kde_add(sm, pos);
f2c4b44b
JM
3380 if (ocv_oci_add(sm, &pos) < 0)
3381 goto done;
6fc6879b 3382
4ec1fd8e 3383#ifdef CONFIG_IEEE80211R_AP
86dfabb8
JM
3384 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3385 int res;
3386 struct wpa_auth_config *conf;
3387
3388 conf = &sm->wpa_auth->conf;
9257610a
JM
3389 if (sm->assoc_resp_ftie &&
3390 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
3391 os_memcpy(pos, sm->assoc_resp_ftie,
3392 2 + sm->assoc_resp_ftie[1]);
3393 res = 2 + sm->assoc_resp_ftie[1];
3394 } else {
a7968ea5
JM
3395 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
3396
3397 res = wpa_write_ftie(conf, use_sha384,
3398 conf->r0_key_holder,
9257610a
JM
3399 conf->r0_key_holder_len,
3400 NULL, NULL, pos,
3401 kde + kde_len - pos,
3402 NULL, 0);
3403 }
86dfabb8
JM
3404 if (res < 0) {
3405 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
3406 "into EAPOL-Key Key Data");
f2c4b44b 3407 goto done;
86dfabb8
JM
3408 }
3409 pos += res;
3410
3411 /* TIE[ReassociationDeadline] (TU) */
3412 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3413 *pos++ = 5;
3414 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
3415 WPA_PUT_LE32(pos, conf->reassociation_deadline);
3416 pos += 4;
3417
3418 /* TIE[KeyLifetime] (seconds) */
3419 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3420 *pos++ = 5;
3421 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
83fe4bd3 3422 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
86dfabb8
JM
3423 pos += 4;
3424 }
4ec1fd8e 3425#endif /* CONFIG_IEEE80211R_AP */
25ef8529
JM
3426#ifdef CONFIG_P2P
3427 if (WPA_GET_BE32(sm->ip_addr) > 0) {
3428 u8 addr[3 * 4];
3429 os_memcpy(addr, sm->ip_addr, 4);
3430 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
3431 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
3432 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
3433 addr, sizeof(addr), NULL, 0);
3434 }
3435#endif /* CONFIG_P2P */
86dfabb8 3436
6fc6879b 3437 wpa_send_eapol(sm->wpa_auth, sm,
b729fd8d 3438 (secure ? WPA_KEY_INFO_SECURE : 0) |
567da5bb
JM
3439 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3440 WPA_KEY_INFO_MIC : 0) |
6fc6879b
JM
3441 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
3442 WPA_KEY_INFO_KEY_TYPE,
7a4b01c8 3443 _rsc, sm->ANonce, kde, pos - kde, 0, encr);
f2c4b44b 3444done:
6fc6879b 3445 os_free(kde);
f2c4b44b 3446 os_free(wpa_ie_buf);
6fc6879b
JM
3447}
3448
3449
3450SM_STATE(WPA_PTK, PTKINITDONE)
3451{
3452 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
3453 sm->EAPOLKeyReceived = FALSE;
3454 if (sm->Pair) {
c3550295
JM
3455 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
3456 int klen = wpa_cipher_key_len(sm->pairwise);
6fc6879b 3457 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
a919a260
AW
3458 sm->PTK.tk, klen,
3459 KEY_FLAG_PAIRWISE_RX_TX)) {
567da5bb
JM
3460 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
3461 WLAN_REASON_PREV_AUTH_NOT_VALID);
6fc6879b
JM
3462 return;
3463 }
3464 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3465 sm->pairwise_set = TRUE;
3466
a40bd06e 3467 wpa_auth_set_ptk_rekey_timer(sm);
581a8cde 3468
a1ea1b45 3469 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
567da5bb 3470 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
a1ea1b45 3471 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
6fc6879b
JM
3472 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3473 WPA_EAPOL_authorized, 1);
3474 }
3475 }
3476
3477 if (0 /* IBSS == TRUE */) {
3478 sm->keycount++;
3479 if (sm->keycount == 2) {
3480 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3481 WPA_EAPOL_portValid, 1);
3482 }
3483 } else {
3484 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
3485 1);
3486 }
3487 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
3488 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
3489 if (sm->wpa == WPA_VERSION_WPA)
3490 sm->PInitAKeys = TRUE;
3491 else
3492 sm->has_GTK = TRUE;
3493 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3494 "pairwise key handshake completed (%s)",
3495 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3496
4ec1fd8e 3497#ifdef CONFIG_IEEE80211R_AP
6fc6879b 3498 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
4ec1fd8e 3499#endif /* CONFIG_IEEE80211R_AP */
6fc6879b
JM
3500}
3501
3502
3503SM_STEP(WPA_PTK)
3504{
3505 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3506
3507 if (sm->Init)
3508 SM_ENTER(WPA_PTK, INITIALIZE);
3509 else if (sm->Disconnect
1aae01fc
BG
3510 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3511 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
3512 "WPA_PTK: sm->Disconnect");
6fc6879b 3513 SM_ENTER(WPA_PTK, DISCONNECT);
1aae01fc 3514 }
6fc6879b
JM
3515 else if (sm->DeauthenticationRequest)
3516 SM_ENTER(WPA_PTK, DISCONNECTED);
3517 else if (sm->AuthenticationRequest)
3518 SM_ENTER(WPA_PTK, AUTHENTICATION);
3519 else if (sm->ReAuthenticationRequest)
3520 SM_ENTER(WPA_PTK, AUTHENTICATION2);
0adc9b28
JM
3521 else if (sm->PTKRequest) {
3522 if (wpa_auth_sm_ptk_update(sm) < 0)
3523 SM_ENTER(WPA_PTK, DISCONNECTED);
3524 else
3525 SM_ENTER(WPA_PTK, PTKSTART);
3526 } else switch (sm->wpa_ptk_state) {
6fc6879b
JM
3527 case WPA_PTK_INITIALIZE:
3528 break;
3529 case WPA_PTK_DISCONNECT:
3530 SM_ENTER(WPA_PTK, DISCONNECTED);
3531 break;
3532 case WPA_PTK_DISCONNECTED:
3533 SM_ENTER(WPA_PTK, INITIALIZE);
3534 break;
3535 case WPA_PTK_AUTHENTICATION:
3536 SM_ENTER(WPA_PTK, AUTHENTICATION2);
3537 break;
3538 case WPA_PTK_AUTHENTICATION2:
56586197 3539 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
6fc6879b
JM
3540 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
3541 WPA_EAPOL_keyRun) > 0)
3542 SM_ENTER(WPA_PTK, INITPMK);
a1ea1b45
JM
3543 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3544 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
6fc6879b
JM
3545 /* FIX: && 802.1X::keyRun */)
3546 SM_ENTER(WPA_PTK, INITPSK);
567da5bb
JM
3547 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
3548 SM_ENTER(WPA_PTK, INITPMK);
6fc6879b
JM
3549 break;
3550 case WPA_PTK_INITPMK:
3551 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
567da5bb 3552 WPA_EAPOL_keyAvailable) > 0) {
6fc6879b 3553 SM_ENTER(WPA_PTK, PTKSTART);
567da5bb
JM
3554#ifdef CONFIG_DPP
3555 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->pmksa) {
3556 SM_ENTER(WPA_PTK, PTKSTART);
3557#endif /* CONFIG_DPP */
3558 } else {
6fc6879b 3559 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1aae01fc
BG
3560 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3561 "INITPMK - keyAvailable = false");
6fc6879b
JM
3562 SM_ENTER(WPA_PTK, DISCONNECT);
3563 }
3564 break;
3565 case WPA_PTK_INITPSK:
759fd76b 3566 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
dbfa691d 3567 NULL, NULL, NULL)) {
6fc6879b 3568 SM_ENTER(WPA_PTK, PTKSTART);
e61fea6b
JM
3569#ifdef CONFIG_SAE
3570 } else if (wpa_auth_uses_sae(sm) && sm->pmksa) {
3571 SM_ENTER(WPA_PTK, PTKSTART);
3572#endif /* CONFIG_SAE */
3573 } else {
6fc6879b
JM
3574 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3575 "no PSK configured for the STA");
3576 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3577 SM_ENTER(WPA_PTK, DISCONNECT);
3578 }
3579 break;
3580 case WPA_PTK_PTKSTART:
3581 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3582 sm->EAPOLKeyPairwise)
3583 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3584 else if (sm->TimeoutCtr >
41f140d3 3585 sm->wpa_auth->conf.wpa_pairwise_update_count) {
6fc6879b 3586 wpa_auth->dot11RSNA4WayHandshakeFailures++;
41f140d3
GK
3587 wpa_auth_vlogger(
3588 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3589 "PTKSTART: Retry limit %u reached",
3590 sm->wpa_auth->conf.wpa_pairwise_update_count);
6fc6879b
JM
3591 SM_ENTER(WPA_PTK, DISCONNECT);
3592 } else if (sm->TimeoutEvt)
3593 SM_ENTER(WPA_PTK, PTKSTART);
3594 break;
3595 case WPA_PTK_PTKCALCNEGOTIATING:
3596 if (sm->MICVerified)
3597 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
3598 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3599 sm->EAPOLKeyPairwise)
3600 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3601 else if (sm->TimeoutEvt)
3602 SM_ENTER(WPA_PTK, PTKSTART);
3603 break;
3604 case WPA_PTK_PTKCALCNEGOTIATING2:
3605 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3606 break;
3607 case WPA_PTK_PTKINITNEGOTIATING:
68921e24
JM
3608 if (sm->update_snonce)
3609 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3610 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3611 sm->EAPOLKeyPairwise && sm->MICVerified)
6fc6879b
JM
3612 SM_ENTER(WPA_PTK, PTKINITDONE);
3613 else if (sm->TimeoutCtr >
6f234c1e
JM
3614 sm->wpa_auth->conf.wpa_pairwise_update_count ||
3615 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3616 sm->TimeoutCtr > 1)) {
6fc6879b 3617 wpa_auth->dot11RSNA4WayHandshakeFailures++;
41f140d3
GK
3618 wpa_auth_vlogger(
3619 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3620 "PTKINITNEGOTIATING: Retry limit %u reached",
3621 sm->wpa_auth->conf.wpa_pairwise_update_count);
6fc6879b
JM
3622 SM_ENTER(WPA_PTK, DISCONNECT);
3623 } else if (sm->TimeoutEvt)
3624 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3625 break;
3626 case WPA_PTK_PTKINITDONE:
3627 break;
3628 }
3629}
3630
3631
3632SM_STATE(WPA_PTK_GROUP, IDLE)
3633{
3634 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
3635 if (sm->Init) {
3636 /* Init flag is not cleared here, so avoid busy
3637 * loop by claiming nothing changed. */
3638 sm->changed = FALSE;
3639 }
3640 sm->GTimeoutCtr = 0;
3641}
3642
3643
3644SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
3645{
3646 u8 rsc[WPA_KEY_RSC_LEN];
3647 struct wpa_group *gsm = sm->group;
e41e4f9e
JM
3648 const u8 *kde;
3649 u8 *kde_buf = NULL, *pos, hdr[2];
6fc6879b 3650 size_t kde_len;
83421850 3651 u8 *gtk, dummy_gtk[32];
6fc6879b
JM
3652
3653 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
bae61562
JM
3654
3655 sm->GTimeoutCtr++;
6f234c1e
JM
3656 if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3657 sm->GTimeoutCtr > 1) {
3658 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3659 return;
3660 }
41f140d3 3661 if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) {
bae61562
JM
3662 /* No point in sending the EAPOL-Key - we will disconnect
3663 * immediately following this. */
3664 return;
3665 }
3666
6fc6879b
JM
3667 if (sm->wpa == WPA_VERSION_WPA)
3668 sm->PInitAKeys = FALSE;
3669 sm->TimeoutEvt = FALSE;
3670 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3671 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3672 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
3673 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3674 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3675 "sending 1/2 msg of Group Key Handshake");
3676
83421850 3677 gtk = gsm->GTK[gsm->GN - 1];
8d660a4b
JM
3678 if (sm->wpa_auth->conf.disable_gtk ||
3679 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
83421850
JM
3680 /*
3681 * Provide unique random GTK to each STA to prevent use
3682 * of GTK in the BSS.
3683 */
3684 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
3685 return;
3686 gtk = dummy_gtk;
3687 }
6fc6879b
JM
3688 if (sm->wpa == WPA_VERSION_WPA2) {
3689 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
17c2559c 3690 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
e41e4f9e
JM
3691 kde_buf = os_malloc(kde_len);
3692 if (kde_buf == NULL)
6fc6879b
JM
3693 return;
3694
e41e4f9e 3695 kde = pos = kde_buf;
6fc6879b
JM
3696 hdr[0] = gsm->GN & 0x03;
3697 hdr[1] = 0;
3698 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
83421850 3699 gtk, gsm->GTK_len);
6fc6879b 3700 pos = ieee80211w_kde_add(sm, pos);
17c2559c
MV
3701 if (ocv_oci_add(sm, &pos) < 0) {
3702 os_free(kde_buf);
3703 return;
3704 }
e41e4f9e 3705 kde_len = pos - kde;
6fc6879b 3706 } else {
83421850 3707 kde = gtk;
e41e4f9e 3708 kde_len = gsm->GTK_len;
6fc6879b
JM
3709 }
3710
3711 wpa_send_eapol(sm->wpa_auth, sm,
b729fd8d 3712 WPA_KEY_INFO_SECURE |
567da5bb
JM
3713 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3714 WPA_KEY_INFO_MIC : 0) |
6fc6879b
JM
3715 WPA_KEY_INFO_ACK |
3716 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
b0fb2be7 3717 rsc, NULL, kde, kde_len, gsm->GN, 1);
e41e4f9e
JM
3718
3719 os_free(kde_buf);
6fc6879b
JM
3720}
3721
3722
3723SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
3724{
aed61c4e
MV
3725#ifdef CONFIG_OCV
3726 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3727 const u8 *key_data, *mic;
3728 struct ieee802_1x_hdr *hdr;
3729 struct wpa_eapol_key *key;
3730 struct wpa_eapol_ie_parse kde;
3731 size_t mic_len;
3732 u16 key_data_length;
3733#endif /* CONFIG_OCV */
3734
6fc6879b
JM
3735 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
3736 sm->EAPOLKeyReceived = FALSE;
aed61c4e
MV
3737
3738#ifdef CONFIG_OCV
3739 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
3740
3741 /*
3742 * Note: last_rx_eapol_key length fields have already been validated in
3743 * wpa_receive().
3744 */
3745 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
3746 key = (struct wpa_eapol_key *) (hdr + 1);
3747 mic = (u8 *) (key + 1);
3748 key_data = mic + mic_len + 2;
3749 key_data_length = WPA_GET_BE16(mic + mic_len);
3750 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
3751 sizeof(*key) - mic_len - 2)
3752 return;
3753
3754 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
3755 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
3756 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3757 return;
3758 }
3759
3760 if (wpa_auth_uses_ocv(sm)) {
3761 struct wpa_channel_info ci;
3762 int tx_chanwidth;
3763 int tx_seg1_idx;
3764
3765 if (wpa_channel_info(wpa_auth, &ci) != 0) {
3766 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3767 "Failed to get channel info to validate received OCI in EAPOL-Key group 1/2");
3768 return;
3769 }
3770
3771 if (get_sta_tx_parameters(sm,
3772 channel_width_to_int(ci.chanwidth),
3773 ci.seg1_idx, &tx_chanwidth,
3774 &tx_seg1_idx) < 0)
3775 return;
3776
3777 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
3778 tx_chanwidth, tx_seg1_idx) != 0) {
3779 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3780 ocv_errorstr);
3781 return;
3782 }
3783 }
3784#endif /* CONFIG_OCV */
3785
6fc6879b
JM
3786 if (sm->GUpdateStationKeys)
3787 sm->group->GKeyDoneStations--;
3788 sm->GUpdateStationKeys = FALSE;
3789 sm->GTimeoutCtr = 0;
3790 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3791 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3792 "group key handshake completed (%s)",
3793 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3794 sm->has_GTK = TRUE;
3795}
3796
3797
3798SM_STATE(WPA_PTK_GROUP, KEYERROR)
3799{
3800 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
3801 if (sm->GUpdateStationKeys)
3802 sm->group->GKeyDoneStations--;
3803 sm->GUpdateStationKeys = FALSE;
3804 sm->Disconnect = TRUE;
4bb9b674
GK
3805 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3806 "group key handshake failed (%s) after %u tries",
3807 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
3808 sm->wpa_auth->conf.wpa_group_update_count);
6fc6879b
JM
3809}
3810
3811
3812SM_STEP(WPA_PTK_GROUP)
3813{
9663596f 3814 if (sm->Init || sm->PtkGroupInit) {
6fc6879b 3815 SM_ENTER(WPA_PTK_GROUP, IDLE);
9663596f
JM
3816 sm->PtkGroupInit = FALSE;
3817 } else switch (sm->wpa_ptk_group_state) {
6fc6879b
JM
3818 case WPA_PTK_GROUP_IDLE:
3819 if (sm->GUpdateStationKeys ||
3820 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
3821 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3822 break;
3823 case WPA_PTK_GROUP_REKEYNEGOTIATING:
3824 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3825 !sm->EAPOLKeyPairwise && sm->MICVerified)
3826 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
3827 else if (sm->GTimeoutCtr >
6f234c1e
JM
3828 sm->wpa_auth->conf.wpa_group_update_count ||
3829 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3830 sm->GTimeoutCtr > 1))
6fc6879b
JM
3831 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
3832 else if (sm->TimeoutEvt)
3833 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3834 break;
3835 case WPA_PTK_GROUP_KEYERROR:
3836 SM_ENTER(WPA_PTK_GROUP, IDLE);
3837 break;
3838 case WPA_PTK_GROUP_REKEYESTABLISHED:
3839 SM_ENTER(WPA_PTK_GROUP, IDLE);
3840 break;
3841 }
3842}
3843
3844
3845static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
3846 struct wpa_group *group)
3847{
3848 int ret = 0;
555dcd75 3849 size_t len;
6fc6879b 3850
6fc6879b
JM
3851 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3852 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3c7302c2
JM
3853 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
3854 wpa_auth->addr, group->GNonce,
3855 group->GTK[group->GN - 1], group->GTK_len) < 0)
3856 ret = -1;
5cb9d5c3
JM
3857 wpa_hexdump_key(MSG_DEBUG, "GTK",
3858 group->GTK[group->GN - 1], group->GTK_len);
6fc6879b 3859
70f8cc8e 3860 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
8dd9f9cd 3861 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
5cb9d5c3
JM
3862 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3863 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3c7302c2
JM
3864 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
3865 wpa_auth->addr, group->GNonce,
8dd9f9cd 3866 group->IGTK[group->GN_igtk - 4], len) < 0)
3c7302c2 3867 ret = -1;
6fc6879b 3868 wpa_hexdump_key(MSG_DEBUG, "IGTK",
8dd9f9cd 3869 group->IGTK[group->GN_igtk - 4], len);
6fc6879b 3870 }
6fc6879b 3871
555dcd75
JM
3872 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION &&
3873 wpa_auth->conf.beacon_prot) {
3874 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3875 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3876 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3877 if (wpa_gmk_to_gtk(group->GMK, "BIGTK key expansion",
3878 wpa_auth->addr, group->GNonce,
3879 group->BIGTK[group->GN_bigtk - 6], len) < 0)
3880 ret = -1;
3881 wpa_hexdump_key(MSG_DEBUG, "BIGTK",
3882 group->BIGTK[group->GN_bigtk - 6], len);
3883 }
3884
6fc6879b
JM
3885 return ret;
3886}
3887
3888
3889static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
3890 struct wpa_group *group)
3891{
3892 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3893 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
3894 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
3895 group->wpa_group_state = WPA_GROUP_GTK_INIT;
3896
3897 /* GTK[0..N] = 0 */
3898 os_memset(group->GTK, 0, sizeof(group->GTK));
3899 group->GN = 1;
3900 group->GM = 2;
6fc6879b
JM
3901 group->GN_igtk = 4;
3902 group->GM_igtk = 5;
555dcd75
JM
3903 group->GN_bigtk = 6;
3904 group->GM_bigtk = 7;
6fc6879b
JM
3905 /* GTK[GN] = CalcGTK() */
3906 wpa_gtk_update(wpa_auth, group);
3907}
3908
3909
3910static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
3911{
473b6f22
MB
3912 if (ctx != NULL && ctx != sm->group)
3913 return 0;
3914
6fc6879b
JM
3915 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
3916 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3917 "Not in PTKINITDONE; skip Group Key update");
3c183894 3918 sm->GUpdateStationKeys = FALSE;
6fc6879b
JM
3919 return 0;
3920 }
9663596f
JM
3921 if (sm->GUpdateStationKeys) {
3922 /*
3c183894
JM
3923 * This should not really happen, so add a debug log entry.
3924 * Since we clear the GKeyDoneStations before the loop, the
3925 * station needs to be counted here anyway.
9663596f
JM
3926 */
3927 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3c183894
JM
3928 "GUpdateStationKeys was already set when "
3929 "marking station for GTK rekeying");
9663596f 3930 }
3c183894 3931
ad3872a3 3932 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
d32d94db
XC
3933 if (sm->is_wnmsleep)
3934 return 0;
d32d94db 3935
3c183894
JM
3936 sm->group->GKeyDoneStations++;
3937 sm->GUpdateStationKeys = TRUE;
3938
6fc6879b
JM
3939 wpa_sm_step(sm);
3940 return 0;
3941}
3942
3943
b5bf84ba 3944#ifdef CONFIG_WNM_AP
ad3872a3 3945/* update GTK when exiting WNM-Sleep Mode */
d32d94db
XC
3946void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
3947{
629edfef 3948 if (sm == NULL || sm->is_wnmsleep)
d32d94db
XC
3949 return;
3950
3951 wpa_group_update_sta(sm, NULL);
3952}
3953
3954
3955void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
3956{
629edfef
JM
3957 if (sm)
3958 sm->is_wnmsleep = !!flag;
d32d94db
XC
3959}
3960
3961
3962int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3963{
d32d94db 3964 struct wpa_group *gsm = sm->group;
835822d4 3965 u8 *start = pos;
d32d94db
XC
3966
3967 /*
835822d4 3968 * GTK subelement:
d32d94db 3969 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
835822d4 3970 * Key[5..32]
d32d94db 3971 */
835822d4
JM
3972 *pos++ = WNM_SLEEP_SUBELEM_GTK;
3973 *pos++ = 11 + gsm->GTK_len;
d32d94db 3974 /* Key ID in B0-B1 of Key Info */
835822d4
JM
3975 WPA_PUT_LE16(pos, gsm->GN & 0x03);
3976 pos += 2;
3977 *pos++ = gsm->GTK_len;
3978 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
d32d94db 3979 return 0;
835822d4
JM
3980 pos += 8;
3981 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3982 pos += gsm->GTK_len;
d32d94db 3983
835822d4
JM
3984 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3985 gsm->GN);
3986 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
d32d94db 3987 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
d32d94db 3988
835822d4 3989 return pos - start;
d32d94db
XC
3990}
3991
3992
d32d94db
XC
3993int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3994{
d32d94db 3995 struct wpa_group *gsm = sm->group;
835822d4 3996 u8 *start = pos;
8dd9f9cd 3997 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
d32d94db 3998
835822d4
JM
3999 /*
4000 * IGTK subelement:
4001 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
4002 */
4003 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
8dd9f9cd 4004 *pos++ = 2 + 6 + len;
835822d4
JM
4005 WPA_PUT_LE16(pos, gsm->GN_igtk);
4006 pos += 2;
4007 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
d32d94db 4008 return 0;
835822d4 4009 pos += 6;
d32d94db 4010
8dd9f9cd
JM
4011 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
4012 pos += len;
d32d94db 4013
835822d4
JM
4014 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
4015 gsm->GN_igtk);
4016 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
8dd9f9cd 4017 gsm->IGTK[gsm->GN_igtk - 4], len);
d32d94db 4018
835822d4 4019 return pos - start;
d32d94db 4020}
7d2ed8ba 4021
16889aff
JM
4022
4023int wpa_wnmsleep_bigtk_subelem(struct wpa_state_machine *sm, u8 *pos)
4024{
4025 struct wpa_group *gsm = sm->group;
4026 u8 *start = pos;
4027 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
4028
4029 /*
4030 * BIGTK subelement:
4031 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
4032 */
4033 *pos++ = WNM_SLEEP_SUBELEM_BIGTK;
4034 *pos++ = 2 + 6 + len;
4035 WPA_PUT_LE16(pos, gsm->GN_bigtk);
4036 pos += 2;
4037 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_bigtk, pos) != 0)
4038 return 0;
4039 pos += 6;
4040
4041 os_memcpy(pos, gsm->BIGTK[gsm->GN_bigtk - 6], len);
4042 pos += len;
4043
4044 wpa_printf(MSG_DEBUG, "WNM: BIGTK Key ID %u in WNM-Sleep Mode exit",
4045 gsm->GN_bigtk);
4046 wpa_hexdump_key(MSG_DEBUG, "WNM: BIGTK in WNM-Sleep Mode exit",
4047 gsm->IGTK[gsm->GN_bigtk - 6], len);
4048
4049 return pos - start;
4050}
4051
b5bf84ba 4052#endif /* CONFIG_WNM_AP */
d32d94db
XC
4053
4054
6fc6879b
JM
4055static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
4056 struct wpa_group *group)
4057{
4058 int tmp;
4059
4060 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
4061 "SETKEYS (VLAN-ID %d)", group->vlan_id);
4062 group->changed = TRUE;
4063 group->wpa_group_state = WPA_GROUP_SETKEYS;
4064 group->GTKReKey = FALSE;
4065 tmp = group->GM;
4066 group->GM = group->GN;
4067 group->GN = tmp;
6fc6879b
JM
4068 tmp = group->GM_igtk;
4069 group->GM_igtk = group->GN_igtk;
4070 group->GN_igtk = tmp;
555dcd75
JM
4071 tmp = group->GM_bigtk;
4072 group->GM_bigtk = group->GN_bigtk;
4073 group->GN_bigtk = tmp;
6fc6879b
JM
4074 /* "GKeyDoneStations = GNoStations" is done in more robust way by
4075 * counting the STAs that are marked with GUpdateStationKeys instead of
4076 * including all STAs that could be in not-yet-completed state. */
4077 wpa_gtk_update(wpa_auth, group);
4078
3c183894
JM
4079 if (group->GKeyDoneStations) {
4080 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
4081 "GKeyDoneStations=%d when starting new GTK rekey",
4082 group->GKeyDoneStations);
4083 group->GKeyDoneStations = 0;
4084 }
473b6f22 4085 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
6fc6879b
JM
4086 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
4087 group->GKeyDoneStations);
4088}
4089
4090
1bdb7ab3
JM
4091static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
4092 struct wpa_group *group)
6fc6879b 4093{
3c7302c2
JM
4094 int ret = 0;
4095
3c7302c2 4096 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
c3550295 4097 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
0382097e 4098 broadcast_ether_addr, group->GN,
a919a260
AW
4099 group->GTK[group->GN - 1], group->GTK_len,
4100 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
3c7302c2 4101 ret = -1;
6fc6879b 4102
8dd9f9cd
JM
4103 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
4104 enum wpa_alg alg;
4105 size_t len;
4106
4107 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
4108 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
4109
4110 if (ret == 0 &&
4111 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
4112 broadcast_ether_addr, group->GN_igtk,
a919a260
AW
4113 group->IGTK[group->GN_igtk - 4], len,
4114 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
8dd9f9cd 4115 ret = -1;
555dcd75
JM
4116
4117 if (ret == 0 && wpa_auth->conf.beacon_prot &&
4118 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
4119 broadcast_ether_addr, group->GN_bigtk,
4120 group->BIGTK[group->GN_bigtk - 6], len,
4121 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
4122 ret = -1;
8dd9f9cd 4123 }
3c7302c2
JM
4124
4125 return ret;
6fc6879b
JM
4126}
4127
4128
7d7f7be2
JM
4129static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
4130{
4131 if (sm->group == ctx) {
4132 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
4133 " for discconnection due to fatal failure",
4134 MAC2STR(sm->addr));
4135 sm->Disconnect = TRUE;
4136 }
4137
4138 return 0;
4139}
4140
4141
4142static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
4143 struct wpa_group *group)
4144{
4145 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
4146 group->changed = TRUE;
4147 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
4148 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
4149}
4150
4151
1bdb7ab3
JM
4152static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
4153 struct wpa_group *group)
4154{
4155 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
4156 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
4157 group->changed = TRUE;
4158 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
4159
7d7f7be2
JM
4160 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
4161 wpa_group_fatal_failure(wpa_auth, group);
1bdb7ab3 4162 return -1;
7d7f7be2 4163 }
1bdb7ab3
JM
4164
4165 return 0;
4166}
4167
4168
6fc6879b
JM
4169static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
4170 struct wpa_group *group)
4171{
4172 if (group->GInit) {
4173 wpa_group_gtk_init(wpa_auth, group);
7d7f7be2
JM
4174 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
4175 /* Do not allow group operations */
6fc6879b
JM
4176 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
4177 group->GTKAuthenticator) {
4178 wpa_group_setkeysdone(wpa_auth, group);
4179 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
4180 group->GTKReKey) {
4181 wpa_group_setkeys(wpa_auth, group);
4182 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
4183 if (group->GKeyDoneStations == 0)
4184 wpa_group_setkeysdone(wpa_auth, group);
4185 else if (group->GTKReKey)
4186 wpa_group_setkeys(wpa_auth, group);
4187 }
4188}
4189
4190
e4a6ea1d 4191static int wpa_sm_step(struct wpa_state_machine *sm)
6fc6879b
JM
4192{
4193 if (sm == NULL)
e4a6ea1d 4194 return 0;
6fc6879b
JM
4195
4196 if (sm->in_step_loop) {
4197 /* This should not happen, but if it does, make sure we do not
4198 * end up freeing the state machine too early by exiting the
4199 * recursive call. */
4200 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
e4a6ea1d 4201 return 0;
6fc6879b
JM
4202 }
4203
4204 sm->in_step_loop = 1;
4205 do {
4206 if (sm->pending_deinit)
4207 break;
4208
4209 sm->changed = FALSE;
4210 sm->wpa_auth->group->changed = FALSE;
4211
4212 SM_STEP_RUN(WPA_PTK);
4213 if (sm->pending_deinit)
4214 break;
4215 SM_STEP_RUN(WPA_PTK_GROUP);
4216 if (sm->pending_deinit)
4217 break;
4218 wpa_group_sm_step(sm->wpa_auth, sm->group);
4219 } while (sm->changed || sm->wpa_auth->group->changed);
4220 sm->in_step_loop = 0;
4221
4222 if (sm->pending_deinit) {
4223 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
4224 "machine deinit for " MACSTR, MAC2STR(sm->addr));
4225 wpa_free_sta_sm(sm);
e4a6ea1d 4226 return 1;
6fc6879b 4227 }
e4a6ea1d 4228 return 0;
6fc6879b
JM
4229}
4230
4231
4232static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
4233{
4234 struct wpa_state_machine *sm = eloop_ctx;
4235 wpa_sm_step(sm);
4236}
4237
4238
4239void wpa_auth_sm_notify(struct wpa_state_machine *sm)
4240{
4241 if (sm == NULL)
4242 return;
4243 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
4244}
4245
4246
4247void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
4248{
4249 int tmp, i;
4250 struct wpa_group *group;
4251
4252 if (wpa_auth == NULL)
4253 return;
4254
4255 group = wpa_auth->group;
4256
4257 for (i = 0; i < 2; i++) {
4258 tmp = group->GM;
4259 group->GM = group->GN;
4260 group->GN = tmp;
6fc6879b
JM
4261 tmp = group->GM_igtk;
4262 group->GM_igtk = group->GN_igtk;
4263 group->GN_igtk = tmp;
555dcd75
JM
4264 tmp = group->GM_bigtk;
4265 group->GM_bigtk = group->GN_bigtk;
4266 group->GN_bigtk = tmp;
6fc6879b 4267 wpa_gtk_update(wpa_auth, group);
9354e594 4268 wpa_group_config_group_keys(wpa_auth, group);
6fc6879b
JM
4269 }
4270}
4271
4272
a6da824b 4273static const char * wpa_bool_txt(int val)
6fc6879b 4274{
a6da824b 4275 return val ? "TRUE" : "FALSE";
6fc6879b
JM
4276}
4277
4278
6fc6879b
JM
4279#define RSN_SUITE "%02x-%02x-%02x-%d"
4280#define RSN_SUITE_ARG(s) \
4281((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
4282
4283int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
4284{
4285 int len = 0, ret;
4286 char pmkid_txt[PMKID_LEN * 2 + 1];
8ce58ceb
IH
4287#ifdef CONFIG_RSN_PREAUTH
4288 const int preauth = 1;
4289#else /* CONFIG_RSN_PREAUTH */
4290 const int preauth = 0;
4291#endif /* CONFIG_RSN_PREAUTH */
6fc6879b
JM
4292
4293 if (wpa_auth == NULL)
4294 return len;
4295
4296 ret = os_snprintf(buf + len, buflen - len,
4297 "dot11RSNAOptionImplemented=TRUE\n"
8ce58ceb 4298 "dot11RSNAPreauthenticationImplemented=%s\n"
6fc6879b
JM
4299 "dot11RSNAEnabled=%s\n"
4300 "dot11RSNAPreauthenticationEnabled=%s\n",
8ce58ceb 4301 wpa_bool_txt(preauth),
6fc6879b
JM
4302 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
4303 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
d85e1fc8 4304 if (os_snprintf_error(buflen - len, ret))
6fc6879b
JM
4305 return len;
4306 len += ret;
4307
4308 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
4309 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
4310
4311 ret = os_snprintf(
4312 buf + len, buflen - len,
4313 "dot11RSNAConfigVersion=%u\n"
4314 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
4315 /* FIX: dot11RSNAConfigGroupCipher */
4316 /* FIX: dot11RSNAConfigGroupRekeyMethod */
4317 /* FIX: dot11RSNAConfigGroupRekeyTime */
4318 /* FIX: dot11RSNAConfigGroupRekeyPackets */
4319 "dot11RSNAConfigGroupRekeyStrict=%u\n"
4320 "dot11RSNAConfigGroupUpdateCount=%u\n"
4321 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
4322 "dot11RSNAConfigGroupCipherSize=%u\n"
4323 "dot11RSNAConfigPMKLifetime=%u\n"
4324 "dot11RSNAConfigPMKReauthThreshold=%u\n"
4325 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4326 "dot11RSNAConfigSATimeout=%u\n"
4327 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
4328 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
4329 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
4330 "dot11RSNAPMKIDUsed=%s\n"
4331 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
4332 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
4333 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
4334 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4335 "dot11RSNA4WayHandshakeFailures=%u\n"
4336 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4337 RSN_VERSION,
4338 !!wpa_auth->conf.wpa_strict_rekey,
41f140d3
GK
4339 wpa_auth->conf.wpa_group_update_count,
4340 wpa_auth->conf.wpa_pairwise_update_count,
c3550295 4341 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
6fc6879b
JM
4342 dot11RSNAConfigPMKLifetime,
4343 dot11RSNAConfigPMKReauthThreshold,
4344 dot11RSNAConfigSATimeout,
4345 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
4346 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
4347 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
4348 pmkid_txt,
4349 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
4350 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
4351 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
4352 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
4353 wpa_auth->dot11RSNA4WayHandshakeFailures);
d85e1fc8 4354 if (os_snprintf_error(buflen - len, ret))
6fc6879b
JM
4355 return len;
4356 len += ret;
4357
4358 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
4359 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4360
4361 /* Private MIB */
4362 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
4363 wpa_auth->group->wpa_group_state);
d85e1fc8 4364 if (os_snprintf_error(buflen - len, ret))
6fc6879b
JM
4365 return len;
4366 len += ret;
4367
4368 return len;
4369}
4370
4371
4372int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
4373{
4374 int len = 0, ret;
4375 u32 pairwise = 0;
4376
4377 if (sm == NULL)
4378 return 0;
4379
4380 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4381
4382 /* dot11RSNAStatsEntry */
4383
c3550295
JM
4384 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
4385 WPA_PROTO_RSN : WPA_PROTO_WPA,
4386 sm->pairwise);
4387 if (pairwise == 0)
6fc6879b
JM
4388 return 0;
4389
4390 ret = os_snprintf(
4391 buf + len, buflen - len,
4392 /* TODO: dot11RSNAStatsIndex */
4393 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
4394 "dot11RSNAStatsVersion=1\n"
4395 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
4396 /* TODO: dot11RSNAStatsTKIPICVErrors */
4397 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
d9040cdb 4398 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
6fc6879b
JM
4399 /* TODO: dot11RSNAStatsCCMPReplays */
4400 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
4401 /* TODO: dot11RSNAStatsTKIPReplays */,
4402 MAC2STR(sm->addr),
4403 RSN_SUITE_ARG(pairwise),
4404 sm->dot11RSNAStatsTKIPLocalMICFailures,
4405 sm->dot11RSNAStatsTKIPRemoteMICFailures);
d85e1fc8 4406 if (os_snprintf_error(buflen - len, ret))
6fc6879b
JM
4407 return len;
4408 len += ret;
4409
4410 /* Private MIB */
4411 ret = os_snprintf(buf + len, buflen - len,
d1b1f9fa
JM
4412 "wpa=%d\n"
4413 "AKMSuiteSelector=" RSN_SUITE "\n"
6fc6879b
JM
4414 "hostapdWPAPTKState=%d\n"
4415 "hostapdWPAPTKGroupState=%d\n",
d1b1f9fa
JM
4416 sm->wpa,
4417 RSN_SUITE_ARG(wpa_akm_to_suite(sm->wpa_key_mgmt)),
6fc6879b
JM
4418 sm->wpa_ptk_state,
4419 sm->wpa_ptk_group_state);
d85e1fc8 4420 if (os_snprintf_error(buflen - len, ret))
6fc6879b
JM
4421 return len;
4422 len += ret;
4423
4424 return len;
4425}
4426
4427
4428void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
4429{
4430 if (wpa_auth)
4431 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
4432}
4433
4434
4435int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
4436{
4437 return sm && sm->pairwise_set;
4438}
4439
4440
ff36ff00
JM
4441int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
4442{
4443 return sm->pairwise;
4444}
4445
4446
b08c9ad0
MK
4447const u8 * wpa_auth_get_pmk(struct wpa_state_machine *sm, int *len)
4448{
4449 if (!sm)
4450 return NULL;
4451 *len = sm->pmk_len;
4452 return sm->PMK;
4453}
4454
4455
6fc6879b
JM
4456int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
4457{
4458 if (sm == NULL)
4459 return -1;
4460 return sm->wpa_key_mgmt;
4461}
4462
4463
4464int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
4465{
4466 if (sm == NULL)
4467 return 0;
4468 return sm->wpa;
4469}
4470
4471
0e3bd7ac
MV
4472int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
4473{
4474 if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
4475 return 0;
4476 return sm->tk_already_set;
4477}
4478
4479
2f1357fb
JM
4480int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm)
4481{
4482 if (!sm || !wpa_key_mgmt_fils(sm->wpa_key_mgmt))
4483 return 0;
4484 return sm->tk_already_set;
4485}
4486
4487
6fc6879b
JM
4488int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
4489 struct rsn_pmksa_cache_entry *entry)
4490{
4491 if (sm == NULL || sm->pmksa != entry)
4492 return -1;
4493 sm->pmksa = NULL;
4494 return 0;
4495}
4496
4497
4498struct rsn_pmksa_cache_entry *
4499wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
4500{
4501 return sm ? sm->pmksa : NULL;
4502}
4503
4504
4505void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
4506{
4507 if (sm)
4508 sm->dot11RSNAStatsTKIPLocalMICFailures++;
4509}
4510
4511
4512const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
4513{
4514 if (wpa_auth == NULL)
4515 return NULL;
4516 *len = wpa_auth->wpa_ie_len;
4517 return wpa_auth->wpa_ie;
4518}
4519
4520
4521int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
207976f0 4522 unsigned int pmk_len,
6fc6879b
JM
4523 int session_timeout, struct eapol_state_machine *eapol)
4524{
cb465555
JM
4525 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
4526 sm->wpa_auth->conf.disable_pmksa_caching)
6fc6879b
JM
4527 return -1;
4528
64f48093
JM
4529#ifdef CONFIG_IEEE80211R_AP
4530 if (pmk_len >= 2 * PMK_LEN && wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
4531 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
4532 !wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
4533 /* Cache MPMK/XXKey instead of initial part from MSK */
4534 pmk = pmk + PMK_LEN;
4535 pmk_len = PMK_LEN;
4536 } else
4537#endif /* CONFIG_IEEE80211R_AP */
834c5d68 4538 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
207976f0
JM
4539 if (pmk_len > PMK_LEN_SUITE_B_192)
4540 pmk_len = PMK_LEN_SUITE_B_192;
4541 } else if (pmk_len > PMK_LEN) {
4542 pmk_len = PMK_LEN;
4543 }
4544
64f48093 4545 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK", pmk, pmk_len);
70c93963 4546 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
98cd3d1c 4547 sm->PTK.kck, sm->PTK.kck_len,
4bb081f1
JM
4548 sm->wpa_auth->addr, sm->addr, session_timeout,
4549 eapol, sm->wpa_key_mgmt))
6fc6879b
JM
4550 return 0;
4551
4552 return -1;
4553}
4554
4555
4556int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
4557 const u8 *pmk, size_t len, const u8 *sta_addr,
4558 int session_timeout,
4559 struct eapol_state_machine *eapol)
4560{
4561 if (wpa_auth == NULL)
4562 return -1;
4563
64f48093 4564 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from preauth", pmk, len);
70c93963 4565 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
087a1f4e
JM
4566 NULL, 0,
4567 wpa_auth->addr,
4bb081f1
JM
4568 sta_addr, session_timeout, eapol,
4569 WPA_KEY_MGMT_IEEE8021X))
6fc6879b
JM
4570 return 0;
4571
4572 return -1;
4573}
4574
4575
f2991170 4576int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
70c93963 4577 const u8 *pmk, const u8 *pmkid)
f2991170
JM
4578{
4579 if (wpa_auth->conf.disable_pmksa_caching)
4580 return -1;
4581
64f48093 4582 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from SAE", pmk, PMK_LEN);
70c93963 4583 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
087a1f4e 4584 NULL, 0,
f2991170
JM
4585 wpa_auth->addr, addr, 0, NULL,
4586 WPA_KEY_MGMT_SAE))
4587 return 0;
4588
4589 return -1;
4590}
4591
4592
9d94e4bb
JM
4593void wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid)
4594{
4595 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
4596 sm->pmkid_set = 1;
4597}
4598
4599
0c52953b
JM
4600int wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr,
4601 const u8 *pmk, size_t pmk_len, const u8 *pmkid,
4602 int session_timeout, int akmp)
4603{
4604 if (wpa_auth->conf.disable_pmksa_caching)
4605 return -1;
4606
64f48093 4607 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK (2)", pmk, PMK_LEN);
0c52953b
JM
4608 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
4609 NULL, 0, wpa_auth->addr, addr, session_timeout,
4610 NULL, akmp))
4611 return 0;
4612
4613 return -1;
4614}
4615
4616
901d1fe1
JM
4617void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
4618 const u8 *sta_addr)
4619{
4620 struct rsn_pmksa_cache_entry *pmksa;
4621
4622 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
4623 return;
4624 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
4625 if (pmksa) {
4626 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
4627 MACSTR " based on request", MAC2STR(sta_addr));
4628 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
4629 }
4630}
4631
4632
b8daac18
MH
4633int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
4634 size_t len)
4635{
4636 if (!wpa_auth || !wpa_auth->pmksa)
4637 return 0;
4638 return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
4639}
4640
4641
4c522c77
MH
4642void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
4643{
4644 if (wpa_auth && wpa_auth->pmksa)
4645 pmksa_cache_auth_flush(wpa_auth->pmksa);
4646}
4647
4648
4d77d80e
MH
4649#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4650#ifdef CONFIG_MESH
4651
4652int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
4653 char *buf, size_t len)
4654{
4655 if (!wpa_auth || !wpa_auth->pmksa)
4656 return 0;
4657
4658 return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
4659}
4660
4661
4662struct rsn_pmksa_cache_entry *
4663wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
4664 const u8 *pmkid, int expiration)
4665{
4666 struct rsn_pmksa_cache_entry *entry;
4667 struct os_reltime now;
4668
4669 entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa,
4670 spa, 0, NULL, WPA_KEY_MGMT_SAE);
4671 if (!entry)
4672 return NULL;
4673
4674 os_get_reltime(&now);
4675 entry->expiration = now.sec + expiration;
4676 return entry;
4677}
4678
4679
4680int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
4681 struct rsn_pmksa_cache_entry *entry)
4682{
4683 int ret;
4684
4685 if (!wpa_auth || !wpa_auth->pmksa)
4686 return -1;
4687
4688 ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
4689 if (ret < 0)
4690 wpa_printf(MSG_DEBUG,
4691 "RSN: Failed to store external PMKSA cache for "
4692 MACSTR, MAC2STR(entry->spa));
4693
4694 return ret;
4695}
4696
4697#endif /* CONFIG_MESH */
4698#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4699
4700
9f2cf23e 4701struct rsn_pmksa_cache_entry *
c1bd4bac
JM
4702wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
4703 const u8 *pmkid)
9f2cf23e
MH
4704{
4705 if (!wpa_auth || !wpa_auth->pmksa)
4706 return NULL;
c1bd4bac 4707 return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
9f2cf23e
MH
4708}
4709
4710
4711void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
4712 struct wpa_state_machine *sm,
4713 struct wpa_authenticator *wpa_auth,
4714 u8 *pmkid, u8 *pmk)
4715{
4716 if (!sm)
4717 return;
4718
4719 sm->pmksa = pmksa;
8854f90b
JM
4720 os_memcpy(pmk, pmksa->pmk, PMK_LEN);
4721 os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
4722 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
9f2cf23e
MH
4723}
4724
4725
a0ad9e8c
MB
4726/*
4727 * Remove and free the group from wpa_authenticator. This is triggered by a
4728 * callback to make sure nobody is currently iterating the group list while it
4729 * gets modified.
4730 */
4731static void wpa_group_free(struct wpa_authenticator *wpa_auth,
4732 struct wpa_group *group)
4733{
4734 struct wpa_group *prev = wpa_auth->group;
4735
4736 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
4737 group->vlan_id);
4738
4739 while (prev) {
4740 if (prev->next == group) {
4741 /* This never frees the special first group as needed */
4742 prev->next = group->next;
4743 os_free(group);
4744 break;
4745 }
4746 prev = prev->next;
4747 }
4748
4749}
4750
4751
4752/* Increase the reference counter for group */
4753static void wpa_group_get(struct wpa_authenticator *wpa_auth,
4754 struct wpa_group *group)
4755{
4756 /* Skip the special first group */
4757 if (wpa_auth->group == group)
4758 return;
4759
4760 group->references++;
4761}
4762
4763
4764/* Decrease the reference counter and maybe free the group */
4765static void wpa_group_put(struct wpa_authenticator *wpa_auth,
4766 struct wpa_group *group)
4767{
4768 /* Skip the special first group */
4769 if (wpa_auth->group == group)
4770 return;
4771
4772 group->references--;
4773 if (group->references)
4774 return;
4775 wpa_group_free(wpa_auth, group);
4776}
4777
4778
4779/*
4780 * Add a group that has its references counter set to zero. Caller needs to
4781 * call wpa_group_get() on the return value to mark the entry in use.
4782 */
6fc6879b
JM
4783static struct wpa_group *
4784wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4785{
4786 struct wpa_group *group;
4787
4788 if (wpa_auth == NULL || wpa_auth->group == NULL)
4789 return NULL;
4790
4791 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
4792 vlan_id);
bdffdc5d 4793 group = wpa_group_init(wpa_auth, vlan_id, 0);
6fc6879b
JM
4794 if (group == NULL)
4795 return NULL;
4796
4797 group->next = wpa_auth->group->next;
4798 wpa_auth->group->next = group;
4799
4800 return group;
4801}
4802
4803
7cebc8e2
MB
4804/*
4805 * Enforce that the group state machine for the VLAN is running, increase
4806 * reference counter as interface is up. References might have been increased
4807 * even if a negative value is returned.
4808 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4809 */
4810int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4811{
4812 struct wpa_group *group;
4813
4814 if (wpa_auth == NULL)
4815 return 0;
4816
4817 group = wpa_auth->group;
4818 while (group) {
4819 if (group->vlan_id == vlan_id)
4820 break;
4821 group = group->next;
4822 }
4823
4824 if (group == NULL) {
4825 group = wpa_auth_add_group(wpa_auth, vlan_id);
4826 if (group == NULL)
4827 return -1;
4828 }
4829
4830 wpa_printf(MSG_DEBUG,
4831 "WPA: Ensure group state machine running for VLAN ID %d",
4832 vlan_id);
4833
4834 wpa_group_get(wpa_auth, group);
4835 group->num_setup_iface++;
4836
4837 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4838 return -1;
4839
4840 return 0;
4841}
4842
4843
4844/*
4845 * Decrease reference counter, expected to be zero afterwards.
4846 * returns: -1 on error (group not found, group in fail state)
4847 * -2 if wpa_group is still referenced
4848 * 0 else
4849 */
4850int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4851{
4852 struct wpa_group *group;
4853 int ret = 0;
4854
4855 if (wpa_auth == NULL)
4856 return 0;
4857
4858 group = wpa_auth->group;
4859 while (group) {
4860 if (group->vlan_id == vlan_id)
4861 break;
4862 group = group->next;
4863 }
4864
4865 if (group == NULL)
4866 return -1;
4867
4868 wpa_printf(MSG_DEBUG,
4869 "WPA: Try stopping group state machine for VLAN ID %d",
4870 vlan_id);
4871
4872 if (group->num_setup_iface <= 0) {
4873 wpa_printf(MSG_ERROR,
4874 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4875 vlan_id);
4876 return -1;
4877 }
4878 group->num_setup_iface--;
4879
4880 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4881 ret = -1;
4882
4883 if (group->references > 1) {
4884 wpa_printf(MSG_DEBUG,
4885 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4886 vlan_id);
4887 ret = -2;
4888 }
4889
4890 wpa_group_put(wpa_auth, group);
4891
4892 return ret;
4893}
4894
4895
6fc6879b
JM
4896int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
4897{
4898 struct wpa_group *group;
4899
4900 if (sm == NULL || sm->wpa_auth == NULL)
4901 return 0;
4902
4903 group = sm->wpa_auth->group;
4904 while (group) {
4905 if (group->vlan_id == vlan_id)
4906 break;
4907 group = group->next;
4908 }
4909
4910 if (group == NULL) {
4911 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
4912 if (group == NULL)
4913 return -1;
4914 }
4915
4916 if (sm->group == group)
4917 return 0;
4918
7d7f7be2
JM
4919 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4920 return -1;
4921
6fc6879b
JM
4922 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
4923 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
4924
a0ad9e8c
MB
4925 wpa_group_get(sm->wpa_auth, group);
4926 wpa_group_put(sm->wpa_auth, sm->group);
6fc6879b 4927 sm->group = group;
a0ad9e8c 4928
6fc6879b
JM
4929 return 0;
4930}
e4bf4db9
JM
4931
4932
4933void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
4934 struct wpa_state_machine *sm, int ack)
4935{
4936 if (wpa_auth == NULL || sm == NULL)
4937 return;
4938 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
4939 " ack=%d", MAC2STR(sm->addr), ack);
4940 if (sm->pending_1_of_4_timeout && ack) {
4941 /*
4942 * Some deployed supplicant implementations update their SNonce
4943 * for each EAPOL-Key 2/4 message even within the same 4-way
4944 * handshake and then fail to use the first SNonce when
4945 * deriving the PTK. This results in unsuccessful 4-way
4946 * handshake whenever the relatively short initial timeout is
4947 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4948 * around this by increasing the timeout now that we know that
4949 * the station has received the frame.
4950 */
4951 int timeout_ms = eapol_key_timeout_subseq;
4952 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
4953 "timeout by %u ms because of acknowledged frame",
4954 timeout_ms);
4955 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
4956 eloop_register_timeout(timeout_ms / 1000,
4957 (timeout_ms % 1000) * 1000,
4958 wpa_send_eapol_timeout, wpa_auth, sm);
4959 }
3d695328
JM
4960
4961#ifdef CONFIG_TESTING_OPTIONS
4962 if (sm->eapol_status_cb) {
4963 sm->eapol_status_cb(sm->eapol_status_cb_ctx1,
4964 sm->eapol_status_cb_ctx2);
4965 sm->eapol_status_cb = NULL;
4966 }
4967#endif /* CONFIG_TESTING_OPTIONS */
e4bf4db9 4968}
c10347f2
JM
4969
4970
4971int wpa_auth_uses_sae(struct wpa_state_machine *sm)
4972{
4973 if (sm == NULL)
4974 return 0;
4975 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
4976}
aa189ac9
JM
4977
4978
4979int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
4980{
4981 if (sm == NULL)
4982 return 0;
4983 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
4984}
25ef8529
JM
4985
4986
4987#ifdef CONFIG_P2P
4988int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
4989{
4990 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
4991 return -1;
4992 os_memcpy(addr, sm->ip_addr, 4);
4993 return 0;
4994}
4995#endif /* CONFIG_P2P */
cbc210de
JM
4996
4997
4998int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
4999 struct radius_das_attrs *attr)
5000{
5001 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
5002}
34782730
JM
5003
5004
5005void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
5006{
5007 struct wpa_group *group;
5008
5009 if (!wpa_auth)
5010 return;
5011 for (group = wpa_auth->group; group; group = group->next)
5012 wpa_group_config_group_keys(wpa_auth, group);
5013}
7eace378
JM
5014
5015
5016#ifdef CONFIG_FILS
5017
5018struct wpa_auth_fils_iter_data {
5019 struct wpa_authenticator *auth;
5020 const u8 *cache_id;
5021 struct rsn_pmksa_cache_entry *pmksa;
5022 const u8 *spa;
5023 const u8 *pmkid;
5024};
5025
5026
5027static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
5028{
5029 struct wpa_auth_fils_iter_data *data = ctx;
5030
4cc6574d 5031 if (a == data->auth || !a->conf.fils_cache_id_set ||
7eace378
JM
5032 os_memcmp(a->conf.fils_cache_id, data->cache_id,
5033 FILS_CACHE_ID_LEN) != 0)
5034 return 0;
5035 data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
5036 return data->pmksa != NULL;
5037}
5038
5039
5040struct rsn_pmksa_cache_entry *
5041wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
5042 const u8 *sta_addr, const u8 *pmkid)
5043{
5044 struct wpa_auth_fils_iter_data idata;
5045
5046 if (!wpa_auth->conf.fils_cache_id_set)
5047 return NULL;
5048 idata.auth = wpa_auth;
5049 idata.cache_id = wpa_auth->conf.fils_cache_id;
5050 idata.pmksa = NULL;
5051 idata.spa = sta_addr;
5052 idata.pmkid = pmkid;
5053 wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
5054 return idata.pmksa;
5055}
5056
5db997e3
JM
5057
5058#ifdef CONFIG_IEEE80211R_AP
a7968ea5
JM
5059int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, int use_sha384,
5060 u8 *buf, size_t len)
5db997e3
JM
5061{
5062 struct wpa_auth_config *conf = &wpa_auth->conf;
5063
a7968ea5 5064 return wpa_write_ftie(conf, use_sha384, conf->r0_key_holder,
5db997e3
JM
5065 conf->r0_key_holder_len,
5066 NULL, NULL, buf, len, NULL, 0);
5067}
5068#endif /* CONFIG_IEEE80211R_AP */
5069
8acbf85f
JM
5070
5071void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm,
5072 u8 *fils_anonce, u8 *fils_snonce,
5073 u8 *fils_kek, size_t *fils_kek_len)
5074{
5075 os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN);
5076 os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN);
5077 os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN);
5078 *fils_kek_len = sm->PTK.kek_len;
5079}
5080
a40bd06e
JM
5081
5082void wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine *sm, const u8 *pmk,
5083 size_t pmk_len, const u8 *pmkid)
5084{
5085 os_memcpy(sm->PMK, pmk, pmk_len);
5086 sm->pmk_len = pmk_len;
5087 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
5088 sm->pmkid_set = 1;
5089}
5090
7eace378 5091#endif /* CONFIG_FILS */
6bc2f00f
JM
5092
5093
36536639
JM
5094void wpa_auth_set_auth_alg(struct wpa_state_machine *sm, u16 auth_alg)
5095{
5096 if (sm)
5097 sm->auth_alg = auth_alg;
5098}
5099
5100
10ec6a5f
JM
5101#ifdef CONFIG_DPP2
5102void wpa_auth_set_dpp_z(struct wpa_state_machine *sm, const struct wpabuf *z)
5103{
5104 if (sm) {
5105 wpabuf_clear_free(sm->dpp_z);
5106 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
5107 }
5108}
5109#endif /* CONFIG_DPP2 */
5110
5111
82424732 5112#ifdef CONFIG_TESTING_OPTIONS
d8afdb21 5113
3d695328
JM
5114int wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce,
5115 void (*cb)(void *ctx1, void *ctx2),
5116 void *ctx1, void *ctx2)
d8afdb21
JM
5117{
5118 const u8 *anonce = sm->ANonce;
5119 u8 anonce_buf[WPA_NONCE_LEN];
5120
5121 if (change_anonce) {
5122 if (random_get_bytes(anonce_buf, WPA_NONCE_LEN))
5123 return -1;
5124 anonce = anonce_buf;
5125 }
5126
5127 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5128 "sending 1/4 msg of 4-Way Handshake (TESTING)");
5129 wpa_send_eapol(sm->wpa_auth, sm,
5130 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
5131 anonce, NULL, 0, 0, 0);
5132 return 0;
5133}
5134
5135
3d695328
JM
5136int wpa_auth_resend_m3(struct wpa_state_machine *sm,
5137 void (*cb)(void *ctx1, void *ctx2),
5138 void *ctx1, void *ctx2)
d8afdb21 5139{
0c3bc1be 5140 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
0c3bc1be 5141 u8 *opos;
d8afdb21
JM
5142 size_t gtk_len, kde_len;
5143 struct wpa_group *gsm = sm->group;
5144 u8 *wpa_ie;
7a4b01c8 5145 int wpa_ie_len, secure, gtkidx, encr = 0;
d8afdb21
JM
5146
5147 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
16889aff 5148 GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2])
d8afdb21
JM
5149 */
5150
5151 /* Use 0 RSC */
5152 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
5153 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
5154 wpa_ie = sm->wpa_auth->wpa_ie;
5155 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
5156 if (sm->wpa == WPA_VERSION_WPA &&
5157 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
5158 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
5159 /* WPA-only STA, remove RSN IE and possible MDIE */
5160 wpa_ie = wpa_ie + wpa_ie[1] + 2;
5161 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
5162 wpa_ie = wpa_ie + wpa_ie[1] + 2;
5163 wpa_ie_len = wpa_ie[1] + 2;
5164 }
5165 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5166 "sending 3/4 msg of 4-Way Handshake (TESTING)");
5167 if (sm->wpa == WPA_VERSION_WPA2) {
5168 /* WPA2 send GTK in the 4-way handshake */
5169 secure = 1;
5170 gtk = gsm->GTK[gsm->GN - 1];
5171 gtk_len = gsm->GTK_len;
7a4b01c8 5172 gtkidx = gsm->GN;
d8afdb21
JM
5173 _rsc = rsc;
5174 encr = 1;
5175 } else {
5176 /* WPA does not include GTK in msg 3/4 */
5177 secure = 0;
5178 gtk = NULL;
5179 gtk_len = 0;
d8afdb21
JM
5180 _rsc = NULL;
5181 if (sm->rx_eapol_key_secure) {
5182 /*
5183 * It looks like Windows 7 supplicant tries to use
5184 * Secure bit in msg 2/4 after having reported Michael
5185 * MIC failure and it then rejects the 4-way handshake
5186 * if msg 3/4 does not set Secure bit. Work around this
5187 * by setting the Secure bit here even in the case of
5188 * WPA if the supplicant used it first.
5189 */
5190 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5191 "STA used Secure bit in WPA msg 2/4 - "
5192 "set Secure for 3/4 as workaround");
5193 secure = 1;
5194 }
5195 }
5196
17c2559c 5197 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
d8afdb21
JM
5198 if (gtk)
5199 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
5200#ifdef CONFIG_IEEE80211R_AP
5201 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5202 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
5203 kde_len += 300; /* FTIE + 2 * TIE */
5204 }
5205#endif /* CONFIG_IEEE80211R_AP */
5206 kde = os_malloc(kde_len);
5207 if (kde == NULL)
5208 return -1;
5209
5210 pos = kde;
5211 os_memcpy(pos, wpa_ie, wpa_ie_len);
5212 pos += wpa_ie_len;
5213#ifdef CONFIG_IEEE80211R_AP
5214 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5215 int res;
5216 size_t elen;
5217
5218 elen = pos - kde;
5219 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
5220 if (res < 0) {
5221 wpa_printf(MSG_ERROR, "FT: Failed to insert "
5222 "PMKR1Name into RSN IE in EAPOL-Key data");
5223 os_free(kde);
5224 return -1;
5225 }
5226 pos -= wpa_ie_len;
5227 pos += elen;
5228 }
5229#endif /* CONFIG_IEEE80211R_AP */
5230 if (gtk) {
5231 u8 hdr[2];
7a4b01c8 5232 hdr[0] = gtkidx & 0x03;
d8afdb21
JM
5233 hdr[1] = 0;
5234 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
5235 gtk, gtk_len);
5236 }
5237 opos = pos;
5238 pos = ieee80211w_kde_add(sm, pos);
9f917339
MV
5239 if (pos - opos >= 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
5240 /* skip KDE header and keyid */
5241 opos += 2 + RSN_SELECTOR_LEN + 2;
d8afdb21
JM
5242 os_memset(opos, 0, 6); /* clear PN */
5243 }
17c2559c
MV
5244 if (ocv_oci_add(sm, &pos) < 0) {
5245 os_free(kde);
5246 return -1;
5247 }
d8afdb21
JM
5248
5249#ifdef CONFIG_IEEE80211R_AP
5250 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5251 int res;
5252 struct wpa_auth_config *conf;
5253
5254 conf = &sm->wpa_auth->conf;
5255 if (sm->assoc_resp_ftie &&
5256 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
5257 os_memcpy(pos, sm->assoc_resp_ftie,
5258 2 + sm->assoc_resp_ftie[1]);
5259 res = 2 + sm->assoc_resp_ftie[1];
5260 } else {
a7968ea5
JM
5261 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
5262
5263 res = wpa_write_ftie(conf, use_sha384,
5264 conf->r0_key_holder,
d8afdb21
JM
5265 conf->r0_key_holder_len,
5266 NULL, NULL, pos,
5267 kde + kde_len - pos,
5268 NULL, 0);
5269 }
5270 if (res < 0) {
5271 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
5272 "into EAPOL-Key Key Data");
5273 os_free(kde);
5274 return -1;
5275 }
5276 pos += res;
5277
5278 /* TIE[ReassociationDeadline] (TU) */
5279 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
5280 *pos++ = 5;
5281 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
5282 WPA_PUT_LE32(pos, conf->reassociation_deadline);
5283 pos += 4;
5284
5285 /* TIE[KeyLifetime] (seconds) */
5286 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
5287 *pos++ = 5;
5288 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
83fe4bd3 5289 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
d8afdb21
JM
5290 pos += 4;
5291 }
5292#endif /* CONFIG_IEEE80211R_AP */
5293
5294 wpa_send_eapol(sm->wpa_auth, sm,
5295 (secure ? WPA_KEY_INFO_SECURE : 0) |
5296 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5297 WPA_KEY_INFO_MIC : 0) |
5298 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
5299 WPA_KEY_INFO_KEY_TYPE,
7a4b01c8 5300 _rsc, sm->ANonce, kde, pos - kde, 0, encr);
d8afdb21
JM
5301 os_free(kde);
5302 return 0;
5303}
5304
5305
3d695328
JM
5306int wpa_auth_resend_group_m1(struct wpa_state_machine *sm,
5307 void (*cb)(void *ctx1, void *ctx2),
5308 void *ctx1, void *ctx2)
6bc2f00f
JM
5309{
5310 u8 rsc[WPA_KEY_RSC_LEN];
5311 struct wpa_group *gsm = sm->group;
5312 const u8 *kde;
0c3bc1be 5313 u8 *kde_buf = NULL, *pos, hdr[2];
0c3bc1be 5314 u8 *opos;
6bc2f00f
JM
5315 size_t kde_len;
5316 u8 *gtk;
5317
5318 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
5319 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
5320 /* Use 0 RSC */
5321 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5322 "sending 1/2 msg of Group Key Handshake (TESTING)");
5323
5324 gtk = gsm->GTK[gsm->GN - 1];
5325 if (sm->wpa == WPA_VERSION_WPA2) {
5326 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
17c2559c 5327 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
6bc2f00f
JM
5328 kde_buf = os_malloc(kde_len);
5329 if (kde_buf == NULL)
5330 return -1;
5331
5332 kde = pos = kde_buf;
5333 hdr[0] = gsm->GN & 0x03;
5334 hdr[1] = 0;
5335 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
5336 gtk, gsm->GTK_len);
5337 opos = pos;
5338 pos = ieee80211w_kde_add(sm, pos);
9f917339
MV
5339 if (pos - opos >=
5340 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
5341 /* skip KDE header and keyid */
5342 opos += 2 + RSN_SELECTOR_LEN + 2;
6bc2f00f
JM
5343 os_memset(opos, 0, 6); /* clear PN */
5344 }
17c2559c
MV
5345 if (ocv_oci_add(sm, &pos) < 0) {
5346 os_free(kde_buf);
5347 return -1;
5348 }
6bc2f00f
JM
5349 kde_len = pos - kde;
5350 } else {
5351 kde = gtk;
5352 kde_len = gsm->GTK_len;
5353 }
5354
3d695328
JM
5355 sm->eapol_status_cb = cb;
5356 sm->eapol_status_cb_ctx1 = ctx1;
5357 sm->eapol_status_cb_ctx2 = ctx2;
5358
6bc2f00f
JM
5359 wpa_send_eapol(sm->wpa_auth, sm,
5360 WPA_KEY_INFO_SECURE |
5361 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5362 WPA_KEY_INFO_MIC : 0) |
5363 WPA_KEY_INFO_ACK |
5364 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
5365 rsc, NULL, kde, kde_len, gsm->GN, 1);
5366
5367 os_free(kde_buf);
5368 return 0;
5369}
d8afdb21 5370
92662fb2
JB
5371
5372int wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth)
5373{
5374 if (!wpa_auth)
5375 return -1;
5376 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
5377 return eloop_register_timeout(0, 0, wpa_rekey_gtk, wpa_auth, NULL);
5378}
5379
6bc2f00f 5380#endif /* CONFIG_TESTING_OPTIONS */