6fcf6b
From c5cc0bb6f2d6e468c7402915a0a4e6799f0febdf Mon Sep 17 00:00:00 2001
d1ed09
From: Colin Walters <walters@verbum.org>
d1ed09
Date: Fri, 7 Jun 2019 18:44:43 +0000
6fcf6b
Subject: [PATCH 1/3] ghmac: Split off wrapper functions into ghmac-utils.c
d1ed09
d1ed09
Prep for adding a GnuTLS HMAC implementation; these are just
d1ed09
utility functions that call the "core" API.
d1ed09
---
d1ed09
 glib/Makefile.am   |   1 +
d1ed09
 glib/ghmac-utils.c | 145 +++++++++++++++++++++++++++++++++++++++++++++
d1ed09
 glib/ghmac.c       | 112 ----------------------------------
d1ed09
 glib/meson.build   |   1 +
d1ed09
 4 files changed, 147 insertions(+), 112 deletions(-)
d1ed09
 create mode 100644 glib/ghmac-utils.c
d1ed09
d1ed09
diff --git a/glib/Makefile.am b/glib/Makefile.am
6fcf6b
index c0c3b92f0..43fa17051 100644
d1ed09
--- a/glib/Makefile.am
d1ed09
+++ b/glib/Makefile.am
d1ed09
@@ -126,6 +126,7 @@ libglib_2_0_la_SOURCES = 	\
d1ed09
 	ggettext.c		\
d1ed09
 	ghash.c			\
d1ed09
 	ghmac.c			\
d1ed09
+	ghmac-utils.c		\
d1ed09
 	ghook.c			\
d1ed09
 	ghostutils.c		\
d1ed09
 	giochannel.c    	\
d1ed09
diff --git a/glib/ghmac-utils.c b/glib/ghmac-utils.c
d1ed09
new file mode 100644
d1ed09
index 000000000..a17359ff1
d1ed09
--- /dev/null
d1ed09
+++ b/glib/ghmac-utils.c
d1ed09
@@ -0,0 +1,145 @@
d1ed09
+/* ghmac.h - data hashing functions
d1ed09
+ *
d1ed09
+ * Copyright (C) 2011  Collabora Ltd.
d1ed09
+ * Copyright (C) 2019  Red Hat, Inc.
d1ed09
+ *
d1ed09
+ * This library is free software; you can redistribute it and/or
d1ed09
+ * modify it under the terms of the GNU Lesser General Public
d1ed09
+ * License as published by the Free Software Foundation; either
d1ed09
+ * version 2.1 of the License, or (at your option) any later version.
d1ed09
+ *
d1ed09
+ * This library is distributed in the hope that it will be useful,
d1ed09
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
d1ed09
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d1ed09
+ * Lesser General Public License for more details.
d1ed09
+ *
d1ed09
+ * You should have received a copy of the GNU Lesser General Public License
d1ed09
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
d1ed09
+ */
d1ed09
+
d1ed09
+#include "config.h"
d1ed09
+
d1ed09
+#include <string.h>
d1ed09
+
d1ed09
+#include "ghmac.h"
d1ed09
+
d1ed09
+#include "glib/galloca.h"
d1ed09
+#include "gatomic.h"
d1ed09
+#include "gslice.h"
d1ed09
+#include "gmem.h"
d1ed09
+#include "gstrfuncs.h"
d1ed09
+#include "gtestutils.h"
d1ed09
+#include "gtypes.h"
d1ed09
+#include "glibintl.h"
d1ed09
+
d1ed09
+/**
d1ed09
+ * g_compute_hmac_for_data:
d1ed09
+ * @digest_type: a #GChecksumType to use for the HMAC
d1ed09
+ * @key: (array length=key_len): the key to use in the HMAC
d1ed09
+ * @key_len: the length of the key
d1ed09
+ * @data: (array length=length): binary blob to compute the HMAC of
d1ed09
+ * @length: length of @data
d1ed09
+ *
d1ed09
+ * Computes the HMAC for a binary @data of @length. This is a
d1ed09
+ * convenience wrapper for g_hmac_new(), g_hmac_get_string()
d1ed09
+ * and g_hmac_unref().
d1ed09
+ *
d1ed09
+ * The hexadecimal string returned will be in lower case.
d1ed09
+ *
d1ed09
+ * Returns: the HMAC of the binary data as a string in hexadecimal.
d1ed09
+ *   The returned string should be freed with g_free() when done using it.
d1ed09
+ *
d1ed09
+ * Since: 2.30
d1ed09
+ */
d1ed09
+gchar *
d1ed09
+g_compute_hmac_for_data (GChecksumType  digest_type,
d1ed09
+                         const guchar  *key,
d1ed09
+                         gsize          key_len,
d1ed09
+                         const guchar  *data,
d1ed09
+                         gsize          length)
d1ed09
+{
d1ed09
+  GHmac *hmac;
d1ed09
+  gchar *retval;
d1ed09
+
d1ed09
+  g_return_val_if_fail (length == 0 || data != NULL, NULL);
d1ed09
+
d1ed09
+  hmac = g_hmac_new (digest_type, key, key_len);
d1ed09
+  if (!hmac)
d1ed09
+    return NULL;
d1ed09
+
d1ed09
+  g_hmac_update (hmac, data, length);
d1ed09
+  retval = g_strdup (g_hmac_get_string (hmac));
d1ed09
+  g_hmac_unref (hmac);
d1ed09
+
d1ed09
+  return retval;
d1ed09
+}
d1ed09
+
d1ed09
+/**
d1ed09
+ * g_compute_hmac_for_bytes:
d1ed09
+ * @digest_type: a #GChecksumType to use for the HMAC
d1ed09
+ * @key: the key to use in the HMAC
d1ed09
+ * @data: binary blob to compute the HMAC of
d1ed09
+ *
d1ed09
+ * Computes the HMAC for a binary @data. This is a
d1ed09
+ * convenience wrapper for g_hmac_new(), g_hmac_get_string()
d1ed09
+ * and g_hmac_unref().
d1ed09
+ *
d1ed09
+ * The hexadecimal string returned will be in lower case.
d1ed09
+ *
d1ed09
+ * Returns: the HMAC of the binary data as a string in hexadecimal.
d1ed09
+ *   The returned string should be freed with g_free() when done using it.
d1ed09
+ *
d1ed09
+ * Since: 2.50
d1ed09
+ */
d1ed09
+gchar *
d1ed09
+g_compute_hmac_for_bytes (GChecksumType  digest_type,
d1ed09
+                          GBytes        *key,
d1ed09
+                          GBytes        *data)
d1ed09
+{
d1ed09
+  gconstpointer byte_data;
d1ed09
+  gsize length;
d1ed09
+  gconstpointer key_data;
d1ed09
+  gsize key_len;
d1ed09
+
d1ed09
+  g_return_val_if_fail (data != NULL, NULL);
d1ed09
+  g_return_val_if_fail (key != NULL, NULL);
d1ed09
+
d1ed09
+  byte_data = g_bytes_get_data (data, &length);
d1ed09
+  key_data = g_bytes_get_data (key, &key_len);
d1ed09
+  return g_compute_hmac_for_data (digest_type, key_data, key_len, byte_data, length);
d1ed09
+}
d1ed09
+
d1ed09
+
d1ed09
+/**
d1ed09
+ * g_compute_hmac_for_string:
d1ed09
+ * @digest_type: a #GChecksumType to use for the HMAC
d1ed09
+ * @key: (array length=key_len): the key to use in the HMAC
d1ed09
+ * @key_len: the length of the key
d1ed09
+ * @str: the string to compute the HMAC for
d1ed09
+ * @length: the length of the string, or -1 if the string is nul-terminated
d1ed09
+ *
d1ed09
+ * Computes the HMAC for a string.
d1ed09
+ *
d1ed09
+ * The hexadecimal string returned will be in lower case.
d1ed09
+ *
d1ed09
+ * Returns: the HMAC as a hexadecimal string.
d1ed09
+ *     The returned string should be freed with g_free()
d1ed09
+ *     when done using it.
d1ed09
+ *
d1ed09
+ * Since: 2.30
d1ed09
+ */
d1ed09
+gchar *
d1ed09
+g_compute_hmac_for_string (GChecksumType  digest_type,
d1ed09
+                           const guchar  *key,
d1ed09
+                           gsize          key_len,
d1ed09
+                           const gchar   *str,
d1ed09
+                           gssize         length)
d1ed09
+{
d1ed09
+  g_return_val_if_fail (length == 0 || str != NULL, NULL);
d1ed09
+
d1ed09
+  if (length < 0)
d1ed09
+    length = strlen (str);
d1ed09
+
d1ed09
+  return g_compute_hmac_for_data (digest_type, key, key_len,
d1ed09
+                                  (const guchar *) str, length);
d1ed09
+}
d1ed09
diff --git a/glib/ghmac.c b/glib/ghmac.c
d1ed09
index 9b58fd81c..7db38e34a 100644
d1ed09
--- a/glib/ghmac.c
d1ed09
+++ b/glib/ghmac.c
d1ed09
@@ -329,115 +329,3 @@ g_hmac_get_digest (GHmac  *hmac,
d1ed09
   g_checksum_update (hmac->digesto, buffer, len);
d1ed09
   g_checksum_get_digest (hmac->digesto, buffer, digest_len);
d1ed09
 }
d1ed09
-
d1ed09
-/**
d1ed09
- * g_compute_hmac_for_data:
d1ed09
- * @digest_type: a #GChecksumType to use for the HMAC
d1ed09
- * @key: (array length=key_len): the key to use in the HMAC
d1ed09
- * @key_len: the length of the key
d1ed09
- * @data: (array length=length): binary blob to compute the HMAC of
d1ed09
- * @length: length of @data
d1ed09
- *
d1ed09
- * Computes the HMAC for a binary @data of @length. This is a
d1ed09
- * convenience wrapper for g_hmac_new(), g_hmac_get_string()
d1ed09
- * and g_hmac_unref().
d1ed09
- *
d1ed09
- * The hexadecimal string returned will be in lower case.
d1ed09
- *
d1ed09
- * Returns: the HMAC of the binary data as a string in hexadecimal.
d1ed09
- *   The returned string should be freed with g_free() when done using it.
d1ed09
- *
d1ed09
- * Since: 2.30
d1ed09
- */
d1ed09
-gchar *
d1ed09
-g_compute_hmac_for_data (GChecksumType  digest_type,
d1ed09
-                         const guchar  *key,
d1ed09
-                         gsize          key_len,
d1ed09
-                         const guchar  *data,
d1ed09
-                         gsize          length)
d1ed09
-{
d1ed09
-  GHmac *hmac;
d1ed09
-  gchar *retval;
d1ed09
-
d1ed09
-  g_return_val_if_fail (length == 0 || data != NULL, NULL);
d1ed09
-
d1ed09
-  hmac = g_hmac_new (digest_type, key, key_len);
d1ed09
-  if (!hmac)
d1ed09
-    return NULL;
d1ed09
-
d1ed09
-  g_hmac_update (hmac, data, length);
d1ed09
-  retval = g_strdup (g_hmac_get_string (hmac));
d1ed09
-  g_hmac_unref (hmac);
d1ed09
-
d1ed09
-  return retval;
d1ed09
-}
d1ed09
-
d1ed09
-/**
d1ed09
- * g_compute_hmac_for_bytes:
d1ed09
- * @digest_type: a #GChecksumType to use for the HMAC
d1ed09
- * @key: the key to use in the HMAC
d1ed09
- * @data: binary blob to compute the HMAC of
d1ed09
- *
d1ed09
- * Computes the HMAC for a binary @data. This is a
d1ed09
- * convenience wrapper for g_hmac_new(), g_hmac_get_string()
d1ed09
- * and g_hmac_unref().
d1ed09
- *
d1ed09
- * The hexadecimal string returned will be in lower case.
d1ed09
- *
d1ed09
- * Returns: the HMAC of the binary data as a string in hexadecimal.
d1ed09
- *   The returned string should be freed with g_free() when done using it.
d1ed09
- *
d1ed09
- * Since: 2.50
d1ed09
- */
d1ed09
-gchar *
d1ed09
-g_compute_hmac_for_bytes (GChecksumType  digest_type,
d1ed09
-                          GBytes        *key,
d1ed09
-                          GBytes        *data)
d1ed09
-{
d1ed09
-  gconstpointer byte_data;
d1ed09
-  gsize length;
d1ed09
-  gconstpointer key_data;
d1ed09
-  gsize key_len;
d1ed09
-
d1ed09
-  g_return_val_if_fail (data != NULL, NULL);
d1ed09
-  g_return_val_if_fail (key != NULL, NULL);
d1ed09
-
d1ed09
-  byte_data = g_bytes_get_data (data, &length);
d1ed09
-  key_data = g_bytes_get_data (key, &key_len);
d1ed09
-  return g_compute_hmac_for_data (digest_type, key_data, key_len, byte_data, length);
d1ed09
-}
d1ed09
-
d1ed09
-
d1ed09
-/**
d1ed09
- * g_compute_hmac_for_string:
d1ed09
- * @digest_type: a #GChecksumType to use for the HMAC
d1ed09
- * @key: (array length=key_len): the key to use in the HMAC
d1ed09
- * @key_len: the length of the key
d1ed09
- * @str: the string to compute the HMAC for
d1ed09
- * @length: the length of the string, or -1 if the string is nul-terminated
d1ed09
- *
d1ed09
- * Computes the HMAC for a string.
d1ed09
- *
d1ed09
- * The hexadecimal string returned will be in lower case.
d1ed09
- *
d1ed09
- * Returns: the HMAC as a hexadecimal string.
d1ed09
- *     The returned string should be freed with g_free()
d1ed09
- *     when done using it.
d1ed09
- *
d1ed09
- * Since: 2.30
d1ed09
- */
d1ed09
-gchar *
d1ed09
-g_compute_hmac_for_string (GChecksumType  digest_type,
d1ed09
-                           const guchar  *key,
d1ed09
-                           gsize          key_len,
d1ed09
-                           const gchar   *str,
d1ed09
-                           gssize         length)
d1ed09
-{
d1ed09
-  g_return_val_if_fail (length == 0 || str != NULL, NULL);
d1ed09
-
d1ed09
-  if (length < 0)
d1ed09
-    length = strlen (str);
d1ed09
-
d1ed09
-  return g_compute_hmac_for_data (digest_type, key, key_len,
d1ed09
-                                  (const guchar *) str, length);
d1ed09
-}
d1ed09
diff --git a/glib/meson.build b/glib/meson.build
6fcf6b
index c81e99f9c..306a67f13 100644
d1ed09
--- a/glib/meson.build
d1ed09
+++ b/glib/meson.build
d1ed09
@@ -138,6 +138,7 @@ glib_sources = files(
d1ed09
   'ggettext.c',
d1ed09
   'ghash.c',
d1ed09
   'ghmac.c',
d1ed09
+  'ghmac-utils.c',
d1ed09
   'ghook.c',
d1ed09
   'ghostutils.c',
d1ed09
   'giochannel.c',
d1ed09
-- 
6fcf6b
2.31.1
d1ed09
6fcf6b
From 3befcf1eb31e0fa7a988b22a9c24240218cd4744 Mon Sep 17 00:00:00 2001
d1ed09
From: Colin Walters <walters@verbum.org>
d1ed09
Date: Fri, 7 Jun 2019 19:36:54 +0000
6fcf6b
Subject: [PATCH 2/3] Add a gnutls backend for GHmac
d1ed09
d1ed09
For RHEL we want apps to use FIPS-certified crypto libraries,
d1ed09
and HMAC apparently counts as "keyed" and hence needs to
d1ed09
be validated.
d1ed09
d1ed09
Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1630260
d1ed09
Replaces: https://gitlab.gnome.org/GNOME/glib/merge_requests/897
d1ed09
d1ed09
This is a build-time option that backs the GHmac API with GnuTLS.
d1ed09
Most distributors ship glib-networking built with GnuTLS, and
d1ed09
most apps use glib-networking, so this isn't a net-new library
d1ed09
in most cases.
d1ed09
6fcf6b
=======================================================================
6fcf6b
6fcf6b
mcatanzaro note:
6fcf6b
6fcf6b
I've updated Colin's original patch with several enhancements:
6fcf6b
6fcf6b
Implement g_hmac_copy() using gnutls_hmac_copy(), which didn't exist
6fcf6b
when Colin developed this patch.
6fcf6b
6fcf6b
Removed use of GSlice
6fcf6b
6fcf6b
Better error checking in g_hmac_new(). It is possible for
6fcf6b
gnutls_hmac_init() to fail if running in FIPS mode and an MD5 digest is
6fcf6b
requested. In this case, we should return NULL rather than returning a
6fcf6b
broken GHmac with a NULL gnutls_hmac_hd_t. This was leading to a later
6fcf6b
null pointer dereference inside gnutls_hmac_update(). Applications are
6fcf6b
responsible for checking to ensure the return value of g_hmac_new() is
6fcf6b
not NULL since it is annotated as nullable. Added documentation to
6fcf6b
indicate this possibility.
6fcf6b
6fcf6b
Properly handle length -1 in g_hmac_update(). This means we've been
6fcf6b
given a NUL-terminated string and should use strlen(). GnuTLS doesn't
6fcf6b
accept -1, so let's call strlen() ourselves.
6fcf6b
6fcf6b
Crash the application with g_error() if gnutls_hmac() fails for any
6fcf6b
reason. This is necessary because g_hmac_update() is not fallible, so we
6fcf6b
have no way to indicate error. Crashing seems better than returning the
6fcf6b
wrong result later when g_hmac_get_string() or g_hmac_get_digest() is
6fcf6b
later called. (Those functions are also not fallible.) Fortunately, I
6fcf6b
don't think this error should actually be hit in practice.
6fcf6b
6fcf6b
https://gitlab.gnome.org/GNOME/glib/-/merge_requests/903
d1ed09
---
6fcf6b
 glib/Makefile.am        |   8 +-
6fcf6b
 glib/gchecksum.c        |   9 +-
6fcf6b
 glib/gchecksumprivate.h |  32 +++++++
6fcf6b
 glib/ghmac-gnutls.c     | 182 ++++++++++++++++++++++++++++++++++++++++
6fcf6b
 glib/ghmac.c            |  13 +++
d1ed09
 glib/meson.build        |  10 ++-
d1ed09
 meson.build             |   7 ++
d1ed09
 meson_options.txt       |   5 ++
6fcf6b
 8 files changed, 258 insertions(+), 8 deletions(-)
d1ed09
 create mode 100644 glib/gchecksumprivate.h
d1ed09
 create mode 100644 glib/ghmac-gnutls.c
d1ed09
d1ed09
diff --git a/glib/Makefile.am b/glib/Makefile.am
6fcf6b
index 43fa17051..1175bbe40 100644
d1ed09
--- a/glib/Makefile.am
d1ed09
+++ b/glib/Makefile.am
d1ed09
@@ -125,7 +125,7 @@ libglib_2_0_la_SOURCES = 	\
d1ed09
 	gfileutils.c		\
d1ed09
 	ggettext.c		\
d1ed09
 	ghash.c			\
d1ed09
-	ghmac.c			\
d1ed09
+	ghmac-gnutls.c		\
d1ed09
 	ghmac-utils.c		\
d1ed09
 	ghook.c			\
d1ed09
 	ghostutils.c		\
6fcf6b
@@ -352,11 +352,15 @@ pcre_lib = pcre/libpcre.la
d1ed09
 pcre_inc =
d1ed09
 endif
d1ed09
 
d1ed09
-libglib_2_0_la_CFLAGS = $(AM_CFLAGS) $(GLIB_HIDDEN_VISIBILITY_CFLAGS) $(LIBSYSTEMD_CFLAGS)
d1ed09
+gnutls_libs = $(shell pkg-config --libs gnutls)
d1ed09
+gnutls_cflags = $(shell pkg-config --cflags gnutls)
d1ed09
+
d1ed09
+libglib_2_0_la_CFLAGS = $(AM_CFLAGS) $(GLIB_HIDDEN_VISIBILITY_CFLAGS) $(LIBSYSTEMD_CFLAGS) $(gnutls_cflags)
d1ed09
 libglib_2_0_la_LIBADD = libcharset/libcharset.la $(printf_la) @GIO@ @GSPAWN@ @PLATFORMDEP@ @ICONV_LIBS@ @G_LIBS_EXTRA@ $(pcre_lib) $(G_THREAD_LIBS_EXTRA) $(G_THREAD_LIBS_FOR_GTHREAD) $(LIBSYSTEMD_LIBS)
d1ed09
 libglib_2_0_la_DEPENDENCIES = libcharset/libcharset.la $(printf_la) @GIO@ @GSPAWN@ @PLATFORMDEP@ $(glib_win32_res) $(glib_def)
d1ed09
 
6fcf6b
 libglib_2_0_la_LDFLAGS = $(GLIB_LINK_FLAGS) \
6fcf6b
+         $(gnutls_libs) \
d1ed09
 	 $(glib_win32_res_ldflag) \
d1ed09
 	-version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) \
d1ed09
 	-export-dynamic $(no_undefined)
d1ed09
diff --git a/glib/gchecksum.c b/glib/gchecksum.c
d1ed09
index 40b1d50e2..2f59d4a66 100644
d1ed09
--- a/glib/gchecksum.c
d1ed09
+++ b/glib/gchecksum.c
d1ed09
@@ -20,7 +20,7 @@
d1ed09
 
d1ed09
 #include <string.h>
d1ed09
 
d1ed09
-#include "gchecksum.h"
d1ed09
+#include "gchecksumprivate.h"
d1ed09
 
d1ed09
 #include "gslice.h"
d1ed09
 #include "gmem.h"
d1ed09
@@ -173,9 +173,9 @@ sha_byte_reverse (guint32 *buffer,
d1ed09
 }
d1ed09
 #endif /* G_BYTE_ORDER == G_BIG_ENDIAN */
d1ed09
 
d1ed09
-static gchar *
d1ed09
-digest_to_string (guint8 *digest,
d1ed09
-                  gsize   digest_len)
d1ed09
+gchar *
d1ed09
+gchecksum_digest_to_string (guint8 *digest,
d1ed09
+                            gsize   digest_len)
d1ed09
 {
d1ed09
   gint len = digest_len * 2;
d1ed09
   gint i;
d1ed09
@@ -195,6 +195,7 @@ digest_to_string (guint8 *digest,
d1ed09
 
d1ed09
   return retval;
d1ed09
 }
d1ed09
+#define digest_to_string gchecksum_digest_to_string
d1ed09
 
d1ed09
 /*
d1ed09
  * MD5 Checksum
d1ed09
diff --git a/glib/gchecksumprivate.h b/glib/gchecksumprivate.h
d1ed09
new file mode 100644
d1ed09
index 000000000..86c7a3b61
d1ed09
--- /dev/null
d1ed09
+++ b/glib/gchecksumprivate.h
d1ed09
@@ -0,0 +1,32 @@
d1ed09
+/* gstdioprivate.h - Private GLib stdio functions
d1ed09
+ *
d1ed09
+ * Copyright 2017 Руслан Ижбулатов
d1ed09
+ *
d1ed09
+ * This library is free software; you can redistribute it and/or
d1ed09
+ * modify it under the terms of the GNU Lesser General Public
d1ed09
+ * License as published by the Free Software Foundation; either
d1ed09
+ * version 2.1 of the License, or (at your option) any later version.
d1ed09
+ *
d1ed09
+ * This library is distributed in the hope that it will be useful,
d1ed09
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
d1ed09
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d1ed09
+ * Lesser General Public License for more details.
d1ed09
+ *
d1ed09
+ * You should have received a copy of the GNU Lesser General Public License
d1ed09
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
d1ed09
+ */
d1ed09
+
d1ed09
+#ifndef __G_CHECKSUMPRIVATE_H__
d1ed09
+#define __G_CHECKSUMPRIVATE_H__
d1ed09
+
d1ed09
+#include "gchecksum.h"
d1ed09
+
d1ed09
+G_BEGIN_DECLS
d1ed09
+
d1ed09
+gchar *
d1ed09
+gchecksum_digest_to_string (guint8 *digest,
d1ed09
+                            gsize   digest_len);
d1ed09
+
d1ed09
+G_END_DECLS
d1ed09
+
d1ed09
+#endif
d1ed09
\ No newline at end of file
d1ed09
diff --git a/glib/ghmac-gnutls.c b/glib/ghmac-gnutls.c
d1ed09
new file mode 100644
6fcf6b
index 000000000..522b9b302
d1ed09
--- /dev/null
d1ed09
+++ b/glib/ghmac-gnutls.c
6fcf6b
@@ -0,0 +1,182 @@
d1ed09
+/* ghmac.h - data hashing functions
d1ed09
+ *
d1ed09
+ * Copyright (C) 2011  Collabora Ltd.
d1ed09
+ * Copyright (C) 2019  Red Hat, Inc.
d1ed09
+ *
d1ed09
+ * This library is free software; you can redistribute it and/or
d1ed09
+ * modify it under the terms of the GNU Lesser General Public
d1ed09
+ * License as published by the Free Software Foundation; either
d1ed09
+ * version 2.1 of the License, or (at your option) any later version.
d1ed09
+ *
d1ed09
+ * This library is distributed in the hope that it will be useful,
d1ed09
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
d1ed09
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d1ed09
+ * Lesser General Public License for more details.
d1ed09
+ *
d1ed09
+ * You should have received a copy of the GNU Lesser General Public License
d1ed09
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
d1ed09
+ */
d1ed09
+
d1ed09
+#include "config.h"
d1ed09
+
d1ed09
+#include <string.h>
d1ed09
+#include <gnutls/crypto.h>
d1ed09
+
d1ed09
+#include "ghmac.h"
d1ed09
+
d1ed09
+#include "glib/galloca.h"
d1ed09
+#include "gatomic.h"
d1ed09
+#include "gslice.h"
d1ed09
+#include "gmem.h"
d1ed09
+#include "gstrfuncs.h"
d1ed09
+#include "gchecksumprivate.h"
d1ed09
+#include "gtestutils.h"
d1ed09
+#include "gtypes.h"
d1ed09
+#include "glibintl.h"
d1ed09
+
d1ed09
+struct _GHmac
d1ed09
+{
d1ed09
+  int ref_count;
d1ed09
+  GChecksumType digest_type;
d1ed09
+  gnutls_hmac_hd_t hmac;
d1ed09
+  gchar *digest_str;
d1ed09
+};
d1ed09
+
d1ed09
+GHmac *
d1ed09
+g_hmac_new (GChecksumType  digest_type,
d1ed09
+            const guchar  *key,
d1ed09
+            gsize          key_len)
d1ed09
+{
d1ed09
+  gnutls_mac_algorithm_t algo;
6fcf6b
+  GHmac *hmac = g_new0 (GHmac, 1);
6fcf6b
+  int ret;
6fcf6b
+
d1ed09
+  hmac->ref_count = 1;
6fcf6b
+  hmac->digest_type = digest_type;
d1ed09
+
d1ed09
+  switch (digest_type)
d1ed09
+    {
d1ed09
+    case G_CHECKSUM_MD5:
d1ed09
+      algo = GNUTLS_MAC_MD5;
d1ed09
+      break;
d1ed09
+    case G_CHECKSUM_SHA1:
d1ed09
+      algo = GNUTLS_MAC_SHA1;
d1ed09
+      break;
d1ed09
+    case G_CHECKSUM_SHA256:
d1ed09
+      algo = GNUTLS_MAC_SHA256;
d1ed09
+      break;
d1ed09
+    case G_CHECKSUM_SHA384:
d1ed09
+      algo = GNUTLS_MAC_SHA384;
d1ed09
+      break;
d1ed09
+    case G_CHECKSUM_SHA512:
d1ed09
+      algo = GNUTLS_MAC_SHA512;
d1ed09
+      break;
d1ed09
+    default:
d1ed09
+      g_return_val_if_reached (NULL);
d1ed09
+    }
d1ed09
+
6fcf6b
+  ret = gnutls_hmac_init (&hmac->hmac, algo, key, key_len);
6fcf6b
+  if (ret != 0)
6fcf6b
+    {
6fcf6b
+      /* There is no way to report an error here, but one possible cause of
6fcf6b
+       * failure is that the requested digest may be disabled by FIPS mode.
6fcf6b
+       */
6fcf6b
+      g_free (hmac->hmac);
6fcf6b
+      return NULL;
6fcf6b
+    }
d1ed09
+
d1ed09
+  return hmac;
d1ed09
+}
d1ed09
+
d1ed09
+GHmac *
d1ed09
+g_hmac_copy (const GHmac *hmac)
d1ed09
+{
645344
+  GHmac *copy;
645344
+
645344
+  g_return_val_if_fail (hmac != NULL, NULL);
645344
+
6fcf6b
+  copy = g_new0 (GHmac, 1);
645344
+  copy->ref_count = 1;
645344
+  copy->digest_type = hmac->digest_type;
645344
+  copy->hmac = gnutls_hmac_copy (hmac->hmac);
645344
+
6fcf6b
+  /* g_hmac_copy is not allowed to fail, so we'll have to crash on error. */
6fcf6b
+  if (!copy->hmac)
6fcf6b
+    g_error ("gnutls_hmac_copy failed");
6fcf6b
+
645344
+  return copy;
d1ed09
+}
d1ed09
+
d1ed09
+GHmac *
d1ed09
+g_hmac_ref (GHmac *hmac)
d1ed09
+{
d1ed09
+  g_return_val_if_fail (hmac != NULL, NULL);
d1ed09
+
d1ed09
+  g_atomic_int_inc (&hmac->ref_count);
d1ed09
+
d1ed09
+  return hmac;
d1ed09
+}
d1ed09
+
d1ed09
+void
d1ed09
+g_hmac_unref (GHmac *hmac)
d1ed09
+{
d1ed09
+  g_return_if_fail (hmac != NULL);
d1ed09
+
d1ed09
+  if (g_atomic_int_dec_and_test (&hmac->ref_count))
d1ed09
+    {
d1ed09
+      gnutls_hmac_deinit (hmac->hmac, NULL);
d1ed09
+      g_free (hmac->digest_str);
6fcf6b
+      g_free (hmac);
d1ed09
+    }
d1ed09
+}
d1ed09
+
d1ed09
+
d1ed09
+void
d1ed09
+g_hmac_update (GHmac        *hmac,
d1ed09
+               const guchar *data,
d1ed09
+               gssize        length)
d1ed09
+{
6fcf6b
+  int ret;
6fcf6b
+
d1ed09
+  g_return_if_fail (hmac != NULL);
d1ed09
+  g_return_if_fail (length == 0 || data != NULL);
d1ed09
+
6fcf6b
+  if (length == -1)
6fcf6b
+    length = strlen ((const char *)data);
6fcf6b
+
6fcf6b
+  /* g_hmac_update is not allowed to fail, so we'll have to crash on error. */
6fcf6b
+  ret = gnutls_hmac (hmac->hmac, data, length);
6fcf6b
+  if (ret != 0)
6fcf6b
+    g_error ("gnutls_hmac failed: %s", gnutls_strerror (ret));
d1ed09
+}
d1ed09
+
d1ed09
+const gchar *
d1ed09
+g_hmac_get_string (GHmac *hmac)
d1ed09
+{
d1ed09
+  guint8 *buffer;
d1ed09
+  gsize digest_len;
d1ed09
+
d1ed09
+  g_return_val_if_fail (hmac != NULL, NULL);
d1ed09
+
d1ed09
+  if (hmac->digest_str)
d1ed09
+    return hmac->digest_str;
d1ed09
+
d1ed09
+  digest_len = g_checksum_type_get_length (hmac->digest_type);
d1ed09
+  buffer = g_alloca (digest_len);
d1ed09
+
d1ed09
+  gnutls_hmac_output (hmac->hmac, buffer);
d1ed09
+  hmac->digest_str = gchecksum_digest_to_string (buffer, digest_len);
d1ed09
+  return hmac->digest_str;
d1ed09
+}
d1ed09
+
d1ed09
+
d1ed09
+void
d1ed09
+g_hmac_get_digest (GHmac  *hmac,
d1ed09
+                   guint8 *buffer,
d1ed09
+                   gsize  *digest_len)
d1ed09
+{
d1ed09
+  g_return_if_fail (hmac != NULL);
d1ed09
+
d1ed09
+  gnutls_hmac_output (hmac->hmac, buffer);
d1ed09
+  *digest_len = g_checksum_type_get_length (hmac->digest_type);
d1ed09
+}
d1ed09
diff --git a/glib/ghmac.c b/glib/ghmac.c
6fcf6b
index 7db38e34a..b03a5aea7 100644
d1ed09
--- a/glib/ghmac.c
d1ed09
+++ b/glib/ghmac.c
d1ed09
@@ -33,6 +33,7 @@
d1ed09
 #include "gtypes.h"
d1ed09
 #include "glibintl.h"
d1ed09
 
d1ed09
+#error "build configuration error"
d1ed09
 
d1ed09
 /**
d1ed09
  * SECTION:hmac
6fcf6b
@@ -84,6 +85,18 @@ struct _GHmac
6fcf6b
  * Support for digests of type %G_CHECKSUM_SHA512 has been added in GLib 2.42.
6fcf6b
  * Support for %G_CHECKSUM_SHA384 was added in GLib 2.52.
6fcf6b
  *
6fcf6b
+ * Note that #GHmac creation may fail, in which case this function will
6fcf6b
+ * return %NULL. Since there is no error parameter, it is not possible
6fcf6b
+ * to indicate why.
6fcf6b
+ *
6fcf6b
+ * In Fedora, CentOS Stream, and Red Hat Enterprise Linux, GLib is
6fcf6b
+ * configured to use GnuTLS to implement #GHmac in order to support FIPS
6fcf6b
+ * compliance. This introduces additional failure possibilities that are
6fcf6b
+ * not present in upstream GLib. For example, the creation of a #GHmac
6fcf6b
+ * will fail if @digest_type is %G_CHECKSUM_MD5 and the system is
6fcf6b
+ * running in FIPS mode. #GHmac creation may also fail if GLib is unable
6fcf6b
+ * to load GnuTLS.
6fcf6b
+ *
6fcf6b
  * Returns: the newly created #GHmac, or %NULL.
6fcf6b
  *   Use g_hmac_unref() to free the memory allocated by it.
6fcf6b
  *
d1ed09
diff --git a/glib/meson.build b/glib/meson.build
6fcf6b
index 306a67f13..07d41456d 100644
d1ed09
--- a/glib/meson.build
d1ed09
+++ b/glib/meson.build
6fcf6b
@@ -127,6 +127,7 @@ glib_sources = files(
6fcf6b
   'gbytes.c',
6fcf6b
   'gcharset.c',
6fcf6b
   'gchecksum.c',
6fcf6b
+  'gchecksumprivate.h',
6fcf6b
   'gconvert.c',
6fcf6b
   'gdataset.c',
6fcf6b
   'gdate.c',
6fcf6b
@@ -137,7 +138,6 @@ glib_sources = files(
d1ed09
   'gfileutils.c',
d1ed09
   'ggettext.c',
d1ed09
   'ghash.c',
d1ed09
-  'ghmac.c',
d1ed09
   'ghmac-utils.c',
d1ed09
   'ghook.c',
d1ed09
   'ghostutils.c',
6fcf6b
@@ -223,6 +223,12 @@ else
d1ed09
   glib_dtrace_hdr = []
d1ed09
 endif
d1ed09
 
d1ed09
+if get_option('gnutls')
d1ed09
+  glib_sources += files('ghmac-gnutls.c')
d1ed09
+else
d1ed09
+  glib_sources += files('ghmac.c')
d1ed09
+endif
d1ed09
+
d1ed09
 pcre_static_args = []
d1ed09
 
d1ed09
 if use_pcre_static_flag
6fcf6b
@@ -239,7 +245,7 @@ libglib = library('glib-2.0',
d1ed09
   link_args : platform_ldflags + noseh_link_args,
d1ed09
   include_directories : configinc,
d1ed09
   link_with : [charset_lib, gnulib_lib],
d1ed09
-  dependencies : [pcre, thread_dep, libintl, librt] + libiconv + platform_deps,
6fcf6b
+  dependencies : [pcre, thread_dep, libintl, librt] + libgnutls_dep + libiconv + platform_deps,
d1ed09
   c_args : ['-DG_LOG_DOMAIN="GLib"', '-DGLIB_COMPILATION'] + pcre_static_args + glib_hidden_visibility_args
d1ed09
 )
d1ed09
 
d1ed09
diff --git a/meson.build b/meson.build
6fcf6b
index 0cefee51d..eaf8d3900 100644
d1ed09
--- a/meson.build
d1ed09
+++ b/meson.build
d1ed09
@@ -1596,6 +1596,13 @@ if host_system == 'linux' and get_option('libmount')
d1ed09
   libmount_dep = [dependency('mount', version : '>=2.23', required : true)]
d1ed09
 endif
d1ed09
 
d1ed09
+# gnutls is used optionally by ghmac
d1ed09
+libgnutls_dep = []
d1ed09
+if get_option('gnutls')
645344
+  libgnutls_dep = [dependency('gnutls', version : '>=3.6.9', required : true)]
d1ed09
+  glib_conf.set('HAVE_GNUTLS', 1)
d1ed09
+endif
d1ed09
+
d1ed09
 if host_system == 'windows'
d1ed09
   winsock2 = cc.find_library('ws2_32')
d1ed09
 endif
d1ed09
diff --git a/meson_options.txt b/meson_options.txt
d1ed09
index 4504c6858..d18c42a36 100644
d1ed09
--- a/meson_options.txt
d1ed09
+++ b/meson_options.txt
d1ed09
@@ -34,6 +34,11 @@ option('libmount',
d1ed09
        value : true,
d1ed09
        description : 'build with libmount support')
d1ed09
 
d1ed09
+option('gnutls',
d1ed09
+       type : 'boolean',
d1ed09
+       value : false,
d1ed09
+       description : 'build with gnutls support')
d1ed09
+
d1ed09
 option('internal_pcre',
d1ed09
        type : 'boolean',
d1ed09
        value : false,
d1ed09
-- 
6fcf6b
2.31.1
6fcf6b
6fcf6b
From 87280b23902290dcf843a42d06cedeef571a673f Mon Sep 17 00:00:00 2001
6fcf6b
From: Michael Catanzaro <mcatanzaro@redhat.com>
6fcf6b
Date: Thu, 1 Jul 2021 15:51:26 -0500
6fcf6b
Subject: [PATCH 3/3] Add more tests for GHmac
d1ed09
6fcf6b
This will test a few problems that we hit recently:
6fcf6b
6fcf6b
g_hmac_copy() is broken, https://bugzilla.redhat.com/show_bug.cgi?id=1786538
6fcf6b
6fcf6b
Crash in g_hmac_update() in FIPS mode, https://bugzilla.redhat.com/show_bug.cgi?id=1971533
6fcf6b
6fcf6b
Crash when passing -1 length to g_hmac_update() (discovered in #1971533)
6fcf6b
---
6fcf6b
 glib/tests/hmac.c | 22 ++++++++++++++++++++++
6fcf6b
 1 file changed, 22 insertions(+)
6fcf6b
6fcf6b
diff --git a/glib/tests/hmac.c b/glib/tests/hmac.c
6fcf6b
index 3ac3206df..16b2fac9c 100644
6fcf6b
--- a/glib/tests/hmac.c
6fcf6b
+++ b/glib/tests/hmac.c
6fcf6b
@@ -493,6 +493,27 @@ test_hmac_for_bytes (void)
6fcf6b
   g_bytes_unref (data);
6fcf6b
 }
6fcf6b
 
6fcf6b
+static void
6fcf6b
+test_ghmac_gnutls_regressions (void)
6fcf6b
+{
6fcf6b
+  GHmac *hmac;
6fcf6b
+  GHmac *copy;
6fcf6b
+
6fcf6b
+  hmac = g_hmac_new (G_CHECKSUM_SHA256, (const guchar *)"abc123", sizeof ("abc123"));
6fcf6b
+  g_assert_nonnull (hmac);
6fcf6b
+
6fcf6b
+  /* Ensure g_hmac_update() does not crash when called with -1. */
6fcf6b
+  g_hmac_update (hmac, (const guchar *)"You win again, gravity!", -1);
6fcf6b
+
6fcf6b
+  /* Ensure g_hmac_copy() does not crash. */
6fcf6b
+  copy = g_hmac_copy (hmac);
6fcf6b
+  g_assert_nonnull (hmac);
6fcf6b
+  g_hmac_unref (hmac);
6fcf6b
+
6fcf6b
+  g_assert_cmpstr (g_hmac_get_string (copy), ==, "795ba6900bcb22e8ce65c2ec02db4e85697da921deb960ee3143bf88a4a60f83");
6fcf6b
+  g_hmac_unref (copy);
6fcf6b
+}
6fcf6b
+
6fcf6b
 int
6fcf6b
 main (int argc,
6fcf6b
     char **argv)
6fcf6b
@@ -545,6 +566,7 @@ main (int argc,
6fcf6b
   g_test_add_func ("/hmac/for-data", test_hmac_for_data);
6fcf6b
   g_test_add_func ("/hmac/for-string", test_hmac_for_string);
6fcf6b
   g_test_add_func ("/hmac/for-bytes", test_hmac_for_bytes);
6fcf6b
+  g_test_add_func ("/hmac/ghmac-gnutls-regressions", test_ghmac_gnutls_regressions);
6fcf6b
 
6fcf6b
   return g_test_run ();
6fcf6b
 }
6fcf6b
-- 
6fcf6b
2.31.1