SSL & TLS Configuration
Relevant source files
The following files were used as context for generating this wiki page:
Uvicorn provides robust support for serving applications over HTTPS and WSS by integrating with the Python standard library ssl module. The system allows for detailed configuration of certificate chains, private keys (including encrypted keys with passwords), client certificate verification, and cipher suite selection.
The configuration is primarily handled within the Config class, which manages the lifecycle of the SSLContext object used by the underlying asyncio transports. This ensures that secure communication is established at the protocol level before ASGI application logic begins.
Sources: uvicorn/config.py:102 uvicorn/config.py:387-418 uvicorn/main.py:321-348
SSL Context Creation
The core of Uvicorn's SSL functionality resides in the create_ssl_context utility function and the Config.load() method. Uvicorn supports two main ways to initialize SSL: direct parameter configuration and a custom factory pattern.
The Creation Logic
The create_ssl_context function takes raw filesystem paths and settings to generate a standard ssl.SSLContext. It specifically targets ssl.PROTOCOL_TLS_SERVER as the default protocol version.
The diagram above illustrates the decision logic within Uvicorn's configuration loader to determine how the SSL context is instantiated.
Sources: uvicorn/config.py:102-118, uvicorn/config.py:387-418
Custom SSL Context Factories
As of version 0.47.0, Uvicorn supports ssl_context_factory. This allows developers to programmatically define the SSLContext. The factory receives the Config instance and a default_factory callable, enabling either a completely custom setup or a mutation of the default context (e.g., restricting TLS versions).
Sources: CHANGELOG.md:14, tests/test_ssl.py:90-105
Configuration Options
Uvicorn exposes several parameters through both its programmatic API and Command Line Interface (CLI) to control TLS behavior.
| Parameter | Type | Default | Description |
|---|---|---|---|
ssl_keyfile | str | PathLike | None | Path to the SSL private key file. |
ssl_certfile | str | PathLike | None | Path to the SSL certificate file. |
ssl_keyfile_password | str | None | Password for decrypting an encrypted private key. |
ssl_version | int | PROTOCOL_TLS_SERVER | SSL/TLS protocol version to use. |
ssl_cert_reqs | int | ssl.CERT_NONE | Whether client certificates are required (ssl.CERT_REQUIRED). |
ssl_ca_certs | str | PathLike | None | Path to CA certificates for client authentication. |
ssl_ciphers | str | None | Allowed cipher suites (OpenSSL format). |
Sources: uvicorn/config.py:228-234, uvicorn/main.py:321-348, uvicorn/config.py:90
Protocol Integration
Once the Config is loaded and the SSLContext is created, it is applied to the network transport. The system identifies if a connection is secure by inspecting the transport's sslcontext or checking if the transport is an instance of asyncio.sslproto.SSLProtocol.
Identifying SSL in Protocols
Protocol implementations (HTTP/1.1 and WebSockets) use utility functions to determine the current scheme (https or wss).
# From uvicorn/protocols/utils.py (referenced in implementation)
def is_ssl(transport: asyncio.Transport) -> bool:
return bool(transport.get_extra_info("sslcontext"))
In the connection_made event of both HttpToolsProtocol and H11Protocol, the scheme is set based on this check:
# uvicorn/protocols/http/httptools_impl.py:104
self.scheme = "https" if is_ssl(transport) else "http"
Sources: uvicorn/protocols/http/httptools_impl.py:104, uvicorn/protocols/http/h11_impl.py:82, uvicorn/protocols/websockets/websockets_sansio_impl.py:92
Sequence of Secure Connection Establishment
When Uvicorn runs with SSL configured, the socket binding process reflects the security state in the server logs.
The sequence shows how the configuration determines the displayed protocol during startup.
Sources: uvicorn/config.py:532-534, uvicorn/config.py:255
Advanced Usage: Client Authentication
By configuring ssl_cert_reqs to ssl.CERT_REQUIRED and providing ssl_ca_certs, Uvicorn enables Mutual TLS (mTLS). In this mode, the server validates the certificate presented by the client against the provided CA bundle.
Sources: uvicorn/config.py:112-114, uvicorn/main.py:341-344
Summary
SSL & TLS configuration in Uvicorn is centered around the flexible Config class, which abstracts the complexity of ssl.SSLContext creation. By providing dedicated support for certificate chains, encrypted keys, and custom context factories, Uvicorn allows for high-security deployments while maintaining a simple interface for standard HTTPS use cases. Significance of these settings is reflected in the protocol implementations where the URI scheme is automatically adjusted between secure and insecure variants based on the presence of an active SSL context on the transport.