1 /*
2  * wpa_supplicant - PASN processing
3  *
4  * Copyright (C) 2019 Intel Corporation
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9 
10 #include "includes.h"
11 
12 #include "common/ieee802_11_defs.h"
13 #include "common/ieee802_11_common.h"
14 #include "common/dragonfly.h"
15 #include "common/ptksa_cache.h"
16 #include "utils/eloop.h"
17 #include "drivers/driver.h"
18 #include "crypto/crypto.h"
19 #include "crypto/random.h"
20 #include "eap_common/eap_defs.h"
21 #include "rsn_supp/wpa.h"
22 #include "rsn_supp/pmksa_cache.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "bss.h"
26 #include "scan.h"
27 #include "config.h"
28 
29 static const int dot11RSNAConfigPMKLifetime = 43200;
30 
31 struct wpa_pasn_auth_work {
32 	u8 own_addr[ETH_ALEN];
33 	u8 peer_addr[ETH_ALEN];
34 	int akmp;
35 	int cipher;
36 	u16 group;
37 	int network_id;
38 	struct wpabuf *comeback;
39 };
40 
41 
wpas_pasn_send_mlme(void * ctx,const u8 * data,size_t data_len,int noack,unsigned int freq,unsigned int wait)42 static int wpas_pasn_send_mlme(void *ctx, const u8 *data, size_t data_len,
43 			       int noack, unsigned int freq, unsigned int wait)
44 {
45 	struct wpa_supplicant *wpa_s = ctx;
46 
47 	return wpa_drv_send_mlme(wpa_s, data, data_len, noack, freq, wait);
48 }
49 
50 
wpas_pasn_free_auth_work(struct wpa_pasn_auth_work * awork)51 static void wpas_pasn_free_auth_work(struct wpa_pasn_auth_work *awork)
52 {
53 	wpabuf_free(awork->comeback);
54 	awork->comeback = NULL;
55 	os_free(awork);
56 }
57 
58 
wpas_pasn_auth_work_timeout(void * eloop_ctx,void * timeout_ctx)59 static void wpas_pasn_auth_work_timeout(void *eloop_ctx, void *timeout_ctx)
60 {
61 	struct wpa_supplicant *wpa_s = eloop_ctx;
62 
63 	wpa_printf(MSG_DEBUG, "PASN: Auth work timeout - stopping auth");
64 
65 	wpas_pasn_auth_stop(wpa_s);
66 
67 	wpas_pasn_auth_work_done(wpa_s, PASN_STATUS_FAILURE);
68 }
69 
70 
wpas_pasn_cancel_auth_work(struct wpa_supplicant * wpa_s)71 static void wpas_pasn_cancel_auth_work(struct wpa_supplicant *wpa_s)
72 {
73 	wpa_printf(MSG_DEBUG, "PASN: Cancel pasn-start-auth work");
74 
75 	/* Remove pending/started work */
76 	radio_remove_works(wpa_s, "pasn-start-auth", 0);
77 }
78 
79 
wpas_pasn_auth_status(struct wpa_supplicant * wpa_s,const u8 * peer_addr,int akmp,int cipher,u8 status,struct wpabuf * comeback,u16 comeback_after)80 static void wpas_pasn_auth_status(struct wpa_supplicant *wpa_s,
81 				  const u8 *peer_addr,
82 				  int akmp, int cipher, u8 status,
83 				  struct wpabuf *comeback,
84 				  u16 comeback_after)
85 {
86 	if (comeback) {
87 		size_t comeback_len = wpabuf_len(comeback);
88 		size_t buflen = comeback_len * 2 + 1;
89 		char *comeback_txt = os_malloc(buflen);
90 
91 		if (comeback_txt) {
92 			wpa_snprintf_hex(comeback_txt, buflen,
93 					 wpabuf_head(comeback), comeback_len);
94 
95 			wpa_msg(wpa_s, MSG_INFO, PASN_AUTH_STATUS MACSTR
96 				" akmp=%s, status=%u comeback_after=%u comeback=%s",
97 				MAC2STR(peer_addr),
98 				wpa_key_mgmt_txt(akmp, WPA_PROTO_RSN),
99 				status, comeback_after, comeback_txt);
100 
101 			os_free(comeback_txt);
102 			return;
103 		}
104 	}
105 
106 	wpa_msg(wpa_s, MSG_INFO,
107 		PASN_AUTH_STATUS MACSTR " akmp=%s, status=%u",
108 		MAC2STR(peer_addr), wpa_key_mgmt_txt(akmp, WPA_PROTO_RSN),
109 		status);
110 }
111 
112 
113 #ifdef CONFIG_SAE
114 
115 static struct sae_pt *
wpas_pasn_sae_derive_pt(struct wpa_ssid * ssid,int group)116 wpas_pasn_sae_derive_pt(struct wpa_ssid *ssid, int group)
117 {
118 	const char *password = ssid->sae_password;
119 	int groups[2] = { group, 0 };
120 
121 	if (!password)
122 		password = ssid->passphrase;
123 
124 	if (!password) {
125 		wpa_printf(MSG_DEBUG, "PASN: SAE without a password");
126 		return NULL;
127 	}
128 
129 	return sae_derive_pt(groups, ssid->ssid, ssid->ssid_len,
130 			    (const u8 *) password, os_strlen(password),
131 			    ssid->sae_password_id);
132 }
133 
134 
wpas_pasn_sae_setup_pt(struct wpa_ssid * ssid,int group)135 static int wpas_pasn_sae_setup_pt(struct wpa_ssid *ssid, int group)
136 {
137 	if (!ssid->sae_password && !ssid->passphrase) {
138 		wpa_printf(MSG_DEBUG, "PASN: SAE without a password");
139 		return -1;
140 	}
141 
142 	if (ssid->pt)
143 		return 0; /* PT already derived */
144 
145 	ssid->pt = wpas_pasn_sae_derive_pt(ssid, group);
146 
147 	return ssid->pt ? 0 : -1;
148 }
149 
150 #endif /* CONFIG_SAE */
151 
152 
wpas_pasn_get_params_from_bss(struct wpa_supplicant * wpa_s,struct pasn_peer * peer)153 static int wpas_pasn_get_params_from_bss(struct wpa_supplicant *wpa_s,
154 					 struct pasn_peer *peer)
155 {
156 	int ret;
157 	const u8 *rsne, *rsnxe;
158 	struct wpa_bss *bss;
159 	struct wpa_ie_data rsne_data;
160 	int sel, key_mgmt, pairwise_cipher;
161 	int network_id = 0, group = 19;
162 	struct wpa_ssid *ssid = NULL;
163 	size_t ssid_str_len = 0;
164 	const u8 *ssid_str = NULL;
165 	const u8 *peer_addr = peer->peer_addr;
166 
167 	bss = wpa_bss_get_bssid(wpa_s, peer_addr);
168 	if (!bss) {
169 		wpa_supplicant_update_scan_results(wpa_s, peer_addr);
170 		bss = wpa_bss_get_bssid(wpa_s, peer_addr);
171 		if (!bss) {
172 			wpa_printf(MSG_DEBUG, "PASN: BSS not found");
173 			return -1;
174 		}
175 	}
176 
177 	rsne = wpa_bss_get_rsne(wpa_s, bss, NULL, false);
178 	if (!rsne) {
179 		wpa_printf(MSG_DEBUG, "PASN: BSS without RSNE");
180 		return -1;
181 	}
182 
183 	ret = wpa_parse_wpa_ie(rsne, *(rsne + 1) + 2, &rsne_data);
184 	if (ret) {
185 		wpa_printf(MSG_DEBUG, "PASN: Failed parsing RSNE data");
186 		return -1;
187 	}
188 
189 	rsnxe = wpa_bss_get_rsnxe(wpa_s, bss, NULL, false);
190 
191 	ssid_str_len = bss->ssid_len;
192 	ssid_str = bss->ssid;
193 
194 	/* Get the network configuration based on the obtained SSID */
195 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
196 		if (!wpas_network_disabled(wpa_s, ssid) &&
197 		    ssid_str_len == ssid->ssid_len &&
198 		    os_memcmp(ssid_str, ssid->ssid, ssid_str_len) == 0)
199 			break;
200 	}
201 
202 	if (ssid)
203 		network_id = ssid->id;
204 
205 	sel = rsne_data.pairwise_cipher;
206 	if (ssid && ssid->pairwise_cipher)
207 		sel &= ssid->pairwise_cipher;
208 
209 	wpa_printf(MSG_DEBUG, "PASN: peer pairwise 0x%x, select 0x%x",
210 		   rsne_data.pairwise_cipher, sel);
211 
212 	pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
213 	if (pairwise_cipher < 0) {
214 		wpa_msg(wpa_s, MSG_WARNING,
215 			"PASN: Failed to select pairwise cipher");
216 		return -1;
217 	}
218 
219 	sel = rsne_data.key_mgmt;
220 	if (ssid && ssid->key_mgmt)
221 		sel &= ssid->key_mgmt;
222 
223 	wpa_printf(MSG_DEBUG, "PASN: peer AKMP 0x%x, select 0x%x",
224 		   rsne_data.key_mgmt, sel);
225 #ifdef CONFIG_SAE
226 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) || !ssid)
227 		sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_SAE_EXT_KEY |
228 			 WPA_KEY_MGMT_FT_SAE | WPA_KEY_MGMT_FT_SAE_EXT_KEY);
229 #endif /* CONFIG_SAE */
230 #ifdef CONFIG_IEEE80211R
231 	if (!(wpa_s->drv_flags & (WPA_DRIVER_FLAGS_SME |
232 				  WPA_DRIVER_FLAGS_UPDATE_FT_IES)))
233 		sel &= ~WPA_KEY_MGMT_FT;
234 #endif /* CONFIG_IEEE80211R */
235 	if (0) {
236 #ifdef CONFIG_IEEE80211R
237 #ifdef CONFIG_SHA384
238 	} else if ((sel & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) &&
239 		   os_strcmp(wpa_supplicant_get_eap_mode(wpa_s), "LEAP") != 0) {
240 		key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
241 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT FT/802.1X-SHA384");
242 		if (ssid && !ssid->ft_eap_pmksa_caching &&
243 		    pmksa_cache_get_current(wpa_s->wpa)) {
244 			/* PMKSA caching with FT may have interoperability
245 			 * issues, so disable that case by default for now.
246 			 */
247 			wpa_printf(MSG_DEBUG,
248 				   "PASN: Disable PMKSA caching for FT/802.1X connection");
249 			pmksa_cache_clear_current(wpa_s->wpa);
250 		}
251 #endif /* CONFIG_SHA384 */
252 #endif /* CONFIG_IEEE80211R */
253 #ifdef CONFIG_SAE
254 	} else if ((sel & WPA_KEY_MGMT_SAE_EXT_KEY) && ssid &&
255 		   (ieee802_11_rsnx_capab(rsnxe,
256 					   WLAN_RSNX_CAPAB_SAE_H2E)) &&
257 		   (wpas_pasn_sae_setup_pt(ssid, group) == 0)) {
258 		key_mgmt = WPA_KEY_MGMT_SAE_EXT_KEY;
259 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT SAE (ext key)");
260 	} else if ((sel & WPA_KEY_MGMT_SAE) && ssid &&
261 		   (ieee802_11_rsnx_capab(rsnxe,
262 					   WLAN_RSNX_CAPAB_SAE_H2E)) &&
263 		   (wpas_pasn_sae_setup_pt(ssid, group) == 0)) {
264 		key_mgmt = WPA_KEY_MGMT_SAE;
265 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT SAE");
266 #endif /* CONFIG_SAE */
267 #ifdef CONFIG_FILS
268 	} else if (sel & WPA_KEY_MGMT_FILS_SHA384) {
269 		key_mgmt = WPA_KEY_MGMT_FILS_SHA384;
270 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT FILS-SHA384");
271 	} else if (sel & WPA_KEY_MGMT_FILS_SHA256) {
272 		key_mgmt = WPA_KEY_MGMT_FILS_SHA256;
273 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT FILS-SHA256");
274 #endif /* CONFIG_FILS */
275 #ifdef CONFIG_IEEE80211R
276 	} else if ((sel & WPA_KEY_MGMT_FT_IEEE8021X) &&
277 		   os_strcmp(wpa_supplicant_get_eap_mode(wpa_s), "LEAP") != 0) {
278 		key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
279 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT FT/802.1X");
280 		if (ssid && !ssid->ft_eap_pmksa_caching &&
281 		    pmksa_cache_get_current(wpa_s->wpa)) {
282 			/* PMKSA caching with FT may have interoperability
283 			 * issues, so disable that case by default for now.
284 			 */
285 			wpa_printf(MSG_DEBUG,
286 				   "PASN: Disable PMKSA caching for FT/802.1X connection");
287 			pmksa_cache_clear_current(wpa_s->wpa);
288 		}
289 	} else if (sel & WPA_KEY_MGMT_FT_PSK) {
290 		key_mgmt = WPA_KEY_MGMT_FT_PSK;
291 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT FT/PSK");
292 #endif /* CONFIG_IEEE80211R */
293 	} else if (sel & WPA_KEY_MGMT_PASN) {
294 		key_mgmt = WPA_KEY_MGMT_PASN;
295 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT PASN");
296 	} else {
297 		wpa_printf(MSG_DEBUG, "PASN: invalid AKMP");
298 		return -1;
299 	}
300 
301 	peer->akmp = key_mgmt;
302 	peer->cipher = pairwise_cipher;
303 	peer->network_id = network_id;
304 	peer->group = group;
305 	return 0;
306 }
307 
308 
wpas_pasn_set_keys_from_cache(struct wpa_supplicant * wpa_s,const u8 * own_addr,const u8 * peer_addr,int cipher,int akmp)309 static int wpas_pasn_set_keys_from_cache(struct wpa_supplicant *wpa_s,
310 					 const u8 *own_addr,
311 					 const u8 *peer_addr,
312 					 int cipher, int akmp)
313 {
314 	struct ptksa_cache_entry *entry;
315 
316 	entry = ptksa_cache_get(wpa_s->ptksa, peer_addr, cipher);
317 	if (!entry) {
318 		wpa_printf(MSG_DEBUG, "PASN: peer " MACSTR
319 			   " not present in PTKSA cache", MAC2STR(peer_addr));
320 		return -1;
321 	}
322 
323 	if (!ether_addr_equal(entry->own_addr, own_addr)) {
324 		wpa_printf(MSG_DEBUG,
325 			   "PASN: own addr " MACSTR " and PTKSA entry own addr "
326 			   MACSTR " differ",
327 			   MAC2STR(own_addr), MAC2STR(entry->own_addr));
328 		return -1;
329 	}
330 
331 	wpa_printf(MSG_DEBUG, "PASN: " MACSTR " present in PTKSA cache",
332 		   MAC2STR(peer_addr));
333 	wpa_drv_set_secure_ranging_ctx(wpa_s, own_addr, peer_addr, cipher,
334 				       entry->ptk.tk_len,
335 				       entry->ptk.tk,
336 				       entry->ptk.ltf_keyseed_len,
337 				       entry->ptk.ltf_keyseed, 0);
338 	return 0;
339 }
340 
341 
wpas_pasn_configure_next_peer(struct wpa_supplicant * wpa_s,struct pasn_auth * pasn_params)342 static void wpas_pasn_configure_next_peer(struct wpa_supplicant *wpa_s,
343 					  struct pasn_auth *pasn_params)
344 {
345 	struct pasn_peer *peer;
346 	u8 comeback_len = 0;
347 	const u8 *comeback = NULL;
348 
349 	if (!pasn_params)
350 		return;
351 
352 	while (wpa_s->pasn_count < pasn_params->num_peers) {
353 		peer = &pasn_params->peer[wpa_s->pasn_count];
354 
355 		if (ether_addr_equal(wpa_s->bssid, peer->peer_addr)) {
356 			wpa_printf(MSG_DEBUG,
357 				   "PASN: Associated peer is not expected");
358 			peer->status = PASN_STATUS_FAILURE;
359 			wpa_s->pasn_count++;
360 			continue;
361 		}
362 
363 		if (wpas_pasn_set_keys_from_cache(wpa_s, peer->own_addr,
364 						  peer->peer_addr,
365 						  peer->cipher,
366 						  peer->akmp) == 0) {
367 			peer->status = PASN_STATUS_SUCCESS;
368 			wpa_s->pasn_count++;
369 			continue;
370 		}
371 
372 		if (wpas_pasn_get_params_from_bss(wpa_s, peer)) {
373 			peer->status = PASN_STATUS_FAILURE;
374 			wpa_s->pasn_count++;
375 			continue;
376 		}
377 
378 		if (wpas_pasn_auth_start(wpa_s, peer->own_addr,
379 					 peer->peer_addr, peer->akmp,
380 					 peer->cipher, peer->group,
381 					 peer->network_id,
382 					 comeback, comeback_len)) {
383 			peer->status = PASN_STATUS_FAILURE;
384 			wpa_s->pasn_count++;
385 			continue;
386 		}
387 		wpa_printf(MSG_DEBUG, "PASN: Sent PASN auth start for " MACSTR,
388 			   MAC2STR(peer->peer_addr));
389 		return;
390 	}
391 
392 	if (wpa_s->pasn_count == pasn_params->num_peers) {
393 		wpa_drv_send_pasn_resp(wpa_s, pasn_params);
394 		wpa_printf(MSG_DEBUG, "PASN: Response sent");
395 		os_free(wpa_s->pasn_params);
396 		wpa_s->pasn_params = NULL;
397 	}
398 }
399 
400 
wpas_pasn_auth_work_done(struct wpa_supplicant * wpa_s,int status)401 void wpas_pasn_auth_work_done(struct wpa_supplicant *wpa_s, int status)
402 {
403 	if (!wpa_s->pasn_params)
404 		return;
405 
406 	wpa_s->pasn_params->peer[wpa_s->pasn_count].status = status;
407 	wpa_s->pasn_count++;
408 	wpas_pasn_configure_next_peer(wpa_s, wpa_s->pasn_params);
409 }
410 
411 
wpas_pasn_delete_peers(struct wpa_supplicant * wpa_s,struct pasn_auth * pasn_params)412 static void wpas_pasn_delete_peers(struct wpa_supplicant *wpa_s,
413 				   struct pasn_auth *pasn_params)
414 {
415 	struct pasn_peer *peer;
416 	unsigned int i;
417 
418 	if (!pasn_params)
419 		return;
420 
421 	for (i = 0; i < pasn_params->num_peers; i++) {
422 		peer = &pasn_params->peer[i];
423 		ptksa_cache_flush(wpa_s->ptksa, peer->peer_addr,
424 				  WPA_CIPHER_NONE);
425 	}
426 }
427 
428 
429 #ifdef CONFIG_FILS
wpas_pasn_initiate_eapol(struct pasn_data * pasn,struct wpa_ssid * ssid)430 static void wpas_pasn_initiate_eapol(struct pasn_data *pasn,
431 				     struct wpa_ssid *ssid)
432 {
433 	struct eapol_config eapol_conf;
434 
435 	wpa_printf(MSG_DEBUG, "PASN: FILS: Initiating EAPOL");
436 
437 	eapol_sm_notify_eap_success(pasn->eapol, false);
438 	eapol_sm_notify_eap_fail(pasn->eapol, false);
439 	eapol_sm_notify_portControl(pasn->eapol, Auto);
440 
441 	os_memset(&eapol_conf, 0, sizeof(eapol_conf));
442 	eapol_conf.fast_reauth = pasn->fast_reauth;
443 	eapol_conf.workaround = ssid->eap_workaround;
444 
445 	eapol_sm_notify_config(pasn->eapol, &ssid->eap, &eapol_conf);
446 }
447 #endif /* CONFIG_FILS */
448 
449 
wpas_pasn_reset(struct wpa_supplicant * wpa_s)450 static void wpas_pasn_reset(struct wpa_supplicant *wpa_s)
451 {
452 	struct pasn_data *pasn = &wpa_s->pasn;
453 
454 	wpas_pasn_cancel_auth_work(wpa_s);
455 	wpa_s->pasn_auth_work = NULL;
456 	eloop_cancel_timeout(wpas_pasn_auth_work_timeout, wpa_s, NULL);
457 
458 	wpa_pasn_reset(pasn);
459 }
460 
461 
wpas_pasn_allowed(struct wpa_supplicant * wpa_s,const u8 * peer_addr,int akmp,int cipher)462 static struct wpa_bss * wpas_pasn_allowed(struct wpa_supplicant *wpa_s,
463 					  const u8 *peer_addr, int akmp,
464 					  int cipher)
465 {
466 	struct wpa_bss *bss;
467 	const u8 *rsne;
468 	struct wpa_ie_data rsne_data;
469 	int ret;
470 
471 	if (ether_addr_equal(wpa_s->bssid, peer_addr)) {
472 		wpa_printf(MSG_DEBUG,
473 			   "PASN: Not doing authentication with current BSS");
474 		return NULL;
475 	}
476 
477 	bss = wpa_bss_get_bssid_latest(wpa_s, peer_addr);
478 	if (!bss) {
479 		wpa_printf(MSG_DEBUG, "PASN: BSS not found");
480 		return NULL;
481 	}
482 
483 	rsne = wpa_bss_get_rsne(wpa_s, bss, NULL, false);
484 	if (!rsne) {
485 		wpa_printf(MSG_DEBUG, "PASN: BSS without RSNE");
486 		return NULL;
487 	}
488 
489 	ret = wpa_parse_wpa_ie(rsne, *(rsne + 1) + 2, &rsne_data);
490 	if (ret) {
491 		wpa_printf(MSG_DEBUG, "PASN: Failed parsing RSNE data");
492 		return NULL;
493 	}
494 
495 	if (!(rsne_data.key_mgmt & akmp) ||
496 	    !(rsne_data.pairwise_cipher & cipher)) {
497 		wpa_printf(MSG_DEBUG,
498 			   "PASN: AP does not support requested AKMP or cipher");
499 		return NULL;
500 	}
501 
502 	return bss;
503 }
504 
505 
wpas_pasn_auth_start_cb(struct wpa_radio_work * work,int deinit)506 static void wpas_pasn_auth_start_cb(struct wpa_radio_work *work, int deinit)
507 {
508 	struct wpa_supplicant *wpa_s = work->wpa_s;
509 	struct wpa_pasn_auth_work *awork = work->ctx;
510 	struct pasn_data *pasn = &wpa_s->pasn;
511 	struct wpa_ssid *ssid;
512 	struct wpa_bss *bss;
513 	const u8 *rsne, *rsnxe;
514 #ifdef CONFIG_FILS
515 	const u8 *indic;
516 	u16 fils_info;
517 #endif /* CONFIG_FILS */
518 	u16 capab = 0;
519 	bool derive_kdk;
520 	int ret;
521 
522 	wpa_printf(MSG_DEBUG, "PASN: auth_start_cb: deinit=%d", deinit);
523 
524 	if (deinit) {
525 		if (work->started) {
526 			eloop_cancel_timeout(wpas_pasn_auth_work_timeout,
527 					     wpa_s, NULL);
528 			wpa_s->pasn_auth_work = NULL;
529 		}
530 
531 		wpas_pasn_free_auth_work(awork);
532 		return;
533 	}
534 
535 	/*
536 	 * It is possible that by the time the callback is called, the PASN
537 	 * authentication is not allowed, e.g., a connection with the AP was
538 	 * established.
539 	 */
540 	bss = wpas_pasn_allowed(wpa_s, awork->peer_addr, awork->akmp,
541 				awork->cipher);
542 	if (!bss) {
543 		wpa_printf(MSG_DEBUG, "PASN: auth_start_cb: Not allowed");
544 		goto fail;
545 	}
546 
547 	rsne = wpa_bss_get_rsne(wpa_s, bss, NULL, false);
548 	if (!rsne) {
549 		wpa_printf(MSG_DEBUG, "PASN: BSS without RSNE");
550 		goto fail;
551 	}
552 
553 	rsnxe = wpa_bss_get_rsnxe(wpa_s, bss, NULL, false);
554 
555 	derive_kdk = (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_STA) &&
556 		ieee802_11_rsnx_capab(rsnxe,
557 				      WLAN_RSNX_CAPAB_SECURE_LTF);
558 #ifdef CONFIG_TESTING_OPTIONS
559 	if (!derive_kdk)
560 		derive_kdk = wpa_s->conf->force_kdk_derivation;
561 #endif /* CONFIG_TESTING_OPTIONS */
562 	if (derive_kdk)
563 		pasn_enable_kdk_derivation(pasn);
564 	else
565 		pasn_disable_kdk_derivation(pasn);
566 
567 	wpa_printf(MSG_DEBUG, "PASN: kdk_len=%zu", pasn->kdk_len);
568 
569 	if ((wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_STA) &&
570 	    ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF))
571 		pasn->secure_ltf = true;
572 	else
573 		pasn->secure_ltf = false;
574 
575 #ifdef CONFIG_TESTING_OPTIONS
576 	pasn->corrupt_mic = wpa_s->conf->pasn_corrupt_mic;
577 #endif /* CONFIG_TESTING_OPTIONS */
578 
579 	capab |= BIT(WLAN_RSNX_CAPAB_SAE_H2E);
580 	if (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_STA)
581 		capab |= BIT(WLAN_RSNX_CAPAB_SECURE_LTF);
582 	if (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_RTT_STA)
583 		capab |= BIT(WLAN_RSNX_CAPAB_SECURE_RTT);
584 	if (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_STA)
585 		capab |= BIT(WLAN_RSNX_CAPAB_URNM_MFPR);
586 	if ((wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SPP_AMSDU) &&
587 	    ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SPP_A_MSDU))
588 		capab |= BIT(WLAN_RSNX_CAPAB_SPP_A_MSDU);
589 
590 	pasn_set_rsnxe_caps(pasn, capab);
591 	pasn_register_callbacks(pasn, wpa_s, wpas_pasn_send_mlme, NULL);
592 	ssid = wpa_config_get_network(wpa_s->conf, awork->network_id);
593 
594 #ifdef CONFIG_SAE
595 	if (awork->akmp == WPA_KEY_MGMT_SAE) {
596 		if (!ssid) {
597 			wpa_printf(MSG_DEBUG,
598 				   "PASN: No network profile found for SAE");
599 			goto fail;
600 		}
601 		pasn_set_pt(pasn, wpas_pasn_sae_derive_pt(ssid, awork->group));
602 		if (!pasn->pt) {
603 			wpa_printf(MSG_DEBUG, "PASN: Failed to derive PT");
604 			goto fail;
605 		}
606 		pasn->network_id = ssid->id;
607 	}
608 #endif /* CONFIG_SAE */
609 
610 #ifdef CONFIG_FILS
611 	/* Prepare needed information for wpas_pasn_wd_fils_auth(). */
612 	if (awork->akmp == WPA_KEY_MGMT_FILS_SHA256 ||
613 	    awork->akmp == WPA_KEY_MGMT_FILS_SHA384) {
614 		indic = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
615 		if (!ssid) {
616 			wpa_printf(MSG_DEBUG, "PASN: FILS: No network block");
617 		} else if (!indic || indic[1] < 2) {
618 			wpa_printf(MSG_DEBUG,
619 				   "PASN: Missing FILS Indication IE");
620 		} else {
621 			fils_info = WPA_GET_LE16(indic + 2);
622 			if ((fils_info & BIT(9)) && ssid) {
623 				pasn->eapol = wpa_s->eapol;
624 				pasn->network_id = ssid->id;
625 				wpas_pasn_initiate_eapol(pasn, ssid);
626 				pasn->fils_eapol = true;
627 			} else {
628 				wpa_printf(MSG_DEBUG,
629 					   "PASN: FILS auth without PFS not supported");
630 			}
631 		}
632 		pasn->fast_reauth = wpa_s->conf->fast_reauth;
633 	}
634 #endif /* CONFIG_FILS */
635 
636 	pasn_set_initiator_pmksa(pasn, wpa_sm_get_pmksa_cache(wpa_s->wpa));
637 
638 	if (wpa_key_mgmt_ft(awork->akmp)) {
639 #ifdef CONFIG_IEEE80211R
640 		ret = wpa_pasn_ft_derive_pmk_r1(wpa_s->wpa, awork->akmp,
641 						awork->peer_addr,
642 						pasn->pmk_r1,
643 						&pasn->pmk_r1_len,
644 						pasn->pmk_r1_name);
645 		if (ret) {
646 			wpa_printf(MSG_DEBUG,
647 				   "PASN: FT: Failed to derive keys");
648 			goto fail;
649 		}
650 #else /* CONFIG_IEEE80211R */
651 		goto fail;
652 #endif /* CONFIG_IEEE80211R */
653 	}
654 
655 
656 	ret = wpas_pasn_start(pasn, awork->own_addr, awork->peer_addr,
657 			      awork->peer_addr, awork->akmp, awork->cipher,
658 			      awork->group, bss->freq, rsne, *(rsne + 1) + 2,
659 			      rsnxe, rsnxe ? *(rsnxe + 1) + 2 : 0,
660 			      awork->comeback);
661 	if (ret) {
662 		wpa_printf(MSG_DEBUG,
663 			   "PASN: Failed to start PASN authentication");
664 		goto fail;
665 	}
666 	eloop_register_timeout(2, 0, wpas_pasn_auth_work_timeout, wpa_s, NULL);
667 
668 	/* comeback token is no longer needed at this stage */
669 	wpabuf_free(awork->comeback);
670 	awork->comeback = NULL;
671 
672 	wpa_s->pasn_auth_work = work;
673 	return;
674 fail:
675 	wpas_pasn_free_auth_work(awork);
676 	work->ctx = NULL;
677 	radio_work_done(work);
678 }
679 
680 
wpas_pasn_auth_start(struct wpa_supplicant * wpa_s,const u8 * own_addr,const u8 * peer_addr,int akmp,int cipher,u16 group,int network_id,const u8 * comeback,size_t comeback_len)681 int wpas_pasn_auth_start(struct wpa_supplicant *wpa_s,
682 			 const u8 *own_addr, const u8 *peer_addr,
683 			 int akmp, int cipher, u16 group, int network_id,
684 			 const u8 *comeback, size_t comeback_len)
685 {
686 	struct wpa_pasn_auth_work *awork;
687 	struct wpa_bss *bss;
688 
689 	wpa_printf(MSG_DEBUG, "PASN: Start: " MACSTR " akmp=0x%x, cipher=0x%x",
690 		   MAC2STR(peer_addr), akmp, cipher);
691 
692 	/*
693 	 * TODO: Consider modifying the offchannel logic to handle additional
694 	 * Management frames other then Action frames. For now allow PASN only
695 	 * with drivers that support off-channel TX.
696 	 */
697 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX)) {
698 		wpa_printf(MSG_DEBUG,
699 			   "PASN: Driver does not support offchannel TX");
700 		return -1;
701 	}
702 
703 	if (radio_work_pending(wpa_s, "pasn-start-auth")) {
704 		wpa_printf(MSG_DEBUG,
705 			   "PASN: send_auth: Work is already pending");
706 		return -1;
707 	}
708 
709 	if (wpa_s->pasn_auth_work) {
710 		wpa_printf(MSG_DEBUG, "PASN: send_auth: Already in progress");
711 		return -1;
712 	}
713 
714 	bss = wpas_pasn_allowed(wpa_s, peer_addr, akmp, cipher);
715 	if (!bss)
716 		return -1;
717 
718 	wpas_pasn_reset(wpa_s);
719 
720 	awork = os_zalloc(sizeof(*awork));
721 	if (!awork)
722 		return -1;
723 
724 	os_memcpy(awork->own_addr, own_addr, ETH_ALEN);
725 	os_memcpy(awork->peer_addr, peer_addr, ETH_ALEN);
726 	awork->akmp = akmp;
727 	awork->cipher = cipher;
728 	awork->group = group;
729 	awork->network_id = network_id;
730 
731 	if (comeback && comeback_len) {
732 		awork->comeback = wpabuf_alloc_copy(comeback, comeback_len);
733 		if (!awork->comeback) {
734 			wpas_pasn_free_auth_work(awork);
735 			return -1;
736 		}
737 	}
738 
739 	if (radio_add_work(wpa_s, bss->freq, "pasn-start-auth", 1,
740 			   wpas_pasn_auth_start_cb, awork) < 0) {
741 		wpas_pasn_free_auth_work(awork);
742 		return -1;
743 	}
744 
745 	wpa_printf(MSG_DEBUG, "PASN: Auth work successfully added");
746 	return 0;
747 }
748 
749 
wpas_pasn_auth_stop(struct wpa_supplicant * wpa_s)750 void wpas_pasn_auth_stop(struct wpa_supplicant *wpa_s)
751 {
752 	struct pasn_data *pasn = &wpa_s->pasn;
753 
754 	if (!wpa_s->pasn.ecdh)
755 		return;
756 
757 	wpa_printf(MSG_DEBUG, "PASN: Stopping authentication");
758 
759 	wpas_pasn_auth_status(wpa_s, pasn->peer_addr, pasn_get_akmp(pasn),
760 			      pasn_get_cipher(pasn),
761 			      pasn->status, pasn->comeback,
762 			      pasn->comeback_after);
763 
764 	wpas_pasn_reset(wpa_s);
765 }
766 
767 
wpas_pasn_immediate_retry(struct wpa_supplicant * wpa_s,struct pasn_data * pasn,struct wpa_pasn_params_data * params)768 static int wpas_pasn_immediate_retry(struct wpa_supplicant *wpa_s,
769 				     struct pasn_data *pasn,
770 				     struct wpa_pasn_params_data *params)
771 {
772 	int akmp = pasn_get_akmp(pasn);
773 	int cipher = pasn_get_cipher(pasn);
774 	u16 group = pasn->group;
775 	u8 own_addr[ETH_ALEN];
776 	u8 peer_addr[ETH_ALEN];
777 
778 	wpa_printf(MSG_DEBUG, "PASN: Immediate retry");
779 	os_memcpy(own_addr, pasn->own_addr, ETH_ALEN);
780 	os_memcpy(peer_addr, pasn->peer_addr, ETH_ALEN);
781 	wpas_pasn_reset(wpa_s);
782 
783 	return wpas_pasn_auth_start(wpa_s, own_addr, peer_addr, akmp, cipher,
784 				    group, pasn->network_id,
785 				    params->comeback, params->comeback_len);
786 }
787 
788 
wpas_pasn_deauth_cb(struct ptksa_cache_entry * entry)789 static void wpas_pasn_deauth_cb(struct ptksa_cache_entry *entry)
790 {
791 	struct wpa_supplicant *wpa_s = entry->ctx;
792 	u8 own_addr[ETH_ALEN];
793 	u8 peer_addr[ETH_ALEN];
794 
795 	/* Use a copy of the addresses from the entry to avoid issues with the
796 	 * entry getting freed during deauthentication processing. */
797 	os_memcpy(own_addr, entry->own_addr, ETH_ALEN);
798 	os_memcpy(peer_addr, entry->addr, ETH_ALEN);
799 	wpas_pasn_deauthenticate(wpa_s, own_addr, peer_addr);
800 }
801 
802 
wpas_pasn_auth_rx(struct wpa_supplicant * wpa_s,const struct ieee80211_mgmt * mgmt,size_t len)803 int wpas_pasn_auth_rx(struct wpa_supplicant *wpa_s,
804 		      const struct ieee80211_mgmt *mgmt, size_t len)
805 {
806 	struct pasn_data *pasn = &wpa_s->pasn;
807 	struct wpa_pasn_params_data pasn_data;
808 	int ret;
809 
810 	if (!wpa_s->pasn_auth_work)
811 		return -2;
812 
813 	wpabuf_free(pasn->frame);
814 	pasn->frame = NULL;
815 
816 	pasn_register_callbacks(pasn, wpa_s, wpas_pasn_send_mlme, NULL);
817 	ret = wpa_pasn_auth_rx(pasn, (const u8 *) mgmt, len, &pasn_data);
818 	if (ret == 0) {
819 		ptksa_cache_add(wpa_s->ptksa, pasn->own_addr, pasn->peer_addr,
820 				pasn_get_cipher(pasn),
821 				dot11RSNAConfigPMKLifetime,
822 				pasn_get_ptk(pasn),
823 				wpa_s->pasn_params ? wpas_pasn_deauth_cb : NULL,
824 				wpa_s->pasn_params ? wpa_s : NULL,
825 				pasn_get_akmp(pasn));
826 
827 		if (pasn->pmksa_entry)
828 			wpa_sm_set_cur_pmksa(wpa_s->wpa, pasn->pmksa_entry);
829 	}
830 
831 	forced_memzero(pasn_get_ptk(pasn), sizeof(pasn->ptk));
832 
833 	if (ret == -1) {
834 		wpas_pasn_auth_stop(wpa_s);
835 		wpas_pasn_auth_work_done(wpa_s, PASN_STATUS_FAILURE);
836 	}
837 
838 	if (ret == 1)
839 		ret = wpas_pasn_immediate_retry(wpa_s, pasn, &pasn_data);
840 
841 	return ret;
842 }
843 
844 
wpas_pasn_auth_trigger(struct wpa_supplicant * wpa_s,struct pasn_auth * pasn_auth)845 void wpas_pasn_auth_trigger(struct wpa_supplicant *wpa_s,
846 			    struct pasn_auth *pasn_auth)
847 {
848 	struct pasn_peer *src, *dst;
849 	unsigned int i, num_peers = pasn_auth->num_peers;
850 
851 	if (wpa_s->pasn_params) {
852 		wpa_printf(MSG_DEBUG,
853 			   "PASN: auth_trigger: Already in progress");
854 		return;
855 	}
856 
857 	if (!num_peers || num_peers > WPAS_MAX_PASN_PEERS) {
858 		wpa_printf(MSG_DEBUG,
859 			   "PASN: auth trigger: Invalid number of peers");
860 		return;
861 	}
862 
863 	wpa_s->pasn_params = os_zalloc(sizeof(struct pasn_auth));
864 	if (!wpa_s->pasn_params) {
865 		wpa_printf(MSG_DEBUG,
866 			   "PASN: auth trigger: Failed to allocate a buffer");
867 		return;
868 	}
869 
870 	wpa_s->pasn_count = 0;
871 	wpa_s->pasn_params->num_peers = num_peers;
872 
873 	for (i = 0; i < num_peers; i++) {
874 		dst = &wpa_s->pasn_params->peer[i];
875 		src = &pasn_auth->peer[i];
876 		os_memcpy(dst->own_addr, wpa_s->own_addr, ETH_ALEN);
877 		os_memcpy(dst->peer_addr, src->peer_addr, ETH_ALEN);
878 		dst->ltf_keyseed_required = src->ltf_keyseed_required;
879 		dst->status = PASN_STATUS_SUCCESS;
880 
881 		if (!is_zero_ether_addr(src->own_addr)) {
882 			os_memcpy(dst->own_addr, src->own_addr, ETH_ALEN);
883 			wpa_printf(MSG_DEBUG, "PASN: Own (source) MAC addr: "
884 				   MACSTR, MAC2STR(dst->own_addr));
885 		}
886 	}
887 
888 	if (pasn_auth->action == PASN_ACTION_DELETE_SECURE_RANGING_CONTEXT) {
889 		wpas_pasn_delete_peers(wpa_s, wpa_s->pasn_params);
890 		os_free(wpa_s->pasn_params);
891 		wpa_s->pasn_params = NULL;
892 	} else if (pasn_auth->action == PASN_ACTION_AUTH) {
893 		wpas_pasn_configure_next_peer(wpa_s, wpa_s->pasn_params);
894 	}
895 }
896 
897 
wpas_pasn_auth_tx_status(struct wpa_supplicant * wpa_s,const u8 * data,size_t data_len,u8 acked)898 int wpas_pasn_auth_tx_status(struct wpa_supplicant *wpa_s,
899 			     const u8 *data, size_t data_len, u8 acked)
900 
901 {
902 	struct pasn_data *pasn = &wpa_s->pasn;
903 	int ret;
904 
905 	if (!wpa_s->pasn_auth_work) {
906 		wpa_printf(MSG_DEBUG,
907 			   "PASN: auth_tx_status: no work in progress");
908 		return -1;
909 	}
910 
911 	ret = wpa_pasn_auth_tx_status(pasn, data, data_len, acked);
912 	if (ret != 1)
913 		return ret;
914 
915 	if (!wpa_s->pasn_params) {
916 		wpas_pasn_auth_stop(wpa_s);
917 		return 0;
918 	}
919 
920 	wpas_pasn_set_keys_from_cache(wpa_s, pasn->own_addr, pasn->peer_addr,
921 				      pasn_get_cipher(pasn),
922 				      pasn_get_akmp(pasn));
923 	wpas_pasn_auth_stop(wpa_s);
924 	wpas_pasn_auth_work_done(wpa_s, PASN_STATUS_SUCCESS);
925 
926 	return 0;
927 }
928 
929 
wpas_pasn_deauthenticate(struct wpa_supplicant * wpa_s,const u8 * own_addr,const u8 * peer_addr)930 int wpas_pasn_deauthenticate(struct wpa_supplicant *wpa_s, const u8 *own_addr,
931 			     const u8 *peer_addr)
932 {
933 	struct wpa_bss *bss;
934 	struct wpabuf *buf;
935 	struct ieee80211_mgmt *deauth;
936 	int ret;
937 
938 	if (ether_addr_equal(wpa_s->bssid, peer_addr)) {
939 		wpa_printf(MSG_DEBUG,
940 			   "PASN: Cannot deauthenticate from current BSS");
941 		return -1;
942 	}
943 
944 	wpa_drv_set_secure_ranging_ctx(wpa_s, own_addr, peer_addr, 0, 0, NULL,
945 				       0, NULL, 1);
946 
947 	wpa_printf(MSG_DEBUG, "PASN: deauth: Flushing all PTKSA entries for "
948 		   MACSTR, MAC2STR(peer_addr));
949 	ptksa_cache_flush(wpa_s->ptksa, peer_addr, WPA_CIPHER_NONE);
950 
951 	bss = wpa_bss_get_bssid(wpa_s, peer_addr);
952 	if (!bss) {
953 		wpa_printf(MSG_DEBUG, "PASN: deauth: BSS not found");
954 		return -1;
955 	}
956 
957 	buf = wpabuf_alloc(64);
958 	if (!buf) {
959 		wpa_printf(MSG_DEBUG, "PASN: deauth: Failed wpabuf allocate");
960 		return -1;
961 	}
962 
963 	deauth = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
964 					  u.deauth.variable));
965 
966 	deauth->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
967 					     (WLAN_FC_STYPE_DEAUTH << 4));
968 
969 	os_memcpy(deauth->da, peer_addr, ETH_ALEN);
970 	os_memcpy(deauth->sa, own_addr, ETH_ALEN);
971 	os_memcpy(deauth->bssid, peer_addr, ETH_ALEN);
972 	deauth->u.deauth.reason_code =
973 		host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
974 
975 	/*
976 	 * Since we do not expect any response from the AP, implement the
977 	 * Deauthentication frame transmission using direct call to the driver
978 	 * without a radio work.
979 	 */
980 	ret = wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
981 				bss->freq, 0);
982 
983 	wpabuf_free(buf);
984 	wpa_printf(MSG_DEBUG, "PASN: deauth: send_mlme ret=%d", ret);
985 
986 	return ret;
987 }
988