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