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