WPS ER: Add preliminary PBC support
[libeap.git] / wpa_supplicant / wps_supplicant.c
index a4efc6e..a2193d1 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "common.h"
 #include "ieee802_11_defs.h"
+#include "ieee802_11_common.h"
 #include "wpa_common.h"
 #include "config.h"
 #include "eap_peer/eap.h"
 #include "eloop.h"
 #include "uuid.h"
 #include "wpa_ctrl.h"
-#include "ctrl_iface_dbus.h"
+#include "notify.h"
 #include "eap_common/eap_wsc_common.h"
 #include "blacklist.h"
 #include "wpa.h"
 #include "wps_supplicant.h"
-#include "dh_groups.h"
+#include "dh_group5.h"
 
 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
 
@@ -188,6 +189,7 @@ static int wpa_supplicant_wps_cred(void *ctx,
        struct wpa_supplicant *wpa_s = ctx;
        struct wpa_ssid *ssid = wpa_s->current_ssid;
        u8 key_idx = 0;
+       u16 auth_type;
 
        if ((wpa_s->conf->wps_cred_processing == 1 ||
             wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
@@ -200,7 +202,8 @@ static int wpa_supplicant_wps_cred(void *ctx,
                                WPS_EVENT_CRED_RECEIVED, buf);
                        os_free(buf);
                }
-               wpa_supplicant_dbus_notify_wps_cred(wpa_s, cred);
+
+               wpas_notify_wps_credential(wpa_s, cred);
        } else
                wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
 
@@ -220,13 +223,20 @@ static int wpa_supplicant_wps_cred(void *ctx,
        wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
                   MAC2STR(cred->mac_addr));
 
-       if (cred->auth_type != WPS_AUTH_OPEN &&
-           cred->auth_type != WPS_AUTH_SHARED &&
-           cred->auth_type != WPS_AUTH_WPAPSK &&
-           cred->auth_type != WPS_AUTH_WPA2PSK) {
+       auth_type = cred->auth_type;
+       if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
+               wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
+                          "auth_type into WPA2PSK");
+               auth_type = WPS_AUTH_WPA2PSK;
+       }
+
+       if (auth_type != WPS_AUTH_OPEN &&
+           auth_type != WPS_AUTH_SHARED &&
+           auth_type != WPS_AUTH_WPAPSK &&
+           auth_type != WPS_AUTH_WPA2PSK) {
                wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
-                          "unsupported authentication type %d",
-                          cred->auth_type);
+                          "unsupported authentication type 0x%x",
+                          auth_type);
                return 0;
        }
 
@@ -246,6 +256,7 @@ static int wpa_supplicant_wps_cred(void *ctx,
                ssid = wpa_config_add_network(wpa_s->conf);
                if (ssid == NULL)
                        return -1;
+               wpas_notify_network_added(wpa_s, ssid);
        }
 
        wpa_config_set_network_defaults(ssid);
@@ -300,7 +311,7 @@ static int wpa_supplicant_wps_cred(void *ctx,
                break;
        }
 
-       switch (cred->auth_type) {
+       switch (auth_type) {
        case WPS_AUTH_OPEN:
                ssid->auth_alg = WPA_AUTH_ALG_OPEN;
                ssid->key_mgmt = WPA_KEY_MGMT_NONE;
@@ -378,6 +389,7 @@ static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
        wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
                "dev_password_id=%d config_error=%d",
                m2d->dev_password_id, m2d->config_error);
+       wpas_notify_wps_event_m2d(wpa_s, m2d);
 }
 
 
@@ -386,6 +398,7 @@ static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
 {
        wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
        wpas_clear_wps(wpa_s);
+       wpas_notify_wps_event_fail(wpa_s, fail);
 }
 
 
@@ -393,6 +406,70 @@ static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
 {
        wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
        wpa_s->wps_success = 1;
+       wpas_notify_wps_event_success(wpa_s);
+}
+
+
+static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
+                                              struct wps_event_er_ap *ap)
+{
+       char uuid_str[100];
+       uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
+       wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s|%s|%s|%s|%s|%s|%s",
+               uuid_str,
+               ap->friendly_name ? ap->friendly_name : "",
+               ap->manufacturer ? ap->manufacturer : "",
+               ap->model_description ? ap->model_description : "",
+               ap->model_name ? ap->model_name : "",
+               ap->manufacturer_url ? ap->manufacturer_url : "",
+               ap->model_url ? ap->model_url : "");
+}
+
+
+static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
+                                                 struct wps_event_er_ap *ap)
+{
+       char uuid_str[100];
+       uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
+       wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
+}
+
+
+static void wpa_supplicant_wps_event_er_enrollee_add(
+       struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
+{
+       char uuid_str[100];
+       char dev_type[20];
+
+       uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
+       if (enrollee->pri_dev_type)
+               os_snprintf(dev_type, sizeof(dev_type), "%u-%08X-%u",
+                           WPA_GET_BE16(enrollee->pri_dev_type),
+                           WPA_GET_BE32(enrollee->pri_dev_type + 2),
+                           WPA_GET_BE16(enrollee->pri_dev_type + 6));
+       else
+               dev_type[0] = '\0';
+
+       wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
+               " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
+               "|%s|%s|%s|%s|%s|",
+               uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
+               enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
+               enrollee->dev_name ? enrollee->dev_name : "",
+               enrollee->manufacturer ? enrollee->manufacturer : "",
+               enrollee->model_name ? enrollee->model_name : "",
+               enrollee->model_number ? enrollee->model_number : "",
+               enrollee->serial_number ? enrollee->serial_number : "");
+}
+
+
+static void wpa_supplicant_wps_event_er_enrollee_remove(
+       struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
+{
+       char uuid_str[100];
+       uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
+       wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
+               uuid_str, MAC2STR(enrollee->mac_addr));
 }
 
 
@@ -412,6 +489,24 @@ static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
                break;
        case WPS_EV_PWD_AUTH_FAIL:
                break;
+       case WPS_EV_PBC_OVERLAP:
+               break;
+       case WPS_EV_PBC_TIMEOUT:
+               break;
+       case WPS_EV_ER_AP_ADD:
+               wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
+               break;
+       case WPS_EV_ER_AP_REMOVE:
+               wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
+               break;
+       case WPS_EV_ER_ENROLLEE_ADD:
+               wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
+                                                        &data->enrollee);
+               break;
+       case WPS_EV_ER_ENROLLEE_REMOVE:
+               wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
+                                                           &data->enrollee);
+               break;
        }
 }
 
@@ -429,7 +524,7 @@ enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
 {
        int id;
-       struct wpa_ssid *ssid;
+       struct wpa_ssid *ssid, *remove_ssid = NULL;
 
        eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
 
@@ -437,14 +532,20 @@ static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
        ssid = wpa_s->conf->ssid;
        while (ssid) {
                if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
-                       if (ssid == wpa_s->current_ssid)
+                       if (ssid == wpa_s->current_ssid) {
                                wpa_s->current_ssid = NULL;
+                               if (ssid != NULL)
+                                       wpas_notify_network_changed(wpa_s);
+                       }
                        id = ssid->id;
+                       remove_ssid = ssid;
                } else
                        id = -1;
                ssid = ssid->next;
-               if (id >= 0)
+               if (id >= 0) {
+                       wpas_notify_network_removed(wpa_s, remove_ssid);
                        wpa_config_remove_network(wpa_s->conf, id);
+               }
        }
 }
 
@@ -466,12 +567,14 @@ static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
        ssid = wpa_config_add_network(wpa_s->conf);
        if (ssid == NULL)
                return NULL;
+       wpas_notify_network_added(wpa_s, ssid);
        wpa_config_set_network_defaults(ssid);
        if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
            wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
            wpa_config_set(ssid, "identity", registrar ?
                           "\"" WSC_ID_REGISTRAR "\"" :
                           "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
+               wpas_notify_network_removed(wpa_s, ssid);
                wpa_config_remove_network(wpa_s->conf, ssid->id);
                return NULL;
        }
@@ -520,7 +623,10 @@ static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
        /* Mark all other networks disabled and trigger reassociation */
        ssid = wpa_s->conf->ssid;
        while (ssid) {
+               int was_disabled = ssid->disabled;
                ssid->disabled = ssid != selected;
+               if (was_disabled != ssid->disabled)
+                       wpas_notify_network_enabled_changed(wpa_s, ssid);
                ssid = ssid->next;
        }
        wpa_s->disconnected = 0;
@@ -594,10 +700,11 @@ int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
                wpabuf_free(wps->dh_pubkey);
                wpabuf_free(wps->dh_privkey);
                wps->dh_privkey = NULL;
-               wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
-                                        &wps->dh_privkey);
+               wps->dh_pubkey = NULL;
+               dh5_free(wps->dh_ctx);
+               wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
                wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
-               if (wps->dh_pubkey == NULL) {
+               if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
                        wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
                                   "Diffie-Hellman handshake");
                        return -1;
@@ -622,10 +729,12 @@ int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
 
 
 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
-                      const char *pin)
+                      const char *pin, struct wps_new_ap_settings *settings)
 {
        struct wpa_ssid *ssid;
-       char val[30];
+       char val[200];
+       char *pos, *end;
+       int res;
 
        if (!pin)
                return -1;
@@ -633,7 +742,24 @@ int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
        ssid = wpas_wps_add_network(wpa_s, 1, bssid);
        if (ssid == NULL)
                return -1;
-       os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
+       pos = val;
+       end = pos + sizeof(val);
+       res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
+       if (res < 0 || res >= end - pos)
+               return -1;
+       pos += res;
+       if (settings) {
+               res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
+                                 "new_encr=%s new_key=%s",
+                                 settings->ssid_hex, settings->auth,
+                                 settings->encr, settings->key_hex);
+               if (res < 0 || res >= end - pos)
+                       return -1;
+               pos += res;
+       }
+       res = os_snprintf(pos, end - pos, "\"");
+       if (res < 0 || res >= end - pos)
+               return -1;
        wpa_config_set(ssid, "phase1", val, 0);
        eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
                               wpa_s, NULL);
@@ -674,6 +800,20 @@ static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
 }
 
 
+static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
+                                   u16 sel_reg_config_methods)
+{
+#ifdef CONFIG_WPS_ER
+       struct wpa_supplicant *wpa_s = ctx;
+
+       if (wpa_s->wps_er == NULL)
+               return;
+       wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
+                          sel_reg_config_methods);
+#endif /* CONFIG_WPS_ER */
+}
+
+
 int wpas_wps_init(struct wpa_supplicant *wpa_s)
 {
        struct wps_context *wps;
@@ -735,6 +875,7 @@ int wpas_wps_init(struct wpa_supplicant *wpa_s)
        os_memset(&rcfg, 0, sizeof(rcfg));
        rcfg.new_psk_cb = wpas_wps_new_psk_cb;
        rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
+       rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
        rcfg.cb_ctx = wpa_s;
 
        wps->registrar = wps_registrar_init(wps, &rcfg);
@@ -765,6 +906,11 @@ void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
        os_free(wpa_s->wps->network_key);
        os_free(wpa_s->wps);
        wpa_s->wps = NULL;
+
+#ifdef CONFIG_WPS_ER
+       wps_er_deinit(wpa_s->wps_er);
+       wpa_s->wps_er = NULL;
+#endif /* CONFIG_WPS_ER */
 }
 
 
@@ -956,3 +1102,73 @@ int wpas_wps_searching(struct wpa_supplicant *wpa_s)
 
        return 0;
 }
+
+
+int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
+                             char *end)
+{
+       struct wpabuf *wps_ie;
+       int ret;
+
+       wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
+       if (wps_ie == NULL)
+               return 0;
+
+       ret = wps_attr_text(wps_ie, buf, end);
+       wpabuf_free(wps_ie);
+       return ret;
+}
+
+
+int wpas_wps_er_start(struct wpa_supplicant *wpa_s)
+{
+#ifdef CONFIG_WPS_ER
+       if (wpa_s->wps_er) {
+               /* TODO: re-send ctrl_iface events for current data? */
+               return 0;
+       }
+       wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname);
+       if (wpa_s->wps_er == NULL)
+               return -1;
+       return 0;
+#else /* CONFIG_WPS_ER */
+       return 0;
+#endif /* CONFIG_WPS_ER */
+}
+
+
+int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
+{
+#ifdef CONFIG_WPS_ER
+       wps_er_deinit(wpa_s->wps_er);
+       wpa_s->wps_er = NULL;
+#endif /* CONFIG_WPS_ER */
+       return 0;
+}
+
+
+#ifdef CONFIG_WPS_ER
+int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const char *uuid,
+                       const char *pin)
+{
+       u8 u[UUID_LEN];
+       int any = 0;
+
+       if (os_strcmp(uuid, "any") == 0)
+               any = 1;
+       else if (uuid_str2bin(uuid, u))
+               return -1;
+       return wps_registrar_add_pin(wpa_s->wps->registrar, any ? NULL : u,
+                                    (const u8 *) pin, os_strlen(pin), 300);
+}
+
+
+int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
+{
+       u8 u[UUID_LEN];
+
+       if (uuid_str2bin(uuid, u))
+               return -1;
+       return wps_er_pbc(wpa_s->wps_er, u);
+}
+#endif /* CONFIG_WPS_ER */