Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / src / wps / wps.c
index 1f259c2..fbaf85a 100644 (file)
@@ -2,14 +2,8 @@
  * Wi-Fi Protected Setup
  * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  */
 
 #include "includes.h"
@@ -24,6 +18,7 @@
 #ifdef CONFIG_WPS_TESTING
 int wps_version_number = 0x20;
 int wps_testing_dummy_cred = 0;
+int wps_corrupt_pkhash = 0;
 #endif /* CONFIG_WPS_TESTING */
 
 
@@ -51,8 +46,7 @@ struct wps_data * wps_init(const struct wps_config *cfg)
                os_memcpy(data->uuid_e, cfg->wps->uuid, WPS_UUID_LEN);
        }
        if (cfg->pin) {
-               data->dev_pw_id = data->wps->oob_dev_pw_id == 0 ?
-                       cfg->dev_pw_id : data->wps->oob_dev_pw_id;
+               data->dev_pw_id = cfg->dev_pw_id;
                data->dev_password = os_malloc(cfg->pin_len);
                if (data->dev_password == NULL) {
                        os_free(data);
@@ -60,19 +54,47 @@ struct wps_data * wps_init(const struct wps_config *cfg)
                }
                os_memcpy(data->dev_password, cfg->pin, cfg->pin_len);
                data->dev_password_len = cfg->pin_len;
+               wpa_hexdump_key(MSG_DEBUG, "WPS: AP PIN dev_password",
+                               data->dev_password, data->dev_password_len);
+       }
+
+#ifdef CONFIG_WPS_NFC
+       if (cfg->pin == NULL &&
+           cfg->dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER)
+               data->dev_pw_id = cfg->dev_pw_id;
+
+       if (cfg->wps->ap && !cfg->registrar && cfg->wps->ap_nfc_dev_pw_id) {
+               /* Keep AP PIN as alternative Device Password */
+               data->alt_dev_pw_id = data->dev_pw_id;
+               data->alt_dev_password = data->dev_password;
+               data->alt_dev_password_len = data->dev_password_len;
+
+               data->dev_pw_id = cfg->wps->ap_nfc_dev_pw_id;
+               data->dev_password =
+                       os_malloc(wpabuf_len(cfg->wps->ap_nfc_dev_pw));
+               if (data->dev_password == NULL) {
+                       os_free(data);
+                       return NULL;
+               }
+               os_memcpy(data->dev_password,
+                         wpabuf_head(cfg->wps->ap_nfc_dev_pw),
+                         wpabuf_len(cfg->wps->ap_nfc_dev_pw));
+               data->dev_password_len = wpabuf_len(cfg->wps->ap_nfc_dev_pw);
+               wpa_hexdump_key(MSG_DEBUG, "WPS: NFC dev_password",
+                           data->dev_password, data->dev_password_len);
        }
+#endif /* CONFIG_WPS_NFC */
 
        data->pbc = cfg->pbc;
        if (cfg->pbc) {
                /* Use special PIN '00000000' for PBC */
                data->dev_pw_id = DEV_PW_PUSHBUTTON;
-               os_free(data->dev_password);
-               data->dev_password = os_malloc(8);
+               bin_clear_free(data->dev_password, data->dev_password_len);
+               data->dev_password = (u8 *) os_strdup("00000000");
                if (data->dev_password == NULL) {
                        os_free(data);
                        return NULL;
                }
-               os_memset(data->dev_password, '0', 8);
                data->dev_password_len = 8;
        }
 
@@ -100,6 +122,8 @@ struct wps_data * wps_init(const struct wps_config *cfg)
                data->new_ap_settings =
                        os_malloc(sizeof(*data->new_ap_settings));
                if (data->new_ap_settings == NULL) {
+                       bin_clear_free(data->dev_password,
+                                      data->dev_password_len);
                        os_free(data);
                        return NULL;
                }
@@ -109,8 +133,17 @@ struct wps_data * wps_init(const struct wps_config *cfg)
 
        if (cfg->peer_addr)
                os_memcpy(data->peer_dev.mac_addr, cfg->peer_addr, ETH_ALEN);
+       if (cfg->p2p_dev_addr)
+               os_memcpy(data->p2p_dev_addr, cfg->p2p_dev_addr, ETH_ALEN);
 
        data->use_psk_key = cfg->use_psk_key;
+       data->pbc_in_m1 = cfg->pbc_in_m1;
+
+       if (cfg->peer_pubkey_hash) {
+               os_memcpy(data->peer_pubkey_hash, cfg->peer_pubkey_hash,
+                         WPS_OOB_PUBKEY_HASH_LEN);
+               data->peer_pubkey_hash_set = 1;
+       }
 
        return data;
 }
@@ -122,6 +155,12 @@ struct wps_data * wps_init(const struct wps_config *cfg)
  */
 void wps_deinit(struct wps_data *data)
 {
+#ifdef CONFIG_WPS_NFC
+       if (data->registrar && data->nfc_pw_token)
+               wps_registrar_remove_nfc_pw_token(data->wps->registrar,
+                                                 data->nfc_pw_token);
+#endif /* CONFIG_WPS_NFC */
+
        if (data->wps_pin_revealed) {
                wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and "
                           "negotiation failed");
@@ -135,10 +174,11 @@ void wps_deinit(struct wps_data *data)
        wpabuf_free(data->dh_pubkey_e);
        wpabuf_free(data->dh_pubkey_r);
        wpabuf_free(data->last_msg);
-       os_free(data->dev_password);
-       os_free(data->new_psk);
+       bin_clear_free(data->dev_password, data->dev_password_len);
+       bin_clear_free(data->alt_dev_password, data->alt_dev_password_len);
+       bin_clear_free(data->new_psk, data->new_psk_len);
        wps_device_data_free(&data->peer_dev);
-       os_free(data->new_ap_settings);
+       bin_clear_free(data->new_ap_settings, sizeof(*data->new_ap_settings));
        dh5_free(data->dh_ctx);
        os_free(data);
 }
@@ -267,7 +307,8 @@ int wps_is_selected_pin_registrar(const struct wpabuf *msg)
  * @msg: WPS IE contents from Beacon or Probe Response frame
  * @addr: MAC address to search for
  * @ver1_compat: Whether to use version 1 compatibility mode
- * Returns: 1 if address is authorized, 0 if not
+ * Returns: 2 if the specified address is explicit authorized, 1 if address is
+ * authorized (broadcast), 0 if not
  */
 int wps_is_addr_authorized(const struct wpabuf *msg, const u8 *addr,
                           int ver1_compat)
@@ -293,8 +334,9 @@ int wps_is_addr_authorized(const struct wpabuf *msg, const u8 *addr,
 
        pos = attr.authorized_macs;
        for (i = 0; i < attr.authorized_macs_len / ETH_ALEN; i++) {
-               if (os_memcmp(pos, addr, ETH_ALEN) == 0 ||
-                   os_memcmp(pos, bcast, ETH_ALEN) == 0)
+               if (os_memcmp(pos, addr, ETH_ALEN) == 0)
+                       return 2;
+               if (os_memcmp(pos, bcast, ETH_ALEN) == 0)
                        return 1;
                pos += ETH_ALEN;
        }
@@ -313,16 +355,16 @@ int wps_is_addr_authorized(const struct wpabuf *msg, const u8 *addr,
 int wps_ap_priority_compar(const struct wpabuf *wps_a,
                           const struct wpabuf *wps_b)
 {
-       struct wps_parse_attr attr_a, attr_b;
+       struct wps_parse_attr attr;
        int sel_a, sel_b;
 
-       if (wps_a == NULL || wps_parse_msg(wps_a, &attr_a) < 0)
+       if (wps_a == NULL || wps_parse_msg(wps_a, &attr) < 0)
                return 1;
-       if (wps_b == NULL || wps_parse_msg(wps_b, &attr_b) < 0)
-               return -1;
+       sel_a = attr.selected_registrar && *attr.selected_registrar != 0;
 
-       sel_a = attr_a.selected_registrar && *attr_a.selected_registrar != 0;
-       sel_b = attr_b.selected_registrar && *attr_b.selected_registrar != 0;
+       if (wps_b == NULL || wps_parse_msg(wps_b, &attr) < 0)
+               return -1;
+       sel_b = attr.selected_registrar && *attr.selected_registrar != 0;
 
        if (sel_a && !sel_b)
                return -1;
@@ -352,6 +394,19 @@ const u8 * wps_get_uuid_e(const struct wpabuf *msg)
 
 
 /**
+ * wps_is_20 - Check whether WPS attributes claim support for WPS 2.0
+ */
+int wps_is_20(const struct wpabuf *msg)
+{
+       struct wps_parse_attr attr;
+
+       if (msg == NULL || wps_parse_msg(msg, &attr) < 0)
+               return 0;
+       return attr.version2 != NULL;
+}
+
+
+/**
  * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
  * @req_type: Value for Request Type attribute
  * Returns: WPS IE or %NULL on failure
@@ -422,20 +477,25 @@ struct wpabuf * wps_build_assoc_resp_ie(void)
 
 /**
  * wps_build_probe_req_ie - Build WPS IE for Probe Request
- * @pbc: Whether searching for PBC mode APs
+ * @pw_id: Password ID (DEV_PW_PUSHBUTTON for active PBC and DEV_PW_DEFAULT for
+ * most other use cases)
  * @dev: Device attributes
  * @uuid: Own UUID
  * @req_type: Value for Request Type attribute
+ * @num_req_dev_types: Number of requested device types
+ * @req_dev_types: Requested device types (8 * num_req_dev_types octets) or
+ *     %NULL if none
  * Returns: WPS IE or %NULL on failure
  *
  * The caller is responsible for freeing the buffer.
  */
-struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
+struct wpabuf * wps_build_probe_req_ie(u16 pw_id, struct wps_device_data *dev,
                                       const u8 *uuid,
-                                      enum wps_request_type req_type)
+                                      enum wps_request_type req_type,
+                                      unsigned int num_req_dev_types,
+                                      const u8 *req_dev_types)
 {
        struct wpabuf *ie;
-       u16 methods;
 
        wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
 
@@ -443,66 +503,28 @@ struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
        if (ie == NULL)
                return NULL;
 
-       if (pbc) {
-               methods = WPS_CONFIG_PUSHBUTTON;
-#ifdef CONFIG_WPS2
-               /*
-                * TODO: At least in theory, should figure out whether this
-                * Probe Request was triggered with physical or virtual
-                * pushbutton.
-                */
-               methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
-#endif /* CONFIG_WPS2 */
-       } else {
-               /*
-                * TODO: At least in theory, should figure out whether this
-                * Probe Request was triggered using physical or virtual
-                * display.
-                */
-               methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY |
-#ifdef CONFIG_WPS2
-                       WPS_CONFIG_VIRT_DISPLAY |
-#endif /* CONFIG_WPS2 */
-                       WPS_CONFIG_KEYPAD;
-#ifdef CONFIG_WPS_UFD
-               methods |= WPS_CONFIG_USBA;
-#endif /* CONFIG_WPS_UFD */
-#ifdef CONFIG_WPS_NFC
-               methods |= WPS_CONFIG_NFC_INTERFACE;
-#endif /* CONFIG_WPS_NFC */
-       }
-
        if (wps_build_version(ie) ||
            wps_build_req_type(ie, req_type) ||
-           wps_build_config_methods(ie, methods) ||
+           wps_build_config_methods(ie, dev->config_methods) ||
            wps_build_uuid_e(ie, uuid) ||
            wps_build_primary_dev_type(dev, ie) ||
-           wps_build_rf_bands(dev, ie) ||
+           wps_build_rf_bands(dev, ie, 0) ||
            wps_build_assoc_state(NULL, ie) ||
            wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
-           wps_build_dev_password_id(ie, pbc ? DEV_PW_PUSHBUTTON :
-                                     DEV_PW_DEFAULT) ||
-#ifdef CONFIG_WPS2
+           wps_build_dev_password_id(ie, pw_id) ||
            wps_build_manufacturer(dev, ie) ||
            wps_build_model_name(dev, ie) ||
            wps_build_model_number(dev, ie) ||
            wps_build_dev_name(dev, ie) ||
-           wps_build_wfa_ext(ie, req_type == WPS_REQ_ENROLLEE, NULL, 0)
-#else /* CONFIG_WPS2 */
-           0
-#endif /* CONFIG_WPS2 */
+           wps_build_wfa_ext(ie, req_type == WPS_REQ_ENROLLEE, NULL, 0) ||
+           wps_build_req_dev_type(dev, ie, num_req_dev_types, req_dev_types)
+           ||
+           wps_build_secondary_dev_type(dev, ie)
                ) {
                wpabuf_free(ie);
                return NULL;
        }
 
-#ifndef CONFIG_WPS2
-       if (dev->p2p && wps_build_dev_name(dev, ie)) {
-               wpabuf_free(ie);
-               return NULL;
-       }
-#endif /* CONFIG_WPS2 */
-
        return wps_ie_encapsulate(ie);
 }
 
@@ -538,7 +560,7 @@ int wps_attr_text(struct wpabuf *data, char *buf, char *end)
                                          "wps_state=configured\n");
                else
                        ret = 0;
-               if (ret < 0 || ret >= end - pos)
+               if (os_snprintf_error(end - pos, ret))
                        return pos - buf;
                pos += ret;
        }
@@ -546,7 +568,7 @@ int wps_attr_text(struct wpabuf *data, char *buf, char *end)
        if (attr.ap_setup_locked && *attr.ap_setup_locked) {
                ret = os_snprintf(pos, end - pos,
                                  "wps_ap_setup_locked=1\n");
-               if (ret < 0 || ret >= end - pos)
+               if (os_snprintf_error(end - pos, ret))
                        return pos - buf;
                pos += ret;
        }
@@ -554,7 +576,7 @@ int wps_attr_text(struct wpabuf *data, char *buf, char *end)
        if (attr.selected_registrar && *attr.selected_registrar) {
                ret = os_snprintf(pos, end - pos,
                                  "wps_selected_registrar=1\n");
-               if (ret < 0 || ret >= end - pos)
+               if (os_snprintf_error(end - pos, ret))
                        return pos - buf;
                pos += ret;
        }
@@ -563,7 +585,7 @@ int wps_attr_text(struct wpabuf *data, char *buf, char *end)
                ret = os_snprintf(pos, end - pos,
                                  "wps_device_password_id=%u\n",
                                  WPA_GET_BE16(attr.dev_password_id));
-               if (ret < 0 || ret >= end - pos)
+               if (os_snprintf_error(end - pos, ret))
                        return pos - buf;
                pos += ret;
        }
@@ -573,7 +595,7 @@ int wps_attr_text(struct wpabuf *data, char *buf, char *end)
                                  "wps_selected_registrar_config_methods="
                                  "0x%04x\n",
                                  WPA_GET_BE16(attr.sel_reg_config_methods));
-               if (ret < 0 || ret >= end - pos)
+               if (os_snprintf_error(end - pos, ret))
                        return pos - buf;
                pos += ret;
        }
@@ -585,7 +607,7 @@ int wps_attr_text(struct wpabuf *data, char *buf, char *end)
                                  wps_dev_type_bin2str(attr.primary_dev_type,
                                                       devtype,
                                                       sizeof(devtype)));
-               if (ret < 0 || ret >= end - pos)
+               if (os_snprintf_error(end - pos, ret))
                        return pos - buf;
                pos += ret;
        }
@@ -596,7 +618,8 @@ int wps_attr_text(struct wpabuf *data, char *buf, char *end)
                if (str == NULL)
                        return pos - buf;
                for (i = 0; i < attr.dev_name_len; i++) {
-                       if (attr.dev_name[i] < 32)
+                       if (attr.dev_name[i] == 0 ||
+                           is_ctrl_char(attr.dev_name[i]))
                                str[i] = '_';
                        else
                                str[i] = attr.dev_name[i];
@@ -604,7 +627,7 @@ int wps_attr_text(struct wpabuf *data, char *buf, char *end)
                str[i] = '\0';
                ret = os_snprintf(pos, end - pos, "wps_device_name=%s\n", str);
                os_free(str);
-               if (ret < 0 || ret >= end - pos)
+               if (os_snprintf_error(end - pos, ret))
                        return pos - buf;
                pos += ret;
        }
@@ -613,10 +636,27 @@ int wps_attr_text(struct wpabuf *data, char *buf, char *end)
                ret = os_snprintf(pos, end - pos,
                                  "wps_config_methods=0x%04x\n",
                                  WPA_GET_BE16(attr.config_methods));
-               if (ret < 0 || ret >= end - pos)
+               if (os_snprintf_error(end - pos, ret))
                        return pos - buf;
                pos += ret;
        }
 
        return pos - buf;
 }
+
+
+const char * wps_ei_str(enum wps_error_indication ei)
+{
+       switch (ei) {
+       case WPS_EI_NO_ERROR:
+               return "No Error";
+       case WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED:
+               return "TKIP Only Prohibited";
+       case WPS_EI_SECURITY_WEP_PROHIBITED:
+               return "WEP Prohibited";
+       case WPS_EI_AUTH_FAILURE:
+               return "Authentication Failure";
+       default:
+               return "Unknown";
+       }
+}