Codive Tunnel - Secure Tunneling Protocol Library
A Rust library providing end-to-end encrypted tunneling protocol types, cryptographic primitives, and wire format encoding for secure Codive communication.
Overview
This crate is the shared foundation for the Codive tunneling system:
- codive-relay uses it for protocol parsing and message routing
- codive-cli uses it for encryption/decryption and tunnel client logic
Features
Cryptography
- XChaCha20-Poly1305: AEAD encryption with 256-bit keys
- Random Nonces: 24-byte nonces generated per message (no reuse risk)
- Secure Key Handling:
Zeroizetrait implementation for memory safety - URL-Safe Key Encoding: Base64url encoding for key sharing via URL fragments
Protocol
- Control Messages: Hello, Welcome, Ping, Pong, Close, Error
- Data Messages: HttpRequest, HttpResponse, HttpResponseChunk, RequestError
- Wire Format: Efficient binary encoding with message type headers
- Routing Headers: Request ID routing for encrypted responses
Usage
Add to your Cargo.toml:
[]
= { = "../codive-tunnel" }
Key Generation and Encryption
use ;
// Generate a new encryption key
let key = generate;
// Share via URL fragment (never sent to server)
let url = format!;
// Create crypto instance
let crypto = new;
// Encrypt data
let plaintext = b"Hello, World!";
let ciphertext = crypto.encrypt?;
// Decrypt data
let decrypted = crypto.decrypt?;
assert_eq!;
Protocol Messages
use ;
// Create a Hello message
let hello = Hello ;
// Encode for transmission
let encoded = encode_control?;
// Decode received message
let decoded = decode_control?;
HTTP Request/Response
use DataMessage;
use HashMap;
// Create an HTTP request
let request = HttpRequest ;
// Serialize to JSON
let json = to_vec?;
// Create a response
let response = HttpResponse ;
Wire Message Format
use ;
// Encode encrypted data with routing header
let wire_msg = encode_encrypted_with_routing;
// Decode with routing header
let =
decode_encrypted_with_routing?;
API Reference
Types
| Type | Description |
|---|---|
TunnelKey |
256-bit encryption key with Zeroize |
TunnelCrypto |
XChaCha20-Poly1305 encrypt/decrypt |
ControlMessage |
Protocol control messages (Hello, Welcome, etc.) |
DataMessage |
Data messages (HttpRequest, HttpResponse, etc.) |
WireMessage |
Binary wire format encoding/decoding |
Control Messages
| Message | Direction | Purpose |
|---|---|---|
Hello |
Agent → Relay | Initial handshake with auth |
Welcome |
Relay → Agent | Tunnel ID and URL assignment |
Ping |
Both | Keep-alive request |
Pong |
Both | Keep-alive response |
Close |
Both | Graceful disconnect |
Error |
Relay → Agent | Error notification |
ClientConnected |
Relay → Agent | Client connected to tunnel |
ClientDisconnected |
Relay → Agent | Client disconnected |
Data Messages
| Message | Direction | Purpose |
|---|---|---|
HttpRequest |
Relay → Agent | Incoming HTTP request |
HttpResponse |
Agent → Relay | HTTP response |
HttpResponseChunk |
Agent → Relay | Streaming response chunk |
RequestError |
Agent → Relay | Request processing error |
Message Type Constants
Wire Protocol
Binary Format
Control Messages:
┌─────────┬─────────────────────┐
│ 0x00 │ JSON payload │
└─────────┴─────────────────────┘
Encrypted Messages:
┌─────────┬─────────────────────┐
│ type │ encrypted payload │
└─────────┴─────────────────────┘
Encrypted with Routing Header:
┌─────────┬─────────┬───────────────┬─────────────────────┐
│ type │ id_len │ request_id │ encrypted payload │
└─────────┴─────────┴───────────────┴─────────────────────┘
Encryption Format
Ciphertext:
┌─────────────────────┬─────────────────────┬─────────────────────┐
│ nonce (24 bytes) │ ciphertext │ auth tag (16 bytes) │
└─────────────────────┴─────────────────────┴─────────────────────┘
Security Considerations
Key Exchange
- Keys are shared via URL fragment (
#E2EKey=...) - Fragments are never sent to the server in HTTP requests
- Keys should be rotated periodically for long-lived tunnels
Memory Safety
TunnelKeyimplementsZeroizeandZeroizeOnDrop- Key material is cleared from memory when dropped
- No key logging in debug output (
Debugshows[REDACTED])
Nonce Safety
- 24-byte random nonces (XChaCha20)
- Extremely low collision probability (2^-96)
- No nonce counter needed
Testing
# Run all tests
# Run with output
# Run specific test
Test coverage: 62 tests
- Crypto tests: Key generation, encryption, decryption, tampering detection
- Protocol tests: Message serialization, wire encoding
- Integration tests: Full E2E flows, streaming, error handling
Dependencies
chacha20poly1305- AEAD encryptionzeroize- Secure memory clearingrand- Cryptographic RNGbase64- URL-safe encodingserde/serde_json- Serialization
Contributing
See the codive-relay README for contribution guidelines.
Adding New Message Types
- Add variant to
ControlMessageorDataMessageinprotocol.rs - Update
serdeattributes if needed - Add tests for serialization roundtrip
- Update wire format handling if binary encoding changes
License
MIT License - see LICENSE file for details.