Skip to content

Commit 1c1e963

Browse files
committed
[3.10] pythongh-118224: Load default OpenSSL provider for nonsecurity algorithms
When OpenSSL is configured to only load "base+fips" providers into the Null library context, md5 might not be available at all. In such cases currently CPython fallsback to internal hashlib implementation is there is one - as there might not be if one compiles python with --with-builtin-hashlib-hashes=blake2. With this change "default" provider is attempted to be loaded to access nonsecurity hashes.
1 parent 812245e commit 1c1e963

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Modules/_hashopenssl.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#define PY_OPENSSL_HAS_BLAKE2 1
5252

5353
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
54+
#include <openssl/provider.h>
5455
#define PY_EVP_MD EVP_MD
5556
#define PY_EVP_MD_fetch(algorithm, properties) EVP_MD_fetch(NULL, algorithm, properties)
5657
#define PY_EVP_MD_up_ref(md) EVP_MD_up_ref(md)
@@ -217,6 +218,17 @@ typedef struct {
217218
_Py_hashtable_t *hashtable;
218219
} _hashlibstate;
219220

221+
static void try_load_default_provider(void) {
222+
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
223+
/* Load the default config file, and expected providers */
224+
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
225+
if (!OSSL_PROVIDER_available(NULL, "default")) {
226+
/* System is configured without the default provider */
227+
OSSL_PROVIDER_load(NULL, "default");
228+
}
229+
#endif
230+
}
231+
220232
static inline _hashlibstate*
221233
get_hashlib_state(PyObject *module)
222234
{
@@ -338,6 +350,7 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht)
338350
break;
339351
case Py_ht_evp_nosecurity:
340352
if (entry->evp_nosecurity == NULL) {
353+
try_load_default_provider();
341354
entry->evp_nosecurity = PY_EVP_MD_fetch(entry->ossl_name, "-fips");
342355
}
343356
digest = entry->evp_nosecurity;
@@ -355,6 +368,7 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht)
355368
digest = PY_EVP_MD_fetch(name, NULL);
356369
break;
357370
case Py_ht_evp_nosecurity:
371+
try_load_default_provider();
358372
digest = PY_EVP_MD_fetch(name, "-fips");
359373
break;
360374
}

0 commit comments

Comments
 (0)