WPS: Make testing operations configurable at runtime
[libeap.git] / src / wps / wps.c
index 870a493..1f259c2 100644 (file)
 #include "includes.h"
 
 #include "common.h"
+#include "crypto/dh_group5.h"
+#include "common/ieee802_11_defs.h"
 #include "wps_i.h"
 #include "wps_dev_attr.h"
-#include "ieee802_11_defs.h"
+
+
+#ifdef CONFIG_WPS_TESTING
+int wps_version_number = 0x20;
+int wps_testing_dummy_cred = 0;
+#endif /* CONFIG_WPS_TESTING */
 
 
 /**
@@ -45,7 +52,7 @@ struct wps_data * wps_init(const struct wps_config *cfg)
        }
        if (cfg->pin) {
                data->dev_pw_id = data->wps->oob_dev_pw_id == 0 ?
-                       DEV_PW_DEFAULT : data->wps->oob_dev_pw_id;
+                       cfg->dev_pw_id : data->wps->oob_dev_pw_id;
                data->dev_password = os_malloc(cfg->pin_len);
                if (data->dev_password == NULL) {
                        os_free(data);
@@ -103,6 +110,8 @@ 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);
 
+       data->use_psk_key = cfg->use_psk_key;
+
        return data;
 }
 
@@ -130,6 +139,7 @@ void wps_deinit(struct wps_data *data)
        os_free(data->new_psk);
        wps_device_data_free(&data->peer_dev);
        os_free(data->new_ap_settings);
+       dh5_free(data->dh_ctx);
        os_free(data);
 }
 
@@ -197,19 +207,19 @@ int wps_is_selected_pbc_registrar(const struct wpabuf *msg)
            WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
                return 0;
 
+#ifdef CONFIG_WPS_STRICT
+       if (!attr.sel_reg_config_methods ||
+           !(WPA_GET_BE16(attr.sel_reg_config_methods) &
+             WPS_CONFIG_PUSHBUTTON))
+               return 0;
+#endif /* CONFIG_WPS_STRICT */
+
        return 1;
 }
 
 
-/**
- * wps_is_selected_pin_registrar - Check whether WPS IE indicates active PIN
- * @msg: WPS IE contents from Beacon or Probe Response frame
- * Returns: 1 if PIN Registrar is active, 0 if not
- */
-int wps_is_selected_pin_registrar(const struct wpabuf *msg)
+static int is_selected_pin_registrar(struct wps_parse_attr *attr)
 {
-       struct wps_parse_attr attr;
-
        /*
         * In theory, this could also verify that attr.sel_reg_config_methods
         * includes WPS_CONFIG_LABEL, WPS_CONFIG_DISPLAY, or WPS_CONFIG_KEYPAD,
@@ -218,21 +228,112 @@ int wps_is_selected_pin_registrar(const struct wpabuf *msg)
         * Device Password ID here.
         */
 
-       if (wps_parse_msg(msg, &attr) < 0)
+       if (!attr->selected_registrar || *attr->selected_registrar == 0)
                return 0;
 
-       if (!attr.selected_registrar || *attr.selected_registrar == 0)
+       if (attr->dev_password_id != NULL &&
+           WPA_GET_BE16(attr->dev_password_id) == DEV_PW_PUSHBUTTON)
                return 0;
 
-       if (attr.dev_password_id != NULL &&
-           WPA_GET_BE16(attr.dev_password_id) == DEV_PW_PUSHBUTTON)
+#ifdef CONFIG_WPS_STRICT
+       if (!attr->sel_reg_config_methods ||
+           !(WPA_GET_BE16(attr->sel_reg_config_methods) &
+             (WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD)))
                return 0;
+#endif /* CONFIG_WPS_STRICT */
 
        return 1;
 }
 
 
 /**
+ * wps_is_selected_pin_registrar - Check whether WPS IE indicates active PIN
+ * @msg: WPS IE contents from Beacon or Probe Response frame
+ * Returns: 1 if PIN Registrar is active, 0 if not
+ */
+int wps_is_selected_pin_registrar(const struct wpabuf *msg)
+{
+       struct wps_parse_attr attr;
+
+       if (wps_parse_msg(msg, &attr) < 0)
+               return 0;
+
+       return is_selected_pin_registrar(&attr);
+}
+
+
+/**
+ * wps_is_addr_authorized - Check whether WPS IE authorizes MAC address
+ * @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
+ */
+int wps_is_addr_authorized(const struct wpabuf *msg, const u8 *addr,
+                          int ver1_compat)
+{
+       struct wps_parse_attr attr;
+       unsigned int i;
+       const u8 *pos;
+       const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
+
+       if (wps_parse_msg(msg, &attr) < 0)
+               return 0;
+
+       if (!attr.version2 && ver1_compat) {
+               /*
+                * Version 1.0 AP - AuthorizedMACs not used, so revert back to
+                * old mechanism of using SelectedRegistrar.
+                */
+               return is_selected_pin_registrar(&attr);
+       }
+
+       if (!attr.authorized_macs)
+               return 0;
+
+       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)
+                       return 1;
+               pos += ETH_ALEN;
+       }
+
+       return 0;
+}
+
+
+/**
+ * wps_ap_priority_compar - Prioritize WPS IE from two APs
+ * @wps_a: WPS IE contents from Beacon or Probe Response frame
+ * @wps_b: WPS IE contents from Beacon or Probe Response frame
+ * Returns: 1 if wps_b is considered more likely selection for WPS
+ * provisioning, -1 if wps_a is considered more like, or 0 if no preference
+ */
+int wps_ap_priority_compar(const struct wpabuf *wps_a,
+                          const struct wpabuf *wps_b)
+{
+       struct wps_parse_attr attr_a, attr_b;
+       int sel_a, sel_b;
+
+       if (wps_a == NULL || wps_parse_msg(wps_a, &attr_a) < 0)
+               return 1;
+       if (wps_b == NULL || wps_parse_msg(wps_b, &attr_b) < 0)
+               return -1;
+
+       sel_a = attr_a.selected_registrar && *attr_a.selected_registrar != 0;
+       sel_b = attr_b.selected_registrar && *attr_b.selected_registrar != 0;
+
+       if (sel_a && !sel_b)
+               return -1;
+       if (!sel_a && sel_b)
+               return 1;
+
+       return 0;
+}
+
+
+/**
  * wps_get_uuid_e - Get UUID-E from WPS IE
  * @msg: WPS IE contents from Beacon or Probe Response frame
  * Returns: Pointer to UUID-E or %NULL if not included
@@ -273,7 +374,42 @@ struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type)
        wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
 
        if (wps_build_version(ie) ||
-           wps_build_req_type(ie, req_type)) {
+           wps_build_req_type(ie, req_type) ||
+           wps_build_wfa_ext(ie, 0, NULL, 0)) {
+               wpabuf_free(ie);
+               return NULL;
+       }
+
+       *len = wpabuf_len(ie) - 2;
+
+       return ie;
+}
+
+
+/**
+ * wps_build_assoc_resp_ie - Build WPS IE for (Re)Association Response
+ * Returns: WPS IE or %NULL on failure
+ *
+ * The caller is responsible for freeing the buffer.
+ */
+struct wpabuf * wps_build_assoc_resp_ie(void)
+{
+       struct wpabuf *ie;
+       u8 *len;
+
+       wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
+                  "Response");
+       ie = wpabuf_alloc(100);
+       if (ie == NULL)
+               return NULL;
+
+       wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
+       len = wpabuf_put(ie, 1);
+       wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
+
+       if (wps_build_version(ie) ||
+           wps_build_resp_type(ie, WPS_RESP_AP) ||
+           wps_build_wfa_ext(ie, 0, NULL, 0)) {
                wpabuf_free(ie);
                return NULL;
        }
@@ -299,23 +435,34 @@ struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
                                       enum wps_request_type req_type)
 {
        struct wpabuf *ie;
-       u8 *len;
        u16 methods;
 
        wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
 
-       ie = wpabuf_alloc(200);
+       ie = wpabuf_alloc(500);
        if (ie == NULL)
                return NULL;
 
-       wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
-       len = wpabuf_put(ie, 1);
-       wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
-
-       if (pbc)
+       if (pbc) {
                methods = WPS_CONFIG_PUSHBUTTON;
-       else {
+#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;
@@ -334,14 +481,29 @@ struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
            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)) {
+                                     DEV_PW_DEFAULT) ||
+#ifdef CONFIG_WPS2
+           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 */
+               ) {
                wpabuf_free(ie);
                return NULL;
        }
 
-       *len = wpabuf_len(ie) - 2;
+#ifndef CONFIG_WPS2
+       if (dev->p2p && wps_build_dev_name(dev, ie)) {
+               wpabuf_free(ie);
+               return NULL;
+       }
+#endif /* CONFIG_WPS2 */
 
-       return ie;
+       return wps_ie_encapsulate(ie);
 }
 
 
@@ -417,11 +579,12 @@ int wps_attr_text(struct wpabuf *data, char *buf, char *end)
        }
 
        if (attr.primary_dev_type) {
+               char devtype[WPS_DEV_TYPE_BUFSIZE];
                ret = os_snprintf(pos, end - pos,
-                                 "wps_primary_device_type=%u-%08x-%u\n",
-                                 WPA_GET_BE16(attr.primary_dev_type),
-                                 WPA_GET_BE32(&attr.primary_dev_type[2]),
-                                 WPA_GET_BE16(&attr.primary_dev_type[6]));
+                                 "wps_primary_device_type=%s\n",
+                                 wps_dev_type_bin2str(attr.primary_dev_type,
+                                                      devtype,
+                                                      sizeof(devtype)));
                if (ret < 0 || ret >= end - pos)
                        return pos - buf;
                pos += ret;