2 * Wi-Fi Protected Setup
3 * Copyright (c) 2007-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.
18 #include "crypto/dh_group5.h"
19 #include "common/ieee802_11_defs.h"
21 #include "wps_dev_attr.h"
24 #ifdef CONFIG_WPS_TESTING
25 int wps_version_number = 0x20;
26 int wps_testing_dummy_cred = 0;
27 #endif /* CONFIG_WPS_TESTING */
31 * wps_init - Initialize WPS Registration protocol data
32 * @cfg: WPS configuration
33 * Returns: Pointer to allocated data or %NULL on failure
35 * This function is used to initialize WPS data for a registration protocol
36 * instance (i.e., each run of registration protocol as a Registrar of
37 * Enrollee. The caller is responsible for freeing this data after the
38 * registration run has been completed by calling wps_deinit().
40 struct wps_data * wps_init(const struct wps_config *cfg)
42 struct wps_data *data = os_zalloc(sizeof(*data));
46 data->registrar = cfg->registrar;
48 os_memcpy(data->uuid_r, cfg->wps->uuid, WPS_UUID_LEN);
50 os_memcpy(data->mac_addr_e, cfg->wps->dev.mac_addr, ETH_ALEN);
51 os_memcpy(data->uuid_e, cfg->wps->uuid, WPS_UUID_LEN);
54 data->dev_pw_id = data->wps->oob_dev_pw_id == 0 ?
55 cfg->dev_pw_id : data->wps->oob_dev_pw_id;
56 data->dev_password = os_malloc(cfg->pin_len);
57 if (data->dev_password == NULL) {
61 os_memcpy(data->dev_password, cfg->pin, cfg->pin_len);
62 data->dev_password_len = cfg->pin_len;
67 /* Use special PIN '00000000' for PBC */
68 data->dev_pw_id = DEV_PW_PUSHBUTTON;
69 os_free(data->dev_password);
70 data->dev_password = os_malloc(8);
71 if (data->dev_password == NULL) {
75 os_memset(data->dev_password, '0', 8);
76 data->dev_password_len = 8;
79 data->state = data->registrar ? RECV_M1 : SEND_M1;
81 if (cfg->assoc_wps_ie) {
82 struct wps_parse_attr attr;
83 wpa_hexdump_buf(MSG_DEBUG, "WPS: WPS IE from (Re)AssocReq",
85 if (wps_parse_msg(cfg->assoc_wps_ie, &attr) < 0) {
86 wpa_printf(MSG_DEBUG, "WPS: Failed to parse WPS IE "
88 } else if (attr.request_type == NULL) {
89 wpa_printf(MSG_DEBUG, "WPS: No Request Type attribute "
90 "in (Re)AssocReq WPS IE");
92 wpa_printf(MSG_DEBUG, "WPS: Request Type (from WPS IE "
93 "in (Re)AssocReq WPS IE): %d",
95 data->request_type = *attr.request_type;
99 if (cfg->new_ap_settings) {
100 data->new_ap_settings =
101 os_malloc(sizeof(*data->new_ap_settings));
102 if (data->new_ap_settings == NULL) {
106 os_memcpy(data->new_ap_settings, cfg->new_ap_settings,
107 sizeof(*data->new_ap_settings));
111 os_memcpy(data->peer_dev.mac_addr, cfg->peer_addr, ETH_ALEN);
112 if (cfg->p2p_dev_addr)
113 os_memcpy(data->p2p_dev_addr, cfg->p2p_dev_addr, ETH_ALEN);
115 data->use_psk_key = cfg->use_psk_key;
116 data->pbc_in_m1 = cfg->pbc_in_m1;
123 * wps_deinit - Deinitialize WPS Registration protocol data
124 * @data: WPS Registration protocol data from wps_init()
126 void wps_deinit(struct wps_data *data)
128 if (data->wps_pin_revealed) {
129 wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and "
130 "negotiation failed");
132 wps_registrar_invalidate_pin(data->wps->registrar,
134 } else if (data->registrar)
135 wps_registrar_unlock_pin(data->wps->registrar, data->uuid_e);
137 wpabuf_free(data->dh_privkey);
138 wpabuf_free(data->dh_pubkey_e);
139 wpabuf_free(data->dh_pubkey_r);
140 wpabuf_free(data->last_msg);
141 os_free(data->dev_password);
142 os_free(data->new_psk);
143 wps_device_data_free(&data->peer_dev);
144 os_free(data->new_ap_settings);
145 dh5_free(data->dh_ctx);
151 * wps_process_msg - Process a WPS message
152 * @wps: WPS Registration protocol data from wps_init()
153 * @op_code: Message OP Code
155 * Returns: Processing result
157 * This function is used to process WPS messages with OP Codes WSC_ACK,
158 * WSC_NACK, WSC_MSG, and WSC_Done. The caller (e.g., EAP server/peer) is
159 * responsible for reassembling the messages before calling this function.
160 * Response to this message is built by calling wps_get_msg().
162 enum wps_process_res wps_process_msg(struct wps_data *wps,
163 enum wsc_op_code op_code,
164 const struct wpabuf *msg)
167 return wps_registrar_process_msg(wps, op_code, msg);
169 return wps_enrollee_process_msg(wps, op_code, msg);
174 * wps_get_msg - Build a WPS message
175 * @wps: WPS Registration protocol data from wps_init()
176 * @op_code: Buffer for returning message OP Code
177 * Returns: The generated WPS message or %NULL on failure
179 * This function is used to build a response to a message processed by calling
180 * wps_process_msg(). The caller is responsible for freeing the buffer.
182 struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code)
185 return wps_registrar_get_msg(wps, op_code);
187 return wps_enrollee_get_msg(wps, op_code);
192 * wps_is_selected_pbc_registrar - Check whether WPS IE indicates active PBC
193 * @msg: WPS IE contents from Beacon or Probe Response frame
194 * Returns: 1 if PBC Registrar is active, 0 if not
196 int wps_is_selected_pbc_registrar(const struct wpabuf *msg)
198 struct wps_parse_attr attr;
201 * In theory, this could also verify that attr.sel_reg_config_methods
202 * includes WPS_CONFIG_PUSHBUTTON, but some deployed AP implementations
203 * do not set Selected Registrar Config Methods attribute properly, so
204 * it is safer to just use Device Password ID here.
207 if (wps_parse_msg(msg, &attr) < 0 ||
208 !attr.selected_registrar || *attr.selected_registrar == 0 ||
209 !attr.dev_password_id ||
210 WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
213 #ifdef CONFIG_WPS_STRICT
214 if (!attr.sel_reg_config_methods ||
215 !(WPA_GET_BE16(attr.sel_reg_config_methods) &
216 WPS_CONFIG_PUSHBUTTON))
218 #endif /* CONFIG_WPS_STRICT */
224 static int is_selected_pin_registrar(struct wps_parse_attr *attr)
227 * In theory, this could also verify that attr.sel_reg_config_methods
228 * includes WPS_CONFIG_LABEL, WPS_CONFIG_DISPLAY, or WPS_CONFIG_KEYPAD,
229 * but some deployed AP implementations do not set Selected Registrar
230 * Config Methods attribute properly, so it is safer to just use
231 * Device Password ID here.
234 if (!attr->selected_registrar || *attr->selected_registrar == 0)
237 if (attr->dev_password_id != NULL &&
238 WPA_GET_BE16(attr->dev_password_id) == DEV_PW_PUSHBUTTON)
241 #ifdef CONFIG_WPS_STRICT
242 if (!attr->sel_reg_config_methods ||
243 !(WPA_GET_BE16(attr->sel_reg_config_methods) &
244 (WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD)))
246 #endif /* CONFIG_WPS_STRICT */
253 * wps_is_selected_pin_registrar - Check whether WPS IE indicates active PIN
254 * @msg: WPS IE contents from Beacon or Probe Response frame
255 * Returns: 1 if PIN Registrar is active, 0 if not
257 int wps_is_selected_pin_registrar(const struct wpabuf *msg)
259 struct wps_parse_attr attr;
261 if (wps_parse_msg(msg, &attr) < 0)
264 return is_selected_pin_registrar(&attr);
269 * wps_is_addr_authorized - Check whether WPS IE authorizes MAC address
270 * @msg: WPS IE contents from Beacon or Probe Response frame
271 * @addr: MAC address to search for
272 * @ver1_compat: Whether to use version 1 compatibility mode
273 * Returns: 1 if address is authorized, 0 if not
275 int wps_is_addr_authorized(const struct wpabuf *msg, const u8 *addr,
278 struct wps_parse_attr attr;
281 const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
283 if (wps_parse_msg(msg, &attr) < 0)
286 if (!attr.version2 && ver1_compat) {
288 * Version 1.0 AP - AuthorizedMACs not used, so revert back to
289 * old mechanism of using SelectedRegistrar.
291 return is_selected_pin_registrar(&attr);
294 if (!attr.authorized_macs)
297 pos = attr.authorized_macs;
298 for (i = 0; i < attr.authorized_macs_len / ETH_ALEN; i++) {
299 if (os_memcmp(pos, addr, ETH_ALEN) == 0 ||
300 os_memcmp(pos, bcast, ETH_ALEN) == 0)
310 * wps_ap_priority_compar - Prioritize WPS IE from two APs
311 * @wps_a: WPS IE contents from Beacon or Probe Response frame
312 * @wps_b: WPS IE contents from Beacon or Probe Response frame
313 * Returns: 1 if wps_b is considered more likely selection for WPS
314 * provisioning, -1 if wps_a is considered more like, or 0 if no preference
316 int wps_ap_priority_compar(const struct wpabuf *wps_a,
317 const struct wpabuf *wps_b)
319 struct wps_parse_attr attr_a, attr_b;
322 if (wps_a == NULL || wps_parse_msg(wps_a, &attr_a) < 0)
324 if (wps_b == NULL || wps_parse_msg(wps_b, &attr_b) < 0)
327 sel_a = attr_a.selected_registrar && *attr_a.selected_registrar != 0;
328 sel_b = attr_b.selected_registrar && *attr_b.selected_registrar != 0;
340 * wps_get_uuid_e - Get UUID-E from WPS IE
341 * @msg: WPS IE contents from Beacon or Probe Response frame
342 * Returns: Pointer to UUID-E or %NULL if not included
344 * The returned pointer is to the msg contents and it remains valid only as
345 * long as the msg buffer is valid.
347 const u8 * wps_get_uuid_e(const struct wpabuf *msg)
349 struct wps_parse_attr attr;
351 if (wps_parse_msg(msg, &attr) < 0)
358 * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
359 * @req_type: Value for Request Type attribute
360 * Returns: WPS IE or %NULL on failure
362 * The caller is responsible for freeing the buffer.
364 struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type)
369 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
371 ie = wpabuf_alloc(100);
375 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
376 len = wpabuf_put(ie, 1);
377 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
379 if (wps_build_version(ie) ||
380 wps_build_req_type(ie, req_type) ||
381 wps_build_wfa_ext(ie, 0, NULL, 0)) {
386 *len = wpabuf_len(ie) - 2;
393 * wps_build_assoc_resp_ie - Build WPS IE for (Re)Association Response
394 * Returns: WPS IE or %NULL on failure
396 * The caller is responsible for freeing the buffer.
398 struct wpabuf * wps_build_assoc_resp_ie(void)
403 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
405 ie = wpabuf_alloc(100);
409 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
410 len = wpabuf_put(ie, 1);
411 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
413 if (wps_build_version(ie) ||
414 wps_build_resp_type(ie, WPS_RESP_AP) ||
415 wps_build_wfa_ext(ie, 0, NULL, 0)) {
420 *len = wpabuf_len(ie) - 2;
427 * wps_build_probe_req_ie - Build WPS IE for Probe Request
428 * @pbc: Whether searching for PBC mode APs
429 * @dev: Device attributes
431 * @req_type: Value for Request Type attribute
432 * @num_req_dev_types: Number of requested device types
433 * @req_dev_types: Requested device types (8 * num_req_dev_types octets) or
435 * Returns: WPS IE or %NULL on failure
437 * The caller is responsible for freeing the buffer.
439 struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
441 enum wps_request_type req_type,
442 unsigned int num_req_dev_types,
443 const u8 *req_dev_types)
448 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
450 ie = wpabuf_alloc(500);
454 methods |= WPS_CONFIG_PUSHBUTTON;
457 * TODO: Should figure out whether this device has a physical or
458 * virtual pushbutton.
460 methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
461 #endif /* CONFIG_WPS2 */
464 * TODO: Should figure out whether this Probe Request was triggered
465 * using physical or virtual display. Also, if the device has a PIN on
466 * a label, that should be indicated here.
468 methods |= WPS_CONFIG_DISPLAY |
470 WPS_CONFIG_VIRT_DISPLAY |
471 #endif /* CONFIG_WPS2 */
473 #ifdef CONFIG_WPS_UFD
474 methods |= WPS_CONFIG_USBA;
475 #endif /* CONFIG_WPS_UFD */
476 #ifdef CONFIG_WPS_NFC
477 methods |= WPS_CONFIG_NFC_INTERFACE;
478 #endif /* CONFIG_WPS_NFC */
480 if (wps_build_version(ie) ||
481 wps_build_req_type(ie, req_type) ||
482 wps_build_config_methods(ie, methods) ||
483 wps_build_uuid_e(ie, uuid) ||
484 wps_build_primary_dev_type(dev, ie) ||
485 wps_build_rf_bands(dev, ie) ||
486 wps_build_assoc_state(NULL, ie) ||
487 wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
488 wps_build_dev_password_id(ie, pbc ? DEV_PW_PUSHBUTTON :
491 wps_build_manufacturer(dev, ie) ||
492 wps_build_model_name(dev, ie) ||
493 wps_build_model_number(dev, ie) ||
494 wps_build_dev_name(dev, ie) ||
495 wps_build_wfa_ext(ie, req_type == WPS_REQ_ENROLLEE, NULL, 0) ||
496 #endif /* CONFIG_WPS2 */
497 wps_build_req_dev_type(dev, ie, num_req_dev_types, req_dev_types)
499 wps_build_secondary_dev_type(dev, ie)
506 if (dev->p2p && wps_build_dev_name(dev, ie)) {
510 #endif /* CONFIG_WPS2 */
512 return wps_ie_encapsulate(ie);
516 void wps_free_pending_msgs(struct upnp_pending_message *msgs)
518 struct upnp_pending_message *p, *prev;
523 wpabuf_free(prev->msg);
529 int wps_attr_text(struct wpabuf *data, char *buf, char *end)
531 struct wps_parse_attr attr;
535 if (wps_parse_msg(data, &attr) < 0)
538 if (attr.wps_state) {
539 if (*attr.wps_state == WPS_STATE_NOT_CONFIGURED)
540 ret = os_snprintf(pos, end - pos,
541 "wps_state=unconfigured\n");
542 else if (*attr.wps_state == WPS_STATE_CONFIGURED)
543 ret = os_snprintf(pos, end - pos,
544 "wps_state=configured\n");
547 if (ret < 0 || ret >= end - pos)
552 if (attr.ap_setup_locked && *attr.ap_setup_locked) {
553 ret = os_snprintf(pos, end - pos,
554 "wps_ap_setup_locked=1\n");
555 if (ret < 0 || ret >= end - pos)
560 if (attr.selected_registrar && *attr.selected_registrar) {
561 ret = os_snprintf(pos, end - pos,
562 "wps_selected_registrar=1\n");
563 if (ret < 0 || ret >= end - pos)
568 if (attr.dev_password_id) {
569 ret = os_snprintf(pos, end - pos,
570 "wps_device_password_id=%u\n",
571 WPA_GET_BE16(attr.dev_password_id));
572 if (ret < 0 || ret >= end - pos)
577 if (attr.sel_reg_config_methods) {
578 ret = os_snprintf(pos, end - pos,
579 "wps_selected_registrar_config_methods="
581 WPA_GET_BE16(attr.sel_reg_config_methods));
582 if (ret < 0 || ret >= end - pos)
587 if (attr.primary_dev_type) {
588 char devtype[WPS_DEV_TYPE_BUFSIZE];
589 ret = os_snprintf(pos, end - pos,
590 "wps_primary_device_type=%s\n",
591 wps_dev_type_bin2str(attr.primary_dev_type,
594 if (ret < 0 || ret >= end - pos)
600 char *str = os_malloc(attr.dev_name_len + 1);
604 for (i = 0; i < attr.dev_name_len; i++) {
605 if (attr.dev_name[i] < 32)
608 str[i] = attr.dev_name[i];
611 ret = os_snprintf(pos, end - pos, "wps_device_name=%s\n", str);
613 if (ret < 0 || ret >= end - pos)
618 if (attr.config_methods) {
619 ret = os_snprintf(pos, end - pos,
620 "wps_config_methods=0x%04x\n",
621 WPA_GET_BE16(attr.config_methods));
622 if (ret < 0 || ret >= end - pos)