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