[network-manager-netbook/MplPanelClient] let NM handle WEP & WPA passphrases (bgo #513820) (rh #441070)



commit e200655c61bcbb1640927c09cb994c7b4ef21109
Author: Dan Williams <dcbw redhat com>
Date:   Tue Sep 29 17:09:32 2009 -0700

    let NM handle WEP & WPA passphrases (bgo #513820) (rh #441070)
    
    requires the following commits:
    
    f3c9887472ef6d773aeabd0bb7fcf102cf725398 (0.7.x)
    07cc26d5fc3df0ed47b4bb993ce9a0d4f0008876 (master)
    
    (port of nm-applet commit d9c45bfcf9db522c1724097341016cfca4e2fb78)

 src/wireless-dialog.c              |   16 +-
 src/wireless-security/Makefile.am  |    2 -
 src/wireless-security/sha1.c       |  692 ------------------------------------
 src/wireless-security/sha1.h       |   34 --
 src/wireless-security/ws-wep-key.c |  199 +----------
 src/wireless-security/ws-wep-key.h |   11 +-
 src/wireless-security/ws-wpa-psk.c |   21 +-
 7 files changed, 30 insertions(+), 945 deletions(-)
---
diff --git a/src/wireless-dialog.c b/src/wireless-dialog.c
index aa860ac..b4e0963 100644
--- a/src/wireless-dialog.c
+++ b/src/wireless-dialog.c
@@ -593,7 +593,7 @@ security_combo_init (NMAWirelessDialog *self)
     guint32 dev_caps;
     NMSettingWirelessSecurity *wsec = NULL;
     NMUtilsSecurityType default_type = NMU_SEC_NONE;
-    WEPKeyType default_wep_type = WEP_KEY_TYPE_KEY;
+    NMWepKeyType wep_type = NM_WEP_KEY_TYPE_KEY;
     int active = -1;
     int item = 0;
     NMSettingWireless *s_wireless = NULL;
@@ -638,11 +638,13 @@ security_combo_init (NMAWirelessDialog *self)
         if (wsec) {
             default_type = get_default_type_for_security (wsec, !!priv->ap, ap_flags, dev_caps);
             if (default_type == NMU_SEC_STATIC_WEP)
-                default_wep_type = ws_wep_guess_key_type (priv->connection);
+                wep_type = nm_setting_wireless_security_get_wep_key_type (wsec);
+            if (wep_type == NM_WEP_KEY_TYPE_UNKNOWN)
+                wep_type = NM_WEP_KEY_TYPE_KEY;
         }
     } else if (is_adhoc) {
         default_type = NMU_SEC_STATIC_WEP;
-        default_wep_type = WEP_KEY_TYPE_PASSPHRASE;
+        wep_type = NM_WEP_KEY_TYPE_PASSPHRASE;
     }
 
     sec_model = gtk_list_store_new (2, G_TYPE_STRING, wireless_security_get_g_type ());
@@ -664,20 +666,20 @@ security_combo_init (NMAWirelessDialog *self)
            && ((!ap_wpa && !ap_rsn) || !(dev_caps & (NM_WIFI_DEVICE_CAP_WPA | NM_WIFI_DEVICE_CAP_RSN)))) {
         WirelessSecurityWEPKey *ws_wep;
 
-        ws_wep = ws_wep_key_new (priv->connection, WEP_KEY_TYPE_KEY, priv->adhoc_create);
+        ws_wep = ws_wep_key_new (priv->connection, NM_WEP_KEY_TYPE_KEY, priv->adhoc_create);
         if (ws_wep) {
             add_security_item (self, WIRELESS_SECURITY (ws_wep), sec_model,
                                &iter, _("WEP 40/128-bit Key"));
-            if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (default_wep_type == WEP_KEY_TYPE_KEY))
+            if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (wep_type == NM_WEP_KEY_TYPE_KEY))
                 active = item;
             item++;
         }
 
-        ws_wep = ws_wep_key_new (priv->connection, WEP_KEY_TYPE_PASSPHRASE, priv->adhoc_create);
+        ws_wep = ws_wep_key_new (priv->connection, NM_WEP_KEY_TYPE_PASSPHRASE, priv->adhoc_create);
         if (ws_wep) {
             add_security_item (self, WIRELESS_SECURITY (ws_wep), sec_model,
                                &iter, _("WEP 128-bit Passphrase"));
-            if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (default_wep_type == WEP_KEY_TYPE_PASSPHRASE))
+            if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (wep_type == NM_WEP_KEY_TYPE_PASSPHRASE))
                 active = item;
             item++;
         }
diff --git a/src/wireless-security/Makefile.am b/src/wireless-security/Makefile.am
index ad019d7..9c49a32 100644
--- a/src/wireless-security/Makefile.am
+++ b/src/wireless-security/Makefile.am
@@ -7,8 +7,6 @@ libwireless_security_la_SOURCES = \
 	ws-wep-key.c \
 	ws-wpa-psk.h \
 	ws-wpa-psk.c \
-	sha1.c \
-	sha1.h \
 	ws-leap.h \
 	ws-leap.c \
 	ws-wpa-eap.h \
diff --git a/src/wireless-security/ws-wep-key.c b/src/wireless-security/ws-wep-key.c
index 15cbdf0..512f55f 100644
--- a/src/wireless-security/ws-wep-key.c
+++ b/src/wireless-security/ws-wep-key.c
@@ -97,7 +97,7 @@ validate (WirelessSecurity *parent, const GByteArray *ssid)
 	if (!key)
 		return FALSE;
 
-	if (sec->type == WEP_KEY_TYPE_KEY) {
+	if (sec->type == NM_WEP_KEY_TYPE_KEY) {
 		if ((strlen (key) == 10) || (strlen (key) == 26)) {
 			for (i = 0; i < strlen (key); i++) {
 				if (!isxdigit (key[i]))
@@ -111,7 +111,7 @@ validate (WirelessSecurity *parent, const GByteArray *ssid)
 		} else {
 			return FALSE;
 		}
-	} else if (sec->type == WEP_KEY_TYPE_PASSPHRASE) {
+	} else if (sec->type == NM_WEP_KEY_TYPE_PASSPHRASE) {
 		if (!strlen (key) || (strlen (key) > 64))
 			return FALSE;
 	}
@@ -134,41 +134,12 @@ add_to_size_group (WirelessSecurity *parent, GtkSizeGroup *group)
 	gtk_size_group_add_widget (group, widget);
 }
 
-static char *
-wep128_passphrase_hash (const char *input)
-{
-	char *result;
-	char md5_data[65];
-	int input_len;
-	int i;
-
-	g_return_val_if_fail (input != NULL, NULL);
-
-	input_len = strlen (input);
-	if (input_len < 1)
-		return NULL;
-
-	/* Get at least 64 bytes */
-	for (i = 0; i < 64; i++)
-		md5_data [i] = input [i % input_len];
-
-	/* Null terminate md5 seed data and hash it */
-	md5_data[64] = 0;
-	result = g_compute_checksum_for_string (G_CHECKSUM_MD5, md5_data, 64);
-
-	/* Cut converted key off at the correct length for this cipher type */
-	if (result)
-	  result[26] = '\0';
-
-	return result;
-}
-
 static void
 fill_connection (WirelessSecurity *parent, NMConnection *connection)
 {
 	WirelessSecurityWEPKey *sec = (WirelessSecurityWEPKey *) parent;
 	NMSettingWireless *s_wireless;
-	NMSettingWirelessSecurity *s_wireless_sec;
+	NMSettingWirelessSecurity *s_wsec;
 	GtkWidget *widget;
 	gint auth_alg;
 	const char *key;
@@ -188,31 +159,18 @@ fill_connection (WirelessSecurity *parent, NMConnection *connection)
 	g_object_set (s_wireless, NM_SETTING_WIRELESS_SEC, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, NULL);
 
 	/* Blow away the old security setting by adding a clear one */
-	s_wireless_sec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();
-	nm_connection_add_setting (connection, (NMSetting *) s_wireless_sec);
+	s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();
+	nm_connection_add_setting (connection, (NMSetting *) s_wsec);
 
-	g_object_set (s_wireless_sec,
+	g_object_set (s_wsec,
 	              NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "none",
 	              NM_SETTING_WIRELESS_SECURITY_WEP_TX_KEYIDX, sec->cur_index,
 	              NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, (auth_alg == 1) ? "shared" : "open",
 	              NULL);
 
 	for (i = 0; i < 4; i++) {
-		int key_len = strlen (sec->keys[i]);
-
-		if (!key_len)
-			continue;
-
-		if (sec->type == WEP_KEY_TYPE_KEY) {
-			if ((key_len == 10) || (key_len == 26))
-				hashed = g_strdup (sec->keys[i]);
-			else if ((key_len == 5) || (key_len == 13))
-				hashed = utils_bin2hexstr (sec->keys[i], key_len, key_len * 2);
-		} else if (sec->type == WEP_KEY_TYPE_PASSPHRASE)
-			hashed = wep128_passphrase_hash (sec->keys[i]);
-
-		nm_setting_wireless_security_set_wep_key (s_wireless_sec, i, hashed);
-		g_free (hashed);
+		if (strlen (sec->keys[i]))
+			nm_setting_wireless_security_set_wep_key (s_wsec, i, sec->keys[i]);
 	}
 }
 
@@ -228,12 +186,12 @@ wep_entry_filter_cb (GtkEntry *   entry,
 	int i, count = 0;
 	gchar *result = g_new (gchar, length);
 
-	if (sec->type == WEP_KEY_TYPE_KEY) {
+	if (sec->type == NM_WEP_KEY_TYPE_KEY) {
 		for (i = 0; i < length; i++) {
 			if (isxdigit(text[i]) || isascii(text[i]))
 				result[count++] = text[i];
 		}
-	} else if (sec->type == WEP_KEY_TYPE_PASSPHRASE) {
+	} else if (sec->type == NM_WEP_KEY_TYPE_PASSPHRASE) {
 		for (i = 0; i < length; i++)
 			result[count++] = text[i];
 	}
@@ -258,9 +216,6 @@ static void
 fill_secrets (WirelessSecurityWEPKey *sec, NMConnection *connection)
 {
 	NMSettingWirelessSecurity *s_wsec;
-	GHashTable *secrets;
-	GError *error = NULL;
-	GValue *value;
 	const char *tmp;
 	int i;
 
@@ -268,46 +223,16 @@ fill_secrets (WirelessSecurityWEPKey *sec, NMConnection *connection)
 	g_return_if_fail (connection != NULL);
 
 	s_wsec = (NMSettingWirelessSecurity *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
-
-	if (nm_connection_get_scope (connection) == NM_CONNECTION_SCOPE_SYSTEM) {
-		for (i = 0; s_wsec && i < 4; i++) {
-			tmp = nm_setting_wireless_security_get_wep_key (s_wsec, i);
-			if (tmp)
-				strcpy (sec->keys[i], tmp);
-		}
-	} else {
-		secrets = nm_gconf_get_keyring_items (connection,
-		                                      NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
-		                                      FALSE,
-		                                      &error);
-		if (!secrets) {
-			g_clear_error (&error);
-			return;
-		}
-
-		value = g_hash_table_lookup (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0);
-		if (value)
-			strcpy (sec->keys[0], g_value_get_string (value));
-
-		value = g_hash_table_lookup (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY1);
-		if (value)
-			strcpy (sec->keys[1], g_value_get_string (value));
-
-		value = g_hash_table_lookup (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY2);
-		if (value)
-			strcpy (sec->keys[2], g_value_get_string (value));
-
-		value = g_hash_table_lookup (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY3);
-		if (value)
-			strcpy (sec->keys[3], g_value_get_string (value));
-
-		g_hash_table_destroy (secrets);
+	for (i = 0; s_wsec && i < 4; i++) {
+		tmp = nm_setting_wireless_security_get_wep_key (s_wsec, i);
+		if (tmp)
+			strcpy (sec->keys[i], tmp);
 	}
 }
 
 WirelessSecurityWEPKey *
 ws_wep_key_new (NMConnection *connection,
-                WEPKeyType type,
+                NMWepKeyType type,
                 gboolean adhoc_create)
 {
 	WirelessSecurityWEPKey *sec;
@@ -358,9 +283,9 @@ ws_wep_key_new (NMConnection *connection,
 	g_signal_connect (G_OBJECT (widget), "insert-text",
 	                  (GCallback) wep_entry_filter_cb,
 	                  sec);
-	if (sec->type == WEP_KEY_TYPE_KEY)
+	if (sec->type == NM_WEP_KEY_TYPE_KEY)
 		gtk_entry_set_max_length (GTK_ENTRY (widget), 26);
-	else if (sec->type == WEP_KEY_TYPE_PASSPHRASE)
+	else if (sec->type == NM_WEP_KEY_TYPE_PASSPHRASE)
 		gtk_entry_set_max_length (GTK_ENTRY (widget), 64);
 
 	widget = GTK_WIDGET (gtk_builder_get_object (builder, "wep_key_index_combo"));
@@ -409,93 +334,3 @@ ws_wep_key_new (NMConnection *connection,
 	return sec;
 }
 
-static WEPKeyType
-guess_type_for_key (const char *key)
-{
-	size_t len = key ? strlen (key) : 0;
-	int i;
-
-	if (!key || !len)
-		return WEP_KEY_TYPE_KEY;
-
-	/* Hex key */
-	if ((len == 10) || (len == 26)) {
-		gboolean hex = TRUE;
-
-		for (i = 0; i < len; i++) {
-			if (!isxdigit(key[i])) {
-				hex = FALSE;
-				break;
-			}
-		}
-		if (hex)
-			return WEP_KEY_TYPE_KEY;
-	}
-
-	/* ASCII key */
-	if ((len == 5) || (len == 13)) {
-		gboolean ascii = TRUE;
-
-		for (i = 0; i < len; i++) {
-			if (!isascii (key[i])) {
-				ascii = FALSE;
-				break;
-			}
-		}
-		if (ascii)
-			return WEP_KEY_TYPE_KEY;
-	}
-
-	return WEP_KEY_TYPE_PASSPHRASE;
-}
-
-WEPKeyType
-ws_wep_guess_key_type (NMConnection *connection)
-{
-	GHashTable *secrets;
-	GError *error = NULL;
-	GValue *value;
-	WEPKeyType key_type = WEP_KEY_TYPE_KEY;
-
-	if (!connection)
-		return WEP_KEY_TYPE_KEY;
-
-	secrets = nm_gconf_get_keyring_items (connection,
-	                                      NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
-	                                      FALSE,
-	                                      &error);
-	if (!secrets || (g_hash_table_size (secrets) == 0)) {
-		if (error)
-			g_error_free (error);
-		return WEP_KEY_TYPE_KEY;
-	}
-
-	value = g_hash_table_lookup (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0);
-	if (value) {
-		key_type = guess_type_for_key (g_value_get_string (value));
-		goto out;
-	}
-
-	value = g_hash_table_lookup (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY1);
-	if (value) {
-		key_type = guess_type_for_key (g_value_get_string (value));
-		goto out;
-	}
-
-	value = g_hash_table_lookup (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY2);
-	if (value) {
-		key_type = guess_type_for_key (g_value_get_string (value));
-		goto out;
-	}
-
-	value = g_hash_table_lookup (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY3);
-	if (value) {
-		key_type = guess_type_for_key (g_value_get_string (value));
-		goto out;
-	}
-
-out:
-	g_hash_table_destroy (secrets);
-	return key_type;
-}
-
diff --git a/src/wireless-security/ws-wep-key.h b/src/wireless-security/ws-wep-key.h
index c1496e4..0b00b4e 100644
--- a/src/wireless-security/ws-wep-key.h
+++ b/src/wireless-security/ws-wep-key.h
@@ -22,24 +22,19 @@
 #ifndef WS_WEP_KEY_H
 #define WS_WEP_KEY_H
 
-typedef enum {
-	WEP_KEY_TYPE_KEY = 0,
-	WEP_KEY_TYPE_PASSPHRASE = 1,
-} WEPKeyType;
+#include <nm-setting-wireless-security.h>
 
 typedef struct {
 	struct _WirelessSecurity parent;
 
-	WEPKeyType type;
+	NMWepKeyType type;
 	char keys[4][65];
 	guint8 cur_index;
 } WirelessSecurityWEPKey;
 
 WirelessSecurityWEPKey * ws_wep_key_new (NMConnection *connection,
-                                         WEPKeyType type,
+                                         NMWepKeyType type,
                                          gboolean adhoc_create);
 
-WEPKeyType ws_wep_guess_key_type (NMConnection *connection);
-
 #endif /* WS_WEP_KEY_H */
 
diff --git a/src/wireless-security/ws-wpa-psk.c b/src/wireless-security/ws-wpa-psk.c
index 9a7edf2..93aab61 100644
--- a/src/wireless-security/ws-wpa-psk.c
+++ b/src/wireless-security/ws-wpa-psk.c
@@ -25,7 +25,6 @@
 
 #include "wireless-security.h"
 #include "utils.h"
-#include "sha1.h"
 #include "gconf-helpers.h"
 #include "helpers.h"
 
@@ -98,8 +97,6 @@ fill_connection (WirelessSecurity *parent, NMConnection *connection)
 {
 	GtkWidget *widget;
 	const char *key;
-	char *hashed = NULL;
-	guint32 len;
 	NMSettingWireless *s_wireless;
 	NMSettingWirelessSecurity *s_wireless_sec;
 	const char *mode;
@@ -120,23 +117,7 @@ fill_connection (WirelessSecurity *parent, NMConnection *connection)
 
 	widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "wpa_psk_entry"));
 	key = gtk_entry_get_text (GTK_ENTRY (widget));
-
-	len = strlen (key);
-	if (len == 64) {
-		/* Hex key */
-		hashed = g_strdup (key);
-	} else {
-		/* passphrase */
-		const GByteArray *ssid = nm_setting_wireless_get_ssid (s_wireless);
-		unsigned char *buf = g_malloc0 (WPA_PMK_LEN * 2);
-
-		pbkdf2_sha1 (key, (char *) ssid->data, ssid->len, 4096, buf, WPA_PMK_LEN);
-		hashed = utils_bin2hexstr ((const char *) buf, WPA_PMK_LEN, WPA_PMK_LEN * 2);
-		g_free (buf);
-	}
-
-	g_object_set (s_wireless_sec, NM_SETTING_WIRELESS_SECURITY_PSK, hashed, NULL);
-	g_free (hashed);
+	g_object_set (s_wireless_sec, NM_SETTING_WIRELESS_SECURITY_PSK, key, NULL);
 
 	wireless_security_clear_ciphers (connection);
 	if (is_adhoc) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]