Source: https://h2corn.monicz.dev/guide/tls/

# Direct TLS

h2corn can terminate TLS itself instead of relying on a reverse proxy. This fits single-server deployments, sidecar-fronted services, or any environment where running an extra edge process isn't worthwhile.

Direct TLS is opt-in. Configure a certificate chain and private key:

```bash
h2corn hello:app \
  --bind 0.0.0.0:8443 \
  --certfile /etc/ssl/example/fullchain.pem \
  --keyfile /etc/ssl/example/privkey.pem
```

h2corn uses [Rustls](https://github.com/rustls/rustls) with **TLS 1.2 and TLS 1.3 only**. ALPN advertises `h2,http/1.1` by default, or only `h2` when [`--no-http1`](https://h2corn.monicz.dev/configuration/#option-http1) is set. OpenSSL cipher strings, legacy TLS versions, and encrypted private-key files are unsupported. Decrypt key files with operator tooling before starting the server.

Direct TLS exposes an HTTPS listener; for edge routing and abuse controls, use a [reverse proxy](https://h2corn.monicz.dev/guide/proxy/).

## Mutual TLS (client certificates)

Provide a CA bundle and choose whether client certificates are optional or required:

```bash
h2corn hello:app \
  --certfile /etc/ssl/example/fullchain.pem \
  --keyfile /etc/ssl/example/privkey.pem \
  --ca-certs /etc/ssl/example/client-ca.pem \
  --client-cert-mode required
```

| [`--client-cert-mode`](https://h2corn.monicz.dev/configuration/#option-client_cert_mode) | Behavior |
| --- | --- |
| `none` | No client certificate is requested. Default. |
| `optional` | Request a client certificate and verify it if presented. |
| `required` | Reject the handshake when no valid client certificate is presented. |

When [`--client-cert-mode`](https://h2corn.monicz.dev/configuration/#option-client_cert_mode) is anything other than `none`, [`--ca-certs`](https://h2corn.monicz.dev/configuration/#option-ca_certs) is required, and the listener must already have a server certificate and key configured.

## Reading the connection from an application

When a client certificate is presented, its verification decides whether the handshake succeeds. The [ASGI TLS extension](https://asgi.readthedocs.io/en/latest/specs/tls.html) exposes the verified identity and negotiated parameters under the [TLS extension mapping](https://h2corn.monicz.dev/api/scopes/#h2corn.types.HTTPExtensions.tls):

partial application fragment

```python
async def app(scope, receive, send):
    tls = scope['extensions'].get('tls')
    if tls is None:
        return  # not a TLS connection h2corn terminated
    chain = tls['client_cert_chain']       # PEM strings, leaf first
    name = tls['client_cert_name']         # RFC 4514 leaf DN, or None
    version = tls['tls_version']           # 0x0304 for TLS 1.3
    suite = tls['cipher_suite']            # e.g. 0x1302
```

Present only on connections h2corn terminated itself; absent behind a TLS-terminating proxy.

`client_cert_chain` is empty unless [`--client-cert-mode`](https://h2corn.monicz.dev/configuration/#option-client_cert_mode) asked for a certificate. When it is not empty, every certificate in it was verified against [`--ca-certs`](https://h2corn.monicz.dev/configuration/#option-ca_certs) during the handshake, so an application may trust the verified certificate identity. `client_cert_name` is the leaf subject as an RFC 4514 string. With optional authentication and no client certificate, the chain is empty and the name is `None`. `client_cert_error` is always `None`: a certificate that fails verification fails the handshake and never reaches an application.

The [known extension mappings](https://h2corn.monicz.dev/api/scopes/#extension-mapping-fields), including `scope["extensions"]["tls"]`, are shared per connection and read-only. The outer `scope["extensions"]` mapping is per-scope and may receive application namespaced keys. Copy a known mapping before mutating it.

## Rotation and restart

The supervisor reads the certificate chain, private key, and client CA bundle once at startup while still privileged, and every worker reuses that material (so the key may stay `root:root` mode `0600`). It does not watch PEM paths, and `SIGHUP` reloads application code only.

Rotate a direct-TLS certificate by writing complete files beside the old ones, setting ownership and permissions, atomically renaming them, running `h2corn` [`--check-config`](https://h2corn.monicz.dev/configuration/#command-check_config) with the same TLS options, and restarting the supervisor.

The preflight catches parse and permission errors; a full restart follows service-manager stop/start behavior and may have downtime. A rolling application reload preserves the old worker until its replacement is ready.

When a reverse proxy terminates TLS, rotate and validate the certificate there.

## Restrictions

Direct TLS is only supported on TCP listeners. A configuration that combines TLS with a `unix:` listener is rejected at startup.

Uncommon ciphers, client-cert revocation lists, and SNI multiplexing require a dedicated TLS proxy. Run `h2corn` on `h2c` upstream as described in [Behind a proxy](https://h2corn.monicz.dev/guide/proxy/#behind-a-reverse-proxy).
