1 /*
2  * wpa_supplicant / WPS integration
3  * Copyright (c) 2008-2014, 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 "includes.h"
10 
11 #include "common.h"
12 #include "eloop.h"
13 #include "uuid.h"
14 #include "crypto/random.h"
15 #include "crypto/dh_group5.h"
16 #include "common/ieee802_11_defs.h"
17 #include "common/ieee802_11_common.h"
18 #include "common/wpa_common.h"
19 #include "common/wpa_ctrl.h"
20 #include "eap_common/eap_wsc_common.h"
21 #include "eap_peer/eap.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "rsn_supp/wpa.h"
24 #include "wps/wps_attr_parse.h"
25 #include "config.h"
26 #include "wpa_supplicant_i.h"
27 #include "driver_i.h"
28 #include "notify.h"
29 #include "bssid_ignore.h"
30 #include "bss.h"
31 #include "scan.h"
32 #include "ap.h"
33 #include "p2p/p2p.h"
34 #include "p2p_supplicant.h"
35 #include "wps_supplicant.h"
36 
37 
38 #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
39 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
40 #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
41 
42 /*
43  * The minimum time in seconds before trying to associate to a WPS PIN AP that
44  * does not have Selected Registrar TRUE.
45  */
46 #ifndef WPS_PIN_TIME_IGNORE_SEL_REG
47 #define WPS_PIN_TIME_IGNORE_SEL_REG 5
48 #endif /* WPS_PIN_TIME_IGNORE_SEL_REG */
49 
50 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
51 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
52 
53 
wpas_wps_clear_ap_info(struct wpa_supplicant * wpa_s)54 static void wpas_wps_clear_ap_info(struct wpa_supplicant *wpa_s)
55 {
56 	os_free(wpa_s->wps_ap);
57 	wpa_s->wps_ap = NULL;
58 	wpa_s->num_wps_ap = 0;
59 	wpa_s->wps_ap_iter = 0;
60 }
61 
62 
wpas_wps_assoc_with_cred(void * eloop_ctx,void * timeout_ctx)63 static void wpas_wps_assoc_with_cred(void *eloop_ctx, void *timeout_ctx)
64 {
65 	struct wpa_supplicant *wpa_s = eloop_ctx;
66 	int use_fast_assoc = timeout_ctx != NULL;
67 
68 	wpa_printf(MSG_DEBUG, "WPS: Continuing association after eapol_cb");
69 	if (!use_fast_assoc ||
70 	    wpa_supplicant_fast_associate(wpa_s) != 1)
71 		wpa_supplicant_req_scan(wpa_s, 0, 0);
72 }
73 
74 
wpas_wps_assoc_with_cred_cancel(struct wpa_supplicant * wpa_s)75 static void wpas_wps_assoc_with_cred_cancel(struct wpa_supplicant *wpa_s)
76 {
77 	eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 0);
78 	eloop_cancel_timeout(wpas_wps_assoc_with_cred, wpa_s, (void *) 1);
79 }
80 
81 
wpas_wps_eapol_cb(struct wpa_supplicant * wpa_s)82 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
83 {
84 	if (wpas_p2p_wps_eapol_cb(wpa_s) > 0)
85 		return 1;
86 
87 	if (!wpa_s->wps_success &&
88 	    wpa_s->current_ssid &&
89 	    eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
90 		const u8 *bssid = wpa_s->bssid;
91 		if (is_zero_ether_addr(bssid))
92 			bssid = wpa_s->pending_bssid;
93 
94 		wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
95 			   " did not succeed - continue trying to find "
96 			   "suitable AP", MAC2STR(bssid));
97 		wpa_bssid_ignore_add(wpa_s, bssid);
98 
99 		wpa_supplicant_deauthenticate(wpa_s,
100 					      WLAN_REASON_DEAUTH_LEAVING);
101 		wpa_s->reassociate = 1;
102 		wpa_supplicant_req_scan(wpa_s,
103 					wpa_s->bssid_ignore_cleared ? 5 : 0, 0);
104 		wpa_s->bssid_ignore_cleared = false;
105 		return 1;
106 	}
107 
108 	wpas_wps_clear_ap_info(wpa_s);
109 	eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
110 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
111 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
112 
113 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
114 	    !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
115 		int disabled = wpa_s->current_ssid->disabled;
116 		unsigned int freq = wpa_s->assoc_freq;
117 		struct wpa_bss *bss;
118 		struct wpa_ssid *ssid = NULL;
119 		int use_fast_assoc = 0;
120 
121 		wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
122 			   "try to associate with the received credential "
123 			   "(freq=%u)", freq);
124 		wpa_s->own_disconnect_req = 1;
125 		wpa_supplicant_deauthenticate(wpa_s,
126 					      WLAN_REASON_DEAUTH_LEAVING);
127 		if (disabled) {
128 			wpa_printf(MSG_DEBUG, "WPS: Current network is "
129 				   "disabled - wait for user to enable");
130 			return 1;
131 		}
132 		wpa_s->after_wps = 5;
133 		wpa_s->wps_freq = freq;
134 		wpa_s->normal_scans = 0;
135 		wpa_s->reassociate = 1;
136 
137 		wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
138 			   "without a new scan can be used");
139 		bss = wpa_supplicant_pick_network(wpa_s, &ssid);
140 		if (bss) {
141 			struct wpabuf *wps;
142 			struct wps_parse_attr attr;
143 
144 			wps = wpa_bss_get_vendor_ie_multi(bss,
145 							  WPS_IE_VENDOR_TYPE);
146 			if (wps && wps_parse_msg(wps, &attr) == 0 &&
147 			    attr.wps_state &&
148 			    *attr.wps_state == WPS_STATE_CONFIGURED)
149 				use_fast_assoc = 1;
150 			wpabuf_free(wps);
151 		}
152 
153 		/*
154 		 * Complete the next step from an eloop timeout to allow pending
155 		 * driver events related to the disconnection to be processed
156 		 * first. This makes it less likely for disconnection event to
157 		 * cause problems with the following connection.
158 		 */
159 		wpa_printf(MSG_DEBUG, "WPS: Continue association from timeout");
160 		wpas_wps_assoc_with_cred_cancel(wpa_s);
161 		eloop_register_timeout(0, 10000,
162 				       wpas_wps_assoc_with_cred, wpa_s,
163 				       use_fast_assoc ? (void *) 1 :
164 				       (void *) 0);
165 		return 1;
166 	}
167 
168 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
169 		wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
170 			   "for external credential processing");
171 		wpas_clear_wps(wpa_s);
172 		wpa_s->own_disconnect_req = 1;
173 		wpa_supplicant_deauthenticate(wpa_s,
174 					      WLAN_REASON_DEAUTH_LEAVING);
175 		return 1;
176 	}
177 
178 	return 0;
179 }
180 
181 
wpas_wps_security_workaround(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const struct wps_credential * cred)182 static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
183 					 struct wpa_ssid *ssid,
184 					 const struct wps_credential *cred)
185 {
186 	struct wpa_driver_capa capa;
187 	struct wpa_bss *bss;
188 	const u8 *ie;
189 	struct wpa_ie_data adv;
190 	int wpa2 = 0, ccmp = 0;
191 	enum wpa_driver_if_type iftype;
192 
193 	/*
194 	 * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
195 	 * case they are configured for mixed mode operation (WPA+WPA2 and
196 	 * TKIP+CCMP). Try to use scan results to figure out whether the AP
197 	 * actually supports stronger security and select that if the client
198 	 * has support for it, too.
199 	 */
200 
201 	if (wpa_drv_get_capa(wpa_s, &capa))
202 		return; /* Unknown what driver supports */
203 
204 	if (ssid->ssid == NULL)
205 		return;
206 	bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
207 	if (!bss)
208 		bss = wpa_bss_get(wpa_s, wpa_s->bssid,
209 				  ssid->ssid, ssid->ssid_len);
210 	if (bss == NULL) {
211 		wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
212 			   "table - use credential as-is");
213 		return;
214 	}
215 
216 	wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
217 
218 	ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
219 	if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
220 		wpa2 = 1;
221 		if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
222 			ccmp = 1;
223 	} else {
224 		ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
225 		if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
226 		    adv.pairwise_cipher & WPA_CIPHER_CCMP)
227 			ccmp = 1;
228 	}
229 
230 	if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
231 	    (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
232 		/*
233 		 * TODO: This could be the initial AP configuration and the
234 		 * Beacon contents could change shortly. Should request a new
235 		 * scan and delay addition of the network until the updated
236 		 * scan results are available.
237 		 */
238 		wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
239 			   "support - use credential as-is");
240 		return;
241 	}
242 
243 	iftype = ssid->p2p_group ? WPA_IF_P2P_CLIENT : WPA_IF_STATION;
244 
245 	if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
246 	    (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
247 	    (capa.key_mgmt_iftype[iftype] &
248 	     WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
249 		wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
250 			   "based on scan results");
251 		if (wpa_s->conf->ap_scan == 1)
252 			ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
253 		else
254 			ssid->pairwise_cipher = WPA_CIPHER_CCMP;
255 	}
256 
257 	if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
258 	    (ssid->proto & WPA_PROTO_WPA) &&
259 	    (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
260 		wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
261 			   "based on scan results");
262 		if (wpa_s->conf->ap_scan == 1)
263 			ssid->proto |= WPA_PROTO_RSN;
264 		else
265 			ssid->proto = WPA_PROTO_RSN;
266 	}
267 }
268 
269 
wpas_wps_remove_dup_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * new_ssid)270 static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
271 					struct wpa_ssid *new_ssid)
272 {
273 	struct wpa_ssid *ssid, *next;
274 
275 	for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
276 	     ssid = next, next = ssid ? ssid->next : NULL) {
277 		/*
278 		 * new_ssid has already been added to the list in
279 		 * wpas_wps_add_network(), so skip it.
280 		 */
281 		if (ssid == new_ssid)
282 			continue;
283 
284 		if (ssid->bssid_set || new_ssid->bssid_set) {
285 			if (ssid->bssid_set != new_ssid->bssid_set)
286 				continue;
287 			if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
288 			    0)
289 				continue;
290 		}
291 
292 		/* compare SSID */
293 		if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
294 			continue;
295 
296 		if (ssid->ssid && new_ssid->ssid) {
297 			if (os_memcmp(ssid->ssid, new_ssid->ssid,
298 				      ssid->ssid_len) != 0)
299 				continue;
300 		} else if (ssid->ssid || new_ssid->ssid)
301 			continue;
302 
303 		/* compare security parameters */
304 		if (ssid->auth_alg != new_ssid->auth_alg ||
305 		    ssid->key_mgmt != new_ssid->key_mgmt ||
306 		    (ssid->group_cipher != new_ssid->group_cipher &&
307 		     !(ssid->group_cipher & new_ssid->group_cipher &
308 		       WPA_CIPHER_CCMP)))
309 			continue;
310 
311 		/*
312 		 * Some existing WPS APs will send two creds in case they are
313 		 * configured for mixed mode operation (WPA+WPA2 and TKIP+CCMP).
314 		 * Try to merge these two creds if they are received in the same
315 		 * M8 message.
316 		 */
317 		if (ssid->wps_run && ssid->wps_run == new_ssid->wps_run &&
318 		    wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
319 			if (new_ssid->passphrase && ssid->passphrase &&
320 			    os_strcmp(new_ssid->passphrase, ssid->passphrase) !=
321 			    0) {
322 				wpa_printf(MSG_DEBUG,
323 					   "WPS: M8 Creds with different passphrase - do not merge");
324 				continue;
325 			}
326 
327 			if (new_ssid->psk_set &&
328 			    (!ssid->psk_set ||
329 			     os_memcmp(new_ssid->psk, ssid->psk, 32) != 0)) {
330 				wpa_printf(MSG_DEBUG,
331 					   "WPS: M8 Creds with different PSK - do not merge");
332 				continue;
333 			}
334 
335 			if ((new_ssid->passphrase && !ssid->passphrase) ||
336 			    (!new_ssid->passphrase && ssid->passphrase)) {
337 				wpa_printf(MSG_DEBUG,
338 					   "WPS: M8 Creds with different passphrase/PSK type - do not merge");
339 				continue;
340 			}
341 
342 			wpa_printf(MSG_DEBUG,
343 				   "WPS: Workaround - merge likely WPA/WPA2-mixed mode creds in same M8 message");
344 			new_ssid->proto |= ssid->proto;
345 			new_ssid->pairwise_cipher |= ssid->pairwise_cipher;
346 		} else {
347 			/*
348 			 * proto and pairwise_cipher difference matter for
349 			 * non-mixed-mode creds.
350 			 */
351 			if (ssid->proto != new_ssid->proto ||
352 			    ssid->pairwise_cipher != new_ssid->pairwise_cipher)
353 				continue;
354 		}
355 
356 		/* Remove the duplicated older network entry. */
357 		wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
358 		wpas_notify_network_removed(wpa_s, ssid);
359 		wpa_config_remove_network(wpa_s->conf, ssid->id);
360 	}
361 }
362 
363 
wpa_supplicant_wps_cred(void * ctx,const struct wps_credential * cred)364 static int wpa_supplicant_wps_cred(void *ctx,
365 				   const struct wps_credential *cred)
366 {
367 	struct wpa_supplicant *wpa_s = ctx;
368 	struct wpa_ssid *ssid = wpa_s->current_ssid;
369 	u16 auth_type;
370 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
371 	int registrar = 0;
372 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
373 	bool add_sae;
374 
375 	if ((wpa_s->conf->wps_cred_processing == 1 ||
376 	     wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
377 		size_t blen = cred->cred_attr_len * 2 + 1;
378 		char *buf = os_malloc(blen);
379 		if (buf) {
380 			wpa_snprintf_hex(buf, blen,
381 					 cred->cred_attr, cred->cred_attr_len);
382 			wpa_msg(wpa_s, MSG_INFO, "%s%s",
383 				WPS_EVENT_CRED_RECEIVED, buf);
384 			os_free(buf);
385 		}
386 
387 		wpas_notify_wps_credential(wpa_s, cred);
388 	} else
389 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
390 
391 	wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
392 			cred->cred_attr, cred->cred_attr_len);
393 
394 	if (wpa_s->conf->wps_cred_processing == 1)
395 		return 0;
396 
397 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
398 	wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
399 		   cred->auth_type);
400 	wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
401 	wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
402 	wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
403 			cred->key, cred->key_len);
404 	wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
405 		   MAC2STR(cred->mac_addr));
406 
407 	auth_type = cred->auth_type;
408 	if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
409 		wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
410 			   "auth_type into WPA2PSK");
411 		auth_type = WPS_AUTH_WPA2PSK;
412 	}
413 
414 	if (auth_type != WPS_AUTH_OPEN &&
415 	    auth_type != WPS_AUTH_WPAPSK &&
416 	    auth_type != WPS_AUTH_WPA2PSK) {
417 		wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
418 			   "unsupported authentication type 0x%x",
419 			   auth_type);
420 		return 0;
421 	}
422 
423 	if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
424 		if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
425 			wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
426 				   "invalid Network Key length %lu",
427 				   (unsigned long) cred->key_len);
428 			return -1;
429 		}
430 	}
431 
432 	if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
433 		wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
434 			   "on the received credential");
435 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
436 		if (ssid->eap.identity &&
437 		    ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
438 		    os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
439 			      WSC_ID_REGISTRAR_LEN) == 0)
440 			registrar = 1;
441 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
442 		os_free(ssid->eap.identity);
443 		ssid->eap.identity = NULL;
444 		ssid->eap.identity_len = 0;
445 		os_free(ssid->eap.phase1);
446 		ssid->eap.phase1 = NULL;
447 		os_free(ssid->eap.eap_methods);
448 		ssid->eap.eap_methods = NULL;
449 		if (!ssid->p2p_group) {
450 			ssid->temporary = 0;
451 			ssid->bssid_set = 0;
452 		}
453 		ssid->disabled_until.sec = 0;
454 		ssid->disabled_until.usec = 0;
455 		ssid->auth_failures = 0;
456 	} else {
457 		wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
458 			   "received credential");
459 		ssid = wpa_config_add_network(wpa_s->conf);
460 		if (ssid == NULL)
461 			return -1;
462 		if (wpa_s->current_ssid) {
463 			/*
464 			 * Should the GO issue multiple credentials for some
465 			 * reason, each credential should be marked as a
466 			 * temporary P2P group similarly to the one that gets
467 			 * marked as such based on the pre-configured values
468 			 * used for the WPS network block.
469 			 */
470 			ssid->p2p_group = wpa_s->current_ssid->p2p_group;
471 			ssid->temporary = wpa_s->current_ssid->temporary;
472 		}
473 		wpas_notify_network_added(wpa_s, ssid);
474 	}
475 
476 	wpa_config_set_network_defaults(ssid);
477 	ssid->wps_run = wpa_s->wps_run;
478 
479 	os_free(ssid->ssid);
480 	ssid->ssid = os_malloc(cred->ssid_len);
481 	if (ssid->ssid) {
482 		os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
483 		ssid->ssid_len = cred->ssid_len;
484 	}
485 
486 	switch (cred->encr_type) {
487 	case WPS_ENCR_NONE:
488 		break;
489 	case WPS_ENCR_TKIP:
490 		ssid->pairwise_cipher = WPA_CIPHER_TKIP | WPA_CIPHER_CCMP;
491 		break;
492 	case WPS_ENCR_AES:
493 		ssid->pairwise_cipher = WPA_CIPHER_CCMP;
494 		if (wpa_s->drv_capa_known &&
495 		    (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP)) {
496 			ssid->pairwise_cipher |= WPA_CIPHER_GCMP;
497 			ssid->group_cipher |= WPA_CIPHER_GCMP;
498 		}
499 		if (wpa_s->drv_capa_known &&
500 		    (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP_256)) {
501 			ssid->pairwise_cipher |= WPA_CIPHER_GCMP_256;
502 			ssid->group_cipher |= WPA_CIPHER_GCMP_256;
503 		}
504 		if (wpa_s->drv_capa_known &&
505 		    (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_CCMP_256)) {
506 			ssid->pairwise_cipher |= WPA_CIPHER_CCMP_256;
507 			ssid->group_cipher |= WPA_CIPHER_CCMP_256;
508 		}
509 		break;
510 	}
511 
512 	switch (auth_type) {
513 	case WPS_AUTH_OPEN:
514 		ssid->auth_alg = WPA_AUTH_ALG_OPEN;
515 		ssid->key_mgmt = WPA_KEY_MGMT_NONE;
516 		ssid->proto = 0;
517 #ifdef CONFIG_WPS_REG_DISABLE_OPEN
518 		if (registrar) {
519 			wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
520 				"id=%d - Credentials for an open "
521 				"network disabled by default - use "
522 				"'select_network %d' to enable",
523 				ssid->id, ssid->id);
524 			ssid->disabled = 1;
525 		}
526 #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
527 		break;
528 	case WPS_AUTH_WPAPSK:
529 		ssid->auth_alg = WPA_AUTH_ALG_OPEN;
530 		ssid->key_mgmt = WPA_KEY_MGMT_PSK;
531 		ssid->proto = WPA_PROTO_WPA | WPA_PROTO_RSN;
532 		break;
533 	case WPS_AUTH_WPA2PSK:
534 		ssid->auth_alg = WPA_AUTH_ALG_OPEN;
535 		ssid->key_mgmt = WPA_KEY_MGMT_PSK;
536 		add_sae = wpa_s->conf->wps_cred_add_sae;
537 #ifdef CONFIG_P2P
538 		if (ssid->p2p_group && is_p2p_6ghz_capable(wpa_s->global->p2p))
539 			add_sae = true;
540 #endif /* CONFIG_P2P */
541 		if (add_sae && cred->key_len != 2 * PMK_LEN) {
542 			ssid->auth_alg = 0;
543 			ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
544 			ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
545 		}
546 		ssid->proto = WPA_PROTO_RSN;
547 		break;
548 	}
549 
550 	if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
551 		if (cred->key_len == 2 * PMK_LEN) {
552 			if (hexstr2bin((const char *) cred->key, ssid->psk,
553 				       PMK_LEN)) {
554 				wpa_printf(MSG_ERROR, "WPS: Invalid Network "
555 					   "Key");
556 				return -1;
557 			}
558 			ssid->psk_set = 1;
559 			ssid->export_keys = 1;
560 		} else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
561 			os_free(ssid->passphrase);
562 			ssid->passphrase = os_malloc(cred->key_len + 1);
563 			if (ssid->passphrase == NULL)
564 				return -1;
565 			os_memcpy(ssid->passphrase, cred->key, cred->key_len);
566 			ssid->passphrase[cred->key_len] = '\0';
567 			wpa_config_update_psk(ssid);
568 			ssid->export_keys = 1;
569 		} else {
570 			wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
571 				   "length %lu",
572 				   (unsigned long) cred->key_len);
573 			return -1;
574 		}
575 	}
576 	ssid->priority = wpa_s->conf->wps_priority;
577 
578 	wpas_wps_security_workaround(wpa_s, ssid, cred);
579 
580 	wpas_wps_remove_dup_network(wpa_s, ssid);
581 
582 #ifndef CONFIG_NO_CONFIG_WRITE
583 	if (wpa_s->conf->update_config &&
584 	    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
585 		wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
586 		return -1;
587 	}
588 #endif /* CONFIG_NO_CONFIG_WRITE */
589 
590 	if (ssid->priority)
591 		wpa_config_update_prio_list(wpa_s->conf);
592 
593 	/*
594 	 * Optimize the post-WPS scan based on the channel used during
595 	 * the provisioning in case EAP-Failure is not received.
596 	 */
597 	wpa_s->after_wps = 5;
598 	wpa_s->wps_freq = wpa_s->assoc_freq;
599 
600 	return 0;
601 }
602 
603 
wpa_supplicant_wps_event_m2d(struct wpa_supplicant * wpa_s,struct wps_event_m2d * m2d)604 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
605 					 struct wps_event_m2d *m2d)
606 {
607 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
608 		"dev_password_id=%d config_error=%d",
609 		m2d->dev_password_id, m2d->config_error);
610 	wpas_notify_wps_event_m2d(wpa_s, m2d);
611 #ifdef CONFIG_P2P
612 	if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s) {
613 		wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_M2D
614 			"dev_password_id=%d config_error=%d",
615 			m2d->dev_password_id, m2d->config_error);
616 	}
617 	if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
618 		/*
619 		 * Notify P2P from eloop timeout to avoid issues with the
620 		 * interface getting removed while processing a message.
621 		 */
622 		eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb, wpa_s,
623 				       NULL);
624 	}
625 #endif /* CONFIG_P2P */
626 }
627 
628 
wpas_wps_clear_timeout(void * eloop_ctx,void * timeout_ctx)629 static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
630 {
631 	struct wpa_supplicant *wpa_s = eloop_ctx;
632 	wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
633 	wpas_clear_wps(wpa_s);
634 }
635 
636 
wpa_supplicant_wps_event_fail(struct wpa_supplicant * wpa_s,struct wps_event_fail * fail)637 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
638 					  struct wps_event_fail *fail)
639 {
640 	if (fail->error_indication > 0 &&
641 	    fail->error_indication < NUM_WPS_EI_VALUES) {
642 		wpa_msg(wpa_s, MSG_INFO,
643 			WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
644 			fail->msg, fail->config_error, fail->error_indication,
645 			wps_ei_str(fail->error_indication));
646 		if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
647 			wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
648 				"msg=%d config_error=%d reason=%d (%s)",
649 				fail->msg, fail->config_error,
650 				fail->error_indication,
651 				wps_ei_str(fail->error_indication));
652 	} else {
653 		wpa_msg(wpa_s, MSG_INFO,
654 			WPS_EVENT_FAIL "msg=%d config_error=%d",
655 			fail->msg, fail->config_error);
656 		if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s)
657 			wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
658 				"msg=%d config_error=%d",
659 				fail->msg, fail->config_error);
660 	}
661 
662 	/*
663 	 * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
664 	 */
665 	wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
666 	eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
667 	eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
668 
669 	wpas_notify_wps_event_fail(wpa_s, fail);
670 	wpas_p2p_wps_failed(wpa_s, fail);
671 }
672 
673 
674 static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
675 
wpas_wps_reenable_networks(struct wpa_supplicant * wpa_s)676 static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
677 {
678 	struct wpa_ssid *ssid;
679 	int changed = 0;
680 
681 	eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
682 
683 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
684 		if (ssid->disabled_for_connect && ssid->disabled) {
685 			ssid->disabled_for_connect = 0;
686 			ssid->disabled = 0;
687 			wpas_notify_network_enabled_changed(wpa_s, ssid);
688 			changed++;
689 		}
690 	}
691 
692 	if (changed) {
693 #ifndef CONFIG_NO_CONFIG_WRITE
694 		if (wpa_s->conf->update_config &&
695 		    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
696 			wpa_printf(MSG_DEBUG, "WPS: Failed to update "
697 				   "configuration");
698 		}
699 #endif /* CONFIG_NO_CONFIG_WRITE */
700 	}
701 }
702 
703 
wpas_wps_reenable_networks_cb(void * eloop_ctx,void * timeout_ctx)704 static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
705 {
706 	struct wpa_supplicant *wpa_s = eloop_ctx;
707 	/* Enable the networks disabled during wpas_wps_reassoc */
708 	wpas_wps_reenable_networks(wpa_s);
709 }
710 
711 
wpas_wps_reenable_networks_pending(struct wpa_supplicant * wpa_s)712 int wpas_wps_reenable_networks_pending(struct wpa_supplicant *wpa_s)
713 {
714 	return eloop_is_timeout_registered(wpas_wps_reenable_networks_cb,
715 					   wpa_s, NULL);
716 }
717 
718 
wpa_supplicant_wps_event_success(struct wpa_supplicant * wpa_s)719 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
720 {
721 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
722 	wpa_s->wps_success = 1;
723 	wpas_notify_wps_event_success(wpa_s);
724 	if (wpa_s->current_ssid)
725 		wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
726 	wpa_s->consecutive_conn_failures = 0;
727 
728 	/*
729 	 * Enable the networks disabled during wpas_wps_reassoc after 10
730 	 * seconds. The 10 seconds timer is to allow the data connection to be
731 	 * formed before allowing other networks to be selected.
732 	 */
733 	eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
734 			       NULL);
735 
736 	wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
737 }
738 
739 
wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant * wpa_s,struct wps_event_er_ap * ap)740 static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
741 					       struct wps_event_er_ap *ap)
742 {
743 	char uuid_str[100];
744 	char dev_type[WPS_DEV_TYPE_BUFSIZE];
745 
746 	uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
747 	if (ap->pri_dev_type)
748 		wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
749 				     sizeof(dev_type));
750 	else
751 		dev_type[0] = '\0';
752 
753 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
754 		" pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
755 		uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
756 		ap->friendly_name ? ap->friendly_name : "",
757 		ap->manufacturer ? ap->manufacturer : "",
758 		ap->model_description ? ap->model_description : "",
759 		ap->model_name ? ap->model_name : "",
760 		ap->manufacturer_url ? ap->manufacturer_url : "",
761 		ap->model_url ? ap->model_url : "");
762 }
763 
764 
wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant * wpa_s,struct wps_event_er_ap * ap)765 static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
766 						  struct wps_event_er_ap *ap)
767 {
768 	char uuid_str[100];
769 	uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
770 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
771 }
772 
773 
wpa_supplicant_wps_event_er_enrollee_add(struct wpa_supplicant * wpa_s,struct wps_event_er_enrollee * enrollee)774 static void wpa_supplicant_wps_event_er_enrollee_add(
775 	struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
776 {
777 	char uuid_str[100];
778 	char dev_type[WPS_DEV_TYPE_BUFSIZE];
779 
780 	uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
781 	if (enrollee->pri_dev_type)
782 		wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
783 				     sizeof(dev_type));
784 	else
785 		dev_type[0] = '\0';
786 
787 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
788 		" M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
789 		"|%s|%s|%s|%s|%s|",
790 		uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
791 		enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
792 		enrollee->dev_name ? enrollee->dev_name : "",
793 		enrollee->manufacturer ? enrollee->manufacturer : "",
794 		enrollee->model_name ? enrollee->model_name : "",
795 		enrollee->model_number ? enrollee->model_number : "",
796 		enrollee->serial_number ? enrollee->serial_number : "");
797 }
798 
799 
wpa_supplicant_wps_event_er_enrollee_remove(struct wpa_supplicant * wpa_s,struct wps_event_er_enrollee * enrollee)800 static void wpa_supplicant_wps_event_er_enrollee_remove(
801 	struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
802 {
803 	char uuid_str[100];
804 	uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
805 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
806 		uuid_str, MAC2STR(enrollee->mac_addr));
807 }
808 
809 
wpa_supplicant_wps_event_er_ap_settings(struct wpa_supplicant * wpa_s,struct wps_event_er_ap_settings * ap_settings)810 static void wpa_supplicant_wps_event_er_ap_settings(
811 	struct wpa_supplicant *wpa_s,
812 	struct wps_event_er_ap_settings *ap_settings)
813 {
814 	char uuid_str[100];
815 	char key_str[65];
816 	const struct wps_credential *cred = ap_settings->cred;
817 
818 	key_str[0] = '\0';
819 	if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
820 		if (cred->key_len >= 8 && cred->key_len <= 64) {
821 			os_memcpy(key_str, cred->key, cred->key_len);
822 			key_str[cred->key_len] = '\0';
823 		}
824 	}
825 
826 	uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
827 	/* Use wpa_msg_ctrl to avoid showing the key in debug log */
828 	wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
829 		     "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
830 		     "key=%s",
831 		     uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
832 		     cred->auth_type, cred->encr_type, key_str);
833 }
834 
835 
wpa_supplicant_wps_event_er_set_sel_reg(struct wpa_supplicant * wpa_s,struct wps_event_er_set_selected_registrar * ev)836 static void wpa_supplicant_wps_event_er_set_sel_reg(
837 	struct wpa_supplicant *wpa_s,
838 	struct wps_event_er_set_selected_registrar *ev)
839 {
840 	char uuid_str[100];
841 
842 	uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
843 	switch (ev->state) {
844 	case WPS_ER_SET_SEL_REG_START:
845 		wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
846 			"uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
847 			"sel_reg_config_methods=0x%x",
848 			uuid_str, ev->sel_reg, ev->dev_passwd_id,
849 			ev->sel_reg_config_methods);
850 		break;
851 	case WPS_ER_SET_SEL_REG_DONE:
852 		wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
853 			"uuid=%s state=DONE", uuid_str);
854 		break;
855 	case WPS_ER_SET_SEL_REG_FAILED:
856 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
857 			"uuid=%s state=FAILED", uuid_str);
858 		break;
859 	}
860 }
861 
862 
wpa_supplicant_wps_event(void * ctx,enum wps_event event,union wps_event_data * data)863 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
864 				     union wps_event_data *data)
865 {
866 	struct wpa_supplicant *wpa_s = ctx;
867 	switch (event) {
868 	case WPS_EV_M2D:
869 		wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
870 		break;
871 	case WPS_EV_FAIL:
872 		wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
873 		break;
874 	case WPS_EV_SUCCESS:
875 		wpa_supplicant_wps_event_success(wpa_s);
876 		break;
877 	case WPS_EV_PWD_AUTH_FAIL:
878 #ifdef CONFIG_AP
879 		if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
880 			wpa_supplicant_ap_pwd_auth_fail(wpa_s);
881 #endif /* CONFIG_AP */
882 		break;
883 	case WPS_EV_PBC_OVERLAP:
884 		break;
885 	case WPS_EV_PBC_TIMEOUT:
886 		break;
887 	case WPS_EV_PBC_ACTIVE:
888 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
889 		break;
890 	case WPS_EV_PBC_DISABLE:
891 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
892 		break;
893 	case WPS_EV_ER_AP_ADD:
894 		wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
895 		break;
896 	case WPS_EV_ER_AP_REMOVE:
897 		wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
898 		break;
899 	case WPS_EV_ER_ENROLLEE_ADD:
900 		wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
901 							 &data->enrollee);
902 		break;
903 	case WPS_EV_ER_ENROLLEE_REMOVE:
904 		wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
905 							    &data->enrollee);
906 		break;
907 	case WPS_EV_ER_AP_SETTINGS:
908 		wpa_supplicant_wps_event_er_ap_settings(wpa_s,
909 							&data->ap_settings);
910 		break;
911 	case WPS_EV_ER_SET_SELECTED_REGISTRAR:
912 		wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
913 							&data->set_sel_reg);
914 		break;
915 	case WPS_EV_AP_PIN_SUCCESS:
916 		break;
917 	}
918 }
919 
920 
wpa_supplicant_wps_rf_band(void * ctx)921 static int wpa_supplicant_wps_rf_band(void *ctx)
922 {
923 	struct wpa_supplicant *wpa_s = ctx;
924 
925 	if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
926 		return 0;
927 
928 	return (wpa_s->assoc_freq > 50000) ? WPS_RF_60GHZ :
929 		(wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
930 }
931 
932 
wpas_wps_get_req_type(struct wpa_ssid * ssid)933 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
934 {
935 	if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
936 	    eap_is_wps_pin_enrollee(&ssid->eap))
937 		return WPS_REQ_ENROLLEE;
938 	else
939 		return WPS_REQ_REGISTRAR;
940 }
941 
942 
wpas_clear_wps(struct wpa_supplicant * wpa_s)943 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
944 {
945 	int id;
946 	struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
947 
948 	wpa_s->after_wps = 0;
949 	wpa_s->known_wps_freq = 0;
950 
951 	prev_current = wpa_s->current_ssid;
952 
953 	/* Enable the networks disabled during wpas_wps_reassoc */
954 	wpas_wps_reenable_networks(wpa_s);
955 
956 	eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
957 	eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
958 
959 	/* Remove any existing WPS network from configuration */
960 	ssid = wpa_s->conf->ssid;
961 	while (ssid) {
962 		if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
963 			if (ssid == wpa_s->current_ssid) {
964 				wpa_s->own_disconnect_req = 1;
965 				wpa_supplicant_deauthenticate(
966 					wpa_s, WLAN_REASON_DEAUTH_LEAVING);
967 			}
968 			id = ssid->id;
969 			remove_ssid = ssid;
970 		} else
971 			id = -1;
972 		ssid = ssid->next;
973 		if (id >= 0) {
974 			if (prev_current == remove_ssid) {
975 				wpa_sm_set_config(wpa_s->wpa, NULL);
976 				eapol_sm_notify_config(wpa_s->eapol, NULL,
977 						       NULL);
978 			}
979 			wpas_notify_network_removed(wpa_s, remove_ssid);
980 			wpa_config_remove_network(wpa_s->conf, id);
981 		}
982 	}
983 
984 	wpas_wps_clear_ap_info(wpa_s);
985 }
986 
987 
wpas_wps_timeout(void * eloop_ctx,void * timeout_ctx)988 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
989 {
990 	struct wpa_supplicant *wpa_s = eloop_ctx;
991 	union wps_event_data data;
992 
993 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
994 		"out");
995 	os_memset(&data, 0, sizeof(data));
996 	data.fail.config_error = WPS_CFG_MSG_TIMEOUT;
997 	data.fail.error_indication = WPS_EI_NO_ERROR;
998 	/*
999 	 * Call wpas_notify_wps_event_fail() directly instead of through
1000 	 * wpa_supplicant_wps_event() which would end up registering unnecessary
1001 	 * timeouts (those are only for the case where the failure happens
1002 	 * during an EAP-WSC exchange).
1003 	 */
1004 	wpas_notify_wps_event_fail(wpa_s, &data.fail);
1005 	wpas_clear_wps(wpa_s);
1006 }
1007 
1008 
wpas_wps_add_network(struct wpa_supplicant * wpa_s,int registrar,const u8 * dev_addr,const u8 * bssid)1009 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
1010 					      int registrar, const u8 *dev_addr,
1011 					      const u8 *bssid)
1012 {
1013 	struct wpa_ssid *ssid;
1014 
1015 	ssid = wpa_config_add_network(wpa_s->conf);
1016 	if (ssid == NULL)
1017 		return NULL;
1018 	wpas_notify_network_added(wpa_s, ssid);
1019 	wpa_config_set_network_defaults(ssid);
1020 	ssid->temporary = 1;
1021 	if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
1022 	    wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
1023 	    wpa_config_set(ssid, "identity", registrar ?
1024 			   "\"" WSC_ID_REGISTRAR "\"" :
1025 			   "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
1026 		wpas_notify_network_removed(wpa_s, ssid);
1027 		wpa_config_remove_network(wpa_s->conf, ssid->id);
1028 		return NULL;
1029 	}
1030 
1031 #ifdef CONFIG_P2P
1032 	if (dev_addr)
1033 		os_memcpy(ssid->go_p2p_dev_addr, dev_addr, ETH_ALEN);
1034 #endif /* CONFIG_P2P */
1035 
1036 	if (bssid) {
1037 #ifndef CONFIG_P2P
1038 		struct wpa_bss *bss;
1039 		int count = 0;
1040 #endif /* CONFIG_P2P */
1041 
1042 		os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1043 		ssid->bssid_set = 1;
1044 
1045 		/*
1046 		 * Note: With P2P, the SSID may change at the time the WPS
1047 		 * provisioning is started, so better not filter the AP based
1048 		 * on the current SSID in the scan results.
1049 		 */
1050 #ifndef CONFIG_P2P
1051 		dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1052 			if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
1053 				continue;
1054 
1055 			os_free(ssid->ssid);
1056 			ssid->ssid = os_memdup(bss->ssid, bss->ssid_len);
1057 			if (ssid->ssid == NULL)
1058 				break;
1059 			ssid->ssid_len = bss->ssid_len;
1060 			wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
1061 					  "scan results",
1062 					  ssid->ssid, ssid->ssid_len);
1063 			count++;
1064 		}
1065 
1066 		if (count > 1) {
1067 			wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
1068 				   "for the AP; use wildcard");
1069 			os_free(ssid->ssid);
1070 			ssid->ssid = NULL;
1071 			ssid->ssid_len = 0;
1072 		}
1073 #endif /* CONFIG_P2P */
1074 	}
1075 
1076 	return ssid;
1077 }
1078 
1079 
wpas_wps_temp_disable(struct wpa_supplicant * wpa_s,struct wpa_ssid * selected)1080 static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
1081 				  struct wpa_ssid *selected)
1082 {
1083 	struct wpa_ssid *ssid;
1084 
1085 	if (wpa_s->current_ssid) {
1086 		wpa_s->own_disconnect_req = 1;
1087 		wpa_supplicant_deauthenticate(
1088 			wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1089 	}
1090 
1091 	/* Mark all other networks disabled and trigger reassociation */
1092 	ssid = wpa_s->conf->ssid;
1093 	while (ssid) {
1094 		int was_disabled = ssid->disabled;
1095 		ssid->disabled_for_connect = 0;
1096 		/*
1097 		 * In case the network object corresponds to a persistent group
1098 		 * then do not send out network disabled signal. In addition,
1099 		 * do not change disabled status of persistent network objects
1100 		 * from 2 to 1 should we connect to another network.
1101 		 */
1102 		if (was_disabled != 2) {
1103 			ssid->disabled = ssid != selected;
1104 			if (was_disabled != ssid->disabled) {
1105 				if (ssid->disabled)
1106 					ssid->disabled_for_connect = 1;
1107 				wpas_notify_network_enabled_changed(wpa_s,
1108 								    ssid);
1109 			}
1110 		}
1111 		ssid = ssid->next;
1112 	}
1113 }
1114 
1115 
wpas_wps_reassoc(struct wpa_supplicant * wpa_s,struct wpa_ssid * selected,const u8 * bssid,int freq)1116 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
1117 			     struct wpa_ssid *selected, const u8 *bssid,
1118 			     int freq)
1119 {
1120 	struct wpa_bss *bss;
1121 
1122 	wpa_s->wps_run++;
1123 	if (wpa_s->wps_run == 0)
1124 		wpa_s->wps_run++;
1125 	wpa_s->after_wps = 0;
1126 	wpa_s->known_wps_freq = 0;
1127 	if (freq) {
1128 		wpa_s->after_wps = 5;
1129 		wpa_s->wps_freq = freq;
1130 	} else if (bssid) {
1131 		bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
1132 		if (bss && bss->freq > 0) {
1133 			wpa_s->known_wps_freq = 1;
1134 			wpa_s->wps_freq = bss->freq;
1135 		}
1136 	}
1137 
1138 	wpas_wps_temp_disable(wpa_s, selected);
1139 
1140 	wpa_s->disconnected = 0;
1141 	wpa_s->reassociate = 1;
1142 	wpa_s->scan_runs = 0;
1143 	wpa_s->normal_scans = 0;
1144 	wpa_s->wps_success = 0;
1145 	wpa_s->bssid_ignore_cleared = false;
1146 
1147 	wpa_supplicant_cancel_sched_scan(wpa_s);
1148 	wpa_supplicant_req_scan(wpa_s, 0, 0);
1149 }
1150 
1151 
wpas_wps_start_pbc(struct wpa_supplicant * wpa_s,const u8 * bssid,int p2p_group,int multi_ap_backhaul_sta)1152 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1153 		       int p2p_group, int multi_ap_backhaul_sta)
1154 {
1155 	struct wpa_ssid *ssid;
1156 	char phase1[32];
1157 
1158 #ifdef CONFIG_AP
1159 	if (wpa_s->ap_iface) {
1160 		wpa_printf(MSG_DEBUG,
1161 			   "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1162 		return -1;
1163 	}
1164 #endif /* CONFIG_AP */
1165 	wpas_clear_wps(wpa_s);
1166 	ssid = wpas_wps_add_network(wpa_s, 0, NULL, bssid);
1167 	if (ssid == NULL)
1168 		return -1;
1169 	ssid->temporary = 1;
1170 	ssid->p2p_group = p2p_group;
1171 	/*
1172 	 * When starting a regular WPS process (not P2P group formation)
1173 	 * the registrar/final station can be either AP or PCP
1174 	 * so use a "don't care" value for the pbss flag.
1175 	 */
1176 	if (!p2p_group)
1177 		ssid->pbss = 2;
1178 #ifdef CONFIG_P2P
1179 	if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1180 		ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1181 		if (ssid->ssid) {
1182 			ssid->ssid_len = wpa_s->go_params->ssid_len;
1183 			os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1184 				  ssid->ssid_len);
1185 			if (wpa_s->go_params->freq > 56160) {
1186 				/* P2P in 60 GHz uses PBSS */
1187 				ssid->pbss = 1;
1188 			}
1189 			if (wpa_s->go_params->edmg &&
1190 			    wpas_p2p_try_edmg_channel(wpa_s,
1191 						      wpa_s->go_params) == 0)
1192 				ssid->enable_edmg = 1;
1193 
1194 			wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1195 					  "SSID", ssid->ssid, ssid->ssid_len);
1196 		}
1197 	}
1198 #endif /* CONFIG_P2P */
1199 	os_snprintf(phase1, sizeof(phase1), "pbc=1%s",
1200 		    multi_ap_backhaul_sta ? " multi_ap=1" : "");
1201 	if (wpa_config_set_quoted(ssid, "phase1", phase1) < 0)
1202 		return -1;
1203 	if (wpa_s->wps_fragment_size)
1204 		ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1205 	if (multi_ap_backhaul_sta)
1206 		ssid->multi_ap_backhaul_sta = 1;
1207 	wpa_supplicant_wps_event(wpa_s, WPS_EV_PBC_ACTIVE, NULL);
1208 	eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1209 			       wpa_s, NULL);
1210 	wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
1211 	return 0;
1212 }
1213 
1214 
wpas_wps_start_dev_pw(struct wpa_supplicant * wpa_s,const u8 * dev_addr,const u8 * bssid,const char * pin,int p2p_group,u16 dev_pw_id,const u8 * peer_pubkey_hash,const u8 * ssid_val,size_t ssid_len,int freq)1215 static int wpas_wps_start_dev_pw(struct wpa_supplicant *wpa_s,
1216 				 const u8 *dev_addr, const u8 *bssid,
1217 				 const char *pin, int p2p_group, u16 dev_pw_id,
1218 				 const u8 *peer_pubkey_hash,
1219 				 const u8 *ssid_val, size_t ssid_len, int freq)
1220 {
1221 	struct wpa_ssid *ssid;
1222 	char val[128 + 2 * WPS_OOB_PUBKEY_HASH_LEN];
1223 	unsigned int rpin = 0;
1224 	char hash[2 * WPS_OOB_PUBKEY_HASH_LEN + 10];
1225 
1226 #ifdef CONFIG_AP
1227 	if (wpa_s->ap_iface) {
1228 		wpa_printf(MSG_DEBUG,
1229 			   "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1230 		return -1;
1231 	}
1232 #endif /* CONFIG_AP */
1233 	wpas_clear_wps(wpa_s);
1234 	if (bssid && is_zero_ether_addr(bssid))
1235 		bssid = NULL;
1236 	ssid = wpas_wps_add_network(wpa_s, 0, dev_addr, bssid);
1237 	if (ssid == NULL) {
1238 		wpa_printf(MSG_DEBUG, "WPS: Could not add network");
1239 		return -1;
1240 	}
1241 	ssid->temporary = 1;
1242 	ssid->p2p_group = p2p_group;
1243 	/*
1244 	 * When starting a regular WPS process (not P2P group formation)
1245 	 * the registrar/final station can be either AP or PCP
1246 	 * so use a "don't care" value for the pbss flag.
1247 	 */
1248 	if (!p2p_group)
1249 		ssid->pbss = 2;
1250 	if (ssid_val) {
1251 		ssid->ssid = os_malloc(ssid_len);
1252 		if (ssid->ssid) {
1253 			os_memcpy(ssid->ssid, ssid_val, ssid_len);
1254 			ssid->ssid_len = ssid_len;
1255 		}
1256 	}
1257 	if (peer_pubkey_hash) {
1258 		os_memcpy(hash, " pkhash=", 8);
1259 		wpa_snprintf_hex_uppercase(hash + 8, sizeof(hash) - 8,
1260 					   peer_pubkey_hash,
1261 					   WPS_OOB_PUBKEY_HASH_LEN);
1262 	} else {
1263 		hash[0] = '\0';
1264 	}
1265 #ifdef CONFIG_P2P
1266 	if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
1267 		os_free(ssid->ssid);
1268 		ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
1269 		if (ssid->ssid) {
1270 			ssid->ssid_len = wpa_s->go_params->ssid_len;
1271 			os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
1272 				  ssid->ssid_len);
1273 			if (wpa_s->go_params->freq > 56160) {
1274 				/* P2P in 60 GHz uses PBSS */
1275 				ssid->pbss = 1;
1276 			}
1277 			if (wpa_s->go_params->edmg &&
1278 			    wpas_p2p_try_edmg_channel(wpa_s,
1279 						      wpa_s->go_params) == 0)
1280 				ssid->enable_edmg = 1;
1281 
1282 			wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
1283 					  "SSID", ssid->ssid, ssid->ssid_len);
1284 		}
1285 	}
1286 #endif /* CONFIG_P2P */
1287 	if (pin)
1288 		os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u%s\"",
1289 			    pin, dev_pw_id, hash);
1290 	else if (pin == NULL && dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1291 		os_snprintf(val, sizeof(val), "\"dev_pw_id=%u%s\"",
1292 			    dev_pw_id, hash);
1293 	} else {
1294 		if (wps_generate_pin(&rpin) < 0) {
1295 			wpa_printf(MSG_DEBUG, "WPS: Could not generate PIN");
1296 			return -1;
1297 		}
1298 		os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u%s\"",
1299 			    rpin, dev_pw_id, hash);
1300 	}
1301 	if (wpa_config_set(ssid, "phase1", val, 0) < 0) {
1302 		wpa_printf(MSG_DEBUG, "WPS: Failed to set phase1 '%s'", val);
1303 		return -1;
1304 	}
1305 
1306 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER)
1307 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_PIN_ACTIVE);
1308 
1309 	if (wpa_s->wps_fragment_size)
1310 		ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1311 	eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1312 			       wpa_s, NULL);
1313 	wpa_s->wps_ap_iter = 1;
1314 	wpas_wps_reassoc(wpa_s, ssid, bssid, freq);
1315 	return rpin;
1316 }
1317 
1318 
wpas_wps_start_pin(struct wpa_supplicant * wpa_s,const u8 * bssid,const char * pin,int p2p_group,u16 dev_pw_id)1319 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1320 		       const char *pin, int p2p_group, u16 dev_pw_id)
1321 {
1322 	os_get_reltime(&wpa_s->wps_pin_start_time);
1323 	return wpas_wps_start_dev_pw(wpa_s, NULL, bssid, pin, p2p_group,
1324 				     dev_pw_id, NULL, NULL, 0, 0);
1325 }
1326 
1327 
wpas_wps_pbc_overlap(struct wpa_supplicant * wpa_s)1328 void wpas_wps_pbc_overlap(struct wpa_supplicant *wpa_s)
1329 {
1330 	union wps_event_data data;
1331 
1332 	os_memset(&data, 0, sizeof(data));
1333 	data.fail.config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
1334 	data.fail.error_indication = WPS_EI_NO_ERROR;
1335 	/*
1336 	 * Call wpas_notify_wps_event_fail() directly instead of through
1337 	 * wpa_supplicant_wps_event() which would end up registering unnecessary
1338 	 * timeouts (those are only for the case where the failure happens
1339 	 * during an EAP-WSC exchange).
1340 	 */
1341 	wpas_notify_wps_event_fail(wpa_s, &data.fail);
1342 }
1343 
1344 /* Cancel the wps pbc/pin requests */
wpas_wps_cancel(struct wpa_supplicant * wpa_s)1345 int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
1346 {
1347 #ifdef CONFIG_AP
1348 	if (wpa_s->ap_iface) {
1349 		wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
1350 		return wpa_supplicant_ap_wps_cancel(wpa_s);
1351 	}
1352 #endif /* CONFIG_AP */
1353 
1354 	if (wpa_s->wpa_state == WPA_SCANNING ||
1355 	    wpa_s->wpa_state == WPA_DISCONNECTED) {
1356 		wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
1357 		wpa_supplicant_cancel_scan(wpa_s);
1358 		wpas_clear_wps(wpa_s);
1359 	} else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1360 		wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
1361 			   "deauthenticate");
1362 		wpa_s->own_disconnect_req = 1;
1363 		wpa_supplicant_deauthenticate(wpa_s,
1364 					      WLAN_REASON_DEAUTH_LEAVING);
1365 		wpas_clear_wps(wpa_s);
1366 	} else {
1367 		wpas_wps_reenable_networks(wpa_s);
1368 		wpas_wps_clear_ap_info(wpa_s);
1369 		if (eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL) >
1370 		    0)
1371 			wpas_clear_wps(wpa_s);
1372 	}
1373 
1374 	wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CANCEL);
1375 	wpa_s->after_wps = 0;
1376 
1377 	return 0;
1378 }
1379 
1380 
wpas_wps_start_reg(struct wpa_supplicant * wpa_s,const u8 * bssid,const char * pin,struct wps_new_ap_settings * settings)1381 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
1382 		       const char *pin, struct wps_new_ap_settings *settings)
1383 {
1384 	struct wpa_ssid *ssid;
1385 	char val[200];
1386 	char *pos, *end;
1387 	int res;
1388 
1389 #ifdef CONFIG_AP
1390 	if (wpa_s->ap_iface) {
1391 		wpa_printf(MSG_DEBUG,
1392 			   "WPS: Reject request to start Registrar(as station) operation while AP mode is enabled");
1393 		return -1;
1394 	}
1395 #endif /* CONFIG_AP */
1396 	if (!pin)
1397 		return -1;
1398 	wpas_clear_wps(wpa_s);
1399 	ssid = wpas_wps_add_network(wpa_s, 1, NULL, bssid);
1400 	if (ssid == NULL)
1401 		return -1;
1402 	ssid->temporary = 1;
1403 	pos = val;
1404 	end = pos + sizeof(val);
1405 	res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
1406 	if (os_snprintf_error(end - pos, res))
1407 		return -1;
1408 	pos += res;
1409 	if (settings) {
1410 		res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
1411 				  "new_encr=%s new_key=%s",
1412 				  settings->ssid_hex, settings->auth,
1413 				  settings->encr, settings->key_hex);
1414 		if (os_snprintf_error(end - pos, res))
1415 			return -1;
1416 		pos += res;
1417 	}
1418 	res = os_snprintf(pos, end - pos, "\"");
1419 	if (os_snprintf_error(end - pos, res))
1420 		return -1;
1421 	if (wpa_config_set(ssid, "phase1", val, 0) < 0)
1422 		return -1;
1423 	if (wpa_s->wps_fragment_size)
1424 		ssid->eap.fragment_size = wpa_s->wps_fragment_size;
1425 	eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
1426 			       wpa_s, NULL);
1427 	wpas_wps_reassoc(wpa_s, ssid, bssid, 0);
1428 	return 0;
1429 }
1430 
1431 
wpas_wps_new_psk_cb(void * ctx,const u8 * mac_addr,const u8 * p2p_dev_addr,const u8 * psk,size_t psk_len)1432 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
1433 			       const u8 *p2p_dev_addr, const u8 *psk,
1434 			       size_t psk_len)
1435 {
1436 	if (is_zero_ether_addr(p2p_dev_addr)) {
1437 		wpa_printf(MSG_DEBUG,
1438 			   "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
1439 			   MAC2STR(mac_addr));
1440 	} else {
1441 		wpa_printf(MSG_DEBUG,
1442 			   "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
1443 			   " P2P Device Addr " MACSTR,
1444 			   MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
1445 	}
1446 	wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
1447 
1448 	/* TODO */
1449 
1450 	return 0;
1451 }
1452 
1453 
wpas_wps_pin_needed_cb(void * ctx,const u8 * uuid_e,const struct wps_device_data * dev)1454 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
1455 				   const struct wps_device_data *dev)
1456 {
1457 	char uuid[40], txt[400];
1458 	int len;
1459 	char devtype[WPS_DEV_TYPE_BUFSIZE];
1460 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
1461 		return;
1462 	wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
1463 	len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
1464 			  " [%s|%s|%s|%s|%s|%s]",
1465 			  uuid, MAC2STR(dev->mac_addr), dev->device_name,
1466 			  dev->manufacturer, dev->model_name,
1467 			  dev->model_number, dev->serial_number,
1468 			  wps_dev_type_bin2str(dev->pri_dev_type, devtype,
1469 					       sizeof(devtype)));
1470 	if (!os_snprintf_error(sizeof(txt), len))
1471 		wpa_printf(MSG_INFO, "%s", txt);
1472 }
1473 
1474 
wpas_wps_set_sel_reg_cb(void * ctx,int sel_reg,u16 dev_passwd_id,u16 sel_reg_config_methods)1475 static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
1476 				    u16 sel_reg_config_methods)
1477 {
1478 #ifdef CONFIG_WPS_ER
1479 	struct wpa_supplicant *wpa_s = ctx;
1480 
1481 	if (wpa_s->wps_er == NULL)
1482 		return;
1483 	wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
1484 		   "dev_password_id=%u sel_reg_config_methods=0x%x",
1485 		   sel_reg, dev_passwd_id, sel_reg_config_methods);
1486 	wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
1487 			   sel_reg_config_methods);
1488 #endif /* CONFIG_WPS_ER */
1489 }
1490 
1491 
wps_fix_config_methods(u16 config_methods)1492 static u16 wps_fix_config_methods(u16 config_methods)
1493 {
1494 	if ((config_methods &
1495 	     (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1496 	      WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1497 		wpa_printf(MSG_INFO, "WPS: Converting display to "
1498 			   "virtual_display for WPS 2.0 compliance");
1499 		config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1500 	}
1501 	if ((config_methods &
1502 	     (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1503 	      WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1504 		wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1505 			   "virtual_push_button for WPS 2.0 compliance");
1506 		config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1507 	}
1508 
1509 	return config_methods;
1510 }
1511 
1512 
wpas_wps_set_uuid(struct wpa_supplicant * wpa_s,struct wps_context * wps)1513 static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
1514 			      struct wps_context *wps)
1515 {
1516 	char buf[50];
1517 	const char *src;
1518 
1519 	if (is_nil_uuid(wpa_s->conf->uuid)) {
1520 		struct wpa_supplicant *first;
1521 		first = wpa_s->global->ifaces;
1522 		while (first && first->next)
1523 			first = first->next;
1524 		if (first && first != wpa_s) {
1525 			if (wps != wpa_s->global->ifaces->wps)
1526 				os_memcpy(wps->uuid,
1527 					  wpa_s->global->ifaces->wps->uuid,
1528 					  WPS_UUID_LEN);
1529 			src = "from the first interface";
1530 		} else if (wpa_s->conf->auto_uuid == 1) {
1531 			uuid_random(wps->uuid);
1532 			src = "based on random data";
1533 		} else {
1534 			uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
1535 			src = "based on MAC address";
1536 		}
1537 	} else {
1538 		os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
1539 		src = "based on configuration";
1540 	}
1541 
1542 	uuid_bin2str(wps->uuid, buf, sizeof(buf));
1543 	wpa_dbg(wpa_s, MSG_DEBUG, "WPS: UUID %s: %s", src, buf);
1544 }
1545 
1546 
wpas_wps_set_vendor_ext_m1(struct wpa_supplicant * wpa_s,struct wps_context * wps)1547 static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
1548 				       struct wps_context *wps)
1549 {
1550 	wpabuf_free(wps->dev.vendor_ext_m1);
1551 	wps->dev.vendor_ext_m1 = NULL;
1552 
1553 	if (wpa_s->conf->wps_vendor_ext_m1) {
1554 		wps->dev.vendor_ext_m1 =
1555 			wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
1556 		if (!wps->dev.vendor_ext_m1) {
1557 			wpa_printf(MSG_ERROR, "WPS: Cannot "
1558 				   "allocate memory for vendor_ext_m1");
1559 		}
1560 	}
1561 }
1562 
1563 
wpas_wps_init(struct wpa_supplicant * wpa_s)1564 int wpas_wps_init(struct wpa_supplicant *wpa_s)
1565 {
1566 	struct wps_context *wps;
1567 	struct wps_registrar_config rcfg;
1568 	struct hostapd_hw_modes *modes;
1569 	u16 m;
1570 
1571 	wps = os_zalloc(sizeof(*wps));
1572 	if (wps == NULL)
1573 		return -1;
1574 
1575 	wps->cred_cb = wpa_supplicant_wps_cred;
1576 	wps->event_cb = wpa_supplicant_wps_event;
1577 	wps->rf_band_cb = wpa_supplicant_wps_rf_band;
1578 	wps->cb_ctx = wpa_s;
1579 
1580 	wps->dev.device_name = wpa_s->conf->device_name;
1581 	wps->dev.manufacturer = wpa_s->conf->manufacturer;
1582 	wps->dev.model_name = wpa_s->conf->model_name;
1583 	wps->dev.model_number = wpa_s->conf->model_number;
1584 	wps->dev.serial_number = wpa_s->conf->serial_number;
1585 	wps->config_methods =
1586 		wps_config_methods_str2bin(wpa_s->conf->config_methods);
1587 	if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
1588 	    (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
1589 		wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
1590 			   "methods are not allowed at the same time");
1591 		os_free(wps);
1592 		return -1;
1593 	}
1594 	wps->config_methods = wps_fix_config_methods(wps->config_methods);
1595 	wps->dev.config_methods = wps->config_methods;
1596 	os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
1597 		  WPS_DEV_TYPE_LEN);
1598 
1599 	wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
1600 	os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
1601 		  WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
1602 
1603 	wpas_wps_set_vendor_ext_m1(wpa_s, wps);
1604 
1605 	wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
1606 	modes = wpa_s->hw.modes;
1607 	if (modes) {
1608 		for (m = 0; m < wpa_s->hw.num_modes; m++) {
1609 			if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
1610 			    modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1611 				wps->dev.rf_bands |= WPS_RF_24GHZ;
1612 			else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
1613 				wps->dev.rf_bands |= WPS_RF_50GHZ;
1614 			else if (modes[m].mode == HOSTAPD_MODE_IEEE80211AD)
1615 				wps->dev.rf_bands |= WPS_RF_60GHZ;
1616 		}
1617 	}
1618 	if (wps->dev.rf_bands == 0) {
1619 		/*
1620 		 * Default to claiming support for both bands if the driver
1621 		 * does not provide support for fetching supported bands.
1622 		 */
1623 		wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
1624 	}
1625 	os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
1626 	wpas_wps_set_uuid(wpa_s, wps);
1627 
1628 #ifdef CONFIG_NO_TKIP
1629 	wps->auth_types = WPS_AUTH_WPA2PSK;
1630 	wps->encr_types = WPS_ENCR_AES;
1631 #else /* CONFIG_NO_TKIP */
1632 	wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1633 	wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1634 #endif /* CONFIG_NO_TKIP */
1635 
1636 	os_memset(&rcfg, 0, sizeof(rcfg));
1637 	rcfg.new_psk_cb = wpas_wps_new_psk_cb;
1638 	rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
1639 	rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
1640 	rcfg.cb_ctx = wpa_s;
1641 
1642 	wps->registrar = wps_registrar_init(wps, &rcfg);
1643 	if (wps->registrar == NULL) {
1644 		wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
1645 		os_free(wps);
1646 		return -1;
1647 	}
1648 
1649 	wpa_s->wps = wps;
1650 
1651 	return 0;
1652 }
1653 
1654 
1655 #ifdef CONFIG_WPS_ER
wpas_wps_nfc_clear(struct wps_context * wps)1656 static void wpas_wps_nfc_clear(struct wps_context *wps)
1657 {
1658 	wps->ap_nfc_dev_pw_id = 0;
1659 	wpabuf_free(wps->ap_nfc_dh_pubkey);
1660 	wps->ap_nfc_dh_pubkey = NULL;
1661 	wpabuf_free(wps->ap_nfc_dh_privkey);
1662 	wps->ap_nfc_dh_privkey = NULL;
1663 	wpabuf_free(wps->ap_nfc_dev_pw);
1664 	wps->ap_nfc_dev_pw = NULL;
1665 }
1666 #endif /* CONFIG_WPS_ER */
1667 
1668 
wpas_wps_deinit(struct wpa_supplicant * wpa_s)1669 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
1670 {
1671 	wpas_wps_assoc_with_cred_cancel(wpa_s);
1672 	eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
1673 	eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
1674 	eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
1675 	wpas_wps_clear_ap_info(wpa_s);
1676 
1677 #ifdef CONFIG_P2P
1678 	eloop_cancel_timeout(wpas_p2p_pbc_overlap_cb, wpa_s, NULL);
1679 #endif /* CONFIG_P2P */
1680 
1681 	if (wpa_s->wps == NULL)
1682 		return;
1683 
1684 #ifdef CONFIG_WPS_ER
1685 	wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1686 	wpa_s->wps_er = NULL;
1687 	wpas_wps_nfc_clear(wpa_s->wps);
1688 #endif /* CONFIG_WPS_ER */
1689 
1690 	wps_registrar_deinit(wpa_s->wps->registrar);
1691 	wpabuf_free(wpa_s->wps->dh_pubkey);
1692 	wpabuf_free(wpa_s->wps->dh_privkey);
1693 	wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
1694 	os_free(wpa_s->wps->network_key);
1695 	os_free(wpa_s->wps);
1696 	wpa_s->wps = NULL;
1697 }
1698 
1699 
wpas_wps_ssid_bss_match(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_bss * bss)1700 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
1701 			    struct wpa_ssid *ssid, struct wpa_bss *bss)
1702 {
1703 	struct wpabuf *wps_ie;
1704 
1705 	if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
1706 		return -1;
1707 
1708 	wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1709 	if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1710 		if (!wps_ie) {
1711 			wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1712 			return 0;
1713 		}
1714 
1715 		if (!wps_is_selected_pbc_registrar(wps_ie)) {
1716 			wpa_printf(MSG_DEBUG, "   skip - WPS AP "
1717 				   "without active PBC Registrar");
1718 			wpabuf_free(wps_ie);
1719 			return 0;
1720 		}
1721 
1722 		/* TODO: overlap detection */
1723 		wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1724 			   "(Active PBC)");
1725 		wpabuf_free(wps_ie);
1726 		return 1;
1727 	}
1728 
1729 	if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1730 		if (!wps_ie) {
1731 			wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
1732 			return 0;
1733 		}
1734 
1735 		/*
1736 		 * Start with WPS APs that advertise our address as an
1737 		 * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
1738 		 * allow any WPS AP after couple of scans since some APs do not
1739 		 * set Selected Registrar attribute properly when using
1740 		 * external Registrar.
1741 		 */
1742 		if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
1743 			struct os_reltime age;
1744 
1745 			os_reltime_age(&wpa_s->wps_pin_start_time, &age);
1746 
1747 			if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG ||
1748 			    age.sec < WPS_PIN_TIME_IGNORE_SEL_REG) {
1749 				wpa_printf(MSG_DEBUG,
1750 					   "   skip - WPS AP without active PIN Registrar (scan_runs=%d age=%d)",
1751 					   wpa_s->scan_runs, (int) age.sec);
1752 				wpabuf_free(wps_ie);
1753 				return 0;
1754 			}
1755 			wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1756 		} else {
1757 			wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
1758 				   "(Authorized MAC or Active PIN)");
1759 		}
1760 		wpabuf_free(wps_ie);
1761 		return 1;
1762 	}
1763 
1764 	if (wps_ie) {
1765 		wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
1766 		wpabuf_free(wps_ie);
1767 		return 1;
1768 	}
1769 
1770 	return -1;
1771 }
1772 
1773 
wpas_wps_ssid_wildcard_ok(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_bss * bss)1774 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
1775 			      struct wpa_ssid *ssid,
1776 			      struct wpa_bss *bss)
1777 {
1778 	struct wpabuf *wps_ie = NULL;
1779 	int ret = 0;
1780 
1781 	if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
1782 		wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1783 		if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
1784 			/* allow wildcard SSID for WPS PBC */
1785 			ret = 1;
1786 		}
1787 	} else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
1788 		wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1789 		if (wps_ie &&
1790 		    (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
1791 		     wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
1792 			/* allow wildcard SSID for WPS PIN */
1793 			ret = 1;
1794 		}
1795 	}
1796 
1797 	if (!ret && ssid->bssid_set &&
1798 	    os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
1799 		/* allow wildcard SSID due to hardcoded BSSID match */
1800 		ret = 1;
1801 	}
1802 
1803 #ifdef CONFIG_WPS_STRICT
1804 	if (wps_ie) {
1805 		if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
1806 						   0, bss->bssid) < 0)
1807 			ret = 0;
1808 		if (bss->beacon_ie_len) {
1809 			struct wpabuf *bcn_wps;
1810 			bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
1811 				bss, WPS_IE_VENDOR_TYPE);
1812 			if (bcn_wps == NULL) {
1813 				wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
1814 					   "missing from AP Beacon");
1815 				ret = 0;
1816 			} else {
1817 				if (wps_validate_beacon(wps_ie) < 0)
1818 					ret = 0;
1819 				wpabuf_free(bcn_wps);
1820 			}
1821 		}
1822 	}
1823 #endif /* CONFIG_WPS_STRICT */
1824 
1825 	wpabuf_free(wps_ie);
1826 
1827 	return ret;
1828 }
1829 
1830 
wpas_wps_scan_pbc_overlap(struct wpa_supplicant * wpa_s,struct wpa_bss * selected,struct wpa_ssid * ssid)1831 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
1832 			      struct wpa_bss *selected, struct wpa_ssid *ssid)
1833 {
1834 	const u8 *sel_uuid;
1835 	struct wpabuf *wps_ie;
1836 	int ret = 0;
1837 	size_t i;
1838 
1839 	if (!eap_is_wps_pbc_enrollee(&ssid->eap))
1840 		return 0;
1841 
1842 	wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
1843 		   "present in scan results; selected BSSID " MACSTR,
1844 		   MAC2STR(selected->bssid));
1845 	if (!is_zero_ether_addr(ssid->bssid))
1846 		wpa_printf(MSG_DEBUG,
1847 			   "WPS: Network profile limited to accept only a single BSSID " MACSTR,
1848 			   MAC2STR(ssid->bssid));
1849 
1850 	/* Make sure that only one AP is in active PBC mode */
1851 	wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
1852 	if (wps_ie) {
1853 		sel_uuid = wps_get_uuid_e(wps_ie);
1854 		wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
1855 			    sel_uuid, UUID_LEN);
1856 	} else {
1857 		wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
1858 			   "WPS IE?!");
1859 		sel_uuid = NULL;
1860 	}
1861 
1862 	for (i = 0; i < wpa_s->num_wps_ap; i++) {
1863 		struct wps_ap_info *ap = &wpa_s->wps_ap[i];
1864 
1865 		if (!ap->pbc_active ||
1866 		    os_memcmp(selected->bssid, ap->bssid, ETH_ALEN) == 0)
1867 			continue;
1868 
1869 		if (!is_zero_ether_addr(ssid->bssid) &&
1870 		    os_memcmp(ap->bssid, ssid->bssid, ETH_ALEN) != 0) {
1871 			wpa_printf(MSG_DEBUG, "WPS: Ignore another BSS " MACSTR
1872 				   " in active PBC mode due to local BSSID limitation",
1873 				   MAC2STR(ap->bssid));
1874 			continue;
1875 		}
1876 
1877 		wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
1878 			   MACSTR, MAC2STR(ap->bssid));
1879 		wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
1880 			    ap->uuid, UUID_LEN);
1881 		if (sel_uuid == NULL ||
1882 		    os_memcmp(sel_uuid, ap->uuid, UUID_LEN) != 0) {
1883 			ret = 1; /* PBC overlap */
1884 			wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
1885 				MACSTR " and " MACSTR,
1886 				MAC2STR(selected->bssid),
1887 				MAC2STR(ap->bssid));
1888 			break;
1889 		}
1890 
1891 		/* TODO: verify that this is reasonable dual-band situation */
1892 	}
1893 
1894 	wpabuf_free(wps_ie);
1895 
1896 	return ret;
1897 }
1898 
1899 
wpas_wps_notify_scan_results(struct wpa_supplicant * wpa_s)1900 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
1901 {
1902 	struct wpa_bss *bss;
1903 	unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
1904 
1905 	if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
1906 		return;
1907 
1908 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1909 		struct wpabuf *ie;
1910 		ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1911 		if (!ie)
1912 			continue;
1913 		if (wps_is_selected_pbc_registrar(ie))
1914 			pbc++;
1915 		else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
1916 			auth++;
1917 		else if (wps_is_selected_pin_registrar(ie))
1918 			pin++;
1919 		else
1920 			wps++;
1921 		wpabuf_free(ie);
1922 	}
1923 
1924 	if (pbc)
1925 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
1926 	else if (auth)
1927 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
1928 	else if (pin)
1929 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
1930 	else if (wps)
1931 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
1932 }
1933 
1934 
wpas_wps_searching(struct wpa_supplicant * wpa_s)1935 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
1936 {
1937 	struct wpa_ssid *ssid;
1938 
1939 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1940 		if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
1941 			return 1;
1942 	}
1943 
1944 	return 0;
1945 }
1946 
1947 
wpas_wps_scan_result_text(const u8 * ies,size_t ies_len,char * buf,char * end)1948 int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
1949 			      char *end)
1950 {
1951 	struct wpabuf *wps_ie;
1952 	int ret;
1953 
1954 	wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
1955 	if (wps_ie == NULL)
1956 		return 0;
1957 
1958 	ret = wps_attr_text(wps_ie, buf, end);
1959 	wpabuf_free(wps_ie);
1960 	return ret;
1961 }
1962 
1963 
wpas_wps_er_start(struct wpa_supplicant * wpa_s,const char * filter)1964 int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
1965 {
1966 #ifdef CONFIG_WPS_ER
1967 	if (wpa_s->wps_er) {
1968 		wps_er_refresh(wpa_s->wps_er);
1969 		return 0;
1970 	}
1971 	wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
1972 	if (wpa_s->wps_er == NULL)
1973 		return -1;
1974 	return 0;
1975 #else /* CONFIG_WPS_ER */
1976 	return 0;
1977 #endif /* CONFIG_WPS_ER */
1978 }
1979 
1980 
wpas_wps_er_stop(struct wpa_supplicant * wpa_s)1981 void wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
1982 {
1983 #ifdef CONFIG_WPS_ER
1984 	wps_er_deinit(wpa_s->wps_er, NULL, NULL);
1985 	wpa_s->wps_er = NULL;
1986 #endif /* CONFIG_WPS_ER */
1987 }
1988 
1989 
1990 #ifdef CONFIG_WPS_ER
wpas_wps_er_add_pin(struct wpa_supplicant * wpa_s,const u8 * addr,const char * uuid,const char * pin)1991 int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
1992 			const char *uuid, const char *pin)
1993 {
1994 	u8 u[UUID_LEN];
1995 	const u8 *use_uuid = NULL;
1996 	u8 addr_buf[ETH_ALEN];
1997 
1998 	if (os_strcmp(uuid, "any") == 0) {
1999 	} else if (uuid_str2bin(uuid, u) == 0) {
2000 		use_uuid = u;
2001 	} else if (hwaddr_aton(uuid, addr_buf) == 0) {
2002 		use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
2003 		if (use_uuid == NULL)
2004 			return -1;
2005 	} else
2006 		return -1;
2007 	return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
2008 				     use_uuid,
2009 				     (const u8 *) pin, os_strlen(pin), 300);
2010 }
2011 
2012 
wpas_wps_er_pbc(struct wpa_supplicant * wpa_s,const char * uuid)2013 int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
2014 {
2015 	u8 u[UUID_LEN], *use_uuid = NULL;
2016 	u8 addr[ETH_ALEN], *use_addr = NULL;
2017 
2018 	if (uuid_str2bin(uuid, u) == 0)
2019 		use_uuid = u;
2020 	else if (hwaddr_aton(uuid, addr) == 0)
2021 		use_addr = addr;
2022 	else
2023 		return -1;
2024 	return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
2025 }
2026 
2027 
wpas_wps_er_learn(struct wpa_supplicant * wpa_s,const char * uuid,const char * pin)2028 int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
2029 		      const char *pin)
2030 {
2031 	u8 u[UUID_LEN], *use_uuid = NULL;
2032 	u8 addr[ETH_ALEN], *use_addr = NULL;
2033 
2034 	if (uuid_str2bin(uuid, u) == 0)
2035 		use_uuid = u;
2036 	else if (hwaddr_aton(uuid, addr) == 0)
2037 		use_addr = addr;
2038 	else
2039 		return -1;
2040 
2041 	return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
2042 			    os_strlen(pin));
2043 }
2044 
2045 
wpas_wps_network_to_cred(struct wpa_ssid * ssid,struct wps_credential * cred)2046 static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
2047 				    struct wps_credential *cred)
2048 {
2049 	os_memset(cred, 0, sizeof(*cred));
2050 	if (ssid->ssid_len > SSID_MAX_LEN)
2051 		return -1;
2052 	os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
2053 	cred->ssid_len = ssid->ssid_len;
2054 	if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
2055 		cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
2056 			WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
2057 		if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
2058 			cred->encr_type = WPS_ENCR_AES;
2059 		else
2060 			cred->encr_type = WPS_ENCR_TKIP;
2061 		if (ssid->passphrase) {
2062 			cred->key_len = os_strlen(ssid->passphrase);
2063 			if (cred->key_len >= 64)
2064 				return -1;
2065 			os_memcpy(cred->key, ssid->passphrase, cred->key_len);
2066 		} else if (ssid->psk_set) {
2067 			cred->key_len = 32;
2068 			os_memcpy(cred->key, ssid->psk, 32);
2069 		} else
2070 			return -1;
2071 	} else {
2072 		cred->auth_type = WPS_AUTH_OPEN;
2073 		cred->encr_type = WPS_ENCR_NONE;
2074 	}
2075 
2076 	return 0;
2077 }
2078 
2079 
wpas_wps_er_set_config(struct wpa_supplicant * wpa_s,const char * uuid,int id)2080 int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
2081 			   int id)
2082 {
2083 	u8 u[UUID_LEN], *use_uuid = NULL;
2084 	u8 addr[ETH_ALEN], *use_addr = NULL;
2085 	struct wpa_ssid *ssid;
2086 	struct wps_credential cred;
2087 	int ret;
2088 
2089 	if (uuid_str2bin(uuid, u) == 0)
2090 		use_uuid = u;
2091 	else if (hwaddr_aton(uuid, addr) == 0)
2092 		use_addr = addr;
2093 	else
2094 		return -1;
2095 	ssid = wpa_config_get_network(wpa_s->conf, id);
2096 	if (ssid == NULL || ssid->ssid == NULL)
2097 		return -1;
2098 
2099 	if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2100 		return -1;
2101 	ret = wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
2102 	os_memset(&cred, 0, sizeof(cred));
2103 	return ret;
2104 }
2105 
2106 
wpas_wps_er_config(struct wpa_supplicant * wpa_s,const char * uuid,const char * pin,struct wps_new_ap_settings * settings)2107 int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
2108 		       const char *pin, struct wps_new_ap_settings *settings)
2109 {
2110 	u8 u[UUID_LEN], *use_uuid = NULL;
2111 	u8 addr[ETH_ALEN], *use_addr = NULL;
2112 	struct wps_credential cred;
2113 	size_t len;
2114 
2115 	if (uuid_str2bin(uuid, u) == 0)
2116 		use_uuid = u;
2117 	else if (hwaddr_aton(uuid, addr) == 0)
2118 		use_addr = addr;
2119 	else
2120 		return -1;
2121 	if (settings->ssid_hex == NULL || settings->auth == NULL ||
2122 	    settings->encr == NULL || settings->key_hex == NULL)
2123 		return -1;
2124 
2125 	os_memset(&cred, 0, sizeof(cred));
2126 	len = os_strlen(settings->ssid_hex);
2127 	if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
2128 	    hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
2129 		return -1;
2130 	cred.ssid_len = len / 2;
2131 
2132 	len = os_strlen(settings->key_hex);
2133 	if ((len & 1) || len > 2 * sizeof(cred.key) ||
2134 	    hexstr2bin(settings->key_hex, cred.key, len / 2))
2135 		return -1;
2136 	cred.key_len = len / 2;
2137 
2138 	if (os_strcmp(settings->auth, "OPEN") == 0)
2139 		cred.auth_type = WPS_AUTH_OPEN;
2140 	else if (os_strcmp(settings->auth, "WPAPSK") == 0)
2141 		cred.auth_type = WPS_AUTH_WPAPSK;
2142 	else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
2143 		cred.auth_type = WPS_AUTH_WPA2PSK;
2144 	else
2145 		return -1;
2146 
2147 	if (os_strcmp(settings->encr, "NONE") == 0)
2148 		cred.encr_type = WPS_ENCR_NONE;
2149 #ifdef CONFIG_TESTING_OPTIONS
2150 	else if (os_strcmp(settings->encr, "WEP") == 0)
2151 		cred.encr_type = WPS_ENCR_WEP;
2152 #endif /* CONFIG_TESTING_OPTIONS */
2153 	else if (os_strcmp(settings->encr, "TKIP") == 0)
2154 		cred.encr_type = WPS_ENCR_TKIP;
2155 	else if (os_strcmp(settings->encr, "CCMP") == 0)
2156 		cred.encr_type = WPS_ENCR_AES;
2157 	else
2158 		return -1;
2159 
2160 	return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
2161 			     (const u8 *) pin, os_strlen(pin), &cred);
2162 }
2163 
2164 
2165 #ifdef CONFIG_WPS_NFC
wpas_wps_er_nfc_config_token(struct wpa_supplicant * wpa_s,int ndef,const char * uuid)2166 struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
2167 					     int ndef, const char *uuid)
2168 {
2169 	struct wpabuf *ret;
2170 	u8 u[UUID_LEN], *use_uuid = NULL;
2171 	u8 addr[ETH_ALEN], *use_addr = NULL;
2172 
2173 	if (!wpa_s->wps_er)
2174 		return NULL;
2175 
2176 	if (uuid_str2bin(uuid, u) == 0)
2177 		use_uuid = u;
2178 	else if (hwaddr_aton(uuid, addr) == 0)
2179 		use_addr = addr;
2180 	else
2181 		return NULL;
2182 
2183 	ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
2184 	if (ndef && ret) {
2185 		struct wpabuf *tmp;
2186 		tmp = ndef_build_wifi(ret);
2187 		wpabuf_free(ret);
2188 		if (tmp == NULL)
2189 			return NULL;
2190 		ret = tmp;
2191 	}
2192 
2193 	return ret;
2194 }
2195 #endif /* CONFIG_WPS_NFC */
2196 
2197 
2198 static int callbacks_pending = 0;
2199 
wpas_wps_terminate_cb(void * ctx)2200 static void wpas_wps_terminate_cb(void *ctx)
2201 {
2202 	wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
2203 	if (--callbacks_pending <= 0)
2204 		eloop_terminate();
2205 }
2206 #endif /* CONFIG_WPS_ER */
2207 
2208 
wpas_wps_terminate_pending(struct wpa_supplicant * wpa_s)2209 int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
2210 {
2211 #ifdef CONFIG_WPS_ER
2212 	if (wpa_s->wps_er) {
2213 		callbacks_pending++;
2214 		wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
2215 		wpa_s->wps_er = NULL;
2216 		return 1;
2217 	}
2218 #endif /* CONFIG_WPS_ER */
2219 	return 0;
2220 }
2221 
2222 
wpas_wps_update_config(struct wpa_supplicant * wpa_s)2223 void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
2224 {
2225 	struct wps_context *wps = wpa_s->wps;
2226 
2227 	if (wps == NULL)
2228 		return;
2229 
2230 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
2231 		wps->config_methods = wps_config_methods_str2bin(
2232 			wpa_s->conf->config_methods);
2233 		if ((wps->config_methods &
2234 		     (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
2235 		    (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
2236 			wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
2237 				   "config methods are not allowed at the "
2238 				   "same time");
2239 			wps->config_methods &= ~WPS_CONFIG_LABEL;
2240 		}
2241 	}
2242 	wps->config_methods = wps_fix_config_methods(wps->config_methods);
2243 	wps->dev.config_methods = wps->config_methods;
2244 
2245 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
2246 		os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
2247 			  WPS_DEV_TYPE_LEN);
2248 
2249 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
2250 		wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2251 		os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
2252 			  wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2253 	}
2254 
2255 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
2256 		wpas_wps_set_vendor_ext_m1(wpa_s, wps);
2257 
2258 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
2259 		wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
2260 
2261 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
2262 		wpas_wps_set_uuid(wpa_s, wps);
2263 
2264 	if (wpa_s->conf->changed_parameters &
2265 	    (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
2266 		/* Update pointers to make sure they refer current values */
2267 		wps->dev.device_name = wpa_s->conf->device_name;
2268 		wps->dev.manufacturer = wpa_s->conf->manufacturer;
2269 		wps->dev.model_name = wpa_s->conf->model_name;
2270 		wps->dev.model_number = wpa_s->conf->model_number;
2271 		wps->dev.serial_number = wpa_s->conf->serial_number;
2272 	}
2273 }
2274 
2275 
wpas_wps_update_mac_addr(struct wpa_supplicant * wpa_s)2276 void wpas_wps_update_mac_addr(struct wpa_supplicant *wpa_s)
2277 {
2278 	struct wps_context *wps;
2279 
2280 	wps = wpa_s->wps;
2281 	if (wps)
2282 		os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
2283 }
2284 
2285 
2286 #ifdef CONFIG_WPS_NFC
2287 
2288 #ifdef CONFIG_WPS_ER
2289 static struct wpabuf *
wpas_wps_network_config_token(struct wpa_supplicant * wpa_s,int ndef,struct wpa_ssid * ssid)2290 wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
2291 			      struct wpa_ssid *ssid)
2292 {
2293 	struct wpabuf *ret;
2294 	struct wps_credential cred;
2295 
2296 	if (wpas_wps_network_to_cred(ssid, &cred) < 0)
2297 		return NULL;
2298 
2299 	ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
2300 
2301 	if (ndef && ret) {
2302 		struct wpabuf *tmp;
2303 		tmp = ndef_build_wifi(ret);
2304 		wpabuf_free(ret);
2305 		if (tmp == NULL)
2306 			return NULL;
2307 		ret = tmp;
2308 	}
2309 
2310 	return ret;
2311 }
2312 #endif /* CONFIG_WPS_ER */
2313 
2314 
wpas_wps_nfc_config_token(struct wpa_supplicant * wpa_s,int ndef,const char * id_str)2315 struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
2316 					  int ndef, const char *id_str)
2317 {
2318 #ifdef CONFIG_WPS_ER
2319 	if (id_str) {
2320 		int id;
2321 		char *end = NULL;
2322 		struct wpa_ssid *ssid;
2323 
2324 		id = strtol(id_str, &end, 10);
2325 		if (end && *end)
2326 			return NULL;
2327 
2328 		ssid = wpa_config_get_network(wpa_s->conf, id);
2329 		if (ssid == NULL)
2330 			return NULL;
2331 		return wpas_wps_network_config_token(wpa_s, ndef, ssid);
2332 	}
2333 #endif /* CONFIG_WPS_ER */
2334 #ifdef CONFIG_AP
2335 	if (wpa_s->ap_iface)
2336 		return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
2337 #endif /* CONFIG_AP */
2338 	return NULL;
2339 }
2340 
2341 
wpas_wps_nfc_token(struct wpa_supplicant * wpa_s,int ndef)2342 struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
2343 {
2344 	if (wpa_s->conf->wps_nfc_pw_from_config) {
2345 		return wps_nfc_token_build(ndef,
2346 					   wpa_s->conf->wps_nfc_dev_pw_id,
2347 					   wpa_s->conf->wps_nfc_dh_pubkey,
2348 					   wpa_s->conf->wps_nfc_dev_pw);
2349 	}
2350 
2351 	return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
2352 				 &wpa_s->conf->wps_nfc_dh_pubkey,
2353 				 &wpa_s->conf->wps_nfc_dh_privkey,
2354 				 &wpa_s->conf->wps_nfc_dev_pw);
2355 }
2356 
2357 
wpas_wps_start_nfc(struct wpa_supplicant * wpa_s,const u8 * go_dev_addr,const u8 * bssid,const struct wpabuf * dev_pw,u16 dev_pw_id,int p2p_group,const u8 * peer_pubkey_hash,const u8 * ssid,size_t ssid_len,int freq)2358 int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *go_dev_addr,
2359 		       const u8 *bssid,
2360 		       const struct wpabuf *dev_pw, u16 dev_pw_id,
2361 		       int p2p_group, const u8 *peer_pubkey_hash,
2362 		       const u8 *ssid, size_t ssid_len, int freq)
2363 {
2364 	struct wps_context *wps = wpa_s->wps;
2365 	char pw[32 * 2 + 1];
2366 
2367 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2368 		dev_pw = wpa_s->conf->wps_nfc_dev_pw;
2369 		dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
2370 	}
2371 
2372 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
2373 	    wpa_s->conf->wps_nfc_dh_privkey == NULL) {
2374 		wpa_printf(MSG_DEBUG, "WPS: Missing DH params - "
2375 			   "cannot start NFC-triggered connection");
2376 		return -1;
2377 	}
2378 
2379 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER && dev_pw == NULL) {
2380 		wpa_printf(MSG_DEBUG, "WPS: Missing Device Password (id=%u) - "
2381 			   "cannot start NFC-triggered connection", dev_pw_id);
2382 		return -1;
2383 	}
2384 
2385 	dh5_free(wps->dh_ctx);
2386 	wpabuf_free(wps->dh_pubkey);
2387 	wpabuf_free(wps->dh_privkey);
2388 	wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2389 	wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2390 	if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
2391 		wps->dh_ctx = NULL;
2392 		wpabuf_free(wps->dh_pubkey);
2393 		wps->dh_pubkey = NULL;
2394 		wpabuf_free(wps->dh_privkey);
2395 		wps->dh_privkey = NULL;
2396 		wpa_printf(MSG_DEBUG, "WPS: Failed to get DH priv/pub key");
2397 		return -1;
2398 	}
2399 	wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
2400 	if (wps->dh_ctx == NULL) {
2401 		wpabuf_free(wps->dh_pubkey);
2402 		wps->dh_pubkey = NULL;
2403 		wpabuf_free(wps->dh_privkey);
2404 		wps->dh_privkey = NULL;
2405 		wpa_printf(MSG_DEBUG, "WPS: Failed to initialize DH context");
2406 		return -1;
2407 	}
2408 
2409 	if (dev_pw) {
2410 		wpa_snprintf_hex_uppercase(pw, sizeof(pw),
2411 					   wpabuf_head(dev_pw),
2412 					   wpabuf_len(dev_pw));
2413 	}
2414 	return wpas_wps_start_dev_pw(wpa_s, go_dev_addr, bssid,
2415 				     dev_pw ? pw : NULL,
2416 				     p2p_group, dev_pw_id, peer_pubkey_hash,
2417 				     ssid, ssid_len, freq);
2418 }
2419 
2420 
wpas_wps_use_cred(struct wpa_supplicant * wpa_s,struct wps_parse_attr * attr)2421 static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
2422 			     struct wps_parse_attr *attr)
2423 {
2424 	/*
2425 	 * Disable existing networks temporarily to allow the newly learned
2426 	 * credential to be preferred. Enable the temporarily disabled networks
2427 	 * after 10 seconds.
2428 	 */
2429 	wpas_wps_temp_disable(wpa_s, NULL);
2430 	eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
2431 			       NULL);
2432 
2433 	if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
2434 		return -1;
2435 
2436 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2437 		return 0;
2438 
2439 	if (attr->ap_channel) {
2440 		u16 chan = WPA_GET_BE16(attr->ap_channel);
2441 		int freq = 0;
2442 
2443 		if (chan >= 1 && chan <= 13)
2444 			freq = 2407 + 5 * chan;
2445 		else if (chan == 14)
2446 			freq = 2484;
2447 		else if (chan >= 30)
2448 			freq = 5000 + 5 * chan;
2449 
2450 		if (freq) {
2451 			wpa_printf(MSG_DEBUG, "WPS: Credential container indicated AP channel %u -> %u MHz",
2452 				   chan, freq);
2453 			wpa_s->after_wps = 5;
2454 			wpa_s->wps_freq = freq;
2455 		}
2456 	}
2457 
2458 	wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
2459 		   "based on the received credential added");
2460 	wpa_s->normal_scans = 0;
2461 	wpa_supplicant_reinit_autoscan(wpa_s);
2462 	wpa_s->disconnected = 0;
2463 	wpa_s->reassociate = 1;
2464 
2465 	wpa_supplicant_cancel_sched_scan(wpa_s);
2466 	wpa_supplicant_req_scan(wpa_s, 0, 0);
2467 
2468 	return 0;
2469 }
2470 
2471 
2472 #ifdef CONFIG_WPS_ER
wpas_wps_add_nfc_password_token(struct wpa_supplicant * wpa_s,struct wps_parse_attr * attr)2473 static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
2474 					   struct wps_parse_attr *attr)
2475 {
2476 	return wps_registrar_add_nfc_password_token(
2477 		wpa_s->wps->registrar, attr->oob_dev_password,
2478 		attr->oob_dev_password_len);
2479 }
2480 #endif /* CONFIG_WPS_ER */
2481 
2482 
wpas_wps_nfc_tag_process(struct wpa_supplicant * wpa_s,const struct wpabuf * wps)2483 static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
2484 				    const struct wpabuf *wps)
2485 {
2486 	struct wps_parse_attr attr;
2487 
2488 	wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2489 
2490 	if (wps_parse_msg(wps, &attr)) {
2491 		wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2492 		return -1;
2493 	}
2494 
2495 	if (attr.num_cred)
2496 		return wpas_wps_use_cred(wpa_s, &attr);
2497 
2498 #ifdef CONFIG_WPS_ER
2499 	if (attr.oob_dev_password)
2500 		return wpas_wps_add_nfc_password_token(wpa_s, &attr);
2501 #endif /* CONFIG_WPS_ER */
2502 
2503 	wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2504 	return -1;
2505 }
2506 
2507 
wpas_wps_nfc_tag_read(struct wpa_supplicant * wpa_s,const struct wpabuf * data,int forced_freq)2508 int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
2509 			  const struct wpabuf *data, int forced_freq)
2510 {
2511 	const struct wpabuf *wps = data;
2512 	struct wpabuf *tmp = NULL;
2513 	int ret;
2514 
2515 	if (wpabuf_len(data) < 4)
2516 		return -1;
2517 
2518 	if (*wpabuf_head_u8(data) != 0x10) {
2519 		/* Assume this contains full NDEF record */
2520 		tmp = ndef_parse_wifi(data);
2521 		if (tmp == NULL) {
2522 #ifdef CONFIG_P2P
2523 			tmp = ndef_parse_p2p(data);
2524 			if (tmp) {
2525 				ret = wpas_p2p_nfc_tag_process(wpa_s, tmp,
2526 							       forced_freq);
2527 				wpabuf_free(tmp);
2528 				return ret;
2529 			}
2530 #endif /* CONFIG_P2P */
2531 			wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2532 			return -1;
2533 		}
2534 		wps = tmp;
2535 	}
2536 
2537 	ret = wpas_wps_nfc_tag_process(wpa_s, wps);
2538 	wpabuf_free(tmp);
2539 	return ret;
2540 }
2541 
2542 
wpas_wps_nfc_handover_req(struct wpa_supplicant * wpa_s,int ndef)2543 struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s,
2544 					  int ndef)
2545 {
2546 	struct wpabuf *ret;
2547 
2548 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
2549 	    wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2550 			   &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2551 		return NULL;
2552 
2553 	ret = wps_build_nfc_handover_req(wpa_s->wps,
2554 					 wpa_s->conf->wps_nfc_dh_pubkey);
2555 
2556 	if (ndef && ret) {
2557 		struct wpabuf *tmp;
2558 		tmp = ndef_build_wifi(ret);
2559 		wpabuf_free(ret);
2560 		if (tmp == NULL)
2561 			return NULL;
2562 		ret = tmp;
2563 	}
2564 
2565 	return ret;
2566 }
2567 
2568 
2569 #ifdef CONFIG_WPS_NFC
2570 
2571 static struct wpabuf *
wpas_wps_er_nfc_handover_sel(struct wpa_supplicant * wpa_s,int ndef,const char * uuid)2572 wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s, int ndef,
2573 			     const char *uuid)
2574 {
2575 #ifdef CONFIG_WPS_ER
2576 	struct wpabuf *ret;
2577 	u8 u[UUID_LEN], *use_uuid = NULL;
2578 	u8 addr[ETH_ALEN], *use_addr = NULL;
2579 	struct wps_context *wps = wpa_s->wps;
2580 
2581 	if (wps == NULL)
2582 		return NULL;
2583 
2584 	if (uuid == NULL)
2585 		return NULL;
2586 	if (uuid_str2bin(uuid, u) == 0)
2587 		use_uuid = u;
2588 	else if (hwaddr_aton(uuid, addr) == 0)
2589 		use_addr = addr;
2590 	else
2591 		return NULL;
2592 
2593 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL) {
2594 		if (wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
2595 				   &wpa_s->conf->wps_nfc_dh_privkey) < 0)
2596 			return NULL;
2597 	}
2598 
2599 	wpas_wps_nfc_clear(wps);
2600 	wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2601 	wps->ap_nfc_dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
2602 	wps->ap_nfc_dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
2603 	if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2604 		wpas_wps_nfc_clear(wps);
2605 		return NULL;
2606 	}
2607 
2608 	ret = wps_er_nfc_handover_sel(wpa_s->wps_er, wpa_s->wps, use_uuid,
2609 				      use_addr, wpa_s->conf->wps_nfc_dh_pubkey);
2610 	if (ndef && ret) {
2611 		struct wpabuf *tmp;
2612 		tmp = ndef_build_wifi(ret);
2613 		wpabuf_free(ret);
2614 		if (tmp == NULL)
2615 			return NULL;
2616 		ret = tmp;
2617 	}
2618 
2619 	return ret;
2620 #else /* CONFIG_WPS_ER */
2621 	return NULL;
2622 #endif /* CONFIG_WPS_ER */
2623 }
2624 #endif /* CONFIG_WPS_NFC */
2625 
2626 
wpas_wps_nfc_handover_sel(struct wpa_supplicant * wpa_s,int ndef,int cr,const char * uuid)2627 struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
2628 					  int ndef, int cr, const char *uuid)
2629 {
2630 	struct wpabuf *ret;
2631 	if (!cr)
2632 		return NULL;
2633 	ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
2634 	if (ret)
2635 		return ret;
2636 	return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
2637 }
2638 
2639 
wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant * wpa_s,const struct wpabuf * data)2640 static int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
2641 					const struct wpabuf *data)
2642 {
2643 	struct wpabuf *wps;
2644 	int ret = -1;
2645 	u16 wsc_len;
2646 	const u8 *pos;
2647 	struct wpabuf msg;
2648 	struct wps_parse_attr attr;
2649 	u16 dev_pw_id;
2650 	const u8 *bssid = NULL;
2651 	int freq = 0;
2652 
2653 	wps = ndef_parse_wifi(data);
2654 	if (wps == NULL)
2655 		return -1;
2656 	wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2657 		   "payload from NFC connection handover");
2658 	wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2659 	if (wpabuf_len(wps) < 2) {
2660 		wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Select "
2661 			   "Message");
2662 		goto out;
2663 	}
2664 	pos = wpabuf_head(wps);
2665 	wsc_len = WPA_GET_BE16(pos);
2666 	if (wsc_len > wpabuf_len(wps) - 2) {
2667 		wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2668 			   "in Wi-Fi Handover Select Message", wsc_len);
2669 		goto out;
2670 	}
2671 	pos += 2;
2672 
2673 	wpa_hexdump(MSG_DEBUG,
2674 		    "WPS: WSC attributes in Wi-Fi Handover Select Message",
2675 		    pos, wsc_len);
2676 	if (wsc_len < wpabuf_len(wps) - 2) {
2677 		wpa_hexdump(MSG_DEBUG,
2678 			    "WPS: Ignore extra data after WSC attributes",
2679 			    pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2680 	}
2681 
2682 	wpabuf_set(&msg, pos, wsc_len);
2683 	ret = wps_parse_msg(&msg, &attr);
2684 	if (ret < 0) {
2685 		wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2686 			   "Wi-Fi Handover Select Message");
2687 		goto out;
2688 	}
2689 
2690 	if (attr.oob_dev_password == NULL ||
2691 	    attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2692 		wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2693 			   "included in Wi-Fi Handover Select Message");
2694 		ret = -1;
2695 		goto out;
2696 	}
2697 
2698 	if (attr.ssid == NULL) {
2699 		wpa_printf(MSG_DEBUG, "WPS: No SSID included in Wi-Fi Handover "
2700 			   "Select Message");
2701 		ret = -1;
2702 		goto out;
2703 	}
2704 
2705 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", attr.ssid, attr.ssid_len);
2706 
2707 	if (attr.mac_addr) {
2708 		bssid = attr.mac_addr;
2709 		wpa_printf(MSG_DEBUG, "WPS: MAC Address (BSSID): " MACSTR,
2710 			   MAC2STR(bssid));
2711 	}
2712 
2713 	if (attr.rf_bands)
2714 		wpa_printf(MSG_DEBUG, "WPS: RF Bands: %d", *attr.rf_bands);
2715 
2716 	if (attr.ap_channel) {
2717 		u16 chan = WPA_GET_BE16(attr.ap_channel);
2718 
2719 		wpa_printf(MSG_DEBUG, "WPS: AP Channel: %d", chan);
2720 
2721 		if (chan >= 1 && chan <= 13 &&
2722 		    (attr.rf_bands == NULL || *attr.rf_bands & WPS_RF_24GHZ))
2723 			freq = 2407 + 5 * chan;
2724 		else if (chan == 14 &&
2725 			 (attr.rf_bands == NULL ||
2726 			  *attr.rf_bands & WPS_RF_24GHZ))
2727 			freq = 2484;
2728 		else if (chan >= 30 &&
2729 			 (attr.rf_bands == NULL ||
2730 			  *attr.rf_bands & WPS_RF_50GHZ))
2731 			freq = 5000 + 5 * chan;
2732 		else if (chan >= 1 && chan <= 6 &&
2733 			 (attr.rf_bands == NULL ||
2734 			  *attr.rf_bands & WPS_RF_60GHZ))
2735 			freq = 56160 + 2160 * chan;
2736 
2737 		if (freq) {
2738 			wpa_printf(MSG_DEBUG,
2739 				   "WPS: AP indicated channel %u -> %u MHz",
2740 				   chan, freq);
2741 		}
2742 	}
2743 
2744 	wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2745 		    attr.oob_dev_password, attr.oob_dev_password_len);
2746 	dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2747 				 WPS_OOB_PUBKEY_HASH_LEN);
2748 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2749 		wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2750 			   "%u in Wi-Fi Handover Select Message", dev_pw_id);
2751 		ret = -1;
2752 		goto out;
2753 	}
2754 	wpa_hexdump(MSG_DEBUG, "WPS: AP Public Key hash",
2755 		    attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2756 
2757 	ret = wpas_wps_start_nfc(wpa_s, NULL, bssid, NULL, dev_pw_id, 0,
2758 				 attr.oob_dev_password,
2759 				 attr.ssid, attr.ssid_len, freq);
2760 
2761 out:
2762 	wpabuf_free(wps);
2763 	return ret;
2764 }
2765 
2766 
wpas_wps_nfc_report_handover(struct wpa_supplicant * wpa_s,const struct wpabuf * req,const struct wpabuf * sel)2767 int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2768 				 const struct wpabuf *req,
2769 				 const struct wpabuf *sel)
2770 {
2771 	wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
2772 	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
2773 	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
2774 	return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
2775 }
2776 
2777 
wpas_er_wps_nfc_report_handover(struct wpa_supplicant * wpa_s,const struct wpabuf * req,const struct wpabuf * sel)2778 int wpas_er_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
2779 				    const struct wpabuf *req,
2780 				    const struct wpabuf *sel)
2781 {
2782 	struct wpabuf *wps;
2783 	int ret = -1;
2784 	u16 wsc_len;
2785 	const u8 *pos;
2786 	struct wpabuf msg;
2787 	struct wps_parse_attr attr;
2788 	u16 dev_pw_id;
2789 
2790 	/*
2791 	 * Enrollee/station is always initiator of the NFC connection handover,
2792 	 * so use the request message here to find Enrollee public key hash.
2793 	 */
2794 	wps = ndef_parse_wifi(req);
2795 	if (wps == NULL)
2796 		return -1;
2797 	wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2798 		   "payload from NFC connection handover");
2799 	wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2800 	if (wpabuf_len(wps) < 2) {
2801 		wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2802 			   "Message");
2803 		goto out;
2804 	}
2805 	pos = wpabuf_head(wps);
2806 	wsc_len = WPA_GET_BE16(pos);
2807 	if (wsc_len > wpabuf_len(wps) - 2) {
2808 		wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2809 			   "in rt Wi-Fi Handover Request Message", wsc_len);
2810 		goto out;
2811 	}
2812 	pos += 2;
2813 
2814 	wpa_hexdump(MSG_DEBUG,
2815 		    "WPS: WSC attributes in Wi-Fi Handover Request Message",
2816 		    pos, wsc_len);
2817 	if (wsc_len < wpabuf_len(wps) - 2) {
2818 		wpa_hexdump(MSG_DEBUG,
2819 			    "WPS: Ignore extra data after WSC attributes",
2820 			    pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2821 	}
2822 
2823 	wpabuf_set(&msg, pos, wsc_len);
2824 	ret = wps_parse_msg(&msg, &attr);
2825 	if (ret < 0) {
2826 		wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2827 			   "Wi-Fi Handover Request Message");
2828 		goto out;
2829 	}
2830 
2831 	if (attr.oob_dev_password == NULL ||
2832 	    attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2833 		wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2834 			   "included in Wi-Fi Handover Request Message");
2835 		ret = -1;
2836 		goto out;
2837 	}
2838 
2839 	if (attr.uuid_e == NULL) {
2840 		wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2841 			   "Handover Request Message");
2842 		ret = -1;
2843 		goto out;
2844 	}
2845 
2846 	wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2847 
2848 	wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2849 		    attr.oob_dev_password, attr.oob_dev_password_len);
2850 	dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2851 				 WPS_OOB_PUBKEY_HASH_LEN);
2852 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2853 		wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2854 			   "%u in Wi-Fi Handover Request Message", dev_pw_id);
2855 		ret = -1;
2856 		goto out;
2857 	}
2858 	wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2859 		    attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2860 
2861 	ret = wps_registrar_add_nfc_pw_token(wpa_s->wps->registrar,
2862 					     attr.oob_dev_password,
2863 					     DEV_PW_NFC_CONNECTION_HANDOVER,
2864 					     NULL, 0, 1);
2865 
2866 out:
2867 	wpabuf_free(wps);
2868 	return ret;
2869 }
2870 
2871 #endif /* CONFIG_WPS_NFC */
2872 
2873 
wpas_wps_dump_ap_info(struct wpa_supplicant * wpa_s)2874 static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
2875 {
2876 	size_t i;
2877 	struct os_reltime now;
2878 
2879 	if (wpa_debug_level > MSG_DEBUG)
2880 		return;
2881 
2882 	if (wpa_s->wps_ap == NULL)
2883 		return;
2884 
2885 	os_get_reltime(&now);
2886 
2887 	for (i = 0; i < wpa_s->num_wps_ap; i++) {
2888 		struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2889 		struct wpa_bssid_ignore *e = wpa_bssid_ignore_get(wpa_s,
2890 								  ap->bssid);
2891 
2892 		wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
2893 			   "tries=%d last_attempt=%d sec ago bssid_ignore=%d",
2894 			   (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
2895 			   ap->last_attempt.sec > 0 ?
2896 			   (int) now.sec - (int) ap->last_attempt.sec : -1,
2897 			   e ? e->count : 0);
2898 	}
2899 }
2900 
2901 
wpas_wps_get_ap_info(struct wpa_supplicant * wpa_s,const u8 * bssid)2902 static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
2903 						 const u8 *bssid)
2904 {
2905 	size_t i;
2906 
2907 	if (wpa_s->wps_ap == NULL)
2908 		return NULL;
2909 
2910 	for (i = 0; i < wpa_s->num_wps_ap; i++) {
2911 		struct wps_ap_info *ap = &wpa_s->wps_ap[i];
2912 		if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
2913 			return ap;
2914 	}
2915 
2916 	return NULL;
2917 }
2918 
2919 
wpas_wps_update_ap_info_bss(struct wpa_supplicant * wpa_s,struct wpa_scan_res * res)2920 static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
2921 					struct wpa_scan_res *res)
2922 {
2923 	struct wpabuf *wps;
2924 	enum wps_ap_info_type type;
2925 	struct wps_ap_info *ap;
2926 	int r, pbc_active;
2927 	const u8 *uuid;
2928 
2929 	if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
2930 		return;
2931 
2932 	wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
2933 	if (wps == NULL)
2934 		return;
2935 
2936 	r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
2937 	if (r == 2)
2938 		type = WPS_AP_SEL_REG_OUR;
2939 	else if (r == 1)
2940 		type = WPS_AP_SEL_REG;
2941 	else
2942 		type = WPS_AP_NOT_SEL_REG;
2943 
2944 	uuid = wps_get_uuid_e(wps);
2945 	pbc_active = wps_is_selected_pbc_registrar(wps);
2946 
2947 	ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
2948 	if (ap) {
2949 		if (ap->type != type) {
2950 			wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
2951 				   " changed type %d -> %d",
2952 				   MAC2STR(res->bssid), ap->type, type);
2953 			ap->type = type;
2954 			if (type != WPS_AP_NOT_SEL_REG)
2955 				wpa_bssid_ignore_del(wpa_s, ap->bssid);
2956 		}
2957 		ap->pbc_active = pbc_active;
2958 		if (uuid)
2959 			os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
2960 		goto out;
2961 	}
2962 
2963 	ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
2964 			      sizeof(struct wps_ap_info));
2965 	if (ap == NULL)
2966 		goto out;
2967 
2968 	wpa_s->wps_ap = ap;
2969 	ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
2970 	wpa_s->num_wps_ap++;
2971 
2972 	os_memset(ap, 0, sizeof(*ap));
2973 	os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
2974 	ap->type = type;
2975 	ap->pbc_active = pbc_active;
2976 	if (uuid)
2977 		os_memcpy(ap->uuid, uuid, WPS_UUID_LEN);
2978 	wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
2979 		   MAC2STR(ap->bssid), ap->type);
2980 
2981 out:
2982 	wpabuf_free(wps);
2983 }
2984 
2985 
wpas_wps_update_ap_info(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)2986 void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
2987 			     struct wpa_scan_results *scan_res)
2988 {
2989 	size_t i;
2990 
2991 	for (i = 0; i < scan_res->num; i++)
2992 		wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
2993 
2994 	wpas_wps_dump_ap_info(wpa_s);
2995 }
2996 
2997 
wpas_wps_notify_assoc(struct wpa_supplicant * wpa_s,const u8 * bssid)2998 void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
2999 {
3000 	struct wps_ap_info *ap;
3001 
3002 	wpa_s->after_wps = 0;
3003 
3004 	if (!wpa_s->wps_ap_iter)
3005 		return;
3006 	ap = wpas_wps_get_ap_info(wpa_s, bssid);
3007 	if (ap == NULL)
3008 		return;
3009 	ap->tries++;
3010 	os_get_reltime(&ap->last_attempt);
3011 }
3012