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 #ifndef CONFIG_WPS_STRICT
31 #define WPS_WORKAROUNDS
32 #endif /* CONFIG_WPS_STRICT */
36 u8 uuid
[WPS_UUID_LEN
];
40 #define PIN_LOCKED BIT(0)
41 #define PIN_EXPIRES BIT(1)
43 struct os_time expiration
;
44 u8 enrollee_addr
[ETH_ALEN
];
48 static void wps_free_pin(struct wps_uuid_pin
*pin
)
55 static void wps_remove_pin(struct wps_uuid_pin
*pin
)
57 dl_list_del(&pin
->list
);
62 static void wps_free_pins(struct dl_list
*pins
)
64 struct wps_uuid_pin
*pin
, *prev
;
65 dl_list_for_each_safe(pin
, prev
, pins
, struct wps_uuid_pin
, list
)
70 struct wps_pbc_session
{
71 struct wps_pbc_session
*next
;
73 u8 uuid_e
[WPS_UUID_LEN
];
74 struct os_time timestamp
;
78 static void wps_free_pbc_sessions(struct wps_pbc_session
*pbc
)
80 struct wps_pbc_session
*prev
;
90 struct wps_registrar_device
{
91 struct wps_registrar_device
*next
;
92 struct wps_device_data dev
;
93 u8 uuid
[WPS_UUID_LEN
];
97 struct wps_registrar
{
98 struct wps_context
*wps
;
101 int selected_registrar
;
103 int (*new_psk_cb
)(void *ctx
, const u8
*mac_addr
, const u8
*psk
,
105 int (*set_ie_cb
)(void *ctx
, struct wpabuf
*beacon_ie
,
106 struct wpabuf
*probe_resp_ie
);
107 void (*pin_needed_cb
)(void *ctx
, const u8
*uuid_e
,
108 const struct wps_device_data
*dev
);
109 void (*reg_success_cb
)(void *ctx
, const u8
*mac_addr
,
111 void (*set_sel_reg_cb
)(void *ctx
, int sel_reg
, u16 dev_passwd_id
,
112 u16 sel_reg_config_methods
);
113 void (*enrollee_seen_cb
)(void *ctx
, const u8
*addr
, const u8
*uuid_e
,
114 const u8
*pri_dev_type
, u16 config_methods
,
115 u16 dev_password_id
, u8 request_type
,
116 const char *dev_name
);
120 struct wps_pbc_session
*pbc_sessions
;
123 struct wpabuf
*extra_cred
;
124 int disable_auto_conf
;
126 int sel_reg_dev_password_id_override
;
127 int sel_reg_config_methods_override
;
130 struct wps_registrar_device
*devices
;
132 int force_pbc_overlap
;
134 u8 authorized_macs
[WPS_MAX_AUTHORIZED_MACS
][ETH_ALEN
];
135 u8 authorized_macs_union
[WPS_MAX_AUTHORIZED_MACS
][ETH_ALEN
];
139 static int wps_set_ie(struct wps_registrar
*reg
);
140 static void wps_registrar_pbc_timeout(void *eloop_ctx
, void *timeout_ctx
);
141 static void wps_registrar_set_selected_timeout(void *eloop_ctx
,
145 static void wps_registrar_add_authorized_mac(struct wps_registrar
*reg
,
149 wpa_printf(MSG_DEBUG
, "WPS: Add authorized MAC " MACSTR
,
151 for (i
= 0; i
< WPS_MAX_AUTHORIZED_MACS
; i
++)
152 if (os_memcmp(reg
->authorized_macs
[i
], addr
, ETH_ALEN
) == 0) {
153 wpa_printf(MSG_DEBUG
, "WPS: Authorized MAC was "
154 "already in the list");
155 return; /* already in list */
157 for (i
= WPS_MAX_AUTHORIZED_MACS
- 1; i
> 0; i
--)
158 os_memcpy(reg
->authorized_macs
[i
], reg
->authorized_macs
[i
- 1],
160 os_memcpy(reg
->authorized_macs
[0], addr
, ETH_ALEN
);
161 wpa_hexdump(MSG_DEBUG
, "WPS: Authorized MACs",
162 (u8
*) reg
->authorized_macs
, sizeof(reg
->authorized_macs
));
166 static void wps_registrar_remove_authorized_mac(struct wps_registrar
*reg
,
170 wpa_printf(MSG_DEBUG
, "WPS: Remove authorized MAC " MACSTR
,
172 for (i
= 0; i
< WPS_MAX_AUTHORIZED_MACS
; i
++) {
173 if (os_memcmp(reg
->authorized_macs
, addr
, ETH_ALEN
) == 0)
176 if (i
== WPS_MAX_AUTHORIZED_MACS
) {
177 wpa_printf(MSG_DEBUG
, "WPS: Authorized MAC was not in the "
179 return; /* not in the list */
181 for (; i
+ 1 < WPS_MAX_AUTHORIZED_MACS
; i
++)
182 os_memcpy(reg
->authorized_macs
[i
], reg
->authorized_macs
[i
+ 1],
184 os_memset(reg
->authorized_macs
[WPS_MAX_AUTHORIZED_MACS
- 1], 0,
186 wpa_hexdump(MSG_DEBUG
, "WPS: Authorized MACs",
187 (u8
*) reg
->authorized_macs
, sizeof(reg
->authorized_macs
));
191 static void wps_free_devices(struct wps_registrar_device
*dev
)
193 struct wps_registrar_device
*prev
;
198 wps_device_data_free(&prev
->dev
);
204 static struct wps_registrar_device
* wps_device_get(struct wps_registrar
*reg
,
207 struct wps_registrar_device
*dev
;
209 for (dev
= reg
->devices
; dev
; dev
= dev
->next
) {
210 if (os_memcmp(dev
->dev
.mac_addr
, addr
, ETH_ALEN
) == 0)
217 static void wps_device_clone_data(struct wps_device_data
*dst
,
218 struct wps_device_data
*src
)
220 os_memcpy(dst
->mac_addr
, src
->mac_addr
, ETH_ALEN
);
221 os_memcpy(dst
->pri_dev_type
, src
->pri_dev_type
, WPS_DEV_TYPE_LEN
);
223 #define WPS_STRDUP(n) \
225 dst->n = src->n ? os_strdup(src->n) : NULL
227 WPS_STRDUP(device_name
);
228 WPS_STRDUP(manufacturer
);
229 WPS_STRDUP(model_name
);
230 WPS_STRDUP(model_number
);
231 WPS_STRDUP(serial_number
);
236 int wps_device_store(struct wps_registrar
*reg
,
237 struct wps_device_data
*dev
, const u8
*uuid
)
239 struct wps_registrar_device
*d
;
241 d
= wps_device_get(reg
, dev
->mac_addr
);
243 d
= os_zalloc(sizeof(*d
));
246 d
->next
= reg
->devices
;
250 wps_device_clone_data(&d
->dev
, dev
);
251 os_memcpy(d
->uuid
, uuid
, WPS_UUID_LEN
);
257 static void wps_registrar_add_pbc_session(struct wps_registrar
*reg
,
258 const u8
*addr
, const u8
*uuid_e
)
260 struct wps_pbc_session
*pbc
, *prev
= NULL
;
265 pbc
= reg
->pbc_sessions
;
267 if (os_memcmp(pbc
->addr
, addr
, ETH_ALEN
) == 0 &&
268 os_memcmp(pbc
->uuid_e
, uuid_e
, WPS_UUID_LEN
) == 0) {
270 prev
->next
= pbc
->next
;
272 reg
->pbc_sessions
= pbc
->next
;
280 pbc
= os_zalloc(sizeof(*pbc
));
283 os_memcpy(pbc
->addr
, addr
, ETH_ALEN
);
285 os_memcpy(pbc
->uuid_e
, uuid_e
, WPS_UUID_LEN
);
288 pbc
->next
= reg
->pbc_sessions
;
289 reg
->pbc_sessions
= pbc
;
290 pbc
->timestamp
= now
;
292 /* remove entries that have timed out */
297 if (now
.sec
> pbc
->timestamp
.sec
+ WPS_PBC_WALK_TIME
) {
299 wps_free_pbc_sessions(pbc
);
308 static void wps_registrar_remove_pbc_session(struct wps_registrar
*reg
,
309 const u8
*addr
, const u8
*uuid_e
)
311 struct wps_pbc_session
*pbc
, *prev
= NULL
;
313 pbc
= reg
->pbc_sessions
;
315 if (os_memcmp(pbc
->addr
, addr
, ETH_ALEN
) == 0 &&
316 os_memcmp(pbc
->uuid_e
, uuid_e
, WPS_UUID_LEN
) == 0) {
318 prev
->next
= pbc
->next
;
320 reg
->pbc_sessions
= pbc
->next
;
330 int wps_registrar_pbc_overlap(struct wps_registrar
*reg
,
331 const u8
*addr
, const u8
*uuid_e
)
334 struct wps_pbc_session
*pbc
;
339 for (pbc
= reg
->pbc_sessions
; pbc
; pbc
= pbc
->next
) {
340 if (now
.sec
> pbc
->timestamp
.sec
+ WPS_PBC_WALK_TIME
)
342 if (addr
== NULL
|| os_memcmp(addr
, pbc
->addr
, ETH_ALEN
) ||
344 os_memcmp(uuid_e
, pbc
->uuid_e
, WPS_UUID_LEN
))
351 return count
> 1 ? 1 : 0;
355 static int wps_build_wps_state(struct wps_context
*wps
, struct wpabuf
*msg
)
357 wpa_printf(MSG_DEBUG
, "WPS: * Wi-Fi Protected Setup State (%d)",
359 wpabuf_put_be16(msg
, ATTR_WPS_STATE
);
360 wpabuf_put_be16(msg
, 1);
361 wpabuf_put_u8(msg
, wps
->wps_state
);
366 #ifdef CONFIG_WPS_UPNP
367 static void wps_registrar_free_pending_m2(struct wps_context
*wps
)
369 struct upnp_pending_message
*p
, *p2
, *prev
= NULL
;
372 if (p
->type
== WPS_M2
|| p
->type
== WPS_M2D
) {
374 wps
->upnp_msgs
= p
->next
;
376 prev
->next
= p
->next
;
377 wpa_printf(MSG_DEBUG
, "WPS UPnP: Drop pending M2/M2D");
380 wpabuf_free(p2
->msg
);
388 #endif /* CONFIG_WPS_UPNP */
391 static int wps_build_ap_setup_locked(struct wps_context
*wps
,
394 if (wps
->ap_setup_locked
) {
395 wpa_printf(MSG_DEBUG
, "WPS: * AP Setup Locked");
396 wpabuf_put_be16(msg
, ATTR_AP_SETUP_LOCKED
);
397 wpabuf_put_be16(msg
, 1);
398 wpabuf_put_u8(msg
, 1);
404 static int wps_build_selected_registrar(struct wps_registrar
*reg
,
407 if (!reg
->sel_reg_union
)
409 wpa_printf(MSG_DEBUG
, "WPS: * Selected Registrar");
410 wpabuf_put_be16(msg
, ATTR_SELECTED_REGISTRAR
);
411 wpabuf_put_be16(msg
, 1);
412 wpabuf_put_u8(msg
, 1);
417 static int wps_build_sel_reg_dev_password_id(struct wps_registrar
*reg
,
420 u16 id
= reg
->pbc
? DEV_PW_PUSHBUTTON
: DEV_PW_DEFAULT
;
421 if (!reg
->sel_reg_union
)
423 if (reg
->sel_reg_dev_password_id_override
>= 0)
424 id
= reg
->sel_reg_dev_password_id_override
;
425 wpa_printf(MSG_DEBUG
, "WPS: * Device Password ID (%d)", id
);
426 wpabuf_put_be16(msg
, ATTR_DEV_PASSWORD_ID
);
427 wpabuf_put_be16(msg
, 2);
428 wpabuf_put_be16(msg
, id
);
433 static void wps_set_pushbutton(u16
*methods
, u16 conf_methods
)
435 *methods
|= WPS_CONFIG_PUSHBUTTON
;
437 if (conf_methods
& WPS_CONFIG_VIRT_PUSHBUTTON
)
438 *methods
|= WPS_CONFIG_VIRT_PUSHBUTTON
;
439 if (conf_methods
& WPS_CONFIG_PHY_PUSHBUTTON
)
440 *methods
|= WPS_CONFIG_PHY_PUSHBUTTON
;
441 if ((*methods
& WPS_CONFIG_VIRT_PUSHBUTTON
) !=
442 WPS_CONFIG_VIRT_PUSHBUTTON
||
443 (*methods
& WPS_CONFIG_PHY_PUSHBUTTON
) !=
444 WPS_CONFIG_PHY_PUSHBUTTON
) {
446 * Required to include virtual/physical flag, but we were not
447 * configured with push button type, so have to default to one
450 *methods
|= WPS_CONFIG_PHY_PUSHBUTTON
;
452 #endif /* CONFIG_WPS2 */
456 static int wps_build_sel_reg_config_methods(struct wps_registrar
*reg
,
460 if (!reg
->sel_reg_union
)
462 methods
= reg
->wps
->config_methods
;
463 methods
&= ~WPS_CONFIG_PUSHBUTTON
;
465 methods
&= ~(WPS_CONFIG_VIRT_PUSHBUTTON
|
466 WPS_CONFIG_PHY_PUSHBUTTON
);
467 #endif /* CONFIG_WPS2 */
469 wps_set_pushbutton(&methods
, reg
->wps
->config_methods
);
470 if (reg
->sel_reg_config_methods_override
>= 0)
471 methods
= reg
->sel_reg_config_methods_override
;
472 wpa_printf(MSG_DEBUG
, "WPS: * Selected Registrar Config Methods (%x)",
474 wpabuf_put_be16(msg
, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS
);
475 wpabuf_put_be16(msg
, 2);
476 wpabuf_put_be16(msg
, methods
);
481 static int wps_build_probe_config_methods(struct wps_registrar
*reg
,
486 * These are the methods that the AP supports as an Enrollee for adding
487 * external Registrars.
489 methods
= reg
->wps
->config_methods
& ~WPS_CONFIG_PUSHBUTTON
;
491 methods
&= ~(WPS_CONFIG_VIRT_PUSHBUTTON
|
492 WPS_CONFIG_PHY_PUSHBUTTON
);
493 #endif /* CONFIG_WPS2 */
494 wpa_printf(MSG_DEBUG
, "WPS: * Config Methods (%x)", methods
);
495 wpabuf_put_be16(msg
, ATTR_CONFIG_METHODS
);
496 wpabuf_put_be16(msg
, 2);
497 wpabuf_put_be16(msg
, methods
);
502 static int wps_build_config_methods_r(struct wps_registrar
*reg
,
506 methods
= reg
->wps
->config_methods
& ~WPS_CONFIG_PUSHBUTTON
;
508 methods
&= ~(WPS_CONFIG_VIRT_PUSHBUTTON
|
509 WPS_CONFIG_PHY_PUSHBUTTON
);
510 #endif /* CONFIG_WPS2 */
512 wps_set_pushbutton(&methods
, reg
->wps
->config_methods
);
513 return wps_build_config_methods(msg
, methods
);
517 const u8
* wps_authorized_macs(struct wps_registrar
*reg
, size_t *count
)
522 while (*count
< WPS_MAX_AUTHORIZED_MACS
) {
523 if (is_zero_ether_addr(reg
->authorized_macs_union
[*count
]))
527 #endif /* CONFIG_WPS2 */
529 return (const u8
*) reg
->authorized_macs_union
;
534 * wps_registrar_init - Initialize WPS Registrar data
535 * @wps: Pointer to longterm WPS context
536 * @cfg: Registrar configuration
537 * Returns: Pointer to allocated Registrar data or %NULL on failure
539 * This function is used to initialize WPS Registrar functionality. It can be
540 * used for a single Registrar run (e.g., when run in a supplicant) or multiple
541 * runs (e.g., when run as an internal Registrar in an AP). Caller is
542 * responsible for freeing the returned data with wps_registrar_deinit() when
543 * Registrar functionality is not needed anymore.
545 struct wps_registrar
*
546 wps_registrar_init(struct wps_context
*wps
,
547 const struct wps_registrar_config
*cfg
)
549 struct wps_registrar
*reg
= os_zalloc(sizeof(*reg
));
553 dl_list_init(®
->pins
);
555 reg
->new_psk_cb
= cfg
->new_psk_cb
;
556 reg
->set_ie_cb
= cfg
->set_ie_cb
;
557 reg
->pin_needed_cb
= cfg
->pin_needed_cb
;
558 reg
->reg_success_cb
= cfg
->reg_success_cb
;
559 reg
->set_sel_reg_cb
= cfg
->set_sel_reg_cb
;
560 reg
->enrollee_seen_cb
= cfg
->enrollee_seen_cb
;
561 reg
->cb_ctx
= cfg
->cb_ctx
;
562 reg
->skip_cred_build
= cfg
->skip_cred_build
;
563 if (cfg
->extra_cred
) {
564 reg
->extra_cred
= wpabuf_alloc_copy(cfg
->extra_cred
,
565 cfg
->extra_cred_len
);
566 if (reg
->extra_cred
== NULL
) {
571 reg
->disable_auto_conf
= cfg
->disable_auto_conf
;
572 reg
->sel_reg_dev_password_id_override
= -1;
573 reg
->sel_reg_config_methods_override
= -1;
574 reg
->static_wep_only
= cfg
->static_wep_only
;
576 if (wps_set_ie(reg
)) {
577 wps_registrar_deinit(reg
);
586 * wps_registrar_deinit - Deinitialize WPS Registrar data
587 * @reg: Registrar data from wps_registrar_init()
589 void wps_registrar_deinit(struct wps_registrar
*reg
)
593 eloop_cancel_timeout(wps_registrar_pbc_timeout
, reg
, NULL
);
594 eloop_cancel_timeout(wps_registrar_set_selected_timeout
, reg
, NULL
);
595 wps_free_pins(®
->pins
);
596 wps_free_pbc_sessions(reg
->pbc_sessions
);
597 wpabuf_free(reg
->extra_cred
);
598 wps_free_devices(reg
->devices
);
604 * wps_registrar_add_pin - Configure a new PIN for Registrar
605 * @reg: Registrar data from wps_registrar_init()
606 * @addr: Enrollee MAC address or %NULL if not known
607 * @uuid: UUID-E or %NULL for wildcard (any UUID)
608 * @pin: PIN (Device Password)
609 * @pin_len: Length of pin in octets
610 * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
611 * Returns: 0 on success, -1 on failure
613 int wps_registrar_add_pin(struct wps_registrar
*reg
, const u8
*addr
,
614 const u8
*uuid
, const u8
*pin
, size_t pin_len
,
617 struct wps_uuid_pin
*p
;
619 p
= os_zalloc(sizeof(*p
));
623 os_memcpy(p
->enrollee_addr
, addr
, ETH_ALEN
);
625 p
->wildcard_uuid
= 1;
627 os_memcpy(p
->uuid
, uuid
, WPS_UUID_LEN
);
628 p
->pin
= os_malloc(pin_len
);
629 if (p
->pin
== NULL
) {
633 os_memcpy(p
->pin
, pin
, pin_len
);
634 p
->pin_len
= pin_len
;
637 p
->flags
|= PIN_EXPIRES
;
638 os_get_time(&p
->expiration
);
639 p
->expiration
.sec
+= timeout
;
642 dl_list_add(®
->pins
, &p
->list
);
644 wpa_printf(MSG_DEBUG
, "WPS: A new PIN configured (timeout=%d)",
646 wpa_hexdump(MSG_DEBUG
, "WPS: UUID", uuid
, WPS_UUID_LEN
);
647 wpa_hexdump_ascii_key(MSG_DEBUG
, "WPS: PIN", pin
, pin_len
);
648 reg
->selected_registrar
= 1;
651 wps_registrar_add_authorized_mac(reg
, addr
);
653 wps_registrar_add_authorized_mac(
654 reg
, (u8
*) "\xff\xff\xff\xff\xff\xff");
655 wps_registrar_selected_registrar_changed(reg
);
656 eloop_cancel_timeout(wps_registrar_set_selected_timeout
, reg
, NULL
);
657 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0,
658 wps_registrar_set_selected_timeout
,
665 static void wps_registrar_remove_pin(struct wps_registrar
*reg
,
666 struct wps_uuid_pin
*pin
)
669 u8 bcast
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
671 if (is_zero_ether_addr(pin
->enrollee_addr
))
674 addr
= pin
->enrollee_addr
;
675 wps_registrar_remove_authorized_mac(reg
, addr
);
677 wps_registrar_selected_registrar_changed(reg
);
681 static void wps_registrar_expire_pins(struct wps_registrar
*reg
)
683 struct wps_uuid_pin
*pin
, *prev
;
687 dl_list_for_each_safe(pin
, prev
, ®
->pins
, struct wps_uuid_pin
, list
)
689 if ((pin
->flags
& PIN_EXPIRES
) &&
690 os_time_before(&pin
->expiration
, &now
)) {
691 wpa_hexdump(MSG_DEBUG
, "WPS: Expired PIN for UUID",
692 pin
->uuid
, WPS_UUID_LEN
);
693 wps_registrar_remove_pin(reg
, pin
);
700 * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
701 * @reg: Registrar data from wps_registrar_init()
702 * Returns: 0 on success, -1 if not wildcard PIN is enabled
704 static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar
*reg
)
706 struct wps_uuid_pin
*pin
, *prev
;
708 dl_list_for_each_safe(pin
, prev
, ®
->pins
, struct wps_uuid_pin
, list
)
710 if (pin
->wildcard_uuid
) {
711 wpa_hexdump(MSG_DEBUG
, "WPS: Invalidated PIN for UUID",
712 pin
->uuid
, WPS_UUID_LEN
);
713 wps_registrar_remove_pin(reg
, pin
);
723 * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
724 * @reg: Registrar data from wps_registrar_init()
726 * Returns: 0 on success, -1 on failure (e.g., PIN not found)
728 int wps_registrar_invalidate_pin(struct wps_registrar
*reg
, const u8
*uuid
)
730 struct wps_uuid_pin
*pin
, *prev
;
732 dl_list_for_each_safe(pin
, prev
, ®
->pins
, struct wps_uuid_pin
, list
)
734 if (os_memcmp(pin
->uuid
, uuid
, WPS_UUID_LEN
) == 0) {
735 wpa_hexdump(MSG_DEBUG
, "WPS: Invalidated PIN for UUID",
736 pin
->uuid
, WPS_UUID_LEN
);
737 wps_registrar_remove_pin(reg
, pin
);
746 static const u8
* wps_registrar_get_pin(struct wps_registrar
*reg
,
747 const u8
*uuid
, size_t *pin_len
)
749 struct wps_uuid_pin
*pin
, *found
= NULL
;
751 wps_registrar_expire_pins(reg
);
753 dl_list_for_each(pin
, ®
->pins
, struct wps_uuid_pin
, list
) {
754 if (!pin
->wildcard_uuid
&&
755 os_memcmp(pin
->uuid
, uuid
, WPS_UUID_LEN
) == 0) {
762 /* Check for wildcard UUIDs since none of the UUID-specific
764 dl_list_for_each(pin
, ®
->pins
, struct wps_uuid_pin
, list
) {
765 if (pin
->wildcard_uuid
== 1) {
766 wpa_printf(MSG_DEBUG
, "WPS: Found a wildcard "
767 "PIN. Assigned it for this UUID-E");
768 pin
->wildcard_uuid
= 2;
769 os_memcpy(pin
->uuid
, uuid
, WPS_UUID_LEN
);
780 * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
781 * that could otherwise avoid PIN invalidations.
783 if (found
->flags
& PIN_LOCKED
) {
784 wpa_printf(MSG_DEBUG
, "WPS: Selected PIN locked - do not "
785 "allow concurrent re-use");
788 *pin_len
= found
->pin_len
;
789 found
->flags
|= PIN_LOCKED
;
795 * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
796 * @reg: Registrar data from wps_registrar_init()
798 * Returns: 0 on success, -1 on failure
800 * PINs are locked to enforce only one concurrent use. This function unlocks a
801 * PIN to allow it to be used again. If the specified PIN was configured using
802 * a wildcard UUID, it will be removed instead of allowing multiple uses.
804 int wps_registrar_unlock_pin(struct wps_registrar
*reg
, const u8
*uuid
)
806 struct wps_uuid_pin
*pin
;
808 dl_list_for_each(pin
, ®
->pins
, struct wps_uuid_pin
, list
) {
809 if (os_memcmp(pin
->uuid
, uuid
, WPS_UUID_LEN
) == 0) {
810 if (pin
->wildcard_uuid
== 2) {
811 wpa_printf(MSG_DEBUG
, "WPS: Invalidating used "
813 return wps_registrar_invalidate_pin(reg
, uuid
);
815 pin
->flags
&= ~PIN_LOCKED
;
824 static void wps_registrar_stop_pbc(struct wps_registrar
*reg
)
826 reg
->selected_registrar
= 0;
828 wps_registrar_selected_registrar_changed(reg
);
832 static void wps_registrar_pbc_timeout(void *eloop_ctx
, void *timeout_ctx
)
834 struct wps_registrar
*reg
= eloop_ctx
;
836 wpa_printf(MSG_DEBUG
, "WPS: PBC timed out - disable PBC mode");
837 wps_pbc_timeout_event(reg
->wps
);
838 wps_registrar_stop_pbc(reg
);
843 * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
844 * @reg: Registrar data from wps_registrar_init()
845 * Returns: 0 on success, -1 on failure
847 * This function is called on an AP when a push button is pushed to activate
848 * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
849 * or when a PBC registration is completed.
851 int wps_registrar_button_pushed(struct wps_registrar
*reg
)
853 if (wps_registrar_pbc_overlap(reg
, NULL
, NULL
)) {
854 wpa_printf(MSG_DEBUG
, "WPS: PBC overlap - do not start PBC "
856 wps_pbc_overlap_event(reg
->wps
);
859 wpa_printf(MSG_DEBUG
, "WPS: Button pushed - PBC mode started");
860 reg
->force_pbc_overlap
= 0;
861 reg
->selected_registrar
= 1;
863 wps_registrar_selected_registrar_changed(reg
);
865 eloop_cancel_timeout(wps_registrar_pbc_timeout
, reg
, NULL
);
866 eloop_register_timeout(WPS_PBC_WALK_TIME
, 0, wps_registrar_pbc_timeout
,
872 static void wps_registrar_pbc_completed(struct wps_registrar
*reg
)
874 wpa_printf(MSG_DEBUG
, "WPS: PBC completed - stopping PBC mode");
875 eloop_cancel_timeout(wps_registrar_pbc_timeout
, reg
, NULL
);
876 wps_registrar_stop_pbc(reg
);
880 static void wps_registrar_pin_completed(struct wps_registrar
*reg
)
882 wpa_printf(MSG_DEBUG
, "WPS: PIN completed using internal Registrar");
883 eloop_cancel_timeout(wps_registrar_set_selected_timeout
, reg
, NULL
);
884 reg
->selected_registrar
= 0;
885 wps_registrar_selected_registrar_changed(reg
);
889 int wps_registrar_wps_cancel(struct wps_registrar
*reg
)
892 wpa_printf(MSG_DEBUG
, "WPS: PBC is set - cancelling it");
893 wps_registrar_pbc_timeout(reg
, NULL
);
895 } else if (reg
->selected_registrar
) {
897 wpa_printf(MSG_DEBUG
, "WPS: PIN is set - cancelling it");
898 wps_registrar_pin_completed(reg
);
899 wps_registrar_invalidate_wildcard_pin(reg
);
907 * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
908 * @reg: Registrar data from wps_registrar_init()
909 * @addr: MAC address of the Probe Request sender
910 * @wps_data: WPS IE contents
912 * This function is called on an AP when a Probe Request with WPS IE is
913 * received. This is used to track PBC mode use and to detect possible overlap
914 * situation with other WPS APs.
916 void wps_registrar_probe_req_rx(struct wps_registrar
*reg
, const u8
*addr
,
917 const struct wpabuf
*wps_data
,
920 struct wps_parse_attr attr
;
922 wpa_hexdump_buf(MSG_MSGDUMP
,
923 "WPS: Probe Request with WPS data received",
926 if (wps_parse_msg(wps_data
, &attr
) < 0)
929 if (attr
.config_methods
== NULL
) {
930 wpa_printf(MSG_DEBUG
, "WPS: No Config Methods attribute in "
935 if (attr
.dev_password_id
== NULL
) {
936 wpa_printf(MSG_DEBUG
, "WPS: No Device Password Id attribute "
941 if (reg
->enrollee_seen_cb
&& attr
.uuid_e
&&
942 attr
.primary_dev_type
&& attr
.request_type
&& !p2p_wildcard
) {
943 char *dev_name
= NULL
;
945 dev_name
= os_zalloc(attr
.dev_name_len
+ 1);
947 os_memcpy(dev_name
, attr
.dev_name
,
951 reg
->enrollee_seen_cb(reg
->cb_ctx
, addr
, attr
.uuid_e
,
952 attr
.primary_dev_type
,
953 WPA_GET_BE16(attr
.config_methods
),
954 WPA_GET_BE16(attr
.dev_password_id
),
955 *attr
.request_type
, dev_name
);
959 if (WPA_GET_BE16(attr
.dev_password_id
) != DEV_PW_PUSHBUTTON
)
960 return; /* Not PBC */
962 wpa_printf(MSG_DEBUG
, "WPS: Probe Request for PBC received from "
963 MACSTR
, MAC2STR(addr
));
964 if (attr
.uuid_e
== NULL
) {
965 wpa_printf(MSG_DEBUG
, "WPS: Invalid Probe Request WPS IE: No "
970 wps_registrar_add_pbc_session(reg
, addr
, attr
.uuid_e
);
971 if (wps_registrar_pbc_overlap(reg
, addr
, attr
.uuid_e
)) {
972 wpa_printf(MSG_DEBUG
, "WPS: PBC session overlap detected");
973 reg
->force_pbc_overlap
= 1;
974 wps_pbc_overlap_event(reg
->wps
);
979 static int wps_cb_new_psk(struct wps_registrar
*reg
, const u8
*mac_addr
,
980 const u8
*psk
, size_t psk_len
)
982 if (reg
->new_psk_cb
== NULL
)
985 return reg
->new_psk_cb(reg
->cb_ctx
, mac_addr
, psk
, psk_len
);
989 static void wps_cb_pin_needed(struct wps_registrar
*reg
, const u8
*uuid_e
,
990 const struct wps_device_data
*dev
)
992 if (reg
->pin_needed_cb
== NULL
)
995 reg
->pin_needed_cb(reg
->cb_ctx
, uuid_e
, dev
);
999 static void wps_cb_reg_success(struct wps_registrar
*reg
, const u8
*mac_addr
,
1002 if (reg
->reg_success_cb
== NULL
)
1005 reg
->reg_success_cb(reg
->cb_ctx
, mac_addr
, uuid_e
);
1009 static int wps_cb_set_ie(struct wps_registrar
*reg
, struct wpabuf
*beacon_ie
,
1010 struct wpabuf
*probe_resp_ie
)
1012 return reg
->set_ie_cb(reg
->cb_ctx
, beacon_ie
, probe_resp_ie
);
1016 static void wps_cb_set_sel_reg(struct wps_registrar
*reg
)
1019 if (reg
->set_sel_reg_cb
== NULL
)
1022 if (reg
->selected_registrar
) {
1023 methods
= reg
->wps
->config_methods
& ~WPS_CONFIG_PUSHBUTTON
;
1025 methods
&= ~(WPS_CONFIG_VIRT_PUSHBUTTON
|
1026 WPS_CONFIG_PHY_PUSHBUTTON
);
1027 #endif /* CONFIG_WPS2 */
1029 wps_set_pushbutton(&methods
, reg
->wps
->config_methods
);
1032 wpa_printf(MSG_DEBUG
, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
1033 "config_methods=0x%x pbc=%d methods=0x%x",
1034 reg
->selected_registrar
, reg
->wps
->config_methods
,
1037 reg
->set_sel_reg_cb(reg
->cb_ctx
, reg
->selected_registrar
,
1038 reg
->pbc
? DEV_PW_PUSHBUTTON
: DEV_PW_DEFAULT
,
1043 static int wps_set_ie(struct wps_registrar
*reg
)
1045 struct wpabuf
*beacon
;
1046 struct wpabuf
*probe
;
1047 const u8
*auth_macs
;
1050 if (reg
->set_ie_cb
== NULL
)
1053 beacon
= wpabuf_alloc(400);
1056 probe
= wpabuf_alloc(500);
1057 if (probe
== NULL
) {
1058 wpabuf_free(beacon
);
1062 auth_macs
= wps_authorized_macs(reg
, &count
);
1064 wpa_printf(MSG_DEBUG
, "WPS: Build Beacon IEs");
1066 if (wps_build_version(beacon
) ||
1067 wps_build_wps_state(reg
->wps
, beacon
) ||
1068 wps_build_ap_setup_locked(reg
->wps
, beacon
) ||
1069 wps_build_selected_registrar(reg
, beacon
) ||
1070 wps_build_sel_reg_dev_password_id(reg
, beacon
) ||
1071 wps_build_sel_reg_config_methods(reg
, beacon
) ||
1072 wps_build_wfa_ext(beacon
, 0, auth_macs
, count
)) {
1073 wpabuf_free(beacon
);
1078 wpa_printf(MSG_DEBUG
, "WPS: Build Probe Response IEs");
1080 if (wps_build_version(probe
) ||
1081 wps_build_wps_state(reg
->wps
, probe
) ||
1082 wps_build_ap_setup_locked(reg
->wps
, probe
) ||
1083 wps_build_selected_registrar(reg
, probe
) ||
1084 wps_build_sel_reg_dev_password_id(reg
, probe
) ||
1085 wps_build_sel_reg_config_methods(reg
, probe
) ||
1086 wps_build_resp_type(probe
, reg
->wps
->ap
? WPS_RESP_AP
:
1087 WPS_RESP_REGISTRAR
) ||
1088 wps_build_uuid_e(probe
, reg
->wps
->uuid
) ||
1089 wps_build_device_attrs(®
->wps
->dev
, probe
) ||
1090 wps_build_probe_config_methods(reg
, probe
) ||
1091 wps_build_rf_bands(®
->wps
->dev
, probe
) ||
1092 wps_build_wfa_ext(probe
, 0, auth_macs
, count
)) {
1093 wpabuf_free(beacon
);
1099 if (wps_build_dev_name(®
->wps
->dev
, beacon
) ||
1100 wps_build_primary_dev_type(®
->wps
->dev
, beacon
)) {
1101 wpabuf_free(beacon
);
1105 #endif /* CONFIG_P2P */
1107 beacon
= wps_ie_encapsulate(beacon
);
1108 probe
= wps_ie_encapsulate(probe
);
1110 if (!beacon
|| !probe
) {
1111 wpabuf_free(beacon
);
1116 if (reg
->static_wep_only
) {
1118 * Windows XP and Vista clients can get confused about
1119 * EAP-Identity/Request when they probe the network with
1120 * EAPOL-Start. In such a case, they may assume the network is
1121 * using IEEE 802.1X and prompt user for a certificate while
1122 * the correct (non-WPS) behavior would be to ask for the
1123 * static WEP key. As a workaround, use Microsoft Provisioning
1124 * IE to advertise that legacy 802.1X is not supported.
1126 const u8 ms_wps
[7] = {
1127 WLAN_EID_VENDOR_SPECIFIC
, 5,
1128 /* Microsoft Provisioning IE (00:50:f2:5) */
1129 0x00, 0x50, 0xf2, 5,
1130 0x00 /* no legacy 802.1X or MS WPS */
1132 wpa_printf(MSG_DEBUG
, "WPS: Add Microsoft Provisioning IE "
1133 "into Beacon/Probe Response frames");
1134 wpabuf_put_data(beacon
, ms_wps
, sizeof(ms_wps
));
1135 wpabuf_put_data(probe
, ms_wps
, sizeof(ms_wps
));
1138 return wps_cb_set_ie(reg
, beacon
, probe
);
1142 static int wps_get_dev_password(struct wps_data
*wps
)
1147 os_free(wps
->dev_password
);
1148 wps
->dev_password
= NULL
;
1151 wpa_printf(MSG_DEBUG
, "WPS: Use default PIN for PBC");
1152 pin
= (const u8
*) "00000000";
1155 pin
= wps_registrar_get_pin(wps
->wps
->registrar
, wps
->uuid_e
,
1159 wpa_printf(MSG_DEBUG
, "WPS: No Device Password available for "
1161 wps_cb_pin_needed(wps
->wps
->registrar
, wps
->uuid_e
,
1166 wps
->dev_password
= os_malloc(pin_len
);
1167 if (wps
->dev_password
== NULL
)
1169 os_memcpy(wps
->dev_password
, pin
, pin_len
);
1170 wps
->dev_password_len
= pin_len
;
1176 static int wps_build_uuid_r(struct wps_data
*wps
, struct wpabuf
*msg
)
1178 wpa_printf(MSG_DEBUG
, "WPS: * UUID-R");
1179 wpabuf_put_be16(msg
, ATTR_UUID_R
);
1180 wpabuf_put_be16(msg
, WPS_UUID_LEN
);
1181 wpabuf_put_data(msg
, wps
->uuid_r
, WPS_UUID_LEN
);
1186 static int wps_build_r_hash(struct wps_data
*wps
, struct wpabuf
*msg
)
1192 if (os_get_random(wps
->snonce
, 2 * WPS_SECRET_NONCE_LEN
) < 0)
1194 wpa_hexdump(MSG_DEBUG
, "WPS: R-S1", wps
->snonce
, WPS_SECRET_NONCE_LEN
);
1195 wpa_hexdump(MSG_DEBUG
, "WPS: R-S2",
1196 wps
->snonce
+ WPS_SECRET_NONCE_LEN
, WPS_SECRET_NONCE_LEN
);
1198 if (wps
->dh_pubkey_e
== NULL
|| wps
->dh_pubkey_r
== NULL
) {
1199 wpa_printf(MSG_DEBUG
, "WPS: DH public keys not available for "
1200 "R-Hash derivation");
1204 wpa_printf(MSG_DEBUG
, "WPS: * R-Hash1");
1205 wpabuf_put_be16(msg
, ATTR_R_HASH1
);
1206 wpabuf_put_be16(msg
, SHA256_MAC_LEN
);
1207 hash
= wpabuf_put(msg
, SHA256_MAC_LEN
);
1208 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1209 addr
[0] = wps
->snonce
;
1210 len
[0] = WPS_SECRET_NONCE_LEN
;
1211 addr
[1] = wps
->psk1
;
1212 len
[1] = WPS_PSK_LEN
;
1213 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
1214 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
1215 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
1216 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
1217 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
1218 wpa_hexdump(MSG_DEBUG
, "WPS: R-Hash1", hash
, SHA256_MAC_LEN
);
1220 wpa_printf(MSG_DEBUG
, "WPS: * R-Hash2");
1221 wpabuf_put_be16(msg
, ATTR_R_HASH2
);
1222 wpabuf_put_be16(msg
, SHA256_MAC_LEN
);
1223 hash
= wpabuf_put(msg
, SHA256_MAC_LEN
);
1224 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1225 addr
[0] = wps
->snonce
+ WPS_SECRET_NONCE_LEN
;
1226 addr
[1] = wps
->psk2
;
1227 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
1228 wpa_hexdump(MSG_DEBUG
, "WPS: R-Hash2", hash
, SHA256_MAC_LEN
);
1234 static int wps_build_r_snonce1(struct wps_data
*wps
, struct wpabuf
*msg
)
1236 wpa_printf(MSG_DEBUG
, "WPS: * R-SNonce1");
1237 wpabuf_put_be16(msg
, ATTR_R_SNONCE1
);
1238 wpabuf_put_be16(msg
, WPS_SECRET_NONCE_LEN
);
1239 wpabuf_put_data(msg
, wps
->snonce
, WPS_SECRET_NONCE_LEN
);
1244 static int wps_build_r_snonce2(struct wps_data
*wps
, struct wpabuf
*msg
)
1246 wpa_printf(MSG_DEBUG
, "WPS: * R-SNonce2");
1247 wpabuf_put_be16(msg
, ATTR_R_SNONCE2
);
1248 wpabuf_put_be16(msg
, WPS_SECRET_NONCE_LEN
);
1249 wpabuf_put_data(msg
, wps
->snonce
+ WPS_SECRET_NONCE_LEN
,
1250 WPS_SECRET_NONCE_LEN
);
1255 static int wps_build_cred_network_idx(struct wpabuf
*msg
,
1256 const struct wps_credential
*cred
)
1258 wpa_printf(MSG_DEBUG
, "WPS: * Network Index");
1259 wpabuf_put_be16(msg
, ATTR_NETWORK_INDEX
);
1260 wpabuf_put_be16(msg
, 1);
1261 wpabuf_put_u8(msg
, 1);
1266 static int wps_build_cred_ssid(struct wpabuf
*msg
,
1267 const struct wps_credential
*cred
)
1269 wpa_printf(MSG_DEBUG
, "WPS: * SSID");
1270 wpabuf_put_be16(msg
, ATTR_SSID
);
1271 wpabuf_put_be16(msg
, cred
->ssid_len
);
1272 wpabuf_put_data(msg
, cred
->ssid
, cred
->ssid_len
);
1277 static int wps_build_cred_auth_type(struct wpabuf
*msg
,
1278 const struct wps_credential
*cred
)
1280 wpa_printf(MSG_DEBUG
, "WPS: * Authentication Type (0x%x)",
1282 wpabuf_put_be16(msg
, ATTR_AUTH_TYPE
);
1283 wpabuf_put_be16(msg
, 2);
1284 wpabuf_put_be16(msg
, cred
->auth_type
);
1289 static int wps_build_cred_encr_type(struct wpabuf
*msg
,
1290 const struct wps_credential
*cred
)
1292 wpa_printf(MSG_DEBUG
, "WPS: * Encryption Type (0x%x)",
1294 wpabuf_put_be16(msg
, ATTR_ENCR_TYPE
);
1295 wpabuf_put_be16(msg
, 2);
1296 wpabuf_put_be16(msg
, cred
->encr_type
);
1301 static int wps_build_cred_network_key(struct wpabuf
*msg
,
1302 const struct wps_credential
*cred
)
1304 wpa_printf(MSG_DEBUG
, "WPS: * Network Key (len=%d)",
1305 (int) cred
->key_len
);
1306 wpabuf_put_be16(msg
, ATTR_NETWORK_KEY
);
1307 wpabuf_put_be16(msg
, cred
->key_len
);
1308 wpabuf_put_data(msg
, cred
->key
, cred
->key_len
);
1313 static int wps_build_cred_mac_addr(struct wpabuf
*msg
,
1314 const struct wps_credential
*cred
)
1316 wpa_printf(MSG_DEBUG
, "WPS: * MAC Address (" MACSTR
")",
1317 MAC2STR(cred
->mac_addr
));
1318 wpabuf_put_be16(msg
, ATTR_MAC_ADDR
);
1319 wpabuf_put_be16(msg
, ETH_ALEN
);
1320 wpabuf_put_data(msg
, cred
->mac_addr
, ETH_ALEN
);
1325 static int wps_build_credential(struct wpabuf
*msg
,
1326 const struct wps_credential
*cred
)
1328 if (wps_build_cred_network_idx(msg
, cred
) ||
1329 wps_build_cred_ssid(msg
, cred
) ||
1330 wps_build_cred_auth_type(msg
, cred
) ||
1331 wps_build_cred_encr_type(msg
, cred
) ||
1332 wps_build_cred_network_key(msg
, cred
) ||
1333 wps_build_cred_mac_addr(msg
, cred
))
1339 int wps_build_cred(struct wps_data
*wps
, struct wpabuf
*msg
)
1341 struct wpabuf
*cred
;
1343 if (wps
->wps
->registrar
->skip_cred_build
)
1344 goto skip_cred_build
;
1346 wpa_printf(MSG_DEBUG
, "WPS: * Credential");
1347 if (wps
->use_cred
) {
1348 os_memcpy(&wps
->cred
, wps
->use_cred
, sizeof(wps
->cred
));
1351 os_memset(&wps
->cred
, 0, sizeof(wps
->cred
));
1353 os_memcpy(wps
->cred
.ssid
, wps
->wps
->ssid
, wps
->wps
->ssid_len
);
1354 wps
->cred
.ssid_len
= wps
->wps
->ssid_len
;
1356 /* Select the best authentication and encryption type */
1357 if (wps
->auth_type
& WPS_AUTH_WPA2PSK
)
1358 wps
->auth_type
= WPS_AUTH_WPA2PSK
;
1359 else if (wps
->auth_type
& WPS_AUTH_WPAPSK
)
1360 wps
->auth_type
= WPS_AUTH_WPAPSK
;
1361 else if (wps
->auth_type
& WPS_AUTH_OPEN
)
1362 wps
->auth_type
= WPS_AUTH_OPEN
;
1363 else if (wps
->auth_type
& WPS_AUTH_SHARED
)
1364 wps
->auth_type
= WPS_AUTH_SHARED
;
1366 wpa_printf(MSG_DEBUG
, "WPS: Unsupported auth_type 0x%x",
1370 wps
->cred
.auth_type
= wps
->auth_type
;
1372 if (wps
->auth_type
== WPS_AUTH_WPA2PSK
||
1373 wps
->auth_type
== WPS_AUTH_WPAPSK
) {
1374 if (wps
->encr_type
& WPS_ENCR_AES
)
1375 wps
->encr_type
= WPS_ENCR_AES
;
1376 else if (wps
->encr_type
& WPS_ENCR_TKIP
)
1377 wps
->encr_type
= WPS_ENCR_TKIP
;
1379 wpa_printf(MSG_DEBUG
, "WPS: No suitable encryption "
1380 "type for WPA/WPA2");
1384 if (wps
->encr_type
& WPS_ENCR_WEP
)
1385 wps
->encr_type
= WPS_ENCR_WEP
;
1386 else if (wps
->encr_type
& WPS_ENCR_NONE
)
1387 wps
->encr_type
= WPS_ENCR_NONE
;
1389 wpa_printf(MSG_DEBUG
, "WPS: No suitable encryption "
1390 "type for non-WPA/WPA2 mode");
1394 wps
->cred
.encr_type
= wps
->encr_type
;
1396 * Set MAC address in the Credential to be the Enrollee's MAC address
1398 os_memcpy(wps
->cred
.mac_addr
, wps
->mac_addr_e
, ETH_ALEN
);
1400 if (wps
->wps
->wps_state
== WPS_STATE_NOT_CONFIGURED
&& wps
->wps
->ap
&&
1401 !wps
->wps
->registrar
->disable_auto_conf
) {
1403 /* Generate a random passphrase */
1404 if (os_get_random(r
, sizeof(r
)) < 0)
1406 os_free(wps
->new_psk
);
1407 wps
->new_psk
= base64_encode(r
, sizeof(r
), &wps
->new_psk_len
);
1408 if (wps
->new_psk
== NULL
)
1410 wps
->new_psk_len
--; /* remove newline */
1411 while (wps
->new_psk_len
&&
1412 wps
->new_psk
[wps
->new_psk_len
- 1] == '=')
1414 wpa_hexdump_ascii_key(MSG_DEBUG
, "WPS: Generated passphrase",
1415 wps
->new_psk
, wps
->new_psk_len
);
1416 os_memcpy(wps
->cred
.key
, wps
->new_psk
, wps
->new_psk_len
);
1417 wps
->cred
.key_len
= wps
->new_psk_len
;
1418 } else if (wps
->use_psk_key
&& wps
->wps
->psk_set
) {
1420 wpa_printf(MSG_DEBUG
, "WPS: Use PSK format for Network Key");
1421 wpa_snprintf_hex(hex
, sizeof(hex
), wps
->wps
->psk
, 32);
1422 os_memcpy(wps
->cred
.key
, hex
, 32 * 2);
1423 wps
->cred
.key_len
= 32 * 2;
1424 } else if (wps
->wps
->network_key
) {
1425 os_memcpy(wps
->cred
.key
, wps
->wps
->network_key
,
1426 wps
->wps
->network_key_len
);
1427 wps
->cred
.key_len
= wps
->wps
->network_key_len
;
1428 } else if (wps
->auth_type
& (WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
)) {
1430 /* Generate a random per-device PSK */
1431 os_free(wps
->new_psk
);
1432 wps
->new_psk_len
= 32;
1433 wps
->new_psk
= os_malloc(wps
->new_psk_len
);
1434 if (wps
->new_psk
== NULL
)
1436 if (os_get_random(wps
->new_psk
, wps
->new_psk_len
) < 0) {
1437 os_free(wps
->new_psk
);
1438 wps
->new_psk
= NULL
;
1441 wpa_hexdump_key(MSG_DEBUG
, "WPS: Generated per-device PSK",
1442 wps
->new_psk
, wps
->new_psk_len
);
1443 wpa_snprintf_hex(hex
, sizeof(hex
), wps
->new_psk
,
1445 os_memcpy(wps
->cred
.key
, hex
, wps
->new_psk_len
* 2);
1446 wps
->cred
.key_len
= wps
->new_psk_len
* 2;
1450 #ifdef CONFIG_WPS_TESTING_EXTRA_CRED
1451 cred
= wpabuf_alloc(200);
1453 struct wps_credential dummy
;
1454 wpa_printf(MSG_DEBUG
, "WPS: Add dummy credential");
1455 os_memset(&dummy
, 0, sizeof(dummy
));
1456 os_memcpy(dummy
.ssid
, "dummy", 5);
1458 dummy
.auth_type
= WPS_AUTH_WPA2PSK
;
1459 dummy
.encr_type
= WPS_ENCR_AES
;
1460 os_memcpy(dummy
.key
, "dummy psk", 9);
1462 os_memcpy(dummy
.mac_addr
, wps
->mac_addr_e
, ETH_ALEN
);
1463 wps_build_credential(cred
, &dummy
);
1464 wpa_hexdump_buf(MSG_DEBUG
, "WPS: Dummy Credential", cred
);
1466 wpabuf_put_be16(msg
, ATTR_CRED
);
1467 wpabuf_put_be16(msg
, wpabuf_len(cred
));
1468 wpabuf_put_buf(msg
, cred
);
1472 #endif /* CONFIG_WPS_TESTING_EXTRA_CRED */
1474 cred
= wpabuf_alloc(200);
1478 if (wps_build_credential(cred
, &wps
->cred
)) {
1483 wpabuf_put_be16(msg
, ATTR_CRED
);
1484 wpabuf_put_be16(msg
, wpabuf_len(cred
));
1485 wpabuf_put_buf(msg
, cred
);
1489 if (wps
->wps
->registrar
->extra_cred
) {
1490 wpa_printf(MSG_DEBUG
, "WPS: * Credential (pre-configured)");
1491 wpabuf_put_buf(msg
, wps
->wps
->registrar
->extra_cred
);
1498 static int wps_build_ap_settings(struct wps_data
*wps
, struct wpabuf
*msg
)
1500 wpa_printf(MSG_DEBUG
, "WPS: * AP Settings");
1502 if (wps_build_credential(msg
, &wps
->cred
))
1509 static struct wpabuf
* wps_build_m2(struct wps_data
*wps
)
1513 if (os_get_random(wps
->nonce_r
, WPS_NONCE_LEN
) < 0)
1515 wpa_hexdump(MSG_DEBUG
, "WPS: Registrar Nonce",
1516 wps
->nonce_r
, WPS_NONCE_LEN
);
1517 wpa_hexdump(MSG_DEBUG
, "WPS: UUID-R", wps
->uuid_r
, WPS_UUID_LEN
);
1519 wpa_printf(MSG_DEBUG
, "WPS: Building Message M2");
1520 msg
= wpabuf_alloc(1000);
1524 if (wps_build_version(msg
) ||
1525 wps_build_msg_type(msg
, WPS_M2
) ||
1526 wps_build_enrollee_nonce(wps
, msg
) ||
1527 wps_build_registrar_nonce(wps
, msg
) ||
1528 wps_build_uuid_r(wps
, msg
) ||
1529 wps_build_public_key(wps
, msg
) ||
1530 wps_derive_keys(wps
) ||
1531 wps_build_auth_type_flags(wps
, msg
) ||
1532 wps_build_encr_type_flags(wps
, msg
) ||
1533 wps_build_conn_type_flags(wps
, msg
) ||
1534 wps_build_config_methods_r(wps
->wps
->registrar
, msg
) ||
1535 wps_build_device_attrs(&wps
->wps
->dev
, msg
) ||
1536 wps_build_rf_bands(&wps
->wps
->dev
, msg
) ||
1537 wps_build_assoc_state(wps
, msg
) ||
1538 wps_build_config_error(msg
, WPS_CFG_NO_ERROR
) ||
1539 wps_build_dev_password_id(msg
, wps
->dev_pw_id
) ||
1540 wps_build_os_version(&wps
->wps
->dev
, msg
) ||
1541 wps_build_wfa_ext(msg
, 0, NULL
, 0) ||
1542 wps_build_authenticator(wps
, msg
)) {
1548 wps
->state
= RECV_M3
;
1553 static struct wpabuf
* wps_build_m2d(struct wps_data
*wps
)
1556 u16 err
= wps
->config_error
;
1558 wpa_printf(MSG_DEBUG
, "WPS: Building Message M2D");
1559 msg
= wpabuf_alloc(1000);
1563 if (wps
->wps
->ap
&& wps
->wps
->ap_setup_locked
&&
1564 err
== WPS_CFG_NO_ERROR
)
1565 err
= WPS_CFG_SETUP_LOCKED
;
1567 if (wps_build_version(msg
) ||
1568 wps_build_msg_type(msg
, WPS_M2D
) ||
1569 wps_build_enrollee_nonce(wps
, msg
) ||
1570 wps_build_registrar_nonce(wps
, msg
) ||
1571 wps_build_uuid_r(wps
, msg
) ||
1572 wps_build_auth_type_flags(wps
, msg
) ||
1573 wps_build_encr_type_flags(wps
, msg
) ||
1574 wps_build_conn_type_flags(wps
, msg
) ||
1575 wps_build_config_methods_r(wps
->wps
->registrar
, msg
) ||
1576 wps_build_device_attrs(&wps
->wps
->dev
, msg
) ||
1577 wps_build_rf_bands(&wps
->wps
->dev
, msg
) ||
1578 wps_build_assoc_state(wps
, msg
) ||
1579 wps_build_config_error(msg
, err
) ||
1580 wps_build_os_version(&wps
->wps
->dev
, msg
) ||
1581 wps_build_wfa_ext(msg
, 0, NULL
, 0)) {
1586 wps
->state
= RECV_M2D_ACK
;
1591 static struct wpabuf
* wps_build_m4(struct wps_data
*wps
)
1593 struct wpabuf
*msg
, *plain
;
1595 wpa_printf(MSG_DEBUG
, "WPS: Building Message M4");
1597 wps_derive_psk(wps
, wps
->dev_password
, wps
->dev_password_len
);
1599 plain
= wpabuf_alloc(200);
1603 msg
= wpabuf_alloc(1000);
1609 if (wps_build_version(msg
) ||
1610 wps_build_msg_type(msg
, WPS_M4
) ||
1611 wps_build_enrollee_nonce(wps
, msg
) ||
1612 wps_build_r_hash(wps
, msg
) ||
1613 wps_build_r_snonce1(wps
, plain
) ||
1614 wps_build_key_wrap_auth(wps
, plain
) ||
1615 wps_build_encr_settings(wps
, msg
, plain
) ||
1616 wps_build_wfa_ext(msg
, 0, NULL
, 0) ||
1617 wps_build_authenticator(wps
, msg
)) {
1624 wps
->state
= RECV_M5
;
1629 static struct wpabuf
* wps_build_m6(struct wps_data
*wps
)
1631 struct wpabuf
*msg
, *plain
;
1633 wpa_printf(MSG_DEBUG
, "WPS: Building Message M6");
1635 plain
= wpabuf_alloc(200);
1639 msg
= wpabuf_alloc(1000);
1645 if (wps_build_version(msg
) ||
1646 wps_build_msg_type(msg
, WPS_M6
) ||
1647 wps_build_enrollee_nonce(wps
, msg
) ||
1648 wps_build_r_snonce2(wps
, plain
) ||
1649 wps_build_key_wrap_auth(wps
, plain
) ||
1650 wps_build_encr_settings(wps
, msg
, plain
) ||
1651 wps_build_wfa_ext(msg
, 0, NULL
, 0) ||
1652 wps_build_authenticator(wps
, msg
)) {
1659 wps
->wps_pin_revealed
= 1;
1660 wps
->state
= RECV_M7
;
1665 static struct wpabuf
* wps_build_m8(struct wps_data
*wps
)
1667 struct wpabuf
*msg
, *plain
;
1669 wpa_printf(MSG_DEBUG
, "WPS: Building Message M8");
1671 plain
= wpabuf_alloc(500);
1675 msg
= wpabuf_alloc(1000);
1681 if (wps_build_version(msg
) ||
1682 wps_build_msg_type(msg
, WPS_M8
) ||
1683 wps_build_enrollee_nonce(wps
, msg
) ||
1684 ((wps
->wps
->ap
|| wps
->er
) && wps_build_cred(wps
, plain
)) ||
1685 (!wps
->wps
->ap
&& !wps
->er
&& wps_build_ap_settings(wps
, plain
)) ||
1686 wps_build_key_wrap_auth(wps
, plain
) ||
1687 wps_build_encr_settings(wps
, msg
, plain
) ||
1688 wps_build_wfa_ext(msg
, 0, NULL
, 0) ||
1689 wps_build_authenticator(wps
, msg
)) {
1696 wps
->state
= RECV_DONE
;
1701 static struct wpabuf
* wps_build_wsc_ack(struct wps_data
*wps
)
1705 wpa_printf(MSG_DEBUG
, "WPS: Building Message WSC_ACK");
1707 msg
= wpabuf_alloc(1000);
1711 if (wps_build_version(msg
) ||
1712 wps_build_msg_type(msg
, WPS_WSC_ACK
) ||
1713 wps_build_enrollee_nonce(wps
, msg
) ||
1714 wps_build_registrar_nonce(wps
, msg
) ||
1715 wps_build_wfa_ext(msg
, 0, NULL
, 0)) {
1724 static struct wpabuf
* wps_build_wsc_nack(struct wps_data
*wps
)
1728 wpa_printf(MSG_DEBUG
, "WPS: Building Message WSC_NACK");
1730 msg
= wpabuf_alloc(1000);
1734 if (wps_build_version(msg
) ||
1735 wps_build_msg_type(msg
, WPS_WSC_NACK
) ||
1736 wps_build_enrollee_nonce(wps
, msg
) ||
1737 wps_build_registrar_nonce(wps
, msg
) ||
1738 wps_build_config_error(msg
, wps
->config_error
) ||
1739 wps_build_wfa_ext(msg
, 0, NULL
, 0)) {
1748 struct wpabuf
* wps_registrar_get_msg(struct wps_data
*wps
,
1749 enum wsc_op_code
*op_code
)
1753 #ifdef CONFIG_WPS_UPNP
1754 if (!wps
->int_reg
&& wps
->wps
->wps_upnp
) {
1755 struct upnp_pending_message
*p
, *prev
= NULL
;
1756 if (wps
->ext_reg
> 1)
1757 wps_registrar_free_pending_m2(wps
->wps
);
1758 p
= wps
->wps
->upnp_msgs
;
1759 /* TODO: check pending message MAC address */
1760 while (p
&& p
->next
) {
1765 wpa_printf(MSG_DEBUG
, "WPS: Use pending message from "
1770 wps
->wps
->upnp_msgs
= NULL
;
1777 *op_code
= WSC_NACK
;
1784 if (wps
->ext_reg
== 0)
1790 wpa_printf(MSG_DEBUG
, "WPS: Using external Registrar, but no "
1791 "pending message available");
1794 #endif /* CONFIG_WPS_UPNP */
1796 switch (wps
->state
) {
1798 if (wps_get_dev_password(wps
) < 0)
1799 msg
= wps_build_m2d(wps
);
1801 msg
= wps_build_m2(wps
);
1805 msg
= wps_build_m2d(wps
);
1809 msg
= wps_build_m4(wps
);
1813 msg
= wps_build_m6(wps
);
1817 msg
= wps_build_m8(wps
);
1821 msg
= wps_build_wsc_ack(wps
);
1825 msg
= wps_build_wsc_nack(wps
);
1826 *op_code
= WSC_NACK
;
1829 wpa_printf(MSG_DEBUG
, "WPS: Unsupported state %d for building "
1830 "a message", wps
->state
);
1835 if (*op_code
== WSC_MSG
&& msg
) {
1836 /* Save a copy of the last message for Authenticator derivation
1838 wpabuf_free(wps
->last_msg
);
1839 wps
->last_msg
= wpabuf_dup(msg
);
1846 static int wps_process_enrollee_nonce(struct wps_data
*wps
, const u8
*e_nonce
)
1848 if (e_nonce
== NULL
) {
1849 wpa_printf(MSG_DEBUG
, "WPS: No Enrollee Nonce received");
1853 os_memcpy(wps
->nonce_e
, e_nonce
, WPS_NONCE_LEN
);
1854 wpa_hexdump(MSG_DEBUG
, "WPS: Enrollee Nonce",
1855 wps
->nonce_e
, WPS_NONCE_LEN
);
1861 static int wps_process_registrar_nonce(struct wps_data
*wps
, const u8
*r_nonce
)
1863 if (r_nonce
== NULL
) {
1864 wpa_printf(MSG_DEBUG
, "WPS: No Registrar Nonce received");
1868 if (os_memcmp(wps
->nonce_r
, r_nonce
, WPS_NONCE_LEN
) != 0) {
1869 wpa_printf(MSG_DEBUG
, "WPS: Invalid Registrar Nonce received");
1877 static int wps_process_uuid_e(struct wps_data
*wps
, const u8
*uuid_e
)
1879 if (uuid_e
== NULL
) {
1880 wpa_printf(MSG_DEBUG
, "WPS: No UUID-E received");
1884 os_memcpy(wps
->uuid_e
, uuid_e
, WPS_UUID_LEN
);
1885 wpa_hexdump(MSG_DEBUG
, "WPS: UUID-E", wps
->uuid_e
, WPS_UUID_LEN
);
1891 static int wps_process_dev_password_id(struct wps_data
*wps
, const u8
*pw_id
)
1893 if (pw_id
== NULL
) {
1894 wpa_printf(MSG_DEBUG
, "WPS: No Device Password ID received");
1898 wps
->dev_pw_id
= WPA_GET_BE16(pw_id
);
1899 wpa_printf(MSG_DEBUG
, "WPS: Device Password ID %d", wps
->dev_pw_id
);
1905 static int wps_process_e_hash1(struct wps_data
*wps
, const u8
*e_hash1
)
1907 if (e_hash1
== NULL
) {
1908 wpa_printf(MSG_DEBUG
, "WPS: No E-Hash1 received");
1912 os_memcpy(wps
->peer_hash1
, e_hash1
, WPS_HASH_LEN
);
1913 wpa_hexdump(MSG_DEBUG
, "WPS: E-Hash1", wps
->peer_hash1
, WPS_HASH_LEN
);
1919 static int wps_process_e_hash2(struct wps_data
*wps
, const u8
*e_hash2
)
1921 if (e_hash2
== NULL
) {
1922 wpa_printf(MSG_DEBUG
, "WPS: No E-Hash2 received");
1926 os_memcpy(wps
->peer_hash2
, e_hash2
, WPS_HASH_LEN
);
1927 wpa_hexdump(MSG_DEBUG
, "WPS: E-Hash2", wps
->peer_hash2
, WPS_HASH_LEN
);
1933 static int wps_process_e_snonce1(struct wps_data
*wps
, const u8
*e_snonce1
)
1935 u8 hash
[SHA256_MAC_LEN
];
1939 if (e_snonce1
== NULL
) {
1940 wpa_printf(MSG_DEBUG
, "WPS: No E-SNonce1 received");
1944 wpa_hexdump_key(MSG_DEBUG
, "WPS: E-SNonce1", e_snonce1
,
1945 WPS_SECRET_NONCE_LEN
);
1947 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1948 addr
[0] = e_snonce1
;
1949 len
[0] = WPS_SECRET_NONCE_LEN
;
1950 addr
[1] = wps
->psk1
;
1951 len
[1] = WPS_PSK_LEN
;
1952 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
1953 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
1954 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
1955 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
1956 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
1958 if (os_memcmp(wps
->peer_hash1
, hash
, WPS_HASH_LEN
) != 0) {
1959 wpa_printf(MSG_DEBUG
, "WPS: E-Hash1 derived from E-S1 does "
1960 "not match with the pre-committed value");
1961 wps
->config_error
= WPS_CFG_DEV_PASSWORD_AUTH_FAILURE
;
1962 wps_pwd_auth_fail_event(wps
->wps
, 0, 1);
1966 wpa_printf(MSG_DEBUG
, "WPS: Enrollee proved knowledge of the first "
1967 "half of the device password");
1973 static int wps_process_e_snonce2(struct wps_data
*wps
, const u8
*e_snonce2
)
1975 u8 hash
[SHA256_MAC_LEN
];
1979 if (e_snonce2
== NULL
) {
1980 wpa_printf(MSG_DEBUG
, "WPS: No E-SNonce2 received");
1984 wpa_hexdump_key(MSG_DEBUG
, "WPS: E-SNonce2", e_snonce2
,
1985 WPS_SECRET_NONCE_LEN
);
1987 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1988 addr
[0] = e_snonce2
;
1989 len
[0] = WPS_SECRET_NONCE_LEN
;
1990 addr
[1] = wps
->psk2
;
1991 len
[1] = WPS_PSK_LEN
;
1992 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
1993 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
1994 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
1995 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
1996 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
1998 if (os_memcmp(wps
->peer_hash2
, hash
, WPS_HASH_LEN
) != 0) {
1999 wpa_printf(MSG_DEBUG
, "WPS: E-Hash2 derived from E-S2 does "
2000 "not match with the pre-committed value");
2001 wps_registrar_invalidate_pin(wps
->wps
->registrar
, wps
->uuid_e
);
2002 wps
->config_error
= WPS_CFG_DEV_PASSWORD_AUTH_FAILURE
;
2003 wps_pwd_auth_fail_event(wps
->wps
, 0, 2);
2007 wpa_printf(MSG_DEBUG
, "WPS: Enrollee proved knowledge of the second "
2008 "half of the device password");
2009 wps
->wps_pin_revealed
= 0;
2010 wps_registrar_unlock_pin(wps
->wps
->registrar
, wps
->uuid_e
);
2016 static int wps_process_mac_addr(struct wps_data
*wps
, const u8
*mac_addr
)
2018 if (mac_addr
== NULL
) {
2019 wpa_printf(MSG_DEBUG
, "WPS: No MAC Address received");
2023 wpa_printf(MSG_DEBUG
, "WPS: Enrollee MAC Address " MACSTR
,
2025 os_memcpy(wps
->mac_addr_e
, mac_addr
, ETH_ALEN
);
2026 os_memcpy(wps
->peer_dev
.mac_addr
, mac_addr
, ETH_ALEN
);
2032 static int wps_process_pubkey(struct wps_data
*wps
, const u8
*pk
,
2035 if (pk
== NULL
|| pk_len
== 0) {
2036 wpa_printf(MSG_DEBUG
, "WPS: No Public Key received");
2040 #ifdef CONFIG_WPS_OOB
2041 if (wps
->wps
->oob_conf
.pubkey_hash
!= NULL
) {
2043 u8 hash
[WPS_HASH_LEN
];
2046 sha256_vector(1, addr
, &pk_len
, hash
);
2048 wpabuf_head(wps
->wps
->oob_conf
.pubkey_hash
),
2049 WPS_OOB_PUBKEY_HASH_LEN
) != 0) {
2050 wpa_printf(MSG_ERROR
, "WPS: Public Key hash error");
2054 #endif /* CONFIG_WPS_OOB */
2056 wpabuf_free(wps
->dh_pubkey_e
);
2057 wps
->dh_pubkey_e
= wpabuf_alloc_copy(pk
, pk_len
);
2058 if (wps
->dh_pubkey_e
== NULL
)
2065 static int wps_process_auth_type_flags(struct wps_data
*wps
, const u8
*auth
)
2070 wpa_printf(MSG_DEBUG
, "WPS: No Authentication Type flags "
2075 auth_types
= WPA_GET_BE16(auth
);
2077 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Authentication Type flags 0x%x",
2079 wps
->auth_type
= wps
->wps
->auth_types
& auth_types
;
2080 if (wps
->auth_type
== 0) {
2081 wpa_printf(MSG_DEBUG
, "WPS: No match in supported "
2082 "authentication types (own 0x%x Enrollee 0x%x)",
2083 wps
->wps
->auth_types
, auth_types
);
2084 #ifdef WPS_WORKAROUNDS
2086 * Some deployed implementations seem to advertise incorrect
2087 * information in this attribute. For example, Linksys WRT350N
2088 * seems to have a byteorder bug that breaks this negotiation.
2089 * In order to interoperate with existing implementations,
2090 * assume that the Enrollee supports everything we do.
2092 wpa_printf(MSG_DEBUG
, "WPS: Workaround - assume Enrollee "
2093 "does not advertise supported authentication types "
2095 wps
->auth_type
= wps
->wps
->auth_types
;
2096 #else /* WPS_WORKAROUNDS */
2098 #endif /* WPS_WORKAROUNDS */
2105 static int wps_process_encr_type_flags(struct wps_data
*wps
, const u8
*encr
)
2110 wpa_printf(MSG_DEBUG
, "WPS: No Encryption Type flags "
2115 encr_types
= WPA_GET_BE16(encr
);
2117 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Encryption Type flags 0x%x",
2119 wps
->encr_type
= wps
->wps
->encr_types
& encr_types
;
2120 if (wps
->encr_type
== 0) {
2121 wpa_printf(MSG_DEBUG
, "WPS: No match in supported "
2122 "encryption types (own 0x%x Enrollee 0x%x)",
2123 wps
->wps
->encr_types
, encr_types
);
2124 #ifdef WPS_WORKAROUNDS
2126 * Some deployed implementations seem to advertise incorrect
2127 * information in this attribute. For example, Linksys WRT350N
2128 * seems to have a byteorder bug that breaks this negotiation.
2129 * In order to interoperate with existing implementations,
2130 * assume that the Enrollee supports everything we do.
2132 wpa_printf(MSG_DEBUG
, "WPS: Workaround - assume Enrollee "
2133 "does not advertise supported encryption types "
2135 wps
->encr_type
= wps
->wps
->encr_types
;
2136 #else /* WPS_WORKAROUNDS */
2138 #endif /* WPS_WORKAROUNDS */
2145 static int wps_process_conn_type_flags(struct wps_data
*wps
, const u8
*conn
)
2148 wpa_printf(MSG_DEBUG
, "WPS: No Connection Type flags "
2153 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Connection Type flags 0x%x",
2160 static int wps_process_config_methods(struct wps_data
*wps
, const u8
*methods
)
2164 if (methods
== NULL
) {
2165 wpa_printf(MSG_DEBUG
, "WPS: No Config Methods received");
2169 m
= WPA_GET_BE16(methods
);
2171 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Config Methods 0x%x"
2172 "%s%s%s%s%s%s%s%s%s", m
,
2173 m
& WPS_CONFIG_USBA
? " [USBA]" : "",
2174 m
& WPS_CONFIG_ETHERNET
? " [Ethernet]" : "",
2175 m
& WPS_CONFIG_LABEL
? " [Label]" : "",
2176 m
& WPS_CONFIG_DISPLAY
? " [Display]" : "",
2177 m
& WPS_CONFIG_EXT_NFC_TOKEN
? " [Ext NFC Token]" : "",
2178 m
& WPS_CONFIG_INT_NFC_TOKEN
? " [Int NFC Token]" : "",
2179 m
& WPS_CONFIG_NFC_INTERFACE
? " [NFC]" : "",
2180 m
& WPS_CONFIG_PUSHBUTTON
? " [PBC]" : "",
2181 m
& WPS_CONFIG_KEYPAD
? " [Keypad]" : "");
2183 if (!(m
& WPS_CONFIG_DISPLAY
) && !wps
->use_psk_key
) {
2185 * The Enrollee does not have a display so it is unlikely to be
2186 * able to show the passphrase to a user and as such, could
2187 * benefit from receiving PSK to reduce key derivation time.
2189 wpa_printf(MSG_DEBUG
, "WPS: Prefer PSK format key due to "
2190 "Enrollee not supporting display");
2191 wps
->use_psk_key
= 1;
2198 static int wps_process_wps_state(struct wps_data
*wps
, const u8
*state
)
2200 if (state
== NULL
) {
2201 wpa_printf(MSG_DEBUG
, "WPS: No Wi-Fi Protected Setup State "
2206 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Wi-Fi Protected Setup State %d",
2213 static int wps_process_assoc_state(struct wps_data
*wps
, const u8
*assoc
)
2217 if (assoc
== NULL
) {
2218 wpa_printf(MSG_DEBUG
, "WPS: No Association State received");
2222 a
= WPA_GET_BE16(assoc
);
2223 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Association State %d", a
);
2229 static int wps_process_config_error(struct wps_data
*wps
, const u8
*err
)
2234 wpa_printf(MSG_DEBUG
, "WPS: No Configuration Error received");
2238 e
= WPA_GET_BE16(err
);
2239 wpa_printf(MSG_DEBUG
, "WPS: Enrollee Configuration Error %d", e
);
2245 static enum wps_process_res
wps_process_m1(struct wps_data
*wps
,
2246 struct wps_parse_attr
*attr
)
2248 wpa_printf(MSG_DEBUG
, "WPS: Received M1");
2250 if (wps
->state
!= RECV_M1
) {
2251 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
2252 "receiving M1", wps
->state
);
2256 if (wps_process_uuid_e(wps
, attr
->uuid_e
) ||
2257 wps_process_mac_addr(wps
, attr
->mac_addr
) ||
2258 wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
2259 wps_process_pubkey(wps
, attr
->public_key
, attr
->public_key_len
) ||
2260 wps_process_auth_type_flags(wps
, attr
->auth_type_flags
) ||
2261 wps_process_encr_type_flags(wps
, attr
->encr_type_flags
) ||
2262 wps_process_conn_type_flags(wps
, attr
->conn_type_flags
) ||
2263 wps_process_config_methods(wps
, attr
->config_methods
) ||
2264 wps_process_wps_state(wps
, attr
->wps_state
) ||
2265 wps_process_device_attrs(&wps
->peer_dev
, attr
) ||
2266 wps_process_rf_bands(&wps
->peer_dev
, attr
->rf_bands
) ||
2267 wps_process_assoc_state(wps
, attr
->assoc_state
) ||
2268 wps_process_dev_password_id(wps
, attr
->dev_password_id
) ||
2269 wps_process_config_error(wps
, attr
->config_error
) ||
2270 wps_process_os_version(&wps
->peer_dev
, attr
->os_version
))
2273 if (wps
->dev_pw_id
< 0x10 &&
2274 wps
->dev_pw_id
!= DEV_PW_DEFAULT
&&
2275 wps
->dev_pw_id
!= DEV_PW_USER_SPECIFIED
&&
2276 wps
->dev_pw_id
!= DEV_PW_MACHINE_SPECIFIED
&&
2277 wps
->dev_pw_id
!= DEV_PW_REGISTRAR_SPECIFIED
&&
2278 (wps
->dev_pw_id
!= DEV_PW_PUSHBUTTON
||
2279 !wps
->wps
->registrar
->pbc
)) {
2280 wpa_printf(MSG_DEBUG
, "WPS: Unsupported Device Password ID %d",
2282 wps
->state
= SEND_M2D
;
2283 return WPS_CONTINUE
;
2286 #ifdef CONFIG_WPS_OOB
2287 if (wps
->dev_pw_id
>= 0x10 &&
2288 wps
->dev_pw_id
!= wps
->wps
->oob_dev_pw_id
) {
2289 wpa_printf(MSG_DEBUG
, "WPS: OOB Device Password ID "
2290 "%d mismatch", wps
->dev_pw_id
);
2291 wps
->state
= SEND_M2D
;
2292 return WPS_CONTINUE
;
2294 #endif /* CONFIG_WPS_OOB */
2296 if (wps
->dev_pw_id
== DEV_PW_PUSHBUTTON
) {
2297 if (wps
->wps
->registrar
->force_pbc_overlap
||
2298 wps_registrar_pbc_overlap(wps
->wps
->registrar
,
2299 wps
->mac_addr_e
, wps
->uuid_e
)) {
2300 wpa_printf(MSG_DEBUG
, "WPS: PBC overlap - deny PBC "
2302 wps
->state
= SEND_M2D
;
2303 wps
->config_error
= WPS_CFG_MULTIPLE_PBC_DETECTED
;
2304 wps_pbc_overlap_event(wps
->wps
);
2305 wps
->wps
->registrar
->force_pbc_overlap
= 1;
2306 return WPS_CONTINUE
;
2308 wps_registrar_add_pbc_session(wps
->wps
->registrar
,
2309 wps
->mac_addr_e
, wps
->uuid_e
);
2313 #ifdef WPS_WORKAROUNDS
2315 * It looks like Mac OS X 10.6.3 and 10.6.4 do not like Network Key in
2316 * passphrase format. To avoid interop issues, force PSK format to be
2319 if (!wps
->use_psk_key
&&
2320 wps
->peer_dev
.manufacturer
&&
2321 os_strncmp(wps
->peer_dev
.manufacturer
, "Apple ", 6) == 0 &&
2322 wps
->peer_dev
.model_name
&&
2323 os_strcmp(wps
->peer_dev
.model_name
, "AirPort") == 0) {
2324 wpa_printf(MSG_DEBUG
, "WPS: Workaround - Force Network Key in "
2326 wps
->use_psk_key
= 1;
2328 #endif /* WPS_WORKAROUNDS */
2330 wps
->state
= SEND_M2
;
2331 return WPS_CONTINUE
;
2335 static enum wps_process_res
wps_process_m3(struct wps_data
*wps
,
2336 const struct wpabuf
*msg
,
2337 struct wps_parse_attr
*attr
)
2339 wpa_printf(MSG_DEBUG
, "WPS: Received M3");
2341 if (wps
->state
!= RECV_M3
) {
2342 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
2343 "receiving M3", wps
->state
);
2344 wps
->state
= SEND_WSC_NACK
;
2345 return WPS_CONTINUE
;
2348 if (wps
->pbc
&& wps
->wps
->registrar
->force_pbc_overlap
) {
2349 wpa_printf(MSG_DEBUG
, "WPS: Reject negotiation due to PBC "
2351 wps
->state
= SEND_WSC_NACK
;
2352 wps
->config_error
= WPS_CFG_MULTIPLE_PBC_DETECTED
;
2353 return WPS_CONTINUE
;
2356 if (wps_process_registrar_nonce(wps
, attr
->registrar_nonce
) ||
2357 wps_process_authenticator(wps
, attr
->authenticator
, msg
) ||
2358 wps_process_e_hash1(wps
, attr
->e_hash1
) ||
2359 wps_process_e_hash2(wps
, attr
->e_hash2
)) {
2360 wps
->state
= SEND_WSC_NACK
;
2361 return WPS_CONTINUE
;
2364 wps
->state
= SEND_M4
;
2365 return WPS_CONTINUE
;
2369 static enum wps_process_res
wps_process_m5(struct wps_data
*wps
,
2370 const struct wpabuf
*msg
,
2371 struct wps_parse_attr
*attr
)
2373 struct wpabuf
*decrypted
;
2374 struct wps_parse_attr eattr
;
2376 wpa_printf(MSG_DEBUG
, "WPS: Received M5");
2378 if (wps
->state
!= RECV_M5
) {
2379 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
2380 "receiving M5", wps
->state
);
2381 wps
->state
= SEND_WSC_NACK
;
2382 return WPS_CONTINUE
;
2385 if (wps
->pbc
&& wps
->wps
->registrar
->force_pbc_overlap
) {
2386 wpa_printf(MSG_DEBUG
, "WPS: Reject negotiation due to PBC "
2388 wps
->state
= SEND_WSC_NACK
;
2389 wps
->config_error
= WPS_CFG_MULTIPLE_PBC_DETECTED
;
2390 return WPS_CONTINUE
;
2393 if (wps_process_registrar_nonce(wps
, attr
->registrar_nonce
) ||
2394 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
2395 wps
->state
= SEND_WSC_NACK
;
2396 return WPS_CONTINUE
;
2399 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
2400 attr
->encr_settings_len
);
2401 if (decrypted
== NULL
) {
2402 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
2403 "Settings attribute");
2404 wps
->state
= SEND_WSC_NACK
;
2405 return WPS_CONTINUE
;
2408 if (wps_validate_m5_encr(decrypted
) < 0) {
2409 wpabuf_free(decrypted
);
2410 wps
->state
= SEND_WSC_NACK
;
2411 return WPS_CONTINUE
;
2414 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
2416 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
2417 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
2418 wps_process_e_snonce1(wps
, eattr
.e_snonce1
)) {
2419 wpabuf_free(decrypted
);
2420 wps
->state
= SEND_WSC_NACK
;
2421 return WPS_CONTINUE
;
2423 wpabuf_free(decrypted
);
2425 wps
->state
= SEND_M6
;
2426 return WPS_CONTINUE
;
2430 static void wps_sta_cred_cb(struct wps_data
*wps
)
2433 * Update credential to only include a single authentication and
2434 * encryption type in case the AP configuration includes more than one
2437 if (wps
->cred
.auth_type
& WPS_AUTH_WPA2PSK
)
2438 wps
->cred
.auth_type
= WPS_AUTH_WPA2PSK
;
2439 else if (wps
->cred
.auth_type
& WPS_AUTH_WPAPSK
)
2440 wps
->cred
.auth_type
= WPS_AUTH_WPAPSK
;
2441 if (wps
->cred
.encr_type
& WPS_ENCR_AES
)
2442 wps
->cred
.encr_type
= WPS_ENCR_AES
;
2443 else if (wps
->cred
.encr_type
& WPS_ENCR_TKIP
)
2444 wps
->cred
.encr_type
= WPS_ENCR_TKIP
;
2445 wpa_printf(MSG_DEBUG
, "WPS: Update local configuration based on the "
2446 "AP configuration");
2447 if (wps
->wps
->cred_cb
)
2448 wps
->wps
->cred_cb(wps
->wps
->cb_ctx
, &wps
->cred
);
2452 static void wps_cred_update(struct wps_credential
*dst
,
2453 struct wps_credential
*src
)
2455 os_memcpy(dst
->ssid
, src
->ssid
, sizeof(dst
->ssid
));
2456 dst
->ssid_len
= src
->ssid_len
;
2457 dst
->auth_type
= src
->auth_type
;
2458 dst
->encr_type
= src
->encr_type
;
2459 dst
->key_idx
= src
->key_idx
;
2460 os_memcpy(dst
->key
, src
->key
, sizeof(dst
->key
));
2461 dst
->key_len
= src
->key_len
;
2465 static int wps_process_ap_settings_r(struct wps_data
*wps
,
2466 struct wps_parse_attr
*attr
)
2468 if (wps
->wps
->ap
|| wps
->er
)
2471 /* AP Settings Attributes in M7 when Enrollee is an AP */
2472 if (wps_process_ap_settings(attr
, &wps
->cred
) < 0)
2475 wpa_printf(MSG_INFO
, "WPS: Received old AP configuration from AP");
2477 if (wps
->new_ap_settings
) {
2478 wpa_printf(MSG_INFO
, "WPS: Update AP configuration based on "
2480 wps_cred_update(&wps
->cred
, wps
->new_ap_settings
);
2484 * Use the AP PIN only to receive the current AP settings, not
2485 * to reconfigure the AP.
2487 if (wps
->ap_settings_cb
) {
2488 wps
->ap_settings_cb(wps
->ap_settings_cb_ctx
,
2492 wps_sta_cred_cb(wps
);
2498 static enum wps_process_res
wps_process_m7(struct wps_data
*wps
,
2499 const struct wpabuf
*msg
,
2500 struct wps_parse_attr
*attr
)
2502 struct wpabuf
*decrypted
;
2503 struct wps_parse_attr eattr
;
2505 wpa_printf(MSG_DEBUG
, "WPS: Received M7");
2507 if (wps
->state
!= RECV_M7
) {
2508 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
2509 "receiving M7", wps
->state
);
2510 wps
->state
= SEND_WSC_NACK
;
2511 return WPS_CONTINUE
;
2514 if (wps
->pbc
&& wps
->wps
->registrar
->force_pbc_overlap
) {
2515 wpa_printf(MSG_DEBUG
, "WPS: Reject negotiation due to PBC "
2517 wps
->state
= SEND_WSC_NACK
;
2518 wps
->config_error
= WPS_CFG_MULTIPLE_PBC_DETECTED
;
2519 return WPS_CONTINUE
;
2522 if (wps_process_registrar_nonce(wps
, attr
->registrar_nonce
) ||
2523 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
2524 wps
->state
= SEND_WSC_NACK
;
2525 return WPS_CONTINUE
;
2528 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
2529 attr
->encr_settings_len
);
2530 if (decrypted
== NULL
) {
2531 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypt Encrypted "
2532 "Settings attribute");
2533 wps
->state
= SEND_WSC_NACK
;
2534 return WPS_CONTINUE
;
2537 if (wps_validate_m7_encr(decrypted
, wps
->wps
->ap
|| wps
->er
) < 0) {
2538 wpabuf_free(decrypted
);
2539 wps
->state
= SEND_WSC_NACK
;
2540 return WPS_CONTINUE
;
2543 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
2545 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
2546 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
2547 wps_process_e_snonce2(wps
, eattr
.e_snonce2
) ||
2548 wps_process_ap_settings_r(wps
, &eattr
)) {
2549 wpabuf_free(decrypted
);
2550 wps
->state
= SEND_WSC_NACK
;
2551 return WPS_CONTINUE
;
2554 wpabuf_free(decrypted
);
2556 wps
->state
= SEND_M8
;
2557 return WPS_CONTINUE
;
2561 static enum wps_process_res
wps_process_wsc_msg(struct wps_data
*wps
,
2562 const struct wpabuf
*msg
)
2564 struct wps_parse_attr attr
;
2565 enum wps_process_res ret
= WPS_CONTINUE
;
2567 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_MSG");
2569 if (wps_parse_msg(msg
, &attr
) < 0)
2572 if (attr
.msg_type
== NULL
) {
2573 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
2577 if (*attr
.msg_type
!= WPS_M1
&&
2578 (attr
.registrar_nonce
== NULL
||
2579 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
,
2580 WPS_NONCE_LEN
!= 0))) {
2581 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
2585 switch (*attr
.msg_type
) {
2587 if (wps_validate_m1(msg
) < 0)
2589 #ifdef CONFIG_WPS_UPNP
2590 if (wps
->wps
->wps_upnp
&& attr
.mac_addr
) {
2591 /* Remove old pending messages when starting new run */
2592 wps_free_pending_msgs(wps
->wps
->upnp_msgs
);
2593 wps
->wps
->upnp_msgs
= NULL
;
2595 upnp_wps_device_send_wlan_event(
2596 wps
->wps
->wps_upnp
, attr
.mac_addr
,
2597 UPNP_WPS_WLANEVENT_TYPE_EAP
, msg
);
2599 #endif /* CONFIG_WPS_UPNP */
2600 ret
= wps_process_m1(wps
, &attr
);
2603 if (wps_validate_m3(msg
) < 0)
2605 ret
= wps_process_m3(wps
, msg
, &attr
);
2606 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
2607 wps_fail_event(wps
->wps
, WPS_M3
);
2610 if (wps_validate_m5(msg
) < 0)
2612 ret
= wps_process_m5(wps
, msg
, &attr
);
2613 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
2614 wps_fail_event(wps
->wps
, WPS_M5
);
2617 if (wps_validate_m7(msg
) < 0)
2619 ret
= wps_process_m7(wps
, msg
, &attr
);
2620 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
2621 wps_fail_event(wps
->wps
, WPS_M7
);
2624 wpa_printf(MSG_DEBUG
, "WPS: Unsupported Message Type %d",
2629 if (ret
== WPS_CONTINUE
) {
2630 /* Save a copy of the last message for Authenticator derivation
2632 wpabuf_free(wps
->last_msg
);
2633 wps
->last_msg
= wpabuf_dup(msg
);
2640 static enum wps_process_res
wps_process_wsc_ack(struct wps_data
*wps
,
2641 const struct wpabuf
*msg
)
2643 struct wps_parse_attr attr
;
2645 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_ACK");
2647 if (wps_parse_msg(msg
, &attr
) < 0)
2650 if (attr
.msg_type
== NULL
) {
2651 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
2655 if (*attr
.msg_type
!= WPS_WSC_ACK
) {
2656 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
2661 #ifdef CONFIG_WPS_UPNP
2662 if (wps
->wps
->wps_upnp
&& wps
->ext_reg
&& wps
->state
== RECV_M2D_ACK
&&
2663 upnp_wps_subscribers(wps
->wps
->wps_upnp
)) {
2664 if (wps
->wps
->upnp_msgs
)
2665 return WPS_CONTINUE
;
2666 wpa_printf(MSG_DEBUG
, "WPS: Wait for response from an "
2667 "external Registrar");
2670 #endif /* CONFIG_WPS_UPNP */
2672 if (attr
.registrar_nonce
== NULL
||
2673 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
2675 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
2679 if (attr
.enrollee_nonce
== NULL
||
2680 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
2681 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
2685 if (wps
->state
== RECV_M2D_ACK
) {
2686 #ifdef CONFIG_WPS_UPNP
2687 if (wps
->wps
->wps_upnp
&&
2688 upnp_wps_subscribers(wps
->wps
->wps_upnp
)) {
2689 if (wps
->wps
->upnp_msgs
)
2690 return WPS_CONTINUE
;
2691 if (wps
->ext_reg
== 0)
2693 wpa_printf(MSG_DEBUG
, "WPS: Wait for response from an "
2694 "external Registrar");
2697 #endif /* CONFIG_WPS_UPNP */
2699 wpa_printf(MSG_DEBUG
, "WPS: No more registrars available - "
2700 "terminate negotiation");
2707 static enum wps_process_res
wps_process_wsc_nack(struct wps_data
*wps
,
2708 const struct wpabuf
*msg
)
2710 struct wps_parse_attr attr
;
2713 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_NACK");
2715 old_state
= wps
->state
;
2716 wps
->state
= SEND_WSC_NACK
;
2718 if (wps_parse_msg(msg
, &attr
) < 0)
2721 if (attr
.msg_type
== NULL
) {
2722 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
2726 if (*attr
.msg_type
!= WPS_WSC_NACK
) {
2727 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
2732 #ifdef CONFIG_WPS_UPNP
2733 if (wps
->wps
->wps_upnp
&& wps
->ext_reg
) {
2734 wpa_printf(MSG_DEBUG
, "WPS: Negotiation using external "
2735 "Registrar terminated by the Enrollee");
2738 #endif /* CONFIG_WPS_UPNP */
2740 if (attr
.registrar_nonce
== NULL
||
2741 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
2743 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
2747 if (attr
.enrollee_nonce
== NULL
||
2748 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
2749 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
2753 if (attr
.config_error
== NULL
) {
2754 wpa_printf(MSG_DEBUG
, "WPS: No Configuration Error attribute "
2759 wpa_printf(MSG_DEBUG
, "WPS: Enrollee terminated negotiation with "
2760 "Configuration Error %d", WPA_GET_BE16(attr
.config_error
));
2762 switch (old_state
) {
2764 wps_fail_event(wps
->wps
, WPS_M2
);
2767 wps_fail_event(wps
->wps
, WPS_M4
);
2770 wps_fail_event(wps
->wps
, WPS_M6
);
2773 wps_fail_event(wps
->wps
, WPS_M8
);
2783 static enum wps_process_res
wps_process_wsc_done(struct wps_data
*wps
,
2784 const struct wpabuf
*msg
)
2786 struct wps_parse_attr attr
;
2788 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_Done");
2790 if (wps
->state
!= RECV_DONE
&&
2791 (!wps
->wps
->wps_upnp
|| !wps
->ext_reg
)) {
2792 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
2793 "receiving WSC_Done", wps
->state
);
2797 if (wps_parse_msg(msg
, &attr
) < 0)
2800 if (attr
.msg_type
== NULL
) {
2801 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
2805 if (*attr
.msg_type
!= WPS_WSC_DONE
) {
2806 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
2811 #ifdef CONFIG_WPS_UPNP
2812 if (wps
->wps
->wps_upnp
&& wps
->ext_reg
) {
2813 wpa_printf(MSG_DEBUG
, "WPS: Negotiation using external "
2814 "Registrar completed successfully");
2815 wps_device_store(wps
->wps
->registrar
, &wps
->peer_dev
,
2819 #endif /* CONFIG_WPS_UPNP */
2821 if (attr
.registrar_nonce
== NULL
||
2822 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
2824 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
2828 if (attr
.enrollee_nonce
== NULL
||
2829 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
2830 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
2834 wpa_printf(MSG_DEBUG
, "WPS: Negotiation completed successfully");
2835 wps_device_store(wps
->wps
->registrar
, &wps
->peer_dev
,
2838 if (wps
->wps
->wps_state
== WPS_STATE_NOT_CONFIGURED
&& wps
->new_psk
&&
2839 wps
->wps
->ap
&& !wps
->wps
->registrar
->disable_auto_conf
) {
2840 struct wps_credential cred
;
2842 wpa_printf(MSG_DEBUG
, "WPS: Moving to Configured state based "
2843 "on first Enrollee connection");
2845 os_memset(&cred
, 0, sizeof(cred
));
2846 os_memcpy(cred
.ssid
, wps
->wps
->ssid
, wps
->wps
->ssid_len
);
2847 cred
.ssid_len
= wps
->wps
->ssid_len
;
2848 cred
.auth_type
= WPS_AUTH_WPAPSK
| WPS_AUTH_WPA2PSK
;
2849 cred
.encr_type
= WPS_ENCR_TKIP
| WPS_ENCR_AES
;
2850 os_memcpy(cred
.key
, wps
->new_psk
, wps
->new_psk_len
);
2851 cred
.key_len
= wps
->new_psk_len
;
2853 wps
->wps
->wps_state
= WPS_STATE_CONFIGURED
;
2854 wpa_hexdump_ascii_key(MSG_DEBUG
,
2855 "WPS: Generated random passphrase",
2856 wps
->new_psk
, wps
->new_psk_len
);
2857 if (wps
->wps
->cred_cb
)
2858 wps
->wps
->cred_cb(wps
->wps
->cb_ctx
, &cred
);
2860 os_free(wps
->new_psk
);
2861 wps
->new_psk
= NULL
;
2864 if (!wps
->wps
->ap
&& !wps
->er
)
2865 wps_sta_cred_cb(wps
);
2868 if (wps_cb_new_psk(wps
->wps
->registrar
, wps
->mac_addr_e
,
2869 wps
->new_psk
, wps
->new_psk_len
)) {
2870 wpa_printf(MSG_DEBUG
, "WPS: Failed to configure the "
2873 os_free(wps
->new_psk
);
2874 wps
->new_psk
= NULL
;
2877 wps_cb_reg_success(wps
->wps
->registrar
, wps
->mac_addr_e
, wps
->uuid_e
);
2880 wps_registrar_remove_pbc_session(wps
->wps
->registrar
,
2881 wps
->mac_addr_e
, wps
->uuid_e
);
2882 wps_registrar_pbc_completed(wps
->wps
->registrar
);
2884 wps_registrar_pin_completed(wps
->wps
->registrar
);
2886 /* TODO: maintain AuthorizedMACs somewhere separately for each ER and
2887 * merge them into APs own list.. */
2889 wps_success_event(wps
->wps
);
2895 enum wps_process_res
wps_registrar_process_msg(struct wps_data
*wps
,
2896 enum wsc_op_code op_code
,
2897 const struct wpabuf
*msg
)
2899 enum wps_process_res ret
;
2901 wpa_printf(MSG_DEBUG
, "WPS: Processing received message (len=%lu "
2903 (unsigned long) wpabuf_len(msg
), op_code
);
2905 #ifdef CONFIG_WPS_UPNP
2906 if (wps
->wps
->wps_upnp
&& op_code
== WSC_MSG
&& wps
->ext_reg
== 1) {
2907 struct wps_parse_attr attr
;
2908 if (wps_parse_msg(msg
, &attr
) == 0 && attr
.msg_type
&&
2909 *attr
.msg_type
== WPS_M3
)
2910 wps
->ext_reg
= 2; /* past M2/M2D phase */
2912 if (wps
->ext_reg
> 1)
2913 wps_registrar_free_pending_m2(wps
->wps
);
2914 if (wps
->wps
->wps_upnp
&& wps
->ext_reg
&&
2915 wps
->wps
->upnp_msgs
== NULL
&&
2916 (op_code
== WSC_MSG
|| op_code
== WSC_Done
|| op_code
== WSC_NACK
))
2918 struct wps_parse_attr attr
;
2920 if (wps_parse_msg(msg
, &attr
) < 0 || attr
.msg_type
== NULL
)
2923 type
= *attr
.msg_type
;
2924 wpa_printf(MSG_DEBUG
, "WPS: Sending received message (type %d)"
2925 " to external Registrar for processing", type
);
2926 upnp_wps_device_send_wlan_event(wps
->wps
->wps_upnp
,
2928 UPNP_WPS_WLANEVENT_TYPE_EAP
,
2930 if (op_code
== WSC_MSG
)
2932 } else if (wps
->wps
->wps_upnp
&& wps
->ext_reg
&& op_code
== WSC_MSG
) {
2933 wpa_printf(MSG_DEBUG
, "WPS: Skip internal processing - using "
2934 "external Registrar");
2935 return WPS_CONTINUE
;
2937 #endif /* CONFIG_WPS_UPNP */
2941 return wps_process_wsc_msg(wps
, msg
);
2943 if (wps_validate_wsc_ack(msg
) < 0)
2945 return wps_process_wsc_ack(wps
, msg
);
2947 if (wps_validate_wsc_nack(msg
) < 0)
2949 return wps_process_wsc_nack(wps
, msg
);
2951 if (wps_validate_wsc_done(msg
) < 0)
2953 ret
= wps_process_wsc_done(wps
, msg
);
2954 if (ret
== WPS_FAILURE
) {
2955 wps
->state
= SEND_WSC_NACK
;
2956 wps_fail_event(wps
->wps
, WPS_WSC_DONE
);
2960 wpa_printf(MSG_DEBUG
, "WPS: Unsupported op_code %d", op_code
);
2966 int wps_registrar_update_ie(struct wps_registrar
*reg
)
2968 return wps_set_ie(reg
);
2972 static void wps_registrar_set_selected_timeout(void *eloop_ctx
,
2975 struct wps_registrar
*reg
= eloop_ctx
;
2977 wpa_printf(MSG_DEBUG
, "WPS: Selected Registrar timeout - "
2978 "unselect internal Registrar");
2979 reg
->selected_registrar
= 0;
2981 wps_registrar_selected_registrar_changed(reg
);
2985 #ifdef CONFIG_WPS_UPNP
2986 static void wps_registrar_sel_reg_add(struct wps_registrar
*reg
,
2987 struct subscription
*s
)
2990 wpa_printf(MSG_DEBUG
, "WPS: External Registrar selected (dev_pw_id=%d "
2991 "config_methods=0x%x)",
2992 s
->dev_password_id
, s
->config_methods
);
2993 reg
->sel_reg_union
= 1;
2994 if (reg
->sel_reg_dev_password_id_override
!= DEV_PW_PUSHBUTTON
)
2995 reg
->sel_reg_dev_password_id_override
= s
->dev_password_id
;
2996 if (reg
->sel_reg_config_methods_override
== -1)
2997 reg
->sel_reg_config_methods_override
= 0;
2998 reg
->sel_reg_config_methods_override
|= s
->config_methods
;
2999 for (i
= 0; i
< WPS_MAX_AUTHORIZED_MACS
; i
++)
3000 if (is_zero_ether_addr(reg
->authorized_macs_union
[i
]))
3002 for (j
= 0; i
< WPS_MAX_AUTHORIZED_MACS
&& j
< WPS_MAX_AUTHORIZED_MACS
;
3004 if (is_zero_ether_addr(s
->authorized_macs
[j
]))
3006 wpa_printf(MSG_DEBUG
, "WPS: Add authorized MAC into union: "
3007 MACSTR
, MAC2STR(s
->authorized_macs
[j
]));
3008 os_memcpy(reg
->authorized_macs_union
[i
],
3009 s
->authorized_macs
[j
], ETH_ALEN
);
3012 wpa_hexdump(MSG_DEBUG
, "WPS: Authorized MACs union",
3013 (u8
*) reg
->authorized_macs_union
,
3014 sizeof(reg
->authorized_macs_union
));
3016 #endif /* CONFIG_WPS_UPNP */
3019 static void wps_registrar_sel_reg_union(struct wps_registrar
*reg
)
3021 #ifdef CONFIG_WPS_UPNP
3022 struct subscription
*s
;
3024 if (reg
->wps
->wps_upnp
== NULL
)
3027 dl_list_for_each(s
, ®
->wps
->wps_upnp
->subscriptions
,
3028 struct subscription
, list
) {
3029 struct subscr_addr
*sa
;
3030 sa
= dl_list_first(&s
->addr_list
, struct subscr_addr
, list
);
3032 wpa_printf(MSG_DEBUG
, "WPS: External Registrar %s:%d",
3033 inet_ntoa(sa
->saddr
.sin_addr
),
3034 ntohs(sa
->saddr
.sin_port
));
3036 if (s
->selected_registrar
)
3037 wps_registrar_sel_reg_add(reg
, s
);
3039 wpa_printf(MSG_DEBUG
, "WPS: External Registrar not "
3042 #endif /* CONFIG_WPS_UPNP */
3047 * wps_registrar_selected_registrar_changed - SetSelectedRegistrar change
3048 * @reg: Registrar data from wps_registrar_init()
3050 * This function is called when selected registrar state changes, e.g., when an
3051 * AP receives a SetSelectedRegistrar UPnP message.
3053 void wps_registrar_selected_registrar_changed(struct wps_registrar
*reg
)
3055 wpa_printf(MSG_DEBUG
, "WPS: Selected registrar information changed");
3057 reg
->sel_reg_union
= reg
->selected_registrar
;
3058 reg
->sel_reg_dev_password_id_override
= -1;
3059 reg
->sel_reg_config_methods_override
= -1;
3060 os_memcpy(reg
->authorized_macs_union
, reg
->authorized_macs
,
3061 WPS_MAX_AUTHORIZED_MACS
* ETH_ALEN
);
3062 wpa_hexdump(MSG_DEBUG
, "WPS: Authorized MACs union (start with own)",
3063 (u8
*) reg
->authorized_macs_union
,
3064 sizeof(reg
->authorized_macs_union
));
3065 if (reg
->selected_registrar
) {
3068 methods
= reg
->wps
->config_methods
& ~WPS_CONFIG_PUSHBUTTON
;
3070 methods
&= ~(WPS_CONFIG_VIRT_PUSHBUTTON
|
3071 WPS_CONFIG_PHY_PUSHBUTTON
);
3072 #endif /* CONFIG_WPS2 */
3074 reg
->sel_reg_dev_password_id_override
=
3076 wps_set_pushbutton(&methods
, reg
->wps
->config_methods
);
3078 wpa_printf(MSG_DEBUG
, "WPS: Internal Registrar selected "
3079 "(pbc=%d)", reg
->pbc
);
3080 reg
->sel_reg_config_methods_override
= methods
;
3082 wpa_printf(MSG_DEBUG
, "WPS: Internal Registrar not selected");
3084 wps_registrar_sel_reg_union(reg
);
3087 wps_cb_set_sel_reg(reg
);
3091 int wps_registrar_get_info(struct wps_registrar
*reg
, const u8
*addr
,
3092 char *buf
, size_t buflen
)
3094 struct wps_registrar_device
*d
;
3097 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
3099 d
= wps_device_get(reg
, addr
);
3102 if (uuid_bin2str(d
->uuid
, uuid
, sizeof(uuid
)))
3105 ret
= os_snprintf(buf
+ len
, buflen
- len
,
3107 "wpsPrimaryDeviceType=%s\n"
3108 "wpsDeviceName=%s\n"
3109 "wpsManufacturer=%s\n"
3111 "wpsModelNumber=%s\n"
3112 "wpsSerialNumber=%s\n",
3114 wps_dev_type_bin2str(d
->dev
.pri_dev_type
, devtype
,
3116 d
->dev
.device_name
? d
->dev
.device_name
: "",
3117 d
->dev
.manufacturer
? d
->dev
.manufacturer
: "",
3118 d
->dev
.model_name
? d
->dev
.model_name
: "",
3119 d
->dev
.model_number
? d
->dev
.model_number
: "",
3120 d
->dev
.serial_number
? d
->dev
.serial_number
: "");
3121 if (ret
< 0 || (size_t) ret
>= buflen
- len
)