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