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);
45 bin_clear_free(token, sizeof(*token));
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)
94 bin_clear_free(pin->pin, pin->pin_len);
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);
679 void wps_registrar_flush(struct wps_registrar *reg)
683 wps_free_pins(®->pins);
684 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, 0);
685 wps_free_pbc_sessions(reg->pbc_sessions);
686 reg->pbc_sessions = NULL;
687 wps_free_devices(reg->devices);
689 #ifdef WPS_WORKAROUNDS
690 reg->pbc_ignore_start.sec = 0;
691 #endif /* WPS_WORKAROUNDS */
696 * wps_registrar_deinit - Deinitialize WPS Registrar data
697 * @reg: Registrar data from wps_registrar_init()
699 void wps_registrar_deinit(struct wps_registrar *reg)
703 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
704 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
705 wps_registrar_flush(reg);
706 wpabuf_free(reg->extra_cred);
711 static void wps_registrar_invalidate_unused(struct wps_registrar *reg)
713 struct wps_uuid_pin *pin;
715 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
716 if (pin->wildcard_uuid == 1 && !(pin->flags & PIN_LOCKED)) {
717 wpa_printf(MSG_DEBUG, "WPS: Invalidate previously "
718 "configured wildcard PIN");
719 wps_registrar_remove_pin(reg, pin);
727 * wps_registrar_add_pin - Configure a new PIN for Registrar
728 * @reg: Registrar data from wps_registrar_init()
729 * @addr: Enrollee MAC address or %NULL if not known
730 * @uuid: UUID-E or %NULL for wildcard (any UUID)
731 * @pin: PIN (Device Password)
732 * @pin_len: Length of pin in octets
733 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
734 * Returns: 0 on success, -1 on failure
736 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
737 const u8 *uuid, const u8 *pin, size_t pin_len,
740 struct wps_uuid_pin *p;
742 p = os_zalloc(sizeof(*p));
746 os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
748 p->wildcard_uuid = 1;
750 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
751 p->pin = os_malloc(pin_len);
752 if (p->pin == NULL) {
756 os_memcpy(p->pin, pin, pin_len);
757 p->pin_len = pin_len;
760 p->flags |= PIN_EXPIRES;
761 os_get_reltime(&p->expiration);
762 p->expiration.sec += timeout;
765 if (p->wildcard_uuid)
766 wps_registrar_invalidate_unused(reg);
768 dl_list_add(®->pins, &p->list);
770 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
772 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
773 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
774 reg->selected_registrar = 1;
777 wps_registrar_add_authorized_mac(reg, addr);
779 wps_registrar_add_authorized_mac(
780 reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
781 wps_registrar_selected_registrar_changed(reg, 0);
782 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
783 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
784 wps_registrar_set_selected_timeout,
791 static void wps_registrar_remove_pin(struct wps_registrar *reg,
792 struct wps_uuid_pin *pin)
795 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
797 if (is_zero_ether_addr(pin->enrollee_addr))
800 addr = pin->enrollee_addr;
801 wps_registrar_remove_authorized_mac(reg, addr);
803 wps_registrar_selected_registrar_changed(reg, 0);
807 static void wps_registrar_expire_pins(struct wps_registrar *reg)
809 struct wps_uuid_pin *pin, *prev;
810 struct os_reltime now;
812 os_get_reltime(&now);
813 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
815 if ((pin->flags & PIN_EXPIRES) &&
816 os_reltime_before(&pin->expiration, &now)) {
817 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
818 pin->uuid, WPS_UUID_LEN);
819 wps_registrar_remove_pin(reg, pin);
826 * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
827 * @reg: Registrar data from wps_registrar_init()
828 * @dev_pw: PIN to search for or %NULL to match any
829 * @dev_pw_len: Length of dev_pw in octets
830 * Returns: 0 on success, -1 if not wildcard PIN is enabled
832 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg,
836 struct wps_uuid_pin *pin, *prev;
838 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
840 if (dev_pw && pin->pin &&
841 (dev_pw_len != pin->pin_len ||
842 os_memcmp_const(dev_pw, pin->pin, dev_pw_len) != 0))
843 continue; /* different PIN */
844 if (pin->wildcard_uuid) {
845 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
846 pin->uuid, WPS_UUID_LEN);
847 wps_registrar_remove_pin(reg, pin);
857 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
858 * @reg: Registrar data from wps_registrar_init()
860 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
862 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
864 struct wps_uuid_pin *pin, *prev;
866 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
868 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
869 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
870 pin->uuid, WPS_UUID_LEN);
871 wps_registrar_remove_pin(reg, pin);
880 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
881 const u8 *uuid, size_t *pin_len)
883 struct wps_uuid_pin *pin, *found = NULL;
885 wps_registrar_expire_pins(reg);
887 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
888 if (!pin->wildcard_uuid &&
889 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
896 /* Check for wildcard UUIDs since none of the UUID-specific
898 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
899 if (pin->wildcard_uuid == 1 ||
900 pin->wildcard_uuid == 2) {
901 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
902 "PIN. Assigned it for this UUID-E");
903 pin->wildcard_uuid++;
904 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
915 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
916 * that could otherwise avoid PIN invalidations.
918 if (found->flags & PIN_LOCKED) {
919 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
920 "allow concurrent re-use");
923 *pin_len = found->pin_len;
924 found->flags |= PIN_LOCKED;
930 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
931 * @reg: Registrar data from wps_registrar_init()
933 * Returns: 0 on success, -1 on failure
935 * PINs are locked to enforce only one concurrent use. This function unlocks a
936 * PIN to allow it to be used again. If the specified PIN was configured using
937 * a wildcard UUID, it will be removed instead of allowing multiple uses.
939 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
941 struct wps_uuid_pin *pin;
943 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
944 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
945 if (pin->wildcard_uuid == 3) {
946 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
948 return wps_registrar_invalidate_pin(reg, uuid);
950 pin->flags &= ~PIN_LOCKED;
959 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
961 reg->selected_registrar = 0;
963 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
964 wps_registrar_remove_authorized_mac(reg,
965 (u8 *) "\xff\xff\xff\xff\xff\xff");
966 wps_registrar_selected_registrar_changed(reg, 0);
970 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
972 struct wps_registrar *reg = eloop_ctx;
974 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
975 wps_pbc_timeout_event(reg->wps);
976 wps_registrar_stop_pbc(reg);
981 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
982 * @reg: Registrar data from wps_registrar_init()
983 * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
984 * indicates no such filtering
985 * Returns: 0 on success, -1 on failure, -2 on session overlap
987 * This function is called on an AP when a push button is pushed to activate
988 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
989 * or when a PBC registration is completed. If more than one Enrollee in active
990 * PBC mode has been detected during the monitor time (previous 2 minutes), the
991 * PBC mode is not activated and -2 is returned to indicate session overlap.
992 * This is skipped if a specific Enrollee is selected.
994 int wps_registrar_button_pushed(struct wps_registrar *reg,
995 const u8 *p2p_dev_addr)
997 if (p2p_dev_addr == NULL &&
998 wps_registrar_pbc_overlap(reg, NULL, NULL)) {
999 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
1001 wps_pbc_overlap_event(reg->wps);
1004 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
1005 reg->force_pbc_overlap = 0;
1006 reg->selected_registrar = 1;
1009 os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
1011 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
1012 wps_registrar_add_authorized_mac(reg,
1013 (u8 *) "\xff\xff\xff\xff\xff\xff");
1014 wps_registrar_selected_registrar_changed(reg, 0);
1016 wps_pbc_active_event(reg->wps);
1017 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1018 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1019 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
1025 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
1027 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
1028 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1029 wps_registrar_stop_pbc(reg);
1030 wps_pbc_disable_event(reg->wps);
1034 static void wps_registrar_pin_completed(struct wps_registrar *reg)
1036 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
1037 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1038 reg->selected_registrar = 0;
1039 wps_registrar_selected_registrar_changed(reg, 0);
1043 void wps_registrar_complete(struct wps_registrar *registrar, const u8 *uuid_e,
1044 const u8 *dev_pw, size_t dev_pw_len)
1046 if (registrar->pbc) {
1047 wps_registrar_remove_pbc_session(registrar,
1049 wps_registrar_pbc_completed(registrar);
1050 #ifdef WPS_WORKAROUNDS
1051 os_get_reltime(®istrar->pbc_ignore_start);
1052 #endif /* WPS_WORKAROUNDS */
1053 os_memcpy(registrar->pbc_ignore_uuid, uuid_e, WPS_UUID_LEN);
1055 wps_registrar_pin_completed(registrar);
1059 wps_registrar_invalidate_wildcard_pin(registrar, dev_pw,
1061 wpa_hexdump_key(MSG_DEBUG, "WPS: Invalidated wildcard PIN",
1062 dev_pw, dev_pw_len);
1067 int wps_registrar_wps_cancel(struct wps_registrar *reg)
1070 wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
1071 wps_registrar_pbc_timeout(reg, NULL);
1072 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1074 } else if (reg->selected_registrar) {
1076 wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
1077 wps_registrar_pin_completed(reg);
1078 wps_registrar_invalidate_wildcard_pin(reg, NULL, 0);
1086 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
1087 * @reg: Registrar data from wps_registrar_init()
1088 * @addr: MAC address of the Probe Request sender
1089 * @wps_data: WPS IE contents
1091 * This function is called on an AP when a Probe Request with WPS IE is
1092 * received. This is used to track PBC mode use and to detect possible overlap
1093 * situation with other WPS APs.
1095 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
1096 const struct wpabuf *wps_data,
1099 struct wps_parse_attr attr;
1102 wpa_hexdump_buf(MSG_MSGDUMP,
1103 "WPS: Probe Request with WPS data received",
1106 if (wps_parse_msg(wps_data, &attr) < 0)
1109 if (attr.config_methods == NULL) {
1110 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
1115 if (attr.dev_password_id == NULL) {
1116 wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
1117 "in Probe Request");
1121 if (reg->enrollee_seen_cb && attr.uuid_e &&
1122 attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
1123 char *dev_name = NULL;
1124 if (attr.dev_name) {
1125 dev_name = os_zalloc(attr.dev_name_len + 1);
1127 os_memcpy(dev_name, attr.dev_name,
1131 reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
1132 attr.primary_dev_type,
1133 WPA_GET_BE16(attr.config_methods),
1134 WPA_GET_BE16(attr.dev_password_id),
1135 *attr.request_type, dev_name);
1139 if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
1140 return; /* Not PBC */
1142 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
1143 MACSTR, MAC2STR(addr));
1144 if (attr.uuid_e == NULL) {
1145 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
1149 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E from Probe Request", attr.uuid_e,
1152 #ifdef WPS_WORKAROUNDS
1153 if (reg->pbc_ignore_start.sec &&
1154 os_memcmp(attr.uuid_e, reg->pbc_ignore_uuid, WPS_UUID_LEN) == 0) {
1155 struct os_reltime now, dur;
1156 os_get_reltime(&now);
1157 os_reltime_sub(&now, ®->pbc_ignore_start, &dur);
1158 if (dur.sec >= 0 && dur.sec < 5) {
1159 wpa_printf(MSG_DEBUG, "WPS: Ignore PBC activation "
1160 "based on Probe Request from the Enrollee "
1161 "that just completed PBC provisioning");
1164 reg->pbc_ignore_start.sec = 0;
1166 #endif /* WPS_WORKAROUNDS */
1169 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
1170 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
1171 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
1172 reg->force_pbc_overlap = 1;
1173 wps_pbc_overlap_event(reg->wps);
1178 int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
1179 const u8 *p2p_dev_addr, const u8 *psk, size_t psk_len)
1181 if (reg->new_psk_cb == NULL)
1184 return reg->new_psk_cb(reg->cb_ctx, mac_addr, p2p_dev_addr, psk,
1189 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
1190 const struct wps_device_data *dev)
1192 if (reg->pin_needed_cb == NULL)
1195 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
1199 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
1200 const u8 *uuid_e, const u8 *dev_pw,
1203 if (reg->reg_success_cb == NULL)
1206 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e, dev_pw, dev_pw_len);
1210 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
1211 struct wpabuf *probe_resp_ie)
1213 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
1217 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
1220 if (reg->set_sel_reg_cb == NULL)
1223 if (reg->selected_registrar) {
1224 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
1225 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
1226 WPS_CONFIG_PHY_PUSHBUTTON);
1228 wps_set_pushbutton(&methods, reg->wps->config_methods);
1231 wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1232 "config_methods=0x%x pbc=%d methods=0x%x",
1233 reg->selected_registrar, reg->wps->config_methods,
1236 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
1237 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
1242 static int wps_set_ie(struct wps_registrar *reg)
1244 struct wpabuf *beacon;
1245 struct wpabuf *probe;
1246 const u8 *auth_macs;
1248 size_t vendor_len = 0;
1251 if (reg->set_ie_cb == NULL)
1254 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
1255 if (reg->wps->dev.vendor_ext[i]) {
1256 vendor_len += 2 + 2;
1257 vendor_len += wpabuf_len(reg->wps->dev.vendor_ext[i]);
1261 beacon = wpabuf_alloc(400 + vendor_len);
1264 probe = wpabuf_alloc(500 + vendor_len);
1265 if (probe == NULL) {
1266 wpabuf_free(beacon);
1270 auth_macs = wps_authorized_macs(reg, &count);
1272 wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
1274 if (wps_build_version(beacon) ||
1275 wps_build_wps_state(reg->wps, beacon) ||
1276 wps_build_ap_setup_locked(reg->wps, beacon) ||
1277 wps_build_selected_registrar(reg, beacon) ||
1278 wps_build_sel_reg_dev_password_id(reg, beacon) ||
1279 wps_build_sel_reg_config_methods(reg, beacon) ||
1280 wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
1281 (reg->dualband && wps_build_rf_bands(®->wps->dev, beacon, 0)) ||
1282 wps_build_wfa_ext(beacon, 0, auth_macs, count) ||
1283 wps_build_vendor_ext(®->wps->dev, beacon)) {
1284 wpabuf_free(beacon);
1290 if (wps_build_dev_name(®->wps->dev, beacon) ||
1291 wps_build_primary_dev_type(®->wps->dev, beacon)) {
1292 wpabuf_free(beacon);
1296 #endif /* CONFIG_P2P */
1298 wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
1300 if (wps_build_version(probe) ||
1301 wps_build_wps_state(reg->wps, probe) ||
1302 wps_build_ap_setup_locked(reg->wps, probe) ||
1303 wps_build_selected_registrar(reg, probe) ||
1304 wps_build_sel_reg_dev_password_id(reg, probe) ||
1305 wps_build_sel_reg_config_methods(reg, probe) ||
1306 wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
1307 WPS_RESP_REGISTRAR) ||
1308 wps_build_uuid_e(probe, reg->wps->uuid) ||
1309 wps_build_device_attrs(®->wps->dev, probe) ||
1310 wps_build_probe_config_methods(reg, probe) ||
1311 (reg->dualband && wps_build_rf_bands(®->wps->dev, probe, 0)) ||
1312 wps_build_wfa_ext(probe, 0, auth_macs, count) ||
1313 wps_build_vendor_ext(®->wps->dev, probe)) {
1314 wpabuf_free(beacon);
1319 beacon = wps_ie_encapsulate(beacon);
1320 probe = wps_ie_encapsulate(probe);
1322 if (!beacon || !probe) {
1323 wpabuf_free(beacon);
1328 if (reg->static_wep_only) {
1330 * Windows XP and Vista clients can get confused about
1331 * EAP-Identity/Request when they probe the network with
1332 * EAPOL-Start. In such a case, they may assume the network is
1333 * using IEEE 802.1X and prompt user for a certificate while
1334 * the correct (non-WPS) behavior would be to ask for the
1335 * static WEP key. As a workaround, use Microsoft Provisioning
1336 * IE to advertise that legacy 802.1X is not supported.
1338 const u8 ms_wps[7] = {
1339 WLAN_EID_VENDOR_SPECIFIC, 5,
1340 /* Microsoft Provisioning IE (00:50:f2:5) */
1341 0x00, 0x50, 0xf2, 5,
1342 0x00 /* no legacy 802.1X or MS WPS */
1344 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
1345 "into Beacon/Probe Response frames");
1346 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
1347 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
1350 return wps_cb_set_ie(reg, beacon, probe);
1354 static int wps_get_dev_password(struct wps_data *wps)
1359 bin_clear_free(wps->dev_password, wps->dev_password_len);
1360 wps->dev_password = NULL;
1363 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
1364 pin = (const u8 *) "00000000";
1366 #ifdef CONFIG_WPS_NFC
1367 } else if (wps->nfc_pw_token) {
1368 if (wps->nfc_pw_token->pw_id == DEV_PW_NFC_CONNECTION_HANDOVER)
1370 wpa_printf(MSG_DEBUG, "WPS: Using NFC connection "
1371 "handover and abbreviated WPS handshake "
1372 "without Device Password");
1375 wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from NFC "
1377 pin = wps->nfc_pw_token->dev_pw;
1378 pin_len = wps->nfc_pw_token->dev_pw_len;
1379 } else if (wps->dev_pw_id >= 0x10 &&
1380 wps->wps->ap_nfc_dev_pw_id == wps->dev_pw_id &&
1381 wps->wps->ap_nfc_dev_pw) {
1382 wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from own NFC Password Token");
1383 pin = wpabuf_head(wps->wps->ap_nfc_dev_pw);
1384 pin_len = wpabuf_len(wps->wps->ap_nfc_dev_pw);
1385 #endif /* CONFIG_WPS_NFC */
1387 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1389 if (pin && wps->dev_pw_id >= 0x10) {
1390 wpa_printf(MSG_DEBUG, "WPS: No match for OOB Device "
1391 "Password ID, but PIN found");
1393 * See whether Enrollee is willing to use PIN instead.
1395 wps->dev_pw_id = DEV_PW_DEFAULT;
1399 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1400 "the Enrollee (context %p registrar %p)",
1401 wps->wps, wps->wps->registrar);
1402 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1407 wps->dev_password = os_malloc(pin_len);
1408 if (wps->dev_password == NULL)
1410 os_memcpy(wps->dev_password, pin, pin_len);
1411 wps->dev_password_len = pin_len;
1417 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1419 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1420 wpabuf_put_be16(msg, ATTR_UUID_R);
1421 wpabuf_put_be16(msg, WPS_UUID_LEN);
1422 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1427 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1433 if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1435 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1436 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1437 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1439 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1440 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1441 "R-Hash derivation");
1445 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1446 wpabuf_put_be16(msg, ATTR_R_HASH1);
1447 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1448 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1449 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1450 addr[0] = wps->snonce;
1451 len[0] = WPS_SECRET_NONCE_LEN;
1452 addr[1] = wps->psk1;
1453 len[1] = WPS_PSK_LEN;
1454 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1455 len[2] = wpabuf_len(wps->dh_pubkey_e);
1456 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1457 len[3] = wpabuf_len(wps->dh_pubkey_r);
1458 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1459 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1461 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1462 wpabuf_put_be16(msg, ATTR_R_HASH2);
1463 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1464 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1465 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1466 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1467 addr[1] = wps->psk2;
1468 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1469 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1475 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1477 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1478 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1479 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1480 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1485 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1487 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1488 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1489 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1490 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1491 WPS_SECRET_NONCE_LEN);
1496 static int wps_build_cred_network_idx(struct wpabuf *msg,
1497 const struct wps_credential *cred)
1499 wpa_printf(MSG_DEBUG, "WPS: * Network Index (1)");
1500 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1501 wpabuf_put_be16(msg, 1);
1502 wpabuf_put_u8(msg, 1);
1507 static int wps_build_cred_ssid(struct wpabuf *msg,
1508 const struct wps_credential *cred)
1510 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1511 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID for Credential",
1512 cred->ssid, cred->ssid_len);
1513 wpabuf_put_be16(msg, ATTR_SSID);
1514 wpabuf_put_be16(msg, cred->ssid_len);
1515 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1520 static int wps_build_cred_auth_type(struct wpabuf *msg,
1521 const struct wps_credential *cred)
1523 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1525 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1526 wpabuf_put_be16(msg, 2);
1527 wpabuf_put_be16(msg, cred->auth_type);
1532 static int wps_build_cred_encr_type(struct wpabuf *msg,
1533 const struct wps_credential *cred)
1535 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1537 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1538 wpabuf_put_be16(msg, 2);
1539 wpabuf_put_be16(msg, cred->encr_type);
1544 static int wps_build_cred_network_key(struct wpabuf *msg,
1545 const struct wps_credential *cred)
1547 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1548 (int) cred->key_len);
1549 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
1550 cred->key, cred->key_len);
1551 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1552 wpabuf_put_be16(msg, cred->key_len);
1553 wpabuf_put_data(msg, cred->key, cred->key_len);
1558 static int wps_build_credential(struct wpabuf *msg,
1559 const struct wps_credential *cred)
1561 if (wps_build_cred_network_idx(msg, cred) ||
1562 wps_build_cred_ssid(msg, cred) ||
1563 wps_build_cred_auth_type(msg, cred) ||
1564 wps_build_cred_encr_type(msg, cred) ||
1565 wps_build_cred_network_key(msg, cred) ||
1566 wps_build_mac_addr(msg, cred->mac_addr))
1572 int wps_build_credential_wrap(struct wpabuf *msg,
1573 const struct wps_credential *cred)
1575 struct wpabuf *wbuf;
1576 wbuf = wpabuf_alloc(200);
1579 if (wps_build_credential(wbuf, cred)) {
1583 wpabuf_put_be16(msg, ATTR_CRED);
1584 wpabuf_put_be16(msg, wpabuf_len(wbuf));
1585 wpabuf_put_buf(msg, wbuf);
1591 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1593 struct wpabuf *cred;
1595 if (wps->wps->registrar->skip_cred_build)
1596 goto skip_cred_build;
1598 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1599 if (wps->use_cred) {
1600 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1603 os_memset(&wps->cred, 0, sizeof(wps->cred));
1605 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1606 wps->cred.ssid_len = wps->wps->ssid_len;
1608 /* Select the best authentication and encryption type */
1609 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1610 wps->auth_type = WPS_AUTH_WPA2PSK;
1611 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1612 wps->auth_type = WPS_AUTH_WPAPSK;
1613 else if (wps->auth_type & WPS_AUTH_OPEN)
1614 wps->auth_type = WPS_AUTH_OPEN;
1616 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1620 wps->cred.auth_type = wps->auth_type;
1622 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1623 wps->auth_type == WPS_AUTH_WPAPSK) {
1624 if (wps->encr_type & WPS_ENCR_AES)
1625 wps->encr_type = WPS_ENCR_AES;
1626 else if (wps->encr_type & WPS_ENCR_TKIP)
1627 wps->encr_type = WPS_ENCR_TKIP;
1629 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1630 "type for WPA/WPA2");
1634 if (wps->encr_type & WPS_ENCR_NONE)
1635 wps->encr_type = WPS_ENCR_NONE;
1636 #ifdef CONFIG_TESTING_OPTIONS
1637 else if (wps->encr_type & WPS_ENCR_WEP)
1638 wps->encr_type = WPS_ENCR_WEP;
1639 #endif /* CONFIG_TESTING_OPTIONS */
1641 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1642 "type for non-WPA/WPA2 mode");
1646 wps->cred.encr_type = wps->encr_type;
1648 * Set MAC address in the Credential to be the Enrollee's MAC address
1650 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1652 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1653 !wps->wps->registrar->disable_auto_conf) {
1655 /* Generate a random passphrase */
1656 if (random_pool_ready() != 1 ||
1657 random_get_bytes(r, sizeof(r)) < 0) {
1658 wpa_printf(MSG_INFO,
1659 "WPS: Could not generate random PSK");
1662 os_free(wps->new_psk);
1663 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1664 if (wps->new_psk == NULL)
1666 wps->new_psk_len--; /* remove newline */
1667 while (wps->new_psk_len &&
1668 wps->new_psk[wps->new_psk_len - 1] == '=')
1670 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1671 wps->new_psk, wps->new_psk_len);
1672 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1673 wps->cred.key_len = wps->new_psk_len;
1674 } else if (!wps->wps->registrar->force_per_enrollee_psk &&
1675 wps->use_psk_key && wps->wps->psk_set) {
1677 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1678 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1679 os_memcpy(wps->cred.key, hex, 32 * 2);
1680 wps->cred.key_len = 32 * 2;
1681 } else if (!wps->wps->registrar->force_per_enrollee_psk &&
1682 wps->wps->network_key) {
1683 os_memcpy(wps->cred.key, wps->wps->network_key,
1684 wps->wps->network_key_len);
1685 wps->cred.key_len = wps->wps->network_key_len;
1686 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1688 /* Generate a random per-device PSK */
1689 os_free(wps->new_psk);
1690 wps->new_psk_len = 32;
1691 wps->new_psk = os_malloc(wps->new_psk_len);
1692 if (wps->new_psk == NULL)
1694 if (random_pool_ready() != 1 ||
1695 random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
1696 wpa_printf(MSG_INFO,
1697 "WPS: Could not generate random PSK");
1698 os_free(wps->new_psk);
1699 wps->new_psk = NULL;
1702 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1703 wps->new_psk, wps->new_psk_len);
1704 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1706 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1707 wps->cred.key_len = wps->new_psk_len * 2;
1711 #ifdef CONFIG_WPS_TESTING
1712 if (wps_testing_dummy_cred)
1713 cred = wpabuf_alloc(200);
1717 struct wps_credential dummy;
1718 wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
1719 os_memset(&dummy, 0, sizeof(dummy));
1720 os_memcpy(dummy.ssid, "dummy", 5);
1722 dummy.auth_type = WPS_AUTH_WPA2PSK;
1723 dummy.encr_type = WPS_ENCR_AES;
1724 os_memcpy(dummy.key, "dummy psk", 9);
1726 os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
1727 wps_build_credential(cred, &dummy);
1728 wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
1730 wpabuf_put_be16(msg, ATTR_CRED);
1731 wpabuf_put_be16(msg, wpabuf_len(cred));
1732 wpabuf_put_buf(msg, cred);
1736 #endif /* CONFIG_WPS_TESTING */
1738 cred = wpabuf_alloc(200);
1742 if (wps_build_credential(cred, &wps->cred)) {
1747 wpabuf_put_be16(msg, ATTR_CRED);
1748 wpabuf_put_be16(msg, wpabuf_len(cred));
1749 wpabuf_put_buf(msg, cred);
1753 if (wps->wps->registrar->extra_cred) {
1754 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1755 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1762 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1764 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1766 if (wps_build_credential(msg, &wps->cred))
1773 static struct wpabuf * wps_build_ap_cred(struct wps_data *wps)
1775 struct wpabuf *msg, *plain;
1777 msg = wpabuf_alloc(1000);
1781 plain = wpabuf_alloc(200);
1782 if (plain == NULL) {
1787 if (wps_build_ap_settings(wps, plain)) {
1793 wpabuf_put_be16(msg, ATTR_CRED);
1794 wpabuf_put_be16(msg, wpabuf_len(plain));
1795 wpabuf_put_buf(msg, plain);
1802 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1805 int config_in_m2 = 0;
1807 if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
1809 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1810 wps->nonce_r, WPS_NONCE_LEN);
1811 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1813 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1814 msg = wpabuf_alloc(1000);
1818 if (wps_build_version(msg) ||
1819 wps_build_msg_type(msg, WPS_M2) ||
1820 wps_build_enrollee_nonce(wps, msg) ||
1821 wps_build_registrar_nonce(wps, msg) ||
1822 wps_build_uuid_r(wps, msg) ||
1823 wps_build_public_key(wps, msg) ||
1824 wps_derive_keys(wps) ||
1825 wps_build_auth_type_flags(wps, msg) ||
1826 wps_build_encr_type_flags(wps, msg) ||
1827 wps_build_conn_type_flags(wps, msg) ||
1828 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1829 wps_build_device_attrs(&wps->wps->dev, msg) ||
1830 wps_build_rf_bands(&wps->wps->dev, msg,
1831 wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
1832 wps_build_assoc_state(wps, msg) ||
1833 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1834 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1835 wps_build_os_version(&wps->wps->dev, msg) ||
1836 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1841 #ifdef CONFIG_WPS_NFC
1842 if (wps->nfc_pw_token && wps->nfc_pw_token->pk_hash_provided_oob &&
1843 wps->nfc_pw_token->pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1845 * Use abbreviated handshake since public key hash allowed
1846 * Enrollee to validate our public key similarly to how Enrollee
1847 * public key was validated. There is no need to validate Device
1848 * Password in this case.
1850 struct wpabuf *plain = wpabuf_alloc(500);
1851 if (plain == NULL ||
1852 wps_build_cred(wps, plain) ||
1853 wps_build_key_wrap_auth(wps, plain) ||
1854 wps_build_encr_settings(wps, msg, plain)) {
1862 #endif /* CONFIG_WPS_NFC */
1864 if (wps_build_authenticator(wps, msg)) {
1870 wps->state = config_in_m2 ? RECV_DONE : RECV_M3;
1875 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1878 u16 err = wps->config_error;
1880 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1881 msg = wpabuf_alloc(1000);
1885 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1886 err == WPS_CFG_NO_ERROR)
1887 err = WPS_CFG_SETUP_LOCKED;
1889 if (wps_build_version(msg) ||
1890 wps_build_msg_type(msg, WPS_M2D) ||
1891 wps_build_enrollee_nonce(wps, msg) ||
1892 wps_build_registrar_nonce(wps, msg) ||
1893 wps_build_uuid_r(wps, msg) ||
1894 wps_build_auth_type_flags(wps, msg) ||
1895 wps_build_encr_type_flags(wps, msg) ||
1896 wps_build_conn_type_flags(wps, msg) ||
1897 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1898 wps_build_device_attrs(&wps->wps->dev, msg) ||
1899 wps_build_rf_bands(&wps->wps->dev, msg,
1900 wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
1901 wps_build_assoc_state(wps, msg) ||
1902 wps_build_config_error(msg, err) ||
1903 wps_build_os_version(&wps->wps->dev, msg) ||
1904 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1909 wps->state = RECV_M2D_ACK;
1914 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1916 struct wpabuf *msg, *plain;
1918 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1920 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1922 plain = wpabuf_alloc(200);
1926 msg = wpabuf_alloc(1000);
1932 if (wps_build_version(msg) ||
1933 wps_build_msg_type(msg, WPS_M4) ||
1934 wps_build_enrollee_nonce(wps, msg) ||
1935 wps_build_r_hash(wps, msg) ||
1936 wps_build_r_snonce1(wps, plain) ||
1937 wps_build_key_wrap_auth(wps, plain) ||
1938 wps_build_encr_settings(wps, msg, plain) ||
1939 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1940 wps_build_authenticator(wps, msg)) {
1947 wps->state = RECV_M5;
1952 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1954 struct wpabuf *msg, *plain;
1956 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1958 plain = wpabuf_alloc(200);
1962 msg = wpabuf_alloc(1000);
1968 if (wps_build_version(msg) ||
1969 wps_build_msg_type(msg, WPS_M6) ||
1970 wps_build_enrollee_nonce(wps, msg) ||
1971 wps_build_r_snonce2(wps, plain) ||
1972 wps_build_key_wrap_auth(wps, plain) ||
1973 wps_build_encr_settings(wps, msg, plain) ||
1974 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1975 wps_build_authenticator(wps, msg)) {
1982 wps->wps_pin_revealed = 1;
1983 wps->state = RECV_M7;
1988 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1990 struct wpabuf *msg, *plain;
1992 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1994 plain = wpabuf_alloc(500);
1998 msg = wpabuf_alloc(1000);
2004 if (wps_build_version(msg) ||
2005 wps_build_msg_type(msg, WPS_M8) ||
2006 wps_build_enrollee_nonce(wps, msg) ||
2007 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
2008 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
2009 wps_build_key_wrap_auth(wps, plain) ||
2010 wps_build_encr_settings(wps, msg, plain) ||
2011 wps_build_wfa_ext(msg, 0, NULL, 0) ||
2012 wps_build_authenticator(wps, msg)) {
2019 wps->state = RECV_DONE;
2024 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
2025 enum wsc_op_code *op_code)
2029 #ifdef CONFIG_WPS_UPNP
2030 if (!wps->int_reg && wps->wps->wps_upnp) {
2031 struct upnp_pending_message *p, *prev = NULL;
2032 if (wps->ext_reg > 1)
2033 wps_registrar_free_pending_m2(wps->wps);
2034 p = wps->wps->upnp_msgs;
2035 /* TODO: check pending message MAC address */
2036 while (p && p->next) {
2041 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
2046 wps->wps->upnp_msgs = NULL;
2053 *op_code = WSC_NACK;
2060 if (wps->ext_reg == 0)
2066 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
2067 "pending message available");
2070 #endif /* CONFIG_WPS_UPNP */
2072 switch (wps->state) {
2074 if (wps_get_dev_password(wps) < 0)
2075 msg = wps_build_m2d(wps);
2077 msg = wps_build_m2(wps);
2081 msg = wps_build_m2d(wps);
2085 msg = wps_build_m4(wps);
2089 msg = wps_build_m6(wps);
2093 msg = wps_build_m8(wps);
2097 msg = wps_build_wsc_ack(wps);
2101 msg = wps_build_wsc_nack(wps);
2102 *op_code = WSC_NACK;
2105 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
2106 "a message", wps->state);
2111 if (*op_code == WSC_MSG && msg) {
2112 /* Save a copy of the last message for Authenticator derivation
2114 wpabuf_free(wps->last_msg);
2115 wps->last_msg = wpabuf_dup(msg);
2122 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
2124 if (e_nonce == NULL) {
2125 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
2129 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
2130 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
2131 wps->nonce_e, WPS_NONCE_LEN);
2137 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
2139 if (r_nonce == NULL) {
2140 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
2144 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
2145 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
2153 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
2155 if (uuid_e == NULL) {
2156 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
2160 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
2161 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
2167 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
2169 if (pw_id == NULL) {
2170 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
2174 wps->dev_pw_id = WPA_GET_BE16(pw_id);
2175 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
2181 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
2183 if (e_hash1 == NULL) {
2184 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
2188 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
2189 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
2195 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
2197 if (e_hash2 == NULL) {
2198 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
2202 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
2203 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
2209 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
2211 u8 hash[SHA256_MAC_LEN];
2215 if (e_snonce1 == NULL) {
2216 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
2220 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
2221 WPS_SECRET_NONCE_LEN);
2223 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
2224 addr[0] = e_snonce1;
2225 len[0] = WPS_SECRET_NONCE_LEN;
2226 addr[1] = wps->psk1;
2227 len[1] = WPS_PSK_LEN;
2228 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2229 len[2] = wpabuf_len(wps->dh_pubkey_e);
2230 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2231 len[3] = wpabuf_len(wps->dh_pubkey_r);
2232 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2234 if (os_memcmp_const(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
2235 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
2236 "not match with the pre-committed value");
2237 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2238 wps_pwd_auth_fail_event(wps->wps, 0, 1, wps->mac_addr_e);
2242 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
2243 "half of the device password");
2249 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
2251 u8 hash[SHA256_MAC_LEN];
2255 if (e_snonce2 == NULL) {
2256 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
2260 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
2261 WPS_SECRET_NONCE_LEN);
2263 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
2264 addr[0] = e_snonce2;
2265 len[0] = WPS_SECRET_NONCE_LEN;
2266 addr[1] = wps->psk2;
2267 len[1] = WPS_PSK_LEN;
2268 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2269 len[2] = wpabuf_len(wps->dh_pubkey_e);
2270 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2271 len[3] = wpabuf_len(wps->dh_pubkey_r);
2272 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2274 if (os_memcmp_const(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
2275 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
2276 "not match with the pre-committed value");
2277 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2278 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2279 wps_pwd_auth_fail_event(wps->wps, 0, 2, wps->mac_addr_e);
2283 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
2284 "half of the device password");
2285 wps->wps_pin_revealed = 0;
2286 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
2289 * In case wildcard PIN is used and WPS handshake succeeds in the first
2290 * attempt, wps_registrar_unlock_pin() would not free the PIN, so make
2291 * sure the PIN gets invalidated here.
2293 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2299 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
2301 if (mac_addr == NULL) {
2302 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
2306 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
2308 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
2309 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
2315 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
2318 if (pk == NULL || pk_len == 0) {
2319 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
2323 wpabuf_free(wps->dh_pubkey_e);
2324 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
2325 if (wps->dh_pubkey_e == NULL)
2332 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
2337 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
2342 auth_types = WPA_GET_BE16(auth);
2344 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
2346 wps->auth_type = wps->wps->auth_types & auth_types;
2347 if (wps->auth_type == 0) {
2348 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2349 "authentication types (own 0x%x Enrollee 0x%x)",
2350 wps->wps->auth_types, auth_types);
2351 #ifdef WPS_WORKAROUNDS
2353 * Some deployed implementations seem to advertise incorrect
2354 * information in this attribute. For example, Linksys WRT350N
2355 * seems to have a byteorder bug that breaks this negotiation.
2356 * In order to interoperate with existing implementations,
2357 * assume that the Enrollee supports everything we do.
2359 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2360 "does not advertise supported authentication types "
2362 wps->auth_type = wps->wps->auth_types;
2363 #else /* WPS_WORKAROUNDS */
2365 #endif /* WPS_WORKAROUNDS */
2372 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
2377 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
2382 encr_types = WPA_GET_BE16(encr);
2384 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
2386 wps->encr_type = wps->wps->encr_types & encr_types;
2387 if (wps->encr_type == 0) {
2388 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2389 "encryption types (own 0x%x Enrollee 0x%x)",
2390 wps->wps->encr_types, encr_types);
2391 #ifdef WPS_WORKAROUNDS
2393 * Some deployed implementations seem to advertise incorrect
2394 * information in this attribute. For example, Linksys WRT350N
2395 * seems to have a byteorder bug that breaks this negotiation.
2396 * In order to interoperate with existing implementations,
2397 * assume that the Enrollee supports everything we do.
2399 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2400 "does not advertise supported encryption types "
2402 wps->encr_type = wps->wps->encr_types;
2403 #else /* WPS_WORKAROUNDS */
2405 #endif /* WPS_WORKAROUNDS */
2412 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
2415 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
2420 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
2427 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
2431 if (methods == NULL) {
2432 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
2436 m = WPA_GET_BE16(methods);
2438 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
2439 "%s%s%s%s%s%s%s%s%s", m,
2440 m & WPS_CONFIG_USBA ? " [USBA]" : "",
2441 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
2442 m & WPS_CONFIG_LABEL ? " [Label]" : "",
2443 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
2444 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
2445 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
2446 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
2447 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
2448 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
2450 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
2452 * The Enrollee does not have a display so it is unlikely to be
2453 * able to show the passphrase to a user and as such, could
2454 * benefit from receiving PSK to reduce key derivation time.
2456 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2457 "Enrollee not supporting display");
2458 wps->use_psk_key = 1;
2465 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2467 if (state == NULL) {
2468 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2473 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2480 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2484 if (assoc == NULL) {
2485 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2489 a = WPA_GET_BE16(assoc);
2490 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2496 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2501 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2505 e = WPA_GET_BE16(err);
2506 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2512 static int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
2515 struct wps_registrar *reg = wps->wps->registrar;
2517 if (is_zero_ether_addr(reg->p2p_dev_addr))
2518 return 1; /* no filtering in use */
2520 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
2521 wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
2522 "filtering for PBC: expected " MACSTR " was "
2523 MACSTR " - indicate PBC session overlap",
2524 MAC2STR(reg->p2p_dev_addr),
2525 MAC2STR(wps->p2p_dev_addr));
2528 #endif /* CONFIG_P2P */
2533 static int wps_registrar_skip_overlap(struct wps_data *wps)
2536 struct wps_registrar *reg = wps->wps->registrar;
2538 if (is_zero_ether_addr(reg->p2p_dev_addr))
2539 return 0; /* no specific Enrollee selected */
2541 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) == 0) {
2542 wpa_printf(MSG_DEBUG, "WPS: Skip PBC overlap due to selected "
2546 #endif /* CONFIG_P2P */
2551 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2552 struct wps_parse_attr *attr)
2554 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2556 if (wps->state != RECV_M1) {
2557 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2558 "receiving M1", wps->state);
2562 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2563 wps_process_mac_addr(wps, attr->mac_addr) ||
2564 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2565 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2566 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2567 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2568 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2569 wps_process_config_methods(wps, attr->config_methods) ||
2570 wps_process_wps_state(wps, attr->wps_state) ||
2571 wps_process_device_attrs(&wps->peer_dev, attr) ||
2572 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2573 wps_process_assoc_state(wps, attr->assoc_state) ||
2574 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2575 wps_process_config_error(wps, attr->config_error) ||
2576 wps_process_os_version(&wps->peer_dev, attr->os_version))
2579 if (wps->dev_pw_id < 0x10 &&
2580 wps->dev_pw_id != DEV_PW_DEFAULT &&
2581 wps->dev_pw_id != DEV_PW_P2PS_DEFAULT &&
2582 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2583 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2584 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2585 #ifdef CONFIG_WPS_NFC
2586 wps->dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER &&
2587 #endif /* CONFIG_WPS_NFC */
2588 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2589 !wps->wps->registrar->pbc)) {
2590 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2592 wps->state = SEND_M2D;
2593 return WPS_CONTINUE;
2596 #ifdef CONFIG_WPS_NFC
2597 if (wps->dev_pw_id >= 0x10 ||
2598 wps->dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
2599 struct wps_nfc_pw_token *token;
2601 u8 hash[WPS_HASH_LEN];
2603 wpa_printf(MSG_DEBUG, "WPS: Searching for NFC token match for id=%d (ctx %p registrar %p)",
2604 wps->dev_pw_id, wps->wps, wps->wps->registrar);
2605 token = wps_get_nfc_pw_token(
2606 &wps->wps->registrar->nfc_pw_tokens, wps->dev_pw_id);
2607 if (token && token->peer_pk_hash_known) {
2610 wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
2612 dl_list_del(&token->list);
2613 wps->nfc_pw_token = token;
2615 addr[0] = attr->public_key;
2616 len = attr->public_key_len;
2617 sha256_vector(1, addr, &len, hash);
2618 if (os_memcmp_const(hash,
2619 wps->nfc_pw_token->pubkey_hash,
2620 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
2621 wpa_printf(MSG_ERROR, "WPS: Public Key hash "
2623 wps->state = SEND_M2D;
2625 WPS_CFG_PUBLIC_KEY_HASH_MISMATCH;
2626 return WPS_CONTINUE;
2629 wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
2630 "Password Token (no peer PK hash)");
2631 wps->nfc_pw_token = token;
2632 } else if (wps->dev_pw_id >= 0x10 &&
2633 wps->wps->ap_nfc_dev_pw_id == wps->dev_pw_id &&
2634 wps->wps->ap_nfc_dev_pw) {
2635 wpa_printf(MSG_DEBUG, "WPS: Found match with own NFC Password Token");
2638 #endif /* CONFIG_WPS_NFC */
2640 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2641 if ((wps->wps->registrar->force_pbc_overlap ||
2642 wps_registrar_pbc_overlap(wps->wps->registrar,
2643 wps->mac_addr_e, wps->uuid_e) ||
2644 !wps_registrar_p2p_dev_addr_match(wps)) &&
2645 !wps_registrar_skip_overlap(wps)) {
2646 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2648 wps->state = SEND_M2D;
2649 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2650 wps_pbc_overlap_event(wps->wps);
2651 wps_fail_event(wps->wps, WPS_M1,
2652 WPS_CFG_MULTIPLE_PBC_DETECTED,
2653 WPS_EI_NO_ERROR, wps->mac_addr_e);
2654 wps->wps->registrar->force_pbc_overlap = 1;
2655 return WPS_CONTINUE;
2657 wps_registrar_add_pbc_session(wps->wps->registrar,
2658 wps->mac_addr_e, wps->uuid_e);
2662 #ifdef WPS_WORKAROUNDS
2664 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2665 * passphrase format. To avoid interop issues, force PSK format to be
2668 if (!wps->use_psk_key &&
2669 wps->peer_dev.manufacturer &&
2670 os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
2671 wps->peer_dev.model_name &&
2672 os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
2673 wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
2675 wps->use_psk_key = 1;
2677 #endif /* WPS_WORKAROUNDS */
2679 wps->state = SEND_M2;
2680 return WPS_CONTINUE;
2684 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2685 const struct wpabuf *msg,
2686 struct wps_parse_attr *attr)
2688 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2690 if (wps->state != RECV_M3) {
2691 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2692 "receiving M3", wps->state);
2693 wps->state = SEND_WSC_NACK;
2694 return WPS_CONTINUE;
2697 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2698 !wps_registrar_skip_overlap(wps)) {
2699 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2701 wps->state = SEND_WSC_NACK;
2702 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2703 return WPS_CONTINUE;
2706 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2707 wps_process_authenticator(wps, attr->authenticator, msg) ||
2708 wps_process_e_hash1(wps, attr->e_hash1) ||
2709 wps_process_e_hash2(wps, attr->e_hash2)) {
2710 wps->state = SEND_WSC_NACK;
2711 return WPS_CONTINUE;
2714 wps->state = SEND_M4;
2715 return WPS_CONTINUE;
2719 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2720 const struct wpabuf *msg,
2721 struct wps_parse_attr *attr)
2723 struct wpabuf *decrypted;
2724 struct wps_parse_attr eattr;
2726 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2728 if (wps->state != RECV_M5) {
2729 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2730 "receiving M5", wps->state);
2731 wps->state = SEND_WSC_NACK;
2732 return WPS_CONTINUE;
2735 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2736 !wps_registrar_skip_overlap(wps)) {
2737 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2739 wps->state = SEND_WSC_NACK;
2740 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2741 return WPS_CONTINUE;
2744 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2745 wps_process_authenticator(wps, attr->authenticator, msg)) {
2746 wps->state = SEND_WSC_NACK;
2747 return WPS_CONTINUE;
2750 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2751 attr->encr_settings_len);
2752 if (decrypted == NULL) {
2753 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2754 "Settings attribute");
2755 wps->state = SEND_WSC_NACK;
2756 return WPS_CONTINUE;
2759 if (wps_validate_m5_encr(decrypted, attr->version2 != NULL) < 0) {
2760 wpabuf_free(decrypted);
2761 wps->state = SEND_WSC_NACK;
2762 return WPS_CONTINUE;
2765 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2767 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2768 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2769 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2770 wpabuf_free(decrypted);
2771 wps->state = SEND_WSC_NACK;
2772 return WPS_CONTINUE;
2774 wpabuf_free(decrypted);
2776 wps->state = SEND_M6;
2777 return WPS_CONTINUE;
2781 static void wps_sta_cred_cb(struct wps_data *wps)
2784 * Update credential to only include a single authentication and
2785 * encryption type in case the AP configuration includes more than one
2788 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2789 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2790 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2791 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2792 if (wps->cred.encr_type & WPS_ENCR_AES)
2793 wps->cred.encr_type = WPS_ENCR_AES;
2794 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2795 wps->cred.encr_type = WPS_ENCR_TKIP;
2796 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2797 "AP configuration");
2798 if (wps->wps->cred_cb)
2799 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2803 static void wps_cred_update(struct wps_credential *dst,
2804 struct wps_credential *src)
2806 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2807 dst->ssid_len = src->ssid_len;
2808 dst->auth_type = src->auth_type;
2809 dst->encr_type = src->encr_type;
2810 dst->key_idx = src->key_idx;
2811 os_memcpy(dst->key, src->key, sizeof(dst->key));
2812 dst->key_len = src->key_len;
2816 static int wps_process_ap_settings_r(struct wps_data *wps,
2817 struct wps_parse_attr *attr)
2821 if (wps->wps->ap || wps->er)
2824 /* AP Settings Attributes in M7 when Enrollee is an AP */
2825 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2828 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2830 if (wps->new_ap_settings) {
2831 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2833 wps_cred_update(&wps->cred, wps->new_ap_settings);
2837 * Use the AP PIN only to receive the current AP settings, not
2838 * to reconfigure the AP.
2842 * Clear selected registrar here since we do not get to
2843 * WSC_Done in this protocol run.
2845 wps_registrar_pin_completed(wps->wps->registrar);
2847 msg = wps_build_ap_cred(wps);
2850 wps->cred.cred_attr = wpabuf_head(msg);
2851 wps->cred.cred_attr_len = wpabuf_len(msg);
2853 if (wps->ap_settings_cb) {
2854 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2859 wps_sta_cred_cb(wps);
2861 wps->cred.cred_attr = NULL;
2862 wps->cred.cred_attr_len = 0;
2870 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2871 const struct wpabuf *msg,
2872 struct wps_parse_attr *attr)
2874 struct wpabuf *decrypted;
2875 struct wps_parse_attr eattr;
2877 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2879 if (wps->state != RECV_M7) {
2880 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2881 "receiving M7", wps->state);
2882 wps->state = SEND_WSC_NACK;
2883 return WPS_CONTINUE;
2886 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2887 !wps_registrar_skip_overlap(wps)) {
2888 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2890 wps->state = SEND_WSC_NACK;
2891 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2892 return WPS_CONTINUE;
2895 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2896 wps_process_authenticator(wps, attr->authenticator, msg)) {
2897 wps->state = SEND_WSC_NACK;
2898 return WPS_CONTINUE;
2901 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2902 attr->encr_settings_len);
2903 if (decrypted == NULL) {
2904 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2905 "Settings attribute");
2906 wps->state = SEND_WSC_NACK;
2907 return WPS_CONTINUE;
2910 if (wps_validate_m7_encr(decrypted, wps->wps->ap || wps->er,
2911 attr->version2 != NULL) < 0) {
2912 wpabuf_free(decrypted);
2913 wps->state = SEND_WSC_NACK;
2914 return WPS_CONTINUE;
2917 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2919 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2920 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2921 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2922 wps_process_ap_settings_r(wps, &eattr)) {
2923 wpabuf_free(decrypted);
2924 wps->state = SEND_WSC_NACK;
2925 return WPS_CONTINUE;
2928 wpabuf_free(decrypted);
2930 wps->state = SEND_M8;
2931 return WPS_CONTINUE;
2935 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2936 const struct wpabuf *msg)
2938 struct wps_parse_attr attr;
2939 enum wps_process_res ret = WPS_CONTINUE;
2941 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2943 if (wps_parse_msg(msg, &attr) < 0)
2946 if (attr.msg_type == NULL) {
2947 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2948 wps->state = SEND_WSC_NACK;
2949 return WPS_CONTINUE;
2952 if (*attr.msg_type != WPS_M1 &&
2953 (attr.registrar_nonce == NULL ||
2954 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2955 WPS_NONCE_LEN) != 0)) {
2956 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2960 switch (*attr.msg_type) {
2962 if (wps_validate_m1(msg) < 0)
2964 #ifdef CONFIG_WPS_UPNP
2965 if (wps->wps->wps_upnp && attr.mac_addr) {
2966 /* Remove old pending messages when starting new run */
2967 wps_free_pending_msgs(wps->wps->upnp_msgs);
2968 wps->wps->upnp_msgs = NULL;
2970 upnp_wps_device_send_wlan_event(
2971 wps->wps->wps_upnp, attr.mac_addr,
2972 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2974 #endif /* CONFIG_WPS_UPNP */
2975 ret = wps_process_m1(wps, &attr);
2978 if (wps_validate_m3(msg) < 0)
2980 ret = wps_process_m3(wps, msg, &attr);
2981 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2982 wps_fail_event(wps->wps, WPS_M3, wps->config_error,
2983 wps->error_indication, wps->mac_addr_e);
2986 if (wps_validate_m5(msg) < 0)
2988 ret = wps_process_m5(wps, msg, &attr);
2989 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2990 wps_fail_event(wps->wps, WPS_M5, wps->config_error,
2991 wps->error_indication, wps->mac_addr_e);
2994 if (wps_validate_m7(msg) < 0)
2996 ret = wps_process_m7(wps, msg, &attr);
2997 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2998 wps_fail_event(wps->wps, WPS_M7, wps->config_error,
2999 wps->error_indication, wps->mac_addr_e);
3002 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
3007 if (ret == WPS_CONTINUE) {
3008 /* Save a copy of the last message for Authenticator derivation
3010 wpabuf_free(wps->last_msg);
3011 wps->last_msg = wpabuf_dup(msg);
3018 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
3019 const struct wpabuf *msg)
3021 struct wps_parse_attr attr;
3023 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
3025 if (wps_parse_msg(msg, &attr) < 0)
3028 if (attr.msg_type == NULL) {
3029 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3033 if (*attr.msg_type != WPS_WSC_ACK) {
3034 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3039 #ifdef CONFIG_WPS_UPNP
3040 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
3041 upnp_wps_subscribers(wps->wps->wps_upnp)) {
3042 if (wps->wps->upnp_msgs)
3043 return WPS_CONTINUE;
3044 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
3045 "external Registrar");
3048 #endif /* CONFIG_WPS_UPNP */
3050 if (attr.registrar_nonce == NULL ||
3051 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3053 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3057 if (attr.enrollee_nonce == NULL ||
3058 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3059 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3063 if (wps->state == RECV_M2D_ACK) {
3064 #ifdef CONFIG_WPS_UPNP
3065 if (wps->wps->wps_upnp &&
3066 upnp_wps_subscribers(wps->wps->wps_upnp)) {
3067 if (wps->wps->upnp_msgs)
3068 return WPS_CONTINUE;
3069 if (wps->ext_reg == 0)
3071 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
3072 "external Registrar");
3075 #endif /* CONFIG_WPS_UPNP */
3077 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
3078 "terminate negotiation");
3085 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
3086 const struct wpabuf *msg)
3088 struct wps_parse_attr attr;
3092 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
3094 old_state = wps->state;
3095 wps->state = SEND_WSC_NACK;
3097 if (wps_parse_msg(msg, &attr) < 0)
3100 if (attr.msg_type == NULL) {
3101 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3105 if (*attr.msg_type != WPS_WSC_NACK) {
3106 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3111 #ifdef CONFIG_WPS_UPNP
3112 if (wps->wps->wps_upnp && wps->ext_reg) {
3113 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3114 "Registrar terminated by the Enrollee");
3117 #endif /* CONFIG_WPS_UPNP */
3119 if (attr.registrar_nonce == NULL ||
3120 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3122 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3126 if (attr.enrollee_nonce == NULL ||
3127 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3128 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3132 if (attr.config_error == NULL) {
3133 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
3138 config_error = WPA_GET_BE16(attr.config_error);
3139 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
3140 "Configuration Error %d", config_error);
3142 switch (old_state) {
3144 wps_fail_event(wps->wps, WPS_M2, config_error,
3145 wps->error_indication, wps->mac_addr_e);
3148 wps_fail_event(wps->wps, WPS_M4, config_error,
3149 wps->error_indication, wps->mac_addr_e);
3152 wps_fail_event(wps->wps, WPS_M6, config_error,
3153 wps->error_indication, wps->mac_addr_e);
3156 wps_fail_event(wps->wps, WPS_M8, config_error,
3157 wps->error_indication, wps->mac_addr_e);
3167 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
3168 const struct wpabuf *msg)
3170 struct wps_parse_attr attr;
3172 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
3174 if (wps->state != RECV_DONE &&
3175 (!wps->wps->wps_upnp || !wps->ext_reg)) {
3176 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
3177 "receiving WSC_Done", wps->state);
3181 if (wps_parse_msg(msg, &attr) < 0)
3184 if (attr.msg_type == NULL) {
3185 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3189 if (*attr.msg_type != WPS_WSC_DONE) {
3190 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3195 #ifdef CONFIG_WPS_UPNP
3196 if (wps->wps->wps_upnp && wps->ext_reg) {
3197 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3198 "Registrar completed successfully");
3199 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3203 #endif /* CONFIG_WPS_UPNP */
3205 if (attr.registrar_nonce == NULL ||
3206 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3208 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3212 if (attr.enrollee_nonce == NULL ||
3213 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3214 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3218 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
3219 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3222 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
3223 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
3224 struct wps_credential cred;
3226 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
3227 "on first Enrollee connection");
3229 os_memset(&cred, 0, sizeof(cred));
3230 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
3231 cred.ssid_len = wps->wps->ssid_len;
3232 if (wps->wps->rf_band_cb(wps->wps->cb_ctx) == WPS_RF_60GHZ) {
3233 cred.auth_type = WPS_AUTH_WPA2PSK;
3234 cred.encr_type = WPS_ENCR_AES;
3236 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
3237 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
3239 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
3240 cred.key_len = wps->new_psk_len;
3242 wps->wps->wps_state = WPS_STATE_CONFIGURED;
3243 wpa_hexdump_ascii_key(MSG_DEBUG,
3244 "WPS: Generated random passphrase",
3245 wps->new_psk, wps->new_psk_len);
3246 if (wps->wps->cred_cb)
3247 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
3249 os_free(wps->new_psk);
3250 wps->new_psk = NULL;
3253 if (!wps->wps->ap && !wps->er)
3254 wps_sta_cred_cb(wps);
3257 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
3258 wps->p2p_dev_addr, wps->new_psk,
3259 wps->new_psk_len)) {
3260 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
3263 os_free(wps->new_psk);
3264 wps->new_psk = NULL;
3267 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e,
3268 wps->dev_password, wps->dev_password_len);
3271 wps_registrar_remove_pbc_session(wps->wps->registrar,
3274 wps_registrar_pbc_completed(wps->wps->registrar);
3275 #ifdef WPS_WORKAROUNDS
3276 os_get_reltime(&wps->wps->registrar->pbc_ignore_start);
3277 #endif /* WPS_WORKAROUNDS */
3278 os_memcpy(wps->wps->registrar->pbc_ignore_uuid, wps->uuid_e,
3281 wps_registrar_pin_completed(wps->wps->registrar);
3283 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
3284 * merge them into APs own list.. */
3286 wps_success_event(wps->wps, wps->mac_addr_e);
3292 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
3293 enum wsc_op_code op_code,
3294 const struct wpabuf *msg)
3296 enum wps_process_res ret;
3298 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
3300 (unsigned long) wpabuf_len(msg), op_code);
3302 #ifdef CONFIG_WPS_UPNP
3303 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
3304 struct wps_parse_attr attr;
3305 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
3306 *attr.msg_type == WPS_M3)
3307 wps->ext_reg = 2; /* past M2/M2D phase */
3309 if (wps->ext_reg > 1)
3310 wps_registrar_free_pending_m2(wps->wps);
3311 if (wps->wps->wps_upnp && wps->ext_reg &&
3312 wps->wps->upnp_msgs == NULL &&
3313 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
3315 struct wps_parse_attr attr;
3317 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
3320 type = *attr.msg_type;
3321 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
3322 " to external Registrar for processing", type);
3323 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
3325 UPNP_WPS_WLANEVENT_TYPE_EAP,
3327 if (op_code == WSC_MSG)
3329 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
3330 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
3331 "external Registrar");
3332 return WPS_CONTINUE;
3334 #endif /* CONFIG_WPS_UPNP */
3338 return wps_process_wsc_msg(wps, msg);
3340 if (wps_validate_wsc_ack(msg) < 0)
3342 return wps_process_wsc_ack(wps, msg);
3344 if (wps_validate_wsc_nack(msg) < 0)
3346 return wps_process_wsc_nack(wps, msg);
3348 if (wps_validate_wsc_done(msg) < 0)
3350 ret = wps_process_wsc_done(wps, msg);
3351 if (ret == WPS_FAILURE) {
3352 wps->state = SEND_WSC_NACK;
3353 wps_fail_event(wps->wps, WPS_WSC_DONE,
3355 wps->error_indication, wps->mac_addr_e);
3359 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
3365 int wps_registrar_update_ie(struct wps_registrar *reg)
3367 return wps_set_ie(reg);
3371 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
3374 struct wps_registrar *reg = eloop_ctx;
3376 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
3377 "unselect internal Registrar");
3378 reg->selected_registrar = 0;
3380 wps_registrar_selected_registrar_changed(reg, 0);
3384 #ifdef CONFIG_WPS_UPNP
3385 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
3386 struct subscription *s)
3389 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
3390 "config_methods=0x%x)",
3391 s->dev_password_id, s->config_methods);
3392 reg->sel_reg_union = 1;
3393 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
3394 reg->sel_reg_dev_password_id_override = s->dev_password_id;
3395 if (reg->sel_reg_config_methods_override == -1)
3396 reg->sel_reg_config_methods_override = 0;
3397 reg->sel_reg_config_methods_override |= s->config_methods;
3398 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
3399 if (is_zero_ether_addr(reg->authorized_macs_union[i]))
3401 for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
3403 if (is_zero_ether_addr(s->authorized_macs[j]))
3405 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC into union: "
3406 MACSTR, MAC2STR(s->authorized_macs[j]));
3407 os_memcpy(reg->authorized_macs_union[i],
3408 s->authorized_macs[j], ETH_ALEN);
3411 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union",
3412 (u8 *) reg->authorized_macs_union,
3413 sizeof(reg->authorized_macs_union));
3415 #endif /* CONFIG_WPS_UPNP */
3418 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
3420 #ifdef CONFIG_WPS_UPNP
3421 struct subscription *s;
3423 if (reg->wps->wps_upnp == NULL)
3426 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
3427 struct subscription, list) {
3428 struct subscr_addr *sa;
3429 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
3431 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
3432 inet_ntoa(sa->saddr.sin_addr),
3433 ntohs(sa->saddr.sin_port));
3435 if (s->selected_registrar)
3436 wps_registrar_sel_reg_add(reg, s);
3438 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
3441 #endif /* CONFIG_WPS_UPNP */
3446 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3447 * @reg: Registrar data from wps_registrar_init()
3449 * This function is called when selected registrar state changes, e.g., when an
3450 * AP receives a SetSelectedRegistrar UPnP message.
3452 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg,
3455 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
3457 reg->sel_reg_union = reg->selected_registrar;
3458 reg->sel_reg_dev_password_id_override = -1;
3459 reg->sel_reg_config_methods_override = -1;
3460 os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
3461 WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
3462 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union (start with own)",
3463 (u8 *) reg->authorized_macs_union,
3464 sizeof(reg->authorized_macs_union));
3465 if (reg->selected_registrar) {
3468 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
3469 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
3470 WPS_CONFIG_PHY_PUSHBUTTON);
3472 reg->sel_reg_dev_password_id_override =
3474 wps_set_pushbutton(&methods, reg->wps->config_methods);
3475 } else if (dev_pw_id)
3476 reg->sel_reg_dev_password_id_override = dev_pw_id;
3477 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
3478 "(pbc=%d)", reg->pbc);
3479 reg->sel_reg_config_methods_override = methods;
3481 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
3483 wps_registrar_sel_reg_union(reg);
3486 wps_cb_set_sel_reg(reg);
3490 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
3491 char *buf, size_t buflen)
3493 struct wps_registrar_device *d;
3496 char devtype[WPS_DEV_TYPE_BUFSIZE];
3498 d = wps_device_get(reg, addr);
3501 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
3504 ret = os_snprintf(buf + len, buflen - len,
3506 "wpsPrimaryDeviceType=%s\n"
3507 "wpsDeviceName=%s\n"
3508 "wpsManufacturer=%s\n"
3510 "wpsModelNumber=%s\n"
3511 "wpsSerialNumber=%s\n",
3513 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
3515 d->dev.device_name ? d->dev.device_name : "",
3516 d->dev.manufacturer ? d->dev.manufacturer : "",
3517 d->dev.model_name ? d->dev.model_name : "",
3518 d->dev.model_number ? d->dev.model_number : "",
3519 d->dev.serial_number ? d->dev.serial_number : "");
3520 if (os_snprintf_error(buflen - len, ret))
3528 int wps_registrar_config_ap(struct wps_registrar *reg,
3529 struct wps_credential *cred)
3531 wpa_printf(MSG_DEBUG, "WPS: encr_type=0x%x", cred->encr_type);
3532 if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
3534 if (cred->encr_type & WPS_ENCR_WEP) {
3535 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
3536 "due to WEP configuration");
3540 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
3541 "invalid encr_type 0x%x", cred->encr_type);
3545 if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
3547 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
3549 cred->encr_type |= WPS_ENCR_AES;
3552 if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
3554 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
3556 cred->auth_type |= WPS_AUTH_WPA2PSK;
3559 if (reg->wps->cred_cb)
3560 return reg->wps->cred_cb(reg->wps->cb_ctx, cred);
3566 #ifdef CONFIG_WPS_NFC
3568 int wps_registrar_add_nfc_pw_token(struct wps_registrar *reg,
3569 const u8 *pubkey_hash, u16 pw_id,
3570 const u8 *dev_pw, size_t dev_pw_len,
3571 int pk_hash_provided_oob)
3573 struct wps_nfc_pw_token *token;
3575 if (dev_pw_len > WPS_OOB_DEVICE_PASSWORD_LEN)
3578 if (pw_id == DEV_PW_NFC_CONNECTION_HANDOVER &&
3579 (pubkey_hash == NULL || !pk_hash_provided_oob)) {
3580 wpa_printf(MSG_DEBUG, "WPS: Unexpected NFC Password Token "
3581 "addition - missing public key hash");
3585 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, pw_id);
3587 token = os_zalloc(sizeof(*token));
3591 token->peer_pk_hash_known = pubkey_hash != NULL;
3593 os_memcpy(token->pubkey_hash, pubkey_hash,
3594 WPS_OOB_PUBKEY_HASH_LEN);
3595 token->pw_id = pw_id;
3596 token->pk_hash_provided_oob = pk_hash_provided_oob;
3598 wpa_snprintf_hex_uppercase((char *) token->dev_pw,
3599 sizeof(token->dev_pw),
3600 dev_pw, dev_pw_len);
3601 token->dev_pw_len = dev_pw_len * 2;
3604 dl_list_add(®->nfc_pw_tokens, &token->list);
3606 reg->selected_registrar = 1;
3608 wps_registrar_add_authorized_mac(reg,
3609 (u8 *) "\xff\xff\xff\xff\xff\xff");
3610 wps_registrar_selected_registrar_changed(reg, pw_id);
3611 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
3612 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
3613 wps_registrar_set_selected_timeout,
3616 wpa_printf(MSG_DEBUG, "WPS: Added NFC Device Password %u to Registrar",
3623 int wps_registrar_add_nfc_password_token(struct wps_registrar *reg,
3624 const u8 *oob_dev_pw,
3625 size_t oob_dev_pw_len)
3627 const u8 *pos, *hash, *dev_pw;
3631 if (oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2 ||
3632 oob_dev_pw_len > WPS_OOB_PUBKEY_HASH_LEN + 2 +
3633 WPS_OOB_DEVICE_PASSWORD_LEN)
3637 pos = oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN;
3638 id = WPA_GET_BE16(pos);
3640 dev_pw_len = oob_dev_pw + oob_dev_pw_len - dev_pw;
3642 wpa_printf(MSG_DEBUG, "WPS: Add NFC Password Token for Password ID %u",
3645 wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
3646 hash, WPS_OOB_PUBKEY_HASH_LEN);
3647 wpa_hexdump_key(MSG_DEBUG, "WPS: Device Password", dev_pw, dev_pw_len);
3649 return wps_registrar_add_nfc_pw_token(reg, hash, id, dev_pw,
3654 void wps_registrar_remove_nfc_pw_token(struct wps_registrar *reg,
3655 struct wps_nfc_pw_token *token)
3657 wps_registrar_remove_authorized_mac(reg,
3658 (u8 *) "\xff\xff\xff\xff\xff\xff");
3659 wps_registrar_selected_registrar_changed(reg, 0);
3662 * Free the NFC password token if it was used only for a single protocol
3663 * run. The static handover case uses the same password token multiple
3664 * times, so do not free that case here.
3666 if (token->peer_pk_hash_known)
3670 #endif /* CONFIG_WPS_NFC */