Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

hmac algorithm fallback is broken #103256

Closed
Closed
Assignees
gpsheadcorona10
Labels
3.11only security fixesextension-modulesC modules in the Modules dirtopic-SSLtype-bugAn unexpected behavior, bug, or error
@geitda

Description

@geitda

hmac won't fall back if OpenSSL is available, the requested algorithm isn't in OpenSSL, but the algorithm is inhashlib.
If you [monkey]patchhashlib to include a new algorithm, you can't use that algorithm fromhmac by name.
It appears that the OpenSSL implementation (known as_hashlib from insidehashlib, or_hashopenssl from insidehmac) doesn't actually return anUnsupportedDigestmodError, but rather it's base classValueError.

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>", line1, in<module>    hmac.digest(b'key',b'message','myhashalg')  File"C:\Python311\Lib\hmac.py", line198, indigestreturn _hashopenssl.hmac_digest(key, msg, digest)ValueError:unsupported hash type myhashalg

The exception goes unhandled at

except_hashopenssl.UnsupportedDigestmodError:
instead of falling through to lethashlib 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 eitherhmac 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

Metadata

Metadata

Labels

3.11only security fixesextension-modulesC modules in the Modules dirtopic-SSLtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp