Skip to content

Latest commit

 

History

History

rust-client

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Rust mTLS Client Example

A high-performance HTTPS client with mutual TLS authentication using Rust and Reqwest.

Features

  • ✅ 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

Prerequisites

  • 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)

Setup

# Build the project
cargo build --release

# Or just run (will build automatically)
cargo run --release

Running the Client

# Development mode
cargo run

# Release mode (optimized)
cargo run --release

# Or run the binary directly
./target/release/mtls-rust-client

What It Tests

The client tests 4 endpoints:

  1. GET / - Main endpoint, displays client certificate info
  2. GET /health - Health check endpoint
  3. GET /api/data - Fetches sample data with metadata
  4. POST /api/echo - Sends JSON and receives echo response

Dependencies

  • tokio: Async runtime
  • reqwest: HTTP client with TLS support
  • rustls: Modern TLS library
  • serde: JSON serialization/deserialization
  • chrono: Date/time handling

Certificate Loading

// 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()?;

Configuration

You can modify the server URL in src/main.rs:

const SERVER_URL: &str = "https://localhost:8443";

Example Output

🔒 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!

Error Handling

The client handles errors gracefully:

  • Connection failures
  • Certificate verification errors
  • Invalid JSON responses
  • HTTP errors

Performance

Reqwest with Rustls provides:

  • Connection pooling
  • HTTP/2 support
  • Async I/O
  • Memory safety
  • Zero-copy optimizations

Troubleshooting

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

Alternative: Native TLS

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.