• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * IEEE 802.11 RSN / WPA Authenticator
3  * Copyright (c) 2004-2019, 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 "utils/bitfield.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ocv.h"
17 #include "crypto/aes.h"
18 #include "crypto/aes_wrap.h"
19 #include "crypto/aes_siv.h"
20 #include "crypto/crypto.h"
21 #include "crypto/sha1.h"
22 #include "crypto/sha256.h"
23 #include "crypto/sha384.h"
24 #include "crypto/random.h"
25 #include "eapol_auth/eapol_auth_sm.h"
26 #include "drivers/driver.h"
27 #include "ap_config.h"
28 #include "ieee802_11.h"
29 #include "wpa_auth.h"
30 #include "pmksa_cache_auth.h"
31 #include "wpa_auth_i.h"
32 #include "wpa_auth_ie.h"
33 
34 #define STATE_MACHINE_DATA struct wpa_state_machine
35 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
36 #define STATE_MACHINE_ADDR sm->addr
37 
38 
39 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
40 static int wpa_sm_step(struct wpa_state_machine *sm);
41 static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
42 			      u8 *data, size_t data_len);
43 #ifdef CONFIG_FILS
44 static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
45 			    u8 *buf, size_t buf_len, u16 *_key_data_len);
46 static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
47 					     const struct wpabuf *hlp);
48 #endif /* CONFIG_FILS */
49 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
50 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
51 			      struct wpa_group *group);
52 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
53 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
54 			  struct wpa_group *group);
55 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
56 				       struct wpa_group *group);
57 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
58 			  const u8 *pmk, unsigned int pmk_len,
59 			  struct wpa_ptk *ptk);
60 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
61 			   struct wpa_group *group);
62 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
63 			  struct wpa_group *group);
64 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
65 			  struct wpa_group *group);
66 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos);
67 
68 static const u32 eapol_key_timeout_first = 100; /* ms */
69 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
70 static const u32 eapol_key_timeout_first_group = 500; /* ms */
71 static const u32 eapol_key_timeout_no_retrans = 4000; /* ms */
72 
73 /* TODO: make these configurable */
74 static const int dot11RSNAConfigPMKLifetime = 43200;
75 static const int dot11RSNAConfigPMKReauthThreshold = 70;
76 static const int dot11RSNAConfigSATimeout = 60;
77 
78 
wpa_auth_mic_failure_report(struct wpa_authenticator * wpa_auth,const u8 * addr)79 static inline int wpa_auth_mic_failure_report(
80 	struct wpa_authenticator *wpa_auth, const u8 *addr)
81 {
82 	if (wpa_auth->cb->mic_failure_report)
83 		return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr);
84 	return 0;
85 }
86 
87 
wpa_auth_psk_failure_report(struct wpa_authenticator * wpa_auth,const u8 * addr)88 static inline void wpa_auth_psk_failure_report(
89 	struct wpa_authenticator *wpa_auth, const u8 *addr)
90 {
91 	if (wpa_auth->cb->psk_failure_report)
92 		wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr);
93 }
94 
95 
wpa_auth_set_eapol(struct wpa_authenticator * wpa_auth,const u8 * addr,wpa_eapol_variable var,int value)96 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
97 				      const u8 *addr, wpa_eapol_variable var,
98 				      int value)
99 {
100 	if (wpa_auth->cb->set_eapol)
101 		wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value);
102 }
103 
104 
wpa_auth_get_eapol(struct wpa_authenticator * wpa_auth,const u8 * addr,wpa_eapol_variable var)105 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
106 				     const u8 *addr, wpa_eapol_variable var)
107 {
108 	if (wpa_auth->cb->get_eapol == NULL)
109 		return -1;
110 	return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var);
111 }
112 
113 
wpa_auth_get_psk(struct wpa_authenticator * wpa_auth,const u8 * addr,const u8 * p2p_dev_addr,const u8 * prev_psk,size_t * psk_len,int * vlan_id)114 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
115 					  const u8 *addr,
116 					  const u8 *p2p_dev_addr,
117 					  const u8 *prev_psk, size_t *psk_len,
118 					  int *vlan_id)
119 {
120 	if (wpa_auth->cb->get_psk == NULL)
121 		return NULL;
122 	return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
123 				     prev_psk, psk_len, vlan_id);
124 }
125 
126 
wpa_auth_get_msk(struct wpa_authenticator * wpa_auth,const u8 * addr,u8 * msk,size_t * len)127 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
128 				   const u8 *addr, u8 *msk, size_t *len)
129 {
130 	if (wpa_auth->cb->get_msk == NULL)
131 		return -1;
132 	return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
133 }
134 
135 
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)136 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
137 				   int vlan_id,
138 				   enum wpa_alg alg, const u8 *addr, int idx,
139 				   u8 *key, size_t key_len)
140 {
141 	if (wpa_auth == NULL)
142 		return -1;
143 
144 	if (wpa_auth->cb == NULL)
145 		return -1;
146 
147 	if (wpa_auth->cb->set_key == NULL)
148 		return -1;
149 	return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
150 				     key, key_len);
151 }
152 
153 
wpa_auth_get_seqnum(struct wpa_authenticator * wpa_auth,const u8 * addr,int idx,u8 * seq)154 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
155 				      const u8 *addr, int idx, u8 *seq)
156 {
157 	if (wpa_auth->cb->get_seqnum == NULL)
158 		return -1;
159 	return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
160 }
161 
162 
163 static inline int
wpa_auth_send_eapol(struct wpa_authenticator * wpa_auth,const u8 * addr,const u8 * data,size_t data_len,int encrypt)164 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
165 		    const u8 *data, size_t data_len, int encrypt)
166 {
167 	if (wpa_auth->cb->send_eapol == NULL)
168 		return -1;
169 	return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
170 					encrypt);
171 }
172 
173 
174 #ifdef CONFIG_MESH
wpa_auth_start_ampe(struct wpa_authenticator * wpa_auth,const u8 * addr)175 static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
176 				      const u8 *addr)
177 {
178 	if (wpa_auth->cb->start_ampe == NULL)
179 		return -1;
180 	return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
181 }
182 #endif /* CONFIG_MESH */
183 
184 
wpa_auth_for_each_sta(struct wpa_authenticator * wpa_auth,int (* cb)(struct wpa_state_machine * sm,void * ctx),void * cb_ctx)185 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
186 			  int (*cb)(struct wpa_state_machine *sm, void *ctx),
187 			  void *cb_ctx)
188 {
189 	if (wpa_auth->cb->for_each_sta == NULL)
190 		return 0;
191 	return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
192 }
193 
194 
wpa_auth_for_each_auth(struct wpa_authenticator * wpa_auth,int (* cb)(struct wpa_authenticator * a,void * ctx),void * cb_ctx)195 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
196 			   int (*cb)(struct wpa_authenticator *a, void *ctx),
197 			   void *cb_ctx)
198 {
199 	if (wpa_auth->cb->for_each_auth == NULL)
200 		return 0;
201 	return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
202 }
203 
204 
wpa_auth_logger(struct wpa_authenticator * wpa_auth,const u8 * addr,logger_level level,const char * txt)205 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
206 		     logger_level level, const char *txt)
207 {
208 	if (wpa_auth->cb->logger == NULL)
209 		return;
210 	wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
211 }
212 
213 
wpa_auth_vlogger(struct wpa_authenticator * wpa_auth,const u8 * addr,logger_level level,const char * fmt,...)214 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
215 		      logger_level level, const char *fmt, ...)
216 {
217 	char *format;
218 	int maxlen;
219 	va_list ap;
220 
221 	if (wpa_auth->cb->logger == NULL)
222 		return;
223 
224 	maxlen = os_strlen(fmt) + 100;
225 	format = os_malloc(maxlen);
226 	if (!format)
227 		return;
228 
229 	va_start(ap, fmt);
230 	vsnprintf(format, maxlen, fmt, ap);
231 	va_end(ap);
232 
233 	wpa_auth_logger(wpa_auth, addr, level, format);
234 
235 	os_free(format);
236 }
237 
238 
wpa_sta_disconnect(struct wpa_authenticator * wpa_auth,const u8 * addr,u16 reason)239 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
240 			       const u8 *addr, u16 reason)
241 {
242 	if (wpa_auth->cb->disconnect == NULL)
243 		return;
244 	wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR " (reason %u)",
245 		   MAC2STR(addr), reason);
246 	wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr, reason);
247 }
248 
249 
250 #ifdef CONFIG_OCV
wpa_channel_info(struct wpa_authenticator * wpa_auth,struct wpa_channel_info * ci)251 static int wpa_channel_info(struct wpa_authenticator *wpa_auth,
252 			    struct wpa_channel_info *ci)
253 {
254 	if (!wpa_auth->cb->channel_info)
255 		return -1;
256 	return wpa_auth->cb->channel_info(wpa_auth->cb_ctx, ci);
257 }
258 #endif /* CONFIG_OCV */
259 
260 
wpa_auth_update_vlan(struct wpa_authenticator * wpa_auth,const u8 * addr,int vlan_id)261 static int wpa_auth_update_vlan(struct wpa_authenticator *wpa_auth,
262 				const u8 *addr, int vlan_id)
263 {
264 	if (!wpa_auth->cb->update_vlan)
265 		return -1;
266 	return wpa_auth->cb->update_vlan(wpa_auth->cb_ctx, addr, vlan_id);
267 }
268 
269 
wpa_rekey_gmk(void * eloop_ctx,void * timeout_ctx)270 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
271 {
272 	struct wpa_authenticator *wpa_auth = eloop_ctx;
273 
274 	if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
275 		wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
276 			   "initialization.");
277 	} else {
278 		wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
279 		wpa_hexdump_key(MSG_DEBUG, "GMK",
280 				wpa_auth->group->GMK, WPA_GMK_LEN);
281 	}
282 
283 	if (wpa_auth->conf.wpa_gmk_rekey) {
284 		eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
285 				       wpa_rekey_gmk, wpa_auth, NULL);
286 	}
287 }
288 
289 
wpa_rekey_gtk(void * eloop_ctx,void * timeout_ctx)290 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
291 {
292 	struct wpa_authenticator *wpa_auth = eloop_ctx;
293 	struct wpa_group *group, *next;
294 
295 	wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
296 	group = wpa_auth->group;
297 	while (group) {
298 		wpa_group_get(wpa_auth, group);
299 
300 		group->GTKReKey = TRUE;
301 		do {
302 			group->changed = FALSE;
303 			wpa_group_sm_step(wpa_auth, group);
304 		} while (group->changed);
305 
306 		next = group->next;
307 		wpa_group_put(wpa_auth, group);
308 		group = next;
309 	}
310 
311 	if (wpa_auth->conf.wpa_group_rekey) {
312 		eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
313 				       0, wpa_rekey_gtk, wpa_auth, NULL);
314 	}
315 }
316 
317 
wpa_rekey_ptk(void * eloop_ctx,void * timeout_ctx)318 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
319 {
320 	struct wpa_authenticator *wpa_auth = eloop_ctx;
321 	struct wpa_state_machine *sm = timeout_ctx;
322 
323 	wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
324 	wpa_request_new_ptk(sm);
325 	wpa_sm_step(sm);
326 }
327 
328 
wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine * sm)329 void wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine *sm)
330 {
331 	if (sm && sm->wpa_auth->conf.wpa_ptk_rekey) {
332 		wpa_printf(MSG_DEBUG, "WPA: Start PTK rekeying timer for "
333 			   MACSTR " (%d seconds)", MAC2STR(sm->addr),
334 			   sm->wpa_auth->conf.wpa_ptk_rekey);
335 		eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
336 		eloop_register_timeout(sm->wpa_auth->conf.wpa_ptk_rekey, 0,
337 				       wpa_rekey_ptk, sm->wpa_auth, sm);
338 	}
339 }
340 
341 
wpa_auth_pmksa_clear_cb(struct wpa_state_machine * sm,void * ctx)342 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
343 {
344 	if (sm->pmksa == ctx)
345 		sm->pmksa = NULL;
346 	return 0;
347 }
348 
349 
wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry * entry,void * ctx)350 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
351 				   void *ctx)
352 {
353 	struct wpa_authenticator *wpa_auth = ctx;
354 	wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
355 }
356 
357 
wpa_group_init_gmk_and_counter(struct wpa_authenticator * wpa_auth,struct wpa_group * group)358 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
359 					  struct wpa_group *group)
360 {
361 	u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
362 	u8 rkey[32];
363 	unsigned long ptr;
364 
365 	if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
366 		return -1;
367 	wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
368 
369 	/*
370 	 * Counter = PRF-256(Random number, "Init Counter",
371 	 *                   Local MAC Address || Time)
372 	 */
373 	os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
374 	wpa_get_ntp_timestamp(buf + ETH_ALEN);
375 	ptr = (unsigned long) group;
376 	os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
377 #ifdef TEST_FUZZ
378 	os_memset(buf + ETH_ALEN, 0xab, 8);
379 	os_memset(buf + ETH_ALEN + 8, 0xcd, sizeof(ptr));
380 #endif /* TEST_FUZZ */
381 	if (random_get_bytes(rkey, sizeof(rkey)) < 0)
382 		return -1;
383 
384 	if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
385 		     group->Counter, WPA_NONCE_LEN) < 0)
386 		return -1;
387 	wpa_hexdump_key(MSG_DEBUG, "Key Counter",
388 			group->Counter, WPA_NONCE_LEN);
389 
390 	return 0;
391 }
392 
393 
wpa_group_init(struct wpa_authenticator * wpa_auth,int vlan_id,int delay_init)394 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
395 					 int vlan_id, int delay_init)
396 {
397 	struct wpa_group *group;
398 
399 	group = os_zalloc(sizeof(struct wpa_group));
400 	if (group == NULL)
401 		return NULL;
402 
403 	group->GTKAuthenticator = TRUE;
404 	group->vlan_id = vlan_id;
405 	group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
406 
407 	if (random_pool_ready() != 1) {
408 		wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
409 			   "for secure operations - update keys later when "
410 			   "the first station connects");
411 	}
412 
413 	/*
414 	 * Set initial GMK/Counter value here. The actual values that will be
415 	 * used in negotiations will be set once the first station tries to
416 	 * connect. This allows more time for collecting additional randomness
417 	 * on embedded devices.
418 	 */
419 	if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
420 		wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
421 			   "initialization.");
422 		os_free(group);
423 		return NULL;
424 	}
425 
426 	group->GInit = TRUE;
427 	if (delay_init) {
428 		wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
429 			   "until Beacon frames have been configured");
430 		/* Initialization is completed in wpa_init_keys(). */
431 	} else {
432 		wpa_group_sm_step(wpa_auth, group);
433 		group->GInit = FALSE;
434 		wpa_group_sm_step(wpa_auth, group);
435 	}
436 
437 	return group;
438 }
439 
440 
441 /**
442  * wpa_init - Initialize WPA authenticator
443  * @addr: Authenticator address
444  * @conf: Configuration for WPA authenticator
445  * @cb: Callback functions for WPA authenticator
446  * Returns: Pointer to WPA authenticator data or %NULL on failure
447  */
wpa_init(const u8 * addr,struct wpa_auth_config * conf,const struct wpa_auth_callbacks * cb,void * cb_ctx)448 struct wpa_authenticator * wpa_init(const u8 *addr,
449 				    struct wpa_auth_config *conf,
450 				    const struct wpa_auth_callbacks *cb,
451 				    void *cb_ctx)
452 {
453 	struct wpa_authenticator *wpa_auth;
454 
455 	wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
456 	if (wpa_auth == NULL)
457 		return NULL;
458 	os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
459 	os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
460 	wpa_auth->cb = cb;
461 	wpa_auth->cb_ctx = cb_ctx;
462 
463 	if (wpa_auth_gen_wpa_ie(wpa_auth)) {
464 		wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
465 		os_free(wpa_auth);
466 		return NULL;
467 	}
468 
469 	wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
470 	if (wpa_auth->group == NULL) {
471 		os_free(wpa_auth->wpa_ie);
472 		os_free(wpa_auth);
473 		return NULL;
474 	}
475 
476 	wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
477 						wpa_auth);
478 	if (wpa_auth->pmksa == NULL) {
479 		wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
480 		os_free(wpa_auth->group);
481 		os_free(wpa_auth->wpa_ie);
482 		os_free(wpa_auth);
483 		return NULL;
484 	}
485 
486 #ifdef CONFIG_IEEE80211R_AP
487 	wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
488 	if (wpa_auth->ft_pmk_cache == NULL) {
489 		wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
490 		os_free(wpa_auth->group);
491 		os_free(wpa_auth->wpa_ie);
492 		pmksa_cache_auth_deinit(wpa_auth->pmksa);
493 		os_free(wpa_auth);
494 		return NULL;
495 	}
496 #endif /* CONFIG_IEEE80211R_AP */
497 
498 	if (wpa_auth->conf.wpa_gmk_rekey) {
499 		eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
500 				       wpa_rekey_gmk, wpa_auth, NULL);
501 	}
502 
503 	if (wpa_auth->conf.wpa_group_rekey) {
504 		eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
505 				       wpa_rekey_gtk, wpa_auth, NULL);
506 	}
507 
508 #ifdef CONFIG_P2P
509 	if (WPA_GET_BE32(conf->ip_addr_start)) {
510 		int count = WPA_GET_BE32(conf->ip_addr_end) -
511 			WPA_GET_BE32(conf->ip_addr_start) + 1;
512 		if (count > 1000)
513 			count = 1000;
514 		if (count > 0)
515 			wpa_auth->ip_pool = bitfield_alloc(count);
516 	}
517 #endif /* CONFIG_P2P */
518 
519 	return wpa_auth;
520 }
521 
522 
wpa_init_keys(struct wpa_authenticator * wpa_auth)523 int wpa_init_keys(struct wpa_authenticator *wpa_auth)
524 {
525 	struct wpa_group *group = wpa_auth->group;
526 
527 	wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
528 		   "keys");
529 	wpa_group_sm_step(wpa_auth, group);
530 	group->GInit = FALSE;
531 	wpa_group_sm_step(wpa_auth, group);
532 	if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
533 		return -1;
534 	return 0;
535 }
536 
537 
538 /**
539  * wpa_deinit - Deinitialize WPA authenticator
540  * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
541  */
wpa_deinit(struct wpa_authenticator * wpa_auth)542 void wpa_deinit(struct wpa_authenticator *wpa_auth)
543 {
544 	struct wpa_group *group, *prev;
545 
546 	eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
547 	eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
548 
549 	pmksa_cache_auth_deinit(wpa_auth->pmksa);
550 
551 #ifdef CONFIG_IEEE80211R_AP
552 	wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
553 	wpa_auth->ft_pmk_cache = NULL;
554 	wpa_ft_deinit(wpa_auth);
555 #endif /* CONFIG_IEEE80211R_AP */
556 
557 #ifdef CONFIG_P2P
558 	bitfield_free(wpa_auth->ip_pool);
559 #endif /* CONFIG_P2P */
560 
561 
562 	os_free(wpa_auth->wpa_ie);
563 
564 	group = wpa_auth->group;
565 	while (group) {
566 		prev = group;
567 		group = group->next;
568 		os_free(prev);
569 	}
570 
571 	os_free(wpa_auth);
572 }
573 
574 
575 /**
576  * wpa_reconfig - Update WPA authenticator configuration
577  * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
578  * @conf: Configuration for WPA authenticator
579  */
wpa_reconfig(struct wpa_authenticator * wpa_auth,struct wpa_auth_config * conf)580 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
581 		 struct wpa_auth_config *conf)
582 {
583 	struct wpa_group *group;
584 	if (wpa_auth == NULL)
585 		return 0;
586 
587 	os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
588 	if (wpa_auth_gen_wpa_ie(wpa_auth)) {
589 		wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
590 		return -1;
591 	}
592 
593 	/*
594 	 * Reinitialize GTK to make sure it is suitable for the new
595 	 * configuration.
596 	 */
597 	group = wpa_auth->group;
598 	group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
599 	group->GInit = TRUE;
600 	wpa_group_sm_step(wpa_auth, group);
601 	group->GInit = FALSE;
602 	wpa_group_sm_step(wpa_auth, group);
603 
604 	return 0;
605 }
606 
607 
608 struct wpa_state_machine *
wpa_auth_sta_init(struct wpa_authenticator * wpa_auth,const u8 * addr,const u8 * p2p_dev_addr)609 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
610 		  const u8 *p2p_dev_addr)
611 {
612 	struct wpa_state_machine *sm;
613 
614 	if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
615 		return NULL;
616 
617 	sm = os_zalloc(sizeof(struct wpa_state_machine));
618 	if (sm == NULL)
619 		return NULL;
620 	os_memcpy(sm->addr, addr, ETH_ALEN);
621 	if (p2p_dev_addr)
622 		os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
623 
624 	sm->wpa_auth = wpa_auth;
625 	sm->group = wpa_auth->group;
626 	wpa_group_get(sm->wpa_auth, sm->group);
627 
628 	return sm;
629 }
630 
631 
wpa_auth_sta_associated(struct wpa_authenticator * wpa_auth,struct wpa_state_machine * sm)632 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
633 			    struct wpa_state_machine *sm)
634 {
635 	if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
636 		return -1;
637 
638 #ifdef CONFIG_IEEE80211R_AP
639 	if (sm->ft_completed) {
640 		wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
641 				"FT authentication already completed - do not "
642 				"start 4-way handshake");
643 		/* Go to PTKINITDONE state to allow GTK rekeying */
644 		sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
645 		sm->Pair = TRUE;
646 		return 0;
647 	}
648 #endif /* CONFIG_IEEE80211R_AP */
649 
650 #ifdef CONFIG_FILS
651 	if (sm->fils_completed) {
652 		wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
653 				"FILS authentication already completed - do not start 4-way handshake");
654 		/* Go to PTKINITDONE state to allow GTK rekeying */
655 		sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
656 		sm->Pair = TRUE;
657 		return 0;
658 	}
659 #endif /* CONFIG_FILS */
660 
661 	if (sm->started) {
662 		os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
663 		sm->ReAuthenticationRequest = TRUE;
664 		return wpa_sm_step(sm);
665 	}
666 
667 	wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
668 			"start authentication");
669 	sm->started = 1;
670 
671 	sm->Init = TRUE;
672 	if (wpa_sm_step(sm) == 1)
673 		return 1; /* should not really happen */
674 	sm->Init = FALSE;
675 	sm->AuthenticationRequest = TRUE;
676 	return wpa_sm_step(sm);
677 }
678 
679 
wpa_auth_sta_no_wpa(struct wpa_state_machine * sm)680 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
681 {
682 	/* WPA/RSN was not used - clear WPA state. This is needed if the STA
683 	 * reassociates back to the same AP while the previous entry for the
684 	 * STA has not yet been removed. */
685 	if (sm == NULL)
686 		return;
687 
688 	sm->wpa_key_mgmt = 0;
689 }
690 
691 
wpa_free_sta_sm(struct wpa_state_machine * sm)692 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
693 {
694 #ifdef CONFIG_P2P
695 	if (WPA_GET_BE32(sm->ip_addr)) {
696 		u32 start;
697 		wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
698 			   "address %u.%u.%u.%u from " MACSTR,
699 			   sm->ip_addr[0], sm->ip_addr[1],
700 			   sm->ip_addr[2], sm->ip_addr[3],
701 			   MAC2STR(sm->addr));
702 		start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
703 		bitfield_clear(sm->wpa_auth->ip_pool,
704 			       WPA_GET_BE32(sm->ip_addr) - start);
705 	}
706 #endif /* CONFIG_P2P */
707 	if (sm->GUpdateStationKeys) {
708 		sm->group->GKeyDoneStations--;
709 		sm->GUpdateStationKeys = FALSE;
710 	}
711 #ifdef CONFIG_IEEE80211R_AP
712 	os_free(sm->assoc_resp_ftie);
713 	wpabuf_free(sm->ft_pending_req_ies);
714 #endif /* CONFIG_IEEE80211R_AP */
715 	os_free(sm->last_rx_eapol_key);
716 	os_free(sm->wpa_ie);
717 	wpa_group_put(sm->wpa_auth, sm->group);
718 #ifdef CONFIG_DPP2
719 	wpabuf_clear_free(sm->dpp_z);
720 #endif /* CONFIG_DPP2 */
721 	bin_clear_free(sm, sizeof(*sm));
722 }
723 
724 
wpa_auth_sta_deinit(struct wpa_state_machine * sm)725 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
726 {
727 	if (sm == NULL)
728 		return;
729 
730 	if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
731 		wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
732 				"strict rekeying - force GTK rekey since STA "
733 				"is leaving");
734 		if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk,
735 					  sm->wpa_auth, NULL) == -1)
736 			eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
737 					       NULL);
738 	}
739 
740 	eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
741 	sm->pending_1_of_4_timeout = 0;
742 	eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
743 	eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
744 #ifdef CONFIG_IEEE80211R_AP
745 	wpa_ft_sta_deinit(sm);
746 #endif /* CONFIG_IEEE80211R_AP */
747 	if (sm->in_step_loop) {
748 		/* Must not free state machine while wpa_sm_step() is running.
749 		 * Freeing will be completed in the end of wpa_sm_step(). */
750 		wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
751 			   "machine deinit for " MACSTR, MAC2STR(sm->addr));
752 		sm->pending_deinit = 1;
753 	} else
754 		wpa_free_sta_sm(sm);
755 }
756 
757 
wpa_request_new_ptk(struct wpa_state_machine * sm)758 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
759 {
760 	if (sm == NULL)
761 		return;
762 
763 	sm->PTKRequest = TRUE;
764 	sm->PTK_valid = 0;
765 }
766 
767 
wpa_replay_counter_valid(struct wpa_key_replay_counter * ctr,const u8 * replay_counter)768 static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
769 				    const u8 *replay_counter)
770 {
771 	int i;
772 	for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
773 		if (!ctr[i].valid)
774 			break;
775 		if (os_memcmp(replay_counter, ctr[i].counter,
776 			      WPA_REPLAY_COUNTER_LEN) == 0)
777 			return 1;
778 	}
779 	return 0;
780 }
781 
782 
wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter * ctr,const u8 * replay_counter)783 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
784 					    const u8 *replay_counter)
785 {
786 	int i;
787 	for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
788 		if (ctr[i].valid &&
789 		    (replay_counter == NULL ||
790 		     os_memcmp(replay_counter, ctr[i].counter,
791 			       WPA_REPLAY_COUNTER_LEN) == 0))
792 			ctr[i].valid = FALSE;
793 	}
794 }
795 
796 
797 #ifdef CONFIG_IEEE80211R_AP
ft_check_msg_2_of_4(struct wpa_authenticator * wpa_auth,struct wpa_state_machine * sm,struct wpa_eapol_ie_parse * kde)798 static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
799 			       struct wpa_state_machine *sm,
800 			       struct wpa_eapol_ie_parse *kde)
801 {
802 	struct wpa_ie_data ie;
803 	struct rsn_mdie *mdie;
804 
805 	if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
806 	    ie.num_pmkid != 1 || ie.pmkid == NULL) {
807 		wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
808 			   "FT 4-way handshake message 2/4");
809 		return -1;
810 	}
811 
812 	os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
813 	wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
814 		    sm->sup_pmk_r1_name, PMKID_LEN);
815 
816 	if (!kde->mdie || !kde->ftie) {
817 		wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
818 			   "message 2/4", kde->mdie ? "FTIE" : "MDIE");
819 		return -1;
820 	}
821 
822 	mdie = (struct rsn_mdie *) (kde->mdie + 2);
823 	if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
824 	    os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
825 		      MOBILITY_DOMAIN_ID_LEN) != 0) {
826 		wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
827 		return -1;
828 	}
829 
830 	if (sm->assoc_resp_ftie &&
831 	    (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
832 	     os_memcmp(kde->ftie, sm->assoc_resp_ftie,
833 		       2 + sm->assoc_resp_ftie[1]) != 0)) {
834 		wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
835 		wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
836 			    kde->ftie, kde->ftie_len);
837 		wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
838 			    sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
839 		return -1;
840 	}
841 
842 	return 0;
843 }
844 #endif /* CONFIG_IEEE80211R_AP */
845 
846 
wpa_receive_error_report(struct wpa_authenticator * wpa_auth,struct wpa_state_machine * sm,int group)847 static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
848 				    struct wpa_state_machine *sm, int group)
849 {
850 	/* Supplicant reported a Michael MIC error */
851 	wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
852 			 "received EAPOL-Key Error Request "
853 			 "(STA detected Michael MIC failure (group=%d))",
854 			 group);
855 
856 	if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
857 		wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
858 				"ignore Michael MIC failure report since "
859 				"group cipher is not TKIP");
860 	} else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
861 		wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
862 				"ignore Michael MIC failure report since "
863 				"pairwise cipher is not TKIP");
864 	} else {
865 		if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
866 			return 1; /* STA entry was removed */
867 		sm->dot11RSNAStatsTKIPRemoteMICFailures++;
868 		wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
869 	}
870 
871 	/*
872 	 * Error report is not a request for a new key handshake, but since
873 	 * Authenticator may do it, let's change the keys now anyway.
874 	 */
875 	wpa_request_new_ptk(sm);
876 	return 0;
877 }
878 
879 
wpa_try_alt_snonce(struct wpa_state_machine * sm,u8 * data,size_t data_len)880 static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
881 			      size_t data_len)
882 {
883 	struct wpa_ptk PTK;
884 	int ok = 0;
885 	const u8 *pmk = NULL;
886 	size_t pmk_len;
887 	int vlan_id = 0;
888 
889 	os_memset(&PTK, 0, sizeof(PTK));
890 	for (;;) {
891 		if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
892 		    !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
893 			pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
894 					       sm->p2p_dev_addr, pmk, &pmk_len,
895 					       &vlan_id);
896 			if (pmk == NULL)
897 				break;
898 #ifdef CONFIG_IEEE80211R_AP
899 			if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
900 				os_memcpy(sm->xxkey, pmk, pmk_len);
901 				sm->xxkey_len = pmk_len;
902 			}
903 #endif /* CONFIG_IEEE80211R_AP */
904 		} else {
905 			pmk = sm->PMK;
906 			pmk_len = sm->pmk_len;
907 		}
908 
909 		if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK) < 0)
910 			break;
911 
912 		if (wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
913 				       data, data_len) == 0) {
914 			if (sm->PMK != pmk) {
915 				os_memcpy(sm->PMK, pmk, pmk_len);
916 				sm->pmk_len = pmk_len;
917 			}
918 			ok = 1;
919 			break;
920 		}
921 
922 		if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
923 		    wpa_key_mgmt_sae(sm->wpa_key_mgmt))
924 			break;
925 	}
926 
927 	if (!ok) {
928 		wpa_printf(MSG_DEBUG,
929 			   "WPA: Earlier SNonce did not result in matching MIC");
930 		return -1;
931 	}
932 
933 	wpa_printf(MSG_DEBUG,
934 		   "WPA: Earlier SNonce resulted in matching MIC");
935 	sm->alt_snonce_valid = 0;
936 
937 	if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
938 	    wpa_auth_update_vlan(sm->wpa_auth, sm->addr, vlan_id) < 0)
939 		return -1;
940 
941 	os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
942 	os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
943 	forced_memzero(&PTK, sizeof(PTK));
944 	sm->PTK_valid = TRUE;
945 
946 	return 0;
947 }
948 
949 
wpa_receive(struct wpa_authenticator * wpa_auth,struct wpa_state_machine * sm,u8 * data,size_t data_len)950 void wpa_receive(struct wpa_authenticator *wpa_auth,
951 		 struct wpa_state_machine *sm,
952 		 u8 *data, size_t data_len)
953 {
954 	struct ieee802_1x_hdr *hdr;
955 	struct wpa_eapol_key *key;
956 	u16 key_info, key_data_length;
957 	enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST } msg;
958 	char *msgtxt;
959 	struct wpa_eapol_ie_parse kde;
960 	const u8 *key_data;
961 	size_t keyhdrlen, mic_len;
962 	u8 *mic;
963 
964 	if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
965 		return;
966 	wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
967 
968 	mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
969 	keyhdrlen = sizeof(*key) + mic_len + 2;
970 
971 	if (data_len < sizeof(*hdr) + keyhdrlen) {
972 		wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
973 		return;
974 	}
975 
976 	hdr = (struct ieee802_1x_hdr *) data;
977 	key = (struct wpa_eapol_key *) (hdr + 1);
978 	mic = (u8 *) (key + 1);
979 	key_info = WPA_GET_BE16(key->key_info);
980 	key_data = mic + mic_len + 2;
981 	key_data_length = WPA_GET_BE16(mic + mic_len);
982 	wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
983 		   " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
984 		   MAC2STR(sm->addr), key_info, key->type,
985 		   (unsigned int) mic_len, key_data_length);
986 	wpa_hexdump(MSG_MSGDUMP,
987 		    "WPA: EAPOL-Key header (ending before Key MIC)",
988 		    key, sizeof(*key));
989 	wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
990 		    mic, mic_len);
991 	if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
992 		wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
993 			   "key_data overflow (%d > %lu)",
994 			   key_data_length,
995 			   (unsigned long) (data_len - sizeof(*hdr) -
996 					    keyhdrlen));
997 		return;
998 	}
999 
1000 	if (sm->wpa == WPA_VERSION_WPA2) {
1001 		if (key->type == EAPOL_KEY_TYPE_WPA) {
1002 			/*
1003 			 * Some deployed station implementations seem to send
1004 			 * msg 4/4 with incorrect type value in WPA2 mode.
1005 			 */
1006 			wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
1007 				   "with unexpected WPA type in RSN mode");
1008 		} else if (key->type != EAPOL_KEY_TYPE_RSN) {
1009 			wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
1010 				   "unexpected type %d in RSN mode",
1011 				   key->type);
1012 			return;
1013 		}
1014 	} else {
1015 		if (key->type != EAPOL_KEY_TYPE_WPA) {
1016 			wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
1017 				   "unexpected type %d in WPA mode",
1018 				   key->type);
1019 			return;
1020 		}
1021 	}
1022 
1023 	wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
1024 		    WPA_NONCE_LEN);
1025 	wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
1026 		    key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1027 
1028 	/* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1029 	 * are set */
1030 
1031 	if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1032 		wpa_printf(MSG_DEBUG, "WPA: Ignore SMK message");
1033 		return;
1034 	}
1035 
1036 	if (key_info & WPA_KEY_INFO_REQUEST) {
1037 		msg = REQUEST;
1038 		msgtxt = "Request";
1039 	} else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1040 		msg = GROUP_2;
1041 		msgtxt = "2/2 Group";
1042 	} else if (key_data_length == 0 ||
1043 		   (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
1044 		    key_data_length == AES_BLOCK_SIZE)) {
1045 		msg = PAIRWISE_4;
1046 		msgtxt = "4/4 Pairwise";
1047 	} else {
1048 		msg = PAIRWISE_2;
1049 		msgtxt = "2/4 Pairwise";
1050 	}
1051 
1052 	if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
1053 	    msg == GROUP_2) {
1054 		u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1055 		if (sm->pairwise == WPA_CIPHER_CCMP ||
1056 		    sm->pairwise == WPA_CIPHER_GCMP) {
1057 			if (wpa_use_cmac(sm->wpa_key_mgmt) &&
1058 			    !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1059 			    ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1060 				wpa_auth_logger(wpa_auth, sm->addr,
1061 						LOGGER_WARNING,
1062 						"advertised support for "
1063 						"AES-128-CMAC, but did not "
1064 						"use it");
1065 				return;
1066 			}
1067 
1068 			if (!wpa_use_cmac(sm->wpa_key_mgmt) &&
1069 			    !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1070 			    ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1071 				wpa_auth_logger(wpa_auth, sm->addr,
1072 						LOGGER_WARNING,
1073 						"did not use HMAC-SHA1-AES "
1074 						"with CCMP/GCMP");
1075 				return;
1076 			}
1077 		}
1078 
1079 		if (wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1080 		    ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1081 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1082 					"did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1083 			return;
1084 		}
1085 	}
1086 
1087 	if (key_info & WPA_KEY_INFO_REQUEST) {
1088 		if (sm->req_replay_counter_used &&
1089 		    os_memcmp(key->replay_counter, sm->req_replay_counter,
1090 			      WPA_REPLAY_COUNTER_LEN) <= 0) {
1091 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1092 					"received EAPOL-Key request with "
1093 					"replayed counter");
1094 			return;
1095 		}
1096 	}
1097 
1098 	if (!(key_info & WPA_KEY_INFO_REQUEST) &&
1099 	    !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
1100 		int i;
1101 
1102 		if (msg == PAIRWISE_2 &&
1103 		    wpa_replay_counter_valid(sm->prev_key_replay,
1104 					     key->replay_counter) &&
1105 		    sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1106 		    os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1107 		{
1108 			/*
1109 			 * Some supplicant implementations (e.g., Windows XP
1110 			 * WZC) update SNonce for each EAPOL-Key 2/4. This
1111 			 * breaks the workaround on accepting any of the
1112 			 * pending requests, so allow the SNonce to be updated
1113 			 * even if we have already sent out EAPOL-Key 3/4.
1114 			 */
1115 			wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1116 					 "Process SNonce update from STA "
1117 					 "based on retransmitted EAPOL-Key "
1118 					 "1/4");
1119 			sm->update_snonce = 1;
1120 			os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1121 			sm->alt_snonce_valid = TRUE;
1122 			os_memcpy(sm->alt_replay_counter,
1123 				  sm->key_replay[0].counter,
1124 				  WPA_REPLAY_COUNTER_LEN);
1125 			goto continue_processing;
1126 		}
1127 
1128 		if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1129 		    sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1130 		    os_memcmp(key->replay_counter, sm->alt_replay_counter,
1131 			      WPA_REPLAY_COUNTER_LEN) == 0) {
1132 			/*
1133 			 * Supplicant may still be using the old SNonce since
1134 			 * there was two EAPOL-Key 2/4 messages and they had
1135 			 * different SNonce values.
1136 			 */
1137 			wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1138 					 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1139 			goto continue_processing;
1140 		}
1141 
1142 		if (msg == PAIRWISE_2 &&
1143 		    wpa_replay_counter_valid(sm->prev_key_replay,
1144 					     key->replay_counter) &&
1145 		    sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1146 			wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1147 					 "ignore retransmitted EAPOL-Key %s - "
1148 					 "SNonce did not change", msgtxt);
1149 		} else {
1150 			wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1151 					 "received EAPOL-Key %s with "
1152 					 "unexpected replay counter", msgtxt);
1153 		}
1154 		for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1155 			if (!sm->key_replay[i].valid)
1156 				break;
1157 			wpa_hexdump(MSG_DEBUG, "pending replay counter",
1158 				    sm->key_replay[i].counter,
1159 				    WPA_REPLAY_COUNTER_LEN);
1160 		}
1161 		wpa_hexdump(MSG_DEBUG, "received replay counter",
1162 			    key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1163 		return;
1164 	}
1165 
1166 continue_processing:
1167 #ifdef CONFIG_FILS
1168 	if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
1169 	    !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1170 		wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1171 				 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1172 		return;
1173 	}
1174 #endif /* CONFIG_FILS */
1175 
1176 	switch (msg) {
1177 	case PAIRWISE_2:
1178 		if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
1179 		    sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1180 		    (!sm->update_snonce ||
1181 		     sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
1182 			wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1183 					 "received EAPOL-Key msg 2/4 in "
1184 					 "invalid state (%d) - dropped",
1185 					 sm->wpa_ptk_state);
1186 			return;
1187 		}
1188 		random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1189 		if (sm->group->reject_4way_hs_for_entropy) {
1190 			/*
1191 			 * The system did not have enough entropy to generate
1192 			 * strong random numbers. Reject the first 4-way
1193 			 * handshake(s) and collect some entropy based on the
1194 			 * information from it. Once enough entropy is
1195 			 * available, the next atempt will trigger GMK/Key
1196 			 * Counter update and the station will be allowed to
1197 			 * continue.
1198 			 */
1199 			wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1200 				   "collect more entropy for random number "
1201 				   "generation");
1202 			random_mark_pool_ready();
1203 			wpa_sta_disconnect(wpa_auth, sm->addr,
1204 					   WLAN_REASON_PREV_AUTH_NOT_VALID);
1205 			return;
1206 		}
1207 		break;
1208 	case PAIRWISE_4:
1209 		if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1210 		    !sm->PTK_valid) {
1211 			wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1212 					 "received EAPOL-Key msg 4/4 in "
1213 					 "invalid state (%d) - dropped",
1214 					 sm->wpa_ptk_state);
1215 			return;
1216 		}
1217 		break;
1218 	case GROUP_2:
1219 		if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1220 		    || !sm->PTK_valid) {
1221 			wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1222 					 "received EAPOL-Key msg 2/2 in "
1223 					 "invalid state (%d) - dropped",
1224 					 sm->wpa_ptk_group_state);
1225 			return;
1226 		}
1227 		break;
1228 	case REQUEST:
1229 		break;
1230 	}
1231 
1232 	wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1233 			 "received EAPOL-Key frame (%s)", msgtxt);
1234 
1235 	if (key_info & WPA_KEY_INFO_ACK) {
1236 		wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1237 				"received invalid EAPOL-Key: Key Ack set");
1238 		return;
1239 	}
1240 
1241 	if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1242 	    !(key_info & WPA_KEY_INFO_MIC)) {
1243 		wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1244 				"received invalid EAPOL-Key: Key MIC not set");
1245 		return;
1246 	}
1247 
1248 #ifdef CONFIG_FILS
1249 	if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1250 	    (key_info & WPA_KEY_INFO_MIC)) {
1251 		wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1252 				"received invalid EAPOL-Key: Key MIC set");
1253 		return;
1254 	}
1255 #endif /* CONFIG_FILS */
1256 
1257 	sm->MICVerified = FALSE;
1258 	if (sm->PTK_valid && !sm->update_snonce) {
1259 		if (mic_len &&
1260 		    wpa_verify_key_mic(sm->wpa_key_mgmt, sm->pmk_len, &sm->PTK,
1261 				       data, data_len) &&
1262 		    (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1263 		     wpa_try_alt_snonce(sm, data, data_len))) {
1264 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1265 					"received EAPOL-Key with invalid MIC");
1266 #ifdef TEST_FUZZ
1267 			wpa_printf(MSG_INFO,
1268 				   "TEST: Ignore Key MIC failure for fuzz testing");
1269 			goto continue_fuzz;
1270 #endif /* TEST_FUZZ */
1271 			return;
1272 		}
1273 #ifdef CONFIG_FILS
1274 		if (!mic_len &&
1275 		    wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
1276 				     &key_data_length) < 0) {
1277 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1278 					"received EAPOL-Key with invalid MIC");
1279 #ifdef TEST_FUZZ
1280 			wpa_printf(MSG_INFO,
1281 				   "TEST: Ignore Key MIC failure for fuzz testing");
1282 			goto continue_fuzz;
1283 #endif /* TEST_FUZZ */
1284 			return;
1285 		}
1286 #endif /* CONFIG_FILS */
1287 #ifdef TEST_FUZZ
1288 	continue_fuzz:
1289 #endif /* TEST_FUZZ */
1290 		sm->MICVerified = TRUE;
1291 		eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1292 		sm->pending_1_of_4_timeout = 0;
1293 	}
1294 
1295 	if (key_info & WPA_KEY_INFO_REQUEST) {
1296 		if (sm->MICVerified) {
1297 			sm->req_replay_counter_used = 1;
1298 			os_memcpy(sm->req_replay_counter, key->replay_counter,
1299 				  WPA_REPLAY_COUNTER_LEN);
1300 		} else {
1301 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1302 					"received EAPOL-Key request with "
1303 					"invalid MIC");
1304 			return;
1305 		}
1306 
1307 		/*
1308 		 * TODO: should decrypt key data field if encryption was used;
1309 		 * even though MAC address KDE is not normally encrypted,
1310 		 * supplicant is allowed to encrypt it.
1311 		 */
1312 		if (key_info & WPA_KEY_INFO_ERROR) {
1313 			if (wpa_receive_error_report(
1314 				    wpa_auth, sm,
1315 				    !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1316 				return; /* STA entry was removed */
1317 		} else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1318 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1319 					"received EAPOL-Key Request for new "
1320 					"4-Way Handshake");
1321 			wpa_request_new_ptk(sm);
1322 		} else if (key_data_length > 0 &&
1323 			   wpa_parse_kde_ies(key_data, key_data_length,
1324 					     &kde) == 0 &&
1325 			   kde.mac_addr) {
1326 		} else {
1327 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1328 					"received EAPOL-Key Request for GTK "
1329 					"rekeying");
1330 			eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1331 			wpa_rekey_gtk(wpa_auth, NULL);
1332 		}
1333 	} else {
1334 		/* Do not allow the same key replay counter to be reused. */
1335 		wpa_replay_counter_mark_invalid(sm->key_replay,
1336 						key->replay_counter);
1337 
1338 		if (msg == PAIRWISE_2) {
1339 			/*
1340 			 * Maintain a copy of the pending EAPOL-Key frames in
1341 			 * case the EAPOL-Key frame was retransmitted. This is
1342 			 * needed to allow EAPOL-Key msg 2/4 reply to another
1343 			 * pending msg 1/4 to update the SNonce to work around
1344 			 * unexpected supplicant behavior.
1345 			 */
1346 			os_memcpy(sm->prev_key_replay, sm->key_replay,
1347 				  sizeof(sm->key_replay));
1348 		} else {
1349 			os_memset(sm->prev_key_replay, 0,
1350 				  sizeof(sm->prev_key_replay));
1351 		}
1352 
1353 		/*
1354 		 * Make sure old valid counters are not accepted anymore and
1355 		 * do not get copied again.
1356 		 */
1357 		wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
1358 	}
1359 
1360 	os_free(sm->last_rx_eapol_key);
1361 	sm->last_rx_eapol_key = os_memdup(data, data_len);
1362 	if (sm->last_rx_eapol_key == NULL)
1363 		return;
1364 	sm->last_rx_eapol_key_len = data_len;
1365 
1366 	sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
1367 	sm->EAPOLKeyReceived = TRUE;
1368 	sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1369 	sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1370 	os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1371 	wpa_sm_step(sm);
1372 }
1373 
1374 
wpa_gmk_to_gtk(const u8 * gmk,const char * label,const u8 * addr,const u8 * gnonce,u8 * gtk,size_t gtk_len)1375 static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1376 			  const u8 *gnonce, u8 *gtk, size_t gtk_len)
1377 {
1378 	u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + WPA_GTK_MAX_LEN];
1379 	u8 *pos;
1380 	int ret = 0;
1381 
1382 	/* GTK = PRF-X(GMK, "Group key expansion",
1383 	 *	AA || GNonce || Time || random data)
1384 	 * The example described in the IEEE 802.11 standard uses only AA and
1385 	 * GNonce as inputs here. Add some more entropy since this derivation
1386 	 * is done only at the Authenticator and as such, does not need to be
1387 	 * exactly same.
1388 	 */
1389 	os_memset(data, 0, sizeof(data));
1390 	os_memcpy(data, addr, ETH_ALEN);
1391 	os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1392 	pos = data + ETH_ALEN + WPA_NONCE_LEN;
1393 	wpa_get_ntp_timestamp(pos);
1394 #ifdef TEST_FUZZ
1395 	os_memset(pos, 0xef, 8);
1396 #endif /* TEST_FUZZ */
1397 	pos += 8;
1398 	if (random_get_bytes(pos, gtk_len) < 0)
1399 		ret = -1;
1400 
1401 #ifdef CONFIG_SHA384
1402 	if (sha384_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1403 		       gtk, gtk_len) < 0)
1404 		ret = -1;
1405 #else /* CONFIG_SHA384 */
1406 #ifdef CONFIG_SHA256
1407 	if (sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1408 		       gtk, gtk_len) < 0)
1409 		ret = -1;
1410 #else /* CONFIG_SHA256 */
1411 	if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1412 		     gtk, gtk_len) < 0)
1413 		ret = -1;
1414 #endif /* CONFIG_SHA256 */
1415 #endif /* CONFIG_SHA384 */
1416 
1417 	forced_memzero(data, sizeof(data));
1418 
1419 	return ret;
1420 }
1421 
1422 
wpa_send_eapol_timeout(void * eloop_ctx,void * timeout_ctx)1423 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1424 {
1425 	struct wpa_authenticator *wpa_auth = eloop_ctx;
1426 	struct wpa_state_machine *sm = timeout_ctx;
1427 
1428 	sm->pending_1_of_4_timeout = 0;
1429 	wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1430 	sm->TimeoutEvt = TRUE;
1431 	wpa_sm_step(sm);
1432 }
1433 
1434 
__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)1435 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1436 		      struct wpa_state_machine *sm, int key_info,
1437 		      const u8 *key_rsc, const u8 *nonce,
1438 		      const u8 *kde, size_t kde_len,
1439 		      int keyidx, int encr, int force_version)
1440 {
1441 	struct ieee802_1x_hdr *hdr;
1442 	struct wpa_eapol_key *key;
1443 	size_t len, mic_len, keyhdrlen;
1444 	int alg;
1445 	int key_data_len, pad_len = 0;
1446 	u8 *buf, *pos;
1447 	int version, pairwise;
1448 	int i;
1449 	u8 *key_mic, *key_data;
1450 
1451 	mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
1452 	keyhdrlen = sizeof(*key) + mic_len + 2;
1453 
1454 	len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
1455 
1456 	if (force_version)
1457 		version = force_version;
1458 	else if (wpa_use_akm_defined(sm->wpa_key_mgmt))
1459 		version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
1460 	else if (wpa_use_cmac(sm->wpa_key_mgmt))
1461 		version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
1462 	else if (sm->pairwise != WPA_CIPHER_TKIP)
1463 		version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1464 	else
1465 		version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1466 
1467 	pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1468 
1469 	wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1470 		   "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1471 		   "encr=%d)",
1472 		   version,
1473 		   (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1474 		   (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1475 		   (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1476 		   (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1477 		   pairwise, (unsigned long) kde_len, keyidx, encr);
1478 
1479 	key_data_len = kde_len;
1480 
1481 	if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1482 	     wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
1483 	     version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1484 		pad_len = key_data_len % 8;
1485 		if (pad_len)
1486 			pad_len = 8 - pad_len;
1487 		key_data_len += pad_len + 8;
1488 	}
1489 
1490 	len += key_data_len;
1491 	if (!mic_len && encr)
1492 		len += AES_BLOCK_SIZE;
1493 
1494 	hdr = os_zalloc(len);
1495 	if (hdr == NULL)
1496 		return;
1497 	hdr->version = wpa_auth->conf.eapol_version;
1498 	hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1499 	hdr->length = host_to_be16(len  - sizeof(*hdr));
1500 	key = (struct wpa_eapol_key *) (hdr + 1);
1501 	key_mic = (u8 *) (key + 1);
1502 	key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
1503 
1504 	key->type = sm->wpa == WPA_VERSION_WPA2 ?
1505 		EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1506 	key_info |= version;
1507 	if (encr && sm->wpa == WPA_VERSION_WPA2)
1508 		key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1509 	if (sm->wpa != WPA_VERSION_WPA2)
1510 		key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1511 	WPA_PUT_BE16(key->key_info, key_info);
1512 
1513 	alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1514 	if (sm->wpa == WPA_VERSION_WPA2 && !pairwise)
1515 		WPA_PUT_BE16(key->key_length, 0);
1516 	else
1517 		WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
1518 
1519 	for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1520 		sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1521 		os_memcpy(sm->key_replay[i].counter,
1522 			  sm->key_replay[i - 1].counter,
1523 			  WPA_REPLAY_COUNTER_LEN);
1524 	}
1525 	inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1526 	os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1527 		  WPA_REPLAY_COUNTER_LEN);
1528 	wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1529 		    key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1530 	sm->key_replay[0].valid = TRUE;
1531 
1532 	if (nonce)
1533 		os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1534 
1535 	if (key_rsc)
1536 		os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1537 
1538 	if (kde && !encr) {
1539 		os_memcpy(key_data, kde, kde_len);
1540 		WPA_PUT_BE16(key_mic + mic_len, kde_len);
1541 #ifdef CONFIG_FILS
1542 	} else if (!mic_len && kde) {
1543 		const u8 *aad[1];
1544 		size_t aad_len[1];
1545 
1546 		WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
1547 		wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1548 				kde, kde_len);
1549 
1550 		wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
1551 				sm->PTK.kek, sm->PTK.kek_len);
1552 		/* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1553 		 * to Key Data (exclusive). */
1554 		aad[0] = (u8 *) hdr;
1555 		aad_len[0] = key_mic + 2 - (u8 *) hdr;
1556 		if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
1557 				    1, aad, aad_len, key_mic + 2) < 0) {
1558 			wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
1559 			return;
1560 		}
1561 
1562 		wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
1563 			    key_mic + 2, AES_BLOCK_SIZE + kde_len);
1564 #endif /* CONFIG_FILS */
1565 	} else if (encr && kde) {
1566 		buf = os_zalloc(key_data_len);
1567 		if (buf == NULL) {
1568 			os_free(hdr);
1569 			return;
1570 		}
1571 		pos = buf;
1572 		os_memcpy(pos, kde, kde_len);
1573 		pos += kde_len;
1574 
1575 		if (pad_len)
1576 			*pos++ = 0xdd;
1577 
1578 		wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1579 				buf, key_data_len);
1580 		if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1581 		    wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
1582 		    version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1583 			wpa_printf(MSG_DEBUG,
1584 				   "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1585 				   (unsigned int) sm->PTK.kek_len);
1586 			if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1587 				     (key_data_len - 8) / 8, buf, key_data)) {
1588 				os_free(hdr);
1589 				os_free(buf);
1590 				return;
1591 			}
1592 			WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1593 #ifndef CONFIG_NO_RC4
1594 		} else if (sm->PTK.kek_len == 16) {
1595 			u8 ek[32];
1596 
1597 			wpa_printf(MSG_DEBUG,
1598 				   "WPA: Encrypt Key Data using RC4");
1599 			os_memcpy(key->key_iv,
1600 				  sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1601 			inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1602 			os_memcpy(ek, key->key_iv, 16);
1603 			os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1604 			os_memcpy(key_data, buf, key_data_len);
1605 			rc4_skip(ek, 32, 256, key_data, key_data_len);
1606 			WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1607 #endif /* CONFIG_NO_RC4 */
1608 		} else {
1609 			os_free(hdr);
1610 			os_free(buf);
1611 			return;
1612 		}
1613 		os_free(buf);
1614 	}
1615 
1616 	if (key_info & WPA_KEY_INFO_MIC) {
1617 		if (!sm->PTK_valid || !mic_len) {
1618 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1619 					"PTK not valid when sending EAPOL-Key "
1620 					"frame");
1621 			os_free(hdr);
1622 			return;
1623 		}
1624 
1625 		if (wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1626 				      sm->wpa_key_mgmt, version,
1627 				      (u8 *) hdr, len, key_mic) < 0) {
1628 			os_free(hdr);
1629 			return;
1630 		}
1631 #ifdef CONFIG_TESTING_OPTIONS
1632 		if (!pairwise &&
1633 		    wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
1634 		    drand48() <
1635 		    wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1636 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1637 					"Corrupting group EAPOL-Key Key MIC");
1638 			key_mic[0]++;
1639 		}
1640 #endif /* CONFIG_TESTING_OPTIONS */
1641 	}
1642 
1643 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1644 			   1);
1645 	wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1646 			    sm->pairwise_set);
1647 	os_free(hdr);
1648 }
1649 
1650 
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)1651 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1652 			   struct wpa_state_machine *sm, int key_info,
1653 			   const u8 *key_rsc, const u8 *nonce,
1654 			   const u8 *kde, size_t kde_len,
1655 			   int keyidx, int encr)
1656 {
1657 	int timeout_ms;
1658 	int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1659 	u32 ctr;
1660 
1661 	if (sm == NULL)
1662 		return;
1663 
1664 	__wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1665 			 keyidx, encr, 0);
1666 
1667 	ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1668 	if (ctr == 1 && wpa_auth->conf.tx_status)
1669 		timeout_ms = pairwise ? eapol_key_timeout_first :
1670 			eapol_key_timeout_first_group;
1671 	else
1672 		timeout_ms = eapol_key_timeout_subseq;
1673 	if (wpa_auth->conf.wpa_disable_eapol_key_retries &&
1674 	    (!pairwise || (key_info & WPA_KEY_INFO_MIC)))
1675 		timeout_ms = eapol_key_timeout_no_retrans;
1676 	if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1677 		sm->pending_1_of_4_timeout = 1;
1678 #ifdef TEST_FUZZ
1679 	timeout_ms = 1;
1680 #endif /* TEST_FUZZ */
1681 	wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1682 		   "counter %u)", timeout_ms, ctr);
1683 	eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1684 			       wpa_send_eapol_timeout, wpa_auth, sm);
1685 }
1686 
1687 
wpa_verify_key_mic(int akmp,size_t pmk_len,struct wpa_ptk * PTK,u8 * data,size_t data_len)1688 static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
1689 			      u8 *data, size_t data_len)
1690 {
1691 	struct ieee802_1x_hdr *hdr;
1692 	struct wpa_eapol_key *key;
1693 	u16 key_info;
1694 	int ret = 0;
1695 	u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
1696 	size_t mic_len = wpa_mic_len(akmp, pmk_len);
1697 
1698 	if (data_len < sizeof(*hdr) + sizeof(*key))
1699 		return -1;
1700 
1701 	hdr = (struct ieee802_1x_hdr *) data;
1702 	key = (struct wpa_eapol_key *) (hdr + 1);
1703 	mic_pos = (u8 *) (key + 1);
1704 	key_info = WPA_GET_BE16(key->key_info);
1705 	os_memcpy(mic, mic_pos, mic_len);
1706 	os_memset(mic_pos, 0, mic_len);
1707 	if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1708 			      key_info & WPA_KEY_INFO_TYPE_MASK,
1709 			      data, data_len, mic_pos) ||
1710 	    os_memcmp_const(mic, mic_pos, mic_len) != 0)
1711 		ret = -1;
1712 	os_memcpy(mic_pos, mic, mic_len);
1713 	return ret;
1714 }
1715 
1716 
wpa_remove_ptk(struct wpa_state_machine * sm)1717 void wpa_remove_ptk(struct wpa_state_machine *sm)
1718 {
1719 	if (sm == NULL)
1720 		return;
1721 
1722 	sm->PTK_valid = FALSE;
1723 	os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1724 	if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
1725 			     0))
1726 		wpa_printf(MSG_DEBUG,
1727 			   "RSN: PTK removal from the driver failed");
1728 	sm->pairwise_set = FALSE;
1729 	eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1730 }
1731 
1732 
wpa_auth_sm_event(struct wpa_state_machine * sm,enum wpa_event event)1733 int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
1734 {
1735 	int remove_ptk = 1;
1736 
1737 	if (sm == NULL)
1738 		return -1;
1739 
1740 	wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1741 			 "event %d notification", event);
1742 
1743 	switch (event) {
1744 	case WPA_AUTH:
1745 #ifdef CONFIG_MESH
1746 		/* PTKs are derived through AMPE */
1747 		if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1748 			/* not mesh */
1749 			break;
1750 		}
1751 		return 0;
1752 #endif /* CONFIG_MESH */
1753 	case WPA_ASSOC:
1754 		break;
1755 	case WPA_DEAUTH:
1756 	case WPA_DISASSOC:
1757 		sm->DeauthenticationRequest = TRUE;
1758 #ifdef CONFIG_IEEE80211R_AP
1759 		os_memset(sm->PMK, 0, sizeof(sm->PMK));
1760 		sm->pmk_len = 0;
1761 		os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
1762 		sm->xxkey_len = 0;
1763 		os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
1764 		sm->pmk_r1_len = 0;
1765 #endif /* CONFIG_IEEE80211R_AP */
1766 		break;
1767 	case WPA_REAUTH:
1768 	case WPA_REAUTH_EAPOL:
1769 		if (!sm->started) {
1770 			/*
1771 			 * When using WPS, we may end up here if the STA
1772 			 * manages to re-associate without the previous STA
1773 			 * entry getting removed. Consequently, we need to make
1774 			 * sure that the WPA state machines gets initialized
1775 			 * properly at this point.
1776 			 */
1777 			wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1778 				   "started - initialize now");
1779 			sm->started = 1;
1780 			sm->Init = TRUE;
1781 			if (wpa_sm_step(sm) == 1)
1782 				return 1; /* should not really happen */
1783 			sm->Init = FALSE;
1784 			sm->AuthenticationRequest = TRUE;
1785 			break;
1786 		}
1787 		if (sm->GUpdateStationKeys) {
1788 			/*
1789 			 * Reauthentication cancels the pending group key
1790 			 * update for this STA.
1791 			 */
1792 			sm->group->GKeyDoneStations--;
1793 			sm->GUpdateStationKeys = FALSE;
1794 			sm->PtkGroupInit = TRUE;
1795 		}
1796 		sm->ReAuthenticationRequest = TRUE;
1797 		break;
1798 	case WPA_ASSOC_FT:
1799 #ifdef CONFIG_IEEE80211R_AP
1800 		wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1801 			   "after association");
1802 		wpa_ft_install_ptk(sm);
1803 
1804 		/* Using FT protocol, not WPA auth state machine */
1805 		sm->ft_completed = 1;
1806 		wpa_auth_set_ptk_rekey_timer(sm);
1807 		return 0;
1808 #else /* CONFIG_IEEE80211R_AP */
1809 		break;
1810 #endif /* CONFIG_IEEE80211R_AP */
1811 	case WPA_ASSOC_FILS:
1812 #ifdef CONFIG_FILS
1813 		wpa_printf(MSG_DEBUG,
1814 			   "FILS: TK configuration after association");
1815 		fils_set_tk(sm);
1816 		sm->fils_completed = 1;
1817 		return 0;
1818 #else /* CONFIG_FILS */
1819 		break;
1820 #endif /* CONFIG_FILS */
1821 	case WPA_DRV_STA_REMOVED:
1822 		sm->tk_already_set = FALSE;
1823 		return 0;
1824 	}
1825 
1826 #ifdef CONFIG_IEEE80211R_AP
1827 	sm->ft_completed = 0;
1828 #endif /* CONFIG_IEEE80211R_AP */
1829 
1830 #ifdef CONFIG_IEEE80211W
1831 	if (sm->mgmt_frame_prot && event == WPA_AUTH)
1832 		remove_ptk = 0;
1833 #endif /* CONFIG_IEEE80211W */
1834 #ifdef CONFIG_FILS
1835 	if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1836 	    (event == WPA_AUTH || event == WPA_ASSOC))
1837 		remove_ptk = 0;
1838 #endif /* CONFIG_FILS */
1839 
1840 	if (remove_ptk) {
1841 		sm->PTK_valid = FALSE;
1842 		os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1843 
1844 		if (event != WPA_REAUTH_EAPOL)
1845 			wpa_remove_ptk(sm);
1846 	}
1847 
1848 	if (sm->in_step_loop) {
1849 		/*
1850 		 * wpa_sm_step() is already running - avoid recursive call to
1851 		 * it by making the existing loop process the new update.
1852 		 */
1853 		sm->changed = TRUE;
1854 		return 0;
1855 	}
1856 	return wpa_sm_step(sm);
1857 }
1858 
1859 
SM_STATE(WPA_PTK,INITIALIZE)1860 SM_STATE(WPA_PTK, INITIALIZE)
1861 {
1862 	SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1863 	if (sm->Init) {
1864 		/* Init flag is not cleared here, so avoid busy
1865 		 * loop by claiming nothing changed. */
1866 		sm->changed = FALSE;
1867 	}
1868 
1869 	sm->keycount = 0;
1870 	if (sm->GUpdateStationKeys)
1871 		sm->group->GKeyDoneStations--;
1872 	sm->GUpdateStationKeys = FALSE;
1873 	if (sm->wpa == WPA_VERSION_WPA)
1874 		sm->PInitAKeys = FALSE;
1875 	if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1876 	       * Local AA > Remote AA)) */) {
1877 		sm->Pair = TRUE;
1878 	}
1879 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1880 	wpa_remove_ptk(sm);
1881 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1882 	sm->TimeoutCtr = 0;
1883 	if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
1884 	    sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
1885 	    sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
1886 		wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1887 				   WPA_EAPOL_authorized, 0);
1888 	}
1889 }
1890 
1891 
SM_STATE(WPA_PTK,DISCONNECT)1892 SM_STATE(WPA_PTK, DISCONNECT)
1893 {
1894 	u16 reason = sm->disconnect_reason;
1895 
1896 	SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1897 	sm->Disconnect = FALSE;
1898 	sm->disconnect_reason = 0;
1899 	if (!reason)
1900 		reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1901 	wpa_sta_disconnect(sm->wpa_auth, sm->addr, reason);
1902 }
1903 
1904 
SM_STATE(WPA_PTK,DISCONNECTED)1905 SM_STATE(WPA_PTK, DISCONNECTED)
1906 {
1907 	SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1908 	sm->DeauthenticationRequest = FALSE;
1909 }
1910 
1911 
SM_STATE(WPA_PTK,AUTHENTICATION)1912 SM_STATE(WPA_PTK, AUTHENTICATION)
1913 {
1914 	SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1915 	os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1916 	sm->PTK_valid = FALSE;
1917 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1918 			   1);
1919 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1920 	sm->AuthenticationRequest = FALSE;
1921 }
1922 
1923 
wpa_group_ensure_init(struct wpa_authenticator * wpa_auth,struct wpa_group * group)1924 static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1925 				  struct wpa_group *group)
1926 {
1927 	if (group->first_sta_seen)
1928 		return;
1929 	/*
1930 	 * System has run bit further than at the time hostapd was started
1931 	 * potentially very early during boot up. This provides better chances
1932 	 * of collecting more randomness on embedded systems. Re-initialize the
1933 	 * GMK and Counter here to improve their strength if there was not
1934 	 * enough entropy available immediately after system startup.
1935 	 */
1936 	wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1937 		   "station");
1938 	if (random_pool_ready() != 1) {
1939 		wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1940 			   "to proceed - reject first 4-way handshake");
1941 		group->reject_4way_hs_for_entropy = TRUE;
1942 	} else {
1943 		group->first_sta_seen = TRUE;
1944 		group->reject_4way_hs_for_entropy = FALSE;
1945 	}
1946 
1947 	if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
1948 	    wpa_gtk_update(wpa_auth, group) < 0 ||
1949 	    wpa_group_config_group_keys(wpa_auth, group) < 0) {
1950 		wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
1951 		group->first_sta_seen = FALSE;
1952 		group->reject_4way_hs_for_entropy = TRUE;
1953 	}
1954 }
1955 
1956 
SM_STATE(WPA_PTK,AUTHENTICATION2)1957 SM_STATE(WPA_PTK, AUTHENTICATION2)
1958 {
1959 	SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1960 
1961 	wpa_group_ensure_init(sm->wpa_auth, sm->group);
1962 	sm->ReAuthenticationRequest = FALSE;
1963 
1964 	/*
1965 	 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1966 	 * ambiguous. The Authenticator state machine uses a counter that is
1967 	 * incremented by one for each 4-way handshake. However, the security
1968 	 * analysis of 4-way handshake points out that unpredictable nonces
1969 	 * help in preventing precomputation attacks. Instead of the state
1970 	 * machine definition, use an unpredictable nonce value here to provide
1971 	 * stronger protection against potential precomputation attacks.
1972 	 */
1973 	if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1974 		wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1975 			   "ANonce.");
1976 		sm->Disconnect = TRUE;
1977 		return;
1978 	}
1979 	wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1980 		    WPA_NONCE_LEN);
1981 	/* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1982 	 * logical place than INITIALIZE since AUTHENTICATION2 can be
1983 	 * re-entered on ReAuthenticationRequest without going through
1984 	 * INITIALIZE. */
1985 	sm->TimeoutCtr = 0;
1986 }
1987 
1988 
wpa_auth_sm_ptk_update(struct wpa_state_machine * sm)1989 static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm)
1990 {
1991 	if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1992 		wpa_printf(MSG_ERROR,
1993 			   "WPA: Failed to get random data for ANonce");
1994 		sm->Disconnect = TRUE;
1995 		return -1;
1996 	}
1997 	wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce,
1998 		    WPA_NONCE_LEN);
1999 	sm->TimeoutCtr = 0;
2000 	return 0;
2001 }
2002 
2003 
SM_STATE(WPA_PTK,INITPMK)2004 SM_STATE(WPA_PTK, INITPMK)
2005 {
2006 	u8 msk[2 * PMK_LEN];
2007 	size_t len = 2 * PMK_LEN;
2008 
2009 	SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
2010 #ifdef CONFIG_IEEE80211R_AP
2011 	sm->xxkey_len = 0;
2012 #endif /* CONFIG_IEEE80211R_AP */
2013 	if (sm->pmksa) {
2014 		wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
2015 		os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2016 		sm->pmk_len = sm->pmksa->pmk_len;
2017 #ifdef CONFIG_DPP
2018 	} else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) {
2019 		wpa_printf(MSG_DEBUG,
2020 			   "DPP: No PMKSA cache entry for STA - reject connection");
2021 		sm->Disconnect = TRUE;
2022 		sm->disconnect_reason = WLAN_REASON_INVALID_PMKID;
2023 		return;
2024 #endif /* CONFIG_DPP */
2025 	} else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
2026 		unsigned int pmk_len;
2027 
2028 		if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
2029 			pmk_len = PMK_LEN_SUITE_B_192;
2030 		else
2031 			pmk_len = PMK_LEN;
2032 		wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
2033 			   "(MSK len=%lu PMK len=%u)", (unsigned long) len,
2034 			   pmk_len);
2035 		if (len < pmk_len) {
2036 			wpa_printf(MSG_DEBUG,
2037 				   "WPA: MSK not long enough (%u) to create PMK (%u)",
2038 				   (unsigned int) len, (unsigned int) pmk_len);
2039 			sm->Disconnect = TRUE;
2040 			return;
2041 		}
2042 		os_memcpy(sm->PMK, msk, pmk_len);
2043 		sm->pmk_len = pmk_len;
2044 #ifdef CONFIG_IEEE80211R_AP
2045 		if (len >= 2 * PMK_LEN) {
2046 			if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
2047 				os_memcpy(sm->xxkey, msk, SHA384_MAC_LEN);
2048 				sm->xxkey_len = SHA384_MAC_LEN;
2049 			} else {
2050 				os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
2051 				sm->xxkey_len = PMK_LEN;
2052 			}
2053 		}
2054 #endif /* CONFIG_IEEE80211R_AP */
2055 	} else {
2056 		wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
2057 			   sm->wpa_auth->cb->get_msk);
2058 		sm->Disconnect = TRUE;
2059 		return;
2060 	}
2061 	forced_memzero(msk, sizeof(msk));
2062 
2063 	sm->req_replay_counter_used = 0;
2064 	/* IEEE 802.11i does not set keyRun to FALSE, but not doing this
2065 	 * will break reauthentication since EAPOL state machines may not be
2066 	 * get into AUTHENTICATING state that clears keyRun before WPA state
2067 	 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2068 	 * state and takes PMK from the previously used AAA Key. This will
2069 	 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2070 	 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2071 	 * be good workaround for this issue. */
2072 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
2073 }
2074 
2075 
SM_STATE(WPA_PTK,INITPSK)2076 SM_STATE(WPA_PTK, INITPSK)
2077 {
2078 	const u8 *psk;
2079 	size_t psk_len;
2080 
2081 	SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
2082 	psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL,
2083 			       &psk_len, NULL);
2084 	if (psk) {
2085 		os_memcpy(sm->PMK, psk, psk_len);
2086 		sm->pmk_len = psk_len;
2087 #ifdef CONFIG_IEEE80211R_AP
2088 		os_memcpy(sm->xxkey, psk, PMK_LEN);
2089 		sm->xxkey_len = PMK_LEN;
2090 #endif /* CONFIG_IEEE80211R_AP */
2091 	}
2092 #ifdef CONFIG_SAE
2093 	if (wpa_auth_uses_sae(sm) && sm->pmksa) {
2094 		wpa_printf(MSG_DEBUG, "SAE: PMK from PMKSA cache");
2095 		os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2096 		sm->pmk_len = sm->pmksa->pmk_len;
2097 #ifdef CONFIG_IEEE80211R_AP
2098 		os_memcpy(sm->xxkey, sm->pmksa->pmk, sm->pmksa->pmk_len);
2099 		sm->xxkey_len = sm->pmksa->pmk_len;
2100 #endif /* CONFIG_IEEE80211R_AP */
2101 	}
2102 #endif /* CONFIG_SAE */
2103 	sm->req_replay_counter_used = 0;
2104 }
2105 
2106 
SM_STATE(WPA_PTK,PTKSTART)2107 SM_STATE(WPA_PTK, PTKSTART)
2108 {
2109 	u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
2110 	size_t pmkid_len = 0;
2111 
2112 	SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
2113 	sm->PTKRequest = FALSE;
2114 	sm->TimeoutEvt = FALSE;
2115 	sm->alt_snonce_valid = FALSE;
2116 
2117 	sm->TimeoutCtr++;
2118 	if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
2119 		/* No point in sending the EAPOL-Key - we will disconnect
2120 		 * immediately following this. */
2121 		return;
2122 	}
2123 
2124 	wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2125 			"sending 1/4 msg of 4-Way Handshake");
2126 	/*
2127 	 * For infrastructure BSS cases, it is better for the AP not to include
2128 	 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2129 	 * offline search for the passphrase/PSK without having to be able to
2130 	 * capture a 4-way handshake from a STA that has access to the network.
2131 	 *
2132 	 * For IBSS cases, addition of PMKID KDE could be considered even with
2133 	 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2134 	 * possible PSK for this STA. However, this should not be done unless
2135 	 * there is support for using that information on the supplicant side.
2136 	 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2137 	 * cases would also apply here, but at least in the IBSS case, this
2138 	 * would cover a potential real use case.
2139 	 */
2140 	if (sm->wpa == WPA_VERSION_WPA2 &&
2141 	    (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) ||
2142 	     (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && sm->pmksa) ||
2143 	     wpa_key_mgmt_sae(sm->wpa_key_mgmt)) &&
2144 	    sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
2145 		pmkid = buf;
2146 		pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2147 		pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
2148 		pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2149 		RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
2150 		if (sm->pmksa) {
2151 			wpa_hexdump(MSG_DEBUG,
2152 				    "RSN: Message 1/4 PMKID from PMKSA entry",
2153 				    sm->pmksa->pmkid, PMKID_LEN);
2154 			os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2155 				  sm->pmksa->pmkid, PMKID_LEN);
2156 		} else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
2157 			/* No KCK available to derive PMKID */
2158 			wpa_printf(MSG_DEBUG,
2159 				   "RSN: No KCK available to derive PMKID for message 1/4");
2160 			pmkid = NULL;
2161 #ifdef CONFIG_FILS
2162 		} else if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2163 			if (sm->pmkid_set) {
2164 				wpa_hexdump(MSG_DEBUG,
2165 					    "RSN: Message 1/4 PMKID from FILS/ERP",
2166 					    sm->pmkid, PMKID_LEN);
2167 				os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2168 					  sm->pmkid, PMKID_LEN);
2169 			} else {
2170 				/* No PMKID available */
2171 				wpa_printf(MSG_DEBUG,
2172 					   "RSN: No FILS/ERP PMKID available for message 1/4");
2173 				pmkid = NULL;
2174 			}
2175 #endif /* CONFIG_FILS */
2176 #ifdef CONFIG_IEEE80211R_AP
2177 		} else if (wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
2178 			   sm->ft_completed) {
2179 			wpa_printf(MSG_DEBUG,
2180 				   "FT: No PMKID in message 1/4 when using FT protocol");
2181 			pmkid = NULL;
2182 			pmkid_len = 0;
2183 #endif /* CONFIG_IEEE80211R_AP */
2184 #ifdef CONFIG_SAE
2185 		} else if (wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
2186 			if (sm->pmkid_set) {
2187 				wpa_hexdump(MSG_DEBUG,
2188 					    "RSN: Message 1/4 PMKID from SAE",
2189 					    sm->pmkid, PMKID_LEN);
2190 				os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2191 					  sm->pmkid, PMKID_LEN);
2192 			} else {
2193 				/* No PMKID available */
2194 				wpa_printf(MSG_DEBUG,
2195 					   "RSN: No SAE PMKID available for message 1/4");
2196 				pmkid = NULL;
2197 			}
2198 #endif /* CONFIG_SAE */
2199 		} else {
2200 			/*
2201 			 * Calculate PMKID since no PMKSA cache entry was
2202 			 * available with pre-calculated PMKID.
2203 			 */
2204 			rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
2205 				  sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
2206 				  sm->wpa_key_mgmt);
2207 			wpa_hexdump(MSG_DEBUG,
2208 				    "RSN: Message 1/4 PMKID derived from PMK",
2209 				    &pmkid[2 + RSN_SELECTOR_LEN], PMKID_LEN);
2210 		}
2211 	}
2212 	wpa_send_eapol(sm->wpa_auth, sm,
2213 		       WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
2214 		       sm->ANonce, pmkid, pmkid_len, 0, 0);
2215 }
2216 
2217 
wpa_derive_ptk(struct wpa_state_machine * sm,const u8 * snonce,const u8 * pmk,unsigned int pmk_len,struct wpa_ptk * ptk)2218 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
2219 			  const u8 *pmk, unsigned int pmk_len,
2220 			  struct wpa_ptk *ptk)
2221 {
2222 	const u8 *z = NULL;
2223 	size_t z_len = 0;
2224 
2225 #ifdef CONFIG_IEEE80211R_AP
2226 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2227 		if (sm->ft_completed) {
2228 			u8 ptk_name[WPA_PMK_NAME_LEN];
2229 
2230 			return wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len,
2231 						 sm->SNonce, sm->ANonce,
2232 						 sm->addr, sm->wpa_auth->addr,
2233 						 sm->pmk_r1_name,
2234 						 ptk, ptk_name,
2235 						 sm->wpa_key_mgmt,
2236 						 sm->pairwise);
2237 		}
2238 		return wpa_auth_derive_ptk_ft(sm, ptk);
2239 	}
2240 #endif /* CONFIG_IEEE80211R_AP */
2241 
2242 #ifdef CONFIG_DPP2
2243 	if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
2244 		z = wpabuf_head(sm->dpp_z);
2245 		z_len = wpabuf_len(sm->dpp_z);
2246 	}
2247 #endif /* CONFIG_DPP2 */
2248 
2249 	return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
2250 			      sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
2251 			      ptk, sm->wpa_key_mgmt, sm->pairwise, z, z_len);
2252 }
2253 
2254 
2255 #ifdef CONFIG_FILS
2256 
fils_auth_pmk_to_ptk(struct wpa_state_machine * sm,const u8 * pmk,size_t pmk_len,const u8 * snonce,const u8 * anonce,const u8 * dhss,size_t dhss_len,struct wpabuf * g_sta,struct wpabuf * g_ap)2257 int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
2258 			 size_t pmk_len, const u8 *snonce, const u8 *anonce,
2259 			 const u8 *dhss, size_t dhss_len,
2260 			 struct wpabuf *g_sta, struct wpabuf *g_ap)
2261 {
2262 	u8 ick[FILS_ICK_MAX_LEN];
2263 	size_t ick_len;
2264 	int res;
2265 	u8 fils_ft[FILS_FT_MAX_LEN];
2266 	size_t fils_ft_len = 0;
2267 
2268 	res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr,
2269 			      snonce, anonce, dhss, dhss_len,
2270 			      &sm->PTK, ick, &ick_len,
2271 			      sm->wpa_key_mgmt, sm->pairwise,
2272 			      fils_ft, &fils_ft_len);
2273 	if (res < 0)
2274 		return res;
2275 	sm->PTK_valid = TRUE;
2276 	sm->tk_already_set = FALSE;
2277 
2278 #ifdef CONFIG_IEEE80211R_AP
2279 	if (fils_ft_len) {
2280 		struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2281 		struct wpa_auth_config *conf = &wpa_auth->conf;
2282 		u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
2283 		int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
2284 		size_t pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
2285 
2286 		if (wpa_derive_pmk_r0(fils_ft, fils_ft_len,
2287 				      conf->ssid, conf->ssid_len,
2288 				      conf->mobility_domain,
2289 				      conf->r0_key_holder,
2290 				      conf->r0_key_holder_len,
2291 				      sm->addr, pmk_r0, pmk_r0_name,
2292 				      use_sha384) < 0)
2293 			return -1;
2294 
2295 		wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
2296 				pmk_r0, pmk_r0_len);
2297 		wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
2298 			    pmk_r0_name, WPA_PMK_NAME_LEN);
2299 		wpa_ft_store_pmk_fils(sm, pmk_r0, pmk_r0_name);
2300 		forced_memzero(fils_ft, sizeof(fils_ft));
2301 
2302 		res = wpa_derive_pmk_r1_name(pmk_r0_name, conf->r1_key_holder,
2303 					     sm->addr, sm->pmk_r1_name,
2304 					     use_sha384);
2305 		forced_memzero(pmk_r0, PMK_LEN_MAX);
2306 		if (res < 0)
2307 			return -1;
2308 		wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name,
2309 			    WPA_PMK_NAME_LEN);
2310 		sm->pmk_r1_name_valid = 1;
2311 	}
2312 #endif /* CONFIG_IEEE80211R_AP */
2313 
2314 	res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
2315 			       sm->addr, sm->wpa_auth->addr,
2316 			       g_sta ? wpabuf_head(g_sta) : NULL,
2317 			       g_sta ? wpabuf_len(g_sta) : 0,
2318 			       g_ap ? wpabuf_head(g_ap) : NULL,
2319 			       g_ap ? wpabuf_len(g_ap) : 0,
2320 			       sm->wpa_key_mgmt, sm->fils_key_auth_sta,
2321 			       sm->fils_key_auth_ap,
2322 			       &sm->fils_key_auth_len);
2323 	forced_memzero(ick, sizeof(ick));
2324 
2325 	/* Store nonces for (Re)Association Request/Response frame processing */
2326 	os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
2327 	os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
2328 
2329 	return res;
2330 }
2331 
2332 
wpa_aead_decrypt(struct wpa_state_machine * sm,struct wpa_ptk * ptk,u8 * buf,size_t buf_len,u16 * _key_data_len)2333 static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2334 			    u8 *buf, size_t buf_len, u16 *_key_data_len)
2335 {
2336 	struct ieee802_1x_hdr *hdr;
2337 	struct wpa_eapol_key *key;
2338 	u8 *pos;
2339 	u16 key_data_len;
2340 	u8 *tmp;
2341 	const u8 *aad[1];
2342 	size_t aad_len[1];
2343 
2344 	hdr = (struct ieee802_1x_hdr *) buf;
2345 	key = (struct wpa_eapol_key *) (hdr + 1);
2346 	pos = (u8 *) (key + 1);
2347 	key_data_len = WPA_GET_BE16(pos);
2348 	if (key_data_len < AES_BLOCK_SIZE ||
2349 	    key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
2350 		wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2351 				"No room for AES-SIV data in the frame");
2352 		return -1;
2353 	}
2354 	pos += 2; /* Pointing at the Encrypted Key Data field */
2355 
2356 	tmp = os_malloc(key_data_len);
2357 	if (!tmp)
2358 		return -1;
2359 
2360 	/* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2361 	 * to Key Data (exclusive). */
2362 	aad[0] = buf;
2363 	aad_len[0] = pos - buf;
2364 	if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
2365 			    1, aad, aad_len, tmp) < 0) {
2366 		wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2367 				"Invalid AES-SIV data in the frame");
2368 		bin_clear_free(tmp, key_data_len);
2369 		return -1;
2370 	}
2371 
2372 	/* AEAD decryption and validation completed successfully */
2373 	key_data_len -= AES_BLOCK_SIZE;
2374 	wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2375 			tmp, key_data_len);
2376 
2377 	/* Replace Key Data field with the decrypted version */
2378 	os_memcpy(pos, tmp, key_data_len);
2379 	pos -= 2; /* Key Data Length field */
2380 	WPA_PUT_BE16(pos, key_data_len);
2381 	bin_clear_free(tmp, key_data_len);
2382 	if (_key_data_len)
2383 		*_key_data_len = key_data_len;
2384 	return 0;
2385 }
2386 
2387 
wpa_fils_validate_fils_session(struct wpa_state_machine * sm,const u8 * ies,size_t ies_len,const u8 * fils_session)2388 const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm,
2389 					  const u8 *ies, size_t ies_len,
2390 					  const u8 *fils_session)
2391 {
2392 	const u8 *ie, *end;
2393 	const u8 *session = NULL;
2394 
2395 	if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2396 		wpa_printf(MSG_DEBUG,
2397 			   "FILS: Not a FILS AKM - reject association");
2398 		return NULL;
2399 	}
2400 
2401 	/* Verify Session element */
2402 	ie = ies;
2403 	end = ((const u8 *) ie) + ies_len;
2404 	while (ie + 1 < end) {
2405 		if (ie + 2 + ie[1] > end)
2406 			break;
2407 		if (ie[0] == WLAN_EID_EXTENSION &&
2408 		    ie[1] >= 1 + FILS_SESSION_LEN &&
2409 		    ie[2] == WLAN_EID_EXT_FILS_SESSION) {
2410 			session = ie;
2411 			break;
2412 		}
2413 		ie += 2 + ie[1];
2414 	}
2415 
2416 	if (!session) {
2417 		wpa_printf(MSG_DEBUG,
2418 			   "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2419 			   __func__);
2420 		return NULL;
2421 	}
2422 
2423 	if (!fils_session) {
2424 		wpa_printf(MSG_DEBUG,
2425 			   "FILS: %s: Could not find FILS Session element in STA entry - reject",
2426 			   __func__);
2427 		return NULL;
2428 	}
2429 
2430 	if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
2431 		wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
2432 		wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
2433 			    fils_session, FILS_SESSION_LEN);
2434 		wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
2435 			    session + 3, FILS_SESSION_LEN);
2436 		return NULL;
2437 	}
2438 	return session;
2439 }
2440 
2441 
wpa_fils_validate_key_confirm(struct wpa_state_machine * sm,const u8 * ies,size_t ies_len)2442 int wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies,
2443 				  size_t ies_len)
2444 {
2445 	struct ieee802_11_elems elems;
2446 
2447 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
2448 		wpa_printf(MSG_DEBUG,
2449 			   "FILS: Failed to parse decrypted elements");
2450 		return -1;
2451 	}
2452 
2453 	if (!elems.fils_session) {
2454 		wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
2455 		return -1;
2456 	}
2457 
2458 	if (!elems.fils_key_confirm) {
2459 		wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
2460 		return -1;
2461 	}
2462 
2463 	if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
2464 		wpa_printf(MSG_DEBUG,
2465 			   "FILS: Unexpected Key-Auth length %d (expected %d)",
2466 			   elems.fils_key_confirm_len,
2467 			   (int) sm->fils_key_auth_len);
2468 		return -1;
2469 	}
2470 
2471 	if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
2472 		      sm->fils_key_auth_len) != 0) {
2473 		wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
2474 		wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
2475 			    elems.fils_key_confirm, elems.fils_key_confirm_len);
2476 		wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
2477 			    sm->fils_key_auth_sta, sm->fils_key_auth_len);
2478 		return -1;
2479 	}
2480 
2481 	return 0;
2482 }
2483 
2484 
fils_decrypt_assoc(struct wpa_state_machine * sm,const u8 * fils_session,const struct ieee80211_mgmt * mgmt,size_t frame_len,u8 * pos,size_t left)2485 int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
2486 		       const struct ieee80211_mgmt *mgmt, size_t frame_len,
2487 		       u8 *pos, size_t left)
2488 {
2489 	u16 fc, stype;
2490 	const u8 *end, *ie_start, *ie, *session, *crypt;
2491 	const u8 *aad[5];
2492 	size_t aad_len[5];
2493 
2494 	if (!sm || !sm->PTK_valid) {
2495 		wpa_printf(MSG_DEBUG,
2496 			   "FILS: No KEK to decrypt Assocication Request frame");
2497 		return -1;
2498 	}
2499 
2500 	if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2501 		wpa_printf(MSG_DEBUG,
2502 			   "FILS: Not a FILS AKM - reject association");
2503 		return -1;
2504 	}
2505 
2506 	end = ((const u8 *) mgmt) + frame_len;
2507 	fc = le_to_host16(mgmt->frame_control);
2508 	stype = WLAN_FC_GET_STYPE(fc);
2509 	if (stype == WLAN_FC_STYPE_REASSOC_REQ)
2510 		ie_start = mgmt->u.reassoc_req.variable;
2511 	else
2512 		ie_start = mgmt->u.assoc_req.variable;
2513 	ie = ie_start;
2514 
2515 	/*
2516 	 * Find FILS Session element which is the last unencrypted element in
2517 	 * the frame.
2518 	 */
2519 	session = wpa_fils_validate_fils_session(sm, ie, end - ie,
2520 						 fils_session);
2521 	if (!session) {
2522 		wpa_printf(MSG_DEBUG, "FILS: Session validation failed");
2523 		return -1;
2524 	}
2525 
2526 	crypt = session + 2 + session[1];
2527 
2528 	if (end - crypt < AES_BLOCK_SIZE) {
2529 		wpa_printf(MSG_DEBUG,
2530 			   "FILS: Too short frame to include AES-SIV data");
2531 		return -1;
2532 	}
2533 
2534 	/* AES-SIV AAD vectors */
2535 
2536 	/* The STA's MAC address */
2537 	aad[0] = mgmt->sa;
2538 	aad_len[0] = ETH_ALEN;
2539 	/* The AP's BSSID */
2540 	aad[1] = mgmt->da;
2541 	aad_len[1] = ETH_ALEN;
2542 	/* The STA's nonce */
2543 	aad[2] = sm->SNonce;
2544 	aad_len[2] = FILS_NONCE_LEN;
2545 	/* The AP's nonce */
2546 	aad[3] = sm->ANonce;
2547 	aad_len[3] = FILS_NONCE_LEN;
2548 	/*
2549 	 * The (Re)Association Request frame from the Capability Information
2550 	 * field to the FILS Session element (both inclusive).
2551 	 */
2552 	aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
2553 	aad_len[4] = crypt - aad[4];
2554 
2555 	if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
2556 			    5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
2557 		wpa_printf(MSG_DEBUG,
2558 			   "FILS: Invalid AES-SIV data in the frame");
2559 		return -1;
2560 	}
2561 	wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
2562 		    pos, left - AES_BLOCK_SIZE);
2563 
2564 	if (wpa_fils_validate_key_confirm(sm, pos, left - AES_BLOCK_SIZE) < 0) {
2565 		wpa_printf(MSG_DEBUG, "FILS: Key Confirm validation failed");
2566 		return -1;
2567 	}
2568 
2569 	return left - AES_BLOCK_SIZE;
2570 }
2571 
2572 
fils_encrypt_assoc(struct wpa_state_machine * sm,u8 * buf,size_t current_len,size_t max_len,const struct wpabuf * hlp)2573 int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
2574 		       size_t current_len, size_t max_len,
2575 		       const struct wpabuf *hlp)
2576 {
2577 	u8 *end = buf + max_len;
2578 	u8 *pos = buf + current_len;
2579 	struct ieee80211_mgmt *mgmt;
2580 	struct wpabuf *plain;
2581 	const u8 *aad[5];
2582 	size_t aad_len[5];
2583 
2584 	if (!sm || !sm->PTK_valid)
2585 		return -1;
2586 
2587 	wpa_hexdump(MSG_DEBUG,
2588 		    "FILS: Association Response frame before FILS processing",
2589 		    buf, current_len);
2590 
2591 	mgmt = (struct ieee80211_mgmt *) buf;
2592 
2593 	/* AES-SIV AAD vectors */
2594 
2595 	/* The AP's BSSID */
2596 	aad[0] = mgmt->sa;
2597 	aad_len[0] = ETH_ALEN;
2598 	/* The STA's MAC address */
2599 	aad[1] = mgmt->da;
2600 	aad_len[1] = ETH_ALEN;
2601 	/* The AP's nonce */
2602 	aad[2] = sm->ANonce;
2603 	aad_len[2] = FILS_NONCE_LEN;
2604 	/* The STA's nonce */
2605 	aad[3] = sm->SNonce;
2606 	aad_len[3] = FILS_NONCE_LEN;
2607 	/*
2608 	 * The (Re)Association Response frame from the Capability Information
2609 	 * field (the same offset in both Association and Reassociation
2610 	 * Response frames) to the FILS Session element (both inclusive).
2611 	 */
2612 	aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
2613 	aad_len[4] = pos - aad[4];
2614 
2615 	/* The following elements will be encrypted with AES-SIV */
2616 	plain = fils_prepare_plainbuf(sm, hlp);
2617 	if (!plain) {
2618 		wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2619 		return -1;
2620 	}
2621 
2622 	if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
2623 		wpa_printf(MSG_DEBUG,
2624 			   "FILS: Not enough room for FILS elements");
2625 		wpabuf_clear_free(plain);
2626 		return -1;
2627 	}
2628 
2629 	wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
2630 			    plain);
2631 
2632 	if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
2633 			    wpabuf_head(plain), wpabuf_len(plain),
2634 			    5, aad, aad_len, pos) < 0) {
2635 		wpabuf_clear_free(plain);
2636 		return -1;
2637 	}
2638 
2639 	wpa_hexdump(MSG_DEBUG,
2640 		    "FILS: Encrypted Association Response elements",
2641 		    pos, AES_BLOCK_SIZE + wpabuf_len(plain));
2642 	current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
2643 	wpabuf_clear_free(plain);
2644 
2645 	sm->fils_completed = 1;
2646 
2647 	return current_len;
2648 }
2649 
2650 
fils_prepare_plainbuf(struct wpa_state_machine * sm,const struct wpabuf * hlp)2651 static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
2652 					     const struct wpabuf *hlp)
2653 {
2654 	struct wpabuf *plain;
2655 	u8 *len, *tmp, *tmp2;
2656 	u8 hdr[2];
2657 	u8 *gtk, dummy_gtk[32];
2658 	size_t gtk_len;
2659 	struct wpa_group *gsm;
2660 
2661 	plain = wpabuf_alloc(1000);
2662 	if (!plain)
2663 		return NULL;
2664 
2665 	/* TODO: FILS Public Key */
2666 
2667 	/* FILS Key Confirmation */
2668 	wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2669 	wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
2670 	/* Element ID Extension */
2671 	wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
2672 	wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
2673 
2674 	/* FILS HLP Container */
2675 	if (hlp)
2676 		wpabuf_put_buf(plain, hlp);
2677 
2678 	/* TODO: FILS IP Address Assignment */
2679 
2680 	/* Key Delivery */
2681 	gsm = sm->group;
2682 	wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2683 	len = wpabuf_put(plain, 1);
2684 	wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
2685 	wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
2686 			    wpabuf_put(plain, WPA_KEY_RSC_LEN));
2687 	/* GTK KDE */
2688 	gtk = gsm->GTK[gsm->GN - 1];
2689 	gtk_len = gsm->GTK_len;
2690 	if (sm->wpa_auth->conf.disable_gtk ||
2691 	    sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
2692 		/*
2693 		 * Provide unique random GTK to each STA to prevent use
2694 		 * of GTK in the BSS.
2695 		 */
2696 		if (random_get_bytes(dummy_gtk, gtk_len) < 0) {
2697 			wpabuf_clear_free(plain);
2698 			return NULL;
2699 		}
2700 		gtk = dummy_gtk;
2701 	}
2702 	hdr[0] = gsm->GN & 0x03;
2703 	hdr[1] = 0;
2704 	tmp = wpabuf_put(plain, 0);
2705 	tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2706 			   gtk, gtk_len);
2707 	wpabuf_put(plain, tmp2 - tmp);
2708 
2709 	/* IGTK KDE */
2710 	tmp = wpabuf_put(plain, 0);
2711 	tmp2 = ieee80211w_kde_add(sm, tmp);
2712 	wpabuf_put(plain, tmp2 - tmp);
2713 
2714 	*len = (u8 *) wpabuf_put(plain, 0) - len - 1;
2715 
2716 #ifdef CONFIG_OCV
2717 	if (wpa_auth_uses_ocv(sm)) {
2718 		struct wpa_channel_info ci;
2719 		u8 *pos;
2720 
2721 		if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
2722 			wpa_printf(MSG_WARNING,
2723 				   "FILS: Failed to get channel info for OCI element");
2724 			wpabuf_clear_free(plain);
2725 			return NULL;
2726 		}
2727 
2728 		pos = wpabuf_put(plain, OCV_OCI_EXTENDED_LEN);
2729 		if (ocv_insert_extended_oci(&ci, pos) < 0) {
2730 			wpabuf_clear_free(plain);
2731 			return NULL;
2732 		}
2733 	}
2734 #endif /* CONFIG_OCV */
2735 
2736 	return plain;
2737 }
2738 
2739 
fils_set_tk(struct wpa_state_machine * sm)2740 int fils_set_tk(struct wpa_state_machine *sm)
2741 {
2742 	enum wpa_alg alg;
2743 	int klen;
2744 
2745 	if (!sm || !sm->PTK_valid) {
2746 		wpa_printf(MSG_DEBUG, "FILS: No valid PTK available to set TK");
2747 		return -1;
2748 	}
2749 	if (sm->tk_already_set) {
2750 		wpa_printf(MSG_DEBUG, "FILS: TK already set to the driver");
2751 		return -1;
2752 	}
2753 
2754 	alg = wpa_cipher_to_alg(sm->pairwise);
2755 	klen = wpa_cipher_key_len(sm->pairwise);
2756 
2757 	wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
2758 	if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2759 			     sm->PTK.tk, klen)) {
2760 		wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
2761 		return -1;
2762 	}
2763 	sm->tk_already_set = TRUE;
2764 
2765 	return 0;
2766 }
2767 
2768 
hostapd_eid_assoc_fils_session(struct wpa_state_machine * sm,u8 * buf,const u8 * fils_session,struct wpabuf * hlp)2769 u8 * hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf,
2770 				    const u8 *fils_session, struct wpabuf *hlp)
2771 {
2772 	struct wpabuf *plain;
2773 	u8 *pos = buf;
2774 
2775 	/* FILS Session */
2776 	*pos++ = WLAN_EID_EXTENSION; /* Element ID */
2777 	*pos++ = 1 + FILS_SESSION_LEN; /* Length */
2778 	*pos++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
2779 	os_memcpy(pos, fils_session, FILS_SESSION_LEN);
2780 	pos += FILS_SESSION_LEN;
2781 
2782 	plain = fils_prepare_plainbuf(sm, hlp);
2783 	if (!plain) {
2784 		wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2785 		return NULL;
2786 	}
2787 
2788 	os_memcpy(pos, wpabuf_head(plain), wpabuf_len(plain));
2789 	pos += wpabuf_len(plain);
2790 
2791 	wpa_printf(MSG_DEBUG, "%s: plain buf_len: %u", __func__,
2792 		   (unsigned int) wpabuf_len(plain));
2793 	wpabuf_clear_free(plain);
2794 	sm->fils_completed = 1;
2795 	return pos;
2796 }
2797 
2798 #endif /* CONFIG_FILS */
2799 
2800 
2801 #ifdef CONFIG_OCV
get_sta_tx_parameters(struct wpa_state_machine * sm,int ap_max_chanwidth,int ap_seg1_idx,int * bandwidth,int * seg1_idx)2802 int get_sta_tx_parameters(struct wpa_state_machine *sm, int ap_max_chanwidth,
2803 			  int ap_seg1_idx, int *bandwidth, int *seg1_idx)
2804 {
2805 	struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2806 
2807 	if (!wpa_auth->cb->get_sta_tx_params)
2808 		return -1;
2809 	return wpa_auth->cb->get_sta_tx_params(wpa_auth->cb_ctx, sm->addr,
2810 					       ap_max_chanwidth, ap_seg1_idx,
2811 					       bandwidth, seg1_idx);
2812 }
2813 #endif /* CONFIG_OCV */
2814 
2815 
SM_STATE(WPA_PTK,PTKCALCNEGOTIATING)2816 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2817 {
2818 	struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2819 	struct wpa_ptk PTK;
2820 	int ok = 0, psk_found = 0;
2821 	const u8 *pmk = NULL;
2822 	size_t pmk_len;
2823 	int ft;
2824 	const u8 *eapol_key_ie, *key_data, *mic;
2825 	u16 key_data_length;
2826 	size_t mic_len, eapol_key_ie_len;
2827 	struct ieee802_1x_hdr *hdr;
2828 	struct wpa_eapol_key *key;
2829 	struct wpa_eapol_ie_parse kde;
2830 	int vlan_id = 0;
2831 
2832 	SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2833 	sm->EAPOLKeyReceived = FALSE;
2834 	sm->update_snonce = FALSE;
2835 	os_memset(&PTK, 0, sizeof(PTK));
2836 
2837 	mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
2838 
2839 	/* WPA with IEEE 802.1X: use the derived PMK from EAP
2840 	 * WPA-PSK: iterate through possible PSKs and select the one matching
2841 	 * the packet */
2842 	for (;;) {
2843 		if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
2844 		    !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
2845 			pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
2846 					       sm->p2p_dev_addr, pmk, &pmk_len,
2847 					       &vlan_id);
2848 			if (pmk == NULL)
2849 				break;
2850 			psk_found = 1;
2851 #ifdef CONFIG_IEEE80211R_AP
2852 			if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
2853 				os_memcpy(sm->xxkey, pmk, pmk_len);
2854 				sm->xxkey_len = pmk_len;
2855 			}
2856 #endif /* CONFIG_IEEE80211R_AP */
2857 		} else {
2858 			pmk = sm->PMK;
2859 			pmk_len = sm->pmk_len;
2860 		}
2861 
2862 		if ((!pmk || !pmk_len) && sm->pmksa) {
2863 			wpa_printf(MSG_DEBUG, "WPA: Use PMK from PMKSA cache");
2864 			pmk = sm->pmksa->pmk;
2865 			pmk_len = sm->pmksa->pmk_len;
2866 		}
2867 
2868 		if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK) < 0)
2869 			break;
2870 
2871 		if (mic_len &&
2872 		    wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
2873 				       sm->last_rx_eapol_key,
2874 				       sm->last_rx_eapol_key_len) == 0) {
2875 			if (sm->PMK != pmk) {
2876 				os_memcpy(sm->PMK, pmk, pmk_len);
2877 				sm->pmk_len = pmk_len;
2878 			}
2879 			ok = 1;
2880 			break;
2881 		}
2882 
2883 #ifdef CONFIG_FILS
2884 		if (!mic_len &&
2885 		    wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
2886 				     sm->last_rx_eapol_key_len, NULL) == 0) {
2887 			ok = 1;
2888 			break;
2889 		}
2890 #endif /* CONFIG_FILS */
2891 
2892 		if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
2893 		    wpa_key_mgmt_sae(sm->wpa_key_mgmt))
2894 			break;
2895 	}
2896 
2897 	if (!ok) {
2898 		wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2899 				"invalid MIC in msg 2/4 of 4-Way Handshake");
2900 		if (psk_found)
2901 			wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
2902 		return;
2903 	}
2904 
2905 	/*
2906 	 * Note: last_rx_eapol_key length fields have already been validated in
2907 	 * wpa_receive().
2908 	 */
2909 	hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
2910 	key = (struct wpa_eapol_key *) (hdr + 1);
2911 	mic = (u8 *) (key + 1);
2912 	key_data = mic + mic_len + 2;
2913 	key_data_length = WPA_GET_BE16(mic + mic_len);
2914 	if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
2915 	    sizeof(*key) - mic_len - 2)
2916 		return;
2917 
2918 	if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
2919 		wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
2920 				 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2921 		return;
2922 	}
2923 	if (kde.rsn_ie) {
2924 		eapol_key_ie = kde.rsn_ie;
2925 		eapol_key_ie_len = kde.rsn_ie_len;
2926 	} else if (kde.osen) {
2927 		eapol_key_ie = kde.osen;
2928 		eapol_key_ie_len = kde.osen_len;
2929 	} else {
2930 		eapol_key_ie = kde.wpa_ie;
2931 		eapol_key_ie_len = kde.wpa_ie_len;
2932 	}
2933 	ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
2934 	if (sm->wpa_ie == NULL ||
2935 	    wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
2936 			       eapol_key_ie, eapol_key_ie_len)) {
2937 		wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2938 				"WPA IE from (Re)AssocReq did not match with msg 2/4");
2939 		if (sm->wpa_ie) {
2940 			wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
2941 				    sm->wpa_ie, sm->wpa_ie_len);
2942 		}
2943 		wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
2944 			    eapol_key_ie, eapol_key_ie_len);
2945 		/* MLME-DEAUTHENTICATE.request */
2946 		wpa_sta_disconnect(wpa_auth, sm->addr,
2947 				   WLAN_REASON_PREV_AUTH_NOT_VALID);
2948 		return;
2949 	}
2950 #ifdef CONFIG_OCV
2951 	if (wpa_auth_uses_ocv(sm)) {
2952 		struct wpa_channel_info ci;
2953 		int tx_chanwidth;
2954 		int tx_seg1_idx;
2955 
2956 		if (wpa_channel_info(wpa_auth, &ci) != 0) {
2957 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2958 					"Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
2959 			return;
2960 		}
2961 
2962 		if (get_sta_tx_parameters(sm,
2963 					  channel_width_to_int(ci.chanwidth),
2964 					  ci.seg1_idx, &tx_chanwidth,
2965 					  &tx_seg1_idx) < 0)
2966 			return;
2967 
2968 		if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
2969 					 tx_chanwidth, tx_seg1_idx) != 0) {
2970 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2971 					ocv_errorstr);
2972 			return;
2973 		}
2974 	}
2975 #endif /* CONFIG_OCV */
2976 #ifdef CONFIG_IEEE80211R_AP
2977 	if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
2978 		wpa_sta_disconnect(wpa_auth, sm->addr,
2979 				   WLAN_REASON_PREV_AUTH_NOT_VALID);
2980 		return;
2981 	}
2982 #endif /* CONFIG_IEEE80211R_AP */
2983 #ifdef CONFIG_P2P
2984 	if (kde.ip_addr_req && kde.ip_addr_req[0] &&
2985 	    wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
2986 		int idx;
2987 		wpa_printf(MSG_DEBUG,
2988 			   "P2P: IP address requested in EAPOL-Key exchange");
2989 		idx = bitfield_get_first_zero(wpa_auth->ip_pool);
2990 		if (idx >= 0) {
2991 			u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
2992 			bitfield_set(wpa_auth->ip_pool, idx);
2993 			WPA_PUT_BE32(sm->ip_addr, start + idx);
2994 			wpa_printf(MSG_DEBUG,
2995 				   "P2P: Assigned IP address %u.%u.%u.%u to "
2996 				   MACSTR, sm->ip_addr[0], sm->ip_addr[1],
2997 				   sm->ip_addr[2], sm->ip_addr[3],
2998 				   MAC2STR(sm->addr));
2999 		}
3000 	}
3001 #endif /* CONFIG_P2P */
3002 
3003 #ifdef CONFIG_IEEE80211R_AP
3004 	if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3005 		/*
3006 		 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
3007 		 * with the value we derived.
3008 		 */
3009 		if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
3010 				    WPA_PMK_NAME_LEN) != 0) {
3011 			wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3012 					"PMKR1Name mismatch in FT 4-way "
3013 					"handshake");
3014 			wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
3015 				    "Supplicant",
3016 				    sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
3017 			wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
3018 				    sm->pmk_r1_name, WPA_PMK_NAME_LEN);
3019 			return;
3020 		}
3021 	}
3022 #endif /* CONFIG_IEEE80211R_AP */
3023 
3024 	if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
3025 	    wpa_auth_update_vlan(wpa_auth, sm->addr, vlan_id) < 0) {
3026 		wpa_sta_disconnect(wpa_auth, sm->addr,
3027 				   WLAN_REASON_PREV_AUTH_NOT_VALID);
3028 		return;
3029 	}
3030 
3031 	sm->pending_1_of_4_timeout = 0;
3032 	eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
3033 
3034 	if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
3035 		/* PSK may have changed from the previous choice, so update
3036 		 * state machine data based on whatever PSK was selected here.
3037 		 */
3038 		os_memcpy(sm->PMK, pmk, PMK_LEN);
3039 		sm->pmk_len = PMK_LEN;
3040 	}
3041 
3042 	sm->MICVerified = TRUE;
3043 
3044 	os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
3045 	forced_memzero(&PTK, sizeof(PTK));
3046 	sm->PTK_valid = TRUE;
3047 }
3048 
3049 
SM_STATE(WPA_PTK,PTKCALCNEGOTIATING2)3050 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
3051 {
3052 	SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
3053 	sm->TimeoutCtr = 0;
3054 }
3055 
3056 
3057 #ifdef CONFIG_IEEE80211W
3058 
ieee80211w_kde_len(struct wpa_state_machine * sm)3059 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
3060 {
3061 	if (sm->mgmt_frame_prot) {
3062 		size_t len;
3063 		len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3064 		return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
3065 	}
3066 
3067 	return 0;
3068 }
3069 
3070 
ieee80211w_kde_add(struct wpa_state_machine * sm,u8 * pos)3071 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
3072 {
3073 	struct wpa_igtk_kde igtk;
3074 	struct wpa_group *gsm = sm->group;
3075 	u8 rsc[WPA_KEY_RSC_LEN];
3076 	size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3077 
3078 	if (!sm->mgmt_frame_prot)
3079 		return pos;
3080 
3081 	igtk.keyid[0] = gsm->GN_igtk;
3082 	igtk.keyid[1] = 0;
3083 	if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
3084 	    wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
3085 		os_memset(igtk.pn, 0, sizeof(igtk.pn));
3086 	else
3087 		os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
3088 	os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
3089 	if (sm->wpa_auth->conf.disable_gtk ||
3090 	    sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3091 		/*
3092 		 * Provide unique random IGTK to each STA to prevent use of
3093 		 * IGTK in the BSS.
3094 		 */
3095 		if (random_get_bytes(igtk.igtk, len) < 0)
3096 			return pos;
3097 	}
3098 	pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
3099 			  (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
3100 			  NULL, 0);
3101 
3102 	return pos;
3103 }
3104 
3105 #else /* CONFIG_IEEE80211W */
3106 
ieee80211w_kde_len(struct wpa_state_machine * sm)3107 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
3108 {
3109 	return 0;
3110 }
3111 
3112 
ieee80211w_kde_add(struct wpa_state_machine * sm,u8 * pos)3113 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
3114 {
3115 	return pos;
3116 }
3117 
3118 #endif /* CONFIG_IEEE80211W */
3119 
3120 
ocv_oci_len(struct wpa_state_machine * sm)3121 static int ocv_oci_len(struct wpa_state_machine *sm)
3122 {
3123 #ifdef CONFIG_OCV
3124 	if (wpa_auth_uses_ocv(sm))
3125 		return OCV_OCI_KDE_LEN;
3126 #endif /* CONFIG_OCV */
3127 	return 0;
3128 }
3129 
ocv_oci_add(struct wpa_state_machine * sm,u8 ** argpos)3130 static int ocv_oci_add(struct wpa_state_machine *sm, u8 **argpos)
3131 {
3132 #ifdef CONFIG_OCV
3133 	struct wpa_channel_info ci;
3134 
3135 	if (!wpa_auth_uses_ocv(sm))
3136 		return 0;
3137 
3138 	if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
3139 		wpa_printf(MSG_WARNING,
3140 			   "Failed to get channel info for OCI element");
3141 		return -1;
3142 	}
3143 
3144 	return ocv_insert_oci_kde(&ci, argpos);
3145 #else /* CONFIG_OCV */
3146 	return 0;
3147 #endif /* CONFIG_OCV */
3148 }
3149 
3150 
SM_STATE(WPA_PTK,PTKINITNEGOTIATING)3151 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
3152 {
3153 	u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
3154 	size_t gtk_len, kde_len;
3155 	struct wpa_group *gsm = sm->group;
3156 	u8 *wpa_ie;
3157 	int wpa_ie_len, secure, keyidx, encr = 0;
3158 
3159 	SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
3160 	sm->TimeoutEvt = FALSE;
3161 
3162 	sm->TimeoutCtr++;
3163 	if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3164 	    sm->TimeoutCtr > 1) {
3165 		/* Do not allow retransmission of EAPOL-Key msg 3/4 */
3166 		return;
3167 	}
3168 	if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
3169 		/* No point in sending the EAPOL-Key - we will disconnect
3170 		 * immediately following this. */
3171 		return;
3172 	}
3173 
3174 	/* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
3175 	   GTK[GN], IGTK, [FTIE], [TIE * 2])
3176 	 */
3177 	os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3178 	wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3179 	/* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
3180 	wpa_ie = sm->wpa_auth->wpa_ie;
3181 	wpa_ie_len = sm->wpa_auth->wpa_ie_len;
3182 	if (sm->wpa == WPA_VERSION_WPA &&
3183 	    (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
3184 	    wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
3185 		/* WPA-only STA, remove RSN IE and possible MDIE */
3186 		wpa_ie = wpa_ie + wpa_ie[1] + 2;
3187 		if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
3188 			wpa_ie = wpa_ie + wpa_ie[1] + 2;
3189 		wpa_ie_len = wpa_ie[1] + 2;
3190 	}
3191 	wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3192 			"sending 3/4 msg of 4-Way Handshake");
3193 	if (sm->wpa == WPA_VERSION_WPA2) {
3194 		/* WPA2 send GTK in the 4-way handshake */
3195 		secure = 1;
3196 		gtk = gsm->GTK[gsm->GN - 1];
3197 		gtk_len = gsm->GTK_len;
3198 		if (sm->wpa_auth->conf.disable_gtk ||
3199 		    sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3200 			/*
3201 			 * Provide unique random GTK to each STA to prevent use
3202 			 * of GTK in the BSS.
3203 			 */
3204 			if (random_get_bytes(dummy_gtk, gtk_len) < 0)
3205 				return;
3206 			gtk = dummy_gtk;
3207 		}
3208 		keyidx = gsm->GN;
3209 		_rsc = rsc;
3210 		encr = 1;
3211 	} else {
3212 		/* WPA does not include GTK in msg 3/4 */
3213 		secure = 0;
3214 		gtk = NULL;
3215 		gtk_len = 0;
3216 		keyidx = 0;
3217 		_rsc = NULL;
3218 		if (sm->rx_eapol_key_secure) {
3219 			/*
3220 			 * It looks like Windows 7 supplicant tries to use
3221 			 * Secure bit in msg 2/4 after having reported Michael
3222 			 * MIC failure and it then rejects the 4-way handshake
3223 			 * if msg 3/4 does not set Secure bit. Work around this
3224 			 * by setting the Secure bit here even in the case of
3225 			 * WPA if the supplicant used it first.
3226 			 */
3227 			wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3228 					"STA used Secure bit in WPA msg 2/4 - "
3229 					"set Secure for 3/4 as workaround");
3230 			secure = 1;
3231 		}
3232 	}
3233 
3234 	kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
3235 	if (gtk)
3236 		kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
3237 #ifdef CONFIG_IEEE80211R_AP
3238 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3239 		kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
3240 		kde_len += 300; /* FTIE + 2 * TIE */
3241 	}
3242 #endif /* CONFIG_IEEE80211R_AP */
3243 #ifdef CONFIG_P2P
3244 	if (WPA_GET_BE32(sm->ip_addr) > 0)
3245 		kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
3246 #endif /* CONFIG_P2P */
3247 	kde = os_malloc(kde_len);
3248 	if (kde == NULL)
3249 		return;
3250 
3251 	pos = kde;
3252 	os_memcpy(pos, wpa_ie, wpa_ie_len);
3253 	pos += wpa_ie_len;
3254 #ifdef CONFIG_IEEE80211R_AP
3255 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3256 		int res;
3257 		size_t elen;
3258 
3259 		elen = pos - kde;
3260 		res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
3261 		if (res < 0) {
3262 			wpa_printf(MSG_ERROR, "FT: Failed to insert "
3263 				   "PMKR1Name into RSN IE in EAPOL-Key data");
3264 			os_free(kde);
3265 			return;
3266 		}
3267 		pos -= wpa_ie_len;
3268 		pos += elen;
3269 	}
3270 #endif /* CONFIG_IEEE80211R_AP */
3271 	if (gtk) {
3272 		u8 hdr[2];
3273 		hdr[0] = keyidx & 0x03;
3274 		hdr[1] = 0;
3275 		pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3276 				  gtk, gtk_len);
3277 	}
3278 	pos = ieee80211w_kde_add(sm, pos);
3279 	if (ocv_oci_add(sm, &pos) < 0) {
3280 		os_free(kde);
3281 		return;
3282 	}
3283 
3284 #ifdef CONFIG_IEEE80211R_AP
3285 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3286 		int res;
3287 		struct wpa_auth_config *conf;
3288 
3289 		conf = &sm->wpa_auth->conf;
3290 		if (sm->assoc_resp_ftie &&
3291 		    kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
3292 			os_memcpy(pos, sm->assoc_resp_ftie,
3293 				  2 + sm->assoc_resp_ftie[1]);
3294 			res = 2 + sm->assoc_resp_ftie[1];
3295 		} else {
3296 			int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
3297 
3298 			res = wpa_write_ftie(conf, use_sha384,
3299 					     conf->r0_key_holder,
3300 					     conf->r0_key_holder_len,
3301 					     NULL, NULL, pos,
3302 					     kde + kde_len - pos,
3303 					     NULL, 0);
3304 		}
3305 		if (res < 0) {
3306 			wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
3307 				   "into EAPOL-Key Key Data");
3308 			os_free(kde);
3309 			return;
3310 		}
3311 		pos += res;
3312 
3313 		/* TIE[ReassociationDeadline] (TU) */
3314 		*pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3315 		*pos++ = 5;
3316 		*pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
3317 		WPA_PUT_LE32(pos, conf->reassociation_deadline);
3318 		pos += 4;
3319 
3320 		/* TIE[KeyLifetime] (seconds) */
3321 		*pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3322 		*pos++ = 5;
3323 		*pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
3324 		WPA_PUT_LE32(pos, conf->r0_key_lifetime);
3325 		pos += 4;
3326 	}
3327 #endif /* CONFIG_IEEE80211R_AP */
3328 #ifdef CONFIG_P2P
3329 	if (WPA_GET_BE32(sm->ip_addr) > 0) {
3330 		u8 addr[3 * 4];
3331 		os_memcpy(addr, sm->ip_addr, 4);
3332 		os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
3333 		os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
3334 		pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
3335 				  addr, sizeof(addr), NULL, 0);
3336 	}
3337 #endif /* CONFIG_P2P */
3338 
3339 	wpa_send_eapol(sm->wpa_auth, sm,
3340 		       (secure ? WPA_KEY_INFO_SECURE : 0) |
3341 		       (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3342 			WPA_KEY_INFO_MIC : 0) |
3343 		       WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
3344 		       WPA_KEY_INFO_KEY_TYPE,
3345 		       _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
3346 	os_free(kde);
3347 }
3348 
3349 
SM_STATE(WPA_PTK,PTKINITDONE)3350 SM_STATE(WPA_PTK, PTKINITDONE)
3351 {
3352 	SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
3353 	sm->EAPOLKeyReceived = FALSE;
3354 	if (sm->Pair) {
3355 		enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
3356 		int klen = wpa_cipher_key_len(sm->pairwise);
3357 		if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
3358 				     sm->PTK.tk, klen)) {
3359 			wpa_sta_disconnect(sm->wpa_auth, sm->addr,
3360 					   WLAN_REASON_PREV_AUTH_NOT_VALID);
3361 			return;
3362 		}
3363 		/* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3364 		sm->pairwise_set = TRUE;
3365 
3366 		wpa_auth_set_ptk_rekey_timer(sm);
3367 
3368 		if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3369 		    sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
3370 		    sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
3371 			wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3372 					   WPA_EAPOL_authorized, 1);
3373 		}
3374 	}
3375 
3376 	if (0 /* IBSS == TRUE */) {
3377 		sm->keycount++;
3378 		if (sm->keycount == 2) {
3379 			wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3380 					   WPA_EAPOL_portValid, 1);
3381 		}
3382 	} else {
3383 		wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
3384 				   1);
3385 	}
3386 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
3387 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
3388 	if (sm->wpa == WPA_VERSION_WPA)
3389 		sm->PInitAKeys = TRUE;
3390 	else
3391 		sm->has_GTK = TRUE;
3392 	wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3393 			 "pairwise key handshake completed (%s)",
3394 			 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3395 
3396 #ifdef CONFIG_IEEE80211R_AP
3397 	wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
3398 #endif /* CONFIG_IEEE80211R_AP */
3399 }
3400 
3401 
SM_STEP(WPA_PTK)3402 SM_STEP(WPA_PTK)
3403 {
3404 	struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3405 
3406 	if (sm->Init)
3407 		SM_ENTER(WPA_PTK, INITIALIZE);
3408 	else if (sm->Disconnect
3409 		 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3410 		wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
3411 				"WPA_PTK: sm->Disconnect");
3412 		SM_ENTER(WPA_PTK, DISCONNECT);
3413 	}
3414 	else if (sm->DeauthenticationRequest)
3415 		SM_ENTER(WPA_PTK, DISCONNECTED);
3416 	else if (sm->AuthenticationRequest)
3417 		SM_ENTER(WPA_PTK, AUTHENTICATION);
3418 	else if (sm->ReAuthenticationRequest)
3419 		SM_ENTER(WPA_PTK, AUTHENTICATION2);
3420 	else if (sm->PTKRequest) {
3421 		if (wpa_auth_sm_ptk_update(sm) < 0)
3422 			SM_ENTER(WPA_PTK, DISCONNECTED);
3423 		else
3424 			SM_ENTER(WPA_PTK, PTKSTART);
3425 	} else switch (sm->wpa_ptk_state) {
3426 	case WPA_PTK_INITIALIZE:
3427 		break;
3428 	case WPA_PTK_DISCONNECT:
3429 		SM_ENTER(WPA_PTK, DISCONNECTED);
3430 		break;
3431 	case WPA_PTK_DISCONNECTED:
3432 		SM_ENTER(WPA_PTK, INITIALIZE);
3433 		break;
3434 	case WPA_PTK_AUTHENTICATION:
3435 		SM_ENTER(WPA_PTK, AUTHENTICATION2);
3436 		break;
3437 	case WPA_PTK_AUTHENTICATION2:
3438 		if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
3439 		    wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
3440 				       WPA_EAPOL_keyRun) > 0)
3441 			SM_ENTER(WPA_PTK, INITPMK);
3442 		else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3443 			 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
3444 			 /* FIX: && 802.1X::keyRun */)
3445 			SM_ENTER(WPA_PTK, INITPSK);
3446 		else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
3447 			SM_ENTER(WPA_PTK, INITPMK);
3448 		break;
3449 	case WPA_PTK_INITPMK:
3450 		if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
3451 				       WPA_EAPOL_keyAvailable) > 0) {
3452 			SM_ENTER(WPA_PTK, PTKSTART);
3453 #ifdef CONFIG_DPP
3454 		} else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->pmksa) {
3455 			SM_ENTER(WPA_PTK, PTKSTART);
3456 #endif /* CONFIG_DPP */
3457 		} else {
3458 			wpa_auth->dot11RSNA4WayHandshakeFailures++;
3459 			wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3460 					"INITPMK - keyAvailable = false");
3461 			SM_ENTER(WPA_PTK, DISCONNECT);
3462 		}
3463 		break;
3464 	case WPA_PTK_INITPSK:
3465 		if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
3466 				     NULL, NULL, NULL)) {
3467 			SM_ENTER(WPA_PTK, PTKSTART);
3468 #ifdef CONFIG_SAE
3469 		} else if (wpa_auth_uses_sae(sm) && sm->pmksa) {
3470 			SM_ENTER(WPA_PTK, PTKSTART);
3471 #endif /* CONFIG_SAE */
3472 		} else {
3473 			wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3474 					"no PSK configured for the STA");
3475 			wpa_auth->dot11RSNA4WayHandshakeFailures++;
3476 			SM_ENTER(WPA_PTK, DISCONNECT);
3477 		}
3478 		break;
3479 	case WPA_PTK_PTKSTART:
3480 		if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3481 		    sm->EAPOLKeyPairwise)
3482 			SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3483 		else if (sm->TimeoutCtr >
3484 			 sm->wpa_auth->conf.wpa_pairwise_update_count) {
3485 			wpa_auth->dot11RSNA4WayHandshakeFailures++;
3486 			wpa_auth_vlogger(
3487 				sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3488 				"PTKSTART: Retry limit %u reached",
3489 				sm->wpa_auth->conf.wpa_pairwise_update_count);
3490 			SM_ENTER(WPA_PTK, DISCONNECT);
3491 		} else if (sm->TimeoutEvt)
3492 			SM_ENTER(WPA_PTK, PTKSTART);
3493 		break;
3494 	case WPA_PTK_PTKCALCNEGOTIATING:
3495 		if (sm->MICVerified)
3496 			SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
3497 		else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3498 			 sm->EAPOLKeyPairwise)
3499 			SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3500 		else if (sm->TimeoutEvt)
3501 			SM_ENTER(WPA_PTK, PTKSTART);
3502 		break;
3503 	case WPA_PTK_PTKCALCNEGOTIATING2:
3504 		SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3505 		break;
3506 	case WPA_PTK_PTKINITNEGOTIATING:
3507 		if (sm->update_snonce)
3508 			SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3509 		else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3510 			 sm->EAPOLKeyPairwise && sm->MICVerified)
3511 			SM_ENTER(WPA_PTK, PTKINITDONE);
3512 		else if (sm->TimeoutCtr >
3513 			 sm->wpa_auth->conf.wpa_pairwise_update_count ||
3514 			 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3515 			  sm->TimeoutCtr > 1)) {
3516 			wpa_auth->dot11RSNA4WayHandshakeFailures++;
3517 			wpa_auth_vlogger(
3518 				sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3519 				"PTKINITNEGOTIATING: Retry limit %u reached",
3520 				sm->wpa_auth->conf.wpa_pairwise_update_count);
3521 			SM_ENTER(WPA_PTK, DISCONNECT);
3522 		} else if (sm->TimeoutEvt)
3523 			SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3524 		break;
3525 	case WPA_PTK_PTKINITDONE:
3526 		break;
3527 	}
3528 }
3529 
3530 
SM_STATE(WPA_PTK_GROUP,IDLE)3531 SM_STATE(WPA_PTK_GROUP, IDLE)
3532 {
3533 	SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
3534 	if (sm->Init) {
3535 		/* Init flag is not cleared here, so avoid busy
3536 		 * loop by claiming nothing changed. */
3537 		sm->changed = FALSE;
3538 	}
3539 	sm->GTimeoutCtr = 0;
3540 }
3541 
3542 
SM_STATE(WPA_PTK_GROUP,REKEYNEGOTIATING)3543 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
3544 {
3545 	u8 rsc[WPA_KEY_RSC_LEN];
3546 	struct wpa_group *gsm = sm->group;
3547 	const u8 *kde;
3548 	u8 *kde_buf = NULL, *pos, hdr[2];
3549 	size_t kde_len;
3550 	u8 *gtk, dummy_gtk[32];
3551 
3552 	SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
3553 
3554 	sm->GTimeoutCtr++;
3555 	if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3556 	    sm->GTimeoutCtr > 1) {
3557 		/* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3558 		return;
3559 	}
3560 	if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) {
3561 		/* No point in sending the EAPOL-Key - we will disconnect
3562 		 * immediately following this. */
3563 		return;
3564 	}
3565 
3566 	if (sm->wpa == WPA_VERSION_WPA)
3567 		sm->PInitAKeys = FALSE;
3568 	sm->TimeoutEvt = FALSE;
3569 	/* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3570 	os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3571 	if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
3572 		wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3573 	wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3574 			"sending 1/2 msg of Group Key Handshake");
3575 
3576 	gtk = gsm->GTK[gsm->GN - 1];
3577 	if (sm->wpa_auth->conf.disable_gtk ||
3578 	    sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3579 		/*
3580 		 * Provide unique random GTK to each STA to prevent use
3581 		 * of GTK in the BSS.
3582 		 */
3583 		if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
3584 			return;
3585 		gtk = dummy_gtk;
3586 	}
3587 	if (sm->wpa == WPA_VERSION_WPA2) {
3588 		kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
3589 			ieee80211w_kde_len(sm) + ocv_oci_len(sm);
3590 		kde_buf = os_malloc(kde_len);
3591 		if (kde_buf == NULL)
3592 			return;
3593 
3594 		kde = pos = kde_buf;
3595 		hdr[0] = gsm->GN & 0x03;
3596 		hdr[1] = 0;
3597 		pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3598 				  gtk, gsm->GTK_len);
3599 		pos = ieee80211w_kde_add(sm, pos);
3600 		if (ocv_oci_add(sm, &pos) < 0) {
3601 			os_free(kde_buf);
3602 			return;
3603 		}
3604 		kde_len = pos - kde;
3605 	} else {
3606 		kde = gtk;
3607 		kde_len = gsm->GTK_len;
3608 	}
3609 
3610 	wpa_send_eapol(sm->wpa_auth, sm,
3611 		       WPA_KEY_INFO_SECURE |
3612 		       (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3613 			WPA_KEY_INFO_MIC : 0) |
3614 		       WPA_KEY_INFO_ACK |
3615 		       (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
3616 		       rsc, NULL, kde, kde_len, gsm->GN, 1);
3617 
3618 	os_free(kde_buf);
3619 }
3620 
3621 
SM_STATE(WPA_PTK_GROUP,REKEYESTABLISHED)3622 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
3623 {
3624 #ifdef CONFIG_OCV
3625 	struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3626 	const u8 *key_data, *mic;
3627 	struct ieee802_1x_hdr *hdr;
3628 	struct wpa_eapol_key *key;
3629 	struct wpa_eapol_ie_parse kde;
3630 	size_t mic_len;
3631 	u16 key_data_length;
3632 #endif /* CONFIG_OCV */
3633 
3634 	SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
3635 	sm->EAPOLKeyReceived = FALSE;
3636 
3637 #ifdef CONFIG_OCV
3638 	mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
3639 
3640 	/*
3641 	 * Note: last_rx_eapol_key length fields have already been validated in
3642 	 * wpa_receive().
3643 	 */
3644 	hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
3645 	key = (struct wpa_eapol_key *) (hdr + 1);
3646 	mic = (u8 *) (key + 1);
3647 	key_data = mic + mic_len + 2;
3648 	key_data_length = WPA_GET_BE16(mic + mic_len);
3649 	if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
3650 	    sizeof(*key) - mic_len - 2)
3651 		return;
3652 
3653 	if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
3654 		wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
3655 				 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3656 		return;
3657 	}
3658 
3659 	if (wpa_auth_uses_ocv(sm)) {
3660 		struct wpa_channel_info ci;
3661 		int tx_chanwidth;
3662 		int tx_seg1_idx;
3663 
3664 		if (wpa_channel_info(wpa_auth, &ci) != 0) {
3665 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3666 					"Failed to get channel info to validate received OCI in EAPOL-Key group 1/2");
3667 			return;
3668 		}
3669 
3670 		if (get_sta_tx_parameters(sm,
3671 					  channel_width_to_int(ci.chanwidth),
3672 					  ci.seg1_idx, &tx_chanwidth,
3673 					  &tx_seg1_idx) < 0)
3674 			return;
3675 
3676 		if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
3677 					 tx_chanwidth, tx_seg1_idx) != 0) {
3678 			wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3679 					ocv_errorstr);
3680 			return;
3681 		}
3682 	}
3683 #endif /* CONFIG_OCV */
3684 
3685 	if (sm->GUpdateStationKeys)
3686 		sm->group->GKeyDoneStations--;
3687 	sm->GUpdateStationKeys = FALSE;
3688 	sm->GTimeoutCtr = 0;
3689 	/* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3690 	wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3691 			 "group key handshake completed (%s)",
3692 			 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3693 	sm->has_GTK = TRUE;
3694 }
3695 
3696 
SM_STATE(WPA_PTK_GROUP,KEYERROR)3697 SM_STATE(WPA_PTK_GROUP, KEYERROR)
3698 {
3699 	SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
3700 	if (sm->GUpdateStationKeys)
3701 		sm->group->GKeyDoneStations--;
3702 	sm->GUpdateStationKeys = FALSE;
3703 	sm->Disconnect = TRUE;
3704 	wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3705 			 "group key handshake failed (%s) after %u tries",
3706 			 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
3707 			 sm->wpa_auth->conf.wpa_group_update_count);
3708 }
3709 
3710 
SM_STEP(WPA_PTK_GROUP)3711 SM_STEP(WPA_PTK_GROUP)
3712 {
3713 	if (sm->Init || sm->PtkGroupInit) {
3714 		SM_ENTER(WPA_PTK_GROUP, IDLE);
3715 		sm->PtkGroupInit = FALSE;
3716 	} else switch (sm->wpa_ptk_group_state) {
3717 	case WPA_PTK_GROUP_IDLE:
3718 		if (sm->GUpdateStationKeys ||
3719 		    (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
3720 			SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3721 		break;
3722 	case WPA_PTK_GROUP_REKEYNEGOTIATING:
3723 		if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3724 		    !sm->EAPOLKeyPairwise && sm->MICVerified)
3725 			SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
3726 		else if (sm->GTimeoutCtr >
3727 			 sm->wpa_auth->conf.wpa_group_update_count ||
3728 			 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3729 			  sm->GTimeoutCtr > 1))
3730 			SM_ENTER(WPA_PTK_GROUP, KEYERROR);
3731 		else if (sm->TimeoutEvt)
3732 			SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3733 		break;
3734 	case WPA_PTK_GROUP_KEYERROR:
3735 		SM_ENTER(WPA_PTK_GROUP, IDLE);
3736 		break;
3737 	case WPA_PTK_GROUP_REKEYESTABLISHED:
3738 		SM_ENTER(WPA_PTK_GROUP, IDLE);
3739 		break;
3740 	}
3741 }
3742 
3743 
wpa_gtk_update(struct wpa_authenticator * wpa_auth,struct wpa_group * group)3744 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
3745 			  struct wpa_group *group)
3746 {
3747 	int ret = 0;
3748 
3749 	os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3750 	inc_byte_array(group->Counter, WPA_NONCE_LEN);
3751 	if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
3752 			   wpa_auth->addr, group->GNonce,
3753 			   group->GTK[group->GN - 1], group->GTK_len) < 0)
3754 		ret = -1;
3755 	wpa_hexdump_key(MSG_DEBUG, "GTK",
3756 			group->GTK[group->GN - 1], group->GTK_len);
3757 
3758 #ifdef CONFIG_IEEE80211W
3759 	if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3760 		size_t len;
3761 		len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3762 		os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3763 		inc_byte_array(group->Counter, WPA_NONCE_LEN);
3764 		if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
3765 				   wpa_auth->addr, group->GNonce,
3766 				   group->IGTK[group->GN_igtk - 4], len) < 0)
3767 			ret = -1;
3768 		wpa_hexdump_key(MSG_DEBUG, "IGTK",
3769 				group->IGTK[group->GN_igtk - 4], len);
3770 	}
3771 #endif /* CONFIG_IEEE80211W */
3772 
3773 	return ret;
3774 }
3775 
3776 
wpa_group_gtk_init(struct wpa_authenticator * wpa_auth,struct wpa_group * group)3777 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
3778 			       struct wpa_group *group)
3779 {
3780 	wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3781 		   "GTK_INIT (VLAN-ID %d)", group->vlan_id);
3782 	group->changed = FALSE; /* GInit is not cleared here; avoid loop */
3783 	group->wpa_group_state = WPA_GROUP_GTK_INIT;
3784 
3785 	/* GTK[0..N] = 0 */
3786 	os_memset(group->GTK, 0, sizeof(group->GTK));
3787 	group->GN = 1;
3788 	group->GM = 2;
3789 #ifdef CONFIG_IEEE80211W
3790 	group->GN_igtk = 4;
3791 	group->GM_igtk = 5;
3792 #endif /* CONFIG_IEEE80211W */
3793 	/* GTK[GN] = CalcGTK() */
3794 	wpa_gtk_update(wpa_auth, group);
3795 }
3796 
3797 
wpa_group_update_sta(struct wpa_state_machine * sm,void * ctx)3798 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
3799 {
3800 	if (ctx != NULL && ctx != sm->group)
3801 		return 0;
3802 
3803 	if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
3804 		wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3805 				"Not in PTKINITDONE; skip Group Key update");
3806 		sm->GUpdateStationKeys = FALSE;
3807 		return 0;
3808 	}
3809 	if (sm->GUpdateStationKeys) {
3810 		/*
3811 		 * This should not really happen, so add a debug log entry.
3812 		 * Since we clear the GKeyDoneStations before the loop, the
3813 		 * station needs to be counted here anyway.
3814 		 */
3815 		wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3816 				"GUpdateStationKeys was already set when "
3817 				"marking station for GTK rekeying");
3818 	}
3819 
3820 	/* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
3821 	if (sm->is_wnmsleep)
3822 		return 0;
3823 
3824 	sm->group->GKeyDoneStations++;
3825 	sm->GUpdateStationKeys = TRUE;
3826 
3827 	wpa_sm_step(sm);
3828 	return 0;
3829 }
3830 
3831 
3832 #ifdef CONFIG_WNM_AP
3833 /* update GTK when exiting WNM-Sleep Mode */
wpa_wnmsleep_rekey_gtk(struct wpa_state_machine * sm)3834 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
3835 {
3836 	if (sm == NULL || sm->is_wnmsleep)
3837 		return;
3838 
3839 	wpa_group_update_sta(sm, NULL);
3840 }
3841 
3842 
wpa_set_wnmsleep(struct wpa_state_machine * sm,int flag)3843 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
3844 {
3845 	if (sm)
3846 		sm->is_wnmsleep = !!flag;
3847 }
3848 
3849 
wpa_wnmsleep_gtk_subelem(struct wpa_state_machine * sm,u8 * pos)3850 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3851 {
3852 	struct wpa_group *gsm = sm->group;
3853 	u8 *start = pos;
3854 
3855 	/*
3856 	 * GTK subelement:
3857 	 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
3858 	 * Key[5..32]
3859 	 */
3860 	*pos++ = WNM_SLEEP_SUBELEM_GTK;
3861 	*pos++ = 11 + gsm->GTK_len;
3862 	/* Key ID in B0-B1 of Key Info */
3863 	WPA_PUT_LE16(pos, gsm->GN & 0x03);
3864 	pos += 2;
3865 	*pos++ = gsm->GTK_len;
3866 	if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
3867 		return 0;
3868 	pos += 8;
3869 	os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3870 	pos += gsm->GTK_len;
3871 
3872 	wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3873 		   gsm->GN);
3874 	wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
3875 			gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3876 
3877 	return pos - start;
3878 }
3879 
3880 
3881 #ifdef CONFIG_IEEE80211W
wpa_wnmsleep_igtk_subelem(struct wpa_state_machine * sm,u8 * pos)3882 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3883 {
3884 	struct wpa_group *gsm = sm->group;
3885 	u8 *start = pos;
3886 	size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3887 
3888 	/*
3889 	 * IGTK subelement:
3890 	 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3891 	 */
3892 	*pos++ = WNM_SLEEP_SUBELEM_IGTK;
3893 	*pos++ = 2 + 6 + len;
3894 	WPA_PUT_LE16(pos, gsm->GN_igtk);
3895 	pos += 2;
3896 	if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
3897 		return 0;
3898 	pos += 6;
3899 
3900 	os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
3901 	pos += len;
3902 
3903 	wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3904 		   gsm->GN_igtk);
3905 	wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
3906 			gsm->IGTK[gsm->GN_igtk - 4], len);
3907 
3908 	return pos - start;
3909 }
3910 #endif /* CONFIG_IEEE80211W */
3911 #endif /* CONFIG_WNM_AP */
3912 
3913 
wpa_group_setkeys(struct wpa_authenticator * wpa_auth,struct wpa_group * group)3914 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
3915 			      struct wpa_group *group)
3916 {
3917 	int tmp;
3918 
3919 	wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3920 		   "SETKEYS (VLAN-ID %d)", group->vlan_id);
3921 	group->changed = TRUE;
3922 	group->wpa_group_state = WPA_GROUP_SETKEYS;
3923 	group->GTKReKey = FALSE;
3924 	tmp = group->GM;
3925 	group->GM = group->GN;
3926 	group->GN = tmp;
3927 #ifdef CONFIG_IEEE80211W
3928 	tmp = group->GM_igtk;
3929 	group->GM_igtk = group->GN_igtk;
3930 	group->GN_igtk = tmp;
3931 #endif /* CONFIG_IEEE80211W */
3932 	/* "GKeyDoneStations = GNoStations" is done in more robust way by
3933 	 * counting the STAs that are marked with GUpdateStationKeys instead of
3934 	 * including all STAs that could be in not-yet-completed state. */
3935 	wpa_gtk_update(wpa_auth, group);
3936 
3937 	if (group->GKeyDoneStations) {
3938 		wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
3939 			   "GKeyDoneStations=%d when starting new GTK rekey",
3940 			   group->GKeyDoneStations);
3941 		group->GKeyDoneStations = 0;
3942 	}
3943 	wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
3944 	wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
3945 		   group->GKeyDoneStations);
3946 }
3947 
3948 
wpa_group_config_group_keys(struct wpa_authenticator * wpa_auth,struct wpa_group * group)3949 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
3950 				       struct wpa_group *group)
3951 {
3952 	int ret = 0;
3953 
3954 	if (wpa_auth_set_key(wpa_auth, group->vlan_id,
3955 			     wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
3956 			     broadcast_ether_addr, group->GN,
3957 			     group->GTK[group->GN - 1], group->GTK_len) < 0)
3958 		ret = -1;
3959 
3960 #ifdef CONFIG_IEEE80211W
3961 	if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3962 		enum wpa_alg alg;
3963 		size_t len;
3964 
3965 		alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
3966 		len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3967 
3968 		if (ret == 0 &&
3969 		    wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
3970 				     broadcast_ether_addr, group->GN_igtk,
3971 				     group->IGTK[group->GN_igtk - 4], len) < 0)
3972 			ret = -1;
3973 	}
3974 #endif /* CONFIG_IEEE80211W */
3975 
3976 	return ret;
3977 }
3978 
3979 
wpa_group_disconnect_cb(struct wpa_state_machine * sm,void * ctx)3980 static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
3981 {
3982 	if (sm->group == ctx) {
3983 		wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
3984 			   " for discconnection due to fatal failure",
3985 			   MAC2STR(sm->addr));
3986 		sm->Disconnect = TRUE;
3987 	}
3988 
3989 	return 0;
3990 }
3991 
3992 
wpa_group_fatal_failure(struct wpa_authenticator * wpa_auth,struct wpa_group * group)3993 static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
3994 				    struct wpa_group *group)
3995 {
3996 	wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
3997 	group->changed = TRUE;
3998 	group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
3999 	wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
4000 }
4001 
4002 
wpa_group_setkeysdone(struct wpa_authenticator * wpa_auth,struct wpa_group * group)4003 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
4004 				 struct wpa_group *group)
4005 {
4006 	wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
4007 		   "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
4008 	group->changed = TRUE;
4009 	group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
4010 
4011 	if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
4012 		wpa_group_fatal_failure(wpa_auth, group);
4013 		return -1;
4014 	}
4015 
4016 	return 0;
4017 }
4018 
4019 
wpa_group_sm_step(struct wpa_authenticator * wpa_auth,struct wpa_group * group)4020 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
4021 			      struct wpa_group *group)
4022 {
4023 	if (group->GInit) {
4024 		wpa_group_gtk_init(wpa_auth, group);
4025 	} else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
4026 		/* Do not allow group operations */
4027 	} else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
4028 		   group->GTKAuthenticator) {
4029 		wpa_group_setkeysdone(wpa_auth, group);
4030 	} else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
4031 		   group->GTKReKey) {
4032 		wpa_group_setkeys(wpa_auth, group);
4033 	} else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
4034 		if (group->GKeyDoneStations == 0)
4035 			wpa_group_setkeysdone(wpa_auth, group);
4036 		else if (group->GTKReKey)
4037 			wpa_group_setkeys(wpa_auth, group);
4038 	}
4039 }
4040 
4041 
wpa_sm_step(struct wpa_state_machine * sm)4042 static int wpa_sm_step(struct wpa_state_machine *sm)
4043 {
4044 	if (sm == NULL)
4045 		return 0;
4046 
4047 	if (sm->in_step_loop) {
4048 		/* This should not happen, but if it does, make sure we do not
4049 		 * end up freeing the state machine too early by exiting the
4050 		 * recursive call. */
4051 		wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
4052 		return 0;
4053 	}
4054 
4055 	sm->in_step_loop = 1;
4056 	do {
4057 		if (sm->pending_deinit)
4058 			break;
4059 
4060 		sm->changed = FALSE;
4061 		sm->wpa_auth->group->changed = FALSE;
4062 
4063 		SM_STEP_RUN(WPA_PTK);
4064 		if (sm->pending_deinit)
4065 			break;
4066 		SM_STEP_RUN(WPA_PTK_GROUP);
4067 		if (sm->pending_deinit)
4068 			break;
4069 		wpa_group_sm_step(sm->wpa_auth, sm->group);
4070 	} while (sm->changed || sm->wpa_auth->group->changed);
4071 	sm->in_step_loop = 0;
4072 
4073 	if (sm->pending_deinit) {
4074 		wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
4075 			   "machine deinit for " MACSTR, MAC2STR(sm->addr));
4076 		wpa_free_sta_sm(sm);
4077 		return 1;
4078 	}
4079 	return 0;
4080 }
4081 
4082 
wpa_sm_call_step(void * eloop_ctx,void * timeout_ctx)4083 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
4084 {
4085 	struct wpa_state_machine *sm = eloop_ctx;
4086 	wpa_sm_step(sm);
4087 }
4088 
4089 
wpa_auth_sm_notify(struct wpa_state_machine * sm)4090 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
4091 {
4092 	if (sm == NULL)
4093 		return;
4094 	eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
4095 }
4096 
4097 
wpa_gtk_rekey(struct wpa_authenticator * wpa_auth)4098 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
4099 {
4100 	int tmp, i;
4101 	struct wpa_group *group;
4102 
4103 	if (wpa_auth == NULL)
4104 		return;
4105 
4106 	group = wpa_auth->group;
4107 
4108 	for (i = 0; i < 2; i++) {
4109 		tmp = group->GM;
4110 		group->GM = group->GN;
4111 		group->GN = tmp;
4112 #ifdef CONFIG_IEEE80211W
4113 		tmp = group->GM_igtk;
4114 		group->GM_igtk = group->GN_igtk;
4115 		group->GN_igtk = tmp;
4116 #endif /* CONFIG_IEEE80211W */
4117 		wpa_gtk_update(wpa_auth, group);
4118 		wpa_group_config_group_keys(wpa_auth, group);
4119 	}
4120 }
4121 
4122 
wpa_bool_txt(int val)4123 static const char * wpa_bool_txt(int val)
4124 {
4125 	return val ? "TRUE" : "FALSE";
4126 }
4127 
4128 
4129 #define RSN_SUITE "%02x-%02x-%02x-%d"
4130 #define RSN_SUITE_ARG(s) \
4131 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
4132 
wpa_get_mib(struct wpa_authenticator * wpa_auth,char * buf,size_t buflen)4133 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
4134 {
4135 	int len = 0, ret;
4136 	char pmkid_txt[PMKID_LEN * 2 + 1];
4137 #ifdef CONFIG_RSN_PREAUTH
4138 	const int preauth = 1;
4139 #else /* CONFIG_RSN_PREAUTH */
4140 	const int preauth = 0;
4141 #endif /* CONFIG_RSN_PREAUTH */
4142 
4143 	if (wpa_auth == NULL)
4144 		return len;
4145 
4146 	ret = os_snprintf(buf + len, buflen - len,
4147 			  "dot11RSNAOptionImplemented=TRUE\n"
4148 			  "dot11RSNAPreauthenticationImplemented=%s\n"
4149 			  "dot11RSNAEnabled=%s\n"
4150 			  "dot11RSNAPreauthenticationEnabled=%s\n",
4151 			  wpa_bool_txt(preauth),
4152 			  wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
4153 			  wpa_bool_txt(wpa_auth->conf.rsn_preauth));
4154 	if (os_snprintf_error(buflen - len, ret))
4155 		return len;
4156 	len += ret;
4157 
4158 	wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
4159 			 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
4160 
4161 	ret = os_snprintf(
4162 		buf + len, buflen - len,
4163 		"dot11RSNAConfigVersion=%u\n"
4164 		"dot11RSNAConfigPairwiseKeysSupported=9999\n"
4165 		/* FIX: dot11RSNAConfigGroupCipher */
4166 		/* FIX: dot11RSNAConfigGroupRekeyMethod */
4167 		/* FIX: dot11RSNAConfigGroupRekeyTime */
4168 		/* FIX: dot11RSNAConfigGroupRekeyPackets */
4169 		"dot11RSNAConfigGroupRekeyStrict=%u\n"
4170 		"dot11RSNAConfigGroupUpdateCount=%u\n"
4171 		"dot11RSNAConfigPairwiseUpdateCount=%u\n"
4172 		"dot11RSNAConfigGroupCipherSize=%u\n"
4173 		"dot11RSNAConfigPMKLifetime=%u\n"
4174 		"dot11RSNAConfigPMKReauthThreshold=%u\n"
4175 		"dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4176 		"dot11RSNAConfigSATimeout=%u\n"
4177 		"dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
4178 		"dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
4179 		"dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
4180 		"dot11RSNAPMKIDUsed=%s\n"
4181 		"dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
4182 		"dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
4183 		"dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
4184 		"dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4185 		"dot11RSNA4WayHandshakeFailures=%u\n"
4186 		"dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4187 		RSN_VERSION,
4188 		!!wpa_auth->conf.wpa_strict_rekey,
4189 		wpa_auth->conf.wpa_group_update_count,
4190 		wpa_auth->conf.wpa_pairwise_update_count,
4191 		wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
4192 		dot11RSNAConfigPMKLifetime,
4193 		dot11RSNAConfigPMKReauthThreshold,
4194 		dot11RSNAConfigSATimeout,
4195 		RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
4196 		RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
4197 		RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
4198 		pmkid_txt,
4199 		RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
4200 		RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
4201 		RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
4202 		wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
4203 		wpa_auth->dot11RSNA4WayHandshakeFailures);
4204 	if (os_snprintf_error(buflen - len, ret))
4205 		return len;
4206 	len += ret;
4207 
4208 	/* TODO: dot11RSNAConfigPairwiseCiphersTable */
4209 	/* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4210 
4211 	/* Private MIB */
4212 	ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
4213 			  wpa_auth->group->wpa_group_state);
4214 	if (os_snprintf_error(buflen - len, ret))
4215 		return len;
4216 	len += ret;
4217 
4218 	return len;
4219 }
4220 
4221 
wpa_get_mib_sta(struct wpa_state_machine * sm,char * buf,size_t buflen)4222 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
4223 {
4224 	int len = 0, ret;
4225 	u32 pairwise = 0;
4226 
4227 	if (sm == NULL)
4228 		return 0;
4229 
4230 	/* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4231 
4232 	/* dot11RSNAStatsEntry */
4233 
4234 	pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
4235 				       WPA_PROTO_RSN : WPA_PROTO_WPA,
4236 				       sm->pairwise);
4237 	if (pairwise == 0)
4238 		return 0;
4239 
4240 	ret = os_snprintf(
4241 		buf + len, buflen - len,
4242 		/* TODO: dot11RSNAStatsIndex */
4243 		"dot11RSNAStatsSTAAddress=" MACSTR "\n"
4244 		"dot11RSNAStatsVersion=1\n"
4245 		"dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
4246 		/* TODO: dot11RSNAStatsTKIPICVErrors */
4247 		"dot11RSNAStatsTKIPLocalMICFailures=%u\n"
4248 		"dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
4249 		/* TODO: dot11RSNAStatsCCMPReplays */
4250 		/* TODO: dot11RSNAStatsCCMPDecryptErrors */
4251 		/* TODO: dot11RSNAStatsTKIPReplays */,
4252 		MAC2STR(sm->addr),
4253 		RSN_SUITE_ARG(pairwise),
4254 		sm->dot11RSNAStatsTKIPLocalMICFailures,
4255 		sm->dot11RSNAStatsTKIPRemoteMICFailures);
4256 	if (os_snprintf_error(buflen - len, ret))
4257 		return len;
4258 	len += ret;
4259 
4260 	/* Private MIB */
4261 	ret = os_snprintf(buf + len, buflen - len,
4262 			  "wpa=%d\n"
4263 			  "AKMSuiteSelector=" RSN_SUITE "\n"
4264 			  "hostapdWPAPTKState=%d\n"
4265 			  "hostapdWPAPTKGroupState=%d\n",
4266 			  sm->wpa,
4267 			  RSN_SUITE_ARG(wpa_akm_to_suite(sm->wpa_key_mgmt)),
4268 			  sm->wpa_ptk_state,
4269 			  sm->wpa_ptk_group_state);
4270 	if (os_snprintf_error(buflen - len, ret))
4271 		return len;
4272 	len += ret;
4273 
4274 	return len;
4275 }
4276 
4277 
wpa_auth_countermeasures_start(struct wpa_authenticator * wpa_auth)4278 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
4279 {
4280 	if (wpa_auth)
4281 		wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
4282 }
4283 
4284 
wpa_auth_pairwise_set(struct wpa_state_machine * sm)4285 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
4286 {
4287 	return sm && sm->pairwise_set;
4288 }
4289 
4290 
wpa_auth_get_pairwise(struct wpa_state_machine * sm)4291 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
4292 {
4293 	return sm->pairwise;
4294 }
4295 
4296 
wpa_auth_get_pmk(struct wpa_state_machine * sm,int * len)4297 const u8 * wpa_auth_get_pmk(struct wpa_state_machine *sm, int *len)
4298 {
4299 	if (!sm)
4300 		return NULL;
4301 	*len = sm->pmk_len;
4302 	return sm->PMK;
4303 }
4304 
4305 
wpa_auth_sta_key_mgmt(struct wpa_state_machine * sm)4306 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
4307 {
4308 	if (sm == NULL)
4309 		return -1;
4310 	return sm->wpa_key_mgmt;
4311 }
4312 
4313 
wpa_auth_sta_wpa_version(struct wpa_state_machine * sm)4314 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
4315 {
4316 	if (sm == NULL)
4317 		return 0;
4318 	return sm->wpa;
4319 }
4320 
4321 
wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine * sm)4322 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
4323 {
4324 	if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
4325 		return 0;
4326 	return sm->tk_already_set;
4327 }
4328 
4329 
wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine * sm)4330 int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm)
4331 {
4332 	if (!sm || !wpa_key_mgmt_fils(sm->wpa_key_mgmt))
4333 		return 0;
4334 	return sm->tk_already_set;
4335 }
4336 
4337 
wpa_auth_sta_clear_pmksa(struct wpa_state_machine * sm,struct rsn_pmksa_cache_entry * entry)4338 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
4339 			     struct rsn_pmksa_cache_entry *entry)
4340 {
4341 	if (sm == NULL || sm->pmksa != entry)
4342 		return -1;
4343 	sm->pmksa = NULL;
4344 	return 0;
4345 }
4346 
4347 
4348 struct rsn_pmksa_cache_entry *
wpa_auth_sta_get_pmksa(struct wpa_state_machine * sm)4349 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
4350 {
4351 	return sm ? sm->pmksa : NULL;
4352 }
4353 
4354 
wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine * sm)4355 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
4356 {
4357 	if (sm)
4358 		sm->dot11RSNAStatsTKIPLocalMICFailures++;
4359 }
4360 
4361 
wpa_auth_get_wpa_ie(struct wpa_authenticator * wpa_auth,size_t * len)4362 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
4363 {
4364 	if (wpa_auth == NULL)
4365 		return NULL;
4366 	*len = wpa_auth->wpa_ie_len;
4367 	return wpa_auth->wpa_ie;
4368 }
4369 
4370 
wpa_auth_pmksa_add(struct wpa_state_machine * sm,const u8 * pmk,unsigned int pmk_len,int session_timeout,struct eapol_state_machine * eapol)4371 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
4372 		       unsigned int pmk_len,
4373 		       int session_timeout, struct eapol_state_machine *eapol)
4374 {
4375 	if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
4376 	    sm->wpa_auth->conf.disable_pmksa_caching)
4377 		return -1;
4378 
4379 #ifdef CONFIG_IEEE80211R_AP
4380 	if (pmk_len >= 2 * PMK_LEN && wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
4381 	    wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
4382 	    !wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
4383 		/* Cache MPMK/XXKey instead of initial part from MSK */
4384 		pmk = pmk + PMK_LEN;
4385 		pmk_len = PMK_LEN;
4386 	} else
4387 #endif /* CONFIG_IEEE80211R_AP */
4388 	if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
4389 		if (pmk_len > PMK_LEN_SUITE_B_192)
4390 			pmk_len = PMK_LEN_SUITE_B_192;
4391 	} else if (pmk_len > PMK_LEN) {
4392 		pmk_len = PMK_LEN;
4393 	}
4394 
4395 	wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK", pmk, pmk_len);
4396 	if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
4397 				 sm->PTK.kck, sm->PTK.kck_len,
4398 				 sm->wpa_auth->addr, sm->addr, session_timeout,
4399 				 eapol, sm->wpa_key_mgmt))
4400 		return 0;
4401 
4402 	return -1;
4403 }
4404 
4405 
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)4406 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
4407 			       const u8 *pmk, size_t len, const u8 *sta_addr,
4408 			       int session_timeout,
4409 			       struct eapol_state_machine *eapol)
4410 {
4411 	if (wpa_auth == NULL)
4412 		return -1;
4413 
4414 	wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from preauth", pmk, len);
4415 	if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
4416 				 NULL, 0,
4417 				 wpa_auth->addr,
4418 				 sta_addr, session_timeout, eapol,
4419 				 WPA_KEY_MGMT_IEEE8021X))
4420 		return 0;
4421 
4422 	return -1;
4423 }
4424 
4425 
wpa_auth_pmksa_add_sae(struct wpa_authenticator * wpa_auth,const u8 * addr,const u8 * pmk,const u8 * pmkid)4426 int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
4427 			   const u8 *pmk, const u8 *pmkid)
4428 {
4429 	if (wpa_auth->conf.disable_pmksa_caching)
4430 		return -1;
4431 
4432 	wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from SAE", pmk, PMK_LEN);
4433 	if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
4434 				 NULL, 0,
4435 				 wpa_auth->addr, addr, 0, NULL,
4436 				 WPA_KEY_MGMT_SAE))
4437 		return 0;
4438 
4439 	return -1;
4440 }
4441 
4442 
wpa_auth_add_sae_pmkid(struct wpa_state_machine * sm,const u8 * pmkid)4443 void wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid)
4444 {
4445 	os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
4446 	sm->pmkid_set = 1;
4447 }
4448 
4449 
wpa_auth_pmksa_add2(struct wpa_authenticator * wpa_auth,const u8 * addr,const u8 * pmk,size_t pmk_len,const u8 * pmkid,int session_timeout,int akmp)4450 int wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr,
4451 			const u8 *pmk, size_t pmk_len, const u8 *pmkid,
4452 			int session_timeout, int akmp)
4453 {
4454 	if (wpa_auth->conf.disable_pmksa_caching)
4455 		return -1;
4456 
4457 	wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK (2)", pmk, PMK_LEN);
4458 	if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
4459 				 NULL, 0, wpa_auth->addr, addr, session_timeout,
4460 				 NULL, akmp))
4461 		return 0;
4462 
4463 	return -1;
4464 }
4465 
4466 
wpa_auth_pmksa_remove(struct wpa_authenticator * wpa_auth,const u8 * sta_addr)4467 void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
4468 			   const u8 *sta_addr)
4469 {
4470 	struct rsn_pmksa_cache_entry *pmksa;
4471 
4472 	if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
4473 		return;
4474 	pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
4475 	if (pmksa) {
4476 		wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
4477 			   MACSTR " based on request", MAC2STR(sta_addr));
4478 		pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
4479 	}
4480 }
4481 
4482 
wpa_auth_pmksa_list(struct wpa_authenticator * wpa_auth,char * buf,size_t len)4483 int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
4484 			size_t len)
4485 {
4486 	if (!wpa_auth || !wpa_auth->pmksa)
4487 		return 0;
4488 	return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
4489 }
4490 
4491 
wpa_auth_pmksa_flush(struct wpa_authenticator * wpa_auth)4492 void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
4493 {
4494 	if (wpa_auth && wpa_auth->pmksa)
4495 		pmksa_cache_auth_flush(wpa_auth->pmksa);
4496 }
4497 
4498 
4499 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4500 #ifdef CONFIG_MESH
4501 
wpa_auth_pmksa_list_mesh(struct wpa_authenticator * wpa_auth,const u8 * addr,char * buf,size_t len)4502 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
4503 			     char *buf, size_t len)
4504 {
4505 	if (!wpa_auth || !wpa_auth->pmksa)
4506 		return 0;
4507 
4508 	return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
4509 }
4510 
4511 
4512 struct rsn_pmksa_cache_entry *
wpa_auth_pmksa_create_entry(const u8 * aa,const u8 * spa,const u8 * pmk,const u8 * pmkid,int expiration)4513 wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
4514 			    const u8 *pmkid, int expiration)
4515 {
4516 	struct rsn_pmksa_cache_entry *entry;
4517 	struct os_reltime now;
4518 
4519 	entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa,
4520 					      spa, 0, NULL, WPA_KEY_MGMT_SAE);
4521 	if (!entry)
4522 		return NULL;
4523 
4524 	os_get_reltime(&now);
4525 	entry->expiration = now.sec + expiration;
4526 	return entry;
4527 }
4528 
4529 
wpa_auth_pmksa_add_entry(struct wpa_authenticator * wpa_auth,struct rsn_pmksa_cache_entry * entry)4530 int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
4531 			     struct rsn_pmksa_cache_entry *entry)
4532 {
4533 	int ret;
4534 
4535 	if (!wpa_auth || !wpa_auth->pmksa)
4536 		return -1;
4537 
4538 	ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
4539 	if (ret < 0)
4540 		wpa_printf(MSG_DEBUG,
4541 			   "RSN: Failed to store external PMKSA cache for "
4542 			   MACSTR, MAC2STR(entry->spa));
4543 
4544 	return ret;
4545 }
4546 
4547 #endif /* CONFIG_MESH */
4548 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4549 
4550 
4551 struct rsn_pmksa_cache_entry *
wpa_auth_pmksa_get(struct wpa_authenticator * wpa_auth,const u8 * sta_addr,const u8 * pmkid)4552 wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
4553 		   const u8 *pmkid)
4554 {
4555 	if (!wpa_auth || !wpa_auth->pmksa)
4556 		return NULL;
4557 	return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
4558 }
4559 
4560 
wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry * pmksa,struct wpa_state_machine * sm,struct wpa_authenticator * wpa_auth,u8 * pmkid,u8 * pmk)4561 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
4562 			      struct wpa_state_machine *sm,
4563 			      struct wpa_authenticator *wpa_auth,
4564 			      u8 *pmkid, u8 *pmk)
4565 {
4566 	if (!sm)
4567 		return;
4568 
4569 	sm->pmksa = pmksa;
4570 	os_memcpy(pmk, pmksa->pmk, PMK_LEN);
4571 	os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
4572 	os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
4573 }
4574 
4575 
4576 /*
4577  * Remove and free the group from wpa_authenticator. This is triggered by a
4578  * callback to make sure nobody is currently iterating the group list while it
4579  * gets modified.
4580  */
wpa_group_free(struct wpa_authenticator * wpa_auth,struct wpa_group * group)4581 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
4582 			   struct wpa_group *group)
4583 {
4584 	struct wpa_group *prev = wpa_auth->group;
4585 
4586 	wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
4587 		   group->vlan_id);
4588 
4589 	while (prev) {
4590 		if (prev->next == group) {
4591 			/* This never frees the special first group as needed */
4592 			prev->next = group->next;
4593 			os_free(group);
4594 			break;
4595 		}
4596 		prev = prev->next;
4597 	}
4598 
4599 }
4600 
4601 
4602 /* Increase the reference counter for group */
wpa_group_get(struct wpa_authenticator * wpa_auth,struct wpa_group * group)4603 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
4604 			  struct wpa_group *group)
4605 {
4606 	/* Skip the special first group */
4607 	if (wpa_auth->group == group)
4608 		return;
4609 
4610 	group->references++;
4611 }
4612 
4613 
4614 /* Decrease the reference counter and maybe free the group */
wpa_group_put(struct wpa_authenticator * wpa_auth,struct wpa_group * group)4615 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
4616 			  struct wpa_group *group)
4617 {
4618 	/* Skip the special first group */
4619 	if (wpa_auth->group == group)
4620 		return;
4621 
4622 	group->references--;
4623 	if (group->references)
4624 		return;
4625 	wpa_group_free(wpa_auth, group);
4626 }
4627 
4628 
4629 /*
4630  * Add a group that has its references counter set to zero. Caller needs to
4631  * call wpa_group_get() on the return value to mark the entry in use.
4632  */
4633 static struct wpa_group *
wpa_auth_add_group(struct wpa_authenticator * wpa_auth,int vlan_id)4634 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4635 {
4636 	struct wpa_group *group;
4637 
4638 	if (wpa_auth == NULL || wpa_auth->group == NULL)
4639 		return NULL;
4640 
4641 	wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
4642 		   vlan_id);
4643 	group = wpa_group_init(wpa_auth, vlan_id, 0);
4644 	if (group == NULL)
4645 		return NULL;
4646 
4647 	group->next = wpa_auth->group->next;
4648 	wpa_auth->group->next = group;
4649 
4650 	return group;
4651 }
4652 
4653 
4654 /*
4655  * Enforce that the group state machine for the VLAN is running, increase
4656  * reference counter as interface is up. References might have been increased
4657  * even if a negative value is returned.
4658  * Returns: -1 on error (group missing, group already failed); otherwise, 0
4659  */
wpa_auth_ensure_group(struct wpa_authenticator * wpa_auth,int vlan_id)4660 int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4661 {
4662 	struct wpa_group *group;
4663 
4664 	if (wpa_auth == NULL)
4665 		return 0;
4666 
4667 	group = wpa_auth->group;
4668 	while (group) {
4669 		if (group->vlan_id == vlan_id)
4670 			break;
4671 		group = group->next;
4672 	}
4673 
4674 	if (group == NULL) {
4675 		group = wpa_auth_add_group(wpa_auth, vlan_id);
4676 		if (group == NULL)
4677 			return -1;
4678 	}
4679 
4680 	wpa_printf(MSG_DEBUG,
4681 		   "WPA: Ensure group state machine running for VLAN ID %d",
4682 		   vlan_id);
4683 
4684 	wpa_group_get(wpa_auth, group);
4685 	group->num_setup_iface++;
4686 
4687 	if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4688 		return -1;
4689 
4690 	return 0;
4691 }
4692 
4693 
4694 /*
4695  * Decrease reference counter, expected to be zero afterwards.
4696  * returns: -1 on error (group not found, group in fail state)
4697  *          -2 if wpa_group is still referenced
4698  *           0 else
4699  */
wpa_auth_release_group(struct wpa_authenticator * wpa_auth,int vlan_id)4700 int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4701 {
4702 	struct wpa_group *group;
4703 	int ret = 0;
4704 
4705 	if (wpa_auth == NULL)
4706 		return 0;
4707 
4708 	group = wpa_auth->group;
4709 	while (group) {
4710 		if (group->vlan_id == vlan_id)
4711 			break;
4712 		group = group->next;
4713 	}
4714 
4715 	if (group == NULL)
4716 		return -1;
4717 
4718 	wpa_printf(MSG_DEBUG,
4719 		   "WPA: Try stopping group state machine for VLAN ID %d",
4720 		   vlan_id);
4721 
4722 	if (group->num_setup_iface <= 0) {
4723 		wpa_printf(MSG_ERROR,
4724 			   "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4725 			   vlan_id);
4726 		return -1;
4727 	}
4728 	group->num_setup_iface--;
4729 
4730 	if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4731 		ret = -1;
4732 
4733 	if (group->references > 1) {
4734 		wpa_printf(MSG_DEBUG,
4735 			   "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4736 			   vlan_id);
4737 		ret = -2;
4738 	}
4739 
4740 	wpa_group_put(wpa_auth, group);
4741 
4742 	return ret;
4743 }
4744 
4745 
wpa_auth_sta_set_vlan(struct wpa_state_machine * sm,int vlan_id)4746 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
4747 {
4748 	struct wpa_group *group;
4749 
4750 	if (sm == NULL || sm->wpa_auth == NULL)
4751 		return 0;
4752 
4753 	group = sm->wpa_auth->group;
4754 	while (group) {
4755 		if (group->vlan_id == vlan_id)
4756 			break;
4757 		group = group->next;
4758 	}
4759 
4760 	if (group == NULL) {
4761 		group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
4762 		if (group == NULL)
4763 			return -1;
4764 	}
4765 
4766 	if (sm->group == group)
4767 		return 0;
4768 
4769 	if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4770 		return -1;
4771 
4772 	wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
4773 		   "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
4774 
4775 	wpa_group_get(sm->wpa_auth, group);
4776 	wpa_group_put(sm->wpa_auth, sm->group);
4777 	sm->group = group;
4778 
4779 	return 0;
4780 }
4781 
4782 
wpa_auth_eapol_key_tx_status(struct wpa_authenticator * wpa_auth,struct wpa_state_machine * sm,int ack)4783 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
4784 				  struct wpa_state_machine *sm, int ack)
4785 {
4786 	if (wpa_auth == NULL || sm == NULL)
4787 		return;
4788 	wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
4789 		   " ack=%d", MAC2STR(sm->addr), ack);
4790 	if (sm->pending_1_of_4_timeout && ack) {
4791 		/*
4792 		 * Some deployed supplicant implementations update their SNonce
4793 		 * for each EAPOL-Key 2/4 message even within the same 4-way
4794 		 * handshake and then fail to use the first SNonce when
4795 		 * deriving the PTK. This results in unsuccessful 4-way
4796 		 * handshake whenever the relatively short initial timeout is
4797 		 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4798 		 * around this by increasing the timeout now that we know that
4799 		 * the station has received the frame.
4800 		 */
4801 		int timeout_ms = eapol_key_timeout_subseq;
4802 		wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
4803 			   "timeout by %u ms because of acknowledged frame",
4804 			   timeout_ms);
4805 		eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
4806 		eloop_register_timeout(timeout_ms / 1000,
4807 				       (timeout_ms % 1000) * 1000,
4808 				       wpa_send_eapol_timeout, wpa_auth, sm);
4809 	}
4810 
4811 #ifdef CONFIG_TESTING_OPTIONS
4812 	if (sm->eapol_status_cb) {
4813 		sm->eapol_status_cb(sm->eapol_status_cb_ctx1,
4814 				    sm->eapol_status_cb_ctx2);
4815 		sm->eapol_status_cb = NULL;
4816 	}
4817 #endif /* CONFIG_TESTING_OPTIONS */
4818 }
4819 
4820 
wpa_auth_uses_sae(struct wpa_state_machine * sm)4821 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
4822 {
4823 	if (sm == NULL)
4824 		return 0;
4825 	return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
4826 }
4827 
4828 
wpa_auth_uses_ft_sae(struct wpa_state_machine * sm)4829 int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
4830 {
4831 	if (sm == NULL)
4832 		return 0;
4833 	return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
4834 }
4835 
4836 
4837 #ifdef CONFIG_P2P
wpa_auth_get_ip_addr(struct wpa_state_machine * sm,u8 * addr)4838 int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
4839 {
4840 	if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
4841 		return -1;
4842 	os_memcpy(addr, sm->ip_addr, 4);
4843 	return 0;
4844 }
4845 #endif /* CONFIG_P2P */
4846 
4847 
wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator * wpa_auth,struct radius_das_attrs * attr)4848 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
4849 					 struct radius_das_attrs *attr)
4850 {
4851 	return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
4852 }
4853 
4854 
wpa_auth_reconfig_group_keys(struct wpa_authenticator * wpa_auth)4855 void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
4856 {
4857 	struct wpa_group *group;
4858 
4859 	if (!wpa_auth)
4860 		return;
4861 	for (group = wpa_auth->group; group; group = group->next)
4862 		wpa_group_config_group_keys(wpa_auth, group);
4863 }
4864 
4865 
4866 #ifdef CONFIG_FILS
4867 
4868 struct wpa_auth_fils_iter_data {
4869 	struct wpa_authenticator *auth;
4870 	const u8 *cache_id;
4871 	struct rsn_pmksa_cache_entry *pmksa;
4872 	const u8 *spa;
4873 	const u8 *pmkid;
4874 };
4875 
4876 
wpa_auth_fils_iter(struct wpa_authenticator * a,void * ctx)4877 static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
4878 {
4879 	struct wpa_auth_fils_iter_data *data = ctx;
4880 
4881 	if (a == data->auth || !a->conf.fils_cache_id_set ||
4882 	    os_memcmp(a->conf.fils_cache_id, data->cache_id,
4883 		      FILS_CACHE_ID_LEN) != 0)
4884 		return 0;
4885 	data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
4886 	return data->pmksa != NULL;
4887 }
4888 
4889 
4890 struct rsn_pmksa_cache_entry *
wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator * wpa_auth,const u8 * sta_addr,const u8 * pmkid)4891 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
4892 				 const u8 *sta_addr, const u8 *pmkid)
4893 {
4894 	struct wpa_auth_fils_iter_data idata;
4895 
4896 	if (!wpa_auth->conf.fils_cache_id_set)
4897 		return NULL;
4898 	idata.auth = wpa_auth;
4899 	idata.cache_id = wpa_auth->conf.fils_cache_id;
4900 	idata.pmksa = NULL;
4901 	idata.spa = sta_addr;
4902 	idata.pmkid = pmkid;
4903 	wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
4904 	return idata.pmksa;
4905 }
4906 
4907 
4908 #ifdef CONFIG_IEEE80211R_AP
wpa_auth_write_fte(struct wpa_authenticator * wpa_auth,int use_sha384,u8 * buf,size_t len)4909 int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, int use_sha384,
4910 		       u8 *buf, size_t len)
4911 {
4912 	struct wpa_auth_config *conf = &wpa_auth->conf;
4913 
4914 	return wpa_write_ftie(conf, use_sha384, conf->r0_key_holder,
4915 			      conf->r0_key_holder_len,
4916 			      NULL, NULL, buf, len, NULL, 0);
4917 }
4918 #endif /* CONFIG_IEEE80211R_AP */
4919 
4920 
wpa_auth_get_fils_aead_params(struct wpa_state_machine * sm,u8 * fils_anonce,u8 * fils_snonce,u8 * fils_kek,size_t * fils_kek_len)4921 void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm,
4922 				   u8 *fils_anonce, u8 *fils_snonce,
4923 				   u8 *fils_kek, size_t *fils_kek_len)
4924 {
4925 	os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN);
4926 	os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN);
4927 	os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN);
4928 	*fils_kek_len = sm->PTK.kek_len;
4929 }
4930 
4931 
wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine * sm,const u8 * pmk,size_t pmk_len,const u8 * pmkid)4932 void wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine *sm, const u8 *pmk,
4933 				 size_t pmk_len, const u8 *pmkid)
4934 {
4935 	os_memcpy(sm->PMK, pmk, pmk_len);
4936 	sm->pmk_len = pmk_len;
4937 	os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
4938 	sm->pmkid_set = 1;
4939 }
4940 
4941 #endif /* CONFIG_FILS */
4942 
4943 
wpa_auth_set_auth_alg(struct wpa_state_machine * sm,u16 auth_alg)4944 void wpa_auth_set_auth_alg(struct wpa_state_machine *sm, u16 auth_alg)
4945 {
4946 	if (sm)
4947 		sm->auth_alg = auth_alg;
4948 }
4949 
4950 
4951 #ifdef CONFIG_DPP2
wpa_auth_set_dpp_z(struct wpa_state_machine * sm,const struct wpabuf * z)4952 void wpa_auth_set_dpp_z(struct wpa_state_machine *sm, const struct wpabuf *z)
4953 {
4954 	if (sm) {
4955 		wpabuf_clear_free(sm->dpp_z);
4956 		sm->dpp_z = z ? wpabuf_dup(z) : NULL;
4957 	}
4958 }
4959 #endif /* CONFIG_DPP2 */
4960 
4961 
4962 #ifdef CONFIG_TESTING_OPTIONS
4963 
wpa_auth_resend_m1(struct wpa_state_machine * sm,int change_anonce,void (* cb)(void * ctx1,void * ctx2),void * ctx1,void * ctx2)4964 int wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce,
4965 		       void (*cb)(void *ctx1, void *ctx2),
4966 		       void *ctx1, void *ctx2)
4967 {
4968 	const u8 *anonce = sm->ANonce;
4969 	u8 anonce_buf[WPA_NONCE_LEN];
4970 
4971 	if (change_anonce) {
4972 		if (random_get_bytes(anonce_buf, WPA_NONCE_LEN))
4973 			return -1;
4974 		anonce = anonce_buf;
4975 	}
4976 
4977 	wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4978 			"sending 1/4 msg of 4-Way Handshake (TESTING)");
4979 	wpa_send_eapol(sm->wpa_auth, sm,
4980 		       WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
4981 		       anonce, NULL, 0, 0, 0);
4982 	return 0;
4983 }
4984 
4985 
wpa_auth_resend_m3(struct wpa_state_machine * sm,void (* cb)(void * ctx1,void * ctx2),void * ctx1,void * ctx2)4986 int wpa_auth_resend_m3(struct wpa_state_machine *sm,
4987 		       void (*cb)(void *ctx1, void *ctx2),
4988 		       void *ctx1, void *ctx2)
4989 {
4990 	u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
4991 #ifdef CONFIG_IEEE80211W
4992 	u8 *opos;
4993 #endif /* CONFIG_IEEE80211W */
4994 	size_t gtk_len, kde_len;
4995 	struct wpa_group *gsm = sm->group;
4996 	u8 *wpa_ie;
4997 	int wpa_ie_len, secure, keyidx, encr = 0;
4998 
4999 	/* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
5000 	   GTK[GN], IGTK, [FTIE], [TIE * 2])
5001 	 */
5002 
5003 	/* Use 0 RSC */
5004 	os_memset(rsc, 0, WPA_KEY_RSC_LEN);
5005 	/* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
5006 	wpa_ie = sm->wpa_auth->wpa_ie;
5007 	wpa_ie_len = sm->wpa_auth->wpa_ie_len;
5008 	if (sm->wpa == WPA_VERSION_WPA &&
5009 	    (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
5010 	    wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
5011 		/* WPA-only STA, remove RSN IE and possible MDIE */
5012 		wpa_ie = wpa_ie + wpa_ie[1] + 2;
5013 		if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
5014 			wpa_ie = wpa_ie + wpa_ie[1] + 2;
5015 		wpa_ie_len = wpa_ie[1] + 2;
5016 	}
5017 	wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5018 			"sending 3/4 msg of 4-Way Handshake (TESTING)");
5019 	if (sm->wpa == WPA_VERSION_WPA2) {
5020 		/* WPA2 send GTK in the 4-way handshake */
5021 		secure = 1;
5022 		gtk = gsm->GTK[gsm->GN - 1];
5023 		gtk_len = gsm->GTK_len;
5024 		keyidx = gsm->GN;
5025 		_rsc = rsc;
5026 		encr = 1;
5027 	} else {
5028 		/* WPA does not include GTK in msg 3/4 */
5029 		secure = 0;
5030 		gtk = NULL;
5031 		gtk_len = 0;
5032 		keyidx = 0;
5033 		_rsc = NULL;
5034 		if (sm->rx_eapol_key_secure) {
5035 			/*
5036 			 * It looks like Windows 7 supplicant tries to use
5037 			 * Secure bit in msg 2/4 after having reported Michael
5038 			 * MIC failure and it then rejects the 4-way handshake
5039 			 * if msg 3/4 does not set Secure bit. Work around this
5040 			 * by setting the Secure bit here even in the case of
5041 			 * WPA if the supplicant used it first.
5042 			 */
5043 			wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5044 					"STA used Secure bit in WPA msg 2/4 - "
5045 					"set Secure for 3/4 as workaround");
5046 			secure = 1;
5047 		}
5048 	}
5049 
5050 	kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
5051 	if (gtk)
5052 		kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
5053 #ifdef CONFIG_IEEE80211R_AP
5054 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5055 		kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
5056 		kde_len += 300; /* FTIE + 2 * TIE */
5057 	}
5058 #endif /* CONFIG_IEEE80211R_AP */
5059 	kde = os_malloc(kde_len);
5060 	if (kde == NULL)
5061 		return -1;
5062 
5063 	pos = kde;
5064 	os_memcpy(pos, wpa_ie, wpa_ie_len);
5065 	pos += wpa_ie_len;
5066 #ifdef CONFIG_IEEE80211R_AP
5067 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5068 		int res;
5069 		size_t elen;
5070 
5071 		elen = pos - kde;
5072 		res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
5073 		if (res < 0) {
5074 			wpa_printf(MSG_ERROR, "FT: Failed to insert "
5075 				   "PMKR1Name into RSN IE in EAPOL-Key data");
5076 			os_free(kde);
5077 			return -1;
5078 		}
5079 		pos -= wpa_ie_len;
5080 		pos += elen;
5081 	}
5082 #endif /* CONFIG_IEEE80211R_AP */
5083 	if (gtk) {
5084 		u8 hdr[2];
5085 		hdr[0] = keyidx & 0x03;
5086 		hdr[1] = 0;
5087 		pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
5088 				  gtk, gtk_len);
5089 	}
5090 #ifdef CONFIG_IEEE80211W
5091 	opos = pos;
5092 	pos = ieee80211w_kde_add(sm, pos);
5093 	if (pos - opos >= 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
5094 		/* skip KDE header and keyid */
5095 		opos += 2 + RSN_SELECTOR_LEN + 2;
5096 		os_memset(opos, 0, 6); /* clear PN */
5097 	}
5098 #endif /* CONFIG_IEEE80211W */
5099 	if (ocv_oci_add(sm, &pos) < 0) {
5100 		os_free(kde);
5101 		return -1;
5102 	}
5103 
5104 #ifdef CONFIG_IEEE80211R_AP
5105 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5106 		int res;
5107 		struct wpa_auth_config *conf;
5108 
5109 		conf = &sm->wpa_auth->conf;
5110 		if (sm->assoc_resp_ftie &&
5111 		    kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
5112 			os_memcpy(pos, sm->assoc_resp_ftie,
5113 				  2 + sm->assoc_resp_ftie[1]);
5114 			res = 2 + sm->assoc_resp_ftie[1];
5115 		} else {
5116 			int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
5117 
5118 			res = wpa_write_ftie(conf, use_sha384,
5119 					     conf->r0_key_holder,
5120 					     conf->r0_key_holder_len,
5121 					     NULL, NULL, pos,
5122 					     kde + kde_len - pos,
5123 					     NULL, 0);
5124 		}
5125 		if (res < 0) {
5126 			wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
5127 				   "into EAPOL-Key Key Data");
5128 			os_free(kde);
5129 			return -1;
5130 		}
5131 		pos += res;
5132 
5133 		/* TIE[ReassociationDeadline] (TU) */
5134 		*pos++ = WLAN_EID_TIMEOUT_INTERVAL;
5135 		*pos++ = 5;
5136 		*pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
5137 		WPA_PUT_LE32(pos, conf->reassociation_deadline);
5138 		pos += 4;
5139 
5140 		/* TIE[KeyLifetime] (seconds) */
5141 		*pos++ = WLAN_EID_TIMEOUT_INTERVAL;
5142 		*pos++ = 5;
5143 		*pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
5144 		WPA_PUT_LE32(pos, conf->r0_key_lifetime);
5145 		pos += 4;
5146 	}
5147 #endif /* CONFIG_IEEE80211R_AP */
5148 
5149 	wpa_send_eapol(sm->wpa_auth, sm,
5150 		       (secure ? WPA_KEY_INFO_SECURE : 0) |
5151 		       (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5152 			WPA_KEY_INFO_MIC : 0) |
5153 		       WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
5154 		       WPA_KEY_INFO_KEY_TYPE,
5155 		       _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
5156 	os_free(kde);
5157 	return 0;
5158 }
5159 
5160 
wpa_auth_resend_group_m1(struct wpa_state_machine * sm,void (* cb)(void * ctx1,void * ctx2),void * ctx1,void * ctx2)5161 int wpa_auth_resend_group_m1(struct wpa_state_machine *sm,
5162 			     void (*cb)(void *ctx1, void *ctx2),
5163 			     void *ctx1, void *ctx2)
5164 {
5165 	u8 rsc[WPA_KEY_RSC_LEN];
5166 	struct wpa_group *gsm = sm->group;
5167 	const u8 *kde;
5168 	u8 *kde_buf = NULL, *pos, hdr[2];
5169 #ifdef CONFIG_IEEE80211W
5170 	u8 *opos;
5171 #endif /* CONFIG_IEEE80211W */
5172 	size_t kde_len;
5173 	u8 *gtk;
5174 
5175 	/* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
5176 	os_memset(rsc, 0, WPA_KEY_RSC_LEN);
5177 	/* Use 0 RSC */
5178 	wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5179 			"sending 1/2 msg of Group Key Handshake (TESTING)");
5180 
5181 	gtk = gsm->GTK[gsm->GN - 1];
5182 	if (sm->wpa == WPA_VERSION_WPA2) {
5183 		kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
5184 			ieee80211w_kde_len(sm) + ocv_oci_len(sm);
5185 		kde_buf = os_malloc(kde_len);
5186 		if (kde_buf == NULL)
5187 			return -1;
5188 
5189 		kde = pos = kde_buf;
5190 		hdr[0] = gsm->GN & 0x03;
5191 		hdr[1] = 0;
5192 		pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
5193 				  gtk, gsm->GTK_len);
5194 #ifdef CONFIG_IEEE80211W
5195 		opos = pos;
5196 		pos = ieee80211w_kde_add(sm, pos);
5197 		if (pos - opos >=
5198 		    2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
5199 			/* skip KDE header and keyid */
5200 			opos += 2 + RSN_SELECTOR_LEN + 2;
5201 			os_memset(opos, 0, 6); /* clear PN */
5202 		}
5203 #endif /* CONFIG_IEEE80211W */
5204 		if (ocv_oci_add(sm, &pos) < 0) {
5205 			os_free(kde_buf);
5206 			return -1;
5207 		}
5208 		kde_len = pos - kde;
5209 	} else {
5210 		kde = gtk;
5211 		kde_len = gsm->GTK_len;
5212 	}
5213 
5214 	sm->eapol_status_cb = cb;
5215 	sm->eapol_status_cb_ctx1 = ctx1;
5216 	sm->eapol_status_cb_ctx2 = ctx2;
5217 
5218 	wpa_send_eapol(sm->wpa_auth, sm,
5219 		       WPA_KEY_INFO_SECURE |
5220 		       (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5221 			WPA_KEY_INFO_MIC : 0) |
5222 		       WPA_KEY_INFO_ACK |
5223 		       (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
5224 		       rsc, NULL, kde, kde_len, gsm->GN, 1);
5225 
5226 	os_free(kde_buf);
5227 	return 0;
5228 }
5229 
5230 
wpa_auth_rekey_gtk(struct wpa_authenticator * wpa_auth)5231 int wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth)
5232 {
5233 	if (!wpa_auth)
5234 		return -1;
5235 	eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
5236 	return eloop_register_timeout(0, 0, wpa_rekey_gtk, wpa_auth, NULL);
5237 }
5238 
5239 #endif /* CONFIG_TESTING_OPTIONS */
5240