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