]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/wpa_auth.c
SAE: Enhance AP implementation to handle auth for mesh interfaces
[thirdparty/hostap.git] / src / ap / wpa_auth.c
CommitLineData
6fc6879b 1/*
00338a4f 2 * IEEE 802.11 RSN / WPA Authenticator
94ddef3e 3 * Copyright (c) 2004-2013, 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"
03da66bd
JM
16#include "crypto/aes_wrap.h"
17#include "crypto/crypto.h"
18#include "crypto/sha1.h"
19#include "crypto/sha256.h"
3642c431 20#include "crypto/random.h"
281c950b 21#include "eapol_auth/eapol_auth_sm.h"
6226e38d 22#include "ap_config.h"
6fc6879b 23#include "ieee802_11.h"
6226e38d
JM
24#include "wpa_auth.h"
25#include "pmksa_cache_auth.h"
6fc6879b
JM
26#include "wpa_auth_i.h"
27#include "wpa_auth_ie.h"
28
29#define STATE_MACHINE_DATA struct wpa_state_machine
30#define STATE_MACHINE_DEBUG_PREFIX "WPA"
31#define STATE_MACHINE_ADDR sm->addr
32
33
34static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
e4a6ea1d 35static int wpa_sm_step(struct wpa_state_machine *sm);
929a2ea5
JM
36static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
37 size_t data_len);
6fc6879b
JM
38static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
39static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
40 struct wpa_group *group);
581a8cde 41static void wpa_request_new_ptk(struct wpa_state_machine *sm);
e6965d4e
JM
42static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
43 struct wpa_group *group);
1bdb7ab3
JM
44static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
45 struct wpa_group *group);
6fc6879b 46
bae61562
JM
47static const u32 dot11RSNAConfigGroupUpdateCount = 4;
48static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
49static const u32 eapol_key_timeout_first = 100; /* ms */
50static const u32 eapol_key_timeout_subseq = 1000; /* ms */
00338a4f 51static const u32 eapol_key_timeout_first_group = 500; /* ms */
6fc6879b
JM
52
53/* TODO: make these configurable */
54static const int dot11RSNAConfigPMKLifetime = 43200;
55static const int dot11RSNAConfigPMKReauthThreshold = 70;
56static const int dot11RSNAConfigSATimeout = 60;
57
58
c772d054 59static inline int wpa_auth_mic_failure_report(
6fc6879b
JM
60 struct wpa_authenticator *wpa_auth, const u8 *addr)
61{
62 if (wpa_auth->cb.mic_failure_report)
c772d054
JM
63 return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
64 return 0;
6fc6879b
JM
65}
66
67
68static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
69 const u8 *addr, wpa_eapol_variable var,
70 int value)
71{
72 if (wpa_auth->cb.set_eapol)
73 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
74}
75
76
77static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
78 const u8 *addr, wpa_eapol_variable var)
79{
80 if (wpa_auth->cb.get_eapol == NULL)
81 return -1;
82 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
83}
84
85
86static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
759fd76b
JM
87 const u8 *addr,
88 const u8 *p2p_dev_addr,
89 const u8 *prev_psk)
6fc6879b
JM
90{
91 if (wpa_auth->cb.get_psk == NULL)
92 return NULL;
759fd76b
JM
93 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, p2p_dev_addr,
94 prev_psk);
6fc6879b
JM
95}
96
97
98static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
99 const u8 *addr, u8 *msk, size_t *len)
100{
101 if (wpa_auth->cb.get_msk == NULL)
102 return -1;
103 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
104}
105
106
107static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
108 int vlan_id,
71934751 109 enum wpa_alg alg, const u8 *addr, int idx,
6fc6879b
JM
110 u8 *key, size_t key_len)
111{
112 if (wpa_auth->cb.set_key == NULL)
113 return -1;
114 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
115 key, key_len);
116}
117
118
119static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
120 const u8 *addr, int idx, u8 *seq)
121{
122 if (wpa_auth->cb.get_seqnum == NULL)
123 return -1;
124 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
125}
126
127
6fc6879b
JM
128static inline int
129wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
130 const u8 *data, size_t data_len, int encrypt)
131{
132 if (wpa_auth->cb.send_eapol == NULL)
133 return -1;
134 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
135 encrypt);
136}
137
138
139int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
140 int (*cb)(struct wpa_state_machine *sm, void *ctx),
141 void *cb_ctx)
142{
143 if (wpa_auth->cb.for_each_sta == NULL)
144 return 0;
145 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
146}
147
148
bf98f7f3
JM
149int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
150 int (*cb)(struct wpa_authenticator *a, void *ctx),
151 void *cb_ctx)
152{
153 if (wpa_auth->cb.for_each_auth == NULL)
154 return 0;
155 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
156}
157
158
6fc6879b
JM
159void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
160 logger_level level, const char *txt)
161{
162 if (wpa_auth->cb.logger == NULL)
163 return;
164 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
165}
166
167
168void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
169 logger_level level, const char *fmt, ...)
170{
171 char *format;
172 int maxlen;
173 va_list ap;
174
175 if (wpa_auth->cb.logger == NULL)
176 return;
177
178 maxlen = os_strlen(fmt) + 100;
179 format = os_malloc(maxlen);
180 if (!format)
181 return;
182
183 va_start(ap, fmt);
184 vsnprintf(format, maxlen, fmt, ap);
185 va_end(ap);
186
187 wpa_auth_logger(wpa_auth, addr, level, format);
188
189 os_free(format);
190}
191
192
193static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
194 const u8 *addr)
195{
196 if (wpa_auth->cb.disconnect == NULL)
197 return;
afcc9ea1 198 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
6fc6879b
JM
199 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
200 WLAN_REASON_PREV_AUTH_NOT_VALID);
201}
202
203
204static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
205{
56586197 206 int ret = 0;
6fc6879b 207#ifdef CONFIG_IEEE80211R
56586197
JM
208 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
209 ret = 1;
6fc6879b 210#endif /* CONFIG_IEEE80211R */
56586197
JM
211#ifdef CONFIG_IEEE80211W
212 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
213 ret = 1;
214#endif /* CONFIG_IEEE80211W */
a14896e8
JM
215 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
216 ret = 1;
56586197 217 return ret;
6fc6879b
JM
218}
219
220
221static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
222{
223 struct wpa_authenticator *wpa_auth = eloop_ctx;
224
3642c431 225 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
6fc6879b
JM
226 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
227 "initialization.");
228 } else {
229 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
5cb9d5c3
JM
230 wpa_hexdump_key(MSG_DEBUG, "GMK",
231 wpa_auth->group->GMK, WPA_GMK_LEN);
6fc6879b
JM
232 }
233
234 if (wpa_auth->conf.wpa_gmk_rekey) {
235 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
236 wpa_rekey_gmk, wpa_auth, NULL);
237 }
238}
239
240
241static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
242{
243 struct wpa_authenticator *wpa_auth = eloop_ctx;
244 struct wpa_group *group;
245
246 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
247 for (group = wpa_auth->group; group; group = group->next) {
248 group->GTKReKey = TRUE;
249 do {
250 group->changed = FALSE;
251 wpa_group_sm_step(wpa_auth, group);
252 } while (group->changed);
253 }
254
255 if (wpa_auth->conf.wpa_group_rekey) {
256 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
257 0, wpa_rekey_gtk, wpa_auth, NULL);
258 }
259}
260
261
581a8cde
JM
262static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
263{
264 struct wpa_authenticator *wpa_auth = eloop_ctx;
265 struct wpa_state_machine *sm = timeout_ctx;
266
267 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
268 wpa_request_new_ptk(sm);
269 wpa_sm_step(sm);
270}
271
272
6fc6879b
JM
273static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
274{
275 if (sm->pmksa == ctx)
276 sm->pmksa = NULL;
277 return 0;
278}
279
280
281static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
282 void *ctx)
283{
284 struct wpa_authenticator *wpa_auth = ctx;
285 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
286}
287
288
1bdb7ab3
JM
289static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
290 struct wpa_group *group)
291{
559cdabb 292 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
1bdb7ab3 293 u8 rkey[32];
559cdabb 294 unsigned long ptr;
1bdb7ab3 295
3642c431 296 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
1bdb7ab3
JM
297 return -1;
298 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
299
300 /*
301 * Counter = PRF-256(Random number, "Init Counter",
302 * Local MAC Address || Time)
303 */
304 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
305 wpa_get_ntp_timestamp(buf + ETH_ALEN);
559cdabb
JM
306 ptr = (unsigned long) group;
307 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
3642c431 308 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
1bdb7ab3
JM
309 return -1;
310
311 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
312 group->Counter, WPA_NONCE_LEN) < 0)
313 return -1;
314 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
315 group->Counter, WPA_NONCE_LEN);
316
317 return 0;
318}
319
320
e6965d4e 321static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
bdffdc5d 322 int vlan_id, int delay_init)
e6965d4e
JM
323{
324 struct wpa_group *group;
e6965d4e
JM
325
326 group = os_zalloc(sizeof(struct wpa_group));
327 if (group == NULL)
328 return NULL;
329
330 group->GTKAuthenticator = TRUE;
331 group->vlan_id = vlan_id;
c3550295 332 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
6fc6879b 333
08704cd8
JM
334 if (random_pool_ready() != 1) {
335 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
336 "for secure operations - update keys later when "
337 "the first station connects");
338 }
339
1bdb7ab3
JM
340 /*
341 * Set initial GMK/Counter value here. The actual values that will be
342 * used in negotiations will be set once the first station tries to
343 * connect. This allows more time for collecting additional randomness
344 * on embedded devices.
6fc6879b 345 */
1bdb7ab3 346 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
6fc6879b
JM
347 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
348 "initialization.");
349 os_free(group);
350 return NULL;
351 }
6fc6879b
JM
352
353 group->GInit = TRUE;
bdffdc5d
JM
354 if (delay_init) {
355 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
356 "until Beacon frames have been configured");
357 /* Initialization is completed in wpa_init_keys(). */
358 } else {
359 wpa_group_sm_step(wpa_auth, group);
360 group->GInit = FALSE;
361 wpa_group_sm_step(wpa_auth, group);
362 }
6fc6879b
JM
363
364 return group;
365}
366
367
368/**
369 * wpa_init - Initialize WPA authenticator
370 * @addr: Authenticator address
371 * @conf: Configuration for WPA authenticator
a17df5fb 372 * @cb: Callback functions for WPA authenticator
6fc6879b
JM
373 * Returns: Pointer to WPA authenticator data or %NULL on failure
374 */
375struct wpa_authenticator * wpa_init(const u8 *addr,
376 struct wpa_auth_config *conf,
377 struct wpa_auth_callbacks *cb)
378{
379 struct wpa_authenticator *wpa_auth;
380
381 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
382 if (wpa_auth == NULL)
383 return NULL;
384 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
385 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
386 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
387
388 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
389 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
390 os_free(wpa_auth);
391 return NULL;
392 }
393
bdffdc5d 394 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
6fc6879b
JM
395 if (wpa_auth->group == NULL) {
396 os_free(wpa_auth->wpa_ie);
397 os_free(wpa_auth);
398 return NULL;
399 }
400
4bb081f1
JM
401 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
402 wpa_auth);
6fc6879b
JM
403 if (wpa_auth->pmksa == NULL) {
404 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
405 os_free(wpa_auth->wpa_ie);
406 os_free(wpa_auth);
407 return NULL;
408 }
409
410#ifdef CONFIG_IEEE80211R
411 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
412 if (wpa_auth->ft_pmk_cache == NULL) {
413 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
414 os_free(wpa_auth->wpa_ie);
4bb081f1 415 pmksa_cache_auth_deinit(wpa_auth->pmksa);
6fc6879b
JM
416 os_free(wpa_auth);
417 return NULL;
418 }
419#endif /* CONFIG_IEEE80211R */
420
421 if (wpa_auth->conf.wpa_gmk_rekey) {
422 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
423 wpa_rekey_gmk, wpa_auth, NULL);
424 }
425
426 if (wpa_auth->conf.wpa_group_rekey) {
427 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
428 wpa_rekey_gtk, wpa_auth, NULL);
429 }
430
25ef8529
JM
431#ifdef CONFIG_P2P
432 if (WPA_GET_BE32(conf->ip_addr_start)) {
433 int count = WPA_GET_BE32(conf->ip_addr_end) -
434 WPA_GET_BE32(conf->ip_addr_start) + 1;
435 if (count > 1000)
436 count = 1000;
437 if (count > 0)
438 wpa_auth->ip_pool = bitfield_alloc(count);
439 }
440#endif /* CONFIG_P2P */
441
6fc6879b
JM
442 return wpa_auth;
443}
444
445
bdffdc5d
JM
446int wpa_init_keys(struct wpa_authenticator *wpa_auth)
447{
448 struct wpa_group *group = wpa_auth->group;
449
450 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
451 "keys");
452 wpa_group_sm_step(wpa_auth, group);
453 group->GInit = FALSE;
454 wpa_group_sm_step(wpa_auth, group);
7d7f7be2
JM
455 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
456 return -1;
bdffdc5d
JM
457 return 0;
458}
459
460
6fc6879b
JM
461/**
462 * wpa_deinit - Deinitialize WPA authenticator
463 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
464 */
465void wpa_deinit(struct wpa_authenticator *wpa_auth)
466{
467 struct wpa_group *group, *prev;
468
469 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
470 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
471
472#ifdef CONFIG_PEERKEY
473 while (wpa_auth->stsl_negotiations)
474 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
475#endif /* CONFIG_PEERKEY */
476
4bb081f1 477 pmksa_cache_auth_deinit(wpa_auth->pmksa);
6fc6879b
JM
478
479#ifdef CONFIG_IEEE80211R
480 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
481 wpa_auth->ft_pmk_cache = NULL;
482#endif /* CONFIG_IEEE80211R */
483
25ef8529
JM
484#ifdef CONFIG_P2P
485 bitfield_free(wpa_auth->ip_pool);
486#endif /* CONFIG_P2P */
487
488
6fc6879b
JM
489 os_free(wpa_auth->wpa_ie);
490
491 group = wpa_auth->group;
492 while (group) {
493 prev = group;
494 group = group->next;
495 os_free(prev);
496 }
497
498 os_free(wpa_auth);
499}
500
501
502/**
503 * wpa_reconfig - Update WPA authenticator configuration
504 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
505 * @conf: Configuration for WPA authenticator
506 */
507int wpa_reconfig(struct wpa_authenticator *wpa_auth,
508 struct wpa_auth_config *conf)
509{
e6965d4e 510 struct wpa_group *group;
6fc6879b
JM
511 if (wpa_auth == NULL)
512 return 0;
513
514 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
ad08c363
JM
515 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
516 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
517 return -1;
518 }
519
e6965d4e
JM
520 /*
521 * Reinitialize GTK to make sure it is suitable for the new
522 * configuration.
523 */
524 group = wpa_auth->group;
c3550295 525 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
e6965d4e
JM
526 group->GInit = TRUE;
527 wpa_group_sm_step(wpa_auth, group);
528 group->GInit = FALSE;
529 wpa_group_sm_step(wpa_auth, group);
530
6fc6879b
JM
531 return 0;
532}
533
534
535struct wpa_state_machine *
94ddef3e
JM
536wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
537 const u8 *p2p_dev_addr)
6fc6879b
JM
538{
539 struct wpa_state_machine *sm;
540
7d7f7be2
JM
541 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
542 return NULL;
543
6fc6879b
JM
544 sm = os_zalloc(sizeof(struct wpa_state_machine));
545 if (sm == NULL)
546 return NULL;
547 os_memcpy(sm->addr, addr, ETH_ALEN);
94ddef3e
JM
548 if (p2p_dev_addr)
549 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
6fc6879b
JM
550
551 sm->wpa_auth = wpa_auth;
552 sm->group = wpa_auth->group;
553
554 return sm;
555}
556
557
6f9b5d16
JM
558int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
559 struct wpa_state_machine *sm)
6fc6879b
JM
560{
561 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
6f9b5d16 562 return -1;
6fc6879b
JM
563
564#ifdef CONFIG_IEEE80211R
565 if (sm->ft_completed) {
566 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
567 "FT authentication already completed - do not "
568 "start 4-way handshake");
3d4d2348
JM
569 /* Go to PTKINITDONE state to allow GTK rekeying */
570 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
6f9b5d16 571 return 0;
6fc6879b
JM
572 }
573#endif /* CONFIG_IEEE80211R */
574
575 if (sm->started) {
22a299ee 576 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
6fc6879b 577 sm->ReAuthenticationRequest = TRUE;
6f9b5d16 578 return wpa_sm_step(sm);
6fc6879b
JM
579 }
580
581 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
582 "start authentication");
583 sm->started = 1;
584
585 sm->Init = TRUE;
e4a6ea1d 586 if (wpa_sm_step(sm) == 1)
6f9b5d16 587 return 1; /* should not really happen */
6fc6879b
JM
588 sm->Init = FALSE;
589 sm->AuthenticationRequest = TRUE;
6f9b5d16 590 return wpa_sm_step(sm);
6fc6879b
JM
591}
592
593
a8d05fca
JM
594void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
595{
596 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
597 * reassociates back to the same AP while the previous entry for the
598 * STA has not yet been removed. */
599 if (sm == NULL)
600 return;
601
602 sm->wpa_key_mgmt = 0;
603}
604
605
6fc6879b
JM
606static void wpa_free_sta_sm(struct wpa_state_machine *sm)
607{
25ef8529
JM
608#ifdef CONFIG_P2P
609 if (WPA_GET_BE32(sm->ip_addr)) {
610 u32 start;
611 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
612 "address %u.%u.%u.%u from " MACSTR,
613 sm->ip_addr[0], sm->ip_addr[1],
614 sm->ip_addr[2], sm->ip_addr[3],
615 MAC2STR(sm->addr));
616 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
617 bitfield_clear(sm->wpa_auth->ip_pool,
618 WPA_GET_BE32(sm->ip_addr) - start);
619 }
620#endif /* CONFIG_P2P */
2ade8ef2
JM
621 if (sm->GUpdateStationKeys) {
622 sm->group->GKeyDoneStations--;
623 sm->GUpdateStationKeys = FALSE;
624 }
0f857f43
JM
625#ifdef CONFIG_IEEE80211R
626 os_free(sm->assoc_resp_ftie);
692ec305 627 wpabuf_free(sm->ft_pending_req_ies);
0f857f43 628#endif /* CONFIG_IEEE80211R */
6fc6879b
JM
629 os_free(sm->last_rx_eapol_key);
630 os_free(sm->wpa_ie);
631 os_free(sm);
632}
633
634
635void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
636{
637 if (sm == NULL)
638 return;
639
640 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
641 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
642 "strict rekeying - force GTK rekey since STA "
643 "is leaving");
644 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
645 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
646 NULL);
647 }
648
649 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
e4bf4db9 650 sm->pending_1_of_4_timeout = 0;
6fc6879b 651 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
581a8cde 652 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
6fc6879b
JM
653 if (sm->in_step_loop) {
654 /* Must not free state machine while wpa_sm_step() is running.
655 * Freeing will be completed in the end of wpa_sm_step(). */
656 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
657 "machine deinit for " MACSTR, MAC2STR(sm->addr));
658 sm->pending_deinit = 1;
659 } else
660 wpa_free_sta_sm(sm);
661}
662
663
664static void wpa_request_new_ptk(struct wpa_state_machine *sm)
665{
666 if (sm == NULL)
667 return;
668
669 sm->PTKRequest = TRUE;
670 sm->PTK_valid = 0;
671}
672
673
68921e24 674static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
22a299ee
JM
675 const u8 *replay_counter)
676{
677 int i;
678 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
68921e24 679 if (!ctr[i].valid)
22a299ee 680 break;
68921e24 681 if (os_memcmp(replay_counter, ctr[i].counter,
22a299ee
JM
682 WPA_REPLAY_COUNTER_LEN) == 0)
683 return 1;
684 }
685 return 0;
686}
687
688
68921e24
JM
689static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
690 const u8 *replay_counter)
691{
692 int i;
693 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
694 if (ctr[i].valid &&
695 (replay_counter == NULL ||
696 os_memcmp(replay_counter, ctr[i].counter,
697 WPA_REPLAY_COUNTER_LEN) == 0))
698 ctr[i].valid = FALSE;
699 }
700}
701
702
0f857f43
JM
703#ifdef CONFIG_IEEE80211R
704static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
705 struct wpa_state_machine *sm,
706 struct wpa_eapol_ie_parse *kde)
707{
708 struct wpa_ie_data ie;
709 struct rsn_mdie *mdie;
710
711 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
712 ie.num_pmkid != 1 || ie.pmkid == NULL) {
713 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
714 "FT 4-way handshake message 2/4");
715 return -1;
716 }
717
718 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
719 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
720 sm->sup_pmk_r1_name, PMKID_LEN);
721
722 if (!kde->mdie || !kde->ftie) {
723 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
724 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
725 return -1;
726 }
727
728 mdie = (struct rsn_mdie *) (kde->mdie + 2);
729 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
730 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
731 MOBILITY_DOMAIN_ID_LEN) != 0) {
732 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
733 return -1;
734 }
735
736 if (sm->assoc_resp_ftie &&
737 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
738 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
739 2 + sm->assoc_resp_ftie[1]) != 0)) {
740 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
741 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
742 kde->ftie, kde->ftie_len);
743 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
744 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
745 return -1;
746 }
747
748 return 0;
749}
750#endif /* CONFIG_IEEE80211R */
751
752
c772d054
JM
753static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
754 struct wpa_state_machine *sm, int group)
ec027805
JM
755{
756 /* Supplicant reported a Michael MIC error */
757 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
758 "received EAPOL-Key Error Request "
759 "(STA detected Michael MIC failure (group=%d))",
760 group);
fbc72d32
JM
761
762 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
763 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
764 "ignore Michael MIC failure report since "
765 "group cipher is not TKIP");
766 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
767 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
768 "ignore Michael MIC failure report since "
769 "pairwise cipher is not TKIP");
770 } else {
c772d054
JM
771 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
772 return 1; /* STA entry was removed */
fbc72d32
JM
773 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
774 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
775 }
ec027805
JM
776
777 /*
778 * Error report is not a request for a new key handshake, but since
779 * Authenticator may do it, let's change the keys now anyway.
780 */
781 wpa_request_new_ptk(sm);
c772d054 782 return 0;
ec027805
JM
783}
784
785
6fc6879b
JM
786void wpa_receive(struct wpa_authenticator *wpa_auth,
787 struct wpa_state_machine *sm,
788 u8 *data, size_t data_len)
789{
790 struct ieee802_1x_hdr *hdr;
791 struct wpa_eapol_key *key;
792 u16 key_info, key_data_length;
793 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
794 SMK_M1, SMK_M3, SMK_ERROR } msg;
795 char *msgtxt;
796 struct wpa_eapol_ie_parse kde;
26e23750 797 int ft;
1566ec46
JM
798 const u8 *eapol_key_ie;
799 size_t eapol_key_ie_len;
6fc6879b
JM
800
801 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
802 return;
803
804 if (data_len < sizeof(*hdr) + sizeof(*key))
805 return;
806
807 hdr = (struct ieee802_1x_hdr *) data;
808 key = (struct wpa_eapol_key *) (hdr + 1);
809 key_info = WPA_GET_BE16(key->key_info);
810 key_data_length = WPA_GET_BE16(key->key_data_length);
0d442aff
JM
811 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
812 " key_info=0x%x type=%u key_data_length=%u",
813 MAC2STR(sm->addr), key_info, key->type, key_data_length);
6fc6879b
JM
814 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
815 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
816 "key_data overflow (%d > %lu)",
817 key_data_length,
818 (unsigned long) (data_len - sizeof(*hdr) -
819 sizeof(*key)));
820 return;
821 }
822
f8e96eb6 823 if (sm->wpa == WPA_VERSION_WPA2) {
74590e71
JM
824 if (key->type == EAPOL_KEY_TYPE_WPA) {
825 /*
826 * Some deployed station implementations seem to send
827 * msg 4/4 with incorrect type value in WPA2 mode.
828 */
829 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
830 "with unexpected WPA type in RSN mode");
831 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
f8e96eb6
JM
832 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
833 "unexpected type %d in RSN mode",
834 key->type);
835 return;
836 }
837 } else {
838 if (key->type != EAPOL_KEY_TYPE_WPA) {
839 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
840 "unexpected type %d in WPA mode",
841 key->type);
842 return;
843 }
844 }
845
bc8318ac
JM
846 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
847 WPA_NONCE_LEN);
848 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
849 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
850
6fc6879b
JM
851 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
852 * are set */
853
854 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
855 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
856 if (key_info & WPA_KEY_INFO_ERROR) {
857 msg = SMK_ERROR;
858 msgtxt = "SMK Error";
859 } else {
860 msg = SMK_M1;
861 msgtxt = "SMK M1";
862 }
863 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
864 msg = SMK_M3;
865 msgtxt = "SMK M3";
866 } else if (key_info & WPA_KEY_INFO_REQUEST) {
867 msg = REQUEST;
868 msgtxt = "Request";
869 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
870 msg = GROUP_2;
871 msgtxt = "2/2 Group";
872 } else if (key_data_length == 0) {
873 msg = PAIRWISE_4;
874 msgtxt = "4/4 Pairwise";
875 } else {
876 msg = PAIRWISE_2;
877 msgtxt = "2/4 Pairwise";
878 }
879
880 /* TODO: key_info type validation for PeerKey */
881 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
882 msg == GROUP_2) {
883 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
eb7719ff
JM
884 if (sm->pairwise == WPA_CIPHER_CCMP ||
885 sm->pairwise == WPA_CIPHER_GCMP) {
6fc6879b 886 if (wpa_use_aes_cmac(sm) &&
a14896e8 887 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
929a2ea5 888 !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
6fc6879b
JM
889 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
890 wpa_auth_logger(wpa_auth, sm->addr,
891 LOGGER_WARNING,
892 "advertised support for "
893 "AES-128-CMAC, but did not "
894 "use it");
895 return;
896 }
897
898 if (!wpa_use_aes_cmac(sm) &&
899 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
900 wpa_auth_logger(wpa_auth, sm->addr,
901 LOGGER_WARNING,
902 "did not use HMAC-SHA1-AES "
eb7719ff 903 "with CCMP/GCMP");
6fc6879b
JM
904 return;
905 }
906 }
929a2ea5
JM
907
908 if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
909 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
910 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
911 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
912 return;
913 }
6fc6879b
JM
914 }
915
916 if (key_info & WPA_KEY_INFO_REQUEST) {
917 if (sm->req_replay_counter_used &&
918 os_memcmp(key->replay_counter, sm->req_replay_counter,
919 WPA_REPLAY_COUNTER_LEN) <= 0) {
920 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
921 "received EAPOL-Key request with "
922 "replayed counter");
923 return;
924 }
925 }
926
927 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
68921e24 928 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
22a299ee 929 int i;
68921e24
JM
930
931 if (msg == PAIRWISE_2 &&
932 wpa_replay_counter_valid(sm->prev_key_replay,
933 key->replay_counter) &&
934 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
935 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
936 {
937 /*
938 * Some supplicant implementations (e.g., Windows XP
939 * WZC) update SNonce for each EAPOL-Key 2/4. This
940 * breaks the workaround on accepting any of the
941 * pending requests, so allow the SNonce to be updated
942 * even if we have already sent out EAPOL-Key 3/4.
943 */
944 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
945 "Process SNonce update from STA "
946 "based on retransmitted EAPOL-Key "
947 "1/4");
948 sm->update_snonce = 1;
949 wpa_replay_counter_mark_invalid(sm->prev_key_replay,
950 key->replay_counter);
951 goto continue_processing;
952 }
953
954 if (msg == PAIRWISE_2 &&
955 wpa_replay_counter_valid(sm->prev_key_replay,
956 key->replay_counter) &&
957 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
958 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
959 "ignore retransmitted EAPOL-Key %s - "
960 "SNonce did not change", msgtxt);
961 } else {
962 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
963 "received EAPOL-Key %s with "
964 "unexpected replay counter", msgtxt);
965 }
22a299ee
JM
966 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
967 if (!sm->key_replay[i].valid)
968 break;
969 wpa_hexdump(MSG_DEBUG, "pending replay counter",
970 sm->key_replay[i].counter,
971 WPA_REPLAY_COUNTER_LEN);
972 }
6fc6879b
JM
973 wpa_hexdump(MSG_DEBUG, "received replay counter",
974 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
975 return;
976 }
977
68921e24 978continue_processing:
6fc6879b
JM
979 switch (msg) {
980 case PAIRWISE_2:
981 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
68921e24
JM
982 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
983 (!sm->update_snonce ||
984 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
6fc6879b
JM
985 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
986 "received EAPOL-Key msg 2/4 in "
987 "invalid state (%d) - dropped",
988 sm->wpa_ptk_state);
989 return;
990 }
bbb921da 991 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
08704cd8
JM
992 if (sm->group->reject_4way_hs_for_entropy) {
993 /*
994 * The system did not have enough entropy to generate
995 * strong random numbers. Reject the first 4-way
996 * handshake(s) and collect some entropy based on the
997 * information from it. Once enough entropy is
998 * available, the next atempt will trigger GMK/Key
999 * Counter update and the station will be allowed to
1000 * continue.
1001 */
1002 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1003 "collect more entropy for random number "
1004 "generation");
08704cd8 1005 random_mark_pool_ready();
08704cd8
JM
1006 wpa_sta_disconnect(wpa_auth, sm->addr);
1007 return;
1008 }
1566ec46
JM
1009 if (wpa_parse_kde_ies((u8 *) (key + 1), key_data_length,
1010 &kde) < 0) {
1011 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1012 "received EAPOL-Key msg 2/4 with "
1013 "invalid Key Data contents");
1014 return;
1015 }
1016 if (kde.rsn_ie) {
1017 eapol_key_ie = kde.rsn_ie;
1018 eapol_key_ie_len = kde.rsn_ie_len;
a14896e8
JM
1019 } else if (kde.osen) {
1020 eapol_key_ie = kde.osen;
1021 eapol_key_ie_len = kde.osen_len;
1566ec46
JM
1022 } else {
1023 eapol_key_ie = kde.wpa_ie;
1024 eapol_key_ie_len = kde.wpa_ie_len;
1025 }
26e23750
JM
1026 ft = sm->wpa == WPA_VERSION_WPA2 &&
1027 wpa_key_mgmt_ft(sm->wpa_key_mgmt);
6fc6879b 1028 if (sm->wpa_ie == NULL ||
26e23750
JM
1029 wpa_compare_rsn_ie(ft,
1030 sm->wpa_ie, sm->wpa_ie_len,
1566ec46 1031 eapol_key_ie, eapol_key_ie_len)) {
6fc6879b
JM
1032 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1033 "WPA IE from (Re)AssocReq did not "
1034 "match with msg 2/4");
1035 if (sm->wpa_ie) {
1036 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
1037 sm->wpa_ie, sm->wpa_ie_len);
1038 }
1039 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
1566ec46 1040 eapol_key_ie, eapol_key_ie_len);
6fc6879b
JM
1041 /* MLME-DEAUTHENTICATE.request */
1042 wpa_sta_disconnect(wpa_auth, sm->addr);
1043 return;
1044 }
26e23750 1045#ifdef CONFIG_IEEE80211R
0f857f43
JM
1046 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
1047 wpa_sta_disconnect(wpa_auth, sm->addr);
1048 return;
26e23750
JM
1049 }
1050#endif /* CONFIG_IEEE80211R */
25ef8529
JM
1051#ifdef CONFIG_P2P
1052 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
1053 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
1054 int idx;
1055 wpa_printf(MSG_DEBUG, "P2P: IP address requested in "
1056 "EAPOL-Key exchange");
1057 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
1058 if (idx >= 0) {
1059 u32 start = WPA_GET_BE32(wpa_auth->conf.
1060 ip_addr_start);
1061 bitfield_set(wpa_auth->ip_pool, idx);
1062 WPA_PUT_BE32(sm->ip_addr, start + idx);
1063 wpa_printf(MSG_DEBUG, "P2P: Assigned IP "
1064 "address %u.%u.%u.%u to " MACSTR,
1065 sm->ip_addr[0], sm->ip_addr[1],
1066 sm->ip_addr[2], sm->ip_addr[3],
1067 MAC2STR(sm->addr));
1068 }
1069 }
1070#endif /* CONFIG_P2P */
6fc6879b
JM
1071 break;
1072 case PAIRWISE_4:
1073 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1074 !sm->PTK_valid) {
1075 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1076 "received EAPOL-Key msg 4/4 in "
1077 "invalid state (%d) - dropped",
1078 sm->wpa_ptk_state);
1079 return;
1080 }
1081 break;
1082 case GROUP_2:
1083 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1084 || !sm->PTK_valid) {
1085 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1086 "received EAPOL-Key msg 2/2 in "
1087 "invalid state (%d) - dropped",
1088 sm->wpa_ptk_group_state);
1089 return;
1090 }
1091 break;
1092#ifdef CONFIG_PEERKEY
1093 case SMK_M1:
1094 case SMK_M3:
1095 case SMK_ERROR:
1096 if (!wpa_auth->conf.peerkey) {
1097 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1098 "PeerKey use disabled - ignoring message");
1099 return;
1100 }
1101 if (!sm->PTK_valid) {
1102 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1103 "received EAPOL-Key msg SMK in "
1104 "invalid state - dropped");
1105 return;
1106 }
1107 break;
1108#else /* CONFIG_PEERKEY */
1109 case SMK_M1:
1110 case SMK_M3:
1111 case SMK_ERROR:
1112 return; /* STSL disabled - ignore SMK messages */
1113#endif /* CONFIG_PEERKEY */
1114 case REQUEST:
1115 break;
1116 }
1117
1118 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1119 "received EAPOL-Key frame (%s)", msgtxt);
1120
1121 if (key_info & WPA_KEY_INFO_ACK) {
1122 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1123 "received invalid EAPOL-Key: Key Ack set");
1124 return;
1125 }
1126
1127 if (!(key_info & WPA_KEY_INFO_MIC)) {
1128 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1129 "received invalid EAPOL-Key: Key MIC not set");
1130 return;
1131 }
1132
1133 sm->MICVerified = FALSE;
68921e24 1134 if (sm->PTK_valid && !sm->update_snonce) {
929a2ea5
JM
1135 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
1136 data_len)) {
6fc6879b
JM
1137 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1138 "received EAPOL-Key with invalid MIC");
1139 return;
1140 }
1141 sm->MICVerified = TRUE;
1142 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
e4bf4db9 1143 sm->pending_1_of_4_timeout = 0;
6fc6879b
JM
1144 }
1145
1146 if (key_info & WPA_KEY_INFO_REQUEST) {
1147 if (sm->MICVerified) {
1148 sm->req_replay_counter_used = 1;
1149 os_memcpy(sm->req_replay_counter, key->replay_counter,
1150 WPA_REPLAY_COUNTER_LEN);
1151 } else {
1152 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1153 "received EAPOL-Key request with "
1154 "invalid MIC");
1155 return;
1156 }
1157
1158 /*
1159 * TODO: should decrypt key data field if encryption was used;
1160 * even though MAC address KDE is not normally encrypted,
1161 * supplicant is allowed to encrypt it.
1162 */
1163 if (msg == SMK_ERROR) {
1164#ifdef CONFIG_PEERKEY
1165 wpa_smk_error(wpa_auth, sm, key);
1166#endif /* CONFIG_PEERKEY */
1167 return;
1168 } else if (key_info & WPA_KEY_INFO_ERROR) {
c772d054
JM
1169 if (wpa_receive_error_report(
1170 wpa_auth, sm,
1171 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1172 return; /* STA entry was removed */
6fc6879b
JM
1173 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1174 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1175 "received EAPOL-Key Request for new "
1176 "4-Way Handshake");
1177 wpa_request_new_ptk(sm);
1178#ifdef CONFIG_PEERKEY
1179 } else if (msg == SMK_M1) {
1180 wpa_smk_m1(wpa_auth, sm, key);
1181#endif /* CONFIG_PEERKEY */
1182 } else if (key_data_length > 0 &&
1183 wpa_parse_kde_ies((const u8 *) (key + 1),
1184 key_data_length, &kde) == 0 &&
1185 kde.mac_addr) {
1186 } else {
1187 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1188 "received EAPOL-Key Request for GTK "
1189 "rekeying");
6fc6879b
JM
1190 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1191 wpa_rekey_gtk(wpa_auth, NULL);
1192 }
1193 } else {
68921e24
JM
1194 /* Do not allow the same key replay counter to be reused. */
1195 wpa_replay_counter_mark_invalid(sm->key_replay,
1196 key->replay_counter);
1197
1198 if (msg == PAIRWISE_2) {
1199 /*
1200 * Maintain a copy of the pending EAPOL-Key frames in
1201 * case the EAPOL-Key frame was retransmitted. This is
1202 * needed to allow EAPOL-Key msg 2/4 reply to another
1203 * pending msg 1/4 to update the SNonce to work around
1204 * unexpected supplicant behavior.
1205 */
1206 os_memcpy(sm->prev_key_replay, sm->key_replay,
1207 sizeof(sm->key_replay));
1208 } else {
1209 os_memset(sm->prev_key_replay, 0,
1210 sizeof(sm->prev_key_replay));
1211 }
1212
1213 /*
1214 * Make sure old valid counters are not accepted anymore and
1215 * do not get copied again.
1216 */
1217 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
6fc6879b
JM
1218 }
1219
1220#ifdef CONFIG_PEERKEY
1221 if (msg == SMK_M3) {
1222 wpa_smk_m3(wpa_auth, sm, key);
1223 return;
1224 }
1225#endif /* CONFIG_PEERKEY */
1226
1227 os_free(sm->last_rx_eapol_key);
1228 sm->last_rx_eapol_key = os_malloc(data_len);
1229 if (sm->last_rx_eapol_key == NULL)
1230 return;
1231 os_memcpy(sm->last_rx_eapol_key, data, data_len);
1232 sm->last_rx_eapol_key_len = data_len;
1233
0d442aff 1234 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
6fc6879b
JM
1235 sm->EAPOLKeyReceived = TRUE;
1236 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1237 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1238 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1239 wpa_sm_step(sm);
1240}
1241
1242
3c7302c2
JM
1243static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1244 const u8 *gnonce, u8 *gtk, size_t gtk_len)
6fc6879b 1245{
5cb9d5c3
JM
1246 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
1247 u8 *pos;
3c7302c2 1248 int ret = 0;
6fc6879b 1249
5cb9d5c3
JM
1250 /* GTK = PRF-X(GMK, "Group key expansion",
1251 * AA || GNonce || Time || random data)
1252 * The example described in the IEEE 802.11 standard uses only AA and
1253 * GNonce as inputs here. Add some more entropy since this derivation
1254 * is done only at the Authenticator and as such, does not need to be
1255 * exactly same.
1256 */
6fc6879b
JM
1257 os_memcpy(data, addr, ETH_ALEN);
1258 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
5cb9d5c3
JM
1259 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1260 wpa_get_ntp_timestamp(pos);
1261 pos += 8;
3642c431 1262 if (random_get_bytes(pos, 16) < 0)
3c7302c2 1263 ret = -1;
6fc6879b 1264
56586197 1265#ifdef CONFIG_IEEE80211W
5cb9d5c3 1266 sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
56586197 1267#else /* CONFIG_IEEE80211W */
3c7302c2
JM
1268 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
1269 < 0)
1270 ret = -1;
56586197 1271#endif /* CONFIG_IEEE80211W */
3c7302c2
JM
1272
1273 return ret;
6fc6879b
JM
1274}
1275
1276
1277static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1278{
1279 struct wpa_authenticator *wpa_auth = eloop_ctx;
1280 struct wpa_state_machine *sm = timeout_ctx;
1281
e4bf4db9 1282 sm->pending_1_of_4_timeout = 0;
6fc6879b
JM
1283 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1284 sm->TimeoutEvt = TRUE;
1285 wpa_sm_step(sm);
1286}
1287
1288
1289void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1290 struct wpa_state_machine *sm, int key_info,
1291 const u8 *key_rsc, const u8 *nonce,
1292 const u8 *kde, size_t kde_len,
1293 int keyidx, int encr, int force_version)
1294{
1295 struct ieee802_1x_hdr *hdr;
1296 struct wpa_eapol_key *key;
1297 size_t len;
1298 int alg;
1299 int key_data_len, pad_len = 0;
1300 u8 *buf, *pos;
1301 int version, pairwise;
22a299ee 1302 int i;
6fc6879b
JM
1303
1304 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
1305
1306 if (force_version)
1307 version = force_version;
929a2ea5
JM
1308 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1309 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt))
a14896e8 1310 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
6fc6879b
JM
1311 else if (wpa_use_aes_cmac(sm))
1312 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
eb7719ff 1313 else if (sm->pairwise != WPA_CIPHER_TKIP)
6fc6879b
JM
1314 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1315 else
1316 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1317
8543ed8a 1318 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
6fc6879b
JM
1319
1320 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1321 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1322 "encr=%d)",
1323 version,
1324 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1325 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1326 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1327 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1328 pairwise, (unsigned long) kde_len, keyidx, encr);
1329
1330 key_data_len = kde_len;
1331
1332 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
a14896e8 1333 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
929a2ea5 1334 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
6fc6879b
JM
1335 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1336 pad_len = key_data_len % 8;
1337 if (pad_len)
1338 pad_len = 8 - pad_len;
1339 key_data_len += pad_len + 8;
1340 }
1341
1342 len += key_data_len;
1343
1344 hdr = os_zalloc(len);
1345 if (hdr == NULL)
1346 return;
1347 hdr->version = wpa_auth->conf.eapol_version;
1348 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1349 hdr->length = host_to_be16(len - sizeof(*hdr));
1350 key = (struct wpa_eapol_key *) (hdr + 1);
1351
1352 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1353 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1354 key_info |= version;
1355 if (encr && sm->wpa == WPA_VERSION_WPA2)
1356 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1357 if (sm->wpa != WPA_VERSION_WPA2)
1358 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1359 WPA_PUT_BE16(key->key_info, key_info);
1360
1361 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
c3550295 1362 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
6fc6879b
JM
1363 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1364 WPA_PUT_BE16(key->key_length, 0);
1365
1366 /* FIX: STSL: what to use as key_replay_counter? */
22a299ee
JM
1367 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1368 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1369 os_memcpy(sm->key_replay[i].counter,
1370 sm->key_replay[i - 1].counter,
1371 WPA_REPLAY_COUNTER_LEN);
1372 }
1373 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1374 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
6fc6879b 1375 WPA_REPLAY_COUNTER_LEN);
22a299ee 1376 sm->key_replay[0].valid = TRUE;
6fc6879b
JM
1377
1378 if (nonce)
1379 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1380
1381 if (key_rsc)
1382 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1383
1384 if (kde && !encr) {
1385 os_memcpy(key + 1, kde, kde_len);
1386 WPA_PUT_BE16(key->key_data_length, kde_len);
1387 } else if (encr && kde) {
1388 buf = os_zalloc(key_data_len);
1389 if (buf == NULL) {
1390 os_free(hdr);
1391 return;
1392 }
1393 pos = buf;
1394 os_memcpy(pos, kde, kde_len);
1395 pos += kde_len;
1396
1397 if (pad_len)
1398 *pos++ = 0xdd;
1399
1400 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1401 buf, key_data_len);
1402 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
a14896e8 1403 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
929a2ea5 1404 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
6fc6879b 1405 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
eefec1e4
JM
1406 if (aes_wrap(sm->PTK.kek, 16,
1407 (key_data_len - 8) / 8, buf,
6fc6879b
JM
1408 (u8 *) (key + 1))) {
1409 os_free(hdr);
1410 os_free(buf);
1411 return;
1412 }
1413 WPA_PUT_BE16(key->key_data_length, key_data_len);
1414 } else {
1415 u8 ek[32];
1416 os_memcpy(key->key_iv,
1417 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1418 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1419 os_memcpy(ek, key->key_iv, 16);
1420 os_memcpy(ek + 16, sm->PTK.kek, 16);
1421 os_memcpy(key + 1, buf, key_data_len);
1422 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1423 WPA_PUT_BE16(key->key_data_length, key_data_len);
1424 }
1425 os_free(buf);
1426 }
1427
1428 if (key_info & WPA_KEY_INFO_MIC) {
1429 if (!sm->PTK_valid) {
1430 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1431 "PTK not valid when sending EAPOL-Key "
1432 "frame");
1433 os_free(hdr);
1434 return;
1435 }
929a2ea5
JM
1436 wpa_eapol_key_mic(sm->PTK.kck, sm->wpa_key_mgmt, version,
1437 (u8 *) hdr, len, key->key_mic);
7af092a0
JB
1438#ifdef CONFIG_TESTING_OPTIONS
1439 if (!pairwise &&
06df2aa6 1440 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
7af092a0
JB
1441 drand48() <
1442 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1443 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1444 "Corrupting group EAPOL-Key Key MIC");
1445 key->key_mic[0]++;
1446 }
1447#endif /* CONFIG_TESTING_OPTIONS */
6fc6879b
JM
1448 }
1449
1450 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1451 1);
1452 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1453 sm->pairwise_set);
1454 os_free(hdr);
1455}
1456
1457
1458static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1459 struct wpa_state_machine *sm, int key_info,
1460 const u8 *key_rsc, const u8 *nonce,
1461 const u8 *kde, size_t kde_len,
1462 int keyidx, int encr)
1463{
1464 int timeout_ms;
1465 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
bae61562 1466 int ctr;
6fc6879b
JM
1467
1468 if (sm == NULL)
1469 return;
1470
1471 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1472 keyidx, encr, 0);
1473
bae61562 1474 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
e4bf4db9 1475 if (ctr == 1 && wpa_auth->conf.tx_status)
00338a4f
JM
1476 timeout_ms = pairwise ? eapol_key_timeout_first :
1477 eapol_key_timeout_first_group;
bae61562
JM
1478 else
1479 timeout_ms = eapol_key_timeout_subseq;
e4bf4db9
JM
1480 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1481 sm->pending_1_of_4_timeout = 1;
1482 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1483 "counter %d)", timeout_ms, ctr);
6fc6879b
JM
1484 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1485 wpa_send_eapol_timeout, wpa_auth, sm);
1486}
1487
1488
929a2ea5
JM
1489static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
1490 size_t data_len)
6fc6879b
JM
1491{
1492 struct ieee802_1x_hdr *hdr;
1493 struct wpa_eapol_key *key;
1494 u16 key_info;
1495 int ret = 0;
1496 u8 mic[16];
1497
1498 if (data_len < sizeof(*hdr) + sizeof(*key))
1499 return -1;
1500
1501 hdr = (struct ieee802_1x_hdr *) data;
1502 key = (struct wpa_eapol_key *) (hdr + 1);
1503 key_info = WPA_GET_BE16(key->key_info);
1504 os_memcpy(mic, key->key_mic, 16);
1505 os_memset(key->key_mic, 0, 16);
929a2ea5 1506 if (wpa_eapol_key_mic(PTK->kck, akmp, key_info & WPA_KEY_INFO_TYPE_MASK,
6fc6879b 1507 data, data_len, key->key_mic) ||
870834a1 1508 os_memcmp_const(mic, key->key_mic, 16) != 0)
6fc6879b
JM
1509 ret = -1;
1510 os_memcpy(key->key_mic, mic, 16);
1511 return ret;
1512}
1513
1514
1515void wpa_remove_ptk(struct wpa_state_machine *sm)
1516{
1517 sm->PTK_valid = FALSE;
1518 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
26083221 1519 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
6fc6879b 1520 sm->pairwise_set = FALSE;
581a8cde 1521 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
6fc6879b
JM
1522}
1523
1524
6f9b5d16 1525int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
6fc6879b 1526{
5d22a1d5
JM
1527 int remove_ptk = 1;
1528
6fc6879b 1529 if (sm == NULL)
6f9b5d16 1530 return -1;
6fc6879b
JM
1531
1532 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1533 "event %d notification", event);
1534
1535 switch (event) {
1536 case WPA_AUTH:
1537 case WPA_ASSOC:
1538 break;
1539 case WPA_DEAUTH:
1540 case WPA_DISASSOC:
1541 sm->DeauthenticationRequest = TRUE;
1542 break;
1543 case WPA_REAUTH:
1544 case WPA_REAUTH_EAPOL:
3ab72b62
JM
1545 if (!sm->started) {
1546 /*
1547 * When using WPS, we may end up here if the STA
1548 * manages to re-associate without the previous STA
1549 * entry getting removed. Consequently, we need to make
1550 * sure that the WPA state machines gets initialized
1551 * properly at this point.
1552 */
1553 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1554 "started - initialize now");
1555 sm->started = 1;
1556 sm->Init = TRUE;
1557 if (wpa_sm_step(sm) == 1)
1558 return 1; /* should not really happen */
1559 sm->Init = FALSE;
1560 sm->AuthenticationRequest = TRUE;
1561 break;
1562 }
9663596f
JM
1563 if (sm->GUpdateStationKeys) {
1564 /*
1565 * Reauthentication cancels the pending group key
1566 * update for this STA.
1567 */
1568 sm->group->GKeyDoneStations--;
1569 sm->GUpdateStationKeys = FALSE;
1570 sm->PtkGroupInit = TRUE;
1571 }
6fc6879b
JM
1572 sm->ReAuthenticationRequest = TRUE;
1573 break;
1574 case WPA_ASSOC_FT:
1575#ifdef CONFIG_IEEE80211R
81a658d7
JM
1576 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1577 "after association");
1578 wpa_ft_install_ptk(sm);
1579
6fc6879b
JM
1580 /* Using FT protocol, not WPA auth state machine */
1581 sm->ft_completed = 1;
6f9b5d16 1582 return 0;
6fc6879b
JM
1583#else /* CONFIG_IEEE80211R */
1584 break;
1585#endif /* CONFIG_IEEE80211R */
1586 }
1587
1588#ifdef CONFIG_IEEE80211R
1589 sm->ft_completed = 0;
1590#endif /* CONFIG_IEEE80211R */
1591
5d22a1d5
JM
1592#ifdef CONFIG_IEEE80211W
1593 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1594 remove_ptk = 0;
1595#endif /* CONFIG_IEEE80211W */
6fc6879b 1596
5d22a1d5
JM
1597 if (remove_ptk) {
1598 sm->PTK_valid = FALSE;
1599 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1600
1601 if (event != WPA_REAUTH_EAPOL)
1602 wpa_remove_ptk(sm);
1603 }
6fc6879b 1604
6f9b5d16 1605 return wpa_sm_step(sm);
6fc6879b
JM
1606}
1607
1608
6fc6879b
JM
1609SM_STATE(WPA_PTK, INITIALIZE)
1610{
1611 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1612 if (sm->Init) {
1613 /* Init flag is not cleared here, so avoid busy
1614 * loop by claiming nothing changed. */
1615 sm->changed = FALSE;
1616 }
1617
1618 sm->keycount = 0;
1619 if (sm->GUpdateStationKeys)
1620 sm->group->GKeyDoneStations--;
1621 sm->GUpdateStationKeys = FALSE;
1622 if (sm->wpa == WPA_VERSION_WPA)
1623 sm->PInitAKeys = FALSE;
1624 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1625 * Local AA > Remote AA)) */) {
1626 sm->Pair = TRUE;
1627 }
1628 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1629 wpa_remove_ptk(sm);
1630 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1631 sm->TimeoutCtr = 0;
56586197 1632 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
6fc6879b
JM
1633 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1634 WPA_EAPOL_authorized, 0);
1635 }
1636}
1637
1638
1639SM_STATE(WPA_PTK, DISCONNECT)
1640{
1641 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1642 sm->Disconnect = FALSE;
1643 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1644}
1645
1646
1647SM_STATE(WPA_PTK, DISCONNECTED)
1648{
1649 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1650 sm->DeauthenticationRequest = FALSE;
1651}
1652
1653
1654SM_STATE(WPA_PTK, AUTHENTICATION)
1655{
1656 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1657 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1658 sm->PTK_valid = FALSE;
1659 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1660 1);
1661 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1662 sm->AuthenticationRequest = FALSE;
1663}
1664
1665
40d00d2b
NC
1666static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1667 struct wpa_group *group)
1bdb7ab3 1668{
40d00d2b
NC
1669 if (group->first_sta_seen)
1670 return;
1bdb7ab3
JM
1671 /*
1672 * System has run bit further than at the time hostapd was started
1673 * potentially very early during boot up. This provides better chances
1674 * of collecting more randomness on embedded systems. Re-initialize the
1675 * GMK and Counter here to improve their strength if there was not
1676 * enough entropy available immediately after system startup.
1677 */
1678 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1679 "station");
08704cd8
JM
1680 if (random_pool_ready() != 1) {
1681 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1682 "to proceed - reject first 4-way handshake");
1683 group->reject_4way_hs_for_entropy = TRUE;
40d00d2b
NC
1684 } else {
1685 group->first_sta_seen = TRUE;
1686 group->reject_4way_hs_for_entropy = FALSE;
08704cd8 1687 }
40d00d2b 1688
1bdb7ab3
JM
1689 wpa_group_init_gmk_and_counter(wpa_auth, group);
1690 wpa_gtk_update(wpa_auth, group);
1691 wpa_group_config_group_keys(wpa_auth, group);
1692}
1693
1694
6fc6879b
JM
1695SM_STATE(WPA_PTK, AUTHENTICATION2)
1696{
1697 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1bdb7ab3 1698
40d00d2b 1699 wpa_group_ensure_init(sm->wpa_auth, sm->group);
65a32cdb 1700 sm->ReAuthenticationRequest = FALSE;
1bdb7ab3 1701
3825a19b
JM
1702 /*
1703 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1704 * ambiguous. The Authenticator state machine uses a counter that is
1705 * incremented by one for each 4-way handshake. However, the security
1706 * analysis of 4-way handshake points out that unpredictable nonces
1707 * help in preventing precomputation attacks. Instead of the state
1708 * machine definition, use an unpredictable nonce value here to provide
1709 * stronger protection against potential precomputation attacks.
1710 */
1711 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1712 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1713 "ANonce.");
65a32cdb 1714 sm->Disconnect = TRUE;
3825a19b
JM
1715 return;
1716 }
bc8318ac
JM
1717 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1718 WPA_NONCE_LEN);
6fc6879b
JM
1719 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1720 * logical place than INITIALIZE since AUTHENTICATION2 can be
1721 * re-entered on ReAuthenticationRequest without going through
1722 * INITIALIZE. */
1723 sm->TimeoutCtr = 0;
1724}
1725
1726
1727SM_STATE(WPA_PTK, INITPMK)
1728{
1729 u8 msk[2 * PMK_LEN];
1730 size_t len = 2 * PMK_LEN;
1731
1732 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1733#ifdef CONFIG_IEEE80211R
1734 sm->xxkey_len = 0;
1735#endif /* CONFIG_IEEE80211R */
1736 if (sm->pmksa) {
1737 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1738 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1739 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1740 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1741 "(len=%lu)", (unsigned long) len);
1742 os_memcpy(sm->PMK, msk, PMK_LEN);
1743#ifdef CONFIG_IEEE80211R
1744 if (len >= 2 * PMK_LEN) {
1745 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1746 sm->xxkey_len = PMK_LEN;
1747 }
1748#endif /* CONFIG_IEEE80211R */
1749 } else {
1750 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1751 }
1752
1753 sm->req_replay_counter_used = 0;
1754 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1755 * will break reauthentication since EAPOL state machines may not be
1756 * get into AUTHENTICATING state that clears keyRun before WPA state
1757 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1758 * state and takes PMK from the previously used AAA Key. This will
1759 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1760 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1761 * be good workaround for this issue. */
1762 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1763}
1764
1765
1766SM_STATE(WPA_PTK, INITPSK)
1767{
1768 const u8 *psk;
1769 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
759fd76b 1770 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
6fc6879b
JM
1771 if (psk) {
1772 os_memcpy(sm->PMK, psk, PMK_LEN);
1773#ifdef CONFIG_IEEE80211R
1774 os_memcpy(sm->xxkey, psk, PMK_LEN);
1775 sm->xxkey_len = PMK_LEN;
1776#endif /* CONFIG_IEEE80211R */
1777 }
1778 sm->req_replay_counter_used = 0;
1779}
1780
1781
1782SM_STATE(WPA_PTK, PTKSTART)
1783{
1784 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1785 size_t pmkid_len = 0;
1786
1787 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1788 sm->PTKRequest = FALSE;
1789 sm->TimeoutEvt = FALSE;
bae61562
JM
1790
1791 sm->TimeoutCtr++;
1792 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1793 /* No point in sending the EAPOL-Key - we will disconnect
1794 * immediately following this. */
1795 return;
1796 }
1797
6fc6879b
JM
1798 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1799 "sending 1/4 msg of 4-Way Handshake");
1800 /*
1801 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1802 * one possible PSK for this STA.
1803 */
1804 if (sm->wpa == WPA_VERSION_WPA2 &&
a14896e8
JM
1805 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1806 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
6fc6879b
JM
1807 pmkid = buf;
1808 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1809 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1810 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1811 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
929a2ea5 1812 if (sm->pmksa) {
6fc6879b
JM
1813 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1814 sm->pmksa->pmkid, PMKID_LEN);
929a2ea5
JM
1815 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
1816 /* No KCK available to derive PMKID */
1817 pmkid = NULL;
1818 } else {
6fc6879b
JM
1819 /*
1820 * Calculate PMKID since no PMKSA cache entry was
1821 * available with pre-calculated PMKID.
1822 */
1823 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
56586197
JM
1824 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1825 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
6fc6879b
JM
1826 }
1827 }
1828 wpa_send_eapol(sm->wpa_auth, sm,
1829 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1830 sm->ANonce, pmkid, pmkid_len, 0, 0);
6fc6879b
JM
1831}
1832
1833
1834static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1835 struct wpa_ptk *ptk)
1836{
3b9c5176 1837 size_t ptk_len = wpa_cipher_key_len(sm->pairwise) + 32;
6fc6879b 1838#ifdef CONFIG_IEEE80211R
56586197 1839 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
c0a61908 1840 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
6fc6879b
JM
1841#endif /* CONFIG_IEEE80211R */
1842
1843 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1844 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
c0a61908 1845 (u8 *) ptk, ptk_len,
56586197 1846 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
6fc6879b
JM
1847
1848 return 0;
1849}
1850
1851
1852SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1853{
1854 struct wpa_ptk PTK;
1855 int ok = 0;
1856 const u8 *pmk = NULL;
1857
1858 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1859 sm->EAPOLKeyReceived = FALSE;
68921e24 1860 sm->update_snonce = FALSE;
6fc6879b
JM
1861
1862 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1863 * WPA-PSK: iterate through possible PSKs and select the one matching
1864 * the packet */
1865 for (;;) {
56586197 1866 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
759fd76b
JM
1867 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
1868 sm->p2p_dev_addr, pmk);
6fc6879b
JM
1869 if (pmk == NULL)
1870 break;
1871 } else
1872 pmk = sm->PMK;
1873
1874 wpa_derive_ptk(sm, pmk, &PTK);
1875
929a2ea5
JM
1876 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
1877 sm->last_rx_eapol_key,
6fc6879b
JM
1878 sm->last_rx_eapol_key_len) == 0) {
1879 ok = 1;
1880 break;
1881 }
1882
56586197 1883 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
6fc6879b
JM
1884 break;
1885 }
1886
1887 if (!ok) {
1888 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1889 "invalid MIC in msg 2/4 of 4-Way Handshake");
1890 return;
1891 }
1892
26e23750
JM
1893#ifdef CONFIG_IEEE80211R
1894 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1895 /*
1896 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
1897 * with the value we derived.
1898 */
870834a1
JM
1899 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
1900 WPA_PMK_NAME_LEN) != 0) {
26e23750
JM
1901 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1902 "PMKR1Name mismatch in FT 4-way "
1903 "handshake");
1904 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
1905 "Supplicant",
1906 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
1907 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1908 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1909 return;
1910 }
1911 }
1912#endif /* CONFIG_IEEE80211R */
1913
e4bf4db9 1914 sm->pending_1_of_4_timeout = 0;
6fc6879b
JM
1915 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1916
56586197 1917 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
6fc6879b
JM
1918 /* PSK may have changed from the previous choice, so update
1919 * state machine data based on whatever PSK was selected here.
1920 */
1921 os_memcpy(sm->PMK, pmk, PMK_LEN);
1922 }
1923
1924 sm->MICVerified = TRUE;
1925
1926 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1927 sm->PTK_valid = TRUE;
1928}
1929
1930
1931SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1932{
1933 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1934 sm->TimeoutCtr = 0;
1935}
1936
1937
1938#ifdef CONFIG_IEEE80211W
1939
1940static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1941{
1942 if (sm->mgmt_frame_prot) {
8dd9f9cd
JM
1943 size_t len;
1944 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
1945 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
6fc6879b
JM
1946 }
1947
1948 return 0;
1949}
1950
1951
1952static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1953{
1954 struct wpa_igtk_kde igtk;
1955 struct wpa_group *gsm = sm->group;
03610ad2 1956 u8 rsc[WPA_KEY_RSC_LEN];
8dd9f9cd 1957 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
6fc6879b
JM
1958
1959 if (!sm->mgmt_frame_prot)
1960 return pos;
1961
1962 igtk.keyid[0] = gsm->GN_igtk;
1963 igtk.keyid[1] = 0;
7b1080da 1964 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
03610ad2 1965 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
6fc6879b 1966 os_memset(igtk.pn, 0, sizeof(igtk.pn));
03610ad2
JM
1967 else
1968 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
8dd9f9cd 1969 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
83421850
JM
1970 if (sm->wpa_auth->conf.disable_gtk) {
1971 /*
1972 * Provide unique random IGTK to each STA to prevent use of
1973 * IGTK in the BSS.
1974 */
8dd9f9cd 1975 if (random_get_bytes(igtk.igtk, len) < 0)
83421850
JM
1976 return pos;
1977 }
6fc6879b 1978 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
8dd9f9cd
JM
1979 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
1980 NULL, 0);
6fc6879b
JM
1981
1982 return pos;
1983}
1984
1985#else /* CONFIG_IEEE80211W */
1986
1987static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1988{
1989 return 0;
1990}
1991
1992
1993static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1994{
1995 return pos;
1996}
1997
1998#endif /* CONFIG_IEEE80211W */
1999
2000
2001SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2002{
83421850 2003 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
6fc6879b
JM
2004 size_t gtk_len, kde_len;
2005 struct wpa_group *gsm = sm->group;
2006 u8 *wpa_ie;
2007 int wpa_ie_len, secure, keyidx, encr = 0;
2008
2009 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2010 sm->TimeoutEvt = FALSE;
bae61562
JM
2011
2012 sm->TimeoutCtr++;
2013 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
2014 /* No point in sending the EAPOL-Key - we will disconnect
2015 * immediately following this. */
2016 return;
2017 }
2018
86dfabb8
JM
2019 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2020 GTK[GN], IGTK, [FTIE], [TIE * 2])
6fc6879b
JM
2021 */
2022 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2023 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
86dfabb8 2024 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
6fc6879b
JM
2025 wpa_ie = sm->wpa_auth->wpa_ie;
2026 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
2027 if (sm->wpa == WPA_VERSION_WPA &&
2028 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
2029 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
2030 /* WPA-only STA, remove RSN IE */
2031 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2032 wpa_ie_len = wpa_ie[1] + 2;
2033 }
2034 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2035 "sending 3/4 msg of 4-Way Handshake");
2036 if (sm->wpa == WPA_VERSION_WPA2) {
2037 /* WPA2 send GTK in the 4-way handshake */
2038 secure = 1;
2039 gtk = gsm->GTK[gsm->GN - 1];
2040 gtk_len = gsm->GTK_len;
83421850
JM
2041 if (sm->wpa_auth->conf.disable_gtk) {
2042 /*
2043 * Provide unique random GTK to each STA to prevent use
2044 * of GTK in the BSS.
2045 */
2046 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
2047 return;
2048 gtk = dummy_gtk;
2049 }
6fc6879b
JM
2050 keyidx = gsm->GN;
2051 _rsc = rsc;
2052 encr = 1;
2053 } else {
2054 /* WPA does not include GTK in msg 3/4 */
2055 secure = 0;
2056 gtk = NULL;
2057 gtk_len = 0;
2058 keyidx = 0;
2059 _rsc = NULL;
0d442aff
JM
2060 if (sm->rx_eapol_key_secure) {
2061 /*
2062 * It looks like Windows 7 supplicant tries to use
2063 * Secure bit in msg 2/4 after having reported Michael
2064 * MIC failure and it then rejects the 4-way handshake
2065 * if msg 3/4 does not set Secure bit. Work around this
2066 * by setting the Secure bit here even in the case of
2067 * WPA if the supplicant used it first.
2068 */
2069 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4740d5b9
JM
2070 "STA used Secure bit in WPA msg 2/4 - "
2071 "set Secure for 3/4 as workaround");
0d442aff
JM
2072 secure = 1;
2073 }
6fc6879b
JM
2074 }
2075
2076 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
2077 if (gtk)
2078 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
26e23750 2079#ifdef CONFIG_IEEE80211R
86dfabb8
JM
2080 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2081 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
2082 kde_len += 300; /* FTIE + 2 * TIE */
2083 }
26e23750 2084#endif /* CONFIG_IEEE80211R */
25ef8529
JM
2085#ifdef CONFIG_P2P
2086 if (WPA_GET_BE32(sm->ip_addr) > 0)
2087 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
2088#endif /* CONFIG_P2P */
6fc6879b
JM
2089 kde = os_malloc(kde_len);
2090 if (kde == NULL)
2091 return;
2092
2093 pos = kde;
2094 os_memcpy(pos, wpa_ie, wpa_ie_len);
2095 pos += wpa_ie_len;
26e23750
JM
2096#ifdef CONFIG_IEEE80211R
2097 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2098 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
2099 if (res < 0) {
2100 wpa_printf(MSG_ERROR, "FT: Failed to insert "
2101 "PMKR1Name into RSN IE in EAPOL-Key data");
2102 os_free(kde);
2103 return;
2104 }
2105 pos += res;
2106 }
2107#endif /* CONFIG_IEEE80211R */
6fc6879b
JM
2108 if (gtk) {
2109 u8 hdr[2];
2110 hdr[0] = keyidx & 0x03;
2111 hdr[1] = 0;
2112 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2113 gtk, gtk_len);
2114 }
2115 pos = ieee80211w_kde_add(sm, pos);
2116
86dfabb8
JM
2117#ifdef CONFIG_IEEE80211R
2118 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2119 int res;
2120 struct wpa_auth_config *conf;
2121
2122 conf = &sm->wpa_auth->conf;
2123 res = wpa_write_ftie(conf, conf->r0_key_holder,
2124 conf->r0_key_holder_len,
2125 NULL, NULL, pos, kde + kde_len - pos,
2126 NULL, 0);
2127 if (res < 0) {
2128 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2129 "into EAPOL-Key Key Data");
2130 os_free(kde);
2131 return;
2132 }
2133 pos += res;
2134
2135 /* TIE[ReassociationDeadline] (TU) */
2136 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2137 *pos++ = 5;
2138 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2139 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2140 pos += 4;
2141
2142 /* TIE[KeyLifetime] (seconds) */
2143 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2144 *pos++ = 5;
2145 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2146 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2147 pos += 4;
2148 }
2149#endif /* CONFIG_IEEE80211R */
25ef8529
JM
2150#ifdef CONFIG_P2P
2151 if (WPA_GET_BE32(sm->ip_addr) > 0) {
2152 u8 addr[3 * 4];
2153 os_memcpy(addr, sm->ip_addr, 4);
2154 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
2155 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
2156 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
2157 addr, sizeof(addr), NULL, 0);
2158 }
2159#endif /* CONFIG_P2P */
86dfabb8 2160
6fc6879b
JM
2161 wpa_send_eapol(sm->wpa_auth, sm,
2162 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
2163 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2164 WPA_KEY_INFO_KEY_TYPE,
2165 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2166 os_free(kde);
6fc6879b
JM
2167}
2168
2169
2170SM_STATE(WPA_PTK, PTKINITDONE)
2171{
2172 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2173 sm->EAPOLKeyReceived = FALSE;
2174 if (sm->Pair) {
c3550295
JM
2175 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2176 int klen = wpa_cipher_key_len(sm->pairwise);
6fc6879b
JM
2177 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2178 sm->PTK.tk1, klen)) {
2179 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2180 return;
2181 }
2182 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2183 sm->pairwise_set = TRUE;
2184
581a8cde
JM
2185 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2186 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2187 eloop_register_timeout(sm->wpa_auth->conf.
2188 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2189 sm->wpa_auth, sm);
2190 }
2191
56586197 2192 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
6fc6879b
JM
2193 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2194 WPA_EAPOL_authorized, 1);
2195 }
2196 }
2197
2198 if (0 /* IBSS == TRUE */) {
2199 sm->keycount++;
2200 if (sm->keycount == 2) {
2201 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2202 WPA_EAPOL_portValid, 1);
2203 }
2204 } else {
2205 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2206 1);
2207 }
2208 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2209 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2210 if (sm->wpa == WPA_VERSION_WPA)
2211 sm->PInitAKeys = TRUE;
2212 else
2213 sm->has_GTK = TRUE;
2214 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2215 "pairwise key handshake completed (%s)",
2216 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2217
2218#ifdef CONFIG_IEEE80211R
2219 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2220#endif /* CONFIG_IEEE80211R */
2221}
2222
2223
2224SM_STEP(WPA_PTK)
2225{
2226 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2227
2228 if (sm->Init)
2229 SM_ENTER(WPA_PTK, INITIALIZE);
2230 else if (sm->Disconnect
1aae01fc
BG
2231 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2232 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2233 "WPA_PTK: sm->Disconnect");
6fc6879b 2234 SM_ENTER(WPA_PTK, DISCONNECT);
1aae01fc 2235 }
6fc6879b
JM
2236 else if (sm->DeauthenticationRequest)
2237 SM_ENTER(WPA_PTK, DISCONNECTED);
2238 else if (sm->AuthenticationRequest)
2239 SM_ENTER(WPA_PTK, AUTHENTICATION);
2240 else if (sm->ReAuthenticationRequest)
2241 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2242 else if (sm->PTKRequest)
2243 SM_ENTER(WPA_PTK, PTKSTART);
2244 else switch (sm->wpa_ptk_state) {
2245 case WPA_PTK_INITIALIZE:
2246 break;
2247 case WPA_PTK_DISCONNECT:
2248 SM_ENTER(WPA_PTK, DISCONNECTED);
2249 break;
2250 case WPA_PTK_DISCONNECTED:
2251 SM_ENTER(WPA_PTK, INITIALIZE);
2252 break;
2253 case WPA_PTK_AUTHENTICATION:
2254 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2255 break;
2256 case WPA_PTK_AUTHENTICATION2:
56586197 2257 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
6fc6879b
JM
2258 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2259 WPA_EAPOL_keyRun) > 0)
2260 SM_ENTER(WPA_PTK, INITPMK);
56586197 2261 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
6fc6879b
JM
2262 /* FIX: && 802.1X::keyRun */)
2263 SM_ENTER(WPA_PTK, INITPSK);
2264 break;
2265 case WPA_PTK_INITPMK:
2266 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2267 WPA_EAPOL_keyAvailable) > 0)
2268 SM_ENTER(WPA_PTK, PTKSTART);
2269 else {
2270 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1aae01fc
BG
2271 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2272 "INITPMK - keyAvailable = false");
6fc6879b
JM
2273 SM_ENTER(WPA_PTK, DISCONNECT);
2274 }
2275 break;
2276 case WPA_PTK_INITPSK:
759fd76b
JM
2277 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
2278 NULL))
6fc6879b
JM
2279 SM_ENTER(WPA_PTK, PTKSTART);
2280 else {
2281 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2282 "no PSK configured for the STA");
2283 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2284 SM_ENTER(WPA_PTK, DISCONNECT);
2285 }
2286 break;
2287 case WPA_PTK_PTKSTART:
2288 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2289 sm->EAPOLKeyPairwise)
2290 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2291 else if (sm->TimeoutCtr >
2292 (int) dot11RSNAConfigPairwiseUpdateCount) {
2293 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1aae01fc
BG
2294 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2295 "PTKSTART: Retry limit %d reached",
2296 dot11RSNAConfigPairwiseUpdateCount);
6fc6879b
JM
2297 SM_ENTER(WPA_PTK, DISCONNECT);
2298 } else if (sm->TimeoutEvt)
2299 SM_ENTER(WPA_PTK, PTKSTART);
2300 break;
2301 case WPA_PTK_PTKCALCNEGOTIATING:
2302 if (sm->MICVerified)
2303 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2304 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2305 sm->EAPOLKeyPairwise)
2306 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2307 else if (sm->TimeoutEvt)
2308 SM_ENTER(WPA_PTK, PTKSTART);
2309 break;
2310 case WPA_PTK_PTKCALCNEGOTIATING2:
2311 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2312 break;
2313 case WPA_PTK_PTKINITNEGOTIATING:
68921e24
JM
2314 if (sm->update_snonce)
2315 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2316 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2317 sm->EAPOLKeyPairwise && sm->MICVerified)
6fc6879b
JM
2318 SM_ENTER(WPA_PTK, PTKINITDONE);
2319 else if (sm->TimeoutCtr >
2320 (int) dot11RSNAConfigPairwiseUpdateCount) {
2321 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1aae01fc
BG
2322 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2323 "PTKINITNEGOTIATING: Retry limit %d "
2324 "reached",
2325 dot11RSNAConfigPairwiseUpdateCount);
6fc6879b
JM
2326 SM_ENTER(WPA_PTK, DISCONNECT);
2327 } else if (sm->TimeoutEvt)
2328 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2329 break;
2330 case WPA_PTK_PTKINITDONE:
2331 break;
2332 }
2333}
2334
2335
2336SM_STATE(WPA_PTK_GROUP, IDLE)
2337{
2338 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2339 if (sm->Init) {
2340 /* Init flag is not cleared here, so avoid busy
2341 * loop by claiming nothing changed. */
2342 sm->changed = FALSE;
2343 }
2344 sm->GTimeoutCtr = 0;
2345}
2346
2347
2348SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2349{
2350 u8 rsc[WPA_KEY_RSC_LEN];
2351 struct wpa_group *gsm = sm->group;
e41e4f9e
JM
2352 const u8 *kde;
2353 u8 *kde_buf = NULL, *pos, hdr[2];
6fc6879b 2354 size_t kde_len;
83421850 2355 u8 *gtk, dummy_gtk[32];
6fc6879b
JM
2356
2357 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
bae61562
JM
2358
2359 sm->GTimeoutCtr++;
2360 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
2361 /* No point in sending the EAPOL-Key - we will disconnect
2362 * immediately following this. */
2363 return;
2364 }
2365
6fc6879b
JM
2366 if (sm->wpa == WPA_VERSION_WPA)
2367 sm->PInitAKeys = FALSE;
2368 sm->TimeoutEvt = FALSE;
2369 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2370 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2371 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2372 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2373 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2374 "sending 1/2 msg of Group Key Handshake");
2375
83421850
JM
2376 gtk = gsm->GTK[gsm->GN - 1];
2377 if (sm->wpa_auth->conf.disable_gtk) {
2378 /*
2379 * Provide unique random GTK to each STA to prevent use
2380 * of GTK in the BSS.
2381 */
2382 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
2383 return;
2384 gtk = dummy_gtk;
2385 }
6fc6879b
JM
2386 if (sm->wpa == WPA_VERSION_WPA2) {
2387 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
2388 ieee80211w_kde_len(sm);
e41e4f9e
JM
2389 kde_buf = os_malloc(kde_len);
2390 if (kde_buf == NULL)
6fc6879b
JM
2391 return;
2392
e41e4f9e 2393 kde = pos = kde_buf;
6fc6879b
JM
2394 hdr[0] = gsm->GN & 0x03;
2395 hdr[1] = 0;
2396 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
83421850 2397 gtk, gsm->GTK_len);
6fc6879b 2398 pos = ieee80211w_kde_add(sm, pos);
e41e4f9e 2399 kde_len = pos - kde;
6fc6879b 2400 } else {
83421850 2401 kde = gtk;
e41e4f9e 2402 kde_len = gsm->GTK_len;
6fc6879b
JM
2403 }
2404
2405 wpa_send_eapol(sm->wpa_auth, sm,
2406 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
2407 WPA_KEY_INFO_ACK |
2408 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
e41e4f9e
JM
2409 rsc, gsm->GNonce, kde, kde_len, gsm->GN, 1);
2410
2411 os_free(kde_buf);
6fc6879b
JM
2412}
2413
2414
2415SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2416{
2417 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2418 sm->EAPOLKeyReceived = FALSE;
2419 if (sm->GUpdateStationKeys)
2420 sm->group->GKeyDoneStations--;
2421 sm->GUpdateStationKeys = FALSE;
2422 sm->GTimeoutCtr = 0;
2423 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2424 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2425 "group key handshake completed (%s)",
2426 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2427 sm->has_GTK = TRUE;
2428}
2429
2430
2431SM_STATE(WPA_PTK_GROUP, KEYERROR)
2432{
2433 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2434 if (sm->GUpdateStationKeys)
2435 sm->group->GKeyDoneStations--;
2436 sm->GUpdateStationKeys = FALSE;
2437 sm->Disconnect = TRUE;
2438}
2439
2440
2441SM_STEP(WPA_PTK_GROUP)
2442{
9663596f 2443 if (sm->Init || sm->PtkGroupInit) {
6fc6879b 2444 SM_ENTER(WPA_PTK_GROUP, IDLE);
9663596f
JM
2445 sm->PtkGroupInit = FALSE;
2446 } else switch (sm->wpa_ptk_group_state) {
6fc6879b
JM
2447 case WPA_PTK_GROUP_IDLE:
2448 if (sm->GUpdateStationKeys ||
2449 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2450 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2451 break;
2452 case WPA_PTK_GROUP_REKEYNEGOTIATING:
2453 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2454 !sm->EAPOLKeyPairwise && sm->MICVerified)
2455 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2456 else if (sm->GTimeoutCtr >
2457 (int) dot11RSNAConfigGroupUpdateCount)
2458 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2459 else if (sm->TimeoutEvt)
2460 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2461 break;
2462 case WPA_PTK_GROUP_KEYERROR:
2463 SM_ENTER(WPA_PTK_GROUP, IDLE);
2464 break;
2465 case WPA_PTK_GROUP_REKEYESTABLISHED:
2466 SM_ENTER(WPA_PTK_GROUP, IDLE);
2467 break;
2468 }
2469}
2470
2471
2472static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
2473 struct wpa_group *group)
2474{
2475 int ret = 0;
2476
6fc6879b
JM
2477 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2478 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3c7302c2
JM
2479 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
2480 wpa_auth->addr, group->GNonce,
2481 group->GTK[group->GN - 1], group->GTK_len) < 0)
2482 ret = -1;
5cb9d5c3
JM
2483 wpa_hexdump_key(MSG_DEBUG, "GTK",
2484 group->GTK[group->GN - 1], group->GTK_len);
6fc6879b
JM
2485
2486#ifdef CONFIG_IEEE80211W
70f8cc8e 2487 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
8dd9f9cd
JM
2488 size_t len;
2489 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
5cb9d5c3
JM
2490 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2491 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3c7302c2
JM
2492 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
2493 wpa_auth->addr, group->GNonce,
8dd9f9cd 2494 group->IGTK[group->GN_igtk - 4], len) < 0)
3c7302c2 2495 ret = -1;
6fc6879b 2496 wpa_hexdump_key(MSG_DEBUG, "IGTK",
8dd9f9cd 2497 group->IGTK[group->GN_igtk - 4], len);
6fc6879b
JM
2498 }
2499#endif /* CONFIG_IEEE80211W */
2500
2501 return ret;
2502}
2503
2504
2505static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2506 struct wpa_group *group)
2507{
2508 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2509 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2510 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2511 group->wpa_group_state = WPA_GROUP_GTK_INIT;
2512
2513 /* GTK[0..N] = 0 */
2514 os_memset(group->GTK, 0, sizeof(group->GTK));
2515 group->GN = 1;
2516 group->GM = 2;
2517#ifdef CONFIG_IEEE80211W
2518 group->GN_igtk = 4;
2519 group->GM_igtk = 5;
2520#endif /* CONFIG_IEEE80211W */
2521 /* GTK[GN] = CalcGTK() */
2522 wpa_gtk_update(wpa_auth, group);
2523}
2524
2525
2526static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2527{
473b6f22
MB
2528 if (ctx != NULL && ctx != sm->group)
2529 return 0;
2530
6fc6879b
JM
2531 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2532 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2533 "Not in PTKINITDONE; skip Group Key update");
3c183894 2534 sm->GUpdateStationKeys = FALSE;
6fc6879b
JM
2535 return 0;
2536 }
9663596f
JM
2537 if (sm->GUpdateStationKeys) {
2538 /*
3c183894
JM
2539 * This should not really happen, so add a debug log entry.
2540 * Since we clear the GKeyDoneStations before the loop, the
2541 * station needs to be counted here anyway.
9663596f
JM
2542 */
2543 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3c183894
JM
2544 "GUpdateStationKeys was already set when "
2545 "marking station for GTK rekeying");
9663596f 2546 }
3c183894 2547
ad3872a3 2548 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
d32d94db
XC
2549 if (sm->is_wnmsleep)
2550 return 0;
d32d94db 2551
3c183894
JM
2552 sm->group->GKeyDoneStations++;
2553 sm->GUpdateStationKeys = TRUE;
2554
6fc6879b
JM
2555 wpa_sm_step(sm);
2556 return 0;
2557}
2558
2559
ad3872a3
JM
2560#ifdef CONFIG_WNM
2561/* update GTK when exiting WNM-Sleep Mode */
d32d94db
XC
2562void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
2563{
629edfef 2564 if (sm == NULL || sm->is_wnmsleep)
d32d94db
XC
2565 return;
2566
2567 wpa_group_update_sta(sm, NULL);
2568}
2569
2570
2571void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
2572{
629edfef
JM
2573 if (sm)
2574 sm->is_wnmsleep = !!flag;
d32d94db
XC
2575}
2576
2577
2578int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2579{
d32d94db 2580 struct wpa_group *gsm = sm->group;
835822d4 2581 u8 *start = pos;
d32d94db
XC
2582
2583 /*
835822d4 2584 * GTK subelement:
d32d94db 2585 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
835822d4 2586 * Key[5..32]
d32d94db 2587 */
835822d4
JM
2588 *pos++ = WNM_SLEEP_SUBELEM_GTK;
2589 *pos++ = 11 + gsm->GTK_len;
d32d94db 2590 /* Key ID in B0-B1 of Key Info */
835822d4
JM
2591 WPA_PUT_LE16(pos, gsm->GN & 0x03);
2592 pos += 2;
2593 *pos++ = gsm->GTK_len;
2594 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
d32d94db 2595 return 0;
835822d4
JM
2596 pos += 8;
2597 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2598 pos += gsm->GTK_len;
d32d94db 2599
835822d4
JM
2600 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
2601 gsm->GN);
2602 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
d32d94db 2603 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
d32d94db 2604
835822d4 2605 return pos - start;
d32d94db
XC
2606}
2607
2608
2609#ifdef CONFIG_IEEE80211W
2610int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2611{
d32d94db 2612 struct wpa_group *gsm = sm->group;
835822d4 2613 u8 *start = pos;
8dd9f9cd 2614 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
d32d94db 2615
835822d4
JM
2616 /*
2617 * IGTK subelement:
2618 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
2619 */
2620 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
8dd9f9cd 2621 *pos++ = 2 + 6 + len;
835822d4
JM
2622 WPA_PUT_LE16(pos, gsm->GN_igtk);
2623 pos += 2;
2624 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
d32d94db 2625 return 0;
835822d4 2626 pos += 6;
d32d94db 2627
8dd9f9cd
JM
2628 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
2629 pos += len;
d32d94db 2630
835822d4
JM
2631 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
2632 gsm->GN_igtk);
2633 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
8dd9f9cd 2634 gsm->IGTK[gsm->GN_igtk - 4], len);
d32d94db 2635
835822d4 2636 return pos - start;
d32d94db
XC
2637}
2638#endif /* CONFIG_IEEE80211W */
ad3872a3 2639#endif /* CONFIG_WNM */
d32d94db
XC
2640
2641
6fc6879b
JM
2642static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2643 struct wpa_group *group)
2644{
2645 int tmp;
2646
2647 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2648 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2649 group->changed = TRUE;
2650 group->wpa_group_state = WPA_GROUP_SETKEYS;
2651 group->GTKReKey = FALSE;
2652 tmp = group->GM;
2653 group->GM = group->GN;
2654 group->GN = tmp;
2655#ifdef CONFIG_IEEE80211W
2656 tmp = group->GM_igtk;
2657 group->GM_igtk = group->GN_igtk;
2658 group->GN_igtk = tmp;
2659#endif /* CONFIG_IEEE80211W */
2660 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2661 * counting the STAs that are marked with GUpdateStationKeys instead of
2662 * including all STAs that could be in not-yet-completed state. */
2663 wpa_gtk_update(wpa_auth, group);
2664
3c183894
JM
2665 if (group->GKeyDoneStations) {
2666 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
2667 "GKeyDoneStations=%d when starting new GTK rekey",
2668 group->GKeyDoneStations);
2669 group->GKeyDoneStations = 0;
2670 }
473b6f22 2671 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
6fc6879b
JM
2672 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2673 group->GKeyDoneStations);
2674}
2675
2676
1bdb7ab3
JM
2677static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
2678 struct wpa_group *group)
6fc6879b 2679{
3c7302c2
JM
2680 int ret = 0;
2681
3c7302c2 2682 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
c3550295 2683 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
0382097e
JM
2684 broadcast_ether_addr, group->GN,
2685 group->GTK[group->GN - 1], group->GTK_len) < 0)
3c7302c2 2686 ret = -1;
6fc6879b
JM
2687
2688#ifdef CONFIG_IEEE80211W
8dd9f9cd
JM
2689 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2690 enum wpa_alg alg;
2691 size_t len;
2692
2693 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
2694 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2695
2696 if (ret == 0 &&
2697 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
2698 broadcast_ether_addr, group->GN_igtk,
2699 group->IGTK[group->GN_igtk - 4], len) < 0)
2700 ret = -1;
2701 }
6fc6879b 2702#endif /* CONFIG_IEEE80211W */
3c7302c2
JM
2703
2704 return ret;
6fc6879b
JM
2705}
2706
2707
7d7f7be2
JM
2708static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
2709{
2710 if (sm->group == ctx) {
2711 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
2712 " for discconnection due to fatal failure",
2713 MAC2STR(sm->addr));
2714 sm->Disconnect = TRUE;
2715 }
2716
2717 return 0;
2718}
2719
2720
2721static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
2722 struct wpa_group *group)
2723{
2724 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
2725 group->changed = TRUE;
2726 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
2727 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
2728}
2729
2730
1bdb7ab3
JM
2731static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2732 struct wpa_group *group)
2733{
2734 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2735 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2736 group->changed = TRUE;
2737 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2738
7d7f7be2
JM
2739 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
2740 wpa_group_fatal_failure(wpa_auth, group);
1bdb7ab3 2741 return -1;
7d7f7be2 2742 }
1bdb7ab3
JM
2743
2744 return 0;
2745}
2746
2747
6fc6879b
JM
2748static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2749 struct wpa_group *group)
2750{
2751 if (group->GInit) {
2752 wpa_group_gtk_init(wpa_auth, group);
7d7f7be2
JM
2753 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
2754 /* Do not allow group operations */
6fc6879b
JM
2755 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2756 group->GTKAuthenticator) {
2757 wpa_group_setkeysdone(wpa_auth, group);
2758 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2759 group->GTKReKey) {
2760 wpa_group_setkeys(wpa_auth, group);
2761 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2762 if (group->GKeyDoneStations == 0)
2763 wpa_group_setkeysdone(wpa_auth, group);
2764 else if (group->GTKReKey)
2765 wpa_group_setkeys(wpa_auth, group);
2766 }
2767}
2768
2769
e4a6ea1d 2770static int wpa_sm_step(struct wpa_state_machine *sm)
6fc6879b
JM
2771{
2772 if (sm == NULL)
e4a6ea1d 2773 return 0;
6fc6879b
JM
2774
2775 if (sm->in_step_loop) {
2776 /* This should not happen, but if it does, make sure we do not
2777 * end up freeing the state machine too early by exiting the
2778 * recursive call. */
2779 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
e4a6ea1d 2780 return 0;
6fc6879b
JM
2781 }
2782
2783 sm->in_step_loop = 1;
2784 do {
2785 if (sm->pending_deinit)
2786 break;
2787
2788 sm->changed = FALSE;
2789 sm->wpa_auth->group->changed = FALSE;
2790
2791 SM_STEP_RUN(WPA_PTK);
2792 if (sm->pending_deinit)
2793 break;
2794 SM_STEP_RUN(WPA_PTK_GROUP);
2795 if (sm->pending_deinit)
2796 break;
2797 wpa_group_sm_step(sm->wpa_auth, sm->group);
2798 } while (sm->changed || sm->wpa_auth->group->changed);
2799 sm->in_step_loop = 0;
2800
2801 if (sm->pending_deinit) {
2802 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2803 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2804 wpa_free_sta_sm(sm);
e4a6ea1d 2805 return 1;
6fc6879b 2806 }
e4a6ea1d 2807 return 0;
6fc6879b
JM
2808}
2809
2810
2811static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2812{
2813 struct wpa_state_machine *sm = eloop_ctx;
2814 wpa_sm_step(sm);
2815}
2816
2817
2818void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2819{
2820 if (sm == NULL)
2821 return;
2822 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2823}
2824
2825
2826void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2827{
2828 int tmp, i;
2829 struct wpa_group *group;
2830
2831 if (wpa_auth == NULL)
2832 return;
2833
2834 group = wpa_auth->group;
2835
2836 for (i = 0; i < 2; i++) {
2837 tmp = group->GM;
2838 group->GM = group->GN;
2839 group->GN = tmp;
2840#ifdef CONFIG_IEEE80211W
2841 tmp = group->GM_igtk;
2842 group->GM_igtk = group->GN_igtk;
2843 group->GN_igtk = tmp;
2844#endif /* CONFIG_IEEE80211W */
2845 wpa_gtk_update(wpa_auth, group);
9354e594 2846 wpa_group_config_group_keys(wpa_auth, group);
6fc6879b
JM
2847 }
2848}
2849
2850
2851static const char * wpa_bool_txt(int bool)
2852{
2853 return bool ? "TRUE" : "FALSE";
2854}
2855
2856
6fc6879b
JM
2857#define RSN_SUITE "%02x-%02x-%02x-%d"
2858#define RSN_SUITE_ARG(s) \
2859((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2860
2861int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2862{
2863 int len = 0, ret;
2864 char pmkid_txt[PMKID_LEN * 2 + 1];
8ce58ceb
IH
2865#ifdef CONFIG_RSN_PREAUTH
2866 const int preauth = 1;
2867#else /* CONFIG_RSN_PREAUTH */
2868 const int preauth = 0;
2869#endif /* CONFIG_RSN_PREAUTH */
6fc6879b
JM
2870
2871 if (wpa_auth == NULL)
2872 return len;
2873
2874 ret = os_snprintf(buf + len, buflen - len,
2875 "dot11RSNAOptionImplemented=TRUE\n"
8ce58ceb 2876 "dot11RSNAPreauthenticationImplemented=%s\n"
6fc6879b
JM
2877 "dot11RSNAEnabled=%s\n"
2878 "dot11RSNAPreauthenticationEnabled=%s\n",
8ce58ceb 2879 wpa_bool_txt(preauth),
6fc6879b
JM
2880 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2881 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2882 if (ret < 0 || (size_t) ret >= buflen - len)
2883 return len;
2884 len += ret;
2885
2886 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2887 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2888
2889 ret = os_snprintf(
2890 buf + len, buflen - len,
2891 "dot11RSNAConfigVersion=%u\n"
2892 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2893 /* FIX: dot11RSNAConfigGroupCipher */
2894 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2895 /* FIX: dot11RSNAConfigGroupRekeyTime */
2896 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2897 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2898 "dot11RSNAConfigGroupUpdateCount=%u\n"
2899 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2900 "dot11RSNAConfigGroupCipherSize=%u\n"
2901 "dot11RSNAConfigPMKLifetime=%u\n"
2902 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2903 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2904 "dot11RSNAConfigSATimeout=%u\n"
2905 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2906 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2907 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2908 "dot11RSNAPMKIDUsed=%s\n"
2909 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2910 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2911 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2912 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2913 "dot11RSNA4WayHandshakeFailures=%u\n"
2914 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2915 RSN_VERSION,
2916 !!wpa_auth->conf.wpa_strict_rekey,
2917 dot11RSNAConfigGroupUpdateCount,
2918 dot11RSNAConfigPairwiseUpdateCount,
c3550295 2919 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
6fc6879b
JM
2920 dot11RSNAConfigPMKLifetime,
2921 dot11RSNAConfigPMKReauthThreshold,
2922 dot11RSNAConfigSATimeout,
2923 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2924 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2925 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2926 pmkid_txt,
2927 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2928 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2929 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2930 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2931 wpa_auth->dot11RSNA4WayHandshakeFailures);
2932 if (ret < 0 || (size_t) ret >= buflen - len)
2933 return len;
2934 len += ret;
2935
2936 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2937 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2938
2939 /* Private MIB */
2940 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2941 wpa_auth->group->wpa_group_state);
2942 if (ret < 0 || (size_t) ret >= buflen - len)
2943 return len;
2944 len += ret;
2945
2946 return len;
2947}
2948
2949
2950int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2951{
2952 int len = 0, ret;
2953 u32 pairwise = 0;
2954
2955 if (sm == NULL)
2956 return 0;
2957
2958 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2959
2960 /* dot11RSNAStatsEntry */
2961
c3550295
JM
2962 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
2963 WPA_PROTO_RSN : WPA_PROTO_WPA,
2964 sm->pairwise);
2965 if (pairwise == 0)
6fc6879b
JM
2966 return 0;
2967
2968 ret = os_snprintf(
2969 buf + len, buflen - len,
2970 /* TODO: dot11RSNAStatsIndex */
2971 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2972 "dot11RSNAStatsVersion=1\n"
2973 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2974 /* TODO: dot11RSNAStatsTKIPICVErrors */
2975 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
d9040cdb 2976 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
6fc6879b
JM
2977 /* TODO: dot11RSNAStatsCCMPReplays */
2978 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2979 /* TODO: dot11RSNAStatsTKIPReplays */,
2980 MAC2STR(sm->addr),
2981 RSN_SUITE_ARG(pairwise),
2982 sm->dot11RSNAStatsTKIPLocalMICFailures,
2983 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2984 if (ret < 0 || (size_t) ret >= buflen - len)
2985 return len;
2986 len += ret;
2987
2988 /* Private MIB */
2989 ret = os_snprintf(buf + len, buflen - len,
2990 "hostapdWPAPTKState=%d\n"
2991 "hostapdWPAPTKGroupState=%d\n",
2992 sm->wpa_ptk_state,
2993 sm->wpa_ptk_group_state);
2994 if (ret < 0 || (size_t) ret >= buflen - len)
2995 return len;
2996 len += ret;
2997
2998 return len;
2999}
3000
3001
3002void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
3003{
3004 if (wpa_auth)
3005 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
3006}
3007
3008
3009int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
3010{
3011 return sm && sm->pairwise_set;
3012}
3013
3014
ff36ff00
JM
3015int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
3016{
3017 return sm->pairwise;
3018}
3019
3020
6fc6879b
JM
3021int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
3022{
3023 if (sm == NULL)
3024 return -1;
3025 return sm->wpa_key_mgmt;
3026}
3027
3028
3029int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
3030{
3031 if (sm == NULL)
3032 return 0;
3033 return sm->wpa;
3034}
3035
3036
3037int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
3038 struct rsn_pmksa_cache_entry *entry)
3039{
3040 if (sm == NULL || sm->pmksa != entry)
3041 return -1;
3042 sm->pmksa = NULL;
3043 return 0;
3044}
3045
3046
3047struct rsn_pmksa_cache_entry *
3048wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
3049{
3050 return sm ? sm->pmksa : NULL;
3051}
3052
3053
3054void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
3055{
3056 if (sm)
3057 sm->dot11RSNAStatsTKIPLocalMICFailures++;
3058}
3059
3060
3061const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
3062{
3063 if (wpa_auth == NULL)
3064 return NULL;
3065 *len = wpa_auth->wpa_ie_len;
3066 return wpa_auth->wpa_ie;
3067}
3068
3069
3070int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
3071 int session_timeout, struct eapol_state_machine *eapol)
3072{
cb465555
JM
3073 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
3074 sm->wpa_auth->conf.disable_pmksa_caching)
6fc6879b
JM
3075 return -1;
3076
4bb081f1 3077 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
087a1f4e 3078 sm->PTK.kck, sizeof(sm->PTK.kck),
4bb081f1
JM
3079 sm->wpa_auth->addr, sm->addr, session_timeout,
3080 eapol, sm->wpa_key_mgmt))
6fc6879b
JM
3081 return 0;
3082
3083 return -1;
3084}
3085
3086
3087int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
3088 const u8 *pmk, size_t len, const u8 *sta_addr,
3089 int session_timeout,
3090 struct eapol_state_machine *eapol)
3091{
3092 if (wpa_auth == NULL)
3093 return -1;
3094
087a1f4e
JM
3095 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len,
3096 NULL, 0,
3097 wpa_auth->addr,
4bb081f1
JM
3098 sta_addr, session_timeout, eapol,
3099 WPA_KEY_MGMT_IEEE8021X))
6fc6879b
JM
3100 return 0;
3101
3102 return -1;
3103}
3104
3105
f2991170
JM
3106int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
3107 const u8 *pmk)
3108{
3109 if (wpa_auth->conf.disable_pmksa_caching)
3110 return -1;
3111
3112 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN,
087a1f4e 3113 NULL, 0,
f2991170
JM
3114 wpa_auth->addr, addr, 0, NULL,
3115 WPA_KEY_MGMT_SAE))
3116 return 0;
3117
3118 return -1;
3119}
3120
3121
901d1fe1
JM
3122void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
3123 const u8 *sta_addr)
3124{
3125 struct rsn_pmksa_cache_entry *pmksa;
3126
3127 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
3128 return;
3129 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3130 if (pmksa) {
3131 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
3132 MACSTR " based on request", MAC2STR(sta_addr));
3133 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
3134 }
3135}
3136
3137
6fc6879b
JM
3138static struct wpa_group *
3139wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3140{
3141 struct wpa_group *group;
3142
3143 if (wpa_auth == NULL || wpa_auth->group == NULL)
3144 return NULL;
3145
3146 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
3147 vlan_id);
bdffdc5d 3148 group = wpa_group_init(wpa_auth, vlan_id, 0);
6fc6879b
JM
3149 if (group == NULL)
3150 return NULL;
3151
3152 group->next = wpa_auth->group->next;
3153 wpa_auth->group->next = group;
3154
3155 return group;
3156}
3157
3158
3159int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
3160{
3161 struct wpa_group *group;
3162
3163 if (sm == NULL || sm->wpa_auth == NULL)
3164 return 0;
3165
3166 group = sm->wpa_auth->group;
3167 while (group) {
3168 if (group->vlan_id == vlan_id)
3169 break;
3170 group = group->next;
3171 }
3172
3173 if (group == NULL) {
3174 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
3175 if (group == NULL)
3176 return -1;
3177 }
3178
3179 if (sm->group == group)
3180 return 0;
3181
7d7f7be2
JM
3182 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
3183 return -1;
3184
6fc6879b
JM
3185 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
3186 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
3187
3188 sm->group = group;
3189 return 0;
3190}
e4bf4db9
JM
3191
3192
3193void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
3194 struct wpa_state_machine *sm, int ack)
3195{
3196 if (wpa_auth == NULL || sm == NULL)
3197 return;
3198 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
3199 " ack=%d", MAC2STR(sm->addr), ack);
3200 if (sm->pending_1_of_4_timeout && ack) {
3201 /*
3202 * Some deployed supplicant implementations update their SNonce
3203 * for each EAPOL-Key 2/4 message even within the same 4-way
3204 * handshake and then fail to use the first SNonce when
3205 * deriving the PTK. This results in unsuccessful 4-way
3206 * handshake whenever the relatively short initial timeout is
3207 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
3208 * around this by increasing the timeout now that we know that
3209 * the station has received the frame.
3210 */
3211 int timeout_ms = eapol_key_timeout_subseq;
3212 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
3213 "timeout by %u ms because of acknowledged frame",
3214 timeout_ms);
3215 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
3216 eloop_register_timeout(timeout_ms / 1000,
3217 (timeout_ms % 1000) * 1000,
3218 wpa_send_eapol_timeout, wpa_auth, sm);
3219 }
3220}
c10347f2
JM
3221
3222
3223int wpa_auth_uses_sae(struct wpa_state_machine *sm)
3224{
3225 if (sm == NULL)
3226 return 0;
3227 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
3228}
aa189ac9
JM
3229
3230
3231int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
3232{
3233 if (sm == NULL)
3234 return 0;
3235 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
3236}
25ef8529
JM
3237
3238
3239#ifdef CONFIG_P2P
3240int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
3241{
3242 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
3243 return -1;
3244 os_memcpy(addr, sm->ip_addr, 4);
3245 return 0;
3246}
3247#endif /* CONFIG_P2P */