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