summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordotnet-bot <dotnet-bot@microsoft.com>2023-11-23 03:13:17 +0000
committerdotnet-bot <dotnet-bot@microsoft.com>2023-11-23 03:13:17 +0000
commit8f4568cdaa2f9654fd133a17cd2dcc55b5f42455 (patch)
treee5e923fcf564ede12cc0c23e8ba5b5c95cff603f
parent46826e2767d3676e3508aceb73286c46019ae625 (diff)
parent8473eeb913cf688e7d3dbdf09a6a1f434498ada3 (diff)
Merge in 'release/7.0' changesv7.0.15
-rw-r--r--src/native/libs/System.Security.Cryptography.Native/opensslshim.h4
-rw-r--r--src/native/libs/System.Security.Cryptography.Native/osslcompat_30.h1
-rw-r--r--src/native/libs/System.Security.Cryptography.Native/pal_evp.c39
3 files changed, 42 insertions, 2 deletions
diff --git a/src/native/libs/System.Security.Cryptography.Native/opensslshim.h b/src/native/libs/System.Security.Cryptography.Native/opensslshim.h
index efb0a9378ae7..efc0d4706027 100644
--- a/src/native/libs/System.Security.Cryptography.Native/opensslshim.h
+++ b/src/native/libs/System.Security.Cryptography.Native/opensslshim.h
@@ -326,6 +326,8 @@ const EVP_CIPHER* EVP_chacha20_poly1305(void);
REQUIRED_FUNCTION(EVP_MD_CTX_copy_ex) \
RENAMED_FUNCTION(EVP_MD_CTX_free, EVP_MD_CTX_destroy) \
RENAMED_FUNCTION(EVP_MD_CTX_new, EVP_MD_CTX_create) \
+ REQUIRED_FUNCTION(EVP_MD_CTX_set_flags) \
+ LIGHTUP_FUNCTION(EVP_MD_fetch) \
RENAMED_FUNCTION(EVP_MD_get_size, EVP_MD_size) \
REQUIRED_FUNCTION(EVP_PKCS82PKEY) \
REQUIRED_FUNCTION(EVP_PKEY2PKCS8) \
@@ -805,6 +807,8 @@ FOR_ALL_OPENSSL_FUNCTIONS
#define EVP_MD_CTX_copy_ex EVP_MD_CTX_copy_ex_ptr
#define EVP_MD_CTX_free EVP_MD_CTX_free_ptr
#define EVP_MD_CTX_new EVP_MD_CTX_new_ptr
+#define EVP_MD_CTX_set_flags EVP_MD_CTX_set_flags_ptr
+#define EVP_MD_fetch EVP_MD_fetch_ptr
#define EVP_MD_get_size EVP_MD_get_size_ptr
#define EVP_PKCS82PKEY EVP_PKCS82PKEY_ptr
#define EVP_PKEY2PKCS8 EVP_PKEY2PKCS8_ptr
diff --git a/src/native/libs/System.Security.Cryptography.Native/osslcompat_30.h b/src/native/libs/System.Security.Cryptography.Native/osslcompat_30.h
index 095dd3176e7a..5167f2a0fbcd 100644
--- a/src/native/libs/System.Security.Cryptography.Native/osslcompat_30.h
+++ b/src/native/libs/System.Security.Cryptography.Native/osslcompat_30.h
@@ -19,6 +19,7 @@ void ERR_new(void);
void ERR_set_debug(const char *file, int line, const char *func);
void ERR_set_error(int lib, int reason, const char *fmt, ...);
int EVP_CIPHER_get_nid(const EVP_CIPHER *e);
+EVP_MD* EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, const char *properties);
int EVP_MD_get_size(const EVP_MD* md);
int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX* ctx, int bits);
int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX* ctx, const EVP_MD* md);
diff --git a/src/native/libs/System.Security.Cryptography.Native/pal_evp.c b/src/native/libs/System.Security.Cryptography.Native/pal_evp.c
index fcdd1aef74f5..aa9ec3344aee 100644
--- a/src/native/libs/System.Security.Cryptography.Native/pal_evp.c
+++ b/src/native/libs/System.Security.Cryptography.Native/pal_evp.c
@@ -1,12 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+#include "openssl.h"
#include "pal_evp.h"
#include <assert.h>
+#include <pthread.h>
#define SUCCESS 1
+static const EVP_MD* g_evpFetchMd5 = NULL;
+static pthread_once_t g_evpFetch = PTHREAD_ONCE_INIT;
+
+static void EnsureFetchEvpMdAlgorithms(void)
+{
+ // This is called from a pthread_once - this method should not be called directly.
+
+#ifdef NEED_OPENSSL_3_0
+ if (API_EXISTS(EVP_MD_fetch))
+ {
+ ERR_clear_error();
+
+ // Try to fetch an MD5 implementation that will work regardless if
+ // FIPS is enforced or not.
+ g_evpFetchMd5 = EVP_MD_fetch(NULL, "MD5", "-fips");
+ }
+#endif
+
+ // No error queue impact.
+ // If EVP_MD_fetch is unavailable, use the implicit loader. If it failed, use the implicit loader as a last resort.
+ if (g_evpFetchMd5 == NULL)
+ {
+ g_evpFetchMd5 = EVP_md5();
+ }
+}
+
EVP_MD_CTX* CryptoNative_EvpMdCtxCreate(const EVP_MD* type)
{
ERR_clear_error();
@@ -22,6 +50,13 @@ EVP_MD_CTX* CryptoNative_EvpMdCtxCreate(const EVP_MD* type)
return NULL;
}
+ // For OpenSSL 1.x, set the non-FIPS allow flag for MD5. OpenSSL 3 does this differently with EVP_MD_fetch
+ // and no longer has this flag.
+ if (CryptoNative_OpenSslVersionNumber() < OPENSSL_VERSION_3_0_RTM && type == EVP_md5())
+ {
+ EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
+ }
+
int ret = EVP_DigestInit_ex(ctx, type, NULL);
if (!ret)
{
@@ -147,8 +182,8 @@ int32_t CryptoNative_EvpMdSize(const EVP_MD* md)
const EVP_MD* CryptoNative_EvpMd5()
{
- // No error queue impact.
- return EVP_md5();
+ pthread_once(&g_evpFetch, EnsureFetchEvpMdAlgorithms);
+ return g_evpFetchMd5;
}
const EVP_MD* CryptoNative_EvpSha1()