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 struct wps_uuid_pin *next;
28 u8 uuid[WPS_UUID_LEN];
36 static void wps_free_pin(struct wps_uuid_pin *pin)
43 static void wps_free_pins(struct wps_uuid_pin *pins)
45 struct wps_uuid_pin *pin, *prev;
56 struct wps_pbc_session {
57 struct wps_pbc_session *next;
59 u8 uuid_e[WPS_UUID_LEN];
60 struct os_time timestamp;
64 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
66 struct wps_pbc_session *prev;
76 struct wps_registrar {
77 struct wps_context *wps;
80 int selected_registrar;
82 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
84 int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
85 const u8 *probe_resp_ie, size_t probe_resp_ie_len);
86 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
87 const struct wps_device_data *dev);
90 struct wps_uuid_pin *pins;
91 struct wps_pbc_session *pbc_sessions;
95 static int wps_set_ie(struct wps_registrar *reg);
96 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
99 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
100 const u8 *addr, const u8 *uuid_e)
102 struct wps_pbc_session *pbc, *prev = NULL;
107 pbc = reg->pbc_sessions;
109 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
110 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
112 prev->next = pbc->next;
114 reg->pbc_sessions = pbc->next;
122 pbc = os_zalloc(sizeof(*pbc));
125 os_memcpy(pbc->addr, addr, ETH_ALEN);
127 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
130 pbc->next = reg->pbc_sessions;
131 reg->pbc_sessions = pbc;
132 pbc->timestamp = now;
134 /* remove entries that have timed out */
139 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
141 wps_free_pbc_sessions(pbc);
150 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
151 const u8 *addr, const u8 *uuid_e)
153 struct wps_pbc_session *pbc, *prev = NULL;
155 pbc = reg->pbc_sessions;
157 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
158 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
160 prev->next = pbc->next;
162 reg->pbc_sessions = pbc->next;
172 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
173 const u8 *addr, const u8 *uuid_e)
176 struct wps_pbc_session *pbc;
181 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
182 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
184 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
186 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
193 return count > 1 ? 1 : 0;
197 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
199 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
201 wpabuf_put_be16(msg, ATTR_WPS_STATE);
202 wpabuf_put_be16(msg, 1);
203 wpabuf_put_u8(msg, wps->wps_state);
208 static int wps_build_ap_setup_locked(struct wps_context *wps,
211 if (wps->ap_setup_locked) {
212 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
213 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
214 wpabuf_put_be16(msg, 1);
215 wpabuf_put_u8(msg, 1);
221 static int wps_build_selected_registrar(struct wps_registrar *reg,
224 if (!reg->selected_registrar)
226 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
227 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
228 wpabuf_put_be16(msg, 1);
229 wpabuf_put_u8(msg, 1);
234 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
237 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
238 if (!reg->selected_registrar)
240 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
241 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
242 wpabuf_put_be16(msg, 2);
243 wpabuf_put_be16(msg, id);
248 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
252 if (!reg->selected_registrar)
254 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
256 methods |= WPS_CONFIG_PUSHBUTTON;
257 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
259 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
260 wpabuf_put_be16(msg, 2);
261 wpabuf_put_be16(msg, methods);
266 static int wps_build_probe_config_methods(struct wps_registrar *reg,
271 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
272 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
273 wpabuf_put_be16(msg, 2);
274 wpabuf_put_be16(msg, methods);
279 static int wps_build_config_methods_r(struct wps_registrar *reg,
283 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
285 methods |= WPS_CONFIG_PUSHBUTTON;
286 return wps_build_config_methods(msg, methods);
290 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
292 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
293 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
294 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
295 wpabuf_put_be16(msg, 1);
296 wpabuf_put_u8(msg, resp);
302 * wps_registrar_init - Initialize WPS Registrar data
303 * @wps: Pointer to longterm WPS context
304 * @cfg: Registrar configuration
305 * Returns: Pointer to allocated Registrar data or %NULL on failure
307 * This function is used to initialize WPS Registrar functionality. It can be
308 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
309 * runs (e.g., when run as an internal Registrar in an AP). Caller is
310 * responsible for freeing the returned data with wps_registrar_deinit() when
311 * Registrar functionality is not needed anymore.
313 struct wps_registrar *
314 wps_registrar_init(struct wps_context *wps,
315 const struct wps_registrar_config *cfg)
317 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
322 reg->new_psk_cb = cfg->new_psk_cb;
323 reg->set_ie_cb = cfg->set_ie_cb;
324 reg->pin_needed_cb = cfg->pin_needed_cb;
325 reg->cb_ctx = cfg->cb_ctx;
327 if (wps_set_ie(reg)) {
328 wps_registrar_deinit(reg);
337 * wps_registrar_deinit - Deinitialize WPS Registrar data
338 * @reg: Registrar data from wps_registrar_deinit()
340 void wps_registrar_deinit(struct wps_registrar *reg)
344 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
345 wps_free_pins(reg->pins);
346 wps_free_pbc_sessions(reg->pbc_sessions);
351 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
352 const u8 *pin, size_t pin_len)
354 struct wps_uuid_pin *p;
356 p = os_zalloc(sizeof(*p));
360 p->wildcard_uuid = 1;
362 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
363 p->pin = os_malloc(pin_len);
364 if (p->pin == NULL) {
368 os_memcpy(p->pin, pin, pin_len);
369 p->pin_len = pin_len;
374 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured");
375 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
376 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
377 reg->selected_registrar = 1;
385 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
387 struct wps_uuid_pin *pin, *prev;
392 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
394 reg->pins = pin->next;
396 prev->next = pin->next;
397 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
398 pin->uuid, WPS_UUID_LEN);
410 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
411 const u8 *uuid, size_t *pin_len)
413 struct wps_uuid_pin *pin;
417 if (!pin->wildcard_uuid &&
418 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
424 /* Check for wildcard UUIDs since none of the UUID-specific
428 if (pin->wildcard_uuid == 1) {
429 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
430 "PIN. Assigned it for this UUID-E");
431 pin->wildcard_uuid = 2;
432 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
443 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
444 * that could otherwise avoid PIN invalidations.
447 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
448 "allow concurrent re-use");
451 *pin_len = pin->pin_len;
457 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
459 struct wps_uuid_pin *pin;
463 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
464 if (pin->wildcard_uuid == 2) {
465 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
467 return wps_registrar_invalidate_pin(reg, uuid);
479 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
481 struct wps_registrar *reg = eloop_ctx;
483 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
484 reg->selected_registrar = 0;
490 int wps_registrar_button_pushed(struct wps_registrar *reg)
492 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
493 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
497 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
498 reg->selected_registrar = 1;
502 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
503 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
509 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
511 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
512 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
513 reg->selected_registrar = 0;
519 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
520 const struct wpabuf *wps_data)
522 struct wps_parse_attr attr;
525 wpa_hexdump_buf(MSG_MSGDUMP,
526 "WPS: Probe Request with WPS data received",
529 if (wps_parse_msg(wps_data, &attr) < 0 ||
530 attr.version == NULL || *attr.version != WPS_VERSION) {
531 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
532 "version 0x%x", attr.version ? *attr.version : 0);
536 if (attr.config_methods == NULL) {
537 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
542 methods = WPA_GET_BE16(attr.config_methods);
543 if (!(methods & WPS_CONFIG_PUSHBUTTON))
544 return; /* Not PBC */
546 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
547 MACSTR, MAC2STR(addr));
549 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
553 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
554 const u8 *psk, size_t psk_len)
556 if (reg->new_psk_cb == NULL)
559 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
563 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
564 const struct wps_device_data *dev)
566 if (reg->pin_needed_cb == NULL)
569 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
573 static int wps_cb_set_ie(struct wps_registrar *reg,
574 const struct wpabuf *beacon_ie,
575 const struct wpabuf *probe_resp_ie)
577 if (reg->set_ie_cb == NULL)
580 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
581 wpabuf_len(beacon_ie),
582 wpabuf_head(probe_resp_ie),
583 wpabuf_len(probe_resp_ie));
587 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
588 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
593 ie = wpabuf_alloc(wpabuf_len(data) + 100);
599 pos = wpabuf_head(data);
600 end = pos + wpabuf_len(data);
603 size_t frag_len = end - pos;
606 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
607 wpabuf_put_u8(ie, 4 + frag_len);
608 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
609 wpabuf_put_data(ie, pos, frag_len);
619 static int wps_set_ie(struct wps_registrar *reg)
621 struct wpabuf *beacon;
622 struct wpabuf *probe;
625 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
627 beacon = wpabuf_alloc(300);
630 probe = wpabuf_alloc(400);
636 if (wps_build_version(beacon) ||
637 wps_build_wps_state(reg->wps, beacon) ||
638 wps_build_ap_setup_locked(reg->wps, beacon) ||
639 wps_build_selected_registrar(reg, beacon) ||
640 wps_build_sel_reg_dev_password_id(reg, beacon) ||
641 wps_build_sel_reg_config_methods(reg, beacon) ||
642 wps_build_version(probe) ||
643 wps_build_wps_state(reg->wps, probe) ||
644 wps_build_ap_setup_locked(reg->wps, probe) ||
645 wps_build_selected_registrar(reg, probe) ||
646 wps_build_sel_reg_dev_password_id(reg, probe) ||
647 wps_build_sel_reg_config_methods(reg, probe) ||
648 wps_build_resp_type(reg, probe) ||
649 wps_build_uuid_e(probe, reg->wps->uuid) ||
650 wps_build_device_attrs(®->wps->dev, probe) ||
651 wps_build_probe_config_methods(reg, probe) ||
652 wps_build_rf_bands(®->wps->dev, probe)) {
658 beacon = wps_ie_encapsulate(beacon);
659 probe = wps_ie_encapsulate(probe);
661 if (!beacon || !probe) {
667 ret = wps_cb_set_ie(reg, beacon, probe);
675 static int wps_get_dev_password(struct wps_data *wps)
680 os_free(wps->dev_password);
681 wps->dev_password = NULL;
684 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
685 pin = (const u8 *) "00000000";
688 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
692 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
694 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
699 wps->dev_password = os_malloc(pin_len);
700 if (wps->dev_password == NULL)
702 os_memcpy(wps->dev_password, pin, pin_len);
703 wps->dev_password_len = pin_len;
709 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
711 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
712 wpabuf_put_be16(msg, ATTR_UUID_R);
713 wpabuf_put_be16(msg, WPS_UUID_LEN);
714 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
719 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
725 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
727 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
728 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
729 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
731 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
732 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
733 "R-Hash derivation");
737 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
738 wpabuf_put_be16(msg, ATTR_R_HASH1);
739 wpabuf_put_be16(msg, SHA256_MAC_LEN);
740 hash = wpabuf_put(msg, SHA256_MAC_LEN);
741 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
742 addr[0] = wps->snonce;
743 len[0] = WPS_SECRET_NONCE_LEN;
745 len[1] = WPS_PSK_LEN;
746 addr[2] = wpabuf_head(wps->dh_pubkey_e);
747 len[2] = wpabuf_len(wps->dh_pubkey_e);
748 addr[3] = wpabuf_head(wps->dh_pubkey_r);
749 len[3] = wpabuf_len(wps->dh_pubkey_r);
750 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
751 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
753 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
754 wpabuf_put_be16(msg, ATTR_R_HASH2);
755 wpabuf_put_be16(msg, SHA256_MAC_LEN);
756 hash = wpabuf_put(msg, SHA256_MAC_LEN);
757 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
758 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
760 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
761 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
767 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
769 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
770 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
771 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
772 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
777 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
779 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
780 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
781 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
782 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
783 WPS_SECRET_NONCE_LEN);
788 static int wps_build_cred_network_idx(struct wpabuf *msg,
789 struct wps_credential *cred)
791 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
792 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
793 wpabuf_put_be16(msg, 1);
794 wpabuf_put_u8(msg, 0);
799 static int wps_build_cred_ssid(struct wpabuf *msg,
800 struct wps_credential *cred)
802 wpa_printf(MSG_DEBUG, "WPS: * SSID");
803 wpabuf_put_be16(msg, ATTR_SSID);
804 wpabuf_put_be16(msg, cred->ssid_len);
805 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
810 static int wps_build_cred_auth_type(struct wpabuf *msg,
811 struct wps_credential *cred)
813 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
815 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
816 wpabuf_put_be16(msg, 2);
817 wpabuf_put_be16(msg, cred->auth_type);
822 static int wps_build_cred_encr_type(struct wpabuf *msg,
823 struct wps_credential *cred)
825 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
827 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
828 wpabuf_put_be16(msg, 2);
829 wpabuf_put_be16(msg, cred->encr_type);
834 static int wps_build_cred_network_key(struct wpabuf *msg,
835 struct wps_credential *cred)
837 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
838 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
839 wpabuf_put_be16(msg, cred->key_len);
840 wpabuf_put_data(msg, cred->key, cred->key_len);
845 static int wps_build_cred_mac_addr(struct wpabuf *msg,
846 struct wps_credential *cred)
848 wpa_printf(MSG_DEBUG, "WPS: * MAC Address");
849 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
850 wpabuf_put_be16(msg, ETH_ALEN);
851 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
856 static int wps_build_credential(struct wpabuf *msg,
857 struct wps_credential *cred)
859 if (wps_build_cred_network_idx(msg, cred) ||
860 wps_build_cred_ssid(msg, cred) ||
861 wps_build_cred_auth_type(msg, cred) ||
862 wps_build_cred_encr_type(msg, cred) ||
863 wps_build_cred_network_key(msg, cred) ||
864 wps_build_cred_mac_addr(msg, cred))
870 static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
874 wpa_printf(MSG_DEBUG, "WPS: * Credential");
875 os_memset(&wps->cred, 0, sizeof(wps->cred));
877 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
878 wps->cred.ssid_len = wps->wps->ssid_len;
880 /* Select the best authentication and encryption type */
881 if (wps->auth_type & WPS_AUTH_WPA2PSK)
882 wps->auth_type = WPS_AUTH_WPA2PSK;
883 else if (wps->auth_type & WPS_AUTH_WPAPSK)
884 wps->auth_type = WPS_AUTH_WPAPSK;
885 else if (wps->auth_type & WPS_AUTH_OPEN)
886 wps->auth_type = WPS_AUTH_OPEN;
887 else if (wps->auth_type & WPS_AUTH_SHARED)
888 wps->auth_type = WPS_AUTH_SHARED;
890 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
894 wps->cred.auth_type = wps->auth_type;
896 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
897 wps->auth_type == WPS_AUTH_WPAPSK) {
898 if (wps->encr_type & WPS_ENCR_AES)
899 wps->encr_type = WPS_ENCR_AES;
900 else if (wps->encr_type & WPS_ENCR_TKIP)
901 wps->encr_type = WPS_ENCR_TKIP;
903 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
904 "type for WPA/WPA2");
908 if (wps->encr_type & WPS_ENCR_WEP)
909 wps->encr_type = WPS_ENCR_WEP;
910 else if (wps->encr_type & WPS_ENCR_NONE)
911 wps->encr_type = WPS_ENCR_NONE;
913 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
914 "type for non-WPA/WPA2 mode");
918 wps->cred.encr_type = wps->encr_type;
919 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
921 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap) {
923 /* Generate a random passphrase */
924 if (os_get_random(r, sizeof(r)) < 0)
926 os_free(wps->new_psk);
927 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
928 if (wps->new_psk == NULL)
930 wps->new_psk_len--; /* remove newline */
931 while (wps->new_psk_len &&
932 wps->new_psk[wps->new_psk_len - 1] == '=')
934 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
935 wps->new_psk, wps->new_psk_len);
936 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
937 wps->cred.key_len = wps->new_psk_len;
938 } else if (wps->wps->network_key) {
939 os_memcpy(wps->cred.key, wps->wps->network_key,
940 wps->wps->network_key_len);
941 wps->cred.key_len = wps->wps->network_key_len;
942 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
944 /* Generate a random per-device PSK */
945 os_free(wps->new_psk);
946 wps->new_psk_len = 32;
947 wps->new_psk = os_malloc(wps->new_psk_len);
948 if (wps->new_psk == NULL)
950 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
951 os_free(wps->new_psk);
955 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
956 wps->new_psk, wps->new_psk_len);
957 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
959 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
960 wps->cred.key_len = wps->new_psk_len * 2;
963 cred = wpabuf_alloc(200);
967 if (wps_build_credential(cred, &wps->cred)) {
972 wpabuf_put_be16(msg, ATTR_CRED);
973 wpabuf_put_be16(msg, wpabuf_len(cred));
974 wpabuf_put_buf(msg, cred);
981 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
983 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
985 if (wps_build_credential(msg, &wps->cred))
992 static struct wpabuf * wps_build_m2(struct wps_data *wps)
996 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
998 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
999 wps->nonce_r, WPS_NONCE_LEN);
1000 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1002 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1003 msg = wpabuf_alloc(1000);
1007 if (wps_build_version(msg) ||
1008 wps_build_msg_type(msg, WPS_M2) ||
1009 wps_build_enrollee_nonce(wps, msg) ||
1010 wps_build_registrar_nonce(wps, msg) ||
1011 wps_build_uuid_r(wps, msg) ||
1012 wps_build_public_key(wps, msg) ||
1013 wps_derive_keys(wps) ||
1014 wps_build_auth_type_flags(wps, msg) ||
1015 wps_build_encr_type_flags(wps, msg) ||
1016 wps_build_conn_type_flags(wps, msg) ||
1017 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1018 wps_build_device_attrs(&wps->wps->dev, msg) ||
1019 wps_build_rf_bands(&wps->wps->dev, msg) ||
1020 wps_build_assoc_state(wps, msg) ||
1021 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1022 wps_build_dev_password_id(msg, DEV_PW_DEFAULT) ||
1023 wps_build_os_version(&wps->wps->dev, msg) ||
1024 wps_build_authenticator(wps, msg)) {
1029 wps->state = RECV_M3;
1034 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1037 u16 err = WPS_CFG_NO_ERROR;
1039 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1040 msg = wpabuf_alloc(1000);
1044 if (wps->wps->ap && wps->wps->ap_setup_locked)
1045 err = WPS_CFG_SETUP_LOCKED;
1047 if (wps_build_version(msg) ||
1048 wps_build_msg_type(msg, WPS_M2D) ||
1049 wps_build_enrollee_nonce(wps, msg) ||
1050 wps_build_registrar_nonce(wps, msg) ||
1051 wps_build_uuid_r(wps, msg) ||
1052 wps_build_auth_type_flags(wps, msg) ||
1053 wps_build_encr_type_flags(wps, msg) ||
1054 wps_build_conn_type_flags(wps, msg) ||
1055 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1056 wps_build_device_attrs(&wps->wps->dev, msg) ||
1057 wps_build_rf_bands(&wps->wps->dev, msg) ||
1058 wps_build_assoc_state(wps, msg) ||
1059 wps_build_config_error(msg, err) ||
1060 wps_build_os_version(&wps->wps->dev, msg)) {
1065 wps->state = RECV_M2D_ACK;
1070 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1072 struct wpabuf *msg, *plain;
1074 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1076 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1078 plain = wpabuf_alloc(200);
1082 msg = wpabuf_alloc(1000);
1088 if (wps_build_version(msg) ||
1089 wps_build_msg_type(msg, WPS_M4) ||
1090 wps_build_enrollee_nonce(wps, msg) ||
1091 wps_build_r_hash(wps, msg) ||
1092 wps_build_r_snonce1(wps, plain) ||
1093 wps_build_key_wrap_auth(wps, plain) ||
1094 wps_build_encr_settings(wps, msg, plain) ||
1095 wps_build_authenticator(wps, msg)) {
1102 wps->state = RECV_M5;
1107 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1109 struct wpabuf *msg, *plain;
1111 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1113 plain = wpabuf_alloc(200);
1117 msg = wpabuf_alloc(1000);
1123 if (wps_build_version(msg) ||
1124 wps_build_msg_type(msg, WPS_M6) ||
1125 wps_build_enrollee_nonce(wps, msg) ||
1126 wps_build_r_snonce2(wps, plain) ||
1127 wps_build_key_wrap_auth(wps, plain) ||
1128 wps_build_encr_settings(wps, msg, plain) ||
1129 wps_build_authenticator(wps, msg)) {
1136 wps->wps_pin_revealed = 1;
1137 wps->state = RECV_M7;
1142 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1144 struct wpabuf *msg, *plain;
1146 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1148 plain = wpabuf_alloc(500);
1152 msg = wpabuf_alloc(1000);
1158 if (wps_build_version(msg) ||
1159 wps_build_msg_type(msg, WPS_M8) ||
1160 wps_build_enrollee_nonce(wps, msg) ||
1161 (wps->wps->ap && wps_build_cred(wps, plain)) ||
1162 (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1163 wps_build_key_wrap_auth(wps, plain) ||
1164 wps_build_encr_settings(wps, msg, plain) ||
1165 wps_build_authenticator(wps, msg)) {
1172 wps->state = RECV_DONE;
1177 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1181 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1183 msg = wpabuf_alloc(1000);
1187 if (wps_build_version(msg) ||
1188 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1189 wps_build_enrollee_nonce(wps, msg) ||
1190 wps_build_registrar_nonce(wps, msg)) {
1199 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1203 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1205 msg = wpabuf_alloc(1000);
1209 if (wps_build_version(msg) ||
1210 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1211 wps_build_enrollee_nonce(wps, msg) ||
1212 wps_build_registrar_nonce(wps, msg) ||
1213 wps_build_config_error(msg, wps->config_error)) {
1222 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1223 enum wsc_op_code *op_code)
1227 switch (wps->state) {
1229 if (wps_get_dev_password(wps) < 0)
1230 msg = wps_build_m2d(wps);
1232 msg = wps_build_m2(wps);
1236 msg = wps_build_m2d(wps);
1240 msg = wps_build_m4(wps);
1244 msg = wps_build_m6(wps);
1248 msg = wps_build_m8(wps);
1252 msg = wps_build_wsc_ack(wps);
1256 msg = wps_build_wsc_nack(wps);
1257 *op_code = WSC_NACK;
1260 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1261 "a message", wps->state);
1266 if (*op_code == WSC_MSG && msg) {
1267 /* Save a copy of the last message for Authenticator derivation
1269 wpabuf_free(wps->last_msg);
1270 wps->last_msg = wpabuf_dup(msg);
1277 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1279 if (e_nonce == NULL) {
1280 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1284 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1285 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1286 wps->nonce_e, WPS_NONCE_LEN);
1292 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1294 if (r_nonce == NULL) {
1295 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1299 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1300 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1308 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1310 if (uuid_e == NULL) {
1311 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1315 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1316 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1322 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1324 if (pw_id == NULL) {
1325 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1329 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1330 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1336 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1338 if (e_hash1 == NULL) {
1339 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1343 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1344 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1350 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1352 if (e_hash2 == NULL) {
1353 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1357 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1358 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1364 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1366 u8 hash[SHA256_MAC_LEN];
1370 if (e_snonce1 == NULL) {
1371 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1375 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1376 WPS_SECRET_NONCE_LEN);
1378 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1379 addr[0] = e_snonce1;
1380 len[0] = WPS_SECRET_NONCE_LEN;
1381 addr[1] = wps->psk1;
1382 len[1] = WPS_PSK_LEN;
1383 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1384 len[2] = wpabuf_len(wps->dh_pubkey_e);
1385 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1386 len[3] = wpabuf_len(wps->dh_pubkey_r);
1387 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1389 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1390 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1391 "not match with the pre-committed value");
1392 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1396 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1397 "half of the device password");
1403 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1405 u8 hash[SHA256_MAC_LEN];
1409 if (e_snonce2 == NULL) {
1410 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1414 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1415 WPS_SECRET_NONCE_LEN);
1417 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1418 addr[0] = e_snonce2;
1419 len[0] = WPS_SECRET_NONCE_LEN;
1420 addr[1] = wps->psk2;
1421 len[1] = WPS_PSK_LEN;
1422 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1423 len[2] = wpabuf_len(wps->dh_pubkey_e);
1424 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1425 len[3] = wpabuf_len(wps->dh_pubkey_r);
1426 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1428 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1429 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1430 "not match with the pre-committed value");
1431 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1432 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1436 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1437 "half of the device password");
1438 wps->wps_pin_revealed = 0;
1439 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1445 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1447 if (mac_addr == NULL) {
1448 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1452 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1454 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1455 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1461 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1464 if (pk == NULL || pk_len == 0) {
1465 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1469 wpabuf_free(wps->dh_pubkey_e);
1470 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1471 if (wps->dh_pubkey_e == NULL)
1478 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1483 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1488 auth_types = WPA_GET_BE16(auth);
1490 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1492 wps->auth_type = wps->wps->auth_types & auth_types;
1493 if (wps->auth_type == 0) {
1494 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1495 "authentication types (own 0x%x Enrollee 0x%x)",
1496 wps->wps->auth_types, auth_types);
1504 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1509 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1514 encr_types = WPA_GET_BE16(encr);
1516 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1518 wps->encr_type = wps->wps->encr_types & encr_types;
1519 if (wps->encr_type == 0) {
1520 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1521 "encryption types");
1529 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1532 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1537 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1544 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1548 if (methods == NULL) {
1549 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1553 m = WPA_GET_BE16(methods);
1555 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1561 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1563 if (state == NULL) {
1564 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1569 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1576 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1580 if (assoc == NULL) {
1581 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1585 a = WPA_GET_BE16(assoc);
1586 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1592 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1597 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1601 e = WPA_GET_BE16(err);
1602 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1608 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1609 struct wps_parse_attr *attr)
1611 wpa_printf(MSG_DEBUG, "WPS: Received M1");
1613 if (wps->state != RECV_M1) {
1614 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1615 "receiving M1", wps->state);
1619 if (wps_process_uuid_e(wps, attr->uuid_e) ||
1620 wps_process_mac_addr(wps, attr->mac_addr) ||
1621 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1622 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1623 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1624 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1625 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1626 wps_process_config_methods(wps, attr->config_methods) ||
1627 wps_process_wps_state(wps, attr->wps_state) ||
1628 wps_process_device_attrs(&wps->peer_dev, attr) ||
1629 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1630 wps_process_assoc_state(wps, attr->assoc_state) ||
1631 wps_process_dev_password_id(wps, attr->dev_password_id) ||
1632 wps_process_config_error(wps, attr->config_error) ||
1633 wps_process_os_version(&wps->peer_dev, attr->os_version))
1636 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
1637 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1638 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1639 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1640 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
1641 !wps->wps->registrar->pbc)) {
1642 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1644 wps->state = SEND_M2D;
1645 return WPS_CONTINUE;
1648 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1649 if (wps_registrar_pbc_overlap(wps->wps->registrar,
1650 wps->mac_addr_e, wps->uuid_e)) {
1651 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1653 wps->state = SEND_M2D;
1654 return WPS_CONTINUE;
1656 wps_registrar_add_pbc_session(wps->wps->registrar,
1657 wps->mac_addr_e, wps->uuid_e);
1661 wps->state = SEND_M2;
1662 return WPS_CONTINUE;
1666 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1667 const struct wpabuf *msg,
1668 struct wps_parse_attr *attr)
1670 wpa_printf(MSG_DEBUG, "WPS: Received M3");
1672 if (wps->state != RECV_M3) {
1673 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1674 "receiving M3", wps->state);
1675 wps->state = SEND_WSC_NACK;
1676 return WPS_CONTINUE;
1679 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1680 wps_process_authenticator(wps, attr->authenticator, msg) ||
1681 wps_process_e_hash1(wps, attr->e_hash1) ||
1682 wps_process_e_hash2(wps, attr->e_hash2)) {
1683 wps->state = SEND_WSC_NACK;
1684 return WPS_CONTINUE;
1687 wps->state = SEND_M4;
1688 return WPS_CONTINUE;
1692 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1693 const struct wpabuf *msg,
1694 struct wps_parse_attr *attr)
1696 struct wpabuf *decrypted;
1697 struct wps_parse_attr eattr;
1699 wpa_printf(MSG_DEBUG, "WPS: Received M5");
1701 if (wps->state != RECV_M5) {
1702 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1703 "receiving M5", wps->state);
1704 wps->state = SEND_WSC_NACK;
1705 return WPS_CONTINUE;
1708 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1709 wps_process_authenticator(wps, attr->authenticator, msg)) {
1710 wps->state = SEND_WSC_NACK;
1711 return WPS_CONTINUE;
1714 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1715 attr->encr_settings_len);
1716 if (decrypted == NULL) {
1717 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1718 "Settings attribute");
1719 wps->state = SEND_WSC_NACK;
1720 return WPS_CONTINUE;
1723 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1725 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1726 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1727 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1728 wpabuf_free(decrypted);
1729 wps->state = SEND_WSC_NACK;
1730 return WPS_CONTINUE;
1732 wpabuf_free(decrypted);
1734 wps->state = SEND_M6;
1735 return WPS_CONTINUE;
1739 static int wps_process_ap_settings_r(struct wps_data *wps,
1740 struct wps_parse_attr *attr)
1745 /* AP Settings Attributes in M7 when Enrollee is an AP */
1746 if (wps_process_ap_settings(attr, &wps->cred) < 0)
1749 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1752 * TODO: Provide access to AP settings and allow changes before sending
1753 * out M8. For now, just copy the settings unchanged into M8.
1760 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1761 const struct wpabuf *msg,
1762 struct wps_parse_attr *attr)
1764 struct wpabuf *decrypted;
1765 struct wps_parse_attr eattr;
1767 wpa_printf(MSG_DEBUG, "WPS: Received M7");
1769 if (wps->state != RECV_M7) {
1770 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1771 "receiving M7", wps->state);
1772 wps->state = SEND_WSC_NACK;
1773 return WPS_CONTINUE;
1776 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1777 wps_process_authenticator(wps, attr->authenticator, msg)) {
1778 wps->state = SEND_WSC_NACK;
1779 return WPS_CONTINUE;
1782 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1783 attr->encr_settings_len);
1784 if (decrypted == NULL) {
1785 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1786 "Settings attribute");
1787 wps->state = SEND_WSC_NACK;
1788 return WPS_CONTINUE;
1791 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1793 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1794 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1795 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1796 wps_process_ap_settings_r(wps, &eattr)) {
1797 wpabuf_free(decrypted);
1798 wps->state = SEND_WSC_NACK;
1799 return WPS_CONTINUE;
1802 wpabuf_free(decrypted);
1804 wps->state = SEND_M8;
1805 return WPS_CONTINUE;
1809 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1810 const struct wpabuf *msg)
1812 struct wps_parse_attr attr;
1813 enum wps_process_res ret = WPS_CONTINUE;
1815 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1817 if (wps_parse_msg(msg, &attr) < 0)
1820 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1821 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1822 attr.version ? *attr.version : 0);
1826 if (attr.msg_type == NULL) {
1827 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1831 if (*attr.msg_type != WPS_M1 &&
1832 (attr.registrar_nonce == NULL ||
1833 os_memcmp(wps->nonce_r, attr.registrar_nonce,
1834 WPS_NONCE_LEN != 0))) {
1835 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1839 switch (*attr.msg_type) {
1841 ret = wps_process_m1(wps, &attr);
1844 ret = wps_process_m3(wps, msg, &attr);
1845 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1846 wps_fail_event(wps->wps, WPS_M3);
1849 ret = wps_process_m5(wps, msg, &attr);
1850 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1851 wps_fail_event(wps->wps, WPS_M5);
1854 ret = wps_process_m7(wps, msg, &attr);
1855 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1856 wps_fail_event(wps->wps, WPS_M7);
1859 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1864 if (ret == WPS_CONTINUE) {
1865 /* Save a copy of the last message for Authenticator derivation
1867 wpabuf_free(wps->last_msg);
1868 wps->last_msg = wpabuf_dup(msg);
1875 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1876 const struct wpabuf *msg)
1878 struct wps_parse_attr attr;
1880 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1882 if (wps_parse_msg(msg, &attr) < 0)
1885 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1886 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1887 attr.version ? *attr.version : 0);
1891 if (attr.msg_type == NULL) {
1892 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1896 if (*attr.msg_type != WPS_WSC_ACK) {
1897 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1902 if (attr.registrar_nonce == NULL ||
1903 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1905 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1909 if (attr.enrollee_nonce == NULL ||
1910 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1911 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1915 if (wps->state == RECV_M2D_ACK) {
1916 /* TODO: support for multiple registrars and sending of
1917 * multiple M2/M2D messages */
1919 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
1920 "terminate negotiation");
1927 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1928 const struct wpabuf *msg)
1930 struct wps_parse_attr attr;
1933 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1935 old_state = wps->state;
1936 wps->state = SEND_WSC_NACK;
1938 if (wps_parse_msg(msg, &attr) < 0)
1941 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1942 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1943 attr.version ? *attr.version : 0);
1947 if (attr.msg_type == NULL) {
1948 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1952 if (*attr.msg_type != WPS_WSC_NACK) {
1953 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1958 if (attr.registrar_nonce == NULL ||
1959 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1961 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1965 if (attr.enrollee_nonce == NULL ||
1966 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1967 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1971 if (attr.config_error == NULL) {
1972 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1977 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
1978 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1980 switch (old_state) {
1982 wps_fail_event(wps->wps, WPS_M2);
1985 wps_fail_event(wps->wps, WPS_M4);
1988 wps_fail_event(wps->wps, WPS_M6);
1991 wps_fail_event(wps->wps, WPS_M8);
2001 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2002 const struct wpabuf *msg)
2004 struct wps_parse_attr attr;
2006 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2008 if (wps->state != RECV_DONE) {
2009 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2010 "receiving WSC_Done", wps->state);
2014 if (wps_parse_msg(msg, &attr) < 0)
2017 if (attr.version == NULL || *attr.version != WPS_VERSION) {
2018 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2019 attr.version ? *attr.version : 0);
2023 if (attr.msg_type == NULL) {
2024 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2028 if (*attr.msg_type != WPS_WSC_DONE) {
2029 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2034 if (attr.registrar_nonce == NULL ||
2035 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2037 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2041 if (attr.enrollee_nonce == NULL ||
2042 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2043 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2047 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2049 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2051 struct wps_credential cred;
2053 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2054 "on first Enrollee connection");
2056 os_memset(&cred, 0, sizeof(cred));
2057 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2058 cred.ssid_len = wps->wps->ssid_len;
2059 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2060 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2061 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2062 cred.key_len = wps->new_psk_len;
2064 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2065 wpa_hexdump_ascii_key(MSG_DEBUG,
2066 "WPS: Generated random passphrase",
2067 wps->new_psk, wps->new_psk_len);
2068 if (wps->wps->cred_cb)
2069 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2071 os_free(wps->new_psk);
2072 wps->new_psk = NULL;
2075 if (!wps->wps->ap) {
2076 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
2077 "on the modified AP configuration");
2078 if (wps->wps->cred_cb)
2079 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2083 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2084 wps->new_psk, wps->new_psk_len)) {
2085 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2088 os_free(wps->new_psk);
2089 wps->new_psk = NULL;
2093 wps_registrar_remove_pbc_session(wps->wps->registrar,
2094 wps->mac_addr_e, wps->uuid_e);
2095 wps_registrar_pbc_completed(wps->wps->registrar);
2098 wps_success_event(wps->wps);
2104 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2105 enum wsc_op_code op_code,
2106 const struct wpabuf *msg)
2108 enum wps_process_res ret;
2110 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2112 (unsigned long) wpabuf_len(msg), op_code);
2116 return wps_process_wsc_msg(wps, msg);
2118 return wps_process_wsc_ack(wps, msg);
2120 return wps_process_wsc_nack(wps, msg);
2122 ret = wps_process_wsc_done(wps, msg);
2123 if (ret == WPS_FAILURE) {
2124 wps->state = SEND_WSC_NACK;
2125 wps_fail_event(wps->wps, WPS_WSC_DONE);
2129 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);