Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,20 @@
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"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need t check for both keccak and sha256/192. In addition, it's not a nice test IMO. It should be more robust with an explicit skip when the hid doesn't even exist. Something like:

try:
    h = hashlib.new(name, usedforsecurity=False)
except ValueError:
    self.skip(...)

# test the rest once you know with an explicit RHS, not with just h.name being compared to itself

try:
h = hashlib.new(requested, usedforsecurity=False)
except ValueError:
continue
with self.subTest(requested=requested):
self.assertNotEqual(h.name, "undefined")

Check failure on line 496 in Lib/test/test_hashlib.py

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests (ubuntu-26.04, aws-lc, 5.10.0)

test_name_attribute_provider_digest (test.test_hashlib.HashLibTestCase.test_name_attribute_provider_digest) (requested='KECCAK-256')

AssertionError: 'undefined' == 'undefined'
self.assertEqual(h.name, h.name.lower())
self.assertEqual(hashlib.new(h.name).digest_size,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve usedforsecurity=False when recreating the provider digest

With OpenSSL's default properties set to fips=yes and a non-FIPS provider digest available, the initial constructor succeeds because it passes usedforsecurity=False, but this round-trip constructor defaults to True and raises ValueError. The new test therefore fails in a supported restricted configuration even when the name fix works correctly. Pass usedforsecurity=False here as well, as the existing test_name_attribute already does.

Useful? React with 👍 / 👎.

h.digest_size)

def test_large_update(self):
aas = b'a' * 128
bees = b'b' * 127
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
19 changes: 19 additions & 0 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use do that in this function, do it get_hashlib_utf8name_by_evp_md or in an other function that is similar to it.

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);
Comment on lines +1058 to +1059

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why normalizing the name? it's an inefficient way of doing it IMO.

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);
Expand Down
Loading