• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * IEEE 802.11 RSN / WPA Authenticator
3  * Copyright (c) 2004-2022, 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 "common/dpp.h"
18 #include "common/wpa_ctrl.h"
19 #include "crypto/aes.h"
20 #include "crypto/aes_wrap.h"
21 #include "crypto/aes_siv.h"
22 #include "crypto/crypto.h"
23 #include "crypto/sha1.h"
24 #include "crypto/sha256.h"
25 #include "crypto/sha384.h"
26 #include "crypto/sha512.h"
27 #include "crypto/random.h"
28 #include "eapol_auth/eapol_auth_sm.h"
29 #include "drivers/driver.h"
30 #include "ap_config.h"
31 #include "ieee802_11.h"
32 #include "sta_info.h"
33 #include "wpa_auth.h"
34 #include "pmksa_cache_auth.h"
35 #include "wpa_auth_i.h"
36 #include "wpa_auth_ie.h"
37 
38 #define STATE_MACHINE_DATA struct wpa_state_machine
39 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
40 #define STATE_MACHINE_ADDR wpa_auth_get_spa(sm)
41 
42 
43 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
44 static int wpa_sm_step(struct wpa_state_machine *sm);
45 static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
46 			      u8 *data, size_t data_len);
47 #ifdef CONFIG_FILS
48 static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
49 			    u8 *buf, size_t buf_len, u16 *_key_data_len);
50 static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
51 					     const struct wpabuf *hlp);
52 #endif /* CONFIG_FILS */
53 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
54 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
55 			      struct wpa_group *group);
56 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
57 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
58 			  struct wpa_group *group);
59 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
60 				       struct wpa_group *group);
61 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
62 			  const u8 *pmk, unsigned int pmk_len,
63 			  struct wpa_ptk *ptk, int force_sha256,
64 			  u8 *pmk_r0, u8 *pmk_r1, u8 *pmk_r0_name,
65 			  size_t *key_len);
66 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
67 			   struct wpa_group *group);
68 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
69 			  struct wpa_group *group);
70 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
71 			  struct wpa_group *group);
72 static int ieee80211w_kde_len(struct wpa_state_machine *sm);
73 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos);
74 
75 static const u32 eapol_key_timeout_first = 100; /* ms */
76 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
77 static const u32 eapol_key_timeout_first_group = 500; /* ms */
78 static const u32 eapol_key_timeout_no_retrans = 4000; /* ms */
79 
80 /* TODO: make these configurable */
81 static const int dot11RSNAConfigPMKLifetime = 43200;
82 static const int dot11RSNAConfigPMKReauthThreshold = 70;
83 static const int dot11RSNAConfigSATimeout = 60;
84 
85 
wpa_auth_get_aa(const struct wpa_state_machine * sm)86 static const u8 * wpa_auth_get_aa(const struct wpa_state_machine *sm)
87 {
88 #ifdef CONFIG_IEEE80211BE
89 	if (sm->mld_assoc_link_id >= 0)
90 		return sm->own_mld_addr;
91 #endif /* CONFIG_IEEE80211BE */
92 	return sm->wpa_auth->addr;
93 }
94 
95 
wpa_auth_get_spa(const struct wpa_state_machine * sm)96 static const u8 * wpa_auth_get_spa(const struct wpa_state_machine *sm)
97 {
98 #ifdef CONFIG_IEEE80211BE
99 	if (sm->mld_assoc_link_id >= 0)
100 		return sm->peer_mld_addr;
101 #endif /* CONFIG_IEEE80211BE */
102 	return sm->addr;
103 }
104 
105 
wpa_auth_mic_failure_report(struct wpa_authenticator * wpa_auth,const u8 * addr)106 static inline int wpa_auth_mic_failure_report(
107 	struct wpa_authenticator *wpa_auth, const u8 *addr)
108 {
109 	if (wpa_auth->cb->mic_failure_report)
110 		return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr);
111 	return 0;
112 }
113 
114 
wpa_auth_psk_failure_report(struct wpa_authenticator * wpa_auth,const u8 * addr)115 static inline void wpa_auth_psk_failure_report(
116 	struct wpa_authenticator *wpa_auth, const u8 *addr)
117 {
118 	if (wpa_auth->cb->psk_failure_report)
119 		wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr);
120 }
121 
122 
wpa_auth_set_eapol(struct wpa_authenticator * wpa_auth,const u8 * addr,wpa_eapol_variable var,int value)123 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
124 				      const u8 *addr, wpa_eapol_variable var,
125 				      int value)
126 {
127 	if (wpa_auth->cb->set_eapol)
128 		wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value);
129 }
130 
131 
wpa_auth_get_eapol(struct wpa_authenticator * wpa_auth,const u8 * addr,wpa_eapol_variable var)132 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
133 				     const u8 *addr, wpa_eapol_variable var)
134 {
135 	if (!wpa_auth->cb->get_eapol)
136 		return -1;
137 	return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var);
138 }
139 
140 
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)141 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
142 					  const u8 *addr,
143 					  const u8 *p2p_dev_addr,
144 					  const u8 *prev_psk, size_t *psk_len,
145 					  int *vlan_id)
146 {
147 	if (!wpa_auth->cb->get_psk)
148 		return NULL;
149 	return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
150 				     prev_psk, psk_len, vlan_id);
151 }
152 
153 
wpa_auth_get_msk(struct wpa_authenticator * wpa_auth,const u8 * addr,u8 * msk,size_t * len)154 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
155 				   const u8 *addr, u8 *msk, size_t *len)
156 {
157 	if (!wpa_auth->cb->get_msk)
158 		return -1;
159 	return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
160 }
161 
162 
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,enum key_flag key_flag)163 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
164 				   int vlan_id,
165 				   enum wpa_alg alg, const u8 *addr, int idx,
166 				   u8 *key, size_t key_len,
167 				   enum key_flag key_flag)
168 {
169 	if (!wpa_auth->cb->set_key)
170 		return -1;
171 	return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
172 				     key, key_len, key_flag);
173 }
174 
175 
176 #ifdef CONFIG_PASN
wpa_auth_set_ltf_keyseed(struct wpa_authenticator * wpa_auth,const u8 * peer_addr,const u8 * ltf_keyseed,size_t ltf_keyseed_len)177 static inline int wpa_auth_set_ltf_keyseed(struct wpa_authenticator *wpa_auth,
178 					   const u8 *peer_addr,
179 					   const u8 *ltf_keyseed,
180 					   size_t ltf_keyseed_len)
181 {
182 	if (!wpa_auth->cb->set_ltf_keyseed)
183 		return -1;
184 	return wpa_auth->cb->set_ltf_keyseed(wpa_auth->cb_ctx, peer_addr,
185 					     ltf_keyseed, ltf_keyseed_len);
186 }
187 #endif /* CONFIG_PASN */
188 
189 
wpa_auth_get_seqnum(struct wpa_authenticator * wpa_auth,const u8 * addr,int idx,u8 * seq)190 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
191 				      const u8 *addr, int idx, u8 *seq)
192 {
193 	int res;
194 
195 	if (!wpa_auth->cb->get_seqnum)
196 		return -1;
197 	res = wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
198 #ifdef CONFIG_TESTING_OPTIONS
199 	if (!addr && idx < 4 && wpa_auth->conf.gtk_rsc_override_set) {
200 		wpa_printf(MSG_DEBUG,
201 			   "TESTING: Override GTK RSC %016llx --> %016llx",
202 			   (long long unsigned) WPA_GET_LE64(seq),
203 			   (long long unsigned)
204 			   WPA_GET_LE64(wpa_auth->conf.gtk_rsc_override));
205 		os_memcpy(seq, wpa_auth->conf.gtk_rsc_override,
206 			  WPA_KEY_RSC_LEN);
207 	}
208 	if (!addr && idx >= 4 && idx <= 5 &&
209 	    wpa_auth->conf.igtk_rsc_override_set) {
210 		wpa_printf(MSG_DEBUG,
211 			   "TESTING: Override IGTK RSC %016llx --> %016llx",
212 			   (long long unsigned) WPA_GET_LE64(seq),
213 			   (long long unsigned)
214 			   WPA_GET_LE64(wpa_auth->conf.igtk_rsc_override));
215 		os_memcpy(seq, wpa_auth->conf.igtk_rsc_override,
216 			  WPA_KEY_RSC_LEN);
217 	}
218 #endif /* CONFIG_TESTING_OPTIONS */
219 	return res;
220 }
221 
222 
223 static inline int
wpa_auth_send_eapol(struct wpa_authenticator * wpa_auth,const u8 * addr,const u8 * data,size_t data_len,int encrypt)224 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
225 		    const u8 *data, size_t data_len, int encrypt)
226 {
227 	if (!wpa_auth->cb->send_eapol)
228 		return -1;
229 #ifdef CONFIG_TESTING_OPTIONS
230 	if (wpa_auth->conf.skip_send_eapol)
231 		return 0;
232 #endif
233 	return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
234 					encrypt);
235 }
236 
237 
238 #ifdef CONFIG_MESH
wpa_auth_start_ampe(struct wpa_authenticator * wpa_auth,const u8 * addr)239 static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
240 				      const u8 *addr)
241 {
242 	if (!wpa_auth->cb->start_ampe)
243 		return -1;
244 	return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
245 }
246 #endif /* CONFIG_MESH */
247 
248 
wpa_auth_for_each_sta(struct wpa_authenticator * wpa_auth,int (* cb)(struct wpa_state_machine * sm,void * ctx),void * cb_ctx)249 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
250 			  int (*cb)(struct wpa_state_machine *sm, void *ctx),
251 			  void *cb_ctx)
252 {
253 	if (!wpa_auth->cb->for_each_sta)
254 		return 0;
255 	return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
256 }
257 
258 
wpa_auth_for_each_auth(struct wpa_authenticator * wpa_auth,int (* cb)(struct wpa_authenticator * a,void * ctx),void * cb_ctx)259 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
260 			   int (*cb)(struct wpa_authenticator *a, void *ctx),
261 			   void *cb_ctx)
262 {
263 	if (!wpa_auth->cb->for_each_auth)
264 		return 0;
265 	return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
266 }
267 
268 
wpa_auth_store_ptksa(struct wpa_authenticator * wpa_auth,const u8 * addr,int cipher,u32 life_time,const struct wpa_ptk * ptk)269 void wpa_auth_store_ptksa(struct wpa_authenticator *wpa_auth,
270 			  const u8 *addr, int cipher,
271 			  u32 life_time, const struct wpa_ptk *ptk)
272 {
273 	if (wpa_auth->cb->store_ptksa)
274 		wpa_auth->cb->store_ptksa(wpa_auth->cb_ctx, addr, cipher,
275 					  life_time, ptk);
276 }
277 
278 
wpa_auth_remove_ptksa(struct wpa_authenticator * wpa_auth,const u8 * addr,int cipher)279 static void wpa_auth_remove_ptksa(struct wpa_authenticator *wpa_auth,
280 				  const u8 *addr, int cipher)
281 {
282 	if (wpa_auth->cb->clear_ptksa)
283 		wpa_auth->cb->clear_ptksa(wpa_auth->cb_ctx, addr, cipher);
284 }
285 
286 
wpa_auth_logger(struct wpa_authenticator * wpa_auth,const u8 * addr,logger_level level,const char * txt)287 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
288 		     logger_level level, const char *txt)
289 {
290 	if (!wpa_auth->cb->logger)
291 		return;
292 	wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
293 }
294 
295 
wpa_auth_vlogger(struct wpa_authenticator * wpa_auth,const u8 * addr,logger_level level,const char * fmt,...)296 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
297 		      logger_level level, const char *fmt, ...)
298 {
299 	char *format;
300 	int maxlen;
301 	va_list ap;
302 
303 	if (!wpa_auth->cb->logger)
304 		return;
305 
306 	maxlen = os_strlen(fmt) + 100;
307 	format = os_malloc(maxlen);
308 	if (!format)
309 		return;
310 
311 	va_start(ap, fmt);
312 	vsnprintf(format, maxlen, fmt, ap);
313 	va_end(ap);
314 
315 	wpa_auth_logger(wpa_auth, addr, level, format);
316 
317 	os_free(format);
318 }
319 
320 
wpa_sta_disconnect(struct wpa_authenticator * wpa_auth,const u8 * addr,u16 reason)321 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
322 			       const u8 *addr, u16 reason)
323 {
324 	if (!wpa_auth->cb->disconnect)
325 		return;
326 	wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR " (reason %u)",
327 		   MAC2STR(addr), reason);
328 	wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr, reason);
329 }
330 
331 
332 #ifdef CONFIG_OCV
wpa_channel_info(struct wpa_authenticator * wpa_auth,struct wpa_channel_info * ci)333 static int wpa_channel_info(struct wpa_authenticator *wpa_auth,
334 			    struct wpa_channel_info *ci)
335 {
336 	if (!wpa_auth->cb->channel_info)
337 		return -1;
338 	return wpa_auth->cb->channel_info(wpa_auth->cb_ctx, ci);
339 }
340 #endif /* CONFIG_OCV */
341 
342 
wpa_auth_update_vlan(struct wpa_authenticator * wpa_auth,const u8 * addr,int vlan_id)343 static int wpa_auth_update_vlan(struct wpa_authenticator *wpa_auth,
344 				const u8 *addr, int vlan_id)
345 {
346 	if (!wpa_auth->cb->update_vlan)
347 		return -1;
348 	return wpa_auth->cb->update_vlan(wpa_auth->cb_ctx, addr, vlan_id);
349 }
350 
351 
wpa_rekey_gmk(void * eloop_ctx,void * timeout_ctx)352 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
353 {
354 	struct wpa_authenticator *wpa_auth = eloop_ctx;
355 
356 	if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
357 		wpa_printf(MSG_ERROR,
358 			   "Failed to get random data for WPA initialization.");
359 	} else {
360 		wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
361 		wpa_hexdump_key(MSG_DEBUG, "GMK",
362 				wpa_auth->group->GMK, WPA_GMK_LEN);
363 	}
364 
365 	if (wpa_auth->conf.wpa_gmk_rekey) {
366 		eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
367 				       wpa_rekey_gmk, wpa_auth, NULL);
368 	}
369 }
370 
371 
wpa_rekey_gtk(void * eloop_ctx,void * timeout_ctx)372 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
373 {
374 	struct wpa_authenticator *wpa_auth = eloop_ctx;
375 	struct wpa_group *group, *next;
376 
377 	wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
378 	group = wpa_auth->group;
379 	while (group) {
380 		wpa_group_get(wpa_auth, group);
381 
382 		group->GTKReKey = true;
383 		do {
384 			group->changed = false;
385 			wpa_group_sm_step(wpa_auth, group);
386 		} while (group->changed);
387 
388 		next = group->next;
389 		wpa_group_put(wpa_auth, group);
390 		group = next;
391 	}
392 
393 	if (wpa_auth->conf.wpa_group_rekey) {
394 		eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
395 				       0, wpa_rekey_gtk, wpa_auth, NULL);
396 	}
397 }
398 
399 
wpa_rekey_ptk(void * eloop_ctx,void * timeout_ctx)400 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
401 {
402 	struct wpa_authenticator *wpa_auth = eloop_ctx;
403 	struct wpa_state_machine *sm = timeout_ctx;
404 
405 	wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
406 			"rekeying PTK");
407 	wpa_request_new_ptk(sm);
408 	wpa_sm_step(sm);
409 }
410 
411 
wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine * sm)412 void wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine *sm)
413 {
414 	if (sm && sm->wpa_auth->conf.wpa_ptk_rekey) {
415 		wpa_printf(MSG_DEBUG, "WPA: Start PTK rekeying timer for "
416 			   MACSTR " (%d seconds)",
417 			   MAC2STR(wpa_auth_get_spa(sm)),
418 			   sm->wpa_auth->conf.wpa_ptk_rekey);
419 		eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
420 		eloop_register_timeout(sm->wpa_auth->conf.wpa_ptk_rekey, 0,
421 				       wpa_rekey_ptk, sm->wpa_auth, sm);
422 	}
423 }
424 
425 
wpa_auth_pmksa_clear_cb(struct wpa_state_machine * sm,void * ctx)426 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
427 {
428 	if (sm->pmksa == ctx)
429 		sm->pmksa = NULL;
430 	return 0;
431 }
432 
433 
wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry * entry,void * ctx)434 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
435 				   void *ctx)
436 {
437 	struct wpa_authenticator *wpa_auth = ctx;
438 	wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
439 }
440 
441 
wpa_group_init_gmk_and_counter(struct wpa_authenticator * wpa_auth,struct wpa_group * group)442 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
443 					  struct wpa_group *group)
444 {
445 	u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
446 	u8 rkey[32];
447 	unsigned long ptr;
448 
449 	if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
450 		return -1;
451 	wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
452 
453 	/*
454 	 * Counter = PRF-256(Random number, "Init Counter",
455 	 *                   Local MAC Address || Time)
456 	 */
457 	os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
458 	wpa_get_ntp_timestamp(buf + ETH_ALEN);
459 	ptr = (unsigned long) group;
460 	os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
461 #ifdef TEST_FUZZ
462 	os_memset(buf + ETH_ALEN, 0xab, 8);
463 	os_memset(buf + ETH_ALEN + 8, 0xcd, sizeof(ptr));
464 #endif /* TEST_FUZZ */
465 	if (random_get_bytes(rkey, sizeof(rkey)) < 0)
466 		return -1;
467 
468 	if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
469 		     group->Counter, WPA_NONCE_LEN) < 0)
470 		return -1;
471 	wpa_hexdump_key(MSG_DEBUG, "Key Counter",
472 			group->Counter, WPA_NONCE_LEN);
473 
474 	return 0;
475 }
476 
477 
wpa_group_init(struct wpa_authenticator * wpa_auth,int vlan_id,int delay_init)478 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
479 					 int vlan_id, int delay_init)
480 {
481 	struct wpa_group *group;
482 
483 	group = os_zalloc(sizeof(struct wpa_group));
484 	if (!group)
485 		return NULL;
486 
487 	group->GTKAuthenticator = true;
488 	group->vlan_id = vlan_id;
489 	group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
490 
491 	if (random_pool_ready() != 1) {
492 		wpa_printf(MSG_INFO,
493 			   "WPA: Not enough entropy in random pool for secure operations - update keys later when the first station connects");
494 	}
495 
496 	/*
497 	 * Set initial GMK/Counter value here. The actual values that will be
498 	 * used in negotiations will be set once the first station tries to
499 	 * connect. This allows more time for collecting additional randomness
500 	 * on embedded devices.
501 	 */
502 	if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
503 		wpa_printf(MSG_ERROR,
504 			   "Failed to get random data for WPA initialization.");
505 		os_free(group);
506 		return NULL;
507 	}
508 
509 	group->GInit = true;
510 	if (delay_init) {
511 		wpa_printf(MSG_DEBUG,
512 			   "WPA: Delay group state machine start until Beacon frames have been configured");
513 		/* Initialization is completed in wpa_init_keys(). */
514 	} else {
515 		wpa_group_sm_step(wpa_auth, group);
516 		group->GInit = false;
517 		wpa_group_sm_step(wpa_auth, group);
518 	}
519 
520 	return group;
521 }
522 
523 
524 /**
525  * wpa_init - Initialize WPA authenticator
526  * @addr: Authenticator address
527  * @conf: Configuration for WPA authenticator
528  * @cb: Callback functions for WPA authenticator
529  * Returns: Pointer to WPA authenticator data or %NULL on failure
530  */
wpa_init(const u8 * addr,struct wpa_auth_config * conf,const struct wpa_auth_callbacks * cb,void * cb_ctx)531 struct wpa_authenticator * wpa_init(const u8 *addr,
532 				    struct wpa_auth_config *conf,
533 				    const struct wpa_auth_callbacks *cb,
534 				    void *cb_ctx)
535 {
536 	struct wpa_authenticator *wpa_auth;
537 
538 	wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
539 	if (!wpa_auth)
540 		return NULL;
541 	os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
542 	os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
543 	wpa_auth->cb = cb;
544 	wpa_auth->cb_ctx = cb_ctx;
545 
546 	if (wpa_auth_gen_wpa_ie(wpa_auth)) {
547 		wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
548 		os_free(wpa_auth);
549 		return NULL;
550 	}
551 
552 	wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
553 	if (!wpa_auth->group) {
554 		os_free(wpa_auth->wpa_ie);
555 		os_free(wpa_auth);
556 		return NULL;
557 	}
558 
559 	wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
560 						wpa_auth);
561 	if (!wpa_auth->pmksa) {
562 		wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
563 		os_free(wpa_auth->group);
564 		os_free(wpa_auth->wpa_ie);
565 		os_free(wpa_auth);
566 		return NULL;
567 	}
568 
569 #ifdef CONFIG_IEEE80211R_AP
570 	wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
571 	if (!wpa_auth->ft_pmk_cache) {
572 		wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
573 		os_free(wpa_auth->group);
574 		os_free(wpa_auth->wpa_ie);
575 		pmksa_cache_auth_deinit(wpa_auth->pmksa);
576 		os_free(wpa_auth);
577 		return NULL;
578 	}
579 #endif /* CONFIG_IEEE80211R_AP */
580 
581 	if (wpa_auth->conf.wpa_gmk_rekey) {
582 		eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
583 				       wpa_rekey_gmk, wpa_auth, NULL);
584 	}
585 
586 	if (wpa_auth->conf.wpa_group_rekey) {
587 		eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
588 				       wpa_rekey_gtk, wpa_auth, NULL);
589 	}
590 
591 #ifdef CONFIG_P2P
592 	if (WPA_GET_BE32(conf->ip_addr_start)) {
593 		int count = WPA_GET_BE32(conf->ip_addr_end) -
594 			WPA_GET_BE32(conf->ip_addr_start) + 1;
595 		if (count > 1000)
596 			count = 1000;
597 		if (count > 0)
598 			wpa_auth->ip_pool = bitfield_alloc(count);
599 	}
600 #endif /* CONFIG_P2P */
601 
602 	return wpa_auth;
603 }
604 
605 
wpa_init_keys(struct wpa_authenticator * wpa_auth)606 int wpa_init_keys(struct wpa_authenticator *wpa_auth)
607 {
608 	struct wpa_group *group = wpa_auth->group;
609 
610 	wpa_printf(MSG_DEBUG,
611 		   "WPA: Start group state machine to set initial keys");
612 	wpa_group_sm_step(wpa_auth, group);
613 	group->GInit = false;
614 	wpa_group_sm_step(wpa_auth, group);
615 	if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
616 		return -1;
617 	return 0;
618 }
619 
620 
621 /**
622  * wpa_deinit - Deinitialize WPA authenticator
623  * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
624  */
wpa_deinit(struct wpa_authenticator * wpa_auth)625 void wpa_deinit(struct wpa_authenticator *wpa_auth)
626 {
627 	struct wpa_group *group, *prev;
628 
629 	eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
630 	eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
631 
632 	pmksa_cache_auth_deinit(wpa_auth->pmksa);
633 
634 #ifdef CONFIG_IEEE80211R_AP
635 	wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
636 	wpa_auth->ft_pmk_cache = NULL;
637 	wpa_ft_deinit(wpa_auth);
638 #endif /* CONFIG_IEEE80211R_AP */
639 
640 #ifdef CONFIG_P2P
641 	bitfield_free(wpa_auth->ip_pool);
642 #endif /* CONFIG_P2P */
643 
644 
645 	os_free(wpa_auth->wpa_ie);
646 
647 	group = wpa_auth->group;
648 	while (group) {
649 		prev = group;
650 		group = group->next;
651 		bin_clear_free(prev, sizeof(*prev));
652 	}
653 
654 	os_free(wpa_auth);
655 }
656 
657 
658 /**
659  * wpa_reconfig - Update WPA authenticator configuration
660  * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
661  * @conf: Configuration for WPA authenticator
662  */
wpa_reconfig(struct wpa_authenticator * wpa_auth,struct wpa_auth_config * conf)663 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
664 		 struct wpa_auth_config *conf)
665 {
666 	struct wpa_group *group;
667 
668 	if (!wpa_auth)
669 		return 0;
670 
671 	os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
672 	if (wpa_auth_gen_wpa_ie(wpa_auth)) {
673 		wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
674 		return -1;
675 	}
676 
677 	/*
678 	 * Reinitialize GTK to make sure it is suitable for the new
679 	 * configuration.
680 	 */
681 	group = wpa_auth->group;
682 	group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
683 	group->GInit = true;
684 	wpa_group_sm_step(wpa_auth, group);
685 	group->GInit = false;
686 	wpa_group_sm_step(wpa_auth, group);
687 
688 	return 0;
689 }
690 
691 
692 struct wpa_state_machine *
wpa_auth_sta_init(struct wpa_authenticator * wpa_auth,const u8 * addr,const u8 * p2p_dev_addr)693 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
694 		  const u8 *p2p_dev_addr)
695 {
696 	struct wpa_state_machine *sm;
697 
698 	if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
699 		return NULL;
700 
701 	sm = os_zalloc(sizeof(struct wpa_state_machine));
702 	if (!sm)
703 		return NULL;
704 	os_memcpy(sm->addr, addr, ETH_ALEN);
705 	if (p2p_dev_addr)
706 		os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
707 
708 	sm->wpa_auth = wpa_auth;
709 	sm->group = wpa_auth->group;
710 	wpa_group_get(sm->wpa_auth, sm->group);
711 #ifdef CONFIG_IEEE80211BE
712 	sm->mld_assoc_link_id = -1;
713 #endif /* CONFIG_IEEE80211BE */
714 
715 	return sm;
716 }
717 
718 
wpa_auth_sta_associated(struct wpa_authenticator * wpa_auth,struct wpa_state_machine * sm)719 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
720 			    struct wpa_state_machine *sm)
721 {
722 	if (!wpa_auth || !wpa_auth->conf.wpa || !sm)
723 		return -1;
724 
725 #ifdef CONFIG_IEEE80211R_AP
726 	if (sm->ft_completed) {
727 		wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
728 				"FT authentication already completed - do not start 4-way handshake");
729 		/* Go to PTKINITDONE state to allow GTK rekeying */
730 		sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
731 		sm->Pair = true;
732 		return 0;
733 	}
734 #endif /* CONFIG_IEEE80211R_AP */
735 
736 #ifdef CONFIG_FILS
737 	if (sm->fils_completed) {
738 		wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
739 				"FILS authentication already completed - do not start 4-way handshake");
740 		/* Go to PTKINITDONE state to allow GTK rekeying */
741 		sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
742 		sm->Pair = true;
743 		return 0;
744 	}
745 #endif /* CONFIG_FILS */
746 
747 	if (sm->started) {
748 		os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
749 		sm->ReAuthenticationRequest = true;
750 		return wpa_sm_step(sm);
751 	}
752 
753 	wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
754 			"start authentication");
755 	sm->started = 1;
756 
757 	sm->Init = true;
758 	if (wpa_sm_step(sm) == 1)
759 		return 1; /* should not really happen */
760 	sm->Init = false;
761 	sm->AuthenticationRequest = true;
762 	return wpa_sm_step(sm);
763 }
764 
765 
wpa_auth_sta_no_wpa(struct wpa_state_machine * sm)766 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
767 {
768 	/* WPA/RSN was not used - clear WPA state. This is needed if the STA
769 	 * reassociates back to the same AP while the previous entry for the
770 	 * STA has not yet been removed. */
771 	if (!sm)
772 		return;
773 
774 	sm->wpa_key_mgmt = 0;
775 }
776 
777 
wpa_free_sta_sm(struct wpa_state_machine * sm)778 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
779 {
780 #ifdef CONFIG_P2P
781 	if (WPA_GET_BE32(sm->ip_addr)) {
782 		wpa_printf(MSG_DEBUG,
783 			   "P2P: Free assigned IP address %u.%u.%u.%u from "
784 			   MACSTR " (bit %u)",
785 			   sm->ip_addr[0], sm->ip_addr[1],
786 			   sm->ip_addr[2], sm->ip_addr[3],
787 			   MAC2STR(wpa_auth_get_spa(sm)),
788 			   sm->ip_addr_bit);
789 		bitfield_clear(sm->wpa_auth->ip_pool, sm->ip_addr_bit);
790 	}
791 #endif /* CONFIG_P2P */
792 	if (sm->GUpdateStationKeys) {
793 		sm->group->GKeyDoneStations--;
794 		sm->GUpdateStationKeys = false;
795 	}
796 #ifdef CONFIG_IEEE80211R_AP
797 	os_free(sm->assoc_resp_ftie);
798 	wpabuf_free(sm->ft_pending_req_ies);
799 #endif /* CONFIG_IEEE80211R_AP */
800 	os_free(sm->last_rx_eapol_key);
801 	os_free(sm->wpa_ie);
802 	os_free(sm->rsnxe);
803 	wpa_group_put(sm->wpa_auth, sm->group);
804 #ifdef CONFIG_DPP2
805 	wpabuf_clear_free(sm->dpp_z);
806 #endif /* CONFIG_DPP2 */
807 	bin_clear_free(sm, sizeof(*sm));
808 }
809 
810 
wpa_auth_sta_deinit(struct wpa_state_machine * sm)811 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
812 {
813 	struct wpa_authenticator *wpa_auth;
814 
815 	if (!sm)
816 		return;
817 
818 	wpa_auth = sm->wpa_auth;
819 	if (wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
820 		wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
821 				"strict rekeying - force GTK rekey since STA is leaving");
822 		if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk,
823 					  wpa_auth, NULL) == -1)
824 			eloop_register_timeout(0, 500000, wpa_rekey_gtk,
825 					       wpa_auth, NULL);
826 	}
827 
828 	eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
829 	sm->pending_1_of_4_timeout = 0;
830 	eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
831 	eloop_cancel_timeout(wpa_rekey_ptk, wpa_auth, sm);
832 #ifdef CONFIG_IEEE80211R_AP
833 	wpa_ft_sta_deinit(sm);
834 #endif /* CONFIG_IEEE80211R_AP */
835 	if (sm->in_step_loop) {
836 		/* Must not free state machine while wpa_sm_step() is running.
837 		 * Freeing will be completed in the end of wpa_sm_step(). */
838 		wpa_printf(MSG_DEBUG,
839 			   "WPA: Registering pending STA state machine deinit for "
840 			   MACSTR, MAC2STR(wpa_auth_get_spa(sm)));
841 		sm->pending_deinit = 1;
842 	} else
843 		wpa_free_sta_sm(sm);
844 }
845 
846 
wpa_request_new_ptk(struct wpa_state_machine * sm)847 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
848 {
849 	if (!sm)
850 		return;
851 
852 	if (!sm->use_ext_key_id && sm->wpa_auth->conf.wpa_deny_ptk0_rekey) {
853 		wpa_printf(MSG_INFO,
854 			   "WPA: PTK0 rekey not allowed, disconnect " MACSTR,
855 			   MAC2STR(wpa_auth_get_spa(sm)));
856 		sm->Disconnect = true;
857 		/* Try to encourage the STA to reconnect */
858 		sm->disconnect_reason =
859 			WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
860 	} else {
861 		if (sm->use_ext_key_id)
862 			sm->keyidx_active ^= 1; /* flip Key ID */
863 		sm->PTKRequest = true;
864 		sm->PTK_valid = 0;
865 	}
866 }
867 
868 
wpa_replay_counter_valid(struct wpa_key_replay_counter * ctr,const u8 * replay_counter)869 static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
870 				    const u8 *replay_counter)
871 {
872 	int i;
873 	for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
874 		if (!ctr[i].valid)
875 			break;
876 		if (os_memcmp(replay_counter, ctr[i].counter,
877 			      WPA_REPLAY_COUNTER_LEN) == 0)
878 			return 1;
879 	}
880 	return 0;
881 }
882 
883 
wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter * ctr,const u8 * replay_counter)884 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
885 					    const u8 *replay_counter)
886 {
887 	int i;
888 	for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
889 		if (ctr[i].valid &&
890 		    (!replay_counter ||
891 		     os_memcmp(replay_counter, ctr[i].counter,
892 			       WPA_REPLAY_COUNTER_LEN) == 0))
893 			ctr[i].valid = false;
894 	}
895 }
896 
897 
898 #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)899 static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
900 			       struct wpa_state_machine *sm,
901 			       struct wpa_eapol_ie_parse *kde)
902 {
903 	struct wpa_ie_data ie;
904 	struct rsn_mdie *mdie;
905 
906 	if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
907 	    ie.num_pmkid != 1 || !ie.pmkid) {
908 		wpa_printf(MSG_DEBUG,
909 			   "FT: No PMKR1Name in FT 4-way handshake message 2/4");
910 		return -1;
911 	}
912 
913 	os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
914 	wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
915 		    sm->sup_pmk_r1_name, PMKID_LEN);
916 
917 	if (!kde->mdie || !kde->ftie) {
918 		wpa_printf(MSG_DEBUG,
919 			   "FT: No %s in FT 4-way handshake message 2/4",
920 			   kde->mdie ? "FTIE" : "MDIE");
921 		return -1;
922 	}
923 
924 	mdie = (struct rsn_mdie *) (kde->mdie + 2);
925 	if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
926 	    os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
927 		      MOBILITY_DOMAIN_ID_LEN) != 0) {
928 		wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
929 		return -1;
930 	}
931 
932 	if (sm->assoc_resp_ftie &&
933 	    (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
934 	     os_memcmp(kde->ftie, sm->assoc_resp_ftie,
935 		       2 + sm->assoc_resp_ftie[1]) != 0)) {
936 		wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
937 		wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
938 			    kde->ftie, kde->ftie_len);
939 		wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
940 			    sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
941 		return -1;
942 	}
943 
944 	return 0;
945 }
946 #endif /* CONFIG_IEEE80211R_AP */
947 
948 
wpa_receive_error_report(struct wpa_authenticator * wpa_auth,struct wpa_state_machine * sm,int group)949 static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
950 				    struct wpa_state_machine *sm, int group)
951 {
952 	/* Supplicant reported a Michael MIC error */
953 	wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
954 			 "received EAPOL-Key Error Request (STA detected Michael MIC failure (group=%d))",
955 			 group);
956 
957 	if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
958 		wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
959 				"ignore Michael MIC failure report since group cipher is not TKIP");
960 	} else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
961 		wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
962 				"ignore Michael MIC failure report since pairwise cipher is not TKIP");
963 	} else {
964 		if (wpa_auth_mic_failure_report(wpa_auth,
965 						wpa_auth_get_spa(sm)) > 0)
966 			return 1; /* STA entry was removed */
967 		sm->dot11RSNAStatsTKIPRemoteMICFailures++;
968 		wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
969 	}
970 
971 	/*
972 	 * Error report is not a request for a new key handshake, but since
973 	 * Authenticator may do it, let's change the keys now anyway.
974 	 */
975 	wpa_request_new_ptk(sm);
976 	return 0;
977 }
978 
979 
wpa_try_alt_snonce(struct wpa_state_machine * sm,u8 * data,size_t data_len)980 static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
981 			      size_t data_len)
982 {
983 	struct wpa_ptk PTK;
984 	int ok = 0;
985 	const u8 *pmk = NULL;
986 	size_t pmk_len;
987 	int vlan_id = 0;
988 	u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
989 	u8 pmk_r1[PMK_LEN_MAX];
990 	size_t key_len;
991 	int ret = -1;
992 
993 	os_memset(&PTK, 0, sizeof(PTK));
994 	for (;;) {
995 		if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
996 		    !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
997 			pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
998 					       sm->p2p_dev_addr, pmk, &pmk_len,
999 					       &vlan_id);
1000 			if (!pmk)
1001 				break;
1002 #ifdef CONFIG_IEEE80211R_AP
1003 			if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
1004 				os_memcpy(sm->xxkey, pmk, pmk_len);
1005 				sm->xxkey_len = pmk_len;
1006 			}
1007 #endif /* CONFIG_IEEE80211R_AP */
1008 		} else {
1009 			pmk = sm->PMK;
1010 			pmk_len = sm->pmk_len;
1011 		}
1012 
1013 		if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK, 0,
1014 				   pmk_r0, pmk_r1, pmk_r0_name, &key_len) < 0)
1015 			break;
1016 
1017 		if (wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
1018 				       data, data_len) == 0) {
1019 			if (sm->PMK != pmk) {
1020 				os_memcpy(sm->PMK, pmk, pmk_len);
1021 				sm->pmk_len = pmk_len;
1022 			}
1023 			ok = 1;
1024 			break;
1025 		}
1026 
1027 		if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
1028 		    wpa_key_mgmt_sae(sm->wpa_key_mgmt))
1029 			break;
1030 	}
1031 
1032 	if (!ok) {
1033 		wpa_printf(MSG_DEBUG,
1034 			   "WPA: Earlier SNonce did not result in matching MIC");
1035 		goto fail;
1036 	}
1037 
1038 	wpa_printf(MSG_DEBUG,
1039 		   "WPA: Earlier SNonce resulted in matching MIC");
1040 	sm->alt_snonce_valid = 0;
1041 
1042 	if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
1043 	    wpa_auth_update_vlan(sm->wpa_auth, sm->addr, vlan_id) < 0)
1044 		goto fail;
1045 
1046 #ifdef CONFIG_IEEE80211R_AP
1047 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt) && !sm->ft_completed) {
1048 		wpa_printf(MSG_DEBUG, "FT: Store PMK-R0/PMK-R1");
1049 		wpa_auth_ft_store_keys(sm, pmk_r0, pmk_r1, pmk_r0_name,
1050 				       key_len);
1051 	}
1052 #endif /* CONFIG_IEEE80211R_AP */
1053 
1054 	os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
1055 	os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1056 	forced_memzero(&PTK, sizeof(PTK));
1057 	sm->PTK_valid = true;
1058 
1059 	ret = 0;
1060 fail:
1061 	forced_memzero(pmk_r0, sizeof(pmk_r0));
1062 	forced_memzero(pmk_r1, sizeof(pmk_r1));
1063 	return ret;
1064 }
1065 
1066 
wpa_auth_gtk_rekey_in_process(struct wpa_authenticator * wpa_auth)1067 static bool wpa_auth_gtk_rekey_in_process(struct wpa_authenticator *wpa_auth)
1068 {
1069 	struct wpa_group *group;
1070 
1071 	for (group = wpa_auth->group; group; group = group->next) {
1072 		if (group->GKeyDoneStations)
1073 			return true;
1074 	}
1075 	return false;
1076 }
1077 
1078 
wpa_receive(struct wpa_authenticator * wpa_auth,struct wpa_state_machine * sm,u8 * data,size_t data_len)1079 void wpa_receive(struct wpa_authenticator *wpa_auth,
1080 		 struct wpa_state_machine *sm,
1081 		 u8 *data, size_t data_len)
1082 {
1083 	struct ieee802_1x_hdr *hdr;
1084 	struct wpa_eapol_key *key;
1085 	u16 key_info, key_data_length;
1086 	enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST } msg;
1087 	char *msgtxt;
1088 	struct wpa_eapol_ie_parse kde;
1089 	const u8 *key_data;
1090 	size_t keyhdrlen, mic_len;
1091 	u8 *mic;
1092 	bool is_mld = false;
1093 
1094 	if (!wpa_auth || !wpa_auth->conf.wpa || !sm)
1095 		return;
1096 
1097 #ifdef CONFIG_IEEE80211BE
1098 	is_mld = sm->mld_assoc_link_id >= 0;
1099 #endif /* CONFIG_IEEE80211BE */
1100 
1101 	wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
1102 
1103 	mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
1104 	keyhdrlen = sizeof(*key) + mic_len + 2;
1105 
1106 	if (data_len < sizeof(*hdr) + keyhdrlen) {
1107 		wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
1108 		return;
1109 	}
1110 
1111 	hdr = (struct ieee802_1x_hdr *) data;
1112 	key = (struct wpa_eapol_key *) (hdr + 1);
1113 	mic = (u8 *) (key + 1);
1114 	key_info = WPA_GET_BE16(key->key_info);
1115 	key_data = mic + mic_len + 2;
1116 	key_data_length = WPA_GET_BE16(mic + mic_len);
1117 	wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
1118 		   " key_info=0x%x type=%u mic_len=%zu key_data_length=%u",
1119 		   MAC2STR(wpa_auth_get_spa(sm)), key_info, key->type,
1120 		   mic_len, key_data_length);
1121 	wpa_hexdump(MSG_MSGDUMP,
1122 		    "WPA: EAPOL-Key header (ending before Key MIC)",
1123 		    key, sizeof(*key));
1124 	wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
1125 		    mic, mic_len);
1126 	if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
1127 		wpa_printf(MSG_INFO,
1128 			   "WPA: Invalid EAPOL-Key frame - key_data overflow (%d > %zu)",
1129 			   key_data_length,
1130 			   data_len - sizeof(*hdr) - keyhdrlen);
1131 		return;
1132 	}
1133 
1134 	if (sm->wpa == WPA_VERSION_WPA2) {
1135 		if (key->type == EAPOL_KEY_TYPE_WPA) {
1136 			/*
1137 			 * Some deployed station implementations seem to send
1138 			 * msg 4/4 with incorrect type value in WPA2 mode.
1139 			 */
1140 			wpa_printf(MSG_DEBUG,
1141 				   "Workaround: Allow EAPOL-Key with unexpected WPA type in RSN mode");
1142 		} else if (key->type != EAPOL_KEY_TYPE_RSN) {
1143 			wpa_printf(MSG_DEBUG,
1144 				   "Ignore EAPOL-Key with unexpected type %d in RSN mode",
1145 				   key->type);
1146 			return;
1147 		}
1148 	} else {
1149 		if (key->type != EAPOL_KEY_TYPE_WPA) {
1150 			wpa_printf(MSG_DEBUG,
1151 				   "Ignore EAPOL-Key with unexpected type %d in WPA mode",
1152 				   key->type);
1153 			return;
1154 		}
1155 	}
1156 
1157 	wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
1158 		    WPA_NONCE_LEN);
1159 	wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
1160 		    key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1161 
1162 	/* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1163 	 * are set */
1164 
1165 	if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1166 		wpa_printf(MSG_DEBUG, "WPA: Ignore SMK message");
1167 		return;
1168 	}
1169 
1170 	/* TODO: Make this more robust for distinguising EAPOL-Key msg 2/4 from
1171 	 * 4/4. Secure=1 is used in msg 2/4 when doing PTK rekeying, so the
1172 	 * MLD mechanism here does not work without the somewhat undesired check
1173 	 * on wpa_ptk_state.. Would likely need to decrypt Key Data first to be
1174 	 * able to know which message this is in MLO cases.. */
1175 	if (key_info & WPA_KEY_INFO_REQUEST) {
1176 		msg = REQUEST;
1177 		msgtxt = "Request";
1178 	} else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1179 		msg = GROUP_2;
1180 		msgtxt = "2/2 Group";
1181 	} else if (key_data_length == 0 ||
1182 		   (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
1183 		    key_data_length == AES_BLOCK_SIZE) ||
1184 		   (is_mld && (key_info & WPA_KEY_INFO_SECURE) &&
1185 		    sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING)) {
1186 		msg = PAIRWISE_4;
1187 		msgtxt = "4/4 Pairwise";
1188 	} else {
1189 		msg = PAIRWISE_2;
1190 		msgtxt = "2/4 Pairwise";
1191 	}
1192 
1193 	if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
1194 	    msg == GROUP_2) {
1195 		u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1196 		if (sm->pairwise == WPA_CIPHER_CCMP ||
1197 		    sm->pairwise == WPA_CIPHER_GCMP) {
1198 			if (wpa_use_cmac(sm->wpa_key_mgmt) &&
1199 			    !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1200 			    ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1201 				wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1202 						LOGGER_WARNING,
1203 						"advertised support for AES-128-CMAC, but did not use it");
1204 				return;
1205 			}
1206 
1207 			if (!wpa_use_cmac(sm->wpa_key_mgmt) &&
1208 			    !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1209 			    ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1210 				wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1211 						LOGGER_WARNING,
1212 						"did not use HMAC-SHA1-AES with CCMP/GCMP");
1213 				return;
1214 			}
1215 		}
1216 
1217 		if (wpa_use_akm_defined(sm->wpa_key_mgmt) &&
1218 		    ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1219 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1220 					LOGGER_WARNING,
1221 					"did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1222 			return;
1223 		}
1224 	}
1225 
1226 	if (key_info & WPA_KEY_INFO_REQUEST) {
1227 		if (sm->req_replay_counter_used &&
1228 		    os_memcmp(key->replay_counter, sm->req_replay_counter,
1229 			      WPA_REPLAY_COUNTER_LEN) <= 0) {
1230 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1231 					LOGGER_WARNING,
1232 					"received EAPOL-Key request with replayed counter");
1233 			return;
1234 		}
1235 	}
1236 
1237 	if (!(key_info & WPA_KEY_INFO_REQUEST) &&
1238 	    !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
1239 		int i;
1240 
1241 		if (msg == PAIRWISE_2 &&
1242 		    wpa_replay_counter_valid(sm->prev_key_replay,
1243 					     key->replay_counter) &&
1244 		    sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1245 		    os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1246 		{
1247 			/*
1248 			 * Some supplicant implementations (e.g., Windows XP
1249 			 * WZC) update SNonce for each EAPOL-Key 2/4. This
1250 			 * breaks the workaround on accepting any of the
1251 			 * pending requests, so allow the SNonce to be updated
1252 			 * even if we have already sent out EAPOL-Key 3/4.
1253 			 */
1254 			wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1255 					 LOGGER_DEBUG,
1256 					 "Process SNonce update from STA based on retransmitted EAPOL-Key 1/4");
1257 			sm->update_snonce = 1;
1258 			os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1259 			sm->alt_snonce_valid = true;
1260 			os_memcpy(sm->alt_replay_counter,
1261 				  sm->key_replay[0].counter,
1262 				  WPA_REPLAY_COUNTER_LEN);
1263 			goto continue_processing;
1264 		}
1265 
1266 		if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1267 		    sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1268 		    os_memcmp(key->replay_counter, sm->alt_replay_counter,
1269 			      WPA_REPLAY_COUNTER_LEN) == 0) {
1270 			/*
1271 			 * Supplicant may still be using the old SNonce since
1272 			 * there was two EAPOL-Key 2/4 messages and they had
1273 			 * different SNonce values.
1274 			 */
1275 			wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1276 					 LOGGER_DEBUG,
1277 					 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1278 			goto continue_processing;
1279 		}
1280 
1281 		if (msg == PAIRWISE_2 &&
1282 		    wpa_replay_counter_valid(sm->prev_key_replay,
1283 					     key->replay_counter) &&
1284 		    sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1285 			wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1286 					 LOGGER_DEBUG,
1287 					 "ignore retransmitted EAPOL-Key %s - SNonce did not change",
1288 					 msgtxt);
1289 		} else {
1290 			wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1291 					 LOGGER_DEBUG,
1292 					 "received EAPOL-Key %s with unexpected replay counter",
1293 					 msgtxt);
1294 		}
1295 		for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1296 			if (!sm->key_replay[i].valid)
1297 				break;
1298 			wpa_hexdump(MSG_DEBUG, "pending replay counter",
1299 				    sm->key_replay[i].counter,
1300 				    WPA_REPLAY_COUNTER_LEN);
1301 		}
1302 		wpa_hexdump(MSG_DEBUG, "received replay counter",
1303 			    key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1304 		return;
1305 	}
1306 
1307 continue_processing:
1308 #ifdef CONFIG_FILS
1309 	if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
1310 	    !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1311 		wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
1312 				 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1313 		return;
1314 	}
1315 #endif /* CONFIG_FILS */
1316 
1317 	switch (msg) {
1318 	case PAIRWISE_2:
1319 		if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
1320 		    sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1321 		    (!sm->update_snonce ||
1322 		     sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
1323 			wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1324 					 LOGGER_INFO,
1325 					 "received EAPOL-Key msg 2/4 in invalid state (%d) - dropped",
1326 					 sm->wpa_ptk_state);
1327 			return;
1328 		}
1329 		random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1330 		if (sm->group->reject_4way_hs_for_entropy) {
1331 			/*
1332 			 * The system did not have enough entropy to generate
1333 			 * strong random numbers. Reject the first 4-way
1334 			 * handshake(s) and collect some entropy based on the
1335 			 * information from it. Once enough entropy is
1336 			 * available, the next atempt will trigger GMK/Key
1337 			 * Counter update and the station will be allowed to
1338 			 * continue.
1339 			 */
1340 			wpa_printf(MSG_DEBUG,
1341 				   "WPA: Reject 4-way handshake to collect more entropy for random number generation");
1342 			random_mark_pool_ready();
1343 			wpa_sta_disconnect(wpa_auth, sm->addr,
1344 					   WLAN_REASON_PREV_AUTH_NOT_VALID);
1345 			return;
1346 		}
1347 		break;
1348 	case PAIRWISE_4:
1349 		if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1350 		    !sm->PTK_valid) {
1351 			wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1352 					 LOGGER_INFO,
1353 					 "received EAPOL-Key msg 4/4 in invalid state (%d) - dropped",
1354 					 sm->wpa_ptk_state);
1355 			return;
1356 		}
1357 		break;
1358 	case GROUP_2:
1359 		if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1360 		    || !sm->PTK_valid) {
1361 			wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
1362 					 LOGGER_INFO,
1363 					 "received EAPOL-Key msg 2/2 in invalid state (%d) - dropped",
1364 					 sm->wpa_ptk_group_state);
1365 			return;
1366 		}
1367 		break;
1368 	case REQUEST:
1369 		break;
1370 	}
1371 
1372 	wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
1373 			 "received EAPOL-Key frame (%s)", msgtxt);
1374 
1375 	if (key_info & WPA_KEY_INFO_ACK) {
1376 		wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
1377 				"received invalid EAPOL-Key: Key Ack set");
1378 		return;
1379 	}
1380 
1381 	if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1382 	    !(key_info & WPA_KEY_INFO_MIC)) {
1383 		wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
1384 				"received invalid EAPOL-Key: Key MIC not set");
1385 		return;
1386 	}
1387 
1388 #ifdef CONFIG_FILS
1389 	if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1390 	    (key_info & WPA_KEY_INFO_MIC)) {
1391 		wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
1392 				"received invalid EAPOL-Key: Key MIC set");
1393 		return;
1394 	}
1395 #endif /* CONFIG_FILS */
1396 
1397 	sm->MICVerified = false;
1398 	if (sm->PTK_valid && !sm->update_snonce) {
1399 		if (mic_len &&
1400 		    wpa_verify_key_mic(sm->wpa_key_mgmt, sm->pmk_len, &sm->PTK,
1401 				       data, data_len) &&
1402 		    (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1403 		     wpa_try_alt_snonce(sm, data, data_len))) {
1404 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1405 					LOGGER_INFO,
1406 					"received EAPOL-Key with invalid MIC");
1407 #ifdef TEST_FUZZ
1408 			wpa_printf(MSG_INFO,
1409 				   "TEST: Ignore Key MIC failure for fuzz testing");
1410 			goto continue_fuzz;
1411 #endif /* TEST_FUZZ */
1412 			return;
1413 		}
1414 #ifdef CONFIG_FILS
1415 		if (!mic_len &&
1416 		    wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
1417 				     &key_data_length) < 0) {
1418 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1419 					LOGGER_INFO,
1420 					"received EAPOL-Key with invalid MIC");
1421 #ifdef TEST_FUZZ
1422 			wpa_printf(MSG_INFO,
1423 				   "TEST: Ignore Key MIC failure for fuzz testing");
1424 			goto continue_fuzz;
1425 #endif /* TEST_FUZZ */
1426 			return;
1427 		}
1428 #endif /* CONFIG_FILS */
1429 #ifdef TEST_FUZZ
1430 	continue_fuzz:
1431 #endif /* TEST_FUZZ */
1432 		sm->MICVerified = true;
1433 		eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1434 		sm->pending_1_of_4_timeout = 0;
1435 	}
1436 
1437 	if (key_info & WPA_KEY_INFO_REQUEST) {
1438 		if (sm->MICVerified) {
1439 			sm->req_replay_counter_used = 1;
1440 			os_memcpy(sm->req_replay_counter, key->replay_counter,
1441 				  WPA_REPLAY_COUNTER_LEN);
1442 		} else {
1443 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1444 					LOGGER_INFO,
1445 					"received EAPOL-Key request with invalid MIC");
1446 			return;
1447 		}
1448 
1449 		/*
1450 		 * TODO: should decrypt key data field if encryption was used;
1451 		 * even though MAC address KDE is not normally encrypted,
1452 		 * supplicant is allowed to encrypt it.
1453 		 */
1454 		if (key_info & WPA_KEY_INFO_ERROR) {
1455 			if (wpa_receive_error_report(
1456 				    wpa_auth, sm,
1457 				    !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1458 				return; /* STA entry was removed */
1459 		} else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1460 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1461 					LOGGER_INFO,
1462 					"received EAPOL-Key Request for new 4-Way Handshake");
1463 			wpa_request_new_ptk(sm);
1464 		} else if (key_data_length > 0 &&
1465 			   wpa_parse_kde_ies(key_data, key_data_length,
1466 					     &kde) == 0 &&
1467 			   kde.mac_addr) {
1468 		} else {
1469 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1470 					LOGGER_INFO,
1471 					"received EAPOL-Key Request for GTK rekeying");
1472 			eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1473 			if (wpa_auth_gtk_rekey_in_process(wpa_auth))
1474 				wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG,
1475 						"skip new GTK rekey - already in process");
1476 			else
1477 				wpa_rekey_gtk(wpa_auth, NULL);
1478 		}
1479 	} else {
1480 		/* Do not allow the same key replay counter to be reused. */
1481 		wpa_replay_counter_mark_invalid(sm->key_replay,
1482 						key->replay_counter);
1483 
1484 		if (msg == PAIRWISE_2) {
1485 			/*
1486 			 * Maintain a copy of the pending EAPOL-Key frames in
1487 			 * case the EAPOL-Key frame was retransmitted. This is
1488 			 * needed to allow EAPOL-Key msg 2/4 reply to another
1489 			 * pending msg 1/4 to update the SNonce to work around
1490 			 * unexpected supplicant behavior.
1491 			 */
1492 			os_memcpy(sm->prev_key_replay, sm->key_replay,
1493 				  sizeof(sm->key_replay));
1494 		} else {
1495 			os_memset(sm->prev_key_replay, 0,
1496 				  sizeof(sm->prev_key_replay));
1497 		}
1498 
1499 		/*
1500 		 * Make sure old valid counters are not accepted anymore and
1501 		 * do not get copied again.
1502 		 */
1503 		wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
1504 	}
1505 
1506 	os_free(sm->last_rx_eapol_key);
1507 	sm->last_rx_eapol_key = os_memdup(data, data_len);
1508 	if (!sm->last_rx_eapol_key)
1509 		return;
1510 	sm->last_rx_eapol_key_len = data_len;
1511 
1512 	sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
1513 	sm->EAPOLKeyReceived = true;
1514 	sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1515 	sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1516 	os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1517 	wpa_sm_step(sm);
1518 }
1519 
1520 
wpa_gmk_to_gtk(const u8 * gmk,const char * label,const u8 * addr,const u8 * gnonce,u8 * gtk,size_t gtk_len)1521 static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1522 			  const u8 *gnonce, u8 *gtk, size_t gtk_len)
1523 {
1524 	u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + WPA_GTK_MAX_LEN];
1525 	u8 *pos;
1526 	int ret = 0;
1527 
1528 	/* GTK = PRF-X(GMK, "Group key expansion",
1529 	 *	AA || GNonce || Time || random data)
1530 	 * The example described in the IEEE 802.11 standard uses only AA and
1531 	 * GNonce as inputs here. Add some more entropy since this derivation
1532 	 * is done only at the Authenticator and as such, does not need to be
1533 	 * exactly same.
1534 	 */
1535 	os_memset(data, 0, sizeof(data));
1536 	os_memcpy(data, addr, ETH_ALEN);
1537 	os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1538 	pos = data + ETH_ALEN + WPA_NONCE_LEN;
1539 	wpa_get_ntp_timestamp(pos);
1540 #ifdef TEST_FUZZ
1541 	os_memset(pos, 0xef, 8);
1542 #endif /* TEST_FUZZ */
1543 	pos += 8;
1544 	if (random_get_bytes(pos, gtk_len) < 0)
1545 		ret = -1;
1546 
1547 #ifdef CONFIG_SHA384
1548 	if (sha384_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1549 		       gtk, gtk_len) < 0)
1550 		ret = -1;
1551 #else /* CONFIG_SHA384 */
1552 #ifdef CONFIG_SHA256
1553 	if (sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1554 		       gtk, gtk_len) < 0)
1555 		ret = -1;
1556 #else /* CONFIG_SHA256 */
1557 	if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1558 		     gtk, gtk_len) < 0)
1559 		ret = -1;
1560 #endif /* CONFIG_SHA256 */
1561 #endif /* CONFIG_SHA384 */
1562 
1563 	forced_memzero(data, sizeof(data));
1564 
1565 	return ret;
1566 }
1567 
1568 
wpa_send_eapol_timeout(void * eloop_ctx,void * timeout_ctx)1569 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1570 {
1571 	struct wpa_authenticator *wpa_auth = eloop_ctx;
1572 	struct wpa_state_machine *sm = timeout_ctx;
1573 
1574 	if (sm->waiting_radius_psk) {
1575 		wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1576 				"Ignore EAPOL-Key timeout while waiting for RADIUS PSK");
1577 		return;
1578 	}
1579 
1580 	sm->pending_1_of_4_timeout = 0;
1581 	wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
1582 			"EAPOL-Key timeout");
1583 	sm->TimeoutEvt = true;
1584 	wpa_sm_step(sm);
1585 }
1586 
1587 
__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)1588 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1589 		      struct wpa_state_machine *sm, int key_info,
1590 		      const u8 *key_rsc, const u8 *nonce,
1591 		      const u8 *kde, size_t kde_len,
1592 		      int keyidx, int encr, int force_version)
1593 {
1594 	struct wpa_auth_config *conf = &wpa_auth->conf;
1595 	struct ieee802_1x_hdr *hdr;
1596 	struct wpa_eapol_key *key;
1597 	size_t len, mic_len, keyhdrlen;
1598 	int alg;
1599 	int key_data_len, pad_len = 0;
1600 	u8 *buf, *pos;
1601 	int version, pairwise;
1602 	int i;
1603 	u8 *key_mic, *key_data;
1604 
1605 	mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
1606 	keyhdrlen = sizeof(*key) + mic_len + 2;
1607 
1608 	len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
1609 
1610 	if (force_version)
1611 		version = force_version;
1612 	else if (wpa_use_akm_defined(sm->wpa_key_mgmt))
1613 		version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
1614 	else if (wpa_use_cmac(sm->wpa_key_mgmt))
1615 		version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
1616 	else if (sm->pairwise != WPA_CIPHER_TKIP)
1617 		version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1618 	else
1619 		version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1620 
1621 	pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1622 
1623 	wpa_printf(MSG_DEBUG,
1624 		   "WPA: Send EAPOL(version=%d secure=%d mic=%d ack=%d install=%d pairwise=%d kde_len=%zu keyidx=%d encr=%d)",
1625 		   version,
1626 		   (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1627 		   (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1628 		   (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1629 		   (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1630 		   pairwise, kde_len, keyidx, encr);
1631 
1632 	key_data_len = kde_len;
1633 
1634 	if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1635 	     wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
1636 	     version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1637 		pad_len = key_data_len % 8;
1638 		if (pad_len)
1639 			pad_len = 8 - pad_len;
1640 		key_data_len += pad_len + 8;
1641 	}
1642 
1643 	len += key_data_len;
1644 	if (!mic_len && encr)
1645 		len += AES_BLOCK_SIZE;
1646 
1647 	hdr = os_zalloc(len);
1648 	if (!hdr)
1649 		return;
1650 	hdr->version = conf->eapol_version;
1651 	hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1652 	hdr->length = host_to_be16(len  - sizeof(*hdr));
1653 	key = (struct wpa_eapol_key *) (hdr + 1);
1654 	key_mic = (u8 *) (key + 1);
1655 	key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
1656 
1657 	key->type = sm->wpa == WPA_VERSION_WPA2 ?
1658 		EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1659 	key_info |= version;
1660 	if (encr && sm->wpa == WPA_VERSION_WPA2)
1661 		key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1662 	if (sm->wpa != WPA_VERSION_WPA2)
1663 		key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1664 	WPA_PUT_BE16(key->key_info, key_info);
1665 
1666 	alg = pairwise ? sm->pairwise : conf->wpa_group;
1667 	if (sm->wpa == WPA_VERSION_WPA2 && !pairwise)
1668 		WPA_PUT_BE16(key->key_length, 0);
1669 	else
1670 		WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
1671 
1672 	for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1673 		sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1674 		os_memcpy(sm->key_replay[i].counter,
1675 			  sm->key_replay[i - 1].counter,
1676 			  WPA_REPLAY_COUNTER_LEN);
1677 	}
1678 	inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1679 	os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1680 		  WPA_REPLAY_COUNTER_LEN);
1681 	wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1682 		    key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1683 	sm->key_replay[0].valid = true;
1684 
1685 	if (nonce)
1686 		os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1687 
1688 	if (key_rsc)
1689 		os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1690 
1691 	if (kde && !encr) {
1692 		os_memcpy(key_data, kde, kde_len);
1693 		WPA_PUT_BE16(key_mic + mic_len, kde_len);
1694 #ifdef CONFIG_FILS
1695 	} else if (!mic_len && kde) {
1696 		const u8 *aad[1];
1697 		size_t aad_len[1];
1698 
1699 		WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
1700 		wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1701 				kde, kde_len);
1702 
1703 		wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
1704 				sm->PTK.kek, sm->PTK.kek_len);
1705 		/* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1706 		 * to Key Data (exclusive). */
1707 		aad[0] = (u8 *) hdr;
1708 		aad_len[0] = key_mic + 2 - (u8 *) hdr;
1709 		if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
1710 				    1, aad, aad_len, key_mic + 2) < 0) {
1711 			wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
1712 			return;
1713 		}
1714 
1715 		wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
1716 			    key_mic + 2, AES_BLOCK_SIZE + kde_len);
1717 #endif /* CONFIG_FILS */
1718 	} else if (encr && kde) {
1719 		buf = os_zalloc(key_data_len);
1720 		if (!buf) {
1721 			os_free(hdr);
1722 			return;
1723 		}
1724 		pos = buf;
1725 		os_memcpy(pos, kde, kde_len);
1726 		pos += kde_len;
1727 
1728 		if (pad_len)
1729 			*pos++ = 0xdd;
1730 
1731 		wpa_hexdump_key(MSG_DEBUG,
1732 				"Plaintext EAPOL-Key Key Data (+ padding)",
1733 				buf, key_data_len);
1734 		if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1735 		    wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
1736 		    version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1737 			wpa_hexdump_key(MSG_DEBUG, "RSN: AES-WRAP using KEK",
1738 					sm->PTK.kek, sm->PTK.kek_len);
1739 			if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1740 				     (key_data_len - 8) / 8, buf, key_data)) {
1741 				os_free(hdr);
1742 				bin_clear_free(buf, key_data_len);
1743 				return;
1744 			}
1745 			wpa_hexdump(MSG_DEBUG,
1746 				    "RSN: Encrypted Key Data from AES-WRAP",
1747 				    key_data, key_data_len);
1748 			WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1749 #if !defined(CONFIG_NO_RC4) && !defined(CONFIG_FIPS)
1750 		} else if (sm->PTK.kek_len == 16) {
1751 			u8 ek[32];
1752 
1753 			wpa_printf(MSG_DEBUG,
1754 				   "WPA: Encrypt Key Data using RC4");
1755 			os_memcpy(key->key_iv,
1756 				  sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1757 			inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1758 			os_memcpy(ek, key->key_iv, 16);
1759 			os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1760 			os_memcpy(key_data, buf, key_data_len);
1761 			rc4_skip(ek, 32, 256, key_data, key_data_len);
1762 			WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1763 #endif /* !(CONFIG_NO_RC4 || CONFIG_FIPS) */
1764 		} else {
1765 			os_free(hdr);
1766 			bin_clear_free(buf, key_data_len);
1767 			return;
1768 		}
1769 		bin_clear_free(buf, key_data_len);
1770 	}
1771 
1772 	if (key_info & WPA_KEY_INFO_MIC) {
1773 		if (!sm->PTK_valid || !mic_len) {
1774 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1775 					LOGGER_DEBUG,
1776 					"PTK not valid when sending EAPOL-Key frame");
1777 			os_free(hdr);
1778 			return;
1779 		}
1780 
1781 		if (wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1782 				      sm->wpa_key_mgmt, version,
1783 				      (u8 *) hdr, len, key_mic) < 0) {
1784 			os_free(hdr);
1785 			return;
1786 		}
1787 #ifdef CONFIG_TESTING_OPTIONS
1788 		if (!pairwise &&
1789 		    conf->corrupt_gtk_rekey_mic_probability > 0.0 &&
1790 		    drand48() < conf->corrupt_gtk_rekey_mic_probability) {
1791 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
1792 					LOGGER_INFO,
1793 					"Corrupting group EAPOL-Key Key MIC");
1794 			key_mic[0]++;
1795 		}
1796 #endif /* CONFIG_TESTING_OPTIONS */
1797 	}
1798 
1799 	wpa_auth_set_eapol(wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx, 1);
1800 	wpa_hexdump(MSG_DEBUG, "Send EAPOL-Key msg", hdr, len);
1801 	wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1802 			sm->pairwise_set);
1803 	os_free(hdr);
1804 }
1805 
1806 
wpa_auth_get_sta_count(struct wpa_authenticator * wpa_auth)1807 static int wpa_auth_get_sta_count(struct wpa_authenticator *wpa_auth)
1808 {
1809 	if (!wpa_auth->cb->get_sta_count)
1810 		return -1;
1811 
1812 	return wpa_auth->cb->get_sta_count(wpa_auth->cb_ctx);
1813 }
1814 
1815 
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)1816 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1817 			   struct wpa_state_machine *sm, int key_info,
1818 			   const u8 *key_rsc, const u8 *nonce,
1819 			   const u8 *kde, size_t kde_len,
1820 			   int keyidx, int encr)
1821 {
1822 	int timeout_ms;
1823 	int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1824 	u32 ctr;
1825 
1826 	if (!sm)
1827 		return;
1828 
1829 	ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1830 
1831 #ifdef CONFIG_TESTING_OPTIONS
1832 	/* When delay_eapol_tx is true, delay the EAPOL-Key transmission by
1833 	 * sending it only on the last attempt after all timeouts for the prior
1834 	 * skipped attemps. */
1835 	if (wpa_auth->conf.delay_eapol_tx &&
1836 	    ctr != wpa_auth->conf.wpa_pairwise_update_count) {
1837 		wpa_msg(sm->wpa_auth->conf.msg_ctx, MSG_INFO,
1838 			"DELAY-EAPOL-TX-%d", ctr);
1839 		goto skip_tx;
1840 	}
1841 #endif /* CONFIG_TESTING_OPTIONS */
1842 	__wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1843 			 keyidx, encr, 0);
1844 #ifdef CONFIG_TESTING_OPTIONS
1845 skip_tx:
1846 #endif /* CONFIG_TESTING_OPTIONS */
1847 
1848 	if (ctr == 1 && wpa_auth->conf.tx_status) {
1849 		if (pairwise)
1850 			timeout_ms = eapol_key_timeout_first;
1851 		else if (wpa_auth_get_sta_count(wpa_auth) > 100)
1852 			timeout_ms = eapol_key_timeout_first_group * 2;
1853 		else
1854 			timeout_ms = eapol_key_timeout_first_group;
1855 	} else {
1856 		timeout_ms = eapol_key_timeout_subseq;
1857 	}
1858 	if (wpa_auth->conf.wpa_disable_eapol_key_retries &&
1859 	    (!pairwise || (key_info & WPA_KEY_INFO_MIC)))
1860 		timeout_ms = eapol_key_timeout_no_retrans;
1861 	if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1862 		sm->pending_1_of_4_timeout = 1;
1863 #ifdef TEST_FUZZ
1864 	timeout_ms = 1;
1865 #endif /* TEST_FUZZ */
1866 #ifdef CONFIG_TESTING_OPTIONS
1867 	if(wpa_auth->conf.enable_eapol_large_timeout) {
1868 		timeout_ms = 50 * 1000;
1869 	}
1870 #endif
1871 	wpa_printf(MSG_DEBUG,
1872 		   "WPA: Use EAPOL-Key timeout of %u ms (retry counter %u)",
1873 		   timeout_ms, ctr);
1874 	eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1875 			       wpa_send_eapol_timeout, wpa_auth, sm);
1876 }
1877 
1878 
wpa_verify_key_mic(int akmp,size_t pmk_len,struct wpa_ptk * PTK,u8 * data,size_t data_len)1879 static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
1880 			      u8 *data, size_t data_len)
1881 {
1882 	struct ieee802_1x_hdr *hdr;
1883 	struct wpa_eapol_key *key;
1884 	u16 key_info;
1885 	int ret = 0;
1886 	u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
1887 	size_t mic_len = wpa_mic_len(akmp, pmk_len);
1888 
1889 	if (data_len < sizeof(*hdr) + sizeof(*key))
1890 		return -1;
1891 
1892 	hdr = (struct ieee802_1x_hdr *) data;
1893 	key = (struct wpa_eapol_key *) (hdr + 1);
1894 	mic_pos = (u8 *) (key + 1);
1895 	key_info = WPA_GET_BE16(key->key_info);
1896 	os_memcpy(mic, mic_pos, mic_len);
1897 	os_memset(mic_pos, 0, mic_len);
1898 	if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1899 			      key_info & WPA_KEY_INFO_TYPE_MASK,
1900 			      data, data_len, mic_pos) ||
1901 	    os_memcmp_const(mic, mic_pos, mic_len) != 0)
1902 		ret = -1;
1903 	os_memcpy(mic_pos, mic, mic_len);
1904 	return ret;
1905 }
1906 
1907 
wpa_remove_ptk(struct wpa_state_machine * sm)1908 void wpa_remove_ptk(struct wpa_state_machine *sm)
1909 {
1910 	sm->PTK_valid = false;
1911 	os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1912 
1913 	wpa_auth_remove_ptksa(sm->wpa_auth, sm->addr, sm->pairwise);
1914 
1915 	if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
1916 			     0, KEY_FLAG_PAIRWISE))
1917 		wpa_printf(MSG_DEBUG,
1918 			   "RSN: PTK removal from the driver failed");
1919 	if (sm->use_ext_key_id &&
1920 	    wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 1, NULL,
1921 			     0, KEY_FLAG_PAIRWISE))
1922 		wpa_printf(MSG_DEBUG,
1923 			   "RSN: PTK Key ID 1 removal from the driver failed");
1924 	sm->pairwise_set = false;
1925 	eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1926 }
1927 
1928 
wpa_auth_sm_event(struct wpa_state_machine * sm,enum wpa_event event)1929 int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
1930 {
1931 	int remove_ptk = 1;
1932 
1933 	if (!sm)
1934 		return -1;
1935 
1936 	wpa_auth_vlogger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
1937 			 "event %d notification", event);
1938 
1939 	switch (event) {
1940 	case WPA_AUTH:
1941 #ifdef CONFIG_MESH
1942 		/* PTKs are derived through AMPE */
1943 		if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1944 			/* not mesh */
1945 			break;
1946 		}
1947 		return 0;
1948 #endif /* CONFIG_MESH */
1949 	case WPA_ASSOC:
1950 		break;
1951 	case WPA_DEAUTH:
1952 	case WPA_DISASSOC:
1953 		sm->DeauthenticationRequest = true;
1954 		os_memset(sm->PMK, 0, sizeof(sm->PMK));
1955 		sm->pmk_len = 0;
1956 #ifdef CONFIG_IEEE80211R_AP
1957 		os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
1958 		sm->xxkey_len = 0;
1959 		os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
1960 		sm->pmk_r1_len = 0;
1961 #endif /* CONFIG_IEEE80211R_AP */
1962 		break;
1963 	case WPA_REAUTH:
1964 	case WPA_REAUTH_EAPOL:
1965 		if (!sm->started) {
1966 			/*
1967 			 * When using WPS, we may end up here if the STA
1968 			 * manages to re-associate without the previous STA
1969 			 * entry getting removed. Consequently, we need to make
1970 			 * sure that the WPA state machines gets initialized
1971 			 * properly at this point.
1972 			 */
1973 			wpa_printf(MSG_DEBUG,
1974 				   "WPA state machine had not been started - initialize now");
1975 			sm->started = 1;
1976 			sm->Init = true;
1977 			if (wpa_sm_step(sm) == 1)
1978 				return 1; /* should not really happen */
1979 			sm->Init = false;
1980 			sm->AuthenticationRequest = true;
1981 			break;
1982 		}
1983 
1984 		if (sm->ptkstart_without_success > 3) {
1985 			wpa_printf(MSG_INFO,
1986 				   "WPA: Multiple EAP reauth attempts without 4-way handshake completion, disconnect "
1987 				   MACSTR, MAC2STR(sm->addr));
1988 			sm->Disconnect = true;
1989 			break;
1990 		}
1991 
1992 		if (!sm->use_ext_key_id &&
1993 		    sm->wpa_auth->conf.wpa_deny_ptk0_rekey) {
1994 			wpa_printf(MSG_INFO,
1995 				   "WPA: PTK0 rekey not allowed, disconnect "
1996 				   MACSTR, MAC2STR(wpa_auth_get_spa(sm)));
1997 			sm->Disconnect = true;
1998 			/* Try to encourage the STA to reconnect */
1999 			sm->disconnect_reason =
2000 				WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2001 			break;
2002 		}
2003 
2004 		if (sm->use_ext_key_id)
2005 			sm->keyidx_active ^= 1; /* flip Key ID */
2006 
2007 		if (sm->GUpdateStationKeys) {
2008 			/*
2009 			 * Reauthentication cancels the pending group key
2010 			 * update for this STA.
2011 			 */
2012 			sm->group->GKeyDoneStations--;
2013 			sm->GUpdateStationKeys = false;
2014 			sm->PtkGroupInit = true;
2015 		}
2016 		sm->ReAuthenticationRequest = true;
2017 		break;
2018 	case WPA_ASSOC_FT:
2019 #ifdef CONFIG_IEEE80211R_AP
2020 		wpa_printf(MSG_DEBUG,
2021 			   "FT: Retry PTK configuration after association");
2022 		wpa_ft_install_ptk(sm, 1);
2023 
2024 		/* Using FT protocol, not WPA auth state machine */
2025 		sm->ft_completed = 1;
2026 		wpa_auth_set_ptk_rekey_timer(sm);
2027 		return 0;
2028 #else /* CONFIG_IEEE80211R_AP */
2029 		break;
2030 #endif /* CONFIG_IEEE80211R_AP */
2031 	case WPA_ASSOC_FILS:
2032 #ifdef CONFIG_FILS
2033 		wpa_printf(MSG_DEBUG,
2034 			   "FILS: TK configuration after association");
2035 		fils_set_tk(sm);
2036 		sm->fils_completed = 1;
2037 		return 0;
2038 #else /* CONFIG_FILS */
2039 		break;
2040 #endif /* CONFIG_FILS */
2041 	case WPA_DRV_STA_REMOVED:
2042 		sm->tk_already_set = false;
2043 		return 0;
2044 	}
2045 
2046 #ifdef CONFIG_IEEE80211R_AP
2047 	sm->ft_completed = 0;
2048 #endif /* CONFIG_IEEE80211R_AP */
2049 
2050 	if (sm->mgmt_frame_prot && event == WPA_AUTH)
2051 		remove_ptk = 0;
2052 #ifdef CONFIG_FILS
2053 	if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
2054 	    (event == WPA_AUTH || event == WPA_ASSOC))
2055 		remove_ptk = 0;
2056 #endif /* CONFIG_FILS */
2057 
2058 	if (remove_ptk) {
2059 		sm->PTK_valid = false;
2060 		os_memset(&sm->PTK, 0, sizeof(sm->PTK));
2061 
2062 		if (event != WPA_REAUTH_EAPOL)
2063 			wpa_remove_ptk(sm);
2064 	}
2065 
2066 	if (sm->in_step_loop) {
2067 		/*
2068 		 * wpa_sm_step() is already running - avoid recursive call to
2069 		 * it by making the existing loop process the new update.
2070 		 */
2071 		sm->changed = true;
2072 		return 0;
2073 	}
2074 	return wpa_sm_step(sm);
2075 }
2076 
2077 
SM_STATE(WPA_PTK,INITIALIZE)2078 SM_STATE(WPA_PTK, INITIALIZE)
2079 {
2080 	SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
2081 	if (sm->Init) {
2082 		/* Init flag is not cleared here, so avoid busy
2083 		 * loop by claiming nothing changed. */
2084 		sm->changed = false;
2085 	}
2086 
2087 	sm->keycount = 0;
2088 	if (sm->GUpdateStationKeys)
2089 		sm->group->GKeyDoneStations--;
2090 	sm->GUpdateStationKeys = false;
2091 	if (sm->wpa == WPA_VERSION_WPA)
2092 		sm->PInitAKeys = false;
2093 	if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
2094 	       * Local AA > Remote AA)) */) {
2095 		sm->Pair = true;
2096 	}
2097 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
2098 	wpa_remove_ptk(sm);
2099 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
2100 	sm->TimeoutCtr = 0;
2101 	if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
2102 	    sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
2103 	    sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
2104 		wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2105 				   WPA_EAPOL_authorized, 0);
2106 	}
2107 }
2108 
2109 
SM_STATE(WPA_PTK,DISCONNECT)2110 SM_STATE(WPA_PTK, DISCONNECT)
2111 {
2112 	u16 reason = sm->disconnect_reason;
2113 
2114 	SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
2115 	sm->Disconnect = false;
2116 	sm->disconnect_reason = 0;
2117 	if (!reason)
2118 		reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
2119 	wpa_sta_disconnect(sm->wpa_auth, sm->addr, reason);
2120 }
2121 
2122 
SM_STATE(WPA_PTK,DISCONNECTED)2123 SM_STATE(WPA_PTK, DISCONNECTED)
2124 {
2125 	SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
2126 	sm->DeauthenticationRequest = false;
2127 }
2128 
2129 
SM_STATE(WPA_PTK,AUTHENTICATION)2130 SM_STATE(WPA_PTK, AUTHENTICATION)
2131 {
2132 	SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
2133 	os_memset(&sm->PTK, 0, sizeof(sm->PTK));
2134 	sm->PTK_valid = false;
2135 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
2136 			   1);
2137 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
2138 	sm->AuthenticationRequest = false;
2139 }
2140 
2141 
wpa_group_ensure_init(struct wpa_authenticator * wpa_auth,struct wpa_group * group)2142 static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
2143 				  struct wpa_group *group)
2144 {
2145 	if (group->first_sta_seen)
2146 		return;
2147 	/*
2148 	 * System has run bit further than at the time hostapd was started
2149 	 * potentially very early during boot up. This provides better chances
2150 	 * of collecting more randomness on embedded systems. Re-initialize the
2151 	 * GMK and Counter here to improve their strength if there was not
2152 	 * enough entropy available immediately after system startup.
2153 	 */
2154 	wpa_printf(MSG_DEBUG,
2155 		   "WPA: Re-initialize GMK/Counter on first station");
2156 	if (random_pool_ready() != 1) {
2157 		wpa_printf(MSG_INFO,
2158 			   "WPA: Not enough entropy in random pool to proceed - reject first 4-way handshake");
2159 		group->reject_4way_hs_for_entropy = true;
2160 	} else {
2161 		group->first_sta_seen = true;
2162 		group->reject_4way_hs_for_entropy = false;
2163 	}
2164 
2165 	if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
2166 	    wpa_gtk_update(wpa_auth, group) < 0 ||
2167 	    wpa_group_config_group_keys(wpa_auth, group) < 0) {
2168 		wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
2169 		group->first_sta_seen = false;
2170 		group->reject_4way_hs_for_entropy = true;
2171 	}
2172 }
2173 
2174 
SM_STATE(WPA_PTK,AUTHENTICATION2)2175 SM_STATE(WPA_PTK, AUTHENTICATION2)
2176 {
2177 	SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
2178 
2179 	wpa_group_ensure_init(sm->wpa_auth, sm->group);
2180 	sm->ReAuthenticationRequest = false;
2181 
2182 	/*
2183 	 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
2184 	 * ambiguous. The Authenticator state machine uses a counter that is
2185 	 * incremented by one for each 4-way handshake. However, the security
2186 	 * analysis of 4-way handshake points out that unpredictable nonces
2187 	 * help in preventing precomputation attacks. Instead of the state
2188 	 * machine definition, use an unpredictable nonce value here to provide
2189 	 * stronger protection against potential precomputation attacks.
2190 	 */
2191 	if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
2192 		wpa_printf(MSG_ERROR,
2193 			   "WPA: Failed to get random data for ANonce.");
2194 		sm->Disconnect = true;
2195 		return;
2196 	}
2197 	wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
2198 		    WPA_NONCE_LEN);
2199 	/* IEEE 802.11i does not clear TimeoutCtr here, but this is more
2200 	 * logical place than INITIALIZE since AUTHENTICATION2 can be
2201 	 * re-entered on ReAuthenticationRequest without going through
2202 	 * INITIALIZE. */
2203 	sm->TimeoutCtr = 0;
2204 }
2205 
2206 
wpa_auth_sm_ptk_update(struct wpa_state_machine * sm)2207 static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm)
2208 {
2209 	if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
2210 		wpa_printf(MSG_ERROR,
2211 			   "WPA: Failed to get random data for ANonce");
2212 		sm->Disconnect = true;
2213 		return -1;
2214 	}
2215 	wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce,
2216 		    WPA_NONCE_LEN);
2217 	sm->TimeoutCtr = 0;
2218 	return 0;
2219 }
2220 
2221 
SM_STATE(WPA_PTK,INITPMK)2222 SM_STATE(WPA_PTK, INITPMK)
2223 {
2224 	u8 msk[2 * PMK_LEN];
2225 	size_t len = 2 * PMK_LEN;
2226 
2227 	SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
2228 #ifdef CONFIG_IEEE80211R_AP
2229 	sm->xxkey_len = 0;
2230 #endif /* CONFIG_IEEE80211R_AP */
2231 	if (sm->pmksa) {
2232 		wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
2233 		os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2234 		sm->pmk_len = sm->pmksa->pmk_len;
2235 #ifdef CONFIG_DPP
2236 	} else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) {
2237 		wpa_printf(MSG_DEBUG,
2238 			   "DPP: No PMKSA cache entry for STA - reject connection");
2239 		sm->Disconnect = true;
2240 		sm->disconnect_reason = WLAN_REASON_INVALID_PMKID;
2241 		return;
2242 #endif /* CONFIG_DPP */
2243 	} else if (wpa_auth_get_msk(sm->wpa_auth, wpa_auth_get_spa(sm),
2244 				    msk, &len) == 0) {
2245 		unsigned int pmk_len;
2246 
2247 		if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
2248 			pmk_len = PMK_LEN_SUITE_B_192;
2249 		else
2250 			pmk_len = PMK_LEN;
2251 		wpa_printf(MSG_DEBUG,
2252 			   "WPA: PMK from EAPOL state machine (MSK len=%zu PMK len=%u)",
2253 			   len, pmk_len);
2254 		if (len < pmk_len) {
2255 			wpa_printf(MSG_DEBUG,
2256 				   "WPA: MSK not long enough (%zu) to create PMK (%u)",
2257 				   len, pmk_len);
2258 			sm->Disconnect = true;
2259 			return;
2260 		}
2261 		os_memcpy(sm->PMK, msk, pmk_len);
2262 		sm->pmk_len = pmk_len;
2263 #ifdef CONFIG_IEEE80211R_AP
2264 		if (len >= 2 * PMK_LEN) {
2265 			if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
2266 				os_memcpy(sm->xxkey, msk, SHA384_MAC_LEN);
2267 				sm->xxkey_len = SHA384_MAC_LEN;
2268 			} else {
2269 				os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
2270 				sm->xxkey_len = PMK_LEN;
2271 			}
2272 		}
2273 #endif /* CONFIG_IEEE80211R_AP */
2274 	} else {
2275 		wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
2276 			   sm->wpa_auth->cb->get_msk);
2277 		sm->Disconnect = true;
2278 		return;
2279 	}
2280 	forced_memzero(msk, sizeof(msk));
2281 
2282 	sm->req_replay_counter_used = 0;
2283 	/* IEEE 802.11i does not set keyRun to false, but not doing this
2284 	 * will break reauthentication since EAPOL state machines may not be
2285 	 * get into AUTHENTICATING state that clears keyRun before WPA state
2286 	 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2287 	 * state and takes PMK from the previously used AAA Key. This will
2288 	 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2289 	 * derived from the new AAA Key. Setting keyRun = false here seems to
2290 	 * be good workaround for this issue. */
2291 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, false);
2292 }
2293 
2294 
SM_STATE(WPA_PTK,INITPSK)2295 SM_STATE(WPA_PTK, INITPSK)
2296 {
2297 	const u8 *psk;
2298 	size_t psk_len;
2299 
2300 	SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
2301 	psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL,
2302 			       &psk_len, NULL);
2303 	if (psk) {
2304 		os_memcpy(sm->PMK, psk, psk_len);
2305 		sm->pmk_len = psk_len;
2306 #ifdef CONFIG_IEEE80211R_AP
2307 		sm->xxkey_len = PMK_LEN;
2308 #ifdef CONFIG_SAE
2309 		if (sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
2310 		    (psk_len == SHA512_MAC_LEN || psk_len == SHA384_MAC_LEN ||
2311 		     psk_len == SHA256_MAC_LEN))
2312 			sm->xxkey_len = psk_len;
2313 #endif /* CONFIG_SAE */
2314 		os_memcpy(sm->xxkey, psk, sm->xxkey_len);
2315 #endif /* CONFIG_IEEE80211R_AP */
2316 	}
2317 #ifdef CONFIG_SAE
2318 	if (wpa_auth_uses_sae(sm) && sm->pmksa) {
2319 		wpa_printf(MSG_DEBUG, "SAE: PMK from PMKSA cache (len=%zu)",
2320 			   sm->pmksa->pmk_len);
2321 		os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2322 		sm->pmk_len = sm->pmksa->pmk_len;
2323 #ifdef CONFIG_IEEE80211R_AP
2324 		os_memcpy(sm->xxkey, sm->pmksa->pmk, sm->pmksa->pmk_len);
2325 		sm->xxkey_len = sm->pmksa->pmk_len;
2326 #endif /* CONFIG_IEEE80211R_AP */
2327 	}
2328 #endif /* CONFIG_SAE */
2329 	sm->req_replay_counter_used = 0;
2330 }
2331 
2332 
SM_STATE(WPA_PTK,PTKSTART)2333 SM_STATE(WPA_PTK, PTKSTART)
2334 {
2335 	u8 buf[2 * (2 + RSN_SELECTOR_LEN) + PMKID_LEN + ETH_ALEN];
2336 	u8 *pmkid = NULL;
2337 	size_t kde_len = 0;
2338 	u16 key_info;
2339 
2340 	SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
2341 	sm->PTKRequest = false;
2342 	sm->TimeoutEvt = false;
2343 	sm->alt_snonce_valid = false;
2344 	sm->ptkstart_without_success++;
2345 
2346 	sm->TimeoutCtr++;
2347 	if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
2348 		/* No point in sending the EAPOL-Key - we will disconnect
2349 		 * immediately following this. */
2350 		return;
2351 	}
2352 
2353 	wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
2354 			"sending 1/4 msg of 4-Way Handshake");
2355 	/*
2356 	 * For infrastructure BSS cases, it is better for the AP not to include
2357 	 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2358 	 * offline search for the passphrase/PSK without having to be able to
2359 	 * capture a 4-way handshake from a STA that has access to the network.
2360 	 *
2361 	 * For IBSS cases, addition of PMKID KDE could be considered even with
2362 	 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2363 	 * possible PSK for this STA. However, this should not be done unless
2364 	 * there is support for using that information on the supplicant side.
2365 	 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2366 	 * cases would also apply here, but at least in the IBSS case, this
2367 	 * would cover a potential real use case.
2368 	 */
2369 	if (sm->wpa == WPA_VERSION_WPA2 &&
2370 	    (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) ||
2371 	     (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && sm->pmksa) ||
2372 	     wpa_key_mgmt_sae(sm->wpa_key_mgmt)) &&
2373 	    sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
2374 		pmkid = buf;
2375 		kde_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2376 		pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
2377 		pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2378 		RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
2379 		if (sm->pmksa) {
2380 			wpa_hexdump(MSG_DEBUG,
2381 				    "RSN: Message 1/4 PMKID from PMKSA entry",
2382 				    sm->pmksa->pmkid, PMKID_LEN);
2383 			os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2384 				  sm->pmksa->pmkid, PMKID_LEN);
2385 		} else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
2386 			/* No KCK available to derive PMKID */
2387 			wpa_printf(MSG_DEBUG,
2388 				   "RSN: No KCK available to derive PMKID for message 1/4");
2389 			pmkid = NULL;
2390 #ifdef CONFIG_FILS
2391 		} else if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2392 			if (sm->pmkid_set) {
2393 				wpa_hexdump(MSG_DEBUG,
2394 					    "RSN: Message 1/4 PMKID from FILS/ERP",
2395 					    sm->pmkid, PMKID_LEN);
2396 				os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2397 					  sm->pmkid, PMKID_LEN);
2398 			} else {
2399 				/* No PMKID available */
2400 				wpa_printf(MSG_DEBUG,
2401 					   "RSN: No FILS/ERP PMKID available for message 1/4");
2402 				pmkid = NULL;
2403 			}
2404 #endif /* CONFIG_FILS */
2405 #ifdef CONFIG_IEEE80211R_AP
2406 		} else if (wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
2407 			   sm->ft_completed) {
2408 			wpa_printf(MSG_DEBUG,
2409 				   "FT: No PMKID in message 1/4 when using FT protocol");
2410 			pmkid = NULL;
2411 #endif /* CONFIG_IEEE80211R_AP */
2412 #ifdef CONFIG_SAE
2413 		} else if (wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
2414 			if (sm->pmkid_set) {
2415 				wpa_hexdump(MSG_DEBUG,
2416 					    "RSN: Message 1/4 PMKID from SAE",
2417 					    sm->pmkid, PMKID_LEN);
2418 				os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2419 					  sm->pmkid, PMKID_LEN);
2420 			} else {
2421 				/* No PMKID available */
2422 				wpa_printf(MSG_DEBUG,
2423 					   "RSN: No SAE PMKID available for message 1/4");
2424 				pmkid = NULL;
2425 			}
2426 #endif /* CONFIG_SAE */
2427 		} else {
2428 			/*
2429 			 * Calculate PMKID since no PMKSA cache entry was
2430 			 * available with pre-calculated PMKID.
2431 			 */
2432 			rsn_pmkid(sm->PMK, sm->pmk_len,
2433 				  wpa_auth_get_aa(sm),
2434 				  wpa_auth_get_spa(sm),
2435 				  &pmkid[2 + RSN_SELECTOR_LEN],
2436 				  sm->wpa_key_mgmt);
2437 			wpa_hexdump(MSG_DEBUG,
2438 				    "RSN: Message 1/4 PMKID derived from PMK",
2439 				    &pmkid[2 + RSN_SELECTOR_LEN], PMKID_LEN);
2440 		}
2441 	}
2442 	if (!pmkid)
2443 		kde_len = 0;
2444 
2445 #ifdef CONFIG_IEEE80211BE
2446 	if (sm->mld_assoc_link_id >= 0) {
2447 		wpa_printf(MSG_DEBUG,
2448 			   "RSN: MLD: Add MAC Address KDE: kde_len=%zu",
2449 			   kde_len);
2450 		wpa_add_kde(buf + kde_len, RSN_KEY_DATA_MAC_ADDR,
2451 			    sm->own_mld_addr, ETH_ALEN, NULL, 0);
2452 		kde_len += 2 + RSN_SELECTOR_LEN + ETH_ALEN;
2453 	}
2454 #endif /* CONFIG_IEEE80211BE */
2455 
2456 	key_info = WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE;
2457 	if (sm->pairwise_set && sm->wpa != WPA_VERSION_WPA)
2458 		key_info |= WPA_KEY_INFO_SECURE;
2459 	wpa_send_eapol(sm->wpa_auth, sm, key_info, NULL,
2460 		       sm->ANonce, kde_len ? buf : NULL, kde_len, 0, 0);
2461 }
2462 
2463 
wpa_derive_ptk(struct wpa_state_machine * sm,const u8 * snonce,const u8 * pmk,unsigned int pmk_len,struct wpa_ptk * ptk,int force_sha256,u8 * pmk_r0,u8 * pmk_r1,u8 * pmk_r0_name,size_t * key_len)2464 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
2465 			  const u8 *pmk, unsigned int pmk_len,
2466 			  struct wpa_ptk *ptk, int force_sha256,
2467 			  u8 *pmk_r0, u8 *pmk_r1, u8 *pmk_r0_name,
2468 			  size_t *key_len)
2469 {
2470 	const u8 *z = NULL;
2471 	size_t z_len = 0, kdk_len;
2472 	int akmp;
2473 	int ret;
2474 
2475 	if (sm->wpa_auth->conf.force_kdk_derivation ||
2476 	    (sm->wpa_auth->conf.secure_ltf &&
2477 	     ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
2478 		kdk_len = WPA_KDK_MAX_LEN;
2479 	else
2480 		kdk_len = 0;
2481 
2482 #ifdef CONFIG_IEEE80211R_AP
2483 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2484 		if (sm->ft_completed) {
2485 			u8 ptk_name[WPA_PMK_NAME_LEN];
2486 
2487 			ret = wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len,
2488 						sm->SNonce, sm->ANonce,
2489 						wpa_auth_get_spa(sm),
2490 						wpa_auth_get_aa(sm),
2491 						sm->pmk_r1_name, ptk,
2492 						ptk_name, sm->wpa_key_mgmt,
2493 						sm->pairwise, kdk_len);
2494 		} else {
2495 			ret = wpa_auth_derive_ptk_ft(sm, ptk, pmk_r0, pmk_r1,
2496 						     pmk_r0_name, key_len,
2497 						     kdk_len);
2498 		}
2499 		if (ret) {
2500 			wpa_printf(MSG_ERROR, "FT: PTK derivation failed");
2501 			return ret;
2502 		}
2503 
2504 #ifdef CONFIG_PASN
2505 		if (sm->wpa_auth->conf.secure_ltf &&
2506 		    ieee802_11_rsnx_capab(sm->rsnxe,
2507 					  WLAN_RSNX_CAPAB_SECURE_LTF)) {
2508 			ret = wpa_ltf_keyseed(ptk, sm->wpa_key_mgmt,
2509 					      sm->pairwise);
2510 			if (ret) {
2511 				wpa_printf(MSG_ERROR,
2512 					   "FT: LTF keyseed derivation failed");
2513 			}
2514 		}
2515 #endif /* CONFIG_PASN */
2516 		return ret;
2517 	}
2518 #endif /* CONFIG_IEEE80211R_AP */
2519 
2520 #ifdef CONFIG_DPP2
2521 	if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
2522 		z = wpabuf_head(sm->dpp_z);
2523 		z_len = wpabuf_len(sm->dpp_z);
2524 	}
2525 #endif /* CONFIG_DPP2 */
2526 
2527 	akmp = sm->wpa_key_mgmt;
2528 	if (force_sha256)
2529 		akmp |= WPA_KEY_MGMT_PSK_SHA256;
2530 	ret = wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
2531 			     wpa_auth_get_aa(sm), wpa_auth_get_spa(sm),
2532 			     sm->ANonce, snonce, ptk, akmp,
2533 			     sm->pairwise, z, z_len, kdk_len);
2534 	if (ret) {
2535 		wpa_printf(MSG_DEBUG,
2536 			   "WPA: PTK derivation failed");
2537 		return ret;
2538 	}
2539 
2540 #ifdef CONFIG_PASN
2541 	if (sm->wpa_auth->conf.secure_ltf &&
2542 	    ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)) {
2543 		ret = wpa_ltf_keyseed(ptk, sm->wpa_key_mgmt, sm->pairwise);
2544 		if (ret) {
2545 			wpa_printf(MSG_DEBUG,
2546 				   "WPA: LTF keyseed derivation failed");
2547 		}
2548 	}
2549 #endif /* CONFIG_PASN */
2550 	return ret;
2551 }
2552 
2553 
2554 #ifdef CONFIG_FILS
2555 
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)2556 int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
2557 			 size_t pmk_len, const u8 *snonce, const u8 *anonce,
2558 			 const u8 *dhss, size_t dhss_len,
2559 			 struct wpabuf *g_sta, struct wpabuf *g_ap)
2560 {
2561 	u8 ick[FILS_ICK_MAX_LEN];
2562 	size_t ick_len;
2563 	int res;
2564 	u8 fils_ft[FILS_FT_MAX_LEN];
2565 	size_t fils_ft_len = 0, kdk_len;
2566 
2567 	if (sm->wpa_auth->conf.force_kdk_derivation ||
2568 	    (sm->wpa_auth->conf.secure_ltf &&
2569 	     ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
2570 		kdk_len = WPA_KDK_MAX_LEN;
2571 	else
2572 		kdk_len = 0;
2573 
2574 	res = fils_pmk_to_ptk(pmk, pmk_len, wpa_auth_get_spa(sm),
2575 			      wpa_auth_get_aa(sm),
2576 			      snonce, anonce, dhss, dhss_len,
2577 			      &sm->PTK, ick, &ick_len,
2578 			      sm->wpa_key_mgmt, sm->pairwise,
2579 			      fils_ft, &fils_ft_len, kdk_len);
2580 	if (res < 0)
2581 		return res;
2582 
2583 #ifdef CONFIG_PASN
2584 	if (sm->wpa_auth->conf.secure_ltf &&
2585 	    ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)) {
2586 		res = wpa_ltf_keyseed(&sm->PTK, sm->wpa_key_mgmt, sm->pairwise);
2587 		if (res) {
2588 			wpa_printf(MSG_ERROR,
2589 				   "FILS: LTF keyseed derivation failed");
2590 			return res;
2591 		}
2592 	}
2593 #endif /* CONFIG_PASN */
2594 
2595 	sm->PTK_valid = true;
2596 	sm->tk_already_set = false;
2597 
2598 #ifdef CONFIG_IEEE80211R_AP
2599 	if (fils_ft_len) {
2600 		struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2601 		struct wpa_auth_config *conf = &wpa_auth->conf;
2602 		u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
2603 
2604 		if (wpa_derive_pmk_r0(fils_ft, fils_ft_len,
2605 				      conf->ssid, conf->ssid_len,
2606 				      conf->mobility_domain,
2607 				      conf->r0_key_holder,
2608 				      conf->r0_key_holder_len,
2609 				      wpa_auth_get_spa(sm), pmk_r0, pmk_r0_name,
2610 				      sm->wpa_key_mgmt) < 0)
2611 			return -1;
2612 
2613 		wpa_ft_store_pmk_fils(sm, pmk_r0, pmk_r0_name);
2614 		forced_memzero(fils_ft, sizeof(fils_ft));
2615 
2616 		res = wpa_derive_pmk_r1_name(pmk_r0_name, conf->r1_key_holder,
2617 					     wpa_auth_get_spa(sm),
2618 					     sm->pmk_r1_name,
2619 					     fils_ft_len);
2620 		forced_memzero(pmk_r0, PMK_LEN_MAX);
2621 		if (res < 0)
2622 			return -1;
2623 		wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name,
2624 			    WPA_PMK_NAME_LEN);
2625 		sm->pmk_r1_name_valid = 1;
2626 	}
2627 #endif /* CONFIG_IEEE80211R_AP */
2628 
2629 	res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
2630 			       wpa_auth_get_spa(sm),
2631 			       wpa_auth_get_aa(sm),
2632 			       g_sta ? wpabuf_head(g_sta) : NULL,
2633 			       g_sta ? wpabuf_len(g_sta) : 0,
2634 			       g_ap ? wpabuf_head(g_ap) : NULL,
2635 			       g_ap ? wpabuf_len(g_ap) : 0,
2636 			       sm->wpa_key_mgmt, sm->fils_key_auth_sta,
2637 			       sm->fils_key_auth_ap,
2638 			       &sm->fils_key_auth_len);
2639 	forced_memzero(ick, sizeof(ick));
2640 
2641 	/* Store nonces for (Re)Association Request/Response frame processing */
2642 	os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
2643 	os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
2644 
2645 	return res;
2646 }
2647 
2648 
wpa_aead_decrypt(struct wpa_state_machine * sm,struct wpa_ptk * ptk,u8 * buf,size_t buf_len,u16 * _key_data_len)2649 static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2650 			    u8 *buf, size_t buf_len, u16 *_key_data_len)
2651 {
2652 	struct ieee802_1x_hdr *hdr;
2653 	struct wpa_eapol_key *key;
2654 	u8 *pos;
2655 	u16 key_data_len;
2656 	u8 *tmp;
2657 	const u8 *aad[1];
2658 	size_t aad_len[1];
2659 
2660 	hdr = (struct ieee802_1x_hdr *) buf;
2661 	key = (struct wpa_eapol_key *) (hdr + 1);
2662 	pos = (u8 *) (key + 1);
2663 	key_data_len = WPA_GET_BE16(pos);
2664 	if (key_data_len < AES_BLOCK_SIZE ||
2665 	    key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
2666 		wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
2667 				"No room for AES-SIV data in the frame");
2668 		return -1;
2669 	}
2670 	pos += 2; /* Pointing at the Encrypted Key Data field */
2671 
2672 	tmp = os_malloc(key_data_len);
2673 	if (!tmp)
2674 		return -1;
2675 
2676 	/* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2677 	 * to Key Data (exclusive). */
2678 	aad[0] = buf;
2679 	aad_len[0] = pos - buf;
2680 	if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
2681 			    1, aad, aad_len, tmp) < 0) {
2682 		wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
2683 				"Invalid AES-SIV data in the frame");
2684 		bin_clear_free(tmp, key_data_len);
2685 		return -1;
2686 	}
2687 
2688 	/* AEAD decryption and validation completed successfully */
2689 	key_data_len -= AES_BLOCK_SIZE;
2690 	wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2691 			tmp, key_data_len);
2692 
2693 	/* Replace Key Data field with the decrypted version */
2694 	os_memcpy(pos, tmp, key_data_len);
2695 	pos -= 2; /* Key Data Length field */
2696 	WPA_PUT_BE16(pos, key_data_len);
2697 	bin_clear_free(tmp, key_data_len);
2698 	if (_key_data_len)
2699 		*_key_data_len = key_data_len;
2700 	return 0;
2701 }
2702 
2703 
wpa_fils_validate_fils_session(struct wpa_state_machine * sm,const u8 * ies,size_t ies_len,const u8 * fils_session)2704 const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm,
2705 					  const u8 *ies, size_t ies_len,
2706 					  const u8 *fils_session)
2707 {
2708 	const u8 *ie, *end;
2709 	const u8 *session = NULL;
2710 
2711 	if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2712 		wpa_printf(MSG_DEBUG,
2713 			   "FILS: Not a FILS AKM - reject association");
2714 		return NULL;
2715 	}
2716 
2717 	/* Verify Session element */
2718 	ie = ies;
2719 	end = ((const u8 *) ie) + ies_len;
2720 	while (ie + 1 < end) {
2721 		if (ie + 2 + ie[1] > end)
2722 			break;
2723 		if (ie[0] == WLAN_EID_EXTENSION &&
2724 		    ie[1] >= 1 + FILS_SESSION_LEN &&
2725 		    ie[2] == WLAN_EID_EXT_FILS_SESSION) {
2726 			session = ie;
2727 			break;
2728 		}
2729 		ie += 2 + ie[1];
2730 	}
2731 
2732 	if (!session) {
2733 		wpa_printf(MSG_DEBUG,
2734 			   "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2735 			   __func__);
2736 		return NULL;
2737 	}
2738 
2739 	if (!fils_session) {
2740 		wpa_printf(MSG_DEBUG,
2741 			   "FILS: %s: Could not find FILS Session element in STA entry - reject",
2742 			   __func__);
2743 		return NULL;
2744 	}
2745 
2746 	if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
2747 		wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
2748 		wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
2749 			    fils_session, FILS_SESSION_LEN);
2750 		wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
2751 			    session + 3, FILS_SESSION_LEN);
2752 		return NULL;
2753 	}
2754 	return session;
2755 }
2756 
2757 
wpa_fils_validate_key_confirm(struct wpa_state_machine * sm,const u8 * ies,size_t ies_len)2758 int wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies,
2759 				  size_t ies_len)
2760 {
2761 	struct ieee802_11_elems elems;
2762 
2763 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
2764 		wpa_printf(MSG_DEBUG,
2765 			   "FILS: Failed to parse decrypted elements");
2766 		return -1;
2767 	}
2768 
2769 	if (!elems.fils_session) {
2770 		wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
2771 		return -1;
2772 	}
2773 
2774 	if (!elems.fils_key_confirm) {
2775 		wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
2776 		return -1;
2777 	}
2778 
2779 	if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
2780 		wpa_printf(MSG_DEBUG,
2781 			   "FILS: Unexpected Key-Auth length %d (expected %zu)",
2782 			   elems.fils_key_confirm_len,
2783 			   sm->fils_key_auth_len);
2784 		return -1;
2785 	}
2786 
2787 	if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
2788 		      sm->fils_key_auth_len) != 0) {
2789 		wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
2790 		wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
2791 			    elems.fils_key_confirm, elems.fils_key_confirm_len);
2792 		wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
2793 			    sm->fils_key_auth_sta, sm->fils_key_auth_len);
2794 		return -1;
2795 	}
2796 
2797 	return 0;
2798 }
2799 
2800 
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)2801 int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
2802 		       const struct ieee80211_mgmt *mgmt, size_t frame_len,
2803 		       u8 *pos, size_t left)
2804 {
2805 	u16 fc, stype;
2806 	const u8 *end, *ie_start, *ie, *session, *crypt;
2807 	const u8 *aad[5];
2808 	size_t aad_len[5];
2809 
2810 	if (!sm || !sm->PTK_valid) {
2811 		wpa_printf(MSG_DEBUG,
2812 			   "FILS: No KEK to decrypt Assocication Request frame");
2813 		return -1;
2814 	}
2815 
2816 	if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2817 		wpa_printf(MSG_DEBUG,
2818 			   "FILS: Not a FILS AKM - reject association");
2819 		return -1;
2820 	}
2821 
2822 	end = ((const u8 *) mgmt) + frame_len;
2823 	fc = le_to_host16(mgmt->frame_control);
2824 	stype = WLAN_FC_GET_STYPE(fc);
2825 	if (stype == WLAN_FC_STYPE_REASSOC_REQ)
2826 		ie_start = mgmt->u.reassoc_req.variable;
2827 	else
2828 		ie_start = mgmt->u.assoc_req.variable;
2829 	ie = ie_start;
2830 
2831 	/*
2832 	 * Find FILS Session element which is the last unencrypted element in
2833 	 * the frame.
2834 	 */
2835 	session = wpa_fils_validate_fils_session(sm, ie, end - ie,
2836 						 fils_session);
2837 	if (!session) {
2838 		wpa_printf(MSG_DEBUG, "FILS: Session validation failed");
2839 		return -1;
2840 	}
2841 
2842 	crypt = session + 2 + session[1];
2843 
2844 	if (end - crypt < AES_BLOCK_SIZE) {
2845 		wpa_printf(MSG_DEBUG,
2846 			   "FILS: Too short frame to include AES-SIV data");
2847 		return -1;
2848 	}
2849 
2850 	/* AES-SIV AAD vectors */
2851 
2852 	/* The STA's MAC address */
2853 	aad[0] = mgmt->sa;
2854 	aad_len[0] = ETH_ALEN;
2855 	/* The AP's BSSID */
2856 	aad[1] = mgmt->da;
2857 	aad_len[1] = ETH_ALEN;
2858 	/* The STA's nonce */
2859 	aad[2] = sm->SNonce;
2860 	aad_len[2] = FILS_NONCE_LEN;
2861 	/* The AP's nonce */
2862 	aad[3] = sm->ANonce;
2863 	aad_len[3] = FILS_NONCE_LEN;
2864 	/*
2865 	 * The (Re)Association Request frame from the Capability Information
2866 	 * field to the FILS Session element (both inclusive).
2867 	 */
2868 	aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
2869 	aad_len[4] = crypt - aad[4];
2870 
2871 	if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
2872 			    5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
2873 		wpa_printf(MSG_DEBUG,
2874 			   "FILS: Invalid AES-SIV data in the frame");
2875 		return -1;
2876 	}
2877 	wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
2878 		    pos, left - AES_BLOCK_SIZE);
2879 
2880 	if (wpa_fils_validate_key_confirm(sm, pos, left - AES_BLOCK_SIZE) < 0) {
2881 		wpa_printf(MSG_DEBUG, "FILS: Key Confirm validation failed");
2882 		return -1;
2883 	}
2884 
2885 	return left - AES_BLOCK_SIZE;
2886 }
2887 
2888 
fils_encrypt_assoc(struct wpa_state_machine * sm,u8 * buf,size_t current_len,size_t max_len,const struct wpabuf * hlp)2889 int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
2890 		       size_t current_len, size_t max_len,
2891 		       const struct wpabuf *hlp)
2892 {
2893 	u8 *end = buf + max_len;
2894 	u8 *pos = buf + current_len;
2895 	struct ieee80211_mgmt *mgmt;
2896 	struct wpabuf *plain;
2897 	const u8 *aad[5];
2898 	size_t aad_len[5];
2899 
2900 	if (!sm || !sm->PTK_valid)
2901 		return -1;
2902 
2903 	wpa_hexdump(MSG_DEBUG,
2904 		    "FILS: Association Response frame before FILS processing",
2905 		    buf, current_len);
2906 
2907 	mgmt = (struct ieee80211_mgmt *) buf;
2908 
2909 	/* AES-SIV AAD vectors */
2910 
2911 	/* The AP's BSSID */
2912 	aad[0] = mgmt->sa;
2913 	aad_len[0] = ETH_ALEN;
2914 	/* The STA's MAC address */
2915 	aad[1] = mgmt->da;
2916 	aad_len[1] = ETH_ALEN;
2917 	/* The AP's nonce */
2918 	aad[2] = sm->ANonce;
2919 	aad_len[2] = FILS_NONCE_LEN;
2920 	/* The STA's nonce */
2921 	aad[3] = sm->SNonce;
2922 	aad_len[3] = FILS_NONCE_LEN;
2923 	/*
2924 	 * The (Re)Association Response frame from the Capability Information
2925 	 * field (the same offset in both Association and Reassociation
2926 	 * Response frames) to the FILS Session element (both inclusive).
2927 	 */
2928 	aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
2929 	aad_len[4] = pos - aad[4];
2930 
2931 	/* The following elements will be encrypted with AES-SIV */
2932 	plain = fils_prepare_plainbuf(sm, hlp);
2933 	if (!plain) {
2934 		wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2935 		return -1;
2936 	}
2937 
2938 	if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
2939 		wpa_printf(MSG_DEBUG,
2940 			   "FILS: Not enough room for FILS elements");
2941 		wpabuf_clear_free(plain);
2942 		return -1;
2943 	}
2944 
2945 	wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
2946 			    plain);
2947 
2948 	if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
2949 			    wpabuf_head(plain), wpabuf_len(plain),
2950 			    5, aad, aad_len, pos) < 0) {
2951 		wpabuf_clear_free(plain);
2952 		return -1;
2953 	}
2954 
2955 	wpa_hexdump(MSG_DEBUG,
2956 		    "FILS: Encrypted Association Response elements",
2957 		    pos, AES_BLOCK_SIZE + wpabuf_len(plain));
2958 	current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
2959 	wpabuf_clear_free(plain);
2960 
2961 	sm->fils_completed = 1;
2962 
2963 	return current_len;
2964 }
2965 
2966 
fils_prepare_plainbuf(struct wpa_state_machine * sm,const struct wpabuf * hlp)2967 static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
2968 					     const struct wpabuf *hlp)
2969 {
2970 	struct wpabuf *plain;
2971 	u8 *len, *tmp, *tmp2;
2972 	u8 hdr[2];
2973 	u8 *gtk, stub_gtk[32];
2974 	size_t gtk_len;
2975 	struct wpa_group *gsm;
2976 	size_t plain_len;
2977 	struct wpa_auth_config *conf = &sm->wpa_auth->conf;
2978 
2979 	plain_len = 1000 + ieee80211w_kde_len(sm);
2980 	if (conf->transition_disable)
2981 		plain_len += 2 + RSN_SELECTOR_LEN + 1;
2982 	plain = wpabuf_alloc(plain_len);
2983 	if (!plain)
2984 		return NULL;
2985 
2986 	/* TODO: FILS Public Key */
2987 
2988 	/* FILS Key Confirmation */
2989 	wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2990 	wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
2991 	/* Element ID Extension */
2992 	wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
2993 	wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
2994 
2995 	/* FILS HLP Container */
2996 	if (hlp)
2997 		wpabuf_put_buf(plain, hlp);
2998 
2999 	/* TODO: FILS IP Address Assignment */
3000 
3001 	/* Key Delivery */
3002 	gsm = sm->group;
3003 	wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
3004 	len = wpabuf_put(plain, 1);
3005 	wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
3006 	wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
3007 			    wpabuf_put(plain, WPA_KEY_RSC_LEN));
3008 	/* GTK KDE */
3009 	gtk = gsm->GTK[gsm->GN - 1];
3010 	gtk_len = gsm->GTK_len;
3011 	if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3012 		/*
3013 		 * Provide unique random GTK to each STA to prevent use
3014 		 * of GTK in the BSS.
3015 		 */
3016 		if (random_get_bytes(stub_gtk, gtk_len) < 0) {
3017 			wpabuf_clear_free(plain);
3018 			return NULL;
3019 		}
3020 		gtk = stub_gtk;
3021 	}
3022 	hdr[0] = gsm->GN & 0x03;
3023 	hdr[1] = 0;
3024 	tmp = wpabuf_put(plain, 0);
3025 	tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3026 			   gtk, gtk_len);
3027 	wpabuf_put(plain, tmp2 - tmp);
3028 
3029 	/* IGTK KDE and BIGTK KDE */
3030 	tmp = wpabuf_put(plain, 0);
3031 	tmp2 = ieee80211w_kde_add(sm, tmp);
3032 	wpabuf_put(plain, tmp2 - tmp);
3033 
3034 	if (conf->transition_disable) {
3035 		tmp = wpabuf_put(plain, 0);
3036 		tmp2 = wpa_add_kde(tmp, WFA_KEY_DATA_TRANSITION_DISABLE,
3037 				   &conf->transition_disable, 1, NULL, 0);
3038 		wpabuf_put(plain, tmp2 - tmp);
3039 	}
3040 
3041 	*len = (u8 *) wpabuf_put(plain, 0) - len - 1;
3042 
3043 #ifdef CONFIG_OCV
3044 	if (wpa_auth_uses_ocv(sm)) {
3045 		struct wpa_channel_info ci;
3046 		u8 *pos;
3047 
3048 		if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
3049 			wpa_printf(MSG_WARNING,
3050 				   "FILS: Failed to get channel info for OCI element");
3051 			wpabuf_clear_free(plain);
3052 			return NULL;
3053 		}
3054 #ifdef CONFIG_TESTING_OPTIONS
3055 		if (conf->oci_freq_override_fils_assoc) {
3056 			wpa_printf(MSG_INFO,
3057 				   "TEST: Override OCI frequency %d -> %u MHz",
3058 				   ci.frequency,
3059 				   conf->oci_freq_override_fils_assoc);
3060 			ci.frequency = conf->oci_freq_override_fils_assoc;
3061 		}
3062 #endif /* CONFIG_TESTING_OPTIONS */
3063 
3064 		pos = wpabuf_put(plain, OCV_OCI_EXTENDED_LEN);
3065 		if (ocv_insert_extended_oci(&ci, pos) < 0) {
3066 			wpabuf_clear_free(plain);
3067 			return NULL;
3068 		}
3069 	}
3070 #endif /* CONFIG_OCV */
3071 
3072 	return plain;
3073 }
3074 
3075 
fils_set_tk(struct wpa_state_machine * sm)3076 int fils_set_tk(struct wpa_state_machine *sm)
3077 {
3078 	enum wpa_alg alg;
3079 	int klen;
3080 
3081 	if (!sm || !sm->PTK_valid) {
3082 		wpa_printf(MSG_DEBUG, "FILS: No valid PTK available to set TK");
3083 		return -1;
3084 	}
3085 	if (sm->tk_already_set) {
3086 		wpa_printf(MSG_DEBUG, "FILS: TK already set to the driver");
3087 		return -1;
3088 	}
3089 
3090 	alg = wpa_cipher_to_alg(sm->pairwise);
3091 	klen = wpa_cipher_key_len(sm->pairwise);
3092 
3093 	wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
3094 	if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
3095 			     sm->PTK.tk, klen, KEY_FLAG_PAIRWISE_RX_TX)) {
3096 		wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
3097 		return -1;
3098 	}
3099 
3100 #ifdef CONFIG_PASN
3101 	if (sm->wpa_auth->conf.secure_ltf &&
3102 	    ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
3103 	    wpa_auth_set_ltf_keyseed(sm->wpa_auth, sm->addr,
3104 				     sm->PTK.ltf_keyseed,
3105 				     sm->PTK.ltf_keyseed_len)) {
3106 		wpa_printf(MSG_ERROR,
3107 			   "FILS: Failed to set LTF keyseed to driver");
3108 		return -1;
3109 	}
3110 #endif /* CONFIG_PASN */
3111 
3112 	sm->pairwise_set = true;
3113 	sm->tk_already_set = true;
3114 
3115 	wpa_auth_store_ptksa(sm->wpa_auth, sm->addr, sm->pairwise,
3116 			     dot11RSNAConfigPMKLifetime, &sm->PTK);
3117 
3118 	return 0;
3119 }
3120 
3121 
hostapd_eid_assoc_fils_session(struct wpa_state_machine * sm,u8 * buf,const u8 * fils_session,struct wpabuf * hlp)3122 u8 * hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf,
3123 				    const u8 *fils_session, struct wpabuf *hlp)
3124 {
3125 	struct wpabuf *plain;
3126 	u8 *pos = buf;
3127 
3128 	/* FILS Session */
3129 	*pos++ = WLAN_EID_EXTENSION; /* Element ID */
3130 	*pos++ = 1 + FILS_SESSION_LEN; /* Length */
3131 	*pos++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
3132 	os_memcpy(pos, fils_session, FILS_SESSION_LEN);
3133 	pos += FILS_SESSION_LEN;
3134 
3135 	plain = fils_prepare_plainbuf(sm, hlp);
3136 	if (!plain) {
3137 		wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
3138 		return NULL;
3139 	}
3140 
3141 	os_memcpy(pos, wpabuf_head(plain), wpabuf_len(plain));
3142 	pos += wpabuf_len(plain);
3143 
3144 	wpa_printf(MSG_DEBUG, "%s: plain buf_len: %zu", __func__,
3145 		   wpabuf_len(plain));
3146 	wpabuf_clear_free(plain);
3147 	sm->fils_completed = 1;
3148 	return pos;
3149 }
3150 
3151 #endif /* CONFIG_FILS */
3152 
3153 
3154 #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)3155 int get_sta_tx_parameters(struct wpa_state_machine *sm, int ap_max_chanwidth,
3156 			  int ap_seg1_idx, int *bandwidth, int *seg1_idx)
3157 {
3158 	struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3159 
3160 	if (!wpa_auth->cb->get_sta_tx_params)
3161 		return -1;
3162 	return wpa_auth->cb->get_sta_tx_params(wpa_auth->cb_ctx, sm->addr,
3163 					       ap_max_chanwidth, ap_seg1_idx,
3164 					       bandwidth, seg1_idx);
3165 }
3166 #endif /* CONFIG_OCV */
3167 
3168 
wpa_auth_validate_ml_kdes_m2(struct wpa_state_machine * sm,struct wpa_eapol_ie_parse * kde)3169 static int wpa_auth_validate_ml_kdes_m2(struct wpa_state_machine *sm,
3170 					struct wpa_eapol_ie_parse *kde)
3171 {
3172 #ifdef CONFIG_IEEE80211BE
3173 	int i;
3174 	unsigned int n_links = 0;
3175 
3176 	if (sm->mld_assoc_link_id < 0)
3177 		return 0;
3178 
3179 	/* MLD MAC address must be the same */
3180 	if (!kde->mac_addr ||
3181 	    os_memcmp(kde->mac_addr, sm->peer_mld_addr, ETH_ALEN) != 0) {
3182 		wpa_printf(MSG_DEBUG, "RSN: MLD: Invalid MLD address");
3183 		return -1;
3184 	}
3185 
3186 	/* Find matching link ID and the MAC address for each link */
3187 	for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
3188 		if (!(kde->valid_mlo_links & BIT(i)))
3189 			continue;
3190 
3191 		/*
3192 		 * Each entry should contain the link information and the MAC
3193 		 * address.
3194 		 */
3195 		if (kde->mlo_link_len[i] != 1 + ETH_ALEN) {
3196 			wpa_printf(MSG_DEBUG,
3197 				   "RSN: MLD: Invalid MLO Link (ID %u) KDE len=%zu",
3198 				   i, kde->mlo_link_len[i]);
3199 			return -1;
3200 		}
3201 
3202 		if (!sm->mld_links[i].valid || i == sm->mld_assoc_link_id) {
3203 			wpa_printf(MSG_DEBUG,
3204 				   "RSN: MLD: Invalid link ID=%u", i);
3205 			return -1;
3206 		}
3207 
3208 		if (os_memcmp(sm->mld_links[i].peer_addr, kde->mlo_link[i] + 1,
3209 			      ETH_ALEN) != 0) {
3210 			wpa_printf(MSG_DEBUG,
3211 				   "RSN: MLD: invalid MAC address=" MACSTR
3212 				   " expected " MACSTR " (link ID %u)",
3213 				   MAC2STR(kde->mlo_link[i] + 1),
3214 				   MAC2STR(sm->mld_links[i].peer_addr), i);
3215 			return -1;
3216 		}
3217 
3218 		n_links++;
3219 	}
3220 
3221 	/* Must have the same number of MLO links (excluding the local one) */
3222 	if (n_links != sm->n_mld_affiliated_links) {
3223 		wpa_printf(MSG_DEBUG,
3224 			   "RSN: MLD: Expecting %u MLD links in msg 2, but got %u",
3225 			   sm->n_mld_affiliated_links, n_links);
3226 		return -1;
3227 	}
3228 #endif /* CONFIG_IEEE80211BE */
3229 
3230 	return 0;
3231 }
3232 
3233 
SM_STATE(WPA_PTK,PTKCALCNEGOTIATING)3234 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
3235 {
3236 	struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3237 	struct wpa_ptk PTK;
3238 	int ok = 0, psk_found = 0;
3239 	const u8 *pmk = NULL;
3240 	size_t pmk_len;
3241 	int ft;
3242 	const u8 *eapol_key_ie, *key_data, *mic;
3243 	u16 key_data_length;
3244 	size_t mic_len, eapol_key_ie_len;
3245 	struct ieee802_1x_hdr *hdr;
3246 	struct wpa_eapol_key *key;
3247 	struct wpa_eapol_ie_parse kde;
3248 	int vlan_id = 0;
3249 	int owe_ptk_workaround = !!wpa_auth->conf.owe_ptk_workaround;
3250 	u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
3251 	u8 pmk_r1[PMK_LEN_MAX];
3252 	size_t key_len;
3253 
3254 	SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
3255 	sm->EAPOLKeyReceived = false;
3256 	sm->update_snonce = false;
3257 	os_memset(&PTK, 0, sizeof(PTK));
3258 
3259 	mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
3260 
3261 	/* WPA with IEEE 802.1X: use the derived PMK from EAP
3262 	 * WPA-PSK: iterate through possible PSKs and select the one matching
3263 	 * the packet */
3264 	for (;;) {
3265 		if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
3266 		    !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
3267 			pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
3268 					       sm->p2p_dev_addr, pmk, &pmk_len,
3269 					       &vlan_id);
3270 			if (!pmk)
3271 				break;
3272 			psk_found = 1;
3273 #ifdef CONFIG_IEEE80211R_AP
3274 			if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
3275 				os_memcpy(sm->xxkey, pmk, pmk_len);
3276 				sm->xxkey_len = pmk_len;
3277 			}
3278 #endif /* CONFIG_IEEE80211R_AP */
3279 		} else {
3280 			pmk = sm->PMK;
3281 			pmk_len = sm->pmk_len;
3282 		}
3283 
3284 		if ((!pmk || !pmk_len) && sm->pmksa) {
3285 			wpa_printf(MSG_DEBUG, "WPA: Use PMK from PMKSA cache");
3286 			pmk = sm->pmksa->pmk;
3287 			pmk_len = sm->pmksa->pmk_len;
3288 		}
3289 
3290 		if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK,
3291 				   owe_ptk_workaround == 2, pmk_r0, pmk_r1,
3292 				   pmk_r0_name, &key_len) < 0)
3293 			break;
3294 
3295 		if (mic_len &&
3296 		    wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
3297 				       sm->last_rx_eapol_key,
3298 				       sm->last_rx_eapol_key_len) == 0) {
3299 			if (sm->PMK != pmk) {
3300 				os_memcpy(sm->PMK, pmk, pmk_len);
3301 				sm->pmk_len = pmk_len;
3302 			}
3303 			ok = 1;
3304 			break;
3305 		}
3306 
3307 #ifdef CONFIG_FILS
3308 		if (!mic_len &&
3309 		    wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
3310 				     sm->last_rx_eapol_key_len, NULL) == 0) {
3311 			ok = 1;
3312 			break;
3313 		}
3314 #endif /* CONFIG_FILS */
3315 
3316 #ifdef CONFIG_OWE
3317 		if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && pmk_len > 32 &&
3318 		    owe_ptk_workaround == 1) {
3319 			wpa_printf(MSG_DEBUG,
3320 				   "OWE: Try PTK derivation workaround with SHA256");
3321 			owe_ptk_workaround = 2;
3322 			continue;
3323 		}
3324 #endif /* CONFIG_OWE */
3325 
3326 		if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3327 		    wpa_key_mgmt_sae(sm->wpa_key_mgmt))
3328 			break;
3329 	}
3330 
3331 	if (!ok && wpa_key_mgmt_wpa_psk_no_sae(sm->wpa_key_mgmt) &&
3332 	    wpa_auth->conf.radius_psk && wpa_auth->cb->request_radius_psk &&
3333 	    !sm->waiting_radius_psk) {
3334 		wpa_printf(MSG_DEBUG, "No PSK available - ask RADIUS server");
3335 		wpa_auth->cb->request_radius_psk(wpa_auth->cb_ctx, sm->addr,
3336 						 sm->wpa_key_mgmt,
3337 						 sm->ANonce,
3338 						 sm->last_rx_eapol_key,
3339 						 sm->last_rx_eapol_key_len);
3340 		sm->waiting_radius_psk = 1;
3341 		goto out;
3342 	}
3343 
3344 	if (!ok) {
3345 		wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm),
3346 				LOGGER_DEBUG,
3347 				"invalid MIC in msg 2/4 of 4-Way Handshake");
3348 		if (psk_found)
3349 			wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
3350 		goto out;
3351 	}
3352 
3353 	/*
3354 	 * Note: last_rx_eapol_key length fields have already been validated in
3355 	 * wpa_receive().
3356 	 */
3357 	hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
3358 	key = (struct wpa_eapol_key *) (hdr + 1);
3359 	mic = (u8 *) (key + 1);
3360 	key_data = mic + mic_len + 2;
3361 	key_data_length = WPA_GET_BE16(mic + mic_len);
3362 	if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
3363 	    sizeof(*key) - mic_len - 2)
3364 		goto out;
3365 
3366 	if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
3367 		wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
3368 				 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
3369 		goto out;
3370 	}
3371 	if (kde.rsn_ie) {
3372 		eapol_key_ie = kde.rsn_ie;
3373 		eapol_key_ie_len = kde.rsn_ie_len;
3374 	} else if (kde.osen) {
3375 		eapol_key_ie = kde.osen;
3376 		eapol_key_ie_len = kde.osen_len;
3377 	} else {
3378 		eapol_key_ie = kde.wpa_ie;
3379 		eapol_key_ie_len = kde.wpa_ie_len;
3380 	}
3381 	ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
3382 	if (!sm->wpa_ie ||
3383 	    wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
3384 			       eapol_key_ie, eapol_key_ie_len)) {
3385 		wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
3386 				"WPA IE from (Re)AssocReq did not match with msg 2/4");
3387 		if (sm->wpa_ie) {
3388 			wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
3389 				    sm->wpa_ie, sm->wpa_ie_len);
3390 		}
3391 		wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
3392 			    eapol_key_ie, eapol_key_ie_len);
3393 		/* MLME-DEAUTHENTICATE.request */
3394 		wpa_sta_disconnect(wpa_auth, sm->addr,
3395 				   WLAN_REASON_PREV_AUTH_NOT_VALID);
3396 		goto out;
3397 	}
3398 	if ((!sm->rsnxe && kde.rsnxe) ||
3399 	    (sm->rsnxe && !kde.rsnxe) ||
3400 	    (sm->rsnxe && kde.rsnxe &&
3401 	     (sm->rsnxe_len != kde.rsnxe_len ||
3402 	      os_memcmp(sm->rsnxe, kde.rsnxe, sm->rsnxe_len) != 0))) {
3403 		wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
3404 				"RSNXE from (Re)AssocReq did not match the one in EAPOL-Key msg 2/4");
3405 		wpa_hexdump(MSG_DEBUG, "RSNXE in AssocReq",
3406 			    sm->rsnxe, sm->rsnxe_len);
3407 		wpa_hexdump(MSG_DEBUG, "RSNXE in EAPOL-Key msg 2/4",
3408 			    kde.rsnxe, kde.rsnxe_len);
3409 		/* MLME-DEAUTHENTICATE.request */
3410 		wpa_sta_disconnect(wpa_auth, sm->addr,
3411 				   WLAN_REASON_PREV_AUTH_NOT_VALID);
3412 		goto out;
3413 	}
3414 #ifdef CONFIG_OCV
3415 	if (wpa_auth_uses_ocv(sm)) {
3416 		struct wpa_channel_info ci;
3417 		int tx_chanwidth;
3418 		int tx_seg1_idx;
3419 		enum oci_verify_result res;
3420 
3421 		if (wpa_channel_info(wpa_auth, &ci) != 0) {
3422 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
3423 					LOGGER_INFO,
3424 					"Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
3425 			goto out;
3426 		}
3427 
3428 		if (get_sta_tx_parameters(sm,
3429 					  channel_width_to_int(ci.chanwidth),
3430 					  ci.seg1_idx, &tx_chanwidth,
3431 					  &tx_seg1_idx) < 0)
3432 			goto out;
3433 
3434 		res = ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
3435 					   tx_chanwidth, tx_seg1_idx);
3436 		if (wpa_auth_uses_ocv(sm) == 2 && res == OCI_NOT_FOUND) {
3437 			/* Work around misbehaving STAs */
3438 			wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
3439 					 LOGGER_INFO,
3440 					 "Disable OCV with a STA that does not send OCI");
3441 			wpa_auth_set_ocv(sm, 0);
3442 		} else if (res != OCI_SUCCESS) {
3443 			wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
3444 					 LOGGER_INFO,
3445 					 "OCV failed: %s", ocv_errorstr);
3446 			if (wpa_auth->conf.msg_ctx)
3447 				wpa_msg(wpa_auth->conf.msg_ctx, MSG_INFO,
3448 					OCV_FAILURE "addr=" MACSTR
3449 					" frame=eapol-key-m2 error=%s",
3450 					MAC2STR(wpa_auth_get_spa(sm)),
3451 					ocv_errorstr);
3452 			goto out;
3453 		}
3454 	}
3455 #endif /* CONFIG_OCV */
3456 #ifdef CONFIG_IEEE80211R_AP
3457 	if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
3458 		wpa_sta_disconnect(wpa_auth, sm->addr,
3459 				   WLAN_REASON_PREV_AUTH_NOT_VALID);
3460 		goto out;
3461 	}
3462 #endif /* CONFIG_IEEE80211R_AP */
3463 #ifdef CONFIG_P2P
3464 	if (kde.ip_addr_req && kde.ip_addr_req[0] &&
3465 	    wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
3466 		int idx;
3467 		wpa_printf(MSG_DEBUG,
3468 			   "P2P: IP address requested in EAPOL-Key exchange");
3469 		idx = bitfield_get_first_zero(wpa_auth->ip_pool);
3470 		if (idx >= 0) {
3471 			u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
3472 			bitfield_set(wpa_auth->ip_pool, idx);
3473 			sm->ip_addr_bit = idx;
3474 			WPA_PUT_BE32(sm->ip_addr, start + idx);
3475 			wpa_printf(MSG_DEBUG,
3476 				   "P2P: Assigned IP address %u.%u.%u.%u to "
3477 				   MACSTR " (bit %u)",
3478 				   sm->ip_addr[0], sm->ip_addr[1],
3479 				   sm->ip_addr[2], sm->ip_addr[3],
3480 				   MAC2STR(wpa_auth_get_spa(sm)),
3481 				   sm->ip_addr_bit);
3482 		}
3483 	}
3484 #endif /* CONFIG_P2P */
3485 
3486 #ifdef CONFIG_DPP2
3487 	if (DPP_VERSION > 1 && kde.dpp_kde) {
3488 		wpa_printf(MSG_DEBUG,
3489 			   "DPP: peer Protocol Version %u Flags 0x%x",
3490 			   kde.dpp_kde[0], kde.dpp_kde[1]);
3491 		if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP &&
3492 		    wpa_auth->conf.dpp_pfs != 2 &&
3493 		    (kde.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) &&
3494 		    !sm->dpp_z) {
3495 			wpa_printf(MSG_INFO,
3496 				   "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
3497 			wpa_sta_disconnect(wpa_auth, sm->addr,
3498 					   WLAN_REASON_PREV_AUTH_NOT_VALID);
3499 			goto out;
3500 		}
3501 	}
3502 #endif /* CONFIG_DPP2 */
3503 
3504 	if (wpa_auth_validate_ml_kdes_m2(sm, &kde) < 0) {
3505 		wpa_sta_disconnect(wpa_auth, sm->addr,
3506 				   WLAN_REASON_PREV_AUTH_NOT_VALID);
3507 		return;
3508 	}
3509 
3510 #ifdef CONFIG_IEEE80211R_AP
3511 	if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3512 		/*
3513 		 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
3514 		 * with the value we derived.
3515 		 */
3516 		if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
3517 				    WPA_PMK_NAME_LEN) != 0) {
3518 			wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm),
3519 					LOGGER_DEBUG,
3520 					"PMKR1Name mismatch in FT 4-way handshake");
3521 			wpa_hexdump(MSG_DEBUG,
3522 				    "FT: PMKR1Name from Supplicant",
3523 				    sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
3524 			wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
3525 				    sm->pmk_r1_name, WPA_PMK_NAME_LEN);
3526 			goto out;
3527 		}
3528 	}
3529 #endif /* CONFIG_IEEE80211R_AP */
3530 
3531 	if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
3532 	    wpa_auth_update_vlan(wpa_auth, sm->addr, vlan_id) < 0) {
3533 		wpa_sta_disconnect(wpa_auth, sm->addr,
3534 				   WLAN_REASON_PREV_AUTH_NOT_VALID);
3535 		goto out;
3536 	}
3537 
3538 	sm->pending_1_of_4_timeout = 0;
3539 	eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
3540 
3541 	if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) && sm->PMK != pmk) {
3542 		/* PSK may have changed from the previous choice, so update
3543 		 * state machine data based on whatever PSK was selected here.
3544 		 */
3545 		os_memcpy(sm->PMK, pmk, PMK_LEN);
3546 		sm->pmk_len = PMK_LEN;
3547 	}
3548 
3549 	sm->MICVerified = true;
3550 
3551 #ifdef CONFIG_IEEE80211R_AP
3552 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt) && !sm->ft_completed) {
3553 		wpa_printf(MSG_DEBUG, "FT: Store PMK-R0/PMK-R1");
3554 		wpa_auth_ft_store_keys(sm, pmk_r0, pmk_r1, pmk_r0_name,
3555 				       key_len);
3556 	}
3557 #endif /* CONFIG_IEEE80211R_AP */
3558 
3559 	os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
3560 	forced_memzero(&PTK, sizeof(PTK));
3561 	sm->PTK_valid = true;
3562 out:
3563 	forced_memzero(pmk_r0, sizeof(pmk_r0));
3564 	forced_memzero(pmk_r1, sizeof(pmk_r1));
3565 }
3566 
3567 
SM_STATE(WPA_PTK,PTKCALCNEGOTIATING2)3568 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
3569 {
3570 	SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
3571 	sm->TimeoutCtr = 0;
3572 }
3573 
3574 
ieee80211w_kde_len(struct wpa_state_machine * sm)3575 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
3576 {
3577 	size_t len = 0;
3578 
3579 	if (sm->mgmt_frame_prot) {
3580 		len += 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN;
3581 		len += wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3582 	}
3583 	if (sm->mgmt_frame_prot && sm->wpa_auth->conf.beacon_prot) {
3584 		len += 2 + RSN_SELECTOR_LEN + WPA_BIGTK_KDE_PREFIX_LEN;
3585 		len += wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3586 	}
3587 
3588 	return len;
3589 }
3590 
3591 
ieee80211w_kde_add(struct wpa_state_machine * sm,u8 * pos)3592 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
3593 {
3594 	struct wpa_igtk_kde igtk;
3595 	struct wpa_bigtk_kde bigtk;
3596 	struct wpa_group *gsm = sm->group;
3597 	u8 rsc[WPA_KEY_RSC_LEN];
3598 	struct wpa_auth_config *conf = &sm->wpa_auth->conf;
3599 	size_t len = wpa_cipher_key_len(conf->group_mgmt_cipher);
3600 
3601 	if (!sm->mgmt_frame_prot)
3602 		return pos;
3603 
3604 #ifdef CONFIG_IEEE80211BE
3605 	if (sm->mld_assoc_link_id >= 0)
3606 		return pos; /* Use per-link MLO KDEs instead */
3607 #endif /* CONFIG_IEEE80211BE */
3608 
3609 	igtk.keyid[0] = gsm->GN_igtk;
3610 	igtk.keyid[1] = 0;
3611 	if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
3612 	    wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
3613 		os_memset(igtk.pn, 0, sizeof(igtk.pn));
3614 	else
3615 		os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
3616 	os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
3617 	if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3618 		/*
3619 		 * Provide unique random IGTK to each STA to prevent use of
3620 		 * IGTK in the BSS.
3621 		 */
3622 		if (random_get_bytes(igtk.igtk, len) < 0)
3623 			return pos;
3624 	}
3625 	pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
3626 			  (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
3627 			  NULL, 0);
3628 	forced_memzero(&igtk, sizeof(igtk));
3629 
3630 	if (!conf->beacon_prot)
3631 		return pos;
3632 
3633 	bigtk.keyid[0] = gsm->GN_bigtk;
3634 	bigtk.keyid[1] = 0;
3635 	if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
3636 	    wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_bigtk, rsc) < 0)
3637 		os_memset(bigtk.pn, 0, sizeof(bigtk.pn));
3638 	else
3639 		os_memcpy(bigtk.pn, rsc, sizeof(bigtk.pn));
3640 	os_memcpy(bigtk.bigtk, gsm->BIGTK[gsm->GN_bigtk - 6], len);
3641 	if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
3642 		/*
3643 		 * Provide unique random BIGTK to each OSEN STA to prevent use
3644 		 * of BIGTK in the BSS.
3645 		 */
3646 		if (random_get_bytes(bigtk.bigtk, len) < 0)
3647 			return pos;
3648 	}
3649 	pos = wpa_add_kde(pos, RSN_KEY_DATA_BIGTK,
3650 			  (const u8 *) &bigtk, WPA_BIGTK_KDE_PREFIX_LEN + len,
3651 			  NULL, 0);
3652 	forced_memzero(&bigtk, sizeof(bigtk));
3653 
3654 	return pos;
3655 }
3656 
3657 
ocv_oci_len(struct wpa_state_machine * sm)3658 static int ocv_oci_len(struct wpa_state_machine *sm)
3659 {
3660 #ifdef CONFIG_OCV
3661 	if (wpa_auth_uses_ocv(sm))
3662 		return OCV_OCI_KDE_LEN;
3663 #endif /* CONFIG_OCV */
3664 	return 0;
3665 }
3666 
3667 
ocv_oci_add(struct wpa_state_machine * sm,u8 ** argpos,unsigned int freq)3668 static int ocv_oci_add(struct wpa_state_machine *sm, u8 **argpos,
3669 		       unsigned int freq)
3670 {
3671 #ifdef CONFIG_OCV
3672 	struct wpa_channel_info ci;
3673 
3674 	if (!wpa_auth_uses_ocv(sm))
3675 		return 0;
3676 
3677 	if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
3678 		wpa_printf(MSG_WARNING,
3679 			   "Failed to get channel info for OCI element");
3680 		return -1;
3681 	}
3682 #ifdef CONFIG_TESTING_OPTIONS
3683 	if (freq) {
3684 		wpa_printf(MSG_INFO,
3685 			   "TEST: Override OCI KDE frequency %d -> %u MHz",
3686 			   ci.frequency, freq);
3687 		ci.frequency = freq;
3688 	}
3689 #endif /* CONFIG_TESTING_OPTIONS */
3690 
3691 	return ocv_insert_oci_kde(&ci, argpos);
3692 #else /* CONFIG_OCV */
3693 	return 0;
3694 #endif /* CONFIG_OCV */
3695 }
3696 
3697 
3698 #ifdef CONFIG_TESTING_OPTIONS
replace_ie(const char * name,const u8 * old_buf,size_t * len,u8 eid,const u8 * ie,size_t ie_len)3699 static u8 * replace_ie(const char *name, const u8 *old_buf, size_t *len, u8 eid,
3700 		       const u8 *ie, size_t ie_len)
3701 {
3702 	const u8 *elem;
3703 	u8 *buf;
3704 
3705 	wpa_printf(MSG_DEBUG, "TESTING: %s EAPOL override", name);
3706 	wpa_hexdump(MSG_DEBUG, "TESTING: wpa_ie before override",
3707 		    old_buf, *len);
3708 	buf = os_malloc(*len + ie_len);
3709 	if (!buf)
3710 		return NULL;
3711 	os_memcpy(buf, old_buf, *len);
3712 	elem = get_ie(buf, *len, eid);
3713 	if (elem) {
3714 		u8 elem_len = 2 + elem[1];
3715 
3716 		os_memmove((void *) elem, elem + elem_len,
3717 			   *len - (elem - buf) - elem_len);
3718 		*len -= elem_len;
3719 	}
3720 	os_memcpy(buf + *len, ie, ie_len);
3721 	*len += ie_len;
3722 	wpa_hexdump(MSG_DEBUG, "TESTING: wpa_ie after EAPOL override",
3723 		    buf, *len);
3724 
3725 	return buf;
3726 }
3727 #endif /* CONFIG_TESTING_OPTIONS */
3728 
3729 
3730 #ifdef CONFIG_IEEE80211BE
3731 
wpa_auth_ml_get_rsn_info(struct wpa_authenticator * a,struct wpa_auth_ml_link_rsn_info * info)3732 void wpa_auth_ml_get_rsn_info(struct wpa_authenticator *a,
3733 			      struct wpa_auth_ml_link_rsn_info *info)
3734 {
3735 	info->rsn_ies = a->wpa_ie;
3736 	info->rsn_ies_len = a->wpa_ie_len;
3737 
3738 	wpa_printf(MSG_DEBUG, "RSN: MLD: link_id=%u, rsn_ies_len=%zu",
3739 		   info->link_id, info->rsn_ies_len);
3740 }
3741 
3742 
wpa_auth_get_ml_rsn_info(struct wpa_authenticator * wpa_auth,struct wpa_auth_ml_rsn_info * info)3743 static void wpa_auth_get_ml_rsn_info(struct wpa_authenticator *wpa_auth,
3744 				     struct wpa_auth_ml_rsn_info *info)
3745 {
3746 	if (!wpa_auth->cb->get_ml_rsn_info)
3747 		return;
3748 
3749 	wpa_auth->cb->get_ml_rsn_info(wpa_auth->cb_ctx, info);
3750 }
3751 
3752 
wpa_auth_ml_get_key_info(struct wpa_authenticator * a,struct wpa_auth_ml_link_key_info * info,bool mgmt_frame_prot,bool beacon_prot)3753 void wpa_auth_ml_get_key_info(struct wpa_authenticator *a,
3754 			      struct wpa_auth_ml_link_key_info *info,
3755 			      bool mgmt_frame_prot, bool beacon_prot)
3756 {
3757 	struct wpa_group *gsm = a->group;
3758 	u8 rsc[WPA_KEY_RSC_LEN];
3759 
3760 	wpa_printf(MSG_DEBUG,
3761 		   "MLD: Get group key info: link_id=%u, IGTK=%u, BIGTK=%u",
3762 		   info->link_id, mgmt_frame_prot, beacon_prot);
3763 
3764 	info->gtkidx = gsm->GN & 0x03;
3765 	info->gtk = gsm->GTK[gsm->GN - 1];
3766 	info->gtk_len = gsm->GTK_len;
3767 
3768 	if (wpa_auth_get_seqnum(a, NULL, gsm->GN, rsc) < 0)
3769 		os_memset(info->pn, 0, sizeof(info->pn));
3770 	else
3771 		os_memcpy(info->pn, rsc, sizeof(info->pn));
3772 
3773 	if (!mgmt_frame_prot)
3774 		return;
3775 
3776 	info->igtkidx = gsm->GN_igtk;
3777 	info->igtk = gsm->IGTK[gsm->GN_igtk - 4];
3778 	info->igtk_len = wpa_cipher_key_len(a->conf.group_mgmt_cipher);
3779 
3780 	if (wpa_auth_get_seqnum(a, NULL, gsm->GN_igtk, rsc) < 0)
3781 		os_memset(info->ipn, 0, sizeof(info->ipn));
3782 	else
3783 		os_memcpy(info->ipn, rsc, sizeof(info->ipn));
3784 
3785 	if (!beacon_prot)
3786 		return;
3787 
3788 	info->bigtkidx = gsm->GN_bigtk;
3789 	info->bigtk = gsm->BIGTK[gsm->GN_bigtk - 6];
3790 
3791 	if (wpa_auth_get_seqnum(a, NULL, gsm->GN_bigtk, rsc) < 0)
3792 		os_memset(info->bipn, 0, sizeof(info->bipn));
3793 	else
3794 		os_memcpy(info->bipn, rsc, sizeof(info->bipn));
3795 }
3796 
3797 
wpa_auth_get_ml_key_info(struct wpa_authenticator * wpa_auth,struct wpa_auth_ml_key_info * info)3798 static void wpa_auth_get_ml_key_info(struct wpa_authenticator *wpa_auth,
3799 				     struct wpa_auth_ml_key_info *info)
3800 {
3801 	if (!wpa_auth->cb->get_ml_key_info)
3802 		return;
3803 
3804 	wpa_auth->cb->get_ml_key_info(wpa_auth->cb_ctx, info);
3805 }
3806 
3807 
wpa_auth_ml_group_kdes_len(struct wpa_state_machine * sm)3808 static size_t wpa_auth_ml_group_kdes_len(struct wpa_state_machine *sm)
3809 {
3810 	struct wpa_group *gsm = sm->group;
3811 	size_t gtk_len = gsm->GTK_len;
3812 	size_t igtk_len;
3813 	size_t kde_len;
3814 	unsigned int n_links;
3815 
3816 	if (sm->mld_assoc_link_id < 0)
3817 		return 0;
3818 
3819 	n_links = sm->n_mld_affiliated_links + 1;
3820 
3821 	/* MLO GTK KDE for each link */
3822 	kde_len = n_links * (2 + RSN_SELECTOR_LEN + 1 + 6 + gtk_len);
3823 
3824 	if (!sm->mgmt_frame_prot)
3825 		return kde_len;
3826 
3827 	/* MLO IGTK KDE for each link */
3828 	igtk_len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3829 	kde_len += n_links * (2 + RSN_SELECTOR_LEN + 2 + 6 + 1 + igtk_len);
3830 
3831 	if (!sm->wpa_auth->conf.beacon_prot)
3832 		return kde_len;
3833 
3834 	/* MLO BIGTK KDE for each link */
3835 	kde_len += n_links * (2 + RSN_SELECTOR_LEN + 2 + 6 + 1 + igtk_len);
3836 
3837 	return kde_len;
3838 }
3839 
3840 
wpa_auth_ml_group_kdes(struct wpa_state_machine * sm,u8 * pos)3841 static u8 * wpa_auth_ml_group_kdes(struct wpa_state_machine *sm, u8 *pos)
3842 {
3843 	struct wpa_auth_ml_key_info ml_key_info;
3844 	unsigned int i, link_id;
3845 
3846 	/* First fetch the key information from all the authenticators */
3847 	os_memset(&ml_key_info, 0, sizeof(ml_key_info));
3848 	ml_key_info.n_mld_links = sm->n_mld_affiliated_links + 1;
3849 
3850 	/*
3851 	 * Assume that management frame protection and beacon protection are the
3852 	 * same on all links.
3853 	 */
3854 	ml_key_info.mgmt_frame_prot = sm->mgmt_frame_prot;
3855 	ml_key_info.beacon_prot = sm->wpa_auth->conf.beacon_prot;
3856 
3857 	for (i = 0, link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
3858 		if (!sm->mld_links[link_id].valid)
3859 			continue;
3860 
3861 		ml_key_info.links[i++].link_id = link_id;
3862 	}
3863 
3864 	wpa_auth_get_ml_key_info(sm->wpa_auth, &ml_key_info);
3865 
3866 	/* Add MLO GTK KDEs */
3867 	for (i = 0, link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
3868 		if (!sm->mld_links[link_id].valid)
3869 			continue;
3870 
3871 		wpa_printf(MSG_DEBUG, "RSN: MLO GTK: link=%u", link_id);
3872 		wpa_hexdump_key(MSG_DEBUG, "RSN: MLO GTK",
3873 				ml_key_info.links[i].gtk,
3874 				ml_key_info.links[i].gtk_len);
3875 
3876 		*pos++ = WLAN_EID_VENDOR_SPECIFIC;
3877 		*pos++ = RSN_SELECTOR_LEN + 1 + 6 +
3878 			ml_key_info.links[i].gtk_len;
3879 
3880 		RSN_SELECTOR_PUT(pos, RSN_KEY_DATA_MLO_GTK);
3881 		pos += RSN_SELECTOR_LEN;
3882 
3883 		*pos++ = (ml_key_info.links[i].gtkidx & 0x3) | (link_id << 4);
3884 
3885 		os_memcpy(pos, ml_key_info.links[i].pn, 6);
3886 		pos += 6;
3887 
3888 		os_memcpy(pos, ml_key_info.links[i].gtk,
3889 			  ml_key_info.links[i].gtk_len);
3890 		pos += ml_key_info.links[i].gtk_len;
3891 
3892 		i++;
3893 	}
3894 
3895 	if (!sm->mgmt_frame_prot)
3896 		return pos;
3897 
3898 	/* Add MLO IGTK KDEs */
3899 	for (i = 0, link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
3900 		if (!sm->mld_links[link_id].valid)
3901 			continue;
3902 
3903 		wpa_printf(MSG_DEBUG, "RSN: MLO IGTK: link=%u", link_id);
3904 		wpa_hexdump_key(MSG_DEBUG, "RSN: MLO IGTK",
3905 				ml_key_info.links[i].igtk,
3906 				ml_key_info.links[i].igtk_len);
3907 
3908 		*pos++ = WLAN_EID_VENDOR_SPECIFIC;
3909 		*pos++ = RSN_SELECTOR_LEN + 2 + 1 +
3910 			sizeof(ml_key_info.links[i].ipn) +
3911 			ml_key_info.links[i].igtk_len;
3912 
3913 		RSN_SELECTOR_PUT(pos, RSN_KEY_DATA_MLO_IGTK);
3914 		pos += RSN_SELECTOR_LEN;
3915 
3916 		/* Add the Key ID */
3917 		*pos++ = ml_key_info.links[i].igtkidx;
3918 		*pos++ = 0;
3919 
3920 		/* Add the IPN */
3921 		os_memcpy(pos, ml_key_info.links[i].ipn,
3922 			  sizeof(ml_key_info.links[i].ipn));
3923 		pos += sizeof(ml_key_info.links[i].ipn);
3924 
3925 		*pos++ = ml_key_info.links[i].link_id << 4;
3926 
3927 		os_memcpy(pos, ml_key_info.links[i].igtk,
3928 			  ml_key_info.links[i].igtk_len);
3929 		pos += ml_key_info.links[i].igtk_len;
3930 
3931 		i++;
3932 	}
3933 
3934 	if (!sm->wpa_auth->conf.beacon_prot)
3935 		return pos;
3936 
3937 	/* Add MLO BIGTK KDEs */
3938 	for (i = 0, link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
3939 		if (!sm->mld_links[link_id].valid)
3940 			continue;
3941 
3942 		wpa_printf(MSG_DEBUG, "RSN: MLO BIGTK: link=%u", link_id);
3943 		wpa_hexdump_key(MSG_DEBUG, "RSN: MLO BIGTK",
3944 				ml_key_info.links[i].bigtk,
3945 				ml_key_info.links[i].igtk_len);
3946 
3947 		*pos++ = WLAN_EID_VENDOR_SPECIFIC;
3948 		*pos++ = RSN_SELECTOR_LEN + 2 + 1 +
3949 			sizeof(ml_key_info.links[i].bipn) +
3950 			ml_key_info.links[i].igtk_len;
3951 
3952 		RSN_SELECTOR_PUT(pos, RSN_KEY_DATA_MLO_BIGTK);
3953 		pos += RSN_SELECTOR_LEN;
3954 
3955 		/* Add the Key ID */
3956 		*pos++ = ml_key_info.links[i].bigtkidx;
3957 		*pos++ = 0;
3958 
3959 		/* Add the BIPN */
3960 		os_memcpy(pos, ml_key_info.links[i].bipn,
3961 			  sizeof(ml_key_info.links[i].bipn));
3962 		pos += sizeof(ml_key_info.links[i].bipn);
3963 
3964 		*pos++ = ml_key_info.links[i].link_id << 4;
3965 
3966 		os_memcpy(pos, ml_key_info.links[i].bigtk,
3967 			  ml_key_info.links[i].igtk_len);
3968 		pos += ml_key_info.links[i].igtk_len;
3969 
3970 		i++;
3971 	}
3972 
3973 	return pos;
3974 }
3975 
3976 #endif /* CONFIG_IEEE80211BE */
3977 
3978 
wpa_auth_ml_kdes_len(struct wpa_state_machine * sm)3979 static size_t wpa_auth_ml_kdes_len(struct wpa_state_machine *sm)
3980 {
3981 	size_t kde_len = 0;
3982 
3983 #ifdef CONFIG_IEEE80211BE
3984 	unsigned int link_id;
3985 
3986 	if (sm->mld_assoc_link_id < 0)
3987 		return 0;
3988 
3989 	/* For the MAC Address KDE */
3990 	kde_len = 2 + RSN_SELECTOR_LEN + ETH_ALEN;
3991 
3992 	/* MLO Link KDE for each link */
3993 	for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
3994 		if (!sm->mld_links[link_id].valid)
3995 			continue;
3996 
3997 		kde_len += 2 + RSN_SELECTOR_LEN + 1 + ETH_ALEN +
3998 			sm->mld_links[link_id].rsne_len +
3999 			sm->mld_links[link_id].rsnxe_len;
4000 	}
4001 
4002 	kde_len += wpa_auth_ml_group_kdes_len(sm);
4003 #endif /* CONFIG_IEEE80211BE */
4004 
4005 	return kde_len;
4006 }
4007 
4008 
wpa_auth_ml_kdes(struct wpa_state_machine * sm,u8 * pos)4009 static u8 * wpa_auth_ml_kdes(struct wpa_state_machine *sm, u8 *pos)
4010 {
4011 #ifdef CONFIG_IEEE80211BE
4012 	u8 link_id;
4013 
4014 	if (sm->mld_assoc_link_id < 0)
4015 		return pos;
4016 
4017 	wpa_printf(MSG_DEBUG, "RSN: MLD: Adding MAC Address KDE");
4018 	pos = wpa_add_kde(pos, RSN_KEY_DATA_MAC_ADDR,
4019 			  sm->own_mld_addr, ETH_ALEN, NULL, 0);
4020 
4021 	for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
4022 		if (!sm->mld_links[link_id].valid)
4023 			continue;
4024 
4025 		wpa_printf(MSG_DEBUG,
4026 			   "RSN: MLO Link: link=%u, len=%zu", link_id,
4027 			   RSN_SELECTOR_LEN + 1 + ETH_ALEN +
4028 			   sm->mld_links[link_id].rsne_len +
4029 			   sm->mld_links[link_id].rsnxe_len);
4030 
4031 		*pos++ = WLAN_EID_VENDOR_SPECIFIC;
4032 		*pos++ = RSN_SELECTOR_LEN + 1 + ETH_ALEN +
4033 			sm->mld_links[link_id].rsne_len +
4034 			sm->mld_links[link_id].rsnxe_len;
4035 
4036 		RSN_SELECTOR_PUT(pos, RSN_KEY_DATA_MLO_LINK);
4037 		pos += RSN_SELECTOR_LEN;
4038 
4039 		/* Add the Link Information */
4040 		*pos = link_id;
4041 		if (sm->mld_links[link_id].rsne_len)
4042 			*pos |= RSN_MLO_LINK_KDE_LI_RSNE_INFO;
4043 		if (sm->mld_links[link_id].rsnxe_len)
4044 			*pos |= RSN_MLO_LINK_KDE_LI_RSNXE_INFO;
4045 
4046 		pos++;
4047 		os_memcpy(pos, sm->mld_links[link_id].own_addr, ETH_ALEN);
4048 		pos += ETH_ALEN;
4049 
4050 		if (sm->mld_links[link_id].rsne_len) {
4051 			os_memcpy(pos, sm->mld_links[link_id].rsne,
4052 				  sm->mld_links[link_id].rsne_len);
4053 			pos += sm->mld_links[link_id].rsne_len;
4054 		}
4055 
4056 		if (sm->mld_links[link_id].rsnxe_len) {
4057 			os_memcpy(pos, sm->mld_links[link_id].rsnxe,
4058 				  sm->mld_links[link_id].rsnxe_len);
4059 			pos += sm->mld_links[link_id].rsnxe_len;
4060 		}
4061 	}
4062 
4063 	pos = wpa_auth_ml_group_kdes(sm, pos);
4064 #endif /* CONFIG_IEEE80211BE */
4065 
4066 	return pos;
4067 }
4068 
4069 
SM_STATE(WPA_PTK,PTKINITNEGOTIATING)4070 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
4071 {
4072 	u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde = NULL, *pos, stub_gtk[32];
4073 	size_t gtk_len, kde_len = 0, wpa_ie_len;
4074 	struct wpa_group *gsm = sm->group;
4075 	u8 *wpa_ie;
4076 	int secure, gtkidx, encr = 0;
4077 	u8 *wpa_ie_buf = NULL, *wpa_ie_buf2 = NULL;
4078 	u8 hdr[2];
4079 	struct wpa_auth_config *conf = &sm->wpa_auth->conf;
4080 #ifdef CONFIG_IEEE80211BE
4081 	bool is_mld = sm->mld_assoc_link_id >= 0;
4082 #else /* CONFIG_IEEE80211BE */
4083 	bool is_mld = false;
4084 #endif /* CONFIG_IEEE80211BE */
4085 
4086 	SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
4087 	sm->TimeoutEvt = false;
4088 
4089 	sm->TimeoutCtr++;
4090 	if (conf->wpa_disable_eapol_key_retries && sm->TimeoutCtr > 1) {
4091 		/* Do not allow retransmission of EAPOL-Key msg 3/4 */
4092 		return;
4093 	}
4094 	if (sm->TimeoutCtr > conf->wpa_pairwise_update_count) {
4095 		/* No point in sending the EAPOL-Key - we will disconnect
4096 		 * immediately following this. */
4097 		return;
4098 	}
4099 
4100 	/* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
4101 	   GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2])
4102 	 */
4103 	os_memset(rsc, 0, WPA_KEY_RSC_LEN);
4104 	wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
4105 	/* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
4106 	wpa_ie = sm->wpa_auth->wpa_ie;
4107 	wpa_ie_len = sm->wpa_auth->wpa_ie_len;
4108 	if (sm->wpa == WPA_VERSION_WPA && (conf->wpa & WPA_PROTO_RSN) &&
4109 	    wpa_ie_len > wpa_ie[1] + 2U && wpa_ie[0] == WLAN_EID_RSN) {
4110 		/* WPA-only STA, remove RSN IE and possible MDIE */
4111 		wpa_ie = wpa_ie + wpa_ie[1] + 2;
4112 		if (wpa_ie[0] == WLAN_EID_RSNX)
4113 			wpa_ie = wpa_ie + wpa_ie[1] + 2;
4114 		if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
4115 			wpa_ie = wpa_ie + wpa_ie[1] + 2;
4116 		wpa_ie_len = wpa_ie[1] + 2;
4117 	}
4118 #ifdef CONFIG_TESTING_OPTIONS
4119 	if (conf->rsne_override_eapol_set) {
4120 		wpa_ie_buf2 = replace_ie(
4121 			"RSNE", wpa_ie, &wpa_ie_len, WLAN_EID_RSN,
4122 			conf->rsne_override_eapol,
4123 			conf->rsne_override_eapol_len);
4124 		if (!wpa_ie_buf2)
4125 			goto done;
4126 		wpa_ie = wpa_ie_buf2;
4127 	}
4128 	if (conf->rsnxe_override_eapol_set) {
4129 		wpa_ie_buf = replace_ie(
4130 			"RSNXE", wpa_ie, &wpa_ie_len, WLAN_EID_RSNX,
4131 			conf->rsnxe_override_eapol,
4132 			conf->rsnxe_override_eapol_len);
4133 		if (!wpa_ie_buf)
4134 			goto done;
4135 		wpa_ie = wpa_ie_buf;
4136 	}
4137 #endif /* CONFIG_TESTING_OPTIONS */
4138 	wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
4139 			"sending 3/4 msg of 4-Way Handshake");
4140 	if (sm->wpa == WPA_VERSION_WPA2) {
4141 		if (sm->use_ext_key_id && sm->TimeoutCtr == 1 &&
4142 		    wpa_auth_set_key(sm->wpa_auth, 0,
4143 				     wpa_cipher_to_alg(sm->pairwise),
4144 				     sm->addr,
4145 				     sm->keyidx_active, sm->PTK.tk,
4146 				     wpa_cipher_key_len(sm->pairwise),
4147 				     KEY_FLAG_PAIRWISE_RX)) {
4148 			wpa_sta_disconnect(sm->wpa_auth, sm->addr,
4149 					   WLAN_REASON_PREV_AUTH_NOT_VALID);
4150 			return;
4151 		}
4152 
4153 #ifdef CONFIG_PASN
4154 		if (sm->wpa_auth->conf.secure_ltf &&
4155 		    ieee802_11_rsnx_capab(sm->rsnxe,
4156 					  WLAN_RSNX_CAPAB_SECURE_LTF) &&
4157 		    wpa_auth_set_ltf_keyseed(sm->wpa_auth, sm->addr,
4158 					     sm->PTK.ltf_keyseed,
4159 					     sm->PTK.ltf_keyseed_len)) {
4160 			wpa_printf(MSG_ERROR,
4161 				   "WPA: Failed to set LTF keyseed to driver");
4162 			wpa_sta_disconnect(sm->wpa_auth, sm->addr,
4163 					   WLAN_REASON_PREV_AUTH_NOT_VALID);
4164 			return;
4165 		}
4166 #endif /* CONFIG_PASN */
4167 
4168 		/* WPA2 send GTK in the 4-way handshake */
4169 		secure = 1;
4170 		gtk = gsm->GTK[gsm->GN - 1];
4171 		gtk_len = gsm->GTK_len;
4172 		if (conf->disable_gtk ||
4173 		    sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
4174 			/*
4175 			 * Provide unique random GTK to each STA to prevent use
4176 			 * of GTK in the BSS.
4177 			 */
4178 			if (random_get_bytes(stub_gtk, gtk_len) < 0)
4179 				goto done;
4180 			gtk = stub_gtk;
4181 		}
4182 		gtkidx = gsm->GN;
4183 		_rsc = rsc;
4184 		encr = 1;
4185 	} else {
4186 		/* WPA does not include GTK in msg 3/4 */
4187 		secure = 0;
4188 		gtk = NULL;
4189 		gtk_len = 0;
4190 		gtkidx = 0;
4191 		_rsc = NULL;
4192 		if (sm->rx_eapol_key_secure) {
4193 			/*
4194 			 * It looks like Windows 7 supplicant tries to use
4195 			 * Secure bit in msg 2/4 after having reported Michael
4196 			 * MIC failure and it then rejects the 4-way handshake
4197 			 * if msg 3/4 does not set Secure bit. Work around this
4198 			 * by setting the Secure bit here even in the case of
4199 			 * WPA if the supplicant used it first.
4200 			 */
4201 			wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm),
4202 					LOGGER_DEBUG,
4203 					"STA used Secure bit in WPA msg 2/4 - set Secure for 3/4 as workaround");
4204 			secure = 1;
4205 		}
4206 	}
4207 
4208 	kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
4209 
4210 	if (sm->use_ext_key_id)
4211 		kde_len += 2 + RSN_SELECTOR_LEN + 2;
4212 
4213 	if (gtk)
4214 		kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
4215 #ifdef CONFIG_IEEE80211R_AP
4216 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4217 		kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
4218 		kde_len += 300; /* FTIE + 2 * TIE */
4219 	}
4220 #endif /* CONFIG_IEEE80211R_AP */
4221 #ifdef CONFIG_P2P
4222 	if (WPA_GET_BE32(sm->ip_addr) > 0)
4223 		kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
4224 #endif /* CONFIG_P2P */
4225 
4226 	if (conf->transition_disable)
4227 		kde_len += 2 + RSN_SELECTOR_LEN + 1;
4228 
4229 #ifdef CONFIG_DPP2
4230 	if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
4231 		kde_len += 2 + RSN_SELECTOR_LEN + 2;
4232 #endif /* CONFIG_DPP2 */
4233 
4234 	kde_len += wpa_auth_ml_kdes_len(sm);
4235 
4236 	kde = os_malloc(kde_len);
4237 	if (!kde)
4238 		goto done;
4239 
4240 	pos = kde;
4241 	if (!is_mld) {
4242 		os_memcpy(pos, wpa_ie, wpa_ie_len);
4243 		pos += wpa_ie_len;
4244 	}
4245 #ifdef CONFIG_IEEE80211R_AP
4246 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4247 		int res;
4248 		size_t elen;
4249 
4250 		elen = pos - kde;
4251 		res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
4252 		if (res < 0) {
4253 			wpa_printf(MSG_ERROR,
4254 				   "FT: Failed to insert PMKR1Name into RSN IE in EAPOL-Key data");
4255 			goto done;
4256 		}
4257 		pos -= wpa_ie_len;
4258 		pos += elen;
4259 	}
4260 #endif /* CONFIG_IEEE80211R_AP */
4261 	hdr[1] = 0;
4262 
4263 	if (sm->use_ext_key_id) {
4264 		hdr[0] = sm->keyidx_active & 0x01;
4265 		pos = wpa_add_kde(pos, RSN_KEY_DATA_KEYID, hdr, 2, NULL, 0);
4266 	}
4267 
4268 	if (gtk && !is_mld) {
4269 		hdr[0] = gtkidx & 0x03;
4270 		pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
4271 				  gtk, gtk_len);
4272 	}
4273 	pos = ieee80211w_kde_add(sm, pos);
4274 	if (ocv_oci_add(sm, &pos, conf->oci_freq_override_eapol_m3) < 0)
4275 		goto done;
4276 
4277 #ifdef CONFIG_IEEE80211R_AP
4278 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
4279 		int res;
4280 
4281 		if (sm->assoc_resp_ftie &&
4282 		    kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
4283 			os_memcpy(pos, sm->assoc_resp_ftie,
4284 				  2 + sm->assoc_resp_ftie[1]);
4285 			res = 2 + sm->assoc_resp_ftie[1];
4286 		} else {
4287 			res = wpa_write_ftie(conf, sm->wpa_key_mgmt,
4288 					     sm->xxkey_len,
4289 					     conf->r0_key_holder,
4290 					     conf->r0_key_holder_len,
4291 					     NULL, NULL, pos,
4292 					     kde + kde_len - pos,
4293 					     NULL, 0, 0);
4294 		}
4295 		if (res < 0) {
4296 			wpa_printf(MSG_ERROR,
4297 				   "FT: Failed to insert FTIE into EAPOL-Key Key Data");
4298 			goto done;
4299 		}
4300 		pos += res;
4301 
4302 		/* TIE[ReassociationDeadline] (TU) */
4303 		*pos++ = WLAN_EID_TIMEOUT_INTERVAL;
4304 		*pos++ = 5;
4305 		*pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
4306 		WPA_PUT_LE32(pos, conf->reassociation_deadline);
4307 		pos += 4;
4308 
4309 		/* TIE[KeyLifetime] (seconds) */
4310 		*pos++ = WLAN_EID_TIMEOUT_INTERVAL;
4311 		*pos++ = 5;
4312 		*pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
4313 		WPA_PUT_LE32(pos, conf->r0_key_lifetime);
4314 		pos += 4;
4315 	}
4316 #endif /* CONFIG_IEEE80211R_AP */
4317 #ifdef CONFIG_P2P
4318 	if (WPA_GET_BE32(sm->ip_addr) > 0) {
4319 		u8 addr[3 * 4];
4320 		os_memcpy(addr, sm->ip_addr, 4);
4321 		os_memcpy(addr + 4, conf->ip_addr_mask, 4);
4322 		os_memcpy(addr + 8, conf->ip_addr_go, 4);
4323 		pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
4324 				  addr, sizeof(addr), NULL, 0);
4325 	}
4326 #endif /* CONFIG_P2P */
4327 
4328 	if (conf->transition_disable)
4329 		pos = wpa_add_kde(pos, WFA_KEY_DATA_TRANSITION_DISABLE,
4330 				  &conf->transition_disable, 1, NULL, 0);
4331 
4332 #ifdef CONFIG_DPP2
4333 	if (DPP_VERSION > 1 && sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) {
4334 		u8 payload[2];
4335 
4336 		payload[0] = DPP_VERSION; /* Protocol Version */
4337 		payload[1] = 0; /* Flags */
4338 		if (conf->dpp_pfs == 0)
4339 			payload[1] |= DPP_KDE_PFS_ALLOWED;
4340 		else if (conf->dpp_pfs == 1)
4341 			payload[1] |= DPP_KDE_PFS_ALLOWED |
4342 				DPP_KDE_PFS_REQUIRED;
4343 		pos = wpa_add_kde(pos, WFA_KEY_DATA_DPP,
4344 				  payload, sizeof(payload), NULL, 0);
4345 	}
4346 #endif /* CONFIG_DPP2 */
4347 
4348 	pos = wpa_auth_ml_kdes(sm, pos);
4349 
4350 	wpa_send_eapol(sm->wpa_auth, sm,
4351 		       (secure ? WPA_KEY_INFO_SECURE : 0) |
4352 		       (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
4353 			WPA_KEY_INFO_MIC : 0) |
4354 		       WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
4355 		       WPA_KEY_INFO_KEY_TYPE,
4356 		       _rsc, sm->ANonce, kde, pos - kde, 0, encr);
4357 done:
4358 	bin_clear_free(kde, kde_len);
4359 	os_free(wpa_ie_buf);
4360 	os_free(wpa_ie_buf2);
4361 }
4362 
4363 
wpa_auth_validate_ml_kdes_m4(struct wpa_state_machine * sm)4364 static int wpa_auth_validate_ml_kdes_m4(struct wpa_state_machine *sm)
4365 {
4366 #ifdef CONFIG_IEEE80211BE
4367 	const struct ieee802_1x_hdr *hdr;
4368 	const struct wpa_eapol_key *key;
4369 	struct wpa_eapol_ie_parse kde;
4370 	const u8 *key_data, *mic;
4371 	u16 key_data_length;
4372 	size_t mic_len;
4373 
4374 	if (sm->mld_assoc_link_id < 0)
4375 		return 0;
4376 
4377 	/*
4378 	 * Note: last_rx_eapol_key length fields have already been validated in
4379 	 * wpa_receive().
4380 	 */
4381 	mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
4382 
4383 	hdr = (const struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
4384 	key = (const struct wpa_eapol_key *) (hdr + 1);
4385 	mic = (const u8 *) (key + 1);
4386 	key_data = mic + mic_len + 2;
4387 	key_data_length = WPA_GET_BE16(mic + mic_len);
4388 	if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
4389 	    sizeof(*key) - mic_len - 2)
4390 		return -1;
4391 
4392 	if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
4393 		wpa_auth_vlogger(sm->wpa_auth, wpa_auth_get_spa(sm),
4394 				 LOGGER_INFO,
4395 				 "received EAPOL-Key msg 4/4 with invalid Key Data contents");
4396 		return -1;
4397 	}
4398 
4399 	/* MLD MAC address must be the same */
4400 	if (!kde.mac_addr ||
4401 	    os_memcmp(kde.mac_addr, sm->peer_mld_addr, ETH_ALEN) != 0) {
4402 		wpa_printf(MSG_DEBUG,
4403 			   "MLD: Mismatching or missing MLD address in EAPOL-Key msg 4/4");
4404 		return -1;
4405 	}
4406 
4407 	wpa_printf(MSG_DEBUG, "MLD: MLD address in EAPOL-Key msg 4/4: " MACSTR,
4408 		   MAC2STR(kde.mac_addr));
4409 #endif /* CONFIG_IEEE80211BE */
4410 
4411 	return 0;
4412 }
4413 
4414 
SM_STATE(WPA_PTK,PTKINITDONE)4415 SM_STATE(WPA_PTK, PTKINITDONE)
4416 {
4417 	SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
4418 	sm->EAPOLKeyReceived = false;
4419 
4420 	if (wpa_auth_validate_ml_kdes_m4(sm) < 0) {
4421 		wpa_sta_disconnect(sm->wpa_auth, sm->addr,
4422 				   WLAN_REASON_PREV_AUTH_NOT_VALID);
4423 		return;
4424 	}
4425 
4426 	if (sm->Pair) {
4427 		enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
4428 		int klen = wpa_cipher_key_len(sm->pairwise);
4429 		int res;
4430 
4431 		if (sm->use_ext_key_id)
4432 			res = wpa_auth_set_key(sm->wpa_auth, 0, 0, sm->addr,
4433 					       sm->keyidx_active, NULL, 0,
4434 					       KEY_FLAG_PAIRWISE_RX_TX_MODIFY);
4435 		else
4436 			res = wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr,
4437 					       0, sm->PTK.tk, klen,
4438 					       KEY_FLAG_PAIRWISE_RX_TX);
4439 		if (res) {
4440 			wpa_sta_disconnect(sm->wpa_auth, sm->addr,
4441 					   WLAN_REASON_PREV_AUTH_NOT_VALID);
4442 			return;
4443 		}
4444 
4445 #ifdef CONFIG_PASN
4446 		if (sm->wpa_auth->conf.secure_ltf &&
4447 		    ieee802_11_rsnx_capab(sm->rsnxe,
4448 					  WLAN_RSNX_CAPAB_SECURE_LTF) &&
4449 		    wpa_auth_set_ltf_keyseed(sm->wpa_auth, sm->addr,
4450 					     sm->PTK.ltf_keyseed,
4451 					     sm->PTK.ltf_keyseed_len)) {
4452 			wpa_printf(MSG_ERROR,
4453 				   "WPA: Failed to set LTF keyseed to driver");
4454 			wpa_sta_disconnect(sm->wpa_auth, sm->addr,
4455 					   WLAN_REASON_PREV_AUTH_NOT_VALID);
4456 			return;
4457 		}
4458 #endif /* CONFIG_PASN */
4459 
4460 		/* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
4461 		sm->pairwise_set = true;
4462 
4463 		wpa_auth_set_ptk_rekey_timer(sm);
4464 		wpa_auth_store_ptksa(sm->wpa_auth, sm->addr, sm->pairwise,
4465 				     dot11RSNAConfigPMKLifetime, &sm->PTK);
4466 
4467 		if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
4468 		    sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
4469 		    sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
4470 			wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
4471 					   WPA_EAPOL_authorized, 1);
4472 		}
4473 	}
4474 
4475 	if (0 /* IBSS == TRUE */) {
4476 		sm->keycount++;
4477 		if (sm->keycount == 2) {
4478 			wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
4479 					   WPA_EAPOL_portValid, 1);
4480 		}
4481 	} else {
4482 		wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
4483 				   1);
4484 	}
4485 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable,
4486 			   false);
4487 	wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, true);
4488 	if (sm->wpa == WPA_VERSION_WPA)
4489 		sm->PInitAKeys = true;
4490 	else
4491 		sm->has_GTK = true;
4492 	wpa_auth_vlogger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
4493 			 "pairwise key handshake completed (%s)",
4494 			 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
4495 	wpa_msg(sm->wpa_auth->conf.msg_ctx, MSG_INFO, "EAPOL-4WAY-HS-COMPLETED "
4496 		MACSTR, MAC2STR(sm->addr));
4497 
4498 #ifdef CONFIG_IEEE80211R_AP
4499 	wpa_ft_push_pmk_r1(sm->wpa_auth, wpa_auth_get_spa(sm));
4500 #endif /* CONFIG_IEEE80211R_AP */
4501 
4502 	sm->ptkstart_without_success = 0;
4503 }
4504 
4505 
SM_STEP(WPA_PTK)4506 SM_STEP(WPA_PTK)
4507 {
4508 	struct wpa_authenticator *wpa_auth = sm->wpa_auth;
4509 	struct wpa_auth_config *conf = &wpa_auth->conf;
4510 
4511 	if (sm->Init)
4512 		SM_ENTER(WPA_PTK, INITIALIZE);
4513 	else if (sm->Disconnect
4514 		 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
4515 		wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
4516 				"WPA_PTK: sm->Disconnect");
4517 		SM_ENTER(WPA_PTK, DISCONNECT);
4518 	}
4519 	else if (sm->DeauthenticationRequest)
4520 		SM_ENTER(WPA_PTK, DISCONNECTED);
4521 	else if (sm->AuthenticationRequest)
4522 		SM_ENTER(WPA_PTK, AUTHENTICATION);
4523 	else if (sm->ReAuthenticationRequest)
4524 		SM_ENTER(WPA_PTK, AUTHENTICATION2);
4525 	else if (sm->PTKRequest) {
4526 		if (wpa_auth_sm_ptk_update(sm) < 0)
4527 			SM_ENTER(WPA_PTK, DISCONNECTED);
4528 		else
4529 			SM_ENTER(WPA_PTK, PTKSTART);
4530 	} else switch (sm->wpa_ptk_state) {
4531 	case WPA_PTK_INITIALIZE:
4532 		break;
4533 	case WPA_PTK_DISCONNECT:
4534 		SM_ENTER(WPA_PTK, DISCONNECTED);
4535 		break;
4536 	case WPA_PTK_DISCONNECTED:
4537 		SM_ENTER(WPA_PTK, INITIALIZE);
4538 		break;
4539 	case WPA_PTK_AUTHENTICATION:
4540 		SM_ENTER(WPA_PTK, AUTHENTICATION2);
4541 		break;
4542 	case WPA_PTK_AUTHENTICATION2:
4543 		if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
4544 		    wpa_auth_get_eapol(wpa_auth, sm->addr,
4545 				       WPA_EAPOL_keyRun))
4546 			SM_ENTER(WPA_PTK, INITPMK);
4547 		else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
4548 			 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
4549 			 /* FIX: && 802.1X::keyRun */)
4550 			SM_ENTER(WPA_PTK, INITPSK);
4551 		else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
4552 			SM_ENTER(WPA_PTK, INITPMK);
4553 		break;
4554 	case WPA_PTK_INITPMK:
4555 		if (wpa_auth_get_eapol(wpa_auth, sm->addr,
4556 				       WPA_EAPOL_keyAvailable)) {
4557 			SM_ENTER(WPA_PTK, PTKSTART);
4558 #ifdef CONFIG_DPP
4559 		} else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->pmksa) {
4560 			SM_ENTER(WPA_PTK, PTKSTART);
4561 #endif /* CONFIG_DPP */
4562 		} else {
4563 			wpa_auth->dot11RSNA4WayHandshakeFailures++;
4564 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
4565 					LOGGER_INFO,
4566 					"INITPMK - keyAvailable = false");
4567 			SM_ENTER(WPA_PTK, DISCONNECT);
4568 		}
4569 		break;
4570 	case WPA_PTK_INITPSK:
4571 		if (wpa_auth_get_psk(wpa_auth, sm->addr, sm->p2p_dev_addr,
4572 				     NULL, NULL, NULL)) {
4573 			SM_ENTER(WPA_PTK, PTKSTART);
4574 #ifdef CONFIG_SAE
4575 		} else if (wpa_auth_uses_sae(sm) && sm->pmksa) {
4576 			SM_ENTER(WPA_PTK, PTKSTART);
4577 #endif /* CONFIG_SAE */
4578 		} else if (wpa_key_mgmt_wpa_psk_no_sae(sm->wpa_key_mgmt) &&
4579 			   wpa_auth->conf.radius_psk) {
4580 			wpa_printf(MSG_DEBUG,
4581 				   "INITPSK: No PSK yet available for STA - use RADIUS later");
4582 			SM_ENTER(WPA_PTK, PTKSTART);
4583 		} else {
4584 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
4585 					LOGGER_INFO,
4586 					"no PSK configured for the STA");
4587 			wpa_auth->dot11RSNA4WayHandshakeFailures++;
4588 			SM_ENTER(WPA_PTK, DISCONNECT);
4589 		}
4590 		break;
4591 	case WPA_PTK_PTKSTART:
4592 		if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
4593 		    sm->EAPOLKeyPairwise)
4594 			SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
4595 		else if (sm->TimeoutCtr > conf->wpa_pairwise_update_count) {
4596 			wpa_auth->dot11RSNA4WayHandshakeFailures++;
4597 			wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
4598 					 LOGGER_DEBUG,
4599 					 "PTKSTART: Retry limit %u reached",
4600 					 conf->wpa_pairwise_update_count);
4601 			sm->disconnect_reason =
4602 				WLAN_REASON_4WAY_HANDSHAKE_TIMEOUT;
4603 			SM_ENTER(WPA_PTK, DISCONNECT);
4604 		} else if (sm->TimeoutEvt)
4605 			SM_ENTER(WPA_PTK, PTKSTART);
4606 		break;
4607 	case WPA_PTK_PTKCALCNEGOTIATING:
4608 		if (sm->MICVerified)
4609 			SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
4610 		else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
4611 			 sm->EAPOLKeyPairwise)
4612 			SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
4613 		else if (sm->TimeoutEvt)
4614 			SM_ENTER(WPA_PTK, PTKSTART);
4615 		break;
4616 	case WPA_PTK_PTKCALCNEGOTIATING2:
4617 		SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
4618 		break;
4619 	case WPA_PTK_PTKINITNEGOTIATING:
4620 		if (sm->update_snonce)
4621 			SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
4622 		else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
4623 			 sm->EAPOLKeyPairwise && sm->MICVerified)
4624 			SM_ENTER(WPA_PTK, PTKINITDONE);
4625 		else if (sm->TimeoutCtr >
4626 			 conf->wpa_pairwise_update_count ||
4627 			 (conf->wpa_disable_eapol_key_retries &&
4628 			  sm->TimeoutCtr > 1)) {
4629 			wpa_auth->dot11RSNA4WayHandshakeFailures++;
4630 			wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
4631 					 LOGGER_DEBUG,
4632 					 "PTKINITNEGOTIATING: Retry limit %u reached",
4633 					 conf->wpa_pairwise_update_count);
4634 			sm->disconnect_reason =
4635 				WLAN_REASON_4WAY_HANDSHAKE_TIMEOUT;
4636 			SM_ENTER(WPA_PTK, DISCONNECT);
4637 		} else if (sm->TimeoutEvt)
4638 			SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
4639 		break;
4640 	case WPA_PTK_PTKINITDONE:
4641 		break;
4642 	}
4643 }
4644 
4645 
SM_STATE(WPA_PTK_GROUP,IDLE)4646 SM_STATE(WPA_PTK_GROUP, IDLE)
4647 {
4648 	SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
4649 	if (sm->Init) {
4650 		/* Init flag is not cleared here, so avoid busy
4651 		 * loop by claiming nothing changed. */
4652 		sm->changed = false;
4653 	}
4654 	sm->GTimeoutCtr = 0;
4655 }
4656 
4657 
SM_STATE(WPA_PTK_GROUP,REKEYNEGOTIATING)4658 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
4659 {
4660 	u8 rsc[WPA_KEY_RSC_LEN];
4661 	struct wpa_group *gsm = sm->group;
4662 	const u8 *kde = NULL;
4663 	u8 *kde_buf = NULL, *pos, hdr[2];
4664 	size_t kde_len = 0;
4665 	u8 *gtk, stub_gtk[32];
4666 	struct wpa_auth_config *conf = &sm->wpa_auth->conf;
4667 	bool is_mld = false;
4668 
4669 #ifdef CONFIG_IEEE80211BE
4670 	is_mld = sm->mld_assoc_link_id >= 0;
4671 #endif /* CONFIG_IEEE80211BE */
4672 
4673 	SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
4674 
4675 	sm->GTimeoutCtr++;
4676 	if (conf->wpa_disable_eapol_key_retries && sm->GTimeoutCtr > 1) {
4677 		/* Do not allow retransmission of EAPOL-Key group msg 1/2 */
4678 		return;
4679 	}
4680 	if (sm->GTimeoutCtr > conf->wpa_group_update_count) {
4681 		/* No point in sending the EAPOL-Key - we will disconnect
4682 		 * immediately following this. */
4683 		return;
4684 	}
4685 
4686 	if (sm->wpa == WPA_VERSION_WPA)
4687 		sm->PInitAKeys = false;
4688 	sm->TimeoutEvt = false;
4689 	/* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
4690 	os_memset(rsc, 0, WPA_KEY_RSC_LEN);
4691 	if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
4692 		wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
4693 	wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
4694 			"sending 1/2 msg of Group Key Handshake");
4695 
4696 	gtk = gsm->GTK[gsm->GN - 1];
4697 	if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
4698 		/*
4699 		 * Provide unique random GTK to each STA to prevent use
4700 		 * of GTK in the BSS.
4701 		 */
4702 		if (random_get_bytes(stub_gtk, gsm->GTK_len) < 0)
4703 			return;
4704 		gtk = stub_gtk;
4705 	}
4706 
4707 	if (sm->wpa == WPA_VERSION_WPA2 && !is_mld) {
4708 		kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
4709 			ieee80211w_kde_len(sm) + ocv_oci_len(sm);
4710 		kde_buf = os_malloc(kde_len);
4711 		if (!kde_buf)
4712 			return;
4713 
4714 		kde = pos = kde_buf;
4715 		hdr[0] = gsm->GN & 0x03;
4716 		hdr[1] = 0;
4717 		pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
4718 				  gtk, gsm->GTK_len);
4719 		pos = ieee80211w_kde_add(sm, pos);
4720 		if (ocv_oci_add(sm, &pos,
4721 				conf->oci_freq_override_eapol_g1) < 0) {
4722 			os_free(kde_buf);
4723 			return;
4724 		}
4725 		kde_len = pos - kde;
4726 #ifdef CONFIG_IEEE80211BE
4727 	} else if (sm->wpa == WPA_VERSION_WPA2 && is_mld) {
4728 		kde_len = wpa_auth_ml_group_kdes_len(sm);
4729 		if (kde_len) {
4730 			kde_buf = os_malloc(kde_len);
4731 			if (!kde_buf)
4732 				return;
4733 
4734 			kde = pos = kde_buf;
4735 			wpa_auth_ml_group_kdes(sm, pos);
4736 		}
4737 #endif /* CONFIG_IEEE80211BE */
4738 	} else {
4739 		kde = gtk;
4740 		kde_len = gsm->GTK_len;
4741 	}
4742 
4743 	wpa_send_eapol(sm->wpa_auth, sm,
4744 		       WPA_KEY_INFO_SECURE |
4745 		       (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
4746 			WPA_KEY_INFO_MIC : 0) |
4747 		       WPA_KEY_INFO_ACK |
4748 		       (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
4749 		       rsc, NULL, kde, kde_len, gsm->GN, 1);
4750 
4751 	bin_clear_free(kde_buf, kde_len);
4752 }
4753 
4754 
SM_STATE(WPA_PTK_GROUP,REKEYESTABLISHED)4755 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
4756 {
4757 	struct wpa_authenticator *wpa_auth = sm->wpa_auth;
4758 #ifdef CONFIG_OCV
4759 	const u8 *key_data, *mic;
4760 	struct ieee802_1x_hdr *hdr;
4761 	struct wpa_eapol_key *key;
4762 	struct wpa_eapol_ie_parse kde;
4763 	size_t mic_len;
4764 	u16 key_data_length;
4765 #endif /* CONFIG_OCV */
4766 
4767 	SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
4768 	sm->EAPOLKeyReceived = false;
4769 
4770 #ifdef CONFIG_OCV
4771 	mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
4772 
4773 	/*
4774 	 * Note: last_rx_eapol_key length fields have already been validated in
4775 	 * wpa_receive().
4776 	 */
4777 	hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
4778 	key = (struct wpa_eapol_key *) (hdr + 1);
4779 	mic = (u8 *) (key + 1);
4780 	key_data = mic + mic_len + 2;
4781 	key_data_length = WPA_GET_BE16(mic + mic_len);
4782 	if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
4783 	    sizeof(*key) - mic_len - 2)
4784 		return;
4785 
4786 	if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
4787 		wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
4788 				 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
4789 		return;
4790 	}
4791 
4792 	if (wpa_auth_uses_ocv(sm)) {
4793 		struct wpa_channel_info ci;
4794 		int tx_chanwidth;
4795 		int tx_seg1_idx;
4796 
4797 		if (wpa_channel_info(wpa_auth, &ci) != 0) {
4798 			wpa_auth_logger(wpa_auth, wpa_auth_get_spa(sm),
4799 					LOGGER_INFO,
4800 					"Failed to get channel info to validate received OCI in EAPOL-Key group 2/2");
4801 			return;
4802 		}
4803 
4804 		if (get_sta_tx_parameters(sm,
4805 					  channel_width_to_int(ci.chanwidth),
4806 					  ci.seg1_idx, &tx_chanwidth,
4807 					  &tx_seg1_idx) < 0)
4808 			return;
4809 
4810 		if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
4811 					 tx_chanwidth, tx_seg1_idx) !=
4812 		    OCI_SUCCESS) {
4813 			wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm),
4814 					 LOGGER_INFO,
4815 					 "OCV failed: %s", ocv_errorstr);
4816 			if (wpa_auth->conf.msg_ctx)
4817 				wpa_msg(wpa_auth->conf.msg_ctx, MSG_INFO,
4818 					OCV_FAILURE "addr=" MACSTR
4819 					" frame=eapol-key-g2 error=%s",
4820 					MAC2STR(wpa_auth_get_spa(sm)),
4821 					ocv_errorstr);
4822 			return;
4823 		}
4824 	}
4825 #endif /* CONFIG_OCV */
4826 
4827 	if (sm->GUpdateStationKeys)
4828 		sm->group->GKeyDoneStations--;
4829 	sm->GUpdateStationKeys = false;
4830 	sm->GTimeoutCtr = 0;
4831 	/* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
4832 	wpa_auth_vlogger(wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
4833 			 "group key handshake completed (%s)",
4834 			 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
4835 	sm->has_GTK = true;
4836 }
4837 
4838 
SM_STATE(WPA_PTK_GROUP,KEYERROR)4839 SM_STATE(WPA_PTK_GROUP, KEYERROR)
4840 {
4841 	SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
4842 	if (sm->GUpdateStationKeys)
4843 		sm->group->GKeyDoneStations--;
4844 	sm->GUpdateStationKeys = false;
4845 	sm->Disconnect = true;
4846 	sm->disconnect_reason = WLAN_REASON_GROUP_KEY_UPDATE_TIMEOUT;
4847 	wpa_auth_vlogger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_INFO,
4848 			 "group key handshake failed (%s) after %u tries",
4849 			 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
4850 			 sm->wpa_auth->conf.wpa_group_update_count);
4851 }
4852 
4853 
SM_STEP(WPA_PTK_GROUP)4854 SM_STEP(WPA_PTK_GROUP)
4855 {
4856 	if (sm->Init || sm->PtkGroupInit) {
4857 		SM_ENTER(WPA_PTK_GROUP, IDLE);
4858 		sm->PtkGroupInit = false;
4859 	} else switch (sm->wpa_ptk_group_state) {
4860 	case WPA_PTK_GROUP_IDLE:
4861 		if (sm->GUpdateStationKeys ||
4862 		    (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
4863 			SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
4864 		break;
4865 	case WPA_PTK_GROUP_REKEYNEGOTIATING:
4866 		if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
4867 		    !sm->EAPOLKeyPairwise && sm->MICVerified)
4868 			SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
4869 		else if (sm->GTimeoutCtr >
4870 			 sm->wpa_auth->conf.wpa_group_update_count ||
4871 			 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
4872 			  sm->GTimeoutCtr > 1))
4873 			SM_ENTER(WPA_PTK_GROUP, KEYERROR);
4874 		else if (sm->TimeoutEvt)
4875 			SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
4876 		break;
4877 	case WPA_PTK_GROUP_KEYERROR:
4878 		SM_ENTER(WPA_PTK_GROUP, IDLE);
4879 		break;
4880 	case WPA_PTK_GROUP_REKEYESTABLISHED:
4881 		SM_ENTER(WPA_PTK_GROUP, IDLE);
4882 		break;
4883 	}
4884 }
4885 
4886 
wpa_gtk_update(struct wpa_authenticator * wpa_auth,struct wpa_group * group)4887 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
4888 			  struct wpa_group *group)
4889 {
4890 	struct wpa_auth_config *conf = &wpa_auth->conf;
4891 	int ret = 0;
4892 	size_t len;
4893 
4894 	os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
4895 	inc_byte_array(group->Counter, WPA_NONCE_LEN);
4896 	if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
4897 			   wpa_auth->addr, group->GNonce,
4898 			   group->GTK[group->GN - 1], group->GTK_len) < 0)
4899 		ret = -1;
4900 	wpa_hexdump_key(MSG_DEBUG, "GTK",
4901 			group->GTK[group->GN - 1], group->GTK_len);
4902 
4903 	if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
4904 		len = wpa_cipher_key_len(conf->group_mgmt_cipher);
4905 		os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
4906 		inc_byte_array(group->Counter, WPA_NONCE_LEN);
4907 		if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
4908 				   wpa_auth->addr, group->GNonce,
4909 				   group->IGTK[group->GN_igtk - 4], len) < 0)
4910 			ret = -1;
4911 		wpa_hexdump_key(MSG_DEBUG, "IGTK",
4912 				group->IGTK[group->GN_igtk - 4], len);
4913 	}
4914 
4915 	if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION &&
4916 	    conf->beacon_prot) {
4917 		len = wpa_cipher_key_len(conf->group_mgmt_cipher);
4918 		os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
4919 		inc_byte_array(group->Counter, WPA_NONCE_LEN);
4920 		if (wpa_gmk_to_gtk(group->GMK, "BIGTK key expansion",
4921 				   wpa_auth->addr, group->GNonce,
4922 				   group->BIGTK[group->GN_bigtk - 6], len) < 0)
4923 			ret = -1;
4924 		wpa_hexdump_key(MSG_DEBUG, "BIGTK",
4925 				group->BIGTK[group->GN_bigtk - 6], len);
4926 	}
4927 
4928 	return ret;
4929 }
4930 
4931 
wpa_group_gtk_init(struct wpa_authenticator * wpa_auth,struct wpa_group * group)4932 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
4933 			       struct wpa_group *group)
4934 {
4935 	wpa_printf(MSG_DEBUG,
4936 		   "WPA: group state machine entering state GTK_INIT (VLAN-ID %d)",
4937 		   group->vlan_id);
4938 	group->changed = false; /* GInit is not cleared here; avoid loop */
4939 	group->wpa_group_state = WPA_GROUP_GTK_INIT;
4940 
4941 	/* GTK[0..N] = 0 */
4942 	os_memset(group->GTK, 0, sizeof(group->GTK));
4943 	group->GN = 1;
4944 	group->GM = 2;
4945 	group->GN_igtk = 4;
4946 	group->GM_igtk = 5;
4947 	group->GN_bigtk = 6;
4948 	group->GM_bigtk = 7;
4949 	/* GTK[GN] = CalcGTK() */
4950 	wpa_gtk_update(wpa_auth, group);
4951 }
4952 
4953 
wpa_group_update_sta(struct wpa_state_machine * sm,void * ctx)4954 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
4955 {
4956 	if (ctx != NULL && ctx != sm->group)
4957 		return 0;
4958 
4959 	if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
4960 		wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm),
4961 				LOGGER_DEBUG,
4962 				"Not in PTKINITDONE; skip Group Key update");
4963 		sm->GUpdateStationKeys = false;
4964 		return 0;
4965 	}
4966 	if (sm->GUpdateStationKeys) {
4967 		/*
4968 		 * This should not really happen, so add a debug log entry.
4969 		 * Since we clear the GKeyDoneStations before the loop, the
4970 		 * station needs to be counted here anyway.
4971 		 */
4972 		wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm),
4973 				LOGGER_DEBUG,
4974 				"GUpdateStationKeys was already set when marking station for GTK rekeying");
4975 	}
4976 
4977 	/* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
4978 	if (sm->is_wnmsleep)
4979 		return 0;
4980 
4981 	sm->group->GKeyDoneStations++;
4982 	sm->GUpdateStationKeys = true;
4983 
4984 	wpa_sm_step(sm);
4985 	return 0;
4986 }
4987 
4988 
4989 #ifdef CONFIG_WNM_AP
4990 /* update GTK when exiting WNM-Sleep Mode */
wpa_wnmsleep_rekey_gtk(struct wpa_state_machine * sm)4991 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
4992 {
4993 	if (!sm || sm->is_wnmsleep)
4994 		return;
4995 
4996 	wpa_group_update_sta(sm, NULL);
4997 }
4998 
4999 
wpa_set_wnmsleep(struct wpa_state_machine * sm,int flag)5000 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
5001 {
5002 	if (sm)
5003 		sm->is_wnmsleep = !!flag;
5004 }
5005 
5006 
wpa_wnmsleep_gtk_subelem(struct wpa_state_machine * sm,u8 * pos)5007 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
5008 {
5009 	struct wpa_auth_config *conf = &sm->wpa_auth->conf;
5010 	struct wpa_group *gsm = sm->group;
5011 	u8 *start = pos;
5012 
5013 	/*
5014 	 * GTK subelement:
5015 	 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
5016 	 * Key[5..32]
5017 	 */
5018 	*pos++ = WNM_SLEEP_SUBELEM_GTK;
5019 	*pos++ = 11 + gsm->GTK_len;
5020 	/* Key ID in B0-B1 of Key Info */
5021 	WPA_PUT_LE16(pos, gsm->GN & 0x03);
5022 	pos += 2;
5023 	*pos++ = gsm->GTK_len;
5024 	if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
5025 		return 0;
5026 	pos += 8;
5027 	os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
5028 	if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
5029 		/*
5030 		 * Provide unique random GTK to each STA to prevent use
5031 		 * of GTK in the BSS.
5032 		 */
5033 		if (random_get_bytes(pos, gsm->GTK_len) < 0)
5034 			return 0;
5035 	}
5036 	pos += gsm->GTK_len;
5037 
5038 	wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
5039 		   gsm->GN);
5040 	wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
5041 			gsm->GTK[gsm->GN - 1], gsm->GTK_len);
5042 
5043 	return pos - start;
5044 }
5045 
5046 
wpa_wnmsleep_igtk_subelem(struct wpa_state_machine * sm,u8 * pos)5047 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
5048 {
5049 	struct wpa_auth_config *conf = &sm->wpa_auth->conf;
5050 	struct wpa_group *gsm = sm->group;
5051 	u8 *start = pos;
5052 	size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
5053 
5054 	/*
5055 	 * IGTK subelement:
5056 	 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
5057 	 */
5058 	*pos++ = WNM_SLEEP_SUBELEM_IGTK;
5059 	*pos++ = 2 + 6 + len;
5060 	WPA_PUT_LE16(pos, gsm->GN_igtk);
5061 	pos += 2;
5062 	if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
5063 		return 0;
5064 	pos += 6;
5065 
5066 	os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
5067 	if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
5068 		/*
5069 		 * Provide unique random IGTK to each STA to prevent use
5070 		 * of IGTK in the BSS.
5071 		 */
5072 		if (random_get_bytes(pos, len) < 0)
5073 			return 0;
5074 	}
5075 	pos += len;
5076 
5077 	wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
5078 		   gsm->GN_igtk);
5079 	wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
5080 			gsm->IGTK[gsm->GN_igtk - 4], len);
5081 
5082 	return pos - start;
5083 }
5084 
5085 
wpa_wnmsleep_bigtk_subelem(struct wpa_state_machine * sm,u8 * pos)5086 int wpa_wnmsleep_bigtk_subelem(struct wpa_state_machine *sm, u8 *pos)
5087 {
5088 	struct wpa_group *gsm = sm->group;
5089 	u8 *start = pos;
5090 	size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
5091 
5092 	/*
5093 	 * BIGTK subelement:
5094 	 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
5095 	 */
5096 	*pos++ = WNM_SLEEP_SUBELEM_BIGTK;
5097 	*pos++ = 2 + 6 + len;
5098 	WPA_PUT_LE16(pos, gsm->GN_bigtk);
5099 	pos += 2;
5100 	if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_bigtk, pos) != 0)
5101 		return 0;
5102 	pos += 6;
5103 
5104 	os_memcpy(pos, gsm->BIGTK[gsm->GN_bigtk - 6], len);
5105 	if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
5106 		/*
5107 		 * Provide unique random BIGTK to each STA to prevent use
5108 		 * of BIGTK in the BSS.
5109 		 */
5110 		if (random_get_bytes(pos, len) < 0)
5111 			return 0;
5112 	}
5113 	pos += len;
5114 
5115 	wpa_printf(MSG_DEBUG, "WNM: BIGTK Key ID %u in WNM-Sleep Mode exit",
5116 		   gsm->GN_bigtk);
5117 	wpa_hexdump_key(MSG_DEBUG, "WNM: BIGTK in WNM-Sleep Mode exit",
5118 			gsm->BIGTK[gsm->GN_bigtk - 6], len);
5119 
5120 	return pos - start;
5121 }
5122 
5123 #endif /* CONFIG_WNM_AP */
5124 
5125 
wpa_group_setkeys(struct wpa_authenticator * wpa_auth,struct wpa_group * group)5126 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
5127 			      struct wpa_group *group)
5128 {
5129 	int tmp;
5130 
5131 	wpa_printf(MSG_DEBUG,
5132 		   "WPA: group state machine entering state SETKEYS (VLAN-ID %d)",
5133 		   group->vlan_id);
5134 	group->changed = true;
5135 	group->wpa_group_state = WPA_GROUP_SETKEYS;
5136 	group->GTKReKey = false;
5137 	tmp = group->GM;
5138 	group->GM = group->GN;
5139 	group->GN = tmp;
5140 	tmp = group->GM_igtk;
5141 	group->GM_igtk = group->GN_igtk;
5142 	group->GN_igtk = tmp;
5143 	tmp = group->GM_bigtk;
5144 	group->GM_bigtk = group->GN_bigtk;
5145 	group->GN_bigtk = tmp;
5146 	/* "GKeyDoneStations = GNoStations" is done in more robust way by
5147 	 * counting the STAs that are marked with GUpdateStationKeys instead of
5148 	 * including all STAs that could be in not-yet-completed state. */
5149 	wpa_gtk_update(wpa_auth, group);
5150 
5151 	if (group->GKeyDoneStations) {
5152 		wpa_printf(MSG_DEBUG,
5153 			   "wpa_group_setkeys: Unexpected GKeyDoneStations=%d when starting new GTK rekey",
5154 			   group->GKeyDoneStations);
5155 		group->GKeyDoneStations = 0;
5156 	}
5157 	wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
5158 	wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
5159 		   group->GKeyDoneStations);
5160 }
5161 
5162 
wpa_group_config_group_keys(struct wpa_authenticator * wpa_auth,struct wpa_group * group)5163 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
5164 				       struct wpa_group *group)
5165 {
5166 	struct wpa_auth_config *conf = &wpa_auth->conf;
5167 	int ret = 0;
5168 
5169 	if (wpa_auth_set_key(wpa_auth, group->vlan_id,
5170 			     wpa_cipher_to_alg(conf->wpa_group),
5171 			     broadcast_ether_addr, group->GN,
5172 			     group->GTK[group->GN - 1], group->GTK_len,
5173 			     KEY_FLAG_GROUP_TX_DEFAULT) < 0)
5174 		ret = -1;
5175 
5176 	if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
5177 		enum wpa_alg alg;
5178 		size_t len;
5179 
5180 		alg = wpa_cipher_to_alg(conf->group_mgmt_cipher);
5181 		len = wpa_cipher_key_len(conf->group_mgmt_cipher);
5182 
5183 		if (ret == 0 &&
5184 		    wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
5185 				     broadcast_ether_addr, group->GN_igtk,
5186 				     group->IGTK[group->GN_igtk - 4], len,
5187 				     KEY_FLAG_GROUP_TX_DEFAULT) < 0)
5188 			ret = -1;
5189 
5190 		if (ret == 0 && conf->beacon_prot &&
5191 		    wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
5192 				     broadcast_ether_addr, group->GN_bigtk,
5193 				     group->BIGTK[group->GN_bigtk - 6], len,
5194 				     KEY_FLAG_GROUP_TX_DEFAULT) < 0)
5195 			ret = -1;
5196 	}
5197 
5198 	return ret;
5199 }
5200 
5201 
wpa_group_disconnect_cb(struct wpa_state_machine * sm,void * ctx)5202 static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
5203 {
5204 	if (sm->group == ctx) {
5205 		wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
5206 			   " for disconnection due to fatal failure",
5207 			   MAC2STR(wpa_auth_get_spa(sm)));
5208 		sm->Disconnect = true;
5209 	}
5210 
5211 	return 0;
5212 }
5213 
5214 
wpa_group_fatal_failure(struct wpa_authenticator * wpa_auth,struct wpa_group * group)5215 static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
5216 				    struct wpa_group *group)
5217 {
5218 	wpa_printf(MSG_DEBUG,
5219 		   "WPA: group state machine entering state FATAL_FAILURE");
5220 	group->changed = true;
5221 	group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
5222 	wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
5223 }
5224 
5225 
wpa_group_setkeysdone(struct wpa_authenticator * wpa_auth,struct wpa_group * group)5226 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
5227 				 struct wpa_group *group)
5228 {
5229 	wpa_printf(MSG_DEBUG,
5230 		   "WPA: group state machine entering state SETKEYSDONE (VLAN-ID %d)",
5231 		   group->vlan_id);
5232 	group->changed = true;
5233 	group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
5234 
5235 	if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
5236 		wpa_group_fatal_failure(wpa_auth, group);
5237 		return -1;
5238 	}
5239 
5240 	return 0;
5241 }
5242 
5243 
wpa_group_sm_step(struct wpa_authenticator * wpa_auth,struct wpa_group * group)5244 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
5245 			      struct wpa_group *group)
5246 {
5247 	if (group->GInit) {
5248 		wpa_group_gtk_init(wpa_auth, group);
5249 	} else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
5250 		/* Do not allow group operations */
5251 	} else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
5252 		   group->GTKAuthenticator) {
5253 		wpa_group_setkeysdone(wpa_auth, group);
5254 	} else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
5255 		   group->GTKReKey) {
5256 		wpa_group_setkeys(wpa_auth, group);
5257 	} else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
5258 		if (group->GKeyDoneStations == 0)
5259 			wpa_group_setkeysdone(wpa_auth, group);
5260 		else if (group->GTKReKey)
5261 			wpa_group_setkeys(wpa_auth, group);
5262 	}
5263 }
5264 
5265 
wpa_sm_step(struct wpa_state_machine * sm)5266 static int wpa_sm_step(struct wpa_state_machine *sm)
5267 {
5268 	if (!sm)
5269 		return 0;
5270 
5271 	if (sm->in_step_loop) {
5272 		/* This should not happen, but if it does, make sure we do not
5273 		 * end up freeing the state machine too early by exiting the
5274 		 * recursive call. */
5275 		wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
5276 		return 0;
5277 	}
5278 
5279 	sm->in_step_loop = 1;
5280 	do {
5281 		if (sm->pending_deinit)
5282 			break;
5283 
5284 		sm->changed = false;
5285 		sm->wpa_auth->group->changed = false;
5286 
5287 		SM_STEP_RUN(WPA_PTK);
5288 		if (sm->pending_deinit)
5289 			break;
5290 		SM_STEP_RUN(WPA_PTK_GROUP);
5291 		if (sm->pending_deinit)
5292 			break;
5293 		wpa_group_sm_step(sm->wpa_auth, sm->group);
5294 	} while (sm->changed || sm->wpa_auth->group->changed);
5295 	sm->in_step_loop = 0;
5296 
5297 	if (sm->pending_deinit) {
5298 		wpa_printf(MSG_DEBUG,
5299 			   "WPA: Completing pending STA state machine deinit for "
5300 			   MACSTR, MAC2STR(wpa_auth_get_spa(sm)));
5301 		wpa_free_sta_sm(sm);
5302 		return 1;
5303 	}
5304 	return 0;
5305 }
5306 
5307 
wpa_sm_call_step(void * eloop_ctx,void * timeout_ctx)5308 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
5309 {
5310 	struct wpa_state_machine *sm = eloop_ctx;
5311 	wpa_sm_step(sm);
5312 }
5313 
5314 
wpa_auth_sm_notify(struct wpa_state_machine * sm)5315 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
5316 {
5317 	if (!sm)
5318 		return;
5319 	eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
5320 }
5321 
5322 
wpa_gtk_rekey(struct wpa_authenticator * wpa_auth)5323 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
5324 {
5325 	int tmp, i;
5326 	struct wpa_group *group;
5327 
5328 	if (!wpa_auth)
5329 		return;
5330 
5331 	group = wpa_auth->group;
5332 
5333 	for (i = 0; i < 2; i++) {
5334 		tmp = group->GM;
5335 		group->GM = group->GN;
5336 		group->GN = tmp;
5337 		tmp = group->GM_igtk;
5338 		group->GM_igtk = group->GN_igtk;
5339 		group->GN_igtk = tmp;
5340 		tmp = group->GM_bigtk;
5341 		group->GM_bigtk = group->GN_bigtk;
5342 		group->GN_bigtk = tmp;
5343 		wpa_gtk_update(wpa_auth, group);
5344 		wpa_group_config_group_keys(wpa_auth, group);
5345 	}
5346 }
5347 
5348 
wpa_bool_txt(int val)5349 static const char * wpa_bool_txt(int val)
5350 {
5351 	return val ? "TRUE" : "FALSE";
5352 }
5353 
5354 
5355 #define RSN_SUITE "%02x-%02x-%02x-%d"
5356 #define RSN_SUITE_ARG(s) \
5357 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
5358 
wpa_get_mib(struct wpa_authenticator * wpa_auth,char * buf,size_t buflen)5359 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
5360 {
5361 	struct wpa_auth_config *conf;
5362 	int len = 0, ret;
5363 	char pmkid_txt[PMKID_LEN * 2 + 1];
5364 #ifdef CONFIG_RSN_PREAUTH
5365 	const int preauth = 1;
5366 #else /* CONFIG_RSN_PREAUTH */
5367 	const int preauth = 0;
5368 #endif /* CONFIG_RSN_PREAUTH */
5369 
5370 	if (!wpa_auth)
5371 		return len;
5372 	conf = &wpa_auth->conf;
5373 
5374 	ret = os_snprintf(buf + len, buflen - len,
5375 			  "dot11RSNAOptionImplemented=TRUE\n"
5376 			  "dot11RSNAPreauthenticationImplemented=%s\n"
5377 			  "dot11RSNAEnabled=%s\n"
5378 			  "dot11RSNAPreauthenticationEnabled=%s\n",
5379 			  wpa_bool_txt(preauth),
5380 			  wpa_bool_txt(conf->wpa & WPA_PROTO_RSN),
5381 			  wpa_bool_txt(conf->rsn_preauth));
5382 	if (os_snprintf_error(buflen - len, ret))
5383 		return len;
5384 	len += ret;
5385 
5386 	wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
5387 			 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
5388 
5389 	ret = os_snprintf(
5390 		buf + len, buflen - len,
5391 		"dot11RSNAConfigVersion=%u\n"
5392 		"dot11RSNAConfigPairwiseKeysSupported=9999\n"
5393 		/* FIX: dot11RSNAConfigGroupCipher */
5394 		/* FIX: dot11RSNAConfigGroupRekeyMethod */
5395 		/* FIX: dot11RSNAConfigGroupRekeyTime */
5396 		/* FIX: dot11RSNAConfigGroupRekeyPackets */
5397 		"dot11RSNAConfigGroupRekeyStrict=%u\n"
5398 		"dot11RSNAConfigGroupUpdateCount=%u\n"
5399 		"dot11RSNAConfigPairwiseUpdateCount=%u\n"
5400 		"dot11RSNAConfigGroupCipherSize=%u\n"
5401 		"dot11RSNAConfigPMKLifetime=%u\n"
5402 		"dot11RSNAConfigPMKReauthThreshold=%u\n"
5403 		"dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
5404 		"dot11RSNAConfigSATimeout=%u\n"
5405 		"dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
5406 		"dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
5407 		"dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
5408 		"dot11RSNAPMKIDUsed=%s\n"
5409 		"dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
5410 		"dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
5411 		"dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
5412 		"dot11RSNATKIPCounterMeasuresInvoked=%u\n"
5413 		"dot11RSNA4WayHandshakeFailures=%u\n"
5414 		"dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
5415 		RSN_VERSION,
5416 		!!conf->wpa_strict_rekey,
5417 		conf->wpa_group_update_count,
5418 		conf->wpa_pairwise_update_count,
5419 		wpa_cipher_key_len(conf->wpa_group) * 8,
5420 		dot11RSNAConfigPMKLifetime,
5421 		dot11RSNAConfigPMKReauthThreshold,
5422 		dot11RSNAConfigSATimeout,
5423 		RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
5424 		RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
5425 		RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
5426 		pmkid_txt,
5427 		RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
5428 		RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
5429 		RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
5430 		wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
5431 		wpa_auth->dot11RSNA4WayHandshakeFailures);
5432 	if (os_snprintf_error(buflen - len, ret))
5433 		return len;
5434 	len += ret;
5435 
5436 	/* TODO: dot11RSNAConfigPairwiseCiphersTable */
5437 	/* TODO: dot11RSNAConfigAuthenticationSuitesTable */
5438 
5439 	/* Private MIB */
5440 	ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
5441 			  wpa_auth->group->wpa_group_state);
5442 	if (os_snprintf_error(buflen - len, ret))
5443 		return len;
5444 	len += ret;
5445 
5446 	return len;
5447 }
5448 
5449 
wpa_get_mib_sta(struct wpa_state_machine * sm,char * buf,size_t buflen)5450 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
5451 {
5452 	int len = 0, ret;
5453 	u32 pairwise = 0;
5454 
5455 	if (!sm)
5456 		return 0;
5457 
5458 	/* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
5459 
5460 	/* dot11RSNAStatsEntry */
5461 
5462 	pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
5463 				       WPA_PROTO_RSN : WPA_PROTO_WPA,
5464 				       sm->pairwise);
5465 	if (pairwise == 0)
5466 		return 0;
5467 
5468 	ret = os_snprintf(
5469 		buf + len, buflen - len,
5470 		/* TODO: dot11RSNAStatsIndex */
5471 		"dot11RSNAStatsSTAAddress=" MACSTR "\n"
5472 		"dot11RSNAStatsVersion=1\n"
5473 		"dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
5474 		/* TODO: dot11RSNAStatsTKIPICVErrors */
5475 		"dot11RSNAStatsTKIPLocalMICFailures=%u\n"
5476 		"dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
5477 		/* TODO: dot11RSNAStatsCCMPReplays */
5478 		/* TODO: dot11RSNAStatsCCMPDecryptErrors */
5479 		/* TODO: dot11RSNAStatsTKIPReplays */,
5480 		MAC2STR(sm->addr),
5481 		RSN_SUITE_ARG(pairwise),
5482 		sm->dot11RSNAStatsTKIPLocalMICFailures,
5483 		sm->dot11RSNAStatsTKIPRemoteMICFailures);
5484 	if (os_snprintf_error(buflen - len, ret))
5485 		return len;
5486 	len += ret;
5487 
5488 	/* Private MIB */
5489 	ret = os_snprintf(buf + len, buflen - len,
5490 			  "wpa=%d\n"
5491 			  "AKMSuiteSelector=" RSN_SUITE "\n"
5492 			  "hostapdWPAPTKState=%d\n"
5493 			  "hostapdWPAPTKGroupState=%d\n"
5494 			  "hostapdMFPR=%d\n",
5495 			  sm->wpa,
5496 			  RSN_SUITE_ARG(wpa_akm_to_suite(sm->wpa_key_mgmt)),
5497 			  sm->wpa_ptk_state,
5498 			  sm->wpa_ptk_group_state,
5499 			  sm->mfpr);
5500 	if (os_snprintf_error(buflen - len, ret))
5501 		return len;
5502 	len += ret;
5503 
5504 	return len;
5505 }
5506 
5507 
wpa_auth_countermeasures_start(struct wpa_authenticator * wpa_auth)5508 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
5509 {
5510 	if (wpa_auth)
5511 		wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
5512 }
5513 
5514 
wpa_auth_pairwise_set(struct wpa_state_machine * sm)5515 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
5516 {
5517 	return sm && sm->pairwise_set;
5518 }
5519 
5520 
wpa_auth_get_pairwise(struct wpa_state_machine * sm)5521 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
5522 {
5523 	return sm->pairwise;
5524 }
5525 
5526 
wpa_auth_get_pmk(struct wpa_state_machine * sm,int * len)5527 const u8 * wpa_auth_get_pmk(struct wpa_state_machine *sm, int *len)
5528 {
5529 	if (!sm)
5530 		return NULL;
5531 	*len = sm->pmk_len;
5532 	return sm->PMK;
5533 }
5534 
5535 
wpa_auth_get_dpp_pkhash(struct wpa_state_machine * sm)5536 const u8 * wpa_auth_get_dpp_pkhash(struct wpa_state_machine *sm)
5537 {
5538 	if (!sm || !sm->pmksa)
5539 		return NULL;
5540 	return sm->pmksa->dpp_pkhash;
5541 }
5542 
5543 
wpa_auth_sta_key_mgmt(struct wpa_state_machine * sm)5544 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
5545 {
5546 	if (!sm)
5547 		return -1;
5548 	return sm->wpa_key_mgmt;
5549 }
5550 
5551 
wpa_auth_sta_wpa_version(struct wpa_state_machine * sm)5552 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
5553 {
5554 	if (!sm)
5555 		return 0;
5556 	return sm->wpa;
5557 }
5558 
5559 
wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine * sm)5560 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
5561 {
5562 	if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
5563 		return 0;
5564 	return sm->tk_already_set;
5565 }
5566 
5567 
wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine * sm)5568 int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm)
5569 {
5570 	if (!sm || !wpa_key_mgmt_fils(sm->wpa_key_mgmt))
5571 		return 0;
5572 	return sm->tk_already_set;
5573 }
5574 
5575 
wpa_auth_sta_clear_pmksa(struct wpa_state_machine * sm,struct rsn_pmksa_cache_entry * entry)5576 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
5577 			     struct rsn_pmksa_cache_entry *entry)
5578 {
5579 	if (!sm || sm->pmksa != entry)
5580 		return -1;
5581 	sm->pmksa = NULL;
5582 	return 0;
5583 }
5584 
5585 
5586 struct rsn_pmksa_cache_entry *
wpa_auth_sta_get_pmksa(struct wpa_state_machine * sm)5587 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
5588 {
5589 	return sm ? sm->pmksa : NULL;
5590 }
5591 
5592 
wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine * sm)5593 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
5594 {
5595 	if (sm)
5596 		sm->dot11RSNAStatsTKIPLocalMICFailures++;
5597 }
5598 
5599 
wpa_auth_get_wpa_ie(struct wpa_authenticator * wpa_auth,size_t * len)5600 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
5601 {
5602 	if (!wpa_auth)
5603 		return NULL;
5604 	*len = wpa_auth->wpa_ie_len;
5605 	return wpa_auth->wpa_ie;
5606 }
5607 
5608 
wpa_auth_pmksa_add(struct wpa_state_machine * sm,const u8 * pmk,unsigned int pmk_len,int session_timeout,struct eapol_state_machine * eapol)5609 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
5610 		       unsigned int pmk_len,
5611 		       int session_timeout, struct eapol_state_machine *eapol)
5612 {
5613 	if (!sm || sm->wpa != WPA_VERSION_WPA2 ||
5614 	    sm->wpa_auth->conf.disable_pmksa_caching)
5615 		return -1;
5616 
5617 #ifdef CONFIG_IEEE80211R_AP
5618 	if (pmk_len >= 2 * PMK_LEN && wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
5619 	    wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
5620 	    !wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
5621 		/* Cache MPMK/XXKey instead of initial part from MSK */
5622 		pmk = pmk + PMK_LEN;
5623 		pmk_len = PMK_LEN;
5624 	} else
5625 #endif /* CONFIG_IEEE80211R_AP */
5626 	if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
5627 		if (pmk_len > PMK_LEN_SUITE_B_192)
5628 			pmk_len = PMK_LEN_SUITE_B_192;
5629 	} else if (pmk_len > PMK_LEN) {
5630 		pmk_len = PMK_LEN;
5631 	}
5632 
5633 	wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK", pmk, pmk_len);
5634 	if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
5635 				 sm->PTK.kck, sm->PTK.kck_len,
5636 				 wpa_auth_get_aa(sm),
5637 				 wpa_auth_get_spa(sm), session_timeout,
5638 				 eapol, sm->wpa_key_mgmt))
5639 		return 0;
5640 
5641 	return -1;
5642 }
5643 
5644 
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)5645 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
5646 			       const u8 *pmk, size_t len, const u8 *sta_addr,
5647 			       int session_timeout,
5648 			       struct eapol_state_machine *eapol)
5649 {
5650 	if (!wpa_auth)
5651 		return -1;
5652 
5653 	wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from preauth", pmk, len);
5654 	if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
5655 				 NULL, 0,
5656 				 wpa_auth->addr,
5657 				 sta_addr, session_timeout, eapol,
5658 				 WPA_KEY_MGMT_IEEE8021X))
5659 		return 0;
5660 
5661 	return -1;
5662 }
5663 
5664 
wpa_auth_pmksa_add_sae(struct wpa_authenticator * wpa_auth,const u8 * addr,const u8 * pmk,size_t pmk_len,const u8 * pmkid,int akmp)5665 int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
5666 			   const u8 *pmk, size_t pmk_len, const u8 *pmkid,
5667 			   int akmp)
5668 {
5669 	if (wpa_auth->conf.disable_pmksa_caching)
5670 		return -1;
5671 
5672 	wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from SAE", pmk, pmk_len);
5673 	if (!akmp)
5674 		akmp = WPA_KEY_MGMT_SAE;
5675 	if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
5676 				 NULL, 0, wpa_auth->addr, addr, 0, NULL, akmp))
5677 		return 0;
5678 
5679 	return -1;
5680 }
5681 
5682 
wpa_auth_add_sae_pmkid(struct wpa_state_machine * sm,const u8 * pmkid)5683 void wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid)
5684 {
5685 	os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
5686 	sm->pmkid_set = 1;
5687 }
5688 
5689 
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)5690 int wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr,
5691 			const u8 *pmk, size_t pmk_len, const u8 *pmkid,
5692 			int session_timeout, int akmp)
5693 {
5694 	if (!wpa_auth || wpa_auth->conf.disable_pmksa_caching)
5695 		return -1;
5696 
5697 	wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK (2)", pmk, PMK_LEN);
5698 	if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
5699 				 NULL, 0, wpa_auth->addr, addr, session_timeout,
5700 				 NULL, akmp))
5701 		return 0;
5702 
5703 	return -1;
5704 }
5705 
5706 
wpa_auth_pmksa_add3(struct wpa_authenticator * wpa_auth,const u8 * addr,const u8 * pmk,size_t pmk_len,const u8 * pmkid,int session_timeout,int akmp,const u8 * dpp_pkhash)5707 int wpa_auth_pmksa_add3(struct wpa_authenticator *wpa_auth, const u8 *addr,
5708 			const u8 *pmk, size_t pmk_len, const u8 *pmkid,
5709 			int session_timeout, int akmp, const u8 *dpp_pkhash)
5710 {
5711 	struct rsn_pmksa_cache_entry *entry;
5712 
5713 	if (wpa_auth->conf.disable_pmksa_caching)
5714 		return -1;
5715 
5716 	wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK (3)", pmk, PMK_LEN);
5717 	entry = pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
5718 				 NULL, 0, wpa_auth->addr, addr, session_timeout,
5719 				 NULL, akmp);
5720 	if (!entry)
5721 		return -1;
5722 
5723 	if (dpp_pkhash)
5724 		entry->dpp_pkhash = os_memdup(dpp_pkhash, SHA256_MAC_LEN);
5725 
5726 	return 0;
5727 }
5728 
5729 
wpa_auth_pmksa_remove(struct wpa_authenticator * wpa_auth,const u8 * sta_addr)5730 void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
5731 			   const u8 *sta_addr)
5732 {
5733 	struct rsn_pmksa_cache_entry *pmksa;
5734 
5735 	if (!wpa_auth || !wpa_auth->pmksa)
5736 		return;
5737 	pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
5738 	if (pmksa) {
5739 		wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
5740 			   MACSTR " based on request", MAC2STR(sta_addr));
5741 		pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
5742 	}
5743 }
5744 
5745 
wpa_auth_pmksa_list(struct wpa_authenticator * wpa_auth,char * buf,size_t len)5746 int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
5747 			size_t len)
5748 {
5749 	if (!wpa_auth || !wpa_auth->pmksa)
5750 		return 0;
5751 	return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
5752 }
5753 
5754 
wpa_auth_pmksa_flush(struct wpa_authenticator * wpa_auth)5755 void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
5756 {
5757 	if (wpa_auth && wpa_auth->pmksa)
5758 		pmksa_cache_auth_flush(wpa_auth->pmksa);
5759 }
5760 
5761 
5762 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
5763 #ifdef CONFIG_MESH
5764 
wpa_auth_pmksa_list_mesh(struct wpa_authenticator * wpa_auth,const u8 * addr,char * buf,size_t len)5765 int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
5766 			     char *buf, size_t len)
5767 {
5768 	if (!wpa_auth || !wpa_auth->pmksa)
5769 		return 0;
5770 
5771 	return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
5772 }
5773 
5774 
5775 struct rsn_pmksa_cache_entry *
wpa_auth_pmksa_create_entry(const u8 * aa,const u8 * spa,const u8 * pmk,size_t pmk_len,int akmp,const u8 * pmkid,int expiration)5776 wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
5777 			    size_t pmk_len, int akmp,
5778 			    const u8 *pmkid, int expiration)
5779 {
5780 	struct rsn_pmksa_cache_entry *entry;
5781 	struct os_reltime now;
5782 
5783 	entry = pmksa_cache_auth_create_entry(pmk, pmk_len, pmkid, NULL, 0, aa,
5784 					      spa, 0, NULL, akmp);
5785 	if (!entry)
5786 		return NULL;
5787 
5788 	os_get_reltime(&now);
5789 	entry->expiration = now.sec + expiration;
5790 	return entry;
5791 }
5792 
5793 
wpa_auth_pmksa_add_entry(struct wpa_authenticator * wpa_auth,struct rsn_pmksa_cache_entry * entry)5794 int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
5795 			     struct rsn_pmksa_cache_entry *entry)
5796 {
5797 	int ret;
5798 
5799 	if (!wpa_auth || !wpa_auth->pmksa)
5800 		return -1;
5801 
5802 	ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
5803 	if (ret < 0)
5804 		wpa_printf(MSG_DEBUG,
5805 			   "RSN: Failed to store external PMKSA cache for "
5806 			   MACSTR, MAC2STR(entry->spa));
5807 
5808 	return ret;
5809 }
5810 
5811 #endif /* CONFIG_MESH */
5812 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
5813 
5814 
5815 struct rsn_pmksa_cache *
wpa_auth_get_pmksa_cache(struct wpa_authenticator * wpa_auth)5816 wpa_auth_get_pmksa_cache(struct wpa_authenticator *wpa_auth)
5817 {
5818 	if (!wpa_auth || !wpa_auth->pmksa)
5819 		return NULL;
5820 	return wpa_auth->pmksa;
5821 }
5822 
5823 
5824 struct rsn_pmksa_cache_entry *
wpa_auth_pmksa_get(struct wpa_authenticator * wpa_auth,const u8 * sta_addr,const u8 * pmkid)5825 wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
5826 		   const u8 *pmkid)
5827 {
5828 	if (!wpa_auth || !wpa_auth->pmksa)
5829 		return NULL;
5830 	return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
5831 }
5832 
5833 
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)5834 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
5835 			      struct wpa_state_machine *sm,
5836 			      struct wpa_authenticator *wpa_auth,
5837 			      u8 *pmkid, u8 *pmk)
5838 {
5839 	if (!sm)
5840 		return;
5841 
5842 	sm->pmksa = pmksa;
5843 	os_memcpy(pmk, pmksa->pmk, PMK_LEN);
5844 	os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
5845 	os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
5846 }
5847 
5848 
5849 /*
5850  * Remove and free the group from wpa_authenticator. This is triggered by a
5851  * callback to make sure nobody is currently iterating the group list while it
5852  * gets modified.
5853  */
wpa_group_free(struct wpa_authenticator * wpa_auth,struct wpa_group * group)5854 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
5855 			   struct wpa_group *group)
5856 {
5857 	struct wpa_group *prev = wpa_auth->group;
5858 
5859 	wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
5860 		   group->vlan_id);
5861 
5862 	while (prev) {
5863 		if (prev->next == group) {
5864 			/* This never frees the special first group as needed */
5865 			prev->next = group->next;
5866 			os_free(group);
5867 			break;
5868 		}
5869 		prev = prev->next;
5870 	}
5871 
5872 }
5873 
5874 
5875 /* Increase the reference counter for group */
wpa_group_get(struct wpa_authenticator * wpa_auth,struct wpa_group * group)5876 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
5877 			  struct wpa_group *group)
5878 {
5879 	/* Skip the special first group */
5880 	if (wpa_auth->group == group)
5881 		return;
5882 
5883 	group->references++;
5884 }
5885 
5886 
5887 /* Decrease the reference counter and maybe free the group */
wpa_group_put(struct wpa_authenticator * wpa_auth,struct wpa_group * group)5888 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
5889 			  struct wpa_group *group)
5890 {
5891 	/* Skip the special first group */
5892 	if (wpa_auth->group == group)
5893 		return;
5894 
5895 	group->references--;
5896 	if (group->references)
5897 		return;
5898 	wpa_group_free(wpa_auth, group);
5899 }
5900 
5901 
5902 /*
5903  * Add a group that has its references counter set to zero. Caller needs to
5904  * call wpa_group_get() on the return value to mark the entry in use.
5905  */
5906 static struct wpa_group *
wpa_auth_add_group(struct wpa_authenticator * wpa_auth,int vlan_id)5907 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
5908 {
5909 	struct wpa_group *group;
5910 
5911 	if (!wpa_auth || !wpa_auth->group)
5912 		return NULL;
5913 
5914 	wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
5915 		   vlan_id);
5916 	group = wpa_group_init(wpa_auth, vlan_id, 0);
5917 	if (!group)
5918 		return NULL;
5919 
5920 	group->next = wpa_auth->group->next;
5921 	wpa_auth->group->next = group;
5922 
5923 	return group;
5924 }
5925 
5926 
5927 /*
5928  * Enforce that the group state machine for the VLAN is running, increase
5929  * reference counter as interface is up. References might have been increased
5930  * even if a negative value is returned.
5931  * Returns: -1 on error (group missing, group already failed); otherwise, 0
5932  */
wpa_auth_ensure_group(struct wpa_authenticator * wpa_auth,int vlan_id)5933 int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
5934 {
5935 	struct wpa_group *group;
5936 
5937 	if (!wpa_auth)
5938 		return 0;
5939 
5940 	group = wpa_auth->group;
5941 	while (group) {
5942 		if (group->vlan_id == vlan_id)
5943 			break;
5944 		group = group->next;
5945 	}
5946 
5947 	if (!group) {
5948 		group = wpa_auth_add_group(wpa_auth, vlan_id);
5949 		if (!group)
5950 			return -1;
5951 	}
5952 
5953 	wpa_printf(MSG_DEBUG,
5954 		   "WPA: Ensure group state machine running for VLAN ID %d",
5955 		   vlan_id);
5956 
5957 	wpa_group_get(wpa_auth, group);
5958 	group->num_setup_iface++;
5959 
5960 	if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
5961 		return -1;
5962 
5963 	return 0;
5964 }
5965 
5966 
5967 /*
5968  * Decrease reference counter, expected to be zero afterwards.
5969  * returns: -1 on error (group not found, group in fail state)
5970  *          -2 if wpa_group is still referenced
5971  *           0 else
5972  */
wpa_auth_release_group(struct wpa_authenticator * wpa_auth,int vlan_id)5973 int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
5974 {
5975 	struct wpa_group *group;
5976 	int ret = 0;
5977 
5978 	if (!wpa_auth)
5979 		return 0;
5980 
5981 	group = wpa_auth->group;
5982 	while (group) {
5983 		if (group->vlan_id == vlan_id)
5984 			break;
5985 		group = group->next;
5986 	}
5987 
5988 	if (!group)
5989 		return -1;
5990 
5991 	wpa_printf(MSG_DEBUG,
5992 		   "WPA: Try stopping group state machine for VLAN ID %d",
5993 		   vlan_id);
5994 
5995 	if (group->num_setup_iface <= 0) {
5996 		wpa_printf(MSG_ERROR,
5997 			   "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
5998 			   vlan_id);
5999 		return -1;
6000 	}
6001 	group->num_setup_iface--;
6002 
6003 	if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
6004 		ret = -1;
6005 
6006 	if (group->references > 1) {
6007 		wpa_printf(MSG_DEBUG,
6008 			   "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
6009 			   vlan_id);
6010 		ret = -2;
6011 	}
6012 
6013 	wpa_group_put(wpa_auth, group);
6014 
6015 	return ret;
6016 }
6017 
6018 
wpa_auth_sta_set_vlan(struct wpa_state_machine * sm,int vlan_id)6019 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
6020 {
6021 	struct wpa_group *group;
6022 
6023 	if (!sm || !sm->wpa_auth)
6024 		return 0;
6025 
6026 	group = sm->wpa_auth->group;
6027 	while (group) {
6028 		if (group->vlan_id == vlan_id)
6029 			break;
6030 		group = group->next;
6031 	}
6032 
6033 	if (!group) {
6034 		group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
6035 		if (!group)
6036 			return -1;
6037 	}
6038 
6039 	if (sm->group == group)
6040 		return 0;
6041 
6042 	if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
6043 		return -1;
6044 
6045 	wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR
6046 		   " to use group state machine for VLAN ID %d",
6047 		   MAC2STR(wpa_auth_get_spa(sm)), vlan_id);
6048 
6049 	wpa_group_get(sm->wpa_auth, group);
6050 	wpa_group_put(sm->wpa_auth, sm->group);
6051 	sm->group = group;
6052 
6053 	return 0;
6054 }
6055 
6056 
wpa_auth_eapol_key_tx_status(struct wpa_authenticator * wpa_auth,struct wpa_state_machine * sm,int ack)6057 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
6058 				  struct wpa_state_machine *sm, int ack)
6059 {
6060 	if (!wpa_auth || !sm)
6061 		return;
6062 	wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
6063 		   " ack=%d", MAC2STR(wpa_auth_get_spa(sm)), ack);
6064 	if (sm->pending_1_of_4_timeout && ack) {
6065 		/*
6066 		 * Some deployed supplicant implementations update their SNonce
6067 		 * for each EAPOL-Key 2/4 message even within the same 4-way
6068 		 * handshake and then fail to use the first SNonce when
6069 		 * deriving the PTK. This results in unsuccessful 4-way
6070 		 * handshake whenever the relatively short initial timeout is
6071 		 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
6072 		 * around this by increasing the timeout now that we know that
6073 		 * the station has received the frame.
6074 		 */
6075 		int timeout_ms = eapol_key_timeout_subseq;
6076 		wpa_printf(MSG_DEBUG,
6077 			   "WPA: Increase initial EAPOL-Key 1/4 timeout by %u ms because of acknowledged frame",
6078 			   timeout_ms);
6079 		eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
6080 		eloop_register_timeout(timeout_ms / 1000,
6081 				       (timeout_ms % 1000) * 1000,
6082 				       wpa_send_eapol_timeout, wpa_auth, sm);
6083 	}
6084 
6085 #ifdef CONFIG_TESTING_OPTIONS
6086 	if (sm->eapol_status_cb) {
6087 		sm->eapol_status_cb(sm->eapol_status_cb_ctx1,
6088 				    sm->eapol_status_cb_ctx2);
6089 		sm->eapol_status_cb = NULL;
6090 	}
6091 #endif /* CONFIG_TESTING_OPTIONS */
6092 }
6093 
6094 
wpa_auth_uses_sae(struct wpa_state_machine * sm)6095 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
6096 {
6097 	if (!sm)
6098 		return 0;
6099 	return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
6100 }
6101 
6102 
wpa_auth_uses_ft_sae(struct wpa_state_machine * sm)6103 int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
6104 {
6105 	if (!sm)
6106 		return 0;
6107 	return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE ||
6108 		sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY;
6109 }
6110 
6111 
6112 #ifdef CONFIG_P2P
wpa_auth_get_ip_addr(struct wpa_state_machine * sm,u8 * addr)6113 int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
6114 {
6115 	if (!sm || WPA_GET_BE32(sm->ip_addr) == 0)
6116 		return -1;
6117 	os_memcpy(addr, sm->ip_addr, 4);
6118 	return 0;
6119 }
6120 #endif /* CONFIG_P2P */
6121 
6122 
wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator * wpa_auth,struct radius_das_attrs * attr)6123 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
6124 					 struct radius_das_attrs *attr)
6125 {
6126 	return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
6127 }
6128 
6129 
wpa_auth_reconfig_group_keys(struct wpa_authenticator * wpa_auth)6130 void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
6131 {
6132 	struct wpa_group *group;
6133 
6134 	if (!wpa_auth)
6135 		return;
6136 	for (group = wpa_auth->group; group; group = group->next)
6137 		wpa_group_config_group_keys(wpa_auth, group);
6138 }
6139 
6140 
6141 #ifdef CONFIG_FILS
6142 
6143 struct wpa_auth_fils_iter_data {
6144 	struct wpa_authenticator *auth;
6145 	const u8 *cache_id;
6146 	struct rsn_pmksa_cache_entry *pmksa;
6147 	const u8 *spa;
6148 	const u8 *pmkid;
6149 };
6150 
6151 
wpa_auth_fils_iter(struct wpa_authenticator * a,void * ctx)6152 static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
6153 {
6154 	struct wpa_auth_fils_iter_data *data = ctx;
6155 
6156 	if (a == data->auth || !a->conf.fils_cache_id_set ||
6157 	    os_memcmp(a->conf.fils_cache_id, data->cache_id,
6158 		      FILS_CACHE_ID_LEN) != 0)
6159 		return 0;
6160 	data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
6161 	return data->pmksa != NULL;
6162 }
6163 
6164 
6165 struct rsn_pmksa_cache_entry *
wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator * wpa_auth,const u8 * sta_addr,const u8 * pmkid)6166 wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
6167 				 const u8 *sta_addr, const u8 *pmkid)
6168 {
6169 	struct wpa_auth_fils_iter_data idata;
6170 
6171 	if (!wpa_auth->conf.fils_cache_id_set)
6172 		return NULL;
6173 	idata.auth = wpa_auth;
6174 	idata.cache_id = wpa_auth->conf.fils_cache_id;
6175 	idata.pmksa = NULL;
6176 	idata.spa = sta_addr;
6177 	idata.pmkid = pmkid;
6178 	wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
6179 	return idata.pmksa;
6180 }
6181 
6182 
6183 #ifdef CONFIG_IEEE80211R_AP
wpa_auth_write_fte(struct wpa_authenticator * wpa_auth,struct wpa_state_machine * sm,u8 * buf,size_t len)6184 int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth,
6185 		       struct wpa_state_machine *sm,
6186 		       u8 *buf, size_t len)
6187 {
6188 	struct wpa_auth_config *conf = &wpa_auth->conf;
6189 
6190 	return wpa_write_ftie(conf, sm->wpa_key_mgmt, sm->xxkey_len,
6191 			      conf->r0_key_holder, conf->r0_key_holder_len,
6192 			      NULL, NULL, buf, len, NULL, 0, 0);
6193 }
6194 #endif /* CONFIG_IEEE80211R_AP */
6195 
6196 
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)6197 void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm,
6198 				   u8 *fils_anonce, u8 *fils_snonce,
6199 				   u8 *fils_kek, size_t *fils_kek_len)
6200 {
6201 	os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN);
6202 	os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN);
6203 	os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN);
6204 	*fils_kek_len = sm->PTK.kek_len;
6205 }
6206 
6207 
wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine * sm,const u8 * pmk,size_t pmk_len,const u8 * pmkid)6208 void wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine *sm, const u8 *pmk,
6209 				 size_t pmk_len, const u8 *pmkid)
6210 {
6211 	os_memcpy(sm->PMK, pmk, pmk_len);
6212 	sm->pmk_len = pmk_len;
6213 	os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
6214 	sm->pmkid_set = 1;
6215 }
6216 
6217 #endif /* CONFIG_FILS */
6218 
6219 
wpa_auth_set_auth_alg(struct wpa_state_machine * sm,u16 auth_alg)6220 void wpa_auth_set_auth_alg(struct wpa_state_machine *sm, u16 auth_alg)
6221 {
6222 	if (sm)
6223 		sm->auth_alg = auth_alg;
6224 }
6225 
6226 
6227 #ifdef CONFIG_DPP2
wpa_auth_set_dpp_z(struct wpa_state_machine * sm,const struct wpabuf * z)6228 void wpa_auth_set_dpp_z(struct wpa_state_machine *sm, const struct wpabuf *z)
6229 {
6230 	if (sm) {
6231 		wpabuf_clear_free(sm->dpp_z);
6232 		sm->dpp_z = z ? wpabuf_dup(z) : NULL;
6233 	}
6234 }
6235 #endif /* CONFIG_DPP2 */
6236 
6237 
wpa_auth_set_transition_disable(struct wpa_authenticator * wpa_auth,u8 val)6238 void wpa_auth_set_transition_disable(struct wpa_authenticator *wpa_auth,
6239 				     u8 val)
6240 {
6241 	if (wpa_auth)
6242 		wpa_auth->conf.transition_disable = val;
6243 }
6244 
6245 
6246 #ifdef CONFIG_TESTING_OPTIONS
6247 
wpa_auth_resend_m1(struct wpa_state_machine * sm,int change_anonce,void (* cb)(void * ctx1,void * ctx2),void * ctx1,void * ctx2)6248 int wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce,
6249 		       void (*cb)(void *ctx1, void *ctx2),
6250 		       void *ctx1, void *ctx2)
6251 {
6252 	const u8 *anonce = sm->ANonce;
6253 	u8 anonce_buf[WPA_NONCE_LEN];
6254 
6255 	if (change_anonce) {
6256 		if (random_get_bytes(anonce_buf, WPA_NONCE_LEN))
6257 			return -1;
6258 		anonce = anonce_buf;
6259 	}
6260 
6261 	wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
6262 			"sending 1/4 msg of 4-Way Handshake (TESTING)");
6263 	wpa_send_eapol(sm->wpa_auth, sm,
6264 		       WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
6265 		       anonce, NULL, 0, 0, 0);
6266 	return 0;
6267 }
6268 
6269 
wpa_auth_resend_m3(struct wpa_state_machine * sm,void (* cb)(void * ctx1,void * ctx2),void * ctx1,void * ctx2)6270 int wpa_auth_resend_m3(struct wpa_state_machine *sm,
6271 		       void (*cb)(void *ctx1, void *ctx2),
6272 		       void *ctx1, void *ctx2)
6273 {
6274 	u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
6275 	u8 *opos;
6276 	size_t gtk_len, kde_len;
6277 	struct wpa_auth_config *conf = &sm->wpa_auth->conf;
6278 	struct wpa_group *gsm = sm->group;
6279 	u8 *wpa_ie;
6280 	int wpa_ie_len, secure, gtkidx, encr = 0;
6281 	u8 hdr[2];
6282 
6283 	/* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
6284 	   GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2])
6285 	 */
6286 
6287 	/* Use 0 RSC */
6288 	os_memset(rsc, 0, WPA_KEY_RSC_LEN);
6289 	/* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
6290 	wpa_ie = sm->wpa_auth->wpa_ie;
6291 	wpa_ie_len = sm->wpa_auth->wpa_ie_len;
6292 	if (sm->wpa == WPA_VERSION_WPA &&
6293 	    (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
6294 	    wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
6295 		/* WPA-only STA, remove RSN IE and possible MDIE */
6296 		wpa_ie = wpa_ie + wpa_ie[1] + 2;
6297 		if (wpa_ie[0] == WLAN_EID_RSNX)
6298 			wpa_ie = wpa_ie + wpa_ie[1] + 2;
6299 		if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
6300 			wpa_ie = wpa_ie + wpa_ie[1] + 2;
6301 		wpa_ie_len = wpa_ie[1] + 2;
6302 	}
6303 	wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
6304 			"sending 3/4 msg of 4-Way Handshake (TESTING)");
6305 	if (sm->wpa == WPA_VERSION_WPA2) {
6306 		/* WPA2 send GTK in the 4-way handshake */
6307 		secure = 1;
6308 		gtk = gsm->GTK[gsm->GN - 1];
6309 		gtk_len = gsm->GTK_len;
6310 		gtkidx = gsm->GN;
6311 		_rsc = rsc;
6312 		encr = 1;
6313 	} else {
6314 		/* WPA does not include GTK in msg 3/4 */
6315 		secure = 0;
6316 		gtk = NULL;
6317 		gtk_len = 0;
6318 		_rsc = NULL;
6319 		if (sm->rx_eapol_key_secure) {
6320 			/*
6321 			 * It looks like Windows 7 supplicant tries to use
6322 			 * Secure bit in msg 2/4 after having reported Michael
6323 			 * MIC failure and it then rejects the 4-way handshake
6324 			 * if msg 3/4 does not set Secure bit. Work around this
6325 			 * by setting the Secure bit here even in the case of
6326 			 * WPA if the supplicant used it first.
6327 			 */
6328 			wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm),
6329 					LOGGER_DEBUG,
6330 					"STA used Secure bit in WPA msg 2/4 - set Secure for 3/4 as workaround");
6331 			secure = 1;
6332 		}
6333 	}
6334 
6335 	kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
6336 
6337 	if (sm->use_ext_key_id)
6338 		kde_len += 2 + RSN_SELECTOR_LEN + 2;
6339 
6340 	if (gtk)
6341 		kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
6342 #ifdef CONFIG_IEEE80211R_AP
6343 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
6344 		kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
6345 		kde_len += 300; /* FTIE + 2 * TIE */
6346 	}
6347 #endif /* CONFIG_IEEE80211R_AP */
6348 	kde = os_malloc(kde_len);
6349 	if (!kde)
6350 		return -1;
6351 
6352 	pos = kde;
6353 	os_memcpy(pos, wpa_ie, wpa_ie_len);
6354 	pos += wpa_ie_len;
6355 #ifdef CONFIG_IEEE80211R_AP
6356 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
6357 		int res;
6358 		size_t elen;
6359 
6360 		elen = pos - kde;
6361 		res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
6362 		if (res < 0) {
6363 			wpa_printf(MSG_ERROR,
6364 				   "FT: Failed to insert PMKR1Name into RSN IE in EAPOL-Key data");
6365 			os_free(kde);
6366 			return -1;
6367 		}
6368 		pos -= wpa_ie_len;
6369 		pos += elen;
6370 	}
6371 #endif /* CONFIG_IEEE80211R_AP */
6372 	hdr[1] = 0;
6373 
6374 	if (sm->use_ext_key_id) {
6375 		hdr[0] = sm->keyidx_active & 0x01;
6376 		pos = wpa_add_kde(pos, RSN_KEY_DATA_KEYID, hdr, 2, NULL, 0);
6377 	}
6378 
6379 	if (gtk) {
6380 		hdr[0] = gtkidx & 0x03;
6381 		pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
6382 				  gtk, gtk_len);
6383 	}
6384 	opos = pos;
6385 	pos = ieee80211w_kde_add(sm, pos);
6386 	if (pos - opos >= 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
6387 		/* skip KDE header and keyid */
6388 		opos += 2 + RSN_SELECTOR_LEN + 2;
6389 		os_memset(opos, 0, 6); /* clear PN */
6390 	}
6391 	if (ocv_oci_add(sm, &pos, conf->oci_freq_override_eapol_m3) < 0) {
6392 		os_free(kde);
6393 		return -1;
6394 	}
6395 
6396 #ifdef CONFIG_IEEE80211R_AP
6397 	if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
6398 		int res;
6399 
6400 		if (sm->assoc_resp_ftie &&
6401 		    kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
6402 			os_memcpy(pos, sm->assoc_resp_ftie,
6403 				  2 + sm->assoc_resp_ftie[1]);
6404 			res = 2 + sm->assoc_resp_ftie[1];
6405 		} else {
6406 			res = wpa_write_ftie(conf, sm->wpa_key_mgmt,
6407 					     sm->xxkey_len,
6408 					     conf->r0_key_holder,
6409 					     conf->r0_key_holder_len,
6410 					     NULL, NULL, pos,
6411 					     kde + kde_len - pos,
6412 					     NULL, 0, 0);
6413 		}
6414 		if (res < 0) {
6415 			wpa_printf(MSG_ERROR,
6416 				   "FT: Failed to insert FTIE into EAPOL-Key Key Data");
6417 			os_free(kde);
6418 			return -1;
6419 		}
6420 		pos += res;
6421 
6422 		/* TIE[ReassociationDeadline] (TU) */
6423 		*pos++ = WLAN_EID_TIMEOUT_INTERVAL;
6424 		*pos++ = 5;
6425 		*pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
6426 		WPA_PUT_LE32(pos, conf->reassociation_deadline);
6427 		pos += 4;
6428 
6429 		/* TIE[KeyLifetime] (seconds) */
6430 		*pos++ = WLAN_EID_TIMEOUT_INTERVAL;
6431 		*pos++ = 5;
6432 		*pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
6433 		WPA_PUT_LE32(pos, conf->r0_key_lifetime);
6434 		pos += 4;
6435 	}
6436 #endif /* CONFIG_IEEE80211R_AP */
6437 
6438 	wpa_send_eapol(sm->wpa_auth, sm,
6439 		       (secure ? WPA_KEY_INFO_SECURE : 0) |
6440 		       (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
6441 			WPA_KEY_INFO_MIC : 0) |
6442 		       WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
6443 		       WPA_KEY_INFO_KEY_TYPE,
6444 		       _rsc, sm->ANonce, kde, pos - kde, 0, encr);
6445 	bin_clear_free(kde, kde_len);
6446 	return 0;
6447 }
6448 
6449 
wpa_auth_resend_group_m1(struct wpa_state_machine * sm,void (* cb)(void * ctx1,void * ctx2),void * ctx1,void * ctx2)6450 int wpa_auth_resend_group_m1(struct wpa_state_machine *sm,
6451 			     void (*cb)(void *ctx1, void *ctx2),
6452 			     void *ctx1, void *ctx2)
6453 {
6454 	u8 rsc[WPA_KEY_RSC_LEN];
6455 	struct wpa_auth_config *conf = &sm->wpa_auth->conf;
6456 	struct wpa_group *gsm = sm->group;
6457 	const u8 *kde;
6458 	u8 *kde_buf = NULL, *pos, hdr[2];
6459 	u8 *opos;
6460 	size_t kde_len;
6461 	u8 *gtk;
6462 
6463 	/* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
6464 	os_memset(rsc, 0, WPA_KEY_RSC_LEN);
6465 	/* Use 0 RSC */
6466 	wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
6467 			"sending 1/2 msg of Group Key Handshake (TESTING)");
6468 
6469 	gtk = gsm->GTK[gsm->GN - 1];
6470 	if (sm->wpa == WPA_VERSION_WPA2) {
6471 		kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
6472 			ieee80211w_kde_len(sm) + ocv_oci_len(sm);
6473 		kde_buf = os_malloc(kde_len);
6474 		if (!kde_buf)
6475 			return -1;
6476 
6477 		kde = pos = kde_buf;
6478 		hdr[0] = gsm->GN & 0x03;
6479 		hdr[1] = 0;
6480 		pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
6481 				  gtk, gsm->GTK_len);
6482 		opos = pos;
6483 		pos = ieee80211w_kde_add(sm, pos);
6484 		if (pos - opos >=
6485 		    2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
6486 			/* skip KDE header and keyid */
6487 			opos += 2 + RSN_SELECTOR_LEN + 2;
6488 			os_memset(opos, 0, 6); /* clear PN */
6489 		}
6490 		if (ocv_oci_add(sm, &pos,
6491 				conf->oci_freq_override_eapol_g1) < 0) {
6492 			os_free(kde_buf);
6493 			return -1;
6494 		}
6495 		kde_len = pos - kde;
6496 	} else {
6497 		kde = gtk;
6498 		kde_len = gsm->GTK_len;
6499 	}
6500 
6501 	sm->eapol_status_cb = cb;
6502 	sm->eapol_status_cb_ctx1 = ctx1;
6503 	sm->eapol_status_cb_ctx2 = ctx2;
6504 
6505 	wpa_send_eapol(sm->wpa_auth, sm,
6506 		       WPA_KEY_INFO_SECURE |
6507 		       (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
6508 			WPA_KEY_INFO_MIC : 0) |
6509 		       WPA_KEY_INFO_ACK |
6510 		       (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
6511 		       rsc, NULL, kde, kde_len, gsm->GN, 1);
6512 
6513 	bin_clear_free(kde_buf, kde_len);
6514 	return 0;
6515 }
6516 
6517 
wpa_auth_rekey_gtk(struct wpa_authenticator * wpa_auth)6518 int wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth)
6519 {
6520 	if (!wpa_auth)
6521 		return -1;
6522 	eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
6523 	return eloop_register_timeout(0, 0, wpa_rekey_gtk, wpa_auth, NULL);
6524 }
6525 
6526 
wpa_auth_rekey_ptk(struct wpa_authenticator * wpa_auth,struct wpa_state_machine * sm)6527 int wpa_auth_rekey_ptk(struct wpa_authenticator *wpa_auth,
6528 		       struct wpa_state_machine *sm)
6529 {
6530 	if (!wpa_auth || !sm)
6531 		return -1;
6532 	wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
6533 	wpa_request_new_ptk(sm);
6534 	wpa_sm_step(sm);
6535 	return 0;
6536 }
6537 
6538 
wpa_auth_set_ft_rsnxe_used(struct wpa_authenticator * wpa_auth,int val)6539 void wpa_auth_set_ft_rsnxe_used(struct wpa_authenticator *wpa_auth, int val)
6540 {
6541 	if (wpa_auth)
6542 		wpa_auth->conf.ft_rsnxe_used = val;
6543 }
6544 
6545 
wpa_auth_set_ocv_override_freq(struct wpa_authenticator * wpa_auth,enum wpa_auth_ocv_override_frame frame,unsigned int freq)6546 void wpa_auth_set_ocv_override_freq(struct wpa_authenticator *wpa_auth,
6547 				    enum wpa_auth_ocv_override_frame frame,
6548 				    unsigned int freq)
6549 {
6550 	if (!wpa_auth)
6551 		return;
6552 	switch (frame) {
6553 	case WPA_AUTH_OCV_OVERRIDE_EAPOL_M3:
6554 		wpa_auth->conf.oci_freq_override_eapol_m3 = freq;
6555 		break;
6556 	case WPA_AUTH_OCV_OVERRIDE_EAPOL_G1:
6557 		wpa_auth->conf.oci_freq_override_eapol_g1 = freq;
6558 		break;
6559 	case WPA_AUTH_OCV_OVERRIDE_FT_ASSOC:
6560 		wpa_auth->conf.oci_freq_override_ft_assoc = freq;
6561 		break;
6562 	case WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC:
6563 		wpa_auth->conf.oci_freq_override_fils_assoc = freq;
6564 		break;
6565 	}
6566 }
6567 
wpa_auth_set_skip_send_eapol(struct wpa_authenticator * wpa_auth,u8 val)6568 void wpa_auth_set_skip_send_eapol(struct wpa_authenticator *wpa_auth,
6569 				     u8 val)
6570 {
6571 	if (wpa_auth)
6572 		wpa_auth->conf.skip_send_eapol = val;
6573 }
6574 
wpa_auth_set_enable_eapol_large_timeout(struct wpa_authenticator * wpa_auth,u8 val)6575 void wpa_auth_set_enable_eapol_large_timeout(struct wpa_authenticator *wpa_auth,
6576 				     u8 val)
6577 {
6578 	if (wpa_auth)
6579 		wpa_auth->conf.enable_eapol_large_timeout = val;
6580 }
6581 
6582 
6583 #endif /* CONFIG_TESTING_OPTIONS */
6584 
6585 
wpa_auth_sta_radius_psk_resp(struct wpa_state_machine * sm,bool success)6586 void wpa_auth_sta_radius_psk_resp(struct wpa_state_machine *sm, bool success)
6587 {
6588 	if (!sm->waiting_radius_psk) {
6589 		wpa_printf(MSG_DEBUG,
6590 			   "Ignore RADIUS PSK response for " MACSTR
6591 			   " that did not wait one",
6592 			   MAC2STR(sm->addr));
6593 		return;
6594 	}
6595 
6596 	wpa_printf(MSG_DEBUG, "RADIUS PSK response for " MACSTR " (%s)",
6597 		   MAC2STR(sm->addr), success ? "success" : "fail");
6598 	sm->waiting_radius_psk = 0;
6599 
6600 	if (success) {
6601 		/* Try to process the EAPOL-Key msg 2/4 again */
6602 		sm->EAPOLKeyReceived = true;
6603 	} else {
6604 		sm->Disconnect = true;
6605 	}
6606 
6607 	eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
6608 }
6609 
6610 
wpa_auth_set_ml_info(struct wpa_state_machine * sm,const u8 * mld_addr,u8 mld_assoc_link_id,struct mld_info * info)6611 void wpa_auth_set_ml_info(struct wpa_state_machine *sm, const u8 *mld_addr,
6612 			  u8 mld_assoc_link_id, struct mld_info *info)
6613 {
6614 #ifdef CONFIG_IEEE80211BE
6615 	struct wpa_auth_ml_rsn_info ml_rsn_info;
6616 	unsigned int link_id, i;
6617 
6618 	if (!info)
6619 		return;
6620 
6621 	os_memset(sm->mld_links, 0, sizeof(sm->mld_links));
6622 
6623 	wpa_auth_logger(sm->wpa_auth, wpa_auth_get_spa(sm), LOGGER_DEBUG,
6624 			"MLD: Initialization");
6625 
6626 	os_memcpy(sm->own_mld_addr, mld_addr, ETH_ALEN);
6627 	os_memcpy(sm->peer_mld_addr, info->common_info.mld_addr, ETH_ALEN);
6628 
6629 	sm->mld_assoc_link_id = mld_assoc_link_id;
6630 
6631 	os_memset(&ml_rsn_info, 0, sizeof(ml_rsn_info));
6632 
6633 	for (i = 0, link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
6634 		struct mld_link_info *link = &info->links[link_id];
6635 		struct mld_link *sm_link = &sm->mld_links[link_id];
6636 
6637 		sm_link->valid = link->valid;
6638 		if (!link->valid)
6639 			continue;
6640 
6641 		os_memcpy(sm_link->peer_addr, link->peer_addr, ETH_ALEN);
6642 		os_memcpy(sm_link->own_addr, link->local_addr, ETH_ALEN);
6643 
6644 		wpa_printf(MSG_DEBUG,
6645 			   "WPA_AUTH: MLD: id=%u, addr=" MACSTR " peer=" MACSTR,
6646 			   link_id,
6647 			   MAC2STR(sm_link->own_addr),
6648 			   MAC2STR(sm_link->peer_addr));
6649 
6650 		if (link_id != mld_assoc_link_id)
6651 			sm->n_mld_affiliated_links++;
6652 
6653 		ml_rsn_info.links[i++].link_id = link_id;
6654 	}
6655 
6656 	ml_rsn_info.n_mld_links = i;
6657 
6658 	wpa_auth_get_ml_rsn_info(sm->wpa_auth, &ml_rsn_info);
6659 
6660 	for (i = 0; i < ml_rsn_info.n_mld_links; i++) {
6661 		struct mld_link *sm_link;
6662 		const u8 *rsn_ies;
6663 		u8 rsn_ies_len;
6664 
6665 		sm_link = &sm->mld_links[ml_rsn_info.links[i].link_id];
6666 		rsn_ies = ml_rsn_info.links[i].rsn_ies;
6667 		rsn_ies_len = ml_rsn_info.links[i].rsn_ies_len;
6668 
6669 		/* This should not really happen */
6670 		if (!rsn_ies || rsn_ies_len < 2 || rsn_ies[0] != WLAN_EID_RSN ||
6671 		    rsn_ies[1] + 2 > rsn_ies_len) {
6672 			wpa_printf(MSG_INFO, "WPA_AUTH: MLD: Invalid RSNE");
6673 			continue;
6674 		}
6675 
6676 		sm_link->rsne = rsn_ies;
6677 		sm_link->rsne_len = rsn_ies[1] + 2;
6678 
6679 		if (rsn_ies[1] + 2UL + 2UL < rsn_ies_len &&
6680 		    rsn_ies[rsn_ies[1] + 2] == WLAN_EID_RSNX) {
6681 			sm_link->rsnxe = rsn_ies + 2 + rsn_ies[1];
6682 			sm_link->rsnxe_len = sm_link->rsnxe[1] + 2;
6683 		}
6684 	}
6685 #endif /* CONFIG_IEEE80211BE */
6686 }
6687