blob: 1905dc94efef115b5d5f02ae41110ff5fe34d988 [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
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "utils/state_machine.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080014#include "utils/bitfield.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "common/ieee802_11_defs.h"
16#include "crypto/aes_wrap.h"
17#include "crypto/crypto.h"
18#include "crypto/sha1.h"
19#include "crypto/sha256.h"
20#include "crypto/random.h"
21#include "eapol_auth/eapol_auth_sm.h"
22#include "ap_config.h"
23#include "ieee802_11.h"
24#include "wpa_auth.h"
25#include "pmksa_cache_auth.h"
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);
35static int wpa_sm_step(struct wpa_state_machine *sm);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080036static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
37 size_t data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038static 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);
41static void wpa_request_new_ptk(struct wpa_state_machine *sm);
42static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
43 struct wpa_group *group);
44static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
45 struct wpa_group *group);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080046static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
47 const u8 *pmk, struct wpa_ptk *ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048
49static const u32 dot11RSNAConfigGroupUpdateCount = 4;
50static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
51static const u32 eapol_key_timeout_first = 100; /* ms */
52static const u32 eapol_key_timeout_subseq = 1000; /* ms */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080053static const u32 eapol_key_timeout_first_group = 500; /* ms */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054
55/* TODO: make these configurable */
56static const int dot11RSNAConfigPMKLifetime = 43200;
57static const int dot11RSNAConfigPMKReauthThreshold = 70;
58static const int dot11RSNAConfigSATimeout = 60;
59
60
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080061static inline int wpa_auth_mic_failure_report(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062 struct wpa_authenticator *wpa_auth, const u8 *addr)
63{
64 if (wpa_auth->cb.mic_failure_report)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080065 return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
66 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067}
68
69
70static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
71 const u8 *addr, wpa_eapol_variable var,
72 int value)
73{
74 if (wpa_auth->cb.set_eapol)
75 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
76}
77
78
79static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
80 const u8 *addr, wpa_eapol_variable var)
81{
82 if (wpa_auth->cb.get_eapol == NULL)
83 return -1;
84 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
85}
86
87
88static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070089 const u8 *addr,
90 const u8 *p2p_dev_addr,
91 const u8 *prev_psk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092{
93 if (wpa_auth->cb.get_psk == NULL)
94 return NULL;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070095 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, p2p_dev_addr,
96 prev_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097}
98
99
100static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
101 const u8 *addr, u8 *msk, size_t *len)
102{
103 if (wpa_auth->cb.get_msk == NULL)
104 return -1;
105 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
106}
107
108
109static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
110 int vlan_id,
111 enum wpa_alg alg, const u8 *addr, int idx,
112 u8 *key, size_t key_len)
113{
114 if (wpa_auth->cb.set_key == NULL)
115 return -1;
116 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
117 key, key_len);
118}
119
120
121static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
122 const u8 *addr, int idx, u8 *seq)
123{
124 if (wpa_auth->cb.get_seqnum == NULL)
125 return -1;
126 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
127}
128
129
130static inline int
131wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
132 const u8 *data, size_t data_len, int encrypt)
133{
134 if (wpa_auth->cb.send_eapol == NULL)
135 return -1;
136 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
137 encrypt);
138}
139
140
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800141#ifdef CONFIG_MESH
142static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
143 const u8 *addr)
144{
145 if (wpa_auth->cb.start_ampe == NULL)
146 return -1;
147 return wpa_auth->cb.start_ampe(wpa_auth->cb.ctx, addr);
148}
149#endif /* CONFIG_MESH */
150
151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
153 int (*cb)(struct wpa_state_machine *sm, void *ctx),
154 void *cb_ctx)
155{
156 if (wpa_auth->cb.for_each_sta == NULL)
157 return 0;
158 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
159}
160
161
162int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
163 int (*cb)(struct wpa_authenticator *a, void *ctx),
164 void *cb_ctx)
165{
166 if (wpa_auth->cb.for_each_auth == NULL)
167 return 0;
168 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
169}
170
171
172void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
173 logger_level level, const char *txt)
174{
175 if (wpa_auth->cb.logger == NULL)
176 return;
177 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
178}
179
180
181void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
182 logger_level level, const char *fmt, ...)
183{
184 char *format;
185 int maxlen;
186 va_list ap;
187
188 if (wpa_auth->cb.logger == NULL)
189 return;
190
191 maxlen = os_strlen(fmt) + 100;
192 format = os_malloc(maxlen);
193 if (!format)
194 return;
195
196 va_start(ap, fmt);
197 vsnprintf(format, maxlen, fmt, ap);
198 va_end(ap);
199
200 wpa_auth_logger(wpa_auth, addr, level, format);
201
202 os_free(format);
203}
204
205
206static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
207 const u8 *addr)
208{
209 if (wpa_auth->cb.disconnect == NULL)
210 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800211 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
213 WLAN_REASON_PREV_AUTH_NOT_VALID);
214}
215
216
217static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
218{
219 int ret = 0;
220#ifdef CONFIG_IEEE80211R
221 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
222 ret = 1;
223#endif /* CONFIG_IEEE80211R */
224#ifdef CONFIG_IEEE80211W
225 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
226 ret = 1;
227#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800228 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
229 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 return ret;
231}
232
233
234static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
235{
236 struct wpa_authenticator *wpa_auth = eloop_ctx;
237
238 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
239 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
240 "initialization.");
241 } else {
242 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
243 wpa_hexdump_key(MSG_DEBUG, "GMK",
244 wpa_auth->group->GMK, WPA_GMK_LEN);
245 }
246
247 if (wpa_auth->conf.wpa_gmk_rekey) {
248 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
249 wpa_rekey_gmk, wpa_auth, NULL);
250 }
251}
252
253
254static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
255{
256 struct wpa_authenticator *wpa_auth = eloop_ctx;
257 struct wpa_group *group;
258
259 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
260 for (group = wpa_auth->group; group; group = group->next) {
261 group->GTKReKey = TRUE;
262 do {
263 group->changed = FALSE;
264 wpa_group_sm_step(wpa_auth, group);
265 } while (group->changed);
266 }
267
268 if (wpa_auth->conf.wpa_group_rekey) {
269 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
270 0, wpa_rekey_gtk, wpa_auth, NULL);
271 }
272}
273
274
275static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
276{
277 struct wpa_authenticator *wpa_auth = eloop_ctx;
278 struct wpa_state_machine *sm = timeout_ctx;
279
280 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
281 wpa_request_new_ptk(sm);
282 wpa_sm_step(sm);
283}
284
285
286static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
287{
288 if (sm->pmksa == ctx)
289 sm->pmksa = NULL;
290 return 0;
291}
292
293
294static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
295 void *ctx)
296{
297 struct wpa_authenticator *wpa_auth = ctx;
298 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
299}
300
301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
303 struct wpa_group *group)
304{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800305 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306 u8 rkey[32];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800307 unsigned long ptr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308
309 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
310 return -1;
311 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
312
313 /*
314 * Counter = PRF-256(Random number, "Init Counter",
315 * Local MAC Address || Time)
316 */
317 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
318 wpa_get_ntp_timestamp(buf + ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800319 ptr = (unsigned long) group;
320 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
322 return -1;
323
324 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
325 group->Counter, WPA_NONCE_LEN) < 0)
326 return -1;
327 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
328 group->Counter, WPA_NONCE_LEN);
329
330 return 0;
331}
332
333
334static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800335 int vlan_id, int delay_init)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336{
337 struct wpa_group *group;
338
339 group = os_zalloc(sizeof(struct wpa_group));
340 if (group == NULL)
341 return NULL;
342
343 group->GTKAuthenticator = TRUE;
344 group->vlan_id = vlan_id;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700345 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346
347 if (random_pool_ready() != 1) {
348 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
349 "for secure operations - update keys later when "
350 "the first station connects");
351 }
352
353 /*
354 * Set initial GMK/Counter value here. The actual values that will be
355 * used in negotiations will be set once the first station tries to
356 * connect. This allows more time for collecting additional randomness
357 * on embedded devices.
358 */
359 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
360 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
361 "initialization.");
362 os_free(group);
363 return NULL;
364 }
365
366 group->GInit = TRUE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800367 if (delay_init) {
368 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
369 "until Beacon frames have been configured");
370 /* Initialization is completed in wpa_init_keys(). */
371 } else {
372 wpa_group_sm_step(wpa_auth, group);
373 group->GInit = FALSE;
374 wpa_group_sm_step(wpa_auth, group);
375 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376
377 return group;
378}
379
380
381/**
382 * wpa_init - Initialize WPA authenticator
383 * @addr: Authenticator address
384 * @conf: Configuration for WPA authenticator
385 * @cb: Callback functions for WPA authenticator
386 * Returns: Pointer to WPA authenticator data or %NULL on failure
387 */
388struct wpa_authenticator * wpa_init(const u8 *addr,
389 struct wpa_auth_config *conf,
390 struct wpa_auth_callbacks *cb)
391{
392 struct wpa_authenticator *wpa_auth;
393
394 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
395 if (wpa_auth == NULL)
396 return NULL;
397 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
398 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
399 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
400
401 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
402 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
403 os_free(wpa_auth);
404 return NULL;
405 }
406
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800407 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 if (wpa_auth->group == NULL) {
409 os_free(wpa_auth->wpa_ie);
410 os_free(wpa_auth);
411 return NULL;
412 }
413
414 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
415 wpa_auth);
416 if (wpa_auth->pmksa == NULL) {
417 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800418 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 os_free(wpa_auth->wpa_ie);
420 os_free(wpa_auth);
421 return NULL;
422 }
423
424#ifdef CONFIG_IEEE80211R
425 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
426 if (wpa_auth->ft_pmk_cache == NULL) {
427 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800428 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 os_free(wpa_auth->wpa_ie);
430 pmksa_cache_auth_deinit(wpa_auth->pmksa);
431 os_free(wpa_auth);
432 return NULL;
433 }
434#endif /* CONFIG_IEEE80211R */
435
436 if (wpa_auth->conf.wpa_gmk_rekey) {
437 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
438 wpa_rekey_gmk, wpa_auth, NULL);
439 }
440
441 if (wpa_auth->conf.wpa_group_rekey) {
442 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
443 wpa_rekey_gtk, wpa_auth, NULL);
444 }
445
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800446#ifdef CONFIG_P2P
447 if (WPA_GET_BE32(conf->ip_addr_start)) {
448 int count = WPA_GET_BE32(conf->ip_addr_end) -
449 WPA_GET_BE32(conf->ip_addr_start) + 1;
450 if (count > 1000)
451 count = 1000;
452 if (count > 0)
453 wpa_auth->ip_pool = bitfield_alloc(count);
454 }
455#endif /* CONFIG_P2P */
456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 return wpa_auth;
458}
459
460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800461int wpa_init_keys(struct wpa_authenticator *wpa_auth)
462{
463 struct wpa_group *group = wpa_auth->group;
464
465 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
466 "keys");
467 wpa_group_sm_step(wpa_auth, group);
468 group->GInit = FALSE;
469 wpa_group_sm_step(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800470 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
471 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800472 return 0;
473}
474
475
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476/**
477 * wpa_deinit - Deinitialize WPA authenticator
478 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
479 */
480void wpa_deinit(struct wpa_authenticator *wpa_auth)
481{
482 struct wpa_group *group, *prev;
483
484 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
485 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
486
487#ifdef CONFIG_PEERKEY
488 while (wpa_auth->stsl_negotiations)
489 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
490#endif /* CONFIG_PEERKEY */
491
492 pmksa_cache_auth_deinit(wpa_auth->pmksa);
493
494#ifdef CONFIG_IEEE80211R
495 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
496 wpa_auth->ft_pmk_cache = NULL;
497#endif /* CONFIG_IEEE80211R */
498
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800499#ifdef CONFIG_P2P
500 bitfield_free(wpa_auth->ip_pool);
501#endif /* CONFIG_P2P */
502
503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504 os_free(wpa_auth->wpa_ie);
505
506 group = wpa_auth->group;
507 while (group) {
508 prev = group;
509 group = group->next;
510 os_free(prev);
511 }
512
513 os_free(wpa_auth);
514}
515
516
517/**
518 * wpa_reconfig - Update WPA authenticator configuration
519 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
520 * @conf: Configuration for WPA authenticator
521 */
522int wpa_reconfig(struct wpa_authenticator *wpa_auth,
523 struct wpa_auth_config *conf)
524{
525 struct wpa_group *group;
526 if (wpa_auth == NULL)
527 return 0;
528
529 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
530 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
531 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
532 return -1;
533 }
534
535 /*
536 * Reinitialize GTK to make sure it is suitable for the new
537 * configuration.
538 */
539 group = wpa_auth->group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700540 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 group->GInit = TRUE;
542 wpa_group_sm_step(wpa_auth, group);
543 group->GInit = FALSE;
544 wpa_group_sm_step(wpa_auth, group);
545
546 return 0;
547}
548
549
550struct wpa_state_machine *
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700551wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
552 const u8 *p2p_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553{
554 struct wpa_state_machine *sm;
555
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800556 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
557 return NULL;
558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559 sm = os_zalloc(sizeof(struct wpa_state_machine));
560 if (sm == NULL)
561 return NULL;
562 os_memcpy(sm->addr, addr, ETH_ALEN);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700563 if (p2p_dev_addr)
564 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700565
566 sm->wpa_auth = wpa_auth;
567 sm->group = wpa_auth->group;
568
569 return sm;
570}
571
572
573int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
574 struct wpa_state_machine *sm)
575{
576 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
577 return -1;
578
579#ifdef CONFIG_IEEE80211R
580 if (sm->ft_completed) {
581 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
582 "FT authentication already completed - do not "
583 "start 4-way handshake");
Dmitry Shmidt71757432014-06-02 13:50:35 -0700584 /* Go to PTKINITDONE state to allow GTK rekeying */
585 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586 return 0;
587 }
588#endif /* CONFIG_IEEE80211R */
589
590 if (sm->started) {
591 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
592 sm->ReAuthenticationRequest = TRUE;
593 return wpa_sm_step(sm);
594 }
595
596 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
597 "start authentication");
598 sm->started = 1;
599
600 sm->Init = TRUE;
601 if (wpa_sm_step(sm) == 1)
602 return 1; /* should not really happen */
603 sm->Init = FALSE;
604 sm->AuthenticationRequest = TRUE;
605 return wpa_sm_step(sm);
606}
607
608
609void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
610{
611 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
612 * reassociates back to the same AP while the previous entry for the
613 * STA has not yet been removed. */
614 if (sm == NULL)
615 return;
616
617 sm->wpa_key_mgmt = 0;
618}
619
620
621static void wpa_free_sta_sm(struct wpa_state_machine *sm)
622{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800623#ifdef CONFIG_P2P
624 if (WPA_GET_BE32(sm->ip_addr)) {
625 u32 start;
626 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
627 "address %u.%u.%u.%u from " MACSTR,
628 sm->ip_addr[0], sm->ip_addr[1],
629 sm->ip_addr[2], sm->ip_addr[3],
630 MAC2STR(sm->addr));
631 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
632 bitfield_clear(sm->wpa_auth->ip_pool,
633 WPA_GET_BE32(sm->ip_addr) - start);
634 }
635#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636 if (sm->GUpdateStationKeys) {
637 sm->group->GKeyDoneStations--;
638 sm->GUpdateStationKeys = FALSE;
639 }
640#ifdef CONFIG_IEEE80211R
641 os_free(sm->assoc_resp_ftie);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700642 wpabuf_free(sm->ft_pending_req_ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643#endif /* CONFIG_IEEE80211R */
644 os_free(sm->last_rx_eapol_key);
645 os_free(sm->wpa_ie);
646 os_free(sm);
647}
648
649
650void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
651{
652 if (sm == NULL)
653 return;
654
655 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
656 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
657 "strict rekeying - force GTK rekey since STA "
658 "is leaving");
659 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
660 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
661 NULL);
662 }
663
664 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
665 sm->pending_1_of_4_timeout = 0;
666 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
667 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
668 if (sm->in_step_loop) {
669 /* Must not free state machine while wpa_sm_step() is running.
670 * Freeing will be completed in the end of wpa_sm_step(). */
671 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
672 "machine deinit for " MACSTR, MAC2STR(sm->addr));
673 sm->pending_deinit = 1;
674 } else
675 wpa_free_sta_sm(sm);
676}
677
678
679static void wpa_request_new_ptk(struct wpa_state_machine *sm)
680{
681 if (sm == NULL)
682 return;
683
684 sm->PTKRequest = TRUE;
685 sm->PTK_valid = 0;
686}
687
688
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800689static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 const u8 *replay_counter)
691{
692 int i;
693 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800694 if (!ctr[i].valid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800696 if (os_memcmp(replay_counter, ctr[i].counter,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 WPA_REPLAY_COUNTER_LEN) == 0)
698 return 1;
699 }
700 return 0;
701}
702
703
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800704static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
705 const u8 *replay_counter)
706{
707 int i;
708 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
709 if (ctr[i].valid &&
710 (replay_counter == NULL ||
711 os_memcmp(replay_counter, ctr[i].counter,
712 WPA_REPLAY_COUNTER_LEN) == 0))
713 ctr[i].valid = FALSE;
714 }
715}
716
717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718#ifdef CONFIG_IEEE80211R
719static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
720 struct wpa_state_machine *sm,
721 struct wpa_eapol_ie_parse *kde)
722{
723 struct wpa_ie_data ie;
724 struct rsn_mdie *mdie;
725
726 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
727 ie.num_pmkid != 1 || ie.pmkid == NULL) {
728 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
729 "FT 4-way handshake message 2/4");
730 return -1;
731 }
732
733 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
734 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
735 sm->sup_pmk_r1_name, PMKID_LEN);
736
737 if (!kde->mdie || !kde->ftie) {
738 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
739 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
740 return -1;
741 }
742
743 mdie = (struct rsn_mdie *) (kde->mdie + 2);
744 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
745 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
746 MOBILITY_DOMAIN_ID_LEN) != 0) {
747 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
748 return -1;
749 }
750
751 if (sm->assoc_resp_ftie &&
752 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
753 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
754 2 + sm->assoc_resp_ftie[1]) != 0)) {
755 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
756 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
757 kde->ftie, kde->ftie_len);
758 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
759 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
760 return -1;
761 }
762
763 return 0;
764}
765#endif /* CONFIG_IEEE80211R */
766
767
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800768static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
769 struct wpa_state_machine *sm, int group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800770{
771 /* Supplicant reported a Michael MIC error */
772 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
773 "received EAPOL-Key Error Request "
774 "(STA detected Michael MIC failure (group=%d))",
775 group);
776
777 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
778 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
779 "ignore Michael MIC failure report since "
780 "group cipher is not TKIP");
781 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
782 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
783 "ignore Michael MIC failure report since "
784 "pairwise cipher is not TKIP");
785 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800786 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
787 return 1; /* STA entry was removed */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800788 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
789 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
790 }
791
792 /*
793 * Error report is not a request for a new key handshake, but since
794 * Authenticator may do it, let's change the keys now anyway.
795 */
796 wpa_request_new_ptk(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800797 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798}
799
800
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800801static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
802 size_t data_len)
803{
804 struct wpa_ptk PTK;
805 int ok = 0;
806 const u8 *pmk = NULL;
807
808 for (;;) {
809 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
810 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
811 sm->p2p_dev_addr, pmk);
812 if (pmk == NULL)
813 break;
814 } else
815 pmk = sm->PMK;
816
817 wpa_derive_ptk(sm, sm->alt_SNonce, pmk, &PTK);
818
819 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len)
820 == 0) {
821 ok = 1;
822 break;
823 }
824
825 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
826 break;
827 }
828
829 if (!ok) {
830 wpa_printf(MSG_DEBUG,
831 "WPA: Earlier SNonce did not result in matching MIC");
832 return -1;
833 }
834
835 wpa_printf(MSG_DEBUG,
836 "WPA: Earlier SNonce resulted in matching MIC");
837 sm->alt_snonce_valid = 0;
838 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
839 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
840 sm->PTK_valid = TRUE;
841
842 return 0;
843}
844
845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846void wpa_receive(struct wpa_authenticator *wpa_auth,
847 struct wpa_state_machine *sm,
848 u8 *data, size_t data_len)
849{
850 struct ieee802_1x_hdr *hdr;
851 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800852 struct wpa_eapol_key_192 *key192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700853 u16 key_info, key_data_length;
854 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
855 SMK_M1, SMK_M3, SMK_ERROR } msg;
856 char *msgtxt;
857 struct wpa_eapol_ie_parse kde;
858 int ft;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800859 const u8 *eapol_key_ie, *key_data;
860 size_t eapol_key_ie_len, keyhdrlen, mic_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861
862 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
863 return;
864
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800865 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
866 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
867
868 if (data_len < sizeof(*hdr) + keyhdrlen)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 return;
870
871 hdr = (struct ieee802_1x_hdr *) data;
872 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800873 key192 = (struct wpa_eapol_key_192 *) (hdr + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800875 if (mic_len == 24) {
876 key_data = (const u8 *) (key192 + 1);
877 key_data_length = WPA_GET_BE16(key192->key_data_length);
878 } else {
879 key_data = (const u8 *) (key + 1);
880 key_data_length = WPA_GET_BE16(key->key_data_length);
881 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800882 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
883 " key_info=0x%x type=%u key_data_length=%u",
884 MAC2STR(sm->addr), key_info, key->type, key_data_length);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800885 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
887 "key_data overflow (%d > %lu)",
888 key_data_length,
889 (unsigned long) (data_len - sizeof(*hdr) -
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800890 keyhdrlen));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 return;
892 }
893
894 if (sm->wpa == WPA_VERSION_WPA2) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800895 if (key->type == EAPOL_KEY_TYPE_WPA) {
896 /*
897 * Some deployed station implementations seem to send
898 * msg 4/4 with incorrect type value in WPA2 mode.
899 */
900 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
901 "with unexpected WPA type in RSN mode");
902 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700903 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
904 "unexpected type %d in RSN mode",
905 key->type);
906 return;
907 }
908 } else {
909 if (key->type != EAPOL_KEY_TYPE_WPA) {
910 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
911 "unexpected type %d in WPA mode",
912 key->type);
913 return;
914 }
915 }
916
917 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
918 WPA_NONCE_LEN);
919 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
920 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
921
922 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
923 * are set */
924
925 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
926 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
927 if (key_info & WPA_KEY_INFO_ERROR) {
928 msg = SMK_ERROR;
929 msgtxt = "SMK Error";
930 } else {
931 msg = SMK_M1;
932 msgtxt = "SMK M1";
933 }
934 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
935 msg = SMK_M3;
936 msgtxt = "SMK M3";
937 } else if (key_info & WPA_KEY_INFO_REQUEST) {
938 msg = REQUEST;
939 msgtxt = "Request";
940 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
941 msg = GROUP_2;
942 msgtxt = "2/2 Group";
943 } else if (key_data_length == 0) {
944 msg = PAIRWISE_4;
945 msgtxt = "4/4 Pairwise";
946 } else {
947 msg = PAIRWISE_2;
948 msgtxt = "2/4 Pairwise";
949 }
950
951 /* TODO: key_info type validation for PeerKey */
952 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
953 msg == GROUP_2) {
954 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700955 if (sm->pairwise == WPA_CIPHER_CCMP ||
956 sm->pairwise == WPA_CIPHER_GCMP) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957 if (wpa_use_aes_cmac(sm) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800958 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800959 !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
961 wpa_auth_logger(wpa_auth, sm->addr,
962 LOGGER_WARNING,
963 "advertised support for "
964 "AES-128-CMAC, but did not "
965 "use it");
966 return;
967 }
968
969 if (!wpa_use_aes_cmac(sm) &&
970 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
971 wpa_auth_logger(wpa_auth, sm->addr,
972 LOGGER_WARNING,
973 "did not use HMAC-SHA1-AES "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700974 "with CCMP/GCMP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 return;
976 }
977 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800978
979 if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
980 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
981 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
982 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
983 return;
984 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 }
986
987 if (key_info & WPA_KEY_INFO_REQUEST) {
988 if (sm->req_replay_counter_used &&
989 os_memcmp(key->replay_counter, sm->req_replay_counter,
990 WPA_REPLAY_COUNTER_LEN) <= 0) {
991 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
992 "received EAPOL-Key request with "
993 "replayed counter");
994 return;
995 }
996 }
997
998 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800999 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 int i;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001001
1002 if (msg == PAIRWISE_2 &&
1003 wpa_replay_counter_valid(sm->prev_key_replay,
1004 key->replay_counter) &&
1005 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1006 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1007 {
1008 /*
1009 * Some supplicant implementations (e.g., Windows XP
1010 * WZC) update SNonce for each EAPOL-Key 2/4. This
1011 * breaks the workaround on accepting any of the
1012 * pending requests, so allow the SNonce to be updated
1013 * even if we have already sent out EAPOL-Key 3/4.
1014 */
1015 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1016 "Process SNonce update from STA "
1017 "based on retransmitted EAPOL-Key "
1018 "1/4");
1019 sm->update_snonce = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001020 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1021 sm->alt_snonce_valid = TRUE;
1022 os_memcpy(sm->alt_replay_counter,
1023 sm->key_replay[0].counter,
1024 WPA_REPLAY_COUNTER_LEN);
1025 goto continue_processing;
1026 }
1027
1028 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1029 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1030 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1031 WPA_REPLAY_COUNTER_LEN) == 0) {
1032 /*
1033 * Supplicant may still be using the old SNonce since
1034 * there was two EAPOL-Key 2/4 messages and they had
1035 * different SNonce values.
1036 */
1037 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1038 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001039 goto continue_processing;
1040 }
1041
1042 if (msg == PAIRWISE_2 &&
1043 wpa_replay_counter_valid(sm->prev_key_replay,
1044 key->replay_counter) &&
1045 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1046 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1047 "ignore retransmitted EAPOL-Key %s - "
1048 "SNonce did not change", msgtxt);
1049 } else {
1050 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1051 "received EAPOL-Key %s with "
1052 "unexpected replay counter", msgtxt);
1053 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1055 if (!sm->key_replay[i].valid)
1056 break;
1057 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1058 sm->key_replay[i].counter,
1059 WPA_REPLAY_COUNTER_LEN);
1060 }
1061 wpa_hexdump(MSG_DEBUG, "received replay counter",
1062 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1063 return;
1064 }
1065
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001066continue_processing:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001067 switch (msg) {
1068 case PAIRWISE_2:
1069 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001070 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1071 (!sm->update_snonce ||
1072 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1074 "received EAPOL-Key msg 2/4 in "
1075 "invalid state (%d) - dropped",
1076 sm->wpa_ptk_state);
1077 return;
1078 }
1079 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1080 if (sm->group->reject_4way_hs_for_entropy) {
1081 /*
1082 * The system did not have enough entropy to generate
1083 * strong random numbers. Reject the first 4-way
1084 * handshake(s) and collect some entropy based on the
1085 * information from it. Once enough entropy is
1086 * available, the next atempt will trigger GMK/Key
1087 * Counter update and the station will be allowed to
1088 * continue.
1089 */
1090 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1091 "collect more entropy for random number "
1092 "generation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093 random_mark_pool_ready();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 wpa_sta_disconnect(wpa_auth, sm->addr);
1095 return;
1096 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001097 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1099 "received EAPOL-Key msg 2/4 with "
1100 "invalid Key Data contents");
1101 return;
1102 }
1103 if (kde.rsn_ie) {
1104 eapol_key_ie = kde.rsn_ie;
1105 eapol_key_ie_len = kde.rsn_ie_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001106 } else if (kde.osen) {
1107 eapol_key_ie = kde.osen;
1108 eapol_key_ie_len = kde.osen_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109 } else {
1110 eapol_key_ie = kde.wpa_ie;
1111 eapol_key_ie_len = kde.wpa_ie_len;
1112 }
1113 ft = sm->wpa == WPA_VERSION_WPA2 &&
1114 wpa_key_mgmt_ft(sm->wpa_key_mgmt);
1115 if (sm->wpa_ie == NULL ||
1116 wpa_compare_rsn_ie(ft,
1117 sm->wpa_ie, sm->wpa_ie_len,
1118 eapol_key_ie, eapol_key_ie_len)) {
1119 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1120 "WPA IE from (Re)AssocReq did not "
1121 "match with msg 2/4");
1122 if (sm->wpa_ie) {
1123 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
1124 sm->wpa_ie, sm->wpa_ie_len);
1125 }
1126 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
1127 eapol_key_ie, eapol_key_ie_len);
1128 /* MLME-DEAUTHENTICATE.request */
1129 wpa_sta_disconnect(wpa_auth, sm->addr);
1130 return;
1131 }
1132#ifdef CONFIG_IEEE80211R
1133 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
1134 wpa_sta_disconnect(wpa_auth, sm->addr);
1135 return;
1136 }
1137#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001138#ifdef CONFIG_P2P
1139 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
1140 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
1141 int idx;
1142 wpa_printf(MSG_DEBUG, "P2P: IP address requested in "
1143 "EAPOL-Key exchange");
1144 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
1145 if (idx >= 0) {
1146 u32 start = WPA_GET_BE32(wpa_auth->conf.
1147 ip_addr_start);
1148 bitfield_set(wpa_auth->ip_pool, idx);
1149 WPA_PUT_BE32(sm->ip_addr, start + idx);
1150 wpa_printf(MSG_DEBUG, "P2P: Assigned IP "
1151 "address %u.%u.%u.%u to " MACSTR,
1152 sm->ip_addr[0], sm->ip_addr[1],
1153 sm->ip_addr[2], sm->ip_addr[3],
1154 MAC2STR(sm->addr));
1155 }
1156 }
1157#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 break;
1159 case PAIRWISE_4:
1160 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1161 !sm->PTK_valid) {
1162 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1163 "received EAPOL-Key msg 4/4 in "
1164 "invalid state (%d) - dropped",
1165 sm->wpa_ptk_state);
1166 return;
1167 }
1168 break;
1169 case GROUP_2:
1170 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1171 || !sm->PTK_valid) {
1172 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1173 "received EAPOL-Key msg 2/2 in "
1174 "invalid state (%d) - dropped",
1175 sm->wpa_ptk_group_state);
1176 return;
1177 }
1178 break;
1179#ifdef CONFIG_PEERKEY
1180 case SMK_M1:
1181 case SMK_M3:
1182 case SMK_ERROR:
1183 if (!wpa_auth->conf.peerkey) {
1184 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1185 "PeerKey use disabled - ignoring message");
1186 return;
1187 }
1188 if (!sm->PTK_valid) {
1189 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1190 "received EAPOL-Key msg SMK in "
1191 "invalid state - dropped");
1192 return;
1193 }
1194 break;
1195#else /* CONFIG_PEERKEY */
1196 case SMK_M1:
1197 case SMK_M3:
1198 case SMK_ERROR:
1199 return; /* STSL disabled - ignore SMK messages */
1200#endif /* CONFIG_PEERKEY */
1201 case REQUEST:
1202 break;
1203 }
1204
1205 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1206 "received EAPOL-Key frame (%s)", msgtxt);
1207
1208 if (key_info & WPA_KEY_INFO_ACK) {
1209 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1210 "received invalid EAPOL-Key: Key Ack set");
1211 return;
1212 }
1213
1214 if (!(key_info & WPA_KEY_INFO_MIC)) {
1215 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1216 "received invalid EAPOL-Key: Key MIC not set");
1217 return;
1218 }
1219
1220 sm->MICVerified = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001221 if (sm->PTK_valid && !sm->update_snonce) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001222 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
1223 data_len) &&
1224 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1225 wpa_try_alt_snonce(sm, data, data_len))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1227 "received EAPOL-Key with invalid MIC");
1228 return;
1229 }
1230 sm->MICVerified = TRUE;
1231 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1232 sm->pending_1_of_4_timeout = 0;
1233 }
1234
1235 if (key_info & WPA_KEY_INFO_REQUEST) {
1236 if (sm->MICVerified) {
1237 sm->req_replay_counter_used = 1;
1238 os_memcpy(sm->req_replay_counter, key->replay_counter,
1239 WPA_REPLAY_COUNTER_LEN);
1240 } else {
1241 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1242 "received EAPOL-Key request with "
1243 "invalid MIC");
1244 return;
1245 }
1246
1247 /*
1248 * TODO: should decrypt key data field if encryption was used;
1249 * even though MAC address KDE is not normally encrypted,
1250 * supplicant is allowed to encrypt it.
1251 */
1252 if (msg == SMK_ERROR) {
1253#ifdef CONFIG_PEERKEY
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001254 wpa_smk_error(wpa_auth, sm, key_data, key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255#endif /* CONFIG_PEERKEY */
1256 return;
1257 } else if (key_info & WPA_KEY_INFO_ERROR) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001258 if (wpa_receive_error_report(
1259 wpa_auth, sm,
1260 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1261 return; /* STA entry was removed */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1263 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1264 "received EAPOL-Key Request for new "
1265 "4-Way Handshake");
1266 wpa_request_new_ptk(sm);
1267#ifdef CONFIG_PEERKEY
1268 } else if (msg == SMK_M1) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001269 wpa_smk_m1(wpa_auth, sm, key, key_data,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001270 key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271#endif /* CONFIG_PEERKEY */
1272 } else if (key_data_length > 0 &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001273 wpa_parse_kde_ies(key_data, key_data_length,
1274 &kde) == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 kde.mac_addr) {
1276 } else {
1277 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1278 "received EAPOL-Key Request for GTK "
1279 "rekeying");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1281 wpa_rekey_gtk(wpa_auth, NULL);
1282 }
1283 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001284 /* Do not allow the same key replay counter to be reused. */
1285 wpa_replay_counter_mark_invalid(sm->key_replay,
1286 key->replay_counter);
1287
1288 if (msg == PAIRWISE_2) {
1289 /*
1290 * Maintain a copy of the pending EAPOL-Key frames in
1291 * case the EAPOL-Key frame was retransmitted. This is
1292 * needed to allow EAPOL-Key msg 2/4 reply to another
1293 * pending msg 1/4 to update the SNonce to work around
1294 * unexpected supplicant behavior.
1295 */
1296 os_memcpy(sm->prev_key_replay, sm->key_replay,
1297 sizeof(sm->key_replay));
1298 } else {
1299 os_memset(sm->prev_key_replay, 0,
1300 sizeof(sm->prev_key_replay));
1301 }
1302
1303 /*
1304 * Make sure old valid counters are not accepted anymore and
1305 * do not get copied again.
1306 */
1307 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308 }
1309
1310#ifdef CONFIG_PEERKEY
1311 if (msg == SMK_M3) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001312 wpa_smk_m3(wpa_auth, sm, key, key_data, key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313 return;
1314 }
1315#endif /* CONFIG_PEERKEY */
1316
1317 os_free(sm->last_rx_eapol_key);
1318 sm->last_rx_eapol_key = os_malloc(data_len);
1319 if (sm->last_rx_eapol_key == NULL)
1320 return;
1321 os_memcpy(sm->last_rx_eapol_key, data, data_len);
1322 sm->last_rx_eapol_key_len = data_len;
1323
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001324 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325 sm->EAPOLKeyReceived = TRUE;
1326 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1327 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1328 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1329 wpa_sm_step(sm);
1330}
1331
1332
1333static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1334 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1335{
1336 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
1337 u8 *pos;
1338 int ret = 0;
1339
1340 /* GTK = PRF-X(GMK, "Group key expansion",
1341 * AA || GNonce || Time || random data)
1342 * The example described in the IEEE 802.11 standard uses only AA and
1343 * GNonce as inputs here. Add some more entropy since this derivation
1344 * is done only at the Authenticator and as such, does not need to be
1345 * exactly same.
1346 */
1347 os_memcpy(data, addr, ETH_ALEN);
1348 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1349 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1350 wpa_get_ntp_timestamp(pos);
1351 pos += 8;
1352 if (random_get_bytes(pos, 16) < 0)
1353 ret = -1;
1354
1355#ifdef CONFIG_IEEE80211W
1356 sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
1357#else /* CONFIG_IEEE80211W */
1358 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
1359 < 0)
1360 ret = -1;
1361#endif /* CONFIG_IEEE80211W */
1362
1363 return ret;
1364}
1365
1366
1367static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1368{
1369 struct wpa_authenticator *wpa_auth = eloop_ctx;
1370 struct wpa_state_machine *sm = timeout_ctx;
1371
1372 sm->pending_1_of_4_timeout = 0;
1373 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1374 sm->TimeoutEvt = TRUE;
1375 wpa_sm_step(sm);
1376}
1377
1378
1379void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1380 struct wpa_state_machine *sm, int key_info,
1381 const u8 *key_rsc, const u8 *nonce,
1382 const u8 *kde, size_t kde_len,
1383 int keyidx, int encr, int force_version)
1384{
1385 struct ieee802_1x_hdr *hdr;
1386 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001387 struct wpa_eapol_key_192 *key192;
1388 size_t len, mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389 int alg;
1390 int key_data_len, pad_len = 0;
1391 u8 *buf, *pos;
1392 int version, pairwise;
1393 int i;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001394 u8 *key_data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001396 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
1397 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
1398
1399 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400
1401 if (force_version)
1402 version = force_version;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001403 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1404 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001405 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406 else if (wpa_use_aes_cmac(sm))
1407 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001408 else if (sm->pairwise != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1410 else
1411 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1412
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001413 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414
1415 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1416 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1417 "encr=%d)",
1418 version,
1419 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1420 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1421 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1422 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1423 pairwise, (unsigned long) kde_len, keyidx, encr);
1424
1425 key_data_len = kde_len;
1426
1427 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001428 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001429 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1431 pad_len = key_data_len % 8;
1432 if (pad_len)
1433 pad_len = 8 - pad_len;
1434 key_data_len += pad_len + 8;
1435 }
1436
1437 len += key_data_len;
1438
1439 hdr = os_zalloc(len);
1440 if (hdr == NULL)
1441 return;
1442 hdr->version = wpa_auth->conf.eapol_version;
1443 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1444 hdr->length = host_to_be16(len - sizeof(*hdr));
1445 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001446 key192 = (struct wpa_eapol_key_192 *) (hdr + 1);
1447 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448
1449 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1450 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1451 key_info |= version;
1452 if (encr && sm->wpa == WPA_VERSION_WPA2)
1453 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1454 if (sm->wpa != WPA_VERSION_WPA2)
1455 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1456 WPA_PUT_BE16(key->key_info, key_info);
1457
1458 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001459 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1461 WPA_PUT_BE16(key->key_length, 0);
1462
1463 /* FIX: STSL: what to use as key_replay_counter? */
1464 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1465 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1466 os_memcpy(sm->key_replay[i].counter,
1467 sm->key_replay[i - 1].counter,
1468 WPA_REPLAY_COUNTER_LEN);
1469 }
1470 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1471 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1472 WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001473 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1474 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 sm->key_replay[0].valid = TRUE;
1476
1477 if (nonce)
1478 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1479
1480 if (key_rsc)
1481 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1482
1483 if (kde && !encr) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001484 os_memcpy(key_data, kde, kde_len);
1485 if (mic_len == 24)
1486 WPA_PUT_BE16(key192->key_data_length, kde_len);
1487 else
1488 WPA_PUT_BE16(key->key_data_length, kde_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489 } else if (encr && kde) {
1490 buf = os_zalloc(key_data_len);
1491 if (buf == NULL) {
1492 os_free(hdr);
1493 return;
1494 }
1495 pos = buf;
1496 os_memcpy(pos, kde, kde_len);
1497 pos += kde_len;
1498
1499 if (pad_len)
1500 *pos++ = 0xdd;
1501
1502 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1503 buf, key_data_len);
1504 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001505 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001506 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001507 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001508 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1509 (key_data_len - 8) / 8, buf, key_data)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 os_free(hdr);
1511 os_free(buf);
1512 return;
1513 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001514 if (mic_len == 24)
1515 WPA_PUT_BE16(key192->key_data_length,
1516 key_data_len);
1517 else
1518 WPA_PUT_BE16(key->key_data_length,
1519 key_data_len);
1520 } else if (sm->PTK.kek_len == 16) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001521 u8 ek[32];
1522 os_memcpy(key->key_iv,
1523 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1524 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1525 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001526 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1527 os_memcpy(key_data, buf, key_data_len);
1528 rc4_skip(ek, 32, 256, key_data, key_data_len);
1529 if (mic_len == 24)
1530 WPA_PUT_BE16(key192->key_data_length,
1531 key_data_len);
1532 else
1533 WPA_PUT_BE16(key->key_data_length,
1534 key_data_len);
1535 } else {
1536 os_free(hdr);
1537 os_free(buf);
1538 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539 }
1540 os_free(buf);
1541 }
1542
1543 if (key_info & WPA_KEY_INFO_MIC) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001544 u8 *key_mic;
1545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546 if (!sm->PTK_valid) {
1547 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1548 "PTK not valid when sending EAPOL-Key "
1549 "frame");
1550 os_free(hdr);
1551 return;
1552 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001553
1554 key_mic = key192->key_mic; /* same offset for key and key192 */
1555 wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1556 sm->wpa_key_mgmt, version,
1557 (u8 *) hdr, len, key_mic);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001558#ifdef CONFIG_TESTING_OPTIONS
1559 if (!pairwise &&
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001560 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001561 drand48() <
1562 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1563 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1564 "Corrupting group EAPOL-Key Key MIC");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001565 key_mic[0]++;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001566 }
1567#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 }
1569
1570 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1571 1);
1572 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1573 sm->pairwise_set);
1574 os_free(hdr);
1575}
1576
1577
1578static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1579 struct wpa_state_machine *sm, int key_info,
1580 const u8 *key_rsc, const u8 *nonce,
1581 const u8 *kde, size_t kde_len,
1582 int keyidx, int encr)
1583{
1584 int timeout_ms;
1585 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1586 int ctr;
1587
1588 if (sm == NULL)
1589 return;
1590
1591 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1592 keyidx, encr, 0);
1593
1594 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1595 if (ctr == 1 && wpa_auth->conf.tx_status)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001596 timeout_ms = pairwise ? eapol_key_timeout_first :
1597 eapol_key_timeout_first_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001598 else
1599 timeout_ms = eapol_key_timeout_subseq;
1600 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1601 sm->pending_1_of_4_timeout = 1;
1602 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1603 "counter %d)", timeout_ms, ctr);
1604 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1605 wpa_send_eapol_timeout, wpa_auth, sm);
1606}
1607
1608
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001609static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
1610 size_t data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611{
1612 struct ieee802_1x_hdr *hdr;
1613 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001614 struct wpa_eapol_key_192 *key192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615 u16 key_info;
1616 int ret = 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001617 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
1618 size_t mic_len = wpa_mic_len(akmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619
1620 if (data_len < sizeof(*hdr) + sizeof(*key))
1621 return -1;
1622
1623 hdr = (struct ieee802_1x_hdr *) data;
1624 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001625 key192 = (struct wpa_eapol_key_192 *) (hdr + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001627 os_memcpy(mic, key192->key_mic, mic_len);
1628 os_memset(key192->key_mic, 0, mic_len);
1629 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1630 key_info & WPA_KEY_INFO_TYPE_MASK,
1631 data, data_len, key192->key_mic) ||
1632 os_memcmp_const(mic, key192->key_mic, mic_len) != 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633 ret = -1;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001634 os_memcpy(key192->key_mic, mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 return ret;
1636}
1637
1638
1639void wpa_remove_ptk(struct wpa_state_machine *sm)
1640{
1641 sm->PTK_valid = FALSE;
1642 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1643 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
1644 sm->pairwise_set = FALSE;
1645 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1646}
1647
1648
1649int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1650{
1651 int remove_ptk = 1;
1652
1653 if (sm == NULL)
1654 return -1;
1655
1656 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1657 "event %d notification", event);
1658
1659 switch (event) {
1660 case WPA_AUTH:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001661#ifdef CONFIG_MESH
1662 /* PTKs are derived through AMPE */
1663 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1664 /* not mesh */
1665 break;
1666 }
1667 return 0;
1668#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 case WPA_ASSOC:
1670 break;
1671 case WPA_DEAUTH:
1672 case WPA_DISASSOC:
1673 sm->DeauthenticationRequest = TRUE;
1674 break;
1675 case WPA_REAUTH:
1676 case WPA_REAUTH_EAPOL:
1677 if (!sm->started) {
1678 /*
1679 * When using WPS, we may end up here if the STA
1680 * manages to re-associate without the previous STA
1681 * entry getting removed. Consequently, we need to make
1682 * sure that the WPA state machines gets initialized
1683 * properly at this point.
1684 */
1685 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1686 "started - initialize now");
1687 sm->started = 1;
1688 sm->Init = TRUE;
1689 if (wpa_sm_step(sm) == 1)
1690 return 1; /* should not really happen */
1691 sm->Init = FALSE;
1692 sm->AuthenticationRequest = TRUE;
1693 break;
1694 }
1695 if (sm->GUpdateStationKeys) {
1696 /*
1697 * Reauthentication cancels the pending group key
1698 * update for this STA.
1699 */
1700 sm->group->GKeyDoneStations--;
1701 sm->GUpdateStationKeys = FALSE;
1702 sm->PtkGroupInit = TRUE;
1703 }
1704 sm->ReAuthenticationRequest = TRUE;
1705 break;
1706 case WPA_ASSOC_FT:
1707#ifdef CONFIG_IEEE80211R
1708 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1709 "after association");
1710 wpa_ft_install_ptk(sm);
1711
1712 /* Using FT protocol, not WPA auth state machine */
1713 sm->ft_completed = 1;
1714 return 0;
1715#else /* CONFIG_IEEE80211R */
1716 break;
1717#endif /* CONFIG_IEEE80211R */
1718 }
1719
1720#ifdef CONFIG_IEEE80211R
1721 sm->ft_completed = 0;
1722#endif /* CONFIG_IEEE80211R */
1723
1724#ifdef CONFIG_IEEE80211W
1725 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1726 remove_ptk = 0;
1727#endif /* CONFIG_IEEE80211W */
1728
1729 if (remove_ptk) {
1730 sm->PTK_valid = FALSE;
1731 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1732
1733 if (event != WPA_REAUTH_EAPOL)
1734 wpa_remove_ptk(sm);
1735 }
1736
1737 return wpa_sm_step(sm);
1738}
1739
1740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741SM_STATE(WPA_PTK, INITIALIZE)
1742{
1743 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1744 if (sm->Init) {
1745 /* Init flag is not cleared here, so avoid busy
1746 * loop by claiming nothing changed. */
1747 sm->changed = FALSE;
1748 }
1749
1750 sm->keycount = 0;
1751 if (sm->GUpdateStationKeys)
1752 sm->group->GKeyDoneStations--;
1753 sm->GUpdateStationKeys = FALSE;
1754 if (sm->wpa == WPA_VERSION_WPA)
1755 sm->PInitAKeys = FALSE;
1756 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1757 * Local AA > Remote AA)) */) {
1758 sm->Pair = TRUE;
1759 }
1760 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1761 wpa_remove_ptk(sm);
1762 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1763 sm->TimeoutCtr = 0;
1764 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1765 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1766 WPA_EAPOL_authorized, 0);
1767 }
1768}
1769
1770
1771SM_STATE(WPA_PTK, DISCONNECT)
1772{
1773 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1774 sm->Disconnect = FALSE;
1775 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1776}
1777
1778
1779SM_STATE(WPA_PTK, DISCONNECTED)
1780{
1781 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1782 sm->DeauthenticationRequest = FALSE;
1783}
1784
1785
1786SM_STATE(WPA_PTK, AUTHENTICATION)
1787{
1788 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1789 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1790 sm->PTK_valid = FALSE;
1791 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1792 1);
1793 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1794 sm->AuthenticationRequest = FALSE;
1795}
1796
1797
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001798static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1799 struct wpa_group *group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001801 if (group->first_sta_seen)
1802 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803 /*
1804 * System has run bit further than at the time hostapd was started
1805 * potentially very early during boot up. This provides better chances
1806 * of collecting more randomness on embedded systems. Re-initialize the
1807 * GMK and Counter here to improve their strength if there was not
1808 * enough entropy available immediately after system startup.
1809 */
1810 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1811 "station");
1812 if (random_pool_ready() != 1) {
1813 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1814 "to proceed - reject first 4-way handshake");
1815 group->reject_4way_hs_for_entropy = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001816 } else {
1817 group->first_sta_seen = TRUE;
1818 group->reject_4way_hs_for_entropy = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821 wpa_group_init_gmk_and_counter(wpa_auth, group);
1822 wpa_gtk_update(wpa_auth, group);
1823 wpa_group_config_group_keys(wpa_auth, group);
1824}
1825
1826
1827SM_STATE(WPA_PTK, AUTHENTICATION2)
1828{
1829 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1830
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001831 wpa_group_ensure_init(sm->wpa_auth, sm->group);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001832 sm->ReAuthenticationRequest = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833
Dmitry Shmidt04949592012-07-19 12:16:46 -07001834 /*
1835 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1836 * ambiguous. The Authenticator state machine uses a counter that is
1837 * incremented by one for each 4-way handshake. However, the security
1838 * analysis of 4-way handshake points out that unpredictable nonces
1839 * help in preventing precomputation attacks. Instead of the state
1840 * machine definition, use an unpredictable nonce value here to provide
1841 * stronger protection against potential precomputation attacks.
1842 */
1843 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1844 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1845 "ANonce.");
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001846 sm->Disconnect = TRUE;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001847 return;
1848 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001849 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1850 WPA_NONCE_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1852 * logical place than INITIALIZE since AUTHENTICATION2 can be
1853 * re-entered on ReAuthenticationRequest without going through
1854 * INITIALIZE. */
1855 sm->TimeoutCtr = 0;
1856}
1857
1858
1859SM_STATE(WPA_PTK, INITPMK)
1860{
1861 u8 msk[2 * PMK_LEN];
1862 size_t len = 2 * PMK_LEN;
1863
1864 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1865#ifdef CONFIG_IEEE80211R
1866 sm->xxkey_len = 0;
1867#endif /* CONFIG_IEEE80211R */
1868 if (sm->pmksa) {
1869 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1870 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1871 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1872 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1873 "(len=%lu)", (unsigned long) len);
1874 os_memcpy(sm->PMK, msk, PMK_LEN);
1875#ifdef CONFIG_IEEE80211R
1876 if (len >= 2 * PMK_LEN) {
1877 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1878 sm->xxkey_len = PMK_LEN;
1879 }
1880#endif /* CONFIG_IEEE80211R */
1881 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001882 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
1883 sm->wpa_auth->cb.get_msk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884 }
1885
1886 sm->req_replay_counter_used = 0;
1887 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1888 * will break reauthentication since EAPOL state machines may not be
1889 * get into AUTHENTICATING state that clears keyRun before WPA state
1890 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1891 * state and takes PMK from the previously used AAA Key. This will
1892 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1893 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1894 * be good workaround for this issue. */
1895 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1896}
1897
1898
1899SM_STATE(WPA_PTK, INITPSK)
1900{
1901 const u8 *psk;
1902 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001903 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 if (psk) {
1905 os_memcpy(sm->PMK, psk, PMK_LEN);
1906#ifdef CONFIG_IEEE80211R
1907 os_memcpy(sm->xxkey, psk, PMK_LEN);
1908 sm->xxkey_len = PMK_LEN;
1909#endif /* CONFIG_IEEE80211R */
1910 }
1911 sm->req_replay_counter_used = 0;
1912}
1913
1914
1915SM_STATE(WPA_PTK, PTKSTART)
1916{
1917 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1918 size_t pmkid_len = 0;
1919
1920 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1921 sm->PTKRequest = FALSE;
1922 sm->TimeoutEvt = FALSE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001923 sm->alt_snonce_valid = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924
1925 sm->TimeoutCtr++;
1926 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1927 /* No point in sending the EAPOL-Key - we will disconnect
1928 * immediately following this. */
1929 return;
1930 }
1931
1932 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1933 "sending 1/4 msg of 4-Way Handshake");
1934 /*
1935 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1936 * one possible PSK for this STA.
1937 */
1938 if (sm->wpa == WPA_VERSION_WPA2 &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001939 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1940 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 pmkid = buf;
1942 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1943 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1944 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1945 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001946 if (sm->pmksa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1948 sm->pmksa->pmkid, PMKID_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001949 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
1950 /* No KCK available to derive PMKID */
1951 pmkid = NULL;
1952 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 /*
1954 * Calculate PMKID since no PMKSA cache entry was
1955 * available with pre-calculated PMKID.
1956 */
1957 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1958 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1959 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1960 }
1961 }
1962 wpa_send_eapol(sm->wpa_auth, sm,
1963 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1964 sm->ANonce, pmkid, pmkid_len, 0, 0);
1965}
1966
1967
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001968static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
1969 const u8 *pmk, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971#ifdef CONFIG_IEEE80211R
1972 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001973 return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974#endif /* CONFIG_IEEE80211R */
1975
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001976 return wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1977 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
1978 ptk, sm->wpa_key_mgmt, sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979}
1980
1981
1982SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1983{
1984 struct wpa_ptk PTK;
1985 int ok = 0;
1986 const u8 *pmk = NULL;
1987
1988 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1989 sm->EAPOLKeyReceived = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001990 sm->update_snonce = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991
1992 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1993 * WPA-PSK: iterate through possible PSKs and select the one matching
1994 * the packet */
1995 for (;;) {
1996 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001997 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
1998 sm->p2p_dev_addr, pmk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 if (pmk == NULL)
2000 break;
2001 } else
2002 pmk = sm->PMK;
2003
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002004 wpa_derive_ptk(sm, sm->SNonce, pmk, &PTK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002006 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
2007 sm->last_rx_eapol_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008 sm->last_rx_eapol_key_len) == 0) {
2009 ok = 1;
2010 break;
2011 }
2012
2013 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
2014 break;
2015 }
2016
2017 if (!ok) {
2018 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2019 "invalid MIC in msg 2/4 of 4-Way Handshake");
2020 return;
2021 }
2022
2023#ifdef CONFIG_IEEE80211R
2024 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2025 /*
2026 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2027 * with the value we derived.
2028 */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002029 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
2030 WPA_PMK_NAME_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2032 "PMKR1Name mismatch in FT 4-way "
2033 "handshake");
2034 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
2035 "Supplicant",
2036 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
2037 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2038 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2039 return;
2040 }
2041 }
2042#endif /* CONFIG_IEEE80211R */
2043
2044 sm->pending_1_of_4_timeout = 0;
2045 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
2046
2047 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2048 /* PSK may have changed from the previous choice, so update
2049 * state machine data based on whatever PSK was selected here.
2050 */
2051 os_memcpy(sm->PMK, pmk, PMK_LEN);
2052 }
2053
2054 sm->MICVerified = TRUE;
2055
2056 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
2057 sm->PTK_valid = TRUE;
2058}
2059
2060
2061SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2062{
2063 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2064 sm->TimeoutCtr = 0;
2065}
2066
2067
2068#ifdef CONFIG_IEEE80211W
2069
2070static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2071{
2072 if (sm->mgmt_frame_prot) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002073 size_t len;
2074 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2075 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076 }
2077
2078 return 0;
2079}
2080
2081
2082static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2083{
2084 struct wpa_igtk_kde igtk;
2085 struct wpa_group *gsm = sm->group;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002086 u8 rsc[WPA_KEY_RSC_LEN];
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002087 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002088
2089 if (!sm->mgmt_frame_prot)
2090 return pos;
2091
2092 igtk.keyid[0] = gsm->GN_igtk;
2093 igtk.keyid[1] = 0;
2094 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002095 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096 os_memset(igtk.pn, 0, sizeof(igtk.pn));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002097 else
2098 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002099 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002100 if (sm->wpa_auth->conf.disable_gtk) {
2101 /*
2102 * Provide unique random IGTK to each STA to prevent use of
2103 * IGTK in the BSS.
2104 */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002105 if (random_get_bytes(igtk.igtk, len) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002106 return pos;
2107 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002109 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
2110 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111
2112 return pos;
2113}
2114
2115#else /* CONFIG_IEEE80211W */
2116
2117static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2118{
2119 return 0;
2120}
2121
2122
2123static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2124{
2125 return pos;
2126}
2127
2128#endif /* CONFIG_IEEE80211W */
2129
2130
2131SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2132{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002133 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134 size_t gtk_len, kde_len;
2135 struct wpa_group *gsm = sm->group;
2136 u8 *wpa_ie;
2137 int wpa_ie_len, secure, keyidx, encr = 0;
2138
2139 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2140 sm->TimeoutEvt = FALSE;
2141
2142 sm->TimeoutCtr++;
2143 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
2144 /* No point in sending the EAPOL-Key - we will disconnect
2145 * immediately following this. */
2146 return;
2147 }
2148
2149 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2150 GTK[GN], IGTK, [FTIE], [TIE * 2])
2151 */
2152 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2153 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2154 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2155 wpa_ie = sm->wpa_auth->wpa_ie;
2156 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
2157 if (sm->wpa == WPA_VERSION_WPA &&
2158 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
2159 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002160 /* WPA-only STA, remove RSN IE and possible MDIE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002162 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
2163 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164 wpa_ie_len = wpa_ie[1] + 2;
2165 }
2166 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2167 "sending 3/4 msg of 4-Way Handshake");
2168 if (sm->wpa == WPA_VERSION_WPA2) {
2169 /* WPA2 send GTK in the 4-way handshake */
2170 secure = 1;
2171 gtk = gsm->GTK[gsm->GN - 1];
2172 gtk_len = gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002173 if (sm->wpa_auth->conf.disable_gtk) {
2174 /*
2175 * Provide unique random GTK to each STA to prevent use
2176 * of GTK in the BSS.
2177 */
2178 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
2179 return;
2180 gtk = dummy_gtk;
2181 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 keyidx = gsm->GN;
2183 _rsc = rsc;
2184 encr = 1;
2185 } else {
2186 /* WPA does not include GTK in msg 3/4 */
2187 secure = 0;
2188 gtk = NULL;
2189 gtk_len = 0;
2190 keyidx = 0;
2191 _rsc = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002192 if (sm->rx_eapol_key_secure) {
2193 /*
2194 * It looks like Windows 7 supplicant tries to use
2195 * Secure bit in msg 2/4 after having reported Michael
2196 * MIC failure and it then rejects the 4-way handshake
2197 * if msg 3/4 does not set Secure bit. Work around this
2198 * by setting the Secure bit here even in the case of
2199 * WPA if the supplicant used it first.
2200 */
2201 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2202 "STA used Secure bit in WPA msg 2/4 - "
2203 "set Secure for 3/4 as workaround");
2204 secure = 1;
2205 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002206 }
2207
2208 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
2209 if (gtk)
2210 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
2211#ifdef CONFIG_IEEE80211R
2212 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2213 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
2214 kde_len += 300; /* FTIE + 2 * TIE */
2215 }
2216#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002217#ifdef CONFIG_P2P
2218 if (WPA_GET_BE32(sm->ip_addr) > 0)
2219 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
2220#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002221 kde = os_malloc(kde_len);
2222 if (kde == NULL)
2223 return;
2224
2225 pos = kde;
2226 os_memcpy(pos, wpa_ie, wpa_ie_len);
2227 pos += wpa_ie_len;
2228#ifdef CONFIG_IEEE80211R
2229 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2230 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
2231 if (res < 0) {
2232 wpa_printf(MSG_ERROR, "FT: Failed to insert "
2233 "PMKR1Name into RSN IE in EAPOL-Key data");
2234 os_free(kde);
2235 return;
2236 }
2237 pos += res;
2238 }
2239#endif /* CONFIG_IEEE80211R */
2240 if (gtk) {
2241 u8 hdr[2];
2242 hdr[0] = keyidx & 0x03;
2243 hdr[1] = 0;
2244 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2245 gtk, gtk_len);
2246 }
2247 pos = ieee80211w_kde_add(sm, pos);
2248
2249#ifdef CONFIG_IEEE80211R
2250 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2251 int res;
2252 struct wpa_auth_config *conf;
2253
2254 conf = &sm->wpa_auth->conf;
2255 res = wpa_write_ftie(conf, conf->r0_key_holder,
2256 conf->r0_key_holder_len,
2257 NULL, NULL, pos, kde + kde_len - pos,
2258 NULL, 0);
2259 if (res < 0) {
2260 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2261 "into EAPOL-Key Key Data");
2262 os_free(kde);
2263 return;
2264 }
2265 pos += res;
2266
2267 /* TIE[ReassociationDeadline] (TU) */
2268 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2269 *pos++ = 5;
2270 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2271 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2272 pos += 4;
2273
2274 /* TIE[KeyLifetime] (seconds) */
2275 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2276 *pos++ = 5;
2277 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2278 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2279 pos += 4;
2280 }
2281#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002282#ifdef CONFIG_P2P
2283 if (WPA_GET_BE32(sm->ip_addr) > 0) {
2284 u8 addr[3 * 4];
2285 os_memcpy(addr, sm->ip_addr, 4);
2286 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
2287 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
2288 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
2289 addr, sizeof(addr), NULL, 0);
2290 }
2291#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292
2293 wpa_send_eapol(sm->wpa_auth, sm,
2294 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
2295 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2296 WPA_KEY_INFO_KEY_TYPE,
2297 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2298 os_free(kde);
2299}
2300
2301
2302SM_STATE(WPA_PTK, PTKINITDONE)
2303{
2304 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2305 sm->EAPOLKeyReceived = FALSE;
2306 if (sm->Pair) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002307 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2308 int klen = wpa_cipher_key_len(sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002309 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002310 sm->PTK.tk, klen)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2312 return;
2313 }
2314 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2315 sm->pairwise_set = TRUE;
2316
2317 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2318 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2319 eloop_register_timeout(sm->wpa_auth->conf.
2320 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2321 sm->wpa_auth, sm);
2322 }
2323
2324 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2325 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2326 WPA_EAPOL_authorized, 1);
2327 }
2328 }
2329
2330 if (0 /* IBSS == TRUE */) {
2331 sm->keycount++;
2332 if (sm->keycount == 2) {
2333 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2334 WPA_EAPOL_portValid, 1);
2335 }
2336 } else {
2337 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2338 1);
2339 }
2340 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2341 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2342 if (sm->wpa == WPA_VERSION_WPA)
2343 sm->PInitAKeys = TRUE;
2344 else
2345 sm->has_GTK = TRUE;
2346 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2347 "pairwise key handshake completed (%s)",
2348 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2349
2350#ifdef CONFIG_IEEE80211R
2351 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2352#endif /* CONFIG_IEEE80211R */
2353}
2354
2355
2356SM_STEP(WPA_PTK)
2357{
2358 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2359
2360 if (sm->Init)
2361 SM_ENTER(WPA_PTK, INITIALIZE);
2362 else if (sm->Disconnect
2363 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2364 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2365 "WPA_PTK: sm->Disconnect");
2366 SM_ENTER(WPA_PTK, DISCONNECT);
2367 }
2368 else if (sm->DeauthenticationRequest)
2369 SM_ENTER(WPA_PTK, DISCONNECTED);
2370 else if (sm->AuthenticationRequest)
2371 SM_ENTER(WPA_PTK, AUTHENTICATION);
2372 else if (sm->ReAuthenticationRequest)
2373 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2374 else if (sm->PTKRequest)
2375 SM_ENTER(WPA_PTK, PTKSTART);
2376 else switch (sm->wpa_ptk_state) {
2377 case WPA_PTK_INITIALIZE:
2378 break;
2379 case WPA_PTK_DISCONNECT:
2380 SM_ENTER(WPA_PTK, DISCONNECTED);
2381 break;
2382 case WPA_PTK_DISCONNECTED:
2383 SM_ENTER(WPA_PTK, INITIALIZE);
2384 break;
2385 case WPA_PTK_AUTHENTICATION:
2386 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2387 break;
2388 case WPA_PTK_AUTHENTICATION2:
2389 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2390 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2391 WPA_EAPOL_keyRun) > 0)
2392 SM_ENTER(WPA_PTK, INITPMK);
2393 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
2394 /* FIX: && 802.1X::keyRun */)
2395 SM_ENTER(WPA_PTK, INITPSK);
2396 break;
2397 case WPA_PTK_INITPMK:
2398 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2399 WPA_EAPOL_keyAvailable) > 0)
2400 SM_ENTER(WPA_PTK, PTKSTART);
2401 else {
2402 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2403 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2404 "INITPMK - keyAvailable = false");
2405 SM_ENTER(WPA_PTK, DISCONNECT);
2406 }
2407 break;
2408 case WPA_PTK_INITPSK:
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002409 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
2410 NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411 SM_ENTER(WPA_PTK, PTKSTART);
2412 else {
2413 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2414 "no PSK configured for the STA");
2415 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2416 SM_ENTER(WPA_PTK, DISCONNECT);
2417 }
2418 break;
2419 case WPA_PTK_PTKSTART:
2420 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2421 sm->EAPOLKeyPairwise)
2422 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2423 else if (sm->TimeoutCtr >
2424 (int) dot11RSNAConfigPairwiseUpdateCount) {
2425 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2426 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2427 "PTKSTART: Retry limit %d reached",
2428 dot11RSNAConfigPairwiseUpdateCount);
2429 SM_ENTER(WPA_PTK, DISCONNECT);
2430 } else if (sm->TimeoutEvt)
2431 SM_ENTER(WPA_PTK, PTKSTART);
2432 break;
2433 case WPA_PTK_PTKCALCNEGOTIATING:
2434 if (sm->MICVerified)
2435 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2436 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2437 sm->EAPOLKeyPairwise)
2438 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2439 else if (sm->TimeoutEvt)
2440 SM_ENTER(WPA_PTK, PTKSTART);
2441 break;
2442 case WPA_PTK_PTKCALCNEGOTIATING2:
2443 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2444 break;
2445 case WPA_PTK_PTKINITNEGOTIATING:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002446 if (sm->update_snonce)
2447 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2448 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2449 sm->EAPOLKeyPairwise && sm->MICVerified)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450 SM_ENTER(WPA_PTK, PTKINITDONE);
2451 else if (sm->TimeoutCtr >
2452 (int) dot11RSNAConfigPairwiseUpdateCount) {
2453 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2454 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2455 "PTKINITNEGOTIATING: Retry limit %d "
2456 "reached",
2457 dot11RSNAConfigPairwiseUpdateCount);
2458 SM_ENTER(WPA_PTK, DISCONNECT);
2459 } else if (sm->TimeoutEvt)
2460 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2461 break;
2462 case WPA_PTK_PTKINITDONE:
2463 break;
2464 }
2465}
2466
2467
2468SM_STATE(WPA_PTK_GROUP, IDLE)
2469{
2470 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2471 if (sm->Init) {
2472 /* Init flag is not cleared here, so avoid busy
2473 * loop by claiming nothing changed. */
2474 sm->changed = FALSE;
2475 }
2476 sm->GTimeoutCtr = 0;
2477}
2478
2479
2480SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2481{
2482 u8 rsc[WPA_KEY_RSC_LEN];
2483 struct wpa_group *gsm = sm->group;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002484 const u8 *kde;
2485 u8 *kde_buf = NULL, *pos, hdr[2];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486 size_t kde_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002487 u8 *gtk, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488
2489 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
2490
2491 sm->GTimeoutCtr++;
2492 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
2493 /* No point in sending the EAPOL-Key - we will disconnect
2494 * immediately following this. */
2495 return;
2496 }
2497
2498 if (sm->wpa == WPA_VERSION_WPA)
2499 sm->PInitAKeys = FALSE;
2500 sm->TimeoutEvt = FALSE;
2501 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2502 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2503 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2504 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2505 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2506 "sending 1/2 msg of Group Key Handshake");
2507
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002508 gtk = gsm->GTK[gsm->GN - 1];
2509 if (sm->wpa_auth->conf.disable_gtk) {
2510 /*
2511 * Provide unique random GTK to each STA to prevent use
2512 * of GTK in the BSS.
2513 */
2514 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
2515 return;
2516 gtk = dummy_gtk;
2517 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518 if (sm->wpa == WPA_VERSION_WPA2) {
2519 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
2520 ieee80211w_kde_len(sm);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002521 kde_buf = os_malloc(kde_len);
2522 if (kde_buf == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002523 return;
2524
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002525 kde = pos = kde_buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002526 hdr[0] = gsm->GN & 0x03;
2527 hdr[1] = 0;
2528 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002529 gtk, gsm->GTK_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 pos = ieee80211w_kde_add(sm, pos);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002531 kde_len = pos - kde;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002533 kde = gtk;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002534 kde_len = gsm->GTK_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 }
2536
2537 wpa_send_eapol(sm->wpa_auth, sm,
2538 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
2539 WPA_KEY_INFO_ACK |
2540 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002541 rsc, gsm->GNonce, kde, kde_len, gsm->GN, 1);
2542
2543 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544}
2545
2546
2547SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2548{
2549 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2550 sm->EAPOLKeyReceived = FALSE;
2551 if (sm->GUpdateStationKeys)
2552 sm->group->GKeyDoneStations--;
2553 sm->GUpdateStationKeys = FALSE;
2554 sm->GTimeoutCtr = 0;
2555 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2556 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2557 "group key handshake completed (%s)",
2558 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2559 sm->has_GTK = TRUE;
2560}
2561
2562
2563SM_STATE(WPA_PTK_GROUP, KEYERROR)
2564{
2565 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2566 if (sm->GUpdateStationKeys)
2567 sm->group->GKeyDoneStations--;
2568 sm->GUpdateStationKeys = FALSE;
2569 sm->Disconnect = TRUE;
2570}
2571
2572
2573SM_STEP(WPA_PTK_GROUP)
2574{
2575 if (sm->Init || sm->PtkGroupInit) {
2576 SM_ENTER(WPA_PTK_GROUP, IDLE);
2577 sm->PtkGroupInit = FALSE;
2578 } else switch (sm->wpa_ptk_group_state) {
2579 case WPA_PTK_GROUP_IDLE:
2580 if (sm->GUpdateStationKeys ||
2581 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2582 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2583 break;
2584 case WPA_PTK_GROUP_REKEYNEGOTIATING:
2585 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2586 !sm->EAPOLKeyPairwise && sm->MICVerified)
2587 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2588 else if (sm->GTimeoutCtr >
2589 (int) dot11RSNAConfigGroupUpdateCount)
2590 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2591 else if (sm->TimeoutEvt)
2592 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2593 break;
2594 case WPA_PTK_GROUP_KEYERROR:
2595 SM_ENTER(WPA_PTK_GROUP, IDLE);
2596 break;
2597 case WPA_PTK_GROUP_REKEYESTABLISHED:
2598 SM_ENTER(WPA_PTK_GROUP, IDLE);
2599 break;
2600 }
2601}
2602
2603
2604static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
2605 struct wpa_group *group)
2606{
2607 int ret = 0;
2608
2609 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2610 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2611 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
2612 wpa_auth->addr, group->GNonce,
2613 group->GTK[group->GN - 1], group->GTK_len) < 0)
2614 ret = -1;
2615 wpa_hexdump_key(MSG_DEBUG, "GTK",
2616 group->GTK[group->GN - 1], group->GTK_len);
2617
2618#ifdef CONFIG_IEEE80211W
2619 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002620 size_t len;
2621 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2623 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2624 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
2625 wpa_auth->addr, group->GNonce,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002626 group->IGTK[group->GN_igtk - 4], len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627 ret = -1;
2628 wpa_hexdump_key(MSG_DEBUG, "IGTK",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002629 group->IGTK[group->GN_igtk - 4], len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630 }
2631#endif /* CONFIG_IEEE80211W */
2632
2633 return ret;
2634}
2635
2636
2637static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2638 struct wpa_group *group)
2639{
2640 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2641 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2642 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2643 group->wpa_group_state = WPA_GROUP_GTK_INIT;
2644
2645 /* GTK[0..N] = 0 */
2646 os_memset(group->GTK, 0, sizeof(group->GTK));
2647 group->GN = 1;
2648 group->GM = 2;
2649#ifdef CONFIG_IEEE80211W
2650 group->GN_igtk = 4;
2651 group->GM_igtk = 5;
2652#endif /* CONFIG_IEEE80211W */
2653 /* GTK[GN] = CalcGTK() */
2654 wpa_gtk_update(wpa_auth, group);
2655}
2656
2657
2658static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2659{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002660 if (ctx != NULL && ctx != sm->group)
2661 return 0;
2662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2664 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2665 "Not in PTKINITDONE; skip Group Key update");
2666 sm->GUpdateStationKeys = FALSE;
2667 return 0;
2668 }
2669 if (sm->GUpdateStationKeys) {
2670 /*
2671 * This should not really happen, so add a debug log entry.
2672 * Since we clear the GKeyDoneStations before the loop, the
2673 * station needs to be counted here anyway.
2674 */
2675 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2676 "GUpdateStationKeys was already set when "
2677 "marking station for GTK rekeying");
2678 }
2679
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002680 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002681 if (sm->is_wnmsleep)
2682 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 sm->group->GKeyDoneStations++;
2685 sm->GUpdateStationKeys = TRUE;
2686
2687 wpa_sm_step(sm);
2688 return 0;
2689}
2690
2691
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002692#ifdef CONFIG_WNM
2693/* update GTK when exiting WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002694void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
2695{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002696 if (sm == NULL || sm->is_wnmsleep)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002697 return;
2698
2699 wpa_group_update_sta(sm, NULL);
2700}
2701
2702
2703void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
2704{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002705 if (sm)
2706 sm->is_wnmsleep = !!flag;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002707}
2708
2709
2710int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2711{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002712 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002713 u8 *start = pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002714
2715 /*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002716 * GTK subelement:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002717 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002718 * Key[5..32]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002719 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002720 *pos++ = WNM_SLEEP_SUBELEM_GTK;
2721 *pos++ = 11 + gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002722 /* Key ID in B0-B1 of Key Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002723 WPA_PUT_LE16(pos, gsm->GN & 0x03);
2724 pos += 2;
2725 *pos++ = gsm->GTK_len;
2726 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002727 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002728 pos += 8;
2729 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2730 pos += gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002731
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002732 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
2733 gsm->GN);
2734 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002735 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002736
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002737 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002738}
2739
2740
2741#ifdef CONFIG_IEEE80211W
2742int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2743{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002744 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002745 u8 *start = pos;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002746 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002747
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002748 /*
2749 * IGTK subelement:
2750 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
2751 */
2752 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002753 *pos++ = 2 + 6 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002754 WPA_PUT_LE16(pos, gsm->GN_igtk);
2755 pos += 2;
2756 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002757 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002758 pos += 6;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002759
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002760 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
2761 pos += len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002762
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002763 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
2764 gsm->GN_igtk);
2765 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002766 gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002767
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002768 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002769}
2770#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002771#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002772
2773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2775 struct wpa_group *group)
2776{
2777 int tmp;
2778
2779 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2780 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2781 group->changed = TRUE;
2782 group->wpa_group_state = WPA_GROUP_SETKEYS;
2783 group->GTKReKey = FALSE;
2784 tmp = group->GM;
2785 group->GM = group->GN;
2786 group->GN = tmp;
2787#ifdef CONFIG_IEEE80211W
2788 tmp = group->GM_igtk;
2789 group->GM_igtk = group->GN_igtk;
2790 group->GN_igtk = tmp;
2791#endif /* CONFIG_IEEE80211W */
2792 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2793 * counting the STAs that are marked with GUpdateStationKeys instead of
2794 * including all STAs that could be in not-yet-completed state. */
2795 wpa_gtk_update(wpa_auth, group);
2796
2797 if (group->GKeyDoneStations) {
2798 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
2799 "GKeyDoneStations=%d when starting new GTK rekey",
2800 group->GKeyDoneStations);
2801 group->GKeyDoneStations = 0;
2802 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002803 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2805 group->GKeyDoneStations);
2806}
2807
2808
2809static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
2810 struct wpa_group *group)
2811{
2812 int ret = 0;
2813
2814 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002815 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002816 broadcast_ether_addr, group->GN,
2817 group->GTK[group->GN - 1], group->GTK_len) < 0)
2818 ret = -1;
2819
2820#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002821 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2822 enum wpa_alg alg;
2823 size_t len;
2824
2825 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
2826 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2827
2828 if (ret == 0 &&
2829 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
2830 broadcast_ether_addr, group->GN_igtk,
2831 group->IGTK[group->GN_igtk - 4], len) < 0)
2832 ret = -1;
2833 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834#endif /* CONFIG_IEEE80211W */
2835
2836 return ret;
2837}
2838
2839
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002840static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
2841{
2842 if (sm->group == ctx) {
2843 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
2844 " for discconnection due to fatal failure",
2845 MAC2STR(sm->addr));
2846 sm->Disconnect = TRUE;
2847 }
2848
2849 return 0;
2850}
2851
2852
2853static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
2854 struct wpa_group *group)
2855{
2856 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
2857 group->changed = TRUE;
2858 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
2859 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
2860}
2861
2862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2864 struct wpa_group *group)
2865{
2866 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2867 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2868 group->changed = TRUE;
2869 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2870
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002871 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
2872 wpa_group_fatal_failure(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002874 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875
2876 return 0;
2877}
2878
2879
2880static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2881 struct wpa_group *group)
2882{
2883 if (group->GInit) {
2884 wpa_group_gtk_init(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002885 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
2886 /* Do not allow group operations */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002887 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2888 group->GTKAuthenticator) {
2889 wpa_group_setkeysdone(wpa_auth, group);
2890 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2891 group->GTKReKey) {
2892 wpa_group_setkeys(wpa_auth, group);
2893 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2894 if (group->GKeyDoneStations == 0)
2895 wpa_group_setkeysdone(wpa_auth, group);
2896 else if (group->GTKReKey)
2897 wpa_group_setkeys(wpa_auth, group);
2898 }
2899}
2900
2901
2902static int wpa_sm_step(struct wpa_state_machine *sm)
2903{
2904 if (sm == NULL)
2905 return 0;
2906
2907 if (sm->in_step_loop) {
2908 /* This should not happen, but if it does, make sure we do not
2909 * end up freeing the state machine too early by exiting the
2910 * recursive call. */
2911 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2912 return 0;
2913 }
2914
2915 sm->in_step_loop = 1;
2916 do {
2917 if (sm->pending_deinit)
2918 break;
2919
2920 sm->changed = FALSE;
2921 sm->wpa_auth->group->changed = FALSE;
2922
2923 SM_STEP_RUN(WPA_PTK);
2924 if (sm->pending_deinit)
2925 break;
2926 SM_STEP_RUN(WPA_PTK_GROUP);
2927 if (sm->pending_deinit)
2928 break;
2929 wpa_group_sm_step(sm->wpa_auth, sm->group);
2930 } while (sm->changed || sm->wpa_auth->group->changed);
2931 sm->in_step_loop = 0;
2932
2933 if (sm->pending_deinit) {
2934 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2935 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2936 wpa_free_sta_sm(sm);
2937 return 1;
2938 }
2939 return 0;
2940}
2941
2942
2943static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2944{
2945 struct wpa_state_machine *sm = eloop_ctx;
2946 wpa_sm_step(sm);
2947}
2948
2949
2950void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2951{
2952 if (sm == NULL)
2953 return;
2954 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2955}
2956
2957
2958void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2959{
2960 int tmp, i;
2961 struct wpa_group *group;
2962
2963 if (wpa_auth == NULL)
2964 return;
2965
2966 group = wpa_auth->group;
2967
2968 for (i = 0; i < 2; i++) {
2969 tmp = group->GM;
2970 group->GM = group->GN;
2971 group->GN = tmp;
2972#ifdef CONFIG_IEEE80211W
2973 tmp = group->GM_igtk;
2974 group->GM_igtk = group->GN_igtk;
2975 group->GN_igtk = tmp;
2976#endif /* CONFIG_IEEE80211W */
2977 wpa_gtk_update(wpa_auth, group);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002978 wpa_group_config_group_keys(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979 }
2980}
2981
2982
2983static const char * wpa_bool_txt(int bool)
2984{
2985 return bool ? "TRUE" : "FALSE";
2986}
2987
2988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002989#define RSN_SUITE "%02x-%02x-%02x-%d"
2990#define RSN_SUITE_ARG(s) \
2991((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2992
2993int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2994{
2995 int len = 0, ret;
2996 char pmkid_txt[PMKID_LEN * 2 + 1];
2997#ifdef CONFIG_RSN_PREAUTH
2998 const int preauth = 1;
2999#else /* CONFIG_RSN_PREAUTH */
3000 const int preauth = 0;
3001#endif /* CONFIG_RSN_PREAUTH */
3002
3003 if (wpa_auth == NULL)
3004 return len;
3005
3006 ret = os_snprintf(buf + len, buflen - len,
3007 "dot11RSNAOptionImplemented=TRUE\n"
3008 "dot11RSNAPreauthenticationImplemented=%s\n"
3009 "dot11RSNAEnabled=%s\n"
3010 "dot11RSNAPreauthenticationEnabled=%s\n",
3011 wpa_bool_txt(preauth),
3012 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
3013 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003014 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015 return len;
3016 len += ret;
3017
3018 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
3019 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
3020
3021 ret = os_snprintf(
3022 buf + len, buflen - len,
3023 "dot11RSNAConfigVersion=%u\n"
3024 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
3025 /* FIX: dot11RSNAConfigGroupCipher */
3026 /* FIX: dot11RSNAConfigGroupRekeyMethod */
3027 /* FIX: dot11RSNAConfigGroupRekeyTime */
3028 /* FIX: dot11RSNAConfigGroupRekeyPackets */
3029 "dot11RSNAConfigGroupRekeyStrict=%u\n"
3030 "dot11RSNAConfigGroupUpdateCount=%u\n"
3031 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
3032 "dot11RSNAConfigGroupCipherSize=%u\n"
3033 "dot11RSNAConfigPMKLifetime=%u\n"
3034 "dot11RSNAConfigPMKReauthThreshold=%u\n"
3035 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
3036 "dot11RSNAConfigSATimeout=%u\n"
3037 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
3038 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
3039 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
3040 "dot11RSNAPMKIDUsed=%s\n"
3041 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
3042 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
3043 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
3044 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
3045 "dot11RSNA4WayHandshakeFailures=%u\n"
3046 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
3047 RSN_VERSION,
3048 !!wpa_auth->conf.wpa_strict_rekey,
3049 dot11RSNAConfigGroupUpdateCount,
3050 dot11RSNAConfigPairwiseUpdateCount,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003051 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052 dot11RSNAConfigPMKLifetime,
3053 dot11RSNAConfigPMKReauthThreshold,
3054 dot11RSNAConfigSATimeout,
3055 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
3056 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
3057 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
3058 pmkid_txt,
3059 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
3060 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
3061 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
3062 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
3063 wpa_auth->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003064 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003065 return len;
3066 len += ret;
3067
3068 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
3069 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
3070
3071 /* Private MIB */
3072 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
3073 wpa_auth->group->wpa_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003074 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 return len;
3076 len += ret;
3077
3078 return len;
3079}
3080
3081
3082int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
3083{
3084 int len = 0, ret;
3085 u32 pairwise = 0;
3086
3087 if (sm == NULL)
3088 return 0;
3089
3090 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
3091
3092 /* dot11RSNAStatsEntry */
3093
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003094 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
3095 WPA_PROTO_RSN : WPA_PROTO_WPA,
3096 sm->pairwise);
3097 if (pairwise == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003098 return 0;
3099
3100 ret = os_snprintf(
3101 buf + len, buflen - len,
3102 /* TODO: dot11RSNAStatsIndex */
3103 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
3104 "dot11RSNAStatsVersion=1\n"
3105 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
3106 /* TODO: dot11RSNAStatsTKIPICVErrors */
3107 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
3108 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
3109 /* TODO: dot11RSNAStatsCCMPReplays */
3110 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
3111 /* TODO: dot11RSNAStatsTKIPReplays */,
3112 MAC2STR(sm->addr),
3113 RSN_SUITE_ARG(pairwise),
3114 sm->dot11RSNAStatsTKIPLocalMICFailures,
3115 sm->dot11RSNAStatsTKIPRemoteMICFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003116 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117 return len;
3118 len += ret;
3119
3120 /* Private MIB */
3121 ret = os_snprintf(buf + len, buflen - len,
3122 "hostapdWPAPTKState=%d\n"
3123 "hostapdWPAPTKGroupState=%d\n",
3124 sm->wpa_ptk_state,
3125 sm->wpa_ptk_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003126 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003127 return len;
3128 len += ret;
3129
3130 return len;
3131}
3132
3133
3134void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
3135{
3136 if (wpa_auth)
3137 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
3138}
3139
3140
3141int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
3142{
3143 return sm && sm->pairwise_set;
3144}
3145
3146
3147int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
3148{
3149 return sm->pairwise;
3150}
3151
3152
3153int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
3154{
3155 if (sm == NULL)
3156 return -1;
3157 return sm->wpa_key_mgmt;
3158}
3159
3160
3161int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
3162{
3163 if (sm == NULL)
3164 return 0;
3165 return sm->wpa;
3166}
3167
3168
3169int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
3170 struct rsn_pmksa_cache_entry *entry)
3171{
3172 if (sm == NULL || sm->pmksa != entry)
3173 return -1;
3174 sm->pmksa = NULL;
3175 return 0;
3176}
3177
3178
3179struct rsn_pmksa_cache_entry *
3180wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
3181{
3182 return sm ? sm->pmksa : NULL;
3183}
3184
3185
3186void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
3187{
3188 if (sm)
3189 sm->dot11RSNAStatsTKIPLocalMICFailures++;
3190}
3191
3192
3193const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
3194{
3195 if (wpa_auth == NULL)
3196 return NULL;
3197 *len = wpa_auth->wpa_ie_len;
3198 return wpa_auth->wpa_ie;
3199}
3200
3201
3202int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
3203 int session_timeout, struct eapol_state_machine *eapol)
3204{
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07003205 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
3206 sm->wpa_auth->conf.disable_pmksa_caching)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207 return -1;
3208
3209 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003210 sm->PTK.kck, sm->PTK.kck_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211 sm->wpa_auth->addr, sm->addr, session_timeout,
3212 eapol, sm->wpa_key_mgmt))
3213 return 0;
3214
3215 return -1;
3216}
3217
3218
3219int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
3220 const u8 *pmk, size_t len, const u8 *sta_addr,
3221 int session_timeout,
3222 struct eapol_state_machine *eapol)
3223{
3224 if (wpa_auth == NULL)
3225 return -1;
3226
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003227 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len,
3228 NULL, 0,
3229 wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003230 sta_addr, session_timeout, eapol,
3231 WPA_KEY_MGMT_IEEE8021X))
3232 return 0;
3233
3234 return -1;
3235}
3236
3237
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003238int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
3239 const u8 *pmk)
3240{
3241 if (wpa_auth->conf.disable_pmksa_caching)
3242 return -1;
3243
3244 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN,
3245 NULL, 0,
3246 wpa_auth->addr, addr, 0, NULL,
3247 WPA_KEY_MGMT_SAE))
3248 return 0;
3249
3250 return -1;
3251}
3252
3253
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003254void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
3255 const u8 *sta_addr)
3256{
3257 struct rsn_pmksa_cache_entry *pmksa;
3258
3259 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
3260 return;
3261 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3262 if (pmksa) {
3263 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
3264 MACSTR " based on request", MAC2STR(sta_addr));
3265 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
3266 }
3267}
3268
3269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003270static struct wpa_group *
3271wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3272{
3273 struct wpa_group *group;
3274
3275 if (wpa_auth == NULL || wpa_auth->group == NULL)
3276 return NULL;
3277
3278 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
3279 vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003280 group = wpa_group_init(wpa_auth, vlan_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003281 if (group == NULL)
3282 return NULL;
3283
3284 group->next = wpa_auth->group->next;
3285 wpa_auth->group->next = group;
3286
3287 return group;
3288}
3289
3290
3291int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
3292{
3293 struct wpa_group *group;
3294
3295 if (sm == NULL || sm->wpa_auth == NULL)
3296 return 0;
3297
3298 group = sm->wpa_auth->group;
3299 while (group) {
3300 if (group->vlan_id == vlan_id)
3301 break;
3302 group = group->next;
3303 }
3304
3305 if (group == NULL) {
3306 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
3307 if (group == NULL)
3308 return -1;
3309 }
3310
3311 if (sm->group == group)
3312 return 0;
3313
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003314 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
3315 return -1;
3316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003317 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
3318 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
3319
3320 sm->group = group;
3321 return 0;
3322}
3323
3324
3325void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
3326 struct wpa_state_machine *sm, int ack)
3327{
3328 if (wpa_auth == NULL || sm == NULL)
3329 return;
3330 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
3331 " ack=%d", MAC2STR(sm->addr), ack);
3332 if (sm->pending_1_of_4_timeout && ack) {
3333 /*
3334 * Some deployed supplicant implementations update their SNonce
3335 * for each EAPOL-Key 2/4 message even within the same 4-way
3336 * handshake and then fail to use the first SNonce when
3337 * deriving the PTK. This results in unsuccessful 4-way
3338 * handshake whenever the relatively short initial timeout is
3339 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
3340 * around this by increasing the timeout now that we know that
3341 * the station has received the frame.
3342 */
3343 int timeout_ms = eapol_key_timeout_subseq;
3344 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
3345 "timeout by %u ms because of acknowledged frame",
3346 timeout_ms);
3347 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
3348 eloop_register_timeout(timeout_ms / 1000,
3349 (timeout_ms % 1000) * 1000,
3350 wpa_send_eapol_timeout, wpa_auth, sm);
3351 }
3352}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003353
3354
3355int wpa_auth_uses_sae(struct wpa_state_machine *sm)
3356{
3357 if (sm == NULL)
3358 return 0;
3359 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
3360}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003361
3362
3363int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
3364{
3365 if (sm == NULL)
3366 return 0;
3367 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
3368}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003369
3370
3371#ifdef CONFIG_P2P
3372int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
3373{
3374 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
3375 return -1;
3376 os_memcpy(addr, sm->ip_addr, 4);
3377 return 0;
3378}
3379#endif /* CONFIG_P2P */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003380
3381
3382int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
3383 struct radius_das_attrs *attr)
3384{
3385 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
3386}