From f2fc8dd1549cd4662ad073d8d9689eaa0747385a Mon Sep 17 00:00:00 2001
From: rpm-build <rpm-build>
Date: Wed, 6 Mar 2024 19:17:15 +0100
Subject: [PATCH 20/59] FIPS: INTEG-CHECK: Embed hmac in fips.so - NOTE

Corrected by squashing in:
0052-Restore-the-correct-verify_integrity-function.patch

Patch-name: 0033-FIPS-embed-hmac.patch
Patch-id: 33
Patch-status: |
    # # Embed HMAC into the fips.so
    # Modify fips self test as per
    # https://github.com/simo5/openssl/commit/9b95ef8bd2f5ac862e5eee74c724b535f1a8578a
From-dist-git-commit: 4334bc837fbc64d14890fdc51679a80770d498ce
---
 providers/fips/self_test.c | 170 ++++++++++++++++++++++++++++++++++---
 test/fipsmodule.cnf        |   2 +
 2 files changed, 161 insertions(+), 11 deletions(-)
 create mode 100644 test/fipsmodule.cnf

diff --git a/providers/fips/self_test.c b/providers/fips/self_test.c
index 456efd139e..c89e91b587 100644
--- a/providers/fips/self_test.c
+++ b/providers/fips/self_test.c
@@ -235,13 +235,137 @@ err:
     return ok;
 }
 
+#define HMAC_LEN 32
+/*
+ * The __attribute__ ensures we've created the .rodata1 section
+ * static ensures it's zero filled
+*/
+static const unsigned char __attribute__ ((section (".rodata1"))) fips_hmac_container[HMAC_LEN] = {0};
+
 /*
  * Calculate the HMAC SHA256 of data read using a BIO and read_cb, and verify
  * the result matches the expected value.
  * Return 1 if verified, or 0 if it fails.
  */
+
+#ifndef __USE_GNU
+#define __USE_GNU
+#include <dlfcn.h>
+#undef __USE_GNU
+#else
+#include <dlfcn.h>
+#endif
+#include <link.h>
+
+static int verify_integrity_rodata(OSSL_CORE_BIO *bio,
+                                   OSSL_FUNC_BIO_read_ex_fn read_ex_cb,
+                                   const unsigned char *expected,
+                                   size_t expected_len, OSSL_LIB_CTX *libctx,
+                                   OSSL_SELF_TEST *ev, const char *event_type)
+{
+    int ret = 0, status;
+    unsigned char out[MAX_MD_SIZE];
+    unsigned char buf[INTEGRITY_BUF_SIZE];
+    size_t bytes_read = 0, out_len = 0;
+    EVP_MAC *mac = NULL;
+    EVP_MAC_CTX *ctx = NULL;
+    OSSL_PARAM params[2], *p = params;
+    Dl_info info;
+    void *extra_info = NULL;
+    struct link_map *lm = NULL;
+    unsigned long paddr;
+    unsigned long off = 0;
+
+    if (expected_len != HMAC_LEN)
+        goto err;
+
+    if (!integrity_self_test(ev, libctx))
+        goto err;
+
+    OSSL_SELF_TEST_onbegin(ev, event_type, OSSL_SELF_TEST_DESC_INTEGRITY_HMAC);
+
+    if (!dladdr1 ((const void *)fips_hmac_container,
+                &info, &extra_info, RTLD_DL_LINKMAP))
+        goto err;
+    lm = extra_info;
+    paddr = (unsigned long)fips_hmac_container - lm->l_addr;
+
+    mac = EVP_MAC_fetch(libctx, MAC_NAME, NULL);
+    if (mac == NULL)
+        goto err;
+    ctx = EVP_MAC_CTX_new(mac);
+    if (ctx == NULL)
+        goto err;
+
+    *p++ = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME, 0);
+    *p = OSSL_PARAM_construct_end();
+
+    if (!EVP_MAC_init(ctx, fixed_key, sizeof(fixed_key), params))
+        goto err;
+
+    while ((off + INTEGRITY_BUF_SIZE) <= paddr) {
+        status = read_ex_cb(bio, buf, sizeof(buf), &bytes_read);
+        if (status != 1)
+            break;
+        if (!EVP_MAC_update(ctx, buf, bytes_read))
+            goto err;
+	off += bytes_read;
+    }
+
+    if (off < paddr) {
+        int delta = paddr - off;
+        status = read_ex_cb(bio, buf, delta, &bytes_read);
+        if (status != 1)
+            goto err;
+        if (!EVP_MAC_update(ctx, buf, bytes_read))
+            goto err;
+	off += bytes_read;
+    }
+
+    /* read away the buffer */
+    status = read_ex_cb(bio, buf, HMAC_LEN, &bytes_read);
+    if (status != 1)
+        goto err;
+
+    /* check that it is the expect bytes, no point in continuing otherwise */
+   if (memcmp(expected, buf, HMAC_LEN) != 0)
+        goto err;
+
+    /* replace in-file HMAC buffer with the original zeros */
+    memset(buf, 0, HMAC_LEN);
+    if (!EVP_MAC_update(ctx, buf, HMAC_LEN))
+        goto err;
+    off += HMAC_LEN;
+
+    while (bytes_read > 0) {
+        status = read_ex_cb(bio, buf, sizeof(buf), &bytes_read);
+        if (status != 1)
+            break;
+        if (!EVP_MAC_update(ctx, buf, bytes_read))
+            goto err;
+	off += bytes_read;
+    }
+
+    if (!EVP_MAC_final(ctx, out, &out_len, sizeof(out)))
+        goto err;
+
+    OSSL_SELF_TEST_oncorrupt_byte(ev, out);
+    if (expected_len != out_len
+            || memcmp(expected, out, out_len) != 0)
+        goto err;
+    ret = 1;
+err:
+    OSSL_SELF_TEST_onend(ev, ret);
+    EVP_MAC_CTX_free(ctx);
+    EVP_MAC_free(mac);
+# ifdef OPENSSL_PEDANTIC_ZEROIZATION
+    OPENSSL_cleanse(out, sizeof(out));
+# endif
+    return ret;
+}
+
 static int verify_integrity(OSSL_CORE_BIO *bio, OSSL_FUNC_BIO_read_ex_fn read_ex_cb,
-                            unsigned char *expected, size_t expected_len,
+                            const unsigned char *expected, size_t expected_len,
                             OSSL_LIB_CTX *libctx, OSSL_SELF_TEST *ev,
                             const char *event_type)
 {
@@ -253,6 +377,9 @@ static int verify_integrity(OSSL_CORE_BIO *bio, OSSL_FUNC_BIO_read_ex_fn read_ex
     EVP_MAC_CTX *ctx = NULL;
     OSSL_PARAM params[2], *p = params;
 
+    if (expected_len != HMAC_LEN)
+        goto err;
+
     if (!integrity_self_test(ev, libctx))
         goto err;
 
@@ -316,7 +443,8 @@ int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
     int ok = 0;
     long checksum_len;
     OSSL_CORE_BIO *bio_module = NULL;
-    unsigned char *module_checksum = NULL;
+    const unsigned char *module_checksum = NULL;
+    unsigned char *alloc_checksum = NULL;
     OSSL_SELF_TEST *ev = NULL;
     EVP_RAND *testrand = NULL;
     EVP_RAND_CTX *rng;
@@ -352,8 +480,7 @@ int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
         return 0;
     }
 
-    if (st == NULL
-            || st->module_checksum_data == NULL) {
+    if (st == NULL) {
         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
         goto end;
     }
@@ -362,8 +489,15 @@ int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
     if (ev == NULL)
         goto end;
 
-    module_checksum = OPENSSL_hexstr2buf(st->module_checksum_data,
-                                         &checksum_len);
+    if (st->module_checksum_data == NULL) {
+        module_checksum = fips_hmac_container;
+        checksum_len = sizeof(fips_hmac_container);
+    } else {
+        alloc_checksum = OPENSSL_hexstr2buf(st->module_checksum_data,
+                                            &checksum_len);
+        module_checksum = alloc_checksum;
+    }
+
     if (module_checksum == NULL) {
         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
         goto end;
@@ -371,14 +505,28 @@ int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
     bio_module = (*st->bio_new_file_cb)(st->module_filename, "rb");
 
     /* Always check the integrity of the fips module */
-    if (bio_module == NULL
-            || !verify_integrity(bio_module, st->bio_read_ex_cb,
-                                 module_checksum, checksum_len, st->libctx,
-                                 ev, OSSL_SELF_TEST_TYPE_MODULE_INTEGRITY)) {
+    if (bio_module == NULL) {
         ERR_raise(ERR_LIB_PROV, PROV_R_MODULE_INTEGRITY_FAILURE);
         goto end;
     }
 
+    if (st->module_checksum_data == NULL) {
+        if (!verify_integrity_rodata(bio_module, st->bio_read_ex_cb,
+                                     module_checksum, checksum_len,
+                                     st->libctx, ev,
+                                     OSSL_SELF_TEST_TYPE_MODULE_INTEGRITY)) {
+            ERR_raise(ERR_LIB_PROV, PROV_R_MODULE_INTEGRITY_FAILURE);
+            goto end;
+        }
+    } else {
+        if (!verify_integrity(bio_module, st->bio_read_ex_cb,
+                              module_checksum, checksum_len, st->libctx,
+                              ev, OSSL_SELF_TEST_TYPE_MODULE_INTEGRITY)) {
+            ERR_raise(ERR_LIB_PROV, PROV_R_MODULE_INTEGRITY_FAILURE);
+            goto end;
+        }
+    }
+
     if (!SELF_TEST_kats(ev, st->libctx)) {
         ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_KAT_FAILURE);
         goto end;
@@ -398,7 +546,7 @@ int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
 end:
     EVP_RAND_free(testrand);
     OSSL_SELF_TEST_free(ev);
-    OPENSSL_free(module_checksum);
+    OPENSSL_free(alloc_checksum);
 
     if (st != NULL)
         (*st->bio_free_cb)(bio_module);
diff --git a/test/fipsmodule.cnf b/test/fipsmodule.cnf
new file mode 100644
index 0000000000..f05d0dedbe
--- /dev/null
+++ b/test/fipsmodule.cnf
@@ -0,0 +1,2 @@
+[fips_sect]
+activate = 1
-- 
2.51.0

