diff --git a/libnm-glib/nm-device.c b/libnm-glib/nm-device.c index 8fee11947..323ae442c 100644 --- a/libnm-glib/nm-device.c +++ b/libnm-glib/nm-device.c @@ -46,6 +46,7 @@ #include "nm-glib-marshal.h" #include "nm-dbus-glib-types.h" #include "nm-glib-compat.h" +#include "nm-utils.h" static GType _nm_device_type_for_path (DBusGConnection *connection, const char *path); @@ -1329,33 +1330,6 @@ nm_device_get_available_connections (NMDevice *device) return handle_ptr_array_return (NM_DEVICE_GET_PRIVATE (device)->available_connections); } -/* From hostap, Copyright (c) 2002-2005, Jouni Malinen */ - -static int hex2num (char c) -{ - if (c >= '0' && c <= '9') - return c - '0'; - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - return -1; -} - -static int hex2byte (const char *hex) -{ - int a, b; - a = hex2num(*hex++); - if (a < 0) - return -1; - b = hex2num(*hex++); - if (b < 0) - return -1; - return (a << 4) | b; -} - -/* End from hostap */ - static char * get_decoded_property (GUdevDevice *device, const char *property) { @@ -1371,7 +1345,7 @@ get_decoded_property (GUdevDevice *device, const char *property) n = unescaped = g_malloc0 (len + 1); while (*p) { if ((len >= 4) && (*p == '\\') && (*(p+1) == 'x')) { - *n++ = (char) hex2byte (p + 2); + *n++ = (char) nm_utils_hex2byte (p + 2); p += 4; len -= 4; } else { diff --git a/libnm-util/libnm-util.ver b/libnm-util/libnm-util.ver index 042118e0f..a0ad19f47 100644 --- a/libnm-util/libnm-util.ver +++ b/libnm-util/libnm-util.ver @@ -523,10 +523,13 @@ global: nm_setting_wireless_security_remove_proto; nm_setting_wireless_security_set_wep_key; nm_utils_ap_mode_security_valid; + nm_utils_bin2hexstr; nm_utils_deinit; nm_utils_escape_ssid; nm_utils_file_is_pkcs12; nm_utils_gvalue_hash_dup; + nm_utils_hex2byte; + nm_utils_hexstr2bin; nm_utils_hwaddr_atoba; nm_utils_hwaddr_aton; nm_utils_hwaddr_len; diff --git a/libnm-util/nm-utils.c b/libnm-util/nm-utils.c index da17b2a67..7b9341870 100644 --- a/libnm-util/nm-utils.c +++ b/libnm-util/nm-utils.c @@ -21,7 +21,7 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * (C) Copyright 2005 - 2012 Red Hat, Inc. + * (C) Copyright 2005 - 2013 Red Hat, Inc. */ #include "config.h" @@ -1559,44 +1559,6 @@ make_key (const char *salt, return key; } -/* - * utils_bin2hexstr - * - * Convert a byte-array into a hexadecimal string. - * - * Code originally by Alex Larsson and - * copyright Red Hat, Inc. under terms of the LGPL. - * - */ -static char * -utils_bin2hexstr (const char *bytes, int len, int final_len) -{ - static char hex_digits[] = "0123456789abcdef"; - char *result; - int i; - gsize buflen = (len * 2) + 1; - - g_return_val_if_fail (bytes != NULL, NULL); - g_return_val_if_fail (len > 0, NULL); - g_return_val_if_fail (len < 4096, NULL); /* Arbitrary limit */ - if (final_len > -1) - g_return_val_if_fail (final_len < buflen, NULL); - - result = g_malloc0 (buflen); - for (i = 0; i < len; i++) - { - result[2*i] = hex_digits[(bytes[i] >> 4) & 0xf]; - result[2*i+1] = hex_digits[bytes[i] & 0xf]; - } - /* Cut converted key off at the correct length for this cipher type */ - if (final_len > -1) - result[final_len] = '\0'; - else - result[buflen - 1] = '\0'; - - return result; -} - /** * nm_utils_rsa_key_encrypt: * @data: RSA private key data to be encrypted @@ -1636,7 +1598,7 @@ nm_utils_rsa_key_encrypt (const GByteArray *data, if (!in_password) { if (!crypto_randomize (pw_buf, sizeof (pw_buf), error)) return NULL; - in_password = tmp_password = utils_bin2hexstr ((const char *) pw_buf, sizeof (pw_buf), -1); + in_password = tmp_password = nm_utils_bin2hexstr ((const char *) pw_buf, sizeof (pw_buf), -1); } if (!crypto_randomize (salt, sizeof (salt), error)) @@ -1655,7 +1617,7 @@ nm_utils_rsa_key_encrypt (const GByteArray *data, g_string_append (pem, "Proc-Type: 4,ENCRYPTED\n"); /* Convert the salt to a hex string */ - tmp = utils_bin2hexstr ((const char *) salt, sizeof (salt), 16); + tmp = nm_utils_bin2hexstr ((const char *) salt, sizeof (salt), 16); g_string_append_printf (pem, "DEK-Info: DES-EDE3-CBC,%s\n\n", tmp); g_free (tmp); @@ -2063,6 +2025,115 @@ nm_utils_hwaddr_ntoa (gconstpointer addr, int type) return g_string_free (out, FALSE); } +/** + * nm_utils_bin2hexstr: + * @bytes: an array of bytes + * @len: the length of the @bytes array + * @final_len: an index where to cut off the returned string, or -1 + * + * Converts a byte-array @bytes into a hexadecimal string. + * If @final_len is greater than -1, the returned string is terminated at + * that index (returned_string[final_len] == '\0'), + * + * Return value: (transfer full): the textual form of @bytes + * + * Since: 0.9.10 + * + * Code originally by Alex Larsson and + * copyright Red Hat, Inc. under terms of the LGPL. + */ +char * +nm_utils_bin2hexstr (const char *bytes, int len, int final_len) +{ + static char hex_digits[] = "0123456789abcdef"; + char *result; + int i; + gsize buflen = (len * 2) + 1; + + g_return_val_if_fail (bytes != NULL, NULL); + g_return_val_if_fail (len > 0, NULL); + g_return_val_if_fail (len < 4096, NULL); /* Arbitrary limit */ + if (final_len > -1) + g_return_val_if_fail (final_len < buflen, NULL); + + result = g_malloc0 (buflen); + for (i = 0; i < len; i++) + { + result[2*i] = hex_digits[(bytes[i] >> 4) & 0xf]; + result[2*i+1] = hex_digits[bytes[i] & 0xf]; + } + /* Cut converted key off at the correct length for this cipher type */ + if (final_len > -1) + result[final_len] = '\0'; + else + result[buflen - 1] = '\0'; + + return result; +} + +/* From hostap, Copyright (c) 2002-2005, Jouni Malinen */ +/** + * nm_utils_hex2byte: + * @hex: a string representing a hex byte + * + * Converts a hex string (2 characters) into its byte representation. + * + * Return value: a byte, or -1 if @hex doesn't represent a hex byte + * + * Since: 0.9.10 + */ +int +nm_utils_hex2byte (const char *hex) +{ + int a, b; + a = g_ascii_xdigit_value (*hex++); + if (a < 0) + return -1; + b = g_ascii_xdigit_value (*hex++); + if (b < 0) + return -1; + return (a << 4) | b; +} + +/** + * nm_utils_hexstr2bin: + * @hex: an hex string + * @len: the length of the @hex string (it has to be even) + * + * Converts a hexadecimal string @hex into a byte-array. The returned array + * length is @len/2. + * + * Return value: (transfer full): a array of bytes, or %NULL on error + * + * Since: 0.9.10 + */ +char * +nm_utils_hexstr2bin (const char *hex, size_t len) +{ + size_t i; + int a; + const char * ipos = hex; + char * buf = NULL; + char * opos; + + /* Length must be a multiple of 2 */ + if ((len % 2) != 0) + return NULL; + + opos = buf = g_malloc0 ((len / 2) + 1); + for (i = 0; i < len; i += 2) { + a = nm_utils_hex2byte (ipos); + if (a < 0) { + g_free (buf); + return NULL; + } + *opos++ = a; + ipos += 2; + } + return buf; +} +/* End from hostap */ + /** * nm_utils_iface_valid_name: * @name: Name of interface diff --git a/libnm-util/nm-utils.h b/libnm-util/nm-utils.h index 60010f459..bb6d51cf7 100644 --- a/libnm-util/nm-utils.h +++ b/libnm-util/nm-utils.h @@ -20,7 +20,7 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * (C) Copyright 2005 - 2012 Red Hat, Inc. + * (C) Copyright 2005 - 2013 Red Hat, Inc. */ #ifndef NM_UTILS_H @@ -141,6 +141,10 @@ char *nm_utils_hwaddr_ntoa (gconstpointer addr, int type); GByteArray *nm_utils_hwaddr_atoba (const char *asc, int type); guint8 *nm_utils_hwaddr_aton (const char *asc, int type, gpointer buffer); +char *nm_utils_bin2hexstr (const char *bytes, int len, int final_len); +int nm_utils_hex2byte (const char *hex); +char *nm_utils_hexstr2bin (const char *hex, size_t len); + gboolean nm_utils_iface_valid_name(const char *name); gboolean nm_utils_is_uuid (const char *str); diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index 7c914c887..5604740de 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -3056,7 +3056,7 @@ make_wireless_setting (shvarFile *ifcfg, p++; } - tmp = utils_hexstr2bin (value + 2, value_len - 2); + tmp = nm_utils_hexstr2bin (value + 2, value_len - 2); ssid_len = (value_len - 2) / 2; memcpy (buf, tmp, ssid_len); p = &buf[0]; diff --git a/src/settings/plugins/ifcfg-rh/utils.c b/src/settings/plugins/ifcfg-rh/utils.c index 82cb206a2..0c87a7f97 100644 --- a/src/settings/plugins/ifcfg-rh/utils.c +++ b/src/settings/plugins/ifcfg-rh/utils.c @@ -24,97 +24,6 @@ #include "utils.h" #include "shvar.h" -/* - * utils_bin2hexstr - * - * Convert a byte-array into a hexadecimal string. - * - * Code originally by Alex Larsson and - * copyright Red Hat, Inc. under terms of the LGPL. - * - */ -char * -utils_bin2hexstr (const char *bytes, int len, int final_len) -{ - static char hex_digits[] = "0123456789abcdef"; - char *result; - int i; - gsize buflen = (len * 2) + 1; - - g_return_val_if_fail (bytes != NULL, NULL); - g_return_val_if_fail (len > 0, NULL); - g_return_val_if_fail (len < 4096, NULL); /* Arbitrary limit */ - if (final_len > -1) - g_return_val_if_fail (final_len < buflen, NULL); - - result = g_malloc0 (buflen); - for (i = 0; i < len; i++) - { - result[2*i] = hex_digits[(bytes[i] >> 4) & 0xf]; - result[2*i+1] = hex_digits[bytes[i] & 0xf]; - } - /* Cut converted key off at the correct length for this cipher type */ - if (final_len > -1) - result[final_len] = '\0'; - else - result[buflen - 1] = '\0'; - - return result; -} - -/* From hostap, Copyright (c) 2002-2005, Jouni Malinen */ - -static int hex2num (char c) -{ - if (c >= '0' && c <= '9') - return c - '0'; - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - return -1; -} - -static int hex2byte (const char *hex) -{ - int a, b; - a = hex2num(*hex++); - if (a < 0) - return -1; - b = hex2num(*hex++); - if (b < 0) - return -1; - return (a << 4) | b; -} - -char * -utils_hexstr2bin (const char *hex, size_t len) -{ - size_t i; - int a; - const char * ipos = hex; - char * buf = NULL; - char * opos; - - /* Length must be a multiple of 2 */ - if ((len % 2) != 0) - return NULL; - - opos = buf = g_malloc0 ((len / 2) + 1); - for (i = 0; i < len; i += 2) { - a = hex2byte (ipos); - if (a < 0) { - g_free (buf); - return NULL; - } - *opos++ = a; - ipos += 2; - } - return buf; -} - -/* End from hostap */ - /* * utils_single_quote_string * diff --git a/src/settings/plugins/ifcfg-rh/utils.h b/src/settings/plugins/ifcfg-rh/utils.h index 9262ef720..2c94aacaa 100644 --- a/src/settings/plugins/ifcfg-rh/utils.h +++ b/src/settings/plugins/ifcfg-rh/utils.h @@ -26,10 +26,6 @@ #include "shvar.h" #include "common.h" -char *utils_bin2hexstr (const char *bytes, int len, int final_len); - -char *utils_hexstr2bin (const char *hex, size_t len); - char *utils_single_quote_string (const char *str); char *utils_single_unquote_string (const char *str); diff --git a/src/settings/plugins/ifnet/connection_parser.c b/src/settings/plugins/ifnet/connection_parser.c index f8ead0ed9..f210be2af 100644 --- a/src/settings/plugins/ifnet/connection_parser.c +++ b/src/settings/plugins/ifnet/connection_parser.c @@ -55,7 +55,7 @@ update_connection_id (NMConnection *connection, const char *conn_name) name_len = strlen (conn_name); if ((name_len > 2) && (g_str_has_prefix (conn_name, "0x"))) { - idstr = utils_hexstr2bin (conn_name + 2, name_len - 2); + idstr = nm_utils_hexstr2bin (conn_name + 2, name_len - 2); } else idstr = g_strdup_printf ("%s", conn_name); uuid_base = idstr; @@ -973,7 +973,7 @@ make_wireless_connection_setting (const char *conn_name, goto error; } - tmp = utils_hexstr2bin (p, value_len - 2); + tmp = nm_utils_hexstr2bin (p, value_len - 2); ssid_len = (value_len - 2) / 2; converted = g_malloc0 (ssid_len + 1); memcpy (converted, tmp, ssid_len); @@ -1141,7 +1141,7 @@ add_one_wep_key (const char *ssid, } - converted = utils_bin2hexstr (tmp, strlen (tmp), strlen (tmp) * 2); + converted = nm_utils_bin2hexstr (tmp, strlen (tmp), strlen (tmp) * 2); g_free (tmp); } else { g_set_error (error, ifnet_plugin_error_quark (), 0, diff --git a/src/settings/plugins/ifnet/net_utils.c b/src/settings/plugins/ifnet/net_utils.c index 17eb16771..c2e3a376a 100644 --- a/src/settings/plugins/ifnet/net_utils.c +++ b/src/settings/plugins/ifnet/net_utils.c @@ -94,88 +94,6 @@ is_true (const char *str) return FALSE; } -static int -hex2num (char c) -{ - if (c >= '0' && c <= '9') - return c - '0'; - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - return -1; -} - -static int -hex2byte (const char *hex) -{ - int a, b; - - a = hex2num (*hex++); - if (a < 0) - return -1; - b = hex2num (*hex++); - if (b < 0) - return -1; - return (a << 4) | b; -} - -/* free return value by caller */ -gchar * -utils_hexstr2bin (const gchar * hex, size_t len) -{ - size_t i; - int a; - const gchar *ipos = hex; - gchar *buf = NULL; - gchar *opos; - - /* Length must be a multiple of 2 */ - if ((len % 2) != 0) - return NULL; - - opos = buf = g_malloc0 ((len / 2) + 1); - for (i = 0; i < len; i += 2) { - a = hex2byte (ipos); - if (a < 0) { - g_free (buf); - return NULL; - } - *opos++ = a; - ipos += 2; - } - return buf; -} - -/* free return value by caller */ -gchar * -utils_bin2hexstr (const gchar * bytes, int len, int final_len) -{ - static gchar hex_digits[] = "0123456789abcdef"; - gchar *result; - int i; - gsize buflen = (len * 2) + 1; - - g_return_val_if_fail (bytes != NULL, NULL); - g_return_val_if_fail (len > 0, NULL); - g_return_val_if_fail (len < 4096, NULL); /* Arbitrary limit */ - if (final_len > -1) - g_return_val_if_fail (final_len < buflen, NULL); - - result = g_malloc0 (buflen); - for (i = 0; i < len; i++) { - result[2 * i] = hex_digits[(bytes[i] >> 4) & 0xf]; - result[2 * i + 1] = hex_digits[bytes[i] & 0xf]; - } - /* Cut converted key off at the correct length for this cipher type */ - if (final_len > -1) - result[final_len] = '\0'; - else - result[buflen - 1] = '\0'; - - return result; -} - GQuark ifnet_plugin_error_quark (void) { diff --git a/src/settings/plugins/ifnet/net_utils.h b/src/settings/plugins/ifnet/net_utils.h index c2ed83a13..71430c838 100644 --- a/src/settings/plugins/ifnet/net_utils.h +++ b/src/settings/plugins/ifnet/net_utils.h @@ -68,8 +68,6 @@ gchar *strip_string (gchar *str, gchar t); gboolean is_managed (const char *conn_name); GQuark ifnet_plugin_error_quark (void); -gchar *utils_hexstr2bin (const gchar * hex, size_t len); -gchar *utils_bin2hexstr (const gchar * bytes, int len, int final_len); gboolean is_hex (const char *value); gboolean is_ascii (const char *value); diff --git a/src/supplicant-manager/nm-supplicant-config.c b/src/supplicant-manager/nm-supplicant-config.c index f7d406b0d..fb4d87cb1 100644 --- a/src/supplicant-manager/nm-supplicant-config.c +++ b/src/supplicant-manager/nm-supplicant-config.c @@ -34,8 +34,7 @@ #include "nm-logging.h" #include "nm-setting.h" #include "NetworkManagerUtils.h" - -static char *hexstr2bin (const char *hex, size_t len); +#include "nm-utils.h" #define NM_SUPPLICANT_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \ NM_TYPE_SUPPLICANT_CONFIG, \ @@ -538,7 +537,7 @@ add_wep_key (NMSupplicantConfig *self, if ( (wep_type == NM_WEP_KEY_TYPE_UNKNOWN) || (wep_type == NM_WEP_KEY_TYPE_KEY)) { if ((key_len == 10) || (key_len == 26)) { - value = hexstr2bin (key, strlen (key)); + value = nm_utils_hexstr2bin (key, strlen (key)); success = nm_supplicant_config_add_option (self, name, value, key_len / 2, TRUE); g_free (value); if (!success) { @@ -599,7 +598,7 @@ nm_supplicant_config_add_setting_wireless_security (NMSupplicantConfig *self, if (psk_len == 64) { /* Hex PSK */ - value = hexstr2bin (psk, psk_len); + value = nm_utils_hexstr2bin (psk, psk_len); success = nm_supplicant_config_add_option (self, "psk", value, psk_len / 2, TRUE); g_free (value); if (!success) { @@ -1046,56 +1045,3 @@ nm_supplicant_config_add_no_security (NMSupplicantConfig *self) return nm_supplicant_config_add_option (self, "key_mgmt", "NONE", -1, FALSE); } -/* From hostap, Copyright (c) 2002-2005, Jouni Malinen */ - -static int hex2num (char c) -{ - if (c >= '0' && c <= '9') - return c - '0'; - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - return -1; -} - -static int hex2byte (const char *hex) -{ - int a, b; - a = hex2num(*hex++); - if (a < 0) - return -1; - b = hex2num(*hex++); - if (b < 0) - return -1; - return (a << 4) | b; -} - -static char * -hexstr2bin (const char *hex, size_t len) -{ - size_t i; - int a; - const char * ipos = hex; - char * buf = NULL; - char * opos; - - /* Length must be a multiple of 2 */ - if ((len % 2) != 0) - return NULL; - - opos = buf = g_malloc0 ((len / 2) + 1); - for (i = 0; i < len; i += 2) { - a = hex2byte (ipos); - if (a < 0) { - g_free (buf); - return NULL; - } - *opos++ = a; - ipos += 2; - } - return buf; -} - -/* End from hostap */ -