From 3a014de5b91bef2cbc26869cc5cd6f7988aae6c5 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Thu, 16 Sep 2010 12:34:48 +0200 Subject: [PATCH 5/5] WEP and WPA-PSK support for GSupplicant --- gsupplicant/gsupplicant.h | 2 +- gsupplicant/supplicant.c | 68 +++++++++++++++++++++++++++++++++++++++++++++ plugins/wifi.c | 2 + 3 files changed, 71 insertions(+), 1 deletions(-) diff --git a/gsupplicant/gsupplicant.h b/gsupplicant/gsupplicant.h index 481d0a3..8461c49 100644 --- a/gsupplicant/gsupplicant.h +++ b/gsupplicant/gsupplicant.h @@ -103,7 +103,7 @@ struct _GSupplicantSSID { GSupplicantMode mode; GSupplicantSecurity security; unsigned int eap_method; - char *passphrase; + const char *passphrase; char *identity; char *ca_cert_path; char *client_cert_path; diff --git a/gsupplicant/supplicant.c b/gsupplicant/supplicant.c index 4850b63..c1679b5 100644 --- a/gsupplicant/supplicant.c +++ b/gsupplicant/supplicant.c @@ -24,6 +24,7 @@ #endif #include +#include #include #include #include @@ -1997,6 +1998,71 @@ done: dbus_free(data); } +static void add_network_security_wep(DBusMessageIter *dict, + GSupplicantSSID *ssid) +{ + const char *auth_alg = "OPEN SHARED"; + const char *key_index = "0"; + + supplicant_dbus_dict_append_basic(dict, "auth_alg", + DBUS_TYPE_STRING, &auth_alg); + + if (ssid->passphrase) { + int size = strlen(ssid->passphrase); + if (size == 10 || size == 26) { + unsigned char *key = g_try_malloc(13); + char tmp[3]; + int i; + + memset(tmp, 0, sizeof(tmp)); + if (key == NULL) + size = 0; + + for (i = 0; i < size / 2; i++) { + memcpy(tmp, ssid->passphrase + (i * 2), 2); + key[i] = (unsigned char) strtol(tmp, NULL, 16); + } + + supplicant_dbus_dict_append_fixed_array(dict, + "wep_key0", + DBUS_TYPE_BYTE, + &key, size / 2); + g_free(key); + } else if (size == 5 || size == 13) { + unsigned char *key = g_try_malloc(13); + int i; + + if (key == NULL) + size = 0; + + for (i = 0; i < size; i++) + key[i] = (unsigned char) ssid->passphrase[i]; + + supplicant_dbus_dict_append_fixed_array(dict, + "wep_key0", + DBUS_TYPE_BYTE, + &key, size); + g_free(key); + } else + supplicant_dbus_dict_append_basic(dict, + "wep_key0", + DBUS_TYPE_STRING, + &ssid->passphrase); + + supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx", + DBUS_TYPE_STRING, &key_index); + } +} + +static void add_network_security_psk(DBusMessageIter *dict, + GSupplicantSSID *ssid) +{ + if (ssid->passphrase && strlen(ssid->passphrase) > 0) + supplicant_dbus_dict_append_basic(dict, "psk", + DBUS_TYPE_STRING, + &ssid->passphrase); +} + static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid) { char *key_mgmt; @@ -2006,9 +2072,11 @@ static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid) case G_SUPPLICANT_SECURITY_NONE: case G_SUPPLICANT_SECURITY_WEP: key_mgmt = "NONE"; + add_network_security_wep(dict, ssid); break; case G_SUPPLICANT_SECURITY_PSK: key_mgmt = "WPA-PSK"; + add_network_security_psk(dict, ssid); break; case G_SUPPLICANT_SECURITY_IEEE8021X: key_mgmt = "WPA-EAP"; diff --git a/plugins/wifi.c b/plugins/wifi.c index 67b0323..4cde53e 100644 --- a/plugins/wifi.c +++ b/plugins/wifi.c @@ -562,6 +562,8 @@ static int network_connect(struct connman_network *network) &ssid.ssid_len); security = connman_network_get_string(network, "WiFi.Security"); ssid.security = network_security(security); + ssid.passphrase = connman_network_get_string(network, + "WiFi.Passphrase"); wifi->network = connman_network_ref(network); -- 1.7.1