Skip to content

Commit a1fb08f

Browse files
committed
[3.12] gh-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 2eaf9ba commit a1fb08f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Modules/_hashopenssl.c

+14
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#endif
5757

5858
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
59+
#include <openssl/provider.h>
5960
#define PY_EVP_MD EVP_MD
6061
#define PY_EVP_MD_fetch(algorithm, properties) EVP_MD_fetch(NULL, algorithm, properties)
6162
#define PY_EVP_MD_up_ref(md) EVP_MD_up_ref(md)
@@ -265,6 +266,17 @@ typedef struct {
265266
_Py_hashtable_t *hashtable;
266267
} _hashlibstate;
267268

269+
static void try_load_default_provider(void) {
270+
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
271+
/* Load the default config file, and expected providers */
272+
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
273+
if (!OSSL_PROVIDER_available(NULL, "default")) {
274+
/* System is configured without the default provider */
275+
OSSL_PROVIDER_load(NULL, "default");
276+
}
277+
#endif
278+
}
279+
268280
static inline _hashlibstate*
269281
get_hashlib_state(PyObject *module)
270282
{
@@ -386,6 +398,7 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht)
386398
break;
387399
case Py_ht_evp_nosecurity:
388400
if (entry->evp_nosecurity == NULL) {
401+
try_load_default_provider();
389402
entry->evp_nosecurity = PY_EVP_MD_fetch(entry->ossl_name, "-fips");
390403
}
391404
digest = entry->evp_nosecurity;
@@ -403,6 +416,7 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht)
403416
digest = PY_EVP_MD_fetch(name, NULL);
404417
break;
405418
case Py_ht_evp_nosecurity:
419+
try_load_default_provider();
406420
digest = PY_EVP_MD_fetch(name, "-fips");
407421
break;
408422
}

0 commit comments

Comments
 (0)