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];
35 u8 dev_pw[WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1];
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 *p2p_dev_addr,
146 const u8 *psk, size_t psk_len);
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;
174 int force_per_enrollee_psk;
176 struct wps_registrar_device *devices;
178 int force_pbc_overlap;
180 u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
181 u8 authorized_macs_union[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
183 u8 p2p_dev_addr[ETH_ALEN];
185 u8 pbc_ignore_uuid[WPS_UUID_LEN];
186 struct os_time pbc_ignore_start;
190 static int wps_set_ie(struct wps_registrar *reg);
191 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
192 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
194 static void wps_registrar_remove_pin(struct wps_registrar *reg,
195 struct wps_uuid_pin *pin);
198 static void wps_registrar_add_authorized_mac(struct wps_registrar *reg,
202 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC " MACSTR,
204 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
205 if (os_memcmp(reg->authorized_macs[i], addr, ETH_ALEN) == 0) {
206 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was "
207 "already in the list");
208 return; /* already in list */
210 for (i = WPS_MAX_AUTHORIZED_MACS - 1; i > 0; i--)
211 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i - 1],
213 os_memcpy(reg->authorized_macs[0], addr, ETH_ALEN);
214 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
215 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
219 static void wps_registrar_remove_authorized_mac(struct wps_registrar *reg,
223 wpa_printf(MSG_DEBUG, "WPS: Remove authorized MAC " MACSTR,
225 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++) {
226 if (os_memcmp(reg->authorized_macs, addr, ETH_ALEN) == 0)
229 if (i == WPS_MAX_AUTHORIZED_MACS) {
230 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was not in the "
232 return; /* not in the list */
234 for (; i + 1 < WPS_MAX_AUTHORIZED_MACS; i++)
235 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i + 1],
237 os_memset(reg->authorized_macs[WPS_MAX_AUTHORIZED_MACS - 1], 0,
239 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
240 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
244 static void wps_free_devices(struct wps_registrar_device *dev)
246 struct wps_registrar_device *prev;
251 wps_device_data_free(&prev->dev);
257 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
260 struct wps_registrar_device *dev;
262 for (dev = reg->devices; dev; dev = dev->next) {
263 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
270 static void wps_device_clone_data(struct wps_device_data *dst,
271 struct wps_device_data *src)
273 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
274 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
276 #define WPS_STRDUP(n) \
278 dst->n = src->n ? os_strdup(src->n) : NULL
280 WPS_STRDUP(device_name);
281 WPS_STRDUP(manufacturer);
282 WPS_STRDUP(model_name);
283 WPS_STRDUP(model_number);
284 WPS_STRDUP(serial_number);
289 int wps_device_store(struct wps_registrar *reg,
290 struct wps_device_data *dev, const u8 *uuid)
292 struct wps_registrar_device *d;
294 d = wps_device_get(reg, dev->mac_addr);
296 d = os_zalloc(sizeof(*d));
299 d->next = reg->devices;
303 wps_device_clone_data(&d->dev, dev);
304 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
310 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
311 const u8 *addr, const u8 *uuid_e)
313 struct wps_pbc_session *pbc, *prev = NULL;
318 pbc = reg->pbc_sessions;
320 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
321 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
323 prev->next = pbc->next;
325 reg->pbc_sessions = pbc->next;
333 pbc = os_zalloc(sizeof(*pbc));
336 os_memcpy(pbc->addr, addr, ETH_ALEN);
338 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
341 pbc->next = reg->pbc_sessions;
342 reg->pbc_sessions = pbc;
343 pbc->timestamp = now;
345 /* remove entries that have timed out */
350 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
352 wps_free_pbc_sessions(pbc);
361 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
363 const u8 *p2p_dev_addr)
365 struct wps_pbc_session *pbc, *prev = NULL, *tmp;
367 pbc = reg->pbc_sessions;
369 if (os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0 ||
370 (p2p_dev_addr && !is_zero_ether_addr(reg->p2p_dev_addr) &&
371 os_memcmp(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
374 prev->next = pbc->next;
376 reg->pbc_sessions = pbc->next;
379 wpa_printf(MSG_DEBUG, "WPS: Removing PBC session for "
380 "addr=" MACSTR, MAC2STR(tmp->addr));
381 wpa_hexdump(MSG_DEBUG, "WPS: Removed UUID-E",
382 tmp->uuid_e, WPS_UUID_LEN);
392 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
393 const u8 *addr, const u8 *uuid_e)
396 struct wps_pbc_session *pbc;
397 struct wps_pbc_session *first = NULL;
402 wpa_printf(MSG_DEBUG, "WPS: Checking active PBC sessions for overlap");
405 wpa_printf(MSG_DEBUG, "WPS: Add one for the requested UUID");
406 wpa_hexdump(MSG_DEBUG, "WPS: Requested UUID",
407 uuid_e, WPS_UUID_LEN);
411 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
412 wpa_printf(MSG_DEBUG, "WPS: Consider PBC session with " MACSTR,
414 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E",
415 pbc->uuid_e, WPS_UUID_LEN);
416 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
417 wpa_printf(MSG_DEBUG, "WPS: PBC walk time has "
422 os_memcmp(pbc->uuid_e, first->uuid_e, WPS_UUID_LEN) == 0) {
423 wpa_printf(MSG_DEBUG, "WPS: Same Enrollee");
424 continue; /* same Enrollee */
426 if (uuid_e == NULL ||
427 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN)) {
428 wpa_printf(MSG_DEBUG, "WPS: New Enrollee");
435 wpa_printf(MSG_DEBUG, "WPS: %u active PBC session(s) found", count);
437 return count > 1 ? 1 : 0;
441 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
443 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
445 wpabuf_put_be16(msg, ATTR_WPS_STATE);
446 wpabuf_put_be16(msg, 1);
447 wpabuf_put_u8(msg, wps->wps_state);
452 #ifdef CONFIG_WPS_UPNP
453 static void wps_registrar_free_pending_m2(struct wps_context *wps)
455 struct upnp_pending_message *p, *p2, *prev = NULL;
458 if (p->type == WPS_M2 || p->type == WPS_M2D) {
460 wps->upnp_msgs = p->next;
462 prev->next = p->next;
463 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
466 wpabuf_free(p2->msg);
474 #endif /* CONFIG_WPS_UPNP */
477 static int wps_build_ap_setup_locked(struct wps_context *wps,
480 if (wps->ap_setup_locked && wps->ap_setup_locked != 2) {
481 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
482 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
483 wpabuf_put_be16(msg, 1);
484 wpabuf_put_u8(msg, 1);
490 static int wps_build_selected_registrar(struct wps_registrar *reg,
493 if (!reg->sel_reg_union)
495 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
496 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
497 wpabuf_put_be16(msg, 1);
498 wpabuf_put_u8(msg, 1);
503 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
506 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
507 if (!reg->sel_reg_union)
509 if (reg->sel_reg_dev_password_id_override >= 0)
510 id = reg->sel_reg_dev_password_id_override;
511 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
512 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
513 wpabuf_put_be16(msg, 2);
514 wpabuf_put_be16(msg, id);
519 static int wps_build_sel_pbc_reg_uuid_e(struct wps_registrar *reg,
522 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
523 if (!reg->sel_reg_union)
525 if (reg->sel_reg_dev_password_id_override >= 0)
526 id = reg->sel_reg_dev_password_id_override;
527 if (id != DEV_PW_PUSHBUTTON || !reg->dualband)
529 return wps_build_uuid_e(msg, reg->wps->uuid);
533 static void wps_set_pushbutton(u16 *methods, u16 conf_methods)
535 *methods |= WPS_CONFIG_PUSHBUTTON;
537 if ((conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON) ==
538 WPS_CONFIG_VIRT_PUSHBUTTON)
539 *methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
540 if ((conf_methods & WPS_CONFIG_PHY_PUSHBUTTON) ==
541 WPS_CONFIG_PHY_PUSHBUTTON)
542 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
543 if ((*methods & WPS_CONFIG_VIRT_PUSHBUTTON) !=
544 WPS_CONFIG_VIRT_PUSHBUTTON &&
545 (*methods & WPS_CONFIG_PHY_PUSHBUTTON) !=
546 WPS_CONFIG_PHY_PUSHBUTTON) {
548 * Required to include virtual/physical flag, but we were not
549 * configured with push button type, so have to default to one
552 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
554 #endif /* CONFIG_WPS2 */
558 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
562 if (!reg->sel_reg_union)
564 methods = reg->wps->config_methods;
565 methods &= ~WPS_CONFIG_PUSHBUTTON;
567 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
568 WPS_CONFIG_PHY_PUSHBUTTON);
569 #endif /* CONFIG_WPS2 */
571 wps_set_pushbutton(&methods, reg->wps->config_methods);
572 if (reg->sel_reg_config_methods_override >= 0)
573 methods = reg->sel_reg_config_methods_override;
574 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
576 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
577 wpabuf_put_be16(msg, 2);
578 wpabuf_put_be16(msg, methods);
583 static int wps_build_probe_config_methods(struct wps_registrar *reg,
588 * These are the methods that the AP supports as an Enrollee for adding
589 * external Registrars.
591 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
593 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
594 WPS_CONFIG_PHY_PUSHBUTTON);
595 #endif /* CONFIG_WPS2 */
596 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
597 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
598 wpabuf_put_be16(msg, 2);
599 wpabuf_put_be16(msg, methods);
604 static int wps_build_config_methods_r(struct wps_registrar *reg,
607 return wps_build_config_methods(msg, reg->wps->config_methods);
611 const u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count)
616 while (*count < WPS_MAX_AUTHORIZED_MACS) {
617 if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
621 #endif /* CONFIG_WPS2 */
623 return (const u8 *) reg->authorized_macs_union;
628 * wps_registrar_init - Initialize WPS Registrar data
629 * @wps: Pointer to longterm WPS context
630 * @cfg: Registrar configuration
631 * Returns: Pointer to allocated Registrar data or %NULL on failure
633 * This function is used to initialize WPS Registrar functionality. It can be
634 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
635 * runs (e.g., when run as an internal Registrar in an AP). Caller is
636 * responsible for freeing the returned data with wps_registrar_deinit() when
637 * Registrar functionality is not needed anymore.
639 struct wps_registrar *
640 wps_registrar_init(struct wps_context *wps,
641 const struct wps_registrar_config *cfg)
643 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
647 dl_list_init(®->pins);
648 dl_list_init(®->nfc_pw_tokens);
650 reg->new_psk_cb = cfg->new_psk_cb;
651 reg->set_ie_cb = cfg->set_ie_cb;
652 reg->pin_needed_cb = cfg->pin_needed_cb;
653 reg->reg_success_cb = cfg->reg_success_cb;
654 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
655 reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
656 reg->cb_ctx = cfg->cb_ctx;
657 reg->skip_cred_build = cfg->skip_cred_build;
658 if (cfg->extra_cred) {
659 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
660 cfg->extra_cred_len);
661 if (reg->extra_cred == NULL) {
666 reg->disable_auto_conf = cfg->disable_auto_conf;
667 reg->sel_reg_dev_password_id_override = -1;
668 reg->sel_reg_config_methods_override = -1;
669 reg->static_wep_only = cfg->static_wep_only;
670 reg->dualband = cfg->dualband;
671 reg->force_per_enrollee_psk = cfg->force_per_enrollee_psk;
673 if (wps_set_ie(reg)) {
674 wps_registrar_deinit(reg);
683 * wps_registrar_deinit - Deinitialize WPS Registrar data
684 * @reg: Registrar data from wps_registrar_init()
686 void wps_registrar_deinit(struct wps_registrar *reg)
690 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
691 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
692 wps_free_pins(®->pins);
693 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, 0);
694 wps_free_pbc_sessions(reg->pbc_sessions);
695 wpabuf_free(reg->extra_cred);
696 wps_free_devices(reg->devices);
701 static void wps_registrar_invalidate_unused(struct wps_registrar *reg)
703 struct wps_uuid_pin *pin;
705 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
706 if (pin->wildcard_uuid == 1 && !(pin->flags & PIN_LOCKED)) {
707 wpa_printf(MSG_DEBUG, "WPS: Invalidate previously "
708 "configured wildcard PIN");
709 wps_registrar_remove_pin(reg, pin);
717 * wps_registrar_add_pin - Configure a new PIN for Registrar
718 * @reg: Registrar data from wps_registrar_init()
719 * @addr: Enrollee MAC address or %NULL if not known
720 * @uuid: UUID-E or %NULL for wildcard (any UUID)
721 * @pin: PIN (Device Password)
722 * @pin_len: Length of pin in octets
723 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
724 * Returns: 0 on success, -1 on failure
726 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
727 const u8 *uuid, const u8 *pin, size_t pin_len,
730 struct wps_uuid_pin *p;
732 p = os_zalloc(sizeof(*p));
736 os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
738 p->wildcard_uuid = 1;
740 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
741 p->pin = os_malloc(pin_len);
742 if (p->pin == NULL) {
746 os_memcpy(p->pin, pin, pin_len);
747 p->pin_len = pin_len;
750 p->flags |= PIN_EXPIRES;
751 os_get_time(&p->expiration);
752 p->expiration.sec += timeout;
755 if (p->wildcard_uuid)
756 wps_registrar_invalidate_unused(reg);
758 dl_list_add(®->pins, &p->list);
760 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
762 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
763 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
764 reg->selected_registrar = 1;
767 wps_registrar_add_authorized_mac(reg, addr);
769 wps_registrar_add_authorized_mac(
770 reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
771 wps_registrar_selected_registrar_changed(reg, 0);
772 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
773 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
774 wps_registrar_set_selected_timeout,
781 static void wps_registrar_remove_pin(struct wps_registrar *reg,
782 struct wps_uuid_pin *pin)
785 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
787 if (is_zero_ether_addr(pin->enrollee_addr))
790 addr = pin->enrollee_addr;
791 wps_registrar_remove_authorized_mac(reg, addr);
793 wps_registrar_selected_registrar_changed(reg, 0);
797 static void wps_registrar_expire_pins(struct wps_registrar *reg)
799 struct wps_uuid_pin *pin, *prev;
803 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
805 if ((pin->flags & PIN_EXPIRES) &&
806 os_time_before(&pin->expiration, &now)) {
807 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
808 pin->uuid, WPS_UUID_LEN);
809 wps_registrar_remove_pin(reg, pin);
816 * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
817 * @reg: Registrar data from wps_registrar_init()
818 * @dev_pw: PIN to search for or %NULL to match any
819 * @dev_pw_len: Length of dev_pw in octets
820 * Returns: 0 on success, -1 if not wildcard PIN is enabled
822 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg,
826 struct wps_uuid_pin *pin, *prev;
828 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
830 if (dev_pw && pin->pin &&
831 (dev_pw_len != pin->pin_len ||
832 os_memcmp(dev_pw, pin->pin, dev_pw_len) != 0))
833 continue; /* different PIN */
834 if (pin->wildcard_uuid) {
835 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
836 pin->uuid, WPS_UUID_LEN);
837 wps_registrar_remove_pin(reg, pin);
847 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
848 * @reg: Registrar data from wps_registrar_init()
850 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
852 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
854 struct wps_uuid_pin *pin, *prev;
856 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
858 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
859 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
860 pin->uuid, WPS_UUID_LEN);
861 wps_registrar_remove_pin(reg, pin);
870 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
871 const u8 *uuid, size_t *pin_len)
873 struct wps_uuid_pin *pin, *found = NULL;
875 wps_registrar_expire_pins(reg);
877 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
878 if (!pin->wildcard_uuid &&
879 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
886 /* Check for wildcard UUIDs since none of the UUID-specific
888 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
889 if (pin->wildcard_uuid == 1 ||
890 pin->wildcard_uuid == 2) {
891 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
892 "PIN. Assigned it for this UUID-E");
893 pin->wildcard_uuid++;
894 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
905 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
906 * that could otherwise avoid PIN invalidations.
908 if (found->flags & PIN_LOCKED) {
909 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
910 "allow concurrent re-use");
913 *pin_len = found->pin_len;
914 found->flags |= PIN_LOCKED;
920 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
921 * @reg: Registrar data from wps_registrar_init()
923 * Returns: 0 on success, -1 on failure
925 * PINs are locked to enforce only one concurrent use. This function unlocks a
926 * PIN to allow it to be used again. If the specified PIN was configured using
927 * a wildcard UUID, it will be removed instead of allowing multiple uses.
929 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
931 struct wps_uuid_pin *pin;
933 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
934 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
935 if (pin->wildcard_uuid == 3) {
936 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
938 return wps_registrar_invalidate_pin(reg, uuid);
940 pin->flags &= ~PIN_LOCKED;
949 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
951 reg->selected_registrar = 0;
953 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
954 wps_registrar_remove_authorized_mac(reg,
955 (u8 *) "\xff\xff\xff\xff\xff\xff");
956 wps_registrar_selected_registrar_changed(reg, 0);
960 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
962 struct wps_registrar *reg = eloop_ctx;
964 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
965 wps_pbc_timeout_event(reg->wps);
966 wps_registrar_stop_pbc(reg);
971 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
972 * @reg: Registrar data from wps_registrar_init()
973 * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
974 * indicates no such filtering
975 * Returns: 0 on success, -1 on failure, -2 on session overlap
977 * This function is called on an AP when a push button is pushed to activate
978 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
979 * or when a PBC registration is completed. If more than one Enrollee in active
980 * PBC mode has been detected during the monitor time (previous 2 minutes), the
981 * PBC mode is not activated and -2 is returned to indicate session overlap.
982 * This is skipped if a specific Enrollee is selected.
984 int wps_registrar_button_pushed(struct wps_registrar *reg,
985 const u8 *p2p_dev_addr)
987 if (p2p_dev_addr == NULL &&
988 wps_registrar_pbc_overlap(reg, NULL, NULL)) {
989 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
991 wps_pbc_overlap_event(reg->wps);
994 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
995 reg->force_pbc_overlap = 0;
996 reg->selected_registrar = 1;
999 os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
1001 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
1002 wps_registrar_add_authorized_mac(reg,
1003 (u8 *) "\xff\xff\xff\xff\xff\xff");
1004 wps_registrar_selected_registrar_changed(reg, 0);
1006 wps_pbc_active_event(reg->wps);
1007 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1008 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1009 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
1015 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
1017 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
1018 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1019 wps_registrar_stop_pbc(reg);
1020 wps_pbc_disable_event(reg->wps);
1024 static void wps_registrar_pin_completed(struct wps_registrar *reg)
1026 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
1027 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1028 reg->selected_registrar = 0;
1029 wps_registrar_selected_registrar_changed(reg, 0);
1033 void wps_registrar_complete(struct wps_registrar *registrar, const u8 *uuid_e,
1034 const u8 *dev_pw, size_t dev_pw_len)
1036 if (registrar->pbc) {
1037 wps_registrar_remove_pbc_session(registrar,
1039 wps_registrar_pbc_completed(registrar);
1040 os_get_time(®istrar->pbc_ignore_start);
1041 os_memcpy(registrar->pbc_ignore_uuid, uuid_e, WPS_UUID_LEN);
1043 wps_registrar_pin_completed(registrar);
1047 wps_registrar_invalidate_wildcard_pin(registrar, dev_pw,
1049 wpa_hexdump_key(MSG_DEBUG, "WPS: Invalidated wildcard PIN",
1050 dev_pw, dev_pw_len);
1055 int wps_registrar_wps_cancel(struct wps_registrar *reg)
1058 wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
1059 wps_registrar_pbc_timeout(reg, NULL);
1060 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1062 } else if (reg->selected_registrar) {
1064 wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
1065 wps_registrar_pin_completed(reg);
1066 wps_registrar_invalidate_wildcard_pin(reg, NULL, 0);
1074 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
1075 * @reg: Registrar data from wps_registrar_init()
1076 * @addr: MAC address of the Probe Request sender
1077 * @wps_data: WPS IE contents
1079 * This function is called on an AP when a Probe Request with WPS IE is
1080 * received. This is used to track PBC mode use and to detect possible overlap
1081 * situation with other WPS APs.
1083 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
1084 const struct wpabuf *wps_data,
1087 struct wps_parse_attr attr;
1090 wpa_hexdump_buf(MSG_MSGDUMP,
1091 "WPS: Probe Request with WPS data received",
1094 if (wps_parse_msg(wps_data, &attr) < 0)
1097 if (attr.config_methods == NULL) {
1098 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
1103 if (attr.dev_password_id == NULL) {
1104 wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
1105 "in Probe Request");
1109 if (reg->enrollee_seen_cb && attr.uuid_e &&
1110 attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
1111 char *dev_name = NULL;
1112 if (attr.dev_name) {
1113 dev_name = os_zalloc(attr.dev_name_len + 1);
1115 os_memcpy(dev_name, attr.dev_name,
1119 reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
1120 attr.primary_dev_type,
1121 WPA_GET_BE16(attr.config_methods),
1122 WPA_GET_BE16(attr.dev_password_id),
1123 *attr.request_type, dev_name);
1127 if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
1128 return; /* Not PBC */
1130 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
1131 MACSTR, MAC2STR(addr));
1132 if (attr.uuid_e == NULL) {
1133 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
1137 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E from Probe Request", attr.uuid_e,
1140 #ifdef WPS_WORKAROUNDS
1141 if (reg->pbc_ignore_start.sec &&
1142 os_memcmp(attr.uuid_e, reg->pbc_ignore_uuid, WPS_UUID_LEN) == 0) {
1143 struct os_time now, dur;
1145 os_time_sub(&now, ®->pbc_ignore_start, &dur);
1146 if (dur.sec >= 0 && dur.sec < 5) {
1147 wpa_printf(MSG_DEBUG, "WPS: Ignore PBC activation "
1148 "based on Probe Request from the Enrollee "
1149 "that just completed PBC provisioning");
1152 reg->pbc_ignore_start.sec = 0;
1154 #endif /* WPS_WORKAROUNDS */
1157 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
1158 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
1159 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
1160 reg->force_pbc_overlap = 1;
1161 wps_pbc_overlap_event(reg->wps);
1166 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
1167 const u8 *p2p_dev_addr, const u8 *psk, size_t psk_len)
1169 if (reg->new_psk_cb == NULL)
1172 return reg->new_psk_cb(reg->cb_ctx, mac_addr, p2p_dev_addr, psk,
1177 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
1178 const struct wps_device_data *dev)
1180 if (reg->pin_needed_cb == NULL)
1183 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
1187 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
1188 const u8 *uuid_e, const u8 *dev_pw,
1191 if (reg->reg_success_cb == NULL)
1194 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e, dev_pw, dev_pw_len);
1198 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
1199 struct wpabuf *probe_resp_ie)
1201 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
1205 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
1208 if (reg->set_sel_reg_cb == NULL)
1211 if (reg->selected_registrar) {
1212 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
1214 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
1215 WPS_CONFIG_PHY_PUSHBUTTON);
1216 #endif /* CONFIG_WPS2 */
1218 wps_set_pushbutton(&methods, reg->wps->config_methods);
1221 wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1222 "config_methods=0x%x pbc=%d methods=0x%x",
1223 reg->selected_registrar, reg->wps->config_methods,
1226 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
1227 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
1232 static int wps_set_ie(struct wps_registrar *reg)
1234 struct wpabuf *beacon;
1235 struct wpabuf *probe;
1236 const u8 *auth_macs;
1238 size_t vendor_len = 0;
1241 if (reg->set_ie_cb == NULL)
1244 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
1245 if (reg->wps->dev.vendor_ext[i]) {
1246 vendor_len += 2 + 2;
1247 vendor_len += wpabuf_len(reg->wps->dev.vendor_ext[i]);
1251 beacon = wpabuf_alloc(400 + vendor_len);
1254 probe = wpabuf_alloc(500 + vendor_len);
1255 if (probe == NULL) {
1256 wpabuf_free(beacon);
1260 auth_macs = wps_authorized_macs(reg, &count);
1262 wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
1264 if (wps_build_version(beacon) ||
1265 wps_build_wps_state(reg->wps, beacon) ||
1266 wps_build_ap_setup_locked(reg->wps, beacon) ||
1267 wps_build_selected_registrar(reg, beacon) ||
1268 wps_build_sel_reg_dev_password_id(reg, beacon) ||
1269 wps_build_sel_reg_config_methods(reg, beacon) ||
1270 wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
1271 (reg->dualband && wps_build_rf_bands(®->wps->dev, beacon, 0)) ||
1272 wps_build_wfa_ext(beacon, 0, auth_macs, count) ||
1273 wps_build_vendor_ext(®->wps->dev, beacon)) {
1274 wpabuf_free(beacon);
1280 if (wps_build_dev_name(®->wps->dev, beacon) ||
1281 wps_build_primary_dev_type(®->wps->dev, beacon)) {
1282 wpabuf_free(beacon);
1286 #endif /* CONFIG_P2P */
1288 wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
1290 if (wps_build_version(probe) ||
1291 wps_build_wps_state(reg->wps, probe) ||
1292 wps_build_ap_setup_locked(reg->wps, probe) ||
1293 wps_build_selected_registrar(reg, probe) ||
1294 wps_build_sel_reg_dev_password_id(reg, probe) ||
1295 wps_build_sel_reg_config_methods(reg, probe) ||
1296 wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
1297 WPS_RESP_REGISTRAR) ||
1298 wps_build_uuid_e(probe, reg->wps->uuid) ||
1299 wps_build_device_attrs(®->wps->dev, probe) ||
1300 wps_build_probe_config_methods(reg, probe) ||
1301 (reg->dualband && wps_build_rf_bands(®->wps->dev, probe, 0)) ||
1302 wps_build_wfa_ext(probe, 0, auth_macs, count) ||
1303 wps_build_vendor_ext(®->wps->dev, probe)) {
1304 wpabuf_free(beacon);
1309 beacon = wps_ie_encapsulate(beacon);
1310 probe = wps_ie_encapsulate(probe);
1312 if (!beacon || !probe) {
1313 wpabuf_free(beacon);
1318 if (reg->static_wep_only) {
1320 * Windows XP and Vista clients can get confused about
1321 * EAP-Identity/Request when they probe the network with
1322 * EAPOL-Start. In such a case, they may assume the network is
1323 * using IEEE 802.1X and prompt user for a certificate while
1324 * the correct (non-WPS) behavior would be to ask for the
1325 * static WEP key. As a workaround, use Microsoft Provisioning
1326 * IE to advertise that legacy 802.1X is not supported.
1328 const u8 ms_wps[7] = {
1329 WLAN_EID_VENDOR_SPECIFIC, 5,
1330 /* Microsoft Provisioning IE (00:50:f2:5) */
1331 0x00, 0x50, 0xf2, 5,
1332 0x00 /* no legacy 802.1X or MS WPS */
1334 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
1335 "into Beacon/Probe Response frames");
1336 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
1337 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
1340 return wps_cb_set_ie(reg, beacon, probe);
1344 static int wps_get_dev_password(struct wps_data *wps)
1349 os_free(wps->dev_password);
1350 wps->dev_password = NULL;
1353 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
1354 pin = (const u8 *) "00000000";
1356 #ifdef CONFIG_WPS_NFC
1357 } else if (wps->nfc_pw_token) {
1358 wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from NFC "
1360 pin = wps->nfc_pw_token->dev_pw;
1361 pin_len = wps->nfc_pw_token->dev_pw_len;
1362 #endif /* CONFIG_WPS_NFC */
1364 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1366 if (pin && wps->dev_pw_id >= 0x10) {
1367 wpa_printf(MSG_DEBUG, "WPS: No match for OOB Device "
1368 "Password ID, but PIN found");
1370 * See whether Enrollee is willing to use PIN instead.
1372 wps->dev_pw_id = DEV_PW_DEFAULT;
1376 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1377 "the Enrollee (context %p registrar %p)",
1378 wps->wps, wps->wps->registrar);
1379 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1384 wps->dev_password = os_malloc(pin_len);
1385 if (wps->dev_password == NULL)
1387 os_memcpy(wps->dev_password, pin, pin_len);
1388 wps->dev_password_len = pin_len;
1394 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1396 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1397 wpabuf_put_be16(msg, ATTR_UUID_R);
1398 wpabuf_put_be16(msg, WPS_UUID_LEN);
1399 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1404 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1410 if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1412 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1413 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1414 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1416 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1417 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1418 "R-Hash derivation");
1422 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1423 wpabuf_put_be16(msg, ATTR_R_HASH1);
1424 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1425 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1426 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1427 addr[0] = wps->snonce;
1428 len[0] = WPS_SECRET_NONCE_LEN;
1429 addr[1] = wps->psk1;
1430 len[1] = WPS_PSK_LEN;
1431 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1432 len[2] = wpabuf_len(wps->dh_pubkey_e);
1433 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1434 len[3] = wpabuf_len(wps->dh_pubkey_r);
1435 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1436 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1438 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1439 wpabuf_put_be16(msg, ATTR_R_HASH2);
1440 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1441 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1442 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1443 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1444 addr[1] = wps->psk2;
1445 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1446 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1452 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1454 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1455 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1456 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1457 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1462 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1464 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1465 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1466 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1467 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1468 WPS_SECRET_NONCE_LEN);
1473 static int wps_build_cred_network_idx(struct wpabuf *msg,
1474 const struct wps_credential *cred)
1476 wpa_printf(MSG_DEBUG, "WPS: * Network Index (1)");
1477 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1478 wpabuf_put_be16(msg, 1);
1479 wpabuf_put_u8(msg, 1);
1484 static int wps_build_cred_ssid(struct wpabuf *msg,
1485 const struct wps_credential *cred)
1487 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1488 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID for Credential",
1489 cred->ssid, cred->ssid_len);
1490 wpabuf_put_be16(msg, ATTR_SSID);
1491 wpabuf_put_be16(msg, cred->ssid_len);
1492 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1497 static int wps_build_cred_auth_type(struct wpabuf *msg,
1498 const struct wps_credential *cred)
1500 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1502 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1503 wpabuf_put_be16(msg, 2);
1504 wpabuf_put_be16(msg, cred->auth_type);
1509 static int wps_build_cred_encr_type(struct wpabuf *msg,
1510 const struct wps_credential *cred)
1512 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1514 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1515 wpabuf_put_be16(msg, 2);
1516 wpabuf_put_be16(msg, cred->encr_type);
1521 static int wps_build_cred_network_key(struct wpabuf *msg,
1522 const struct wps_credential *cred)
1524 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1525 (int) cred->key_len);
1526 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
1527 cred->key, cred->key_len);
1528 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1529 wpabuf_put_be16(msg, cred->key_len);
1530 wpabuf_put_data(msg, cred->key, cred->key_len);
1535 static int wps_build_credential(struct wpabuf *msg,
1536 const struct wps_credential *cred)
1538 if (wps_build_cred_network_idx(msg, cred) ||
1539 wps_build_cred_ssid(msg, cred) ||
1540 wps_build_cred_auth_type(msg, cred) ||
1541 wps_build_cred_encr_type(msg, cred) ||
1542 wps_build_cred_network_key(msg, cred) ||
1543 wps_build_mac_addr(msg, cred->mac_addr))
1549 int wps_build_credential_wrap(struct wpabuf *msg,
1550 const struct wps_credential *cred)
1552 struct wpabuf *wbuf;
1553 wbuf = wpabuf_alloc(200);
1556 if (wps_build_credential(wbuf, cred)) {
1560 wpabuf_put_be16(msg, ATTR_CRED);
1561 wpabuf_put_be16(msg, wpabuf_len(wbuf));
1562 wpabuf_put_buf(msg, wbuf);
1568 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1570 struct wpabuf *cred;
1572 if (wps->wps->registrar->skip_cred_build)
1573 goto skip_cred_build;
1575 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1576 if (wps->use_cred) {
1577 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1580 os_memset(&wps->cred, 0, sizeof(wps->cred));
1582 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1583 wps->cred.ssid_len = wps->wps->ssid_len;
1585 /* Select the best authentication and encryption type */
1586 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1587 wps->auth_type = WPS_AUTH_WPA2PSK;
1588 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1589 wps->auth_type = WPS_AUTH_WPAPSK;
1590 else if (wps->auth_type & WPS_AUTH_OPEN)
1591 wps->auth_type = WPS_AUTH_OPEN;
1592 else if (wps->auth_type & WPS_AUTH_SHARED)
1593 wps->auth_type = WPS_AUTH_SHARED;
1595 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1599 wps->cred.auth_type = wps->auth_type;
1601 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1602 wps->auth_type == WPS_AUTH_WPAPSK) {
1603 if (wps->encr_type & WPS_ENCR_AES)
1604 wps->encr_type = WPS_ENCR_AES;
1605 else if (wps->encr_type & WPS_ENCR_TKIP)
1606 wps->encr_type = WPS_ENCR_TKIP;
1608 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1609 "type for WPA/WPA2");
1613 if (wps->encr_type & WPS_ENCR_WEP)
1614 wps->encr_type = WPS_ENCR_WEP;
1615 else if (wps->encr_type & WPS_ENCR_NONE)
1616 wps->encr_type = WPS_ENCR_NONE;
1618 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1619 "type for non-WPA/WPA2 mode");
1623 wps->cred.encr_type = wps->encr_type;
1625 * Set MAC address in the Credential to be the Enrollee's MAC address
1627 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1629 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1630 !wps->wps->registrar->disable_auto_conf) {
1632 /* Generate a random passphrase */
1633 if (random_get_bytes(r, sizeof(r)) < 0)
1635 os_free(wps->new_psk);
1636 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1637 if (wps->new_psk == NULL)
1639 wps->new_psk_len--; /* remove newline */
1640 while (wps->new_psk_len &&
1641 wps->new_psk[wps->new_psk_len - 1] == '=')
1643 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1644 wps->new_psk, wps->new_psk_len);
1645 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1646 wps->cred.key_len = wps->new_psk_len;
1647 } else if (!wps->wps->registrar->force_per_enrollee_psk &&
1648 wps->use_psk_key && wps->wps->psk_set) {
1650 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1651 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1652 os_memcpy(wps->cred.key, hex, 32 * 2);
1653 wps->cred.key_len = 32 * 2;
1654 } else if (!wps->wps->registrar->force_per_enrollee_psk &&
1655 wps->wps->network_key) {
1656 os_memcpy(wps->cred.key, wps->wps->network_key,
1657 wps->wps->network_key_len);
1658 wps->cred.key_len = wps->wps->network_key_len;
1659 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1661 /* Generate a random per-device PSK */
1662 os_free(wps->new_psk);
1663 wps->new_psk_len = 32;
1664 wps->new_psk = os_malloc(wps->new_psk_len);
1665 if (wps->new_psk == NULL)
1667 if (random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
1668 os_free(wps->new_psk);
1669 wps->new_psk = NULL;
1672 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1673 wps->new_psk, wps->new_psk_len);
1674 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1676 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1677 wps->cred.key_len = wps->new_psk_len * 2;
1681 #ifdef CONFIG_WPS_TESTING
1682 if (wps_testing_dummy_cred)
1683 cred = wpabuf_alloc(200);
1687 struct wps_credential dummy;
1688 wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
1689 os_memset(&dummy, 0, sizeof(dummy));
1690 os_memcpy(dummy.ssid, "dummy", 5);
1692 dummy.auth_type = WPS_AUTH_WPA2PSK;
1693 dummy.encr_type = WPS_ENCR_AES;
1694 os_memcpy(dummy.key, "dummy psk", 9);
1696 os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
1697 wps_build_credential(cred, &dummy);
1698 wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
1700 wpabuf_put_be16(msg, ATTR_CRED);
1701 wpabuf_put_be16(msg, wpabuf_len(cred));
1702 wpabuf_put_buf(msg, cred);
1706 #endif /* CONFIG_WPS_TESTING */
1708 cred = wpabuf_alloc(200);
1712 if (wps_build_credential(cred, &wps->cred)) {
1717 wpabuf_put_be16(msg, ATTR_CRED);
1718 wpabuf_put_be16(msg, wpabuf_len(cred));
1719 wpabuf_put_buf(msg, cred);
1723 if (wps->wps->registrar->extra_cred) {
1724 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1725 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1732 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1734 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1736 if (wps_build_credential(msg, &wps->cred))
1743 static struct wpabuf * wps_build_ap_cred(struct wps_data *wps)
1745 struct wpabuf *msg, *plain;
1747 msg = wpabuf_alloc(1000);
1751 plain = wpabuf_alloc(200);
1752 if (plain == NULL) {
1757 if (wps_build_ap_settings(wps, plain)) {
1763 wpabuf_put_be16(msg, ATTR_CRED);
1764 wpabuf_put_be16(msg, wpabuf_len(plain));
1765 wpabuf_put_buf(msg, plain);
1772 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1776 if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
1778 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1779 wps->nonce_r, WPS_NONCE_LEN);
1780 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1782 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1783 msg = wpabuf_alloc(1000);
1787 if (wps_build_version(msg) ||
1788 wps_build_msg_type(msg, WPS_M2) ||
1789 wps_build_enrollee_nonce(wps, msg) ||
1790 wps_build_registrar_nonce(wps, msg) ||
1791 wps_build_uuid_r(wps, msg) ||
1792 wps_build_public_key(wps, msg) ||
1793 wps_derive_keys(wps) ||
1794 wps_build_auth_type_flags(wps, msg) ||
1795 wps_build_encr_type_flags(wps, msg) ||
1796 wps_build_conn_type_flags(wps, msg) ||
1797 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1798 wps_build_device_attrs(&wps->wps->dev, msg) ||
1799 wps_build_rf_bands(&wps->wps->dev, msg,
1800 wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
1801 wps_build_assoc_state(wps, msg) ||
1802 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1803 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1804 wps_build_os_version(&wps->wps->dev, msg) ||
1805 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1806 wps_build_authenticator(wps, msg)) {
1812 wps->state = RECV_M3;
1817 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1820 u16 err = wps->config_error;
1822 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1823 msg = wpabuf_alloc(1000);
1827 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1828 err == WPS_CFG_NO_ERROR)
1829 err = WPS_CFG_SETUP_LOCKED;
1831 if (wps_build_version(msg) ||
1832 wps_build_msg_type(msg, WPS_M2D) ||
1833 wps_build_enrollee_nonce(wps, msg) ||
1834 wps_build_registrar_nonce(wps, msg) ||
1835 wps_build_uuid_r(wps, msg) ||
1836 wps_build_auth_type_flags(wps, msg) ||
1837 wps_build_encr_type_flags(wps, msg) ||
1838 wps_build_conn_type_flags(wps, msg) ||
1839 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1840 wps_build_device_attrs(&wps->wps->dev, msg) ||
1841 wps_build_rf_bands(&wps->wps->dev, msg,
1842 wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
1843 wps_build_assoc_state(wps, msg) ||
1844 wps_build_config_error(msg, err) ||
1845 wps_build_os_version(&wps->wps->dev, msg) ||
1846 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1851 wps->state = RECV_M2D_ACK;
1856 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1858 struct wpabuf *msg, *plain;
1860 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1862 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1864 plain = wpabuf_alloc(200);
1868 msg = wpabuf_alloc(1000);
1874 if (wps_build_version(msg) ||
1875 wps_build_msg_type(msg, WPS_M4) ||
1876 wps_build_enrollee_nonce(wps, msg) ||
1877 wps_build_r_hash(wps, msg) ||
1878 wps_build_r_snonce1(wps, plain) ||
1879 wps_build_key_wrap_auth(wps, plain) ||
1880 wps_build_encr_settings(wps, msg, plain) ||
1881 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1882 wps_build_authenticator(wps, msg)) {
1889 wps->state = RECV_M5;
1894 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1896 struct wpabuf *msg, *plain;
1898 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1900 plain = wpabuf_alloc(200);
1904 msg = wpabuf_alloc(1000);
1910 if (wps_build_version(msg) ||
1911 wps_build_msg_type(msg, WPS_M6) ||
1912 wps_build_enrollee_nonce(wps, msg) ||
1913 wps_build_r_snonce2(wps, plain) ||
1914 wps_build_key_wrap_auth(wps, plain) ||
1915 wps_build_encr_settings(wps, msg, plain) ||
1916 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1917 wps_build_authenticator(wps, msg)) {
1924 wps->wps_pin_revealed = 1;
1925 wps->state = RECV_M7;
1930 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1932 struct wpabuf *msg, *plain;
1934 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1936 plain = wpabuf_alloc(500);
1940 msg = wpabuf_alloc(1000);
1946 if (wps_build_version(msg) ||
1947 wps_build_msg_type(msg, WPS_M8) ||
1948 wps_build_enrollee_nonce(wps, msg) ||
1949 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1950 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1951 wps_build_key_wrap_auth(wps, plain) ||
1952 wps_build_encr_settings(wps, msg, plain) ||
1953 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1954 wps_build_authenticator(wps, msg)) {
1961 wps->state = RECV_DONE;
1966 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1967 enum wsc_op_code *op_code)
1971 #ifdef CONFIG_WPS_UPNP
1972 if (!wps->int_reg && wps->wps->wps_upnp) {
1973 struct upnp_pending_message *p, *prev = NULL;
1974 if (wps->ext_reg > 1)
1975 wps_registrar_free_pending_m2(wps->wps);
1976 p = wps->wps->upnp_msgs;
1977 /* TODO: check pending message MAC address */
1978 while (p && p->next) {
1983 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1988 wps->wps->upnp_msgs = NULL;
1995 *op_code = WSC_NACK;
2002 if (wps->ext_reg == 0)
2008 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
2009 "pending message available");
2012 #endif /* CONFIG_WPS_UPNP */
2014 switch (wps->state) {
2016 if (wps_get_dev_password(wps) < 0)
2017 msg = wps_build_m2d(wps);
2019 msg = wps_build_m2(wps);
2023 msg = wps_build_m2d(wps);
2027 msg = wps_build_m4(wps);
2031 msg = wps_build_m6(wps);
2035 msg = wps_build_m8(wps);
2039 msg = wps_build_wsc_ack(wps);
2043 msg = wps_build_wsc_nack(wps);
2044 *op_code = WSC_NACK;
2047 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
2048 "a message", wps->state);
2053 if (*op_code == WSC_MSG && msg) {
2054 /* Save a copy of the last message for Authenticator derivation
2056 wpabuf_free(wps->last_msg);
2057 wps->last_msg = wpabuf_dup(msg);
2064 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
2066 if (e_nonce == NULL) {
2067 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
2071 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
2072 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
2073 wps->nonce_e, WPS_NONCE_LEN);
2079 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
2081 if (r_nonce == NULL) {
2082 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
2086 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
2087 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
2095 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
2097 if (uuid_e == NULL) {
2098 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
2102 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
2103 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
2109 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
2111 if (pw_id == NULL) {
2112 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
2116 wps->dev_pw_id = WPA_GET_BE16(pw_id);
2117 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
2123 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
2125 if (e_hash1 == NULL) {
2126 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
2130 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
2131 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
2137 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
2139 if (e_hash2 == NULL) {
2140 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
2144 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
2145 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
2151 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
2153 u8 hash[SHA256_MAC_LEN];
2157 if (e_snonce1 == NULL) {
2158 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
2162 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
2163 WPS_SECRET_NONCE_LEN);
2165 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
2166 addr[0] = e_snonce1;
2167 len[0] = WPS_SECRET_NONCE_LEN;
2168 addr[1] = wps->psk1;
2169 len[1] = WPS_PSK_LEN;
2170 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2171 len[2] = wpabuf_len(wps->dh_pubkey_e);
2172 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2173 len[3] = wpabuf_len(wps->dh_pubkey_r);
2174 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2176 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
2177 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
2178 "not match with the pre-committed value");
2179 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2180 wps_pwd_auth_fail_event(wps->wps, 0, 1, wps->mac_addr_e);
2184 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
2185 "half of the device password");
2191 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
2193 u8 hash[SHA256_MAC_LEN];
2197 if (e_snonce2 == NULL) {
2198 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
2202 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
2203 WPS_SECRET_NONCE_LEN);
2205 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
2206 addr[0] = e_snonce2;
2207 len[0] = WPS_SECRET_NONCE_LEN;
2208 addr[1] = wps->psk2;
2209 len[1] = WPS_PSK_LEN;
2210 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2211 len[2] = wpabuf_len(wps->dh_pubkey_e);
2212 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2213 len[3] = wpabuf_len(wps->dh_pubkey_r);
2214 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2216 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
2217 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
2218 "not match with the pre-committed value");
2219 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2220 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2221 wps_pwd_auth_fail_event(wps->wps, 0, 2, wps->mac_addr_e);
2225 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
2226 "half of the device password");
2227 wps->wps_pin_revealed = 0;
2228 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
2231 * In case wildcard PIN is used and WPS handshake succeeds in the first
2232 * attempt, wps_registrar_unlock_pin() would not free the PIN, so make
2233 * sure the PIN gets invalidated here.
2235 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2241 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
2243 if (mac_addr == NULL) {
2244 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
2248 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
2250 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
2251 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
2257 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
2260 if (pk == NULL || pk_len == 0) {
2261 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
2265 wpabuf_free(wps->dh_pubkey_e);
2266 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
2267 if (wps->dh_pubkey_e == NULL)
2274 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
2279 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
2284 auth_types = WPA_GET_BE16(auth);
2286 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
2288 wps->auth_type = wps->wps->auth_types & auth_types;
2289 if (wps->auth_type == 0) {
2290 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2291 "authentication types (own 0x%x Enrollee 0x%x)",
2292 wps->wps->auth_types, auth_types);
2293 #ifdef WPS_WORKAROUNDS
2295 * Some deployed implementations seem to advertise incorrect
2296 * information in this attribute. For example, Linksys WRT350N
2297 * seems to have a byteorder bug that breaks this negotiation.
2298 * In order to interoperate with existing implementations,
2299 * assume that the Enrollee supports everything we do.
2301 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2302 "does not advertise supported authentication types "
2304 wps->auth_type = wps->wps->auth_types;
2305 #else /* WPS_WORKAROUNDS */
2307 #endif /* WPS_WORKAROUNDS */
2314 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
2319 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
2324 encr_types = WPA_GET_BE16(encr);
2326 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
2328 wps->encr_type = wps->wps->encr_types & encr_types;
2329 if (wps->encr_type == 0) {
2330 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2331 "encryption types (own 0x%x Enrollee 0x%x)",
2332 wps->wps->encr_types, encr_types);
2333 #ifdef WPS_WORKAROUNDS
2335 * Some deployed implementations seem to advertise incorrect
2336 * information in this attribute. For example, Linksys WRT350N
2337 * seems to have a byteorder bug that breaks this negotiation.
2338 * In order to interoperate with existing implementations,
2339 * assume that the Enrollee supports everything we do.
2341 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2342 "does not advertise supported encryption types "
2344 wps->encr_type = wps->wps->encr_types;
2345 #else /* WPS_WORKAROUNDS */
2347 #endif /* WPS_WORKAROUNDS */
2354 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
2357 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
2362 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
2369 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
2373 if (methods == NULL) {
2374 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
2378 m = WPA_GET_BE16(methods);
2380 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
2381 "%s%s%s%s%s%s%s%s%s", m,
2382 m & WPS_CONFIG_USBA ? " [USBA]" : "",
2383 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
2384 m & WPS_CONFIG_LABEL ? " [Label]" : "",
2385 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
2386 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
2387 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
2388 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
2389 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
2390 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
2392 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
2394 * The Enrollee does not have a display so it is unlikely to be
2395 * able to show the passphrase to a user and as such, could
2396 * benefit from receiving PSK to reduce key derivation time.
2398 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2399 "Enrollee not supporting display");
2400 wps->use_psk_key = 1;
2407 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2409 if (state == NULL) {
2410 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2415 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2422 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2426 if (assoc == NULL) {
2427 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2431 a = WPA_GET_BE16(assoc);
2432 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2438 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2443 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2447 e = WPA_GET_BE16(err);
2448 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2454 static int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
2457 struct wps_registrar *reg = wps->wps->registrar;
2459 if (is_zero_ether_addr(reg->p2p_dev_addr))
2460 return 1; /* no filtering in use */
2462 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
2463 wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
2464 "filtering for PBC: expected " MACSTR " was "
2465 MACSTR " - indicate PBC session overlap",
2466 MAC2STR(reg->p2p_dev_addr),
2467 MAC2STR(wps->p2p_dev_addr));
2470 #endif /* CONFIG_P2P */
2475 static int wps_registrar_skip_overlap(struct wps_data *wps)
2478 struct wps_registrar *reg = wps->wps->registrar;
2480 if (is_zero_ether_addr(reg->p2p_dev_addr))
2481 return 0; /* no specific Enrollee selected */
2483 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) == 0) {
2484 wpa_printf(MSG_DEBUG, "WPS: Skip PBC overlap due to selected "
2488 #endif /* CONFIG_P2P */
2493 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2494 struct wps_parse_attr *attr)
2496 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2498 if (wps->state != RECV_M1) {
2499 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2500 "receiving M1", wps->state);
2504 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2505 wps_process_mac_addr(wps, attr->mac_addr) ||
2506 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2507 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2508 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2509 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2510 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2511 wps_process_config_methods(wps, attr->config_methods) ||
2512 wps_process_wps_state(wps, attr->wps_state) ||
2513 wps_process_device_attrs(&wps->peer_dev, attr) ||
2514 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2515 wps_process_assoc_state(wps, attr->assoc_state) ||
2516 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2517 wps_process_config_error(wps, attr->config_error) ||
2518 wps_process_os_version(&wps->peer_dev, attr->os_version))
2521 if (wps->dev_pw_id < 0x10 &&
2522 wps->dev_pw_id != DEV_PW_DEFAULT &&
2523 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2524 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2525 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2526 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2527 !wps->wps->registrar->pbc)) {
2528 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2530 wps->state = SEND_M2D;
2531 return WPS_CONTINUE;
2534 #ifdef CONFIG_WPS_NFC
2535 if (wps->dev_pw_id >= 0x10) {
2536 struct wps_nfc_pw_token *token;
2538 u8 hash[WPS_HASH_LEN];
2540 wpa_printf(MSG_DEBUG, "WPS: Searching for NFC token match for id=%d (ctx %p registrar %p)",
2541 wps->dev_pw_id, wps->wps, wps->wps->registrar);
2542 token = wps_get_nfc_pw_token(
2543 &wps->wps->registrar->nfc_pw_tokens, wps->dev_pw_id);
2545 wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
2547 dl_list_del(&token->list);
2548 wps->nfc_pw_token = token;
2550 addr[0] = attr->public_key;
2551 sha256_vector(1, addr, &attr->public_key_len, hash);
2552 if (os_memcmp(hash, wps->nfc_pw_token->pubkey_hash,
2553 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
2554 wpa_printf(MSG_ERROR, "WPS: Public Key hash "
2560 #endif /* CONFIG_WPS_NFC */
2562 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2563 if ((wps->wps->registrar->force_pbc_overlap ||
2564 wps_registrar_pbc_overlap(wps->wps->registrar,
2565 wps->mac_addr_e, wps->uuid_e) ||
2566 !wps_registrar_p2p_dev_addr_match(wps)) &&
2567 !wps_registrar_skip_overlap(wps)) {
2568 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2570 wps->state = SEND_M2D;
2571 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2572 wps_pbc_overlap_event(wps->wps);
2573 wps_fail_event(wps->wps, WPS_M1,
2574 WPS_CFG_MULTIPLE_PBC_DETECTED,
2575 WPS_EI_NO_ERROR, wps->mac_addr_e);
2576 wps->wps->registrar->force_pbc_overlap = 1;
2577 return WPS_CONTINUE;
2579 wps_registrar_add_pbc_session(wps->wps->registrar,
2580 wps->mac_addr_e, wps->uuid_e);
2584 #ifdef WPS_WORKAROUNDS
2586 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2587 * passphrase format. To avoid interop issues, force PSK format to be
2590 if (!wps->use_psk_key &&
2591 wps->peer_dev.manufacturer &&
2592 os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
2593 wps->peer_dev.model_name &&
2594 os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
2595 wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
2597 wps->use_psk_key = 1;
2599 #endif /* WPS_WORKAROUNDS */
2601 wps->state = SEND_M2;
2602 return WPS_CONTINUE;
2606 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2607 const struct wpabuf *msg,
2608 struct wps_parse_attr *attr)
2610 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2612 if (wps->state != RECV_M3) {
2613 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2614 "receiving M3", wps->state);
2615 wps->state = SEND_WSC_NACK;
2616 return WPS_CONTINUE;
2619 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2620 !wps_registrar_skip_overlap(wps)) {
2621 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2623 wps->state = SEND_WSC_NACK;
2624 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2625 return WPS_CONTINUE;
2628 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2629 wps_process_authenticator(wps, attr->authenticator, msg) ||
2630 wps_process_e_hash1(wps, attr->e_hash1) ||
2631 wps_process_e_hash2(wps, attr->e_hash2)) {
2632 wps->state = SEND_WSC_NACK;
2633 return WPS_CONTINUE;
2636 wps->state = SEND_M4;
2637 return WPS_CONTINUE;
2641 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2642 const struct wpabuf *msg,
2643 struct wps_parse_attr *attr)
2645 struct wpabuf *decrypted;
2646 struct wps_parse_attr eattr;
2648 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2650 if (wps->state != RECV_M5) {
2651 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2652 "receiving M5", wps->state);
2653 wps->state = SEND_WSC_NACK;
2654 return WPS_CONTINUE;
2657 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2658 !wps_registrar_skip_overlap(wps)) {
2659 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2661 wps->state = SEND_WSC_NACK;
2662 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2663 return WPS_CONTINUE;
2666 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2667 wps_process_authenticator(wps, attr->authenticator, msg)) {
2668 wps->state = SEND_WSC_NACK;
2669 return WPS_CONTINUE;
2672 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2673 attr->encr_settings_len);
2674 if (decrypted == NULL) {
2675 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2676 "Settings attribute");
2677 wps->state = SEND_WSC_NACK;
2678 return WPS_CONTINUE;
2681 if (wps_validate_m5_encr(decrypted, attr->version2 != NULL) < 0) {
2682 wpabuf_free(decrypted);
2683 wps->state = SEND_WSC_NACK;
2684 return WPS_CONTINUE;
2687 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2689 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2690 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2691 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2692 wpabuf_free(decrypted);
2693 wps->state = SEND_WSC_NACK;
2694 return WPS_CONTINUE;
2696 wpabuf_free(decrypted);
2698 wps->state = SEND_M6;
2699 return WPS_CONTINUE;
2703 static void wps_sta_cred_cb(struct wps_data *wps)
2706 * Update credential to only include a single authentication and
2707 * encryption type in case the AP configuration includes more than one
2710 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2711 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2712 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2713 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2714 if (wps->cred.encr_type & WPS_ENCR_AES)
2715 wps->cred.encr_type = WPS_ENCR_AES;
2716 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2717 wps->cred.encr_type = WPS_ENCR_TKIP;
2718 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2719 "AP configuration");
2720 if (wps->wps->cred_cb)
2721 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2725 static void wps_cred_update(struct wps_credential *dst,
2726 struct wps_credential *src)
2728 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2729 dst->ssid_len = src->ssid_len;
2730 dst->auth_type = src->auth_type;
2731 dst->encr_type = src->encr_type;
2732 dst->key_idx = src->key_idx;
2733 os_memcpy(dst->key, src->key, sizeof(dst->key));
2734 dst->key_len = src->key_len;
2738 static int wps_process_ap_settings_r(struct wps_data *wps,
2739 struct wps_parse_attr *attr)
2743 if (wps->wps->ap || wps->er)
2746 /* AP Settings Attributes in M7 when Enrollee is an AP */
2747 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2750 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2752 if (wps->new_ap_settings) {
2753 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2755 wps_cred_update(&wps->cred, wps->new_ap_settings);
2759 * Use the AP PIN only to receive the current AP settings, not
2760 * to reconfigure the AP.
2764 * Clear selected registrar here since we do not get to
2765 * WSC_Done in this protocol run.
2767 wps_registrar_pin_completed(wps->wps->registrar);
2769 msg = wps_build_ap_cred(wps);
2772 wps->cred.cred_attr = wpabuf_head(msg);
2773 wps->cred.cred_attr_len = wpabuf_len(msg);
2775 if (wps->ap_settings_cb) {
2776 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2781 wps_sta_cred_cb(wps);
2783 wps->cred.cred_attr = NULL;
2784 wps->cred.cred_attr_len = 0;
2792 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2793 const struct wpabuf *msg,
2794 struct wps_parse_attr *attr)
2796 struct wpabuf *decrypted;
2797 struct wps_parse_attr eattr;
2799 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2801 if (wps->state != RECV_M7) {
2802 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2803 "receiving M7", wps->state);
2804 wps->state = SEND_WSC_NACK;
2805 return WPS_CONTINUE;
2808 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2809 !wps_registrar_skip_overlap(wps)) {
2810 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2812 wps->state = SEND_WSC_NACK;
2813 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2814 return WPS_CONTINUE;
2817 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2818 wps_process_authenticator(wps, attr->authenticator, msg)) {
2819 wps->state = SEND_WSC_NACK;
2820 return WPS_CONTINUE;
2823 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2824 attr->encr_settings_len);
2825 if (decrypted == NULL) {
2826 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2827 "Settings attribute");
2828 wps->state = SEND_WSC_NACK;
2829 return WPS_CONTINUE;
2832 if (wps_validate_m7_encr(decrypted, wps->wps->ap || wps->er,
2833 attr->version2 != NULL) < 0) {
2834 wpabuf_free(decrypted);
2835 wps->state = SEND_WSC_NACK;
2836 return WPS_CONTINUE;
2839 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2841 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2842 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2843 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2844 wps_process_ap_settings_r(wps, &eattr)) {
2845 wpabuf_free(decrypted);
2846 wps->state = SEND_WSC_NACK;
2847 return WPS_CONTINUE;
2850 wpabuf_free(decrypted);
2852 wps->state = SEND_M8;
2853 return WPS_CONTINUE;
2857 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2858 const struct wpabuf *msg)
2860 struct wps_parse_attr attr;
2861 enum wps_process_res ret = WPS_CONTINUE;
2863 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2865 if (wps_parse_msg(msg, &attr) < 0)
2868 if (attr.msg_type == NULL) {
2869 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2870 wps->state = SEND_WSC_NACK;
2871 return WPS_CONTINUE;
2874 if (*attr.msg_type != WPS_M1 &&
2875 (attr.registrar_nonce == NULL ||
2876 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2877 WPS_NONCE_LEN) != 0)) {
2878 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2882 switch (*attr.msg_type) {
2884 if (wps_validate_m1(msg) < 0)
2886 #ifdef CONFIG_WPS_UPNP
2887 if (wps->wps->wps_upnp && attr.mac_addr) {
2888 /* Remove old pending messages when starting new run */
2889 wps_free_pending_msgs(wps->wps->upnp_msgs);
2890 wps->wps->upnp_msgs = NULL;
2892 upnp_wps_device_send_wlan_event(
2893 wps->wps->wps_upnp, attr.mac_addr,
2894 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2896 #endif /* CONFIG_WPS_UPNP */
2897 ret = wps_process_m1(wps, &attr);
2900 if (wps_validate_m3(msg) < 0)
2902 ret = wps_process_m3(wps, msg, &attr);
2903 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2904 wps_fail_event(wps->wps, WPS_M3, wps->config_error,
2905 wps->error_indication, wps->mac_addr_e);
2908 if (wps_validate_m5(msg) < 0)
2910 ret = wps_process_m5(wps, msg, &attr);
2911 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2912 wps_fail_event(wps->wps, WPS_M5, wps->config_error,
2913 wps->error_indication, wps->mac_addr_e);
2916 if (wps_validate_m7(msg) < 0)
2918 ret = wps_process_m7(wps, msg, &attr);
2919 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2920 wps_fail_event(wps->wps, WPS_M7, wps->config_error,
2921 wps->error_indication, wps->mac_addr_e);
2924 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2929 if (ret == WPS_CONTINUE) {
2930 /* Save a copy of the last message for Authenticator derivation
2932 wpabuf_free(wps->last_msg);
2933 wps->last_msg = wpabuf_dup(msg);
2940 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2941 const struct wpabuf *msg)
2943 struct wps_parse_attr attr;
2945 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2947 if (wps_parse_msg(msg, &attr) < 0)
2950 if (attr.msg_type == NULL) {
2951 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2955 if (*attr.msg_type != WPS_WSC_ACK) {
2956 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2961 #ifdef CONFIG_WPS_UPNP
2962 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2963 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2964 if (wps->wps->upnp_msgs)
2965 return WPS_CONTINUE;
2966 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2967 "external Registrar");
2970 #endif /* CONFIG_WPS_UPNP */
2972 if (attr.registrar_nonce == NULL ||
2973 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
2975 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2979 if (attr.enrollee_nonce == NULL ||
2980 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
2981 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2985 if (wps->state == RECV_M2D_ACK) {
2986 #ifdef CONFIG_WPS_UPNP
2987 if (wps->wps->wps_upnp &&
2988 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2989 if (wps->wps->upnp_msgs)
2990 return WPS_CONTINUE;
2991 if (wps->ext_reg == 0)
2993 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2994 "external Registrar");
2997 #endif /* CONFIG_WPS_UPNP */
2999 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
3000 "terminate negotiation");
3007 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
3008 const struct wpabuf *msg)
3010 struct wps_parse_attr attr;
3014 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
3016 old_state = wps->state;
3017 wps->state = SEND_WSC_NACK;
3019 if (wps_parse_msg(msg, &attr) < 0)
3022 if (attr.msg_type == NULL) {
3023 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3027 if (*attr.msg_type != WPS_WSC_NACK) {
3028 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3033 #ifdef CONFIG_WPS_UPNP
3034 if (wps->wps->wps_upnp && wps->ext_reg) {
3035 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3036 "Registrar terminated by the Enrollee");
3039 #endif /* CONFIG_WPS_UPNP */
3041 if (attr.registrar_nonce == NULL ||
3042 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3044 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3048 if (attr.enrollee_nonce == NULL ||
3049 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3050 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3054 if (attr.config_error == NULL) {
3055 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
3060 config_error = WPA_GET_BE16(attr.config_error);
3061 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
3062 "Configuration Error %d", config_error);
3064 switch (old_state) {
3066 wps_fail_event(wps->wps, WPS_M2, config_error,
3067 wps->error_indication, wps->mac_addr_e);
3070 wps_fail_event(wps->wps, WPS_M4, config_error,
3071 wps->error_indication, wps->mac_addr_e);
3074 wps_fail_event(wps->wps, WPS_M6, config_error,
3075 wps->error_indication, wps->mac_addr_e);
3078 wps_fail_event(wps->wps, WPS_M8, config_error,
3079 wps->error_indication, wps->mac_addr_e);
3089 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
3090 const struct wpabuf *msg)
3092 struct wps_parse_attr attr;
3094 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
3096 if (wps->state != RECV_DONE &&
3097 (!wps->wps->wps_upnp || !wps->ext_reg)) {
3098 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
3099 "receiving WSC_Done", wps->state);
3103 if (wps_parse_msg(msg, &attr) < 0)
3106 if (attr.msg_type == NULL) {
3107 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3111 if (*attr.msg_type != WPS_WSC_DONE) {
3112 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3117 #ifdef CONFIG_WPS_UPNP
3118 if (wps->wps->wps_upnp && wps->ext_reg) {
3119 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3120 "Registrar completed successfully");
3121 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3125 #endif /* CONFIG_WPS_UPNP */
3127 if (attr.registrar_nonce == NULL ||
3128 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3130 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3134 if (attr.enrollee_nonce == NULL ||
3135 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3136 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3140 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
3141 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3144 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
3145 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
3146 struct wps_credential cred;
3148 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
3149 "on first Enrollee connection");
3151 os_memset(&cred, 0, sizeof(cred));
3152 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
3153 cred.ssid_len = wps->wps->ssid_len;
3154 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
3155 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
3156 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
3157 cred.key_len = wps->new_psk_len;
3159 wps->wps->wps_state = WPS_STATE_CONFIGURED;
3160 wpa_hexdump_ascii_key(MSG_DEBUG,
3161 "WPS: Generated random passphrase",
3162 wps->new_psk, wps->new_psk_len);
3163 if (wps->wps->cred_cb)
3164 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
3166 os_free(wps->new_psk);
3167 wps->new_psk = NULL;
3170 if (!wps->wps->ap && !wps->er)
3171 wps_sta_cred_cb(wps);
3174 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
3175 wps->p2p_dev_addr, wps->new_psk,
3176 wps->new_psk_len)) {
3177 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
3180 os_free(wps->new_psk);
3181 wps->new_psk = NULL;
3184 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e,
3185 wps->dev_password, wps->dev_password_len);
3188 wps_registrar_remove_pbc_session(wps->wps->registrar,
3191 wps_registrar_pbc_completed(wps->wps->registrar);
3192 os_get_time(&wps->wps->registrar->pbc_ignore_start);
3193 os_memcpy(wps->wps->registrar->pbc_ignore_uuid, wps->uuid_e,
3196 wps_registrar_pin_completed(wps->wps->registrar);
3198 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
3199 * merge them into APs own list.. */
3201 wps_success_event(wps->wps, wps->mac_addr_e);
3207 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
3208 enum wsc_op_code op_code,
3209 const struct wpabuf *msg)
3211 enum wps_process_res ret;
3213 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
3215 (unsigned long) wpabuf_len(msg), op_code);
3217 #ifdef CONFIG_WPS_UPNP
3218 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
3219 struct wps_parse_attr attr;
3220 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
3221 *attr.msg_type == WPS_M3)
3222 wps->ext_reg = 2; /* past M2/M2D phase */
3224 if (wps->ext_reg > 1)
3225 wps_registrar_free_pending_m2(wps->wps);
3226 if (wps->wps->wps_upnp && wps->ext_reg &&
3227 wps->wps->upnp_msgs == NULL &&
3228 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
3230 struct wps_parse_attr attr;
3232 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
3235 type = *attr.msg_type;
3236 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
3237 " to external Registrar for processing", type);
3238 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
3240 UPNP_WPS_WLANEVENT_TYPE_EAP,
3242 if (op_code == WSC_MSG)
3244 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
3245 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
3246 "external Registrar");
3247 return WPS_CONTINUE;
3249 #endif /* CONFIG_WPS_UPNP */
3253 return wps_process_wsc_msg(wps, msg);
3255 if (wps_validate_wsc_ack(msg) < 0)
3257 return wps_process_wsc_ack(wps, msg);
3259 if (wps_validate_wsc_nack(msg) < 0)
3261 return wps_process_wsc_nack(wps, msg);
3263 if (wps_validate_wsc_done(msg) < 0)
3265 ret = wps_process_wsc_done(wps, msg);
3266 if (ret == WPS_FAILURE) {
3267 wps->state = SEND_WSC_NACK;
3268 wps_fail_event(wps->wps, WPS_WSC_DONE,
3270 wps->error_indication, wps->mac_addr_e);
3274 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
3280 int wps_registrar_update_ie(struct wps_registrar *reg)
3282 return wps_set_ie(reg);
3286 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
3289 struct wps_registrar *reg = eloop_ctx;
3291 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
3292 "unselect internal Registrar");
3293 reg->selected_registrar = 0;
3295 wps_registrar_selected_registrar_changed(reg, 0);
3299 #ifdef CONFIG_WPS_UPNP
3300 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
3301 struct subscription *s)
3304 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
3305 "config_methods=0x%x)",
3306 s->dev_password_id, s->config_methods);
3307 reg->sel_reg_union = 1;
3308 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
3309 reg->sel_reg_dev_password_id_override = s->dev_password_id;
3310 if (reg->sel_reg_config_methods_override == -1)
3311 reg->sel_reg_config_methods_override = 0;
3312 reg->sel_reg_config_methods_override |= s->config_methods;
3313 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
3314 if (is_zero_ether_addr(reg->authorized_macs_union[i]))
3316 for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
3318 if (is_zero_ether_addr(s->authorized_macs[j]))
3320 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC into union: "
3321 MACSTR, MAC2STR(s->authorized_macs[j]));
3322 os_memcpy(reg->authorized_macs_union[i],
3323 s->authorized_macs[j], ETH_ALEN);
3326 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union",
3327 (u8 *) reg->authorized_macs_union,
3328 sizeof(reg->authorized_macs_union));
3330 #endif /* CONFIG_WPS_UPNP */
3333 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
3335 #ifdef CONFIG_WPS_UPNP
3336 struct subscription *s;
3338 if (reg->wps->wps_upnp == NULL)
3341 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
3342 struct subscription, list) {
3343 struct subscr_addr *sa;
3344 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
3346 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
3347 inet_ntoa(sa->saddr.sin_addr),
3348 ntohs(sa->saddr.sin_port));
3350 if (s->selected_registrar)
3351 wps_registrar_sel_reg_add(reg, s);
3353 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
3356 #endif /* CONFIG_WPS_UPNP */
3361 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3362 * @reg: Registrar data from wps_registrar_init()
3364 * This function is called when selected registrar state changes, e.g., when an
3365 * AP receives a SetSelectedRegistrar UPnP message.
3367 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg,
3370 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
3372 reg->sel_reg_union = reg->selected_registrar;
3373 reg->sel_reg_dev_password_id_override = -1;
3374 reg->sel_reg_config_methods_override = -1;
3375 os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
3376 WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
3377 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union (start with own)",
3378 (u8 *) reg->authorized_macs_union,
3379 sizeof(reg->authorized_macs_union));
3380 if (reg->selected_registrar) {
3383 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
3385 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
3386 WPS_CONFIG_PHY_PUSHBUTTON);
3387 #endif /* CONFIG_WPS2 */
3389 reg->sel_reg_dev_password_id_override =
3391 wps_set_pushbutton(&methods, reg->wps->config_methods);
3392 } else if (dev_pw_id)
3393 reg->sel_reg_dev_password_id_override = dev_pw_id;
3394 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
3395 "(pbc=%d)", reg->pbc);
3396 reg->sel_reg_config_methods_override = methods;
3398 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
3400 wps_registrar_sel_reg_union(reg);
3403 wps_cb_set_sel_reg(reg);
3407 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
3408 char *buf, size_t buflen)
3410 struct wps_registrar_device *d;
3413 char devtype[WPS_DEV_TYPE_BUFSIZE];
3415 d = wps_device_get(reg, addr);
3418 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
3421 ret = os_snprintf(buf + len, buflen - len,
3423 "wpsPrimaryDeviceType=%s\n"
3424 "wpsDeviceName=%s\n"
3425 "wpsManufacturer=%s\n"
3427 "wpsModelNumber=%s\n"
3428 "wpsSerialNumber=%s\n",
3430 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
3432 d->dev.device_name ? d->dev.device_name : "",
3433 d->dev.manufacturer ? d->dev.manufacturer : "",
3434 d->dev.model_name ? d->dev.model_name : "",
3435 d->dev.model_number ? d->dev.model_number : "",
3436 d->dev.serial_number ? d->dev.serial_number : "");
3437 if (ret < 0 || (size_t) ret >= buflen - len)
3445 int wps_registrar_config_ap(struct wps_registrar *reg,
3446 struct wps_credential *cred)
3449 printf("encr_type=0x%x\n", cred->encr_type);
3450 if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
3452 if (cred->encr_type & WPS_ENCR_WEP) {
3453 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
3454 "due to WEP configuration");
3458 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
3459 "invalid encr_type 0x%x", cred->encr_type);
3463 if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
3465 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
3467 cred->encr_type |= WPS_ENCR_AES;
3470 if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
3472 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
3474 cred->auth_type |= WPS_AUTH_WPA2PSK;
3476 #endif /* CONFIG_WPS2 */
3478 if (reg->wps->cred_cb)
3479 return reg->wps->cred_cb(reg->wps->cb_ctx, cred);
3485 #ifdef CONFIG_WPS_NFC
3487 int wps_registrar_add_nfc_pw_token(struct wps_registrar *reg,
3488 const u8 *pubkey_hash, u16 pw_id,
3489 const u8 *dev_pw, size_t dev_pw_len)
3491 struct wps_nfc_pw_token *token;
3493 if (dev_pw_len > WPS_OOB_DEVICE_PASSWORD_LEN)
3496 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, pw_id);
3498 token = os_zalloc(sizeof(*token));
3502 os_memcpy(token->pubkey_hash, pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
3503 token->pw_id = pw_id;
3504 wpa_snprintf_hex_uppercase((char *) token->dev_pw,
3505 sizeof(token->dev_pw),
3506 dev_pw, dev_pw_len);
3507 token->dev_pw_len = dev_pw_len * 2;
3509 dl_list_add(®->nfc_pw_tokens, &token->list);
3511 reg->selected_registrar = 1;
3513 wps_registrar_add_authorized_mac(reg,
3514 (u8 *) "\xff\xff\xff\xff\xff\xff");
3515 wps_registrar_selected_registrar_changed(reg, pw_id);
3516 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
3517 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
3518 wps_registrar_set_selected_timeout,
3521 wpa_printf(MSG_DEBUG, "WPS: Added NFC Device Password %u to Registrar",
3528 int wps_registrar_add_nfc_password_token(struct wps_registrar *reg,
3529 const u8 *oob_dev_pw,
3530 size_t oob_dev_pw_len)
3532 const u8 *pos, *hash, *dev_pw;
3536 if (oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2 +
3537 WPS_OOB_DEVICE_PASSWORD_MIN_LEN ||
3538 oob_dev_pw_len > WPS_OOB_PUBKEY_HASH_LEN + 2 +
3539 WPS_OOB_DEVICE_PASSWORD_LEN)
3543 pos = oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN;
3544 id = WPA_GET_BE16(pos);
3546 dev_pw_len = oob_dev_pw + oob_dev_pw_len - dev_pw;
3548 wpa_printf(MSG_DEBUG, "WPS: Add NFC Password Token for Password ID %u",
3551 wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
3552 hash, WPS_OOB_PUBKEY_HASH_LEN);
3553 wpa_hexdump_key(MSG_DEBUG, "WPS: Device Password", dev_pw, dev_pw_len);
3555 return wps_registrar_add_nfc_pw_token(reg, hash, id, dev_pw,
3560 void wps_registrar_remove_nfc_pw_token(struct wps_registrar *reg,
3561 struct wps_nfc_pw_token *token)
3563 wps_registrar_remove_authorized_mac(reg,
3564 (u8 *) "\xff\xff\xff\xff\xff\xff");
3565 wps_registrar_selected_registrar_changed(reg, 0);
3568 #endif /* CONFIG_WPS_NFC */