Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / src / wps / wps_registrar.c
index 7da374a..4ca3a42 100644 (file)
@@ -1,15 +1,9 @@
 /*
  * Wi-Fi Protected Setup - Registrar
- * Copyright (c) 2008-2009, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2008-2013, Jouni Malinen <j@w1.fi>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  */
 
 #include "utils/includes.h"
@@ -21,6 +15,7 @@
 #include "utils/list.h"
 #include "crypto/crypto.h"
 #include "crypto/sha256.h"
+#include "crypto/random.h"
 #include "common/ieee802_11_defs.h"
 #include "wps_i.h"
 #include "wps_dev_attr.h"
 #define WPS_WORKAROUNDS
 #endif /* CONFIG_WPS_STRICT */
 
+#ifdef CONFIG_WPS_NFC
+
+struct wps_nfc_pw_token {
+       struct dl_list list;
+       u8 pubkey_hash[WPS_OOB_PUBKEY_HASH_LEN];
+       unsigned int peer_pk_hash_known:1;
+       u16 pw_id;
+       u8 dev_pw[WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1];
+       size_t dev_pw_len;
+       int pk_hash_provided_oob; /* whether own PK hash was provided OOB */
+};
+
+
+static void wps_remove_nfc_pw_token(struct wps_nfc_pw_token *token)
+{
+       dl_list_del(&token->list);
+       bin_clear_free(token, sizeof(*token));
+}
+
+
+static void wps_free_nfc_pw_tokens(struct dl_list *tokens, u16 pw_id)
+{
+       struct wps_nfc_pw_token *token, *prev;
+       dl_list_for_each_safe(token, prev, tokens, struct wps_nfc_pw_token,
+                             list) {
+               if (pw_id == 0 || pw_id == token->pw_id)
+                       wps_remove_nfc_pw_token(token);
+       }
+}
+
+
+static struct wps_nfc_pw_token * wps_get_nfc_pw_token(struct dl_list *tokens,
+                                                     u16 pw_id)
+{
+       struct wps_nfc_pw_token *token;
+       dl_list_for_each(token, tokens, struct wps_nfc_pw_token, list) {
+               if (pw_id == token->pw_id)
+                       return token;
+       }
+       return NULL;
+}
+
+#else /* CONFIG_WPS_NFC */
+
+#define wps_free_nfc_pw_tokens(t, p) do { } while (0)
+
+#endif /* CONFIG_WPS_NFC */
+
+
 struct wps_uuid_pin {
        struct dl_list list;
        u8 uuid[WPS_UUID_LEN];
@@ -40,14 +84,14 @@ struct wps_uuid_pin {
 #define PIN_LOCKED BIT(0)
 #define PIN_EXPIRES BIT(1)
        int flags;
-       struct os_time expiration;
+       struct os_reltime expiration;
        u8 enrollee_addr[ETH_ALEN];
 };
 
 
 static void wps_free_pin(struct wps_uuid_pin *pin)
 {
-       os_free(pin->pin);
+       bin_clear_free(pin->pin, pin->pin_len);
        os_free(pin);
 }
 
@@ -71,7 +115,7 @@ struct wps_pbc_session {
        struct wps_pbc_session *next;
        u8 addr[ETH_ALEN];
        u8 uuid_e[WPS_UUID_LEN];
-       struct os_time timestamp;
+       struct os_reltime timestamp;
 };
 
 
@@ -100,14 +144,15 @@ struct wps_registrar {
        int pbc;
        int selected_registrar;
 
-       int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
-                         size_t psk_len);
+       int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
+                         const u8 *psk, size_t psk_len);
        int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
                         struct wpabuf *probe_resp_ie);
        void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
                              const struct wps_device_data *dev);
        void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
-                              const u8 *uuid_e);
+                              const u8 *uuid_e, const u8 *dev_pw,
+                              size_t dev_pw_len);
        void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
                               u16 sel_reg_config_methods);
        void (*enrollee_seen_cb)(void *ctx, const u8 *addr, const u8 *uuid_e,
@@ -117,6 +162,7 @@ struct wps_registrar {
        void *cb_ctx;
 
        struct dl_list pins;
+       struct dl_list nfc_pw_tokens;
        struct wps_pbc_session *pbc_sessions;
 
        int skip_cred_build;
@@ -127,6 +173,7 @@ struct wps_registrar {
        int sel_reg_config_methods_override;
        int static_wep_only;
        int dualband;
+       int force_per_enrollee_psk;
 
        struct wps_registrar_device *devices;
 
@@ -134,6 +181,13 @@ struct wps_registrar {
 
        u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
        u8 authorized_macs_union[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
+
+       u8 p2p_dev_addr[ETH_ALEN];
+
+       u8 pbc_ignore_uuid[WPS_UUID_LEN];
+#ifdef WPS_WORKAROUNDS
+       struct os_reltime pbc_ignore_start;
+#endif /* WPS_WORKAROUNDS */
 };
 
 
@@ -141,6 +195,8 @@ static int wps_set_ie(struct wps_registrar *reg);
 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
                                               void *timeout_ctx);
+static void wps_registrar_remove_pin(struct wps_registrar *reg,
+                                    struct wps_uuid_pin *pin);
 
 
 static void wps_registrar_add_authorized_mac(struct wps_registrar *reg,
@@ -259,9 +315,9 @@ static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
                                          const u8 *addr, const u8 *uuid_e)
 {
        struct wps_pbc_session *pbc, *prev = NULL;
-       struct os_time now;
+       struct os_reltime now;
 
-       os_get_time(&now);
+       os_get_reltime(&now);
 
        pbc = reg->pbc_sessions;
        while (pbc) {
@@ -295,7 +351,8 @@ static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
        pbc = pbc->next;
 
        while (pbc) {
-               if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
+               if (os_reltime_expired(&now, &pbc->timestamp,
+                                      WPS_PBC_WALK_TIME)) {
                        prev->next = NULL;
                        wps_free_pbc_sessions(pbc);
                        break;
@@ -307,20 +364,29 @@ static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
 
 
 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
-                                            const u8 *addr, const u8 *uuid_e)
+                                            const u8 *uuid_e,
+                                            const u8 *p2p_dev_addr)
 {
-       struct wps_pbc_session *pbc, *prev = NULL;
+       struct wps_pbc_session *pbc, *prev = NULL, *tmp;
 
        pbc = reg->pbc_sessions;
        while (pbc) {
-               if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
-                   os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
+               if (os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0 ||
+                   (p2p_dev_addr && !is_zero_ether_addr(reg->p2p_dev_addr) &&
+                    os_memcmp(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
+                    0)) {
                        if (prev)
                                prev->next = pbc->next;
                        else
                                reg->pbc_sessions = pbc->next;
-                       os_free(pbc);
-                       break;
+                       tmp = pbc;
+                       pbc = pbc->next;
+                       wpa_printf(MSG_DEBUG, "WPS: Removing PBC session for "
+                                  "addr=" MACSTR, MAC2STR(tmp->addr));
+                       wpa_hexdump(MSG_DEBUG, "WPS: Removed UUID-E",
+                                   tmp->uuid_e, WPS_UUID_LEN);
+                       os_free(tmp);
+                       continue;
                }
                prev = pbc;
                pbc = pbc->next;
@@ -333,21 +399,45 @@ int wps_registrar_pbc_overlap(struct wps_registrar *reg,
 {
        int count = 0;
        struct wps_pbc_session *pbc;
-       struct os_time now;
+       struct wps_pbc_session *first = NULL;
+       struct os_reltime now;
+
+       os_get_reltime(&now);
 
-       os_get_time(&now);
+       wpa_printf(MSG_DEBUG, "WPS: Checking active PBC sessions for overlap");
+
+       if (uuid_e) {
+               wpa_printf(MSG_DEBUG, "WPS: Add one for the requested UUID");
+               wpa_hexdump(MSG_DEBUG, "WPS: Requested UUID",
+                           uuid_e, WPS_UUID_LEN);
+               count++;
+       }
 
        for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
-               if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
+               wpa_printf(MSG_DEBUG, "WPS: Consider PBC session with " MACSTR,
+                          MAC2STR(pbc->addr));
+               wpa_hexdump(MSG_DEBUG, "WPS: UUID-E",
+                           pbc->uuid_e, WPS_UUID_LEN);
+               if (os_reltime_expired(&now, &pbc->timestamp,
+                                      WPS_PBC_WALK_TIME)) {
+                       wpa_printf(MSG_DEBUG, "WPS: PBC walk time has expired");
                        break;
-               if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
-                   uuid_e == NULL ||
-                   os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
+               }
+               if (first &&
+                   os_memcmp(pbc->uuid_e, first->uuid_e, WPS_UUID_LEN) == 0) {
+                       wpa_printf(MSG_DEBUG, "WPS: Same Enrollee");
+                       continue; /* same Enrollee */
+               }
+               if (uuid_e == NULL ||
+                   os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN)) {
+                       wpa_printf(MSG_DEBUG, "WPS: New Enrollee");
                        count++;
+               }
+               if (first == NULL)
+                       first = pbc;
        }
 
-       if (addr || uuid_e)
-               count++;
+       wpa_printf(MSG_DEBUG, "WPS: %u active PBC session(s) found", count);
 
        return count > 1 ? 1 : 0;
 }
@@ -392,7 +482,7 @@ static void wps_registrar_free_pending_m2(struct wps_context *wps)
 static int wps_build_ap_setup_locked(struct wps_context *wps,
                                     struct wpabuf *msg)
 {
-       if (wps->ap_setup_locked) {
+       if (wps->ap_setup_locked && wps->ap_setup_locked != 2) {
                wpa_printf(MSG_DEBUG, "WPS:  * AP Setup Locked");
                wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
                wpabuf_put_be16(msg, 1);
@@ -448,13 +538,14 @@ static int wps_build_sel_pbc_reg_uuid_e(struct wps_registrar *reg,
 static void wps_set_pushbutton(u16 *methods, u16 conf_methods)
 {
        *methods |= WPS_CONFIG_PUSHBUTTON;
-#ifdef CONFIG_WPS2
-       if (conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON)
+       if ((conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON) ==
+           WPS_CONFIG_VIRT_PUSHBUTTON)
                *methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
-       if (conf_methods & WPS_CONFIG_PHY_PUSHBUTTON)
+       if ((conf_methods & WPS_CONFIG_PHY_PUSHBUTTON) ==
+           WPS_CONFIG_PHY_PUSHBUTTON)
                *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
        if ((*methods & WPS_CONFIG_VIRT_PUSHBUTTON) !=
-           WPS_CONFIG_VIRT_PUSHBUTTON ||
+           WPS_CONFIG_VIRT_PUSHBUTTON &&
            (*methods & WPS_CONFIG_PHY_PUSHBUTTON) !=
            WPS_CONFIG_PHY_PUSHBUTTON) {
                /*
@@ -464,7 +555,6 @@ static void wps_set_pushbutton(u16 *methods, u16 conf_methods)
                 */
                *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
        }
-#endif /* CONFIG_WPS2 */
 }
 
 
@@ -476,10 +566,8 @@ static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
                return 0;
        methods = reg->wps->config_methods;
        methods &= ~WPS_CONFIG_PUSHBUTTON;
-#ifdef CONFIG_WPS2
        methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
                     WPS_CONFIG_PHY_PUSHBUTTON);
-#endif /* CONFIG_WPS2 */
        if (reg->pbc)
                wps_set_pushbutton(&methods, reg->wps->config_methods);
        if (reg->sel_reg_config_methods_override >= 0)
@@ -502,10 +590,8 @@ static int wps_build_probe_config_methods(struct wps_registrar *reg,
         * external Registrars.
         */
        methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
-#ifdef CONFIG_WPS2
        methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
                     WPS_CONFIG_PHY_PUSHBUTTON);
-#endif /* CONFIG_WPS2 */
        wpa_printf(MSG_DEBUG, "WPS:  * Config Methods (%x)", methods);
        wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
        wpabuf_put_be16(msg, 2);
@@ -517,15 +603,7 @@ static int wps_build_probe_config_methods(struct wps_registrar *reg,
 static int wps_build_config_methods_r(struct wps_registrar *reg,
                                      struct wpabuf *msg)
 {
-       u16 methods;
-       methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
-#ifdef CONFIG_WPS2
-       methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
-                    WPS_CONFIG_PHY_PUSHBUTTON);
-#endif /* CONFIG_WPS2 */
-       if (reg->pbc)
-               wps_set_pushbutton(&methods, reg->wps->config_methods);
-       return wps_build_config_methods(msg, methods);
+       return wps_build_config_methods(msg, reg->wps->config_methods);
 }
 
 
@@ -533,13 +611,11 @@ const u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count)
 {
        *count = 0;
 
-#ifdef CONFIG_WPS2
        while (*count < WPS_MAX_AUTHORIZED_MACS) {
                if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
                        break;
                (*count)++;
        }
-#endif /* CONFIG_WPS2 */
 
        return (const u8 *) reg->authorized_macs_union;
 }
@@ -566,6 +642,7 @@ wps_registrar_init(struct wps_context *wps,
                return NULL;
 
        dl_list_init(&reg->pins);
+       dl_list_init(&reg->nfc_pw_tokens);
        reg->wps = wps;
        reg->new_psk_cb = cfg->new_psk_cb;
        reg->set_ie_cb = cfg->set_ie_cb;
@@ -588,6 +665,7 @@ wps_registrar_init(struct wps_context *wps,
        reg->sel_reg_config_methods_override = -1;
        reg->static_wep_only = cfg->static_wep_only;
        reg->dualband = cfg->dualband;
+       reg->force_per_enrollee_psk = cfg->force_per_enrollee_psk;
 
        if (wps_set_ie(reg)) {
                wps_registrar_deinit(reg);
@@ -598,6 +676,22 @@ wps_registrar_init(struct wps_context *wps,
 }
 
 
+void wps_registrar_flush(struct wps_registrar *reg)
+{
+       if (reg == NULL)
+               return;
+       wps_free_pins(&reg->pins);
+       wps_free_nfc_pw_tokens(&reg->nfc_pw_tokens, 0);
+       wps_free_pbc_sessions(reg->pbc_sessions);
+       reg->pbc_sessions = NULL;
+       wps_free_devices(reg->devices);
+       reg->devices = NULL;
+#ifdef WPS_WORKAROUNDS
+       reg->pbc_ignore_start.sec = 0;
+#endif /* WPS_WORKAROUNDS */
+}
+
+
 /**
  * wps_registrar_deinit - Deinitialize WPS Registrar data
  * @reg: Registrar data from wps_registrar_init()
@@ -608,14 +702,27 @@ void wps_registrar_deinit(struct wps_registrar *reg)
                return;
        eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
        eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
-       wps_free_pins(&reg->pins);
-       wps_free_pbc_sessions(reg->pbc_sessions);
+       wps_registrar_flush(reg);
        wpabuf_free(reg->extra_cred);
-       wps_free_devices(reg->devices);
        os_free(reg);
 }
 
 
+static void wps_registrar_invalidate_unused(struct wps_registrar *reg)
+{
+       struct wps_uuid_pin *pin;
+
+       dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
+               if (pin->wildcard_uuid == 1 && !(pin->flags & PIN_LOCKED)) {
+                       wpa_printf(MSG_DEBUG, "WPS: Invalidate previously "
+                                  "configured wildcard PIN");
+                       wps_registrar_remove_pin(reg, pin);
+                       break;
+               }
+       }
+}
+
+
 /**
  * wps_registrar_add_pin - Configure a new PIN for Registrar
  * @reg: Registrar data from wps_registrar_init()
@@ -651,10 +758,13 @@ int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
 
        if (timeout) {
                p->flags |= PIN_EXPIRES;
-               os_get_time(&p->expiration);
+               os_get_reltime(&p->expiration);
                p->expiration.sec += timeout;
        }
 
+       if (p->wildcard_uuid)
+               wps_registrar_invalidate_unused(reg);
+
        dl_list_add(&reg->pins, &p->list);
 
        wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
@@ -668,7 +778,7 @@ int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
        else
                wps_registrar_add_authorized_mac(
                        reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
-       wps_registrar_selected_registrar_changed(reg);
+       wps_registrar_selected_registrar_changed(reg, 0);
        eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
        eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
                               wps_registrar_set_selected_timeout,
@@ -690,20 +800,20 @@ static void wps_registrar_remove_pin(struct wps_registrar *reg,
                addr = pin->enrollee_addr;
        wps_registrar_remove_authorized_mac(reg, addr);
        wps_remove_pin(pin);
-       wps_registrar_selected_registrar_changed(reg);
+       wps_registrar_selected_registrar_changed(reg, 0);
 }
 
 
 static void wps_registrar_expire_pins(struct wps_registrar *reg)
 {
        struct wps_uuid_pin *pin, *prev;
-       struct os_time now;
+       struct os_reltime now;
 
-       os_get_time(&now);
+       os_get_reltime(&now);
        dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
        {
                if ((pin->flags & PIN_EXPIRES) &&
-                   os_time_before(&pin->expiration, &now)) {
+                   os_reltime_before(&pin->expiration, &now)) {
                        wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
                                    pin->uuid, WPS_UUID_LEN);
                        wps_registrar_remove_pin(reg, pin);
@@ -715,14 +825,22 @@ static void wps_registrar_expire_pins(struct wps_registrar *reg)
 /**
  * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
  * @reg: Registrar data from wps_registrar_init()
+ * @dev_pw: PIN to search for or %NULL to match any
+ * @dev_pw_len: Length of dev_pw in octets
  * Returns: 0 on success, -1 if not wildcard PIN is enabled
  */
-static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg)
+static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg,
+                                                const u8 *dev_pw,
+                                                size_t dev_pw_len)
 {
        struct wps_uuid_pin *pin, *prev;
 
        dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
        {
+               if (dev_pw && pin->pin &&
+                   (dev_pw_len != pin->pin_len ||
+                    os_memcmp_const(dev_pw, pin->pin, dev_pw_len) != 0))
+                       continue; /* different PIN */
                if (pin->wildcard_uuid) {
                        wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
                                    pin->uuid, WPS_UUID_LEN);
@@ -778,10 +896,11 @@ static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
                /* Check for wildcard UUIDs since none of the UUID-specific
                 * PINs matched */
                dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
-                       if (pin->wildcard_uuid == 1) {
+                       if (pin->wildcard_uuid == 1 ||
+                           pin->wildcard_uuid == 2) {
                                wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
                                           "PIN. Assigned it for this UUID-E");
-                               pin->wildcard_uuid = 2;
+                               pin->wildcard_uuid++;
                                os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
                                found = pin;
                                break;
@@ -823,7 +942,7 @@ int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
 
        dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
                if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
-                       if (pin->wildcard_uuid == 2) {
+                       if (pin->wildcard_uuid == 3) {
                                wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
                                           "wildcard PIN");
                                return wps_registrar_invalidate_pin(reg, uuid);
@@ -841,7 +960,10 @@ static void wps_registrar_stop_pbc(struct wps_registrar *reg)
 {
        reg->selected_registrar = 0;
        reg->pbc = 0;
-       wps_registrar_selected_registrar_changed(reg);
+       os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
+       wps_registrar_remove_authorized_mac(reg,
+                                           (u8 *) "\xff\xff\xff\xff\xff\xff");
+       wps_registrar_selected_registrar_changed(reg, 0);
 }
 
 
@@ -858,26 +980,40 @@ static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
 /**
  * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
  * @reg: Registrar data from wps_registrar_init()
- * Returns: 0 on success, -1 on failure
+ * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
+ *     indicates no such filtering
+ * Returns: 0 on success, -1 on failure, -2 on session overlap
  *
  * This function is called on an AP when a push button is pushed to activate
  * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
- * or when a PBC registration is completed.
+ * or when a PBC registration is completed. If more than one Enrollee in active
+ * PBC mode has been detected during the monitor time (previous 2 minutes), the
+ * PBC mode is not activated and -2 is returned to indicate session overlap.
+ * This is skipped if a specific Enrollee is selected.
  */
-int wps_registrar_button_pushed(struct wps_registrar *reg)
+int wps_registrar_button_pushed(struct wps_registrar *reg,
+                               const u8 *p2p_dev_addr)
 {
-       if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
+       if (p2p_dev_addr == NULL &&
+           wps_registrar_pbc_overlap(reg, NULL, NULL)) {
                wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
                           "mode");
                wps_pbc_overlap_event(reg->wps);
-               return -1;
+               return -2;
        }
        wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
        reg->force_pbc_overlap = 0;
        reg->selected_registrar = 1;
        reg->pbc = 1;
-       wps_registrar_selected_registrar_changed(reg);
+       if (p2p_dev_addr)
+               os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
+       else
+               os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
+       wps_registrar_add_authorized_mac(reg,
+                                        (u8 *) "\xff\xff\xff\xff\xff\xff");
+       wps_registrar_selected_registrar_changed(reg, 0);
 
+       wps_pbc_active_event(reg->wps);
        eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
        eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
        eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
@@ -891,6 +1027,7 @@ static void wps_registrar_pbc_completed(struct wps_registrar *reg)
        wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
        eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
        wps_registrar_stop_pbc(reg);
+       wps_pbc_disable_event(reg->wps);
 }
 
 
@@ -899,7 +1036,31 @@ static void wps_registrar_pin_completed(struct wps_registrar *reg)
        wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
        eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
        reg->selected_registrar = 0;
-       wps_registrar_selected_registrar_changed(reg);
+       wps_registrar_selected_registrar_changed(reg, 0);
+}
+
+
+void wps_registrar_complete(struct wps_registrar *registrar, const u8 *uuid_e,
+                           const u8 *dev_pw, size_t dev_pw_len)
+{
+       if (registrar->pbc) {
+               wps_registrar_remove_pbc_session(registrar,
+                                                uuid_e, NULL);
+               wps_registrar_pbc_completed(registrar);
+#ifdef WPS_WORKAROUNDS
+               os_get_reltime(&registrar->pbc_ignore_start);
+#endif /* WPS_WORKAROUNDS */
+               os_memcpy(registrar->pbc_ignore_uuid, uuid_e, WPS_UUID_LEN);
+       } else {
+               wps_registrar_pin_completed(registrar);
+       }
+
+       if (dev_pw &&
+           wps_registrar_invalidate_wildcard_pin(registrar, dev_pw,
+                                                 dev_pw_len) == 0) {
+               wpa_hexdump_key(MSG_DEBUG, "WPS: Invalidated wildcard PIN",
+                               dev_pw, dev_pw_len);
+       }
 }
 
 
@@ -908,12 +1069,13 @@ int wps_registrar_wps_cancel(struct wps_registrar *reg)
        if (reg->pbc) {
                wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
                wps_registrar_pbc_timeout(reg, NULL);
+               eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
                return 1;
        } else if (reg->selected_registrar) {
                /* PIN Method */
                wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
                wps_registrar_pin_completed(reg);
-               wps_registrar_invalidate_wildcard_pin(reg);
+               wps_registrar_invalidate_wildcard_pin(reg, NULL, 0);
                return 1;
        }
        return 0;
@@ -935,6 +1097,7 @@ void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
                                int p2p_wildcard)
 {
        struct wps_parse_attr attr;
+       int skip_add = 0;
 
        wpa_hexdump_buf(MSG_MSGDUMP,
                        "WPS: Probe Request with WPS data received",
@@ -983,8 +1146,27 @@ void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
                           "UUID-E included");
                return;
        }
+       wpa_hexdump(MSG_DEBUG, "WPS: UUID-E from Probe Request", attr.uuid_e,
+                   WPS_UUID_LEN);
 
-       wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
+#ifdef WPS_WORKAROUNDS
+       if (reg->pbc_ignore_start.sec &&
+           os_memcmp(attr.uuid_e, reg->pbc_ignore_uuid, WPS_UUID_LEN) == 0) {
+               struct os_reltime now, dur;
+               os_get_reltime(&now);
+               os_reltime_sub(&now, &reg->pbc_ignore_start, &dur);
+               if (dur.sec >= 0 && dur.sec < 5) {
+                       wpa_printf(MSG_DEBUG, "WPS: Ignore PBC activation "
+                                  "based on Probe Request from the Enrollee "
+                                  "that just completed PBC provisioning");
+                       skip_add = 1;
+               } else
+                       reg->pbc_ignore_start.sec = 0;
+       }
+#endif /* WPS_WORKAROUNDS */
+
+       if (!skip_add)
+               wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
        if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
                wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
                reg->force_pbc_overlap = 1;
@@ -993,13 +1175,14 @@ void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
 }
 
 
-static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
-                         const u8 *psk, size_t psk_len)
+int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
+                  const u8 *p2p_dev_addr, const u8 *psk, size_t psk_len)
 {
        if (reg->new_psk_cb == NULL)
                return 0;
 
-       return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
+       return reg->new_psk_cb(reg->cb_ctx, mac_addr, p2p_dev_addr, psk,
+                              psk_len);
 }
 
 
@@ -1014,12 +1197,13 @@ static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
 
 
 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
-                              const u8 *uuid_e)
+                              const u8 *uuid_e, const u8 *dev_pw,
+                              size_t dev_pw_len)
 {
        if (reg->reg_success_cb == NULL)
                return;
 
-       reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
+       reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e, dev_pw, dev_pw_len);
 }
 
 
@@ -1038,10 +1222,8 @@ static void wps_cb_set_sel_reg(struct wps_registrar *reg)
 
        if (reg->selected_registrar) {
                methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
-#ifdef CONFIG_WPS2
                methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
                             WPS_CONFIG_PHY_PUSHBUTTON);
-#endif /* CONFIG_WPS2 */
                if (reg->pbc)
                        wps_set_pushbutton(&methods, reg->wps->config_methods);
        }
@@ -1063,14 +1245,23 @@ static int wps_set_ie(struct wps_registrar *reg)
        struct wpabuf *probe;
        const u8 *auth_macs;
        size_t count;
+       size_t vendor_len = 0;
+       int i;
 
        if (reg->set_ie_cb == NULL)
                return 0;
 
-       beacon = wpabuf_alloc(400);
+       for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
+               if (reg->wps->dev.vendor_ext[i]) {
+                       vendor_len += 2 + 2;
+                       vendor_len += wpabuf_len(reg->wps->dev.vendor_ext[i]);
+               }
+       }
+
+       beacon = wpabuf_alloc(400 + vendor_len);
        if (beacon == NULL)
                return -1;
-       probe = wpabuf_alloc(500);
+       probe = wpabuf_alloc(500 + vendor_len);
        if (probe == NULL) {
                wpabuf_free(beacon);
                return -1;
@@ -1087,13 +1278,23 @@ static int wps_set_ie(struct wps_registrar *reg)
            wps_build_sel_reg_dev_password_id(reg, beacon) ||
            wps_build_sel_reg_config_methods(reg, beacon) ||
            wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
-           (reg->dualband && wps_build_rf_bands(&reg->wps->dev, beacon)) ||
-           wps_build_wfa_ext(beacon, 0, auth_macs, count)) {
+           (reg->dualband && wps_build_rf_bands(&reg->wps->dev, beacon, 0)) ||
+           wps_build_wfa_ext(beacon, 0, auth_macs, count) ||
+           wps_build_vendor_ext(&reg->wps->dev, beacon)) {
                wpabuf_free(beacon);
                wpabuf_free(probe);
                return -1;
        }
 
+#ifdef CONFIG_P2P
+       if (wps_build_dev_name(&reg->wps->dev, beacon) ||
+           wps_build_primary_dev_type(&reg->wps->dev, beacon)) {
+               wpabuf_free(beacon);
+               wpabuf_free(probe);
+               return -1;
+       }
+#endif /* CONFIG_P2P */
+
        wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
 
        if (wps_build_version(probe) ||
@@ -1107,21 +1308,13 @@ static int wps_set_ie(struct wps_registrar *reg)
            wps_build_uuid_e(probe, reg->wps->uuid) ||
            wps_build_device_attrs(&reg->wps->dev, probe) ||
            wps_build_probe_config_methods(reg, probe) ||
-           wps_build_rf_bands(&reg->wps->dev, probe) ||
-           wps_build_wfa_ext(probe, 0, auth_macs, count)) {
-               wpabuf_free(beacon);
-               wpabuf_free(probe);
-               return -1;
-       }
-
-#ifdef CONFIG_P2P
-       if (wps_build_dev_name(&reg->wps->dev, beacon) ||
-           wps_build_primary_dev_type(&reg->wps->dev, beacon)) {
+           (reg->dualband && wps_build_rf_bands(&reg->wps->dev, probe, 0)) ||
+           wps_build_wfa_ext(probe, 0, auth_macs, count) ||
+           wps_build_vendor_ext(&reg->wps->dev, probe)) {
                wpabuf_free(beacon);
                wpabuf_free(probe);
                return -1;
        }
-#endif /* CONFIG_P2P */
 
        beacon = wps_ie_encapsulate(beacon);
        probe = wps_ie_encapsulate(probe);
@@ -1163,20 +1356,49 @@ static int wps_get_dev_password(struct wps_data *wps)
        const u8 *pin;
        size_t pin_len = 0;
 
-       os_free(wps->dev_password);
+       bin_clear_free(wps->dev_password, wps->dev_password_len);
        wps->dev_password = NULL;
 
        if (wps->pbc) {
                wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
                pin = (const u8 *) "00000000";
                pin_len = 8;
+#ifdef CONFIG_WPS_NFC
+       } else if (wps->nfc_pw_token) {
+               if (wps->nfc_pw_token->pw_id == DEV_PW_NFC_CONNECTION_HANDOVER)
+               {
+                       wpa_printf(MSG_DEBUG, "WPS: Using NFC connection "
+                                  "handover and abbreviated WPS handshake "
+                                  "without Device Password");
+                       return 0;
+               }
+               wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from NFC "
+                          "Password Token");
+               pin = wps->nfc_pw_token->dev_pw;
+               pin_len = wps->nfc_pw_token->dev_pw_len;
+       } else if (wps->dev_pw_id >= 0x10 &&
+                  wps->wps->ap_nfc_dev_pw_id == wps->dev_pw_id &&
+                  wps->wps->ap_nfc_dev_pw) {
+               wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from own NFC Password Token");
+               pin = wpabuf_head(wps->wps->ap_nfc_dev_pw);
+               pin_len = wpabuf_len(wps->wps->ap_nfc_dev_pw);
+#endif /* CONFIG_WPS_NFC */
        } else {
                pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
                                            &pin_len);
+               if (pin && wps->dev_pw_id >= 0x10) {
+                       wpa_printf(MSG_DEBUG, "WPS: No match for OOB Device "
+                                  "Password ID, but PIN found");
+                       /*
+                        * See whether Enrollee is willing to use PIN instead.
+                        */
+                       wps->dev_pw_id = DEV_PW_DEFAULT;
+               }
        }
        if (pin == NULL) {
                wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
-                          "the Enrollee");
+                          "the Enrollee (context %p registrar %p)",
+                          wps->wps, wps->wps->registrar);
                wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
                                  &wps->peer_dev);
                return -1;
@@ -1208,7 +1430,7 @@ static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
        const u8 *addr[4];
        size_t len[4];
 
-       if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
+       if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
                return -1;
        wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
        wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
@@ -1333,18 +1555,6 @@ static int wps_build_cred_network_key(struct wpabuf *msg,
 }
 
 
-static int wps_build_cred_mac_addr(struct wpabuf *msg,
-                                  const struct wps_credential *cred)
-{
-       wpa_printf(MSG_DEBUG, "WPS:  * MAC Address (" MACSTR ")",
-                  MAC2STR(cred->mac_addr));
-       wpabuf_put_be16(msg, ATTR_MAC_ADDR);
-       wpabuf_put_be16(msg, ETH_ALEN);
-       wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
-       return 0;
-}
-
-
 static int wps_build_credential(struct wpabuf *msg,
                                const struct wps_credential *cred)
 {
@@ -1353,12 +1563,31 @@ static int wps_build_credential(struct wpabuf *msg,
            wps_build_cred_auth_type(msg, cred) ||
            wps_build_cred_encr_type(msg, cred) ||
            wps_build_cred_network_key(msg, cred) ||
-           wps_build_cred_mac_addr(msg, cred))
+           wps_build_mac_addr(msg, cred->mac_addr))
                return -1;
        return 0;
 }
 
 
+int wps_build_credential_wrap(struct wpabuf *msg,
+                             const struct wps_credential *cred)
+{
+       struct wpabuf *wbuf;
+       wbuf = wpabuf_alloc(200);
+       if (wbuf == NULL)
+               return -1;
+       if (wps_build_credential(wbuf, cred)) {
+               wpabuf_free(wbuf);
+               return -1;
+       }
+       wpabuf_put_be16(msg, ATTR_CRED);
+       wpabuf_put_be16(msg, wpabuf_len(wbuf));
+       wpabuf_put_buf(msg, wbuf);
+       wpabuf_free(wbuf);
+       return 0;
+}
+
+
 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
 {
        struct wpabuf *cred;
@@ -1383,8 +1612,6 @@ int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
                wps->auth_type = WPS_AUTH_WPAPSK;
        else if (wps->auth_type & WPS_AUTH_OPEN)
                wps->auth_type = WPS_AUTH_OPEN;
-       else if (wps->auth_type & WPS_AUTH_SHARED)
-               wps->auth_type = WPS_AUTH_SHARED;
        else {
                wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
                           wps->auth_type);
@@ -1404,10 +1631,12 @@ int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
                        return -1;
                }
        } else {
-               if (wps->encr_type & WPS_ENCR_WEP)
-                       wps->encr_type = WPS_ENCR_WEP;
-               else if (wps->encr_type & WPS_ENCR_NONE)
+               if (wps->encr_type & WPS_ENCR_NONE)
                        wps->encr_type = WPS_ENCR_NONE;
+#ifdef CONFIG_TESTING_OPTIONS
+               else if (wps->encr_type & WPS_ENCR_WEP)
+                       wps->encr_type = WPS_ENCR_WEP;
+#endif /* CONFIG_TESTING_OPTIONS */
                else {
                        wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
                                   "type for non-WPA/WPA2 mode");
@@ -1424,8 +1653,12 @@ int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
            !wps->wps->registrar->disable_auto_conf) {
                u8 r[16];
                /* Generate a random passphrase */
-               if (os_get_random(r, sizeof(r)) < 0)
+               if (random_pool_ready() != 1 ||
+                   random_get_bytes(r, sizeof(r)) < 0) {
+                       wpa_printf(MSG_INFO,
+                                  "WPS: Could not generate random PSK");
                        return -1;
+               }
                os_free(wps->new_psk);
                wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
                if (wps->new_psk == NULL)
@@ -1438,13 +1671,15 @@ int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
                                      wps->new_psk, wps->new_psk_len);
                os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
                wps->cred.key_len = wps->new_psk_len;
-       } else if (wps->use_psk_key && wps->wps->psk_set) {
+       } else if (!wps->wps->registrar->force_per_enrollee_psk &&
+                  wps->use_psk_key && wps->wps->psk_set) {
                char hex[65];
                wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
                wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
                os_memcpy(wps->cred.key, hex, 32 * 2);
                wps->cred.key_len = 32 * 2;
-       } else if (wps->wps->network_key) {
+       } else if (!wps->wps->registrar->force_per_enrollee_psk &&
+                  wps->wps->network_key) {
                os_memcpy(wps->cred.key, wps->wps->network_key,
                          wps->wps->network_key_len);
                wps->cred.key_len = wps->wps->network_key_len;
@@ -1456,7 +1691,10 @@ int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
                wps->new_psk = os_malloc(wps->new_psk_len);
                if (wps->new_psk == NULL)
                        return -1;
-               if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
+               if (random_pool_ready() != 1 ||
+                   random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
+                       wpa_printf(MSG_INFO,
+                                  "WPS: Could not generate random PSK");
                        os_free(wps->new_psk);
                        wps->new_psk = NULL;
                        return -1;
@@ -1532,11 +1770,41 @@ static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
 }
 
 
+static struct wpabuf * wps_build_ap_cred(struct wps_data *wps)
+{
+       struct wpabuf *msg, *plain;
+
+       msg = wpabuf_alloc(1000);
+       if (msg == NULL)
+               return NULL;
+
+       plain = wpabuf_alloc(200);
+       if (plain == NULL) {
+               wpabuf_free(msg);
+               return NULL;
+       }
+
+       if (wps_build_ap_settings(wps, plain)) {
+               wpabuf_free(plain);
+               wpabuf_free(msg);
+               return NULL;
+       }
+
+       wpabuf_put_be16(msg, ATTR_CRED);
+       wpabuf_put_be16(msg, wpabuf_len(plain));
+       wpabuf_put_buf(msg, plain);
+       wpabuf_free(plain);
+
+       return msg;
+}
+
+
 static struct wpabuf * wps_build_m2(struct wps_data *wps)
 {
        struct wpabuf *msg;
+       int config_in_m2 = 0;
 
-       if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
+       if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
                return NULL;
        wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
                    wps->nonce_r, WPS_NONCE_LEN);
@@ -1559,19 +1827,47 @@ static struct wpabuf * wps_build_m2(struct wps_data *wps)
            wps_build_conn_type_flags(wps, msg) ||
            wps_build_config_methods_r(wps->wps->registrar, msg) ||
            wps_build_device_attrs(&wps->wps->dev, msg) ||
-           wps_build_rf_bands(&wps->wps->dev, msg) ||
+           wps_build_rf_bands(&wps->wps->dev, msg,
+                              wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
            wps_build_assoc_state(wps, msg) ||
            wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
            wps_build_dev_password_id(msg, wps->dev_pw_id) ||
            wps_build_os_version(&wps->wps->dev, msg) ||
-           wps_build_wfa_ext(msg, 0, NULL, 0) ||
-           wps_build_authenticator(wps, msg)) {
+           wps_build_wfa_ext(msg, 0, NULL, 0)) {
+               wpabuf_free(msg);
+               return NULL;
+       }
+
+#ifdef CONFIG_WPS_NFC
+       if (wps->nfc_pw_token && wps->nfc_pw_token->pk_hash_provided_oob &&
+           wps->nfc_pw_token->pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
+               /*
+                * Use abbreviated handshake since public key hash allowed
+                * Enrollee to validate our public key similarly to how Enrollee
+                * public key was validated. There is no need to validate Device
+                * Password in this case.
+                */
+               struct wpabuf *plain = wpabuf_alloc(500);
+               if (plain == NULL ||
+                   wps_build_cred(wps, plain) ||
+                   wps_build_key_wrap_auth(wps, plain) ||
+                   wps_build_encr_settings(wps, msg, plain)) {
+                       wpabuf_free(msg);
+                       wpabuf_free(plain);
+                       return NULL;
+               }
+               wpabuf_free(plain);
+               config_in_m2 = 1;
+       }
+#endif /* CONFIG_WPS_NFC */
+
+       if (wps_build_authenticator(wps, msg)) {
                wpabuf_free(msg);
                return NULL;
        }
 
        wps->int_reg = 1;
-       wps->state = RECV_M3;
+       wps->state = config_in_m2 ? RECV_DONE : RECV_M3;
        return msg;
 }
 
@@ -1600,7 +1896,8 @@ static struct wpabuf * wps_build_m2d(struct wps_data *wps)
            wps_build_conn_type_flags(wps, msg) ||
            wps_build_config_methods_r(wps->wps->registrar, msg) ||
            wps_build_device_attrs(&wps->wps->dev, msg) ||
-           wps_build_rf_bands(&wps->wps->dev, msg) ||
+           wps_build_rf_bands(&wps->wps->dev, msg,
+                              wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
            wps_build_assoc_state(wps, msg) ||
            wps_build_config_error(msg, err) ||
            wps_build_os_version(&wps->wps->dev, msg) ||
@@ -1724,53 +2021,6 @@ static struct wpabuf * wps_build_m8(struct wps_data *wps)
 }
 
 
-static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
-{
-       struct wpabuf *msg;
-
-       wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
-
-       msg = wpabuf_alloc(1000);
-       if (msg == NULL)
-               return NULL;
-
-       if (wps_build_version(msg) ||
-           wps_build_msg_type(msg, WPS_WSC_ACK) ||
-           wps_build_enrollee_nonce(wps, msg) ||
-           wps_build_registrar_nonce(wps, msg) ||
-           wps_build_wfa_ext(msg, 0, NULL, 0)) {
-               wpabuf_free(msg);
-               return NULL;
-       }
-
-       return msg;
-}
-
-
-static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
-{
-       struct wpabuf *msg;
-
-       wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
-
-       msg = wpabuf_alloc(1000);
-       if (msg == NULL)
-               return NULL;
-
-       if (wps_build_version(msg) ||
-           wps_build_msg_type(msg, WPS_WSC_NACK) ||
-           wps_build_enrollee_nonce(wps, msg) ||
-           wps_build_registrar_nonce(wps, msg) ||
-           wps_build_config_error(msg, wps->config_error) ||
-           wps_build_wfa_ext(msg, 0, NULL, 0)) {
-               wpabuf_free(msg);
-               return NULL;
-       }
-
-       return msg;
-}
-
-
 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
                                      enum wsc_op_code *op_code)
 {
@@ -1981,11 +2231,11 @@ static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
        len[3] = wpabuf_len(wps->dh_pubkey_r);
        hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
 
-       if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
+       if (os_memcmp_const(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
                wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
                           "not match with the pre-committed value");
                wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
-               wps_pwd_auth_fail_event(wps->wps, 0, 1);
+               wps_pwd_auth_fail_event(wps->wps, 0, 1, wps->mac_addr_e);
                return -1;
        }
 
@@ -2021,12 +2271,12 @@ static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
        len[3] = wpabuf_len(wps->dh_pubkey_r);
        hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
 
-       if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
+       if (os_memcmp_const(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
                wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
                           "not match with the pre-committed value");
                wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
                wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
-               wps_pwd_auth_fail_event(wps->wps, 0, 2);
+               wps_pwd_auth_fail_event(wps->wps, 0, 2, wps->mac_addr_e);
                return -1;
        }
 
@@ -2035,6 +2285,13 @@ static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
        wps->wps_pin_revealed = 0;
        wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
 
+       /*
+        * In case wildcard PIN is used and WPS handshake succeeds in the first
+        * attempt, wps_registrar_unlock_pin() would not free the PIN, so make
+        * sure the PIN gets invalidated here.
+        */
+       wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
+
        return 0;
 }
 
@@ -2063,22 +2320,6 @@ static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
                return -1;
        }
 
-#ifdef CONFIG_WPS_OOB
-       if (wps->wps->oob_conf.pubkey_hash != NULL) {
-               const u8 *addr[1];
-               u8 hash[WPS_HASH_LEN];
-
-               addr[0] = pk;
-               sha256_vector(1, addr, &pk_len, hash);
-               if (os_memcmp(hash,
-                             wpabuf_head(wps->wps->oob_conf.pubkey_hash),
-                             WPS_OOB_PUBKEY_HASH_LEN) != 0) {
-                       wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
-                       return -1;
-               }
-       }
-#endif /* CONFIG_WPS_OOB */
-
        wpabuf_free(wps->dh_pubkey_e);
        wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
        if (wps->dh_pubkey_e == NULL)
@@ -2268,6 +2509,45 @@ static int wps_process_config_error(struct wps_data *wps, const u8 *err)
 }
 
 
+static int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
+{
+#ifdef CONFIG_P2P
+       struct wps_registrar *reg = wps->wps->registrar;
+
+       if (is_zero_ether_addr(reg->p2p_dev_addr))
+               return 1; /* no filtering in use */
+
+       if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
+               wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
+                          "filtering for PBC: expected " MACSTR " was "
+                          MACSTR " - indicate PBC session overlap",
+                          MAC2STR(reg->p2p_dev_addr),
+                          MAC2STR(wps->p2p_dev_addr));
+               return 0;
+       }
+#endif /* CONFIG_P2P */
+       return 1;
+}
+
+
+static int wps_registrar_skip_overlap(struct wps_data *wps)
+{
+#ifdef CONFIG_P2P
+       struct wps_registrar *reg = wps->wps->registrar;
+
+       if (is_zero_ether_addr(reg->p2p_dev_addr))
+               return 0; /* no specific Enrollee selected */
+
+       if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) == 0) {
+               wpa_printf(MSG_DEBUG, "WPS: Skip PBC overlap due to selected "
+                          "Enrollee match");
+               return 1;
+       }
+#endif /* CONFIG_P2P */
+       return 0;
+}
+
+
 static enum wps_process_res wps_process_m1(struct wps_data *wps,
                                           struct wps_parse_attr *attr)
 {
@@ -2298,9 +2578,13 @@ static enum wps_process_res wps_process_m1(struct wps_data *wps,
 
        if (wps->dev_pw_id < 0x10 &&
            wps->dev_pw_id != DEV_PW_DEFAULT &&
+           wps->dev_pw_id != DEV_PW_P2PS_DEFAULT &&
            wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
            wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
            wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
+#ifdef CONFIG_WPS_NFC
+           wps->dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER &&
+#endif /* CONFIG_WPS_NFC */
            (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
             !wps->wps->registrar->pbc)) {
                wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
@@ -2309,25 +2593,64 @@ static enum wps_process_res wps_process_m1(struct wps_data *wps,
                return WPS_CONTINUE;
        }
 
-#ifdef CONFIG_WPS_OOB
-       if (wps->dev_pw_id >= 0x10 &&
-           wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
-               wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
-                          "%d mismatch", wps->dev_pw_id);
-               wps->state = SEND_M2D;
-               return WPS_CONTINUE;
+#ifdef CONFIG_WPS_NFC
+       if (wps->dev_pw_id >= 0x10 ||
+           wps->dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
+               struct wps_nfc_pw_token *token;
+               const u8 *addr[1];
+               u8 hash[WPS_HASH_LEN];
+
+               wpa_printf(MSG_DEBUG, "WPS: Searching for NFC token match for id=%d (ctx %p registrar %p)",
+                          wps->dev_pw_id, wps->wps, wps->wps->registrar);
+               token = wps_get_nfc_pw_token(
+                       &wps->wps->registrar->nfc_pw_tokens, wps->dev_pw_id);
+               if (token && token->peer_pk_hash_known) {
+                       size_t len;
+
+                       wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
+                                  "Password Token");
+                       dl_list_del(&token->list);
+                       wps->nfc_pw_token = token;
+
+                       addr[0] = attr->public_key;
+                       len = attr->public_key_len;
+                       sha256_vector(1, addr, &len, hash);
+                       if (os_memcmp_const(hash,
+                                           wps->nfc_pw_token->pubkey_hash,
+                                           WPS_OOB_PUBKEY_HASH_LEN) != 0) {
+                               wpa_printf(MSG_ERROR, "WPS: Public Key hash "
+                                          "mismatch");
+                               wps->state = SEND_M2D;
+                               wps->config_error =
+                                       WPS_CFG_PUBLIC_KEY_HASH_MISMATCH;
+                               return WPS_CONTINUE;
+                       }
+               } else if (token) {
+                       wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
+                                  "Password Token (no peer PK hash)");
+                       wps->nfc_pw_token = token;
+               } else if (wps->dev_pw_id >= 0x10 &&
+                          wps->wps->ap_nfc_dev_pw_id == wps->dev_pw_id &&
+                          wps->wps->ap_nfc_dev_pw) {
+                       wpa_printf(MSG_DEBUG, "WPS: Found match with own NFC Password Token");
+               }
        }
-#endif /* CONFIG_WPS_OOB */
+#endif /* CONFIG_WPS_NFC */
 
        if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
-               if (wps->wps->registrar->force_pbc_overlap ||
-                   wps_registrar_pbc_overlap(wps->wps->registrar,
-                                             wps->mac_addr_e, wps->uuid_e)) {
+               if ((wps->wps->registrar->force_pbc_overlap ||
+                    wps_registrar_pbc_overlap(wps->wps->registrar,
+                                              wps->mac_addr_e, wps->uuid_e) ||
+                    !wps_registrar_p2p_dev_addr_match(wps)) &&
+                   !wps_registrar_skip_overlap(wps)) {
                        wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
                                   "negotiation");
                        wps->state = SEND_M2D;
                        wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
                        wps_pbc_overlap_event(wps->wps);
+                       wps_fail_event(wps->wps, WPS_M1,
+                                      WPS_CFG_MULTIPLE_PBC_DETECTED,
+                                      WPS_EI_NO_ERROR, wps->mac_addr_e);
                        wps->wps->registrar->force_pbc_overlap = 1;
                        return WPS_CONTINUE;
                }
@@ -2371,7 +2694,8 @@ static enum wps_process_res wps_process_m3(struct wps_data *wps,
                return WPS_CONTINUE;
        }
 
-       if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
+       if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
+           !wps_registrar_skip_overlap(wps)) {
                wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
                           "session overlap");
                wps->state = SEND_WSC_NACK;
@@ -2408,7 +2732,8 @@ static enum wps_process_res wps_process_m5(struct wps_data *wps,
                return WPS_CONTINUE;
        }
 
-       if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
+       if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
+           !wps_registrar_skip_overlap(wps)) {
                wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
                           "session overlap");
                wps->state = SEND_WSC_NACK;
@@ -2491,6 +2816,8 @@ static void wps_cred_update(struct wps_credential *dst,
 static int wps_process_ap_settings_r(struct wps_data *wps,
                                     struct wps_parse_attr *attr)
 {
+       struct wpabuf *msg;
+
        if (wps->wps->ap || wps->er)
                return 0;
 
@@ -2517,12 +2844,24 @@ static int wps_process_ap_settings_r(struct wps_data *wps,
                 */
                wps_registrar_pin_completed(wps->wps->registrar);
 
+               msg = wps_build_ap_cred(wps);
+               if (msg == NULL)
+                       return -1;
+               wps->cred.cred_attr = wpabuf_head(msg);
+               wps->cred.cred_attr_len = wpabuf_len(msg);
+
                if (wps->ap_settings_cb) {
                        wps->ap_settings_cb(wps->ap_settings_cb_ctx,
                                            &wps->cred);
+                       wpabuf_free(msg);
                        return 1;
                }
                wps_sta_cred_cb(wps);
+
+               wps->cred.cred_attr = NULL;
+               wps->cred.cred_attr_len = 0;
+               wpabuf_free(msg);
+
                return 1;
        }
 }
@@ -2544,7 +2883,8 @@ static enum wps_process_res wps_process_m7(struct wps_data *wps,
                return WPS_CONTINUE;
        }
 
-       if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
+       if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
+           !wps_registrar_skip_overlap(wps)) {
                wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
                           "session overlap");
                wps->state = SEND_WSC_NACK;
@@ -2605,13 +2945,14 @@ static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
 
        if (attr.msg_type == NULL) {
                wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
-               return WPS_FAILURE;
+               wps->state = SEND_WSC_NACK;
+               return WPS_CONTINUE;
        }
 
        if (*attr.msg_type != WPS_M1 &&
            (attr.registrar_nonce == NULL ||
             os_memcmp(wps->nonce_r, attr.registrar_nonce,
-                      WPS_NONCE_LEN != 0))) {
+                      WPS_NONCE_LEN) != 0)) {
                wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
                return WPS_FAILURE;
        }
@@ -2638,21 +2979,24 @@ static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
                        return WPS_FAILURE;
                ret = wps_process_m3(wps, msg, &attr);
                if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
-                       wps_fail_event(wps->wps, WPS_M3, wps->config_error);
+                       wps_fail_event(wps->wps, WPS_M3, wps->config_error,
+                                      wps->error_indication, wps->mac_addr_e);
                break;
        case WPS_M5:
                if (wps_validate_m5(msg) < 0)
                        return WPS_FAILURE;
                ret = wps_process_m5(wps, msg, &attr);
                if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
-                       wps_fail_event(wps->wps, WPS_M5, wps->config_error);
+                       wps_fail_event(wps->wps, WPS_M5, wps->config_error,
+                                      wps->error_indication, wps->mac_addr_e);
                break;
        case WPS_M7:
                if (wps_validate_m7(msg) < 0)
                        return WPS_FAILURE;
                ret = wps_process_m7(wps, msg, &attr);
                if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
-                       wps_fail_event(wps->wps, WPS_M7, wps->config_error);
+                       wps_fail_event(wps->wps, WPS_M7, wps->config_error,
+                                      wps->error_indication, wps->mac_addr_e);
                break;
        default:
                wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
@@ -2704,14 +3048,14 @@ static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
 #endif /* CONFIG_WPS_UPNP */
 
        if (attr.registrar_nonce == NULL ||
-           os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
+           os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
        {
                wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
                return WPS_FAILURE;
        }
 
        if (attr.enrollee_nonce == NULL ||
-           os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
+           os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
                wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
                return WPS_FAILURE;
        }
@@ -2773,14 +3117,14 @@ static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
 #endif /* CONFIG_WPS_UPNP */
 
        if (attr.registrar_nonce == NULL ||
-           os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
+           os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
        {
                wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
                return WPS_FAILURE;
        }
 
        if (attr.enrollee_nonce == NULL ||
-           os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
+           os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
                wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
                return WPS_FAILURE;
        }
@@ -2797,16 +3141,20 @@ static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
 
        switch (old_state) {
        case RECV_M3:
-               wps_fail_event(wps->wps, WPS_M2, config_error);
+               wps_fail_event(wps->wps, WPS_M2, config_error,
+                              wps->error_indication, wps->mac_addr_e);
                break;
        case RECV_M5:
-               wps_fail_event(wps->wps, WPS_M4, config_error);
+               wps_fail_event(wps->wps, WPS_M4, config_error,
+                              wps->error_indication, wps->mac_addr_e);
                break;
        case RECV_M7:
-               wps_fail_event(wps->wps, WPS_M6, config_error);
+               wps_fail_event(wps->wps, WPS_M6, config_error,
+                              wps->error_indication, wps->mac_addr_e);
                break;
        case RECV_DONE:
-               wps_fail_event(wps->wps, WPS_M8, config_error);
+               wps_fail_event(wps->wps, WPS_M8, config_error,
+                              wps->error_indication, wps->mac_addr_e);
                break;
        default:
                break;
@@ -2855,14 +3203,14 @@ static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
 #endif /* CONFIG_WPS_UPNP */
 
        if (attr.registrar_nonce == NULL ||
-           os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
+           os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
        {
                wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
                return WPS_FAILURE;
        }
 
        if (attr.enrollee_nonce == NULL ||
-           os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
+           os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
                wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
                return WPS_FAILURE;
        }
@@ -2881,8 +3229,13 @@ static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
                os_memset(&cred, 0, sizeof(cred));
                os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
                cred.ssid_len = wps->wps->ssid_len;
-               cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
-               cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
+               if (wps->wps->rf_band_cb(wps->wps->cb_ctx) == WPS_RF_60GHZ) {
+                       cred.auth_type = WPS_AUTH_WPA2PSK;
+                       cred.encr_type = WPS_ENCR_AES;
+               } else {
+                       cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
+                       cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
+               }
                os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
                cred.key_len = wps->new_psk_len;
 
@@ -2902,7 +3255,8 @@ static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
 
        if (wps->new_psk) {
                if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
-                                  wps->new_psk, wps->new_psk_len)) {
+                                  wps->p2p_dev_addr, wps->new_psk,
+                                  wps->new_psk_len)) {
                        wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
                                   "new PSK");
                }
@@ -2910,19 +3264,26 @@ static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
                wps->new_psk = NULL;
        }
 
-       wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
+       wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e,
+                          wps->dev_password, wps->dev_password_len);
 
        if (wps->pbc) {
                wps_registrar_remove_pbc_session(wps->wps->registrar,
-                                                wps->mac_addr_e, wps->uuid_e);
+                                                wps->uuid_e,
+                                                wps->p2p_dev_addr);
                wps_registrar_pbc_completed(wps->wps->registrar);
+#ifdef WPS_WORKAROUNDS
+               os_get_reltime(&wps->wps->registrar->pbc_ignore_start);
+#endif /* WPS_WORKAROUNDS */
+               os_memcpy(wps->wps->registrar->pbc_ignore_uuid, wps->uuid_e,
+                         WPS_UUID_LEN);
        } else {
                wps_registrar_pin_completed(wps->wps->registrar);
        }
        /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
         * merge them into APs own list.. */
 
-       wps_success_event(wps->wps);
+       wps_success_event(wps->wps, wps->mac_addr_e);
 
        return WPS_DONE;
 }
@@ -2990,7 +3351,8 @@ enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
                if (ret == WPS_FAILURE) {
                        wps->state = SEND_WSC_NACK;
                        wps_fail_event(wps->wps, WPS_WSC_DONE,
-                                      wps->config_error);
+                                      wps->config_error,
+                                      wps->error_indication, wps->mac_addr_e);
                }
                return ret;
        default:
@@ -3015,7 +3377,7 @@ static void wps_registrar_set_selected_timeout(void *eloop_ctx,
                   "unselect internal Registrar");
        reg->selected_registrar = 0;
        reg->pbc = 0;
-       wps_registrar_selected_registrar_changed(reg);
+       wps_registrar_selected_registrar_changed(reg, 0);
 }
 
 
@@ -3087,7 +3449,8 @@ static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
  * This function is called when selected registrar state changes, e.g., when an
  * AP receives a SetSelectedRegistrar UPnP message.
  */
-void wps_registrar_selected_registrar_changed(struct wps_registrar *reg)
+void wps_registrar_selected_registrar_changed(struct wps_registrar *reg,
+                                             u16 dev_pw_id)
 {
        wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
 
@@ -3103,15 +3466,14 @@ void wps_registrar_selected_registrar_changed(struct wps_registrar *reg)
                u16 methods;
 
                methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
-#ifdef CONFIG_WPS2
                methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
                             WPS_CONFIG_PHY_PUSHBUTTON);
-#endif /* CONFIG_WPS2 */
                if (reg->pbc) {
                        reg->sel_reg_dev_password_id_override =
                                DEV_PW_PUSHBUTTON;
                        wps_set_pushbutton(&methods, reg->wps->config_methods);
-               }
+               } else if (dev_pw_id)
+                       reg->sel_reg_dev_password_id_override = dev_pw_id;
                wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
                           "(pbc=%d)", reg->pbc);
                reg->sel_reg_config_methods_override = methods;
@@ -3155,9 +3517,154 @@ int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
                          d->dev.model_name ? d->dev.model_name : "",
                          d->dev.model_number ? d->dev.model_number : "",
                          d->dev.serial_number ? d->dev.serial_number : "");
-       if (ret < 0 || (size_t) ret >= buflen - len)
+       if (os_snprintf_error(buflen - len, ret))
                return len;
        len += ret;
 
        return len;
 }
+
+
+int wps_registrar_config_ap(struct wps_registrar *reg,
+                           struct wps_credential *cred)
+{
+       wpa_printf(MSG_DEBUG, "WPS: encr_type=0x%x", cred->encr_type);
+       if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
+                                WPS_ENCR_AES))) {
+               if (cred->encr_type & WPS_ENCR_WEP) {
+                       wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
+                                  "due to WEP configuration");
+                       return -1;
+               }
+
+               wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
+                          "invalid encr_type 0x%x", cred->encr_type);
+               return -1;
+       }
+
+       if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
+           WPS_ENCR_TKIP) {
+               wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
+                          "TKIP+AES");
+               cred->encr_type |= WPS_ENCR_AES;
+       }
+
+       if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
+           WPS_AUTH_WPAPSK) {
+               wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
+                          "WPAPSK+WPA2PSK");
+               cred->auth_type |= WPS_AUTH_WPA2PSK;
+       }
+
+       if (reg->wps->cred_cb)
+               return reg->wps->cred_cb(reg->wps->cb_ctx, cred);
+
+       return -1;
+}
+
+
+#ifdef CONFIG_WPS_NFC
+
+int wps_registrar_add_nfc_pw_token(struct wps_registrar *reg,
+                                  const u8 *pubkey_hash, u16 pw_id,
+                                  const u8 *dev_pw, size_t dev_pw_len,
+                                  int pk_hash_provided_oob)
+{
+       struct wps_nfc_pw_token *token;
+
+       if (dev_pw_len > WPS_OOB_DEVICE_PASSWORD_LEN)
+               return -1;
+
+       if (pw_id == DEV_PW_NFC_CONNECTION_HANDOVER &&
+           (pubkey_hash == NULL || !pk_hash_provided_oob)) {
+               wpa_printf(MSG_DEBUG, "WPS: Unexpected NFC Password Token "
+                          "addition - missing public key hash");
+               return -1;
+       }
+
+       wps_free_nfc_pw_tokens(&reg->nfc_pw_tokens, pw_id);
+
+       token = os_zalloc(sizeof(*token));
+       if (token == NULL)
+               return -1;
+
+       token->peer_pk_hash_known = pubkey_hash != NULL;
+       if (pubkey_hash)
+               os_memcpy(token->pubkey_hash, pubkey_hash,
+                         WPS_OOB_PUBKEY_HASH_LEN);
+       token->pw_id = pw_id;
+       token->pk_hash_provided_oob = pk_hash_provided_oob;
+       if (dev_pw) {
+               wpa_snprintf_hex_uppercase((char *) token->dev_pw,
+                                          sizeof(token->dev_pw),
+                                          dev_pw, dev_pw_len);
+               token->dev_pw_len = dev_pw_len * 2;
+       }
+
+       dl_list_add(&reg->nfc_pw_tokens, &token->list);
+
+       reg->selected_registrar = 1;
+       reg->pbc = 0;
+       wps_registrar_add_authorized_mac(reg,
+                                        (u8 *) "\xff\xff\xff\xff\xff\xff");
+       wps_registrar_selected_registrar_changed(reg, pw_id);
+       eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
+       eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
+                              wps_registrar_set_selected_timeout,
+                              reg, NULL);
+
+       wpa_printf(MSG_DEBUG, "WPS: Added NFC Device Password %u to Registrar",
+                  pw_id);
+
+       return 0;
+}
+
+
+int wps_registrar_add_nfc_password_token(struct wps_registrar *reg,
+                                        const u8 *oob_dev_pw,
+                                        size_t oob_dev_pw_len)
+{
+       const u8 *pos, *hash, *dev_pw;
+       u16 id;
+       size_t dev_pw_len;
+
+       if (oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2 ||
+           oob_dev_pw_len > WPS_OOB_PUBKEY_HASH_LEN + 2 +
+           WPS_OOB_DEVICE_PASSWORD_LEN)
+               return -1;
+
+       hash = oob_dev_pw;
+       pos = oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN;
+       id = WPA_GET_BE16(pos);
+       dev_pw = pos + 2;
+       dev_pw_len = oob_dev_pw + oob_dev_pw_len - dev_pw;
+
+       wpa_printf(MSG_DEBUG, "WPS: Add NFC Password Token for Password ID %u",
+                  id);
+
+       wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
+                   hash, WPS_OOB_PUBKEY_HASH_LEN);
+       wpa_hexdump_key(MSG_DEBUG, "WPS: Device Password", dev_pw, dev_pw_len);
+
+       return wps_registrar_add_nfc_pw_token(reg, hash, id, dev_pw,
+                                             dev_pw_len, 0);
+}
+
+
+void wps_registrar_remove_nfc_pw_token(struct wps_registrar *reg,
+                                      struct wps_nfc_pw_token *token)
+{
+       wps_registrar_remove_authorized_mac(reg,
+                                           (u8 *) "\xff\xff\xff\xff\xff\xff");
+       wps_registrar_selected_registrar_changed(reg, 0);
+
+       /*
+        * Free the NFC password token if it was used only for a single protocol
+        * run. The static handover case uses the same password token multiple
+        * times, so do not free that case here.
+        */
+       if (token->peer_pk_hash_known)
+               os_free(token);
+}
+
+#endif /* CONFIG_WPS_NFC */