Repository navigation
gh-162712: Report the real name for provider digests in hashlib #162731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"): | ||
| 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
|
||
| self.assertEqual(h.name, h.name.lower()) | ||
| self.assertEqual(hashlib.new(h.name).digest_size, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
With OpenSSL's default properties set to Useful? React with 👍 / 👎. |
||
| h.digest_size) | ||
|
|
||
| def test_large_update(self): | ||
| aas = b'a' * 128 | ||
| bees = b'b' * 127 | ||
|
|
||
| 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`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use do that in this function, do 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
||
There was a problem hiding this comment.
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: