axum-server-mtls
mTLS client certificate extraction for axum-server.
axum-server does not expose peer certificates after the TLS handshake
(issue #162).
This crate fills that gap by wrapping RustlsAcceptor with a custom Accept
implementation that extracts the client certificate chain and injects it into
every HTTP request as an extension.
Quick Start
use ;
use ;
use ;
async
async
How It Works
MtlsAcceptorwrapsRustlsAcceptorand implementsaxum_server::accept::Accept.- After the TLS handshake, it reads
ServerConnection::peer_certificates(). - It wraps the connection's service so that every request carries a
PeerCertificatesvalue in its extensions. - Handlers extract it via
Extension<PeerCertificates>.
Enabling Client Certificate Verification
For clients to present certificates, the Rustls ServerConfig must be built with
a client cert verifier. MtlsAcceptor only extracts certificates that Rustls
has already verified — it does not perform verification itself.
use WebPkiClientVerifier;
use RootCertStore;
use Arc;
// Load your client CA certificates
let mut roots = empty;
// roots.add(...) your client CA certs
let verifier = builder
.allow_unauthenticated // optional: allow clients without certs too
.build
.unwrap;
let config = builder
.with_client_cert_verifier
.with_single_cert
.unwrap;
Then pass this config to RustlsConfig::from_config(Arc::new(config)).
PeerCertificates API
| Method | Returns | Description |
|---|---|---|
is_present() |
bool |
Client presented at least one certificate |
is_empty() |
bool |
No client certificate presented |
chain() |
&[CertificateDer] |
Full DER-encoded cert chain, leaf first |
leaf() |
Option<&CertificateDer> |
The client's own certificate |
leaf_cn() |
Option<String> |
Common Name from the leaf cert's subject |
leaf_sans() |
Vec<String> |
Subject Alternative Names (DNS, email, IP) |
leaf_serial_hex() |
Option<String> |
Serial number as hex string |
leaf_not_after_unix() |
Option<i64> |
Expiry as UNIX timestamp |
Compatibility
| Dependency | Version |
|---|---|
| axum-server | 0.7.x |
| rustls | 0.23.x |
| tokio-rustls | 0.26.x |
| axum | 0.8.x (for Extension extractor) |
What This Crate Does NOT Do
- TLS verification — that's Rustls' job. Configure
WebPkiClientVerifieron yourServerConfig. - Identity mapping — mapping CN/SANs to users/roles is application logic.
- Certificate revocation — use Rustls' CRL/OCSP support in the verifier.
- Certificate management — generating, storing, or rotating certs is out of scope.
License
Licensed under either of Apache License, Version 2.0 or MIT License at your option.