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 #define WPS_WORKAROUNDS
34 u8 uuid[WPS_UUID_LEN];
38 #define PIN_LOCKED BIT(0)
39 #define PIN_EXPIRES BIT(1)
41 struct os_time expiration;
45 static void wps_free_pin(struct wps_uuid_pin *pin)
52 static void wps_remove_pin(struct wps_uuid_pin *pin)
54 dl_list_del(&pin->list);
59 static void wps_free_pins(struct dl_list *pins)
61 struct wps_uuid_pin *pin, *prev;
62 dl_list_for_each_safe(pin, prev, pins, struct wps_uuid_pin, list)
67 struct wps_pbc_session {
68 struct wps_pbc_session *next;
70 u8 uuid_e[WPS_UUID_LEN];
71 struct os_time timestamp;
75 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
77 struct wps_pbc_session *prev;
87 struct wps_registrar_device {
88 struct wps_registrar_device *next;
89 struct wps_device_data dev;
90 u8 uuid[WPS_UUID_LEN];
94 struct wps_registrar {
95 struct wps_context *wps;
98 int selected_registrar;
100 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
102 int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
103 struct wpabuf *probe_resp_ie);
104 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
105 const struct wps_device_data *dev);
106 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
108 void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
109 u16 sel_reg_config_methods);
110 void (*enrollee_seen_cb)(void *ctx, const u8 *addr, const u8 *uuid_e,
111 const u8 *pri_dev_type, u16 config_methods,
112 u16 dev_password_id, u8 request_type,
113 const char *dev_name);
117 struct wps_pbc_session *pbc_sessions;
120 struct wpabuf *extra_cred;
121 int disable_auto_conf;
123 int sel_reg_dev_password_id_override;
124 int sel_reg_config_methods_override;
127 struct wps_registrar_device *devices;
129 int force_pbc_overlap;
133 static int wps_set_ie(struct wps_registrar *reg);
134 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
135 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
139 static void wps_free_devices(struct wps_registrar_device *dev)
141 struct wps_registrar_device *prev;
146 wps_device_data_free(&prev->dev);
152 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
155 struct wps_registrar_device *dev;
157 for (dev = reg->devices; dev; dev = dev->next) {
158 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
165 static void wps_device_clone_data(struct wps_device_data *dst,
166 struct wps_device_data *src)
168 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
169 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
171 #define WPS_STRDUP(n) \
173 dst->n = src->n ? os_strdup(src->n) : NULL
175 WPS_STRDUP(device_name);
176 WPS_STRDUP(manufacturer);
177 WPS_STRDUP(model_name);
178 WPS_STRDUP(model_number);
179 WPS_STRDUP(serial_number);
184 int wps_device_store(struct wps_registrar *reg,
185 struct wps_device_data *dev, const u8 *uuid)
187 struct wps_registrar_device *d;
189 d = wps_device_get(reg, dev->mac_addr);
191 d = os_zalloc(sizeof(*d));
194 d->next = reg->devices;
198 wps_device_clone_data(&d->dev, dev);
199 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
205 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
206 const u8 *addr, const u8 *uuid_e)
208 struct wps_pbc_session *pbc, *prev = NULL;
213 pbc = reg->pbc_sessions;
215 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
216 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
218 prev->next = pbc->next;
220 reg->pbc_sessions = pbc->next;
228 pbc = os_zalloc(sizeof(*pbc));
231 os_memcpy(pbc->addr, addr, ETH_ALEN);
233 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
236 pbc->next = reg->pbc_sessions;
237 reg->pbc_sessions = pbc;
238 pbc->timestamp = now;
240 /* remove entries that have timed out */
245 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
247 wps_free_pbc_sessions(pbc);
256 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
257 const u8 *addr, const u8 *uuid_e)
259 struct wps_pbc_session *pbc, *prev = NULL;
261 pbc = reg->pbc_sessions;
263 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
264 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
266 prev->next = pbc->next;
268 reg->pbc_sessions = pbc->next;
278 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
279 const u8 *addr, const u8 *uuid_e)
282 struct wps_pbc_session *pbc;
287 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
288 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
290 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
292 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
299 return count > 1 ? 1 : 0;
303 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
305 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
307 wpabuf_put_be16(msg, ATTR_WPS_STATE);
308 wpabuf_put_be16(msg, 1);
309 wpabuf_put_u8(msg, wps->wps_state);
314 #ifdef CONFIG_WPS_UPNP
315 static void wps_registrar_free_pending_m2(struct wps_context *wps)
317 struct upnp_pending_message *p, *p2, *prev = NULL;
320 if (p->type == WPS_M2 || p->type == WPS_M2D) {
322 wps->upnp_msgs = p->next;
324 prev->next = p->next;
325 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
328 wpabuf_free(p2->msg);
336 #endif /* CONFIG_WPS_UPNP */
339 static int wps_build_ap_setup_locked(struct wps_context *wps,
342 if (wps->ap_setup_locked) {
343 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
344 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
345 wpabuf_put_be16(msg, 1);
346 wpabuf_put_u8(msg, 1);
352 static int wps_build_selected_registrar(struct wps_registrar *reg,
355 if (!reg->sel_reg_union)
357 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
358 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
359 wpabuf_put_be16(msg, 1);
360 wpabuf_put_u8(msg, 1);
365 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
368 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
369 if (!reg->sel_reg_union)
371 if (reg->sel_reg_dev_password_id_override >= 0)
372 id = reg->sel_reg_dev_password_id_override;
373 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
374 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
375 wpabuf_put_be16(msg, 2);
376 wpabuf_put_be16(msg, id);
381 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
385 if (!reg->sel_reg_union)
387 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
389 methods |= WPS_CONFIG_PUSHBUTTON;
390 if (reg->sel_reg_config_methods_override >= 0)
391 methods = reg->sel_reg_config_methods_override;
392 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
394 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
395 wpabuf_put_be16(msg, 2);
396 wpabuf_put_be16(msg, methods);
401 static int wps_build_probe_config_methods(struct wps_registrar *reg,
406 * These are the methods that the AP supports as an Enrollee for adding
407 * external Registrars.
409 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
410 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
411 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
412 wpabuf_put_be16(msg, 2);
413 wpabuf_put_be16(msg, methods);
418 static int wps_build_config_methods_r(struct wps_registrar *reg,
422 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
424 methods |= WPS_CONFIG_PUSHBUTTON;
425 return wps_build_config_methods(msg, methods);
429 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
431 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
432 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
433 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
434 wpabuf_put_be16(msg, 1);
435 wpabuf_put_u8(msg, resp);
441 * wps_registrar_init - Initialize WPS Registrar data
442 * @wps: Pointer to longterm WPS context
443 * @cfg: Registrar configuration
444 * Returns: Pointer to allocated Registrar data or %NULL on failure
446 * This function is used to initialize WPS Registrar functionality. It can be
447 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
448 * runs (e.g., when run as an internal Registrar in an AP). Caller is
449 * responsible for freeing the returned data with wps_registrar_deinit() when
450 * Registrar functionality is not needed anymore.
452 struct wps_registrar *
453 wps_registrar_init(struct wps_context *wps,
454 const struct wps_registrar_config *cfg)
456 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
460 dl_list_init(®->pins);
462 reg->new_psk_cb = cfg->new_psk_cb;
463 reg->set_ie_cb = cfg->set_ie_cb;
464 reg->pin_needed_cb = cfg->pin_needed_cb;
465 reg->reg_success_cb = cfg->reg_success_cb;
466 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
467 reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
468 reg->cb_ctx = cfg->cb_ctx;
469 reg->skip_cred_build = cfg->skip_cred_build;
470 if (cfg->extra_cred) {
471 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
472 cfg->extra_cred_len);
473 if (reg->extra_cred == NULL) {
478 reg->disable_auto_conf = cfg->disable_auto_conf;
479 reg->sel_reg_dev_password_id_override = -1;
480 reg->sel_reg_config_methods_override = -1;
481 reg->static_wep_only = cfg->static_wep_only;
483 if (wps_set_ie(reg)) {
484 wps_registrar_deinit(reg);
493 * wps_registrar_deinit - Deinitialize WPS Registrar data
494 * @reg: Registrar data from wps_registrar_init()
496 void wps_registrar_deinit(struct wps_registrar *reg)
500 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
501 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
502 wps_free_pins(®->pins);
503 wps_free_pbc_sessions(reg->pbc_sessions);
504 wpabuf_free(reg->extra_cred);
505 wps_free_devices(reg->devices);
511 * wps_registrar_add_pin - Configure a new PIN for Registrar
512 * @reg: Registrar data from wps_registrar_init()
513 * @uuid: UUID-E or %NULL for wildcard (any UUID)
514 * @pin: PIN (Device Password)
515 * @pin_len: Length of pin in octets
516 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
517 * Returns: 0 on success, -1 on failure
519 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
520 const u8 *pin, size_t pin_len, int timeout)
522 struct wps_uuid_pin *p;
524 p = os_zalloc(sizeof(*p));
528 p->wildcard_uuid = 1;
530 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
531 p->pin = os_malloc(pin_len);
532 if (p->pin == NULL) {
536 os_memcpy(p->pin, pin, pin_len);
537 p->pin_len = pin_len;
540 p->flags |= PIN_EXPIRES;
541 os_get_time(&p->expiration);
542 p->expiration.sec += timeout;
545 dl_list_add(®->pins, &p->list);
547 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
549 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
550 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
551 reg->selected_registrar = 1;
553 wps_registrar_selected_registrar_changed(reg);
554 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
555 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
556 wps_registrar_set_selected_timeout,
563 static void wps_registrar_expire_pins(struct wps_registrar *reg)
565 struct wps_uuid_pin *pin, *prev;
569 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
571 if ((pin->flags & PIN_EXPIRES) &&
572 os_time_before(&pin->expiration, &now)) {
573 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
574 pin->uuid, WPS_UUID_LEN);
582 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
583 * @reg: Registrar data from wps_registrar_init()
585 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
587 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
589 struct wps_uuid_pin *pin, *prev;
591 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
593 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
594 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
595 pin->uuid, WPS_UUID_LEN);
605 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
606 const u8 *uuid, size_t *pin_len)
608 struct wps_uuid_pin *pin, *found = NULL;
610 wps_registrar_expire_pins(reg);
612 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
613 if (!pin->wildcard_uuid &&
614 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
621 /* Check for wildcard UUIDs since none of the UUID-specific
623 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
624 if (pin->wildcard_uuid == 1) {
625 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
626 "PIN. Assigned it for this UUID-E");
627 pin->wildcard_uuid = 2;
628 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
639 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
640 * that could otherwise avoid PIN invalidations.
642 if (found->flags & PIN_LOCKED) {
643 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
644 "allow concurrent re-use");
647 *pin_len = found->pin_len;
648 found->flags |= PIN_LOCKED;
654 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
655 * @reg: Registrar data from wps_registrar_init()
657 * Returns: 0 on success, -1 on failure
659 * PINs are locked to enforce only one concurrent use. This function unlocks a
660 * PIN to allow it to be used again. If the specified PIN was configured using
661 * a wildcard UUID, it will be removed instead of allowing multiple uses.
663 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
665 struct wps_uuid_pin *pin;
667 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
668 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
669 if (pin->wildcard_uuid == 2) {
670 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
672 return wps_registrar_invalidate_pin(reg, uuid);
674 pin->flags &= ~PIN_LOCKED;
683 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
685 reg->selected_registrar = 0;
687 wps_registrar_selected_registrar_changed(reg);
691 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
693 struct wps_registrar *reg = eloop_ctx;
695 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
696 wps_pbc_timeout_event(reg->wps);
697 wps_registrar_stop_pbc(reg);
702 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
703 * @reg: Registrar data from wps_registrar_init()
704 * Returns: 0 on success, -1 on failure
706 * This function is called on an AP when a push button is pushed to activate
707 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
708 * or when a PBC registration is completed.
710 int wps_registrar_button_pushed(struct wps_registrar *reg)
712 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
713 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
715 wps_pbc_overlap_event(reg->wps);
718 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
719 reg->force_pbc_overlap = 0;
720 reg->selected_registrar = 1;
722 wps_registrar_selected_registrar_changed(reg);
724 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
725 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
731 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
733 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
734 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
735 wps_registrar_stop_pbc(reg);
739 static void wps_registrar_pin_completed(struct wps_registrar *reg)
741 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
742 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
743 reg->selected_registrar = 0;
744 wps_registrar_selected_registrar_changed(reg);
749 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
750 * @reg: Registrar data from wps_registrar_init()
751 * @addr: MAC address of the Probe Request sender
752 * @wps_data: WPS IE contents
754 * This function is called on an AP when a Probe Request with WPS IE is
755 * received. This is used to track PBC mode use and to detect possible overlap
756 * situation with other WPS APs.
758 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
759 const struct wpabuf *wps_data)
761 struct wps_parse_attr attr;
764 wpa_hexdump_buf(MSG_MSGDUMP,
765 "WPS: Probe Request with WPS data received",
768 if (wps_parse_msg(wps_data, &attr) < 0)
770 if (!wps_version_supported(attr.version)) {
771 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
772 "version 0x%x", attr.version ? *attr.version : 0);
776 if (attr.config_methods == NULL) {
777 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
782 if (reg->enrollee_seen_cb && attr.dev_password_id && attr.uuid_e &&
783 attr.primary_dev_type && attr.request_type) {
784 char *dev_name = NULL;
786 dev_name = os_zalloc(attr.dev_name_len + 1);
788 os_memcpy(dev_name, attr.dev_name,
792 reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
793 attr.primary_dev_type,
794 WPA_GET_BE16(attr.config_methods),
795 WPA_GET_BE16(attr.dev_password_id),
796 *attr.request_type, dev_name);
800 methods = WPA_GET_BE16(attr.config_methods);
801 if (!(methods & WPS_CONFIG_PUSHBUTTON))
802 return; /* Not PBC */
804 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
805 MACSTR, MAC2STR(addr));
806 if (attr.uuid_e == NULL) {
807 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
812 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
813 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
814 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
815 reg->force_pbc_overlap = 1;
816 wps_pbc_overlap_event(reg->wps);
821 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
822 const u8 *psk, size_t psk_len)
824 if (reg->new_psk_cb == NULL)
827 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
831 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
832 const struct wps_device_data *dev)
834 if (reg->pin_needed_cb == NULL)
837 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
841 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
844 if (reg->reg_success_cb == NULL)
847 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
851 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
852 struct wpabuf *probe_resp_ie)
854 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
858 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
861 if (reg->set_sel_reg_cb == NULL)
864 if (reg->selected_registrar) {
865 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
867 methods |= WPS_CONFIG_PUSHBUTTON;
870 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
871 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
876 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
877 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
882 ie = wpabuf_alloc(wpabuf_len(data) + 100);
888 pos = wpabuf_head(data);
889 end = pos + wpabuf_len(data);
892 size_t frag_len = end - pos;
895 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
896 wpabuf_put_u8(ie, 4 + frag_len);
897 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
898 wpabuf_put_data(ie, pos, frag_len);
908 static int wps_set_ie(struct wps_registrar *reg)
910 struct wpabuf *beacon;
911 struct wpabuf *probe;
913 if (reg->set_ie_cb == NULL)
916 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
918 beacon = wpabuf_alloc(300);
921 probe = wpabuf_alloc(400);
927 if (wps_build_version(beacon) ||
928 wps_build_wps_state(reg->wps, beacon) ||
929 wps_build_ap_setup_locked(reg->wps, beacon) ||
930 wps_build_selected_registrar(reg, beacon) ||
931 wps_build_sel_reg_dev_password_id(reg, beacon) ||
932 wps_build_sel_reg_config_methods(reg, beacon) ||
933 wps_build_version(probe) ||
934 wps_build_wps_state(reg->wps, probe) ||
935 wps_build_ap_setup_locked(reg->wps, probe) ||
936 wps_build_selected_registrar(reg, probe) ||
937 wps_build_sel_reg_dev_password_id(reg, probe) ||
938 wps_build_sel_reg_config_methods(reg, probe) ||
939 wps_build_resp_type(reg, probe) ||
940 wps_build_uuid_e(probe, reg->wps->uuid) ||
941 wps_build_device_attrs(®->wps->dev, probe) ||
942 wps_build_probe_config_methods(reg, probe) ||
943 wps_build_rf_bands(®->wps->dev, probe)) {
949 beacon = wps_ie_encapsulate(beacon);
950 probe = wps_ie_encapsulate(probe);
952 if (!beacon || !probe) {
958 if (reg->static_wep_only) {
960 * Windows XP and Vista clients can get confused about
961 * EAP-Identity/Request when they probe the network with
962 * EAPOL-Start. In such a case, they may assume the network is
963 * using IEEE 802.1X and prompt user for a certificate while
964 * the correct (non-WPS) behavior would be to ask for the
965 * static WEP key. As a workaround, use Microsoft Provisioning
966 * IE to advertise that legacy 802.1X is not supported.
968 const u8 ms_wps[7] = {
969 WLAN_EID_VENDOR_SPECIFIC, 5,
970 /* Microsoft Provisioning IE (00:50:f2:5) */
972 0x00 /* no legacy 802.1X or MS WPS */
974 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
975 "into Beacon/Probe Response frames");
976 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
977 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
980 return wps_cb_set_ie(reg, beacon, probe);
984 static int wps_get_dev_password(struct wps_data *wps)
989 os_free(wps->dev_password);
990 wps->dev_password = NULL;
993 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
994 pin = (const u8 *) "00000000";
997 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1001 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1003 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1008 wps->dev_password = os_malloc(pin_len);
1009 if (wps->dev_password == NULL)
1011 os_memcpy(wps->dev_password, pin, pin_len);
1012 wps->dev_password_len = pin_len;
1018 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1020 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1021 wpabuf_put_be16(msg, ATTR_UUID_R);
1022 wpabuf_put_be16(msg, WPS_UUID_LEN);
1023 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1028 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1034 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1036 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1037 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1038 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1040 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1041 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1042 "R-Hash derivation");
1046 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1047 wpabuf_put_be16(msg, ATTR_R_HASH1);
1048 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1049 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1050 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1051 addr[0] = wps->snonce;
1052 len[0] = WPS_SECRET_NONCE_LEN;
1053 addr[1] = wps->psk1;
1054 len[1] = WPS_PSK_LEN;
1055 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1056 len[2] = wpabuf_len(wps->dh_pubkey_e);
1057 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1058 len[3] = wpabuf_len(wps->dh_pubkey_r);
1059 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1060 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1062 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1063 wpabuf_put_be16(msg, ATTR_R_HASH2);
1064 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1065 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1066 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1067 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1068 addr[1] = wps->psk2;
1069 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1070 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1076 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1078 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1079 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1080 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1081 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1086 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1088 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1089 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1090 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1091 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1092 WPS_SECRET_NONCE_LEN);
1097 static int wps_build_cred_network_idx(struct wpabuf *msg,
1098 const struct wps_credential *cred)
1100 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
1101 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1102 wpabuf_put_be16(msg, 1);
1103 wpabuf_put_u8(msg, 1);
1108 static int wps_build_cred_ssid(struct wpabuf *msg,
1109 const struct wps_credential *cred)
1111 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1112 wpabuf_put_be16(msg, ATTR_SSID);
1113 wpabuf_put_be16(msg, cred->ssid_len);
1114 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1119 static int wps_build_cred_auth_type(struct wpabuf *msg,
1120 const struct wps_credential *cred)
1122 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1124 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1125 wpabuf_put_be16(msg, 2);
1126 wpabuf_put_be16(msg, cred->auth_type);
1131 static int wps_build_cred_encr_type(struct wpabuf *msg,
1132 const struct wps_credential *cred)
1134 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1136 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1137 wpabuf_put_be16(msg, 2);
1138 wpabuf_put_be16(msg, cred->encr_type);
1143 static int wps_build_cred_network_key(struct wpabuf *msg,
1144 const struct wps_credential *cred)
1146 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1147 (int) cred->key_len);
1148 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1149 wpabuf_put_be16(msg, cred->key_len);
1150 wpabuf_put_data(msg, cred->key, cred->key_len);
1155 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1156 const struct wps_credential *cred)
1158 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1159 MAC2STR(cred->mac_addr));
1160 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1161 wpabuf_put_be16(msg, ETH_ALEN);
1162 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1167 static int wps_build_credential(struct wpabuf *msg,
1168 const struct wps_credential *cred)
1170 if (wps_build_cred_network_idx(msg, cred) ||
1171 wps_build_cred_ssid(msg, cred) ||
1172 wps_build_cred_auth_type(msg, cred) ||
1173 wps_build_cred_encr_type(msg, cred) ||
1174 wps_build_cred_network_key(msg, cred) ||
1175 wps_build_cred_mac_addr(msg, cred))
1181 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1183 struct wpabuf *cred;
1185 if (wps->wps->registrar->skip_cred_build)
1186 goto skip_cred_build;
1188 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1189 if (wps->use_cred) {
1190 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1193 os_memset(&wps->cred, 0, sizeof(wps->cred));
1195 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1196 wps->cred.ssid_len = wps->wps->ssid_len;
1198 /* Select the best authentication and encryption type */
1199 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1200 wps->auth_type = WPS_AUTH_WPA2PSK;
1201 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1202 wps->auth_type = WPS_AUTH_WPAPSK;
1203 else if (wps->auth_type & WPS_AUTH_OPEN)
1204 wps->auth_type = WPS_AUTH_OPEN;
1205 else if (wps->auth_type & WPS_AUTH_SHARED)
1206 wps->auth_type = WPS_AUTH_SHARED;
1208 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1212 wps->cred.auth_type = wps->auth_type;
1214 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1215 wps->auth_type == WPS_AUTH_WPAPSK) {
1216 if (wps->encr_type & WPS_ENCR_AES)
1217 wps->encr_type = WPS_ENCR_AES;
1218 else if (wps->encr_type & WPS_ENCR_TKIP)
1219 wps->encr_type = WPS_ENCR_TKIP;
1221 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1222 "type for WPA/WPA2");
1226 if (wps->encr_type & WPS_ENCR_WEP)
1227 wps->encr_type = WPS_ENCR_WEP;
1228 else if (wps->encr_type & WPS_ENCR_NONE)
1229 wps->encr_type = WPS_ENCR_NONE;
1231 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1232 "type for non-WPA/WPA2 mode");
1236 wps->cred.encr_type = wps->encr_type;
1238 * Set MAC address in the Credential to be the Enrollee's MAC address
1240 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1242 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1243 !wps->wps->registrar->disable_auto_conf) {
1245 /* Generate a random passphrase */
1246 if (os_get_random(r, sizeof(r)) < 0)
1248 os_free(wps->new_psk);
1249 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1250 if (wps->new_psk == NULL)
1252 wps->new_psk_len--; /* remove newline */
1253 while (wps->new_psk_len &&
1254 wps->new_psk[wps->new_psk_len - 1] == '=')
1256 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1257 wps->new_psk, wps->new_psk_len);
1258 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1259 wps->cred.key_len = wps->new_psk_len;
1260 } else if (wps->use_psk_key && wps->wps->psk_set) {
1262 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1263 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1264 os_memcpy(wps->cred.key, hex, 32 * 2);
1265 wps->cred.key_len = 32 * 2;
1266 } else if (wps->wps->network_key) {
1267 os_memcpy(wps->cred.key, wps->wps->network_key,
1268 wps->wps->network_key_len);
1269 wps->cred.key_len = wps->wps->network_key_len;
1270 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1272 /* Generate a random per-device PSK */
1273 os_free(wps->new_psk);
1274 wps->new_psk_len = 32;
1275 wps->new_psk = os_malloc(wps->new_psk_len);
1276 if (wps->new_psk == NULL)
1278 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1279 os_free(wps->new_psk);
1280 wps->new_psk = NULL;
1283 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1284 wps->new_psk, wps->new_psk_len);
1285 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1287 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1288 wps->cred.key_len = wps->new_psk_len * 2;
1292 cred = wpabuf_alloc(200);
1296 if (wps_build_credential(cred, &wps->cred)) {
1301 wpabuf_put_be16(msg, ATTR_CRED);
1302 wpabuf_put_be16(msg, wpabuf_len(cred));
1303 wpabuf_put_buf(msg, cred);
1307 if (wps->wps->registrar->extra_cred) {
1308 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1309 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1316 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1318 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1320 if (wps_build_credential(msg, &wps->cred))
1327 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1331 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1333 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1334 wps->nonce_r, WPS_NONCE_LEN);
1335 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1337 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1338 msg = wpabuf_alloc(1000);
1342 if (wps_build_version(msg) ||
1343 wps_build_msg_type(msg, WPS_M2) ||
1344 wps_build_enrollee_nonce(wps, msg) ||
1345 wps_build_registrar_nonce(wps, msg) ||
1346 wps_build_uuid_r(wps, msg) ||
1347 wps_build_public_key(wps, msg) ||
1348 wps_derive_keys(wps) ||
1349 wps_build_auth_type_flags(wps, msg) ||
1350 wps_build_encr_type_flags(wps, msg) ||
1351 wps_build_conn_type_flags(wps, msg) ||
1352 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1353 wps_build_device_attrs(&wps->wps->dev, msg) ||
1354 wps_build_rf_bands(&wps->wps->dev, msg) ||
1355 wps_build_assoc_state(wps, msg) ||
1356 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1357 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1358 wps_build_os_version(&wps->wps->dev, msg) ||
1359 wps_build_authenticator(wps, msg)) {
1365 wps->state = RECV_M3;
1370 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1373 u16 err = wps->config_error;
1375 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1376 msg = wpabuf_alloc(1000);
1380 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1381 err == WPS_CFG_NO_ERROR)
1382 err = WPS_CFG_SETUP_LOCKED;
1384 if (wps_build_version(msg) ||
1385 wps_build_msg_type(msg, WPS_M2D) ||
1386 wps_build_enrollee_nonce(wps, msg) ||
1387 wps_build_registrar_nonce(wps, msg) ||
1388 wps_build_uuid_r(wps, msg) ||
1389 wps_build_auth_type_flags(wps, msg) ||
1390 wps_build_encr_type_flags(wps, msg) ||
1391 wps_build_conn_type_flags(wps, msg) ||
1392 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1393 wps_build_device_attrs(&wps->wps->dev, msg) ||
1394 wps_build_rf_bands(&wps->wps->dev, msg) ||
1395 wps_build_assoc_state(wps, msg) ||
1396 wps_build_config_error(msg, err) ||
1397 wps_build_os_version(&wps->wps->dev, msg)) {
1402 wps->state = RECV_M2D_ACK;
1407 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1409 struct wpabuf *msg, *plain;
1411 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1413 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1415 plain = wpabuf_alloc(200);
1419 msg = wpabuf_alloc(1000);
1425 if (wps_build_version(msg) ||
1426 wps_build_msg_type(msg, WPS_M4) ||
1427 wps_build_enrollee_nonce(wps, msg) ||
1428 wps_build_r_hash(wps, msg) ||
1429 wps_build_r_snonce1(wps, plain) ||
1430 wps_build_key_wrap_auth(wps, plain) ||
1431 wps_build_encr_settings(wps, msg, plain) ||
1432 wps_build_authenticator(wps, msg)) {
1439 wps->state = RECV_M5;
1444 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1446 struct wpabuf *msg, *plain;
1448 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1450 plain = wpabuf_alloc(200);
1454 msg = wpabuf_alloc(1000);
1460 if (wps_build_version(msg) ||
1461 wps_build_msg_type(msg, WPS_M6) ||
1462 wps_build_enrollee_nonce(wps, msg) ||
1463 wps_build_r_snonce2(wps, plain) ||
1464 wps_build_key_wrap_auth(wps, plain) ||
1465 wps_build_encr_settings(wps, msg, plain) ||
1466 wps_build_authenticator(wps, msg)) {
1473 wps->wps_pin_revealed = 1;
1474 wps->state = RECV_M7;
1479 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1481 struct wpabuf *msg, *plain;
1483 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1485 plain = wpabuf_alloc(500);
1489 msg = wpabuf_alloc(1000);
1495 if (wps_build_version(msg) ||
1496 wps_build_msg_type(msg, WPS_M8) ||
1497 wps_build_enrollee_nonce(wps, msg) ||
1498 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1499 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1500 wps_build_key_wrap_auth(wps, plain) ||
1501 wps_build_encr_settings(wps, msg, plain) ||
1502 wps_build_authenticator(wps, msg)) {
1509 wps->state = RECV_DONE;
1514 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1518 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1520 msg = wpabuf_alloc(1000);
1524 if (wps_build_version(msg) ||
1525 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1526 wps_build_enrollee_nonce(wps, msg) ||
1527 wps_build_registrar_nonce(wps, msg)) {
1536 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1540 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1542 msg = wpabuf_alloc(1000);
1546 if (wps_build_version(msg) ||
1547 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1548 wps_build_enrollee_nonce(wps, msg) ||
1549 wps_build_registrar_nonce(wps, msg) ||
1550 wps_build_config_error(msg, wps->config_error)) {
1559 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1560 enum wsc_op_code *op_code)
1564 #ifdef CONFIG_WPS_UPNP
1565 if (!wps->int_reg && wps->wps->wps_upnp) {
1566 struct upnp_pending_message *p, *prev = NULL;
1567 if (wps->ext_reg > 1)
1568 wps_registrar_free_pending_m2(wps->wps);
1569 p = wps->wps->upnp_msgs;
1570 /* TODO: check pending message MAC address */
1571 while (p && p->next) {
1576 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1581 wps->wps->upnp_msgs = NULL;
1588 *op_code = WSC_NACK;
1595 if (wps->ext_reg == 0)
1601 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1602 "pending message available");
1605 #endif /* CONFIG_WPS_UPNP */
1607 switch (wps->state) {
1609 if (wps_get_dev_password(wps) < 0)
1610 msg = wps_build_m2d(wps);
1612 msg = wps_build_m2(wps);
1616 msg = wps_build_m2d(wps);
1620 msg = wps_build_m4(wps);
1624 msg = wps_build_m6(wps);
1628 msg = wps_build_m8(wps);
1632 msg = wps_build_wsc_ack(wps);
1636 msg = wps_build_wsc_nack(wps);
1637 *op_code = WSC_NACK;
1640 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1641 "a message", wps->state);
1646 if (*op_code == WSC_MSG && msg) {
1647 /* Save a copy of the last message for Authenticator derivation
1649 wpabuf_free(wps->last_msg);
1650 wps->last_msg = wpabuf_dup(msg);
1657 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1659 if (e_nonce == NULL) {
1660 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1664 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1665 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1666 wps->nonce_e, WPS_NONCE_LEN);
1672 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1674 if (r_nonce == NULL) {
1675 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1679 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1680 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1688 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1690 if (uuid_e == NULL) {
1691 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1695 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1696 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1702 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1704 if (pw_id == NULL) {
1705 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1709 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1710 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1716 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1718 if (e_hash1 == NULL) {
1719 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1723 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1724 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1730 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1732 if (e_hash2 == NULL) {
1733 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1737 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1738 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1744 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1746 u8 hash[SHA256_MAC_LEN];
1750 if (e_snonce1 == NULL) {
1751 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1755 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1756 WPS_SECRET_NONCE_LEN);
1758 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1759 addr[0] = e_snonce1;
1760 len[0] = WPS_SECRET_NONCE_LEN;
1761 addr[1] = wps->psk1;
1762 len[1] = WPS_PSK_LEN;
1763 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1764 len[2] = wpabuf_len(wps->dh_pubkey_e);
1765 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1766 len[3] = wpabuf_len(wps->dh_pubkey_r);
1767 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1769 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1770 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1771 "not match with the pre-committed value");
1772 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1773 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1777 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1778 "half of the device password");
1784 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1786 u8 hash[SHA256_MAC_LEN];
1790 if (e_snonce2 == NULL) {
1791 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1795 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1796 WPS_SECRET_NONCE_LEN);
1798 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1799 addr[0] = e_snonce2;
1800 len[0] = WPS_SECRET_NONCE_LEN;
1801 addr[1] = wps->psk2;
1802 len[1] = WPS_PSK_LEN;
1803 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1804 len[2] = wpabuf_len(wps->dh_pubkey_e);
1805 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1806 len[3] = wpabuf_len(wps->dh_pubkey_r);
1807 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1809 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1810 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1811 "not match with the pre-committed value");
1812 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1813 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1814 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1818 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1819 "half of the device password");
1820 wps->wps_pin_revealed = 0;
1821 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1827 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1829 if (mac_addr == NULL) {
1830 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1834 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1836 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1837 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1843 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1846 if (pk == NULL || pk_len == 0) {
1847 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1851 #ifdef CONFIG_WPS_OOB
1852 if (wps->wps->oob_conf.pubkey_hash != NULL) {
1854 u8 hash[WPS_HASH_LEN];
1857 sha256_vector(1, addr, &pk_len, hash);
1859 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
1860 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
1861 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
1865 #endif /* CONFIG_WPS_OOB */
1867 wpabuf_free(wps->dh_pubkey_e);
1868 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1869 if (wps->dh_pubkey_e == NULL)
1876 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1881 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1886 auth_types = WPA_GET_BE16(auth);
1888 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1890 wps->auth_type = wps->wps->auth_types & auth_types;
1891 if (wps->auth_type == 0) {
1892 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1893 "authentication types (own 0x%x Enrollee 0x%x)",
1894 wps->wps->auth_types, auth_types);
1895 #ifdef WPS_WORKAROUNDS
1897 * Some deployed implementations seem to advertise incorrect
1898 * information in this attribute. For example, Linksys WRT350N
1899 * seems to have a byteorder bug that breaks this negotiation.
1900 * In order to interoperate with existing implementations,
1901 * assume that the Enrollee supports everything we do.
1903 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1904 "does not advertise supported authentication types "
1906 wps->auth_type = wps->wps->auth_types;
1907 #else /* WPS_WORKAROUNDS */
1909 #endif /* WPS_WORKAROUNDS */
1916 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1921 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1926 encr_types = WPA_GET_BE16(encr);
1928 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1930 wps->encr_type = wps->wps->encr_types & encr_types;
1931 if (wps->encr_type == 0) {
1932 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1933 "encryption types (own 0x%x Enrollee 0x%x)",
1934 wps->wps->encr_types, encr_types);
1935 #ifdef WPS_WORKAROUNDS
1937 * Some deployed implementations seem to advertise incorrect
1938 * information in this attribute. For example, Linksys WRT350N
1939 * seems to have a byteorder bug that breaks this negotiation.
1940 * In order to interoperate with existing implementations,
1941 * assume that the Enrollee supports everything we do.
1943 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1944 "does not advertise supported encryption types "
1946 wps->encr_type = wps->wps->encr_types;
1947 #else /* WPS_WORKAROUNDS */
1949 #endif /* WPS_WORKAROUNDS */
1956 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1959 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1964 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1971 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1975 if (methods == NULL) {
1976 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1980 m = WPA_GET_BE16(methods);
1982 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
1983 "%s%s%s%s%s%s%s%s%s", m,
1984 m & WPS_CONFIG_USBA ? " [USBA]" : "",
1985 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
1986 m & WPS_CONFIG_LABEL ? " [Label]" : "",
1987 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
1988 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
1989 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
1990 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
1991 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
1992 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
1994 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
1996 * The Enrollee does not have a display so it is unlikely to be
1997 * able to show the passphrase to a user and as such, could
1998 * benefit from receiving PSK to reduce key derivation time.
2000 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2001 "Enrollee not supporting display");
2002 wps->use_psk_key = 1;
2009 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2011 if (state == NULL) {
2012 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2017 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2024 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2028 if (assoc == NULL) {
2029 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2033 a = WPA_GET_BE16(assoc);
2034 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2040 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2045 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2049 e = WPA_GET_BE16(err);
2050 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2056 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2057 struct wps_parse_attr *attr)
2059 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2061 if (wps->state != RECV_M1) {
2062 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2063 "receiving M1", wps->state);
2067 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2068 wps_process_mac_addr(wps, attr->mac_addr) ||
2069 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2070 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2071 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2072 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2073 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2074 wps_process_config_methods(wps, attr->config_methods) ||
2075 wps_process_wps_state(wps, attr->wps_state) ||
2076 wps_process_device_attrs(&wps->peer_dev, attr) ||
2077 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2078 wps_process_assoc_state(wps, attr->assoc_state) ||
2079 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2080 wps_process_config_error(wps, attr->config_error) ||
2081 wps_process_os_version(&wps->peer_dev, attr->os_version))
2084 if (wps->dev_pw_id < 0x10 &&
2085 wps->dev_pw_id != DEV_PW_DEFAULT &&
2086 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2087 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2088 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2089 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2090 !wps->wps->registrar->pbc)) {
2091 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2093 wps->state = SEND_M2D;
2094 return WPS_CONTINUE;
2097 #ifdef CONFIG_WPS_OOB
2098 if (wps->dev_pw_id >= 0x10 &&
2099 wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
2100 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
2101 "%d mismatch", wps->dev_pw_id);
2102 wps->state = SEND_M2D;
2103 return WPS_CONTINUE;
2105 #endif /* CONFIG_WPS_OOB */
2107 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2108 if (wps->wps->registrar->force_pbc_overlap ||
2109 wps_registrar_pbc_overlap(wps->wps->registrar,
2110 wps->mac_addr_e, wps->uuid_e)) {
2111 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2113 wps->state = SEND_M2D;
2114 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2115 wps_pbc_overlap_event(wps->wps);
2116 wps->wps->registrar->force_pbc_overlap = 1;
2117 return WPS_CONTINUE;
2119 wps_registrar_add_pbc_session(wps->wps->registrar,
2120 wps->mac_addr_e, wps->uuid_e);
2124 wps->state = SEND_M2;
2125 return WPS_CONTINUE;
2129 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2130 const struct wpabuf *msg,
2131 struct wps_parse_attr *attr)
2133 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2135 if (wps->state != RECV_M3) {
2136 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2137 "receiving M3", wps->state);
2138 wps->state = SEND_WSC_NACK;
2139 return WPS_CONTINUE;
2142 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2143 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2145 wps->state = SEND_WSC_NACK;
2146 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2147 return WPS_CONTINUE;
2150 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2151 wps_process_authenticator(wps, attr->authenticator, msg) ||
2152 wps_process_e_hash1(wps, attr->e_hash1) ||
2153 wps_process_e_hash2(wps, attr->e_hash2)) {
2154 wps->state = SEND_WSC_NACK;
2155 return WPS_CONTINUE;
2158 wps->state = SEND_M4;
2159 return WPS_CONTINUE;
2163 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2164 const struct wpabuf *msg,
2165 struct wps_parse_attr *attr)
2167 struct wpabuf *decrypted;
2168 struct wps_parse_attr eattr;
2170 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2172 if (wps->state != RECV_M5) {
2173 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2174 "receiving M5", wps->state);
2175 wps->state = SEND_WSC_NACK;
2176 return WPS_CONTINUE;
2179 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2180 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2182 wps->state = SEND_WSC_NACK;
2183 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2184 return WPS_CONTINUE;
2187 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2188 wps_process_authenticator(wps, attr->authenticator, msg)) {
2189 wps->state = SEND_WSC_NACK;
2190 return WPS_CONTINUE;
2193 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2194 attr->encr_settings_len);
2195 if (decrypted == NULL) {
2196 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2197 "Settings attribute");
2198 wps->state = SEND_WSC_NACK;
2199 return WPS_CONTINUE;
2202 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2204 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2205 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2206 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2207 wpabuf_free(decrypted);
2208 wps->state = SEND_WSC_NACK;
2209 return WPS_CONTINUE;
2211 wpabuf_free(decrypted);
2213 wps->state = SEND_M6;
2214 return WPS_CONTINUE;
2218 static void wps_sta_cred_cb(struct wps_data *wps)
2221 * Update credential to only include a single authentication and
2222 * encryption type in case the AP configuration includes more than one
2225 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2226 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2227 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2228 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2229 if (wps->cred.encr_type & WPS_ENCR_AES)
2230 wps->cred.encr_type = WPS_ENCR_AES;
2231 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2232 wps->cred.encr_type = WPS_ENCR_TKIP;
2233 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2234 "AP configuration");
2235 if (wps->wps->cred_cb)
2236 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2240 static void wps_cred_update(struct wps_credential *dst,
2241 struct wps_credential *src)
2243 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2244 dst->ssid_len = src->ssid_len;
2245 dst->auth_type = src->auth_type;
2246 dst->encr_type = src->encr_type;
2247 dst->key_idx = src->key_idx;
2248 os_memcpy(dst->key, src->key, sizeof(dst->key));
2249 dst->key_len = src->key_len;
2253 static int wps_process_ap_settings_r(struct wps_data *wps,
2254 struct wps_parse_attr *attr)
2256 if (wps->wps->ap || wps->er)
2259 /* AP Settings Attributes in M7 when Enrollee is an AP */
2260 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2263 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2265 if (wps->new_ap_settings) {
2266 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2268 wps_cred_update(&wps->cred, wps->new_ap_settings);
2272 * Use the AP PIN only to receive the current AP settings, not
2273 * to reconfigure the AP.
2275 if (wps->ap_settings_cb) {
2276 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2280 wps_sta_cred_cb(wps);
2286 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2287 const struct wpabuf *msg,
2288 struct wps_parse_attr *attr)
2290 struct wpabuf *decrypted;
2291 struct wps_parse_attr eattr;
2293 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2295 if (wps->state != RECV_M7) {
2296 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2297 "receiving M7", wps->state);
2298 wps->state = SEND_WSC_NACK;
2299 return WPS_CONTINUE;
2302 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2303 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2305 wps->state = SEND_WSC_NACK;
2306 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2307 return WPS_CONTINUE;
2310 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2311 wps_process_authenticator(wps, attr->authenticator, msg)) {
2312 wps->state = SEND_WSC_NACK;
2313 return WPS_CONTINUE;
2316 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2317 attr->encr_settings_len);
2318 if (decrypted == NULL) {
2319 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2320 "Settings attribute");
2321 wps->state = SEND_WSC_NACK;
2322 return WPS_CONTINUE;
2325 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2327 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2328 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2329 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2330 wps_process_ap_settings_r(wps, &eattr)) {
2331 wpabuf_free(decrypted);
2332 wps->state = SEND_WSC_NACK;
2333 return WPS_CONTINUE;
2336 wpabuf_free(decrypted);
2338 wps->state = SEND_M8;
2339 return WPS_CONTINUE;
2343 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2344 const struct wpabuf *msg)
2346 struct wps_parse_attr attr;
2347 enum wps_process_res ret = WPS_CONTINUE;
2349 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2351 if (wps_parse_msg(msg, &attr) < 0)
2354 if (!wps_version_supported(attr.version)) {
2355 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2356 attr.version ? *attr.version : 0);
2360 if (attr.msg_type == NULL) {
2361 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2365 if (*attr.msg_type != WPS_M1 &&
2366 (attr.registrar_nonce == NULL ||
2367 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2368 WPS_NONCE_LEN != 0))) {
2369 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2373 switch (*attr.msg_type) {
2375 #ifdef CONFIG_WPS_UPNP
2376 if (wps->wps->wps_upnp && attr.mac_addr) {
2377 /* Remove old pending messages when starting new run */
2378 wps_free_pending_msgs(wps->wps->upnp_msgs);
2379 wps->wps->upnp_msgs = NULL;
2381 upnp_wps_device_send_wlan_event(
2382 wps->wps->wps_upnp, attr.mac_addr,
2383 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2385 #endif /* CONFIG_WPS_UPNP */
2386 ret = wps_process_m1(wps, &attr);
2389 ret = wps_process_m3(wps, msg, &attr);
2390 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2391 wps_fail_event(wps->wps, WPS_M3);
2394 ret = wps_process_m5(wps, msg, &attr);
2395 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2396 wps_fail_event(wps->wps, WPS_M5);
2399 ret = wps_process_m7(wps, msg, &attr);
2400 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2401 wps_fail_event(wps->wps, WPS_M7);
2404 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2409 if (ret == WPS_CONTINUE) {
2410 /* Save a copy of the last message for Authenticator derivation
2412 wpabuf_free(wps->last_msg);
2413 wps->last_msg = wpabuf_dup(msg);
2420 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2421 const struct wpabuf *msg)
2423 struct wps_parse_attr attr;
2425 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2427 if (wps_parse_msg(msg, &attr) < 0)
2430 if (!wps_version_supported(attr.version)) {
2431 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2432 attr.version ? *attr.version : 0);
2436 if (attr.msg_type == NULL) {
2437 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2441 if (*attr.msg_type != WPS_WSC_ACK) {
2442 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2447 #ifdef CONFIG_WPS_UPNP
2448 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2449 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2450 if (wps->wps->upnp_msgs)
2451 return WPS_CONTINUE;
2452 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2453 "external Registrar");
2456 #endif /* CONFIG_WPS_UPNP */
2458 if (attr.registrar_nonce == NULL ||
2459 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2461 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2465 if (attr.enrollee_nonce == NULL ||
2466 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2467 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2471 if (wps->state == RECV_M2D_ACK) {
2472 #ifdef CONFIG_WPS_UPNP
2473 if (wps->wps->wps_upnp &&
2474 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2475 if (wps->wps->upnp_msgs)
2476 return WPS_CONTINUE;
2477 if (wps->ext_reg == 0)
2479 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2480 "external Registrar");
2483 #endif /* CONFIG_WPS_UPNP */
2485 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2486 "terminate negotiation");
2493 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2494 const struct wpabuf *msg)
2496 struct wps_parse_attr attr;
2499 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2501 old_state = wps->state;
2502 wps->state = SEND_WSC_NACK;
2504 if (wps_parse_msg(msg, &attr) < 0)
2507 if (!wps_version_supported(attr.version)) {
2508 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2509 attr.version ? *attr.version : 0);
2513 if (attr.msg_type == NULL) {
2514 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2518 if (*attr.msg_type != WPS_WSC_NACK) {
2519 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2524 #ifdef CONFIG_WPS_UPNP
2525 if (wps->wps->wps_upnp && wps->ext_reg) {
2526 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2527 "Registrar terminated by the Enrollee");
2530 #endif /* CONFIG_WPS_UPNP */
2532 if (attr.registrar_nonce == NULL ||
2533 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2535 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2539 if (attr.enrollee_nonce == NULL ||
2540 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2541 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2545 if (attr.config_error == NULL) {
2546 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2551 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2552 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2554 switch (old_state) {
2556 wps_fail_event(wps->wps, WPS_M2);
2559 wps_fail_event(wps->wps, WPS_M4);
2562 wps_fail_event(wps->wps, WPS_M6);
2565 wps_fail_event(wps->wps, WPS_M8);
2575 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2576 const struct wpabuf *msg)
2578 struct wps_parse_attr attr;
2580 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2582 if (wps->state != RECV_DONE &&
2583 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2584 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2585 "receiving WSC_Done", wps->state);
2589 if (wps_parse_msg(msg, &attr) < 0)
2592 if (!wps_version_supported(attr.version)) {
2593 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2594 attr.version ? *attr.version : 0);
2598 if (attr.msg_type == NULL) {
2599 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2603 if (*attr.msg_type != WPS_WSC_DONE) {
2604 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2609 #ifdef CONFIG_WPS_UPNP
2610 if (wps->wps->wps_upnp && wps->ext_reg) {
2611 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2612 "Registrar completed successfully");
2613 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2617 #endif /* CONFIG_WPS_UPNP */
2619 if (attr.registrar_nonce == NULL ||
2620 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2622 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2626 if (attr.enrollee_nonce == NULL ||
2627 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2628 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2632 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2633 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2636 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2637 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2638 struct wps_credential cred;
2640 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2641 "on first Enrollee connection");
2643 os_memset(&cred, 0, sizeof(cred));
2644 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2645 cred.ssid_len = wps->wps->ssid_len;
2646 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2647 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2648 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2649 cred.key_len = wps->new_psk_len;
2651 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2652 wpa_hexdump_ascii_key(MSG_DEBUG,
2653 "WPS: Generated random passphrase",
2654 wps->new_psk, wps->new_psk_len);
2655 if (wps->wps->cred_cb)
2656 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2658 os_free(wps->new_psk);
2659 wps->new_psk = NULL;
2662 if (!wps->wps->ap && !wps->er)
2663 wps_sta_cred_cb(wps);
2666 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2667 wps->new_psk, wps->new_psk_len)) {
2668 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2671 os_free(wps->new_psk);
2672 wps->new_psk = NULL;
2675 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2678 wps_registrar_remove_pbc_session(wps->wps->registrar,
2679 wps->mac_addr_e, wps->uuid_e);
2680 wps_registrar_pbc_completed(wps->wps->registrar);
2682 wps_registrar_pin_completed(wps->wps->registrar);
2685 wps_success_event(wps->wps);
2691 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2692 enum wsc_op_code op_code,
2693 const struct wpabuf *msg)
2695 enum wps_process_res ret;
2697 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2699 (unsigned long) wpabuf_len(msg), op_code);
2701 #ifdef CONFIG_WPS_UPNP
2702 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2703 struct wps_parse_attr attr;
2704 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2705 *attr.msg_type == WPS_M3)
2706 wps->ext_reg = 2; /* past M2/M2D phase */
2708 if (wps->ext_reg > 1)
2709 wps_registrar_free_pending_m2(wps->wps);
2710 if (wps->wps->wps_upnp && wps->ext_reg &&
2711 wps->wps->upnp_msgs == NULL &&
2712 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
2714 struct wps_parse_attr attr;
2716 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2719 type = *attr.msg_type;
2720 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2721 " to external Registrar for processing", type);
2722 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2724 UPNP_WPS_WLANEVENT_TYPE_EAP,
2726 if (op_code == WSC_MSG)
2728 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2729 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2730 "external Registrar");
2731 return WPS_CONTINUE;
2733 #endif /* CONFIG_WPS_UPNP */
2737 return wps_process_wsc_msg(wps, msg);
2739 return wps_process_wsc_ack(wps, msg);
2741 return wps_process_wsc_nack(wps, msg);
2743 ret = wps_process_wsc_done(wps, msg);
2744 if (ret == WPS_FAILURE) {
2745 wps->state = SEND_WSC_NACK;
2746 wps_fail_event(wps->wps, WPS_WSC_DONE);
2750 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2756 int wps_registrar_update_ie(struct wps_registrar *reg)
2758 return wps_set_ie(reg);
2762 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2765 struct wps_registrar *reg = eloop_ctx;
2767 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
2768 "unselect internal Registrar");
2769 reg->selected_registrar = 0;
2771 wps_registrar_selected_registrar_changed(reg);
2775 #ifdef CONFIG_WPS_UPNP
2776 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
2777 struct subscription *s)
2779 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
2780 "config_methods=0x%x)",
2781 s->dev_password_id, s->config_methods);
2782 reg->sel_reg_union = 1;
2783 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
2784 reg->sel_reg_dev_password_id_override = s->dev_password_id;
2785 if (reg->sel_reg_config_methods_override == -1)
2786 reg->sel_reg_config_methods_override = 0;
2787 reg->sel_reg_config_methods_override |= s->config_methods;
2789 #endif /* CONFIG_WPS_UPNP */
2792 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
2794 #ifdef CONFIG_WPS_UPNP
2795 struct subscription *s;
2797 if (reg->wps->wps_upnp == NULL)
2800 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
2801 struct subscription, list) {
2802 struct subscr_addr *sa;
2803 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
2805 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
2806 inet_ntoa(sa->saddr.sin_addr),
2807 ntohs(sa->saddr.sin_port));
2809 if (s->selected_registrar)
2810 wps_registrar_sel_reg_add(reg, s);
2812 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
2815 #endif /* CONFIG_WPS_UPNP */
2820 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
2821 * @reg: Registrar data from wps_registrar_init()
2823 * This function is called when selected registrar state changes, e.g., when an
2824 * AP receives a SetSelectedRegistrar UPnP message.
2826 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg)
2828 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
2830 reg->sel_reg_union = reg->selected_registrar;
2831 reg->sel_reg_dev_password_id_override = -1;
2832 reg->sel_reg_config_methods_override = -1;
2833 if (reg->selected_registrar) {
2834 reg->sel_reg_config_methods_override =
2835 reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
2837 reg->sel_reg_dev_password_id_override =
2839 reg->sel_reg_config_methods_override |=
2840 WPS_CONFIG_PUSHBUTTON;
2842 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
2843 "(pbc=%d)", reg->pbc);
2845 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
2847 wps_registrar_sel_reg_union(reg);
2850 wps_cb_set_sel_reg(reg);
2854 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
2855 char *buf, size_t buflen)
2857 struct wps_registrar_device *d;
2860 char devtype[WPS_DEV_TYPE_BUFSIZE];
2862 d = wps_device_get(reg, addr);
2865 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
2868 ret = os_snprintf(buf + len, buflen - len,
2870 "wpsPrimaryDeviceType=%s\n"
2871 "wpsDeviceName=%s\n"
2872 "wpsManufacturer=%s\n"
2874 "wpsModelNumber=%s\n"
2875 "wpsSerialNumber=%s\n",
2877 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
2879 d->dev.device_name ? d->dev.device_name : "",
2880 d->dev.manufacturer ? d->dev.manufacturer : "",
2881 d->dev.model_name ? d->dev.model_name : "",
2882 d->dev.model_number ? d->dev.model_number : "",
2883 d->dev.serial_number ? d->dev.serial_number : "");
2884 if (ret < 0 || (size_t) ret >= buflen - len)