# Post-quantum TLS in Python: ssl, OpenSSL 3.5 and SSLContext.set_groups

> Python negotiates hybrid X25519MLKEM768 when its ssl module is linked to OpenSSL 3.5+. Python 3.15 adds SSLContext.set_groups. Check, configure and verify it.

Source: https://pqshift.dev/learn/post-quantum-tls-python · Published 2026-12-04 · Updated 2026-12-04 · pqshift

Python's `ssl` module offers hybrid post-quantum TLS (`X25519MLKEM768`) whenever it is linked against OpenSSL 3.5 or later and your code doesn't restrict the key-exchange groups. That covers `requests`, `httpx`, `urllib3`, `aiohttp` and anything else built on `ssl`. Python 3.15 adds `SSLContext.set_groups()` and `get_groups()` for explicit control.

## Which OpenSSL is Python linked to?

```bash
python -c "import ssl; print(ssl.OPENSSL_VERSION)"   # needs OpenSSL 3.5.0 or later
grep -rn "set_ecdh_curve\|set_groups" --include=*.py . | grep -v site-packages
```

What matters is the OpenSSL **your Python links to**, not the one on your PATH. The `openssl version` command and `ssl.OPENSSL_VERSION` can differ. Official python.org installers bundle their own OpenSSL; Linux distribution Pythons and most `python:` Docker images link the system library.

## On Python 3.15+: set_groups

```python
import ssl

ctx = ssl.create_default_context()
ctx.minimum_version = ssl.TLSVersion.TLSv1_2
# Hybrid first, classical kept as a fallback (OpenSSL group-list format)
ctx.set_groups("X25519MLKEM768:X25519:secp256r1:secp384r1")

print(ctx.get_groups())   # preferred IANA names your OpenSSL will offer
```

`set_groups()` takes a string in OpenSSL's group-list format. Keep the classical groups: peers without ML-KEM support, and TLS 1.2 connections, then still negotiate X25519 or P-256.

## On older Pythons: remove set_ecdh_curve

Before 3.15 there is no way to pass a group list from Python, but you rarely need one: OpenSSL 3.5's default list already puts `X25519MLKEM768` first. The thing to remove is `set_ecdh_curve()`, which takes a **single** classical curve and pins key exchange to it:

```python
# Before: pins key exchange to one classical curve, so no hybrid group
ctx.set_ecdh_curve("prime256v1")

# After: delete the call and let OpenSSL 3.5 defaults apply
```

If you can't upgrade the linked OpenSSL (an old base image, say), the fix is the image, not the code: move to a base image whose OpenSSL is 3.5+ and rebuild.

## Requests, httpx and urllib3

These libraries build an `SSLContext` for you, so they inherit OpenSSL's defaults. To pin groups on Python 3.15+, pass your own context:

```python
import ssl, httpx

ctx = ssl.create_default_context()
ctx.set_groups("X25519MLKEM768:X25519:secp256r1")
client = httpx.Client(verify=ctx)
```

For `requests`, mount an `HTTPAdapter` subclass whose `init_poolmanager` passes `ssl_context=ctx`.

## Servers: gunicorn, uvicorn and friends

Python app servers that terminate TLS themselves use the same `ssl` module, so the same rule applies: OpenSSL 3.5+ and no `set_ecdh_curve`. In most production setups TLS terminates at nginx, a load balancer or a CDN in front of Python; check that layer with the [nginx and OpenSSL server guide](https://pqshift.dev/learn/post-quantum-tls-nginx).

## Verify the handshake

```bash
openssl s_client -connect your-service.example:443 -groups X25519MLKEM768 </dev/null 2>/dev/null | grep -i group
```

The negotiated group should be `X25519MLKEM768`. More methods, including a CI job, are in [How to check if a server uses post-quantum TLS](https://pqshift.dev/learn/test-post-quantum-tls).

## Application-level RSA and ECDH

Code that wraps keys with RSA-OAEP or derives them with ECDH (`cryptography`'s `rsa`/`ec`/`x25519` modules, PyCryptodome, `node-forge` ports) is exposed to harvest-now-decrypt-later just like TLS, and needs a code change to a hybrid KEM with a versioned ciphertext format so existing data still decrypts. [pqshift](https://pqshift.dev/) finds these call sites and opens a reviewable PR for them.

## FAQ

### Does Python support post-quantum TLS?

Yes, when the ssl module is linked against OpenSSL 3.5 or later, which offers X25519MLKEM768 by default. Check ssl.OPENSSL_VERSION. Python 3.15 adds SSLContext.set_groups() to choose groups explicitly.

### Why does set_ecdh_curve break post-quantum key exchange?

set_ecdh_curve() takes a single classical curve name and restricts key exchange to it, so the hybrid group is no longer offered. Remove the call, or on Python 3.15+ use set_groups('X25519MLKEM768:X25519:secp256r1').

## Sources

- [Python 3.15 ssl module documentation (SSLContext.set_groups, get_groups)](https://docs.python.org/3.15/library/ssl.html)
- [OpenSSL 3.5 release announcement](https://openssl-library.org/post/2025-03-25-openssl-3.5-beta/)
