From b2e24c56abfada37b0e1fc463af283d30d837c45 Mon Sep 17 00:00:00 2001 From: ege-arhan Date: Fri, 27 Nov 2026 11:55:35 +0000 Subject: [PATCH] gh-162712: Report the real name for provider digests in hashlib Digests fetched from an OpenSSL 3 provider may have NID_undef, so OBJ_nid2ln(0) returned "undefined" as hashlib.hash.name even though hashlib.new() supports the algorithm. Fall back to the name of the fetched EVP_MD object, which is what the documentation promises. Signed-off-by: ege-arhan --- Lib/test/test_hashlib.py | 14 ++++++++++++++ ...-11-27-11-52-03.gh-issue-162712.Kx3vQa.rst | 4 ++++ Modules/_hashopenssl.c | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-27-11-52-03.gh-issue-162712.Kx3vQa.rst diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index eff95fd8aa84f62..8c6ffdad8129439 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -484,6 +484,20 @@ def test_name_attribute(self): hashlib.new(h.name, usedforsecurity=False).name ) + def test_name_attribute_provider_digest(self): + # gh-162712: digests from an OpenSSL 3 provider can have NID_undef, + # but the exposed name must still work with hashlib.new(). + for requested in ("SHA256-192", "KECCAK-256"): + try: + h = hashlib.new(requested, usedforsecurity=False) + except ValueError: + continue + with self.subTest(requested=requested): + self.assertNotEqual(h.name, "undefined") + self.assertEqual(h.name, h.name.lower()) + self.assertEqual(hashlib.new(h.name).digest_size, + h.digest_size) + def test_large_update(self): aas = b'a' * 128 bees = b'b' * 127 diff --git a/Misc/NEWS.d/next/Library/2026-11-27-11-52-03.gh-issue-162712.Kx3vQa.rst b/Misc/NEWS.d/next/Library/2026-11-27-11-52-03.gh-issue-162712.Kx3vQa.rst new file mode 100644 index 000000000000000..f6ed38797351972 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-27-11-52-03.gh-issue-162712.Kx3vQa.rst @@ -0,0 +1,4 @@ +Fix :attr:`hashlib.hash.name` for digests provided by OpenSSL 3 providers. +These digests may not have a numeric identifier, in which case the name +used to be reported as ``"undefined"`` instead of the actual algorithm +name, so it could not be passed back to :func:`hashlib.new`. diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 9d7602b39d7b938..bc86e73bebd8c39 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -1041,6 +1041,25 @@ _hashlib_HASH_get_name(PyObject *op, void *Py_UNUSED(closure)) notify_ssl_error_occurred("missing EVP_MD for HASH context"); return NULL; } +#ifdef Py_HAS_OPENSSL3_SUPPORT + if (EVP_MD_nid(md) == NID_undef) { + /* Digests supplied by an OpenSSL 3 provider need not have a NID, + * in which case the ASN.1 name is just "undefined". Use the name + * of the fetched EVP_MD instead (gh-162712). */ + const char *md_name = EVP_MD_get0_name(md); + if (md_name == NULL) { + notify_ssl_error_occurred("missing name for EVP_MD"); + return NULL; + } + PyObject *name = PyUnicode_FromString(md_name); + if (name == NULL) { + return NULL; + } + PyObject *lower = PyObject_CallMethod(name, "lower", NULL); + Py_DECREF(name); + return lower; + } +#endif const char *name = get_hashlib_utf8name_by_evp_md(md); assert(name != NULL || PyErr_Occurred()); return name == NULL ? NULL : PyUnicode_FromString(name);