Blame SOURCES/pam-1.5.1-timestamp-openssl-hmac-authentication.patch

6402e1
From b3bb13e18a74e9ece825b7de1b81db97ebb107a0 Mon Sep 17 00:00:00 2001
6402e1
From: Iker Pedrosa <ipedrosa@redhat.com>
6402e1
Date: Thu, 25 Mar 2021 09:43:30 +0100
6402e1
Subject: [PATCH] pam_timestamp: replace hmac implementation
6402e1
6402e1
sha1 is no longer recommended as a cryptographic algorithm for
6402e1
authentication. Thus, the idea of this change is to replace the
6402e1
implementation provided by hmacsha1 included in pam_timestamp module by
6402e1
the one in the openssl library. This way, there's no need to maintain
6402e1
the cryptographic algorithm implementation and it can be easily changed
6402e1
with a single configuration change.
6402e1
6402e1
modules/pam_timestamp/hmac_openssl_wrapper.c: implement wrapper
6402e1
functions around openssl's hmac implementation. Moreover, manage the key
6402e1
generation and its read and write in a file. Include an option to
6402e1
configure the cryptographic algorithm in login.defs file.
6402e1
modules/pam_timestamp/hmac_openssl_wrapper.h: likewise.
6402e1
modules/pam_timestamp/pam_timestamp.c: replace calls to functions
6402e1
provided by hmacsha1 by functions provided by openssl's wrapper.
6402e1
configure.ac: include openssl dependecy if it is enabled.
6402e1
modules/pam_timestamp/Makefile.am: include new files and openssl library
6402e1
to compilation.
6402e1
ci/install-dependencies.sh: include openssl library to dependencies.
6402e1
NEWS: add new item to next release.
6402e1
Make.xml.rules.in: add stringparam profiling for hmac
6402e1
doc/custom-man.xsl: change import docbook to one with profiling
6402e1
modules/pam_timestamp/pam_timestamp.8.xml: add conditional paragraph to
6402e1
indicate the value in /etc/login.defs that holds the value for the
6402e1
encryption algorithm
6402e1
6402e1
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1947294
6402e1
---
6402e1
 Make.xml.rules.in                            |   2 +-
6402e1
 NEWS                                         |   5 +
6402e1
 configure.ac                                 |  16 +
6402e1
 doc/custom-man.xsl                           |   2 +-
6402e1
 modules/pam_timestamp/Makefile.am            |  15 +-
6402e1
 modules/pam_timestamp/hmac_openssl_wrapper.c | 381 +++++++++++++++++++
6402e1
 modules/pam_timestamp/hmac_openssl_wrapper.h |  57 +++
6402e1
 modules/pam_timestamp/pam_timestamp.8.xml    |   5 +
6402e1
 modules/pam_timestamp/pam_timestamp.c        |  53 ++-
6402e1
 10 files changed, 524 insertions(+), 13 deletions(-)
6402e1
 create mode 100644 modules/pam_timestamp/hmac_openssl_wrapper.c
6402e1
 create mode 100644 modules/pam_timestamp/hmac_openssl_wrapper.h
6402e1
6402e1
diff --git a/Make.xml.rules.in b/Make.xml.rules.in
6402e1
index daa1b97b..27bb510e 100644
6402e1
--- a/Make.xml.rules.in
6402e1
+++ b/Make.xml.rules.in
6402e1
@@ -21,6 +21,6 @@ README: README.xml
6402e1
 
6402e1
 %.8: %.8.xml
6402e1
 	$(XMLLINT) --nonet --xinclude --postvalid --noout $<
6402e1
-	$(XSLTPROC) -o $(srcdir)/$@ --path $(srcdir) --xinclude @STRINGPARAM_VENDORDIR@ --nonet $(top_srcdir)/doc/custom-man.xsl $<
6402e1
+	$(XSLTPROC) -o $(srcdir)/$@ --path $(srcdir) --xinclude @STRINGPARAM_VENDORDIR@ @STRINGPARAM_HMAC@ --nonet $(top_srcdir)/doc/custom-man.xsl $<
6402e1
 
6402e1
 #CLEANFILES += $(man_MANS) README
6402e1
diff --git a/NEWS b/NEWS
6402e1
index 2d49ec39..f4d11303 100644
6402e1
--- a/NEWS
6402e1
+++ b/NEWS
6402e1
@@ -1,5 +1,10 @@
6402e1
 Linux-PAM NEWS -- history of user-visible changes.
6402e1
 
6402e1
+Release next
6402e1
+* pam_timestamp: change hmac algorithm to call openssl instead of the bundled
6402e1
+                 sha1 implementation if selected. Add option to select the hash
6402e1
+                 algorithm to use with HMAC.
6402e1
+
6402e1
 Release 1.5.1
6402e1
 * pam_unix: fixed CVE-2020-27780 - authentication bypass when a user
6402e1
             doesn't exist and root password is blank
6402e1
diff --git a/configure.ac b/configure.ac
6402e1
index bd806473..9c92d0de 100644
6402e1
--- a/configure.ac
6402e1
+++ b/configure.ac
6402e1
@@ -504,6 +504,22 @@ else
6402e1
 fi
6402e1
 AC_SUBST([STRINGPARAM_VENDORDIR])
6402e1
 
6402e1
+AC_ARG_ENABLE([openssl],
6402e1
+  AS_HELP_STRING([--enable-openssl],[use OpenSSL crypto libraries]),
6402e1
+  [OPENSSL_ENABLED=$enableval], OPENSSL_ENABLED=no)
6402e1
+if test "$OPENSSL_ENABLED" = "yes" ; then
6402e1
+  AC_CHECK_LIB([crypto], [EVP_MAC_CTX_new],
6402e1
+  [CRYPTO_LIBS="-lcrypto"
6402e1
+   use_openssl=yes
6402e1
+   AC_DEFINE([WITH_OPENSSL], 1, [OpenSSL provides crypto algorithm for hmac])
6402e1
+   STRINGPARAM_HMAC="--stringparam profile.condition 'openssl_hmac'"],
6402e1
+  [CRYPTO_LIBS=""
6402e1
+   STRINGPARAM_HMAC="--stringparam profile.condition 'no_openssl_hmac'"])
6402e1
+fi
6402e1
+AC_SUBST([CRYPTO_LIBS])
6402e1
+AC_SUBST([STRINGPARAM_HMAC])
6402e1
+AM_CONDITIONAL([COND_USE_OPENSSL], [test "x$use_openssl" = "xyes"])
6402e1
+
6402e1
 dnl Checks for header files.
6402e1
 AC_HEADER_DIRENT
6402e1
 AC_HEADER_STDC
6402e1
diff --git a/doc/custom-man.xsl b/doc/custom-man.xsl
6402e1
index 4c35e839..a3408e6c 100644
6402e1
--- a/doc/custom-man.xsl
6402e1
+++ b/doc/custom-man.xsl
6402e1
@@ -1,6 +1,6 @@
6402e1
 
6402e1
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ss="http://docbook.sf.net/xmlns/string.subst/1.0" version="1.0">
6402e1
-  <xsl:import href="http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl"/>
6402e1
+  <xsl:import href="http://docbook.sourceforge.net/release/xsl/current/manpages/profile-docbook.xsl"/>
6402e1
   <xsl:param name="vendordir"/>
6402e1
 
6402e1
   <xsl:param name="man.string.subst.map.local.pre">
6402e1
diff --git a/modules/pam_timestamp/Makefile.am b/modules/pam_timestamp/Makefile.am
6402e1
index 1faa324a..d290b85f 100644
6402e1
--- a/modules/pam_timestamp/Makefile.am
6402e1
+++ b/modules/pam_timestamp/Makefile.am
6402e1
@@ -18,12 +18,12 @@ TESTS = $(dist_check_SCRIPTS) $(check_PROGRAMS)
6402e1
 securelibdir = $(SECUREDIR)
6402e1
 secureconfdir = $(SCONFIGDIR)
6402e1
 
6402e1
-noinst_HEADERS = hmacsha1.h sha1.h
6402e1
+noinst_HEADERS = hmacsha1.h sha1.h hmac_openssl_wrapper.h
6402e1
 
6402e1
 AM_CFLAGS = -I$(top_srcdir)/libpam/include -I$(top_srcdir)/libpamc/include \
6402e1
 	$(WARN_CFLAGS)
6402e1
 
6402e1
-pam_timestamp_la_LDFLAGS = -no-undefined -avoid-version -module $(AM_LDFLAGS)
6402e1
+pam_timestamp_la_LDFLAGS = -no-undefined -avoid-version -module $(AM_LDFLAGS) $(CRYPTO_LIBS)
6402e1
 pam_timestamp_la_LIBADD = $(top_builddir)/libpam/libpam.la
6402e1
 if HAVE_VERSIONING
6402e1
   pam_timestamp_la_LDFLAGS += -Wl,--version-script=$(srcdir)/../modules.map
6402e1
@@ -32,7 +32,12 @@ endif
6402e1
 securelib_LTLIBRARIES = pam_timestamp.la
6402e1
 sbin_PROGRAMS = pam_timestamp_check
6402e1
 
6402e1
-pam_timestamp_la_SOURCES = pam_timestamp.c hmacsha1.c sha1.c
6402e1
+pam_timestamp_la_SOURCES = pam_timestamp.c
6402e1
+if COND_USE_OPENSSL
6402e1
+pam_timestamp_la_SOURCES += hmac_openssl_wrapper.c
6402e1
+else
6402e1
+pam_timestamp_la_SOURCES += hmacsha1.c sha1.c
6402e1
+endif
6402e1
 pam_timestamp_la_CFLAGS = $(AM_CFLAGS)
6402e1
 
6402e1
 pam_timestamp_check_SOURCES = pam_timestamp_check.c
6402e1
@@ -40,7 +45,11 @@ pam_timestamp_check_CFLAGS = $(AM_CFLAGS) @EXE_CFLAGS@
6402e1
 pam_timestamp_check_LDADD = $(top_builddir)/libpam/libpam.la
6402e1
 pam_timestamp_check_LDFLAGS = @EXE_LDFLAGS@
6402e1
 
6402e1
+if COND_USE_OPENSSL
6402e1
+hmacfile_SOURCES = hmac_openssl_wrapper.c
6402e1
+else
6402e1
 hmacfile_SOURCES = hmacfile.c hmacsha1.c sha1.c
6402e1
+endif
6402e1
 hmacfile_LDADD = $(top_builddir)/libpam/libpam.la
6402e1
 
6402e1
 check_PROGRAMS = hmacfile
6402e1
diff --git a/modules/pam_timestamp/hmac_openssl_wrapper.c b/modules/pam_timestamp/hmac_openssl_wrapper.c
6402e1
new file mode 100644
6402e1
index 00000000..926c2fb9
6402e1
--- /dev/null
6402e1
+++ b/modules/pam_timestamp/hmac_openssl_wrapper.c
6402e1
@@ -0,0 +1,381 @@
6402e1
+/* Wrapper for hmac openssl implementation.
6402e1
+ *
6402e1
+ * Copyright (c) 2021 Red Hat, Inc.
6402e1
+ * Written by Iker Pedrosa <ipedrosa@redhat.com>
6402e1
+ *
6402e1
+ * Redistribution and use in source and binary forms, with or without
6402e1
+ * modification, are permitted provided that the following conditions
6402e1
+ * are met:
6402e1
+ * 1. Redistributions of source code must retain the above copyright
6402e1
+ *    notice, and the entire permission notice in its entirety,
6402e1
+ *    including the disclaimer of warranties.
6402e1
+ * 2. Redistributions in binary form must reproduce the above copyright
6402e1
+ *    notice, this list of conditions and the following disclaimer in the
6402e1
+ *    documentation and/or other materials provided with the distribution.
6402e1
+ * 3. The name of the author may not be used to endorse or promote
6402e1
+ *    products derived from this software without specific prior
6402e1
+ *    written permission.
6402e1
+ *
6402e1
+ * ALTERNATIVELY, this product may be distributed under the terms of
6402e1
+ * the GNU Public License, in which case the provisions of the GPL are
6402e1
+ * required INSTEAD OF the above restrictions.  (This clause is
6402e1
+ * necessary due to a potential bad interaction between the GPL and
6402e1
+ * the restrictions contained in a BSD-style copyright.)
6402e1
+ *
6402e1
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
6402e1
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
6402e1
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
6402e1
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
6402e1
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
6402e1
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
6402e1
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6402e1
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
6402e1
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6402e1
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
6402e1
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
6402e1
+ *
6402e1
+ */
6402e1
+
6402e1
+#include "config.h"
6402e1
+
6402e1
+#ifdef WITH_OPENSSL
6402e1
+
6402e1
+#include <sys/stat.h>
6402e1
+#include <fcntl.h>
6402e1
+#include <syslog.h>
6402e1
+#include <unistd.h>
6402e1
+#include <string.h>
6402e1
+#include <errno.h>
6402e1
+#include <openssl/evp.h>
6402e1
+#include <openssl/params.h>
6402e1
+#include <openssl/core_names.h>
6402e1
+
6402e1
+#include <security/pam_ext.h>
6402e1
+#include <security/pam_modutil.h>
6402e1
+
6402e1
+#include "hmac_openssl_wrapper.h"
6402e1
+
6402e1
+#define LOGIN_DEFS          "/etc/login.defs"
6402e1
+#define CRYPTO_KEY          "HMAC_CRYPTO_ALGO"
6402e1
+#define DEFAULT_ALGORITHM   "SHA512"
6402e1
+#define MAX_HMAC_LENGTH     512
6402e1
+#define MAX_KEY_LENGTH      EVP_MAX_KEY_LENGTH
6402e1
+
6402e1
+static char *
6402e1
+get_crypto_algorithm(pam_handle_t *pamh, int debug){
6402e1
+    char *config_value = NULL;
6402e1
+
6402e1
+    config_value = pam_modutil_search_key(pamh, LOGIN_DEFS, CRYPTO_KEY);
6402e1
+
6402e1
+    if (config_value == NULL) {
6402e1
+        config_value = strdup(DEFAULT_ALGORITHM);
6402e1
+        if (debug) {
6402e1
+            pam_syslog(pamh, LOG_DEBUG,
6402e1
+                   "Key [%s] not found, falling back to default algorithm [%s]\n",
6402e1
+                   CRYPTO_KEY, DEFAULT_ALGORITHM);
6402e1
+        }
6402e1
+    }
6402e1
+
6402e1
+    return config_value;
6402e1
+}
6402e1
+
6402e1
+static int
6402e1
+generate_key(pam_handle_t *pamh, char **key, size_t key_size)
6402e1
+{
6402e1
+    int fd = 0;
6402e1
+    size_t bytes_read = 0;
6402e1
+    char * tmp = NULL;
6402e1
+
6402e1
+    fd = open("/dev/urandom", O_RDONLY);
6402e1
+    if (fd == -1) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Cannot open /dev/urandom: %m");
6402e1
+        return PAM_AUTH_ERR;
6402e1
+    }
6402e1
+
6402e1
+    tmp = malloc(key_size);
6402e1
+    if (!tmp) {
6402e1
+        pam_syslog(pamh, LOG_CRIT, "Not enough memory");
6402e1
+        close(fd);
6402e1
+        return PAM_AUTH_ERR;
6402e1
+    }
6402e1
+
6402e1
+    bytes_read = pam_modutil_read(fd, tmp, key_size);
6402e1
+    close(fd);
6402e1
+
6402e1
+    if (bytes_read < key_size) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Short read on random device");
6402e1
+        free(tmp);
6402e1
+        return PAM_AUTH_ERR;
6402e1
+    }
6402e1
+
6402e1
+    *key = tmp;
6402e1
+
6402e1
+    return PAM_SUCCESS;
6402e1
+}
6402e1
+
6402e1
+static int
6402e1
+read_file(pam_handle_t *pamh, int fd, char **text, size_t *text_length)
6402e1
+{
6402e1
+    struct stat st;
6402e1
+    size_t bytes_read = 0;
6402e1
+    char *tmp = NULL;
6402e1
+
6402e1
+    if (fstat(fd, &st) == -1) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Unable to stat file: %m");
6402e1
+        close(fd);
6402e1
+        return PAM_AUTH_ERR;
6402e1
+    }
6402e1
+
6402e1
+    if (st.st_size == 0) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Key file size cannot be 0");
6402e1
+        close(fd);
6402e1
+        return PAM_AUTH_ERR;
6402e1
+    }
6402e1
+
6402e1
+    tmp = malloc(st.st_size);
6402e1
+    if (!tmp) {
6402e1
+        pam_syslog(pamh, LOG_CRIT, "Not enough memory");
6402e1
+        close(fd);
6402e1
+        return PAM_AUTH_ERR;
6402e1
+    }
6402e1
+
6402e1
+    bytes_read = pam_modutil_read(fd, tmp, st.st_size);
6402e1
+    close(fd);
6402e1
+
6402e1
+    if (bytes_read < (size_t)st.st_size) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Short read on key file");
6402e1
+        memset(tmp, 0, st.st_size);
6402e1
+        free(tmp);
6402e1
+        return PAM_AUTH_ERR;
6402e1
+    }
6402e1
+
6402e1
+    *text = tmp;
6402e1
+    *text_length = st.st_size;
6402e1
+
6402e1
+    return PAM_SUCCESS;
6402e1
+}
6402e1
+
6402e1
+static int
6402e1
+write_file(pam_handle_t *pamh, const char *file_name, char *text,
6402e1
+           size_t text_length, uid_t owner, gid_t group)
6402e1
+{
6402e1
+    int fd = 0;
6402e1
+    size_t bytes_written = 0;
6402e1
+
6402e1
+    fd = open(file_name,
6402e1
+              O_WRONLY | O_CREAT | O_TRUNC,
6402e1
+              S_IRUSR | S_IWUSR);
6402e1
+    if (fd == -1) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Unable to open [%s]: %m", file_name);
6402e1
+        memset(text, 0, text_length);
6402e1
+        free(text);
6402e1
+        return PAM_AUTH_ERR;
6402e1
+    }
6402e1
+
6402e1
+    if (fchown(fd, owner, group) == -1) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Unable to change ownership [%s]: %m", file_name);
6402e1
+        memset(text, 0, text_length);
6402e1
+        free(text);
6402e1
+        close(fd);
6402e1
+        return PAM_AUTH_ERR;
6402e1
+    }
6402e1
+
6402e1
+    bytes_written = pam_modutil_write(fd, text, text_length);
6402e1
+    close(fd);
6402e1
+
6402e1
+    if (bytes_written < text_length) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Short write on %s", file_name);
6402e1
+        free(text);
6402e1
+        return PAM_AUTH_ERR;
6402e1
+    }
6402e1
+
6402e1
+    return PAM_SUCCESS;
6402e1
+}
6402e1
+
6402e1
+static int
6402e1
+key_management(pam_handle_t *pamh, const char *file_name, char **text,
6402e1
+                size_t text_length, uid_t owner, gid_t group)
6402e1
+{
6402e1
+    int fd = 0;
6402e1
+
6402e1
+    fd = open(file_name, O_RDONLY | O_NOFOLLOW);
6402e1
+    if (fd == -1) {
6402e1
+        if (errno == ENOENT) {
6402e1
+            if (generate_key(pamh, text, text_length)) {
6402e1
+                pam_syslog(pamh, LOG_ERR, "Unable to generate key");
6402e1
+                return PAM_AUTH_ERR;
6402e1
+            }
6402e1
+
6402e1
+            if (write_file(pamh, file_name, *text, text_length, owner, group)) {
6402e1
+                pam_syslog(pamh, LOG_ERR, "Unable to write key");
6402e1
+                return PAM_AUTH_ERR;
6402e1
+            }
6402e1
+        } else {
6402e1
+            pam_syslog(pamh, LOG_ERR, "Unable to open %s: %m", file_name);
6402e1
+            return PAM_AUTH_ERR;
6402e1
+        }
6402e1
+    } else {
6402e1
+        if (read_file(pamh, fd, text, &text_length)) {
6402e1
+            pam_syslog(pamh, LOG_ERR, "Error reading key file %s\n", file_name);
6402e1
+            return PAM_AUTH_ERR;
6402e1
+        }
6402e1
+    }
6402e1
+
6402e1
+    return PAM_SUCCESS;
6402e1
+}
6402e1
+
6402e1
+static int
6402e1
+hmac_management(pam_handle_t *pamh, int debug, void **out, size_t *out_length,
6402e1
+                char *key, size_t key_length,
6402e1
+                const void *text, size_t text_length)
6402e1
+{
6402e1
+    int ret = PAM_AUTH_ERR;
6402e1
+    EVP_MAC *evp_mac = NULL;
6402e1
+    EVP_MAC_CTX *ctx = NULL;
6402e1
+    unsigned char *hmac_message = NULL;
6402e1
+    size_t hmac_length;
6402e1
+    char *algo = NULL;
6402e1
+    OSSL_PARAM subalg_param[] = { OSSL_PARAM_END, OSSL_PARAM_END };
6402e1
+
6402e1
+    algo = get_crypto_algorithm(pamh, debug);
6402e1
+
6402e1
+    subalg_param[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
6402e1
+                                                       algo,
6402e1
+                                                       0);
6402e1
+
6402e1
+    evp_mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
6402e1
+    if (evp_mac == NULL) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Unable to create hmac implementation");
6402e1
+        goto done;
6402e1
+    }
6402e1
+
6402e1
+    ctx = EVP_MAC_CTX_new(evp_mac);
6402e1
+    if (ctx == NULL) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Unable to create hmac context");
6402e1
+        goto done;
6402e1
+    }
6402e1
+
6402e1
+    ret = EVP_MAC_init(ctx, (const unsigned char *)key, key_length, subalg_param);
6402e1
+    if (ret == 0) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Unable to initialize hmac context");
6402e1
+        goto done;
6402e1
+    }
6402e1
+
6402e1
+    ret = EVP_MAC_update(ctx, (const unsigned char *)text, text_length);
6402e1
+    if (ret == 0) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Unable to update hmac context");
6402e1
+        goto done;
6402e1
+    }
6402e1
+
6402e1
+    hmac_message = (unsigned char*)malloc(sizeof(unsigned char) * MAX_HMAC_LENGTH);
6402e1
+    if (!hmac_message) {
6402e1
+        pam_syslog(pamh, LOG_CRIT, "Not enough memory");
6402e1
+        goto done;
6402e1
+    }
6402e1
+
6402e1
+    ret = EVP_MAC_final(ctx, hmac_message, &hmac_length, MAX_HMAC_LENGTH);
6402e1
+    if (ret == 0) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Unable to calculate hmac message");
6402e1
+        goto done;
6402e1
+    }
6402e1
+
6402e1
+    *out_length = hmac_length;
6402e1
+    *out = malloc(*out_length);
6402e1
+    if (*out == NULL) {
6402e1
+        pam_syslog(pamh, LOG_CRIT, "Not enough memory");
6402e1
+        goto done;
6402e1
+    }
6402e1
+
6402e1
+    memcpy(*out, hmac_message, *out_length);
6402e1
+    ret = PAM_SUCCESS;
6402e1
+
6402e1
+done:
6402e1
+    if (hmac_message != NULL) {
6402e1
+        free(hmac_message);
6402e1
+    }
6402e1
+    if (key != NULL) {
6402e1
+        memset(key, 0, key_length);
6402e1
+        free(key);
6402e1
+    }
6402e1
+    if (ctx != NULL) {
6402e1
+        EVP_MAC_CTX_free(ctx);
6402e1
+    }
6402e1
+    if (evp_mac != NULL) {
6402e1
+        EVP_MAC_free(evp_mac);
6402e1
+    }
6402e1
+    free(algo);
6402e1
+
6402e1
+    return ret;
6402e1
+}
6402e1
+
6402e1
+int
6402e1
+hmac_size(pam_handle_t *pamh, int debug, size_t *hmac_length)
6402e1
+{
6402e1
+    int ret = PAM_AUTH_ERR;
6402e1
+    EVP_MAC *evp_mac = NULL;
6402e1
+    EVP_MAC_CTX *ctx = NULL;
6402e1
+    const unsigned char key[] = "ThisIsJustAKey";
6402e1
+    size_t key_length = MAX_KEY_LENGTH;
6402e1
+    char *algo = NULL;
6402e1
+    OSSL_PARAM subalg_param[] = { OSSL_PARAM_END, OSSL_PARAM_END };
6402e1
+
6402e1
+    algo = get_crypto_algorithm(pamh, debug);
6402e1
+
6402e1
+    subalg_param[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
6402e1
+                                                       algo,
6402e1
+                                                       0);
6402e1
+
6402e1
+    evp_mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
6402e1
+    if (evp_mac == NULL) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Unable to create hmac implementation");
6402e1
+        goto done;
6402e1
+    }
6402e1
+
6402e1
+    ctx = EVP_MAC_CTX_new(evp_mac);
6402e1
+    if (ctx == NULL) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Unable to create hmac context");
6402e1
+        goto done;
6402e1
+    }
6402e1
+
6402e1
+    ret = EVP_MAC_init(ctx, key, key_length, subalg_param);
6402e1
+    if (ret == 0) {
6402e1
+        pam_syslog(pamh, LOG_ERR, "Unable to initialize hmac context");
6402e1
+        goto done;
6402e1
+    }
6402e1
+
6402e1
+    *hmac_length = EVP_MAC_CTX_get_mac_size(ctx);
6402e1
+    ret = PAM_SUCCESS;
6402e1
+
6402e1
+done:
6402e1
+    if (ctx != NULL) {
6402e1
+        EVP_MAC_CTX_free(ctx);
6402e1
+    }
6402e1
+    if (evp_mac != NULL) {
6402e1
+        EVP_MAC_free(evp_mac);
6402e1
+    }
6402e1
+    free(algo);
6402e1
+
6402e1
+    return ret;
6402e1
+}
6402e1
+
6402e1
+int
6402e1
+hmac_generate(pam_handle_t *pamh, int debug, void **mac, size_t *mac_length,
6402e1
+              const char *key_file, uid_t owner, gid_t group,
6402e1
+              const void *text, size_t text_length)
6402e1
+{
6402e1
+    char *key = NULL;
6402e1
+    size_t key_length = MAX_KEY_LENGTH;
6402e1
+
6402e1
+    if (key_management(pamh, key_file, &key, key_length, owner, group)) {
6402e1
+        return PAM_AUTH_ERR;
6402e1
+    }
6402e1
+
6402e1
+    if (hmac_management(pamh, debug, mac, mac_length, key, key_length,
6402e1
+                        text, text_length)) {
6402e1
+        return PAM_AUTH_ERR;
6402e1
+    }
6402e1
+
6402e1
+    return PAM_SUCCESS;
6402e1
+}
6402e1
+
6402e1
+#endif /* WITH_OPENSSL */
6402e1
diff --git a/modules/pam_timestamp/hmac_openssl_wrapper.h b/modules/pam_timestamp/hmac_openssl_wrapper.h
6402e1
new file mode 100644
6402e1
index 00000000..cc27c811
6402e1
--- /dev/null
6402e1
+++ b/modules/pam_timestamp/hmac_openssl_wrapper.h
6402e1
@@ -0,0 +1,57 @@
6402e1
+/* Wrapper for hmac openssl implementation.
6402e1
+ *
6402e1
+ * Copyright (c) 2021 Red Hat, Inc.
6402e1
+ * Written by Iker Pedrosa <ipedrosa@redhat.com>
6402e1
+ *
6402e1
+ * Redistribution and use in source and binary forms, with or without
6402e1
+ * modification, are permitted provided that the following conditions
6402e1
+ * are met:
6402e1
+ * 1. Redistributions of source code must retain the above copyright
6402e1
+ *    notice, and the entire permission notice in its entirety,
6402e1
+ *    including the disclaimer of warranties.
6402e1
+ * 2. Redistributions in binary form must reproduce the above copyright
6402e1
+ *    notice, this list of conditions and the following disclaimer in the
6402e1
+ *    documentation and/or other materials provided with the distribution.
6402e1
+ * 3. The name of the author may not be used to endorse or promote
6402e1
+ *    products derived from this software without specific prior
6402e1
+ *    written permission.
6402e1
+ *
6402e1
+ * ALTERNATIVELY, this product may be distributed under the terms of
6402e1
+ * the GNU Public License, in which case the provisions of the GPL are
6402e1
+ * required INSTEAD OF the above restrictions.  (This clause is
6402e1
+ * necessary due to a potential bad interaction between the GPL and
6402e1
+ * the restrictions contained in a BSD-style copyright.)
6402e1
+ *
6402e1
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
6402e1
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
6402e1
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
6402e1
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
6402e1
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
6402e1
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
6402e1
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6402e1
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
6402e1
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6402e1
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
6402e1
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
6402e1
+ *
6402e1
+ */
6402e1
+#ifndef PAM_TIMESTAMP_HMAC_OPENSSL_WRAPPER_H
6402e1
+#define PAM_TIMESTAMP_HMAC_OPENSSL_WRAPPER_H
6402e1
+
6402e1
+#include "config.h"
6402e1
+
6402e1
+#ifdef WITH_OPENSSL
6402e1
+
6402e1
+#include <openssl/hmac.h>
6402e1
+#include <security/pam_modules.h>
6402e1
+
6402e1
+int
6402e1
+hmac_size(pam_handle_t *pamh, int debug, size_t *hmac_length);
6402e1
+
6402e1
+int
6402e1
+hmac_generate(pam_handle_t *pamh, int debug, void **mac, size_t *mac_length,
6402e1
+              const char *key_file, uid_t owner, gid_t group,
6402e1
+              const void *text, size_t text_length);
6402e1
+
6402e1
+#endif /* WITH_OPENSSL */
6402e1
+#endif /* PAM_TIMESTAMP_HMAC_OPENSSL_WRAPPER_H */
6402e1
diff --git a/modules/pam_timestamp/pam_timestamp.8.xml b/modules/pam_timestamp/pam_timestamp.8.xml
6402e1
index e19a0bcf..83e5aea8 100644
6402e1
--- a/modules/pam_timestamp/pam_timestamp.8.xml
6402e1
+++ b/modules/pam_timestamp/pam_timestamp.8.xml
6402e1
@@ -50,6 +50,11 @@ for the user.  When an application attempts to authenticate the user, a
6402e1
 <emphasis>pam_timestamp</emphasis> will treat a sufficiently recent timestamp
6402e1
 file as grounds for succeeding.
6402e1
     </para>
6402e1
+    <para condition="openssl_hmac">
6402e1
+      The default encryption hash is taken from the
6402e1
+      <emphasis remap='B'>HMAC_CRYPTO_ALGO</emphasis> variable from
6402e1
+      <emphasis>/etc/login.defs</emphasis>.
6402e1
+    </para>
6402e1
   </refsect1>
6402e1
 
6402e1
   <refsect1 id="pam_timestamp-options">
6402e1
diff --git a/modules/pam_timestamp/pam_timestamp.c b/modules/pam_timestamp/pam_timestamp.c
6402e1
index 30be883c..01dd1385 100644
6402e1
--- a/modules/pam_timestamp/pam_timestamp.c
6402e1
+++ b/modules/pam_timestamp/pam_timestamp.c
6402e1
@@ -56,7 +56,11 @@
6402e1
 #include <utmp.h>
6402e1
 #include <syslog.h>
6402e1
 #include <paths.h>
6402e1
+#ifdef WITH_OPENSSL
6402e1
+#include "hmac_openssl_wrapper.h"
6402e1
+#else
6402e1
 #include "hmacsha1.h"
6402e1
+#endif /* WITH_OPENSSL */
6402e1
 
6402e1
 #include <security/pam_modules.h>
6402e1
 #include <security/_pam_macros.h>
6402e1
@@ -79,6 +83,9 @@
6402e1
 #define BUFLEN PATH_MAX
6402e1
 #endif
6402e1
 
6402e1
+#define ROOT_USER 	0
6402e1
+#define ROOT_GROUP 	0
6402e1
+
6402e1
 /* Return PAM_SUCCESS if the given directory looks "safe". */
6402e1
 static int
6402e1
 check_dir_perms(pam_handle_t *pamh, const char *tdir)
6402e1
@@ -449,6 +456,13 @@ pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
6402e1
 			return PAM_AUTH_ERR;
6402e1
 		}
6402e1
 
6402e1
+#ifdef WITH_OPENSSL
6402e1
+		if (hmac_size(pamh, debug, &maclen)) {
6402e1
+			return PAM_AUTH_ERR;
6402e1
+		}
6402e1
+#else
6402e1
+		maclen = hmac_sha1_size();
6402e1
+#endif /* WITH_OPENSSL */
6402e1
 		/* Check that the file is the expected size. */
6402e1
 		if (st.st_size == 0) {
6402e1
 			/* Invalid, but may have been created by sudo. */
6402e1
@@ -456,7 +470,7 @@ pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
6402e1
 			return PAM_AUTH_ERR;
6402e1
 		}
6402e1
 		if (st.st_size !=
6402e1
-		    (off_t)(strlen(path) + 1 + sizeof(then) + hmac_sha1_size())) {
6402e1
+		    (off_t)(strlen(path) + 1 + sizeof(then) + maclen)) {
6402e1
 			pam_syslog(pamh, LOG_NOTICE, "timestamp file `%s' "
6402e1
 			       "appears to be corrupted", path);
6402e1
 			close(fd);
6402e1
@@ -487,8 +501,17 @@ pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
6402e1
 		message_end = message + strlen(path) + 1 + sizeof(then);
6402e1
 
6402e1
 		/* Regenerate the MAC. */
6402e1
-		hmac_sha1_generate_file(pamh, &mac, &maclen, TIMESTAMPKEY, 0, 0,
6402e1
-					message, message_end - message);
6402e1
+#ifdef WITH_OPENSSL
6402e1
+		if (hmac_generate(pamh, debug, &mac, &maclen, TIMESTAMPKEY,
6402e1
+				  ROOT_USER, ROOT_GROUP, message, message_end - message)) {
6402e1
+			close(fd);
6402e1
+			free(message);
6402e1
+			return PAM_AUTH_ERR;
6402e1
+		}
6402e1
+#else
6402e1
+		hmac_sha1_generate_file(pamh, &mac, &maclen, TIMESTAMPKEY,
6402e1
+					ROOT_USER, ROOT_GROUP, message, message_end - message);
6402e1
+#endif /* WITH_OPENSSL */
6402e1
 		if ((mac == NULL) ||
6402e1
 		    (memcmp(path, message, strlen(path)) != 0) ||
6402e1
 		    (memcmp(mac, message_end, maclen) != 0)) {
6402e1
@@ -605,8 +628,16 @@ pam_sm_open_session(pam_handle_t *pamh, int flags UNUSED, int argc, const char *
6402e1
 		}
6402e1
 	}
6402e1
 
6402e1
+#ifdef WITH_OPENSSL
6402e1
+	if (hmac_size(pamh, debug, &maclen)) {
6402e1
+		return PAM_SESSION_ERR;
6402e1
+	}
6402e1
+#else
6402e1
+	maclen = hmac_sha1_size();
6402e1
+#endif /* WITH_OPENSSL */
6402e1
+
6402e1
 	/* Generate the message. */
6402e1
-	text = malloc(strlen(path) + 1 + sizeof(now) + hmac_sha1_size());
6402e1
+	text = malloc(strlen(path) + 1 + sizeof(now) + maclen);
6402e1
 	if (text == NULL) {
6402e1
 		pam_syslog(pamh, LOG_CRIT, "unable to allocate memory: %m");
6402e1
 		return PAM_SESSION_ERR;
6402e1
@@ -621,15 +652,21 @@ pam_sm_open_session(pam_handle_t *pamh, int flags UNUSED, int argc, const char *
6402e1
 	p += sizeof(now);
6402e1
 
6402e1
 	/* Generate the MAC and append it to the plaintext. */
6402e1
-	hmac_sha1_generate_file(pamh, &mac, &maclen,
6402e1
-				TIMESTAMPKEY,
6402e1
-				0, 0,
6402e1
-				text, p - text);
6402e1
+#ifdef WITH_OPENSSL
6402e1
+	if (hmac_generate(pamh, debug, &mac, &maclen, TIMESTAMPKEY,
6402e1
+			  ROOT_USER, ROOT_GROUP, text, p - text)) {
6402e1
+		free(text);
6402e1
+		return PAM_SESSION_ERR;
6402e1
+	}
6402e1
+#else
6402e1
+	hmac_sha1_generate_file(pamh, &mac, &maclen, TIMESTAMPKEY,
6402e1
+				ROOT_USER, ROOT_GROUP, text, p - text);
6402e1
 	if (mac == NULL) {
6402e1
 		pam_syslog(pamh, LOG_ERR, "failure generating MAC: %m");
6402e1
 		free(text);
6402e1
 		return PAM_SESSION_ERR;
6402e1
 	}
6402e1
+#endif /* WITH_OPENSSL */
6402e1
 	memmove(p, mac, maclen);
6402e1
 	p += maclen;
6402e1
 	free(mac);
6402e1
-- 
6402e1
2.31.1
6402e1