2 * Wi-Fi Protected Setup
3 * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/dh_group5.h"
13 #include "common/ieee802_11_defs.h"
15 #include "wps_dev_attr.h"
18 #ifdef CONFIG_WPS_TESTING
19 int wps_version_number
= 0x20;
20 int wps_testing_dummy_cred
= 0;
21 #endif /* CONFIG_WPS_TESTING */
25 * wps_init - Initialize WPS Registration protocol data
26 * @cfg: WPS configuration
27 * Returns: Pointer to allocated data or %NULL on failure
29 * This function is used to initialize WPS data for a registration protocol
30 * instance (i.e., each run of registration protocol as a Registrar of
31 * Enrollee. The caller is responsible for freeing this data after the
32 * registration run has been completed by calling wps_deinit().
34 struct wps_data
* wps_init(const struct wps_config
*cfg
)
36 struct wps_data
*data
= os_zalloc(sizeof(*data
));
40 data
->registrar
= cfg
->registrar
;
42 os_memcpy(data
->uuid_r
, cfg
->wps
->uuid
, WPS_UUID_LEN
);
44 os_memcpy(data
->mac_addr_e
, cfg
->wps
->dev
.mac_addr
, ETH_ALEN
);
45 os_memcpy(data
->uuid_e
, cfg
->wps
->uuid
, WPS_UUID_LEN
);
48 data
->dev_pw_id
= data
->wps
->oob_dev_pw_id
== 0 ?
49 cfg
->dev_pw_id
: data
->wps
->oob_dev_pw_id
;
50 data
->dev_password
= os_malloc(cfg
->pin_len
);
51 if (data
->dev_password
== NULL
) {
55 os_memcpy(data
->dev_password
, cfg
->pin
, cfg
->pin_len
);
56 data
->dev_password_len
= cfg
->pin_len
;
60 if (cfg
->wps
->ap
&& !cfg
->registrar
&& cfg
->wps
->ap_nfc_dev_pw_id
) {
61 data
->dev_pw_id
= cfg
->wps
->ap_nfc_dev_pw_id
;
62 os_free(data
->dev_password
);
64 os_malloc(wpabuf_len(cfg
->wps
->ap_nfc_dev_pw
));
65 if (data
->dev_password
== NULL
) {
69 os_memcpy(data
->dev_password
,
70 wpabuf_head(cfg
->wps
->ap_nfc_dev_pw
),
71 wpabuf_len(cfg
->wps
->ap_nfc_dev_pw
));
72 data
->dev_password_len
= wpabuf_len(cfg
->wps
->ap_nfc_dev_pw
);
74 #endif /* CONFIG_WPS_NFC */
78 /* Use special PIN '00000000' for PBC */
79 data
->dev_pw_id
= DEV_PW_PUSHBUTTON
;
80 os_free(data
->dev_password
);
81 data
->dev_password
= (u8
*) os_strdup("00000000");
82 if (data
->dev_password
== NULL
) {
86 data
->dev_password_len
= 8;
89 data
->state
= data
->registrar
? RECV_M1
: SEND_M1
;
91 if (cfg
->assoc_wps_ie
) {
92 struct wps_parse_attr attr
;
93 wpa_hexdump_buf(MSG_DEBUG
, "WPS: WPS IE from (Re)AssocReq",
95 if (wps_parse_msg(cfg
->assoc_wps_ie
, &attr
) < 0) {
96 wpa_printf(MSG_DEBUG
, "WPS: Failed to parse WPS IE "
98 } else if (attr
.request_type
== NULL
) {
99 wpa_printf(MSG_DEBUG
, "WPS: No Request Type attribute "
100 "in (Re)AssocReq WPS IE");
102 wpa_printf(MSG_DEBUG
, "WPS: Request Type (from WPS IE "
103 "in (Re)AssocReq WPS IE): %d",
105 data
->request_type
= *attr
.request_type
;
109 if (cfg
->new_ap_settings
) {
110 data
->new_ap_settings
=
111 os_malloc(sizeof(*data
->new_ap_settings
));
112 if (data
->new_ap_settings
== NULL
) {
113 os_free(data
->dev_password
);
117 os_memcpy(data
->new_ap_settings
, cfg
->new_ap_settings
,
118 sizeof(*data
->new_ap_settings
));
122 os_memcpy(data
->peer_dev
.mac_addr
, cfg
->peer_addr
, ETH_ALEN
);
123 if (cfg
->p2p_dev_addr
)
124 os_memcpy(data
->p2p_dev_addr
, cfg
->p2p_dev_addr
, ETH_ALEN
);
126 data
->use_psk_key
= cfg
->use_psk_key
;
127 data
->pbc_in_m1
= cfg
->pbc_in_m1
;
134 * wps_deinit - Deinitialize WPS Registration protocol data
135 * @data: WPS Registration protocol data from wps_init()
137 void wps_deinit(struct wps_data
*data
)
139 #ifdef CONFIG_WPS_NFC
140 if (data
->registrar
&& data
->nfc_pw_token
)
141 wps_registrar_remove_nfc_pw_token(data
->wps
->registrar
,
143 #endif /* CONFIG_WPS_NFC */
145 if (data
->wps_pin_revealed
) {
146 wpa_printf(MSG_DEBUG
, "WPS: Full PIN information revealed and "
147 "negotiation failed");
149 wps_registrar_invalidate_pin(data
->wps
->registrar
,
151 } else if (data
->registrar
)
152 wps_registrar_unlock_pin(data
->wps
->registrar
, data
->uuid_e
);
154 wpabuf_free(data
->dh_privkey
);
155 wpabuf_free(data
->dh_pubkey_e
);
156 wpabuf_free(data
->dh_pubkey_r
);
157 wpabuf_free(data
->last_msg
);
158 os_free(data
->dev_password
);
159 os_free(data
->new_psk
);
160 wps_device_data_free(&data
->peer_dev
);
161 os_free(data
->new_ap_settings
);
162 dh5_free(data
->dh_ctx
);
163 os_free(data
->nfc_pw_token
);
169 * wps_process_msg - Process a WPS message
170 * @wps: WPS Registration protocol data from wps_init()
171 * @op_code: Message OP Code
173 * Returns: Processing result
175 * This function is used to process WPS messages with OP Codes WSC_ACK,
176 * WSC_NACK, WSC_MSG, and WSC_Done. The caller (e.g., EAP server/peer) is
177 * responsible for reassembling the messages before calling this function.
178 * Response to this message is built by calling wps_get_msg().
180 enum wps_process_res
wps_process_msg(struct wps_data
*wps
,
181 enum wsc_op_code op_code
,
182 const struct wpabuf
*msg
)
185 return wps_registrar_process_msg(wps
, op_code
, msg
);
187 return wps_enrollee_process_msg(wps
, op_code
, msg
);
192 * wps_get_msg - Build a WPS message
193 * @wps: WPS Registration protocol data from wps_init()
194 * @op_code: Buffer for returning message OP Code
195 * Returns: The generated WPS message or %NULL on failure
197 * This function is used to build a response to a message processed by calling
198 * wps_process_msg(). The caller is responsible for freeing the buffer.
200 struct wpabuf
* wps_get_msg(struct wps_data
*wps
, enum wsc_op_code
*op_code
)
203 return wps_registrar_get_msg(wps
, op_code
);
205 return wps_enrollee_get_msg(wps
, op_code
);
210 * wps_is_selected_pbc_registrar - Check whether WPS IE indicates active PBC
211 * @msg: WPS IE contents from Beacon or Probe Response frame
212 * Returns: 1 if PBC Registrar is active, 0 if not
214 int wps_is_selected_pbc_registrar(const struct wpabuf
*msg
)
216 struct wps_parse_attr attr
;
219 * In theory, this could also verify that attr.sel_reg_config_methods
220 * includes WPS_CONFIG_PUSHBUTTON, but some deployed AP implementations
221 * do not set Selected Registrar Config Methods attribute properly, so
222 * it is safer to just use Device Password ID here.
225 if (wps_parse_msg(msg
, &attr
) < 0 ||
226 !attr
.selected_registrar
|| *attr
.selected_registrar
== 0 ||
227 !attr
.dev_password_id
||
228 WPA_GET_BE16(attr
.dev_password_id
) != DEV_PW_PUSHBUTTON
)
231 #ifdef CONFIG_WPS_STRICT
232 if (!attr
.sel_reg_config_methods
||
233 !(WPA_GET_BE16(attr
.sel_reg_config_methods
) &
234 WPS_CONFIG_PUSHBUTTON
))
236 #endif /* CONFIG_WPS_STRICT */
242 static int is_selected_pin_registrar(struct wps_parse_attr
*attr
)
245 * In theory, this could also verify that attr.sel_reg_config_methods
246 * includes WPS_CONFIG_LABEL, WPS_CONFIG_DISPLAY, or WPS_CONFIG_KEYPAD,
247 * but some deployed AP implementations do not set Selected Registrar
248 * Config Methods attribute properly, so it is safer to just use
249 * Device Password ID here.
252 if (!attr
->selected_registrar
|| *attr
->selected_registrar
== 0)
255 if (attr
->dev_password_id
!= NULL
&&
256 WPA_GET_BE16(attr
->dev_password_id
) == DEV_PW_PUSHBUTTON
)
259 #ifdef CONFIG_WPS_STRICT
260 if (!attr
->sel_reg_config_methods
||
261 !(WPA_GET_BE16(attr
->sel_reg_config_methods
) &
262 (WPS_CONFIG_LABEL
| WPS_CONFIG_DISPLAY
| WPS_CONFIG_KEYPAD
)))
264 #endif /* CONFIG_WPS_STRICT */
271 * wps_is_selected_pin_registrar - Check whether WPS IE indicates active PIN
272 * @msg: WPS IE contents from Beacon or Probe Response frame
273 * Returns: 1 if PIN Registrar is active, 0 if not
275 int wps_is_selected_pin_registrar(const struct wpabuf
*msg
)
277 struct wps_parse_attr attr
;
279 if (wps_parse_msg(msg
, &attr
) < 0)
282 return is_selected_pin_registrar(&attr
);
287 * wps_is_addr_authorized - Check whether WPS IE authorizes MAC address
288 * @msg: WPS IE contents from Beacon or Probe Response frame
289 * @addr: MAC address to search for
290 * @ver1_compat: Whether to use version 1 compatibility mode
291 * Returns: 2 if the specified address is explicit authorized, 1 if address is
292 * authorized (broadcast), 0 if not
294 int wps_is_addr_authorized(const struct wpabuf
*msg
, const u8
*addr
,
297 struct wps_parse_attr attr
;
300 const u8 bcast
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
302 if (wps_parse_msg(msg
, &attr
) < 0)
305 if (!attr
.version2
&& ver1_compat
) {
307 * Version 1.0 AP - AuthorizedMACs not used, so revert back to
308 * old mechanism of using SelectedRegistrar.
310 return is_selected_pin_registrar(&attr
);
313 if (!attr
.authorized_macs
)
316 pos
= attr
.authorized_macs
;
317 for (i
= 0; i
< attr
.authorized_macs_len
/ ETH_ALEN
; i
++) {
318 if (os_memcmp(pos
, addr
, ETH_ALEN
) == 0)
320 if (os_memcmp(pos
, bcast
, ETH_ALEN
) == 0)
330 * wps_ap_priority_compar - Prioritize WPS IE from two APs
331 * @wps_a: WPS IE contents from Beacon or Probe Response frame
332 * @wps_b: WPS IE contents from Beacon or Probe Response frame
333 * Returns: 1 if wps_b is considered more likely selection for WPS
334 * provisioning, -1 if wps_a is considered more like, or 0 if no preference
336 int wps_ap_priority_compar(const struct wpabuf
*wps_a
,
337 const struct wpabuf
*wps_b
)
339 struct wps_parse_attr attr_a
, attr_b
;
342 if (wps_a
== NULL
|| wps_parse_msg(wps_a
, &attr_a
) < 0)
344 if (wps_b
== NULL
|| wps_parse_msg(wps_b
, &attr_b
) < 0)
347 sel_a
= attr_a
.selected_registrar
&& *attr_a
.selected_registrar
!= 0;
348 sel_b
= attr_b
.selected_registrar
&& *attr_b
.selected_registrar
!= 0;
360 * wps_get_uuid_e - Get UUID-E from WPS IE
361 * @msg: WPS IE contents from Beacon or Probe Response frame
362 * Returns: Pointer to UUID-E or %NULL if not included
364 * The returned pointer is to the msg contents and it remains valid only as
365 * long as the msg buffer is valid.
367 const u8
* wps_get_uuid_e(const struct wpabuf
*msg
)
369 struct wps_parse_attr attr
;
371 if (wps_parse_msg(msg
, &attr
) < 0)
378 * wps_is_20 - Check whether WPS attributes claim support for WPS 2.0
380 int wps_is_20(const struct wpabuf
*msg
)
382 struct wps_parse_attr attr
;
384 if (msg
== NULL
|| wps_parse_msg(msg
, &attr
) < 0)
386 return attr
.version2
!= NULL
;
391 * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
392 * @req_type: Value for Request Type attribute
393 * Returns: WPS IE or %NULL on failure
395 * The caller is responsible for freeing the buffer.
397 struct wpabuf
* wps_build_assoc_req_ie(enum wps_request_type req_type
)
402 wpa_printf(MSG_DEBUG
, "WPS: Building WPS IE for (Re)Association "
404 ie
= wpabuf_alloc(100);
408 wpabuf_put_u8(ie
, WLAN_EID_VENDOR_SPECIFIC
);
409 len
= wpabuf_put(ie
, 1);
410 wpabuf_put_be32(ie
, WPS_DEV_OUI_WFA
);
412 if (wps_build_version(ie
) ||
413 wps_build_req_type(ie
, req_type
) ||
414 wps_build_wfa_ext(ie
, 0, NULL
, 0)) {
419 *len
= wpabuf_len(ie
) - 2;
426 * wps_build_assoc_resp_ie - Build WPS IE for (Re)Association Response
427 * Returns: WPS IE or %NULL on failure
429 * The caller is responsible for freeing the buffer.
431 struct wpabuf
* wps_build_assoc_resp_ie(void)
436 wpa_printf(MSG_DEBUG
, "WPS: Building WPS IE for (Re)Association "
438 ie
= wpabuf_alloc(100);
442 wpabuf_put_u8(ie
, WLAN_EID_VENDOR_SPECIFIC
);
443 len
= wpabuf_put(ie
, 1);
444 wpabuf_put_be32(ie
, WPS_DEV_OUI_WFA
);
446 if (wps_build_version(ie
) ||
447 wps_build_resp_type(ie
, WPS_RESP_AP
) ||
448 wps_build_wfa_ext(ie
, 0, NULL
, 0)) {
453 *len
= wpabuf_len(ie
) - 2;
460 * wps_build_probe_req_ie - Build WPS IE for Probe Request
461 * @pw_id: Password ID (DEV_PW_PUSHBUTTON for active PBC and DEV_PW_DEFAULT for
462 * most other use cases)
463 * @dev: Device attributes
465 * @req_type: Value for Request Type attribute
466 * @num_req_dev_types: Number of requested device types
467 * @req_dev_types: Requested device types (8 * num_req_dev_types octets) or
469 * Returns: WPS IE or %NULL on failure
471 * The caller is responsible for freeing the buffer.
473 struct wpabuf
* wps_build_probe_req_ie(u16 pw_id
, struct wps_device_data
*dev
,
475 enum wps_request_type req_type
,
476 unsigned int num_req_dev_types
,
477 const u8
*req_dev_types
)
481 wpa_printf(MSG_DEBUG
, "WPS: Building WPS IE for Probe Request");
483 ie
= wpabuf_alloc(500);
487 if (wps_build_version(ie
) ||
488 wps_build_req_type(ie
, req_type
) ||
489 wps_build_config_methods(ie
, dev
->config_methods
) ||
490 wps_build_uuid_e(ie
, uuid
) ||
491 wps_build_primary_dev_type(dev
, ie
) ||
492 wps_build_rf_bands(dev
, ie
) ||
493 wps_build_assoc_state(NULL
, ie
) ||
494 wps_build_config_error(ie
, WPS_CFG_NO_ERROR
) ||
495 wps_build_dev_password_id(ie
, pw_id
) ||
497 wps_build_manufacturer(dev
, ie
) ||
498 wps_build_model_name(dev
, ie
) ||
499 wps_build_model_number(dev
, ie
) ||
500 wps_build_dev_name(dev
, ie
) ||
501 wps_build_wfa_ext(ie
, req_type
== WPS_REQ_ENROLLEE
, NULL
, 0) ||
502 #endif /* CONFIG_WPS2 */
503 wps_build_req_dev_type(dev
, ie
, num_req_dev_types
, req_dev_types
)
505 wps_build_secondary_dev_type(dev
, ie
)
512 if (dev
->p2p
&& wps_build_dev_name(dev
, ie
)) {
516 #endif /* CONFIG_WPS2 */
518 return wps_ie_encapsulate(ie
);
522 void wps_free_pending_msgs(struct upnp_pending_message
*msgs
)
524 struct upnp_pending_message
*p
, *prev
;
529 wpabuf_free(prev
->msg
);
535 int wps_attr_text(struct wpabuf
*data
, char *buf
, char *end
)
537 struct wps_parse_attr attr
;
541 if (wps_parse_msg(data
, &attr
) < 0)
544 if (attr
.wps_state
) {
545 if (*attr
.wps_state
== WPS_STATE_NOT_CONFIGURED
)
546 ret
= os_snprintf(pos
, end
- pos
,
547 "wps_state=unconfigured\n");
548 else if (*attr
.wps_state
== WPS_STATE_CONFIGURED
)
549 ret
= os_snprintf(pos
, end
- pos
,
550 "wps_state=configured\n");
553 if (ret
< 0 || ret
>= end
- pos
)
558 if (attr
.ap_setup_locked
&& *attr
.ap_setup_locked
) {
559 ret
= os_snprintf(pos
, end
- pos
,
560 "wps_ap_setup_locked=1\n");
561 if (ret
< 0 || ret
>= end
- pos
)
566 if (attr
.selected_registrar
&& *attr
.selected_registrar
) {
567 ret
= os_snprintf(pos
, end
- pos
,
568 "wps_selected_registrar=1\n");
569 if (ret
< 0 || ret
>= end
- pos
)
574 if (attr
.dev_password_id
) {
575 ret
= os_snprintf(pos
, end
- pos
,
576 "wps_device_password_id=%u\n",
577 WPA_GET_BE16(attr
.dev_password_id
));
578 if (ret
< 0 || ret
>= end
- pos
)
583 if (attr
.sel_reg_config_methods
) {
584 ret
= os_snprintf(pos
, end
- pos
,
585 "wps_selected_registrar_config_methods="
587 WPA_GET_BE16(attr
.sel_reg_config_methods
));
588 if (ret
< 0 || ret
>= end
- pos
)
593 if (attr
.primary_dev_type
) {
594 char devtype
[WPS_DEV_TYPE_BUFSIZE
];
595 ret
= os_snprintf(pos
, end
- pos
,
596 "wps_primary_device_type=%s\n",
597 wps_dev_type_bin2str(attr
.primary_dev_type
,
600 if (ret
< 0 || ret
>= end
- pos
)
606 char *str
= os_malloc(attr
.dev_name_len
+ 1);
610 for (i
= 0; i
< attr
.dev_name_len
; i
++) {
611 if (attr
.dev_name
[i
] < 32)
614 str
[i
] = attr
.dev_name
[i
];
617 ret
= os_snprintf(pos
, end
- pos
, "wps_device_name=%s\n", str
);
619 if (ret
< 0 || ret
>= end
- pos
)
624 if (attr
.config_methods
) {
625 ret
= os_snprintf(pos
, end
- pos
,
626 "wps_config_methods=0x%04x\n",
627 WPA_GET_BE16(attr
.config_methods
));
628 if (ret
< 0 || ret
>= end
- pos
)