2165f81
From 12e9cd51eb0ef07c3554cd035f92d8b7b5b82304 Mon Sep 17 00:00:00 2001
Michael Catanzaro f52f413
From: Colin Walters <walters@verbum.org>
Michael Catanzaro f52f413
Date: Fri, 7 Jun 2019 18:44:43 +0000
Michael Catanzaro f52f413
Subject: [PATCH 1/2] ghmac: Split off wrapper functions into ghmac-utils.c
Michael Catanzaro f52f413
Michael Catanzaro f52f413
Prep for adding a GnuTLS HMAC implementation; these are just
Michael Catanzaro f52f413
utility functions that call the "core" API.
Michael Catanzaro f52f413
---
Michael Catanzaro f52f413
 glib/ghmac-utils.c | 145 +++++++++++++++++++++++++++++++++++++++++++++
Michael Catanzaro f52f413
 glib/ghmac.c       | 112 ----------------------------------
Michael Catanzaro f52f413
 glib/meson.build   |   1 +
Michael Catanzaro f52f413
 3 files changed, 146 insertions(+), 112 deletions(-)
Michael Catanzaro f52f413
 create mode 100644 glib/ghmac-utils.c
Michael Catanzaro f52f413
Michael Catanzaro f52f413
diff --git a/glib/ghmac-utils.c b/glib/ghmac-utils.c
Michael Catanzaro f52f413
new file mode 100644
Michael Catanzaro f52f413
index 000000000..a17359ff1
Michael Catanzaro f52f413
--- /dev/null
Michael Catanzaro f52f413
+++ b/glib/ghmac-utils.c
Michael Catanzaro f52f413
@@ -0,0 +1,145 @@
Michael Catanzaro f52f413
+/* ghmac.h - data hashing functions
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * Copyright (C) 2011  Collabora Ltd.
Michael Catanzaro f52f413
+ * Copyright (C) 2019  Red Hat, Inc.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * This library is free software; you can redistribute it and/or
Michael Catanzaro f52f413
+ * modify it under the terms of the GNU Lesser General Public
Michael Catanzaro f52f413
+ * License as published by the Free Software Foundation; either
Michael Catanzaro f52f413
+ * version 2.1 of the License, or (at your option) any later version.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * This library is distributed in the hope that it will be useful,
Michael Catanzaro f52f413
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Michael Catanzaro f52f413
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Michael Catanzaro f52f413
+ * Lesser General Public License for more details.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * You should have received a copy of the GNU Lesser General Public License
Michael Catanzaro f52f413
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
Michael Catanzaro f52f413
+ */
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+#include "config.h"
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+#include <string.h>
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+#include "ghmac.h"
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+#include "glib/galloca.h"
Michael Catanzaro f52f413
+#include "gatomic.h"
Michael Catanzaro f52f413
+#include "gslice.h"
Michael Catanzaro f52f413
+#include "gmem.h"
Michael Catanzaro f52f413
+#include "gstrfuncs.h"
Michael Catanzaro f52f413
+#include "gtestutils.h"
Michael Catanzaro f52f413
+#include "gtypes.h"
Michael Catanzaro f52f413
+#include "glibintl.h"
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+/**
Michael Catanzaro f52f413
+ * g_compute_hmac_for_data:
Michael Catanzaro f52f413
+ * @digest_type: a #GChecksumType to use for the HMAC
Michael Catanzaro f52f413
+ * @key: (array length=key_len): the key to use in the HMAC
Michael Catanzaro f52f413
+ * @key_len: the length of the key
Michael Catanzaro f52f413
+ * @data: (array length=length): binary blob to compute the HMAC of
Michael Catanzaro f52f413
+ * @length: length of @data
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * Computes the HMAC for a binary @data of @length. This is a
Michael Catanzaro f52f413
+ * convenience wrapper for g_hmac_new(), g_hmac_get_string()
Michael Catanzaro f52f413
+ * and g_hmac_unref().
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * The hexadecimal string returned will be in lower case.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * Returns: the HMAC of the binary data as a string in hexadecimal.
Michael Catanzaro f52f413
+ *   The returned string should be freed with g_free() when done using it.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * Since: 2.30
Michael Catanzaro f52f413
+ */
Michael Catanzaro f52f413
+gchar *
Michael Catanzaro f52f413
+g_compute_hmac_for_data (GChecksumType  digest_type,
Michael Catanzaro f52f413
+                         const guchar  *key,
Michael Catanzaro f52f413
+                         gsize          key_len,
Michael Catanzaro f52f413
+                         const guchar  *data,
Michael Catanzaro f52f413
+                         gsize          length)
Michael Catanzaro f52f413
+{
Michael Catanzaro f52f413
+  GHmac *hmac;
Michael Catanzaro f52f413
+  gchar *retval;
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  g_return_val_if_fail (length == 0 || data != NULL, NULL);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  hmac = g_hmac_new (digest_type, key, key_len);
Michael Catanzaro f52f413
+  if (!hmac)
Michael Catanzaro f52f413
+    return NULL;
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  g_hmac_update (hmac, data, length);
Michael Catanzaro f52f413
+  retval = g_strdup (g_hmac_get_string (hmac));
Michael Catanzaro f52f413
+  g_hmac_unref (hmac);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  return retval;
Michael Catanzaro f52f413
+}
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+/**
Michael Catanzaro f52f413
+ * g_compute_hmac_for_bytes:
Michael Catanzaro f52f413
+ * @digest_type: a #GChecksumType to use for the HMAC
Michael Catanzaro f52f413
+ * @key: the key to use in the HMAC
Michael Catanzaro f52f413
+ * @data: binary blob to compute the HMAC of
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * Computes the HMAC for a binary @data. This is a
Michael Catanzaro f52f413
+ * convenience wrapper for g_hmac_new(), g_hmac_get_string()
Michael Catanzaro f52f413
+ * and g_hmac_unref().
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * The hexadecimal string returned will be in lower case.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * Returns: the HMAC of the binary data as a string in hexadecimal.
Michael Catanzaro f52f413
+ *   The returned string should be freed with g_free() when done using it.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * Since: 2.50
Michael Catanzaro f52f413
+ */
Michael Catanzaro f52f413
+gchar *
Michael Catanzaro f52f413
+g_compute_hmac_for_bytes (GChecksumType  digest_type,
Michael Catanzaro f52f413
+                          GBytes        *key,
Michael Catanzaro f52f413
+                          GBytes        *data)
Michael Catanzaro f52f413
+{
Michael Catanzaro f52f413
+  gconstpointer byte_data;
Michael Catanzaro f52f413
+  gsize length;
Michael Catanzaro f52f413
+  gconstpointer key_data;
Michael Catanzaro f52f413
+  gsize key_len;
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  g_return_val_if_fail (data != NULL, NULL);
Michael Catanzaro f52f413
+  g_return_val_if_fail (key != NULL, NULL);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  byte_data = g_bytes_get_data (data, &length);
Michael Catanzaro f52f413
+  key_data = g_bytes_get_data (key, &key_len);
Michael Catanzaro f52f413
+  return g_compute_hmac_for_data (digest_type, key_data, key_len, byte_data, length);
Michael Catanzaro f52f413
+}
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+/**
Michael Catanzaro f52f413
+ * g_compute_hmac_for_string:
Michael Catanzaro f52f413
+ * @digest_type: a #GChecksumType to use for the HMAC
Michael Catanzaro f52f413
+ * @key: (array length=key_len): the key to use in the HMAC
Michael Catanzaro f52f413
+ * @key_len: the length of the key
Michael Catanzaro f52f413
+ * @str: the string to compute the HMAC for
Michael Catanzaro f52f413
+ * @length: the length of the string, or -1 if the string is nul-terminated
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * Computes the HMAC for a string.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * The hexadecimal string returned will be in lower case.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * Returns: the HMAC as a hexadecimal string.
Michael Catanzaro f52f413
+ *     The returned string should be freed with g_free()
Michael Catanzaro f52f413
+ *     when done using it.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * Since: 2.30
Michael Catanzaro f52f413
+ */
Michael Catanzaro f52f413
+gchar *
Michael Catanzaro f52f413
+g_compute_hmac_for_string (GChecksumType  digest_type,
Michael Catanzaro f52f413
+                           const guchar  *key,
Michael Catanzaro f52f413
+                           gsize          key_len,
Michael Catanzaro f52f413
+                           const gchar   *str,
Michael Catanzaro f52f413
+                           gssize         length)
Michael Catanzaro f52f413
+{
Michael Catanzaro f52f413
+  g_return_val_if_fail (length == 0 || str != NULL, NULL);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  if (length < 0)
Michael Catanzaro f52f413
+    length = strlen (str);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  return g_compute_hmac_for_data (digest_type, key, key_len,
Michael Catanzaro f52f413
+                                  (const guchar *) str, length);
Michael Catanzaro f52f413
+}
Michael Catanzaro f52f413
diff --git a/glib/ghmac.c b/glib/ghmac.c
Michael Catanzaro f52f413
index 49fd272f0..4f181f21f 100644
Michael Catanzaro f52f413
--- a/glib/ghmac.c
Michael Catanzaro f52f413
+++ b/glib/ghmac.c
Michael Catanzaro f52f413
@@ -329,115 +329,3 @@ g_hmac_get_digest (GHmac  *hmac,
Michael Catanzaro f52f413
   g_checksum_update (hmac->digesto, buffer, len);
Michael Catanzaro f52f413
   g_checksum_get_digest (hmac->digesto, buffer, digest_len);
Michael Catanzaro f52f413
 }
Michael Catanzaro f52f413
-
Michael Catanzaro f52f413
-/**
Michael Catanzaro f52f413
- * g_compute_hmac_for_data:
Michael Catanzaro f52f413
- * @digest_type: a #GChecksumType to use for the HMAC
Michael Catanzaro f52f413
- * @key: (array length=key_len): the key to use in the HMAC
Michael Catanzaro f52f413
- * @key_len: the length of the key
Michael Catanzaro f52f413
- * @data: (array length=length): binary blob to compute the HMAC of
Michael Catanzaro f52f413
- * @length: length of @data
Michael Catanzaro f52f413
- *
Michael Catanzaro f52f413
- * Computes the HMAC for a binary @data of @length. This is a
Michael Catanzaro f52f413
- * convenience wrapper for g_hmac_new(), g_hmac_get_string()
Michael Catanzaro f52f413
- * and g_hmac_unref().
Michael Catanzaro f52f413
- *
Michael Catanzaro f52f413
- * The hexadecimal string returned will be in lower case.
Michael Catanzaro f52f413
- *
Michael Catanzaro f52f413
- * Returns: the HMAC of the binary data as a string in hexadecimal.
Michael Catanzaro f52f413
- *   The returned string should be freed with g_free() when done using it.
Michael Catanzaro f52f413
- *
Michael Catanzaro f52f413
- * Since: 2.30
Michael Catanzaro f52f413
- */
Michael Catanzaro f52f413
-gchar *
Michael Catanzaro f52f413
-g_compute_hmac_for_data (GChecksumType  digest_type,
Michael Catanzaro f52f413
-                         const guchar  *key,
Michael Catanzaro f52f413
-                         gsize          key_len,
Michael Catanzaro f52f413
-                         const guchar  *data,
Michael Catanzaro f52f413
-                         gsize          length)
Michael Catanzaro f52f413
-{
Michael Catanzaro f52f413
-  GHmac *hmac;
Michael Catanzaro f52f413
-  gchar *retval;
Michael Catanzaro f52f413
-
Michael Catanzaro f52f413
-  g_return_val_if_fail (length == 0 || data != NULL, NULL);
Michael Catanzaro f52f413
-
Michael Catanzaro f52f413
-  hmac = g_hmac_new (digest_type, key, key_len);
Michael Catanzaro f52f413
-  if (!hmac)
Michael Catanzaro f52f413
-    return NULL;
Michael Catanzaro f52f413
-
Michael Catanzaro f52f413
-  g_hmac_update (hmac, data, length);
Michael Catanzaro f52f413
-  retval = g_strdup (g_hmac_get_string (hmac));
Michael Catanzaro f52f413
-  g_hmac_unref (hmac);
Michael Catanzaro f52f413
-
Michael Catanzaro f52f413
-  return retval;
Michael Catanzaro f52f413
-}
Michael Catanzaro f52f413
-
Michael Catanzaro f52f413
-/**
Michael Catanzaro f52f413
- * g_compute_hmac_for_bytes:
Michael Catanzaro f52f413
- * @digest_type: a #GChecksumType to use for the HMAC
Michael Catanzaro f52f413
- * @key: the key to use in the HMAC
Michael Catanzaro f52f413
- * @data: binary blob to compute the HMAC of
Michael Catanzaro f52f413
- *
Michael Catanzaro f52f413
- * Computes the HMAC for a binary @data. This is a
Michael Catanzaro f52f413
- * convenience wrapper for g_hmac_new(), g_hmac_get_string()
Michael Catanzaro f52f413
- * and g_hmac_unref().
Michael Catanzaro f52f413
- *
Michael Catanzaro f52f413
- * The hexadecimal string returned will be in lower case.
Michael Catanzaro f52f413
- *
Michael Catanzaro f52f413
- * Returns: the HMAC of the binary data as a string in hexadecimal.
Michael Catanzaro f52f413
- *   The returned string should be freed with g_free() when done using it.
Michael Catanzaro f52f413
- *
Michael Catanzaro f52f413
- * Since: 2.50
Michael Catanzaro f52f413
- */
Michael Catanzaro f52f413
-gchar *
Michael Catanzaro f52f413
-g_compute_hmac_for_bytes (GChecksumType  digest_type,
Michael Catanzaro f52f413
-                          GBytes        *key,
Michael Catanzaro f52f413
-                          GBytes        *data)
Michael Catanzaro f52f413
-{
Michael Catanzaro f52f413
-  gconstpointer byte_data;
Michael Catanzaro f52f413
-  gsize length;
Michael Catanzaro f52f413
-  gconstpointer key_data;
Michael Catanzaro f52f413
-  gsize key_len;
Michael Catanzaro f52f413
-
Michael Catanzaro f52f413
-  g_return_val_if_fail (data != NULL, NULL);
Michael Catanzaro f52f413
-  g_return_val_if_fail (key != NULL, NULL);
Michael Catanzaro f52f413
-
Michael Catanzaro f52f413
-  byte_data = g_bytes_get_data (data, &length);
Michael Catanzaro f52f413
-  key_data = g_bytes_get_data (key, &key_len);
Michael Catanzaro f52f413
-  return g_compute_hmac_for_data (digest_type, key_data, key_len, byte_data, length);
Michael Catanzaro f52f413
-}
Michael Catanzaro f52f413
-
Michael Catanzaro f52f413
-
Michael Catanzaro f52f413
-/**
Michael Catanzaro f52f413
- * g_compute_hmac_for_string:
Michael Catanzaro f52f413
- * @digest_type: a #GChecksumType to use for the HMAC
Michael Catanzaro f52f413
- * @key: (array length=key_len): the key to use in the HMAC
Michael Catanzaro f52f413
- * @key_len: the length of the key
Michael Catanzaro f52f413
- * @str: the string to compute the HMAC for
Michael Catanzaro f52f413
- * @length: the length of the string, or -1 if the string is nul-terminated
Michael Catanzaro f52f413
- *
Michael Catanzaro f52f413
- * Computes the HMAC for a string.
Michael Catanzaro f52f413
- *
Michael Catanzaro f52f413
- * The hexadecimal string returned will be in lower case.
Michael Catanzaro f52f413
- *
Michael Catanzaro f52f413
- * Returns: the HMAC as a hexadecimal string.
Michael Catanzaro f52f413
- *     The returned string should be freed with g_free()
Michael Catanzaro f52f413
- *     when done using it.
Michael Catanzaro f52f413
- *
Michael Catanzaro f52f413
- * Since: 2.30
Michael Catanzaro f52f413
- */
Michael Catanzaro f52f413
-gchar *
Michael Catanzaro f52f413
-g_compute_hmac_for_string (GChecksumType  digest_type,
Michael Catanzaro f52f413
-                           const guchar  *key,
Michael Catanzaro f52f413
-                           gsize          key_len,
Michael Catanzaro f52f413
-                           const gchar   *str,
Michael Catanzaro f52f413
-                           gssize         length)
Michael Catanzaro f52f413
-{
Michael Catanzaro f52f413
-  g_return_val_if_fail (length == 0 || str != NULL, NULL);
Michael Catanzaro f52f413
-
Michael Catanzaro f52f413
-  if (length < 0)
Michael Catanzaro f52f413
-    length = strlen (str);
Michael Catanzaro f52f413
-
Michael Catanzaro f52f413
-  return g_compute_hmac_for_data (digest_type, key, key_len,
Michael Catanzaro f52f413
-                                  (const guchar *) str, length);
Michael Catanzaro f52f413
-}
Michael Catanzaro f52f413
diff --git a/glib/meson.build b/glib/meson.build
2165f81
index 8c18e6de4..329b8d197 100644
Michael Catanzaro f52f413
--- a/glib/meson.build
Michael Catanzaro f52f413
+++ b/glib/meson.build
Michael Catanzaro f52f413
@@ -253,6 +253,7 @@ glib_sources = files(
Michael Catanzaro f52f413
   'ggettext.c',
Michael Catanzaro f52f413
   'ghash.c',
Michael Catanzaro f52f413
   'ghmac.c',
Michael Catanzaro f52f413
+  'ghmac-utils.c',
Michael Catanzaro f52f413
   'ghook.c',
Michael Catanzaro f52f413
   'ghostutils.c',
Michael Catanzaro f52f413
   'giochannel.c',
Michael Catanzaro f52f413
-- 
2165f81
2.29.2
Michael Catanzaro f52f413
Michael Catanzaro f52f413
2165f81
From 231ed985074af4a354405cf1961fabf9c60bce43 Mon Sep 17 00:00:00 2001
Michael Catanzaro f52f413
From: Colin Walters <walters@verbum.org>
Michael Catanzaro f52f413
Date: Fri, 7 Jun 2019 19:36:54 +0000
Michael Catanzaro f52f413
Subject: [PATCH 2/2] Add a gnutls backend for GHmac
Michael Catanzaro f52f413
Michael Catanzaro f52f413
For RHEL we want apps to use FIPS-certified crypto libraries,
Michael Catanzaro f52f413
and HMAC apparently counts as "keyed" and hence needs to
Michael Catanzaro f52f413
be validated.
Michael Catanzaro f52f413
Michael Catanzaro f52f413
Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1630260
Michael Catanzaro f52f413
Replaces: https://gitlab.gnome.org/GNOME/glib/merge_requests/897
Michael Catanzaro f52f413
Michael Catanzaro f52f413
This is a build-time option that backs the GHmac API with GnuTLS.
Michael Catanzaro f52f413
Most distributors ship glib-networking built with GnuTLS, and
Michael Catanzaro f52f413
most apps use glib-networking, so this isn't a net-new library
Michael Catanzaro f52f413
in most cases.
Michael Catanzaro f52f413
Michael Catanzaro f52f413
mcatanzaro note: I've updated Colin's original patch to implement
Michael Catanzaro f52f413
g_hmac_copy() using gnutls_hmac_copy(), which didn't exist when Colin
Michael Catanzaro a6c3663
developed this patch.
Michael Catanzaro f52f413
---
Michael Catanzaro f52f413
 glib/gchecksum.c        |   9 ++-
Michael Catanzaro f52f413
 glib/gchecksumprivate.h |  32 ++++++++
Michael Catanzaro f52f413
 glib/ghmac-gnutls.c     | 164 ++++++++++++++++++++++++++++++++++++++++
Michael Catanzaro f52f413
 glib/ghmac.c            |   3 +
Michael Catanzaro f52f413
 glib/meson.build        |  10 ++-
Michael Catanzaro f52f413
 meson.build             |   7 ++
Michael Catanzaro f52f413
 meson_options.txt       |   5 ++
Michael Catanzaro f52f413
 7 files changed, 224 insertions(+), 6 deletions(-)
Michael Catanzaro f52f413
 create mode 100644 glib/gchecksumprivate.h
Michael Catanzaro f52f413
 create mode 100644 glib/ghmac-gnutls.c
Michael Catanzaro f52f413
Michael Catanzaro f52f413
diff --git a/glib/gchecksum.c b/glib/gchecksum.c
2165f81
index 29b479bc6..929958c3a 100644
Michael Catanzaro f52f413
--- a/glib/gchecksum.c
Michael Catanzaro f52f413
+++ b/glib/gchecksum.c
Michael Catanzaro f52f413
@@ -20,7 +20,7 @@
Michael Catanzaro f52f413
 
Michael Catanzaro f52f413
 #include <string.h>
Michael Catanzaro f52f413
 
Michael Catanzaro f52f413
-#include "gchecksum.h"
Michael Catanzaro f52f413
+#include "gchecksumprivate.h"
Michael Catanzaro f52f413
 
Michael Catanzaro f52f413
 #include "gslice.h"
Michael Catanzaro f52f413
 #include "gmem.h"
Michael Catanzaro f52f413
@@ -173,9 +173,9 @@ sha_byte_reverse (guint32 *buffer,
Michael Catanzaro f52f413
 }
Michael Catanzaro f52f413
 #endif /* G_BYTE_ORDER == G_BIG_ENDIAN */
Michael Catanzaro f52f413
 
Michael Catanzaro f52f413
-static gchar *
Michael Catanzaro f52f413
-digest_to_string (guint8 *digest,
Michael Catanzaro f52f413
-                  gsize   digest_len)
Michael Catanzaro f52f413
+gchar *
Michael Catanzaro f52f413
+gchecksum_digest_to_string (guint8 *digest,
Michael Catanzaro f52f413
+                            gsize   digest_len)
Michael Catanzaro f52f413
 {
Michael Catanzaro f52f413
   gsize i, len = digest_len * 2;
Michael Catanzaro f52f413
   gchar *retval;
Michael Catanzaro f52f413
@@ -194,6 +194,7 @@ digest_to_string (guint8 *digest,
Michael Catanzaro f52f413
 
Michael Catanzaro f52f413
   return retval;
Michael Catanzaro f52f413
 }
Michael Catanzaro f52f413
+#define digest_to_string gchecksum_digest_to_string
Michael Catanzaro f52f413
 
Michael Catanzaro f52f413
 /*
Michael Catanzaro f52f413
  * MD5 Checksum
Michael Catanzaro f52f413
diff --git a/glib/gchecksumprivate.h b/glib/gchecksumprivate.h
Michael Catanzaro f52f413
new file mode 100644
Michael Catanzaro f52f413
index 000000000..86c7a3b61
Michael Catanzaro f52f413
--- /dev/null
Michael Catanzaro f52f413
+++ b/glib/gchecksumprivate.h
Michael Catanzaro f52f413
@@ -0,0 +1,32 @@
Michael Catanzaro f52f413
+/* gstdioprivate.h - Private GLib stdio functions
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * Copyright 2017 Руслан Ижбулатов
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * This library is free software; you can redistribute it and/or
Michael Catanzaro f52f413
+ * modify it under the terms of the GNU Lesser General Public
Michael Catanzaro f52f413
+ * License as published by the Free Software Foundation; either
Michael Catanzaro f52f413
+ * version 2.1 of the License, or (at your option) any later version.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * This library is distributed in the hope that it will be useful,
Michael Catanzaro f52f413
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Michael Catanzaro f52f413
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Michael Catanzaro f52f413
+ * Lesser General Public License for more details.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * You should have received a copy of the GNU Lesser General Public License
Michael Catanzaro f52f413
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
Michael Catanzaro f52f413
+ */
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+#ifndef __G_CHECKSUMPRIVATE_H__
Michael Catanzaro f52f413
+#define __G_CHECKSUMPRIVATE_H__
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+#include "gchecksum.h"
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+G_BEGIN_DECLS
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+gchar *
Michael Catanzaro f52f413
+gchecksum_digest_to_string (guint8 *digest,
Michael Catanzaro f52f413
+                            gsize   digest_len);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+G_END_DECLS
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+#endif
Michael Catanzaro f52f413
\ No newline at end of file
Michael Catanzaro f52f413
diff --git a/glib/ghmac-gnutls.c b/glib/ghmac-gnutls.c
Michael Catanzaro f52f413
new file mode 100644
Michael Catanzaro f52f413
index 000000000..f1a74a849
Michael Catanzaro f52f413
--- /dev/null
Michael Catanzaro f52f413
+++ b/glib/ghmac-gnutls.c
Michael Catanzaro f52f413
@@ -0,0 +1,164 @@
Michael Catanzaro f52f413
+/* ghmac.h - data hashing functions
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * Copyright (C) 2011  Collabora Ltd.
Michael Catanzaro f52f413
+ * Copyright (C) 2019  Red Hat, Inc.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * This library is free software; you can redistribute it and/or
Michael Catanzaro f52f413
+ * modify it under the terms of the GNU Lesser General Public
Michael Catanzaro f52f413
+ * License as published by the Free Software Foundation; either
Michael Catanzaro f52f413
+ * version 2.1 of the License, or (at your option) any later version.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * This library is distributed in the hope that it will be useful,
Michael Catanzaro f52f413
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Michael Catanzaro f52f413
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Michael Catanzaro f52f413
+ * Lesser General Public License for more details.
Michael Catanzaro f52f413
+ *
Michael Catanzaro f52f413
+ * You should have received a copy of the GNU Lesser General Public License
Michael Catanzaro f52f413
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
Michael Catanzaro f52f413
+ */
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+#include "config.h"
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+#include <string.h>
Michael Catanzaro f52f413
+#include <gnutls/crypto.h>
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+#include "ghmac.h"
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+#include "glib/galloca.h"
Michael Catanzaro f52f413
+#include "gatomic.h"
Michael Catanzaro f52f413
+#include "gslice.h"
Michael Catanzaro f52f413
+#include "gmem.h"
Michael Catanzaro f52f413
+#include "gstrfuncs.h"
Michael Catanzaro f52f413
+#include "gchecksumprivate.h"
Michael Catanzaro f52f413
+#include "gtestutils.h"
Michael Catanzaro f52f413
+#include "gtypes.h"
Michael Catanzaro f52f413
+#include "glibintl.h"
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+#ifndef HAVE_GNUTLS
Michael Catanzaro f52f413
+#error "build configuration error"
Michael Catanzaro f52f413
+#endif
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+struct _GHmac
Michael Catanzaro f52f413
+{
Michael Catanzaro f52f413
+  int ref_count;
Michael Catanzaro f52f413
+  GChecksumType digest_type;
Michael Catanzaro f52f413
+  gnutls_hmac_hd_t hmac;
Michael Catanzaro f52f413
+  gchar *digest_str;
Michael Catanzaro f52f413
+};
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+GHmac *
Michael Catanzaro f52f413
+g_hmac_new (GChecksumType  digest_type,
Michael Catanzaro f52f413
+            const guchar  *key,
Michael Catanzaro f52f413
+            gsize          key_len)
Michael Catanzaro f52f413
+{
Michael Catanzaro f52f413
+  gnutls_mac_algorithm_t algo;
Michael Catanzaro f52f413
+  GHmac *hmac = g_slice_new0 (GHmac);
Michael Catanzaro f52f413
+  hmac->ref_count = 1;
Michael Catanzaro f52f413
+  hmac->digest_type = digest_type;  
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  switch (digest_type)
Michael Catanzaro f52f413
+    {
Michael Catanzaro f52f413
+    case G_CHECKSUM_MD5:
Michael Catanzaro f52f413
+      algo = GNUTLS_MAC_MD5;
Michael Catanzaro f52f413
+      break;
Michael Catanzaro f52f413
+    case G_CHECKSUM_SHA1:
Michael Catanzaro f52f413
+      algo = GNUTLS_MAC_SHA1;
Michael Catanzaro f52f413
+      break;
Michael Catanzaro f52f413
+    case G_CHECKSUM_SHA256:
Michael Catanzaro f52f413
+      algo = GNUTLS_MAC_SHA256;
Michael Catanzaro f52f413
+      break;
Michael Catanzaro f52f413
+    case G_CHECKSUM_SHA384:
Michael Catanzaro f52f413
+      algo = GNUTLS_MAC_SHA384;
Michael Catanzaro f52f413
+      break;
Michael Catanzaro f52f413
+    case G_CHECKSUM_SHA512:
Michael Catanzaro f52f413
+      algo = GNUTLS_MAC_SHA512;
Michael Catanzaro f52f413
+      break;
Michael Catanzaro f52f413
+    default:
Michael Catanzaro f52f413
+      g_return_val_if_reached (NULL);
Michael Catanzaro f52f413
+    }
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  gnutls_hmac_init (&hmac->hmac, algo, key, key_len);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  return hmac;
Michael Catanzaro f52f413
+}
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+GHmac *
Michael Catanzaro f52f413
+g_hmac_copy (const GHmac *hmac)
Michael Catanzaro f52f413
+{
Michael Catanzaro f52f413
+  GHmac *copy;
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  g_return_val_if_fail (hmac != NULL, NULL);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  copy = g_slice_new0 (GHmac);
Michael Catanzaro f52f413
+  copy->ref_count = 1;
Michael Catanzaro f52f413
+  copy->digest_type = hmac->digest_type;
Michael Catanzaro f52f413
+  copy->hmac = gnutls_hmac_copy (hmac->hmac);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  return copy;
Michael Catanzaro f52f413
+}
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+GHmac *
Michael Catanzaro f52f413
+g_hmac_ref (GHmac *hmac)
Michael Catanzaro f52f413
+{
Michael Catanzaro f52f413
+  g_return_val_if_fail (hmac != NULL, NULL);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  g_atomic_int_inc (&hmac->ref_count);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  return hmac;
Michael Catanzaro f52f413
+}
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+void
Michael Catanzaro f52f413
+g_hmac_unref (GHmac *hmac)
Michael Catanzaro f52f413
+{
Michael Catanzaro f52f413
+  g_return_if_fail (hmac != NULL);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  if (g_atomic_int_dec_and_test (&hmac->ref_count))
Michael Catanzaro f52f413
+    {
Michael Catanzaro f52f413
+      gnutls_hmac_deinit (hmac->hmac, NULL);
Michael Catanzaro f52f413
+      g_free (hmac->digest_str);
Michael Catanzaro f52f413
+      g_slice_free (GHmac, hmac);
Michael Catanzaro f52f413
+    }
Michael Catanzaro f52f413
+}
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+void
Michael Catanzaro f52f413
+g_hmac_update (GHmac        *hmac,
Michael Catanzaro f52f413
+               const guchar *data,
Michael Catanzaro f52f413
+               gssize        length)
Michael Catanzaro f52f413
+{
Michael Catanzaro f52f413
+  g_return_if_fail (hmac != NULL);
Michael Catanzaro f52f413
+  g_return_if_fail (length == 0 || data != NULL);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  gnutls_hmac (hmac->hmac, data, length);
Michael Catanzaro f52f413
+}
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+const gchar *
Michael Catanzaro f52f413
+g_hmac_get_string (GHmac *hmac)
Michael Catanzaro f52f413
+{
Michael Catanzaro f52f413
+  guint8 *buffer;
Michael Catanzaro f52f413
+  gsize digest_len;
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  g_return_val_if_fail (hmac != NULL, NULL);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  if (hmac->digest_str)
Michael Catanzaro f52f413
+    return hmac->digest_str;
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  digest_len = g_checksum_type_get_length (hmac->digest_type);
Michael Catanzaro f52f413
+  buffer = g_alloca (digest_len);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  gnutls_hmac_output (hmac->hmac, buffer);
Michael Catanzaro f52f413
+  hmac->digest_str = gchecksum_digest_to_string (buffer, digest_len);
Michael Catanzaro f52f413
+  return hmac->digest_str;
Michael Catanzaro f52f413
+}
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+void
Michael Catanzaro f52f413
+g_hmac_get_digest (GHmac  *hmac,
Michael Catanzaro f52f413
+                   guint8 *buffer,
Michael Catanzaro f52f413
+                   gsize  *digest_len)
Michael Catanzaro f52f413
+{
Michael Catanzaro f52f413
+  g_return_if_fail (hmac != NULL);
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
+  gnutls_hmac_output (hmac->hmac, buffer);
Michael Catanzaro f52f413
+  *digest_len = g_checksum_type_get_length (hmac->digest_type);
Michael Catanzaro f52f413
+}
Michael Catanzaro f52f413
diff --git a/glib/ghmac.c b/glib/ghmac.c
Michael Catanzaro f52f413
index 4f181f21f..c62d9ce4e 100644
Michael Catanzaro f52f413
--- a/glib/ghmac.c
Michael Catanzaro f52f413
+++ b/glib/ghmac.c
Michael Catanzaro f52f413
@@ -33,6 +33,9 @@
Michael Catanzaro f52f413
 #include "gtypes.h"
Michael Catanzaro f52f413
 #include "glibintl.h"
Michael Catanzaro f52f413
 
Michael Catanzaro f52f413
+#ifdef HAVE_GNUTLS
Michael Catanzaro f52f413
+#error "build configuration error"
Michael Catanzaro f52f413
+#endif
Michael Catanzaro f52f413
 
Michael Catanzaro f52f413
 /**
Michael Catanzaro f52f413
  * SECTION:hmac
Michael Catanzaro f52f413
diff --git a/glib/meson.build b/glib/meson.build
2165f81
index 329b8d197..2942a7e9b 100644
Michael Catanzaro f52f413
--- a/glib/meson.build
Michael Catanzaro f52f413
+++ b/glib/meson.build
Michael Catanzaro f52f413
@@ -252,7 +252,6 @@ glib_sources = files(
Michael Catanzaro f52f413
   'gfileutils.c',
Michael Catanzaro f52f413
   'ggettext.c',
Michael Catanzaro f52f413
   'ghash.c',
Michael Catanzaro f52f413
-  'ghmac.c',
Michael Catanzaro f52f413
   'ghmac-utils.c',
Michael Catanzaro f52f413
   'ghook.c',
Michael Catanzaro f52f413
   'ghostutils.c',
Michael Catanzaro f52f413
@@ -308,6 +307,7 @@ glib_sources = files(
Michael Catanzaro f52f413
   'guriprivate.h',
Michael Catanzaro f52f413
   'gutils.c',
Michael Catanzaro f52f413
   'gutilsprivate.h',
Michael Catanzaro f52f413
+  'gchecksumprivate.h',
Michael Catanzaro f52f413
   'guuid.c',
Michael Catanzaro f52f413
   'gvariant.c',
Michael Catanzaro f52f413
   'gvariant-core.c',
Michael Catanzaro f52f413
@@ -352,6 +352,12 @@ else
Michael Catanzaro f52f413
   glib_dtrace_hdr = []
Michael Catanzaro f52f413
 endif
Michael Catanzaro f52f413
 
Michael Catanzaro f52f413
+if get_option('gnutls')
Michael Catanzaro f52f413
+  glib_sources += files('ghmac-gnutls.c')
Michael Catanzaro f52f413
+else
Michael Catanzaro f52f413
+  glib_sources += files('ghmac.c')
Michael Catanzaro f52f413
+endif
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
 pcre_static_args = []
Michael Catanzaro f52f413
 
Michael Catanzaro f52f413
 if use_pcre_static_flag
Michael Catanzaro f52f413
@@ -378,7 +384,7 @@ libglib = library('glib-2.0',
Michael Catanzaro f52f413
   # intl.lib is not compatible with SAFESEH
Michael Catanzaro f52f413
   link_args : [noseh_link_args, glib_link_flags, win32_ldflags],
Michael Catanzaro f52f413
   include_directories : configinc,
2165f81
-  dependencies : pcre_deps + [thread_dep, librt] + libintl_deps + libiconv + platform_deps + [gnulib_libm_dependency, libm] + [libsysprof_capture_dep],
2165f81
+  dependencies : pcre_deps + libgnutls_dep + [thread_dep, librt] + libintl_deps + libiconv + platform_deps + [gnulib_libm_dependency, libm] + [libsysprof_capture_dep],
Michael Catanzaro f52f413
   c_args : glib_c_args,
Michael Catanzaro f52f413
   objc_args : glib_c_args,
Michael Catanzaro f52f413
 )
Michael Catanzaro f52f413
diff --git a/meson.build b/meson.build
2165f81
index 0d892fb2d..091029fea 100644
Michael Catanzaro f52f413
--- a/meson.build
Michael Catanzaro f52f413
+++ b/meson.build
2165f81
@@ -2078,6 +2078,13 @@ if host_system == 'linux'
Michael Catanzaro f52f413
   glib_conf.set('HAVE_LIBMOUNT', libmount_dep.found())
Michael Catanzaro f52f413
 endif
Michael Catanzaro f52f413
 
Michael Catanzaro f52f413
+# gnutls is used optionally by ghmac
Michael Catanzaro f52f413
+libgnutls_dep = []
Michael Catanzaro f52f413
+if get_option('gnutls')
Michael Catanzaro f52f413
+  libgnutls_dep = [dependency('gnutls', version : '>=3.6.9', required : true)]
Michael Catanzaro f52f413
+  glib_conf.set('HAVE_GNUTLS', 1)
Michael Catanzaro f52f413
+endif
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
 if host_system == 'windows'
Michael Catanzaro f52f413
   winsock2 = cc.find_library('ws2_32')
Michael Catanzaro f52f413
 endif
Michael Catanzaro f52f413
diff --git a/meson_options.txt b/meson_options.txt
2165f81
index 072765361..d2370042f 100644
Michael Catanzaro f52f413
--- a/meson_options.txt
Michael Catanzaro f52f413
+++ b/meson_options.txt
Michael Catanzaro f52f413
@@ -34,6 +34,11 @@ option('libmount',
Michael Catanzaro f52f413
        value : 'auto',
Michael Catanzaro f52f413
        description : 'build with libmount support')
Michael Catanzaro f52f413
 
Michael Catanzaro f52f413
+option('gnutls',
Michael Catanzaro f52f413
+       type : 'boolean',
Michael Catanzaro a6c3663
+       value : false,
Michael Catanzaro f52f413
+       description : 'build with gnutls support')
Michael Catanzaro f52f413
+
Michael Catanzaro f52f413
 option('internal_pcre',
Michael Catanzaro f52f413
        type : 'boolean',
Michael Catanzaro f52f413
        value : false,
Michael Catanzaro f52f413
-- 
2165f81
2.29.2
2165f81