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