2 * Wi-Fi Protected Setup - Registrar
3 * Copyright (c) 2008, 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"
26 #define WPS_WORKAROUNDS
29 struct wps_uuid_pin *next;
30 u8 uuid[WPS_UUID_LEN];
34 #define PIN_LOCKED BIT(0)
35 #define PIN_EXPIRES BIT(1)
37 struct os_time expiration;
41 static void wps_free_pin(struct wps_uuid_pin *pin)
48 static void wps_free_pins(struct wps_uuid_pin *pins)
50 struct wps_uuid_pin *pin, *prev;
61 struct wps_pbc_session {
62 struct wps_pbc_session *next;
64 u8 uuid_e[WPS_UUID_LEN];
65 struct os_time timestamp;
69 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
71 struct wps_pbc_session *prev;
81 struct wps_registrar {
82 struct wps_context *wps;
85 int selected_registrar;
87 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
89 int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
90 const u8 *probe_resp_ie, size_t probe_resp_ie_len);
91 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
92 const struct wps_device_data *dev);
93 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
97 struct wps_uuid_pin *pins;
98 struct wps_pbc_session *pbc_sessions;
101 struct wpabuf *extra_cred;
102 int disable_auto_conf;
103 int sel_reg_dev_password_id_override;
104 int sel_reg_config_methods_override;
107 int force_pbc_overlap;
111 static int wps_set_ie(struct wps_registrar *reg);
112 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
113 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
117 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
118 const u8 *addr, const u8 *uuid_e)
120 struct wps_pbc_session *pbc, *prev = NULL;
125 pbc = reg->pbc_sessions;
127 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
128 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
130 prev->next = pbc->next;
132 reg->pbc_sessions = pbc->next;
140 pbc = os_zalloc(sizeof(*pbc));
143 os_memcpy(pbc->addr, addr, ETH_ALEN);
145 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
148 pbc->next = reg->pbc_sessions;
149 reg->pbc_sessions = pbc;
150 pbc->timestamp = now;
152 /* remove entries that have timed out */
157 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
159 wps_free_pbc_sessions(pbc);
168 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
169 const u8 *addr, const u8 *uuid_e)
171 struct wps_pbc_session *pbc, *prev = NULL;
173 pbc = reg->pbc_sessions;
175 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
176 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
178 prev->next = pbc->next;
180 reg->pbc_sessions = pbc->next;
190 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
191 const u8 *addr, const u8 *uuid_e)
194 struct wps_pbc_session *pbc;
199 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
200 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
202 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
204 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
211 return count > 1 ? 1 : 0;
215 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
217 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
219 wpabuf_put_be16(msg, ATTR_WPS_STATE);
220 wpabuf_put_be16(msg, 1);
221 wpabuf_put_u8(msg, wps->wps_state);
226 #ifdef CONFIG_WPS_UPNP
227 static void wps_registrar_free_pending_m2(struct wps_context *wps)
229 struct upnp_pending_message *p, *p2, *prev = NULL;
232 if (p->type == WPS_M2 || p->type == WPS_M2D) {
234 wps->upnp_msgs = p->next;
236 prev->next = p->next;
237 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
240 wpabuf_free(p2->msg);
248 #endif /* CONFIG_WPS_UPNP */
251 static int wps_build_ap_setup_locked(struct wps_context *wps,
254 if (wps->ap_setup_locked) {
255 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
256 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
257 wpabuf_put_be16(msg, 1);
258 wpabuf_put_u8(msg, 1);
264 static int wps_build_selected_registrar(struct wps_registrar *reg,
267 if (!reg->selected_registrar)
269 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
270 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
271 wpabuf_put_be16(msg, 1);
272 wpabuf_put_u8(msg, 1);
277 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
280 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
281 if (!reg->selected_registrar)
283 if (reg->sel_reg_dev_password_id_override >= 0)
284 id = reg->sel_reg_dev_password_id_override;
285 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
286 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
287 wpabuf_put_be16(msg, 2);
288 wpabuf_put_be16(msg, id);
293 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
297 if (!reg->selected_registrar)
299 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
301 methods |= WPS_CONFIG_PUSHBUTTON;
302 if (reg->sel_reg_config_methods_override >= 0)
303 methods = reg->sel_reg_config_methods_override;
304 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
306 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
307 wpabuf_put_be16(msg, 2);
308 wpabuf_put_be16(msg, methods);
313 static int wps_build_probe_config_methods(struct wps_registrar *reg,
318 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
319 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
320 wpabuf_put_be16(msg, 2);
321 wpabuf_put_be16(msg, methods);
326 static int wps_build_config_methods_r(struct wps_registrar *reg,
330 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
332 methods |= WPS_CONFIG_PUSHBUTTON;
333 return wps_build_config_methods(msg, methods);
337 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
339 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
340 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
341 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
342 wpabuf_put_be16(msg, 1);
343 wpabuf_put_u8(msg, resp);
349 * wps_registrar_init - Initialize WPS Registrar data
350 * @wps: Pointer to longterm WPS context
351 * @cfg: Registrar configuration
352 * Returns: Pointer to allocated Registrar data or %NULL on failure
354 * This function is used to initialize WPS Registrar functionality. It can be
355 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
356 * runs (e.g., when run as an internal Registrar in an AP). Caller is
357 * responsible for freeing the returned data with wps_registrar_deinit() when
358 * Registrar functionality is not needed anymore.
360 struct wps_registrar *
361 wps_registrar_init(struct wps_context *wps,
362 const struct wps_registrar_config *cfg)
364 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
369 reg->new_psk_cb = cfg->new_psk_cb;
370 reg->set_ie_cb = cfg->set_ie_cb;
371 reg->pin_needed_cb = cfg->pin_needed_cb;
372 reg->reg_success_cb = cfg->reg_success_cb;
373 reg->cb_ctx = cfg->cb_ctx;
374 reg->skip_cred_build = cfg->skip_cred_build;
375 if (cfg->extra_cred) {
376 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
377 cfg->extra_cred_len);
378 if (reg->extra_cred == NULL) {
383 reg->disable_auto_conf = cfg->disable_auto_conf;
384 reg->sel_reg_dev_password_id_override = -1;
385 reg->sel_reg_config_methods_override = -1;
386 reg->static_wep_only = cfg->static_wep_only;
388 if (wps_set_ie(reg)) {
389 wps_registrar_deinit(reg);
398 * wps_registrar_deinit - Deinitialize WPS Registrar data
399 * @reg: Registrar data from wps_registrar_init()
401 void wps_registrar_deinit(struct wps_registrar *reg)
405 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
406 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
407 wps_free_pins(reg->pins);
408 wps_free_pbc_sessions(reg->pbc_sessions);
409 wpabuf_free(reg->extra_cred);
415 * wps_registrar_add_pin - Configure a new PIN for Registrar
416 * @reg: Registrar data from wps_registrar_init()
417 * @uuid: UUID-E or %NULL for wildcard (any UUID)
418 * @pin: PIN (Device Password)
419 * @pin_len: Length of pin in octets
420 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
421 * Returns: 0 on success, -1 on failure
423 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
424 const u8 *pin, size_t pin_len, int timeout)
426 struct wps_uuid_pin *p;
428 p = os_zalloc(sizeof(*p));
432 p->wildcard_uuid = 1;
434 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
435 p->pin = os_malloc(pin_len);
436 if (p->pin == NULL) {
440 os_memcpy(p->pin, pin, pin_len);
441 p->pin_len = pin_len;
444 p->flags |= PIN_EXPIRES;
445 os_get_time(&p->expiration);
446 p->expiration.sec += timeout;
452 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
454 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
455 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
456 reg->selected_registrar = 1;
459 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
460 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
461 wps_registrar_set_selected_timeout,
468 static void wps_registrar_expire_pins(struct wps_registrar *reg)
470 struct wps_uuid_pin *pin, *prev, *del;
477 if ((pin->flags & PIN_EXPIRES) &&
478 os_time_before(&pin->expiration, &now)) {
480 reg->pins = pin->next;
482 prev->next = pin->next;
485 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
486 del->uuid, WPS_UUID_LEN);
497 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
498 * @reg: Registrar data from wps_registrar_init()
500 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
502 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
504 struct wps_uuid_pin *pin, *prev;
509 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
511 reg->pins = pin->next;
513 prev->next = pin->next;
514 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
515 pin->uuid, WPS_UUID_LEN);
527 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
528 const u8 *uuid, size_t *pin_len)
530 struct wps_uuid_pin *pin;
532 wps_registrar_expire_pins(reg);
536 if (!pin->wildcard_uuid &&
537 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
543 /* Check for wildcard UUIDs since none of the UUID-specific
547 if (pin->wildcard_uuid == 1) {
548 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
549 "PIN. Assigned it for this UUID-E");
550 pin->wildcard_uuid = 2;
551 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
562 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
563 * that could otherwise avoid PIN invalidations.
565 if (pin->flags & PIN_LOCKED) {
566 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
567 "allow concurrent re-use");
570 *pin_len = pin->pin_len;
571 pin->flags |= PIN_LOCKED;
577 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
578 * @reg: Registrar data from wps_registrar_init()
580 * Returns: 0 on success, -1 on failure
582 * PINs are locked to enforce only one concurrent use. This function unlocks a
583 * PIN to allow it to be used again. If the specified PIN was configured using
584 * a wildcard UUID, it will be removed instead of allowing multiple uses.
586 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
588 struct wps_uuid_pin *pin;
592 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
593 if (pin->wildcard_uuid == 2) {
594 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
596 return wps_registrar_invalidate_pin(reg, uuid);
598 pin->flags &= ~PIN_LOCKED;
608 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
610 reg->selected_registrar = 0;
616 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
618 struct wps_registrar *reg = eloop_ctx;
620 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
621 wps_pbc_timeout_event(reg->wps);
622 wps_registrar_stop_pbc(reg);
627 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
628 * @reg: Registrar data from wps_registrar_init()
629 * Returns: 0 on success, -1 on failure
631 * This function is called on an AP when a push button is pushed to activate
632 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
633 * or when a PBC registration is completed.
635 int wps_registrar_button_pushed(struct wps_registrar *reg)
637 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
638 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
640 wps_pbc_overlap_event(reg->wps);
643 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
644 reg->force_pbc_overlap = 0;
645 reg->selected_registrar = 1;
649 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
650 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
656 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
658 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
659 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
660 wps_registrar_stop_pbc(reg);
663 static void wps_registrar_pin_completed(struct wps_registrar *reg)
665 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
666 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
667 reg->selected_registrar = 0;
673 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
674 * @reg: Registrar data from wps_registrar_init()
675 * @addr: MAC address of the Probe Request sender
676 * @wps_data: WPS IE contents
678 * This function is called on an AP when a Probe Request with WPS IE is
679 * received. This is used to track PBC mode use and to detect possible overlap
680 * situation with other WPS APs.
682 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
683 const struct wpabuf *wps_data)
685 struct wps_parse_attr attr;
688 wpa_hexdump_buf(MSG_MSGDUMP,
689 "WPS: Probe Request with WPS data received",
692 if (wps_parse_msg(wps_data, &attr) < 0)
694 if (!wps_version_supported(attr.version)) {
695 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
696 "version 0x%x", attr.version ? *attr.version : 0);
700 if (attr.config_methods == NULL) {
701 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
706 methods = WPA_GET_BE16(attr.config_methods);
707 if (!(methods & WPS_CONFIG_PUSHBUTTON))
708 return; /* Not PBC */
710 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
711 MACSTR, MAC2STR(addr));
712 if (attr.uuid_e == NULL) {
713 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
718 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
719 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
720 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
721 reg->force_pbc_overlap = 1;
722 wps_pbc_overlap_event(reg->wps);
727 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
728 const u8 *psk, size_t psk_len)
730 if (reg->new_psk_cb == NULL)
733 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
737 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
738 const struct wps_device_data *dev)
740 if (reg->pin_needed_cb == NULL)
743 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
747 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
750 if (reg->reg_success_cb == NULL)
753 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
757 static int wps_cb_set_ie(struct wps_registrar *reg,
758 const struct wpabuf *beacon_ie,
759 const struct wpabuf *probe_resp_ie)
761 if (reg->set_ie_cb == NULL)
764 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
765 wpabuf_len(beacon_ie),
766 wpabuf_head(probe_resp_ie),
767 wpabuf_len(probe_resp_ie));
771 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
772 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
777 ie = wpabuf_alloc(wpabuf_len(data) + 100);
783 pos = wpabuf_head(data);
784 end = pos + wpabuf_len(data);
787 size_t frag_len = end - pos;
790 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
791 wpabuf_put_u8(ie, 4 + frag_len);
792 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
793 wpabuf_put_data(ie, pos, frag_len);
803 static int wps_set_ie(struct wps_registrar *reg)
805 struct wpabuf *beacon;
806 struct wpabuf *probe;
809 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
811 beacon = wpabuf_alloc(300);
814 probe = wpabuf_alloc(400);
820 if (wps_build_version(beacon) ||
821 wps_build_wps_state(reg->wps, beacon) ||
822 wps_build_ap_setup_locked(reg->wps, beacon) ||
823 wps_build_selected_registrar(reg, beacon) ||
824 wps_build_sel_reg_dev_password_id(reg, beacon) ||
825 wps_build_sel_reg_config_methods(reg, beacon) ||
826 wps_build_version(probe) ||
827 wps_build_wps_state(reg->wps, probe) ||
828 wps_build_ap_setup_locked(reg->wps, probe) ||
829 wps_build_selected_registrar(reg, probe) ||
830 wps_build_sel_reg_dev_password_id(reg, probe) ||
831 wps_build_sel_reg_config_methods(reg, probe) ||
832 wps_build_resp_type(reg, probe) ||
833 wps_build_uuid_e(probe, reg->wps->uuid) ||
834 wps_build_device_attrs(®->wps->dev, probe) ||
835 wps_build_probe_config_methods(reg, probe) ||
836 wps_build_rf_bands(®->wps->dev, probe)) {
842 beacon = wps_ie_encapsulate(beacon);
843 probe = wps_ie_encapsulate(probe);
845 if (!beacon || !probe) {
851 if (reg->static_wep_only) {
853 * Windows XP and Vista clients can get confused about
854 * EAP-Identity/Request when they probe the network with
855 * EAPOL-Start. In such a case, they may assume the network is
856 * using IEEE 802.1X and prompt user for a certificate while
857 * the correct (non-WPS) behavior would be to ask for the
858 * static WEP key. As a workaround, use Microsoft Provisioning
859 * IE to advertise that legacy 802.1X is not supported.
861 const u8 ms_wps[7] = {
862 WLAN_EID_VENDOR_SPECIFIC, 5,
863 /* Microsoft Provisioning IE (00:50:f2:5) */
865 0x00 /* no legacy 802.1X or MS WPS */
867 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
868 "into Beacon/Probe Response frames");
869 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
870 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
873 ret = wps_cb_set_ie(reg, beacon, probe);
881 static int wps_get_dev_password(struct wps_data *wps)
886 os_free(wps->dev_password);
887 wps->dev_password = NULL;
890 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
891 pin = (const u8 *) "00000000";
894 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
898 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
900 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
905 wps->dev_password = os_malloc(pin_len);
906 if (wps->dev_password == NULL)
908 os_memcpy(wps->dev_password, pin, pin_len);
909 wps->dev_password_len = pin_len;
915 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
917 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
918 wpabuf_put_be16(msg, ATTR_UUID_R);
919 wpabuf_put_be16(msg, WPS_UUID_LEN);
920 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
925 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
931 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
933 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
934 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
935 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
937 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
938 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
939 "R-Hash derivation");
943 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
944 wpabuf_put_be16(msg, ATTR_R_HASH1);
945 wpabuf_put_be16(msg, SHA256_MAC_LEN);
946 hash = wpabuf_put(msg, SHA256_MAC_LEN);
947 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
948 addr[0] = wps->snonce;
949 len[0] = WPS_SECRET_NONCE_LEN;
951 len[1] = WPS_PSK_LEN;
952 addr[2] = wpabuf_head(wps->dh_pubkey_e);
953 len[2] = wpabuf_len(wps->dh_pubkey_e);
954 addr[3] = wpabuf_head(wps->dh_pubkey_r);
955 len[3] = wpabuf_len(wps->dh_pubkey_r);
956 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
957 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
959 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
960 wpabuf_put_be16(msg, ATTR_R_HASH2);
961 wpabuf_put_be16(msg, SHA256_MAC_LEN);
962 hash = wpabuf_put(msg, SHA256_MAC_LEN);
963 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
964 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
966 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
967 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
973 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
975 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
976 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
977 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
978 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
983 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
985 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
986 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
987 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
988 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
989 WPS_SECRET_NONCE_LEN);
994 static int wps_build_cred_network_idx(struct wpabuf *msg,
995 struct wps_credential *cred)
997 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
998 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
999 wpabuf_put_be16(msg, 1);
1000 wpabuf_put_u8(msg, 1);
1005 static int wps_build_cred_ssid(struct wpabuf *msg,
1006 struct wps_credential *cred)
1008 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1009 wpabuf_put_be16(msg, ATTR_SSID);
1010 wpabuf_put_be16(msg, cred->ssid_len);
1011 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1016 static int wps_build_cred_auth_type(struct wpabuf *msg,
1017 struct wps_credential *cred)
1019 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1021 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1022 wpabuf_put_be16(msg, 2);
1023 wpabuf_put_be16(msg, cred->auth_type);
1028 static int wps_build_cred_encr_type(struct wpabuf *msg,
1029 struct wps_credential *cred)
1031 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1033 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1034 wpabuf_put_be16(msg, 2);
1035 wpabuf_put_be16(msg, cred->encr_type);
1040 static int wps_build_cred_network_key(struct wpabuf *msg,
1041 struct wps_credential *cred)
1043 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
1044 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1045 wpabuf_put_be16(msg, cred->key_len);
1046 wpabuf_put_data(msg, cred->key, cred->key_len);
1051 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1052 struct wps_credential *cred)
1054 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1055 MAC2STR(cred->mac_addr));
1056 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1057 wpabuf_put_be16(msg, ETH_ALEN);
1058 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1063 static int wps_build_credential(struct wpabuf *msg,
1064 struct wps_credential *cred)
1066 if (wps_build_cred_network_idx(msg, cred) ||
1067 wps_build_cred_ssid(msg, cred) ||
1068 wps_build_cred_auth_type(msg, cred) ||
1069 wps_build_cred_encr_type(msg, cred) ||
1070 wps_build_cred_network_key(msg, cred) ||
1071 wps_build_cred_mac_addr(msg, cred))
1077 static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1079 struct wpabuf *cred;
1081 if (wps->wps->registrar->skip_cred_build)
1082 goto skip_cred_build;
1084 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1085 os_memset(&wps->cred, 0, sizeof(wps->cred));
1087 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1088 wps->cred.ssid_len = wps->wps->ssid_len;
1090 /* Select the best authentication and encryption type */
1091 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1092 wps->auth_type = WPS_AUTH_WPA2PSK;
1093 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1094 wps->auth_type = WPS_AUTH_WPAPSK;
1095 else if (wps->auth_type & WPS_AUTH_OPEN)
1096 wps->auth_type = WPS_AUTH_OPEN;
1097 else if (wps->auth_type & WPS_AUTH_SHARED)
1098 wps->auth_type = WPS_AUTH_SHARED;
1100 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1104 wps->cred.auth_type = wps->auth_type;
1106 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1107 wps->auth_type == WPS_AUTH_WPAPSK) {
1108 if (wps->encr_type & WPS_ENCR_AES)
1109 wps->encr_type = WPS_ENCR_AES;
1110 else if (wps->encr_type & WPS_ENCR_TKIP)
1111 wps->encr_type = WPS_ENCR_TKIP;
1113 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1114 "type for WPA/WPA2");
1118 if (wps->encr_type & WPS_ENCR_WEP)
1119 wps->encr_type = WPS_ENCR_WEP;
1120 else if (wps->encr_type & WPS_ENCR_NONE)
1121 wps->encr_type = WPS_ENCR_NONE;
1123 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1124 "type for non-WPA/WPA2 mode");
1128 wps->cred.encr_type = wps->encr_type;
1130 * Set MAC address in the Credential to be the Enrollee's MAC address
1132 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1134 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1135 !wps->wps->registrar->disable_auto_conf) {
1137 /* Generate a random passphrase */
1138 if (os_get_random(r, sizeof(r)) < 0)
1140 os_free(wps->new_psk);
1141 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1142 if (wps->new_psk == NULL)
1144 wps->new_psk_len--; /* remove newline */
1145 while (wps->new_psk_len &&
1146 wps->new_psk[wps->new_psk_len - 1] == '=')
1148 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1149 wps->new_psk, wps->new_psk_len);
1150 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1151 wps->cred.key_len = wps->new_psk_len;
1152 } else if (wps->wps->network_key) {
1153 os_memcpy(wps->cred.key, wps->wps->network_key,
1154 wps->wps->network_key_len);
1155 wps->cred.key_len = wps->wps->network_key_len;
1156 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1158 /* Generate a random per-device PSK */
1159 os_free(wps->new_psk);
1160 wps->new_psk_len = 32;
1161 wps->new_psk = os_malloc(wps->new_psk_len);
1162 if (wps->new_psk == NULL)
1164 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1165 os_free(wps->new_psk);
1166 wps->new_psk = NULL;
1169 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1170 wps->new_psk, wps->new_psk_len);
1171 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1173 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1174 wps->cred.key_len = wps->new_psk_len * 2;
1177 cred = wpabuf_alloc(200);
1181 if (wps_build_credential(cred, &wps->cred)) {
1186 wpabuf_put_be16(msg, ATTR_CRED);
1187 wpabuf_put_be16(msg, wpabuf_len(cred));
1188 wpabuf_put_buf(msg, cred);
1192 if (wps->wps->registrar->extra_cred) {
1193 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1194 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1201 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1203 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1205 if (wps_build_credential(msg, &wps->cred))
1212 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1216 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1218 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1219 wps->nonce_r, WPS_NONCE_LEN);
1220 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1222 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1223 msg = wpabuf_alloc(1000);
1227 if (wps_build_version(msg) ||
1228 wps_build_msg_type(msg, WPS_M2) ||
1229 wps_build_enrollee_nonce(wps, msg) ||
1230 wps_build_registrar_nonce(wps, msg) ||
1231 wps_build_uuid_r(wps, msg) ||
1232 wps_build_public_key(wps, msg) ||
1233 wps_derive_keys(wps) ||
1234 wps_build_auth_type_flags(wps, msg) ||
1235 wps_build_encr_type_flags(wps, msg) ||
1236 wps_build_conn_type_flags(wps, msg) ||
1237 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1238 wps_build_device_attrs(&wps->wps->dev, msg) ||
1239 wps_build_rf_bands(&wps->wps->dev, msg) ||
1240 wps_build_assoc_state(wps, msg) ||
1241 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1242 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1243 wps_build_os_version(&wps->wps->dev, msg) ||
1244 wps_build_authenticator(wps, msg)) {
1249 wps->state = RECV_M3;
1254 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1257 u16 err = wps->config_error;
1259 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1260 msg = wpabuf_alloc(1000);
1264 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1265 err == WPS_CFG_NO_ERROR)
1266 err = WPS_CFG_SETUP_LOCKED;
1268 if (wps_build_version(msg) ||
1269 wps_build_msg_type(msg, WPS_M2D) ||
1270 wps_build_enrollee_nonce(wps, msg) ||
1271 wps_build_registrar_nonce(wps, msg) ||
1272 wps_build_uuid_r(wps, msg) ||
1273 wps_build_auth_type_flags(wps, msg) ||
1274 wps_build_encr_type_flags(wps, msg) ||
1275 wps_build_conn_type_flags(wps, msg) ||
1276 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1277 wps_build_device_attrs(&wps->wps->dev, msg) ||
1278 wps_build_rf_bands(&wps->wps->dev, msg) ||
1279 wps_build_assoc_state(wps, msg) ||
1280 wps_build_config_error(msg, err) ||
1281 wps_build_os_version(&wps->wps->dev, msg)) {
1286 wps->state = RECV_M2D_ACK;
1291 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1293 struct wpabuf *msg, *plain;
1295 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1297 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1299 plain = wpabuf_alloc(200);
1303 msg = wpabuf_alloc(1000);
1309 if (wps_build_version(msg) ||
1310 wps_build_msg_type(msg, WPS_M4) ||
1311 wps_build_enrollee_nonce(wps, msg) ||
1312 wps_build_r_hash(wps, msg) ||
1313 wps_build_r_snonce1(wps, plain) ||
1314 wps_build_key_wrap_auth(wps, plain) ||
1315 wps_build_encr_settings(wps, msg, plain) ||
1316 wps_build_authenticator(wps, msg)) {
1323 wps->state = RECV_M5;
1328 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1330 struct wpabuf *msg, *plain;
1332 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1334 plain = wpabuf_alloc(200);
1338 msg = wpabuf_alloc(1000);
1344 if (wps_build_version(msg) ||
1345 wps_build_msg_type(msg, WPS_M6) ||
1346 wps_build_enrollee_nonce(wps, msg) ||
1347 wps_build_r_snonce2(wps, plain) ||
1348 wps_build_key_wrap_auth(wps, plain) ||
1349 wps_build_encr_settings(wps, msg, plain) ||
1350 wps_build_authenticator(wps, msg)) {
1357 wps->wps_pin_revealed = 1;
1358 wps->state = RECV_M7;
1363 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1365 struct wpabuf *msg, *plain;
1367 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1369 plain = wpabuf_alloc(500);
1373 msg = wpabuf_alloc(1000);
1379 if (wps_build_version(msg) ||
1380 wps_build_msg_type(msg, WPS_M8) ||
1381 wps_build_enrollee_nonce(wps, msg) ||
1382 (wps->wps->ap && wps_build_cred(wps, plain)) ||
1383 (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1384 wps_build_key_wrap_auth(wps, plain) ||
1385 wps_build_encr_settings(wps, msg, plain) ||
1386 wps_build_authenticator(wps, msg)) {
1393 wps->state = RECV_DONE;
1398 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1402 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1404 msg = wpabuf_alloc(1000);
1408 if (wps_build_version(msg) ||
1409 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1410 wps_build_enrollee_nonce(wps, msg) ||
1411 wps_build_registrar_nonce(wps, msg)) {
1420 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1424 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1426 msg = wpabuf_alloc(1000);
1430 if (wps_build_version(msg) ||
1431 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1432 wps_build_enrollee_nonce(wps, msg) ||
1433 wps_build_registrar_nonce(wps, msg) ||
1434 wps_build_config_error(msg, wps->config_error)) {
1443 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1444 enum wsc_op_code *op_code)
1448 #ifdef CONFIG_WPS_UPNP
1449 if (wps->wps->wps_upnp) {
1450 struct upnp_pending_message *p, *prev = NULL;
1451 if (wps->ext_reg > 1)
1452 wps_registrar_free_pending_m2(wps->wps);
1453 p = wps->wps->upnp_msgs;
1454 /* TODO: check pending message MAC address */
1455 while (p && p->next) {
1460 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1465 wps->wps->upnp_msgs = NULL;
1472 *op_code = WSC_NACK;
1479 if (wps->ext_reg == 0)
1485 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1486 "pending message available");
1489 #endif /* CONFIG_WPS_UPNP */
1491 switch (wps->state) {
1493 if (wps_get_dev_password(wps) < 0)
1494 msg = wps_build_m2d(wps);
1496 msg = wps_build_m2(wps);
1500 msg = wps_build_m2d(wps);
1504 msg = wps_build_m4(wps);
1508 msg = wps_build_m6(wps);
1512 msg = wps_build_m8(wps);
1516 msg = wps_build_wsc_ack(wps);
1520 msg = wps_build_wsc_nack(wps);
1521 *op_code = WSC_NACK;
1524 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1525 "a message", wps->state);
1530 if (*op_code == WSC_MSG && msg) {
1531 /* Save a copy of the last message for Authenticator derivation
1533 wpabuf_free(wps->last_msg);
1534 wps->last_msg = wpabuf_dup(msg);
1541 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1543 if (e_nonce == NULL) {
1544 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1548 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1549 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1550 wps->nonce_e, WPS_NONCE_LEN);
1556 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1558 if (r_nonce == NULL) {
1559 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1563 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1564 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1572 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1574 if (uuid_e == NULL) {
1575 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1579 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1580 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1586 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1588 if (pw_id == NULL) {
1589 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1593 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1594 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1600 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1602 if (e_hash1 == NULL) {
1603 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1607 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1608 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1614 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1616 if (e_hash2 == NULL) {
1617 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1621 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1622 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1628 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1630 u8 hash[SHA256_MAC_LEN];
1634 if (e_snonce1 == NULL) {
1635 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1639 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1640 WPS_SECRET_NONCE_LEN);
1642 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1643 addr[0] = e_snonce1;
1644 len[0] = WPS_SECRET_NONCE_LEN;
1645 addr[1] = wps->psk1;
1646 len[1] = WPS_PSK_LEN;
1647 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1648 len[2] = wpabuf_len(wps->dh_pubkey_e);
1649 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1650 len[3] = wpabuf_len(wps->dh_pubkey_r);
1651 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1653 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1654 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1655 "not match with the pre-committed value");
1656 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1657 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1661 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1662 "half of the device password");
1668 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1670 u8 hash[SHA256_MAC_LEN];
1674 if (e_snonce2 == NULL) {
1675 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1679 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1680 WPS_SECRET_NONCE_LEN);
1682 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1683 addr[0] = e_snonce2;
1684 len[0] = WPS_SECRET_NONCE_LEN;
1685 addr[1] = wps->psk2;
1686 len[1] = WPS_PSK_LEN;
1687 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1688 len[2] = wpabuf_len(wps->dh_pubkey_e);
1689 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1690 len[3] = wpabuf_len(wps->dh_pubkey_r);
1691 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1693 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1694 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1695 "not match with the pre-committed value");
1696 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1697 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1698 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1702 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1703 "half of the device password");
1704 wps->wps_pin_revealed = 0;
1705 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1711 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1713 if (mac_addr == NULL) {
1714 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1718 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1720 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1721 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1727 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1730 if (pk == NULL || pk_len == 0) {
1731 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1735 wpabuf_free(wps->dh_pubkey_e);
1736 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1737 if (wps->dh_pubkey_e == NULL)
1744 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1749 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1754 auth_types = WPA_GET_BE16(auth);
1756 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1758 wps->auth_type = wps->wps->auth_types & auth_types;
1759 if (wps->auth_type == 0) {
1760 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1761 "authentication types (own 0x%x Enrollee 0x%x)",
1762 wps->wps->auth_types, auth_types);
1763 #ifdef WPS_WORKAROUNDS
1765 * Some deployed implementations seem to advertise incorrect
1766 * information in this attribute. For example, Linksys WRT350N
1767 * seems to have a byteorder bug that breaks this negotiation.
1768 * In order to interoperate with existing implementations,
1769 * assume that the Enrollee supports everything we do.
1771 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1772 "does not advertise supported authentication types "
1774 wps->auth_type = wps->wps->auth_types;
1775 #else /* WPS_WORKAROUNDS */
1777 #endif /* WPS_WORKAROUNDS */
1784 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1789 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1794 encr_types = WPA_GET_BE16(encr);
1796 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1798 wps->encr_type = wps->wps->encr_types & encr_types;
1799 if (wps->encr_type == 0) {
1800 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1801 "encryption types (own 0x%x Enrollee 0x%x)",
1802 wps->wps->encr_types, encr_types);
1803 #ifdef WPS_WORKAROUNDS
1805 * Some deployed implementations seem to advertise incorrect
1806 * information in this attribute. For example, Linksys WRT350N
1807 * seems to have a byteorder bug that breaks this negotiation.
1808 * In order to interoperate with existing implementations,
1809 * assume that the Enrollee supports everything we do.
1811 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1812 "does not advertise supported encryption types "
1814 wps->encr_type = wps->wps->encr_types;
1815 #else /* WPS_WORKAROUNDS */
1817 #endif /* WPS_WORKAROUNDS */
1824 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1827 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1832 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1839 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1843 if (methods == NULL) {
1844 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1848 m = WPA_GET_BE16(methods);
1850 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1856 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1858 if (state == NULL) {
1859 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1864 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1871 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1875 if (assoc == NULL) {
1876 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1880 a = WPA_GET_BE16(assoc);
1881 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1887 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1892 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1896 e = WPA_GET_BE16(err);
1897 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1903 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1904 struct wps_parse_attr *attr)
1906 wpa_printf(MSG_DEBUG, "WPS: Received M1");
1908 if (wps->state != RECV_M1) {
1909 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1910 "receiving M1", wps->state);
1914 if (wps_process_uuid_e(wps, attr->uuid_e) ||
1915 wps_process_mac_addr(wps, attr->mac_addr) ||
1916 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1917 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1918 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1919 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1920 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1921 wps_process_config_methods(wps, attr->config_methods) ||
1922 wps_process_wps_state(wps, attr->wps_state) ||
1923 wps_process_device_attrs(&wps->peer_dev, attr) ||
1924 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1925 wps_process_assoc_state(wps, attr->assoc_state) ||
1926 wps_process_dev_password_id(wps, attr->dev_password_id) ||
1927 wps_process_config_error(wps, attr->config_error) ||
1928 wps_process_os_version(&wps->peer_dev, attr->os_version))
1931 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
1932 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1933 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1934 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1935 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
1936 !wps->wps->registrar->pbc)) {
1937 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1939 wps->state = SEND_M2D;
1940 return WPS_CONTINUE;
1943 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1944 if (wps->wps->registrar->force_pbc_overlap ||
1945 wps_registrar_pbc_overlap(wps->wps->registrar,
1946 wps->mac_addr_e, wps->uuid_e)) {
1947 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1949 wps->state = SEND_M2D;
1950 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
1951 wps_pbc_overlap_event(wps->wps);
1952 wps->wps->registrar->force_pbc_overlap = 1;
1953 return WPS_CONTINUE;
1955 wps_registrar_add_pbc_session(wps->wps->registrar,
1956 wps->mac_addr_e, wps->uuid_e);
1960 wps->state = SEND_M2;
1961 return WPS_CONTINUE;
1965 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1966 const struct wpabuf *msg,
1967 struct wps_parse_attr *attr)
1969 wpa_printf(MSG_DEBUG, "WPS: Received M3");
1971 if (wps->state != RECV_M3) {
1972 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1973 "receiving M3", wps->state);
1974 wps->state = SEND_WSC_NACK;
1975 return WPS_CONTINUE;
1978 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
1979 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
1981 wps->state = SEND_WSC_NACK;
1982 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
1983 return WPS_CONTINUE;
1986 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1987 wps_process_authenticator(wps, attr->authenticator, msg) ||
1988 wps_process_e_hash1(wps, attr->e_hash1) ||
1989 wps_process_e_hash2(wps, attr->e_hash2)) {
1990 wps->state = SEND_WSC_NACK;
1991 return WPS_CONTINUE;
1994 wps->state = SEND_M4;
1995 return WPS_CONTINUE;
1999 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2000 const struct wpabuf *msg,
2001 struct wps_parse_attr *attr)
2003 struct wpabuf *decrypted;
2004 struct wps_parse_attr eattr;
2006 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2008 if (wps->state != RECV_M5) {
2009 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2010 "receiving M5", wps->state);
2011 wps->state = SEND_WSC_NACK;
2012 return WPS_CONTINUE;
2015 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2016 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2018 wps->state = SEND_WSC_NACK;
2019 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2020 return WPS_CONTINUE;
2023 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2024 wps_process_authenticator(wps, attr->authenticator, msg)) {
2025 wps->state = SEND_WSC_NACK;
2026 return WPS_CONTINUE;
2029 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2030 attr->encr_settings_len);
2031 if (decrypted == NULL) {
2032 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2033 "Settings attribute");
2034 wps->state = SEND_WSC_NACK;
2035 return WPS_CONTINUE;
2038 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2040 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2041 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2042 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2043 wpabuf_free(decrypted);
2044 wps->state = SEND_WSC_NACK;
2045 return WPS_CONTINUE;
2047 wpabuf_free(decrypted);
2049 wps->state = SEND_M6;
2050 return WPS_CONTINUE;
2054 static void wps_sta_cred_cb(struct wps_data *wps)
2057 * Update credential to only include a single authentication and
2058 * encryption type in case the AP configuration includes more than one
2061 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2062 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2063 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2064 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2065 if (wps->cred.encr_type & WPS_ENCR_AES)
2066 wps->cred.encr_type = WPS_ENCR_AES;
2067 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2068 wps->cred.encr_type = WPS_ENCR_TKIP;
2069 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2070 "AP configuration");
2071 if (wps->wps->cred_cb)
2072 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2076 static int wps_process_ap_settings_r(struct wps_data *wps,
2077 struct wps_parse_attr *attr)
2082 /* AP Settings Attributes in M7 when Enrollee is an AP */
2083 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2086 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2090 * TODO: Provide access to AP settings and allow changes before sending
2091 * out M8. For now, just copy the settings unchanged into M8.
2097 * For now, use the AP PIN only to receive the current AP settings,
2098 * not to reconfigure the AP.
2100 wps_sta_cred_cb(wps);
2106 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2107 const struct wpabuf *msg,
2108 struct wps_parse_attr *attr)
2110 struct wpabuf *decrypted;
2111 struct wps_parse_attr eattr;
2113 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2115 if (wps->state != RECV_M7) {
2116 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2117 "receiving M7", wps->state);
2118 wps->state = SEND_WSC_NACK;
2119 return WPS_CONTINUE;
2122 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2123 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2125 wps->state = SEND_WSC_NACK;
2126 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2127 return WPS_CONTINUE;
2130 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2131 wps_process_authenticator(wps, attr->authenticator, msg)) {
2132 wps->state = SEND_WSC_NACK;
2133 return WPS_CONTINUE;
2136 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2137 attr->encr_settings_len);
2138 if (decrypted == NULL) {
2139 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2140 "Settings attribute");
2141 wps->state = SEND_WSC_NACK;
2142 return WPS_CONTINUE;
2145 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2147 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2148 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2149 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2150 wps_process_ap_settings_r(wps, &eattr)) {
2151 wpabuf_free(decrypted);
2152 wps->state = SEND_WSC_NACK;
2153 return WPS_CONTINUE;
2156 wpabuf_free(decrypted);
2158 wps->state = SEND_M8;
2159 return WPS_CONTINUE;
2163 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2164 const struct wpabuf *msg)
2166 struct wps_parse_attr attr;
2167 enum wps_process_res ret = WPS_CONTINUE;
2169 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2171 if (wps_parse_msg(msg, &attr) < 0)
2174 if (!wps_version_supported(attr.version)) {
2175 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2176 attr.version ? *attr.version : 0);
2180 if (attr.msg_type == NULL) {
2181 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2185 if (*attr.msg_type != WPS_M1 &&
2186 (attr.registrar_nonce == NULL ||
2187 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2188 WPS_NONCE_LEN != 0))) {
2189 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2193 switch (*attr.msg_type) {
2195 #ifdef CONFIG_WPS_UPNP
2196 if (wps->wps->wps_upnp && attr.mac_addr) {
2197 /* Remove old pending messages when starting new run */
2198 wps_free_pending_msgs(wps->wps->upnp_msgs);
2199 wps->wps->upnp_msgs = NULL;
2201 upnp_wps_device_send_wlan_event(
2202 wps->wps->wps_upnp, attr.mac_addr,
2203 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2205 #endif /* CONFIG_WPS_UPNP */
2206 ret = wps_process_m1(wps, &attr);
2209 ret = wps_process_m3(wps, msg, &attr);
2210 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2211 wps_fail_event(wps->wps, WPS_M3);
2214 ret = wps_process_m5(wps, msg, &attr);
2215 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2216 wps_fail_event(wps->wps, WPS_M5);
2219 ret = wps_process_m7(wps, msg, &attr);
2220 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2221 wps_fail_event(wps->wps, WPS_M7);
2224 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2229 if (ret == WPS_CONTINUE) {
2230 /* Save a copy of the last message for Authenticator derivation
2232 wpabuf_free(wps->last_msg);
2233 wps->last_msg = wpabuf_dup(msg);
2240 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2241 const struct wpabuf *msg)
2243 struct wps_parse_attr attr;
2245 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2247 if (wps_parse_msg(msg, &attr) < 0)
2250 if (!wps_version_supported(attr.version)) {
2251 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2252 attr.version ? *attr.version : 0);
2256 if (attr.msg_type == NULL) {
2257 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2261 if (*attr.msg_type != WPS_WSC_ACK) {
2262 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2267 #ifdef CONFIG_WPS_UPNP
2268 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2269 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2270 if (wps->wps->upnp_msgs)
2271 return WPS_CONTINUE;
2272 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2273 "external Registrar");
2276 #endif /* CONFIG_WPS_UPNP */
2278 if (attr.registrar_nonce == NULL ||
2279 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2281 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2285 if (attr.enrollee_nonce == NULL ||
2286 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2287 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2291 if (wps->state == RECV_M2D_ACK) {
2292 #ifdef CONFIG_WPS_UPNP
2293 if (wps->wps->wps_upnp &&
2294 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2295 if (wps->wps->upnp_msgs)
2296 return WPS_CONTINUE;
2297 if (wps->ext_reg == 0)
2299 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2300 "external Registrar");
2303 #endif /* CONFIG_WPS_UPNP */
2305 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2306 "terminate negotiation");
2313 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2314 const struct wpabuf *msg)
2316 struct wps_parse_attr attr;
2319 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2321 old_state = wps->state;
2322 wps->state = SEND_WSC_NACK;
2324 if (wps_parse_msg(msg, &attr) < 0)
2327 if (!wps_version_supported(attr.version)) {
2328 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2329 attr.version ? *attr.version : 0);
2333 if (attr.msg_type == NULL) {
2334 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2338 if (*attr.msg_type != WPS_WSC_NACK) {
2339 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2344 #ifdef CONFIG_WPS_UPNP
2345 if (wps->wps->wps_upnp && wps->ext_reg) {
2346 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2347 "Registrar terminated by the Enrollee");
2350 #endif /* CONFIG_WPS_UPNP */
2352 if (attr.registrar_nonce == NULL ||
2353 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2355 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2359 if (attr.enrollee_nonce == NULL ||
2360 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2361 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2365 if (attr.config_error == NULL) {
2366 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2371 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2372 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2374 switch (old_state) {
2376 wps_fail_event(wps->wps, WPS_M2);
2379 wps_fail_event(wps->wps, WPS_M4);
2382 wps_fail_event(wps->wps, WPS_M6);
2385 wps_fail_event(wps->wps, WPS_M8);
2395 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2396 const struct wpabuf *msg)
2398 struct wps_parse_attr attr;
2400 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2402 if (wps->state != RECV_DONE &&
2403 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2404 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2405 "receiving WSC_Done", wps->state);
2409 if (wps_parse_msg(msg, &attr) < 0)
2412 if (!wps_version_supported(attr.version)) {
2413 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2414 attr.version ? *attr.version : 0);
2418 if (attr.msg_type == NULL) {
2419 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2423 if (*attr.msg_type != WPS_WSC_DONE) {
2424 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2429 #ifdef CONFIG_WPS_UPNP
2430 if (wps->wps->wps_upnp && wps->ext_reg) {
2431 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2432 "Registrar completed successfully");
2435 #endif /* CONFIG_WPS_UPNP */
2437 if (attr.registrar_nonce == NULL ||
2438 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2440 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2444 if (attr.enrollee_nonce == NULL ||
2445 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2446 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2450 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2452 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2453 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2454 struct wps_credential cred;
2456 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2457 "on first Enrollee connection");
2459 os_memset(&cred, 0, sizeof(cred));
2460 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2461 cred.ssid_len = wps->wps->ssid_len;
2462 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2463 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2464 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2465 cred.key_len = wps->new_psk_len;
2467 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2468 wpa_hexdump_ascii_key(MSG_DEBUG,
2469 "WPS: Generated random passphrase",
2470 wps->new_psk, wps->new_psk_len);
2471 if (wps->wps->cred_cb)
2472 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2474 os_free(wps->new_psk);
2475 wps->new_psk = NULL;
2479 wps_sta_cred_cb(wps);
2482 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2483 wps->new_psk, wps->new_psk_len)) {
2484 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2487 os_free(wps->new_psk);
2488 wps->new_psk = NULL;
2491 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2494 wps_registrar_remove_pbc_session(wps->wps->registrar,
2495 wps->mac_addr_e, wps->uuid_e);
2496 wps_registrar_pbc_completed(wps->wps->registrar);
2498 wps_registrar_pin_completed(wps->wps->registrar);
2501 wps_success_event(wps->wps);
2507 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2508 enum wsc_op_code op_code,
2509 const struct wpabuf *msg)
2511 enum wps_process_res ret;
2513 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2515 (unsigned long) wpabuf_len(msg), op_code);
2517 #ifdef CONFIG_WPS_UPNP
2518 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2519 struct wps_parse_attr attr;
2520 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2521 *attr.msg_type == WPS_M3)
2522 wps->ext_reg = 2; /* past M2/M2D phase */
2524 if (wps->ext_reg > 1)
2525 wps_registrar_free_pending_m2(wps->wps);
2526 if (wps->wps->wps_upnp && wps->ext_reg &&
2527 wps->wps->upnp_msgs == NULL &&
2528 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
2530 struct wps_parse_attr attr;
2532 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2535 type = *attr.msg_type;
2536 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2537 " to external Registrar for processing", type);
2538 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2540 UPNP_WPS_WLANEVENT_TYPE_EAP,
2542 if (op_code == WSC_MSG)
2544 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2545 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2546 "external Registrar");
2547 return WPS_CONTINUE;
2549 #endif /* CONFIG_WPS_UPNP */
2553 return wps_process_wsc_msg(wps, msg);
2555 return wps_process_wsc_ack(wps, msg);
2557 return wps_process_wsc_nack(wps, msg);
2559 ret = wps_process_wsc_done(wps, msg);
2560 if (ret == WPS_FAILURE) {
2561 wps->state = SEND_WSC_NACK;
2562 wps_fail_event(wps->wps, WPS_WSC_DONE);
2566 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2572 int wps_registrar_update_ie(struct wps_registrar *reg)
2574 return wps_set_ie(reg);
2578 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2581 struct wps_registrar *reg = eloop_ctx;
2583 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar timed out - "
2584 "unselect Registrar");
2585 reg->selected_registrar = 0;
2587 reg->sel_reg_dev_password_id_override = -1;
2588 reg->sel_reg_config_methods_override = -1;
2594 * wps_registrar_set_selected_registrar - Notification of SetSelectedRegistrar
2595 * @reg: Registrar data from wps_registrar_init()
2596 * @msg: Received message from SetSelectedRegistrar
2597 * Returns: 0 on success, -1 on failure
2599 * This function is called when an AP receives a SetSelectedRegistrar UPnP
2602 int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
2603 const struct wpabuf *msg)
2605 struct wps_parse_attr attr;
2607 wpa_hexdump_buf(MSG_MSGDUMP, "WPS: SetSelectedRegistrar attributes",
2610 if (wps_parse_msg(msg, &attr) < 0)
2612 if (!wps_version_supported(attr.version)) {
2613 wpa_printf(MSG_DEBUG, "WPS: Unsupported SetSelectedRegistrar "
2614 "version 0x%x", attr.version ? *attr.version : 0);
2618 if (attr.selected_registrar == NULL ||
2619 *attr.selected_registrar == 0) {
2620 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar: Disable "
2621 "Selected Registrar");
2622 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg,
2624 wps_registrar_set_selected_timeout(reg, NULL);
2628 reg->selected_registrar = 1;
2629 reg->sel_reg_dev_password_id_override = attr.dev_password_id ?
2630 WPA_GET_BE16(attr.dev_password_id) : DEV_PW_DEFAULT;
2631 reg->sel_reg_config_methods_override = attr.sel_reg_config_methods ?
2632 WPA_GET_BE16(attr.sel_reg_config_methods) : -1;
2635 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
2636 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
2637 wps_registrar_set_selected_timeout,