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