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 "crypto/random.h"
25 #include "common/ieee802_11_defs.h"
27 #include "wps_dev_attr.h"
29 #include "wps_upnp_i.h"
31 #ifndef CONFIG_WPS_STRICT
32 #define WPS_WORKAROUNDS
33 #endif /* CONFIG_WPS_STRICT */
37 u8 uuid
[WPS_UUID_LEN
];
41 #define PIN_LOCKED BIT(0)
42 #define PIN_EXPIRES BIT(1)
44 struct os_time expiration
;
45 u8 enrollee_addr
[ETH_ALEN
];
49 static void wps_free_pin(struct wps_uuid_pin
*pin
)
56 static void wps_remove_pin(struct wps_uuid_pin
*pin
)
58 dl_list_del(&pin
->list
);
63 static void wps_free_pins(struct dl_list
*pins
)
65 struct wps_uuid_pin
*pin
, *prev
;
66 dl_list_for_each_safe(pin
, prev
, pins
, struct wps_uuid_pin
, list
)
71 struct wps_pbc_session
{
72 struct wps_pbc_session
*next
;
74 u8 uuid_e
[WPS_UUID_LEN
];
75 struct os_time timestamp
;
79 static void wps_free_pbc_sessions(struct wps_pbc_session
*pbc
)
81 struct wps_pbc_session
*prev
;
91 struct wps_registrar_device
{
92 struct wps_registrar_device
*next
;
93 struct wps_device_data dev
;
94 u8 uuid
[WPS_UUID_LEN
];
98 struct wps_registrar
{
99 struct wps_context
*wps
;
102 int selected_registrar
;
104 int (*new_psk_cb
)(void *ctx
, const u8
*mac_addr
, const u8
*psk
,
106 int (*set_ie_cb
)(void *ctx
, struct wpabuf
*beacon_ie
,
107 struct wpabuf
*probe_resp_ie
);
108 void (*pin_needed_cb
)(void *ctx
, const u8
*uuid_e
,
109 const struct wps_device_data
*dev
);
110 void (*reg_success_cb
)(void *ctx
, const u8
*mac_addr
,
112 void (*set_sel_reg_cb
)(void *ctx
, int sel_reg
, u16 dev_passwd_id
,
113 u16 sel_reg_config_methods
);
114 void (*enrollee_seen_cb
)(void *ctx
, const u8
*addr
, const u8
*uuid_e
,
115 const u8
*pri_dev_type
, u16 config_methods
,
116 u16 dev_password_id
, u8 request_type
,
117 const char *dev_name
);
121 struct wps_pbc_session
*pbc_sessions
;
124 struct wpabuf
*extra_cred
;
125 int disable_auto_conf
;
127 int sel_reg_dev_password_id_override
;
128 int sel_reg_config_methods_override
;
132 struct wps_registrar_device
*devices
;
134 int force_pbc_overlap
;
136 u8 authorized_macs
[WPS_MAX_AUTHORIZED_MACS
][ETH_ALEN
];
137 u8 authorized_macs_union
[WPS_MAX_AUTHORIZED_MACS
][ETH_ALEN
];
139 u8 p2p_dev_addr
[ETH_ALEN
];
143 static int wps_set_ie(struct wps_registrar
*reg
);
144 static void wps_registrar_pbc_timeout(void *eloop_ctx
, void *timeout_ctx
);
145 static void wps_registrar_set_selected_timeout(void *eloop_ctx
,
149 static void wps_registrar_add_authorized_mac(struct wps_registrar
*reg
,
153 wpa_printf(MSG_DEBUG
, "WPS: Add authorized MAC " MACSTR
,
155 for (i
= 0; i
< WPS_MAX_AUTHORIZED_MACS
; i
++)
156 if (os_memcmp(reg
->authorized_macs
[i
], addr
, ETH_ALEN
) == 0) {
157 wpa_printf(MSG_DEBUG
, "WPS: Authorized MAC was "
158 "already in the list");
159 return; /* already in list */
161 for (i
= WPS_MAX_AUTHORIZED_MACS
- 1; i
> 0; i
--)
162 os_memcpy(reg
->authorized_macs
[i
], reg
->authorized_macs
[i
- 1],
164 os_memcpy(reg
->authorized_macs
[0], addr
, ETH_ALEN
);
165 wpa_hexdump(MSG_DEBUG
, "WPS: Authorized MACs",
166 (u8
*) reg
->authorized_macs
, sizeof(reg
->authorized_macs
));
170 static void wps_registrar_remove_authorized_mac(struct wps_registrar
*reg
,
174 wpa_printf(MSG_DEBUG
, "WPS: Remove authorized MAC " MACSTR
,
176 for (i
= 0; i
< WPS_MAX_AUTHORIZED_MACS
; i
++) {
177 if (os_memcmp(reg
->authorized_macs
, addr
, ETH_ALEN
) == 0)
180 if (i
== WPS_MAX_AUTHORIZED_MACS
) {
181 wpa_printf(MSG_DEBUG
, "WPS: Authorized MAC was not in the "
183 return; /* not in the list */
185 for (; i
+ 1 < WPS_MAX_AUTHORIZED_MACS
; i
++)
186 os_memcpy(reg
->authorized_macs
[i
], reg
->authorized_macs
[i
+ 1],
188 os_memset(reg
->authorized_macs
[WPS_MAX_AUTHORIZED_MACS
- 1], 0,
190 wpa_hexdump(MSG_DEBUG
, "WPS: Authorized MACs",
191 (u8
*) reg
->authorized_macs
, sizeof(reg
->authorized_macs
));
195 static void wps_free_devices(struct wps_registrar_device
*dev
)
197 struct wps_registrar_device
*prev
;
202 wps_device_data_free(&prev
->dev
);
208 static struct wps_registrar_device
* wps_device_get(struct wps_registrar
*reg
,
211 struct wps_registrar_device
*dev
;
213 for (dev
= reg
->devices
; dev
; dev
= dev
->next
) {
214 if (os_memcmp(dev
->dev
.mac_addr
, addr
, ETH_ALEN
) == 0)
221 static void wps_device_clone_data(struct wps_device_data
*dst
,
222 struct wps_device_data
*src
)
224 os_memcpy(dst
->mac_addr
, src
->mac_addr
, ETH_ALEN
);
225 os_memcpy(dst
->pri_dev_type
, src
->pri_dev_type
, WPS_DEV_TYPE_LEN
);
227 #define WPS_STRDUP(n) \
229 dst->n = src->n ? os_strdup(src->n) : NULL
231 WPS_STRDUP(device_name
);
232 WPS_STRDUP(manufacturer
);
233 WPS_STRDUP(model_name
);
234 WPS_STRDUP(model_number
);
235 WPS_STRDUP(serial_number
);
240 int wps_device_store(struct wps_registrar
*reg
,
241 struct wps_device_data
*dev
, const u8
*uuid
)
243 struct wps_registrar_device
*d
;
245 d
= wps_device_get(reg
, dev
->mac_addr
);
247 d
= os_zalloc(sizeof(*d
));
250 d
->next
= reg
->devices
;
254 wps_device_clone_data(&d
->dev
, dev
);
255 os_memcpy(d
->uuid
, uuid
, WPS_UUID_LEN
);
261 static void wps_registrar_add_pbc_session(struct wps_registrar
*reg
,
262 const u8
*addr
, const u8
*uuid_e
)
264 struct wps_pbc_session
*pbc
, *prev
= NULL
;
269 pbc
= reg
->pbc_sessions
;
271 if (os_memcmp(pbc
->addr
, addr
, ETH_ALEN
) == 0 &&
272 os_memcmp(pbc
->uuid_e
, uuid_e
, WPS_UUID_LEN
) == 0) {
274 prev
->next
= pbc
->next
;
276 reg
->pbc_sessions
= pbc
->next
;
284 pbc
= os_zalloc(sizeof(*pbc
));
287 os_memcpy(pbc
->addr
, addr
, ETH_ALEN
);
289 os_memcpy(pbc
->uuid_e
, uuid_e
, WPS_UUID_LEN
);
292 pbc
->next
= reg
->pbc_sessions
;
293 reg
->pbc_sessions
= pbc
;
294 pbc
->timestamp
= now
;
296 /* remove entries that have timed out */
301 if (now
.sec
> pbc
->timestamp
.sec
+ WPS_PBC_WALK_TIME
) {
303 wps_free_pbc_sessions(pbc
);
312 static void wps_registrar_remove_pbc_session(struct wps_registrar
*reg
,
313 const u8
*addr
, const u8
*uuid_e
)
315 struct wps_pbc_session
*pbc
, *prev
= NULL
;
317 pbc
= reg
->pbc_sessions
;
319 if (os_memcmp(pbc
->addr
, addr
, ETH_ALEN
) == 0 &&
320 os_memcmp(pbc
->uuid_e
, uuid_e
, WPS_UUID_LEN
) == 0) {
322 prev
->next
= pbc
->next
;
324 reg
->pbc_sessions
= pbc
->next
;
334 int wps_registrar_pbc_overlap(struct wps_registrar
*reg
,
335 const u8
*addr
, const u8
*uuid_e
)
338 struct wps_pbc_session
*pbc
;
339 struct wps_pbc_session
*first
= NULL
;
344 for (pbc
= reg
->pbc_sessions
; pbc
; pbc
= pbc
->next
) {
345 if (now
.sec
> pbc
->timestamp
.sec
+ WPS_PBC_WALK_TIME
)
348 os_memcmp(pbc
->uuid_e
, first
->uuid_e
, WPS_UUID_LEN
) == 0)
349 continue; /* same Enrollee */
350 if (uuid_e
== NULL
||
351 os_memcmp(uuid_e
, pbc
->uuid_e
, WPS_UUID_LEN
))
359 os_memcmp(uuid_e
, first
->uuid_e
, WPS_UUID_LEN
) != 0))
362 return count
> 1 ? 1 : 0;
366 static int wps_build_wps_state(struct wps_context
*wps
, struct wpabuf
*msg
)
368 wpa_printf(MSG_DEBUG
, "WPS: * Wi-Fi Protected Setup State (%d)",
370 wpabuf_put_be16(msg
, ATTR_WPS_STATE
);
371 wpabuf_put_be16(msg
, 1);
372 wpabuf_put_u8(msg
, wps
->wps_state
);
377 #ifdef CONFIG_WPS_UPNP
378 static void wps_registrar_free_pending_m2(struct wps_context
*wps
)
380 struct upnp_pending_message
*p
, *p2
, *prev
= NULL
;
383 if (p
->type
== WPS_M2
|| p
->type
== WPS_M2D
) {
385 wps
->upnp_msgs
= p
->next
;
387 prev
->next
= p
->next
;
388 wpa_printf(MSG_DEBUG
, "WPS UPnP: Drop pending M2/M2D");
391 wpabuf_free(p2
->msg
);
399 #endif /* CONFIG_WPS_UPNP */
402 static int wps_build_ap_setup_locked(struct wps_context
*wps
,
405 if (wps
->ap_setup_locked
&& wps
->ap_setup_locked
!= 2) {
406 wpa_printf(MSG_DEBUG
, "WPS: * AP Setup Locked");
407 wpabuf_put_be16(msg
, ATTR_AP_SETUP_LOCKED
);
408 wpabuf_put_be16(msg
, 1);
409 wpabuf_put_u8(msg
, 1);
415 static int wps_build_selected_registrar(struct wps_registrar
*reg
,
418 if (!reg
->sel_reg_union
)
420 wpa_printf(MSG_DEBUG
, "WPS: * Selected Registrar");
421 wpabuf_put_be16(msg
, ATTR_SELECTED_REGISTRAR
);
422 wpabuf_put_be16(msg
, 1);
423 wpabuf_put_u8(msg
, 1);
428 static int wps_build_sel_reg_dev_password_id(struct wps_registrar
*reg
,
431 u16 id
= reg
->pbc
? DEV_PW_PUSHBUTTON
: DEV_PW_DEFAULT
;
432 if (!reg
->sel_reg_union
)
434 if (reg
->sel_reg_dev_password_id_override
>= 0)
435 id
= reg
->sel_reg_dev_password_id_override
;
436 wpa_printf(MSG_DEBUG
, "WPS: * Device Password ID (%d)", id
);
437 wpabuf_put_be16(msg
, ATTR_DEV_PASSWORD_ID
);
438 wpabuf_put_be16(msg
, 2);
439 wpabuf_put_be16(msg
, id
);
444 static int wps_build_sel_pbc_reg_uuid_e(struct wps_registrar
*reg
,
447 u16 id
= reg
->pbc
? DEV_PW_PUSHBUTTON
: DEV_PW_DEFAULT
;
448 if (!reg
->sel_reg_union
)
450 if (reg
->sel_reg_dev_password_id_override
>= 0)
451 id
= reg
->sel_reg_dev_password_id_override
;
452 if (id
!= DEV_PW_PUSHBUTTON
|| !reg
->dualband
)
454 return wps_build_uuid_e(msg
, reg
->wps
->uuid
);
458 static void wps_set_pushbutton(u16
*methods
, u16 conf_methods
)
460 *methods
|= WPS_CONFIG_PUSHBUTTON
;
462 if (conf_methods
& WPS_CONFIG_VIRT_PUSHBUTTON
)
463 *methods
|= WPS_CONFIG_VIRT_PUSHBUTTON
;
464 if (conf_methods
& WPS_CONFIG_PHY_PUSHBUTTON
)
465 *methods
|= WPS_CONFIG_PHY_PUSHBUTTON
;
466 if ((*methods
& WPS_CONFIG_VIRT_PUSHBUTTON
) !=
467 WPS_CONFIG_VIRT_PUSHBUTTON
||
468 (*methods
& WPS_CONFIG_PHY_PUSHBUTTON
) !=
469 WPS_CONFIG_PHY_PUSHBUTTON
) {
471 * Required to include virtual/physical flag, but we were not
472 * configured with push button type, so have to default to one
475 *methods
|= WPS_CONFIG_PHY_PUSHBUTTON
;
477 #endif /* CONFIG_WPS2 */
481 static int wps_build_sel_reg_config_methods(struct wps_registrar
*reg
,
485 if (!reg
->sel_reg_union
)
487 methods
= reg
->wps
->config_methods
;
488 methods
&= ~WPS_CONFIG_PUSHBUTTON
;
490 methods
&= ~(WPS_CONFIG_VIRT_PUSHBUTTON
|
491 WPS_CONFIG_PHY_PUSHBUTTON
);
492 #endif /* CONFIG_WPS2 */
494 wps_set_pushbutton(&methods
, reg
->wps
->config_methods
);
495 if (reg
->sel_reg_config_methods_override
>= 0)
496 methods
= reg
->sel_reg_config_methods_override
;
497 wpa_printf(MSG_DEBUG
, "WPS: * Selected Registrar Config Methods (%x)",
499 wpabuf_put_be16(msg
, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS
);
500 wpabuf_put_be16(msg
, 2);
501 wpabuf_put_be16(msg
, methods
);
506 static int wps_build_probe_config_methods(struct wps_registrar
*reg
,
511 * These are the methods that the AP supports as an Enrollee for adding
512 * external Registrars.
514 methods
= reg
->wps
->config_methods
& ~WPS_CONFIG_PUSHBUTTON
;
516 methods
&= ~(WPS_CONFIG_VIRT_PUSHBUTTON
|
517 WPS_CONFIG_PHY_PUSHBUTTON
);
518 #endif /* CONFIG_WPS2 */
519 wpa_printf(MSG_DEBUG
, "WPS: * Config Methods (%x)", methods
);
520 wpabuf_put_be16(msg
, ATTR_CONFIG_METHODS
);
521 wpabuf_put_be16(msg
, 2);
522 wpabuf_put_be16(msg
, methods
);
527 static int wps_build_config_methods_r(struct wps_registrar
*reg
,
531 methods
= reg
->wps
->config_methods
& ~WPS_CONFIG_PUSHBUTTON
;
533 methods
&= ~(WPS_CONFIG_VIRT_PUSHBUTTON
|
534 WPS_CONFIG_PHY_PUSHBUTTON
);
535 #endif /* CONFIG_WPS2 */
537 wps_set_pushbutton(&methods
, reg
->wps
->config_methods
);
538 return wps_build_config_methods(msg
, methods
);
542 const u8
* wps_authorized_macs(struct wps_registrar
*reg
, size_t *count
)
547 while (*count
< WPS_MAX_AUTHORIZED_MACS
) {
548 if (is_zero_ether_addr(reg
->authorized_macs_union
[*count
]))
552 #endif /* CONFIG_WPS2 */
554 return (const u8
*) reg
->authorized_macs_union
;
559 * wps_registrar_init - Initialize WPS Registrar data
560 * @wps: Pointer to longterm WPS context
561 * @cfg: Registrar configuration
562 * Returns: Pointer to allocated Registrar data or %NULL on failure
564 * This function is used to initialize WPS Registrar functionality. It can be
565 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
566 * runs (e.g., when run as an internal Registrar in an AP). Caller is
567 * responsible for freeing the returned data with wps_registrar_deinit() when
568 * Registrar functionality is not needed anymore.
570 struct wps_registrar
*
571 wps_registrar_init(struct wps_context
*wps
,
572 const struct wps_registrar_config
*cfg
)
574 struct wps_registrar
*reg
= os_zalloc(sizeof(*reg
));
578 dl_list_init(®
->pins
);
580 reg
->new_psk_cb
= cfg
->new_psk_cb
;
581 reg
->set_ie_cb
= cfg
->set_ie_cb
;
582 reg
->pin_needed_cb
= cfg
->pin_needed_cb
;
583 reg
->reg_success_cb
= cfg
->reg_success_cb
;
584 reg
->set_sel_reg_cb
= cfg
->set_sel_reg_cb
;
585 reg
->enrollee_seen_cb
= cfg
->enrollee_seen_cb
;
586 reg
->cb_ctx
= cfg
->cb_ctx
;
587 reg
->skip_cred_build
= cfg
->skip_cred_build
;
588 if (cfg
->extra_cred
) {
589 reg
->extra_cred
= wpabuf_alloc_copy(cfg
->extra_cred
,
590 cfg
->extra_cred_len
);
591 if (reg
->extra_cred
== NULL
) {
596 reg
->disable_auto_conf
= cfg
->disable_auto_conf
;
597 reg
->sel_reg_dev_password_id_override
= -1;
598 reg
->sel_reg_config_methods_override
= -1;
599 reg
->static_wep_only
= cfg
->static_wep_only
;
600 reg
->dualband
= cfg
->dualband
;
602 if (wps_set_ie(reg
)) {
603 wps_registrar_deinit(reg
);
612 * wps_registrar_deinit - Deinitialize WPS Registrar data
613 * @reg: Registrar data from wps_registrar_init()
615 void wps_registrar_deinit(struct wps_registrar
*reg
)
619 eloop_cancel_timeout(wps_registrar_pbc_timeout
, reg
, NULL
);
620 eloop_cancel_timeout(wps_registrar_set_selected_timeout
, reg
, NULL
);
621 wps_free_pins(®
->pins
);
622 wps_free_pbc_sessions(reg
->pbc_sessions
);
623 wpabuf_free(reg
->extra_cred
);
624 wps_free_devices(reg
->devices
);
630 * wps_registrar_add_pin - Configure a new PIN for Registrar
631 * @reg: Registrar data from wps_registrar_init()
632 * @addr: Enrollee MAC address or %NULL if not known
633 * @uuid: UUID-E or %NULL for wildcard (any UUID)
634 * @pin: PIN (Device Password)
635 * @pin_len: Length of pin in octets
636 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
637 * Returns: 0 on success, -1 on failure
639 int wps_registrar_add_pin(struct wps_registrar
*reg
, const u8
*addr
,
640 const u8
*uuid
, const u8
*pin
, size_t pin_len
,
643 struct wps_uuid_pin
*p
;
645 p
= os_zalloc(sizeof(*p
));
649 os_memcpy(p
->enrollee_addr
, addr
, ETH_ALEN
);
651 p
->wildcard_uuid
= 1;
653 os_memcpy(p
->uuid
, uuid
, WPS_UUID_LEN
);
654 p
->pin
= os_malloc(pin_len
);
655 if (p
->pin
== NULL
) {
659 os_memcpy(p
->pin
, pin
, pin_len
);
660 p
->pin_len
= pin_len
;
663 p
->flags
|= PIN_EXPIRES
;
664 os_get_time(&p
->expiration
);
665 p
->expiration
.sec
+= timeout
;
668 dl_list_add(®
->pins
, &p
->list
);
670 wpa_printf(MSG_DEBUG
, "WPS: A new PIN configured (timeout=%d)",
672 wpa_hexdump(MSG_DEBUG
, "WPS: UUID", uuid
, WPS_UUID_LEN
);
673 wpa_hexdump_ascii_key(MSG_DEBUG
, "WPS: PIN", pin
, pin_len
);
674 reg
->selected_registrar
= 1;
677 wps_registrar_add_authorized_mac(reg
, addr
);
679 wps_registrar_add_authorized_mac(
680 reg
, (u8
*) "\xff\xff\xff\xff\xff\xff");
681 wps_registrar_selected_registrar_changed(reg
);
682 eloop_cancel_timeout(wps_registrar_set_selected_timeout
, reg
, NULL
);
683 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0,
684 wps_registrar_set_selected_timeout
,
691 static void wps_registrar_remove_pin(struct wps_registrar
*reg
,
692 struct wps_uuid_pin
*pin
)
695 u8 bcast
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
697 if (is_zero_ether_addr(pin
->enrollee_addr
))
700 addr
= pin
->enrollee_addr
;
701 wps_registrar_remove_authorized_mac(reg
, addr
);
703 wps_registrar_selected_registrar_changed(reg
);
707 static void wps_registrar_expire_pins(struct wps_registrar
*reg
)
709 struct wps_uuid_pin
*pin
, *prev
;
713 dl_list_for_each_safe(pin
, prev
, ®
->pins
, struct wps_uuid_pin
, list
)
715 if ((pin
->flags
& PIN_EXPIRES
) &&
716 os_time_before(&pin
->expiration
, &now
)) {
717 wpa_hexdump(MSG_DEBUG
, "WPS: Expired PIN for UUID",
718 pin
->uuid
, WPS_UUID_LEN
);
719 wps_registrar_remove_pin(reg
, pin
);
726 * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
727 * @reg: Registrar data from wps_registrar_init()
728 * Returns: 0 on success, -1 if not wildcard PIN is enabled
730 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar
*reg
)
732 struct wps_uuid_pin
*pin
, *prev
;
734 dl_list_for_each_safe(pin
, prev
, ®
->pins
, struct wps_uuid_pin
, list
)
736 if (pin
->wildcard_uuid
) {
737 wpa_hexdump(MSG_DEBUG
, "WPS: Invalidated PIN for UUID",
738 pin
->uuid
, WPS_UUID_LEN
);
739 wps_registrar_remove_pin(reg
, pin
);
749 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
750 * @reg: Registrar data from wps_registrar_init()
752 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
754 int wps_registrar_invalidate_pin(struct wps_registrar
*reg
, const u8
*uuid
)
756 struct wps_uuid_pin
*pin
, *prev
;
758 dl_list_for_each_safe(pin
, prev
, ®
->pins
, struct wps_uuid_pin
, list
)
760 if (os_memcmp(pin
->uuid
, uuid
, WPS_UUID_LEN
) == 0) {
761 wpa_hexdump(MSG_DEBUG
, "WPS: Invalidated PIN for UUID",
762 pin
->uuid
, WPS_UUID_LEN
);
763 wps_registrar_remove_pin(reg
, pin
);
772 static const u8
* wps_registrar_get_pin(struct wps_registrar
*reg
,
773 const u8
*uuid
, size_t *pin_len
)
775 struct wps_uuid_pin
*pin
, *found
= NULL
;
777 wps_registrar_expire_pins(reg
);
779 dl_list_for_each(pin
, ®
->pins
, struct wps_uuid_pin
, list
) {
780 if (!pin
->wildcard_uuid
&&
781 os_memcmp(pin
->uuid
, uuid
, WPS_UUID_LEN
) == 0) {
788 /* Check for wildcard UUIDs since none of the UUID-specific
790 dl_list_for_each(pin
, ®
->pins
, struct wps_uuid_pin
, list
) {
791 if (pin
->wildcard_uuid
== 1) {
792 wpa_printf(MSG_DEBUG
, "WPS: Found a wildcard "
793 "PIN. Assigned it for this UUID-E");
794 pin
->wildcard_uuid
= 2;
795 os_memcpy(pin
->uuid
, uuid
, WPS_UUID_LEN
);
806 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
807 * that could otherwise avoid PIN invalidations.
809 if (found
->flags
& PIN_LOCKED
) {
810 wpa_printf(MSG_DEBUG
, "WPS: Selected PIN locked - do not "
811 "allow concurrent re-use");
814 *pin_len
= found
->pin_len
;
815 found
->flags
|= PIN_LOCKED
;
821 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
822 * @reg: Registrar data from wps_registrar_init()
824 * Returns: 0 on success, -1 on failure
826 * PINs are locked to enforce only one concurrent use. This function unlocks a
827 * PIN to allow it to be used again. If the specified PIN was configured using
828 * a wildcard UUID, it will be removed instead of allowing multiple uses.
830 int wps_registrar_unlock_pin(struct wps_registrar
*reg
, const u8
*uuid
)
832 struct wps_uuid_pin
*pin
;
834 dl_list_for_each(pin
, ®
->pins
, struct wps_uuid_pin
, list
) {
835 if (os_memcmp(pin
->uuid
, uuid
, WPS_UUID_LEN
) == 0) {
836 if (pin
->wildcard_uuid
== 2) {
837 wpa_printf(MSG_DEBUG
, "WPS: Invalidating used "
839 return wps_registrar_invalidate_pin(reg
, uuid
);
841 pin
->flags
&= ~PIN_LOCKED
;
850 static void wps_registrar_stop_pbc(struct wps_registrar
*reg
)
852 reg
->selected_registrar
= 0;
854 os_memset(reg
->p2p_dev_addr
, 0, ETH_ALEN
);
855 wps_registrar_remove_authorized_mac(reg
,
856 (u8
*) "\xff\xff\xff\xff\xff\xff");
857 wps_registrar_selected_registrar_changed(reg
);
861 static void wps_registrar_pbc_timeout(void *eloop_ctx
, void *timeout_ctx
)
863 struct wps_registrar
*reg
= eloop_ctx
;
865 wpa_printf(MSG_DEBUG
, "WPS: PBC timed out - disable PBC mode");
866 wps_pbc_timeout_event(reg
->wps
);
867 wps_registrar_stop_pbc(reg
);
872 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
873 * @reg: Registrar data from wps_registrar_init()
874 * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
875 * indicates no such filtering
876 * Returns: 0 on success, -1 on failure, -2 on session overlap
878 * This function is called on an AP when a push button is pushed to activate
879 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
880 * or when a PBC registration is completed. If more than one Enrollee in active
881 * PBC mode has been detected during the monitor time (previous 2 minutes), the
882 * PBC mode is not actived and -2 is returned to indicate session overlap. This
883 * is skipped if a specific Enrollee is selected.
885 int wps_registrar_button_pushed(struct wps_registrar
*reg
,
886 const u8
*p2p_dev_addr
)
888 if (p2p_dev_addr
== NULL
&&
889 wps_registrar_pbc_overlap(reg
, NULL
, NULL
)) {
890 wpa_printf(MSG_DEBUG
, "WPS: PBC overlap - do not start PBC "
892 wps_pbc_overlap_event(reg
->wps
);
895 wpa_printf(MSG_DEBUG
, "WPS: Button pushed - PBC mode started");
896 reg
->force_pbc_overlap
= 0;
897 reg
->selected_registrar
= 1;
900 os_memcpy(reg
->p2p_dev_addr
, p2p_dev_addr
, ETH_ALEN
);
902 os_memset(reg
->p2p_dev_addr
, 0, ETH_ALEN
);
903 wps_registrar_add_authorized_mac(reg
,
904 (u8
*) "\xff\xff\xff\xff\xff\xff");
905 wps_registrar_selected_registrar_changed(reg
);
907 eloop_cancel_timeout(wps_registrar_set_selected_timeout
, reg
, NULL
);
908 eloop_cancel_timeout(wps_registrar_pbc_timeout
, reg
, NULL
);
909 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0, wps_registrar_pbc_timeout
,
915 static void wps_registrar_pbc_completed(struct wps_registrar
*reg
)
917 wpa_printf(MSG_DEBUG
, "WPS: PBC completed - stopping PBC mode");
918 eloop_cancel_timeout(wps_registrar_pbc_timeout
, reg
, NULL
);
919 wps_registrar_stop_pbc(reg
);
923 static void wps_registrar_pin_completed(struct wps_registrar
*reg
)
925 wpa_printf(MSG_DEBUG
, "WPS: PIN completed using internal Registrar");
926 eloop_cancel_timeout(wps_registrar_set_selected_timeout
, reg
, NULL
);
927 reg
->selected_registrar
= 0;
928 wps_registrar_selected_registrar_changed(reg
);
932 int wps_registrar_wps_cancel(struct wps_registrar
*reg
)
935 wpa_printf(MSG_DEBUG
, "WPS: PBC is set - cancelling it");
936 wps_registrar_pbc_timeout(reg
, NULL
);
938 } else if (reg
->selected_registrar
) {
940 wpa_printf(MSG_DEBUG
, "WPS: PIN is set - cancelling it");
941 wps_registrar_pin_completed(reg
);
942 wps_registrar_invalidate_wildcard_pin(reg
);
950 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
951 * @reg: Registrar data from wps_registrar_init()
952 * @addr: MAC address of the Probe Request sender
953 * @wps_data: WPS IE contents
955 * This function is called on an AP when a Probe Request with WPS IE is
956 * received. This is used to track PBC mode use and to detect possible overlap
957 * situation with other WPS APs.
959 void wps_registrar_probe_req_rx(struct wps_registrar
*reg
, const u8
*addr
,
960 const struct wpabuf
*wps_data
,
963 struct wps_parse_attr attr
;
965 wpa_hexdump_buf(MSG_MSGDUMP
,
966 "WPS: Probe Request with WPS data received",
969 if (wps_parse_msg(wps_data
, &attr
) < 0)
972 if (attr
.config_methods
== NULL
) {
973 wpa_printf(MSG_DEBUG
, "WPS: No Config Methods attribute in "
978 if (attr
.dev_password_id
== NULL
) {
979 wpa_printf(MSG_DEBUG
, "WPS: No Device Password Id attribute "
984 if (reg
->enrollee_seen_cb
&& attr
.uuid_e
&&
985 attr
.primary_dev_type
&& attr
.request_type
&& !p2p_wildcard
) {
986 char *dev_name
= NULL
;
988 dev_name
= os_zalloc(attr
.dev_name_len
+ 1);
990 os_memcpy(dev_name
, attr
.dev_name
,
994 reg
->enrollee_seen_cb(reg
->cb_ctx
, addr
, attr
.uuid_e
,
995 attr
.primary_dev_type
,
996 WPA_GET_BE16(attr
.config_methods
),
997 WPA_GET_BE16(attr
.dev_password_id
),
998 *attr
.request_type
, dev_name
);
1002 if (WPA_GET_BE16(attr
.dev_password_id
) != DEV_PW_PUSHBUTTON
)
1003 return; /* Not PBC */
1005 wpa_printf(MSG_DEBUG
, "WPS: Probe Request for PBC received from "
1006 MACSTR
, MAC2STR(addr
));
1007 if (attr
.uuid_e
== NULL
) {
1008 wpa_printf(MSG_DEBUG
, "WPS: Invalid Probe Request WPS IE: No "
1012 wpa_hexdump(MSG_DEBUG
, "WPS: UUID-E from Probe Request", attr
.uuid_e
,
1015 wps_registrar_add_pbc_session(reg
, addr
, attr
.uuid_e
);
1016 if (wps_registrar_pbc_overlap(reg
, addr
, attr
.uuid_e
)) {
1017 wpa_printf(MSG_DEBUG
, "WPS: PBC session overlap detected");
1018 reg
->force_pbc_overlap
= 1;
1019 wps_pbc_overlap_event(reg
->wps
);
1024 static int wps_cb_new_psk(struct wps_registrar
*reg
, const u8
*mac_addr
,
1025 const u8
*psk
, size_t psk_len
)
1027 if (reg
->new_psk_cb
== NULL
)
1030 return reg
->new_psk_cb(reg
->cb_ctx
, mac_addr
, psk
, psk_len
);
1034 static void wps_cb_pin_needed(struct wps_registrar
*reg
, const u8
*uuid_e
,
1035 const struct wps_device_data
*dev
)
1037 if (reg
->pin_needed_cb
== NULL
)
1040 reg
->pin_needed_cb(reg
->cb_ctx
, uuid_e
, dev
);
1044 static void wps_cb_reg_success(struct wps_registrar
*reg
, const u8
*mac_addr
,
1047 if (reg
->reg_success_cb
== NULL
)
1050 reg
->reg_success_cb(reg
->cb_ctx
, mac_addr
, uuid_e
);
1054 static int wps_cb_set_ie(struct wps_registrar
*reg
, struct wpabuf
*beacon_ie
,
1055 struct wpabuf
*probe_resp_ie
)
1057 return reg
->set_ie_cb(reg
->cb_ctx
, beacon_ie
, probe_resp_ie
);
1061 static void wps_cb_set_sel_reg(struct wps_registrar
*reg
)
1064 if (reg
->set_sel_reg_cb
== NULL
)
1067 if (reg
->selected_registrar
) {
1068 methods
= reg
->wps
->config_methods
& ~WPS_CONFIG_PUSHBUTTON
;
1070 methods
&= ~(WPS_CONFIG_VIRT_PUSHBUTTON
|
1071 WPS_CONFIG_PHY_PUSHBUTTON
);
1072 #endif /* CONFIG_WPS2 */
1074 wps_set_pushbutton(&methods
, reg
->wps
->config_methods
);
1077 wpa_printf(MSG_DEBUG
, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1078 "config_methods=0x%x pbc=%d methods=0x%x",
1079 reg
->selected_registrar
, reg
->wps
->config_methods
,
1082 reg
->set_sel_reg_cb(reg
->cb_ctx
, reg
->selected_registrar
,
1083 reg
->pbc
? DEV_PW_PUSHBUTTON
: DEV_PW_DEFAULT
,
1088 static int wps_set_ie(struct wps_registrar
*reg
)
1090 struct wpabuf
*beacon
;
1091 struct wpabuf
*probe
;
1092 const u8
*auth_macs
;
1095 if (reg
->set_ie_cb
== NULL
)
1098 beacon
= wpabuf_alloc(400);
1101 probe
= wpabuf_alloc(500);
1102 if (probe
== NULL
) {
1103 wpabuf_free(beacon
);
1107 auth_macs
= wps_authorized_macs(reg
, &count
);
1109 wpa_printf(MSG_DEBUG
, "WPS: Build Beacon IEs");
1111 if (wps_build_version(beacon
) ||
1112 wps_build_wps_state(reg
->wps
, beacon
) ||
1113 wps_build_ap_setup_locked(reg
->wps
, beacon
) ||
1114 wps_build_selected_registrar(reg
, beacon
) ||
1115 wps_build_sel_reg_dev_password_id(reg
, beacon
) ||
1116 wps_build_sel_reg_config_methods(reg
, beacon
) ||
1117 wps_build_sel_pbc_reg_uuid_e(reg
, beacon
) ||
1118 (reg
->dualband
&& wps_build_rf_bands(®
->wps
->dev
, beacon
)) ||
1119 wps_build_wfa_ext(beacon
, 0, auth_macs
, count
)) {
1120 wpabuf_free(beacon
);
1125 wpa_printf(MSG_DEBUG
, "WPS: Build Probe Response IEs");
1127 if (wps_build_version(probe
) ||
1128 wps_build_wps_state(reg
->wps
, probe
) ||
1129 wps_build_ap_setup_locked(reg
->wps
, probe
) ||
1130 wps_build_selected_registrar(reg
, probe
) ||
1131 wps_build_sel_reg_dev_password_id(reg
, probe
) ||
1132 wps_build_sel_reg_config_methods(reg
, probe
) ||
1133 wps_build_resp_type(probe
, reg
->wps
->ap
? WPS_RESP_AP
:
1134 WPS_RESP_REGISTRAR
) ||
1135 wps_build_uuid_e(probe
, reg
->wps
->uuid
) ||
1136 wps_build_device_attrs(®
->wps
->dev
, probe
) ||
1137 wps_build_probe_config_methods(reg
, probe
) ||
1138 wps_build_rf_bands(®
->wps
->dev
, probe
) ||
1139 wps_build_wfa_ext(probe
, 0, auth_macs
, count
)) {
1140 wpabuf_free(beacon
);
1146 if (wps_build_dev_name(®
->wps
->dev
, beacon
) ||
1147 wps_build_primary_dev_type(®
->wps
->dev
, beacon
)) {
1148 wpabuf_free(beacon
);
1152 #endif /* CONFIG_P2P */
1154 beacon
= wps_ie_encapsulate(beacon
);
1155 probe
= wps_ie_encapsulate(probe
);
1157 if (!beacon
|| !probe
) {
1158 wpabuf_free(beacon
);
1163 if (reg
->static_wep_only
) {
1165 * Windows XP and Vista clients can get confused about
1166 * EAP-Identity/Request when they probe the network with
1167 * EAPOL-Start. In such a case, they may assume the network is
1168 * using IEEE 802.1X and prompt user for a certificate while
1169 * the correct (non-WPS) behavior would be to ask for the
1170 * static WEP key. As a workaround, use Microsoft Provisioning
1171 * IE to advertise that legacy 802.1X is not supported.
1173 const u8 ms_wps
[7] = {
1174 WLAN_EID_VENDOR_SPECIFIC
, 5,
1175 /* Microsoft Provisioning IE (00:50:f2:5) */
1176 0x00, 0x50, 0xf2, 5,
1177 0x00 /* no legacy 802.1X or MS WPS */
1179 wpa_printf(MSG_DEBUG
, "WPS: Add Microsoft Provisioning IE "
1180 "into Beacon/Probe Response frames");
1181 wpabuf_put_data(beacon
, ms_wps
, sizeof(ms_wps
));
1182 wpabuf_put_data(probe
, ms_wps
, sizeof(ms_wps
));
1185 return wps_cb_set_ie(reg
, beacon
, probe
);
1189 static int wps_get_dev_password(struct wps_data
*wps
)
1194 os_free(wps
->dev_password
);
1195 wps
->dev_password
= NULL
;
1198 wpa_printf(MSG_DEBUG
, "WPS: Use default PIN for PBC");
1199 pin
= (const u8
*) "00000000";
1202 pin
= wps_registrar_get_pin(wps
->wps
->registrar
, wps
->uuid_e
,
1206 wpa_printf(MSG_DEBUG
, "WPS: No Device Password available for "
1208 wps_cb_pin_needed(wps
->wps
->registrar
, wps
->uuid_e
,
1213 wps
->dev_password
= os_malloc(pin_len
);
1214 if (wps
->dev_password
== NULL
)
1216 os_memcpy(wps
->dev_password
, pin
, pin_len
);
1217 wps
->dev_password_len
= pin_len
;
1223 static int wps_build_uuid_r(struct wps_data
*wps
, struct wpabuf
*msg
)
1225 wpa_printf(MSG_DEBUG
, "WPS: * UUID-R");
1226 wpabuf_put_be16(msg
, ATTR_UUID_R
);
1227 wpabuf_put_be16(msg
, WPS_UUID_LEN
);
1228 wpabuf_put_data(msg
, wps
->uuid_r
, WPS_UUID_LEN
);
1233 static int wps_build_r_hash(struct wps_data
*wps
, struct wpabuf
*msg
)
1239 if (random_get_bytes(wps
->snonce
, 2 * WPS_SECRET_NONCE_LEN
) < 0)
1241 wpa_hexdump(MSG_DEBUG
, "WPS: R-S1", wps
->snonce
, WPS_SECRET_NONCE_LEN
);
1242 wpa_hexdump(MSG_DEBUG
, "WPS: R-S2",
1243 wps
->snonce
+ WPS_SECRET_NONCE_LEN
, WPS_SECRET_NONCE_LEN
);
1245 if (wps
->dh_pubkey_e
== NULL
|| wps
->dh_pubkey_r
== NULL
) {
1246 wpa_printf(MSG_DEBUG
, "WPS: DH public keys not available for "
1247 "R-Hash derivation");
1251 wpa_printf(MSG_DEBUG
, "WPS: * R-Hash1");
1252 wpabuf_put_be16(msg
, ATTR_R_HASH1
);
1253 wpabuf_put_be16(msg
, SHA256_MAC_LEN
);
1254 hash
= wpabuf_put(msg
, SHA256_MAC_LEN
);
1255 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1256 addr
[0] = wps
->snonce
;
1257 len
[0] = WPS_SECRET_NONCE_LEN
;
1258 addr
[1] = wps
->psk1
;
1259 len
[1] = WPS_PSK_LEN
;
1260 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
1261 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
1262 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
1263 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
1264 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
1265 wpa_hexdump(MSG_DEBUG
, "WPS: R-Hash1", hash
, SHA256_MAC_LEN
);
1267 wpa_printf(MSG_DEBUG
, "WPS: * R-Hash2");
1268 wpabuf_put_be16(msg
, ATTR_R_HASH2
);
1269 wpabuf_put_be16(msg
, SHA256_MAC_LEN
);
1270 hash
= wpabuf_put(msg
, SHA256_MAC_LEN
);
1271 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1272 addr
[0] = wps
->snonce
+ WPS_SECRET_NONCE_LEN
;
1273 addr
[1] = wps
->psk2
;
1274 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
1275 wpa_hexdump(MSG_DEBUG
, "WPS: R-Hash2", hash
, SHA256_MAC_LEN
);
1281 static int wps_build_r_snonce1(struct wps_data
*wps
, struct wpabuf
*msg
)
1283 wpa_printf(MSG_DEBUG
, "WPS: * R-SNonce1");
1284 wpabuf_put_be16(msg
, ATTR_R_SNONCE1
);
1285 wpabuf_put_be16(msg
, WPS_SECRET_NONCE_LEN
);
1286 wpabuf_put_data(msg
, wps
->snonce
, WPS_SECRET_NONCE_LEN
);
1291 static int wps_build_r_snonce2(struct wps_data
*wps
, struct wpabuf
*msg
)
1293 wpa_printf(MSG_DEBUG
, "WPS: * R-SNonce2");
1294 wpabuf_put_be16(msg
, ATTR_R_SNONCE2
);
1295 wpabuf_put_be16(msg
, WPS_SECRET_NONCE_LEN
);
1296 wpabuf_put_data(msg
, wps
->snonce
+ WPS_SECRET_NONCE_LEN
,
1297 WPS_SECRET_NONCE_LEN
);
1302 static int wps_build_cred_network_idx(struct wpabuf
*msg
,
1303 const struct wps_credential
*cred
)
1305 wpa_printf(MSG_DEBUG
, "WPS: * Network Index (1)");
1306 wpabuf_put_be16(msg
, ATTR_NETWORK_INDEX
);
1307 wpabuf_put_be16(msg
, 1);
1308 wpabuf_put_u8(msg
, 1);
1313 static int wps_build_cred_ssid(struct wpabuf
*msg
,
1314 const struct wps_credential
*cred
)
1316 wpa_printf(MSG_DEBUG
, "WPS: * SSID");
1317 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: SSID for Credential",
1318 cred
->ssid
, cred
->ssid_len
);
1319 wpabuf_put_be16(msg
, ATTR_SSID
);
1320 wpabuf_put_be16(msg
, cred
->ssid_len
);
1321 wpabuf_put_data(msg
, cred
->ssid
, cred
->ssid_len
);
1326 static int wps_build_cred_auth_type(struct wpabuf
*msg
,
1327 const struct wps_credential
*cred
)
1329 wpa_printf(MSG_DEBUG
, "WPS: * Authentication Type (0x%x)",
1331 wpabuf_put_be16(msg
, ATTR_AUTH_TYPE
);
1332 wpabuf_put_be16(msg
, 2);
1333 wpabuf_put_be16(msg
, cred
->auth_type
);
1338 static int wps_build_cred_encr_type(struct wpabuf
*msg
,
1339 const struct wps_credential
*cred
)
1341 wpa_printf(MSG_DEBUG
, "WPS: * Encryption Type (0x%x)",
1343 wpabuf_put_be16(msg
, ATTR_ENCR_TYPE
);
1344 wpabuf_put_be16(msg
, 2);
1345 wpabuf_put_be16(msg
, cred
->encr_type
);
1350 static int wps_build_cred_network_key(struct wpabuf
*msg
,
1351 const struct wps_credential
*cred
)
1353 wpa_printf(MSG_DEBUG
, "WPS: * Network Key (len=%d)",
1354 (int) cred
->key_len
);
1355 wpa_hexdump_key(MSG_DEBUG
, "WPS: Network Key",
1356 cred
->key
, cred
->key_len
);
1357 wpabuf_put_be16(msg
, ATTR_NETWORK_KEY
);
1358 wpabuf_put_be16(msg
, cred
->key_len
);
1359 wpabuf_put_data(msg
, cred
->key
, cred
->key_len
);
1364 static int wps_build_cred_mac_addr(struct wpabuf
*msg
,
1365 const struct wps_credential
*cred
)
1367 wpa_printf(MSG_DEBUG
, "WPS: * MAC Address (" MACSTR
")",
1368 MAC2STR(cred
->mac_addr
));
1369 wpabuf_put_be16(msg
, ATTR_MAC_ADDR
);
1370 wpabuf_put_be16(msg
, ETH_ALEN
);
1371 wpabuf_put_data(msg
, cred
->mac_addr
, ETH_ALEN
);
1376 static int wps_build_credential(struct wpabuf
*msg
,
1377 const struct wps_credential
*cred
)
1379 if (wps_build_cred_network_idx(msg
, cred
) ||
1380 wps_build_cred_ssid(msg
, cred
) ||
1381 wps_build_cred_auth_type(msg
, cred
) ||
1382 wps_build_cred_encr_type(msg
, cred
) ||
1383 wps_build_cred_network_key(msg
, cred
) ||
1384 wps_build_cred_mac_addr(msg
, cred
))
1390 int wps_build_cred(struct wps_data
*wps
, struct wpabuf
*msg
)
1392 struct wpabuf
*cred
;
1394 if (wps
->wps
->registrar
->skip_cred_build
)
1395 goto skip_cred_build
;
1397 wpa_printf(MSG_DEBUG
, "WPS: * Credential");
1398 if (wps
->use_cred
) {
1399 os_memcpy(&wps
->cred
, wps
->use_cred
, sizeof(wps
->cred
));
1402 os_memset(&wps
->cred
, 0, sizeof(wps
->cred
));
1404 os_memcpy(wps
->cred
.ssid
, wps
->wps
->ssid
, wps
->wps
->ssid_len
);
1405 wps
->cred
.ssid_len
= wps
->wps
->ssid_len
;
1407 /* Select the best authentication and encryption type */
1408 if (wps
->auth_type
& WPS_AUTH_WPA2PSK
)
1409 wps
->auth_type
= WPS_AUTH_WPA2PSK
;
1410 else if (wps
->auth_type
& WPS_AUTH_WPAPSK
)
1411 wps
->auth_type
= WPS_AUTH_WPAPSK
;
1412 else if (wps
->auth_type
& WPS_AUTH_OPEN
)
1413 wps
->auth_type
= WPS_AUTH_OPEN
;
1414 else if (wps
->auth_type
& WPS_AUTH_SHARED
)
1415 wps
->auth_type
= WPS_AUTH_SHARED
;
1417 wpa_printf(MSG_DEBUG
, "WPS: Unsupported auth_type 0x%x",
1421 wps
->cred
.auth_type
= wps
->auth_type
;
1423 if (wps
->auth_type
== WPS_AUTH_WPA2PSK
||
1424 wps
->auth_type
== WPS_AUTH_WPAPSK
) {
1425 if (wps
->encr_type
& WPS_ENCR_AES
)
1426 wps
->encr_type
= WPS_ENCR_AES
;
1427 else if (wps
->encr_type
& WPS_ENCR_TKIP
)
1428 wps
->encr_type
= WPS_ENCR_TKIP
;
1430 wpa_printf(MSG_DEBUG
, "WPS: No suitable encryption "
1431 "type for WPA/WPA2");
1435 if (wps
->encr_type
& WPS_ENCR_WEP
)
1436 wps
->encr_type
= WPS_ENCR_WEP
;
1437 else if (wps
->encr_type
& WPS_ENCR_NONE
)
1438 wps
->encr_type
= WPS_ENCR_NONE
;
1440 wpa_printf(MSG_DEBUG
, "WPS: No suitable encryption "
1441 "type for non-WPA/WPA2 mode");
1445 wps
->cred
.encr_type
= wps
->encr_type
;
1447 * Set MAC address in the Credential to be the Enrollee's MAC address
1449 os_memcpy(wps
->cred
.mac_addr
, wps
->mac_addr_e
, ETH_ALEN
);
1451 if (wps
->wps
->wps_state
== WPS_STATE_NOT_CONFIGURED
&& wps
->wps
->ap
&&
1452 !wps
->wps
->registrar
->disable_auto_conf
) {
1454 /* Generate a random passphrase */
1455 if (random_get_bytes(r
, sizeof(r
)) < 0)
1457 os_free(wps
->new_psk
);
1458 wps
->new_psk
= base64_encode(r
, sizeof(r
), &wps
->new_psk_len
);
1459 if (wps
->new_psk
== NULL
)
1461 wps
->new_psk_len
--; /* remove newline */
1462 while (wps
->new_psk_len
&&
1463 wps
->new_psk
[wps
->new_psk_len
- 1] == '=')
1465 wpa_hexdump_ascii_key(MSG_DEBUG
, "WPS: Generated passphrase",
1466 wps
->new_psk
, wps
->new_psk_len
);
1467 os_memcpy(wps
->cred
.key
, wps
->new_psk
, wps
->new_psk_len
);
1468 wps
->cred
.key_len
= wps
->new_psk_len
;
1469 } else if (wps
->use_psk_key
&& wps
->wps
->psk_set
) {
1471 wpa_printf(MSG_DEBUG
, "WPS: Use PSK format for Network Key");
1472 wpa_snprintf_hex(hex
, sizeof(hex
), wps
->wps
->psk
, 32);
1473 os_memcpy(wps
->cred
.key
, hex
, 32 * 2);
1474 wps
->cred
.key_len
= 32 * 2;
1475 } else if (wps
->wps
->network_key
) {
1476 os_memcpy(wps
->cred
.key
, wps
->wps
->network_key
,
1477 wps
->wps
->network_key_len
);
1478 wps
->cred
.key_len
= wps
->wps
->network_key_len
;
1479 } else if (wps
->auth_type
& (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
)) {
1481 /* Generate a random per-device PSK */
1482 os_free(wps
->new_psk
);
1483 wps
->new_psk_len
= 32;
1484 wps
->new_psk
= os_malloc(wps
->new_psk_len
);
1485 if (wps
->new_psk
== NULL
)
1487 if (random_get_bytes(wps
->new_psk
, wps
->new_psk_len
) < 0) {
1488 os_free(wps
->new_psk
);
1489 wps
->new_psk
= NULL
;
1492 wpa_hexdump_key(MSG_DEBUG
, "WPS: Generated per-device PSK",
1493 wps
->new_psk
, wps
->new_psk_len
);
1494 wpa_snprintf_hex(hex
, sizeof(hex
), wps
->new_psk
,
1496 os_memcpy(wps
->cred
.key
, hex
, wps
->new_psk_len
* 2);
1497 wps
->cred
.key_len
= wps
->new_psk_len
* 2;
1501 #ifdef CONFIG_WPS_TESTING
1502 if (wps_testing_dummy_cred
)
1503 cred
= wpabuf_alloc(200);
1507 struct wps_credential dummy
;
1508 wpa_printf(MSG_DEBUG
, "WPS: Add dummy credential");
1509 os_memset(&dummy
, 0, sizeof(dummy
));
1510 os_memcpy(dummy
.ssid
, "dummy", 5);
1512 dummy
.auth_type
= WPS_AUTH_WPA2PSK
;
1513 dummy
.encr_type
= WPS_ENCR_AES
;
1514 os_memcpy(dummy
.key
, "dummy psk", 9);
1516 os_memcpy(dummy
.mac_addr
, wps
->mac_addr_e
, ETH_ALEN
);
1517 wps_build_credential(cred
, &dummy
);
1518 wpa_hexdump_buf(MSG_DEBUG
, "WPS: Dummy Credential", cred
);
1520 wpabuf_put_be16(msg
, ATTR_CRED
);
1521 wpabuf_put_be16(msg
, wpabuf_len(cred
));
1522 wpabuf_put_buf(msg
, cred
);
1526 #endif /* CONFIG_WPS_TESTING */
1528 cred
= wpabuf_alloc(200);
1532 if (wps_build_credential(cred
, &wps
->cred
)) {
1537 wpabuf_put_be16(msg
, ATTR_CRED
);
1538 wpabuf_put_be16(msg
, wpabuf_len(cred
));
1539 wpabuf_put_buf(msg
, cred
);
1543 if (wps
->wps
->registrar
->extra_cred
) {
1544 wpa_printf(MSG_DEBUG
, "WPS: * Credential (pre-configured)");
1545 wpabuf_put_buf(msg
, wps
->wps
->registrar
->extra_cred
);
1552 static int wps_build_ap_settings(struct wps_data
*wps
, struct wpabuf
*msg
)
1554 wpa_printf(MSG_DEBUG
, "WPS: * AP Settings");
1556 if (wps_build_credential(msg
, &wps
->cred
))
1563 static struct wpabuf
* wps_build_m2(struct wps_data
*wps
)
1567 if (random_get_bytes(wps
->nonce_r
, WPS_NONCE_LEN
) < 0)
1569 wpa_hexdump(MSG_DEBUG
, "WPS: Registrar Nonce",
1570 wps
->nonce_r
, WPS_NONCE_LEN
);
1571 wpa_hexdump(MSG_DEBUG
, "WPS: UUID-R", wps
->uuid_r
, WPS_UUID_LEN
);
1573 wpa_printf(MSG_DEBUG
, "WPS: Building Message M2");
1574 msg
= wpabuf_alloc(1000);
1578 if (wps_build_version(msg
) ||
1579 wps_build_msg_type(msg
, WPS_M2
) ||
1580 wps_build_enrollee_nonce(wps
, msg
) ||
1581 wps_build_registrar_nonce(wps
, msg
) ||
1582 wps_build_uuid_r(wps
, msg
) ||
1583 wps_build_public_key(wps
, msg
) ||
1584 wps_derive_keys(wps
) ||
1585 wps_build_auth_type_flags(wps
, msg
) ||
1586 wps_build_encr_type_flags(wps
, msg
) ||
1587 wps_build_conn_type_flags(wps
, msg
) ||
1588 wps_build_config_methods_r(wps
->wps
->registrar
, msg
) ||
1589 wps_build_device_attrs(&wps
->wps
->dev
, msg
) ||
1590 wps_build_rf_bands(&wps
->wps
->dev
, msg
) ||
1591 wps_build_assoc_state(wps
, msg
) ||
1592 wps_build_config_error(msg
, WPS_CFG_NO_ERROR
) ||
1593 wps_build_dev_password_id(msg
, wps
->dev_pw_id
) ||
1594 wps_build_os_version(&wps
->wps
->dev
, msg
) ||
1595 wps_build_wfa_ext(msg
, 0, NULL
, 0) ||
1596 wps_build_authenticator(wps
, msg
)) {
1602 wps
->state
= RECV_M3
;
1607 static struct wpabuf
* wps_build_m2d(struct wps_data
*wps
)
1610 u16 err
= wps
->config_error
;
1612 wpa_printf(MSG_DEBUG
, "WPS: Building Message M2D");
1613 msg
= wpabuf_alloc(1000);
1617 if (wps
->wps
->ap
&& wps
->wps
->ap_setup_locked
&&
1618 err
== WPS_CFG_NO_ERROR
)
1619 err
= WPS_CFG_SETUP_LOCKED
;
1621 if (wps_build_version(msg
) ||
1622 wps_build_msg_type(msg
, WPS_M2D
) ||
1623 wps_build_enrollee_nonce(wps
, msg
) ||
1624 wps_build_registrar_nonce(wps
, msg
) ||
1625 wps_build_uuid_r(wps
, msg
) ||
1626 wps_build_auth_type_flags(wps
, msg
) ||
1627 wps_build_encr_type_flags(wps
, msg
) ||
1628 wps_build_conn_type_flags(wps
, msg
) ||
1629 wps_build_config_methods_r(wps
->wps
->registrar
, msg
) ||
1630 wps_build_device_attrs(&wps
->wps
->dev
, msg
) ||
1631 wps_build_rf_bands(&wps
->wps
->dev
, msg
) ||
1632 wps_build_assoc_state(wps
, msg
) ||
1633 wps_build_config_error(msg
, err
) ||
1634 wps_build_os_version(&wps
->wps
->dev
, msg
) ||
1635 wps_build_wfa_ext(msg
, 0, NULL
, 0)) {
1640 wps
->state
= RECV_M2D_ACK
;
1645 static struct wpabuf
* wps_build_m4(struct wps_data
*wps
)
1647 struct wpabuf
*msg
, *plain
;
1649 wpa_printf(MSG_DEBUG
, "WPS: Building Message M4");
1651 wps_derive_psk(wps
, wps
->dev_password
, wps
->dev_password_len
);
1653 plain
= wpabuf_alloc(200);
1657 msg
= wpabuf_alloc(1000);
1663 if (wps_build_version(msg
) ||
1664 wps_build_msg_type(msg
, WPS_M4
) ||
1665 wps_build_enrollee_nonce(wps
, msg
) ||
1666 wps_build_r_hash(wps
, msg
) ||
1667 wps_build_r_snonce1(wps
, plain
) ||
1668 wps_build_key_wrap_auth(wps
, plain
) ||
1669 wps_build_encr_settings(wps
, msg
, plain
) ||
1670 wps_build_wfa_ext(msg
, 0, NULL
, 0) ||
1671 wps_build_authenticator(wps
, msg
)) {
1678 wps
->state
= RECV_M5
;
1683 static struct wpabuf
* wps_build_m6(struct wps_data
*wps
)
1685 struct wpabuf
*msg
, *plain
;
1687 wpa_printf(MSG_DEBUG
, "WPS: Building Message M6");
1689 plain
= wpabuf_alloc(200);
1693 msg
= wpabuf_alloc(1000);
1699 if (wps_build_version(msg
) ||
1700 wps_build_msg_type(msg
, WPS_M6
) ||
1701 wps_build_enrollee_nonce(wps
, msg
) ||
1702 wps_build_r_snonce2(wps
, plain
) ||
1703 wps_build_key_wrap_auth(wps
, plain
) ||
1704 wps_build_encr_settings(wps
, msg
, plain
) ||
1705 wps_build_wfa_ext(msg
, 0, NULL
, 0) ||
1706 wps_build_authenticator(wps
, msg
)) {
1713 wps
->wps_pin_revealed
= 1;
1714 wps
->state
= RECV_M7
;
1719 static struct wpabuf
* wps_build_m8(struct wps_data
*wps
)
1721 struct wpabuf
*msg
, *plain
;
1723 wpa_printf(MSG_DEBUG
, "WPS: Building Message M8");
1725 plain
= wpabuf_alloc(500);
1729 msg
= wpabuf_alloc(1000);
1735 if (wps_build_version(msg
) ||
1736 wps_build_msg_type(msg
, WPS_M8
) ||
1737 wps_build_enrollee_nonce(wps
, msg
) ||
1738 ((wps
->wps
->ap
|| wps
->er
) && wps_build_cred(wps
, plain
)) ||
1739 (!wps
->wps
->ap
&& !wps
->er
&& wps_build_ap_settings(wps
, plain
)) ||
1740 wps_build_key_wrap_auth(wps
, plain
) ||
1741 wps_build_encr_settings(wps
, msg
, plain
) ||
1742 wps_build_wfa_ext(msg
, 0, NULL
, 0) ||
1743 wps_build_authenticator(wps
, msg
)) {
1750 wps
->state
= RECV_DONE
;
1755 struct wpabuf
* wps_registrar_get_msg(struct wps_data
*wps
,
1756 enum wsc_op_code
*op_code
)
1760 #ifdef CONFIG_WPS_UPNP
1761 if (!wps
->int_reg
&& wps
->wps
->wps_upnp
) {
1762 struct upnp_pending_message
*p
, *prev
= NULL
;
1763 if (wps
->ext_reg
> 1)
1764 wps_registrar_free_pending_m2(wps
->wps
);
1765 p
= wps
->wps
->upnp_msgs
;
1766 /* TODO: check pending message MAC address */
1767 while (p
&& p
->next
) {
1772 wpa_printf(MSG_DEBUG
, "WPS: Use pending message from "
1777 wps
->wps
->upnp_msgs
= NULL
;
1784 *op_code
= WSC_NACK
;
1791 if (wps
->ext_reg
== 0)
1797 wpa_printf(MSG_DEBUG
, "WPS: Using external Registrar, but no "
1798 "pending message available");
1801 #endif /* CONFIG_WPS_UPNP */
1803 switch (wps
->state
) {
1805 if (wps_get_dev_password(wps
) < 0)
1806 msg
= wps_build_m2d(wps
);
1808 msg
= wps_build_m2(wps
);
1812 msg
= wps_build_m2d(wps
);
1816 msg
= wps_build_m4(wps
);
1820 msg
= wps_build_m6(wps
);
1824 msg
= wps_build_m8(wps
);
1828 msg
= wps_build_wsc_ack(wps
);
1832 msg
= wps_build_wsc_nack(wps
);
1833 *op_code
= WSC_NACK
;
1836 wpa_printf(MSG_DEBUG
, "WPS: Unsupported state %d for building "
1837 "a message", wps
->state
);
1842 if (*op_code
== WSC_MSG
&& msg
) {
1843 /* Save a copy of the last message for Authenticator derivation
1845 wpabuf_free(wps
->last_msg
);
1846 wps
->last_msg
= wpabuf_dup(msg
);
1853 static int wps_process_enrollee_nonce(struct wps_data
*wps
, const u8
*e_nonce
)
1855 if (e_nonce
== NULL
) {
1856 wpa_printf(MSG_DEBUG
, "WPS: No Enrollee Nonce received");
1860 os_memcpy(wps
->nonce_e
, e_nonce
, WPS_NONCE_LEN
);
1861 wpa_hexdump(MSG_DEBUG
, "WPS: Enrollee Nonce",
1862 wps
->nonce_e
, WPS_NONCE_LEN
);
1868 static int wps_process_registrar_nonce(struct wps_data
*wps
, const u8
*r_nonce
)
1870 if (r_nonce
== NULL
) {
1871 wpa_printf(MSG_DEBUG
, "WPS: No Registrar Nonce received");
1875 if (os_memcmp(wps
->nonce_r
, r_nonce
, WPS_NONCE_LEN
) != 0) {
1876 wpa_printf(MSG_DEBUG
, "WPS: Invalid Registrar Nonce received");
1884 static int wps_process_uuid_e(struct wps_data
*wps
, const u8
*uuid_e
)
1886 if (uuid_e
== NULL
) {
1887 wpa_printf(MSG_DEBUG
, "WPS: No UUID-E received");
1891 os_memcpy(wps
->uuid_e
, uuid_e
, WPS_UUID_LEN
);
1892 wpa_hexdump(MSG_DEBUG
, "WPS: UUID-E", wps
->uuid_e
, WPS_UUID_LEN
);
1898 static int wps_process_dev_password_id(struct wps_data
*wps
, const u8
*pw_id
)
1900 if (pw_id
== NULL
) {
1901 wpa_printf(MSG_DEBUG
, "WPS: No Device Password ID received");
1905 wps
->dev_pw_id
= WPA_GET_BE16(pw_id
);
1906 wpa_printf(MSG_DEBUG
, "WPS: Device Password ID %d", wps
->dev_pw_id
);
1912 static int wps_process_e_hash1(struct wps_data
*wps
, const u8
*e_hash1
)
1914 if (e_hash1
== NULL
) {
1915 wpa_printf(MSG_DEBUG
, "WPS: No E-Hash1 received");
1919 os_memcpy(wps
->peer_hash1
, e_hash1
, WPS_HASH_LEN
);
1920 wpa_hexdump(MSG_DEBUG
, "WPS: E-Hash1", wps
->peer_hash1
, WPS_HASH_LEN
);
1926 static int wps_process_e_hash2(struct wps_data
*wps
, const u8
*e_hash2
)
1928 if (e_hash2
== NULL
) {
1929 wpa_printf(MSG_DEBUG
, "WPS: No E-Hash2 received");
1933 os_memcpy(wps
->peer_hash2
, e_hash2
, WPS_HASH_LEN
);
1934 wpa_hexdump(MSG_DEBUG
, "WPS: E-Hash2", wps
->peer_hash2
, WPS_HASH_LEN
);
1940 static int wps_process_e_snonce1(struct wps_data
*wps
, const u8
*e_snonce1
)
1942 u8 hash
[SHA256_MAC_LEN
];
1946 if (e_snonce1
== NULL
) {
1947 wpa_printf(MSG_DEBUG
, "WPS: No E-SNonce1 received");
1951 wpa_hexdump_key(MSG_DEBUG
, "WPS: E-SNonce1", e_snonce1
,
1952 WPS_SECRET_NONCE_LEN
);
1954 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1955 addr
[0] = e_snonce1
;
1956 len
[0] = WPS_SECRET_NONCE_LEN
;
1957 addr
[1] = wps
->psk1
;
1958 len
[1] = WPS_PSK_LEN
;
1959 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
1960 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
1961 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
1962 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
1963 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
1965 if (os_memcmp(wps
->peer_hash1
, hash
, WPS_HASH_LEN
) != 0) {
1966 wpa_printf(MSG_DEBUG
, "WPS: E-Hash1 derived from E-S1 does "
1967 "not match with the pre-committed value");
1968 wps
->config_error
= WPS_CFG_DEV_PASSWORD_AUTH_FAILURE
;
1969 wps_pwd_auth_fail_event(wps
->wps
, 0, 1);
1973 wpa_printf(MSG_DEBUG
, "WPS: Enrollee proved knowledge of the first "
1974 "half of the device password");
1980 static int wps_process_e_snonce2(struct wps_data
*wps
, const u8
*e_snonce2
)
1982 u8 hash
[SHA256_MAC_LEN
];
1986 if (e_snonce2
== NULL
) {
1987 wpa_printf(MSG_DEBUG
, "WPS: No E-SNonce2 received");
1991 wpa_hexdump_key(MSG_DEBUG
, "WPS: E-SNonce2", e_snonce2
,
1992 WPS_SECRET_NONCE_LEN
);
1994 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1995 addr
[0] = e_snonce2
;
1996 len
[0] = WPS_SECRET_NONCE_LEN
;
1997 addr
[1] = wps
->psk2
;
1998 len
[1] = WPS_PSK_LEN
;
1999 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
2000 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
2001 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
2002 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
2003 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
2005 if (os_memcmp(wps
->peer_hash2
, hash
, WPS_HASH_LEN
) != 0) {
2006 wpa_printf(MSG_DEBUG
, "WPS: E-Hash2 derived from E-S2 does "
2007 "not match with the pre-committed value");
2008 wps_registrar_invalidate_pin(wps
->wps
->registrar
, wps
->uuid_e
);
2009 wps
->config_error
= WPS_CFG_DEV_PASSWORD_AUTH_FAILURE
;
2010 wps_pwd_auth_fail_event(wps
->wps
, 0, 2);
2014 wpa_printf(MSG_DEBUG
, "WPS: Enrollee proved knowledge of the second "
2015 "half of the device password");
2016 wps
->wps_pin_revealed
= 0;
2017 wps_registrar_unlock_pin(wps
->wps
->registrar
, wps
->uuid_e
);
2023 static int wps_process_mac_addr(struct wps_data
*wps
, const u8
*mac_addr
)
2025 if (mac_addr
== NULL
) {
2026 wpa_printf(MSG_DEBUG
, "WPS: No MAC Address received");
2030 wpa_printf(MSG_DEBUG
, "WPS: Enrollee MAC Address " MACSTR
,
2032 os_memcpy(wps
->mac_addr_e
, mac_addr
, ETH_ALEN
);
2033 os_memcpy(wps
->peer_dev
.mac_addr
, mac_addr
, ETH_ALEN
);
2039 static int wps_process_pubkey(struct wps_data
*wps
, const u8
*pk
,
2042 if (pk
== NULL
|| pk_len
== 0) {
2043 wpa_printf(MSG_DEBUG
, "WPS: No Public Key received");
2047 #ifdef CONFIG_WPS_OOB
2048 if (wps
->wps
->oob_conf
.pubkey_hash
!= NULL
) {
2050 u8 hash
[WPS_HASH_LEN
];
2053 sha256_vector(1, addr
, &pk_len
, hash
);
2055 wpabuf_head(wps
->wps
->oob_conf
.pubkey_hash
),
2056 WPS_OOB_PUBKEY_HASH_LEN
) != 0) {
2057 wpa_printf(MSG_ERROR
, "WPS: Public Key hash error");
2061 #endif /* CONFIG_WPS_OOB */
2063 wpabuf_free(wps
->dh_pubkey_e
);
2064 wps
->dh_pubkey_e
= wpabuf_alloc_copy(pk
, pk_len
);
2065 if (wps
->dh_pubkey_e
== NULL
)
2072 static int wps_process_auth_type_flags(struct wps_data
*wps
, const u8
*auth
)
2077 wpa_printf(MSG_DEBUG
, "WPS: No Authentication Type flags "
2082 auth_types
= WPA_GET_BE16(auth
);
2084 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Authentication Type flags 0x%x",
2086 wps
->auth_type
= wps
->wps
->auth_types
& auth_types
;
2087 if (wps
->auth_type
== 0) {
2088 wpa_printf(MSG_DEBUG
, "WPS: No match in supported "
2089 "authentication types (own 0x%x Enrollee 0x%x)",
2090 wps
->wps
->auth_types
, auth_types
);
2091 #ifdef WPS_WORKAROUNDS
2093 * Some deployed implementations seem to advertise incorrect
2094 * information in this attribute. For example, Linksys WRT350N
2095 * seems to have a byteorder bug that breaks this negotiation.
2096 * In order to interoperate with existing implementations,
2097 * assume that the Enrollee supports everything we do.
2099 wpa_printf(MSG_DEBUG
, "WPS: Workaround - assume Enrollee "
2100 "does not advertise supported authentication types "
2102 wps
->auth_type
= wps
->wps
->auth_types
;
2103 #else /* WPS_WORKAROUNDS */
2105 #endif /* WPS_WORKAROUNDS */
2112 static int wps_process_encr_type_flags(struct wps_data
*wps
, const u8
*encr
)
2117 wpa_printf(MSG_DEBUG
, "WPS: No Encryption Type flags "
2122 encr_types
= WPA_GET_BE16(encr
);
2124 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Encryption Type flags 0x%x",
2126 wps
->encr_type
= wps
->wps
->encr_types
& encr_types
;
2127 if (wps
->encr_type
== 0) {
2128 wpa_printf(MSG_DEBUG
, "WPS: No match in supported "
2129 "encryption types (own 0x%x Enrollee 0x%x)",
2130 wps
->wps
->encr_types
, encr_types
);
2131 #ifdef WPS_WORKAROUNDS
2133 * Some deployed implementations seem to advertise incorrect
2134 * information in this attribute. For example, Linksys WRT350N
2135 * seems to have a byteorder bug that breaks this negotiation.
2136 * In order to interoperate with existing implementations,
2137 * assume that the Enrollee supports everything we do.
2139 wpa_printf(MSG_DEBUG
, "WPS: Workaround - assume Enrollee "
2140 "does not advertise supported encryption types "
2142 wps
->encr_type
= wps
->wps
->encr_types
;
2143 #else /* WPS_WORKAROUNDS */
2145 #endif /* WPS_WORKAROUNDS */
2152 static int wps_process_conn_type_flags(struct wps_data
*wps
, const u8
*conn
)
2155 wpa_printf(MSG_DEBUG
, "WPS: No Connection Type flags "
2160 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Connection Type flags 0x%x",
2167 static int wps_process_config_methods(struct wps_data
*wps
, const u8
*methods
)
2171 if (methods
== NULL
) {
2172 wpa_printf(MSG_DEBUG
, "WPS: No Config Methods received");
2176 m
= WPA_GET_BE16(methods
);
2178 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Config Methods 0x%x"
2179 "%s%s%s%s%s%s%s%s%s", m
,
2180 m
& WPS_CONFIG_USBA
? " [USBA]" : "",
2181 m
& WPS_CONFIG_ETHERNET
? " [Ethernet]" : "",
2182 m
& WPS_CONFIG_LABEL
? " [Label]" : "",
2183 m
& WPS_CONFIG_DISPLAY
? " [Display]" : "",
2184 m
& WPS_CONFIG_EXT_NFC_TOKEN
? " [Ext NFC Token]" : "",
2185 m
& WPS_CONFIG_INT_NFC_TOKEN
? " [Int NFC Token]" : "",
2186 m
& WPS_CONFIG_NFC_INTERFACE
? " [NFC]" : "",
2187 m
& WPS_CONFIG_PUSHBUTTON
? " [PBC]" : "",
2188 m
& WPS_CONFIG_KEYPAD
? " [Keypad]" : "");
2190 if (!(m
& WPS_CONFIG_DISPLAY
) && !wps
->use_psk_key
) {
2192 * The Enrollee does not have a display so it is unlikely to be
2193 * able to show the passphrase to a user and as such, could
2194 * benefit from receiving PSK to reduce key derivation time.
2196 wpa_printf(MSG_DEBUG
, "WPS: Prefer PSK format key due to "
2197 "Enrollee not supporting display");
2198 wps
->use_psk_key
= 1;
2205 static int wps_process_wps_state(struct wps_data
*wps
, const u8
*state
)
2207 if (state
== NULL
) {
2208 wpa_printf(MSG_DEBUG
, "WPS: No Wi-Fi Protected Setup State "
2213 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2220 static int wps_process_assoc_state(struct wps_data
*wps
, const u8
*assoc
)
2224 if (assoc
== NULL
) {
2225 wpa_printf(MSG_DEBUG
, "WPS: No Association State received");
2229 a
= WPA_GET_BE16(assoc
);
2230 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Association State %d", a
);
2236 static int wps_process_config_error(struct wps_data
*wps
, const u8
*err
)
2241 wpa_printf(MSG_DEBUG
, "WPS: No Configuration Error received");
2245 e
= WPA_GET_BE16(err
);
2246 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Configuration Error %d", e
);
2252 static int wps_registrar_p2p_dev_addr_match(struct wps_data
*wps
)
2255 struct wps_registrar
*reg
= wps
->wps
->registrar
;
2257 if (is_zero_ether_addr(reg
->p2p_dev_addr
))
2258 return 1; /* no filtering in use */
2260 if (os_memcmp(reg
->p2p_dev_addr
, wps
->p2p_dev_addr
, ETH_ALEN
) != 0) {
2261 wpa_printf(MSG_DEBUG
, "WPS: No match on P2P Device Address "
2262 "filtering for PBC: expected " MACSTR
" was "
2263 MACSTR
" - indicate PBC session overlap",
2264 MAC2STR(reg
->p2p_dev_addr
),
2265 MAC2STR(wps
->p2p_dev_addr
));
2268 #endif /* CONFIG_P2P */
2273 static int wps_registrar_skip_overlap(struct wps_data
*wps
)
2276 struct wps_registrar
*reg
= wps
->wps
->registrar
;
2278 if (is_zero_ether_addr(reg
->p2p_dev_addr
))
2279 return 0; /* no specific Enrollee selected */
2281 if (os_memcmp(reg
->p2p_dev_addr
, wps
->p2p_dev_addr
, ETH_ALEN
) == 0) {
2282 wpa_printf(MSG_DEBUG
, "WPS: Skip PBC overlap due to selected "
2286 #endif /* CONFIG_P2P */
2291 static enum wps_process_res
wps_process_m1(struct wps_data
*wps
,
2292 struct wps_parse_attr
*attr
)
2294 wpa_printf(MSG_DEBUG
, "WPS: Received M1");
2296 if (wps
->state
!= RECV_M1
) {
2297 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
2298 "receiving M1", wps
->state
);
2302 if (wps_process_uuid_e(wps
, attr
->uuid_e
) ||
2303 wps_process_mac_addr(wps
, attr
->mac_addr
) ||
2304 wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
2305 wps_process_pubkey(wps
, attr
->public_key
, attr
->public_key_len
) ||
2306 wps_process_auth_type_flags(wps
, attr
->auth_type_flags
) ||
2307 wps_process_encr_type_flags(wps
, attr
->encr_type_flags
) ||
2308 wps_process_conn_type_flags(wps
, attr
->conn_type_flags
) ||
2309 wps_process_config_methods(wps
, attr
->config_methods
) ||
2310 wps_process_wps_state(wps
, attr
->wps_state
) ||
2311 wps_process_device_attrs(&wps
->peer_dev
, attr
) ||
2312 wps_process_rf_bands(&wps
->peer_dev
, attr
->rf_bands
) ||
2313 wps_process_assoc_state(wps
, attr
->assoc_state
) ||
2314 wps_process_dev_password_id(wps
, attr
->dev_password_id
) ||
2315 wps_process_config_error(wps
, attr
->config_error
) ||
2316 wps_process_os_version(&wps
->peer_dev
, attr
->os_version
))
2319 if (wps
->dev_pw_id
< 0x10 &&
2320 wps
->dev_pw_id
!= DEV_PW_DEFAULT
&&
2321 wps
->dev_pw_id
!= DEV_PW_USER_SPECIFIED
&&
2322 wps
->dev_pw_id
!= DEV_PW_MACHINE_SPECIFIED
&&
2323 wps
->dev_pw_id
!= DEV_PW_REGISTRAR_SPECIFIED
&&
2324 (wps
->dev_pw_id
!= DEV_PW_PUSHBUTTON
||
2325 !wps
->wps
->registrar
->pbc
)) {
2326 wpa_printf(MSG_DEBUG
, "WPS: Unsupported Device Password ID %d",
2328 wps
->state
= SEND_M2D
;
2329 return WPS_CONTINUE
;
2332 #ifdef CONFIG_WPS_OOB
2333 if (wps
->dev_pw_id
>= 0x10 &&
2334 wps
->dev_pw_id
!= wps
->wps
->oob_dev_pw_id
) {
2335 wpa_printf(MSG_DEBUG
, "WPS: OOB Device Password ID "
2336 "%d mismatch", wps
->dev_pw_id
);
2337 wps
->state
= SEND_M2D
;
2338 return WPS_CONTINUE
;
2340 #endif /* CONFIG_WPS_OOB */
2342 if (wps
->dev_pw_id
== DEV_PW_PUSHBUTTON
) {
2343 if ((wps
->wps
->registrar
->force_pbc_overlap
||
2344 wps_registrar_pbc_overlap(wps
->wps
->registrar
,
2345 wps
->mac_addr_e
, wps
->uuid_e
) ||
2346 !wps_registrar_p2p_dev_addr_match(wps
)) &&
2347 !wps_registrar_skip_overlap(wps
)) {
2348 wpa_printf(MSG_DEBUG
, "WPS: PBC overlap - deny PBC "
2350 wps
->state
= SEND_M2D
;
2351 wps
->config_error
= WPS_CFG_MULTIPLE_PBC_DETECTED
;
2352 wps_pbc_overlap_event(wps
->wps
);
2353 wps_fail_event(wps
->wps
, WPS_M1
,
2354 WPS_CFG_MULTIPLE_PBC_DETECTED
,
2356 wps
->wps
->registrar
->force_pbc_overlap
= 1;
2357 return WPS_CONTINUE
;
2359 wps_registrar_add_pbc_session(wps
->wps
->registrar
,
2360 wps
->mac_addr_e
, wps
->uuid_e
);
2364 #ifdef WPS_WORKAROUNDS
2366 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2367 * passphrase format. To avoid interop issues, force PSK format to be
2370 if (!wps
->use_psk_key
&&
2371 wps
->peer_dev
.manufacturer
&&
2372 os_strncmp(wps
->peer_dev
.manufacturer
, "Apple ", 6) == 0 &&
2373 wps
->peer_dev
.model_name
&&
2374 os_strcmp(wps
->peer_dev
.model_name
, "AirPort") == 0) {
2375 wpa_printf(MSG_DEBUG
, "WPS: Workaround - Force Network Key in "
2377 wps
->use_psk_key
= 1;
2379 #endif /* WPS_WORKAROUNDS */
2381 wps
->state
= SEND_M2
;
2382 return WPS_CONTINUE
;
2386 static enum wps_process_res
wps_process_m3(struct wps_data
*wps
,
2387 const struct wpabuf
*msg
,
2388 struct wps_parse_attr
*attr
)
2390 wpa_printf(MSG_DEBUG
, "WPS: Received M3");
2392 if (wps
->state
!= RECV_M3
) {
2393 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
2394 "receiving M3", wps
->state
);
2395 wps
->state
= SEND_WSC_NACK
;
2396 return WPS_CONTINUE
;
2399 if (wps
->pbc
&& wps
->wps
->registrar
->force_pbc_overlap
&&
2400 !wps_registrar_skip_overlap(wps
)) {
2401 wpa_printf(MSG_DEBUG
, "WPS: Reject negotiation due to PBC "
2403 wps
->state
= SEND_WSC_NACK
;
2404 wps
->config_error
= WPS_CFG_MULTIPLE_PBC_DETECTED
;
2405 return WPS_CONTINUE
;
2408 if (wps_process_registrar_nonce(wps
, attr
->registrar_nonce
) ||
2409 wps_process_authenticator(wps
, attr
->authenticator
, msg
) ||
2410 wps_process_e_hash1(wps
, attr
->e_hash1
) ||
2411 wps_process_e_hash2(wps
, attr
->e_hash2
)) {
2412 wps
->state
= SEND_WSC_NACK
;
2413 return WPS_CONTINUE
;
2416 wps
->state
= SEND_M4
;
2417 return WPS_CONTINUE
;
2421 static enum wps_process_res
wps_process_m5(struct wps_data
*wps
,
2422 const struct wpabuf
*msg
,
2423 struct wps_parse_attr
*attr
)
2425 struct wpabuf
*decrypted
;
2426 struct wps_parse_attr eattr
;
2428 wpa_printf(MSG_DEBUG
, "WPS: Received M5");
2430 if (wps
->state
!= RECV_M5
) {
2431 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
2432 "receiving M5", wps
->state
);
2433 wps
->state
= SEND_WSC_NACK
;
2434 return WPS_CONTINUE
;
2437 if (wps
->pbc
&& wps
->wps
->registrar
->force_pbc_overlap
&&
2438 !wps_registrar_skip_overlap(wps
)) {
2439 wpa_printf(MSG_DEBUG
, "WPS: Reject negotiation due to PBC "
2441 wps
->state
= SEND_WSC_NACK
;
2442 wps
->config_error
= WPS_CFG_MULTIPLE_PBC_DETECTED
;
2443 return WPS_CONTINUE
;
2446 if (wps_process_registrar_nonce(wps
, attr
->registrar_nonce
) ||
2447 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
2448 wps
->state
= SEND_WSC_NACK
;
2449 return WPS_CONTINUE
;
2452 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
2453 attr
->encr_settings_len
);
2454 if (decrypted
== NULL
) {
2455 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
2456 "Settings attribute");
2457 wps
->state
= SEND_WSC_NACK
;
2458 return WPS_CONTINUE
;
2461 if (wps_validate_m5_encr(decrypted
, attr
->version2
!= NULL
) < 0) {
2462 wpabuf_free(decrypted
);
2463 wps
->state
= SEND_WSC_NACK
;
2464 return WPS_CONTINUE
;
2467 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
2469 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
2470 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
2471 wps_process_e_snonce1(wps
, eattr
.e_snonce1
)) {
2472 wpabuf_free(decrypted
);
2473 wps
->state
= SEND_WSC_NACK
;
2474 return WPS_CONTINUE
;
2476 wpabuf_free(decrypted
);
2478 wps
->state
= SEND_M6
;
2479 return WPS_CONTINUE
;
2483 static void wps_sta_cred_cb(struct wps_data
*wps
)
2486 * Update credential to only include a single authentication and
2487 * encryption type in case the AP configuration includes more than one
2490 if (wps
->cred
.auth_type
& WPS_AUTH_WPA2PSK
)
2491 wps
->cred
.auth_type
= WPS_AUTH_WPA2PSK
;
2492 else if (wps
->cred
.auth_type
& WPS_AUTH_WPAPSK
)
2493 wps
->cred
.auth_type
= WPS_AUTH_WPAPSK
;
2494 if (wps
->cred
.encr_type
& WPS_ENCR_AES
)
2495 wps
->cred
.encr_type
= WPS_ENCR_AES
;
2496 else if (wps
->cred
.encr_type
& WPS_ENCR_TKIP
)
2497 wps
->cred
.encr_type
= WPS_ENCR_TKIP
;
2498 wpa_printf(MSG_DEBUG
, "WPS: Update local configuration based on the "
2499 "AP configuration");
2500 if (wps
->wps
->cred_cb
)
2501 wps
->wps
->cred_cb(wps
->wps
->cb_ctx
, &wps
->cred
);
2505 static void wps_cred_update(struct wps_credential
*dst
,
2506 struct wps_credential
*src
)
2508 os_memcpy(dst
->ssid
, src
->ssid
, sizeof(dst
->ssid
));
2509 dst
->ssid_len
= src
->ssid_len
;
2510 dst
->auth_type
= src
->auth_type
;
2511 dst
->encr_type
= src
->encr_type
;
2512 dst
->key_idx
= src
->key_idx
;
2513 os_memcpy(dst
->key
, src
->key
, sizeof(dst
->key
));
2514 dst
->key_len
= src
->key_len
;
2518 static int wps_process_ap_settings_r(struct wps_data
*wps
,
2519 struct wps_parse_attr
*attr
)
2521 if (wps
->wps
->ap
|| wps
->er
)
2524 /* AP Settings Attributes in M7 when Enrollee is an AP */
2525 if (wps_process_ap_settings(attr
, &wps
->cred
) < 0)
2528 wpa_printf(MSG_INFO
, "WPS: Received old AP configuration from AP");
2530 if (wps
->new_ap_settings
) {
2531 wpa_printf(MSG_INFO
, "WPS: Update AP configuration based on "
2533 wps_cred_update(&wps
->cred
, wps
->new_ap_settings
);
2537 * Use the AP PIN only to receive the current AP settings, not
2538 * to reconfigure the AP.
2542 * Clear selected registrar here since we do not get to
2543 * WSC_Done in this protocol run.
2545 wps_registrar_pin_completed(wps
->wps
->registrar
);
2547 if (wps
->ap_settings_cb
) {
2548 wps
->ap_settings_cb(wps
->ap_settings_cb_ctx
,
2552 wps_sta_cred_cb(wps
);
2558 static enum wps_process_res
wps_process_m7(struct wps_data
*wps
,
2559 const struct wpabuf
*msg
,
2560 struct wps_parse_attr
*attr
)
2562 struct wpabuf
*decrypted
;
2563 struct wps_parse_attr eattr
;
2565 wpa_printf(MSG_DEBUG
, "WPS: Received M7");
2567 if (wps
->state
!= RECV_M7
) {
2568 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
2569 "receiving M7", wps
->state
);
2570 wps
->state
= SEND_WSC_NACK
;
2571 return WPS_CONTINUE
;
2574 if (wps
->pbc
&& wps
->wps
->registrar
->force_pbc_overlap
&&
2575 !wps_registrar_skip_overlap(wps
)) {
2576 wpa_printf(MSG_DEBUG
, "WPS: Reject negotiation due to PBC "
2578 wps
->state
= SEND_WSC_NACK
;
2579 wps
->config_error
= WPS_CFG_MULTIPLE_PBC_DETECTED
;
2580 return WPS_CONTINUE
;
2583 if (wps_process_registrar_nonce(wps
, attr
->registrar_nonce
) ||
2584 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
2585 wps
->state
= SEND_WSC_NACK
;
2586 return WPS_CONTINUE
;
2589 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
2590 attr
->encr_settings_len
);
2591 if (decrypted
== NULL
) {
2592 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypt Encrypted "
2593 "Settings attribute");
2594 wps
->state
= SEND_WSC_NACK
;
2595 return WPS_CONTINUE
;
2598 if (wps_validate_m7_encr(decrypted
, wps
->wps
->ap
|| wps
->er
,
2599 attr
->version2
!= NULL
) < 0) {
2600 wpabuf_free(decrypted
);
2601 wps
->state
= SEND_WSC_NACK
;
2602 return WPS_CONTINUE
;
2605 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
2607 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
2608 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
2609 wps_process_e_snonce2(wps
, eattr
.e_snonce2
) ||
2610 wps_process_ap_settings_r(wps
, &eattr
)) {
2611 wpabuf_free(decrypted
);
2612 wps
->state
= SEND_WSC_NACK
;
2613 return WPS_CONTINUE
;
2616 wpabuf_free(decrypted
);
2618 wps
->state
= SEND_M8
;
2619 return WPS_CONTINUE
;
2623 static enum wps_process_res
wps_process_wsc_msg(struct wps_data
*wps
,
2624 const struct wpabuf
*msg
)
2626 struct wps_parse_attr attr
;
2627 enum wps_process_res ret
= WPS_CONTINUE
;
2629 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_MSG");
2631 if (wps_parse_msg(msg
, &attr
) < 0)
2634 if (attr
.msg_type
== NULL
) {
2635 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
2636 wps
->state
= SEND_WSC_NACK
;
2637 return WPS_CONTINUE
;
2640 if (*attr
.msg_type
!= WPS_M1
&&
2641 (attr
.registrar_nonce
== NULL
||
2642 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
,
2643 WPS_NONCE_LEN
!= 0))) {
2644 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
2648 switch (*attr
.msg_type
) {
2650 if (wps_validate_m1(msg
) < 0)
2652 #ifdef CONFIG_WPS_UPNP
2653 if (wps
->wps
->wps_upnp
&& attr
.mac_addr
) {
2654 /* Remove old pending messages when starting new run */
2655 wps_free_pending_msgs(wps
->wps
->upnp_msgs
);
2656 wps
->wps
->upnp_msgs
= NULL
;
2658 upnp_wps_device_send_wlan_event(
2659 wps
->wps
->wps_upnp
, attr
.mac_addr
,
2660 UPNP_WPS_WLANEVENT_TYPE_EAP
, msg
);
2662 #endif /* CONFIG_WPS_UPNP */
2663 ret
= wps_process_m1(wps
, &attr
);
2666 if (wps_validate_m3(msg
) < 0)
2668 ret
= wps_process_m3(wps
, msg
, &attr
);
2669 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
2670 wps_fail_event(wps
->wps
, WPS_M3
, wps
->config_error
,
2671 wps
->error_indication
);
2674 if (wps_validate_m5(msg
) < 0)
2676 ret
= wps_process_m5(wps
, msg
, &attr
);
2677 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
2678 wps_fail_event(wps
->wps
, WPS_M5
, wps
->config_error
,
2679 wps
->error_indication
);
2682 if (wps_validate_m7(msg
) < 0)
2684 ret
= wps_process_m7(wps
, msg
, &attr
);
2685 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
2686 wps_fail_event(wps
->wps
, WPS_M7
, wps
->config_error
,
2687 wps
->error_indication
);
2690 wpa_printf(MSG_DEBUG
, "WPS: Unsupported Message Type %d",
2695 if (ret
== WPS_CONTINUE
) {
2696 /* Save a copy of the last message for Authenticator derivation
2698 wpabuf_free(wps
->last_msg
);
2699 wps
->last_msg
= wpabuf_dup(msg
);
2706 static enum wps_process_res
wps_process_wsc_ack(struct wps_data
*wps
,
2707 const struct wpabuf
*msg
)
2709 struct wps_parse_attr attr
;
2711 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_ACK");
2713 if (wps_parse_msg(msg
, &attr
) < 0)
2716 if (attr
.msg_type
== NULL
) {
2717 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
2721 if (*attr
.msg_type
!= WPS_WSC_ACK
) {
2722 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
2727 #ifdef CONFIG_WPS_UPNP
2728 if (wps
->wps
->wps_upnp
&& wps
->ext_reg
&& wps
->state
== RECV_M2D_ACK
&&
2729 upnp_wps_subscribers(wps
->wps
->wps_upnp
)) {
2730 if (wps
->wps
->upnp_msgs
)
2731 return WPS_CONTINUE
;
2732 wpa_printf(MSG_DEBUG
, "WPS: Wait for response from an "
2733 "external Registrar");
2736 #endif /* CONFIG_WPS_UPNP */
2738 if (attr
.registrar_nonce
== NULL
||
2739 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
2741 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
2745 if (attr
.enrollee_nonce
== NULL
||
2746 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
2747 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
2751 if (wps
->state
== RECV_M2D_ACK
) {
2752 #ifdef CONFIG_WPS_UPNP
2753 if (wps
->wps
->wps_upnp
&&
2754 upnp_wps_subscribers(wps
->wps
->wps_upnp
)) {
2755 if (wps
->wps
->upnp_msgs
)
2756 return WPS_CONTINUE
;
2757 if (wps
->ext_reg
== 0)
2759 wpa_printf(MSG_DEBUG
, "WPS: Wait for response from an "
2760 "external Registrar");
2763 #endif /* CONFIG_WPS_UPNP */
2765 wpa_printf(MSG_DEBUG
, "WPS: No more registrars available - "
2766 "terminate negotiation");
2773 static enum wps_process_res
wps_process_wsc_nack(struct wps_data
*wps
,
2774 const struct wpabuf
*msg
)
2776 struct wps_parse_attr attr
;
2780 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_NACK");
2782 old_state
= wps
->state
;
2783 wps
->state
= SEND_WSC_NACK
;
2785 if (wps_parse_msg(msg
, &attr
) < 0)
2788 if (attr
.msg_type
== NULL
) {
2789 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
2793 if (*attr
.msg_type
!= WPS_WSC_NACK
) {
2794 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
2799 #ifdef CONFIG_WPS_UPNP
2800 if (wps
->wps
->wps_upnp
&& wps
->ext_reg
) {
2801 wpa_printf(MSG_DEBUG
, "WPS: Negotiation using external "
2802 "Registrar terminated by the Enrollee");
2805 #endif /* CONFIG_WPS_UPNP */
2807 if (attr
.registrar_nonce
== NULL
||
2808 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
2810 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
2814 if (attr
.enrollee_nonce
== NULL
||
2815 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
2816 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
2820 if (attr
.config_error
== NULL
) {
2821 wpa_printf(MSG_DEBUG
, "WPS: No Configuration Error attribute "
2826 config_error
= WPA_GET_BE16(attr
.config_error
);
2827 wpa_printf(MSG_DEBUG
, "WPS: Enrollee terminated negotiation with "
2828 "Configuration Error %d", config_error
);
2830 switch (old_state
) {
2832 wps_fail_event(wps
->wps
, WPS_M2
, config_error
,
2833 wps
->error_indication
);
2836 wps_fail_event(wps
->wps
, WPS_M4
, config_error
,
2837 wps
->error_indication
);
2840 wps_fail_event(wps
->wps
, WPS_M6
, config_error
,
2841 wps
->error_indication
);
2844 wps_fail_event(wps
->wps
, WPS_M8
, config_error
,
2845 wps
->error_indication
);
2855 static enum wps_process_res
wps_process_wsc_done(struct wps_data
*wps
,
2856 const struct wpabuf
*msg
)
2858 struct wps_parse_attr attr
;
2860 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_Done");
2862 if (wps
->state
!= RECV_DONE
&&
2863 (!wps
->wps
->wps_upnp
|| !wps
->ext_reg
)) {
2864 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
2865 "receiving WSC_Done", wps
->state
);
2869 if (wps_parse_msg(msg
, &attr
) < 0)
2872 if (attr
.msg_type
== NULL
) {
2873 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
2877 if (*attr
.msg_type
!= WPS_WSC_DONE
) {
2878 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
2883 #ifdef CONFIG_WPS_UPNP
2884 if (wps
->wps
->wps_upnp
&& wps
->ext_reg
) {
2885 wpa_printf(MSG_DEBUG
, "WPS: Negotiation using external "
2886 "Registrar completed successfully");
2887 wps_device_store(wps
->wps
->registrar
, &wps
->peer_dev
,
2891 #endif /* CONFIG_WPS_UPNP */
2893 if (attr
.registrar_nonce
== NULL
||
2894 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
2896 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
2900 if (attr
.enrollee_nonce
== NULL
||
2901 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
2902 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
2906 wpa_printf(MSG_DEBUG
, "WPS: Negotiation completed successfully");
2907 wps_device_store(wps
->wps
->registrar
, &wps
->peer_dev
,
2910 if (wps
->wps
->wps_state
== WPS_STATE_NOT_CONFIGURED
&& wps
->new_psk
&&
2911 wps
->wps
->ap
&& !wps
->wps
->registrar
->disable_auto_conf
) {
2912 struct wps_credential cred
;
2914 wpa_printf(MSG_DEBUG
, "WPS: Moving to Configured state based "
2915 "on first Enrollee connection");
2917 os_memset(&cred
, 0, sizeof(cred
));
2918 os_memcpy(cred
.ssid
, wps
->wps
->ssid
, wps
->wps
->ssid_len
);
2919 cred
.ssid_len
= wps
->wps
->ssid_len
;
2920 cred
.auth_type
= WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
;
2921 cred
.encr_type
= WPS_ENCR_TKIP
| WPS_ENCR_AES
;
2922 os_memcpy(cred
.key
, wps
->new_psk
, wps
->new_psk_len
);
2923 cred
.key_len
= wps
->new_psk_len
;
2925 wps
->wps
->wps_state
= WPS_STATE_CONFIGURED
;
2926 wpa_hexdump_ascii_key(MSG_DEBUG
,
2927 "WPS: Generated random passphrase",
2928 wps
->new_psk
, wps
->new_psk_len
);
2929 if (wps
->wps
->cred_cb
)
2930 wps
->wps
->cred_cb(wps
->wps
->cb_ctx
, &cred
);
2932 os_free(wps
->new_psk
);
2933 wps
->new_psk
= NULL
;
2936 if (!wps
->wps
->ap
&& !wps
->er
)
2937 wps_sta_cred_cb(wps
);
2940 if (wps_cb_new_psk(wps
->wps
->registrar
, wps
->mac_addr_e
,
2941 wps
->new_psk
, wps
->new_psk_len
)) {
2942 wpa_printf(MSG_DEBUG
, "WPS: Failed to configure the "
2945 os_free(wps
->new_psk
);
2946 wps
->new_psk
= NULL
;
2949 wps_cb_reg_success(wps
->wps
->registrar
, wps
->mac_addr_e
, wps
->uuid_e
);
2952 wps_registrar_remove_pbc_session(wps
->wps
->registrar
,
2953 wps
->mac_addr_e
, wps
->uuid_e
);
2954 wps_registrar_pbc_completed(wps
->wps
->registrar
);
2956 wps_registrar_pin_completed(wps
->wps
->registrar
);
2958 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
2959 * merge them into APs own list.. */
2961 wps_success_event(wps
->wps
);
2967 enum wps_process_res
wps_registrar_process_msg(struct wps_data
*wps
,
2968 enum wsc_op_code op_code
,
2969 const struct wpabuf
*msg
)
2971 enum wps_process_res ret
;
2973 wpa_printf(MSG_DEBUG
, "WPS: Processing received message (len=%lu "
2975 (unsigned long) wpabuf_len(msg
), op_code
);
2977 #ifdef CONFIG_WPS_UPNP
2978 if (wps
->wps
->wps_upnp
&& op_code
== WSC_MSG
&& wps
->ext_reg
== 1) {
2979 struct wps_parse_attr attr
;
2980 if (wps_parse_msg(msg
, &attr
) == 0 && attr
.msg_type
&&
2981 *attr
.msg_type
== WPS_M3
)
2982 wps
->ext_reg
= 2; /* past M2/M2D phase */
2984 if (wps
->ext_reg
> 1)
2985 wps_registrar_free_pending_m2(wps
->wps
);
2986 if (wps
->wps
->wps_upnp
&& wps
->ext_reg
&&
2987 wps
->wps
->upnp_msgs
== NULL
&&
2988 (op_code
== WSC_MSG
|| op_code
== WSC_Done
|| op_code
== WSC_NACK
))
2990 struct wps_parse_attr attr
;
2992 if (wps_parse_msg(msg
, &attr
) < 0 || attr
.msg_type
== NULL
)
2995 type
= *attr
.msg_type
;
2996 wpa_printf(MSG_DEBUG
, "WPS: Sending received message (type %d)"
2997 " to external Registrar for processing", type
);
2998 upnp_wps_device_send_wlan_event(wps
->wps
->wps_upnp
,
3000 UPNP_WPS_WLANEVENT_TYPE_EAP
,
3002 if (op_code
== WSC_MSG
)
3004 } else if (wps
->wps
->wps_upnp
&& wps
->ext_reg
&& op_code
== WSC_MSG
) {
3005 wpa_printf(MSG_DEBUG
, "WPS: Skip internal processing - using "
3006 "external Registrar");
3007 return WPS_CONTINUE
;
3009 #endif /* CONFIG_WPS_UPNP */
3013 return wps_process_wsc_msg(wps
, msg
);
3015 if (wps_validate_wsc_ack(msg
) < 0)
3017 return wps_process_wsc_ack(wps
, msg
);
3019 if (wps_validate_wsc_nack(msg
) < 0)
3021 return wps_process_wsc_nack(wps
, msg
);
3023 if (wps_validate_wsc_done(msg
) < 0)
3025 ret
= wps_process_wsc_done(wps
, msg
);
3026 if (ret
== WPS_FAILURE
) {
3027 wps
->state
= SEND_WSC_NACK
;
3028 wps_fail_event(wps
->wps
, WPS_WSC_DONE
,
3030 wps
->error_indication
);
3034 wpa_printf(MSG_DEBUG
, "WPS: Unsupported op_code %d", op_code
);
3040 int wps_registrar_update_ie(struct wps_registrar
*reg
)
3042 return wps_set_ie(reg
);
3046 static void wps_registrar_set_selected_timeout(void *eloop_ctx
,
3049 struct wps_registrar
*reg
= eloop_ctx
;
3051 wpa_printf(MSG_DEBUG
, "WPS: Selected Registrar timeout - "
3052 "unselect internal Registrar");
3053 reg
->selected_registrar
= 0;
3055 wps_registrar_selected_registrar_changed(reg
);
3059 #ifdef CONFIG_WPS_UPNP
3060 static void wps_registrar_sel_reg_add(struct wps_registrar
*reg
,
3061 struct subscription
*s
)
3064 wpa_printf(MSG_DEBUG
, "WPS: External Registrar selected (dev_pw_id=%d "
3065 "config_methods=0x%x)",
3066 s
->dev_password_id
, s
->config_methods
);
3067 reg
->sel_reg_union
= 1;
3068 if (reg
->sel_reg_dev_password_id_override
!= DEV_PW_PUSHBUTTON
)
3069 reg
->sel_reg_dev_password_id_override
= s
->dev_password_id
;
3070 if (reg
->sel_reg_config_methods_override
== -1)
3071 reg
->sel_reg_config_methods_override
= 0;
3072 reg
->sel_reg_config_methods_override
|= s
->config_methods
;
3073 for (i
= 0; i
< WPS_MAX_AUTHORIZED_MACS
; i
++)
3074 if (is_zero_ether_addr(reg
->authorized_macs_union
[i
]))
3076 for (j
= 0; i
< WPS_MAX_AUTHORIZED_MACS
&& j
< WPS_MAX_AUTHORIZED_MACS
;
3078 if (is_zero_ether_addr(s
->authorized_macs
[j
]))
3080 wpa_printf(MSG_DEBUG
, "WPS: Add authorized MAC into union: "
3081 MACSTR
, MAC2STR(s
->authorized_macs
[j
]));
3082 os_memcpy(reg
->authorized_macs_union
[i
],
3083 s
->authorized_macs
[j
], ETH_ALEN
);
3086 wpa_hexdump(MSG_DEBUG
, "WPS: Authorized MACs union",
3087 (u8
*) reg
->authorized_macs_union
,
3088 sizeof(reg
->authorized_macs_union
));
3090 #endif /* CONFIG_WPS_UPNP */
3093 static void wps_registrar_sel_reg_union(struct wps_registrar
*reg
)
3095 #ifdef CONFIG_WPS_UPNP
3096 struct subscription
*s
;
3098 if (reg
->wps
->wps_upnp
== NULL
)
3101 dl_list_for_each(s
, ®
->wps
->wps_upnp
->subscriptions
,
3102 struct subscription
, list
) {
3103 struct subscr_addr
*sa
;
3104 sa
= dl_list_first(&s
->addr_list
, struct subscr_addr
, list
);
3106 wpa_printf(MSG_DEBUG
, "WPS: External Registrar %s:%d",
3107 inet_ntoa(sa
->saddr
.sin_addr
),
3108 ntohs(sa
->saddr
.sin_port
));
3110 if (s
->selected_registrar
)
3111 wps_registrar_sel_reg_add(reg
, s
);
3113 wpa_printf(MSG_DEBUG
, "WPS: External Registrar not "
3116 #endif /* CONFIG_WPS_UPNP */
3121 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3122 * @reg: Registrar data from wps_registrar_init()
3124 * This function is called when selected registrar state changes, e.g., when an
3125 * AP receives a SetSelectedRegistrar UPnP message.
3127 void wps_registrar_selected_registrar_changed(struct wps_registrar
*reg
)
3129 wpa_printf(MSG_DEBUG
, "WPS: Selected registrar information changed");
3131 reg
->sel_reg_union
= reg
->selected_registrar
;
3132 reg
->sel_reg_dev_password_id_override
= -1;
3133 reg
->sel_reg_config_methods_override
= -1;
3134 os_memcpy(reg
->authorized_macs_union
, reg
->authorized_macs
,
3135 WPS_MAX_AUTHORIZED_MACS
* ETH_ALEN
);
3136 wpa_hexdump(MSG_DEBUG
, "WPS: Authorized MACs union (start with own)",
3137 (u8
*) reg
->authorized_macs_union
,
3138 sizeof(reg
->authorized_macs_union
));
3139 if (reg
->selected_registrar
) {
3142 methods
= reg
->wps
->config_methods
& ~WPS_CONFIG_PUSHBUTTON
;
3144 methods
&= ~(WPS_CONFIG_VIRT_PUSHBUTTON
|
3145 WPS_CONFIG_PHY_PUSHBUTTON
);
3146 #endif /* CONFIG_WPS2 */
3148 reg
->sel_reg_dev_password_id_override
=
3150 wps_set_pushbutton(&methods
, reg
->wps
->config_methods
);
3152 wpa_printf(MSG_DEBUG
, "WPS: Internal Registrar selected "
3153 "(pbc=%d)", reg
->pbc
);
3154 reg
->sel_reg_config_methods_override
= methods
;
3156 wpa_printf(MSG_DEBUG
, "WPS: Internal Registrar not selected");
3158 wps_registrar_sel_reg_union(reg
);
3161 wps_cb_set_sel_reg(reg
);
3165 int wps_registrar_get_info(struct wps_registrar
*reg
, const u8
*addr
,
3166 char *buf
, size_t buflen
)
3168 struct wps_registrar_device
*d
;
3171 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
3173 d
= wps_device_get(reg
, addr
);
3176 if (uuid_bin2str(d
->uuid
, uuid
, sizeof(uuid
)))
3179 ret
= os_snprintf(buf
+ len
, buflen
- len
,
3181 "wpsPrimaryDeviceType=%s\n"
3182 "wpsDeviceName=%s\n"
3183 "wpsManufacturer=%s\n"
3185 "wpsModelNumber=%s\n"
3186 "wpsSerialNumber=%s\n",
3188 wps_dev_type_bin2str(d
->dev
.pri_dev_type
, devtype
,
3190 d
->dev
.device_name
? d
->dev
.device_name
: "",
3191 d
->dev
.manufacturer
? d
->dev
.manufacturer
: "",
3192 d
->dev
.model_name
? d
->dev
.model_name
: "",
3193 d
->dev
.model_number
? d
->dev
.model_number
: "",
3194 d
->dev
.serial_number
? d
->dev
.serial_number
: "");
3195 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
3203 int wps_registrar_config_ap(struct wps_registrar
*reg
,
3204 struct wps_credential
*cred
)
3207 printf("encr_type=0x%x\n", cred
->encr_type
);
3208 if (!(cred
->encr_type
& (WPS_ENCR_NONE
| WPS_ENCR_TKIP
|
3210 if (cred
->encr_type
& WPS_ENCR_WEP
) {
3211 wpa_printf(MSG_INFO
, "WPS: Reject new AP settings "
3212 "due to WEP configuration");
3216 wpa_printf(MSG_INFO
, "WPS: Reject new AP settings due to "
3217 "invalid encr_type 0x%x", cred
->encr_type
);
3221 if ((cred
->encr_type
& (WPS_ENCR_TKIP
| WPS_ENCR_AES
)) ==
3223 wpa_printf(MSG_DEBUG
, "WPS: Upgrade encr_type TKIP -> "
3225 cred
->encr_type
|= WPS_ENCR_AES
;
3228 if ((cred
->auth_type
& (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
)) ==
3230 wpa_printf(MSG_DEBUG
, "WPS: Upgrade auth_type WPAPSK -> "
3232 cred
->auth_type
|= WPS_AUTH_WPA2PSK
;
3234 #endif /* CONFIG_WPS2 */
3236 if (reg
->wps
->cred_cb
)
3237 return reg
->wps
->cred_cb(reg
->wps
->cb_ctx
, cred
);