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 "ieee802_11_defs.h"
23 #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 dst->categ = src->categ;
163 dst->sub_categ = src->sub_categ;
165 #define WPS_STRDUP(n) \
167 dst->n = src->n ? os_strdup(src->n) : NULL
169 WPS_STRDUP(device_name);
170 WPS_STRDUP(manufacturer);
171 WPS_STRDUP(model_name);
172 WPS_STRDUP(model_number);
173 WPS_STRDUP(serial_number);
178 int wps_device_store(struct wps_registrar *reg,
179 struct wps_device_data *dev, const u8 *uuid)
181 struct wps_registrar_device *d;
183 d = wps_device_get(reg, dev->mac_addr);
185 d = os_zalloc(sizeof(*d));
188 d->next = reg->devices;
192 wps_device_clone_data(&d->dev, dev);
193 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
199 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
200 const u8 *addr, const u8 *uuid_e)
202 struct wps_pbc_session *pbc, *prev = NULL;
207 pbc = reg->pbc_sessions;
209 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
210 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
212 prev->next = pbc->next;
214 reg->pbc_sessions = pbc->next;
222 pbc = os_zalloc(sizeof(*pbc));
225 os_memcpy(pbc->addr, addr, ETH_ALEN);
227 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
230 pbc->next = reg->pbc_sessions;
231 reg->pbc_sessions = pbc;
232 pbc->timestamp = now;
234 /* remove entries that have timed out */
239 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
241 wps_free_pbc_sessions(pbc);
250 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
251 const u8 *addr, const u8 *uuid_e)
253 struct wps_pbc_session *pbc, *prev = NULL;
255 pbc = reg->pbc_sessions;
257 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
258 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
260 prev->next = pbc->next;
262 reg->pbc_sessions = pbc->next;
272 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
273 const u8 *addr, const u8 *uuid_e)
276 struct wps_pbc_session *pbc;
281 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
282 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
284 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
286 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
293 return count > 1 ? 1 : 0;
297 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
299 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
301 wpabuf_put_be16(msg, ATTR_WPS_STATE);
302 wpabuf_put_be16(msg, 1);
303 wpabuf_put_u8(msg, wps->wps_state);
308 #ifdef CONFIG_WPS_UPNP
309 static void wps_registrar_free_pending_m2(struct wps_context *wps)
311 struct upnp_pending_message *p, *p2, *prev = NULL;
314 if (p->type == WPS_M2 || p->type == WPS_M2D) {
316 wps->upnp_msgs = p->next;
318 prev->next = p->next;
319 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
322 wpabuf_free(p2->msg);
330 #endif /* CONFIG_WPS_UPNP */
333 static int wps_build_ap_setup_locked(struct wps_context *wps,
336 if (wps->ap_setup_locked) {
337 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
338 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
339 wpabuf_put_be16(msg, 1);
340 wpabuf_put_u8(msg, 1);
346 static int wps_build_selected_registrar(struct wps_registrar *reg,
349 if (!reg->selected_registrar)
351 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
352 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
353 wpabuf_put_be16(msg, 1);
354 wpabuf_put_u8(msg, 1);
359 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
362 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
363 if (!reg->selected_registrar)
365 if (reg->sel_reg_dev_password_id_override >= 0)
366 id = reg->sel_reg_dev_password_id_override;
367 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
368 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
369 wpabuf_put_be16(msg, 2);
370 wpabuf_put_be16(msg, id);
375 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
379 if (!reg->selected_registrar)
381 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
383 methods |= WPS_CONFIG_PUSHBUTTON;
384 if (reg->sel_reg_config_methods_override >= 0)
385 methods = reg->sel_reg_config_methods_override;
386 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
388 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
389 wpabuf_put_be16(msg, 2);
390 wpabuf_put_be16(msg, methods);
395 static int wps_build_probe_config_methods(struct wps_registrar *reg,
400 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
401 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
402 wpabuf_put_be16(msg, 2);
403 wpabuf_put_be16(msg, methods);
408 static int wps_build_config_methods_r(struct wps_registrar *reg,
412 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
414 methods |= WPS_CONFIG_PUSHBUTTON;
415 return wps_build_config_methods(msg, methods);
419 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
421 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
422 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
423 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
424 wpabuf_put_be16(msg, 1);
425 wpabuf_put_u8(msg, resp);
431 * wps_registrar_init - Initialize WPS Registrar data
432 * @wps: Pointer to longterm WPS context
433 * @cfg: Registrar configuration
434 * Returns: Pointer to allocated Registrar data or %NULL on failure
436 * This function is used to initialize WPS Registrar functionality. It can be
437 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
438 * runs (e.g., when run as an internal Registrar in an AP). Caller is
439 * responsible for freeing the returned data with wps_registrar_deinit() when
440 * Registrar functionality is not needed anymore.
442 struct wps_registrar *
443 wps_registrar_init(struct wps_context *wps,
444 const struct wps_registrar_config *cfg)
446 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
451 reg->new_psk_cb = cfg->new_psk_cb;
452 reg->set_ie_cb = cfg->set_ie_cb;
453 reg->pin_needed_cb = cfg->pin_needed_cb;
454 reg->reg_success_cb = cfg->reg_success_cb;
455 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
456 reg->cb_ctx = cfg->cb_ctx;
457 reg->skip_cred_build = cfg->skip_cred_build;
458 if (cfg->extra_cred) {
459 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
460 cfg->extra_cred_len);
461 if (reg->extra_cred == NULL) {
466 reg->disable_auto_conf = cfg->disable_auto_conf;
467 reg->sel_reg_dev_password_id_override = -1;
468 reg->sel_reg_config_methods_override = -1;
469 reg->static_wep_only = cfg->static_wep_only;
471 if (wps_set_ie(reg)) {
472 wps_registrar_deinit(reg);
481 * wps_registrar_deinit - Deinitialize WPS Registrar data
482 * @reg: Registrar data from wps_registrar_init()
484 void wps_registrar_deinit(struct wps_registrar *reg)
488 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
489 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
490 wps_free_pins(reg->pins);
491 wps_free_pbc_sessions(reg->pbc_sessions);
492 wpabuf_free(reg->extra_cred);
493 wps_free_devices(reg->devices);
499 * wps_registrar_add_pin - Configure a new PIN for Registrar
500 * @reg: Registrar data from wps_registrar_init()
501 * @uuid: UUID-E or %NULL for wildcard (any UUID)
502 * @pin: PIN (Device Password)
503 * @pin_len: Length of pin in octets
504 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
505 * Returns: 0 on success, -1 on failure
507 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
508 const u8 *pin, size_t pin_len, int timeout)
510 struct wps_uuid_pin *p;
512 p = os_zalloc(sizeof(*p));
516 p->wildcard_uuid = 1;
518 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
519 p->pin = os_malloc(pin_len);
520 if (p->pin == NULL) {
524 os_memcpy(p->pin, pin, pin_len);
525 p->pin_len = pin_len;
528 p->flags |= PIN_EXPIRES;
529 os_get_time(&p->expiration);
530 p->expiration.sec += timeout;
536 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
538 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
539 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
540 reg->selected_registrar = 1;
543 wps_cb_set_sel_reg(reg);
544 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
545 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
546 wps_registrar_set_selected_timeout,
553 static void wps_registrar_expire_pins(struct wps_registrar *reg)
555 struct wps_uuid_pin *pin, *prev, *del;
562 if ((pin->flags & PIN_EXPIRES) &&
563 os_time_before(&pin->expiration, &now)) {
565 reg->pins = pin->next;
567 prev->next = pin->next;
570 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
571 del->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;
594 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
596 reg->pins = pin->next;
598 prev->next = pin->next;
599 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
600 pin->uuid, WPS_UUID_LEN);
612 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
613 const u8 *uuid, size_t *pin_len)
615 struct wps_uuid_pin *pin;
617 wps_registrar_expire_pins(reg);
621 if (!pin->wildcard_uuid &&
622 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
628 /* Check for wildcard UUIDs since none of the UUID-specific
632 if (pin->wildcard_uuid == 1) {
633 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
634 "PIN. Assigned it for this UUID-E");
635 pin->wildcard_uuid = 2;
636 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
647 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
648 * that could otherwise avoid PIN invalidations.
650 if (pin->flags & PIN_LOCKED) {
651 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
652 "allow concurrent re-use");
655 *pin_len = pin->pin_len;
656 pin->flags |= PIN_LOCKED;
662 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
663 * @reg: Registrar data from wps_registrar_init()
665 * Returns: 0 on success, -1 on failure
667 * PINs are locked to enforce only one concurrent use. This function unlocks a
668 * PIN to allow it to be used again. If the specified PIN was configured using
669 * a wildcard UUID, it will be removed instead of allowing multiple uses.
671 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
673 struct wps_uuid_pin *pin;
677 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
678 if (pin->wildcard_uuid == 2) {
679 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
681 return wps_registrar_invalidate_pin(reg, uuid);
683 pin->flags &= ~PIN_LOCKED;
693 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
695 reg->selected_registrar = 0;
698 wps_cb_set_sel_reg(reg);
702 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
704 struct wps_registrar *reg = eloop_ctx;
706 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
707 wps_pbc_timeout_event(reg->wps);
708 wps_registrar_stop_pbc(reg);
713 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
714 * @reg: Registrar data from wps_registrar_init()
715 * Returns: 0 on success, -1 on failure
717 * This function is called on an AP when a push button is pushed to activate
718 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
719 * or when a PBC registration is completed.
721 int wps_registrar_button_pushed(struct wps_registrar *reg)
723 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
724 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
726 wps_pbc_overlap_event(reg->wps);
729 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
730 reg->force_pbc_overlap = 0;
731 reg->selected_registrar = 1;
734 wps_cb_set_sel_reg(reg);
736 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
737 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
743 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
745 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
746 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
747 wps_registrar_stop_pbc(reg);
750 static void wps_registrar_pin_completed(struct wps_registrar *reg)
752 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
753 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
754 reg->selected_registrar = 0;
756 wps_cb_set_sel_reg(reg);
761 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
762 * @reg: Registrar data from wps_registrar_init()
763 * @addr: MAC address of the Probe Request sender
764 * @wps_data: WPS IE contents
766 * This function is called on an AP when a Probe Request with WPS IE is
767 * received. This is used to track PBC mode use and to detect possible overlap
768 * situation with other WPS APs.
770 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
771 const struct wpabuf *wps_data)
773 struct wps_parse_attr attr;
776 wpa_hexdump_buf(MSG_MSGDUMP,
777 "WPS: Probe Request with WPS data received",
780 if (wps_parse_msg(wps_data, &attr) < 0)
782 if (!wps_version_supported(attr.version)) {
783 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
784 "version 0x%x", attr.version ? *attr.version : 0);
788 if (attr.config_methods == NULL) {
789 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
794 methods = WPA_GET_BE16(attr.config_methods);
795 if (!(methods & WPS_CONFIG_PUSHBUTTON))
796 return; /* Not PBC */
798 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
799 MACSTR, MAC2STR(addr));
801 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
802 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
803 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
804 reg->force_pbc_overlap = 1;
805 wps_pbc_overlap_event(reg->wps);
810 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
811 const u8 *psk, size_t psk_len)
813 if (reg->new_psk_cb == NULL)
816 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
820 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
821 const struct wps_device_data *dev)
823 if (reg->pin_needed_cb == NULL)
826 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
830 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
833 if (reg->reg_success_cb == NULL)
836 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
840 static int wps_cb_set_ie(struct wps_registrar *reg,
841 const struct wpabuf *beacon_ie,
842 const struct wpabuf *probe_resp_ie)
844 if (reg->set_ie_cb == NULL)
847 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
848 wpabuf_len(beacon_ie),
849 wpabuf_head(probe_resp_ie),
850 wpabuf_len(probe_resp_ie));
854 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
857 if (reg->set_sel_reg_cb == NULL)
860 if (reg->selected_registrar) {
861 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
863 methods |= WPS_CONFIG_PUSHBUTTON;
866 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
867 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
872 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
873 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
878 ie = wpabuf_alloc(wpabuf_len(data) + 100);
884 pos = wpabuf_head(data);
885 end = pos + wpabuf_len(data);
888 size_t frag_len = end - pos;
891 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
892 wpabuf_put_u8(ie, 4 + frag_len);
893 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
894 wpabuf_put_data(ie, pos, frag_len);
904 static int wps_set_ie(struct wps_registrar *reg)
906 struct wpabuf *beacon;
907 struct wpabuf *probe;
910 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
912 beacon = wpabuf_alloc(300);
915 probe = wpabuf_alloc(400);
921 if (wps_build_version(beacon) ||
922 wps_build_wps_state(reg->wps, beacon) ||
923 wps_build_ap_setup_locked(reg->wps, beacon) ||
924 wps_build_selected_registrar(reg, beacon) ||
925 wps_build_sel_reg_dev_password_id(reg, beacon) ||
926 wps_build_sel_reg_config_methods(reg, beacon) ||
927 wps_build_version(probe) ||
928 wps_build_wps_state(reg->wps, probe) ||
929 wps_build_ap_setup_locked(reg->wps, probe) ||
930 wps_build_selected_registrar(reg, probe) ||
931 wps_build_sel_reg_dev_password_id(reg, probe) ||
932 wps_build_sel_reg_config_methods(reg, probe) ||
933 wps_build_resp_type(reg, probe) ||
934 wps_build_uuid_e(probe, reg->wps->uuid) ||
935 wps_build_device_attrs(®->wps->dev, probe) ||
936 wps_build_probe_config_methods(reg, probe) ||
937 wps_build_rf_bands(®->wps->dev, probe)) {
943 beacon = wps_ie_encapsulate(beacon);
944 probe = wps_ie_encapsulate(probe);
946 if (!beacon || !probe) {
952 if (reg->static_wep_only) {
954 * Windows XP and Vista clients can get confused about
955 * EAP-Identity/Request when they probe the network with
956 * EAPOL-Start. In such a case, they may assume the network is
957 * using IEEE 802.1X and prompt user for a certificate while
958 * the correct (non-WPS) behavior would be to ask for the
959 * static WEP key. As a workaround, use Microsoft Provisioning
960 * IE to advertise that legacy 802.1X is not supported.
962 const u8 ms_wps[7] = {
963 WLAN_EID_VENDOR_SPECIFIC, 5,
964 /* Microsoft Provisioning IE (00:50:f2:5) */
966 0x00 /* no legacy 802.1X or MS WPS */
968 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
969 "into Beacon/Probe Response frames");
970 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
971 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
974 ret = wps_cb_set_ie(reg, beacon, probe);
982 static int wps_get_dev_password(struct wps_data *wps)
987 os_free(wps->dev_password);
988 wps->dev_password = NULL;
991 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
992 pin = (const u8 *) "00000000";
995 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
999 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1001 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1006 wps->dev_password = os_malloc(pin_len);
1007 if (wps->dev_password == NULL)
1009 os_memcpy(wps->dev_password, pin, pin_len);
1010 wps->dev_password_len = pin_len;
1016 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1018 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1019 wpabuf_put_be16(msg, ATTR_UUID_R);
1020 wpabuf_put_be16(msg, WPS_UUID_LEN);
1021 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1026 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1032 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1034 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1035 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1036 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1038 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1039 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1040 "R-Hash derivation");
1044 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1045 wpabuf_put_be16(msg, ATTR_R_HASH1);
1046 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1047 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1048 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1049 addr[0] = wps->snonce;
1050 len[0] = WPS_SECRET_NONCE_LEN;
1051 addr[1] = wps->psk1;
1052 len[1] = WPS_PSK_LEN;
1053 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1054 len[2] = wpabuf_len(wps->dh_pubkey_e);
1055 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1056 len[3] = wpabuf_len(wps->dh_pubkey_r);
1057 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1058 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1060 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1061 wpabuf_put_be16(msg, ATTR_R_HASH2);
1062 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1063 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1064 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1065 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1066 addr[1] = wps->psk2;
1067 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1068 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1074 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1076 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1077 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1078 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1079 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1084 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1086 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1087 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1088 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1089 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1090 WPS_SECRET_NONCE_LEN);
1095 static int wps_build_cred_network_idx(struct wpabuf *msg,
1096 struct wps_credential *cred)
1098 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
1099 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1100 wpabuf_put_be16(msg, 1);
1101 wpabuf_put_u8(msg, 1);
1106 static int wps_build_cred_ssid(struct wpabuf *msg,
1107 struct wps_credential *cred)
1109 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1110 wpabuf_put_be16(msg, ATTR_SSID);
1111 wpabuf_put_be16(msg, cred->ssid_len);
1112 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1117 static int wps_build_cred_auth_type(struct wpabuf *msg,
1118 struct wps_credential *cred)
1120 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1122 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1123 wpabuf_put_be16(msg, 2);
1124 wpabuf_put_be16(msg, cred->auth_type);
1129 static int wps_build_cred_encr_type(struct wpabuf *msg,
1130 struct wps_credential *cred)
1132 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1134 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1135 wpabuf_put_be16(msg, 2);
1136 wpabuf_put_be16(msg, cred->encr_type);
1141 static int wps_build_cred_network_key(struct wpabuf *msg,
1142 struct wps_credential *cred)
1144 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
1145 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1146 wpabuf_put_be16(msg, cred->key_len);
1147 wpabuf_put_data(msg, cred->key, cred->key_len);
1152 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1153 struct wps_credential *cred)
1155 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1156 MAC2STR(cred->mac_addr));
1157 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1158 wpabuf_put_be16(msg, ETH_ALEN);
1159 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1164 static int wps_build_credential(struct wpabuf *msg,
1165 struct wps_credential *cred)
1167 if (wps_build_cred_network_idx(msg, cred) ||
1168 wps_build_cred_ssid(msg, cred) ||
1169 wps_build_cred_auth_type(msg, cred) ||
1170 wps_build_cred_encr_type(msg, cred) ||
1171 wps_build_cred_network_key(msg, cred) ||
1172 wps_build_cred_mac_addr(msg, cred))
1178 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1180 struct wpabuf *cred;
1182 if (wps->wps->registrar->skip_cred_build)
1183 goto skip_cred_build;
1185 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1186 os_memset(&wps->cred, 0, sizeof(wps->cred));
1188 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1189 wps->cred.ssid_len = wps->wps->ssid_len;
1191 /* Select the best authentication and encryption type */
1192 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1193 wps->auth_type = WPS_AUTH_WPA2PSK;
1194 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1195 wps->auth_type = WPS_AUTH_WPAPSK;
1196 else if (wps->auth_type & WPS_AUTH_OPEN)
1197 wps->auth_type = WPS_AUTH_OPEN;
1198 else if (wps->auth_type & WPS_AUTH_SHARED)
1199 wps->auth_type = WPS_AUTH_SHARED;
1201 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1205 wps->cred.auth_type = wps->auth_type;
1207 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1208 wps->auth_type == WPS_AUTH_WPAPSK) {
1209 if (wps->encr_type & WPS_ENCR_AES)
1210 wps->encr_type = WPS_ENCR_AES;
1211 else if (wps->encr_type & WPS_ENCR_TKIP)
1212 wps->encr_type = WPS_ENCR_TKIP;
1214 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1215 "type for WPA/WPA2");
1219 if (wps->encr_type & WPS_ENCR_WEP)
1220 wps->encr_type = WPS_ENCR_WEP;
1221 else if (wps->encr_type & WPS_ENCR_NONE)
1222 wps->encr_type = WPS_ENCR_NONE;
1224 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1225 "type for non-WPA/WPA2 mode");
1229 wps->cred.encr_type = wps->encr_type;
1230 /* Set MAC address in the Credential to be the AP's address (BSSID) */
1231 os_memcpy(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN);
1233 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1234 !wps->wps->registrar->disable_auto_conf) {
1236 /* Generate a random passphrase */
1237 if (os_get_random(r, sizeof(r)) < 0)
1239 os_free(wps->new_psk);
1240 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1241 if (wps->new_psk == NULL)
1243 wps->new_psk_len--; /* remove newline */
1244 while (wps->new_psk_len &&
1245 wps->new_psk[wps->new_psk_len - 1] == '=')
1247 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1248 wps->new_psk, wps->new_psk_len);
1249 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1250 wps->cred.key_len = wps->new_psk_len;
1251 } else if (wps->wps->network_key) {
1252 os_memcpy(wps->cred.key, wps->wps->network_key,
1253 wps->wps->network_key_len);
1254 wps->cred.key_len = wps->wps->network_key_len;
1255 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1257 /* Generate a random per-device PSK */
1258 os_free(wps->new_psk);
1259 wps->new_psk_len = 32;
1260 wps->new_psk = os_malloc(wps->new_psk_len);
1261 if (wps->new_psk == NULL)
1263 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1264 os_free(wps->new_psk);
1265 wps->new_psk = NULL;
1268 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1269 wps->new_psk, wps->new_psk_len);
1270 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1272 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1273 wps->cred.key_len = wps->new_psk_len * 2;
1276 cred = wpabuf_alloc(200);
1280 if (wps_build_credential(cred, &wps->cred)) {
1285 wpabuf_put_be16(msg, ATTR_CRED);
1286 wpabuf_put_be16(msg, wpabuf_len(cred));
1287 wpabuf_put_buf(msg, cred);
1291 if (wps->wps->registrar->extra_cred) {
1292 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1293 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1300 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1302 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1304 if (wps_build_credential(msg, &wps->cred))
1311 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1315 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1317 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1318 wps->nonce_r, WPS_NONCE_LEN);
1319 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1321 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1322 msg = wpabuf_alloc(1000);
1326 if (wps_build_version(msg) ||
1327 wps_build_msg_type(msg, WPS_M2) ||
1328 wps_build_enrollee_nonce(wps, msg) ||
1329 wps_build_registrar_nonce(wps, msg) ||
1330 wps_build_uuid_r(wps, msg) ||
1331 wps_build_public_key(wps, msg) ||
1332 wps_derive_keys(wps) ||
1333 wps_build_auth_type_flags(wps, msg) ||
1334 wps_build_encr_type_flags(wps, msg) ||
1335 wps_build_conn_type_flags(wps, msg) ||
1336 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1337 wps_build_device_attrs(&wps->wps->dev, msg) ||
1338 wps_build_rf_bands(&wps->wps->dev, msg) ||
1339 wps_build_assoc_state(wps, msg) ||
1340 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1341 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1342 wps_build_os_version(&wps->wps->dev, msg) ||
1343 wps_build_authenticator(wps, msg)) {
1348 wps->state = RECV_M3;
1353 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1356 u16 err = wps->config_error;
1358 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1359 msg = wpabuf_alloc(1000);
1363 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1364 err == WPS_CFG_NO_ERROR)
1365 err = WPS_CFG_SETUP_LOCKED;
1367 if (wps_build_version(msg) ||
1368 wps_build_msg_type(msg, WPS_M2D) ||
1369 wps_build_enrollee_nonce(wps, msg) ||
1370 wps_build_registrar_nonce(wps, msg) ||
1371 wps_build_uuid_r(wps, msg) ||
1372 wps_build_auth_type_flags(wps, msg) ||
1373 wps_build_encr_type_flags(wps, msg) ||
1374 wps_build_conn_type_flags(wps, msg) ||
1375 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1376 wps_build_device_attrs(&wps->wps->dev, msg) ||
1377 wps_build_rf_bands(&wps->wps->dev, msg) ||
1378 wps_build_assoc_state(wps, msg) ||
1379 wps_build_config_error(msg, err) ||
1380 wps_build_os_version(&wps->wps->dev, msg)) {
1385 wps->state = RECV_M2D_ACK;
1390 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1392 struct wpabuf *msg, *plain;
1394 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1396 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1398 plain = wpabuf_alloc(200);
1402 msg = wpabuf_alloc(1000);
1408 if (wps_build_version(msg) ||
1409 wps_build_msg_type(msg, WPS_M4) ||
1410 wps_build_enrollee_nonce(wps, msg) ||
1411 wps_build_r_hash(wps, msg) ||
1412 wps_build_r_snonce1(wps, plain) ||
1413 wps_build_key_wrap_auth(wps, plain) ||
1414 wps_build_encr_settings(wps, msg, plain) ||
1415 wps_build_authenticator(wps, msg)) {
1422 wps->state = RECV_M5;
1427 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1429 struct wpabuf *msg, *plain;
1431 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1433 plain = wpabuf_alloc(200);
1437 msg = wpabuf_alloc(1000);
1443 if (wps_build_version(msg) ||
1444 wps_build_msg_type(msg, WPS_M6) ||
1445 wps_build_enrollee_nonce(wps, msg) ||
1446 wps_build_r_snonce2(wps, plain) ||
1447 wps_build_key_wrap_auth(wps, plain) ||
1448 wps_build_encr_settings(wps, msg, plain) ||
1449 wps_build_authenticator(wps, msg)) {
1456 wps->wps_pin_revealed = 1;
1457 wps->state = RECV_M7;
1462 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1464 struct wpabuf *msg, *plain;
1466 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1468 plain = wpabuf_alloc(500);
1472 msg = wpabuf_alloc(1000);
1478 if (wps_build_version(msg) ||
1479 wps_build_msg_type(msg, WPS_M8) ||
1480 wps_build_enrollee_nonce(wps, msg) ||
1481 (wps->wps->ap && wps_build_cred(wps, plain)) ||
1482 (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1483 wps_build_key_wrap_auth(wps, plain) ||
1484 wps_build_encr_settings(wps, msg, plain) ||
1485 wps_build_authenticator(wps, msg)) {
1492 wps->state = RECV_DONE;
1497 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1501 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1503 msg = wpabuf_alloc(1000);
1507 if (wps_build_version(msg) ||
1508 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1509 wps_build_enrollee_nonce(wps, msg) ||
1510 wps_build_registrar_nonce(wps, msg)) {
1519 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1523 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1525 msg = wpabuf_alloc(1000);
1529 if (wps_build_version(msg) ||
1530 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1531 wps_build_enrollee_nonce(wps, msg) ||
1532 wps_build_registrar_nonce(wps, msg) ||
1533 wps_build_config_error(msg, wps->config_error)) {
1542 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1543 enum wsc_op_code *op_code)
1547 #ifdef CONFIG_WPS_UPNP
1548 if (wps->wps->wps_upnp) {
1549 struct upnp_pending_message *p, *prev = NULL;
1550 if (wps->ext_reg > 1)
1551 wps_registrar_free_pending_m2(wps->wps);
1552 p = wps->wps->upnp_msgs;
1553 /* TODO: check pending message MAC address */
1554 while (p && p->next) {
1559 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1564 wps->wps->upnp_msgs = NULL;
1571 *op_code = WSC_NACK;
1578 if (wps->ext_reg == 0)
1584 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1585 "pending message available");
1588 #endif /* CONFIG_WPS_UPNP */
1590 switch (wps->state) {
1592 if (wps_get_dev_password(wps) < 0)
1593 msg = wps_build_m2d(wps);
1595 msg = wps_build_m2(wps);
1599 msg = wps_build_m2d(wps);
1603 msg = wps_build_m4(wps);
1607 msg = wps_build_m6(wps);
1611 msg = wps_build_m8(wps);
1615 msg = wps_build_wsc_ack(wps);
1619 msg = wps_build_wsc_nack(wps);
1620 *op_code = WSC_NACK;
1623 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1624 "a message", wps->state);
1629 if (*op_code == WSC_MSG && msg) {
1630 /* Save a copy of the last message for Authenticator derivation
1632 wpabuf_free(wps->last_msg);
1633 wps->last_msg = wpabuf_dup(msg);
1640 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1642 if (e_nonce == NULL) {
1643 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1647 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1648 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1649 wps->nonce_e, WPS_NONCE_LEN);
1655 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1657 if (r_nonce == NULL) {
1658 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1662 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1663 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1671 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1673 if (uuid_e == NULL) {
1674 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1678 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1679 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1685 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1687 if (pw_id == NULL) {
1688 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1692 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1693 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1699 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1701 if (e_hash1 == NULL) {
1702 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1706 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1707 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1713 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1715 if (e_hash2 == NULL) {
1716 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1720 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1721 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1727 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1729 u8 hash[SHA256_MAC_LEN];
1733 if (e_snonce1 == NULL) {
1734 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1738 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1739 WPS_SECRET_NONCE_LEN);
1741 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1742 addr[0] = e_snonce1;
1743 len[0] = WPS_SECRET_NONCE_LEN;
1744 addr[1] = wps->psk1;
1745 len[1] = WPS_PSK_LEN;
1746 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1747 len[2] = wpabuf_len(wps->dh_pubkey_e);
1748 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1749 len[3] = wpabuf_len(wps->dh_pubkey_r);
1750 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1752 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1753 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1754 "not match with the pre-committed value");
1755 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1756 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1760 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1761 "half of the device password");
1767 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1769 u8 hash[SHA256_MAC_LEN];
1773 if (e_snonce2 == NULL) {
1774 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1778 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1779 WPS_SECRET_NONCE_LEN);
1781 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1782 addr[0] = e_snonce2;
1783 len[0] = WPS_SECRET_NONCE_LEN;
1784 addr[1] = wps->psk2;
1785 len[1] = WPS_PSK_LEN;
1786 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1787 len[2] = wpabuf_len(wps->dh_pubkey_e);
1788 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1789 len[3] = wpabuf_len(wps->dh_pubkey_r);
1790 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1792 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1793 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1794 "not match with the pre-committed value");
1795 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1796 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1797 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1801 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1802 "half of the device password");
1803 wps->wps_pin_revealed = 0;
1804 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1810 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1812 if (mac_addr == NULL) {
1813 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1817 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1819 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1820 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1826 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1829 if (pk == NULL || pk_len == 0) {
1830 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1834 #ifdef CONFIG_WPS_OOB
1835 if (wps->wps->oob_conf.pubkey_hash != NULL) {
1837 u8 hash[WPS_HASH_LEN];
1840 sha256_vector(1, addr, &pk_len, hash);
1842 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
1843 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
1844 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
1848 #endif /* CONFIG_WPS_OOB */
1850 wpabuf_free(wps->dh_pubkey_e);
1851 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1852 if (wps->dh_pubkey_e == NULL)
1859 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1864 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1869 auth_types = WPA_GET_BE16(auth);
1871 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1873 wps->auth_type = wps->wps->auth_types & auth_types;
1874 if (wps->auth_type == 0) {
1875 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1876 "authentication types (own 0x%x Enrollee 0x%x)",
1877 wps->wps->auth_types, auth_types);
1878 #ifdef WPS_WORKAROUNDS
1880 * Some deployed implementations seem to advertise incorrect
1881 * information in this attribute. For example, Linksys WRT350N
1882 * seems to have a byteorder bug that breaks this negotiation.
1883 * In order to interoperate with existing implementations,
1884 * assume that the Enrollee supports everything we do.
1886 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1887 "does not advertise supported authentication types "
1889 wps->auth_type = wps->wps->auth_types;
1890 #else /* WPS_WORKAROUNDS */
1892 #endif /* WPS_WORKAROUNDS */
1899 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1904 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1909 encr_types = WPA_GET_BE16(encr);
1911 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1913 wps->encr_type = wps->wps->encr_types & encr_types;
1914 if (wps->encr_type == 0) {
1915 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1916 "encryption types (own 0x%x Enrollee 0x%x)",
1917 wps->wps->encr_types, encr_types);
1918 #ifdef WPS_WORKAROUNDS
1920 * Some deployed implementations seem to advertise incorrect
1921 * information in this attribute. For example, Linksys WRT350N
1922 * seems to have a byteorder bug that breaks this negotiation.
1923 * In order to interoperate with existing implementations,
1924 * assume that the Enrollee supports everything we do.
1926 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1927 "does not advertise supported encryption types "
1929 wps->encr_type = wps->wps->encr_types;
1930 #else /* WPS_WORKAROUNDS */
1932 #endif /* WPS_WORKAROUNDS */
1939 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1942 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1947 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1954 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1958 if (methods == NULL) {
1959 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1963 m = WPA_GET_BE16(methods);
1965 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1971 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1973 if (state == NULL) {
1974 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1979 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1986 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1990 if (assoc == NULL) {
1991 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1995 a = WPA_GET_BE16(assoc);
1996 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2002 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2007 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2011 e = WPA_GET_BE16(err);
2012 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2018 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2019 struct wps_parse_attr *attr)
2021 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2023 if (wps->state != RECV_M1) {
2024 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2025 "receiving M1", wps->state);
2029 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2030 wps_process_mac_addr(wps, attr->mac_addr) ||
2031 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2032 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2033 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2034 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2035 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2036 wps_process_config_methods(wps, attr->config_methods) ||
2037 wps_process_wps_state(wps, attr->wps_state) ||
2038 wps_process_device_attrs(&wps->peer_dev, attr) ||
2039 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2040 wps_process_assoc_state(wps, attr->assoc_state) ||
2041 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2042 wps_process_config_error(wps, attr->config_error) ||
2043 wps_process_os_version(&wps->peer_dev, attr->os_version))
2046 if (wps->dev_pw_id < 0x10 &&
2047 wps->dev_pw_id != DEV_PW_DEFAULT &&
2048 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2049 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2050 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2051 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2052 !wps->wps->registrar->pbc)) {
2053 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2055 wps->state = SEND_M2D;
2056 return WPS_CONTINUE;
2059 #ifdef CONFIG_WPS_OOB
2060 if (wps->dev_pw_id >= 0x10 &&
2061 wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
2062 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
2063 "%d mismatch", wps->dev_pw_id);
2064 wps->state = SEND_M2D;
2065 return WPS_CONTINUE;
2067 #endif /* CONFIG_WPS_OOB */
2069 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2070 if (wps->wps->registrar->force_pbc_overlap ||
2071 wps_registrar_pbc_overlap(wps->wps->registrar,
2072 wps->mac_addr_e, wps->uuid_e)) {
2073 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2075 wps->state = SEND_M2D;
2076 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2077 wps_pbc_overlap_event(wps->wps);
2078 wps->wps->registrar->force_pbc_overlap = 1;
2079 return WPS_CONTINUE;
2081 wps_registrar_add_pbc_session(wps->wps->registrar,
2082 wps->mac_addr_e, wps->uuid_e);
2086 wps->state = SEND_M2;
2087 return WPS_CONTINUE;
2091 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2092 const struct wpabuf *msg,
2093 struct wps_parse_attr *attr)
2095 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2097 if (wps->state != RECV_M3) {
2098 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2099 "receiving M3", wps->state);
2100 wps->state = SEND_WSC_NACK;
2101 return WPS_CONTINUE;
2104 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2105 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2107 wps->state = SEND_WSC_NACK;
2108 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2109 return WPS_CONTINUE;
2112 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2113 wps_process_authenticator(wps, attr->authenticator, msg) ||
2114 wps_process_e_hash1(wps, attr->e_hash1) ||
2115 wps_process_e_hash2(wps, attr->e_hash2)) {
2116 wps->state = SEND_WSC_NACK;
2117 return WPS_CONTINUE;
2120 wps->state = SEND_M4;
2121 return WPS_CONTINUE;
2125 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2126 const struct wpabuf *msg,
2127 struct wps_parse_attr *attr)
2129 struct wpabuf *decrypted;
2130 struct wps_parse_attr eattr;
2132 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2134 if (wps->state != RECV_M5) {
2135 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2136 "receiving M5", wps->state);
2137 wps->state = SEND_WSC_NACK;
2138 return WPS_CONTINUE;
2141 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2142 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2144 wps->state = SEND_WSC_NACK;
2145 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2146 return WPS_CONTINUE;
2149 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2150 wps_process_authenticator(wps, attr->authenticator, msg)) {
2151 wps->state = SEND_WSC_NACK;
2152 return WPS_CONTINUE;
2155 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2156 attr->encr_settings_len);
2157 if (decrypted == NULL) {
2158 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2159 "Settings attribute");
2160 wps->state = SEND_WSC_NACK;
2161 return WPS_CONTINUE;
2164 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2166 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2167 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2168 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2169 wpabuf_free(decrypted);
2170 wps->state = SEND_WSC_NACK;
2171 return WPS_CONTINUE;
2173 wpabuf_free(decrypted);
2175 wps->state = SEND_M6;
2176 return WPS_CONTINUE;
2180 static void wps_sta_cred_cb(struct wps_data *wps)
2183 * Update credential to only include a single authentication and
2184 * encryption type in case the AP configuration includes more than one
2187 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2188 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2189 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2190 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2191 if (wps->cred.encr_type & WPS_ENCR_AES)
2192 wps->cred.encr_type = WPS_ENCR_AES;
2193 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2194 wps->cred.encr_type = WPS_ENCR_TKIP;
2195 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2196 "AP configuration");
2197 if (wps->wps->cred_cb)
2198 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2202 static void wps_cred_update(struct wps_credential *dst,
2203 struct wps_credential *src)
2205 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2206 dst->ssid_len = src->ssid_len;
2207 dst->auth_type = src->auth_type;
2208 dst->encr_type = src->encr_type;
2209 dst->key_idx = src->key_idx;
2210 os_memcpy(dst->key, src->key, sizeof(dst->key));
2211 dst->key_len = src->key_len;
2215 static int wps_process_ap_settings_r(struct wps_data *wps,
2216 struct wps_parse_attr *attr)
2218 if (wps->wps->ap || wps->er)
2221 /* AP Settings Attributes in M7 when Enrollee is an AP */
2222 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2225 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2227 if (wps->new_ap_settings) {
2228 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2230 wps_cred_update(&wps->cred, wps->new_ap_settings);
2234 * Use the AP PIN only to receive the current AP settings, not
2235 * to reconfigure the AP.
2237 if (wps->ap_settings_cb) {
2238 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2242 wps_sta_cred_cb(wps);
2248 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2249 const struct wpabuf *msg,
2250 struct wps_parse_attr *attr)
2252 struct wpabuf *decrypted;
2253 struct wps_parse_attr eattr;
2255 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2257 if (wps->state != RECV_M7) {
2258 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2259 "receiving M7", wps->state);
2260 wps->state = SEND_WSC_NACK;
2261 return WPS_CONTINUE;
2264 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2265 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2267 wps->state = SEND_WSC_NACK;
2268 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2269 return WPS_CONTINUE;
2272 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2273 wps_process_authenticator(wps, attr->authenticator, msg)) {
2274 wps->state = SEND_WSC_NACK;
2275 return WPS_CONTINUE;
2278 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2279 attr->encr_settings_len);
2280 if (decrypted == NULL) {
2281 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2282 "Settings attribute");
2283 wps->state = SEND_WSC_NACK;
2284 return WPS_CONTINUE;
2287 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2289 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2290 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2291 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2292 wps_process_ap_settings_r(wps, &eattr)) {
2293 wpabuf_free(decrypted);
2294 wps->state = SEND_WSC_NACK;
2295 return WPS_CONTINUE;
2298 wpabuf_free(decrypted);
2300 wps->state = SEND_M8;
2301 return WPS_CONTINUE;
2305 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2306 const struct wpabuf *msg)
2308 struct wps_parse_attr attr;
2309 enum wps_process_res ret = WPS_CONTINUE;
2311 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2313 if (wps_parse_msg(msg, &attr) < 0)
2316 if (!wps_version_supported(attr.version)) {
2317 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2318 attr.version ? *attr.version : 0);
2322 if (attr.msg_type == NULL) {
2323 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2327 if (*attr.msg_type != WPS_M1 &&
2328 (attr.registrar_nonce == NULL ||
2329 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2330 WPS_NONCE_LEN != 0))) {
2331 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2335 switch (*attr.msg_type) {
2337 #ifdef CONFIG_WPS_UPNP
2338 if (wps->wps->wps_upnp && attr.mac_addr) {
2339 /* Remove old pending messages when starting new run */
2340 wps_free_pending_msgs(wps->wps->upnp_msgs);
2341 wps->wps->upnp_msgs = NULL;
2343 upnp_wps_device_send_wlan_event(
2344 wps->wps->wps_upnp, attr.mac_addr,
2345 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2347 #endif /* CONFIG_WPS_UPNP */
2348 ret = wps_process_m1(wps, &attr);
2351 ret = wps_process_m3(wps, msg, &attr);
2352 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2353 wps_fail_event(wps->wps, WPS_M3);
2356 ret = wps_process_m5(wps, msg, &attr);
2357 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2358 wps_fail_event(wps->wps, WPS_M5);
2361 ret = wps_process_m7(wps, msg, &attr);
2362 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2363 wps_fail_event(wps->wps, WPS_M7);
2366 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2371 if (ret == WPS_CONTINUE) {
2372 /* Save a copy of the last message for Authenticator derivation
2374 wpabuf_free(wps->last_msg);
2375 wps->last_msg = wpabuf_dup(msg);
2382 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2383 const struct wpabuf *msg)
2385 struct wps_parse_attr attr;
2387 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2389 if (wps_parse_msg(msg, &attr) < 0)
2392 if (!wps_version_supported(attr.version)) {
2393 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2394 attr.version ? *attr.version : 0);
2398 if (attr.msg_type == NULL) {
2399 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2403 if (*attr.msg_type != WPS_WSC_ACK) {
2404 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2409 #ifdef CONFIG_WPS_UPNP
2410 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2411 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2412 if (wps->wps->upnp_msgs)
2413 return WPS_CONTINUE;
2414 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2415 "external Registrar");
2418 #endif /* CONFIG_WPS_UPNP */
2420 if (attr.registrar_nonce == NULL ||
2421 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2423 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2427 if (attr.enrollee_nonce == NULL ||
2428 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2429 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2433 if (wps->state == RECV_M2D_ACK) {
2434 #ifdef CONFIG_WPS_UPNP
2435 if (wps->wps->wps_upnp &&
2436 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2437 if (wps->wps->upnp_msgs)
2438 return WPS_CONTINUE;
2439 if (wps->ext_reg == 0)
2441 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2442 "external Registrar");
2445 #endif /* CONFIG_WPS_UPNP */
2447 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2448 "terminate negotiation");
2455 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2456 const struct wpabuf *msg)
2458 struct wps_parse_attr attr;
2461 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2463 old_state = wps->state;
2464 wps->state = SEND_WSC_NACK;
2466 if (wps_parse_msg(msg, &attr) < 0)
2469 if (!wps_version_supported(attr.version)) {
2470 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2471 attr.version ? *attr.version : 0);
2475 if (attr.msg_type == NULL) {
2476 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2480 if (*attr.msg_type != WPS_WSC_NACK) {
2481 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2486 #ifdef CONFIG_WPS_UPNP
2487 if (wps->wps->wps_upnp && wps->ext_reg) {
2488 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2489 "Registrar terminated by the Enrollee");
2492 #endif /* CONFIG_WPS_UPNP */
2494 if (attr.registrar_nonce == NULL ||
2495 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2497 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2501 if (attr.enrollee_nonce == NULL ||
2502 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2503 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2507 if (attr.config_error == NULL) {
2508 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2513 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2514 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2516 switch (old_state) {
2518 wps_fail_event(wps->wps, WPS_M2);
2521 wps_fail_event(wps->wps, WPS_M4);
2524 wps_fail_event(wps->wps, WPS_M6);
2527 wps_fail_event(wps->wps, WPS_M8);
2537 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2538 const struct wpabuf *msg)
2540 struct wps_parse_attr attr;
2542 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2544 if (wps->state != RECV_DONE &&
2545 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2546 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2547 "receiving WSC_Done", wps->state);
2551 if (wps_parse_msg(msg, &attr) < 0)
2554 if (!wps_version_supported(attr.version)) {
2555 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2556 attr.version ? *attr.version : 0);
2560 if (attr.msg_type == NULL) {
2561 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2565 if (*attr.msg_type != WPS_WSC_DONE) {
2566 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2571 #ifdef CONFIG_WPS_UPNP
2572 if (wps->wps->wps_upnp && wps->ext_reg) {
2573 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2574 "Registrar completed successfully");
2575 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2579 #endif /* CONFIG_WPS_UPNP */
2581 if (attr.registrar_nonce == NULL ||
2582 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2584 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2588 if (attr.enrollee_nonce == NULL ||
2589 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2590 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2594 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2595 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2598 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2599 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2600 struct wps_credential cred;
2602 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2603 "on first Enrollee connection");
2605 os_memset(&cred, 0, sizeof(cred));
2606 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2607 cred.ssid_len = wps->wps->ssid_len;
2608 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2609 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2610 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2611 cred.key_len = wps->new_psk_len;
2613 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2614 wpa_hexdump_ascii_key(MSG_DEBUG,
2615 "WPS: Generated random passphrase",
2616 wps->new_psk, wps->new_psk_len);
2617 if (wps->wps->cred_cb)
2618 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2620 os_free(wps->new_psk);
2621 wps->new_psk = NULL;
2625 wps_sta_cred_cb(wps);
2628 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2629 wps->new_psk, wps->new_psk_len)) {
2630 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2633 os_free(wps->new_psk);
2634 wps->new_psk = NULL;
2637 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2640 wps_registrar_remove_pbc_session(wps->wps->registrar,
2641 wps->mac_addr_e, wps->uuid_e);
2642 wps_registrar_pbc_completed(wps->wps->registrar);
2644 wps_registrar_pin_completed(wps->wps->registrar);
2647 wps_success_event(wps->wps);
2653 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2654 enum wsc_op_code op_code,
2655 const struct wpabuf *msg)
2657 enum wps_process_res ret;
2659 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2661 (unsigned long) wpabuf_len(msg), op_code);
2663 #ifdef CONFIG_WPS_UPNP
2664 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2665 struct wps_parse_attr attr;
2666 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2667 *attr.msg_type == WPS_M3)
2668 wps->ext_reg = 2; /* past M2/M2D phase */
2670 if (wps->ext_reg > 1)
2671 wps_registrar_free_pending_m2(wps->wps);
2672 if (wps->wps->wps_upnp && wps->ext_reg &&
2673 wps->wps->upnp_msgs == NULL &&
2674 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
2676 struct wps_parse_attr attr;
2678 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2681 type = *attr.msg_type;
2682 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2683 " to external Registrar for processing", type);
2684 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2686 UPNP_WPS_WLANEVENT_TYPE_EAP,
2688 if (op_code == WSC_MSG)
2690 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2691 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2692 "external Registrar");
2693 return WPS_CONTINUE;
2695 #endif /* CONFIG_WPS_UPNP */
2699 return wps_process_wsc_msg(wps, msg);
2701 return wps_process_wsc_ack(wps, msg);
2703 return wps_process_wsc_nack(wps, msg);
2705 ret = wps_process_wsc_done(wps, msg);
2706 if (ret == WPS_FAILURE) {
2707 wps->state = SEND_WSC_NACK;
2708 wps_fail_event(wps->wps, WPS_WSC_DONE);
2712 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2718 int wps_registrar_update_ie(struct wps_registrar *reg)
2720 return wps_set_ie(reg);
2724 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2727 struct wps_registrar *reg = eloop_ctx;
2729 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar timed out - "
2730 "unselect Registrar");
2731 reg->selected_registrar = 0;
2733 reg->sel_reg_dev_password_id_override = -1;
2734 reg->sel_reg_config_methods_override = -1;
2736 wps_cb_set_sel_reg(reg);
2741 * wps_registrar_set_selected_registrar - Notification of SetSelectedRegistrar
2742 * @reg: Registrar data from wps_registrar_init()
2743 * @msg: Received message from SetSelectedRegistrar
2744 * Returns: 0 on success, -1 on failure
2746 * This function is called when an AP receives a SetSelectedRegistrar UPnP
2749 int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
2750 const struct wpabuf *msg)
2752 struct wps_parse_attr attr;
2754 wpa_hexdump_buf(MSG_MSGDUMP, "WPS: SetSelectedRegistrar attributes",
2757 if (wps_parse_msg(msg, &attr) < 0)
2759 if (!wps_version_supported(attr.version)) {
2760 wpa_printf(MSG_DEBUG, "WPS: Unsupported SetSelectedRegistrar "
2761 "version 0x%x", attr.version ? *attr.version : 0);
2765 if (attr.selected_registrar == NULL ||
2766 *attr.selected_registrar == 0) {
2767 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar: Disable "
2768 "Selected Registrar");
2769 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg,
2771 wps_registrar_set_selected_timeout(reg, NULL);
2775 reg->selected_registrar = 1;
2776 reg->sel_reg_dev_password_id_override = attr.dev_password_id ?
2777 WPA_GET_BE16(attr.dev_password_id) : DEV_PW_DEFAULT;
2778 reg->sel_reg_config_methods_override = attr.sel_reg_config_methods ?
2779 WPA_GET_BE16(attr.sel_reg_config_methods) : -1;
2782 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
2783 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
2784 wps_registrar_set_selected_timeout,
2790 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
2791 char *buf, size_t buflen)
2793 struct wps_registrar_device *d;
2797 d = wps_device_get(reg, addr);
2800 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
2803 ret = os_snprintf(buf + len, buflen - len,
2805 "wpsPrimaryDeviceType=%u-%08X-%u\n"
2806 "wpsDeviceName=%s\n"
2807 "wpsManufacturer=%s\n"
2809 "wpsModelNumber=%s\n"
2810 "wpsSerialNumber=%s\n",
2812 d->dev.categ, d->dev.oui, d->dev.sub_categ,
2813 d->dev.device_name ? d->dev.device_name : "",
2814 d->dev.manufacturer ? d->dev.manufacturer : "",
2815 d->dev.model_name ? d->dev.model_name : "",
2816 d->dev.model_number ? d->dev.model_number : "",
2817 d->dev.serial_number ? d->dev.serial_number : "");
2818 if (ret < 0 || (size_t) ret >= buflen - len)