2 * Wi-Fi Protected Setup - Registrar
3 * Copyright (c) 2008-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/base64.h"
19 #include "utils/eloop.h"
20 #include "utils/uuid.h"
21 #include "utils/list.h"
22 #include "crypto/crypto.h"
23 #include "crypto/sha256.h"
24 #include "crypto/random.h"
25 #include "common/ieee802_11_defs.h"
27 #include "wps_dev_attr.h"
29 #include "wps_upnp_i.h"
31 #ifndef CONFIG_WPS_STRICT
32 #define WPS_WORKAROUNDS
33 #endif /* CONFIG_WPS_STRICT */
37 u8 uuid[WPS_UUID_LEN];
41 #define PIN_LOCKED BIT(0)
42 #define PIN_EXPIRES BIT(1)
44 struct os_time expiration;
45 u8 enrollee_addr[ETH_ALEN];
49 static void wps_free_pin(struct wps_uuid_pin *pin)
56 static void wps_remove_pin(struct wps_uuid_pin *pin)
58 dl_list_del(&pin->list);
63 static void wps_free_pins(struct dl_list *pins)
65 struct wps_uuid_pin *pin, *prev;
66 dl_list_for_each_safe(pin, prev, pins, struct wps_uuid_pin, list)
71 struct wps_pbc_session {
72 struct wps_pbc_session *next;
74 u8 uuid_e[WPS_UUID_LEN];
75 struct os_time timestamp;
79 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
81 struct wps_pbc_session *prev;
91 struct wps_registrar_device {
92 struct wps_registrar_device *next;
93 struct wps_device_data dev;
94 u8 uuid[WPS_UUID_LEN];
98 struct wps_registrar {
99 struct wps_context *wps;
102 int selected_registrar;
104 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
106 int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
107 struct wpabuf *probe_resp_ie);
108 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
109 const struct wps_device_data *dev);
110 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
112 void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
113 u16 sel_reg_config_methods);
114 void (*enrollee_seen_cb)(void *ctx, const u8 *addr, const u8 *uuid_e,
115 const u8 *pri_dev_type, u16 config_methods,
116 u16 dev_password_id, u8 request_type,
117 const char *dev_name);
121 struct wps_pbc_session *pbc_sessions;
124 struct wpabuf *extra_cred;
125 int disable_auto_conf;
127 int sel_reg_dev_password_id_override;
128 int sel_reg_config_methods_override;
132 struct wps_registrar_device *devices;
134 int force_pbc_overlap;
136 u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
137 u8 authorized_macs_union[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
139 u8 p2p_dev_addr[ETH_ALEN];
143 static int wps_set_ie(struct wps_registrar *reg);
144 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
145 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
149 static void wps_registrar_add_authorized_mac(struct wps_registrar *reg,
153 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC " MACSTR,
155 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
156 if (os_memcmp(reg->authorized_macs[i], addr, ETH_ALEN) == 0) {
157 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was "
158 "already in the list");
159 return; /* already in list */
161 for (i = WPS_MAX_AUTHORIZED_MACS - 1; i > 0; i--)
162 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i - 1],
164 os_memcpy(reg->authorized_macs[0], addr, ETH_ALEN);
165 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
166 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
170 static void wps_registrar_remove_authorized_mac(struct wps_registrar *reg,
174 wpa_printf(MSG_DEBUG, "WPS: Remove authorized MAC " MACSTR,
176 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++) {
177 if (os_memcmp(reg->authorized_macs, addr, ETH_ALEN) == 0)
180 if (i == WPS_MAX_AUTHORIZED_MACS) {
181 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was not in the "
183 return; /* not in the list */
185 for (; i + 1 < WPS_MAX_AUTHORIZED_MACS; i++)
186 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i + 1],
188 os_memset(reg->authorized_macs[WPS_MAX_AUTHORIZED_MACS - 1], 0,
190 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
191 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
195 static void wps_free_devices(struct wps_registrar_device *dev)
197 struct wps_registrar_device *prev;
202 wps_device_data_free(&prev->dev);
208 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
211 struct wps_registrar_device *dev;
213 for (dev = reg->devices; dev; dev = dev->next) {
214 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
221 static void wps_device_clone_data(struct wps_device_data *dst,
222 struct wps_device_data *src)
224 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
225 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
227 #define WPS_STRDUP(n) \
229 dst->n = src->n ? os_strdup(src->n) : NULL
231 WPS_STRDUP(device_name);
232 WPS_STRDUP(manufacturer);
233 WPS_STRDUP(model_name);
234 WPS_STRDUP(model_number);
235 WPS_STRDUP(serial_number);
240 int wps_device_store(struct wps_registrar *reg,
241 struct wps_device_data *dev, const u8 *uuid)
243 struct wps_registrar_device *d;
245 d = wps_device_get(reg, dev->mac_addr);
247 d = os_zalloc(sizeof(*d));
250 d->next = reg->devices;
254 wps_device_clone_data(&d->dev, dev);
255 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
261 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
262 const u8 *addr, const u8 *uuid_e)
264 struct wps_pbc_session *pbc, *prev = NULL;
269 pbc = reg->pbc_sessions;
271 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
272 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
274 prev->next = pbc->next;
276 reg->pbc_sessions = pbc->next;
284 pbc = os_zalloc(sizeof(*pbc));
287 os_memcpy(pbc->addr, addr, ETH_ALEN);
289 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
292 pbc->next = reg->pbc_sessions;
293 reg->pbc_sessions = pbc;
294 pbc->timestamp = now;
296 /* remove entries that have timed out */
301 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
303 wps_free_pbc_sessions(pbc);
312 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
314 const u8 *p2p_dev_addr)
316 struct wps_pbc_session *pbc, *prev = NULL, *tmp;
318 pbc = reg->pbc_sessions;
320 if (os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0 ||
321 (p2p_dev_addr && !is_zero_ether_addr(reg->p2p_dev_addr) &&
322 os_memcmp(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
325 prev->next = pbc->next;
327 reg->pbc_sessions = pbc->next;
330 wpa_printf(MSG_DEBUG, "WPS: Removing PBC session for "
331 "addr=" MACSTR, MAC2STR(tmp->addr));
332 wpa_hexdump(MSG_DEBUG, "WPS: Removed UUID-E",
333 tmp->uuid_e, WPS_UUID_LEN);
343 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
344 const u8 *addr, const u8 *uuid_e)
347 struct wps_pbc_session *pbc;
348 struct wps_pbc_session *first = NULL;
353 wpa_printf(MSG_DEBUG, "WPS: Checking active PBC sessions for overlap");
356 wpa_printf(MSG_DEBUG, "WPS: Add one for the requested UUID");
357 wpa_hexdump(MSG_DEBUG, "WPS: Requested UUID",
358 uuid_e, WPS_UUID_LEN);
362 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
363 wpa_printf(MSG_DEBUG, "WPS: Consider PBC session with " MACSTR,
365 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E",
366 pbc->uuid_e, WPS_UUID_LEN);
367 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
368 wpa_printf(MSG_DEBUG, "WPS: PBC walk time has "
373 os_memcmp(pbc->uuid_e, first->uuid_e, WPS_UUID_LEN) == 0) {
374 wpa_printf(MSG_DEBUG, "WPS: Same Enrollee");
375 continue; /* same Enrollee */
377 if (uuid_e == NULL ||
378 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN)) {
379 wpa_printf(MSG_DEBUG, "WPS: New Enrollee");
386 wpa_printf(MSG_DEBUG, "WPS: %u active PBC session(s) found", count);
388 return count > 1 ? 1 : 0;
392 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
394 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
396 wpabuf_put_be16(msg, ATTR_WPS_STATE);
397 wpabuf_put_be16(msg, 1);
398 wpabuf_put_u8(msg, wps->wps_state);
403 #ifdef CONFIG_WPS_UPNP
404 static void wps_registrar_free_pending_m2(struct wps_context *wps)
406 struct upnp_pending_message *p, *p2, *prev = NULL;
409 if (p->type == WPS_M2 || p->type == WPS_M2D) {
411 wps->upnp_msgs = p->next;
413 prev->next = p->next;
414 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
417 wpabuf_free(p2->msg);
425 #endif /* CONFIG_WPS_UPNP */
428 static int wps_build_ap_setup_locked(struct wps_context *wps,
431 if (wps->ap_setup_locked && wps->ap_setup_locked != 2) {
432 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
433 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
434 wpabuf_put_be16(msg, 1);
435 wpabuf_put_u8(msg, 1);
441 static int wps_build_selected_registrar(struct wps_registrar *reg,
444 if (!reg->sel_reg_union)
446 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
447 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
448 wpabuf_put_be16(msg, 1);
449 wpabuf_put_u8(msg, 1);
454 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
457 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
458 if (!reg->sel_reg_union)
460 if (reg->sel_reg_dev_password_id_override >= 0)
461 id = reg->sel_reg_dev_password_id_override;
462 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
463 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
464 wpabuf_put_be16(msg, 2);
465 wpabuf_put_be16(msg, id);
470 static int wps_build_sel_pbc_reg_uuid_e(struct wps_registrar *reg,
473 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
474 if (!reg->sel_reg_union)
476 if (reg->sel_reg_dev_password_id_override >= 0)
477 id = reg->sel_reg_dev_password_id_override;
478 if (id != DEV_PW_PUSHBUTTON || !reg->dualband)
480 return wps_build_uuid_e(msg, reg->wps->uuid);
484 static void wps_set_pushbutton(u16 *methods, u16 conf_methods)
486 *methods |= WPS_CONFIG_PUSHBUTTON;
488 if (conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON)
489 *methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
490 if (conf_methods & WPS_CONFIG_PHY_PUSHBUTTON)
491 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
492 if (!(*methods & (WPS_CONFIG_VIRT_PUSHBUTTON |
493 WPS_CONFIG_PHY_PUSHBUTTON))) {
495 * Required to include virtual/physical flag, but we were not
496 * configured with push button type, so have to default to one
499 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
501 #endif /* CONFIG_WPS2 */
505 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
509 if (!reg->sel_reg_union)
511 methods = reg->wps->config_methods;
512 methods &= ~WPS_CONFIG_PUSHBUTTON;
514 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
515 WPS_CONFIG_PHY_PUSHBUTTON);
516 #endif /* CONFIG_WPS2 */
518 wps_set_pushbutton(&methods, reg->wps->config_methods);
519 if (reg->sel_reg_config_methods_override >= 0)
520 methods = reg->sel_reg_config_methods_override;
521 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
523 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
524 wpabuf_put_be16(msg, 2);
525 wpabuf_put_be16(msg, methods);
530 static int wps_build_probe_config_methods(struct wps_registrar *reg,
535 * These are the methods that the AP supports as an Enrollee for adding
536 * external Registrars.
538 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
540 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
541 WPS_CONFIG_PHY_PUSHBUTTON);
542 #endif /* CONFIG_WPS2 */
543 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
544 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
545 wpabuf_put_be16(msg, 2);
546 wpabuf_put_be16(msg, methods);
551 static int wps_build_config_methods_r(struct wps_registrar *reg,
554 return wps_build_config_methods(msg, reg->wps->config_methods);
558 const u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count)
563 while (*count < WPS_MAX_AUTHORIZED_MACS) {
564 if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
568 #endif /* CONFIG_WPS2 */
570 return (const u8 *) reg->authorized_macs_union;
575 * wps_registrar_init - Initialize WPS Registrar data
576 * @wps: Pointer to longterm WPS context
577 * @cfg: Registrar configuration
578 * Returns: Pointer to allocated Registrar data or %NULL on failure
580 * This function is used to initialize WPS Registrar functionality. It can be
581 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
582 * runs (e.g., when run as an internal Registrar in an AP). Caller is
583 * responsible for freeing the returned data with wps_registrar_deinit() when
584 * Registrar functionality is not needed anymore.
586 struct wps_registrar *
587 wps_registrar_init(struct wps_context *wps,
588 const struct wps_registrar_config *cfg)
590 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
594 dl_list_init(®->pins);
596 reg->new_psk_cb = cfg->new_psk_cb;
597 reg->set_ie_cb = cfg->set_ie_cb;
598 reg->pin_needed_cb = cfg->pin_needed_cb;
599 reg->reg_success_cb = cfg->reg_success_cb;
600 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
601 reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
602 reg->cb_ctx = cfg->cb_ctx;
603 reg->skip_cred_build = cfg->skip_cred_build;
604 if (cfg->extra_cred) {
605 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
606 cfg->extra_cred_len);
607 if (reg->extra_cred == NULL) {
612 reg->disable_auto_conf = cfg->disable_auto_conf;
613 reg->sel_reg_dev_password_id_override = -1;
614 reg->sel_reg_config_methods_override = -1;
615 reg->static_wep_only = cfg->static_wep_only;
616 reg->dualband = cfg->dualband;
618 if (wps_set_ie(reg)) {
619 wps_registrar_deinit(reg);
628 * wps_registrar_deinit - Deinitialize WPS Registrar data
629 * @reg: Registrar data from wps_registrar_init()
631 void wps_registrar_deinit(struct wps_registrar *reg)
635 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
636 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
637 wps_free_pins(®->pins);
638 wps_free_pbc_sessions(reg->pbc_sessions);
639 wpabuf_free(reg->extra_cred);
640 wps_free_devices(reg->devices);
646 * wps_registrar_add_pin - Configure a new PIN for Registrar
647 * @reg: Registrar data from wps_registrar_init()
648 * @addr: Enrollee MAC address or %NULL if not known
649 * @uuid: UUID-E or %NULL for wildcard (any UUID)
650 * @pin: PIN (Device Password)
651 * @pin_len: Length of pin in octets
652 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
653 * Returns: 0 on success, -1 on failure
655 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
656 const u8 *uuid, const u8 *pin, size_t pin_len,
659 struct wps_uuid_pin *p;
661 p = os_zalloc(sizeof(*p));
665 os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
667 p->wildcard_uuid = 1;
669 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
670 p->pin = os_malloc(pin_len);
671 if (p->pin == NULL) {
675 os_memcpy(p->pin, pin, pin_len);
676 p->pin_len = pin_len;
679 p->flags |= PIN_EXPIRES;
680 os_get_time(&p->expiration);
681 p->expiration.sec += timeout;
684 dl_list_add(®->pins, &p->list);
686 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
688 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
689 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
690 reg->selected_registrar = 1;
693 wps_registrar_add_authorized_mac(reg, addr);
695 wps_registrar_add_authorized_mac(
696 reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
697 wps_registrar_selected_registrar_changed(reg);
698 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
699 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
700 wps_registrar_set_selected_timeout,
707 static void wps_registrar_remove_pin(struct wps_registrar *reg,
708 struct wps_uuid_pin *pin)
711 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
713 if (is_zero_ether_addr(pin->enrollee_addr))
716 addr = pin->enrollee_addr;
717 wps_registrar_remove_authorized_mac(reg, addr);
719 wps_registrar_selected_registrar_changed(reg);
723 static void wps_registrar_expire_pins(struct wps_registrar *reg)
725 struct wps_uuid_pin *pin, *prev;
729 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
731 if ((pin->flags & PIN_EXPIRES) &&
732 os_time_before(&pin->expiration, &now)) {
733 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
734 pin->uuid, WPS_UUID_LEN);
735 wps_registrar_remove_pin(reg, pin);
742 * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
743 * @reg: Registrar data from wps_registrar_init()
744 * Returns: 0 on success, -1 if not wildcard PIN is enabled
746 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg)
748 struct wps_uuid_pin *pin, *prev;
750 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
752 if (pin->wildcard_uuid) {
753 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
754 pin->uuid, WPS_UUID_LEN);
755 wps_registrar_remove_pin(reg, pin);
765 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
766 * @reg: Registrar data from wps_registrar_init()
768 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
770 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
772 struct wps_uuid_pin *pin, *prev;
774 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
776 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
777 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
778 pin->uuid, WPS_UUID_LEN);
779 wps_registrar_remove_pin(reg, pin);
788 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
789 const u8 *uuid, size_t *pin_len)
791 struct wps_uuid_pin *pin, *found = NULL;
793 wps_registrar_expire_pins(reg);
795 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
796 if (!pin->wildcard_uuid &&
797 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
804 /* Check for wildcard UUIDs since none of the UUID-specific
806 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
807 if (pin->wildcard_uuid == 1) {
808 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
809 "PIN. Assigned it for this UUID-E");
810 pin->wildcard_uuid = 2;
811 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
822 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
823 * that could otherwise avoid PIN invalidations.
825 if (found->flags & PIN_LOCKED) {
826 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
827 "allow concurrent re-use");
830 *pin_len = found->pin_len;
831 found->flags |= PIN_LOCKED;
837 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
838 * @reg: Registrar data from wps_registrar_init()
840 * Returns: 0 on success, -1 on failure
842 * PINs are locked to enforce only one concurrent use. This function unlocks a
843 * PIN to allow it to be used again. If the specified PIN was configured using
844 * a wildcard UUID, it will be removed instead of allowing multiple uses.
846 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
848 struct wps_uuid_pin *pin;
850 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
851 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
852 if (pin->wildcard_uuid == 2) {
853 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
855 return wps_registrar_invalidate_pin(reg, uuid);
857 pin->flags &= ~PIN_LOCKED;
866 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
868 reg->selected_registrar = 0;
870 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
871 wps_registrar_remove_authorized_mac(reg,
872 (u8 *) "\xff\xff\xff\xff\xff\xff");
873 wps_registrar_selected_registrar_changed(reg);
877 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
879 struct wps_registrar *reg = eloop_ctx;
881 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
882 wps_pbc_timeout_event(reg->wps);
883 wps_registrar_stop_pbc(reg);
888 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
889 * @reg: Registrar data from wps_registrar_init()
890 * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
891 * indicates no such filtering
892 * Returns: 0 on success, -1 on failure, -2 on session overlap
894 * This function is called on an AP when a push button is pushed to activate
895 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
896 * or when a PBC registration is completed. If more than one Enrollee in active
897 * PBC mode has been detected during the monitor time (previous 2 minutes), the
898 * PBC mode is not activated and -2 is returned to indicate session overlap.
899 * This is skipped if a specific Enrollee is selected.
901 int wps_registrar_button_pushed(struct wps_registrar *reg,
902 const u8 *p2p_dev_addr)
904 if (p2p_dev_addr == NULL &&
905 wps_registrar_pbc_overlap(reg, NULL, NULL)) {
906 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
908 wps_pbc_overlap_event(reg->wps);
911 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
912 reg->force_pbc_overlap = 0;
913 reg->selected_registrar = 1;
916 os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
918 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
919 wps_registrar_add_authorized_mac(reg,
920 (u8 *) "\xff\xff\xff\xff\xff\xff");
921 wps_registrar_selected_registrar_changed(reg);
923 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
924 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
925 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
931 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
933 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
934 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
935 wps_registrar_stop_pbc(reg);
939 static void wps_registrar_pin_completed(struct wps_registrar *reg)
941 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
942 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
943 reg->selected_registrar = 0;
944 wps_registrar_selected_registrar_changed(reg);
948 void wps_registrar_complete(struct wps_registrar *registrar, const u8 *uuid_e)
950 if (registrar->pbc) {
951 wps_registrar_remove_pbc_session(registrar,
953 wps_registrar_pbc_completed(registrar);
955 wps_registrar_pin_completed(registrar);
960 int wps_registrar_wps_cancel(struct wps_registrar *reg)
963 wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
964 wps_registrar_pbc_timeout(reg, NULL);
965 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
967 } else if (reg->selected_registrar) {
969 wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
970 wps_registrar_pin_completed(reg);
971 wps_registrar_invalidate_wildcard_pin(reg);
979 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
980 * @reg: Registrar data from wps_registrar_init()
981 * @addr: MAC address of the Probe Request sender
982 * @wps_data: WPS IE contents
984 * This function is called on an AP when a Probe Request with WPS IE is
985 * received. This is used to track PBC mode use and to detect possible overlap
986 * situation with other WPS APs.
988 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
989 const struct wpabuf *wps_data,
992 struct wps_parse_attr attr;
994 wpa_hexdump_buf(MSG_MSGDUMP,
995 "WPS: Probe Request with WPS data received",
998 if (wps_parse_msg(wps_data, &attr) < 0)
1001 if (attr.config_methods == NULL) {
1002 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
1007 if (attr.dev_password_id == NULL) {
1008 wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
1009 "in Probe Request");
1013 if (reg->enrollee_seen_cb && attr.uuid_e &&
1014 attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
1015 char *dev_name = NULL;
1016 if (attr.dev_name) {
1017 dev_name = os_zalloc(attr.dev_name_len + 1);
1019 os_memcpy(dev_name, attr.dev_name,
1023 reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
1024 attr.primary_dev_type,
1025 WPA_GET_BE16(attr.config_methods),
1026 WPA_GET_BE16(attr.dev_password_id),
1027 *attr.request_type, dev_name);
1031 if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
1032 return; /* Not PBC */
1034 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
1035 MACSTR, MAC2STR(addr));
1036 if (attr.uuid_e == NULL) {
1037 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
1041 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E from Probe Request", attr.uuid_e,
1044 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
1045 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
1046 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
1047 reg->force_pbc_overlap = 1;
1048 wps_pbc_overlap_event(reg->wps);
1053 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
1054 const u8 *psk, size_t psk_len)
1056 if (reg->new_psk_cb == NULL)
1059 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
1063 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
1064 const struct wps_device_data *dev)
1066 if (reg->pin_needed_cb == NULL)
1069 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
1073 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
1076 if (reg->reg_success_cb == NULL)
1079 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
1083 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
1084 struct wpabuf *probe_resp_ie)
1086 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
1090 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
1093 if (reg->set_sel_reg_cb == NULL)
1096 if (reg->selected_registrar) {
1097 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
1099 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
1100 WPS_CONFIG_PHY_PUSHBUTTON);
1101 #endif /* CONFIG_WPS2 */
1103 wps_set_pushbutton(&methods, reg->wps->config_methods);
1106 wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1107 "config_methods=0x%x pbc=%d methods=0x%x",
1108 reg->selected_registrar, reg->wps->config_methods,
1111 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
1112 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
1117 static int wps_set_ie(struct wps_registrar *reg)
1119 struct wpabuf *beacon;
1120 struct wpabuf *probe;
1121 const u8 *auth_macs;
1123 size_t vendor_len = 0;
1126 if (reg->set_ie_cb == NULL)
1129 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
1130 if (reg->wps->dev.vendor_ext[i]) {
1131 vendor_len += 2 + 2;
1132 vendor_len += wpabuf_len(reg->wps->dev.vendor_ext[i]);
1136 beacon = wpabuf_alloc(400 + vendor_len);
1139 probe = wpabuf_alloc(500 + vendor_len);
1140 if (probe == NULL) {
1141 wpabuf_free(beacon);
1145 auth_macs = wps_authorized_macs(reg, &count);
1147 wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
1149 if (wps_build_version(beacon) ||
1150 wps_build_wps_state(reg->wps, beacon) ||
1151 wps_build_ap_setup_locked(reg->wps, beacon) ||
1152 wps_build_selected_registrar(reg, beacon) ||
1153 wps_build_sel_reg_dev_password_id(reg, beacon) ||
1154 wps_build_sel_reg_config_methods(reg, beacon) ||
1155 wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
1156 (reg->dualband && wps_build_rf_bands(®->wps->dev, beacon)) ||
1157 wps_build_wfa_ext(beacon, 0, auth_macs, count) ||
1158 wps_build_vendor_ext(®->wps->dev, beacon)) {
1159 wpabuf_free(beacon);
1165 if (wps_build_dev_name(®->wps->dev, beacon) ||
1166 wps_build_primary_dev_type(®->wps->dev, beacon)) {
1167 wpabuf_free(beacon);
1171 #endif /* CONFIG_P2P */
1173 wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
1175 if (wps_build_version(probe) ||
1176 wps_build_wps_state(reg->wps, probe) ||
1177 wps_build_ap_setup_locked(reg->wps, probe) ||
1178 wps_build_selected_registrar(reg, probe) ||
1179 wps_build_sel_reg_dev_password_id(reg, probe) ||
1180 wps_build_sel_reg_config_methods(reg, probe) ||
1181 wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
1182 WPS_RESP_REGISTRAR) ||
1183 wps_build_uuid_e(probe, reg->wps->uuid) ||
1184 wps_build_device_attrs(®->wps->dev, probe) ||
1185 wps_build_probe_config_methods(reg, probe) ||
1186 wps_build_rf_bands(®->wps->dev, probe) ||
1187 wps_build_wfa_ext(probe, 0, auth_macs, count) ||
1188 wps_build_vendor_ext(®->wps->dev, probe)) {
1189 wpabuf_free(beacon);
1194 beacon = wps_ie_encapsulate(beacon);
1195 probe = wps_ie_encapsulate(probe);
1197 if (!beacon || !probe) {
1198 wpabuf_free(beacon);
1203 if (reg->static_wep_only) {
1205 * Windows XP and Vista clients can get confused about
1206 * EAP-Identity/Request when they probe the network with
1207 * EAPOL-Start. In such a case, they may assume the network is
1208 * using IEEE 802.1X and prompt user for a certificate while
1209 * the correct (non-WPS) behavior would be to ask for the
1210 * static WEP key. As a workaround, use Microsoft Provisioning
1211 * IE to advertise that legacy 802.1X is not supported.
1213 const u8 ms_wps[7] = {
1214 WLAN_EID_VENDOR_SPECIFIC, 5,
1215 /* Microsoft Provisioning IE (00:50:f2:5) */
1216 0x00, 0x50, 0xf2, 5,
1217 0x00 /* no legacy 802.1X or MS WPS */
1219 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
1220 "into Beacon/Probe Response frames");
1221 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
1222 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
1225 return wps_cb_set_ie(reg, beacon, probe);
1229 static int wps_get_dev_password(struct wps_data *wps)
1234 os_free(wps->dev_password);
1235 wps->dev_password = NULL;
1238 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
1239 pin = (const u8 *) "00000000";
1242 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1246 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1248 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1253 wps->dev_password = os_malloc(pin_len);
1254 if (wps->dev_password == NULL)
1256 os_memcpy(wps->dev_password, pin, pin_len);
1257 wps->dev_password_len = pin_len;
1263 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1265 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1266 wpabuf_put_be16(msg, ATTR_UUID_R);
1267 wpabuf_put_be16(msg, WPS_UUID_LEN);
1268 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1273 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1279 if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1281 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1282 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1283 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1285 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1286 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1287 "R-Hash derivation");
1291 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1292 wpabuf_put_be16(msg, ATTR_R_HASH1);
1293 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1294 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1295 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1296 addr[0] = wps->snonce;
1297 len[0] = WPS_SECRET_NONCE_LEN;
1298 addr[1] = wps->psk1;
1299 len[1] = WPS_PSK_LEN;
1300 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1301 len[2] = wpabuf_len(wps->dh_pubkey_e);
1302 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1303 len[3] = wpabuf_len(wps->dh_pubkey_r);
1304 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1305 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1307 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1308 wpabuf_put_be16(msg, ATTR_R_HASH2);
1309 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1310 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1311 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1312 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1313 addr[1] = wps->psk2;
1314 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1315 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1321 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1323 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1324 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1325 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1326 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1331 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1333 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1334 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1335 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1336 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1337 WPS_SECRET_NONCE_LEN);
1342 static int wps_build_cred_network_idx(struct wpabuf *msg,
1343 const struct wps_credential *cred)
1345 wpa_printf(MSG_DEBUG, "WPS: * Network Index (1)");
1346 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1347 wpabuf_put_be16(msg, 1);
1348 wpabuf_put_u8(msg, 1);
1353 static int wps_build_cred_ssid(struct wpabuf *msg,
1354 const struct wps_credential *cred)
1356 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1357 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID for Credential",
1358 cred->ssid, cred->ssid_len);
1359 wpabuf_put_be16(msg, ATTR_SSID);
1360 wpabuf_put_be16(msg, cred->ssid_len);
1361 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1366 static int wps_build_cred_auth_type(struct wpabuf *msg,
1367 const struct wps_credential *cred)
1369 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1371 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1372 wpabuf_put_be16(msg, 2);
1373 wpabuf_put_be16(msg, cred->auth_type);
1378 static int wps_build_cred_encr_type(struct wpabuf *msg,
1379 const struct wps_credential *cred)
1381 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1383 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1384 wpabuf_put_be16(msg, 2);
1385 wpabuf_put_be16(msg, cred->encr_type);
1390 static int wps_build_cred_network_key(struct wpabuf *msg,
1391 const struct wps_credential *cred)
1393 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1394 (int) cred->key_len);
1395 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
1396 cred->key, cred->key_len);
1397 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1398 wpabuf_put_be16(msg, cred->key_len);
1399 wpabuf_put_data(msg, cred->key, cred->key_len);
1404 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1405 const struct wps_credential *cred)
1407 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1408 MAC2STR(cred->mac_addr));
1409 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1410 wpabuf_put_be16(msg, ETH_ALEN);
1411 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1416 static int wps_build_credential(struct wpabuf *msg,
1417 const struct wps_credential *cred)
1419 if (wps_build_cred_network_idx(msg, cred) ||
1420 wps_build_cred_ssid(msg, cred) ||
1421 wps_build_cred_auth_type(msg, cred) ||
1422 wps_build_cred_encr_type(msg, cred) ||
1423 wps_build_cred_network_key(msg, cred) ||
1424 wps_build_cred_mac_addr(msg, cred))
1430 int wps_build_credential_wrap(struct wpabuf *msg,
1431 const struct wps_credential *cred)
1433 struct wpabuf *wbuf;
1434 wbuf = wpabuf_alloc(200);
1437 if (wps_build_credential(wbuf, cred)) {
1441 wpabuf_put_be16(msg, ATTR_CRED);
1442 wpabuf_put_be16(msg, wpabuf_len(wbuf));
1443 wpabuf_put_buf(msg, wbuf);
1449 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1451 struct wpabuf *cred;
1453 if (wps->wps->registrar->skip_cred_build)
1454 goto skip_cred_build;
1456 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1457 if (wps->use_cred) {
1458 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1461 os_memset(&wps->cred, 0, sizeof(wps->cred));
1463 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1464 wps->cred.ssid_len = wps->wps->ssid_len;
1466 /* Select the best authentication and encryption type */
1467 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1468 wps->auth_type = WPS_AUTH_WPA2PSK;
1469 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1470 wps->auth_type = WPS_AUTH_WPAPSK;
1471 else if (wps->auth_type & WPS_AUTH_OPEN)
1472 wps->auth_type = WPS_AUTH_OPEN;
1473 else if (wps->auth_type & WPS_AUTH_SHARED)
1474 wps->auth_type = WPS_AUTH_SHARED;
1476 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1480 wps->cred.auth_type = wps->auth_type;
1482 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1483 wps->auth_type == WPS_AUTH_WPAPSK) {
1484 if (wps->encr_type & WPS_ENCR_AES)
1485 wps->encr_type = WPS_ENCR_AES;
1486 else if (wps->encr_type & WPS_ENCR_TKIP)
1487 wps->encr_type = WPS_ENCR_TKIP;
1489 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1490 "type for WPA/WPA2");
1494 if (wps->encr_type & WPS_ENCR_WEP)
1495 wps->encr_type = WPS_ENCR_WEP;
1496 else if (wps->encr_type & WPS_ENCR_NONE)
1497 wps->encr_type = WPS_ENCR_NONE;
1499 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1500 "type for non-WPA/WPA2 mode");
1504 wps->cred.encr_type = wps->encr_type;
1506 * Set MAC address in the Credential to be the Enrollee's MAC address
1508 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1510 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1511 !wps->wps->registrar->disable_auto_conf) {
1513 /* Generate a random passphrase */
1514 if (random_get_bytes(r, sizeof(r)) < 0)
1516 os_free(wps->new_psk);
1517 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1518 if (wps->new_psk == NULL)
1520 wps->new_psk_len--; /* remove newline */
1521 while (wps->new_psk_len &&
1522 wps->new_psk[wps->new_psk_len - 1] == '=')
1524 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1525 wps->new_psk, wps->new_psk_len);
1526 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1527 wps->cred.key_len = wps->new_psk_len;
1528 } else if (wps->use_psk_key && wps->wps->psk_set) {
1530 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1531 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1532 os_memcpy(wps->cred.key, hex, 32 * 2);
1533 wps->cred.key_len = 32 * 2;
1534 } else if (wps->wps->network_key) {
1535 os_memcpy(wps->cred.key, wps->wps->network_key,
1536 wps->wps->network_key_len);
1537 wps->cred.key_len = wps->wps->network_key_len;
1538 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1540 /* Generate a random per-device PSK */
1541 os_free(wps->new_psk);
1542 wps->new_psk_len = 32;
1543 wps->new_psk = os_malloc(wps->new_psk_len);
1544 if (wps->new_psk == NULL)
1546 if (random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
1547 os_free(wps->new_psk);
1548 wps->new_psk = NULL;
1551 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1552 wps->new_psk, wps->new_psk_len);
1553 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1555 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1556 wps->cred.key_len = wps->new_psk_len * 2;
1560 #ifdef CONFIG_WPS_TESTING
1561 if (wps_testing_dummy_cred)
1562 cred = wpabuf_alloc(200);
1566 struct wps_credential dummy;
1567 wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
1568 os_memset(&dummy, 0, sizeof(dummy));
1569 os_memcpy(dummy.ssid, "dummy", 5);
1571 dummy.auth_type = WPS_AUTH_WPA2PSK;
1572 dummy.encr_type = WPS_ENCR_AES;
1573 os_memcpy(dummy.key, "dummy psk", 9);
1575 os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
1576 wps_build_credential(cred, &dummy);
1577 wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
1579 wpabuf_put_be16(msg, ATTR_CRED);
1580 wpabuf_put_be16(msg, wpabuf_len(cred));
1581 wpabuf_put_buf(msg, cred);
1585 #endif /* CONFIG_WPS_TESTING */
1587 cred = wpabuf_alloc(200);
1591 if (wps_build_credential(cred, &wps->cred)) {
1596 wpabuf_put_be16(msg, ATTR_CRED);
1597 wpabuf_put_be16(msg, wpabuf_len(cred));
1598 wpabuf_put_buf(msg, cred);
1602 if (wps->wps->registrar->extra_cred) {
1603 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1604 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1611 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1613 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1615 if (wps_build_credential(msg, &wps->cred))
1622 static struct wpabuf * wps_build_ap_cred(struct wps_data *wps)
1624 struct wpabuf *msg, *plain;
1626 msg = wpabuf_alloc(1000);
1630 plain = wpabuf_alloc(200);
1631 if (plain == NULL) {
1636 if (wps_build_ap_settings(wps, plain)) {
1642 wpabuf_put_be16(msg, ATTR_CRED);
1643 wpabuf_put_be16(msg, wpabuf_len(plain));
1644 wpabuf_put_buf(msg, plain);
1651 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1655 if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
1657 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1658 wps->nonce_r, WPS_NONCE_LEN);
1659 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1661 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1662 msg = wpabuf_alloc(1000);
1666 if (wps_build_version(msg) ||
1667 wps_build_msg_type(msg, WPS_M2) ||
1668 wps_build_enrollee_nonce(wps, msg) ||
1669 wps_build_registrar_nonce(wps, msg) ||
1670 wps_build_uuid_r(wps, msg) ||
1671 wps_build_public_key(wps, msg) ||
1672 wps_derive_keys(wps) ||
1673 wps_build_auth_type_flags(wps, msg) ||
1674 wps_build_encr_type_flags(wps, msg) ||
1675 wps_build_conn_type_flags(wps, msg) ||
1676 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1677 wps_build_device_attrs(&wps->wps->dev, msg) ||
1678 wps_build_rf_bands(&wps->wps->dev, msg) ||
1679 wps_build_assoc_state(wps, msg) ||
1680 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1681 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1682 wps_build_os_version(&wps->wps->dev, msg) ||
1683 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1684 wps_build_authenticator(wps, msg)) {
1690 wps->state = RECV_M3;
1695 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1698 u16 err = wps->config_error;
1700 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1701 msg = wpabuf_alloc(1000);
1705 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1706 err == WPS_CFG_NO_ERROR)
1707 err = WPS_CFG_SETUP_LOCKED;
1709 if (wps_build_version(msg) ||
1710 wps_build_msg_type(msg, WPS_M2D) ||
1711 wps_build_enrollee_nonce(wps, msg) ||
1712 wps_build_registrar_nonce(wps, msg) ||
1713 wps_build_uuid_r(wps, msg) ||
1714 wps_build_auth_type_flags(wps, msg) ||
1715 wps_build_encr_type_flags(wps, msg) ||
1716 wps_build_conn_type_flags(wps, msg) ||
1717 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1718 wps_build_device_attrs(&wps->wps->dev, msg) ||
1719 wps_build_rf_bands(&wps->wps->dev, msg) ||
1720 wps_build_assoc_state(wps, msg) ||
1721 wps_build_config_error(msg, err) ||
1722 wps_build_os_version(&wps->wps->dev, msg) ||
1723 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1728 wps->state = RECV_M2D_ACK;
1733 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1735 struct wpabuf *msg, *plain;
1737 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1739 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1741 plain = wpabuf_alloc(200);
1745 msg = wpabuf_alloc(1000);
1751 if (wps_build_version(msg) ||
1752 wps_build_msg_type(msg, WPS_M4) ||
1753 wps_build_enrollee_nonce(wps, msg) ||
1754 wps_build_r_hash(wps, msg) ||
1755 wps_build_r_snonce1(wps, plain) ||
1756 wps_build_key_wrap_auth(wps, plain) ||
1757 wps_build_encr_settings(wps, msg, plain) ||
1758 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1759 wps_build_authenticator(wps, msg)) {
1766 wps->state = RECV_M5;
1771 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1773 struct wpabuf *msg, *plain;
1775 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1777 plain = wpabuf_alloc(200);
1781 msg = wpabuf_alloc(1000);
1787 if (wps_build_version(msg) ||
1788 wps_build_msg_type(msg, WPS_M6) ||
1789 wps_build_enrollee_nonce(wps, msg) ||
1790 wps_build_r_snonce2(wps, plain) ||
1791 wps_build_key_wrap_auth(wps, plain) ||
1792 wps_build_encr_settings(wps, msg, plain) ||
1793 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1794 wps_build_authenticator(wps, msg)) {
1801 wps->wps_pin_revealed = 1;
1802 wps->state = RECV_M7;
1807 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1809 struct wpabuf *msg, *plain;
1811 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1813 plain = wpabuf_alloc(500);
1817 msg = wpabuf_alloc(1000);
1823 if (wps_build_version(msg) ||
1824 wps_build_msg_type(msg, WPS_M8) ||
1825 wps_build_enrollee_nonce(wps, msg) ||
1826 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1827 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1828 wps_build_key_wrap_auth(wps, plain) ||
1829 wps_build_encr_settings(wps, msg, plain) ||
1830 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1831 wps_build_authenticator(wps, msg)) {
1838 wps->state = RECV_DONE;
1843 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1844 enum wsc_op_code *op_code)
1848 #ifdef CONFIG_WPS_UPNP
1849 if (!wps->int_reg && wps->wps->wps_upnp) {
1850 struct upnp_pending_message *p, *prev = NULL;
1851 if (wps->ext_reg > 1)
1852 wps_registrar_free_pending_m2(wps->wps);
1853 p = wps->wps->upnp_msgs;
1854 /* TODO: check pending message MAC address */
1855 while (p && p->next) {
1860 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1865 wps->wps->upnp_msgs = NULL;
1872 *op_code = WSC_NACK;
1879 if (wps->ext_reg == 0)
1885 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1886 "pending message available");
1889 #endif /* CONFIG_WPS_UPNP */
1891 switch (wps->state) {
1893 if (wps_get_dev_password(wps) < 0)
1894 msg = wps_build_m2d(wps);
1896 msg = wps_build_m2(wps);
1900 msg = wps_build_m2d(wps);
1904 msg = wps_build_m4(wps);
1908 msg = wps_build_m6(wps);
1912 msg = wps_build_m8(wps);
1916 msg = wps_build_wsc_ack(wps);
1920 msg = wps_build_wsc_nack(wps);
1921 *op_code = WSC_NACK;
1924 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1925 "a message", wps->state);
1930 if (*op_code == WSC_MSG && msg) {
1931 /* Save a copy of the last message for Authenticator derivation
1933 wpabuf_free(wps->last_msg);
1934 wps->last_msg = wpabuf_dup(msg);
1941 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1943 if (e_nonce == NULL) {
1944 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1948 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1949 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1950 wps->nonce_e, WPS_NONCE_LEN);
1956 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1958 if (r_nonce == NULL) {
1959 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1963 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1964 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1972 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1974 if (uuid_e == NULL) {
1975 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1979 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1980 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1986 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1988 if (pw_id == NULL) {
1989 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1993 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1994 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
2000 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
2002 if (e_hash1 == NULL) {
2003 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
2007 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
2008 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
2014 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
2016 if (e_hash2 == NULL) {
2017 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
2021 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
2022 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
2028 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
2030 u8 hash[SHA256_MAC_LEN];
2034 if (e_snonce1 == NULL) {
2035 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
2039 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
2040 WPS_SECRET_NONCE_LEN);
2042 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
2043 addr[0] = e_snonce1;
2044 len[0] = WPS_SECRET_NONCE_LEN;
2045 addr[1] = wps->psk1;
2046 len[1] = WPS_PSK_LEN;
2047 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2048 len[2] = wpabuf_len(wps->dh_pubkey_e);
2049 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2050 len[3] = wpabuf_len(wps->dh_pubkey_r);
2051 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2053 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
2054 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
2055 "not match with the pre-committed value");
2056 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2057 wps_pwd_auth_fail_event(wps->wps, 0, 1);
2061 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
2062 "half of the device password");
2068 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
2070 u8 hash[SHA256_MAC_LEN];
2074 if (e_snonce2 == NULL) {
2075 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
2079 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
2080 WPS_SECRET_NONCE_LEN);
2082 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
2083 addr[0] = e_snonce2;
2084 len[0] = WPS_SECRET_NONCE_LEN;
2085 addr[1] = wps->psk2;
2086 len[1] = WPS_PSK_LEN;
2087 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2088 len[2] = wpabuf_len(wps->dh_pubkey_e);
2089 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2090 len[3] = wpabuf_len(wps->dh_pubkey_r);
2091 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2093 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
2094 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
2095 "not match with the pre-committed value");
2096 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2097 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2098 wps_pwd_auth_fail_event(wps->wps, 0, 2);
2102 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
2103 "half of the device password");
2104 wps->wps_pin_revealed = 0;
2105 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
2111 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
2113 if (mac_addr == NULL) {
2114 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
2118 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
2120 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
2121 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
2127 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
2130 if (pk == NULL || pk_len == 0) {
2131 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
2135 #ifdef CONFIG_WPS_OOB
2136 if (wps->wps->oob_conf.pubkey_hash != NULL) {
2138 u8 hash[WPS_HASH_LEN];
2141 sha256_vector(1, addr, &pk_len, hash);
2143 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
2144 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
2145 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
2149 #endif /* CONFIG_WPS_OOB */
2151 wpabuf_free(wps->dh_pubkey_e);
2152 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
2153 if (wps->dh_pubkey_e == NULL)
2160 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
2165 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
2170 auth_types = WPA_GET_BE16(auth);
2172 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
2174 wps->auth_type = wps->wps->auth_types & auth_types;
2175 if (wps->auth_type == 0) {
2176 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2177 "authentication types (own 0x%x Enrollee 0x%x)",
2178 wps->wps->auth_types, auth_types);
2179 #ifdef WPS_WORKAROUNDS
2181 * Some deployed implementations seem to advertise incorrect
2182 * information in this attribute. For example, Linksys WRT350N
2183 * seems to have a byteorder bug that breaks this negotiation.
2184 * In order to interoperate with existing implementations,
2185 * assume that the Enrollee supports everything we do.
2187 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2188 "does not advertise supported authentication types "
2190 wps->auth_type = wps->wps->auth_types;
2191 #else /* WPS_WORKAROUNDS */
2193 #endif /* WPS_WORKAROUNDS */
2200 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
2205 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
2210 encr_types = WPA_GET_BE16(encr);
2212 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
2214 wps->encr_type = wps->wps->encr_types & encr_types;
2215 if (wps->encr_type == 0) {
2216 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2217 "encryption types (own 0x%x Enrollee 0x%x)",
2218 wps->wps->encr_types, encr_types);
2219 #ifdef WPS_WORKAROUNDS
2221 * Some deployed implementations seem to advertise incorrect
2222 * information in this attribute. For example, Linksys WRT350N
2223 * seems to have a byteorder bug that breaks this negotiation.
2224 * In order to interoperate with existing implementations,
2225 * assume that the Enrollee supports everything we do.
2227 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2228 "does not advertise supported encryption types "
2230 wps->encr_type = wps->wps->encr_types;
2231 #else /* WPS_WORKAROUNDS */
2233 #endif /* WPS_WORKAROUNDS */
2240 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
2243 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
2248 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
2255 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
2259 if (methods == NULL) {
2260 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
2264 m = WPA_GET_BE16(methods);
2266 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
2267 "%s%s%s%s%s%s%s%s%s", m,
2268 m & WPS_CONFIG_USBA ? " [USBA]" : "",
2269 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
2270 m & WPS_CONFIG_LABEL ? " [Label]" : "",
2271 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
2272 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
2273 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
2274 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
2275 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
2276 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
2278 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
2280 * The Enrollee does not have a display so it is unlikely to be
2281 * able to show the passphrase to a user and as such, could
2282 * benefit from receiving PSK to reduce key derivation time.
2284 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2285 "Enrollee not supporting display");
2286 wps->use_psk_key = 1;
2293 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2295 if (state == NULL) {
2296 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2301 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2308 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2312 if (assoc == NULL) {
2313 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2317 a = WPA_GET_BE16(assoc);
2318 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2324 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2329 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2333 e = WPA_GET_BE16(err);
2334 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2340 static int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
2343 struct wps_registrar *reg = wps->wps->registrar;
2345 if (is_zero_ether_addr(reg->p2p_dev_addr))
2346 return 1; /* no filtering in use */
2348 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
2349 wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
2350 "filtering for PBC: expected " MACSTR " was "
2351 MACSTR " - indicate PBC session overlap",
2352 MAC2STR(reg->p2p_dev_addr),
2353 MAC2STR(wps->p2p_dev_addr));
2356 #endif /* CONFIG_P2P */
2361 static int wps_registrar_skip_overlap(struct wps_data *wps)
2364 struct wps_registrar *reg = wps->wps->registrar;
2366 if (is_zero_ether_addr(reg->p2p_dev_addr))
2367 return 0; /* no specific Enrollee selected */
2369 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) == 0) {
2370 wpa_printf(MSG_DEBUG, "WPS: Skip PBC overlap due to selected "
2374 #endif /* CONFIG_P2P */
2379 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2380 struct wps_parse_attr *attr)
2382 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2384 if (wps->state != RECV_M1) {
2385 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2386 "receiving M1", wps->state);
2390 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2391 wps_process_mac_addr(wps, attr->mac_addr) ||
2392 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2393 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2394 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2395 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2396 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2397 wps_process_config_methods(wps, attr->config_methods) ||
2398 wps_process_wps_state(wps, attr->wps_state) ||
2399 wps_process_device_attrs(&wps->peer_dev, attr) ||
2400 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2401 wps_process_assoc_state(wps, attr->assoc_state) ||
2402 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2403 wps_process_config_error(wps, attr->config_error) ||
2404 wps_process_os_version(&wps->peer_dev, attr->os_version))
2407 if (wps->dev_pw_id < 0x10 &&
2408 wps->dev_pw_id != DEV_PW_DEFAULT &&
2409 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2410 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2411 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2412 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2413 !wps->wps->registrar->pbc)) {
2414 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2416 wps->state = SEND_M2D;
2417 return WPS_CONTINUE;
2420 #ifdef CONFIG_WPS_OOB
2421 if (wps->dev_pw_id >= 0x10 &&
2422 wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
2423 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
2424 "%d mismatch", wps->dev_pw_id);
2425 wps->state = SEND_M2D;
2426 return WPS_CONTINUE;
2428 #endif /* CONFIG_WPS_OOB */
2430 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2431 if ((wps->wps->registrar->force_pbc_overlap ||
2432 wps_registrar_pbc_overlap(wps->wps->registrar,
2433 wps->mac_addr_e, wps->uuid_e) ||
2434 !wps_registrar_p2p_dev_addr_match(wps)) &&
2435 !wps_registrar_skip_overlap(wps)) {
2436 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2438 wps->state = SEND_M2D;
2439 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2440 wps_pbc_overlap_event(wps->wps);
2441 wps_fail_event(wps->wps, WPS_M1,
2442 WPS_CFG_MULTIPLE_PBC_DETECTED,
2444 wps->wps->registrar->force_pbc_overlap = 1;
2445 return WPS_CONTINUE;
2447 wps_registrar_add_pbc_session(wps->wps->registrar,
2448 wps->mac_addr_e, wps->uuid_e);
2452 #ifdef WPS_WORKAROUNDS
2454 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2455 * passphrase format. To avoid interop issues, force PSK format to be
2458 if (!wps->use_psk_key &&
2459 wps->peer_dev.manufacturer &&
2460 os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
2461 wps->peer_dev.model_name &&
2462 os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
2463 wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
2465 wps->use_psk_key = 1;
2467 #endif /* WPS_WORKAROUNDS */
2469 wps->state = SEND_M2;
2470 return WPS_CONTINUE;
2474 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2475 const struct wpabuf *msg,
2476 struct wps_parse_attr *attr)
2478 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2480 if (wps->state != RECV_M3) {
2481 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2482 "receiving M3", wps->state);
2483 wps->state = SEND_WSC_NACK;
2484 return WPS_CONTINUE;
2487 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2488 !wps_registrar_skip_overlap(wps)) {
2489 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2491 wps->state = SEND_WSC_NACK;
2492 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2493 return WPS_CONTINUE;
2496 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2497 wps_process_authenticator(wps, attr->authenticator, msg) ||
2498 wps_process_e_hash1(wps, attr->e_hash1) ||
2499 wps_process_e_hash2(wps, attr->e_hash2)) {
2500 wps->state = SEND_WSC_NACK;
2501 return WPS_CONTINUE;
2504 wps->state = SEND_M4;
2505 return WPS_CONTINUE;
2509 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2510 const struct wpabuf *msg,
2511 struct wps_parse_attr *attr)
2513 struct wpabuf *decrypted;
2514 struct wps_parse_attr eattr;
2516 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2518 if (wps->state != RECV_M5) {
2519 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2520 "receiving M5", wps->state);
2521 wps->state = SEND_WSC_NACK;
2522 return WPS_CONTINUE;
2525 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2526 !wps_registrar_skip_overlap(wps)) {
2527 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2529 wps->state = SEND_WSC_NACK;
2530 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2531 return WPS_CONTINUE;
2534 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2535 wps_process_authenticator(wps, attr->authenticator, msg)) {
2536 wps->state = SEND_WSC_NACK;
2537 return WPS_CONTINUE;
2540 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2541 attr->encr_settings_len);
2542 if (decrypted == NULL) {
2543 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2544 "Settings attribute");
2545 wps->state = SEND_WSC_NACK;
2546 return WPS_CONTINUE;
2549 if (wps_validate_m5_encr(decrypted, attr->version2 != NULL) < 0) {
2550 wpabuf_free(decrypted);
2551 wps->state = SEND_WSC_NACK;
2552 return WPS_CONTINUE;
2555 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2557 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2558 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2559 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2560 wpabuf_free(decrypted);
2561 wps->state = SEND_WSC_NACK;
2562 return WPS_CONTINUE;
2564 wpabuf_free(decrypted);
2566 wps->state = SEND_M6;
2567 return WPS_CONTINUE;
2571 static void wps_sta_cred_cb(struct wps_data *wps)
2574 * Update credential to only include a single authentication and
2575 * encryption type in case the AP configuration includes more than one
2578 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2579 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2580 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2581 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2582 if (wps->cred.encr_type & WPS_ENCR_AES)
2583 wps->cred.encr_type = WPS_ENCR_AES;
2584 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2585 wps->cred.encr_type = WPS_ENCR_TKIP;
2586 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2587 "AP configuration");
2588 if (wps->wps->cred_cb)
2589 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2593 static void wps_cred_update(struct wps_credential *dst,
2594 struct wps_credential *src)
2596 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2597 dst->ssid_len = src->ssid_len;
2598 dst->auth_type = src->auth_type;
2599 dst->encr_type = src->encr_type;
2600 dst->key_idx = src->key_idx;
2601 os_memcpy(dst->key, src->key, sizeof(dst->key));
2602 dst->key_len = src->key_len;
2606 static int wps_process_ap_settings_r(struct wps_data *wps,
2607 struct wps_parse_attr *attr)
2611 if (wps->wps->ap || wps->er)
2614 /* AP Settings Attributes in M7 when Enrollee is an AP */
2615 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2618 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2620 if (wps->new_ap_settings) {
2621 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2623 wps_cred_update(&wps->cred, wps->new_ap_settings);
2627 * Use the AP PIN only to receive the current AP settings, not
2628 * to reconfigure the AP.
2632 * Clear selected registrar here since we do not get to
2633 * WSC_Done in this protocol run.
2635 wps_registrar_pin_completed(wps->wps->registrar);
2637 msg = wps_build_ap_cred(wps);
2640 wps->cred.cred_attr = wpabuf_head(msg);
2641 wps->cred.cred_attr_len = wpabuf_len(msg);
2643 if (wps->ap_settings_cb) {
2644 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2649 wps_sta_cred_cb(wps);
2651 wps->cred.cred_attr = NULL;
2652 wps->cred.cred_attr_len = 0;
2660 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2661 const struct wpabuf *msg,
2662 struct wps_parse_attr *attr)
2664 struct wpabuf *decrypted;
2665 struct wps_parse_attr eattr;
2667 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2669 if (wps->state != RECV_M7) {
2670 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2671 "receiving M7", wps->state);
2672 wps->state = SEND_WSC_NACK;
2673 return WPS_CONTINUE;
2676 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2677 !wps_registrar_skip_overlap(wps)) {
2678 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2680 wps->state = SEND_WSC_NACK;
2681 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2682 return WPS_CONTINUE;
2685 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2686 wps_process_authenticator(wps, attr->authenticator, msg)) {
2687 wps->state = SEND_WSC_NACK;
2688 return WPS_CONTINUE;
2691 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2692 attr->encr_settings_len);
2693 if (decrypted == NULL) {
2694 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2695 "Settings attribute");
2696 wps->state = SEND_WSC_NACK;
2697 return WPS_CONTINUE;
2700 if (wps_validate_m7_encr(decrypted, wps->wps->ap || wps->er,
2701 attr->version2 != NULL) < 0) {
2702 wpabuf_free(decrypted);
2703 wps->state = SEND_WSC_NACK;
2704 return WPS_CONTINUE;
2707 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2709 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2710 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2711 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2712 wps_process_ap_settings_r(wps, &eattr)) {
2713 wpabuf_free(decrypted);
2714 wps->state = SEND_WSC_NACK;
2715 return WPS_CONTINUE;
2718 wpabuf_free(decrypted);
2720 wps->state = SEND_M8;
2721 return WPS_CONTINUE;
2725 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2726 const struct wpabuf *msg)
2728 struct wps_parse_attr attr;
2729 enum wps_process_res ret = WPS_CONTINUE;
2731 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2733 if (wps_parse_msg(msg, &attr) < 0)
2736 if (attr.msg_type == NULL) {
2737 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2738 wps->state = SEND_WSC_NACK;
2739 return WPS_CONTINUE;
2742 if (*attr.msg_type != WPS_M1 &&
2743 (attr.registrar_nonce == NULL ||
2744 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2745 WPS_NONCE_LEN != 0))) {
2746 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2750 switch (*attr.msg_type) {
2752 if (wps_validate_m1(msg) < 0)
2754 #ifdef CONFIG_WPS_UPNP
2755 if (wps->wps->wps_upnp && attr.mac_addr) {
2756 /* Remove old pending messages when starting new run */
2757 wps_free_pending_msgs(wps->wps->upnp_msgs);
2758 wps->wps->upnp_msgs = NULL;
2760 upnp_wps_device_send_wlan_event(
2761 wps->wps->wps_upnp, attr.mac_addr,
2762 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2764 #endif /* CONFIG_WPS_UPNP */
2765 ret = wps_process_m1(wps, &attr);
2768 if (wps_validate_m3(msg) < 0)
2770 ret = wps_process_m3(wps, msg, &attr);
2771 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2772 wps_fail_event(wps->wps, WPS_M3, wps->config_error,
2773 wps->error_indication);
2776 if (wps_validate_m5(msg) < 0)
2778 ret = wps_process_m5(wps, msg, &attr);
2779 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2780 wps_fail_event(wps->wps, WPS_M5, wps->config_error,
2781 wps->error_indication);
2784 if (wps_validate_m7(msg) < 0)
2786 ret = wps_process_m7(wps, msg, &attr);
2787 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2788 wps_fail_event(wps->wps, WPS_M7, wps->config_error,
2789 wps->error_indication);
2792 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2797 if (ret == WPS_CONTINUE) {
2798 /* Save a copy of the last message for Authenticator derivation
2800 wpabuf_free(wps->last_msg);
2801 wps->last_msg = wpabuf_dup(msg);
2808 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2809 const struct wpabuf *msg)
2811 struct wps_parse_attr attr;
2813 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2815 if (wps_parse_msg(msg, &attr) < 0)
2818 if (attr.msg_type == NULL) {
2819 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2823 if (*attr.msg_type != WPS_WSC_ACK) {
2824 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2829 #ifdef CONFIG_WPS_UPNP
2830 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2831 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2832 if (wps->wps->upnp_msgs)
2833 return WPS_CONTINUE;
2834 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2835 "external Registrar");
2838 #endif /* CONFIG_WPS_UPNP */
2840 if (attr.registrar_nonce == NULL ||
2841 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2843 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2847 if (attr.enrollee_nonce == NULL ||
2848 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2849 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2853 if (wps->state == RECV_M2D_ACK) {
2854 #ifdef CONFIG_WPS_UPNP
2855 if (wps->wps->wps_upnp &&
2856 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2857 if (wps->wps->upnp_msgs)
2858 return WPS_CONTINUE;
2859 if (wps->ext_reg == 0)
2861 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2862 "external Registrar");
2865 #endif /* CONFIG_WPS_UPNP */
2867 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2868 "terminate negotiation");
2875 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2876 const struct wpabuf *msg)
2878 struct wps_parse_attr attr;
2882 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2884 old_state = wps->state;
2885 wps->state = SEND_WSC_NACK;
2887 if (wps_parse_msg(msg, &attr) < 0)
2890 if (attr.msg_type == NULL) {
2891 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2895 if (*attr.msg_type != WPS_WSC_NACK) {
2896 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2901 #ifdef CONFIG_WPS_UPNP
2902 if (wps->wps->wps_upnp && wps->ext_reg) {
2903 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2904 "Registrar terminated by the Enrollee");
2907 #endif /* CONFIG_WPS_UPNP */
2909 if (attr.registrar_nonce == NULL ||
2910 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2912 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2916 if (attr.enrollee_nonce == NULL ||
2917 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2918 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2922 if (attr.config_error == NULL) {
2923 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2928 config_error = WPA_GET_BE16(attr.config_error);
2929 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2930 "Configuration Error %d", config_error);
2932 switch (old_state) {
2934 wps_fail_event(wps->wps, WPS_M2, config_error,
2935 wps->error_indication);
2938 wps_fail_event(wps->wps, WPS_M4, config_error,
2939 wps->error_indication);
2942 wps_fail_event(wps->wps, WPS_M6, config_error,
2943 wps->error_indication);
2946 wps_fail_event(wps->wps, WPS_M8, config_error,
2947 wps->error_indication);
2957 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2958 const struct wpabuf *msg)
2960 struct wps_parse_attr attr;
2962 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2964 if (wps->state != RECV_DONE &&
2965 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2966 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2967 "receiving WSC_Done", wps->state);
2971 if (wps_parse_msg(msg, &attr) < 0)
2974 if (attr.msg_type == NULL) {
2975 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2979 if (*attr.msg_type != WPS_WSC_DONE) {
2980 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2985 #ifdef CONFIG_WPS_UPNP
2986 if (wps->wps->wps_upnp && wps->ext_reg) {
2987 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2988 "Registrar completed successfully");
2989 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2993 #endif /* CONFIG_WPS_UPNP */
2995 if (attr.registrar_nonce == NULL ||
2996 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2998 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3002 if (attr.enrollee_nonce == NULL ||
3003 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
3004 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3008 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
3009 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3012 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
3013 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
3014 struct wps_credential cred;
3016 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
3017 "on first Enrollee connection");
3019 os_memset(&cred, 0, sizeof(cred));
3020 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
3021 cred.ssid_len = wps->wps->ssid_len;
3022 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
3023 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
3024 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
3025 cred.key_len = wps->new_psk_len;
3027 wps->wps->wps_state = WPS_STATE_CONFIGURED;
3028 wpa_hexdump_ascii_key(MSG_DEBUG,
3029 "WPS: Generated random passphrase",
3030 wps->new_psk, wps->new_psk_len);
3031 if (wps->wps->cred_cb)
3032 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
3034 os_free(wps->new_psk);
3035 wps->new_psk = NULL;
3038 if (!wps->wps->ap && !wps->er)
3039 wps_sta_cred_cb(wps);
3042 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
3043 wps->new_psk, wps->new_psk_len)) {
3044 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
3047 os_free(wps->new_psk);
3048 wps->new_psk = NULL;
3051 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
3054 wps_registrar_remove_pbc_session(wps->wps->registrar,
3057 wps_registrar_pbc_completed(wps->wps->registrar);
3059 wps_registrar_pin_completed(wps->wps->registrar);
3061 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
3062 * merge them into APs own list.. */
3064 wps_success_event(wps->wps);
3070 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
3071 enum wsc_op_code op_code,
3072 const struct wpabuf *msg)
3074 enum wps_process_res ret;
3076 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
3078 (unsigned long) wpabuf_len(msg), op_code);
3080 #ifdef CONFIG_WPS_UPNP
3081 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
3082 struct wps_parse_attr attr;
3083 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
3084 *attr.msg_type == WPS_M3)
3085 wps->ext_reg = 2; /* past M2/M2D phase */
3087 if (wps->ext_reg > 1)
3088 wps_registrar_free_pending_m2(wps->wps);
3089 if (wps->wps->wps_upnp && wps->ext_reg &&
3090 wps->wps->upnp_msgs == NULL &&
3091 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
3093 struct wps_parse_attr attr;
3095 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
3098 type = *attr.msg_type;
3099 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
3100 " to external Registrar for processing", type);
3101 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
3103 UPNP_WPS_WLANEVENT_TYPE_EAP,
3105 if (op_code == WSC_MSG)
3107 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
3108 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
3109 "external Registrar");
3110 return WPS_CONTINUE;
3112 #endif /* CONFIG_WPS_UPNP */
3116 return wps_process_wsc_msg(wps, msg);
3118 if (wps_validate_wsc_ack(msg) < 0)
3120 return wps_process_wsc_ack(wps, msg);
3122 if (wps_validate_wsc_nack(msg) < 0)
3124 return wps_process_wsc_nack(wps, msg);
3126 if (wps_validate_wsc_done(msg) < 0)
3128 ret = wps_process_wsc_done(wps, msg);
3129 if (ret == WPS_FAILURE) {
3130 wps->state = SEND_WSC_NACK;
3131 wps_fail_event(wps->wps, WPS_WSC_DONE,
3133 wps->error_indication);
3137 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
3143 int wps_registrar_update_ie(struct wps_registrar *reg)
3145 return wps_set_ie(reg);
3149 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
3152 struct wps_registrar *reg = eloop_ctx;
3154 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
3155 "unselect internal Registrar");
3156 reg->selected_registrar = 0;
3158 wps_registrar_selected_registrar_changed(reg);
3162 #ifdef CONFIG_WPS_UPNP
3163 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
3164 struct subscription *s)
3167 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
3168 "config_methods=0x%x)",
3169 s->dev_password_id, s->config_methods);
3170 reg->sel_reg_union = 1;
3171 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
3172 reg->sel_reg_dev_password_id_override = s->dev_password_id;
3173 if (reg->sel_reg_config_methods_override == -1)
3174 reg->sel_reg_config_methods_override = 0;
3175 reg->sel_reg_config_methods_override |= s->config_methods;
3176 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
3177 if (is_zero_ether_addr(reg->authorized_macs_union[i]))
3179 for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
3181 if (is_zero_ether_addr(s->authorized_macs[j]))
3183 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC into union: "
3184 MACSTR, MAC2STR(s->authorized_macs[j]));
3185 os_memcpy(reg->authorized_macs_union[i],
3186 s->authorized_macs[j], ETH_ALEN);
3189 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union",
3190 (u8 *) reg->authorized_macs_union,
3191 sizeof(reg->authorized_macs_union));
3193 #endif /* CONFIG_WPS_UPNP */
3196 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
3198 #ifdef CONFIG_WPS_UPNP
3199 struct subscription *s;
3201 if (reg->wps->wps_upnp == NULL)
3204 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
3205 struct subscription, list) {
3206 struct subscr_addr *sa;
3207 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
3209 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
3210 inet_ntoa(sa->saddr.sin_addr),
3211 ntohs(sa->saddr.sin_port));
3213 if (s->selected_registrar)
3214 wps_registrar_sel_reg_add(reg, s);
3216 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
3219 #endif /* CONFIG_WPS_UPNP */
3224 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3225 * @reg: Registrar data from wps_registrar_init()
3227 * This function is called when selected registrar state changes, e.g., when an
3228 * AP receives a SetSelectedRegistrar UPnP message.
3230 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg)
3232 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
3234 reg->sel_reg_union = reg->selected_registrar;
3235 reg->sel_reg_dev_password_id_override = -1;
3236 reg->sel_reg_config_methods_override = -1;
3237 os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
3238 WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
3239 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union (start with own)",
3240 (u8 *) reg->authorized_macs_union,
3241 sizeof(reg->authorized_macs_union));
3242 if (reg->selected_registrar) {
3245 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
3247 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
3248 WPS_CONFIG_PHY_PUSHBUTTON);
3249 #endif /* CONFIG_WPS2 */
3251 reg->sel_reg_dev_password_id_override =
3253 wps_set_pushbutton(&methods, reg->wps->config_methods);
3255 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
3256 "(pbc=%d)", reg->pbc);
3257 reg->sel_reg_config_methods_override = methods;
3259 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
3261 wps_registrar_sel_reg_union(reg);
3264 wps_cb_set_sel_reg(reg);
3268 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
3269 char *buf, size_t buflen)
3271 struct wps_registrar_device *d;
3274 char devtype[WPS_DEV_TYPE_BUFSIZE];
3276 d = wps_device_get(reg, addr);
3279 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
3282 ret = os_snprintf(buf + len, buflen - len,
3284 "wpsPrimaryDeviceType=%s\n"
3285 "wpsDeviceName=%s\n"
3286 "wpsManufacturer=%s\n"
3288 "wpsModelNumber=%s\n"
3289 "wpsSerialNumber=%s\n",
3291 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
3293 d->dev.device_name ? d->dev.device_name : "",
3294 d->dev.manufacturer ? d->dev.manufacturer : "",
3295 d->dev.model_name ? d->dev.model_name : "",
3296 d->dev.model_number ? d->dev.model_number : "",
3297 d->dev.serial_number ? d->dev.serial_number : "");
3298 if (ret < 0 || (size_t) ret >= buflen - len)
3306 int wps_registrar_config_ap(struct wps_registrar *reg,
3307 struct wps_credential *cred)
3310 printf("encr_type=0x%x\n", cred->encr_type);
3311 if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
3313 if (cred->encr_type & WPS_ENCR_WEP) {
3314 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
3315 "due to WEP configuration");
3319 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
3320 "invalid encr_type 0x%x", cred->encr_type);
3324 if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
3326 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
3328 cred->encr_type |= WPS_ENCR_AES;
3331 if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
3333 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
3335 cred->auth_type |= WPS_AUTH_WPA2PSK;
3337 #endif /* CONFIG_WPS2 */
3339 if (reg->wps->cred_cb)
3340 return reg->wps->cred_cb(reg->wps->cb_ctx, cred);