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