Skip to content

Commit 8f4568c

Browse files
committed
Merge in 'release/7.0' changes
2 parents 46826e2 + 8473eeb commit 8f4568c

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/native/libs/System.Security.Cryptography.Native/opensslshim.h

+4
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ const EVP_CIPHER* EVP_chacha20_poly1305(void);
326326
REQUIRED_FUNCTION(EVP_MD_CTX_copy_ex) \
327327
RENAMED_FUNCTION(EVP_MD_CTX_free, EVP_MD_CTX_destroy) \
328328
RENAMED_FUNCTION(EVP_MD_CTX_new, EVP_MD_CTX_create) \
329+
REQUIRED_FUNCTION(EVP_MD_CTX_set_flags) \
330+
LIGHTUP_FUNCTION(EVP_MD_fetch) \
329331
RENAMED_FUNCTION(EVP_MD_get_size, EVP_MD_size) \
330332
REQUIRED_FUNCTION(EVP_PKCS82PKEY) \
331333
REQUIRED_FUNCTION(EVP_PKEY2PKCS8) \
@@ -805,6 +807,8 @@ FOR_ALL_OPENSSL_FUNCTIONS
805807
#define EVP_MD_CTX_copy_ex EVP_MD_CTX_copy_ex_ptr
806808
#define EVP_MD_CTX_free EVP_MD_CTX_free_ptr
807809
#define EVP_MD_CTX_new EVP_MD_CTX_new_ptr
810+
#define EVP_MD_CTX_set_flags EVP_MD_CTX_set_flags_ptr
811+
#define EVP_MD_fetch EVP_MD_fetch_ptr
808812
#define EVP_MD_get_size EVP_MD_get_size_ptr
809813
#define EVP_PKCS82PKEY EVP_PKCS82PKEY_ptr
810814
#define EVP_PKEY2PKCS8 EVP_PKEY2PKCS8_ptr

src/native/libs/System.Security.Cryptography.Native/osslcompat_30.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ void ERR_new(void);
1919
void ERR_set_debug(const char *file, int line, const char *func);
2020
void ERR_set_error(int lib, int reason, const char *fmt, ...);
2121
int EVP_CIPHER_get_nid(const EVP_CIPHER *e);
22+
EVP_MD* EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, const char *properties);
2223
int EVP_MD_get_size(const EVP_MD* md);
2324
int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX* ctx, int bits);
2425
int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX* ctx, const EVP_MD* md);

src/native/libs/System.Security.Cryptography.Native/pal_evp.c

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#include "openssl.h"
45
#include "pal_evp.h"
56

67
#include <assert.h>
8+
#include <pthread.h>
79

810
#define SUCCESS 1
911

12+
static const EVP_MD* g_evpFetchMd5 = NULL;
13+
static pthread_once_t g_evpFetch = PTHREAD_ONCE_INIT;
14+
15+
static void EnsureFetchEvpMdAlgorithms(void)
16+
{
17+
// This is called from a pthread_once - this method should not be called directly.
18+
19+
#ifdef NEED_OPENSSL_3_0
20+
if (API_EXISTS(EVP_MD_fetch))
21+
{
22+
ERR_clear_error();
23+
24+
// Try to fetch an MD5 implementation that will work regardless if
25+
// FIPS is enforced or not.
26+
g_evpFetchMd5 = EVP_MD_fetch(NULL, "MD5", "-fips");
27+
}
28+
#endif
29+
30+
// No error queue impact.
31+
// If EVP_MD_fetch is unavailable, use the implicit loader. If it failed, use the implicit loader as a last resort.
32+
if (g_evpFetchMd5 == NULL)
33+
{
34+
g_evpFetchMd5 = EVP_md5();
35+
}
36+
}
37+
1038
EVP_MD_CTX* CryptoNative_EvpMdCtxCreate(const EVP_MD* type)
1139
{
1240
ERR_clear_error();
@@ -22,6 +50,13 @@ EVP_MD_CTX* CryptoNative_EvpMdCtxCreate(const EVP_MD* type)
2250
return NULL;
2351
}
2452

53+
// For OpenSSL 1.x, set the non-FIPS allow flag for MD5. OpenSSL 3 does this differently with EVP_MD_fetch
54+
// and no longer has this flag.
55+
if (CryptoNative_OpenSslVersionNumber() < OPENSSL_VERSION_3_0_RTM && type == EVP_md5())
56+
{
57+
EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
58+
}
59+
2560
int ret = EVP_DigestInit_ex(ctx, type, NULL);
2661
if (!ret)
2762
{
@@ -147,8 +182,8 @@ int32_t CryptoNative_EvpMdSize(const EVP_MD* md)
147182

148183
const EVP_MD* CryptoNative_EvpMd5()
149184
{
150-
// No error queue impact.
151-
return EVP_md5();
185+
pthread_once(&g_evpFetch, EnsureFetchEvpMdAlgorithms);
186+
return g_evpFetchMd5;
152187
}
153188

154189
const EVP_MD* CryptoNative_EvpSha1()

0 commit comments

Comments
 (0)