spiffe-rustls
SPIFFE-based mutual TLS integration for rustls.
Builds rustls::ClientConfig and rustls::ServerConfig from spiffe's X509Source.
Handles certificate rotation, trust domain selection, and TLS-level peer authorization based on SPIFFE IDs.
All cryptography and TLS protocol handling are delegated to rustls.
Quick Start
1. Create an X509Source
The source is configured via the SPIFFE_ENDPOINT_SOCKET environment variable.
let source = new.await?;
2. Build a client configuration (mTLS)
use ;
let source = new.await?;
// Authorize only specific server SPIFFE IDs
let client_cfg = mtls_client
.authorize
.build?;
The resulting ClientConfig can be used directly with rustls, or integrated into
spiffe-rustls-tokio, tokio-rustls, tonic-rustls, or similar libraries.
Federation
When SPIFFE federation is configured, the Workload API delivers trust bundles for multiple
trust domains. spiffe-rustls automatically handles this during certificate verification:
- Extracts the peer’s SPIFFE ID from the certificate
- Derives the trust domain from that SPIFFE ID
- Selects the correct root certificate bundle from the bundle set
- Verifies the certificate chain using the selected bundle
No federation-specific configuration is required. Federation works automatically whenever the Workload API provides bundles for multiple trust domains.
Trust Domain Policy (verification)
You may optionally restrict which trust domains are allowed during certificate verification
using TrustDomainPolicy.
This is a defense-in-depth mechanism. The primary trust model comes from the bundle set delivered by the Workload API.
use TrustDomainPolicy;
// Default: trust all domains present in the Workload API bundle set
let policy = AnyInBundleSet;
// Restrict verification to a fixed set of trust domains
let policy = AllowList;
// Restrict verification to exactly one trust domain
let policy = LocalOnly;
Note: Trust domain policy affects verification only. Authorization is handled separately via
Authorizer.
Authorization
Authorization is applied after cryptographic verification succeeds.
The crate provides a strongly-typed Authorizer trait and constructors for
common authorization strategies.
Common authorization patterns
use authorizer;
// 1) Authentication only (allow any SPIFFE ID)
let auth = any;
// 2) Allow only specific SPIFFE IDs
let auth = exact?;
// 3) Allow any SPIFFE ID from specific trust domains
let auth = trust_domains?;
Custom authorization logic
use SpiffeId;
// Custom rule using a closure
let auth = ;
Closures automatically implement Authorizer and require no allocation.
Client Configuration
ClientConfigBuilder
Builds a rustls::ClientConfig that:
- presents the current SPIFFE X.509 SVID
- validates the server certificate chain using Workload API bundles
- automatically selects the correct trust domain
- authorizes the server by SPIFFE ID (URI SAN)
use ;
let source = new.await?;
let client_cfg = mtls_client
.authorize
.trust_domain_policy
.with_alpn_protocols // Optional: for gRPC/HTTP/2
.build?;
Server Configuration
ServerConfigBuilder
Builds a rustls::ServerConfig that:
- presents the current SPIFFE X.509 SVID
- requires and validates client certificates (mTLS)
- automatically selects the correct trust domain
- authorizes the client by SPIFFE ID (URI SAN)
use ;
let source = new.await?;
let server_cfg = mtls_server
.authorize
.trust_domain_policy
.with_alpn_protocols // Optional: for gRPC/HTTP/2
.build?;
Advanced Configuration
ALPN (Application-Layer Protocol Negotiation)
Configure ALPN protocols for protocol negotiation during the TLS handshake. Protocols are advertised in order of preference (most preferred first):
use mtls_client;
let source = new.await?;
// HTTP/2 preferred, HTTP/1.1 fallback
let config = mtls_client
.with_alpn_protocols
.build?;
// Also accepts owned vectors
let protocols = vec!;
let config = mtls_client
.with_alpn_protocols
.build?;
Common protocols:
b"h2"— HTTP/2 (required for gRPC)b"http/1.1"— HTTP/1.1
Config Customization (Advanced)
For configuration not directly exposed by the builder, use with_config_customizer.
The customizer runs last, after all other builder settings (including ALPN)
have been applied, allowing you to override any configuration:
use mtls_server;
let source = new.await?;
let config = mtls_server
.with_config_customizer
.build?;
Warning: Do not modify or replace the verifier or certificate resolver, as they are required for SPIFFE authentication and authorization. Safe to modify: ALPN, cipher suites, protocol versions, and other non-security-critical settings.
Features
Most features are additive and opt-in.
Crypto provider features are mutually exclusive — exactly one must be enabled.
Crypto providers
[]
= ["ring"]
= ["rustls/ring"]
= ["rustls/aws_lc_rs"]
- Default:
ring - Optional:
aws-lc-rs
Example (AWS-LC):
Provider choice affects only cryptographic primitives. SPIFFE semantics and API behavior are identical across providers.
Optional performance features
parking-lot
Enable parking-lot to use a faster internal synchronization strategy.
This can improve throughput under high concurrency and reduce contention
during cold-start bursts.
Note: This feature does not change the public API or SPIFFE semantics. It only affects internal synchronization.
Examples
Prerequisites
All examples require:
- a running SPIRE agent
- a valid Workload API socket (
SPIFFE_ENDPOINT_SOCKET) - local DNS resolution for
example.org
For local testing, add to /etc/hosts:
127.0.0.1 example.org
Raw TLS (tokio-rustls)
Tokio Integration (spiffe-rustls-tokio)
For Tokio integration with automatic peer identity extraction, use the
spiffe-rustls-tokio crate:
See the spiffe-rustls-tokio README for details.
gRPC (tonic-rustls)
gRPC examples live in a separate crate (spiffe-rustls-grpc-examples) to avoid pulling
gRPC/protobuf dependencies into the library.
Performance
Performance characteristics:
- Zero-copy certificate access — SVIDs and bundles accessed via
Arcreferences - Atomic updates — New handshakes automatically use rotated material without locks
- Zero-allocation authorization —
Authorizertrait allows zero-allocation checks - Minimal overhead — Authorization runs after TLS verification (no impact on handshake)
- Cached verifier reuse — Verifiers are cached per trust domain and bundle generation, avoiding repeated construction under concurrent handshakes.
Architecture
spiffe-rustls builds rustls::ClientConfig and rustls::ServerConfig from spiffe::X509Source.
Authorization is applied after TLS verification succeeds, ensuring cryptographic security before policy checks.
Security Considerations
- Certificates must contain exactly one SPIFFE ID URI SAN
- Trust bundles are sourced exclusively from the Workload API
- Trust domain selection is automatic and deterministic
- Authorization runs after cryptographic verification
- Material updates are atomic; new handshakes use fresh material
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.