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];
35 static void wps_free_pin(struct wps_uuid_pin *pin)
42 static void wps_free_pins(struct wps_uuid_pin *pins)
44 struct wps_uuid_pin *pin, *prev;
55 struct wps_pbc_session {
56 struct wps_pbc_session *next;
58 u8 uuid_e[WPS_UUID_LEN];
59 struct os_time timestamp;
63 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
65 struct wps_pbc_session *prev;
75 struct wps_registrar {
76 struct wps_context *wps;
79 int selected_registrar;
81 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
83 int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
84 const u8 *probe_resp_ie, size_t probe_resp_ie_len);
85 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
86 const struct wps_device_data *dev);
89 struct wps_uuid_pin *pins;
90 struct wps_pbc_session *pbc_sessions;
94 static int wps_set_ie(struct wps_registrar *reg);
95 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
98 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
99 const u8 *addr, const u8 *uuid_e)
101 struct wps_pbc_session *pbc, *prev = NULL;
106 pbc = reg->pbc_sessions;
108 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
109 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
111 prev->next = pbc->next;
113 reg->pbc_sessions = pbc->next;
121 pbc = os_zalloc(sizeof(*pbc));
124 os_memcpy(pbc->addr, addr, ETH_ALEN);
126 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
129 pbc->next = reg->pbc_sessions;
130 reg->pbc_sessions = pbc;
131 pbc->timestamp = now;
133 /* remove entries that have timed out */
138 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
140 wps_free_pbc_sessions(pbc);
149 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
150 const u8 *addr, const u8 *uuid_e)
152 struct wps_pbc_session *pbc, *prev = NULL;
154 pbc = reg->pbc_sessions;
156 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
157 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
159 prev->next = pbc->next;
161 reg->pbc_sessions = pbc->next;
171 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
172 const u8 *addr, const u8 *uuid_e)
175 struct wps_pbc_session *pbc;
180 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
181 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
183 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
185 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
192 return count > 1 ? 1 : 0;
196 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
198 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
200 wpabuf_put_be16(msg, ATTR_WPS_STATE);
201 wpabuf_put_be16(msg, 1);
202 wpabuf_put_u8(msg, wps->wps_state);
207 static int wps_build_ap_setup_locked(struct wps_context *wps,
210 if (wps->ap_setup_locked) {
211 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
212 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
213 wpabuf_put_be16(msg, 1);
214 wpabuf_put_u8(msg, 1);
220 static int wps_build_selected_registrar(struct wps_registrar *reg,
223 if (!reg->selected_registrar)
225 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
226 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
227 wpabuf_put_be16(msg, 1);
228 wpabuf_put_u8(msg, 1);
233 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
236 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
237 if (!reg->selected_registrar)
239 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
240 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
241 wpabuf_put_be16(msg, 2);
242 wpabuf_put_be16(msg, id);
247 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
251 if (!reg->selected_registrar)
253 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
255 methods |= WPS_CONFIG_PUSHBUTTON;
256 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
258 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
259 wpabuf_put_be16(msg, 2);
260 wpabuf_put_be16(msg, methods);
265 static int wps_build_probe_config_methods(struct wps_registrar *reg,
270 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
271 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
272 wpabuf_put_be16(msg, 2);
273 wpabuf_put_be16(msg, methods);
278 static int wps_build_config_methods(struct wps_registrar *reg,
282 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
284 methods |= WPS_CONFIG_PUSHBUTTON;
285 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
286 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
287 wpabuf_put_be16(msg, 2);
288 wpabuf_put_be16(msg, methods);
293 static int wps_build_rf_bands(struct wps_registrar *reg, struct wpabuf *msg)
295 u8 bands = WPS_RF_24GHZ /* TODO: | WPS_RF_50GHZ */;
296 wpa_printf(MSG_DEBUG, "WPS: * RF Bands (%x)", bands);
297 wpabuf_put_be16(msg, ATTR_RF_BANDS);
298 wpabuf_put_be16(msg, 1);
299 wpabuf_put_u8(msg, bands);
304 static int wps_build_uuid_e(struct wps_registrar *reg, struct wpabuf *msg)
306 wpa_printf(MSG_DEBUG, "WPS: * UUID-E");
307 wpabuf_put_be16(msg, ATTR_UUID_E);
308 wpabuf_put_be16(msg, WPS_UUID_LEN);
309 wpabuf_put_data(msg, reg->wps->uuid, WPS_UUID_LEN);
314 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
316 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
317 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
318 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
319 wpabuf_put_be16(msg, 1);
320 wpabuf_put_u8(msg, resp);
325 struct wps_registrar *
326 wps_registrar_init(struct wps_context *wps,
327 const struct wps_registrar_config *cfg)
329 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
334 reg->new_psk_cb = cfg->new_psk_cb;
335 reg->set_ie_cb = cfg->set_ie_cb;
336 reg->pin_needed_cb = cfg->pin_needed_cb;
337 reg->cb_ctx = cfg->cb_ctx;
339 if (wps_set_ie(reg)) {
340 wps_registrar_deinit(reg);
348 void wps_registrar_deinit(struct wps_registrar *reg)
352 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
353 wps_free_pins(reg->pins);
354 wps_free_pbc_sessions(reg->pbc_sessions);
359 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
360 const u8 *pin, size_t pin_len)
362 struct wps_uuid_pin *p;
364 p = os_zalloc(sizeof(*p));
367 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
368 p->pin = os_malloc(pin_len);
369 if (p->pin == NULL) {
373 os_memcpy(p->pin, pin, pin_len);
374 p->pin_len = pin_len;
379 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured");
380 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
381 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
382 reg->selected_registrar = 1;
390 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
392 struct wps_uuid_pin *pin, *prev;
397 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
399 reg->pins = pin->next;
401 prev->next = pin->next;
402 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
403 pin->uuid, WPS_UUID_LEN);
415 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
416 const u8 *uuid, size_t *pin_len)
418 struct wps_uuid_pin *pin;
422 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
424 * Lock the PIN to avoid attacks based on concurrent
425 * re-use of the PIN that could otherwise avoid PIN
429 wpa_printf(MSG_DEBUG, "WPS: Selected PIN "
430 "locked - do not allow concurrent "
434 *pin_len = pin->pin_len;
445 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
447 struct wps_uuid_pin *pin;
451 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
462 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
464 struct wps_registrar *reg = eloop_ctx;
466 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
467 reg->selected_registrar = 0;
473 int wps_registrar_button_pushed(struct wps_registrar *reg)
475 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
476 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
480 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
481 reg->selected_registrar = 1;
485 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
486 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
492 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
494 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
495 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
496 reg->selected_registrar = 0;
502 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
503 const struct wpabuf *wps_data)
505 struct wps_parse_attr attr;
508 wpa_hexdump_buf(MSG_MSGDUMP,
509 "WPS: Probe Request with WPS data received",
512 if (wps_parse_msg(wps_data, &attr) < 0 ||
513 attr.version == NULL || *attr.version != WPS_VERSION) {
514 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
515 "version 0x%x", attr.version ? *attr.version : 0);
519 if (attr.config_methods == NULL) {
520 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
525 methods = WPA_GET_BE16(attr.config_methods);
526 if (!(methods & WPS_CONFIG_PUSHBUTTON))
527 return; /* Not PBC */
529 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
530 MACSTR, MAC2STR(addr));
532 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
536 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
537 const u8 *psk, size_t psk_len)
539 if (reg->new_psk_cb == NULL)
542 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
546 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
547 const struct wps_device_data *dev)
549 if (reg->pin_needed_cb == NULL)
552 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
556 static int wps_cb_set_ie(struct wps_registrar *reg,
557 const struct wpabuf *beacon_ie,
558 const struct wpabuf *probe_resp_ie)
560 if (reg->set_ie_cb == NULL)
563 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
564 wpabuf_len(beacon_ie),
565 wpabuf_head(probe_resp_ie),
566 wpabuf_len(probe_resp_ie));
570 static int wps_set_ie(struct wps_registrar *reg)
572 struct wpabuf *beacon;
573 struct wpabuf *probe;
577 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
579 beacon = wpabuf_alloc(300);
582 probe = wpabuf_alloc(300);
588 wpabuf_put_u8(beacon, WLAN_EID_VENDOR_SPECIFIC);
589 blen = wpabuf_put(beacon, 1);
590 wpabuf_put_be32(beacon, WPS_DEV_OUI_WFA);
592 wpabuf_put_u8(probe, WLAN_EID_VENDOR_SPECIFIC);
593 plen = wpabuf_put(probe, 1);
594 wpabuf_put_be32(probe, WPS_DEV_OUI_WFA);
596 if (wps_build_version(beacon) ||
597 wps_build_wps_state(reg->wps, beacon) ||
598 wps_build_ap_setup_locked(reg->wps, beacon) ||
599 wps_build_selected_registrar(reg, beacon) ||
600 wps_build_sel_reg_dev_password_id(reg, beacon) ||
601 wps_build_sel_reg_config_methods(reg, beacon) ||
602 wps_build_version(probe) ||
603 wps_build_wps_state(reg->wps, probe) ||
604 wps_build_ap_setup_locked(reg->wps, probe) ||
605 wps_build_selected_registrar(reg, probe) ||
606 wps_build_sel_reg_dev_password_id(reg, probe) ||
607 wps_build_sel_reg_config_methods(reg, probe) ||
608 wps_build_resp_type(reg, probe) ||
609 wps_build_uuid_e(reg, probe) ||
610 wps_build_device_attrs(®->wps->dev, probe) ||
611 wps_build_probe_config_methods(reg, probe) ||
612 wps_build_rf_bands(reg, probe)) {
618 *blen = wpabuf_len(beacon) - 2;
619 *plen = wpabuf_len(probe) - 2;
621 ret = wps_cb_set_ie(reg, beacon, probe);
629 static int wps_get_dev_password(struct wps_data *wps)
634 os_free(wps->dev_password);
635 wps->dev_password = NULL;
638 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
639 pin = (const u8 *) "00000000";
642 pin = wps_registrar_get_pin(wps->registrar, wps->uuid_e,
646 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
648 wps_cb_pin_needed(wps->registrar, wps->uuid_e, &wps->peer_dev);
652 wps->dev_password = os_malloc(pin_len);
653 if (wps->dev_password == NULL)
655 os_memcpy(wps->dev_password, pin, pin_len);
656 wps->dev_password_len = pin_len;
662 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
664 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
665 wpabuf_put_be16(msg, ATTR_UUID_R);
666 wpabuf_put_be16(msg, WPS_UUID_LEN);
667 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
672 static int wps_build_dev_password_id(struct wps_data *wps, struct wpabuf *msg)
674 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID");
675 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
676 wpabuf_put_be16(msg, 2);
677 wpabuf_put_be16(msg, DEV_PW_DEFAULT);
682 static int wps_build_config_error(struct wps_data *wps, struct wpabuf *msg)
684 wpa_printf(MSG_DEBUG, "WPS: * Configuration Error");
685 wpabuf_put_be16(msg, ATTR_CONFIG_ERROR);
686 wpabuf_put_be16(msg, 2);
687 wpabuf_put_be16(msg, WPS_CFG_NO_ERROR);
692 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
698 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
700 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
701 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
702 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
704 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
705 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
706 "R-Hash derivation");
710 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
711 wpabuf_put_be16(msg, ATTR_R_HASH1);
712 wpabuf_put_be16(msg, SHA256_MAC_LEN);
713 hash = wpabuf_put(msg, SHA256_MAC_LEN);
714 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
715 addr[0] = wps->snonce;
716 len[0] = WPS_SECRET_NONCE_LEN;
718 len[1] = WPS_PSK_LEN;
719 addr[2] = wpabuf_head(wps->dh_pubkey_e);
720 len[2] = wpabuf_len(wps->dh_pubkey_e);
721 addr[3] = wpabuf_head(wps->dh_pubkey_r);
722 len[3] = wpabuf_len(wps->dh_pubkey_r);
723 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
724 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
726 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
727 wpabuf_put_be16(msg, ATTR_R_HASH2);
728 wpabuf_put_be16(msg, SHA256_MAC_LEN);
729 hash = wpabuf_put(msg, SHA256_MAC_LEN);
730 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
731 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
733 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
734 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
740 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
742 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
743 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
744 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
745 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
750 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
752 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
753 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
754 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
755 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
756 WPS_SECRET_NONCE_LEN);
761 static int wps_build_cred_network_idx(struct wpabuf *msg,
762 struct wps_credential *cred)
764 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
765 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
766 wpabuf_put_be16(msg, 1);
767 wpabuf_put_u8(msg, 0);
772 static int wps_build_cred_ssid(struct wpabuf *msg,
773 struct wps_credential *cred)
775 wpa_printf(MSG_DEBUG, "WPS: * SSID");
776 wpabuf_put_be16(msg, ATTR_SSID);
777 wpabuf_put_be16(msg, cred->ssid_len);
778 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
783 static int wps_build_cred_auth_type(struct wpabuf *msg,
784 struct wps_credential *cred)
786 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
788 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
789 wpabuf_put_be16(msg, 2);
790 wpabuf_put_be16(msg, cred->auth_type);
795 static int wps_build_cred_encr_type(struct wpabuf *msg,
796 struct wps_credential *cred)
798 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
800 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
801 wpabuf_put_be16(msg, 2);
802 wpabuf_put_be16(msg, cred->encr_type);
807 static int wps_build_cred_network_key(struct wpabuf *msg,
808 struct wps_credential *cred)
810 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
811 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
812 wpabuf_put_be16(msg, cred->key_len);
813 wpabuf_put_data(msg, cred->key, cred->key_len);
818 static int wps_build_cred_mac_addr(struct wpabuf *msg,
819 struct wps_credential *cred)
821 wpa_printf(MSG_DEBUG, "WPS: * MAC Address");
822 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
823 wpabuf_put_be16(msg, ETH_ALEN);
824 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
829 static int wps_build_credential(struct wpabuf *msg,
830 struct wps_credential *cred)
832 if (wps_build_cred_network_idx(msg, cred) ||
833 wps_build_cred_ssid(msg, cred) ||
834 wps_build_cred_auth_type(msg, cred) ||
835 wps_build_cred_encr_type(msg, cred) ||
836 wps_build_cred_network_key(msg, cred) ||
837 wps_build_cred_mac_addr(msg, cred))
843 static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
847 wpa_printf(MSG_DEBUG, "WPS: * Credential");
848 os_memset(&wps->cred, 0, sizeof(wps->cred));
850 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
851 wps->cred.ssid_len = wps->wps->ssid_len;
853 /* Select the best authentication and encryption type */
854 if (wps->auth_type & WPS_AUTH_WPA2PSK)
855 wps->auth_type = WPS_AUTH_WPA2PSK;
856 else if (wps->auth_type & WPS_AUTH_WPAPSK)
857 wps->auth_type = WPS_AUTH_WPAPSK;
858 else if (wps->auth_type & WPS_AUTH_OPEN)
859 wps->auth_type = WPS_AUTH_OPEN;
860 else if (wps->auth_type & WPS_AUTH_SHARED)
861 wps->auth_type = WPS_AUTH_SHARED;
863 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
867 wps->cred.auth_type = wps->auth_type;
869 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
870 wps->auth_type == WPS_AUTH_WPAPSK) {
871 if (wps->encr_type & WPS_ENCR_AES)
872 wps->encr_type = WPS_ENCR_AES;
873 else if (wps->encr_type & WPS_ENCR_TKIP)
874 wps->encr_type = WPS_ENCR_TKIP;
876 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
877 "type for WPA/WPA2");
881 if (wps->encr_type & WPS_ENCR_WEP)
882 wps->encr_type = WPS_ENCR_WEP;
883 else if (wps->encr_type & WPS_ENCR_NONE)
884 wps->encr_type = WPS_ENCR_NONE;
886 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
887 "type for non-WPA/WPA2 mode");
891 wps->cred.encr_type = wps->encr_type;
892 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
894 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap) {
896 /* Generate a random passphrase */
897 if (os_get_random(r, sizeof(r)) < 0)
899 os_free(wps->new_psk);
900 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
901 if (wps->new_psk == NULL)
903 wps->new_psk_len--; /* remove newline */
904 while (wps->new_psk_len &&
905 wps->new_psk[wps->new_psk_len - 1] == '=')
907 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
908 wps->new_psk, wps->new_psk_len);
909 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
910 wps->cred.key_len = wps->new_psk_len;
911 } else if (wps->wps->network_key) {
912 os_memcpy(wps->cred.key, wps->wps->network_key,
913 wps->wps->network_key_len);
914 wps->cred.key_len = wps->wps->network_key_len;
915 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
917 /* Generate a random per-device PSK */
918 os_free(wps->new_psk);
919 wps->new_psk_len = 32;
920 wps->new_psk = os_malloc(wps->new_psk_len);
921 if (wps->new_psk == NULL)
923 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
924 os_free(wps->new_psk);
928 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
929 wps->new_psk, wps->new_psk_len);
930 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
932 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
933 wps->cred.key_len = wps->new_psk_len * 2;
936 cred = wpabuf_alloc(200);
940 if (wps_build_credential(cred, &wps->cred)) {
945 wpabuf_put_be16(msg, ATTR_CRED);
946 wpabuf_put_be16(msg, wpabuf_len(cred));
947 wpabuf_put_buf(msg, cred);
954 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
956 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
958 if (wps_build_credential(msg, &wps->cred))
965 static struct wpabuf * wps_build_m2(struct wps_data *wps)
969 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
971 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
972 wps->nonce_r, WPS_NONCE_LEN);
973 os_memcpy(wps->uuid_r, wps->wps->uuid, WPS_UUID_LEN);
974 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
976 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
977 msg = wpabuf_alloc(1000);
981 if (wps_build_version(msg) ||
982 wps_build_msg_type(msg, WPS_M2) ||
983 wps_build_enrollee_nonce(wps, msg) ||
984 wps_build_registrar_nonce(wps, msg) ||
985 wps_build_uuid_r(wps, msg) ||
986 wps_build_public_key(wps, msg) ||
987 wps_derive_keys(wps) ||
988 wps_build_auth_type_flags(wps, msg) ||
989 wps_build_encr_type_flags(wps, msg) ||
990 wps_build_conn_type_flags(wps, msg) ||
991 wps_build_config_methods(wps->registrar, msg) ||
992 wps_build_device_attrs(&wps->wps->dev, msg) ||
993 wps_build_rf_bands(wps->registrar, msg) ||
994 wps_build_assoc_state(wps, msg) ||
995 wps_build_config_error(wps, msg) ||
996 wps_build_dev_password_id(wps, msg) ||
997 wps_build_os_version(&wps->wps->dev, msg) ||
998 wps_build_authenticator(wps, msg)) {
1003 wps->state = RECV_M3;
1008 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1012 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1013 msg = wpabuf_alloc(1000);
1017 if (wps_build_version(msg) ||
1018 wps_build_msg_type(msg, WPS_M2D) ||
1019 wps_build_enrollee_nonce(wps, msg) ||
1020 wps_build_registrar_nonce(wps, msg) ||
1021 wps_build_uuid_r(wps, msg) ||
1022 wps_build_auth_type_flags(wps, msg) ||
1023 wps_build_encr_type_flags(wps, msg) ||
1024 wps_build_conn_type_flags(wps, msg) ||
1025 wps_build_config_methods(wps->registrar, msg) ||
1026 wps_build_device_attrs(&wps->wps->dev, msg) ||
1027 wps_build_rf_bands(wps->registrar, msg) ||
1028 wps_build_assoc_state(wps, msg) ||
1029 wps_build_config_error(wps, msg) ||
1030 wps_build_os_version(&wps->wps->dev, msg)) {
1035 wps->state = RECV_M2D_ACK;
1040 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1042 struct wpabuf *msg, *plain;
1044 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1046 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1048 plain = wpabuf_alloc(200);
1052 msg = wpabuf_alloc(1000);
1058 if (wps_build_version(msg) ||
1059 wps_build_msg_type(msg, WPS_M4) ||
1060 wps_build_enrollee_nonce(wps, msg) ||
1061 wps_build_r_hash(wps, msg) ||
1062 wps_build_r_snonce1(wps, plain) ||
1063 wps_build_key_wrap_auth(wps, plain) ||
1064 wps_build_encr_settings(wps, msg, plain) ||
1065 wps_build_authenticator(wps, msg)) {
1072 wps->state = RECV_M5;
1077 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1079 struct wpabuf *msg, *plain;
1081 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1083 plain = wpabuf_alloc(200);
1087 msg = wpabuf_alloc(1000);
1093 if (wps_build_version(msg) ||
1094 wps_build_msg_type(msg, WPS_M6) ||
1095 wps_build_enrollee_nonce(wps, msg) ||
1096 wps_build_r_snonce2(wps, plain) ||
1097 wps_build_key_wrap_auth(wps, plain) ||
1098 wps_build_encr_settings(wps, msg, plain) ||
1099 wps_build_authenticator(wps, msg)) {
1106 wps->wps_pin_revealed = 1;
1107 wps->state = RECV_M7;
1112 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1114 struct wpabuf *msg, *plain;
1116 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1118 plain = wpabuf_alloc(500);
1122 msg = wpabuf_alloc(1000);
1128 if (wps_build_version(msg) ||
1129 wps_build_msg_type(msg, WPS_M8) ||
1130 wps_build_enrollee_nonce(wps, msg) ||
1131 (wps->wps->ap && wps_build_cred(wps, plain)) ||
1132 (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1133 wps_build_key_wrap_auth(wps, plain) ||
1134 wps_build_encr_settings(wps, msg, plain) ||
1135 wps_build_authenticator(wps, msg)) {
1142 wps->state = RECV_DONE;
1147 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1151 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1153 msg = wpabuf_alloc(1000);
1157 if (wps_build_version(msg) ||
1158 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1159 wps_build_enrollee_nonce(wps, msg) ||
1160 wps_build_registrar_nonce(wps, msg)) {
1169 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps, u8 *op_code)
1173 switch (wps->state) {
1175 if (wps_get_dev_password(wps) < 0)
1176 msg = wps_build_m2d(wps);
1178 msg = wps_build_m2(wps);
1182 msg = wps_build_m2d(wps);
1186 msg = wps_build_m4(wps);
1190 msg = wps_build_m6(wps);
1194 msg = wps_build_m8(wps);
1198 msg = wps_build_wsc_ack(wps);
1202 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1203 "a message", wps->state);
1208 if (*op_code == WSC_MSG && msg) {
1209 /* Save a copy of the last message for Authenticator derivation
1211 wpabuf_free(wps->last_msg);
1212 wps->last_msg = wpabuf_dup(msg);
1219 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1221 if (e_nonce == NULL) {
1222 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1226 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1227 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1228 wps->nonce_e, WPS_NONCE_LEN);
1234 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1236 if (r_nonce == NULL) {
1237 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1241 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1242 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1250 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1252 if (uuid_e == NULL) {
1253 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1257 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1258 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1264 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1266 if (pw_id == NULL) {
1267 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1271 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1272 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1278 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1280 if (e_hash1 == NULL) {
1281 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1285 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1286 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1292 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1294 if (e_hash2 == NULL) {
1295 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1299 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1300 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1306 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1308 u8 hash[SHA256_MAC_LEN];
1312 if (e_snonce1 == NULL) {
1313 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1317 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1318 WPS_SECRET_NONCE_LEN);
1320 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1321 addr[0] = e_snonce1;
1322 len[0] = WPS_SECRET_NONCE_LEN;
1323 addr[1] = wps->psk1;
1324 len[1] = WPS_PSK_LEN;
1325 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1326 len[2] = wpabuf_len(wps->dh_pubkey_e);
1327 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1328 len[3] = wpabuf_len(wps->dh_pubkey_r);
1329 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1331 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1332 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1333 "not match with the pre-committed value");
1337 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1338 "half of the device password");
1344 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1346 u8 hash[SHA256_MAC_LEN];
1350 if (e_snonce2 == NULL) {
1351 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1355 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1356 WPS_SECRET_NONCE_LEN);
1358 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1359 addr[0] = e_snonce2;
1360 len[0] = WPS_SECRET_NONCE_LEN;
1361 addr[1] = wps->psk2;
1362 len[1] = WPS_PSK_LEN;
1363 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1364 len[2] = wpabuf_len(wps->dh_pubkey_e);
1365 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1366 len[3] = wpabuf_len(wps->dh_pubkey_r);
1367 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1369 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1370 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1371 "not match with the pre-committed value");
1372 wps_registrar_invalidate_pin(wps->registrar, wps->uuid_e);
1376 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1377 "half of the device password");
1378 wps->wps_pin_revealed = 0;
1379 wps_registrar_unlock_pin(wps->registrar, wps->uuid_e);
1385 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1387 if (mac_addr == NULL) {
1388 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1392 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1394 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1395 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1401 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1404 if (pk == NULL || pk_len == 0) {
1405 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1409 wpabuf_free(wps->dh_pubkey_e);
1410 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1411 if (wps->dh_pubkey_e == NULL)
1418 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1423 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1428 auth_types = WPA_GET_BE16(auth);
1430 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1432 wps->auth_type = wps->wps->auth_types & auth_types;
1433 if (wps->auth_type == 0) {
1434 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1435 "authentication types");
1443 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1448 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1453 encr_types = WPA_GET_BE16(encr);
1455 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1457 wps->encr_type = wps->wps->encr_types & encr_types;
1458 if (wps->encr_type == 0) {
1459 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1460 "encryption types");
1468 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1471 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1476 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1483 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1487 if (methods == NULL) {
1488 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1492 m = WPA_GET_BE16(methods);
1494 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1500 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1502 if (state == NULL) {
1503 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1508 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1515 static int wps_process_rf_bands(struct wps_data *wps, const u8 *bands)
1517 if (bands == NULL) {
1518 wpa_printf(MSG_DEBUG, "WPS: No RF Bands received");
1522 wpa_printf(MSG_DEBUG, "WPS: Enrollee RF Bands 0x%x", *bands);
1528 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1532 if (assoc == NULL) {
1533 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1537 a = WPA_GET_BE16(assoc);
1538 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1544 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1549 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1553 e = WPA_GET_BE16(err);
1554 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1560 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1561 struct wps_parse_attr *attr)
1563 wpa_printf(MSG_DEBUG, "WPS: Received M1");
1565 if (wps->state != RECV_M1) {
1566 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1567 "receiving M1", wps->state);
1571 if (wps_process_uuid_e(wps, attr->uuid_e) ||
1572 wps_process_mac_addr(wps, attr->mac_addr) ||
1573 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1574 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1575 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1576 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1577 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1578 wps_process_config_methods(wps, attr->config_methods) ||
1579 wps_process_wps_state(wps, attr->wps_state) ||
1580 wps_process_device_attrs(&wps->peer_dev, attr) ||
1581 wps_process_rf_bands(wps, attr->rf_bands) ||
1582 wps_process_assoc_state(wps, attr->assoc_state) ||
1583 wps_process_dev_password_id(wps, attr->dev_password_id) ||
1584 wps_process_config_error(wps, attr->config_error) ||
1585 wps_process_os_version(&wps->peer_dev, attr->os_version))
1588 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
1589 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1590 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1591 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1592 (wps->dev_pw_id != DEV_PW_PUSHBUTTON || !wps->registrar->pbc)) {
1593 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1595 wps->state = SEND_M2D;
1596 return WPS_CONTINUE;
1599 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1600 if (wps_registrar_pbc_overlap(wps->registrar, wps->mac_addr_e,
1602 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1604 wps->state = SEND_M2D;
1605 return WPS_CONTINUE;
1607 wps_registrar_add_pbc_session(wps->registrar, wps->mac_addr_e,
1612 wps->state = SEND_M2;
1613 return WPS_CONTINUE;
1617 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1618 const struct wpabuf *msg,
1619 struct wps_parse_attr *attr)
1621 wpa_printf(MSG_DEBUG, "WPS: Received M3");
1623 if (wps->state != RECV_M3) {
1624 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1625 "receiving M3", wps->state);
1629 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1630 wps_process_authenticator(wps, attr->authenticator, msg) ||
1631 wps_process_e_hash1(wps, attr->e_hash1) ||
1632 wps_process_e_hash2(wps, attr->e_hash2))
1635 wps->state = SEND_M4;
1636 return WPS_CONTINUE;
1640 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1641 const struct wpabuf *msg,
1642 struct wps_parse_attr *attr)
1644 struct wpabuf *decrypted;
1645 struct wps_parse_attr eattr;
1647 wpa_printf(MSG_DEBUG, "WPS: Received M5");
1649 if (wps->state != RECV_M5) {
1650 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1651 "receiving M5", wps->state);
1655 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1656 wps_process_authenticator(wps, attr->authenticator, msg))
1659 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1660 attr->encr_settings_len);
1661 if (decrypted == NULL) {
1662 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1663 "Settings attribute");
1667 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1669 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1670 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1671 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1672 wpabuf_free(decrypted);
1675 wpabuf_free(decrypted);
1677 wps->state = SEND_M6;
1678 return WPS_CONTINUE;
1682 static int wps_process_ap_settings_r(struct wps_data *wps,
1683 struct wps_parse_attr *attr)
1688 /* AP Settings Attributes in M7 when Enrollee is an AP */
1689 if (wps_process_ap_settings(attr, &wps->cred) < 0)
1692 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1695 * TODO: Provide access to AP settings and allow changes before sending
1696 * out M8. For now, just copy the settings unchanged into M8.
1703 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1704 const struct wpabuf *msg,
1705 struct wps_parse_attr *attr)
1707 struct wpabuf *decrypted;
1708 struct wps_parse_attr eattr;
1710 wpa_printf(MSG_DEBUG, "WPS: Received M7");
1712 if (wps->state != RECV_M7) {
1713 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1714 "receiving M7", wps->state);
1718 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1719 wps_process_authenticator(wps, attr->authenticator, msg))
1722 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1723 attr->encr_settings_len);
1724 if (decrypted == NULL) {
1725 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1726 "Settings attribute");
1730 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1732 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1733 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1734 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1735 wps_process_ap_settings_r(wps, &eattr)) {
1736 wpabuf_free(decrypted);
1740 wpabuf_free(decrypted);
1742 wps->state = SEND_M8;
1743 return WPS_CONTINUE;
1747 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1748 const struct wpabuf *msg)
1750 struct wps_parse_attr attr;
1751 enum wps_process_res ret = WPS_CONTINUE;
1753 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1755 if (wps_parse_msg(msg, &attr) < 0)
1758 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1759 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1760 attr.version ? *attr.version : 0);
1764 if (attr.msg_type == NULL) {
1765 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1769 if (*attr.msg_type != WPS_M1 &&
1770 (attr.registrar_nonce == NULL ||
1771 os_memcmp(wps->nonce_r, attr.registrar_nonce,
1772 WPS_NONCE_LEN != 0))) {
1773 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1777 switch (*attr.msg_type) {
1779 ret = wps_process_m1(wps, &attr);
1782 ret = wps_process_m3(wps, msg, &attr);
1785 ret = wps_process_m5(wps, msg, &attr);
1788 ret = wps_process_m7(wps, msg, &attr);
1791 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1796 if (ret == WPS_CONTINUE) {
1797 /* Save a copy of the last message for Authenticator derivation
1799 wpabuf_free(wps->last_msg);
1800 wps->last_msg = wpabuf_dup(msg);
1807 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1808 const struct wpabuf *msg)
1810 struct wps_parse_attr attr;
1812 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1814 if (wps_parse_msg(msg, &attr) < 0)
1817 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1818 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1819 attr.version ? *attr.version : 0);
1823 if (attr.msg_type == NULL) {
1824 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1828 if (*attr.msg_type != WPS_WSC_ACK) {
1829 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1834 if (attr.registrar_nonce == NULL ||
1835 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1837 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1841 if (attr.enrollee_nonce == NULL ||
1842 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1843 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1847 if (wps->state == RECV_M2D_ACK) {
1848 /* TODO: support for multiple registrars and sending of
1849 * multiple M2/M2D messages */
1851 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
1852 "terminate negotiation");
1859 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1860 const struct wpabuf *msg)
1862 struct wps_parse_attr attr;
1864 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1866 if (wps_parse_msg(msg, &attr) < 0)
1869 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1870 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1871 attr.version ? *attr.version : 0);
1875 if (attr.msg_type == NULL) {
1876 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1880 if (*attr.msg_type != WPS_WSC_NACK) {
1881 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1886 if (attr.registrar_nonce == NULL ||
1887 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1889 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1893 if (attr.enrollee_nonce == NULL ||
1894 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1895 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1899 if (attr.config_error == NULL) {
1900 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1905 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
1906 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1912 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
1913 const struct wpabuf *msg)
1915 struct wps_parse_attr attr;
1917 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
1919 if (wps->state != RECV_DONE) {
1920 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1921 "receiving WSC_Done", wps->state);
1925 if (wps_parse_msg(msg, &attr) < 0)
1928 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1929 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1930 attr.version ? *attr.version : 0);
1934 if (attr.msg_type == NULL) {
1935 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1939 if (*attr.msg_type != WPS_WSC_DONE) {
1940 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1945 if (attr.registrar_nonce == NULL ||
1946 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1948 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1952 if (attr.enrollee_nonce == NULL ||
1953 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1954 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1958 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
1960 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
1962 struct wps_credential cred;
1964 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
1965 "on first Enrollee connection");
1967 os_memset(&cred, 0, sizeof(cred));
1968 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1969 cred.ssid_len = wps->wps->ssid_len;
1970 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
1971 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
1972 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
1973 cred.key_len = wps->new_psk_len;
1975 wps->wps->wps_state = WPS_STATE_CONFIGURED;
1976 wpa_hexdump_ascii_key(MSG_DEBUG,
1977 "WPS: Generated random passphrase",
1978 wps->new_psk, wps->new_psk_len);
1979 if (wps->wps->cred_cb)
1980 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
1982 os_free(wps->new_psk);
1983 wps->new_psk = NULL;
1987 if (wps_cb_new_psk(wps->registrar, wps->mac_addr_e,
1988 wps->new_psk, wps->new_psk_len)) {
1989 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
1992 os_free(wps->new_psk);
1993 wps->new_psk = NULL;
1997 wps_registrar_remove_pbc_session(wps->registrar,
1998 wps->mac_addr_e, wps->uuid_e);
1999 wps_registrar_pbc_completed(wps->registrar);
2006 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2008 const struct wpabuf *msg)
2011 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2013 (unsigned long) wpabuf_len(msg), op_code);
2017 return wps_process_wsc_msg(wps, msg);
2019 return wps_process_wsc_ack(wps, msg);
2021 return wps_process_wsc_nack(wps, msg);
2023 return wps_process_wsc_done(wps, msg);
2025 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);