A high-performance HTTPS client with mutual TLS authentication using Rust and Reqwest.
- ✅ Mutual TLS authentication with Rustls
- ✅ Server certificate verification
- ✅ Multiple endpoint tests
- ✅ Async/await with Tokio runtime
- ✅ Type-safe JSON handling with Serde
- ✅ Production-ready performance
- Rust 1.70+ (install from https://rustup.rs/)
- Certificates generated by the mtls CLI tool
- Running mTLS server (Rust, Go, Python, Node.js, PHP, or Caddy)
# Build the project
cargo build --release
# Or just run (will build automatically)
cargo run --release# Development mode
cargo run
# Release mode (optimized)
cargo run --release
# Or run the binary directly
./target/release/mtls-rust-clientThe client tests 4 endpoints:
- GET / - Main endpoint, displays client certificate info
- GET /health - Health check endpoint
- GET /api/data - Fetches sample data with metadata
- POST /api/echo - Sends JSON and receives echo response
- tokio: Async runtime
- reqwest: HTTP client with TLS support
- rustls: Modern TLS library
- serde: JSON serialization/deserialization
- chrono: Date/time handling
// Load client certificate and key
let cert_pem = fs::read("server-cert.pem")?;
let key_pem = fs::read("server-key.pem")?;
// Combine for Identity
let mut pem = cert_pem;
pem.extend_from_slice(&key_pem);
let identity = Identity::from_pem(&pem)?;
// Load CA certificate
let ca_cert = fs::read("ca-cert.pem")?;
let ca = Certificate::from_pem(&ca_cert)?;
// Build client
let client = reqwest::Client::builder()
.identity(identity)
.add_root_certificate(ca)
.build()?;You can modify the server URL in src/main.rs:
const SERVER_URL: &str = "https://localhost:8443";🔒 mTLS Rust Client
===================
📡 Test 1: Main endpoint (GET /)
✅ Status: 200 OK
Message: mTLS Rust Server
Client Certificate: localhost
Verified: true
Server Time: 2024-01-15T10:30:00Z
📡 Test 2: Health check (GET /health)
✅ Status: 200 OK
Response: OK
📡 Test 3: API data (GET /api/data)
✅ Status: 200 OK
Data:
{
"timestamp": "2024-01-15T10:30:00Z",
"client": { ... }
}
📡 Test 4: Echo test (POST /api/echo)
✅ Status: 200 OK
Response: { ... }
✅ All tests completed successfully!
The client handles errors gracefully:
- Connection failures
- Certificate verification errors
- Invalid JSON responses
- HTTP errors
Reqwest with Rustls provides:
- Connection pooling
- HTTP/2 support
- Async I/O
- Memory safety
- Zero-copy optimizations
Connection refused:
- Make sure the server is running
- Check the port number and hostname
Certificate verification failed:
- Ensure all certificates are signed by the same CA
- Check certificate paths are correct
- Verify certificates are not expired
Identity parsing error:
- Client certificate and key must be in PEM format
- Key should not be encrypted (no passphrase)
- Certificate and key must match
Rustls error:
- Ensure Rustls features are enabled in Cargo.toml
- Check certificate format compatibility
- Verify TLS version compatibility
If you need OpenSSL instead of Rustls, modify Cargo.toml:
reqwest = { version = "0.11", features = ["native-tls", "json"] }Note: Rustls is recommended for better security and portability.