xref: /freebsd/contrib/wpa/src/wps/wps_registrar.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /*
2  * Wi-Fi Protected Setup - Registrar
3  * Copyright (c) 2008-2012, 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/base64.h"
13 #include "utils/eloop.h"
14 #include "utils/uuid.h"
15 #include "utils/list.h"
16 #include "crypto/crypto.h"
17 #include "crypto/sha256.h"
18 #include "crypto/random.h"
19 #include "common/ieee802_11_defs.h"
20 #include "wps_i.h"
21 #include "wps_dev_attr.h"
22 #include "wps_upnp.h"
23 #include "wps_upnp_i.h"
24 
25 #ifndef CONFIG_WPS_STRICT
26 #define WPS_WORKAROUNDS
27 #endif /* CONFIG_WPS_STRICT */
28 
29 #ifdef CONFIG_WPS_NFC
30 
31 struct wps_nfc_pw_token {
32 	struct dl_list list;
33 	u8 pubkey_hash[WPS_OOB_PUBKEY_HASH_LEN];
34 	u16 pw_id;
35 	u8 dev_pw[WPS_OOB_DEVICE_PASSWORD_LEN];
36 	size_t dev_pw_len;
37 };
38 
39 
40 static void wps_remove_nfc_pw_token(struct wps_nfc_pw_token *token)
41 {
42 	dl_list_del(&token->list);
43 	os_free(token);
44 }
45 
46 
47 static void wps_free_nfc_pw_tokens(struct dl_list *tokens, u16 pw_id)
48 {
49 	struct wps_nfc_pw_token *token, *prev;
50 	dl_list_for_each_safe(token, prev, tokens, struct wps_nfc_pw_token,
51 			      list) {
52 		if (pw_id == 0 || pw_id == token->pw_id)
53 			wps_remove_nfc_pw_token(token);
54 	}
55 }
56 
57 
58 static struct wps_nfc_pw_token * wps_get_nfc_pw_token(struct dl_list *tokens,
59 						      u16 pw_id)
60 {
61 	struct wps_nfc_pw_token *token;
62 	dl_list_for_each(token, tokens, struct wps_nfc_pw_token, list) {
63 		if (pw_id == token->pw_id)
64 			return token;
65 	}
66 	return NULL;
67 }
68 
69 #else /* CONFIG_WPS_NFC */
70 
71 #define wps_free_nfc_pw_tokens(t, p) do { } while (0)
72 
73 #endif /* CONFIG_WPS_NFC */
74 
75 
76 struct wps_uuid_pin {
77 	struct dl_list list;
78 	u8 uuid[WPS_UUID_LEN];
79 	int wildcard_uuid;
80 	u8 *pin;
81 	size_t pin_len;
82 #define PIN_LOCKED BIT(0)
83 #define PIN_EXPIRES BIT(1)
84 	int flags;
85 	struct os_time expiration;
86 	u8 enrollee_addr[ETH_ALEN];
87 };
88 
89 
90 static void wps_free_pin(struct wps_uuid_pin *pin)
91 {
92 	os_free(pin->pin);
93 	os_free(pin);
94 }
95 
96 
97 static void wps_remove_pin(struct wps_uuid_pin *pin)
98 {
99 	dl_list_del(&pin->list);
100 	wps_free_pin(pin);
101 }
102 
103 
104 static void wps_free_pins(struct dl_list *pins)
105 {
106 	struct wps_uuid_pin *pin, *prev;
107 	dl_list_for_each_safe(pin, prev, pins, struct wps_uuid_pin, list)
108 		wps_remove_pin(pin);
109 }
110 
111 
112 struct wps_pbc_session {
113 	struct wps_pbc_session *next;
114 	u8 addr[ETH_ALEN];
115 	u8 uuid_e[WPS_UUID_LEN];
116 	struct os_time timestamp;
117 };
118 
119 
120 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
121 {
122 	struct wps_pbc_session *prev;
123 
124 	while (pbc) {
125 		prev = pbc;
126 		pbc = pbc->next;
127 		os_free(prev);
128 	}
129 }
130 
131 
132 struct wps_registrar_device {
133 	struct wps_registrar_device *next;
134 	struct wps_device_data dev;
135 	u8 uuid[WPS_UUID_LEN];
136 };
137 
138 
139 struct wps_registrar {
140 	struct wps_context *wps;
141 
142 	int pbc;
143 	int selected_registrar;
144 
145 	int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
146 			  size_t psk_len);
147 	int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
148 			 struct wpabuf *probe_resp_ie);
149 	void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
150 			      const struct wps_device_data *dev);
151 	void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
152 			       const u8 *uuid_e, const u8 *dev_pw,
153 			       size_t dev_pw_len);
154 	void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
155 			       u16 sel_reg_config_methods);
156 	void (*enrollee_seen_cb)(void *ctx, const u8 *addr, const u8 *uuid_e,
157 				 const u8 *pri_dev_type, u16 config_methods,
158 				 u16 dev_password_id, u8 request_type,
159 				 const char *dev_name);
160 	void *cb_ctx;
161 
162 	struct dl_list pins;
163 	struct dl_list nfc_pw_tokens;
164 	struct wps_pbc_session *pbc_sessions;
165 
166 	int skip_cred_build;
167 	struct wpabuf *extra_cred;
168 	int disable_auto_conf;
169 	int sel_reg_union;
170 	int sel_reg_dev_password_id_override;
171 	int sel_reg_config_methods_override;
172 	int static_wep_only;
173 	int dualband;
174 
175 	struct wps_registrar_device *devices;
176 
177 	int force_pbc_overlap;
178 
179 	u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
180 	u8 authorized_macs_union[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
181 
182 	u8 p2p_dev_addr[ETH_ALEN];
183 
184 	u8 pbc_ignore_uuid[WPS_UUID_LEN];
185 	struct os_time pbc_ignore_start;
186 };
187 
188 
189 static int wps_set_ie(struct wps_registrar *reg);
190 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
191 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
192 					       void *timeout_ctx);
193 static void wps_registrar_remove_pin(struct wps_registrar *reg,
194 				     struct wps_uuid_pin *pin);
195 
196 
197 static void wps_registrar_add_authorized_mac(struct wps_registrar *reg,
198 					     const u8 *addr)
199 {
200 	int i;
201 	wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC " MACSTR,
202 		   MAC2STR(addr));
203 	for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
204 		if (os_memcmp(reg->authorized_macs[i], addr, ETH_ALEN) == 0) {
205 			wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was "
206 				   "already in the list");
207 			return; /* already in list */
208 		}
209 	for (i = WPS_MAX_AUTHORIZED_MACS - 1; i > 0; i--)
210 		os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i - 1],
211 			  ETH_ALEN);
212 	os_memcpy(reg->authorized_macs[0], addr, ETH_ALEN);
213 	wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
214 		    (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
215 }
216 
217 
218 static void wps_registrar_remove_authorized_mac(struct wps_registrar *reg,
219 						const u8 *addr)
220 {
221 	int i;
222 	wpa_printf(MSG_DEBUG, "WPS: Remove authorized MAC " MACSTR,
223 		   MAC2STR(addr));
224 	for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++) {
225 		if (os_memcmp(reg->authorized_macs, addr, ETH_ALEN) == 0)
226 			break;
227 	}
228 	if (i == WPS_MAX_AUTHORIZED_MACS) {
229 		wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was not in the "
230 			   "list");
231 		return; /* not in the list */
232 	}
233 	for (; i + 1 < WPS_MAX_AUTHORIZED_MACS; i++)
234 		os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i + 1],
235 			  ETH_ALEN);
236 	os_memset(reg->authorized_macs[WPS_MAX_AUTHORIZED_MACS - 1], 0,
237 		  ETH_ALEN);
238 	wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
239 		    (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
240 }
241 
242 
243 static void wps_free_devices(struct wps_registrar_device *dev)
244 {
245 	struct wps_registrar_device *prev;
246 
247 	while (dev) {
248 		prev = dev;
249 		dev = dev->next;
250 		wps_device_data_free(&prev->dev);
251 		os_free(prev);
252 	}
253 }
254 
255 
256 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
257 						    const u8 *addr)
258 {
259 	struct wps_registrar_device *dev;
260 
261 	for (dev = reg->devices; dev; dev = dev->next) {
262 		if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
263 			return dev;
264 	}
265 	return NULL;
266 }
267 
268 
269 static void wps_device_clone_data(struct wps_device_data *dst,
270 				  struct wps_device_data *src)
271 {
272 	os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
273 	os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
274 
275 #define WPS_STRDUP(n) \
276 	os_free(dst->n); \
277 	dst->n = src->n ? os_strdup(src->n) : NULL
278 
279 	WPS_STRDUP(device_name);
280 	WPS_STRDUP(manufacturer);
281 	WPS_STRDUP(model_name);
282 	WPS_STRDUP(model_number);
283 	WPS_STRDUP(serial_number);
284 #undef WPS_STRDUP
285 }
286 
287 
288 int wps_device_store(struct wps_registrar *reg,
289 		     struct wps_device_data *dev, const u8 *uuid)
290 {
291 	struct wps_registrar_device *d;
292 
293 	d = wps_device_get(reg, dev->mac_addr);
294 	if (d == NULL) {
295 		d = os_zalloc(sizeof(*d));
296 		if (d == NULL)
297 			return -1;
298 		d->next = reg->devices;
299 		reg->devices = d;
300 	}
301 
302 	wps_device_clone_data(&d->dev, dev);
303 	os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
304 
305 	return 0;
306 }
307 
308 
309 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
310 					  const u8 *addr, const u8 *uuid_e)
311 {
312 	struct wps_pbc_session *pbc, *prev = NULL;
313 	struct os_time now;
314 
315 	os_get_time(&now);
316 
317 	pbc = reg->pbc_sessions;
318 	while (pbc) {
319 		if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
320 		    os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
321 			if (prev)
322 				prev->next = pbc->next;
323 			else
324 				reg->pbc_sessions = pbc->next;
325 			break;
326 		}
327 		prev = pbc;
328 		pbc = pbc->next;
329 	}
330 
331 	if (!pbc) {
332 		pbc = os_zalloc(sizeof(*pbc));
333 		if (pbc == NULL)
334 			return;
335 		os_memcpy(pbc->addr, addr, ETH_ALEN);
336 		if (uuid_e)
337 			os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
338 	}
339 
340 	pbc->next = reg->pbc_sessions;
341 	reg->pbc_sessions = pbc;
342 	pbc->timestamp = now;
343 
344 	/* remove entries that have timed out */
345 	prev = pbc;
346 	pbc = pbc->next;
347 
348 	while (pbc) {
349 		if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
350 			prev->next = NULL;
351 			wps_free_pbc_sessions(pbc);
352 			break;
353 		}
354 		prev = pbc;
355 		pbc = pbc->next;
356 	}
357 }
358 
359 
360 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
361 					     const u8 *uuid_e,
362 					     const u8 *p2p_dev_addr)
363 {
364 	struct wps_pbc_session *pbc, *prev = NULL, *tmp;
365 
366 	pbc = reg->pbc_sessions;
367 	while (pbc) {
368 		if (os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0 ||
369 		    (p2p_dev_addr && !is_zero_ether_addr(reg->p2p_dev_addr) &&
370 		     os_memcmp(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
371 		     0)) {
372 			if (prev)
373 				prev->next = pbc->next;
374 			else
375 				reg->pbc_sessions = pbc->next;
376 			tmp = pbc;
377 			pbc = pbc->next;
378 			wpa_printf(MSG_DEBUG, "WPS: Removing PBC session for "
379 				   "addr=" MACSTR, MAC2STR(tmp->addr));
380 			wpa_hexdump(MSG_DEBUG, "WPS: Removed UUID-E",
381 				    tmp->uuid_e, WPS_UUID_LEN);
382 			os_free(tmp);
383 			continue;
384 		}
385 		prev = pbc;
386 		pbc = pbc->next;
387 	}
388 }
389 
390 
391 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
392 			      const u8 *addr, const u8 *uuid_e)
393 {
394 	int count = 0;
395 	struct wps_pbc_session *pbc;
396 	struct wps_pbc_session *first = NULL;
397 	struct os_time now;
398 
399 	os_get_time(&now);
400 
401 	wpa_printf(MSG_DEBUG, "WPS: Checking active PBC sessions for overlap");
402 
403 	if (uuid_e) {
404 		wpa_printf(MSG_DEBUG, "WPS: Add one for the requested UUID");
405 		wpa_hexdump(MSG_DEBUG, "WPS: Requested UUID",
406 			    uuid_e, WPS_UUID_LEN);
407 		count++;
408 	}
409 
410 	for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
411 		wpa_printf(MSG_DEBUG, "WPS: Consider PBC session with " MACSTR,
412 			   MAC2STR(pbc->addr));
413 		wpa_hexdump(MSG_DEBUG, "WPS: UUID-E",
414 			    pbc->uuid_e, WPS_UUID_LEN);
415 		if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
416 			wpa_printf(MSG_DEBUG, "WPS: PBC walk time has "
417 				   "expired");
418 			break;
419 		}
420 		if (first &&
421 		    os_memcmp(pbc->uuid_e, first->uuid_e, WPS_UUID_LEN) == 0) {
422 			wpa_printf(MSG_DEBUG, "WPS: Same Enrollee");
423 			continue; /* same Enrollee */
424 		}
425 		if (uuid_e == NULL ||
426 		    os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN)) {
427 			wpa_printf(MSG_DEBUG, "WPS: New Enrollee");
428 			count++;
429 		}
430 		if (first == NULL)
431 			first = pbc;
432 	}
433 
434 	wpa_printf(MSG_DEBUG, "WPS: %u active PBC session(s) found", count);
435 
436 	return count > 1 ? 1 : 0;
437 }
438 
439 
440 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
441 {
442 	wpa_printf(MSG_DEBUG, "WPS:  * Wi-Fi Protected Setup State (%d)",
443 		   wps->wps_state);
444 	wpabuf_put_be16(msg, ATTR_WPS_STATE);
445 	wpabuf_put_be16(msg, 1);
446 	wpabuf_put_u8(msg, wps->wps_state);
447 	return 0;
448 }
449 
450 
451 #ifdef CONFIG_WPS_UPNP
452 static void wps_registrar_free_pending_m2(struct wps_context *wps)
453 {
454 	struct upnp_pending_message *p, *p2, *prev = NULL;
455 	p = wps->upnp_msgs;
456 	while (p) {
457 		if (p->type == WPS_M2 || p->type == WPS_M2D) {
458 			if (prev == NULL)
459 				wps->upnp_msgs = p->next;
460 			else
461 				prev->next = p->next;
462 			wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
463 			p2 = p;
464 			p = p->next;
465 			wpabuf_free(p2->msg);
466 			os_free(p2);
467 			continue;
468 		}
469 		prev = p;
470 		p = p->next;
471 	}
472 }
473 #endif /* CONFIG_WPS_UPNP */
474 
475 
476 static int wps_build_ap_setup_locked(struct wps_context *wps,
477 				     struct wpabuf *msg)
478 {
479 	if (wps->ap_setup_locked && wps->ap_setup_locked != 2) {
480 		wpa_printf(MSG_DEBUG, "WPS:  * AP Setup Locked");
481 		wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
482 		wpabuf_put_be16(msg, 1);
483 		wpabuf_put_u8(msg, 1);
484 	}
485 	return 0;
486 }
487 
488 
489 static int wps_build_selected_registrar(struct wps_registrar *reg,
490 					struct wpabuf *msg)
491 {
492 	if (!reg->sel_reg_union)
493 		return 0;
494 	wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar");
495 	wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
496 	wpabuf_put_be16(msg, 1);
497 	wpabuf_put_u8(msg, 1);
498 	return 0;
499 }
500 
501 
502 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
503 					     struct wpabuf *msg)
504 {
505 	u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
506 	if (!reg->sel_reg_union)
507 		return 0;
508 	if (reg->sel_reg_dev_password_id_override >= 0)
509 		id = reg->sel_reg_dev_password_id_override;
510 	wpa_printf(MSG_DEBUG, "WPS:  * Device Password ID (%d)", id);
511 	wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
512 	wpabuf_put_be16(msg, 2);
513 	wpabuf_put_be16(msg, id);
514 	return 0;
515 }
516 
517 
518 static int wps_build_sel_pbc_reg_uuid_e(struct wps_registrar *reg,
519 					struct wpabuf *msg)
520 {
521 	u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
522 	if (!reg->sel_reg_union)
523 		return 0;
524 	if (reg->sel_reg_dev_password_id_override >= 0)
525 		id = reg->sel_reg_dev_password_id_override;
526 	if (id != DEV_PW_PUSHBUTTON || !reg->dualband)
527 		return 0;
528 	return wps_build_uuid_e(msg, reg->wps->uuid);
529 }
530 
531 
532 static void wps_set_pushbutton(u16 *methods, u16 conf_methods)
533 {
534 	*methods |= WPS_CONFIG_PUSHBUTTON;
535 #ifdef CONFIG_WPS2
536 	if ((conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON) ==
537 	    WPS_CONFIG_VIRT_PUSHBUTTON)
538 		*methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
539 	if ((conf_methods & WPS_CONFIG_PHY_PUSHBUTTON) ==
540 	    WPS_CONFIG_PHY_PUSHBUTTON)
541 		*methods |= WPS_CONFIG_PHY_PUSHBUTTON;
542 	if ((*methods & WPS_CONFIG_VIRT_PUSHBUTTON) !=
543 	    WPS_CONFIG_VIRT_PUSHBUTTON &&
544 	    (*methods & WPS_CONFIG_PHY_PUSHBUTTON) !=
545 	    WPS_CONFIG_PHY_PUSHBUTTON) {
546 		/*
547 		 * Required to include virtual/physical flag, but we were not
548 		 * configured with push button type, so have to default to one
549 		 * of them.
550 		 */
551 		*methods |= WPS_CONFIG_PHY_PUSHBUTTON;
552 	}
553 #endif /* CONFIG_WPS2 */
554 }
555 
556 
557 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
558 					    struct wpabuf *msg)
559 {
560 	u16 methods;
561 	if (!reg->sel_reg_union)
562 		return 0;
563 	methods = reg->wps->config_methods;
564 	methods &= ~WPS_CONFIG_PUSHBUTTON;
565 #ifdef CONFIG_WPS2
566 	methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
567 		     WPS_CONFIG_PHY_PUSHBUTTON);
568 #endif /* CONFIG_WPS2 */
569 	if (reg->pbc)
570 		wps_set_pushbutton(&methods, reg->wps->config_methods);
571 	if (reg->sel_reg_config_methods_override >= 0)
572 		methods = reg->sel_reg_config_methods_override;
573 	wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar Config Methods (%x)",
574 		   methods);
575 	wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
576 	wpabuf_put_be16(msg, 2);
577 	wpabuf_put_be16(msg, methods);
578 	return 0;
579 }
580 
581 
582 static int wps_build_probe_config_methods(struct wps_registrar *reg,
583 					  struct wpabuf *msg)
584 {
585 	u16 methods;
586 	/*
587 	 * These are the methods that the AP supports as an Enrollee for adding
588 	 * external Registrars.
589 	 */
590 	methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
591 #ifdef CONFIG_WPS2
592 	methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
593 		     WPS_CONFIG_PHY_PUSHBUTTON);
594 #endif /* CONFIG_WPS2 */
595 	wpa_printf(MSG_DEBUG, "WPS:  * Config Methods (%x)", methods);
596 	wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
597 	wpabuf_put_be16(msg, 2);
598 	wpabuf_put_be16(msg, methods);
599 	return 0;
600 }
601 
602 
603 static int wps_build_config_methods_r(struct wps_registrar *reg,
604 				      struct wpabuf *msg)
605 {
606 	return wps_build_config_methods(msg, reg->wps->config_methods);
607 }
608 
609 
610 const u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count)
611 {
612 	*count = 0;
613 
614 #ifdef CONFIG_WPS2
615 	while (*count < WPS_MAX_AUTHORIZED_MACS) {
616 		if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
617 			break;
618 		(*count)++;
619 	}
620 #endif /* CONFIG_WPS2 */
621 
622 	return (const u8 *) reg->authorized_macs_union;
623 }
624 
625 
626 /**
627  * wps_registrar_init - Initialize WPS Registrar data
628  * @wps: Pointer to longterm WPS context
629  * @cfg: Registrar configuration
630  * Returns: Pointer to allocated Registrar data or %NULL on failure
631  *
632  * This function is used to initialize WPS Registrar functionality. It can be
633  * used for a single Registrar run (e.g., when run in a supplicant) or multiple
634  * runs (e.g., when run as an internal Registrar in an AP). Caller is
635  * responsible for freeing the returned data with wps_registrar_deinit() when
636  * Registrar functionality is not needed anymore.
637  */
638 struct wps_registrar *
639 wps_registrar_init(struct wps_context *wps,
640 		   const struct wps_registrar_config *cfg)
641 {
642 	struct wps_registrar *reg = os_zalloc(sizeof(*reg));
643 	if (reg == NULL)
644 		return NULL;
645 
646 	dl_list_init(&reg->pins);
647 	dl_list_init(&reg->nfc_pw_tokens);
648 	reg->wps = wps;
649 	reg->new_psk_cb = cfg->new_psk_cb;
650 	reg->set_ie_cb = cfg->set_ie_cb;
651 	reg->pin_needed_cb = cfg->pin_needed_cb;
652 	reg->reg_success_cb = cfg->reg_success_cb;
653 	reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
654 	reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
655 	reg->cb_ctx = cfg->cb_ctx;
656 	reg->skip_cred_build = cfg->skip_cred_build;
657 	if (cfg->extra_cred) {
658 		reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
659 						    cfg->extra_cred_len);
660 		if (reg->extra_cred == NULL) {
661 			os_free(reg);
662 			return NULL;
663 		}
664 	}
665 	reg->disable_auto_conf = cfg->disable_auto_conf;
666 	reg->sel_reg_dev_password_id_override = -1;
667 	reg->sel_reg_config_methods_override = -1;
668 	reg->static_wep_only = cfg->static_wep_only;
669 	reg->dualband = cfg->dualband;
670 
671 	if (wps_set_ie(reg)) {
672 		wps_registrar_deinit(reg);
673 		return NULL;
674 	}
675 
676 	return reg;
677 }
678 
679 
680 /**
681  * wps_registrar_deinit - Deinitialize WPS Registrar data
682  * @reg: Registrar data from wps_registrar_init()
683  */
684 void wps_registrar_deinit(struct wps_registrar *reg)
685 {
686 	if (reg == NULL)
687 		return;
688 	eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
689 	eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
690 	wps_free_pins(&reg->pins);
691 	wps_free_nfc_pw_tokens(&reg->nfc_pw_tokens, 0);
692 	wps_free_pbc_sessions(reg->pbc_sessions);
693 	wpabuf_free(reg->extra_cred);
694 	wps_free_devices(reg->devices);
695 	os_free(reg);
696 }
697 
698 
699 static void wps_registrar_invalidate_unused(struct wps_registrar *reg)
700 {
701 	struct wps_uuid_pin *pin;
702 
703 	dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
704 		if (pin->wildcard_uuid == 1 && !(pin->flags & PIN_LOCKED)) {
705 			wpa_printf(MSG_DEBUG, "WPS: Invalidate previously "
706 				   "configured wildcard PIN");
707 			wps_registrar_remove_pin(reg, pin);
708 			break;
709 		}
710 	}
711 }
712 
713 
714 /**
715  * wps_registrar_add_pin - Configure a new PIN for Registrar
716  * @reg: Registrar data from wps_registrar_init()
717  * @addr: Enrollee MAC address or %NULL if not known
718  * @uuid: UUID-E or %NULL for wildcard (any UUID)
719  * @pin: PIN (Device Password)
720  * @pin_len: Length of pin in octets
721  * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
722  * Returns: 0 on success, -1 on failure
723  */
724 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
725 			  const u8 *uuid, const u8 *pin, size_t pin_len,
726 			  int timeout)
727 {
728 	struct wps_uuid_pin *p;
729 
730 	p = os_zalloc(sizeof(*p));
731 	if (p == NULL)
732 		return -1;
733 	if (addr)
734 		os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
735 	if (uuid == NULL)
736 		p->wildcard_uuid = 1;
737 	else
738 		os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
739 	p->pin = os_malloc(pin_len);
740 	if (p->pin == NULL) {
741 		os_free(p);
742 		return -1;
743 	}
744 	os_memcpy(p->pin, pin, pin_len);
745 	p->pin_len = pin_len;
746 
747 	if (timeout) {
748 		p->flags |= PIN_EXPIRES;
749 		os_get_time(&p->expiration);
750 		p->expiration.sec += timeout;
751 	}
752 
753 	if (p->wildcard_uuid)
754 		wps_registrar_invalidate_unused(reg);
755 
756 	dl_list_add(&reg->pins, &p->list);
757 
758 	wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
759 		   timeout);
760 	wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
761 	wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
762 	reg->selected_registrar = 1;
763 	reg->pbc = 0;
764 	if (addr)
765 		wps_registrar_add_authorized_mac(reg, addr);
766 	else
767 		wps_registrar_add_authorized_mac(
768 			reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
769 	wps_registrar_selected_registrar_changed(reg);
770 	eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
771 	eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
772 			       wps_registrar_set_selected_timeout,
773 			       reg, NULL);
774 
775 	return 0;
776 }
777 
778 
779 static void wps_registrar_remove_pin(struct wps_registrar *reg,
780 				     struct wps_uuid_pin *pin)
781 {
782 	u8 *addr;
783 	u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
784 
785 	if (is_zero_ether_addr(pin->enrollee_addr))
786 		addr = bcast;
787 	else
788 		addr = pin->enrollee_addr;
789 	wps_registrar_remove_authorized_mac(reg, addr);
790 	wps_remove_pin(pin);
791 	wps_registrar_selected_registrar_changed(reg);
792 }
793 
794 
795 static void wps_registrar_expire_pins(struct wps_registrar *reg)
796 {
797 	struct wps_uuid_pin *pin, *prev;
798 	struct os_time now;
799 
800 	os_get_time(&now);
801 	dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
802 	{
803 		if ((pin->flags & PIN_EXPIRES) &&
804 		    os_time_before(&pin->expiration, &now)) {
805 			wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
806 				    pin->uuid, WPS_UUID_LEN);
807 			wps_registrar_remove_pin(reg, pin);
808 		}
809 	}
810 }
811 
812 
813 /**
814  * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
815  * @reg: Registrar data from wps_registrar_init()
816  * @dev_pw: PIN to search for or %NULL to match any
817  * @dev_pw_len: Length of dev_pw in octets
818  * Returns: 0 on success, -1 if not wildcard PIN is enabled
819  */
820 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg,
821 						 const u8 *dev_pw,
822 						 size_t dev_pw_len)
823 {
824 	struct wps_uuid_pin *pin, *prev;
825 
826 	dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
827 	{
828 		if (dev_pw && pin->pin &&
829 		    (dev_pw_len != pin->pin_len ||
830 		     os_memcmp(dev_pw, pin->pin, dev_pw_len) != 0))
831 			continue; /* different PIN */
832 		if (pin->wildcard_uuid) {
833 			wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
834 				    pin->uuid, WPS_UUID_LEN);
835 			wps_registrar_remove_pin(reg, pin);
836 			return 0;
837 		}
838 	}
839 
840 	return -1;
841 }
842 
843 
844 /**
845  * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
846  * @reg: Registrar data from wps_registrar_init()
847  * @uuid: UUID-E
848  * Returns: 0 on success, -1 on failure (e.g., PIN not found)
849  */
850 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
851 {
852 	struct wps_uuid_pin *pin, *prev;
853 
854 	dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
855 	{
856 		if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
857 			wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
858 				    pin->uuid, WPS_UUID_LEN);
859 			wps_registrar_remove_pin(reg, pin);
860 			return 0;
861 		}
862 	}
863 
864 	return -1;
865 }
866 
867 
868 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
869 					const u8 *uuid, size_t *pin_len)
870 {
871 	struct wps_uuid_pin *pin, *found = NULL;
872 
873 	wps_registrar_expire_pins(reg);
874 
875 	dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
876 		if (!pin->wildcard_uuid &&
877 		    os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
878 			found = pin;
879 			break;
880 		}
881 	}
882 
883 	if (!found) {
884 		/* Check for wildcard UUIDs since none of the UUID-specific
885 		 * PINs matched */
886 		dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
887 			if (pin->wildcard_uuid == 1 ||
888 			    pin->wildcard_uuid == 2) {
889 				wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
890 					   "PIN. Assigned it for this UUID-E");
891 				pin->wildcard_uuid++;
892 				os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
893 				found = pin;
894 				break;
895 			}
896 		}
897 	}
898 
899 	if (!found)
900 		return NULL;
901 
902 	/*
903 	 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
904 	 * that could otherwise avoid PIN invalidations.
905 	 */
906 	if (found->flags & PIN_LOCKED) {
907 		wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
908 			   "allow concurrent re-use");
909 		return NULL;
910 	}
911 	*pin_len = found->pin_len;
912 	found->flags |= PIN_LOCKED;
913 	return found->pin;
914 }
915 
916 
917 /**
918  * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
919  * @reg: Registrar data from wps_registrar_init()
920  * @uuid: UUID-E
921  * Returns: 0 on success, -1 on failure
922  *
923  * PINs are locked to enforce only one concurrent use. This function unlocks a
924  * PIN to allow it to be used again. If the specified PIN was configured using
925  * a wildcard UUID, it will be removed instead of allowing multiple uses.
926  */
927 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
928 {
929 	struct wps_uuid_pin *pin;
930 
931 	dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
932 		if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
933 			if (pin->wildcard_uuid == 3) {
934 				wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
935 					   "wildcard PIN");
936 				return wps_registrar_invalidate_pin(reg, uuid);
937 			}
938 			pin->flags &= ~PIN_LOCKED;
939 			return 0;
940 		}
941 	}
942 
943 	return -1;
944 }
945 
946 
947 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
948 {
949 	reg->selected_registrar = 0;
950 	reg->pbc = 0;
951 	os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
952 	wps_registrar_remove_authorized_mac(reg,
953 					    (u8 *) "\xff\xff\xff\xff\xff\xff");
954 	wps_registrar_selected_registrar_changed(reg);
955 }
956 
957 
958 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
959 {
960 	struct wps_registrar *reg = eloop_ctx;
961 
962 	wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
963 	wps_pbc_timeout_event(reg->wps);
964 	wps_registrar_stop_pbc(reg);
965 }
966 
967 
968 /**
969  * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
970  * @reg: Registrar data from wps_registrar_init()
971  * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
972  *	indicates no such filtering
973  * Returns: 0 on success, -1 on failure, -2 on session overlap
974  *
975  * This function is called on an AP when a push button is pushed to activate
976  * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
977  * or when a PBC registration is completed. If more than one Enrollee in active
978  * PBC mode has been detected during the monitor time (previous 2 minutes), the
979  * PBC mode is not activated and -2 is returned to indicate session overlap.
980  * This is skipped if a specific Enrollee is selected.
981  */
982 int wps_registrar_button_pushed(struct wps_registrar *reg,
983 				const u8 *p2p_dev_addr)
984 {
985 	if (p2p_dev_addr == NULL &&
986 	    wps_registrar_pbc_overlap(reg, NULL, NULL)) {
987 		wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
988 			   "mode");
989 		wps_pbc_overlap_event(reg->wps);
990 		return -2;
991 	}
992 	wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
993 	reg->force_pbc_overlap = 0;
994 	reg->selected_registrar = 1;
995 	reg->pbc = 1;
996 	if (p2p_dev_addr)
997 		os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
998 	else
999 		os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
1000 	wps_registrar_add_authorized_mac(reg,
1001 					 (u8 *) "\xff\xff\xff\xff\xff\xff");
1002 	wps_registrar_selected_registrar_changed(reg);
1003 
1004 	eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1005 	eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1006 	eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
1007 			       reg, NULL);
1008 	return 0;
1009 }
1010 
1011 
1012 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
1013 {
1014 	wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
1015 	eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1016 	wps_registrar_stop_pbc(reg);
1017 }
1018 
1019 
1020 static void wps_registrar_pin_completed(struct wps_registrar *reg)
1021 {
1022 	wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
1023 	eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1024 	reg->selected_registrar = 0;
1025 	wps_registrar_selected_registrar_changed(reg);
1026 }
1027 
1028 
1029 void wps_registrar_complete(struct wps_registrar *registrar, const u8 *uuid_e,
1030 			    const u8 *dev_pw, size_t dev_pw_len)
1031 {
1032 	if (registrar->pbc) {
1033 		wps_registrar_remove_pbc_session(registrar,
1034 						 uuid_e, NULL);
1035 		wps_registrar_pbc_completed(registrar);
1036 		os_get_time(&registrar->pbc_ignore_start);
1037 		os_memcpy(registrar->pbc_ignore_uuid, uuid_e, WPS_UUID_LEN);
1038 	} else {
1039 		wps_registrar_pin_completed(registrar);
1040 	}
1041 
1042 	if (dev_pw &&
1043 	    wps_registrar_invalidate_wildcard_pin(registrar, dev_pw,
1044 						  dev_pw_len) == 0) {
1045 		wpa_hexdump_key(MSG_DEBUG, "WPS: Invalidated wildcard PIN",
1046 				dev_pw, dev_pw_len);
1047 	}
1048 }
1049 
1050 
1051 int wps_registrar_wps_cancel(struct wps_registrar *reg)
1052 {
1053 	if (reg->pbc) {
1054 		wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
1055 		wps_registrar_pbc_timeout(reg, NULL);
1056 		eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1057 		return 1;
1058 	} else if (reg->selected_registrar) {
1059 		/* PIN Method */
1060 		wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
1061 		wps_registrar_pin_completed(reg);
1062 		wps_registrar_invalidate_wildcard_pin(reg, NULL, 0);
1063 		return 1;
1064 	}
1065 	return 0;
1066 }
1067 
1068 
1069 /**
1070  * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
1071  * @reg: Registrar data from wps_registrar_init()
1072  * @addr: MAC address of the Probe Request sender
1073  * @wps_data: WPS IE contents
1074  *
1075  * This function is called on an AP when a Probe Request with WPS IE is
1076  * received. This is used to track PBC mode use and to detect possible overlap
1077  * situation with other WPS APs.
1078  */
1079 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
1080 				const struct wpabuf *wps_data,
1081 				int p2p_wildcard)
1082 {
1083 	struct wps_parse_attr attr;
1084 	int skip_add = 0;
1085 
1086 	wpa_hexdump_buf(MSG_MSGDUMP,
1087 			"WPS: Probe Request with WPS data received",
1088 			wps_data);
1089 
1090 	if (wps_parse_msg(wps_data, &attr) < 0)
1091 		return;
1092 
1093 	if (attr.config_methods == NULL) {
1094 		wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
1095 			   "Probe Request");
1096 		return;
1097 	}
1098 
1099 	if (attr.dev_password_id == NULL) {
1100 		wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
1101 			   "in Probe Request");
1102 		return;
1103 	}
1104 
1105 	if (reg->enrollee_seen_cb && attr.uuid_e &&
1106 	    attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
1107 		char *dev_name = NULL;
1108 		if (attr.dev_name) {
1109 			dev_name = os_zalloc(attr.dev_name_len + 1);
1110 			if (dev_name) {
1111 				os_memcpy(dev_name, attr.dev_name,
1112 					  attr.dev_name_len);
1113 			}
1114 		}
1115 		reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
1116 				      attr.primary_dev_type,
1117 				      WPA_GET_BE16(attr.config_methods),
1118 				      WPA_GET_BE16(attr.dev_password_id),
1119 				      *attr.request_type, dev_name);
1120 		os_free(dev_name);
1121 	}
1122 
1123 	if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
1124 		return; /* Not PBC */
1125 
1126 	wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
1127 		   MACSTR, MAC2STR(addr));
1128 	if (attr.uuid_e == NULL) {
1129 		wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
1130 			   "UUID-E included");
1131 		return;
1132 	}
1133 	wpa_hexdump(MSG_DEBUG, "WPS: UUID-E from Probe Request", attr.uuid_e,
1134 		    WPS_UUID_LEN);
1135 
1136 #ifdef WPS_WORKAROUNDS
1137 	if (reg->pbc_ignore_start.sec &&
1138 	    os_memcmp(attr.uuid_e, reg->pbc_ignore_uuid, WPS_UUID_LEN) == 0) {
1139 		struct os_time now, dur;
1140 		os_get_time(&now);
1141 		os_time_sub(&now, &reg->pbc_ignore_start, &dur);
1142 		if (dur.sec >= 0 && dur.sec < 5) {
1143 			wpa_printf(MSG_DEBUG, "WPS: Ignore PBC activation "
1144 				   "based on Probe Request from the Enrollee "
1145 				   "that just completed PBC provisioning");
1146 			skip_add = 1;
1147 		} else
1148 			reg->pbc_ignore_start.sec = 0;
1149 	}
1150 #endif /* WPS_WORKAROUNDS */
1151 
1152 	if (!skip_add)
1153 		wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
1154 	if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
1155 		wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
1156 		reg->force_pbc_overlap = 1;
1157 		wps_pbc_overlap_event(reg->wps);
1158 	}
1159 }
1160 
1161 
1162 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
1163 			  const u8 *psk, size_t psk_len)
1164 {
1165 	if (reg->new_psk_cb == NULL)
1166 		return 0;
1167 
1168 	return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
1169 }
1170 
1171 
1172 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
1173 			      const struct wps_device_data *dev)
1174 {
1175 	if (reg->pin_needed_cb == NULL)
1176 		return;
1177 
1178 	reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
1179 }
1180 
1181 
1182 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
1183 			       const u8 *uuid_e, const u8 *dev_pw,
1184 			       size_t dev_pw_len)
1185 {
1186 	if (reg->reg_success_cb == NULL)
1187 		return;
1188 
1189 	reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e, dev_pw, dev_pw_len);
1190 }
1191 
1192 
1193 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
1194 			 struct wpabuf *probe_resp_ie)
1195 {
1196 	return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
1197 }
1198 
1199 
1200 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
1201 {
1202 	u16 methods = 0;
1203 	if (reg->set_sel_reg_cb == NULL)
1204 		return;
1205 
1206 	if (reg->selected_registrar) {
1207 		methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
1208 #ifdef CONFIG_WPS2
1209 		methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
1210 			     WPS_CONFIG_PHY_PUSHBUTTON);
1211 #endif /* CONFIG_WPS2 */
1212 		if (reg->pbc)
1213 			wps_set_pushbutton(&methods, reg->wps->config_methods);
1214 	}
1215 
1216 	wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1217 		   "config_methods=0x%x pbc=%d methods=0x%x",
1218 		   reg->selected_registrar, reg->wps->config_methods,
1219 		   reg->pbc, methods);
1220 
1221 	reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
1222 			    reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
1223 			    methods);
1224 }
1225 
1226 
1227 static int wps_set_ie(struct wps_registrar *reg)
1228 {
1229 	struct wpabuf *beacon;
1230 	struct wpabuf *probe;
1231 	const u8 *auth_macs;
1232 	size_t count;
1233 	size_t vendor_len = 0;
1234 	int i;
1235 
1236 	if (reg->set_ie_cb == NULL)
1237 		return 0;
1238 
1239 	for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
1240 		if (reg->wps->dev.vendor_ext[i]) {
1241 			vendor_len += 2 + 2;
1242 			vendor_len += wpabuf_len(reg->wps->dev.vendor_ext[i]);
1243 		}
1244 	}
1245 
1246 	beacon = wpabuf_alloc(400 + vendor_len);
1247 	if (beacon == NULL)
1248 		return -1;
1249 	probe = wpabuf_alloc(500 + vendor_len);
1250 	if (probe == NULL) {
1251 		wpabuf_free(beacon);
1252 		return -1;
1253 	}
1254 
1255 	auth_macs = wps_authorized_macs(reg, &count);
1256 
1257 	wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
1258 
1259 	if (wps_build_version(beacon) ||
1260 	    wps_build_wps_state(reg->wps, beacon) ||
1261 	    wps_build_ap_setup_locked(reg->wps, beacon) ||
1262 	    wps_build_selected_registrar(reg, beacon) ||
1263 	    wps_build_sel_reg_dev_password_id(reg, beacon) ||
1264 	    wps_build_sel_reg_config_methods(reg, beacon) ||
1265 	    wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
1266 	    (reg->dualband && wps_build_rf_bands(&reg->wps->dev, beacon)) ||
1267 	    wps_build_wfa_ext(beacon, 0, auth_macs, count) ||
1268 	    wps_build_vendor_ext(&reg->wps->dev, beacon)) {
1269 		wpabuf_free(beacon);
1270 		wpabuf_free(probe);
1271 		return -1;
1272 	}
1273 
1274 #ifdef CONFIG_P2P
1275 	if (wps_build_dev_name(&reg->wps->dev, beacon) ||
1276 	    wps_build_primary_dev_type(&reg->wps->dev, beacon)) {
1277 		wpabuf_free(beacon);
1278 		wpabuf_free(probe);
1279 		return -1;
1280 	}
1281 #endif /* CONFIG_P2P */
1282 
1283 	wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
1284 
1285 	if (wps_build_version(probe) ||
1286 	    wps_build_wps_state(reg->wps, probe) ||
1287 	    wps_build_ap_setup_locked(reg->wps, probe) ||
1288 	    wps_build_selected_registrar(reg, probe) ||
1289 	    wps_build_sel_reg_dev_password_id(reg, probe) ||
1290 	    wps_build_sel_reg_config_methods(reg, probe) ||
1291 	    wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
1292 				WPS_RESP_REGISTRAR) ||
1293 	    wps_build_uuid_e(probe, reg->wps->uuid) ||
1294 	    wps_build_device_attrs(&reg->wps->dev, probe) ||
1295 	    wps_build_probe_config_methods(reg, probe) ||
1296 	    (reg->dualband && wps_build_rf_bands(&reg->wps->dev, probe)) ||
1297 	    wps_build_wfa_ext(probe, 0, auth_macs, count) ||
1298 	    wps_build_vendor_ext(&reg->wps->dev, probe)) {
1299 		wpabuf_free(beacon);
1300 		wpabuf_free(probe);
1301 		return -1;
1302 	}
1303 
1304 	beacon = wps_ie_encapsulate(beacon);
1305 	probe = wps_ie_encapsulate(probe);
1306 
1307 	if (!beacon || !probe) {
1308 		wpabuf_free(beacon);
1309 		wpabuf_free(probe);
1310 		return -1;
1311 	}
1312 
1313 	if (reg->static_wep_only) {
1314 		/*
1315 		 * Windows XP and Vista clients can get confused about
1316 		 * EAP-Identity/Request when they probe the network with
1317 		 * EAPOL-Start. In such a case, they may assume the network is
1318 		 * using IEEE 802.1X and prompt user for a certificate while
1319 		 * the correct (non-WPS) behavior would be to ask for the
1320 		 * static WEP key. As a workaround, use Microsoft Provisioning
1321 		 * IE to advertise that legacy 802.1X is not supported.
1322 		 */
1323 		const u8 ms_wps[7] = {
1324 			WLAN_EID_VENDOR_SPECIFIC, 5,
1325 			/* Microsoft Provisioning IE (00:50:f2:5) */
1326 			0x00, 0x50, 0xf2, 5,
1327 			0x00 /* no legacy 802.1X or MS WPS */
1328 		};
1329 		wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
1330 			   "into Beacon/Probe Response frames");
1331 		wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
1332 		wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
1333 	}
1334 
1335 	return wps_cb_set_ie(reg, beacon, probe);
1336 }
1337 
1338 
1339 static int wps_get_dev_password(struct wps_data *wps)
1340 {
1341 	const u8 *pin;
1342 	size_t pin_len = 0;
1343 
1344 	os_free(wps->dev_password);
1345 	wps->dev_password = NULL;
1346 
1347 	if (wps->pbc) {
1348 		wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
1349 		pin = (const u8 *) "00000000";
1350 		pin_len = 8;
1351 #ifdef CONFIG_WPS_NFC
1352 	} else if (wps->nfc_pw_token) {
1353 		wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from NFC "
1354 			   "Password Token");
1355 		pin = wps->nfc_pw_token->dev_pw;
1356 		pin_len = wps->nfc_pw_token->dev_pw_len;
1357 #endif /* CONFIG_WPS_NFC */
1358 	} else {
1359 		pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1360 					    &pin_len);
1361 	}
1362 	if (pin == NULL) {
1363 		wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1364 			   "the Enrollee");
1365 		wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1366 				  &wps->peer_dev);
1367 		return -1;
1368 	}
1369 
1370 	wps->dev_password = os_malloc(pin_len);
1371 	if (wps->dev_password == NULL)
1372 		return -1;
1373 	os_memcpy(wps->dev_password, pin, pin_len);
1374 	wps->dev_password_len = pin_len;
1375 
1376 	return 0;
1377 }
1378 
1379 
1380 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1381 {
1382 	wpa_printf(MSG_DEBUG, "WPS:  * UUID-R");
1383 	wpabuf_put_be16(msg, ATTR_UUID_R);
1384 	wpabuf_put_be16(msg, WPS_UUID_LEN);
1385 	wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1386 	return 0;
1387 }
1388 
1389 
1390 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1391 {
1392 	u8 *hash;
1393 	const u8 *addr[4];
1394 	size_t len[4];
1395 
1396 	if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1397 		return -1;
1398 	wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1399 	wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1400 		    wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1401 
1402 	if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1403 		wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1404 			   "R-Hash derivation");
1405 		return -1;
1406 	}
1407 
1408 	wpa_printf(MSG_DEBUG, "WPS:  * R-Hash1");
1409 	wpabuf_put_be16(msg, ATTR_R_HASH1);
1410 	wpabuf_put_be16(msg, SHA256_MAC_LEN);
1411 	hash = wpabuf_put(msg, SHA256_MAC_LEN);
1412 	/* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1413 	addr[0] = wps->snonce;
1414 	len[0] = WPS_SECRET_NONCE_LEN;
1415 	addr[1] = wps->psk1;
1416 	len[1] = WPS_PSK_LEN;
1417 	addr[2] = wpabuf_head(wps->dh_pubkey_e);
1418 	len[2] = wpabuf_len(wps->dh_pubkey_e);
1419 	addr[3] = wpabuf_head(wps->dh_pubkey_r);
1420 	len[3] = wpabuf_len(wps->dh_pubkey_r);
1421 	hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1422 	wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1423 
1424 	wpa_printf(MSG_DEBUG, "WPS:  * R-Hash2");
1425 	wpabuf_put_be16(msg, ATTR_R_HASH2);
1426 	wpabuf_put_be16(msg, SHA256_MAC_LEN);
1427 	hash = wpabuf_put(msg, SHA256_MAC_LEN);
1428 	/* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1429 	addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1430 	addr[1] = wps->psk2;
1431 	hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1432 	wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1433 
1434 	return 0;
1435 }
1436 
1437 
1438 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1439 {
1440 	wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce1");
1441 	wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1442 	wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1443 	wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1444 	return 0;
1445 }
1446 
1447 
1448 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1449 {
1450 	wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce2");
1451 	wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1452 	wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1453 	wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1454 			WPS_SECRET_NONCE_LEN);
1455 	return 0;
1456 }
1457 
1458 
1459 static int wps_build_cred_network_idx(struct wpabuf *msg,
1460 				      const struct wps_credential *cred)
1461 {
1462 	wpa_printf(MSG_DEBUG, "WPS:  * Network Index (1)");
1463 	wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1464 	wpabuf_put_be16(msg, 1);
1465 	wpabuf_put_u8(msg, 1);
1466 	return 0;
1467 }
1468 
1469 
1470 static int wps_build_cred_ssid(struct wpabuf *msg,
1471 			       const struct wps_credential *cred)
1472 {
1473 	wpa_printf(MSG_DEBUG, "WPS:  * SSID");
1474 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID for Credential",
1475 			  cred->ssid, cred->ssid_len);
1476 	wpabuf_put_be16(msg, ATTR_SSID);
1477 	wpabuf_put_be16(msg, cred->ssid_len);
1478 	wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1479 	return 0;
1480 }
1481 
1482 
1483 static int wps_build_cred_auth_type(struct wpabuf *msg,
1484 				    const struct wps_credential *cred)
1485 {
1486 	wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type (0x%x)",
1487 		   cred->auth_type);
1488 	wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1489 	wpabuf_put_be16(msg, 2);
1490 	wpabuf_put_be16(msg, cred->auth_type);
1491 	return 0;
1492 }
1493 
1494 
1495 static int wps_build_cred_encr_type(struct wpabuf *msg,
1496 				    const struct wps_credential *cred)
1497 {
1498 	wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type (0x%x)",
1499 		   cred->encr_type);
1500 	wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1501 	wpabuf_put_be16(msg, 2);
1502 	wpabuf_put_be16(msg, cred->encr_type);
1503 	return 0;
1504 }
1505 
1506 
1507 static int wps_build_cred_network_key(struct wpabuf *msg,
1508 				      const struct wps_credential *cred)
1509 {
1510 	wpa_printf(MSG_DEBUG, "WPS:  * Network Key (len=%d)",
1511 		   (int) cred->key_len);
1512 	wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
1513 			cred->key, cred->key_len);
1514 	wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1515 	wpabuf_put_be16(msg, cred->key_len);
1516 	wpabuf_put_data(msg, cred->key, cred->key_len);
1517 	return 0;
1518 }
1519 
1520 
1521 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1522 				   const struct wps_credential *cred)
1523 {
1524 	wpa_printf(MSG_DEBUG, "WPS:  * MAC Address (" MACSTR ")",
1525 		   MAC2STR(cred->mac_addr));
1526 	wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1527 	wpabuf_put_be16(msg, ETH_ALEN);
1528 	wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1529 	return 0;
1530 }
1531 
1532 
1533 static int wps_build_credential(struct wpabuf *msg,
1534 				const struct wps_credential *cred)
1535 {
1536 	if (wps_build_cred_network_idx(msg, cred) ||
1537 	    wps_build_cred_ssid(msg, cred) ||
1538 	    wps_build_cred_auth_type(msg, cred) ||
1539 	    wps_build_cred_encr_type(msg, cred) ||
1540 	    wps_build_cred_network_key(msg, cred) ||
1541 	    wps_build_cred_mac_addr(msg, cred))
1542 		return -1;
1543 	return 0;
1544 }
1545 
1546 
1547 int wps_build_credential_wrap(struct wpabuf *msg,
1548 			      const struct wps_credential *cred)
1549 {
1550 	struct wpabuf *wbuf;
1551 	wbuf = wpabuf_alloc(200);
1552 	if (wbuf == NULL)
1553 		return -1;
1554 	if (wps_build_credential(wbuf, cred)) {
1555 		wpabuf_free(wbuf);
1556 		return -1;
1557 	}
1558 	wpabuf_put_be16(msg, ATTR_CRED);
1559 	wpabuf_put_be16(msg, wpabuf_len(wbuf));
1560 	wpabuf_put_buf(msg, wbuf);
1561 	wpabuf_free(wbuf);
1562 	return 0;
1563 }
1564 
1565 
1566 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1567 {
1568 	struct wpabuf *cred;
1569 
1570 	if (wps->wps->registrar->skip_cred_build)
1571 		goto skip_cred_build;
1572 
1573 	wpa_printf(MSG_DEBUG, "WPS:  * Credential");
1574 	if (wps->use_cred) {
1575 		os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1576 		goto use_provided;
1577 	}
1578 	os_memset(&wps->cred, 0, sizeof(wps->cred));
1579 
1580 	os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1581 	wps->cred.ssid_len = wps->wps->ssid_len;
1582 
1583 	/* Select the best authentication and encryption type */
1584 	if (wps->auth_type & WPS_AUTH_WPA2PSK)
1585 		wps->auth_type = WPS_AUTH_WPA2PSK;
1586 	else if (wps->auth_type & WPS_AUTH_WPAPSK)
1587 		wps->auth_type = WPS_AUTH_WPAPSK;
1588 	else if (wps->auth_type & WPS_AUTH_OPEN)
1589 		wps->auth_type = WPS_AUTH_OPEN;
1590 	else if (wps->auth_type & WPS_AUTH_SHARED)
1591 		wps->auth_type = WPS_AUTH_SHARED;
1592 	else {
1593 		wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1594 			   wps->auth_type);
1595 		return -1;
1596 	}
1597 	wps->cred.auth_type = wps->auth_type;
1598 
1599 	if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1600 	    wps->auth_type == WPS_AUTH_WPAPSK) {
1601 		if (wps->encr_type & WPS_ENCR_AES)
1602 			wps->encr_type = WPS_ENCR_AES;
1603 		else if (wps->encr_type & WPS_ENCR_TKIP)
1604 			wps->encr_type = WPS_ENCR_TKIP;
1605 		else {
1606 			wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1607 				   "type for WPA/WPA2");
1608 			return -1;
1609 		}
1610 	} else {
1611 		if (wps->encr_type & WPS_ENCR_WEP)
1612 			wps->encr_type = WPS_ENCR_WEP;
1613 		else if (wps->encr_type & WPS_ENCR_NONE)
1614 			wps->encr_type = WPS_ENCR_NONE;
1615 		else {
1616 			wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1617 				   "type for non-WPA/WPA2 mode");
1618 			return -1;
1619 		}
1620 	}
1621 	wps->cred.encr_type = wps->encr_type;
1622 	/*
1623 	 * Set MAC address in the Credential to be the Enrollee's MAC address
1624 	 */
1625 	os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1626 
1627 	if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1628 	    !wps->wps->registrar->disable_auto_conf) {
1629 		u8 r[16];
1630 		/* Generate a random passphrase */
1631 		if (random_get_bytes(r, sizeof(r)) < 0)
1632 			return -1;
1633 		os_free(wps->new_psk);
1634 		wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1635 		if (wps->new_psk == NULL)
1636 			return -1;
1637 		wps->new_psk_len--; /* remove newline */
1638 		while (wps->new_psk_len &&
1639 		       wps->new_psk[wps->new_psk_len - 1] == '=')
1640 			wps->new_psk_len--;
1641 		wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1642 				      wps->new_psk, wps->new_psk_len);
1643 		os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1644 		wps->cred.key_len = wps->new_psk_len;
1645 	} else if (wps->use_psk_key && wps->wps->psk_set) {
1646 		char hex[65];
1647 		wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1648 		wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1649 		os_memcpy(wps->cred.key, hex, 32 * 2);
1650 		wps->cred.key_len = 32 * 2;
1651 	} else if (wps->wps->network_key) {
1652 		os_memcpy(wps->cred.key, wps->wps->network_key,
1653 			  wps->wps->network_key_len);
1654 		wps->cred.key_len = wps->wps->network_key_len;
1655 	} else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1656 		char hex[65];
1657 		/* Generate a random per-device PSK */
1658 		os_free(wps->new_psk);
1659 		wps->new_psk_len = 32;
1660 		wps->new_psk = os_malloc(wps->new_psk_len);
1661 		if (wps->new_psk == NULL)
1662 			return -1;
1663 		if (random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
1664 			os_free(wps->new_psk);
1665 			wps->new_psk = NULL;
1666 			return -1;
1667 		}
1668 		wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1669 				wps->new_psk, wps->new_psk_len);
1670 		wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1671 				 wps->new_psk_len);
1672 		os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1673 		wps->cred.key_len = wps->new_psk_len * 2;
1674 	}
1675 
1676 use_provided:
1677 #ifdef CONFIG_WPS_TESTING
1678 	if (wps_testing_dummy_cred)
1679 		cred = wpabuf_alloc(200);
1680 	else
1681 		cred = NULL;
1682 	if (cred) {
1683 		struct wps_credential dummy;
1684 		wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
1685 		os_memset(&dummy, 0, sizeof(dummy));
1686 		os_memcpy(dummy.ssid, "dummy", 5);
1687 		dummy.ssid_len = 5;
1688 		dummy.auth_type = WPS_AUTH_WPA2PSK;
1689 		dummy.encr_type = WPS_ENCR_AES;
1690 		os_memcpy(dummy.key, "dummy psk", 9);
1691 		dummy.key_len = 9;
1692 		os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
1693 		wps_build_credential(cred, &dummy);
1694 		wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
1695 
1696 		wpabuf_put_be16(msg, ATTR_CRED);
1697 		wpabuf_put_be16(msg, wpabuf_len(cred));
1698 		wpabuf_put_buf(msg, cred);
1699 
1700 		wpabuf_free(cred);
1701 	}
1702 #endif /* CONFIG_WPS_TESTING */
1703 
1704 	cred = wpabuf_alloc(200);
1705 	if (cred == NULL)
1706 		return -1;
1707 
1708 	if (wps_build_credential(cred, &wps->cred)) {
1709 		wpabuf_free(cred);
1710 		return -1;
1711 	}
1712 
1713 	wpabuf_put_be16(msg, ATTR_CRED);
1714 	wpabuf_put_be16(msg, wpabuf_len(cred));
1715 	wpabuf_put_buf(msg, cred);
1716 	wpabuf_free(cred);
1717 
1718 skip_cred_build:
1719 	if (wps->wps->registrar->extra_cred) {
1720 		wpa_printf(MSG_DEBUG, "WPS:  * Credential (pre-configured)");
1721 		wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1722 	}
1723 
1724 	return 0;
1725 }
1726 
1727 
1728 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1729 {
1730 	wpa_printf(MSG_DEBUG, "WPS:  * AP Settings");
1731 
1732 	if (wps_build_credential(msg, &wps->cred))
1733 		return -1;
1734 
1735 	return 0;
1736 }
1737 
1738 
1739 static struct wpabuf * wps_build_ap_cred(struct wps_data *wps)
1740 {
1741 	struct wpabuf *msg, *plain;
1742 
1743 	msg = wpabuf_alloc(1000);
1744 	if (msg == NULL)
1745 		return NULL;
1746 
1747 	plain = wpabuf_alloc(200);
1748 	if (plain == NULL) {
1749 		wpabuf_free(msg);
1750 		return NULL;
1751 	}
1752 
1753 	if (wps_build_ap_settings(wps, plain)) {
1754 		wpabuf_free(plain);
1755 		wpabuf_free(msg);
1756 		return NULL;
1757 	}
1758 
1759 	wpabuf_put_be16(msg, ATTR_CRED);
1760 	wpabuf_put_be16(msg, wpabuf_len(plain));
1761 	wpabuf_put_buf(msg, plain);
1762 	wpabuf_free(plain);
1763 
1764 	return msg;
1765 }
1766 
1767 
1768 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1769 {
1770 	struct wpabuf *msg;
1771 
1772 	if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
1773 		return NULL;
1774 	wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1775 		    wps->nonce_r, WPS_NONCE_LEN);
1776 	wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1777 
1778 	wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1779 	msg = wpabuf_alloc(1000);
1780 	if (msg == NULL)
1781 		return NULL;
1782 
1783 	if (wps_build_version(msg) ||
1784 	    wps_build_msg_type(msg, WPS_M2) ||
1785 	    wps_build_enrollee_nonce(wps, msg) ||
1786 	    wps_build_registrar_nonce(wps, msg) ||
1787 	    wps_build_uuid_r(wps, msg) ||
1788 	    wps_build_public_key(wps, msg) ||
1789 	    wps_derive_keys(wps) ||
1790 	    wps_build_auth_type_flags(wps, msg) ||
1791 	    wps_build_encr_type_flags(wps, msg) ||
1792 	    wps_build_conn_type_flags(wps, msg) ||
1793 	    wps_build_config_methods_r(wps->wps->registrar, msg) ||
1794 	    wps_build_device_attrs(&wps->wps->dev, msg) ||
1795 	    wps_build_rf_bands(&wps->wps->dev, msg) ||
1796 	    wps_build_assoc_state(wps, msg) ||
1797 	    wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1798 	    wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1799 	    wps_build_os_version(&wps->wps->dev, msg) ||
1800 	    wps_build_wfa_ext(msg, 0, NULL, 0) ||
1801 	    wps_build_authenticator(wps, msg)) {
1802 		wpabuf_free(msg);
1803 		return NULL;
1804 	}
1805 
1806 	wps->int_reg = 1;
1807 	wps->state = RECV_M3;
1808 	return msg;
1809 }
1810 
1811 
1812 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1813 {
1814 	struct wpabuf *msg;
1815 	u16 err = wps->config_error;
1816 
1817 	wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1818 	msg = wpabuf_alloc(1000);
1819 	if (msg == NULL)
1820 		return NULL;
1821 
1822 	if (wps->wps->ap && wps->wps->ap_setup_locked &&
1823 	    err == WPS_CFG_NO_ERROR)
1824 		err = WPS_CFG_SETUP_LOCKED;
1825 
1826 	if (wps_build_version(msg) ||
1827 	    wps_build_msg_type(msg, WPS_M2D) ||
1828 	    wps_build_enrollee_nonce(wps, msg) ||
1829 	    wps_build_registrar_nonce(wps, msg) ||
1830 	    wps_build_uuid_r(wps, msg) ||
1831 	    wps_build_auth_type_flags(wps, msg) ||
1832 	    wps_build_encr_type_flags(wps, msg) ||
1833 	    wps_build_conn_type_flags(wps, msg) ||
1834 	    wps_build_config_methods_r(wps->wps->registrar, msg) ||
1835 	    wps_build_device_attrs(&wps->wps->dev, msg) ||
1836 	    wps_build_rf_bands(&wps->wps->dev, msg) ||
1837 	    wps_build_assoc_state(wps, msg) ||
1838 	    wps_build_config_error(msg, err) ||
1839 	    wps_build_os_version(&wps->wps->dev, msg) ||
1840 	    wps_build_wfa_ext(msg, 0, NULL, 0)) {
1841 		wpabuf_free(msg);
1842 		return NULL;
1843 	}
1844 
1845 	wps->state = RECV_M2D_ACK;
1846 	return msg;
1847 }
1848 
1849 
1850 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1851 {
1852 	struct wpabuf *msg, *plain;
1853 
1854 	wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1855 
1856 	wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1857 
1858 	plain = wpabuf_alloc(200);
1859 	if (plain == NULL)
1860 		return NULL;
1861 
1862 	msg = wpabuf_alloc(1000);
1863 	if (msg == NULL) {
1864 		wpabuf_free(plain);
1865 		return NULL;
1866 	}
1867 
1868 	if (wps_build_version(msg) ||
1869 	    wps_build_msg_type(msg, WPS_M4) ||
1870 	    wps_build_enrollee_nonce(wps, msg) ||
1871 	    wps_build_r_hash(wps, msg) ||
1872 	    wps_build_r_snonce1(wps, plain) ||
1873 	    wps_build_key_wrap_auth(wps, plain) ||
1874 	    wps_build_encr_settings(wps, msg, plain) ||
1875 	    wps_build_wfa_ext(msg, 0, NULL, 0) ||
1876 	    wps_build_authenticator(wps, msg)) {
1877 		wpabuf_free(plain);
1878 		wpabuf_free(msg);
1879 		return NULL;
1880 	}
1881 	wpabuf_free(plain);
1882 
1883 	wps->state = RECV_M5;
1884 	return msg;
1885 }
1886 
1887 
1888 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1889 {
1890 	struct wpabuf *msg, *plain;
1891 
1892 	wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1893 
1894 	plain = wpabuf_alloc(200);
1895 	if (plain == NULL)
1896 		return NULL;
1897 
1898 	msg = wpabuf_alloc(1000);
1899 	if (msg == NULL) {
1900 		wpabuf_free(plain);
1901 		return NULL;
1902 	}
1903 
1904 	if (wps_build_version(msg) ||
1905 	    wps_build_msg_type(msg, WPS_M6) ||
1906 	    wps_build_enrollee_nonce(wps, msg) ||
1907 	    wps_build_r_snonce2(wps, plain) ||
1908 	    wps_build_key_wrap_auth(wps, plain) ||
1909 	    wps_build_encr_settings(wps, msg, plain) ||
1910 	    wps_build_wfa_ext(msg, 0, NULL, 0) ||
1911 	    wps_build_authenticator(wps, msg)) {
1912 		wpabuf_free(plain);
1913 		wpabuf_free(msg);
1914 		return NULL;
1915 	}
1916 	wpabuf_free(plain);
1917 
1918 	wps->wps_pin_revealed = 1;
1919 	wps->state = RECV_M7;
1920 	return msg;
1921 }
1922 
1923 
1924 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1925 {
1926 	struct wpabuf *msg, *plain;
1927 
1928 	wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1929 
1930 	plain = wpabuf_alloc(500);
1931 	if (plain == NULL)
1932 		return NULL;
1933 
1934 	msg = wpabuf_alloc(1000);
1935 	if (msg == NULL) {
1936 		wpabuf_free(plain);
1937 		return NULL;
1938 	}
1939 
1940 	if (wps_build_version(msg) ||
1941 	    wps_build_msg_type(msg, WPS_M8) ||
1942 	    wps_build_enrollee_nonce(wps, msg) ||
1943 	    ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1944 	    (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1945 	    wps_build_key_wrap_auth(wps, plain) ||
1946 	    wps_build_encr_settings(wps, msg, plain) ||
1947 	    wps_build_wfa_ext(msg, 0, NULL, 0) ||
1948 	    wps_build_authenticator(wps, msg)) {
1949 		wpabuf_free(plain);
1950 		wpabuf_free(msg);
1951 		return NULL;
1952 	}
1953 	wpabuf_free(plain);
1954 
1955 	wps->state = RECV_DONE;
1956 	return msg;
1957 }
1958 
1959 
1960 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1961 				      enum wsc_op_code *op_code)
1962 {
1963 	struct wpabuf *msg;
1964 
1965 #ifdef CONFIG_WPS_UPNP
1966 	if (!wps->int_reg && wps->wps->wps_upnp) {
1967 		struct upnp_pending_message *p, *prev = NULL;
1968 		if (wps->ext_reg > 1)
1969 			wps_registrar_free_pending_m2(wps->wps);
1970 		p = wps->wps->upnp_msgs;
1971 		/* TODO: check pending message MAC address */
1972 		while (p && p->next) {
1973 			prev = p;
1974 			p = p->next;
1975 		}
1976 		if (p) {
1977 			wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1978 				   "UPnP");
1979 			if (prev)
1980 				prev->next = NULL;
1981 			else
1982 				wps->wps->upnp_msgs = NULL;
1983 			msg = p->msg;
1984 			switch (p->type) {
1985 			case WPS_WSC_ACK:
1986 				*op_code = WSC_ACK;
1987 				break;
1988 			case WPS_WSC_NACK:
1989 				*op_code = WSC_NACK;
1990 				break;
1991 			default:
1992 				*op_code = WSC_MSG;
1993 				break;
1994 			}
1995 			os_free(p);
1996 			if (wps->ext_reg == 0)
1997 				wps->ext_reg = 1;
1998 			return msg;
1999 		}
2000 	}
2001 	if (wps->ext_reg) {
2002 		wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
2003 			   "pending message available");
2004 		return NULL;
2005 	}
2006 #endif /* CONFIG_WPS_UPNP */
2007 
2008 	switch (wps->state) {
2009 	case SEND_M2:
2010 		if (wps_get_dev_password(wps) < 0)
2011 			msg = wps_build_m2d(wps);
2012 		else
2013 			msg = wps_build_m2(wps);
2014 		*op_code = WSC_MSG;
2015 		break;
2016 	case SEND_M2D:
2017 		msg = wps_build_m2d(wps);
2018 		*op_code = WSC_MSG;
2019 		break;
2020 	case SEND_M4:
2021 		msg = wps_build_m4(wps);
2022 		*op_code = WSC_MSG;
2023 		break;
2024 	case SEND_M6:
2025 		msg = wps_build_m6(wps);
2026 		*op_code = WSC_MSG;
2027 		break;
2028 	case SEND_M8:
2029 		msg = wps_build_m8(wps);
2030 		*op_code = WSC_MSG;
2031 		break;
2032 	case RECV_DONE:
2033 		msg = wps_build_wsc_ack(wps);
2034 		*op_code = WSC_ACK;
2035 		break;
2036 	case SEND_WSC_NACK:
2037 		msg = wps_build_wsc_nack(wps);
2038 		*op_code = WSC_NACK;
2039 		break;
2040 	default:
2041 		wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
2042 			   "a message", wps->state);
2043 		msg = NULL;
2044 		break;
2045 	}
2046 
2047 	if (*op_code == WSC_MSG && msg) {
2048 		/* Save a copy of the last message for Authenticator derivation
2049 		 */
2050 		wpabuf_free(wps->last_msg);
2051 		wps->last_msg = wpabuf_dup(msg);
2052 	}
2053 
2054 	return msg;
2055 }
2056 
2057 
2058 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
2059 {
2060 	if (e_nonce == NULL) {
2061 		wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
2062 		return -1;
2063 	}
2064 
2065 	os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
2066 	wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
2067 		    wps->nonce_e, WPS_NONCE_LEN);
2068 
2069 	return 0;
2070 }
2071 
2072 
2073 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
2074 {
2075 	if (r_nonce == NULL) {
2076 		wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
2077 		return -1;
2078 	}
2079 
2080 	if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
2081 		wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
2082 		return -1;
2083 	}
2084 
2085 	return 0;
2086 }
2087 
2088 
2089 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
2090 {
2091 	if (uuid_e == NULL) {
2092 		wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
2093 		return -1;
2094 	}
2095 
2096 	os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
2097 	wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
2098 
2099 	return 0;
2100 }
2101 
2102 
2103 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
2104 {
2105 	if (pw_id == NULL) {
2106 		wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
2107 		return -1;
2108 	}
2109 
2110 	wps->dev_pw_id = WPA_GET_BE16(pw_id);
2111 	wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
2112 
2113 	return 0;
2114 }
2115 
2116 
2117 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
2118 {
2119 	if (e_hash1 == NULL) {
2120 		wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
2121 		return -1;
2122 	}
2123 
2124 	os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
2125 	wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
2126 
2127 	return 0;
2128 }
2129 
2130 
2131 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
2132 {
2133 	if (e_hash2 == NULL) {
2134 		wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
2135 		return -1;
2136 	}
2137 
2138 	os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
2139 	wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
2140 
2141 	return 0;
2142 }
2143 
2144 
2145 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
2146 {
2147 	u8 hash[SHA256_MAC_LEN];
2148 	const u8 *addr[4];
2149 	size_t len[4];
2150 
2151 	if (e_snonce1 == NULL) {
2152 		wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
2153 		return -1;
2154 	}
2155 
2156 	wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
2157 			WPS_SECRET_NONCE_LEN);
2158 
2159 	/* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
2160 	addr[0] = e_snonce1;
2161 	len[0] = WPS_SECRET_NONCE_LEN;
2162 	addr[1] = wps->psk1;
2163 	len[1] = WPS_PSK_LEN;
2164 	addr[2] = wpabuf_head(wps->dh_pubkey_e);
2165 	len[2] = wpabuf_len(wps->dh_pubkey_e);
2166 	addr[3] = wpabuf_head(wps->dh_pubkey_r);
2167 	len[3] = wpabuf_len(wps->dh_pubkey_r);
2168 	hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2169 
2170 	if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
2171 		wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
2172 			   "not match with the pre-committed value");
2173 		wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2174 		wps_pwd_auth_fail_event(wps->wps, 0, 1);
2175 		return -1;
2176 	}
2177 
2178 	wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
2179 		   "half of the device password");
2180 
2181 	return 0;
2182 }
2183 
2184 
2185 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
2186 {
2187 	u8 hash[SHA256_MAC_LEN];
2188 	const u8 *addr[4];
2189 	size_t len[4];
2190 
2191 	if (e_snonce2 == NULL) {
2192 		wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
2193 		return -1;
2194 	}
2195 
2196 	wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
2197 			WPS_SECRET_NONCE_LEN);
2198 
2199 	/* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
2200 	addr[0] = e_snonce2;
2201 	len[0] = WPS_SECRET_NONCE_LEN;
2202 	addr[1] = wps->psk2;
2203 	len[1] = WPS_PSK_LEN;
2204 	addr[2] = wpabuf_head(wps->dh_pubkey_e);
2205 	len[2] = wpabuf_len(wps->dh_pubkey_e);
2206 	addr[3] = wpabuf_head(wps->dh_pubkey_r);
2207 	len[3] = wpabuf_len(wps->dh_pubkey_r);
2208 	hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2209 
2210 	if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
2211 		wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
2212 			   "not match with the pre-committed value");
2213 		wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2214 		wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2215 		wps_pwd_auth_fail_event(wps->wps, 0, 2);
2216 		return -1;
2217 	}
2218 
2219 	wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
2220 		   "half of the device password");
2221 	wps->wps_pin_revealed = 0;
2222 	wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
2223 
2224 	/*
2225 	 * In case wildcard PIN is used and WPS handshake succeeds in the first
2226 	 * attempt, wps_registrar_unlock_pin() would not free the PIN, so make
2227 	 * sure the PIN gets invalidated here.
2228 	 */
2229 	wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2230 
2231 	return 0;
2232 }
2233 
2234 
2235 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
2236 {
2237 	if (mac_addr == NULL) {
2238 		wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
2239 		return -1;
2240 	}
2241 
2242 	wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
2243 		   MAC2STR(mac_addr));
2244 	os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
2245 	os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
2246 
2247 	return 0;
2248 }
2249 
2250 
2251 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
2252 			      size_t pk_len)
2253 {
2254 	if (pk == NULL || pk_len == 0) {
2255 		wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
2256 		return -1;
2257 	}
2258 
2259 	wpabuf_free(wps->dh_pubkey_e);
2260 	wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
2261 	if (wps->dh_pubkey_e == NULL)
2262 		return -1;
2263 
2264 	return 0;
2265 }
2266 
2267 
2268 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
2269 {
2270 	u16 auth_types;
2271 
2272 	if (auth == NULL) {
2273 		wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
2274 			   "received");
2275 		return -1;
2276 	}
2277 
2278 	auth_types = WPA_GET_BE16(auth);
2279 
2280 	wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
2281 		   auth_types);
2282 	wps->auth_type = wps->wps->auth_types & auth_types;
2283 	if (wps->auth_type == 0) {
2284 		wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2285 			   "authentication types (own 0x%x Enrollee 0x%x)",
2286 			   wps->wps->auth_types, auth_types);
2287 #ifdef WPS_WORKAROUNDS
2288 		/*
2289 		 * Some deployed implementations seem to advertise incorrect
2290 		 * information in this attribute. For example, Linksys WRT350N
2291 		 * seems to have a byteorder bug that breaks this negotiation.
2292 		 * In order to interoperate with existing implementations,
2293 		 * assume that the Enrollee supports everything we do.
2294 		 */
2295 		wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2296 			   "does not advertise supported authentication types "
2297 			   "correctly");
2298 		wps->auth_type = wps->wps->auth_types;
2299 #else /* WPS_WORKAROUNDS */
2300 		return -1;
2301 #endif /* WPS_WORKAROUNDS */
2302 	}
2303 
2304 	return 0;
2305 }
2306 
2307 
2308 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
2309 {
2310 	u16 encr_types;
2311 
2312 	if (encr == NULL) {
2313 		wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
2314 			   "received");
2315 		return -1;
2316 	}
2317 
2318 	encr_types = WPA_GET_BE16(encr);
2319 
2320 	wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
2321 		   encr_types);
2322 	wps->encr_type = wps->wps->encr_types & encr_types;
2323 	if (wps->encr_type == 0) {
2324 		wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2325 			   "encryption types (own 0x%x Enrollee 0x%x)",
2326 			   wps->wps->encr_types, encr_types);
2327 #ifdef WPS_WORKAROUNDS
2328 		/*
2329 		 * Some deployed implementations seem to advertise incorrect
2330 		 * information in this attribute. For example, Linksys WRT350N
2331 		 * seems to have a byteorder bug that breaks this negotiation.
2332 		 * In order to interoperate with existing implementations,
2333 		 * assume that the Enrollee supports everything we do.
2334 		 */
2335 		wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2336 			   "does not advertise supported encryption types "
2337 			   "correctly");
2338 		wps->encr_type = wps->wps->encr_types;
2339 #else /* WPS_WORKAROUNDS */
2340 		return -1;
2341 #endif /* WPS_WORKAROUNDS */
2342 	}
2343 
2344 	return 0;
2345 }
2346 
2347 
2348 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
2349 {
2350 	if (conn == NULL) {
2351 		wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
2352 			   "received");
2353 		return -1;
2354 	}
2355 
2356 	wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
2357 		   *conn);
2358 
2359 	return 0;
2360 }
2361 
2362 
2363 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
2364 {
2365 	u16 m;
2366 
2367 	if (methods == NULL) {
2368 		wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
2369 		return -1;
2370 	}
2371 
2372 	m = WPA_GET_BE16(methods);
2373 
2374 	wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
2375 		   "%s%s%s%s%s%s%s%s%s", m,
2376 		   m & WPS_CONFIG_USBA ? " [USBA]" : "",
2377 		   m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
2378 		   m & WPS_CONFIG_LABEL ? " [Label]" : "",
2379 		   m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
2380 		   m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
2381 		   m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
2382 		   m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
2383 		   m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
2384 		   m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
2385 
2386 	if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
2387 		/*
2388 		 * The Enrollee does not have a display so it is unlikely to be
2389 		 * able to show the passphrase to a user and as such, could
2390 		 * benefit from receiving PSK to reduce key derivation time.
2391 		 */
2392 		wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2393 			   "Enrollee not supporting display");
2394 		wps->use_psk_key = 1;
2395 	}
2396 
2397 	return 0;
2398 }
2399 
2400 
2401 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2402 {
2403 	if (state == NULL) {
2404 		wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2405 			   "received");
2406 		return -1;
2407 	}
2408 
2409 	wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2410 		   *state);
2411 
2412 	return 0;
2413 }
2414 
2415 
2416 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2417 {
2418 	u16 a;
2419 
2420 	if (assoc == NULL) {
2421 		wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2422 		return -1;
2423 	}
2424 
2425 	a = WPA_GET_BE16(assoc);
2426 	wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2427 
2428 	return 0;
2429 }
2430 
2431 
2432 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2433 {
2434 	u16 e;
2435 
2436 	if (err == NULL) {
2437 		wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2438 		return -1;
2439 	}
2440 
2441 	e = WPA_GET_BE16(err);
2442 	wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2443 
2444 	return 0;
2445 }
2446 
2447 
2448 static int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
2449 {
2450 #ifdef CONFIG_P2P
2451 	struct wps_registrar *reg = wps->wps->registrar;
2452 
2453 	if (is_zero_ether_addr(reg->p2p_dev_addr))
2454 		return 1; /* no filtering in use */
2455 
2456 	if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
2457 		wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
2458 			   "filtering for PBC: expected " MACSTR " was "
2459 			   MACSTR " - indicate PBC session overlap",
2460 			   MAC2STR(reg->p2p_dev_addr),
2461 			   MAC2STR(wps->p2p_dev_addr));
2462 		return 0;
2463 	}
2464 #endif /* CONFIG_P2P */
2465 	return 1;
2466 }
2467 
2468 
2469 static int wps_registrar_skip_overlap(struct wps_data *wps)
2470 {
2471 #ifdef CONFIG_P2P
2472 	struct wps_registrar *reg = wps->wps->registrar;
2473 
2474 	if (is_zero_ether_addr(reg->p2p_dev_addr))
2475 		return 0; /* no specific Enrollee selected */
2476 
2477 	if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) == 0) {
2478 		wpa_printf(MSG_DEBUG, "WPS: Skip PBC overlap due to selected "
2479 			   "Enrollee match");
2480 		return 1;
2481 	}
2482 #endif /* CONFIG_P2P */
2483 	return 0;
2484 }
2485 
2486 
2487 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2488 					   struct wps_parse_attr *attr)
2489 {
2490 	wpa_printf(MSG_DEBUG, "WPS: Received M1");
2491 
2492 	if (wps->state != RECV_M1) {
2493 		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2494 			   "receiving M1", wps->state);
2495 		return WPS_FAILURE;
2496 	}
2497 
2498 	if (wps_process_uuid_e(wps, attr->uuid_e) ||
2499 	    wps_process_mac_addr(wps, attr->mac_addr) ||
2500 	    wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2501 	    wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2502 	    wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2503 	    wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2504 	    wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2505 	    wps_process_config_methods(wps, attr->config_methods) ||
2506 	    wps_process_wps_state(wps, attr->wps_state) ||
2507 	    wps_process_device_attrs(&wps->peer_dev, attr) ||
2508 	    wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2509 	    wps_process_assoc_state(wps, attr->assoc_state) ||
2510 	    wps_process_dev_password_id(wps, attr->dev_password_id) ||
2511 	    wps_process_config_error(wps, attr->config_error) ||
2512 	    wps_process_os_version(&wps->peer_dev, attr->os_version))
2513 		return WPS_FAILURE;
2514 
2515 	if (wps->dev_pw_id < 0x10 &&
2516 	    wps->dev_pw_id != DEV_PW_DEFAULT &&
2517 	    wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2518 	    wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2519 	    wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2520 	    (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2521 	     !wps->wps->registrar->pbc)) {
2522 		wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2523 			   wps->dev_pw_id);
2524 		wps->state = SEND_M2D;
2525 		return WPS_CONTINUE;
2526 	}
2527 
2528 #ifdef CONFIG_WPS_NFC
2529 	if (wps->dev_pw_id >= 0x10) {
2530 		struct wps_nfc_pw_token *token;
2531 		const u8 *addr[1];
2532 		u8 hash[WPS_HASH_LEN];
2533 
2534 		token = wps_get_nfc_pw_token(
2535 			&wps->wps->registrar->nfc_pw_tokens, wps->dev_pw_id);
2536 		if (token) {
2537 			wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
2538 				   "Password Token");
2539 			dl_list_del(&token->list);
2540 			wps->nfc_pw_token = token;
2541 
2542 			addr[0] = attr->public_key;
2543 			sha256_vector(1, addr, &attr->public_key_len, hash);
2544 			if (os_memcmp(hash, wps->nfc_pw_token->pubkey_hash,
2545 				      WPS_OOB_PUBKEY_HASH_LEN) != 0) {
2546 				wpa_printf(MSG_ERROR, "WPS: Public Key hash "
2547 					   "mismatch");
2548 				return WPS_FAILURE;
2549 			}
2550 		}
2551 	}
2552 #endif /* CONFIG_WPS_NFC */
2553 
2554 	if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2555 		if ((wps->wps->registrar->force_pbc_overlap ||
2556 		     wps_registrar_pbc_overlap(wps->wps->registrar,
2557 					       wps->mac_addr_e, wps->uuid_e) ||
2558 		     !wps_registrar_p2p_dev_addr_match(wps)) &&
2559 		    !wps_registrar_skip_overlap(wps)) {
2560 			wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2561 				   "negotiation");
2562 			wps->state = SEND_M2D;
2563 			wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2564 			wps_pbc_overlap_event(wps->wps);
2565 			wps_fail_event(wps->wps, WPS_M1,
2566 				       WPS_CFG_MULTIPLE_PBC_DETECTED,
2567 				       WPS_EI_NO_ERROR);
2568 			wps->wps->registrar->force_pbc_overlap = 1;
2569 			return WPS_CONTINUE;
2570 		}
2571 		wps_registrar_add_pbc_session(wps->wps->registrar,
2572 					      wps->mac_addr_e, wps->uuid_e);
2573 		wps->pbc = 1;
2574 	}
2575 
2576 #ifdef WPS_WORKAROUNDS
2577 	/*
2578 	 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2579 	 * passphrase format. To avoid interop issues, force PSK format to be
2580 	 * used.
2581 	 */
2582 	if (!wps->use_psk_key &&
2583 	    wps->peer_dev.manufacturer &&
2584 	    os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
2585 	    wps->peer_dev.model_name &&
2586 	    os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
2587 		wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
2588 			   "PSK format");
2589 		wps->use_psk_key = 1;
2590 	}
2591 #endif /* WPS_WORKAROUNDS */
2592 
2593 	wps->state = SEND_M2;
2594 	return WPS_CONTINUE;
2595 }
2596 
2597 
2598 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2599 					   const struct wpabuf *msg,
2600 					   struct wps_parse_attr *attr)
2601 {
2602 	wpa_printf(MSG_DEBUG, "WPS: Received M3");
2603 
2604 	if (wps->state != RECV_M3) {
2605 		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2606 			   "receiving M3", wps->state);
2607 		wps->state = SEND_WSC_NACK;
2608 		return WPS_CONTINUE;
2609 	}
2610 
2611 	if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2612 	    !wps_registrar_skip_overlap(wps)) {
2613 		wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2614 			   "session overlap");
2615 		wps->state = SEND_WSC_NACK;
2616 		wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2617 		return WPS_CONTINUE;
2618 	}
2619 
2620 	if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2621 	    wps_process_authenticator(wps, attr->authenticator, msg) ||
2622 	    wps_process_e_hash1(wps, attr->e_hash1) ||
2623 	    wps_process_e_hash2(wps, attr->e_hash2)) {
2624 		wps->state = SEND_WSC_NACK;
2625 		return WPS_CONTINUE;
2626 	}
2627 
2628 	wps->state = SEND_M4;
2629 	return WPS_CONTINUE;
2630 }
2631 
2632 
2633 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2634 					   const struct wpabuf *msg,
2635 					   struct wps_parse_attr *attr)
2636 {
2637 	struct wpabuf *decrypted;
2638 	struct wps_parse_attr eattr;
2639 
2640 	wpa_printf(MSG_DEBUG, "WPS: Received M5");
2641 
2642 	if (wps->state != RECV_M5) {
2643 		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2644 			   "receiving M5", wps->state);
2645 		wps->state = SEND_WSC_NACK;
2646 		return WPS_CONTINUE;
2647 	}
2648 
2649 	if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2650 	    !wps_registrar_skip_overlap(wps)) {
2651 		wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2652 			   "session overlap");
2653 		wps->state = SEND_WSC_NACK;
2654 		wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2655 		return WPS_CONTINUE;
2656 	}
2657 
2658 	if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2659 	    wps_process_authenticator(wps, attr->authenticator, msg)) {
2660 		wps->state = SEND_WSC_NACK;
2661 		return WPS_CONTINUE;
2662 	}
2663 
2664 	decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2665 					      attr->encr_settings_len);
2666 	if (decrypted == NULL) {
2667 		wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2668 			   "Settings attribute");
2669 		wps->state = SEND_WSC_NACK;
2670 		return WPS_CONTINUE;
2671 	}
2672 
2673 	if (wps_validate_m5_encr(decrypted, attr->version2 != NULL) < 0) {
2674 		wpabuf_free(decrypted);
2675 		wps->state = SEND_WSC_NACK;
2676 		return WPS_CONTINUE;
2677 	}
2678 
2679 	wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2680 		   "attribute");
2681 	if (wps_parse_msg(decrypted, &eattr) < 0 ||
2682 	    wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2683 	    wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2684 		wpabuf_free(decrypted);
2685 		wps->state = SEND_WSC_NACK;
2686 		return WPS_CONTINUE;
2687 	}
2688 	wpabuf_free(decrypted);
2689 
2690 	wps->state = SEND_M6;
2691 	return WPS_CONTINUE;
2692 }
2693 
2694 
2695 static void wps_sta_cred_cb(struct wps_data *wps)
2696 {
2697 	/*
2698 	 * Update credential to only include a single authentication and
2699 	 * encryption type in case the AP configuration includes more than one
2700 	 * option.
2701 	 */
2702 	if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2703 		wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2704 	else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2705 		wps->cred.auth_type = WPS_AUTH_WPAPSK;
2706 	if (wps->cred.encr_type & WPS_ENCR_AES)
2707 		wps->cred.encr_type = WPS_ENCR_AES;
2708 	else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2709 		wps->cred.encr_type = WPS_ENCR_TKIP;
2710 	wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2711 		   "AP configuration");
2712 	if (wps->wps->cred_cb)
2713 		wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2714 }
2715 
2716 
2717 static void wps_cred_update(struct wps_credential *dst,
2718 			    struct wps_credential *src)
2719 {
2720 	os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2721 	dst->ssid_len = src->ssid_len;
2722 	dst->auth_type = src->auth_type;
2723 	dst->encr_type = src->encr_type;
2724 	dst->key_idx = src->key_idx;
2725 	os_memcpy(dst->key, src->key, sizeof(dst->key));
2726 	dst->key_len = src->key_len;
2727 }
2728 
2729 
2730 static int wps_process_ap_settings_r(struct wps_data *wps,
2731 				     struct wps_parse_attr *attr)
2732 {
2733 	struct wpabuf *msg;
2734 
2735 	if (wps->wps->ap || wps->er)
2736 		return 0;
2737 
2738 	/* AP Settings Attributes in M7 when Enrollee is an AP */
2739 	if (wps_process_ap_settings(attr, &wps->cred) < 0)
2740 		return -1;
2741 
2742 	wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2743 
2744 	if (wps->new_ap_settings) {
2745 		wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2746 			   "new settings");
2747 		wps_cred_update(&wps->cred, wps->new_ap_settings);
2748 		return 0;
2749 	} else {
2750 		/*
2751 		 * Use the AP PIN only to receive the current AP settings, not
2752 		 * to reconfigure the AP.
2753 		 */
2754 
2755 		/*
2756 		 * Clear selected registrar here since we do not get to
2757 		 * WSC_Done in this protocol run.
2758 		 */
2759 		wps_registrar_pin_completed(wps->wps->registrar);
2760 
2761 		msg = wps_build_ap_cred(wps);
2762 		if (msg == NULL)
2763 			return -1;
2764 		wps->cred.cred_attr = wpabuf_head(msg);
2765 		wps->cred.cred_attr_len = wpabuf_len(msg);
2766 
2767 		if (wps->ap_settings_cb) {
2768 			wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2769 					    &wps->cred);
2770 			wpabuf_free(msg);
2771 			return 1;
2772 		}
2773 		wps_sta_cred_cb(wps);
2774 
2775 		wps->cred.cred_attr = NULL;
2776 		wps->cred.cred_attr_len = 0;
2777 		wpabuf_free(msg);
2778 
2779 		return 1;
2780 	}
2781 }
2782 
2783 
2784 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2785 					   const struct wpabuf *msg,
2786 					   struct wps_parse_attr *attr)
2787 {
2788 	struct wpabuf *decrypted;
2789 	struct wps_parse_attr eattr;
2790 
2791 	wpa_printf(MSG_DEBUG, "WPS: Received M7");
2792 
2793 	if (wps->state != RECV_M7) {
2794 		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2795 			   "receiving M7", wps->state);
2796 		wps->state = SEND_WSC_NACK;
2797 		return WPS_CONTINUE;
2798 	}
2799 
2800 	if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2801 	    !wps_registrar_skip_overlap(wps)) {
2802 		wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2803 			   "session overlap");
2804 		wps->state = SEND_WSC_NACK;
2805 		wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2806 		return WPS_CONTINUE;
2807 	}
2808 
2809 	if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2810 	    wps_process_authenticator(wps, attr->authenticator, msg)) {
2811 		wps->state = SEND_WSC_NACK;
2812 		return WPS_CONTINUE;
2813 	}
2814 
2815 	decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2816 					      attr->encr_settings_len);
2817 	if (decrypted == NULL) {
2818 		wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2819 			   "Settings attribute");
2820 		wps->state = SEND_WSC_NACK;
2821 		return WPS_CONTINUE;
2822 	}
2823 
2824 	if (wps_validate_m7_encr(decrypted, wps->wps->ap || wps->er,
2825 				 attr->version2 != NULL) < 0) {
2826 		wpabuf_free(decrypted);
2827 		wps->state = SEND_WSC_NACK;
2828 		return WPS_CONTINUE;
2829 	}
2830 
2831 	wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2832 		   "attribute");
2833 	if (wps_parse_msg(decrypted, &eattr) < 0 ||
2834 	    wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2835 	    wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2836 	    wps_process_ap_settings_r(wps, &eattr)) {
2837 		wpabuf_free(decrypted);
2838 		wps->state = SEND_WSC_NACK;
2839 		return WPS_CONTINUE;
2840 	}
2841 
2842 	wpabuf_free(decrypted);
2843 
2844 	wps->state = SEND_M8;
2845 	return WPS_CONTINUE;
2846 }
2847 
2848 
2849 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2850 						const struct wpabuf *msg)
2851 {
2852 	struct wps_parse_attr attr;
2853 	enum wps_process_res ret = WPS_CONTINUE;
2854 
2855 	wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2856 
2857 	if (wps_parse_msg(msg, &attr) < 0)
2858 		return WPS_FAILURE;
2859 
2860 	if (attr.msg_type == NULL) {
2861 		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2862 		wps->state = SEND_WSC_NACK;
2863 		return WPS_CONTINUE;
2864 	}
2865 
2866 	if (*attr.msg_type != WPS_M1 &&
2867 	    (attr.registrar_nonce == NULL ||
2868 	     os_memcmp(wps->nonce_r, attr.registrar_nonce,
2869 		       WPS_NONCE_LEN) != 0)) {
2870 		wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2871 		return WPS_FAILURE;
2872 	}
2873 
2874 	switch (*attr.msg_type) {
2875 	case WPS_M1:
2876 		if (wps_validate_m1(msg) < 0)
2877 			return WPS_FAILURE;
2878 #ifdef CONFIG_WPS_UPNP
2879 		if (wps->wps->wps_upnp && attr.mac_addr) {
2880 			/* Remove old pending messages when starting new run */
2881 			wps_free_pending_msgs(wps->wps->upnp_msgs);
2882 			wps->wps->upnp_msgs = NULL;
2883 
2884 			upnp_wps_device_send_wlan_event(
2885 				wps->wps->wps_upnp, attr.mac_addr,
2886 				UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2887 		}
2888 #endif /* CONFIG_WPS_UPNP */
2889 		ret = wps_process_m1(wps, &attr);
2890 		break;
2891 	case WPS_M3:
2892 		if (wps_validate_m3(msg) < 0)
2893 			return WPS_FAILURE;
2894 		ret = wps_process_m3(wps, msg, &attr);
2895 		if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2896 			wps_fail_event(wps->wps, WPS_M3, wps->config_error,
2897 				       wps->error_indication);
2898 		break;
2899 	case WPS_M5:
2900 		if (wps_validate_m5(msg) < 0)
2901 			return WPS_FAILURE;
2902 		ret = wps_process_m5(wps, msg, &attr);
2903 		if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2904 			wps_fail_event(wps->wps, WPS_M5, wps->config_error,
2905 				       wps->error_indication);
2906 		break;
2907 	case WPS_M7:
2908 		if (wps_validate_m7(msg) < 0)
2909 			return WPS_FAILURE;
2910 		ret = wps_process_m7(wps, msg, &attr);
2911 		if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2912 			wps_fail_event(wps->wps, WPS_M7, wps->config_error,
2913 				       wps->error_indication);
2914 		break;
2915 	default:
2916 		wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2917 			   *attr.msg_type);
2918 		return WPS_FAILURE;
2919 	}
2920 
2921 	if (ret == WPS_CONTINUE) {
2922 		/* Save a copy of the last message for Authenticator derivation
2923 		 */
2924 		wpabuf_free(wps->last_msg);
2925 		wps->last_msg = wpabuf_dup(msg);
2926 	}
2927 
2928 	return ret;
2929 }
2930 
2931 
2932 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2933 						const struct wpabuf *msg)
2934 {
2935 	struct wps_parse_attr attr;
2936 
2937 	wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2938 
2939 	if (wps_parse_msg(msg, &attr) < 0)
2940 		return WPS_FAILURE;
2941 
2942 	if (attr.msg_type == NULL) {
2943 		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2944 		return WPS_FAILURE;
2945 	}
2946 
2947 	if (*attr.msg_type != WPS_WSC_ACK) {
2948 		wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2949 			   *attr.msg_type);
2950 		return WPS_FAILURE;
2951 	}
2952 
2953 #ifdef CONFIG_WPS_UPNP
2954 	if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2955 	    upnp_wps_subscribers(wps->wps->wps_upnp)) {
2956 		if (wps->wps->upnp_msgs)
2957 			return WPS_CONTINUE;
2958 		wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2959 			   "external Registrar");
2960 		return WPS_PENDING;
2961 	}
2962 #endif /* CONFIG_WPS_UPNP */
2963 
2964 	if (attr.registrar_nonce == NULL ||
2965 	    os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
2966 	{
2967 		wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2968 		return WPS_FAILURE;
2969 	}
2970 
2971 	if (attr.enrollee_nonce == NULL ||
2972 	    os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
2973 		wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2974 		return WPS_FAILURE;
2975 	}
2976 
2977 	if (wps->state == RECV_M2D_ACK) {
2978 #ifdef CONFIG_WPS_UPNP
2979 		if (wps->wps->wps_upnp &&
2980 		    upnp_wps_subscribers(wps->wps->wps_upnp)) {
2981 			if (wps->wps->upnp_msgs)
2982 				return WPS_CONTINUE;
2983 			if (wps->ext_reg == 0)
2984 				wps->ext_reg = 1;
2985 			wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2986 				   "external Registrar");
2987 			return WPS_PENDING;
2988 		}
2989 #endif /* CONFIG_WPS_UPNP */
2990 
2991 		wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2992 			   "terminate negotiation");
2993 	}
2994 
2995 	return WPS_FAILURE;
2996 }
2997 
2998 
2999 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
3000 						 const struct wpabuf *msg)
3001 {
3002 	struct wps_parse_attr attr;
3003 	int old_state;
3004 	u16 config_error;
3005 
3006 	wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
3007 
3008 	old_state = wps->state;
3009 	wps->state = SEND_WSC_NACK;
3010 
3011 	if (wps_parse_msg(msg, &attr) < 0)
3012 		return WPS_FAILURE;
3013 
3014 	if (attr.msg_type == NULL) {
3015 		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3016 		return WPS_FAILURE;
3017 	}
3018 
3019 	if (*attr.msg_type != WPS_WSC_NACK) {
3020 		wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3021 			   *attr.msg_type);
3022 		return WPS_FAILURE;
3023 	}
3024 
3025 #ifdef CONFIG_WPS_UPNP
3026 	if (wps->wps->wps_upnp && wps->ext_reg) {
3027 		wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3028 			   "Registrar terminated by the Enrollee");
3029 		return WPS_FAILURE;
3030 	}
3031 #endif /* CONFIG_WPS_UPNP */
3032 
3033 	if (attr.registrar_nonce == NULL ||
3034 	    os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3035 	{
3036 		wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3037 		return WPS_FAILURE;
3038 	}
3039 
3040 	if (attr.enrollee_nonce == NULL ||
3041 	    os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3042 		wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3043 		return WPS_FAILURE;
3044 	}
3045 
3046 	if (attr.config_error == NULL) {
3047 		wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
3048 			   "in WSC_NACK");
3049 		return WPS_FAILURE;
3050 	}
3051 
3052 	config_error = WPA_GET_BE16(attr.config_error);
3053 	wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
3054 		   "Configuration Error %d", config_error);
3055 
3056 	switch (old_state) {
3057 	case RECV_M3:
3058 		wps_fail_event(wps->wps, WPS_M2, config_error,
3059 			       wps->error_indication);
3060 		break;
3061 	case RECV_M5:
3062 		wps_fail_event(wps->wps, WPS_M4, config_error,
3063 			       wps->error_indication);
3064 		break;
3065 	case RECV_M7:
3066 		wps_fail_event(wps->wps, WPS_M6, config_error,
3067 			       wps->error_indication);
3068 		break;
3069 	case RECV_DONE:
3070 		wps_fail_event(wps->wps, WPS_M8, config_error,
3071 			       wps->error_indication);
3072 		break;
3073 	default:
3074 		break;
3075 	}
3076 
3077 	return WPS_FAILURE;
3078 }
3079 
3080 
3081 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
3082 						 const struct wpabuf *msg)
3083 {
3084 	struct wps_parse_attr attr;
3085 
3086 	wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
3087 
3088 	if (wps->state != RECV_DONE &&
3089 	    (!wps->wps->wps_upnp || !wps->ext_reg)) {
3090 		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
3091 			   "receiving WSC_Done", wps->state);
3092 		return WPS_FAILURE;
3093 	}
3094 
3095 	if (wps_parse_msg(msg, &attr) < 0)
3096 		return WPS_FAILURE;
3097 
3098 	if (attr.msg_type == NULL) {
3099 		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3100 		return WPS_FAILURE;
3101 	}
3102 
3103 	if (*attr.msg_type != WPS_WSC_DONE) {
3104 		wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3105 			   *attr.msg_type);
3106 		return WPS_FAILURE;
3107 	}
3108 
3109 #ifdef CONFIG_WPS_UPNP
3110 	if (wps->wps->wps_upnp && wps->ext_reg) {
3111 		wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3112 			   "Registrar completed successfully");
3113 		wps_device_store(wps->wps->registrar, &wps->peer_dev,
3114 				 wps->uuid_e);
3115 		return WPS_DONE;
3116 	}
3117 #endif /* CONFIG_WPS_UPNP */
3118 
3119 	if (attr.registrar_nonce == NULL ||
3120 	    os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3121 	{
3122 		wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3123 		return WPS_FAILURE;
3124 	}
3125 
3126 	if (attr.enrollee_nonce == NULL ||
3127 	    os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3128 		wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3129 		return WPS_FAILURE;
3130 	}
3131 
3132 	wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
3133 	wps_device_store(wps->wps->registrar, &wps->peer_dev,
3134 			 wps->uuid_e);
3135 
3136 	if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
3137 	    wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
3138 		struct wps_credential cred;
3139 
3140 		wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
3141 			   "on first Enrollee connection");
3142 
3143 		os_memset(&cred, 0, sizeof(cred));
3144 		os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
3145 		cred.ssid_len = wps->wps->ssid_len;
3146 		cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
3147 		cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
3148 		os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
3149 		cred.key_len = wps->new_psk_len;
3150 
3151 		wps->wps->wps_state = WPS_STATE_CONFIGURED;
3152 		wpa_hexdump_ascii_key(MSG_DEBUG,
3153 				      "WPS: Generated random passphrase",
3154 				      wps->new_psk, wps->new_psk_len);
3155 		if (wps->wps->cred_cb)
3156 			wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
3157 
3158 		os_free(wps->new_psk);
3159 		wps->new_psk = NULL;
3160 	}
3161 
3162 	if (!wps->wps->ap && !wps->er)
3163 		wps_sta_cred_cb(wps);
3164 
3165 	if (wps->new_psk) {
3166 		if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
3167 				   wps->new_psk, wps->new_psk_len)) {
3168 			wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
3169 				   "new PSK");
3170 		}
3171 		os_free(wps->new_psk);
3172 		wps->new_psk = NULL;
3173 	}
3174 
3175 	wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e,
3176 			   wps->dev_password, wps->dev_password_len);
3177 
3178 	if (wps->pbc) {
3179 		wps_registrar_remove_pbc_session(wps->wps->registrar,
3180 						 wps->uuid_e,
3181 						 wps->p2p_dev_addr);
3182 		wps_registrar_pbc_completed(wps->wps->registrar);
3183 		os_get_time(&wps->wps->registrar->pbc_ignore_start);
3184 		os_memcpy(wps->wps->registrar->pbc_ignore_uuid, wps->uuid_e,
3185 			  WPS_UUID_LEN);
3186 	} else {
3187 		wps_registrar_pin_completed(wps->wps->registrar);
3188 	}
3189 	/* TODO: maintain AuthorizedMACs somewhere separately for each ER and
3190 	 * merge them into APs own list.. */
3191 
3192 	wps_success_event(wps->wps);
3193 
3194 	return WPS_DONE;
3195 }
3196 
3197 
3198 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
3199 					       enum wsc_op_code op_code,
3200 					       const struct wpabuf *msg)
3201 {
3202 	enum wps_process_res ret;
3203 
3204 	wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
3205 		   "op_code=%d)",
3206 		   (unsigned long) wpabuf_len(msg), op_code);
3207 
3208 #ifdef CONFIG_WPS_UPNP
3209 	if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
3210 		struct wps_parse_attr attr;
3211 		if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
3212 		    *attr.msg_type == WPS_M3)
3213 			wps->ext_reg = 2; /* past M2/M2D phase */
3214 	}
3215 	if (wps->ext_reg > 1)
3216 		wps_registrar_free_pending_m2(wps->wps);
3217 	if (wps->wps->wps_upnp && wps->ext_reg &&
3218 	    wps->wps->upnp_msgs == NULL &&
3219 	    (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
3220 	{
3221 		struct wps_parse_attr attr;
3222 		int type;
3223 		if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
3224 			type = -1;
3225 		else
3226 			type = *attr.msg_type;
3227 		wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
3228 			   " to external Registrar for processing", type);
3229 		upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
3230 						wps->mac_addr_e,
3231 						UPNP_WPS_WLANEVENT_TYPE_EAP,
3232 						msg);
3233 		if (op_code == WSC_MSG)
3234 			return WPS_PENDING;
3235 	} else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
3236 		wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
3237 			   "external Registrar");
3238 		return WPS_CONTINUE;
3239 	}
3240 #endif /* CONFIG_WPS_UPNP */
3241 
3242 	switch (op_code) {
3243 	case WSC_MSG:
3244 		return wps_process_wsc_msg(wps, msg);
3245 	case WSC_ACK:
3246 		if (wps_validate_wsc_ack(msg) < 0)
3247 			return WPS_FAILURE;
3248 		return wps_process_wsc_ack(wps, msg);
3249 	case WSC_NACK:
3250 		if (wps_validate_wsc_nack(msg) < 0)
3251 			return WPS_FAILURE;
3252 		return wps_process_wsc_nack(wps, msg);
3253 	case WSC_Done:
3254 		if (wps_validate_wsc_done(msg) < 0)
3255 			return WPS_FAILURE;
3256 		ret = wps_process_wsc_done(wps, msg);
3257 		if (ret == WPS_FAILURE) {
3258 			wps->state = SEND_WSC_NACK;
3259 			wps_fail_event(wps->wps, WPS_WSC_DONE,
3260 				       wps->config_error,
3261 				       wps->error_indication);
3262 		}
3263 		return ret;
3264 	default:
3265 		wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
3266 		return WPS_FAILURE;
3267 	}
3268 }
3269 
3270 
3271 int wps_registrar_update_ie(struct wps_registrar *reg)
3272 {
3273 	return wps_set_ie(reg);
3274 }
3275 
3276 
3277 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
3278 					       void *timeout_ctx)
3279 {
3280 	struct wps_registrar *reg = eloop_ctx;
3281 
3282 	wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
3283 		   "unselect internal Registrar");
3284 	reg->selected_registrar = 0;
3285 	reg->pbc = 0;
3286 	wps_registrar_selected_registrar_changed(reg);
3287 }
3288 
3289 
3290 #ifdef CONFIG_WPS_UPNP
3291 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
3292 				      struct subscription *s)
3293 {
3294 	int i, j;
3295 	wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
3296 		   "config_methods=0x%x)",
3297 		   s->dev_password_id, s->config_methods);
3298 	reg->sel_reg_union = 1;
3299 	if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
3300 		reg->sel_reg_dev_password_id_override = s->dev_password_id;
3301 	if (reg->sel_reg_config_methods_override == -1)
3302 		reg->sel_reg_config_methods_override = 0;
3303 	reg->sel_reg_config_methods_override |= s->config_methods;
3304 	for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
3305 		if (is_zero_ether_addr(reg->authorized_macs_union[i]))
3306 			break;
3307 	for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
3308 	     j++) {
3309 		if (is_zero_ether_addr(s->authorized_macs[j]))
3310 			break;
3311 		wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC into union: "
3312 			   MACSTR, MAC2STR(s->authorized_macs[j]));
3313 		os_memcpy(reg->authorized_macs_union[i],
3314 			  s->authorized_macs[j], ETH_ALEN);
3315 		i++;
3316 	}
3317 	wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union",
3318 		    (u8 *) reg->authorized_macs_union,
3319 		    sizeof(reg->authorized_macs_union));
3320 }
3321 #endif /* CONFIG_WPS_UPNP */
3322 
3323 
3324 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
3325 {
3326 #ifdef CONFIG_WPS_UPNP
3327 	struct subscription *s;
3328 
3329 	if (reg->wps->wps_upnp == NULL)
3330 		return;
3331 
3332 	dl_list_for_each(s, &reg->wps->wps_upnp->subscriptions,
3333 			 struct subscription, list) {
3334 		struct subscr_addr *sa;
3335 		sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
3336 		if (sa) {
3337 			wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
3338 				   inet_ntoa(sa->saddr.sin_addr),
3339 				   ntohs(sa->saddr.sin_port));
3340 		}
3341 		if (s->selected_registrar)
3342 			wps_registrar_sel_reg_add(reg, s);
3343 		else
3344 			wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
3345 				   "selected");
3346 	}
3347 #endif /* CONFIG_WPS_UPNP */
3348 }
3349 
3350 
3351 /**
3352  * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3353  * @reg: Registrar data from wps_registrar_init()
3354  *
3355  * This function is called when selected registrar state changes, e.g., when an
3356  * AP receives a SetSelectedRegistrar UPnP message.
3357  */
3358 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg)
3359 {
3360 	wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
3361 
3362 	reg->sel_reg_union = reg->selected_registrar;
3363 	reg->sel_reg_dev_password_id_override = -1;
3364 	reg->sel_reg_config_methods_override = -1;
3365 	os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
3366 		  WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
3367 	wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union (start with own)",
3368 		    (u8 *) reg->authorized_macs_union,
3369 		    sizeof(reg->authorized_macs_union));
3370 	if (reg->selected_registrar) {
3371 		u16 methods;
3372 
3373 		methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
3374 #ifdef CONFIG_WPS2
3375 		methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
3376 			     WPS_CONFIG_PHY_PUSHBUTTON);
3377 #endif /* CONFIG_WPS2 */
3378 		if (reg->pbc) {
3379 			reg->sel_reg_dev_password_id_override =
3380 				DEV_PW_PUSHBUTTON;
3381 			wps_set_pushbutton(&methods, reg->wps->config_methods);
3382 		}
3383 		wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
3384 			   "(pbc=%d)", reg->pbc);
3385 		reg->sel_reg_config_methods_override = methods;
3386 	} else
3387 		wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
3388 
3389 	wps_registrar_sel_reg_union(reg);
3390 
3391 	wps_set_ie(reg);
3392 	wps_cb_set_sel_reg(reg);
3393 }
3394 
3395 
3396 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
3397 			   char *buf, size_t buflen)
3398 {
3399 	struct wps_registrar_device *d;
3400 	int len = 0, ret;
3401 	char uuid[40];
3402 	char devtype[WPS_DEV_TYPE_BUFSIZE];
3403 
3404 	d = wps_device_get(reg, addr);
3405 	if (d == NULL)
3406 		return 0;
3407 	if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
3408 		return 0;
3409 
3410 	ret = os_snprintf(buf + len, buflen - len,
3411 			  "wpsUuid=%s\n"
3412 			  "wpsPrimaryDeviceType=%s\n"
3413 			  "wpsDeviceName=%s\n"
3414 			  "wpsManufacturer=%s\n"
3415 			  "wpsModelName=%s\n"
3416 			  "wpsModelNumber=%s\n"
3417 			  "wpsSerialNumber=%s\n",
3418 			  uuid,
3419 			  wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
3420 					       sizeof(devtype)),
3421 			  d->dev.device_name ? d->dev.device_name : "",
3422 			  d->dev.manufacturer ? d->dev.manufacturer : "",
3423 			  d->dev.model_name ? d->dev.model_name : "",
3424 			  d->dev.model_number ? d->dev.model_number : "",
3425 			  d->dev.serial_number ? d->dev.serial_number : "");
3426 	if (ret < 0 || (size_t) ret >= buflen - len)
3427 		return len;
3428 	len += ret;
3429 
3430 	return len;
3431 }
3432 
3433 
3434 int wps_registrar_config_ap(struct wps_registrar *reg,
3435 			    struct wps_credential *cred)
3436 {
3437 #ifdef CONFIG_WPS2
3438 	printf("encr_type=0x%x\n", cred->encr_type);
3439 	if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
3440 				 WPS_ENCR_AES))) {
3441 		if (cred->encr_type & WPS_ENCR_WEP) {
3442 			wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
3443 				   "due to WEP configuration");
3444 			return -1;
3445 		}
3446 
3447 		wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
3448 			   "invalid encr_type 0x%x", cred->encr_type);
3449 		return -1;
3450 	}
3451 
3452 	if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
3453 	    WPS_ENCR_TKIP) {
3454 		wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
3455 			   "TKIP+AES");
3456 		cred->encr_type |= WPS_ENCR_AES;
3457 	}
3458 
3459 	if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
3460 	    WPS_AUTH_WPAPSK) {
3461 		wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
3462 			   "WPAPSK+WPA2PSK");
3463 		cred->auth_type |= WPS_AUTH_WPA2PSK;
3464 	}
3465 #endif /* CONFIG_WPS2 */
3466 
3467 	if (reg->wps->cred_cb)
3468 		return reg->wps->cred_cb(reg->wps->cb_ctx, cred);
3469 
3470 	return -1;
3471 }
3472 
3473 
3474 #ifdef CONFIG_WPS_NFC
3475 
3476 int wps_registrar_add_nfc_pw_token(struct wps_registrar *reg,
3477 				   const u8 *pubkey_hash, u16 pw_id,
3478 				   const u8 *dev_pw, size_t dev_pw_len)
3479 {
3480 	struct wps_nfc_pw_token *token;
3481 
3482 	if (dev_pw_len > WPS_OOB_DEVICE_PASSWORD_LEN)
3483 		return -1;
3484 
3485 	wps_free_nfc_pw_tokens(&reg->nfc_pw_tokens, pw_id);
3486 
3487 	token = os_zalloc(sizeof(*token));
3488 	if (token == NULL)
3489 		return -1;
3490 
3491 	os_memcpy(token->pubkey_hash, pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
3492 	token->pw_id = pw_id;
3493 	os_memcpy(token->dev_pw, dev_pw, dev_pw_len);
3494 	token->dev_pw_len = dev_pw_len;
3495 
3496 	dl_list_add(&reg->nfc_pw_tokens, &token->list);
3497 
3498 	reg->selected_registrar = 1;
3499 	reg->pbc = 0;
3500 	wps_registrar_add_authorized_mac(reg,
3501 					 (u8 *) "\xff\xff\xff\xff\xff\xff");
3502 	wps_registrar_selected_registrar_changed(reg);
3503 	eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
3504 	eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
3505 			       wps_registrar_set_selected_timeout,
3506 			       reg, NULL);
3507 
3508 	return 0;
3509 }
3510 
3511 
3512 int wps_registrar_add_nfc_password_token(struct wps_registrar *reg,
3513 					 const u8 *oob_dev_pw,
3514 					 size_t oob_dev_pw_len)
3515 {
3516 	const u8 *pos, *hash, *dev_pw;
3517 	u16 id;
3518 	size_t dev_pw_len;
3519 
3520 	if (oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2 +
3521 	    WPS_OOB_DEVICE_PASSWORD_MIN_LEN ||
3522 	    oob_dev_pw_len > WPS_OOB_PUBKEY_HASH_LEN + 2 +
3523 	    WPS_OOB_DEVICE_PASSWORD_LEN)
3524 		return -1;
3525 
3526 	hash = oob_dev_pw;
3527 	pos = oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN;
3528 	id = WPA_GET_BE16(pos);
3529 	dev_pw = pos + 2;
3530 	dev_pw_len = oob_dev_pw + oob_dev_pw_len - dev_pw;
3531 
3532 	wpa_printf(MSG_DEBUG, "WPS: Add NFC Password Token for Password ID %u",
3533 		   id);
3534 
3535 	wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
3536 		    hash, WPS_OOB_PUBKEY_HASH_LEN);
3537 	wpa_hexdump_key(MSG_DEBUG, "WPS: Device Password", dev_pw, dev_pw_len);
3538 
3539 	return wps_registrar_add_nfc_pw_token(reg, hash, id, dev_pw,
3540 					      dev_pw_len);
3541 }
3542 
3543 
3544 void wps_registrar_remove_nfc_pw_token(struct wps_registrar *reg,
3545 				       struct wps_nfc_pw_token *token)
3546 {
3547 	wps_registrar_remove_authorized_mac(reg,
3548 					    (u8 *) "\xff\xff\xff\xff\xff\xff");
3549 	wps_registrar_selected_registrar_changed(reg);
3550 }
3551 
3552 #endif /* CONFIG_WPS_NFC */
3553