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