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 = cfg->dev_pw_id;
49 data->dev_password = os_malloc(cfg->pin_len);
50 if (data->dev_password == NULL) {
54 os_memcpy(data->dev_password, cfg->pin, cfg->pin_len);
55 data->dev_password_len = cfg->pin_len;
56 wpa_hexdump_key(MSG_DEBUG, "WPS: AP PIN dev_password",
57 data->dev_password, data->dev_password_len);
61 if (cfg->wps->ap && !cfg->registrar && cfg->wps->ap_nfc_dev_pw_id) {
62 /* Keep AP PIN as alternative Device Password */
63 data->alt_dev_pw_id = data->dev_pw_id;
64 data->alt_dev_password = data->dev_password;
65 data->alt_dev_password_len = data->dev_password_len;
67 data->dev_pw_id = cfg->wps->ap_nfc_dev_pw_id;
69 os_malloc(wpabuf_len(cfg->wps->ap_nfc_dev_pw));
70 if (data->dev_password == NULL) {
74 os_memcpy(data->dev_password,
75 wpabuf_head(cfg->wps->ap_nfc_dev_pw),
76 wpabuf_len(cfg->wps->ap_nfc_dev_pw));
77 data->dev_password_len = wpabuf_len(cfg->wps->ap_nfc_dev_pw);
78 wpa_hexdump_key(MSG_DEBUG, "WPS: NFC dev_password",
79 data->dev_password, data->dev_password_len);
81 #endif /* CONFIG_WPS_NFC */
85 /* Use special PIN '00000000' for PBC */
86 data->dev_pw_id = DEV_PW_PUSHBUTTON;
87 os_free(data->dev_password);
88 data->dev_password = (u8 *) os_strdup("00000000");
89 if (data->dev_password == NULL) {
93 data->dev_password_len = 8;
96 data->state = data->registrar ? RECV_M1 : SEND_M1;
98 if (cfg->assoc_wps_ie) {
99 struct wps_parse_attr attr;
100 wpa_hexdump_buf(MSG_DEBUG, "WPS: WPS IE from (Re)AssocReq",
102 if (wps_parse_msg(cfg->assoc_wps_ie, &attr) < 0) {
103 wpa_printf(MSG_DEBUG, "WPS: Failed to parse WPS IE "
104 "from (Re)AssocReq");
105 } else if (attr.request_type == NULL) {
106 wpa_printf(MSG_DEBUG, "WPS: No Request Type attribute "
107 "in (Re)AssocReq WPS IE");
109 wpa_printf(MSG_DEBUG, "WPS: Request Type (from WPS IE "
110 "in (Re)AssocReq WPS IE): %d",
112 data->request_type = *attr.request_type;
116 if (cfg->new_ap_settings) {
117 data->new_ap_settings =
118 os_malloc(sizeof(*data->new_ap_settings));
119 if (data->new_ap_settings == NULL) {
120 os_free(data->dev_password);
124 os_memcpy(data->new_ap_settings, cfg->new_ap_settings,
125 sizeof(*data->new_ap_settings));
129 os_memcpy(data->peer_dev.mac_addr, cfg->peer_addr, ETH_ALEN);
130 if (cfg->p2p_dev_addr)
131 os_memcpy(data->p2p_dev_addr, cfg->p2p_dev_addr, ETH_ALEN);
133 data->use_psk_key = cfg->use_psk_key;
134 data->pbc_in_m1 = cfg->pbc_in_m1;
141 * wps_deinit - Deinitialize WPS Registration protocol data
142 * @data: WPS Registration protocol data from wps_init()
144 void wps_deinit(struct wps_data *data)
146 #ifdef CONFIG_WPS_NFC
147 if (data->registrar && data->nfc_pw_token)
148 wps_registrar_remove_nfc_pw_token(data->wps->registrar,
150 #endif /* CONFIG_WPS_NFC */
152 if (data->wps_pin_revealed) {
153 wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and "
154 "negotiation failed");
156 wps_registrar_invalidate_pin(data->wps->registrar,
158 } else if (data->registrar)
159 wps_registrar_unlock_pin(data->wps->registrar, data->uuid_e);
161 wpabuf_free(data->dh_privkey);
162 wpabuf_free(data->dh_pubkey_e);
163 wpabuf_free(data->dh_pubkey_r);
164 wpabuf_free(data->last_msg);
165 os_free(data->dev_password);
166 os_free(data->alt_dev_password);
167 os_free(data->new_psk);
168 wps_device_data_free(&data->peer_dev);
169 os_free(data->new_ap_settings);
170 dh5_free(data->dh_ctx);
171 os_free(data->nfc_pw_token);
177 * wps_process_msg - Process a WPS message
178 * @wps: WPS Registration protocol data from wps_init()
179 * @op_code: Message OP Code
181 * Returns: Processing result
183 * This function is used to process WPS messages with OP Codes WSC_ACK,
184 * WSC_NACK, WSC_MSG, and WSC_Done. The caller (e.g., EAP server/peer) is
185 * responsible for reassembling the messages before calling this function.
186 * Response to this message is built by calling wps_get_msg().
188 enum wps_process_res wps_process_msg(struct wps_data *wps,
189 enum wsc_op_code op_code,
190 const struct wpabuf *msg)
193 return wps_registrar_process_msg(wps, op_code, msg);
195 return wps_enrollee_process_msg(wps, op_code, msg);
200 * wps_get_msg - Build a WPS message
201 * @wps: WPS Registration protocol data from wps_init()
202 * @op_code: Buffer for returning message OP Code
203 * Returns: The generated WPS message or %NULL on failure
205 * This function is used to build a response to a message processed by calling
206 * wps_process_msg(). The caller is responsible for freeing the buffer.
208 struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code)
211 return wps_registrar_get_msg(wps, op_code);
213 return wps_enrollee_get_msg(wps, op_code);
218 * wps_is_selected_pbc_registrar - Check whether WPS IE indicates active PBC
219 * @msg: WPS IE contents from Beacon or Probe Response frame
220 * Returns: 1 if PBC Registrar is active, 0 if not
222 int wps_is_selected_pbc_registrar(const struct wpabuf *msg)
224 struct wps_parse_attr attr;
227 * In theory, this could also verify that attr.sel_reg_config_methods
228 * includes WPS_CONFIG_PUSHBUTTON, but some deployed AP implementations
229 * do not set Selected Registrar Config Methods attribute properly, so
230 * it is safer to just use Device Password ID here.
233 if (wps_parse_msg(msg, &attr) < 0 ||
234 !attr.selected_registrar || *attr.selected_registrar == 0 ||
235 !attr.dev_password_id ||
236 WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
239 #ifdef CONFIG_WPS_STRICT
240 if (!attr.sel_reg_config_methods ||
241 !(WPA_GET_BE16(attr.sel_reg_config_methods) &
242 WPS_CONFIG_PUSHBUTTON))
244 #endif /* CONFIG_WPS_STRICT */
250 static int is_selected_pin_registrar(struct wps_parse_attr *attr)
253 * In theory, this could also verify that attr.sel_reg_config_methods
254 * includes WPS_CONFIG_LABEL, WPS_CONFIG_DISPLAY, or WPS_CONFIG_KEYPAD,
255 * but some deployed AP implementations do not set Selected Registrar
256 * Config Methods attribute properly, so it is safer to just use
257 * Device Password ID here.
260 if (!attr->selected_registrar || *attr->selected_registrar == 0)
263 if (attr->dev_password_id != NULL &&
264 WPA_GET_BE16(attr->dev_password_id) == DEV_PW_PUSHBUTTON)
267 #ifdef CONFIG_WPS_STRICT
268 if (!attr->sel_reg_config_methods ||
269 !(WPA_GET_BE16(attr->sel_reg_config_methods) &
270 (WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD)))
272 #endif /* CONFIG_WPS_STRICT */
279 * wps_is_selected_pin_registrar - Check whether WPS IE indicates active PIN
280 * @msg: WPS IE contents from Beacon or Probe Response frame
281 * Returns: 1 if PIN Registrar is active, 0 if not
283 int wps_is_selected_pin_registrar(const struct wpabuf *msg)
285 struct wps_parse_attr attr;
287 if (wps_parse_msg(msg, &attr) < 0)
290 return is_selected_pin_registrar(&attr);
295 * wps_is_addr_authorized - Check whether WPS IE authorizes MAC address
296 * @msg: WPS IE contents from Beacon or Probe Response frame
297 * @addr: MAC address to search for
298 * @ver1_compat: Whether to use version 1 compatibility mode
299 * Returns: 2 if the specified address is explicit authorized, 1 if address is
300 * authorized (broadcast), 0 if not
302 int wps_is_addr_authorized(const struct wpabuf *msg, const u8 *addr,
305 struct wps_parse_attr attr;
308 const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
310 if (wps_parse_msg(msg, &attr) < 0)
313 if (!attr.version2 && ver1_compat) {
315 * Version 1.0 AP - AuthorizedMACs not used, so revert back to
316 * old mechanism of using SelectedRegistrar.
318 return is_selected_pin_registrar(&attr);
321 if (!attr.authorized_macs)
324 pos = attr.authorized_macs;
325 for (i = 0; i < attr.authorized_macs_len / ETH_ALEN; i++) {
326 if (os_memcmp(pos, addr, ETH_ALEN) == 0)
328 if (os_memcmp(pos, bcast, ETH_ALEN) == 0)
338 * wps_ap_priority_compar - Prioritize WPS IE from two APs
339 * @wps_a: WPS IE contents from Beacon or Probe Response frame
340 * @wps_b: WPS IE contents from Beacon or Probe Response frame
341 * Returns: 1 if wps_b is considered more likely selection for WPS
342 * provisioning, -1 if wps_a is considered more like, or 0 if no preference
344 int wps_ap_priority_compar(const struct wpabuf *wps_a,
345 const struct wpabuf *wps_b)
347 struct wps_parse_attr attr_a, attr_b;
350 if (wps_a == NULL || wps_parse_msg(wps_a, &attr_a) < 0)
352 if (wps_b == NULL || wps_parse_msg(wps_b, &attr_b) < 0)
355 sel_a = attr_a.selected_registrar && *attr_a.selected_registrar != 0;
356 sel_b = attr_b.selected_registrar && *attr_b.selected_registrar != 0;
368 * wps_get_uuid_e - Get UUID-E from WPS IE
369 * @msg: WPS IE contents from Beacon or Probe Response frame
370 * Returns: Pointer to UUID-E or %NULL if not included
372 * The returned pointer is to the msg contents and it remains valid only as
373 * long as the msg buffer is valid.
375 const u8 * wps_get_uuid_e(const struct wpabuf *msg)
377 struct wps_parse_attr attr;
379 if (wps_parse_msg(msg, &attr) < 0)
386 * wps_is_20 - Check whether WPS attributes claim support for WPS 2.0
388 int wps_is_20(const struct wpabuf *msg)
390 struct wps_parse_attr attr;
392 if (msg == NULL || wps_parse_msg(msg, &attr) < 0)
394 return attr.version2 != NULL;
399 * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
400 * @req_type: Value for Request Type attribute
401 * Returns: WPS IE or %NULL on failure
403 * The caller is responsible for freeing the buffer.
405 struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type)
410 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
412 ie = wpabuf_alloc(100);
416 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
417 len = wpabuf_put(ie, 1);
418 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
420 if (wps_build_version(ie) ||
421 wps_build_req_type(ie, req_type) ||
422 wps_build_wfa_ext(ie, 0, NULL, 0)) {
427 *len = wpabuf_len(ie) - 2;
434 * wps_build_assoc_resp_ie - Build WPS IE for (Re)Association Response
435 * Returns: WPS IE or %NULL on failure
437 * The caller is responsible for freeing the buffer.
439 struct wpabuf * wps_build_assoc_resp_ie(void)
444 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
446 ie = wpabuf_alloc(100);
450 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
451 len = wpabuf_put(ie, 1);
452 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
454 if (wps_build_version(ie) ||
455 wps_build_resp_type(ie, WPS_RESP_AP) ||
456 wps_build_wfa_ext(ie, 0, NULL, 0)) {
461 *len = wpabuf_len(ie) - 2;
468 * wps_build_probe_req_ie - Build WPS IE for Probe Request
469 * @pw_id: Password ID (DEV_PW_PUSHBUTTON for active PBC and DEV_PW_DEFAULT for
470 * most other use cases)
471 * @dev: Device attributes
473 * @req_type: Value for Request Type attribute
474 * @num_req_dev_types: Number of requested device types
475 * @req_dev_types: Requested device types (8 * num_req_dev_types octets) or
477 * Returns: WPS IE or %NULL on failure
479 * The caller is responsible for freeing the buffer.
481 struct wpabuf * wps_build_probe_req_ie(u16 pw_id, struct wps_device_data *dev,
483 enum wps_request_type req_type,
484 unsigned int num_req_dev_types,
485 const u8 *req_dev_types)
489 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
491 ie = wpabuf_alloc(500);
495 if (wps_build_version(ie) ||
496 wps_build_req_type(ie, req_type) ||
497 wps_build_config_methods(ie, dev->config_methods) ||
498 wps_build_uuid_e(ie, uuid) ||
499 wps_build_primary_dev_type(dev, ie) ||
500 wps_build_rf_bands(dev, ie) ||
501 wps_build_assoc_state(NULL, ie) ||
502 wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
503 wps_build_dev_password_id(ie, pw_id) ||
505 wps_build_manufacturer(dev, ie) ||
506 wps_build_model_name(dev, ie) ||
507 wps_build_model_number(dev, ie) ||
508 wps_build_dev_name(dev, ie) ||
509 wps_build_wfa_ext(ie, req_type == WPS_REQ_ENROLLEE, NULL, 0) ||
510 #endif /* CONFIG_WPS2 */
511 wps_build_req_dev_type(dev, ie, num_req_dev_types, req_dev_types)
513 wps_build_secondary_dev_type(dev, ie)
520 if (dev->p2p && wps_build_dev_name(dev, ie)) {
524 #endif /* CONFIG_WPS2 */
526 return wps_ie_encapsulate(ie);
530 void wps_free_pending_msgs(struct upnp_pending_message *msgs)
532 struct upnp_pending_message *p, *prev;
537 wpabuf_free(prev->msg);
543 int wps_attr_text(struct wpabuf *data, char *buf, char *end)
545 struct wps_parse_attr attr;
549 if (wps_parse_msg(data, &attr) < 0)
552 if (attr.wps_state) {
553 if (*attr.wps_state == WPS_STATE_NOT_CONFIGURED)
554 ret = os_snprintf(pos, end - pos,
555 "wps_state=unconfigured\n");
556 else if (*attr.wps_state == WPS_STATE_CONFIGURED)
557 ret = os_snprintf(pos, end - pos,
558 "wps_state=configured\n");
561 if (ret < 0 || ret >= end - pos)
566 if (attr.ap_setup_locked && *attr.ap_setup_locked) {
567 ret = os_snprintf(pos, end - pos,
568 "wps_ap_setup_locked=1\n");
569 if (ret < 0 || ret >= end - pos)
574 if (attr.selected_registrar && *attr.selected_registrar) {
575 ret = os_snprintf(pos, end - pos,
576 "wps_selected_registrar=1\n");
577 if (ret < 0 || ret >= end - pos)
582 if (attr.dev_password_id) {
583 ret = os_snprintf(pos, end - pos,
584 "wps_device_password_id=%u\n",
585 WPA_GET_BE16(attr.dev_password_id));
586 if (ret < 0 || ret >= end - pos)
591 if (attr.sel_reg_config_methods) {
592 ret = os_snprintf(pos, end - pos,
593 "wps_selected_registrar_config_methods="
595 WPA_GET_BE16(attr.sel_reg_config_methods));
596 if (ret < 0 || ret >= end - pos)
601 if (attr.primary_dev_type) {
602 char devtype[WPS_DEV_TYPE_BUFSIZE];
603 ret = os_snprintf(pos, end - pos,
604 "wps_primary_device_type=%s\n",
605 wps_dev_type_bin2str(attr.primary_dev_type,
608 if (ret < 0 || ret >= end - pos)
614 char *str = os_malloc(attr.dev_name_len + 1);
618 for (i = 0; i < attr.dev_name_len; i++) {
619 if (attr.dev_name[i] < 32)
622 str[i] = attr.dev_name[i];
625 ret = os_snprintf(pos, end - pos, "wps_device_name=%s\n", str);
627 if (ret < 0 || ret >= end - pos)
632 if (attr.config_methods) {
633 ret = os_snprintf(pos, end - pos,
634 "wps_config_methods=0x%04x\n",
635 WPA_GET_BE16(attr.config_methods));
636 if (ret < 0 || ret >= end - pos)