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