1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ #include "openssl.h"
4
5
#include "pal_evp.h"
5
6
6
7
#include <assert.h>
8
+ #include <pthread.h>
7
9
8
10
#define SUCCESS 1
9
11
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
+
10
38
EVP_MD_CTX * CryptoNative_EvpMdCtxCreate (const EVP_MD * type )
11
39
{
12
40
ERR_clear_error ();
@@ -22,6 +50,13 @@ EVP_MD_CTX* CryptoNative_EvpMdCtxCreate(const EVP_MD* type)
22
50
return NULL ;
23
51
}
24
52
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
+
25
60
int ret = EVP_DigestInit_ex (ctx , type , NULL );
26
61
if (!ret )
27
62
{
@@ -147,8 +182,8 @@ int32_t CryptoNative_EvpMdSize(const EVP_MD* md)
147
182
148
183
const EVP_MD * CryptoNative_EvpMd5 ()
149
184
{
150
- // No error queue impact.
151
- return EVP_md5 () ;
185
+ pthread_once ( & g_evpFetch , EnsureFetchEvpMdAlgorithms );
186
+ return g_evpFetchMd5 ;
152
187
}
153
188
154
189
const EVP_MD * CryptoNative_EvpSha1 ()
0 commit comments