Skip to content

hmac algorithm fallback is broken #103256

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

Closed
geitda opened this issue Apr 4, 2023 · 0 comments
Closed

hmac algorithm fallback is broken #103256

geitda opened this issue Apr 4, 2023 · 0 comments
Assignees
Labels
3.11 only security fixes extension-modules C modules in the Modules dir topic-SSL type-bug An unexpected behavior, bug, or error

Comments

@geitda
Copy link

geitda commented Apr 4, 2023

hmac won't fall back if OpenSSL is available, the requested algorithm isn't in OpenSSL, but the algorithm is in hashlib.
If you [monkey]patch hashlib to include a new algorithm, you can't use that algorithm from hmac by name.
It appears that the OpenSSL implementation (known as _hashlib from inside hashlib, or _hashopenssl from inside hmac) doesn't actually return an UnsupportedDigestmodError, but rather it's base class ValueError.

MRE

# The following is MRE-specific to easily introduce a new name
# My use case involves a monkeypatch, but imagine any algorithm NOT implemented by OpenSSL, ONLY by hashlib
>>> hashlib.__builtin_constructor_cache['myhashalg'] = hashlib.md5
>>> hashlib.new('myhashalg', b'').digest().hex() # confirm hashlib can use that name
'd41d8cd98f00b204e9800998ecf8427e'
>>> hmac.digest(b'key', b'message', 'myhashalg')
Traceback (most recent call last):
  File "<pyshell#nnn>", line 1, in <module>
    hmac.digest(b'key', b'message', 'myhashalg')
  File "C:\Python311\Lib\hmac.py", line 198, in digest
    return _hashopenssl.hmac_digest(key, msg, digest)
ValueError: unsupported hash type myhashalg

The exception goes unhandled at

except _hashopenssl.UnsupportedDigestmodError:
instead of falling through to let hashlib handle it.

This also shows up in the stateful (non-oneshot) code at

except _hashopenssl.UnsupportedDigestmodError:

Passing a callable works as intended with my monkeypatch, so I have a workaround. However, I'd argue that either hmac is trying to catch the wrong thing, or OpenSSL is throwing the wrong thing, so some sort of fix is called for.

Environment

Windows 10 64-bit
Python 3.11.2

Possible fixes

Linked PRs

@geitda geitda added the type-bug An unexpected behavior, bug, or error label Apr 4, 2023
@arhadthedev arhadthedev added extension-modules C modules in the Modules dir topic-SSL labels Apr 5, 2023
@gpshead gpshead self-assigned this Apr 7, 2023
@gpshead gpshead added the 3.11 only security fixes label Apr 7, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Apr 7, 2023
pythongh-103286)

(cherry picked from commit efb0a2c)

Co-authored-by: Dong-hee Na <[email protected]>
Co-authored-by: Gregory P. Smith <[email protected]>
miss-islington added a commit that referenced this issue Apr 7, 2023
…103286)

(cherry picked from commit efb0a2c)

Co-authored-by: Dong-hee Na <[email protected]>
Co-authored-by: Gregory P. Smith <[email protected]>
@corona10 corona10 closed this as completed Apr 7, 2023
warsaw pushed a commit to warsaw/cpython that referenced this issue Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes extension-modules C modules in the Modules dir topic-SSL type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants