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 "common/ieee802_11_defs.h"
26 #include "wps_dev_attr.h"
28 #include "wps_upnp_i.h"
30 #ifndef CONFIG_WPS_STRICT
31 #define WPS_WORKAROUNDS
32 #endif /* CONFIG_WPS_STRICT */
36 u8 uuid[WPS_UUID_LEN];
40 #define PIN_LOCKED BIT(0)
41 #define PIN_EXPIRES BIT(1)
43 struct os_time expiration;
44 u8 enrollee_addr[ETH_ALEN];
48 static void wps_free_pin(struct wps_uuid_pin *pin)
55 static void wps_remove_pin(struct wps_uuid_pin *pin)
57 dl_list_del(&pin->list);
62 static void wps_free_pins(struct dl_list *pins)
64 struct wps_uuid_pin *pin, *prev;
65 dl_list_for_each_safe(pin, prev, pins, struct wps_uuid_pin, list)
70 struct wps_pbc_session {
71 struct wps_pbc_session *next;
73 u8 uuid_e[WPS_UUID_LEN];
74 struct os_time timestamp;
78 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
80 struct wps_pbc_session *prev;
90 struct wps_registrar_device {
91 struct wps_registrar_device *next;
92 struct wps_device_data dev;
93 u8 uuid[WPS_UUID_LEN];
97 struct wps_registrar {
98 struct wps_context *wps;
101 int selected_registrar;
103 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
105 int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
106 struct wpabuf *probe_resp_ie);
107 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
108 const struct wps_device_data *dev);
109 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
111 void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
112 u16 sel_reg_config_methods);
113 void (*enrollee_seen_cb)(void *ctx, const u8 *addr, const u8 *uuid_e,
114 const u8 *pri_dev_type, u16 config_methods,
115 u16 dev_password_id, u8 request_type,
116 const char *dev_name);
120 struct wps_pbc_session *pbc_sessions;
123 struct wpabuf *extra_cred;
124 int disable_auto_conf;
126 int sel_reg_dev_password_id_override;
127 int sel_reg_config_methods_override;
130 struct wps_registrar_device *devices;
132 int force_pbc_overlap;
134 u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
135 u8 authorized_macs_union[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
139 static int wps_set_ie(struct wps_registrar *reg);
140 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
141 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
145 static void wps_registrar_add_authorized_mac(struct wps_registrar *reg,
149 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
150 if (os_memcmp(reg->authorized_macs[i], addr, ETH_ALEN) == 0)
151 return; /* already in list */
152 for (i = WPS_MAX_AUTHORIZED_MACS - 1; i > 0; i--)
153 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i - 1],
155 os_memcpy(reg->authorized_macs[0], addr, ETH_ALEN);
159 static void wps_registrar_remove_authorized_mac(struct wps_registrar *reg,
163 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++) {
164 if (os_memcmp(reg->authorized_macs, addr, ETH_ALEN) == 0)
167 if (i == WPS_MAX_AUTHORIZED_MACS)
168 return; /* not in the list */
169 for (; i + 1 < WPS_MAX_AUTHORIZED_MACS; i++)
170 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i + 1],
172 os_memset(reg->authorized_macs[WPS_MAX_AUTHORIZED_MACS - 1], 0,
177 static void wps_free_devices(struct wps_registrar_device *dev)
179 struct wps_registrar_device *prev;
184 wps_device_data_free(&prev->dev);
190 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
193 struct wps_registrar_device *dev;
195 for (dev = reg->devices; dev; dev = dev->next) {
196 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
203 static void wps_device_clone_data(struct wps_device_data *dst,
204 struct wps_device_data *src)
206 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
207 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
209 #define WPS_STRDUP(n) \
211 dst->n = src->n ? os_strdup(src->n) : NULL
213 WPS_STRDUP(device_name);
214 WPS_STRDUP(manufacturer);
215 WPS_STRDUP(model_name);
216 WPS_STRDUP(model_number);
217 WPS_STRDUP(serial_number);
222 int wps_device_store(struct wps_registrar *reg,
223 struct wps_device_data *dev, const u8 *uuid)
225 struct wps_registrar_device *d;
227 d = wps_device_get(reg, dev->mac_addr);
229 d = os_zalloc(sizeof(*d));
232 d->next = reg->devices;
236 wps_device_clone_data(&d->dev, dev);
237 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
243 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
244 const u8 *addr, const u8 *uuid_e)
246 struct wps_pbc_session *pbc, *prev = NULL;
251 pbc = reg->pbc_sessions;
253 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
254 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
256 prev->next = pbc->next;
258 reg->pbc_sessions = pbc->next;
266 pbc = os_zalloc(sizeof(*pbc));
269 os_memcpy(pbc->addr, addr, ETH_ALEN);
271 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
274 pbc->next = reg->pbc_sessions;
275 reg->pbc_sessions = pbc;
276 pbc->timestamp = now;
278 /* remove entries that have timed out */
283 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
285 wps_free_pbc_sessions(pbc);
294 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
295 const u8 *addr, const u8 *uuid_e)
297 struct wps_pbc_session *pbc, *prev = NULL;
299 pbc = reg->pbc_sessions;
301 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
302 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
304 prev->next = pbc->next;
306 reg->pbc_sessions = pbc->next;
316 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
317 const u8 *addr, const u8 *uuid_e)
320 struct wps_pbc_session *pbc;
325 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
326 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
328 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
330 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
337 return count > 1 ? 1 : 0;
341 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
343 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
345 wpabuf_put_be16(msg, ATTR_WPS_STATE);
346 wpabuf_put_be16(msg, 1);
347 wpabuf_put_u8(msg, wps->wps_state);
352 #ifdef CONFIG_WPS_UPNP
353 static void wps_registrar_free_pending_m2(struct wps_context *wps)
355 struct upnp_pending_message *p, *p2, *prev = NULL;
358 if (p->type == WPS_M2 || p->type == WPS_M2D) {
360 wps->upnp_msgs = p->next;
362 prev->next = p->next;
363 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
366 wpabuf_free(p2->msg);
374 #endif /* CONFIG_WPS_UPNP */
377 static int wps_build_ap_setup_locked(struct wps_context *wps,
380 if (wps->ap_setup_locked) {
381 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
382 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
383 wpabuf_put_be16(msg, 1);
384 wpabuf_put_u8(msg, 1);
390 static int wps_build_selected_registrar(struct wps_registrar *reg,
393 if (!reg->sel_reg_union)
395 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
396 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
397 wpabuf_put_be16(msg, 1);
398 wpabuf_put_u8(msg, 1);
403 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
406 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
407 if (!reg->sel_reg_union)
409 if (reg->sel_reg_dev_password_id_override >= 0)
410 id = reg->sel_reg_dev_password_id_override;
411 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
412 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
413 wpabuf_put_be16(msg, 2);
414 wpabuf_put_be16(msg, id);
419 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
423 if (!reg->sel_reg_union)
425 methods = reg->wps->config_methods &
426 ~(WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
427 WPS_CONFIG_PHY_PUSHBUTTON);
429 methods |= WPS_CONFIG_PUSHBUTTON;
430 if (reg->wps->config_methods & WPS_CONFIG_VIRT_PUSHBUTTON)
431 methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
432 if (reg->wps->config_methods & WPS_CONFIG_PHY_PUSHBUTTON)
433 methods |= WPS_CONFIG_PHY_PUSHBUTTON;
435 if (reg->sel_reg_config_methods_override >= 0)
436 methods = reg->sel_reg_config_methods_override;
437 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
439 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
440 wpabuf_put_be16(msg, 2);
441 wpabuf_put_be16(msg, methods);
446 static int wps_build_probe_config_methods(struct wps_registrar *reg,
451 * These are the methods that the AP supports as an Enrollee for adding
452 * external Registrars.
454 methods = reg->wps->config_methods &
455 ~(WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
456 WPS_CONFIG_PHY_PUSHBUTTON);
457 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
458 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
459 wpabuf_put_be16(msg, 2);
460 wpabuf_put_be16(msg, methods);
465 static int wps_build_config_methods_r(struct wps_registrar *reg,
469 methods = reg->wps->config_methods &
470 ~(WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
471 WPS_CONFIG_PHY_PUSHBUTTON);
473 methods |= WPS_CONFIG_PUSHBUTTON;
474 if (reg->wps->config_methods & WPS_CONFIG_VIRT_PUSHBUTTON)
475 methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
476 if (reg->wps->config_methods & WPS_CONFIG_PHY_PUSHBUTTON)
477 methods |= WPS_CONFIG_PHY_PUSHBUTTON;
479 return wps_build_config_methods(msg, methods);
483 int wps_build_authorized_macs(struct wps_registrar *reg, struct wpabuf *msg)
487 while (count < WPS_MAX_AUTHORIZED_MACS) {
488 if (is_zero_ether_addr(reg->authorized_macs_union[count]))
496 wpa_printf(MSG_DEBUG, "WPS: * AuthorizedMACs (count=%d)", count);
497 wpabuf_put_be16(msg, ATTR_AUTHORIZED_MACS);
498 wpabuf_put_be16(msg, count * ETH_ALEN);
499 wpabuf_put_data(msg, reg->authorized_macs_union, count * ETH_ALEN);
506 * wps_registrar_init - Initialize WPS Registrar data
507 * @wps: Pointer to longterm WPS context
508 * @cfg: Registrar configuration
509 * Returns: Pointer to allocated Registrar data or %NULL on failure
511 * This function is used to initialize WPS Registrar functionality. It can be
512 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
513 * runs (e.g., when run as an internal Registrar in an AP). Caller is
514 * responsible for freeing the returned data with wps_registrar_deinit() when
515 * Registrar functionality is not needed anymore.
517 struct wps_registrar *
518 wps_registrar_init(struct wps_context *wps,
519 const struct wps_registrar_config *cfg)
521 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
525 dl_list_init(®->pins);
527 reg->new_psk_cb = cfg->new_psk_cb;
528 reg->set_ie_cb = cfg->set_ie_cb;
529 reg->pin_needed_cb = cfg->pin_needed_cb;
530 reg->reg_success_cb = cfg->reg_success_cb;
531 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
532 reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
533 reg->cb_ctx = cfg->cb_ctx;
534 reg->skip_cred_build = cfg->skip_cred_build;
535 if (cfg->extra_cred) {
536 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
537 cfg->extra_cred_len);
538 if (reg->extra_cred == NULL) {
543 reg->disable_auto_conf = cfg->disable_auto_conf;
544 reg->sel_reg_dev_password_id_override = -1;
545 reg->sel_reg_config_methods_override = -1;
546 reg->static_wep_only = cfg->static_wep_only;
548 if (wps_set_ie(reg)) {
549 wps_registrar_deinit(reg);
558 * wps_registrar_deinit - Deinitialize WPS Registrar data
559 * @reg: Registrar data from wps_registrar_init()
561 void wps_registrar_deinit(struct wps_registrar *reg)
565 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
566 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
567 wps_free_pins(®->pins);
568 wps_free_pbc_sessions(reg->pbc_sessions);
569 wpabuf_free(reg->extra_cred);
570 wps_free_devices(reg->devices);
576 * wps_registrar_add_pin - Configure a new PIN for Registrar
577 * @reg: Registrar data from wps_registrar_init()
578 * @addr: Enrollee MAC address or %NULL if not known
579 * @uuid: UUID-E or %NULL for wildcard (any UUID)
580 * @pin: PIN (Device Password)
581 * @pin_len: Length of pin in octets
582 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
583 * Returns: 0 on success, -1 on failure
585 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
586 const u8 *uuid, const u8 *pin, size_t pin_len,
589 struct wps_uuid_pin *p;
591 p = os_zalloc(sizeof(*p));
595 os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
597 p->wildcard_uuid = 1;
599 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
600 p->pin = os_malloc(pin_len);
601 if (p->pin == NULL) {
605 os_memcpy(p->pin, pin, pin_len);
606 p->pin_len = pin_len;
609 p->flags |= PIN_EXPIRES;
610 os_get_time(&p->expiration);
611 p->expiration.sec += timeout;
614 dl_list_add(®->pins, &p->list);
616 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
618 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
619 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
620 reg->selected_registrar = 1;
623 wps_registrar_add_authorized_mac(reg, addr);
624 wps_registrar_selected_registrar_changed(reg);
625 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
626 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
627 wps_registrar_set_selected_timeout,
634 static void wps_registrar_expire_pins(struct wps_registrar *reg)
636 struct wps_uuid_pin *pin, *prev;
640 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
642 if ((pin->flags & PIN_EXPIRES) &&
643 os_time_before(&pin->expiration, &now)) {
644 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
645 pin->uuid, WPS_UUID_LEN);
646 wps_registrar_remove_authorized_mac(
647 reg, pin->enrollee_addr);
649 wps_registrar_selected_registrar_changed(reg);
656 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
657 * @reg: Registrar data from wps_registrar_init()
659 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
661 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
663 struct wps_uuid_pin *pin, *prev;
665 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
667 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
668 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
669 pin->uuid, WPS_UUID_LEN);
670 wps_registrar_remove_authorized_mac(
671 reg, pin->enrollee_addr);
673 wps_registrar_selected_registrar_changed(reg);
682 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
683 const u8 *uuid, size_t *pin_len)
685 struct wps_uuid_pin *pin, *found = NULL;
687 wps_registrar_expire_pins(reg);
689 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
690 if (!pin->wildcard_uuid &&
691 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
698 /* Check for wildcard UUIDs since none of the UUID-specific
700 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
701 if (pin->wildcard_uuid == 1) {
702 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
703 "PIN. Assigned it for this UUID-E");
704 pin->wildcard_uuid = 2;
705 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
716 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
717 * that could otherwise avoid PIN invalidations.
719 if (found->flags & PIN_LOCKED) {
720 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
721 "allow concurrent re-use");
724 *pin_len = found->pin_len;
725 found->flags |= PIN_LOCKED;
731 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
732 * @reg: Registrar data from wps_registrar_init()
734 * Returns: 0 on success, -1 on failure
736 * PINs are locked to enforce only one concurrent use. This function unlocks a
737 * PIN to allow it to be used again. If the specified PIN was configured using
738 * a wildcard UUID, it will be removed instead of allowing multiple uses.
740 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
742 struct wps_uuid_pin *pin;
744 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
745 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
746 if (pin->wildcard_uuid == 2) {
747 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
749 return wps_registrar_invalidate_pin(reg, uuid);
751 pin->flags &= ~PIN_LOCKED;
760 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
762 reg->selected_registrar = 0;
764 wps_registrar_selected_registrar_changed(reg);
768 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
770 struct wps_registrar *reg = eloop_ctx;
772 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
773 wps_pbc_timeout_event(reg->wps);
774 wps_registrar_stop_pbc(reg);
779 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
780 * @reg: Registrar data from wps_registrar_init()
781 * Returns: 0 on success, -1 on failure
783 * This function is called on an AP when a push button is pushed to activate
784 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
785 * or when a PBC registration is completed.
787 int wps_registrar_button_pushed(struct wps_registrar *reg)
789 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
790 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
792 wps_pbc_overlap_event(reg->wps);
795 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
796 reg->force_pbc_overlap = 0;
797 reg->selected_registrar = 1;
799 wps_registrar_selected_registrar_changed(reg);
801 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
802 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
808 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
810 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
811 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
812 wps_registrar_stop_pbc(reg);
816 static void wps_registrar_pin_completed(struct wps_registrar *reg)
818 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
819 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
820 reg->selected_registrar = 0;
821 wps_registrar_selected_registrar_changed(reg);
826 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
827 * @reg: Registrar data from wps_registrar_init()
828 * @addr: MAC address of the Probe Request sender
829 * @wps_data: WPS IE contents
831 * This function is called on an AP when a Probe Request with WPS IE is
832 * received. This is used to track PBC mode use and to detect possible overlap
833 * situation with other WPS APs.
835 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
836 const struct wpabuf *wps_data)
838 struct wps_parse_attr attr;
840 wpa_hexdump_buf(MSG_MSGDUMP,
841 "WPS: Probe Request with WPS data received",
844 if (wps_parse_msg(wps_data, &attr) < 0)
847 if (attr.config_methods == NULL) {
848 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
853 if (attr.dev_password_id == NULL) {
854 wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
859 if (reg->enrollee_seen_cb && attr.uuid_e &&
860 attr.primary_dev_type && attr.request_type) {
861 char *dev_name = NULL;
863 dev_name = os_zalloc(attr.dev_name_len + 1);
865 os_memcpy(dev_name, attr.dev_name,
869 reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
870 attr.primary_dev_type,
871 WPA_GET_BE16(attr.config_methods),
872 WPA_GET_BE16(attr.dev_password_id),
873 *attr.request_type, dev_name);
877 if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
878 return; /* Not PBC */
880 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
881 MACSTR, MAC2STR(addr));
882 if (attr.uuid_e == NULL) {
883 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
888 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
889 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
890 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
891 reg->force_pbc_overlap = 1;
892 wps_pbc_overlap_event(reg->wps);
897 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
898 const u8 *psk, size_t psk_len)
900 if (reg->new_psk_cb == NULL)
903 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
907 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
908 const struct wps_device_data *dev)
910 if (reg->pin_needed_cb == NULL)
913 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
917 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
920 if (reg->reg_success_cb == NULL)
923 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
927 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
928 struct wpabuf *probe_resp_ie)
930 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
934 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
937 if (reg->set_sel_reg_cb == NULL)
940 if (reg->selected_registrar) {
941 methods = reg->wps->config_methods &
942 ~(WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
943 WPS_CONFIG_PHY_PUSHBUTTON);
945 methods |= WPS_CONFIG_PUSHBUTTON;
946 if (reg->wps->config_methods &
947 WPS_CONFIG_VIRT_PUSHBUTTON)
948 methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
949 if (reg->wps->config_methods &
950 WPS_CONFIG_PHY_PUSHBUTTON)
951 methods |= WPS_CONFIG_PHY_PUSHBUTTON;
955 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
956 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
961 static int wps_set_ie(struct wps_registrar *reg)
963 struct wpabuf *beacon;
964 struct wpabuf *probe;
966 if (reg->set_ie_cb == NULL)
969 beacon = wpabuf_alloc(400);
972 probe = wpabuf_alloc(500);
978 wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
980 if (wps_build_version(beacon) ||
981 wps_build_wps_state(reg->wps, beacon) ||
982 wps_build_ap_setup_locked(reg->wps, beacon) ||
983 wps_build_selected_registrar(reg, beacon) ||
984 wps_build_sel_reg_dev_password_id(reg, beacon) ||
985 wps_build_sel_reg_config_methods(reg, beacon) ||
986 wps_build_version2(beacon) ||
987 wps_build_authorized_macs(reg, beacon)) {
993 wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
995 if (wps_build_version(probe) ||
996 wps_build_wps_state(reg->wps, probe) ||
997 wps_build_ap_setup_locked(reg->wps, probe) ||
998 wps_build_selected_registrar(reg, probe) ||
999 wps_build_sel_reg_dev_password_id(reg, probe) ||
1000 wps_build_sel_reg_config_methods(reg, probe) ||
1001 wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
1002 WPS_RESP_REGISTRAR) ||
1003 wps_build_uuid_e(probe, reg->wps->uuid) ||
1004 wps_build_device_attrs(®->wps->dev, probe) ||
1005 wps_build_probe_config_methods(reg, probe) ||
1006 wps_build_rf_bands(®->wps->dev, probe) ||
1007 wps_build_version2(probe) ||
1008 wps_build_authorized_macs(reg, probe)) {
1009 wpabuf_free(beacon);
1014 beacon = wps_ie_encapsulate(beacon);
1015 probe = wps_ie_encapsulate(probe);
1017 if (!beacon || !probe) {
1018 wpabuf_free(beacon);
1023 if (reg->static_wep_only) {
1025 * Windows XP and Vista clients can get confused about
1026 * EAP-Identity/Request when they probe the network with
1027 * EAPOL-Start. In such a case, they may assume the network is
1028 * using IEEE 802.1X and prompt user for a certificate while
1029 * the correct (non-WPS) behavior would be to ask for the
1030 * static WEP key. As a workaround, use Microsoft Provisioning
1031 * IE to advertise that legacy 802.1X is not supported.
1033 const u8 ms_wps[7] = {
1034 WLAN_EID_VENDOR_SPECIFIC, 5,
1035 /* Microsoft Provisioning IE (00:50:f2:5) */
1036 0x00, 0x50, 0xf2, 5,
1037 0x00 /* no legacy 802.1X or MS WPS */
1039 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
1040 "into Beacon/Probe Response frames");
1041 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
1042 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
1045 return wps_cb_set_ie(reg, beacon, probe);
1049 static int wps_get_dev_password(struct wps_data *wps)
1054 os_free(wps->dev_password);
1055 wps->dev_password = NULL;
1058 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
1059 pin = (const u8 *) "00000000";
1062 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1066 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1068 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1073 wps->dev_password = os_malloc(pin_len);
1074 if (wps->dev_password == NULL)
1076 os_memcpy(wps->dev_password, pin, pin_len);
1077 wps->dev_password_len = pin_len;
1083 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1085 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1086 wpabuf_put_be16(msg, ATTR_UUID_R);
1087 wpabuf_put_be16(msg, WPS_UUID_LEN);
1088 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1093 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1099 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1101 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1102 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1103 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1105 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1106 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1107 "R-Hash derivation");
1111 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1112 wpabuf_put_be16(msg, ATTR_R_HASH1);
1113 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1114 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1115 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1116 addr[0] = wps->snonce;
1117 len[0] = WPS_SECRET_NONCE_LEN;
1118 addr[1] = wps->psk1;
1119 len[1] = WPS_PSK_LEN;
1120 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1121 len[2] = wpabuf_len(wps->dh_pubkey_e);
1122 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1123 len[3] = wpabuf_len(wps->dh_pubkey_r);
1124 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1125 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1127 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1128 wpabuf_put_be16(msg, ATTR_R_HASH2);
1129 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1130 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1131 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1132 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1133 addr[1] = wps->psk2;
1134 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1135 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1141 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1143 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1144 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1145 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1146 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1151 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1153 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1154 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1155 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1156 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1157 WPS_SECRET_NONCE_LEN);
1162 static int wps_build_cred_network_idx(struct wpabuf *msg,
1163 const struct wps_credential *cred)
1165 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
1166 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1167 wpabuf_put_be16(msg, 1);
1168 wpabuf_put_u8(msg, 1);
1173 static int wps_build_cred_ssid(struct wpabuf *msg,
1174 const struct wps_credential *cred)
1176 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1177 wpabuf_put_be16(msg, ATTR_SSID);
1178 wpabuf_put_be16(msg, cred->ssid_len);
1179 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1184 static int wps_build_cred_auth_type(struct wpabuf *msg,
1185 const struct wps_credential *cred)
1187 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1189 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1190 wpabuf_put_be16(msg, 2);
1191 wpabuf_put_be16(msg, cred->auth_type);
1196 static int wps_build_cred_encr_type(struct wpabuf *msg,
1197 const struct wps_credential *cred)
1199 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1201 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1202 wpabuf_put_be16(msg, 2);
1203 wpabuf_put_be16(msg, cred->encr_type);
1208 static int wps_build_cred_network_key(struct wpabuf *msg,
1209 const struct wps_credential *cred)
1211 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1212 (int) cred->key_len);
1213 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1214 wpabuf_put_be16(msg, cred->key_len);
1215 wpabuf_put_data(msg, cred->key, cred->key_len);
1220 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1221 const struct wps_credential *cred)
1223 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1224 MAC2STR(cred->mac_addr));
1225 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1226 wpabuf_put_be16(msg, ETH_ALEN);
1227 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1232 static int wps_build_credential(struct wpabuf *msg,
1233 const struct wps_credential *cred)
1235 if (wps_build_cred_network_idx(msg, cred) ||
1236 wps_build_cred_ssid(msg, cred) ||
1237 wps_build_cred_auth_type(msg, cred) ||
1238 wps_build_cred_encr_type(msg, cred) ||
1239 wps_build_cred_network_key(msg, cred) ||
1240 wps_build_cred_mac_addr(msg, cred))
1246 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1248 struct wpabuf *cred;
1250 if (wps->wps->registrar->skip_cred_build)
1251 goto skip_cred_build;
1253 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1254 if (wps->use_cred) {
1255 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1258 os_memset(&wps->cred, 0, sizeof(wps->cred));
1260 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1261 wps->cred.ssid_len = wps->wps->ssid_len;
1263 /* Select the best authentication and encryption type */
1264 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1265 wps->auth_type = WPS_AUTH_WPA2PSK;
1266 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1267 wps->auth_type = WPS_AUTH_WPAPSK;
1268 else if (wps->auth_type & WPS_AUTH_OPEN)
1269 wps->auth_type = WPS_AUTH_OPEN;
1270 else if (wps->auth_type & WPS_AUTH_SHARED)
1271 wps->auth_type = WPS_AUTH_SHARED;
1273 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1277 wps->cred.auth_type = wps->auth_type;
1279 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1280 wps->auth_type == WPS_AUTH_WPAPSK) {
1281 if (wps->encr_type & WPS_ENCR_AES)
1282 wps->encr_type = WPS_ENCR_AES;
1283 else if (wps->encr_type & WPS_ENCR_TKIP)
1284 wps->encr_type = WPS_ENCR_TKIP;
1286 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1287 "type for WPA/WPA2");
1291 if (wps->encr_type & WPS_ENCR_WEP)
1292 wps->encr_type = WPS_ENCR_WEP;
1293 else if (wps->encr_type & WPS_ENCR_NONE)
1294 wps->encr_type = WPS_ENCR_NONE;
1296 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1297 "type for non-WPA/WPA2 mode");
1301 wps->cred.encr_type = wps->encr_type;
1303 * Set MAC address in the Credential to be the Enrollee's MAC address
1305 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1307 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1308 !wps->wps->registrar->disable_auto_conf) {
1310 /* Generate a random passphrase */
1311 if (os_get_random(r, sizeof(r)) < 0)
1313 os_free(wps->new_psk);
1314 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1315 if (wps->new_psk == NULL)
1317 wps->new_psk_len--; /* remove newline */
1318 while (wps->new_psk_len &&
1319 wps->new_psk[wps->new_psk_len - 1] == '=')
1321 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1322 wps->new_psk, wps->new_psk_len);
1323 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1324 wps->cred.key_len = wps->new_psk_len;
1325 } else if (wps->use_psk_key && wps->wps->psk_set) {
1327 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1328 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1329 os_memcpy(wps->cred.key, hex, 32 * 2);
1330 wps->cred.key_len = 32 * 2;
1331 } else if (wps->wps->network_key) {
1332 os_memcpy(wps->cred.key, wps->wps->network_key,
1333 wps->wps->network_key_len);
1334 wps->cred.key_len = wps->wps->network_key_len;
1335 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1337 /* Generate a random per-device PSK */
1338 os_free(wps->new_psk);
1339 wps->new_psk_len = 32;
1340 wps->new_psk = os_malloc(wps->new_psk_len);
1341 if (wps->new_psk == NULL)
1343 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1344 os_free(wps->new_psk);
1345 wps->new_psk = NULL;
1348 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1349 wps->new_psk, wps->new_psk_len);
1350 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1352 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1353 wps->cred.key_len = wps->new_psk_len * 2;
1357 cred = wpabuf_alloc(200);
1361 if (wps_build_credential(cred, &wps->cred)) {
1366 wpabuf_put_be16(msg, ATTR_CRED);
1367 wpabuf_put_be16(msg, wpabuf_len(cred));
1368 wpabuf_put_buf(msg, cred);
1372 if (wps->wps->registrar->extra_cred) {
1373 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1374 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1381 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1383 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1385 if (wps_build_credential(msg, &wps->cred))
1392 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1396 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1398 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1399 wps->nonce_r, WPS_NONCE_LEN);
1400 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1402 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1403 msg = wpabuf_alloc(1000);
1407 if (wps_build_version(msg) ||
1408 wps_build_msg_type(msg, WPS_M2) ||
1409 wps_build_enrollee_nonce(wps, msg) ||
1410 wps_build_registrar_nonce(wps, msg) ||
1411 wps_build_uuid_r(wps, msg) ||
1412 wps_build_public_key(wps, msg) ||
1413 wps_derive_keys(wps) ||
1414 wps_build_auth_type_flags(wps, msg) ||
1415 wps_build_encr_type_flags(wps, msg) ||
1416 wps_build_conn_type_flags(wps, msg) ||
1417 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1418 wps_build_device_attrs(&wps->wps->dev, msg) ||
1419 wps_build_rf_bands(&wps->wps->dev, msg) ||
1420 wps_build_assoc_state(wps, msg) ||
1421 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1422 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1423 wps_build_os_version(&wps->wps->dev, msg) ||
1424 wps_build_version2(msg) ||
1425 wps_build_authenticator(wps, msg)) {
1431 wps->state = RECV_M3;
1436 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1439 u16 err = wps->config_error;
1441 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1442 msg = wpabuf_alloc(1000);
1446 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1447 err == WPS_CFG_NO_ERROR)
1448 err = WPS_CFG_SETUP_LOCKED;
1450 if (wps_build_version(msg) ||
1451 wps_build_msg_type(msg, WPS_M2D) ||
1452 wps_build_enrollee_nonce(wps, msg) ||
1453 wps_build_registrar_nonce(wps, msg) ||
1454 wps_build_uuid_r(wps, msg) ||
1455 wps_build_auth_type_flags(wps, msg) ||
1456 wps_build_encr_type_flags(wps, msg) ||
1457 wps_build_conn_type_flags(wps, msg) ||
1458 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1459 wps_build_device_attrs(&wps->wps->dev, msg) ||
1460 wps_build_rf_bands(&wps->wps->dev, msg) ||
1461 wps_build_assoc_state(wps, msg) ||
1462 wps_build_config_error(msg, err) ||
1463 wps_build_os_version(&wps->wps->dev, msg) ||
1464 wps_build_version2(msg)) {
1469 wps->state = RECV_M2D_ACK;
1474 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1476 struct wpabuf *msg, *plain;
1478 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1480 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1482 plain = wpabuf_alloc(200);
1486 msg = wpabuf_alloc(1000);
1492 if (wps_build_version(msg) ||
1493 wps_build_msg_type(msg, WPS_M4) ||
1494 wps_build_enrollee_nonce(wps, msg) ||
1495 wps_build_r_hash(wps, msg) ||
1496 wps_build_r_snonce1(wps, plain) ||
1497 wps_build_key_wrap_auth(wps, plain) ||
1498 wps_build_encr_settings(wps, msg, plain) ||
1499 wps_build_version2(msg) ||
1500 wps_build_authenticator(wps, msg)) {
1507 wps->state = RECV_M5;
1512 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1514 struct wpabuf *msg, *plain;
1516 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1518 plain = wpabuf_alloc(200);
1522 msg = wpabuf_alloc(1000);
1528 if (wps_build_version(msg) ||
1529 wps_build_msg_type(msg, WPS_M6) ||
1530 wps_build_enrollee_nonce(wps, msg) ||
1531 wps_build_r_snonce2(wps, plain) ||
1532 wps_build_key_wrap_auth(wps, plain) ||
1533 wps_build_encr_settings(wps, msg, plain) ||
1534 wps_build_version2(msg) ||
1535 wps_build_authenticator(wps, msg)) {
1542 wps->wps_pin_revealed = 1;
1543 wps->state = RECV_M7;
1548 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1550 struct wpabuf *msg, *plain;
1552 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1554 plain = wpabuf_alloc(500);
1558 msg = wpabuf_alloc(1000);
1564 if (wps_build_version(msg) ||
1565 wps_build_msg_type(msg, WPS_M8) ||
1566 wps_build_enrollee_nonce(wps, msg) ||
1567 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1568 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1569 wps_build_key_wrap_auth(wps, plain) ||
1570 wps_build_encr_settings(wps, msg, plain) ||
1571 wps_build_version2(msg) ||
1572 wps_build_authenticator(wps, msg)) {
1579 wps->state = RECV_DONE;
1584 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1588 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1590 msg = wpabuf_alloc(1000);
1594 if (wps_build_version(msg) ||
1595 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1596 wps_build_enrollee_nonce(wps, msg) ||
1597 wps_build_registrar_nonce(wps, msg) ||
1598 wps_build_version2(msg)) {
1607 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1611 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1613 msg = wpabuf_alloc(1000);
1617 if (wps_build_version(msg) ||
1618 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1619 wps_build_enrollee_nonce(wps, msg) ||
1620 wps_build_registrar_nonce(wps, msg) ||
1621 wps_build_config_error(msg, wps->config_error) ||
1622 wps_build_version2(msg)) {
1631 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1632 enum wsc_op_code *op_code)
1636 #ifdef CONFIG_WPS_UPNP
1637 if (!wps->int_reg && wps->wps->wps_upnp) {
1638 struct upnp_pending_message *p, *prev = NULL;
1639 if (wps->ext_reg > 1)
1640 wps_registrar_free_pending_m2(wps->wps);
1641 p = wps->wps->upnp_msgs;
1642 /* TODO: check pending message MAC address */
1643 while (p && p->next) {
1648 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1653 wps->wps->upnp_msgs = NULL;
1660 *op_code = WSC_NACK;
1667 if (wps->ext_reg == 0)
1673 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1674 "pending message available");
1677 #endif /* CONFIG_WPS_UPNP */
1679 switch (wps->state) {
1681 if (wps_get_dev_password(wps) < 0)
1682 msg = wps_build_m2d(wps);
1684 msg = wps_build_m2(wps);
1688 msg = wps_build_m2d(wps);
1692 msg = wps_build_m4(wps);
1696 msg = wps_build_m6(wps);
1700 msg = wps_build_m8(wps);
1704 msg = wps_build_wsc_ack(wps);
1708 msg = wps_build_wsc_nack(wps);
1709 *op_code = WSC_NACK;
1712 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1713 "a message", wps->state);
1718 if (*op_code == WSC_MSG && msg) {
1719 /* Save a copy of the last message for Authenticator derivation
1721 wpabuf_free(wps->last_msg);
1722 wps->last_msg = wpabuf_dup(msg);
1729 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1731 if (e_nonce == NULL) {
1732 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1736 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1737 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1738 wps->nonce_e, WPS_NONCE_LEN);
1744 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1746 if (r_nonce == NULL) {
1747 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1751 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1752 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1760 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1762 if (uuid_e == NULL) {
1763 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1767 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1768 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1774 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1776 if (pw_id == NULL) {
1777 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1781 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1782 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1788 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1790 if (e_hash1 == NULL) {
1791 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1795 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1796 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1802 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1804 if (e_hash2 == NULL) {
1805 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1809 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1810 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1816 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1818 u8 hash[SHA256_MAC_LEN];
1822 if (e_snonce1 == NULL) {
1823 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1827 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1828 WPS_SECRET_NONCE_LEN);
1830 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1831 addr[0] = e_snonce1;
1832 len[0] = WPS_SECRET_NONCE_LEN;
1833 addr[1] = wps->psk1;
1834 len[1] = WPS_PSK_LEN;
1835 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1836 len[2] = wpabuf_len(wps->dh_pubkey_e);
1837 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1838 len[3] = wpabuf_len(wps->dh_pubkey_r);
1839 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1841 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1842 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1843 "not match with the pre-committed value");
1844 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1845 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1849 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1850 "half of the device password");
1856 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1858 u8 hash[SHA256_MAC_LEN];
1862 if (e_snonce2 == NULL) {
1863 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1867 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1868 WPS_SECRET_NONCE_LEN);
1870 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1871 addr[0] = e_snonce2;
1872 len[0] = WPS_SECRET_NONCE_LEN;
1873 addr[1] = wps->psk2;
1874 len[1] = WPS_PSK_LEN;
1875 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1876 len[2] = wpabuf_len(wps->dh_pubkey_e);
1877 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1878 len[3] = wpabuf_len(wps->dh_pubkey_r);
1879 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1881 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1882 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1883 "not match with the pre-committed value");
1884 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1885 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1886 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1890 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1891 "half of the device password");
1892 wps->wps_pin_revealed = 0;
1893 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1899 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1901 if (mac_addr == NULL) {
1902 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1906 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1908 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1909 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1915 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1918 if (pk == NULL || pk_len == 0) {
1919 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1923 #ifdef CONFIG_WPS_OOB
1924 if (wps->wps->oob_conf.pubkey_hash != NULL) {
1926 u8 hash[WPS_HASH_LEN];
1929 sha256_vector(1, addr, &pk_len, hash);
1931 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
1932 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
1933 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
1937 #endif /* CONFIG_WPS_OOB */
1939 wpabuf_free(wps->dh_pubkey_e);
1940 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1941 if (wps->dh_pubkey_e == NULL)
1948 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1953 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1958 auth_types = WPA_GET_BE16(auth);
1960 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1962 wps->auth_type = wps->wps->auth_types & auth_types;
1963 if (wps->auth_type == 0) {
1964 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1965 "authentication types (own 0x%x Enrollee 0x%x)",
1966 wps->wps->auth_types, auth_types);
1967 #ifdef WPS_WORKAROUNDS
1969 * Some deployed implementations seem to advertise incorrect
1970 * information in this attribute. For example, Linksys WRT350N
1971 * seems to have a byteorder bug that breaks this negotiation.
1972 * In order to interoperate with existing implementations,
1973 * assume that the Enrollee supports everything we do.
1975 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1976 "does not advertise supported authentication types "
1978 wps->auth_type = wps->wps->auth_types;
1979 #else /* WPS_WORKAROUNDS */
1981 #endif /* WPS_WORKAROUNDS */
1988 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1993 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1998 encr_types = WPA_GET_BE16(encr);
2000 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
2002 wps->encr_type = wps->wps->encr_types & encr_types;
2003 if (wps->encr_type == 0) {
2004 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2005 "encryption types (own 0x%x Enrollee 0x%x)",
2006 wps->wps->encr_types, encr_types);
2007 #ifdef WPS_WORKAROUNDS
2009 * Some deployed implementations seem to advertise incorrect
2010 * information in this attribute. For example, Linksys WRT350N
2011 * seems to have a byteorder bug that breaks this negotiation.
2012 * In order to interoperate with existing implementations,
2013 * assume that the Enrollee supports everything we do.
2015 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2016 "does not advertise supported encryption types "
2018 wps->encr_type = wps->wps->encr_types;
2019 #else /* WPS_WORKAROUNDS */
2021 #endif /* WPS_WORKAROUNDS */
2028 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
2031 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
2036 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
2043 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
2047 if (methods == NULL) {
2048 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
2052 m = WPA_GET_BE16(methods);
2054 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
2055 "%s%s%s%s%s%s%s%s%s", m,
2056 m & WPS_CONFIG_USBA ? " [USBA]" : "",
2057 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
2058 m & WPS_CONFIG_LABEL ? " [Label]" : "",
2059 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
2060 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
2061 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
2062 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
2063 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
2064 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
2066 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
2068 * The Enrollee does not have a display so it is unlikely to be
2069 * able to show the passphrase to a user and as such, could
2070 * benefit from receiving PSK to reduce key derivation time.
2072 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2073 "Enrollee not supporting display");
2074 wps->use_psk_key = 1;
2081 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2083 if (state == NULL) {
2084 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2089 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2096 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2100 if (assoc == NULL) {
2101 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2105 a = WPA_GET_BE16(assoc);
2106 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2112 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2117 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2121 e = WPA_GET_BE16(err);
2122 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2128 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2129 struct wps_parse_attr *attr)
2131 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2133 if (wps->state != RECV_M1) {
2134 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2135 "receiving M1", wps->state);
2139 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2140 wps_process_mac_addr(wps, attr->mac_addr) ||
2141 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2142 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2143 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2144 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2145 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2146 wps_process_config_methods(wps, attr->config_methods) ||
2147 wps_process_wps_state(wps, attr->wps_state) ||
2148 wps_process_device_attrs(&wps->peer_dev, attr) ||
2149 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2150 wps_process_assoc_state(wps, attr->assoc_state) ||
2151 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2152 wps_process_config_error(wps, attr->config_error) ||
2153 wps_process_os_version(&wps->peer_dev, attr->os_version))
2156 if (wps->dev_pw_id < 0x10 &&
2157 wps->dev_pw_id != DEV_PW_DEFAULT &&
2158 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2159 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2160 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2161 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2162 !wps->wps->registrar->pbc)) {
2163 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2165 wps->state = SEND_M2D;
2166 return WPS_CONTINUE;
2169 #ifdef CONFIG_WPS_OOB
2170 if (wps->dev_pw_id >= 0x10 &&
2171 wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
2172 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
2173 "%d mismatch", wps->dev_pw_id);
2174 wps->state = SEND_M2D;
2175 return WPS_CONTINUE;
2177 #endif /* CONFIG_WPS_OOB */
2179 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2180 if (wps->wps->registrar->force_pbc_overlap ||
2181 wps_registrar_pbc_overlap(wps->wps->registrar,
2182 wps->mac_addr_e, wps->uuid_e)) {
2183 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2185 wps->state = SEND_M2D;
2186 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2187 wps_pbc_overlap_event(wps->wps);
2188 wps->wps->registrar->force_pbc_overlap = 1;
2189 return WPS_CONTINUE;
2191 wps_registrar_add_pbc_session(wps->wps->registrar,
2192 wps->mac_addr_e, wps->uuid_e);
2196 #ifdef WPS_WORKAROUNDS
2198 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2199 * passphrase format. To avoid interop issues, force PSK format to be
2202 if (!wps->use_psk_key &&
2203 wps->peer_dev.manufacturer &&
2204 os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
2205 wps->peer_dev.model_name &&
2206 os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
2207 wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
2209 wps->use_psk_key = 1;
2211 #endif /* WPS_WORKAROUNDS */
2213 wps->state = SEND_M2;
2214 return WPS_CONTINUE;
2218 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2219 const struct wpabuf *msg,
2220 struct wps_parse_attr *attr)
2222 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2224 if (wps->state != RECV_M3) {
2225 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2226 "receiving M3", wps->state);
2227 wps->state = SEND_WSC_NACK;
2228 return WPS_CONTINUE;
2231 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2232 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2234 wps->state = SEND_WSC_NACK;
2235 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2236 return WPS_CONTINUE;
2239 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2240 wps_process_authenticator(wps, attr->authenticator, msg) ||
2241 wps_process_e_hash1(wps, attr->e_hash1) ||
2242 wps_process_e_hash2(wps, attr->e_hash2)) {
2243 wps->state = SEND_WSC_NACK;
2244 return WPS_CONTINUE;
2247 wps->state = SEND_M4;
2248 return WPS_CONTINUE;
2252 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2253 const struct wpabuf *msg,
2254 struct wps_parse_attr *attr)
2256 struct wpabuf *decrypted;
2257 struct wps_parse_attr eattr;
2259 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2261 if (wps->state != RECV_M5) {
2262 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2263 "receiving M5", wps->state);
2264 wps->state = SEND_WSC_NACK;
2265 return WPS_CONTINUE;
2268 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2269 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2271 wps->state = SEND_WSC_NACK;
2272 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2273 return WPS_CONTINUE;
2276 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2277 wps_process_authenticator(wps, attr->authenticator, msg)) {
2278 wps->state = SEND_WSC_NACK;
2279 return WPS_CONTINUE;
2282 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2283 attr->encr_settings_len);
2284 if (decrypted == NULL) {
2285 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2286 "Settings attribute");
2287 wps->state = SEND_WSC_NACK;
2288 return WPS_CONTINUE;
2291 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2293 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2294 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2295 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2296 wpabuf_free(decrypted);
2297 wps->state = SEND_WSC_NACK;
2298 return WPS_CONTINUE;
2300 wpabuf_free(decrypted);
2302 wps->state = SEND_M6;
2303 return WPS_CONTINUE;
2307 static void wps_sta_cred_cb(struct wps_data *wps)
2310 * Update credential to only include a single authentication and
2311 * encryption type in case the AP configuration includes more than one
2314 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2315 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2316 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2317 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2318 if (wps->cred.encr_type & WPS_ENCR_AES)
2319 wps->cred.encr_type = WPS_ENCR_AES;
2320 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2321 wps->cred.encr_type = WPS_ENCR_TKIP;
2322 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2323 "AP configuration");
2324 if (wps->wps->cred_cb)
2325 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2329 static void wps_cred_update(struct wps_credential *dst,
2330 struct wps_credential *src)
2332 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2333 dst->ssid_len = src->ssid_len;
2334 dst->auth_type = src->auth_type;
2335 dst->encr_type = src->encr_type;
2336 dst->key_idx = src->key_idx;
2337 os_memcpy(dst->key, src->key, sizeof(dst->key));
2338 dst->key_len = src->key_len;
2342 static int wps_process_ap_settings_r(struct wps_data *wps,
2343 struct wps_parse_attr *attr)
2345 if (wps->wps->ap || wps->er)
2348 /* AP Settings Attributes in M7 when Enrollee is an AP */
2349 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2352 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2354 if (wps->new_ap_settings) {
2355 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2357 wps_cred_update(&wps->cred, wps->new_ap_settings);
2361 * Use the AP PIN only to receive the current AP settings, not
2362 * to reconfigure the AP.
2364 if (wps->ap_settings_cb) {
2365 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2369 wps_sta_cred_cb(wps);
2375 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2376 const struct wpabuf *msg,
2377 struct wps_parse_attr *attr)
2379 struct wpabuf *decrypted;
2380 struct wps_parse_attr eattr;
2382 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2384 if (wps->state != RECV_M7) {
2385 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2386 "receiving M7", wps->state);
2387 wps->state = SEND_WSC_NACK;
2388 return WPS_CONTINUE;
2391 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2392 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2394 wps->state = SEND_WSC_NACK;
2395 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2396 return WPS_CONTINUE;
2399 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2400 wps_process_authenticator(wps, attr->authenticator, msg)) {
2401 wps->state = SEND_WSC_NACK;
2402 return WPS_CONTINUE;
2405 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2406 attr->encr_settings_len);
2407 if (decrypted == NULL) {
2408 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2409 "Settings attribute");
2410 wps->state = SEND_WSC_NACK;
2411 return WPS_CONTINUE;
2414 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2416 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2417 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2418 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2419 wps_process_ap_settings_r(wps, &eattr)) {
2420 wpabuf_free(decrypted);
2421 wps->state = SEND_WSC_NACK;
2422 return WPS_CONTINUE;
2425 wpabuf_free(decrypted);
2427 wps->state = SEND_M8;
2428 return WPS_CONTINUE;
2432 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2433 const struct wpabuf *msg)
2435 struct wps_parse_attr attr;
2436 enum wps_process_res ret = WPS_CONTINUE;
2438 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2440 if (wps_parse_msg(msg, &attr) < 0)
2443 if (attr.msg_type == NULL) {
2444 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2448 if (*attr.msg_type != WPS_M1 &&
2449 (attr.registrar_nonce == NULL ||
2450 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2451 WPS_NONCE_LEN != 0))) {
2452 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2456 switch (*attr.msg_type) {
2458 #ifdef CONFIG_WPS_UPNP
2459 if (wps->wps->wps_upnp && attr.mac_addr) {
2460 /* Remove old pending messages when starting new run */
2461 wps_free_pending_msgs(wps->wps->upnp_msgs);
2462 wps->wps->upnp_msgs = NULL;
2464 upnp_wps_device_send_wlan_event(
2465 wps->wps->wps_upnp, attr.mac_addr,
2466 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2468 #endif /* CONFIG_WPS_UPNP */
2469 ret = wps_process_m1(wps, &attr);
2472 ret = wps_process_m3(wps, msg, &attr);
2473 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2474 wps_fail_event(wps->wps, WPS_M3);
2477 ret = wps_process_m5(wps, msg, &attr);
2478 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2479 wps_fail_event(wps->wps, WPS_M5);
2482 ret = wps_process_m7(wps, msg, &attr);
2483 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2484 wps_fail_event(wps->wps, WPS_M7);
2487 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2492 if (ret == WPS_CONTINUE) {
2493 /* Save a copy of the last message for Authenticator derivation
2495 wpabuf_free(wps->last_msg);
2496 wps->last_msg = wpabuf_dup(msg);
2503 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2504 const struct wpabuf *msg)
2506 struct wps_parse_attr attr;
2508 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2510 if (wps_parse_msg(msg, &attr) < 0)
2513 if (attr.msg_type == NULL) {
2514 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2518 if (*attr.msg_type != WPS_WSC_ACK) {
2519 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2524 #ifdef CONFIG_WPS_UPNP
2525 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2526 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2527 if (wps->wps->upnp_msgs)
2528 return WPS_CONTINUE;
2529 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2530 "external Registrar");
2533 #endif /* CONFIG_WPS_UPNP */
2535 if (attr.registrar_nonce == NULL ||
2536 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2538 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2542 if (attr.enrollee_nonce == NULL ||
2543 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2544 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2548 if (wps->state == RECV_M2D_ACK) {
2549 #ifdef CONFIG_WPS_UPNP
2550 if (wps->wps->wps_upnp &&
2551 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2552 if (wps->wps->upnp_msgs)
2553 return WPS_CONTINUE;
2554 if (wps->ext_reg == 0)
2556 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2557 "external Registrar");
2560 #endif /* CONFIG_WPS_UPNP */
2562 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2563 "terminate negotiation");
2570 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2571 const struct wpabuf *msg)
2573 struct wps_parse_attr attr;
2576 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2578 old_state = wps->state;
2579 wps->state = SEND_WSC_NACK;
2581 if (wps_parse_msg(msg, &attr) < 0)
2584 if (attr.msg_type == NULL) {
2585 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2589 if (*attr.msg_type != WPS_WSC_NACK) {
2590 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2595 #ifdef CONFIG_WPS_UPNP
2596 if (wps->wps->wps_upnp && wps->ext_reg) {
2597 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2598 "Registrar terminated by the Enrollee");
2601 #endif /* CONFIG_WPS_UPNP */
2603 if (attr.registrar_nonce == NULL ||
2604 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2606 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2610 if (attr.enrollee_nonce == NULL ||
2611 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2612 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2616 if (attr.config_error == NULL) {
2617 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2622 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2623 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2625 switch (old_state) {
2627 wps_fail_event(wps->wps, WPS_M2);
2630 wps_fail_event(wps->wps, WPS_M4);
2633 wps_fail_event(wps->wps, WPS_M6);
2636 wps_fail_event(wps->wps, WPS_M8);
2646 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2647 const struct wpabuf *msg)
2649 struct wps_parse_attr attr;
2651 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2653 if (wps->state != RECV_DONE &&
2654 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2655 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2656 "receiving WSC_Done", wps->state);
2660 if (wps_parse_msg(msg, &attr) < 0)
2663 if (attr.msg_type == NULL) {
2664 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2668 if (*attr.msg_type != WPS_WSC_DONE) {
2669 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2674 #ifdef CONFIG_WPS_UPNP
2675 if (wps->wps->wps_upnp && wps->ext_reg) {
2676 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2677 "Registrar completed successfully");
2678 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2682 #endif /* CONFIG_WPS_UPNP */
2684 if (attr.registrar_nonce == NULL ||
2685 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2687 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2691 if (attr.enrollee_nonce == NULL ||
2692 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2693 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2697 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2698 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2701 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2702 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2703 struct wps_credential cred;
2705 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2706 "on first Enrollee connection");
2708 os_memset(&cred, 0, sizeof(cred));
2709 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2710 cred.ssid_len = wps->wps->ssid_len;
2711 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2712 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2713 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2714 cred.key_len = wps->new_psk_len;
2716 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2717 wpa_hexdump_ascii_key(MSG_DEBUG,
2718 "WPS: Generated random passphrase",
2719 wps->new_psk, wps->new_psk_len);
2720 if (wps->wps->cred_cb)
2721 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2723 os_free(wps->new_psk);
2724 wps->new_psk = NULL;
2727 if (!wps->wps->ap && !wps->er)
2728 wps_sta_cred_cb(wps);
2731 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2732 wps->new_psk, wps->new_psk_len)) {
2733 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2736 os_free(wps->new_psk);
2737 wps->new_psk = NULL;
2740 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2743 wps_registrar_remove_pbc_session(wps->wps->registrar,
2744 wps->mac_addr_e, wps->uuid_e);
2745 wps_registrar_pbc_completed(wps->wps->registrar);
2747 wps_registrar_pin_completed(wps->wps->registrar);
2749 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
2750 * merge them into APs own list.. */
2752 wps_success_event(wps->wps);
2758 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2759 enum wsc_op_code op_code,
2760 const struct wpabuf *msg)
2762 enum wps_process_res ret;
2764 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2766 (unsigned long) wpabuf_len(msg), op_code);
2768 #ifdef CONFIG_WPS_UPNP
2769 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2770 struct wps_parse_attr attr;
2771 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2772 *attr.msg_type == WPS_M3)
2773 wps->ext_reg = 2; /* past M2/M2D phase */
2775 if (wps->ext_reg > 1)
2776 wps_registrar_free_pending_m2(wps->wps);
2777 if (wps->wps->wps_upnp && wps->ext_reg &&
2778 wps->wps->upnp_msgs == NULL &&
2779 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
2781 struct wps_parse_attr attr;
2783 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2786 type = *attr.msg_type;
2787 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2788 " to external Registrar for processing", type);
2789 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2791 UPNP_WPS_WLANEVENT_TYPE_EAP,
2793 if (op_code == WSC_MSG)
2795 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2796 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2797 "external Registrar");
2798 return WPS_CONTINUE;
2800 #endif /* CONFIG_WPS_UPNP */
2804 return wps_process_wsc_msg(wps, msg);
2806 return wps_process_wsc_ack(wps, msg);
2808 return wps_process_wsc_nack(wps, msg);
2810 ret = wps_process_wsc_done(wps, msg);
2811 if (ret == WPS_FAILURE) {
2812 wps->state = SEND_WSC_NACK;
2813 wps_fail_event(wps->wps, WPS_WSC_DONE);
2817 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2823 int wps_registrar_update_ie(struct wps_registrar *reg)
2825 return wps_set_ie(reg);
2829 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2832 struct wps_registrar *reg = eloop_ctx;
2834 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
2835 "unselect internal Registrar");
2836 reg->selected_registrar = 0;
2838 wps_registrar_selected_registrar_changed(reg);
2842 #ifdef CONFIG_WPS_UPNP
2843 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
2844 struct subscription *s)
2847 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
2848 "config_methods=0x%x)",
2849 s->dev_password_id, s->config_methods);
2850 reg->sel_reg_union = 1;
2851 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
2852 reg->sel_reg_dev_password_id_override = s->dev_password_id;
2853 if (reg->sel_reg_config_methods_override == -1)
2854 reg->sel_reg_config_methods_override = 0;
2855 reg->sel_reg_config_methods_override |= s->config_methods;
2856 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
2857 if (is_zero_ether_addr(reg->authorized_macs_union[i]))
2859 for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
2861 if (is_zero_ether_addr(s->authorized_macs[j]))
2863 os_memcpy(reg->authorized_macs_union[i],
2864 s->authorized_macs[j], ETH_ALEN);
2868 #endif /* CONFIG_WPS_UPNP */
2871 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
2873 #ifdef CONFIG_WPS_UPNP
2874 struct subscription *s;
2876 if (reg->wps->wps_upnp == NULL)
2879 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
2880 struct subscription, list) {
2881 struct subscr_addr *sa;
2882 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
2884 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
2885 inet_ntoa(sa->saddr.sin_addr),
2886 ntohs(sa->saddr.sin_port));
2888 if (s->selected_registrar)
2889 wps_registrar_sel_reg_add(reg, s);
2891 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
2894 #endif /* CONFIG_WPS_UPNP */
2899 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
2900 * @reg: Registrar data from wps_registrar_init()
2902 * This function is called when selected registrar state changes, e.g., when an
2903 * AP receives a SetSelectedRegistrar UPnP message.
2905 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg)
2907 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
2909 reg->sel_reg_union = reg->selected_registrar;
2910 reg->sel_reg_dev_password_id_override = -1;
2911 reg->sel_reg_config_methods_override = -1;
2912 os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
2913 WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
2914 if (reg->selected_registrar) {
2915 reg->sel_reg_config_methods_override =
2916 reg->wps->config_methods &
2917 ~(WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
2918 WPS_CONFIG_PHY_PUSHBUTTON);
2920 reg->sel_reg_dev_password_id_override =
2922 reg->sel_reg_config_methods_override |=
2923 WPS_CONFIG_PUSHBUTTON;
2924 if (reg->wps->config_methods &
2925 WPS_CONFIG_VIRT_PUSHBUTTON)
2926 reg->sel_reg_config_methods_override |=
2927 WPS_CONFIG_VIRT_PUSHBUTTON;
2928 if (reg->wps->config_methods &
2929 WPS_CONFIG_PHY_PUSHBUTTON)
2930 reg->sel_reg_config_methods_override |=
2931 WPS_CONFIG_PHY_PUSHBUTTON;
2933 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
2934 "(pbc=%d)", reg->pbc);
2936 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
2938 wps_registrar_sel_reg_union(reg);
2941 wps_cb_set_sel_reg(reg);
2945 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
2946 char *buf, size_t buflen)
2948 struct wps_registrar_device *d;
2951 char devtype[WPS_DEV_TYPE_BUFSIZE];
2953 d = wps_device_get(reg, addr);
2956 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
2959 ret = os_snprintf(buf + len, buflen - len,
2961 "wpsPrimaryDeviceType=%s\n"
2962 "wpsDeviceName=%s\n"
2963 "wpsManufacturer=%s\n"
2965 "wpsModelNumber=%s\n"
2966 "wpsSerialNumber=%s\n",
2968 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
2970 d->dev.device_name ? d->dev.device_name : "",
2971 d->dev.manufacturer ? d->dev.manufacturer : "",
2972 d->dev.model_name ? d->dev.model_name : "",
2973 d->dev.model_number ? d->dev.model_number : "",
2974 d->dev.serial_number ? d->dev.serial_number : "");
2975 if (ret < 0 || (size_t) ret >= buflen - len)