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,
313 const u8 *addr, const u8 *uuid_e)
315 struct wps_pbc_session *pbc, *prev = NULL;
317 pbc = reg->pbc_sessions;
319 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
320 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
322 prev->next = pbc->next;
324 reg->pbc_sessions = pbc->next;
334 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
335 const u8 *addr, const u8 *uuid_e)
338 struct wps_pbc_session *pbc;
343 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
344 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
346 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
348 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
355 return count > 1 ? 1 : 0;
359 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
361 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
363 wpabuf_put_be16(msg, ATTR_WPS_STATE);
364 wpabuf_put_be16(msg, 1);
365 wpabuf_put_u8(msg, wps->wps_state);
370 #ifdef CONFIG_WPS_UPNP
371 static void wps_registrar_free_pending_m2(struct wps_context *wps)
373 struct upnp_pending_message *p, *p2, *prev = NULL;
376 if (p->type == WPS_M2 || p->type == WPS_M2D) {
378 wps->upnp_msgs = p->next;
380 prev->next = p->next;
381 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
384 wpabuf_free(p2->msg);
392 #endif /* CONFIG_WPS_UPNP */
395 static int wps_build_ap_setup_locked(struct wps_context *wps,
398 if (wps->ap_setup_locked && wps->ap_setup_locked != 2) {
399 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
400 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
401 wpabuf_put_be16(msg, 1);
402 wpabuf_put_u8(msg, 1);
408 static int wps_build_selected_registrar(struct wps_registrar *reg,
411 if (!reg->sel_reg_union)
413 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
414 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
415 wpabuf_put_be16(msg, 1);
416 wpabuf_put_u8(msg, 1);
421 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
424 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
425 if (!reg->sel_reg_union)
427 if (reg->sel_reg_dev_password_id_override >= 0)
428 id = reg->sel_reg_dev_password_id_override;
429 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
430 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
431 wpabuf_put_be16(msg, 2);
432 wpabuf_put_be16(msg, id);
437 static int wps_build_sel_pbc_reg_uuid_e(struct wps_registrar *reg,
440 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
441 if (!reg->sel_reg_union)
443 if (reg->sel_reg_dev_password_id_override >= 0)
444 id = reg->sel_reg_dev_password_id_override;
445 if (id != DEV_PW_PUSHBUTTON || !reg->dualband)
447 return wps_build_uuid_e(msg, reg->wps->uuid);
451 static void wps_set_pushbutton(u16 *methods, u16 conf_methods)
453 *methods |= WPS_CONFIG_PUSHBUTTON;
455 if (conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON)
456 *methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
457 if (conf_methods & WPS_CONFIG_PHY_PUSHBUTTON)
458 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
459 if ((*methods & WPS_CONFIG_VIRT_PUSHBUTTON) !=
460 WPS_CONFIG_VIRT_PUSHBUTTON ||
461 (*methods & WPS_CONFIG_PHY_PUSHBUTTON) !=
462 WPS_CONFIG_PHY_PUSHBUTTON) {
464 * Required to include virtual/physical flag, but we were not
465 * configured with push button type, so have to default to one
468 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
470 #endif /* CONFIG_WPS2 */
474 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
478 if (!reg->sel_reg_union)
480 methods = reg->wps->config_methods;
481 methods &= ~WPS_CONFIG_PUSHBUTTON;
483 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
484 WPS_CONFIG_PHY_PUSHBUTTON);
485 #endif /* CONFIG_WPS2 */
487 wps_set_pushbutton(&methods, reg->wps->config_methods);
488 if (reg->sel_reg_config_methods_override >= 0)
489 methods = reg->sel_reg_config_methods_override;
490 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
492 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
493 wpabuf_put_be16(msg, 2);
494 wpabuf_put_be16(msg, methods);
499 static int wps_build_probe_config_methods(struct wps_registrar *reg,
504 * These are the methods that the AP supports as an Enrollee for adding
505 * external Registrars.
507 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
509 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
510 WPS_CONFIG_PHY_PUSHBUTTON);
511 #endif /* CONFIG_WPS2 */
512 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
513 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
514 wpabuf_put_be16(msg, 2);
515 wpabuf_put_be16(msg, methods);
520 static int wps_build_config_methods_r(struct wps_registrar *reg,
524 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
526 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
527 WPS_CONFIG_PHY_PUSHBUTTON);
528 #endif /* CONFIG_WPS2 */
530 wps_set_pushbutton(&methods, reg->wps->config_methods);
531 return wps_build_config_methods(msg, methods);
535 const u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count)
540 while (*count < WPS_MAX_AUTHORIZED_MACS) {
541 if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
545 #endif /* CONFIG_WPS2 */
547 return (const u8 *) reg->authorized_macs_union;
552 * wps_registrar_init - Initialize WPS Registrar data
553 * @wps: Pointer to longterm WPS context
554 * @cfg: Registrar configuration
555 * Returns: Pointer to allocated Registrar data or %NULL on failure
557 * This function is used to initialize WPS Registrar functionality. It can be
558 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
559 * runs (e.g., when run as an internal Registrar in an AP). Caller is
560 * responsible for freeing the returned data with wps_registrar_deinit() when
561 * Registrar functionality is not needed anymore.
563 struct wps_registrar *
564 wps_registrar_init(struct wps_context *wps,
565 const struct wps_registrar_config *cfg)
567 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
571 dl_list_init(®->pins);
573 reg->new_psk_cb = cfg->new_psk_cb;
574 reg->set_ie_cb = cfg->set_ie_cb;
575 reg->pin_needed_cb = cfg->pin_needed_cb;
576 reg->reg_success_cb = cfg->reg_success_cb;
577 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
578 reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
579 reg->cb_ctx = cfg->cb_ctx;
580 reg->skip_cred_build = cfg->skip_cred_build;
581 if (cfg->extra_cred) {
582 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
583 cfg->extra_cred_len);
584 if (reg->extra_cred == NULL) {
589 reg->disable_auto_conf = cfg->disable_auto_conf;
590 reg->sel_reg_dev_password_id_override = -1;
591 reg->sel_reg_config_methods_override = -1;
592 reg->static_wep_only = cfg->static_wep_only;
593 reg->dualband = cfg->dualband;
595 if (wps_set_ie(reg)) {
596 wps_registrar_deinit(reg);
605 * wps_registrar_deinit - Deinitialize WPS Registrar data
606 * @reg: Registrar data from wps_registrar_init()
608 void wps_registrar_deinit(struct wps_registrar *reg)
612 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
613 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
614 wps_free_pins(®->pins);
615 wps_free_pbc_sessions(reg->pbc_sessions);
616 wpabuf_free(reg->extra_cred);
617 wps_free_devices(reg->devices);
623 * wps_registrar_add_pin - Configure a new PIN for Registrar
624 * @reg: Registrar data from wps_registrar_init()
625 * @addr: Enrollee MAC address or %NULL if not known
626 * @uuid: UUID-E or %NULL for wildcard (any UUID)
627 * @pin: PIN (Device Password)
628 * @pin_len: Length of pin in octets
629 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
630 * Returns: 0 on success, -1 on failure
632 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
633 const u8 *uuid, const u8 *pin, size_t pin_len,
636 struct wps_uuid_pin *p;
638 p = os_zalloc(sizeof(*p));
642 os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
644 p->wildcard_uuid = 1;
646 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
647 p->pin = os_malloc(pin_len);
648 if (p->pin == NULL) {
652 os_memcpy(p->pin, pin, pin_len);
653 p->pin_len = pin_len;
656 p->flags |= PIN_EXPIRES;
657 os_get_time(&p->expiration);
658 p->expiration.sec += timeout;
661 dl_list_add(®->pins, &p->list);
663 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
665 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
666 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
667 reg->selected_registrar = 1;
670 wps_registrar_add_authorized_mac(reg, addr);
672 wps_registrar_add_authorized_mac(
673 reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
674 wps_registrar_selected_registrar_changed(reg);
675 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
676 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
677 wps_registrar_set_selected_timeout,
684 static void wps_registrar_remove_pin(struct wps_registrar *reg,
685 struct wps_uuid_pin *pin)
688 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
690 if (is_zero_ether_addr(pin->enrollee_addr))
693 addr = pin->enrollee_addr;
694 wps_registrar_remove_authorized_mac(reg, addr);
696 wps_registrar_selected_registrar_changed(reg);
700 static void wps_registrar_expire_pins(struct wps_registrar *reg)
702 struct wps_uuid_pin *pin, *prev;
706 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
708 if ((pin->flags & PIN_EXPIRES) &&
709 os_time_before(&pin->expiration, &now)) {
710 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
711 pin->uuid, WPS_UUID_LEN);
712 wps_registrar_remove_pin(reg, pin);
719 * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
720 * @reg: Registrar data from wps_registrar_init()
721 * Returns: 0 on success, -1 if not wildcard PIN is enabled
723 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg)
725 struct wps_uuid_pin *pin, *prev;
727 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
729 if (pin->wildcard_uuid) {
730 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
731 pin->uuid, WPS_UUID_LEN);
732 wps_registrar_remove_pin(reg, pin);
742 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
743 * @reg: Registrar data from wps_registrar_init()
745 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
747 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
749 struct wps_uuid_pin *pin, *prev;
751 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
753 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
754 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
755 pin->uuid, WPS_UUID_LEN);
756 wps_registrar_remove_pin(reg, pin);
765 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
766 const u8 *uuid, size_t *pin_len)
768 struct wps_uuid_pin *pin, *found = NULL;
770 wps_registrar_expire_pins(reg);
772 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
773 if (!pin->wildcard_uuid &&
774 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
781 /* Check for wildcard UUIDs since none of the UUID-specific
783 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
784 if (pin->wildcard_uuid == 1) {
785 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
786 "PIN. Assigned it for this UUID-E");
787 pin->wildcard_uuid = 2;
788 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
799 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
800 * that could otherwise avoid PIN invalidations.
802 if (found->flags & PIN_LOCKED) {
803 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
804 "allow concurrent re-use");
807 *pin_len = found->pin_len;
808 found->flags |= PIN_LOCKED;
814 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
815 * @reg: Registrar data from wps_registrar_init()
817 * Returns: 0 on success, -1 on failure
819 * PINs are locked to enforce only one concurrent use. This function unlocks a
820 * PIN to allow it to be used again. If the specified PIN was configured using
821 * a wildcard UUID, it will be removed instead of allowing multiple uses.
823 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
825 struct wps_uuid_pin *pin;
827 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
828 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
829 if (pin->wildcard_uuid == 2) {
830 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
832 return wps_registrar_invalidate_pin(reg, uuid);
834 pin->flags &= ~PIN_LOCKED;
843 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
845 reg->selected_registrar = 0;
847 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
848 wps_registrar_remove_authorized_mac(reg,
849 (u8 *) "\xff\xff\xff\xff\xff\xff");
850 wps_registrar_selected_registrar_changed(reg);
854 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
856 struct wps_registrar *reg = eloop_ctx;
858 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
859 wps_pbc_timeout_event(reg->wps);
860 wps_registrar_stop_pbc(reg);
865 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
866 * @reg: Registrar data from wps_registrar_init()
867 * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
868 * indicates no such filtering
869 * Returns: 0 on success, -1 on failure
871 * This function is called on an AP when a push button is pushed to activate
872 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
873 * or when a PBC registration is completed.
875 int wps_registrar_button_pushed(struct wps_registrar *reg,
876 const u8 *p2p_dev_addr)
878 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
879 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
881 wps_pbc_overlap_event(reg->wps);
884 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
885 reg->force_pbc_overlap = 0;
886 reg->selected_registrar = 1;
889 os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
891 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
892 wps_registrar_add_authorized_mac(reg,
893 (u8 *) "\xff\xff\xff\xff\xff\xff");
894 wps_registrar_selected_registrar_changed(reg);
896 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
897 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
898 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
904 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
906 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
907 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
908 wps_registrar_stop_pbc(reg);
912 static void wps_registrar_pin_completed(struct wps_registrar *reg)
914 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
915 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
916 reg->selected_registrar = 0;
917 wps_registrar_selected_registrar_changed(reg);
921 int wps_registrar_wps_cancel(struct wps_registrar *reg)
924 wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
925 wps_registrar_pbc_timeout(reg, NULL);
927 } else if (reg->selected_registrar) {
929 wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
930 wps_registrar_pin_completed(reg);
931 wps_registrar_invalidate_wildcard_pin(reg);
939 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
940 * @reg: Registrar data from wps_registrar_init()
941 * @addr: MAC address of the Probe Request sender
942 * @wps_data: WPS IE contents
944 * This function is called on an AP when a Probe Request with WPS IE is
945 * received. This is used to track PBC mode use and to detect possible overlap
946 * situation with other WPS APs.
948 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
949 const struct wpabuf *wps_data,
952 struct wps_parse_attr attr;
954 wpa_hexdump_buf(MSG_MSGDUMP,
955 "WPS: Probe Request with WPS data received",
958 if (wps_parse_msg(wps_data, &attr) < 0)
961 if (attr.config_methods == NULL) {
962 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
967 if (attr.dev_password_id == NULL) {
968 wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
973 if (reg->enrollee_seen_cb && attr.uuid_e &&
974 attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
975 char *dev_name = NULL;
977 dev_name = os_zalloc(attr.dev_name_len + 1);
979 os_memcpy(dev_name, attr.dev_name,
983 reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
984 attr.primary_dev_type,
985 WPA_GET_BE16(attr.config_methods),
986 WPA_GET_BE16(attr.dev_password_id),
987 *attr.request_type, dev_name);
991 if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
992 return; /* Not PBC */
994 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
995 MACSTR, MAC2STR(addr));
996 if (attr.uuid_e == NULL) {
997 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
1002 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
1003 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
1004 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
1005 reg->force_pbc_overlap = 1;
1006 wps_pbc_overlap_event(reg->wps);
1011 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
1012 const u8 *psk, size_t psk_len)
1014 if (reg->new_psk_cb == NULL)
1017 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
1021 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
1022 const struct wps_device_data *dev)
1024 if (reg->pin_needed_cb == NULL)
1027 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
1031 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
1034 if (reg->reg_success_cb == NULL)
1037 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
1041 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
1042 struct wpabuf *probe_resp_ie)
1044 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
1048 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
1051 if (reg->set_sel_reg_cb == NULL)
1054 if (reg->selected_registrar) {
1055 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
1057 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
1058 WPS_CONFIG_PHY_PUSHBUTTON);
1059 #endif /* CONFIG_WPS2 */
1061 wps_set_pushbutton(&methods, reg->wps->config_methods);
1064 wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1065 "config_methods=0x%x pbc=%d methods=0x%x",
1066 reg->selected_registrar, reg->wps->config_methods,
1069 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
1070 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
1075 static int wps_set_ie(struct wps_registrar *reg)
1077 struct wpabuf *beacon;
1078 struct wpabuf *probe;
1079 const u8 *auth_macs;
1082 if (reg->set_ie_cb == NULL)
1085 beacon = wpabuf_alloc(400);
1088 probe = wpabuf_alloc(500);
1089 if (probe == NULL) {
1090 wpabuf_free(beacon);
1094 auth_macs = wps_authorized_macs(reg, &count);
1096 wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
1098 if (wps_build_version(beacon) ||
1099 wps_build_wps_state(reg->wps, beacon) ||
1100 wps_build_ap_setup_locked(reg->wps, beacon) ||
1101 wps_build_selected_registrar(reg, beacon) ||
1102 wps_build_sel_reg_dev_password_id(reg, beacon) ||
1103 wps_build_sel_reg_config_methods(reg, beacon) ||
1104 wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
1105 (reg->dualband && wps_build_rf_bands(®->wps->dev, beacon)) ||
1106 wps_build_wfa_ext(beacon, 0, auth_macs, count)) {
1107 wpabuf_free(beacon);
1112 wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
1114 if (wps_build_version(probe) ||
1115 wps_build_wps_state(reg->wps, probe) ||
1116 wps_build_ap_setup_locked(reg->wps, probe) ||
1117 wps_build_selected_registrar(reg, probe) ||
1118 wps_build_sel_reg_dev_password_id(reg, probe) ||
1119 wps_build_sel_reg_config_methods(reg, probe) ||
1120 wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
1121 WPS_RESP_REGISTRAR) ||
1122 wps_build_uuid_e(probe, reg->wps->uuid) ||
1123 wps_build_device_attrs(®->wps->dev, probe) ||
1124 wps_build_probe_config_methods(reg, probe) ||
1125 wps_build_rf_bands(®->wps->dev, probe) ||
1126 wps_build_wfa_ext(probe, 0, auth_macs, count)) {
1127 wpabuf_free(beacon);
1133 if (wps_build_dev_name(®->wps->dev, beacon) ||
1134 wps_build_primary_dev_type(®->wps->dev, beacon)) {
1135 wpabuf_free(beacon);
1139 #endif /* CONFIG_P2P */
1141 beacon = wps_ie_encapsulate(beacon);
1142 probe = wps_ie_encapsulate(probe);
1144 if (!beacon || !probe) {
1145 wpabuf_free(beacon);
1150 if (reg->static_wep_only) {
1152 * Windows XP and Vista clients can get confused about
1153 * EAP-Identity/Request when they probe the network with
1154 * EAPOL-Start. In such a case, they may assume the network is
1155 * using IEEE 802.1X and prompt user for a certificate while
1156 * the correct (non-WPS) behavior would be to ask for the
1157 * static WEP key. As a workaround, use Microsoft Provisioning
1158 * IE to advertise that legacy 802.1X is not supported.
1160 const u8 ms_wps[7] = {
1161 WLAN_EID_VENDOR_SPECIFIC, 5,
1162 /* Microsoft Provisioning IE (00:50:f2:5) */
1163 0x00, 0x50, 0xf2, 5,
1164 0x00 /* no legacy 802.1X or MS WPS */
1166 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
1167 "into Beacon/Probe Response frames");
1168 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
1169 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
1172 return wps_cb_set_ie(reg, beacon, probe);
1176 static int wps_get_dev_password(struct wps_data *wps)
1181 os_free(wps->dev_password);
1182 wps->dev_password = NULL;
1185 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
1186 pin = (const u8 *) "00000000";
1189 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1193 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1195 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1200 wps->dev_password = os_malloc(pin_len);
1201 if (wps->dev_password == NULL)
1203 os_memcpy(wps->dev_password, pin, pin_len);
1204 wps->dev_password_len = pin_len;
1210 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1212 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1213 wpabuf_put_be16(msg, ATTR_UUID_R);
1214 wpabuf_put_be16(msg, WPS_UUID_LEN);
1215 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1220 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1226 if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1228 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1229 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1230 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1232 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1233 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1234 "R-Hash derivation");
1238 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1239 wpabuf_put_be16(msg, ATTR_R_HASH1);
1240 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1241 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1242 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1243 addr[0] = wps->snonce;
1244 len[0] = WPS_SECRET_NONCE_LEN;
1245 addr[1] = wps->psk1;
1246 len[1] = WPS_PSK_LEN;
1247 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1248 len[2] = wpabuf_len(wps->dh_pubkey_e);
1249 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1250 len[3] = wpabuf_len(wps->dh_pubkey_r);
1251 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1252 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1254 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1255 wpabuf_put_be16(msg, ATTR_R_HASH2);
1256 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1257 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1258 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1259 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1260 addr[1] = wps->psk2;
1261 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1262 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1268 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1270 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1271 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1272 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1273 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1278 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1280 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1281 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1282 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1283 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1284 WPS_SECRET_NONCE_LEN);
1289 static int wps_build_cred_network_idx(struct wpabuf *msg,
1290 const struct wps_credential *cred)
1292 wpa_printf(MSG_DEBUG, "WPS: * Network Index (1)");
1293 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1294 wpabuf_put_be16(msg, 1);
1295 wpabuf_put_u8(msg, 1);
1300 static int wps_build_cred_ssid(struct wpabuf *msg,
1301 const struct wps_credential *cred)
1303 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1304 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID for Credential",
1305 cred->ssid, cred->ssid_len);
1306 wpabuf_put_be16(msg, ATTR_SSID);
1307 wpabuf_put_be16(msg, cred->ssid_len);
1308 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1313 static int wps_build_cred_auth_type(struct wpabuf *msg,
1314 const struct wps_credential *cred)
1316 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1318 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1319 wpabuf_put_be16(msg, 2);
1320 wpabuf_put_be16(msg, cred->auth_type);
1325 static int wps_build_cred_encr_type(struct wpabuf *msg,
1326 const struct wps_credential *cred)
1328 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1330 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1331 wpabuf_put_be16(msg, 2);
1332 wpabuf_put_be16(msg, cred->encr_type);
1337 static int wps_build_cred_network_key(struct wpabuf *msg,
1338 const struct wps_credential *cred)
1340 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1341 (int) cred->key_len);
1342 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
1343 cred->key, cred->key_len);
1344 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1345 wpabuf_put_be16(msg, cred->key_len);
1346 wpabuf_put_data(msg, cred->key, cred->key_len);
1351 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1352 const struct wps_credential *cred)
1354 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1355 MAC2STR(cred->mac_addr));
1356 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1357 wpabuf_put_be16(msg, ETH_ALEN);
1358 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1363 static int wps_build_credential(struct wpabuf *msg,
1364 const struct wps_credential *cred)
1366 if (wps_build_cred_network_idx(msg, cred) ||
1367 wps_build_cred_ssid(msg, cred) ||
1368 wps_build_cred_auth_type(msg, cred) ||
1369 wps_build_cred_encr_type(msg, cred) ||
1370 wps_build_cred_network_key(msg, cred) ||
1371 wps_build_cred_mac_addr(msg, cred))
1377 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1379 struct wpabuf *cred;
1381 if (wps->wps->registrar->skip_cred_build)
1382 goto skip_cred_build;
1384 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1385 if (wps->use_cred) {
1386 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1389 os_memset(&wps->cred, 0, sizeof(wps->cred));
1391 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1392 wps->cred.ssid_len = wps->wps->ssid_len;
1394 /* Select the best authentication and encryption type */
1395 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1396 wps->auth_type = WPS_AUTH_WPA2PSK;
1397 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1398 wps->auth_type = WPS_AUTH_WPAPSK;
1399 else if (wps->auth_type & WPS_AUTH_OPEN)
1400 wps->auth_type = WPS_AUTH_OPEN;
1401 else if (wps->auth_type & WPS_AUTH_SHARED)
1402 wps->auth_type = WPS_AUTH_SHARED;
1404 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1408 wps->cred.auth_type = wps->auth_type;
1410 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1411 wps->auth_type == WPS_AUTH_WPAPSK) {
1412 if (wps->encr_type & WPS_ENCR_AES)
1413 wps->encr_type = WPS_ENCR_AES;
1414 else if (wps->encr_type & WPS_ENCR_TKIP)
1415 wps->encr_type = WPS_ENCR_TKIP;
1417 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1418 "type for WPA/WPA2");
1422 if (wps->encr_type & WPS_ENCR_WEP)
1423 wps->encr_type = WPS_ENCR_WEP;
1424 else if (wps->encr_type & WPS_ENCR_NONE)
1425 wps->encr_type = WPS_ENCR_NONE;
1427 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1428 "type for non-WPA/WPA2 mode");
1432 wps->cred.encr_type = wps->encr_type;
1434 * Set MAC address in the Credential to be the Enrollee's MAC address
1436 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1438 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1439 !wps->wps->registrar->disable_auto_conf) {
1441 /* Generate a random passphrase */
1442 if (random_get_bytes(r, sizeof(r)) < 0)
1444 os_free(wps->new_psk);
1445 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1446 if (wps->new_psk == NULL)
1448 wps->new_psk_len--; /* remove newline */
1449 while (wps->new_psk_len &&
1450 wps->new_psk[wps->new_psk_len - 1] == '=')
1452 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1453 wps->new_psk, wps->new_psk_len);
1454 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1455 wps->cred.key_len = wps->new_psk_len;
1456 } else if (wps->use_psk_key && wps->wps->psk_set) {
1458 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1459 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1460 os_memcpy(wps->cred.key, hex, 32 * 2);
1461 wps->cred.key_len = 32 * 2;
1462 } else if (wps->wps->network_key) {
1463 os_memcpy(wps->cred.key, wps->wps->network_key,
1464 wps->wps->network_key_len);
1465 wps->cred.key_len = wps->wps->network_key_len;
1466 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1468 /* Generate a random per-device PSK */
1469 os_free(wps->new_psk);
1470 wps->new_psk_len = 32;
1471 wps->new_psk = os_malloc(wps->new_psk_len);
1472 if (wps->new_psk == NULL)
1474 if (random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
1475 os_free(wps->new_psk);
1476 wps->new_psk = NULL;
1479 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1480 wps->new_psk, wps->new_psk_len);
1481 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1483 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1484 wps->cred.key_len = wps->new_psk_len * 2;
1488 #ifdef CONFIG_WPS_TESTING
1489 if (wps_testing_dummy_cred)
1490 cred = wpabuf_alloc(200);
1494 struct wps_credential dummy;
1495 wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
1496 os_memset(&dummy, 0, sizeof(dummy));
1497 os_memcpy(dummy.ssid, "dummy", 5);
1499 dummy.auth_type = WPS_AUTH_WPA2PSK;
1500 dummy.encr_type = WPS_ENCR_AES;
1501 os_memcpy(dummy.key, "dummy psk", 9);
1503 os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
1504 wps_build_credential(cred, &dummy);
1505 wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
1507 wpabuf_put_be16(msg, ATTR_CRED);
1508 wpabuf_put_be16(msg, wpabuf_len(cred));
1509 wpabuf_put_buf(msg, cred);
1513 #endif /* CONFIG_WPS_TESTING */
1515 cred = wpabuf_alloc(200);
1519 if (wps_build_credential(cred, &wps->cred)) {
1524 wpabuf_put_be16(msg, ATTR_CRED);
1525 wpabuf_put_be16(msg, wpabuf_len(cred));
1526 wpabuf_put_buf(msg, cred);
1530 if (wps->wps->registrar->extra_cred) {
1531 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1532 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1539 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1541 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1543 if (wps_build_credential(msg, &wps->cred))
1550 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1554 if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
1556 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1557 wps->nonce_r, WPS_NONCE_LEN);
1558 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1560 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1561 msg = wpabuf_alloc(1000);
1565 if (wps_build_version(msg) ||
1566 wps_build_msg_type(msg, WPS_M2) ||
1567 wps_build_enrollee_nonce(wps, msg) ||
1568 wps_build_registrar_nonce(wps, msg) ||
1569 wps_build_uuid_r(wps, msg) ||
1570 wps_build_public_key(wps, msg) ||
1571 wps_derive_keys(wps) ||
1572 wps_build_auth_type_flags(wps, msg) ||
1573 wps_build_encr_type_flags(wps, msg) ||
1574 wps_build_conn_type_flags(wps, msg) ||
1575 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1576 wps_build_device_attrs(&wps->wps->dev, msg) ||
1577 wps_build_rf_bands(&wps->wps->dev, msg) ||
1578 wps_build_assoc_state(wps, msg) ||
1579 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1580 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1581 wps_build_os_version(&wps->wps->dev, msg) ||
1582 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1583 wps_build_authenticator(wps, msg)) {
1589 wps->state = RECV_M3;
1594 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1597 u16 err = wps->config_error;
1599 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1600 msg = wpabuf_alloc(1000);
1604 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1605 err == WPS_CFG_NO_ERROR)
1606 err = WPS_CFG_SETUP_LOCKED;
1608 if (wps_build_version(msg) ||
1609 wps_build_msg_type(msg, WPS_M2D) ||
1610 wps_build_enrollee_nonce(wps, msg) ||
1611 wps_build_registrar_nonce(wps, msg) ||
1612 wps_build_uuid_r(wps, msg) ||
1613 wps_build_auth_type_flags(wps, msg) ||
1614 wps_build_encr_type_flags(wps, msg) ||
1615 wps_build_conn_type_flags(wps, msg) ||
1616 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1617 wps_build_device_attrs(&wps->wps->dev, msg) ||
1618 wps_build_rf_bands(&wps->wps->dev, msg) ||
1619 wps_build_assoc_state(wps, msg) ||
1620 wps_build_config_error(msg, err) ||
1621 wps_build_os_version(&wps->wps->dev, msg) ||
1622 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1627 wps->state = RECV_M2D_ACK;
1632 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1634 struct wpabuf *msg, *plain;
1636 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1638 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1640 plain = wpabuf_alloc(200);
1644 msg = wpabuf_alloc(1000);
1650 if (wps_build_version(msg) ||
1651 wps_build_msg_type(msg, WPS_M4) ||
1652 wps_build_enrollee_nonce(wps, msg) ||
1653 wps_build_r_hash(wps, msg) ||
1654 wps_build_r_snonce1(wps, plain) ||
1655 wps_build_key_wrap_auth(wps, plain) ||
1656 wps_build_encr_settings(wps, msg, plain) ||
1657 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1658 wps_build_authenticator(wps, msg)) {
1665 wps->state = RECV_M5;
1670 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1672 struct wpabuf *msg, *plain;
1674 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1676 plain = wpabuf_alloc(200);
1680 msg = wpabuf_alloc(1000);
1686 if (wps_build_version(msg) ||
1687 wps_build_msg_type(msg, WPS_M6) ||
1688 wps_build_enrollee_nonce(wps, msg) ||
1689 wps_build_r_snonce2(wps, plain) ||
1690 wps_build_key_wrap_auth(wps, plain) ||
1691 wps_build_encr_settings(wps, msg, plain) ||
1692 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1693 wps_build_authenticator(wps, msg)) {
1700 wps->wps_pin_revealed = 1;
1701 wps->state = RECV_M7;
1706 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1708 struct wpabuf *msg, *plain;
1710 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1712 plain = wpabuf_alloc(500);
1716 msg = wpabuf_alloc(1000);
1722 if (wps_build_version(msg) ||
1723 wps_build_msg_type(msg, WPS_M8) ||
1724 wps_build_enrollee_nonce(wps, msg) ||
1725 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1726 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1727 wps_build_key_wrap_auth(wps, plain) ||
1728 wps_build_encr_settings(wps, msg, plain) ||
1729 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1730 wps_build_authenticator(wps, msg)) {
1737 wps->state = RECV_DONE;
1742 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1743 enum wsc_op_code *op_code)
1747 #ifdef CONFIG_WPS_UPNP
1748 if (!wps->int_reg && wps->wps->wps_upnp) {
1749 struct upnp_pending_message *p, *prev = NULL;
1750 if (wps->ext_reg > 1)
1751 wps_registrar_free_pending_m2(wps->wps);
1752 p = wps->wps->upnp_msgs;
1753 /* TODO: check pending message MAC address */
1754 while (p && p->next) {
1759 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1764 wps->wps->upnp_msgs = NULL;
1771 *op_code = WSC_NACK;
1778 if (wps->ext_reg == 0)
1784 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1785 "pending message available");
1788 #endif /* CONFIG_WPS_UPNP */
1790 switch (wps->state) {
1792 if (wps_get_dev_password(wps) < 0)
1793 msg = wps_build_m2d(wps);
1795 msg = wps_build_m2(wps);
1799 msg = wps_build_m2d(wps);
1803 msg = wps_build_m4(wps);
1807 msg = wps_build_m6(wps);
1811 msg = wps_build_m8(wps);
1815 msg = wps_build_wsc_ack(wps);
1819 msg = wps_build_wsc_nack(wps);
1820 *op_code = WSC_NACK;
1823 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1824 "a message", wps->state);
1829 if (*op_code == WSC_MSG && msg) {
1830 /* Save a copy of the last message for Authenticator derivation
1832 wpabuf_free(wps->last_msg);
1833 wps->last_msg = wpabuf_dup(msg);
1840 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1842 if (e_nonce == NULL) {
1843 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1847 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1848 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1849 wps->nonce_e, WPS_NONCE_LEN);
1855 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1857 if (r_nonce == NULL) {
1858 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1862 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1863 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1871 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1873 if (uuid_e == NULL) {
1874 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1878 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1879 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1885 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1887 if (pw_id == NULL) {
1888 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1892 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1893 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1899 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1901 if (e_hash1 == NULL) {
1902 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1906 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1907 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1913 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1915 if (e_hash2 == NULL) {
1916 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1920 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1921 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1927 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1929 u8 hash[SHA256_MAC_LEN];
1933 if (e_snonce1 == NULL) {
1934 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1938 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1939 WPS_SECRET_NONCE_LEN);
1941 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1942 addr[0] = e_snonce1;
1943 len[0] = WPS_SECRET_NONCE_LEN;
1944 addr[1] = wps->psk1;
1945 len[1] = WPS_PSK_LEN;
1946 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1947 len[2] = wpabuf_len(wps->dh_pubkey_e);
1948 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1949 len[3] = wpabuf_len(wps->dh_pubkey_r);
1950 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1952 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1953 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1954 "not match with the pre-committed value");
1955 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1956 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1960 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1961 "half of the device password");
1967 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1969 u8 hash[SHA256_MAC_LEN];
1973 if (e_snonce2 == NULL) {
1974 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1978 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1979 WPS_SECRET_NONCE_LEN);
1981 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1982 addr[0] = e_snonce2;
1983 len[0] = WPS_SECRET_NONCE_LEN;
1984 addr[1] = wps->psk2;
1985 len[1] = WPS_PSK_LEN;
1986 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1987 len[2] = wpabuf_len(wps->dh_pubkey_e);
1988 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1989 len[3] = wpabuf_len(wps->dh_pubkey_r);
1990 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1992 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1993 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1994 "not match with the pre-committed value");
1995 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1996 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1997 wps_pwd_auth_fail_event(wps->wps, 0, 2);
2001 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
2002 "half of the device password");
2003 wps->wps_pin_revealed = 0;
2004 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
2010 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
2012 if (mac_addr == NULL) {
2013 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
2017 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
2019 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
2020 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
2026 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
2029 if (pk == NULL || pk_len == 0) {
2030 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
2034 #ifdef CONFIG_WPS_OOB
2035 if (wps->wps->oob_conf.pubkey_hash != NULL) {
2037 u8 hash[WPS_HASH_LEN];
2040 sha256_vector(1, addr, &pk_len, hash);
2042 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
2043 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
2044 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
2048 #endif /* CONFIG_WPS_OOB */
2050 wpabuf_free(wps->dh_pubkey_e);
2051 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
2052 if (wps->dh_pubkey_e == NULL)
2059 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
2064 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
2069 auth_types = WPA_GET_BE16(auth);
2071 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
2073 wps->auth_type = wps->wps->auth_types & auth_types;
2074 if (wps->auth_type == 0) {
2075 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2076 "authentication types (own 0x%x Enrollee 0x%x)",
2077 wps->wps->auth_types, auth_types);
2078 #ifdef WPS_WORKAROUNDS
2080 * Some deployed implementations seem to advertise incorrect
2081 * information in this attribute. For example, Linksys WRT350N
2082 * seems to have a byteorder bug that breaks this negotiation.
2083 * In order to interoperate with existing implementations,
2084 * assume that the Enrollee supports everything we do.
2086 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2087 "does not advertise supported authentication types "
2089 wps->auth_type = wps->wps->auth_types;
2090 #else /* WPS_WORKAROUNDS */
2092 #endif /* WPS_WORKAROUNDS */
2099 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
2104 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
2109 encr_types = WPA_GET_BE16(encr);
2111 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
2113 wps->encr_type = wps->wps->encr_types & encr_types;
2114 if (wps->encr_type == 0) {
2115 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2116 "encryption types (own 0x%x Enrollee 0x%x)",
2117 wps->wps->encr_types, encr_types);
2118 #ifdef WPS_WORKAROUNDS
2120 * Some deployed implementations seem to advertise incorrect
2121 * information in this attribute. For example, Linksys WRT350N
2122 * seems to have a byteorder bug that breaks this negotiation.
2123 * In order to interoperate with existing implementations,
2124 * assume that the Enrollee supports everything we do.
2126 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2127 "does not advertise supported encryption types "
2129 wps->encr_type = wps->wps->encr_types;
2130 #else /* WPS_WORKAROUNDS */
2132 #endif /* WPS_WORKAROUNDS */
2139 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
2142 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
2147 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
2154 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
2158 if (methods == NULL) {
2159 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
2163 m = WPA_GET_BE16(methods);
2165 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
2166 "%s%s%s%s%s%s%s%s%s", m,
2167 m & WPS_CONFIG_USBA ? " [USBA]" : "",
2168 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
2169 m & WPS_CONFIG_LABEL ? " [Label]" : "",
2170 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
2171 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
2172 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
2173 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
2174 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
2175 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
2177 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
2179 * The Enrollee does not have a display so it is unlikely to be
2180 * able to show the passphrase to a user and as such, could
2181 * benefit from receiving PSK to reduce key derivation time.
2183 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2184 "Enrollee not supporting display");
2185 wps->use_psk_key = 1;
2192 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2194 if (state == NULL) {
2195 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2200 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2207 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2211 if (assoc == NULL) {
2212 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2216 a = WPA_GET_BE16(assoc);
2217 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2223 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2228 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2232 e = WPA_GET_BE16(err);
2233 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2239 static int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
2242 struct wps_registrar *reg = wps->wps->registrar;
2244 if (is_zero_ether_addr(reg->p2p_dev_addr))
2245 return 1; /* no filtering in use */
2247 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
2248 wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
2249 "filtering for PBC: expected " MACSTR " was "
2250 MACSTR " - indicate PBC session overlap",
2251 MAC2STR(reg->p2p_dev_addr),
2252 MAC2STR(wps->p2p_dev_addr));
2255 #endif /* CONFIG_P2P */
2260 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2261 struct wps_parse_attr *attr)
2263 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2265 if (wps->state != RECV_M1) {
2266 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2267 "receiving M1", wps->state);
2271 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2272 wps_process_mac_addr(wps, attr->mac_addr) ||
2273 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2274 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2275 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2276 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2277 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2278 wps_process_config_methods(wps, attr->config_methods) ||
2279 wps_process_wps_state(wps, attr->wps_state) ||
2280 wps_process_device_attrs(&wps->peer_dev, attr) ||
2281 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2282 wps_process_assoc_state(wps, attr->assoc_state) ||
2283 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2284 wps_process_config_error(wps, attr->config_error) ||
2285 wps_process_os_version(&wps->peer_dev, attr->os_version))
2288 if (wps->dev_pw_id < 0x10 &&
2289 wps->dev_pw_id != DEV_PW_DEFAULT &&
2290 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2291 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2292 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2293 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2294 !wps->wps->registrar->pbc)) {
2295 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2297 wps->state = SEND_M2D;
2298 return WPS_CONTINUE;
2301 #ifdef CONFIG_WPS_OOB
2302 if (wps->dev_pw_id >= 0x10 &&
2303 wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
2304 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
2305 "%d mismatch", wps->dev_pw_id);
2306 wps->state = SEND_M2D;
2307 return WPS_CONTINUE;
2309 #endif /* CONFIG_WPS_OOB */
2311 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2312 if (wps->wps->registrar->force_pbc_overlap ||
2313 wps_registrar_pbc_overlap(wps->wps->registrar,
2314 wps->mac_addr_e, wps->uuid_e) ||
2315 !wps_registrar_p2p_dev_addr_match(wps)) {
2316 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2318 wps->state = SEND_M2D;
2319 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2320 wps_pbc_overlap_event(wps->wps);
2321 wps_fail_event(wps->wps, WPS_M1,
2322 WPS_CFG_MULTIPLE_PBC_DETECTED,
2324 wps->wps->registrar->force_pbc_overlap = 1;
2325 return WPS_CONTINUE;
2327 wps_registrar_add_pbc_session(wps->wps->registrar,
2328 wps->mac_addr_e, wps->uuid_e);
2332 #ifdef WPS_WORKAROUNDS
2334 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2335 * passphrase format. To avoid interop issues, force PSK format to be
2338 if (!wps->use_psk_key &&
2339 wps->peer_dev.manufacturer &&
2340 os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
2341 wps->peer_dev.model_name &&
2342 os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
2343 wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
2345 wps->use_psk_key = 1;
2347 #endif /* WPS_WORKAROUNDS */
2349 wps->state = SEND_M2;
2350 return WPS_CONTINUE;
2354 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2355 const struct wpabuf *msg,
2356 struct wps_parse_attr *attr)
2358 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2360 if (wps->state != RECV_M3) {
2361 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2362 "receiving M3", wps->state);
2363 wps->state = SEND_WSC_NACK;
2364 return WPS_CONTINUE;
2367 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2368 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2370 wps->state = SEND_WSC_NACK;
2371 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2372 return WPS_CONTINUE;
2375 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2376 wps_process_authenticator(wps, attr->authenticator, msg) ||
2377 wps_process_e_hash1(wps, attr->e_hash1) ||
2378 wps_process_e_hash2(wps, attr->e_hash2)) {
2379 wps->state = SEND_WSC_NACK;
2380 return WPS_CONTINUE;
2383 wps->state = SEND_M4;
2384 return WPS_CONTINUE;
2388 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2389 const struct wpabuf *msg,
2390 struct wps_parse_attr *attr)
2392 struct wpabuf *decrypted;
2393 struct wps_parse_attr eattr;
2395 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2397 if (wps->state != RECV_M5) {
2398 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2399 "receiving M5", wps->state);
2400 wps->state = SEND_WSC_NACK;
2401 return WPS_CONTINUE;
2404 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2405 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2407 wps->state = SEND_WSC_NACK;
2408 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2409 return WPS_CONTINUE;
2412 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2413 wps_process_authenticator(wps, attr->authenticator, msg)) {
2414 wps->state = SEND_WSC_NACK;
2415 return WPS_CONTINUE;
2418 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2419 attr->encr_settings_len);
2420 if (decrypted == NULL) {
2421 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2422 "Settings attribute");
2423 wps->state = SEND_WSC_NACK;
2424 return WPS_CONTINUE;
2427 if (wps_validate_m5_encr(decrypted, attr->version2 != NULL) < 0) {
2428 wpabuf_free(decrypted);
2429 wps->state = SEND_WSC_NACK;
2430 return WPS_CONTINUE;
2433 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2435 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2436 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2437 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2438 wpabuf_free(decrypted);
2439 wps->state = SEND_WSC_NACK;
2440 return WPS_CONTINUE;
2442 wpabuf_free(decrypted);
2444 wps->state = SEND_M6;
2445 return WPS_CONTINUE;
2449 static void wps_sta_cred_cb(struct wps_data *wps)
2452 * Update credential to only include a single authentication and
2453 * encryption type in case the AP configuration includes more than one
2456 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2457 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2458 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2459 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2460 if (wps->cred.encr_type & WPS_ENCR_AES)
2461 wps->cred.encr_type = WPS_ENCR_AES;
2462 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2463 wps->cred.encr_type = WPS_ENCR_TKIP;
2464 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2465 "AP configuration");
2466 if (wps->wps->cred_cb)
2467 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2471 static void wps_cred_update(struct wps_credential *dst,
2472 struct wps_credential *src)
2474 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2475 dst->ssid_len = src->ssid_len;
2476 dst->auth_type = src->auth_type;
2477 dst->encr_type = src->encr_type;
2478 dst->key_idx = src->key_idx;
2479 os_memcpy(dst->key, src->key, sizeof(dst->key));
2480 dst->key_len = src->key_len;
2484 static int wps_process_ap_settings_r(struct wps_data *wps,
2485 struct wps_parse_attr *attr)
2487 if (wps->wps->ap || wps->er)
2490 /* AP Settings Attributes in M7 when Enrollee is an AP */
2491 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2494 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2496 if (wps->new_ap_settings) {
2497 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2499 wps_cred_update(&wps->cred, wps->new_ap_settings);
2503 * Use the AP PIN only to receive the current AP settings, not
2504 * to reconfigure the AP.
2508 * Clear selected registrar here since we do not get to
2509 * WSC_Done in this protocol run.
2511 wps_registrar_pin_completed(wps->wps->registrar);
2513 if (wps->ap_settings_cb) {
2514 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2518 wps_sta_cred_cb(wps);
2524 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2525 const struct wpabuf *msg,
2526 struct wps_parse_attr *attr)
2528 struct wpabuf *decrypted;
2529 struct wps_parse_attr eattr;
2531 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2533 if (wps->state != RECV_M7) {
2534 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2535 "receiving M7", wps->state);
2536 wps->state = SEND_WSC_NACK;
2537 return WPS_CONTINUE;
2540 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2541 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2543 wps->state = SEND_WSC_NACK;
2544 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2545 return WPS_CONTINUE;
2548 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2549 wps_process_authenticator(wps, attr->authenticator, msg)) {
2550 wps->state = SEND_WSC_NACK;
2551 return WPS_CONTINUE;
2554 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2555 attr->encr_settings_len);
2556 if (decrypted == NULL) {
2557 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2558 "Settings attribute");
2559 wps->state = SEND_WSC_NACK;
2560 return WPS_CONTINUE;
2563 if (wps_validate_m7_encr(decrypted, wps->wps->ap || wps->er,
2564 attr->version2 != NULL) < 0) {
2565 wpabuf_free(decrypted);
2566 wps->state = SEND_WSC_NACK;
2567 return WPS_CONTINUE;
2570 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2572 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2573 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2574 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2575 wps_process_ap_settings_r(wps, &eattr)) {
2576 wpabuf_free(decrypted);
2577 wps->state = SEND_WSC_NACK;
2578 return WPS_CONTINUE;
2581 wpabuf_free(decrypted);
2583 wps->state = SEND_M8;
2584 return WPS_CONTINUE;
2588 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2589 const struct wpabuf *msg)
2591 struct wps_parse_attr attr;
2592 enum wps_process_res ret = WPS_CONTINUE;
2594 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2596 if (wps_parse_msg(msg, &attr) < 0)
2599 if (attr.msg_type == NULL) {
2600 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2601 wps->state = SEND_WSC_NACK;
2602 return WPS_CONTINUE;
2605 if (*attr.msg_type != WPS_M1 &&
2606 (attr.registrar_nonce == NULL ||
2607 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2608 WPS_NONCE_LEN != 0))) {
2609 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2613 switch (*attr.msg_type) {
2615 if (wps_validate_m1(msg) < 0)
2617 #ifdef CONFIG_WPS_UPNP
2618 if (wps->wps->wps_upnp && attr.mac_addr) {
2619 /* Remove old pending messages when starting new run */
2620 wps_free_pending_msgs(wps->wps->upnp_msgs);
2621 wps->wps->upnp_msgs = NULL;
2623 upnp_wps_device_send_wlan_event(
2624 wps->wps->wps_upnp, attr.mac_addr,
2625 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2627 #endif /* CONFIG_WPS_UPNP */
2628 ret = wps_process_m1(wps, &attr);
2631 if (wps_validate_m3(msg) < 0)
2633 ret = wps_process_m3(wps, msg, &attr);
2634 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2635 wps_fail_event(wps->wps, WPS_M3, wps->config_error,
2636 wps->error_indication);
2639 if (wps_validate_m5(msg) < 0)
2641 ret = wps_process_m5(wps, msg, &attr);
2642 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2643 wps_fail_event(wps->wps, WPS_M5, wps->config_error,
2644 wps->error_indication);
2647 if (wps_validate_m7(msg) < 0)
2649 ret = wps_process_m7(wps, msg, &attr);
2650 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2651 wps_fail_event(wps->wps, WPS_M7, wps->config_error,
2652 wps->error_indication);
2655 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2660 if (ret == WPS_CONTINUE) {
2661 /* Save a copy of the last message for Authenticator derivation
2663 wpabuf_free(wps->last_msg);
2664 wps->last_msg = wpabuf_dup(msg);
2671 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2672 const struct wpabuf *msg)
2674 struct wps_parse_attr attr;
2676 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2678 if (wps_parse_msg(msg, &attr) < 0)
2681 if (attr.msg_type == NULL) {
2682 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2686 if (*attr.msg_type != WPS_WSC_ACK) {
2687 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2692 #ifdef CONFIG_WPS_UPNP
2693 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2694 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2695 if (wps->wps->upnp_msgs)
2696 return WPS_CONTINUE;
2697 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2698 "external Registrar");
2701 #endif /* CONFIG_WPS_UPNP */
2703 if (attr.registrar_nonce == NULL ||
2704 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2706 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2710 if (attr.enrollee_nonce == NULL ||
2711 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2712 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2716 if (wps->state == RECV_M2D_ACK) {
2717 #ifdef CONFIG_WPS_UPNP
2718 if (wps->wps->wps_upnp &&
2719 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2720 if (wps->wps->upnp_msgs)
2721 return WPS_CONTINUE;
2722 if (wps->ext_reg == 0)
2724 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2725 "external Registrar");
2728 #endif /* CONFIG_WPS_UPNP */
2730 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2731 "terminate negotiation");
2738 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2739 const struct wpabuf *msg)
2741 struct wps_parse_attr attr;
2745 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2747 old_state = wps->state;
2748 wps->state = SEND_WSC_NACK;
2750 if (wps_parse_msg(msg, &attr) < 0)
2753 if (attr.msg_type == NULL) {
2754 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2758 if (*attr.msg_type != WPS_WSC_NACK) {
2759 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2764 #ifdef CONFIG_WPS_UPNP
2765 if (wps->wps->wps_upnp && wps->ext_reg) {
2766 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2767 "Registrar terminated by the Enrollee");
2770 #endif /* CONFIG_WPS_UPNP */
2772 if (attr.registrar_nonce == NULL ||
2773 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2775 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2779 if (attr.enrollee_nonce == NULL ||
2780 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2781 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2785 if (attr.config_error == NULL) {
2786 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2791 config_error = WPA_GET_BE16(attr.config_error);
2792 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2793 "Configuration Error %d", config_error);
2795 switch (old_state) {
2797 wps_fail_event(wps->wps, WPS_M2, config_error,
2798 wps->error_indication);
2801 wps_fail_event(wps->wps, WPS_M4, config_error,
2802 wps->error_indication);
2805 wps_fail_event(wps->wps, WPS_M6, config_error,
2806 wps->error_indication);
2809 wps_fail_event(wps->wps, WPS_M8, config_error,
2810 wps->error_indication);
2820 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2821 const struct wpabuf *msg)
2823 struct wps_parse_attr attr;
2825 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2827 if (wps->state != RECV_DONE &&
2828 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2829 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2830 "receiving WSC_Done", wps->state);
2834 if (wps_parse_msg(msg, &attr) < 0)
2837 if (attr.msg_type == NULL) {
2838 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2842 if (*attr.msg_type != WPS_WSC_DONE) {
2843 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2848 #ifdef CONFIG_WPS_UPNP
2849 if (wps->wps->wps_upnp && wps->ext_reg) {
2850 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2851 "Registrar completed successfully");
2852 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2856 #endif /* CONFIG_WPS_UPNP */
2858 if (attr.registrar_nonce == NULL ||
2859 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2861 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2865 if (attr.enrollee_nonce == NULL ||
2866 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2867 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2871 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2872 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2875 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2876 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2877 struct wps_credential cred;
2879 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2880 "on first Enrollee connection");
2882 os_memset(&cred, 0, sizeof(cred));
2883 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2884 cred.ssid_len = wps->wps->ssid_len;
2885 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2886 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2887 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2888 cred.key_len = wps->new_psk_len;
2890 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2891 wpa_hexdump_ascii_key(MSG_DEBUG,
2892 "WPS: Generated random passphrase",
2893 wps->new_psk, wps->new_psk_len);
2894 if (wps->wps->cred_cb)
2895 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2897 os_free(wps->new_psk);
2898 wps->new_psk = NULL;
2901 if (!wps->wps->ap && !wps->er)
2902 wps_sta_cred_cb(wps);
2905 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2906 wps->new_psk, wps->new_psk_len)) {
2907 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2910 os_free(wps->new_psk);
2911 wps->new_psk = NULL;
2914 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2917 wps_registrar_remove_pbc_session(wps->wps->registrar,
2918 wps->mac_addr_e, wps->uuid_e);
2919 wps_registrar_pbc_completed(wps->wps->registrar);
2921 wps_registrar_pin_completed(wps->wps->registrar);
2923 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
2924 * merge them into APs own list.. */
2926 wps_success_event(wps->wps);
2932 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2933 enum wsc_op_code op_code,
2934 const struct wpabuf *msg)
2936 enum wps_process_res ret;
2938 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2940 (unsigned long) wpabuf_len(msg), op_code);
2942 #ifdef CONFIG_WPS_UPNP
2943 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2944 struct wps_parse_attr attr;
2945 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2946 *attr.msg_type == WPS_M3)
2947 wps->ext_reg = 2; /* past M2/M2D phase */
2949 if (wps->ext_reg > 1)
2950 wps_registrar_free_pending_m2(wps->wps);
2951 if (wps->wps->wps_upnp && wps->ext_reg &&
2952 wps->wps->upnp_msgs == NULL &&
2953 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
2955 struct wps_parse_attr attr;
2957 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2960 type = *attr.msg_type;
2961 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2962 " to external Registrar for processing", type);
2963 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2965 UPNP_WPS_WLANEVENT_TYPE_EAP,
2967 if (op_code == WSC_MSG)
2969 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2970 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2971 "external Registrar");
2972 return WPS_CONTINUE;
2974 #endif /* CONFIG_WPS_UPNP */
2978 return wps_process_wsc_msg(wps, msg);
2980 if (wps_validate_wsc_ack(msg) < 0)
2982 return wps_process_wsc_ack(wps, msg);
2984 if (wps_validate_wsc_nack(msg) < 0)
2986 return wps_process_wsc_nack(wps, msg);
2988 if (wps_validate_wsc_done(msg) < 0)
2990 ret = wps_process_wsc_done(wps, msg);
2991 if (ret == WPS_FAILURE) {
2992 wps->state = SEND_WSC_NACK;
2993 wps_fail_event(wps->wps, WPS_WSC_DONE,
2995 wps->error_indication);
2999 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
3005 int wps_registrar_update_ie(struct wps_registrar *reg)
3007 return wps_set_ie(reg);
3011 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
3014 struct wps_registrar *reg = eloop_ctx;
3016 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
3017 "unselect internal Registrar");
3018 reg->selected_registrar = 0;
3020 wps_registrar_selected_registrar_changed(reg);
3024 #ifdef CONFIG_WPS_UPNP
3025 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
3026 struct subscription *s)
3029 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
3030 "config_methods=0x%x)",
3031 s->dev_password_id, s->config_methods);
3032 reg->sel_reg_union = 1;
3033 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
3034 reg->sel_reg_dev_password_id_override = s->dev_password_id;
3035 if (reg->sel_reg_config_methods_override == -1)
3036 reg->sel_reg_config_methods_override = 0;
3037 reg->sel_reg_config_methods_override |= s->config_methods;
3038 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
3039 if (is_zero_ether_addr(reg->authorized_macs_union[i]))
3041 for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
3043 if (is_zero_ether_addr(s->authorized_macs[j]))
3045 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC into union: "
3046 MACSTR, MAC2STR(s->authorized_macs[j]));
3047 os_memcpy(reg->authorized_macs_union[i],
3048 s->authorized_macs[j], ETH_ALEN);
3051 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union",
3052 (u8 *) reg->authorized_macs_union,
3053 sizeof(reg->authorized_macs_union));
3055 #endif /* CONFIG_WPS_UPNP */
3058 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
3060 #ifdef CONFIG_WPS_UPNP
3061 struct subscription *s;
3063 if (reg->wps->wps_upnp == NULL)
3066 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
3067 struct subscription, list) {
3068 struct subscr_addr *sa;
3069 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
3071 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
3072 inet_ntoa(sa->saddr.sin_addr),
3073 ntohs(sa->saddr.sin_port));
3075 if (s->selected_registrar)
3076 wps_registrar_sel_reg_add(reg, s);
3078 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
3081 #endif /* CONFIG_WPS_UPNP */
3086 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3087 * @reg: Registrar data from wps_registrar_init()
3089 * This function is called when selected registrar state changes, e.g., when an
3090 * AP receives a SetSelectedRegistrar UPnP message.
3092 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg)
3094 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
3096 reg->sel_reg_union = reg->selected_registrar;
3097 reg->sel_reg_dev_password_id_override = -1;
3098 reg->sel_reg_config_methods_override = -1;
3099 os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
3100 WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
3101 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union (start with own)",
3102 (u8 *) reg->authorized_macs_union,
3103 sizeof(reg->authorized_macs_union));
3104 if (reg->selected_registrar) {
3107 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
3109 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
3110 WPS_CONFIG_PHY_PUSHBUTTON);
3111 #endif /* CONFIG_WPS2 */
3113 reg->sel_reg_dev_password_id_override =
3115 wps_set_pushbutton(&methods, reg->wps->config_methods);
3117 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
3118 "(pbc=%d)", reg->pbc);
3119 reg->sel_reg_config_methods_override = methods;
3121 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
3123 wps_registrar_sel_reg_union(reg);
3126 wps_cb_set_sel_reg(reg);
3130 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
3131 char *buf, size_t buflen)
3133 struct wps_registrar_device *d;
3136 char devtype[WPS_DEV_TYPE_BUFSIZE];
3138 d = wps_device_get(reg, addr);
3141 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
3144 ret = os_snprintf(buf + len, buflen - len,
3146 "wpsPrimaryDeviceType=%s\n"
3147 "wpsDeviceName=%s\n"
3148 "wpsManufacturer=%s\n"
3150 "wpsModelNumber=%s\n"
3151 "wpsSerialNumber=%s\n",
3153 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
3155 d->dev.device_name ? d->dev.device_name : "",
3156 d->dev.manufacturer ? d->dev.manufacturer : "",
3157 d->dev.model_name ? d->dev.model_name : "",
3158 d->dev.model_number ? d->dev.model_number : "",
3159 d->dev.serial_number ? d->dev.serial_number : "");
3160 if (ret < 0 || (size_t) ret >= buflen - len)
3168 int wps_registrar_config_ap(struct wps_registrar *reg,
3169 struct wps_credential *cred)
3172 printf("encr_type=0x%x\n", cred->encr_type);
3173 if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
3175 if (cred->encr_type & WPS_ENCR_WEP) {
3176 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
3177 "due to WEP configuration");
3181 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
3182 "invalid encr_type 0x%x", cred->encr_type);
3186 if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
3188 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
3190 cred->encr_type |= WPS_ENCR_AES;
3193 if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
3195 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
3197 cred->auth_type |= WPS_AUTH_WPA2PSK;
3199 #endif /* CONFIG_WPS2 */
3201 if (reg->wps->cred_cb)
3202 return reg->wps->cred_cb(reg->wps->cb_ctx, cred);