xref: /netbsd-src/external/bsd/wpa/dist/src/ap/wps_hostapd.c (revision 1b9578b8c2c1f848eeb16dabbfd7d1f0d9fdefbd)
1 /*
2  * hostapd / WPS integration
3  * Copyright (c) 2008-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14 
15 #include "utils/includes.h"
16 
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "utils/uuid.h"
20 #include "crypto/dh_groups.h"
21 #include "common/wpa_ctrl.h"
22 #include "common/ieee802_11_defs.h"
23 #include "common/ieee802_11_common.h"
24 #include "eapol_auth/eapol_auth_sm.h"
25 #include "eapol_auth/eapol_auth_sm_i.h"
26 #include "wps/wps.h"
27 #include "wps/wps_defs.h"
28 #include "wps/wps_dev_attr.h"
29 #include "hostapd.h"
30 #include "ap_config.h"
31 #include "beacon.h"
32 #include "sta_info.h"
33 #include "wps_hostapd.h"
34 
35 
36 #ifdef CONFIG_WPS_UPNP
37 #include "wps/wps_upnp.h"
38 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
39 				 struct wps_context *wps);
40 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
41 #endif /* CONFIG_WPS_UPNP */
42 
43 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
44 				    const u8 *ie, size_t ie_len);
45 
46 
47 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
48 				  size_t psk_len)
49 {
50 	struct hostapd_data *hapd = ctx;
51 	struct hostapd_wpa_psk *p;
52 	struct hostapd_ssid *ssid = &hapd->conf->ssid;
53 
54 	wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
55 		   MACSTR, MAC2STR(mac_addr));
56 	wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
57 
58 	if (psk_len != PMK_LEN) {
59 		wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
60 			   (unsigned long) psk_len);
61 		return -1;
62 	}
63 
64 	/* Add the new PSK to runtime PSK list */
65 	p = os_zalloc(sizeof(*p));
66 	if (p == NULL)
67 		return -1;
68 	os_memcpy(p->addr, mac_addr, ETH_ALEN);
69 	os_memcpy(p->psk, psk, PMK_LEN);
70 
71 	p->next = ssid->wpa_psk;
72 	ssid->wpa_psk = p;
73 
74 	if (ssid->wpa_psk_file) {
75 		FILE *f;
76 		char hex[PMK_LEN * 2 + 1];
77 		/* Add the new PSK to PSK list file */
78 		f = fopen(ssid->wpa_psk_file, "a");
79 		if (f == NULL) {
80 			wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
81 				   "'%s'", ssid->wpa_psk_file);
82 			return -1;
83 		}
84 
85 		wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
86 		fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
87 		fclose(f);
88 	}
89 
90 	return 0;
91 }
92 
93 
94 static int hostapd_wps_set_ie_cb(void *ctx, struct wpabuf *beacon_ie,
95 				 struct wpabuf *probe_resp_ie)
96 {
97 	struct hostapd_data *hapd = ctx;
98 	wpabuf_free(hapd->wps_beacon_ie);
99 	hapd->wps_beacon_ie = beacon_ie;
100 	wpabuf_free(hapd->wps_probe_resp_ie);
101 	hapd->wps_probe_resp_ie = probe_resp_ie;
102 	ieee802_11_set_beacon(hapd);
103 	return hapd->drv.set_ap_wps_ie(hapd);
104 }
105 
106 
107 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
108 				      const struct wps_device_data *dev)
109 {
110 	struct hostapd_data *hapd = ctx;
111 	char uuid[40], txt[400];
112 	int len;
113 	char devtype[WPS_DEV_TYPE_BUFSIZE];
114 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
115 		return;
116 	wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
117 	len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
118 			  "%s " MACSTR " [%s|%s|%s|%s|%s|%s]",
119 			  uuid, MAC2STR(dev->mac_addr), dev->device_name,
120 			  dev->manufacturer, dev->model_name,
121 			  dev->model_number, dev->serial_number,
122 			  wps_dev_type_bin2str(dev->pri_dev_type, devtype,
123 					       sizeof(devtype)));
124 	if (len > 0 && len < (int) sizeof(txt))
125 		wpa_msg(hapd->msg_ctx, MSG_INFO, "%s", txt);
126 
127 	if (hapd->conf->wps_pin_requests) {
128 		FILE *f;
129 		struct os_time t;
130 		f = fopen(hapd->conf->wps_pin_requests, "a");
131 		if (f == NULL)
132 			return;
133 		os_get_time(&t);
134 		fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
135 			"\t%s\n",
136 			t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
137 			dev->manufacturer, dev->model_name, dev->model_number,
138 			dev->serial_number,
139 			wps_dev_type_bin2str(dev->pri_dev_type, devtype,
140 					     sizeof(devtype)));
141 		fclose(f);
142 	}
143 }
144 
145 
146 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
147 				       const u8 *uuid_e)
148 {
149 	struct hostapd_data *hapd = ctx;
150 	char uuid[40];
151 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
152 		return;
153 	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
154 		MAC2STR(mac_addr), uuid);
155 	if (hapd->wps_reg_success_cb)
156 		hapd->wps_reg_success_cb(hapd->wps_reg_success_cb_ctx,
157 					 mac_addr, uuid_e);
158 }
159 
160 
161 static void hostapd_wps_enrollee_seen_cb(void *ctx, const u8 *addr,
162 					 const u8 *uuid_e,
163 					 const u8 *pri_dev_type,
164 					 u16 config_methods,
165 					 u16 dev_password_id, u8 request_type,
166 					 const char *dev_name)
167 {
168 	struct hostapd_data *hapd = ctx;
169 	char uuid[40];
170 	char devtype[WPS_DEV_TYPE_BUFSIZE];
171 	if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
172 		return;
173 	if (dev_name == NULL)
174 		dev_name = "";
175 	wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, WPS_EVENT_ENROLLEE_SEEN MACSTR
176 		     " %s %s 0x%x %u %u [%s]",
177 		     MAC2STR(addr), uuid,
178 		     wps_dev_type_bin2str(pri_dev_type, devtype,
179 					  sizeof(devtype)),
180 		     config_methods, dev_password_id, request_type, dev_name);
181 }
182 
183 
184 static int str_starts(const char *str, const char *start)
185 {
186 	return os_strncmp(str, start, os_strlen(start)) == 0;
187 }
188 
189 
190 static void wps_reload_config(void *eloop_data, void *user_ctx)
191 {
192 	struct hostapd_iface *iface = eloop_data;
193 
194 	wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
195 	if (iface->reload_config(iface) < 0) {
196 		wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
197 			   "configuration");
198 	}
199 }
200 
201 
202 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
203 {
204 	struct hostapd_data *hapd = ctx;
205 	FILE *oconf, *nconf;
206 	size_t len, i;
207 	char *tmp_fname;
208 	char buf[1024];
209 	int multi_bss;
210 	int wpa;
211 
212 	wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
213 			cred->cred_attr, cred->cred_attr_len);
214 
215 	wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
216 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
217 	wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
218 		   cred->auth_type);
219 	wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
220 	wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
221 	wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
222 			cred->key, cred->key_len);
223 	wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
224 		   MAC2STR(cred->mac_addr));
225 
226 	if ((hapd->conf->wps_cred_processing == 1 ||
227 	     hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
228 		size_t blen = cred->cred_attr_len * 2 + 1;
229 		char *_buf = os_malloc(blen);
230 		if (_buf) {
231 			wpa_snprintf_hex(_buf, blen,
232 					 cred->cred_attr, cred->cred_attr_len);
233 			wpa_msg(hapd->msg_ctx, MSG_INFO, "%s%s",
234 				WPS_EVENT_NEW_AP_SETTINGS, _buf);
235 			os_free(_buf);
236 		}
237 	} else
238 		wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
239 
240 	if (hapd->conf->wps_cred_processing == 1)
241 		return 0;
242 
243 	os_memcpy(hapd->wps->ssid, cred->ssid, cred->ssid_len);
244 	hapd->wps->ssid_len = cred->ssid_len;
245 	hapd->wps->encr_types = cred->encr_type;
246 	hapd->wps->auth_types = cred->auth_type;
247 	if (cred->key_len == 0) {
248 		os_free(hapd->wps->network_key);
249 		hapd->wps->network_key = NULL;
250 		hapd->wps->network_key_len = 0;
251 	} else {
252 		if (hapd->wps->network_key == NULL ||
253 		    hapd->wps->network_key_len < cred->key_len) {
254 			hapd->wps->network_key_len = 0;
255 			os_free(hapd->wps->network_key);
256 			hapd->wps->network_key = os_malloc(cred->key_len);
257 			if (hapd->wps->network_key == NULL)
258 				return -1;
259 		}
260 		hapd->wps->network_key_len = cred->key_len;
261 		os_memcpy(hapd->wps->network_key, cred->key, cred->key_len);
262 	}
263 	hapd->wps->wps_state = WPS_STATE_CONFIGURED;
264 
265 	len = os_strlen(hapd->iface->config_fname) + 5;
266 	tmp_fname = os_malloc(len);
267 	if (tmp_fname == NULL)
268 		return -1;
269 	os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
270 
271 	oconf = fopen(hapd->iface->config_fname, "r");
272 	if (oconf == NULL) {
273 		wpa_printf(MSG_WARNING, "WPS: Could not open current "
274 			   "configuration file");
275 		os_free(tmp_fname);
276 		return -1;
277 	}
278 
279 	nconf = fopen(tmp_fname, "w");
280 	if (nconf == NULL) {
281 		wpa_printf(MSG_WARNING, "WPS: Could not write updated "
282 			   "configuration file");
283 		os_free(tmp_fname);
284 		fclose(oconf);
285 		return -1;
286 	}
287 
288 	fprintf(nconf, "# WPS configuration - START\n");
289 
290 	fprintf(nconf, "wps_state=2\n");
291 
292 	fprintf(nconf, "ssid=");
293 	for (i = 0; i < cred->ssid_len; i++)
294 		fputc(cred->ssid[i], nconf);
295 	fprintf(nconf, "\n");
296 
297 	if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
298 	    (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
299 		wpa = 3;
300 	else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
301 		wpa = 2;
302 	else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
303 		wpa = 1;
304 	else
305 		wpa = 0;
306 
307 	if (wpa) {
308 		char *prefix;
309 		fprintf(nconf, "wpa=%d\n", wpa);
310 
311 		fprintf(nconf, "wpa_key_mgmt=");
312 		prefix = "";
313 		if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
314 			fprintf(nconf, "WPA-EAP");
315 			prefix = " ";
316 		}
317 		if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
318 			fprintf(nconf, "%sWPA-PSK", prefix);
319 		fprintf(nconf, "\n");
320 
321 		fprintf(nconf, "wpa_pairwise=");
322 		prefix = "";
323 		if (cred->encr_type & WPS_ENCR_AES) {
324 			fprintf(nconf, "CCMP");
325 			prefix = " ";
326 		}
327 		if (cred->encr_type & WPS_ENCR_TKIP) {
328 			fprintf(nconf, "%sTKIP", prefix);
329 		}
330 		fprintf(nconf, "\n");
331 
332 		if (cred->key_len >= 8 && cred->key_len < 64) {
333 			fprintf(nconf, "wpa_passphrase=");
334 			for (i = 0; i < cred->key_len; i++)
335 				fputc(cred->key[i], nconf);
336 			fprintf(nconf, "\n");
337 		} else if (cred->key_len == 64) {
338 			fprintf(nconf, "wpa_psk=");
339 			for (i = 0; i < cred->key_len; i++)
340 				fputc(cred->key[i], nconf);
341 			fprintf(nconf, "\n");
342 		} else {
343 			wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
344 				   "for WPA/WPA2",
345 				   (unsigned long) cred->key_len);
346 		}
347 
348 		fprintf(nconf, "auth_algs=1\n");
349 	} else {
350 		if ((cred->auth_type & WPS_AUTH_OPEN) &&
351 		    (cred->auth_type & WPS_AUTH_SHARED))
352 			fprintf(nconf, "auth_algs=3\n");
353 		else if (cred->auth_type & WPS_AUTH_SHARED)
354 			fprintf(nconf, "auth_algs=2\n");
355 		else
356 			fprintf(nconf, "auth_algs=1\n");
357 
358 		if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
359 			int key_idx = cred->key_idx;
360 			if (key_idx)
361 				key_idx--;
362 			fprintf(nconf, "wep_default_key=%d\n", key_idx);
363 			fprintf(nconf, "wep_key%d=", key_idx);
364 			if (cred->key_len == 10 || cred->key_len == 26) {
365 				/* WEP key as a hex string */
366 				for (i = 0; i < cred->key_len; i++)
367 					fputc(cred->key[i], nconf);
368 			} else {
369 				/* Raw WEP key; convert to hex */
370 				for (i = 0; i < cred->key_len; i++)
371 					fprintf(nconf, "%02x", cred->key[i]);
372 			}
373 			fprintf(nconf, "\n");
374 		}
375 	}
376 
377 	fprintf(nconf, "# WPS configuration - END\n");
378 
379 	multi_bss = 0;
380 	while (fgets(buf, sizeof(buf), oconf)) {
381 		if (os_strncmp(buf, "bss=", 4) == 0)
382 			multi_bss = 1;
383 		if (!multi_bss &&
384 		    (str_starts(buf, "ssid=") ||
385 		     str_starts(buf, "auth_algs=") ||
386 		     str_starts(buf, "wps_state=") ||
387 		     str_starts(buf, "wpa=") ||
388 		     str_starts(buf, "wpa_psk=") ||
389 		     str_starts(buf, "wpa_pairwise=") ||
390 		     str_starts(buf, "rsn_pairwise=") ||
391 		     str_starts(buf, "wpa_key_mgmt=") ||
392 		     str_starts(buf, "wpa_passphrase="))) {
393 			fprintf(nconf, "#WPS# %s", buf);
394 		} else
395 			fprintf(nconf, "%s", buf);
396 	}
397 
398 	fclose(nconf);
399 	fclose(oconf);
400 
401 	if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
402 		wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
403 			   "configuration file: %s", strerror(errno));
404 		os_free(tmp_fname);
405 		return -1;
406 	}
407 
408 	os_free(tmp_fname);
409 
410 	/* Schedule configuration reload after short period of time to allow
411 	 * EAP-WSC to be finished.
412 	 */
413 	eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
414 			       NULL);
415 
416 	/* TODO: dualband AP may need to update multiple configuration files */
417 
418 	wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
419 
420 	return 0;
421 }
422 
423 
424 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
425 				  struct wps_event_pwd_auth_fail *data)
426 {
427 	FILE *f;
428 
429 	if (!data->enrollee)
430 		return;
431 
432 	/*
433 	 * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
434 	 * if this happens multiple times.
435 	 */
436 	hapd->ap_pin_failures++;
437 	if (hapd->ap_pin_failures < 4)
438 		return;
439 
440 	wpa_msg(hapd->msg_ctx, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
441 	hapd->wps->ap_setup_locked = 1;
442 
443 	wps_registrar_update_ie(hapd->wps->registrar);
444 
445 	if (hapd->conf->wps_cred_processing == 1)
446 		return;
447 
448 	f = fopen(hapd->iface->config_fname, "a");
449 	if (f == NULL) {
450 		wpa_printf(MSG_WARNING, "WPS: Could not append to the current "
451 			   "configuration file");
452 		return;
453 	}
454 
455 	fprintf(f, "# WPS AP Setup Locked based on possible attack\n");
456 	fprintf(f, "ap_setup_locked=1\n");
457 	fclose(f);
458 
459 	/* TODO: dualband AP may need to update multiple configuration files */
460 
461 	wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
462 }
463 
464 
465 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
466 				 union wps_event_data *data)
467 {
468 	struct hostapd_data *hapd = ctx;
469 
470 	if (event == WPS_EV_PWD_AUTH_FAIL)
471 		hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
472 }
473 
474 
475 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
476 {
477 	wpabuf_free(hapd->wps_beacon_ie);
478 	hapd->wps_beacon_ie = NULL;
479 
480 	wpabuf_free(hapd->wps_probe_resp_ie);
481 	hapd->wps_probe_resp_ie = NULL;
482 
483 	hapd->drv.set_ap_wps_ie(hapd);
484 }
485 
486 
487 int hostapd_init_wps(struct hostapd_data *hapd,
488 		     struct hostapd_bss_config *conf)
489 {
490 	struct wps_context *wps;
491 	struct wps_registrar_config cfg;
492 
493 	if (conf->wps_state == 0) {
494 		hostapd_wps_clear_ies(hapd);
495 		return 0;
496 	}
497 
498 	wps = os_zalloc(sizeof(*wps));
499 	if (wps == NULL)
500 		return -1;
501 
502 	wps->cred_cb = hostapd_wps_cred_cb;
503 	wps->event_cb = hostapd_wps_event_cb;
504 	wps->cb_ctx = hapd;
505 
506 	os_memset(&cfg, 0, sizeof(cfg));
507 	wps->wps_state = hapd->conf->wps_state;
508 	wps->ap_setup_locked = hapd->conf->ap_setup_locked;
509 	if (is_nil_uuid(hapd->conf->uuid)) {
510 		uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
511 		wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
512 			    wps->uuid, UUID_LEN);
513 	} else
514 		os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
515 	wps->ssid_len = hapd->conf->ssid.ssid_len;
516 	os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
517 	wps->ap = 1;
518 	os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
519 	wps->dev.device_name = hapd->conf->device_name ?
520 		os_strdup(hapd->conf->device_name) : NULL;
521 	wps->dev.manufacturer = hapd->conf->manufacturer ?
522 		os_strdup(hapd->conf->manufacturer) : NULL;
523 	wps->dev.model_name = hapd->conf->model_name ?
524 		os_strdup(hapd->conf->model_name) : NULL;
525 	wps->dev.model_number = hapd->conf->model_number ?
526 		os_strdup(hapd->conf->model_number) : NULL;
527 	wps->dev.serial_number = hapd->conf->serial_number ?
528 		os_strdup(hapd->conf->serial_number) : NULL;
529 	wps->config_methods =
530 		wps_config_methods_str2bin(hapd->conf->config_methods);
531 	if (hapd->conf->device_type &&
532 	    wps_dev_type_str2bin(hapd->conf->device_type,
533 				 wps->dev.pri_dev_type) < 0) {
534 		wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
535 		os_free(wps);
536 		return -1;
537 	}
538 	wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
539 	wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
540 		WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
541 
542 	if (conf->wpa & WPA_PROTO_RSN) {
543 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
544 			wps->auth_types |= WPS_AUTH_WPA2PSK;
545 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
546 			wps->auth_types |= WPS_AUTH_WPA2;
547 
548 		if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
549 			wps->encr_types |= WPS_ENCR_AES;
550 		if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
551 			wps->encr_types |= WPS_ENCR_TKIP;
552 	}
553 
554 	if (conf->wpa & WPA_PROTO_WPA) {
555 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
556 			wps->auth_types |= WPS_AUTH_WPAPSK;
557 		if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
558 			wps->auth_types |= WPS_AUTH_WPA;
559 
560 		if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
561 			wps->encr_types |= WPS_ENCR_AES;
562 		if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
563 			wps->encr_types |= WPS_ENCR_TKIP;
564 	}
565 
566 	if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
567 		wps->encr_types |= WPS_ENCR_NONE;
568 		wps->auth_types |= WPS_AUTH_OPEN;
569 	} else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
570 		wps->encr_types |= WPS_ENCR_WEP;
571 		if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
572 			wps->auth_types |= WPS_AUTH_OPEN;
573 		if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
574 			wps->auth_types |= WPS_AUTH_SHARED;
575 	} else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
576 		wps->auth_types |= WPS_AUTH_OPEN;
577 		if (conf->default_wep_key_len)
578 			wps->encr_types |= WPS_ENCR_WEP;
579 		else
580 			wps->encr_types |= WPS_ENCR_NONE;
581 	}
582 
583 	if (conf->ssid.wpa_psk_file) {
584 		/* Use per-device PSKs */
585 	} else if (conf->ssid.wpa_passphrase) {
586 		wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
587 		wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
588 	} else if (conf->ssid.wpa_psk) {
589 		wps->network_key = os_malloc(2 * PMK_LEN + 1);
590 		if (wps->network_key == NULL) {
591 			os_free(wps);
592 			return -1;
593 		}
594 		wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
595 				 conf->ssid.wpa_psk->psk, PMK_LEN);
596 		wps->network_key_len = 2 * PMK_LEN;
597 	} else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
598 		wps->network_key = os_malloc(conf->ssid.wep.len[0]);
599 		if (wps->network_key == NULL) {
600 			os_free(wps);
601 			return -1;
602 		}
603 		os_memcpy(wps->network_key, conf->ssid.wep.key[0],
604 			  conf->ssid.wep.len[0]);
605 		wps->network_key_len = conf->ssid.wep.len[0];
606 	}
607 
608 	if (conf->ssid.wpa_psk) {
609 		os_memcpy(wps->psk, conf->ssid.wpa_psk->psk, PMK_LEN);
610 		wps->psk_set = 1;
611 	}
612 
613 	if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
614 		/* Override parameters to enable security by default */
615 		wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
616 		wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
617 	}
618 
619 	wps->ap_settings = conf->ap_settings;
620 	wps->ap_settings_len = conf->ap_settings_len;
621 
622 	cfg.new_psk_cb = hostapd_wps_new_psk_cb;
623 	cfg.set_ie_cb = hostapd_wps_set_ie_cb;
624 	cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
625 	cfg.reg_success_cb = hostapd_wps_reg_success_cb;
626 	cfg.enrollee_seen_cb = hostapd_wps_enrollee_seen_cb;
627 	cfg.cb_ctx = hapd;
628 	cfg.skip_cred_build = conf->skip_cred_build;
629 	cfg.extra_cred = conf->extra_cred;
630 	cfg.extra_cred_len = conf->extra_cred_len;
631 	cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
632 		conf->skip_cred_build;
633 	if (conf->ssid.security_policy == SECURITY_STATIC_WEP)
634 		cfg.static_wep_only = 1;
635 
636 	wps->registrar = wps_registrar_init(wps, &cfg);
637 	if (wps->registrar == NULL) {
638 		printf("Failed to initialize WPS Registrar\n");
639 		os_free(wps->network_key);
640 		os_free(wps);
641 		return -1;
642 	}
643 
644 #ifdef CONFIG_WPS_UPNP
645 	wps->friendly_name = hapd->conf->friendly_name;
646 	wps->manufacturer_url = hapd->conf->manufacturer_url;
647 	wps->model_description = hapd->conf->model_description;
648 	wps->model_url = hapd->conf->model_url;
649 	wps->upc = hapd->conf->upc;
650 
651 	if (hostapd_wps_upnp_init(hapd, wps) < 0) {
652 		wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
653 		wps_registrar_deinit(wps->registrar);
654 		os_free(wps->network_key);
655 		os_free(wps);
656 		return -1;
657 	}
658 #endif /* CONFIG_WPS_UPNP */
659 
660 	hostapd_register_probereq_cb(hapd, hostapd_wps_probe_req_rx, hapd);
661 
662 	hapd->wps = wps;
663 
664 	return 0;
665 }
666 
667 
668 void hostapd_deinit_wps(struct hostapd_data *hapd)
669 {
670 	if (hapd->wps == NULL)
671 		return;
672 #ifdef CONFIG_WPS_UPNP
673 	hostapd_wps_upnp_deinit(hapd);
674 #endif /* CONFIG_WPS_UPNP */
675 	wps_registrar_deinit(hapd->wps->registrar);
676 	os_free(hapd->wps->network_key);
677 	wps_device_data_free(&hapd->wps->dev);
678 	wpabuf_free(hapd->wps->dh_pubkey);
679 	wpabuf_free(hapd->wps->dh_privkey);
680 	wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
681 	wpabuf_free(hapd->wps->oob_conf.dev_password);
682 	wps_free_pending_msgs(hapd->wps->upnp_msgs);
683 	os_free(hapd->wps);
684 	hapd->wps = NULL;
685 	hostapd_wps_clear_ies(hapd);
686 }
687 
688 
689 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
690 			const char *pin, int timeout)
691 {
692 	u8 u[UUID_LEN];
693 	int any = 0;
694 
695 	if (hapd->wps == NULL)
696 		return -1;
697 	if (os_strcmp(uuid, "any") == 0)
698 		any = 1;
699 	else if (uuid_str2bin(uuid, u))
700 		return -1;
701 	return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
702 				     (const u8 *) pin, os_strlen(pin),
703 				     timeout);
704 }
705 
706 
707 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
708 {
709 	if (hapd->wps == NULL)
710 		return -1;
711 	return wps_registrar_button_pushed(hapd->wps->registrar);
712 }
713 
714 
715 #ifdef CONFIG_WPS_OOB
716 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
717 			  char *path, char *method, char *name)
718 {
719 	struct wps_context *wps = hapd->wps;
720 	struct oob_device_data *oob_dev;
721 
722 	oob_dev = wps_get_oob_device(device_type);
723 	if (oob_dev == NULL)
724 		return -1;
725 	oob_dev->device_path = path;
726 	oob_dev->device_name = name;
727 	wps->oob_conf.oob_method = wps_get_oob_method(method);
728 
729 	if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
730 		/*
731 		 * Use pre-configured DH keys in order to be able to write the
732 		 * key hash into the OOB file.
733 		 */
734 		wpabuf_free(wps->dh_pubkey);
735 		wpabuf_free(wps->dh_privkey);
736 		wps->dh_privkey = NULL;
737 		wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
738 					 &wps->dh_privkey);
739 		wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
740 		if (wps->dh_pubkey == NULL) {
741 			wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
742 				   "Diffie-Hellman handshake");
743 			return -1;
744 		}
745 	}
746 
747 	if (wps_process_oob(wps, oob_dev, 1) < 0)
748 		goto error;
749 
750 	if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
751 	     wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
752 	    hostapd_wps_add_pin(hapd, "any",
753 				wpabuf_head(wps->oob_conf.dev_password), 0) <
754 	    0)
755 		goto error;
756 
757 	return 0;
758 
759 error:
760 	wpabuf_free(wps->dh_pubkey);
761 	wps->dh_pubkey = NULL;
762 	wpabuf_free(wps->dh_privkey);
763 	wps->dh_privkey = NULL;
764 	return -1;
765 }
766 #endif /* CONFIG_WPS_OOB */
767 
768 
769 static int hostapd_wps_probe_req_rx(void *ctx, const u8 *addr,
770 				    const u8 *ie, size_t ie_len)
771 {
772 	struct hostapd_data *hapd = ctx;
773 	struct wpabuf *wps_ie;
774 
775 	if (hapd->wps == NULL)
776 		return 0;
777 
778 	wps_ie = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
779 	if (wps_ie == NULL)
780 		return 0;
781 
782 	if (wpabuf_len(wps_ie) > 0) {
783 		wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
784 #ifdef CONFIG_WPS_UPNP
785 		/* FIX: what exactly should be included in the WLANEvent?
786 		 * WPS attributes? Full ProbeReq frame? */
787 		upnp_wps_device_send_wlan_event(hapd->wps_upnp, addr,
788 						UPNP_WPS_WLANEVENT_TYPE_PROBE,
789 						wps_ie);
790 #endif /* CONFIG_WPS_UPNP */
791 	}
792 
793 	wpabuf_free(wps_ie);
794 
795 	return 0;
796 }
797 
798 
799 #ifdef CONFIG_WPS_UPNP
800 
801 static int hostapd_rx_req_put_wlan_response(
802 	void *priv, enum upnp_wps_wlanevent_type ev_type,
803 	const u8 *mac_addr, const struct wpabuf *msg,
804 	enum wps_msg_type msg_type)
805 {
806 	struct hostapd_data *hapd = priv;
807 	struct sta_info *sta;
808 	struct upnp_pending_message *p;
809 
810 	wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
811 		   MACSTR, ev_type, MAC2STR(mac_addr));
812 	wpa_hexdump(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
813 		    wpabuf_head(msg), wpabuf_len(msg));
814 	if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
815 		wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
816 			   "PutWLANResponse WLANEventType %d", ev_type);
817 		return -1;
818 	}
819 
820 	/*
821 	 * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
822 	 * server implementation for delivery to the peer.
823 	 */
824 
825 	sta = ap_get_sta(hapd, mac_addr);
826 	if (!sta) {
827 		/*
828 		 * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
829 		 * Pick STA that is in an ongoing WPS registration without
830 		 * checking the MAC address.
831 		 */
832 		wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
833 			   "on NewWLANEventMAC; try wildcard match");
834 		for (sta = hapd->sta_list; sta; sta = sta->next) {
835 			if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
836 				break;
837 		}
838 	}
839 
840 	if (!sta) {
841 		wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
842 		return 0;
843 	}
844 
845 	p = os_zalloc(sizeof(*p));
846 	if (p == NULL)
847 		return -1;
848 	os_memcpy(p->addr, sta->addr, ETH_ALEN);
849 	p->msg = wpabuf_dup(msg);
850 	p->type = msg_type;
851 	p->next = hapd->wps->upnp_msgs;
852 	hapd->wps->upnp_msgs = p;
853 
854 	return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
855 }
856 
857 
858 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
859 				 struct wps_context *wps)
860 {
861 	struct upnp_wps_device_ctx *ctx;
862 
863 	if (!hapd->conf->upnp_iface)
864 		return 0;
865 	ctx = os_zalloc(sizeof(*ctx));
866 	if (ctx == NULL)
867 		return -1;
868 
869 	ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
870 	if (hapd->conf->ap_pin)
871 		ctx->ap_pin = os_strdup(hapd->conf->ap_pin);
872 
873 	hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
874 	if (hapd->wps_upnp == NULL) {
875 		os_free(ctx);
876 		return -1;
877 	}
878 	wps->wps_upnp = hapd->wps_upnp;
879 
880 	if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
881 		upnp_wps_device_deinit(hapd->wps_upnp);
882 		hapd->wps_upnp = NULL;
883 		return -1;
884 	}
885 
886 	return 0;
887 }
888 
889 
890 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
891 {
892 	upnp_wps_device_deinit(hapd->wps_upnp);
893 }
894 
895 #endif /* CONFIG_WPS_UPNP */
896 
897 
898 int hostapd_wps_get_mib_sta(struct hostapd_data *hapd, const u8 *addr,
899 			    char *buf, size_t buflen)
900 {
901 	if (hapd->wps == NULL)
902 		return 0;
903 	return wps_registrar_get_info(hapd->wps->registrar, addr, buf, buflen);
904 }
905