summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2018-08-29 13:27:00 +0200
committerThomas Haller <thaller@redhat.com>2018-09-03 18:07:59 +0200
commitfdcc7e5be9805aa1448699f6e0e8b54342ade2d3 (patch)
treeffaff5e324c60601c150fda7aa539c9ee9876d2a
parent47ba0470a35dfbefafa33b4e487d674beb772647 (diff)
downloadNetworkManager-fdcc7e5be9805aa1448699f6e0e8b54342ade2d3.tar.gz
libnm/crypto: adjust argument types for crypto_md5_hash()
There should be a clear distinction between whether an array is a NUL terminated string or binary with a length. crypto_md5_hash() is already complicated enough. Adjust it's API to only support binary arguments, and thus have "guint8 *" type.
-rw-r--r--libnm-core/crypto.c27
-rw-r--r--libnm-core/crypto.h10
-rw-r--r--libnm-core/nm-utils.c17
-rw-r--r--libnm-core/tests/test-crypto.c7
4 files changed, 35 insertions, 26 deletions
diff --git a/libnm-core/crypto.c b/libnm-core/crypto.c
index e52c2cf89c..78f0802670 100644
--- a/libnm-core/crypto.c
+++ b/libnm-core/crypto.c
@@ -395,11 +395,11 @@ crypto_make_des_aes_key (const char *cipher,
key = g_malloc0 (digest_len + 1);
- crypto_md5_hash (salt,
+ crypto_md5_hash ((guint8 *) salt,
8,
- password,
+ (guint8 *) password,
strlen (password),
- key,
+ (guint8 *) key,
digest_len);
*out_len = digest_len;
@@ -730,11 +730,11 @@ crypto_verify_private_key (const char *filename,
}
void
-crypto_md5_hash (const char *salt,
- gssize salt_len,
- const char *password,
- gssize password_len,
- char *buffer,
+crypto_md5_hash (const guint8 *salt,
+ gsize salt_len,
+ const guint8 *password,
+ gsize password_len,
+ guint8 *buffer,
gsize buflen)
{
nm_auto_free_checksum GChecksum *ctx = NULL;
@@ -746,17 +746,12 @@ crypto_md5_hash (const char *salt,
nm_assert (g_checksum_type_get_length (G_CHECKSUM_MD5) == MD5_DIGEST_LEN);
g_return_if_fail (password_len == 0 || password);
- g_return_if_fail (buffer != NULL);
+ g_return_if_fail (buffer);
g_return_if_fail (buflen > 0);
g_return_if_fail (salt_len == 0 || salt);
ctx = g_checksum_new (G_CHECKSUM_MD5);
- if (salt_len < 0)
- salt_len = strlen (salt);
- if (password_len < 0)
- password_len = strlen (password);
-
for (;;) {
gsize digest_len;
@@ -766,13 +761,13 @@ crypto_md5_hash (const char *salt,
g_checksum_update (ctx, (const guchar *) salt, salt_len);
digest_len = MD5_DIGEST_LEN;
- g_checksum_get_digest (ctx, digest.ptr, &digest_len);
+ g_checksum_get_digest (ctx, digest.bin, &digest_len);
nm_assert (digest_len == MD5_DIGEST_LEN);
for (i = 0; i < MD5_DIGEST_LEN; i++) {
if (bufidx >= buflen)
return;
- buffer[bufidx++] = digest.str[i];
+ buffer[bufidx++] = digest.bin[i];
}
g_checksum_reset (ctx);
diff --git a/libnm-core/crypto.h b/libnm-core/crypto.h
index d20d6f3100..8021117ea6 100644
--- a/libnm-core/crypto.h
+++ b/libnm-core/crypto.h
@@ -82,11 +82,11 @@ NMCryptoFileFormat crypto_verify_private_key (const char *file,
/* Internal utils API bits for crypto providers */
-void crypto_md5_hash (const char *salt,
- gssize salt_len,
- const char *password,
- gssize password_len,
- char *buffer,
+void crypto_md5_hash (const guint8 *salt,
+ gsize salt_len,
+ const guint8 *password,
+ gsize password_len,
+ guint8 *buffer,
gsize buflen);
char *crypto_make_des_aes_key (const char *cipher,
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index cb71d6e77d..3fb1188fae 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -2838,9 +2838,17 @@ nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, g
g_return_val_if_fail (uuid_type == NM_UTILS_UUID_TYPE_LEGACY || uuid_type == NM_UTILS_UUID_TYPE_VARIANT3, NULL);
g_return_val_if_fail (!type_args || uuid_type == NM_UTILS_UUID_TYPE_VARIANT3, NULL);
+ if (slen < 0)
+ slen = s ? strlen (s) : 0;
+
switch (uuid_type) {
case NM_UTILS_UUID_TYPE_LEGACY:
- crypto_md5_hash (NULL, 0, s, slen, (char *) uuid, sizeof (uuid));
+ crypto_md5_hash (NULL,
+ 0,
+ (guint8 *) s,
+ slen,
+ (guint8 *) uuid,
+ sizeof (uuid));
break;
case NM_UTILS_UUID_TYPE_VARIANT3: {
uuid_t ns_uuid = { 0 };
@@ -2851,7 +2859,12 @@ nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, g
g_return_val_if_reached (NULL);
}
- crypto_md5_hash (s, slen, (char *) ns_uuid, sizeof (ns_uuid), (char *) uuid, sizeof (uuid));
+ crypto_md5_hash ((guint8 *) s,
+ slen,
+ (guint8 *) ns_uuid,
+ sizeof (ns_uuid),
+ (guint8 *) uuid,
+ sizeof (uuid));
uuid[6] = (uuid[6] & 0x0F) | 0x30;
uuid[8] = (uuid[8] & 0x3F) | 0x80;
diff --git a/libnm-core/tests/test-crypto.c b/libnm-core/tests/test-crypto.c
index 5fb26c1fcc..8865c47654 100644
--- a/libnm-core/tests/test-crypto.c
+++ b/libnm-core/tests/test-crypto.c
@@ -417,15 +417,16 @@ test_md5 (void)
for (i = 0; i < G_N_ELEMENTS (md5_tests); i++) {
memset (digest, 0, sizeof (digest));
- crypto_md5_hash (md5_tests[i].salt,
+ crypto_md5_hash ((const guint8 *) md5_tests[i].salt,
/* crypto_md5_hash() used to clamp salt_len to 8. It
* doesn't any more, so we need to do it here now to
* get output that matches md5_tests[i].result.
*/
md5_tests[i].salt ? 8 : 0,
- md5_tests[i].password,
+ (const guint8 *) md5_tests[i].password,
strlen (md5_tests[i].password),
- digest, md5_tests[i].digest_size);
+ (guint8 *) digest,
+ md5_tests[i].digest_size);
hex = nm_utils_bin2hexstr (digest, md5_tests[i].digest_size, -1);
g_assert_cmpstr (hex, ==, md5_tests[i].result);