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"
27 #define WPS_WORKAROUNDS
30 struct wps_uuid_pin *next;
31 u8 uuid[WPS_UUID_LEN];
35 #define PIN_LOCKED BIT(0)
36 #define PIN_EXPIRES BIT(1)
38 struct os_time expiration;
42 static void wps_free_pin(struct wps_uuid_pin *pin)
49 static void wps_free_pins(struct wps_uuid_pin *pins)
51 struct wps_uuid_pin *pin, *prev;
62 struct wps_pbc_session {
63 struct wps_pbc_session *next;
65 u8 uuid_e[WPS_UUID_LEN];
66 struct os_time timestamp;
70 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
72 struct wps_pbc_session *prev;
82 struct wps_registrar {
83 struct wps_context *wps;
86 int selected_registrar;
88 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
90 int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
91 const u8 *probe_resp_ie, size_t probe_resp_ie_len);
92 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
93 const struct wps_device_data *dev);
94 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
98 struct wps_uuid_pin *pins;
99 struct wps_pbc_session *pbc_sessions;
102 struct wpabuf *extra_cred;
103 int disable_auto_conf;
104 int sel_reg_dev_password_id_override;
105 int sel_reg_config_methods_override;
110 static int wps_set_ie(struct wps_registrar *reg);
111 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
112 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
116 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
117 const u8 *addr, const u8 *uuid_e)
119 struct wps_pbc_session *pbc, *prev = NULL;
124 pbc = reg->pbc_sessions;
126 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
127 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
129 prev->next = pbc->next;
131 reg->pbc_sessions = pbc->next;
139 pbc = os_zalloc(sizeof(*pbc));
142 os_memcpy(pbc->addr, addr, ETH_ALEN);
144 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
147 pbc->next = reg->pbc_sessions;
148 reg->pbc_sessions = pbc;
149 pbc->timestamp = now;
151 /* remove entries that have timed out */
156 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
158 wps_free_pbc_sessions(pbc);
167 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
168 const u8 *addr, const u8 *uuid_e)
170 struct wps_pbc_session *pbc, *prev = NULL;
172 pbc = reg->pbc_sessions;
174 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
175 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
177 prev->next = pbc->next;
179 reg->pbc_sessions = pbc->next;
189 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
190 const u8 *addr, const u8 *uuid_e)
193 struct wps_pbc_session *pbc;
198 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
199 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
201 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
203 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
210 return count > 1 ? 1 : 0;
214 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
216 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
218 wpabuf_put_be16(msg, ATTR_WPS_STATE);
219 wpabuf_put_be16(msg, 1);
220 wpabuf_put_u8(msg, wps->wps_state);
225 #ifdef CONFIG_WPS_UPNP
226 static void wps_registrar_free_pending_m2(struct wps_context *wps)
228 struct upnp_pending_message *p, *p2, *prev = NULL;
231 if (p->type == WPS_M2 || p->type == WPS_M2D) {
233 wps->upnp_msgs = p->next;
235 prev->next = p->next;
236 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
239 wpabuf_free(p2->msg);
247 #endif /* CONFIG_WPS_UPNP */
250 static int wps_build_ap_setup_locked(struct wps_context *wps,
253 if (wps->ap_setup_locked) {
254 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
255 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
256 wpabuf_put_be16(msg, 1);
257 wpabuf_put_u8(msg, 1);
263 static int wps_build_selected_registrar(struct wps_registrar *reg,
266 if (!reg->selected_registrar)
268 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
269 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
270 wpabuf_put_be16(msg, 1);
271 wpabuf_put_u8(msg, 1);
276 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
279 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
280 if (!reg->selected_registrar)
282 if (reg->sel_reg_dev_password_id_override >= 0)
283 id = reg->sel_reg_dev_password_id_override;
284 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
285 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
286 wpabuf_put_be16(msg, 2);
287 wpabuf_put_be16(msg, id);
292 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
296 if (!reg->selected_registrar)
298 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
300 methods |= WPS_CONFIG_PUSHBUTTON;
301 if (reg->sel_reg_config_methods_override >= 0)
302 methods = reg->sel_reg_config_methods_override;
303 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
305 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
306 wpabuf_put_be16(msg, 2);
307 wpabuf_put_be16(msg, methods);
312 static int wps_build_probe_config_methods(struct wps_registrar *reg,
317 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
318 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
319 wpabuf_put_be16(msg, 2);
320 wpabuf_put_be16(msg, methods);
325 static int wps_build_config_methods_r(struct wps_registrar *reg,
329 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
331 methods |= WPS_CONFIG_PUSHBUTTON;
332 return wps_build_config_methods(msg, methods);
336 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
338 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
339 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
340 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
341 wpabuf_put_be16(msg, 1);
342 wpabuf_put_u8(msg, resp);
348 * wps_registrar_init - Initialize WPS Registrar data
349 * @wps: Pointer to longterm WPS context
350 * @cfg: Registrar configuration
351 * Returns: Pointer to allocated Registrar data or %NULL on failure
353 * This function is used to initialize WPS Registrar functionality. It can be
354 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
355 * runs (e.g., when run as an internal Registrar in an AP). Caller is
356 * responsible for freeing the returned data with wps_registrar_deinit() when
357 * Registrar functionality is not needed anymore.
359 struct wps_registrar *
360 wps_registrar_init(struct wps_context *wps,
361 const struct wps_registrar_config *cfg)
363 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
368 reg->new_psk_cb = cfg->new_psk_cb;
369 reg->set_ie_cb = cfg->set_ie_cb;
370 reg->pin_needed_cb = cfg->pin_needed_cb;
371 reg->reg_success_cb = cfg->reg_success_cb;
372 reg->cb_ctx = cfg->cb_ctx;
373 reg->skip_cred_build = cfg->skip_cred_build;
374 if (cfg->extra_cred) {
375 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
376 cfg->extra_cred_len);
377 if (reg->extra_cred == NULL) {
382 reg->disable_auto_conf = cfg->disable_auto_conf;
383 reg->sel_reg_dev_password_id_override = -1;
384 reg->sel_reg_config_methods_override = -1;
385 reg->static_wep_only = cfg->static_wep_only;
387 if (wps_set_ie(reg)) {
388 wps_registrar_deinit(reg);
397 * wps_registrar_deinit - Deinitialize WPS Registrar data
398 * @reg: Registrar data from wps_registrar_init()
400 void wps_registrar_deinit(struct wps_registrar *reg)
404 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
405 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
406 wps_free_pins(reg->pins);
407 wps_free_pbc_sessions(reg->pbc_sessions);
408 wpabuf_free(reg->extra_cred);
414 * wps_registrar_add_pin - Configure a new PIN for Registrar
415 * @reg: Registrar data from wps_registrar_init()
416 * @uuid: UUID-E or %NULL for wildcard (any UUID)
417 * @pin: PIN (Device Password)
418 * @pin_len: Length of pin in octets
419 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
420 * Returns: 0 on success, -1 on failure
422 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
423 const u8 *pin, size_t pin_len, int timeout)
425 struct wps_uuid_pin *p;
427 p = os_zalloc(sizeof(*p));
431 p->wildcard_uuid = 1;
433 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
434 p->pin = os_malloc(pin_len);
435 if (p->pin == NULL) {
439 os_memcpy(p->pin, pin, pin_len);
440 p->pin_len = pin_len;
443 p->flags |= PIN_EXPIRES;
444 os_get_time(&p->expiration);
445 p->expiration.sec += timeout;
451 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
453 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
454 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
455 reg->selected_registrar = 1;
463 static void wps_registrar_expire_pins(struct wps_registrar *reg)
465 struct wps_uuid_pin *pin, *prev, *del;
472 if ((pin->flags & PIN_EXPIRES) &&
473 os_time_before(&pin->expiration, &now)) {
475 reg->pins = pin->next;
477 prev->next = pin->next;
480 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
481 del->uuid, WPS_UUID_LEN);
492 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
493 * @reg: Registrar data from wps_registrar_init()
495 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
497 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
499 struct wps_uuid_pin *pin, *prev;
504 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
506 reg->pins = pin->next;
508 prev->next = pin->next;
509 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
510 pin->uuid, WPS_UUID_LEN);
522 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
523 const u8 *uuid, size_t *pin_len)
525 struct wps_uuid_pin *pin;
527 wps_registrar_expire_pins(reg);
531 if (!pin->wildcard_uuid &&
532 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
538 /* Check for wildcard UUIDs since none of the UUID-specific
542 if (pin->wildcard_uuid == 1) {
543 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
544 "PIN. Assigned it for this UUID-E");
545 pin->wildcard_uuid = 2;
546 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
557 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
558 * that could otherwise avoid PIN invalidations.
560 if (pin->flags & PIN_LOCKED) {
561 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
562 "allow concurrent re-use");
565 *pin_len = pin->pin_len;
566 pin->flags |= PIN_LOCKED;
572 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
573 * @reg: Registrar data from wps_registrar_init()
575 * Returns: 0 on success, -1 on failure
577 * PINs are locked to enforce only one concurrent use. This function unlocks a
578 * PIN to allow it to be used again. If the specified PIN was configured using
579 * a wildcard UUID, it will be removed instead of allowing multiple uses.
581 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
583 struct wps_uuid_pin *pin;
587 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
588 if (pin->wildcard_uuid == 2) {
589 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
591 return wps_registrar_invalidate_pin(reg, uuid);
593 pin->flags &= ~PIN_LOCKED;
603 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
605 reg->selected_registrar = 0;
611 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
613 struct wps_registrar *reg = eloop_ctx;
615 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
616 wps_registrar_stop_pbc(reg);
621 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
622 * @reg: Registrar data from wps_registrar_init()
623 * Returns: 0 on success, -1 on failure
625 * This function is called on an AP when a push button is pushed to activate
626 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
627 * or when a PBC registration is completed.
629 int wps_registrar_button_pushed(struct wps_registrar *reg)
631 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
632 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
636 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
637 reg->selected_registrar = 1;
641 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
642 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
648 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
650 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
651 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
652 wps_registrar_stop_pbc(reg);
657 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
658 * @reg: Registrar data from wps_registrar_init()
659 * @addr: MAC address of the Probe Request sender
660 * @wps_data: WPS IE contents
662 * This function is called on an AP when a Probe Request with WPS IE is
663 * received. This is used to track PBC mode use and to detect possible overlap
664 * situation with other WPS APs.
666 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
667 const struct wpabuf *wps_data)
669 struct wps_parse_attr attr;
672 wpa_hexdump_buf(MSG_MSGDUMP,
673 "WPS: Probe Request with WPS data received",
676 if (wps_parse_msg(wps_data, &attr) < 0)
678 if (!wps_version_supported(attr.version)) {
679 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
680 "version 0x%x", attr.version ? *attr.version : 0);
684 if (attr.config_methods == NULL) {
685 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
690 methods = WPA_GET_BE16(attr.config_methods);
691 if (!(methods & WPS_CONFIG_PUSHBUTTON))
692 return; /* Not PBC */
694 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
695 MACSTR, MAC2STR(addr));
697 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
701 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
702 const u8 *psk, size_t psk_len)
704 if (reg->new_psk_cb == NULL)
707 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
711 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
712 const struct wps_device_data *dev)
714 if (reg->pin_needed_cb == NULL)
717 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
721 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
724 if (reg->reg_success_cb == NULL)
727 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
731 static int wps_cb_set_ie(struct wps_registrar *reg,
732 const struct wpabuf *beacon_ie,
733 const struct wpabuf *probe_resp_ie)
735 if (reg->set_ie_cb == NULL)
738 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
739 wpabuf_len(beacon_ie),
740 wpabuf_head(probe_resp_ie),
741 wpabuf_len(probe_resp_ie));
745 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
746 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
751 ie = wpabuf_alloc(wpabuf_len(data) + 100);
757 pos = wpabuf_head(data);
758 end = pos + wpabuf_len(data);
761 size_t frag_len = end - pos;
764 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
765 wpabuf_put_u8(ie, 4 + frag_len);
766 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
767 wpabuf_put_data(ie, pos, frag_len);
777 static int wps_set_ie(struct wps_registrar *reg)
779 struct wpabuf *beacon;
780 struct wpabuf *probe;
783 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
785 beacon = wpabuf_alloc(300);
788 probe = wpabuf_alloc(400);
794 if (wps_build_version(beacon) ||
795 wps_build_wps_state(reg->wps, beacon) ||
796 wps_build_ap_setup_locked(reg->wps, beacon) ||
797 wps_build_selected_registrar(reg, beacon) ||
798 wps_build_sel_reg_dev_password_id(reg, beacon) ||
799 wps_build_sel_reg_config_methods(reg, beacon) ||
800 wps_build_version(probe) ||
801 wps_build_wps_state(reg->wps, probe) ||
802 wps_build_ap_setup_locked(reg->wps, probe) ||
803 wps_build_selected_registrar(reg, probe) ||
804 wps_build_sel_reg_dev_password_id(reg, probe) ||
805 wps_build_sel_reg_config_methods(reg, probe) ||
806 wps_build_resp_type(reg, probe) ||
807 wps_build_uuid_e(probe, reg->wps->uuid) ||
808 wps_build_device_attrs(®->wps->dev, probe) ||
809 wps_build_probe_config_methods(reg, probe) ||
810 wps_build_rf_bands(®->wps->dev, probe)) {
816 beacon = wps_ie_encapsulate(beacon);
817 probe = wps_ie_encapsulate(probe);
819 if (!beacon || !probe) {
825 if (reg->static_wep_only) {
827 * Windows XP and Vista clients can get confused about
828 * EAP-Identity/Request when they probe the network with
829 * EAPOL-Start. In such a case, they may assume the network is
830 * using IEEE 802.1X and prompt user for a certificate while
831 * the correct (non-WPS) behavior would be to ask for the
832 * static WEP key. As a workaround, use Microsoft Provisioning
833 * IE to advertise that legacy 802.1X is not supported.
835 const u8 ms_wps[7] = {
836 WLAN_EID_VENDOR_SPECIFIC, 5,
837 /* Microsoft Provisioning IE (00:50:f2:5) */
839 0x00 /* no legacy 802.1X or MS WPS */
841 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
842 "into Beacon/Probe Response frames");
843 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
844 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
847 ret = wps_cb_set_ie(reg, beacon, probe);
855 static int wps_get_dev_password(struct wps_data *wps)
860 os_free(wps->dev_password);
861 wps->dev_password = NULL;
864 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
865 pin = (const u8 *) "00000000";
868 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
872 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
874 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
879 wps->dev_password = os_malloc(pin_len);
880 if (wps->dev_password == NULL)
882 os_memcpy(wps->dev_password, pin, pin_len);
883 wps->dev_password_len = pin_len;
889 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
891 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
892 wpabuf_put_be16(msg, ATTR_UUID_R);
893 wpabuf_put_be16(msg, WPS_UUID_LEN);
894 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
899 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
905 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
907 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
908 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
909 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
911 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
912 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
913 "R-Hash derivation");
917 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
918 wpabuf_put_be16(msg, ATTR_R_HASH1);
919 wpabuf_put_be16(msg, SHA256_MAC_LEN);
920 hash = wpabuf_put(msg, SHA256_MAC_LEN);
921 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
922 addr[0] = wps->snonce;
923 len[0] = WPS_SECRET_NONCE_LEN;
925 len[1] = WPS_PSK_LEN;
926 addr[2] = wpabuf_head(wps->dh_pubkey_e);
927 len[2] = wpabuf_len(wps->dh_pubkey_e);
928 addr[3] = wpabuf_head(wps->dh_pubkey_r);
929 len[3] = wpabuf_len(wps->dh_pubkey_r);
930 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
931 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
933 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
934 wpabuf_put_be16(msg, ATTR_R_HASH2);
935 wpabuf_put_be16(msg, SHA256_MAC_LEN);
936 hash = wpabuf_put(msg, SHA256_MAC_LEN);
937 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
938 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
940 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
941 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
947 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
949 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
950 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
951 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
952 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
957 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
959 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
960 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
961 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
962 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
963 WPS_SECRET_NONCE_LEN);
968 static int wps_build_cred_network_idx(struct wpabuf *msg,
969 struct wps_credential *cred)
971 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
972 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
973 wpabuf_put_be16(msg, 1);
974 wpabuf_put_u8(msg, 1);
979 static int wps_build_cred_ssid(struct wpabuf *msg,
980 struct wps_credential *cred)
982 wpa_printf(MSG_DEBUG, "WPS: * SSID");
983 wpabuf_put_be16(msg, ATTR_SSID);
984 wpabuf_put_be16(msg, cred->ssid_len);
985 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
990 static int wps_build_cred_auth_type(struct wpabuf *msg,
991 struct wps_credential *cred)
993 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
995 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
996 wpabuf_put_be16(msg, 2);
997 wpabuf_put_be16(msg, cred->auth_type);
1002 static int wps_build_cred_encr_type(struct wpabuf *msg,
1003 struct wps_credential *cred)
1005 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1007 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1008 wpabuf_put_be16(msg, 2);
1009 wpabuf_put_be16(msg, cred->encr_type);
1014 static int wps_build_cred_network_key(struct wpabuf *msg,
1015 struct wps_credential *cred)
1017 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
1018 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1019 wpabuf_put_be16(msg, cred->key_len);
1020 wpabuf_put_data(msg, cred->key, cred->key_len);
1025 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1026 struct wps_credential *cred)
1028 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1029 MAC2STR(cred->mac_addr));
1030 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1031 wpabuf_put_be16(msg, ETH_ALEN);
1032 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1037 static int wps_build_credential(struct wpabuf *msg,
1038 struct wps_credential *cred)
1040 if (wps_build_cred_network_idx(msg, cred) ||
1041 wps_build_cred_ssid(msg, cred) ||
1042 wps_build_cred_auth_type(msg, cred) ||
1043 wps_build_cred_encr_type(msg, cred) ||
1044 wps_build_cred_network_key(msg, cred) ||
1045 wps_build_cred_mac_addr(msg, cred))
1051 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1053 struct wpabuf *cred;
1055 if (wps->wps->registrar->skip_cred_build)
1056 goto skip_cred_build;
1058 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1059 os_memset(&wps->cred, 0, sizeof(wps->cred));
1061 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1062 wps->cred.ssid_len = wps->wps->ssid_len;
1064 /* Select the best authentication and encryption type */
1065 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1066 wps->auth_type = WPS_AUTH_WPA2PSK;
1067 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1068 wps->auth_type = WPS_AUTH_WPAPSK;
1069 else if (wps->auth_type & WPS_AUTH_OPEN)
1070 wps->auth_type = WPS_AUTH_OPEN;
1071 else if (wps->auth_type & WPS_AUTH_SHARED)
1072 wps->auth_type = WPS_AUTH_SHARED;
1074 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1078 wps->cred.auth_type = wps->auth_type;
1080 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1081 wps->auth_type == WPS_AUTH_WPAPSK) {
1082 if (wps->encr_type & WPS_ENCR_AES)
1083 wps->encr_type = WPS_ENCR_AES;
1084 else if (wps->encr_type & WPS_ENCR_TKIP)
1085 wps->encr_type = WPS_ENCR_TKIP;
1087 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1088 "type for WPA/WPA2");
1092 if (wps->encr_type & WPS_ENCR_WEP)
1093 wps->encr_type = WPS_ENCR_WEP;
1094 else if (wps->encr_type & WPS_ENCR_NONE)
1095 wps->encr_type = WPS_ENCR_NONE;
1097 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1098 "type for non-WPA/WPA2 mode");
1102 wps->cred.encr_type = wps->encr_type;
1103 /* Set MAC address in the Credential to be the AP's address (BSSID) */
1104 os_memcpy(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN);
1106 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1107 !wps->wps->registrar->disable_auto_conf) {
1109 /* Generate a random passphrase */
1110 if (os_get_random(r, sizeof(r)) < 0)
1112 os_free(wps->new_psk);
1113 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1114 if (wps->new_psk == NULL)
1116 wps->new_psk_len--; /* remove newline */
1117 while (wps->new_psk_len &&
1118 wps->new_psk[wps->new_psk_len - 1] == '=')
1120 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1121 wps->new_psk, wps->new_psk_len);
1122 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1123 wps->cred.key_len = wps->new_psk_len;
1124 } else if (wps->wps->network_key) {
1125 os_memcpy(wps->cred.key, wps->wps->network_key,
1126 wps->wps->network_key_len);
1127 wps->cred.key_len = wps->wps->network_key_len;
1128 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1130 /* Generate a random per-device PSK */
1131 os_free(wps->new_psk);
1132 wps->new_psk_len = 32;
1133 wps->new_psk = os_malloc(wps->new_psk_len);
1134 if (wps->new_psk == NULL)
1136 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1137 os_free(wps->new_psk);
1138 wps->new_psk = NULL;
1141 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1142 wps->new_psk, wps->new_psk_len);
1143 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1145 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1146 wps->cred.key_len = wps->new_psk_len * 2;
1149 cred = wpabuf_alloc(200);
1153 if (wps_build_credential(cred, &wps->cred)) {
1158 wpabuf_put_be16(msg, ATTR_CRED);
1159 wpabuf_put_be16(msg, wpabuf_len(cred));
1160 wpabuf_put_buf(msg, cred);
1164 if (wps->wps->registrar->extra_cred) {
1165 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1166 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1173 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1175 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1177 if (wps_build_credential(msg, &wps->cred))
1184 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1188 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1190 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1191 wps->nonce_r, WPS_NONCE_LEN);
1192 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1194 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1195 msg = wpabuf_alloc(1000);
1199 if (wps_build_version(msg) ||
1200 wps_build_msg_type(msg, WPS_M2) ||
1201 wps_build_enrollee_nonce(wps, msg) ||
1202 wps_build_registrar_nonce(wps, msg) ||
1203 wps_build_uuid_r(wps, msg) ||
1204 wps_build_public_key(wps, msg) ||
1205 wps_derive_keys(wps) ||
1206 wps_build_auth_type_flags(wps, msg) ||
1207 wps_build_encr_type_flags(wps, msg) ||
1208 wps_build_conn_type_flags(wps, msg) ||
1209 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1210 wps_build_device_attrs(&wps->wps->dev, msg) ||
1211 wps_build_rf_bands(&wps->wps->dev, msg) ||
1212 wps_build_assoc_state(wps, msg) ||
1213 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1214 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1215 wps_build_os_version(&wps->wps->dev, msg) ||
1216 wps_build_authenticator(wps, msg)) {
1221 wps->state = RECV_M3;
1226 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1229 u16 err = WPS_CFG_NO_ERROR;
1231 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1232 msg = wpabuf_alloc(1000);
1236 if (wps->wps->ap && wps->wps->ap_setup_locked)
1237 err = WPS_CFG_SETUP_LOCKED;
1239 if (wps_build_version(msg) ||
1240 wps_build_msg_type(msg, WPS_M2D) ||
1241 wps_build_enrollee_nonce(wps, msg) ||
1242 wps_build_registrar_nonce(wps, msg) ||
1243 wps_build_uuid_r(wps, msg) ||
1244 wps_build_auth_type_flags(wps, msg) ||
1245 wps_build_encr_type_flags(wps, msg) ||
1246 wps_build_conn_type_flags(wps, msg) ||
1247 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1248 wps_build_device_attrs(&wps->wps->dev, msg) ||
1249 wps_build_rf_bands(&wps->wps->dev, msg) ||
1250 wps_build_assoc_state(wps, msg) ||
1251 wps_build_config_error(msg, err) ||
1252 wps_build_os_version(&wps->wps->dev, msg)) {
1257 wps->state = RECV_M2D_ACK;
1262 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1264 struct wpabuf *msg, *plain;
1266 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1268 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1270 plain = wpabuf_alloc(200);
1274 msg = wpabuf_alloc(1000);
1280 if (wps_build_version(msg) ||
1281 wps_build_msg_type(msg, WPS_M4) ||
1282 wps_build_enrollee_nonce(wps, msg) ||
1283 wps_build_r_hash(wps, msg) ||
1284 wps_build_r_snonce1(wps, plain) ||
1285 wps_build_key_wrap_auth(wps, plain) ||
1286 wps_build_encr_settings(wps, msg, plain) ||
1287 wps_build_authenticator(wps, msg)) {
1294 wps->state = RECV_M5;
1299 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1301 struct wpabuf *msg, *plain;
1303 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1305 plain = wpabuf_alloc(200);
1309 msg = wpabuf_alloc(1000);
1315 if (wps_build_version(msg) ||
1316 wps_build_msg_type(msg, WPS_M6) ||
1317 wps_build_enrollee_nonce(wps, msg) ||
1318 wps_build_r_snonce2(wps, plain) ||
1319 wps_build_key_wrap_auth(wps, plain) ||
1320 wps_build_encr_settings(wps, msg, plain) ||
1321 wps_build_authenticator(wps, msg)) {
1328 wps->wps_pin_revealed = 1;
1329 wps->state = RECV_M7;
1334 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1336 struct wpabuf *msg, *plain;
1338 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1340 plain = wpabuf_alloc(500);
1344 msg = wpabuf_alloc(1000);
1350 if (wps_build_version(msg) ||
1351 wps_build_msg_type(msg, WPS_M8) ||
1352 wps_build_enrollee_nonce(wps, msg) ||
1353 (wps->wps->ap && wps_build_cred(wps, plain)) ||
1354 (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1355 wps_build_key_wrap_auth(wps, plain) ||
1356 wps_build_encr_settings(wps, msg, plain) ||
1357 wps_build_authenticator(wps, msg)) {
1364 wps->state = RECV_DONE;
1369 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1373 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1375 msg = wpabuf_alloc(1000);
1379 if (wps_build_version(msg) ||
1380 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1381 wps_build_enrollee_nonce(wps, msg) ||
1382 wps_build_registrar_nonce(wps, msg)) {
1391 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1395 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1397 msg = wpabuf_alloc(1000);
1401 if (wps_build_version(msg) ||
1402 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1403 wps_build_enrollee_nonce(wps, msg) ||
1404 wps_build_registrar_nonce(wps, msg) ||
1405 wps_build_config_error(msg, wps->config_error)) {
1414 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1415 enum wsc_op_code *op_code)
1419 #ifdef CONFIG_WPS_UPNP
1420 if (wps->wps->wps_upnp) {
1421 struct upnp_pending_message *p, *prev = NULL;
1422 if (wps->ext_reg > 1)
1423 wps_registrar_free_pending_m2(wps->wps);
1424 p = wps->wps->upnp_msgs;
1425 /* TODO: check pending message MAC address */
1426 while (p && p->next) {
1431 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1436 wps->wps->upnp_msgs = NULL;
1440 if (wps->ext_reg == 0)
1446 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1447 "pending message available");
1450 #endif /* CONFIG_WPS_UPNP */
1452 switch (wps->state) {
1454 if (wps_get_dev_password(wps) < 0)
1455 msg = wps_build_m2d(wps);
1457 msg = wps_build_m2(wps);
1461 msg = wps_build_m2d(wps);
1465 msg = wps_build_m4(wps);
1469 msg = wps_build_m6(wps);
1473 msg = wps_build_m8(wps);
1477 msg = wps_build_wsc_ack(wps);
1481 msg = wps_build_wsc_nack(wps);
1482 *op_code = WSC_NACK;
1485 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1486 "a message", wps->state);
1491 if (*op_code == WSC_MSG && msg) {
1492 /* Save a copy of the last message for Authenticator derivation
1494 wpabuf_free(wps->last_msg);
1495 wps->last_msg = wpabuf_dup(msg);
1502 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1504 if (e_nonce == NULL) {
1505 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1509 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1510 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1511 wps->nonce_e, WPS_NONCE_LEN);
1517 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1519 if (r_nonce == NULL) {
1520 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1524 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1525 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1533 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1535 if (uuid_e == NULL) {
1536 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1540 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1541 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1547 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1549 if (pw_id == NULL) {
1550 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1554 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1555 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1561 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1563 if (e_hash1 == NULL) {
1564 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1568 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1569 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1575 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1577 if (e_hash2 == NULL) {
1578 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1582 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1583 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1589 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1591 u8 hash[SHA256_MAC_LEN];
1595 if (e_snonce1 == NULL) {
1596 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1600 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1601 WPS_SECRET_NONCE_LEN);
1603 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1604 addr[0] = e_snonce1;
1605 len[0] = WPS_SECRET_NONCE_LEN;
1606 addr[1] = wps->psk1;
1607 len[1] = WPS_PSK_LEN;
1608 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1609 len[2] = wpabuf_len(wps->dh_pubkey_e);
1610 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1611 len[3] = wpabuf_len(wps->dh_pubkey_r);
1612 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1614 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1615 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1616 "not match with the pre-committed value");
1617 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1618 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1622 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1623 "half of the device password");
1629 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1631 u8 hash[SHA256_MAC_LEN];
1635 if (e_snonce2 == NULL) {
1636 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1640 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1641 WPS_SECRET_NONCE_LEN);
1643 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1644 addr[0] = e_snonce2;
1645 len[0] = WPS_SECRET_NONCE_LEN;
1646 addr[1] = wps->psk2;
1647 len[1] = WPS_PSK_LEN;
1648 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1649 len[2] = wpabuf_len(wps->dh_pubkey_e);
1650 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1651 len[3] = wpabuf_len(wps->dh_pubkey_r);
1652 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1654 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1655 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1656 "not match with the pre-committed value");
1657 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1658 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1659 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1663 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1664 "half of the device password");
1665 wps->wps_pin_revealed = 0;
1666 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1672 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1674 if (mac_addr == NULL) {
1675 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1679 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1681 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1682 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1688 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1691 if (pk == NULL || pk_len == 0) {
1692 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1696 #ifdef CONFIG_WPS_OOB
1697 if (wps->wps->oob_conf.pubkey_hash != NULL) {
1699 u8 hash[WPS_HASH_LEN];
1702 sha256_vector(1, addr, &pk_len, hash);
1704 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
1705 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
1706 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
1710 #endif /* CONFIG_WPS_OOB */
1712 wpabuf_free(wps->dh_pubkey_e);
1713 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1714 if (wps->dh_pubkey_e == NULL)
1721 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1726 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1731 auth_types = WPA_GET_BE16(auth);
1733 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1735 wps->auth_type = wps->wps->auth_types & auth_types;
1736 if (wps->auth_type == 0) {
1737 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1738 "authentication types (own 0x%x Enrollee 0x%x)",
1739 wps->wps->auth_types, auth_types);
1740 #ifdef WPS_WORKAROUNDS
1742 * Some deployed implementations seem to advertise incorrect
1743 * information in this attribute. For example, Linksys WRT350N
1744 * seems to have a byteorder bug that breaks this negotiation.
1745 * In order to interoperate with existing implementations,
1746 * assume that the Enrollee supports everything we do.
1748 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1749 "does not advertise supported authentication types "
1751 wps->auth_type = wps->wps->auth_types;
1752 #else /* WPS_WORKAROUNDS */
1754 #endif /* WPS_WORKAROUNDS */
1761 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1766 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1771 encr_types = WPA_GET_BE16(encr);
1773 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1775 wps->encr_type = wps->wps->encr_types & encr_types;
1776 if (wps->encr_type == 0) {
1777 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1778 "encryption types (own 0x%x Enrollee 0x%x)",
1779 wps->wps->encr_types, encr_types);
1780 #ifdef WPS_WORKAROUNDS
1782 * Some deployed implementations seem to advertise incorrect
1783 * information in this attribute. For example, Linksys WRT350N
1784 * seems to have a byteorder bug that breaks this negotiation.
1785 * In order to interoperate with existing implementations,
1786 * assume that the Enrollee supports everything we do.
1788 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1789 "does not advertise supported encryption types "
1791 wps->encr_type = wps->wps->encr_types;
1792 #else /* WPS_WORKAROUNDS */
1794 #endif /* WPS_WORKAROUNDS */
1801 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1804 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1809 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1816 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1820 if (methods == NULL) {
1821 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1825 m = WPA_GET_BE16(methods);
1827 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1833 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1835 if (state == NULL) {
1836 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1841 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1848 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1852 if (assoc == NULL) {
1853 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1857 a = WPA_GET_BE16(assoc);
1858 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1864 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1869 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1873 e = WPA_GET_BE16(err);
1874 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1880 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1881 struct wps_parse_attr *attr)
1883 wpa_printf(MSG_DEBUG, "WPS: Received M1");
1885 if (wps->state != RECV_M1) {
1886 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1887 "receiving M1", wps->state);
1891 if (wps_process_uuid_e(wps, attr->uuid_e) ||
1892 wps_process_mac_addr(wps, attr->mac_addr) ||
1893 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1894 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1895 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1896 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1897 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1898 wps_process_config_methods(wps, attr->config_methods) ||
1899 wps_process_wps_state(wps, attr->wps_state) ||
1900 wps_process_device_attrs(&wps->peer_dev, attr) ||
1901 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1902 wps_process_assoc_state(wps, attr->assoc_state) ||
1903 wps_process_dev_password_id(wps, attr->dev_password_id) ||
1904 wps_process_config_error(wps, attr->config_error) ||
1905 wps_process_os_version(&wps->peer_dev, attr->os_version))
1908 if (wps->dev_pw_id < 0x10 &&
1909 wps->dev_pw_id != DEV_PW_DEFAULT &&
1910 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1911 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1912 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1913 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
1914 !wps->wps->registrar->pbc)) {
1915 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1917 wps->state = SEND_M2D;
1918 return WPS_CONTINUE;
1921 #ifdef CONFIG_WPS_OOB
1922 if (wps->dev_pw_id >= 0x10 &&
1923 wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
1924 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
1925 "%d mismatch", wps->dev_pw_id);
1926 wps->state = SEND_M2D;
1927 return WPS_CONTINUE;
1929 #endif /* CONFIG_WPS_OOB */
1931 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1932 if (wps_registrar_pbc_overlap(wps->wps->registrar,
1933 wps->mac_addr_e, wps->uuid_e)) {
1934 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1936 wps->state = SEND_M2D;
1937 return WPS_CONTINUE;
1939 wps_registrar_add_pbc_session(wps->wps->registrar,
1940 wps->mac_addr_e, wps->uuid_e);
1944 wps->state = SEND_M2;
1945 return WPS_CONTINUE;
1949 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1950 const struct wpabuf *msg,
1951 struct wps_parse_attr *attr)
1953 wpa_printf(MSG_DEBUG, "WPS: Received M3");
1955 if (wps->state != RECV_M3) {
1956 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1957 "receiving M3", wps->state);
1958 wps->state = SEND_WSC_NACK;
1959 return WPS_CONTINUE;
1962 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1963 wps_process_authenticator(wps, attr->authenticator, msg) ||
1964 wps_process_e_hash1(wps, attr->e_hash1) ||
1965 wps_process_e_hash2(wps, attr->e_hash2)) {
1966 wps->state = SEND_WSC_NACK;
1967 return WPS_CONTINUE;
1970 wps->state = SEND_M4;
1971 return WPS_CONTINUE;
1975 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1976 const struct wpabuf *msg,
1977 struct wps_parse_attr *attr)
1979 struct wpabuf *decrypted;
1980 struct wps_parse_attr eattr;
1982 wpa_printf(MSG_DEBUG, "WPS: Received M5");
1984 if (wps->state != RECV_M5) {
1985 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1986 "receiving M5", wps->state);
1987 wps->state = SEND_WSC_NACK;
1988 return WPS_CONTINUE;
1991 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1992 wps_process_authenticator(wps, attr->authenticator, msg)) {
1993 wps->state = SEND_WSC_NACK;
1994 return WPS_CONTINUE;
1997 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1998 attr->encr_settings_len);
1999 if (decrypted == NULL) {
2000 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2001 "Settings attribute");
2002 wps->state = SEND_WSC_NACK;
2003 return WPS_CONTINUE;
2006 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2008 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2009 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2010 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2011 wpabuf_free(decrypted);
2012 wps->state = SEND_WSC_NACK;
2013 return WPS_CONTINUE;
2015 wpabuf_free(decrypted);
2017 wps->state = SEND_M6;
2018 return WPS_CONTINUE;
2022 static void wps_sta_cred_cb(struct wps_data *wps)
2025 * Update credential to only include a single authentication and
2026 * encryption type in case the AP configuration includes more than one
2029 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2030 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2031 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2032 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2033 if (wps->cred.encr_type & WPS_ENCR_AES)
2034 wps->cred.encr_type = WPS_ENCR_AES;
2035 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2036 wps->cred.encr_type = WPS_ENCR_TKIP;
2037 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2038 "AP configuration");
2039 if (wps->wps->cred_cb)
2040 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2044 static int wps_process_ap_settings_r(struct wps_data *wps,
2045 struct wps_parse_attr *attr)
2050 /* AP Settings Attributes in M7 when Enrollee is an AP */
2051 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2054 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2058 * TODO: Provide access to AP settings and allow changes before sending
2059 * out M8. For now, just copy the settings unchanged into M8.
2065 * For now, use the AP PIN only to receive the current AP settings,
2066 * not to reconfigure the AP.
2068 wps_sta_cred_cb(wps);
2074 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2075 const struct wpabuf *msg,
2076 struct wps_parse_attr *attr)
2078 struct wpabuf *decrypted;
2079 struct wps_parse_attr eattr;
2081 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2083 if (wps->state != RECV_M7) {
2084 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2085 "receiving M7", wps->state);
2086 wps->state = SEND_WSC_NACK;
2087 return WPS_CONTINUE;
2090 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2091 wps_process_authenticator(wps, attr->authenticator, msg)) {
2092 wps->state = SEND_WSC_NACK;
2093 return WPS_CONTINUE;
2096 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2097 attr->encr_settings_len);
2098 if (decrypted == NULL) {
2099 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2100 "Settings attribute");
2101 wps->state = SEND_WSC_NACK;
2102 return WPS_CONTINUE;
2105 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2107 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2108 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2109 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2110 wps_process_ap_settings_r(wps, &eattr)) {
2111 wpabuf_free(decrypted);
2112 wps->state = SEND_WSC_NACK;
2113 return WPS_CONTINUE;
2116 wpabuf_free(decrypted);
2118 wps->state = SEND_M8;
2119 return WPS_CONTINUE;
2123 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2124 const struct wpabuf *msg)
2126 struct wps_parse_attr attr;
2127 enum wps_process_res ret = WPS_CONTINUE;
2129 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2131 if (wps_parse_msg(msg, &attr) < 0)
2134 if (!wps_version_supported(attr.version)) {
2135 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2136 attr.version ? *attr.version : 0);
2140 if (attr.msg_type == NULL) {
2141 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2145 if (*attr.msg_type != WPS_M1 &&
2146 (attr.registrar_nonce == NULL ||
2147 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2148 WPS_NONCE_LEN != 0))) {
2149 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2153 switch (*attr.msg_type) {
2155 #ifdef CONFIG_WPS_UPNP
2156 if (wps->wps->wps_upnp && attr.mac_addr) {
2157 /* Remove old pending messages when starting new run */
2158 wps_free_pending_msgs(wps->wps->upnp_msgs);
2159 wps->wps->upnp_msgs = NULL;
2161 upnp_wps_device_send_wlan_event(
2162 wps->wps->wps_upnp, attr.mac_addr,
2163 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2165 #endif /* CONFIG_WPS_UPNP */
2166 ret = wps_process_m1(wps, &attr);
2169 ret = wps_process_m3(wps, msg, &attr);
2170 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2171 wps_fail_event(wps->wps, WPS_M3);
2174 ret = wps_process_m5(wps, msg, &attr);
2175 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2176 wps_fail_event(wps->wps, WPS_M5);
2179 ret = wps_process_m7(wps, msg, &attr);
2180 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2181 wps_fail_event(wps->wps, WPS_M7);
2184 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2189 if (ret == WPS_CONTINUE) {
2190 /* Save a copy of the last message for Authenticator derivation
2192 wpabuf_free(wps->last_msg);
2193 wps->last_msg = wpabuf_dup(msg);
2200 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2201 const struct wpabuf *msg)
2203 struct wps_parse_attr attr;
2205 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2207 if (wps_parse_msg(msg, &attr) < 0)
2210 if (!wps_version_supported(attr.version)) {
2211 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2212 attr.version ? *attr.version : 0);
2216 if (attr.msg_type == NULL) {
2217 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2221 if (*attr.msg_type != WPS_WSC_ACK) {
2222 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2227 #ifdef CONFIG_WPS_UPNP
2228 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2229 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2230 if (wps->wps->upnp_msgs)
2231 return WPS_CONTINUE;
2232 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2233 "external Registrar");
2236 #endif /* CONFIG_WPS_UPNP */
2238 if (attr.registrar_nonce == NULL ||
2239 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2241 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2245 if (attr.enrollee_nonce == NULL ||
2246 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2247 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2251 if (wps->state == RECV_M2D_ACK) {
2252 #ifdef CONFIG_WPS_UPNP
2253 if (wps->wps->wps_upnp &&
2254 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2255 if (wps->wps->upnp_msgs)
2256 return WPS_CONTINUE;
2257 if (wps->ext_reg == 0)
2259 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2260 "external Registrar");
2263 #endif /* CONFIG_WPS_UPNP */
2265 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2266 "terminate negotiation");
2273 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2274 const struct wpabuf *msg)
2276 struct wps_parse_attr attr;
2279 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2281 old_state = wps->state;
2282 wps->state = SEND_WSC_NACK;
2284 if (wps_parse_msg(msg, &attr) < 0)
2287 if (!wps_version_supported(attr.version)) {
2288 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2289 attr.version ? *attr.version : 0);
2293 if (attr.msg_type == NULL) {
2294 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2298 if (*attr.msg_type != WPS_WSC_NACK) {
2299 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2304 #ifdef CONFIG_WPS_UPNP
2305 if (wps->wps->wps_upnp && wps->ext_reg) {
2306 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2307 "Registrar terminated by the Enrollee");
2310 #endif /* CONFIG_WPS_UPNP */
2312 if (attr.registrar_nonce == NULL ||
2313 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2315 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2319 if (attr.enrollee_nonce == NULL ||
2320 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2321 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2325 if (attr.config_error == NULL) {
2326 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2331 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2332 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2334 switch (old_state) {
2336 wps_fail_event(wps->wps, WPS_M2);
2339 wps_fail_event(wps->wps, WPS_M4);
2342 wps_fail_event(wps->wps, WPS_M6);
2345 wps_fail_event(wps->wps, WPS_M8);
2355 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2356 const struct wpabuf *msg)
2358 struct wps_parse_attr attr;
2360 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2362 if (wps->state != RECV_DONE &&
2363 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2364 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2365 "receiving WSC_Done", wps->state);
2369 if (wps_parse_msg(msg, &attr) < 0)
2372 if (!wps_version_supported(attr.version)) {
2373 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2374 attr.version ? *attr.version : 0);
2378 if (attr.msg_type == NULL) {
2379 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2383 if (*attr.msg_type != WPS_WSC_DONE) {
2384 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2389 #ifdef CONFIG_WPS_UPNP
2390 if (wps->wps->wps_upnp && wps->ext_reg) {
2391 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2392 "Registrar completed successfully");
2395 #endif /* CONFIG_WPS_UPNP */
2397 if (attr.registrar_nonce == NULL ||
2398 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2400 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2404 if (attr.enrollee_nonce == NULL ||
2405 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2406 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2410 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2412 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2413 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2414 struct wps_credential cred;
2416 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2417 "on first Enrollee connection");
2419 os_memset(&cred, 0, sizeof(cred));
2420 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2421 cred.ssid_len = wps->wps->ssid_len;
2422 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2423 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2424 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2425 cred.key_len = wps->new_psk_len;
2427 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2428 wpa_hexdump_ascii_key(MSG_DEBUG,
2429 "WPS: Generated random passphrase",
2430 wps->new_psk, wps->new_psk_len);
2431 if (wps->wps->cred_cb)
2432 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2434 os_free(wps->new_psk);
2435 wps->new_psk = NULL;
2439 wps_sta_cred_cb(wps);
2442 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2443 wps->new_psk, wps->new_psk_len)) {
2444 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2447 os_free(wps->new_psk);
2448 wps->new_psk = NULL;
2451 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2454 wps_registrar_remove_pbc_session(wps->wps->registrar,
2455 wps->mac_addr_e, wps->uuid_e);
2456 wps_registrar_pbc_completed(wps->wps->registrar);
2459 wps_success_event(wps->wps);
2465 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2466 enum wsc_op_code op_code,
2467 const struct wpabuf *msg)
2469 enum wps_process_res ret;
2471 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2473 (unsigned long) wpabuf_len(msg), op_code);
2475 #ifdef CONFIG_WPS_UPNP
2476 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2477 struct wps_parse_attr attr;
2478 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2479 *attr.msg_type == WPS_M3)
2480 wps->ext_reg = 2; /* past M2/M2D phase */
2482 if (wps->ext_reg > 1)
2483 wps_registrar_free_pending_m2(wps->wps);
2484 if (wps->wps->wps_upnp && wps->ext_reg &&
2485 wps->wps->upnp_msgs == NULL &&
2486 (op_code == WSC_MSG || op_code == WSC_Done)) {
2487 struct wps_parse_attr attr;
2489 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2492 type = *attr.msg_type;
2493 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2494 " to external Registrar for processing", type);
2495 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2497 UPNP_WPS_WLANEVENT_TYPE_EAP,
2499 if (op_code == WSC_MSG)
2501 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2502 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2503 "external Registrar");
2504 return WPS_CONTINUE;
2506 #endif /* CONFIG_WPS_UPNP */
2510 return wps_process_wsc_msg(wps, msg);
2512 return wps_process_wsc_ack(wps, msg);
2514 return wps_process_wsc_nack(wps, msg);
2516 ret = wps_process_wsc_done(wps, msg);
2517 if (ret == WPS_FAILURE) {
2518 wps->state = SEND_WSC_NACK;
2519 wps_fail_event(wps->wps, WPS_WSC_DONE);
2523 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2529 int wps_registrar_update_ie(struct wps_registrar *reg)
2531 return wps_set_ie(reg);
2535 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2538 struct wps_registrar *reg = eloop_ctx;
2540 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar timed out - "
2541 "unselect Registrar");
2542 reg->selected_registrar = 0;
2544 reg->sel_reg_dev_password_id_override = -1;
2545 reg->sel_reg_config_methods_override = -1;
2551 * wps_registrar_set_selected_registrar - Notification of SetSelectedRegistrar
2552 * @reg: Registrar data from wps_registrar_init()
2553 * @msg: Received message from SetSelectedRegistrar
2554 * Returns: 0 on success, -1 on failure
2556 * This function is called when an AP receives a SetSelectedRegistrar UPnP
2559 int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
2560 const struct wpabuf *msg)
2562 struct wps_parse_attr attr;
2564 wpa_hexdump_buf(MSG_MSGDUMP, "WPS: SetSelectedRegistrar attributes",
2567 if (wps_parse_msg(msg, &attr) < 0)
2569 if (!wps_version_supported(attr.version)) {
2570 wpa_printf(MSG_DEBUG, "WPS: Unsupported SetSelectedRegistrar "
2571 "version 0x%x", attr.version ? *attr.version : 0);
2575 if (attr.selected_registrar == NULL ||
2576 *attr.selected_registrar == 0) {
2577 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar: Disable "
2578 "Selected Registrar");
2579 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg,
2581 wps_registrar_set_selected_timeout(reg, NULL);
2585 reg->selected_registrar = 1;
2586 reg->sel_reg_dev_password_id_override = attr.dev_password_id ?
2587 WPA_GET_BE16(attr.dev_password_id) : DEV_PW_DEFAULT;
2588 reg->sel_reg_config_methods_override = attr.sel_reg_config_methods ?
2589 WPA_GET_BE16(attr.sel_reg_config_methods) : -1;
2592 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
2593 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
2594 wps_registrar_set_selected_timeout,