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;
122 * wps_deinit - Deinitialize WPS Registration protocol data
123 * @data: WPS Registration protocol data from wps_init()
125 void wps_deinit(struct wps_data *data)
127 if (data->wps_pin_revealed) {
128 wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and "
129 "negotiation failed");
131 wps_registrar_invalidate_pin(data->wps->registrar,
133 } else if (data->registrar)
134 wps_registrar_unlock_pin(data->wps->registrar, data->uuid_e);
136 wpabuf_free(data->dh_privkey);
137 wpabuf_free(data->dh_pubkey_e);
138 wpabuf_free(data->dh_pubkey_r);
139 wpabuf_free(data->last_msg);
140 os_free(data->dev_password);
141 os_free(data->new_psk);
142 wps_device_data_free(&data->peer_dev);
143 os_free(data->new_ap_settings);
144 dh5_free(data->dh_ctx);
150 * wps_process_msg - Process a WPS message
151 * @wps: WPS Registration protocol data from wps_init()
152 * @op_code: Message OP Code
154 * Returns: Processing result
156 * This function is used to process WPS messages with OP Codes WSC_ACK,
157 * WSC_NACK, WSC_MSG, and WSC_Done. The caller (e.g., EAP server/peer) is
158 * responsible for reassembling the messages before calling this function.
159 * Response to this message is built by calling wps_get_msg().
161 enum wps_process_res wps_process_msg(struct wps_data *wps,
162 enum wsc_op_code op_code,
163 const struct wpabuf *msg)
166 return wps_registrar_process_msg(wps, op_code, msg);
168 return wps_enrollee_process_msg(wps, op_code, msg);
173 * wps_get_msg - Build a WPS message
174 * @wps: WPS Registration protocol data from wps_init()
175 * @op_code: Buffer for returning message OP Code
176 * Returns: The generated WPS message or %NULL on failure
178 * This function is used to build a response to a message processed by calling
179 * wps_process_msg(). The caller is responsible for freeing the buffer.
181 struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code)
184 return wps_registrar_get_msg(wps, op_code);
186 return wps_enrollee_get_msg(wps, op_code);
191 * wps_is_selected_pbc_registrar - Check whether WPS IE indicates active PBC
192 * @msg: WPS IE contents from Beacon or Probe Response frame
193 * Returns: 1 if PBC Registrar is active, 0 if not
195 int wps_is_selected_pbc_registrar(const struct wpabuf *msg)
197 struct wps_parse_attr attr;
200 * In theory, this could also verify that attr.sel_reg_config_methods
201 * includes WPS_CONFIG_PUSHBUTTON, but some deployed AP implementations
202 * do not set Selected Registrar Config Methods attribute properly, so
203 * it is safer to just use Device Password ID here.
206 if (wps_parse_msg(msg, &attr) < 0 ||
207 !attr.selected_registrar || *attr.selected_registrar == 0 ||
208 !attr.dev_password_id ||
209 WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
212 #ifdef CONFIG_WPS_STRICT
213 if (!attr.sel_reg_config_methods ||
214 !(WPA_GET_BE16(attr.sel_reg_config_methods) &
215 WPS_CONFIG_PUSHBUTTON))
217 #endif /* CONFIG_WPS_STRICT */
223 static int is_selected_pin_registrar(struct wps_parse_attr *attr)
226 * In theory, this could also verify that attr.sel_reg_config_methods
227 * includes WPS_CONFIG_LABEL, WPS_CONFIG_DISPLAY, or WPS_CONFIG_KEYPAD,
228 * but some deployed AP implementations do not set Selected Registrar
229 * Config Methods attribute properly, so it is safer to just use
230 * Device Password ID here.
233 if (!attr->selected_registrar || *attr->selected_registrar == 0)
236 if (attr->dev_password_id != NULL &&
237 WPA_GET_BE16(attr->dev_password_id) == DEV_PW_PUSHBUTTON)
240 #ifdef CONFIG_WPS_STRICT
241 if (!attr->sel_reg_config_methods ||
242 !(WPA_GET_BE16(attr->sel_reg_config_methods) &
243 (WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD)))
245 #endif /* CONFIG_WPS_STRICT */
252 * wps_is_selected_pin_registrar - Check whether WPS IE indicates active PIN
253 * @msg: WPS IE contents from Beacon or Probe Response frame
254 * Returns: 1 if PIN Registrar is active, 0 if not
256 int wps_is_selected_pin_registrar(const struct wpabuf *msg)
258 struct wps_parse_attr attr;
260 if (wps_parse_msg(msg, &attr) < 0)
263 return is_selected_pin_registrar(&attr);
268 * wps_is_addr_authorized - Check whether WPS IE authorizes MAC address
269 * @msg: WPS IE contents from Beacon or Probe Response frame
270 * @addr: MAC address to search for
271 * @ver1_compat: Whether to use version 1 compatibility mode
272 * Returns: 1 if address is authorized, 0 if not
274 int wps_is_addr_authorized(const struct wpabuf *msg, const u8 *addr,
277 struct wps_parse_attr attr;
280 const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
282 if (wps_parse_msg(msg, &attr) < 0)
285 if (!attr.version2 && ver1_compat) {
287 * Version 1.0 AP - AuthorizedMACs not used, so revert back to
288 * old mechanism of using SelectedRegistrar.
290 return is_selected_pin_registrar(&attr);
293 if (!attr.authorized_macs)
296 pos = attr.authorized_macs;
297 for (i = 0; i < attr.authorized_macs_len / ETH_ALEN; i++) {
298 if (os_memcmp(pos, addr, ETH_ALEN) == 0 ||
299 os_memcmp(pos, bcast, ETH_ALEN) == 0)
309 * wps_ap_priority_compar - Prioritize WPS IE from two APs
310 * @wps_a: WPS IE contents from Beacon or Probe Response frame
311 * @wps_b: WPS IE contents from Beacon or Probe Response frame
312 * Returns: 1 if wps_b is considered more likely selection for WPS
313 * provisioning, -1 if wps_a is considered more like, or 0 if no preference
315 int wps_ap_priority_compar(const struct wpabuf *wps_a,
316 const struct wpabuf *wps_b)
318 struct wps_parse_attr attr_a, attr_b;
321 if (wps_a == NULL || wps_parse_msg(wps_a, &attr_a) < 0)
323 if (wps_b == NULL || wps_parse_msg(wps_b, &attr_b) < 0)
326 sel_a = attr_a.selected_registrar && *attr_a.selected_registrar != 0;
327 sel_b = attr_b.selected_registrar && *attr_b.selected_registrar != 0;
339 * wps_get_uuid_e - Get UUID-E from WPS IE
340 * @msg: WPS IE contents from Beacon or Probe Response frame
341 * Returns: Pointer to UUID-E or %NULL if not included
343 * The returned pointer is to the msg contents and it remains valid only as
344 * long as the msg buffer is valid.
346 const u8 * wps_get_uuid_e(const struct wpabuf *msg)
348 struct wps_parse_attr attr;
350 if (wps_parse_msg(msg, &attr) < 0)
357 * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
358 * @req_type: Value for Request Type attribute
359 * Returns: WPS IE or %NULL on failure
361 * The caller is responsible for freeing the buffer.
363 struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type)
368 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
370 ie = wpabuf_alloc(100);
374 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
375 len = wpabuf_put(ie, 1);
376 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
378 if (wps_build_version(ie) ||
379 wps_build_req_type(ie, req_type) ||
380 wps_build_wfa_ext(ie, 0, NULL, 0)) {
385 *len = wpabuf_len(ie) - 2;
392 * wps_build_assoc_resp_ie - Build WPS IE for (Re)Association Response
393 * Returns: WPS IE or %NULL on failure
395 * The caller is responsible for freeing the buffer.
397 struct wpabuf * wps_build_assoc_resp_ie(void)
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_resp_type(ie, WPS_RESP_AP) ||
414 wps_build_wfa_ext(ie, 0, NULL, 0)) {
419 *len = wpabuf_len(ie) - 2;
426 * wps_build_probe_req_ie - Build WPS IE for Probe Request
427 * @pbc: Whether searching for PBC mode APs
428 * @dev: Device attributes
430 * @req_type: Value for Request Type attribute
431 * Returns: WPS IE or %NULL on failure
433 * The caller is responsible for freeing the buffer.
435 struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
437 enum wps_request_type req_type)
442 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
444 ie = wpabuf_alloc(500);
448 methods |= WPS_CONFIG_PUSHBUTTON;
451 * TODO: Should figure out whether this device has a physical or
452 * virtual pushbutton.
454 methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
455 #endif /* CONFIG_WPS2 */
458 * TODO: Should figure out whether this Probe Request was triggered
459 * using physical or virtual display. Also, if the device has a PIN on
460 * a label, that should be indicated here.
462 methods |= WPS_CONFIG_DISPLAY |
464 WPS_CONFIG_VIRT_DISPLAY |
465 #endif /* CONFIG_WPS2 */
467 #ifdef CONFIG_WPS_UFD
468 methods |= WPS_CONFIG_USBA;
469 #endif /* CONFIG_WPS_UFD */
470 #ifdef CONFIG_WPS_NFC
471 methods |= WPS_CONFIG_NFC_INTERFACE;
472 #endif /* CONFIG_WPS_NFC */
474 if (wps_build_version(ie) ||
475 wps_build_req_type(ie, req_type) ||
476 wps_build_config_methods(ie, methods) ||
477 wps_build_uuid_e(ie, uuid) ||
478 wps_build_primary_dev_type(dev, ie) ||
479 wps_build_rf_bands(dev, ie) ||
480 wps_build_assoc_state(NULL, ie) ||
481 wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
482 wps_build_dev_password_id(ie, pbc ? DEV_PW_PUSHBUTTON :
485 wps_build_manufacturer(dev, ie) ||
486 wps_build_model_name(dev, ie) ||
487 wps_build_model_number(dev, ie) ||
488 wps_build_dev_name(dev, ie) ||
489 wps_build_wfa_ext(ie, req_type == WPS_REQ_ENROLLEE, NULL, 0)
490 #else /* CONFIG_WPS2 */
492 #endif /* CONFIG_WPS2 */
499 if (dev->p2p && wps_build_dev_name(dev, ie)) {
503 #endif /* CONFIG_WPS2 */
505 return wps_ie_encapsulate(ie);
509 void wps_free_pending_msgs(struct upnp_pending_message *msgs)
511 struct upnp_pending_message *p, *prev;
516 wpabuf_free(prev->msg);
522 int wps_attr_text(struct wpabuf *data, char *buf, char *end)
524 struct wps_parse_attr attr;
528 if (wps_parse_msg(data, &attr) < 0)
531 if (attr.wps_state) {
532 if (*attr.wps_state == WPS_STATE_NOT_CONFIGURED)
533 ret = os_snprintf(pos, end - pos,
534 "wps_state=unconfigured\n");
535 else if (*attr.wps_state == WPS_STATE_CONFIGURED)
536 ret = os_snprintf(pos, end - pos,
537 "wps_state=configured\n");
540 if (ret < 0 || ret >= end - pos)
545 if (attr.ap_setup_locked && *attr.ap_setup_locked) {
546 ret = os_snprintf(pos, end - pos,
547 "wps_ap_setup_locked=1\n");
548 if (ret < 0 || ret >= end - pos)
553 if (attr.selected_registrar && *attr.selected_registrar) {
554 ret = os_snprintf(pos, end - pos,
555 "wps_selected_registrar=1\n");
556 if (ret < 0 || ret >= end - pos)
561 if (attr.dev_password_id) {
562 ret = os_snprintf(pos, end - pos,
563 "wps_device_password_id=%u\n",
564 WPA_GET_BE16(attr.dev_password_id));
565 if (ret < 0 || ret >= end - pos)
570 if (attr.sel_reg_config_methods) {
571 ret = os_snprintf(pos, end - pos,
572 "wps_selected_registrar_config_methods="
574 WPA_GET_BE16(attr.sel_reg_config_methods));
575 if (ret < 0 || ret >= end - pos)
580 if (attr.primary_dev_type) {
581 char devtype[WPS_DEV_TYPE_BUFSIZE];
582 ret = os_snprintf(pos, end - pos,
583 "wps_primary_device_type=%s\n",
584 wps_dev_type_bin2str(attr.primary_dev_type,
587 if (ret < 0 || ret >= end - pos)
593 char *str = os_malloc(attr.dev_name_len + 1);
597 for (i = 0; i < attr.dev_name_len; i++) {
598 if (attr.dev_name[i] < 32)
601 str[i] = attr.dev_name[i];
604 ret = os_snprintf(pos, end - pos, "wps_device_name=%s\n", str);
606 if (ret < 0 || ret >= end - pos)
611 if (attr.config_methods) {
612 ret = os_snprintf(pos, end - pos,
613 "wps_config_methods=0x%04x\n",
614 WPA_GET_BE16(attr.config_methods));
615 if (ret < 0 || ret >= end - pos)