blob: da2073c0a22af7dd37be2f88bd25e5f2bc0fdd22 [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 Shmidt391c59f2013-09-03 12:16:28 -07003 * Copyright (c) 2004-2013, 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.");
418 os_free(wpa_auth->wpa_ie);
419 os_free(wpa_auth);
420 return NULL;
421 }
422
423#ifdef CONFIG_IEEE80211R
424 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
425 if (wpa_auth->ft_pmk_cache == NULL) {
426 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
427 os_free(wpa_auth->wpa_ie);
428 pmksa_cache_auth_deinit(wpa_auth->pmksa);
429 os_free(wpa_auth);
430 return NULL;
431 }
432#endif /* CONFIG_IEEE80211R */
433
434 if (wpa_auth->conf.wpa_gmk_rekey) {
435 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
436 wpa_rekey_gmk, wpa_auth, NULL);
437 }
438
439 if (wpa_auth->conf.wpa_group_rekey) {
440 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
441 wpa_rekey_gtk, wpa_auth, NULL);
442 }
443
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800444#ifdef CONFIG_P2P
445 if (WPA_GET_BE32(conf->ip_addr_start)) {
446 int count = WPA_GET_BE32(conf->ip_addr_end) -
447 WPA_GET_BE32(conf->ip_addr_start) + 1;
448 if (count > 1000)
449 count = 1000;
450 if (count > 0)
451 wpa_auth->ip_pool = bitfield_alloc(count);
452 }
453#endif /* CONFIG_P2P */
454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455 return wpa_auth;
456}
457
458
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800459int wpa_init_keys(struct wpa_authenticator *wpa_auth)
460{
461 struct wpa_group *group = wpa_auth->group;
462
463 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
464 "keys");
465 wpa_group_sm_step(wpa_auth, group);
466 group->GInit = FALSE;
467 wpa_group_sm_step(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800468 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
469 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800470 return 0;
471}
472
473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474/**
475 * wpa_deinit - Deinitialize WPA authenticator
476 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
477 */
478void wpa_deinit(struct wpa_authenticator *wpa_auth)
479{
480 struct wpa_group *group, *prev;
481
482 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
483 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
484
485#ifdef CONFIG_PEERKEY
486 while (wpa_auth->stsl_negotiations)
487 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
488#endif /* CONFIG_PEERKEY */
489
490 pmksa_cache_auth_deinit(wpa_auth->pmksa);
491
492#ifdef CONFIG_IEEE80211R
493 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
494 wpa_auth->ft_pmk_cache = NULL;
495#endif /* CONFIG_IEEE80211R */
496
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800497#ifdef CONFIG_P2P
498 bitfield_free(wpa_auth->ip_pool);
499#endif /* CONFIG_P2P */
500
501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 os_free(wpa_auth->wpa_ie);
503
504 group = wpa_auth->group;
505 while (group) {
506 prev = group;
507 group = group->next;
508 os_free(prev);
509 }
510
511 os_free(wpa_auth);
512}
513
514
515/**
516 * wpa_reconfig - Update WPA authenticator configuration
517 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
518 * @conf: Configuration for WPA authenticator
519 */
520int wpa_reconfig(struct wpa_authenticator *wpa_auth,
521 struct wpa_auth_config *conf)
522{
523 struct wpa_group *group;
524 if (wpa_auth == NULL)
525 return 0;
526
527 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
528 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
529 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
530 return -1;
531 }
532
533 /*
534 * Reinitialize GTK to make sure it is suitable for the new
535 * configuration.
536 */
537 group = wpa_auth->group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700538 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 group->GInit = TRUE;
540 wpa_group_sm_step(wpa_auth, group);
541 group->GInit = FALSE;
542 wpa_group_sm_step(wpa_auth, group);
543
544 return 0;
545}
546
547
548struct wpa_state_machine *
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700549wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
550 const u8 *p2p_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551{
552 struct wpa_state_machine *sm;
553
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800554 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
555 return NULL;
556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700557 sm = os_zalloc(sizeof(struct wpa_state_machine));
558 if (sm == NULL)
559 return NULL;
560 os_memcpy(sm->addr, addr, ETH_ALEN);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700561 if (p2p_dev_addr)
562 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563
564 sm->wpa_auth = wpa_auth;
565 sm->group = wpa_auth->group;
566
567 return sm;
568}
569
570
571int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
572 struct wpa_state_machine *sm)
573{
574 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
575 return -1;
576
577#ifdef CONFIG_IEEE80211R
578 if (sm->ft_completed) {
579 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
580 "FT authentication already completed - do not "
581 "start 4-way handshake");
Dmitry Shmidt71757432014-06-02 13:50:35 -0700582 /* Go to PTKINITDONE state to allow GTK rekeying */
583 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584 return 0;
585 }
586#endif /* CONFIG_IEEE80211R */
587
588 if (sm->started) {
589 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
590 sm->ReAuthenticationRequest = TRUE;
591 return wpa_sm_step(sm);
592 }
593
594 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
595 "start authentication");
596 sm->started = 1;
597
598 sm->Init = TRUE;
599 if (wpa_sm_step(sm) == 1)
600 return 1; /* should not really happen */
601 sm->Init = FALSE;
602 sm->AuthenticationRequest = TRUE;
603 return wpa_sm_step(sm);
604}
605
606
607void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
608{
609 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
610 * reassociates back to the same AP while the previous entry for the
611 * STA has not yet been removed. */
612 if (sm == NULL)
613 return;
614
615 sm->wpa_key_mgmt = 0;
616}
617
618
619static void wpa_free_sta_sm(struct wpa_state_machine *sm)
620{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800621#ifdef CONFIG_P2P
622 if (WPA_GET_BE32(sm->ip_addr)) {
623 u32 start;
624 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
625 "address %u.%u.%u.%u from " MACSTR,
626 sm->ip_addr[0], sm->ip_addr[1],
627 sm->ip_addr[2], sm->ip_addr[3],
628 MAC2STR(sm->addr));
629 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
630 bitfield_clear(sm->wpa_auth->ip_pool,
631 WPA_GET_BE32(sm->ip_addr) - start);
632 }
633#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 if (sm->GUpdateStationKeys) {
635 sm->group->GKeyDoneStations--;
636 sm->GUpdateStationKeys = FALSE;
637 }
638#ifdef CONFIG_IEEE80211R
639 os_free(sm->assoc_resp_ftie);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700640 wpabuf_free(sm->ft_pending_req_ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641#endif /* CONFIG_IEEE80211R */
642 os_free(sm->last_rx_eapol_key);
643 os_free(sm->wpa_ie);
644 os_free(sm);
645}
646
647
648void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
649{
650 if (sm == NULL)
651 return;
652
653 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
654 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
655 "strict rekeying - force GTK rekey since STA "
656 "is leaving");
657 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
658 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
659 NULL);
660 }
661
662 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
663 sm->pending_1_of_4_timeout = 0;
664 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
665 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
666 if (sm->in_step_loop) {
667 /* Must not free state machine while wpa_sm_step() is running.
668 * Freeing will be completed in the end of wpa_sm_step(). */
669 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
670 "machine deinit for " MACSTR, MAC2STR(sm->addr));
671 sm->pending_deinit = 1;
672 } else
673 wpa_free_sta_sm(sm);
674}
675
676
677static void wpa_request_new_ptk(struct wpa_state_machine *sm)
678{
679 if (sm == NULL)
680 return;
681
682 sm->PTKRequest = TRUE;
683 sm->PTK_valid = 0;
684}
685
686
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800687static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688 const u8 *replay_counter)
689{
690 int i;
691 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800692 if (!ctr[i].valid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800694 if (os_memcmp(replay_counter, ctr[i].counter,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695 WPA_REPLAY_COUNTER_LEN) == 0)
696 return 1;
697 }
698 return 0;
699}
700
701
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800702static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
703 const u8 *replay_counter)
704{
705 int i;
706 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
707 if (ctr[i].valid &&
708 (replay_counter == NULL ||
709 os_memcmp(replay_counter, ctr[i].counter,
710 WPA_REPLAY_COUNTER_LEN) == 0))
711 ctr[i].valid = FALSE;
712 }
713}
714
715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716#ifdef CONFIG_IEEE80211R
717static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
718 struct wpa_state_machine *sm,
719 struct wpa_eapol_ie_parse *kde)
720{
721 struct wpa_ie_data ie;
722 struct rsn_mdie *mdie;
723
724 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
725 ie.num_pmkid != 1 || ie.pmkid == NULL) {
726 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
727 "FT 4-way handshake message 2/4");
728 return -1;
729 }
730
731 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
732 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
733 sm->sup_pmk_r1_name, PMKID_LEN);
734
735 if (!kde->mdie || !kde->ftie) {
736 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
737 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
738 return -1;
739 }
740
741 mdie = (struct rsn_mdie *) (kde->mdie + 2);
742 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
743 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
744 MOBILITY_DOMAIN_ID_LEN) != 0) {
745 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
746 return -1;
747 }
748
749 if (sm->assoc_resp_ftie &&
750 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
751 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
752 2 + sm->assoc_resp_ftie[1]) != 0)) {
753 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
754 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
755 kde->ftie, kde->ftie_len);
756 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
757 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
758 return -1;
759 }
760
761 return 0;
762}
763#endif /* CONFIG_IEEE80211R */
764
765
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800766static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
767 struct wpa_state_machine *sm, int group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800768{
769 /* Supplicant reported a Michael MIC error */
770 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
771 "received EAPOL-Key Error Request "
772 "(STA detected Michael MIC failure (group=%d))",
773 group);
774
775 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
776 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
777 "ignore Michael MIC failure report since "
778 "group cipher is not TKIP");
779 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
780 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
781 "ignore Michael MIC failure report since "
782 "pairwise cipher is not TKIP");
783 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800784 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
785 return 1; /* STA entry was removed */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800786 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
787 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
788 }
789
790 /*
791 * Error report is not a request for a new key handshake, but since
792 * Authenticator may do it, let's change the keys now anyway.
793 */
794 wpa_request_new_ptk(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800795 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800796}
797
798
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800799static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
800 size_t data_len)
801{
802 struct wpa_ptk PTK;
803 int ok = 0;
804 const u8 *pmk = NULL;
805
806 for (;;) {
807 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
808 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
809 sm->p2p_dev_addr, pmk);
810 if (pmk == NULL)
811 break;
812 } else
813 pmk = sm->PMK;
814
815 wpa_derive_ptk(sm, sm->alt_SNonce, pmk, &PTK);
816
817 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len)
818 == 0) {
819 ok = 1;
820 break;
821 }
822
823 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
824 break;
825 }
826
827 if (!ok) {
828 wpa_printf(MSG_DEBUG,
829 "WPA: Earlier SNonce did not result in matching MIC");
830 return -1;
831 }
832
833 wpa_printf(MSG_DEBUG,
834 "WPA: Earlier SNonce resulted in matching MIC");
835 sm->alt_snonce_valid = 0;
836 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
837 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
838 sm->PTK_valid = TRUE;
839
840 return 0;
841}
842
843
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844void wpa_receive(struct wpa_authenticator *wpa_auth,
845 struct wpa_state_machine *sm,
846 u8 *data, size_t data_len)
847{
848 struct ieee802_1x_hdr *hdr;
849 struct wpa_eapol_key *key;
850 u16 key_info, key_data_length;
851 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
852 SMK_M1, SMK_M3, SMK_ERROR } msg;
853 char *msgtxt;
854 struct wpa_eapol_ie_parse kde;
855 int ft;
856 const u8 *eapol_key_ie;
857 size_t eapol_key_ie_len;
858
859 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
860 return;
861
862 if (data_len < sizeof(*hdr) + sizeof(*key))
863 return;
864
865 hdr = (struct ieee802_1x_hdr *) data;
866 key = (struct wpa_eapol_key *) (hdr + 1);
867 key_info = WPA_GET_BE16(key->key_info);
868 key_data_length = WPA_GET_BE16(key->key_data_length);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800869 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
870 " key_info=0x%x type=%u key_data_length=%u",
871 MAC2STR(sm->addr), key_info, key->type, key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
873 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
874 "key_data overflow (%d > %lu)",
875 key_data_length,
876 (unsigned long) (data_len - sizeof(*hdr) -
877 sizeof(*key)));
878 return;
879 }
880
881 if (sm->wpa == WPA_VERSION_WPA2) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800882 if (key->type == EAPOL_KEY_TYPE_WPA) {
883 /*
884 * Some deployed station implementations seem to send
885 * msg 4/4 with incorrect type value in WPA2 mode.
886 */
887 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
888 "with unexpected WPA type in RSN mode");
889 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
891 "unexpected type %d in RSN mode",
892 key->type);
893 return;
894 }
895 } else {
896 if (key->type != EAPOL_KEY_TYPE_WPA) {
897 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
898 "unexpected type %d in WPA mode",
899 key->type);
900 return;
901 }
902 }
903
904 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
905 WPA_NONCE_LEN);
906 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
907 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
908
909 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
910 * are set */
911
912 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
913 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
914 if (key_info & WPA_KEY_INFO_ERROR) {
915 msg = SMK_ERROR;
916 msgtxt = "SMK Error";
917 } else {
918 msg = SMK_M1;
919 msgtxt = "SMK M1";
920 }
921 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
922 msg = SMK_M3;
923 msgtxt = "SMK M3";
924 } else if (key_info & WPA_KEY_INFO_REQUEST) {
925 msg = REQUEST;
926 msgtxt = "Request";
927 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
928 msg = GROUP_2;
929 msgtxt = "2/2 Group";
930 } else if (key_data_length == 0) {
931 msg = PAIRWISE_4;
932 msgtxt = "4/4 Pairwise";
933 } else {
934 msg = PAIRWISE_2;
935 msgtxt = "2/4 Pairwise";
936 }
937
938 /* TODO: key_info type validation for PeerKey */
939 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
940 msg == GROUP_2) {
941 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700942 if (sm->pairwise == WPA_CIPHER_CCMP ||
943 sm->pairwise == WPA_CIPHER_GCMP) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 if (wpa_use_aes_cmac(sm) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800945 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800946 !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
948 wpa_auth_logger(wpa_auth, sm->addr,
949 LOGGER_WARNING,
950 "advertised support for "
951 "AES-128-CMAC, but did not "
952 "use it");
953 return;
954 }
955
956 if (!wpa_use_aes_cmac(sm) &&
957 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
958 wpa_auth_logger(wpa_auth, sm->addr,
959 LOGGER_WARNING,
960 "did not use HMAC-SHA1-AES "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700961 "with CCMP/GCMP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962 return;
963 }
964 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800965
966 if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
967 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
968 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
969 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
970 return;
971 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972 }
973
974 if (key_info & WPA_KEY_INFO_REQUEST) {
975 if (sm->req_replay_counter_used &&
976 os_memcmp(key->replay_counter, sm->req_replay_counter,
977 WPA_REPLAY_COUNTER_LEN) <= 0) {
978 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
979 "received EAPOL-Key request with "
980 "replayed counter");
981 return;
982 }
983 }
984
985 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800986 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 int i;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800988
989 if (msg == PAIRWISE_2 &&
990 wpa_replay_counter_valid(sm->prev_key_replay,
991 key->replay_counter) &&
992 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
993 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
994 {
995 /*
996 * Some supplicant implementations (e.g., Windows XP
997 * WZC) update SNonce for each EAPOL-Key 2/4. This
998 * breaks the workaround on accepting any of the
999 * pending requests, so allow the SNonce to be updated
1000 * even if we have already sent out EAPOL-Key 3/4.
1001 */
1002 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1003 "Process SNonce update from STA "
1004 "based on retransmitted EAPOL-Key "
1005 "1/4");
1006 sm->update_snonce = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001007 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1008 sm->alt_snonce_valid = TRUE;
1009 os_memcpy(sm->alt_replay_counter,
1010 sm->key_replay[0].counter,
1011 WPA_REPLAY_COUNTER_LEN);
1012 goto continue_processing;
1013 }
1014
1015 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1016 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1017 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1018 WPA_REPLAY_COUNTER_LEN) == 0) {
1019 /*
1020 * Supplicant may still be using the old SNonce since
1021 * there was two EAPOL-Key 2/4 messages and they had
1022 * different SNonce values.
1023 */
1024 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1025 "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 -08001026 goto continue_processing;
1027 }
1028
1029 if (msg == PAIRWISE_2 &&
1030 wpa_replay_counter_valid(sm->prev_key_replay,
1031 key->replay_counter) &&
1032 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1033 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1034 "ignore retransmitted EAPOL-Key %s - "
1035 "SNonce did not change", msgtxt);
1036 } else {
1037 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1038 "received EAPOL-Key %s with "
1039 "unexpected replay counter", msgtxt);
1040 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1042 if (!sm->key_replay[i].valid)
1043 break;
1044 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1045 sm->key_replay[i].counter,
1046 WPA_REPLAY_COUNTER_LEN);
1047 }
1048 wpa_hexdump(MSG_DEBUG, "received replay counter",
1049 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1050 return;
1051 }
1052
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001053continue_processing:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 switch (msg) {
1055 case PAIRWISE_2:
1056 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001057 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1058 (!sm->update_snonce ||
1059 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1061 "received EAPOL-Key msg 2/4 in "
1062 "invalid state (%d) - dropped",
1063 sm->wpa_ptk_state);
1064 return;
1065 }
1066 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1067 if (sm->group->reject_4way_hs_for_entropy) {
1068 /*
1069 * The system did not have enough entropy to generate
1070 * strong random numbers. Reject the first 4-way
1071 * handshake(s) and collect some entropy based on the
1072 * information from it. Once enough entropy is
1073 * available, the next atempt will trigger GMK/Key
1074 * Counter update and the station will be allowed to
1075 * continue.
1076 */
1077 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1078 "collect more entropy for random number "
1079 "generation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 random_mark_pool_ready();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 wpa_sta_disconnect(wpa_auth, sm->addr);
1082 return;
1083 }
1084 if (wpa_parse_kde_ies((u8 *) (key + 1), key_data_length,
1085 &kde) < 0) {
1086 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1087 "received EAPOL-Key msg 2/4 with "
1088 "invalid Key Data contents");
1089 return;
1090 }
1091 if (kde.rsn_ie) {
1092 eapol_key_ie = kde.rsn_ie;
1093 eapol_key_ie_len = kde.rsn_ie_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001094 } else if (kde.osen) {
1095 eapol_key_ie = kde.osen;
1096 eapol_key_ie_len = kde.osen_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 } else {
1098 eapol_key_ie = kde.wpa_ie;
1099 eapol_key_ie_len = kde.wpa_ie_len;
1100 }
1101 ft = sm->wpa == WPA_VERSION_WPA2 &&
1102 wpa_key_mgmt_ft(sm->wpa_key_mgmt);
1103 if (sm->wpa_ie == NULL ||
1104 wpa_compare_rsn_ie(ft,
1105 sm->wpa_ie, sm->wpa_ie_len,
1106 eapol_key_ie, eapol_key_ie_len)) {
1107 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1108 "WPA IE from (Re)AssocReq did not "
1109 "match with msg 2/4");
1110 if (sm->wpa_ie) {
1111 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
1112 sm->wpa_ie, sm->wpa_ie_len);
1113 }
1114 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
1115 eapol_key_ie, eapol_key_ie_len);
1116 /* MLME-DEAUTHENTICATE.request */
1117 wpa_sta_disconnect(wpa_auth, sm->addr);
1118 return;
1119 }
1120#ifdef CONFIG_IEEE80211R
1121 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
1122 wpa_sta_disconnect(wpa_auth, sm->addr);
1123 return;
1124 }
1125#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001126#ifdef CONFIG_P2P
1127 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
1128 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
1129 int idx;
1130 wpa_printf(MSG_DEBUG, "P2P: IP address requested in "
1131 "EAPOL-Key exchange");
1132 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
1133 if (idx >= 0) {
1134 u32 start = WPA_GET_BE32(wpa_auth->conf.
1135 ip_addr_start);
1136 bitfield_set(wpa_auth->ip_pool, idx);
1137 WPA_PUT_BE32(sm->ip_addr, start + idx);
1138 wpa_printf(MSG_DEBUG, "P2P: Assigned IP "
1139 "address %u.%u.%u.%u to " MACSTR,
1140 sm->ip_addr[0], sm->ip_addr[1],
1141 sm->ip_addr[2], sm->ip_addr[3],
1142 MAC2STR(sm->addr));
1143 }
1144 }
1145#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 break;
1147 case PAIRWISE_4:
1148 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1149 !sm->PTK_valid) {
1150 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1151 "received EAPOL-Key msg 4/4 in "
1152 "invalid state (%d) - dropped",
1153 sm->wpa_ptk_state);
1154 return;
1155 }
1156 break;
1157 case GROUP_2:
1158 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1159 || !sm->PTK_valid) {
1160 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1161 "received EAPOL-Key msg 2/2 in "
1162 "invalid state (%d) - dropped",
1163 sm->wpa_ptk_group_state);
1164 return;
1165 }
1166 break;
1167#ifdef CONFIG_PEERKEY
1168 case SMK_M1:
1169 case SMK_M3:
1170 case SMK_ERROR:
1171 if (!wpa_auth->conf.peerkey) {
1172 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1173 "PeerKey use disabled - ignoring message");
1174 return;
1175 }
1176 if (!sm->PTK_valid) {
1177 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1178 "received EAPOL-Key msg SMK in "
1179 "invalid state - dropped");
1180 return;
1181 }
1182 break;
1183#else /* CONFIG_PEERKEY */
1184 case SMK_M1:
1185 case SMK_M3:
1186 case SMK_ERROR:
1187 return; /* STSL disabled - ignore SMK messages */
1188#endif /* CONFIG_PEERKEY */
1189 case REQUEST:
1190 break;
1191 }
1192
1193 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1194 "received EAPOL-Key frame (%s)", msgtxt);
1195
1196 if (key_info & WPA_KEY_INFO_ACK) {
1197 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1198 "received invalid EAPOL-Key: Key Ack set");
1199 return;
1200 }
1201
1202 if (!(key_info & WPA_KEY_INFO_MIC)) {
1203 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1204 "received invalid EAPOL-Key: Key MIC not set");
1205 return;
1206 }
1207
1208 sm->MICVerified = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001209 if (sm->PTK_valid && !sm->update_snonce) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001210 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
1211 data_len) &&
1212 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1213 wpa_try_alt_snonce(sm, data, data_len))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001214 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1215 "received EAPOL-Key with invalid MIC");
1216 return;
1217 }
1218 sm->MICVerified = TRUE;
1219 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1220 sm->pending_1_of_4_timeout = 0;
1221 }
1222
1223 if (key_info & WPA_KEY_INFO_REQUEST) {
1224 if (sm->MICVerified) {
1225 sm->req_replay_counter_used = 1;
1226 os_memcpy(sm->req_replay_counter, key->replay_counter,
1227 WPA_REPLAY_COUNTER_LEN);
1228 } else {
1229 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1230 "received EAPOL-Key request with "
1231 "invalid MIC");
1232 return;
1233 }
1234
1235 /*
1236 * TODO: should decrypt key data field if encryption was used;
1237 * even though MAC address KDE is not normally encrypted,
1238 * supplicant is allowed to encrypt it.
1239 */
1240 if (msg == SMK_ERROR) {
1241#ifdef CONFIG_PEERKEY
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001242 wpa_smk_error(wpa_auth, sm, (const u8 *) (key + 1),
1243 key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244#endif /* CONFIG_PEERKEY */
1245 return;
1246 } else if (key_info & WPA_KEY_INFO_ERROR) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001247 if (wpa_receive_error_report(
1248 wpa_auth, sm,
1249 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1250 return; /* STA entry was removed */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1252 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1253 "received EAPOL-Key Request for new "
1254 "4-Way Handshake");
1255 wpa_request_new_ptk(sm);
1256#ifdef CONFIG_PEERKEY
1257 } else if (msg == SMK_M1) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001258 wpa_smk_m1(wpa_auth, sm, key, (const u8 *) (key + 1),
1259 key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260#endif /* CONFIG_PEERKEY */
1261 } else if (key_data_length > 0 &&
1262 wpa_parse_kde_ies((const u8 *) (key + 1),
1263 key_data_length, &kde) == 0 &&
1264 kde.mac_addr) {
1265 } else {
1266 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1267 "received EAPOL-Key Request for GTK "
1268 "rekeying");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1270 wpa_rekey_gtk(wpa_auth, NULL);
1271 }
1272 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001273 /* Do not allow the same key replay counter to be reused. */
1274 wpa_replay_counter_mark_invalid(sm->key_replay,
1275 key->replay_counter);
1276
1277 if (msg == PAIRWISE_2) {
1278 /*
1279 * Maintain a copy of the pending EAPOL-Key frames in
1280 * case the EAPOL-Key frame was retransmitted. This is
1281 * needed to allow EAPOL-Key msg 2/4 reply to another
1282 * pending msg 1/4 to update the SNonce to work around
1283 * unexpected supplicant behavior.
1284 */
1285 os_memcpy(sm->prev_key_replay, sm->key_replay,
1286 sizeof(sm->key_replay));
1287 } else {
1288 os_memset(sm->prev_key_replay, 0,
1289 sizeof(sm->prev_key_replay));
1290 }
1291
1292 /*
1293 * Make sure old valid counters are not accepted anymore and
1294 * do not get copied again.
1295 */
1296 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297 }
1298
1299#ifdef CONFIG_PEERKEY
1300 if (msg == SMK_M3) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001301 wpa_smk_m3(wpa_auth, sm, key, (const u8 *) (key + 1),
1302 key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 return;
1304 }
1305#endif /* CONFIG_PEERKEY */
1306
1307 os_free(sm->last_rx_eapol_key);
1308 sm->last_rx_eapol_key = os_malloc(data_len);
1309 if (sm->last_rx_eapol_key == NULL)
1310 return;
1311 os_memcpy(sm->last_rx_eapol_key, data, data_len);
1312 sm->last_rx_eapol_key_len = data_len;
1313
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001314 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 sm->EAPOLKeyReceived = TRUE;
1316 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1317 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1318 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1319 wpa_sm_step(sm);
1320}
1321
1322
1323static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1324 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1325{
1326 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
1327 u8 *pos;
1328 int ret = 0;
1329
1330 /* GTK = PRF-X(GMK, "Group key expansion",
1331 * AA || GNonce || Time || random data)
1332 * The example described in the IEEE 802.11 standard uses only AA and
1333 * GNonce as inputs here. Add some more entropy since this derivation
1334 * is done only at the Authenticator and as such, does not need to be
1335 * exactly same.
1336 */
1337 os_memcpy(data, addr, ETH_ALEN);
1338 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1339 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1340 wpa_get_ntp_timestamp(pos);
1341 pos += 8;
1342 if (random_get_bytes(pos, 16) < 0)
1343 ret = -1;
1344
1345#ifdef CONFIG_IEEE80211W
1346 sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
1347#else /* CONFIG_IEEE80211W */
1348 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
1349 < 0)
1350 ret = -1;
1351#endif /* CONFIG_IEEE80211W */
1352
1353 return ret;
1354}
1355
1356
1357static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1358{
1359 struct wpa_authenticator *wpa_auth = eloop_ctx;
1360 struct wpa_state_machine *sm = timeout_ctx;
1361
1362 sm->pending_1_of_4_timeout = 0;
1363 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1364 sm->TimeoutEvt = TRUE;
1365 wpa_sm_step(sm);
1366}
1367
1368
1369void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1370 struct wpa_state_machine *sm, int key_info,
1371 const u8 *key_rsc, const u8 *nonce,
1372 const u8 *kde, size_t kde_len,
1373 int keyidx, int encr, int force_version)
1374{
1375 struct ieee802_1x_hdr *hdr;
1376 struct wpa_eapol_key *key;
1377 size_t len;
1378 int alg;
1379 int key_data_len, pad_len = 0;
1380 u8 *buf, *pos;
1381 int version, pairwise;
1382 int i;
1383
1384 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
1385
1386 if (force_version)
1387 version = force_version;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001388 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1389 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001390 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391 else if (wpa_use_aes_cmac(sm))
1392 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001393 else if (sm->pairwise != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1395 else
1396 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1397
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001398 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399
1400 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1401 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1402 "encr=%d)",
1403 version,
1404 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1405 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1406 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1407 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1408 pairwise, (unsigned long) kde_len, keyidx, encr);
1409
1410 key_data_len = kde_len;
1411
1412 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001413 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001414 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1416 pad_len = key_data_len % 8;
1417 if (pad_len)
1418 pad_len = 8 - pad_len;
1419 key_data_len += pad_len + 8;
1420 }
1421
1422 len += key_data_len;
1423
1424 hdr = os_zalloc(len);
1425 if (hdr == NULL)
1426 return;
1427 hdr->version = wpa_auth->conf.eapol_version;
1428 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1429 hdr->length = host_to_be16(len - sizeof(*hdr));
1430 key = (struct wpa_eapol_key *) (hdr + 1);
1431
1432 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1433 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1434 key_info |= version;
1435 if (encr && sm->wpa == WPA_VERSION_WPA2)
1436 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1437 if (sm->wpa != WPA_VERSION_WPA2)
1438 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1439 WPA_PUT_BE16(key->key_info, key_info);
1440
1441 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001442 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1444 WPA_PUT_BE16(key->key_length, 0);
1445
1446 /* FIX: STSL: what to use as key_replay_counter? */
1447 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1448 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1449 os_memcpy(sm->key_replay[i].counter,
1450 sm->key_replay[i - 1].counter,
1451 WPA_REPLAY_COUNTER_LEN);
1452 }
1453 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1454 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1455 WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001456 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1457 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 sm->key_replay[0].valid = TRUE;
1459
1460 if (nonce)
1461 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1462
1463 if (key_rsc)
1464 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1465
1466 if (kde && !encr) {
1467 os_memcpy(key + 1, kde, kde_len);
1468 WPA_PUT_BE16(key->key_data_length, kde_len);
1469 } else if (encr && kde) {
1470 buf = os_zalloc(key_data_len);
1471 if (buf == NULL) {
1472 os_free(hdr);
1473 return;
1474 }
1475 pos = buf;
1476 os_memcpy(pos, kde, kde_len);
1477 pos += kde_len;
1478
1479 if (pad_len)
1480 *pos++ = 0xdd;
1481
1482 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1483 buf, key_data_len);
1484 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001485 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001486 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001487 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001488 if (aes_wrap(sm->PTK.kek, 16,
1489 (key_data_len - 8) / 8, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 (u8 *) (key + 1))) {
1491 os_free(hdr);
1492 os_free(buf);
1493 return;
1494 }
1495 WPA_PUT_BE16(key->key_data_length, key_data_len);
1496 } else {
1497 u8 ek[32];
1498 os_memcpy(key->key_iv,
1499 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1500 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1501 os_memcpy(ek, key->key_iv, 16);
1502 os_memcpy(ek + 16, sm->PTK.kek, 16);
1503 os_memcpy(key + 1, buf, key_data_len);
1504 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1505 WPA_PUT_BE16(key->key_data_length, key_data_len);
1506 }
1507 os_free(buf);
1508 }
1509
1510 if (key_info & WPA_KEY_INFO_MIC) {
1511 if (!sm->PTK_valid) {
1512 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1513 "PTK not valid when sending EAPOL-Key "
1514 "frame");
1515 os_free(hdr);
1516 return;
1517 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001518 wpa_eapol_key_mic(sm->PTK.kck, sm->wpa_key_mgmt, version,
1519 (u8 *) hdr, len, key->key_mic);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001520#ifdef CONFIG_TESTING_OPTIONS
1521 if (!pairwise &&
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001522 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001523 drand48() <
1524 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1525 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1526 "Corrupting group EAPOL-Key Key MIC");
1527 key->key_mic[0]++;
1528 }
1529#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530 }
1531
1532 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1533 1);
1534 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1535 sm->pairwise_set);
1536 os_free(hdr);
1537}
1538
1539
1540static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1541 struct wpa_state_machine *sm, int key_info,
1542 const u8 *key_rsc, const u8 *nonce,
1543 const u8 *kde, size_t kde_len,
1544 int keyidx, int encr)
1545{
1546 int timeout_ms;
1547 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1548 int ctr;
1549
1550 if (sm == NULL)
1551 return;
1552
1553 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1554 keyidx, encr, 0);
1555
1556 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1557 if (ctr == 1 && wpa_auth->conf.tx_status)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001558 timeout_ms = pairwise ? eapol_key_timeout_first :
1559 eapol_key_timeout_first_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560 else
1561 timeout_ms = eapol_key_timeout_subseq;
1562 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1563 sm->pending_1_of_4_timeout = 1;
1564 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1565 "counter %d)", timeout_ms, ctr);
1566 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1567 wpa_send_eapol_timeout, wpa_auth, sm);
1568}
1569
1570
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001571static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
1572 size_t data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573{
1574 struct ieee802_1x_hdr *hdr;
1575 struct wpa_eapol_key *key;
1576 u16 key_info;
1577 int ret = 0;
1578 u8 mic[16];
1579
1580 if (data_len < sizeof(*hdr) + sizeof(*key))
1581 return -1;
1582
1583 hdr = (struct ieee802_1x_hdr *) data;
1584 key = (struct wpa_eapol_key *) (hdr + 1);
1585 key_info = WPA_GET_BE16(key->key_info);
1586 os_memcpy(mic, key->key_mic, 16);
1587 os_memset(key->key_mic, 0, 16);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001588 if (wpa_eapol_key_mic(PTK->kck, akmp, key_info & WPA_KEY_INFO_TYPE_MASK,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001589 data, data_len, key->key_mic) ||
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001590 os_memcmp_const(mic, key->key_mic, 16) != 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591 ret = -1;
1592 os_memcpy(key->key_mic, mic, 16);
1593 return ret;
1594}
1595
1596
1597void wpa_remove_ptk(struct wpa_state_machine *sm)
1598{
1599 sm->PTK_valid = FALSE;
1600 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1601 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
1602 sm->pairwise_set = FALSE;
1603 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1604}
1605
1606
1607int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1608{
1609 int remove_ptk = 1;
1610
1611 if (sm == NULL)
1612 return -1;
1613
1614 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1615 "event %d notification", event);
1616
1617 switch (event) {
1618 case WPA_AUTH:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001619#ifdef CONFIG_MESH
1620 /* PTKs are derived through AMPE */
1621 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1622 /* not mesh */
1623 break;
1624 }
1625 return 0;
1626#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627 case WPA_ASSOC:
1628 break;
1629 case WPA_DEAUTH:
1630 case WPA_DISASSOC:
1631 sm->DeauthenticationRequest = TRUE;
1632 break;
1633 case WPA_REAUTH:
1634 case WPA_REAUTH_EAPOL:
1635 if (!sm->started) {
1636 /*
1637 * When using WPS, we may end up here if the STA
1638 * manages to re-associate without the previous STA
1639 * entry getting removed. Consequently, we need to make
1640 * sure that the WPA state machines gets initialized
1641 * properly at this point.
1642 */
1643 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1644 "started - initialize now");
1645 sm->started = 1;
1646 sm->Init = TRUE;
1647 if (wpa_sm_step(sm) == 1)
1648 return 1; /* should not really happen */
1649 sm->Init = FALSE;
1650 sm->AuthenticationRequest = TRUE;
1651 break;
1652 }
1653 if (sm->GUpdateStationKeys) {
1654 /*
1655 * Reauthentication cancels the pending group key
1656 * update for this STA.
1657 */
1658 sm->group->GKeyDoneStations--;
1659 sm->GUpdateStationKeys = FALSE;
1660 sm->PtkGroupInit = TRUE;
1661 }
1662 sm->ReAuthenticationRequest = TRUE;
1663 break;
1664 case WPA_ASSOC_FT:
1665#ifdef CONFIG_IEEE80211R
1666 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1667 "after association");
1668 wpa_ft_install_ptk(sm);
1669
1670 /* Using FT protocol, not WPA auth state machine */
1671 sm->ft_completed = 1;
1672 return 0;
1673#else /* CONFIG_IEEE80211R */
1674 break;
1675#endif /* CONFIG_IEEE80211R */
1676 }
1677
1678#ifdef CONFIG_IEEE80211R
1679 sm->ft_completed = 0;
1680#endif /* CONFIG_IEEE80211R */
1681
1682#ifdef CONFIG_IEEE80211W
1683 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1684 remove_ptk = 0;
1685#endif /* CONFIG_IEEE80211W */
1686
1687 if (remove_ptk) {
1688 sm->PTK_valid = FALSE;
1689 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1690
1691 if (event != WPA_REAUTH_EAPOL)
1692 wpa_remove_ptk(sm);
1693 }
1694
1695 return wpa_sm_step(sm);
1696}
1697
1698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699SM_STATE(WPA_PTK, INITIALIZE)
1700{
1701 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1702 if (sm->Init) {
1703 /* Init flag is not cleared here, so avoid busy
1704 * loop by claiming nothing changed. */
1705 sm->changed = FALSE;
1706 }
1707
1708 sm->keycount = 0;
1709 if (sm->GUpdateStationKeys)
1710 sm->group->GKeyDoneStations--;
1711 sm->GUpdateStationKeys = FALSE;
1712 if (sm->wpa == WPA_VERSION_WPA)
1713 sm->PInitAKeys = FALSE;
1714 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1715 * Local AA > Remote AA)) */) {
1716 sm->Pair = TRUE;
1717 }
1718 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1719 wpa_remove_ptk(sm);
1720 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1721 sm->TimeoutCtr = 0;
1722 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1723 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1724 WPA_EAPOL_authorized, 0);
1725 }
1726}
1727
1728
1729SM_STATE(WPA_PTK, DISCONNECT)
1730{
1731 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1732 sm->Disconnect = FALSE;
1733 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1734}
1735
1736
1737SM_STATE(WPA_PTK, DISCONNECTED)
1738{
1739 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1740 sm->DeauthenticationRequest = FALSE;
1741}
1742
1743
1744SM_STATE(WPA_PTK, AUTHENTICATION)
1745{
1746 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1747 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1748 sm->PTK_valid = FALSE;
1749 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1750 1);
1751 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1752 sm->AuthenticationRequest = FALSE;
1753}
1754
1755
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001756static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1757 struct wpa_group *group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001759 if (group->first_sta_seen)
1760 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761 /*
1762 * System has run bit further than at the time hostapd was started
1763 * potentially very early during boot up. This provides better chances
1764 * of collecting more randomness on embedded systems. Re-initialize the
1765 * GMK and Counter here to improve their strength if there was not
1766 * enough entropy available immediately after system startup.
1767 */
1768 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1769 "station");
1770 if (random_pool_ready() != 1) {
1771 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1772 "to proceed - reject first 4-way handshake");
1773 group->reject_4way_hs_for_entropy = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001774 } else {
1775 group->first_sta_seen = TRUE;
1776 group->reject_4way_hs_for_entropy = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001778
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779 wpa_group_init_gmk_and_counter(wpa_auth, group);
1780 wpa_gtk_update(wpa_auth, group);
1781 wpa_group_config_group_keys(wpa_auth, group);
1782}
1783
1784
1785SM_STATE(WPA_PTK, AUTHENTICATION2)
1786{
1787 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1788
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001789 wpa_group_ensure_init(sm->wpa_auth, sm->group);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001790 sm->ReAuthenticationRequest = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791
Dmitry Shmidt04949592012-07-19 12:16:46 -07001792 /*
1793 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1794 * ambiguous. The Authenticator state machine uses a counter that is
1795 * incremented by one for each 4-way handshake. However, the security
1796 * analysis of 4-way handshake points out that unpredictable nonces
1797 * help in preventing precomputation attacks. Instead of the state
1798 * machine definition, use an unpredictable nonce value here to provide
1799 * stronger protection against potential precomputation attacks.
1800 */
1801 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1802 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1803 "ANonce.");
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001804 sm->Disconnect = TRUE;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001805 return;
1806 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1808 WPA_NONCE_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1810 * logical place than INITIALIZE since AUTHENTICATION2 can be
1811 * re-entered on ReAuthenticationRequest without going through
1812 * INITIALIZE. */
1813 sm->TimeoutCtr = 0;
1814}
1815
1816
1817SM_STATE(WPA_PTK, INITPMK)
1818{
1819 u8 msk[2 * PMK_LEN];
1820 size_t len = 2 * PMK_LEN;
1821
1822 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1823#ifdef CONFIG_IEEE80211R
1824 sm->xxkey_len = 0;
1825#endif /* CONFIG_IEEE80211R */
1826 if (sm->pmksa) {
1827 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1828 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1829 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1830 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1831 "(len=%lu)", (unsigned long) len);
1832 os_memcpy(sm->PMK, msk, PMK_LEN);
1833#ifdef CONFIG_IEEE80211R
1834 if (len >= 2 * PMK_LEN) {
1835 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1836 sm->xxkey_len = PMK_LEN;
1837 }
1838#endif /* CONFIG_IEEE80211R */
1839 } else {
1840 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1841 }
1842
1843 sm->req_replay_counter_used = 0;
1844 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1845 * will break reauthentication since EAPOL state machines may not be
1846 * get into AUTHENTICATING state that clears keyRun before WPA state
1847 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1848 * state and takes PMK from the previously used AAA Key. This will
1849 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1850 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1851 * be good workaround for this issue. */
1852 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1853}
1854
1855
1856SM_STATE(WPA_PTK, INITPSK)
1857{
1858 const u8 *psk;
1859 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001860 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001861 if (psk) {
1862 os_memcpy(sm->PMK, psk, PMK_LEN);
1863#ifdef CONFIG_IEEE80211R
1864 os_memcpy(sm->xxkey, psk, PMK_LEN);
1865 sm->xxkey_len = PMK_LEN;
1866#endif /* CONFIG_IEEE80211R */
1867 }
1868 sm->req_replay_counter_used = 0;
1869}
1870
1871
1872SM_STATE(WPA_PTK, PTKSTART)
1873{
1874 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1875 size_t pmkid_len = 0;
1876
1877 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1878 sm->PTKRequest = FALSE;
1879 sm->TimeoutEvt = FALSE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001880 sm->alt_snonce_valid = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881
1882 sm->TimeoutCtr++;
1883 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1884 /* No point in sending the EAPOL-Key - we will disconnect
1885 * immediately following this. */
1886 return;
1887 }
1888
1889 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1890 "sending 1/4 msg of 4-Way Handshake");
1891 /*
1892 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1893 * one possible PSK for this STA.
1894 */
1895 if (sm->wpa == WPA_VERSION_WPA2 &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001896 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1897 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 pmkid = buf;
1899 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1900 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1901 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1902 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001903 if (sm->pmksa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1905 sm->pmksa->pmkid, PMKID_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001906 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
1907 /* No KCK available to derive PMKID */
1908 pmkid = NULL;
1909 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001910 /*
1911 * Calculate PMKID since no PMKSA cache entry was
1912 * available with pre-calculated PMKID.
1913 */
1914 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1915 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1916 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1917 }
1918 }
1919 wpa_send_eapol(sm->wpa_auth, sm,
1920 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1921 sm->ANonce, pmkid, pmkid_len, 0, 0);
1922}
1923
1924
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001925static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
1926 const u8 *pmk, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927{
Dmitry Shmidt98660862014-03-11 17:26:21 -07001928 size_t ptk_len = wpa_cipher_key_len(sm->pairwise) + 32;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929#ifdef CONFIG_IEEE80211R
1930 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1931 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1932#endif /* CONFIG_IEEE80211R */
1933
1934 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001935 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001936 (u8 *) ptk, ptk_len,
1937 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1938
1939 return 0;
1940}
1941
1942
1943SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1944{
1945 struct wpa_ptk PTK;
1946 int ok = 0;
1947 const u8 *pmk = NULL;
1948
1949 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1950 sm->EAPOLKeyReceived = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001951 sm->update_snonce = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952
1953 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1954 * WPA-PSK: iterate through possible PSKs and select the one matching
1955 * the packet */
1956 for (;;) {
1957 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001958 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
1959 sm->p2p_dev_addr, pmk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960 if (pmk == NULL)
1961 break;
1962 } else
1963 pmk = sm->PMK;
1964
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001965 wpa_derive_ptk(sm, sm->SNonce, pmk, &PTK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001967 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
1968 sm->last_rx_eapol_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969 sm->last_rx_eapol_key_len) == 0) {
1970 ok = 1;
1971 break;
1972 }
1973
1974 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1975 break;
1976 }
1977
1978 if (!ok) {
1979 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1980 "invalid MIC in msg 2/4 of 4-Way Handshake");
1981 return;
1982 }
1983
1984#ifdef CONFIG_IEEE80211R
1985 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1986 /*
1987 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
1988 * with the value we derived.
1989 */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001990 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
1991 WPA_PMK_NAME_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001992 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1993 "PMKR1Name mismatch in FT 4-way "
1994 "handshake");
1995 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
1996 "Supplicant",
1997 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
1998 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1999 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2000 return;
2001 }
2002 }
2003#endif /* CONFIG_IEEE80211R */
2004
2005 sm->pending_1_of_4_timeout = 0;
2006 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
2007
2008 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2009 /* PSK may have changed from the previous choice, so update
2010 * state machine data based on whatever PSK was selected here.
2011 */
2012 os_memcpy(sm->PMK, pmk, PMK_LEN);
2013 }
2014
2015 sm->MICVerified = TRUE;
2016
2017 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
2018 sm->PTK_valid = TRUE;
2019}
2020
2021
2022SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2023{
2024 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2025 sm->TimeoutCtr = 0;
2026}
2027
2028
2029#ifdef CONFIG_IEEE80211W
2030
2031static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2032{
2033 if (sm->mgmt_frame_prot) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002034 size_t len;
2035 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2036 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002037 }
2038
2039 return 0;
2040}
2041
2042
2043static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2044{
2045 struct wpa_igtk_kde igtk;
2046 struct wpa_group *gsm = sm->group;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002047 u8 rsc[WPA_KEY_RSC_LEN];
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002048 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049
2050 if (!sm->mgmt_frame_prot)
2051 return pos;
2052
2053 igtk.keyid[0] = gsm->GN_igtk;
2054 igtk.keyid[1] = 0;
2055 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002056 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057 os_memset(igtk.pn, 0, sizeof(igtk.pn));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002058 else
2059 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002060 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002061 if (sm->wpa_auth->conf.disable_gtk) {
2062 /*
2063 * Provide unique random IGTK to each STA to prevent use of
2064 * IGTK in the BSS.
2065 */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002066 if (random_get_bytes(igtk.igtk, len) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002067 return pos;
2068 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002070 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
2071 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072
2073 return pos;
2074}
2075
2076#else /* CONFIG_IEEE80211W */
2077
2078static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2079{
2080 return 0;
2081}
2082
2083
2084static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2085{
2086 return pos;
2087}
2088
2089#endif /* CONFIG_IEEE80211W */
2090
2091
2092SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2093{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002094 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095 size_t gtk_len, kde_len;
2096 struct wpa_group *gsm = sm->group;
2097 u8 *wpa_ie;
2098 int wpa_ie_len, secure, keyidx, encr = 0;
2099
2100 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2101 sm->TimeoutEvt = FALSE;
2102
2103 sm->TimeoutCtr++;
2104 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
2105 /* No point in sending the EAPOL-Key - we will disconnect
2106 * immediately following this. */
2107 return;
2108 }
2109
2110 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2111 GTK[GN], IGTK, [FTIE], [TIE * 2])
2112 */
2113 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2114 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2115 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2116 wpa_ie = sm->wpa_auth->wpa_ie;
2117 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
2118 if (sm->wpa == WPA_VERSION_WPA &&
2119 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
2120 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002121 /* WPA-only STA, remove RSN IE and possible MDIE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002123 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
2124 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125 wpa_ie_len = wpa_ie[1] + 2;
2126 }
2127 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2128 "sending 3/4 msg of 4-Way Handshake");
2129 if (sm->wpa == WPA_VERSION_WPA2) {
2130 /* WPA2 send GTK in the 4-way handshake */
2131 secure = 1;
2132 gtk = gsm->GTK[gsm->GN - 1];
2133 gtk_len = gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002134 if (sm->wpa_auth->conf.disable_gtk) {
2135 /*
2136 * Provide unique random GTK to each STA to prevent use
2137 * of GTK in the BSS.
2138 */
2139 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
2140 return;
2141 gtk = dummy_gtk;
2142 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143 keyidx = gsm->GN;
2144 _rsc = rsc;
2145 encr = 1;
2146 } else {
2147 /* WPA does not include GTK in msg 3/4 */
2148 secure = 0;
2149 gtk = NULL;
2150 gtk_len = 0;
2151 keyidx = 0;
2152 _rsc = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002153 if (sm->rx_eapol_key_secure) {
2154 /*
2155 * It looks like Windows 7 supplicant tries to use
2156 * Secure bit in msg 2/4 after having reported Michael
2157 * MIC failure and it then rejects the 4-way handshake
2158 * if msg 3/4 does not set Secure bit. Work around this
2159 * by setting the Secure bit here even in the case of
2160 * WPA if the supplicant used it first.
2161 */
2162 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2163 "STA used Secure bit in WPA msg 2/4 - "
2164 "set Secure for 3/4 as workaround");
2165 secure = 1;
2166 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167 }
2168
2169 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
2170 if (gtk)
2171 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
2172#ifdef CONFIG_IEEE80211R
2173 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2174 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
2175 kde_len += 300; /* FTIE + 2 * TIE */
2176 }
2177#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002178#ifdef CONFIG_P2P
2179 if (WPA_GET_BE32(sm->ip_addr) > 0)
2180 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
2181#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 kde = os_malloc(kde_len);
2183 if (kde == NULL)
2184 return;
2185
2186 pos = kde;
2187 os_memcpy(pos, wpa_ie, wpa_ie_len);
2188 pos += wpa_ie_len;
2189#ifdef CONFIG_IEEE80211R
2190 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2191 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
2192 if (res < 0) {
2193 wpa_printf(MSG_ERROR, "FT: Failed to insert "
2194 "PMKR1Name into RSN IE in EAPOL-Key data");
2195 os_free(kde);
2196 return;
2197 }
2198 pos += res;
2199 }
2200#endif /* CONFIG_IEEE80211R */
2201 if (gtk) {
2202 u8 hdr[2];
2203 hdr[0] = keyidx & 0x03;
2204 hdr[1] = 0;
2205 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2206 gtk, gtk_len);
2207 }
2208 pos = ieee80211w_kde_add(sm, pos);
2209
2210#ifdef CONFIG_IEEE80211R
2211 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2212 int res;
2213 struct wpa_auth_config *conf;
2214
2215 conf = &sm->wpa_auth->conf;
2216 res = wpa_write_ftie(conf, conf->r0_key_holder,
2217 conf->r0_key_holder_len,
2218 NULL, NULL, pos, kde + kde_len - pos,
2219 NULL, 0);
2220 if (res < 0) {
2221 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2222 "into EAPOL-Key Key Data");
2223 os_free(kde);
2224 return;
2225 }
2226 pos += res;
2227
2228 /* TIE[ReassociationDeadline] (TU) */
2229 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2230 *pos++ = 5;
2231 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2232 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2233 pos += 4;
2234
2235 /* TIE[KeyLifetime] (seconds) */
2236 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2237 *pos++ = 5;
2238 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2239 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2240 pos += 4;
2241 }
2242#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002243#ifdef CONFIG_P2P
2244 if (WPA_GET_BE32(sm->ip_addr) > 0) {
2245 u8 addr[3 * 4];
2246 os_memcpy(addr, sm->ip_addr, 4);
2247 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
2248 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
2249 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
2250 addr, sizeof(addr), NULL, 0);
2251 }
2252#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253
2254 wpa_send_eapol(sm->wpa_auth, sm,
2255 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
2256 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2257 WPA_KEY_INFO_KEY_TYPE,
2258 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2259 os_free(kde);
2260}
2261
2262
2263SM_STATE(WPA_PTK, PTKINITDONE)
2264{
2265 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2266 sm->EAPOLKeyReceived = FALSE;
2267 if (sm->Pair) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002268 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2269 int klen = wpa_cipher_key_len(sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002270 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2271 sm->PTK.tk1, klen)) {
2272 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2273 return;
2274 }
2275 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2276 sm->pairwise_set = TRUE;
2277
2278 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2279 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2280 eloop_register_timeout(sm->wpa_auth->conf.
2281 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2282 sm->wpa_auth, sm);
2283 }
2284
2285 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2286 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2287 WPA_EAPOL_authorized, 1);
2288 }
2289 }
2290
2291 if (0 /* IBSS == TRUE */) {
2292 sm->keycount++;
2293 if (sm->keycount == 2) {
2294 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2295 WPA_EAPOL_portValid, 1);
2296 }
2297 } else {
2298 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2299 1);
2300 }
2301 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2302 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2303 if (sm->wpa == WPA_VERSION_WPA)
2304 sm->PInitAKeys = TRUE;
2305 else
2306 sm->has_GTK = TRUE;
2307 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2308 "pairwise key handshake completed (%s)",
2309 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2310
2311#ifdef CONFIG_IEEE80211R
2312 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2313#endif /* CONFIG_IEEE80211R */
2314}
2315
2316
2317SM_STEP(WPA_PTK)
2318{
2319 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2320
2321 if (sm->Init)
2322 SM_ENTER(WPA_PTK, INITIALIZE);
2323 else if (sm->Disconnect
2324 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2325 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2326 "WPA_PTK: sm->Disconnect");
2327 SM_ENTER(WPA_PTK, DISCONNECT);
2328 }
2329 else if (sm->DeauthenticationRequest)
2330 SM_ENTER(WPA_PTK, DISCONNECTED);
2331 else if (sm->AuthenticationRequest)
2332 SM_ENTER(WPA_PTK, AUTHENTICATION);
2333 else if (sm->ReAuthenticationRequest)
2334 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2335 else if (sm->PTKRequest)
2336 SM_ENTER(WPA_PTK, PTKSTART);
2337 else switch (sm->wpa_ptk_state) {
2338 case WPA_PTK_INITIALIZE:
2339 break;
2340 case WPA_PTK_DISCONNECT:
2341 SM_ENTER(WPA_PTK, DISCONNECTED);
2342 break;
2343 case WPA_PTK_DISCONNECTED:
2344 SM_ENTER(WPA_PTK, INITIALIZE);
2345 break;
2346 case WPA_PTK_AUTHENTICATION:
2347 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2348 break;
2349 case WPA_PTK_AUTHENTICATION2:
2350 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2351 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2352 WPA_EAPOL_keyRun) > 0)
2353 SM_ENTER(WPA_PTK, INITPMK);
2354 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
2355 /* FIX: && 802.1X::keyRun */)
2356 SM_ENTER(WPA_PTK, INITPSK);
2357 break;
2358 case WPA_PTK_INITPMK:
2359 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2360 WPA_EAPOL_keyAvailable) > 0)
2361 SM_ENTER(WPA_PTK, PTKSTART);
2362 else {
2363 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2364 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2365 "INITPMK - keyAvailable = false");
2366 SM_ENTER(WPA_PTK, DISCONNECT);
2367 }
2368 break;
2369 case WPA_PTK_INITPSK:
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002370 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
2371 NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 SM_ENTER(WPA_PTK, PTKSTART);
2373 else {
2374 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2375 "no PSK configured for the STA");
2376 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2377 SM_ENTER(WPA_PTK, DISCONNECT);
2378 }
2379 break;
2380 case WPA_PTK_PTKSTART:
2381 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2382 sm->EAPOLKeyPairwise)
2383 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2384 else if (sm->TimeoutCtr >
2385 (int) dot11RSNAConfigPairwiseUpdateCount) {
2386 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2387 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2388 "PTKSTART: Retry limit %d reached",
2389 dot11RSNAConfigPairwiseUpdateCount);
2390 SM_ENTER(WPA_PTK, DISCONNECT);
2391 } else if (sm->TimeoutEvt)
2392 SM_ENTER(WPA_PTK, PTKSTART);
2393 break;
2394 case WPA_PTK_PTKCALCNEGOTIATING:
2395 if (sm->MICVerified)
2396 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2397 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2398 sm->EAPOLKeyPairwise)
2399 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2400 else if (sm->TimeoutEvt)
2401 SM_ENTER(WPA_PTK, PTKSTART);
2402 break;
2403 case WPA_PTK_PTKCALCNEGOTIATING2:
2404 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2405 break;
2406 case WPA_PTK_PTKINITNEGOTIATING:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002407 if (sm->update_snonce)
2408 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2409 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2410 sm->EAPOLKeyPairwise && sm->MICVerified)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411 SM_ENTER(WPA_PTK, PTKINITDONE);
2412 else if (sm->TimeoutCtr >
2413 (int) dot11RSNAConfigPairwiseUpdateCount) {
2414 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2415 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2416 "PTKINITNEGOTIATING: Retry limit %d "
2417 "reached",
2418 dot11RSNAConfigPairwiseUpdateCount);
2419 SM_ENTER(WPA_PTK, DISCONNECT);
2420 } else if (sm->TimeoutEvt)
2421 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2422 break;
2423 case WPA_PTK_PTKINITDONE:
2424 break;
2425 }
2426}
2427
2428
2429SM_STATE(WPA_PTK_GROUP, IDLE)
2430{
2431 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2432 if (sm->Init) {
2433 /* Init flag is not cleared here, so avoid busy
2434 * loop by claiming nothing changed. */
2435 sm->changed = FALSE;
2436 }
2437 sm->GTimeoutCtr = 0;
2438}
2439
2440
2441SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2442{
2443 u8 rsc[WPA_KEY_RSC_LEN];
2444 struct wpa_group *gsm = sm->group;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002445 const u8 *kde;
2446 u8 *kde_buf = NULL, *pos, hdr[2];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447 size_t kde_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002448 u8 *gtk, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449
2450 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
2451
2452 sm->GTimeoutCtr++;
2453 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
2454 /* No point in sending the EAPOL-Key - we will disconnect
2455 * immediately following this. */
2456 return;
2457 }
2458
2459 if (sm->wpa == WPA_VERSION_WPA)
2460 sm->PInitAKeys = FALSE;
2461 sm->TimeoutEvt = FALSE;
2462 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2463 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2464 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2465 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2466 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2467 "sending 1/2 msg of Group Key Handshake");
2468
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002469 gtk = gsm->GTK[gsm->GN - 1];
2470 if (sm->wpa_auth->conf.disable_gtk) {
2471 /*
2472 * Provide unique random GTK to each STA to prevent use
2473 * of GTK in the BSS.
2474 */
2475 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
2476 return;
2477 gtk = dummy_gtk;
2478 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479 if (sm->wpa == WPA_VERSION_WPA2) {
2480 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
2481 ieee80211w_kde_len(sm);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002482 kde_buf = os_malloc(kde_len);
2483 if (kde_buf == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484 return;
2485
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002486 kde = pos = kde_buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487 hdr[0] = gsm->GN & 0x03;
2488 hdr[1] = 0;
2489 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002490 gtk, gsm->GTK_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002491 pos = ieee80211w_kde_add(sm, pos);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002492 kde_len = pos - kde;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002494 kde = gtk;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002495 kde_len = gsm->GTK_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496 }
2497
2498 wpa_send_eapol(sm->wpa_auth, sm,
2499 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
2500 WPA_KEY_INFO_ACK |
2501 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002502 rsc, gsm->GNonce, kde, kde_len, gsm->GN, 1);
2503
2504 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505}
2506
2507
2508SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2509{
2510 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2511 sm->EAPOLKeyReceived = FALSE;
2512 if (sm->GUpdateStationKeys)
2513 sm->group->GKeyDoneStations--;
2514 sm->GUpdateStationKeys = FALSE;
2515 sm->GTimeoutCtr = 0;
2516 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2517 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2518 "group key handshake completed (%s)",
2519 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2520 sm->has_GTK = TRUE;
2521}
2522
2523
2524SM_STATE(WPA_PTK_GROUP, KEYERROR)
2525{
2526 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2527 if (sm->GUpdateStationKeys)
2528 sm->group->GKeyDoneStations--;
2529 sm->GUpdateStationKeys = FALSE;
2530 sm->Disconnect = TRUE;
2531}
2532
2533
2534SM_STEP(WPA_PTK_GROUP)
2535{
2536 if (sm->Init || sm->PtkGroupInit) {
2537 SM_ENTER(WPA_PTK_GROUP, IDLE);
2538 sm->PtkGroupInit = FALSE;
2539 } else switch (sm->wpa_ptk_group_state) {
2540 case WPA_PTK_GROUP_IDLE:
2541 if (sm->GUpdateStationKeys ||
2542 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2543 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2544 break;
2545 case WPA_PTK_GROUP_REKEYNEGOTIATING:
2546 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2547 !sm->EAPOLKeyPairwise && sm->MICVerified)
2548 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2549 else if (sm->GTimeoutCtr >
2550 (int) dot11RSNAConfigGroupUpdateCount)
2551 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2552 else if (sm->TimeoutEvt)
2553 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2554 break;
2555 case WPA_PTK_GROUP_KEYERROR:
2556 SM_ENTER(WPA_PTK_GROUP, IDLE);
2557 break;
2558 case WPA_PTK_GROUP_REKEYESTABLISHED:
2559 SM_ENTER(WPA_PTK_GROUP, IDLE);
2560 break;
2561 }
2562}
2563
2564
2565static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
2566 struct wpa_group *group)
2567{
2568 int ret = 0;
2569
2570 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2571 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2572 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
2573 wpa_auth->addr, group->GNonce,
2574 group->GTK[group->GN - 1], group->GTK_len) < 0)
2575 ret = -1;
2576 wpa_hexdump_key(MSG_DEBUG, "GTK",
2577 group->GTK[group->GN - 1], group->GTK_len);
2578
2579#ifdef CONFIG_IEEE80211W
2580 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002581 size_t len;
2582 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2584 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2585 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
2586 wpa_auth->addr, group->GNonce,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002587 group->IGTK[group->GN_igtk - 4], len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588 ret = -1;
2589 wpa_hexdump_key(MSG_DEBUG, "IGTK",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002590 group->IGTK[group->GN_igtk - 4], len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002591 }
2592#endif /* CONFIG_IEEE80211W */
2593
2594 return ret;
2595}
2596
2597
2598static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2599 struct wpa_group *group)
2600{
2601 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2602 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2603 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2604 group->wpa_group_state = WPA_GROUP_GTK_INIT;
2605
2606 /* GTK[0..N] = 0 */
2607 os_memset(group->GTK, 0, sizeof(group->GTK));
2608 group->GN = 1;
2609 group->GM = 2;
2610#ifdef CONFIG_IEEE80211W
2611 group->GN_igtk = 4;
2612 group->GM_igtk = 5;
2613#endif /* CONFIG_IEEE80211W */
2614 /* GTK[GN] = CalcGTK() */
2615 wpa_gtk_update(wpa_auth, group);
2616}
2617
2618
2619static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2620{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002621 if (ctx != NULL && ctx != sm->group)
2622 return 0;
2623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2625 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2626 "Not in PTKINITDONE; skip Group Key update");
2627 sm->GUpdateStationKeys = FALSE;
2628 return 0;
2629 }
2630 if (sm->GUpdateStationKeys) {
2631 /*
2632 * This should not really happen, so add a debug log entry.
2633 * Since we clear the GKeyDoneStations before the loop, the
2634 * station needs to be counted here anyway.
2635 */
2636 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2637 "GUpdateStationKeys was already set when "
2638 "marking station for GTK rekeying");
2639 }
2640
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002641 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002642 if (sm->is_wnmsleep)
2643 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645 sm->group->GKeyDoneStations++;
2646 sm->GUpdateStationKeys = TRUE;
2647
2648 wpa_sm_step(sm);
2649 return 0;
2650}
2651
2652
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002653#ifdef CONFIG_WNM
2654/* update GTK when exiting WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002655void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
2656{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002657 if (sm == NULL || sm->is_wnmsleep)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002658 return;
2659
2660 wpa_group_update_sta(sm, NULL);
2661}
2662
2663
2664void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
2665{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002666 if (sm)
2667 sm->is_wnmsleep = !!flag;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002668}
2669
2670
2671int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2672{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002673 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002674 u8 *start = pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002675
2676 /*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002677 * GTK subelement:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002678 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002679 * Key[5..32]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002680 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002681 *pos++ = WNM_SLEEP_SUBELEM_GTK;
2682 *pos++ = 11 + gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002683 /* Key ID in B0-B1 of Key Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002684 WPA_PUT_LE16(pos, gsm->GN & 0x03);
2685 pos += 2;
2686 *pos++ = gsm->GTK_len;
2687 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002688 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002689 pos += 8;
2690 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2691 pos += gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002692
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002693 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
2694 gsm->GN);
2695 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002696 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002697
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002698 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002699}
2700
2701
2702#ifdef CONFIG_IEEE80211W
2703int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2704{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002705 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002706 u8 *start = pos;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002707 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002708
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002709 /*
2710 * IGTK subelement:
2711 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
2712 */
2713 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002714 *pos++ = 2 + 6 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002715 WPA_PUT_LE16(pos, gsm->GN_igtk);
2716 pos += 2;
2717 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002718 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002719 pos += 6;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002720
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002721 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
2722 pos += len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002723
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002724 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
2725 gsm->GN_igtk);
2726 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002727 gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002728
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002729 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002730}
2731#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002732#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002733
2734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002735static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2736 struct wpa_group *group)
2737{
2738 int tmp;
2739
2740 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2741 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2742 group->changed = TRUE;
2743 group->wpa_group_state = WPA_GROUP_SETKEYS;
2744 group->GTKReKey = FALSE;
2745 tmp = group->GM;
2746 group->GM = group->GN;
2747 group->GN = tmp;
2748#ifdef CONFIG_IEEE80211W
2749 tmp = group->GM_igtk;
2750 group->GM_igtk = group->GN_igtk;
2751 group->GN_igtk = tmp;
2752#endif /* CONFIG_IEEE80211W */
2753 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2754 * counting the STAs that are marked with GUpdateStationKeys instead of
2755 * including all STAs that could be in not-yet-completed state. */
2756 wpa_gtk_update(wpa_auth, group);
2757
2758 if (group->GKeyDoneStations) {
2759 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
2760 "GKeyDoneStations=%d when starting new GTK rekey",
2761 group->GKeyDoneStations);
2762 group->GKeyDoneStations = 0;
2763 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002764 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2766 group->GKeyDoneStations);
2767}
2768
2769
2770static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
2771 struct wpa_group *group)
2772{
2773 int ret = 0;
2774
2775 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002776 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 broadcast_ether_addr, group->GN,
2778 group->GTK[group->GN - 1], group->GTK_len) < 0)
2779 ret = -1;
2780
2781#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002782 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2783 enum wpa_alg alg;
2784 size_t len;
2785
2786 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
2787 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2788
2789 if (ret == 0 &&
2790 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
2791 broadcast_ether_addr, group->GN_igtk,
2792 group->IGTK[group->GN_igtk - 4], len) < 0)
2793 ret = -1;
2794 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795#endif /* CONFIG_IEEE80211W */
2796
2797 return ret;
2798}
2799
2800
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002801static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
2802{
2803 if (sm->group == ctx) {
2804 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
2805 " for discconnection due to fatal failure",
2806 MAC2STR(sm->addr));
2807 sm->Disconnect = TRUE;
2808 }
2809
2810 return 0;
2811}
2812
2813
2814static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
2815 struct wpa_group *group)
2816{
2817 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
2818 group->changed = TRUE;
2819 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
2820 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
2821}
2822
2823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2825 struct wpa_group *group)
2826{
2827 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2828 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2829 group->changed = TRUE;
2830 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2831
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002832 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
2833 wpa_group_fatal_failure(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002835 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002836
2837 return 0;
2838}
2839
2840
2841static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2842 struct wpa_group *group)
2843{
2844 if (group->GInit) {
2845 wpa_group_gtk_init(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002846 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
2847 /* Do not allow group operations */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2849 group->GTKAuthenticator) {
2850 wpa_group_setkeysdone(wpa_auth, group);
2851 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2852 group->GTKReKey) {
2853 wpa_group_setkeys(wpa_auth, group);
2854 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2855 if (group->GKeyDoneStations == 0)
2856 wpa_group_setkeysdone(wpa_auth, group);
2857 else if (group->GTKReKey)
2858 wpa_group_setkeys(wpa_auth, group);
2859 }
2860}
2861
2862
2863static int wpa_sm_step(struct wpa_state_machine *sm)
2864{
2865 if (sm == NULL)
2866 return 0;
2867
2868 if (sm->in_step_loop) {
2869 /* This should not happen, but if it does, make sure we do not
2870 * end up freeing the state machine too early by exiting the
2871 * recursive call. */
2872 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2873 return 0;
2874 }
2875
2876 sm->in_step_loop = 1;
2877 do {
2878 if (sm->pending_deinit)
2879 break;
2880
2881 sm->changed = FALSE;
2882 sm->wpa_auth->group->changed = FALSE;
2883
2884 SM_STEP_RUN(WPA_PTK);
2885 if (sm->pending_deinit)
2886 break;
2887 SM_STEP_RUN(WPA_PTK_GROUP);
2888 if (sm->pending_deinit)
2889 break;
2890 wpa_group_sm_step(sm->wpa_auth, sm->group);
2891 } while (sm->changed || sm->wpa_auth->group->changed);
2892 sm->in_step_loop = 0;
2893
2894 if (sm->pending_deinit) {
2895 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2896 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2897 wpa_free_sta_sm(sm);
2898 return 1;
2899 }
2900 return 0;
2901}
2902
2903
2904static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2905{
2906 struct wpa_state_machine *sm = eloop_ctx;
2907 wpa_sm_step(sm);
2908}
2909
2910
2911void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2912{
2913 if (sm == NULL)
2914 return;
2915 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2916}
2917
2918
2919void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2920{
2921 int tmp, i;
2922 struct wpa_group *group;
2923
2924 if (wpa_auth == NULL)
2925 return;
2926
2927 group = wpa_auth->group;
2928
2929 for (i = 0; i < 2; i++) {
2930 tmp = group->GM;
2931 group->GM = group->GN;
2932 group->GN = tmp;
2933#ifdef CONFIG_IEEE80211W
2934 tmp = group->GM_igtk;
2935 group->GM_igtk = group->GN_igtk;
2936 group->GN_igtk = tmp;
2937#endif /* CONFIG_IEEE80211W */
2938 wpa_gtk_update(wpa_auth, group);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002939 wpa_group_config_group_keys(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940 }
2941}
2942
2943
2944static const char * wpa_bool_txt(int bool)
2945{
2946 return bool ? "TRUE" : "FALSE";
2947}
2948
2949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002950#define RSN_SUITE "%02x-%02x-%02x-%d"
2951#define RSN_SUITE_ARG(s) \
2952((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2953
2954int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2955{
2956 int len = 0, ret;
2957 char pmkid_txt[PMKID_LEN * 2 + 1];
2958#ifdef CONFIG_RSN_PREAUTH
2959 const int preauth = 1;
2960#else /* CONFIG_RSN_PREAUTH */
2961 const int preauth = 0;
2962#endif /* CONFIG_RSN_PREAUTH */
2963
2964 if (wpa_auth == NULL)
2965 return len;
2966
2967 ret = os_snprintf(buf + len, buflen - len,
2968 "dot11RSNAOptionImplemented=TRUE\n"
2969 "dot11RSNAPreauthenticationImplemented=%s\n"
2970 "dot11RSNAEnabled=%s\n"
2971 "dot11RSNAPreauthenticationEnabled=%s\n",
2972 wpa_bool_txt(preauth),
2973 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2974 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002975 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 return len;
2977 len += ret;
2978
2979 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2980 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2981
2982 ret = os_snprintf(
2983 buf + len, buflen - len,
2984 "dot11RSNAConfigVersion=%u\n"
2985 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2986 /* FIX: dot11RSNAConfigGroupCipher */
2987 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2988 /* FIX: dot11RSNAConfigGroupRekeyTime */
2989 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2990 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2991 "dot11RSNAConfigGroupUpdateCount=%u\n"
2992 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2993 "dot11RSNAConfigGroupCipherSize=%u\n"
2994 "dot11RSNAConfigPMKLifetime=%u\n"
2995 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2996 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2997 "dot11RSNAConfigSATimeout=%u\n"
2998 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2999 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
3000 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
3001 "dot11RSNAPMKIDUsed=%s\n"
3002 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
3003 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
3004 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
3005 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
3006 "dot11RSNA4WayHandshakeFailures=%u\n"
3007 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
3008 RSN_VERSION,
3009 !!wpa_auth->conf.wpa_strict_rekey,
3010 dot11RSNAConfigGroupUpdateCount,
3011 dot11RSNAConfigPairwiseUpdateCount,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003012 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003013 dot11RSNAConfigPMKLifetime,
3014 dot11RSNAConfigPMKReauthThreshold,
3015 dot11RSNAConfigSATimeout,
3016 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
3017 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
3018 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
3019 pmkid_txt,
3020 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
3021 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
3022 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
3023 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
3024 wpa_auth->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003025 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026 return len;
3027 len += ret;
3028
3029 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
3030 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
3031
3032 /* Private MIB */
3033 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
3034 wpa_auth->group->wpa_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003035 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003036 return len;
3037 len += ret;
3038
3039 return len;
3040}
3041
3042
3043int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
3044{
3045 int len = 0, ret;
3046 u32 pairwise = 0;
3047
3048 if (sm == NULL)
3049 return 0;
3050
3051 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
3052
3053 /* dot11RSNAStatsEntry */
3054
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003055 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
3056 WPA_PROTO_RSN : WPA_PROTO_WPA,
3057 sm->pairwise);
3058 if (pairwise == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059 return 0;
3060
3061 ret = os_snprintf(
3062 buf + len, buflen - len,
3063 /* TODO: dot11RSNAStatsIndex */
3064 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
3065 "dot11RSNAStatsVersion=1\n"
3066 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
3067 /* TODO: dot11RSNAStatsTKIPICVErrors */
3068 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
3069 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
3070 /* TODO: dot11RSNAStatsCCMPReplays */
3071 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
3072 /* TODO: dot11RSNAStatsTKIPReplays */,
3073 MAC2STR(sm->addr),
3074 RSN_SUITE_ARG(pairwise),
3075 sm->dot11RSNAStatsTKIPLocalMICFailures,
3076 sm->dot11RSNAStatsTKIPRemoteMICFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003077 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 return len;
3079 len += ret;
3080
3081 /* Private MIB */
3082 ret = os_snprintf(buf + len, buflen - len,
3083 "hostapdWPAPTKState=%d\n"
3084 "hostapdWPAPTKGroupState=%d\n",
3085 sm->wpa_ptk_state,
3086 sm->wpa_ptk_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003087 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003088 return len;
3089 len += ret;
3090
3091 return len;
3092}
3093
3094
3095void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
3096{
3097 if (wpa_auth)
3098 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
3099}
3100
3101
3102int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
3103{
3104 return sm && sm->pairwise_set;
3105}
3106
3107
3108int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
3109{
3110 return sm->pairwise;
3111}
3112
3113
3114int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
3115{
3116 if (sm == NULL)
3117 return -1;
3118 return sm->wpa_key_mgmt;
3119}
3120
3121
3122int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
3123{
3124 if (sm == NULL)
3125 return 0;
3126 return sm->wpa;
3127}
3128
3129
3130int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
3131 struct rsn_pmksa_cache_entry *entry)
3132{
3133 if (sm == NULL || sm->pmksa != entry)
3134 return -1;
3135 sm->pmksa = NULL;
3136 return 0;
3137}
3138
3139
3140struct rsn_pmksa_cache_entry *
3141wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
3142{
3143 return sm ? sm->pmksa : NULL;
3144}
3145
3146
3147void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
3148{
3149 if (sm)
3150 sm->dot11RSNAStatsTKIPLocalMICFailures++;
3151}
3152
3153
3154const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
3155{
3156 if (wpa_auth == NULL)
3157 return NULL;
3158 *len = wpa_auth->wpa_ie_len;
3159 return wpa_auth->wpa_ie;
3160}
3161
3162
3163int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
3164 int session_timeout, struct eapol_state_machine *eapol)
3165{
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07003166 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
3167 sm->wpa_auth->conf.disable_pmksa_caching)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003168 return -1;
3169
3170 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003171 sm->PTK.kck, sizeof(sm->PTK.kck),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003172 sm->wpa_auth->addr, sm->addr, session_timeout,
3173 eapol, sm->wpa_key_mgmt))
3174 return 0;
3175
3176 return -1;
3177}
3178
3179
3180int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
3181 const u8 *pmk, size_t len, const u8 *sta_addr,
3182 int session_timeout,
3183 struct eapol_state_machine *eapol)
3184{
3185 if (wpa_auth == NULL)
3186 return -1;
3187
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003188 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len,
3189 NULL, 0,
3190 wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003191 sta_addr, session_timeout, eapol,
3192 WPA_KEY_MGMT_IEEE8021X))
3193 return 0;
3194
3195 return -1;
3196}
3197
3198
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003199int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
3200 const u8 *pmk)
3201{
3202 if (wpa_auth->conf.disable_pmksa_caching)
3203 return -1;
3204
3205 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN,
3206 NULL, 0,
3207 wpa_auth->addr, addr, 0, NULL,
3208 WPA_KEY_MGMT_SAE))
3209 return 0;
3210
3211 return -1;
3212}
3213
3214
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003215void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
3216 const u8 *sta_addr)
3217{
3218 struct rsn_pmksa_cache_entry *pmksa;
3219
3220 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
3221 return;
3222 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3223 if (pmksa) {
3224 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
3225 MACSTR " based on request", MAC2STR(sta_addr));
3226 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
3227 }
3228}
3229
3230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003231static struct wpa_group *
3232wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3233{
3234 struct wpa_group *group;
3235
3236 if (wpa_auth == NULL || wpa_auth->group == NULL)
3237 return NULL;
3238
3239 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
3240 vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003241 group = wpa_group_init(wpa_auth, vlan_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003242 if (group == NULL)
3243 return NULL;
3244
3245 group->next = wpa_auth->group->next;
3246 wpa_auth->group->next = group;
3247
3248 return group;
3249}
3250
3251
3252int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
3253{
3254 struct wpa_group *group;
3255
3256 if (sm == NULL || sm->wpa_auth == NULL)
3257 return 0;
3258
3259 group = sm->wpa_auth->group;
3260 while (group) {
3261 if (group->vlan_id == vlan_id)
3262 break;
3263 group = group->next;
3264 }
3265
3266 if (group == NULL) {
3267 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
3268 if (group == NULL)
3269 return -1;
3270 }
3271
3272 if (sm->group == group)
3273 return 0;
3274
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003275 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
3276 return -1;
3277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
3279 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
3280
3281 sm->group = group;
3282 return 0;
3283}
3284
3285
3286void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
3287 struct wpa_state_machine *sm, int ack)
3288{
3289 if (wpa_auth == NULL || sm == NULL)
3290 return;
3291 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
3292 " ack=%d", MAC2STR(sm->addr), ack);
3293 if (sm->pending_1_of_4_timeout && ack) {
3294 /*
3295 * Some deployed supplicant implementations update their SNonce
3296 * for each EAPOL-Key 2/4 message even within the same 4-way
3297 * handshake and then fail to use the first SNonce when
3298 * deriving the PTK. This results in unsuccessful 4-way
3299 * handshake whenever the relatively short initial timeout is
3300 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
3301 * around this by increasing the timeout now that we know that
3302 * the station has received the frame.
3303 */
3304 int timeout_ms = eapol_key_timeout_subseq;
3305 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
3306 "timeout by %u ms because of acknowledged frame",
3307 timeout_ms);
3308 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
3309 eloop_register_timeout(timeout_ms / 1000,
3310 (timeout_ms % 1000) * 1000,
3311 wpa_send_eapol_timeout, wpa_auth, sm);
3312 }
3313}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003314
3315
3316int wpa_auth_uses_sae(struct wpa_state_machine *sm)
3317{
3318 if (sm == NULL)
3319 return 0;
3320 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
3321}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003322
3323
3324int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
3325{
3326 if (sm == NULL)
3327 return 0;
3328 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
3329}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003330
3331
3332#ifdef CONFIG_P2P
3333int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
3334{
3335 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
3336 return -1;
3337 os_memcpy(addr, sm->ip_addr, 4);
3338 return 0;
3339}
3340#endif /* CONFIG_P2P */