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