]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-47101: list only activated algorithms in hashlib.algorithms_available (GH-32076)
authorChristian Heimes <christian@python.org>
Wed, 23 Mar 2022 20:30:05 +0000 (22:30 +0200)
committerGitHub <noreply@github.com>
Wed, 23 Mar 2022 20:30:05 +0000 (13:30 -0700)
Lib/test/test_hashlib.py
Misc/NEWS.d/next/Library/2022-03-23-15-31-02.bpo-47101.rVSld-.rst [new file with mode: 0644]
Modules/_hashopenssl.c

index 369bbde6e78b6d7d9820c50e07ee08a3da9b6104..d2a92147d5f0244ecf261fad7ab50dfcc99f3bdc 100644 (file)
@@ -223,6 +223,10 @@ class HashLibTestCase(unittest.TestCase):
     def test_algorithms_available(self):
         self.assertTrue(set(hashlib.algorithms_guaranteed).
                             issubset(hashlib.algorithms_available))
+        # all available algorithms must be loadable, bpo-47101
+        self.assertNotIn("undefined", hashlib.algorithms_available)
+        for name in hashlib.algorithms_available:
+            digest = hashlib.new(name, usedforsecurity=False)
 
     def test_usedforsecurity_true(self):
         hashlib.new("sha256", usedforsecurity=True)
diff --git a/Misc/NEWS.d/next/Library/2022-03-23-15-31-02.bpo-47101.rVSld-.rst b/Misc/NEWS.d/next/Library/2022-03-23-15-31-02.bpo-47101.rVSld-.rst
new file mode 100644 (file)
index 0000000..1a65024
--- /dev/null
@@ -0,0 +1,4 @@
+:const:`hashlib.algorithms_available` now lists only algorithms that are
+provided by activated crypto providers on OpenSSL 3.0. Legacy algorithms are
+not listed unless the legacy provider has been loaded into the default
+OSSL context.
index bb9487204e741586242a86a47d8ba112700ba984..203366e380d4e72e3a27bdc0fe0e4338a2ebec97 100644 (file)
@@ -1836,15 +1836,21 @@ typedef struct _internal_name_mapper_state {
 
 /* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */
 static void
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
+_openssl_hash_name_mapper(EVP_MD *md, void *arg)
+#else
 _openssl_hash_name_mapper(const EVP_MD *md, const char *from,
                           const char *to, void *arg)
+#endif
 {
     _InternalNameMapperState *state = (_InternalNameMapperState *)arg;
     PyObject *py_name;
 
     assert(state != NULL);
-    if (md == NULL)
+    // ignore all undefined providers
+    if ((md == NULL) || (EVP_MD_nid(md) == NID_undef)) {
         return;
+    }
 
     py_name = py_digest_name(md);
     if (py_name == NULL) {
@@ -1870,7 +1876,12 @@ hashlib_md_meth_names(PyObject *module)
         return -1;
     }
 
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
+    // get algorithms from all activated providers in default context
+    EVP_MD_do_all_provided(NULL, &_openssl_hash_name_mapper, &state);
+#else
     EVP_MD_do_all(&_openssl_hash_name_mapper, &state);
+#endif
 
     if (state.error) {
         Py_DECREF(state.set);