Skip to content

Commit b2e24c5

Browse files
committed
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 <egearhany@gmail.com>
1 parent eb03dbd commit b2e24c5

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎Lib/test/test_hashlib.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,20 @@ def test_name_attribute(self):
484484
hashlib.new(h.name, usedforsecurity=False).name
485485
)
486486

487+
def test_name_attribute_provider_digest(self):
488+
# gh-162712: digests from an OpenSSL 3 provider can have NID_undef,
489+
# but the exposed name must still work with hashlib.new().
490+
for requested in ("SHA256-192", "KECCAK-256"):
491+
try:
492+
h = hashlib.new(requested, usedforsecurity=False)
493+
except ValueError:
494+
continue
495+
with self.subTest(requested=requested):
496+
self.assertNotEqual(h.name, "undefined")
497+
self.assertEqual(h.name, h.name.lower())
498+
self.assertEqual(hashlib.new(h.name).digest_size,
499+
h.digest_size)
500+
487501
def test_large_update(self):
488502
aas = b'a' * 128
489503
bees = b'b' * 127
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix :attr:`hashlib.hash.name` for digests provided by OpenSSL 3 providers.
2+
These digests may not have a numeric identifier, in which case the name
3+
used to be reported as ``"undefined"`` instead of the actual algorithm
4+
name, so it could not be passed back to :func:`hashlib.new`.

‎Modules/_hashopenssl.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,25 @@ _hashlib_HASH_get_name(PyObject *op, void *Py_UNUSED(closure))
10411041
notify_ssl_error_occurred("missing EVP_MD for HASH context");
10421042
return NULL;
10431043
}
1044+
#ifdef Py_HAS_OPENSSL3_SUPPORT
1045+
if (EVP_MD_nid(md) == NID_undef) {
1046+
/* Digests supplied by an OpenSSL 3 provider need not have a NID,
1047+
* in which case the ASN.1 name is just "undefined". Use the name
1048+
* of the fetched EVP_MD instead (gh-162712). */
1049+
const char *md_name = EVP_MD_get0_name(md);
1050+
if (md_name == NULL) {
1051+
notify_ssl_error_occurred("missing name for EVP_MD");
1052+
return NULL;
1053+
}
1054+
PyObject *name = PyUnicode_FromString(md_name);
1055+
if (name == NULL) {
1056+
return NULL;
1057+
}
1058+
PyObject *lower = PyObject_CallMethod(name, "lower", NULL);
1059+
Py_DECREF(name);
1060+
return lower;
1061+
}
1062+
#endif
10441063
const char *name = get_hashlib_utf8name_by_evp_md(md);
10451064
assert(name != NULL || PyErr_Occurred());
10461065
return name == NULL ? NULL : PyUnicode_FromString(name);

0 commit comments

Comments
 (0)