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