2 * Wi-Fi Protected Setup - Registrar
3 * Copyright (c) 2008-2012, 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];
35 u8 dev_pw[WPS_OOB_DEVICE_PASSWORD_LEN];
40 static void wps_remove_nfc_pw_token(struct wps_nfc_pw_token *token)
42 dl_list_del(&token->list);
47 static void wps_free_nfc_pw_tokens(struct dl_list *tokens, u16 pw_id)
49 struct wps_nfc_pw_token *token, *prev;
50 dl_list_for_each_safe(token, prev, tokens, struct wps_nfc_pw_token,
52 if (pw_id == 0 || pw_id == token->pw_id)
53 wps_remove_nfc_pw_token(token);
58 static struct wps_nfc_pw_token * wps_get_nfc_pw_token(struct dl_list *tokens,
61 struct wps_nfc_pw_token *token;
62 dl_list_for_each(token, tokens, struct wps_nfc_pw_token, list) {
63 if (pw_id == token->pw_id)
69 #else /* CONFIG_WPS_NFC */
71 #define wps_free_nfc_pw_tokens(t, p) do { } while (0)
73 #endif /* CONFIG_WPS_NFC */
78 u8 uuid[WPS_UUID_LEN];
82 #define PIN_LOCKED BIT(0)
83 #define PIN_EXPIRES BIT(1)
85 struct os_time expiration;
86 u8 enrollee_addr[ETH_ALEN];
90 static void wps_free_pin(struct wps_uuid_pin *pin)
97 static void wps_remove_pin(struct wps_uuid_pin *pin)
99 dl_list_del(&pin->list);
104 static void wps_free_pins(struct dl_list *pins)
106 struct wps_uuid_pin *pin, *prev;
107 dl_list_for_each_safe(pin, prev, pins, struct wps_uuid_pin, list)
112 struct wps_pbc_session {
113 struct wps_pbc_session *next;
115 u8 uuid_e[WPS_UUID_LEN];
116 struct os_time timestamp;
120 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
122 struct wps_pbc_session *prev;
132 struct wps_registrar_device {
133 struct wps_registrar_device *next;
134 struct wps_device_data dev;
135 u8 uuid[WPS_UUID_LEN];
139 struct wps_registrar {
140 struct wps_context *wps;
143 int selected_registrar;
145 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
147 int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
148 struct wpabuf *probe_resp_ie);
149 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
150 const struct wps_device_data *dev);
151 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
152 const u8 *uuid_e, const u8 *dev_pw,
154 void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
155 u16 sel_reg_config_methods);
156 void (*enrollee_seen_cb)(void *ctx, const u8 *addr, const u8 *uuid_e,
157 const u8 *pri_dev_type, u16 config_methods,
158 u16 dev_password_id, u8 request_type,
159 const char *dev_name);
163 struct dl_list nfc_pw_tokens;
164 struct wps_pbc_session *pbc_sessions;
167 struct wpabuf *extra_cred;
168 int disable_auto_conf;
170 int sel_reg_dev_password_id_override;
171 int sel_reg_config_methods_override;
175 struct wps_registrar_device *devices;
177 int force_pbc_overlap;
179 u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
180 u8 authorized_macs_union[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
182 u8 p2p_dev_addr[ETH_ALEN];
184 u8 pbc_ignore_uuid[WPS_UUID_LEN];
185 struct os_time pbc_ignore_start;
189 static int wps_set_ie(struct wps_registrar *reg);
190 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
191 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
193 static void wps_registrar_remove_pin(struct wps_registrar *reg,
194 struct wps_uuid_pin *pin);
197 static void wps_registrar_add_authorized_mac(struct wps_registrar *reg,
201 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC " MACSTR,
203 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
204 if (os_memcmp(reg->authorized_macs[i], addr, ETH_ALEN) == 0) {
205 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was "
206 "already in the list");
207 return; /* already in list */
209 for (i = WPS_MAX_AUTHORIZED_MACS - 1; i > 0; i--)
210 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i - 1],
212 os_memcpy(reg->authorized_macs[0], addr, ETH_ALEN);
213 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
214 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
218 static void wps_registrar_remove_authorized_mac(struct wps_registrar *reg,
222 wpa_printf(MSG_DEBUG, "WPS: Remove authorized MAC " MACSTR,
224 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++) {
225 if (os_memcmp(reg->authorized_macs, addr, ETH_ALEN) == 0)
228 if (i == WPS_MAX_AUTHORIZED_MACS) {
229 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was not in the "
231 return; /* not in the list */
233 for (; i + 1 < WPS_MAX_AUTHORIZED_MACS; i++)
234 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i + 1],
236 os_memset(reg->authorized_macs[WPS_MAX_AUTHORIZED_MACS - 1], 0,
238 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
239 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
243 static void wps_free_devices(struct wps_registrar_device *dev)
245 struct wps_registrar_device *prev;
250 wps_device_data_free(&prev->dev);
256 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
259 struct wps_registrar_device *dev;
261 for (dev = reg->devices; dev; dev = dev->next) {
262 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
269 static void wps_device_clone_data(struct wps_device_data *dst,
270 struct wps_device_data *src)
272 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
273 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
275 #define WPS_STRDUP(n) \
277 dst->n = src->n ? os_strdup(src->n) : NULL
279 WPS_STRDUP(device_name);
280 WPS_STRDUP(manufacturer);
281 WPS_STRDUP(model_name);
282 WPS_STRDUP(model_number);
283 WPS_STRDUP(serial_number);
288 int wps_device_store(struct wps_registrar *reg,
289 struct wps_device_data *dev, const u8 *uuid)
291 struct wps_registrar_device *d;
293 d = wps_device_get(reg, dev->mac_addr);
295 d = os_zalloc(sizeof(*d));
298 d->next = reg->devices;
302 wps_device_clone_data(&d->dev, dev);
303 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
309 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
310 const u8 *addr, const u8 *uuid_e)
312 struct wps_pbc_session *pbc, *prev = NULL;
317 pbc = reg->pbc_sessions;
319 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
320 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
322 prev->next = pbc->next;
324 reg->pbc_sessions = pbc->next;
332 pbc = os_zalloc(sizeof(*pbc));
335 os_memcpy(pbc->addr, addr, ETH_ALEN);
337 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
340 pbc->next = reg->pbc_sessions;
341 reg->pbc_sessions = pbc;
342 pbc->timestamp = now;
344 /* remove entries that have timed out */
349 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
351 wps_free_pbc_sessions(pbc);
360 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
362 const u8 *p2p_dev_addr)
364 struct wps_pbc_session *pbc, *prev = NULL, *tmp;
366 pbc = reg->pbc_sessions;
368 if (os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0 ||
369 (p2p_dev_addr && !is_zero_ether_addr(reg->p2p_dev_addr) &&
370 os_memcmp(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
373 prev->next = pbc->next;
375 reg->pbc_sessions = pbc->next;
378 wpa_printf(MSG_DEBUG, "WPS: Removing PBC session for "
379 "addr=" MACSTR, MAC2STR(tmp->addr));
380 wpa_hexdump(MSG_DEBUG, "WPS: Removed UUID-E",
381 tmp->uuid_e, WPS_UUID_LEN);
391 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
392 const u8 *addr, const u8 *uuid_e)
395 struct wps_pbc_session *pbc;
396 struct wps_pbc_session *first = NULL;
401 wpa_printf(MSG_DEBUG, "WPS: Checking active PBC sessions for overlap");
404 wpa_printf(MSG_DEBUG, "WPS: Add one for the requested UUID");
405 wpa_hexdump(MSG_DEBUG, "WPS: Requested UUID",
406 uuid_e, WPS_UUID_LEN);
410 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
411 wpa_printf(MSG_DEBUG, "WPS: Consider PBC session with " MACSTR,
413 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E",
414 pbc->uuid_e, WPS_UUID_LEN);
415 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
416 wpa_printf(MSG_DEBUG, "WPS: PBC walk time has "
421 os_memcmp(pbc->uuid_e, first->uuid_e, WPS_UUID_LEN) == 0) {
422 wpa_printf(MSG_DEBUG, "WPS: Same Enrollee");
423 continue; /* same Enrollee */
425 if (uuid_e == NULL ||
426 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN)) {
427 wpa_printf(MSG_DEBUG, "WPS: New Enrollee");
434 wpa_printf(MSG_DEBUG, "WPS: %u active PBC session(s) found", count);
436 return count > 1 ? 1 : 0;
440 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
442 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
444 wpabuf_put_be16(msg, ATTR_WPS_STATE);
445 wpabuf_put_be16(msg, 1);
446 wpabuf_put_u8(msg, wps->wps_state);
451 #ifdef CONFIG_WPS_UPNP
452 static void wps_registrar_free_pending_m2(struct wps_context *wps)
454 struct upnp_pending_message *p, *p2, *prev = NULL;
457 if (p->type == WPS_M2 || p->type == WPS_M2D) {
459 wps->upnp_msgs = p->next;
461 prev->next = p->next;
462 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
465 wpabuf_free(p2->msg);
473 #endif /* CONFIG_WPS_UPNP */
476 static int wps_build_ap_setup_locked(struct wps_context *wps,
479 if (wps->ap_setup_locked && wps->ap_setup_locked != 2) {
480 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
481 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
482 wpabuf_put_be16(msg, 1);
483 wpabuf_put_u8(msg, 1);
489 static int wps_build_selected_registrar(struct wps_registrar *reg,
492 if (!reg->sel_reg_union)
494 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
495 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
496 wpabuf_put_be16(msg, 1);
497 wpabuf_put_u8(msg, 1);
502 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
505 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
506 if (!reg->sel_reg_union)
508 if (reg->sel_reg_dev_password_id_override >= 0)
509 id = reg->sel_reg_dev_password_id_override;
510 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
511 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
512 wpabuf_put_be16(msg, 2);
513 wpabuf_put_be16(msg, id);
518 static int wps_build_sel_pbc_reg_uuid_e(struct wps_registrar *reg,
521 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
522 if (!reg->sel_reg_union)
524 if (reg->sel_reg_dev_password_id_override >= 0)
525 id = reg->sel_reg_dev_password_id_override;
526 if (id != DEV_PW_PUSHBUTTON || !reg->dualband)
528 return wps_build_uuid_e(msg, reg->wps->uuid);
532 static void wps_set_pushbutton(u16 *methods, u16 conf_methods)
534 *methods |= WPS_CONFIG_PUSHBUTTON;
536 if ((conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON) ==
537 WPS_CONFIG_VIRT_PUSHBUTTON)
538 *methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
539 if ((conf_methods & WPS_CONFIG_PHY_PUSHBUTTON) ==
540 WPS_CONFIG_PHY_PUSHBUTTON)
541 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
542 if ((*methods & WPS_CONFIG_VIRT_PUSHBUTTON) !=
543 WPS_CONFIG_VIRT_PUSHBUTTON &&
544 (*methods & WPS_CONFIG_PHY_PUSHBUTTON) !=
545 WPS_CONFIG_PHY_PUSHBUTTON) {
547 * Required to include virtual/physical flag, but we were not
548 * configured with push button type, so have to default to one
551 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
553 #endif /* CONFIG_WPS2 */
557 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
561 if (!reg->sel_reg_union)
563 methods = reg->wps->config_methods;
564 methods &= ~WPS_CONFIG_PUSHBUTTON;
566 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
567 WPS_CONFIG_PHY_PUSHBUTTON);
568 #endif /* CONFIG_WPS2 */
570 wps_set_pushbutton(&methods, reg->wps->config_methods);
571 if (reg->sel_reg_config_methods_override >= 0)
572 methods = reg->sel_reg_config_methods_override;
573 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
575 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
576 wpabuf_put_be16(msg, 2);
577 wpabuf_put_be16(msg, methods);
582 static int wps_build_probe_config_methods(struct wps_registrar *reg,
587 * These are the methods that the AP supports as an Enrollee for adding
588 * external Registrars.
590 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
592 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
593 WPS_CONFIG_PHY_PUSHBUTTON);
594 #endif /* CONFIG_WPS2 */
595 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
596 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
597 wpabuf_put_be16(msg, 2);
598 wpabuf_put_be16(msg, methods);
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)
615 while (*count < WPS_MAX_AUTHORIZED_MACS) {
616 if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
620 #endif /* CONFIG_WPS2 */
622 return (const u8 *) reg->authorized_macs_union;
627 * wps_registrar_init - Initialize WPS Registrar data
628 * @wps: Pointer to longterm WPS context
629 * @cfg: Registrar configuration
630 * Returns: Pointer to allocated Registrar data or %NULL on failure
632 * This function is used to initialize WPS Registrar functionality. It can be
633 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
634 * runs (e.g., when run as an internal Registrar in an AP). Caller is
635 * responsible for freeing the returned data with wps_registrar_deinit() when
636 * Registrar functionality is not needed anymore.
638 struct wps_registrar *
639 wps_registrar_init(struct wps_context *wps,
640 const struct wps_registrar_config *cfg)
642 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
646 dl_list_init(®->pins);
647 dl_list_init(®->nfc_pw_tokens);
649 reg->new_psk_cb = cfg->new_psk_cb;
650 reg->set_ie_cb = cfg->set_ie_cb;
651 reg->pin_needed_cb = cfg->pin_needed_cb;
652 reg->reg_success_cb = cfg->reg_success_cb;
653 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
654 reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
655 reg->cb_ctx = cfg->cb_ctx;
656 reg->skip_cred_build = cfg->skip_cred_build;
657 if (cfg->extra_cred) {
658 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
659 cfg->extra_cred_len);
660 if (reg->extra_cred == NULL) {
665 reg->disable_auto_conf = cfg->disable_auto_conf;
666 reg->sel_reg_dev_password_id_override = -1;
667 reg->sel_reg_config_methods_override = -1;
668 reg->static_wep_only = cfg->static_wep_only;
669 reg->dualband = cfg->dualband;
671 if (wps_set_ie(reg)) {
672 wps_registrar_deinit(reg);
681 * wps_registrar_deinit - Deinitialize WPS Registrar data
682 * @reg: Registrar data from wps_registrar_init()
684 void wps_registrar_deinit(struct wps_registrar *reg)
688 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
689 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
690 wps_free_pins(®->pins);
691 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, 0);
692 wps_free_pbc_sessions(reg->pbc_sessions);
693 wpabuf_free(reg->extra_cred);
694 wps_free_devices(reg->devices);
699 static void wps_registrar_invalidate_unused(struct wps_registrar *reg)
701 struct wps_uuid_pin *pin;
703 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
704 if (pin->wildcard_uuid == 1 && !(pin->flags & PIN_LOCKED)) {
705 wpa_printf(MSG_DEBUG, "WPS: Invalidate previously "
706 "configured wildcard PIN");
707 wps_registrar_remove_pin(reg, pin);
715 * wps_registrar_add_pin - Configure a new PIN for Registrar
716 * @reg: Registrar data from wps_registrar_init()
717 * @addr: Enrollee MAC address or %NULL if not known
718 * @uuid: UUID-E or %NULL for wildcard (any UUID)
719 * @pin: PIN (Device Password)
720 * @pin_len: Length of pin in octets
721 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
722 * Returns: 0 on success, -1 on failure
724 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
725 const u8 *uuid, const u8 *pin, size_t pin_len,
728 struct wps_uuid_pin *p;
730 p = os_zalloc(sizeof(*p));
734 os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
736 p->wildcard_uuid = 1;
738 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
739 p->pin = os_malloc(pin_len);
740 if (p->pin == NULL) {
744 os_memcpy(p->pin, pin, pin_len);
745 p->pin_len = pin_len;
748 p->flags |= PIN_EXPIRES;
749 os_get_time(&p->expiration);
750 p->expiration.sec += timeout;
753 if (p->wildcard_uuid)
754 wps_registrar_invalidate_unused(reg);
756 dl_list_add(®->pins, &p->list);
758 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
760 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
761 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
762 reg->selected_registrar = 1;
765 wps_registrar_add_authorized_mac(reg, addr);
767 wps_registrar_add_authorized_mac(
768 reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
769 wps_registrar_selected_registrar_changed(reg);
770 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
771 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
772 wps_registrar_set_selected_timeout,
779 static void wps_registrar_remove_pin(struct wps_registrar *reg,
780 struct wps_uuid_pin *pin)
783 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
785 if (is_zero_ether_addr(pin->enrollee_addr))
788 addr = pin->enrollee_addr;
789 wps_registrar_remove_authorized_mac(reg, addr);
791 wps_registrar_selected_registrar_changed(reg);
795 static void wps_registrar_expire_pins(struct wps_registrar *reg)
797 struct wps_uuid_pin *pin, *prev;
801 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
803 if ((pin->flags & PIN_EXPIRES) &&
804 os_time_before(&pin->expiration, &now)) {
805 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
806 pin->uuid, WPS_UUID_LEN);
807 wps_registrar_remove_pin(reg, pin);
814 * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
815 * @reg: Registrar data from wps_registrar_init()
816 * @dev_pw: PIN to search for or %NULL to match any
817 * @dev_pw_len: Length of dev_pw in octets
818 * Returns: 0 on success, -1 if not wildcard PIN is enabled
820 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg,
824 struct wps_uuid_pin *pin, *prev;
826 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
828 if (dev_pw && pin->pin &&
829 (dev_pw_len != pin->pin_len ||
830 os_memcmp(dev_pw, pin->pin, dev_pw_len) != 0))
831 continue; /* different PIN */
832 if (pin->wildcard_uuid) {
833 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
834 pin->uuid, WPS_UUID_LEN);
835 wps_registrar_remove_pin(reg, pin);
845 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
846 * @reg: Registrar data from wps_registrar_init()
848 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
850 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
852 struct wps_uuid_pin *pin, *prev;
854 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
856 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
857 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
858 pin->uuid, WPS_UUID_LEN);
859 wps_registrar_remove_pin(reg, pin);
868 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
869 const u8 *uuid, size_t *pin_len)
871 struct wps_uuid_pin *pin, *found = NULL;
873 wps_registrar_expire_pins(reg);
875 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
876 if (!pin->wildcard_uuid &&
877 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
884 /* Check for wildcard UUIDs since none of the UUID-specific
886 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
887 if (pin->wildcard_uuid == 1 ||
888 pin->wildcard_uuid == 2) {
889 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
890 "PIN. Assigned it for this UUID-E");
891 pin->wildcard_uuid++;
892 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
903 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
904 * that could otherwise avoid PIN invalidations.
906 if (found->flags & PIN_LOCKED) {
907 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
908 "allow concurrent re-use");
911 *pin_len = found->pin_len;
912 found->flags |= PIN_LOCKED;
918 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
919 * @reg: Registrar data from wps_registrar_init()
921 * Returns: 0 on success, -1 on failure
923 * PINs are locked to enforce only one concurrent use. This function unlocks a
924 * PIN to allow it to be used again. If the specified PIN was configured using
925 * a wildcard UUID, it will be removed instead of allowing multiple uses.
927 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
929 struct wps_uuid_pin *pin;
931 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
932 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
933 if (pin->wildcard_uuid == 3) {
934 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
936 return wps_registrar_invalidate_pin(reg, uuid);
938 pin->flags &= ~PIN_LOCKED;
947 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
949 reg->selected_registrar = 0;
951 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
952 wps_registrar_remove_authorized_mac(reg,
953 (u8 *) "\xff\xff\xff\xff\xff\xff");
954 wps_registrar_selected_registrar_changed(reg);
958 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
960 struct wps_registrar *reg = eloop_ctx;
962 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
963 wps_pbc_timeout_event(reg->wps);
964 wps_registrar_stop_pbc(reg);
969 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
970 * @reg: Registrar data from wps_registrar_init()
971 * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
972 * indicates no such filtering
973 * Returns: 0 on success, -1 on failure, -2 on session overlap
975 * This function is called on an AP when a push button is pushed to activate
976 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
977 * or when a PBC registration is completed. If more than one Enrollee in active
978 * PBC mode has been detected during the monitor time (previous 2 minutes), the
979 * PBC mode is not activated and -2 is returned to indicate session overlap.
980 * This is skipped if a specific Enrollee is selected.
982 int wps_registrar_button_pushed(struct wps_registrar *reg,
983 const u8 *p2p_dev_addr)
985 if (p2p_dev_addr == NULL &&
986 wps_registrar_pbc_overlap(reg, NULL, NULL)) {
987 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
989 wps_pbc_overlap_event(reg->wps);
992 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
993 reg->force_pbc_overlap = 0;
994 reg->selected_registrar = 1;
997 os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
999 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
1000 wps_registrar_add_authorized_mac(reg,
1001 (u8 *) "\xff\xff\xff\xff\xff\xff");
1002 wps_registrar_selected_registrar_changed(reg);
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);
1020 static void wps_registrar_pin_completed(struct wps_registrar *reg)
1022 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
1023 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1024 reg->selected_registrar = 0;
1025 wps_registrar_selected_registrar_changed(reg);
1029 void wps_registrar_complete(struct wps_registrar *registrar, const u8 *uuid_e,
1030 const u8 *dev_pw, size_t dev_pw_len)
1032 if (registrar->pbc) {
1033 wps_registrar_remove_pbc_session(registrar,
1035 wps_registrar_pbc_completed(registrar);
1036 os_get_time(®istrar->pbc_ignore_start);
1037 os_memcpy(registrar->pbc_ignore_uuid, uuid_e, WPS_UUID_LEN);
1039 wps_registrar_pin_completed(registrar);
1043 wps_registrar_invalidate_wildcard_pin(registrar, dev_pw,
1045 wpa_hexdump_key(MSG_DEBUG, "WPS: Invalidated wildcard PIN",
1046 dev_pw, dev_pw_len);
1051 int wps_registrar_wps_cancel(struct wps_registrar *reg)
1054 wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
1055 wps_registrar_pbc_timeout(reg, NULL);
1056 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1058 } else if (reg->selected_registrar) {
1060 wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
1061 wps_registrar_pin_completed(reg);
1062 wps_registrar_invalidate_wildcard_pin(reg, NULL, 0);
1070 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
1071 * @reg: Registrar data from wps_registrar_init()
1072 * @addr: MAC address of the Probe Request sender
1073 * @wps_data: WPS IE contents
1075 * This function is called on an AP when a Probe Request with WPS IE is
1076 * received. This is used to track PBC mode use and to detect possible overlap
1077 * situation with other WPS APs.
1079 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
1080 const struct wpabuf *wps_data,
1083 struct wps_parse_attr attr;
1086 wpa_hexdump_buf(MSG_MSGDUMP,
1087 "WPS: Probe Request with WPS data received",
1090 if (wps_parse_msg(wps_data, &attr) < 0)
1093 if (attr.config_methods == NULL) {
1094 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
1099 if (attr.dev_password_id == NULL) {
1100 wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
1101 "in Probe Request");
1105 if (reg->enrollee_seen_cb && attr.uuid_e &&
1106 attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
1107 char *dev_name = NULL;
1108 if (attr.dev_name) {
1109 dev_name = os_zalloc(attr.dev_name_len + 1);
1111 os_memcpy(dev_name, attr.dev_name,
1115 reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
1116 attr.primary_dev_type,
1117 WPA_GET_BE16(attr.config_methods),
1118 WPA_GET_BE16(attr.dev_password_id),
1119 *attr.request_type, dev_name);
1123 if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
1124 return; /* Not PBC */
1126 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
1127 MACSTR, MAC2STR(addr));
1128 if (attr.uuid_e == NULL) {
1129 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
1133 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E from Probe Request", attr.uuid_e,
1136 #ifdef WPS_WORKAROUNDS
1137 if (reg->pbc_ignore_start.sec &&
1138 os_memcmp(attr.uuid_e, reg->pbc_ignore_uuid, WPS_UUID_LEN) == 0) {
1139 struct os_time now, dur;
1141 os_time_sub(&now, ®->pbc_ignore_start, &dur);
1142 if (dur.sec >= 0 && dur.sec < 5) {
1143 wpa_printf(MSG_DEBUG, "WPS: Ignore PBC activation "
1144 "based on Probe Request from the Enrollee "
1145 "that just completed PBC provisioning");
1148 reg->pbc_ignore_start.sec = 0;
1150 #endif /* WPS_WORKAROUNDS */
1153 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
1154 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
1155 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
1156 reg->force_pbc_overlap = 1;
1157 wps_pbc_overlap_event(reg->wps);
1162 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
1163 const u8 *psk, size_t psk_len)
1165 if (reg->new_psk_cb == NULL)
1168 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
1172 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
1173 const struct wps_device_data *dev)
1175 if (reg->pin_needed_cb == NULL)
1178 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
1182 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
1183 const u8 *uuid_e, const u8 *dev_pw,
1186 if (reg->reg_success_cb == NULL)
1189 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e, dev_pw, dev_pw_len);
1193 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
1194 struct wpabuf *probe_resp_ie)
1196 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
1200 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
1203 if (reg->set_sel_reg_cb == NULL)
1206 if (reg->selected_registrar) {
1207 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
1209 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
1210 WPS_CONFIG_PHY_PUSHBUTTON);
1211 #endif /* CONFIG_WPS2 */
1213 wps_set_pushbutton(&methods, reg->wps->config_methods);
1216 wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1217 "config_methods=0x%x pbc=%d methods=0x%x",
1218 reg->selected_registrar, reg->wps->config_methods,
1221 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
1222 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
1227 static int wps_set_ie(struct wps_registrar *reg)
1229 struct wpabuf *beacon;
1230 struct wpabuf *probe;
1231 const u8 *auth_macs;
1233 size_t vendor_len = 0;
1236 if (reg->set_ie_cb == NULL)
1239 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
1240 if (reg->wps->dev.vendor_ext[i]) {
1241 vendor_len += 2 + 2;
1242 vendor_len += wpabuf_len(reg->wps->dev.vendor_ext[i]);
1246 beacon = wpabuf_alloc(400 + vendor_len);
1249 probe = wpabuf_alloc(500 + vendor_len);
1250 if (probe == NULL) {
1251 wpabuf_free(beacon);
1255 auth_macs = wps_authorized_macs(reg, &count);
1257 wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
1259 if (wps_build_version(beacon) ||
1260 wps_build_wps_state(reg->wps, beacon) ||
1261 wps_build_ap_setup_locked(reg->wps, beacon) ||
1262 wps_build_selected_registrar(reg, beacon) ||
1263 wps_build_sel_reg_dev_password_id(reg, beacon) ||
1264 wps_build_sel_reg_config_methods(reg, beacon) ||
1265 wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
1266 (reg->dualband && wps_build_rf_bands(®->wps->dev, beacon)) ||
1267 wps_build_wfa_ext(beacon, 0, auth_macs, count) ||
1268 wps_build_vendor_ext(®->wps->dev, beacon)) {
1269 wpabuf_free(beacon);
1275 if (wps_build_dev_name(®->wps->dev, beacon) ||
1276 wps_build_primary_dev_type(®->wps->dev, beacon)) {
1277 wpabuf_free(beacon);
1281 #endif /* CONFIG_P2P */
1283 wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
1285 if (wps_build_version(probe) ||
1286 wps_build_wps_state(reg->wps, probe) ||
1287 wps_build_ap_setup_locked(reg->wps, probe) ||
1288 wps_build_selected_registrar(reg, probe) ||
1289 wps_build_sel_reg_dev_password_id(reg, probe) ||
1290 wps_build_sel_reg_config_methods(reg, probe) ||
1291 wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
1292 WPS_RESP_REGISTRAR) ||
1293 wps_build_uuid_e(probe, reg->wps->uuid) ||
1294 wps_build_device_attrs(®->wps->dev, probe) ||
1295 wps_build_probe_config_methods(reg, probe) ||
1296 (reg->dualband && wps_build_rf_bands(®->wps->dev, probe)) ||
1297 wps_build_wfa_ext(probe, 0, auth_macs, count) ||
1298 wps_build_vendor_ext(®->wps->dev, probe)) {
1299 wpabuf_free(beacon);
1304 beacon = wps_ie_encapsulate(beacon);
1305 probe = wps_ie_encapsulate(probe);
1307 if (!beacon || !probe) {
1308 wpabuf_free(beacon);
1313 if (reg->static_wep_only) {
1315 * Windows XP and Vista clients can get confused about
1316 * EAP-Identity/Request when they probe the network with
1317 * EAPOL-Start. In such a case, they may assume the network is
1318 * using IEEE 802.1X and prompt user for a certificate while
1319 * the correct (non-WPS) behavior would be to ask for the
1320 * static WEP key. As a workaround, use Microsoft Provisioning
1321 * IE to advertise that legacy 802.1X is not supported.
1323 const u8 ms_wps[7] = {
1324 WLAN_EID_VENDOR_SPECIFIC, 5,
1325 /* Microsoft Provisioning IE (00:50:f2:5) */
1326 0x00, 0x50, 0xf2, 5,
1327 0x00 /* no legacy 802.1X or MS WPS */
1329 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
1330 "into Beacon/Probe Response frames");
1331 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
1332 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
1335 return wps_cb_set_ie(reg, beacon, probe);
1339 static int wps_get_dev_password(struct wps_data *wps)
1344 os_free(wps->dev_password);
1345 wps->dev_password = NULL;
1348 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
1349 pin = (const u8 *) "00000000";
1351 #ifdef CONFIG_WPS_NFC
1352 } else if (wps->nfc_pw_token) {
1353 wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from NFC "
1355 pin = wps->nfc_pw_token->dev_pw;
1356 pin_len = wps->nfc_pw_token->dev_pw_len;
1357 #endif /* CONFIG_WPS_NFC */
1359 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1363 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1365 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1370 wps->dev_password = os_malloc(pin_len);
1371 if (wps->dev_password == NULL)
1373 os_memcpy(wps->dev_password, pin, pin_len);
1374 wps->dev_password_len = pin_len;
1380 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1382 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1383 wpabuf_put_be16(msg, ATTR_UUID_R);
1384 wpabuf_put_be16(msg, WPS_UUID_LEN);
1385 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1390 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1396 if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1398 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1399 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1400 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1402 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1403 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1404 "R-Hash derivation");
1408 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1409 wpabuf_put_be16(msg, ATTR_R_HASH1);
1410 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1411 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1412 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1413 addr[0] = wps->snonce;
1414 len[0] = WPS_SECRET_NONCE_LEN;
1415 addr[1] = wps->psk1;
1416 len[1] = WPS_PSK_LEN;
1417 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1418 len[2] = wpabuf_len(wps->dh_pubkey_e);
1419 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1420 len[3] = wpabuf_len(wps->dh_pubkey_r);
1421 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1422 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1424 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1425 wpabuf_put_be16(msg, ATTR_R_HASH2);
1426 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1427 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1428 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1429 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1430 addr[1] = wps->psk2;
1431 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1432 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1438 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1440 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1441 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1442 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1443 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1448 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1450 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1451 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1452 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1453 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1454 WPS_SECRET_NONCE_LEN);
1459 static int wps_build_cred_network_idx(struct wpabuf *msg,
1460 const struct wps_credential *cred)
1462 wpa_printf(MSG_DEBUG, "WPS: * Network Index (1)");
1463 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1464 wpabuf_put_be16(msg, 1);
1465 wpabuf_put_u8(msg, 1);
1470 static int wps_build_cred_ssid(struct wpabuf *msg,
1471 const struct wps_credential *cred)
1473 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1474 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID for Credential",
1475 cred->ssid, cred->ssid_len);
1476 wpabuf_put_be16(msg, ATTR_SSID);
1477 wpabuf_put_be16(msg, cred->ssid_len);
1478 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1483 static int wps_build_cred_auth_type(struct wpabuf *msg,
1484 const struct wps_credential *cred)
1486 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1488 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1489 wpabuf_put_be16(msg, 2);
1490 wpabuf_put_be16(msg, cred->auth_type);
1495 static int wps_build_cred_encr_type(struct wpabuf *msg,
1496 const struct wps_credential *cred)
1498 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1500 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1501 wpabuf_put_be16(msg, 2);
1502 wpabuf_put_be16(msg, cred->encr_type);
1507 static int wps_build_cred_network_key(struct wpabuf *msg,
1508 const struct wps_credential *cred)
1510 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1511 (int) cred->key_len);
1512 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
1513 cred->key, cred->key_len);
1514 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1515 wpabuf_put_be16(msg, cred->key_len);
1516 wpabuf_put_data(msg, cred->key, cred->key_len);
1521 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1522 const struct wps_credential *cred)
1524 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1525 MAC2STR(cred->mac_addr));
1526 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1527 wpabuf_put_be16(msg, ETH_ALEN);
1528 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1533 static int wps_build_credential(struct wpabuf *msg,
1534 const struct wps_credential *cred)
1536 if (wps_build_cred_network_idx(msg, cred) ||
1537 wps_build_cred_ssid(msg, cred) ||
1538 wps_build_cred_auth_type(msg, cred) ||
1539 wps_build_cred_encr_type(msg, cred) ||
1540 wps_build_cred_network_key(msg, cred) ||
1541 wps_build_cred_mac_addr(msg, cred))
1547 int wps_build_credential_wrap(struct wpabuf *msg,
1548 const struct wps_credential *cred)
1550 struct wpabuf *wbuf;
1551 wbuf = wpabuf_alloc(200);
1554 if (wps_build_credential(wbuf, cred)) {
1558 wpabuf_put_be16(msg, ATTR_CRED);
1559 wpabuf_put_be16(msg, wpabuf_len(wbuf));
1560 wpabuf_put_buf(msg, wbuf);
1566 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1568 struct wpabuf *cred;
1570 if (wps->wps->registrar->skip_cred_build)
1571 goto skip_cred_build;
1573 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1574 if (wps->use_cred) {
1575 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1578 os_memset(&wps->cred, 0, sizeof(wps->cred));
1580 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1581 wps->cred.ssid_len = wps->wps->ssid_len;
1583 /* Select the best authentication and encryption type */
1584 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1585 wps->auth_type = WPS_AUTH_WPA2PSK;
1586 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1587 wps->auth_type = WPS_AUTH_WPAPSK;
1588 else if (wps->auth_type & WPS_AUTH_OPEN)
1589 wps->auth_type = WPS_AUTH_OPEN;
1590 else if (wps->auth_type & WPS_AUTH_SHARED)
1591 wps->auth_type = WPS_AUTH_SHARED;
1593 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1597 wps->cred.auth_type = wps->auth_type;
1599 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1600 wps->auth_type == WPS_AUTH_WPAPSK) {
1601 if (wps->encr_type & WPS_ENCR_AES)
1602 wps->encr_type = WPS_ENCR_AES;
1603 else if (wps->encr_type & WPS_ENCR_TKIP)
1604 wps->encr_type = WPS_ENCR_TKIP;
1606 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1607 "type for WPA/WPA2");
1611 if (wps->encr_type & WPS_ENCR_WEP)
1612 wps->encr_type = WPS_ENCR_WEP;
1613 else if (wps->encr_type & WPS_ENCR_NONE)
1614 wps->encr_type = WPS_ENCR_NONE;
1616 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1617 "type for non-WPA/WPA2 mode");
1621 wps->cred.encr_type = wps->encr_type;
1623 * Set MAC address in the Credential to be the Enrollee's MAC address
1625 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1627 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1628 !wps->wps->registrar->disable_auto_conf) {
1630 /* Generate a random passphrase */
1631 if (random_get_bytes(r, sizeof(r)) < 0)
1633 os_free(wps->new_psk);
1634 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1635 if (wps->new_psk == NULL)
1637 wps->new_psk_len--; /* remove newline */
1638 while (wps->new_psk_len &&
1639 wps->new_psk[wps->new_psk_len - 1] == '=')
1641 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1642 wps->new_psk, wps->new_psk_len);
1643 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1644 wps->cred.key_len = wps->new_psk_len;
1645 } else if (wps->use_psk_key && wps->wps->psk_set) {
1647 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1648 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1649 os_memcpy(wps->cred.key, hex, 32 * 2);
1650 wps->cred.key_len = 32 * 2;
1651 } else if (wps->wps->network_key) {
1652 os_memcpy(wps->cred.key, wps->wps->network_key,
1653 wps->wps->network_key_len);
1654 wps->cred.key_len = wps->wps->network_key_len;
1655 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1657 /* Generate a random per-device PSK */
1658 os_free(wps->new_psk);
1659 wps->new_psk_len = 32;
1660 wps->new_psk = os_malloc(wps->new_psk_len);
1661 if (wps->new_psk == NULL)
1663 if (random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
1664 os_free(wps->new_psk);
1665 wps->new_psk = NULL;
1668 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1669 wps->new_psk, wps->new_psk_len);
1670 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1672 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1673 wps->cred.key_len = wps->new_psk_len * 2;
1677 #ifdef CONFIG_WPS_TESTING
1678 if (wps_testing_dummy_cred)
1679 cred = wpabuf_alloc(200);
1683 struct wps_credential dummy;
1684 wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
1685 os_memset(&dummy, 0, sizeof(dummy));
1686 os_memcpy(dummy.ssid, "dummy", 5);
1688 dummy.auth_type = WPS_AUTH_WPA2PSK;
1689 dummy.encr_type = WPS_ENCR_AES;
1690 os_memcpy(dummy.key, "dummy psk", 9);
1692 os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
1693 wps_build_credential(cred, &dummy);
1694 wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
1696 wpabuf_put_be16(msg, ATTR_CRED);
1697 wpabuf_put_be16(msg, wpabuf_len(cred));
1698 wpabuf_put_buf(msg, cred);
1702 #endif /* CONFIG_WPS_TESTING */
1704 cred = wpabuf_alloc(200);
1708 if (wps_build_credential(cred, &wps->cred)) {
1713 wpabuf_put_be16(msg, ATTR_CRED);
1714 wpabuf_put_be16(msg, wpabuf_len(cred));
1715 wpabuf_put_buf(msg, cred);
1719 if (wps->wps->registrar->extra_cred) {
1720 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1721 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1728 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1730 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1732 if (wps_build_credential(msg, &wps->cred))
1739 static struct wpabuf * wps_build_ap_cred(struct wps_data *wps)
1741 struct wpabuf *msg, *plain;
1743 msg = wpabuf_alloc(1000);
1747 plain = wpabuf_alloc(200);
1748 if (plain == NULL) {
1753 if (wps_build_ap_settings(wps, plain)) {
1759 wpabuf_put_be16(msg, ATTR_CRED);
1760 wpabuf_put_be16(msg, wpabuf_len(plain));
1761 wpabuf_put_buf(msg, plain);
1768 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1772 if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
1774 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1775 wps->nonce_r, WPS_NONCE_LEN);
1776 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1778 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1779 msg = wpabuf_alloc(1000);
1783 if (wps_build_version(msg) ||
1784 wps_build_msg_type(msg, WPS_M2) ||
1785 wps_build_enrollee_nonce(wps, msg) ||
1786 wps_build_registrar_nonce(wps, msg) ||
1787 wps_build_uuid_r(wps, msg) ||
1788 wps_build_public_key(wps, msg) ||
1789 wps_derive_keys(wps) ||
1790 wps_build_auth_type_flags(wps, msg) ||
1791 wps_build_encr_type_flags(wps, msg) ||
1792 wps_build_conn_type_flags(wps, msg) ||
1793 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1794 wps_build_device_attrs(&wps->wps->dev, msg) ||
1795 wps_build_rf_bands(&wps->wps->dev, msg) ||
1796 wps_build_assoc_state(wps, msg) ||
1797 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1798 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1799 wps_build_os_version(&wps->wps->dev, msg) ||
1800 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1801 wps_build_authenticator(wps, msg)) {
1807 wps->state = RECV_M3;
1812 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1815 u16 err = wps->config_error;
1817 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1818 msg = wpabuf_alloc(1000);
1822 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1823 err == WPS_CFG_NO_ERROR)
1824 err = WPS_CFG_SETUP_LOCKED;
1826 if (wps_build_version(msg) ||
1827 wps_build_msg_type(msg, WPS_M2D) ||
1828 wps_build_enrollee_nonce(wps, msg) ||
1829 wps_build_registrar_nonce(wps, msg) ||
1830 wps_build_uuid_r(wps, msg) ||
1831 wps_build_auth_type_flags(wps, msg) ||
1832 wps_build_encr_type_flags(wps, msg) ||
1833 wps_build_conn_type_flags(wps, msg) ||
1834 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1835 wps_build_device_attrs(&wps->wps->dev, msg) ||
1836 wps_build_rf_bands(&wps->wps->dev, msg) ||
1837 wps_build_assoc_state(wps, msg) ||
1838 wps_build_config_error(msg, err) ||
1839 wps_build_os_version(&wps->wps->dev, msg) ||
1840 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1845 wps->state = RECV_M2D_ACK;
1850 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1852 struct wpabuf *msg, *plain;
1854 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1856 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1858 plain = wpabuf_alloc(200);
1862 msg = wpabuf_alloc(1000);
1868 if (wps_build_version(msg) ||
1869 wps_build_msg_type(msg, WPS_M4) ||
1870 wps_build_enrollee_nonce(wps, msg) ||
1871 wps_build_r_hash(wps, msg) ||
1872 wps_build_r_snonce1(wps, plain) ||
1873 wps_build_key_wrap_auth(wps, plain) ||
1874 wps_build_encr_settings(wps, msg, plain) ||
1875 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1876 wps_build_authenticator(wps, msg)) {
1883 wps->state = RECV_M5;
1888 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1890 struct wpabuf *msg, *plain;
1892 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1894 plain = wpabuf_alloc(200);
1898 msg = wpabuf_alloc(1000);
1904 if (wps_build_version(msg) ||
1905 wps_build_msg_type(msg, WPS_M6) ||
1906 wps_build_enrollee_nonce(wps, msg) ||
1907 wps_build_r_snonce2(wps, plain) ||
1908 wps_build_key_wrap_auth(wps, plain) ||
1909 wps_build_encr_settings(wps, msg, plain) ||
1910 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1911 wps_build_authenticator(wps, msg)) {
1918 wps->wps_pin_revealed = 1;
1919 wps->state = RECV_M7;
1924 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1926 struct wpabuf *msg, *plain;
1928 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1930 plain = wpabuf_alloc(500);
1934 msg = wpabuf_alloc(1000);
1940 if (wps_build_version(msg) ||
1941 wps_build_msg_type(msg, WPS_M8) ||
1942 wps_build_enrollee_nonce(wps, msg) ||
1943 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1944 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1945 wps_build_key_wrap_auth(wps, plain) ||
1946 wps_build_encr_settings(wps, msg, plain) ||
1947 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1948 wps_build_authenticator(wps, msg)) {
1955 wps->state = RECV_DONE;
1960 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1961 enum wsc_op_code *op_code)
1965 #ifdef CONFIG_WPS_UPNP
1966 if (!wps->int_reg && wps->wps->wps_upnp) {
1967 struct upnp_pending_message *p, *prev = NULL;
1968 if (wps->ext_reg > 1)
1969 wps_registrar_free_pending_m2(wps->wps);
1970 p = wps->wps->upnp_msgs;
1971 /* TODO: check pending message MAC address */
1972 while (p && p->next) {
1977 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1982 wps->wps->upnp_msgs = NULL;
1989 *op_code = WSC_NACK;
1996 if (wps->ext_reg == 0)
2002 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
2003 "pending message available");
2006 #endif /* CONFIG_WPS_UPNP */
2008 switch (wps->state) {
2010 if (wps_get_dev_password(wps) < 0)
2011 msg = wps_build_m2d(wps);
2013 msg = wps_build_m2(wps);
2017 msg = wps_build_m2d(wps);
2021 msg = wps_build_m4(wps);
2025 msg = wps_build_m6(wps);
2029 msg = wps_build_m8(wps);
2033 msg = wps_build_wsc_ack(wps);
2037 msg = wps_build_wsc_nack(wps);
2038 *op_code = WSC_NACK;
2041 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
2042 "a message", wps->state);
2047 if (*op_code == WSC_MSG && msg) {
2048 /* Save a copy of the last message for Authenticator derivation
2050 wpabuf_free(wps->last_msg);
2051 wps->last_msg = wpabuf_dup(msg);
2058 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
2060 if (e_nonce == NULL) {
2061 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
2065 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
2066 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
2067 wps->nonce_e, WPS_NONCE_LEN);
2073 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
2075 if (r_nonce == NULL) {
2076 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
2080 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
2081 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
2089 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
2091 if (uuid_e == NULL) {
2092 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
2096 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
2097 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
2103 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
2105 if (pw_id == NULL) {
2106 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
2110 wps->dev_pw_id = WPA_GET_BE16(pw_id);
2111 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
2117 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
2119 if (e_hash1 == NULL) {
2120 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
2124 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
2125 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
2131 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
2133 if (e_hash2 == NULL) {
2134 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
2138 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
2139 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
2145 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
2147 u8 hash[SHA256_MAC_LEN];
2151 if (e_snonce1 == NULL) {
2152 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
2156 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
2157 WPS_SECRET_NONCE_LEN);
2159 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
2160 addr[0] = e_snonce1;
2161 len[0] = WPS_SECRET_NONCE_LEN;
2162 addr[1] = wps->psk1;
2163 len[1] = WPS_PSK_LEN;
2164 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2165 len[2] = wpabuf_len(wps->dh_pubkey_e);
2166 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2167 len[3] = wpabuf_len(wps->dh_pubkey_r);
2168 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2170 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
2171 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
2172 "not match with the pre-committed value");
2173 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2174 wps_pwd_auth_fail_event(wps->wps, 0, 1);
2178 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
2179 "half of the device password");
2185 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
2187 u8 hash[SHA256_MAC_LEN];
2191 if (e_snonce2 == NULL) {
2192 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
2196 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
2197 WPS_SECRET_NONCE_LEN);
2199 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
2200 addr[0] = e_snonce2;
2201 len[0] = WPS_SECRET_NONCE_LEN;
2202 addr[1] = wps->psk2;
2203 len[1] = WPS_PSK_LEN;
2204 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2205 len[2] = wpabuf_len(wps->dh_pubkey_e);
2206 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2207 len[3] = wpabuf_len(wps->dh_pubkey_r);
2208 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2210 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
2211 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
2212 "not match with the pre-committed value");
2213 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2214 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2215 wps_pwd_auth_fail_event(wps->wps, 0, 2);
2219 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
2220 "half of the device password");
2221 wps->wps_pin_revealed = 0;
2222 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
2225 * In case wildcard PIN is used and WPS handshake succeeds in the first
2226 * attempt, wps_registrar_unlock_pin() would not free the PIN, so make
2227 * sure the PIN gets invalidated here.
2229 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2235 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
2237 if (mac_addr == NULL) {
2238 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
2242 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
2244 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
2245 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
2251 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
2254 if (pk == NULL || pk_len == 0) {
2255 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
2259 wpabuf_free(wps->dh_pubkey_e);
2260 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
2261 if (wps->dh_pubkey_e == NULL)
2268 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
2273 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
2278 auth_types = WPA_GET_BE16(auth);
2280 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
2282 wps->auth_type = wps->wps->auth_types & auth_types;
2283 if (wps->auth_type == 0) {
2284 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2285 "authentication types (own 0x%x Enrollee 0x%x)",
2286 wps->wps->auth_types, auth_types);
2287 #ifdef WPS_WORKAROUNDS
2289 * Some deployed implementations seem to advertise incorrect
2290 * information in this attribute. For example, Linksys WRT350N
2291 * seems to have a byteorder bug that breaks this negotiation.
2292 * In order to interoperate with existing implementations,
2293 * assume that the Enrollee supports everything we do.
2295 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2296 "does not advertise supported authentication types "
2298 wps->auth_type = wps->wps->auth_types;
2299 #else /* WPS_WORKAROUNDS */
2301 #endif /* WPS_WORKAROUNDS */
2308 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
2313 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
2318 encr_types = WPA_GET_BE16(encr);
2320 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
2322 wps->encr_type = wps->wps->encr_types & encr_types;
2323 if (wps->encr_type == 0) {
2324 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2325 "encryption types (own 0x%x Enrollee 0x%x)",
2326 wps->wps->encr_types, encr_types);
2327 #ifdef WPS_WORKAROUNDS
2329 * Some deployed implementations seem to advertise incorrect
2330 * information in this attribute. For example, Linksys WRT350N
2331 * seems to have a byteorder bug that breaks this negotiation.
2332 * In order to interoperate with existing implementations,
2333 * assume that the Enrollee supports everything we do.
2335 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2336 "does not advertise supported encryption types "
2338 wps->encr_type = wps->wps->encr_types;
2339 #else /* WPS_WORKAROUNDS */
2341 #endif /* WPS_WORKAROUNDS */
2348 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
2351 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
2356 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
2363 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
2367 if (methods == NULL) {
2368 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
2372 m = WPA_GET_BE16(methods);
2374 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
2375 "%s%s%s%s%s%s%s%s%s", m,
2376 m & WPS_CONFIG_USBA ? " [USBA]" : "",
2377 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
2378 m & WPS_CONFIG_LABEL ? " [Label]" : "",
2379 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
2380 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
2381 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
2382 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
2383 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
2384 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
2386 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
2388 * The Enrollee does not have a display so it is unlikely to be
2389 * able to show the passphrase to a user and as such, could
2390 * benefit from receiving PSK to reduce key derivation time.
2392 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2393 "Enrollee not supporting display");
2394 wps->use_psk_key = 1;
2401 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2403 if (state == NULL) {
2404 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2409 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2416 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2420 if (assoc == NULL) {
2421 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2425 a = WPA_GET_BE16(assoc);
2426 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2432 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2437 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2441 e = WPA_GET_BE16(err);
2442 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2448 static int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
2451 struct wps_registrar *reg = wps->wps->registrar;
2453 if (is_zero_ether_addr(reg->p2p_dev_addr))
2454 return 1; /* no filtering in use */
2456 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
2457 wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
2458 "filtering for PBC: expected " MACSTR " was "
2459 MACSTR " - indicate PBC session overlap",
2460 MAC2STR(reg->p2p_dev_addr),
2461 MAC2STR(wps->p2p_dev_addr));
2464 #endif /* CONFIG_P2P */
2469 static int wps_registrar_skip_overlap(struct wps_data *wps)
2472 struct wps_registrar *reg = wps->wps->registrar;
2474 if (is_zero_ether_addr(reg->p2p_dev_addr))
2475 return 0; /* no specific Enrollee selected */
2477 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) == 0) {
2478 wpa_printf(MSG_DEBUG, "WPS: Skip PBC overlap due to selected "
2482 #endif /* CONFIG_P2P */
2487 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2488 struct wps_parse_attr *attr)
2490 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2492 if (wps->state != RECV_M1) {
2493 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2494 "receiving M1", wps->state);
2498 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2499 wps_process_mac_addr(wps, attr->mac_addr) ||
2500 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2501 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2502 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2503 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2504 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2505 wps_process_config_methods(wps, attr->config_methods) ||
2506 wps_process_wps_state(wps, attr->wps_state) ||
2507 wps_process_device_attrs(&wps->peer_dev, attr) ||
2508 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2509 wps_process_assoc_state(wps, attr->assoc_state) ||
2510 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2511 wps_process_config_error(wps, attr->config_error) ||
2512 wps_process_os_version(&wps->peer_dev, attr->os_version))
2515 if (wps->dev_pw_id < 0x10 &&
2516 wps->dev_pw_id != DEV_PW_DEFAULT &&
2517 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2518 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2519 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2520 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2521 !wps->wps->registrar->pbc)) {
2522 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2524 wps->state = SEND_M2D;
2525 return WPS_CONTINUE;
2528 #ifdef CONFIG_WPS_NFC
2529 if (wps->dev_pw_id >= 0x10) {
2530 struct wps_nfc_pw_token *token;
2532 u8 hash[WPS_HASH_LEN];
2534 token = wps_get_nfc_pw_token(
2535 &wps->wps->registrar->nfc_pw_tokens, wps->dev_pw_id);
2537 wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
2539 dl_list_del(&token->list);
2540 wps->nfc_pw_token = token;
2542 addr[0] = attr->public_key;
2543 sha256_vector(1, addr, &attr->public_key_len, hash);
2544 if (os_memcmp(hash, wps->nfc_pw_token->pubkey_hash,
2545 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
2546 wpa_printf(MSG_ERROR, "WPS: Public Key hash "
2552 #endif /* CONFIG_WPS_NFC */
2554 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2555 if ((wps->wps->registrar->force_pbc_overlap ||
2556 wps_registrar_pbc_overlap(wps->wps->registrar,
2557 wps->mac_addr_e, wps->uuid_e) ||
2558 !wps_registrar_p2p_dev_addr_match(wps)) &&
2559 !wps_registrar_skip_overlap(wps)) {
2560 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2562 wps->state = SEND_M2D;
2563 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2564 wps_pbc_overlap_event(wps->wps);
2565 wps_fail_event(wps->wps, WPS_M1,
2566 WPS_CFG_MULTIPLE_PBC_DETECTED,
2568 wps->wps->registrar->force_pbc_overlap = 1;
2569 return WPS_CONTINUE;
2571 wps_registrar_add_pbc_session(wps->wps->registrar,
2572 wps->mac_addr_e, wps->uuid_e);
2576 #ifdef WPS_WORKAROUNDS
2578 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2579 * passphrase format. To avoid interop issues, force PSK format to be
2582 if (!wps->use_psk_key &&
2583 wps->peer_dev.manufacturer &&
2584 os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
2585 wps->peer_dev.model_name &&
2586 os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
2587 wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
2589 wps->use_psk_key = 1;
2591 #endif /* WPS_WORKAROUNDS */
2593 wps->state = SEND_M2;
2594 return WPS_CONTINUE;
2598 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2599 const struct wpabuf *msg,
2600 struct wps_parse_attr *attr)
2602 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2604 if (wps->state != RECV_M3) {
2605 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2606 "receiving M3", wps->state);
2607 wps->state = SEND_WSC_NACK;
2608 return WPS_CONTINUE;
2611 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2612 !wps_registrar_skip_overlap(wps)) {
2613 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2615 wps->state = SEND_WSC_NACK;
2616 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2617 return WPS_CONTINUE;
2620 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2621 wps_process_authenticator(wps, attr->authenticator, msg) ||
2622 wps_process_e_hash1(wps, attr->e_hash1) ||
2623 wps_process_e_hash2(wps, attr->e_hash2)) {
2624 wps->state = SEND_WSC_NACK;
2625 return WPS_CONTINUE;
2628 wps->state = SEND_M4;
2629 return WPS_CONTINUE;
2633 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2634 const struct wpabuf *msg,
2635 struct wps_parse_attr *attr)
2637 struct wpabuf *decrypted;
2638 struct wps_parse_attr eattr;
2640 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2642 if (wps->state != RECV_M5) {
2643 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2644 "receiving M5", wps->state);
2645 wps->state = SEND_WSC_NACK;
2646 return WPS_CONTINUE;
2649 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2650 !wps_registrar_skip_overlap(wps)) {
2651 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2653 wps->state = SEND_WSC_NACK;
2654 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2655 return WPS_CONTINUE;
2658 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2659 wps_process_authenticator(wps, attr->authenticator, msg)) {
2660 wps->state = SEND_WSC_NACK;
2661 return WPS_CONTINUE;
2664 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2665 attr->encr_settings_len);
2666 if (decrypted == NULL) {
2667 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2668 "Settings attribute");
2669 wps->state = SEND_WSC_NACK;
2670 return WPS_CONTINUE;
2673 if (wps_validate_m5_encr(decrypted, attr->version2 != NULL) < 0) {
2674 wpabuf_free(decrypted);
2675 wps->state = SEND_WSC_NACK;
2676 return WPS_CONTINUE;
2679 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2681 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2682 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2683 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2684 wpabuf_free(decrypted);
2685 wps->state = SEND_WSC_NACK;
2686 return WPS_CONTINUE;
2688 wpabuf_free(decrypted);
2690 wps->state = SEND_M6;
2691 return WPS_CONTINUE;
2695 static void wps_sta_cred_cb(struct wps_data *wps)
2698 * Update credential to only include a single authentication and
2699 * encryption type in case the AP configuration includes more than one
2702 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2703 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2704 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2705 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2706 if (wps->cred.encr_type & WPS_ENCR_AES)
2707 wps->cred.encr_type = WPS_ENCR_AES;
2708 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2709 wps->cred.encr_type = WPS_ENCR_TKIP;
2710 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2711 "AP configuration");
2712 if (wps->wps->cred_cb)
2713 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2717 static void wps_cred_update(struct wps_credential *dst,
2718 struct wps_credential *src)
2720 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2721 dst->ssid_len = src->ssid_len;
2722 dst->auth_type = src->auth_type;
2723 dst->encr_type = src->encr_type;
2724 dst->key_idx = src->key_idx;
2725 os_memcpy(dst->key, src->key, sizeof(dst->key));
2726 dst->key_len = src->key_len;
2730 static int wps_process_ap_settings_r(struct wps_data *wps,
2731 struct wps_parse_attr *attr)
2735 if (wps->wps->ap || wps->er)
2738 /* AP Settings Attributes in M7 when Enrollee is an AP */
2739 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2742 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2744 if (wps->new_ap_settings) {
2745 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2747 wps_cred_update(&wps->cred, wps->new_ap_settings);
2751 * Use the AP PIN only to receive the current AP settings, not
2752 * to reconfigure the AP.
2756 * Clear selected registrar here since we do not get to
2757 * WSC_Done in this protocol run.
2759 wps_registrar_pin_completed(wps->wps->registrar);
2761 msg = wps_build_ap_cred(wps);
2764 wps->cred.cred_attr = wpabuf_head(msg);
2765 wps->cred.cred_attr_len = wpabuf_len(msg);
2767 if (wps->ap_settings_cb) {
2768 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2773 wps_sta_cred_cb(wps);
2775 wps->cred.cred_attr = NULL;
2776 wps->cred.cred_attr_len = 0;
2784 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2785 const struct wpabuf *msg,
2786 struct wps_parse_attr *attr)
2788 struct wpabuf *decrypted;
2789 struct wps_parse_attr eattr;
2791 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2793 if (wps->state != RECV_M7) {
2794 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2795 "receiving M7", wps->state);
2796 wps->state = SEND_WSC_NACK;
2797 return WPS_CONTINUE;
2800 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2801 !wps_registrar_skip_overlap(wps)) {
2802 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2804 wps->state = SEND_WSC_NACK;
2805 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2806 return WPS_CONTINUE;
2809 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2810 wps_process_authenticator(wps, attr->authenticator, msg)) {
2811 wps->state = SEND_WSC_NACK;
2812 return WPS_CONTINUE;
2815 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2816 attr->encr_settings_len);
2817 if (decrypted == NULL) {
2818 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2819 "Settings attribute");
2820 wps->state = SEND_WSC_NACK;
2821 return WPS_CONTINUE;
2824 if (wps_validate_m7_encr(decrypted, wps->wps->ap || wps->er,
2825 attr->version2 != NULL) < 0) {
2826 wpabuf_free(decrypted);
2827 wps->state = SEND_WSC_NACK;
2828 return WPS_CONTINUE;
2831 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2833 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2834 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2835 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2836 wps_process_ap_settings_r(wps, &eattr)) {
2837 wpabuf_free(decrypted);
2838 wps->state = SEND_WSC_NACK;
2839 return WPS_CONTINUE;
2842 wpabuf_free(decrypted);
2844 wps->state = SEND_M8;
2845 return WPS_CONTINUE;
2849 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2850 const struct wpabuf *msg)
2852 struct wps_parse_attr attr;
2853 enum wps_process_res ret = WPS_CONTINUE;
2855 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2857 if (wps_parse_msg(msg, &attr) < 0)
2860 if (attr.msg_type == NULL) {
2861 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2862 wps->state = SEND_WSC_NACK;
2863 return WPS_CONTINUE;
2866 if (*attr.msg_type != WPS_M1 &&
2867 (attr.registrar_nonce == NULL ||
2868 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2869 WPS_NONCE_LEN) != 0)) {
2870 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2874 switch (*attr.msg_type) {
2876 if (wps_validate_m1(msg) < 0)
2878 #ifdef CONFIG_WPS_UPNP
2879 if (wps->wps->wps_upnp && attr.mac_addr) {
2880 /* Remove old pending messages when starting new run */
2881 wps_free_pending_msgs(wps->wps->upnp_msgs);
2882 wps->wps->upnp_msgs = NULL;
2884 upnp_wps_device_send_wlan_event(
2885 wps->wps->wps_upnp, attr.mac_addr,
2886 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2888 #endif /* CONFIG_WPS_UPNP */
2889 ret = wps_process_m1(wps, &attr);
2892 if (wps_validate_m3(msg) < 0)
2894 ret = wps_process_m3(wps, msg, &attr);
2895 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2896 wps_fail_event(wps->wps, WPS_M3, wps->config_error,
2897 wps->error_indication);
2900 if (wps_validate_m5(msg) < 0)
2902 ret = wps_process_m5(wps, msg, &attr);
2903 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2904 wps_fail_event(wps->wps, WPS_M5, wps->config_error,
2905 wps->error_indication);
2908 if (wps_validate_m7(msg) < 0)
2910 ret = wps_process_m7(wps, msg, &attr);
2911 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2912 wps_fail_event(wps->wps, WPS_M7, wps->config_error,
2913 wps->error_indication);
2916 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2921 if (ret == WPS_CONTINUE) {
2922 /* Save a copy of the last message for Authenticator derivation
2924 wpabuf_free(wps->last_msg);
2925 wps->last_msg = wpabuf_dup(msg);
2932 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2933 const struct wpabuf *msg)
2935 struct wps_parse_attr attr;
2937 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2939 if (wps_parse_msg(msg, &attr) < 0)
2942 if (attr.msg_type == NULL) {
2943 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2947 if (*attr.msg_type != WPS_WSC_ACK) {
2948 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2953 #ifdef CONFIG_WPS_UPNP
2954 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2955 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2956 if (wps->wps->upnp_msgs)
2957 return WPS_CONTINUE;
2958 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2959 "external Registrar");
2962 #endif /* CONFIG_WPS_UPNP */
2964 if (attr.registrar_nonce == NULL ||
2965 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
2967 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2971 if (attr.enrollee_nonce == NULL ||
2972 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
2973 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2977 if (wps->state == RECV_M2D_ACK) {
2978 #ifdef CONFIG_WPS_UPNP
2979 if (wps->wps->wps_upnp &&
2980 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2981 if (wps->wps->upnp_msgs)
2982 return WPS_CONTINUE;
2983 if (wps->ext_reg == 0)
2985 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2986 "external Registrar");
2989 #endif /* CONFIG_WPS_UPNP */
2991 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2992 "terminate negotiation");
2999 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
3000 const struct wpabuf *msg)
3002 struct wps_parse_attr attr;
3006 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
3008 old_state = wps->state;
3009 wps->state = SEND_WSC_NACK;
3011 if (wps_parse_msg(msg, &attr) < 0)
3014 if (attr.msg_type == NULL) {
3015 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3019 if (*attr.msg_type != WPS_WSC_NACK) {
3020 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3025 #ifdef CONFIG_WPS_UPNP
3026 if (wps->wps->wps_upnp && wps->ext_reg) {
3027 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3028 "Registrar terminated by the Enrollee");
3031 #endif /* CONFIG_WPS_UPNP */
3033 if (attr.registrar_nonce == NULL ||
3034 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3036 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3040 if (attr.enrollee_nonce == NULL ||
3041 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3042 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3046 if (attr.config_error == NULL) {
3047 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
3052 config_error = WPA_GET_BE16(attr.config_error);
3053 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
3054 "Configuration Error %d", config_error);
3056 switch (old_state) {
3058 wps_fail_event(wps->wps, WPS_M2, config_error,
3059 wps->error_indication);
3062 wps_fail_event(wps->wps, WPS_M4, config_error,
3063 wps->error_indication);
3066 wps_fail_event(wps->wps, WPS_M6, config_error,
3067 wps->error_indication);
3070 wps_fail_event(wps->wps, WPS_M8, config_error,
3071 wps->error_indication);
3081 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
3082 const struct wpabuf *msg)
3084 struct wps_parse_attr attr;
3086 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
3088 if (wps->state != RECV_DONE &&
3089 (!wps->wps->wps_upnp || !wps->ext_reg)) {
3090 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
3091 "receiving WSC_Done", wps->state);
3095 if (wps_parse_msg(msg, &attr) < 0)
3098 if (attr.msg_type == NULL) {
3099 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3103 if (*attr.msg_type != WPS_WSC_DONE) {
3104 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3109 #ifdef CONFIG_WPS_UPNP
3110 if (wps->wps->wps_upnp && wps->ext_reg) {
3111 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3112 "Registrar completed successfully");
3113 wps_device_store(wps->wps->registrar, &wps->peer_dev,
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 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
3133 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3136 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
3137 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
3138 struct wps_credential cred;
3140 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
3141 "on first Enrollee connection");
3143 os_memset(&cred, 0, sizeof(cred));
3144 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
3145 cred.ssid_len = wps->wps->ssid_len;
3146 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
3147 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
3148 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
3149 cred.key_len = wps->new_psk_len;
3151 wps->wps->wps_state = WPS_STATE_CONFIGURED;
3152 wpa_hexdump_ascii_key(MSG_DEBUG,
3153 "WPS: Generated random passphrase",
3154 wps->new_psk, wps->new_psk_len);
3155 if (wps->wps->cred_cb)
3156 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
3158 os_free(wps->new_psk);
3159 wps->new_psk = NULL;
3162 if (!wps->wps->ap && !wps->er)
3163 wps_sta_cred_cb(wps);
3166 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
3167 wps->new_psk, wps->new_psk_len)) {
3168 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
3171 os_free(wps->new_psk);
3172 wps->new_psk = NULL;
3175 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e,
3176 wps->dev_password, wps->dev_password_len);
3179 wps_registrar_remove_pbc_session(wps->wps->registrar,
3182 wps_registrar_pbc_completed(wps->wps->registrar);
3183 os_get_time(&wps->wps->registrar->pbc_ignore_start);
3184 os_memcpy(wps->wps->registrar->pbc_ignore_uuid, wps->uuid_e,
3187 wps_registrar_pin_completed(wps->wps->registrar);
3189 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
3190 * merge them into APs own list.. */
3192 wps_success_event(wps->wps);
3198 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
3199 enum wsc_op_code op_code,
3200 const struct wpabuf *msg)
3202 enum wps_process_res ret;
3204 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
3206 (unsigned long) wpabuf_len(msg), op_code);
3208 #ifdef CONFIG_WPS_UPNP
3209 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
3210 struct wps_parse_attr attr;
3211 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
3212 *attr.msg_type == WPS_M3)
3213 wps->ext_reg = 2; /* past M2/M2D phase */
3215 if (wps->ext_reg > 1)
3216 wps_registrar_free_pending_m2(wps->wps);
3217 if (wps->wps->wps_upnp && wps->ext_reg &&
3218 wps->wps->upnp_msgs == NULL &&
3219 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
3221 struct wps_parse_attr attr;
3223 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
3226 type = *attr.msg_type;
3227 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
3228 " to external Registrar for processing", type);
3229 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
3231 UPNP_WPS_WLANEVENT_TYPE_EAP,
3233 if (op_code == WSC_MSG)
3235 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
3236 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
3237 "external Registrar");
3238 return WPS_CONTINUE;
3240 #endif /* CONFIG_WPS_UPNP */
3244 return wps_process_wsc_msg(wps, msg);
3246 if (wps_validate_wsc_ack(msg) < 0)
3248 return wps_process_wsc_ack(wps, msg);
3250 if (wps_validate_wsc_nack(msg) < 0)
3252 return wps_process_wsc_nack(wps, msg);
3254 if (wps_validate_wsc_done(msg) < 0)
3256 ret = wps_process_wsc_done(wps, msg);
3257 if (ret == WPS_FAILURE) {
3258 wps->state = SEND_WSC_NACK;
3259 wps_fail_event(wps->wps, WPS_WSC_DONE,
3261 wps->error_indication);
3265 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
3271 int wps_registrar_update_ie(struct wps_registrar *reg)
3273 return wps_set_ie(reg);
3277 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
3280 struct wps_registrar *reg = eloop_ctx;
3282 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
3283 "unselect internal Registrar");
3284 reg->selected_registrar = 0;
3286 wps_registrar_selected_registrar_changed(reg);
3290 #ifdef CONFIG_WPS_UPNP
3291 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
3292 struct subscription *s)
3295 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
3296 "config_methods=0x%x)",
3297 s->dev_password_id, s->config_methods);
3298 reg->sel_reg_union = 1;
3299 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
3300 reg->sel_reg_dev_password_id_override = s->dev_password_id;
3301 if (reg->sel_reg_config_methods_override == -1)
3302 reg->sel_reg_config_methods_override = 0;
3303 reg->sel_reg_config_methods_override |= s->config_methods;
3304 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
3305 if (is_zero_ether_addr(reg->authorized_macs_union[i]))
3307 for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
3309 if (is_zero_ether_addr(s->authorized_macs[j]))
3311 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC into union: "
3312 MACSTR, MAC2STR(s->authorized_macs[j]));
3313 os_memcpy(reg->authorized_macs_union[i],
3314 s->authorized_macs[j], ETH_ALEN);
3317 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union",
3318 (u8 *) reg->authorized_macs_union,
3319 sizeof(reg->authorized_macs_union));
3321 #endif /* CONFIG_WPS_UPNP */
3324 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
3326 #ifdef CONFIG_WPS_UPNP
3327 struct subscription *s;
3329 if (reg->wps->wps_upnp == NULL)
3332 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
3333 struct subscription, list) {
3334 struct subscr_addr *sa;
3335 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
3337 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
3338 inet_ntoa(sa->saddr.sin_addr),
3339 ntohs(sa->saddr.sin_port));
3341 if (s->selected_registrar)
3342 wps_registrar_sel_reg_add(reg, s);
3344 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
3347 #endif /* CONFIG_WPS_UPNP */
3352 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3353 * @reg: Registrar data from wps_registrar_init()
3355 * This function is called when selected registrar state changes, e.g., when an
3356 * AP receives a SetSelectedRegistrar UPnP message.
3358 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg)
3360 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
3362 reg->sel_reg_union = reg->selected_registrar;
3363 reg->sel_reg_dev_password_id_override = -1;
3364 reg->sel_reg_config_methods_override = -1;
3365 os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
3366 WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
3367 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union (start with own)",
3368 (u8 *) reg->authorized_macs_union,
3369 sizeof(reg->authorized_macs_union));
3370 if (reg->selected_registrar) {
3373 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
3375 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
3376 WPS_CONFIG_PHY_PUSHBUTTON);
3377 #endif /* CONFIG_WPS2 */
3379 reg->sel_reg_dev_password_id_override =
3381 wps_set_pushbutton(&methods, reg->wps->config_methods);
3383 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
3384 "(pbc=%d)", reg->pbc);
3385 reg->sel_reg_config_methods_override = methods;
3387 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
3389 wps_registrar_sel_reg_union(reg);
3392 wps_cb_set_sel_reg(reg);
3396 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
3397 char *buf, size_t buflen)
3399 struct wps_registrar_device *d;
3402 char devtype[WPS_DEV_TYPE_BUFSIZE];
3404 d = wps_device_get(reg, addr);
3407 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
3410 ret = os_snprintf(buf + len, buflen - len,
3412 "wpsPrimaryDeviceType=%s\n"
3413 "wpsDeviceName=%s\n"
3414 "wpsManufacturer=%s\n"
3416 "wpsModelNumber=%s\n"
3417 "wpsSerialNumber=%s\n",
3419 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
3421 d->dev.device_name ? d->dev.device_name : "",
3422 d->dev.manufacturer ? d->dev.manufacturer : "",
3423 d->dev.model_name ? d->dev.model_name : "",
3424 d->dev.model_number ? d->dev.model_number : "",
3425 d->dev.serial_number ? d->dev.serial_number : "");
3426 if (ret < 0 || (size_t) ret >= buflen - len)
3434 int wps_registrar_config_ap(struct wps_registrar *reg,
3435 struct wps_credential *cred)
3438 printf("encr_type=0x%x\n", cred->encr_type);
3439 if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
3441 if (cred->encr_type & WPS_ENCR_WEP) {
3442 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
3443 "due to WEP configuration");
3447 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
3448 "invalid encr_type 0x%x", cred->encr_type);
3452 if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
3454 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
3456 cred->encr_type |= WPS_ENCR_AES;
3459 if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
3461 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
3463 cred->auth_type |= WPS_AUTH_WPA2PSK;
3465 #endif /* CONFIG_WPS2 */
3467 if (reg->wps->cred_cb)
3468 return reg->wps->cred_cb(reg->wps->cb_ctx, cred);
3474 #ifdef CONFIG_WPS_NFC
3476 int wps_registrar_add_nfc_pw_token(struct wps_registrar *reg,
3477 const u8 *pubkey_hash, u16 pw_id,
3478 const u8 *dev_pw, size_t dev_pw_len)
3480 struct wps_nfc_pw_token *token;
3482 if (dev_pw_len > WPS_OOB_DEVICE_PASSWORD_LEN)
3485 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, pw_id);
3487 token = os_zalloc(sizeof(*token));
3491 os_memcpy(token->pubkey_hash, pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
3492 token->pw_id = pw_id;
3493 os_memcpy(token->dev_pw, dev_pw, dev_pw_len);
3494 token->dev_pw_len = dev_pw_len;
3496 dl_list_add(®->nfc_pw_tokens, &token->list);
3498 reg->selected_registrar = 1;
3500 wps_registrar_add_authorized_mac(reg,
3501 (u8 *) "\xff\xff\xff\xff\xff\xff");
3502 wps_registrar_selected_registrar_changed(reg);
3503 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
3504 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
3505 wps_registrar_set_selected_timeout,
3512 int wps_registrar_add_nfc_password_token(struct wps_registrar *reg,
3513 const u8 *oob_dev_pw,
3514 size_t oob_dev_pw_len)
3516 const u8 *pos, *hash, *dev_pw;
3520 if (oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2 +
3521 WPS_OOB_DEVICE_PASSWORD_MIN_LEN ||
3522 oob_dev_pw_len > WPS_OOB_PUBKEY_HASH_LEN + 2 +
3523 WPS_OOB_DEVICE_PASSWORD_LEN)
3527 pos = oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN;
3528 id = WPA_GET_BE16(pos);
3530 dev_pw_len = oob_dev_pw + oob_dev_pw_len - dev_pw;
3532 wpa_printf(MSG_DEBUG, "WPS: Add NFC Password Token for Password ID %u",
3535 wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
3536 hash, WPS_OOB_PUBKEY_HASH_LEN);
3537 wpa_hexdump_key(MSG_DEBUG, "WPS: Device Password", dev_pw, dev_pw_len);
3539 return wps_registrar_add_nfc_pw_token(reg, hash, id, dev_pw,
3544 void wps_registrar_remove_nfc_pw_token(struct wps_registrar *reg,
3545 struct wps_nfc_pw_token *token)
3547 wps_registrar_remove_authorized_mac(reg,
3548 (u8 *) "\xff\xff\xff\xff\xff\xff");
3549 wps_registrar_selected_registrar_changed(reg);
3552 #endif /* CONFIG_WPS_NFC */