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