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;
94 struct wpabuf *extra_cred;
98 static int wps_set_ie(struct wps_registrar *reg);
99 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
102 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
103 const u8 *addr, const u8 *uuid_e)
105 struct wps_pbc_session *pbc, *prev = NULL;
110 pbc = reg->pbc_sessions;
112 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
113 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
115 prev->next = pbc->next;
117 reg->pbc_sessions = pbc->next;
125 pbc = os_zalloc(sizeof(*pbc));
128 os_memcpy(pbc->addr, addr, ETH_ALEN);
130 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
133 pbc->next = reg->pbc_sessions;
134 reg->pbc_sessions = pbc;
135 pbc->timestamp = now;
137 /* remove entries that have timed out */
142 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
144 wps_free_pbc_sessions(pbc);
153 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
154 const u8 *addr, const u8 *uuid_e)
156 struct wps_pbc_session *pbc, *prev = NULL;
158 pbc = reg->pbc_sessions;
160 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
161 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
163 prev->next = pbc->next;
165 reg->pbc_sessions = pbc->next;
175 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
176 const u8 *addr, const u8 *uuid_e)
179 struct wps_pbc_session *pbc;
184 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
185 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
187 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
189 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
196 return count > 1 ? 1 : 0;
200 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
202 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
204 wpabuf_put_be16(msg, ATTR_WPS_STATE);
205 wpabuf_put_be16(msg, 1);
206 wpabuf_put_u8(msg, wps->wps_state);
211 static int wps_build_ap_setup_locked(struct wps_context *wps,
214 if (wps->ap_setup_locked) {
215 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
216 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
217 wpabuf_put_be16(msg, 1);
218 wpabuf_put_u8(msg, 1);
224 static int wps_build_selected_registrar(struct wps_registrar *reg,
227 if (!reg->selected_registrar)
229 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
230 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
231 wpabuf_put_be16(msg, 1);
232 wpabuf_put_u8(msg, 1);
237 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
240 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
241 if (!reg->selected_registrar)
243 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
244 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
245 wpabuf_put_be16(msg, 2);
246 wpabuf_put_be16(msg, id);
251 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
255 if (!reg->selected_registrar)
257 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
259 methods |= WPS_CONFIG_PUSHBUTTON;
260 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
262 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
263 wpabuf_put_be16(msg, 2);
264 wpabuf_put_be16(msg, methods);
269 static int wps_build_probe_config_methods(struct wps_registrar *reg,
274 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
275 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
276 wpabuf_put_be16(msg, 2);
277 wpabuf_put_be16(msg, methods);
282 static int wps_build_config_methods_r(struct wps_registrar *reg,
286 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
288 methods |= WPS_CONFIG_PUSHBUTTON;
289 return wps_build_config_methods(msg, methods);
293 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
295 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
296 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
297 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
298 wpabuf_put_be16(msg, 1);
299 wpabuf_put_u8(msg, resp);
305 * wps_registrar_init - Initialize WPS Registrar data
306 * @wps: Pointer to longterm WPS context
307 * @cfg: Registrar configuration
308 * Returns: Pointer to allocated Registrar data or %NULL on failure
310 * This function is used to initialize WPS Registrar functionality. It can be
311 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
312 * runs (e.g., when run as an internal Registrar in an AP). Caller is
313 * responsible for freeing the returned data with wps_registrar_deinit() when
314 * Registrar functionality is not needed anymore.
316 struct wps_registrar *
317 wps_registrar_init(struct wps_context *wps,
318 const struct wps_registrar_config *cfg)
320 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
325 reg->new_psk_cb = cfg->new_psk_cb;
326 reg->set_ie_cb = cfg->set_ie_cb;
327 reg->pin_needed_cb = cfg->pin_needed_cb;
328 reg->cb_ctx = cfg->cb_ctx;
329 reg->skip_cred_build = cfg->skip_cred_build;
330 if (cfg->extra_cred) {
331 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
332 cfg->extra_cred_len);
333 if (reg->extra_cred == NULL) {
339 if (wps_set_ie(reg)) {
340 wps_registrar_deinit(reg);
349 * wps_registrar_deinit - Deinitialize WPS Registrar data
350 * @reg: Registrar data from wps_registrar_init()
352 void wps_registrar_deinit(struct wps_registrar *reg)
356 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
357 wps_free_pins(reg->pins);
358 wps_free_pbc_sessions(reg->pbc_sessions);
359 wpabuf_free(reg->extra_cred);
365 * wps_registrar_add_pin - Configure a new PIN for Registrar
366 * @reg: Registrar data from wps_registrar_init()
367 * @uuid: UUID-E or %NULL for wildcard (any UUID)
368 * @pin: PIN (Device Password)
369 * @pin_len: Length of pin in octets
370 * Returns: 0 on success, -1 on failure
372 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
373 const u8 *pin, size_t pin_len)
375 struct wps_uuid_pin *p;
377 p = os_zalloc(sizeof(*p));
381 p->wildcard_uuid = 1;
383 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
384 p->pin = os_malloc(pin_len);
385 if (p->pin == NULL) {
389 os_memcpy(p->pin, pin, pin_len);
390 p->pin_len = pin_len;
395 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured");
396 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
397 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
398 reg->selected_registrar = 1;
407 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
408 * @reg: Registrar data from wps_registrar_init()
410 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
412 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
414 struct wps_uuid_pin *pin, *prev;
419 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
421 reg->pins = pin->next;
423 prev->next = pin->next;
424 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
425 pin->uuid, WPS_UUID_LEN);
437 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
438 const u8 *uuid, size_t *pin_len)
440 struct wps_uuid_pin *pin;
444 if (!pin->wildcard_uuid &&
445 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
451 /* Check for wildcard UUIDs since none of the UUID-specific
455 if (pin->wildcard_uuid == 1) {
456 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
457 "PIN. Assigned it for this UUID-E");
458 pin->wildcard_uuid = 2;
459 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
470 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
471 * that could otherwise avoid PIN invalidations.
474 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
475 "allow concurrent re-use");
478 *pin_len = pin->pin_len;
485 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
486 * @reg: Registrar data from wps_registrar_init()
488 * Returns: 0 on success, -1 on failure
490 * PINs are locked to enforce only one concurrent use. This function unlocks a
491 * PIN to allow it to be used again. If the specified PIN was configured using
492 * a wildcard UUID, it will be removed instead of allowing multiple uses.
494 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
496 struct wps_uuid_pin *pin;
500 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
501 if (pin->wildcard_uuid == 2) {
502 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
504 return wps_registrar_invalidate_pin(reg, uuid);
516 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
518 reg->selected_registrar = 0;
524 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
526 struct wps_registrar *reg = eloop_ctx;
528 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
529 wps_registrar_stop_pbc(reg);
534 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
535 * @reg: Registrar data from wps_registrar_init()
536 * Returns: 0 on success, -1 on failure
538 * This function is called on an AP when a push button is pushed to activate
539 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
540 * or when a PBC registration is completed.
542 int wps_registrar_button_pushed(struct wps_registrar *reg)
544 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
545 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
549 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
550 reg->selected_registrar = 1;
554 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
555 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
561 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
563 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
564 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
565 wps_registrar_stop_pbc(reg);
570 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
571 * @reg: Registrar data from wps_registrar_init()
572 * @addr: MAC address of the Probe Request sender
573 * @wps_data: WPS IE contents
575 * This function is called on an AP when a Probe Request with WPS IE is
576 * received. This is used to track PBC mode use and to detect possible overlap
577 * situation with other WPS APs.
579 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
580 const struct wpabuf *wps_data)
582 struct wps_parse_attr attr;
585 wpa_hexdump_buf(MSG_MSGDUMP,
586 "WPS: Probe Request with WPS data received",
589 if (wps_parse_msg(wps_data, &attr) < 0 ||
590 attr.version == NULL || *attr.version != WPS_VERSION) {
591 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
592 "version 0x%x", attr.version ? *attr.version : 0);
596 if (attr.config_methods == NULL) {
597 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
602 methods = WPA_GET_BE16(attr.config_methods);
603 if (!(methods & WPS_CONFIG_PUSHBUTTON))
604 return; /* Not PBC */
606 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
607 MACSTR, MAC2STR(addr));
609 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
613 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
614 const u8 *psk, size_t psk_len)
616 if (reg->new_psk_cb == NULL)
619 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
623 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
624 const struct wps_device_data *dev)
626 if (reg->pin_needed_cb == NULL)
629 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
633 static int wps_cb_set_ie(struct wps_registrar *reg,
634 const struct wpabuf *beacon_ie,
635 const struct wpabuf *probe_resp_ie)
637 if (reg->set_ie_cb == NULL)
640 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
641 wpabuf_len(beacon_ie),
642 wpabuf_head(probe_resp_ie),
643 wpabuf_len(probe_resp_ie));
647 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
648 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
653 ie = wpabuf_alloc(wpabuf_len(data) + 100);
659 pos = wpabuf_head(data);
660 end = pos + wpabuf_len(data);
663 size_t frag_len = end - pos;
666 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
667 wpabuf_put_u8(ie, 4 + frag_len);
668 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
669 wpabuf_put_data(ie, pos, frag_len);
679 static int wps_set_ie(struct wps_registrar *reg)
681 struct wpabuf *beacon;
682 struct wpabuf *probe;
685 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
687 beacon = wpabuf_alloc(300);
690 probe = wpabuf_alloc(400);
696 if (wps_build_version(beacon) ||
697 wps_build_wps_state(reg->wps, beacon) ||
698 wps_build_ap_setup_locked(reg->wps, beacon) ||
699 wps_build_selected_registrar(reg, beacon) ||
700 wps_build_sel_reg_dev_password_id(reg, beacon) ||
701 wps_build_sel_reg_config_methods(reg, beacon) ||
702 wps_build_version(probe) ||
703 wps_build_wps_state(reg->wps, probe) ||
704 wps_build_ap_setup_locked(reg->wps, probe) ||
705 wps_build_selected_registrar(reg, probe) ||
706 wps_build_sel_reg_dev_password_id(reg, probe) ||
707 wps_build_sel_reg_config_methods(reg, probe) ||
708 wps_build_resp_type(reg, probe) ||
709 wps_build_uuid_e(probe, reg->wps->uuid) ||
710 wps_build_device_attrs(®->wps->dev, probe) ||
711 wps_build_probe_config_methods(reg, probe) ||
712 wps_build_rf_bands(®->wps->dev, probe)) {
718 beacon = wps_ie_encapsulate(beacon);
719 probe = wps_ie_encapsulate(probe);
721 if (!beacon || !probe) {
727 ret = wps_cb_set_ie(reg, beacon, probe);
735 static int wps_get_dev_password(struct wps_data *wps)
740 os_free(wps->dev_password);
741 wps->dev_password = NULL;
744 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
745 pin = (const u8 *) "00000000";
748 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
752 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
754 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
759 wps->dev_password = os_malloc(pin_len);
760 if (wps->dev_password == NULL)
762 os_memcpy(wps->dev_password, pin, pin_len);
763 wps->dev_password_len = pin_len;
769 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
771 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
772 wpabuf_put_be16(msg, ATTR_UUID_R);
773 wpabuf_put_be16(msg, WPS_UUID_LEN);
774 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
779 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
785 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
787 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
788 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
789 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
791 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
792 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
793 "R-Hash derivation");
797 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
798 wpabuf_put_be16(msg, ATTR_R_HASH1);
799 wpabuf_put_be16(msg, SHA256_MAC_LEN);
800 hash = wpabuf_put(msg, SHA256_MAC_LEN);
801 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
802 addr[0] = wps->snonce;
803 len[0] = WPS_SECRET_NONCE_LEN;
805 len[1] = WPS_PSK_LEN;
806 addr[2] = wpabuf_head(wps->dh_pubkey_e);
807 len[2] = wpabuf_len(wps->dh_pubkey_e);
808 addr[3] = wpabuf_head(wps->dh_pubkey_r);
809 len[3] = wpabuf_len(wps->dh_pubkey_r);
810 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
811 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
813 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
814 wpabuf_put_be16(msg, ATTR_R_HASH2);
815 wpabuf_put_be16(msg, SHA256_MAC_LEN);
816 hash = wpabuf_put(msg, SHA256_MAC_LEN);
817 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
818 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
820 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
821 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
827 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
829 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
830 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
831 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
832 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
837 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
839 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
840 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
841 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
842 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
843 WPS_SECRET_NONCE_LEN);
848 static int wps_build_cred_network_idx(struct wpabuf *msg,
849 struct wps_credential *cred)
851 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
852 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
853 wpabuf_put_be16(msg, 1);
854 wpabuf_put_u8(msg, 1);
859 static int wps_build_cred_ssid(struct wpabuf *msg,
860 struct wps_credential *cred)
862 wpa_printf(MSG_DEBUG, "WPS: * SSID");
863 wpabuf_put_be16(msg, ATTR_SSID);
864 wpabuf_put_be16(msg, cred->ssid_len);
865 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
870 static int wps_build_cred_auth_type(struct wpabuf *msg,
871 struct wps_credential *cred)
873 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
875 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
876 wpabuf_put_be16(msg, 2);
877 wpabuf_put_be16(msg, cred->auth_type);
882 static int wps_build_cred_encr_type(struct wpabuf *msg,
883 struct wps_credential *cred)
885 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
887 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
888 wpabuf_put_be16(msg, 2);
889 wpabuf_put_be16(msg, cred->encr_type);
894 static int wps_build_cred_network_key(struct wpabuf *msg,
895 struct wps_credential *cred)
897 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
898 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
899 wpabuf_put_be16(msg, cred->key_len);
900 wpabuf_put_data(msg, cred->key, cred->key_len);
905 static int wps_build_cred_mac_addr(struct wpabuf *msg,
906 struct wps_credential *cred)
908 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
909 MAC2STR(cred->mac_addr));
910 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
911 wpabuf_put_be16(msg, ETH_ALEN);
912 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
917 static int wps_build_credential(struct wpabuf *msg,
918 struct wps_credential *cred)
920 if (wps_build_cred_network_idx(msg, cred) ||
921 wps_build_cred_ssid(msg, cred) ||
922 wps_build_cred_auth_type(msg, cred) ||
923 wps_build_cred_encr_type(msg, cred) ||
924 wps_build_cred_network_key(msg, cred) ||
925 wps_build_cred_mac_addr(msg, cred))
931 static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
935 if (wps->wps->registrar->skip_cred_build)
936 goto skip_cred_build;
938 wpa_printf(MSG_DEBUG, "WPS: * Credential");
939 os_memset(&wps->cred, 0, sizeof(wps->cred));
941 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
942 wps->cred.ssid_len = wps->wps->ssid_len;
944 /* Select the best authentication and encryption type */
945 if (wps->auth_type & WPS_AUTH_WPA2PSK)
946 wps->auth_type = WPS_AUTH_WPA2PSK;
947 else if (wps->auth_type & WPS_AUTH_WPAPSK)
948 wps->auth_type = WPS_AUTH_WPAPSK;
949 else if (wps->auth_type & WPS_AUTH_OPEN)
950 wps->auth_type = WPS_AUTH_OPEN;
951 else if (wps->auth_type & WPS_AUTH_SHARED)
952 wps->auth_type = WPS_AUTH_SHARED;
954 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
958 wps->cred.auth_type = wps->auth_type;
960 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
961 wps->auth_type == WPS_AUTH_WPAPSK) {
962 if (wps->encr_type & WPS_ENCR_AES)
963 wps->encr_type = WPS_ENCR_AES;
964 else if (wps->encr_type & WPS_ENCR_TKIP)
965 wps->encr_type = WPS_ENCR_TKIP;
967 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
968 "type for WPA/WPA2");
972 if (wps->encr_type & WPS_ENCR_WEP)
973 wps->encr_type = WPS_ENCR_WEP;
974 else if (wps->encr_type & WPS_ENCR_NONE)
975 wps->encr_type = WPS_ENCR_NONE;
977 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
978 "type for non-WPA/WPA2 mode");
982 wps->cred.encr_type = wps->encr_type;
983 /* Set MAC address in the Credential to be the AP's address (BSSID) */
984 os_memcpy(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN);
986 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap) {
988 /* Generate a random passphrase */
989 if (os_get_random(r, sizeof(r)) < 0)
991 os_free(wps->new_psk);
992 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
993 if (wps->new_psk == NULL)
995 wps->new_psk_len--; /* remove newline */
996 while (wps->new_psk_len &&
997 wps->new_psk[wps->new_psk_len - 1] == '=')
999 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1000 wps->new_psk, wps->new_psk_len);
1001 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1002 wps->cred.key_len = wps->new_psk_len;
1003 } else if (wps->wps->network_key) {
1004 os_memcpy(wps->cred.key, wps->wps->network_key,
1005 wps->wps->network_key_len);
1006 wps->cred.key_len = wps->wps->network_key_len;
1007 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1009 /* Generate a random per-device PSK */
1010 os_free(wps->new_psk);
1011 wps->new_psk_len = 32;
1012 wps->new_psk = os_malloc(wps->new_psk_len);
1013 if (wps->new_psk == NULL)
1015 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1016 os_free(wps->new_psk);
1017 wps->new_psk = NULL;
1020 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1021 wps->new_psk, wps->new_psk_len);
1022 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1024 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1025 wps->cred.key_len = wps->new_psk_len * 2;
1028 cred = wpabuf_alloc(200);
1032 if (wps_build_credential(cred, &wps->cred)) {
1037 wpabuf_put_be16(msg, ATTR_CRED);
1038 wpabuf_put_be16(msg, wpabuf_len(cred));
1039 wpabuf_put_buf(msg, cred);
1043 if (wps->wps->registrar->extra_cred) {
1044 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1045 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1052 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1054 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1056 if (wps_build_credential(msg, &wps->cred))
1063 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1067 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1069 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1070 wps->nonce_r, WPS_NONCE_LEN);
1071 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1073 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1074 msg = wpabuf_alloc(1000);
1078 if (wps_build_version(msg) ||
1079 wps_build_msg_type(msg, WPS_M2) ||
1080 wps_build_enrollee_nonce(wps, msg) ||
1081 wps_build_registrar_nonce(wps, msg) ||
1082 wps_build_uuid_r(wps, msg) ||
1083 wps_build_public_key(wps, msg) ||
1084 wps_derive_keys(wps) ||
1085 wps_build_auth_type_flags(wps, msg) ||
1086 wps_build_encr_type_flags(wps, msg) ||
1087 wps_build_conn_type_flags(wps, msg) ||
1088 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1089 wps_build_device_attrs(&wps->wps->dev, msg) ||
1090 wps_build_rf_bands(&wps->wps->dev, msg) ||
1091 wps_build_assoc_state(wps, msg) ||
1092 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1093 wps_build_dev_password_id(msg, DEV_PW_DEFAULT) ||
1094 wps_build_os_version(&wps->wps->dev, msg) ||
1095 wps_build_authenticator(wps, msg)) {
1100 wps->state = RECV_M3;
1105 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1108 u16 err = WPS_CFG_NO_ERROR;
1110 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1111 msg = wpabuf_alloc(1000);
1115 if (wps->wps->ap && wps->wps->ap_setup_locked)
1116 err = WPS_CFG_SETUP_LOCKED;
1118 if (wps_build_version(msg) ||
1119 wps_build_msg_type(msg, WPS_M2D) ||
1120 wps_build_enrollee_nonce(wps, msg) ||
1121 wps_build_registrar_nonce(wps, msg) ||
1122 wps_build_uuid_r(wps, msg) ||
1123 wps_build_auth_type_flags(wps, msg) ||
1124 wps_build_encr_type_flags(wps, msg) ||
1125 wps_build_conn_type_flags(wps, msg) ||
1126 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1127 wps_build_device_attrs(&wps->wps->dev, msg) ||
1128 wps_build_rf_bands(&wps->wps->dev, msg) ||
1129 wps_build_assoc_state(wps, msg) ||
1130 wps_build_config_error(msg, err) ||
1131 wps_build_os_version(&wps->wps->dev, msg)) {
1136 wps->state = RECV_M2D_ACK;
1141 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1143 struct wpabuf *msg, *plain;
1145 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1147 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1149 plain = wpabuf_alloc(200);
1153 msg = wpabuf_alloc(1000);
1159 if (wps_build_version(msg) ||
1160 wps_build_msg_type(msg, WPS_M4) ||
1161 wps_build_enrollee_nonce(wps, msg) ||
1162 wps_build_r_hash(wps, msg) ||
1163 wps_build_r_snonce1(wps, plain) ||
1164 wps_build_key_wrap_auth(wps, plain) ||
1165 wps_build_encr_settings(wps, msg, plain) ||
1166 wps_build_authenticator(wps, msg)) {
1173 wps->state = RECV_M5;
1178 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1180 struct wpabuf *msg, *plain;
1182 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1184 plain = wpabuf_alloc(200);
1188 msg = wpabuf_alloc(1000);
1194 if (wps_build_version(msg) ||
1195 wps_build_msg_type(msg, WPS_M6) ||
1196 wps_build_enrollee_nonce(wps, msg) ||
1197 wps_build_r_snonce2(wps, plain) ||
1198 wps_build_key_wrap_auth(wps, plain) ||
1199 wps_build_encr_settings(wps, msg, plain) ||
1200 wps_build_authenticator(wps, msg)) {
1207 wps->wps_pin_revealed = 1;
1208 wps->state = RECV_M7;
1213 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1215 struct wpabuf *msg, *plain;
1217 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1219 plain = wpabuf_alloc(500);
1223 msg = wpabuf_alloc(1000);
1229 if (wps_build_version(msg) ||
1230 wps_build_msg_type(msg, WPS_M8) ||
1231 wps_build_enrollee_nonce(wps, msg) ||
1232 (wps->wps->ap && wps_build_cred(wps, plain)) ||
1233 (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1234 wps_build_key_wrap_auth(wps, plain) ||
1235 wps_build_encr_settings(wps, msg, plain) ||
1236 wps_build_authenticator(wps, msg)) {
1243 wps->state = RECV_DONE;
1248 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1252 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1254 msg = wpabuf_alloc(1000);
1258 if (wps_build_version(msg) ||
1259 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1260 wps_build_enrollee_nonce(wps, msg) ||
1261 wps_build_registrar_nonce(wps, msg)) {
1270 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1274 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1276 msg = wpabuf_alloc(1000);
1280 if (wps_build_version(msg) ||
1281 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1282 wps_build_enrollee_nonce(wps, msg) ||
1283 wps_build_registrar_nonce(wps, msg) ||
1284 wps_build_config_error(msg, wps->config_error)) {
1293 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1294 enum wsc_op_code *op_code)
1298 switch (wps->state) {
1300 if (wps_get_dev_password(wps) < 0)
1301 msg = wps_build_m2d(wps);
1303 msg = wps_build_m2(wps);
1307 msg = wps_build_m2d(wps);
1311 msg = wps_build_m4(wps);
1315 msg = wps_build_m6(wps);
1319 msg = wps_build_m8(wps);
1323 msg = wps_build_wsc_ack(wps);
1327 msg = wps_build_wsc_nack(wps);
1328 *op_code = WSC_NACK;
1331 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1332 "a message", wps->state);
1337 if (*op_code == WSC_MSG && msg) {
1338 /* Save a copy of the last message for Authenticator derivation
1340 wpabuf_free(wps->last_msg);
1341 wps->last_msg = wpabuf_dup(msg);
1348 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1350 if (e_nonce == NULL) {
1351 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1355 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1356 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1357 wps->nonce_e, WPS_NONCE_LEN);
1363 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1365 if (r_nonce == NULL) {
1366 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1370 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1371 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1379 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1381 if (uuid_e == NULL) {
1382 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1386 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1387 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1393 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1395 if (pw_id == NULL) {
1396 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1400 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1401 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1407 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1409 if (e_hash1 == NULL) {
1410 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1414 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1415 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1421 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1423 if (e_hash2 == NULL) {
1424 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1428 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1429 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1435 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1437 u8 hash[SHA256_MAC_LEN];
1441 if (e_snonce1 == NULL) {
1442 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1446 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1447 WPS_SECRET_NONCE_LEN);
1449 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1450 addr[0] = e_snonce1;
1451 len[0] = WPS_SECRET_NONCE_LEN;
1452 addr[1] = wps->psk1;
1453 len[1] = WPS_PSK_LEN;
1454 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1455 len[2] = wpabuf_len(wps->dh_pubkey_e);
1456 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1457 len[3] = wpabuf_len(wps->dh_pubkey_r);
1458 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1460 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1461 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1462 "not match with the pre-committed value");
1463 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1467 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1468 "half of the device password");
1474 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1476 u8 hash[SHA256_MAC_LEN];
1480 if (e_snonce2 == NULL) {
1481 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1485 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1486 WPS_SECRET_NONCE_LEN);
1488 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1489 addr[0] = e_snonce2;
1490 len[0] = WPS_SECRET_NONCE_LEN;
1491 addr[1] = wps->psk2;
1492 len[1] = WPS_PSK_LEN;
1493 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1494 len[2] = wpabuf_len(wps->dh_pubkey_e);
1495 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1496 len[3] = wpabuf_len(wps->dh_pubkey_r);
1497 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1499 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1500 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1501 "not match with the pre-committed value");
1502 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1503 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1507 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1508 "half of the device password");
1509 wps->wps_pin_revealed = 0;
1510 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1516 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1518 if (mac_addr == NULL) {
1519 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1523 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1525 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1526 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1532 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1535 if (pk == NULL || pk_len == 0) {
1536 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1540 wpabuf_free(wps->dh_pubkey_e);
1541 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1542 if (wps->dh_pubkey_e == NULL)
1549 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1554 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1559 auth_types = WPA_GET_BE16(auth);
1561 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1563 wps->auth_type = wps->wps->auth_types & auth_types;
1564 if (wps->auth_type == 0) {
1565 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1566 "authentication types (own 0x%x Enrollee 0x%x)",
1567 wps->wps->auth_types, auth_types);
1575 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1580 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1585 encr_types = WPA_GET_BE16(encr);
1587 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1589 wps->encr_type = wps->wps->encr_types & encr_types;
1590 if (wps->encr_type == 0) {
1591 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1592 "encryption types");
1600 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1603 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1608 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1615 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1619 if (methods == NULL) {
1620 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1624 m = WPA_GET_BE16(methods);
1626 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1632 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1634 if (state == NULL) {
1635 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1640 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1647 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1651 if (assoc == NULL) {
1652 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1656 a = WPA_GET_BE16(assoc);
1657 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1663 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1668 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1672 e = WPA_GET_BE16(err);
1673 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1679 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1680 struct wps_parse_attr *attr)
1682 wpa_printf(MSG_DEBUG, "WPS: Received M1");
1684 if (wps->state != RECV_M1) {
1685 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1686 "receiving M1", wps->state);
1690 if (wps_process_uuid_e(wps, attr->uuid_e) ||
1691 wps_process_mac_addr(wps, attr->mac_addr) ||
1692 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1693 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1694 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1695 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1696 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1697 wps_process_config_methods(wps, attr->config_methods) ||
1698 wps_process_wps_state(wps, attr->wps_state) ||
1699 wps_process_device_attrs(&wps->peer_dev, attr) ||
1700 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1701 wps_process_assoc_state(wps, attr->assoc_state) ||
1702 wps_process_dev_password_id(wps, attr->dev_password_id) ||
1703 wps_process_config_error(wps, attr->config_error) ||
1704 wps_process_os_version(&wps->peer_dev, attr->os_version))
1707 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
1708 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1709 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1710 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1711 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
1712 !wps->wps->registrar->pbc)) {
1713 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1715 wps->state = SEND_M2D;
1716 return WPS_CONTINUE;
1719 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1720 if (wps_registrar_pbc_overlap(wps->wps->registrar,
1721 wps->mac_addr_e, wps->uuid_e)) {
1722 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1724 wps->state = SEND_M2D;
1725 return WPS_CONTINUE;
1727 wps_registrar_add_pbc_session(wps->wps->registrar,
1728 wps->mac_addr_e, wps->uuid_e);
1732 wps->state = SEND_M2;
1733 return WPS_CONTINUE;
1737 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1738 const struct wpabuf *msg,
1739 struct wps_parse_attr *attr)
1741 wpa_printf(MSG_DEBUG, "WPS: Received M3");
1743 if (wps->state != RECV_M3) {
1744 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1745 "receiving M3", wps->state);
1746 wps->state = SEND_WSC_NACK;
1747 return WPS_CONTINUE;
1750 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1751 wps_process_authenticator(wps, attr->authenticator, msg) ||
1752 wps_process_e_hash1(wps, attr->e_hash1) ||
1753 wps_process_e_hash2(wps, attr->e_hash2)) {
1754 wps->state = SEND_WSC_NACK;
1755 return WPS_CONTINUE;
1758 wps->state = SEND_M4;
1759 return WPS_CONTINUE;
1763 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1764 const struct wpabuf *msg,
1765 struct wps_parse_attr *attr)
1767 struct wpabuf *decrypted;
1768 struct wps_parse_attr eattr;
1770 wpa_printf(MSG_DEBUG, "WPS: Received M5");
1772 if (wps->state != RECV_M5) {
1773 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1774 "receiving M5", wps->state);
1775 wps->state = SEND_WSC_NACK;
1776 return WPS_CONTINUE;
1779 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1780 wps_process_authenticator(wps, attr->authenticator, msg)) {
1781 wps->state = SEND_WSC_NACK;
1782 return WPS_CONTINUE;
1785 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1786 attr->encr_settings_len);
1787 if (decrypted == NULL) {
1788 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1789 "Settings attribute");
1790 wps->state = SEND_WSC_NACK;
1791 return WPS_CONTINUE;
1794 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1796 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1797 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1798 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1799 wpabuf_free(decrypted);
1800 wps->state = SEND_WSC_NACK;
1801 return WPS_CONTINUE;
1803 wpabuf_free(decrypted);
1805 wps->state = SEND_M6;
1806 return WPS_CONTINUE;
1810 static int wps_process_ap_settings_r(struct wps_data *wps,
1811 struct wps_parse_attr *attr)
1816 /* AP Settings Attributes in M7 when Enrollee is an AP */
1817 if (wps_process_ap_settings(attr, &wps->cred) < 0)
1820 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1823 * TODO: Provide access to AP settings and allow changes before sending
1824 * out M8. For now, just copy the settings unchanged into M8.
1831 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1832 const struct wpabuf *msg,
1833 struct wps_parse_attr *attr)
1835 struct wpabuf *decrypted;
1836 struct wps_parse_attr eattr;
1838 wpa_printf(MSG_DEBUG, "WPS: Received M7");
1840 if (wps->state != RECV_M7) {
1841 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1842 "receiving M7", wps->state);
1843 wps->state = SEND_WSC_NACK;
1844 return WPS_CONTINUE;
1847 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1848 wps_process_authenticator(wps, attr->authenticator, msg)) {
1849 wps->state = SEND_WSC_NACK;
1850 return WPS_CONTINUE;
1853 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1854 attr->encr_settings_len);
1855 if (decrypted == NULL) {
1856 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1857 "Settings attribute");
1858 wps->state = SEND_WSC_NACK;
1859 return WPS_CONTINUE;
1862 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1864 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1865 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1866 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1867 wps_process_ap_settings_r(wps, &eattr)) {
1868 wpabuf_free(decrypted);
1869 wps->state = SEND_WSC_NACK;
1870 return WPS_CONTINUE;
1873 wpabuf_free(decrypted);
1875 wps->state = SEND_M8;
1876 return WPS_CONTINUE;
1880 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1881 const struct wpabuf *msg)
1883 struct wps_parse_attr attr;
1884 enum wps_process_res ret = WPS_CONTINUE;
1886 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1888 if (wps_parse_msg(msg, &attr) < 0)
1891 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1892 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1893 attr.version ? *attr.version : 0);
1897 if (attr.msg_type == NULL) {
1898 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1902 if (*attr.msg_type != WPS_M1 &&
1903 (attr.registrar_nonce == NULL ||
1904 os_memcmp(wps->nonce_r, attr.registrar_nonce,
1905 WPS_NONCE_LEN != 0))) {
1906 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1910 switch (*attr.msg_type) {
1912 ret = wps_process_m1(wps, &attr);
1915 ret = wps_process_m3(wps, msg, &attr);
1916 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1917 wps_fail_event(wps->wps, WPS_M3);
1920 ret = wps_process_m5(wps, msg, &attr);
1921 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1922 wps_fail_event(wps->wps, WPS_M5);
1925 ret = wps_process_m7(wps, msg, &attr);
1926 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1927 wps_fail_event(wps->wps, WPS_M7);
1930 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1935 if (ret == WPS_CONTINUE) {
1936 /* Save a copy of the last message for Authenticator derivation
1938 wpabuf_free(wps->last_msg);
1939 wps->last_msg = wpabuf_dup(msg);
1946 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1947 const struct wpabuf *msg)
1949 struct wps_parse_attr attr;
1951 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1953 if (wps_parse_msg(msg, &attr) < 0)
1956 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1957 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1958 attr.version ? *attr.version : 0);
1962 if (attr.msg_type == NULL) {
1963 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1967 if (*attr.msg_type != WPS_WSC_ACK) {
1968 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1973 if (attr.registrar_nonce == NULL ||
1974 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1976 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1980 if (attr.enrollee_nonce == NULL ||
1981 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1982 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1986 if (wps->state == RECV_M2D_ACK) {
1987 /* TODO: support for multiple registrars and sending of
1988 * multiple M2/M2D messages */
1990 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
1991 "terminate negotiation");
1998 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1999 const struct wpabuf *msg)
2001 struct wps_parse_attr attr;
2004 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2006 old_state = wps->state;
2007 wps->state = SEND_WSC_NACK;
2009 if (wps_parse_msg(msg, &attr) < 0)
2012 if (attr.version == NULL || *attr.version != WPS_VERSION) {
2013 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2014 attr.version ? *attr.version : 0);
2018 if (attr.msg_type == NULL) {
2019 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2023 if (*attr.msg_type != WPS_WSC_NACK) {
2024 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2029 if (attr.registrar_nonce == NULL ||
2030 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2032 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2036 if (attr.enrollee_nonce == NULL ||
2037 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2038 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2042 if (attr.config_error == NULL) {
2043 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2048 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2049 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2051 switch (old_state) {
2053 wps_fail_event(wps->wps, WPS_M2);
2056 wps_fail_event(wps->wps, WPS_M4);
2059 wps_fail_event(wps->wps, WPS_M6);
2062 wps_fail_event(wps->wps, WPS_M8);
2072 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2073 const struct wpabuf *msg)
2075 struct wps_parse_attr attr;
2077 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2079 if (wps->state != RECV_DONE) {
2080 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2081 "receiving WSC_Done", wps->state);
2085 if (wps_parse_msg(msg, &attr) < 0)
2088 if (attr.version == NULL || *attr.version != WPS_VERSION) {
2089 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2090 attr.version ? *attr.version : 0);
2094 if (attr.msg_type == NULL) {
2095 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2099 if (*attr.msg_type != WPS_WSC_DONE) {
2100 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2105 if (attr.registrar_nonce == NULL ||
2106 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2108 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2112 if (attr.enrollee_nonce == NULL ||
2113 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2114 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2118 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2120 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2122 struct wps_credential cred;
2124 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2125 "on first Enrollee connection");
2127 os_memset(&cred, 0, sizeof(cred));
2128 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2129 cred.ssid_len = wps->wps->ssid_len;
2130 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2131 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2132 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2133 cred.key_len = wps->new_psk_len;
2135 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2136 wpa_hexdump_ascii_key(MSG_DEBUG,
2137 "WPS: Generated random passphrase",
2138 wps->new_psk, wps->new_psk_len);
2139 if (wps->wps->cred_cb)
2140 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2142 os_free(wps->new_psk);
2143 wps->new_psk = NULL;
2146 if (!wps->wps->ap) {
2147 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
2148 "on the modified AP configuration");
2149 if (wps->wps->cred_cb)
2150 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2154 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2155 wps->new_psk, wps->new_psk_len)) {
2156 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2159 os_free(wps->new_psk);
2160 wps->new_psk = NULL;
2164 wps_registrar_remove_pbc_session(wps->wps->registrar,
2165 wps->mac_addr_e, wps->uuid_e);
2166 wps_registrar_pbc_completed(wps->wps->registrar);
2169 wps_success_event(wps->wps);
2175 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2176 enum wsc_op_code op_code,
2177 const struct wpabuf *msg)
2179 enum wps_process_res ret;
2181 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2183 (unsigned long) wpabuf_len(msg), op_code);
2187 return wps_process_wsc_msg(wps, msg);
2189 return wps_process_wsc_ack(wps, msg);
2191 return wps_process_wsc_nack(wps, msg);
2193 ret = wps_process_wsc_done(wps, msg);
2194 if (ret == WPS_FAILURE) {
2195 wps->state = SEND_WSC_NACK;
2196 wps_fail_event(wps->wps, WPS_WSC_DONE);
2200 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);