xref: /freebsd/contrib/wpa/src/ap/wps_hostapd.c (revision 71e72c9e91c4b8007a4292e09669e8b549c29e97)
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 "crypto/dh_group5.h"
24 #include "hostapd.h"
25 #include "ap_config.h"
26 #include "ap_drv_ops.h"
27 #include "beacon.h"
28 #include "sta_info.h"
29 #include "wps_hostapd.h"
30 
31 
32 #ifdef CONFIG_WPS_UPNP
33 #include "wps/wps_upnp.h"
34 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
35 				 struct wps_context *wps);
36 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
37 #endif /* CONFIG_WPS_UPNP */
38 
39 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
40 				    const u8 *bssid,
41 				    const u8 *ie, size_t ie_len,
42 				    int ssi_signal);
43 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
44 static void hostapd_wps_nfc_clear(struct wps_context *wps);
45 
46 
47 struct wps_for_each_data {
48 	int (*func)(struct hostapd_data *h, void *ctx);
49 	void *ctx;
50 	struct hostapd_data *calling_hapd;
51 };
52 
53 
wps_for_each(struct hostapd_iface * iface,void * ctx)54 static int wps_for_each(struct hostapd_iface *iface, void *ctx)
55 {
56 	struct wps_for_each_data *data = ctx;
57 	size_t j;
58 
59 	if (iface == NULL)
60 		return 0;
61 	for (j = 0; j < iface->num_bss; j++) {
62 		struct hostapd_data *hapd = iface->bss[j];
63 		int ret;
64 
65 		if (hapd != data->calling_hapd &&
66 		    (hapd->conf->wps_independent ||
67 		     data->calling_hapd->conf->wps_independent))
68 			continue;
69 
70 		ret = data->func(hapd, data->ctx);
71 		if (ret)
72 			return ret;
73 	}
74 
75 	return 0;
76 }
77 
78 
hostapd_wps_for_each(struct hostapd_data * hapd,int (* func)(struct hostapd_data * h,void * ctx),void * ctx)79 static int hostapd_wps_for_each(struct hostapd_data *hapd,
80 				int (*func)(struct hostapd_data *h, void *ctx),
81 				void *ctx)
82 {
83 	struct hostapd_iface *iface = hapd->iface;
84 	struct wps_for_each_data data;
85 	data.func = func;
86 	data.ctx = ctx;
87 	data.calling_hapd = hapd;
88 	if (iface->interfaces == NULL ||
89 	    iface->interfaces->for_each_interface == NULL)
90 		return wps_for_each(iface, &data);
91 	return iface->interfaces->for_each_interface(iface->interfaces,
92 						     wps_for_each, &data);
93 }
94 
95 
hostapd_wps_new_psk_cb(void * ctx,const u8 * mac_addr,const u8 * p2p_dev_addr,const u8 * psk,size_t psk_len)96 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
97 				  const u8 *p2p_dev_addr, const u8 *psk,
98 				  size_t psk_len)
99 {
100 	struct hostapd_data *hapd = ctx;
101 	struct hostapd_wpa_psk *p;
102 	struct hostapd_ssid *ssid = &hapd->conf->ssid;
103 
104 	if (is_zero_ether_addr(p2p_dev_addr)) {
105 		wpa_printf(MSG_DEBUG,
106 			   "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
107 			   MAC2STR(mac_addr));
108 	} else {
109 		wpa_printf(MSG_DEBUG,
110 			   "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
111 			   " P2P Device Addr " MACSTR,
112 			   MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
113 	}
114 	wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
115 
116 	if (psk_len != PMK_LEN) {
117 		wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
118 			   (unsigned long) psk_len);
119 		return -1;
120 	}
121 
122 	/* Add the new PSK to runtime PSK list */
123 	p = os_zalloc(sizeof(*p));
124 	if (p == NULL)
125 		return -1;
126 	os_memcpy(p->addr, mac_addr, ETH_ALEN);
127 	os_memcpy(p->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
128 	os_memcpy(p->psk, psk, PMK_LEN);
129 	p->wps = 1;
130 
131 	if (hapd->new_psk_cb) {
132 		hapd->new_psk_cb(hapd->new_psk_cb_ctx, mac_addr, p2p_dev_addr,
133 				 psk, psk_len);
134 	}
135 
136 	p->next = ssid->wpa_psk;
137 	ssid->wpa_psk = p;
138 
139 	if (ssid->wpa_psk_file) {
140 		FILE *f;
141 		char hex[PMK_LEN * 2 + 1];
142 
143 		/* Add the new PSK to PSK list file */
144 		f = fopen(ssid->wpa_psk_file, "a");
145 		if (!f) {
146 			wpa_printf(MSG_DEBUG, "Failed to add the PSK to '%s'",
147 				   ssid->wpa_psk_file);
148 			return -1;
149 		}
150 
151 		wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
152 		fprintf(f, "wps=1 " MACSTR " %s\n", MAC2STR(mac_addr), hex);
153 		fclose(f);
154 	}
155 
156 	return 0;
157 }
158 
159 
hostapd_wps_set_ie_cb(void * ctx,struct wpabuf * beacon_ie,struct wpabuf * probe_resp_ie)160 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
161 				 struct wpabuf *probe_resp_ie)
162 {
163 	struct hostapd_data *hapd = ctx;
164 	wpabuf_free(hapd->wps_beacon_ie);
165 	hapd->wps_beacon_ie = beacon_ie;
166 	wpabuf_free(hapd->wps_probe_resp_ie);
167 	hapd->wps_probe_resp_ie = probe_resp_ie;
168 	if (hapd->beacon_set_done)
169 		ieee802_11_set_beacon(hapd);
170 	return hostapd_set_ap_wps_ie(hapd);
171 }
172 
173 
hostapd_wps_pin_needed_cb(void * ctx,const u8 * uuid_e,const struct wps_device_data * dev)174 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
175 				      const struct wps_device_data *dev)
176 {
177 	struct hostapd_data *hapd = ctx;
178 	char uuid[40], txt[400];
179 	int len;
180 	char devtype[WPS_DEV_TYPE_BUFSIZE];
181 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
182 		return;
183 	wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
184 	len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
185 			  "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
186 			  uuid, MAC2STR(dev->mac_addr), dev->device_name,
187 			  dev->manufacturer, dev->model_name,
188 			  dev->model_number, dev->serial_number,
189 			  wps_dev_type_bin2str(dev->pri_dev_type, devtype,
190 					       sizeof(devtype)));
191 	if (!os_snprintf_error(sizeof(txt), len))
192 		wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
193 
194 	if (hapd->conf->wps_pin_requests) {
195 		FILE *f;
196 		struct os_time t;
197 		f = fopen(hapd->conf->wps_pin_requests, "a");
198 		if (f == NULL)
199 			return;
200 		os_get_time(&t);
201 		fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
202 			"\t%s\n",
203 			t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
204 			dev->manufacturer, dev->model_name, dev->model_number,
205 			dev->serial_number,
206 			wps_dev_type_bin2str(dev->pri_dev_type, devtype,
207 					     sizeof(devtype)));
208 		fclose(f);
209 	}
210 }
211 
212 
213 struct wps_stop_reg_data {
214 	struct hostapd_data *current_hapd;
215 	const u8 *uuid_e;
216 	const u8 *dev_pw;
217 	size_t dev_pw_len;
218 };
219 
wps_stop_registrar(struct hostapd_data * hapd,void * ctx)220 static int wps_stop_registrar(struct hostapd_data *hapd, void *ctx)
221 {
222 	struct wps_stop_reg_data *data = ctx;
223 	if (hapd != data->current_hapd && hapd->wps != NULL)
224 		wps_registrar_complete(hapd->wps->registrar, data->uuid_e,
225 				       data->dev_pw, data->dev_pw_len);
226 	return 0;
227 }
228 
229 
hostapd_wps_reg_success_cb(void * ctx,const u8 * mac_addr,const u8 * uuid_e,const u8 * dev_pw,size_t dev_pw_len)230 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
231 				       const u8 *uuid_e, const u8 *dev_pw,
232 				       size_t dev_pw_len)
233 {
234 	struct hostapd_data *hapd = ctx;
235 	char uuid[40];
236 	struct wps_stop_reg_data data;
237 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
238 		return;
239 	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
240 		MAC2STR(mac_addr), 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,
482 		   MAC2STR(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 	dh5_free(wps->dh_ctx);
1076 	wpabuf_free(wps->dh_pubkey);
1077 	wpabuf_free(wps->dh_privkey);
1078 	forced_memzero(wps->psk, sizeof(wps->psk));
1079 	os_free(wps);
1080 }
1081 
1082 
hostapd_init_wps(struct hostapd_data * hapd,struct hostapd_bss_config * conf)1083 int hostapd_init_wps(struct hostapd_data *hapd,
1084 		     struct hostapd_bss_config *conf)
1085 {
1086 	struct wps_context *wps;
1087 	struct wps_registrar_config cfg;
1088 	u8 *multi_ap_netw_key = NULL;
1089 
1090 	if (conf->wps_state == 0) {
1091 		hostapd_wps_clear_ies(hapd, 0);
1092 		return 0;
1093 	}
1094 
1095 	wps = os_zalloc(sizeof(*wps));
1096 	if (wps == NULL)
1097 		return -1;
1098 
1099 	wps->cred_cb = hostapd_wps_cred_cb;
1100 	wps->event_cb = hostapd_wps_event_cb;
1101 	wps->rf_band_cb = hostapd_wps_rf_band_cb;
1102 	wps->cb_ctx = hapd;
1103 
1104 	os_memset(&cfg, 0, sizeof(cfg));
1105 	wps->wps_state = hapd->conf->wps_state;
1106 	wps->ap_setup_locked = hapd->conf->ap_setup_locked;
1107 	if (is_nil_uuid(hapd->conf->uuid)) {
1108 		const u8 *uuid;
1109 		uuid = get_own_uuid(hapd->iface);
1110 		if (uuid && !conf->wps_independent) {
1111 			os_memcpy(wps->uuid, uuid, UUID_LEN);
1112 			wpa_hexdump(MSG_DEBUG, "WPS: Clone UUID from another "
1113 				    "interface", wps->uuid, UUID_LEN);
1114 		} else {
1115 			uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
1116 			wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
1117 				    "address", wps->uuid, UUID_LEN);
1118 		}
1119 	} else {
1120 		os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
1121 		wpa_hexdump(MSG_DEBUG, "WPS: Use configured UUID",
1122 			    wps->uuid, UUID_LEN);
1123 	}
1124 	wps->ssid_len = hapd->conf->ssid.ssid_len;
1125 	os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
1126 	wps->ap = 1;
1127 	os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
1128 	wps->dev.device_name = hapd->conf->device_name ?
1129 		os_strdup(hapd->conf->device_name) : NULL;
1130 	wps->dev.manufacturer = hapd->conf->manufacturer ?
1131 		os_strdup(hapd->conf->manufacturer) : NULL;
1132 	wps->dev.model_name = hapd->conf->model_name ?
1133 		os_strdup(hapd->conf->model_name) : NULL;
1134 	wps->dev.model_number = hapd->conf->model_number ?
1135 		os_strdup(hapd->conf->model_number) : NULL;
1136 	wps->dev.serial_number = hapd->conf->serial_number ?
1137 		os_strdup(hapd->conf->serial_number) : NULL;
1138 	wps->config_methods =
1139 		wps_config_methods_str2bin(hapd->conf->config_methods);
1140 	if ((wps->config_methods &
1141 	     (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
1142 	      WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
1143 		wpa_printf(MSG_INFO, "WPS: Converting display to "
1144 			   "virtual_display for WPS 2.0 compliance");
1145 		wps->config_methods |= WPS_CONFIG_VIRT_DISPLAY;
1146 	}
1147 	if ((wps->config_methods &
1148 	     (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
1149 	      WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
1150 		wpa_printf(MSG_INFO, "WPS: Converting push_button to "
1151 			   "virtual_push_button for WPS 2.0 compliance");
1152 		wps->config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
1153 	}
1154 	os_memcpy(wps->dev.pri_dev_type, hapd->conf->device_type,
1155 		  WPS_DEV_TYPE_LEN);
1156 
1157 	if (hostapd_wps_set_vendor_ext(hapd, wps) < 0 ||
1158 	    hostapd_wps_set_application_ext(hapd, wps) < 0)
1159 		goto fail;
1160 
1161 	wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
1162 
1163 	if (conf->wps_rf_bands) {
1164 		wps->dev.rf_bands = conf->wps_rf_bands;
1165 	} else {
1166 		wps->dev.rf_bands =
1167 			hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
1168 			WPS_RF_50GHZ :
1169 			hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD ?
1170 			WPS_RF_60GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
1171 	}
1172 
1173 	if (conf->wpa & WPA_PROTO_RSN) {
1174 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1175 			wps->auth_types |= WPS_AUTH_WPA2PSK;
1176 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1177 			wps->auth_types |= WPS_AUTH_WPA2;
1178 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE)
1179 			wps->auth_types |= WPS_AUTH_WPA2PSK;
1180 
1181 		if (conf->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
1182 					  WPA_CIPHER_CCMP_256 |
1183 					  WPA_CIPHER_GCMP_256)) {
1184 			wps->encr_types |= WPS_ENCR_AES;
1185 			wps->encr_types_rsn |= WPS_ENCR_AES;
1186 		}
1187 		if (conf->rsn_pairwise & WPA_CIPHER_TKIP) {
1188 #ifdef CONFIG_NO_TKIP
1189 			wpa_printf(MSG_INFO, "WPS: TKIP not supported");
1190 			goto fail;
1191 #else /* CONFIG_NO_TKIP */
1192 			wps->encr_types |= WPS_ENCR_TKIP;
1193 			wps->encr_types_rsn |= WPS_ENCR_TKIP;
1194 #endif /* CONFIG_NO_TKIP */
1195 		}
1196 	}
1197 
1198 	if (conf->wpa & WPA_PROTO_WPA) {
1199 #ifdef CONFIG_NO_TKIP
1200 		if (!(conf->wpa & WPA_PROTO_RSN)) {
1201 			wpa_printf(MSG_INFO, "WPS: WPA(v1) not supported");
1202 			goto fail;
1203 		}
1204 		conf->wpa &= ~WPA_PROTO_WPA;
1205 #else /* CONFIG_NO_TKIP */
1206 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
1207 			wps->auth_types |= WPS_AUTH_WPAPSK;
1208 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1209 			wps->auth_types |= WPS_AUTH_WPA;
1210 
1211 		if (conf->wpa_pairwise & WPA_CIPHER_CCMP) {
1212 			wps->encr_types |= WPS_ENCR_AES;
1213 			wps->encr_types_wpa |= WPS_ENCR_AES;
1214 		}
1215 		if (conf->wpa_pairwise & WPA_CIPHER_TKIP) {
1216 			wps->encr_types |= WPS_ENCR_TKIP;
1217 			wps->encr_types_wpa |= WPS_ENCR_TKIP;
1218 		}
1219 #endif /* CONFIG_NO_TKIP */
1220 	}
1221 
1222 	if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
1223 		wps->encr_types |= WPS_ENCR_NONE;
1224 		wps->auth_types |= WPS_AUTH_OPEN;
1225 	}
1226 
1227 	if (conf->ssid.wpa_psk_file) {
1228 		/* Use per-device PSKs */
1229 	} else if (conf->ssid.wpa_passphrase) {
1230 		wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
1231 		wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
1232 	} else if (conf->ssid.wpa_psk) {
1233 		wps->network_key = os_malloc(2 * PMK_LEN + 1);
1234 		if (wps->network_key == NULL)
1235 			goto fail;
1236 		wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
1237 				 conf->ssid.wpa_psk->psk, PMK_LEN);
1238 		wps->network_key_len = 2 * PMK_LEN;
1239 #ifdef CONFIG_WEP
1240 	} else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
1241 		wps->network_key = os_malloc(conf->ssid.wep.len[0]);
1242 		if (wps->network_key == NULL)
1243 			goto fail;
1244 		os_memcpy(wps->network_key, conf->ssid.wep.key[0],
1245 			  conf->ssid.wep.len[0]);
1246 		wps->network_key_len = conf->ssid.wep.len[0];
1247 #endif /* CONFIG_WEP */
1248 	}
1249 
1250 	if (conf->ssid.wpa_psk) {
1251 		os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
1252 		wps->psk_set = 1;
1253 	}
1254 
1255 	wps->ap_auth_type = wps->auth_types;
1256 	wps->ap_encr_type = wps->encr_types;
1257 	if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
1258 		/* Override parameters to enable security by default */
1259 #ifdef CONFIG_NO_TKIP
1260 		wps->auth_types = WPS_AUTH_WPA2PSK;
1261 		wps->encr_types = WPS_ENCR_AES;
1262 		wps->encr_types_rsn = WPS_ENCR_AES;
1263 		wps->encr_types_wpa = WPS_ENCR_AES;
1264 #else /* CONFIG_NO_TKIP */
1265 		wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
1266 		wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
1267 		wps->encr_types_rsn = WPS_ENCR_AES | WPS_ENCR_TKIP;
1268 		wps->encr_types_wpa = WPS_ENCR_AES | WPS_ENCR_TKIP;
1269 #endif /* CONFIG_NO_TKIP */
1270 	}
1271 
1272 	if ((hapd->conf->multi_ap & FRONTHAUL_BSS) &&
1273 	    hapd->conf->multi_ap_backhaul_ssid.ssid_len) {
1274 		cfg.multi_ap_backhaul_ssid_len =
1275 			hapd->conf->multi_ap_backhaul_ssid.ssid_len;
1276 		cfg.multi_ap_backhaul_ssid =
1277 			hapd->conf->multi_ap_backhaul_ssid.ssid;
1278 
1279 		if (conf->multi_ap_backhaul_ssid.wpa_passphrase) {
1280 			cfg.multi_ap_backhaul_network_key = (const u8 *)
1281 				conf->multi_ap_backhaul_ssid.wpa_passphrase;
1282 			cfg.multi_ap_backhaul_network_key_len =
1283 				os_strlen(conf->multi_ap_backhaul_ssid.wpa_passphrase);
1284 		} else if (conf->multi_ap_backhaul_ssid.wpa_psk) {
1285 			multi_ap_netw_key = os_malloc(2 * PMK_LEN + 1);
1286 			if (!multi_ap_netw_key)
1287 				goto fail;
1288 			wpa_snprintf_hex((char *) multi_ap_netw_key,
1289 					 2 * PMK_LEN + 1,
1290 					 conf->multi_ap_backhaul_ssid.wpa_psk->psk,
1291 					 PMK_LEN);
1292 			cfg.multi_ap_backhaul_network_key = multi_ap_netw_key;
1293 			cfg.multi_ap_backhaul_network_key_len = 2 * PMK_LEN;
1294 		}
1295 	}
1296 
1297 	wps->ap_settings = conf->ap_settings;
1298 	wps->ap_settings_len = conf->ap_settings_len;
1299 
1300 	cfg.new_psk_cb = hostapd_wps_new_psk_cb;
1301 	cfg.set_ie_cb = hostapd_wps_set_ie_cb;
1302 	cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
1303 	cfg.reg_success_cb = hostapd_wps_reg_success_cb;
1304 	cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
1305 	cfg.lookup_pskfile_cb = hostapd_wps_lookup_pskfile_cb;
1306 	cfg.cb_ctx = hapd;
1307 	cfg.skip_cred_build = conf->skip_cred_build;
1308 	cfg.extra_cred = conf->extra_cred;
1309 	cfg.extra_cred_len = conf->extra_cred_len;
1310 	cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
1311 		conf->skip_cred_build;
1312 	cfg.dualband = interface_count(hapd->iface) > 1;
1313 	if ((wps->dev.rf_bands & (WPS_RF_50GHZ | WPS_RF_24GHZ)) ==
1314 	    (WPS_RF_50GHZ | WPS_RF_24GHZ))
1315 		cfg.dualband = 1;
1316 	if (cfg.dualband)
1317 		wpa_printf(MSG_DEBUG, "WPS: Dualband AP");
1318 	cfg.force_per_enrollee_psk = conf->force_per_enrollee_psk;
1319 
1320 	wps->registrar = wps_registrar_init(wps, &cfg);
1321 	if (wps->registrar == NULL) {
1322 		wpa_printf(MSG_ERROR, "Failed to initialize WPS Registrar");
1323 		goto fail;
1324 	}
1325 
1326 #ifdef CONFIG_WPS_UPNP
1327 	wps->friendly_name = hapd->conf->friendly_name;
1328 	wps->manufacturer_url = hapd->conf->manufacturer_url;
1329 	wps->model_description = hapd->conf->model_description;
1330 	wps->model_url = hapd->conf->model_url;
1331 	wps->upc = hapd->conf->upc;
1332 #endif /* CONFIG_WPS_UPNP */
1333 
1334 	hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
1335 
1336 #ifdef CONFIG_P2P
1337 	if ((hapd->conf->p2p & P2P_ENABLED) &&
1338 	    is_6ghz_op_class(hapd->iconf->op_class))
1339 		wps->use_passphrase = true;
1340 #endif /* CONFIG_P2P */
1341 	hapd->wps = wps;
1342 	bin_clear_free(multi_ap_netw_key, 2 * PMK_LEN);
1343 
1344 	return 0;
1345 
1346 fail:
1347 	bin_clear_free(multi_ap_netw_key, 2 * PMK_LEN);
1348 	hostapd_free_wps(wps);
1349 	return -1;
1350 }
1351 
1352 
hostapd_init_wps_complete(struct hostapd_data * hapd)1353 int hostapd_init_wps_complete(struct hostapd_data *hapd)
1354 {
1355 	struct wps_context *wps = hapd->wps;
1356 
1357 	if (wps == NULL)
1358 		return 0;
1359 
1360 #ifdef CONFIG_WPS_UPNP
1361 	if (hostapd_wps_upnp_init(hapd, wps) < 0) {
1362 		wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
1363 		wps_registrar_deinit(wps->registrar);
1364 		hostapd_free_wps(wps);
1365 		hapd->wps = NULL;
1366 		return -1;
1367 	}
1368 #endif /* CONFIG_WPS_UPNP */
1369 
1370 	return 0;
1371 }
1372 
1373 
hostapd_wps_nfc_clear(struct wps_context * wps)1374 static void hostapd_wps_nfc_clear(struct wps_context *wps)
1375 {
1376 #ifdef CONFIG_WPS_NFC
1377 	wpa_printf(MSG_DEBUG, "WPS: Clear NFC Tag context %p", wps);
1378 	wps->ap_nfc_dev_pw_id = 0;
1379 	wpabuf_free(wps->ap_nfc_dh_pubkey);
1380 	wps->ap_nfc_dh_pubkey = NULL;
1381 	wpabuf_free(wps->ap_nfc_dh_privkey);
1382 	wps->ap_nfc_dh_privkey = NULL;
1383 	wpabuf_free(wps->ap_nfc_dev_pw);
1384 	wps->ap_nfc_dev_pw = NULL;
1385 #endif /* CONFIG_WPS_NFC */
1386 }
1387 
1388 
hostapd_wps_update_multi_ap(struct hostapd_data * hapd,struct wps_registrar * reg)1389 static int hostapd_wps_update_multi_ap(struct hostapd_data *hapd,
1390 				       struct wps_registrar *reg)
1391 {
1392 	struct hostapd_bss_config *conf = hapd->conf;
1393 	u8 *multi_ap_backhaul_network_key = NULL;
1394 	size_t multi_ap_backhaul_network_key_len = 0;
1395 	int ret;
1396 
1397 	if (!(conf->multi_ap & FRONTHAUL_BSS) ||
1398 	    !conf->multi_ap_backhaul_ssid.ssid_len)
1399 		return 0;
1400 
1401 	if (conf->multi_ap_backhaul_ssid.wpa_passphrase) {
1402 		multi_ap_backhaul_network_key =
1403 			(u8 *) os_strdup(
1404 				conf->multi_ap_backhaul_ssid.wpa_passphrase);
1405 		if (!multi_ap_backhaul_network_key)
1406 			return -1;
1407 		multi_ap_backhaul_network_key_len =
1408 			os_strlen(conf->multi_ap_backhaul_ssid.wpa_passphrase);
1409 	} else if (conf->multi_ap_backhaul_ssid.wpa_psk) {
1410 		multi_ap_backhaul_network_key = os_malloc(2 * PMK_LEN + 1);
1411 		if (!multi_ap_backhaul_network_key)
1412 			return -1;
1413 		wpa_snprintf_hex((char *) multi_ap_backhaul_network_key,
1414 				 2 * PMK_LEN + 1,
1415 				 conf->multi_ap_backhaul_ssid.wpa_psk->psk,
1416 				 PMK_LEN);
1417 		multi_ap_backhaul_network_key_len = 2 * PMK_LEN;
1418 	}
1419 
1420 	ret = wps_registrar_update_multi_ap(
1421 		reg, conf->multi_ap_backhaul_ssid.ssid,
1422 		conf->multi_ap_backhaul_ssid.ssid_len,
1423 		multi_ap_backhaul_network_key,
1424 		multi_ap_backhaul_network_key_len);
1425 	os_free(multi_ap_backhaul_network_key);
1426 
1427 	return ret;
1428 }
1429 
1430 
hostapd_deinit_wps(struct hostapd_data * hapd)1431 void hostapd_deinit_wps(struct hostapd_data *hapd)
1432 {
1433 	eloop_cancel_timeout(hostapd_wps_reenable_ap_pin, hapd, NULL);
1434 	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1435 	eloop_cancel_timeout(wps_reload_config, hapd->iface, NULL);
1436 	if (hapd->wps == NULL) {
1437 		hostapd_wps_clear_ies(hapd, 1);
1438 		return;
1439 	}
1440 #ifdef CONFIG_WPS_UPNP
1441 	hostapd_wps_upnp_deinit(hapd);
1442 #endif /* CONFIG_WPS_UPNP */
1443 	wps_registrar_deinit(hapd->wps->registrar);
1444 	wps_free_pending_msgs(hapd->wps->upnp_msgs);
1445 	hostapd_free_wps(hapd->wps);
1446 	hapd->wps = NULL;
1447 	hostapd_wps_clear_ies(hapd, 1);
1448 }
1449 
1450 
hostapd_update_wps(struct hostapd_data * hapd)1451 void hostapd_update_wps(struct hostapd_data *hapd)
1452 {
1453 	struct wps_context *wps = hapd->wps;
1454 	struct hostapd_bss_config *conf = hapd->conf;
1455 
1456 	if (!wps)
1457 		return;
1458 
1459 #ifdef CONFIG_WPS_UPNP
1460 	wps->friendly_name = conf->friendly_name;
1461 	wps->manufacturer_url = conf->manufacturer_url;
1462 	wps->model_description = conf->model_description;
1463 	wps->model_url = conf->model_url;
1464 	wps->upc = conf->upc;
1465 #endif /* CONFIG_WPS_UPNP */
1466 
1467 	os_memcpy(wps->ssid, conf->ssid.ssid, conf->ssid.ssid_len);
1468 	wps->ssid_len = conf->ssid.ssid_len;
1469 
1470 	/* Clear WPS settings, then fill them again */
1471 	os_free(wps->network_key);
1472 	wps->network_key = NULL;
1473 	wps->network_key_len = 0;
1474 	wps->psk_set = 0;
1475 	if (conf->ssid.wpa_psk_file) {
1476 		/* Use per-device PSKs */
1477 	} else if (conf->ssid.wpa_passphrase) {
1478 		wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
1479 		if (!wps->network_key)
1480 			return;
1481 		wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
1482 	} else if (conf->ssid.wpa_psk) {
1483 		wps->network_key = os_malloc(2 * PMK_LEN + 1);
1484 		if (!wps->network_key)
1485 			return;
1486 		wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
1487 				 conf->ssid.wpa_psk->psk, PMK_LEN);
1488 		wps->network_key_len = 2 * PMK_LEN;
1489 #ifdef CONFIG_WEP
1490 	} else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
1491 		wps->network_key = os_malloc(conf->ssid.wep.len[0]);
1492 		if (!wps->network_key)
1493 			return;
1494 		os_memcpy(wps->network_key, conf->ssid.wep.key[0],
1495 			  conf->ssid.wep.len[0]);
1496 		wps->network_key_len = conf->ssid.wep.len[0];
1497 #endif /* CONFIG_WEP */
1498 	}
1499 
1500 	if (conf->ssid.wpa_psk) {
1501 		os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
1502 		wps->psk_set = 1;
1503 	}
1504 
1505 	hostapd_wps_update_multi_ap(hapd, wps->registrar);
1506 
1507 	hostapd_wps_set_vendor_ext(hapd, wps);
1508 	hostapd_wps_set_application_ext(hapd, wps);
1509 
1510 	if (conf->wps_state)
1511 		wps_registrar_update_ie(wps->registrar);
1512 	else
1513 		hostapd_deinit_wps(hapd);
1514 }
1515 
1516 
1517 struct wps_add_pin_data {
1518 	const u8 *addr;
1519 	const u8 *uuid;
1520 	const u8 *pin;
1521 	size_t pin_len;
1522 	int timeout;
1523 	int added;
1524 };
1525 
1526 
wps_add_pin(struct hostapd_data * hapd,void * ctx)1527 static int wps_add_pin(struct hostapd_data *hapd, void *ctx)
1528 {
1529 	struct wps_add_pin_data *data = ctx;
1530 	int ret;
1531 
1532 	if (hapd->wps == NULL)
1533 		return 0;
1534 	ret = wps_registrar_add_pin(hapd->wps->registrar, data->addr,
1535 				    data->uuid, data->pin, data->pin_len,
1536 				    data->timeout);
1537 	if (ret == 0)
1538 		data->added++;
1539 	return ret;
1540 }
1541 
1542 
hostapd_wps_add_pin(struct hostapd_data * hapd,const u8 * addr,const char * uuid,const char * pin,int timeout)1543 int hostapd_wps_add_pin(struct hostapd_data *hapd, const u8 *addr,
1544 			const char *uuid, const char *pin, int timeout)
1545 {
1546 	u8 u[UUID_LEN];
1547 	struct wps_add_pin_data data;
1548 
1549 	data.addr = addr;
1550 	data.uuid = u;
1551 	data.pin = (const u8 *) pin;
1552 	data.pin_len = os_strlen(pin);
1553 	data.timeout = timeout;
1554 	data.added = 0;
1555 
1556 	if (os_strcmp(uuid, "any") == 0)
1557 		data.uuid = NULL;
1558 	else {
1559 		if (uuid_str2bin(uuid, u))
1560 			return -1;
1561 		data.uuid = u;
1562 	}
1563 	if (hostapd_wps_for_each(hapd, wps_add_pin, &data) < 0)
1564 		return -1;
1565 	return data.added ? 0 : -1;
1566 }
1567 
1568 
1569 struct wps_button_pushed_ctx {
1570 	const u8 *p2p_dev_addr;
1571 	unsigned int count;
1572 };
1573 
wps_button_pushed(struct hostapd_data * hapd,void * ctx)1574 static int wps_button_pushed(struct hostapd_data *hapd, void *ctx)
1575 {
1576 	struct wps_button_pushed_ctx *data = ctx;
1577 
1578 	if (hapd->wps) {
1579 		data->count++;
1580 		return wps_registrar_button_pushed(hapd->wps->registrar,
1581 						   data->p2p_dev_addr);
1582 	}
1583 
1584 	return 0;
1585 }
1586 
1587 
hostapd_wps_button_pushed(struct hostapd_data * hapd,const u8 * p2p_dev_addr)1588 int hostapd_wps_button_pushed(struct hostapd_data *hapd,
1589 			      const u8 *p2p_dev_addr)
1590 {
1591 	struct wps_button_pushed_ctx ctx;
1592 	int ret;
1593 
1594 	os_memset(&ctx, 0, sizeof(ctx));
1595 	ctx.p2p_dev_addr = p2p_dev_addr;
1596 	ret = hostapd_wps_for_each(hapd, wps_button_pushed, &ctx);
1597 	if (ret == 0 && !ctx.count)
1598 		ret = -1;
1599 	return ret;
1600 }
1601 
1602 
1603 struct wps_cancel_ctx {
1604 	unsigned int count;
1605 };
1606 
wps_cancel(struct hostapd_data * hapd,void * ctx)1607 static int wps_cancel(struct hostapd_data *hapd, void *ctx)
1608 {
1609 	struct wps_cancel_ctx *data = ctx;
1610 
1611 	if (hapd->wps) {
1612 		data->count++;
1613 		wps_registrar_wps_cancel(hapd->wps->registrar);
1614 		ap_for_each_sta(hapd, ap_sta_wps_cancel, NULL);
1615 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_CANCEL);
1616 	}
1617 
1618 	return 0;
1619 }
1620 
1621 
hostapd_wps_cancel(struct hostapd_data * hapd)1622 int hostapd_wps_cancel(struct hostapd_data *hapd)
1623 {
1624 	struct wps_cancel_ctx ctx;
1625 	int ret;
1626 
1627 	os_memset(&ctx, 0, sizeof(ctx));
1628 	ret = hostapd_wps_for_each(hapd, wps_cancel, &ctx);
1629 	if (ret == 0 && !ctx.count)
1630 		ret = -1;
1631 	return ret;
1632 }
1633 
1634 
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)1635 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr, const u8 *da,
1636 				    const u8 *bssid,
1637 				    const u8 *ie, size_t ie_len,
1638 				    int ssi_signal)
1639 {
1640 	struct hostapd_data *hapd = ctx;
1641 	struct wpabuf *wps_ie;
1642 	struct ieee802_11_elems elems;
1643 
1644 	if (hapd->wps == NULL)
1645 		return 0;
1646 
1647 	if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
1648 		wpa_printf(MSG_DEBUG, "WPS: Could not parse ProbeReq from "
1649 			   MACSTR, MAC2STR(addr));
1650 		return 0;
1651 	}
1652 
1653 	if (elems.ssid && elems.ssid_len > 0 &&
1654 	    (elems.ssid_len != hapd->conf->ssid.ssid_len ||
1655 	     os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) !=
1656 	     0))
1657 		return 0; /* Not for us */
1658 
1659 	wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1660 	if (wps_ie == NULL)
1661 		return 0;
1662 	if (wps_validate_probe_req(wps_ie, addr) < 0) {
1663 		wpabuf_free(wps_ie);
1664 		return 0;
1665 	}
1666 
1667 	if (wpabuf_len(wps_ie) > 0) {
1668 		int p2p_wildcard = 0;
1669 #ifdef CONFIG_P2P
1670 		if (elems.ssid && elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
1671 		    os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
1672 			      P2P_WILDCARD_SSID_LEN) == 0)
1673 			p2p_wildcard = 1;
1674 #endif /* CONFIG_P2P */
1675 		wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie,
1676 					   p2p_wildcard);
1677 #ifdef CONFIG_WPS_UPNP
1678 		/* FIX: what exactly should be included in the WLANEvent?
1679 		 * WPS attributes? Full ProbeReq frame? */
1680 		if (!p2p_wildcard)
1681 			upnp_wps_device_send_wlan_event(
1682 				hapd->wps_upnp, addr,
1683 				UPNP_WPS_WLANEVENT_TYPE_PROBE, wps_ie);
1684 #endif /* CONFIG_WPS_UPNP */
1685 	}
1686 
1687 	wpabuf_free(wps_ie);
1688 
1689 	return 0;
1690 }
1691 
1692 
1693 #ifdef CONFIG_WPS_UPNP
1694 
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)1695 static int hostapd_rx_req_put_wlan_response(
1696 	void *priv, enum upnp_wps_wlanevent_type ev_type,
1697 	const u8 *mac_addr, const struct wpabuf *msg,
1698 	enum wps_msg_type msg_type)
1699 {
1700 	struct hostapd_data *hapd = priv;
1701 	struct sta_info *sta;
1702 	struct upnp_pending_message *p;
1703 
1704 	wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
1705 		   MACSTR, ev_type, MAC2STR(mac_addr));
1706 	wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
1707 		    wpabuf_head(msg), wpabuf_len(msg));
1708 	if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
1709 		wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
1710 			   "PutWLANResponse WLANEventType %d", ev_type);
1711 		return -1;
1712 	}
1713 
1714 	/*
1715 	 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
1716 	 * server implementation for delivery to the peer.
1717 	 */
1718 
1719 	sta = ap_get_sta(hapd, mac_addr);
1720 #ifndef CONFIG_WPS_STRICT
1721 	if (!sta) {
1722 		/*
1723 		 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
1724 		 * Pick STA that is in an ongoing WPS registration without
1725 		 * checking the MAC address.
1726 		 */
1727 		wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
1728 			   "on NewWLANEventMAC; try wildcard match");
1729 		for (sta = hapd->sta_list; sta; sta = sta->next) {
1730 			if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
1731 				break;
1732 		}
1733 	}
1734 #endif /* CONFIG_WPS_STRICT */
1735 
1736 	if (!sta || !(sta->flags & WLAN_STA_WPS)) {
1737 		wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
1738 		return 0;
1739 	}
1740 
1741 	if (!sta->eapol_sm) {
1742 		/*
1743 		 * This can happen, e.g., if an ER sends an extra message after
1744 		 * the station has disassociated (but not fully
1745 		 * deauthenticated).
1746 		 */
1747 		wpa_printf(MSG_DEBUG, "WPS UPnP: Matching STA did not have EAPOL state machine initialized");
1748 		return 0;
1749 	}
1750 
1751 	p = os_zalloc(sizeof(*p));
1752 	if (p == NULL)
1753 		return -1;
1754 	os_memcpy(p->addr, sta->addr, ETH_ALEN);
1755 	p->msg = wpabuf_dup(msg);
1756 	p->type = msg_type;
1757 	p->next = hapd->wps->upnp_msgs;
1758 	hapd->wps->upnp_msgs = p;
1759 
1760 	return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
1761 }
1762 
1763 
hostapd_wps_upnp_init(struct hostapd_data * hapd,struct wps_context * wps)1764 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1765 				 struct wps_context *wps)
1766 {
1767 	struct upnp_wps_device_ctx *ctx;
1768 
1769 	if (!hapd->conf->upnp_iface)
1770 		return 0;
1771 	ctx = os_zalloc(sizeof(*ctx));
1772 	if (ctx == NULL)
1773 		return -1;
1774 
1775 	ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1776 	if (hapd->conf->ap_pin)
1777 		ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
1778 
1779 	hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd,
1780 					      hapd->conf->upnp_iface);
1781 	if (hapd->wps_upnp == NULL)
1782 		return -1;
1783 	wps->wps_upnp = hapd->wps_upnp;
1784 
1785 	return 0;
1786 }
1787 
1788 
hostapd_wps_upnp_deinit(struct hostapd_data * hapd)1789 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1790 {
1791 	upnp_wps_device_deinit(hapd->wps_upnp, hapd);
1792 }
1793 
1794 #endif /* CONFIG_WPS_UPNP */
1795 
1796 
hostapd_wps_get_mib_sta(struct hostapd_data * hapd,const u8 * addr,char * buf,size_t buflen)1797 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
1798 			    char *buf, size_t buflen)
1799 {
1800 	if (hapd->wps == NULL)
1801 		return 0;
1802 	return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
1803 }
1804 
1805 
hostapd_wps_ap_pin_timeout(void * eloop_data,void * user_ctx)1806 static void hostapd_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1807 {
1808 	struct hostapd_data *hapd = eloop_data;
1809 	wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1810 	hostapd_wps_ap_pin_disable(hapd);
1811 	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_PIN_DISABLED);
1812 }
1813 
1814 
hostapd_wps_ap_pin_enable(struct hostapd_data * hapd,int timeout)1815 static void hostapd_wps_ap_pin_enable(struct hostapd_data *hapd, int timeout)
1816 {
1817 	wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1818 	hapd->ap_pin_failures = 0;
1819 	hapd->ap_pin_failures_consecutive = 0;
1820 	hapd->conf->ap_setup_locked = 0;
1821 	if (hapd->wps->ap_setup_locked) {
1822 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_UNLOCKED);
1823 		hapd->wps->ap_setup_locked = 0;
1824 		wps_registrar_update_ie(hapd->wps->registrar);
1825 	}
1826 	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1827 	if (timeout > 0)
1828 		eloop_register_timeout(timeout, 0,
1829 				       hostapd_wps_ap_pin_timeout, hapd, NULL);
1830 }
1831 
1832 
wps_ap_pin_disable(struct hostapd_data * hapd,void * ctx)1833 static int wps_ap_pin_disable(struct hostapd_data *hapd, void *ctx)
1834 {
1835 	os_free(hapd->conf->ap_pin);
1836 	hapd->conf->ap_pin = NULL;
1837 #ifdef CONFIG_WPS_UPNP
1838 	upnp_wps_set_ap_pin(hapd->wps_upnp, NULL);
1839 #endif /* CONFIG_WPS_UPNP */
1840 	eloop_cancel_timeout(hostapd_wps_ap_pin_timeout, hapd, NULL);
1841 	return 0;
1842 }
1843 
1844 
hostapd_wps_ap_pin_disable(struct hostapd_data * hapd)1845 void hostapd_wps_ap_pin_disable(struct hostapd_data *hapd)
1846 {
1847 	wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1848 	hostapd_wps_for_each(hapd, wps_ap_pin_disable, NULL);
1849 }
1850 
1851 
1852 struct wps_ap_pin_data {
1853 	char pin_txt[9];
1854 	int timeout;
1855 };
1856 
1857 
wps_ap_pin_set(struct hostapd_data * hapd,void * ctx)1858 static int wps_ap_pin_set(struct hostapd_data *hapd, void *ctx)
1859 {
1860 	struct wps_ap_pin_data *data = ctx;
1861 
1862 	if (!hapd->wps)
1863 		return 0;
1864 
1865 	os_free(hapd->conf->ap_pin);
1866 	hapd->conf->ap_pin = os_strdup(data->pin_txt);
1867 #ifdef CONFIG_WPS_UPNP
1868 	upnp_wps_set_ap_pin(hapd->wps_upnp, data->pin_txt);
1869 #endif /* CONFIG_WPS_UPNP */
1870 	hostapd_wps_ap_pin_enable(hapd, data->timeout);
1871 	return 0;
1872 }
1873 
1874 
hostapd_wps_ap_pin_random(struct hostapd_data * hapd,int timeout)1875 const char * hostapd_wps_ap_pin_random(struct hostapd_data *hapd, int timeout)
1876 {
1877 	unsigned int pin;
1878 	struct wps_ap_pin_data data;
1879 
1880 	if (wps_generate_pin(&pin) < 0)
1881 		return NULL;
1882 	os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%08u", pin);
1883 	data.timeout = timeout;
1884 	hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1885 	return hapd->conf->ap_pin;
1886 }
1887 
1888 
hostapd_wps_ap_pin_get(struct hostapd_data * hapd)1889 const char * hostapd_wps_ap_pin_get(struct hostapd_data *hapd)
1890 {
1891 	return hapd->conf->ap_pin;
1892 }
1893 
1894 
hostapd_wps_ap_pin_set(struct hostapd_data * hapd,const char * pin,int timeout)1895 int hostapd_wps_ap_pin_set(struct hostapd_data *hapd, const char *pin,
1896 			   int timeout)
1897 {
1898 	struct wps_ap_pin_data data;
1899 	int ret;
1900 
1901 	ret = os_snprintf(data.pin_txt, sizeof(data.pin_txt), "%s", pin);
1902 	if (os_snprintf_error(sizeof(data.pin_txt), ret))
1903 		return -1;
1904 	data.timeout = timeout;
1905 	return hostapd_wps_for_each(hapd, wps_ap_pin_set, &data);
1906 }
1907 
1908 
wps_update_ie(struct hostapd_data * hapd,void * ctx)1909 static int wps_update_ie(struct hostapd_data *hapd, void *ctx)
1910 {
1911 	if (hapd->wps)
1912 		wps_registrar_update_ie(hapd->wps->registrar);
1913 	return 0;
1914 }
1915 
1916 
hostapd_wps_update_ie(struct hostapd_data * hapd)1917 void hostapd_wps_update_ie(struct hostapd_data *hapd)
1918 {
1919 	hostapd_wps_for_each(hapd, wps_update_ie, NULL);
1920 }
1921 
1922 
hostapd_wps_config_ap(struct hostapd_data * hapd,const char * ssid,const char * auth,const char * encr,const char * key)1923 int hostapd_wps_config_ap(struct hostapd_data *hapd, const char *ssid,
1924 			  const char *auth, const char *encr, const char *key)
1925 {
1926 	struct wps_credential cred;
1927 	size_t len;
1928 
1929 	os_memset(&cred, 0, sizeof(cred));
1930 
1931 	len = os_strlen(ssid);
1932 	if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
1933 	    hexstr2bin(ssid, cred.ssid, len / 2))
1934 		return -1;
1935 	cred.ssid_len = len / 2;
1936 
1937 	if (os_strncmp(auth, "OPEN", 4) == 0)
1938 		cred.auth_type = WPS_AUTH_OPEN;
1939 #ifndef CONFIG_NO_TKIP
1940 	else if (os_strncmp(auth, "WPAPSK", 6) == 0)
1941 		cred.auth_type = WPS_AUTH_WPAPSK;
1942 #endif /* CONFIG_NO_TKIP */
1943 	else if (os_strncmp(auth, "WPA2PSK", 7) == 0)
1944 		cred.auth_type = WPS_AUTH_WPA2PSK;
1945 	else
1946 		return -1;
1947 
1948 	if (encr) {
1949 		if (os_strncmp(encr, "NONE", 4) == 0)
1950 			cred.encr_type = WPS_ENCR_NONE;
1951 #ifndef CONFIG_NO_TKIP
1952 		else if (os_strncmp(encr, "TKIP", 4) == 0)
1953 			cred.encr_type = WPS_ENCR_TKIP;
1954 #endif /* CONFIG_NO_TKIP */
1955 		else if (os_strncmp(encr, "CCMP", 4) == 0)
1956 			cred.encr_type = WPS_ENCR_AES;
1957 		else
1958 			return -1;
1959 	} else
1960 		cred.encr_type = WPS_ENCR_NONE;
1961 
1962 	if (key) {
1963 		len = os_strlen(key);
1964 		if ((len & 1) || len > 2 * sizeof(cred.key) ||
1965 		    hexstr2bin(key, cred.key, len / 2))
1966 			return -1;
1967 		cred.key_len = len / 2;
1968 	}
1969 
1970 	if (!hapd->wps) {
1971 		wpa_printf(MSG_ERROR, "WPS: WPS config does not exist");
1972 		return -1;
1973 	}
1974 
1975 	return wps_registrar_config_ap(hapd->wps->registrar, &cred);
1976 }
1977 
1978 
1979 #ifdef CONFIG_WPS_NFC
1980 
1981 struct wps_nfc_password_token_data {
1982 	const u8 *oob_dev_pw;
1983 	size_t oob_dev_pw_len;
1984 	int added;
1985 };
1986 
1987 
wps_add_nfc_password_token(struct hostapd_data * hapd,void * ctx)1988 static int wps_add_nfc_password_token(struct hostapd_data *hapd, void *ctx)
1989 {
1990 	struct wps_nfc_password_token_data *data = ctx;
1991 	int ret;
1992 
1993 	if (hapd->wps == NULL)
1994 		return 0;
1995 	ret = wps_registrar_add_nfc_password_token(hapd->wps->registrar,
1996 						   data->oob_dev_pw,
1997 						   data->oob_dev_pw_len);
1998 	if (ret == 0)
1999 		data->added++;
2000 	return ret;
2001 }
2002 
2003 
hostapd_wps_add_nfc_password_token(struct hostapd_data * hapd,struct wps_parse_attr * attr)2004 static int hostapd_wps_add_nfc_password_token(struct hostapd_data *hapd,
2005 					      struct wps_parse_attr *attr)
2006 {
2007 	struct wps_nfc_password_token_data data;
2008 
2009 	data.oob_dev_pw = attr->oob_dev_password;
2010 	data.oob_dev_pw_len = attr->oob_dev_password_len;
2011 	data.added = 0;
2012 	if (hostapd_wps_for_each(hapd, wps_add_nfc_password_token, &data) < 0)
2013 		return -1;
2014 	return data.added ? 0 : -1;
2015 }
2016 
2017 
hostapd_wps_nfc_tag_process(struct hostapd_data * hapd,const struct wpabuf * wps)2018 static int hostapd_wps_nfc_tag_process(struct hostapd_data *hapd,
2019 				       const struct wpabuf *wps)
2020 {
2021 	struct wps_parse_attr attr;
2022 
2023 	wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
2024 
2025 	if (wps_parse_msg(wps, &attr)) {
2026 		wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
2027 		return -1;
2028 	}
2029 
2030 	if (attr.oob_dev_password)
2031 		return hostapd_wps_add_nfc_password_token(hapd, &attr);
2032 
2033 	wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
2034 	return -1;
2035 }
2036 
2037 
hostapd_wps_nfc_tag_read(struct hostapd_data * hapd,const struct wpabuf * data)2038 int hostapd_wps_nfc_tag_read(struct hostapd_data *hapd,
2039 			     const struct wpabuf *data)
2040 {
2041 	const struct wpabuf *wps = data;
2042 	struct wpabuf *tmp = NULL;
2043 	int ret;
2044 
2045 	if (wpabuf_len(data) < 4)
2046 		return -1;
2047 
2048 	if (*wpabuf_head_u8(data) != 0x10) {
2049 		/* Assume this contains full NDEF record */
2050 		tmp = ndef_parse_wifi(data);
2051 		if (tmp == NULL) {
2052 			wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
2053 			return -1;
2054 		}
2055 		wps = tmp;
2056 	}
2057 
2058 	ret = hostapd_wps_nfc_tag_process(hapd, wps);
2059 	wpabuf_free(tmp);
2060 	return ret;
2061 }
2062 
2063 
hostapd_wps_nfc_config_token(struct hostapd_data * hapd,int ndef)2064 struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd,
2065 					     int ndef)
2066 {
2067 	struct wpabuf *ret;
2068 
2069 	if (hapd->wps == NULL)
2070 		return NULL;
2071 
2072 	ret = wps_get_oob_cred(hapd->wps, hostapd_wps_rf_band_cb(hapd),
2073 			       hapd->iconf->channel);
2074 	if (ndef && ret) {
2075 		struct wpabuf *tmp;
2076 		tmp = ndef_build_wifi(ret);
2077 		wpabuf_free(ret);
2078 		if (tmp == NULL)
2079 			return NULL;
2080 		ret = tmp;
2081 	}
2082 
2083 	return ret;
2084 }
2085 
2086 
hostapd_wps_nfc_hs_cr(struct hostapd_data * hapd,int ndef)2087 struct wpabuf * hostapd_wps_nfc_hs_cr(struct hostapd_data *hapd, int ndef)
2088 {
2089 	struct wpabuf *ret;
2090 
2091 	if (hapd->wps == NULL)
2092 		return NULL;
2093 
2094 	if (hapd->conf->wps_nfc_dh_pubkey == NULL) {
2095 		struct wps_context *wps = hapd->wps;
2096 		if (wps_nfc_gen_dh(&hapd->conf->wps_nfc_dh_pubkey,
2097 				   &hapd->conf->wps_nfc_dh_privkey) < 0)
2098 			return NULL;
2099 		hostapd_wps_nfc_clear(wps);
2100 		wps->ap_nfc_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
2101 		wps->ap_nfc_dh_pubkey =
2102 			wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
2103 		wps->ap_nfc_dh_privkey =
2104 			wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
2105 		if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey) {
2106 			hostapd_wps_nfc_clear(wps);
2107 			return NULL;
2108 		}
2109 	}
2110 
2111 	ret = wps_build_nfc_handover_sel(hapd->wps,
2112 					 hapd->conf->wps_nfc_dh_pubkey,
2113 					 hapd->own_addr, hapd->iface->freq);
2114 
2115 	if (ndef && ret) {
2116 		struct wpabuf *tmp;
2117 		tmp = ndef_build_wifi(ret);
2118 		wpabuf_free(ret);
2119 		if (tmp == NULL)
2120 			return NULL;
2121 		ret = tmp;
2122 	}
2123 
2124 	return ret;
2125 }
2126 
2127 
hostapd_wps_nfc_report_handover(struct hostapd_data * hapd,const struct wpabuf * req,const struct wpabuf * sel)2128 int hostapd_wps_nfc_report_handover(struct hostapd_data *hapd,
2129 				    const struct wpabuf *req,
2130 				    const struct wpabuf *sel)
2131 {
2132 	struct wpabuf *wps;
2133 	int ret = -1;
2134 	u16 wsc_len;
2135 	const u8 *pos;
2136 	struct wpabuf msg;
2137 	struct wps_parse_attr attr;
2138 	u16 dev_pw_id;
2139 
2140 	/*
2141 	 * Enrollee/station is always initiator of the NFC connection handover,
2142 	 * so use the request message here to find Enrollee public key hash.
2143 	 */
2144 	wps = ndef_parse_wifi(req);
2145 	if (wps == NULL)
2146 		return -1;
2147 	wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
2148 		   "payload from NFC connection handover");
2149 	wpa_hexdump_buf(MSG_DEBUG, "WPS: NFC payload", wps);
2150 	if (wpabuf_len(wps) < 2) {
2151 		wpa_printf(MSG_DEBUG, "WPS: Too short Wi-Fi Handover Request "
2152 			   "Message");
2153 		goto out;
2154 	}
2155 	pos = wpabuf_head(wps);
2156 	wsc_len = WPA_GET_BE16(pos);
2157 	if (wsc_len > wpabuf_len(wps) - 2) {
2158 		wpa_printf(MSG_DEBUG, "WPS: Invalid WSC attribute length (%u) "
2159 			   "in rt Wi-Fi Handover Request Message", wsc_len);
2160 		goto out;
2161 	}
2162 	pos += 2;
2163 
2164 	wpa_hexdump(MSG_DEBUG,
2165 		    "WPS: WSC attributes in Wi-Fi Handover Request Message",
2166 		    pos, wsc_len);
2167 	if (wsc_len < wpabuf_len(wps) - 2) {
2168 		wpa_hexdump(MSG_DEBUG,
2169 			    "WPS: Ignore extra data after WSC attributes",
2170 			    pos + wsc_len, wpabuf_len(wps) - 2 - wsc_len);
2171 	}
2172 
2173 	wpabuf_set(&msg, pos, wsc_len);
2174 	ret = wps_parse_msg(&msg, &attr);
2175 	if (ret < 0) {
2176 		wpa_printf(MSG_DEBUG, "WPS: Could not parse WSC attributes in "
2177 			   "Wi-Fi Handover Request Message");
2178 		goto out;
2179 	}
2180 
2181 	if (attr.oob_dev_password == NULL ||
2182 	    attr.oob_dev_password_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
2183 		wpa_printf(MSG_DEBUG, "WPS: No Out-of-Band Device Password "
2184 			   "included in Wi-Fi Handover Request Message");
2185 		ret = -1;
2186 		goto out;
2187 	}
2188 
2189 	if (attr.uuid_e == NULL) {
2190 		wpa_printf(MSG_DEBUG, "WPS: No UUID-E included in Wi-Fi "
2191 			   "Handover Request Message");
2192 		ret = -1;
2193 		goto out;
2194 	}
2195 
2196 	wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", attr.uuid_e, WPS_UUID_LEN);
2197 
2198 	wpa_hexdump(MSG_DEBUG, "WPS: Out-of-Band Device Password",
2199 		    attr.oob_dev_password, attr.oob_dev_password_len);
2200 	dev_pw_id = WPA_GET_BE16(attr.oob_dev_password +
2201 				 WPS_OOB_PUBKEY_HASH_LEN);
2202 	if (dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER) {
2203 		wpa_printf(MSG_DEBUG, "WPS: Unexpected OOB Device Password ID "
2204 			   "%u in Wi-Fi Handover Request Message", dev_pw_id);
2205 		ret = -1;
2206 		goto out;
2207 	}
2208 	wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Public Key hash",
2209 		    attr.oob_dev_password, WPS_OOB_PUBKEY_HASH_LEN);
2210 
2211 	ret = wps_registrar_add_nfc_pw_token(hapd->wps->registrar,
2212 					     attr.oob_dev_password,
2213 					     DEV_PW_NFC_CONNECTION_HANDOVER,
2214 					     NULL, 0, 1);
2215 
2216 out:
2217 	wpabuf_free(wps);
2218 	return ret;
2219 }
2220 
2221 
hostapd_wps_nfc_token_gen(struct hostapd_data * hapd,int ndef)2222 struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef)
2223 {
2224 	if (hapd->conf->wps_nfc_pw_from_config) {
2225 		return wps_nfc_token_build(ndef,
2226 					   hapd->conf->wps_nfc_dev_pw_id,
2227 					   hapd->conf->wps_nfc_dh_pubkey,
2228 					   hapd->conf->wps_nfc_dev_pw);
2229 	}
2230 
2231 	return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id,
2232 				 &hapd->conf->wps_nfc_dh_pubkey,
2233 				 &hapd->conf->wps_nfc_dh_privkey,
2234 				 &hapd->conf->wps_nfc_dev_pw);
2235 }
2236 
2237 
hostapd_wps_nfc_token_enable(struct hostapd_data * hapd)2238 int hostapd_wps_nfc_token_enable(struct hostapd_data *hapd)
2239 {
2240 	struct wps_context *wps = hapd->wps;
2241 	struct wpabuf *pw;
2242 
2243 	if (wps == NULL)
2244 		return -1;
2245 
2246 	if (!hapd->conf->wps_nfc_dh_pubkey ||
2247 	    !hapd->conf->wps_nfc_dh_privkey ||
2248 	    !hapd->conf->wps_nfc_dev_pw ||
2249 	    !hapd->conf->wps_nfc_dev_pw_id)
2250 		return -1;
2251 
2252 	hostapd_wps_nfc_clear(wps);
2253 	wpa_printf(MSG_DEBUG,
2254 		   "WPS: Enable NFC Tag (Dev Pw Id %u) for AP interface %s (context %p)",
2255 		   hapd->conf->wps_nfc_dev_pw_id, hapd->conf->iface, wps);
2256 	wps->ap_nfc_dev_pw_id = hapd->conf->wps_nfc_dev_pw_id;
2257 	wps->ap_nfc_dh_pubkey = wpabuf_dup(hapd->conf->wps_nfc_dh_pubkey);
2258 	wps->ap_nfc_dh_privkey = wpabuf_dup(hapd->conf->wps_nfc_dh_privkey);
2259 	pw = hapd->conf->wps_nfc_dev_pw;
2260 	wps->ap_nfc_dev_pw = wpabuf_alloc(
2261 		wpabuf_len(pw) * 2 + 1);
2262 	if (wps->ap_nfc_dev_pw) {
2263 		wpa_snprintf_hex_uppercase(
2264 			(char *) wpabuf_put(wps->ap_nfc_dev_pw,
2265 					    wpabuf_len(pw) * 2),
2266 			wpabuf_len(pw) * 2 + 1,
2267 			wpabuf_head(pw), wpabuf_len(pw));
2268 	}
2269 
2270 	if (!wps->ap_nfc_dh_pubkey || !wps->ap_nfc_dh_privkey ||
2271 	    !wps->ap_nfc_dev_pw) {
2272 		hostapd_wps_nfc_clear(wps);
2273 		return -1;
2274 	}
2275 
2276 	return 0;
2277 }
2278 
2279 
hostapd_wps_nfc_token_disable(struct hostapd_data * hapd)2280 void hostapd_wps_nfc_token_disable(struct hostapd_data *hapd)
2281 {
2282 	wpa_printf(MSG_DEBUG, "WPS: Disable NFC token for AP interface %s",
2283 		   hapd->conf->iface);
2284 	hostapd_wps_nfc_clear(hapd->wps);
2285 }
2286 
2287 #endif /* CONFIG_WPS_NFC */
2288