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_r(struct wps_registrar *reg,
282 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
284 methods |= WPS_CONFIG_PUSHBUTTON;
285 return wps_build_config_methods(msg, methods);
289 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
291 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
292 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
293 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
294 wpabuf_put_be16(msg, 1);
295 wpabuf_put_u8(msg, resp);
300 struct wps_registrar *
301 wps_registrar_init(struct wps_context *wps,
302 const struct wps_registrar_config *cfg)
304 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
309 reg->new_psk_cb = cfg->new_psk_cb;
310 reg->set_ie_cb = cfg->set_ie_cb;
311 reg->pin_needed_cb = cfg->pin_needed_cb;
312 reg->cb_ctx = cfg->cb_ctx;
314 if (wps_set_ie(reg)) {
315 wps_registrar_deinit(reg);
323 void wps_registrar_deinit(struct wps_registrar *reg)
327 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
328 wps_free_pins(reg->pins);
329 wps_free_pbc_sessions(reg->pbc_sessions);
334 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
335 const u8 *pin, size_t pin_len)
337 struct wps_uuid_pin *p;
339 p = os_zalloc(sizeof(*p));
342 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
343 p->pin = os_malloc(pin_len);
344 if (p->pin == NULL) {
348 os_memcpy(p->pin, pin, pin_len);
349 p->pin_len = pin_len;
354 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured");
355 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
356 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
357 reg->selected_registrar = 1;
365 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
367 struct wps_uuid_pin *pin, *prev;
372 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
374 reg->pins = pin->next;
376 prev->next = pin->next;
377 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
378 pin->uuid, WPS_UUID_LEN);
390 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
391 const u8 *uuid, size_t *pin_len)
393 struct wps_uuid_pin *pin;
397 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
399 * Lock the PIN to avoid attacks based on concurrent
400 * re-use of the PIN that could otherwise avoid PIN
404 wpa_printf(MSG_DEBUG, "WPS: Selected PIN "
405 "locked - do not allow concurrent "
409 *pin_len = pin->pin_len;
420 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
422 struct wps_uuid_pin *pin;
426 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
437 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
439 struct wps_registrar *reg = eloop_ctx;
441 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
442 reg->selected_registrar = 0;
448 int wps_registrar_button_pushed(struct wps_registrar *reg)
450 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
451 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
455 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
456 reg->selected_registrar = 1;
460 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
461 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
467 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
469 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
470 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
471 reg->selected_registrar = 0;
477 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
478 const struct wpabuf *wps_data)
480 struct wps_parse_attr attr;
483 wpa_hexdump_buf(MSG_MSGDUMP,
484 "WPS: Probe Request with WPS data received",
487 if (wps_parse_msg(wps_data, &attr) < 0 ||
488 attr.version == NULL || *attr.version != WPS_VERSION) {
489 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
490 "version 0x%x", attr.version ? *attr.version : 0);
494 if (attr.config_methods == NULL) {
495 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
500 methods = WPA_GET_BE16(attr.config_methods);
501 if (!(methods & WPS_CONFIG_PUSHBUTTON))
502 return; /* Not PBC */
504 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
505 MACSTR, MAC2STR(addr));
507 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
511 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
512 const u8 *psk, size_t psk_len)
514 if (reg->new_psk_cb == NULL)
517 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
521 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
522 const struct wps_device_data *dev)
524 if (reg->pin_needed_cb == NULL)
527 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
531 static int wps_cb_set_ie(struct wps_registrar *reg,
532 const struct wpabuf *beacon_ie,
533 const struct wpabuf *probe_resp_ie)
535 if (reg->set_ie_cb == NULL)
538 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
539 wpabuf_len(beacon_ie),
540 wpabuf_head(probe_resp_ie),
541 wpabuf_len(probe_resp_ie));
545 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
546 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
551 ie = wpabuf_alloc(wpabuf_len(data) + 100);
557 pos = wpabuf_head(data);
558 end = pos + wpabuf_len(data);
561 size_t frag_len = end - pos;
564 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
565 wpabuf_put_u8(ie, 4 + frag_len);
566 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
567 wpabuf_put_data(ie, pos, frag_len);
577 static int wps_set_ie(struct wps_registrar *reg)
579 struct wpabuf *beacon;
580 struct wpabuf *probe;
583 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
585 beacon = wpabuf_alloc(300);
588 probe = wpabuf_alloc(400);
594 if (wps_build_version(beacon) ||
595 wps_build_wps_state(reg->wps, beacon) ||
596 wps_build_ap_setup_locked(reg->wps, beacon) ||
597 wps_build_selected_registrar(reg, beacon) ||
598 wps_build_sel_reg_dev_password_id(reg, beacon) ||
599 wps_build_sel_reg_config_methods(reg, beacon) ||
600 wps_build_version(probe) ||
601 wps_build_wps_state(reg->wps, probe) ||
602 wps_build_ap_setup_locked(reg->wps, probe) ||
603 wps_build_selected_registrar(reg, probe) ||
604 wps_build_sel_reg_dev_password_id(reg, probe) ||
605 wps_build_sel_reg_config_methods(reg, probe) ||
606 wps_build_resp_type(reg, probe) ||
607 wps_build_uuid_e(probe, reg->wps->uuid) ||
608 wps_build_device_attrs(®->wps->dev, probe) ||
609 wps_build_probe_config_methods(reg, probe) ||
610 wps_build_rf_bands(®->wps->dev, probe)) {
616 beacon = wps_ie_encapsulate(beacon);
617 probe = wps_ie_encapsulate(probe);
619 if (!beacon || !probe) {
625 ret = wps_cb_set_ie(reg, beacon, probe);
633 static int wps_get_dev_password(struct wps_data *wps)
638 os_free(wps->dev_password);
639 wps->dev_password = NULL;
642 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
643 pin = (const u8 *) "00000000";
646 pin = wps_registrar_get_pin(wps->registrar, wps->uuid_e,
650 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
652 wps_cb_pin_needed(wps->registrar, wps->uuid_e, &wps->peer_dev);
656 wps->dev_password = os_malloc(pin_len);
657 if (wps->dev_password == NULL)
659 os_memcpy(wps->dev_password, pin, pin_len);
660 wps->dev_password_len = pin_len;
666 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
668 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
669 wpabuf_put_be16(msg, ATTR_UUID_R);
670 wpabuf_put_be16(msg, WPS_UUID_LEN);
671 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
676 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
682 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
684 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
685 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
686 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
688 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
689 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
690 "R-Hash derivation");
694 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
695 wpabuf_put_be16(msg, ATTR_R_HASH1);
696 wpabuf_put_be16(msg, SHA256_MAC_LEN);
697 hash = wpabuf_put(msg, SHA256_MAC_LEN);
698 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
699 addr[0] = wps->snonce;
700 len[0] = WPS_SECRET_NONCE_LEN;
702 len[1] = WPS_PSK_LEN;
703 addr[2] = wpabuf_head(wps->dh_pubkey_e);
704 len[2] = wpabuf_len(wps->dh_pubkey_e);
705 addr[3] = wpabuf_head(wps->dh_pubkey_r);
706 len[3] = wpabuf_len(wps->dh_pubkey_r);
707 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
708 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
710 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
711 wpabuf_put_be16(msg, ATTR_R_HASH2);
712 wpabuf_put_be16(msg, SHA256_MAC_LEN);
713 hash = wpabuf_put(msg, SHA256_MAC_LEN);
714 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
715 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
717 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
718 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
724 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
726 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
727 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
728 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
729 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
734 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
736 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
737 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
738 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
739 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
740 WPS_SECRET_NONCE_LEN);
745 static int wps_build_cred_network_idx(struct wpabuf *msg,
746 struct wps_credential *cred)
748 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
749 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
750 wpabuf_put_be16(msg, 1);
751 wpabuf_put_u8(msg, 0);
756 static int wps_build_cred_ssid(struct wpabuf *msg,
757 struct wps_credential *cred)
759 wpa_printf(MSG_DEBUG, "WPS: * SSID");
760 wpabuf_put_be16(msg, ATTR_SSID);
761 wpabuf_put_be16(msg, cred->ssid_len);
762 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
767 static int wps_build_cred_auth_type(struct wpabuf *msg,
768 struct wps_credential *cred)
770 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
772 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
773 wpabuf_put_be16(msg, 2);
774 wpabuf_put_be16(msg, cred->auth_type);
779 static int wps_build_cred_encr_type(struct wpabuf *msg,
780 struct wps_credential *cred)
782 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
784 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
785 wpabuf_put_be16(msg, 2);
786 wpabuf_put_be16(msg, cred->encr_type);
791 static int wps_build_cred_network_key(struct wpabuf *msg,
792 struct wps_credential *cred)
794 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
795 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
796 wpabuf_put_be16(msg, cred->key_len);
797 wpabuf_put_data(msg, cred->key, cred->key_len);
802 static int wps_build_cred_mac_addr(struct wpabuf *msg,
803 struct wps_credential *cred)
805 wpa_printf(MSG_DEBUG, "WPS: * MAC Address");
806 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
807 wpabuf_put_be16(msg, ETH_ALEN);
808 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
813 static int wps_build_credential(struct wpabuf *msg,
814 struct wps_credential *cred)
816 if (wps_build_cred_network_idx(msg, cred) ||
817 wps_build_cred_ssid(msg, cred) ||
818 wps_build_cred_auth_type(msg, cred) ||
819 wps_build_cred_encr_type(msg, cred) ||
820 wps_build_cred_network_key(msg, cred) ||
821 wps_build_cred_mac_addr(msg, cred))
827 static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
831 wpa_printf(MSG_DEBUG, "WPS: * Credential");
832 os_memset(&wps->cred, 0, sizeof(wps->cred));
834 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
835 wps->cred.ssid_len = wps->wps->ssid_len;
837 /* Select the best authentication and encryption type */
838 if (wps->auth_type & WPS_AUTH_WPA2PSK)
839 wps->auth_type = WPS_AUTH_WPA2PSK;
840 else if (wps->auth_type & WPS_AUTH_WPAPSK)
841 wps->auth_type = WPS_AUTH_WPAPSK;
842 else if (wps->auth_type & WPS_AUTH_OPEN)
843 wps->auth_type = WPS_AUTH_OPEN;
844 else if (wps->auth_type & WPS_AUTH_SHARED)
845 wps->auth_type = WPS_AUTH_SHARED;
847 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
851 wps->cred.auth_type = wps->auth_type;
853 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
854 wps->auth_type == WPS_AUTH_WPAPSK) {
855 if (wps->encr_type & WPS_ENCR_AES)
856 wps->encr_type = WPS_ENCR_AES;
857 else if (wps->encr_type & WPS_ENCR_TKIP)
858 wps->encr_type = WPS_ENCR_TKIP;
860 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
861 "type for WPA/WPA2");
865 if (wps->encr_type & WPS_ENCR_WEP)
866 wps->encr_type = WPS_ENCR_WEP;
867 else if (wps->encr_type & WPS_ENCR_NONE)
868 wps->encr_type = WPS_ENCR_NONE;
870 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
871 "type for non-WPA/WPA2 mode");
875 wps->cred.encr_type = wps->encr_type;
876 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
878 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap) {
880 /* Generate a random passphrase */
881 if (os_get_random(r, sizeof(r)) < 0)
883 os_free(wps->new_psk);
884 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
885 if (wps->new_psk == NULL)
887 wps->new_psk_len--; /* remove newline */
888 while (wps->new_psk_len &&
889 wps->new_psk[wps->new_psk_len - 1] == '=')
891 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
892 wps->new_psk, wps->new_psk_len);
893 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
894 wps->cred.key_len = wps->new_psk_len;
895 } else if (wps->wps->network_key) {
896 os_memcpy(wps->cred.key, wps->wps->network_key,
897 wps->wps->network_key_len);
898 wps->cred.key_len = wps->wps->network_key_len;
899 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
901 /* Generate a random per-device PSK */
902 os_free(wps->new_psk);
903 wps->new_psk_len = 32;
904 wps->new_psk = os_malloc(wps->new_psk_len);
905 if (wps->new_psk == NULL)
907 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
908 os_free(wps->new_psk);
912 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
913 wps->new_psk, wps->new_psk_len);
914 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
916 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
917 wps->cred.key_len = wps->new_psk_len * 2;
920 cred = wpabuf_alloc(200);
924 if (wps_build_credential(cred, &wps->cred)) {
929 wpabuf_put_be16(msg, ATTR_CRED);
930 wpabuf_put_be16(msg, wpabuf_len(cred));
931 wpabuf_put_buf(msg, cred);
938 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
940 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
942 if (wps_build_credential(msg, &wps->cred))
949 static struct wpabuf * wps_build_m2(struct wps_data *wps)
953 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
955 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
956 wps->nonce_r, WPS_NONCE_LEN);
957 os_memcpy(wps->uuid_r, wps->wps->uuid, WPS_UUID_LEN);
958 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
960 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
961 msg = wpabuf_alloc(1000);
965 if (wps_build_version(msg) ||
966 wps_build_msg_type(msg, WPS_M2) ||
967 wps_build_enrollee_nonce(wps, msg) ||
968 wps_build_registrar_nonce(wps, msg) ||
969 wps_build_uuid_r(wps, msg) ||
970 wps_build_public_key(wps, msg) ||
971 wps_derive_keys(wps) ||
972 wps_build_auth_type_flags(wps, msg) ||
973 wps_build_encr_type_flags(wps, msg) ||
974 wps_build_conn_type_flags(wps, msg) ||
975 wps_build_config_methods_r(wps->registrar, msg) ||
976 wps_build_device_attrs(&wps->wps->dev, msg) ||
977 wps_build_rf_bands(&wps->wps->dev, msg) ||
978 wps_build_assoc_state(wps, msg) ||
979 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
980 wps_build_dev_password_id(msg, DEV_PW_DEFAULT) ||
981 wps_build_os_version(&wps->wps->dev, msg) ||
982 wps_build_authenticator(wps, msg)) {
987 wps->state = RECV_M3;
992 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
995 u16 err = WPS_CFG_NO_ERROR;
997 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
998 msg = wpabuf_alloc(1000);
1002 if (wps->authenticator && wps->wps->ap_setup_locked)
1003 err = WPS_CFG_SETUP_LOCKED;
1005 if (wps_build_version(msg) ||
1006 wps_build_msg_type(msg, WPS_M2D) ||
1007 wps_build_enrollee_nonce(wps, msg) ||
1008 wps_build_registrar_nonce(wps, msg) ||
1009 wps_build_uuid_r(wps, msg) ||
1010 wps_build_auth_type_flags(wps, msg) ||
1011 wps_build_encr_type_flags(wps, msg) ||
1012 wps_build_conn_type_flags(wps, msg) ||
1013 wps_build_config_methods_r(wps->registrar, msg) ||
1014 wps_build_device_attrs(&wps->wps->dev, msg) ||
1015 wps_build_rf_bands(&wps->wps->dev, msg) ||
1016 wps_build_assoc_state(wps, msg) ||
1017 wps_build_config_error(msg, err) ||
1018 wps_build_os_version(&wps->wps->dev, msg)) {
1023 wps->state = RECV_M2D_ACK;
1028 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1030 struct wpabuf *msg, *plain;
1032 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1034 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1036 plain = wpabuf_alloc(200);
1040 msg = wpabuf_alloc(1000);
1046 if (wps_build_version(msg) ||
1047 wps_build_msg_type(msg, WPS_M4) ||
1048 wps_build_enrollee_nonce(wps, msg) ||
1049 wps_build_r_hash(wps, msg) ||
1050 wps_build_r_snonce1(wps, plain) ||
1051 wps_build_key_wrap_auth(wps, plain) ||
1052 wps_build_encr_settings(wps, msg, plain) ||
1053 wps_build_authenticator(wps, msg)) {
1060 wps->state = RECV_M5;
1065 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1067 struct wpabuf *msg, *plain;
1069 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1071 plain = wpabuf_alloc(200);
1075 msg = wpabuf_alloc(1000);
1081 if (wps_build_version(msg) ||
1082 wps_build_msg_type(msg, WPS_M6) ||
1083 wps_build_enrollee_nonce(wps, msg) ||
1084 wps_build_r_snonce2(wps, plain) ||
1085 wps_build_key_wrap_auth(wps, plain) ||
1086 wps_build_encr_settings(wps, msg, plain) ||
1087 wps_build_authenticator(wps, msg)) {
1094 wps->wps_pin_revealed = 1;
1095 wps->state = RECV_M7;
1100 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1102 struct wpabuf *msg, *plain;
1104 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1106 plain = wpabuf_alloc(500);
1110 msg = wpabuf_alloc(1000);
1116 if (wps_build_version(msg) ||
1117 wps_build_msg_type(msg, WPS_M8) ||
1118 wps_build_enrollee_nonce(wps, msg) ||
1119 (wps->wps->ap && wps_build_cred(wps, plain)) ||
1120 (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1121 wps_build_key_wrap_auth(wps, plain) ||
1122 wps_build_encr_settings(wps, msg, plain) ||
1123 wps_build_authenticator(wps, msg)) {
1130 wps->state = RECV_DONE;
1135 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1139 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1141 msg = wpabuf_alloc(1000);
1145 if (wps_build_version(msg) ||
1146 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1147 wps_build_enrollee_nonce(wps, msg) ||
1148 wps_build_registrar_nonce(wps, msg)) {
1157 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps, u8 *op_code)
1161 switch (wps->state) {
1163 if (wps_get_dev_password(wps) < 0)
1164 msg = wps_build_m2d(wps);
1166 msg = wps_build_m2(wps);
1170 msg = wps_build_m2d(wps);
1174 msg = wps_build_m4(wps);
1178 msg = wps_build_m6(wps);
1182 msg = wps_build_m8(wps);
1186 msg = wps_build_wsc_ack(wps);
1190 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1191 "a message", wps->state);
1196 if (*op_code == WSC_MSG && msg) {
1197 /* Save a copy of the last message for Authenticator derivation
1199 wpabuf_free(wps->last_msg);
1200 wps->last_msg = wpabuf_dup(msg);
1207 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1209 if (e_nonce == NULL) {
1210 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1214 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1215 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1216 wps->nonce_e, WPS_NONCE_LEN);
1222 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1224 if (r_nonce == NULL) {
1225 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1229 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1230 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1238 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1240 if (uuid_e == NULL) {
1241 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1245 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1246 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1252 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1254 if (pw_id == NULL) {
1255 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1259 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1260 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1266 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1268 if (e_hash1 == NULL) {
1269 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1273 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1274 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1280 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1282 if (e_hash2 == NULL) {
1283 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1287 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1288 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1294 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1296 u8 hash[SHA256_MAC_LEN];
1300 if (e_snonce1 == NULL) {
1301 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1305 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1306 WPS_SECRET_NONCE_LEN);
1308 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1309 addr[0] = e_snonce1;
1310 len[0] = WPS_SECRET_NONCE_LEN;
1311 addr[1] = wps->psk1;
1312 len[1] = WPS_PSK_LEN;
1313 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1314 len[2] = wpabuf_len(wps->dh_pubkey_e);
1315 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1316 len[3] = wpabuf_len(wps->dh_pubkey_r);
1317 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1319 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1320 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1321 "not match with the pre-committed value");
1325 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1326 "half of the device password");
1332 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1334 u8 hash[SHA256_MAC_LEN];
1338 if (e_snonce2 == NULL) {
1339 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1343 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1344 WPS_SECRET_NONCE_LEN);
1346 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1347 addr[0] = e_snonce2;
1348 len[0] = WPS_SECRET_NONCE_LEN;
1349 addr[1] = wps->psk2;
1350 len[1] = WPS_PSK_LEN;
1351 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1352 len[2] = wpabuf_len(wps->dh_pubkey_e);
1353 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1354 len[3] = wpabuf_len(wps->dh_pubkey_r);
1355 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1357 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1358 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1359 "not match with the pre-committed value");
1360 wps_registrar_invalidate_pin(wps->registrar, wps->uuid_e);
1364 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1365 "half of the device password");
1366 wps->wps_pin_revealed = 0;
1367 wps_registrar_unlock_pin(wps->registrar, wps->uuid_e);
1373 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1375 if (mac_addr == NULL) {
1376 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1380 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1382 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1383 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1389 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1392 if (pk == NULL || pk_len == 0) {
1393 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1397 wpabuf_free(wps->dh_pubkey_e);
1398 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1399 if (wps->dh_pubkey_e == NULL)
1406 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1411 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1416 auth_types = WPA_GET_BE16(auth);
1418 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1420 wps->auth_type = wps->wps->auth_types & auth_types;
1421 if (wps->auth_type == 0) {
1422 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1423 "authentication types (own 0x%x Enrollee 0x%x)",
1424 wps->wps->auth_types, auth_types);
1432 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1437 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1442 encr_types = WPA_GET_BE16(encr);
1444 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1446 wps->encr_type = wps->wps->encr_types & encr_types;
1447 if (wps->encr_type == 0) {
1448 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1449 "encryption types");
1457 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1460 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1465 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1472 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1476 if (methods == NULL) {
1477 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1481 m = WPA_GET_BE16(methods);
1483 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1489 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1491 if (state == NULL) {
1492 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1497 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1504 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1508 if (assoc == NULL) {
1509 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1513 a = WPA_GET_BE16(assoc);
1514 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1520 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1525 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1529 e = WPA_GET_BE16(err);
1530 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1536 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1537 struct wps_parse_attr *attr)
1539 wpa_printf(MSG_DEBUG, "WPS: Received M1");
1541 if (wps->state != RECV_M1) {
1542 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1543 "receiving M1", wps->state);
1547 if (wps_process_uuid_e(wps, attr->uuid_e) ||
1548 wps_process_mac_addr(wps, attr->mac_addr) ||
1549 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1550 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1551 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1552 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1553 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1554 wps_process_config_methods(wps, attr->config_methods) ||
1555 wps_process_wps_state(wps, attr->wps_state) ||
1556 wps_process_device_attrs(&wps->peer_dev, attr) ||
1557 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1558 wps_process_assoc_state(wps, attr->assoc_state) ||
1559 wps_process_dev_password_id(wps, attr->dev_password_id) ||
1560 wps_process_config_error(wps, attr->config_error) ||
1561 wps_process_os_version(&wps->peer_dev, attr->os_version))
1564 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
1565 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1566 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1567 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1568 (wps->dev_pw_id != DEV_PW_PUSHBUTTON || !wps->registrar->pbc)) {
1569 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1571 wps->state = SEND_M2D;
1572 return WPS_CONTINUE;
1575 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1576 if (wps_registrar_pbc_overlap(wps->registrar, wps->mac_addr_e,
1578 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1580 wps->state = SEND_M2D;
1581 return WPS_CONTINUE;
1583 wps_registrar_add_pbc_session(wps->registrar, wps->mac_addr_e,
1588 wps->state = SEND_M2;
1589 return WPS_CONTINUE;
1593 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1594 const struct wpabuf *msg,
1595 struct wps_parse_attr *attr)
1597 wpa_printf(MSG_DEBUG, "WPS: Received M3");
1599 if (wps->state != RECV_M3) {
1600 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1601 "receiving M3", wps->state);
1605 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1606 wps_process_authenticator(wps, attr->authenticator, msg) ||
1607 wps_process_e_hash1(wps, attr->e_hash1) ||
1608 wps_process_e_hash2(wps, attr->e_hash2))
1611 wps->state = SEND_M4;
1612 return WPS_CONTINUE;
1616 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1617 const struct wpabuf *msg,
1618 struct wps_parse_attr *attr)
1620 struct wpabuf *decrypted;
1621 struct wps_parse_attr eattr;
1623 wpa_printf(MSG_DEBUG, "WPS: Received M5");
1625 if (wps->state != RECV_M5) {
1626 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1627 "receiving M5", wps->state);
1631 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1632 wps_process_authenticator(wps, attr->authenticator, msg))
1635 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1636 attr->encr_settings_len);
1637 if (decrypted == NULL) {
1638 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1639 "Settings attribute");
1643 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1645 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1646 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1647 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1648 wpabuf_free(decrypted);
1651 wpabuf_free(decrypted);
1653 wps->state = SEND_M6;
1654 return WPS_CONTINUE;
1658 static int wps_process_ap_settings_r(struct wps_data *wps,
1659 struct wps_parse_attr *attr)
1664 /* AP Settings Attributes in M7 when Enrollee is an AP */
1665 if (wps_process_ap_settings(attr, &wps->cred) < 0)
1668 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1671 * TODO: Provide access to AP settings and allow changes before sending
1672 * out M8. For now, just copy the settings unchanged into M8.
1679 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1680 const struct wpabuf *msg,
1681 struct wps_parse_attr *attr)
1683 struct wpabuf *decrypted;
1684 struct wps_parse_attr eattr;
1686 wpa_printf(MSG_DEBUG, "WPS: Received M7");
1688 if (wps->state != RECV_M7) {
1689 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1690 "receiving M7", wps->state);
1694 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1695 wps_process_authenticator(wps, attr->authenticator, msg))
1698 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1699 attr->encr_settings_len);
1700 if (decrypted == NULL) {
1701 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1702 "Settings attribute");
1706 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1708 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1709 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1710 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1711 wps_process_ap_settings_r(wps, &eattr)) {
1712 wpabuf_free(decrypted);
1716 wpabuf_free(decrypted);
1718 wps->state = SEND_M8;
1719 return WPS_CONTINUE;
1723 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1724 const struct wpabuf *msg)
1726 struct wps_parse_attr attr;
1727 enum wps_process_res ret = WPS_CONTINUE;
1729 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1731 if (wps_parse_msg(msg, &attr) < 0)
1734 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1735 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1736 attr.version ? *attr.version : 0);
1740 if (attr.msg_type == NULL) {
1741 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1745 if (*attr.msg_type != WPS_M1 &&
1746 (attr.registrar_nonce == NULL ||
1747 os_memcmp(wps->nonce_r, attr.registrar_nonce,
1748 WPS_NONCE_LEN != 0))) {
1749 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1753 switch (*attr.msg_type) {
1755 ret = wps_process_m1(wps, &attr);
1758 ret = wps_process_m3(wps, msg, &attr);
1761 ret = wps_process_m5(wps, msg, &attr);
1764 ret = wps_process_m7(wps, msg, &attr);
1767 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1772 if (ret == WPS_CONTINUE) {
1773 /* Save a copy of the last message for Authenticator derivation
1775 wpabuf_free(wps->last_msg);
1776 wps->last_msg = wpabuf_dup(msg);
1783 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1784 const struct wpabuf *msg)
1786 struct wps_parse_attr attr;
1788 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1790 if (wps_parse_msg(msg, &attr) < 0)
1793 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1794 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1795 attr.version ? *attr.version : 0);
1799 if (attr.msg_type == NULL) {
1800 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1804 if (*attr.msg_type != WPS_WSC_ACK) {
1805 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1810 if (attr.registrar_nonce == NULL ||
1811 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1813 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1817 if (attr.enrollee_nonce == NULL ||
1818 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1819 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1823 if (wps->state == RECV_M2D_ACK) {
1824 /* TODO: support for multiple registrars and sending of
1825 * multiple M2/M2D messages */
1827 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
1828 "terminate negotiation");
1835 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1836 const struct wpabuf *msg)
1838 struct wps_parse_attr attr;
1840 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1842 if (wps_parse_msg(msg, &attr) < 0)
1845 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1846 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1847 attr.version ? *attr.version : 0);
1851 if (attr.msg_type == NULL) {
1852 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1856 if (*attr.msg_type != WPS_WSC_NACK) {
1857 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1862 if (attr.registrar_nonce == NULL ||
1863 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1865 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1869 if (attr.enrollee_nonce == NULL ||
1870 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1871 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1875 if (attr.config_error == NULL) {
1876 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1881 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
1882 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1888 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
1889 const struct wpabuf *msg)
1891 struct wps_parse_attr attr;
1893 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
1895 if (wps->state != RECV_DONE) {
1896 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1897 "receiving WSC_Done", wps->state);
1901 if (wps_parse_msg(msg, &attr) < 0)
1904 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1905 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1906 attr.version ? *attr.version : 0);
1910 if (attr.msg_type == NULL) {
1911 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1915 if (*attr.msg_type != WPS_WSC_DONE) {
1916 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1921 if (attr.registrar_nonce == NULL ||
1922 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1924 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1928 if (attr.enrollee_nonce == NULL ||
1929 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1930 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1934 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
1936 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
1938 struct wps_credential cred;
1940 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
1941 "on first Enrollee connection");
1943 os_memset(&cred, 0, sizeof(cred));
1944 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1945 cred.ssid_len = wps->wps->ssid_len;
1946 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
1947 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
1948 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
1949 cred.key_len = wps->new_psk_len;
1951 wps->wps->wps_state = WPS_STATE_CONFIGURED;
1952 wpa_hexdump_ascii_key(MSG_DEBUG,
1953 "WPS: Generated random passphrase",
1954 wps->new_psk, wps->new_psk_len);
1955 if (wps->wps->cred_cb)
1956 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
1958 os_free(wps->new_psk);
1959 wps->new_psk = NULL;
1962 if (!wps->wps->ap) {
1963 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
1964 "on the modified AP configuration");
1965 if (wps->wps->cred_cb)
1966 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
1970 if (wps_cb_new_psk(wps->registrar, wps->mac_addr_e,
1971 wps->new_psk, wps->new_psk_len)) {
1972 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
1975 os_free(wps->new_psk);
1976 wps->new_psk = NULL;
1980 wps_registrar_remove_pbc_session(wps->registrar,
1981 wps->mac_addr_e, wps->uuid_e);
1982 wps_registrar_pbc_completed(wps->registrar);
1989 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
1991 const struct wpabuf *msg)
1994 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1996 (unsigned long) wpabuf_len(msg), op_code);
2000 return wps_process_wsc_msg(wps, msg);
2002 return wps_process_wsc_ack(wps, msg);
2004 return wps_process_wsc_nack(wps, msg);
2006 return wps_process_wsc_done(wps, msg);
2008 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);