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,
106 struct wps_uuid_pin *pins;
107 struct wps_pbc_session *pbc_sessions;
110 struct wpabuf *extra_cred;
111 int disable_auto_conf;
112 int sel_reg_dev_password_id_override;
113 int sel_reg_config_methods_override;
116 struct wps_registrar_device *devices;
120 static int wps_set_ie(struct wps_registrar *reg);
121 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
122 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
126 static void wps_free_devices(struct wps_registrar_device *dev)
128 struct wps_registrar_device *prev;
133 wps_device_data_free(&prev->dev);
139 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
142 struct wps_registrar_device *dev;
144 for (dev = reg->devices; dev; dev = dev->next) {
145 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
152 static void wps_device_clone_data(struct wps_device_data *dst,
153 struct wps_device_data *src)
155 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
156 dst->categ = src->categ;
158 dst->sub_categ = src->sub_categ;
160 #define WPS_STRDUP(n) \
162 dst->n = src->n ? os_strdup(src->n) : NULL
164 WPS_STRDUP(device_name);
165 WPS_STRDUP(manufacturer);
166 WPS_STRDUP(model_name);
167 WPS_STRDUP(model_number);
168 WPS_STRDUP(serial_number);
173 int wps_device_store(struct wps_registrar *reg,
174 struct wps_device_data *dev, const u8 *uuid)
176 struct wps_registrar_device *d;
178 d = wps_device_get(reg, dev->mac_addr);
180 d = os_zalloc(sizeof(*d));
183 d->next = reg->devices;
187 wps_device_clone_data(&d->dev, dev);
188 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
194 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
195 const u8 *addr, const u8 *uuid_e)
197 struct wps_pbc_session *pbc, *prev = NULL;
202 pbc = reg->pbc_sessions;
204 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
205 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
207 prev->next = pbc->next;
209 reg->pbc_sessions = pbc->next;
217 pbc = os_zalloc(sizeof(*pbc));
220 os_memcpy(pbc->addr, addr, ETH_ALEN);
222 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
225 pbc->next = reg->pbc_sessions;
226 reg->pbc_sessions = pbc;
227 pbc->timestamp = now;
229 /* remove entries that have timed out */
234 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
236 wps_free_pbc_sessions(pbc);
245 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
246 const u8 *addr, const u8 *uuid_e)
248 struct wps_pbc_session *pbc, *prev = NULL;
250 pbc = reg->pbc_sessions;
252 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
253 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
255 prev->next = pbc->next;
257 reg->pbc_sessions = pbc->next;
267 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
268 const u8 *addr, const u8 *uuid_e)
271 struct wps_pbc_session *pbc;
276 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
277 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
279 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
281 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
288 return count > 1 ? 1 : 0;
292 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
294 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
296 wpabuf_put_be16(msg, ATTR_WPS_STATE);
297 wpabuf_put_be16(msg, 1);
298 wpabuf_put_u8(msg, wps->wps_state);
303 #ifdef CONFIG_WPS_UPNP
304 static void wps_registrar_free_pending_m2(struct wps_context *wps)
306 struct upnp_pending_message *p, *p2, *prev = NULL;
309 if (p->type == WPS_M2 || p->type == WPS_M2D) {
311 wps->upnp_msgs = p->next;
313 prev->next = p->next;
314 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
317 wpabuf_free(p2->msg);
325 #endif /* CONFIG_WPS_UPNP */
328 static int wps_build_ap_setup_locked(struct wps_context *wps,
331 if (wps->ap_setup_locked) {
332 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
333 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
334 wpabuf_put_be16(msg, 1);
335 wpabuf_put_u8(msg, 1);
341 static int wps_build_selected_registrar(struct wps_registrar *reg,
344 if (!reg->selected_registrar)
346 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
347 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
348 wpabuf_put_be16(msg, 1);
349 wpabuf_put_u8(msg, 1);
354 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
357 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
358 if (!reg->selected_registrar)
360 if (reg->sel_reg_dev_password_id_override >= 0)
361 id = reg->sel_reg_dev_password_id_override;
362 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
363 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
364 wpabuf_put_be16(msg, 2);
365 wpabuf_put_be16(msg, id);
370 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
374 if (!reg->selected_registrar)
376 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
378 methods |= WPS_CONFIG_PUSHBUTTON;
379 if (reg->sel_reg_config_methods_override >= 0)
380 methods = reg->sel_reg_config_methods_override;
381 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
383 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
384 wpabuf_put_be16(msg, 2);
385 wpabuf_put_be16(msg, methods);
390 static int wps_build_probe_config_methods(struct wps_registrar *reg,
395 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
396 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
397 wpabuf_put_be16(msg, 2);
398 wpabuf_put_be16(msg, methods);
403 static int wps_build_config_methods_r(struct wps_registrar *reg,
407 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
409 methods |= WPS_CONFIG_PUSHBUTTON;
410 return wps_build_config_methods(msg, methods);
414 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
416 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
417 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
418 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
419 wpabuf_put_be16(msg, 1);
420 wpabuf_put_u8(msg, resp);
426 * wps_registrar_init - Initialize WPS Registrar data
427 * @wps: Pointer to longterm WPS context
428 * @cfg: Registrar configuration
429 * Returns: Pointer to allocated Registrar data or %NULL on failure
431 * This function is used to initialize WPS Registrar functionality. It can be
432 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
433 * runs (e.g., when run as an internal Registrar in an AP). Caller is
434 * responsible for freeing the returned data with wps_registrar_deinit() when
435 * Registrar functionality is not needed anymore.
437 struct wps_registrar *
438 wps_registrar_init(struct wps_context *wps,
439 const struct wps_registrar_config *cfg)
441 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
446 reg->new_psk_cb = cfg->new_psk_cb;
447 reg->set_ie_cb = cfg->set_ie_cb;
448 reg->pin_needed_cb = cfg->pin_needed_cb;
449 reg->reg_success_cb = cfg->reg_success_cb;
450 reg->cb_ctx = cfg->cb_ctx;
451 reg->skip_cred_build = cfg->skip_cred_build;
452 if (cfg->extra_cred) {
453 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
454 cfg->extra_cred_len);
455 if (reg->extra_cred == NULL) {
460 reg->disable_auto_conf = cfg->disable_auto_conf;
461 reg->sel_reg_dev_password_id_override = -1;
462 reg->sel_reg_config_methods_override = -1;
463 reg->static_wep_only = cfg->static_wep_only;
465 if (wps_set_ie(reg)) {
466 wps_registrar_deinit(reg);
475 * wps_registrar_deinit - Deinitialize WPS Registrar data
476 * @reg: Registrar data from wps_registrar_init()
478 void wps_registrar_deinit(struct wps_registrar *reg)
482 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
483 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
484 wps_free_pins(reg->pins);
485 wps_free_pbc_sessions(reg->pbc_sessions);
486 wpabuf_free(reg->extra_cred);
487 wps_free_devices(reg->devices);
493 * wps_registrar_add_pin - Configure a new PIN for Registrar
494 * @reg: Registrar data from wps_registrar_init()
495 * @uuid: UUID-E or %NULL for wildcard (any UUID)
496 * @pin: PIN (Device Password)
497 * @pin_len: Length of pin in octets
498 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
499 * Returns: 0 on success, -1 on failure
501 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
502 const u8 *pin, size_t pin_len, int timeout)
504 struct wps_uuid_pin *p;
506 p = os_zalloc(sizeof(*p));
510 p->wildcard_uuid = 1;
512 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
513 p->pin = os_malloc(pin_len);
514 if (p->pin == NULL) {
518 os_memcpy(p->pin, pin, pin_len);
519 p->pin_len = pin_len;
522 p->flags |= PIN_EXPIRES;
523 os_get_time(&p->expiration);
524 p->expiration.sec += timeout;
530 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
532 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
533 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
534 reg->selected_registrar = 1;
542 static void wps_registrar_expire_pins(struct wps_registrar *reg)
544 struct wps_uuid_pin *pin, *prev, *del;
551 if ((pin->flags & PIN_EXPIRES) &&
552 os_time_before(&pin->expiration, &now)) {
554 reg->pins = pin->next;
556 prev->next = pin->next;
559 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
560 del->uuid, WPS_UUID_LEN);
571 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
572 * @reg: Registrar data from wps_registrar_init()
574 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
576 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
578 struct wps_uuid_pin *pin, *prev;
583 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
585 reg->pins = pin->next;
587 prev->next = pin->next;
588 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
589 pin->uuid, WPS_UUID_LEN);
601 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
602 const u8 *uuid, size_t *pin_len)
604 struct wps_uuid_pin *pin;
606 wps_registrar_expire_pins(reg);
610 if (!pin->wildcard_uuid &&
611 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
617 /* Check for wildcard UUIDs since none of the UUID-specific
621 if (pin->wildcard_uuid == 1) {
622 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
623 "PIN. Assigned it for this UUID-E");
624 pin->wildcard_uuid = 2;
625 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
636 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
637 * that could otherwise avoid PIN invalidations.
639 if (pin->flags & PIN_LOCKED) {
640 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
641 "allow concurrent re-use");
644 *pin_len = pin->pin_len;
645 pin->flags |= PIN_LOCKED;
651 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
652 * @reg: Registrar data from wps_registrar_init()
654 * Returns: 0 on success, -1 on failure
656 * PINs are locked to enforce only one concurrent use. This function unlocks a
657 * PIN to allow it to be used again. If the specified PIN was configured using
658 * a wildcard UUID, it will be removed instead of allowing multiple uses.
660 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
662 struct wps_uuid_pin *pin;
666 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
667 if (pin->wildcard_uuid == 2) {
668 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
670 return wps_registrar_invalidate_pin(reg, uuid);
672 pin->flags &= ~PIN_LOCKED;
682 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
684 reg->selected_registrar = 0;
690 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
692 struct wps_registrar *reg = eloop_ctx;
694 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
695 wps_registrar_stop_pbc(reg);
700 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
701 * @reg: Registrar data from wps_registrar_init()
702 * Returns: 0 on success, -1 on failure
704 * This function is called on an AP when a push button is pushed to activate
705 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
706 * or when a PBC registration is completed.
708 int wps_registrar_button_pushed(struct wps_registrar *reg)
710 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
711 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
715 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
716 reg->selected_registrar = 1;
720 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
721 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
727 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
729 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
730 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
731 wps_registrar_stop_pbc(reg);
736 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
737 * @reg: Registrar data from wps_registrar_init()
738 * @addr: MAC address of the Probe Request sender
739 * @wps_data: WPS IE contents
741 * This function is called on an AP when a Probe Request with WPS IE is
742 * received. This is used to track PBC mode use and to detect possible overlap
743 * situation with other WPS APs.
745 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
746 const struct wpabuf *wps_data)
748 struct wps_parse_attr attr;
751 wpa_hexdump_buf(MSG_MSGDUMP,
752 "WPS: Probe Request with WPS data received",
755 if (wps_parse_msg(wps_data, &attr) < 0)
757 if (!wps_version_supported(attr.version)) {
758 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
759 "version 0x%x", attr.version ? *attr.version : 0);
763 if (attr.config_methods == NULL) {
764 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
769 methods = WPA_GET_BE16(attr.config_methods);
770 if (!(methods & WPS_CONFIG_PUSHBUTTON))
771 return; /* Not PBC */
773 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
774 MACSTR, MAC2STR(addr));
776 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
780 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
781 const u8 *psk, size_t psk_len)
783 if (reg->new_psk_cb == NULL)
786 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
790 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
791 const struct wps_device_data *dev)
793 if (reg->pin_needed_cb == NULL)
796 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
800 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
803 if (reg->reg_success_cb == NULL)
806 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
810 static int wps_cb_set_ie(struct wps_registrar *reg,
811 const struct wpabuf *beacon_ie,
812 const struct wpabuf *probe_resp_ie)
814 if (reg->set_ie_cb == NULL)
817 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
818 wpabuf_len(beacon_ie),
819 wpabuf_head(probe_resp_ie),
820 wpabuf_len(probe_resp_ie));
824 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
825 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
830 ie = wpabuf_alloc(wpabuf_len(data) + 100);
836 pos = wpabuf_head(data);
837 end = pos + wpabuf_len(data);
840 size_t frag_len = end - pos;
843 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
844 wpabuf_put_u8(ie, 4 + frag_len);
845 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
846 wpabuf_put_data(ie, pos, frag_len);
856 static int wps_set_ie(struct wps_registrar *reg)
858 struct wpabuf *beacon;
859 struct wpabuf *probe;
862 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
864 beacon = wpabuf_alloc(300);
867 probe = wpabuf_alloc(400);
873 if (wps_build_version(beacon) ||
874 wps_build_wps_state(reg->wps, beacon) ||
875 wps_build_ap_setup_locked(reg->wps, beacon) ||
876 wps_build_selected_registrar(reg, beacon) ||
877 wps_build_sel_reg_dev_password_id(reg, beacon) ||
878 wps_build_sel_reg_config_methods(reg, beacon) ||
879 wps_build_version(probe) ||
880 wps_build_wps_state(reg->wps, probe) ||
881 wps_build_ap_setup_locked(reg->wps, probe) ||
882 wps_build_selected_registrar(reg, probe) ||
883 wps_build_sel_reg_dev_password_id(reg, probe) ||
884 wps_build_sel_reg_config_methods(reg, probe) ||
885 wps_build_resp_type(reg, probe) ||
886 wps_build_uuid_e(probe, reg->wps->uuid) ||
887 wps_build_device_attrs(®->wps->dev, probe) ||
888 wps_build_probe_config_methods(reg, probe) ||
889 wps_build_rf_bands(®->wps->dev, probe)) {
895 beacon = wps_ie_encapsulate(beacon);
896 probe = wps_ie_encapsulate(probe);
898 if (!beacon || !probe) {
904 if (reg->static_wep_only) {
906 * Windows XP and Vista clients can get confused about
907 * EAP-Identity/Request when they probe the network with
908 * EAPOL-Start. In such a case, they may assume the network is
909 * using IEEE 802.1X and prompt user for a certificate while
910 * the correct (non-WPS) behavior would be to ask for the
911 * static WEP key. As a workaround, use Microsoft Provisioning
912 * IE to advertise that legacy 802.1X is not supported.
914 const u8 ms_wps[7] = {
915 WLAN_EID_VENDOR_SPECIFIC, 5,
916 /* Microsoft Provisioning IE (00:50:f2:5) */
918 0x00 /* no legacy 802.1X or MS WPS */
920 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
921 "into Beacon/Probe Response frames");
922 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
923 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
926 ret = wps_cb_set_ie(reg, beacon, probe);
934 static int wps_get_dev_password(struct wps_data *wps)
939 os_free(wps->dev_password);
940 wps->dev_password = NULL;
943 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
944 pin = (const u8 *) "00000000";
947 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
951 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
953 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
958 wps->dev_password = os_malloc(pin_len);
959 if (wps->dev_password == NULL)
961 os_memcpy(wps->dev_password, pin, pin_len);
962 wps->dev_password_len = pin_len;
968 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
970 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
971 wpabuf_put_be16(msg, ATTR_UUID_R);
972 wpabuf_put_be16(msg, WPS_UUID_LEN);
973 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
978 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
984 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
986 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
987 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
988 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
990 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
991 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
992 "R-Hash derivation");
996 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
997 wpabuf_put_be16(msg, ATTR_R_HASH1);
998 wpabuf_put_be16(msg, SHA256_MAC_LEN);
999 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1000 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1001 addr[0] = wps->snonce;
1002 len[0] = WPS_SECRET_NONCE_LEN;
1003 addr[1] = wps->psk1;
1004 len[1] = WPS_PSK_LEN;
1005 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1006 len[2] = wpabuf_len(wps->dh_pubkey_e);
1007 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1008 len[3] = wpabuf_len(wps->dh_pubkey_r);
1009 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1010 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1012 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1013 wpabuf_put_be16(msg, ATTR_R_HASH2);
1014 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1015 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1016 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1017 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1018 addr[1] = wps->psk2;
1019 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1020 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1026 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1028 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1029 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1030 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1031 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1036 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1038 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1039 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1040 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1041 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1042 WPS_SECRET_NONCE_LEN);
1047 static int wps_build_cred_network_idx(struct wpabuf *msg,
1048 struct wps_credential *cred)
1050 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
1051 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1052 wpabuf_put_be16(msg, 1);
1053 wpabuf_put_u8(msg, 1);
1058 static int wps_build_cred_ssid(struct wpabuf *msg,
1059 struct wps_credential *cred)
1061 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1062 wpabuf_put_be16(msg, ATTR_SSID);
1063 wpabuf_put_be16(msg, cred->ssid_len);
1064 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1069 static int wps_build_cred_auth_type(struct wpabuf *msg,
1070 struct wps_credential *cred)
1072 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1074 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1075 wpabuf_put_be16(msg, 2);
1076 wpabuf_put_be16(msg, cred->auth_type);
1081 static int wps_build_cred_encr_type(struct wpabuf *msg,
1082 struct wps_credential *cred)
1084 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1086 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1087 wpabuf_put_be16(msg, 2);
1088 wpabuf_put_be16(msg, cred->encr_type);
1093 static int wps_build_cred_network_key(struct wpabuf *msg,
1094 struct wps_credential *cred)
1096 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
1097 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1098 wpabuf_put_be16(msg, cred->key_len);
1099 wpabuf_put_data(msg, cred->key, cred->key_len);
1104 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1105 struct wps_credential *cred)
1107 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1108 MAC2STR(cred->mac_addr));
1109 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1110 wpabuf_put_be16(msg, ETH_ALEN);
1111 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1116 static int wps_build_credential(struct wpabuf *msg,
1117 struct wps_credential *cred)
1119 if (wps_build_cred_network_idx(msg, cred) ||
1120 wps_build_cred_ssid(msg, cred) ||
1121 wps_build_cred_auth_type(msg, cred) ||
1122 wps_build_cred_encr_type(msg, cred) ||
1123 wps_build_cred_network_key(msg, cred) ||
1124 wps_build_cred_mac_addr(msg, cred))
1130 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1132 struct wpabuf *cred;
1134 if (wps->wps->registrar->skip_cred_build)
1135 goto skip_cred_build;
1137 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1138 os_memset(&wps->cred, 0, sizeof(wps->cred));
1140 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1141 wps->cred.ssid_len = wps->wps->ssid_len;
1143 /* Select the best authentication and encryption type */
1144 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1145 wps->auth_type = WPS_AUTH_WPA2PSK;
1146 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1147 wps->auth_type = WPS_AUTH_WPAPSK;
1148 else if (wps->auth_type & WPS_AUTH_OPEN)
1149 wps->auth_type = WPS_AUTH_OPEN;
1150 else if (wps->auth_type & WPS_AUTH_SHARED)
1151 wps->auth_type = WPS_AUTH_SHARED;
1153 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1157 wps->cred.auth_type = wps->auth_type;
1159 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1160 wps->auth_type == WPS_AUTH_WPAPSK) {
1161 if (wps->encr_type & WPS_ENCR_AES)
1162 wps->encr_type = WPS_ENCR_AES;
1163 else if (wps->encr_type & WPS_ENCR_TKIP)
1164 wps->encr_type = WPS_ENCR_TKIP;
1166 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1167 "type for WPA/WPA2");
1171 if (wps->encr_type & WPS_ENCR_WEP)
1172 wps->encr_type = WPS_ENCR_WEP;
1173 else if (wps->encr_type & WPS_ENCR_NONE)
1174 wps->encr_type = WPS_ENCR_NONE;
1176 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1177 "type for non-WPA/WPA2 mode");
1181 wps->cred.encr_type = wps->encr_type;
1182 /* Set MAC address in the Credential to be the AP's address (BSSID) */
1183 os_memcpy(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN);
1185 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1186 !wps->wps->registrar->disable_auto_conf) {
1188 /* Generate a random passphrase */
1189 if (os_get_random(r, sizeof(r)) < 0)
1191 os_free(wps->new_psk);
1192 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1193 if (wps->new_psk == NULL)
1195 wps->new_psk_len--; /* remove newline */
1196 while (wps->new_psk_len &&
1197 wps->new_psk[wps->new_psk_len - 1] == '=')
1199 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1200 wps->new_psk, wps->new_psk_len);
1201 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1202 wps->cred.key_len = wps->new_psk_len;
1203 } else if (wps->wps->network_key) {
1204 os_memcpy(wps->cred.key, wps->wps->network_key,
1205 wps->wps->network_key_len);
1206 wps->cred.key_len = wps->wps->network_key_len;
1207 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1209 /* Generate a random per-device PSK */
1210 os_free(wps->new_psk);
1211 wps->new_psk_len = 32;
1212 wps->new_psk = os_malloc(wps->new_psk_len);
1213 if (wps->new_psk == NULL)
1215 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1216 os_free(wps->new_psk);
1217 wps->new_psk = NULL;
1220 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1221 wps->new_psk, wps->new_psk_len);
1222 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1224 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1225 wps->cred.key_len = wps->new_psk_len * 2;
1228 cred = wpabuf_alloc(200);
1232 if (wps_build_credential(cred, &wps->cred)) {
1237 wpabuf_put_be16(msg, ATTR_CRED);
1238 wpabuf_put_be16(msg, wpabuf_len(cred));
1239 wpabuf_put_buf(msg, cred);
1243 if (wps->wps->registrar->extra_cred) {
1244 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1245 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1252 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1254 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1256 if (wps_build_credential(msg, &wps->cred))
1263 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1267 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1269 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1270 wps->nonce_r, WPS_NONCE_LEN);
1271 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1273 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1274 msg = wpabuf_alloc(1000);
1278 if (wps_build_version(msg) ||
1279 wps_build_msg_type(msg, WPS_M2) ||
1280 wps_build_enrollee_nonce(wps, msg) ||
1281 wps_build_registrar_nonce(wps, msg) ||
1282 wps_build_uuid_r(wps, msg) ||
1283 wps_build_public_key(wps, msg) ||
1284 wps_derive_keys(wps) ||
1285 wps_build_auth_type_flags(wps, msg) ||
1286 wps_build_encr_type_flags(wps, msg) ||
1287 wps_build_conn_type_flags(wps, msg) ||
1288 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1289 wps_build_device_attrs(&wps->wps->dev, msg) ||
1290 wps_build_rf_bands(&wps->wps->dev, msg) ||
1291 wps_build_assoc_state(wps, msg) ||
1292 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1293 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1294 wps_build_os_version(&wps->wps->dev, msg) ||
1295 wps_build_authenticator(wps, msg)) {
1300 wps->state = RECV_M3;
1305 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1308 u16 err = wps->config_error;
1310 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1311 msg = wpabuf_alloc(1000);
1315 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1316 err == WPS_CFG_NO_ERROR)
1317 err = WPS_CFG_SETUP_LOCKED;
1319 if (wps_build_version(msg) ||
1320 wps_build_msg_type(msg, WPS_M2D) ||
1321 wps_build_enrollee_nonce(wps, msg) ||
1322 wps_build_registrar_nonce(wps, msg) ||
1323 wps_build_uuid_r(wps, msg) ||
1324 wps_build_auth_type_flags(wps, msg) ||
1325 wps_build_encr_type_flags(wps, msg) ||
1326 wps_build_conn_type_flags(wps, msg) ||
1327 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1328 wps_build_device_attrs(&wps->wps->dev, msg) ||
1329 wps_build_rf_bands(&wps->wps->dev, msg) ||
1330 wps_build_assoc_state(wps, msg) ||
1331 wps_build_config_error(msg, err) ||
1332 wps_build_os_version(&wps->wps->dev, msg)) {
1337 wps->state = RECV_M2D_ACK;
1342 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1344 struct wpabuf *msg, *plain;
1346 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1348 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1350 plain = wpabuf_alloc(200);
1354 msg = wpabuf_alloc(1000);
1360 if (wps_build_version(msg) ||
1361 wps_build_msg_type(msg, WPS_M4) ||
1362 wps_build_enrollee_nonce(wps, msg) ||
1363 wps_build_r_hash(wps, msg) ||
1364 wps_build_r_snonce1(wps, plain) ||
1365 wps_build_key_wrap_auth(wps, plain) ||
1366 wps_build_encr_settings(wps, msg, plain) ||
1367 wps_build_authenticator(wps, msg)) {
1374 wps->state = RECV_M5;
1379 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1381 struct wpabuf *msg, *plain;
1383 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1385 plain = wpabuf_alloc(200);
1389 msg = wpabuf_alloc(1000);
1395 if (wps_build_version(msg) ||
1396 wps_build_msg_type(msg, WPS_M6) ||
1397 wps_build_enrollee_nonce(wps, msg) ||
1398 wps_build_r_snonce2(wps, plain) ||
1399 wps_build_key_wrap_auth(wps, plain) ||
1400 wps_build_encr_settings(wps, msg, plain) ||
1401 wps_build_authenticator(wps, msg)) {
1408 wps->wps_pin_revealed = 1;
1409 wps->state = RECV_M7;
1414 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1416 struct wpabuf *msg, *plain;
1418 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1420 plain = wpabuf_alloc(500);
1424 msg = wpabuf_alloc(1000);
1430 if (wps_build_version(msg) ||
1431 wps_build_msg_type(msg, WPS_M8) ||
1432 wps_build_enrollee_nonce(wps, msg) ||
1433 (wps->wps->ap && wps_build_cred(wps, plain)) ||
1434 (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1435 wps_build_key_wrap_auth(wps, plain) ||
1436 wps_build_encr_settings(wps, msg, plain) ||
1437 wps_build_authenticator(wps, msg)) {
1444 wps->state = RECV_DONE;
1449 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1453 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1455 msg = wpabuf_alloc(1000);
1459 if (wps_build_version(msg) ||
1460 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1461 wps_build_enrollee_nonce(wps, msg) ||
1462 wps_build_registrar_nonce(wps, msg)) {
1471 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1475 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1477 msg = wpabuf_alloc(1000);
1481 if (wps_build_version(msg) ||
1482 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1483 wps_build_enrollee_nonce(wps, msg) ||
1484 wps_build_registrar_nonce(wps, msg) ||
1485 wps_build_config_error(msg, wps->config_error)) {
1494 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1495 enum wsc_op_code *op_code)
1499 #ifdef CONFIG_WPS_UPNP
1500 if (wps->wps->wps_upnp) {
1501 struct upnp_pending_message *p, *prev = NULL;
1502 if (wps->ext_reg > 1)
1503 wps_registrar_free_pending_m2(wps->wps);
1504 p = wps->wps->upnp_msgs;
1505 /* TODO: check pending message MAC address */
1506 while (p && p->next) {
1511 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1516 wps->wps->upnp_msgs = NULL;
1520 if (wps->ext_reg == 0)
1526 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1527 "pending message available");
1530 #endif /* CONFIG_WPS_UPNP */
1532 switch (wps->state) {
1534 if (wps_get_dev_password(wps) < 0)
1535 msg = wps_build_m2d(wps);
1537 msg = wps_build_m2(wps);
1541 msg = wps_build_m2d(wps);
1545 msg = wps_build_m4(wps);
1549 msg = wps_build_m6(wps);
1553 msg = wps_build_m8(wps);
1557 msg = wps_build_wsc_ack(wps);
1561 msg = wps_build_wsc_nack(wps);
1562 *op_code = WSC_NACK;
1565 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1566 "a message", wps->state);
1571 if (*op_code == WSC_MSG && msg) {
1572 /* Save a copy of the last message for Authenticator derivation
1574 wpabuf_free(wps->last_msg);
1575 wps->last_msg = wpabuf_dup(msg);
1582 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1584 if (e_nonce == NULL) {
1585 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1589 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1590 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1591 wps->nonce_e, WPS_NONCE_LEN);
1597 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1599 if (r_nonce == NULL) {
1600 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1604 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1605 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1613 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1615 if (uuid_e == NULL) {
1616 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1620 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1621 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1627 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1629 if (pw_id == NULL) {
1630 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1634 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1635 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1641 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1643 if (e_hash1 == NULL) {
1644 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1648 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1649 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1655 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1657 if (e_hash2 == NULL) {
1658 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1662 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1663 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1669 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1671 u8 hash[SHA256_MAC_LEN];
1675 if (e_snonce1 == NULL) {
1676 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1680 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1681 WPS_SECRET_NONCE_LEN);
1683 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1684 addr[0] = e_snonce1;
1685 len[0] = WPS_SECRET_NONCE_LEN;
1686 addr[1] = wps->psk1;
1687 len[1] = WPS_PSK_LEN;
1688 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1689 len[2] = wpabuf_len(wps->dh_pubkey_e);
1690 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1691 len[3] = wpabuf_len(wps->dh_pubkey_r);
1692 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1694 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1695 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1696 "not match with the pre-committed value");
1697 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1698 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1702 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1703 "half of the device password");
1709 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1711 u8 hash[SHA256_MAC_LEN];
1715 if (e_snonce2 == NULL) {
1716 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1720 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1721 WPS_SECRET_NONCE_LEN);
1723 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1724 addr[0] = e_snonce2;
1725 len[0] = WPS_SECRET_NONCE_LEN;
1726 addr[1] = wps->psk2;
1727 len[1] = WPS_PSK_LEN;
1728 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1729 len[2] = wpabuf_len(wps->dh_pubkey_e);
1730 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1731 len[3] = wpabuf_len(wps->dh_pubkey_r);
1732 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1734 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1735 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1736 "not match with the pre-committed value");
1737 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1738 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1739 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1743 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1744 "half of the device password");
1745 wps->wps_pin_revealed = 0;
1746 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1752 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1754 if (mac_addr == NULL) {
1755 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1759 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1761 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1762 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1768 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1771 if (pk == NULL || pk_len == 0) {
1772 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1776 #ifdef CONFIG_WPS_OOB
1777 if (wps->wps->oob_conf.pubkey_hash != NULL) {
1779 u8 hash[WPS_HASH_LEN];
1782 sha256_vector(1, addr, &pk_len, hash);
1784 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
1785 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
1786 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
1790 #endif /* CONFIG_WPS_OOB */
1792 wpabuf_free(wps->dh_pubkey_e);
1793 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1794 if (wps->dh_pubkey_e == NULL)
1801 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1806 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1811 auth_types = WPA_GET_BE16(auth);
1813 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1815 wps->auth_type = wps->wps->auth_types & auth_types;
1816 if (wps->auth_type == 0) {
1817 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1818 "authentication types (own 0x%x Enrollee 0x%x)",
1819 wps->wps->auth_types, auth_types);
1820 #ifdef WPS_WORKAROUNDS
1822 * Some deployed implementations seem to advertise incorrect
1823 * information in this attribute. For example, Linksys WRT350N
1824 * seems to have a byteorder bug that breaks this negotiation.
1825 * In order to interoperate with existing implementations,
1826 * assume that the Enrollee supports everything we do.
1828 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1829 "does not advertise supported authentication types "
1831 wps->auth_type = wps->wps->auth_types;
1832 #else /* WPS_WORKAROUNDS */
1834 #endif /* WPS_WORKAROUNDS */
1841 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1846 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1851 encr_types = WPA_GET_BE16(encr);
1853 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1855 wps->encr_type = wps->wps->encr_types & encr_types;
1856 if (wps->encr_type == 0) {
1857 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1858 "encryption types (own 0x%x Enrollee 0x%x)",
1859 wps->wps->encr_types, encr_types);
1860 #ifdef WPS_WORKAROUNDS
1862 * Some deployed implementations seem to advertise incorrect
1863 * information in this attribute. For example, Linksys WRT350N
1864 * seems to have a byteorder bug that breaks this negotiation.
1865 * In order to interoperate with existing implementations,
1866 * assume that the Enrollee supports everything we do.
1868 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1869 "does not advertise supported encryption types "
1871 wps->encr_type = wps->wps->encr_types;
1872 #else /* WPS_WORKAROUNDS */
1874 #endif /* WPS_WORKAROUNDS */
1881 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1884 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1889 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1896 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1900 if (methods == NULL) {
1901 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1905 m = WPA_GET_BE16(methods);
1907 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1913 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1915 if (state == NULL) {
1916 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1921 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1928 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1932 if (assoc == NULL) {
1933 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1937 a = WPA_GET_BE16(assoc);
1938 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1944 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1949 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1953 e = WPA_GET_BE16(err);
1954 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1960 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1961 struct wps_parse_attr *attr)
1963 wpa_printf(MSG_DEBUG, "WPS: Received M1");
1965 if (wps->state != RECV_M1) {
1966 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1967 "receiving M1", wps->state);
1971 if (wps_process_uuid_e(wps, attr->uuid_e) ||
1972 wps_process_mac_addr(wps, attr->mac_addr) ||
1973 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1974 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1975 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1976 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1977 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1978 wps_process_config_methods(wps, attr->config_methods) ||
1979 wps_process_wps_state(wps, attr->wps_state) ||
1980 wps_process_device_attrs(&wps->peer_dev, attr) ||
1981 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1982 wps_process_assoc_state(wps, attr->assoc_state) ||
1983 wps_process_dev_password_id(wps, attr->dev_password_id) ||
1984 wps_process_config_error(wps, attr->config_error) ||
1985 wps_process_os_version(&wps->peer_dev, attr->os_version))
1988 if (wps->dev_pw_id < 0x10 &&
1989 wps->dev_pw_id != DEV_PW_DEFAULT &&
1990 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1991 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1992 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1993 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
1994 !wps->wps->registrar->pbc)) {
1995 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1997 wps->state = SEND_M2D;
1998 return WPS_CONTINUE;
2001 #ifdef CONFIG_WPS_OOB
2002 if (wps->dev_pw_id >= 0x10 &&
2003 wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
2004 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
2005 "%d mismatch", wps->dev_pw_id);
2006 wps->state = SEND_M2D;
2007 return WPS_CONTINUE;
2009 #endif /* CONFIG_WPS_OOB */
2011 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2012 if (wps_registrar_pbc_overlap(wps->wps->registrar,
2013 wps->mac_addr_e, wps->uuid_e)) {
2014 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2016 wps->state = SEND_M2D;
2017 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2018 return WPS_CONTINUE;
2020 wps_registrar_add_pbc_session(wps->wps->registrar,
2021 wps->mac_addr_e, wps->uuid_e);
2025 wps->state = SEND_M2;
2026 return WPS_CONTINUE;
2030 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2031 const struct wpabuf *msg,
2032 struct wps_parse_attr *attr)
2034 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2036 if (wps->state != RECV_M3) {
2037 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2038 "receiving M3", wps->state);
2039 wps->state = SEND_WSC_NACK;
2040 return WPS_CONTINUE;
2043 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2044 wps_process_authenticator(wps, attr->authenticator, msg) ||
2045 wps_process_e_hash1(wps, attr->e_hash1) ||
2046 wps_process_e_hash2(wps, attr->e_hash2)) {
2047 wps->state = SEND_WSC_NACK;
2048 return WPS_CONTINUE;
2051 wps->state = SEND_M4;
2052 return WPS_CONTINUE;
2056 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2057 const struct wpabuf *msg,
2058 struct wps_parse_attr *attr)
2060 struct wpabuf *decrypted;
2061 struct wps_parse_attr eattr;
2063 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2065 if (wps->state != RECV_M5) {
2066 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2067 "receiving M5", wps->state);
2068 wps->state = SEND_WSC_NACK;
2069 return WPS_CONTINUE;
2072 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2073 wps_process_authenticator(wps, attr->authenticator, msg)) {
2074 wps->state = SEND_WSC_NACK;
2075 return WPS_CONTINUE;
2078 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2079 attr->encr_settings_len);
2080 if (decrypted == NULL) {
2081 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2082 "Settings attribute");
2083 wps->state = SEND_WSC_NACK;
2084 return WPS_CONTINUE;
2087 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2089 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2090 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2091 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2092 wpabuf_free(decrypted);
2093 wps->state = SEND_WSC_NACK;
2094 return WPS_CONTINUE;
2096 wpabuf_free(decrypted);
2098 wps->state = SEND_M6;
2099 return WPS_CONTINUE;
2103 static void wps_sta_cred_cb(struct wps_data *wps)
2106 * Update credential to only include a single authentication and
2107 * encryption type in case the AP configuration includes more than one
2110 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2111 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2112 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2113 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2114 if (wps->cred.encr_type & WPS_ENCR_AES)
2115 wps->cred.encr_type = WPS_ENCR_AES;
2116 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2117 wps->cred.encr_type = WPS_ENCR_TKIP;
2118 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2119 "AP configuration");
2120 if (wps->wps->cred_cb)
2121 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2125 static void wps_cred_update(struct wps_credential *dst,
2126 struct wps_credential *src)
2128 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2129 dst->ssid_len = src->ssid_len;
2130 dst->auth_type = src->auth_type;
2131 dst->encr_type = src->encr_type;
2132 dst->key_idx = src->key_idx;
2133 os_memcpy(dst->key, src->key, sizeof(dst->key));
2134 dst->key_len = src->key_len;
2138 static int wps_process_ap_settings_r(struct wps_data *wps,
2139 struct wps_parse_attr *attr)
2144 /* AP Settings Attributes in M7 when Enrollee is an AP */
2145 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2148 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2150 if (wps->new_ap_settings) {
2151 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2153 wps_cred_update(&wps->cred, wps->new_ap_settings);
2157 * Use the AP PIN only to receive the current AP settings, not
2158 * to reconfigure the AP.
2160 wps_sta_cred_cb(wps);
2166 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2167 const struct wpabuf *msg,
2168 struct wps_parse_attr *attr)
2170 struct wpabuf *decrypted;
2171 struct wps_parse_attr eattr;
2173 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2175 if (wps->state != RECV_M7) {
2176 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2177 "receiving M7", wps->state);
2178 wps->state = SEND_WSC_NACK;
2179 return WPS_CONTINUE;
2182 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2183 wps_process_authenticator(wps, attr->authenticator, msg)) {
2184 wps->state = SEND_WSC_NACK;
2185 return WPS_CONTINUE;
2188 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2189 attr->encr_settings_len);
2190 if (decrypted == NULL) {
2191 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2192 "Settings attribute");
2193 wps->state = SEND_WSC_NACK;
2194 return WPS_CONTINUE;
2197 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2199 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2200 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2201 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2202 wps_process_ap_settings_r(wps, &eattr)) {
2203 wpabuf_free(decrypted);
2204 wps->state = SEND_WSC_NACK;
2205 return WPS_CONTINUE;
2208 wpabuf_free(decrypted);
2210 wps->state = SEND_M8;
2211 return WPS_CONTINUE;
2215 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2216 const struct wpabuf *msg)
2218 struct wps_parse_attr attr;
2219 enum wps_process_res ret = WPS_CONTINUE;
2221 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2223 if (wps_parse_msg(msg, &attr) < 0)
2226 if (!wps_version_supported(attr.version)) {
2227 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2228 attr.version ? *attr.version : 0);
2232 if (attr.msg_type == NULL) {
2233 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2237 if (*attr.msg_type != WPS_M1 &&
2238 (attr.registrar_nonce == NULL ||
2239 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2240 WPS_NONCE_LEN != 0))) {
2241 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2245 switch (*attr.msg_type) {
2247 #ifdef CONFIG_WPS_UPNP
2248 if (wps->wps->wps_upnp && attr.mac_addr) {
2249 /* Remove old pending messages when starting new run */
2250 wps_free_pending_msgs(wps->wps->upnp_msgs);
2251 wps->wps->upnp_msgs = NULL;
2253 upnp_wps_device_send_wlan_event(
2254 wps->wps->wps_upnp, attr.mac_addr,
2255 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2257 #endif /* CONFIG_WPS_UPNP */
2258 ret = wps_process_m1(wps, &attr);
2261 ret = wps_process_m3(wps, msg, &attr);
2262 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2263 wps_fail_event(wps->wps, WPS_M3);
2266 ret = wps_process_m5(wps, msg, &attr);
2267 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2268 wps_fail_event(wps->wps, WPS_M5);
2271 ret = wps_process_m7(wps, msg, &attr);
2272 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2273 wps_fail_event(wps->wps, WPS_M7);
2276 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2281 if (ret == WPS_CONTINUE) {
2282 /* Save a copy of the last message for Authenticator derivation
2284 wpabuf_free(wps->last_msg);
2285 wps->last_msg = wpabuf_dup(msg);
2292 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2293 const struct wpabuf *msg)
2295 struct wps_parse_attr attr;
2297 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2299 if (wps_parse_msg(msg, &attr) < 0)
2302 if (!wps_version_supported(attr.version)) {
2303 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2304 attr.version ? *attr.version : 0);
2308 if (attr.msg_type == NULL) {
2309 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2313 if (*attr.msg_type != WPS_WSC_ACK) {
2314 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2319 #ifdef CONFIG_WPS_UPNP
2320 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2321 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2322 if (wps->wps->upnp_msgs)
2323 return WPS_CONTINUE;
2324 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2325 "external Registrar");
2328 #endif /* CONFIG_WPS_UPNP */
2330 if (attr.registrar_nonce == NULL ||
2331 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2333 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2337 if (attr.enrollee_nonce == NULL ||
2338 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2339 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2343 if (wps->state == RECV_M2D_ACK) {
2344 #ifdef CONFIG_WPS_UPNP
2345 if (wps->wps->wps_upnp &&
2346 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2347 if (wps->wps->upnp_msgs)
2348 return WPS_CONTINUE;
2349 if (wps->ext_reg == 0)
2351 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2352 "external Registrar");
2355 #endif /* CONFIG_WPS_UPNP */
2357 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2358 "terminate negotiation");
2365 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2366 const struct wpabuf *msg)
2368 struct wps_parse_attr attr;
2371 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2373 old_state = wps->state;
2374 wps->state = SEND_WSC_NACK;
2376 if (wps_parse_msg(msg, &attr) < 0)
2379 if (!wps_version_supported(attr.version)) {
2380 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2381 attr.version ? *attr.version : 0);
2385 if (attr.msg_type == NULL) {
2386 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2390 if (*attr.msg_type != WPS_WSC_NACK) {
2391 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2396 #ifdef CONFIG_WPS_UPNP
2397 if (wps->wps->wps_upnp && wps->ext_reg) {
2398 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2399 "Registrar terminated by the Enrollee");
2402 #endif /* CONFIG_WPS_UPNP */
2404 if (attr.registrar_nonce == NULL ||
2405 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2407 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2411 if (attr.enrollee_nonce == NULL ||
2412 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2413 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2417 if (attr.config_error == NULL) {
2418 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2423 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2424 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2426 switch (old_state) {
2428 wps_fail_event(wps->wps, WPS_M2);
2431 wps_fail_event(wps->wps, WPS_M4);
2434 wps_fail_event(wps->wps, WPS_M6);
2437 wps_fail_event(wps->wps, WPS_M8);
2447 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2448 const struct wpabuf *msg)
2450 struct wps_parse_attr attr;
2452 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2454 if (wps->state != RECV_DONE &&
2455 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2456 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2457 "receiving WSC_Done", wps->state);
2461 if (wps_parse_msg(msg, &attr) < 0)
2464 if (!wps_version_supported(attr.version)) {
2465 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2466 attr.version ? *attr.version : 0);
2470 if (attr.msg_type == NULL) {
2471 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2475 if (*attr.msg_type != WPS_WSC_DONE) {
2476 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2481 #ifdef CONFIG_WPS_UPNP
2482 if (wps->wps->wps_upnp && wps->ext_reg) {
2483 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2484 "Registrar completed successfully");
2485 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2489 #endif /* CONFIG_WPS_UPNP */
2491 if (attr.registrar_nonce == NULL ||
2492 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2494 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2498 if (attr.enrollee_nonce == NULL ||
2499 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2500 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2504 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2505 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2508 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2509 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2510 struct wps_credential cred;
2512 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2513 "on first Enrollee connection");
2515 os_memset(&cred, 0, sizeof(cred));
2516 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2517 cred.ssid_len = wps->wps->ssid_len;
2518 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2519 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2520 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2521 cred.key_len = wps->new_psk_len;
2523 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2524 wpa_hexdump_ascii_key(MSG_DEBUG,
2525 "WPS: Generated random passphrase",
2526 wps->new_psk, wps->new_psk_len);
2527 if (wps->wps->cred_cb)
2528 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2530 os_free(wps->new_psk);
2531 wps->new_psk = NULL;
2535 wps_sta_cred_cb(wps);
2538 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2539 wps->new_psk, wps->new_psk_len)) {
2540 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2543 os_free(wps->new_psk);
2544 wps->new_psk = NULL;
2547 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2550 wps_registrar_remove_pbc_session(wps->wps->registrar,
2551 wps->mac_addr_e, wps->uuid_e);
2552 wps_registrar_pbc_completed(wps->wps->registrar);
2555 wps_success_event(wps->wps);
2561 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2562 enum wsc_op_code op_code,
2563 const struct wpabuf *msg)
2565 enum wps_process_res ret;
2567 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2569 (unsigned long) wpabuf_len(msg), op_code);
2571 #ifdef CONFIG_WPS_UPNP
2572 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2573 struct wps_parse_attr attr;
2574 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2575 *attr.msg_type == WPS_M3)
2576 wps->ext_reg = 2; /* past M2/M2D phase */
2578 if (wps->ext_reg > 1)
2579 wps_registrar_free_pending_m2(wps->wps);
2580 if (wps->wps->wps_upnp && wps->ext_reg &&
2581 wps->wps->upnp_msgs == NULL &&
2582 (op_code == WSC_MSG || op_code == WSC_Done)) {
2583 struct wps_parse_attr attr;
2585 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2588 type = *attr.msg_type;
2589 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2590 " to external Registrar for processing", type);
2591 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2593 UPNP_WPS_WLANEVENT_TYPE_EAP,
2595 if (op_code == WSC_MSG)
2597 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2598 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2599 "external Registrar");
2600 return WPS_CONTINUE;
2602 #endif /* CONFIG_WPS_UPNP */
2606 return wps_process_wsc_msg(wps, msg);
2608 return wps_process_wsc_ack(wps, msg);
2610 return wps_process_wsc_nack(wps, msg);
2612 ret = wps_process_wsc_done(wps, msg);
2613 if (ret == WPS_FAILURE) {
2614 wps->state = SEND_WSC_NACK;
2615 wps_fail_event(wps->wps, WPS_WSC_DONE);
2619 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2625 int wps_registrar_update_ie(struct wps_registrar *reg)
2627 return wps_set_ie(reg);
2631 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2634 struct wps_registrar *reg = eloop_ctx;
2636 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar timed out - "
2637 "unselect Registrar");
2638 reg->selected_registrar = 0;
2640 reg->sel_reg_dev_password_id_override = -1;
2641 reg->sel_reg_config_methods_override = -1;
2647 * wps_registrar_set_selected_registrar - Notification of SetSelectedRegistrar
2648 * @reg: Registrar data from wps_registrar_init()
2649 * @msg: Received message from SetSelectedRegistrar
2650 * Returns: 0 on success, -1 on failure
2652 * This function is called when an AP receives a SetSelectedRegistrar UPnP
2655 int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
2656 const struct wpabuf *msg)
2658 struct wps_parse_attr attr;
2660 wpa_hexdump_buf(MSG_MSGDUMP, "WPS: SetSelectedRegistrar attributes",
2663 if (wps_parse_msg(msg, &attr) < 0)
2665 if (!wps_version_supported(attr.version)) {
2666 wpa_printf(MSG_DEBUG, "WPS: Unsupported SetSelectedRegistrar "
2667 "version 0x%x", attr.version ? *attr.version : 0);
2671 if (attr.selected_registrar == NULL ||
2672 *attr.selected_registrar == 0) {
2673 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar: Disable "
2674 "Selected Registrar");
2675 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg,
2677 wps_registrar_set_selected_timeout(reg, NULL);
2681 reg->selected_registrar = 1;
2682 reg->sel_reg_dev_password_id_override = attr.dev_password_id ?
2683 WPA_GET_BE16(attr.dev_password_id) : DEV_PW_DEFAULT;
2684 reg->sel_reg_config_methods_override = attr.sel_reg_config_methods ?
2685 WPA_GET_BE16(attr.sel_reg_config_methods) : -1;
2688 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
2689 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
2690 wps_registrar_set_selected_timeout,
2696 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
2697 char *buf, size_t buflen)
2699 struct wps_registrar_device *d;
2703 d = wps_device_get(reg, addr);
2706 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
2709 ret = os_snprintf(buf + len, buflen - len,
2711 "wpsPrimaryDeviceType=%u-%08X-%u\n"
2712 "wpsDeviceName=%s\n"
2713 "wpsManufacturer=%s\n"
2715 "wpsModelNumber=%s\n"
2716 "wpsSerialNumber=%s\n",
2718 d->dev.categ, d->dev.oui, d->dev.sub_categ,
2719 d->dev.device_name ? d->dev.device_name : "",
2720 d->dev.manufacturer ? d->dev.manufacturer : "",
2721 d->dev.model_name ? d->dev.model_name : "",
2722 d->dev.model_number ? d->dev.model_number : "",
2723 d->dev.serial_number ? d->dev.serial_number : "");
2724 if (ret < 0 || (size_t) ret >= buflen - len)