2 * Wi-Fi Protected Setup - Registrar
3 * Copyright (c) 2008-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/base64.h"
13 #include "utils/eloop.h"
14 #include "utils/uuid.h"
15 #include "utils/list.h"
16 #include "crypto/crypto.h"
17 #include "crypto/sha256.h"
18 #include "crypto/random.h"
19 #include "common/ieee802_11_defs.h"
21 #include "wps_dev_attr.h"
23 #include "wps_upnp_i.h"
25 #ifndef CONFIG_WPS_STRICT
26 #define WPS_WORKAROUNDS
27 #endif /* CONFIG_WPS_STRICT */
31 struct wps_nfc_pw_token {
33 u8 pubkey_hash[WPS_OOB_PUBKEY_HASH_LEN];
34 unsigned int peer_pk_hash_known:1;
36 u8 dev_pw[WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1];
38 int pk_hash_provided_oob; /* whether own PK hash was provided OOB */
42 static void wps_remove_nfc_pw_token(struct wps_nfc_pw_token *token)
44 dl_list_del(&token->list);
49 static void wps_free_nfc_pw_tokens(struct dl_list *tokens, u16 pw_id)
51 struct wps_nfc_pw_token *token, *prev;
52 dl_list_for_each_safe(token, prev, tokens, struct wps_nfc_pw_token,
54 if (pw_id == 0 || pw_id == token->pw_id)
55 wps_remove_nfc_pw_token(token);
60 static struct wps_nfc_pw_token * wps_get_nfc_pw_token(struct dl_list *tokens,
63 struct wps_nfc_pw_token *token;
64 dl_list_for_each(token, tokens, struct wps_nfc_pw_token, list) {
65 if (pw_id == token->pw_id)
71 #else /* CONFIG_WPS_NFC */
73 #define wps_free_nfc_pw_tokens(t, p) do { } while (0)
75 #endif /* CONFIG_WPS_NFC */
80 u8 uuid[WPS_UUID_LEN];
84 #define PIN_LOCKED BIT(0)
85 #define PIN_EXPIRES BIT(1)
87 struct os_reltime expiration;
88 u8 enrollee_addr[ETH_ALEN];
92 static void wps_free_pin(struct wps_uuid_pin *pin)
99 static void wps_remove_pin(struct wps_uuid_pin *pin)
101 dl_list_del(&pin->list);
106 static void wps_free_pins(struct dl_list *pins)
108 struct wps_uuid_pin *pin, *prev;
109 dl_list_for_each_safe(pin, prev, pins, struct wps_uuid_pin, list)
114 struct wps_pbc_session {
115 struct wps_pbc_session *next;
117 u8 uuid_e[WPS_UUID_LEN];
118 struct os_reltime timestamp;
122 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
124 struct wps_pbc_session *prev;
134 struct wps_registrar_device {
135 struct wps_registrar_device *next;
136 struct wps_device_data dev;
137 u8 uuid[WPS_UUID_LEN];
141 struct wps_registrar {
142 struct wps_context *wps;
145 int selected_registrar;
147 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
148 const u8 *psk, size_t psk_len);
149 int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
150 struct wpabuf *probe_resp_ie);
151 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
152 const struct wps_device_data *dev);
153 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
154 const u8 *uuid_e, const u8 *dev_pw,
156 void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
157 u16 sel_reg_config_methods);
158 void (*enrollee_seen_cb)(void *ctx, const u8 *addr, const u8 *uuid_e,
159 const u8 *pri_dev_type, u16 config_methods,
160 u16 dev_password_id, u8 request_type,
161 const char *dev_name);
165 struct dl_list nfc_pw_tokens;
166 struct wps_pbc_session *pbc_sessions;
169 struct wpabuf *extra_cred;
170 int disable_auto_conf;
172 int sel_reg_dev_password_id_override;
173 int sel_reg_config_methods_override;
176 int force_per_enrollee_psk;
178 struct wps_registrar_device *devices;
180 int force_pbc_overlap;
182 u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
183 u8 authorized_macs_union[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
185 u8 p2p_dev_addr[ETH_ALEN];
187 u8 pbc_ignore_uuid[WPS_UUID_LEN];
188 #ifdef WPS_WORKAROUNDS
189 struct os_reltime pbc_ignore_start;
190 #endif /* WPS_WORKAROUNDS */
194 static int wps_set_ie(struct wps_registrar *reg);
195 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
196 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
198 static void wps_registrar_remove_pin(struct wps_registrar *reg,
199 struct wps_uuid_pin *pin);
202 static void wps_registrar_add_authorized_mac(struct wps_registrar *reg,
206 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC " MACSTR,
208 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
209 if (os_memcmp(reg->authorized_macs[i], addr, ETH_ALEN) == 0) {
210 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was "
211 "already in the list");
212 return; /* already in list */
214 for (i = WPS_MAX_AUTHORIZED_MACS - 1; i > 0; i--)
215 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i - 1],
217 os_memcpy(reg->authorized_macs[0], addr, ETH_ALEN);
218 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
219 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
223 static void wps_registrar_remove_authorized_mac(struct wps_registrar *reg,
227 wpa_printf(MSG_DEBUG, "WPS: Remove authorized MAC " MACSTR,
229 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++) {
230 if (os_memcmp(reg->authorized_macs, addr, ETH_ALEN) == 0)
233 if (i == WPS_MAX_AUTHORIZED_MACS) {
234 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was not in the "
236 return; /* not in the list */
238 for (; i + 1 < WPS_MAX_AUTHORIZED_MACS; i++)
239 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i + 1],
241 os_memset(reg->authorized_macs[WPS_MAX_AUTHORIZED_MACS - 1], 0,
243 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
244 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
248 static void wps_free_devices(struct wps_registrar_device *dev)
250 struct wps_registrar_device *prev;
255 wps_device_data_free(&prev->dev);
261 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
264 struct wps_registrar_device *dev;
266 for (dev = reg->devices; dev; dev = dev->next) {
267 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
274 static void wps_device_clone_data(struct wps_device_data *dst,
275 struct wps_device_data *src)
277 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
278 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
280 #define WPS_STRDUP(n) \
282 dst->n = src->n ? os_strdup(src->n) : NULL
284 WPS_STRDUP(device_name);
285 WPS_STRDUP(manufacturer);
286 WPS_STRDUP(model_name);
287 WPS_STRDUP(model_number);
288 WPS_STRDUP(serial_number);
293 int wps_device_store(struct wps_registrar *reg,
294 struct wps_device_data *dev, const u8 *uuid)
296 struct wps_registrar_device *d;
298 d = wps_device_get(reg, dev->mac_addr);
300 d = os_zalloc(sizeof(*d));
303 d->next = reg->devices;
307 wps_device_clone_data(&d->dev, dev);
308 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
314 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
315 const u8 *addr, const u8 *uuid_e)
317 struct wps_pbc_session *pbc, *prev = NULL;
318 struct os_reltime now;
320 os_get_reltime(&now);
322 pbc = reg->pbc_sessions;
324 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
325 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
327 prev->next = pbc->next;
329 reg->pbc_sessions = pbc->next;
337 pbc = os_zalloc(sizeof(*pbc));
340 os_memcpy(pbc->addr, addr, ETH_ALEN);
342 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
345 pbc->next = reg->pbc_sessions;
346 reg->pbc_sessions = pbc;
347 pbc->timestamp = now;
349 /* remove entries that have timed out */
354 if (os_reltime_expired(&now, &pbc->timestamp,
355 WPS_PBC_WALK_TIME)) {
357 wps_free_pbc_sessions(pbc);
366 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
368 const u8 *p2p_dev_addr)
370 struct wps_pbc_session *pbc, *prev = NULL, *tmp;
372 pbc = reg->pbc_sessions;
374 if (os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0 ||
375 (p2p_dev_addr && !is_zero_ether_addr(reg->p2p_dev_addr) &&
376 os_memcmp(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
379 prev->next = pbc->next;
381 reg->pbc_sessions = pbc->next;
384 wpa_printf(MSG_DEBUG, "WPS: Removing PBC session for "
385 "addr=" MACSTR, MAC2STR(tmp->addr));
386 wpa_hexdump(MSG_DEBUG, "WPS: Removed UUID-E",
387 tmp->uuid_e, WPS_UUID_LEN);
397 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
398 const u8 *addr, const u8 *uuid_e)
401 struct wps_pbc_session *pbc;
402 struct wps_pbc_session *first = NULL;
403 struct os_reltime now;
405 os_get_reltime(&now);
407 wpa_printf(MSG_DEBUG, "WPS: Checking active PBC sessions for overlap");
410 wpa_printf(MSG_DEBUG, "WPS: Add one for the requested UUID");
411 wpa_hexdump(MSG_DEBUG, "WPS: Requested UUID",
412 uuid_e, WPS_UUID_LEN);
416 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
417 wpa_printf(MSG_DEBUG, "WPS: Consider PBC session with " MACSTR,
419 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E",
420 pbc->uuid_e, WPS_UUID_LEN);
421 if (os_reltime_expired(&now, &pbc->timestamp,
422 WPS_PBC_WALK_TIME)) {
423 wpa_printf(MSG_DEBUG, "WPS: PBC walk time has expired");
427 os_memcmp(pbc->uuid_e, first->uuid_e, WPS_UUID_LEN) == 0) {
428 wpa_printf(MSG_DEBUG, "WPS: Same Enrollee");
429 continue; /* same Enrollee */
431 if (uuid_e == NULL ||
432 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN)) {
433 wpa_printf(MSG_DEBUG, "WPS: New Enrollee");
440 wpa_printf(MSG_DEBUG, "WPS: %u active PBC session(s) found", count);
442 return count > 1 ? 1 : 0;
446 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
448 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
450 wpabuf_put_be16(msg, ATTR_WPS_STATE);
451 wpabuf_put_be16(msg, 1);
452 wpabuf_put_u8(msg, wps->wps_state);
457 #ifdef CONFIG_WPS_UPNP
458 static void wps_registrar_free_pending_m2(struct wps_context *wps)
460 struct upnp_pending_message *p, *p2, *prev = NULL;
463 if (p->type == WPS_M2 || p->type == WPS_M2D) {
465 wps->upnp_msgs = p->next;
467 prev->next = p->next;
468 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
471 wpabuf_free(p2->msg);
479 #endif /* CONFIG_WPS_UPNP */
482 static int wps_build_ap_setup_locked(struct wps_context *wps,
485 if (wps->ap_setup_locked && wps->ap_setup_locked != 2) {
486 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
487 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
488 wpabuf_put_be16(msg, 1);
489 wpabuf_put_u8(msg, 1);
495 static int wps_build_selected_registrar(struct wps_registrar *reg,
498 if (!reg->sel_reg_union)
500 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
501 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
502 wpabuf_put_be16(msg, 1);
503 wpabuf_put_u8(msg, 1);
508 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
511 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
512 if (!reg->sel_reg_union)
514 if (reg->sel_reg_dev_password_id_override >= 0)
515 id = reg->sel_reg_dev_password_id_override;
516 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
517 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
518 wpabuf_put_be16(msg, 2);
519 wpabuf_put_be16(msg, id);
524 static int wps_build_sel_pbc_reg_uuid_e(struct wps_registrar *reg,
527 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
528 if (!reg->sel_reg_union)
530 if (reg->sel_reg_dev_password_id_override >= 0)
531 id = reg->sel_reg_dev_password_id_override;
532 if (id != DEV_PW_PUSHBUTTON || !reg->dualband)
534 return wps_build_uuid_e(msg, reg->wps->uuid);
538 static void wps_set_pushbutton(u16 *methods, u16 conf_methods)
540 *methods |= WPS_CONFIG_PUSHBUTTON;
541 if ((conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON) ==
542 WPS_CONFIG_VIRT_PUSHBUTTON)
543 *methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
544 if ((conf_methods & WPS_CONFIG_PHY_PUSHBUTTON) ==
545 WPS_CONFIG_PHY_PUSHBUTTON)
546 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
547 if ((*methods & WPS_CONFIG_VIRT_PUSHBUTTON) !=
548 WPS_CONFIG_VIRT_PUSHBUTTON &&
549 (*methods & WPS_CONFIG_PHY_PUSHBUTTON) !=
550 WPS_CONFIG_PHY_PUSHBUTTON) {
552 * Required to include virtual/physical flag, but we were not
553 * configured with push button type, so have to default to one
556 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
561 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
565 if (!reg->sel_reg_union)
567 methods = reg->wps->config_methods;
568 methods &= ~WPS_CONFIG_PUSHBUTTON;
569 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
570 WPS_CONFIG_PHY_PUSHBUTTON);
572 wps_set_pushbutton(&methods, reg->wps->config_methods);
573 if (reg->sel_reg_config_methods_override >= 0)
574 methods = reg->sel_reg_config_methods_override;
575 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
577 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
578 wpabuf_put_be16(msg, 2);
579 wpabuf_put_be16(msg, methods);
584 static int wps_build_probe_config_methods(struct wps_registrar *reg,
589 * These are the methods that the AP supports as an Enrollee for adding
590 * external Registrars.
592 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
593 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
594 WPS_CONFIG_PHY_PUSHBUTTON);
595 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
596 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
597 wpabuf_put_be16(msg, 2);
598 wpabuf_put_be16(msg, methods);
603 static int wps_build_config_methods_r(struct wps_registrar *reg,
606 return wps_build_config_methods(msg, reg->wps->config_methods);
610 const u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count)
614 while (*count < WPS_MAX_AUTHORIZED_MACS) {
615 if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
620 return (const u8 *) reg->authorized_macs_union;
625 * wps_registrar_init - Initialize WPS Registrar data
626 * @wps: Pointer to longterm WPS context
627 * @cfg: Registrar configuration
628 * Returns: Pointer to allocated Registrar data or %NULL on failure
630 * This function is used to initialize WPS Registrar functionality. It can be
631 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
632 * runs (e.g., when run as an internal Registrar in an AP). Caller is
633 * responsible for freeing the returned data with wps_registrar_deinit() when
634 * Registrar functionality is not needed anymore.
636 struct wps_registrar *
637 wps_registrar_init(struct wps_context *wps,
638 const struct wps_registrar_config *cfg)
640 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
644 dl_list_init(®->pins);
645 dl_list_init(®->nfc_pw_tokens);
647 reg->new_psk_cb = cfg->new_psk_cb;
648 reg->set_ie_cb = cfg->set_ie_cb;
649 reg->pin_needed_cb = cfg->pin_needed_cb;
650 reg->reg_success_cb = cfg->reg_success_cb;
651 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
652 reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
653 reg->cb_ctx = cfg->cb_ctx;
654 reg->skip_cred_build = cfg->skip_cred_build;
655 if (cfg->extra_cred) {
656 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
657 cfg->extra_cred_len);
658 if (reg->extra_cred == NULL) {
663 reg->disable_auto_conf = cfg->disable_auto_conf;
664 reg->sel_reg_dev_password_id_override = -1;
665 reg->sel_reg_config_methods_override = -1;
666 reg->static_wep_only = cfg->static_wep_only;
667 reg->dualband = cfg->dualband;
668 reg->force_per_enrollee_psk = cfg->force_per_enrollee_psk;
670 if (wps_set_ie(reg)) {
671 wps_registrar_deinit(reg);
680 * wps_registrar_deinit - Deinitialize WPS Registrar data
681 * @reg: Registrar data from wps_registrar_init()
683 void wps_registrar_deinit(struct wps_registrar *reg)
687 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
688 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
689 wps_free_pins(®->pins);
690 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, 0);
691 wps_free_pbc_sessions(reg->pbc_sessions);
692 wpabuf_free(reg->extra_cred);
693 wps_free_devices(reg->devices);
698 static void wps_registrar_invalidate_unused(struct wps_registrar *reg)
700 struct wps_uuid_pin *pin;
702 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
703 if (pin->wildcard_uuid == 1 && !(pin->flags & PIN_LOCKED)) {
704 wpa_printf(MSG_DEBUG, "WPS: Invalidate previously "
705 "configured wildcard PIN");
706 wps_registrar_remove_pin(reg, pin);
714 * wps_registrar_add_pin - Configure a new PIN for Registrar
715 * @reg: Registrar data from wps_registrar_init()
716 * @addr: Enrollee MAC address or %NULL if not known
717 * @uuid: UUID-E or %NULL for wildcard (any UUID)
718 * @pin: PIN (Device Password)
719 * @pin_len: Length of pin in octets
720 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
721 * Returns: 0 on success, -1 on failure
723 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
724 const u8 *uuid, const u8 *pin, size_t pin_len,
727 struct wps_uuid_pin *p;
729 p = os_zalloc(sizeof(*p));
733 os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
735 p->wildcard_uuid = 1;
737 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
738 p->pin = os_malloc(pin_len);
739 if (p->pin == NULL) {
743 os_memcpy(p->pin, pin, pin_len);
744 p->pin_len = pin_len;
747 p->flags |= PIN_EXPIRES;
748 os_get_reltime(&p->expiration);
749 p->expiration.sec += timeout;
752 if (p->wildcard_uuid)
753 wps_registrar_invalidate_unused(reg);
755 dl_list_add(®->pins, &p->list);
757 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
759 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
760 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
761 reg->selected_registrar = 1;
764 wps_registrar_add_authorized_mac(reg, addr);
766 wps_registrar_add_authorized_mac(
767 reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
768 wps_registrar_selected_registrar_changed(reg, 0);
769 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
770 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
771 wps_registrar_set_selected_timeout,
778 static void wps_registrar_remove_pin(struct wps_registrar *reg,
779 struct wps_uuid_pin *pin)
782 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
784 if (is_zero_ether_addr(pin->enrollee_addr))
787 addr = pin->enrollee_addr;
788 wps_registrar_remove_authorized_mac(reg, addr);
790 wps_registrar_selected_registrar_changed(reg, 0);
794 static void wps_registrar_expire_pins(struct wps_registrar *reg)
796 struct wps_uuid_pin *pin, *prev;
797 struct os_reltime now;
799 os_get_reltime(&now);
800 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
802 if ((pin->flags & PIN_EXPIRES) &&
803 os_reltime_before(&pin->expiration, &now)) {
804 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
805 pin->uuid, WPS_UUID_LEN);
806 wps_registrar_remove_pin(reg, pin);
813 * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
814 * @reg: Registrar data from wps_registrar_init()
815 * @dev_pw: PIN to search for or %NULL to match any
816 * @dev_pw_len: Length of dev_pw in octets
817 * Returns: 0 on success, -1 if not wildcard PIN is enabled
819 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg,
823 struct wps_uuid_pin *pin, *prev;
825 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
827 if (dev_pw && pin->pin &&
828 (dev_pw_len != pin->pin_len ||
829 os_memcmp(dev_pw, pin->pin, dev_pw_len) != 0))
830 continue; /* different PIN */
831 if (pin->wildcard_uuid) {
832 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
833 pin->uuid, WPS_UUID_LEN);
834 wps_registrar_remove_pin(reg, pin);
844 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
845 * @reg: Registrar data from wps_registrar_init()
847 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
849 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
851 struct wps_uuid_pin *pin, *prev;
853 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
855 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
856 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
857 pin->uuid, WPS_UUID_LEN);
858 wps_registrar_remove_pin(reg, pin);
867 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
868 const u8 *uuid, size_t *pin_len)
870 struct wps_uuid_pin *pin, *found = NULL;
872 wps_registrar_expire_pins(reg);
874 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
875 if (!pin->wildcard_uuid &&
876 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
883 /* Check for wildcard UUIDs since none of the UUID-specific
885 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
886 if (pin->wildcard_uuid == 1 ||
887 pin->wildcard_uuid == 2) {
888 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
889 "PIN. Assigned it for this UUID-E");
890 pin->wildcard_uuid++;
891 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
902 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
903 * that could otherwise avoid PIN invalidations.
905 if (found->flags & PIN_LOCKED) {
906 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
907 "allow concurrent re-use");
910 *pin_len = found->pin_len;
911 found->flags |= PIN_LOCKED;
917 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
918 * @reg: Registrar data from wps_registrar_init()
920 * Returns: 0 on success, -1 on failure
922 * PINs are locked to enforce only one concurrent use. This function unlocks a
923 * PIN to allow it to be used again. If the specified PIN was configured using
924 * a wildcard UUID, it will be removed instead of allowing multiple uses.
926 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
928 struct wps_uuid_pin *pin;
930 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
931 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
932 if (pin->wildcard_uuid == 3) {
933 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
935 return wps_registrar_invalidate_pin(reg, uuid);
937 pin->flags &= ~PIN_LOCKED;
946 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
948 reg->selected_registrar = 0;
950 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
951 wps_registrar_remove_authorized_mac(reg,
952 (u8 *) "\xff\xff\xff\xff\xff\xff");
953 wps_registrar_selected_registrar_changed(reg, 0);
957 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
959 struct wps_registrar *reg = eloop_ctx;
961 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
962 wps_pbc_timeout_event(reg->wps);
963 wps_registrar_stop_pbc(reg);
968 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
969 * @reg: Registrar data from wps_registrar_init()
970 * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
971 * indicates no such filtering
972 * Returns: 0 on success, -1 on failure, -2 on session overlap
974 * This function is called on an AP when a push button is pushed to activate
975 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
976 * or when a PBC registration is completed. If more than one Enrollee in active
977 * PBC mode has been detected during the monitor time (previous 2 minutes), the
978 * PBC mode is not activated and -2 is returned to indicate session overlap.
979 * This is skipped if a specific Enrollee is selected.
981 int wps_registrar_button_pushed(struct wps_registrar *reg,
982 const u8 *p2p_dev_addr)
984 if (p2p_dev_addr == NULL &&
985 wps_registrar_pbc_overlap(reg, NULL, NULL)) {
986 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
988 wps_pbc_overlap_event(reg->wps);
991 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
992 reg->force_pbc_overlap = 0;
993 reg->selected_registrar = 1;
996 os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
998 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
999 wps_registrar_add_authorized_mac(reg,
1000 (u8 *) "\xff\xff\xff\xff\xff\xff");
1001 wps_registrar_selected_registrar_changed(reg, 0);
1003 wps_pbc_active_event(reg->wps);
1004 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1005 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1006 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
1012 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
1014 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
1015 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1016 wps_registrar_stop_pbc(reg);
1017 wps_pbc_disable_event(reg->wps);
1021 static void wps_registrar_pin_completed(struct wps_registrar *reg)
1023 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
1024 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1025 reg->selected_registrar = 0;
1026 wps_registrar_selected_registrar_changed(reg, 0);
1030 void wps_registrar_complete(struct wps_registrar *registrar, const u8 *uuid_e,
1031 const u8 *dev_pw, size_t dev_pw_len)
1033 if (registrar->pbc) {
1034 wps_registrar_remove_pbc_session(registrar,
1036 wps_registrar_pbc_completed(registrar);
1037 #ifdef WPS_WORKAROUNDS
1038 os_get_reltime(®istrar->pbc_ignore_start);
1039 #endif /* WPS_WORKAROUNDS */
1040 os_memcpy(registrar->pbc_ignore_uuid, uuid_e, WPS_UUID_LEN);
1042 wps_registrar_pin_completed(registrar);
1046 wps_registrar_invalidate_wildcard_pin(registrar, dev_pw,
1048 wpa_hexdump_key(MSG_DEBUG, "WPS: Invalidated wildcard PIN",
1049 dev_pw, dev_pw_len);
1054 int wps_registrar_wps_cancel(struct wps_registrar *reg)
1057 wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
1058 wps_registrar_pbc_timeout(reg, NULL);
1059 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1061 } else if (reg->selected_registrar) {
1063 wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
1064 wps_registrar_pin_completed(reg);
1065 wps_registrar_invalidate_wildcard_pin(reg, NULL, 0);
1073 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
1074 * @reg: Registrar data from wps_registrar_init()
1075 * @addr: MAC address of the Probe Request sender
1076 * @wps_data: WPS IE contents
1078 * This function is called on an AP when a Probe Request with WPS IE is
1079 * received. This is used to track PBC mode use and to detect possible overlap
1080 * situation with other WPS APs.
1082 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
1083 const struct wpabuf *wps_data,
1086 struct wps_parse_attr attr;
1089 wpa_hexdump_buf(MSG_MSGDUMP,
1090 "WPS: Probe Request with WPS data received",
1093 if (wps_parse_msg(wps_data, &attr) < 0)
1096 if (attr.config_methods == NULL) {
1097 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
1102 if (attr.dev_password_id == NULL) {
1103 wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
1104 "in Probe Request");
1108 if (reg->enrollee_seen_cb && attr.uuid_e &&
1109 attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
1110 char *dev_name = NULL;
1111 if (attr.dev_name) {
1112 dev_name = os_zalloc(attr.dev_name_len + 1);
1114 os_memcpy(dev_name, attr.dev_name,
1118 reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
1119 attr.primary_dev_type,
1120 WPA_GET_BE16(attr.config_methods),
1121 WPA_GET_BE16(attr.dev_password_id),
1122 *attr.request_type, dev_name);
1126 if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
1127 return; /* Not PBC */
1129 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
1130 MACSTR, MAC2STR(addr));
1131 if (attr.uuid_e == NULL) {
1132 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
1136 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E from Probe Request", attr.uuid_e,
1139 #ifdef WPS_WORKAROUNDS
1140 if (reg->pbc_ignore_start.sec &&
1141 os_memcmp(attr.uuid_e, reg->pbc_ignore_uuid, WPS_UUID_LEN) == 0) {
1142 struct os_reltime now, dur;
1143 os_get_reltime(&now);
1144 os_reltime_sub(&now, ®->pbc_ignore_start, &dur);
1145 if (dur.sec >= 0 && dur.sec < 5) {
1146 wpa_printf(MSG_DEBUG, "WPS: Ignore PBC activation "
1147 "based on Probe Request from the Enrollee "
1148 "that just completed PBC provisioning");
1151 reg->pbc_ignore_start.sec = 0;
1153 #endif /* WPS_WORKAROUNDS */
1156 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
1157 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
1158 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
1159 reg->force_pbc_overlap = 1;
1160 wps_pbc_overlap_event(reg->wps);
1165 int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
1166 const u8 *p2p_dev_addr, const u8 *psk, size_t psk_len)
1168 if (reg->new_psk_cb == NULL)
1171 return reg->new_psk_cb(reg->cb_ctx, mac_addr, p2p_dev_addr, psk,
1176 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
1177 const struct wps_device_data *dev)
1179 if (reg->pin_needed_cb == NULL)
1182 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
1186 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
1187 const u8 *uuid_e, const u8 *dev_pw,
1190 if (reg->reg_success_cb == NULL)
1193 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e, dev_pw, dev_pw_len);
1197 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
1198 struct wpabuf *probe_resp_ie)
1200 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
1204 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
1207 if (reg->set_sel_reg_cb == NULL)
1210 if (reg->selected_registrar) {
1211 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
1212 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
1213 WPS_CONFIG_PHY_PUSHBUTTON);
1215 wps_set_pushbutton(&methods, reg->wps->config_methods);
1218 wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1219 "config_methods=0x%x pbc=%d methods=0x%x",
1220 reg->selected_registrar, reg->wps->config_methods,
1223 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
1224 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
1229 static int wps_set_ie(struct wps_registrar *reg)
1231 struct wpabuf *beacon;
1232 struct wpabuf *probe;
1233 const u8 *auth_macs;
1235 size_t vendor_len = 0;
1238 if (reg->set_ie_cb == NULL)
1241 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
1242 if (reg->wps->dev.vendor_ext[i]) {
1243 vendor_len += 2 + 2;
1244 vendor_len += wpabuf_len(reg->wps->dev.vendor_ext[i]);
1248 beacon = wpabuf_alloc(400 + vendor_len);
1251 probe = wpabuf_alloc(500 + vendor_len);
1252 if (probe == NULL) {
1253 wpabuf_free(beacon);
1257 auth_macs = wps_authorized_macs(reg, &count);
1259 wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
1261 if (wps_build_version(beacon) ||
1262 wps_build_wps_state(reg->wps, beacon) ||
1263 wps_build_ap_setup_locked(reg->wps, beacon) ||
1264 wps_build_selected_registrar(reg, beacon) ||
1265 wps_build_sel_reg_dev_password_id(reg, beacon) ||
1266 wps_build_sel_reg_config_methods(reg, beacon) ||
1267 wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
1268 (reg->dualband && wps_build_rf_bands(®->wps->dev, beacon, 0)) ||
1269 wps_build_wfa_ext(beacon, 0, auth_macs, count) ||
1270 wps_build_vendor_ext(®->wps->dev, beacon)) {
1271 wpabuf_free(beacon);
1277 if (wps_build_dev_name(®->wps->dev, beacon) ||
1278 wps_build_primary_dev_type(®->wps->dev, beacon)) {
1279 wpabuf_free(beacon);
1283 #endif /* CONFIG_P2P */
1285 wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
1287 if (wps_build_version(probe) ||
1288 wps_build_wps_state(reg->wps, probe) ||
1289 wps_build_ap_setup_locked(reg->wps, probe) ||
1290 wps_build_selected_registrar(reg, probe) ||
1291 wps_build_sel_reg_dev_password_id(reg, probe) ||
1292 wps_build_sel_reg_config_methods(reg, probe) ||
1293 wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
1294 WPS_RESP_REGISTRAR) ||
1295 wps_build_uuid_e(probe, reg->wps->uuid) ||
1296 wps_build_device_attrs(®->wps->dev, probe) ||
1297 wps_build_probe_config_methods(reg, probe) ||
1298 (reg->dualband && wps_build_rf_bands(®->wps->dev, probe, 0)) ||
1299 wps_build_wfa_ext(probe, 0, auth_macs, count) ||
1300 wps_build_vendor_ext(®->wps->dev, probe)) {
1301 wpabuf_free(beacon);
1306 beacon = wps_ie_encapsulate(beacon);
1307 probe = wps_ie_encapsulate(probe);
1309 if (!beacon || !probe) {
1310 wpabuf_free(beacon);
1315 if (reg->static_wep_only) {
1317 * Windows XP and Vista clients can get confused about
1318 * EAP-Identity/Request when they probe the network with
1319 * EAPOL-Start. In such a case, they may assume the network is
1320 * using IEEE 802.1X and prompt user for a certificate while
1321 * the correct (non-WPS) behavior would be to ask for the
1322 * static WEP key. As a workaround, use Microsoft Provisioning
1323 * IE to advertise that legacy 802.1X is not supported.
1325 const u8 ms_wps[7] = {
1326 WLAN_EID_VENDOR_SPECIFIC, 5,
1327 /* Microsoft Provisioning IE (00:50:f2:5) */
1328 0x00, 0x50, 0xf2, 5,
1329 0x00 /* no legacy 802.1X or MS WPS */
1331 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
1332 "into Beacon/Probe Response frames");
1333 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
1334 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
1337 return wps_cb_set_ie(reg, beacon, probe);
1341 static int wps_get_dev_password(struct wps_data *wps)
1346 os_free(wps->dev_password);
1347 wps->dev_password = NULL;
1350 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
1351 pin = (const u8 *) "00000000";
1353 #ifdef CONFIG_WPS_NFC
1354 } else if (wps->nfc_pw_token) {
1355 if (wps->nfc_pw_token->pw_id == DEV_PW_NFC_CONNECTION_HANDOVER)
1357 wpa_printf(MSG_DEBUG, "WPS: Using NFC connection "
1358 "handover and abbreviated WPS handshake "
1359 "without Device Password");
1362 wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from NFC "
1364 pin = wps->nfc_pw_token->dev_pw;
1365 pin_len = wps->nfc_pw_token->dev_pw_len;
1366 } else if (wps->dev_pw_id >= 0x10 &&
1367 wps->wps->ap_nfc_dev_pw_id == wps->dev_pw_id &&
1368 wps->wps->ap_nfc_dev_pw) {
1369 wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from own NFC Password Token");
1370 pin = wpabuf_head(wps->wps->ap_nfc_dev_pw);
1371 pin_len = wpabuf_len(wps->wps->ap_nfc_dev_pw);
1372 #endif /* CONFIG_WPS_NFC */
1374 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1376 if (pin && wps->dev_pw_id >= 0x10) {
1377 wpa_printf(MSG_DEBUG, "WPS: No match for OOB Device "
1378 "Password ID, but PIN found");
1380 * See whether Enrollee is willing to use PIN instead.
1382 wps->dev_pw_id = DEV_PW_DEFAULT;
1386 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1387 "the Enrollee (context %p registrar %p)",
1388 wps->wps, wps->wps->registrar);
1389 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1394 wps->dev_password = os_malloc(pin_len);
1395 if (wps->dev_password == NULL)
1397 os_memcpy(wps->dev_password, pin, pin_len);
1398 wps->dev_password_len = pin_len;
1404 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1406 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1407 wpabuf_put_be16(msg, ATTR_UUID_R);
1408 wpabuf_put_be16(msg, WPS_UUID_LEN);
1409 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1414 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1420 if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1422 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1423 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1424 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1426 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1427 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1428 "R-Hash derivation");
1432 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1433 wpabuf_put_be16(msg, ATTR_R_HASH1);
1434 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1435 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1436 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1437 addr[0] = wps->snonce;
1438 len[0] = WPS_SECRET_NONCE_LEN;
1439 addr[1] = wps->psk1;
1440 len[1] = WPS_PSK_LEN;
1441 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1442 len[2] = wpabuf_len(wps->dh_pubkey_e);
1443 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1444 len[3] = wpabuf_len(wps->dh_pubkey_r);
1445 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1446 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1448 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1449 wpabuf_put_be16(msg, ATTR_R_HASH2);
1450 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1451 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1452 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1453 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1454 addr[1] = wps->psk2;
1455 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1456 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1462 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1464 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1465 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1466 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1467 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1472 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1474 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1475 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1476 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1477 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1478 WPS_SECRET_NONCE_LEN);
1483 static int wps_build_cred_network_idx(struct wpabuf *msg,
1484 const struct wps_credential *cred)
1486 wpa_printf(MSG_DEBUG, "WPS: * Network Index (1)");
1487 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1488 wpabuf_put_be16(msg, 1);
1489 wpabuf_put_u8(msg, 1);
1494 static int wps_build_cred_ssid(struct wpabuf *msg,
1495 const struct wps_credential *cred)
1497 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1498 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID for Credential",
1499 cred->ssid, cred->ssid_len);
1500 wpabuf_put_be16(msg, ATTR_SSID);
1501 wpabuf_put_be16(msg, cred->ssid_len);
1502 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1507 static int wps_build_cred_auth_type(struct wpabuf *msg,
1508 const struct wps_credential *cred)
1510 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1512 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1513 wpabuf_put_be16(msg, 2);
1514 wpabuf_put_be16(msg, cred->auth_type);
1519 static int wps_build_cred_encr_type(struct wpabuf *msg,
1520 const struct wps_credential *cred)
1522 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1524 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1525 wpabuf_put_be16(msg, 2);
1526 wpabuf_put_be16(msg, cred->encr_type);
1531 static int wps_build_cred_network_key(struct wpabuf *msg,
1532 const struct wps_credential *cred)
1534 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1535 (int) cred->key_len);
1536 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
1537 cred->key, cred->key_len);
1538 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1539 wpabuf_put_be16(msg, cred->key_len);
1540 wpabuf_put_data(msg, cred->key, cred->key_len);
1545 static int wps_build_credential(struct wpabuf *msg,
1546 const struct wps_credential *cred)
1548 if (wps_build_cred_network_idx(msg, cred) ||
1549 wps_build_cred_ssid(msg, cred) ||
1550 wps_build_cred_auth_type(msg, cred) ||
1551 wps_build_cred_encr_type(msg, cred) ||
1552 wps_build_cred_network_key(msg, cred) ||
1553 wps_build_mac_addr(msg, cred->mac_addr))
1559 int wps_build_credential_wrap(struct wpabuf *msg,
1560 const struct wps_credential *cred)
1562 struct wpabuf *wbuf;
1563 wbuf = wpabuf_alloc(200);
1566 if (wps_build_credential(wbuf, cred)) {
1570 wpabuf_put_be16(msg, ATTR_CRED);
1571 wpabuf_put_be16(msg, wpabuf_len(wbuf));
1572 wpabuf_put_buf(msg, wbuf);
1578 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1580 struct wpabuf *cred;
1582 if (wps->wps->registrar->skip_cred_build)
1583 goto skip_cred_build;
1585 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1586 if (wps->use_cred) {
1587 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1590 os_memset(&wps->cred, 0, sizeof(wps->cred));
1592 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1593 wps->cred.ssid_len = wps->wps->ssid_len;
1595 /* Select the best authentication and encryption type */
1596 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1597 wps->auth_type = WPS_AUTH_WPA2PSK;
1598 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1599 wps->auth_type = WPS_AUTH_WPAPSK;
1600 else if (wps->auth_type & WPS_AUTH_OPEN)
1601 wps->auth_type = WPS_AUTH_OPEN;
1602 else if (wps->auth_type & WPS_AUTH_SHARED)
1603 wps->auth_type = WPS_AUTH_SHARED;
1605 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1609 wps->cred.auth_type = wps->auth_type;
1611 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1612 wps->auth_type == WPS_AUTH_WPAPSK) {
1613 if (wps->encr_type & WPS_ENCR_AES)
1614 wps->encr_type = WPS_ENCR_AES;
1615 else if (wps->encr_type & WPS_ENCR_TKIP)
1616 wps->encr_type = WPS_ENCR_TKIP;
1618 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1619 "type for WPA/WPA2");
1623 if (wps->encr_type & WPS_ENCR_WEP)
1624 wps->encr_type = WPS_ENCR_WEP;
1625 else if (wps->encr_type & WPS_ENCR_NONE)
1626 wps->encr_type = WPS_ENCR_NONE;
1628 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1629 "type for non-WPA/WPA2 mode");
1633 wps->cred.encr_type = wps->encr_type;
1635 * Set MAC address in the Credential to be the Enrollee's MAC address
1637 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1639 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1640 !wps->wps->registrar->disable_auto_conf) {
1642 /* Generate a random passphrase */
1643 if (random_get_bytes(r, sizeof(r)) < 0)
1645 os_free(wps->new_psk);
1646 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1647 if (wps->new_psk == NULL)
1649 wps->new_psk_len--; /* remove newline */
1650 while (wps->new_psk_len &&
1651 wps->new_psk[wps->new_psk_len - 1] == '=')
1653 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1654 wps->new_psk, wps->new_psk_len);
1655 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1656 wps->cred.key_len = wps->new_psk_len;
1657 } else if (!wps->wps->registrar->force_per_enrollee_psk &&
1658 wps->use_psk_key && wps->wps->psk_set) {
1660 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1661 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1662 os_memcpy(wps->cred.key, hex, 32 * 2);
1663 wps->cred.key_len = 32 * 2;
1664 } else if (!wps->wps->registrar->force_per_enrollee_psk &&
1665 wps->wps->network_key) {
1666 os_memcpy(wps->cred.key, wps->wps->network_key,
1667 wps->wps->network_key_len);
1668 wps->cred.key_len = wps->wps->network_key_len;
1669 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1671 /* Generate a random per-device PSK */
1672 os_free(wps->new_psk);
1673 wps->new_psk_len = 32;
1674 wps->new_psk = os_malloc(wps->new_psk_len);
1675 if (wps->new_psk == NULL)
1677 if (random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
1678 os_free(wps->new_psk);
1679 wps->new_psk = NULL;
1682 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1683 wps->new_psk, wps->new_psk_len);
1684 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1686 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1687 wps->cred.key_len = wps->new_psk_len * 2;
1691 #ifdef CONFIG_WPS_TESTING
1692 if (wps_testing_dummy_cred)
1693 cred = wpabuf_alloc(200);
1697 struct wps_credential dummy;
1698 wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
1699 os_memset(&dummy, 0, sizeof(dummy));
1700 os_memcpy(dummy.ssid, "dummy", 5);
1702 dummy.auth_type = WPS_AUTH_WPA2PSK;
1703 dummy.encr_type = WPS_ENCR_AES;
1704 os_memcpy(dummy.key, "dummy psk", 9);
1706 os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
1707 wps_build_credential(cred, &dummy);
1708 wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
1710 wpabuf_put_be16(msg, ATTR_CRED);
1711 wpabuf_put_be16(msg, wpabuf_len(cred));
1712 wpabuf_put_buf(msg, cred);
1716 #endif /* CONFIG_WPS_TESTING */
1718 cred = wpabuf_alloc(200);
1722 if (wps_build_credential(cred, &wps->cred)) {
1727 wpabuf_put_be16(msg, ATTR_CRED);
1728 wpabuf_put_be16(msg, wpabuf_len(cred));
1729 wpabuf_put_buf(msg, cred);
1733 if (wps->wps->registrar->extra_cred) {
1734 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1735 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1742 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1744 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1746 if (wps_build_credential(msg, &wps->cred))
1753 static struct wpabuf * wps_build_ap_cred(struct wps_data *wps)
1755 struct wpabuf *msg, *plain;
1757 msg = wpabuf_alloc(1000);
1761 plain = wpabuf_alloc(200);
1762 if (plain == NULL) {
1767 if (wps_build_ap_settings(wps, plain)) {
1773 wpabuf_put_be16(msg, ATTR_CRED);
1774 wpabuf_put_be16(msg, wpabuf_len(plain));
1775 wpabuf_put_buf(msg, plain);
1782 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1785 int config_in_m2 = 0;
1787 if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
1789 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1790 wps->nonce_r, WPS_NONCE_LEN);
1791 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1793 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1794 msg = wpabuf_alloc(1000);
1798 if (wps_build_version(msg) ||
1799 wps_build_msg_type(msg, WPS_M2) ||
1800 wps_build_enrollee_nonce(wps, msg) ||
1801 wps_build_registrar_nonce(wps, msg) ||
1802 wps_build_uuid_r(wps, msg) ||
1803 wps_build_public_key(wps, msg) ||
1804 wps_derive_keys(wps) ||
1805 wps_build_auth_type_flags(wps, msg) ||
1806 wps_build_encr_type_flags(wps, msg) ||
1807 wps_build_conn_type_flags(wps, msg) ||
1808 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1809 wps_build_device_attrs(&wps->wps->dev, msg) ||
1810 wps_build_rf_bands(&wps->wps->dev, msg,
1811 wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
1812 wps_build_assoc_state(wps, msg) ||
1813 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1814 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1815 wps_build_os_version(&wps->wps->dev, msg) ||
1816 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1821 #ifdef CONFIG_WPS_NFC
1822 if (wps->nfc_pw_token && wps->nfc_pw_token->pk_hash_provided_oob &&
1823 wps->nfc_pw_token->pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1825 * Use abbreviated handshake since public key hash allowed
1826 * Enrollee to validate our public key similarly to how Enrollee
1827 * public key was validated. There is no need to validate Device
1828 * Password in this case.
1830 struct wpabuf *plain = wpabuf_alloc(500);
1831 if (plain == NULL ||
1832 wps_build_cred(wps, plain) ||
1833 wps_build_key_wrap_auth(wps, plain) ||
1834 wps_build_encr_settings(wps, msg, plain)) {
1842 #endif /* CONFIG_WPS_NFC */
1844 if (wps_build_authenticator(wps, msg)) {
1850 wps->state = config_in_m2 ? RECV_DONE : RECV_M3;
1855 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1858 u16 err = wps->config_error;
1860 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1861 msg = wpabuf_alloc(1000);
1865 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1866 err == WPS_CFG_NO_ERROR)
1867 err = WPS_CFG_SETUP_LOCKED;
1869 if (wps_build_version(msg) ||
1870 wps_build_msg_type(msg, WPS_M2D) ||
1871 wps_build_enrollee_nonce(wps, msg) ||
1872 wps_build_registrar_nonce(wps, msg) ||
1873 wps_build_uuid_r(wps, msg) ||
1874 wps_build_auth_type_flags(wps, msg) ||
1875 wps_build_encr_type_flags(wps, msg) ||
1876 wps_build_conn_type_flags(wps, msg) ||
1877 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1878 wps_build_device_attrs(&wps->wps->dev, msg) ||
1879 wps_build_rf_bands(&wps->wps->dev, msg,
1880 wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
1881 wps_build_assoc_state(wps, msg) ||
1882 wps_build_config_error(msg, err) ||
1883 wps_build_os_version(&wps->wps->dev, msg) ||
1884 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1889 wps->state = RECV_M2D_ACK;
1894 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1896 struct wpabuf *msg, *plain;
1898 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1900 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1902 plain = wpabuf_alloc(200);
1906 msg = wpabuf_alloc(1000);
1912 if (wps_build_version(msg) ||
1913 wps_build_msg_type(msg, WPS_M4) ||
1914 wps_build_enrollee_nonce(wps, msg) ||
1915 wps_build_r_hash(wps, msg) ||
1916 wps_build_r_snonce1(wps, plain) ||
1917 wps_build_key_wrap_auth(wps, plain) ||
1918 wps_build_encr_settings(wps, msg, plain) ||
1919 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1920 wps_build_authenticator(wps, msg)) {
1927 wps->state = RECV_M5;
1932 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1934 struct wpabuf *msg, *plain;
1936 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1938 plain = wpabuf_alloc(200);
1942 msg = wpabuf_alloc(1000);
1948 if (wps_build_version(msg) ||
1949 wps_build_msg_type(msg, WPS_M6) ||
1950 wps_build_enrollee_nonce(wps, msg) ||
1951 wps_build_r_snonce2(wps, plain) ||
1952 wps_build_key_wrap_auth(wps, plain) ||
1953 wps_build_encr_settings(wps, msg, plain) ||
1954 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1955 wps_build_authenticator(wps, msg)) {
1962 wps->wps_pin_revealed = 1;
1963 wps->state = RECV_M7;
1968 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1970 struct wpabuf *msg, *plain;
1972 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1974 plain = wpabuf_alloc(500);
1978 msg = wpabuf_alloc(1000);
1984 if (wps_build_version(msg) ||
1985 wps_build_msg_type(msg, WPS_M8) ||
1986 wps_build_enrollee_nonce(wps, msg) ||
1987 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1988 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1989 wps_build_key_wrap_auth(wps, plain) ||
1990 wps_build_encr_settings(wps, msg, plain) ||
1991 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1992 wps_build_authenticator(wps, msg)) {
1999 wps->state = RECV_DONE;
2004 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
2005 enum wsc_op_code *op_code)
2009 #ifdef CONFIG_WPS_UPNP
2010 if (!wps->int_reg && wps->wps->wps_upnp) {
2011 struct upnp_pending_message *p, *prev = NULL;
2012 if (wps->ext_reg > 1)
2013 wps_registrar_free_pending_m2(wps->wps);
2014 p = wps->wps->upnp_msgs;
2015 /* TODO: check pending message MAC address */
2016 while (p && p->next) {
2021 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
2026 wps->wps->upnp_msgs = NULL;
2033 *op_code = WSC_NACK;
2040 if (wps->ext_reg == 0)
2046 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
2047 "pending message available");
2050 #endif /* CONFIG_WPS_UPNP */
2052 switch (wps->state) {
2054 if (wps_get_dev_password(wps) < 0)
2055 msg = wps_build_m2d(wps);
2057 msg = wps_build_m2(wps);
2061 msg = wps_build_m2d(wps);
2065 msg = wps_build_m4(wps);
2069 msg = wps_build_m6(wps);
2073 msg = wps_build_m8(wps);
2077 msg = wps_build_wsc_ack(wps);
2081 msg = wps_build_wsc_nack(wps);
2082 *op_code = WSC_NACK;
2085 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
2086 "a message", wps->state);
2091 if (*op_code == WSC_MSG && msg) {
2092 /* Save a copy of the last message for Authenticator derivation
2094 wpabuf_free(wps->last_msg);
2095 wps->last_msg = wpabuf_dup(msg);
2102 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
2104 if (e_nonce == NULL) {
2105 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
2109 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
2110 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
2111 wps->nonce_e, WPS_NONCE_LEN);
2117 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
2119 if (r_nonce == NULL) {
2120 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
2124 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
2125 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
2133 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
2135 if (uuid_e == NULL) {
2136 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
2140 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
2141 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
2147 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
2149 if (pw_id == NULL) {
2150 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
2154 wps->dev_pw_id = WPA_GET_BE16(pw_id);
2155 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
2161 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
2163 if (e_hash1 == NULL) {
2164 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
2168 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
2169 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
2175 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
2177 if (e_hash2 == NULL) {
2178 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
2182 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
2183 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
2189 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
2191 u8 hash[SHA256_MAC_LEN];
2195 if (e_snonce1 == NULL) {
2196 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
2200 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
2201 WPS_SECRET_NONCE_LEN);
2203 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
2204 addr[0] = e_snonce1;
2205 len[0] = WPS_SECRET_NONCE_LEN;
2206 addr[1] = wps->psk1;
2207 len[1] = WPS_PSK_LEN;
2208 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2209 len[2] = wpabuf_len(wps->dh_pubkey_e);
2210 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2211 len[3] = wpabuf_len(wps->dh_pubkey_r);
2212 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2214 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
2215 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
2216 "not match with the pre-committed value");
2217 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2218 wps_pwd_auth_fail_event(wps->wps, 0, 1, wps->mac_addr_e);
2222 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
2223 "half of the device password");
2229 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
2231 u8 hash[SHA256_MAC_LEN];
2235 if (e_snonce2 == NULL) {
2236 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
2240 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
2241 WPS_SECRET_NONCE_LEN);
2243 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
2244 addr[0] = e_snonce2;
2245 len[0] = WPS_SECRET_NONCE_LEN;
2246 addr[1] = wps->psk2;
2247 len[1] = WPS_PSK_LEN;
2248 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2249 len[2] = wpabuf_len(wps->dh_pubkey_e);
2250 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2251 len[3] = wpabuf_len(wps->dh_pubkey_r);
2252 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2254 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
2255 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
2256 "not match with the pre-committed value");
2257 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2258 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2259 wps_pwd_auth_fail_event(wps->wps, 0, 2, wps->mac_addr_e);
2263 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
2264 "half of the device password");
2265 wps->wps_pin_revealed = 0;
2266 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
2269 * In case wildcard PIN is used and WPS handshake succeeds in the first
2270 * attempt, wps_registrar_unlock_pin() would not free the PIN, so make
2271 * sure the PIN gets invalidated here.
2273 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2279 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
2281 if (mac_addr == NULL) {
2282 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
2286 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
2288 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
2289 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
2295 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
2298 if (pk == NULL || pk_len == 0) {
2299 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
2303 wpabuf_free(wps->dh_pubkey_e);
2304 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
2305 if (wps->dh_pubkey_e == NULL)
2312 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
2317 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
2322 auth_types = WPA_GET_BE16(auth);
2324 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
2326 wps->auth_type = wps->wps->auth_types & auth_types;
2327 if (wps->auth_type == 0) {
2328 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2329 "authentication types (own 0x%x Enrollee 0x%x)",
2330 wps->wps->auth_types, auth_types);
2331 #ifdef WPS_WORKAROUNDS
2333 * Some deployed implementations seem to advertise incorrect
2334 * information in this attribute. For example, Linksys WRT350N
2335 * seems to have a byteorder bug that breaks this negotiation.
2336 * In order to interoperate with existing implementations,
2337 * assume that the Enrollee supports everything we do.
2339 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2340 "does not advertise supported authentication types "
2342 wps->auth_type = wps->wps->auth_types;
2343 #else /* WPS_WORKAROUNDS */
2345 #endif /* WPS_WORKAROUNDS */
2352 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
2357 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
2362 encr_types = WPA_GET_BE16(encr);
2364 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
2366 wps->encr_type = wps->wps->encr_types & encr_types;
2367 if (wps->encr_type == 0) {
2368 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2369 "encryption types (own 0x%x Enrollee 0x%x)",
2370 wps->wps->encr_types, encr_types);
2371 #ifdef WPS_WORKAROUNDS
2373 * Some deployed implementations seem to advertise incorrect
2374 * information in this attribute. For example, Linksys WRT350N
2375 * seems to have a byteorder bug that breaks this negotiation.
2376 * In order to interoperate with existing implementations,
2377 * assume that the Enrollee supports everything we do.
2379 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2380 "does not advertise supported encryption types "
2382 wps->encr_type = wps->wps->encr_types;
2383 #else /* WPS_WORKAROUNDS */
2385 #endif /* WPS_WORKAROUNDS */
2392 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
2395 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
2400 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
2407 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
2411 if (methods == NULL) {
2412 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
2416 m = WPA_GET_BE16(methods);
2418 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
2419 "%s%s%s%s%s%s%s%s%s", m,
2420 m & WPS_CONFIG_USBA ? " [USBA]" : "",
2421 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
2422 m & WPS_CONFIG_LABEL ? " [Label]" : "",
2423 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
2424 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
2425 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
2426 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
2427 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
2428 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
2430 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
2432 * The Enrollee does not have a display so it is unlikely to be
2433 * able to show the passphrase to a user and as such, could
2434 * benefit from receiving PSK to reduce key derivation time.
2436 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2437 "Enrollee not supporting display");
2438 wps->use_psk_key = 1;
2445 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2447 if (state == NULL) {
2448 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2453 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2460 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2464 if (assoc == NULL) {
2465 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2469 a = WPA_GET_BE16(assoc);
2470 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2476 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2481 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2485 e = WPA_GET_BE16(err);
2486 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2492 static int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
2495 struct wps_registrar *reg = wps->wps->registrar;
2497 if (is_zero_ether_addr(reg->p2p_dev_addr))
2498 return 1; /* no filtering in use */
2500 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
2501 wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
2502 "filtering for PBC: expected " MACSTR " was "
2503 MACSTR " - indicate PBC session overlap",
2504 MAC2STR(reg->p2p_dev_addr),
2505 MAC2STR(wps->p2p_dev_addr));
2508 #endif /* CONFIG_P2P */
2513 static int wps_registrar_skip_overlap(struct wps_data *wps)
2516 struct wps_registrar *reg = wps->wps->registrar;
2518 if (is_zero_ether_addr(reg->p2p_dev_addr))
2519 return 0; /* no specific Enrollee selected */
2521 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) == 0) {
2522 wpa_printf(MSG_DEBUG, "WPS: Skip PBC overlap due to selected "
2526 #endif /* CONFIG_P2P */
2531 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2532 struct wps_parse_attr *attr)
2534 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2536 if (wps->state != RECV_M1) {
2537 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2538 "receiving M1", wps->state);
2542 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2543 wps_process_mac_addr(wps, attr->mac_addr) ||
2544 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2545 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2546 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2547 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2548 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2549 wps_process_config_methods(wps, attr->config_methods) ||
2550 wps_process_wps_state(wps, attr->wps_state) ||
2551 wps_process_device_attrs(&wps->peer_dev, attr) ||
2552 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2553 wps_process_assoc_state(wps, attr->assoc_state) ||
2554 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2555 wps_process_config_error(wps, attr->config_error) ||
2556 wps_process_os_version(&wps->peer_dev, attr->os_version))
2559 if (wps->dev_pw_id < 0x10 &&
2560 wps->dev_pw_id != DEV_PW_DEFAULT &&
2561 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2562 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2563 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2564 #ifdef CONFIG_WPS_NFC
2565 wps->dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER &&
2566 #endif /* CONFIG_WPS_NFC */
2567 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2568 !wps->wps->registrar->pbc)) {
2569 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2571 wps->state = SEND_M2D;
2572 return WPS_CONTINUE;
2575 #ifdef CONFIG_WPS_NFC
2576 if (wps->dev_pw_id >= 0x10 ||
2577 wps->dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
2578 struct wps_nfc_pw_token *token;
2580 u8 hash[WPS_HASH_LEN];
2582 wpa_printf(MSG_DEBUG, "WPS: Searching for NFC token match for id=%d (ctx %p registrar %p)",
2583 wps->dev_pw_id, wps->wps, wps->wps->registrar);
2584 token = wps_get_nfc_pw_token(
2585 &wps->wps->registrar->nfc_pw_tokens, wps->dev_pw_id);
2586 if (token && token->peer_pk_hash_known) {
2587 wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
2589 dl_list_del(&token->list);
2590 wps->nfc_pw_token = token;
2592 addr[0] = attr->public_key;
2593 sha256_vector(1, addr, &attr->public_key_len, hash);
2594 if (os_memcmp(hash, wps->nfc_pw_token->pubkey_hash,
2595 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
2596 wpa_printf(MSG_ERROR, "WPS: Public Key hash "
2598 wps->state = SEND_M2D;
2600 WPS_CFG_PUBLIC_KEY_HASH_MISMATCH;
2601 return WPS_CONTINUE;
2604 wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
2605 "Password Token (no peer PK hash)");
2606 wps->nfc_pw_token = token;
2607 } else if (wps->dev_pw_id >= 0x10 &&
2608 wps->wps->ap_nfc_dev_pw_id == wps->dev_pw_id &&
2609 wps->wps->ap_nfc_dev_pw) {
2610 wpa_printf(MSG_DEBUG, "WPS: Found match with own NFC Password Token");
2613 #endif /* CONFIG_WPS_NFC */
2615 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2616 if ((wps->wps->registrar->force_pbc_overlap ||
2617 wps_registrar_pbc_overlap(wps->wps->registrar,
2618 wps->mac_addr_e, wps->uuid_e) ||
2619 !wps_registrar_p2p_dev_addr_match(wps)) &&
2620 !wps_registrar_skip_overlap(wps)) {
2621 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2623 wps->state = SEND_M2D;
2624 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2625 wps_pbc_overlap_event(wps->wps);
2626 wps_fail_event(wps->wps, WPS_M1,
2627 WPS_CFG_MULTIPLE_PBC_DETECTED,
2628 WPS_EI_NO_ERROR, wps->mac_addr_e);
2629 wps->wps->registrar->force_pbc_overlap = 1;
2630 return WPS_CONTINUE;
2632 wps_registrar_add_pbc_session(wps->wps->registrar,
2633 wps->mac_addr_e, wps->uuid_e);
2637 #ifdef WPS_WORKAROUNDS
2639 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2640 * passphrase format. To avoid interop issues, force PSK format to be
2643 if (!wps->use_psk_key &&
2644 wps->peer_dev.manufacturer &&
2645 os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
2646 wps->peer_dev.model_name &&
2647 os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
2648 wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
2650 wps->use_psk_key = 1;
2652 #endif /* WPS_WORKAROUNDS */
2654 wps->state = SEND_M2;
2655 return WPS_CONTINUE;
2659 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2660 const struct wpabuf *msg,
2661 struct wps_parse_attr *attr)
2663 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2665 if (wps->state != RECV_M3) {
2666 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2667 "receiving M3", wps->state);
2668 wps->state = SEND_WSC_NACK;
2669 return WPS_CONTINUE;
2672 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2673 !wps_registrar_skip_overlap(wps)) {
2674 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2676 wps->state = SEND_WSC_NACK;
2677 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2678 return WPS_CONTINUE;
2681 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2682 wps_process_authenticator(wps, attr->authenticator, msg) ||
2683 wps_process_e_hash1(wps, attr->e_hash1) ||
2684 wps_process_e_hash2(wps, attr->e_hash2)) {
2685 wps->state = SEND_WSC_NACK;
2686 return WPS_CONTINUE;
2689 wps->state = SEND_M4;
2690 return WPS_CONTINUE;
2694 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2695 const struct wpabuf *msg,
2696 struct wps_parse_attr *attr)
2698 struct wpabuf *decrypted;
2699 struct wps_parse_attr eattr;
2701 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2703 if (wps->state != RECV_M5) {
2704 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2705 "receiving M5", wps->state);
2706 wps->state = SEND_WSC_NACK;
2707 return WPS_CONTINUE;
2710 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2711 !wps_registrar_skip_overlap(wps)) {
2712 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2714 wps->state = SEND_WSC_NACK;
2715 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2716 return WPS_CONTINUE;
2719 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2720 wps_process_authenticator(wps, attr->authenticator, msg)) {
2721 wps->state = SEND_WSC_NACK;
2722 return WPS_CONTINUE;
2725 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2726 attr->encr_settings_len);
2727 if (decrypted == NULL) {
2728 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2729 "Settings attribute");
2730 wps->state = SEND_WSC_NACK;
2731 return WPS_CONTINUE;
2734 if (wps_validate_m5_encr(decrypted, attr->version2 != NULL) < 0) {
2735 wpabuf_free(decrypted);
2736 wps->state = SEND_WSC_NACK;
2737 return WPS_CONTINUE;
2740 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2742 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2743 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2744 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2745 wpabuf_free(decrypted);
2746 wps->state = SEND_WSC_NACK;
2747 return WPS_CONTINUE;
2749 wpabuf_free(decrypted);
2751 wps->state = SEND_M6;
2752 return WPS_CONTINUE;
2756 static void wps_sta_cred_cb(struct wps_data *wps)
2759 * Update credential to only include a single authentication and
2760 * encryption type in case the AP configuration includes more than one
2763 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2764 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2765 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2766 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2767 if (wps->cred.encr_type & WPS_ENCR_AES)
2768 wps->cred.encr_type = WPS_ENCR_AES;
2769 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2770 wps->cred.encr_type = WPS_ENCR_TKIP;
2771 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2772 "AP configuration");
2773 if (wps->wps->cred_cb)
2774 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2778 static void wps_cred_update(struct wps_credential *dst,
2779 struct wps_credential *src)
2781 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2782 dst->ssid_len = src->ssid_len;
2783 dst->auth_type = src->auth_type;
2784 dst->encr_type = src->encr_type;
2785 dst->key_idx = src->key_idx;
2786 os_memcpy(dst->key, src->key, sizeof(dst->key));
2787 dst->key_len = src->key_len;
2791 static int wps_process_ap_settings_r(struct wps_data *wps,
2792 struct wps_parse_attr *attr)
2796 if (wps->wps->ap || wps->er)
2799 /* AP Settings Attributes in M7 when Enrollee is an AP */
2800 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2803 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2805 if (wps->new_ap_settings) {
2806 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2808 wps_cred_update(&wps->cred, wps->new_ap_settings);
2812 * Use the AP PIN only to receive the current AP settings, not
2813 * to reconfigure the AP.
2817 * Clear selected registrar here since we do not get to
2818 * WSC_Done in this protocol run.
2820 wps_registrar_pin_completed(wps->wps->registrar);
2822 msg = wps_build_ap_cred(wps);
2825 wps->cred.cred_attr = wpabuf_head(msg);
2826 wps->cred.cred_attr_len = wpabuf_len(msg);
2828 if (wps->ap_settings_cb) {
2829 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2834 wps_sta_cred_cb(wps);
2836 wps->cred.cred_attr = NULL;
2837 wps->cred.cred_attr_len = 0;
2845 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2846 const struct wpabuf *msg,
2847 struct wps_parse_attr *attr)
2849 struct wpabuf *decrypted;
2850 struct wps_parse_attr eattr;
2852 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2854 if (wps->state != RECV_M7) {
2855 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2856 "receiving M7", wps->state);
2857 wps->state = SEND_WSC_NACK;
2858 return WPS_CONTINUE;
2861 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2862 !wps_registrar_skip_overlap(wps)) {
2863 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2865 wps->state = SEND_WSC_NACK;
2866 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2867 return WPS_CONTINUE;
2870 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2871 wps_process_authenticator(wps, attr->authenticator, msg)) {
2872 wps->state = SEND_WSC_NACK;
2873 return WPS_CONTINUE;
2876 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2877 attr->encr_settings_len);
2878 if (decrypted == NULL) {
2879 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2880 "Settings attribute");
2881 wps->state = SEND_WSC_NACK;
2882 return WPS_CONTINUE;
2885 if (wps_validate_m7_encr(decrypted, wps->wps->ap || wps->er,
2886 attr->version2 != NULL) < 0) {
2887 wpabuf_free(decrypted);
2888 wps->state = SEND_WSC_NACK;
2889 return WPS_CONTINUE;
2892 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2894 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2895 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2896 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2897 wps_process_ap_settings_r(wps, &eattr)) {
2898 wpabuf_free(decrypted);
2899 wps->state = SEND_WSC_NACK;
2900 return WPS_CONTINUE;
2903 wpabuf_free(decrypted);
2905 wps->state = SEND_M8;
2906 return WPS_CONTINUE;
2910 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2911 const struct wpabuf *msg)
2913 struct wps_parse_attr attr;
2914 enum wps_process_res ret = WPS_CONTINUE;
2916 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2918 if (wps_parse_msg(msg, &attr) < 0)
2921 if (attr.msg_type == NULL) {
2922 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2923 wps->state = SEND_WSC_NACK;
2924 return WPS_CONTINUE;
2927 if (*attr.msg_type != WPS_M1 &&
2928 (attr.registrar_nonce == NULL ||
2929 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2930 WPS_NONCE_LEN) != 0)) {
2931 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2935 switch (*attr.msg_type) {
2937 if (wps_validate_m1(msg) < 0)
2939 #ifdef CONFIG_WPS_UPNP
2940 if (wps->wps->wps_upnp && attr.mac_addr) {
2941 /* Remove old pending messages when starting new run */
2942 wps_free_pending_msgs(wps->wps->upnp_msgs);
2943 wps->wps->upnp_msgs = NULL;
2945 upnp_wps_device_send_wlan_event(
2946 wps->wps->wps_upnp, attr.mac_addr,
2947 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2949 #endif /* CONFIG_WPS_UPNP */
2950 ret = wps_process_m1(wps, &attr);
2953 if (wps_validate_m3(msg) < 0)
2955 ret = wps_process_m3(wps, msg, &attr);
2956 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2957 wps_fail_event(wps->wps, WPS_M3, wps->config_error,
2958 wps->error_indication, wps->mac_addr_e);
2961 if (wps_validate_m5(msg) < 0)
2963 ret = wps_process_m5(wps, msg, &attr);
2964 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2965 wps_fail_event(wps->wps, WPS_M5, wps->config_error,
2966 wps->error_indication, wps->mac_addr_e);
2969 if (wps_validate_m7(msg) < 0)
2971 ret = wps_process_m7(wps, msg, &attr);
2972 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2973 wps_fail_event(wps->wps, WPS_M7, wps->config_error,
2974 wps->error_indication, wps->mac_addr_e);
2977 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2982 if (ret == WPS_CONTINUE) {
2983 /* Save a copy of the last message for Authenticator derivation
2985 wpabuf_free(wps->last_msg);
2986 wps->last_msg = wpabuf_dup(msg);
2993 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2994 const struct wpabuf *msg)
2996 struct wps_parse_attr attr;
2998 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
3000 if (wps_parse_msg(msg, &attr) < 0)
3003 if (attr.msg_type == NULL) {
3004 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3008 if (*attr.msg_type != WPS_WSC_ACK) {
3009 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3014 #ifdef CONFIG_WPS_UPNP
3015 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
3016 upnp_wps_subscribers(wps->wps->wps_upnp)) {
3017 if (wps->wps->upnp_msgs)
3018 return WPS_CONTINUE;
3019 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
3020 "external Registrar");
3023 #endif /* CONFIG_WPS_UPNP */
3025 if (attr.registrar_nonce == NULL ||
3026 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3028 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3032 if (attr.enrollee_nonce == NULL ||
3033 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3034 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3038 if (wps->state == RECV_M2D_ACK) {
3039 #ifdef CONFIG_WPS_UPNP
3040 if (wps->wps->wps_upnp &&
3041 upnp_wps_subscribers(wps->wps->wps_upnp)) {
3042 if (wps->wps->upnp_msgs)
3043 return WPS_CONTINUE;
3044 if (wps->ext_reg == 0)
3046 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
3047 "external Registrar");
3050 #endif /* CONFIG_WPS_UPNP */
3052 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
3053 "terminate negotiation");
3060 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
3061 const struct wpabuf *msg)
3063 struct wps_parse_attr attr;
3067 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
3069 old_state = wps->state;
3070 wps->state = SEND_WSC_NACK;
3072 if (wps_parse_msg(msg, &attr) < 0)
3075 if (attr.msg_type == NULL) {
3076 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3080 if (*attr.msg_type != WPS_WSC_NACK) {
3081 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3086 #ifdef CONFIG_WPS_UPNP
3087 if (wps->wps->wps_upnp && wps->ext_reg) {
3088 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3089 "Registrar terminated by the Enrollee");
3092 #endif /* CONFIG_WPS_UPNP */
3094 if (attr.registrar_nonce == NULL ||
3095 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3097 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3101 if (attr.enrollee_nonce == NULL ||
3102 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3103 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3107 if (attr.config_error == NULL) {
3108 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
3113 config_error = WPA_GET_BE16(attr.config_error);
3114 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
3115 "Configuration Error %d", config_error);
3117 switch (old_state) {
3119 wps_fail_event(wps->wps, WPS_M2, config_error,
3120 wps->error_indication, wps->mac_addr_e);
3123 wps_fail_event(wps->wps, WPS_M4, config_error,
3124 wps->error_indication, wps->mac_addr_e);
3127 wps_fail_event(wps->wps, WPS_M6, config_error,
3128 wps->error_indication, wps->mac_addr_e);
3131 wps_fail_event(wps->wps, WPS_M8, config_error,
3132 wps->error_indication, wps->mac_addr_e);
3142 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
3143 const struct wpabuf *msg)
3145 struct wps_parse_attr attr;
3147 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
3149 if (wps->state != RECV_DONE &&
3150 (!wps->wps->wps_upnp || !wps->ext_reg)) {
3151 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
3152 "receiving WSC_Done", wps->state);
3156 if (wps_parse_msg(msg, &attr) < 0)
3159 if (attr.msg_type == NULL) {
3160 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3164 if (*attr.msg_type != WPS_WSC_DONE) {
3165 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3170 #ifdef CONFIG_WPS_UPNP
3171 if (wps->wps->wps_upnp && wps->ext_reg) {
3172 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3173 "Registrar completed successfully");
3174 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3178 #endif /* CONFIG_WPS_UPNP */
3180 if (attr.registrar_nonce == NULL ||
3181 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3183 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3187 if (attr.enrollee_nonce == NULL ||
3188 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3189 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3193 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
3194 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3197 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
3198 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
3199 struct wps_credential cred;
3201 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
3202 "on first Enrollee connection");
3204 os_memset(&cred, 0, sizeof(cred));
3205 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
3206 cred.ssid_len = wps->wps->ssid_len;
3207 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
3208 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
3209 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
3210 cred.key_len = wps->new_psk_len;
3212 wps->wps->wps_state = WPS_STATE_CONFIGURED;
3213 wpa_hexdump_ascii_key(MSG_DEBUG,
3214 "WPS: Generated random passphrase",
3215 wps->new_psk, wps->new_psk_len);
3216 if (wps->wps->cred_cb)
3217 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
3219 os_free(wps->new_psk);
3220 wps->new_psk = NULL;
3223 if (!wps->wps->ap && !wps->er)
3224 wps_sta_cred_cb(wps);
3227 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
3228 wps->p2p_dev_addr, wps->new_psk,
3229 wps->new_psk_len)) {
3230 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
3233 os_free(wps->new_psk);
3234 wps->new_psk = NULL;
3237 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e,
3238 wps->dev_password, wps->dev_password_len);
3241 wps_registrar_remove_pbc_session(wps->wps->registrar,
3244 wps_registrar_pbc_completed(wps->wps->registrar);
3245 #ifdef WPS_WORKAROUNDS
3246 os_get_reltime(&wps->wps->registrar->pbc_ignore_start);
3247 #endif /* WPS_WORKAROUNDS */
3248 os_memcpy(wps->wps->registrar->pbc_ignore_uuid, wps->uuid_e,
3251 wps_registrar_pin_completed(wps->wps->registrar);
3253 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
3254 * merge them into APs own list.. */
3256 wps_success_event(wps->wps, wps->mac_addr_e);
3262 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
3263 enum wsc_op_code op_code,
3264 const struct wpabuf *msg)
3266 enum wps_process_res ret;
3268 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
3270 (unsigned long) wpabuf_len(msg), op_code);
3272 #ifdef CONFIG_WPS_UPNP
3273 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
3274 struct wps_parse_attr attr;
3275 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
3276 *attr.msg_type == WPS_M3)
3277 wps->ext_reg = 2; /* past M2/M2D phase */
3279 if (wps->ext_reg > 1)
3280 wps_registrar_free_pending_m2(wps->wps);
3281 if (wps->wps->wps_upnp && wps->ext_reg &&
3282 wps->wps->upnp_msgs == NULL &&
3283 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
3285 struct wps_parse_attr attr;
3287 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
3290 type = *attr.msg_type;
3291 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
3292 " to external Registrar for processing", type);
3293 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
3295 UPNP_WPS_WLANEVENT_TYPE_EAP,
3297 if (op_code == WSC_MSG)
3299 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
3300 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
3301 "external Registrar");
3302 return WPS_CONTINUE;
3304 #endif /* CONFIG_WPS_UPNP */
3308 return wps_process_wsc_msg(wps, msg);
3310 if (wps_validate_wsc_ack(msg) < 0)
3312 return wps_process_wsc_ack(wps, msg);
3314 if (wps_validate_wsc_nack(msg) < 0)
3316 return wps_process_wsc_nack(wps, msg);
3318 if (wps_validate_wsc_done(msg) < 0)
3320 ret = wps_process_wsc_done(wps, msg);
3321 if (ret == WPS_FAILURE) {
3322 wps->state = SEND_WSC_NACK;
3323 wps_fail_event(wps->wps, WPS_WSC_DONE,
3325 wps->error_indication, wps->mac_addr_e);
3329 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
3335 int wps_registrar_update_ie(struct wps_registrar *reg)
3337 return wps_set_ie(reg);
3341 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
3344 struct wps_registrar *reg = eloop_ctx;
3346 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
3347 "unselect internal Registrar");
3348 reg->selected_registrar = 0;
3350 wps_registrar_selected_registrar_changed(reg, 0);
3354 #ifdef CONFIG_WPS_UPNP
3355 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
3356 struct subscription *s)
3359 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
3360 "config_methods=0x%x)",
3361 s->dev_password_id, s->config_methods);
3362 reg->sel_reg_union = 1;
3363 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
3364 reg->sel_reg_dev_password_id_override = s->dev_password_id;
3365 if (reg->sel_reg_config_methods_override == -1)
3366 reg->sel_reg_config_methods_override = 0;
3367 reg->sel_reg_config_methods_override |= s->config_methods;
3368 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
3369 if (is_zero_ether_addr(reg->authorized_macs_union[i]))
3371 for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
3373 if (is_zero_ether_addr(s->authorized_macs[j]))
3375 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC into union: "
3376 MACSTR, MAC2STR(s->authorized_macs[j]));
3377 os_memcpy(reg->authorized_macs_union[i],
3378 s->authorized_macs[j], ETH_ALEN);
3381 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union",
3382 (u8 *) reg->authorized_macs_union,
3383 sizeof(reg->authorized_macs_union));
3385 #endif /* CONFIG_WPS_UPNP */
3388 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
3390 #ifdef CONFIG_WPS_UPNP
3391 struct subscription *s;
3393 if (reg->wps->wps_upnp == NULL)
3396 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
3397 struct subscription, list) {
3398 struct subscr_addr *sa;
3399 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
3401 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
3402 inet_ntoa(sa->saddr.sin_addr),
3403 ntohs(sa->saddr.sin_port));
3405 if (s->selected_registrar)
3406 wps_registrar_sel_reg_add(reg, s);
3408 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
3411 #endif /* CONFIG_WPS_UPNP */
3416 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3417 * @reg: Registrar data from wps_registrar_init()
3419 * This function is called when selected registrar state changes, e.g., when an
3420 * AP receives a SetSelectedRegistrar UPnP message.
3422 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg,
3425 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
3427 reg->sel_reg_union = reg->selected_registrar;
3428 reg->sel_reg_dev_password_id_override = -1;
3429 reg->sel_reg_config_methods_override = -1;
3430 os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
3431 WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
3432 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union (start with own)",
3433 (u8 *) reg->authorized_macs_union,
3434 sizeof(reg->authorized_macs_union));
3435 if (reg->selected_registrar) {
3438 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
3439 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
3440 WPS_CONFIG_PHY_PUSHBUTTON);
3442 reg->sel_reg_dev_password_id_override =
3444 wps_set_pushbutton(&methods, reg->wps->config_methods);
3445 } else if (dev_pw_id)
3446 reg->sel_reg_dev_password_id_override = dev_pw_id;
3447 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
3448 "(pbc=%d)", reg->pbc);
3449 reg->sel_reg_config_methods_override = methods;
3451 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
3453 wps_registrar_sel_reg_union(reg);
3456 wps_cb_set_sel_reg(reg);
3460 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
3461 char *buf, size_t buflen)
3463 struct wps_registrar_device *d;
3466 char devtype[WPS_DEV_TYPE_BUFSIZE];
3468 d = wps_device_get(reg, addr);
3471 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
3474 ret = os_snprintf(buf + len, buflen - len,
3476 "wpsPrimaryDeviceType=%s\n"
3477 "wpsDeviceName=%s\n"
3478 "wpsManufacturer=%s\n"
3480 "wpsModelNumber=%s\n"
3481 "wpsSerialNumber=%s\n",
3483 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
3485 d->dev.device_name ? d->dev.device_name : "",
3486 d->dev.manufacturer ? d->dev.manufacturer : "",
3487 d->dev.model_name ? d->dev.model_name : "",
3488 d->dev.model_number ? d->dev.model_number : "",
3489 d->dev.serial_number ? d->dev.serial_number : "");
3490 if (ret < 0 || (size_t) ret >= buflen - len)
3498 int wps_registrar_config_ap(struct wps_registrar *reg,
3499 struct wps_credential *cred)
3501 wpa_printf(MSG_DEBUG, "WPS: encr_type=0x%x", cred->encr_type);
3502 if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
3504 if (cred->encr_type & WPS_ENCR_WEP) {
3505 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
3506 "due to WEP configuration");
3510 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
3511 "invalid encr_type 0x%x", cred->encr_type);
3515 if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
3517 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
3519 cred->encr_type |= WPS_ENCR_AES;
3522 if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
3524 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
3526 cred->auth_type |= WPS_AUTH_WPA2PSK;
3529 if (reg->wps->cred_cb)
3530 return reg->wps->cred_cb(reg->wps->cb_ctx, cred);
3536 #ifdef CONFIG_WPS_NFC
3538 int wps_registrar_add_nfc_pw_token(struct wps_registrar *reg,
3539 const u8 *pubkey_hash, u16 pw_id,
3540 const u8 *dev_pw, size_t dev_pw_len,
3541 int pk_hash_provided_oob)
3543 struct wps_nfc_pw_token *token;
3545 if (dev_pw_len > WPS_OOB_DEVICE_PASSWORD_LEN)
3548 if (pw_id == DEV_PW_NFC_CONNECTION_HANDOVER &&
3549 (pubkey_hash == NULL || !pk_hash_provided_oob)) {
3550 wpa_printf(MSG_DEBUG, "WPS: Unexpected NFC Password Token "
3551 "addition - missing public key hash");
3555 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, pw_id);
3557 token = os_zalloc(sizeof(*token));
3561 token->peer_pk_hash_known = pubkey_hash != NULL;
3563 os_memcpy(token->pubkey_hash, pubkey_hash,
3564 WPS_OOB_PUBKEY_HASH_LEN);
3565 token->pw_id = pw_id;
3566 token->pk_hash_provided_oob = pk_hash_provided_oob;
3568 wpa_snprintf_hex_uppercase((char *) token->dev_pw,
3569 sizeof(token->dev_pw),
3570 dev_pw, dev_pw_len);
3571 token->dev_pw_len = dev_pw_len * 2;
3574 dl_list_add(®->nfc_pw_tokens, &token->list);
3576 reg->selected_registrar = 1;
3578 wps_registrar_add_authorized_mac(reg,
3579 (u8 *) "\xff\xff\xff\xff\xff\xff");
3580 wps_registrar_selected_registrar_changed(reg, pw_id);
3581 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
3582 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
3583 wps_registrar_set_selected_timeout,
3586 wpa_printf(MSG_DEBUG, "WPS: Added NFC Device Password %u to Registrar",
3593 int wps_registrar_add_nfc_password_token(struct wps_registrar *reg,
3594 const u8 *oob_dev_pw,
3595 size_t oob_dev_pw_len)
3597 const u8 *pos, *hash, *dev_pw;
3601 if (oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2 ||
3602 oob_dev_pw_len > WPS_OOB_PUBKEY_HASH_LEN + 2 +
3603 WPS_OOB_DEVICE_PASSWORD_LEN)
3607 pos = oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN;
3608 id = WPA_GET_BE16(pos);
3610 dev_pw_len = oob_dev_pw + oob_dev_pw_len - dev_pw;
3612 wpa_printf(MSG_DEBUG, "WPS: Add NFC Password Token for Password ID %u",
3615 wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
3616 hash, WPS_OOB_PUBKEY_HASH_LEN);
3617 wpa_hexdump_key(MSG_DEBUG, "WPS: Device Password", dev_pw, dev_pw_len);
3619 return wps_registrar_add_nfc_pw_token(reg, hash, id, dev_pw,
3624 void wps_registrar_remove_nfc_pw_token(struct wps_registrar *reg,
3625 struct wps_nfc_pw_token *token)
3627 wps_registrar_remove_authorized_mac(reg,
3628 (u8 *) "\xff\xff\xff\xff\xff\xff");
3629 wps_registrar_selected_registrar_changed(reg, 0);
3632 * Free the NFC password token if it was used only for a single protocol
3633 * run. The static handover case uses the same password token multiple
3634 * times, so do not free that case here.
3636 if (token->peer_pk_hash_known)
3640 #endif /* CONFIG_WPS_NFC */