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