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 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->registrar, wps->uuid_e,
692 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
694 wps_cb_pin_needed(wps->registrar, wps->uuid_e, &wps->peer_dev);
698 wps->dev_password = os_malloc(pin_len);
699 if (wps->dev_password == NULL)
701 os_memcpy(wps->dev_password, pin, pin_len);
702 wps->dev_password_len = pin_len;
708 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
710 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
711 wpabuf_put_be16(msg, ATTR_UUID_R);
712 wpabuf_put_be16(msg, WPS_UUID_LEN);
713 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
718 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
724 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
726 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
727 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
728 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
730 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
731 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
732 "R-Hash derivation");
736 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
737 wpabuf_put_be16(msg, ATTR_R_HASH1);
738 wpabuf_put_be16(msg, SHA256_MAC_LEN);
739 hash = wpabuf_put(msg, SHA256_MAC_LEN);
740 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
741 addr[0] = wps->snonce;
742 len[0] = WPS_SECRET_NONCE_LEN;
744 len[1] = WPS_PSK_LEN;
745 addr[2] = wpabuf_head(wps->dh_pubkey_e);
746 len[2] = wpabuf_len(wps->dh_pubkey_e);
747 addr[3] = wpabuf_head(wps->dh_pubkey_r);
748 len[3] = wpabuf_len(wps->dh_pubkey_r);
749 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
750 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
752 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
753 wpabuf_put_be16(msg, ATTR_R_HASH2);
754 wpabuf_put_be16(msg, SHA256_MAC_LEN);
755 hash = wpabuf_put(msg, SHA256_MAC_LEN);
756 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
757 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
759 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
760 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
766 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
768 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
769 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
770 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
771 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
776 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
778 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
779 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
780 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
781 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
782 WPS_SECRET_NONCE_LEN);
787 static int wps_build_cred_network_idx(struct wpabuf *msg,
788 struct wps_credential *cred)
790 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
791 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
792 wpabuf_put_be16(msg, 1);
793 wpabuf_put_u8(msg, 0);
798 static int wps_build_cred_ssid(struct wpabuf *msg,
799 struct wps_credential *cred)
801 wpa_printf(MSG_DEBUG, "WPS: * SSID");
802 wpabuf_put_be16(msg, ATTR_SSID);
803 wpabuf_put_be16(msg, cred->ssid_len);
804 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
809 static int wps_build_cred_auth_type(struct wpabuf *msg,
810 struct wps_credential *cred)
812 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
814 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
815 wpabuf_put_be16(msg, 2);
816 wpabuf_put_be16(msg, cred->auth_type);
821 static int wps_build_cred_encr_type(struct wpabuf *msg,
822 struct wps_credential *cred)
824 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
826 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
827 wpabuf_put_be16(msg, 2);
828 wpabuf_put_be16(msg, cred->encr_type);
833 static int wps_build_cred_network_key(struct wpabuf *msg,
834 struct wps_credential *cred)
836 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
837 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
838 wpabuf_put_be16(msg, cred->key_len);
839 wpabuf_put_data(msg, cred->key, cred->key_len);
844 static int wps_build_cred_mac_addr(struct wpabuf *msg,
845 struct wps_credential *cred)
847 wpa_printf(MSG_DEBUG, "WPS: * MAC Address");
848 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
849 wpabuf_put_be16(msg, ETH_ALEN);
850 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
855 static int wps_build_credential(struct wpabuf *msg,
856 struct wps_credential *cred)
858 if (wps_build_cred_network_idx(msg, cred) ||
859 wps_build_cred_ssid(msg, cred) ||
860 wps_build_cred_auth_type(msg, cred) ||
861 wps_build_cred_encr_type(msg, cred) ||
862 wps_build_cred_network_key(msg, cred) ||
863 wps_build_cred_mac_addr(msg, cred))
869 static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
873 wpa_printf(MSG_DEBUG, "WPS: * Credential");
874 os_memset(&wps->cred, 0, sizeof(wps->cred));
876 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
877 wps->cred.ssid_len = wps->wps->ssid_len;
879 /* Select the best authentication and encryption type */
880 if (wps->auth_type & WPS_AUTH_WPA2PSK)
881 wps->auth_type = WPS_AUTH_WPA2PSK;
882 else if (wps->auth_type & WPS_AUTH_WPAPSK)
883 wps->auth_type = WPS_AUTH_WPAPSK;
884 else if (wps->auth_type & WPS_AUTH_OPEN)
885 wps->auth_type = WPS_AUTH_OPEN;
886 else if (wps->auth_type & WPS_AUTH_SHARED)
887 wps->auth_type = WPS_AUTH_SHARED;
889 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
893 wps->cred.auth_type = wps->auth_type;
895 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
896 wps->auth_type == WPS_AUTH_WPAPSK) {
897 if (wps->encr_type & WPS_ENCR_AES)
898 wps->encr_type = WPS_ENCR_AES;
899 else if (wps->encr_type & WPS_ENCR_TKIP)
900 wps->encr_type = WPS_ENCR_TKIP;
902 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
903 "type for WPA/WPA2");
907 if (wps->encr_type & WPS_ENCR_WEP)
908 wps->encr_type = WPS_ENCR_WEP;
909 else if (wps->encr_type & WPS_ENCR_NONE)
910 wps->encr_type = WPS_ENCR_NONE;
912 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
913 "type for non-WPA/WPA2 mode");
917 wps->cred.encr_type = wps->encr_type;
918 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
920 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap) {
922 /* Generate a random passphrase */
923 if (os_get_random(r, sizeof(r)) < 0)
925 os_free(wps->new_psk);
926 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
927 if (wps->new_psk == NULL)
929 wps->new_psk_len--; /* remove newline */
930 while (wps->new_psk_len &&
931 wps->new_psk[wps->new_psk_len - 1] == '=')
933 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
934 wps->new_psk, wps->new_psk_len);
935 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
936 wps->cred.key_len = wps->new_psk_len;
937 } else if (wps->wps->network_key) {
938 os_memcpy(wps->cred.key, wps->wps->network_key,
939 wps->wps->network_key_len);
940 wps->cred.key_len = wps->wps->network_key_len;
941 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
943 /* Generate a random per-device PSK */
944 os_free(wps->new_psk);
945 wps->new_psk_len = 32;
946 wps->new_psk = os_malloc(wps->new_psk_len);
947 if (wps->new_psk == NULL)
949 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
950 os_free(wps->new_psk);
954 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
955 wps->new_psk, wps->new_psk_len);
956 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
958 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
959 wps->cred.key_len = wps->new_psk_len * 2;
962 cred = wpabuf_alloc(200);
966 if (wps_build_credential(cred, &wps->cred)) {
971 wpabuf_put_be16(msg, ATTR_CRED);
972 wpabuf_put_be16(msg, wpabuf_len(cred));
973 wpabuf_put_buf(msg, cred);
980 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
982 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
984 if (wps_build_credential(msg, &wps->cred))
991 static struct wpabuf * wps_build_m2(struct wps_data *wps)
995 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
997 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
998 wps->nonce_r, WPS_NONCE_LEN);
999 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1001 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1002 msg = wpabuf_alloc(1000);
1006 if (wps_build_version(msg) ||
1007 wps_build_msg_type(msg, WPS_M2) ||
1008 wps_build_enrollee_nonce(wps, msg) ||
1009 wps_build_registrar_nonce(wps, msg) ||
1010 wps_build_uuid_r(wps, msg) ||
1011 wps_build_public_key(wps, msg) ||
1012 wps_derive_keys(wps) ||
1013 wps_build_auth_type_flags(wps, msg) ||
1014 wps_build_encr_type_flags(wps, msg) ||
1015 wps_build_conn_type_flags(wps, msg) ||
1016 wps_build_config_methods_r(wps->registrar, msg) ||
1017 wps_build_device_attrs(&wps->wps->dev, msg) ||
1018 wps_build_rf_bands(&wps->wps->dev, msg) ||
1019 wps_build_assoc_state(wps, msg) ||
1020 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1021 wps_build_dev_password_id(msg, DEV_PW_DEFAULT) ||
1022 wps_build_os_version(&wps->wps->dev, msg) ||
1023 wps_build_authenticator(wps, msg)) {
1028 wps->state = RECV_M3;
1033 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1036 u16 err = WPS_CFG_NO_ERROR;
1038 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1039 msg = wpabuf_alloc(1000);
1043 if (wps->authenticator && wps->wps->ap_setup_locked)
1044 err = WPS_CFG_SETUP_LOCKED;
1046 if (wps_build_version(msg) ||
1047 wps_build_msg_type(msg, WPS_M2D) ||
1048 wps_build_enrollee_nonce(wps, msg) ||
1049 wps_build_registrar_nonce(wps, msg) ||
1050 wps_build_uuid_r(wps, msg) ||
1051 wps_build_auth_type_flags(wps, msg) ||
1052 wps_build_encr_type_flags(wps, msg) ||
1053 wps_build_conn_type_flags(wps, msg) ||
1054 wps_build_config_methods_r(wps->registrar, msg) ||
1055 wps_build_device_attrs(&wps->wps->dev, msg) ||
1056 wps_build_rf_bands(&wps->wps->dev, msg) ||
1057 wps_build_assoc_state(wps, msg) ||
1058 wps_build_config_error(msg, err) ||
1059 wps_build_os_version(&wps->wps->dev, msg)) {
1064 wps->state = RECV_M2D_ACK;
1069 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1071 struct wpabuf *msg, *plain;
1073 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1075 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1077 plain = wpabuf_alloc(200);
1081 msg = wpabuf_alloc(1000);
1087 if (wps_build_version(msg) ||
1088 wps_build_msg_type(msg, WPS_M4) ||
1089 wps_build_enrollee_nonce(wps, msg) ||
1090 wps_build_r_hash(wps, msg) ||
1091 wps_build_r_snonce1(wps, plain) ||
1092 wps_build_key_wrap_auth(wps, plain) ||
1093 wps_build_encr_settings(wps, msg, plain) ||
1094 wps_build_authenticator(wps, msg)) {
1101 wps->state = RECV_M5;
1106 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1108 struct wpabuf *msg, *plain;
1110 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1112 plain = wpabuf_alloc(200);
1116 msg = wpabuf_alloc(1000);
1122 if (wps_build_version(msg) ||
1123 wps_build_msg_type(msg, WPS_M6) ||
1124 wps_build_enrollee_nonce(wps, msg) ||
1125 wps_build_r_snonce2(wps, plain) ||
1126 wps_build_key_wrap_auth(wps, plain) ||
1127 wps_build_encr_settings(wps, msg, plain) ||
1128 wps_build_authenticator(wps, msg)) {
1135 wps->wps_pin_revealed = 1;
1136 wps->state = RECV_M7;
1141 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1143 struct wpabuf *msg, *plain;
1145 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1147 plain = wpabuf_alloc(500);
1151 msg = wpabuf_alloc(1000);
1157 if (wps_build_version(msg) ||
1158 wps_build_msg_type(msg, WPS_M8) ||
1159 wps_build_enrollee_nonce(wps, msg) ||
1160 (wps->wps->ap && wps_build_cred(wps, plain)) ||
1161 (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1162 wps_build_key_wrap_auth(wps, plain) ||
1163 wps_build_encr_settings(wps, msg, plain) ||
1164 wps_build_authenticator(wps, msg)) {
1171 wps->state = RECV_DONE;
1176 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1180 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1182 msg = wpabuf_alloc(1000);
1186 if (wps_build_version(msg) ||
1187 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1188 wps_build_enrollee_nonce(wps, msg) ||
1189 wps_build_registrar_nonce(wps, msg)) {
1198 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1202 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1204 msg = wpabuf_alloc(1000);
1208 if (wps_build_version(msg) ||
1209 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1210 wps_build_enrollee_nonce(wps, msg) ||
1211 wps_build_registrar_nonce(wps, msg) ||
1212 wps_build_config_error(msg, wps->config_error)) {
1221 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1222 enum wsc_op_code *op_code)
1226 switch (wps->state) {
1228 if (wps_get_dev_password(wps) < 0)
1229 msg = wps_build_m2d(wps);
1231 msg = wps_build_m2(wps);
1235 msg = wps_build_m2d(wps);
1239 msg = wps_build_m4(wps);
1243 msg = wps_build_m6(wps);
1247 msg = wps_build_m8(wps);
1251 msg = wps_build_wsc_ack(wps);
1255 msg = wps_build_wsc_nack(wps);
1256 *op_code = WSC_NACK;
1259 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1260 "a message", wps->state);
1265 if (*op_code == WSC_MSG && msg) {
1266 /* Save a copy of the last message for Authenticator derivation
1268 wpabuf_free(wps->last_msg);
1269 wps->last_msg = wpabuf_dup(msg);
1276 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1278 if (e_nonce == NULL) {
1279 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1283 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1284 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1285 wps->nonce_e, WPS_NONCE_LEN);
1291 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1293 if (r_nonce == NULL) {
1294 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1298 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1299 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1307 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1309 if (uuid_e == NULL) {
1310 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1314 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1315 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1321 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1323 if (pw_id == NULL) {
1324 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1328 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1329 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1335 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1337 if (e_hash1 == NULL) {
1338 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1342 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1343 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1349 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1351 if (e_hash2 == NULL) {
1352 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1356 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1357 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1363 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1365 u8 hash[SHA256_MAC_LEN];
1369 if (e_snonce1 == NULL) {
1370 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1374 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1375 WPS_SECRET_NONCE_LEN);
1377 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1378 addr[0] = e_snonce1;
1379 len[0] = WPS_SECRET_NONCE_LEN;
1380 addr[1] = wps->psk1;
1381 len[1] = WPS_PSK_LEN;
1382 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1383 len[2] = wpabuf_len(wps->dh_pubkey_e);
1384 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1385 len[3] = wpabuf_len(wps->dh_pubkey_r);
1386 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1388 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1389 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1390 "not match with the pre-committed value");
1391 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1395 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1396 "half of the device password");
1402 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1404 u8 hash[SHA256_MAC_LEN];
1408 if (e_snonce2 == NULL) {
1409 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1413 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1414 WPS_SECRET_NONCE_LEN);
1416 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1417 addr[0] = e_snonce2;
1418 len[0] = WPS_SECRET_NONCE_LEN;
1419 addr[1] = wps->psk2;
1420 len[1] = WPS_PSK_LEN;
1421 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1422 len[2] = wpabuf_len(wps->dh_pubkey_e);
1423 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1424 len[3] = wpabuf_len(wps->dh_pubkey_r);
1425 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1427 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1428 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1429 "not match with the pre-committed value");
1430 wps_registrar_invalidate_pin(wps->registrar, wps->uuid_e);
1431 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1435 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1436 "half of the device password");
1437 wps->wps_pin_revealed = 0;
1438 wps_registrar_unlock_pin(wps->registrar, wps->uuid_e);
1444 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1446 if (mac_addr == NULL) {
1447 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1451 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1453 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1454 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1460 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1463 if (pk == NULL || pk_len == 0) {
1464 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1468 wpabuf_free(wps->dh_pubkey_e);
1469 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1470 if (wps->dh_pubkey_e == NULL)
1477 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1482 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1487 auth_types = WPA_GET_BE16(auth);
1489 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1491 wps->auth_type = wps->wps->auth_types & auth_types;
1492 if (wps->auth_type == 0) {
1493 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1494 "authentication types (own 0x%x Enrollee 0x%x)",
1495 wps->wps->auth_types, auth_types);
1503 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1508 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1513 encr_types = WPA_GET_BE16(encr);
1515 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1517 wps->encr_type = wps->wps->encr_types & encr_types;
1518 if (wps->encr_type == 0) {
1519 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1520 "encryption types");
1528 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1531 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1536 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1543 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1547 if (methods == NULL) {
1548 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1552 m = WPA_GET_BE16(methods);
1554 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1560 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1562 if (state == NULL) {
1563 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1568 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1575 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1579 if (assoc == NULL) {
1580 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1584 a = WPA_GET_BE16(assoc);
1585 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1591 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1596 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1600 e = WPA_GET_BE16(err);
1601 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1607 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1608 struct wps_parse_attr *attr)
1610 wpa_printf(MSG_DEBUG, "WPS: Received M1");
1612 if (wps->state != RECV_M1) {
1613 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1614 "receiving M1", wps->state);
1618 if (wps_process_uuid_e(wps, attr->uuid_e) ||
1619 wps_process_mac_addr(wps, attr->mac_addr) ||
1620 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1621 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1622 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1623 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1624 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1625 wps_process_config_methods(wps, attr->config_methods) ||
1626 wps_process_wps_state(wps, attr->wps_state) ||
1627 wps_process_device_attrs(&wps->peer_dev, attr) ||
1628 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1629 wps_process_assoc_state(wps, attr->assoc_state) ||
1630 wps_process_dev_password_id(wps, attr->dev_password_id) ||
1631 wps_process_config_error(wps, attr->config_error) ||
1632 wps_process_os_version(&wps->peer_dev, attr->os_version))
1635 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
1636 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1637 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1638 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1639 (wps->dev_pw_id != DEV_PW_PUSHBUTTON || !wps->registrar->pbc)) {
1640 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1642 wps->state = SEND_M2D;
1643 return WPS_CONTINUE;
1646 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1647 if (wps_registrar_pbc_overlap(wps->registrar, wps->mac_addr_e,
1649 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1651 wps->state = SEND_M2D;
1652 return WPS_CONTINUE;
1654 wps_registrar_add_pbc_session(wps->registrar, wps->mac_addr_e,
1659 wps->state = SEND_M2;
1660 return WPS_CONTINUE;
1664 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1665 const struct wpabuf *msg,
1666 struct wps_parse_attr *attr)
1668 wpa_printf(MSG_DEBUG, "WPS: Received M3");
1670 if (wps->state != RECV_M3) {
1671 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1672 "receiving M3", wps->state);
1673 wps->state = SEND_WSC_NACK;
1674 return WPS_CONTINUE;
1677 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1678 wps_process_authenticator(wps, attr->authenticator, msg) ||
1679 wps_process_e_hash1(wps, attr->e_hash1) ||
1680 wps_process_e_hash2(wps, attr->e_hash2)) {
1681 wps->state = SEND_WSC_NACK;
1682 return WPS_CONTINUE;
1685 wps->state = SEND_M4;
1686 return WPS_CONTINUE;
1690 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1691 const struct wpabuf *msg,
1692 struct wps_parse_attr *attr)
1694 struct wpabuf *decrypted;
1695 struct wps_parse_attr eattr;
1697 wpa_printf(MSG_DEBUG, "WPS: Received M5");
1699 if (wps->state != RECV_M5) {
1700 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1701 "receiving M5", wps->state);
1702 wps->state = SEND_WSC_NACK;
1703 return WPS_CONTINUE;
1706 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1707 wps_process_authenticator(wps, attr->authenticator, msg)) {
1708 wps->state = SEND_WSC_NACK;
1709 return WPS_CONTINUE;
1712 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1713 attr->encr_settings_len);
1714 if (decrypted == NULL) {
1715 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1716 "Settings attribute");
1717 wps->state = SEND_WSC_NACK;
1718 return WPS_CONTINUE;
1721 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1723 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1724 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1725 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1726 wpabuf_free(decrypted);
1727 wps->state = SEND_WSC_NACK;
1728 return WPS_CONTINUE;
1730 wpabuf_free(decrypted);
1732 wps->state = SEND_M6;
1733 return WPS_CONTINUE;
1737 static int wps_process_ap_settings_r(struct wps_data *wps,
1738 struct wps_parse_attr *attr)
1743 /* AP Settings Attributes in M7 when Enrollee is an AP */
1744 if (wps_process_ap_settings(attr, &wps->cred) < 0)
1747 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1750 * TODO: Provide access to AP settings and allow changes before sending
1751 * out M8. For now, just copy the settings unchanged into M8.
1758 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1759 const struct wpabuf *msg,
1760 struct wps_parse_attr *attr)
1762 struct wpabuf *decrypted;
1763 struct wps_parse_attr eattr;
1765 wpa_printf(MSG_DEBUG, "WPS: Received M7");
1767 if (wps->state != RECV_M7) {
1768 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1769 "receiving M7", wps->state);
1770 wps->state = SEND_WSC_NACK;
1771 return WPS_CONTINUE;
1774 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1775 wps_process_authenticator(wps, attr->authenticator, msg)) {
1776 wps->state = SEND_WSC_NACK;
1777 return WPS_CONTINUE;
1780 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1781 attr->encr_settings_len);
1782 if (decrypted == NULL) {
1783 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1784 "Settings attribute");
1785 wps->state = SEND_WSC_NACK;
1786 return WPS_CONTINUE;
1789 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1791 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1792 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1793 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1794 wps_process_ap_settings_r(wps, &eattr)) {
1795 wpabuf_free(decrypted);
1796 wps->state = SEND_WSC_NACK;
1797 return WPS_CONTINUE;
1800 wpabuf_free(decrypted);
1802 wps->state = SEND_M8;
1803 return WPS_CONTINUE;
1807 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1808 const struct wpabuf *msg)
1810 struct wps_parse_attr attr;
1811 enum wps_process_res ret = WPS_CONTINUE;
1813 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1815 if (wps_parse_msg(msg, &attr) < 0)
1818 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1819 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1820 attr.version ? *attr.version : 0);
1824 if (attr.msg_type == NULL) {
1825 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1829 if (*attr.msg_type != WPS_M1 &&
1830 (attr.registrar_nonce == NULL ||
1831 os_memcmp(wps->nonce_r, attr.registrar_nonce,
1832 WPS_NONCE_LEN != 0))) {
1833 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1837 switch (*attr.msg_type) {
1839 ret = wps_process_m1(wps, &attr);
1842 ret = wps_process_m3(wps, msg, &attr);
1843 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1844 wps_fail_event(wps->wps, WPS_M3);
1847 ret = wps_process_m5(wps, msg, &attr);
1848 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1849 wps_fail_event(wps->wps, WPS_M5);
1852 ret = wps_process_m7(wps, msg, &attr);
1853 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1854 wps_fail_event(wps->wps, WPS_M7);
1857 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1862 if (ret == WPS_CONTINUE) {
1863 /* Save a copy of the last message for Authenticator derivation
1865 wpabuf_free(wps->last_msg);
1866 wps->last_msg = wpabuf_dup(msg);
1873 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1874 const struct wpabuf *msg)
1876 struct wps_parse_attr attr;
1878 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1880 if (wps_parse_msg(msg, &attr) < 0)
1883 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1884 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1885 attr.version ? *attr.version : 0);
1889 if (attr.msg_type == NULL) {
1890 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1894 if (*attr.msg_type != WPS_WSC_ACK) {
1895 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1900 if (attr.registrar_nonce == NULL ||
1901 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1903 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1907 if (attr.enrollee_nonce == NULL ||
1908 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1909 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1913 if (wps->state == RECV_M2D_ACK) {
1914 /* TODO: support for multiple registrars and sending of
1915 * multiple M2/M2D messages */
1917 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
1918 "terminate negotiation");
1925 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1926 const struct wpabuf *msg)
1928 struct wps_parse_attr attr;
1931 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1933 old_state = wps->state;
1934 wps->state = SEND_WSC_NACK;
1936 if (wps_parse_msg(msg, &attr) < 0)
1939 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1940 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1941 attr.version ? *attr.version : 0);
1945 if (attr.msg_type == NULL) {
1946 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1950 if (*attr.msg_type != WPS_WSC_NACK) {
1951 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1956 if (attr.registrar_nonce == NULL ||
1957 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1959 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1963 if (attr.enrollee_nonce == NULL ||
1964 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1965 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1969 if (attr.config_error == NULL) {
1970 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1975 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
1976 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1978 switch (old_state) {
1980 wps_fail_event(wps->wps, WPS_M2);
1983 wps_fail_event(wps->wps, WPS_M4);
1986 wps_fail_event(wps->wps, WPS_M6);
1989 wps_fail_event(wps->wps, WPS_M8);
1999 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2000 const struct wpabuf *msg)
2002 struct wps_parse_attr attr;
2004 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2006 if (wps->state != RECV_DONE) {
2007 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2008 "receiving WSC_Done", wps->state);
2012 if (wps_parse_msg(msg, &attr) < 0)
2015 if (attr.version == NULL || *attr.version != WPS_VERSION) {
2016 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2017 attr.version ? *attr.version : 0);
2021 if (attr.msg_type == NULL) {
2022 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2026 if (*attr.msg_type != WPS_WSC_DONE) {
2027 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2032 if (attr.registrar_nonce == NULL ||
2033 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2035 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2039 if (attr.enrollee_nonce == NULL ||
2040 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2041 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2045 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2047 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2049 struct wps_credential cred;
2051 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2052 "on first Enrollee connection");
2054 os_memset(&cred, 0, sizeof(cred));
2055 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2056 cred.ssid_len = wps->wps->ssid_len;
2057 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2058 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2059 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2060 cred.key_len = wps->new_psk_len;
2062 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2063 wpa_hexdump_ascii_key(MSG_DEBUG,
2064 "WPS: Generated random passphrase",
2065 wps->new_psk, wps->new_psk_len);
2066 if (wps->wps->cred_cb)
2067 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2069 os_free(wps->new_psk);
2070 wps->new_psk = NULL;
2073 if (!wps->wps->ap) {
2074 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
2075 "on the modified AP configuration");
2076 if (wps->wps->cred_cb)
2077 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2081 if (wps_cb_new_psk(wps->registrar, wps->mac_addr_e,
2082 wps->new_psk, wps->new_psk_len)) {
2083 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2086 os_free(wps->new_psk);
2087 wps->new_psk = NULL;
2091 wps_registrar_remove_pbc_session(wps->registrar,
2092 wps->mac_addr_e, wps->uuid_e);
2093 wps_registrar_pbc_completed(wps->registrar);
2096 wps_success_event(wps->wps);
2102 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2103 enum wsc_op_code op_code,
2104 const struct wpabuf *msg)
2106 enum wps_process_res ret;
2108 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2110 (unsigned long) wpabuf_len(msg), op_code);
2114 return wps_process_wsc_msg(wps, msg);
2116 return wps_process_wsc_ack(wps, msg);
2118 return wps_process_wsc_nack(wps, msg);
2120 ret = wps_process_wsc_done(wps, msg);
2121 if (ret == WPS_FAILURE) {
2122 wps->state = SEND_WSC_NACK;
2123 wps_fail_event(wps->wps, WPS_WSC_DONE);
2127 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);