2 * Wi-Fi Protected Setup - Registrar
3 * Copyright (c) 2008-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/base64.h"
19 #include "utils/eloop.h"
20 #include "utils/uuid.h"
21 #include "utils/list.h"
22 #include "crypto/crypto.h"
23 #include "crypto/sha256.h"
24 #include "common/ieee802_11_defs.h"
26 #include "wps_dev_attr.h"
28 #include "wps_upnp_i.h"
30 #define WPS_WORKAROUNDS
34 u8 uuid[WPS_UUID_LEN];
38 #define PIN_LOCKED BIT(0)
39 #define PIN_EXPIRES BIT(1)
41 struct os_time expiration;
45 static void wps_free_pin(struct wps_uuid_pin *pin)
52 static void wps_remove_pin(struct wps_uuid_pin *pin)
54 dl_list_del(&pin->list);
59 static void wps_free_pins(struct dl_list *pins)
61 struct wps_uuid_pin *pin, *prev;
62 dl_list_for_each_safe(pin, prev, pins, struct wps_uuid_pin, list)
67 struct wps_pbc_session {
68 struct wps_pbc_session *next;
70 u8 uuid_e[WPS_UUID_LEN];
71 struct os_time timestamp;
75 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
77 struct wps_pbc_session *prev;
87 struct wps_registrar_device {
88 struct wps_registrar_device *next;
89 struct wps_device_data dev;
90 u8 uuid[WPS_UUID_LEN];
94 struct wps_registrar {
95 struct wps_context *wps;
98 int selected_registrar;
100 int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
102 int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
103 const u8 *probe_resp_ie, size_t probe_resp_ie_len);
104 void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
105 const struct wps_device_data *dev);
106 void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
108 void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
109 u16 sel_reg_config_methods);
113 struct wps_pbc_session *pbc_sessions;
116 struct wpabuf *extra_cred;
117 int disable_auto_conf;
119 int sel_reg_dev_password_id_override;
120 int sel_reg_config_methods_override;
123 struct wps_registrar_device *devices;
125 int force_pbc_overlap;
129 static int wps_set_ie(struct wps_registrar *reg);
130 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
131 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
135 static void wps_free_devices(struct wps_registrar_device *dev)
137 struct wps_registrar_device *prev;
142 wps_device_data_free(&prev->dev);
148 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
151 struct wps_registrar_device *dev;
153 for (dev = reg->devices; dev; dev = dev->next) {
154 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
161 static void wps_device_clone_data(struct wps_device_data *dst,
162 struct wps_device_data *src)
164 os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
165 os_memcpy(dst->pri_dev_type, src->pri_dev_type, WPS_DEV_TYPE_LEN);
167 #define WPS_STRDUP(n) \
169 dst->n = src->n ? os_strdup(src->n) : NULL
171 WPS_STRDUP(device_name);
172 WPS_STRDUP(manufacturer);
173 WPS_STRDUP(model_name);
174 WPS_STRDUP(model_number);
175 WPS_STRDUP(serial_number);
180 int wps_device_store(struct wps_registrar *reg,
181 struct wps_device_data *dev, const u8 *uuid)
183 struct wps_registrar_device *d;
185 d = wps_device_get(reg, dev->mac_addr);
187 d = os_zalloc(sizeof(*d));
190 d->next = reg->devices;
194 wps_device_clone_data(&d->dev, dev);
195 os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
201 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
202 const u8 *addr, const u8 *uuid_e)
204 struct wps_pbc_session *pbc, *prev = NULL;
209 pbc = reg->pbc_sessions;
211 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
212 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
214 prev->next = pbc->next;
216 reg->pbc_sessions = pbc->next;
224 pbc = os_zalloc(sizeof(*pbc));
227 os_memcpy(pbc->addr, addr, ETH_ALEN);
229 os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
232 pbc->next = reg->pbc_sessions;
233 reg->pbc_sessions = pbc;
234 pbc->timestamp = now;
236 /* remove entries that have timed out */
241 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
243 wps_free_pbc_sessions(pbc);
252 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
253 const u8 *addr, const u8 *uuid_e)
255 struct wps_pbc_session *pbc, *prev = NULL;
257 pbc = reg->pbc_sessions;
259 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
260 os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
262 prev->next = pbc->next;
264 reg->pbc_sessions = pbc->next;
274 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
275 const u8 *addr, const u8 *uuid_e)
278 struct wps_pbc_session *pbc;
283 for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
284 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
286 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
288 os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
295 return count > 1 ? 1 : 0;
299 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
301 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
303 wpabuf_put_be16(msg, ATTR_WPS_STATE);
304 wpabuf_put_be16(msg, 1);
305 wpabuf_put_u8(msg, wps->wps_state);
310 #ifdef CONFIG_WPS_UPNP
311 static void wps_registrar_free_pending_m2(struct wps_context *wps)
313 struct upnp_pending_message *p, *p2, *prev = NULL;
316 if (p->type == WPS_M2 || p->type == WPS_M2D) {
318 wps->upnp_msgs = p->next;
320 prev->next = p->next;
321 wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
324 wpabuf_free(p2->msg);
332 #endif /* CONFIG_WPS_UPNP */
335 static int wps_build_ap_setup_locked(struct wps_context *wps,
338 if (wps->ap_setup_locked) {
339 wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
340 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
341 wpabuf_put_be16(msg, 1);
342 wpabuf_put_u8(msg, 1);
348 static int wps_build_selected_registrar(struct wps_registrar *reg,
351 if (!reg->sel_reg_union)
353 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
354 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
355 wpabuf_put_be16(msg, 1);
356 wpabuf_put_u8(msg, 1);
361 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
364 u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
365 if (!reg->sel_reg_union)
367 if (reg->sel_reg_dev_password_id_override >= 0)
368 id = reg->sel_reg_dev_password_id_override;
369 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
370 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
371 wpabuf_put_be16(msg, 2);
372 wpabuf_put_be16(msg, id);
377 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
381 if (!reg->sel_reg_union)
383 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
385 methods |= WPS_CONFIG_PUSHBUTTON;
386 if (reg->sel_reg_config_methods_override >= 0)
387 methods = reg->sel_reg_config_methods_override;
388 wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
390 wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
391 wpabuf_put_be16(msg, 2);
392 wpabuf_put_be16(msg, methods);
397 static int wps_build_probe_config_methods(struct wps_registrar *reg,
402 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
403 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
404 wpabuf_put_be16(msg, 2);
405 wpabuf_put_be16(msg, methods);
410 static int wps_build_config_methods_r(struct wps_registrar *reg,
414 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
416 methods |= WPS_CONFIG_PUSHBUTTON;
417 return wps_build_config_methods(msg, methods);
421 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
423 u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
424 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
425 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
426 wpabuf_put_be16(msg, 1);
427 wpabuf_put_u8(msg, resp);
433 * wps_registrar_init - Initialize WPS Registrar data
434 * @wps: Pointer to longterm WPS context
435 * @cfg: Registrar configuration
436 * Returns: Pointer to allocated Registrar data or %NULL on failure
438 * This function is used to initialize WPS Registrar functionality. It can be
439 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
440 * runs (e.g., when run as an internal Registrar in an AP). Caller is
441 * responsible for freeing the returned data with wps_registrar_deinit() when
442 * Registrar functionality is not needed anymore.
444 struct wps_registrar *
445 wps_registrar_init(struct wps_context *wps,
446 const struct wps_registrar_config *cfg)
448 struct wps_registrar *reg = os_zalloc(sizeof(*reg));
452 dl_list_init(®->pins);
454 reg->new_psk_cb = cfg->new_psk_cb;
455 reg->set_ie_cb = cfg->set_ie_cb;
456 reg->pin_needed_cb = cfg->pin_needed_cb;
457 reg->reg_success_cb = cfg->reg_success_cb;
458 reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
459 reg->cb_ctx = cfg->cb_ctx;
460 reg->skip_cred_build = cfg->skip_cred_build;
461 if (cfg->extra_cred) {
462 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
463 cfg->extra_cred_len);
464 if (reg->extra_cred == NULL) {
469 reg->disable_auto_conf = cfg->disable_auto_conf;
470 reg->sel_reg_dev_password_id_override = -1;
471 reg->sel_reg_config_methods_override = -1;
472 reg->static_wep_only = cfg->static_wep_only;
474 if (wps_set_ie(reg)) {
475 wps_registrar_deinit(reg);
484 * wps_registrar_deinit - Deinitialize WPS Registrar data
485 * @reg: Registrar data from wps_registrar_init()
487 void wps_registrar_deinit(struct wps_registrar *reg)
491 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
492 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
493 wps_free_pins(®->pins);
494 wps_free_pbc_sessions(reg->pbc_sessions);
495 wpabuf_free(reg->extra_cred);
496 wps_free_devices(reg->devices);
502 * wps_registrar_add_pin - Configure a new PIN for Registrar
503 * @reg: Registrar data from wps_registrar_init()
504 * @uuid: UUID-E or %NULL for wildcard (any UUID)
505 * @pin: PIN (Device Password)
506 * @pin_len: Length of pin in octets
507 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
508 * Returns: 0 on success, -1 on failure
510 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
511 const u8 *pin, size_t pin_len, int timeout)
513 struct wps_uuid_pin *p;
515 p = os_zalloc(sizeof(*p));
519 p->wildcard_uuid = 1;
521 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
522 p->pin = os_malloc(pin_len);
523 if (p->pin == NULL) {
527 os_memcpy(p->pin, pin, pin_len);
528 p->pin_len = pin_len;
531 p->flags |= PIN_EXPIRES;
532 os_get_time(&p->expiration);
533 p->expiration.sec += timeout;
536 dl_list_add(®->pins, &p->list);
538 wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
540 wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
541 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
542 reg->selected_registrar = 1;
544 wps_registrar_selected_registrar_changed(reg);
545 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
546 eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
547 wps_registrar_set_selected_timeout,
554 static void wps_registrar_expire_pins(struct wps_registrar *reg)
556 struct wps_uuid_pin *pin, *prev;
560 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
562 if ((pin->flags & PIN_EXPIRES) &&
563 os_time_before(&pin->expiration, &now)) {
564 wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
565 pin->uuid, WPS_UUID_LEN);
573 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
574 * @reg: Registrar data from wps_registrar_init()
576 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
578 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
580 struct wps_uuid_pin *pin, *prev;
582 dl_list_for_each_safe(pin, prev, ®->pins, struct wps_uuid_pin, list)
584 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
585 wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
586 pin->uuid, WPS_UUID_LEN);
596 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
597 const u8 *uuid, size_t *pin_len)
599 struct wps_uuid_pin *pin, *found = NULL;
601 wps_registrar_expire_pins(reg);
603 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
604 if (!pin->wildcard_uuid &&
605 os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
612 /* Check for wildcard UUIDs since none of the UUID-specific
614 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
615 if (pin->wildcard_uuid == 1) {
616 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
617 "PIN. Assigned it for this UUID-E");
618 pin->wildcard_uuid = 2;
619 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
630 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
631 * that could otherwise avoid PIN invalidations.
633 if (found->flags & PIN_LOCKED) {
634 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
635 "allow concurrent re-use");
638 *pin_len = found->pin_len;
639 found->flags |= PIN_LOCKED;
645 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
646 * @reg: Registrar data from wps_registrar_init()
648 * Returns: 0 on success, -1 on failure
650 * PINs are locked to enforce only one concurrent use. This function unlocks a
651 * PIN to allow it to be used again. If the specified PIN was configured using
652 * a wildcard UUID, it will be removed instead of allowing multiple uses.
654 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
656 struct wps_uuid_pin *pin;
658 dl_list_for_each(pin, ®->pins, struct wps_uuid_pin, list) {
659 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
660 if (pin->wildcard_uuid == 2) {
661 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
663 return wps_registrar_invalidate_pin(reg, uuid);
665 pin->flags &= ~PIN_LOCKED;
674 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
676 reg->selected_registrar = 0;
678 wps_registrar_selected_registrar_changed(reg);
682 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
684 struct wps_registrar *reg = eloop_ctx;
686 wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
687 wps_pbc_timeout_event(reg->wps);
688 wps_registrar_stop_pbc(reg);
693 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
694 * @reg: Registrar data from wps_registrar_init()
695 * Returns: 0 on success, -1 on failure
697 * This function is called on an AP when a push button is pushed to activate
698 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
699 * or when a PBC registration is completed.
701 int wps_registrar_button_pushed(struct wps_registrar *reg)
703 if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
704 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
706 wps_pbc_overlap_event(reg->wps);
709 wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
710 reg->force_pbc_overlap = 0;
711 reg->selected_registrar = 1;
713 wps_registrar_selected_registrar_changed(reg);
715 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
716 eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
722 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
724 wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
725 eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
726 wps_registrar_stop_pbc(reg);
730 static void wps_registrar_pin_completed(struct wps_registrar *reg)
732 wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
733 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
734 reg->selected_registrar = 0;
735 wps_registrar_selected_registrar_changed(reg);
740 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
741 * @reg: Registrar data from wps_registrar_init()
742 * @addr: MAC address of the Probe Request sender
743 * @wps_data: WPS IE contents
745 * This function is called on an AP when a Probe Request with WPS IE is
746 * received. This is used to track PBC mode use and to detect possible overlap
747 * situation with other WPS APs.
749 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
750 const struct wpabuf *wps_data)
752 struct wps_parse_attr attr;
755 wpa_hexdump_buf(MSG_MSGDUMP,
756 "WPS: Probe Request with WPS data received",
759 if (wps_parse_msg(wps_data, &attr) < 0)
761 if (!wps_version_supported(attr.version)) {
762 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
763 "version 0x%x", attr.version ? *attr.version : 0);
767 if (attr.config_methods == NULL) {
768 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
773 methods = WPA_GET_BE16(attr.config_methods);
774 if (!(methods & WPS_CONFIG_PUSHBUTTON))
775 return; /* Not PBC */
777 wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
778 MACSTR, MAC2STR(addr));
780 wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
781 if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
782 wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
783 reg->force_pbc_overlap = 1;
784 wps_pbc_overlap_event(reg->wps);
789 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
790 const u8 *psk, size_t psk_len)
792 if (reg->new_psk_cb == NULL)
795 return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
799 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
800 const struct wps_device_data *dev)
802 if (reg->pin_needed_cb == NULL)
805 reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
809 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
812 if (reg->reg_success_cb == NULL)
815 reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
819 static int wps_cb_set_ie(struct wps_registrar *reg,
820 const struct wpabuf *beacon_ie,
821 const struct wpabuf *probe_resp_ie)
823 if (reg->set_ie_cb == NULL)
826 return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
827 wpabuf_len(beacon_ie),
828 wpabuf_head(probe_resp_ie),
829 wpabuf_len(probe_resp_ie));
833 static void wps_cb_set_sel_reg(struct wps_registrar *reg)
836 if (reg->set_sel_reg_cb == NULL)
839 if (reg->selected_registrar) {
840 methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
842 methods |= WPS_CONFIG_PUSHBUTTON;
845 reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
846 reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
851 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
852 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
857 ie = wpabuf_alloc(wpabuf_len(data) + 100);
863 pos = wpabuf_head(data);
864 end = pos + wpabuf_len(data);
867 size_t frag_len = end - pos;
870 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
871 wpabuf_put_u8(ie, 4 + frag_len);
872 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
873 wpabuf_put_data(ie, pos, frag_len);
883 static int wps_set_ie(struct wps_registrar *reg)
885 struct wpabuf *beacon;
886 struct wpabuf *probe;
889 wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
891 beacon = wpabuf_alloc(300);
894 probe = wpabuf_alloc(400);
900 if (wps_build_version(beacon) ||
901 wps_build_wps_state(reg->wps, beacon) ||
902 wps_build_ap_setup_locked(reg->wps, beacon) ||
903 wps_build_selected_registrar(reg, beacon) ||
904 wps_build_sel_reg_dev_password_id(reg, beacon) ||
905 wps_build_sel_reg_config_methods(reg, beacon) ||
906 wps_build_version(probe) ||
907 wps_build_wps_state(reg->wps, probe) ||
908 wps_build_ap_setup_locked(reg->wps, probe) ||
909 wps_build_selected_registrar(reg, probe) ||
910 wps_build_sel_reg_dev_password_id(reg, probe) ||
911 wps_build_sel_reg_config_methods(reg, probe) ||
912 wps_build_resp_type(reg, probe) ||
913 wps_build_uuid_e(probe, reg->wps->uuid) ||
914 wps_build_device_attrs(®->wps->dev, probe) ||
915 wps_build_probe_config_methods(reg, probe) ||
916 wps_build_rf_bands(®->wps->dev, probe)) {
922 beacon = wps_ie_encapsulate(beacon);
923 probe = wps_ie_encapsulate(probe);
925 if (!beacon || !probe) {
931 if (reg->static_wep_only) {
933 * Windows XP and Vista clients can get confused about
934 * EAP-Identity/Request when they probe the network with
935 * EAPOL-Start. In such a case, they may assume the network is
936 * using IEEE 802.1X and prompt user for a certificate while
937 * the correct (non-WPS) behavior would be to ask for the
938 * static WEP key. As a workaround, use Microsoft Provisioning
939 * IE to advertise that legacy 802.1X is not supported.
941 const u8 ms_wps[7] = {
942 WLAN_EID_VENDOR_SPECIFIC, 5,
943 /* Microsoft Provisioning IE (00:50:f2:5) */
945 0x00 /* no legacy 802.1X or MS WPS */
947 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
948 "into Beacon/Probe Response frames");
949 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
950 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
953 ret = wps_cb_set_ie(reg, beacon, probe);
961 static int wps_get_dev_password(struct wps_data *wps)
966 os_free(wps->dev_password);
967 wps->dev_password = NULL;
970 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
971 pin = (const u8 *) "00000000";
974 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
978 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
980 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
985 wps->dev_password = os_malloc(pin_len);
986 if (wps->dev_password == NULL)
988 os_memcpy(wps->dev_password, pin, pin_len);
989 wps->dev_password_len = pin_len;
995 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
997 wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
998 wpabuf_put_be16(msg, ATTR_UUID_R);
999 wpabuf_put_be16(msg, WPS_UUID_LEN);
1000 wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
1005 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
1011 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
1013 wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
1014 wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
1015 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
1017 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
1018 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
1019 "R-Hash derivation");
1023 wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
1024 wpabuf_put_be16(msg, ATTR_R_HASH1);
1025 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1026 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1027 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1028 addr[0] = wps->snonce;
1029 len[0] = WPS_SECRET_NONCE_LEN;
1030 addr[1] = wps->psk1;
1031 len[1] = WPS_PSK_LEN;
1032 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1033 len[2] = wpabuf_len(wps->dh_pubkey_e);
1034 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1035 len[3] = wpabuf_len(wps->dh_pubkey_r);
1036 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1037 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1039 wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
1040 wpabuf_put_be16(msg, ATTR_R_HASH2);
1041 wpabuf_put_be16(msg, SHA256_MAC_LEN);
1042 hash = wpabuf_put(msg, SHA256_MAC_LEN);
1043 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1044 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1045 addr[1] = wps->psk2;
1046 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1047 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1053 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1055 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
1056 wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1057 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1058 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1063 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1065 wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
1066 wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1067 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1068 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1069 WPS_SECRET_NONCE_LEN);
1074 static int wps_build_cred_network_idx(struct wpabuf *msg,
1075 const struct wps_credential *cred)
1077 wpa_printf(MSG_DEBUG, "WPS: * Network Index");
1078 wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1079 wpabuf_put_be16(msg, 1);
1080 wpabuf_put_u8(msg, 1);
1085 static int wps_build_cred_ssid(struct wpabuf *msg,
1086 const struct wps_credential *cred)
1088 wpa_printf(MSG_DEBUG, "WPS: * SSID");
1089 wpabuf_put_be16(msg, ATTR_SSID);
1090 wpabuf_put_be16(msg, cred->ssid_len);
1091 wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1096 static int wps_build_cred_auth_type(struct wpabuf *msg,
1097 const struct wps_credential *cred)
1099 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
1101 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1102 wpabuf_put_be16(msg, 2);
1103 wpabuf_put_be16(msg, cred->auth_type);
1108 static int wps_build_cred_encr_type(struct wpabuf *msg,
1109 const struct wps_credential *cred)
1111 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
1113 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1114 wpabuf_put_be16(msg, 2);
1115 wpabuf_put_be16(msg, cred->encr_type);
1120 static int wps_build_cred_network_key(struct wpabuf *msg,
1121 const struct wps_credential *cred)
1123 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
1124 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1125 wpabuf_put_be16(msg, cred->key_len);
1126 wpabuf_put_data(msg, cred->key, cred->key_len);
1131 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1132 const struct wps_credential *cred)
1134 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
1135 MAC2STR(cred->mac_addr));
1136 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1137 wpabuf_put_be16(msg, ETH_ALEN);
1138 wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1143 static int wps_build_credential(struct wpabuf *msg,
1144 const struct wps_credential *cred)
1146 if (wps_build_cred_network_idx(msg, cred) ||
1147 wps_build_cred_ssid(msg, cred) ||
1148 wps_build_cred_auth_type(msg, cred) ||
1149 wps_build_cred_encr_type(msg, cred) ||
1150 wps_build_cred_network_key(msg, cred) ||
1151 wps_build_cred_mac_addr(msg, cred))
1157 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1159 struct wpabuf *cred;
1161 if (wps->wps->registrar->skip_cred_build)
1162 goto skip_cred_build;
1164 wpa_printf(MSG_DEBUG, "WPS: * Credential");
1165 if (wps->use_cred) {
1166 os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
1169 os_memset(&wps->cred, 0, sizeof(wps->cred));
1171 os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1172 wps->cred.ssid_len = wps->wps->ssid_len;
1174 /* Select the best authentication and encryption type */
1175 if (wps->auth_type & WPS_AUTH_WPA2PSK)
1176 wps->auth_type = WPS_AUTH_WPA2PSK;
1177 else if (wps->auth_type & WPS_AUTH_WPAPSK)
1178 wps->auth_type = WPS_AUTH_WPAPSK;
1179 else if (wps->auth_type & WPS_AUTH_OPEN)
1180 wps->auth_type = WPS_AUTH_OPEN;
1181 else if (wps->auth_type & WPS_AUTH_SHARED)
1182 wps->auth_type = WPS_AUTH_SHARED;
1184 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1188 wps->cred.auth_type = wps->auth_type;
1190 if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1191 wps->auth_type == WPS_AUTH_WPAPSK) {
1192 if (wps->encr_type & WPS_ENCR_AES)
1193 wps->encr_type = WPS_ENCR_AES;
1194 else if (wps->encr_type & WPS_ENCR_TKIP)
1195 wps->encr_type = WPS_ENCR_TKIP;
1197 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1198 "type for WPA/WPA2");
1202 if (wps->encr_type & WPS_ENCR_WEP)
1203 wps->encr_type = WPS_ENCR_WEP;
1204 else if (wps->encr_type & WPS_ENCR_NONE)
1205 wps->encr_type = WPS_ENCR_NONE;
1207 wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1208 "type for non-WPA/WPA2 mode");
1212 wps->cred.encr_type = wps->encr_type;
1214 * Set MAC address in the Credential to be the Enrollee's MAC address
1216 os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
1218 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1219 !wps->wps->registrar->disable_auto_conf) {
1221 /* Generate a random passphrase */
1222 if (os_get_random(r, sizeof(r)) < 0)
1224 os_free(wps->new_psk);
1225 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1226 if (wps->new_psk == NULL)
1228 wps->new_psk_len--; /* remove newline */
1229 while (wps->new_psk_len &&
1230 wps->new_psk[wps->new_psk_len - 1] == '=')
1232 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1233 wps->new_psk, wps->new_psk_len);
1234 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1235 wps->cred.key_len = wps->new_psk_len;
1236 } else if (wps->wps->network_key) {
1237 os_memcpy(wps->cred.key, wps->wps->network_key,
1238 wps->wps->network_key_len);
1239 wps->cred.key_len = wps->wps->network_key_len;
1240 } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1242 /* Generate a random per-device PSK */
1243 os_free(wps->new_psk);
1244 wps->new_psk_len = 32;
1245 wps->new_psk = os_malloc(wps->new_psk_len);
1246 if (wps->new_psk == NULL)
1248 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1249 os_free(wps->new_psk);
1250 wps->new_psk = NULL;
1253 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1254 wps->new_psk, wps->new_psk_len);
1255 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1257 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1258 wps->cred.key_len = wps->new_psk_len * 2;
1262 cred = wpabuf_alloc(200);
1266 if (wps_build_credential(cred, &wps->cred)) {
1271 wpabuf_put_be16(msg, ATTR_CRED);
1272 wpabuf_put_be16(msg, wpabuf_len(cred));
1273 wpabuf_put_buf(msg, cred);
1277 if (wps->wps->registrar->extra_cred) {
1278 wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
1279 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1286 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1288 wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
1290 if (wps_build_credential(msg, &wps->cred))
1297 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1301 if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1303 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1304 wps->nonce_r, WPS_NONCE_LEN);
1305 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1307 wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1308 msg = wpabuf_alloc(1000);
1312 if (wps_build_version(msg) ||
1313 wps_build_msg_type(msg, WPS_M2) ||
1314 wps_build_enrollee_nonce(wps, msg) ||
1315 wps_build_registrar_nonce(wps, msg) ||
1316 wps_build_uuid_r(wps, msg) ||
1317 wps_build_public_key(wps, msg) ||
1318 wps_derive_keys(wps) ||
1319 wps_build_auth_type_flags(wps, msg) ||
1320 wps_build_encr_type_flags(wps, msg) ||
1321 wps_build_conn_type_flags(wps, msg) ||
1322 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1323 wps_build_device_attrs(&wps->wps->dev, msg) ||
1324 wps_build_rf_bands(&wps->wps->dev, msg) ||
1325 wps_build_assoc_state(wps, msg) ||
1326 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1327 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1328 wps_build_os_version(&wps->wps->dev, msg) ||
1329 wps_build_authenticator(wps, msg)) {
1334 wps->state = RECV_M3;
1339 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1342 u16 err = wps->config_error;
1344 wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1345 msg = wpabuf_alloc(1000);
1349 if (wps->wps->ap && wps->wps->ap_setup_locked &&
1350 err == WPS_CFG_NO_ERROR)
1351 err = WPS_CFG_SETUP_LOCKED;
1353 if (wps_build_version(msg) ||
1354 wps_build_msg_type(msg, WPS_M2D) ||
1355 wps_build_enrollee_nonce(wps, msg) ||
1356 wps_build_registrar_nonce(wps, msg) ||
1357 wps_build_uuid_r(wps, msg) ||
1358 wps_build_auth_type_flags(wps, msg) ||
1359 wps_build_encr_type_flags(wps, msg) ||
1360 wps_build_conn_type_flags(wps, msg) ||
1361 wps_build_config_methods_r(wps->wps->registrar, msg) ||
1362 wps_build_device_attrs(&wps->wps->dev, msg) ||
1363 wps_build_rf_bands(&wps->wps->dev, msg) ||
1364 wps_build_assoc_state(wps, msg) ||
1365 wps_build_config_error(msg, err) ||
1366 wps_build_os_version(&wps->wps->dev, msg)) {
1371 wps->state = RECV_M2D_ACK;
1376 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1378 struct wpabuf *msg, *plain;
1380 wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1382 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1384 plain = wpabuf_alloc(200);
1388 msg = wpabuf_alloc(1000);
1394 if (wps_build_version(msg) ||
1395 wps_build_msg_type(msg, WPS_M4) ||
1396 wps_build_enrollee_nonce(wps, msg) ||
1397 wps_build_r_hash(wps, msg) ||
1398 wps_build_r_snonce1(wps, plain) ||
1399 wps_build_key_wrap_auth(wps, plain) ||
1400 wps_build_encr_settings(wps, msg, plain) ||
1401 wps_build_authenticator(wps, msg)) {
1408 wps->state = RECV_M5;
1413 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1415 struct wpabuf *msg, *plain;
1417 wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1419 plain = wpabuf_alloc(200);
1423 msg = wpabuf_alloc(1000);
1429 if (wps_build_version(msg) ||
1430 wps_build_msg_type(msg, WPS_M6) ||
1431 wps_build_enrollee_nonce(wps, msg) ||
1432 wps_build_r_snonce2(wps, plain) ||
1433 wps_build_key_wrap_auth(wps, plain) ||
1434 wps_build_encr_settings(wps, msg, plain) ||
1435 wps_build_authenticator(wps, msg)) {
1442 wps->wps_pin_revealed = 1;
1443 wps->state = RECV_M7;
1448 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1450 struct wpabuf *msg, *plain;
1452 wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1454 plain = wpabuf_alloc(500);
1458 msg = wpabuf_alloc(1000);
1464 if (wps_build_version(msg) ||
1465 wps_build_msg_type(msg, WPS_M8) ||
1466 wps_build_enrollee_nonce(wps, msg) ||
1467 ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
1468 (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
1469 wps_build_key_wrap_auth(wps, plain) ||
1470 wps_build_encr_settings(wps, msg, plain) ||
1471 wps_build_authenticator(wps, msg)) {
1478 wps->state = RECV_DONE;
1483 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1487 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1489 msg = wpabuf_alloc(1000);
1493 if (wps_build_version(msg) ||
1494 wps_build_msg_type(msg, WPS_WSC_ACK) ||
1495 wps_build_enrollee_nonce(wps, msg) ||
1496 wps_build_registrar_nonce(wps, msg)) {
1505 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1509 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1511 msg = wpabuf_alloc(1000);
1515 if (wps_build_version(msg) ||
1516 wps_build_msg_type(msg, WPS_WSC_NACK) ||
1517 wps_build_enrollee_nonce(wps, msg) ||
1518 wps_build_registrar_nonce(wps, msg) ||
1519 wps_build_config_error(msg, wps->config_error)) {
1528 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1529 enum wsc_op_code *op_code)
1533 #ifdef CONFIG_WPS_UPNP
1534 if (wps->wps->wps_upnp) {
1535 struct upnp_pending_message *p, *prev = NULL;
1536 if (wps->ext_reg > 1)
1537 wps_registrar_free_pending_m2(wps->wps);
1538 p = wps->wps->upnp_msgs;
1539 /* TODO: check pending message MAC address */
1540 while (p && p->next) {
1545 wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1550 wps->wps->upnp_msgs = NULL;
1557 *op_code = WSC_NACK;
1564 if (wps->ext_reg == 0)
1570 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1571 "pending message available");
1574 #endif /* CONFIG_WPS_UPNP */
1576 switch (wps->state) {
1578 if (wps_get_dev_password(wps) < 0)
1579 msg = wps_build_m2d(wps);
1581 msg = wps_build_m2(wps);
1585 msg = wps_build_m2d(wps);
1589 msg = wps_build_m4(wps);
1593 msg = wps_build_m6(wps);
1597 msg = wps_build_m8(wps);
1601 msg = wps_build_wsc_ack(wps);
1605 msg = wps_build_wsc_nack(wps);
1606 *op_code = WSC_NACK;
1609 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1610 "a message", wps->state);
1615 if (*op_code == WSC_MSG && msg) {
1616 /* Save a copy of the last message for Authenticator derivation
1618 wpabuf_free(wps->last_msg);
1619 wps->last_msg = wpabuf_dup(msg);
1626 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1628 if (e_nonce == NULL) {
1629 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1633 os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1634 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1635 wps->nonce_e, WPS_NONCE_LEN);
1641 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1643 if (r_nonce == NULL) {
1644 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1648 if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1649 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1657 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1659 if (uuid_e == NULL) {
1660 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1664 os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1665 wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1671 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1673 if (pw_id == NULL) {
1674 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1678 wps->dev_pw_id = WPA_GET_BE16(pw_id);
1679 wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1685 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1687 if (e_hash1 == NULL) {
1688 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1692 os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1693 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1699 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1701 if (e_hash2 == NULL) {
1702 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1706 os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1707 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1713 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1715 u8 hash[SHA256_MAC_LEN];
1719 if (e_snonce1 == NULL) {
1720 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1724 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1725 WPS_SECRET_NONCE_LEN);
1727 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1728 addr[0] = e_snonce1;
1729 len[0] = WPS_SECRET_NONCE_LEN;
1730 addr[1] = wps->psk1;
1731 len[1] = WPS_PSK_LEN;
1732 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1733 len[2] = wpabuf_len(wps->dh_pubkey_e);
1734 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1735 len[3] = wpabuf_len(wps->dh_pubkey_r);
1736 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1738 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1739 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1740 "not match with the pre-committed value");
1741 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1742 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1746 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1747 "half of the device password");
1753 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1755 u8 hash[SHA256_MAC_LEN];
1759 if (e_snonce2 == NULL) {
1760 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1764 wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1765 WPS_SECRET_NONCE_LEN);
1767 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1768 addr[0] = e_snonce2;
1769 len[0] = WPS_SECRET_NONCE_LEN;
1770 addr[1] = wps->psk2;
1771 len[1] = WPS_PSK_LEN;
1772 addr[2] = wpabuf_head(wps->dh_pubkey_e);
1773 len[2] = wpabuf_len(wps->dh_pubkey_e);
1774 addr[3] = wpabuf_head(wps->dh_pubkey_r);
1775 len[3] = wpabuf_len(wps->dh_pubkey_r);
1776 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1778 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1779 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1780 "not match with the pre-committed value");
1781 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1782 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1783 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1787 wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1788 "half of the device password");
1789 wps->wps_pin_revealed = 0;
1790 wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1796 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1798 if (mac_addr == NULL) {
1799 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1803 wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1805 os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1806 os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1812 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1815 if (pk == NULL || pk_len == 0) {
1816 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1820 #ifdef CONFIG_WPS_OOB
1821 if (wps->wps->oob_conf.pubkey_hash != NULL) {
1823 u8 hash[WPS_HASH_LEN];
1826 sha256_vector(1, addr, &pk_len, hash);
1828 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
1829 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
1830 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
1834 #endif /* CONFIG_WPS_OOB */
1836 wpabuf_free(wps->dh_pubkey_e);
1837 wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1838 if (wps->dh_pubkey_e == NULL)
1845 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1850 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1855 auth_types = WPA_GET_BE16(auth);
1857 wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1859 wps->auth_type = wps->wps->auth_types & auth_types;
1860 if (wps->auth_type == 0) {
1861 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1862 "authentication types (own 0x%x Enrollee 0x%x)",
1863 wps->wps->auth_types, auth_types);
1864 #ifdef WPS_WORKAROUNDS
1866 * Some deployed implementations seem to advertise incorrect
1867 * information in this attribute. For example, Linksys WRT350N
1868 * seems to have a byteorder bug that breaks this negotiation.
1869 * In order to interoperate with existing implementations,
1870 * assume that the Enrollee supports everything we do.
1872 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1873 "does not advertise supported authentication types "
1875 wps->auth_type = wps->wps->auth_types;
1876 #else /* WPS_WORKAROUNDS */
1878 #endif /* WPS_WORKAROUNDS */
1885 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1890 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1895 encr_types = WPA_GET_BE16(encr);
1897 wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1899 wps->encr_type = wps->wps->encr_types & encr_types;
1900 if (wps->encr_type == 0) {
1901 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1902 "encryption types (own 0x%x Enrollee 0x%x)",
1903 wps->wps->encr_types, encr_types);
1904 #ifdef WPS_WORKAROUNDS
1906 * Some deployed implementations seem to advertise incorrect
1907 * information in this attribute. For example, Linksys WRT350N
1908 * seems to have a byteorder bug that breaks this negotiation.
1909 * In order to interoperate with existing implementations,
1910 * assume that the Enrollee supports everything we do.
1912 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1913 "does not advertise supported encryption types "
1915 wps->encr_type = wps->wps->encr_types;
1916 #else /* WPS_WORKAROUNDS */
1918 #endif /* WPS_WORKAROUNDS */
1925 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1928 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1933 wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1940 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1944 if (methods == NULL) {
1945 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1949 m = WPA_GET_BE16(methods);
1951 wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1957 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1959 if (state == NULL) {
1960 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1965 wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1972 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1976 if (assoc == NULL) {
1977 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1981 a = WPA_GET_BE16(assoc);
1982 wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1988 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1993 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1997 e = WPA_GET_BE16(err);
1998 wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
2004 static enum wps_process_res wps_process_m1(struct wps_data *wps,
2005 struct wps_parse_attr *attr)
2007 wpa_printf(MSG_DEBUG, "WPS: Received M1");
2009 if (wps->state != RECV_M1) {
2010 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2011 "receiving M1", wps->state);
2015 if (wps_process_uuid_e(wps, attr->uuid_e) ||
2016 wps_process_mac_addr(wps, attr->mac_addr) ||
2017 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
2018 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
2019 wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
2020 wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
2021 wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
2022 wps_process_config_methods(wps, attr->config_methods) ||
2023 wps_process_wps_state(wps, attr->wps_state) ||
2024 wps_process_device_attrs(&wps->peer_dev, attr) ||
2025 wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
2026 wps_process_assoc_state(wps, attr->assoc_state) ||
2027 wps_process_dev_password_id(wps, attr->dev_password_id) ||
2028 wps_process_config_error(wps, attr->config_error) ||
2029 wps_process_os_version(&wps->peer_dev, attr->os_version))
2032 if (wps->dev_pw_id < 0x10 &&
2033 wps->dev_pw_id != DEV_PW_DEFAULT &&
2034 wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
2035 wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
2036 wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
2037 (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
2038 !wps->wps->registrar->pbc)) {
2039 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
2041 wps->state = SEND_M2D;
2042 return WPS_CONTINUE;
2045 #ifdef CONFIG_WPS_OOB
2046 if (wps->dev_pw_id >= 0x10 &&
2047 wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
2048 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
2049 "%d mismatch", wps->dev_pw_id);
2050 wps->state = SEND_M2D;
2051 return WPS_CONTINUE;
2053 #endif /* CONFIG_WPS_OOB */
2055 if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2056 if (wps->wps->registrar->force_pbc_overlap ||
2057 wps_registrar_pbc_overlap(wps->wps->registrar,
2058 wps->mac_addr_e, wps->uuid_e)) {
2059 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2061 wps->state = SEND_M2D;
2062 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2063 wps_pbc_overlap_event(wps->wps);
2064 wps->wps->registrar->force_pbc_overlap = 1;
2065 return WPS_CONTINUE;
2067 wps_registrar_add_pbc_session(wps->wps->registrar,
2068 wps->mac_addr_e, wps->uuid_e);
2072 wps->state = SEND_M2;
2073 return WPS_CONTINUE;
2077 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2078 const struct wpabuf *msg,
2079 struct wps_parse_attr *attr)
2081 wpa_printf(MSG_DEBUG, "WPS: Received M3");
2083 if (wps->state != RECV_M3) {
2084 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2085 "receiving M3", wps->state);
2086 wps->state = SEND_WSC_NACK;
2087 return WPS_CONTINUE;
2090 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2091 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2093 wps->state = SEND_WSC_NACK;
2094 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2095 return WPS_CONTINUE;
2098 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2099 wps_process_authenticator(wps, attr->authenticator, msg) ||
2100 wps_process_e_hash1(wps, attr->e_hash1) ||
2101 wps_process_e_hash2(wps, attr->e_hash2)) {
2102 wps->state = SEND_WSC_NACK;
2103 return WPS_CONTINUE;
2106 wps->state = SEND_M4;
2107 return WPS_CONTINUE;
2111 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2112 const struct wpabuf *msg,
2113 struct wps_parse_attr *attr)
2115 struct wpabuf *decrypted;
2116 struct wps_parse_attr eattr;
2118 wpa_printf(MSG_DEBUG, "WPS: Received M5");
2120 if (wps->state != RECV_M5) {
2121 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2122 "receiving M5", wps->state);
2123 wps->state = SEND_WSC_NACK;
2124 return WPS_CONTINUE;
2127 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2128 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2130 wps->state = SEND_WSC_NACK;
2131 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2132 return WPS_CONTINUE;
2135 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2136 wps_process_authenticator(wps, attr->authenticator, msg)) {
2137 wps->state = SEND_WSC_NACK;
2138 return WPS_CONTINUE;
2141 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2142 attr->encr_settings_len);
2143 if (decrypted == NULL) {
2144 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2145 "Settings attribute");
2146 wps->state = SEND_WSC_NACK;
2147 return WPS_CONTINUE;
2150 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2152 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2153 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2154 wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2155 wpabuf_free(decrypted);
2156 wps->state = SEND_WSC_NACK;
2157 return WPS_CONTINUE;
2159 wpabuf_free(decrypted);
2161 wps->state = SEND_M6;
2162 return WPS_CONTINUE;
2166 static void wps_sta_cred_cb(struct wps_data *wps)
2169 * Update credential to only include a single authentication and
2170 * encryption type in case the AP configuration includes more than one
2173 if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2174 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2175 else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2176 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2177 if (wps->cred.encr_type & WPS_ENCR_AES)
2178 wps->cred.encr_type = WPS_ENCR_AES;
2179 else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2180 wps->cred.encr_type = WPS_ENCR_TKIP;
2181 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2182 "AP configuration");
2183 if (wps->wps->cred_cb)
2184 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2188 static void wps_cred_update(struct wps_credential *dst,
2189 struct wps_credential *src)
2191 os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2192 dst->ssid_len = src->ssid_len;
2193 dst->auth_type = src->auth_type;
2194 dst->encr_type = src->encr_type;
2195 dst->key_idx = src->key_idx;
2196 os_memcpy(dst->key, src->key, sizeof(dst->key));
2197 dst->key_len = src->key_len;
2201 static int wps_process_ap_settings_r(struct wps_data *wps,
2202 struct wps_parse_attr *attr)
2204 if (wps->wps->ap || wps->er)
2207 /* AP Settings Attributes in M7 when Enrollee is an AP */
2208 if (wps_process_ap_settings(attr, &wps->cred) < 0)
2211 wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2213 if (wps->new_ap_settings) {
2214 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2216 wps_cred_update(&wps->cred, wps->new_ap_settings);
2220 * Use the AP PIN only to receive the current AP settings, not
2221 * to reconfigure the AP.
2223 if (wps->ap_settings_cb) {
2224 wps->ap_settings_cb(wps->ap_settings_cb_ctx,
2228 wps_sta_cred_cb(wps);
2234 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2235 const struct wpabuf *msg,
2236 struct wps_parse_attr *attr)
2238 struct wpabuf *decrypted;
2239 struct wps_parse_attr eattr;
2241 wpa_printf(MSG_DEBUG, "WPS: Received M7");
2243 if (wps->state != RECV_M7) {
2244 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2245 "receiving M7", wps->state);
2246 wps->state = SEND_WSC_NACK;
2247 return WPS_CONTINUE;
2250 if (wps->pbc && wps->wps->registrar->force_pbc_overlap) {
2251 wpa_printf(MSG_DEBUG, "WPS: Reject negotiation due to PBC "
2253 wps->state = SEND_WSC_NACK;
2254 wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2255 return WPS_CONTINUE;
2258 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2259 wps_process_authenticator(wps, attr->authenticator, msg)) {
2260 wps->state = SEND_WSC_NACK;
2261 return WPS_CONTINUE;
2264 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2265 attr->encr_settings_len);
2266 if (decrypted == NULL) {
2267 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypt Encrypted "
2268 "Settings attribute");
2269 wps->state = SEND_WSC_NACK;
2270 return WPS_CONTINUE;
2273 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2275 if (wps_parse_msg(decrypted, &eattr) < 0 ||
2276 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2277 wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2278 wps_process_ap_settings_r(wps, &eattr)) {
2279 wpabuf_free(decrypted);
2280 wps->state = SEND_WSC_NACK;
2281 return WPS_CONTINUE;
2284 wpabuf_free(decrypted);
2286 wps->state = SEND_M8;
2287 return WPS_CONTINUE;
2291 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2292 const struct wpabuf *msg)
2294 struct wps_parse_attr attr;
2295 enum wps_process_res ret = WPS_CONTINUE;
2297 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2299 if (wps_parse_msg(msg, &attr) < 0)
2302 if (!wps_version_supported(attr.version)) {
2303 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2304 attr.version ? *attr.version : 0);
2308 if (attr.msg_type == NULL) {
2309 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2313 if (*attr.msg_type != WPS_M1 &&
2314 (attr.registrar_nonce == NULL ||
2315 os_memcmp(wps->nonce_r, attr.registrar_nonce,
2316 WPS_NONCE_LEN != 0))) {
2317 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2321 switch (*attr.msg_type) {
2323 #ifdef CONFIG_WPS_UPNP
2324 if (wps->wps->wps_upnp && attr.mac_addr) {
2325 /* Remove old pending messages when starting new run */
2326 wps_free_pending_msgs(wps->wps->upnp_msgs);
2327 wps->wps->upnp_msgs = NULL;
2329 upnp_wps_device_send_wlan_event(
2330 wps->wps->wps_upnp, attr.mac_addr,
2331 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2333 #endif /* CONFIG_WPS_UPNP */
2334 ret = wps_process_m1(wps, &attr);
2337 ret = wps_process_m3(wps, msg, &attr);
2338 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2339 wps_fail_event(wps->wps, WPS_M3);
2342 ret = wps_process_m5(wps, msg, &attr);
2343 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2344 wps_fail_event(wps->wps, WPS_M5);
2347 ret = wps_process_m7(wps, msg, &attr);
2348 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2349 wps_fail_event(wps->wps, WPS_M7);
2352 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2357 if (ret == WPS_CONTINUE) {
2358 /* Save a copy of the last message for Authenticator derivation
2360 wpabuf_free(wps->last_msg);
2361 wps->last_msg = wpabuf_dup(msg);
2368 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2369 const struct wpabuf *msg)
2371 struct wps_parse_attr attr;
2373 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2375 if (wps_parse_msg(msg, &attr) < 0)
2378 if (!wps_version_supported(attr.version)) {
2379 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2380 attr.version ? *attr.version : 0);
2384 if (attr.msg_type == NULL) {
2385 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2389 if (*attr.msg_type != WPS_WSC_ACK) {
2390 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2395 #ifdef CONFIG_WPS_UPNP
2396 if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2397 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2398 if (wps->wps->upnp_msgs)
2399 return WPS_CONTINUE;
2400 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2401 "external Registrar");
2404 #endif /* CONFIG_WPS_UPNP */
2406 if (attr.registrar_nonce == NULL ||
2407 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2409 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2413 if (attr.enrollee_nonce == NULL ||
2414 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2415 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2419 if (wps->state == RECV_M2D_ACK) {
2420 #ifdef CONFIG_WPS_UPNP
2421 if (wps->wps->wps_upnp &&
2422 upnp_wps_subscribers(wps->wps->wps_upnp)) {
2423 if (wps->wps->upnp_msgs)
2424 return WPS_CONTINUE;
2425 if (wps->ext_reg == 0)
2427 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2428 "external Registrar");
2431 #endif /* CONFIG_WPS_UPNP */
2433 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2434 "terminate negotiation");
2441 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2442 const struct wpabuf *msg)
2444 struct wps_parse_attr attr;
2447 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2449 old_state = wps->state;
2450 wps->state = SEND_WSC_NACK;
2452 if (wps_parse_msg(msg, &attr) < 0)
2455 if (!wps_version_supported(attr.version)) {
2456 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2457 attr.version ? *attr.version : 0);
2461 if (attr.msg_type == NULL) {
2462 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2466 if (*attr.msg_type != WPS_WSC_NACK) {
2467 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2472 #ifdef CONFIG_WPS_UPNP
2473 if (wps->wps->wps_upnp && wps->ext_reg) {
2474 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2475 "Registrar terminated by the Enrollee");
2478 #endif /* CONFIG_WPS_UPNP */
2480 if (attr.registrar_nonce == NULL ||
2481 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2483 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2487 if (attr.enrollee_nonce == NULL ||
2488 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2489 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2493 if (attr.config_error == NULL) {
2494 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2499 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2500 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2502 switch (old_state) {
2504 wps_fail_event(wps->wps, WPS_M2);
2507 wps_fail_event(wps->wps, WPS_M4);
2510 wps_fail_event(wps->wps, WPS_M6);
2513 wps_fail_event(wps->wps, WPS_M8);
2523 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2524 const struct wpabuf *msg)
2526 struct wps_parse_attr attr;
2528 wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2530 if (wps->state != RECV_DONE &&
2531 (!wps->wps->wps_upnp || !wps->ext_reg)) {
2532 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2533 "receiving WSC_Done", wps->state);
2537 if (wps_parse_msg(msg, &attr) < 0)
2540 if (!wps_version_supported(attr.version)) {
2541 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2542 attr.version ? *attr.version : 0);
2546 if (attr.msg_type == NULL) {
2547 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2551 if (*attr.msg_type != WPS_WSC_DONE) {
2552 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2557 #ifdef CONFIG_WPS_UPNP
2558 if (wps->wps->wps_upnp && wps->ext_reg) {
2559 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2560 "Registrar completed successfully");
2561 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2565 #endif /* CONFIG_WPS_UPNP */
2567 if (attr.registrar_nonce == NULL ||
2568 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2570 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2574 if (attr.enrollee_nonce == NULL ||
2575 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2576 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2580 wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2581 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2584 if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2585 wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2586 struct wps_credential cred;
2588 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2589 "on first Enrollee connection");
2591 os_memset(&cred, 0, sizeof(cred));
2592 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2593 cred.ssid_len = wps->wps->ssid_len;
2594 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2595 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2596 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2597 cred.key_len = wps->new_psk_len;
2599 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2600 wpa_hexdump_ascii_key(MSG_DEBUG,
2601 "WPS: Generated random passphrase",
2602 wps->new_psk, wps->new_psk_len);
2603 if (wps->wps->cred_cb)
2604 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2606 os_free(wps->new_psk);
2607 wps->new_psk = NULL;
2610 if (!wps->wps->ap && !wps->er)
2611 wps_sta_cred_cb(wps);
2614 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2615 wps->new_psk, wps->new_psk_len)) {
2616 wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2619 os_free(wps->new_psk);
2620 wps->new_psk = NULL;
2623 wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2626 wps_registrar_remove_pbc_session(wps->wps->registrar,
2627 wps->mac_addr_e, wps->uuid_e);
2628 wps_registrar_pbc_completed(wps->wps->registrar);
2630 wps_registrar_pin_completed(wps->wps->registrar);
2633 wps_success_event(wps->wps);
2639 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2640 enum wsc_op_code op_code,
2641 const struct wpabuf *msg)
2643 enum wps_process_res ret;
2645 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2647 (unsigned long) wpabuf_len(msg), op_code);
2649 #ifdef CONFIG_WPS_UPNP
2650 if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2651 struct wps_parse_attr attr;
2652 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2653 *attr.msg_type == WPS_M3)
2654 wps->ext_reg = 2; /* past M2/M2D phase */
2656 if (wps->ext_reg > 1)
2657 wps_registrar_free_pending_m2(wps->wps);
2658 if (wps->wps->wps_upnp && wps->ext_reg &&
2659 wps->wps->upnp_msgs == NULL &&
2660 (op_code == WSC_MSG || op_code == WSC_Done || op_code == WSC_NACK))
2662 struct wps_parse_attr attr;
2664 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2667 type = *attr.msg_type;
2668 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2669 " to external Registrar for processing", type);
2670 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2672 UPNP_WPS_WLANEVENT_TYPE_EAP,
2674 if (op_code == WSC_MSG)
2676 } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2677 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2678 "external Registrar");
2679 return WPS_CONTINUE;
2681 #endif /* CONFIG_WPS_UPNP */
2685 return wps_process_wsc_msg(wps, msg);
2687 return wps_process_wsc_ack(wps, msg);
2689 return wps_process_wsc_nack(wps, msg);
2691 ret = wps_process_wsc_done(wps, msg);
2692 if (ret == WPS_FAILURE) {
2693 wps->state = SEND_WSC_NACK;
2694 wps_fail_event(wps->wps, WPS_WSC_DONE);
2698 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2704 int wps_registrar_update_ie(struct wps_registrar *reg)
2706 return wps_set_ie(reg);
2710 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2713 struct wps_registrar *reg = eloop_ctx;
2715 wpa_printf(MSG_DEBUG, "WPS: Selected Registrar timeout - "
2716 "unselect internal Registrar");
2717 reg->selected_registrar = 0;
2719 wps_registrar_selected_registrar_changed(reg);
2723 #ifdef CONFIG_WPS_UPNP
2724 static void wps_registrar_sel_reg_add(struct wps_registrar *reg,
2725 struct subscription *s)
2727 wpa_printf(MSG_DEBUG, "WPS: External Registrar selected (dev_pw_id=%d "
2728 "config_methods=0x%x)",
2729 s->dev_password_id, s->config_methods);
2730 reg->sel_reg_union = 1;
2731 if (reg->sel_reg_dev_password_id_override != DEV_PW_PUSHBUTTON)
2732 reg->sel_reg_dev_password_id_override = s->dev_password_id;
2733 if (reg->sel_reg_config_methods_override == -1)
2734 reg->sel_reg_config_methods_override = 0;
2735 reg->sel_reg_config_methods_override |= s->config_methods;
2737 #endif /* CONFIG_WPS_UPNP */
2740 static void wps_registrar_sel_reg_union(struct wps_registrar *reg)
2742 #ifdef CONFIG_WPS_UPNP
2743 struct subscription *s;
2745 if (reg->wps->wps_upnp == NULL)
2748 dl_list_for_each(s, ®->wps->wps_upnp->subscriptions,
2749 struct subscription, list) {
2750 struct subscr_addr *sa;
2751 sa = dl_list_first(&s->addr_list, struct subscr_addr, list);
2753 wpa_printf(MSG_DEBUG, "WPS: External Registrar %s:%d",
2754 inet_ntoa(sa->saddr.sin_addr),
2755 ntohs(sa->saddr.sin_port));
2757 if (s->selected_registrar)
2758 wps_registrar_sel_reg_add(reg, s);
2760 wpa_printf(MSG_DEBUG, "WPS: External Registrar not "
2763 #endif /* CONFIG_WPS_UPNP */
2768 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
2769 * @reg: Registrar data from wps_registrar_init()
2771 * This function is called when selected registrar state changes, e.g., when an
2772 * AP receives a SetSelectedRegistrar UPnP message.
2774 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg)
2776 wpa_printf(MSG_DEBUG, "WPS: Selected registrar information changed");
2778 reg->sel_reg_union = reg->selected_registrar;
2779 reg->sel_reg_dev_password_id_override = -1;
2780 reg->sel_reg_config_methods_override = -1;
2781 if (reg->selected_registrar) {
2782 reg->sel_reg_config_methods_override =
2783 reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
2785 reg->sel_reg_dev_password_id_override =
2787 reg->sel_reg_config_methods_override |=
2788 WPS_CONFIG_PUSHBUTTON;
2790 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
2791 "(pbc=%d)", reg->pbc);
2793 wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
2795 wps_registrar_sel_reg_union(reg);
2798 wps_cb_set_sel_reg(reg);
2802 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
2803 char *buf, size_t buflen)
2805 struct wps_registrar_device *d;
2808 char devtype[WPS_DEV_TYPE_BUFSIZE];
2810 d = wps_device_get(reg, addr);
2813 if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
2816 ret = os_snprintf(buf + len, buflen - len,
2818 "wpsPrimaryDeviceType=%s\n"
2819 "wpsDeviceName=%s\n"
2820 "wpsManufacturer=%s\n"
2822 "wpsModelNumber=%s\n"
2823 "wpsSerialNumber=%s\n",
2825 wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
2827 d->dev.device_name ? d->dev.device_name : "",
2828 d->dev.manufacturer ? d->dev.manufacturer : "",
2829 d->dev.model_name ? d->dev.model_name : "",
2830 d->dev.model_number ? d->dev.model_number : "",
2831 d->dev.serial_number ? d->dev.serial_number : "");
2832 if (ret < 0 || (size_t) ret >= buflen - len)