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