Skip to content

CRAM-MD5 fails if Python has been built without HMAC-MD5 support #136134

Closed

Description

@ouldriw

Bug report

Bug description:

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

CPython versions tested on:

3.9

Operating systems tested on:

Linux

Linked PRs

Activity

  1. LamentXU123 commented on Jun 30, 2025

    @LamentXU123
    Contributor

    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)

  2. kannansasientrust commented on Jun 30, 2025

    @kannansasientrust

    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

    $ cat Dockerfile
    FROM docker.io/salrashid123/openssl:3.4.0.fips
    RUN apt-get update && apt-get install -y python3 python3-venv
    WORKDIR /apps
    COPY a.py /apps
    $
    

    Code

    $ cat a.py
    import sys
    from smtplib import SMTP
     
    args = sys.argv[1:]
     
    smtp = SMTP()
    smtp.connect(args[0], int(args[1]))
    smtp.login(args[2], args[3])
    

    Build and run

    $ docker build -t python-fips .
    $ docker run --entrypoint bash -it python-fips
    root@5c050cbf4f8c:/apps# python3 --version
    Python 3.11.2
    root@5c050cbf4f8c:/apps# python3 a.py mail.smtp2go.com 2525 user@nonexistent.com badpassword
    Traceback (most recent call last):
      File "/apps/a.py", line 9, in <module>
        smtp.login(args[2], args[3])
      File "/usr/lib/python3.11/smtplib.py", line 739, in login
        (code, resp) = self.auth(
                       ^^^^^^^^^^
      File "/usr/lib/python3.11/smtplib.py", line 652, in auth
        authobject(challenge).encode('ascii'), eol='')
        ^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.11/smtplib.py", line 670, in auth_cram_md5
        return self.user + " " + hmac.HMAC(
                                 ^^^^^^^^^^
      File "/usr/lib/python3.11/hmac.py", line 60, in __init__
        self._init_hmac(key, msg, digestmod)
      File "/usr/lib/python3.11/hmac.py", line 67, in _init_hmac
        self._hmac = _hashopenssl.hmac_new(key, msg, digestmod=digestmod)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ValueError: [digital envelope routines] unsupported
    root@5c050cbf4f8c:/apps#
    
  3. ZeroIntensity commented on Jun 30, 2025

    @ZeroIntensity
    Member
  4. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jun 30, 2025
  5. picnixz commented on Jul 1, 2025

    @picnixz
    Member

    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.

  6. self-assigned this
    on Jul 1, 2025
  7. added a commit that references this issue on Jul 1, 2025
  8. nogueira-raphael commented on Jul 1, 2025

    @nogueira-raphael

    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.

  9. added a commit that references this issue on Jul 1, 2025
  10. picnixz commented on Jul 11, 2025

    @picnixz
    Member

    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.

  11. nogueira-raphael commented on Jul 11, 2025

    @nogueira-raphael

    No worries @picnixz you can checkout my branch if you think something there can be used.

  12. picnixz commented on Jul 11, 2025

    @picnixz
    Member

    I have already written something locally so it won't be necessary. Sorry for the unnecessary work I've made you do.

  13. 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
  14. added a commit that references this issue on Aug 22, 2025
  15. 9 remaining items

  16. added 9 commits that reference this issue on Aug 23, 2025
  17. added 2 commits that reference this issue on Sep 4, 2025
  18. picnixz commented on Sep 6, 2025

    @picnixz
    Member

    Thanks for the report! now, imaplib will correctly abort while smtplib will try to use the next available auth method.

  19. added 2 commits that reference this issue on Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions