wps_registrar.c revision 391c59f0632df8db1c325da1d31d479b2eedce45
18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/*
28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Wi-Fi Protected Setup - Registrar
3391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt * Copyright (c) 2008-2013, Jouni Malinen <j@w1.fi>
48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
5c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * This software may be distributed under the terms of the BSD license.
6c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * See README for more details.
78d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
88d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
98d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "utils/includes.h"
108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "utils/common.h"
128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "utils/base64.h"
138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "utils/eloop.h"
148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "utils/uuid.h"
158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "utils/list.h"
168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "crypto/crypto.h"
178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "crypto/sha256.h"
188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "crypto/random.h"
198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "common/ieee802_11_defs.h"
208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "wps_i.h"
218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "wps_dev_attr.h"
228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "wps_upnp.h"
238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "wps_upnp_i.h"
248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifndef CONFIG_WPS_STRICT
268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define WPS_WORKAROUNDS
278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_STRICT */
288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#ifdef CONFIG_WPS_NFC
3004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
3104949598a23f501be6eec21697465fd46a28840aDmitry Shmidtstruct wps_nfc_pw_token {
3204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	struct dl_list list;
3304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	u8 pubkey_hash[WPS_OOB_PUBKEY_HASH_LEN];
3404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	u16 pw_id;
3533e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	u8 dev_pw[WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1];
3604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	size_t dev_pw_len;
3704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt};
3804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
3904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
4004949598a23f501be6eec21697465fd46a28840aDmitry Shmidtstatic void wps_remove_nfc_pw_token(struct wps_nfc_pw_token *token)
4104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt{
4204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	dl_list_del(&token->list);
4304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	os_free(token);
4404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt}
4504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
4604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
4704949598a23f501be6eec21697465fd46a28840aDmitry Shmidtstatic void wps_free_nfc_pw_tokens(struct dl_list *tokens, u16 pw_id)
4804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt{
4904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	struct wps_nfc_pw_token *token, *prev;
5004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	dl_list_for_each_safe(token, prev, tokens, struct wps_nfc_pw_token,
5104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			      list) {
5204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		if (pw_id == 0 || pw_id == token->pw_id)
5304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			wps_remove_nfc_pw_token(token);
5404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	}
5504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt}
5604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
5704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
5804949598a23f501be6eec21697465fd46a28840aDmitry Shmidtstatic struct wps_nfc_pw_token * wps_get_nfc_pw_token(struct dl_list *tokens,
5904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt						      u16 pw_id)
6004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt{
6104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	struct wps_nfc_pw_token *token;
6204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	dl_list_for_each(token, tokens, struct wps_nfc_pw_token, list) {
6304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		if (pw_id == token->pw_id)
6404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			return token;
6504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	}
6604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	return NULL;
6704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt}
6804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
6904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#else /* CONFIG_WPS_NFC */
7004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
7104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#define wps_free_nfc_pw_tokens(t, p) do { } while (0)
7204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
7304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#endif /* CONFIG_WPS_NFC */
7404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
7504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wps_uuid_pin {
778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct dl_list list;
788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 uuid[WPS_UUID_LEN];
798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int wildcard_uuid;
808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *pin;
818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t pin_len;
828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define PIN_LOCKED BIT(0)
838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define PIN_EXPIRES BIT(1)
848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int flags;
858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct os_time expiration;
868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 enrollee_addr[ETH_ALEN];
878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_free_pin(struct wps_uuid_pin *pin)
918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(pin->pin);
938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(pin);
948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_remove_pin(struct wps_uuid_pin *pin)
988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dl_list_del(&pin->list);
1008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_free_pin(pin);
1018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_free_pins(struct dl_list *pins)
1058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_uuid_pin *pin, *prev;
1078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dl_list_for_each_safe(pin, prev, pins, struct wps_uuid_pin, list)
1088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_remove_pin(pin);
1098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wps_pbc_session {
1138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_pbc_session *next;
1148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 addr[ETH_ALEN];
1158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 uuid_e[WPS_UUID_LEN];
1168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct os_time timestamp;
1178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
1188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
1218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_pbc_session *prev;
1238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (pbc) {
1258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		prev = pbc;
1268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pbc = pbc->next;
1278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(prev);
1288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
1298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wps_registrar_device {
1338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_registrar_device *next;
1348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_device_data dev;
1358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 uuid[WPS_UUID_LEN];
1368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
1378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wps_registrar {
1408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_context *wps;
1418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int pbc;
1438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int selected_registrar;
1448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
145391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt	int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
146391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt			  const u8 *psk, size_t psk_len);
1478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
1488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 struct wpabuf *probe_resp_ie);
1498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
1508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			      const struct wps_device_data *dev);
1518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
15204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			       const u8 *uuid_e, const u8 *dev_pw,
15304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			       size_t dev_pw_len);
1548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
1558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       u16 sel_reg_config_methods);
1568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	void (*enrollee_seen_cb)(void *ctx, const u8 *addr, const u8 *uuid_e,
1578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 const u8 *pri_dev_type, u16 config_methods,
1588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 u16 dev_password_id, u8 request_type,
1598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 const char *dev_name);
1608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	void *cb_ctx;
1618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct dl_list pins;
16304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	struct dl_list nfc_pw_tokens;
1648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_pbc_session *pbc_sessions;
1658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int skip_cred_build;
1678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *extra_cred;
1688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int disable_auto_conf;
1698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int sel_reg_union;
1708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int sel_reg_dev_password_id_override;
1718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int sel_reg_config_methods_override;
1728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int static_wep_only;
1738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int dualband;
174391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt	int force_per_enrollee_psk;
1758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_registrar_device *devices;
1778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int force_pbc_overlap;
1798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
1818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 authorized_macs_union[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
1828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 p2p_dev_addr[ETH_ALEN];
184d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
185d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	u8 pbc_ignore_uuid[WPS_UUID_LEN];
186d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	struct os_time pbc_ignore_start;
1878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
1888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_set_ie(struct wps_registrar *reg);
1918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
1928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_set_selected_timeout(void *eloop_ctx,
1938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					       void *timeout_ctx);
194d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidtstatic void wps_registrar_remove_pin(struct wps_registrar *reg,
195d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt				     struct wps_uuid_pin *pin);
1968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_add_authorized_mac(struct wps_registrar *reg,
1998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					     const u8 *addr)
2008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int i;
2028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC " MACSTR,
2038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MAC2STR(addr));
2048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
2058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(reg->authorized_macs[i], addr, ETH_ALEN) == 0) {
2068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was "
2078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "already in the list");
2088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return; /* already in list */
2098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
2108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = WPS_MAX_AUTHORIZED_MACS - 1; i > 0; i--)
2118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i - 1],
2128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  ETH_ALEN);
2138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(reg->authorized_macs[0], addr, ETH_ALEN);
2148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
2158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
2168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_remove_authorized_mac(struct wps_registrar *reg,
2208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						const u8 *addr)
2218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int i;
2238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Remove authorized MAC " MACSTR,
2248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MAC2STR(addr));
2258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++) {
2268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(reg->authorized_macs, addr, ETH_ALEN) == 0)
2278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
2288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (i == WPS_MAX_AUTHORIZED_MACS) {
2308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was not in the "
2318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "list");
2328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return; /* not in the list */
2338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (; i + 1 < WPS_MAX_AUTHORIZED_MACS; i++)
2358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i + 1],
2368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  ETH_ALEN);
2378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(reg->authorized_macs[WPS_MAX_AUTHORIZED_MACS - 1], 0,
2388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		  ETH_ALEN);
2398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
2408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
2418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_free_devices(struct wps_registrar_device *dev)
2458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_registrar_device *prev;
2478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (dev) {
2498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		prev = dev;
2508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		dev = dev->next;
2518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_device_data_free(&prev->dev);
2528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(prev);
2538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
2588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						    const u8 *addr)
2598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_registrar_device *dev;
2618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (dev = reg->devices; dev; dev = dev->next) {
2638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
2648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return dev;
2658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NULL;
2678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_device_clone_data(struct wps_device_data *dst,
2718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  struct wps_device_data *src)
2728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
2748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
2758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#define WPS_STRDUP(n) \
2778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(dst->n); \
2788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dst->n = src->n ? os_strdup(src->n) : NULL
2798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPS_STRDUP(device_name);
2818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPS_STRDUP(manufacturer);
2828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPS_STRDUP(model_name);
2838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPS_STRDUP(model_number);
2848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	WPS_STRDUP(serial_number);
2858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#undef WPS_STRDUP
2868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_device_store(struct wps_registrar *reg,
2908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		     struct wps_device_data *dev, const u8 *uuid)
2918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_registrar_device *d;
2938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	d = wps_device_get(reg, dev->mac_addr);
2958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (d == NULL) {
2968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		d = os_zalloc(sizeof(*d));
2978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (d == NULL)
2988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
2998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		d->next = reg->devices;
3008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		reg->devices = d;
3018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_device_clone_data(&d->dev, dev);
3048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
3058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
3078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
3088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_add_pbc_session(struct wps_registrar *reg,
3118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  const u8 *addr, const u8 *uuid_e)
3128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
3138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_pbc_session *pbc, *prev = NULL;
3148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct os_time now;
3158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_get_time(&now);
3178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pbc = reg->pbc_sessions;
3198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (pbc) {
3208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
3218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
3228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (prev)
3238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				prev->next = pbc->next;
3248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			else
3258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				reg->pbc_sessions = pbc->next;
3268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
3278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
3288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		prev = pbc;
3298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pbc = pbc->next;
3308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!pbc) {
3338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pbc = os_zalloc(sizeof(*pbc));
3348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (pbc == NULL)
3358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return;
3368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(pbc->addr, addr, ETH_ALEN);
3378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (uuid_e)
3388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
3398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pbc->next = reg->pbc_sessions;
3428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->pbc_sessions = pbc;
3438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pbc->timestamp = now;
3448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* remove entries that have timed out */
3468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	prev = pbc;
3478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pbc = pbc->next;
3488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (pbc) {
3508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
3518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			prev->next = NULL;
3528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_free_pbc_sessions(pbc);
3538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
3548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
3558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		prev = pbc;
3568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pbc = pbc->next;
3578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
3598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
3621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					     const u8 *uuid_e,
3631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt					     const u8 *p2p_dev_addr)
3648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
3658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_pbc_session *pbc, *prev = NULL, *tmp;
3668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pbc = reg->pbc_sessions;
3688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (pbc) {
3691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0 ||
37098f9e76624da6bb96edc1982c423e4a119c5170aDmitry Shmidt#ifdef ANDROID_P2P
37198f9e76624da6bb96edc1982c423e4a119c5170aDmitry Shmidt		    (p2p_dev_addr && !is_zero_ether_addr(pbc->addr) &&
37298f9e76624da6bb96edc1982c423e4a119c5170aDmitry Shmidt		     os_memcmp(pbc->addr, p2p_dev_addr, ETH_ALEN) ==
37398f9e76624da6bb96edc1982c423e4a119c5170aDmitry Shmidt#else
3741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		    (p2p_dev_addr && !is_zero_ether_addr(reg->p2p_dev_addr) &&
3751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		     os_memcmp(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
37698f9e76624da6bb96edc1982c423e4a119c5170aDmitry Shmidt#endif
3771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		     0)) {
3788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (prev)
3798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				prev->next = pbc->next;
3808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			else
3818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				reg->pbc_sessions = pbc->next;
3828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			tmp = pbc;
3838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			pbc = pbc->next;
3848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: Removing PBC session for "
3858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "addr=" MACSTR, MAC2STR(tmp->addr));
3868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_hexdump(MSG_DEBUG, "WPS: Removed UUID-E",
3878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    tmp->uuid_e, WPS_UUID_LEN);
3888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(tmp);
3898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue;
3908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
3918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		prev = pbc;
3928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pbc = pbc->next;
3938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
3958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_registrar_pbc_overlap(struct wps_registrar *reg,
3988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			      const u8 *addr, const u8 *uuid_e)
3998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int count = 0;
4018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_pbc_session *pbc;
4028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_pbc_session *first = NULL;
4038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct os_time now;
4048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_get_time(&now);
4068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Checking active PBC sessions for overlap");
4088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (uuid_e) {
4108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Add one for the requested UUID");
4118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "WPS: Requested UUID",
4128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    uuid_e, WPS_UUID_LEN);
4138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		count++;
4148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
4158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
4178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Consider PBC session with " MACSTR,
4188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   MAC2STR(pbc->addr));
4198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "WPS: UUID-E",
4208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    pbc->uuid_e, WPS_UUID_LEN);
4218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
4228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: PBC walk time has "
4238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "expired");
4248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
4258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
4268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (first &&
4278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    os_memcmp(pbc->uuid_e, first->uuid_e, WPS_UUID_LEN) == 0) {
4288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: Same Enrollee");
4298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue; /* same Enrollee */
4308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
4318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (uuid_e == NULL ||
4328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN)) {
4338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: New Enrollee");
4348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			count++;
4358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
4368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (first == NULL)
4378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			first = pbc;
4388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
4398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: %u active PBC session(s) found", count);
4418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return count > 1 ? 1 : 0;
4438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
4448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
4478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * Wi-Fi Protected Setup State (%d)",
4498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   wps->wps_state);
4508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_WPS_STATE);
4518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, 1);
4528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_u8(msg, wps->wps_state);
4538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
4548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
4558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_UPNP
4588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_free_pending_m2(struct wps_context *wps)
4598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct upnp_pending_message *p, *p2, *prev = NULL;
4618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	p = wps->upnp_msgs;
4628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (p) {
4638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (p->type == WPS_M2 || p->type == WPS_M2D) {
4648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (prev == NULL)
4658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wps->upnp_msgs = p->next;
4668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			else
4678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				prev->next = p->next;
4688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
4698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			p2 = p;
4708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			p = p->next;
4718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpabuf_free(p2->msg);
4728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(p2);
4738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			continue;
4748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
4758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		prev = p;
4768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		p = p->next;
4778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
4788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
4798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_UPNP */
4808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_ap_setup_locked(struct wps_context *wps,
4838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     struct wpabuf *msg)
4848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->ap_setup_locked && wps->ap_setup_locked != 2) {
4868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS:  * AP Setup Locked");
4878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
4888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_put_be16(msg, 1);
4898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_put_u8(msg, 1);
4908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
4918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
4928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
4938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_selected_registrar(struct wps_registrar *reg,
4968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					struct wpabuf *msg)
4978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!reg->sel_reg_union)
4998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
5008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar");
5018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
5028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, 1);
5038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_u8(msg, 1);
5048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
5058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
5098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					     struct wpabuf *msg)
5108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
5128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!reg->sel_reg_union)
5138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
5148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->sel_reg_dev_password_id_override >= 0)
5158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		id = reg->sel_reg_dev_password_id_override;
5168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * Device Password ID (%d)", id);
5178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
5188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, 2);
5198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, id);
5208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
5218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_sel_pbc_reg_uuid_e(struct wps_registrar *reg,
5258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					struct wpabuf *msg)
5268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
5288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!reg->sel_reg_union)
5298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
5308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->sel_reg_dev_password_id_override >= 0)
5318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		id = reg->sel_reg_dev_password_id_override;
5328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (id != DEV_PW_PUSHBUTTON || !reg->dualband)
5338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
5348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wps_build_uuid_e(msg, reg->wps->uuid);
5358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_set_pushbutton(u16 *methods, u16 conf_methods)
5398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*methods |= WPS_CONFIG_PUSHBUTTON;
5418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS2
54204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if ((conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON) ==
54304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	    WPS_CONFIG_VIRT_PUSHBUTTON)
5448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
54504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if ((conf_methods & WPS_CONFIG_PHY_PUSHBUTTON) ==
54604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	    WPS_CONFIG_PHY_PUSHBUTTON)
5478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*methods |= WPS_CONFIG_PHY_PUSHBUTTON;
54804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if ((*methods & WPS_CONFIG_VIRT_PUSHBUTTON) !=
54904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	    WPS_CONFIG_VIRT_PUSHBUTTON &&
55004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	    (*methods & WPS_CONFIG_PHY_PUSHBUTTON) !=
55104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	    WPS_CONFIG_PHY_PUSHBUTTON) {
5528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
5538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Required to include virtual/physical flag, but we were not
5548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * configured with push button type, so have to default to one
5558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * of them.
5568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
5578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*methods |= WPS_CONFIG_PHY_PUSHBUTTON;
5588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS2 */
5608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
5648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					    struct wpabuf *msg)
5658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 methods;
5678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!reg->sel_reg_union)
5688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
5698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	methods = reg->wps->config_methods;
5708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	methods &= ~WPS_CONFIG_PUSHBUTTON;
5718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS2
5728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
5738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		     WPS_CONFIG_PHY_PUSHBUTTON);
5748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS2 */
5758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->pbc)
5768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_set_pushbutton(&methods, reg->wps->config_methods);
5778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->sel_reg_config_methods_override >= 0)
5788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		methods = reg->sel_reg_config_methods_override;
5798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar Config Methods (%x)",
5808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   methods);
5818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
5828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, 2);
5838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, methods);
5848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
5858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_probe_config_methods(struct wps_registrar *reg,
5898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  struct wpabuf *msg)
5908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 methods;
5928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
5938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * These are the methods that the AP supports as an Enrollee for adding
5948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * external Registrars.
5958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
5968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
5978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS2
5988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
5998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		     WPS_CONFIG_PHY_PUSHBUTTON);
6008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS2 */
6018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * Config Methods (%x)", methods);
6028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
6038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, 2);
6048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, methods);
6058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
6068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
6078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_config_methods_r(struct wps_registrar *reg,
6108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      struct wpabuf *msg)
6118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
6121f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return wps_build_config_methods(msg, reg->wps->config_methods);
6138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
6148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtconst u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count)
6178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
6188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*count = 0;
6198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS2
6218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (*count < WPS_MAX_AUTHORIZED_MACS) {
6228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
6238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
6248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		(*count)++;
6258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
6268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS2 */
6278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return (const u8 *) reg->authorized_macs_union;
6298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
6308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
6338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_registrar_init - Initialize WPS Registrar data
6348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @wps: Pointer to longterm WPS context
6358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @cfg: Registrar configuration
6368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: Pointer to allocated Registrar data or %NULL on failure
6378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
6388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function is used to initialize WPS Registrar functionality. It can be
6398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * used for a single Registrar run (e.g., when run in a supplicant) or multiple
6408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * runs (e.g., when run as an internal Registrar in an AP). Caller is
6418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * responsible for freeing the returned data with wps_registrar_deinit() when
6428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Registrar functionality is not needed anymore.
6438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
6448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wps_registrar *
6458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtwps_registrar_init(struct wps_context *wps,
6468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   const struct wps_registrar_config *cfg)
6478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
6488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_registrar *reg = os_zalloc(sizeof(*reg));
6498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg == NULL)
6508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
6518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dl_list_init(&reg->pins);
65304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	dl_list_init(&reg->nfc_pw_tokens);
6548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->wps = wps;
6558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->new_psk_cb = cfg->new_psk_cb;
6568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->set_ie_cb = cfg->set_ie_cb;
6578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->pin_needed_cb = cfg->pin_needed_cb;
6588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->reg_success_cb = cfg->reg_success_cb;
6598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
6608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
6618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->cb_ctx = cfg->cb_ctx;
6628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->skip_cred_build = cfg->skip_cred_build;
6638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cfg->extra_cred) {
6648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
6658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						    cfg->extra_cred_len);
6668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (reg->extra_cred == NULL) {
6678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(reg);
6688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return NULL;
6698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
6708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
6718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->disable_auto_conf = cfg->disable_auto_conf;
6728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->sel_reg_dev_password_id_override = -1;
6738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->sel_reg_config_methods_override = -1;
6748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->static_wep_only = cfg->static_wep_only;
6758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->dualband = cfg->dualband;
676391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt	reg->force_per_enrollee_psk = cfg->force_per_enrollee_psk;
6778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_set_ie(reg)) {
6798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_deinit(reg);
6808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
6818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
6828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return reg;
6848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
6858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
6878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
6888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_registrar_deinit - Deinitialize WPS Registrar data
6898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @reg: Registrar data from wps_registrar_init()
6908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
6918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wps_registrar_deinit(struct wps_registrar *reg)
6928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
6938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg == NULL)
6948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
6958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
6968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
6978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_free_pins(&reg->pins);
69804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	wps_free_nfc_pw_tokens(&reg->nfc_pw_tokens, 0);
6998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_free_pbc_sessions(reg->pbc_sessions);
7008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_free(reg->extra_cred);
7018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_free_devices(reg->devices);
7028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(reg);
7038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
7048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
706d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidtstatic void wps_registrar_invalidate_unused(struct wps_registrar *reg)
707d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt{
708d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	struct wps_uuid_pin *pin;
709d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
710d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
711d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		if (pin->wildcard_uuid == 1 && !(pin->flags & PIN_LOCKED)) {
712d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: Invalidate previously "
713d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt				   "configured wildcard PIN");
714d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			wps_registrar_remove_pin(reg, pin);
715d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			break;
716d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		}
717d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	}
718d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt}
719d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
720d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
7218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
7228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_registrar_add_pin - Configure a new PIN for Registrar
7238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @reg: Registrar data from wps_registrar_init()
7248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @addr: Enrollee MAC address or %NULL if not known
7258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @uuid: UUID-E or %NULL for wildcard (any UUID)
7268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @pin: PIN (Device Password)
7278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @pin_len: Length of pin in octets
7288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
7298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 0 on success, -1 on failure
7308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
7318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
7328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  const u8 *uuid, const u8 *pin, size_t pin_len,
7338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  int timeout)
7348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
7358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_uuid_pin *p;
7368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	p = os_zalloc(sizeof(*p));
7388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (p == NULL)
7398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
7408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (addr)
7418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
7428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (uuid == NULL)
7438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		p->wildcard_uuid = 1;
7448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
7458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
7468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	p->pin = os_malloc(pin_len);
7478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (p->pin == NULL) {
7488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(p);
7498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
7508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
7518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(p->pin, pin, pin_len);
7528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	p->pin_len = pin_len;
7538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (timeout) {
7558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		p->flags |= PIN_EXPIRES;
7568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_get_time(&p->expiration);
7578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		p->expiration.sec += timeout;
7588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
7598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
760d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	if (p->wildcard_uuid)
761d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wps_registrar_invalidate_unused(reg);
762d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
7638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dl_list_add(&reg->pins, &p->list);
7648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
7668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   timeout);
7678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
7688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
7698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->selected_registrar = 1;
7708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->pbc = 0;
7718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (addr)
7728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_add_authorized_mac(reg, addr);
7738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
7748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_add_authorized_mac(
7758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
7764b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt	wps_registrar_selected_registrar_changed(reg, 0);
7778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
7788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
7798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       wps_registrar_set_selected_timeout,
7808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       reg, NULL);
7818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
7838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
7848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_remove_pin(struct wps_registrar *reg,
7878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     struct wps_uuid_pin *pin)
7888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
7898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *addr;
7908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
7928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (is_zero_ether_addr(pin->enrollee_addr))
7938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		addr = bcast;
7948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
7958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		addr = pin->enrollee_addr;
7968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_registrar_remove_authorized_mac(reg, addr);
7978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_remove_pin(pin);
7984b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt	wps_registrar_selected_registrar_changed(reg, 0);
7998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
8008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_expire_pins(struct wps_registrar *reg)
8038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
8048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_uuid_pin *pin, *prev;
8058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct os_time now;
8068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_get_time(&now);
8088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
8098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	{
8108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if ((pin->flags & PIN_EXPIRES) &&
8118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    os_time_before(&pin->expiration, &now)) {
8128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
8138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    pin->uuid, WPS_UUID_LEN);
8148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_registrar_remove_pin(reg, pin);
8158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
8168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
8178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
8188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
8218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
8228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @reg: Registrar data from wps_registrar_init()
82304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt * @dev_pw: PIN to search for or %NULL to match any
82404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt * @dev_pw_len: Length of dev_pw in octets
8258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 0 on success, -1 if not wildcard PIN is enabled
8268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
82704949598a23f501be6eec21697465fd46a28840aDmitry Shmidtstatic int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg,
82804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt						 const u8 *dev_pw,
82904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt						 size_t dev_pw_len)
8308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
8318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_uuid_pin *pin, *prev;
8328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
8348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	{
83504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		if (dev_pw && pin->pin &&
83604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		    (dev_pw_len != pin->pin_len ||
83704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		     os_memcmp(dev_pw, pin->pin, dev_pw_len) != 0))
83804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			continue; /* different PIN */
8398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (pin->wildcard_uuid) {
8408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
8418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    pin->uuid, WPS_UUID_LEN);
8428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_registrar_remove_pin(reg, pin);
8438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return 0;
8448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
8458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
8468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
8488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
8498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
8528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
8538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @reg: Registrar data from wps_registrar_init()
8548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @uuid: UUID-E
8558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 0 on success, -1 on failure (e.g., PIN not found)
8568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
8578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
8588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
8598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_uuid_pin *pin, *prev;
8608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
8628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	{
8638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
8648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
8658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    pin->uuid, WPS_UUID_LEN);
8668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_registrar_remove_pin(reg, pin);
8678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return 0;
8688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
8698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
8708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
8728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
8738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
8768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					const u8 *uuid, size_t *pin_len)
8778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
8788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_uuid_pin *pin, *found = NULL;
8798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_registrar_expire_pins(reg);
8818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
8838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (!pin->wildcard_uuid &&
8848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
8858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			found = pin;
8868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
8878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
8888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
8898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
8908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!found) {
8918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Check for wildcard UUIDs since none of the UUID-specific
8928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * PINs matched */
8938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
894c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt			if (pin->wildcard_uuid == 1 ||
895c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt			    pin->wildcard_uuid == 2) {
8968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
8978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   "PIN. Assigned it for this UUID-E");
898c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt				pin->wildcard_uuid++;
8998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
9008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				found = pin;
9018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				break;
9028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
9038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
9048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
9058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!found)
9078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
9088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
9108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
9118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * that could otherwise avoid PIN invalidations.
9128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
9138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (found->flags & PIN_LOCKED) {
9148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
9158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "allow concurrent re-use");
9168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
9178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
9188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	*pin_len = found->pin_len;
9198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	found->flags |= PIN_LOCKED;
9208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return found->pin;
9218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
9228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
9258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
9268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @reg: Registrar data from wps_registrar_init()
9278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @uuid: UUID-E
9288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 0 on success, -1 on failure
9298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
9308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * PINs are locked to enforce only one concurrent use. This function unlocks a
9318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * PIN to allow it to be used again. If the specified PIN was configured using
9328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * a wildcard UUID, it will be removed instead of allowing multiple uses.
9338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
9348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
9358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
9368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_uuid_pin *pin;
9378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
9398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
940c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt			if (pin->wildcard_uuid == 3) {
9418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
9428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   "wildcard PIN");
9438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				return wps_registrar_invalidate_pin(reg, uuid);
9448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
9458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			pin->flags &= ~PIN_LOCKED;
9468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return 0;
9478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
9488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
9498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
9518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
9528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_stop_pbc(struct wps_registrar *reg)
9558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
9568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->selected_registrar = 0;
9578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->pbc = 0;
9588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
9598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_registrar_remove_authorized_mac(reg,
9608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					    (u8 *) "\xff\xff\xff\xff\xff\xff");
9614b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt	wps_registrar_selected_registrar_changed(reg, 0);
9628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
9638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
9668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
9678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_registrar *reg = eloop_ctx;
9688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
9708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_pbc_timeout_event(reg->wps);
9718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_registrar_stop_pbc(reg);
9728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
9738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
9758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
9768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
9778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @reg: Registrar data from wps_registrar_init()
9788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
9798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *	indicates no such filtering
9808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 0 on success, -1 on failure, -2 on session overlap
9818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
9828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function is called on an AP when a push button is pushed to activate
9838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
9848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * or when a PBC registration is completed. If more than one Enrollee in active
9858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * PBC mode has been detected during the monitor time (previous 2 minutes), the
9861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * PBC mode is not activated and -2 is returned to indicate session overlap.
9871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * This is skipped if a specific Enrollee is selected.
9888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
9898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_registrar_button_pushed(struct wps_registrar *reg,
9908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				const u8 *p2p_dev_addr)
9918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
9928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (p2p_dev_addr == NULL &&
9938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_registrar_pbc_overlap(reg, NULL, NULL)) {
9948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
9958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "mode");
9968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_pbc_overlap_event(reg->wps);
9978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -2;
9988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
9998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
10008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->force_pbc_overlap = 0;
10018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->selected_registrar = 1;
10028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->pbc = 1;
10038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (p2p_dev_addr)
10048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
10058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
10068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
10078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_registrar_add_authorized_mac(reg,
10088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					 (u8 *) "\xff\xff\xff\xff\xff\xff");
10094b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt	wps_registrar_selected_registrar_changed(reg, 0);
10108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1011b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	wps_pbc_active_event(reg->wps);
10128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
10138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
10148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
10158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       reg, NULL);
10168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
10178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
10188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_pbc_completed(struct wps_registrar *reg)
10218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
10228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
10238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
10248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_registrar_stop_pbc(reg);
1025b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	wps_pbc_disable_event(reg->wps);
10268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
10278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_pin_completed(struct wps_registrar *reg)
10308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
10318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
10328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
10338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->selected_registrar = 0;
10344b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt	wps_registrar_selected_registrar_changed(reg, 0);
10358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
10368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
103804949598a23f501be6eec21697465fd46a28840aDmitry Shmidtvoid wps_registrar_complete(struct wps_registrar *registrar, const u8 *uuid_e,
103904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			    const u8 *dev_pw, size_t dev_pw_len)
10401f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
10411f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (registrar->pbc) {
10421f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wps_registrar_remove_pbc_session(registrar,
10431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt						 uuid_e, NULL);
10441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wps_registrar_pbc_completed(registrar);
1045d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		os_get_time(&registrar->pbc_ignore_start);
1046d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		os_memcpy(registrar->pbc_ignore_uuid, uuid_e, WPS_UUID_LEN);
10471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	} else {
10481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wps_registrar_pin_completed(registrar);
10491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
105004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
105104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (dev_pw &&
105204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	    wps_registrar_invalidate_wildcard_pin(registrar, dev_pw,
105304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt						  dev_pw_len) == 0) {
105404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		wpa_hexdump_key(MSG_DEBUG, "WPS: Invalidated wildcard PIN",
105504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				dev_pw, dev_pw_len);
105604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	}
10571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
10581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
10591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
10608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_registrar_wps_cancel(struct wps_registrar *reg)
10618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
10628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->pbc) {
10638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
10648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_pbc_timeout(reg, NULL);
1065c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
10668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 1;
10678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else if (reg->selected_registrar) {
10688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* PIN Method */
10698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
10708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_pin_completed(reg);
107104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		wps_registrar_invalidate_wildcard_pin(reg, NULL, 0);
10728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 1;
10738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
10748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
10758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
10768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
10798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
10808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @reg: Registrar data from wps_registrar_init()
10818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @addr: MAC address of the Probe Request sender
10828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @wps_data: WPS IE contents
10838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
10848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function is called on an AP when a Probe Request with WPS IE is
10858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * received. This is used to track PBC mode use and to detect possible overlap
10868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * situation with other WPS APs.
10878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
10888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
10898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				const struct wpabuf *wps_data,
10908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				int p2p_wildcard)
10918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
10928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr attr;
1093d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	int skip_add = 0;
10948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump_buf(MSG_MSGDUMP,
10968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			"WPS: Probe Request with WPS data received",
10978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_data);
10988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
10998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_parse_msg(wps_data, &attr) < 0)
11008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
11018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.config_methods == NULL) {
11038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
11048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "Probe Request");
11058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
11068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
11078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.dev_password_id == NULL) {
11098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
11108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "in Probe Request");
11118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
11128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
11138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->enrollee_seen_cb && attr.uuid_e &&
11158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
11168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		char *dev_name = NULL;
11178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (attr.dev_name) {
11188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			dev_name = os_zalloc(attr.dev_name_len + 1);
11198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (dev_name) {
11208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				os_memcpy(dev_name, attr.dev_name,
11218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  attr.dev_name_len);
11228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
11238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
11248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
11258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      attr.primary_dev_type,
11268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      WPA_GET_BE16(attr.config_methods),
11278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      WPA_GET_BE16(attr.dev_password_id),
11288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      *attr.request_type, dev_name);
11298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(dev_name);
11308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
11318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
11338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return; /* Not PBC */
11348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
11368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MACSTR, MAC2STR(addr));
11378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.uuid_e == NULL) {
11388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
11398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "UUID-E included");
11408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
11418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
11428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: UUID-E from Probe Request", attr.uuid_e,
11438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    WPS_UUID_LEN);
11448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1145d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt#ifdef WPS_WORKAROUNDS
1146d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	if (reg->pbc_ignore_start.sec &&
1147d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	    os_memcmp(attr.uuid_e, reg->pbc_ignore_uuid, WPS_UUID_LEN) == 0) {
1148d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		struct os_time now, dur;
1149d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		os_get_time(&now);
1150d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		os_time_sub(&now, &reg->pbc_ignore_start, &dur);
1151d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		if (dur.sec >= 0 && dur.sec < 5) {
1152d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: Ignore PBC activation "
1153d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt				   "based on Probe Request from the Enrollee "
1154d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt				   "that just completed PBC provisioning");
1155d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			skip_add = 1;
1156d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		} else
1157d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			reg->pbc_ignore_start.sec = 0;
1158d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	}
1159d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt#endif /* WPS_WORKAROUNDS */
1160d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt
1161d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	if (!skip_add)
1162d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
11638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
11648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
11658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		reg->force_pbc_overlap = 1;
11668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_pbc_overlap_event(reg->wps);
11678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
11688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
11698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
1172391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt			  const u8 *p2p_dev_addr, const u8 *psk, size_t psk_len)
11738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
11748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->new_psk_cb == NULL)
11758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
11768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1177391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt	return reg->new_psk_cb(reg->cb_ctx, mac_addr, p2p_dev_addr, psk,
1178391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt			       psk_len);
11798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
11808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
11838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			      const struct wps_device_data *dev)
11848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
11858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->pin_needed_cb == NULL)
11868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
11878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
11898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
11908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
11928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
119304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			       const u8 *uuid_e, const u8 *dev_pw,
119404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			       size_t dev_pw_len)
11958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
11968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->reg_success_cb == NULL)
11978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
11988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
119904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e, dev_pw, dev_pw_len);
12008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
12018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
12048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 struct wpabuf *probe_resp_ie)
12058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
12068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
12078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
12088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_cb_set_sel_reg(struct wps_registrar *reg)
12118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
12128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 methods = 0;
12138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->set_sel_reg_cb == NULL)
12148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
12158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->selected_registrar) {
12178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
12188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS2
12198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
12208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     WPS_CONFIG_PHY_PUSHBUTTON);
12218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS2 */
12228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (reg->pbc)
12238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_set_pushbutton(&methods, reg->wps->config_methods);
12248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
12258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
12278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "config_methods=0x%x pbc=%d methods=0x%x",
12288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   reg->selected_registrar, reg->wps->config_methods,
12298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   reg->pbc, methods);
12308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
12328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
12338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    methods);
12348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
12358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_set_ie(struct wps_registrar *reg)
12388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
12398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *beacon;
12408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *probe;
12418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *auth_macs;
12428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t count;
12438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t vendor_len = 0;
12448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int i;
12458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->set_ie_cb == NULL)
12478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
12488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
12508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (reg->wps->dev.vendor_ext[i]) {
12518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			vendor_len += 2 + 2;
12528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			vendor_len += wpabuf_len(reg->wps->dev.vendor_ext[i]);
12538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
12548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
12558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	beacon = wpabuf_alloc(400 + vendor_len);
12578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (beacon == NULL)
12588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
12598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	probe = wpabuf_alloc(500 + vendor_len);
12608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (probe == NULL) {
12618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(beacon);
12628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
12638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
12648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	auth_macs = wps_authorized_macs(reg, &count);
12668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
12688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_version(beacon) ||
12708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_wps_state(reg->wps, beacon) ||
12718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_ap_setup_locked(reg->wps, beacon) ||
12728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_selected_registrar(reg, beacon) ||
12738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_sel_reg_dev_password_id(reg, beacon) ||
12748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_sel_reg_config_methods(reg, beacon) ||
12758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
1276b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	    (reg->dualband && wps_build_rf_bands(&reg->wps->dev, beacon, 0)) ||
12778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_wfa_ext(beacon, 0, auth_macs, count) ||
12788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_vendor_ext(&reg->wps->dev, beacon)) {
12798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(beacon);
12808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(probe);
12818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
12828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
12838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_P2P
12858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_dev_name(&reg->wps->dev, beacon) ||
12868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_primary_dev_type(&reg->wps->dev, beacon)) {
12878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(beacon);
12888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(probe);
12898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
12908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
12918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_P2P */
12928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
12948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
12958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_version(probe) ||
12968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_wps_state(reg->wps, probe) ||
12978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_ap_setup_locked(reg->wps, probe) ||
12988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_selected_registrar(reg, probe) ||
12998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_sel_reg_dev_password_id(reg, probe) ||
13008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_sel_reg_config_methods(reg, probe) ||
13018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
13028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				WPS_RESP_REGISTRAR) ||
13038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_uuid_e(probe, reg->wps->uuid) ||
13048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_device_attrs(&reg->wps->dev, probe) ||
13058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_probe_config_methods(reg, probe) ||
1306b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	    (reg->dualband && wps_build_rf_bands(&reg->wps->dev, probe, 0)) ||
13078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_wfa_ext(probe, 0, auth_macs, count) ||
13088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_vendor_ext(&reg->wps->dev, probe)) {
13098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(beacon);
13108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(probe);
13118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
13128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
13138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	beacon = wps_ie_encapsulate(beacon);
13158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	probe = wps_ie_encapsulate(probe);
13168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!beacon || !probe) {
13188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(beacon);
13198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(probe);
13208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
13218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
13228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->static_wep_only) {
13248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
13258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Windows XP and Vista clients can get confused about
13268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * EAP-Identity/Request when they probe the network with
13278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * EAPOL-Start. In such a case, they may assume the network is
13288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * using IEEE 802.1X and prompt user for a certificate while
13298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * the correct (non-WPS) behavior would be to ask for the
13308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * static WEP key. As a workaround, use Microsoft Provisioning
13318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * IE to advertise that legacy 802.1X is not supported.
13328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
13338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		const u8 ms_wps[7] = {
13348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			WLAN_EID_VENDOR_SPECIFIC, 5,
13358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			/* Microsoft Provisioning IE (00:50:f2:5) */
13368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			0x00, 0x50, 0xf2, 5,
13378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			0x00 /* no legacy 802.1X or MS WPS */
13388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		};
13398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
13408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "into Beacon/Probe Response frames");
13418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
13428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
13438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
13448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wps_cb_set_ie(reg, beacon, probe);
13468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
13478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_get_dev_password(struct wps_data *wps)
13508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
13518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *pin;
13528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t pin_len = 0;
13538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(wps->dev_password);
13558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->dev_password = NULL;
13568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->pbc) {
13588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
13598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pin = (const u8 *) "00000000";
13608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pin_len = 8;
136104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#ifdef CONFIG_WPS_NFC
136204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	} else if (wps->nfc_pw_token) {
136304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from NFC "
136404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			   "Password Token");
136504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		pin = wps->nfc_pw_token->dev_pw;
136604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		pin_len = wps->nfc_pw_token->dev_pw_len;
136704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#endif /* CONFIG_WPS_NFC */
13688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
13698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
13708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					    &pin_len);
1371f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		if (pin && wps->dev_pw_id >= 0x10) {
1372f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: No match for OOB Device "
1373f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt				   "Password ID, but PIN found");
1374f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			/*
1375f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			 * See whether Enrollee is willing to use PIN instead.
1376f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			 */
1377f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt			wps->dev_pw_id = DEV_PW_DEFAULT;
1378f86232838cf712377867cb42417c1613ab5dc425Dmitry Shmidt		}
13798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
13808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (pin == NULL) {
13818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
13828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "the Enrollee");
13838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
13848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  &wps->peer_dev);
13858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
13868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
13878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->dev_password = os_malloc(pin_len);
13898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->dev_password == NULL)
13908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
13918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(wps->dev_password, pin, pin_len);
13928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->dev_password_len = pin_len;
13938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
13958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
13968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
13988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
13998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
14008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * UUID-R");
14018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_UUID_R);
14028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, WPS_UUID_LEN);
14038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
14048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
14058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
14068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
14098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
14108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 *hash;
14118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *addr[4];
14128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t len[4];
14138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
14158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
14168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
14178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
14188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
14198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
14218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
14228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "R-Hash derivation");
14238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
14248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
14258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * R-Hash1");
14278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_R_HASH1);
14288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, SHA256_MAC_LEN);
14298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hash = wpabuf_put(msg, SHA256_MAC_LEN);
14308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
14318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[0] = wps->snonce;
14328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[0] = WPS_SECRET_NONCE_LEN;
14338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[1] = wps->psk1;
14348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[1] = WPS_PSK_LEN;
14358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[2] = wpabuf_head(wps->dh_pubkey_e);
14368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[2] = wpabuf_len(wps->dh_pubkey_e);
14378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[3] = wpabuf_head(wps->dh_pubkey_r);
14388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[3] = wpabuf_len(wps->dh_pubkey_r);
14398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
14408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
14418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * R-Hash2");
14438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_R_HASH2);
14448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, SHA256_MAC_LEN);
14458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hash = wpabuf_put(msg, SHA256_MAC_LEN);
14468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
14478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
14488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[1] = wps->psk2;
14498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
14508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
14518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
14538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
14548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
14578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
14588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce1");
14598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_R_SNONCE1);
14608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
14618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
14628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
14638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
14648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
14678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
14688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce2");
14698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_R_SNONCE2);
14708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
14718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
14728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			WPS_SECRET_NONCE_LEN);
14738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
14748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
14758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_cred_network_idx(struct wpabuf *msg,
14788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      const struct wps_credential *cred)
14798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
14808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * Network Index (1)");
14818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
14828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, 1);
14838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_u8(msg, 1);
14848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
14858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
14868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
14888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_cred_ssid(struct wpabuf *msg,
14898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			       const struct wps_credential *cred)
14908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
14918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * SSID");
14928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID for Credential",
14938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  cred->ssid, cred->ssid_len);
14948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_SSID);
14958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, cred->ssid_len);
14968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
14978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
14988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
14998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_cred_auth_type(struct wpabuf *msg,
15028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    const struct wps_credential *cred)
15038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
15048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type (0x%x)",
15058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   cred->auth_type);
15068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
15078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, 2);
15088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, cred->auth_type);
15098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
15108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
15118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_cred_encr_type(struct wpabuf *msg,
15148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    const struct wps_credential *cred)
15158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
15168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type (0x%x)",
15178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   cred->encr_type);
15188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
15198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, 2);
15208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, cred->encr_type);
15218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
15228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
15238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_cred_network_key(struct wpabuf *msg,
15268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      const struct wps_credential *cred)
15278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
15288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * Network Key (len=%d)",
15298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   (int) cred->key_len);
15308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
15318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			cred->key, cred->key_len);
15328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
15338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, cred->key_len);
15348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_data(msg, cred->key, cred->key_len);
15358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
15368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
15378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_credential(struct wpabuf *msg,
15408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				const struct wps_credential *cred)
15418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
15428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_cred_network_idx(msg, cred) ||
15438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_cred_ssid(msg, cred) ||
15448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_cred_auth_type(msg, cred) ||
15458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_cred_encr_type(msg, cred) ||
15468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_cred_network_key(msg, cred) ||
15474b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt	    wps_build_mac_addr(msg, cred->mac_addr))
15488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
15498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
15508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
15518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtint wps_build_credential_wrap(struct wpabuf *msg,
15541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			      const struct wps_credential *cred)
15551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
15561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpabuf *wbuf;
15571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wbuf = wpabuf_alloc(200);
15581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (wbuf == NULL)
15591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
15601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (wps_build_credential(wbuf, cred)) {
15611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpabuf_free(wbuf);
15621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return -1;
15631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
15641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpabuf_put_be16(msg, ATTR_CRED);
15651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpabuf_put_be16(msg, wpabuf_len(wbuf));
15661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpabuf_put_buf(msg, wbuf);
15671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpabuf_free(wbuf);
15681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return 0;
15691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
15701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
15711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
15728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
15738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
15748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *cred;
15758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->wps->registrar->skip_cred_build)
15778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto skip_cred_build;
15788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * Credential");
15808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->use_cred) {
15818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
15828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		goto use_provided;
15838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
15848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memset(&wps->cred, 0, sizeof(wps->cred));
15858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
15878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->cred.ssid_len = wps->wps->ssid_len;
15888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
15898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Select the best authentication and encryption type */
15908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->auth_type & WPS_AUTH_WPA2PSK)
15918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->auth_type = WPS_AUTH_WPA2PSK;
15928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else if (wps->auth_type & WPS_AUTH_WPAPSK)
15938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->auth_type = WPS_AUTH_WPAPSK;
15948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else if (wps->auth_type & WPS_AUTH_OPEN)
15958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->auth_type = WPS_AUTH_OPEN;
15968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else if (wps->auth_type & WPS_AUTH_SHARED)
15978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->auth_type = WPS_AUTH_SHARED;
15988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else {
15998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
16008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   wps->auth_type);
16018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
16028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
16038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->cred.auth_type = wps->auth_type;
16048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->auth_type == WPS_AUTH_WPA2PSK ||
16068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps->auth_type == WPS_AUTH_WPAPSK) {
16078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps->encr_type & WPS_ENCR_AES)
16088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->encr_type = WPS_ENCR_AES;
16098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		else if (wps->encr_type & WPS_ENCR_TKIP)
16108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->encr_type = WPS_ENCR_TKIP;
16118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		else {
16128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
16138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "type for WPA/WPA2");
16148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
16158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
16168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
16178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps->encr_type & WPS_ENCR_WEP)
16188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->encr_type = WPS_ENCR_WEP;
16198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		else if (wps->encr_type & WPS_ENCR_NONE)
16208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->encr_type = WPS_ENCR_NONE;
16218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		else {
16228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
16238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "type for non-WPA/WPA2 mode");
16248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
16258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
16268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
16278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->cred.encr_type = wps->encr_type;
16288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
16298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Set MAC address in the Credential to be the Enrollee's MAC address
16308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
16318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
16328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
16348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    !wps->wps->registrar->disable_auto_conf) {
16358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u8 r[16];
16368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Generate a random passphrase */
16378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (random_get_bytes(r, sizeof(r)) < 0)
16388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
16398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(wps->new_psk);
16408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
16418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps->new_psk == NULL)
16428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
16438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->new_psk_len--; /* remove newline */
16448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		while (wps->new_psk_len &&
16458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		       wps->new_psk[wps->new_psk_len - 1] == '=')
16468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->new_psk_len--;
16478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
16488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      wps->new_psk, wps->new_psk_len);
16498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
16508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->cred.key_len = wps->new_psk_len;
1651391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt	} else if (!wps->wps->registrar->force_per_enrollee_psk &&
1652391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt		   wps->use_psk_key && wps->wps->psk_set) {
16538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		char hex[65];
16548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
16558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
16568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(wps->cred.key, hex, 32 * 2);
16578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->cred.key_len = 32 * 2;
1658391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt	} else if (!wps->wps->registrar->force_per_enrollee_psk &&
1659391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt		   wps->wps->network_key) {
16608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(wps->cred.key, wps->wps->network_key,
16618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  wps->wps->network_key_len);
16628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->cred.key_len = wps->wps->network_key_len;
16638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
16648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		char hex[65];
16658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Generate a random per-device PSK */
16668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(wps->new_psk);
16678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->new_psk_len = 32;
16688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->new_psk = os_malloc(wps->new_psk_len);
16698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps->new_psk == NULL)
16708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
16718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
16728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(wps->new_psk);
16738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->new_psk = NULL;
16748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
16758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
16768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
16778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wps->new_psk, wps->new_psk_len);
16788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
16798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 wps->new_psk_len);
16808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
16818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->cred.key_len = wps->new_psk_len * 2;
16828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
16838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
16848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtuse_provided:
16858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_TESTING
16868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_testing_dummy_cred)
16878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cred = wpabuf_alloc(200);
16888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else
16898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cred = NULL;
16908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cred) {
16918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct wps_credential dummy;
16928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
16938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memset(&dummy, 0, sizeof(dummy));
16948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(dummy.ssid, "dummy", 5);
16958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		dummy.ssid_len = 5;
16968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		dummy.auth_type = WPS_AUTH_WPA2PSK;
16978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		dummy.encr_type = WPS_ENCR_AES;
16988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(dummy.key, "dummy psk", 9);
16998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		dummy.key_len = 9;
17008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
17018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_build_credential(cred, &dummy);
17028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
17038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_put_be16(msg, ATTR_CRED);
17058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_put_be16(msg, wpabuf_len(cred));
17068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_put_buf(msg, cred);
17078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(cred);
17098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
17108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_TESTING */
17118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	cred = wpabuf_alloc(200);
17138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (cred == NULL)
17148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
17158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_credential(cred, &wps->cred)) {
17178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(cred);
17188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
17198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
17208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, ATTR_CRED);
17228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_be16(msg, wpabuf_len(cred));
17238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_put_buf(msg, cred);
17248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_free(cred);
17258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtskip_cred_build:
17278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->wps->registrar->extra_cred) {
17288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS:  * Credential (pre-configured)");
17298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
17308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
17318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
17338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
17348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
17378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
17388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS:  * AP Settings");
17398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_credential(msg, &wps->cred))
17418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
17428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
17448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
17458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17471f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidtstatic struct wpabuf * wps_build_ap_cred(struct wps_data *wps)
17481f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
17491f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpabuf *msg, *plain;
17501f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
17511f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	msg = wpabuf_alloc(1000);
17521f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (msg == NULL)
17531f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NULL;
17541f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
17551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	plain = wpabuf_alloc(200);
17561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (plain == NULL) {
17571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpabuf_free(msg);
17581f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NULL;
17591f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
17601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
17611f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (wps_build_ap_settings(wps, plain)) {
17621f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpabuf_free(plain);
17631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpabuf_free(msg);
17641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		return NULL;
17651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
17661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
17671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpabuf_put_be16(msg, ATTR_CRED);
17681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpabuf_put_be16(msg, wpabuf_len(plain));
17691f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpabuf_put_buf(msg, plain);
17701f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpabuf_free(plain);
17711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
17721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	return msg;
17731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
17741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
17751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
17768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct wpabuf * wps_build_m2(struct wps_data *wps)
17778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
17788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *msg;
17798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
17818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
17828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
17838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    wps->nonce_r, WPS_NONCE_LEN);
17848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
17858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
17878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = wpabuf_alloc(1000);
17888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (msg == NULL)
17898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
17908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
17918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_version(msg) ||
17928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_msg_type(msg, WPS_M2) ||
17938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_enrollee_nonce(wps, msg) ||
17948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_registrar_nonce(wps, msg) ||
17958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_uuid_r(wps, msg) ||
17968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_public_key(wps, msg) ||
17978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_derive_keys(wps) ||
17988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_auth_type_flags(wps, msg) ||
17998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_encr_type_flags(wps, msg) ||
18008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_conn_type_flags(wps, msg) ||
18018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_config_methods_r(wps->wps->registrar, msg) ||
18028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_device_attrs(&wps->wps->dev, msg) ||
1803b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	    wps_build_rf_bands(&wps->wps->dev, msg,
1804b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			       wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
18058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_assoc_state(wps, msg) ||
18068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
18078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_dev_password_id(msg, wps->dev_pw_id) ||
18088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_os_version(&wps->wps->dev, msg) ||
18098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_wfa_ext(msg, 0, NULL, 0) ||
18108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_authenticator(wps, msg)) {
18118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(msg);
18128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
18138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->int_reg = 1;
18168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->state = RECV_M3;
18178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return msg;
18188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
18198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct wpabuf * wps_build_m2d(struct wps_data *wps)
18228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
18238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *msg;
18248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 err = wps->config_error;
18258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
18278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = wpabuf_alloc(1000);
18288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (msg == NULL)
18298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
18308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->wps->ap && wps->wps->ap_setup_locked &&
18328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    err == WPS_CFG_NO_ERROR)
18338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		err = WPS_CFG_SETUP_LOCKED;
18348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_version(msg) ||
18368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_msg_type(msg, WPS_M2D) ||
18378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_enrollee_nonce(wps, msg) ||
18388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_registrar_nonce(wps, msg) ||
18398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_uuid_r(wps, msg) ||
18408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_auth_type_flags(wps, msg) ||
18418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_encr_type_flags(wps, msg) ||
18428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_conn_type_flags(wps, msg) ||
18438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_config_methods_r(wps->wps->registrar, msg) ||
18448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_device_attrs(&wps->wps->dev, msg) ||
1845b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	    wps_build_rf_bands(&wps->wps->dev, msg,
1846b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			       wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
18478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_assoc_state(wps, msg) ||
18488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_config_error(msg, err) ||
18498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_os_version(&wps->wps->dev, msg) ||
18508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_wfa_ext(msg, 0, NULL, 0)) {
18518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(msg);
18528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
18538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->state = RECV_M2D_ACK;
18568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return msg;
18578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
18588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct wpabuf * wps_build_m4(struct wps_data *wps)
18618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
18628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *msg, *plain;
18638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
18658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
18678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	plain = wpabuf_alloc(200);
18698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (plain == NULL)
18708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
18718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = wpabuf_alloc(1000);
18738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (msg == NULL) {
18748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(plain);
18758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
18768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_version(msg) ||
18798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_msg_type(msg, WPS_M4) ||
18808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_enrollee_nonce(wps, msg) ||
18818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_r_hash(wps, msg) ||
18828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_r_snonce1(wps, plain) ||
18838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_key_wrap_auth(wps, plain) ||
18848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_encr_settings(wps, msg, plain) ||
18858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_wfa_ext(msg, 0, NULL, 0) ||
18868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_authenticator(wps, msg)) {
18878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(plain);
18888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(msg);
18898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
18908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
18918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_free(plain);
18928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->state = RECV_M5;
18948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return msg;
18958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
18968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
18988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct wpabuf * wps_build_m6(struct wps_data *wps)
18998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
19008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *msg, *plain;
19018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
19038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	plain = wpabuf_alloc(200);
19058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (plain == NULL)
19068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
19078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = wpabuf_alloc(1000);
19098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (msg == NULL) {
19108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(plain);
19118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
19128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_version(msg) ||
19158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_msg_type(msg, WPS_M6) ||
19168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_enrollee_nonce(wps, msg) ||
19178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_r_snonce2(wps, plain) ||
19188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_key_wrap_auth(wps, plain) ||
19198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_encr_settings(wps, msg, plain) ||
19208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_wfa_ext(msg, 0, NULL, 0) ||
19218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_authenticator(wps, msg)) {
19228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(plain);
19238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(msg);
19248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
19258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_free(plain);
19278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->wps_pin_revealed = 1;
19298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->state = RECV_M7;
19308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return msg;
19318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
19328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct wpabuf * wps_build_m8(struct wps_data *wps)
19358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
19368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *msg, *plain;
19378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
19398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	plain = wpabuf_alloc(500);
19418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (plain == NULL)
19428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
19438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	msg = wpabuf_alloc(1000);
19458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (msg == NULL) {
19468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(plain);
19478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
19488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_build_version(msg) ||
19518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_msg_type(msg, WPS_M8) ||
19528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_enrollee_nonce(wps, msg) ||
19538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
19548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
19558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_key_wrap_auth(wps, plain) ||
19568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_encr_settings(wps, msg, plain) ||
19578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_wfa_ext(msg, 0, NULL, 0) ||
19588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_build_authenticator(wps, msg)) {
19598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(plain);
19608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(msg);
19618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
19628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
19638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_free(plain);
19648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->state = RECV_DONE;
19668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return msg;
19678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
19688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
19718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      enum wsc_op_code *op_code)
19728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
19738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *msg;
19748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
19758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_UPNP
19768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!wps->int_reg && wps->wps->wps_upnp) {
19778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct upnp_pending_message *p, *prev = NULL;
19788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps->ext_reg > 1)
19798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_registrar_free_pending_m2(wps->wps);
19808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		p = wps->wps->upnp_msgs;
19818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* TODO: check pending message MAC address */
19828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		while (p && p->next) {
19838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			prev = p;
19848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			p = p->next;
19858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
19868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (p) {
19878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
19888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "UPnP");
19898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (prev)
19908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				prev->next = NULL;
19918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			else
19928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wps->wps->upnp_msgs = NULL;
19938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			msg = p->msg;
19948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			switch (p->type) {
19958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			case WPS_WSC_ACK:
19968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				*op_code = WSC_ACK;
19978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				break;
19988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			case WPS_WSC_NACK:
19998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				*op_code = WSC_NACK;
20008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				break;
20018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			default:
20028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				*op_code = WSC_MSG;
20038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				break;
20048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
20058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(p);
20068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (wps->ext_reg == 0)
20078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wps->ext_reg = 1;
20088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return msg;
20098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
20108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->ext_reg) {
20128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
20138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "pending message available");
20148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
20158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_UPNP */
20178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (wps->state) {
20198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case SEND_M2:
20208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps_get_dev_password(wps) < 0)
20218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			msg = wps_build_m2d(wps);
20228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		else
20238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			msg = wps_build_m2(wps);
20248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*op_code = WSC_MSG;
20258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
20268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case SEND_M2D:
20278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		msg = wps_build_m2d(wps);
20288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*op_code = WSC_MSG;
20298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
20308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case SEND_M4:
20318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		msg = wps_build_m4(wps);
20328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*op_code = WSC_MSG;
20338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
20348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case SEND_M6:
20358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		msg = wps_build_m6(wps);
20368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*op_code = WSC_MSG;
20378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
20388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case SEND_M8:
20398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		msg = wps_build_m8(wps);
20408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*op_code = WSC_MSG;
20418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
20428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case RECV_DONE:
20438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		msg = wps_build_wsc_ack(wps);
20448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*op_code = WSC_ACK;
20458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
20468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case SEND_WSC_NACK:
20478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		msg = wps_build_wsc_nack(wps);
20488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		*op_code = WSC_NACK;
20498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
20508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	default:
20518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
20528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "a message", wps->state);
20538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		msg = NULL;
20548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
20558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (*op_code == WSC_MSG && msg) {
20588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Save a copy of the last message for Authenticator derivation
20598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
20608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(wps->last_msg);
20618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->last_msg = wpabuf_dup(msg);
20628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return msg;
20658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
20668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
20698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
20708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (e_nonce == NULL) {
20718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
20728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
20738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
20768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
20778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    wps->nonce_e, WPS_NONCE_LEN);
20788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
20808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
20818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
20848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
20858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (r_nonce == NULL) {
20868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
20878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
20888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
20918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
20928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
20938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
20948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
20968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
20978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
20998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
21008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
21018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (uuid_e == NULL) {
21028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
21038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
21048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
21078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
21088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
21108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
21118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
21148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
21158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (pw_id == NULL) {
21168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
21178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
21188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->dev_pw_id = WPA_GET_BE16(pw_id);
21218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
21228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
21248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
21258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
21288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
21298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (e_hash1 == NULL) {
21308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
21318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
21328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
21358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
21368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
21388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
21398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
21428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
21438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (e_hash2 == NULL) {
21448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
21458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
21468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
21498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
21508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
21528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
21538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
21568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
21578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 hash[SHA256_MAC_LEN];
21588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *addr[4];
21598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t len[4];
21608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (e_snonce1 == NULL) {
21628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
21638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
21648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
21678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			WPS_SECRET_NONCE_LEN);
21688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
21708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[0] = e_snonce1;
21718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[0] = WPS_SECRET_NONCE_LEN;
21728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[1] = wps->psk1;
21738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[1] = WPS_PSK_LEN;
21748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[2] = wpabuf_head(wps->dh_pubkey_e);
21758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[2] = wpabuf_len(wps->dh_pubkey_e);
21768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[3] = wpabuf_head(wps->dh_pubkey_r);
21778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[3] = wpabuf_len(wps->dh_pubkey_r);
21788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
21798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
21818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
21828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "not match with the pre-committed value");
21838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2184b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		wps_pwd_auth_fail_event(wps->wps, 0, 1, wps->mac_addr_e);
21858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
21868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
21878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
21898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "half of the device password");
21908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
21928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
21938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
21958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
21968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
21978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 hash[SHA256_MAC_LEN];
21988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	const u8 *addr[4];
21998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t len[4];
22008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (e_snonce2 == NULL) {
22028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
22038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
22048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
22058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
22078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			WPS_SECRET_NONCE_LEN);
22088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
22108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[0] = e_snonce2;
22118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[0] = WPS_SECRET_NONCE_LEN;
22128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[1] = wps->psk2;
22138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[1] = WPS_PSK_LEN;
22148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[2] = wpabuf_head(wps->dh_pubkey_e);
22158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[2] = wpabuf_len(wps->dh_pubkey_e);
22168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	addr[3] = wpabuf_head(wps->dh_pubkey_r);
22178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len[3] = wpabuf_len(wps->dh_pubkey_r);
22188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
22198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
22218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
22228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "not match with the pre-committed value");
22238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
22248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2225b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt		wps_pwd_auth_fail_event(wps->wps, 0, 2, wps->mac_addr_e);
22268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
22278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
22288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
22308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "half of the device password");
22318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->wps_pin_revealed = 0;
22328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
22338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2234c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	/*
2235c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	 * In case wildcard PIN is used and WPS handshake succeeds in the first
2236c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	 * attempt, wps_registrar_unlock_pin() would not free the PIN, so make
2237c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	 * sure the PIN gets invalidated here.
2238c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	 */
2239c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2240c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt
22418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
22428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
22438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
22468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
22478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (mac_addr == NULL) {
22488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
22498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
22508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
22518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
22538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   MAC2STR(mac_addr));
22548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
22558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
22568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
22588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
22598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
22628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			      size_t pk_len)
22638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
22648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (pk == NULL || pk_len == 0) {
22658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
22668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
22678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
22688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_free(wps->dh_pubkey_e);
22708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
22718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->dh_pubkey_e == NULL)
22728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
22738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
22758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
22768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
22798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
22808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 auth_types;
22818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (auth == NULL) {
22838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
22848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "received");
22858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
22868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
22878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	auth_types = WPA_GET_BE16(auth);
22898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
22908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
22918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   auth_types);
22928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->auth_type = wps->wps->auth_types & auth_types;
22938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->auth_type == 0) {
22948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No match in supported "
22958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "authentication types (own 0x%x Enrollee 0x%x)",
22968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   wps->wps->auth_types, auth_types);
22978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef WPS_WORKAROUNDS
22988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
22998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Some deployed implementations seem to advertise incorrect
23008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * information in this attribute. For example, Linksys WRT350N
23018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * seems to have a byteorder bug that breaks this negotiation.
23028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * In order to interoperate with existing implementations,
23038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * assume that the Enrollee supports everything we do.
23048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
23058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
23068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "does not advertise supported authentication types "
23078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "correctly");
23088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->auth_type = wps->wps->auth_types;
23098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#else /* WPS_WORKAROUNDS */
23108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
23118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* WPS_WORKAROUNDS */
23128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
23158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
23168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
23198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
23208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 encr_types;
23218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (encr == NULL) {
23238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
23248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "received");
23258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
23268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	encr_types = WPA_GET_BE16(encr);
23298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
23318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   encr_types);
23328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->encr_type = wps->wps->encr_types & encr_types;
23338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->encr_type == 0) {
23348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No match in supported "
23358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "encryption types (own 0x%x Enrollee 0x%x)",
23368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   wps->wps->encr_types, encr_types);
23378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef WPS_WORKAROUNDS
23388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
23398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Some deployed implementations seem to advertise incorrect
23408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * information in this attribute. For example, Linksys WRT350N
23418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * seems to have a byteorder bug that breaks this negotiation.
23428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * In order to interoperate with existing implementations,
23438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * assume that the Enrollee supports everything we do.
23448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
23458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
23468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "does not advertise supported encryption types "
23478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "correctly");
23488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->encr_type = wps->wps->encr_types;
23498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#else /* WPS_WORKAROUNDS */
23508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
23518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* WPS_WORKAROUNDS */
23528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
23558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
23568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
23598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
23608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (conn == NULL) {
23618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
23628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "received");
23638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
23648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
23678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   *conn);
23688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
23708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
23718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
23748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
23758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 m;
23768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (methods == NULL) {
23788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
23798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
23808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
23818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	m = WPA_GET_BE16(methods);
23838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
23858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "%s%s%s%s%s%s%s%s%s", m,
23868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   m & WPS_CONFIG_USBA ? " [USBA]" : "",
23878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
23888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   m & WPS_CONFIG_LABEL ? " [Label]" : "",
23898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
23908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
23918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
23928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
23938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
23948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
23958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
23968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
23978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
23988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * The Enrollee does not have a display so it is unlikely to be
23998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * able to show the passphrase to a user and as such, could
24008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * benefit from receiving PSK to reduce key derivation time.
24018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
24028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
24038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "Enrollee not supporting display");
24048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->use_psk_key = 1;
24058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
24068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
24088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_wps_state(struct wps_data *wps, const u8 *state)
24128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (state == NULL) {
24148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
24158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "received");
24168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
24178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
24188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
24208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   *state);
24218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
24238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
24278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 a;
24298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (assoc == NULL) {
24318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Association State received");
24328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
24338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
24348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	a = WPA_GET_BE16(assoc);
24368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
24378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
24398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_config_error(struct wps_data *wps, const u8 *err)
24438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 e;
24458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (err == NULL) {
24478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
24488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
24498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
24508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	e = WPA_GET_BE16(err);
24528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
24538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
24558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
24598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_P2P
24618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_registrar *reg = wps->wps->registrar;
24628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (is_zero_ether_addr(reg->p2p_dev_addr))
24648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 1; /* no filtering in use */
24658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
24678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
24688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "filtering for PBC: expected " MACSTR " was "
24698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   MACSTR " - indicate PBC session overlap",
24708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   MAC2STR(reg->p2p_dev_addr),
24718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   MAC2STR(wps->p2p_dev_addr));
24728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
24738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
24748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_P2P */
24758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 1;
24768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_registrar_skip_overlap(struct wps_data *wps)
24808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
24818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_P2P
24828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_registrar *reg = wps->wps->registrar;
24838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (is_zero_ether_addr(reg->p2p_dev_addr))
24858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0; /* no specific Enrollee selected */
24868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) == 0) {
24888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Skip PBC overlap due to selected "
24898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "Enrollee match");
24908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 1;
24918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
24928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_P2P */
24938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
24948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
24958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
24978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic enum wps_process_res wps_process_m1(struct wps_data *wps,
24988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   struct wps_parse_attr *attr)
24998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
25008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Received M1");
25018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
25028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->state != RECV_M1) {
25038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
25048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "receiving M1", wps->state);
25058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
25068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
25078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
25088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_process_uuid_e(wps, attr->uuid_e) ||
25098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_mac_addr(wps, attr->mac_addr) ||
25108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
25118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
25128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
25138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
25148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
25158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_config_methods(wps, attr->config_methods) ||
25168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_wps_state(wps, attr->wps_state) ||
25178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_device_attrs(&wps->peer_dev, attr) ||
25188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
25198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_assoc_state(wps, attr->assoc_state) ||
25208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_dev_password_id(wps, attr->dev_password_id) ||
25218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_config_error(wps, attr->config_error) ||
25228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_os_version(&wps->peer_dev, attr->os_version))
25238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
25248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
25258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->dev_pw_id < 0x10 &&
25268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps->dev_pw_id != DEV_PW_DEFAULT &&
25278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
25288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
25298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
25308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
25318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	     !wps->wps->registrar->pbc)) {
25328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
25338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   wps->dev_pw_id);
25348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_M2D;
25358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
25368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
25378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
253804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#ifdef CONFIG_WPS_NFC
253904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (wps->dev_pw_id >= 0x10) {
254004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		struct wps_nfc_pw_token *token;
254104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		const u8 *addr[1];
254204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		u8 hash[WPS_HASH_LEN];
254304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
254404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		token = wps_get_nfc_pw_token(
254504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			&wps->wps->registrar->nfc_pw_tokens, wps->dev_pw_id);
254604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		if (token) {
254704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
254804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				   "Password Token");
254904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			dl_list_del(&token->list);
255004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			wps->nfc_pw_token = token;
255104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
255204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			addr[0] = attr->public_key;
255304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			sha256_vector(1, addr, &attr->public_key_len, hash);
255404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			if (os_memcmp(hash, wps->nfc_pw_token->pubkey_hash,
255504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				      WPS_OOB_PUBKEY_HASH_LEN) != 0) {
255604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				wpa_printf(MSG_ERROR, "WPS: Public Key hash "
255704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt					   "mismatch");
255804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				return WPS_FAILURE;
255904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			}
256004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		}
256104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	}
256204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#endif /* CONFIG_WPS_NFC */
256304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
25648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
25658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if ((wps->wps->registrar->force_pbc_overlap ||
25668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		     wps_registrar_pbc_overlap(wps->wps->registrar,
25678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					       wps->mac_addr_e, wps->uuid_e) ||
25688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		     !wps_registrar_p2p_dev_addr_match(wps)) &&
25698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    !wps_registrar_skip_overlap(wps)) {
25708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
25718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "negotiation");
25728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->state = SEND_M2D;
25738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
25748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_pbc_overlap_event(wps->wps);
25758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_fail_event(wps->wps, WPS_M1,
25768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       WPS_CFG_MULTIPLE_PBC_DETECTED,
2577b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt				       WPS_EI_NO_ERROR, wps->mac_addr_e);
25788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->wps->registrar->force_pbc_overlap = 1;
25798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return WPS_CONTINUE;
25808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
25818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_add_pbc_session(wps->wps->registrar,
25828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					      wps->mac_addr_e, wps->uuid_e);
25838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->pbc = 1;
25848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
25858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
25868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef WPS_WORKAROUNDS
25878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
25888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
25898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * passphrase format. To avoid interop issues, force PSK format to be
25908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * used.
25918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
25928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!wps->use_psk_key &&
25938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps->peer_dev.manufacturer &&
25948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
25958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps->peer_dev.model_name &&
25968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
25978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
25988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "PSK format");
25998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->use_psk_key = 1;
26008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
26018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* WPS_WORKAROUNDS */
26028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->state = SEND_M2;
26048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return WPS_CONTINUE;
26058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
26068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic enum wps_process_res wps_process_m3(struct wps_data *wps,
26098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   const struct wpabuf *msg,
26108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   struct wps_parse_attr *attr)
26118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
26128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Received M3");
26138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->state != RECV_M3) {
26158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
26168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "receiving M3", wps->state);
26178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
26188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
26198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
26208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
26228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    !wps_registrar_skip_overlap(wps)) {
26238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
26248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "session overlap");
26258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
26268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
26278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
26288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
26298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
26318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_authenticator(wps, attr->authenticator, msg) ||
26328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_e_hash1(wps, attr->e_hash1) ||
26338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_e_hash2(wps, attr->e_hash2)) {
26348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
26358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
26368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
26378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->state = SEND_M4;
26398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return WPS_CONTINUE;
26408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
26418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic enum wps_process_res wps_process_m5(struct wps_data *wps,
26448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   const struct wpabuf *msg,
26458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   struct wps_parse_attr *attr)
26468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
26478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *decrypted;
26488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr eattr;
26498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Received M5");
26518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->state != RECV_M5) {
26538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
26548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "receiving M5", wps->state);
26558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
26568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
26578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
26588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
26608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    !wps_registrar_skip_overlap(wps)) {
26618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
26628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "session overlap");
26638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
26648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
26658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
26668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
26678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
26698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_authenticator(wps, attr->authenticator, msg)) {
26708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
26718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
26728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
26738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
26758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					      attr->encr_settings_len);
26768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (decrypted == NULL) {
26778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
26788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "Settings attribute");
26798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
26808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
26818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
26828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_validate_m5_encr(decrypted, attr->version2 != NULL) < 0) {
26848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(decrypted);
26858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
26868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
26878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
26888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
26898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
26908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "attribute");
26918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_parse_msg(decrypted, &eattr) < 0 ||
26928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
26938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_e_snonce1(wps, eattr.e_snonce1)) {
26948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(decrypted);
26958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
26968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
26978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
26988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_free(decrypted);
26998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->state = SEND_M6;
27018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return WPS_CONTINUE;
27028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
27038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_sta_cred_cb(struct wps_data *wps)
27068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
27078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/*
27088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * Update credential to only include a single authentication and
27098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * encryption type in case the AP configuration includes more than one
27108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * option.
27118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 */
27128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
27138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->cred.auth_type = WPS_AUTH_WPA2PSK;
27148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
27158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->cred.auth_type = WPS_AUTH_WPAPSK;
27168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->cred.encr_type & WPS_ENCR_AES)
27178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->cred.encr_type = WPS_ENCR_AES;
27188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	else if (wps->cred.encr_type & WPS_ENCR_TKIP)
27198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->cred.encr_type = WPS_ENCR_TKIP;
27208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
27218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "AP configuration");
27228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->wps->cred_cb)
27238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
27248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
27258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_cred_update(struct wps_credential *dst,
27288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    struct wps_credential *src)
27298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
27308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
27318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dst->ssid_len = src->ssid_len;
27328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dst->auth_type = src->auth_type;
27338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dst->encr_type = src->encr_type;
27348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dst->key_idx = src->key_idx;
27358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(dst->key, src->key, sizeof(dst->key));
27368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dst->key_len = src->key_len;
27378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
27388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int wps_process_ap_settings_r(struct wps_data *wps,
27418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     struct wps_parse_attr *attr)
27428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
27431f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct wpabuf *msg;
27441f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
27458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->wps->ap || wps->er)
27468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
27478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* AP Settings Attributes in M7 when Enrollee is an AP */
27498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_process_ap_settings(attr, &wps->cred) < 0)
27508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
27518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
27538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->new_ap_settings) {
27558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
27568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "new settings");
27578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_cred_update(&wps->cred, wps->new_ap_settings);
27588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
27598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
27608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
27618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Use the AP PIN only to receive the current AP settings, not
27628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * to reconfigure the AP.
27638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
27648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/*
27668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * Clear selected registrar here since we do not get to
27678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 * WSC_Done in this protocol run.
27688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
27698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_pin_completed(wps->wps->registrar);
27708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27711f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		msg = wps_build_ap_cred(wps);
27721f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		if (msg == NULL)
27731f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			return -1;
27741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wps->cred.cred_attr = wpabuf_head(msg);
27751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wps->cred.cred_attr_len = wpabuf_len(msg);
27761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
27778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps->ap_settings_cb) {
27788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->ap_settings_cb(wps->ap_settings_cb_ctx,
27798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					    &wps->cred);
27801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpabuf_free(msg);
27818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return 1;
27828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
27838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_sta_cred_cb(wps);
27841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
27851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wps->cred.cred_attr = NULL;
27861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wps->cred.cred_attr_len = 0;
27871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		wpabuf_free(msg);
27881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
27898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 1;
27908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
27918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
27928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
27948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic enum wps_process_res wps_process_m7(struct wps_data *wps,
27958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   const struct wpabuf *msg,
27968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   struct wps_parse_attr *attr)
27978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
27988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpabuf *decrypted;
27998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr eattr;
28008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Received M7");
28028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->state != RECV_M7) {
28048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
28058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "receiving M7", wps->state);
28068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
28078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
28088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
28098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
28118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    !wps_registrar_skip_overlap(wps)) {
28128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
28138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "session overlap");
28148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
28158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
28168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
28178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
28188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
28208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_authenticator(wps, attr->authenticator, msg)) {
28218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
28228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
28238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
28248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
28268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					      attr->encr_settings_len);
28278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (decrypted == NULL) {
28288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
28298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "Settings attribute");
28308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
28318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
28328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
28338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_validate_m7_encr(decrypted, wps->wps->ap || wps->er,
28358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 attr->version2 != NULL) < 0) {
28368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(decrypted);
28378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
28388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
28398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
28408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
28428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "attribute");
28438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_parse_msg(decrypted, &eattr) < 0 ||
28448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
28458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_e_snonce2(wps, eattr.e_snonce2) ||
28468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps_process_ap_settings_r(wps, &eattr)) {
28478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(decrypted);
28488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
28498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
28508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
28518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpabuf_free(decrypted);
28538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->state = SEND_M8;
28558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return WPS_CONTINUE;
28568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
28578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
28608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						const struct wpabuf *msg)
28618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
28628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr attr;
28638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	enum wps_process_res ret = WPS_CONTINUE;
28648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
28668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_parse_msg(msg, &attr) < 0)
28688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
28698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.msg_type == NULL) {
28718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
28728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->state = SEND_WSC_NACK;
28738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
28748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
28758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (*attr.msg_type != WPS_M1 &&
28778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    (attr.registrar_nonce == NULL ||
28788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	     os_memcmp(wps->nonce_r, attr.registrar_nonce,
287961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		       WPS_NONCE_LEN) != 0)) {
28808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
28818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
28828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
28838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (*attr.msg_type) {
28858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WPS_M1:
28868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps_validate_m1(msg) < 0)
28878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return WPS_FAILURE;
28888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_UPNP
28898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps->wps->wps_upnp && attr.mac_addr) {
28908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			/* Remove old pending messages when starting new run */
28918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_free_pending_msgs(wps->wps->upnp_msgs);
28928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->wps->upnp_msgs = NULL;
28938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
28948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			upnp_wps_device_send_wlan_event(
28958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wps->wps->wps_upnp, attr.mac_addr,
28968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
28978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
28988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_UPNP */
28998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = wps_process_m1(wps, &attr);
29008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WPS_M3:
29028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps_validate_m3(msg) < 0)
29038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return WPS_FAILURE;
29048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = wps_process_m3(wps, msg, &attr);
29058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
29068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_fail_event(wps->wps, WPS_M3, wps->config_error,
2907b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt				       wps->error_indication, wps->mac_addr_e);
29088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WPS_M5:
29108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps_validate_m5(msg) < 0)
29118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return WPS_FAILURE;
29128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = wps_process_m5(wps, msg, &attr);
29138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
29148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_fail_event(wps->wps, WPS_M5, wps->config_error,
2915b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt				       wps->error_indication, wps->mac_addr_e);
29168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WPS_M7:
29188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps_validate_m7(msg) < 0)
29198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return WPS_FAILURE;
29208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = wps_process_m7(wps, msg, &attr);
29218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
29228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_fail_event(wps->wps, WPS_M7, wps->config_error,
2923b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt				       wps->error_indication, wps->mac_addr_e);
29248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
29258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	default:
29268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
29278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   *attr.msg_type);
29288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
29298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
29308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
29318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret == WPS_CONTINUE) {
29328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Save a copy of the last message for Authenticator derivation
29338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 */
29348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpabuf_free(wps->last_msg);
29358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->last_msg = wpabuf_dup(msg);
29368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
29378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
29388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ret;
29398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
29408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
29418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
29428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
29438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						const struct wpabuf *msg)
29448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
29458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr attr;
29468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
29478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
29488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
29498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_parse_msg(msg, &attr) < 0)
29508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
29518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
29528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.msg_type == NULL) {
29538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
29548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
29558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
29568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
29578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (*attr.msg_type != WPS_WSC_ACK) {
29588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
29598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   *attr.msg_type);
29608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
29618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
29628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
29638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_UPNP
29648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
29658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    upnp_wps_subscribers(wps->wps->wps_upnp)) {
29668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps->wps->upnp_msgs)
29678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return WPS_CONTINUE;
29688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
29698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "external Registrar");
29708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_PENDING;
29718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
29728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_UPNP */
29738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
29748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.registrar_nonce == NULL ||
297561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	    os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
29768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	{
29778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
29788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
29798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
29808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
29818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.enrollee_nonce == NULL ||
298261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	    os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
29838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
29848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
29858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
29868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
29878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->state == RECV_M2D_ACK) {
29888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_UPNP
29898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps->wps->wps_upnp &&
29908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    upnp_wps_subscribers(wps->wps->wps_upnp)) {
29918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (wps->wps->upnp_msgs)
29928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				return WPS_CONTINUE;
29938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (wps->ext_reg == 0)
29948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wps->ext_reg = 1;
29958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
29968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "external Registrar");
29978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return WPS_PENDING;
29988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
29998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_UPNP */
30008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
30028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "terminate negotiation");
30038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
30048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return WPS_FAILURE;
30068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
30078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
30108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						 const struct wpabuf *msg)
30118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
30128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr attr;
30138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int old_state;
30148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u16 config_error;
30158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
30178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	old_state = wps->state;
30198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps->state = SEND_WSC_NACK;
30208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_parse_msg(msg, &attr) < 0)
30228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
30238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.msg_type == NULL) {
30258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
30268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
30278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
30288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (*attr.msg_type != WPS_WSC_NACK) {
30308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
30318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   *attr.msg_type);
30328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
30338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
30348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_UPNP
30368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->wps->wps_upnp && wps->ext_reg) {
30378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
30388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "Registrar terminated by the Enrollee");
30398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
30408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
30418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_UPNP */
30428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.registrar_nonce == NULL ||
304461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	    os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
30458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	{
30468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
30478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
30488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
30498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.enrollee_nonce == NULL ||
305161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	    os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
30528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
30538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
30548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
30558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.config_error == NULL) {
30578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
30588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "in WSC_NACK");
30598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
30608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
30618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	config_error = WPA_GET_BE16(attr.config_error);
30638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
30648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "Configuration Error %d", config_error);
30658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (old_state) {
30678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case RECV_M3:
30688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_fail_event(wps->wps, WPS_M2, config_error,
3069b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			       wps->error_indication, wps->mac_addr_e);
30708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
30718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case RECV_M5:
30728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_fail_event(wps->wps, WPS_M4, config_error,
3073b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			       wps->error_indication, wps->mac_addr_e);
30748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
30758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case RECV_M7:
30768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_fail_event(wps->wps, WPS_M6, config_error,
3077b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			       wps->error_indication, wps->mac_addr_e);
30788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
30798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case RECV_DONE:
30808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_fail_event(wps->wps, WPS_M8, config_error,
3081b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt			       wps->error_indication, wps->mac_addr_e);
30828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
30838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	default:
30848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
30858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
30868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return WPS_FAILURE;
30888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
30898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
30928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						 const struct wpabuf *msg)
30938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
30948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_parse_attr attr;
30958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
30978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
30988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->state != RECV_DONE &&
30998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    (!wps->wps->wps_upnp || !wps->ext_reg)) {
31008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
31018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "receiving WSC_Done", wps->state);
31028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
31038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
31048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps_parse_msg(msg, &attr) < 0)
31068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
31078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.msg_type == NULL) {
31098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
31108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
31118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
31128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (*attr.msg_type != WPS_WSC_DONE) {
31148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
31158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   *attr.msg_type);
31168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
31178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
31188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_UPNP
31208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->wps->wps_upnp && wps->ext_reg) {
31218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
31228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "Registrar completed successfully");
31238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_device_store(wps->wps->registrar, &wps->peer_dev,
31248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 wps->uuid_e);
31258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_DONE;
31268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
31278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_UPNP */
31288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.registrar_nonce == NULL ||
313061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	    os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
31318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	{
31328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
31338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
31348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
31358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (attr.enrollee_nonce == NULL ||
313761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	    os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
31388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
31398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
31408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
31418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
31438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_device_store(wps->wps->registrar, &wps->peer_dev,
31448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 wps->uuid_e);
31458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
31478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
31488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct wps_credential cred;
31498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
31518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "on first Enrollee connection");
31528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memset(&cred, 0, sizeof(cred));
31548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
31558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cred.ssid_len = wps->wps->ssid_len;
31568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
31578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
31588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
31598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cred.key_len = wps->new_psk_len;
31608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->wps->wps_state = WPS_STATE_CONFIGURED;
31628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_hexdump_ascii_key(MSG_DEBUG,
31638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      "WPS: Generated random passphrase",
31648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      wps->new_psk, wps->new_psk_len);
31658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps->wps->cred_cb)
31668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
31678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(wps->new_psk);
31698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->new_psk = NULL;
31708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
31718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!wps->wps->ap && !wps->er)
31738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_sta_cred_cb(wps);
31748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->new_psk) {
31768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
3177391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt				   wps->p2p_dev_addr, wps->new_psk,
3178391c59f0632df8db1c325da1d31d479b2eedce45Dmitry Shmidt				   wps->new_psk_len)) {
31798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
31808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "new PSK");
31818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
31828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(wps->new_psk);
31838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps->new_psk = NULL;
31848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
31858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
318604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e,
318704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			   wps->dev_password, wps->dev_password_len);
31888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
31898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->pbc) {
31908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_remove_pbc_session(wps->wps->registrar,
31911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt						 wps->uuid_e,
31921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt						 wps->p2p_dev_addr);
31938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_pbc_completed(wps->wps->registrar);
3194d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		os_get_time(&wps->wps->registrar->pbc_ignore_start);
3195d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		os_memcpy(wps->wps->registrar->pbc_ignore_uuid, wps->uuid_e,
3196d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			  WPS_UUID_LEN);
31978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
31988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_pin_completed(wps->wps->registrar);
31998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
32008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* TODO: maintain AuthorizedMACs somewhere separately for each ER and
32018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * merge them into APs own list.. */
32028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3203b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt	wps_success_event(wps->wps, wps->mac_addr_e);
32048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return WPS_DONE;
32068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
32078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtenum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
32108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					       enum wsc_op_code op_code,
32118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					       const struct wpabuf *msg)
32128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
32138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	enum wps_process_res ret;
32148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
32168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "op_code=%d)",
32178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   (unsigned long) wpabuf_len(msg), op_code);
32188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_UPNP
32208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
32218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct wps_parse_attr attr;
32228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
32238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    *attr.msg_type == WPS_M3)
32248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->ext_reg = 2; /* past M2/M2D phase */
32258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
32268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->ext_reg > 1)
32278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wps_registrar_free_pending_m2(wps->wps);
32288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (wps->wps->wps_upnp && wps->ext_reg &&
32298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    wps->wps->upnp_msgs == NULL &&
32308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
32318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	{
32328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct wps_parse_attr attr;
32338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		int type;
32348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
32358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			type = -1;
32368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		else
32378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			type = *attr.msg_type;
32388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
32398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   " to external Registrar for processing", type);
32408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
32418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						wps->mac_addr_e,
32428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						UPNP_WPS_WLANEVENT_TYPE_EAP,
32438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt						msg);
32448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (op_code == WSC_MSG)
32458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return WPS_PENDING;
32468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
32478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
32488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "external Registrar");
32498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_CONTINUE;
32508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
32518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_UPNP */
32528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (op_code) {
32548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WSC_MSG:
32558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return wps_process_wsc_msg(wps, msg);
32568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WSC_ACK:
32578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps_validate_wsc_ack(msg) < 0)
32588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return WPS_FAILURE;
32598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return wps_process_wsc_ack(wps, msg);
32608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WSC_NACK:
32618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps_validate_wsc_nack(msg) < 0)
32628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return WPS_FAILURE;
32638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return wps_process_wsc_nack(wps, msg);
32648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case WSC_Done:
32658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (wps_validate_wsc_done(msg) < 0)
32668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return WPS_FAILURE;
32678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = wps_process_wsc_done(wps, msg);
32688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ret == WPS_FAILURE) {
32698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps->state = SEND_WSC_NACK;
32708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_fail_event(wps->wps, WPS_WSC_DONE,
32718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       wps->config_error,
3272b7b4d0ec07161a6d76c40ba7ef1306e82fbb7e15Dmitry Shmidt				       wps->error_indication, wps->mac_addr_e);
32738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
32748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return ret;
32758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	default:
32768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
32778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return WPS_FAILURE;
32788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
32798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
32808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_registrar_update_ie(struct wps_registrar *reg)
32838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
32848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return wps_set_ie(reg);
32858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
32868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_set_selected_timeout(void *eloop_ctx,
32898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					       void *timeout_ctx)
32908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
32918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_registrar *reg = eloop_ctx;
32928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
32938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
32948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "unselect internal Registrar");
32958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->selected_registrar = 0;
32968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->pbc = 0;
32974b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt	wps_registrar_selected_registrar_changed(reg, 0);
32988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
32998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_UPNP
33028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_sel_reg_add(struct wps_registrar *reg,
33038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				      struct subscription *s)
33048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
33058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int i, j;
33068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
33078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   "config_methods=0x%x)",
33088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		   s->dev_password_id, s->config_methods);
33098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->sel_reg_union = 1;
33108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
33118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		reg->sel_reg_dev_password_id_override = s->dev_password_id;
33128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->sel_reg_config_methods_override == -1)
33138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		reg->sel_reg_config_methods_override = 0;
33148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->sel_reg_config_methods_override |= s->config_methods;
33158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
33168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (is_zero_ether_addr(reg->authorized_macs_union[i]))
33178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
33188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
33198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	     j++) {
33208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (is_zero_ether_addr(s->authorized_macs[j]))
33218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
33228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC into union: "
33238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   MACSTR, MAC2STR(s->authorized_macs[j]));
33248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(reg->authorized_macs_union[i],
33258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  s->authorized_macs[j], ETH_ALEN);
33268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		i++;
33278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
33288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union",
33298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    (u8 *) reg->authorized_macs_union,
33308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    sizeof(reg->authorized_macs_union));
33318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
33328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_UPNP */
33338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void wps_registrar_sel_reg_union(struct wps_registrar *reg)
33368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
33378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS_UPNP
33388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct subscription *s;
33398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->wps->wps_upnp == NULL)
33418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
33428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	dl_list_for_each(s, &reg->wps->wps_upnp->subscriptions,
33448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 struct subscription, list) {
33458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct subscr_addr *sa;
33468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
33478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (sa) {
33488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
33498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   inet_ntoa(sa->saddr.sin_addr),
33508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   ntohs(sa->saddr.sin_port));
33518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
33528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (s->selected_registrar)
33538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_registrar_sel_reg_add(reg, s);
33548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		else
33558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
33568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "selected");
33578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
33588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS_UPNP */
33598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
33608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
33638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
33648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @reg: Registrar data from wps_registrar_init()
33658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
33668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function is called when selected registrar state changes, e.g., when an
33678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * AP receives a SetSelectedRegistrar UPnP message.
33688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
33694b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidtvoid wps_registrar_selected_registrar_changed(struct wps_registrar *reg,
33704b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt					      u16 dev_pw_id)
33718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
33728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
33738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->sel_reg_union = reg->selected_registrar;
33758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->sel_reg_dev_password_id_override = -1;
33768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	reg->sel_reg_config_methods_override = -1;
33778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
33788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		  WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
33798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union (start with own)",
33808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    (u8 *) reg->authorized_macs_union,
33818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    sizeof(reg->authorized_macs_union));
33828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->selected_registrar) {
33838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		u16 methods;
33848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
33858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
33868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS2
33878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
33888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			     WPS_CONFIG_PHY_PUSHBUTTON);
33898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS2 */
33908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (reg->pbc) {
33918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			reg->sel_reg_dev_password_id_override =
33928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				DEV_PW_PUSHBUTTON;
33938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wps_set_pushbutton(&methods, reg->wps->config_methods);
33944b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt		} else if (dev_pw_id)
33954b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt			reg->sel_reg_dev_password_id_override = dev_pw_id;
33968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
33978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "(pbc=%d)", reg->pbc);
33988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		reg->sel_reg_config_methods_override = methods;
33998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else
34008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
34018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_registrar_sel_reg_union(reg);
34038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_set_ie(reg);
34058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wps_cb_set_sel_reg(reg);
34068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
34078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
34108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   char *buf, size_t buflen)
34118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
34128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wps_registrar_device *d;
34138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int len = 0, ret;
34148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	char uuid[40];
34158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	char devtype[WPS_DEV_TYPE_BUFSIZE];
34168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	d = wps_device_get(reg, addr);
34188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (d == NULL)
34198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
34208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
34218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
34228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = os_snprintf(buf + len, buflen - len,
34248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  "wpsUuid=%s\n"
34258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  "wpsPrimaryDeviceType=%s\n"
34268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  "wpsDeviceName=%s\n"
34278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  "wpsManufacturer=%s\n"
34288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  "wpsModelName=%s\n"
34298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  "wpsModelNumber=%s\n"
34308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  "wpsSerialNumber=%s\n",
34318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  uuid,
34328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
34338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					       sizeof(devtype)),
34348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  d->dev.device_name ? d->dev.device_name : "",
34358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  d->dev.manufacturer ? d->dev.manufacturer : "",
34368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  d->dev.model_name ? d->dev.model_name : "",
34378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  d->dev.model_number ? d->dev.model_number : "",
34388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  d->dev.serial_number ? d->dev.serial_number : "");
34398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ret < 0 || (size_t) ret >= buflen - len)
34408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return len;
34418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	len += ret;
34428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return len;
34448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
34458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint wps_registrar_config_ap(struct wps_registrar *reg,
34488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    struct wps_credential *cred)
34498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
34508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#ifdef CONFIG_WPS2
34518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	printf("encr_type=0x%x\n", cred->encr_type);
34528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
34538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				 WPS_ENCR_AES))) {
34548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (cred->encr_type & WPS_ENCR_WEP) {
34558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
34568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   "due to WEP configuration");
34578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
34588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
34598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
34618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "invalid encr_type 0x%x", cred->encr_type);
34628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
34638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
34648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
34668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    WPS_ENCR_TKIP) {
34678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
34688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "TKIP+AES");
34698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cred->encr_type |= WPS_ENCR_AES;
34708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
34718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
34738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	    WPS_AUTH_WPAPSK) {
34748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
34758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   "WPAPSK+WPA2PSK");
34768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		cred->auth_type |= WPS_AUTH_WPA2PSK;
34778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
34788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#endif /* CONFIG_WPS2 */
34798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (reg->wps->cred_cb)
34818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return reg->wps->cred_cb(reg->wps->cb_ctx, cred);
34828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
34838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
34848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
348504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
348604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
348704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#ifdef CONFIG_WPS_NFC
348804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
348904949598a23f501be6eec21697465fd46a28840aDmitry Shmidtint wps_registrar_add_nfc_pw_token(struct wps_registrar *reg,
349004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				   const u8 *pubkey_hash, u16 pw_id,
349104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				   const u8 *dev_pw, size_t dev_pw_len)
349204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt{
349304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	struct wps_nfc_pw_token *token;
349404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
349504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (dev_pw_len > WPS_OOB_DEVICE_PASSWORD_LEN)
349604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		return -1;
349704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
349804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	wps_free_nfc_pw_tokens(&reg->nfc_pw_tokens, pw_id);
349904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
350004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	token = os_zalloc(sizeof(*token));
350104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (token == NULL)
350204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		return -1;
350304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
350404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	os_memcpy(token->pubkey_hash, pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
350504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	token->pw_id = pw_id;
350633e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	wpa_snprintf_hex_uppercase((char *) token->dev_pw,
350733e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt				   sizeof(token->dev_pw),
350833e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt				   dev_pw, dev_pw_len);
350933e38bfa7159cef089d6ee0d904778e184c72c47Dmitry Shmidt	token->dev_pw_len = dev_pw_len * 2;
351004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
351104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	dl_list_add(&reg->nfc_pw_tokens, &token->list);
351204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
351304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	reg->selected_registrar = 1;
351404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	reg->pbc = 0;
351504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	wps_registrar_add_authorized_mac(reg,
351604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt					 (u8 *) "\xff\xff\xff\xff\xff\xff");
35174b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt	wps_registrar_selected_registrar_changed(reg, pw_id);
351804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
351904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
352004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			       wps_registrar_set_selected_timeout,
352104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt			       reg, NULL);
352204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
35234b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Added NFC Device Password %u to Registrar",
35244b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt		   pw_id);
35254b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt
352604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	return 0;
352704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt}
352804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
352904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
353004949598a23f501be6eec21697465fd46a28840aDmitry Shmidtint wps_registrar_add_nfc_password_token(struct wps_registrar *reg,
353104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt					 const u8 *oob_dev_pw,
353204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt					 size_t oob_dev_pw_len)
353304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt{
353404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	const u8 *pos, *hash, *dev_pw;
353504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	u16 id;
353604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	size_t dev_pw_len;
353704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
353804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	if (oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2 +
353904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	    WPS_OOB_DEVICE_PASSWORD_MIN_LEN ||
354004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	    oob_dev_pw_len > WPS_OOB_PUBKEY_HASH_LEN + 2 +
354104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	    WPS_OOB_DEVICE_PASSWORD_LEN)
354204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		return -1;
354304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
354404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	hash = oob_dev_pw;
354504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	pos = oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN;
354604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	id = WPA_GET_BE16(pos);
354704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	dev_pw = pos + 2;
354804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	dev_pw_len = oob_dev_pw + oob_dev_pw_len - dev_pw;
354904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
355004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	wpa_printf(MSG_DEBUG, "WPS: Add NFC Password Token for Password ID %u",
355104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		   id);
355204949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
355304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
355404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt		    hash, WPS_OOB_PUBKEY_HASH_LEN);
355504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	wpa_hexdump_key(MSG_DEBUG, "WPS: Device Password", dev_pw, dev_pw_len);
355604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
355704949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	return wps_registrar_add_nfc_pw_token(reg, hash, id, dev_pw,
355804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt					      dev_pw_len);
355904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt}
356004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
356104949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
356204949598a23f501be6eec21697465fd46a28840aDmitry Shmidtvoid wps_registrar_remove_nfc_pw_token(struct wps_registrar *reg,
356304949598a23f501be6eec21697465fd46a28840aDmitry Shmidt				       struct wps_nfc_pw_token *token)
356404949598a23f501be6eec21697465fd46a28840aDmitry Shmidt{
356504949598a23f501be6eec21697465fd46a28840aDmitry Shmidt	wps_registrar_remove_authorized_mac(reg,
356604949598a23f501be6eec21697465fd46a28840aDmitry Shmidt					    (u8 *) "\xff\xff\xff\xff\xff\xff");
35674b06059785b935dd1f4f09314e4e12c417d2c6a4Dmitry Shmidt	wps_registrar_selected_registrar_changed(reg, 0);
356804949598a23f501be6eec21697465fd46a28840aDmitry Shmidt}
356904949598a23f501be6eec21697465fd46a28840aDmitry Shmidt
357004949598a23f501be6eec21697465fd46a28840aDmitry Shmidt#endif /* CONFIG_WPS_NFC */
3571