blob: b83b46001c3fc4ce907145ce6037bd16c2328687 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002 * IEEE 802.11 RSN / WPA Authenticator
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "utils/state_machine.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080014#include "utils/bitfield.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "common/ieee802_11_defs.h"
16#include "crypto/aes_wrap.h"
17#include "crypto/crypto.h"
18#include "crypto/sha1.h"
19#include "crypto/sha256.h"
20#include "crypto/random.h"
21#include "eapol_auth/eapol_auth_sm.h"
22#include "ap_config.h"
23#include "ieee802_11.h"
24#include "wpa_auth.h"
25#include "pmksa_cache_auth.h"
26#include "wpa_auth_i.h"
27#include "wpa_auth_ie.h"
28
29#define STATE_MACHINE_DATA struct wpa_state_machine
30#define STATE_MACHINE_DEBUG_PREFIX "WPA"
31#define STATE_MACHINE_ADDR sm->addr
32
33
34static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
35static int wpa_sm_step(struct wpa_state_machine *sm);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080036static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
37 size_t data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
39static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
40 struct wpa_group *group);
41static void wpa_request_new_ptk(struct wpa_state_machine *sm);
42static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
43 struct wpa_group *group);
44static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
45 struct wpa_group *group);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080046static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
47 const u8 *pmk, struct wpa_ptk *ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048
49static const u32 dot11RSNAConfigGroupUpdateCount = 4;
50static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
51static const u32 eapol_key_timeout_first = 100; /* ms */
52static const u32 eapol_key_timeout_subseq = 1000; /* ms */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080053static const u32 eapol_key_timeout_first_group = 500; /* ms */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054
55/* TODO: make these configurable */
56static const int dot11RSNAConfigPMKLifetime = 43200;
57static const int dot11RSNAConfigPMKReauthThreshold = 70;
58static const int dot11RSNAConfigSATimeout = 60;
59
60
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080061static inline int wpa_auth_mic_failure_report(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062 struct wpa_authenticator *wpa_auth, const u8 *addr)
63{
64 if (wpa_auth->cb.mic_failure_report)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080065 return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
66 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067}
68
69
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070070static inline void wpa_auth_psk_failure_report(
71 struct wpa_authenticator *wpa_auth, const u8 *addr)
72{
73 if (wpa_auth->cb.psk_failure_report)
74 wpa_auth->cb.psk_failure_report(wpa_auth->cb.ctx, addr);
75}
76
77
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
79 const u8 *addr, wpa_eapol_variable var,
80 int value)
81{
82 if (wpa_auth->cb.set_eapol)
83 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
84}
85
86
87static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
88 const u8 *addr, wpa_eapol_variable var)
89{
90 if (wpa_auth->cb.get_eapol == NULL)
91 return -1;
92 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
93}
94
95
96static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070097 const u8 *addr,
98 const u8 *p2p_dev_addr,
99 const u8 *prev_psk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100{
101 if (wpa_auth->cb.get_psk == NULL)
102 return NULL;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700103 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, p2p_dev_addr,
104 prev_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105}
106
107
108static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
109 const u8 *addr, u8 *msk, size_t *len)
110{
111 if (wpa_auth->cb.get_msk == NULL)
112 return -1;
113 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
114}
115
116
117static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
118 int vlan_id,
119 enum wpa_alg alg, const u8 *addr, int idx,
120 u8 *key, size_t key_len)
121{
122 if (wpa_auth->cb.set_key == NULL)
123 return -1;
124 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
125 key, key_len);
126}
127
128
129static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
130 const u8 *addr, int idx, u8 *seq)
131{
132 if (wpa_auth->cb.get_seqnum == NULL)
133 return -1;
134 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
135}
136
137
138static inline int
139wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
140 const u8 *data, size_t data_len, int encrypt)
141{
142 if (wpa_auth->cb.send_eapol == NULL)
143 return -1;
144 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
145 encrypt);
146}
147
148
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800149#ifdef CONFIG_MESH
150static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
151 const u8 *addr)
152{
153 if (wpa_auth->cb.start_ampe == NULL)
154 return -1;
155 return wpa_auth->cb.start_ampe(wpa_auth->cb.ctx, addr);
156}
157#endif /* CONFIG_MESH */
158
159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
161 int (*cb)(struct wpa_state_machine *sm, void *ctx),
162 void *cb_ctx)
163{
164 if (wpa_auth->cb.for_each_sta == NULL)
165 return 0;
166 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
167}
168
169
170int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
171 int (*cb)(struct wpa_authenticator *a, void *ctx),
172 void *cb_ctx)
173{
174 if (wpa_auth->cb.for_each_auth == NULL)
175 return 0;
176 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
177}
178
179
180void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
181 logger_level level, const char *txt)
182{
183 if (wpa_auth->cb.logger == NULL)
184 return;
185 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
186}
187
188
189void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
190 logger_level level, const char *fmt, ...)
191{
192 char *format;
193 int maxlen;
194 va_list ap;
195
196 if (wpa_auth->cb.logger == NULL)
197 return;
198
199 maxlen = os_strlen(fmt) + 100;
200 format = os_malloc(maxlen);
201 if (!format)
202 return;
203
204 va_start(ap, fmt);
205 vsnprintf(format, maxlen, fmt, ap);
206 va_end(ap);
207
208 wpa_auth_logger(wpa_auth, addr, level, format);
209
210 os_free(format);
211}
212
213
214static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
215 const u8 *addr)
216{
217 if (wpa_auth->cb.disconnect == NULL)
218 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800219 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
221 WLAN_REASON_PREV_AUTH_NOT_VALID);
222}
223
224
225static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
226{
227 int ret = 0;
228#ifdef CONFIG_IEEE80211R
229 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
230 ret = 1;
231#endif /* CONFIG_IEEE80211R */
232#ifdef CONFIG_IEEE80211W
233 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
234 ret = 1;
235#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800236 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
237 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238 return ret;
239}
240
241
242static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
243{
244 struct wpa_authenticator *wpa_auth = eloop_ctx;
245
246 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
247 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
248 "initialization.");
249 } else {
250 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
251 wpa_hexdump_key(MSG_DEBUG, "GMK",
252 wpa_auth->group->GMK, WPA_GMK_LEN);
253 }
254
255 if (wpa_auth->conf.wpa_gmk_rekey) {
256 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
257 wpa_rekey_gmk, wpa_auth, NULL);
258 }
259}
260
261
262static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
263{
264 struct wpa_authenticator *wpa_auth = eloop_ctx;
265 struct wpa_group *group;
266
267 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
268 for (group = wpa_auth->group; group; group = group->next) {
269 group->GTKReKey = TRUE;
270 do {
271 group->changed = FALSE;
272 wpa_group_sm_step(wpa_auth, group);
273 } while (group->changed);
274 }
275
276 if (wpa_auth->conf.wpa_group_rekey) {
277 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
278 0, wpa_rekey_gtk, wpa_auth, NULL);
279 }
280}
281
282
283static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
284{
285 struct wpa_authenticator *wpa_auth = eloop_ctx;
286 struct wpa_state_machine *sm = timeout_ctx;
287
288 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
289 wpa_request_new_ptk(sm);
290 wpa_sm_step(sm);
291}
292
293
294static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
295{
296 if (sm->pmksa == ctx)
297 sm->pmksa = NULL;
298 return 0;
299}
300
301
302static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
303 void *ctx)
304{
305 struct wpa_authenticator *wpa_auth = ctx;
306 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
307}
308
309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
311 struct wpa_group *group)
312{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800313 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 u8 rkey[32];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800315 unsigned long ptr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316
317 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
318 return -1;
319 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
320
321 /*
322 * Counter = PRF-256(Random number, "Init Counter",
323 * Local MAC Address || Time)
324 */
325 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
326 wpa_get_ntp_timestamp(buf + ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800327 ptr = (unsigned long) group;
328 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
330 return -1;
331
332 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
333 group->Counter, WPA_NONCE_LEN) < 0)
334 return -1;
335 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
336 group->Counter, WPA_NONCE_LEN);
337
338 return 0;
339}
340
341
342static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800343 int vlan_id, int delay_init)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344{
345 struct wpa_group *group;
346
347 group = os_zalloc(sizeof(struct wpa_group));
348 if (group == NULL)
349 return NULL;
350
351 group->GTKAuthenticator = TRUE;
352 group->vlan_id = vlan_id;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700353 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354
355 if (random_pool_ready() != 1) {
356 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
357 "for secure operations - update keys later when "
358 "the first station connects");
359 }
360
361 /*
362 * Set initial GMK/Counter value here. The actual values that will be
363 * used in negotiations will be set once the first station tries to
364 * connect. This allows more time for collecting additional randomness
365 * on embedded devices.
366 */
367 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
368 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
369 "initialization.");
370 os_free(group);
371 return NULL;
372 }
373
374 group->GInit = TRUE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800375 if (delay_init) {
376 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
377 "until Beacon frames have been configured");
378 /* Initialization is completed in wpa_init_keys(). */
379 } else {
380 wpa_group_sm_step(wpa_auth, group);
381 group->GInit = FALSE;
382 wpa_group_sm_step(wpa_auth, group);
383 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384
385 return group;
386}
387
388
389/**
390 * wpa_init - Initialize WPA authenticator
391 * @addr: Authenticator address
392 * @conf: Configuration for WPA authenticator
393 * @cb: Callback functions for WPA authenticator
394 * Returns: Pointer to WPA authenticator data or %NULL on failure
395 */
396struct wpa_authenticator * wpa_init(const u8 *addr,
397 struct wpa_auth_config *conf,
398 struct wpa_auth_callbacks *cb)
399{
400 struct wpa_authenticator *wpa_auth;
401
402 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
403 if (wpa_auth == NULL)
404 return NULL;
405 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
406 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
407 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
408
409 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
410 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
411 os_free(wpa_auth);
412 return NULL;
413 }
414
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800415 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 if (wpa_auth->group == NULL) {
417 os_free(wpa_auth->wpa_ie);
418 os_free(wpa_auth);
419 return NULL;
420 }
421
422 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
423 wpa_auth);
424 if (wpa_auth->pmksa == NULL) {
425 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800426 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 os_free(wpa_auth->wpa_ie);
428 os_free(wpa_auth);
429 return NULL;
430 }
431
432#ifdef CONFIG_IEEE80211R
433 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
434 if (wpa_auth->ft_pmk_cache == NULL) {
435 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800436 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 os_free(wpa_auth->wpa_ie);
438 pmksa_cache_auth_deinit(wpa_auth->pmksa);
439 os_free(wpa_auth);
440 return NULL;
441 }
442#endif /* CONFIG_IEEE80211R */
443
444 if (wpa_auth->conf.wpa_gmk_rekey) {
445 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
446 wpa_rekey_gmk, wpa_auth, NULL);
447 }
448
449 if (wpa_auth->conf.wpa_group_rekey) {
450 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
451 wpa_rekey_gtk, wpa_auth, NULL);
452 }
453
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800454#ifdef CONFIG_P2P
455 if (WPA_GET_BE32(conf->ip_addr_start)) {
456 int count = WPA_GET_BE32(conf->ip_addr_end) -
457 WPA_GET_BE32(conf->ip_addr_start) + 1;
458 if (count > 1000)
459 count = 1000;
460 if (count > 0)
461 wpa_auth->ip_pool = bitfield_alloc(count);
462 }
463#endif /* CONFIG_P2P */
464
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700465 return wpa_auth;
466}
467
468
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800469int wpa_init_keys(struct wpa_authenticator *wpa_auth)
470{
471 struct wpa_group *group = wpa_auth->group;
472
473 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
474 "keys");
475 wpa_group_sm_step(wpa_auth, group);
476 group->GInit = FALSE;
477 wpa_group_sm_step(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800478 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
479 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800480 return 0;
481}
482
483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484/**
485 * wpa_deinit - Deinitialize WPA authenticator
486 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
487 */
488void wpa_deinit(struct wpa_authenticator *wpa_auth)
489{
490 struct wpa_group *group, *prev;
491
492 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
493 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
494
495#ifdef CONFIG_PEERKEY
496 while (wpa_auth->stsl_negotiations)
497 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
498#endif /* CONFIG_PEERKEY */
499
500 pmksa_cache_auth_deinit(wpa_auth->pmksa);
501
502#ifdef CONFIG_IEEE80211R
503 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
504 wpa_auth->ft_pmk_cache = NULL;
505#endif /* CONFIG_IEEE80211R */
506
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800507#ifdef CONFIG_P2P
508 bitfield_free(wpa_auth->ip_pool);
509#endif /* CONFIG_P2P */
510
511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700512 os_free(wpa_auth->wpa_ie);
513
514 group = wpa_auth->group;
515 while (group) {
516 prev = group;
517 group = group->next;
518 os_free(prev);
519 }
520
521 os_free(wpa_auth);
522}
523
524
525/**
526 * wpa_reconfig - Update WPA authenticator configuration
527 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
528 * @conf: Configuration for WPA authenticator
529 */
530int wpa_reconfig(struct wpa_authenticator *wpa_auth,
531 struct wpa_auth_config *conf)
532{
533 struct wpa_group *group;
534 if (wpa_auth == NULL)
535 return 0;
536
537 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
538 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
539 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
540 return -1;
541 }
542
543 /*
544 * Reinitialize GTK to make sure it is suitable for the new
545 * configuration.
546 */
547 group = wpa_auth->group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700548 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 group->GInit = TRUE;
550 wpa_group_sm_step(wpa_auth, group);
551 group->GInit = FALSE;
552 wpa_group_sm_step(wpa_auth, group);
553
554 return 0;
555}
556
557
558struct wpa_state_machine *
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700559wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
560 const u8 *p2p_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561{
562 struct wpa_state_machine *sm;
563
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800564 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
565 return NULL;
566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567 sm = os_zalloc(sizeof(struct wpa_state_machine));
568 if (sm == NULL)
569 return NULL;
570 os_memcpy(sm->addr, addr, ETH_ALEN);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700571 if (p2p_dev_addr)
572 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573
574 sm->wpa_auth = wpa_auth;
575 sm->group = wpa_auth->group;
576
577 return sm;
578}
579
580
581int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
582 struct wpa_state_machine *sm)
583{
584 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
585 return -1;
586
587#ifdef CONFIG_IEEE80211R
588 if (sm->ft_completed) {
589 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
590 "FT authentication already completed - do not "
591 "start 4-way handshake");
Dmitry Shmidt71757432014-06-02 13:50:35 -0700592 /* Go to PTKINITDONE state to allow GTK rekeying */
593 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594 return 0;
595 }
596#endif /* CONFIG_IEEE80211R */
597
598 if (sm->started) {
599 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
600 sm->ReAuthenticationRequest = TRUE;
601 return wpa_sm_step(sm);
602 }
603
604 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
605 "start authentication");
606 sm->started = 1;
607
608 sm->Init = TRUE;
609 if (wpa_sm_step(sm) == 1)
610 return 1; /* should not really happen */
611 sm->Init = FALSE;
612 sm->AuthenticationRequest = TRUE;
613 return wpa_sm_step(sm);
614}
615
616
617void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
618{
619 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
620 * reassociates back to the same AP while the previous entry for the
621 * STA has not yet been removed. */
622 if (sm == NULL)
623 return;
624
625 sm->wpa_key_mgmt = 0;
626}
627
628
629static void wpa_free_sta_sm(struct wpa_state_machine *sm)
630{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800631#ifdef CONFIG_P2P
632 if (WPA_GET_BE32(sm->ip_addr)) {
633 u32 start;
634 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
635 "address %u.%u.%u.%u from " MACSTR,
636 sm->ip_addr[0], sm->ip_addr[1],
637 sm->ip_addr[2], sm->ip_addr[3],
638 MAC2STR(sm->addr));
639 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
640 bitfield_clear(sm->wpa_auth->ip_pool,
641 WPA_GET_BE32(sm->ip_addr) - start);
642 }
643#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700644 if (sm->GUpdateStationKeys) {
645 sm->group->GKeyDoneStations--;
646 sm->GUpdateStationKeys = FALSE;
647 }
648#ifdef CONFIG_IEEE80211R
649 os_free(sm->assoc_resp_ftie);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700650 wpabuf_free(sm->ft_pending_req_ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651#endif /* CONFIG_IEEE80211R */
652 os_free(sm->last_rx_eapol_key);
653 os_free(sm->wpa_ie);
654 os_free(sm);
655}
656
657
658void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
659{
660 if (sm == NULL)
661 return;
662
663 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
664 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
665 "strict rekeying - force GTK rekey since STA "
666 "is leaving");
667 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
668 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
669 NULL);
670 }
671
672 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
673 sm->pending_1_of_4_timeout = 0;
674 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
675 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
676 if (sm->in_step_loop) {
677 /* Must not free state machine while wpa_sm_step() is running.
678 * Freeing will be completed in the end of wpa_sm_step(). */
679 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
680 "machine deinit for " MACSTR, MAC2STR(sm->addr));
681 sm->pending_deinit = 1;
682 } else
683 wpa_free_sta_sm(sm);
684}
685
686
687static void wpa_request_new_ptk(struct wpa_state_machine *sm)
688{
689 if (sm == NULL)
690 return;
691
692 sm->PTKRequest = TRUE;
693 sm->PTK_valid = 0;
694}
695
696
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800697static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 const u8 *replay_counter)
699{
700 int i;
701 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800702 if (!ctr[i].valid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800704 if (os_memcmp(replay_counter, ctr[i].counter,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700705 WPA_REPLAY_COUNTER_LEN) == 0)
706 return 1;
707 }
708 return 0;
709}
710
711
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800712static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
713 const u8 *replay_counter)
714{
715 int i;
716 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
717 if (ctr[i].valid &&
718 (replay_counter == NULL ||
719 os_memcmp(replay_counter, ctr[i].counter,
720 WPA_REPLAY_COUNTER_LEN) == 0))
721 ctr[i].valid = FALSE;
722 }
723}
724
725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726#ifdef CONFIG_IEEE80211R
727static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
728 struct wpa_state_machine *sm,
729 struct wpa_eapol_ie_parse *kde)
730{
731 struct wpa_ie_data ie;
732 struct rsn_mdie *mdie;
733
734 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
735 ie.num_pmkid != 1 || ie.pmkid == NULL) {
736 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
737 "FT 4-way handshake message 2/4");
738 return -1;
739 }
740
741 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
742 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
743 sm->sup_pmk_r1_name, PMKID_LEN);
744
745 if (!kde->mdie || !kde->ftie) {
746 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
747 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
748 return -1;
749 }
750
751 mdie = (struct rsn_mdie *) (kde->mdie + 2);
752 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
753 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
754 MOBILITY_DOMAIN_ID_LEN) != 0) {
755 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
756 return -1;
757 }
758
759 if (sm->assoc_resp_ftie &&
760 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
761 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
762 2 + sm->assoc_resp_ftie[1]) != 0)) {
763 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
764 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
765 kde->ftie, kde->ftie_len);
766 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
767 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
768 return -1;
769 }
770
771 return 0;
772}
773#endif /* CONFIG_IEEE80211R */
774
775
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800776static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
777 struct wpa_state_machine *sm, int group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800778{
779 /* Supplicant reported a Michael MIC error */
780 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
781 "received EAPOL-Key Error Request "
782 "(STA detected Michael MIC failure (group=%d))",
783 group);
784
785 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
786 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
787 "ignore Michael MIC failure report since "
788 "group cipher is not TKIP");
789 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
790 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
791 "ignore Michael MIC failure report since "
792 "pairwise cipher is not TKIP");
793 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800794 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
795 return 1; /* STA entry was removed */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800796 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
797 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
798 }
799
800 /*
801 * Error report is not a request for a new key handshake, but since
802 * Authenticator may do it, let's change the keys now anyway.
803 */
804 wpa_request_new_ptk(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800805 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800806}
807
808
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800809static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
810 size_t data_len)
811{
812 struct wpa_ptk PTK;
813 int ok = 0;
814 const u8 *pmk = NULL;
815
816 for (;;) {
817 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
818 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
819 sm->p2p_dev_addr, pmk);
820 if (pmk == NULL)
821 break;
822 } else
823 pmk = sm->PMK;
824
825 wpa_derive_ptk(sm, sm->alt_SNonce, pmk, &PTK);
826
827 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len)
828 == 0) {
829 ok = 1;
830 break;
831 }
832
833 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
834 break;
835 }
836
837 if (!ok) {
838 wpa_printf(MSG_DEBUG,
839 "WPA: Earlier SNonce did not result in matching MIC");
840 return -1;
841 }
842
843 wpa_printf(MSG_DEBUG,
844 "WPA: Earlier SNonce resulted in matching MIC");
845 sm->alt_snonce_valid = 0;
846 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
847 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
848 sm->PTK_valid = TRUE;
849
850 return 0;
851}
852
853
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854void wpa_receive(struct wpa_authenticator *wpa_auth,
855 struct wpa_state_machine *sm,
856 u8 *data, size_t data_len)
857{
858 struct ieee802_1x_hdr *hdr;
859 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800860 struct wpa_eapol_key_192 *key192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 u16 key_info, key_data_length;
862 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
863 SMK_M1, SMK_M3, SMK_ERROR } msg;
864 char *msgtxt;
865 struct wpa_eapol_ie_parse kde;
866 int ft;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800867 const u8 *eapol_key_ie, *key_data;
868 size_t eapol_key_ie_len, keyhdrlen, mic_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869
870 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
871 return;
872
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800873 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
874 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
875
876 if (data_len < sizeof(*hdr) + keyhdrlen)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 return;
878
879 hdr = (struct ieee802_1x_hdr *) data;
880 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800881 key192 = (struct wpa_eapol_key_192 *) (hdr + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800883 if (mic_len == 24) {
884 key_data = (const u8 *) (key192 + 1);
885 key_data_length = WPA_GET_BE16(key192->key_data_length);
886 } else {
887 key_data = (const u8 *) (key + 1);
888 key_data_length = WPA_GET_BE16(key->key_data_length);
889 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800890 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
891 " key_info=0x%x type=%u key_data_length=%u",
892 MAC2STR(sm->addr), key_info, key->type, key_data_length);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800893 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
895 "key_data overflow (%d > %lu)",
896 key_data_length,
897 (unsigned long) (data_len - sizeof(*hdr) -
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800898 keyhdrlen));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 return;
900 }
901
902 if (sm->wpa == WPA_VERSION_WPA2) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800903 if (key->type == EAPOL_KEY_TYPE_WPA) {
904 /*
905 * Some deployed station implementations seem to send
906 * msg 4/4 with incorrect type value in WPA2 mode.
907 */
908 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
909 "with unexpected WPA type in RSN mode");
910 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
912 "unexpected type %d in RSN mode",
913 key->type);
914 return;
915 }
916 } else {
917 if (key->type != EAPOL_KEY_TYPE_WPA) {
918 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
919 "unexpected type %d in WPA mode",
920 key->type);
921 return;
922 }
923 }
924
925 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
926 WPA_NONCE_LEN);
927 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
928 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
929
930 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
931 * are set */
932
933 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
934 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
935 if (key_info & WPA_KEY_INFO_ERROR) {
936 msg = SMK_ERROR;
937 msgtxt = "SMK Error";
938 } else {
939 msg = SMK_M1;
940 msgtxt = "SMK M1";
941 }
942 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
943 msg = SMK_M3;
944 msgtxt = "SMK M3";
945 } else if (key_info & WPA_KEY_INFO_REQUEST) {
946 msg = REQUEST;
947 msgtxt = "Request";
948 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
949 msg = GROUP_2;
950 msgtxt = "2/2 Group";
951 } else if (key_data_length == 0) {
952 msg = PAIRWISE_4;
953 msgtxt = "4/4 Pairwise";
954 } else {
955 msg = PAIRWISE_2;
956 msgtxt = "2/4 Pairwise";
957 }
958
959 /* TODO: key_info type validation for PeerKey */
960 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
961 msg == GROUP_2) {
962 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700963 if (sm->pairwise == WPA_CIPHER_CCMP ||
964 sm->pairwise == WPA_CIPHER_GCMP) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 if (wpa_use_aes_cmac(sm) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800966 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800967 !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
969 wpa_auth_logger(wpa_auth, sm->addr,
970 LOGGER_WARNING,
971 "advertised support for "
972 "AES-128-CMAC, but did not "
973 "use it");
974 return;
975 }
976
977 if (!wpa_use_aes_cmac(sm) &&
978 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
979 wpa_auth_logger(wpa_auth, sm->addr,
980 LOGGER_WARNING,
981 "did not use HMAC-SHA1-AES "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700982 "with CCMP/GCMP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 return;
984 }
985 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800986
987 if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
988 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
989 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
990 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
991 return;
992 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993 }
994
995 if (key_info & WPA_KEY_INFO_REQUEST) {
996 if (sm->req_replay_counter_used &&
997 os_memcmp(key->replay_counter, sm->req_replay_counter,
998 WPA_REPLAY_COUNTER_LEN) <= 0) {
999 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1000 "received EAPOL-Key request with "
1001 "replayed counter");
1002 return;
1003 }
1004 }
1005
1006 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001007 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 int i;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001009
1010 if (msg == PAIRWISE_2 &&
1011 wpa_replay_counter_valid(sm->prev_key_replay,
1012 key->replay_counter) &&
1013 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1014 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1015 {
1016 /*
1017 * Some supplicant implementations (e.g., Windows XP
1018 * WZC) update SNonce for each EAPOL-Key 2/4. This
1019 * breaks the workaround on accepting any of the
1020 * pending requests, so allow the SNonce to be updated
1021 * even if we have already sent out EAPOL-Key 3/4.
1022 */
1023 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1024 "Process SNonce update from STA "
1025 "based on retransmitted EAPOL-Key "
1026 "1/4");
1027 sm->update_snonce = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001028 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1029 sm->alt_snonce_valid = TRUE;
1030 os_memcpy(sm->alt_replay_counter,
1031 sm->key_replay[0].counter,
1032 WPA_REPLAY_COUNTER_LEN);
1033 goto continue_processing;
1034 }
1035
1036 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1037 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1038 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1039 WPA_REPLAY_COUNTER_LEN) == 0) {
1040 /*
1041 * Supplicant may still be using the old SNonce since
1042 * there was two EAPOL-Key 2/4 messages and they had
1043 * different SNonce values.
1044 */
1045 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1046 "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 -08001047 goto continue_processing;
1048 }
1049
1050 if (msg == PAIRWISE_2 &&
1051 wpa_replay_counter_valid(sm->prev_key_replay,
1052 key->replay_counter) &&
1053 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1054 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1055 "ignore retransmitted EAPOL-Key %s - "
1056 "SNonce did not change", msgtxt);
1057 } else {
1058 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1059 "received EAPOL-Key %s with "
1060 "unexpected replay counter", msgtxt);
1061 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1063 if (!sm->key_replay[i].valid)
1064 break;
1065 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1066 sm->key_replay[i].counter,
1067 WPA_REPLAY_COUNTER_LEN);
1068 }
1069 wpa_hexdump(MSG_DEBUG, "received replay counter",
1070 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1071 return;
1072 }
1073
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001074continue_processing:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001075 switch (msg) {
1076 case PAIRWISE_2:
1077 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001078 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1079 (!sm->update_snonce ||
1080 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1082 "received EAPOL-Key msg 2/4 in "
1083 "invalid state (%d) - dropped",
1084 sm->wpa_ptk_state);
1085 return;
1086 }
1087 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1088 if (sm->group->reject_4way_hs_for_entropy) {
1089 /*
1090 * The system did not have enough entropy to generate
1091 * strong random numbers. Reject the first 4-way
1092 * handshake(s) and collect some entropy based on the
1093 * information from it. Once enough entropy is
1094 * available, the next atempt will trigger GMK/Key
1095 * Counter update and the station will be allowed to
1096 * continue.
1097 */
1098 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1099 "collect more entropy for random number "
1100 "generation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 random_mark_pool_ready();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 wpa_sta_disconnect(wpa_auth, sm->addr);
1103 return;
1104 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001105 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1107 "received EAPOL-Key msg 2/4 with "
1108 "invalid Key Data contents");
1109 return;
1110 }
1111 if (kde.rsn_ie) {
1112 eapol_key_ie = kde.rsn_ie;
1113 eapol_key_ie_len = kde.rsn_ie_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001114 } else if (kde.osen) {
1115 eapol_key_ie = kde.osen;
1116 eapol_key_ie_len = kde.osen_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117 } else {
1118 eapol_key_ie = kde.wpa_ie;
1119 eapol_key_ie_len = kde.wpa_ie_len;
1120 }
1121 ft = sm->wpa == WPA_VERSION_WPA2 &&
1122 wpa_key_mgmt_ft(sm->wpa_key_mgmt);
1123 if (sm->wpa_ie == NULL ||
1124 wpa_compare_rsn_ie(ft,
1125 sm->wpa_ie, sm->wpa_ie_len,
1126 eapol_key_ie, eapol_key_ie_len)) {
1127 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1128 "WPA IE from (Re)AssocReq did not "
1129 "match with msg 2/4");
1130 if (sm->wpa_ie) {
1131 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
1132 sm->wpa_ie, sm->wpa_ie_len);
1133 }
1134 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
1135 eapol_key_ie, eapol_key_ie_len);
1136 /* MLME-DEAUTHENTICATE.request */
1137 wpa_sta_disconnect(wpa_auth, sm->addr);
1138 return;
1139 }
1140#ifdef CONFIG_IEEE80211R
1141 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
1142 wpa_sta_disconnect(wpa_auth, sm->addr);
1143 return;
1144 }
1145#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001146#ifdef CONFIG_P2P
1147 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
1148 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
1149 int idx;
1150 wpa_printf(MSG_DEBUG, "P2P: IP address requested in "
1151 "EAPOL-Key exchange");
1152 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
1153 if (idx >= 0) {
1154 u32 start = WPA_GET_BE32(wpa_auth->conf.
1155 ip_addr_start);
1156 bitfield_set(wpa_auth->ip_pool, idx);
1157 WPA_PUT_BE32(sm->ip_addr, start + idx);
1158 wpa_printf(MSG_DEBUG, "P2P: Assigned IP "
1159 "address %u.%u.%u.%u to " MACSTR,
1160 sm->ip_addr[0], sm->ip_addr[1],
1161 sm->ip_addr[2], sm->ip_addr[3],
1162 MAC2STR(sm->addr));
1163 }
1164 }
1165#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 break;
1167 case PAIRWISE_4:
1168 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1169 !sm->PTK_valid) {
1170 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1171 "received EAPOL-Key msg 4/4 in "
1172 "invalid state (%d) - dropped",
1173 sm->wpa_ptk_state);
1174 return;
1175 }
1176 break;
1177 case GROUP_2:
1178 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1179 || !sm->PTK_valid) {
1180 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1181 "received EAPOL-Key msg 2/2 in "
1182 "invalid state (%d) - dropped",
1183 sm->wpa_ptk_group_state);
1184 return;
1185 }
1186 break;
1187#ifdef CONFIG_PEERKEY
1188 case SMK_M1:
1189 case SMK_M3:
1190 case SMK_ERROR:
1191 if (!wpa_auth->conf.peerkey) {
1192 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1193 "PeerKey use disabled - ignoring message");
1194 return;
1195 }
1196 if (!sm->PTK_valid) {
1197 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1198 "received EAPOL-Key msg SMK in "
1199 "invalid state - dropped");
1200 return;
1201 }
1202 break;
1203#else /* CONFIG_PEERKEY */
1204 case SMK_M1:
1205 case SMK_M3:
1206 case SMK_ERROR:
1207 return; /* STSL disabled - ignore SMK messages */
1208#endif /* CONFIG_PEERKEY */
1209 case REQUEST:
1210 break;
1211 }
1212
1213 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1214 "received EAPOL-Key frame (%s)", msgtxt);
1215
1216 if (key_info & WPA_KEY_INFO_ACK) {
1217 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1218 "received invalid EAPOL-Key: Key Ack set");
1219 return;
1220 }
1221
1222 if (!(key_info & WPA_KEY_INFO_MIC)) {
1223 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1224 "received invalid EAPOL-Key: Key MIC not set");
1225 return;
1226 }
1227
1228 sm->MICVerified = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001229 if (sm->PTK_valid && !sm->update_snonce) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001230 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
1231 data_len) &&
1232 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1233 wpa_try_alt_snonce(sm, data, data_len))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1235 "received EAPOL-Key with invalid MIC");
1236 return;
1237 }
1238 sm->MICVerified = TRUE;
1239 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1240 sm->pending_1_of_4_timeout = 0;
1241 }
1242
1243 if (key_info & WPA_KEY_INFO_REQUEST) {
1244 if (sm->MICVerified) {
1245 sm->req_replay_counter_used = 1;
1246 os_memcpy(sm->req_replay_counter, key->replay_counter,
1247 WPA_REPLAY_COUNTER_LEN);
1248 } else {
1249 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1250 "received EAPOL-Key request with "
1251 "invalid MIC");
1252 return;
1253 }
1254
1255 /*
1256 * TODO: should decrypt key data field if encryption was used;
1257 * even though MAC address KDE is not normally encrypted,
1258 * supplicant is allowed to encrypt it.
1259 */
1260 if (msg == SMK_ERROR) {
1261#ifdef CONFIG_PEERKEY
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001262 wpa_smk_error(wpa_auth, sm, key_data, key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263#endif /* CONFIG_PEERKEY */
1264 return;
1265 } else if (key_info & WPA_KEY_INFO_ERROR) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001266 if (wpa_receive_error_report(
1267 wpa_auth, sm,
1268 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1269 return; /* STA entry was removed */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1271 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1272 "received EAPOL-Key Request for new "
1273 "4-Way Handshake");
1274 wpa_request_new_ptk(sm);
1275#ifdef CONFIG_PEERKEY
1276 } else if (msg == SMK_M1) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001277 wpa_smk_m1(wpa_auth, sm, key, key_data,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001278 key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001279#endif /* CONFIG_PEERKEY */
1280 } else if (key_data_length > 0 &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001281 wpa_parse_kde_ies(key_data, key_data_length,
1282 &kde) == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283 kde.mac_addr) {
1284 } else {
1285 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1286 "received EAPOL-Key Request for GTK "
1287 "rekeying");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1289 wpa_rekey_gtk(wpa_auth, NULL);
1290 }
1291 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001292 /* Do not allow the same key replay counter to be reused. */
1293 wpa_replay_counter_mark_invalid(sm->key_replay,
1294 key->replay_counter);
1295
1296 if (msg == PAIRWISE_2) {
1297 /*
1298 * Maintain a copy of the pending EAPOL-Key frames in
1299 * case the EAPOL-Key frame was retransmitted. This is
1300 * needed to allow EAPOL-Key msg 2/4 reply to another
1301 * pending msg 1/4 to update the SNonce to work around
1302 * unexpected supplicant behavior.
1303 */
1304 os_memcpy(sm->prev_key_replay, sm->key_replay,
1305 sizeof(sm->key_replay));
1306 } else {
1307 os_memset(sm->prev_key_replay, 0,
1308 sizeof(sm->prev_key_replay));
1309 }
1310
1311 /*
1312 * Make sure old valid counters are not accepted anymore and
1313 * do not get copied again.
1314 */
1315 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 }
1317
1318#ifdef CONFIG_PEERKEY
1319 if (msg == SMK_M3) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001320 wpa_smk_m3(wpa_auth, sm, key, key_data, key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 return;
1322 }
1323#endif /* CONFIG_PEERKEY */
1324
1325 os_free(sm->last_rx_eapol_key);
1326 sm->last_rx_eapol_key = os_malloc(data_len);
1327 if (sm->last_rx_eapol_key == NULL)
1328 return;
1329 os_memcpy(sm->last_rx_eapol_key, data, data_len);
1330 sm->last_rx_eapol_key_len = data_len;
1331
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001332 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001333 sm->EAPOLKeyReceived = TRUE;
1334 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1335 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1336 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1337 wpa_sm_step(sm);
1338}
1339
1340
1341static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1342 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1343{
1344 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
1345 u8 *pos;
1346 int ret = 0;
1347
1348 /* GTK = PRF-X(GMK, "Group key expansion",
1349 * AA || GNonce || Time || random data)
1350 * The example described in the IEEE 802.11 standard uses only AA and
1351 * GNonce as inputs here. Add some more entropy since this derivation
1352 * is done only at the Authenticator and as such, does not need to be
1353 * exactly same.
1354 */
1355 os_memcpy(data, addr, ETH_ALEN);
1356 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1357 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1358 wpa_get_ntp_timestamp(pos);
1359 pos += 8;
1360 if (random_get_bytes(pos, 16) < 0)
1361 ret = -1;
1362
1363#ifdef CONFIG_IEEE80211W
1364 sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
1365#else /* CONFIG_IEEE80211W */
1366 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
1367 < 0)
1368 ret = -1;
1369#endif /* CONFIG_IEEE80211W */
1370
1371 return ret;
1372}
1373
1374
1375static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1376{
1377 struct wpa_authenticator *wpa_auth = eloop_ctx;
1378 struct wpa_state_machine *sm = timeout_ctx;
1379
1380 sm->pending_1_of_4_timeout = 0;
1381 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1382 sm->TimeoutEvt = TRUE;
1383 wpa_sm_step(sm);
1384}
1385
1386
1387void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1388 struct wpa_state_machine *sm, int key_info,
1389 const u8 *key_rsc, const u8 *nonce,
1390 const u8 *kde, size_t kde_len,
1391 int keyidx, int encr, int force_version)
1392{
1393 struct ieee802_1x_hdr *hdr;
1394 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001395 struct wpa_eapol_key_192 *key192;
1396 size_t len, mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 int alg;
1398 int key_data_len, pad_len = 0;
1399 u8 *buf, *pos;
1400 int version, pairwise;
1401 int i;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001402 u8 *key_data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001404 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
1405 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
1406
1407 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408
1409 if (force_version)
1410 version = force_version;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001411 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1412 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001413 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 else if (wpa_use_aes_cmac(sm))
1415 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001416 else if (sm->pairwise != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1418 else
1419 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1420
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001421 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422
1423 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1424 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1425 "encr=%d)",
1426 version,
1427 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1428 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1429 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1430 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1431 pairwise, (unsigned long) kde_len, keyidx, encr);
1432
1433 key_data_len = kde_len;
1434
1435 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001436 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001437 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1439 pad_len = key_data_len % 8;
1440 if (pad_len)
1441 pad_len = 8 - pad_len;
1442 key_data_len += pad_len + 8;
1443 }
1444
1445 len += key_data_len;
1446
1447 hdr = os_zalloc(len);
1448 if (hdr == NULL)
1449 return;
1450 hdr->version = wpa_auth->conf.eapol_version;
1451 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1452 hdr->length = host_to_be16(len - sizeof(*hdr));
1453 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001454 key192 = (struct wpa_eapol_key_192 *) (hdr + 1);
1455 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456
1457 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1458 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1459 key_info |= version;
1460 if (encr && sm->wpa == WPA_VERSION_WPA2)
1461 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1462 if (sm->wpa != WPA_VERSION_WPA2)
1463 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1464 WPA_PUT_BE16(key->key_info, key_info);
1465
1466 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001467 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1469 WPA_PUT_BE16(key->key_length, 0);
1470
1471 /* FIX: STSL: what to use as key_replay_counter? */
1472 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1473 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1474 os_memcpy(sm->key_replay[i].counter,
1475 sm->key_replay[i - 1].counter,
1476 WPA_REPLAY_COUNTER_LEN);
1477 }
1478 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1479 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1480 WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001481 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1482 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001483 sm->key_replay[0].valid = TRUE;
1484
1485 if (nonce)
1486 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1487
1488 if (key_rsc)
1489 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1490
1491 if (kde && !encr) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001492 os_memcpy(key_data, kde, kde_len);
1493 if (mic_len == 24)
1494 WPA_PUT_BE16(key192->key_data_length, kde_len);
1495 else
1496 WPA_PUT_BE16(key->key_data_length, kde_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497 } else if (encr && kde) {
1498 buf = os_zalloc(key_data_len);
1499 if (buf == NULL) {
1500 os_free(hdr);
1501 return;
1502 }
1503 pos = buf;
1504 os_memcpy(pos, kde, kde_len);
1505 pos += kde_len;
1506
1507 if (pad_len)
1508 *pos++ = 0xdd;
1509
1510 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1511 buf, key_data_len);
1512 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001513 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001514 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001515 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001516 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1517 (key_data_len - 8) / 8, buf, key_data)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001518 os_free(hdr);
1519 os_free(buf);
1520 return;
1521 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001522 if (mic_len == 24)
1523 WPA_PUT_BE16(key192->key_data_length,
1524 key_data_len);
1525 else
1526 WPA_PUT_BE16(key->key_data_length,
1527 key_data_len);
1528 } else if (sm->PTK.kek_len == 16) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001529 u8 ek[32];
1530 os_memcpy(key->key_iv,
1531 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1532 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1533 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001534 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1535 os_memcpy(key_data, buf, key_data_len);
1536 rc4_skip(ek, 32, 256, key_data, key_data_len);
1537 if (mic_len == 24)
1538 WPA_PUT_BE16(key192->key_data_length,
1539 key_data_len);
1540 else
1541 WPA_PUT_BE16(key->key_data_length,
1542 key_data_len);
1543 } else {
1544 os_free(hdr);
1545 os_free(buf);
1546 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 }
1548 os_free(buf);
1549 }
1550
1551 if (key_info & WPA_KEY_INFO_MIC) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001552 u8 *key_mic;
1553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 if (!sm->PTK_valid) {
1555 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1556 "PTK not valid when sending EAPOL-Key "
1557 "frame");
1558 os_free(hdr);
1559 return;
1560 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001561
1562 key_mic = key192->key_mic; /* same offset for key and key192 */
1563 wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1564 sm->wpa_key_mgmt, version,
1565 (u8 *) hdr, len, key_mic);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001566#ifdef CONFIG_TESTING_OPTIONS
1567 if (!pairwise &&
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001568 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001569 drand48() <
1570 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1571 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1572 "Corrupting group EAPOL-Key Key MIC");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001573 key_mic[0]++;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001574 }
1575#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576 }
1577
1578 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1579 1);
1580 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1581 sm->pairwise_set);
1582 os_free(hdr);
1583}
1584
1585
1586static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1587 struct wpa_state_machine *sm, int key_info,
1588 const u8 *key_rsc, const u8 *nonce,
1589 const u8 *kde, size_t kde_len,
1590 int keyidx, int encr)
1591{
1592 int timeout_ms;
1593 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1594 int ctr;
1595
1596 if (sm == NULL)
1597 return;
1598
1599 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1600 keyidx, encr, 0);
1601
1602 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1603 if (ctr == 1 && wpa_auth->conf.tx_status)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001604 timeout_ms = pairwise ? eapol_key_timeout_first :
1605 eapol_key_timeout_first_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606 else
1607 timeout_ms = eapol_key_timeout_subseq;
1608 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1609 sm->pending_1_of_4_timeout = 1;
1610 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1611 "counter %d)", timeout_ms, ctr);
1612 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1613 wpa_send_eapol_timeout, wpa_auth, sm);
1614}
1615
1616
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001617static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
1618 size_t data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619{
1620 struct ieee802_1x_hdr *hdr;
1621 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001622 struct wpa_eapol_key_192 *key192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623 u16 key_info;
1624 int ret = 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001625 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
1626 size_t mic_len = wpa_mic_len(akmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627
1628 if (data_len < sizeof(*hdr) + sizeof(*key))
1629 return -1;
1630
1631 hdr = (struct ieee802_1x_hdr *) data;
1632 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001633 key192 = (struct wpa_eapol_key_192 *) (hdr + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001635 os_memcpy(mic, key192->key_mic, mic_len);
1636 os_memset(key192->key_mic, 0, mic_len);
1637 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1638 key_info & WPA_KEY_INFO_TYPE_MASK,
1639 data, data_len, key192->key_mic) ||
1640 os_memcmp_const(mic, key192->key_mic, mic_len) != 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641 ret = -1;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001642 os_memcpy(key192->key_mic, mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643 return ret;
1644}
1645
1646
1647void wpa_remove_ptk(struct wpa_state_machine *sm)
1648{
1649 sm->PTK_valid = FALSE;
1650 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1651 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
1652 sm->pairwise_set = FALSE;
1653 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1654}
1655
1656
1657int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1658{
1659 int remove_ptk = 1;
1660
1661 if (sm == NULL)
1662 return -1;
1663
1664 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1665 "event %d notification", event);
1666
1667 switch (event) {
1668 case WPA_AUTH:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001669#ifdef CONFIG_MESH
1670 /* PTKs are derived through AMPE */
1671 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1672 /* not mesh */
1673 break;
1674 }
1675 return 0;
1676#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 case WPA_ASSOC:
1678 break;
1679 case WPA_DEAUTH:
1680 case WPA_DISASSOC:
1681 sm->DeauthenticationRequest = TRUE;
1682 break;
1683 case WPA_REAUTH:
1684 case WPA_REAUTH_EAPOL:
1685 if (!sm->started) {
1686 /*
1687 * When using WPS, we may end up here if the STA
1688 * manages to re-associate without the previous STA
1689 * entry getting removed. Consequently, we need to make
1690 * sure that the WPA state machines gets initialized
1691 * properly at this point.
1692 */
1693 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1694 "started - initialize now");
1695 sm->started = 1;
1696 sm->Init = TRUE;
1697 if (wpa_sm_step(sm) == 1)
1698 return 1; /* should not really happen */
1699 sm->Init = FALSE;
1700 sm->AuthenticationRequest = TRUE;
1701 break;
1702 }
1703 if (sm->GUpdateStationKeys) {
1704 /*
1705 * Reauthentication cancels the pending group key
1706 * update for this STA.
1707 */
1708 sm->group->GKeyDoneStations--;
1709 sm->GUpdateStationKeys = FALSE;
1710 sm->PtkGroupInit = TRUE;
1711 }
1712 sm->ReAuthenticationRequest = TRUE;
1713 break;
1714 case WPA_ASSOC_FT:
1715#ifdef CONFIG_IEEE80211R
1716 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1717 "after association");
1718 wpa_ft_install_ptk(sm);
1719
1720 /* Using FT protocol, not WPA auth state machine */
1721 sm->ft_completed = 1;
1722 return 0;
1723#else /* CONFIG_IEEE80211R */
1724 break;
1725#endif /* CONFIG_IEEE80211R */
1726 }
1727
1728#ifdef CONFIG_IEEE80211R
1729 sm->ft_completed = 0;
1730#endif /* CONFIG_IEEE80211R */
1731
1732#ifdef CONFIG_IEEE80211W
1733 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1734 remove_ptk = 0;
1735#endif /* CONFIG_IEEE80211W */
1736
1737 if (remove_ptk) {
1738 sm->PTK_valid = FALSE;
1739 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1740
1741 if (event != WPA_REAUTH_EAPOL)
1742 wpa_remove_ptk(sm);
1743 }
1744
1745 return wpa_sm_step(sm);
1746}
1747
1748
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001749SM_STATE(WPA_PTK, INITIALIZE)
1750{
1751 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1752 if (sm->Init) {
1753 /* Init flag is not cleared here, so avoid busy
1754 * loop by claiming nothing changed. */
1755 sm->changed = FALSE;
1756 }
1757
1758 sm->keycount = 0;
1759 if (sm->GUpdateStationKeys)
1760 sm->group->GKeyDoneStations--;
1761 sm->GUpdateStationKeys = FALSE;
1762 if (sm->wpa == WPA_VERSION_WPA)
1763 sm->PInitAKeys = FALSE;
1764 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1765 * Local AA > Remote AA)) */) {
1766 sm->Pair = TRUE;
1767 }
1768 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1769 wpa_remove_ptk(sm);
1770 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1771 sm->TimeoutCtr = 0;
1772 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1773 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1774 WPA_EAPOL_authorized, 0);
1775 }
1776}
1777
1778
1779SM_STATE(WPA_PTK, DISCONNECT)
1780{
1781 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1782 sm->Disconnect = FALSE;
1783 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1784}
1785
1786
1787SM_STATE(WPA_PTK, DISCONNECTED)
1788{
1789 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1790 sm->DeauthenticationRequest = FALSE;
1791}
1792
1793
1794SM_STATE(WPA_PTK, AUTHENTICATION)
1795{
1796 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1797 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1798 sm->PTK_valid = FALSE;
1799 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1800 1);
1801 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1802 sm->AuthenticationRequest = FALSE;
1803}
1804
1805
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001806static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1807 struct wpa_group *group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001809 if (group->first_sta_seen)
1810 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811 /*
1812 * System has run bit further than at the time hostapd was started
1813 * potentially very early during boot up. This provides better chances
1814 * of collecting more randomness on embedded systems. Re-initialize the
1815 * GMK and Counter here to improve their strength if there was not
1816 * enough entropy available immediately after system startup.
1817 */
1818 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1819 "station");
1820 if (random_pool_ready() != 1) {
1821 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1822 "to proceed - reject first 4-way handshake");
1823 group->reject_4way_hs_for_entropy = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001824 } else {
1825 group->first_sta_seen = TRUE;
1826 group->reject_4way_hs_for_entropy = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829 wpa_group_init_gmk_and_counter(wpa_auth, group);
1830 wpa_gtk_update(wpa_auth, group);
1831 wpa_group_config_group_keys(wpa_auth, group);
1832}
1833
1834
1835SM_STATE(WPA_PTK, AUTHENTICATION2)
1836{
1837 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1838
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001839 wpa_group_ensure_init(sm->wpa_auth, sm->group);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001840 sm->ReAuthenticationRequest = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001841
Dmitry Shmidt04949592012-07-19 12:16:46 -07001842 /*
1843 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1844 * ambiguous. The Authenticator state machine uses a counter that is
1845 * incremented by one for each 4-way handshake. However, the security
1846 * analysis of 4-way handshake points out that unpredictable nonces
1847 * help in preventing precomputation attacks. Instead of the state
1848 * machine definition, use an unpredictable nonce value here to provide
1849 * stronger protection against potential precomputation attacks.
1850 */
1851 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1852 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1853 "ANonce.");
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001854 sm->Disconnect = TRUE;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001855 return;
1856 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1858 WPA_NONCE_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1860 * logical place than INITIALIZE since AUTHENTICATION2 can be
1861 * re-entered on ReAuthenticationRequest without going through
1862 * INITIALIZE. */
1863 sm->TimeoutCtr = 0;
1864}
1865
1866
1867SM_STATE(WPA_PTK, INITPMK)
1868{
1869 u8 msk[2 * PMK_LEN];
1870 size_t len = 2 * PMK_LEN;
1871
1872 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1873#ifdef CONFIG_IEEE80211R
1874 sm->xxkey_len = 0;
1875#endif /* CONFIG_IEEE80211R */
1876 if (sm->pmksa) {
1877 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1878 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1879 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1880 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1881 "(len=%lu)", (unsigned long) len);
1882 os_memcpy(sm->PMK, msk, PMK_LEN);
1883#ifdef CONFIG_IEEE80211R
1884 if (len >= 2 * PMK_LEN) {
1885 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1886 sm->xxkey_len = PMK_LEN;
1887 }
1888#endif /* CONFIG_IEEE80211R */
1889 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001890 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
1891 sm->wpa_auth->cb.get_msk);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001892 sm->Disconnect = TRUE;
1893 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001895 os_memset(msk, 0, sizeof(msk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896
1897 sm->req_replay_counter_used = 0;
1898 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1899 * will break reauthentication since EAPOL state machines may not be
1900 * get into AUTHENTICATING state that clears keyRun before WPA state
1901 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1902 * state and takes PMK from the previously used AAA Key. This will
1903 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1904 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1905 * be good workaround for this issue. */
1906 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1907}
1908
1909
1910SM_STATE(WPA_PTK, INITPSK)
1911{
1912 const u8 *psk;
1913 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001914 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001915 if (psk) {
1916 os_memcpy(sm->PMK, psk, PMK_LEN);
1917#ifdef CONFIG_IEEE80211R
1918 os_memcpy(sm->xxkey, psk, PMK_LEN);
1919 sm->xxkey_len = PMK_LEN;
1920#endif /* CONFIG_IEEE80211R */
1921 }
1922 sm->req_replay_counter_used = 0;
1923}
1924
1925
1926SM_STATE(WPA_PTK, PTKSTART)
1927{
1928 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1929 size_t pmkid_len = 0;
1930
1931 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1932 sm->PTKRequest = FALSE;
1933 sm->TimeoutEvt = FALSE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001934 sm->alt_snonce_valid = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935
1936 sm->TimeoutCtr++;
1937 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1938 /* No point in sending the EAPOL-Key - we will disconnect
1939 * immediately following this. */
1940 return;
1941 }
1942
1943 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1944 "sending 1/4 msg of 4-Way Handshake");
1945 /*
1946 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1947 * one possible PSK for this STA.
1948 */
1949 if (sm->wpa == WPA_VERSION_WPA2 &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001950 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1951 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952 pmkid = buf;
1953 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1954 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1955 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1956 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001957 if (sm->pmksa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001958 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1959 sm->pmksa->pmkid, PMKID_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001960 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
1961 /* No KCK available to derive PMKID */
1962 pmkid = NULL;
1963 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964 /*
1965 * Calculate PMKID since no PMKSA cache entry was
1966 * available with pre-calculated PMKID.
1967 */
1968 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1969 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1970 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1971 }
1972 }
1973 wpa_send_eapol(sm->wpa_auth, sm,
1974 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1975 sm->ANonce, pmkid, pmkid_len, 0, 0);
1976}
1977
1978
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001979static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
1980 const u8 *pmk, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982#ifdef CONFIG_IEEE80211R
1983 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001984 return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985#endif /* CONFIG_IEEE80211R */
1986
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001987 return wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1988 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
1989 ptk, sm->wpa_key_mgmt, sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001990}
1991
1992
1993SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1994{
1995 struct wpa_ptk PTK;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07001996 int ok = 0, psk_found = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997 const u8 *pmk = NULL;
1998
1999 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2000 sm->EAPOLKeyReceived = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002001 sm->update_snonce = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002
2003 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2004 * WPA-PSK: iterate through possible PSKs and select the one matching
2005 * the packet */
2006 for (;;) {
2007 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002008 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
2009 sm->p2p_dev_addr, pmk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002010 if (pmk == NULL)
2011 break;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002012 psk_found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 } else
2014 pmk = sm->PMK;
2015
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002016 wpa_derive_ptk(sm, sm->SNonce, pmk, &PTK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002018 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
2019 sm->last_rx_eapol_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020 sm->last_rx_eapol_key_len) == 0) {
2021 ok = 1;
2022 break;
2023 }
2024
2025 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
2026 break;
2027 }
2028
2029 if (!ok) {
2030 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2031 "invalid MIC in msg 2/4 of 4-Way Handshake");
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002032 if (psk_found)
2033 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002034 return;
2035 }
2036
2037#ifdef CONFIG_IEEE80211R
2038 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2039 /*
2040 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2041 * with the value we derived.
2042 */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002043 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
2044 WPA_PMK_NAME_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2046 "PMKR1Name mismatch in FT 4-way "
2047 "handshake");
2048 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
2049 "Supplicant",
2050 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
2051 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2052 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2053 return;
2054 }
2055 }
2056#endif /* CONFIG_IEEE80211R */
2057
2058 sm->pending_1_of_4_timeout = 0;
2059 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
2060
2061 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2062 /* PSK may have changed from the previous choice, so update
2063 * state machine data based on whatever PSK was selected here.
2064 */
2065 os_memcpy(sm->PMK, pmk, PMK_LEN);
2066 }
2067
2068 sm->MICVerified = TRUE;
2069
2070 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
2071 sm->PTK_valid = TRUE;
2072}
2073
2074
2075SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2076{
2077 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2078 sm->TimeoutCtr = 0;
2079}
2080
2081
2082#ifdef CONFIG_IEEE80211W
2083
2084static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2085{
2086 if (sm->mgmt_frame_prot) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002087 size_t len;
2088 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2089 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090 }
2091
2092 return 0;
2093}
2094
2095
2096static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2097{
2098 struct wpa_igtk_kde igtk;
2099 struct wpa_group *gsm = sm->group;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002100 u8 rsc[WPA_KEY_RSC_LEN];
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002101 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102
2103 if (!sm->mgmt_frame_prot)
2104 return pos;
2105
2106 igtk.keyid[0] = gsm->GN_igtk;
2107 igtk.keyid[1] = 0;
2108 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002109 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 os_memset(igtk.pn, 0, sizeof(igtk.pn));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002111 else
2112 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002113 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002114 if (sm->wpa_auth->conf.disable_gtk) {
2115 /*
2116 * Provide unique random IGTK to each STA to prevent use of
2117 * IGTK in the BSS.
2118 */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002119 if (random_get_bytes(igtk.igtk, len) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002120 return pos;
2121 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002123 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
2124 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125
2126 return pos;
2127}
2128
2129#else /* CONFIG_IEEE80211W */
2130
2131static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2132{
2133 return 0;
2134}
2135
2136
2137static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2138{
2139 return pos;
2140}
2141
2142#endif /* CONFIG_IEEE80211W */
2143
2144
2145SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2146{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002147 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002148 size_t gtk_len, kde_len;
2149 struct wpa_group *gsm = sm->group;
2150 u8 *wpa_ie;
2151 int wpa_ie_len, secure, keyidx, encr = 0;
2152
2153 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2154 sm->TimeoutEvt = FALSE;
2155
2156 sm->TimeoutCtr++;
2157 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
2158 /* No point in sending the EAPOL-Key - we will disconnect
2159 * immediately following this. */
2160 return;
2161 }
2162
2163 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2164 GTK[GN], IGTK, [FTIE], [TIE * 2])
2165 */
2166 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2167 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2168 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2169 wpa_ie = sm->wpa_auth->wpa_ie;
2170 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
2171 if (sm->wpa == WPA_VERSION_WPA &&
2172 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
2173 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002174 /* WPA-only STA, remove RSN IE and possible MDIE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002175 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002176 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
2177 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 wpa_ie_len = wpa_ie[1] + 2;
2179 }
2180 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2181 "sending 3/4 msg of 4-Way Handshake");
2182 if (sm->wpa == WPA_VERSION_WPA2) {
2183 /* WPA2 send GTK in the 4-way handshake */
2184 secure = 1;
2185 gtk = gsm->GTK[gsm->GN - 1];
2186 gtk_len = gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002187 if (sm->wpa_auth->conf.disable_gtk) {
2188 /*
2189 * Provide unique random GTK to each STA to prevent use
2190 * of GTK in the BSS.
2191 */
2192 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
2193 return;
2194 gtk = dummy_gtk;
2195 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196 keyidx = gsm->GN;
2197 _rsc = rsc;
2198 encr = 1;
2199 } else {
2200 /* WPA does not include GTK in msg 3/4 */
2201 secure = 0;
2202 gtk = NULL;
2203 gtk_len = 0;
2204 keyidx = 0;
2205 _rsc = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002206 if (sm->rx_eapol_key_secure) {
2207 /*
2208 * It looks like Windows 7 supplicant tries to use
2209 * Secure bit in msg 2/4 after having reported Michael
2210 * MIC failure and it then rejects the 4-way handshake
2211 * if msg 3/4 does not set Secure bit. Work around this
2212 * by setting the Secure bit here even in the case of
2213 * WPA if the supplicant used it first.
2214 */
2215 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2216 "STA used Secure bit in WPA msg 2/4 - "
2217 "set Secure for 3/4 as workaround");
2218 secure = 1;
2219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002220 }
2221
2222 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
2223 if (gtk)
2224 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
2225#ifdef CONFIG_IEEE80211R
2226 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2227 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
2228 kde_len += 300; /* FTIE + 2 * TIE */
2229 }
2230#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002231#ifdef CONFIG_P2P
2232 if (WPA_GET_BE32(sm->ip_addr) > 0)
2233 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
2234#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 kde = os_malloc(kde_len);
2236 if (kde == NULL)
2237 return;
2238
2239 pos = kde;
2240 os_memcpy(pos, wpa_ie, wpa_ie_len);
2241 pos += wpa_ie_len;
2242#ifdef CONFIG_IEEE80211R
2243 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2244 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
2245 if (res < 0) {
2246 wpa_printf(MSG_ERROR, "FT: Failed to insert "
2247 "PMKR1Name into RSN IE in EAPOL-Key data");
2248 os_free(kde);
2249 return;
2250 }
2251 pos += res;
2252 }
2253#endif /* CONFIG_IEEE80211R */
2254 if (gtk) {
2255 u8 hdr[2];
2256 hdr[0] = keyidx & 0x03;
2257 hdr[1] = 0;
2258 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2259 gtk, gtk_len);
2260 }
2261 pos = ieee80211w_kde_add(sm, pos);
2262
2263#ifdef CONFIG_IEEE80211R
2264 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2265 int res;
2266 struct wpa_auth_config *conf;
2267
2268 conf = &sm->wpa_auth->conf;
2269 res = wpa_write_ftie(conf, conf->r0_key_holder,
2270 conf->r0_key_holder_len,
2271 NULL, NULL, pos, kde + kde_len - pos,
2272 NULL, 0);
2273 if (res < 0) {
2274 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2275 "into EAPOL-Key Key Data");
2276 os_free(kde);
2277 return;
2278 }
2279 pos += res;
2280
2281 /* TIE[ReassociationDeadline] (TU) */
2282 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2283 *pos++ = 5;
2284 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2285 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2286 pos += 4;
2287
2288 /* TIE[KeyLifetime] (seconds) */
2289 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2290 *pos++ = 5;
2291 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2292 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2293 pos += 4;
2294 }
2295#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002296#ifdef CONFIG_P2P
2297 if (WPA_GET_BE32(sm->ip_addr) > 0) {
2298 u8 addr[3 * 4];
2299 os_memcpy(addr, sm->ip_addr, 4);
2300 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
2301 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
2302 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
2303 addr, sizeof(addr), NULL, 0);
2304 }
2305#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002306
2307 wpa_send_eapol(sm->wpa_auth, sm,
2308 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
2309 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2310 WPA_KEY_INFO_KEY_TYPE,
2311 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2312 os_free(kde);
2313}
2314
2315
2316SM_STATE(WPA_PTK, PTKINITDONE)
2317{
2318 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2319 sm->EAPOLKeyReceived = FALSE;
2320 if (sm->Pair) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002321 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2322 int klen = wpa_cipher_key_len(sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002323 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002324 sm->PTK.tk, klen)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2326 return;
2327 }
2328 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2329 sm->pairwise_set = TRUE;
2330
2331 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2332 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2333 eloop_register_timeout(sm->wpa_auth->conf.
2334 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2335 sm->wpa_auth, sm);
2336 }
2337
2338 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2339 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2340 WPA_EAPOL_authorized, 1);
2341 }
2342 }
2343
2344 if (0 /* IBSS == TRUE */) {
2345 sm->keycount++;
2346 if (sm->keycount == 2) {
2347 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2348 WPA_EAPOL_portValid, 1);
2349 }
2350 } else {
2351 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2352 1);
2353 }
2354 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2355 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2356 if (sm->wpa == WPA_VERSION_WPA)
2357 sm->PInitAKeys = TRUE;
2358 else
2359 sm->has_GTK = TRUE;
2360 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2361 "pairwise key handshake completed (%s)",
2362 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2363
2364#ifdef CONFIG_IEEE80211R
2365 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2366#endif /* CONFIG_IEEE80211R */
2367}
2368
2369
2370SM_STEP(WPA_PTK)
2371{
2372 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2373
2374 if (sm->Init)
2375 SM_ENTER(WPA_PTK, INITIALIZE);
2376 else if (sm->Disconnect
2377 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2378 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2379 "WPA_PTK: sm->Disconnect");
2380 SM_ENTER(WPA_PTK, DISCONNECT);
2381 }
2382 else if (sm->DeauthenticationRequest)
2383 SM_ENTER(WPA_PTK, DISCONNECTED);
2384 else if (sm->AuthenticationRequest)
2385 SM_ENTER(WPA_PTK, AUTHENTICATION);
2386 else if (sm->ReAuthenticationRequest)
2387 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2388 else if (sm->PTKRequest)
2389 SM_ENTER(WPA_PTK, PTKSTART);
2390 else switch (sm->wpa_ptk_state) {
2391 case WPA_PTK_INITIALIZE:
2392 break;
2393 case WPA_PTK_DISCONNECT:
2394 SM_ENTER(WPA_PTK, DISCONNECTED);
2395 break;
2396 case WPA_PTK_DISCONNECTED:
2397 SM_ENTER(WPA_PTK, INITIALIZE);
2398 break;
2399 case WPA_PTK_AUTHENTICATION:
2400 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2401 break;
2402 case WPA_PTK_AUTHENTICATION2:
2403 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2404 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2405 WPA_EAPOL_keyRun) > 0)
2406 SM_ENTER(WPA_PTK, INITPMK);
2407 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
2408 /* FIX: && 802.1X::keyRun */)
2409 SM_ENTER(WPA_PTK, INITPSK);
2410 break;
2411 case WPA_PTK_INITPMK:
2412 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2413 WPA_EAPOL_keyAvailable) > 0)
2414 SM_ENTER(WPA_PTK, PTKSTART);
2415 else {
2416 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2417 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2418 "INITPMK - keyAvailable = false");
2419 SM_ENTER(WPA_PTK, DISCONNECT);
2420 }
2421 break;
2422 case WPA_PTK_INITPSK:
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002423 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
2424 NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425 SM_ENTER(WPA_PTK, PTKSTART);
2426 else {
2427 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2428 "no PSK configured for the STA");
2429 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2430 SM_ENTER(WPA_PTK, DISCONNECT);
2431 }
2432 break;
2433 case WPA_PTK_PTKSTART:
2434 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2435 sm->EAPOLKeyPairwise)
2436 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2437 else if (sm->TimeoutCtr >
2438 (int) dot11RSNAConfigPairwiseUpdateCount) {
2439 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2440 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2441 "PTKSTART: Retry limit %d reached",
2442 dot11RSNAConfigPairwiseUpdateCount);
2443 SM_ENTER(WPA_PTK, DISCONNECT);
2444 } else if (sm->TimeoutEvt)
2445 SM_ENTER(WPA_PTK, PTKSTART);
2446 break;
2447 case WPA_PTK_PTKCALCNEGOTIATING:
2448 if (sm->MICVerified)
2449 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2450 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2451 sm->EAPOLKeyPairwise)
2452 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2453 else if (sm->TimeoutEvt)
2454 SM_ENTER(WPA_PTK, PTKSTART);
2455 break;
2456 case WPA_PTK_PTKCALCNEGOTIATING2:
2457 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2458 break;
2459 case WPA_PTK_PTKINITNEGOTIATING:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002460 if (sm->update_snonce)
2461 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2462 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2463 sm->EAPOLKeyPairwise && sm->MICVerified)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464 SM_ENTER(WPA_PTK, PTKINITDONE);
2465 else if (sm->TimeoutCtr >
2466 (int) dot11RSNAConfigPairwiseUpdateCount) {
2467 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2468 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2469 "PTKINITNEGOTIATING: Retry limit %d "
2470 "reached",
2471 dot11RSNAConfigPairwiseUpdateCount);
2472 SM_ENTER(WPA_PTK, DISCONNECT);
2473 } else if (sm->TimeoutEvt)
2474 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2475 break;
2476 case WPA_PTK_PTKINITDONE:
2477 break;
2478 }
2479}
2480
2481
2482SM_STATE(WPA_PTK_GROUP, IDLE)
2483{
2484 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2485 if (sm->Init) {
2486 /* Init flag is not cleared here, so avoid busy
2487 * loop by claiming nothing changed. */
2488 sm->changed = FALSE;
2489 }
2490 sm->GTimeoutCtr = 0;
2491}
2492
2493
2494SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2495{
2496 u8 rsc[WPA_KEY_RSC_LEN];
2497 struct wpa_group *gsm = sm->group;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002498 const u8 *kde;
2499 u8 *kde_buf = NULL, *pos, hdr[2];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500 size_t kde_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002501 u8 *gtk, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502
2503 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
2504
2505 sm->GTimeoutCtr++;
2506 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
2507 /* No point in sending the EAPOL-Key - we will disconnect
2508 * immediately following this. */
2509 return;
2510 }
2511
2512 if (sm->wpa == WPA_VERSION_WPA)
2513 sm->PInitAKeys = FALSE;
2514 sm->TimeoutEvt = FALSE;
2515 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2516 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2517 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2518 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2519 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2520 "sending 1/2 msg of Group Key Handshake");
2521
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002522 gtk = gsm->GTK[gsm->GN - 1];
2523 if (sm->wpa_auth->conf.disable_gtk) {
2524 /*
2525 * Provide unique random GTK to each STA to prevent use
2526 * of GTK in the BSS.
2527 */
2528 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
2529 return;
2530 gtk = dummy_gtk;
2531 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 if (sm->wpa == WPA_VERSION_WPA2) {
2533 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
2534 ieee80211w_kde_len(sm);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002535 kde_buf = os_malloc(kde_len);
2536 if (kde_buf == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 return;
2538
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002539 kde = pos = kde_buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540 hdr[0] = gsm->GN & 0x03;
2541 hdr[1] = 0;
2542 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002543 gtk, gsm->GTK_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 pos = ieee80211w_kde_add(sm, pos);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002545 kde_len = pos - kde;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002547 kde = gtk;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002548 kde_len = gsm->GTK_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549 }
2550
2551 wpa_send_eapol(sm->wpa_auth, sm,
2552 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
2553 WPA_KEY_INFO_ACK |
2554 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002555 rsc, gsm->GNonce, kde, kde_len, gsm->GN, 1);
2556
2557 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558}
2559
2560
2561SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2562{
2563 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2564 sm->EAPOLKeyReceived = FALSE;
2565 if (sm->GUpdateStationKeys)
2566 sm->group->GKeyDoneStations--;
2567 sm->GUpdateStationKeys = FALSE;
2568 sm->GTimeoutCtr = 0;
2569 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2570 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2571 "group key handshake completed (%s)",
2572 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2573 sm->has_GTK = TRUE;
2574}
2575
2576
2577SM_STATE(WPA_PTK_GROUP, KEYERROR)
2578{
2579 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2580 if (sm->GUpdateStationKeys)
2581 sm->group->GKeyDoneStations--;
2582 sm->GUpdateStationKeys = FALSE;
2583 sm->Disconnect = TRUE;
2584}
2585
2586
2587SM_STEP(WPA_PTK_GROUP)
2588{
2589 if (sm->Init || sm->PtkGroupInit) {
2590 SM_ENTER(WPA_PTK_GROUP, IDLE);
2591 sm->PtkGroupInit = FALSE;
2592 } else switch (sm->wpa_ptk_group_state) {
2593 case WPA_PTK_GROUP_IDLE:
2594 if (sm->GUpdateStationKeys ||
2595 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2596 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2597 break;
2598 case WPA_PTK_GROUP_REKEYNEGOTIATING:
2599 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2600 !sm->EAPOLKeyPairwise && sm->MICVerified)
2601 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2602 else if (sm->GTimeoutCtr >
2603 (int) dot11RSNAConfigGroupUpdateCount)
2604 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2605 else if (sm->TimeoutEvt)
2606 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2607 break;
2608 case WPA_PTK_GROUP_KEYERROR:
2609 SM_ENTER(WPA_PTK_GROUP, IDLE);
2610 break;
2611 case WPA_PTK_GROUP_REKEYESTABLISHED:
2612 SM_ENTER(WPA_PTK_GROUP, IDLE);
2613 break;
2614 }
2615}
2616
2617
2618static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
2619 struct wpa_group *group)
2620{
2621 int ret = 0;
2622
2623 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2624 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2625 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
2626 wpa_auth->addr, group->GNonce,
2627 group->GTK[group->GN - 1], group->GTK_len) < 0)
2628 ret = -1;
2629 wpa_hexdump_key(MSG_DEBUG, "GTK",
2630 group->GTK[group->GN - 1], group->GTK_len);
2631
2632#ifdef CONFIG_IEEE80211W
2633 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002634 size_t len;
2635 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2637 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2638 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
2639 wpa_auth->addr, group->GNonce,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002640 group->IGTK[group->GN_igtk - 4], len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 ret = -1;
2642 wpa_hexdump_key(MSG_DEBUG, "IGTK",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002643 group->IGTK[group->GN_igtk - 4], len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 }
2645#endif /* CONFIG_IEEE80211W */
2646
2647 return ret;
2648}
2649
2650
2651static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2652 struct wpa_group *group)
2653{
2654 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2655 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2656 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2657 group->wpa_group_state = WPA_GROUP_GTK_INIT;
2658
2659 /* GTK[0..N] = 0 */
2660 os_memset(group->GTK, 0, sizeof(group->GTK));
2661 group->GN = 1;
2662 group->GM = 2;
2663#ifdef CONFIG_IEEE80211W
2664 group->GN_igtk = 4;
2665 group->GM_igtk = 5;
2666#endif /* CONFIG_IEEE80211W */
2667 /* GTK[GN] = CalcGTK() */
2668 wpa_gtk_update(wpa_auth, group);
2669}
2670
2671
2672static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2673{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002674 if (ctx != NULL && ctx != sm->group)
2675 return 0;
2676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002677 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2678 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2679 "Not in PTKINITDONE; skip Group Key update");
2680 sm->GUpdateStationKeys = FALSE;
2681 return 0;
2682 }
2683 if (sm->GUpdateStationKeys) {
2684 /*
2685 * This should not really happen, so add a debug log entry.
2686 * Since we clear the GKeyDoneStations before the loop, the
2687 * station needs to be counted here anyway.
2688 */
2689 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2690 "GUpdateStationKeys was already set when "
2691 "marking station for GTK rekeying");
2692 }
2693
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002694 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002695 if (sm->is_wnmsleep)
2696 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002698 sm->group->GKeyDoneStations++;
2699 sm->GUpdateStationKeys = TRUE;
2700
2701 wpa_sm_step(sm);
2702 return 0;
2703}
2704
2705
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002706#ifdef CONFIG_WNM
2707/* update GTK when exiting WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002708void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
2709{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002710 if (sm == NULL || sm->is_wnmsleep)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002711 return;
2712
2713 wpa_group_update_sta(sm, NULL);
2714}
2715
2716
2717void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
2718{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002719 if (sm)
2720 sm->is_wnmsleep = !!flag;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002721}
2722
2723
2724int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2725{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002726 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002727 u8 *start = pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002728
2729 /*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002730 * GTK subelement:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002731 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002732 * Key[5..32]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002733 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002734 *pos++ = WNM_SLEEP_SUBELEM_GTK;
2735 *pos++ = 11 + gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002736 /* Key ID in B0-B1 of Key Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002737 WPA_PUT_LE16(pos, gsm->GN & 0x03);
2738 pos += 2;
2739 *pos++ = gsm->GTK_len;
2740 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002741 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002742 pos += 8;
2743 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2744 pos += gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002745
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002746 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
2747 gsm->GN);
2748 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002749 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002750
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002751 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002752}
2753
2754
2755#ifdef CONFIG_IEEE80211W
2756int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2757{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002758 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002759 u8 *start = pos;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002760 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002761
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002762 /*
2763 * IGTK subelement:
2764 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
2765 */
2766 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002767 *pos++ = 2 + 6 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002768 WPA_PUT_LE16(pos, gsm->GN_igtk);
2769 pos += 2;
2770 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002771 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002772 pos += 6;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002773
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002774 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
2775 pos += len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002776
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002777 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
2778 gsm->GN_igtk);
2779 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002780 gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002781
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002782 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002783}
2784#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002785#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002786
2787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2789 struct wpa_group *group)
2790{
2791 int tmp;
2792
2793 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2794 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2795 group->changed = TRUE;
2796 group->wpa_group_state = WPA_GROUP_SETKEYS;
2797 group->GTKReKey = FALSE;
2798 tmp = group->GM;
2799 group->GM = group->GN;
2800 group->GN = tmp;
2801#ifdef CONFIG_IEEE80211W
2802 tmp = group->GM_igtk;
2803 group->GM_igtk = group->GN_igtk;
2804 group->GN_igtk = tmp;
2805#endif /* CONFIG_IEEE80211W */
2806 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2807 * counting the STAs that are marked with GUpdateStationKeys instead of
2808 * including all STAs that could be in not-yet-completed state. */
2809 wpa_gtk_update(wpa_auth, group);
2810
2811 if (group->GKeyDoneStations) {
2812 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
2813 "GKeyDoneStations=%d when starting new GTK rekey",
2814 group->GKeyDoneStations);
2815 group->GKeyDoneStations = 0;
2816 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002817 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2819 group->GKeyDoneStations);
2820}
2821
2822
2823static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
2824 struct wpa_group *group)
2825{
2826 int ret = 0;
2827
2828 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002829 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830 broadcast_ether_addr, group->GN,
2831 group->GTK[group->GN - 1], group->GTK_len) < 0)
2832 ret = -1;
2833
2834#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002835 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2836 enum wpa_alg alg;
2837 size_t len;
2838
2839 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
2840 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2841
2842 if (ret == 0 &&
2843 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
2844 broadcast_ether_addr, group->GN_igtk,
2845 group->IGTK[group->GN_igtk - 4], len) < 0)
2846 ret = -1;
2847 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848#endif /* CONFIG_IEEE80211W */
2849
2850 return ret;
2851}
2852
2853
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002854static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
2855{
2856 if (sm->group == ctx) {
2857 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
2858 " for discconnection due to fatal failure",
2859 MAC2STR(sm->addr));
2860 sm->Disconnect = TRUE;
2861 }
2862
2863 return 0;
2864}
2865
2866
2867static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
2868 struct wpa_group *group)
2869{
2870 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
2871 group->changed = TRUE;
2872 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
2873 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
2874}
2875
2876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2878 struct wpa_group *group)
2879{
2880 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2881 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2882 group->changed = TRUE;
2883 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2884
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002885 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
2886 wpa_group_fatal_failure(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002887 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002888 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889
2890 return 0;
2891}
2892
2893
2894static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2895 struct wpa_group *group)
2896{
2897 if (group->GInit) {
2898 wpa_group_gtk_init(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002899 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
2900 /* Do not allow group operations */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2902 group->GTKAuthenticator) {
2903 wpa_group_setkeysdone(wpa_auth, group);
2904 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2905 group->GTKReKey) {
2906 wpa_group_setkeys(wpa_auth, group);
2907 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2908 if (group->GKeyDoneStations == 0)
2909 wpa_group_setkeysdone(wpa_auth, group);
2910 else if (group->GTKReKey)
2911 wpa_group_setkeys(wpa_auth, group);
2912 }
2913}
2914
2915
2916static int wpa_sm_step(struct wpa_state_machine *sm)
2917{
2918 if (sm == NULL)
2919 return 0;
2920
2921 if (sm->in_step_loop) {
2922 /* This should not happen, but if it does, make sure we do not
2923 * end up freeing the state machine too early by exiting the
2924 * recursive call. */
2925 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2926 return 0;
2927 }
2928
2929 sm->in_step_loop = 1;
2930 do {
2931 if (sm->pending_deinit)
2932 break;
2933
2934 sm->changed = FALSE;
2935 sm->wpa_auth->group->changed = FALSE;
2936
2937 SM_STEP_RUN(WPA_PTK);
2938 if (sm->pending_deinit)
2939 break;
2940 SM_STEP_RUN(WPA_PTK_GROUP);
2941 if (sm->pending_deinit)
2942 break;
2943 wpa_group_sm_step(sm->wpa_auth, sm->group);
2944 } while (sm->changed || sm->wpa_auth->group->changed);
2945 sm->in_step_loop = 0;
2946
2947 if (sm->pending_deinit) {
2948 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2949 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2950 wpa_free_sta_sm(sm);
2951 return 1;
2952 }
2953 return 0;
2954}
2955
2956
2957static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2958{
2959 struct wpa_state_machine *sm = eloop_ctx;
2960 wpa_sm_step(sm);
2961}
2962
2963
2964void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2965{
2966 if (sm == NULL)
2967 return;
2968 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2969}
2970
2971
2972void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2973{
2974 int tmp, i;
2975 struct wpa_group *group;
2976
2977 if (wpa_auth == NULL)
2978 return;
2979
2980 group = wpa_auth->group;
2981
2982 for (i = 0; i < 2; i++) {
2983 tmp = group->GM;
2984 group->GM = group->GN;
2985 group->GN = tmp;
2986#ifdef CONFIG_IEEE80211W
2987 tmp = group->GM_igtk;
2988 group->GM_igtk = group->GN_igtk;
2989 group->GN_igtk = tmp;
2990#endif /* CONFIG_IEEE80211W */
2991 wpa_gtk_update(wpa_auth, group);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002992 wpa_group_config_group_keys(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993 }
2994}
2995
2996
2997static const char * wpa_bool_txt(int bool)
2998{
2999 return bool ? "TRUE" : "FALSE";
3000}
3001
3002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003#define RSN_SUITE "%02x-%02x-%02x-%d"
3004#define RSN_SUITE_ARG(s) \
3005((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3006
3007int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
3008{
3009 int len = 0, ret;
3010 char pmkid_txt[PMKID_LEN * 2 + 1];
3011#ifdef CONFIG_RSN_PREAUTH
3012 const int preauth = 1;
3013#else /* CONFIG_RSN_PREAUTH */
3014 const int preauth = 0;
3015#endif /* CONFIG_RSN_PREAUTH */
3016
3017 if (wpa_auth == NULL)
3018 return len;
3019
3020 ret = os_snprintf(buf + len, buflen - len,
3021 "dot11RSNAOptionImplemented=TRUE\n"
3022 "dot11RSNAPreauthenticationImplemented=%s\n"
3023 "dot11RSNAEnabled=%s\n"
3024 "dot11RSNAPreauthenticationEnabled=%s\n",
3025 wpa_bool_txt(preauth),
3026 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
3027 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003028 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 return len;
3030 len += ret;
3031
3032 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
3033 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
3034
3035 ret = os_snprintf(
3036 buf + len, buflen - len,
3037 "dot11RSNAConfigVersion=%u\n"
3038 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
3039 /* FIX: dot11RSNAConfigGroupCipher */
3040 /* FIX: dot11RSNAConfigGroupRekeyMethod */
3041 /* FIX: dot11RSNAConfigGroupRekeyTime */
3042 /* FIX: dot11RSNAConfigGroupRekeyPackets */
3043 "dot11RSNAConfigGroupRekeyStrict=%u\n"
3044 "dot11RSNAConfigGroupUpdateCount=%u\n"
3045 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
3046 "dot11RSNAConfigGroupCipherSize=%u\n"
3047 "dot11RSNAConfigPMKLifetime=%u\n"
3048 "dot11RSNAConfigPMKReauthThreshold=%u\n"
3049 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
3050 "dot11RSNAConfigSATimeout=%u\n"
3051 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
3052 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
3053 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
3054 "dot11RSNAPMKIDUsed=%s\n"
3055 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
3056 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
3057 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
3058 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
3059 "dot11RSNA4WayHandshakeFailures=%u\n"
3060 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
3061 RSN_VERSION,
3062 !!wpa_auth->conf.wpa_strict_rekey,
3063 dot11RSNAConfigGroupUpdateCount,
3064 dot11RSNAConfigPairwiseUpdateCount,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003065 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003066 dot11RSNAConfigPMKLifetime,
3067 dot11RSNAConfigPMKReauthThreshold,
3068 dot11RSNAConfigSATimeout,
3069 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
3070 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
3071 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
3072 pmkid_txt,
3073 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
3074 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
3075 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
3076 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
3077 wpa_auth->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003078 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079 return len;
3080 len += ret;
3081
3082 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
3083 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
3084
3085 /* Private MIB */
3086 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
3087 wpa_auth->group->wpa_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003088 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003089 return len;
3090 len += ret;
3091
3092 return len;
3093}
3094
3095
3096int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
3097{
3098 int len = 0, ret;
3099 u32 pairwise = 0;
3100
3101 if (sm == NULL)
3102 return 0;
3103
3104 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
3105
3106 /* dot11RSNAStatsEntry */
3107
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003108 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
3109 WPA_PROTO_RSN : WPA_PROTO_WPA,
3110 sm->pairwise);
3111 if (pairwise == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112 return 0;
3113
3114 ret = os_snprintf(
3115 buf + len, buflen - len,
3116 /* TODO: dot11RSNAStatsIndex */
3117 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
3118 "dot11RSNAStatsVersion=1\n"
3119 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
3120 /* TODO: dot11RSNAStatsTKIPICVErrors */
3121 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
3122 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
3123 /* TODO: dot11RSNAStatsCCMPReplays */
3124 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
3125 /* TODO: dot11RSNAStatsTKIPReplays */,
3126 MAC2STR(sm->addr),
3127 RSN_SUITE_ARG(pairwise),
3128 sm->dot11RSNAStatsTKIPLocalMICFailures,
3129 sm->dot11RSNAStatsTKIPRemoteMICFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003130 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003131 return len;
3132 len += ret;
3133
3134 /* Private MIB */
3135 ret = os_snprintf(buf + len, buflen - len,
3136 "hostapdWPAPTKState=%d\n"
3137 "hostapdWPAPTKGroupState=%d\n",
3138 sm->wpa_ptk_state,
3139 sm->wpa_ptk_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003140 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141 return len;
3142 len += ret;
3143
3144 return len;
3145}
3146
3147
3148void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
3149{
3150 if (wpa_auth)
3151 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
3152}
3153
3154
3155int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
3156{
3157 return sm && sm->pairwise_set;
3158}
3159
3160
3161int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
3162{
3163 return sm->pairwise;
3164}
3165
3166
3167int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
3168{
3169 if (sm == NULL)
3170 return -1;
3171 return sm->wpa_key_mgmt;
3172}
3173
3174
3175int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
3176{
3177 if (sm == NULL)
3178 return 0;
3179 return sm->wpa;
3180}
3181
3182
3183int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
3184 struct rsn_pmksa_cache_entry *entry)
3185{
3186 if (sm == NULL || sm->pmksa != entry)
3187 return -1;
3188 sm->pmksa = NULL;
3189 return 0;
3190}
3191
3192
3193struct rsn_pmksa_cache_entry *
3194wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
3195{
3196 return sm ? sm->pmksa : NULL;
3197}
3198
3199
3200void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
3201{
3202 if (sm)
3203 sm->dot11RSNAStatsTKIPLocalMICFailures++;
3204}
3205
3206
3207const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
3208{
3209 if (wpa_auth == NULL)
3210 return NULL;
3211 *len = wpa_auth->wpa_ie_len;
3212 return wpa_auth->wpa_ie;
3213}
3214
3215
3216int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
3217 int session_timeout, struct eapol_state_machine *eapol)
3218{
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07003219 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
3220 sm->wpa_auth->conf.disable_pmksa_caching)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003221 return -1;
3222
3223 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003224 sm->PTK.kck, sm->PTK.kck_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225 sm->wpa_auth->addr, sm->addr, session_timeout,
3226 eapol, sm->wpa_key_mgmt))
3227 return 0;
3228
3229 return -1;
3230}
3231
3232
3233int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
3234 const u8 *pmk, size_t len, const u8 *sta_addr,
3235 int session_timeout,
3236 struct eapol_state_machine *eapol)
3237{
3238 if (wpa_auth == NULL)
3239 return -1;
3240
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003241 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len,
3242 NULL, 0,
3243 wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003244 sta_addr, session_timeout, eapol,
3245 WPA_KEY_MGMT_IEEE8021X))
3246 return 0;
3247
3248 return -1;
3249}
3250
3251
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003252int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
3253 const u8 *pmk)
3254{
3255 if (wpa_auth->conf.disable_pmksa_caching)
3256 return -1;
3257
3258 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN,
3259 NULL, 0,
3260 wpa_auth->addr, addr, 0, NULL,
3261 WPA_KEY_MGMT_SAE))
3262 return 0;
3263
3264 return -1;
3265}
3266
3267
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003268void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
3269 const u8 *sta_addr)
3270{
3271 struct rsn_pmksa_cache_entry *pmksa;
3272
3273 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
3274 return;
3275 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3276 if (pmksa) {
3277 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
3278 MACSTR " based on request", MAC2STR(sta_addr));
3279 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
3280 }
3281}
3282
3283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003284static struct wpa_group *
3285wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3286{
3287 struct wpa_group *group;
3288
3289 if (wpa_auth == NULL || wpa_auth->group == NULL)
3290 return NULL;
3291
3292 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
3293 vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003294 group = wpa_group_init(wpa_auth, vlan_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003295 if (group == NULL)
3296 return NULL;
3297
3298 group->next = wpa_auth->group->next;
3299 wpa_auth->group->next = group;
3300
3301 return group;
3302}
3303
3304
3305int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
3306{
3307 struct wpa_group *group;
3308
3309 if (sm == NULL || sm->wpa_auth == NULL)
3310 return 0;
3311
3312 group = sm->wpa_auth->group;
3313 while (group) {
3314 if (group->vlan_id == vlan_id)
3315 break;
3316 group = group->next;
3317 }
3318
3319 if (group == NULL) {
3320 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
3321 if (group == NULL)
3322 return -1;
3323 }
3324
3325 if (sm->group == group)
3326 return 0;
3327
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003328 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
3329 return -1;
3330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
3332 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
3333
3334 sm->group = group;
3335 return 0;
3336}
3337
3338
3339void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
3340 struct wpa_state_machine *sm, int ack)
3341{
3342 if (wpa_auth == NULL || sm == NULL)
3343 return;
3344 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
3345 " ack=%d", MAC2STR(sm->addr), ack);
3346 if (sm->pending_1_of_4_timeout && ack) {
3347 /*
3348 * Some deployed supplicant implementations update their SNonce
3349 * for each EAPOL-Key 2/4 message even within the same 4-way
3350 * handshake and then fail to use the first SNonce when
3351 * deriving the PTK. This results in unsuccessful 4-way
3352 * handshake whenever the relatively short initial timeout is
3353 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
3354 * around this by increasing the timeout now that we know that
3355 * the station has received the frame.
3356 */
3357 int timeout_ms = eapol_key_timeout_subseq;
3358 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
3359 "timeout by %u ms because of acknowledged frame",
3360 timeout_ms);
3361 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
3362 eloop_register_timeout(timeout_ms / 1000,
3363 (timeout_ms % 1000) * 1000,
3364 wpa_send_eapol_timeout, wpa_auth, sm);
3365 }
3366}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003367
3368
3369int wpa_auth_uses_sae(struct wpa_state_machine *sm)
3370{
3371 if (sm == NULL)
3372 return 0;
3373 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
3374}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003375
3376
3377int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
3378{
3379 if (sm == NULL)
3380 return 0;
3381 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
3382}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003383
3384
3385#ifdef CONFIG_P2P
3386int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
3387{
3388 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
3389 return -1;
3390 os_memcpy(addr, sm->ip_addr, 4);
3391 return 0;
3392}
3393#endif /* CONFIG_P2P */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003394
3395
3396int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
3397 struct radius_das_attrs *attr)
3398{
3399 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
3400}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003401
3402
3403void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
3404{
3405 struct wpa_group *group;
3406
3407 if (!wpa_auth)
3408 return;
3409 for (group = wpa_auth->group; group; group = group->next)
3410 wpa_group_config_group_keys(wpa_auth, group);
3411}