# Enable post-quantum TLS on a Python HTTPS server

Source: https://pq-scan.com/fix/python-post-quantum-tls
Fixes: pq-kex-missing, pq-kex-supported-not-preferred. Server: Python (ssl). Effort: 5 min.
Requires: Python linked to OpenSSL 3.5+ (set_groups needs Python 3.15). Lab-tested on Python 3.15.2, OpenSSL 3.5.9, 2026-12-05 (PQ Scan Docker lab).

Run Python on OpenSSL 3.5+ and keep the X25519MLKEM768 hybrid preferred, with classical fallback for old clients.

## The fix

The `ssl` module uses the OpenSSL it was built against. With OpenSSL 3.5 or newer, the default already prefers post-quantum. So on most setups the fix is: **run on OpenSSL 3.5+ and don't call `set_ecdh_curve`.**

If you need an explicit list, use `set_groups` (Python 3.15+). Put the hybrid group in its own first tuple. The `/` tells OpenSSL to prefer it, even when a client first sends only a classical key share.

```python title="server.py"
import ssl

ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.load_cert_chain("server.crt", "server.key")
ctx.set_groups("X25519MLKEM768 / X25519:P-256:P-384")  # Python 3.15+
```

Don't use `set_ecdh_curve` for this. It takes one curve name and replaces the whole list. It rejects `X25519MLKEM768` with `ValueError: unknown elliptic curve name`, and any classical name you give it switches post-quantum off.

## Check your OpenSSL first

```sh
python -c "import ssl; print(ssl.OPENSSL_VERSION)"
```

You need 3.5 or newer. Older OpenSSL does not know `X25519MLKEM768`.

| Official `python` image | Python | OpenSSL | Ready? |
|---|---|---|---|
| `python:3.10-slim` (Debian 13) | 3.10.22 | 3.5.7 | Yes |
| `python:3.11-slim`, `3.12-slim`, `3.13-slim`, `3.14-slim` (Debian 13) | 3.11.18 to 3.14.10 | 3.5.9 | Yes |
| `python:3.15-slim` (Debian 13) | 3.15.2 | 3.5.9 | Yes, and has `set_groups` |
| `python:3.12-alpine`, `3.14-alpine`, `3.15-alpine` (Alpine 3.24) | 3.12.16 to 3.15.2 | 3.5.9 | Yes |
| `python:3.12-slim-bookworm`, `3.13-slim-bookworm` (Debian 12) | 3.12.16, 3.13.18 | 3.0.22 | No: move to the default (Debian 13) tags |

Checked by running each image on 2026-12-06. Python 3.14.10 has no `set_groups`; 3.15.2 does. Outside Docker, the OS decides: Debian 12 and Ubuntu 24.04 ship OpenSSL 3.0 (see the [Nginx recipe](/fix/nginx-post-quantum-tls) for the OS table).

## What the common configs actually do

Tested in Docker against five client offers, including the exact one browsers send. "Preferred" means the server asks for post-quantum even when the client led with a classical key share.

| Config | Post-quantum | What browsers get |
|---|---|---|
| `set_groups("X25519MLKEM768 / X25519:…")` (this fix, 3.15) | Preferred | X25519MLKEM768 |
| No group call, OpenSSL 3.5 (Python 3.14 and 3.15) | Preferred | X25519MLKEM768 |
| `set_groups("X25519MLKEM768:X25519:…")` (no `/`) | Supported, not preferred | X25519MLKEM768 |
| `set_ecdh_curve("X25519")` (old advice) | Off | X25519 (classical) |
| No group call, Debian 12 image (OpenSSL 3.0) | Off | X25519 (classical) |

"Supported, not preferred" looks the same in a browser today, but a client that leads with a classical key share gets classical encryption. Preferring post-quantum closes that gap.

## Verify

```sh
openssl s_client -connect example.com:443 -groups X25519MLKEM768 </dev/null 2>&1 | grep "Negotiated TLS1.3 group"
# Negotiated TLS1.3 group: X25519MLKEM768
```

Or rescan with PQ Scan: the Confidentiality axis should read **PQ preferred**.

## Things that don't change
- Your certificate stays the same (post-quantum certificates are not available for public websites yet).
- Clients without post-quantum support still connect with X25519 or P-256.
- If a CDN, load balancer or reverse proxy terminates TLS in front of Python, visitors see its TLS, not this config. Fix it there too.

Verify after deploying: https://pq-scan.com/r/<your-domain>?fresh=1
