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.
20 #include "crypto/crypto.h"
21 #include "crypto/sha256.h"
22 #include "common/ieee802_11_defs.h"
24 #include "wps_dev_attr.h"
28 #define WPS_WORKAROUNDS
31 struct wps_uuid_pin *next;
32 u8 uuid[WPS_UUID_LEN];
36 #define PIN_LOCKED BIT(0)
37 #define PIN_EXPIRES BIT(1)
39 struct os_time expiration;
43 static void wps_free_pin(struct wps_uuid_pin *pin)
50 static void wps_free_pins(struct wps_uuid_pin *pins)
52 struct wps_uuid_pin *pin, *prev;
63 struct wps_pbc_session {
64 struct wps_pbc_session *next;
66 u8 uuid_e[WPS_UUID_LEN];
67 struct os_time timestamp;
71 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
73 struct wps_pbc_session *prev;
83 struct wps_registrar_device {
84 struct wps_registrar_device *next;
85 struct wps_device_data dev;
86 u8 uuid[WPS_UUID_LEN];
90 struct wps_registrar {
91 struct wps_context *wps;
94 int selected_registrar;
96 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
98 int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
99 const u8 *probe_resp_ie, size_t probe_resp_ie_len);
100 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
101 const struct wps_device_data *dev);
102 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
104 void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
105 u16 sel_reg_config_methods);
108 struct wps_uuid_pin *pins;
109 struct wps_pbc_session *pbc_sessions;
112 struct wpabuf *extra_cred;
113 int disable_auto_conf;
114 int sel_reg_dev_password_id_override;
115 int sel_reg_config_methods_override;
118 struct wps_registrar_device *devices;
120 int force_pbc_overlap;
124 static int wps_set_ie(struct wps_registrar *reg);
125 static void wps_cb_set_sel_reg(struct wps_registrar *reg);
126 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
127 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
131 static void wps_free_devices(struct wps_registrar_device *dev)
133 struct wps_registrar_device *prev;
138 wps_device_data_free(&prev->dev);
144 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
147 struct wps_registrar_device *dev;
149 for (dev = reg->devices; dev; dev = dev->next) {
150 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
157 static void wps_device_clone_data(struct wps_device_data *dst,
158 struct wps_device_data *src)
160 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
161 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
163 #define WPS_STRDUP(n) \
165 dst->n = src->n ? os_strdup(src->n) : NULL
167 WPS_STRDUP(device_name);
168 WPS_STRDUP(manufacturer);
169 WPS_STRDUP(model_name);
170 WPS_STRDUP(model_number);
171 WPS_STRDUP(serial_number);
176 int wps_device_store(struct wps_registrar *reg,
177 struct wps_device_data *dev, const u8 *uuid)
179 struct wps_registrar_device *d;
181 d = wps_device_get(reg, dev->mac_addr);
183 d = os_zalloc(sizeof(*d));
186 d->next = reg->devices;
190 wps_device_clone_data(&d->dev, dev);
191 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
197 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
198 const u8 *addr, const u8 *uuid_e)
200 struct wps_pbc_session *pbc, *prev = NULL;
205 pbc = reg->pbc_sessions;
207 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
208 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
210 prev->next = pbc->next;
212 reg->pbc_sessions = pbc->next;
220 pbc = os_zalloc(sizeof(*pbc));
223 os_memcpy(pbc->addr, addr, ETH_ALEN);
225 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
228 pbc->next = reg->pbc_sessions;
229 reg->pbc_sessions = pbc;
230 pbc->timestamp = now;
232 /* remove entries that have timed out */
237 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
239 wps_free_pbc_sessions(pbc);
248 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
249 const u8 *addr, const u8 *uuid_e)
251 struct wps_pbc_session *pbc, *prev = NULL;
253 pbc = reg->pbc_sessions;
255 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
256 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
258 prev->next = pbc->next;
260 reg->pbc_sessions = pbc->next;
270 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
271 const u8 *addr, const u8 *uuid_e)
274 struct wps_pbc_session *pbc;
279 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
280 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
282 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
284 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
291 return count > 1 ? 1 : 0;
295 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
297 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
299 wpabuf_put_be16(msg, ATTR_WPS_STATE);
300 wpabuf_put_be16(msg, 1);
301 wpabuf_put_u8(msg, wps->wps_state);
306 #ifdef CONFIG_WPS_UPNP
307 static void wps_registrar_free_pending_m2(struct wps_context *wps)
309 struct upnp_pending_message *p, *p2, *prev = NULL;
312 if (p->type == WPS_M2 || p->type == WPS_M2D) {
314 wps->upnp_msgs = p->next;
316 prev->next = p->next;
317 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
320 wpabuf_free(p2->msg);
328 #endif /* CONFIG_WPS_UPNP */
331 static int wps_build_ap_setup_locked(struct wps_context *wps,
334 if (wps->ap_setup_locked) {
335 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
336 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
337 wpabuf_put_be16(msg, 1);
338 wpabuf_put_u8(msg, 1);
344 static int wps_build_selected_registrar(struct wps_registrar *reg,
347 if (!reg->selected_registrar)
349 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
350 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
351 wpabuf_put_be16(msg, 1);
352 wpabuf_put_u8(msg, 1);
357 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
360 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
361 if (!reg->selected_registrar)
363 if (reg->sel_reg_dev_password_id_override >= 0)
364 id = reg->sel_reg_dev_password_id_override;
365 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
366 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
367 wpabuf_put_be16(msg, 2);
368 wpabuf_put_be16(msg, id);
373 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
377 if (!reg->selected_registrar)
379 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
381 methods |= WPS_CONFIG_PUSHBUTTON;
382 if (reg->sel_reg_config_methods_override >= 0)
383 methods = reg->sel_reg_config_methods_override;
384 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
386 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
387 wpabuf_put_be16(msg, 2);
388 wpabuf_put_be16(msg, methods);
393 static int wps_build_probe_config_methods(struct wps_registrar *reg,
398 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
399 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
400 wpabuf_put_be16(msg, 2);
401 wpabuf_put_be16(msg, methods);
406 static int wps_build_config_methods_r(struct wps_registrar *reg,
410 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
412 methods |= WPS_CONFIG_PUSHBUTTON;
413 return wps_build_config_methods(msg, methods);
417 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
419 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
420 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
421 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
422 wpabuf_put_be16(msg, 1);
423 wpabuf_put_u8(msg, resp);
429 * wps_registrar_init - Initialize WPS Registrar data
430 * @wps: Pointer to longterm WPS context
431 * @cfg: Registrar configuration
432 * Returns: Pointer to allocated Registrar data or %NULL on failure
434 * This function is used to initialize WPS Registrar functionality. It can be
435 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
436 * runs (e.g., when run as an internal Registrar in an AP). Caller is
437 * responsible for freeing the returned data with wps_registrar_deinit() when
438 * Registrar functionality is not needed anymore.
440 struct wps_registrar *
441 wps_registrar_init(struct wps_context *wps,
442 const struct wps_registrar_config *cfg)
444 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
449 reg->new_psk_cb = cfg->new_psk_cb;
450 reg->set_ie_cb = cfg->set_ie_cb;
451 reg->pin_needed_cb = cfg->pin_needed_cb;
452 reg->reg_success_cb = cfg->reg_success_cb;
453 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
454 reg->cb_ctx = cfg->cb_ctx;
455 reg->skip_cred_build = cfg->skip_cred_build;
456 if (cfg->extra_cred) {
457 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
458 cfg->extra_cred_len);
459 if (reg->extra_cred == NULL) {
464 reg->disable_auto_conf = cfg->disable_auto_conf;
465 reg->sel_reg_dev_password_id_override = -1;
466 reg->sel_reg_config_methods_override = -1;
467 reg->static_wep_only = cfg->static_wep_only;
469 if (wps_set_ie(reg)) {
470 wps_registrar_deinit(reg);
479 * wps_registrar_deinit - Deinitialize WPS Registrar data
480 * @reg: Registrar data from wps_registrar_init()
482 void wps_registrar_deinit(struct wps_registrar *reg)
486 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
487 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
488 wps_free_pins(reg->pins);
489 wps_free_pbc_sessions(reg->pbc_sessions);
490 wpabuf_free(reg->extra_cred);
491 wps_free_devices(reg->devices);
497 * wps_registrar_add_pin - Configure a new PIN for Registrar
498 * @reg: Registrar data from wps_registrar_init()
499 * @uuid: UUID-E or %NULL for wildcard (any UUID)
500 * @pin: PIN (Device Password)
501 * @pin_len: Length of pin in octets
502 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
503 * Returns: 0 on success, -1 on failure
505 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
506 const u8 *pin, size_t pin_len, int timeout)
508 struct wps_uuid_pin *p;
510 p = os_zalloc(sizeof(*p));
514 p->wildcard_uuid = 1;
516 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
517 p->pin = os_malloc(pin_len);
518 if (p->pin == NULL) {
522 os_memcpy(p->pin, pin, pin_len);
523 p->pin_len = pin_len;
526 p->flags |= PIN_EXPIRES;
527 os_get_time(&p->expiration);
528 p->expiration.sec += timeout;
534 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
536 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
537 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
538 reg->selected_registrar = 1;
541 wps_cb_set_sel_reg(reg);
542 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
543 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
544 wps_registrar_set_selected_timeout,
551 static void wps_registrar_expire_pins(struct wps_registrar *reg)
553 struct wps_uuid_pin *pin, *prev, *del;
560 if ((pin->flags & PIN_EXPIRES) &&
561 os_time_before(&pin->expiration, &now)) {
563 reg->pins = pin->next;
565 prev->next = pin->next;
568 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
569 del->uuid, WPS_UUID_LEN);
580 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
581 * @reg: Registrar data from wps_registrar_init()
583 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
585 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
587 struct wps_uuid_pin *pin, *prev;
592 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
594 reg->pins = pin->next;
596 prev->next = pin->next;
597 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
598 pin->uuid, WPS_UUID_LEN);
610 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
611 const u8 *uuid, size_t *pin_len)
613 struct wps_uuid_pin *pin;
615 wps_registrar_expire_pins(reg);
619 if (!pin->wildcard_uuid &&
620 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
626 /* Check for wildcard UUIDs since none of the UUID-specific
630 if (pin->wildcard_uuid == 1) {
631 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
632 "PIN. Assigned it for this UUID-E");
633 pin->wildcard_uuid = 2;
634 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
645 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
646 * that could otherwise avoid PIN invalidations.
648 if (pin->flags & PIN_LOCKED) {
649 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
650 "allow concurrent re-use");
653 *pin_len = pin->pin_len;
654 pin->flags |= PIN_LOCKED;
660 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
661 * @reg: Registrar data from wps_registrar_init()
663 * Returns: 0 on success, -1 on failure
665 * PINs are locked to enforce only one concurrent use. This function unlocks a
666 * PIN to allow it to be used again. If the specified PIN was configured using
667 * a wildcard UUID, it will be removed instead of allowing multiple uses.
669 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
671 struct wps_uuid_pin *pin;
675 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
676 if (pin->wildcard_uuid == 2) {
677 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
679 return wps_registrar_invalidate_pin(reg, uuid);
681 pin->flags &= ~PIN_LOCKED;
691 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
693 reg->selected_registrar = 0;
696 wps_cb_set_sel_reg(reg);
700 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
702 struct wps_registrar *reg = eloop_ctx;
704 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
705 wps_pbc_timeout_event(reg->wps);
706 wps_registrar_stop_pbc(reg);
711 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
712 * @reg: Registrar data from wps_registrar_init()
713 * Returns: 0 on success, -1 on failure
715 * This function is called on an AP when a push button is pushed to activate
716 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
717 * or when a PBC registration is completed.
719 int wps_registrar_button_pushed(struct wps_registrar *reg)
721 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
722 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
724 wps_pbc_overlap_event(reg->wps);
727 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
728 reg->force_pbc_overlap = 0;
729 reg->selected_registrar = 1;
732 wps_cb_set_sel_reg(reg);
734 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
735 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
741 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
743 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
744 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
745 wps_registrar_stop_pbc(reg);
748 static void wps_registrar_pin_completed(struct wps_registrar *reg)
750 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
751 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
752 reg->selected_registrar = 0;
754 wps_cb_set_sel_reg(reg);
759 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
760 * @reg: Registrar data from wps_registrar_init()
761 * @addr: MAC address of the Probe Request sender
762 * @wps_data: WPS IE contents
764 * This function is called on an AP when a Probe Request with WPS IE is
765 * received. This is used to track PBC mode use and to detect possible overlap
766 * situation with other WPS APs.
768 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
769 const struct wpabuf *wps_data)
771 struct wps_parse_attr attr;
774 wpa_hexdump_buf(MSG_MSGDUMP,
775 "WPS: Probe Request with WPS data received",
778 if (wps_parse_msg(wps_data, &attr) < 0)
780 if (!wps_version_supported(attr.version)) {
781 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
782 "version 0x%x", attr.version ? *attr.version : 0);
786 if (attr.config_methods == NULL) {
787 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
792 methods = WPA_GET_BE16(attr.config_methods);
793 if (!(methods & WPS_CONFIG_PUSHBUTTON))
794 return; /* Not PBC */
796 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
797 MACSTR, MAC2STR(addr));
799 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
800 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
801 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
802 reg->force_pbc_overlap = 1;
803 wps_pbc_overlap_event(reg->wps);
808 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
809 const u8 *psk, size_t psk_len)
811 if (reg->new_psk_cb == NULL)
814 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
818 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
819 const struct wps_device_data *dev)
821 if (reg->pin_needed_cb == NULL)
824 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
828 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
831 if (reg->reg_success_cb == NULL)
834 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
838 static int wps_cb_set_ie(struct wps_registrar *reg,
839 const struct wpabuf *beacon_ie,
840 const struct wpabuf *probe_resp_ie)
842 if (reg->set_ie_cb == NULL)
845 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
846 wpabuf_len(beacon_ie),
847 wpabuf_head(probe_resp_ie),
848 wpabuf_len(probe_resp_ie));
852 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
855 if (reg->set_sel_reg_cb == NULL)
858 if (reg->selected_registrar) {
859 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
861 methods |= WPS_CONFIG_PUSHBUTTON;
864 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
865 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
870 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
871 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
876 ie = wpabuf_alloc(wpabuf_len(data) + 100);
882 pos = wpabuf_head(data);
883 end = pos + wpabuf_len(data);
886 size_t frag_len = end - pos;
889 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
890 wpabuf_put_u8(ie, 4 + frag_len);
891 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
892 wpabuf_put_data(ie, pos, frag_len);
902 static int wps_set_ie(struct wps_registrar *reg)
904 struct wpabuf *beacon;
905 struct wpabuf *probe;
908 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
910 beacon = wpabuf_alloc(300);
913 probe = wpabuf_alloc(400);
919 if (wps_build_version(beacon) ||
920 wps_build_wps_state(reg->wps, beacon) ||
921 wps_build_ap_setup_locked(reg->wps, beacon) ||
922 wps_build_selected_registrar(reg, beacon) ||
923 wps_build_sel_reg_dev_password_id(reg, beacon) ||
924 wps_build_sel_reg_config_methods(reg, beacon) ||
925 wps_build_version(probe) ||
926 wps_build_wps_state(reg->wps, probe) ||
927 wps_build_ap_setup_locked(reg->wps, probe) ||
928 wps_build_selected_registrar(reg, probe) ||
929 wps_build_sel_reg_dev_password_id(reg, probe) ||
930 wps_build_sel_reg_config_methods(reg, probe) ||
931 wps_build_resp_type(reg, probe) ||
932 wps_build_uuid_e(probe, reg->wps->uuid) ||
933 wps_build_device_attrs(®->wps->dev, probe) ||
934 wps_build_probe_config_methods(reg, probe) ||
935 wps_build_rf_bands(®->wps->dev, probe)) {
941 beacon = wps_ie_encapsulate(beacon);
942 probe = wps_ie_encapsulate(probe);
944 if (!beacon || !probe) {
950 if (reg->static_wep_only) {
952 * Windows XP and Vista clients can get confused about
953 * EAP-Identity/Request when they probe the network with
954 * EAPOL-Start. In such a case, they may assume the network is
955 * using IEEE 802.1X and prompt user for a certificate while
956 * the correct (non-WPS) behavior would be to ask for the
957 * static WEP key. As a workaround, use Microsoft Provisioning
958 * IE to advertise that legacy 802.1X is not supported.
960 const u8 ms_wps[7] = {
961 WLAN_EID_VENDOR_SPECIFIC, 5,
962 /* Microsoft Provisioning IE (00:50:f2:5) */
964 0x00 /* no legacy 802.1X or MS WPS */
966 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
967 "into Beacon/Probe Response frames");
968 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
969 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
972 ret = wps_cb_set_ie(reg, beacon, probe);
980 static int wps_get_dev_password(struct wps_data *wps)
985 os_free(wps->dev_password);
986 wps->dev_password = NULL;
989 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
990 pin = (const u8 *) "00000000";
993 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
997 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
999 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1004 wps->dev_password = os_malloc(pin_len);
1005 if (wps->dev_password == NULL)
1007 os_memcpy(wps->dev_password, pin, pin_len);
1008 wps->dev_password_len = pin_len;
1014 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1016 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1017 wpabuf_put_be16(msg, ATTR_UUID_R);
1018 wpabuf_put_be16(msg, WPS_UUID_LEN);
1019 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1024 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1030 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1032 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1033 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1034 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1036 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1037 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1038 "R-Hash derivation");
1042 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1043 wpabuf_put_be16(msg, ATTR_R_HASH1);
1044 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1045 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1046 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1047 addr[0] = wps->snonce;
1048 len[0] = WPS_SECRET_NONCE_LEN;
1049 addr[1] = wps->psk1;
1050 len[1] = WPS_PSK_LEN;
1051 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1052 len[2] = wpabuf_len(wps->dh_pubkey_e);
1053 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1054 len[3] = wpabuf_len(wps->dh_pubkey_r);
1055 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1056 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1058 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1059 wpabuf_put_be16(msg, ATTR_R_HASH2);
1060 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1061 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1062 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1063 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1064 addr[1] = wps->psk2;
1065 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1066 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1072 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1074 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1075 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1076 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1077 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1082 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1084 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1085 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1086 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1087 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1088 WPS_SECRET_NONCE_LEN);
1093 static int wps_build_cred_network_idx(struct wpabuf *msg,
1094 const struct wps_credential *cred)
1096 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
1097 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1098 wpabuf_put_be16(msg, 1);
1099 wpabuf_put_u8(msg, 1);
1104 static int wps_build_cred_ssid(struct wpabuf *msg,
1105 const struct wps_credential *cred)
1107 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1108 wpabuf_put_be16(msg, ATTR_SSID);
1109 wpabuf_put_be16(msg, cred->ssid_len);
1110 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1115 static int wps_build_cred_auth_type(struct wpabuf *msg,
1116 const struct wps_credential *cred)
1118 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1120 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1121 wpabuf_put_be16(msg, 2);
1122 wpabuf_put_be16(msg, cred->auth_type);
1127 static int wps_build_cred_encr_type(struct wpabuf *msg,
1128 const struct wps_credential *cred)
1130 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1132 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1133 wpabuf_put_be16(msg, 2);
1134 wpabuf_put_be16(msg, cred->encr_type);
1139 static int wps_build_cred_network_key(struct wpabuf *msg,
1140 const struct wps_credential *cred)
1142 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
1143 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1144 wpabuf_put_be16(msg, cred->key_len);
1145 wpabuf_put_data(msg, cred->key, cred->key_len);
1150 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1151 const struct wps_credential *cred)
1153 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1154 MAC2STR(cred->mac_addr));
1155 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1156 wpabuf_put_be16(msg, ETH_ALEN);
1157 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1162 static int wps_build_credential(struct wpabuf *msg,
1163 const struct wps_credential *cred)
1165 if (wps_build_cred_network_idx(msg, cred) ||
1166 wps_build_cred_ssid(msg, cred) ||
1167 wps_build_cred_auth_type(msg, cred) ||
1168 wps_build_cred_encr_type(msg, cred) ||
1169 wps_build_cred_network_key(msg, cred) ||
1170 wps_build_cred_mac_addr(msg, cred))
1176 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1178 struct wpabuf *cred;
1180 if (wps->wps->registrar->skip_cred_build)
1181 goto skip_cred_build;
1183 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1184 if (wps->use_cred) {
1185 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1188 os_memset(&wps->cred, 0, sizeof(wps->cred));
1190 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1191 wps->cred.ssid_len = wps->wps->ssid_len;
1193 /* Select the best authentication and encryption type */
1194 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1195 wps->auth_type = WPS_AUTH_WPA2PSK;
1196 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1197 wps->auth_type = WPS_AUTH_WPAPSK;
1198 else if (wps->auth_type & WPS_AUTH_OPEN)
1199 wps->auth_type = WPS_AUTH_OPEN;
1200 else if (wps->auth_type & WPS_AUTH_SHARED)
1201 wps->auth_type = WPS_AUTH_SHARED;
1203 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1207 wps->cred.auth_type = wps->auth_type;
1209 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1210 wps->auth_type == WPS_AUTH_WPAPSK) {
1211 if (wps->encr_type & WPS_ENCR_AES)
1212 wps->encr_type = WPS_ENCR_AES;
1213 else if (wps->encr_type & WPS_ENCR_TKIP)
1214 wps->encr_type = WPS_ENCR_TKIP;
1216 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1217 "type for WPA/WPA2");
1221 if (wps->encr_type & WPS_ENCR_WEP)
1222 wps->encr_type = WPS_ENCR_WEP;
1223 else if (wps->encr_type & WPS_ENCR_NONE)
1224 wps->encr_type = WPS_ENCR_NONE;
1226 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1227 "type for non-WPA/WPA2 mode");
1231 wps->cred.encr_type = wps->encr_type;
1233 * Set MAC address in the Credential to be the Enrollee's MAC address
1235 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1237 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1238 !wps->wps->registrar->disable_auto_conf) {
1240 /* Generate a random passphrase */
1241 if (os_get_random(r, sizeof(r)) < 0)
1243 os_free(wps->new_psk);
1244 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1245 if (wps->new_psk == NULL)
1247 wps->new_psk_len--; /* remove newline */
1248 while (wps->new_psk_len &&
1249 wps->new_psk[wps->new_psk_len - 1] == '=')
1251 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1252 wps->new_psk, wps->new_psk_len);
1253 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1254 wps->cred.key_len = wps->new_psk_len;
1255 } else if (wps->wps->network_key) {
1256 os_memcpy(wps->cred.key, wps->wps->network_key,
1257 wps->wps->network_key_len);
1258 wps->cred.key_len = wps->wps->network_key_len;
1259 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1261 /* Generate a random per-device PSK */
1262 os_free(wps->new_psk);
1263 wps->new_psk_len = 32;
1264 wps->new_psk = os_malloc(wps->new_psk_len);
1265 if (wps->new_psk == NULL)
1267 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1268 os_free(wps->new_psk);
1269 wps->new_psk = NULL;
1272 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1273 wps->new_psk, wps->new_psk_len);
1274 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1276 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1277 wps->cred.key_len = wps->new_psk_len * 2;
1281 cred = wpabuf_alloc(200);
1285 if (wps_build_credential(cred, &wps->cred)) {
1290 wpabuf_put_be16(msg, ATTR_CRED);
1291 wpabuf_put_be16(msg, wpabuf_len(cred));
1292 wpabuf_put_buf(msg, cred);
1296 if (wps->wps->registrar->extra_cred) {
1297 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1298 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1305 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1307 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1309 if (wps_build_credential(msg, &wps->cred))
1316 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1320 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1322 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1323 wps->nonce_r, WPS_NONCE_LEN);
1324 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1326 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1327 msg = wpabuf_alloc(1000);
1331 if (wps_build_version(msg) ||
1332 wps_build_msg_type(msg, WPS_M2) ||
1333 wps_build_enrollee_nonce(wps, msg) ||
1334 wps_build_registrar_nonce(wps, msg) ||
1335 wps_build_uuid_r(wps, msg) ||
1336 wps_build_public_key(wps, msg) ||
1337 wps_derive_keys(wps) ||
1338 wps_build_auth_type_flags(wps, msg) ||
1339 wps_build_encr_type_flags(wps, msg) ||
1340 wps_build_conn_type_flags(wps, msg) ||
1341 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1342 wps_build_device_attrs(&wps->wps->dev, msg) ||
1343 wps_build_rf_bands(&wps->wps->dev, msg) ||
1344 wps_build_assoc_state(wps, msg) ||
1345 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1346 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1347 wps_build_os_version(&wps->wps->dev, msg) ||
1348 wps_build_authenticator(wps, msg)) {
1353 wps->state = RECV_M3;
1358 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1361 u16 err = wps->config_error;
1363 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1364 msg = wpabuf_alloc(1000);
1368 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1369 err == WPS_CFG_NO_ERROR)
1370 err = WPS_CFG_SETUP_LOCKED;
1372 if (wps_build_version(msg) ||
1373 wps_build_msg_type(msg, WPS_M2D) ||
1374 wps_build_enrollee_nonce(wps, msg) ||
1375 wps_build_registrar_nonce(wps, msg) ||
1376 wps_build_uuid_r(wps, msg) ||
1377 wps_build_auth_type_flags(wps, msg) ||
1378 wps_build_encr_type_flags(wps, msg) ||
1379 wps_build_conn_type_flags(wps, msg) ||
1380 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1381 wps_build_device_attrs(&wps->wps->dev, msg) ||
1382 wps_build_rf_bands(&wps->wps->dev, msg) ||
1383 wps_build_assoc_state(wps, msg) ||
1384 wps_build_config_error(msg, err) ||
1385 wps_build_os_version(&wps->wps->dev, msg)) {
1390 wps->state = RECV_M2D_ACK;
1395 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1397 struct wpabuf *msg, *plain;
1399 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1401 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1403 plain = wpabuf_alloc(200);
1407 msg = wpabuf_alloc(1000);
1413 if (wps_build_version(msg) ||
1414 wps_build_msg_type(msg, WPS_M4) ||
1415 wps_build_enrollee_nonce(wps, msg) ||
1416 wps_build_r_hash(wps, msg) ||
1417 wps_build_r_snonce1(wps, plain) ||
1418 wps_build_key_wrap_auth(wps, plain) ||
1419 wps_build_encr_settings(wps, msg, plain) ||
1420 wps_build_authenticator(wps, msg)) {
1427 wps->state = RECV_M5;
1432 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1434 struct wpabuf *msg, *plain;
1436 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1438 plain = wpabuf_alloc(200);
1442 msg = wpabuf_alloc(1000);
1448 if (wps_build_version(msg) ||
1449 wps_build_msg_type(msg, WPS_M6) ||
1450 wps_build_enrollee_nonce(wps, msg) ||
1451 wps_build_r_snonce2(wps, plain) ||
1452 wps_build_key_wrap_auth(wps, plain) ||
1453 wps_build_encr_settings(wps, msg, plain) ||
1454 wps_build_authenticator(wps, msg)) {
1461 wps->wps_pin_revealed = 1;
1462 wps->state = RECV_M7;
1467 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1469 struct wpabuf *msg, *plain;
1471 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1473 plain = wpabuf_alloc(500);
1477 msg = wpabuf_alloc(1000);
1483 if (wps_build_version(msg) ||
1484 wps_build_msg_type(msg, WPS_M8) ||
1485 wps_build_enrollee_nonce(wps, msg) ||
1486 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1487 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1488 wps_build_key_wrap_auth(wps, plain) ||
1489 wps_build_encr_settings(wps, msg, plain) ||
1490 wps_build_authenticator(wps, msg)) {
1497 wps->state = RECV_DONE;
1502 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1506 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1508 msg = wpabuf_alloc(1000);
1512 if (wps_build_version(msg) ||
1513 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1514 wps_build_enrollee_nonce(wps, msg) ||
1515 wps_build_registrar_nonce(wps, msg)) {
1524 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1528 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1530 msg = wpabuf_alloc(1000);
1534 if (wps_build_version(msg) ||
1535 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1536 wps_build_enrollee_nonce(wps, msg) ||
1537 wps_build_registrar_nonce(wps, msg) ||
1538 wps_build_config_error(msg, wps->config_error)) {
1547 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1548 enum wsc_op_code *op_code)
1552 #ifdef CONFIG_WPS_UPNP
1553 if (wps->wps->wps_upnp) {
1554 struct upnp_pending_message *p, *prev = NULL;
1555 if (wps->ext_reg > 1)
1556 wps_registrar_free_pending_m2(wps->wps);
1557 p = wps->wps->upnp_msgs;
1558 /* TODO: check pending message MAC address */
1559 while (p && p->next) {
1564 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1569 wps->wps->upnp_msgs = NULL;
1576 *op_code = WSC_NACK;
1583 if (wps->ext_reg == 0)
1589 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1590 "pending message available");
1593 #endif /* CONFIG_WPS_UPNP */
1595 switch (wps->state) {
1597 if (wps_get_dev_password(wps) < 0)
1598 msg = wps_build_m2d(wps);
1600 msg = wps_build_m2(wps);
1604 msg = wps_build_m2d(wps);
1608 msg = wps_build_m4(wps);
1612 msg = wps_build_m6(wps);
1616 msg = wps_build_m8(wps);
1620 msg = wps_build_wsc_ack(wps);
1624 msg = wps_build_wsc_nack(wps);
1625 *op_code = WSC_NACK;
1628 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1629 "a message", wps->state);
1634 if (*op_code == WSC_MSG && msg) {
1635 /* Save a copy of the last message for Authenticator derivation
1637 wpabuf_free(wps->last_msg);
1638 wps->last_msg = wpabuf_dup(msg);
1645 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1647 if (e_nonce == NULL) {
1648 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1652 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1653 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1654 wps->nonce_e, WPS_NONCE_LEN);
1660 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1662 if (r_nonce == NULL) {
1663 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1667 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1668 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1676 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1678 if (uuid_e == NULL) {
1679 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1683 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1684 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1690 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1692 if (pw_id == NULL) {
1693 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1697 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1698 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1704 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1706 if (e_hash1 == NULL) {
1707 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1711 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1712 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1718 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1720 if (e_hash2 == NULL) {
1721 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1725 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1726 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1732 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1734 u8 hash[SHA256_MAC_LEN];
1738 if (e_snonce1 == NULL) {
1739 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1743 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1744 WPS_SECRET_NONCE_LEN);
1746 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1747 addr[0] = e_snonce1;
1748 len[0] = WPS_SECRET_NONCE_LEN;
1749 addr[1] = wps->psk1;
1750 len[1] = WPS_PSK_LEN;
1751 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1752 len[2] = wpabuf_len(wps->dh_pubkey_e);
1753 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1754 len[3] = wpabuf_len(wps->dh_pubkey_r);
1755 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1757 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1758 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1759 "not match with the pre-committed value");
1760 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1761 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1765 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1766 "half of the device password");
1772 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1774 u8 hash[SHA256_MAC_LEN];
1778 if (e_snonce2 == NULL) {
1779 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1783 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1784 WPS_SECRET_NONCE_LEN);
1786 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1787 addr[0] = e_snonce2;
1788 len[0] = WPS_SECRET_NONCE_LEN;
1789 addr[1] = wps->psk2;
1790 len[1] = WPS_PSK_LEN;
1791 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1792 len[2] = wpabuf_len(wps->dh_pubkey_e);
1793 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1794 len[3] = wpabuf_len(wps->dh_pubkey_r);
1795 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1797 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1798 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1799 "not match with the pre-committed value");
1800 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1801 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1802 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1806 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1807 "half of the device password");
1808 wps->wps_pin_revealed = 0;
1809 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1815 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1817 if (mac_addr == NULL) {
1818 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1822 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1824 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1825 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1831 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1834 if (pk == NULL || pk_len == 0) {
1835 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1839 #ifdef CONFIG_WPS_OOB
1840 if (wps->wps->oob_conf.pubkey_hash != NULL) {
1842 u8 hash[WPS_HASH_LEN];
1845 sha256_vector(1, addr, &pk_len, hash);
1847 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
1848 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
1849 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
1853 #endif /* CONFIG_WPS_OOB */
1855 wpabuf_free(wps->dh_pubkey_e);
1856 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1857 if (wps->dh_pubkey_e == NULL)
1864 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1869 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1874 auth_types = WPA_GET_BE16(auth);
1876 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1878 wps->auth_type = wps->wps->auth_types & auth_types;
1879 if (wps->auth_type == 0) {
1880 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1881 "authentication types (own 0x%x Enrollee 0x%x)",
1882 wps->wps->auth_types, auth_types);
1883 #ifdef WPS_WORKAROUNDS
1885 * Some deployed implementations seem to advertise incorrect
1886 * information in this attribute. For example, Linksys WRT350N
1887 * seems to have a byteorder bug that breaks this negotiation.
1888 * In order to interoperate with existing implementations,
1889 * assume that the Enrollee supports everything we do.
1891 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1892 "does not advertise supported authentication types "
1894 wps->auth_type = wps->wps->auth_types;
1895 #else /* WPS_WORKAROUNDS */
1897 #endif /* WPS_WORKAROUNDS */
1904 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1909 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1914 encr_types = WPA_GET_BE16(encr);
1916 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1918 wps->encr_type = wps->wps->encr_types & encr_types;
1919 if (wps->encr_type == 0) {
1920 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1921 "encryption types (own 0x%x Enrollee 0x%x)",
1922 wps->wps->encr_types, encr_types);
1923 #ifdef WPS_WORKAROUNDS
1925 * Some deployed implementations seem to advertise incorrect
1926 * information in this attribute. For example, Linksys WRT350N
1927 * seems to have a byteorder bug that breaks this negotiation.
1928 * In order to interoperate with existing implementations,
1929 * assume that the Enrollee supports everything we do.
1931 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1932 "does not advertise supported encryption types "
1934 wps->encr_type = wps->wps->encr_types;
1935 #else /* WPS_WORKAROUNDS */
1937 #endif /* WPS_WORKAROUNDS */
1944 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1947 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1952 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1959 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1963 if (methods == NULL) {
1964 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1968 m = WPA_GET_BE16(methods);
1970 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1976 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1978 if (state == NULL) {
1979 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1984 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1991 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1995 if (assoc == NULL) {
1996 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2000 a = WPA_GET_BE16(assoc);
2001 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2007 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2012 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2016 e = WPA_GET_BE16(err);
2017 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2023 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2024 struct wps_parse_attr *attr)
2026 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2028 if (wps->state != RECV_M1) {
2029 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2030 "receiving M1", wps->state);
2034 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2035 wps_process_mac_addr(wps, attr->mac_addr) ||
2036 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2037 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2038 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2039 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2040 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2041 wps_process_config_methods(wps, attr->config_methods) ||
2042 wps_process_wps_state(wps, attr->wps_state) ||
2043 wps_process_device_attrs(&wps->peer_dev, attr) ||
2044 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2045 wps_process_assoc_state(wps, attr->assoc_state) ||
2046 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2047 wps_process_config_error(wps, attr->config_error) ||
2048 wps_process_os_version(&wps->peer_dev, attr->os_version))
2051 if (wps->dev_pw_id < 0x10 &&
2052 wps->dev_pw_id != DEV_PW_DEFAULT &&
2053 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2054 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2055 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2056 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2057 !wps->wps->registrar->pbc)) {
2058 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2060 wps->state = SEND_M2D;
2061 return WPS_CONTINUE;
2064 #ifdef CONFIG_WPS_OOB
2065 if (wps->dev_pw_id >= 0x10 &&
2066 wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
2067 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
2068 "%d mismatch", wps->dev_pw_id);
2069 wps->state = SEND_M2D;
2070 return WPS_CONTINUE;
2072 #endif /* CONFIG_WPS_OOB */
2074 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2075 if (wps->wps->registrar->force_pbc_overlap ||
2076 wps_registrar_pbc_overlap(wps->wps->registrar,
2077 wps->mac_addr_e, wps->uuid_e)) {
2078 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2080 wps->state = SEND_M2D;
2081 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2082 wps_pbc_overlap_event(wps->wps);
2083 wps->wps->registrar->force_pbc_overlap = 1;
2084 return WPS_CONTINUE;
2086 wps_registrar_add_pbc_session(wps->wps->registrar,
2087 wps->mac_addr_e, wps->uuid_e);
2091 wps->state = SEND_M2;
2092 return WPS_CONTINUE;
2096 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2097 const struct wpabuf *msg,
2098 struct wps_parse_attr *attr)
2100 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2102 if (wps->state != RECV_M3) {
2103 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2104 "receiving M3", wps->state);
2105 wps->state = SEND_WSC_NACK;
2106 return WPS_CONTINUE;
2109 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2110 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2112 wps->state = SEND_WSC_NACK;
2113 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2114 return WPS_CONTINUE;
2117 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2118 wps_process_authenticator(wps, attr->authenticator, msg) ||
2119 wps_process_e_hash1(wps, attr->e_hash1) ||
2120 wps_process_e_hash2(wps, attr->e_hash2)) {
2121 wps->state = SEND_WSC_NACK;
2122 return WPS_CONTINUE;
2125 wps->state = SEND_M4;
2126 return WPS_CONTINUE;
2130 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2131 const struct wpabuf *msg,
2132 struct wps_parse_attr *attr)
2134 struct wpabuf *decrypted;
2135 struct wps_parse_attr eattr;
2137 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2139 if (wps->state != RECV_M5) {
2140 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2141 "receiving M5", wps->state);
2142 wps->state = SEND_WSC_NACK;
2143 return WPS_CONTINUE;
2146 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2147 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2149 wps->state = SEND_WSC_NACK;
2150 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2151 return WPS_CONTINUE;
2154 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2155 wps_process_authenticator(wps, attr->authenticator, msg)) {
2156 wps->state = SEND_WSC_NACK;
2157 return WPS_CONTINUE;
2160 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2161 attr->encr_settings_len);
2162 if (decrypted == NULL) {
2163 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2164 "Settings attribute");
2165 wps->state = SEND_WSC_NACK;
2166 return WPS_CONTINUE;
2169 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2171 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2172 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2173 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2174 wpabuf_free(decrypted);
2175 wps->state = SEND_WSC_NACK;
2176 return WPS_CONTINUE;
2178 wpabuf_free(decrypted);
2180 wps->state = SEND_M6;
2181 return WPS_CONTINUE;
2185 static void wps_sta_cred_cb(struct wps_data *wps)
2188 * Update credential to only include a single authentication and
2189 * encryption type in case the AP configuration includes more than one
2192 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2193 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2194 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2195 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2196 if (wps->cred.encr_type & WPS_ENCR_AES)
2197 wps->cred.encr_type = WPS_ENCR_AES;
2198 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2199 wps->cred.encr_type = WPS_ENCR_TKIP;
2200 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2201 "AP configuration");
2202 if (wps->wps->cred_cb)
2203 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2207 static void wps_cred_update(struct wps_credential *dst,
2208 struct wps_credential *src)
2210 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2211 dst->ssid_len = src->ssid_len;
2212 dst->auth_type = src->auth_type;
2213 dst->encr_type = src->encr_type;
2214 dst->key_idx = src->key_idx;
2215 os_memcpy(dst->key, src->key, sizeof(dst->key));
2216 dst->key_len = src->key_len;
2220 static int wps_process_ap_settings_r(struct wps_data *wps,
2221 struct wps_parse_attr *attr)
2223 if (wps->wps->ap || wps->er)
2226 /* AP Settings Attributes in M7 when Enrollee is an AP */
2227 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2230 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2232 if (wps->new_ap_settings) {
2233 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2235 wps_cred_update(&wps->cred, wps->new_ap_settings);
2239 * Use the AP PIN only to receive the current AP settings, not
2240 * to reconfigure the AP.
2242 if (wps->ap_settings_cb) {
2243 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2247 wps_sta_cred_cb(wps);
2253 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2254 const struct wpabuf *msg,
2255 struct wps_parse_attr *attr)
2257 struct wpabuf *decrypted;
2258 struct wps_parse_attr eattr;
2260 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2262 if (wps->state != RECV_M7) {
2263 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2264 "receiving M7", wps->state);
2265 wps->state = SEND_WSC_NACK;
2266 return WPS_CONTINUE;
2269 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2270 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2272 wps->state = SEND_WSC_NACK;
2273 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2274 return WPS_CONTINUE;
2277 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2278 wps_process_authenticator(wps, attr->authenticator, msg)) {
2279 wps->state = SEND_WSC_NACK;
2280 return WPS_CONTINUE;
2283 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2284 attr->encr_settings_len);
2285 if (decrypted == NULL) {
2286 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2287 "Settings attribute");
2288 wps->state = SEND_WSC_NACK;
2289 return WPS_CONTINUE;
2292 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2294 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2295 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2296 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2297 wps_process_ap_settings_r(wps, &eattr)) {
2298 wpabuf_free(decrypted);
2299 wps->state = SEND_WSC_NACK;
2300 return WPS_CONTINUE;
2303 wpabuf_free(decrypted);
2305 wps->state = SEND_M8;
2306 return WPS_CONTINUE;
2310 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2311 const struct wpabuf *msg)
2313 struct wps_parse_attr attr;
2314 enum wps_process_res ret = WPS_CONTINUE;
2316 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2318 if (wps_parse_msg(msg, &attr) < 0)
2321 if (!wps_version_supported(attr.version)) {
2322 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2323 attr.version ? *attr.version : 0);
2327 if (attr.msg_type == NULL) {
2328 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2332 if (*attr.msg_type != WPS_M1 &&
2333 (attr.registrar_nonce == NULL ||
2334 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2335 WPS_NONCE_LEN != 0))) {
2336 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2340 switch (*attr.msg_type) {
2342 #ifdef CONFIG_WPS_UPNP
2343 if (wps->wps->wps_upnp && attr.mac_addr) {
2344 /* Remove old pending messages when starting new run */
2345 wps_free_pending_msgs(wps->wps->upnp_msgs);
2346 wps->wps->upnp_msgs = NULL;
2348 upnp_wps_device_send_wlan_event(
2349 wps->wps->wps_upnp, attr.mac_addr,
2350 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2352 #endif /* CONFIG_WPS_UPNP */
2353 ret = wps_process_m1(wps, &attr);
2356 ret = wps_process_m3(wps, msg, &attr);
2357 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2358 wps_fail_event(wps->wps, WPS_M3);
2361 ret = wps_process_m5(wps, msg, &attr);
2362 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2363 wps_fail_event(wps->wps, WPS_M5);
2366 ret = wps_process_m7(wps, msg, &attr);
2367 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2368 wps_fail_event(wps->wps, WPS_M7);
2371 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2376 if (ret == WPS_CONTINUE) {
2377 /* Save a copy of the last message for Authenticator derivation
2379 wpabuf_free(wps->last_msg);
2380 wps->last_msg = wpabuf_dup(msg);
2387 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2388 const struct wpabuf *msg)
2390 struct wps_parse_attr attr;
2392 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2394 if (wps_parse_msg(msg, &attr) < 0)
2397 if (!wps_version_supported(attr.version)) {
2398 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2399 attr.version ? *attr.version : 0);
2403 if (attr.msg_type == NULL) {
2404 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2408 if (*attr.msg_type != WPS_WSC_ACK) {
2409 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2414 #ifdef CONFIG_WPS_UPNP
2415 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2416 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2417 if (wps->wps->upnp_msgs)
2418 return WPS_CONTINUE;
2419 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2420 "external Registrar");
2423 #endif /* CONFIG_WPS_UPNP */
2425 if (attr.registrar_nonce == NULL ||
2426 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2428 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2432 if (attr.enrollee_nonce == NULL ||
2433 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2434 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2438 if (wps->state == RECV_M2D_ACK) {
2439 #ifdef CONFIG_WPS_UPNP
2440 if (wps->wps->wps_upnp &&
2441 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2442 if (wps->wps->upnp_msgs)
2443 return WPS_CONTINUE;
2444 if (wps->ext_reg == 0)
2446 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2447 "external Registrar");
2450 #endif /* CONFIG_WPS_UPNP */
2452 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2453 "terminate negotiation");
2460 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2461 const struct wpabuf *msg)
2463 struct wps_parse_attr attr;
2466 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2468 old_state = wps->state;
2469 wps->state = SEND_WSC_NACK;
2471 if (wps_parse_msg(msg, &attr) < 0)
2474 if (!wps_version_supported(attr.version)) {
2475 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2476 attr.version ? *attr.version : 0);
2480 if (attr.msg_type == NULL) {
2481 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2485 if (*attr.msg_type != WPS_WSC_NACK) {
2486 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2491 #ifdef CONFIG_WPS_UPNP
2492 if (wps->wps->wps_upnp && wps->ext_reg) {
2493 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2494 "Registrar terminated by the Enrollee");
2497 #endif /* CONFIG_WPS_UPNP */
2499 if (attr.registrar_nonce == NULL ||
2500 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2502 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2506 if (attr.enrollee_nonce == NULL ||
2507 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2508 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2512 if (attr.config_error == NULL) {
2513 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2518 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2519 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2521 switch (old_state) {
2523 wps_fail_event(wps->wps, WPS_M2);
2526 wps_fail_event(wps->wps, WPS_M4);
2529 wps_fail_event(wps->wps, WPS_M6);
2532 wps_fail_event(wps->wps, WPS_M8);
2542 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2543 const struct wpabuf *msg)
2545 struct wps_parse_attr attr;
2547 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2549 if (wps->state != RECV_DONE &&
2550 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2551 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2552 "receiving WSC_Done", wps->state);
2556 if (wps_parse_msg(msg, &attr) < 0)
2559 if (!wps_version_supported(attr.version)) {
2560 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2561 attr.version ? *attr.version : 0);
2565 if (attr.msg_type == NULL) {
2566 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2570 if (*attr.msg_type != WPS_WSC_DONE) {
2571 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2576 #ifdef CONFIG_WPS_UPNP
2577 if (wps->wps->wps_upnp && wps->ext_reg) {
2578 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2579 "Registrar completed successfully");
2580 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2584 #endif /* CONFIG_WPS_UPNP */
2586 if (attr.registrar_nonce == NULL ||
2587 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2589 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2593 if (attr.enrollee_nonce == NULL ||
2594 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2595 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2599 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2600 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2603 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2604 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2605 struct wps_credential cred;
2607 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2608 "on first Enrollee connection");
2610 os_memset(&cred, 0, sizeof(cred));
2611 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2612 cred.ssid_len = wps->wps->ssid_len;
2613 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2614 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2615 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2616 cred.key_len = wps->new_psk_len;
2618 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2619 wpa_hexdump_ascii_key(MSG_DEBUG,
2620 "WPS: Generated random passphrase",
2621 wps->new_psk, wps->new_psk_len);
2622 if (wps->wps->cred_cb)
2623 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2625 os_free(wps->new_psk);
2626 wps->new_psk = NULL;
2629 if (!wps->wps->ap && !wps->er)
2630 wps_sta_cred_cb(wps);
2633 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2634 wps->new_psk, wps->new_psk_len)) {
2635 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2638 os_free(wps->new_psk);
2639 wps->new_psk = NULL;
2642 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2645 wps_registrar_remove_pbc_session(wps->wps->registrar,
2646 wps->mac_addr_e, wps->uuid_e);
2647 wps_registrar_pbc_completed(wps->wps->registrar);
2649 wps_registrar_pin_completed(wps->wps->registrar);
2652 wps_success_event(wps->wps);
2658 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2659 enum wsc_op_code op_code,
2660 const struct wpabuf *msg)
2662 enum wps_process_res ret;
2664 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2666 (unsigned long) wpabuf_len(msg), op_code);
2668 #ifdef CONFIG_WPS_UPNP
2669 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2670 struct wps_parse_attr attr;
2671 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2672 *attr.msg_type == WPS_M3)
2673 wps->ext_reg = 2; /* past M2/M2D phase */
2675 if (wps->ext_reg > 1)
2676 wps_registrar_free_pending_m2(wps->wps);
2677 if (wps->wps->wps_upnp && wps->ext_reg &&
2678 wps->wps->upnp_msgs == NULL &&
2679 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
2681 struct wps_parse_attr attr;
2683 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2686 type = *attr.msg_type;
2687 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2688 " to external Registrar for processing", type);
2689 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2691 UPNP_WPS_WLANEVENT_TYPE_EAP,
2693 if (op_code == WSC_MSG)
2695 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2696 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2697 "external Registrar");
2698 return WPS_CONTINUE;
2700 #endif /* CONFIG_WPS_UPNP */
2704 return wps_process_wsc_msg(wps, msg);
2706 return wps_process_wsc_ack(wps, msg);
2708 return wps_process_wsc_nack(wps, msg);
2710 ret = wps_process_wsc_done(wps, msg);
2711 if (ret == WPS_FAILURE) {
2712 wps->state = SEND_WSC_NACK;
2713 wps_fail_event(wps->wps, WPS_WSC_DONE);
2717 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2723 int wps_registrar_update_ie(struct wps_registrar *reg)
2725 return wps_set_ie(reg);
2729 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2732 struct wps_registrar *reg = eloop_ctx;
2734 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar timed out - "
2735 "unselect Registrar");
2736 reg->selected_registrar = 0;
2738 reg->sel_reg_dev_password_id_override = -1;
2739 reg->sel_reg_config_methods_override = -1;
2741 wps_cb_set_sel_reg(reg);
2746 * wps_registrar_set_selected_registrar - Notification of SetSelectedRegistrar
2747 * @reg: Registrar data from wps_registrar_init()
2748 * @msg: Received message from SetSelectedRegistrar
2749 * Returns: 0 on success, -1 on failure
2751 * This function is called when an AP receives a SetSelectedRegistrar UPnP
2754 int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
2755 const struct wpabuf *msg)
2757 struct wps_parse_attr attr;
2759 wpa_hexdump_buf(MSG_MSGDUMP, "WPS: SetSelectedRegistrar attributes",
2762 if (wps_parse_msg(msg, &attr) < 0)
2764 if (!wps_version_supported(attr.version)) {
2765 wpa_printf(MSG_DEBUG, "WPS: Unsupported SetSelectedRegistrar "
2766 "version 0x%x", attr.version ? *attr.version : 0);
2770 if (attr.selected_registrar == NULL ||
2771 *attr.selected_registrar == 0) {
2772 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar: Disable "
2773 "Selected Registrar");
2774 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg,
2776 wps_registrar_set_selected_timeout(reg, NULL);
2780 reg->selected_registrar = 1;
2781 reg->sel_reg_dev_password_id_override = attr.dev_password_id ?
2782 WPA_GET_BE16(attr.dev_password_id) : DEV_PW_DEFAULT;
2783 reg->sel_reg_config_methods_override = attr.sel_reg_config_methods ?
2784 WPA_GET_BE16(attr.sel_reg_config_methods) : -1;
2787 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
2788 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
2789 wps_registrar_set_selected_timeout,
2795 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
2796 char *buf, size_t buflen)
2798 struct wps_registrar_device *d;
2801 char devtype[WPS_DEV_TYPE_BUFSIZE];
2803 d = wps_device_get(reg, addr);
2806 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
2809 ret = os_snprintf(buf + len, buflen - len,
2811 "wpsPrimaryDeviceType=%s\n"
2812 "wpsDeviceName=%s\n"
2813 "wpsManufacturer=%s\n"
2815 "wpsModelNumber=%s\n"
2816 "wpsSerialNumber=%s\n",
2818 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
2820 d->dev.device_name ? d->dev.device_name : "",
2821 d->dev.manufacturer ? d->dev.manufacturer : "",
2822 d->dev.model_name ? d->dev.model_name : "",
2823 d->dev.model_number ? d->dev.model_number : "",
2824 d->dev.serial_number ? d->dev.serial_number : "");
2825 if (ret < 0 || (size_t) ret >= buflen - len)