Skip to content

Make SSL_get_ciphers() results const in Modules/_ssl.c #162141

Open

Description

@chlily1

Bug report

Modules/_ssl.c stores results from SSL_get_ciphers() in non-const STACK_OF(SSL_CIPHER) * variables in two places:

  • server_ciphers in _ssl__SSLSocket_shared_ciphers_impl (*)
  • sk in _ssl__SSLContext_get_ciphers_impl

Both variables should be const: they are not modified, and are only read via sk_SSL_CIPHER_num() / sk_SSL_CIPHER_value(), which already take a const stack in OpenSSL and AWS-LC, so declaring them const is a no-op for existing builds.

(*) The client_ciphers variable immediately below this has to stay non-const for now, because OpenSSL's sk_SSL_CIPHER_find() still takes a non-const stack (openssl/openssl#33707).

Motivation: Though BoringSSL is not supported per PEP 644, this const-correctness change would be aligned with BoringSSL's planned changes to SSL_get_ciphers() and SSL_CTX_get_ciphers() to return const STACK_OF(SSL_CIPHER) * (see https://crbug.com/550501994).

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    type-refactorCode refactoring (with no changes in behavior)
    on Nov 17, 2026
  2. picnixz commented on Nov 17, 2026

    @picnixz
    Member

    I am not fond of doing this change just for BoringSSL. If we want to support it, we would anyway need more changes in the future I think and we should rather do it them at once at this time. If you are a maintainer of BoringSSL I would however be happy if you could tell us what needs to be done for it to be entirely compatible (not just this specific change).

    In addition, is the constness ok for every OpenSSL versions since 1.1.1? (I don't remember when this API was introduced) What about LibreSSL as well which is more or less in the same boat as BoringSSL?

  3. chlily1 commented on Nov 19, 2026

    @chlily1
    Author

    To clarify the intent: I'm a BoringSSL maintainer, and we are definitely not asking CPython to support BoringSSL or take piecemeal BoringSSL compatibility patches (per PEP 644). As @davidben noted, our policy on the BoringSSL side is to carry BoringSSL-specific patches downstream (or add compat APIs in BoringSSL) rather than burdening upstreams, and only send a change upstream when we think it stands on its own as a clean improvement for OpenSSL builds with zero #ifdefs or maintenance cost.

    Why I think this stands on its own for OpenSSL:

    • SSL_get_ciphers() returns a borrowed pointer to OpenSSL's internal cipher stack (unlike SSL_get1_supported_ciphers(), which returns a caller-owned copy). OpenSSL's SSL_get_ciphers(3) docs note that callers must not free it, and mutating it would corrupt internal SSL state.
    • Marking these two variables const lets the compiler enforce the borrowed/read-only contract (catching an accidental free or mutation at compile time), matching how _ssl.c already const-qualifies other borrowed OpenSSL pointers (const SSL_CIPHER *, const X509_NAME *, const X509_OBJECT *, etc.).
    • OpenSSL can't easily add const to SSL_get_ciphers()'s return type in C without breaking existing callers that assign to non-const locals. In OpenSSL 1.1.0+, read-only stack accessors (sk_SSL_CIPHER_num, sk_SSL_CIPHER_value) accept const STACK_OF(SSL_CIPHER) *, while sk_SSL_CIPHER_free() and mutators require non-const STACK_OF(SSL_CIPHER) *.

    On version/library compatibility:

    • OpenSSL >= 1.1.1: sk_SSL_CIPHER_num and sk_SSL_CIPHER_value take const STACK_OF(SSL_CIPHER) * going back to OpenSSL 1.1.0. The PR CI checks also passed across OpenSSL versions including 1.1.1w.
    • LibreSSL: LibreSSL's sk_num/sk_value take const _STACK *, and CHECKED_STACK_OF accepts const STACK_OF(SSL_CIPHER) * cleanly with no warnings under GCC and Clang (-Wall -Wextra -Wpedantic).

    Since you asked what full BoringSSL compatibility requires: #116399 is a draft PR from 2024 showing the rough delta, and downstream at Google we carry a few small patches for areas where BoringSSL intentionally diverges. Those involve real API/feature differences that belong downstream rather than in CPython, whereas here const is just standard C const-correctness that works everywhere.

  4. davidben commented on Nov 20, 2026

    @davidben
    Contributor

    FWIW, I was talking with @bob-beck and OpenSSL may be interested in making the API const too. Returning non-const pointers to internal structures is a bit horrifying.

  5. picnixz commented on Nov 20, 2026

    @picnixz
    Member

    Ok thanks for sharing the backstory! The change is not really large so ok for it. Addressing the delta can be left to the future.

  6. davidben commented on Nov 20, 2026

    @davidben
    Contributor

    Yeah, we're definitely not looking to upstream all our patches or anything. Some of them are places where we think the right fix is to make BoringSSL more OpenSSL compatible but haven't yet because other problems were more pressing. Others would need some more OpenSSL/BoringSSL coordination to solve. Others we just haven't thought about very hard yet. TBH it's been a while since I'd looked at the patches. :-)

    This one seemed prudent since const-correctness is generally valuable and both OpenSSL and BoringSSL have been slowly inching towards const-correcting the APIs. (OpenSSL recently const-corrected a bunch of X.509 stuff. Actually @bob-beck is a bit ahead of us on that particular project and I'm quite jealous. 😆)

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions