2 * Wi-Fi Protected Setup - Registrar
3 * Copyright (c) 2008-2016, 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];
34 unsigned int peer_pk_hash_known:1;
36 u8 dev_pw[WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1];
38 int pk_hash_provided_oob; /* whether own PK hash was provided OOB */
42 static void wps_remove_nfc_pw_token(struct wps_nfc_pw_token *token)
44 dl_list_del(&token->list);
45 bin_clear_free(token, sizeof(*token));
49 static void wps_free_nfc_pw_tokens(struct dl_list *tokens, u16 pw_id)
51 struct wps_nfc_pw_token *token, *prev;
52 dl_list_for_each_safe(token, prev, tokens, struct wps_nfc_pw_token,
54 if (pw_id == 0 || pw_id == token->pw_id)
55 wps_remove_nfc_pw_token(token);
60 static struct wps_nfc_pw_token * wps_get_nfc_pw_token(struct dl_list *tokens,
63 struct wps_nfc_pw_token *token;
64 dl_list_for_each(token, tokens, struct wps_nfc_pw_token, list) {
65 if (pw_id == token->pw_id)
71 #else /* CONFIG_WPS_NFC */
73 #define wps_free_nfc_pw_tokens(t, p) do { } while (0)
75 #endif /* CONFIG_WPS_NFC */
80 u8 uuid[WPS_UUID_LEN];
84 #define PIN_LOCKED BIT(0)
85 #define PIN_EXPIRES BIT(1)
87 struct os_reltime expiration;
88 u8 enrollee_addr[ETH_ALEN];
92 static void wps_free_pin(struct wps_uuid_pin *pin)
94 bin_clear_free(pin->pin, pin->pin_len);
99 static void wps_remove_pin(struct wps_uuid_pin *pin)
101 dl_list_del(&pin->list);
106 static void wps_free_pins(struct dl_list *pins)
108 struct wps_uuid_pin *pin, *prev;
109 dl_list_for_each_safe(pin, prev, pins, struct wps_uuid_pin, list)
114 struct wps_pbc_session {
115 struct wps_pbc_session *next;
117 u8 uuid_e[WPS_UUID_LEN];
118 struct os_reltime timestamp;
122 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
124 struct wps_pbc_session *prev;
134 struct wps_registrar_device {
135 struct wps_registrar_device *next;
136 struct wps_device_data dev;
137 u8 uuid[WPS_UUID_LEN];
141 struct wps_registrar {
142 struct wps_context *wps;
145 int selected_registrar;
147 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
148 const u8 *psk, size_t psk_len);
149 int (*set_ie_cb)(void *ctx, struct wpabuf *beacon_ie,
150 struct wpabuf *probe_resp_ie);
151 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
152 const struct wps_device_data *dev);
153 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
154 const u8 *uuid_e, const u8 *dev_pw,
156 void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
157 u16 sel_reg_config_methods);
158 void (*enrollee_seen_cb)(void *ctx, const u8 *addr, const u8 *uuid_e,
159 const u8 *pri_dev_type, u16 config_methods,
160 u16 dev_password_id, u8 request_type,
161 const char *dev_name);
165 struct dl_list nfc_pw_tokens;
166 struct wps_pbc_session *pbc_sessions;
169 struct wpabuf *extra_cred;
170 int disable_auto_conf;
172 int sel_reg_dev_password_id_override;
173 int sel_reg_config_methods_override;
176 int force_per_enrollee_psk;
178 struct wps_registrar_device *devices;
180 int force_pbc_overlap;
182 u8 authorized_macs[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
183 u8 authorized_macs_union[WPS_MAX_AUTHORIZED_MACS][ETH_ALEN];
185 u8 p2p_dev_addr[ETH_ALEN];
187 u8 pbc_ignore_uuid[WPS_UUID_LEN];
188 #ifdef WPS_WORKAROUNDS
189 struct os_reltime pbc_ignore_start;
190 #endif /* WPS_WORKAROUNDS */
194 static int wps_set_ie(struct wps_registrar *reg);
195 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
196 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
198 static void wps_registrar_remove_pin(struct wps_registrar *reg,
199 struct wps_uuid_pin *pin);
202 static void wps_registrar_add_authorized_mac(struct wps_registrar *reg,
206 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC " MACSTR,
208 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
209 if (os_memcmp(reg->authorized_macs[i], addr, ETH_ALEN) == 0) {
210 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was "
211 "already in the list");
212 return; /* already in list */
214 for (i = WPS_MAX_AUTHORIZED_MACS - 1; i > 0; i--)
215 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i - 1],
217 os_memcpy(reg->authorized_macs[0], addr, ETH_ALEN);
218 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
219 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
223 static void wps_registrar_remove_authorized_mac(struct wps_registrar *reg,
227 wpa_printf(MSG_DEBUG, "WPS: Remove authorized MAC " MACSTR,
229 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++) {
230 if (os_memcmp(reg->authorized_macs, addr, ETH_ALEN) == 0)
233 if (i == WPS_MAX_AUTHORIZED_MACS) {
234 wpa_printf(MSG_DEBUG, "WPS: Authorized MAC was not in the "
236 return; /* not in the list */
238 for (; i + 1 < WPS_MAX_AUTHORIZED_MACS; i++)
239 os_memcpy(reg->authorized_macs[i], reg->authorized_macs[i + 1],
241 os_memset(reg->authorized_macs[WPS_MAX_AUTHORIZED_MACS - 1], 0,
243 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs",
244 (u8 *) reg->authorized_macs, sizeof(reg->authorized_macs));
248 static void wps_free_devices(struct wps_registrar_device *dev)
250 struct wps_registrar_device *prev;
255 wps_device_data_free(&prev->dev);
261 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
264 struct wps_registrar_device *dev;
266 for (dev = reg->devices; dev; dev = dev->next) {
267 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
274 static void wps_device_clone_data(struct wps_device_data *dst,
275 struct wps_device_data *src)
277 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
278 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
280 #define WPS_STRDUP(n) \
282 dst->n = src->n ? os_strdup(src->n) : NULL
284 WPS_STRDUP(device_name);
285 WPS_STRDUP(manufacturer);
286 WPS_STRDUP(model_name);
287 WPS_STRDUP(model_number);
288 WPS_STRDUP(serial_number);
293 int wps_device_store(struct wps_registrar *reg,
294 struct wps_device_data *dev, const u8 *uuid)
296 struct wps_registrar_device *d;
298 d = wps_device_get(reg, dev->mac_addr);
300 d = os_zalloc(sizeof(*d));
303 d->next = reg->devices;
307 wps_device_clone_data(&d->dev, dev);
308 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
314 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
315 const u8 *addr, const u8 *uuid_e)
317 struct wps_pbc_session *pbc, *prev = NULL;
318 struct os_reltime now;
320 os_get_reltime(&now);
322 pbc = reg->pbc_sessions;
324 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
325 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
327 prev->next = pbc->next;
329 reg->pbc_sessions = pbc->next;
337 pbc = os_zalloc(sizeof(*pbc));
340 os_memcpy(pbc->addr, addr, ETH_ALEN);
342 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
345 pbc->next = reg->pbc_sessions;
346 reg->pbc_sessions = pbc;
347 pbc->timestamp = now;
349 /* remove entries that have timed out */
354 if (os_reltime_expired(&now, &pbc->timestamp,
355 WPS_PBC_WALK_TIME)) {
357 wps_free_pbc_sessions(pbc);
366 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
368 const u8 *p2p_dev_addr)
370 struct wps_pbc_session *pbc, *prev = NULL, *tmp;
372 pbc = reg->pbc_sessions;
374 if (os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0 ||
375 (p2p_dev_addr && !is_zero_ether_addr(reg->p2p_dev_addr) &&
376 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;
403 struct os_reltime now;
405 os_get_reltime(&now);
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 (os_reltime_expired(&now, &pbc->timestamp,
422 WPS_PBC_WALK_TIME)) {
423 wpa_printf(MSG_DEBUG, "WPS: PBC walk time has expired");
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;
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;
561 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
565 if (!reg->sel_reg_union)
567 methods = reg->wps->config_methods;
568 methods &= ~WPS_CONFIG_PUSHBUTTON;
569 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
570 WPS_CONFIG_PHY_PUSHBUTTON);
572 wps_set_pushbutton(&methods, reg->wps->config_methods);
573 if (reg->sel_reg_config_methods_override >= 0)
574 methods = reg->sel_reg_config_methods_override;
575 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
577 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
578 wpabuf_put_be16(msg, 2);
579 wpabuf_put_be16(msg, methods);
584 static int wps_build_probe_config_methods(struct wps_registrar *reg,
589 * These are the methods that the AP supports as an Enrollee for adding
590 * external Registrars.
592 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
593 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
594 WPS_CONFIG_PHY_PUSHBUTTON);
595 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
596 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
597 wpabuf_put_be16(msg, 2);
598 wpabuf_put_be16(msg, methods);
603 static int wps_build_config_methods_r(struct wps_registrar *reg,
606 return wps_build_config_methods(msg, reg->wps->config_methods);
610 const u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count)
614 while (*count < WPS_MAX_AUTHORIZED_MACS) {
615 if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
620 return (const u8 *) reg->authorized_macs_union;
625 * wps_registrar_init - Initialize WPS Registrar data
626 * @wps: Pointer to longterm WPS context
627 * @cfg: Registrar configuration
628 * Returns: Pointer to allocated Registrar data or %NULL on failure
630 * This function is used to initialize WPS Registrar functionality. It can be
631 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
632 * runs (e.g., when run as an internal Registrar in an AP). Caller is
633 * responsible for freeing the returned data with wps_registrar_deinit() when
634 * Registrar functionality is not needed anymore.
636 struct wps_registrar *
637 wps_registrar_init(struct wps_context *wps,
638 const struct wps_registrar_config *cfg)
640 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
644 dl_list_init(®->pins);
645 dl_list_init(®->nfc_pw_tokens);
647 reg->new_psk_cb = cfg->new_psk_cb;
648 reg->set_ie_cb = cfg->set_ie_cb;
649 reg->pin_needed_cb = cfg->pin_needed_cb;
650 reg->reg_success_cb = cfg->reg_success_cb;
651 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
652 reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
653 reg->cb_ctx = cfg->cb_ctx;
654 reg->skip_cred_build = cfg->skip_cred_build;
655 if (cfg->extra_cred) {
656 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
657 cfg->extra_cred_len);
658 if (reg->extra_cred == NULL) {
663 reg->disable_auto_conf = cfg->disable_auto_conf;
664 reg->sel_reg_dev_password_id_override = -1;
665 reg->sel_reg_config_methods_override = -1;
666 reg->static_wep_only = cfg->static_wep_only;
667 reg->dualband = cfg->dualband;
668 reg->force_per_enrollee_psk = cfg->force_per_enrollee_psk;
670 if (wps_set_ie(reg)) {
671 wps_registrar_deinit(reg);
679 void wps_registrar_flush(struct wps_registrar *reg)
683 wps_free_pins(®->pins);
684 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, 0);
685 wps_free_pbc_sessions(reg->pbc_sessions);
686 reg->pbc_sessions = NULL;
687 wps_free_devices(reg->devices);
689 #ifdef WPS_WORKAROUNDS
690 reg->pbc_ignore_start.sec = 0;
691 #endif /* WPS_WORKAROUNDS */
696 * wps_registrar_deinit - Deinitialize WPS Registrar data
697 * @reg: Registrar data from wps_registrar_init()
699 void wps_registrar_deinit(struct wps_registrar *reg)
703 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
704 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
705 wps_registrar_flush(reg);
706 wpabuf_clear_free(reg->extra_cred);
711 static void wps_registrar_invalidate_unused(struct wps_registrar *reg)
713 struct wps_uuid_pin *pin;
715 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
716 if (pin->wildcard_uuid == 1 && !(pin->flags & PIN_LOCKED)) {
717 wpa_printf(MSG_DEBUG, "WPS: Invalidate previously "
718 "configured wildcard PIN");
719 wps_registrar_remove_pin(reg, pin);
727 * wps_registrar_add_pin - Configure a new PIN for Registrar
728 * @reg: Registrar data from wps_registrar_init()
729 * @addr: Enrollee MAC address or %NULL if not known
730 * @uuid: UUID-E or %NULL for wildcard (any UUID)
731 * @pin: PIN (Device Password)
732 * @pin_len: Length of pin in octets
733 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
734 * Returns: 0 on success, -1 on failure
736 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
737 const u8 *uuid, const u8 *pin, size_t pin_len,
740 struct wps_uuid_pin *p;
742 p = os_zalloc(sizeof(*p));
746 os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
748 p->wildcard_uuid = 1;
750 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
751 p->pin = os_malloc(pin_len);
752 if (p->pin == NULL) {
756 os_memcpy(p->pin, pin, pin_len);
757 p->pin_len = pin_len;
760 p->flags |= PIN_EXPIRES;
761 os_get_reltime(&p->expiration);
762 p->expiration.sec += timeout;
765 if (p->wildcard_uuid)
766 wps_registrar_invalidate_unused(reg);
768 dl_list_add(®->pins, &p->list);
770 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
772 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
773 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
774 reg->selected_registrar = 1;
777 wps_registrar_add_authorized_mac(reg, addr);
779 wps_registrar_add_authorized_mac(
780 reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
781 wps_registrar_selected_registrar_changed(reg, 0);
782 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
783 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
784 wps_registrar_set_selected_timeout,
791 static void wps_registrar_remove_pin(struct wps_registrar *reg,
792 struct wps_uuid_pin *pin)
795 u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
797 if (is_zero_ether_addr(pin->enrollee_addr))
800 addr = pin->enrollee_addr;
801 wps_registrar_remove_authorized_mac(reg, addr);
803 wps_registrar_selected_registrar_changed(reg, 0);
807 static void wps_registrar_expire_pins(struct wps_registrar *reg)
809 struct wps_uuid_pin *pin, *prev;
810 struct os_reltime now;
812 os_get_reltime(&now);
813 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
815 if ((pin->flags & PIN_EXPIRES) &&
816 os_reltime_before(&pin->expiration, &now)) {
817 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
818 pin->uuid, WPS_UUID_LEN);
819 wps_registrar_remove_pin(reg, pin);
826 * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
827 * @reg: Registrar data from wps_registrar_init()
828 * @dev_pw: PIN to search for or %NULL to match any
829 * @dev_pw_len: Length of dev_pw in octets
830 * Returns: 0 on success, -1 if not wildcard PIN is enabled
832 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg,
836 struct wps_uuid_pin *pin, *prev;
838 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
840 if (dev_pw && pin->pin &&
841 (dev_pw_len != pin->pin_len ||
842 os_memcmp_const(dev_pw, pin->pin, dev_pw_len) != 0))
843 continue; /* different PIN */
844 if (pin->wildcard_uuid) {
845 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
846 pin->uuid, WPS_UUID_LEN);
847 wps_registrar_remove_pin(reg, pin);
857 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
858 * @reg: Registrar data from wps_registrar_init()
860 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
862 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
864 struct wps_uuid_pin *pin, *prev;
866 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
868 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
869 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
870 pin->uuid, WPS_UUID_LEN);
871 wps_registrar_remove_pin(reg, pin);
880 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
881 const u8 *uuid, size_t *pin_len)
883 struct wps_uuid_pin *pin, *found = NULL;
885 wps_registrar_expire_pins(reg);
887 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
888 if (!pin->wildcard_uuid &&
889 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
896 /* Check for wildcard UUIDs since none of the UUID-specific
898 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
899 if (pin->wildcard_uuid == 1 ||
900 pin->wildcard_uuid == 2) {
901 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
902 "PIN. Assigned it for this UUID-E");
903 pin->wildcard_uuid++;
904 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
915 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
916 * that could otherwise avoid PIN invalidations.
918 if (found->flags & PIN_LOCKED) {
919 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
920 "allow concurrent re-use");
923 *pin_len = found->pin_len;
924 found->flags |= PIN_LOCKED;
930 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
931 * @reg: Registrar data from wps_registrar_init()
933 * Returns: 0 on success, -1 on failure
935 * PINs are locked to enforce only one concurrent use. This function unlocks a
936 * PIN to allow it to be used again. If the specified PIN was configured using
937 * a wildcard UUID, it will be removed instead of allowing multiple uses.
939 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
941 struct wps_uuid_pin *pin;
943 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
944 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
945 if (pin->wildcard_uuid == 3) {
946 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
948 return wps_registrar_invalidate_pin(reg, uuid);
950 pin->flags &= ~PIN_LOCKED;
959 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
961 reg->selected_registrar = 0;
963 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
964 wps_registrar_remove_authorized_mac(reg,
965 (u8 *) "\xff\xff\xff\xff\xff\xff");
966 wps_registrar_selected_registrar_changed(reg, 0);
970 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
972 struct wps_registrar *reg = eloop_ctx;
974 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
975 wps_pbc_timeout_event(reg->wps);
976 wps_registrar_stop_pbc(reg);
981 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
982 * @reg: Registrar data from wps_registrar_init()
983 * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
984 * indicates no such filtering
985 * Returns: 0 on success, -1 on failure, -2 on session overlap
987 * This function is called on an AP when a push button is pushed to activate
988 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
989 * or when a PBC registration is completed. If more than one Enrollee in active
990 * PBC mode has been detected during the monitor time (previous 2 minutes), the
991 * PBC mode is not activated and -2 is returned to indicate session overlap.
992 * This is skipped if a specific Enrollee is selected.
994 int wps_registrar_button_pushed(struct wps_registrar *reg,
995 const u8 *p2p_dev_addr)
997 if (p2p_dev_addr == NULL &&
998 wps_registrar_pbc_overlap(reg, NULL, NULL)) {
999 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
1001 wps_pbc_overlap_event(reg->wps);
1004 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
1005 reg->force_pbc_overlap = 0;
1006 reg->selected_registrar = 1;
1009 os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
1011 os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
1012 wps_registrar_add_authorized_mac(reg,
1013 (u8 *) "\xff\xff\xff\xff\xff\xff");
1014 wps_registrar_selected_registrar_changed(reg, 0);
1016 wps_pbc_active_event(reg->wps);
1017 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1018 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1019 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
1025 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
1027 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
1028 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1029 wps_registrar_stop_pbc(reg);
1030 wps_pbc_disable_event(reg->wps);
1034 static void wps_registrar_pin_completed(struct wps_registrar *reg)
1036 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
1037 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
1038 reg->selected_registrar = 0;
1039 wps_registrar_selected_registrar_changed(reg, 0);
1043 void wps_registrar_complete(struct wps_registrar *registrar, const u8 *uuid_e,
1044 const u8 *dev_pw, size_t dev_pw_len)
1046 if (registrar->pbc) {
1047 wps_registrar_remove_pbc_session(registrar,
1049 wps_registrar_pbc_completed(registrar);
1050 #ifdef WPS_WORKAROUNDS
1051 os_get_reltime(®istrar->pbc_ignore_start);
1052 #endif /* WPS_WORKAROUNDS */
1053 os_memcpy(registrar->pbc_ignore_uuid, uuid_e, WPS_UUID_LEN);
1055 wps_registrar_pin_completed(registrar);
1059 wps_registrar_invalidate_wildcard_pin(registrar, dev_pw,
1061 wpa_hexdump_key(MSG_DEBUG, "WPS: Invalidated wildcard PIN",
1062 dev_pw, dev_pw_len);
1067 int wps_registrar_wps_cancel(struct wps_registrar *reg)
1070 wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
1071 wps_registrar_pbc_timeout(reg, NULL);
1072 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
1074 } else if (reg->selected_registrar) {
1076 wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
1077 wps_registrar_pin_completed(reg);
1078 wps_registrar_invalidate_wildcard_pin(reg, NULL, 0);
1086 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
1087 * @reg: Registrar data from wps_registrar_init()
1088 * @addr: MAC address of the Probe Request sender
1089 * @wps_data: WPS IE contents
1091 * This function is called on an AP when a Probe Request with WPS IE is
1092 * received. This is used to track PBC mode use and to detect possible overlap
1093 * situation with other WPS APs.
1095 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
1096 const struct wpabuf *wps_data,
1099 struct wps_parse_attr attr;
1102 wpa_hexdump_buf(MSG_MSGDUMP,
1103 "WPS: Probe Request with WPS data received",
1106 if (wps_parse_msg(wps_data, &attr) < 0)
1109 if (attr.config_methods == NULL) {
1110 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
1115 if (attr.dev_password_id == NULL) {
1116 wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
1117 "in Probe Request");
1121 if (reg->enrollee_seen_cb && attr.uuid_e &&
1122 attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
1123 char *dev_name = NULL;
1124 if (attr.dev_name) {
1125 dev_name = os_zalloc(attr.dev_name_len + 1);
1127 os_memcpy(dev_name, attr.dev_name,
1131 reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
1132 attr.primary_dev_type,
1133 WPA_GET_BE16(attr.config_methods),
1134 WPA_GET_BE16(attr.dev_password_id),
1135 *attr.request_type, dev_name);
1139 if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
1140 return; /* Not PBC */
1142 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
1143 MACSTR, MAC2STR(addr));
1144 if (attr.uuid_e == NULL) {
1145 wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
1149 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E from Probe Request", attr.uuid_e,
1152 #ifdef WPS_WORKAROUNDS
1153 if (reg->pbc_ignore_start.sec &&
1154 os_memcmp(attr.uuid_e, reg->pbc_ignore_uuid, WPS_UUID_LEN) == 0) {
1155 struct os_reltime now, dur;
1156 os_get_reltime(&now);
1157 os_reltime_sub(&now, ®->pbc_ignore_start, &dur);
1158 if (dur.sec >= 0 && dur.sec < 5) {
1159 wpa_printf(MSG_DEBUG, "WPS: Ignore PBC activation "
1160 "based on Probe Request from the Enrollee "
1161 "that just completed PBC provisioning");
1164 reg->pbc_ignore_start.sec = 0;
1166 #endif /* WPS_WORKAROUNDS */
1169 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
1170 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
1171 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
1172 reg->force_pbc_overlap = 1;
1173 wps_pbc_overlap_event(reg->wps);
1178 int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
1179 const u8 *p2p_dev_addr, const u8 *psk, size_t psk_len)
1181 if (reg->new_psk_cb == NULL)
1184 return reg->new_psk_cb(reg->cb_ctx, mac_addr, p2p_dev_addr, psk,
1189 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
1190 const struct wps_device_data *dev)
1192 if (reg->pin_needed_cb == NULL)
1195 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
1199 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
1200 const u8 *uuid_e, const u8 *dev_pw,
1203 if (reg->reg_success_cb == NULL)
1206 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e, dev_pw, dev_pw_len);
1210 static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
1211 struct wpabuf *probe_resp_ie)
1213 return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
1217 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
1220 if (reg->set_sel_reg_cb == NULL)
1223 if (reg->selected_registrar) {
1224 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
1225 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
1226 WPS_CONFIG_PHY_PUSHBUTTON);
1228 wps_set_pushbutton(&methods, reg->wps->config_methods);
1231 wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1232 "config_methods=0x%x pbc=%d methods=0x%x",
1233 reg->selected_registrar, reg->wps->config_methods,
1236 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
1237 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
1242 static int wps_set_ie(struct wps_registrar *reg)
1244 struct wpabuf *beacon;
1245 struct wpabuf *probe;
1246 const u8 *auth_macs;
1248 size_t vendor_len = 0;
1251 if (reg->set_ie_cb == NULL)
1254 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
1255 if (reg->wps->dev.vendor_ext[i]) {
1256 vendor_len += 2 + 2;
1257 vendor_len += wpabuf_len(reg->wps->dev.vendor_ext[i]);
1261 beacon = wpabuf_alloc(400 + vendor_len);
1264 probe = wpabuf_alloc(500 + vendor_len);
1265 if (probe == NULL) {
1266 wpabuf_free(beacon);
1270 auth_macs = wps_authorized_macs(reg, &count);
1272 wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
1274 if (wps_build_version(beacon) ||
1275 wps_build_wps_state(reg->wps, beacon) ||
1276 wps_build_ap_setup_locked(reg->wps, beacon) ||
1277 wps_build_selected_registrar(reg, beacon) ||
1278 wps_build_sel_reg_dev_password_id(reg, beacon) ||
1279 wps_build_sel_reg_config_methods(reg, beacon) ||
1280 wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
1281 (reg->dualband && wps_build_rf_bands(®->wps->dev, beacon, 0)) ||
1282 wps_build_wfa_ext(beacon, 0, auth_macs, count) ||
1283 wps_build_vendor_ext(®->wps->dev, beacon)) {
1284 wpabuf_free(beacon);
1290 if (wps_build_dev_name(®->wps->dev, beacon) ||
1291 wps_build_primary_dev_type(®->wps->dev, beacon)) {
1292 wpabuf_free(beacon);
1296 #endif /* CONFIG_P2P */
1298 wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
1300 if (wps_build_version(probe) ||
1301 wps_build_wps_state(reg->wps, probe) ||
1302 wps_build_ap_setup_locked(reg->wps, probe) ||
1303 wps_build_selected_registrar(reg, probe) ||
1304 wps_build_sel_reg_dev_password_id(reg, probe) ||
1305 wps_build_sel_reg_config_methods(reg, probe) ||
1306 wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
1307 WPS_RESP_REGISTRAR) ||
1308 wps_build_uuid_e(probe, reg->wps->uuid) ||
1309 wps_build_device_attrs(®->wps->dev, probe) ||
1310 wps_build_probe_config_methods(reg, probe) ||
1311 (reg->dualband && wps_build_rf_bands(®->wps->dev, probe, 0)) ||
1312 wps_build_wfa_ext(probe, 0, auth_macs, count) ||
1313 wps_build_vendor_ext(®->wps->dev, probe)) {
1314 wpabuf_free(beacon);
1319 beacon = wps_ie_encapsulate(beacon);
1320 probe = wps_ie_encapsulate(probe);
1322 if (!beacon || !probe) {
1323 wpabuf_free(beacon);
1328 if (reg->static_wep_only) {
1330 * Windows XP and Vista clients can get confused about
1331 * EAP-Identity/Request when they probe the network with
1332 * EAPOL-Start. In such a case, they may assume the network is
1333 * using IEEE 802.1X and prompt user for a certificate while
1334 * the correct (non-WPS) behavior would be to ask for the
1335 * static WEP key. As a workaround, use Microsoft Provisioning
1336 * IE to advertise that legacy 802.1X is not supported.
1338 const u8 ms_wps[7] = {
1339 WLAN_EID_VENDOR_SPECIFIC, 5,
1340 /* Microsoft Provisioning IE (00:50:f2:5) */
1341 0x00, 0x50, 0xf2, 5,
1342 0x00 /* no legacy 802.1X or MS WPS */
1344 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
1345 "into Beacon/Probe Response frames");
1346 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
1347 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
1350 return wps_cb_set_ie(reg, beacon, probe);
1354 static int wps_get_dev_password(struct wps_data *wps)
1359 bin_clear_free(wps->dev_password, wps->dev_password_len);
1360 wps->dev_password = NULL;
1363 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
1364 pin = (const u8 *) "00000000";
1366 #ifdef CONFIG_WPS_NFC
1367 } else if (wps->nfc_pw_token) {
1368 if (wps->nfc_pw_token->pw_id == DEV_PW_NFC_CONNECTION_HANDOVER)
1370 wpa_printf(MSG_DEBUG, "WPS: Using NFC connection "
1371 "handover and abbreviated WPS handshake "
1372 "without Device Password");
1375 wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from NFC "
1377 pin = wps->nfc_pw_token->dev_pw;
1378 pin_len = wps->nfc_pw_token->dev_pw_len;
1379 } else if (wps->dev_pw_id >= 0x10 &&
1380 wps->wps->ap_nfc_dev_pw_id == wps->dev_pw_id &&
1381 wps->wps->ap_nfc_dev_pw) {
1382 wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from own NFC Password Token");
1383 pin = wpabuf_head(wps->wps->ap_nfc_dev_pw);
1384 pin_len = wpabuf_len(wps->wps->ap_nfc_dev_pw);
1385 #endif /* CONFIG_WPS_NFC */
1387 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
1389 if (pin && wps->dev_pw_id >= 0x10) {
1390 wpa_printf(MSG_DEBUG, "WPS: No match for OOB Device "
1391 "Password ID, but PIN found");
1393 * See whether Enrollee is willing to use PIN instead.
1395 wps->dev_pw_id = DEV_PW_DEFAULT;
1399 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
1400 "the Enrollee (context %p registrar %p)",
1401 wps->wps, wps->wps->registrar);
1402 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
1407 wps->dev_password = os_malloc(pin_len);
1408 if (wps->dev_password == NULL)
1410 os_memcpy(wps->dev_password, pin, pin_len);
1411 wps->dev_password_len = pin_len;
1417 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
1419 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
1420 wpabuf_put_be16(msg, ATTR_UUID_R);
1421 wpabuf_put_be16(msg, WPS_UUID_LEN);
1422 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1427 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1433 if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1435 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1436 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1437 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1439 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1440 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1441 "R-Hash derivation");
1445 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1446 wpabuf_put_be16(msg, ATTR_R_HASH1);
1447 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1448 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1449 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1450 addr[0] = wps->snonce;
1451 len[0] = WPS_SECRET_NONCE_LEN;
1452 addr[1] = wps->psk1;
1453 len[1] = WPS_PSK_LEN;
1454 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1455 len[2] = wpabuf_len(wps->dh_pubkey_e);
1456 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1457 len[3] = wpabuf_len(wps->dh_pubkey_r);
1458 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1459 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1461 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1462 wpabuf_put_be16(msg, ATTR_R_HASH2);
1463 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1464 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1465 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1466 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1467 addr[1] = wps->psk2;
1468 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1469 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1475 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1477 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1478 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1479 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1480 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1485 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1487 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1488 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1489 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1490 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1491 WPS_SECRET_NONCE_LEN);
1496 static int wps_build_cred_network_idx(struct wpabuf *msg,
1497 const struct wps_credential *cred)
1499 wpa_printf(MSG_DEBUG, "WPS: * Network Index (1)");
1500 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1501 wpabuf_put_be16(msg, 1);
1502 wpabuf_put_u8(msg, 1);
1507 static int wps_build_cred_ssid(struct wpabuf *msg,
1508 const struct wps_credential *cred)
1510 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1511 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID for Credential",
1512 cred->ssid, cred->ssid_len);
1513 wpabuf_put_be16(msg, ATTR_SSID);
1514 wpabuf_put_be16(msg, cred->ssid_len);
1515 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1520 static int wps_build_cred_auth_type(struct wpabuf *msg,
1521 const struct wps_credential *cred)
1523 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1525 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1526 wpabuf_put_be16(msg, 2);
1527 wpabuf_put_be16(msg, cred->auth_type);
1532 static int wps_build_cred_encr_type(struct wpabuf *msg,
1533 const struct wps_credential *cred)
1535 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1537 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1538 wpabuf_put_be16(msg, 2);
1539 wpabuf_put_be16(msg, cred->encr_type);
1544 static int wps_build_cred_network_key(struct wpabuf *msg,
1545 const struct wps_credential *cred)
1547 wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
1548 (int) cred->key_len);
1549 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
1550 cred->key, cred->key_len);
1551 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1552 wpabuf_put_be16(msg, cred->key_len);
1553 wpabuf_put_data(msg, cred->key, cred->key_len);
1558 static int wps_build_credential(struct wpabuf *msg,
1559 const struct wps_credential *cred)
1561 if (wps_build_cred_network_idx(msg, cred) ||
1562 wps_build_cred_ssid(msg, cred) ||
1563 wps_build_cred_auth_type(msg, cred) ||
1564 wps_build_cred_encr_type(msg, cred) ||
1565 wps_build_cred_network_key(msg, cred) ||
1566 wps_build_mac_addr(msg, cred->mac_addr))
1572 int wps_build_credential_wrap(struct wpabuf *msg,
1573 const struct wps_credential *cred)
1575 struct wpabuf *wbuf;
1576 wbuf = wpabuf_alloc(200);
1579 if (wps_build_credential(wbuf, cred)) {
1580 wpabuf_clear_free(wbuf);
1583 wpabuf_put_be16(msg, ATTR_CRED);
1584 wpabuf_put_be16(msg, wpabuf_len(wbuf));
1585 wpabuf_put_buf(msg, wbuf);
1586 wpabuf_clear_free(wbuf);
1591 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1593 struct wpabuf *cred;
1595 if (wps->wps->registrar->skip_cred_build)
1596 goto skip_cred_build;
1598 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1599 if (wps->use_cred) {
1600 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1603 os_memset(&wps->cred, 0, sizeof(wps->cred));
1605 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1606 wps->cred.ssid_len = wps->wps->ssid_len;
1608 /* Select the best authentication and encryption type */
1609 wpa_printf(MSG_DEBUG,
1610 "WPS: Own auth types 0x%x - masked Enrollee auth types 0x%x",
1611 wps->wps->auth_types, wps->auth_type);
1612 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1613 wps->auth_type = WPS_AUTH_WPA2PSK;
1614 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1615 wps->auth_type = WPS_AUTH_WPAPSK;
1616 else if (wps->auth_type & WPS_AUTH_OPEN)
1617 wps->auth_type = WPS_AUTH_OPEN;
1619 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1623 wps->cred.auth_type = wps->auth_type;
1625 wpa_printf(MSG_DEBUG,
1626 "WPS: Own encr types 0x%x (rsn: 0x%x, wpa: 0x%x) - masked Enrollee encr types 0x%x",
1627 wps->wps->encr_types, wps->wps->encr_types_rsn,
1628 wps->wps->encr_types_wpa, wps->encr_type);
1629 if (wps->wps->ap && wps->auth_type == WPS_AUTH_WPA2PSK)
1630 wps->encr_type &= wps->wps->encr_types_rsn;
1631 else if (wps->wps->ap && wps->auth_type == WPS_AUTH_WPAPSK)
1632 wps->encr_type &= wps->wps->encr_types_wpa;
1633 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1634 wps->auth_type == WPS_AUTH_WPAPSK) {
1635 if (wps->encr_type & WPS_ENCR_AES)
1636 wps->encr_type = WPS_ENCR_AES;
1637 else if (wps->encr_type & WPS_ENCR_TKIP)
1638 wps->encr_type = WPS_ENCR_TKIP;
1640 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1641 "type for WPA/WPA2");
1645 if (wps->encr_type & WPS_ENCR_NONE)
1646 wps->encr_type = WPS_ENCR_NONE;
1647 #ifdef CONFIG_TESTING_OPTIONS
1648 else if (wps->encr_type & WPS_ENCR_WEP)
1649 wps->encr_type = WPS_ENCR_WEP;
1650 #endif /* CONFIG_TESTING_OPTIONS */
1652 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1653 "type for non-WPA/WPA2 mode");
1657 wps->cred.encr_type = wps->encr_type;
1659 * Set MAC address in the Credential to be the Enrollee's MAC address
1661 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1663 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1664 !wps->wps->registrar->disable_auto_conf) {
1666 /* Generate a random passphrase */
1667 if (random_pool_ready() != 1 ||
1668 random_get_bytes(r, sizeof(r)) < 0) {
1669 wpa_printf(MSG_INFO,
1670 "WPS: Could not generate random PSK");
1673 os_free(wps->new_psk);
1674 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1675 if (wps->new_psk == NULL)
1677 wps->new_psk_len--; /* remove newline */
1678 while (wps->new_psk_len &&
1679 wps->new_psk[wps->new_psk_len - 1] == '=')
1681 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1682 wps->new_psk, wps->new_psk_len);
1683 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1684 wps->cred.key_len = wps->new_psk_len;
1685 } else if (!wps->wps->registrar->force_per_enrollee_psk &&
1686 wps->use_psk_key && wps->wps->psk_set) {
1688 wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
1689 wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
1690 os_memcpy(wps->cred.key, hex, 32 * 2);
1691 wps->cred.key_len = 32 * 2;
1692 } else if (!wps->wps->registrar->force_per_enrollee_psk &&
1693 wps->wps->network_key) {
1694 os_memcpy(wps->cred.key, wps->wps->network_key,
1695 wps->wps->network_key_len);
1696 wps->cred.key_len = wps->wps->network_key_len;
1697 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1699 /* Generate a random per-device PSK */
1700 os_free(wps->new_psk);
1701 wps->new_psk_len = 32;
1702 wps->new_psk = os_malloc(wps->new_psk_len);
1703 if (wps->new_psk == NULL)
1705 if (random_pool_ready() != 1 ||
1706 random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
1707 wpa_printf(MSG_INFO,
1708 "WPS: Could not generate random PSK");
1709 os_free(wps->new_psk);
1710 wps->new_psk = NULL;
1713 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1714 wps->new_psk, wps->new_psk_len);
1715 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1717 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1718 wps->cred.key_len = wps->new_psk_len * 2;
1722 #ifdef CONFIG_WPS_TESTING
1723 if (wps_testing_dummy_cred)
1724 cred = wpabuf_alloc(200);
1728 struct wps_credential dummy;
1729 wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
1730 os_memset(&dummy, 0, sizeof(dummy));
1731 os_memcpy(dummy.ssid, "dummy", 5);
1733 dummy.auth_type = WPS_AUTH_WPA2PSK;
1734 dummy.encr_type = WPS_ENCR_AES;
1735 os_memcpy(dummy.key, "dummy psk", 9);
1737 os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
1738 wps_build_credential(cred, &dummy);
1739 wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
1741 wpabuf_put_be16(msg, ATTR_CRED);
1742 wpabuf_put_be16(msg, wpabuf_len(cred));
1743 wpabuf_put_buf(msg, cred);
1747 #endif /* CONFIG_WPS_TESTING */
1749 cred = wpabuf_alloc(200);
1753 if (wps_build_credential(cred, &wps->cred)) {
1754 wpabuf_clear_free(cred);
1758 wpabuf_put_be16(msg, ATTR_CRED);
1759 wpabuf_put_be16(msg, wpabuf_len(cred));
1760 wpabuf_put_buf(msg, cred);
1761 wpabuf_clear_free(cred);
1764 if (wps->wps->registrar->extra_cred) {
1765 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1766 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1773 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1775 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1777 if (wps_build_credential(msg, &wps->cred))
1784 static struct wpabuf * wps_build_ap_cred(struct wps_data *wps)
1786 struct wpabuf *msg, *plain;
1788 msg = wpabuf_alloc(1000);
1792 plain = wpabuf_alloc(200);
1793 if (plain == NULL) {
1798 if (wps_build_ap_settings(wps, plain)) {
1799 wpabuf_clear_free(plain);
1804 wpabuf_put_be16(msg, ATTR_CRED);
1805 wpabuf_put_be16(msg, wpabuf_len(plain));
1806 wpabuf_put_buf(msg, plain);
1807 wpabuf_clear_free(plain);
1813 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1816 int config_in_m2 = 0;
1818 if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
1820 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1821 wps->nonce_r, WPS_NONCE_LEN);
1822 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1824 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1825 msg = wpabuf_alloc(1000);
1829 if (wps_build_version(msg) ||
1830 wps_build_msg_type(msg, WPS_M2) ||
1831 wps_build_enrollee_nonce(wps, msg) ||
1832 wps_build_registrar_nonce(wps, msg) ||
1833 wps_build_uuid_r(wps, msg) ||
1834 wps_build_public_key(wps, msg) ||
1835 wps_derive_keys(wps) ||
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->wps->rf_band_cb(wps->wps->cb_ctx)) ||
1843 wps_build_assoc_state(wps, msg) ||
1844 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1845 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1846 wps_build_os_version(&wps->wps->dev, msg) ||
1847 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1852 #ifdef CONFIG_WPS_NFC
1853 if (wps->nfc_pw_token && wps->nfc_pw_token->pk_hash_provided_oob &&
1854 wps->nfc_pw_token->pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
1856 * Use abbreviated handshake since public key hash allowed
1857 * Enrollee to validate our public key similarly to how Enrollee
1858 * public key was validated. There is no need to validate Device
1859 * Password in this case.
1861 struct wpabuf *plain = wpabuf_alloc(500);
1862 if (plain == NULL ||
1863 wps_build_cred(wps, plain) ||
1864 wps_build_key_wrap_auth(wps, plain) ||
1865 wps_build_encr_settings(wps, msg, plain)) {
1867 wpabuf_clear_free(plain);
1870 wpabuf_clear_free(plain);
1873 #endif /* CONFIG_WPS_NFC */
1875 if (wps_build_authenticator(wps, msg)) {
1881 wps->state = config_in_m2 ? RECV_DONE : RECV_M3;
1886 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1889 u16 err = wps->config_error;
1891 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1892 msg = wpabuf_alloc(1000);
1896 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1897 err == WPS_CFG_NO_ERROR)
1898 err = WPS_CFG_SETUP_LOCKED;
1900 if (wps_build_version(msg) ||
1901 wps_build_msg_type(msg, WPS_M2D) ||
1902 wps_build_enrollee_nonce(wps, msg) ||
1903 wps_build_registrar_nonce(wps, msg) ||
1904 wps_build_uuid_r(wps, msg) ||
1905 wps_build_auth_type_flags(wps, msg) ||
1906 wps_build_encr_type_flags(wps, msg) ||
1907 wps_build_conn_type_flags(wps, msg) ||
1908 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1909 wps_build_device_attrs(&wps->wps->dev, msg) ||
1910 wps_build_rf_bands(&wps->wps->dev, msg,
1911 wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
1912 wps_build_assoc_state(wps, msg) ||
1913 wps_build_config_error(msg, err) ||
1914 wps_build_os_version(&wps->wps->dev, msg) ||
1915 wps_build_wfa_ext(msg, 0, NULL, 0)) {
1920 wps->state = RECV_M2D_ACK;
1925 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1927 struct wpabuf *msg, *plain;
1929 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1931 if (wps_derive_psk(wps, wps->dev_password, wps->dev_password_len) < 0)
1934 plain = wpabuf_alloc(200);
1938 msg = wpabuf_alloc(1000);
1944 if (wps_build_version(msg) ||
1945 wps_build_msg_type(msg, WPS_M4) ||
1946 wps_build_enrollee_nonce(wps, msg) ||
1947 wps_build_r_hash(wps, msg) ||
1948 wps_build_r_snonce1(wps, plain) ||
1949 wps_build_key_wrap_auth(wps, plain) ||
1950 wps_build_encr_settings(wps, msg, plain) ||
1951 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1952 wps_build_authenticator(wps, msg)) {
1953 wpabuf_clear_free(plain);
1957 wpabuf_clear_free(plain);
1959 wps->state = RECV_M5;
1964 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1966 struct wpabuf *msg, *plain;
1968 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1970 plain = wpabuf_alloc(200);
1974 msg = wpabuf_alloc(1000);
1980 if (wps_build_version(msg) ||
1981 wps_build_msg_type(msg, WPS_M6) ||
1982 wps_build_enrollee_nonce(wps, msg) ||
1983 wps_build_r_snonce2(wps, plain) ||
1984 wps_build_key_wrap_auth(wps, plain) ||
1985 wps_build_encr_settings(wps, msg, plain) ||
1986 wps_build_wfa_ext(msg, 0, NULL, 0) ||
1987 wps_build_authenticator(wps, msg)) {
1988 wpabuf_clear_free(plain);
1992 wpabuf_clear_free(plain);
1994 wps->wps_pin_revealed = 1;
1995 wps->state = RECV_M7;
2000 static struct wpabuf * wps_build_m8(struct wps_data *wps)
2002 struct wpabuf *msg, *plain;
2004 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
2006 plain = wpabuf_alloc(500);
2010 msg = wpabuf_alloc(1000);
2016 if (wps_build_version(msg) ||
2017 wps_build_msg_type(msg, WPS_M8) ||
2018 wps_build_enrollee_nonce(wps, msg) ||
2019 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
2020 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
2021 wps_build_key_wrap_auth(wps, plain) ||
2022 wps_build_encr_settings(wps, msg, plain) ||
2023 wps_build_wfa_ext(msg, 0, NULL, 0) ||
2024 wps_build_authenticator(wps, msg)) {
2025 wpabuf_clear_free(plain);
2026 wpabuf_clear_free(msg);
2029 wpabuf_clear_free(plain);
2031 wps->state = RECV_DONE;
2036 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
2037 enum wsc_op_code *op_code)
2041 #ifdef CONFIG_WPS_UPNP
2042 if (!wps->int_reg && wps->wps->wps_upnp) {
2043 struct upnp_pending_message *p, *prev = NULL;
2044 if (wps->ext_reg > 1)
2045 wps_registrar_free_pending_m2(wps->wps);
2046 p = wps->wps->upnp_msgs;
2047 /* TODO: check pending message MAC address */
2048 while (p && p->next) {
2053 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
2058 wps->wps->upnp_msgs = NULL;
2065 *op_code = WSC_NACK;
2072 if (wps->ext_reg == 0)
2078 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
2079 "pending message available");
2082 #endif /* CONFIG_WPS_UPNP */
2084 switch (wps->state) {
2086 if (wps_get_dev_password(wps) < 0)
2087 msg = wps_build_m2d(wps);
2089 msg = wps_build_m2(wps);
2093 msg = wps_build_m2d(wps);
2097 msg = wps_build_m4(wps);
2101 msg = wps_build_m6(wps);
2105 msg = wps_build_m8(wps);
2109 msg = wps_build_wsc_ack(wps);
2113 msg = wps_build_wsc_nack(wps);
2114 *op_code = WSC_NACK;
2117 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
2118 "a message", wps->state);
2123 if (*op_code == WSC_MSG && msg) {
2124 /* Save a copy of the last message for Authenticator derivation
2126 wpabuf_free(wps->last_msg);
2127 wps->last_msg = wpabuf_dup(msg);
2134 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
2136 if (e_nonce == NULL) {
2137 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
2141 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
2142 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
2143 wps->nonce_e, WPS_NONCE_LEN);
2149 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
2151 if (r_nonce == NULL) {
2152 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
2156 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
2157 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
2165 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
2167 if (uuid_e == NULL) {
2168 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
2172 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
2173 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
2179 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
2181 if (pw_id == NULL) {
2182 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
2186 wps->dev_pw_id = WPA_GET_BE16(pw_id);
2187 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
2193 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
2195 if (e_hash1 == NULL) {
2196 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
2200 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
2201 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
2207 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
2209 if (e_hash2 == NULL) {
2210 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
2214 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
2215 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
2221 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
2223 u8 hash[SHA256_MAC_LEN];
2227 if (e_snonce1 == NULL) {
2228 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
2232 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
2233 WPS_SECRET_NONCE_LEN);
2235 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
2236 addr[0] = e_snonce1;
2237 len[0] = WPS_SECRET_NONCE_LEN;
2238 addr[1] = wps->psk1;
2239 len[1] = WPS_PSK_LEN;
2240 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2241 len[2] = wpabuf_len(wps->dh_pubkey_e);
2242 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2243 len[3] = wpabuf_len(wps->dh_pubkey_r);
2244 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2246 if (os_memcmp_const(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
2247 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
2248 "not match with the pre-committed value");
2249 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2250 wps_pwd_auth_fail_event(wps->wps, 0, 1, wps->mac_addr_e);
2254 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
2255 "half of the device password");
2261 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
2263 u8 hash[SHA256_MAC_LEN];
2267 if (e_snonce2 == NULL) {
2268 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
2272 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
2273 WPS_SECRET_NONCE_LEN);
2275 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
2276 addr[0] = e_snonce2;
2277 len[0] = WPS_SECRET_NONCE_LEN;
2278 addr[1] = wps->psk2;
2279 len[1] = WPS_PSK_LEN;
2280 addr[2] = wpabuf_head(wps->dh_pubkey_e);
2281 len[2] = wpabuf_len(wps->dh_pubkey_e);
2282 addr[3] = wpabuf_head(wps->dh_pubkey_r);
2283 len[3] = wpabuf_len(wps->dh_pubkey_r);
2284 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
2286 if (os_memcmp_const(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
2287 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
2288 "not match with the pre-committed value");
2289 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2290 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
2291 wps_pwd_auth_fail_event(wps->wps, 0, 2, wps->mac_addr_e);
2295 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
2296 "half of the device password");
2297 wps->wps_pin_revealed = 0;
2298 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
2301 * In case wildcard PIN is used and WPS handshake succeeds in the first
2302 * attempt, wps_registrar_unlock_pin() would not free the PIN, so make
2303 * sure the PIN gets invalidated here.
2305 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
2311 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
2313 if (mac_addr == NULL) {
2314 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
2318 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
2320 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
2321 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
2327 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
2330 if (pk == NULL || pk_len == 0) {
2331 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
2335 wpabuf_free(wps->dh_pubkey_e);
2336 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
2337 if (wps->dh_pubkey_e == NULL)
2344 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
2349 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
2354 auth_types = WPA_GET_BE16(auth);
2356 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
2358 #ifdef WPS_WORKAROUNDS
2360 * Some deployed implementations seem to advertise incorrect information
2361 * in this attribute. A value of 0x1b (WPA2 + WPA + WPAPSK + OPEN, but
2362 * no WPA2PSK) has been reported to be used. Add WPA2PSK to the list to
2363 * avoid issues with building Credentials that do not use the strongest
2364 * actually supported authentication option (that device does support
2365 * WPA2PSK even when it does not claim it here).
2368 (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) ==
2369 (WPS_AUTH_WPA2 | WPS_AUTH_WPAPSK)) {
2370 wpa_printf(MSG_DEBUG,
2371 "WPS: Workaround - assume Enrollee supports WPA2PSK based on claimed WPA2 support");
2372 auth_types |= WPS_AUTH_WPA2PSK;
2374 #endif /* WPS_WORKAROUNDS */
2375 wps->auth_type = wps->wps->auth_types & auth_types;
2376 if (wps->auth_type == 0) {
2377 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2378 "authentication types (own 0x%x Enrollee 0x%x)",
2379 wps->wps->auth_types, auth_types);
2380 #ifdef WPS_WORKAROUNDS
2382 * Some deployed implementations seem to advertise incorrect
2383 * information in this attribute. For example, Linksys WRT350N
2384 * seems to have a byteorder bug that breaks this negotiation.
2385 * In order to interoperate with existing implementations,
2386 * assume that the Enrollee supports everything we do.
2388 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2389 "does not advertise supported authentication types "
2391 wps->auth_type = wps->wps->auth_types;
2392 #else /* WPS_WORKAROUNDS */
2394 #endif /* WPS_WORKAROUNDS */
2401 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
2406 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
2411 encr_types = WPA_GET_BE16(encr);
2413 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
2415 wps->encr_type = wps->wps->encr_types & encr_types;
2416 if (wps->encr_type == 0) {
2417 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
2418 "encryption types (own 0x%x Enrollee 0x%x)",
2419 wps->wps->encr_types, encr_types);
2420 #ifdef WPS_WORKAROUNDS
2422 * Some deployed implementations seem to advertise incorrect
2423 * information in this attribute. For example, Linksys WRT350N
2424 * seems to have a byteorder bug that breaks this negotiation.
2425 * In order to interoperate with existing implementations,
2426 * assume that the Enrollee supports everything we do.
2428 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
2429 "does not advertise supported encryption types "
2431 wps->encr_type = wps->wps->encr_types;
2432 #else /* WPS_WORKAROUNDS */
2434 #endif /* WPS_WORKAROUNDS */
2441 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
2444 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
2449 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
2456 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
2460 if (methods == NULL) {
2461 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
2465 m = WPA_GET_BE16(methods);
2467 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
2468 "%s%s%s%s%s%s%s%s%s", m,
2469 m & WPS_CONFIG_USBA ? " [USBA]" : "",
2470 m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
2471 m & WPS_CONFIG_LABEL ? " [Label]" : "",
2472 m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
2473 m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
2474 m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
2475 m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
2476 m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
2477 m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
2479 if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
2481 * The Enrollee does not have a display so it is unlikely to be
2482 * able to show the passphrase to a user and as such, could
2483 * benefit from receiving PSK to reduce key derivation time.
2485 wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
2486 "Enrollee not supporting display");
2487 wps->use_psk_key = 1;
2494 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
2496 if (state == NULL) {
2497 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
2502 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2509 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
2513 if (assoc == NULL) {
2514 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
2518 a = WPA_GET_BE16(assoc);
2519 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
2525 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
2530 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
2534 e = WPA_GET_BE16(err);
2535 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2541 static int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
2544 struct wps_registrar *reg = wps->wps->registrar;
2546 if (is_zero_ether_addr(reg->p2p_dev_addr))
2547 return 1; /* no filtering in use */
2549 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
2550 wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
2551 "filtering for PBC: expected " MACSTR " was "
2552 MACSTR " - indicate PBC session overlap",
2553 MAC2STR(reg->p2p_dev_addr),
2554 MAC2STR(wps->p2p_dev_addr));
2557 #endif /* CONFIG_P2P */
2562 static int wps_registrar_skip_overlap(struct wps_data *wps)
2565 struct wps_registrar *reg = wps->wps->registrar;
2567 if (is_zero_ether_addr(reg->p2p_dev_addr))
2568 return 0; /* no specific Enrollee selected */
2570 if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) == 0) {
2571 wpa_printf(MSG_DEBUG, "WPS: Skip PBC overlap due to selected "
2575 #endif /* CONFIG_P2P */
2580 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2581 struct wps_parse_attr *attr)
2583 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2585 if (wps->state != RECV_M1) {
2586 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2587 "receiving M1", wps->state);
2591 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2592 wps_process_mac_addr(wps, attr->mac_addr) ||
2593 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2594 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2595 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2596 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2597 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2598 wps_process_config_methods(wps, attr->config_methods) ||
2599 wps_process_wps_state(wps, attr->wps_state) ||
2600 wps_process_device_attrs(&wps->peer_dev, attr) ||
2601 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2602 wps_process_assoc_state(wps, attr->assoc_state) ||
2603 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2604 wps_process_config_error(wps, attr->config_error) ||
2605 wps_process_os_version(&wps->peer_dev, attr->os_version))
2608 if (wps->dev_pw_id < 0x10 &&
2609 wps->dev_pw_id != DEV_PW_DEFAULT &&
2610 wps->dev_pw_id != DEV_PW_P2PS_DEFAULT &&
2611 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2612 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2613 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2614 #ifdef CONFIG_WPS_NFC
2615 wps->dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER &&
2616 #endif /* CONFIG_WPS_NFC */
2617 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2618 !wps->wps->registrar->pbc)) {
2619 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2621 wps->state = SEND_M2D;
2622 return WPS_CONTINUE;
2625 #ifdef CONFIG_WPS_NFC
2626 if (wps->dev_pw_id >= 0x10 ||
2627 wps->dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
2628 struct wps_nfc_pw_token *token;
2630 u8 hash[WPS_HASH_LEN];
2632 wpa_printf(MSG_DEBUG, "WPS: Searching for NFC token match for id=%d (ctx %p registrar %p)",
2633 wps->dev_pw_id, wps->wps, wps->wps->registrar);
2634 token = wps_get_nfc_pw_token(
2635 &wps->wps->registrar->nfc_pw_tokens, wps->dev_pw_id);
2636 if (token && token->peer_pk_hash_known) {
2639 wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
2641 dl_list_del(&token->list);
2642 wps->nfc_pw_token = token;
2644 addr[0] = attr->public_key;
2645 len = attr->public_key_len;
2646 sha256_vector(1, addr, &len, hash);
2647 if (os_memcmp_const(hash,
2648 wps->nfc_pw_token->pubkey_hash,
2649 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
2650 wpa_printf(MSG_ERROR, "WPS: Public Key hash "
2652 wps->state = SEND_M2D;
2654 WPS_CFG_PUBLIC_KEY_HASH_MISMATCH;
2655 return WPS_CONTINUE;
2658 wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
2659 "Password Token (no peer PK hash)");
2660 wps->nfc_pw_token = token;
2661 } else if (wps->dev_pw_id >= 0x10 &&
2662 wps->wps->ap_nfc_dev_pw_id == wps->dev_pw_id &&
2663 wps->wps->ap_nfc_dev_pw) {
2664 wpa_printf(MSG_DEBUG, "WPS: Found match with own NFC Password Token");
2667 #endif /* CONFIG_WPS_NFC */
2669 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2670 if ((wps->wps->registrar->force_pbc_overlap ||
2671 wps_registrar_pbc_overlap(wps->wps->registrar,
2672 wps->mac_addr_e, wps->uuid_e) ||
2673 !wps_registrar_p2p_dev_addr_match(wps)) &&
2674 !wps_registrar_skip_overlap(wps)) {
2675 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2677 wps->state = SEND_M2D;
2678 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2679 wps_pbc_overlap_event(wps->wps);
2680 wps_fail_event(wps->wps, WPS_M1,
2681 WPS_CFG_MULTIPLE_PBC_DETECTED,
2682 WPS_EI_NO_ERROR, wps->mac_addr_e);
2683 wps->wps->registrar->force_pbc_overlap = 1;
2684 return WPS_CONTINUE;
2686 wps_registrar_add_pbc_session(wps->wps->registrar,
2687 wps->mac_addr_e, wps->uuid_e);
2691 #ifdef WPS_WORKAROUNDS
2693 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2694 * passphrase format. To avoid interop issues, force PSK format to be
2697 if (!wps->use_psk_key &&
2698 wps->peer_dev.manufacturer &&
2699 os_strncmp(wps->peer_dev.manufacturer, "Apple ", 6) == 0 &&
2700 wps->peer_dev.model_name &&
2701 os_strcmp(wps->peer_dev.model_name, "AirPort") == 0) {
2702 wpa_printf(MSG_DEBUG, "WPS: Workaround - Force Network Key in "
2704 wps->use_psk_key = 1;
2706 #endif /* WPS_WORKAROUNDS */
2708 wps->state = SEND_M2;
2709 return WPS_CONTINUE;
2713 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2714 const struct wpabuf *msg,
2715 struct wps_parse_attr *attr)
2717 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2719 if (wps->state != RECV_M3) {
2720 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2721 "receiving M3", wps->state);
2722 wps->state = SEND_WSC_NACK;
2723 return WPS_CONTINUE;
2726 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2727 !wps_registrar_skip_overlap(wps)) {
2728 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2730 wps->state = SEND_WSC_NACK;
2731 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2732 return WPS_CONTINUE;
2735 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2736 wps_process_authenticator(wps, attr->authenticator, msg) ||
2737 wps_process_e_hash1(wps, attr->e_hash1) ||
2738 wps_process_e_hash2(wps, attr->e_hash2)) {
2739 wps->state = SEND_WSC_NACK;
2740 return WPS_CONTINUE;
2743 wps->state = SEND_M4;
2744 return WPS_CONTINUE;
2748 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2749 const struct wpabuf *msg,
2750 struct wps_parse_attr *attr)
2752 struct wpabuf *decrypted;
2753 struct wps_parse_attr eattr;
2755 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2757 if (wps->state != RECV_M5) {
2758 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2759 "receiving M5", wps->state);
2760 wps->state = SEND_WSC_NACK;
2761 return WPS_CONTINUE;
2764 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2765 !wps_registrar_skip_overlap(wps)) {
2766 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2768 wps->state = SEND_WSC_NACK;
2769 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2770 return WPS_CONTINUE;
2773 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2774 wps_process_authenticator(wps, attr->authenticator, msg)) {
2775 wps->state = SEND_WSC_NACK;
2776 return WPS_CONTINUE;
2779 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2780 attr->encr_settings_len);
2781 if (decrypted == NULL) {
2782 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2783 "Settings attribute");
2784 wps->state = SEND_WSC_NACK;
2785 return WPS_CONTINUE;
2788 if (wps_validate_m5_encr(decrypted, attr->version2 != NULL) < 0) {
2789 wpabuf_clear_free(decrypted);
2790 wps->state = SEND_WSC_NACK;
2791 return WPS_CONTINUE;
2794 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2796 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2797 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2798 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2799 wpabuf_clear_free(decrypted);
2800 wps->state = SEND_WSC_NACK;
2801 return WPS_CONTINUE;
2803 wpabuf_clear_free(decrypted);
2805 wps->state = SEND_M6;
2806 return WPS_CONTINUE;
2810 static void wps_sta_cred_cb(struct wps_data *wps)
2813 * Update credential to only include a single authentication and
2814 * encryption type in case the AP configuration includes more than one
2817 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2818 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2819 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2820 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2821 if (wps->cred.encr_type & WPS_ENCR_AES)
2822 wps->cred.encr_type = WPS_ENCR_AES;
2823 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2824 wps->cred.encr_type = WPS_ENCR_TKIP;
2825 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2826 "AP configuration");
2827 if (wps->wps->cred_cb)
2828 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2832 static void wps_cred_update(struct wps_credential *dst,
2833 struct wps_credential *src)
2835 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2836 dst->ssid_len = src->ssid_len;
2837 dst->auth_type = src->auth_type;
2838 dst->encr_type = src->encr_type;
2839 dst->key_idx = src->key_idx;
2840 os_memcpy(dst->key, src->key, sizeof(dst->key));
2841 dst->key_len = src->key_len;
2845 static int wps_process_ap_settings_r(struct wps_data *wps,
2846 struct wps_parse_attr *attr)
2850 if (wps->wps->ap || wps->er)
2853 /* AP Settings Attributes in M7 when Enrollee is an AP */
2854 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2857 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2859 if (wps->new_ap_settings) {
2860 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2862 wps_cred_update(&wps->cred, wps->new_ap_settings);
2866 * Use the AP PIN only to receive the current AP settings, not
2867 * to reconfigure the AP.
2871 * Clear selected registrar here since we do not get to
2872 * WSC_Done in this protocol run.
2874 wps_registrar_pin_completed(wps->wps->registrar);
2876 msg = wps_build_ap_cred(wps);
2879 wps->cred.cred_attr = wpabuf_head(msg);
2880 wps->cred.cred_attr_len = wpabuf_len(msg);
2882 if (wps->ap_settings_cb) {
2883 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2888 wps_sta_cred_cb(wps);
2890 wps->cred.cred_attr = NULL;
2891 wps->cred.cred_attr_len = 0;
2899 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2900 const struct wpabuf *msg,
2901 struct wps_parse_attr *attr)
2903 struct wpabuf *decrypted;
2904 struct wps_parse_attr eattr;
2906 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2908 if (wps->state != RECV_M7) {
2909 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2910 "receiving M7", wps->state);
2911 wps->state = SEND_WSC_NACK;
2912 return WPS_CONTINUE;
2915 if (wps->pbc && wps->wps->registrar->force_pbc_overlap &&
2916 !wps_registrar_skip_overlap(wps)) {
2917 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2919 wps->state = SEND_WSC_NACK;
2920 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2921 return WPS_CONTINUE;
2924 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2925 wps_process_authenticator(wps, attr->authenticator, msg)) {
2926 wps->state = SEND_WSC_NACK;
2927 return WPS_CONTINUE;
2930 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2931 attr->encr_settings_len);
2932 if (decrypted == NULL) {
2933 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2934 "Settings attribute");
2935 wps->state = SEND_WSC_NACK;
2936 return WPS_CONTINUE;
2939 if (wps_validate_m7_encr(decrypted, wps->wps->ap || wps->er,
2940 attr->version2 != NULL) < 0) {
2941 wpabuf_clear_free(decrypted);
2942 wps->state = SEND_WSC_NACK;
2943 return WPS_CONTINUE;
2946 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2948 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2949 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2950 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2951 wps_process_ap_settings_r(wps, &eattr)) {
2952 wpabuf_clear_free(decrypted);
2953 wps->state = SEND_WSC_NACK;
2954 return WPS_CONTINUE;
2957 wpabuf_clear_free(decrypted);
2959 wps->state = SEND_M8;
2960 return WPS_CONTINUE;
2964 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2965 const struct wpabuf *msg)
2967 struct wps_parse_attr attr;
2968 enum wps_process_res ret = WPS_CONTINUE;
2970 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2972 if (wps_parse_msg(msg, &attr) < 0)
2975 if (attr.msg_type == NULL) {
2976 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2977 wps->state = SEND_WSC_NACK;
2978 return WPS_CONTINUE;
2981 if (*attr.msg_type != WPS_M1 &&
2982 (attr.registrar_nonce == NULL ||
2983 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2984 WPS_NONCE_LEN) != 0)) {
2985 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2989 switch (*attr.msg_type) {
2991 if (wps_validate_m1(msg) < 0)
2993 #ifdef CONFIG_WPS_UPNP
2994 if (wps->wps->wps_upnp && attr.mac_addr) {
2995 /* Remove old pending messages when starting new run */
2996 wps_free_pending_msgs(wps->wps->upnp_msgs);
2997 wps->wps->upnp_msgs = NULL;
2999 upnp_wps_device_send_wlan_event(
3000 wps->wps->wps_upnp, attr.mac_addr,
3001 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
3003 #endif /* CONFIG_WPS_UPNP */
3004 ret = wps_process_m1(wps, &attr);
3007 if (wps_validate_m3(msg) < 0)
3009 ret = wps_process_m3(wps, msg, &attr);
3010 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
3011 wps_fail_event(wps->wps, WPS_M3, wps->config_error,
3012 wps->error_indication, wps->mac_addr_e);
3015 if (wps_validate_m5(msg) < 0)
3017 ret = wps_process_m5(wps, msg, &attr);
3018 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
3019 wps_fail_event(wps->wps, WPS_M5, wps->config_error,
3020 wps->error_indication, wps->mac_addr_e);
3023 if (wps_validate_m7(msg) < 0)
3025 ret = wps_process_m7(wps, msg, &attr);
3026 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
3027 wps_fail_event(wps->wps, WPS_M7, wps->config_error,
3028 wps->error_indication, wps->mac_addr_e);
3031 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
3036 if (ret == WPS_CONTINUE) {
3037 /* Save a copy of the last message for Authenticator derivation
3039 wpabuf_free(wps->last_msg);
3040 wps->last_msg = wpabuf_dup(msg);
3047 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
3048 const struct wpabuf *msg)
3050 struct wps_parse_attr attr;
3052 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
3054 if (wps_parse_msg(msg, &attr) < 0)
3057 if (attr.msg_type == NULL) {
3058 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3062 if (*attr.msg_type != WPS_WSC_ACK) {
3063 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3068 #ifdef CONFIG_WPS_UPNP
3069 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
3070 upnp_wps_subscribers(wps->wps->wps_upnp)) {
3071 if (wps->wps->upnp_msgs)
3072 return WPS_CONTINUE;
3073 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
3074 "external Registrar");
3077 #endif /* CONFIG_WPS_UPNP */
3079 if (attr.registrar_nonce == NULL ||
3080 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3082 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3086 if (attr.enrollee_nonce == NULL ||
3087 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3088 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3092 if (wps->state == RECV_M2D_ACK) {
3093 #ifdef CONFIG_WPS_UPNP
3094 if (wps->wps->wps_upnp &&
3095 upnp_wps_subscribers(wps->wps->wps_upnp)) {
3096 if (wps->wps->upnp_msgs)
3097 return WPS_CONTINUE;
3098 if (wps->ext_reg == 0)
3100 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
3101 "external Registrar");
3104 #endif /* CONFIG_WPS_UPNP */
3106 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
3107 "terminate negotiation");
3114 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
3115 const struct wpabuf *msg)
3117 struct wps_parse_attr attr;
3121 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
3123 old_state = wps->state;
3124 wps->state = SEND_WSC_NACK;
3126 if (wps_parse_msg(msg, &attr) < 0)
3129 if (attr.msg_type == NULL) {
3130 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3134 if (*attr.msg_type != WPS_WSC_NACK) {
3135 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3140 #ifdef CONFIG_WPS_UPNP
3141 if (wps->wps->wps_upnp && wps->ext_reg) {
3142 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3143 "Registrar terminated by the Enrollee");
3146 #endif /* CONFIG_WPS_UPNP */
3148 if (attr.registrar_nonce == NULL ||
3149 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3151 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3155 if (attr.enrollee_nonce == NULL ||
3156 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3157 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3161 if (attr.config_error == NULL) {
3162 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
3167 config_error = WPA_GET_BE16(attr.config_error);
3168 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
3169 "Configuration Error %d", config_error);
3171 switch (old_state) {
3173 wps_fail_event(wps->wps, WPS_M2, config_error,
3174 wps->error_indication, wps->mac_addr_e);
3177 wps_fail_event(wps->wps, WPS_M4, config_error,
3178 wps->error_indication, wps->mac_addr_e);
3181 wps_fail_event(wps->wps, WPS_M6, config_error,
3182 wps->error_indication, wps->mac_addr_e);
3185 wps_fail_event(wps->wps, WPS_M8, config_error,
3186 wps->error_indication, wps->mac_addr_e);
3196 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
3197 const struct wpabuf *msg)
3199 struct wps_parse_attr attr;
3201 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
3203 if (wps->state != RECV_DONE &&
3204 (!wps->wps->wps_upnp || !wps->ext_reg)) {
3205 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
3206 "receiving WSC_Done", wps->state);
3210 if (wps_parse_msg(msg, &attr) < 0)
3213 if (attr.msg_type == NULL) {
3214 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
3218 if (*attr.msg_type != WPS_WSC_DONE) {
3219 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
3224 #ifdef CONFIG_WPS_UPNP
3225 if (wps->wps->wps_upnp && wps->ext_reg) {
3226 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
3227 "Registrar completed successfully");
3228 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3232 #endif /* CONFIG_WPS_UPNP */
3234 if (attr.registrar_nonce == NULL ||
3235 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
3237 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
3241 if (attr.enrollee_nonce == NULL ||
3242 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
3243 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
3247 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
3248 wps_device_store(wps->wps->registrar, &wps->peer_dev,
3251 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
3252 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
3253 struct wps_credential cred;
3255 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
3256 "on first Enrollee connection");
3258 os_memset(&cred, 0, sizeof(cred));
3259 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
3260 cred.ssid_len = wps->wps->ssid_len;
3261 if (wps->wps->rf_band_cb(wps->wps->cb_ctx) == WPS_RF_60GHZ) {
3262 cred.auth_type = WPS_AUTH_WPA2PSK;
3263 cred.encr_type = WPS_ENCR_AES;
3265 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
3266 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
3268 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
3269 cred.key_len = wps->new_psk_len;
3271 wps->wps->wps_state = WPS_STATE_CONFIGURED;
3272 wpa_hexdump_ascii_key(MSG_DEBUG,
3273 "WPS: Generated random passphrase",
3274 wps->new_psk, wps->new_psk_len);
3275 if (wps->wps->cred_cb)
3276 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
3278 os_free(wps->new_psk);
3279 wps->new_psk = NULL;
3282 if (!wps->wps->ap && !wps->er)
3283 wps_sta_cred_cb(wps);
3286 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
3287 wps->p2p_dev_addr, wps->new_psk,
3288 wps->new_psk_len)) {
3289 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
3292 os_free(wps->new_psk);
3293 wps->new_psk = NULL;
3296 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e,
3297 wps->dev_password, wps->dev_password_len);
3300 wps_registrar_remove_pbc_session(wps->wps->registrar,
3303 wps_registrar_pbc_completed(wps->wps->registrar);
3304 #ifdef WPS_WORKAROUNDS
3305 os_get_reltime(&wps->wps->registrar->pbc_ignore_start);
3306 #endif /* WPS_WORKAROUNDS */
3307 os_memcpy(wps->wps->registrar->pbc_ignore_uuid, wps->uuid_e,
3310 wps_registrar_pin_completed(wps->wps->registrar);
3312 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
3313 * merge them into APs own list.. */
3315 wps_success_event(wps->wps, wps->mac_addr_e);
3321 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
3322 enum wsc_op_code op_code,
3323 const struct wpabuf *msg)
3325 enum wps_process_res ret;
3327 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
3329 (unsigned long) wpabuf_len(msg), op_code);
3331 #ifdef CONFIG_WPS_UPNP
3332 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
3333 struct wps_parse_attr attr;
3334 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
3335 *attr.msg_type == WPS_M3)
3336 wps->ext_reg = 2; /* past M2/M2D phase */
3338 if (wps->ext_reg > 1)
3339 wps_registrar_free_pending_m2(wps->wps);
3340 if (wps->wps->wps_upnp && wps->ext_reg &&
3341 wps->wps->upnp_msgs == NULL &&
3342 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
3344 struct wps_parse_attr attr;
3346 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
3349 type = *attr.msg_type;
3350 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
3351 " to external Registrar for processing", type);
3352 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
3354 UPNP_WPS_WLANEVENT_TYPE_EAP,
3356 if (op_code == WSC_MSG)
3358 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
3359 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
3360 "external Registrar");
3361 return WPS_CONTINUE;
3363 #endif /* CONFIG_WPS_UPNP */
3367 return wps_process_wsc_msg(wps, msg);
3369 if (wps_validate_wsc_ack(msg) < 0)
3371 return wps_process_wsc_ack(wps, msg);
3373 if (wps_validate_wsc_nack(msg) < 0)
3375 return wps_process_wsc_nack(wps, msg);
3377 if (wps_validate_wsc_done(msg) < 0)
3379 ret = wps_process_wsc_done(wps, msg);
3380 if (ret == WPS_FAILURE) {
3381 wps->state = SEND_WSC_NACK;
3382 wps_fail_event(wps->wps, WPS_WSC_DONE,
3384 wps->error_indication, wps->mac_addr_e);
3388 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
3394 int wps_registrar_update_ie(struct wps_registrar *reg)
3396 return wps_set_ie(reg);
3400 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
3403 struct wps_registrar *reg = eloop_ctx;
3405 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
3406 "unselect internal Registrar");
3407 reg->selected_registrar = 0;
3409 wps_registrar_selected_registrar_changed(reg, 0);
3413 #ifdef CONFIG_WPS_UPNP
3414 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
3415 struct subscription *s)
3418 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
3419 "config_methods=0x%x)",
3420 s->dev_password_id, s->config_methods);
3421 reg->sel_reg_union = 1;
3422 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
3423 reg->sel_reg_dev_password_id_override = s->dev_password_id;
3424 if (reg->sel_reg_config_methods_override == -1)
3425 reg->sel_reg_config_methods_override = 0;
3426 reg->sel_reg_config_methods_override |= s->config_methods;
3427 for (i = 0; i < WPS_MAX_AUTHORIZED_MACS; i++)
3428 if (is_zero_ether_addr(reg->authorized_macs_union[i]))
3430 for (j = 0; i < WPS_MAX_AUTHORIZED_MACS && j < WPS_MAX_AUTHORIZED_MACS;
3432 if (is_zero_ether_addr(s->authorized_macs[j]))
3434 wpa_printf(MSG_DEBUG, "WPS: Add authorized MAC into union: "
3435 MACSTR, MAC2STR(s->authorized_macs[j]));
3436 os_memcpy(reg->authorized_macs_union[i],
3437 s->authorized_macs[j], ETH_ALEN);
3440 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union",
3441 (u8 *) reg->authorized_macs_union,
3442 sizeof(reg->authorized_macs_union));
3444 #endif /* CONFIG_WPS_UPNP */
3447 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
3449 #ifdef CONFIG_WPS_UPNP
3450 struct subscription *s;
3452 if (reg->wps->wps_upnp == NULL)
3455 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
3456 struct subscription, list) {
3457 struct subscr_addr *sa;
3458 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
3460 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
3461 inet_ntoa(sa->saddr.sin_addr),
3462 ntohs(sa->saddr.sin_port));
3464 if (s->selected_registrar)
3465 wps_registrar_sel_reg_add(reg, s);
3467 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
3470 #endif /* CONFIG_WPS_UPNP */
3475 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3476 * @reg: Registrar data from wps_registrar_init()
3478 * This function is called when selected registrar state changes, e.g., when an
3479 * AP receives a SetSelectedRegistrar UPnP message.
3481 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg,
3484 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
3486 reg->sel_reg_union = reg->selected_registrar;
3487 reg->sel_reg_dev_password_id_override = -1;
3488 reg->sel_reg_config_methods_override = -1;
3489 os_memcpy(reg->authorized_macs_union, reg->authorized_macs,
3490 WPS_MAX_AUTHORIZED_MACS * ETH_ALEN);
3491 wpa_hexdump(MSG_DEBUG, "WPS: Authorized MACs union (start with own)",
3492 (u8 *) reg->authorized_macs_union,
3493 sizeof(reg->authorized_macs_union));
3494 if (reg->selected_registrar) {
3497 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
3498 methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
3499 WPS_CONFIG_PHY_PUSHBUTTON);
3501 reg->sel_reg_dev_password_id_override =
3503 wps_set_pushbutton(&methods, reg->wps->config_methods);
3504 } else if (dev_pw_id)
3505 reg->sel_reg_dev_password_id_override = dev_pw_id;
3506 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
3507 "(pbc=%d)", reg->pbc);
3508 reg->sel_reg_config_methods_override = methods;
3510 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
3512 wps_registrar_sel_reg_union(reg);
3515 wps_cb_set_sel_reg(reg);
3519 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
3520 char *buf, size_t buflen)
3522 struct wps_registrar_device *d;
3525 char devtype[WPS_DEV_TYPE_BUFSIZE];
3527 d = wps_device_get(reg, addr);
3530 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
3533 ret = os_snprintf(buf + len, buflen - len,
3535 "wpsPrimaryDeviceType=%s\n"
3536 "wpsDeviceName=%s\n"
3537 "wpsManufacturer=%s\n"
3539 "wpsModelNumber=%s\n"
3540 "wpsSerialNumber=%s\n",
3542 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
3544 d->dev.device_name ? d->dev.device_name : "",
3545 d->dev.manufacturer ? d->dev.manufacturer : "",
3546 d->dev.model_name ? d->dev.model_name : "",
3547 d->dev.model_number ? d->dev.model_number : "",
3548 d->dev.serial_number ? d->dev.serial_number : "");
3549 if (os_snprintf_error(buflen - len, ret))
3557 int wps_registrar_config_ap(struct wps_registrar *reg,
3558 struct wps_credential *cred)
3560 wpa_printf(MSG_DEBUG, "WPS: encr_type=0x%x", cred->encr_type);
3561 if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
3563 if (cred->encr_type & WPS_ENCR_WEP) {
3564 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
3565 "due to WEP configuration");
3569 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
3570 "invalid encr_type 0x%x", cred->encr_type);
3574 if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
3576 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
3578 cred->encr_type |= WPS_ENCR_AES;
3581 if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
3583 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
3585 cred->auth_type |= WPS_AUTH_WPA2PSK;
3588 if (reg->wps->cred_cb)
3589 return reg->wps->cred_cb(reg->wps->cb_ctx, cred);
3595 #ifdef CONFIG_WPS_NFC
3597 int wps_registrar_add_nfc_pw_token(struct wps_registrar *reg,
3598 const u8 *pubkey_hash, u16 pw_id,
3599 const u8 *dev_pw, size_t dev_pw_len,
3600 int pk_hash_provided_oob)
3602 struct wps_nfc_pw_token *token;
3604 if (dev_pw_len > WPS_OOB_DEVICE_PASSWORD_LEN)
3607 if (pw_id == DEV_PW_NFC_CONNECTION_HANDOVER &&
3608 (pubkey_hash == NULL || !pk_hash_provided_oob)) {
3609 wpa_printf(MSG_DEBUG, "WPS: Unexpected NFC Password Token "
3610 "addition - missing public key hash");
3614 wps_free_nfc_pw_tokens(®->nfc_pw_tokens, pw_id);
3616 token = os_zalloc(sizeof(*token));
3620 token->peer_pk_hash_known = pubkey_hash != NULL;
3622 os_memcpy(token->pubkey_hash, pubkey_hash,
3623 WPS_OOB_PUBKEY_HASH_LEN);
3624 token->pw_id = pw_id;
3625 token->pk_hash_provided_oob = pk_hash_provided_oob;
3627 wpa_snprintf_hex_uppercase((char *) token->dev_pw,
3628 sizeof(token->dev_pw),
3629 dev_pw, dev_pw_len);
3630 token->dev_pw_len = dev_pw_len * 2;
3633 dl_list_add(®->nfc_pw_tokens, &token->list);
3635 reg->selected_registrar = 1;
3637 wps_registrar_add_authorized_mac(reg,
3638 (u8 *) "\xff\xff\xff\xff\xff\xff");
3639 wps_registrar_selected_registrar_changed(reg, pw_id);
3640 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
3641 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
3642 wps_registrar_set_selected_timeout,
3645 wpa_printf(MSG_DEBUG, "WPS: Added NFC Device Password %u to Registrar",
3652 int wps_registrar_add_nfc_password_token(struct wps_registrar *reg,
3653 const u8 *oob_dev_pw,
3654 size_t oob_dev_pw_len)
3656 const u8 *pos, *hash, *dev_pw;
3660 if (oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2 ||
3661 oob_dev_pw_len > WPS_OOB_PUBKEY_HASH_LEN + 2 +
3662 WPS_OOB_DEVICE_PASSWORD_LEN)
3666 pos = oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN;
3667 id = WPA_GET_BE16(pos);
3669 dev_pw_len = oob_dev_pw + oob_dev_pw_len - dev_pw;
3671 wpa_printf(MSG_DEBUG, "WPS: Add NFC Password Token for Password ID %u",
3674 wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
3675 hash, WPS_OOB_PUBKEY_HASH_LEN);
3676 wpa_hexdump_key(MSG_DEBUG, "WPS: Device Password", dev_pw, dev_pw_len);
3678 return wps_registrar_add_nfc_pw_token(reg, hash, id, dev_pw,
3683 void wps_registrar_remove_nfc_pw_token(struct wps_registrar *reg,
3684 struct wps_nfc_pw_token *token)
3686 wps_registrar_remove_authorized_mac(reg,
3687 (u8 *) "\xff\xff\xff\xff\xff\xff");
3688 wps_registrar_selected_registrar_changed(reg, 0);
3691 * Free the NFC password token if it was used only for a single protocol
3692 * run. The static handover case uses the same password token multiple
3693 * times, so do not free that case here.
3695 if (token->peer_pk_hash_known)
3699 #endif /* CONFIG_WPS_NFC */