You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CRAM-MD5 fails if Python has been built without HMAC-MD5 support #136134
When using smtplib to send emails in an environment where MD5 is not supported (e.g. FIPS), using SMTP.login to authenticate with an SMTP server will fail with:
File "/usr/lib64/python3.9/smtplib.py", line 739, in login
(code, resp) = self.auth(
File "/usr/lib64/python3.9/smtplib.py", line 652, in auth
authobject(challenge).encode('ascii'), eol='')
File "/usr/lib64/python3.9/smtplib.py", line 670, in auth_cram_md5
return self.user + " " + hmac.HMAC(
File "/usr/lib64/python3.9/hmac.py", line 60, in _init_
self._init_hmac(key, msg, digestmod)
File "/usr/lib64/python3.9/hmac.py", line 69, in _init_hmac
self._hmac = _hashopenssl.hmac_new(key, msg, digestmod=digestmod)
ValueError: [digital envelope routines] unsupported
The docs for SMTP.login say:
"Each of the authentication methods supported by smtplib are tried in turn if they are advertised as supported by the server."
But the problem here is auth_cram_md5 is always attempted first and the ValueError is not caught so smtplib never moves on to other authentication methods
Could you produce a python script to reproduce? TiA
Edited: Maybe you could try in on newer version of python. I failed to reproduce it on 3.14 windows (or maybe my bad here, so to make it clearer I look forward to a more precise discription, e.g. what is your script to connect with the SMTP server? Or what is the config of your SMTP server or something) Thank you for your report!
Edited 2: After staring at the code for a while, I think this could be a bug. Maybe adding a Error handling here would be better(?). (I think I still need a more detailed description, a reproducing POC will be great, TiA)
It looks like Python is 3.9 but it may still be present in main. In some sense, it can lead to an unavailable service but... I am travelling so I'll take a look in 10 days.
The fix ensures that if CRAM-MD5 fails due to an unsupported hash (as seen in FIPS environments), the login flow falls back to the next supported auth method instead of raising a ValueError.
I've actually checked stuff more precisely offline and observed that the problem is much more intricate to solve. We can't just raise an arbitrary exception and a similar issue occurs in imaplib. The problem with just raising an exception is that the server is sending a challenge and waiting for a reponse as well. So the current smtplib tests need to be updated.
Sorry but I'll work on this task myself as I actually needed to test it as well.
changed the title [-]SMTP.login() method fails with ValueError in environments that do not support MD5[/-][+]CRAM-MD5 fails if Python has been built without HMAC-MD5 support[/+]on Jul 13, 2025
Activity
LamentXU123 commentedon Jun 30, 2025
Could you produce a python script to reproduce? TiA
Edited: Maybe you could try in on newer version of python. I failed to reproduce it on 3.14 windows (or maybe my bad here, so to make it clearer I look forward to a more precise discription, e.g. what is your script to connect with the SMTP server? Or what is the config of your SMTP server or something) Thank you for your report!Edited 2: After staring at the code for a while, I think this could be a bug. Maybe adding a Error handling here would be better(?). (I think I still need a more detailed description, a reproducing POC will be great, TiA)
kannansasientrust commentedon Jun 30, 2025
You can reproduce this on any linux container with fips enabled and a mail server that uses MD5 auth method. Here is how I did it
Dockerfile
Code
Build and run
ZeroIntensity commentedon Jun 30, 2025
cc @picnixz
picnixz commentedon Jul 1, 2025
It looks like Python is 3.9 but it may still be present in main. In some sense, it can lead to an unavailable service but... I am travelling so I'll take a look in 10 days.
"pythongh-136134: Fallback to next auth method when CRAM-MD5 fails du…
nogueira-raphael commentedon Jul 1, 2025
I’ve submitted a PR that addresses this issue.
The fix ensures that if CRAM-MD5 fails due to an unsupported hash (as seen in FIPS environments), the login flow falls back to the next supported auth method instead of raising a
ValueError.#136188
I'm not sure if you have another better way to handle with this, but any questions I can change the strategy used to handle with the error.
pythongh-136134: Add code comment explaining CRAM-MD5 fallback and NE…
picnixz commentedon Jul 11, 2025
I've actually checked stuff more precisely offline and observed that the problem is much more intricate to solve. We can't just raise an arbitrary exception and a similar issue occurs in imaplib. The problem with just raising an exception is that the server is sending a challenge and waiting for a reponse as well. So the current smtplib tests need to be updated.
Sorry but I'll work on this task myself as I actually needed to test it as well.
nogueira-raphael commentedon Jul 11, 2025
No worries @picnixz you can checkout my branch if you think something there can be used.
picnixz commentedon Jul 11, 2025
I have already written something locally so it won't be necessary. Sorry for the unnecessary work I've made you do.
[-]SMTP.login() method fails with ValueError in environments that do not support MD5[/-][+]CRAM-MD5 fails if Python has been built without HMAC-MD5 support[/+]gh-136134: imaplib: fix CRAM-MD5 on FIPS-only environments (#136615)
9 remaining items
[3.13] pythongh-136134: smtplib: fix CRAM-MD5 on FIPS-only environmen…
[3.13] pythongh-136134: smtplib: fix CRAM-MD5 on FIPS-only environmen…
[3.14] pythongh-136134: smtplib: fix CRAM-MD5 on FIPS-only environmen…
gh-136134: restore truncated comment post GH-136623 (#138088)
[3.13] pythongh-136134: smtplib: fix CRAM-MD5 on FIPS-only environmen…
[3.14] pythongh-136134: smtplib: fix CRAM-MD5 on FIPS-only environmen…
[3.14] pythongh-136134: smtplib: fix CRAM-MD5 on FIPS-only environmen…
[3.13] gh-136134: imaplib: fix CRAM-MD5 on FIPS-only environments (GH…
[3.13] gh-136134: smtplib: fix CRAM-MD5 on FIPS-only environments (GH…
[3.14] gh-136134: smtplib: fix CRAM-MD5 on FIPS-only environments (GH…
[3.14] gh-136134: imaplib: fix CRAM-MD5 on FIPS-only environments (GH…
picnixz commentedon Sep 6, 2025
Thanks for the report! now, imaplib will correctly abort while smtplib will try to use the next available auth method.
[3.14] pythongh-136134: smtplib: fix CRAM-MD5 on FIPS-only environmen…
[3.14] pythongh-136134: imaplib: fix CRAM-MD5 on FIPS-only environmen…