Blame SOURCES/0010-Test-Use-EVP_MAC_xxx-with-OpenSSL-3.0.patch

a56c8e
From 89b2bd01f6fa1e267f57b2ceeb2ffaafb9cdb7c0 Mon Sep 17 00:00:00 2001
a56c8e
From: Petr Gotthard <petr.gotthard@centrum.cz>
a56c8e
Date: Sun, 18 Jul 2021 14:56:18 +0200
a56c8e
Subject: Test: Use EVP_MAC_xxx with OpenSSL 3.0
a56c8e
MIME-Version: 1.0
a56c8e
Content-Type: text/plain; charset=UTF-8
a56c8e
Content-Transfer-Encoding: 8bit
a56c8e
a56c8e
Drop support for OpenSSL < 1.1.0 and add support for OpenSSL >= 3.0.0.
a56c8e
a56c8e
The HMAC_Update is deprecated in OpenSSL 3.0, but the replacement
a56c8e
EVP_MAC_update was added in OpenSSL 3.0, so version specific code is
a56c8e
needed.
a56c8e
a56c8e
Signed-off-by: Petr Gotthard <petr.gotthard@centrum.cz>
a56c8e
---
a56c8e
 test/integration/sys-util.c | 50 +++++++++++++++++++++++--------------
a56c8e
 1 file changed, 31 insertions(+), 19 deletions(-)
a56c8e
a56c8e
diff --git a/test/integration/sys-util.c b/test/integration/sys-util.c
a56c8e
index af83cf55..5865f002 100644
a56c8e
--- a/test/integration/sys-util.c
a56c8e
+++ b/test/integration/sys-util.c
a56c8e
@@ -13,10 +13,13 @@
a56c8e
 #include <string.h>
a56c8e
 #include <assert.h>
a56c8e
 
a56c8e
+#include <openssl/evp.h>
a56c8e
 #include <openssl/sha.h>
a56c8e
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a56c8e
 #include <openssl/hmac.h>
a56c8e
-#include <openssl/evp.h>
a56c8e
-#include <openssl/opensslv.h>
a56c8e
+#else
a56c8e
+#include <openssl/core_names.h>
a56c8e
+#endif
a56c8e
 
a56c8e
 #define LOGMODULE testintegration
a56c8e
 #include "util/log.h"
a56c8e
@@ -489,22 +492,18 @@ hmac(
a56c8e
     TPM2B_DIGEST **buffer_list,
a56c8e
     TPM2B_DIGEST *out)
a56c8e
 {
a56c8e
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
a56c8e
-    HMAC_CTX *ctx;
a56c8e
-#else
a56c8e
-    HMAC_CTX _ctx;
a56c8e
-    HMAC_CTX *ctx = &_ctx;
a56c8e
-#endif
a56c8e
-    EVP_MD *evp;
a56c8e
     int rc = 1, i;
a56c8e
-    unsigned int *buf = NULL, size;
a56c8e
+    unsigned int *buf = NULL;
a56c8e
     uint8_t *buf_ptr;
a56c8e
+    EVP_MD *evp;
a56c8e
 
a56c8e
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
a56c8e
-    /* HMAC_CTX_new and HMAC_CTX_free are new in openSSL 1.1.0 */
a56c8e
-    ctx = HMAC_CTX_new();
a56c8e
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a56c8e
+    unsigned int size;
a56c8e
+    HMAC_CTX *ctx = HMAC_CTX_new();
a56c8e
 #else
a56c8e
-    HMAC_CTX_init(ctx);
a56c8e
+    size_t size;
a56c8e
+    EVP_MAC *hmac = EVP_MAC_fetch(NULL, "HMAC", NULL);
a56c8e
+    EVP_MAC_CTX *ctx = EVP_MAC_CTX_new(hmac);
a56c8e
 #endif
a56c8e
 
a56c8e
     if (!ctx)
a56c8e
@@ -538,21 +537,33 @@ hmac(
a56c8e
 
a56c8e
     buf_ptr = (uint8_t *)buf;
a56c8e
 
a56c8e
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
a56c8e
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a56c8e
     rc = HMAC_Init_ex(ctx, key, key_len, evp, NULL);
a56c8e
 #else
a56c8e
-    rc = HMAC_Init(ctx, key, key_len, evp);
a56c8e
-#endif
a56c8e
+    OSSL_PARAM params[2];
a56c8e
 
a56c8e
+    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST,
a56c8e
+                                                 (char *)EVP_MD_get0_name(evp), 0);
a56c8e
+    params[1] = OSSL_PARAM_construct_end();
a56c8e
+    rc = EVP_MAC_init(ctx, key, key_len, params);
a56c8e
+#endif
a56c8e
     if (rc != 1)
a56c8e
         goto out;
a56c8e
     for (i = 0; buffer_list[i] != 0; i++) {
a56c8e
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a56c8e
         rc = HMAC_Update(ctx, buffer_list[i]->buffer, buffer_list[i]->size);
a56c8e
+#else
a56c8e
+        rc = EVP_MAC_update(ctx, buffer_list[i]->buffer, buffer_list[i]->size);
a56c8e
+#endif
a56c8e
         if (rc != 1)
a56c8e
             goto out;
a56c8e
     }
a56c8e
     /* buf_ptr has to be 4 bytes alligned for whatever reason */
a56c8e
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a56c8e
     rc = HMAC_Final(ctx, buf_ptr, &size);
a56c8e
+#else
a56c8e
+    rc = EVP_MAC_final(ctx, buf_ptr, &size, out->size);
a56c8e
+#endif
a56c8e
     if (rc != 1)
a56c8e
         goto out;
a56c8e
 
a56c8e
@@ -561,10 +572,11 @@ hmac(
a56c8e
     memcpy(out->buffer, buf, out->size);
a56c8e
 
a56c8e
 out:
a56c8e
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
a56c8e
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
a56c8e
     HMAC_CTX_free(ctx);
a56c8e
 #else
a56c8e
-    HMAC_CTX_cleanup(ctx);
a56c8e
+    EVP_MAC_CTX_free(ctx);
a56c8e
+    EVP_MAC_free(hmac);
a56c8e
 #endif
a56c8e
 
a56c8e
     if (buf)
a56c8e
-- 
a56c8e
2.26.3
a56c8e