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