Skip to content

SSLContext loads certificates from the "CA" Intermediate certificate store. #130304

Open

Description

@bnicht

Bug report

Bug description:

ssl.py

class SSLContext(_SSLContext):
    """An SSLContext holds various SSL-related configuration options and
    data, such as certificates and possibly a private key."""
    _windows_cert_stores = ("CA", "ROOT")

When a certificate is imported into the windows "Intermediate Certification Authorities" most applications do not consider this a trusted CA and will fail to verify. Examples are the chrome browser and .Net Applications.
This can be tested using - https://untrusted-root.badssl.com/ and downloading the public key and importing into the "Intermediate Certificate Authorities".

Cert = Windows CertMgr Name
Root = Trusted Root Certification Authorities
CA = Intermediate Certification Authorities

Given that other applications (chrome, .Net) seem to not treat "CA" certificates as a trusted root, should python load these by default?

Use Case:
Using requests Adapter to load the windows certificates rather than rely on Certifi.
https://requests.readthedocs.io/en/latest/user/advanced/#:~:text=10%27%2C%20%27rel%27%3A%20%27last%27%7D-,Transport%20Adapters%C2%B6,-As%20of%20v1.0.0

class WindowsSSLContextAdapter(HTTPAdapter):
    def __init__(self, url_prefix):
        self.url_prefix = url_prefix
        super().__init__()

    def init_poolmanager(self, *args, **kwargs):
        # loads CA and ROOT certificates on windows
        context = ssl.create_default_context()
        kwargs['ssl_context'] = context
        return super().init_poolmanager(*args, **kwargs)

#Mount the HTTPAdapter on requests session
session.mount(url_prefix, adapter)

CPython versions tested on:

3.11

Operating systems tested on:

Windows

Activity

  1. added
    type-bug
    on Feb 19, 2025
  2. added
    stdlib
    on Feb 21, 2025
  3. bobince commented on Nov 18, 2026

    @bobince
    Contributor

    This has been broken forever as far as I can see. The root cause is the mismatch between Windows and OpenSSL's expectations of how trust is signalled:

    • For Windows, anything (not explicitly distrusted) in the Trusted Root Certificate Authorities store is an acceptable trust anchor, and anything in Intermediate Certificate Authorities is only to be used to build a chain to something that is in Trusted Root Certificate Authorities.
    • For OpenSSL, certs added to the store from SSL_CTX_get_cert_store (via SSLContext.load_verify_locations) are trust anchors if they are self-signed, and intermediates otherwise.

    So as well as unexpectedly trusting self-signed certificates that have been put in the ICAs store, the default SSLContext as of Python 3.11 would unexpectedly _dis_trust a non-self-signed certificate in TCAs.

    (That's something I can conceive of someone having a use for, if, say, they wanted to directly trust Bar CA, but they only had a cross-certificate for them signed by Foo CA who they otherwise don't trust. Whereas I can't think of a good reason for an admin to go putting a self-signed cert in Intermediate Certificate Authorities. Not that ‘there being no good reason to do it’ is a valid reason for us to ignore the problem, either — certainly on corporate machines I have access to there are indeed a bunch of self-signed certificates present in Intermediate Certificate Authorities that presumably are a mistake because under Windows rules they should do nothing, but which the default SSLContext would happily trust.)

    Anyway, the problem has got significantly worse since #112389 enabled VERIFY_X509_PARTIAL_CHAIN in the default SSLContext. This allows OpenSSL to treat a non-self-signed cert as a trust anchor, so we will trust an actual intermediate whose signer root CA is unknown or specifically distrusted.

    (Again, it would be unusual for an admin to put an intermediate in ICAs without giving it anything to chain to in TRCAs; the only scenario I can think of would be a lower intermediate that's only present in the ICAs store chaining through a higher intermediate only present in the server's returned chain, to a root present in TRCAs. Which seems quite unlikely in practice. But being weird is not a good reason for us to fail open.)

    Typing this I started to doubt my understanding of how the Windows cert store is supposed to work so I've tested a bunch of Windows (11, 25H2) HTTPS clients I had to hand to see what they do with some custom locally-generated certificates:

                                                           Edge Ffox Curl WinH .NET P311 P314
    server cert signed by root ca
        root ca in TRCAs                                   ✅   ✅   ✅   ✅   ✅   ✅   ✅
        root ca in ICAs                                    ❎   ❎   ❎   ❎   ❎   ✔️   ✔️
        root ca in TRCAs but distrusted                    ❎   ❎   ❎   ❎   ❎   ❎   ❎
    server cert signed by intermediate ca
        intermediate in ICAs, root in TRCAs                ✅   ✅   ✅   ✅   ✅   ✅   ✅
        intermediate in ICAs, no root                      ❎   ❎   ❎   ❎   ✔️   ❎   ✔️
        intermediate in TRCAs, no root                     ✅   ✅   ❌   ❌   ✅   ❌   ✅
        intermediate in ICAs, root in TRCAs but distrusted ❎   ❎   ❎   ❎   ✔️   ❎   ✔️
    

    Where by ‘distrusted’ I mean ‘Disable all purposes for this certificate’ being selected on the Properties box for the cert, and the clients are:

    Edge: Edge 157.0.4341.46
    Ffox: Firefox 160.0
    Curl: the Windows built-in curl 8.22.0 that uses SChannel for TLS (with --ssl-no-revoke)
    WinH: WinHTTP, via the MSXML.ServerXMLHttp COM object
    .NET: .NET 11 HttpClient via powershell Invoke-WebRequest
    P311: Python 3.11 urllib with default SSLContext (or VERIFY_X509_PARTIAL_CHAIN explicitly off)
    P314: Python 3.14 urllib with default SSLContext (or VERIFY_X509_PARTIAL_CHAIN explicitly on)

    So, unless I've completely misunderstood something here:

    • Browsers behave ‘correctly’.
    • Python 3.11 indeed fails in the wrong-store scenarios above.
    • Python 3.14 indeed fails open more. This seems like a significantly bad security issue.
    • There are also seemingly somewhat-similar fail-open bugs in the .NET HttpClient (!) and some lesser fail-closed cases in wincurl and WinHTTP.

    For now locally we are working around this by using our own SSLContext which only loads certs from TRCAs ("ROOT", not "CA"). We don't care about the case where a server supplies an incomplete chain and the client fills in the missing intermediate from ICAs, since that seems like a bad misconfiguration that shouldn't happen on the public web. But I suspect this solution might not be acceptable for all Python users.

    Is there something else we can do? eg does it make sense to verify that every cert from ICAs chains to a valid cert in TRCAs before loading it (and then continuing to use VERIFY_X509_PARTIAL_CHAIN)? Or is there a way to get trusted and intermediate CAs into separate stores in the OpenSSL API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtopic-SSLtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions