| Crates.io | mssql-tls |
| lib.rs | mssql-tls |
| version | 0.6.0 |
| created_at | 2025-12-17 22:55:09.359699+00 |
| updated_at | 2026-01-13 22:17:54.733179+00 |
| description | TLS negotiation for SQL Server connections (TDS 7.x and 8.0) |
| homepage | |
| repository | https://github.com/praxiomlabs/rust-mssql-driver |
| max_upload_size | |
| id | 1991262 |
| size | 57,608 |
Part of the rust-mssql-driver project.
TLS negotiation layer for SQL Server connections.
This crate handles the complexity of TLS negotiation for both TDS 7.x (pre-login encryption negotiation) and TDS 8.0 (strict TLS-first mode). It uses rustls for a pure-Rust, memory-safe TLS implementation.
TCP Connect -> PreLogin (cleartext) -> TLS Handshake -> Login7 (encrypted)
TCP Connect -> TLS Handshake -> PreLogin (encrypted) -> Login7 (encrypted)
use mssql_tls::{default_tls_config, TlsConnector};
// Secure default configuration
let config = default_tls_config()?;
let connector = TlsConnector::new(config);
use mssql_tls::{TlsConfig, TlsVersion};
let config = TlsConfig::builder()
.strict_mode(true) // TDS 8.0
.min_protocol_version(TlsVersion::Tls13)
.hostname_verification(true)
.build()?;
// WARNING: Disables certificate validation - development only!
let config = TlsConfig::builder()
.trust_server_certificate(true)
.build()?;
let config = TlsConfig::builder()
.ca_certificate_path("/path/to/ca.pem")
.build()?;
use mssql_tls::ClientAuth;
let config = TlsConfig::builder()
.strict_mode(true) // Required for client certs
.client_auth(ClientAuth::Certificate {
cert_path: "/path/to/client.pem".into(),
key_path: "/path/to/client-key.pem".into(),
})
.build()?;
| Mode | When Used | Description |
|---|---|---|
PostPreLogin |
TDS 7.x, Encrypt=true |
TLS after PreLogin exchange |
Strict |
TDS 8.0, Encrypt=strict |
TLS immediately after TCP |
use mssql_tls::TlsNegotiationMode;
let mode = TlsNegotiationMode::from_encrypt_mode(encrypt_strict);
if mode.is_tls_first() {
// TDS 8.0: TLS handshake before any TDS traffic
}
| Module | Description |
|---|---|
config |
TLS configuration builder |
connector |
TLS connection establishment |
error |
TLS error types |
| Type | Description |
|---|---|
TlsConfig |
TLS configuration options |
TlsConnector |
Establishes TLS connections |
TlsVersion |
TLS protocol versions |
TlsNegotiationMode |
When TLS handshake occurs |
ClientAuth |
Client authentication options |
TlsStream |
Encrypted stream (re-exported from tokio-rustls) |
By default, this crate validates server certificates using the Mozilla root certificate store. This provides:
The trust_server_certificate option disables validation and logs a warning. Use only for:
Never use in production without explicit security review.
SQL Server 2022+ supports strict TLS mode where:
use mssql_tls::TlsError;
match connector.connect(stream, hostname).await {
Ok(tls_stream) => { /* use encrypted stream */ }
Err(TlsError::HandshakeFailed(e)) => {
// TLS handshake failed
}
Err(TlsError::CertificateInvalid(e)) => {
// Server certificate validation failed
}
Err(TlsError::HostnameVerificationFailed) => {
// Certificate CN doesn't match hostname
}
Err(e) => {
// Other errors
}
}
MIT OR Apache-2.0