From bf610255a164824977825aa1057ebba2498f4f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Mon, 4 Feb 2013 11:36:10 +0100 Subject: [PATCH] libnm-util: move verify_wep_key() and verify_wpa_psk() to nm-utils.c and rename them to nm_utils_wep_key_valid(), nm_utils_wpa_psk_valid(). They are general functions and can also be used elsewere usefully. --- libnm-util/libnm-util.ver | 2 + libnm-util/nm-setting-wireless-security.c | 77 +++-------------------- libnm-util/nm-utils.c | 73 +++++++++++++++++++++ libnm-util/nm-utils.h | 3 + 4 files changed, 88 insertions(+), 67 deletions(-) diff --git a/libnm-util/libnm-util.ver b/libnm-util/libnm-util.ver index e40531ea37..04f2a91194 100644 --- a/libnm-util/libnm-util.ver +++ b/libnm-util/libnm-util.ver @@ -551,10 +551,12 @@ global: nm_utils_ssid_to_utf8; nm_utils_uuid_generate; nm_utils_uuid_generate_from_string; + nm_utils_wep_key_valid; nm_utils_wifi_channel_to_freq; nm_utils_wifi_find_next_channel; nm_utils_wifi_freq_to_channel; nm_utils_wifi_is_channel_valid; + nm_utils_wpa_psk_valid; nm_vlan_flags_get_type; nm_vlan_priority_map_get_type; nm_wep_key_type_get_type; diff --git a/libnm-util/nm-setting-wireless-security.c b/libnm-util/nm-setting-wireless-security.c index 404a38ad9d..5dec266f3c 100644 --- a/libnm-util/nm-setting-wireless-security.c +++ b/libnm-util/nm-setting-wireless-security.c @@ -695,63 +695,6 @@ nm_setting_wireless_security_get_wep_key_type (NMSettingWirelessSecurity *settin return NM_SETTING_WIRELESS_SECURITY_GET_PRIVATE (setting)->wep_key_type; } -static gboolean -verify_wep_key (const char *key, NMWepKeyType wep_type) -{ - int keylen, i; - - if (!key) - return FALSE; - - keylen = strlen (key); - if (wep_type == NM_WEP_KEY_TYPE_KEY || NM_WEP_KEY_TYPE_UNKNOWN) { - if (keylen == 10 || keylen == 26) { - /* Hex key */ - for (i = 0; i < keylen; i++) { - if (!g_ascii_isxdigit (key[i])) - return FALSE; - } - } else if (keylen == 5 || keylen == 13) { - /* ASCII key */ - for (i = 0; i < keylen; i++) { - if (!g_ascii_isprint (key[i])) - return FALSE; - } - } else - return FALSE; - - } else if (wep_type == NM_WEP_KEY_TYPE_PASSPHRASE) { - if (!keylen || keylen > 64) - return FALSE; - } - - return TRUE; -} - -static gboolean -verify_wpa_psk (const char *psk) -{ - int psklen, i; - - if (!psk) - return FALSE; - - psklen = strlen (psk); - if (psklen < 8 || psklen > 64) - return FALSE; - - if (psklen == 64) { - /* Hex PSK */ - for (i = 0; i < psklen; i++) { - if (!g_ascii_isxdigit (psk[i])) - return FALSE; - } - } - - return TRUE; -} - - static GPtrArray * need_secrets (NMSetting *setting) { @@ -769,19 +712,19 @@ need_secrets (NMSetting *setting) /* Static WEP */ if (strcmp (priv->key_mgmt, "none") == 0) { - if ((priv->wep_tx_keyidx == 0) && !verify_wep_key (priv->wep_key0, priv->wep_key_type)) { + if ((priv->wep_tx_keyidx == 0) && !nm_utils_wep_key_valid (priv->wep_key0, priv->wep_key_type)) { g_ptr_array_add (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0); return secrets; } - if ((priv->wep_tx_keyidx == 1) && !verify_wep_key (priv->wep_key1, priv->wep_key_type)) { + if ((priv->wep_tx_keyidx == 1) && !nm_utils_wep_key_valid (priv->wep_key1, priv->wep_key_type)) { g_ptr_array_add (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY1); return secrets; } - if ((priv->wep_tx_keyidx == 2) && !verify_wep_key (priv->wep_key2, priv->wep_key_type)) { + if ((priv->wep_tx_keyidx == 2) && !nm_utils_wep_key_valid (priv->wep_key2, priv->wep_key_type)) { g_ptr_array_add (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY2); return secrets; } - if ((priv->wep_tx_keyidx == 3) && !verify_wep_key (priv->wep_key3, priv->wep_key_type)) { + if ((priv->wep_tx_keyidx == 3) && !nm_utils_wep_key_valid (priv->wep_key3, priv->wep_key_type)) { g_ptr_array_add (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY3); return secrets; } @@ -791,7 +734,7 @@ need_secrets (NMSetting *setting) /* WPA-PSK infrastructure and adhoc */ if ( (strcmp (priv->key_mgmt, "wpa-none") == 0) || (strcmp (priv->key_mgmt, "wpa-psk") == 0)) { - if (!verify_wpa_psk (priv->psk)) { + if (!nm_utils_wpa_psk_valid (priv->psk)) { g_ptr_array_add (secrets, NM_SETTING_WIRELESS_SECURITY_PSK); return secrets; } @@ -921,28 +864,28 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (priv->wep_key0 && !verify_wep_key (priv->wep_key0, priv->wep_key_type)) { + if (priv->wep_key0 && !nm_utils_wep_key_valid (priv->wep_key0, priv->wep_key_type)) { g_set_error (error, NM_SETTING_WIRELESS_SECURITY_ERROR, NM_SETTING_WIRELESS_SECURITY_ERROR_INVALID_PROPERTY, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0); return FALSE; } - if (priv->wep_key1 && !verify_wep_key (priv->wep_key1, priv->wep_key_type)) { + if (priv->wep_key1 && !nm_utils_wep_key_valid (priv->wep_key1, priv->wep_key_type)) { g_set_error (error, NM_SETTING_WIRELESS_SECURITY_ERROR, NM_SETTING_WIRELESS_SECURITY_ERROR_INVALID_PROPERTY, NM_SETTING_WIRELESS_SECURITY_WEP_KEY1); return FALSE; } - if (priv->wep_key2 && !verify_wep_key (priv->wep_key2, priv->wep_key_type)) { + if (priv->wep_key2 && !nm_utils_wep_key_valid (priv->wep_key2, priv->wep_key_type)) { g_set_error (error, NM_SETTING_WIRELESS_SECURITY_ERROR, NM_SETTING_WIRELESS_SECURITY_ERROR_INVALID_PROPERTY, NM_SETTING_WIRELESS_SECURITY_WEP_KEY2); return FALSE; } - if (priv->wep_key3 && !verify_wep_key (priv->wep_key3, priv->wep_key_type)) { + if (priv->wep_key3 && !nm_utils_wep_key_valid (priv->wep_key3, priv->wep_key_type)) { g_set_error (error, NM_SETTING_WIRELESS_SECURITY_ERROR, NM_SETTING_WIRELESS_SECURITY_ERROR_INVALID_PROPERTY, @@ -958,7 +901,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (priv->psk && !verify_wpa_psk (priv->psk)) { + if (priv->psk && !nm_utils_wpa_psk_valid (priv->psk)) { g_set_error (error, NM_SETTING_WIRELESS_SECURITY_ERROR, NM_SETTING_WIRELESS_SECURITY_ERROR_INVALID_PROPERTY, diff --git a/libnm-util/nm-utils.c b/libnm-util/nm-utils.c index 51ddfeb897..9d85ec9f42 100644 --- a/libnm-util/nm-utils.c +++ b/libnm-util/nm-utils.c @@ -1410,6 +1410,79 @@ nm_utils_security_valid (NMUtilsSecurityType type, return good; } +/** + * nm_utils_wep_key_valid: + * @key: a string that might be a WEP key + * @wep_type: the #NMWepKeyType type of the WEP key + * + * Checks if @key is a valid WEP key + * + * Returns: %TRUE if @key is a WEP key, %FALSE if not + */ +gboolean +nm_utils_wep_key_valid (const char *key, NMWepKeyType wep_type) +{ + int keylen, i; + + if (!key) + return FALSE; + + keylen = strlen (key); + if (wep_type == NM_WEP_KEY_TYPE_KEY || NM_WEP_KEY_TYPE_UNKNOWN) { + if (keylen == 10 || keylen == 26) { + /* Hex key */ + for (i = 0; i < keylen; i++) { + if (!g_ascii_isxdigit (key[i])) + return FALSE; + } + } else if (keylen == 5 || keylen == 13) { + /* ASCII key */ + for (i = 0; i < keylen; i++) { + if (!g_ascii_isprint (key[i])) + return FALSE; + } + } else + return FALSE; + + } else if (wep_type == NM_WEP_KEY_TYPE_PASSPHRASE) { + if (!keylen || keylen > 64) + return FALSE; + } + + return TRUE; +} + +/** + * nm_utils_wpa_psk_valid: + * @psk: a string that might be a WPA PSK + * + * Checks if @psk is a valid WPA PSK + * + * Returns: %TRUE if @psk is a WPA PSK, %FALSE if not + */ +gboolean +nm_utils_wpa_psk_valid (const char *psk) +{ + int psklen, i; + + if (!psk) + return FALSE; + + psklen = strlen (psk); + if (psklen < 8 || psklen > 64) + return FALSE; + + if (psklen == 64) { + /* Hex PSK */ + for (i = 0; i < psklen; i++) { + if (!g_ascii_isxdigit (psk[i])) + return FALSE; + } + } + + return TRUE; +} + /** * nm_utils_ip4_addresses_from_gvalue: * @value: gvalue containing a GPtrArray of GArrays of guint32s diff --git a/libnm-util/nm-utils.h b/libnm-util/nm-utils.h index 203d83b5cb..60010f4598 100644 --- a/libnm-util/nm-utils.h +++ b/libnm-util/nm-utils.h @@ -90,6 +90,9 @@ gboolean nm_utils_security_valid (NMUtilsSecurityType type, gboolean nm_utils_ap_mode_security_valid (NMUtilsSecurityType type, NMDeviceWifiCapabilities wifi_caps); +gboolean nm_utils_wep_key_valid (const char *key, NMWepKeyType wep_type); +gboolean nm_utils_wpa_psk_valid (const char *psk); + GSList *nm_utils_ip4_addresses_from_gvalue (const GValue *value); void nm_utils_ip4_addresses_to_gvalue (GSList *list, GValue *value);