2 * Wi-Fi Protected Setup - Registrar
3 * Copyright (c) 2008-2012, 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];
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 *psk,
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;
175 struct wps_registrar_device *devices;
177 int force_pbc_overlap;
179 u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
180 u8 authorized_macs_union[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
182 u8 p2p_dev_addr[ETH_ALEN];
184 u8 pbc_ignore_uuid[WPS_UUID_LEN];
185 struct os_time pbc_ignore_start;
189 static int wps_set_ie(struct wps_registrar *reg);
190 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
191 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
193 static void wps_registrar_remove_pin(struct wps_registrar *reg,
194 struct wps_uuid_pin *pin);
197 static void wps_registrar_add_authorized_mac(struct wps_registrar *reg,
201 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC " MACSTR,
203 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
204 if (os_memcmp(reg->authorized_macs[i], addr, ETH_ALEN) == 0) {
205 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was "
206 "already in the list");
207 return; /* already in list */
209 for (i = WPS_MAX_AUTHORIZED_MACS - 1; i > 0; i--)
210 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i - 1],
212 os_memcpy(reg->authorized_macs[0], addr, ETH_ALEN);
213 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
214 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
218 static void wps_registrar_remove_authorized_mac(struct wps_registrar *reg,
222 wpa_printf(MSG_DEBUG, "WPS: Remove authorized MAC " MACSTR,
224 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++) {
225 if (os_memcmp(reg->authorized_macs, addr, ETH_ALEN) == 0)
228 if (i == WPS_MAX_AUTHORIZED_MACS) {
229 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was not in the "
231 return; /* not in the list */
233 for (; i + 1 < WPS_MAX_AUTHORIZED_MACS; i++)
234 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i + 1],
236 os_memset(reg->authorized_macs[WPS_MAX_AUTHORIZED_MACS - 1], 0,
238 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
239 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
243 static void wps_free_devices(struct wps_registrar_device *dev)
245 struct wps_registrar_device *prev;
250 wps_device_data_free(&prev->dev);
256 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
259 struct wps_registrar_device *dev;
261 for (dev = reg->devices; dev; dev = dev->next) {
262 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
269 static void wps_device_clone_data(struct wps_device_data *dst,
270 struct wps_device_data *src)
272 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
273 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
275 #define WPS_STRDUP(n) \
277 dst->n = src->n ? os_strdup(src->n) : NULL
279 WPS_STRDUP(device_name);
280 WPS_STRDUP(manufacturer);
281 WPS_STRDUP(model_name);
282 WPS_STRDUP(model_number);
283 WPS_STRDUP(serial_number);
288 int wps_device_store(struct wps_registrar *reg,
289 struct wps_device_data *dev, const u8 *uuid)
291 struct wps_registrar_device *d;
293 d = wps_device_get(reg, dev->mac_addr);
295 d = os_zalloc(sizeof(*d));
298 d->next = reg->devices;
302 wps_device_clone_data(&d->dev, dev);
303 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
309 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
310 const u8 *addr, const u8 *uuid_e)
312 struct wps_pbc_session *pbc, *prev = NULL;
317 pbc = reg->pbc_sessions;
319 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
320 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
322 prev->next = pbc->next;
324 reg->pbc_sessions = pbc->next;
332 pbc = os_zalloc(sizeof(*pbc));
335 os_memcpy(pbc->addr, addr, ETH_ALEN);
337 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
340 pbc->next = reg->pbc_sessions;
341 reg->pbc_sessions = pbc;
342 pbc->timestamp = now;
344 /* remove entries that have timed out */
349 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
351 wps_free_pbc_sessions(pbc);
360 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
362 const u8 *p2p_dev_addr)
364 struct wps_pbc_session *pbc, *prev = NULL, *tmp;
366 pbc = reg->pbc_sessions;
368 if (os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0 ||
370 (p2p_dev_addr && !is_zero_ether_addr(pbc->addr) &&
371 os_memcmp(pbc->addr, p2p_dev_addr, ETH_ALEN) ==
373 (p2p_dev_addr && !is_zero_ether_addr(reg->p2p_dev_addr) &&
374 os_memcmp(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
378 prev->next = pbc->next;
380 reg->pbc_sessions = pbc->next;
383 wpa_printf(MSG_DEBUG, "WPS: Removing PBC session for "
384 "addr=" MACSTR, MAC2STR(tmp->addr));
385 wpa_hexdump(MSG_DEBUG, "WPS: Removed UUID-E",
386 tmp->uuid_e, WPS_UUID_LEN);
396 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
397 const u8 *addr, const u8 *uuid_e)
400 struct wps_pbc_session *pbc;
401 struct wps_pbc_session *first = NULL;
406 wpa_printf(MSG_DEBUG, "WPS: Checking active PBC sessions for overlap");
409 wpa_printf(MSG_DEBUG, "WPS: Add one for the requested UUID");
410 wpa_hexdump(MSG_DEBUG, "WPS: Requested UUID",
411 uuid_e, WPS_UUID_LEN);
415 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
416 wpa_printf(MSG_DEBUG, "WPS: Consider PBC session with " MACSTR,
418 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E",
419 pbc->uuid_e, WPS_UUID_LEN);
420 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
421 wpa_printf(MSG_DEBUG, "WPS: PBC walk time has "
426 os_memcmp(pbc->uuid_e, first->uuid_e, WPS_UUID_LEN) == 0) {
427 wpa_printf(MSG_DEBUG, "WPS: Same Enrollee");
428 continue; /* same Enrollee */
430 if (uuid_e == NULL ||
431 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN)) {
432 wpa_printf(MSG_DEBUG, "WPS: New Enrollee");
439 wpa_printf(MSG_DEBUG, "WPS: %u active PBC session(s) found", count);
441 return count > 1 ? 1 : 0;
445 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
447 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
449 wpabuf_put_be16(msg, ATTR_WPS_STATE);
450 wpabuf_put_be16(msg, 1);
451 wpabuf_put_u8(msg, wps->wps_state);
456 #ifdef CONFIG_WPS_UPNP
457 static void wps_registrar_free_pending_m2(struct wps_context *wps)
459 struct upnp_pending_message *p, *p2, *prev = NULL;
462 if (p->type == WPS_M2 || p->type == WPS_M2D) {
464 wps->upnp_msgs = p->next;
466 prev->next = p->next;
467 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
470 wpabuf_free(p2->msg);
478 #endif /* CONFIG_WPS_UPNP */
481 static int wps_build_ap_setup_locked(struct wps_context *wps,
484 if (wps->ap_setup_locked && wps->ap_setup_locked != 2) {
485 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
486 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
487 wpabuf_put_be16(msg, 1);
488 wpabuf_put_u8(msg, 1);
494 static int wps_build_selected_registrar(struct wps_registrar *reg,
497 if (!reg->sel_reg_union)
499 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
500 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
501 wpabuf_put_be16(msg, 1);
502 wpabuf_put_u8(msg, 1);
507 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
510 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
511 if (!reg->sel_reg_union)
513 if (reg->sel_reg_dev_password_id_override >= 0)
514 id = reg->sel_reg_dev_password_id_override;
515 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
516 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
517 wpabuf_put_be16(msg, 2);
518 wpabuf_put_be16(msg, id);
523 static int wps_build_sel_pbc_reg_uuid_e(struct wps_registrar *reg,
526 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
527 if (!reg->sel_reg_union)
529 if (reg->sel_reg_dev_password_id_override >= 0)
530 id = reg->sel_reg_dev_password_id_override;
531 if (id != DEV_PW_PUSHBUTTON || !reg->dualband)
533 return wps_build_uuid_e(msg, reg->wps->uuid);
537 static void wps_set_pushbutton(u16 *methods, u16 conf_methods)
539 *methods |= WPS_CONFIG_PUSHBUTTON;
541 if ((conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON) ==
542 WPS_CONFIG_VIRT_PUSHBUTTON)
543 *methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
544 if ((conf_methods & WPS_CONFIG_PHY_PUSHBUTTON) ==
545 WPS_CONFIG_PHY_PUSHBUTTON)
546 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
547 if ((*methods & WPS_CONFIG_VIRT_PUSHBUTTON) !=
548 WPS_CONFIG_VIRT_PUSHBUTTON &&
549 (*methods & WPS_CONFIG_PHY_PUSHBUTTON) !=
550 WPS_CONFIG_PHY_PUSHBUTTON) {
552 * Required to include virtual/physical flag, but we were not
553 * configured with push button type, so have to default to one
556 *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
558 #endif /* CONFIG_WPS2 */
562 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
566 if (!reg->sel_reg_union)
568 methods = reg->wps->config_methods;
569 methods &= ~WPS_CONFIG_PUSHBUTTON;
571 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
572 WPS_CONFIG_PHY_PUSHBUTTON);
573 #endif /* CONFIG_WPS2 */
575 wps_set_pushbutton(&methods, reg->wps->config_methods);
576 if (reg->sel_reg_config_methods_override >= 0)
577 methods = reg->sel_reg_config_methods_override;
578 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
580 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
581 wpabuf_put_be16(msg, 2);
582 wpabuf_put_be16(msg, methods);
587 static int wps_build_probe_config_methods(struct wps_registrar *reg,
592 * These are the methods that the AP supports as an Enrollee for adding
593 * external Registrars.
595 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
597 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
598 WPS_CONFIG_PHY_PUSHBUTTON);
599 #endif /* CONFIG_WPS2 */
600 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
601 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
602 wpabuf_put_be16(msg, 2);
603 wpabuf_put_be16(msg, methods);
608 static int wps_build_config_methods_r(struct wps_registrar *reg,
611 return wps_build_config_methods(msg, reg->wps->config_methods);
615 const u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count)
620 while (*count < WPS_MAX_AUTHORIZED_MACS) {
621 if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
625 #endif /* CONFIG_WPS2 */
627 return (const u8 *) reg->authorized_macs_union;
632 * wps_registrar_init - Initialize WPS Registrar data
633 * @wps: Pointer to longterm WPS context
634 * @cfg: Registrar configuration
635 * Returns: Pointer to allocated Registrar data or %NULL on failure
637 * This function is used to initialize WPS Registrar functionality. It can be
638 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
639 * runs (e.g., when run as an internal Registrar in an AP). Caller is
640 * responsible for freeing the returned data with wps_registrar_deinit() when
641 * Registrar functionality is not needed anymore.
643 struct wps_registrar *
644 wps_registrar_init(struct wps_context *wps,
645 const struct wps_registrar_config *cfg)
647 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
651 dl_list_init(®->pins);
652 dl_list_init(®->nfc_pw_tokens);
654 reg->new_psk_cb = cfg->new_psk_cb;
655 reg->set_ie_cb = cfg->set_ie_cb;
656 reg->pin_needed_cb = cfg->pin_needed_cb;
657 reg->reg_success_cb = cfg->reg_success_cb;
658 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
659 reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
660 reg->cb_ctx = cfg->cb_ctx;
661 reg->skip_cred_build = cfg->skip_cred_build;
662 if (cfg->extra_cred) {
663 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
664 cfg->extra_cred_len);
665 if (reg->extra_cred == NULL) {
670 reg->disable_auto_conf = cfg->disable_auto_conf;
671 reg->sel_reg_dev_password_id_override = -1;
672 reg->sel_reg_config_methods_override = -1;
673 reg->static_wep_only = cfg->static_wep_only;
674 reg->dualband = cfg->dualband;
676 if (wps_set_ie(reg)) {
677 wps_registrar_deinit(reg);
686 * wps_registrar_deinit - Deinitialize WPS Registrar data
687 * @reg: Registrar data from wps_registrar_init()
689 void wps_registrar_deinit(struct wps_registrar *reg)
693 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
694 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
695 wps_free_pins(®->pins);
696 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, 0);
697 wps_free_pbc_sessions(reg->pbc_sessions);
698 wpabuf_free(reg->extra_cred);
699 wps_free_devices(reg->devices);
704 static void wps_registrar_invalidate_unused(struct wps_registrar *reg)
706 struct wps_uuid_pin *pin;
708 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
709 if (pin->wildcard_uuid == 1 && !(pin->flags & PIN_LOCKED)) {
710 wpa_printf(MSG_DEBUG, "WPS: Invalidate previously "
711 "configured wildcard PIN");
712 wps_registrar_remove_pin(reg, pin);
720 * wps_registrar_add_pin - Configure a new PIN for Registrar
721 * @reg: Registrar data from wps_registrar_init()
722 * @addr: Enrollee MAC address or %NULL if not known
723 * @uuid: UUID-E or %NULL for wildcard (any UUID)
724 * @pin: PIN (Device Password)
725 * @pin_len: Length of pin in octets
726 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
727 * Returns: 0 on success, -1 on failure
729 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
730 const u8 *uuid, const u8 *pin, size_t pin_len,
733 struct wps_uuid_pin *p;
735 p = os_zalloc(sizeof(*p));
739 os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
741 p->wildcard_uuid = 1;
743 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
744 p->pin = os_malloc(pin_len);
745 if (p->pin == NULL) {
749 os_memcpy(p->pin, pin, pin_len);
750 p->pin_len = pin_len;
753 p->flags |= PIN_EXPIRES;
754 os_get_time(&p->expiration);
755 p->expiration.sec += timeout;
758 if (p->wildcard_uuid)
759 wps_registrar_invalidate_unused(reg);
761 dl_list_add(®->pins, &p->list);
763 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
765 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
766 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
767 reg->selected_registrar = 1;
770 wps_registrar_add_authorized_mac(reg, addr);
772 wps_registrar_add_authorized_mac(
773 reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
774 wps_registrar_selected_registrar_changed(reg);
775 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
776 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
777 wps_registrar_set_selected_timeout,
784 static void wps_registrar_remove_pin(struct wps_registrar *reg,
785 struct wps_uuid_pin *pin)
788 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
790 if (is_zero_ether_addr(pin->enrollee_addr))
793 addr = pin->enrollee_addr;
794 wps_registrar_remove_authorized_mac(reg, addr);
796 wps_registrar_selected_registrar_changed(reg);
800 static void wps_registrar_expire_pins(struct wps_registrar *reg)
802 struct wps_uuid_pin *pin, *prev;
806 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
808 if ((pin->flags & PIN_EXPIRES) &&
809 os_time_before(&pin->expiration, &now)) {
810 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
811 pin->uuid, WPS_UUID_LEN);
812 wps_registrar_remove_pin(reg, pin);
819 * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
820 * @reg: Registrar data from wps_registrar_init()
821 * @dev_pw: PIN to search for or %NULL to match any
822 * @dev_pw_len: Length of dev_pw in octets
823 * Returns: 0 on success, -1 if not wildcard PIN is enabled
825 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg,
829 struct wps_uuid_pin *pin, *prev;
831 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
833 if (dev_pw && pin->pin &&
834 (dev_pw_len != pin->pin_len ||
835 os_memcmp(dev_pw, pin->pin, dev_pw_len) != 0))
836 continue; /* different PIN */
837 if (pin->wildcard_uuid) {
838 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
839 pin->uuid, WPS_UUID_LEN);
840 wps_registrar_remove_pin(reg, pin);
850 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
851 * @reg: Registrar data from wps_registrar_init()
853 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
855 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
857 struct wps_uuid_pin *pin, *prev;
859 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
861 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
862 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
863 pin->uuid, WPS_UUID_LEN);
864 wps_registrar_remove_pin(reg, pin);
873 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
874 const u8 *uuid, size_t *pin_len)
876 struct wps_uuid_pin *pin, *found = NULL;
878 wps_registrar_expire_pins(reg);
880 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
881 if (!pin->wildcard_uuid &&
882 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
889 /* Check for wildcard UUIDs since none of the UUID-specific
891 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
892 if (pin->wildcard_uuid == 1 ||
893 pin->wildcard_uuid == 2) {
894 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
895 "PIN. Assigned it for this UUID-E");
896 pin->wildcard_uuid++;
897 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
908 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
909 * that could otherwise avoid PIN invalidations.
911 if (found->flags & PIN_LOCKED) {
912 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
913 "allow concurrent re-use");
916 *pin_len = found->pin_len;
917 found->flags |= PIN_LOCKED;
923 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
924 * @reg: Registrar data from wps_registrar_init()
926 * Returns: 0 on success, -1 on failure
928 * PINs are locked to enforce only one concurrent use. This function unlocks a
929 * PIN to allow it to be used again. If the specified PIN was configured using
930 * a wildcard UUID, it will be removed instead of allowing multiple uses.
932 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
934 struct wps_uuid_pin *pin;
936 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
937 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
938 if (pin->wildcard_uuid == 3) {
939 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
941 return wps_registrar_invalidate_pin(reg, uuid);
943 pin->flags &= ~PIN_LOCKED;
952 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
954 reg->selected_registrar = 0;
956 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
957 wps_registrar_remove_authorized_mac(reg,
958 (u8 *) "\xff\xff\xff\xff\xff\xff");
959 wps_registrar_selected_registrar_changed(reg);
963 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
965 struct wps_registrar *reg = eloop_ctx;
967 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
968 wps_pbc_timeout_event(reg->wps);
969 wps_registrar_stop_pbc(reg);
974 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
975 * @reg: Registrar data from wps_registrar_init()
976 * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
977 * indicates no such filtering
978 * Returns: 0 on success, -1 on failure, -2 on session overlap
980 * This function is called on an AP when a push button is pushed to activate
981 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
982 * or when a PBC registration is completed. If more than one Enrollee in active
983 * PBC mode has been detected during the monitor time (previous 2 minutes), the
984 * PBC mode is not activated and -2 is returned to indicate session overlap.
985 * This is skipped if a specific Enrollee is selected.
987 int wps_registrar_button_pushed(struct wps_registrar *reg,
988 const u8 *p2p_dev_addr)
990 if (p2p_dev_addr == NULL &&
991 wps_registrar_pbc_overlap(reg, NULL, NULL)) {
992 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
994 wps_pbc_overlap_event(reg->wps);
997 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
998 reg->force_pbc_overlap = 0;
999 reg->selected_registrar = 1;
1002 os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
1004 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
1005 wps_registrar_add_authorized_mac(reg,
1006 (u8 *) "\xff\xff\xff\xff\xff\xff");
1007 wps_registrar_selected_registrar_changed(reg);
1009 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1010 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1011 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
1017 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
1019 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
1020 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1021 wps_registrar_stop_pbc(reg);
1025 static void wps_registrar_pin_completed(struct wps_registrar *reg)
1027 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
1028 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1029 reg->selected_registrar = 0;
1030 wps_registrar_selected_registrar_changed(reg);
1034 void wps_registrar_complete(struct wps_registrar *registrar, const u8 *uuid_e,
1035 const u8 *dev_pw, size_t dev_pw_len)
1037 if (registrar->pbc) {
1038 wps_registrar_remove_pbc_session(registrar,
1040 wps_registrar_pbc_completed(registrar);
1041 os_get_time(®istrar->pbc_ignore_start);
1042 os_memcpy(registrar->pbc_ignore_uuid, uuid_e, WPS_UUID_LEN);
1044 wps_registrar_pin_completed(registrar);
1048 wps_registrar_invalidate_wildcard_pin(registrar, dev_pw,
1050 wpa_hexdump_key(MSG_DEBUG, "WPS: Invalidated wildcard PIN",
1051 dev_pw, dev_pw_len);
1056 int wps_registrar_wps_cancel(struct wps_registrar *reg)
1059 wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
1060 wps_registrar_pbc_timeout(reg, NULL);
1061 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1063 } else if (reg->selected_registrar) {
1065 wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
1066 wps_registrar_pin_completed(reg);
1067 wps_registrar_invalidate_wildcard_pin(reg, NULL, 0);
1075 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
1076 * @reg: Registrar data from wps_registrar_init()
1077 * @addr: MAC address of the Probe Request sender
1078 * @wps_data: WPS IE contents
1080 * This function is called on an AP when a Probe Request with WPS IE is
1081 * received. This is used to track PBC mode use and to detect possible overlap
1082 * situation with other WPS APs.
1084 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
1085 const struct wpabuf *wps_data,
1088 struct wps_parse_attr attr;
1091 wpa_hexdump_buf(MSG_MSGDUMP,
1092 "WPS: Probe Request with WPS data received",
1095 if (wps_parse_msg(wps_data, &attr) < 0)
1098 if (attr.config_methods == NULL) {
1099 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
1104 if (attr.dev_password_id == NULL) {
1105 wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
1106 "in Probe Request");
1110 if (reg->enrollee_seen_cb && attr.uuid_e &&
1111 attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
1112 char *dev_name = NULL;
1113 if (attr.dev_name) {
1114 dev_name = os_zalloc(attr.dev_name_len + 1);
1116 os_memcpy(dev_name, attr.dev_name,
1120 reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
1121 attr.primary_dev_type,
1122 WPA_GET_BE16(attr.config_methods),
1123 WPA_GET_BE16(attr.dev_password_id),
1124 *attr.request_type, dev_name);
1128 if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
1129 return; /* Not PBC */
1131 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
1132 MACSTR, MAC2STR(addr));
1133 if (attr.uuid_e == NULL) {
1134 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
1138 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E from Probe Request", attr.uuid_e,
1141 #ifdef WPS_WORKAROUNDS
1142 if (reg->pbc_ignore_start.sec &&
1143 os_memcmp(attr.uuid_e, reg->pbc_ignore_uuid, WPS_UUID_LEN) == 0) {
1144 struct os_time now, dur;
1146 os_time_sub(&now, ®->pbc_ignore_start, &dur);
1147 if (dur.sec >= 0 && dur.sec < 5) {
1148 wpa_printf(MSG_DEBUG, "WPS: Ignore PBC activation "
1149 "based on Probe Request from the Enrollee "
1150 "that just completed PBC provisioning");
1153 reg->pbc_ignore_start.sec = 0;
1155 #endif /* WPS_WORKAROUNDS */
1158 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
1159 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
1160 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
1161 reg->force_pbc_overlap = 1;
1162 wps_pbc_overlap_event(reg->wps);
1167 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
1168 const u8 *psk, size_t psk_len)
1170 if (reg->new_psk_cb == NULL)
1173 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
1177 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
1178 const struct wps_device_data *dev)
1180 if (reg->pin_needed_cb == NULL)
1183 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
1187 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
1188 const u8 *uuid_e, const u8 *dev_pw,
1191 if (reg->reg_success_cb == NULL)
1194 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e, dev_pw, dev_pw_len);
1198 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
1199 struct wpabuf *probe_resp_ie)
1201 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
1205 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
1208 if (reg->set_sel_reg_cb == NULL)
1211 if (reg->selected_registrar) {
1212 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
1214 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
1215 WPS_CONFIG_PHY_PUSHBUTTON);
1216 #endif /* CONFIG_WPS2 */
1218 wps_set_pushbutton(&methods, reg->wps->config_methods);
1221 wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1222 "config_methods=0x%x pbc=%d methods=0x%x",
1223 reg->selected_registrar, reg->wps->config_methods,
1226 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
1227 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
1232 static int wps_set_ie(struct wps_registrar *reg)
1234 struct wpabuf *beacon;
1235 struct wpabuf *probe;
1236 const u8 *auth_macs;
1238 size_t vendor_len = 0;
1241 if (reg->set_ie_cb == NULL)
1244 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
1245 if (reg->wps->dev.vendor_ext[i]) {
1246 vendor_len += 2 + 2;
1247 vendor_len += wpabuf_len(reg->wps->dev.vendor_ext[i]);
1251 beacon = wpabuf_alloc(400 + vendor_len);
1254 probe = wpabuf_alloc(500 + vendor_len);
1255 if (probe == NULL) {
1256 wpabuf_free(beacon);
1260 auth_macs = wps_authorized_macs(reg, &count);
1262 wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
1264 if (wps_build_version(beacon) ||
1265 wps_build_wps_state(reg->wps, beacon) ||
1266 wps_build_ap_setup_locked(reg->wps, beacon) ||
1267 wps_build_selected_registrar(reg, beacon) ||
1268 wps_build_sel_reg_dev_password_id(reg, beacon) ||
1269 wps_build_sel_reg_config_methods(reg, beacon) ||
1270 wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
1271 (reg->dualband && wps_build_rf_bands(®->wps->dev, beacon)) ||
1272 wps_build_wfa_ext(beacon, 0, auth_macs, count) ||
1273 wps_build_vendor_ext(®->wps->dev, beacon)) {
1274 wpabuf_free(beacon);
1280 if (wps_build_dev_name(®->wps->dev, beacon) ||
1281 wps_build_primary_dev_type(®->wps->dev, beacon)) {
1282 wpabuf_free(beacon);
1286 #endif /* CONFIG_P2P */
1288 wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
1290 if (wps_build_version(probe) ||
1291 wps_build_wps_state(reg->wps, probe) ||
1292 wps_build_ap_setup_locked(reg->wps, probe) ||
1293 wps_build_selected_registrar(reg, probe) ||
1294 wps_build_sel_reg_dev_password_id(reg, probe) ||
1295 wps_build_sel_reg_config_methods(reg, probe) ||
1296 wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
1297 WPS_RESP_REGISTRAR) ||
1298 wps_build_uuid_e(probe, reg->wps->uuid) ||
1299 wps_build_device_attrs(®->wps->dev, probe) ||
1300 wps_build_probe_config_methods(reg, probe) ||
1301 (reg->dualband && wps_build_rf_bands(®->wps->dev, probe)) ||
1302 wps_build_wfa_ext(probe, 0, auth_macs, count) ||
1303 wps_build_vendor_ext(®->wps->dev, probe)) {
1304 wpabuf_free(beacon);
1309 beacon = wps_ie_encapsulate(beacon);
1310 probe = wps_ie_encapsulate(probe);
1312 if (!beacon || !probe) {
1313 wpabuf_free(beacon);
1318 if (reg->static_wep_only) {
1320 * Windows XP and Vista clients can get confused about
1321 * EAP-Identity/Request when they probe the network with
1322 * EAPOL-Start. In such a case, they may assume the network is
1323 * using IEEE 802.1X and prompt user for a certificate while
1324 * the correct (non-WPS) behavior would be to ask for the
1325 * static WEP key. As a workaround, use Microsoft Provisioning
1326 * IE to advertise that legacy 802.1X is not supported.
1328 const u8 ms_wps[7] = {
1329 WLAN_EID_VENDOR_SPECIFIC, 5,
1330 /* Microsoft Provisioning IE (00:50:f2:5) */
1331 0x00, 0x50, 0xf2, 5,
1332 0x00 /* no legacy 802.1X or MS WPS */
1334 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
1335 "into Beacon/Probe Response frames");
1336 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
1337 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
1340 return wps_cb_set_ie(reg, beacon, probe);
1344 static int wps_get_dev_password(struct wps_data *wps)
1349 os_free(wps->dev_password);
1350 wps->dev_password = NULL;
1353 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
1354 pin = (const u8 *) "00000000";
1356 #ifdef CONFIG_WPS_NFC
1357 } else if (wps->nfc_pw_token) {
1358 wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from NFC "
1360 pin = wps->nfc_pw_token->dev_pw;
1361 pin_len = wps->nfc_pw_token->dev_pw_len;
1362 #endif /* CONFIG_WPS_NFC */
1364 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1368 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1370 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1375 wps->dev_password = os_malloc(pin_len);
1376 if (wps->dev_password == NULL)
1378 os_memcpy(wps->dev_password, pin, pin_len);
1379 wps->dev_password_len = pin_len;
1385 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1387 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1388 wpabuf_put_be16(msg, ATTR_UUID_R);
1389 wpabuf_put_be16(msg, WPS_UUID_LEN);
1390 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1395 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1401 if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1403 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1404 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1405 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1407 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1408 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1409 "R-Hash derivation");
1413 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1414 wpabuf_put_be16(msg, ATTR_R_HASH1);
1415 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1416 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1417 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1418 addr[0] = wps->snonce;
1419 len[0] = WPS_SECRET_NONCE_LEN;
1420 addr[1] = wps->psk1;
1421 len[1] = WPS_PSK_LEN;
1422 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1423 len[2] = wpabuf_len(wps->dh_pubkey_e);
1424 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1425 len[3] = wpabuf_len(wps->dh_pubkey_r);
1426 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1427 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1429 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1430 wpabuf_put_be16(msg, ATTR_R_HASH2);
1431 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1432 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1433 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1434 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1435 addr[1] = wps->psk2;
1436 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1437 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1443 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1445 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1446 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1447 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1448 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1453 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1455 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1456 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1457 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1458 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1459 WPS_SECRET_NONCE_LEN);
1464 static int wps_build_cred_network_idx(struct wpabuf *msg,
1465 const struct wps_credential *cred)
1467 wpa_printf(MSG_DEBUG, "WPS: * Network Index (1)");
1468 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1469 wpabuf_put_be16(msg, 1);
1470 wpabuf_put_u8(msg, 1);
1475 static int wps_build_cred_ssid(struct wpabuf *msg,
1476 const struct wps_credential *cred)
1478 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1479 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID for Credential",
1480 cred->ssid, cred->ssid_len);
1481 wpabuf_put_be16(msg, ATTR_SSID);
1482 wpabuf_put_be16(msg, cred->ssid_len);
1483 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1488 static int wps_build_cred_auth_type(struct wpabuf *msg,
1489 const struct wps_credential *cred)
1491 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1493 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1494 wpabuf_put_be16(msg, 2);
1495 wpabuf_put_be16(msg, cred->auth_type);
1500 static int wps_build_cred_encr_type(struct wpabuf *msg,
1501 const struct wps_credential *cred)
1503 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1505 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1506 wpabuf_put_be16(msg, 2);
1507 wpabuf_put_be16(msg, cred->encr_type);
1512 static int wps_build_cred_network_key(struct wpabuf *msg,
1513 const struct wps_credential *cred)
1515 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1516 (int) cred->key_len);
1517 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
1518 cred->key, cred->key_len);
1519 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1520 wpabuf_put_be16(msg, cred->key_len);
1521 wpabuf_put_data(msg, cred->key, cred->key_len);
1526 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1527 const struct wps_credential *cred)
1529 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1530 MAC2STR(cred->mac_addr));
1531 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1532 wpabuf_put_be16(msg, ETH_ALEN);
1533 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1538 static int wps_build_credential(struct wpabuf *msg,
1539 const struct wps_credential *cred)
1541 if (wps_build_cred_network_idx(msg, cred) ||
1542 wps_build_cred_ssid(msg, cred) ||
1543 wps_build_cred_auth_type(msg, cred) ||
1544 wps_build_cred_encr_type(msg, cred) ||
1545 wps_build_cred_network_key(msg, cred) ||
1546 wps_build_cred_mac_addr(msg, cred))
1552 int wps_build_credential_wrap(struct wpabuf *msg,
1553 const struct wps_credential *cred)
1555 struct wpabuf *wbuf;
1556 wbuf = wpabuf_alloc(200);
1559 if (wps_build_credential(wbuf, cred)) {
1563 wpabuf_put_be16(msg, ATTR_CRED);
1564 wpabuf_put_be16(msg, wpabuf_len(wbuf));
1565 wpabuf_put_buf(msg, wbuf);
1571 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1573 struct wpabuf *cred;
1575 if (wps->wps->registrar->skip_cred_build)
1576 goto skip_cred_build;
1578 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1579 if (wps->use_cred) {
1580 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1583 os_memset(&wps->cred, 0, sizeof(wps->cred));
1585 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1586 wps->cred.ssid_len = wps->wps->ssid_len;
1588 /* Select the best authentication and encryption type */
1589 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1590 wps->auth_type = WPS_AUTH_WPA2PSK;
1591 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1592 wps->auth_type = WPS_AUTH_WPAPSK;
1593 else if (wps->auth_type & WPS_AUTH_OPEN)
1594 wps->auth_type = WPS_AUTH_OPEN;
1595 else if (wps->auth_type & WPS_AUTH_SHARED)
1596 wps->auth_type = WPS_AUTH_SHARED;
1598 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1602 wps->cred.auth_type = wps->auth_type;
1604 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1605 wps->auth_type == WPS_AUTH_WPAPSK) {
1606 if (wps->encr_type & WPS_ENCR_AES)
1607 wps->encr_type = WPS_ENCR_AES;
1608 else if (wps->encr_type & WPS_ENCR_TKIP)
1609 wps->encr_type = WPS_ENCR_TKIP;
1611 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1612 "type for WPA/WPA2");
1616 if (wps->encr_type & WPS_ENCR_WEP)
1617 wps->encr_type = WPS_ENCR_WEP;
1618 else if (wps->encr_type & WPS_ENCR_NONE)
1619 wps->encr_type = WPS_ENCR_NONE;
1621 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1622 "type for non-WPA/WPA2 mode");
1626 wps->cred.encr_type = wps->encr_type;
1628 * Set MAC address in the Credential to be the Enrollee's MAC address
1630 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1632 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1633 !wps->wps->registrar->disable_auto_conf) {
1635 /* Generate a random passphrase */
1636 if (random_get_bytes(r, sizeof(r)) < 0)
1638 os_free(wps->new_psk);
1639 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1640 if (wps->new_psk == NULL)
1642 wps->new_psk_len--; /* remove newline */
1643 while (wps->new_psk_len &&
1644 wps->new_psk[wps->new_psk_len - 1] == '=')
1646 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1647 wps->new_psk, wps->new_psk_len);
1648 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1649 wps->cred.key_len = wps->new_psk_len;
1650 } else if (wps->use_psk_key && wps->wps->psk_set) {
1652 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1653 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1654 os_memcpy(wps->cred.key, hex, 32 * 2);
1655 wps->cred.key_len = 32 * 2;
1656 } else if (wps->wps->network_key) {
1657 os_memcpy(wps->cred.key, wps->wps->network_key,
1658 wps->wps->network_key_len);
1659 wps->cred.key_len = wps->wps->network_key_len;
1660 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1662 /* Generate a random per-device PSK */
1663 os_free(wps->new_psk);
1664 wps->new_psk_len = 32;
1665 wps->new_psk = os_malloc(wps->new_psk_len);
1666 if (wps->new_psk == NULL)
1668 if (random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
1669 os_free(wps->new_psk);
1670 wps->new_psk = NULL;
1673 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1674 wps->new_psk, wps->new_psk_len);
1675 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1677 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1678 wps->cred.key_len = wps->new_psk_len * 2;
1682 #ifdef CONFIG_WPS_TESTING
1683 if (wps_testing_dummy_cred)
1684 cred = wpabuf_alloc(200);
1688 struct wps_credential dummy;
1689 wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
1690 os_memset(&dummy, 0, sizeof(dummy));
1691 os_memcpy(dummy.ssid, "dummy", 5);
1693 dummy.auth_type = WPS_AUTH_WPA2PSK;
1694 dummy.encr_type = WPS_ENCR_AES;
1695 os_memcpy(dummy.key, "dummy psk", 9);
1697 os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
1698 wps_build_credential(cred, &dummy);
1699 wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
1701 wpabuf_put_be16(msg, ATTR_CRED);
1702 wpabuf_put_be16(msg, wpabuf_len(cred));
1703 wpabuf_put_buf(msg, cred);
1707 #endif /* CONFIG_WPS_TESTING */
1709 cred = wpabuf_alloc(200);
1713 if (wps_build_credential(cred, &wps->cred)) {
1718 wpabuf_put_be16(msg, ATTR_CRED);
1719 wpabuf_put_be16(msg, wpabuf_len(cred));
1720 wpabuf_put_buf(msg, cred);
1724 if (wps->wps->registrar->extra_cred) {
1725 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1726 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1733 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1735 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1737 if (wps_build_credential(msg, &wps->cred))
1744 static struct wpabuf * wps_build_ap_cred(struct wps_data *wps)
1746 struct wpabuf *msg, *plain;
1748 msg = wpabuf_alloc(1000);
1752 plain = wpabuf_alloc(200);
1753 if (plain == NULL) {
1758 if (wps_build_ap_settings(wps, plain)) {
1764 wpabuf_put_be16(msg, ATTR_CRED);
1765 wpabuf_put_be16(msg, wpabuf_len(plain));
1766 wpabuf_put_buf(msg, plain);
1773 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1777 if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
1779 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1780 wps->nonce_r, WPS_NONCE_LEN);
1781 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1783 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1784 msg = wpabuf_alloc(1000);
1788 if (wps_build_version(msg) ||
1789 wps_build_msg_type(msg, WPS_M2) ||
1790 wps_build_enrollee_nonce(wps, msg) ||
1791 wps_build_registrar_nonce(wps, msg) ||
1792 wps_build_uuid_r(wps, msg) ||
1793 wps_build_public_key(wps, msg) ||
1794 wps_derive_keys(wps) ||
1795 wps_build_auth_type_flags(wps, msg) ||
1796 wps_build_encr_type_flags(wps, msg) ||
1797 wps_build_conn_type_flags(wps, msg) ||
1798 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1799 wps_build_device_attrs(&wps->wps->dev, msg) ||
1800 wps_build_rf_bands(&wps->wps->dev, msg) ||
1801 wps_build_assoc_state(wps, msg) ||
1802 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1803 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1804 wps_build_os_version(&wps->wps->dev, msg) ||
1805 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1806 wps_build_authenticator(wps, msg)) {
1812 wps->state = RECV_M3;
1817 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1820 u16 err = wps->config_error;
1822 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1823 msg = wpabuf_alloc(1000);
1827 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1828 err == WPS_CFG_NO_ERROR)
1829 err = WPS_CFG_SETUP_LOCKED;
1831 if (wps_build_version(msg) ||
1832 wps_build_msg_type(msg, WPS_M2D) ||
1833 wps_build_enrollee_nonce(wps, msg) ||
1834 wps_build_registrar_nonce(wps, msg) ||
1835 wps_build_uuid_r(wps, msg) ||
1836 wps_build_auth_type_flags(wps, msg) ||
1837 wps_build_encr_type_flags(wps, msg) ||
1838 wps_build_conn_type_flags(wps, msg) ||
1839 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1840 wps_build_device_attrs(&wps->wps->dev, msg) ||
1841 wps_build_rf_bands(&wps->wps->dev, msg) ||
1842 wps_build_assoc_state(wps, msg) ||
1843 wps_build_config_error(msg, err) ||
1844 wps_build_os_version(&wps->wps->dev, msg) ||
1845 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1850 wps->state = RECV_M2D_ACK;
1855 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1857 struct wpabuf *msg, *plain;
1859 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1861 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1863 plain = wpabuf_alloc(200);
1867 msg = wpabuf_alloc(1000);
1873 if (wps_build_version(msg) ||
1874 wps_build_msg_type(msg, WPS_M4) ||
1875 wps_build_enrollee_nonce(wps, msg) ||
1876 wps_build_r_hash(wps, msg) ||
1877 wps_build_r_snonce1(wps, plain) ||
1878 wps_build_key_wrap_auth(wps, plain) ||
1879 wps_build_encr_settings(wps, msg, plain) ||
1880 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1881 wps_build_authenticator(wps, msg)) {
1888 wps->state = RECV_M5;
1893 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1895 struct wpabuf *msg, *plain;
1897 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1899 plain = wpabuf_alloc(200);
1903 msg = wpabuf_alloc(1000);
1909 if (wps_build_version(msg) ||
1910 wps_build_msg_type(msg, WPS_M6) ||
1911 wps_build_enrollee_nonce(wps, msg) ||
1912 wps_build_r_snonce2(wps, plain) ||
1913 wps_build_key_wrap_auth(wps, plain) ||
1914 wps_build_encr_settings(wps, msg, plain) ||
1915 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1916 wps_build_authenticator(wps, msg)) {
1923 wps->wps_pin_revealed = 1;
1924 wps->state = RECV_M7;
1929 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1931 struct wpabuf *msg, *plain;
1933 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1935 plain = wpabuf_alloc(500);
1939 msg = wpabuf_alloc(1000);
1945 if (wps_build_version(msg) ||
1946 wps_build_msg_type(msg, WPS_M8) ||
1947 wps_build_enrollee_nonce(wps, msg) ||
1948 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1949 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1950 wps_build_key_wrap_auth(wps, plain) ||
1951 wps_build_encr_settings(wps, msg, plain) ||
1952 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1953 wps_build_authenticator(wps, msg)) {
1960 wps->state = RECV_DONE;
1965 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1966 enum wsc_op_code *op_code)
1970 #ifdef CONFIG_WPS_UPNP
1971 if (!wps->int_reg && wps->wps->wps_upnp) {
1972 struct upnp_pending_message *p, *prev = NULL;
1973 if (wps->ext_reg > 1)
1974 wps_registrar_free_pending_m2(wps->wps);
1975 p = wps->wps->upnp_msgs;
1976 /* TODO: check pending message MAC address */
1977 while (p && p->next) {
1982 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1987 wps->wps->upnp_msgs = NULL;
1994 *op_code = WSC_NACK;
2001 if (wps->ext_reg == 0)
2007 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
2008 "pending message available");
2011 #endif /* CONFIG_WPS_UPNP */
2013 switch (wps->state) {
2015 if (wps_get_dev_password(wps) < 0)
2016 msg = wps_build_m2d(wps);
2018 msg = wps_build_m2(wps);
2022 msg = wps_build_m2d(wps);
2026 msg = wps_build_m4(wps);
2030 msg = wps_build_m6(wps);
2034 msg = wps_build_m8(wps);
2038 msg = wps_build_wsc_ack(wps);
2042 msg = wps_build_wsc_nack(wps);
2043 *op_code = WSC_NACK;
2046 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
2047 "a message", wps->state);
2052 if (*op_code == WSC_MSG && msg) {
2053 /* Save a copy of the last message for Authenticator derivation
2055 wpabuf_free(wps->last_msg);
2056 wps->last_msg = wpabuf_dup(msg);
2063 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
2065 if (e_nonce == NULL) {
2066 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
2070 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
2071 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
2072 wps->nonce_e, WPS_NONCE_LEN);
2078 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
2080 if (r_nonce == NULL) {
2081 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
2085 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
2086 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
2094 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
2096 if (uuid_e == NULL) {
2097 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
2101 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
2102 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
2108 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
2110 if (pw_id == NULL) {
2111 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
2115 wps->dev_pw_id = WPA_GET_BE16(pw_id);
2116 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
2122 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
2124 if (e_hash1 == NULL) {
2125 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
2129 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
2130 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
2136 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
2138 if (e_hash2 == NULL) {
2139 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
2143 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
2144 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
2150 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
2152 u8 hash[SHA256_MAC_LEN];
2156 if (e_snonce1 == NULL) {
2157 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
2161 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
2162 WPS_SECRET_NONCE_LEN);
2164 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
2165 addr[0] = e_snonce1;
2166 len[0] = WPS_SECRET_NONCE_LEN;
2167 addr[1] = wps->psk1;
2168 len[1] = WPS_PSK_LEN;
2169 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2170 len[2] = wpabuf_len(wps->dh_pubkey_e);
2171 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2172 len[3] = wpabuf_len(wps->dh_pubkey_r);
2173 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2175 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
2176 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
2177 "not match with the pre-committed value");
2178 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2179 wps_pwd_auth_fail_event(wps->wps, 0, 1);
2183 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
2184 "half of the device password");
2190 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
2192 u8 hash[SHA256_MAC_LEN];
2196 if (e_snonce2 == NULL) {
2197 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
2201 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
2202 WPS_SECRET_NONCE_LEN);
2204 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
2205 addr[0] = e_snonce2;
2206 len[0] = WPS_SECRET_NONCE_LEN;
2207 addr[1] = wps->psk2;
2208 len[1] = WPS_PSK_LEN;
2209 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2210 len[2] = wpabuf_len(wps->dh_pubkey_e);
2211 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2212 len[3] = wpabuf_len(wps->dh_pubkey_r);
2213 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2215 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
2216 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
2217 "not match with the pre-committed value");
2218 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2219 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2220 wps_pwd_auth_fail_event(wps->wps, 0, 2);
2224 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
2225 "half of the device password");
2226 wps->wps_pin_revealed = 0;
2227 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
2230 * In case wildcard PIN is used and WPS handshake succeeds in the first
2231 * attempt, wps_registrar_unlock_pin() would not free the PIN, so make
2232 * sure the PIN gets invalidated here.
2234 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2240 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
2242 if (mac_addr == NULL) {
2243 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
2247 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
2249 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
2250 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
2256 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
2259 if (pk == NULL || pk_len == 0) {
2260 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
2264 wpabuf_free(wps->dh_pubkey_e);
2265 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
2266 if (wps->dh_pubkey_e == NULL)
2273 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
2278 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
2283 auth_types = WPA_GET_BE16(auth);
2285 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
2287 wps->auth_type = wps->wps->auth_types & auth_types;
2288 if (wps->auth_type == 0) {
2289 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2290 "authentication types (own 0x%x Enrollee 0x%x)",
2291 wps->wps->auth_types, auth_types);
2292 #ifdef WPS_WORKAROUNDS
2294 * Some deployed implementations seem to advertise incorrect
2295 * information in this attribute. For example, Linksys WRT350N
2296 * seems to have a byteorder bug that breaks this negotiation.
2297 * In order to interoperate with existing implementations,
2298 * assume that the Enrollee supports everything we do.
2300 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2301 "does not advertise supported authentication types "
2303 wps->auth_type = wps->wps->auth_types;
2304 #else /* WPS_WORKAROUNDS */
2306 #endif /* WPS_WORKAROUNDS */
2313 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
2318 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
2323 encr_types = WPA_GET_BE16(encr);
2325 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
2327 wps->encr_type = wps->wps->encr_types & encr_types;
2328 if (wps->encr_type == 0) {
2329 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2330 "encryption types (own 0x%x Enrollee 0x%x)",
2331 wps->wps->encr_types, encr_types);
2332 #ifdef WPS_WORKAROUNDS
2334 * Some deployed implementations seem to advertise incorrect
2335 * information in this attribute. For example, Linksys WRT350N
2336 * seems to have a byteorder bug that breaks this negotiation.
2337 * In order to interoperate with existing implementations,
2338 * assume that the Enrollee supports everything we do.
2340 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2341 "does not advertise supported encryption types "
2343 wps->encr_type = wps->wps->encr_types;
2344 #else /* WPS_WORKAROUNDS */
2346 #endif /* WPS_WORKAROUNDS */
2353 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
2356 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
2361 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
2368 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
2372 if (methods == NULL) {
2373 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
2377 m = WPA_GET_BE16(methods);
2379 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
2380 "%s%s%s%s%s%s%s%s%s", m,
2381 m & WPS_CONFIG_USBA ? " [USBA]" : "",
2382 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
2383 m & WPS_CONFIG_LABEL ? " [Label]" : "",
2384 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
2385 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
2386 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
2387 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
2388 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
2389 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
2391 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
2393 * The Enrollee does not have a display so it is unlikely to be
2394 * able to show the passphrase to a user and as such, could
2395 * benefit from receiving PSK to reduce key derivation time.
2397 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2398 "Enrollee not supporting display");
2399 wps->use_psk_key = 1;
2406 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2408 if (state == NULL) {
2409 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2414 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2421 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2425 if (assoc == NULL) {
2426 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2430 a = WPA_GET_BE16(assoc);
2431 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2437 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2442 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2446 e = WPA_GET_BE16(err);
2447 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2453 static int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
2456 struct wps_registrar *reg = wps->wps->registrar;
2458 if (is_zero_ether_addr(reg->p2p_dev_addr))
2459 return 1; /* no filtering in use */
2461 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
2462 wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
2463 "filtering for PBC: expected " MACSTR " was "
2464 MACSTR " - indicate PBC session overlap",
2465 MAC2STR(reg->p2p_dev_addr),
2466 MAC2STR(wps->p2p_dev_addr));
2469 #endif /* CONFIG_P2P */
2474 static int wps_registrar_skip_overlap(struct wps_data *wps)
2477 struct wps_registrar *reg = wps->wps->registrar;
2479 if (is_zero_ether_addr(reg->p2p_dev_addr))
2480 return 0; /* no specific Enrollee selected */
2482 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) == 0) {
2483 wpa_printf(MSG_DEBUG, "WPS: Skip PBC overlap due to selected "
2487 #endif /* CONFIG_P2P */
2492 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2493 struct wps_parse_attr *attr)
2495 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2497 if (wps->state != RECV_M1) {
2498 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2499 "receiving M1", wps->state);
2503 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2504 wps_process_mac_addr(wps, attr->mac_addr) ||
2505 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2506 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2507 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2508 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2509 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2510 wps_process_config_methods(wps, attr->config_methods) ||
2511 wps_process_wps_state(wps, attr->wps_state) ||
2512 wps_process_device_attrs(&wps->peer_dev, attr) ||
2513 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2514 wps_process_assoc_state(wps, attr->assoc_state) ||
2515 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2516 wps_process_config_error(wps, attr->config_error) ||
2517 wps_process_os_version(&wps->peer_dev, attr->os_version))
2520 if (wps->dev_pw_id < 0x10 &&
2521 wps->dev_pw_id != DEV_PW_DEFAULT &&
2522 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2523 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2524 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2525 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2526 !wps->wps->registrar->pbc)) {
2527 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2529 wps->state = SEND_M2D;
2530 return WPS_CONTINUE;
2533 #ifdef CONFIG_WPS_NFC
2534 if (wps->dev_pw_id >= 0x10) {
2535 struct wps_nfc_pw_token *token;
2537 u8 hash[WPS_HASH_LEN];
2539 token = wps_get_nfc_pw_token(
2540 &wps->wps->registrar->nfc_pw_tokens, wps->dev_pw_id);
2542 wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
2544 dl_list_del(&token->list);
2545 wps->nfc_pw_token = token;
2547 addr[0] = attr->public_key;
2548 sha256_vector(1, addr, &attr->public_key_len, hash);
2549 if (os_memcmp(hash, wps->nfc_pw_token->pubkey_hash,
2550 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
2551 wpa_printf(MSG_ERROR, "WPS: Public Key hash "
2557 #endif /* CONFIG_WPS_NFC */
2559 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2560 if ((wps->wps->registrar->force_pbc_overlap ||
2561 wps_registrar_pbc_overlap(wps->wps->registrar,
2562 wps->mac_addr_e, wps->uuid_e) ||
2563 !wps_registrar_p2p_dev_addr_match(wps)) &&
2564 !wps_registrar_skip_overlap(wps)) {
2565 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2567 wps->state = SEND_M2D;
2568 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2569 wps_pbc_overlap_event(wps->wps);
2570 wps_fail_event(wps->wps, WPS_M1,
2571 WPS_CFG_MULTIPLE_PBC_DETECTED,
2573 wps->wps->registrar->force_pbc_overlap = 1;
2574 return WPS_CONTINUE;
2576 wps_registrar_add_pbc_session(wps->wps->registrar,
2577 wps->mac_addr_e, wps->uuid_e);
2581 #ifdef WPS_WORKAROUNDS
2583 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2584 * passphrase format. To avoid interop issues, force PSK format to be
2587 if (!wps->use_psk_key &&
2588 wps->peer_dev.manufacturer &&
2589 os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
2590 wps->peer_dev.model_name &&
2591 os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
2592 wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
2594 wps->use_psk_key = 1;
2596 #endif /* WPS_WORKAROUNDS */
2598 wps->state = SEND_M2;
2599 return WPS_CONTINUE;
2603 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2604 const struct wpabuf *msg,
2605 struct wps_parse_attr *attr)
2607 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2609 if (wps->state != RECV_M3) {
2610 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2611 "receiving M3", wps->state);
2612 wps->state = SEND_WSC_NACK;
2613 return WPS_CONTINUE;
2616 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2617 !wps_registrar_skip_overlap(wps)) {
2618 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2620 wps->state = SEND_WSC_NACK;
2621 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2622 return WPS_CONTINUE;
2625 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2626 wps_process_authenticator(wps, attr->authenticator, msg) ||
2627 wps_process_e_hash1(wps, attr->e_hash1) ||
2628 wps_process_e_hash2(wps, attr->e_hash2)) {
2629 wps->state = SEND_WSC_NACK;
2630 return WPS_CONTINUE;
2633 wps->state = SEND_M4;
2634 return WPS_CONTINUE;
2638 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2639 const struct wpabuf *msg,
2640 struct wps_parse_attr *attr)
2642 struct wpabuf *decrypted;
2643 struct wps_parse_attr eattr;
2645 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2647 if (wps->state != RECV_M5) {
2648 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2649 "receiving M5", wps->state);
2650 wps->state = SEND_WSC_NACK;
2651 return WPS_CONTINUE;
2654 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2655 !wps_registrar_skip_overlap(wps)) {
2656 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2658 wps->state = SEND_WSC_NACK;
2659 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2660 return WPS_CONTINUE;
2663 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2664 wps_process_authenticator(wps, attr->authenticator, msg)) {
2665 wps->state = SEND_WSC_NACK;
2666 return WPS_CONTINUE;
2669 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2670 attr->encr_settings_len);
2671 if (decrypted == NULL) {
2672 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2673 "Settings attribute");
2674 wps->state = SEND_WSC_NACK;
2675 return WPS_CONTINUE;
2678 if (wps_validate_m5_encr(decrypted, attr->version2 != NULL) < 0) {
2679 wpabuf_free(decrypted);
2680 wps->state = SEND_WSC_NACK;
2681 return WPS_CONTINUE;
2684 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2686 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2687 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2688 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2689 wpabuf_free(decrypted);
2690 wps->state = SEND_WSC_NACK;
2691 return WPS_CONTINUE;
2693 wpabuf_free(decrypted);
2695 wps->state = SEND_M6;
2696 return WPS_CONTINUE;
2700 static void wps_sta_cred_cb(struct wps_data *wps)
2703 * Update credential to only include a single authentication and
2704 * encryption type in case the AP configuration includes more than one
2707 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2708 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2709 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2710 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2711 if (wps->cred.encr_type & WPS_ENCR_AES)
2712 wps->cred.encr_type = WPS_ENCR_AES;
2713 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2714 wps->cred.encr_type = WPS_ENCR_TKIP;
2715 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2716 "AP configuration");
2717 if (wps->wps->cred_cb)
2718 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2722 static void wps_cred_update(struct wps_credential *dst,
2723 struct wps_credential *src)
2725 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2726 dst->ssid_len = src->ssid_len;
2727 dst->auth_type = src->auth_type;
2728 dst->encr_type = src->encr_type;
2729 dst->key_idx = src->key_idx;
2730 os_memcpy(dst->key, src->key, sizeof(dst->key));
2731 dst->key_len = src->key_len;
2735 static int wps_process_ap_settings_r(struct wps_data *wps,
2736 struct wps_parse_attr *attr)
2740 if (wps->wps->ap || wps->er)
2743 /* AP Settings Attributes in M7 when Enrollee is an AP */
2744 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2747 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2749 if (wps->new_ap_settings) {
2750 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2752 wps_cred_update(&wps->cred, wps->new_ap_settings);
2756 * Use the AP PIN only to receive the current AP settings, not
2757 * to reconfigure the AP.
2761 * Clear selected registrar here since we do not get to
2762 * WSC_Done in this protocol run.
2764 wps_registrar_pin_completed(wps->wps->registrar);
2766 msg = wps_build_ap_cred(wps);
2769 wps->cred.cred_attr = wpabuf_head(msg);
2770 wps->cred.cred_attr_len = wpabuf_len(msg);
2772 if (wps->ap_settings_cb) {
2773 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2778 wps_sta_cred_cb(wps);
2780 wps->cred.cred_attr = NULL;
2781 wps->cred.cred_attr_len = 0;
2789 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2790 const struct wpabuf *msg,
2791 struct wps_parse_attr *attr)
2793 struct wpabuf *decrypted;
2794 struct wps_parse_attr eattr;
2796 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2798 if (wps->state != RECV_M7) {
2799 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2800 "receiving M7", wps->state);
2801 wps->state = SEND_WSC_NACK;
2802 return WPS_CONTINUE;
2805 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2806 !wps_registrar_skip_overlap(wps)) {
2807 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2809 wps->state = SEND_WSC_NACK;
2810 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2811 return WPS_CONTINUE;
2814 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2815 wps_process_authenticator(wps, attr->authenticator, msg)) {
2816 wps->state = SEND_WSC_NACK;
2817 return WPS_CONTINUE;
2820 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2821 attr->encr_settings_len);
2822 if (decrypted == NULL) {
2823 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2824 "Settings attribute");
2825 wps->state = SEND_WSC_NACK;
2826 return WPS_CONTINUE;
2829 if (wps_validate_m7_encr(decrypted, wps->wps->ap || wps->er,
2830 attr->version2 != NULL) < 0) {
2831 wpabuf_free(decrypted);
2832 wps->state = SEND_WSC_NACK;
2833 return WPS_CONTINUE;
2836 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2838 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2839 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2840 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2841 wps_process_ap_settings_r(wps, &eattr)) {
2842 wpabuf_free(decrypted);
2843 wps->state = SEND_WSC_NACK;
2844 return WPS_CONTINUE;
2847 wpabuf_free(decrypted);
2849 wps->state = SEND_M8;
2850 return WPS_CONTINUE;
2854 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2855 const struct wpabuf *msg)
2857 struct wps_parse_attr attr;
2858 enum wps_process_res ret = WPS_CONTINUE;
2860 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2862 if (wps_parse_msg(msg, &attr) < 0)
2865 if (attr.msg_type == NULL) {
2866 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2867 wps->state = SEND_WSC_NACK;
2868 return WPS_CONTINUE;
2871 if (*attr.msg_type != WPS_M1 &&
2872 (attr.registrar_nonce == NULL ||
2873 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2874 WPS_NONCE_LEN) != 0)) {
2875 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2879 switch (*attr.msg_type) {
2881 if (wps_validate_m1(msg) < 0)
2883 #ifdef CONFIG_WPS_UPNP
2884 if (wps->wps->wps_upnp && attr.mac_addr) {
2885 /* Remove old pending messages when starting new run */
2886 wps_free_pending_msgs(wps->wps->upnp_msgs);
2887 wps->wps->upnp_msgs = NULL;
2889 upnp_wps_device_send_wlan_event(
2890 wps->wps->wps_upnp, attr.mac_addr,
2891 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2893 #endif /* CONFIG_WPS_UPNP */
2894 ret = wps_process_m1(wps, &attr);
2897 if (wps_validate_m3(msg) < 0)
2899 ret = wps_process_m3(wps, msg, &attr);
2900 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2901 wps_fail_event(wps->wps, WPS_M3, wps->config_error,
2902 wps->error_indication);
2905 if (wps_validate_m5(msg) < 0)
2907 ret = wps_process_m5(wps, msg, &attr);
2908 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2909 wps_fail_event(wps->wps, WPS_M5, wps->config_error,
2910 wps->error_indication);
2913 if (wps_validate_m7(msg) < 0)
2915 ret = wps_process_m7(wps, msg, &attr);
2916 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2917 wps_fail_event(wps->wps, WPS_M7, wps->config_error,
2918 wps->error_indication);
2921 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2926 if (ret == WPS_CONTINUE) {
2927 /* Save a copy of the last message for Authenticator derivation
2929 wpabuf_free(wps->last_msg);
2930 wps->last_msg = wpabuf_dup(msg);
2937 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2938 const struct wpabuf *msg)
2940 struct wps_parse_attr attr;
2942 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2944 if (wps_parse_msg(msg, &attr) < 0)
2947 if (attr.msg_type == NULL) {
2948 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2952 if (*attr.msg_type != WPS_WSC_ACK) {
2953 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2958 #ifdef CONFIG_WPS_UPNP
2959 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2960 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2961 if (wps->wps->upnp_msgs)
2962 return WPS_CONTINUE;
2963 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2964 "external Registrar");
2967 #endif /* CONFIG_WPS_UPNP */
2969 if (attr.registrar_nonce == NULL ||
2970 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
2972 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2976 if (attr.enrollee_nonce == NULL ||
2977 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
2978 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2982 if (wps->state == RECV_M2D_ACK) {
2983 #ifdef CONFIG_WPS_UPNP
2984 if (wps->wps->wps_upnp &&
2985 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2986 if (wps->wps->upnp_msgs)
2987 return WPS_CONTINUE;
2988 if (wps->ext_reg == 0)
2990 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2991 "external Registrar");
2994 #endif /* CONFIG_WPS_UPNP */
2996 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2997 "terminate negotiation");
3004 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
3005 const struct wpabuf *msg)
3007 struct wps_parse_attr attr;
3011 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
3013 old_state = wps->state;
3014 wps->state = SEND_WSC_NACK;
3016 if (wps_parse_msg(msg, &attr) < 0)
3019 if (attr.msg_type == NULL) {
3020 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3024 if (*attr.msg_type != WPS_WSC_NACK) {
3025 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3030 #ifdef CONFIG_WPS_UPNP
3031 if (wps->wps->wps_upnp && wps->ext_reg) {
3032 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3033 "Registrar terminated by the Enrollee");
3036 #endif /* CONFIG_WPS_UPNP */
3038 if (attr.registrar_nonce == NULL ||
3039 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3041 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3045 if (attr.enrollee_nonce == NULL ||
3046 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3047 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3051 if (attr.config_error == NULL) {
3052 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
3057 config_error = WPA_GET_BE16(attr.config_error);
3058 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
3059 "Configuration Error %d", config_error);
3061 switch (old_state) {
3063 wps_fail_event(wps->wps, WPS_M2, config_error,
3064 wps->error_indication);
3067 wps_fail_event(wps->wps, WPS_M4, config_error,
3068 wps->error_indication);
3071 wps_fail_event(wps->wps, WPS_M6, config_error,
3072 wps->error_indication);
3075 wps_fail_event(wps->wps, WPS_M8, config_error,
3076 wps->error_indication);
3086 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
3087 const struct wpabuf *msg)
3089 struct wps_parse_attr attr;
3091 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
3093 if (wps->state != RECV_DONE &&
3094 (!wps->wps->wps_upnp || !wps->ext_reg)) {
3095 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
3096 "receiving WSC_Done", wps->state);
3100 if (wps_parse_msg(msg, &attr) < 0)
3103 if (attr.msg_type == NULL) {
3104 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3108 if (*attr.msg_type != WPS_WSC_DONE) {
3109 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3114 #ifdef CONFIG_WPS_UPNP
3115 if (wps->wps->wps_upnp && wps->ext_reg) {
3116 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3117 "Registrar completed successfully");
3118 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3122 #endif /* CONFIG_WPS_UPNP */
3124 if (attr.registrar_nonce == NULL ||
3125 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3127 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3131 if (attr.enrollee_nonce == NULL ||
3132 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3133 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3137 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
3138 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3141 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
3142 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
3143 struct wps_credential cred;
3145 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
3146 "on first Enrollee connection");
3148 os_memset(&cred, 0, sizeof(cred));
3149 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
3150 cred.ssid_len = wps->wps->ssid_len;
3151 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
3152 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
3153 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
3154 cred.key_len = wps->new_psk_len;
3156 wps->wps->wps_state = WPS_STATE_CONFIGURED;
3157 wpa_hexdump_ascii_key(MSG_DEBUG,
3158 "WPS: Generated random passphrase",
3159 wps->new_psk, wps->new_psk_len);
3160 if (wps->wps->cred_cb)
3161 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
3163 os_free(wps->new_psk);
3164 wps->new_psk = NULL;
3167 if (!wps->wps->ap && !wps->er)
3168 wps_sta_cred_cb(wps);
3171 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
3172 wps->new_psk, wps->new_psk_len)) {
3173 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
3176 os_free(wps->new_psk);
3177 wps->new_psk = NULL;
3180 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e,
3181 wps->dev_password, wps->dev_password_len);
3184 wps_registrar_remove_pbc_session(wps->wps->registrar,
3187 wps_registrar_pbc_completed(wps->wps->registrar);
3188 os_get_time(&wps->wps->registrar->pbc_ignore_start);
3189 os_memcpy(wps->wps->registrar->pbc_ignore_uuid, wps->uuid_e,
3192 wps_registrar_pin_completed(wps->wps->registrar);
3194 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
3195 * merge them into APs own list.. */
3197 wps_success_event(wps->wps);
3203 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
3204 enum wsc_op_code op_code,
3205 const struct wpabuf *msg)
3207 enum wps_process_res ret;
3209 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
3211 (unsigned long) wpabuf_len(msg), op_code);
3213 #ifdef CONFIG_WPS_UPNP
3214 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
3215 struct wps_parse_attr attr;
3216 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
3217 *attr.msg_type == WPS_M3)
3218 wps->ext_reg = 2; /* past M2/M2D phase */
3220 if (wps->ext_reg > 1)
3221 wps_registrar_free_pending_m2(wps->wps);
3222 if (wps->wps->wps_upnp && wps->ext_reg &&
3223 wps->wps->upnp_msgs == NULL &&
3224 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
3226 struct wps_parse_attr attr;
3228 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
3231 type = *attr.msg_type;
3232 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
3233 " to external Registrar for processing", type);
3234 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
3236 UPNP_WPS_WLANEVENT_TYPE_EAP,
3238 if (op_code == WSC_MSG)
3240 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
3241 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
3242 "external Registrar");
3243 return WPS_CONTINUE;
3245 #endif /* CONFIG_WPS_UPNP */
3249 return wps_process_wsc_msg(wps, msg);
3251 if (wps_validate_wsc_ack(msg) < 0)
3253 return wps_process_wsc_ack(wps, msg);
3255 if (wps_validate_wsc_nack(msg) < 0)
3257 return wps_process_wsc_nack(wps, msg);
3259 if (wps_validate_wsc_done(msg) < 0)
3261 ret = wps_process_wsc_done(wps, msg);
3262 if (ret == WPS_FAILURE) {
3263 wps->state = SEND_WSC_NACK;
3264 wps_fail_event(wps->wps, WPS_WSC_DONE,
3266 wps->error_indication);
3270 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
3276 int wps_registrar_update_ie(struct wps_registrar *reg)
3278 return wps_set_ie(reg);
3282 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
3285 struct wps_registrar *reg = eloop_ctx;
3287 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
3288 "unselect internal Registrar");
3289 reg->selected_registrar = 0;
3291 wps_registrar_selected_registrar_changed(reg);
3295 #ifdef CONFIG_WPS_UPNP
3296 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
3297 struct subscription *s)
3300 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
3301 "config_methods=0x%x)",
3302 s->dev_password_id, s->config_methods);
3303 reg->sel_reg_union = 1;
3304 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
3305 reg->sel_reg_dev_password_id_override = s->dev_password_id;
3306 if (reg->sel_reg_config_methods_override == -1)
3307 reg->sel_reg_config_methods_override = 0;
3308 reg->sel_reg_config_methods_override |= s->config_methods;
3309 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
3310 if (is_zero_ether_addr(reg->authorized_macs_union[i]))
3312 for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
3314 if (is_zero_ether_addr(s->authorized_macs[j]))
3316 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC into union: "
3317 MACSTR, MAC2STR(s->authorized_macs[j]));
3318 os_memcpy(reg->authorized_macs_union[i],
3319 s->authorized_macs[j], ETH_ALEN);
3322 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union",
3323 (u8 *) reg->authorized_macs_union,
3324 sizeof(reg->authorized_macs_union));
3326 #endif /* CONFIG_WPS_UPNP */
3329 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
3331 #ifdef CONFIG_WPS_UPNP
3332 struct subscription *s;
3334 if (reg->wps->wps_upnp == NULL)
3337 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
3338 struct subscription, list) {
3339 struct subscr_addr *sa;
3340 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
3342 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
3343 inet_ntoa(sa->saddr.sin_addr),
3344 ntohs(sa->saddr.sin_port));
3346 if (s->selected_registrar)
3347 wps_registrar_sel_reg_add(reg, s);
3349 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
3352 #endif /* CONFIG_WPS_UPNP */
3357 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3358 * @reg: Registrar data from wps_registrar_init()
3360 * This function is called when selected registrar state changes, e.g., when an
3361 * AP receives a SetSelectedRegistrar UPnP message.
3363 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg)
3365 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
3367 reg->sel_reg_union = reg->selected_registrar;
3368 reg->sel_reg_dev_password_id_override = -1;
3369 reg->sel_reg_config_methods_override = -1;
3370 os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
3371 WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
3372 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union (start with own)",
3373 (u8 *) reg->authorized_macs_union,
3374 sizeof(reg->authorized_macs_union));
3375 if (reg->selected_registrar) {
3378 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
3380 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
3381 WPS_CONFIG_PHY_PUSHBUTTON);
3382 #endif /* CONFIG_WPS2 */
3384 reg->sel_reg_dev_password_id_override =
3386 wps_set_pushbutton(&methods, reg->wps->config_methods);
3388 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
3389 "(pbc=%d)", reg->pbc);
3390 reg->sel_reg_config_methods_override = methods;
3392 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
3394 wps_registrar_sel_reg_union(reg);
3397 wps_cb_set_sel_reg(reg);
3401 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
3402 char *buf, size_t buflen)
3404 struct wps_registrar_device *d;
3407 char devtype[WPS_DEV_TYPE_BUFSIZE];
3409 d = wps_device_get(reg, addr);
3412 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
3415 ret = os_snprintf(buf + len, buflen - len,
3417 "wpsPrimaryDeviceType=%s\n"
3418 "wpsDeviceName=%s\n"
3419 "wpsManufacturer=%s\n"
3421 "wpsModelNumber=%s\n"
3422 "wpsSerialNumber=%s\n",
3424 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
3426 d->dev.device_name ? d->dev.device_name : "",
3427 d->dev.manufacturer ? d->dev.manufacturer : "",
3428 d->dev.model_name ? d->dev.model_name : "",
3429 d->dev.model_number ? d->dev.model_number : "",
3430 d->dev.serial_number ? d->dev.serial_number : "");
3431 if (ret < 0 || (size_t) ret >= buflen - len)
3439 int wps_registrar_config_ap(struct wps_registrar *reg,
3440 struct wps_credential *cred)
3443 printf("encr_type=0x%x\n", cred->encr_type);
3444 if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
3446 if (cred->encr_type & WPS_ENCR_WEP) {
3447 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
3448 "due to WEP configuration");
3452 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
3453 "invalid encr_type 0x%x", cred->encr_type);
3457 if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
3459 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
3461 cred->encr_type |= WPS_ENCR_AES;
3464 if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
3466 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
3468 cred->auth_type |= WPS_AUTH_WPA2PSK;
3470 #endif /* CONFIG_WPS2 */
3472 if (reg->wps->cred_cb)
3473 return reg->wps->cred_cb(reg->wps->cb_ctx, cred);
3479 #ifdef CONFIG_WPS_NFC
3481 int wps_registrar_add_nfc_pw_token(struct wps_registrar *reg,
3482 const u8 *pubkey_hash, u16 pw_id,
3483 const u8 *dev_pw, size_t dev_pw_len)
3485 struct wps_nfc_pw_token *token;
3487 if (dev_pw_len > WPS_OOB_DEVICE_PASSWORD_LEN)
3490 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, pw_id);
3492 token = os_zalloc(sizeof(*token));
3496 os_memcpy(token->pubkey_hash, pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
3497 token->pw_id = pw_id;
3498 os_memcpy(token->dev_pw, dev_pw, dev_pw_len);
3499 token->dev_pw_len = dev_pw_len;
3501 dl_list_add(®->nfc_pw_tokens, &token->list);
3503 reg->selected_registrar = 1;
3505 wps_registrar_add_authorized_mac(reg,
3506 (u8 *) "\xff\xff\xff\xff\xff\xff");
3507 wps_registrar_selected_registrar_changed(reg);
3508 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
3509 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
3510 wps_registrar_set_selected_timeout,
3517 int wps_registrar_add_nfc_password_token(struct wps_registrar *reg,
3518 const u8 *oob_dev_pw,
3519 size_t oob_dev_pw_len)
3521 const u8 *pos, *hash, *dev_pw;
3525 if (oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2 +
3526 WPS_OOB_DEVICE_PASSWORD_MIN_LEN ||
3527 oob_dev_pw_len > WPS_OOB_PUBKEY_HASH_LEN + 2 +
3528 WPS_OOB_DEVICE_PASSWORD_LEN)
3532 pos = oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN;
3533 id = WPA_GET_BE16(pos);
3535 dev_pw_len = oob_dev_pw + oob_dev_pw_len - dev_pw;
3537 wpa_printf(MSG_DEBUG, "WPS: Add NFC Password Token for Password ID %u",
3540 wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
3541 hash, WPS_OOB_PUBKEY_HASH_LEN);
3542 wpa_hexdump_key(MSG_DEBUG, "WPS: Device Password", dev_pw, dev_pw_len);
3544 return wps_registrar_add_nfc_pw_token(reg, hash, id, dev_pw,
3549 void wps_registrar_remove_nfc_pw_token(struct wps_registrar *reg,
3550 struct wps_nfc_pw_token *token)
3552 wps_registrar_remove_authorized_mac(reg,
3553 (u8 *) "\xff\xff\xff\xff\xff\xff");
3554 wps_registrar_selected_registrar_changed(reg);
3557 #endif /* CONFIG_WPS_NFC */