• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd / WPS integration
3  * Copyright (c) 2008-2016, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/uuid.h"
14 #include "common/wpa_ctrl.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ieee802_11_common.h"
17 #include "eapol_auth/eapol_auth_sm.h"
18 #include "eapol_auth/eapol_auth_sm_i.h"
19 #include "wps/wps.h"
20 #include "wps/wps_defs.h"
21 #include "wps/wps_dev_attr.h"
22 #include "wps/wps_attr_parse.h"
23 #include "hostapd.h"
24 #include "ap_config.h"
25 #include "ap_drv_ops.h"
26 #include "beacon.h"
27 #include "sta_info.h"
28 #include "wps_hostapd.h"
29 
30 
31 #ifdef CONFIG_WPS_UPNP
32 #include "wps/wps_upnp.h"
33 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
34 				 struct wps_context *wps);
35 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
36 #endif /* CONFIG_WPS_UPNP */
37 
38 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
39 				    const u8 *bssid,
40 				    const u8 *ie, size_t ie_len,
41 				    int ssi_signal);
42 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
43 static void hostapd_wps_nfc_clear(struct wps_context *wps);
44 
45 
46 struct wps_for_each_data {
47 	int (*func)(struct hostapd_data *h, void *ctx);
48 	void *ctx;
49 	struct hostapd_data *calling_hapd;
50 };
51 
52 
wps_for_each(struct hostapd_iface * iface,void * ctx)53 static int wps_for_each(struct hostapd_iface *iface, void *ctx)
54 {
55 	struct wps_for_each_data *data = ctx;
56 	size_t j;
57 
58 	if (iface == NULL)
59 		return 0;
60 	for (j = 0; j < iface->num_bss; j++) {
61 		struct hostapd_data *hapd = iface->bss[j];
62 		int ret;
63 
64 		if (hapd != data->calling_hapd &&
65 		    (hapd->conf->wps_independent ||
66 		     data->calling_hapd->conf->wps_independent))
67 			continue;
68 
69 		ret = data->func(hapd, data->ctx);
70 		if (ret)
71 			return ret;
72 	}
73 
74 	return 0;
75 }
76 
77 
hostapd_wps_for_each(struct hostapd_data * hapd,int (* func)(struct hostapd_data * h,void * ctx),void * ctx)78 static int hostapd_wps_for_each(struct hostapd_data *hapd,
79 				int (*func)(struct hostapd_data *h, void *ctx),
80 				void *ctx)
81 {
82 	struct hostapd_iface *iface = hapd->iface;
83 	struct wps_for_each_data data;
84 	data.func = func;
85 	data.ctx = ctx;
86 	data.calling_hapd = hapd;
87 	if (iface->interfaces == NULL ||
88 	    iface->interfaces->for_each_interface == NULL)
89 		return wps_for_each(iface, &data);
90 	return iface->interfaces->for_each_interface(iface->interfaces,
91 						     wps_for_each, &data);
92 }
93 
94 
hostapd_wps_new_psk_cb(void * ctx,const u8 * mac_addr,const u8 * p2p_dev_addr,const u8 * psk,size_t psk_len)95 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
96 				  const u8 *p2p_dev_addr, const u8 *psk,
97 				  size_t psk_len)
98 {
99 	struct hostapd_data *hapd = ctx;
100 	struct hostapd_wpa_psk *p;
101 	struct hostapd_ssid *ssid = &hapd->conf->ssid;
102 
103 	if (is_zero_ether_addr(p2p_dev_addr)) {
104 		wpa_printf(MSG_DEBUG,
105 			   "Received new WPA/WPA2-PSK from WPS for STA " MACSTR_SEC,
106 			   MAC2STR_SEC(mac_addr));
107 	} else {
108 		wpa_printf(MSG_DEBUG,
109 			   "Received new WPA/WPA2-PSK from WPS for STA " MACSTR_SEC
110 			   " P2P Device Addr " MACSTR_SEC,
111 			   MAC2STR_SEC(mac_addr), MAC2STR_SEC(p2p_dev_addr));
112 	}
113 	wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
114 
115 	if (psk_len != PMK_LEN) {
116 		wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
117 			   (unsigned long) psk_len);
118 		return -1;
119 	}
120 
121 	/* Add the new PSK to runtime PSK list */
122 	p = os_zalloc(sizeof(*p));
123 	if (p == NULL)
124 		return -1;
125 	os_memcpy(p->addr, mac_addr, ETH_ALEN);
126 	os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
127 	os_memcpy(p->psk, psk, PMK_LEN);
128 	p->wps = 1;
129 
130 	if (hapd->new_psk_cb) {
131 		hapd->new_psk_cb(hapd->new_psk_cb_ctx, mac_addr, p2p_dev_addr,
132 				 psk, psk_len);
133 	}
134 
135 	p->next = ssid->wpa_psk;
136 	ssid->wpa_psk = p;
137 
138 	if (ssid->wpa_psk_file) {
139 		FILE *f;
140 		char hex[PMK_LEN * 2 + 1];
141 
142 		/* Add the new PSK to PSK list file */
143 		f = fopen(ssid->wpa_psk_file, "a");
144 		if (!f) {
145 			wpa_printf(MSG_DEBUG, "Failed to add the PSK to '%s'",
146 				   ssid->wpa_psk_file);
147 			return -1;
148 		}
149 
150 		wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
151 		fprintf(f, "wps=1 " MACSTR " %s\n", MAC2STR(mac_addr), hex);
152 		fclose(f);
153 	}
154 
155 	return 0;
156 }
157 
158 
hostapd_wps_set_ie_cb(void * ctx,struct wpabuf * beacon_ie,struct wpabuf * probe_resp_ie)159 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
160 				 struct wpabuf *probe_resp_ie)
161 {
162 	struct hostapd_data *hapd = ctx;
163 	wpabuf_free(hapd->wps_beacon_ie);
164 	hapd->wps_beacon_ie = beacon_ie;
165 	wpabuf_free(hapd->wps_probe_resp_ie);
166 	hapd->wps_probe_resp_ie = probe_resp_ie;
167 	if (hapd->beacon_set_done)
168 		ieee802_11_set_beacon(hapd);
169 	return hostapd_set_ap_wps_ie(hapd);
170 }
171 
172 
hostapd_wps_pin_needed_cb(void * ctx,const u8 * uuid_e,const struct wps_device_data * dev)173 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
174 				      const struct wps_device_data *dev)
175 {
176 	struct hostapd_data *hapd = ctx;
177 	char uuid[40], txt[400];
178 	int len;
179 	char devtype[WPS_DEV_TYPE_BUFSIZE];
180 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
181 		return;
182 	wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", anonymize_common(uuid));
183 	len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
184 			  "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
185 			  uuid, MAC2STR(dev->mac_addr), dev->device_name,
186 			  dev->manufacturer, dev->model_name,
187 			  dev->model_number, dev->serial_number,
188 			  wps_dev_type_bin2str(dev->pri_dev_type, devtype,
189 					       sizeof(devtype)));
190 	if (!os_snprintf_error(sizeof(txt), len))
191 		wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
192 
193 	if (hapd->conf->wps_pin_requests) {
194 		FILE *f;
195 		struct os_time t;
196 		f = fopen(hapd->conf->wps_pin_requests, "a");
197 		if (f == NULL)
198 			return;
199 		os_get_time(&t);
200 		fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
201 			"\t%s\n",
202 			t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
203 			dev->manufacturer, dev->model_name, dev->model_number,
204 			dev->serial_number,
205 			wps_dev_type_bin2str(dev->pri_dev_type, devtype,
206 					     sizeof(devtype)));
207 		fclose(f);
208 	}
209 }
210 
211 
212 struct wps_stop_reg_data {
213 	struct hostapd_data *current_hapd;
214 	const u8 *uuid_e;
215 	const u8 *dev_pw;
216 	size_t dev_pw_len;
217 };
218 
wps_stop_registrar(struct hostapd_data * hapd,void * ctx)219 static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
220 {
221 	struct wps_stop_reg_data *data = ctx;
222 	if (hapd != data->current_hapd && hapd->wps != NULL)
223 		wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
224 				       data->dev_pw, data->dev_pw_len);
225 	return 0;
226 }
227 
228 
hostapd_wps_reg_success_cb(void * ctx,const u8 * mac_addr,const u8 * uuid_e,const u8 * dev_pw,size_t dev_pw_len)229 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
230 				       const u8 *uuid_e, const u8 *dev_pw,
231 				       size_t dev_pw_len)
232 {
233 	struct hostapd_data *hapd = ctx;
234 	char uuid[40];
235 	struct wps_stop_reg_data data;
236 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
237 		return;
238 	wpa_msg_only_for_cb(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
239 		MAC2STR(mac_addr), uuid);
240 	wpa_printf(MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR_SEC " %s", MAC2STR_SEC(mac_addr), anonymize_common(uuid));
241 	if (hapd->wps_reg_success_cb)
242 		hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
243 					 mac_addr, uuid_e);
244 	data.current_hapd = hapd;
245 	data.uuid_e = uuid_e;
246 	data.dev_pw = dev_pw;
247 	data.dev_pw_len = dev_pw_len;
248 	hostapd_wps_for_each(hapd, wps_stop_registrar, &data);
249 }
250 
251 
hostapd_wps_enrollee_seen_cb(void * ctx,const u8 * addr,const u8 * uuid_e,const u8 * pri_dev_type,u16 config_methods,u16 dev_password_id,u8 request_type,const char * dev_name)252 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
253 					 const u8 *uuid_e,
254 					 const u8 *pri_dev_type,
255 					 u16 config_methods,
256 					 u16 dev_password_id, u8 request_type,
257 					 const char *dev_name)
258 {
259 	struct hostapd_data *hapd = ctx;
260 	char uuid[40];
261 	char devtype[WPS_DEV_TYPE_BUFSIZE];
262 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
263 		return;
264 	if (dev_name == NULL)
265 		dev_name = "";
266 	wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
267 		     " %s %s 0x%x %u %u [%s]",
268 		     MAC2STR(addr), uuid,
269 		     wps_dev_type_bin2str(pri_dev_type, devtype,
270 					  sizeof(devtype)),
271 		     config_methods, dev_password_id, request_type, dev_name);
272 }
273 
274 
hostapd_wps_lookup_pskfile_cb(void * ctx,const u8 * mac_addr,const u8 ** psk)275 static int hostapd_wps_lookup_pskfile_cb(void *ctx, const u8 *mac_addr,
276 					 const u8 **psk)
277 {
278 	const struct hostapd_data *hapd = ctx;
279 	const struct hostapd_wpa_psk *wpa_psk;
280 	const u8 *any_psk = NULL;
281 	const u8 *dev_psk = NULL;
282 
283 	for (wpa_psk = hapd->conf->ssid.wpa_psk; wpa_psk;
284 	     wpa_psk = wpa_psk->next) {
285 		if (!wpa_psk->wps)
286 			continue;
287 
288 		if (!any_psk && is_zero_ether_addr(wpa_psk->addr))
289 			any_psk = wpa_psk->psk;
290 
291 		if (mac_addr && !dev_psk &&
292 		    ether_addr_equal(mac_addr, wpa_psk->addr)) {
293 			dev_psk = wpa_psk->psk;
294 			break;
295 		}
296 	}
297 
298 	if (dev_psk) {
299 		*psk = dev_psk;
300 	} else if (any_psk) {
301 		*psk = any_psk;
302 	} else {
303 		*psk = NULL;
304 		wpa_printf(MSG_DEBUG,
305 			   "WPS: No appropriate PSK in wpa_psk_file");
306 		return 0;
307 	}
308 
309 	return 1;
310 }
311 
312 
wps_reload_config(void * eloop_data,void * user_ctx)313 static void wps_reload_config(void *eloop_data, void *user_ctx)
314 {
315 	struct hostapd_iface *iface = eloop_data;
316 
317 	wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
318 	if (iface->interfaces == NULL ||
319 	    iface->interfaces->reload_config(iface) < 0) {
320 		wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
321 			   "configuration");
322 	}
323 }
324 
325 
hostapd_wps_eap_completed(struct hostapd_data * hapd)326 void hostapd_wps_eap_completed(struct hostapd_data *hapd)
327 {
328 	/*
329 	 * Reduce race condition of the station trying to reconnect immediately
330 	 * after AP reconfiguration through WPS by rescheduling the reload
331 	 * timeout to happen after EAP completion rather than the originally
332 	 * scheduled 100 ms after new configuration became known.
333 	 */
334 	if (eloop_deplete_timeout(0, 0, wps_reload_config, hapd->iface, NULL) ==
335 	    1)
336 		wpa_printf(MSG_DEBUG, "WPS: Reschedule immediate configuration reload");
337 }
338 
339 
hapd_new_ap_event(struct hostapd_data * hapd,const u8 * attr,size_t attr_len)340 static void hapd_new_ap_event(struct hostapd_data *hapd, const u8 *attr,
341 			      size_t attr_len)
342 {
343 	size_t blen = attr_len * 2 + 1;
344 	char *buf = os_malloc(blen);
345 	if (buf) {
346 		wpa_snprintf_hex(buf, blen, attr, attr_len);
347 		wpa_msg(hapd->msg_ctx, MSG_INFO,
348 			WPS_EVENT_NEW_AP_SETTINGS "%s", buf);
349 		os_free(buf);
350 	}
351 }
352 
353 
hapd_wps_reconfig_in_memory(struct hostapd_data * hapd,const struct wps_credential * cred)354 static int hapd_wps_reconfig_in_memory(struct hostapd_data *hapd,
355 				       const struct wps_credential *cred)
356 {
357 	struct hostapd_bss_config *bss = hapd->conf;
358 
359 	wpa_printf(MSG_DEBUG, "WPS: Updating in-memory configuration");
360 
361 	bss->wps_state = 2;
362 	if (cred->ssid_len <= SSID_MAX_LEN) {
363 		os_memcpy(bss->ssid.ssid, cred->ssid, cred->ssid_len);
364 		bss->ssid.ssid_len = cred->ssid_len;
365 		bss->ssid.ssid_set = 1;
366 	}
367 
368 #ifdef CONFIG_NO_TKIP
369 	if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK |
370 			       WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
371 		bss->wpa = 2;
372 	else
373 		bss->wpa = 0;
374 #else /* CONFIG_NO_TKIP */
375 	if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
376 	    (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
377 		bss->wpa = 3;
378 	else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
379 		bss->wpa = 2;
380 	else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
381 		bss->wpa = 1;
382 	else
383 		bss->wpa = 0;
384 #endif /* CONFIG_NO_TKIP */
385 
386 	if (bss->wpa) {
387 		if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA))
388 			bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
389 		if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
390 			bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
391 
392 		bss->wpa_pairwise = 0;
393 		if (cred->encr_type & WPS_ENCR_AES) {
394 			if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
395 				bss->wpa_pairwise |= WPA_CIPHER_GCMP;
396 			else
397 				bss->wpa_pairwise |= WPA_CIPHER_CCMP;
398 		}
399 #ifndef CONFIG_NO_TKIP
400 		if (cred->encr_type & WPS_ENCR_TKIP)
401 			bss->wpa_pairwise |= WPA_CIPHER_TKIP;
402 #endif /* CONFIG_NO_TKIP */
403 		bss->rsn_pairwise = bss->wpa_pairwise;
404 		bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa,
405 							    bss->wpa_pairwise,
406 							    bss->rsn_pairwise);
407 
408 		if (hapd->conf->wps_cred_add_sae &&
409 		    (cred->auth_type & WPS_AUTH_WPA2PSK) &&
410 		    cred->key_len != 2 * PMK_LEN) {
411 			bss->wpa_key_mgmt |= WPA_KEY_MGMT_SAE;
412 			if (bss->ieee80211w == NO_MGMT_FRAME_PROTECTION)
413 				bss->ieee80211w =
414 					MGMT_FRAME_PROTECTION_OPTIONAL;
415 			bss->sae_require_mfp = 1;
416 		}
417 
418 		if (cred->key_len >= 8 && cred->key_len < 64) {
419 			os_free(bss->ssid.wpa_passphrase);
420 			bss->ssid.wpa_passphrase = os_zalloc(cred->key_len + 1);
421 			if (bss->ssid.wpa_passphrase)
422 				os_memcpy(bss->ssid.wpa_passphrase, cred->key,
423 					  cred->key_len);
424 			hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
425 		} else if (cred->key_len == 64) {
426 			hostapd_config_clear_wpa_psk(&bss->ssid.wpa_psk);
427 			bss->ssid.wpa_psk =
428 				os_zalloc(sizeof(struct hostapd_wpa_psk));
429 			if (bss->ssid.wpa_psk &&
430 			    hexstr2bin((const char *) cred->key,
431 				       bss->ssid.wpa_psk->psk, PMK_LEN) == 0) {
432 				bss->ssid.wpa_psk->group = 1;
433 				os_free(bss->ssid.wpa_passphrase);
434 				bss->ssid.wpa_passphrase = NULL;
435 			}
436 		}
437 		bss->auth_algs = 1;
438 	} else {
439 		/*
440 		 * WPS 2.0 does not allow WEP to be configured, so no need to
441 		 * process that option here either.
442 		 */
443 		bss->auth_algs = 1;
444 	}
445 
446 	/* Schedule configuration reload after short period of time to allow
447 	 * EAP-WSC to be finished.
448 	 */
449 	eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
450 			       NULL);
451 
452 	return 0;
453 }
454 
455 
hapd_wps_cred_cb(struct hostapd_data * hapd,void * ctx)456 static int hapd_wps_cred_cb(struct hostapd_data *hapd, void *ctx)
457 {
458 	const struct wps_credential *cred = ctx;
459 	FILE *oconf, *nconf;
460 	size_t len, i;
461 	char *tmp_fname;
462 	char buf[1024];
463 	int multi_bss;
464 	int wpa;
465 	int pmf_changed = 0;
466 
467 	if (hapd->wps == NULL)
468 		return 0;
469 
470 	wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
471 			cred->cred_attr, cred->cred_attr_len);
472 
473 	wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
474 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
475 	wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
476 		   cred->auth_type);
477 	wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
478 	wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
479 	wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
480 			cred->key, cred->key_len);
481 	wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR_SEC,
482 		   MAC2STR_SEC(cred->mac_addr));
483 
484 	if ((hapd->conf->wps_cred_processing == 1 ||
485 	     hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
486 		hapd_new_ap_event(hapd, cred->cred_attr, cred->cred_attr_len);
487 	} else if (hapd->conf->wps_cred_processing == 1 ||
488 		   hapd->conf->wps_cred_processing == 2) {
489 		struct wpabuf *attr;
490 		attr = wpabuf_alloc(200);
491 		if (attr && wps_build_credential_wrap(attr, cred) == 0)
492 			hapd_new_ap_event(hapd, wpabuf_head_u8(attr),
493 					  wpabuf_len(attr));
494 		wpabuf_free(attr);
495 	} else
496 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
497 
498 	if (hapd->conf->wps_cred_processing == 1)
499 		return 0;
500 
501 	os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
502 	hapd->wps->ssid_len = cred->ssid_len;
503 	hapd->wps->encr_types = cred->encr_type;
504 	hapd->wps->encr_types_rsn = cred->encr_type;
505 	hapd->wps->encr_types_wpa = cred->encr_type;
506 	hapd->wps->auth_types = cred->auth_type;
507 	hapd->wps->ap_encr_type = cred->encr_type;
508 	hapd->wps->ap_auth_type = cred->auth_type;
509 	if (cred->key_len == 0) {
510 		os_free(hapd->wps->network_key);
511 		hapd->wps->network_key = NULL;
512 		hapd->wps->network_key_len = 0;
513 	} else if ((cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) &&
514 		   (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN)) {
515 		wpa_printf(MSG_INFO, "WPS: Invalid key length %lu for WPA/WPA2",
516 			   (unsigned long) cred->key_len);
517 		return -1;
518 	} else {
519 		if (hapd->wps->network_key == NULL ||
520 		    hapd->wps->network_key_len < cred->key_len) {
521 			hapd->wps->network_key_len = 0;
522 			os_free(hapd->wps->network_key);
523 			hapd->wps->network_key = os_malloc(cred->key_len);
524 			if (hapd->wps->network_key == NULL)
525 				return -1;
526 		}
527 		hapd->wps->network_key_len = cred->key_len;
528 		os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
529 	}
530 	hapd->wps->wps_state = WPS_STATE_CONFIGURED;
531 
532 	if (hapd->iface->config_fname == NULL)
533 		return hapd_wps_reconfig_in_memory(hapd, cred);
534 	len = os_strlen(hapd->iface->config_fname) + 5;
535 	tmp_fname = os_malloc(len);
536 	if (tmp_fname == NULL)
537 		return -1;
538 	os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
539 
540 	oconf = fopen(hapd->iface->config_fname, "r");
541 	if (oconf == NULL) {
542 		wpa_printf(MSG_WARNING, "WPS: Could not open current "
543 			   "configuration file");
544 		os_free(tmp_fname);
545 		return -1;
546 	}
547 
548 	nconf = fopen(tmp_fname, "w");
549 	if (nconf == NULL) {
550 		wpa_printf(MSG_WARNING, "WPS: Could not write updated "
551 			   "configuration file");
552 		os_free(tmp_fname);
553 		fclose(oconf);
554 		return -1;
555 	}
556 
557 	fprintf(nconf, "# WPS configuration - START\n");
558 
559 	fprintf(nconf, "wps_state=2\n");
560 
561 	if (is_hex(cred->ssid, cred->ssid_len)) {
562 		fprintf(nconf, "ssid2=");
563 		for (i = 0; i < cred->ssid_len; i++)
564 			fprintf(nconf, "%02x", cred->ssid[i]);
565 		fprintf(nconf, "\n");
566 	} else {
567 		fprintf(nconf, "ssid=");
568 		for (i = 0; i < cred->ssid_len; i++)
569 			fputc(cred->ssid[i], nconf);
570 		fprintf(nconf, "\n");
571 	}
572 
573 #ifdef CONFIG_NO_TKIP
574 	if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK |
575 			       WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
576 		wpa = 2;
577 	else
578 		wpa = 0;
579 #else /* CONFIG_NO_TKIP */
580 	if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
581 	    (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
582 		wpa = 3;
583 	else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
584 		wpa = 2;
585 	else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
586 		wpa = 1;
587 	else
588 		wpa = 0;
589 #endif /* CONFIG_NO_TKIP */
590 
591 	if (wpa) {
592 		char *prefix;
593 		int sae = 0;
594 
595 		fprintf(nconf, "wpa=%d\n", wpa);
596 
597 		fprintf(nconf, "wpa_key_mgmt=");
598 		prefix = "";
599 		if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
600 			fprintf(nconf, "WPA-EAP");
601 			prefix = " ";
602 		}
603 		if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) {
604 			fprintf(nconf, "%sWPA-PSK", prefix);
605 			prefix = " ";
606 		}
607 		if (hapd->conf->wps_cred_add_sae &&
608 		    (cred->auth_type & WPS_AUTH_WPA2PSK) &&
609 		    cred->key_len != 2 * PMK_LEN) {
610 			fprintf(nconf, "%sSAE", prefix);
611 			sae = 1;
612 		}
613 		fprintf(nconf, "\n");
614 
615 		if (sae && hapd->conf->ieee80211w == NO_MGMT_FRAME_PROTECTION) {
616 			fprintf(nconf, "ieee80211w=%d\n",
617 				MGMT_FRAME_PROTECTION_OPTIONAL);
618 			pmf_changed = 1;
619 		}
620 		if (sae)
621 			fprintf(nconf, "sae_require_mfp=1\n");
622 
623 		fprintf(nconf, "wpa_pairwise=");
624 		prefix = "";
625 		if (cred->encr_type & WPS_ENCR_AES) {
626 			if (hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
627 				fprintf(nconf, "GCMP");
628 			else
629 				fprintf(nconf, "CCMP");
630 
631 			prefix = " ";
632 		}
633 #ifndef CONFIG_NO_TKIP
634 		if (cred->encr_type & WPS_ENCR_TKIP) {
635 			fprintf(nconf, "%sTKIP", prefix);
636 		}
637 #endif /* CONFIG_NO_TKIP */
638 		fprintf(nconf, "\n");
639 
640 		if (cred->key_len >= 8 && cred->key_len < 64) {
641 			fprintf(nconf, "wpa_passphrase=");
642 			for (i = 0; i < cred->key_len; i++)
643 				fputc(cred->key[i], nconf);
644 			fprintf(nconf, "\n");
645 		} else if (cred->key_len == 64) {
646 			fprintf(nconf, "wpa_psk=");
647 			for (i = 0; i < cred->key_len; i++)
648 				fputc(cred->key[i], nconf);
649 			fprintf(nconf, "\n");
650 		} else {
651 			wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
652 				   "for WPA/WPA2",
653 				   (unsigned long) cred->key_len);
654 		}
655 
656 		fprintf(nconf, "auth_algs=1\n");
657 	} else {
658 		/*
659 		 * WPS 2.0 does not allow WEP to be configured, so no need to
660 		 * process that option here either.
661 		 */
662 		fprintf(nconf, "auth_algs=1\n");
663 	}
664 
665 	fprintf(nconf, "# WPS configuration - END\n");
666 
667 	multi_bss = 0;
668 	while (fgets(buf, sizeof(buf), oconf)) {
669 		if (os_strncmp(buf, "bss=", 4) == 0)
670 			multi_bss = 1;
671 		if (!multi_bss &&
672 		    (str_starts(buf, "ssid=") ||
673 		     str_starts(buf, "ssid2=") ||
674 		     str_starts(buf, "auth_algs=") ||
675 #ifdef CONFIG_WEP
676 		     str_starts(buf, "wep_default_key=") ||
677 		     str_starts(buf, "wep_key") ||
678 #endif /* CONFIG_WEP */
679 		     str_starts(buf, "wps_state=") ||
680 		     (pmf_changed && str_starts(buf, "ieee80211w=")) ||
681 		     str_starts(buf, "wpa=") ||
682 		     str_starts(buf, "wpa_psk=") ||
683 		     str_starts(buf, "wpa_pairwise=") ||
684 		     str_starts(buf, "rsn_pairwise=") ||
685 		     str_starts(buf, "wpa_key_mgmt=") ||
686 		     str_starts(buf, "wpa_passphrase="))) {
687 			fprintf(nconf, "#WPS# %s", buf);
688 		} else
689 			fprintf(nconf, "%s", buf);
690 	}
691 
692 	fclose(nconf);
693 	fclose(oconf);
694 
695 	if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
696 		wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
697 			   "configuration file: %s", strerror(errno));
698 		os_free(tmp_fname);
699 		return -1;
700 	}
701 
702 	os_free(tmp_fname);
703 
704 	/* Schedule configuration reload after short period of time to allow
705 	 * EAP-WSC to be finished.
706 	 */
707 	eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
708 			       NULL);
709 
710 	wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
711 
712 	return 0;
713 }
714 
715 
hostapd_wps_cred_cb(void * ctx,const struct wps_credential * cred)716 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
717 {
718 	struct hostapd_data *hapd = ctx;
719 	return hostapd_wps_for_each(hapd, hapd_wps_cred_cb, (void *) cred);
720 }
721 
722 
hostapd_wps_reenable_ap_pin(void * eloop_data,void * user_ctx)723 static void hostapd_wps_reenable_ap_pin(void *eloop_data, void *user_ctx)
724 {
725 	struct hostapd_data *hapd = eloop_data;
726 
727 	if (hapd->conf->ap_setup_locked)
728 		return;
729 	if (hapd->ap_pin_failures_consecutive >= 10)
730 		return;
731 
732 	wpa_printf(MSG_DEBUG, "WPS: Re-enable AP PIN");
733 	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
734 	hapd->wps->ap_setup_locked = 0;
735 	wps_registrar_update_ie(hapd->wps->registrar);
736 }
737 
738 
wps_pwd_auth_fail(struct hostapd_data * hapd,void * ctx)739 static int wps_pwd_auth_fail(struct hostapd_data *hapd, void *ctx)
740 {
741 	struct wps_event_pwd_auth_fail *data = ctx;
742 
743 	if (!data->enrollee || hapd->conf->ap_pin == NULL || hapd->wps == NULL)
744 		return 0;
745 
746 	/*
747 	 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
748 	 * for some time if this happens multiple times to slow down brute
749 	 * force attacks.
750 	 */
751 	hapd->ap_pin_failures++;
752 	hapd->ap_pin_failures_consecutive++;
753 	wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u "
754 		   "(%u consecutive)",
755 		   hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
756 	if (hapd->ap_pin_failures < 3)
757 		return 0;
758 
759 	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
760 	hapd->wps->ap_setup_locked = 1;
761 
762 	wps_registrar_update_ie(hapd->wps->registrar);
763 
764 	if (!hapd->conf->ap_setup_locked &&
765 	    hapd->ap_pin_failures_consecutive >= 10) {
766 		/*
767 		 * In indefinite lockdown - disable automatic AP PIN
768 		 * reenablement.
769 		 */
770 		eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
771 		wpa_printf(MSG_DEBUG, "WPS: AP PIN disabled indefinitely");
772 	} else if (!hapd->conf->ap_setup_locked) {
773 		if (hapd->ap_pin_lockout_time == 0)
774 			hapd->ap_pin_lockout_time = 60;
775 		else if (hapd->ap_pin_lockout_time < 365 * 24 * 60 * 60 &&
776 			 (hapd->ap_pin_failures % 3) == 0)
777 			hapd->ap_pin_lockout_time *= 2;
778 
779 		wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN for %u seconds",
780 			   hapd->ap_pin_lockout_time);
781 		eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
782 		eloop_register_timeout(hapd->ap_pin_lockout_time, 0,
783 				       hostapd_wps_reenable_ap_pin, hapd,
784 				       NULL);
785 	}
786 
787 	return 0;
788 }
789 
790 
hostapd_pwd_auth_fail(struct hostapd_data * hapd,struct wps_event_pwd_auth_fail * data)791 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
792 				  struct wps_event_pwd_auth_fail *data)
793 {
794 	/* Update WPS Status - Authentication Failure */
795 	wpa_printf(MSG_DEBUG, "WPS: Authentication failure update");
796 	hapd->wps_stats.status = WPS_STATUS_FAILURE;
797 	hapd->wps_stats.failure_reason = WPS_EI_AUTH_FAILURE;
798 	os_memcpy(hapd->wps_stats.peer_addr, data->peer_macaddr, ETH_ALEN);
799 
800 	hostapd_wps_for_each(hapd, wps_pwd_auth_fail, data);
801 }
802 
803 
wps_ap_pin_success(struct hostapd_data * hapd,void * ctx)804 static int wps_ap_pin_success(struct hostapd_data *hapd, void *ctx)
805 {
806 	if (hapd->conf->ap_pin == NULL || hapd->wps == NULL)
807 		return 0;
808 
809 	if (hapd->ap_pin_failures_consecutive == 0)
810 		return 0;
811 
812 	wpa_printf(MSG_DEBUG, "WPS: Clear consecutive AP PIN failure counter "
813 		   "- total validation failures %u (%u consecutive)",
814 		   hapd->ap_pin_failures, hapd->ap_pin_failures_consecutive);
815 	hapd->ap_pin_failures_consecutive = 0;
816 
817 	return 0;
818 }
819 
820 
hostapd_wps_ap_pin_success(struct hostapd_data * hapd)821 static void hostapd_wps_ap_pin_success(struct hostapd_data *hapd)
822 {
823 	hostapd_wps_for_each(hapd, wps_ap_pin_success, NULL);
824 }
825 
826 
hostapd_wps_event_pbc_overlap(struct hostapd_data * hapd)827 static void hostapd_wps_event_pbc_overlap(struct hostapd_data *hapd)
828 {
829 	/* Update WPS Status - PBC Overlap */
830 	hapd->wps_stats.pbc_status = WPS_PBC_STATUS_OVERLAP;
831 }
832 
833 
hostapd_wps_event_pbc_timeout(struct hostapd_data * hapd)834 static void hostapd_wps_event_pbc_timeout(struct hostapd_data *hapd)
835 {
836 	/* Update WPS PBC Status:PBC Timeout */
837 	hapd->wps_stats.pbc_status = WPS_PBC_STATUS_TIMEOUT;
838 }
839 
840 
hostapd_wps_event_pbc_active(struct hostapd_data * hapd)841 static void hostapd_wps_event_pbc_active(struct hostapd_data *hapd)
842 {
843 	/* Update WPS PBC status - Active */
844 	hapd->wps_stats.pbc_status = WPS_PBC_STATUS_ACTIVE;
845 }
846 
847 
hostapd_wps_event_pbc_disable(struct hostapd_data * hapd)848 static void hostapd_wps_event_pbc_disable(struct hostapd_data *hapd)
849 {
850 	/* Update WPS PBC status - Active */
851 	hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
852 }
853 
854 
hostapd_wps_event_success(struct hostapd_data * hapd,struct wps_event_success * success)855 static void hostapd_wps_event_success(struct hostapd_data *hapd,
856 				      struct wps_event_success *success)
857 {
858 	/* Update WPS status - Success */
859 	hapd->wps_stats.pbc_status = WPS_PBC_STATUS_DISABLE;
860 	hapd->wps_stats.status = WPS_STATUS_SUCCESS;
861 	os_memcpy(hapd->wps_stats.peer_addr, success->peer_macaddr, ETH_ALEN);
862 }
863 
864 
hostapd_wps_event_fail(struct hostapd_data * hapd,struct wps_event_fail * fail)865 static void hostapd_wps_event_fail(struct hostapd_data *hapd,
866 				   struct wps_event_fail *fail)
867 {
868 	/* Update WPS status - Failure */
869 	hapd->wps_stats.status = WPS_STATUS_FAILURE;
870 	os_memcpy(hapd->wps_stats.peer_addr, fail->peer_macaddr, ETH_ALEN);
871 
872 	hapd->wps_stats.failure_reason = fail->error_indication;
873 
874 	if (fail->error_indication > 0 &&
875 	    fail->error_indication < NUM_WPS_EI_VALUES) {
876 		wpa_msg(hapd->msg_ctx, MSG_INFO,
877 			WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
878 			fail->msg, fail->config_error, fail->error_indication,
879 			wps_ei_str(fail->error_indication));
880 	} else {
881 		wpa_msg(hapd->msg_ctx, MSG_INFO,
882 			WPS_EVENT_FAIL "msg=%d config_error=%d",
883 			fail->msg, fail->config_error);
884 	}
885 }
886 
887 
hostapd_wps_event_cb(void * ctx,enum wps_event event,union wps_event_data * data)888 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
889 				 union wps_event_data *data)
890 {
891 	struct hostapd_data *hapd = ctx;
892 
893 	switch (event) {
894 	case WPS_EV_M2D:
895 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_M2D);
896 		break;
897 	case WPS_EV_FAIL:
898 		hostapd_wps_event_fail(hapd, &data->fail);
899 		break;
900 	case WPS_EV_SUCCESS:
901 		hostapd_wps_event_success(hapd, &data->success);
902 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_SUCCESS);
903 		break;
904 	case WPS_EV_PWD_AUTH_FAIL:
905 		hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
906 		break;
907 	case WPS_EV_PBC_OVERLAP:
908 		hostapd_wps_event_pbc_overlap(hapd);
909 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_OVERLAP);
910 		break;
911 	case WPS_EV_PBC_TIMEOUT:
912 		hostapd_wps_event_pbc_timeout(hapd);
913 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_TIMEOUT);
914 		break;
915 	case WPS_EV_PBC_ACTIVE:
916 		hostapd_wps_event_pbc_active(hapd);
917 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ACTIVE);
918 		break;
919 	case WPS_EV_PBC_DISABLE:
920 		hostapd_wps_event_pbc_disable(hapd);
921 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_DISABLE);
922 		break;
923 	case WPS_EV_ER_AP_ADD:
924 		break;
925 	case WPS_EV_ER_AP_REMOVE:
926 		break;
927 	case WPS_EV_ER_ENROLLEE_ADD:
928 		break;
929 	case WPS_EV_ER_ENROLLEE_REMOVE:
930 		break;
931 	case WPS_EV_ER_AP_SETTINGS:
932 		break;
933 	case WPS_EV_ER_SET_SELECTED_REGISTRAR:
934 		break;
935 	case WPS_EV_AP_PIN_SUCCESS:
936 		hostapd_wps_ap_pin_success(hapd);
937 		break;
938 	}
939 	if (hapd->wps_event_cb)
940 		hapd->wps_event_cb(hapd->wps_event_cb_ctx, event, data);
941 }
942 
943 
hostapd_wps_rf_band_cb(void * ctx)944 static int hostapd_wps_rf_band_cb(void *ctx)
945 {
946 	struct hostapd_data *hapd = ctx;
947 
948 	return hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
949 		WPS_RF_50GHZ :
950 		hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ?
951 		WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
952 }
953 
954 
hostapd_wps_clear_ies(struct hostapd_data * hapd,int deinit_only)955 static void hostapd_wps_clear_ies(struct hostapd_data *hapd, int deinit_only)
956 {
957 	wpabuf_free(hapd->wps_beacon_ie);
958 	hapd->wps_beacon_ie = NULL;
959 
960 	wpabuf_free(hapd->wps_probe_resp_ie);
961 	hapd->wps_probe_resp_ie = NULL;
962 
963 	if (deinit_only) {
964 		if (hapd->drv_priv)
965 			hostapd_reset_ap_wps_ie(hapd);
966 		return;
967 	}
968 
969 	hostapd_set_ap_wps_ie(hapd);
970 }
971 
972 
get_uuid_cb(struct hostapd_iface * iface,void * ctx)973 static int get_uuid_cb(struct hostapd_iface *iface, void *ctx)
974 {
975 	const u8 **uuid = ctx;
976 	size_t j;
977 
978 	if (iface == NULL)
979 		return 0;
980 	for (j = 0; j < iface->num_bss; j++) {
981 		struct hostapd_data *hapd = iface->bss[j];
982 		if (hapd->wps && !hapd->conf->wps_independent &&
983 		    !is_nil_uuid(hapd->wps->uuid)) {
984 			*uuid = hapd->wps->uuid;
985 			return 1;
986 		}
987 	}
988 
989 	return 0;
990 }
991 
992 
get_own_uuid(struct hostapd_iface * iface)993 static const u8 * get_own_uuid(struct hostapd_iface *iface)
994 {
995 	const u8 *uuid;
996 	if (iface->interfaces == NULL ||
997 	    iface->interfaces->for_each_interface == NULL)
998 		return NULL;
999 	uuid = NULL;
1000 	iface->interfaces->for_each_interface(iface->interfaces, get_uuid_cb,
1001 					      &uuid);
1002 	return uuid;
1003 }
1004 
1005 
count_interface_cb(struct hostapd_iface * iface,void * ctx)1006 static int count_interface_cb(struct hostapd_iface *iface, void *ctx)
1007 {
1008 	int *count= ctx;
1009 	(*count)++;
1010 	return 0;
1011 }
1012 
1013 
interface_count(struct hostapd_iface * iface)1014 static int interface_count(struct hostapd_iface *iface)
1015 {
1016 	int count = 0;
1017 	if (iface->interfaces == NULL ||
1018 	    iface->interfaces->for_each_interface == NULL)
1019 		return 0;
1020 	iface->interfaces->for_each_interface(iface->interfaces,
1021 					      count_interface_cb, &count);
1022 	return count;
1023 }
1024 
1025 
hostapd_wps_set_vendor_ext(struct hostapd_data * hapd,struct wps_context * wps)1026 static int hostapd_wps_set_vendor_ext(struct hostapd_data *hapd,
1027 				      struct wps_context *wps)
1028 {
1029 	int i;
1030 
1031 	for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
1032 		wpabuf_free(wps->dev.vendor_ext[i]);
1033 		wps->dev.vendor_ext[i] = NULL;
1034 
1035 		if (hapd->conf->wps_vendor_ext[i] == NULL)
1036 			continue;
1037 
1038 		wps->dev.vendor_ext[i] =
1039 			wpabuf_dup(hapd->conf->wps_vendor_ext[i]);
1040 		if (wps->dev.vendor_ext[i] == NULL) {
1041 			while (--i >= 0)
1042 				wpabuf_free(wps->dev.vendor_ext[i]);
1043 			return -1;
1044 		}
1045 	}
1046 
1047 	return 0;
1048 }
1049 
1050 
hostapd_wps_set_application_ext(struct hostapd_data * hapd,struct wps_context * wps)1051 static int hostapd_wps_set_application_ext(struct hostapd_data *hapd,
1052 					   struct wps_context *wps)
1053 {
1054 	wpabuf_free(wps->dev.application_ext);
1055 
1056 	if (!hapd->conf->wps_application_ext) {
1057 		wps->dev.application_ext = NULL;
1058 		return 0;
1059 	}
1060 
1061 	wps->dev.application_ext = wpabuf_dup(hapd->conf->wps_application_ext);
1062 	return wps->dev.application_ext ? 0 : -1;
1063 }
1064 
1065 
hostapd_free_wps(struct wps_context * wps)1066 static void hostapd_free_wps(struct wps_context *wps)
1067 {
1068 	int i;
1069 
1070 	for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
1071 		wpabuf_free(wps->dev.vendor_ext[i]);
1072 	wps_device_data_free(&wps->dev);
1073 	bin_clear_free(wps->network_key, wps->network_key_len);
1074 	hostapd_wps_nfc_clear(wps);
1075 	wpabuf_free(wps->dh_pubkey);
1076 	wpabuf_free(wps->dh_privkey);
1077 	forced_memzero(wps->psk, sizeof(wps->psk));
1078 	os_free(wps);
1079 }
1080 
1081 
hostapd_init_wps(struct hostapd_data * hapd,struct hostapd_bss_config * conf)1082 int hostapd_init_wps(struct hostapd_data *hapd,
1083 		     struct hostapd_bss_config *conf)
1084 {
1085 	struct wps_context *wps;
1086 	struct wps_registrar_config cfg;
1087 	u8 *multi_ap_netw_key = NULL;
1088 
1089 	if (conf->wps_state == 0) {
1090 		hostapd_wps_clear_ies(hapd, 0);
1091 		return 0;
1092 	}
1093 
1094 	wps = os_zalloc(sizeof(*wps));
1095 	if (wps == NULL)
1096 		return -1;
1097 
1098 	wps->cred_cb = hostapd_wps_cred_cb;
1099 	wps->event_cb = hostapd_wps_event_cb;
1100 	wps->rf_band_cb = hostapd_wps_rf_band_cb;
1101 	wps->cb_ctx = hapd;
1102 
1103 	os_memset(&cfg, 0, sizeof(cfg));
1104 	wps->wps_state = hapd->conf->wps_state;
1105 	wps->ap_setup_locked = hapd->conf->ap_setup_locked;
1106 	if (is_nil_uuid(hapd->conf->uuid)) {
1107 		const u8 *uuid;
1108 		uuid = get_own_uuid(hapd->iface);
1109 		if (uuid && !conf->wps_independent) {
1110 			os_memcpy(wps->uuid, uuid, UUID_LEN);
1111 			wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
1112 				    "interface", wps->uuid, UUID_LEN);
1113 		} else {
1114 			uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
1115 			wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1116 				    "address", wps->uuid, UUID_LEN);
1117 		}
1118 	} else {
1119 		os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
1120 		wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
1121 			    wps->uuid, UUID_LEN);
1122 	}
1123 	wps->ssid_len = hapd->conf->ssid.ssid_len;
1124 	os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
1125 	wps->ap = 1;
1126 	os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
1127 	wps->dev.device_name = hapd->conf->device_name ?
1128 		os_strdup(hapd->conf->device_name) : NULL;
1129 	wps->dev.manufacturer = hapd->conf->manufacturer ?
1130 		os_strdup(hapd->conf->manufacturer) : NULL;
1131 	wps->dev.model_name = hapd->conf->model_name ?
1132 		os_strdup(hapd->conf->model_name) : NULL;
1133 	wps->dev.model_number = hapd->conf->model_number ?
1134 		os_strdup(hapd->conf->model_number) : NULL;
1135 	wps->dev.serial_number = hapd->conf->serial_number ?
1136 		os_strdup(hapd->conf->serial_number) : NULL;
1137 	wps->config_methods =
1138 		wps_config_methods_str2bin(hapd->conf->config_methods);
1139 	if ((wps->config_methods &
1140 	     (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1141 	      WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1142 		wpa_printf(MSG_INFO, "WPS: Converting display to "
1143 			   "virtual_display for WPS 2.0 compliance");
1144 		wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1145 	}
1146 	if ((wps->config_methods &
1147 	     (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1148 	      WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1149 		wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1150 			   "virtual_push_button for WPS 2.0 compliance");
1151 		wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1152 	}
1153 	os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
1154 		  WPS_DEV_TYPE_LEN);
1155 
1156 	if (hostapd_wps_set_vendor_ext(hapd, wps) < 0 ||
1157 	    hostapd_wps_set_application_ext(hapd, wps) < 0)
1158 		goto fail;
1159 
1160 	wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
1161 
1162 	if (conf->wps_rf_bands) {
1163 		wps->dev.rf_bands = conf->wps_rf_bands;
1164 	} else {
1165 		wps->dev.rf_bands =
1166 			hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
1167 			WPS_RF_50GHZ :
1168 			hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ?
1169 			WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
1170 	}
1171 
1172 	if (conf->wpa & WPA_PROTO_RSN) {
1173 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1174 			wps->auth_types |= WPS_AUTH_WPA2PSK;
1175 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1176 			wps->auth_types |= WPS_AUTH_WPA2;
1177 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE)
1178 			wps->auth_types |= WPS_AUTH_WPA2PSK;
1179 
1180 		if (conf->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
1181 					  WPA_CIPHER_CCMP_256 |
1182 					  WPA_CIPHER_GCMP_256)) {
1183 			wps->encr_types |= WPS_ENCR_AES;
1184 			wps->encr_types_rsn |= WPS_ENCR_AES;
1185 		}
1186 		if (conf->rsn_pairwise & WPA_CIPHER_TKIP) {
1187 #ifdef CONFIG_NO_TKIP
1188 			wpa_printf(MSG_INFO, "WPS: TKIP not supported");
1189 			goto fail;
1190 #else /* CONFIG_NO_TKIP */
1191 			wps->encr_types |= WPS_ENCR_TKIP;
1192 			wps->encr_types_rsn |= WPS_ENCR_TKIP;
1193 #endif /* CONFIG_NO_TKIP */
1194 		}
1195 	}
1196 
1197 	if (conf->wpa & WPA_PROTO_WPA) {
1198 #ifdef CONFIG_NO_TKIP
1199 		if (!(conf->wpa & WPA_PROTO_RSN)) {
1200 			wpa_printf(MSG_INFO, "WPS: WPA(v1) not supported");
1201 			goto fail;
1202 		}
1203 		conf->wpa &= ~WPA_PROTO_WPA;
1204 #else /* CONFIG_NO_TKIP */
1205 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1206 			wps->auth_types |= WPS_AUTH_WPAPSK;
1207 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1208 			wps->auth_types |= WPS_AUTH_WPA;
1209 
1210 		if (conf->wpa_pairwise & WPA_CIPHER_CCMP) {
1211 			wps->encr_types |= WPS_ENCR_AES;
1212 			wps->encr_types_wpa |= WPS_ENCR_AES;
1213 		}
1214 		if (conf->wpa_pairwise & WPA_CIPHER_TKIP) {
1215 			wps->encr_types |= WPS_ENCR_TKIP;
1216 			wps->encr_types_wpa |= WPS_ENCR_TKIP;
1217 		}
1218 #endif /* CONFIG_NO_TKIP */
1219 	}
1220 
1221 	if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
1222 		wps->encr_types |= WPS_ENCR_NONE;
1223 		wps->auth_types |= WPS_AUTH_OPEN;
1224 	}
1225 
1226 	if (conf->ssid.wpa_psk_file) {
1227 		/* Use per-device PSKs */
1228 	} else if (conf->ssid.wpa_passphrase) {
1229 		wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
1230 		wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
1231 	} else if (conf->ssid.wpa_psk) {
1232 		wps->network_key = os_malloc(2 * PMK_LEN + 1);
1233 		if (wps->network_key == NULL)
1234 			goto fail;
1235 		wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
1236 				 conf->ssid.wpa_psk->psk, PMK_LEN);
1237 		wps->network_key_len = 2 * PMK_LEN;
1238 #ifdef CONFIG_WEP
1239 	} else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
1240 		wps->network_key = os_malloc(conf->ssid.wep.len[0]);
1241 		if (wps->network_key == NULL)
1242 			goto fail;
1243 		os_memcpy(wps->network_key, conf->ssid.wep.key[0],
1244 			  conf->ssid.wep.len[0]);
1245 		wps->network_key_len = conf->ssid.wep.len[0];
1246 #endif /* CONFIG_WEP */
1247 	}
1248 
1249 	if (conf->ssid.wpa_psk) {
1250 		os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
1251 		wps->psk_set = 1;
1252 	}
1253 
1254 	wps->ap_auth_type = wps->auth_types;
1255 	wps->ap_encr_type = wps->encr_types;
1256 	if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
1257 		/* Override parameters to enable security by default */
1258 #ifdef CONFIG_NO_TKIP
1259 		wps->auth_types = WPS_AUTH_WPA2PSK;
1260 		wps->encr_types = WPS_ENCR_AES;
1261 		wps->encr_types_rsn = WPS_ENCR_AES;
1262 		wps->encr_types_wpa = WPS_ENCR_AES;
1263 #else /* CONFIG_NO_TKIP */
1264 		wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1265 		wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1266 		wps->encr_types_rsn = WPS_ENCR_AES | WPS_ENCR_TKIP;
1267 		wps->encr_types_wpa = WPS_ENCR_AES | WPS_ENCR_TKIP;
1268 #endif /* CONFIG_NO_TKIP */
1269 	}
1270 
1271 	if ((hapd->conf->multi_ap & FRONTHAUL_BSS) &&
1272 	    hapd->conf->multi_ap_backhaul_ssid.ssid_len) {
1273 		cfg.multi_ap_backhaul_ssid_len =
1274 			hapd->conf->multi_ap_backhaul_ssid.ssid_len;
1275 		cfg.multi_ap_backhaul_ssid =
1276 			hapd->conf->multi_ap_backhaul_ssid.ssid;
1277 
1278 		if (conf->multi_ap_backhaul_ssid.wpa_passphrase) {
1279 			cfg.multi_ap_backhaul_network_key = (const u8 *)
1280 				conf->multi_ap_backhaul_ssid.wpa_passphrase;
1281 			cfg.multi_ap_backhaul_network_key_len =
1282 				os_strlen(conf->multi_ap_backhaul_ssid.wpa_passphrase);
1283 		} else if (conf->multi_ap_backhaul_ssid.wpa_psk) {
1284 			multi_ap_netw_key = os_malloc(2 * PMK_LEN + 1);
1285 			if (!multi_ap_netw_key)
1286 				goto fail;
1287 			wpa_snprintf_hex((char *) multi_ap_netw_key,
1288 					 2 * PMK_LEN + 1,
1289 					 conf->multi_ap_backhaul_ssid.wpa_psk->psk,
1290 					 PMK_LEN);
1291 			cfg.multi_ap_backhaul_network_key = multi_ap_netw_key;
1292 			cfg.multi_ap_backhaul_network_key_len = 2 * PMK_LEN;
1293 		}
1294 	}
1295 
1296 	wps->ap_settings = conf->ap_settings;
1297 	wps->ap_settings_len = conf->ap_settings_len;
1298 
1299 	cfg.new_psk_cb = hostapd_wps_new_psk_cb;
1300 	cfg.set_ie_cb = hostapd_wps_set_ie_cb;
1301 	cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
1302 	cfg.reg_success_cb = hostapd_wps_reg_success_cb;
1303 	cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
1304 	cfg.lookup_pskfile_cb = hostapd_wps_lookup_pskfile_cb;
1305 	cfg.cb_ctx = hapd;
1306 	cfg.skip_cred_build = conf->skip_cred_build;
1307 	cfg.extra_cred = conf->extra_cred;
1308 	cfg.extra_cred_len = conf->extra_cred_len;
1309 	cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
1310 		conf->skip_cred_build;
1311 	cfg.dualband = interface_count(hapd->iface) > 1;
1312 	if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
1313 	    (WPS_RF_50GHZ | WPS_RF_24GHZ))
1314 		cfg.dualband = 1;
1315 	if (cfg.dualband)
1316 		wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
1317 	cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
1318 
1319 	wps->registrar = wps_registrar_init(wps, &cfg);
1320 	if (wps->registrar == NULL) {
1321 		wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
1322 		goto fail;
1323 	}
1324 
1325 #ifdef CONFIG_WPS_UPNP
1326 	wps->friendly_name = hapd->conf->friendly_name;
1327 	wps->manufacturer_url = hapd->conf->manufacturer_url;
1328 	wps->model_description = hapd->conf->model_description;
1329 	wps->model_url = hapd->conf->model_url;
1330 	wps->upc = hapd->conf->upc;
1331 #endif /* CONFIG_WPS_UPNP */
1332 
1333 	hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
1334 
1335 #ifdef CONFIG_P2P
1336 	if ((hapd->conf->p2p & P2P_ENABLED) &&
1337 	    is_6ghz_op_class(hapd->iconf->op_class))
1338 		wps->use_passphrase = true;
1339 #endif /* CONFIG_P2P */
1340 	hapd->wps = wps;
1341 	bin_clear_free(multi_ap_netw_key, 2 * PMK_LEN);
1342 
1343 	return 0;
1344 
1345 fail:
1346 	bin_clear_free(multi_ap_netw_key, 2 * PMK_LEN);
1347 	hostapd_free_wps(wps);
1348 	return -1;
1349 }
1350 
1351 
hostapd_init_wps_complete(struct hostapd_data * hapd)1352 int hostapd_init_wps_complete(struct hostapd_data *hapd)
1353 {
1354 	struct wps_context *wps = hapd->wps;
1355 
1356 	if (wps == NULL)
1357 		return 0;
1358 
1359 #ifdef CONFIG_WPS_UPNP
1360 	if (hostapd_wps_upnp_init(hapd, wps) < 0) {
1361 		wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
1362 		wps_registrar_deinit(wps->registrar);
1363 		hostapd_free_wps(wps);
1364 		hapd->wps = NULL;
1365 		return -1;
1366 	}
1367 #endif /* CONFIG_WPS_UPNP */
1368 
1369 	return 0;
1370 }
1371 
1372 
hostapd_wps_nfc_clear(struct wps_context * wps)1373 static void hostapd_wps_nfc_clear(struct wps_context *wps)
1374 {
1375 #ifdef CONFIG_WPS_NFC
1376 	wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
1377 	wps->ap_nfc_dev_pw_id = 0;
1378 	wpabuf_free(wps->ap_nfc_dh_pubkey);
1379 	wps->ap_nfc_dh_pubkey = NULL;
1380 	wpabuf_free(wps->ap_nfc_dh_privkey);
1381 	wps->ap_nfc_dh_privkey = NULL;
1382 	wpabuf_free(wps->ap_nfc_dev_pw);
1383 	wps->ap_nfc_dev_pw = NULL;
1384 #endif /* CONFIG_WPS_NFC */
1385 }
1386 
1387 
hostapd_wps_update_multi_ap(struct hostapd_data * hapd,struct wps_registrar * reg)1388 static int hostapd_wps_update_multi_ap(struct hostapd_data *hapd,
1389 				       struct wps_registrar *reg)
1390 {
1391 	struct hostapd_bss_config *conf = hapd->conf;
1392 	u8 *multi_ap_backhaul_network_key = NULL;
1393 	size_t multi_ap_backhaul_network_key_len = 0;
1394 	int ret;
1395 
1396 	if (!(conf->multi_ap & FRONTHAUL_BSS) ||
1397 	    !conf->multi_ap_backhaul_ssid.ssid_len)
1398 		return 0;
1399 
1400 	if (conf->multi_ap_backhaul_ssid.wpa_passphrase) {
1401 		multi_ap_backhaul_network_key =
1402 			(u8 *) os_strdup(
1403 				conf->multi_ap_backhaul_ssid.wpa_passphrase);
1404 		if (!multi_ap_backhaul_network_key)
1405 			return -1;
1406 		multi_ap_backhaul_network_key_len =
1407 			os_strlen(conf->multi_ap_backhaul_ssid.wpa_passphrase);
1408 	} else if (conf->multi_ap_backhaul_ssid.wpa_psk) {
1409 		multi_ap_backhaul_network_key = os_malloc(2 * PMK_LEN + 1);
1410 		if (!multi_ap_backhaul_network_key)
1411 			return -1;
1412 		wpa_snprintf_hex((char *) multi_ap_backhaul_network_key,
1413 				 2 * PMK_LEN + 1,
1414 				 conf->multi_ap_backhaul_ssid.wpa_psk->psk,
1415 				 PMK_LEN);
1416 		multi_ap_backhaul_network_key_len = 2 * PMK_LEN;
1417 	}
1418 
1419 	ret = wps_registrar_update_multi_ap(
1420 		reg, conf->multi_ap_backhaul_ssid.ssid,
1421 		conf->multi_ap_backhaul_ssid.ssid_len,
1422 		multi_ap_backhaul_network_key,
1423 		multi_ap_backhaul_network_key_len);
1424 	os_free(multi_ap_backhaul_network_key);
1425 
1426 	return ret;
1427 }
1428 
1429 
hostapd_deinit_wps(struct hostapd_data * hapd)1430 void hostapd_deinit_wps(struct hostapd_data *hapd)
1431 {
1432 	eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
1433 	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1434 	eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL);
1435 	if (hapd->wps == NULL) {
1436 		hostapd_wps_clear_ies(hapd, 1);
1437 		return;
1438 	}
1439 #ifdef CONFIG_WPS_UPNP
1440 	hostapd_wps_upnp_deinit(hapd);
1441 #endif /* CONFIG_WPS_UPNP */
1442 	wps_registrar_deinit(hapd->wps->registrar);
1443 	wps_free_pending_msgs(hapd->wps->upnp_msgs);
1444 	hostapd_free_wps(hapd->wps);
1445 	hapd->wps = NULL;
1446 	hostapd_wps_clear_ies(hapd, 1);
1447 }
1448 
1449 
hostapd_update_wps(struct hostapd_data * hapd)1450 void hostapd_update_wps(struct hostapd_data *hapd)
1451 {
1452 	struct wps_context *wps = hapd->wps;
1453 	struct hostapd_bss_config *conf = hapd->conf;
1454 
1455 	if (!wps)
1456 		return;
1457 
1458 #ifdef CONFIG_WPS_UPNP
1459 	wps->friendly_name = conf->friendly_name;
1460 	wps->manufacturer_url = conf->manufacturer_url;
1461 	wps->model_description = conf->model_description;
1462 	wps->model_url = conf->model_url;
1463 	wps->upc = conf->upc;
1464 #endif /* CONFIG_WPS_UPNP */
1465 
1466 	os_memcpy(wps->ssid, conf->ssid.ssid, conf->ssid.ssid_len);
1467 	wps->ssid_len = conf->ssid.ssid_len;
1468 
1469 	/* Clear WPS settings, then fill them again */
1470 	os_free(wps->network_key);
1471 	wps->network_key = NULL;
1472 	wps->network_key_len = 0;
1473 	wps->psk_set = 0;
1474 	if (conf->ssid.wpa_psk_file) {
1475 		/* Use per-device PSKs */
1476 	} else if (conf->ssid.wpa_passphrase) {
1477 		wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
1478 		if (!wps->network_key)
1479 			return;
1480 		wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
1481 	} else if (conf->ssid.wpa_psk) {
1482 		wps->network_key = os_malloc(2 * PMK_LEN + 1);
1483 		if (!wps->network_key)
1484 			return;
1485 		wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
1486 				 conf->ssid.wpa_psk->psk, PMK_LEN);
1487 		wps->network_key_len = 2 * PMK_LEN;
1488 #ifdef CONFIG_WEP
1489 	} else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
1490 		wps->network_key = os_malloc(conf->ssid.wep.len[0]);
1491 		if (!wps->network_key)
1492 			return;
1493 		os_memcpy(wps->network_key, conf->ssid.wep.key[0],
1494 			  conf->ssid.wep.len[0]);
1495 		wps->network_key_len = conf->ssid.wep.len[0];
1496 #endif /* CONFIG_WEP */
1497 	}
1498 
1499 	if (conf->ssid.wpa_psk) {
1500 		os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
1501 		wps->psk_set = 1;
1502 	}
1503 
1504 	hostapd_wps_update_multi_ap(hapd, wps->registrar);
1505 
1506 	hostapd_wps_set_vendor_ext(hapd, wps);
1507 	hostapd_wps_set_application_ext(hapd, wps);
1508 
1509 	if (conf->wps_state)
1510 		wps_registrar_update_ie(wps->registrar);
1511 	else
1512 		hostapd_deinit_wps(hapd);
1513 }
1514 
1515 
1516 struct wps_add_pin_data {
1517 	const u8 *addr;
1518 	const u8 *uuid;
1519 	const u8 *pin;
1520 	size_t pin_len;
1521 	int timeout;
1522 	int added;
1523 };
1524 
1525 
wps_add_pin(struct hostapd_data * hapd,void * ctx)1526 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
1527 {
1528 	struct wps_add_pin_data *data = ctx;
1529 	int ret;
1530 
1531 	if (hapd->wps == NULL)
1532 		return 0;
1533 	ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
1534 				    data->uuid, data->pin, data->pin_len,
1535 				    data->timeout);
1536 	if (ret == 0)
1537 		data->added++;
1538 	return ret;
1539 }
1540 
1541 
hostapd_wps_add_pin(struct hostapd_data * hapd,const u8 * addr,const char * uuid,const char * pin,int timeout)1542 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
1543 			const char *uuid, const char *pin, int timeout)
1544 {
1545 	u8 u[UUID_LEN];
1546 	struct wps_add_pin_data data;
1547 
1548 	data.addr = addr;
1549 	data.uuid = u;
1550 	data.pin = (const u8 *) pin;
1551 	data.pin_len = os_strlen(pin);
1552 	data.timeout = timeout;
1553 	data.added = 0;
1554 
1555 	if (os_strcmp(uuid, "any") == 0)
1556 		data.uuid = NULL;
1557 	else {
1558 		if (uuid_str2bin(uuid, u))
1559 			return -1;
1560 		data.uuid = u;
1561 	}
1562 	if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1563 		return -1;
1564 	return data.added ? 0 : -1;
1565 }
1566 
1567 
1568 struct wps_button_pushed_ctx {
1569 	const u8 *p2p_dev_addr;
1570 	unsigned int count;
1571 };
1572 
wps_button_pushed(struct hostapd_data * hapd,void * ctx)1573 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1574 {
1575 	struct wps_button_pushed_ctx *data = ctx;
1576 
1577 	if (hapd->wps) {
1578 		data->count++;
1579 		return wps_registrar_button_pushed(hapd->wps->registrar,
1580 						   data->p2p_dev_addr);
1581 	}
1582 
1583 	return 0;
1584 }
1585 
1586 
hostapd_wps_button_pushed(struct hostapd_data * hapd,const u8 * p2p_dev_addr)1587 int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1588 			      const u8 *p2p_dev_addr)
1589 {
1590 	struct wps_button_pushed_ctx ctx;
1591 	int ret;
1592 
1593 	os_memset(&ctx, 0, sizeof(ctx));
1594 	ctx.p2p_dev_addr = p2p_dev_addr;
1595 	ret = hostapd_wps_for_each(hapd, wps_button_pushed, &ctx);
1596 	if (ret == 0 && !ctx.count)
1597 		ret = -1;
1598 	return ret;
1599 }
1600 
1601 
1602 struct wps_cancel_ctx {
1603 	unsigned int count;
1604 };
1605 
wps_cancel(struct hostapd_data * hapd,void * ctx)1606 static int wps_cancel(struct hostapd_data *hapd, void *ctx)
1607 {
1608 	struct wps_cancel_ctx *data = ctx;
1609 
1610 	if (hapd->wps) {
1611 		data->count++;
1612 		wps_registrar_wps_cancel(hapd->wps->registrar);
1613 		ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
1614 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_CANCEL);
1615 	}
1616 
1617 	return 0;
1618 }
1619 
1620 
hostapd_wps_cancel(struct hostapd_data * hapd)1621 int hostapd_wps_cancel(struct hostapd_data *hapd)
1622 {
1623 	struct wps_cancel_ctx ctx;
1624 	int ret;
1625 
1626 	os_memset(&ctx, 0, sizeof(ctx));
1627 	ret = hostapd_wps_for_each(hapd, wps_cancel, &ctx);
1628 	if (ret == 0 && !ctx.count)
1629 		ret = -1;
1630 	return ret;
1631 }
1632 
1633 
hostapd_wps_probe_req_rx(void * ctx,const u8 * addr,const u8 * da,const u8 * bssid,const u8 * ie,size_t ie_len,int ssi_signal)1634 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1635 				    const u8 *bssid,
1636 				    const u8 *ie, size_t ie_len,
1637 				    int ssi_signal)
1638 {
1639 	struct hostapd_data *hapd = ctx;
1640 	struct wpabuf *wps_ie;
1641 	struct ieee802_11_elems elems;
1642 
1643 	if (hapd->wps == NULL)
1644 		return 0;
1645 
1646 	if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1647 		wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1648 			   MACSTR_SEC, MAC2STR_SEC(addr));
1649 		return 0;
1650 	}
1651 
1652 	if (elems.ssid && elems.ssid_len > 0 &&
1653 	    (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1654 	     os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1655 	     0))
1656 		return 0; /* Not for us */
1657 
1658 	wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1659 	if (wps_ie == NULL)
1660 		return 0;
1661 	if (wps_validate_probe_req(wps_ie, addr) < 0) {
1662 		wpabuf_free(wps_ie);
1663 		return 0;
1664 	}
1665 
1666 	if (wpabuf_len(wps_ie) > 0) {
1667 		int p2p_wildcard = 0;
1668 #ifdef CONFIG_P2P
1669 		if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1670 		    os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1671 			      P2P_WILDCARD_SSID_LEN) == 0)
1672 			p2p_wildcard = 1;
1673 #endif /* CONFIG_P2P */
1674 		wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1675 					   p2p_wildcard);
1676 #ifdef CONFIG_WPS_UPNP
1677 		/* FIX: what exactly should be included in the WLANEvent?
1678 		 * WPS attributes? Full ProbeReq frame? */
1679 		if (!p2p_wildcard)
1680 			upnp_wps_device_send_wlan_event(
1681 				hapd->wps_upnp, addr,
1682 				UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1683 #endif /* CONFIG_WPS_UPNP */
1684 	}
1685 
1686 	wpabuf_free(wps_ie);
1687 
1688 	return 0;
1689 }
1690 
1691 
1692 #ifdef CONFIG_WPS_UPNP
1693 
hostapd_rx_req_put_wlan_response(void * priv,enum upnp_wps_wlanevent_type ev_type,const u8 * mac_addr,const struct wpabuf * msg,enum wps_msg_type msg_type)1694 static int hostapd_rx_req_put_wlan_response(
1695 	void *priv, enum upnp_wps_wlanevent_type ev_type,
1696 	const u8 *mac_addr, const struct wpabuf *msg,
1697 	enum wps_msg_type msg_type)
1698 {
1699 	struct hostapd_data *hapd = priv;
1700 	struct sta_info *sta;
1701 	struct upnp_pending_message *p;
1702 
1703 	wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1704 		   MACSTR_SEC, ev_type, MAC2STR_SEC(mac_addr));
1705 	wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1706 		    wpabuf_head(msg), wpabuf_len(msg));
1707 	if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1708 		wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1709 			   "PutWLANResponse WLANEventType %d", ev_type);
1710 		return -1;
1711 	}
1712 
1713 	/*
1714 	 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1715 	 * server implementation for delivery to the peer.
1716 	 */
1717 
1718 	sta = ap_get_sta(hapd, mac_addr);
1719 #ifndef CONFIG_WPS_STRICT
1720 	if (!sta) {
1721 		/*
1722 		 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1723 		 * Pick STA that is in an ongoing WPS registration without
1724 		 * checking the MAC address.
1725 		 */
1726 		wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1727 			   "on NewWLANEventMAC; try wildcard match");
1728 		for (sta = hapd->sta_list; sta; sta = sta->next) {
1729 			if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1730 				break;
1731 		}
1732 	}
1733 #endif /* CONFIG_WPS_STRICT */
1734 
1735 	if (!sta || !(sta->flags & WLAN_STA_WPS)) {
1736 		wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1737 		return 0;
1738 	}
1739 
1740 	if (!sta->eapol_sm) {
1741 		/*
1742 		 * This can happen, e.g., if an ER sends an extra message after
1743 		 * the station has disassociated (but not fully
1744 		 * deauthenticated).
1745 		 */
1746 		wpa_printf(MSG_DEBUG, "WPS UPnP: Matching STA did not have EAPOL state machine initialized");
1747 		return 0;
1748 	}
1749 
1750 	p = os_zalloc(sizeof(*p));
1751 	if (p == NULL)
1752 		return -1;
1753 	os_memcpy(p->addr, sta->addr, ETH_ALEN);
1754 	p->msg = wpabuf_dup(msg);
1755 	p->type = msg_type;
1756 	p->next = hapd->wps->upnp_msgs;
1757 	hapd->wps->upnp_msgs = p;
1758 
1759 	return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1760 }
1761 
1762 
hostapd_wps_upnp_init(struct hostapd_data * hapd,struct wps_context * wps)1763 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1764 				 struct wps_context *wps)
1765 {
1766 	struct upnp_wps_device_ctx *ctx;
1767 
1768 	if (!hapd->conf->upnp_iface)
1769 		return 0;
1770 	ctx = os_zalloc(sizeof(*ctx));
1771 	if (ctx == NULL)
1772 		return -1;
1773 
1774 	ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1775 	if (hapd->conf->ap_pin)
1776 		ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1777 
1778 	hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1779 					      hapd->conf->upnp_iface);
1780 	if (hapd->wps_upnp == NULL)
1781 		return -1;
1782 	wps->wps_upnp = hapd->wps_upnp;
1783 
1784 	return 0;
1785 }
1786 
1787 
hostapd_wps_upnp_deinit(struct hostapd_data * hapd)1788 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1789 {
1790 	upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1791 }
1792 
1793 #endif /* CONFIG_WPS_UPNP */
1794 
1795 
hostapd_wps_get_mib_sta(struct hostapd_data * hapd,const u8 * addr,char * buf,size_t buflen)1796 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1797 			    char *buf, size_t buflen)
1798 {
1799 	if (hapd->wps == NULL)
1800 		return 0;
1801 	return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1802 }
1803 
1804 
hostapd_wps_ap_pin_timeout(void * eloop_data,void * user_ctx)1805 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1806 {
1807 	struct hostapd_data *hapd = eloop_data;
1808 	wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1809 	hostapd_wps_ap_pin_disable(hapd);
1810 	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
1811 }
1812 
1813 
hostapd_wps_ap_pin_enable(struct hostapd_data * hapd,int timeout)1814 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1815 {
1816 	wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1817 	hapd->ap_pin_failures = 0;
1818 	hapd->ap_pin_failures_consecutive = 0;
1819 	hapd->conf->ap_setup_locked = 0;
1820 	if (hapd->wps->ap_setup_locked) {
1821 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1822 		hapd->wps->ap_setup_locked = 0;
1823 		wps_registrar_update_ie(hapd->wps->registrar);
1824 	}
1825 	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1826 	if (timeout > 0)
1827 		eloop_register_timeout(timeout, 0,
1828 				       hostapd_wps_ap_pin_timeout, hapd, NULL);
1829 }
1830 
1831 
wps_ap_pin_disable(struct hostapd_data * hapd,void * ctx)1832 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1833 {
1834 	os_free(hapd->conf->ap_pin);
1835 	hapd->conf->ap_pin = NULL;
1836 #ifdef CONFIG_WPS_UPNP
1837 	upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1838 #endif /* CONFIG_WPS_UPNP */
1839 	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1840 	return 0;
1841 }
1842 
1843 
hostapd_wps_ap_pin_disable(struct hostapd_data * hapd)1844 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1845 {
1846 	wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1847 	hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1848 }
1849 
1850 
1851 struct wps_ap_pin_data {
1852 	char pin_txt[9];
1853 	int timeout;
1854 };
1855 
1856 
wps_ap_pin_set(struct hostapd_data * hapd,void * ctx)1857 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1858 {
1859 	struct wps_ap_pin_data *data = ctx;
1860 
1861 	if (!hapd->wps)
1862 		return 0;
1863 
1864 	os_free(hapd->conf->ap_pin);
1865 	hapd->conf->ap_pin = os_strdup(data->pin_txt);
1866 #ifdef CONFIG_WPS_UPNP
1867 	upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1868 #endif /* CONFIG_WPS_UPNP */
1869 	hostapd_wps_ap_pin_enable(hapd, data->timeout);
1870 	return 0;
1871 }
1872 
1873 
hostapd_wps_ap_pin_random(struct hostapd_data * hapd,int timeout)1874 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1875 {
1876 	unsigned int pin;
1877 	struct wps_ap_pin_data data;
1878 
1879 	if (wps_generate_pin(&pin) < 0)
1880 		return NULL;
1881 	os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
1882 	data.timeout = timeout;
1883 	hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1884 	return hapd->conf->ap_pin;
1885 }
1886 
1887 
hostapd_wps_ap_pin_get(struct hostapd_data * hapd)1888 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1889 {
1890 	return hapd->conf->ap_pin;
1891 }
1892 
1893 
hostapd_wps_ap_pin_set(struct hostapd_data * hapd,const char * pin,int timeout)1894 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1895 			   int timeout)
1896 {
1897 	struct wps_ap_pin_data data;
1898 	int ret;
1899 
1900 	ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1901 	if (os_snprintf_error(sizeof(data.pin_txt), ret))
1902 		return -1;
1903 	data.timeout = timeout;
1904 	return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1905 }
1906 
1907 
wps_update_ie(struct hostapd_data * hapd,void * ctx)1908 static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1909 {
1910 	if (hapd->wps)
1911 		wps_registrar_update_ie(hapd->wps->registrar);
1912 	return 0;
1913 }
1914 
1915 
hostapd_wps_update_ie(struct hostapd_data * hapd)1916 void hostapd_wps_update_ie(struct hostapd_data *hapd)
1917 {
1918 	hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1919 }
1920 
1921 
hostapd_wps_config_ap(struct hostapd_data * hapd,const char * ssid,const char * auth,const char * encr,const char * key)1922 int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1923 			  const char *auth, const char *encr, const char *key)
1924 {
1925 	struct wps_credential cred;
1926 	size_t len;
1927 
1928 	os_memset(&cred, 0, sizeof(cred));
1929 
1930 	len = os_strlen(ssid);
1931 	if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1932 	    hexstr2bin(ssid, cred.ssid, len / 2))
1933 		return -1;
1934 	cred.ssid_len = len / 2;
1935 
1936 	if (os_strncmp(auth, "OPEN", 4) == 0)
1937 		cred.auth_type = WPS_AUTH_OPEN;
1938 #ifndef CONFIG_NO_TKIP
1939 	else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1940 		cred.auth_type = WPS_AUTH_WPAPSK;
1941 #endif /* CONFIG_NO_TKIP */
1942 	else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1943 		cred.auth_type = WPS_AUTH_WPA2PSK;
1944 	else
1945 		return -1;
1946 
1947 	if (encr) {
1948 		if (os_strncmp(encr, "NONE", 4) == 0)
1949 			cred.encr_type = WPS_ENCR_NONE;
1950 #ifndef CONFIG_NO_TKIP
1951 		else if (os_strncmp(encr, "TKIP", 4) == 0)
1952 			cred.encr_type = WPS_ENCR_TKIP;
1953 #endif /* CONFIG_NO_TKIP */
1954 		else if (os_strncmp(encr, "CCMP", 4) == 0)
1955 			cred.encr_type = WPS_ENCR_AES;
1956 		else
1957 			return -1;
1958 	} else
1959 		cred.encr_type = WPS_ENCR_NONE;
1960 
1961 	if (key) {
1962 		len = os_strlen(key);
1963 		if ((len & 1) || len > 2 * sizeof(cred.key) ||
1964 		    hexstr2bin(key, cred.key, len / 2))
1965 			return -1;
1966 		cred.key_len = len / 2;
1967 	}
1968 
1969 	if (!hapd->wps) {
1970 		wpa_printf(MSG_ERROR, "WPS: WPS config does not exist");
1971 		return -1;
1972 	}
1973 
1974 	return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1975 }
1976 
1977 
1978 #ifdef CONFIG_WPS_NFC
1979 
1980 struct wps_nfc_password_token_data {
1981 	const u8 *oob_dev_pw;
1982 	size_t oob_dev_pw_len;
1983 	int added;
1984 };
1985 
1986 
wps_add_nfc_password_token(struct hostapd_data * hapd,void * ctx)1987 static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
1988 {
1989 	struct wps_nfc_password_token_data *data = ctx;
1990 	int ret;
1991 
1992 	if (hapd->wps == NULL)
1993 		return 0;
1994 	ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
1995 						   data->oob_dev_pw,
1996 						   data->oob_dev_pw_len);
1997 	if (ret == 0)
1998 		data->added++;
1999 	return ret;
2000 }
2001 
2002 
hostapd_wps_add_nfc_password_token(struct hostapd_data * hapd,struct wps_parse_attr * attr)2003 static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
2004 					      struct wps_parse_attr *attr)
2005 {
2006 	struct wps_nfc_password_token_data data;
2007 
2008 	data.oob_dev_pw = attr->oob_dev_password;
2009 	data.oob_dev_pw_len = attr->oob_dev_password_len;
2010 	data.added = 0;
2011 	if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
2012 		return -1;
2013 	return data.added ? 0 : -1;
2014 }
2015 
2016 
hostapd_wps_nfc_tag_process(struct hostapd_data * hapd,const struct wpabuf * wps)2017 static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
2018 				       const struct wpabuf *wps)
2019 {
2020 	struct wps_parse_attr attr;
2021 
2022 	wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2023 
2024 	if (wps_parse_msg(wps, &attr)) {
2025 		wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2026 		return -1;
2027 	}
2028 
2029 	if (attr.oob_dev_password)
2030 		return hostapd_wps_add_nfc_password_token(hapd, &attr);
2031 
2032 	wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2033 	return -1;
2034 }
2035 
2036 
hostapd_wps_nfc_tag_read(struct hostapd_data * hapd,const struct wpabuf * data)2037 int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
2038 			     const struct wpabuf *data)
2039 {
2040 	const struct wpabuf *wps = data;
2041 	struct wpabuf *tmp = NULL;
2042 	int ret;
2043 
2044 	if (wpabuf_len(data) < 4)
2045 		return -1;
2046 
2047 	if (*wpabuf_head_u8(data) != 0x10) {
2048 		/* Assume this contains full NDEF record */
2049 		tmp = ndef_parse_wifi(data);
2050 		if (tmp == NULL) {
2051 			wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2052 			return -1;
2053 		}
2054 		wps = tmp;
2055 	}
2056 
2057 	ret = hostapd_wps_nfc_tag_process(hapd, wps);
2058 	wpabuf_free(tmp);
2059 	return ret;
2060 }
2061 
2062 
hostapd_wps_nfc_config_token(struct hostapd_data * hapd,int ndef)2063 struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
2064 					     int ndef)
2065 {
2066 	struct wpabuf *ret;
2067 
2068 	if (hapd->wps == NULL)
2069 		return NULL;
2070 
2071 	ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd),
2072 			       hapd->iconf->channel);
2073 	if (ndef && ret) {
2074 		struct wpabuf *tmp;
2075 		tmp = ndef_build_wifi(ret);
2076 		wpabuf_free(ret);
2077 		if (tmp == NULL)
2078 			return NULL;
2079 		ret = tmp;
2080 	}
2081 
2082 	return ret;
2083 }
2084 
2085 
hostapd_wps_nfc_hs_cr(struct hostapd_data * hapd,int ndef)2086 struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
2087 {
2088 	struct wpabuf *ret;
2089 
2090 	if (hapd->wps == NULL)
2091 		return NULL;
2092 
2093 	if (hapd->conf->wps_nfc_dh_pubkey == NULL) {
2094 		struct wps_context *wps = hapd->wps;
2095 		if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey,
2096 				   &hapd->conf->wps_nfc_dh_privkey) < 0)
2097 			return NULL;
2098 		hostapd_wps_nfc_clear(wps);
2099 		wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2100 		wps->ap_nfc_dh_pubkey =
2101 			wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
2102 		wps->ap_nfc_dh_privkey =
2103 			wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
2104 		if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2105 			hostapd_wps_nfc_clear(wps);
2106 			return NULL;
2107 		}
2108 	}
2109 
2110 	ret = wps_build_nfc_handover_sel(hapd->wps,
2111 					 hapd->conf->wps_nfc_dh_pubkey,
2112 					 hapd->own_addr, hapd->iface->freq);
2113 
2114 	if (ndef && ret) {
2115 		struct wpabuf *tmp;
2116 		tmp = ndef_build_wifi(ret);
2117 		wpabuf_free(ret);
2118 		if (tmp == NULL)
2119 			return NULL;
2120 		ret = tmp;
2121 	}
2122 
2123 	return ret;
2124 }
2125 
2126 
hostapd_wps_nfc_report_handover(struct hostapd_data * hapd,const struct wpabuf * req,const struct wpabuf * sel)2127 int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd,
2128 				    const struct wpabuf *req,
2129 				    const struct wpabuf *sel)
2130 {
2131 	struct wpabuf *wps;
2132 	int ret = -1;
2133 	u16 wsc_len;
2134 	const u8 *pos;
2135 	struct wpabuf msg;
2136 	struct wps_parse_attr attr;
2137 	u16 dev_pw_id;
2138 
2139 	/*
2140 	 * Enrollee/station is always initiator of the NFC connection handover,
2141 	 * so use the request message here to find Enrollee public key hash.
2142 	 */
2143 	wps = ndef_parse_wifi(req);
2144 	if (wps == NULL)
2145 		return -1;
2146 	wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2147 		   "payload from NFC connection handover");
2148 	wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2149 	if (wpabuf_len(wps) < 2) {
2150 		wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2151 			   "Message");
2152 		goto out;
2153 	}
2154 	pos = wpabuf_head(wps);
2155 	wsc_len = WPA_GET_BE16(pos);
2156 	if (wsc_len > wpabuf_len(wps) - 2) {
2157 		wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2158 			   "in rt Wi-Fi Handover Request Message", wsc_len);
2159 		goto out;
2160 	}
2161 	pos += 2;
2162 
2163 	wpa_hexdump(MSG_DEBUG,
2164 		    "WPS: WSC attributes in Wi-Fi Handover Request Message",
2165 		    pos, wsc_len);
2166 	if (wsc_len < wpabuf_len(wps) - 2) {
2167 		wpa_hexdump(MSG_DEBUG,
2168 			    "WPS: Ignore extra data after WSC attributes",
2169 			    pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2170 	}
2171 
2172 	wpabuf_set(&msg, pos, wsc_len);
2173 	ret = wps_parse_msg(&msg, &attr);
2174 	if (ret < 0) {
2175 		wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2176 			   "Wi-Fi Handover Request Message");
2177 		goto out;
2178 	}
2179 
2180 	if (attr.oob_dev_password == NULL ||
2181 	    attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2182 		wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2183 			   "included in Wi-Fi Handover Request Message");
2184 		ret = -1;
2185 		goto out;
2186 	}
2187 
2188 	if (attr.uuid_e == NULL) {
2189 		wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2190 			   "Handover Request Message");
2191 		ret = -1;
2192 		goto out;
2193 	}
2194 
2195 	wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2196 
2197 	wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2198 		    attr.oob_dev_password, attr.oob_dev_password_len);
2199 	dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2200 				 WPS_OOB_PUBKEY_HASH_LEN);
2201 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2202 		wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2203 			   "%u in Wi-Fi Handover Request Message", dev_pw_id);
2204 		ret = -1;
2205 		goto out;
2206 	}
2207 	wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2208 		    attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2209 
2210 	ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar,
2211 					     attr.oob_dev_password,
2212 					     DEV_PW_NFC_CONNECTION_HANDOVER,
2213 					     NULL, 0, 1);
2214 
2215 out:
2216 	wpabuf_free(wps);
2217 	return ret;
2218 }
2219 
2220 
hostapd_wps_nfc_token_gen(struct hostapd_data * hapd,int ndef)2221 struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
2222 {
2223 	if (hapd->conf->wps_nfc_pw_from_config) {
2224 		return wps_nfc_token_build(ndef,
2225 					   hapd->conf->wps_nfc_dev_pw_id,
2226 					   hapd->conf->wps_nfc_dh_pubkey,
2227 					   hapd->conf->wps_nfc_dev_pw);
2228 	}
2229 
2230 	return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
2231 				 &hapd->conf->wps_nfc_dh_pubkey,
2232 				 &hapd->conf->wps_nfc_dh_privkey,
2233 				 &hapd->conf->wps_nfc_dev_pw);
2234 }
2235 
2236 
hostapd_wps_nfc_token_enable(struct hostapd_data * hapd)2237 int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
2238 {
2239 	struct wps_context *wps = hapd->wps;
2240 	struct wpabuf *pw;
2241 
2242 	if (wps == NULL)
2243 		return -1;
2244 
2245 	if (!hapd->conf->wps_nfc_dh_pubkey ||
2246 	    !hapd->conf->wps_nfc_dh_privkey ||
2247 	    !hapd->conf->wps_nfc_dev_pw ||
2248 	    !hapd->conf->wps_nfc_dev_pw_id)
2249 		return -1;
2250 
2251 	hostapd_wps_nfc_clear(wps);
2252 	wpa_printf(MSG_DEBUG,
2253 		   "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
2254 		   hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
2255 	wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
2256 	wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
2257 	wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
2258 	pw = hapd->conf->wps_nfc_dev_pw;
2259 	wps->ap_nfc_dev_pw = wpabuf_alloc(
2260 		wpabuf_len(pw) * 2 + 1);
2261 	if (wps->ap_nfc_dev_pw) {
2262 		wpa_snprintf_hex_uppercase(
2263 			(char *) wpabuf_put(wps->ap_nfc_dev_pw,
2264 					    wpabuf_len(pw) * 2),
2265 			wpabuf_len(pw) * 2 + 1,
2266 			wpabuf_head(pw), wpabuf_len(pw));
2267 	}
2268 
2269 	if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
2270 	    !wps->ap_nfc_dev_pw) {
2271 		hostapd_wps_nfc_clear(wps);
2272 		return -1;
2273 	}
2274 
2275 	return 0;
2276 }
2277 
2278 
hostapd_wps_nfc_token_disable(struct hostapd_data * hapd)2279 void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
2280 {
2281 	wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
2282 		   hapd->conf->iface);
2283 	hostapd_wps_nfc_clear(hapd->wps);
2284 }
2285 
2286 #endif /* CONFIG_WPS_NFC */
2287