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"
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;
61 /* Use special PIN '00000000' for PBC */
62 data->dev_pw_id = DEV_PW_PUSHBUTTON;
63 os_free(data->dev_password);
64 data->dev_password = os_malloc(8);
65 if (data->dev_password == NULL) {
69 os_memset(data->dev_password, '0', 8);
70 data->dev_password_len = 8;
73 data->state = data->registrar ? RECV_M1 : SEND_M1;
75 if (cfg->assoc_wps_ie) {
76 struct wps_parse_attr attr;
77 wpa_hexdump_buf(MSG_DEBUG, "WPS: WPS IE from (Re)AssocReq",
79 if (wps_parse_msg(cfg->assoc_wps_ie, &attr) < 0) {
80 wpa_printf(MSG_DEBUG, "WPS: Failed to parse WPS IE "
82 } else if (attr.request_type == NULL) {
83 wpa_printf(MSG_DEBUG, "WPS: No Request Type attribute "
84 "in (Re)AssocReq WPS IE");
86 wpa_printf(MSG_DEBUG, "WPS: Request Type (from WPS IE "
87 "in (Re)AssocReq WPS IE): %d",
89 data->request_type = *attr.request_type;
93 if (cfg->new_ap_settings) {
94 data->new_ap_settings =
95 os_malloc(sizeof(*data->new_ap_settings));
96 if (data->new_ap_settings == NULL) {
100 os_memcpy(data->new_ap_settings, cfg->new_ap_settings,
101 sizeof(*data->new_ap_settings));
105 os_memcpy(data->peer_dev.mac_addr, cfg->peer_addr, ETH_ALEN);
107 data->use_psk_key = cfg->use_psk_key;
114 * wps_deinit - Deinitialize WPS Registration protocol data
115 * @data: WPS Registration protocol data from wps_init()
117 void wps_deinit(struct wps_data *data)
119 if (data->wps_pin_revealed) {
120 wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and "
121 "negotiation failed");
123 wps_registrar_invalidate_pin(data->wps->registrar,
125 } else if (data->registrar)
126 wps_registrar_unlock_pin(data->wps->registrar, data->uuid_e);
128 wpabuf_free(data->dh_privkey);
129 wpabuf_free(data->dh_pubkey_e);
130 wpabuf_free(data->dh_pubkey_r);
131 wpabuf_free(data->last_msg);
132 os_free(data->dev_password);
133 os_free(data->new_psk);
134 wps_device_data_free(&data->peer_dev);
135 os_free(data->new_ap_settings);
136 dh5_free(data->dh_ctx);
142 * wps_process_msg - Process a WPS message
143 * @wps: WPS Registration protocol data from wps_init()
144 * @op_code: Message OP Code
146 * Returns: Processing result
148 * This function is used to process WPS messages with OP Codes WSC_ACK,
149 * WSC_NACK, WSC_MSG, and WSC_Done. The caller (e.g., EAP server/peer) is
150 * responsible for reassembling the messages before calling this function.
151 * Response to this message is built by calling wps_get_msg().
153 enum wps_process_res wps_process_msg(struct wps_data *wps,
154 enum wsc_op_code op_code,
155 const struct wpabuf *msg)
158 return wps_registrar_process_msg(wps, op_code, msg);
160 return wps_enrollee_process_msg(wps, op_code, msg);
165 * wps_get_msg - Build a WPS message
166 * @wps: WPS Registration protocol data from wps_init()
167 * @op_code: Buffer for returning message OP Code
168 * Returns: The generated WPS message or %NULL on failure
170 * This function is used to build a response to a message processed by calling
171 * wps_process_msg(). The caller is responsible for freeing the buffer.
173 struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code)
176 return wps_registrar_get_msg(wps, op_code);
178 return wps_enrollee_get_msg(wps, op_code);
183 * wps_is_selected_pbc_registrar - Check whether WPS IE indicates active PBC
184 * @msg: WPS IE contents from Beacon or Probe Response frame
185 * Returns: 1 if PBC Registrar is active, 0 if not
187 int wps_is_selected_pbc_registrar(const struct wpabuf *msg)
189 struct wps_parse_attr attr;
192 * In theory, this could also verify that attr.sel_reg_config_methods
193 * includes WPS_CONFIG_PUSHBUTTON, but some deployed AP implementations
194 * do not set Selected Registrar Config Methods attribute properly, so
195 * it is safer to just use Device Password ID here.
198 if (wps_parse_msg(msg, &attr) < 0 ||
199 !attr.selected_registrar || *attr.selected_registrar == 0 ||
200 !attr.dev_password_id ||
201 WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
204 #ifdef CONFIG_WPS_STRICT
205 if (!attr.sel_reg_config_methods ||
206 !(WPA_GET_BE16(attr.sel_reg_config_methods) &
207 WPS_CONFIG_PUSHBUTTON))
209 #endif /* CONFIG_WPS_STRICT */
215 static int is_selected_pin_registrar(struct wps_parse_attr *attr)
218 * In theory, this could also verify that attr.sel_reg_config_methods
219 * includes WPS_CONFIG_LABEL, WPS_CONFIG_DISPLAY, or WPS_CONFIG_KEYPAD,
220 * but some deployed AP implementations do not set Selected Registrar
221 * Config Methods attribute properly, so it is safer to just use
222 * Device Password ID here.
225 if (!attr->selected_registrar || *attr->selected_registrar == 0)
228 if (attr->dev_password_id != NULL &&
229 WPA_GET_BE16(attr->dev_password_id) == DEV_PW_PUSHBUTTON)
232 #ifdef CONFIG_WPS_STRICT
233 if (!attr->sel_reg_config_methods ||
234 !(WPA_GET_BE16(attr->sel_reg_config_methods) &
235 (WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD)))
237 #endif /* CONFIG_WPS_STRICT */
244 * wps_is_selected_pin_registrar - Check whether WPS IE indicates active PIN
245 * @msg: WPS IE contents from Beacon or Probe Response frame
246 * Returns: 1 if PIN Registrar is active, 0 if not
248 int wps_is_selected_pin_registrar(const struct wpabuf *msg)
250 struct wps_parse_attr attr;
252 if (wps_parse_msg(msg, &attr) < 0)
255 return is_selected_pin_registrar(&attr);
260 * wps_is_addr_authorized - Check whether WPS IE authorizes MAC address
261 * @msg: WPS IE contents from Beacon or Probe Response frame
262 * @addr: MAC address to search for
263 * @ver1_compat: Whether to use version 1 compatibility mode
264 * Returns: 1 if address is authorized, 0 if not
266 int wps_is_addr_authorized(const struct wpabuf *msg, const u8 *addr,
269 struct wps_parse_attr attr;
273 if (wps_parse_msg(msg, &attr) < 0)
276 if (!attr.version2 && ver1_compat) {
278 * Version 1.0 AP - AuthorizedMACs not used, so revert back to
279 * old mechanism of using SelectedRegistrar.
281 return is_selected_pin_registrar(&attr);
284 if (!attr.authorized_macs)
287 pos = attr.authorized_macs;
288 for (i = 0; i < attr.authorized_macs_len / ETH_ALEN; i++) {
289 if (os_memcmp(pos, addr, ETH_ALEN) == 0)
299 * wps_ap_priority_compar - Prioritize WPS IE from two APs
300 * @wps_a: WPS IE contents from Beacon or Probe Response frame
301 * @wps_b: WPS IE contents from Beacon or Probe Response frame
302 * Returns: 1 if wps_b is considered more likely selection for WPS
303 * provisioning, -1 if wps_a is considered more like, or 0 if no preference
305 int wps_ap_priority_compar(const struct wpabuf *wps_a,
306 const struct wpabuf *wps_b)
308 struct wps_parse_attr attr_a, attr_b;
311 if (wps_a == NULL || wps_parse_msg(wps_a, &attr_a) < 0)
313 if (wps_b == NULL || wps_parse_msg(wps_b, &attr_b) < 0)
316 sel_a = attr_a.selected_registrar && *attr_a.selected_registrar != 0;
317 sel_b = attr_b.selected_registrar && *attr_b.selected_registrar != 0;
329 * wps_get_uuid_e - Get UUID-E from WPS IE
330 * @msg: WPS IE contents from Beacon or Probe Response frame
331 * Returns: Pointer to UUID-E or %NULL if not included
333 * The returned pointer is to the msg contents and it remains valid only as
334 * long as the msg buffer is valid.
336 const u8 * wps_get_uuid_e(const struct wpabuf *msg)
338 struct wps_parse_attr attr;
340 if (wps_parse_msg(msg, &attr) < 0)
347 * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
348 * @req_type: Value for Request Type attribute
349 * Returns: WPS IE or %NULL on failure
351 * The caller is responsible for freeing the buffer.
353 struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type)
358 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
360 ie = wpabuf_alloc(100);
364 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
365 len = wpabuf_put(ie, 1);
366 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
368 if (wps_build_version(ie) ||
369 wps_build_req_type(ie, req_type) ||
370 wps_build_wfa_ext(ie, 0, NULL, 0)) {
375 *len = wpabuf_len(ie) - 2;
382 * wps_build_assoc_resp_ie - Build WPS IE for (Re)Association Response
383 * Returns: WPS IE or %NULL on failure
385 * The caller is responsible for freeing the buffer.
387 struct wpabuf * wps_build_assoc_resp_ie(void)
392 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
394 ie = wpabuf_alloc(100);
398 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
399 len = wpabuf_put(ie, 1);
400 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
402 if (wps_build_version(ie) ||
403 wps_build_resp_type(ie, WPS_RESP_AP) ||
404 wps_build_wfa_ext(ie, 0, NULL, 0)) {
409 *len = wpabuf_len(ie) - 2;
416 * wps_build_probe_req_ie - Build WPS IE for Probe Request
417 * @pbc: Whether searching for PBC mode APs
418 * @dev: Device attributes
420 * @req_type: Value for Request Type attribute
421 * Returns: WPS IE or %NULL on failure
423 * The caller is responsible for freeing the buffer.
425 struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
427 enum wps_request_type req_type)
432 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
434 ie = wpabuf_alloc(500);
439 methods = WPS_CONFIG_PUSHBUTTON;
442 * TODO: At least in theory, should figure out whether this
443 * Probe Request was triggered with physical or virtual
446 methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
447 #endif /* CONFIG_WPS2 */
450 * TODO: At least in theory, should figure out whether this
451 * Probe Request was triggered using physical or virtual
454 methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY |
456 WPS_CONFIG_VIRT_DISPLAY |
457 #endif /* CONFIG_WPS2 */
459 #ifdef CONFIG_WPS_UFD
460 methods |= WPS_CONFIG_USBA;
461 #endif /* CONFIG_WPS_UFD */
462 #ifdef CONFIG_WPS_NFC
463 methods |= WPS_CONFIG_NFC_INTERFACE;
464 #endif /* CONFIG_WPS_NFC */
467 if (wps_build_version(ie) ||
468 wps_build_req_type(ie, req_type) ||
469 wps_build_config_methods(ie, methods) ||
470 wps_build_uuid_e(ie, uuid) ||
471 wps_build_primary_dev_type(dev, ie) ||
472 wps_build_rf_bands(dev, ie) ||
473 wps_build_assoc_state(NULL, ie) ||
474 wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
475 wps_build_dev_password_id(ie, pbc ? DEV_PW_PUSHBUTTON :
478 wps_build_manufacturer(dev, ie) ||
479 wps_build_model_name(dev, ie) ||
480 wps_build_model_number(dev, ie) ||
481 wps_build_dev_name(dev, ie) ||
482 wps_build_wfa_ext(ie, req_type == WPS_REQ_ENROLLEE, NULL, 0)
483 #else /* CONFIG_WPS2 */
485 #endif /* CONFIG_WPS2 */
492 if (dev->p2p && wps_build_dev_name(dev, ie)) {
496 #endif /* CONFIG_WPS2 */
498 return wps_ie_encapsulate(ie);
502 void wps_free_pending_msgs(struct upnp_pending_message *msgs)
504 struct upnp_pending_message *p, *prev;
509 wpabuf_free(prev->msg);
515 int wps_attr_text(struct wpabuf *data, char *buf, char *end)
517 struct wps_parse_attr attr;
521 if (wps_parse_msg(data, &attr) < 0)
524 if (attr.wps_state) {
525 if (*attr.wps_state == WPS_STATE_NOT_CONFIGURED)
526 ret = os_snprintf(pos, end - pos,
527 "wps_state=unconfigured\n");
528 else if (*attr.wps_state == WPS_STATE_CONFIGURED)
529 ret = os_snprintf(pos, end - pos,
530 "wps_state=configured\n");
533 if (ret < 0 || ret >= end - pos)
538 if (attr.ap_setup_locked && *attr.ap_setup_locked) {
539 ret = os_snprintf(pos, end - pos,
540 "wps_ap_setup_locked=1\n");
541 if (ret < 0 || ret >= end - pos)
546 if (attr.selected_registrar && *attr.selected_registrar) {
547 ret = os_snprintf(pos, end - pos,
548 "wps_selected_registrar=1\n");
549 if (ret < 0 || ret >= end - pos)
554 if (attr.dev_password_id) {
555 ret = os_snprintf(pos, end - pos,
556 "wps_device_password_id=%u\n",
557 WPA_GET_BE16(attr.dev_password_id));
558 if (ret < 0 || ret >= end - pos)
563 if (attr.sel_reg_config_methods) {
564 ret = os_snprintf(pos, end - pos,
565 "wps_selected_registrar_config_methods="
567 WPA_GET_BE16(attr.sel_reg_config_methods));
568 if (ret < 0 || ret >= end - pos)
573 if (attr.primary_dev_type) {
574 char devtype[WPS_DEV_TYPE_BUFSIZE];
575 ret = os_snprintf(pos, end - pos,
576 "wps_primary_device_type=%s\n",
577 wps_dev_type_bin2str(attr.primary_dev_type,
580 if (ret < 0 || ret >= end - pos)
586 char *str = os_malloc(attr.dev_name_len + 1);
590 for (i = 0; i < attr.dev_name_len; i++) {
591 if (attr.dev_name[i] < 32)
594 str[i] = attr.dev_name[i];
597 ret = os_snprintf(pos, end - pos, "wps_device_name=%s\n", str);
599 if (ret < 0 || ret >= end - pos)
604 if (attr.config_methods) {
605 ret = os_snprintf(pos, end - pos,
606 "wps_config_methods=0x%04x\n",
607 WPA_GET_BE16(attr.config_methods));
608 if (ret < 0 || ret >= end - pos)