2 * Wi-Fi Protected Setup - Registrar
3 * Copyright (c) 2008-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/base64.h"
13 #include "utils/eloop.h"
14 #include "utils/uuid.h"
15 #include "utils/list.h"
16 #include "crypto/crypto.h"
17 #include "crypto/sha256.h"
18 #include "crypto/random.h"
19 #include "common/ieee802_11_defs.h"
21 #include "wps_dev_attr.h"
23 #include "wps_upnp_i.h"
25 #ifndef CONFIG_WPS_STRICT
26 #define WPS_WORKAROUNDS
27 #endif /* CONFIG_WPS_STRICT */
31 struct wps_nfc_pw_token {
33 u8 pubkey_hash[WPS_OOB_PUBKEY_HASH_LEN];
35 u8 dev_pw[WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1];
40 static void wps_remove_nfc_pw_token(struct wps_nfc_pw_token *token)
42 dl_list_del(&token->list);
47 static void wps_free_nfc_pw_tokens(struct dl_list *tokens, u16 pw_id)
49 struct wps_nfc_pw_token *token, *prev;
50 dl_list_for_each_safe(token, prev, tokens, struct wps_nfc_pw_token,
52 if (pw_id == 0 || pw_id == token->pw_id)
53 wps_remove_nfc_pw_token(token);
58 static struct wps_nfc_pw_token * wps_get_nfc_pw_token(struct dl_list *tokens,
61 struct wps_nfc_pw_token *token;
62 dl_list_for_each(token, tokens, struct wps_nfc_pw_token, list) {
63 if (pw_id == token->pw_id)
69 #else /* CONFIG_WPS_NFC */
71 #define wps_free_nfc_pw_tokens(t, p) do { } while (0)
73 #endif /* CONFIG_WPS_NFC */
78 u8 uuid[WPS_UUID_LEN];
82 #define PIN_LOCKED BIT(0)
83 #define PIN_EXPIRES BIT(1)
85 struct os_time expiration;
86 u8 enrollee_addr[ETH_ALEN];
90 static void wps_free_pin(struct wps_uuid_pin *pin)
97 static void wps_remove_pin(struct wps_uuid_pin *pin)
99 dl_list_del(&pin->list);
104 static void wps_free_pins(struct dl_list *pins)
106 struct wps_uuid_pin *pin, *prev;
107 dl_list_for_each_safe(pin, prev, pins, struct wps_uuid_pin, list)
112 struct wps_pbc_session {
113 struct wps_pbc_session *next;
115 u8 uuid_e[WPS_UUID_LEN];
116 struct os_time timestamp;
120 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
122 struct wps_pbc_session *prev;
132 struct wps_registrar_device {
133 struct wps_registrar_device *next;
134 struct wps_device_data dev;
135 u8 uuid[WPS_UUID_LEN];
139 struct wps_registrar {
140 struct wps_context *wps;
143 int selected_registrar;
145 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
146 const u8 *psk, size_t psk_len);
147 int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
148 struct wpabuf *probe_resp_ie);
149 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
150 const struct wps_device_data *dev);
151 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
152 const u8 *uuid_e, const u8 *dev_pw,
154 void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
155 u16 sel_reg_config_methods);
156 void (*enrollee_seen_cb)(void *ctx, const u8 *addr, const u8 *uuid_e,
157 const u8 *pri_dev_type, u16 config_methods,
158 u16 dev_password_id, u8 request_type,
159 const char *dev_name);
163 struct dl_list nfc_pw_tokens;
164 struct wps_pbc_session *pbc_sessions;
167 struct wpabuf *extra_cred;
168 int disable_auto_conf;
170 int sel_reg_dev_password_id_override;
171 int sel_reg_config_methods_override;
174 int force_per_enrollee_psk;
176 struct wps_registrar_device *devices;
178 int force_pbc_overlap;
180 u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
181 u8 authorized_macs_union[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
183 u8 p2p_dev_addr[ETH_ALEN];
185 u8 pbc_ignore_uuid[WPS_UUID_LEN];
186 struct os_time pbc_ignore_start;
190 static int wps_set_ie(struct wps_registrar *reg);
191 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
192 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
194 static void wps_registrar_remove_pin(struct wps_registrar *reg,
195 struct wps_uuid_pin *pin);
198 static void wps_registrar_add_authorized_mac(struct wps_registrar *reg,
202 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC " MACSTR,
204 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
205 if (os_memcmp(reg->authorized_macs[i], addr, ETH_ALEN) == 0) {
206 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was "
207 "already in the list");
208 return; /* already in list */
210 for (i = WPS_MAX_AUTHORIZED_MACS - 1; i > 0; i--)
211 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i - 1],
213 os_memcpy(reg->authorized_macs[0], addr, ETH_ALEN);
214 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
215 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
219 static void wps_registrar_remove_authorized_mac(struct wps_registrar *reg,
223 wpa_printf(MSG_DEBUG, "WPS: Remove authorized MAC " MACSTR,
225 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++) {
226 if (os_memcmp(reg->authorized_macs, addr, ETH_ALEN) == 0)
229 if (i == WPS_MAX_AUTHORIZED_MACS) {
230 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was not in the "
232 return; /* not in the list */
234 for (; i + 1 < WPS_MAX_AUTHORIZED_MACS; i++)
235 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i + 1],
237 os_memset(reg->authorized_macs[WPS_MAX_AUTHORIZED_MACS - 1], 0,
239 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
240 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
244 static void wps_free_devices(struct wps_registrar_device *dev)
246 struct wps_registrar_device *prev;
251 wps_device_data_free(&prev->dev);
257 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
260 struct wps_registrar_device *dev;
262 for (dev = reg->devices; dev; dev = dev->next) {
263 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
270 static void wps_device_clone_data(struct wps_device_data *dst,
271 struct wps_device_data *src)
273 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
274 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
276 #define WPS_STRDUP(n) \
278 dst->n = src->n ? os_strdup(src->n) : NULL
280 WPS_STRDUP(device_name);
281 WPS_STRDUP(manufacturer);
282 WPS_STRDUP(model_name);
283 WPS_STRDUP(model_number);
284 WPS_STRDUP(serial_number);
289 int wps_device_store(struct wps_registrar *reg,
290 struct wps_device_data *dev, const u8 *uuid)
292 struct wps_registrar_device *d;
294 d = wps_device_get(reg, dev->mac_addr);
296 d = os_zalloc(sizeof(*d));
299 d->next = reg->devices;
303 wps_device_clone_data(&d->dev, dev);
304 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
310 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
311 const u8 *addr, const u8 *uuid_e)
313 struct wps_pbc_session *pbc, *prev = NULL;
318 pbc = reg->pbc_sessions;
320 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
321 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
323 prev->next = pbc->next;
325 reg->pbc_sessions = pbc->next;
333 pbc = os_zalloc(sizeof(*pbc));
336 os_memcpy(pbc->addr, addr, ETH_ALEN);
338 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
341 pbc->next = reg->pbc_sessions;
342 reg->pbc_sessions = pbc;
343 pbc->timestamp = now;
345 /* remove entries that have timed out */
350 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
352 wps_free_pbc_sessions(pbc);
361 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
363 const u8 *p2p_dev_addr)
365 struct wps_pbc_session *pbc, *prev = NULL, *tmp;
367 pbc = reg->pbc_sessions;
369 if (os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0 ||
371 (p2p_dev_addr && !is_zero_ether_addr(pbc->addr) &&
372 os_memcmp(pbc->addr, p2p_dev_addr, ETH_ALEN) ==
374 (p2p_dev_addr && !is_zero_ether_addr(reg->p2p_dev_addr) &&
375 os_memcmp(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
379 prev->next = pbc->next;
381 reg->pbc_sessions = pbc->next;
384 wpa_printf(MSG_DEBUG, "WPS: Removing PBC session for "
385 "addr=" MACSTR, MAC2STR(tmp->addr));
386 wpa_hexdump(MSG_DEBUG, "WPS: Removed UUID-E",
387 tmp->uuid_e, WPS_UUID_LEN);
397 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
398 const u8 *addr, const u8 *uuid_e)
401 struct wps_pbc_session *pbc;
402 struct wps_pbc_session *first = NULL;
407 wpa_printf(MSG_DEBUG, "WPS: Checking active PBC sessions for overlap");
410 wpa_printf(MSG_DEBUG, "WPS: Add one for the requested UUID");
411 wpa_hexdump(MSG_DEBUG, "WPS: Requested UUID",
412 uuid_e, WPS_UUID_LEN);
416 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
417 wpa_printf(MSG_DEBUG, "WPS: Consider PBC session with " MACSTR,
419 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E",
420 pbc->uuid_e, WPS_UUID_LEN);
421 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
422 wpa_printf(MSG_DEBUG, "WPS: PBC walk time has "
427 os_memcmp(pbc->uuid_e, first->uuid_e, WPS_UUID_LEN) == 0) {
428 wpa_printf(MSG_DEBUG, "WPS: Same Enrollee");
429 continue; /* same Enrollee */
431 if (uuid_e == NULL ||
432 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN)) {
433 wpa_printf(MSG_DEBUG, "WPS: New Enrollee");
440 wpa_printf(MSG_DEBUG, "WPS: %u active PBC session(s) found", count);
442 return count > 1 ? 1 : 0;
446 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
448 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
450 wpabuf_put_be16(msg, ATTR_WPS_STATE);
451 wpabuf_put_be16(msg, 1);
452 wpabuf_put_u8(msg, wps->wps_state);
457 #ifdef CONFIG_WPS_UPNP
458 static void wps_registrar_free_pending_m2(struct wps_context *wps)
460 struct upnp_pending_message *p, *p2, *prev = NULL;
463 if (p->type == WPS_M2 || p->type == WPS_M2D) {
465 wps->upnp_msgs = p->next;
467 prev->next = p->next;
468 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
471 wpabuf_free(p2->msg);
479 #endif /* CONFIG_WPS_UPNP */
482 static int wps_build_ap_setup_locked(struct wps_context *wps,
485 if (wps->ap_setup_locked && wps->ap_setup_locked != 2) {
486 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
487 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
488 wpabuf_put_be16(msg, 1);
489 wpabuf_put_u8(msg, 1);
495 static int wps_build_selected_registrar(struct wps_registrar *reg,
498 if (!reg->sel_reg_union)
500 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
501 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
502 wpabuf_put_be16(msg, 1);
503 wpabuf_put_u8(msg, 1);
508 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
511 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
512 if (!reg->sel_reg_union)
514 if (reg->sel_reg_dev_password_id_override >= 0)
515 id = reg->sel_reg_dev_password_id_override;
516 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
517 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
518 wpabuf_put_be16(msg, 2);
519 wpabuf_put_be16(msg, id);
524 static int wps_build_sel_pbc_reg_uuid_e(struct wps_registrar *reg,
527 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
528 if (!reg->sel_reg_union)
530 if (reg->sel_reg_dev_password_id_override >= 0)
531 id = reg->sel_reg_dev_password_id_override;
532 if (id != DEV_PW_PUSHBUTTON || !reg->dualband)
534 return wps_build_uuid_e(msg, reg->wps->uuid);
538 static void wps_set_pushbutton(u16 *methods, u16 conf_methods)
540 *methods |= WPS_CONFIG_PUSHBUTTON;
542 if ((conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON) ==
543 WPS_CONFIG_VIRT_PUSHBUTTON)
544 *methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
545 if ((conf_methods & WPS_CONFIG_PHY_PUSHBUTTON) ==
546 WPS_CONFIG_PHY_PUSHBUTTON)
547 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
548 if ((*methods & WPS_CONFIG_VIRT_PUSHBUTTON) !=
549 WPS_CONFIG_VIRT_PUSHBUTTON &&
550 (*methods & WPS_CONFIG_PHY_PUSHBUTTON) !=
551 WPS_CONFIG_PHY_PUSHBUTTON) {
553 * Required to include virtual/physical flag, but we were not
554 * configured with push button type, so have to default to one
557 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
559 #endif /* CONFIG_WPS2 */
563 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
567 if (!reg->sel_reg_union)
569 methods = reg->wps->config_methods;
570 methods &= ~WPS_CONFIG_PUSHBUTTON;
572 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
573 WPS_CONFIG_PHY_PUSHBUTTON);
574 #endif /* CONFIG_WPS2 */
576 wps_set_pushbutton(&methods, reg->wps->config_methods);
577 if (reg->sel_reg_config_methods_override >= 0)
578 methods = reg->sel_reg_config_methods_override;
579 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
581 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
582 wpabuf_put_be16(msg, 2);
583 wpabuf_put_be16(msg, methods);
588 static int wps_build_probe_config_methods(struct wps_registrar *reg,
593 * These are the methods that the AP supports as an Enrollee for adding
594 * external Registrars.
596 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
598 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
599 WPS_CONFIG_PHY_PUSHBUTTON);
600 #endif /* CONFIG_WPS2 */
601 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
602 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
603 wpabuf_put_be16(msg, 2);
604 wpabuf_put_be16(msg, methods);
609 static int wps_build_config_methods_r(struct wps_registrar *reg,
612 return wps_build_config_methods(msg, reg->wps->config_methods);
616 const u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count)
621 while (*count < WPS_MAX_AUTHORIZED_MACS) {
622 if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
626 #endif /* CONFIG_WPS2 */
628 return (const u8 *) reg->authorized_macs_union;
633 * wps_registrar_init - Initialize WPS Registrar data
634 * @wps: Pointer to longterm WPS context
635 * @cfg: Registrar configuration
636 * Returns: Pointer to allocated Registrar data or %NULL on failure
638 * This function is used to initialize WPS Registrar functionality. It can be
639 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
640 * runs (e.g., when run as an internal Registrar in an AP). Caller is
641 * responsible for freeing the returned data with wps_registrar_deinit() when
642 * Registrar functionality is not needed anymore.
644 struct wps_registrar *
645 wps_registrar_init(struct wps_context *wps,
646 const struct wps_registrar_config *cfg)
648 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
652 dl_list_init(®->pins);
653 dl_list_init(®->nfc_pw_tokens);
655 reg->new_psk_cb = cfg->new_psk_cb;
656 reg->set_ie_cb = cfg->set_ie_cb;
657 reg->pin_needed_cb = cfg->pin_needed_cb;
658 reg->reg_success_cb = cfg->reg_success_cb;
659 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
660 reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
661 reg->cb_ctx = cfg->cb_ctx;
662 reg->skip_cred_build = cfg->skip_cred_build;
663 if (cfg->extra_cred) {
664 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
665 cfg->extra_cred_len);
666 if (reg->extra_cred == NULL) {
671 reg->disable_auto_conf = cfg->disable_auto_conf;
672 reg->sel_reg_dev_password_id_override = -1;
673 reg->sel_reg_config_methods_override = -1;
674 reg->static_wep_only = cfg->static_wep_only;
675 reg->dualband = cfg->dualband;
676 reg->force_per_enrollee_psk = cfg->force_per_enrollee_psk;
678 if (wps_set_ie(reg)) {
679 wps_registrar_deinit(reg);
688 * wps_registrar_deinit - Deinitialize WPS Registrar data
689 * @reg: Registrar data from wps_registrar_init()
691 void wps_registrar_deinit(struct wps_registrar *reg)
695 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
696 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
697 wps_free_pins(®->pins);
698 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, 0);
699 wps_free_pbc_sessions(reg->pbc_sessions);
700 wpabuf_free(reg->extra_cred);
701 wps_free_devices(reg->devices);
706 static void wps_registrar_invalidate_unused(struct wps_registrar *reg)
708 struct wps_uuid_pin *pin;
710 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
711 if (pin->wildcard_uuid == 1 && !(pin->flags & PIN_LOCKED)) {
712 wpa_printf(MSG_DEBUG, "WPS: Invalidate previously "
713 "configured wildcard PIN");
714 wps_registrar_remove_pin(reg, pin);
722 * wps_registrar_add_pin - Configure a new PIN for Registrar
723 * @reg: Registrar data from wps_registrar_init()
724 * @addr: Enrollee MAC address or %NULL if not known
725 * @uuid: UUID-E or %NULL for wildcard (any UUID)
726 * @pin: PIN (Device Password)
727 * @pin_len: Length of pin in octets
728 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
729 * Returns: 0 on success, -1 on failure
731 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
732 const u8 *uuid, const u8 *pin, size_t pin_len,
735 struct wps_uuid_pin *p;
737 p = os_zalloc(sizeof(*p));
741 os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
743 p->wildcard_uuid = 1;
745 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
746 p->pin = os_malloc(pin_len);
747 if (p->pin == NULL) {
751 os_memcpy(p->pin, pin, pin_len);
752 p->pin_len = pin_len;
755 p->flags |= PIN_EXPIRES;
756 os_get_time(&p->expiration);
757 p->expiration.sec += timeout;
760 if (p->wildcard_uuid)
761 wps_registrar_invalidate_unused(reg);
763 dl_list_add(®->pins, &p->list);
765 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
767 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
768 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
769 reg->selected_registrar = 1;
772 wps_registrar_add_authorized_mac(reg, addr);
774 wps_registrar_add_authorized_mac(
775 reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
776 wps_registrar_selected_registrar_changed(reg, 0);
777 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
778 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
779 wps_registrar_set_selected_timeout,
786 static void wps_registrar_remove_pin(struct wps_registrar *reg,
787 struct wps_uuid_pin *pin)
790 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
792 if (is_zero_ether_addr(pin->enrollee_addr))
795 addr = pin->enrollee_addr;
796 wps_registrar_remove_authorized_mac(reg, addr);
798 wps_registrar_selected_registrar_changed(reg, 0);
802 static void wps_registrar_expire_pins(struct wps_registrar *reg)
804 struct wps_uuid_pin *pin, *prev;
808 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
810 if ((pin->flags & PIN_EXPIRES) &&
811 os_time_before(&pin->expiration, &now)) {
812 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
813 pin->uuid, WPS_UUID_LEN);
814 wps_registrar_remove_pin(reg, pin);
821 * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
822 * @reg: Registrar data from wps_registrar_init()
823 * @dev_pw: PIN to search for or %NULL to match any
824 * @dev_pw_len: Length of dev_pw in octets
825 * Returns: 0 on success, -1 if not wildcard PIN is enabled
827 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg,
831 struct wps_uuid_pin *pin, *prev;
833 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
835 if (dev_pw && pin->pin &&
836 (dev_pw_len != pin->pin_len ||
837 os_memcmp(dev_pw, pin->pin, dev_pw_len) != 0))
838 continue; /* different PIN */
839 if (pin->wildcard_uuid) {
840 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
841 pin->uuid, WPS_UUID_LEN);
842 wps_registrar_remove_pin(reg, pin);
852 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
853 * @reg: Registrar data from wps_registrar_init()
855 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
857 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
859 struct wps_uuid_pin *pin, *prev;
861 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
863 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
864 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
865 pin->uuid, WPS_UUID_LEN);
866 wps_registrar_remove_pin(reg, pin);
875 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
876 const u8 *uuid, size_t *pin_len)
878 struct wps_uuid_pin *pin, *found = NULL;
880 wps_registrar_expire_pins(reg);
882 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
883 if (!pin->wildcard_uuid &&
884 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
891 /* Check for wildcard UUIDs since none of the UUID-specific
893 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
894 if (pin->wildcard_uuid == 1 ||
895 pin->wildcard_uuid == 2) {
896 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
897 "PIN. Assigned it for this UUID-E");
898 pin->wildcard_uuid++;
899 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
910 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
911 * that could otherwise avoid PIN invalidations.
913 if (found->flags & PIN_LOCKED) {
914 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
915 "allow concurrent re-use");
918 *pin_len = found->pin_len;
919 found->flags |= PIN_LOCKED;
925 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
926 * @reg: Registrar data from wps_registrar_init()
928 * Returns: 0 on success, -1 on failure
930 * PINs are locked to enforce only one concurrent use. This function unlocks a
931 * PIN to allow it to be used again. If the specified PIN was configured using
932 * a wildcard UUID, it will be removed instead of allowing multiple uses.
934 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
936 struct wps_uuid_pin *pin;
938 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
939 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
940 if (pin->wildcard_uuid == 3) {
941 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
943 return wps_registrar_invalidate_pin(reg, uuid);
945 pin->flags &= ~PIN_LOCKED;
954 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
956 reg->selected_registrar = 0;
958 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
959 wps_registrar_remove_authorized_mac(reg,
960 (u8 *) "\xff\xff\xff\xff\xff\xff");
961 wps_registrar_selected_registrar_changed(reg, 0);
965 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
967 struct wps_registrar *reg = eloop_ctx;
969 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
970 wps_pbc_timeout_event(reg->wps);
971 wps_registrar_stop_pbc(reg);
976 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
977 * @reg: Registrar data from wps_registrar_init()
978 * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
979 * indicates no such filtering
980 * Returns: 0 on success, -1 on failure, -2 on session overlap
982 * This function is called on an AP when a push button is pushed to activate
983 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
984 * or when a PBC registration is completed. If more than one Enrollee in active
985 * PBC mode has been detected during the monitor time (previous 2 minutes), the
986 * PBC mode is not activated and -2 is returned to indicate session overlap.
987 * This is skipped if a specific Enrollee is selected.
989 int wps_registrar_button_pushed(struct wps_registrar *reg,
990 const u8 *p2p_dev_addr)
992 if (p2p_dev_addr == NULL &&
993 wps_registrar_pbc_overlap(reg, NULL, NULL)) {
994 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
996 wps_pbc_overlap_event(reg->wps);
999 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
1000 reg->force_pbc_overlap = 0;
1001 reg->selected_registrar = 1;
1004 os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
1006 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
1007 wps_registrar_add_authorized_mac(reg,
1008 (u8 *) "\xff\xff\xff\xff\xff\xff");
1009 wps_registrar_selected_registrar_changed(reg, 0);
1011 wps_pbc_active_event(reg->wps);
1012 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1013 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1014 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
1020 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
1022 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
1023 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1024 wps_registrar_stop_pbc(reg);
1025 wps_pbc_disable_event(reg->wps);
1029 static void wps_registrar_pin_completed(struct wps_registrar *reg)
1031 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
1032 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1033 reg->selected_registrar = 0;
1034 wps_registrar_selected_registrar_changed(reg, 0);
1038 void wps_registrar_complete(struct wps_registrar *registrar, const u8 *uuid_e,
1039 const u8 *dev_pw, size_t dev_pw_len)
1041 if (registrar->pbc) {
1042 wps_registrar_remove_pbc_session(registrar,
1044 wps_registrar_pbc_completed(registrar);
1045 os_get_time(®istrar->pbc_ignore_start);
1046 os_memcpy(registrar->pbc_ignore_uuid, uuid_e, WPS_UUID_LEN);
1048 wps_registrar_pin_completed(registrar);
1052 wps_registrar_invalidate_wildcard_pin(registrar, dev_pw,
1054 wpa_hexdump_key(MSG_DEBUG, "WPS: Invalidated wildcard PIN",
1055 dev_pw, dev_pw_len);
1060 int wps_registrar_wps_cancel(struct wps_registrar *reg)
1063 wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
1064 wps_registrar_pbc_timeout(reg, NULL);
1065 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1067 } else if (reg->selected_registrar) {
1069 wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
1070 wps_registrar_pin_completed(reg);
1071 wps_registrar_invalidate_wildcard_pin(reg, NULL, 0);
1079 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
1080 * @reg: Registrar data from wps_registrar_init()
1081 * @addr: MAC address of the Probe Request sender
1082 * @wps_data: WPS IE contents
1084 * This function is called on an AP when a Probe Request with WPS IE is
1085 * received. This is used to track PBC mode use and to detect possible overlap
1086 * situation with other WPS APs.
1088 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
1089 const struct wpabuf *wps_data,
1092 struct wps_parse_attr attr;
1095 wpa_hexdump_buf(MSG_MSGDUMP,
1096 "WPS: Probe Request with WPS data received",
1099 if (wps_parse_msg(wps_data, &attr) < 0)
1102 if (attr.config_methods == NULL) {
1103 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
1108 if (attr.dev_password_id == NULL) {
1109 wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
1110 "in Probe Request");
1114 if (reg->enrollee_seen_cb && attr.uuid_e &&
1115 attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
1116 char *dev_name = NULL;
1117 if (attr.dev_name) {
1118 dev_name = os_zalloc(attr.dev_name_len + 1);
1120 os_memcpy(dev_name, attr.dev_name,
1124 reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
1125 attr.primary_dev_type,
1126 WPA_GET_BE16(attr.config_methods),
1127 WPA_GET_BE16(attr.dev_password_id),
1128 *attr.request_type, dev_name);
1132 if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
1133 return; /* Not PBC */
1135 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
1136 MACSTR, MAC2STR(addr));
1137 if (attr.uuid_e == NULL) {
1138 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
1142 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E from Probe Request", attr.uuid_e,
1145 #ifdef WPS_WORKAROUNDS
1146 if (reg->pbc_ignore_start.sec &&
1147 os_memcmp(attr.uuid_e, reg->pbc_ignore_uuid, WPS_UUID_LEN) == 0) {
1148 struct os_time now, dur;
1150 os_time_sub(&now, ®->pbc_ignore_start, &dur);
1151 if (dur.sec >= 0 && dur.sec < 5) {
1152 wpa_printf(MSG_DEBUG, "WPS: Ignore PBC activation "
1153 "based on Probe Request from the Enrollee "
1154 "that just completed PBC provisioning");
1157 reg->pbc_ignore_start.sec = 0;
1159 #endif /* WPS_WORKAROUNDS */
1162 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
1163 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
1164 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
1165 reg->force_pbc_overlap = 1;
1166 wps_pbc_overlap_event(reg->wps);
1171 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
1172 const u8 *p2p_dev_addr, const u8 *psk, size_t psk_len)
1174 if (reg->new_psk_cb == NULL)
1177 return reg->new_psk_cb(reg->cb_ctx, mac_addr, p2p_dev_addr, psk,
1182 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
1183 const struct wps_device_data *dev)
1185 if (reg->pin_needed_cb == NULL)
1188 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
1192 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
1193 const u8 *uuid_e, const u8 *dev_pw,
1196 if (reg->reg_success_cb == NULL)
1199 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e, dev_pw, dev_pw_len);
1203 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
1204 struct wpabuf *probe_resp_ie)
1206 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
1210 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
1213 if (reg->set_sel_reg_cb == NULL)
1216 if (reg->selected_registrar) {
1217 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
1219 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
1220 WPS_CONFIG_PHY_PUSHBUTTON);
1221 #endif /* CONFIG_WPS2 */
1223 wps_set_pushbutton(&methods, reg->wps->config_methods);
1226 wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1227 "config_methods=0x%x pbc=%d methods=0x%x",
1228 reg->selected_registrar, reg->wps->config_methods,
1231 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
1232 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
1237 static int wps_set_ie(struct wps_registrar *reg)
1239 struct wpabuf *beacon;
1240 struct wpabuf *probe;
1241 const u8 *auth_macs;
1243 size_t vendor_len = 0;
1246 if (reg->set_ie_cb == NULL)
1249 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
1250 if (reg->wps->dev.vendor_ext[i]) {
1251 vendor_len += 2 + 2;
1252 vendor_len += wpabuf_len(reg->wps->dev.vendor_ext[i]);
1256 beacon = wpabuf_alloc(400 + vendor_len);
1259 probe = wpabuf_alloc(500 + vendor_len);
1260 if (probe == NULL) {
1261 wpabuf_free(beacon);
1265 auth_macs = wps_authorized_macs(reg, &count);
1267 wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
1269 if (wps_build_version(beacon) ||
1270 wps_build_wps_state(reg->wps, beacon) ||
1271 wps_build_ap_setup_locked(reg->wps, beacon) ||
1272 wps_build_selected_registrar(reg, beacon) ||
1273 wps_build_sel_reg_dev_password_id(reg, beacon) ||
1274 wps_build_sel_reg_config_methods(reg, beacon) ||
1275 wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
1276 (reg->dualband && wps_build_rf_bands(®->wps->dev, beacon, 0)) ||
1277 wps_build_wfa_ext(beacon, 0, auth_macs, count) ||
1278 wps_build_vendor_ext(®->wps->dev, beacon)) {
1279 wpabuf_free(beacon);
1285 if (wps_build_dev_name(®->wps->dev, beacon) ||
1286 wps_build_primary_dev_type(®->wps->dev, beacon)) {
1287 wpabuf_free(beacon);
1291 #endif /* CONFIG_P2P */
1293 wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
1295 if (wps_build_version(probe) ||
1296 wps_build_wps_state(reg->wps, probe) ||
1297 wps_build_ap_setup_locked(reg->wps, probe) ||
1298 wps_build_selected_registrar(reg, probe) ||
1299 wps_build_sel_reg_dev_password_id(reg, probe) ||
1300 wps_build_sel_reg_config_methods(reg, probe) ||
1301 wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
1302 WPS_RESP_REGISTRAR) ||
1303 wps_build_uuid_e(probe, reg->wps->uuid) ||
1304 wps_build_device_attrs(®->wps->dev, probe) ||
1305 wps_build_probe_config_methods(reg, probe) ||
1306 (reg->dualband && wps_build_rf_bands(®->wps->dev, probe, 0)) ||
1307 wps_build_wfa_ext(probe, 0, auth_macs, count) ||
1308 wps_build_vendor_ext(®->wps->dev, probe)) {
1309 wpabuf_free(beacon);
1314 beacon = wps_ie_encapsulate(beacon);
1315 probe = wps_ie_encapsulate(probe);
1317 if (!beacon || !probe) {
1318 wpabuf_free(beacon);
1323 if (reg->static_wep_only) {
1325 * Windows XP and Vista clients can get confused about
1326 * EAP-Identity/Request when they probe the network with
1327 * EAPOL-Start. In such a case, they may assume the network is
1328 * using IEEE 802.1X and prompt user for a certificate while
1329 * the correct (non-WPS) behavior would be to ask for the
1330 * static WEP key. As a workaround, use Microsoft Provisioning
1331 * IE to advertise that legacy 802.1X is not supported.
1333 const u8 ms_wps[7] = {
1334 WLAN_EID_VENDOR_SPECIFIC, 5,
1335 /* Microsoft Provisioning IE (00:50:f2:5) */
1336 0x00, 0x50, 0xf2, 5,
1337 0x00 /* no legacy 802.1X or MS WPS */
1339 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
1340 "into Beacon/Probe Response frames");
1341 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
1342 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
1345 return wps_cb_set_ie(reg, beacon, probe);
1349 static int wps_get_dev_password(struct wps_data *wps)
1354 os_free(wps->dev_password);
1355 wps->dev_password = NULL;
1358 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
1359 pin = (const u8 *) "00000000";
1361 #ifdef CONFIG_WPS_NFC
1362 } else if (wps->nfc_pw_token) {
1363 wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from NFC "
1365 pin = wps->nfc_pw_token->dev_pw;
1366 pin_len = wps->nfc_pw_token->dev_pw_len;
1367 #endif /* CONFIG_WPS_NFC */
1369 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1371 if (pin && wps->dev_pw_id >= 0x10) {
1372 wpa_printf(MSG_DEBUG, "WPS: No match for OOB Device "
1373 "Password ID, but PIN found");
1375 * See whether Enrollee is willing to use PIN instead.
1377 wps->dev_pw_id = DEV_PW_DEFAULT;
1381 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1383 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1388 wps->dev_password = os_malloc(pin_len);
1389 if (wps->dev_password == NULL)
1391 os_memcpy(wps->dev_password, pin, pin_len);
1392 wps->dev_password_len = pin_len;
1398 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1400 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1401 wpabuf_put_be16(msg, ATTR_UUID_R);
1402 wpabuf_put_be16(msg, WPS_UUID_LEN);
1403 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1408 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1414 if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1416 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1417 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1418 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1420 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1421 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1422 "R-Hash derivation");
1426 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1427 wpabuf_put_be16(msg, ATTR_R_HASH1);
1428 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1429 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1430 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1431 addr[0] = wps->snonce;
1432 len[0] = WPS_SECRET_NONCE_LEN;
1433 addr[1] = wps->psk1;
1434 len[1] = WPS_PSK_LEN;
1435 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1436 len[2] = wpabuf_len(wps->dh_pubkey_e);
1437 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1438 len[3] = wpabuf_len(wps->dh_pubkey_r);
1439 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1440 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1442 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1443 wpabuf_put_be16(msg, ATTR_R_HASH2);
1444 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1445 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1446 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1447 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1448 addr[1] = wps->psk2;
1449 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1450 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1456 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1458 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1459 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1460 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1461 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1466 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1468 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1469 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1470 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1471 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1472 WPS_SECRET_NONCE_LEN);
1477 static int wps_build_cred_network_idx(struct wpabuf *msg,
1478 const struct wps_credential *cred)
1480 wpa_printf(MSG_DEBUG, "WPS: * Network Index (1)");
1481 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1482 wpabuf_put_be16(msg, 1);
1483 wpabuf_put_u8(msg, 1);
1488 static int wps_build_cred_ssid(struct wpabuf *msg,
1489 const struct wps_credential *cred)
1491 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1492 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID for Credential",
1493 cred->ssid, cred->ssid_len);
1494 wpabuf_put_be16(msg, ATTR_SSID);
1495 wpabuf_put_be16(msg, cred->ssid_len);
1496 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1501 static int wps_build_cred_auth_type(struct wpabuf *msg,
1502 const struct wps_credential *cred)
1504 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1506 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1507 wpabuf_put_be16(msg, 2);
1508 wpabuf_put_be16(msg, cred->auth_type);
1513 static int wps_build_cred_encr_type(struct wpabuf *msg,
1514 const struct wps_credential *cred)
1516 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1518 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1519 wpabuf_put_be16(msg, 2);
1520 wpabuf_put_be16(msg, cred->encr_type);
1525 static int wps_build_cred_network_key(struct wpabuf *msg,
1526 const struct wps_credential *cred)
1528 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1529 (int) cred->key_len);
1530 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
1531 cred->key, cred->key_len);
1532 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1533 wpabuf_put_be16(msg, cred->key_len);
1534 wpabuf_put_data(msg, cred->key, cred->key_len);
1539 static int wps_build_credential(struct wpabuf *msg,
1540 const struct wps_credential *cred)
1542 if (wps_build_cred_network_idx(msg, cred) ||
1543 wps_build_cred_ssid(msg, cred) ||
1544 wps_build_cred_auth_type(msg, cred) ||
1545 wps_build_cred_encr_type(msg, cred) ||
1546 wps_build_cred_network_key(msg, cred) ||
1547 wps_build_mac_addr(msg, cred->mac_addr))
1553 int wps_build_credential_wrap(struct wpabuf *msg,
1554 const struct wps_credential *cred)
1556 struct wpabuf *wbuf;
1557 wbuf = wpabuf_alloc(200);
1560 if (wps_build_credential(wbuf, cred)) {
1564 wpabuf_put_be16(msg, ATTR_CRED);
1565 wpabuf_put_be16(msg, wpabuf_len(wbuf));
1566 wpabuf_put_buf(msg, wbuf);
1572 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1574 struct wpabuf *cred;
1576 if (wps->wps->registrar->skip_cred_build)
1577 goto skip_cred_build;
1579 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1580 if (wps->use_cred) {
1581 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1584 os_memset(&wps->cred, 0, sizeof(wps->cred));
1586 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1587 wps->cred.ssid_len = wps->wps->ssid_len;
1589 /* Select the best authentication and encryption type */
1590 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1591 wps->auth_type = WPS_AUTH_WPA2PSK;
1592 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1593 wps->auth_type = WPS_AUTH_WPAPSK;
1594 else if (wps->auth_type & WPS_AUTH_OPEN)
1595 wps->auth_type = WPS_AUTH_OPEN;
1596 else if (wps->auth_type & WPS_AUTH_SHARED)
1597 wps->auth_type = WPS_AUTH_SHARED;
1599 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1603 wps->cred.auth_type = wps->auth_type;
1605 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1606 wps->auth_type == WPS_AUTH_WPAPSK) {
1607 if (wps->encr_type & WPS_ENCR_AES)
1608 wps->encr_type = WPS_ENCR_AES;
1609 else if (wps->encr_type & WPS_ENCR_TKIP)
1610 wps->encr_type = WPS_ENCR_TKIP;
1612 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1613 "type for WPA/WPA2");
1617 if (wps->encr_type & WPS_ENCR_WEP)
1618 wps->encr_type = WPS_ENCR_WEP;
1619 else if (wps->encr_type & WPS_ENCR_NONE)
1620 wps->encr_type = WPS_ENCR_NONE;
1622 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1623 "type for non-WPA/WPA2 mode");
1627 wps->cred.encr_type = wps->encr_type;
1629 * Set MAC address in the Credential to be the Enrollee's MAC address
1631 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1633 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1634 !wps->wps->registrar->disable_auto_conf) {
1636 /* Generate a random passphrase */
1637 if (random_get_bytes(r, sizeof(r)) < 0)
1639 os_free(wps->new_psk);
1640 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1641 if (wps->new_psk == NULL)
1643 wps->new_psk_len--; /* remove newline */
1644 while (wps->new_psk_len &&
1645 wps->new_psk[wps->new_psk_len - 1] == '=')
1647 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1648 wps->new_psk, wps->new_psk_len);
1649 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1650 wps->cred.key_len = wps->new_psk_len;
1651 } else if (!wps->wps->registrar->force_per_enrollee_psk &&
1652 wps->use_psk_key && wps->wps->psk_set) {
1654 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1655 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1656 os_memcpy(wps->cred.key, hex, 32 * 2);
1657 wps->cred.key_len = 32 * 2;
1658 } else if (!wps->wps->registrar->force_per_enrollee_psk &&
1659 wps->wps->network_key) {
1660 os_memcpy(wps->cred.key, wps->wps->network_key,
1661 wps->wps->network_key_len);
1662 wps->cred.key_len = wps->wps->network_key_len;
1663 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1665 /* Generate a random per-device PSK */
1666 os_free(wps->new_psk);
1667 wps->new_psk_len = 32;
1668 wps->new_psk = os_malloc(wps->new_psk_len);
1669 if (wps->new_psk == NULL)
1671 if (random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
1672 os_free(wps->new_psk);
1673 wps->new_psk = NULL;
1676 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1677 wps->new_psk, wps->new_psk_len);
1678 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1680 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1681 wps->cred.key_len = wps->new_psk_len * 2;
1685 #ifdef CONFIG_WPS_TESTING
1686 if (wps_testing_dummy_cred)
1687 cred = wpabuf_alloc(200);
1691 struct wps_credential dummy;
1692 wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
1693 os_memset(&dummy, 0, sizeof(dummy));
1694 os_memcpy(dummy.ssid, "dummy", 5);
1696 dummy.auth_type = WPS_AUTH_WPA2PSK;
1697 dummy.encr_type = WPS_ENCR_AES;
1698 os_memcpy(dummy.key, "dummy psk", 9);
1700 os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
1701 wps_build_credential(cred, &dummy);
1702 wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
1704 wpabuf_put_be16(msg, ATTR_CRED);
1705 wpabuf_put_be16(msg, wpabuf_len(cred));
1706 wpabuf_put_buf(msg, cred);
1710 #endif /* CONFIG_WPS_TESTING */
1712 cred = wpabuf_alloc(200);
1716 if (wps_build_credential(cred, &wps->cred)) {
1721 wpabuf_put_be16(msg, ATTR_CRED);
1722 wpabuf_put_be16(msg, wpabuf_len(cred));
1723 wpabuf_put_buf(msg, cred);
1727 if (wps->wps->registrar->extra_cred) {
1728 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1729 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1736 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1738 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1740 if (wps_build_credential(msg, &wps->cred))
1747 static struct wpabuf * wps_build_ap_cred(struct wps_data *wps)
1749 struct wpabuf *msg, *plain;
1751 msg = wpabuf_alloc(1000);
1755 plain = wpabuf_alloc(200);
1756 if (plain == NULL) {
1761 if (wps_build_ap_settings(wps, plain)) {
1767 wpabuf_put_be16(msg, ATTR_CRED);
1768 wpabuf_put_be16(msg, wpabuf_len(plain));
1769 wpabuf_put_buf(msg, plain);
1776 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1780 if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
1782 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1783 wps->nonce_r, WPS_NONCE_LEN);
1784 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1786 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1787 msg = wpabuf_alloc(1000);
1791 if (wps_build_version(msg) ||
1792 wps_build_msg_type(msg, WPS_M2) ||
1793 wps_build_enrollee_nonce(wps, msg) ||
1794 wps_build_registrar_nonce(wps, msg) ||
1795 wps_build_uuid_r(wps, msg) ||
1796 wps_build_public_key(wps, msg) ||
1797 wps_derive_keys(wps) ||
1798 wps_build_auth_type_flags(wps, msg) ||
1799 wps_build_encr_type_flags(wps, msg) ||
1800 wps_build_conn_type_flags(wps, msg) ||
1801 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1802 wps_build_device_attrs(&wps->wps->dev, msg) ||
1803 wps_build_rf_bands(&wps->wps->dev, msg,
1804 wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
1805 wps_build_assoc_state(wps, msg) ||
1806 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1807 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1808 wps_build_os_version(&wps->wps->dev, msg) ||
1809 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1810 wps_build_authenticator(wps, msg)) {
1816 wps->state = RECV_M3;
1821 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1824 u16 err = wps->config_error;
1826 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1827 msg = wpabuf_alloc(1000);
1831 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1832 err == WPS_CFG_NO_ERROR)
1833 err = WPS_CFG_SETUP_LOCKED;
1835 if (wps_build_version(msg) ||
1836 wps_build_msg_type(msg, WPS_M2D) ||
1837 wps_build_enrollee_nonce(wps, msg) ||
1838 wps_build_registrar_nonce(wps, msg) ||
1839 wps_build_uuid_r(wps, msg) ||
1840 wps_build_auth_type_flags(wps, msg) ||
1841 wps_build_encr_type_flags(wps, msg) ||
1842 wps_build_conn_type_flags(wps, msg) ||
1843 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1844 wps_build_device_attrs(&wps->wps->dev, msg) ||
1845 wps_build_rf_bands(&wps->wps->dev, msg,
1846 wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
1847 wps_build_assoc_state(wps, msg) ||
1848 wps_build_config_error(msg, err) ||
1849 wps_build_os_version(&wps->wps->dev, msg) ||
1850 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1855 wps->state = RECV_M2D_ACK;
1860 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1862 struct wpabuf *msg, *plain;
1864 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1866 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1868 plain = wpabuf_alloc(200);
1872 msg = wpabuf_alloc(1000);
1878 if (wps_build_version(msg) ||
1879 wps_build_msg_type(msg, WPS_M4) ||
1880 wps_build_enrollee_nonce(wps, msg) ||
1881 wps_build_r_hash(wps, msg) ||
1882 wps_build_r_snonce1(wps, plain) ||
1883 wps_build_key_wrap_auth(wps, plain) ||
1884 wps_build_encr_settings(wps, msg, plain) ||
1885 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1886 wps_build_authenticator(wps, msg)) {
1893 wps->state = RECV_M5;
1898 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1900 struct wpabuf *msg, *plain;
1902 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1904 plain = wpabuf_alloc(200);
1908 msg = wpabuf_alloc(1000);
1914 if (wps_build_version(msg) ||
1915 wps_build_msg_type(msg, WPS_M6) ||
1916 wps_build_enrollee_nonce(wps, msg) ||
1917 wps_build_r_snonce2(wps, plain) ||
1918 wps_build_key_wrap_auth(wps, plain) ||
1919 wps_build_encr_settings(wps, msg, plain) ||
1920 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1921 wps_build_authenticator(wps, msg)) {
1928 wps->wps_pin_revealed = 1;
1929 wps->state = RECV_M7;
1934 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1936 struct wpabuf *msg, *plain;
1938 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1940 plain = wpabuf_alloc(500);
1944 msg = wpabuf_alloc(1000);
1950 if (wps_build_version(msg) ||
1951 wps_build_msg_type(msg, WPS_M8) ||
1952 wps_build_enrollee_nonce(wps, msg) ||
1953 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1954 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1955 wps_build_key_wrap_auth(wps, plain) ||
1956 wps_build_encr_settings(wps, msg, plain) ||
1957 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1958 wps_build_authenticator(wps, msg)) {
1965 wps->state = RECV_DONE;
1970 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1971 enum wsc_op_code *op_code)
1975 #ifdef CONFIG_WPS_UPNP
1976 if (!wps->int_reg && wps->wps->wps_upnp) {
1977 struct upnp_pending_message *p, *prev = NULL;
1978 if (wps->ext_reg > 1)
1979 wps_registrar_free_pending_m2(wps->wps);
1980 p = wps->wps->upnp_msgs;
1981 /* TODO: check pending message MAC address */
1982 while (p && p->next) {
1987 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1992 wps->wps->upnp_msgs = NULL;
1999 *op_code = WSC_NACK;
2006 if (wps->ext_reg == 0)
2012 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
2013 "pending message available");
2016 #endif /* CONFIG_WPS_UPNP */
2018 switch (wps->state) {
2020 if (wps_get_dev_password(wps) < 0)
2021 msg = wps_build_m2d(wps);
2023 msg = wps_build_m2(wps);
2027 msg = wps_build_m2d(wps);
2031 msg = wps_build_m4(wps);
2035 msg = wps_build_m6(wps);
2039 msg = wps_build_m8(wps);
2043 msg = wps_build_wsc_ack(wps);
2047 msg = wps_build_wsc_nack(wps);
2048 *op_code = WSC_NACK;
2051 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
2052 "a message", wps->state);
2057 if (*op_code == WSC_MSG && msg) {
2058 /* Save a copy of the last message for Authenticator derivation
2060 wpabuf_free(wps->last_msg);
2061 wps->last_msg = wpabuf_dup(msg);
2068 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
2070 if (e_nonce == NULL) {
2071 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
2075 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
2076 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
2077 wps->nonce_e, WPS_NONCE_LEN);
2083 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
2085 if (r_nonce == NULL) {
2086 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
2090 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
2091 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
2099 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
2101 if (uuid_e == NULL) {
2102 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
2106 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
2107 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
2113 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
2115 if (pw_id == NULL) {
2116 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
2120 wps->dev_pw_id = WPA_GET_BE16(pw_id);
2121 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
2127 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
2129 if (e_hash1 == NULL) {
2130 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
2134 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
2135 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
2141 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
2143 if (e_hash2 == NULL) {
2144 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
2148 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
2149 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
2155 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
2157 u8 hash[SHA256_MAC_LEN];
2161 if (e_snonce1 == NULL) {
2162 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
2166 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
2167 WPS_SECRET_NONCE_LEN);
2169 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
2170 addr[0] = e_snonce1;
2171 len[0] = WPS_SECRET_NONCE_LEN;
2172 addr[1] = wps->psk1;
2173 len[1] = WPS_PSK_LEN;
2174 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2175 len[2] = wpabuf_len(wps->dh_pubkey_e);
2176 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2177 len[3] = wpabuf_len(wps->dh_pubkey_r);
2178 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2180 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
2181 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
2182 "not match with the pre-committed value");
2183 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2184 wps_pwd_auth_fail_event(wps->wps, 0, 1, wps->mac_addr_e);
2188 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
2189 "half of the device password");
2195 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
2197 u8 hash[SHA256_MAC_LEN];
2201 if (e_snonce2 == NULL) {
2202 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
2206 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
2207 WPS_SECRET_NONCE_LEN);
2209 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
2210 addr[0] = e_snonce2;
2211 len[0] = WPS_SECRET_NONCE_LEN;
2212 addr[1] = wps->psk2;
2213 len[1] = WPS_PSK_LEN;
2214 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2215 len[2] = wpabuf_len(wps->dh_pubkey_e);
2216 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2217 len[3] = wpabuf_len(wps->dh_pubkey_r);
2218 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2220 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
2221 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
2222 "not match with the pre-committed value");
2223 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2224 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2225 wps_pwd_auth_fail_event(wps->wps, 0, 2, wps->mac_addr_e);
2229 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
2230 "half of the device password");
2231 wps->wps_pin_revealed = 0;
2232 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
2235 * In case wildcard PIN is used and WPS handshake succeeds in the first
2236 * attempt, wps_registrar_unlock_pin() would not free the PIN, so make
2237 * sure the PIN gets invalidated here.
2239 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2245 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
2247 if (mac_addr == NULL) {
2248 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
2252 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
2254 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
2255 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
2261 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
2264 if (pk == NULL || pk_len == 0) {
2265 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
2269 wpabuf_free(wps->dh_pubkey_e);
2270 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
2271 if (wps->dh_pubkey_e == NULL)
2278 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
2283 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
2288 auth_types = WPA_GET_BE16(auth);
2290 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
2292 wps->auth_type = wps->wps->auth_types & auth_types;
2293 if (wps->auth_type == 0) {
2294 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2295 "authentication types (own 0x%x Enrollee 0x%x)",
2296 wps->wps->auth_types, auth_types);
2297 #ifdef WPS_WORKAROUNDS
2299 * Some deployed implementations seem to advertise incorrect
2300 * information in this attribute. For example, Linksys WRT350N
2301 * seems to have a byteorder bug that breaks this negotiation.
2302 * In order to interoperate with existing implementations,
2303 * assume that the Enrollee supports everything we do.
2305 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2306 "does not advertise supported authentication types "
2308 wps->auth_type = wps->wps->auth_types;
2309 #else /* WPS_WORKAROUNDS */
2311 #endif /* WPS_WORKAROUNDS */
2318 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
2323 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
2328 encr_types = WPA_GET_BE16(encr);
2330 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
2332 wps->encr_type = wps->wps->encr_types & encr_types;
2333 if (wps->encr_type == 0) {
2334 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2335 "encryption types (own 0x%x Enrollee 0x%x)",
2336 wps->wps->encr_types, encr_types);
2337 #ifdef WPS_WORKAROUNDS
2339 * Some deployed implementations seem to advertise incorrect
2340 * information in this attribute. For example, Linksys WRT350N
2341 * seems to have a byteorder bug that breaks this negotiation.
2342 * In order to interoperate with existing implementations,
2343 * assume that the Enrollee supports everything we do.
2345 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2346 "does not advertise supported encryption types "
2348 wps->encr_type = wps->wps->encr_types;
2349 #else /* WPS_WORKAROUNDS */
2351 #endif /* WPS_WORKAROUNDS */
2358 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
2361 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
2366 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
2373 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
2377 if (methods == NULL) {
2378 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
2382 m = WPA_GET_BE16(methods);
2384 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
2385 "%s%s%s%s%s%s%s%s%s", m,
2386 m & WPS_CONFIG_USBA ? " [USBA]" : "",
2387 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
2388 m & WPS_CONFIG_LABEL ? " [Label]" : "",
2389 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
2390 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
2391 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
2392 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
2393 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
2394 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
2396 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
2398 * The Enrollee does not have a display so it is unlikely to be
2399 * able to show the passphrase to a user and as such, could
2400 * benefit from receiving PSK to reduce key derivation time.
2402 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2403 "Enrollee not supporting display");
2404 wps->use_psk_key = 1;
2411 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2413 if (state == NULL) {
2414 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2419 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2426 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2430 if (assoc == NULL) {
2431 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2435 a = WPA_GET_BE16(assoc);
2436 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2442 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2447 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2451 e = WPA_GET_BE16(err);
2452 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2458 static int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
2461 struct wps_registrar *reg = wps->wps->registrar;
2463 if (is_zero_ether_addr(reg->p2p_dev_addr))
2464 return 1; /* no filtering in use */
2466 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
2467 wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
2468 "filtering for PBC: expected " MACSTR " was "
2469 MACSTR " - indicate PBC session overlap",
2470 MAC2STR(reg->p2p_dev_addr),
2471 MAC2STR(wps->p2p_dev_addr));
2474 #endif /* CONFIG_P2P */
2479 static int wps_registrar_skip_overlap(struct wps_data *wps)
2482 struct wps_registrar *reg = wps->wps->registrar;
2484 if (is_zero_ether_addr(reg->p2p_dev_addr))
2485 return 0; /* no specific Enrollee selected */
2487 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) == 0) {
2488 wpa_printf(MSG_DEBUG, "WPS: Skip PBC overlap due to selected "
2492 #endif /* CONFIG_P2P */
2497 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2498 struct wps_parse_attr *attr)
2500 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2502 if (wps->state != RECV_M1) {
2503 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2504 "receiving M1", wps->state);
2508 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2509 wps_process_mac_addr(wps, attr->mac_addr) ||
2510 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2511 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2512 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2513 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2514 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2515 wps_process_config_methods(wps, attr->config_methods) ||
2516 wps_process_wps_state(wps, attr->wps_state) ||
2517 wps_process_device_attrs(&wps->peer_dev, attr) ||
2518 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2519 wps_process_assoc_state(wps, attr->assoc_state) ||
2520 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2521 wps_process_config_error(wps, attr->config_error) ||
2522 wps_process_os_version(&wps->peer_dev, attr->os_version))
2525 if (wps->dev_pw_id < 0x10 &&
2526 wps->dev_pw_id != DEV_PW_DEFAULT &&
2527 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2528 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2529 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2530 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2531 !wps->wps->registrar->pbc)) {
2532 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2534 wps->state = SEND_M2D;
2535 return WPS_CONTINUE;
2538 #ifdef CONFIG_WPS_NFC
2539 if (wps->dev_pw_id >= 0x10) {
2540 struct wps_nfc_pw_token *token;
2542 u8 hash[WPS_HASH_LEN];
2544 token = wps_get_nfc_pw_token(
2545 &wps->wps->registrar->nfc_pw_tokens, wps->dev_pw_id);
2547 wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
2549 dl_list_del(&token->list);
2550 wps->nfc_pw_token = token;
2552 addr[0] = attr->public_key;
2553 sha256_vector(1, addr, &attr->public_key_len, hash);
2554 if (os_memcmp(hash, wps->nfc_pw_token->pubkey_hash,
2555 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
2556 wpa_printf(MSG_ERROR, "WPS: Public Key hash "
2562 #endif /* CONFIG_WPS_NFC */
2564 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2565 if ((wps->wps->registrar->force_pbc_overlap ||
2566 wps_registrar_pbc_overlap(wps->wps->registrar,
2567 wps->mac_addr_e, wps->uuid_e) ||
2568 !wps_registrar_p2p_dev_addr_match(wps)) &&
2569 !wps_registrar_skip_overlap(wps)) {
2570 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2572 wps->state = SEND_M2D;
2573 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2574 wps_pbc_overlap_event(wps->wps);
2575 wps_fail_event(wps->wps, WPS_M1,
2576 WPS_CFG_MULTIPLE_PBC_DETECTED,
2577 WPS_EI_NO_ERROR, wps->mac_addr_e);
2578 wps->wps->registrar->force_pbc_overlap = 1;
2579 return WPS_CONTINUE;
2581 wps_registrar_add_pbc_session(wps->wps->registrar,
2582 wps->mac_addr_e, wps->uuid_e);
2586 #ifdef WPS_WORKAROUNDS
2588 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2589 * passphrase format. To avoid interop issues, force PSK format to be
2592 if (!wps->use_psk_key &&
2593 wps->peer_dev.manufacturer &&
2594 os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
2595 wps->peer_dev.model_name &&
2596 os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
2597 wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
2599 wps->use_psk_key = 1;
2601 #endif /* WPS_WORKAROUNDS */
2603 wps->state = SEND_M2;
2604 return WPS_CONTINUE;
2608 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2609 const struct wpabuf *msg,
2610 struct wps_parse_attr *attr)
2612 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2614 if (wps->state != RECV_M3) {
2615 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2616 "receiving M3", wps->state);
2617 wps->state = SEND_WSC_NACK;
2618 return WPS_CONTINUE;
2621 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2622 !wps_registrar_skip_overlap(wps)) {
2623 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2625 wps->state = SEND_WSC_NACK;
2626 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2627 return WPS_CONTINUE;
2630 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2631 wps_process_authenticator(wps, attr->authenticator, msg) ||
2632 wps_process_e_hash1(wps, attr->e_hash1) ||
2633 wps_process_e_hash2(wps, attr->e_hash2)) {
2634 wps->state = SEND_WSC_NACK;
2635 return WPS_CONTINUE;
2638 wps->state = SEND_M4;
2639 return WPS_CONTINUE;
2643 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2644 const struct wpabuf *msg,
2645 struct wps_parse_attr *attr)
2647 struct wpabuf *decrypted;
2648 struct wps_parse_attr eattr;
2650 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2652 if (wps->state != RECV_M5) {
2653 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2654 "receiving M5", wps->state);
2655 wps->state = SEND_WSC_NACK;
2656 return WPS_CONTINUE;
2659 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2660 !wps_registrar_skip_overlap(wps)) {
2661 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2663 wps->state = SEND_WSC_NACK;
2664 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2665 return WPS_CONTINUE;
2668 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2669 wps_process_authenticator(wps, attr->authenticator, msg)) {
2670 wps->state = SEND_WSC_NACK;
2671 return WPS_CONTINUE;
2674 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2675 attr->encr_settings_len);
2676 if (decrypted == NULL) {
2677 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2678 "Settings attribute");
2679 wps->state = SEND_WSC_NACK;
2680 return WPS_CONTINUE;
2683 if (wps_validate_m5_encr(decrypted, attr->version2 != NULL) < 0) {
2684 wpabuf_free(decrypted);
2685 wps->state = SEND_WSC_NACK;
2686 return WPS_CONTINUE;
2689 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2691 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2692 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2693 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2694 wpabuf_free(decrypted);
2695 wps->state = SEND_WSC_NACK;
2696 return WPS_CONTINUE;
2698 wpabuf_free(decrypted);
2700 wps->state = SEND_M6;
2701 return WPS_CONTINUE;
2705 static void wps_sta_cred_cb(struct wps_data *wps)
2708 * Update credential to only include a single authentication and
2709 * encryption type in case the AP configuration includes more than one
2712 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2713 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2714 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2715 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2716 if (wps->cred.encr_type & WPS_ENCR_AES)
2717 wps->cred.encr_type = WPS_ENCR_AES;
2718 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2719 wps->cred.encr_type = WPS_ENCR_TKIP;
2720 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2721 "AP configuration");
2722 if (wps->wps->cred_cb)
2723 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2727 static void wps_cred_update(struct wps_credential *dst,
2728 struct wps_credential *src)
2730 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2731 dst->ssid_len = src->ssid_len;
2732 dst->auth_type = src->auth_type;
2733 dst->encr_type = src->encr_type;
2734 dst->key_idx = src->key_idx;
2735 os_memcpy(dst->key, src->key, sizeof(dst->key));
2736 dst->key_len = src->key_len;
2740 static int wps_process_ap_settings_r(struct wps_data *wps,
2741 struct wps_parse_attr *attr)
2745 if (wps->wps->ap || wps->er)
2748 /* AP Settings Attributes in M7 when Enrollee is an AP */
2749 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2752 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2754 if (wps->new_ap_settings) {
2755 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2757 wps_cred_update(&wps->cred, wps->new_ap_settings);
2761 * Use the AP PIN only to receive the current AP settings, not
2762 * to reconfigure the AP.
2766 * Clear selected registrar here since we do not get to
2767 * WSC_Done in this protocol run.
2769 wps_registrar_pin_completed(wps->wps->registrar);
2771 msg = wps_build_ap_cred(wps);
2774 wps->cred.cred_attr = wpabuf_head(msg);
2775 wps->cred.cred_attr_len = wpabuf_len(msg);
2777 if (wps->ap_settings_cb) {
2778 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2783 wps_sta_cred_cb(wps);
2785 wps->cred.cred_attr = NULL;
2786 wps->cred.cred_attr_len = 0;
2794 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2795 const struct wpabuf *msg,
2796 struct wps_parse_attr *attr)
2798 struct wpabuf *decrypted;
2799 struct wps_parse_attr eattr;
2801 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2803 if (wps->state != RECV_M7) {
2804 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2805 "receiving M7", wps->state);
2806 wps->state = SEND_WSC_NACK;
2807 return WPS_CONTINUE;
2810 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2811 !wps_registrar_skip_overlap(wps)) {
2812 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2814 wps->state = SEND_WSC_NACK;
2815 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2816 return WPS_CONTINUE;
2819 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2820 wps_process_authenticator(wps, attr->authenticator, msg)) {
2821 wps->state = SEND_WSC_NACK;
2822 return WPS_CONTINUE;
2825 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2826 attr->encr_settings_len);
2827 if (decrypted == NULL) {
2828 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2829 "Settings attribute");
2830 wps->state = SEND_WSC_NACK;
2831 return WPS_CONTINUE;
2834 if (wps_validate_m7_encr(decrypted, wps->wps->ap || wps->er,
2835 attr->version2 != NULL) < 0) {
2836 wpabuf_free(decrypted);
2837 wps->state = SEND_WSC_NACK;
2838 return WPS_CONTINUE;
2841 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2843 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2844 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2845 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2846 wps_process_ap_settings_r(wps, &eattr)) {
2847 wpabuf_free(decrypted);
2848 wps->state = SEND_WSC_NACK;
2849 return WPS_CONTINUE;
2852 wpabuf_free(decrypted);
2854 wps->state = SEND_M8;
2855 return WPS_CONTINUE;
2859 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2860 const struct wpabuf *msg)
2862 struct wps_parse_attr attr;
2863 enum wps_process_res ret = WPS_CONTINUE;
2865 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2867 if (wps_parse_msg(msg, &attr) < 0)
2870 if (attr.msg_type == NULL) {
2871 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2872 wps->state = SEND_WSC_NACK;
2873 return WPS_CONTINUE;
2876 if (*attr.msg_type != WPS_M1 &&
2877 (attr.registrar_nonce == NULL ||
2878 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2879 WPS_NONCE_LEN) != 0)) {
2880 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2884 switch (*attr.msg_type) {
2886 if (wps_validate_m1(msg) < 0)
2888 #ifdef CONFIG_WPS_UPNP
2889 if (wps->wps->wps_upnp && attr.mac_addr) {
2890 /* Remove old pending messages when starting new run */
2891 wps_free_pending_msgs(wps->wps->upnp_msgs);
2892 wps->wps->upnp_msgs = NULL;
2894 upnp_wps_device_send_wlan_event(
2895 wps->wps->wps_upnp, attr.mac_addr,
2896 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2898 #endif /* CONFIG_WPS_UPNP */
2899 ret = wps_process_m1(wps, &attr);
2902 if (wps_validate_m3(msg) < 0)
2904 ret = wps_process_m3(wps, msg, &attr);
2905 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2906 wps_fail_event(wps->wps, WPS_M3, wps->config_error,
2907 wps->error_indication, wps->mac_addr_e);
2910 if (wps_validate_m5(msg) < 0)
2912 ret = wps_process_m5(wps, msg, &attr);
2913 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2914 wps_fail_event(wps->wps, WPS_M5, wps->config_error,
2915 wps->error_indication, wps->mac_addr_e);
2918 if (wps_validate_m7(msg) < 0)
2920 ret = wps_process_m7(wps, msg, &attr);
2921 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2922 wps_fail_event(wps->wps, WPS_M7, wps->config_error,
2923 wps->error_indication, wps->mac_addr_e);
2926 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2931 if (ret == WPS_CONTINUE) {
2932 /* Save a copy of the last message for Authenticator derivation
2934 wpabuf_free(wps->last_msg);
2935 wps->last_msg = wpabuf_dup(msg);
2942 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2943 const struct wpabuf *msg)
2945 struct wps_parse_attr attr;
2947 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2949 if (wps_parse_msg(msg, &attr) < 0)
2952 if (attr.msg_type == NULL) {
2953 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2957 if (*attr.msg_type != WPS_WSC_ACK) {
2958 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2963 #ifdef CONFIG_WPS_UPNP
2964 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2965 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2966 if (wps->wps->upnp_msgs)
2967 return WPS_CONTINUE;
2968 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2969 "external Registrar");
2972 #endif /* CONFIG_WPS_UPNP */
2974 if (attr.registrar_nonce == NULL ||
2975 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
2977 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2981 if (attr.enrollee_nonce == NULL ||
2982 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
2983 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2987 if (wps->state == RECV_M2D_ACK) {
2988 #ifdef CONFIG_WPS_UPNP
2989 if (wps->wps->wps_upnp &&
2990 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2991 if (wps->wps->upnp_msgs)
2992 return WPS_CONTINUE;
2993 if (wps->ext_reg == 0)
2995 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2996 "external Registrar");
2999 #endif /* CONFIG_WPS_UPNP */
3001 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
3002 "terminate negotiation");
3009 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
3010 const struct wpabuf *msg)
3012 struct wps_parse_attr attr;
3016 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
3018 old_state = wps->state;
3019 wps->state = SEND_WSC_NACK;
3021 if (wps_parse_msg(msg, &attr) < 0)
3024 if (attr.msg_type == NULL) {
3025 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3029 if (*attr.msg_type != WPS_WSC_NACK) {
3030 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3035 #ifdef CONFIG_WPS_UPNP
3036 if (wps->wps->wps_upnp && wps->ext_reg) {
3037 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3038 "Registrar terminated by the Enrollee");
3041 #endif /* CONFIG_WPS_UPNP */
3043 if (attr.registrar_nonce == NULL ||
3044 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3046 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3050 if (attr.enrollee_nonce == NULL ||
3051 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3052 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3056 if (attr.config_error == NULL) {
3057 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
3062 config_error = WPA_GET_BE16(attr.config_error);
3063 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
3064 "Configuration Error %d", config_error);
3066 switch (old_state) {
3068 wps_fail_event(wps->wps, WPS_M2, config_error,
3069 wps->error_indication, wps->mac_addr_e);
3072 wps_fail_event(wps->wps, WPS_M4, config_error,
3073 wps->error_indication, wps->mac_addr_e);
3076 wps_fail_event(wps->wps, WPS_M6, config_error,
3077 wps->error_indication, wps->mac_addr_e);
3080 wps_fail_event(wps->wps, WPS_M8, config_error,
3081 wps->error_indication, wps->mac_addr_e);
3091 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
3092 const struct wpabuf *msg)
3094 struct wps_parse_attr attr;
3096 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
3098 if (wps->state != RECV_DONE &&
3099 (!wps->wps->wps_upnp || !wps->ext_reg)) {
3100 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
3101 "receiving WSC_Done", wps->state);
3105 if (wps_parse_msg(msg, &attr) < 0)
3108 if (attr.msg_type == NULL) {
3109 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3113 if (*attr.msg_type != WPS_WSC_DONE) {
3114 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3119 #ifdef CONFIG_WPS_UPNP
3120 if (wps->wps->wps_upnp && wps->ext_reg) {
3121 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3122 "Registrar completed successfully");
3123 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3127 #endif /* CONFIG_WPS_UPNP */
3129 if (attr.registrar_nonce == NULL ||
3130 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3132 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3136 if (attr.enrollee_nonce == NULL ||
3137 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3138 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3142 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
3143 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3146 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
3147 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
3148 struct wps_credential cred;
3150 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
3151 "on first Enrollee connection");
3153 os_memset(&cred, 0, sizeof(cred));
3154 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
3155 cred.ssid_len = wps->wps->ssid_len;
3156 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
3157 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
3158 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
3159 cred.key_len = wps->new_psk_len;
3161 wps->wps->wps_state = WPS_STATE_CONFIGURED;
3162 wpa_hexdump_ascii_key(MSG_DEBUG,
3163 "WPS: Generated random passphrase",
3164 wps->new_psk, wps->new_psk_len);
3165 if (wps->wps->cred_cb)
3166 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
3168 os_free(wps->new_psk);
3169 wps->new_psk = NULL;
3172 if (!wps->wps->ap && !wps->er)
3173 wps_sta_cred_cb(wps);
3176 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
3177 wps->p2p_dev_addr, wps->new_psk,
3178 wps->new_psk_len)) {
3179 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
3182 os_free(wps->new_psk);
3183 wps->new_psk = NULL;
3186 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e,
3187 wps->dev_password, wps->dev_password_len);
3190 wps_registrar_remove_pbc_session(wps->wps->registrar,
3193 wps_registrar_pbc_completed(wps->wps->registrar);
3194 os_get_time(&wps->wps->registrar->pbc_ignore_start);
3195 os_memcpy(wps->wps->registrar->pbc_ignore_uuid, wps->uuid_e,
3198 wps_registrar_pin_completed(wps->wps->registrar);
3200 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
3201 * merge them into APs own list.. */
3203 wps_success_event(wps->wps, wps->mac_addr_e);
3209 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
3210 enum wsc_op_code op_code,
3211 const struct wpabuf *msg)
3213 enum wps_process_res ret;
3215 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
3217 (unsigned long) wpabuf_len(msg), op_code);
3219 #ifdef CONFIG_WPS_UPNP
3220 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
3221 struct wps_parse_attr attr;
3222 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
3223 *attr.msg_type == WPS_M3)
3224 wps->ext_reg = 2; /* past M2/M2D phase */
3226 if (wps->ext_reg > 1)
3227 wps_registrar_free_pending_m2(wps->wps);
3228 if (wps->wps->wps_upnp && wps->ext_reg &&
3229 wps->wps->upnp_msgs == NULL &&
3230 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
3232 struct wps_parse_attr attr;
3234 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
3237 type = *attr.msg_type;
3238 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
3239 " to external Registrar for processing", type);
3240 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
3242 UPNP_WPS_WLANEVENT_TYPE_EAP,
3244 if (op_code == WSC_MSG)
3246 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
3247 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
3248 "external Registrar");
3249 return WPS_CONTINUE;
3251 #endif /* CONFIG_WPS_UPNP */
3255 return wps_process_wsc_msg(wps, msg);
3257 if (wps_validate_wsc_ack(msg) < 0)
3259 return wps_process_wsc_ack(wps, msg);
3261 if (wps_validate_wsc_nack(msg) < 0)
3263 return wps_process_wsc_nack(wps, msg);
3265 if (wps_validate_wsc_done(msg) < 0)
3267 ret = wps_process_wsc_done(wps, msg);
3268 if (ret == WPS_FAILURE) {
3269 wps->state = SEND_WSC_NACK;
3270 wps_fail_event(wps->wps, WPS_WSC_DONE,
3272 wps->error_indication, wps->mac_addr_e);
3276 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
3282 int wps_registrar_update_ie(struct wps_registrar *reg)
3284 return wps_set_ie(reg);
3288 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
3291 struct wps_registrar *reg = eloop_ctx;
3293 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
3294 "unselect internal Registrar");
3295 reg->selected_registrar = 0;
3297 wps_registrar_selected_registrar_changed(reg, 0);
3301 #ifdef CONFIG_WPS_UPNP
3302 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
3303 struct subscription *s)
3306 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
3307 "config_methods=0x%x)",
3308 s->dev_password_id, s->config_methods);
3309 reg->sel_reg_union = 1;
3310 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
3311 reg->sel_reg_dev_password_id_override = s->dev_password_id;
3312 if (reg->sel_reg_config_methods_override == -1)
3313 reg->sel_reg_config_methods_override = 0;
3314 reg->sel_reg_config_methods_override |= s->config_methods;
3315 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
3316 if (is_zero_ether_addr(reg->authorized_macs_union[i]))
3318 for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
3320 if (is_zero_ether_addr(s->authorized_macs[j]))
3322 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC into union: "
3323 MACSTR, MAC2STR(s->authorized_macs[j]));
3324 os_memcpy(reg->authorized_macs_union[i],
3325 s->authorized_macs[j], ETH_ALEN);
3328 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union",
3329 (u8 *) reg->authorized_macs_union,
3330 sizeof(reg->authorized_macs_union));
3332 #endif /* CONFIG_WPS_UPNP */
3335 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
3337 #ifdef CONFIG_WPS_UPNP
3338 struct subscription *s;
3340 if (reg->wps->wps_upnp == NULL)
3343 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
3344 struct subscription, list) {
3345 struct subscr_addr *sa;
3346 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
3348 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
3349 inet_ntoa(sa->saddr.sin_addr),
3350 ntohs(sa->saddr.sin_port));
3352 if (s->selected_registrar)
3353 wps_registrar_sel_reg_add(reg, s);
3355 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
3358 #endif /* CONFIG_WPS_UPNP */
3363 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3364 * @reg: Registrar data from wps_registrar_init()
3366 * This function is called when selected registrar state changes, e.g., when an
3367 * AP receives a SetSelectedRegistrar UPnP message.
3369 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg,
3372 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
3374 reg->sel_reg_union = reg->selected_registrar;
3375 reg->sel_reg_dev_password_id_override = -1;
3376 reg->sel_reg_config_methods_override = -1;
3377 os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
3378 WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
3379 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union (start with own)",
3380 (u8 *) reg->authorized_macs_union,
3381 sizeof(reg->authorized_macs_union));
3382 if (reg->selected_registrar) {
3385 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
3387 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
3388 WPS_CONFIG_PHY_PUSHBUTTON);
3389 #endif /* CONFIG_WPS2 */
3391 reg->sel_reg_dev_password_id_override =
3393 wps_set_pushbutton(&methods, reg->wps->config_methods);
3394 } else if (dev_pw_id)
3395 reg->sel_reg_dev_password_id_override = dev_pw_id;
3396 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
3397 "(pbc=%d)", reg->pbc);
3398 reg->sel_reg_config_methods_override = methods;
3400 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
3402 wps_registrar_sel_reg_union(reg);
3405 wps_cb_set_sel_reg(reg);
3409 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
3410 char *buf, size_t buflen)
3412 struct wps_registrar_device *d;
3415 char devtype[WPS_DEV_TYPE_BUFSIZE];
3417 d = wps_device_get(reg, addr);
3420 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
3423 ret = os_snprintf(buf + len, buflen - len,
3425 "wpsPrimaryDeviceType=%s\n"
3426 "wpsDeviceName=%s\n"
3427 "wpsManufacturer=%s\n"
3429 "wpsModelNumber=%s\n"
3430 "wpsSerialNumber=%s\n",
3432 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
3434 d->dev.device_name ? d->dev.device_name : "",
3435 d->dev.manufacturer ? d->dev.manufacturer : "",
3436 d->dev.model_name ? d->dev.model_name : "",
3437 d->dev.model_number ? d->dev.model_number : "",
3438 d->dev.serial_number ? d->dev.serial_number : "");
3439 if (ret < 0 || (size_t) ret >= buflen - len)
3447 int wps_registrar_config_ap(struct wps_registrar *reg,
3448 struct wps_credential *cred)
3451 printf("encr_type=0x%x\n", cred->encr_type);
3452 if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
3454 if (cred->encr_type & WPS_ENCR_WEP) {
3455 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
3456 "due to WEP configuration");
3460 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
3461 "invalid encr_type 0x%x", cred->encr_type);
3465 if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
3467 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
3469 cred->encr_type |= WPS_ENCR_AES;
3472 if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
3474 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
3476 cred->auth_type |= WPS_AUTH_WPA2PSK;
3478 #endif /* CONFIG_WPS2 */
3480 if (reg->wps->cred_cb)
3481 return reg->wps->cred_cb(reg->wps->cb_ctx, cred);
3487 #ifdef CONFIG_WPS_NFC
3489 int wps_registrar_add_nfc_pw_token(struct wps_registrar *reg,
3490 const u8 *pubkey_hash, u16 pw_id,
3491 const u8 *dev_pw, size_t dev_pw_len)
3493 struct wps_nfc_pw_token *token;
3495 if (dev_pw_len > WPS_OOB_DEVICE_PASSWORD_LEN)
3498 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, pw_id);
3500 token = os_zalloc(sizeof(*token));
3504 os_memcpy(token->pubkey_hash, pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
3505 token->pw_id = pw_id;
3506 wpa_snprintf_hex_uppercase((char *) token->dev_pw,
3507 sizeof(token->dev_pw),
3508 dev_pw, dev_pw_len);
3509 token->dev_pw_len = dev_pw_len * 2;
3511 dl_list_add(®->nfc_pw_tokens, &token->list);
3513 reg->selected_registrar = 1;
3515 wps_registrar_add_authorized_mac(reg,
3516 (u8 *) "\xff\xff\xff\xff\xff\xff");
3517 wps_registrar_selected_registrar_changed(reg, pw_id);
3518 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
3519 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
3520 wps_registrar_set_selected_timeout,
3523 wpa_printf(MSG_DEBUG, "WPS: Added NFC Device Password %u to Registrar",
3530 int wps_registrar_add_nfc_password_token(struct wps_registrar *reg,
3531 const u8 *oob_dev_pw,
3532 size_t oob_dev_pw_len)
3534 const u8 *pos, *hash, *dev_pw;
3538 if (oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2 +
3539 WPS_OOB_DEVICE_PASSWORD_MIN_LEN ||
3540 oob_dev_pw_len > WPS_OOB_PUBKEY_HASH_LEN + 2 +
3541 WPS_OOB_DEVICE_PASSWORD_LEN)
3545 pos = oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN;
3546 id = WPA_GET_BE16(pos);
3548 dev_pw_len = oob_dev_pw + oob_dev_pw_len - dev_pw;
3550 wpa_printf(MSG_DEBUG, "WPS: Add NFC Password Token for Password ID %u",
3553 wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
3554 hash, WPS_OOB_PUBKEY_HASH_LEN);
3555 wpa_hexdump_key(MSG_DEBUG, "WPS: Device Password", dev_pw, dev_pw_len);
3557 return wps_registrar_add_nfc_pw_token(reg, hash, id, dev_pw,
3562 void wps_registrar_remove_nfc_pw_token(struct wps_registrar *reg,
3563 struct wps_nfc_pw_token *token)
3565 wps_registrar_remove_authorized_mac(reg,
3566 (u8 *) "\xff\xff\xff\xff\xff\xff");
3567 wps_registrar_selected_registrar_changed(reg, 0);
3570 #endif /* CONFIG_WPS_NFC */