Blame SOURCES/0015-downstream-Allow-KRB5KDF-MD5-and-MD4-in-FIPS-mode.patch

905476
From 6aea69170c2064aaea73ad3283b6d7dd0cae47e1 Mon Sep 17 00:00:00 2001
905476
From: Julien Rische <jrische@redhat.com>
905476
Date: Thu, 19 Jan 2023 19:22:27 +0100
905476
Subject: [PATCH] [downstream] Allow KRB5KDF, MD5, and MD4 in FIPS mode
905476
905476
OpenSSL's restrictions to use KRB5KDF, MD5, and MD4 in FIPS mode are
905476
bypassed in case AES SHA-1 HMAC or RC4 encryption types are allowed by
905476
the crypto policy.
905476
---
905476
 .../crypto/openssl/hash_provider/hash_evp.c   | 97 +++++++++++++++++--
905476
 src/lib/crypto/openssl/kdf.c                  |  2 +-
905476
 2 files changed, 89 insertions(+), 10 deletions(-)
905476
905476
diff --git a/src/lib/crypto/openssl/hash_provider/hash_evp.c b/src/lib/crypto/openssl/hash_provider/hash_evp.c
905476
index 11659908bb..eb2e693e9f 100644
905476
--- a/src/lib/crypto/openssl/hash_provider/hash_evp.c
905476
+++ b/src/lib/crypto/openssl/hash_provider/hash_evp.c
905476
@@ -44,6 +44,49 @@
905476
 #define EVP_MD_CTX_free EVP_MD_CTX_destroy
905476
 #endif
905476
 
905476
+#include <openssl/provider.h>
905476
+#include <openssl/fips.h>
905476
+#include <threads.h>
905476
+
905476
+typedef struct ossl_lib_md_context {
905476
+    OSSL_LIB_CTX *libctx;
905476
+    OSSL_PROVIDER *default_provider;
905476
+    OSSL_PROVIDER *legacy_provider;
905476
+} ossl_md_context_t;
905476
+
905476
+static thread_local ossl_md_context_t *ossl_md_ctx = NULL;
905476
+
905476
+static krb5_error_code
905476
+init_ossl_md_ctx(ossl_md_context_t *ctx, const char *algo)
905476
+{
905476
+    ctx->libctx = OSSL_LIB_CTX_new();
905476
+    if (!ctx->libctx)
905476
+        return KRB5_CRYPTO_INTERNAL;
905476
+
905476
+    /* Load both legacy and default provider as both may be needed. */
905476
+    ctx->default_provider = OSSL_PROVIDER_load(ctx->libctx, "default");
905476
+    ctx->legacy_provider = OSSL_PROVIDER_load(ctx->libctx, "legacy");
905476
+
905476
+    if (!(ctx->default_provider && ctx->legacy_provider))
905476
+        return KRB5_CRYPTO_INTERNAL;
905476
+
905476
+    return 0;
905476
+}
905476
+
905476
+static void
905476
+deinit_ossl_ctx(ossl_md_context_t *ctx)
905476
+{
905476
+    if (ctx->legacy_provider)
905476
+        OSSL_PROVIDER_unload(ctx->legacy_provider);
905476
+
905476
+    if (ctx->default_provider)
905476
+        OSSL_PROVIDER_unload(ctx->default_provider);
905476
+
905476
+    if (ctx->libctx)
905476
+        OSSL_LIB_CTX_free(ctx->libctx);
905476
+}
905476
+
905476
+
905476
 static krb5_error_code
905476
 hash_evp(const EVP_MD *type, const krb5_crypto_iov *data, size_t num_data,
905476
          krb5_data *output)
905476
@@ -60,11 +103,6 @@ hash_evp(const EVP_MD *type, const krb5_crypto_iov *data, size_t num_data,
905476
     if (ctx == NULL)
905476
         return ENOMEM;
905476
 
905476
-    if (type == EVP_md4() || type == EVP_md5()) {
905476
-        /* See comments below in hash_md4() and hash_md5(). */
905476
-        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
905476
-    }
905476
-
905476
     ok = EVP_DigestInit_ex(ctx, type, NULL);
905476
     for (i = 0; i < num_data; i++) {
905476
         if (!SIGN_IOV(&data[i]))
905476
@@ -77,6 +115,43 @@ hash_evp(const EVP_MD *type, const krb5_crypto_iov *data, size_t num_data,
905476
     return ok ? 0 : KRB5_CRYPTO_INTERNAL;
905476
 }
905476
 
905476
+static krb5_error_code
905476
+hash_legacy_evp(const char *algo, const krb5_crypto_iov *data, size_t num_data,
905476
+                krb5_data *output)
905476
+{
905476
+    krb5_error_code err;
905476
+    EVP_MD *md = NULL;
905476
+
905476
+    if (!ossl_md_ctx) {
905476
+        ossl_md_ctx = malloc(sizeof(ossl_md_context_t));
905476
+        if (!ossl_md_ctx) {
905476
+            err = ENOMEM;
905476
+            goto end;
905476
+        }
905476
+
905476
+        err = init_ossl_md_ctx(ossl_md_ctx, algo);
905476
+        if (err) {
905476
+            deinit_ossl_ctx(ossl_md_ctx);
905476
+            free(ossl_md_ctx);
905476
+            ossl_md_ctx = NULL;
905476
+            goto end;
905476
+        }
905476
+    }
905476
+
905476
+    md = EVP_MD_fetch(ossl_md_ctx->libctx, algo, NULL);
905476
+    if (!md) {
905476
+        err = KRB5_CRYPTO_INTERNAL;
905476
+        goto end;
905476
+    }
905476
+
905476
+    err = hash_evp(md, data, num_data, output);
905476
+
905476
+end:
905476
+    if (md)
905476
+        EVP_MD_free(md);
905476
+
905476
+    return err;
905476
+}
905476
 #endif
905476
 
905476
 #ifdef K5_OPENSSL_MD4
905476
@@ -88,7 +163,8 @@ hash_md4(const krb5_crypto_iov *data, size_t num_data, krb5_data *output)
905476
      * by IPA.  These keys are only used along a (separately) secured channel
905476
      * for legacy reasons when performing trusts to Active Directory.
905476
      */
905476
-    return hash_evp(EVP_md4(), data, num_data, output);
905476
+    return FIPS_mode() ? hash_legacy_evp("MD4", data, num_data, output)
905476
+                       : hash_evp(EVP_md4(), data, num_data, output);
905476
 }
905476
 
905476
 const struct krb5_hash_provider krb5int_hash_md4 = {
905476
@@ -100,9 +176,12 @@ const struct krb5_hash_provider krb5int_hash_md4 = {
905476
 static krb5_error_code
905476
 hash_md5(const krb5_crypto_iov *data, size_t num_data, krb5_data *output)
905476
 {
905476
-    /* MD5 is needed in FIPS mode for communication with RADIUS servers.  This
905476
-     * is gated in libkrad by libdefaults->radius_md5_fips_override. */
905476
-    return hash_evp(EVP_md5(), data, num_data, output);
905476
+    /*
905476
+     * MD5 is needed in FIPS mode for communication with RADIUS servers.  This
905476
+     * is gated in libkrad by libdefaults->radius_md5_fips_override.
905476
+     */
905476
+    return FIPS_mode() ? hash_legacy_evp("MD5", data, num_data, output)
905476
+                       : hash_evp(EVP_md5(), data, num_data, output);
905476
 }
905476
 
905476
 const struct krb5_hash_provider krb5int_hash_md5 = {
905476
diff --git a/src/lib/crypto/openssl/kdf.c b/src/lib/crypto/openssl/kdf.c
905476
index 5a43c3d9eb..8528ddc4a9 100644
905476
--- a/src/lib/crypto/openssl/kdf.c
905476
+++ b/src/lib/crypto/openssl/kdf.c
905476
@@ -198,7 +198,7 @@ k5_derive_random_rfc3961(const struct krb5_enc_provider *enc, krb5_key key,
905476
         goto done;
905476
     }
905476
 
905476
-    kdf = EVP_KDF_fetch(NULL, "KRB5KDF", NULL);
905476
+    kdf = EVP_KDF_fetch(NULL, "KRB5KDF", "-fips");
905476
     if (kdf == NULL) {
905476
         ret = KRB5_CRYPTO_INTERNAL;
905476
         goto done;
905476
-- 
905476
2.38.1
905476