2 * Wi-Fi Protected Setup - Registrar
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #include "ieee802_11_defs.h"
23 #include "wps_dev_attr.h"
27 struct wps_uuid_pin *next;
28 u8 uuid[WPS_UUID_LEN];
36 static void wps_free_pin(struct wps_uuid_pin *pin)
43 static void wps_free_pins(struct wps_uuid_pin *pins)
45 struct wps_uuid_pin *pin, *prev;
56 struct wps_pbc_session {
57 struct wps_pbc_session *next;
59 u8 uuid_e[WPS_UUID_LEN];
60 struct os_time timestamp;
64 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
66 struct wps_pbc_session *prev;
76 struct wps_registrar {
77 struct wps_context *wps;
80 int selected_registrar;
82 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
84 int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
85 const u8 *probe_resp_ie, size_t probe_resp_ie_len);
86 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
87 const struct wps_device_data *dev);
90 struct wps_uuid_pin *pins;
91 struct wps_pbc_session *pbc_sessions;
95 static int wps_set_ie(struct wps_registrar *reg);
96 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
99 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
100 const u8 *addr, const u8 *uuid_e)
102 struct wps_pbc_session *pbc, *prev = NULL;
107 pbc = reg->pbc_sessions;
109 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
110 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
112 prev->next = pbc->next;
114 reg->pbc_sessions = pbc->next;
122 pbc = os_zalloc(sizeof(*pbc));
125 os_memcpy(pbc->addr, addr, ETH_ALEN);
127 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
130 pbc->next = reg->pbc_sessions;
131 reg->pbc_sessions = pbc;
132 pbc->timestamp = now;
134 /* remove entries that have timed out */
139 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
141 wps_free_pbc_sessions(pbc);
150 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
151 const u8 *addr, const u8 *uuid_e)
153 struct wps_pbc_session *pbc, *prev = NULL;
155 pbc = reg->pbc_sessions;
157 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
158 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
160 prev->next = pbc->next;
162 reg->pbc_sessions = pbc->next;
172 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
173 const u8 *addr, const u8 *uuid_e)
176 struct wps_pbc_session *pbc;
181 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
182 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
184 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
186 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
193 return count > 1 ? 1 : 0;
197 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
199 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
201 wpabuf_put_be16(msg, ATTR_WPS_STATE);
202 wpabuf_put_be16(msg, 1);
203 wpabuf_put_u8(msg, wps->wps_state);
208 static int wps_build_ap_setup_locked(struct wps_context *wps,
211 if (wps->ap_setup_locked) {
212 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
213 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
214 wpabuf_put_be16(msg, 1);
215 wpabuf_put_u8(msg, 1);
221 static int wps_build_selected_registrar(struct wps_registrar *reg,
224 if (!reg->selected_registrar)
226 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
227 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
228 wpabuf_put_be16(msg, 1);
229 wpabuf_put_u8(msg, 1);
234 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
237 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
238 if (!reg->selected_registrar)
240 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
241 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
242 wpabuf_put_be16(msg, 2);
243 wpabuf_put_be16(msg, id);
248 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
252 if (!reg->selected_registrar)
254 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
256 methods |= WPS_CONFIG_PUSHBUTTON;
257 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
259 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
260 wpabuf_put_be16(msg, 2);
261 wpabuf_put_be16(msg, methods);
266 static int wps_build_probe_config_methods(struct wps_registrar *reg,
271 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
272 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
273 wpabuf_put_be16(msg, 2);
274 wpabuf_put_be16(msg, methods);
279 static int wps_build_config_methods_r(struct wps_registrar *reg,
283 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
285 methods |= WPS_CONFIG_PUSHBUTTON;
286 return wps_build_config_methods(msg, methods);
290 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
292 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
293 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
294 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
295 wpabuf_put_be16(msg, 1);
296 wpabuf_put_u8(msg, resp);
302 * wps_registrar_init - Initialize WPS Registrar data
303 * @wps: Pointer to longterm WPS context
304 * @cfg: Registrar configuration
305 * Returns: Pointer to allocated Registrar data or %NULL on failure
307 * This function is used to initialize WPS Registrar functionality. It can be
308 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
309 * runs (e.g., when run as an internal Registrar in an AP). Caller is
310 * responsible for freeing the returned data with wps_registrar_deinit() when
311 * Registrar functionality is not needed anymore.
313 struct wps_registrar *
314 wps_registrar_init(struct wps_context *wps,
315 const struct wps_registrar_config *cfg)
317 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
322 reg->new_psk_cb = cfg->new_psk_cb;
323 reg->set_ie_cb = cfg->set_ie_cb;
324 reg->pin_needed_cb = cfg->pin_needed_cb;
325 reg->cb_ctx = cfg->cb_ctx;
327 if (wps_set_ie(reg)) {
328 wps_registrar_deinit(reg);
337 * wps_registrar_deinit - Deinitialize WPS Registrar data
338 * @reg: Registrar data from wps_registrar_init()
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);
352 * wps_registrar_add_pin - Configure a new PIN for Registrar
353 * @reg: Registrar data from wps_registrar_init()
354 * @uuid: UUID-E or %NULL for wildcard (any UUID)
355 * @pin: PIN (Device Password)
356 * @pin_len: Length of pin in octets
357 * Returns: 0 on success, -1 on failure
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));
368 p->wildcard_uuid = 1;
370 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
371 p->pin = os_malloc(pin_len);
372 if (p->pin == NULL) {
376 os_memcpy(p->pin, pin, pin_len);
377 p->pin_len = pin_len;
382 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured");
383 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
384 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
385 reg->selected_registrar = 1;
394 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
395 * @reg: Registrar data from wps_registrar_init()
397 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
399 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
401 struct wps_uuid_pin *pin, *prev;
406 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
408 reg->pins = pin->next;
410 prev->next = pin->next;
411 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
412 pin->uuid, WPS_UUID_LEN);
424 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
425 const u8 *uuid, size_t *pin_len)
427 struct wps_uuid_pin *pin;
431 if (!pin->wildcard_uuid &&
432 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
438 /* Check for wildcard UUIDs since none of the UUID-specific
442 if (pin->wildcard_uuid == 1) {
443 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
444 "PIN. Assigned it for this UUID-E");
445 pin->wildcard_uuid = 2;
446 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
457 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
458 * that could otherwise avoid PIN invalidations.
461 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
462 "allow concurrent re-use");
465 *pin_len = pin->pin_len;
472 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
473 * @reg: Registrar data from wps_registrar_init()
475 * Returns: 0 on success, -1 on failure
477 * PINs are locked to enforce only one concurrent use. This function unlocks a
478 * PIN to allow it to be used again. If the specified PIN was configured using
479 * a wildcard UUID, it will be removed instead of allowing multiple uses.
481 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
483 struct wps_uuid_pin *pin;
487 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
488 if (pin->wildcard_uuid == 2) {
489 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
491 return wps_registrar_invalidate_pin(reg, uuid);
503 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
505 reg->selected_registrar = 0;
511 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
513 struct wps_registrar *reg = eloop_ctx;
515 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
516 wps_registrar_stop_pbc(reg);
521 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
522 * @reg: Registrar data from wps_registrar_init()
523 * Returns: 0 on success, -1 on failure
525 * This function is called on an AP when a push button is pushed to activate
526 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
527 * or when a PBC registration is completed.
529 int wps_registrar_button_pushed(struct wps_registrar *reg)
531 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
532 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
536 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
537 reg->selected_registrar = 1;
541 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
542 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
548 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
550 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
551 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
552 wps_registrar_stop_pbc(reg);
557 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
558 * @reg: Registrar data from wps_registrar_init()
559 * @addr: MAC address of the Probe Request sender
560 * @wps_data: WPS IE contents
562 * This function is called on an AP when a Probe Request with WPS IE is
563 * received. This is used to track PBC mode use and to detect possible overlap
564 * situation with other WPS APs.
566 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
567 const struct wpabuf *wps_data)
569 struct wps_parse_attr attr;
572 wpa_hexdump_buf(MSG_MSGDUMP,
573 "WPS: Probe Request with WPS data received",
576 if (wps_parse_msg(wps_data, &attr) < 0 ||
577 attr.version == NULL || *attr.version != WPS_VERSION) {
578 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
579 "version 0x%x", attr.version ? *attr.version : 0);
583 if (attr.config_methods == NULL) {
584 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
589 methods = WPA_GET_BE16(attr.config_methods);
590 if (!(methods & WPS_CONFIG_PUSHBUTTON))
591 return; /* Not PBC */
593 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
594 MACSTR, MAC2STR(addr));
596 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
600 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
601 const u8 *psk, size_t psk_len)
603 if (reg->new_psk_cb == NULL)
606 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
610 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
611 const struct wps_device_data *dev)
613 if (reg->pin_needed_cb == NULL)
616 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
620 static int wps_cb_set_ie(struct wps_registrar *reg,
621 const struct wpabuf *beacon_ie,
622 const struct wpabuf *probe_resp_ie)
624 if (reg->set_ie_cb == NULL)
627 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
628 wpabuf_len(beacon_ie),
629 wpabuf_head(probe_resp_ie),
630 wpabuf_len(probe_resp_ie));
634 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
635 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
640 ie = wpabuf_alloc(wpabuf_len(data) + 100);
646 pos = wpabuf_head(data);
647 end = pos + wpabuf_len(data);
650 size_t frag_len = end - pos;
653 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
654 wpabuf_put_u8(ie, 4 + frag_len);
655 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
656 wpabuf_put_data(ie, pos, frag_len);
666 static int wps_set_ie(struct wps_registrar *reg)
668 struct wpabuf *beacon;
669 struct wpabuf *probe;
672 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
674 beacon = wpabuf_alloc(300);
677 probe = wpabuf_alloc(400);
683 if (wps_build_version(beacon) ||
684 wps_build_wps_state(reg->wps, beacon) ||
685 wps_build_ap_setup_locked(reg->wps, beacon) ||
686 wps_build_selected_registrar(reg, beacon) ||
687 wps_build_sel_reg_dev_password_id(reg, beacon) ||
688 wps_build_sel_reg_config_methods(reg, beacon) ||
689 wps_build_version(probe) ||
690 wps_build_wps_state(reg->wps, probe) ||
691 wps_build_ap_setup_locked(reg->wps, probe) ||
692 wps_build_selected_registrar(reg, probe) ||
693 wps_build_sel_reg_dev_password_id(reg, probe) ||
694 wps_build_sel_reg_config_methods(reg, probe) ||
695 wps_build_resp_type(reg, probe) ||
696 wps_build_uuid_e(probe, reg->wps->uuid) ||
697 wps_build_device_attrs(®->wps->dev, probe) ||
698 wps_build_probe_config_methods(reg, probe) ||
699 wps_build_rf_bands(®->wps->dev, probe)) {
705 beacon = wps_ie_encapsulate(beacon);
706 probe = wps_ie_encapsulate(probe);
708 if (!beacon || !probe) {
714 ret = wps_cb_set_ie(reg, beacon, probe);
722 static int wps_get_dev_password(struct wps_data *wps)
727 os_free(wps->dev_password);
728 wps->dev_password = NULL;
731 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
732 pin = (const u8 *) "00000000";
735 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
739 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
741 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
746 wps->dev_password = os_malloc(pin_len);
747 if (wps->dev_password == NULL)
749 os_memcpy(wps->dev_password, pin, pin_len);
750 wps->dev_password_len = pin_len;
756 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
758 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
759 wpabuf_put_be16(msg, ATTR_UUID_R);
760 wpabuf_put_be16(msg, WPS_UUID_LEN);
761 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
766 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
772 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
774 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
775 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
776 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
778 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
779 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
780 "R-Hash derivation");
784 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
785 wpabuf_put_be16(msg, ATTR_R_HASH1);
786 wpabuf_put_be16(msg, SHA256_MAC_LEN);
787 hash = wpabuf_put(msg, SHA256_MAC_LEN);
788 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
789 addr[0] = wps->snonce;
790 len[0] = WPS_SECRET_NONCE_LEN;
792 len[1] = WPS_PSK_LEN;
793 addr[2] = wpabuf_head(wps->dh_pubkey_e);
794 len[2] = wpabuf_len(wps->dh_pubkey_e);
795 addr[3] = wpabuf_head(wps->dh_pubkey_r);
796 len[3] = wpabuf_len(wps->dh_pubkey_r);
797 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
798 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
800 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
801 wpabuf_put_be16(msg, ATTR_R_HASH2);
802 wpabuf_put_be16(msg, SHA256_MAC_LEN);
803 hash = wpabuf_put(msg, SHA256_MAC_LEN);
804 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
805 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
807 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
808 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
814 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
816 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
817 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
818 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
819 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
824 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
826 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
827 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
828 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
829 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
830 WPS_SECRET_NONCE_LEN);
835 static int wps_build_cred_network_idx(struct wpabuf *msg,
836 struct wps_credential *cred)
838 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
839 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
840 wpabuf_put_be16(msg, 1);
841 wpabuf_put_u8(msg, 0);
846 static int wps_build_cred_ssid(struct wpabuf *msg,
847 struct wps_credential *cred)
849 wpa_printf(MSG_DEBUG, "WPS: * SSID");
850 wpabuf_put_be16(msg, ATTR_SSID);
851 wpabuf_put_be16(msg, cred->ssid_len);
852 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
857 static int wps_build_cred_auth_type(struct wpabuf *msg,
858 struct wps_credential *cred)
860 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
862 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
863 wpabuf_put_be16(msg, 2);
864 wpabuf_put_be16(msg, cred->auth_type);
869 static int wps_build_cred_encr_type(struct wpabuf *msg,
870 struct wps_credential *cred)
872 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
874 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
875 wpabuf_put_be16(msg, 2);
876 wpabuf_put_be16(msg, cred->encr_type);
881 static int wps_build_cred_network_key(struct wpabuf *msg,
882 struct wps_credential *cred)
884 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
885 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
886 wpabuf_put_be16(msg, cred->key_len);
887 wpabuf_put_data(msg, cred->key, cred->key_len);
892 static int wps_build_cred_mac_addr(struct wpabuf *msg,
893 struct wps_credential *cred)
895 wpa_printf(MSG_DEBUG, "WPS: * MAC Address");
896 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
897 wpabuf_put_be16(msg, ETH_ALEN);
898 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
903 static int wps_build_credential(struct wpabuf *msg,
904 struct wps_credential *cred)
906 if (wps_build_cred_network_idx(msg, cred) ||
907 wps_build_cred_ssid(msg, cred) ||
908 wps_build_cred_auth_type(msg, cred) ||
909 wps_build_cred_encr_type(msg, cred) ||
910 wps_build_cred_network_key(msg, cred) ||
911 wps_build_cred_mac_addr(msg, cred))
917 static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
921 wpa_printf(MSG_DEBUG, "WPS: * Credential");
922 os_memset(&wps->cred, 0, sizeof(wps->cred));
924 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
925 wps->cred.ssid_len = wps->wps->ssid_len;
927 /* Select the best authentication and encryption type */
928 if (wps->auth_type & WPS_AUTH_WPA2PSK)
929 wps->auth_type = WPS_AUTH_WPA2PSK;
930 else if (wps->auth_type & WPS_AUTH_WPAPSK)
931 wps->auth_type = WPS_AUTH_WPAPSK;
932 else if (wps->auth_type & WPS_AUTH_OPEN)
933 wps->auth_type = WPS_AUTH_OPEN;
934 else if (wps->auth_type & WPS_AUTH_SHARED)
935 wps->auth_type = WPS_AUTH_SHARED;
937 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
941 wps->cred.auth_type = wps->auth_type;
943 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
944 wps->auth_type == WPS_AUTH_WPAPSK) {
945 if (wps->encr_type & WPS_ENCR_AES)
946 wps->encr_type = WPS_ENCR_AES;
947 else if (wps->encr_type & WPS_ENCR_TKIP)
948 wps->encr_type = WPS_ENCR_TKIP;
950 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
951 "type for WPA/WPA2");
955 if (wps->encr_type & WPS_ENCR_WEP)
956 wps->encr_type = WPS_ENCR_WEP;
957 else if (wps->encr_type & WPS_ENCR_NONE)
958 wps->encr_type = WPS_ENCR_NONE;
960 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
961 "type for non-WPA/WPA2 mode");
965 wps->cred.encr_type = wps->encr_type;
966 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
968 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap) {
970 /* Generate a random passphrase */
971 if (os_get_random(r, sizeof(r)) < 0)
973 os_free(wps->new_psk);
974 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
975 if (wps->new_psk == NULL)
977 wps->new_psk_len--; /* remove newline */
978 while (wps->new_psk_len &&
979 wps->new_psk[wps->new_psk_len - 1] == '=')
981 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
982 wps->new_psk, wps->new_psk_len);
983 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
984 wps->cred.key_len = wps->new_psk_len;
985 } else if (wps->wps->network_key) {
986 os_memcpy(wps->cred.key, wps->wps->network_key,
987 wps->wps->network_key_len);
988 wps->cred.key_len = wps->wps->network_key_len;
989 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
991 /* Generate a random per-device PSK */
992 os_free(wps->new_psk);
993 wps->new_psk_len = 32;
994 wps->new_psk = os_malloc(wps->new_psk_len);
995 if (wps->new_psk == NULL)
997 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
998 os_free(wps->new_psk);
1002 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1003 wps->new_psk, wps->new_psk_len);
1004 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1006 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1007 wps->cred.key_len = wps->new_psk_len * 2;
1010 cred = wpabuf_alloc(200);
1014 if (wps_build_credential(cred, &wps->cred)) {
1019 wpabuf_put_be16(msg, ATTR_CRED);
1020 wpabuf_put_be16(msg, wpabuf_len(cred));
1021 wpabuf_put_buf(msg, cred);
1028 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1030 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1032 if (wps_build_credential(msg, &wps->cred))
1039 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1043 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1045 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1046 wps->nonce_r, WPS_NONCE_LEN);
1047 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1049 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1050 msg = wpabuf_alloc(1000);
1054 if (wps_build_version(msg) ||
1055 wps_build_msg_type(msg, WPS_M2) ||
1056 wps_build_enrollee_nonce(wps, msg) ||
1057 wps_build_registrar_nonce(wps, msg) ||
1058 wps_build_uuid_r(wps, msg) ||
1059 wps_build_public_key(wps, msg) ||
1060 wps_derive_keys(wps) ||
1061 wps_build_auth_type_flags(wps, msg) ||
1062 wps_build_encr_type_flags(wps, msg) ||
1063 wps_build_conn_type_flags(wps, msg) ||
1064 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1065 wps_build_device_attrs(&wps->wps->dev, msg) ||
1066 wps_build_rf_bands(&wps->wps->dev, msg) ||
1067 wps_build_assoc_state(wps, msg) ||
1068 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1069 wps_build_dev_password_id(msg, DEV_PW_DEFAULT) ||
1070 wps_build_os_version(&wps->wps->dev, msg) ||
1071 wps_build_authenticator(wps, msg)) {
1076 wps->state = RECV_M3;
1081 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1084 u16 err = WPS_CFG_NO_ERROR;
1086 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1087 msg = wpabuf_alloc(1000);
1091 if (wps->wps->ap && wps->wps->ap_setup_locked)
1092 err = WPS_CFG_SETUP_LOCKED;
1094 if (wps_build_version(msg) ||
1095 wps_build_msg_type(msg, WPS_M2D) ||
1096 wps_build_enrollee_nonce(wps, msg) ||
1097 wps_build_registrar_nonce(wps, msg) ||
1098 wps_build_uuid_r(wps, msg) ||
1099 wps_build_auth_type_flags(wps, msg) ||
1100 wps_build_encr_type_flags(wps, msg) ||
1101 wps_build_conn_type_flags(wps, msg) ||
1102 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1103 wps_build_device_attrs(&wps->wps->dev, msg) ||
1104 wps_build_rf_bands(&wps->wps->dev, msg) ||
1105 wps_build_assoc_state(wps, msg) ||
1106 wps_build_config_error(msg, err) ||
1107 wps_build_os_version(&wps->wps->dev, msg)) {
1112 wps->state = RECV_M2D_ACK;
1117 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1119 struct wpabuf *msg, *plain;
1121 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1123 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1125 plain = wpabuf_alloc(200);
1129 msg = wpabuf_alloc(1000);
1135 if (wps_build_version(msg) ||
1136 wps_build_msg_type(msg, WPS_M4) ||
1137 wps_build_enrollee_nonce(wps, msg) ||
1138 wps_build_r_hash(wps, msg) ||
1139 wps_build_r_snonce1(wps, plain) ||
1140 wps_build_key_wrap_auth(wps, plain) ||
1141 wps_build_encr_settings(wps, msg, plain) ||
1142 wps_build_authenticator(wps, msg)) {
1149 wps->state = RECV_M5;
1154 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1156 struct wpabuf *msg, *plain;
1158 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1160 plain = wpabuf_alloc(200);
1164 msg = wpabuf_alloc(1000);
1170 if (wps_build_version(msg) ||
1171 wps_build_msg_type(msg, WPS_M6) ||
1172 wps_build_enrollee_nonce(wps, msg) ||
1173 wps_build_r_snonce2(wps, plain) ||
1174 wps_build_key_wrap_auth(wps, plain) ||
1175 wps_build_encr_settings(wps, msg, plain) ||
1176 wps_build_authenticator(wps, msg)) {
1183 wps->wps_pin_revealed = 1;
1184 wps->state = RECV_M7;
1189 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1191 struct wpabuf *msg, *plain;
1193 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1195 plain = wpabuf_alloc(500);
1199 msg = wpabuf_alloc(1000);
1205 if (wps_build_version(msg) ||
1206 wps_build_msg_type(msg, WPS_M8) ||
1207 wps_build_enrollee_nonce(wps, msg) ||
1208 (wps->wps->ap && wps_build_cred(wps, plain)) ||
1209 (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1210 wps_build_key_wrap_auth(wps, plain) ||
1211 wps_build_encr_settings(wps, msg, plain) ||
1212 wps_build_authenticator(wps, msg)) {
1219 wps->state = RECV_DONE;
1224 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1228 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1230 msg = wpabuf_alloc(1000);
1234 if (wps_build_version(msg) ||
1235 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1236 wps_build_enrollee_nonce(wps, msg) ||
1237 wps_build_registrar_nonce(wps, msg)) {
1246 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1250 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1252 msg = wpabuf_alloc(1000);
1256 if (wps_build_version(msg) ||
1257 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1258 wps_build_enrollee_nonce(wps, msg) ||
1259 wps_build_registrar_nonce(wps, msg) ||
1260 wps_build_config_error(msg, wps->config_error)) {
1269 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1270 enum wsc_op_code *op_code)
1274 switch (wps->state) {
1276 if (wps_get_dev_password(wps) < 0)
1277 msg = wps_build_m2d(wps);
1279 msg = wps_build_m2(wps);
1283 msg = wps_build_m2d(wps);
1287 msg = wps_build_m4(wps);
1291 msg = wps_build_m6(wps);
1295 msg = wps_build_m8(wps);
1299 msg = wps_build_wsc_ack(wps);
1303 msg = wps_build_wsc_nack(wps);
1304 *op_code = WSC_NACK;
1307 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1308 "a message", wps->state);
1313 if (*op_code == WSC_MSG && msg) {
1314 /* Save a copy of the last message for Authenticator derivation
1316 wpabuf_free(wps->last_msg);
1317 wps->last_msg = wpabuf_dup(msg);
1324 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1326 if (e_nonce == NULL) {
1327 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1331 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1332 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1333 wps->nonce_e, WPS_NONCE_LEN);
1339 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1341 if (r_nonce == NULL) {
1342 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1346 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1347 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1355 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1357 if (uuid_e == NULL) {
1358 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1362 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1363 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1369 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1371 if (pw_id == NULL) {
1372 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1376 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1377 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1383 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1385 if (e_hash1 == NULL) {
1386 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1390 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1391 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1397 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1399 if (e_hash2 == NULL) {
1400 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1404 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1405 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1411 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1413 u8 hash[SHA256_MAC_LEN];
1417 if (e_snonce1 == NULL) {
1418 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1422 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1423 WPS_SECRET_NONCE_LEN);
1425 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1426 addr[0] = e_snonce1;
1427 len[0] = WPS_SECRET_NONCE_LEN;
1428 addr[1] = wps->psk1;
1429 len[1] = WPS_PSK_LEN;
1430 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1431 len[2] = wpabuf_len(wps->dh_pubkey_e);
1432 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1433 len[3] = wpabuf_len(wps->dh_pubkey_r);
1434 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1436 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1437 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1438 "not match with the pre-committed value");
1439 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1443 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1444 "half of the device password");
1450 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1452 u8 hash[SHA256_MAC_LEN];
1456 if (e_snonce2 == NULL) {
1457 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1461 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1462 WPS_SECRET_NONCE_LEN);
1464 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1465 addr[0] = e_snonce2;
1466 len[0] = WPS_SECRET_NONCE_LEN;
1467 addr[1] = wps->psk2;
1468 len[1] = WPS_PSK_LEN;
1469 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1470 len[2] = wpabuf_len(wps->dh_pubkey_e);
1471 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1472 len[3] = wpabuf_len(wps->dh_pubkey_r);
1473 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1475 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1476 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1477 "not match with the pre-committed value");
1478 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1479 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1483 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1484 "half of the device password");
1485 wps->wps_pin_revealed = 0;
1486 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1492 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1494 if (mac_addr == NULL) {
1495 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1499 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1501 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1502 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1508 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1511 if (pk == NULL || pk_len == 0) {
1512 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1516 wpabuf_free(wps->dh_pubkey_e);
1517 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1518 if (wps->dh_pubkey_e == NULL)
1525 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1530 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1535 auth_types = WPA_GET_BE16(auth);
1537 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1539 wps->auth_type = wps->wps->auth_types & auth_types;
1540 if (wps->auth_type == 0) {
1541 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1542 "authentication types (own 0x%x Enrollee 0x%x)",
1543 wps->wps->auth_types, auth_types);
1551 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1556 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1561 encr_types = WPA_GET_BE16(encr);
1563 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1565 wps->encr_type = wps->wps->encr_types & encr_types;
1566 if (wps->encr_type == 0) {
1567 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1568 "encryption types");
1576 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1579 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1584 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1591 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1595 if (methods == NULL) {
1596 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1600 m = WPA_GET_BE16(methods);
1602 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1608 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1610 if (state == NULL) {
1611 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1616 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1623 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1627 if (assoc == NULL) {
1628 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1632 a = WPA_GET_BE16(assoc);
1633 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1639 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1644 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1648 e = WPA_GET_BE16(err);
1649 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1655 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1656 struct wps_parse_attr *attr)
1658 wpa_printf(MSG_DEBUG, "WPS: Received M1");
1660 if (wps->state != RECV_M1) {
1661 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1662 "receiving M1", wps->state);
1666 if (wps_process_uuid_e(wps, attr->uuid_e) ||
1667 wps_process_mac_addr(wps, attr->mac_addr) ||
1668 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1669 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1670 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1671 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1672 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1673 wps_process_config_methods(wps, attr->config_methods) ||
1674 wps_process_wps_state(wps, attr->wps_state) ||
1675 wps_process_device_attrs(&wps->peer_dev, attr) ||
1676 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1677 wps_process_assoc_state(wps, attr->assoc_state) ||
1678 wps_process_dev_password_id(wps, attr->dev_password_id) ||
1679 wps_process_config_error(wps, attr->config_error) ||
1680 wps_process_os_version(&wps->peer_dev, attr->os_version))
1683 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
1684 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1685 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1686 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1687 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
1688 !wps->wps->registrar->pbc)) {
1689 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1691 wps->state = SEND_M2D;
1692 return WPS_CONTINUE;
1695 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1696 if (wps_registrar_pbc_overlap(wps->wps->registrar,
1697 wps->mac_addr_e, wps->uuid_e)) {
1698 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1700 wps->state = SEND_M2D;
1701 return WPS_CONTINUE;
1703 wps_registrar_add_pbc_session(wps->wps->registrar,
1704 wps->mac_addr_e, wps->uuid_e);
1708 wps->state = SEND_M2;
1709 return WPS_CONTINUE;
1713 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1714 const struct wpabuf *msg,
1715 struct wps_parse_attr *attr)
1717 wpa_printf(MSG_DEBUG, "WPS: Received M3");
1719 if (wps->state != RECV_M3) {
1720 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1721 "receiving M3", wps->state);
1722 wps->state = SEND_WSC_NACK;
1723 return WPS_CONTINUE;
1726 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1727 wps_process_authenticator(wps, attr->authenticator, msg) ||
1728 wps_process_e_hash1(wps, attr->e_hash1) ||
1729 wps_process_e_hash2(wps, attr->e_hash2)) {
1730 wps->state = SEND_WSC_NACK;
1731 return WPS_CONTINUE;
1734 wps->state = SEND_M4;
1735 return WPS_CONTINUE;
1739 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1740 const struct wpabuf *msg,
1741 struct wps_parse_attr *attr)
1743 struct wpabuf *decrypted;
1744 struct wps_parse_attr eattr;
1746 wpa_printf(MSG_DEBUG, "WPS: Received M5");
1748 if (wps->state != RECV_M5) {
1749 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1750 "receiving M5", wps->state);
1751 wps->state = SEND_WSC_NACK;
1752 return WPS_CONTINUE;
1755 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1756 wps_process_authenticator(wps, attr->authenticator, msg)) {
1757 wps->state = SEND_WSC_NACK;
1758 return WPS_CONTINUE;
1761 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1762 attr->encr_settings_len);
1763 if (decrypted == NULL) {
1764 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1765 "Settings attribute");
1766 wps->state = SEND_WSC_NACK;
1767 return WPS_CONTINUE;
1770 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1772 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1773 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1774 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1775 wpabuf_free(decrypted);
1776 wps->state = SEND_WSC_NACK;
1777 return WPS_CONTINUE;
1779 wpabuf_free(decrypted);
1781 wps->state = SEND_M6;
1782 return WPS_CONTINUE;
1786 static int wps_process_ap_settings_r(struct wps_data *wps,
1787 struct wps_parse_attr *attr)
1792 /* AP Settings Attributes in M7 when Enrollee is an AP */
1793 if (wps_process_ap_settings(attr, &wps->cred) < 0)
1796 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1799 * TODO: Provide access to AP settings and allow changes before sending
1800 * out M8. For now, just copy the settings unchanged into M8.
1807 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1808 const struct wpabuf *msg,
1809 struct wps_parse_attr *attr)
1811 struct wpabuf *decrypted;
1812 struct wps_parse_attr eattr;
1814 wpa_printf(MSG_DEBUG, "WPS: Received M7");
1816 if (wps->state != RECV_M7) {
1817 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1818 "receiving M7", wps->state);
1819 wps->state = SEND_WSC_NACK;
1820 return WPS_CONTINUE;
1823 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1824 wps_process_authenticator(wps, attr->authenticator, msg)) {
1825 wps->state = SEND_WSC_NACK;
1826 return WPS_CONTINUE;
1829 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1830 attr->encr_settings_len);
1831 if (decrypted == NULL) {
1832 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1833 "Settings attribute");
1834 wps->state = SEND_WSC_NACK;
1835 return WPS_CONTINUE;
1838 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1840 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1841 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1842 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1843 wps_process_ap_settings_r(wps, &eattr)) {
1844 wpabuf_free(decrypted);
1845 wps->state = SEND_WSC_NACK;
1846 return WPS_CONTINUE;
1849 wpabuf_free(decrypted);
1851 wps->state = SEND_M8;
1852 return WPS_CONTINUE;
1856 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1857 const struct wpabuf *msg)
1859 struct wps_parse_attr attr;
1860 enum wps_process_res ret = WPS_CONTINUE;
1862 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1864 if (wps_parse_msg(msg, &attr) < 0)
1867 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1868 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1869 attr.version ? *attr.version : 0);
1873 if (attr.msg_type == NULL) {
1874 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1878 if (*attr.msg_type != WPS_M1 &&
1879 (attr.registrar_nonce == NULL ||
1880 os_memcmp(wps->nonce_r, attr.registrar_nonce,
1881 WPS_NONCE_LEN != 0))) {
1882 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1886 switch (*attr.msg_type) {
1888 ret = wps_process_m1(wps, &attr);
1891 ret = wps_process_m3(wps, msg, &attr);
1892 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1893 wps_fail_event(wps->wps, WPS_M3);
1896 ret = wps_process_m5(wps, msg, &attr);
1897 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1898 wps_fail_event(wps->wps, WPS_M5);
1901 ret = wps_process_m7(wps, msg, &attr);
1902 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1903 wps_fail_event(wps->wps, WPS_M7);
1906 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1911 if (ret == WPS_CONTINUE) {
1912 /* Save a copy of the last message for Authenticator derivation
1914 wpabuf_free(wps->last_msg);
1915 wps->last_msg = wpabuf_dup(msg);
1922 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1923 const struct wpabuf *msg)
1925 struct wps_parse_attr attr;
1927 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1929 if (wps_parse_msg(msg, &attr) < 0)
1932 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1933 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1934 attr.version ? *attr.version : 0);
1938 if (attr.msg_type == NULL) {
1939 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1943 if (*attr.msg_type != WPS_WSC_ACK) {
1944 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1949 if (attr.registrar_nonce == NULL ||
1950 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1952 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1956 if (attr.enrollee_nonce == NULL ||
1957 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1958 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1962 if (wps->state == RECV_M2D_ACK) {
1963 /* TODO: support for multiple registrars and sending of
1964 * multiple M2/M2D messages */
1966 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
1967 "terminate negotiation");
1974 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1975 const struct wpabuf *msg)
1977 struct wps_parse_attr attr;
1980 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1982 old_state = wps->state;
1983 wps->state = SEND_WSC_NACK;
1985 if (wps_parse_msg(msg, &attr) < 0)
1988 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1989 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1990 attr.version ? *attr.version : 0);
1994 if (attr.msg_type == NULL) {
1995 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1999 if (*attr.msg_type != WPS_WSC_NACK) {
2000 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2005 if (attr.registrar_nonce == NULL ||
2006 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2008 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2012 if (attr.enrollee_nonce == NULL ||
2013 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2014 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2018 if (attr.config_error == NULL) {
2019 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2024 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2025 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2027 switch (old_state) {
2029 wps_fail_event(wps->wps, WPS_M2);
2032 wps_fail_event(wps->wps, WPS_M4);
2035 wps_fail_event(wps->wps, WPS_M6);
2038 wps_fail_event(wps->wps, WPS_M8);
2048 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2049 const struct wpabuf *msg)
2051 struct wps_parse_attr attr;
2053 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2055 if (wps->state != RECV_DONE) {
2056 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2057 "receiving WSC_Done", wps->state);
2061 if (wps_parse_msg(msg, &attr) < 0)
2064 if (attr.version == NULL || *attr.version != WPS_VERSION) {
2065 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2066 attr.version ? *attr.version : 0);
2070 if (attr.msg_type == NULL) {
2071 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2075 if (*attr.msg_type != WPS_WSC_DONE) {
2076 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2081 if (attr.registrar_nonce == NULL ||
2082 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2084 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2088 if (attr.enrollee_nonce == NULL ||
2089 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2090 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2094 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2096 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2098 struct wps_credential cred;
2100 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2101 "on first Enrollee connection");
2103 os_memset(&cred, 0, sizeof(cred));
2104 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2105 cred.ssid_len = wps->wps->ssid_len;
2106 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2107 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2108 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2109 cred.key_len = wps->new_psk_len;
2111 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2112 wpa_hexdump_ascii_key(MSG_DEBUG,
2113 "WPS: Generated random passphrase",
2114 wps->new_psk, wps->new_psk_len);
2115 if (wps->wps->cred_cb)
2116 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2118 os_free(wps->new_psk);
2119 wps->new_psk = NULL;
2122 if (!wps->wps->ap) {
2123 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
2124 "on the modified AP configuration");
2125 if (wps->wps->cred_cb)
2126 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2130 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2131 wps->new_psk, wps->new_psk_len)) {
2132 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2135 os_free(wps->new_psk);
2136 wps->new_psk = NULL;
2140 wps_registrar_remove_pbc_session(wps->wps->registrar,
2141 wps->mac_addr_e, wps->uuid_e);
2142 wps_registrar_pbc_completed(wps->wps->registrar);
2145 wps_success_event(wps->wps);
2151 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2152 enum wsc_op_code op_code,
2153 const struct wpabuf *msg)
2155 enum wps_process_res ret;
2157 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2159 (unsigned long) wpabuf_len(msg), op_code);
2163 return wps_process_wsc_msg(wps, msg);
2165 return wps_process_wsc_ack(wps, msg);
2167 return wps_process_wsc_nack(wps, msg);
2169 ret = wps_process_wsc_done(wps, msg);
2170 if (ret == WPS_FAILURE) {
2171 wps->state = SEND_WSC_NACK;
2172 wps_fail_event(wps->wps, WPS_WSC_DONE);
2176 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);