Skip to main content

Crate mssql_tls

Crate mssql_tls 

Source
Expand description

§mssql-tls

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

§TDS Version Differences

§TDS 7.x (SQL Server 2019 and earlier)

TCP Connect → PreLogin (cleartext) → TLS Handshake → Login7 (encrypted)

§TDS 8.0 (SQL Server 2022+ strict mode)

TCP Connect → TLS Handshake → PreLogin (encrypted) → Login7 (encrypted)

§Features

  • TLS 1.2 and TLS 1.3 support via rustls
  • Server certificate validation
  • Hostname verification
  • Custom certificate authority support
  • Client certificate authentication (TDS 8.0)
  • Optional OS/platform trust store (native-certs feature)

§Security

By default, this crate validates server certificates using the Mozilla root certificate store. The TrustServerCertificate option disables validation but logs a warning - this should only be used for development.

Enterprise deployments often issue server certificates from an internal CA that lives in the operating system trust store rather than Mozilla’s list. Enable the native-certs feature to delegate validation to the OS/platform verifier (Windows CryptoAPI, macOS SecTrust, Linux native certs), which honors those internal CAs along with OS revocation and policy. Explicit TlsConfig::add_root_certificate roots always take precedence over the OS store.

ⓘ
use mssql_tls::{TlsConfig, TlsConnector, default_tls_config};

// Secure default configuration
let config = default_tls_config()?;

// Or use the builder pattern
let tls_config = TlsConfig::new()
    .strict_mode(true)  // TDS 8.0
    .min_protocol_version(TlsVersion::Tls13);

§Encryption modes

The driver’s Encrypt connection-string setting maps onto this crate as:

EncryptTLSNegotiationNotes
strictYesTlsNegotiationMode::Strict (TDS 8.0)All traffic encrypted, including PreLogin. SQL Server 2022+ only.
true / mandatoryYesTlsNegotiationMode::PostPreLoginPreLogin is cleartext; Login7 onward is encrypted.
no_tlsNo—No TLS at all; credentials travel in plaintext.

no_tls exists only for legacy SQL Server (2008-2016) that cannot negotiate TLS 1.2+. rustls does not support TLS 1.0/1.1, so those servers cannot use TLS through this driver — use no_tls only on a trusted network.

§SQL Server version requirements

VersionTLS supportTDS 8.0 strict
2008-2016TLS 1.0/1.1 by default — use no_tls, or configure the server for 1.2No
2017-2019TLS 1.2No
2022+TLS 1.2 / 1.3Yes
Azure SQLTLS 1.2 minimumVaries

§Certificate validation

By default the server certificate is validated against the Mozilla root CA store, must be unexpired, and must match the server hostname.

use mssql_tls::{TlsConfig, TlsVersion};

let _config = TlsConfig::new()
    .min_protocol_version(TlsVersion::Tls12)
    .max_protocol_version(TlsVersion::Tls13)
    .strict_mode(true); // TDS 8.0

For an internal CA or self-signed certificate, add the CA with TlsConfig::add_root_certificate; override the verified hostname with TlsConfig::with_server_name. TlsConfig::trust_server_certificate disables validation entirely and is for development only — it logs a warning and leaves the connection open to man-in-the-middle attacks.

§Troubleshooting

  • certificate verify failed — self-signed/internal cert (add the CA), expired cert, or hostname mismatch (set TlsConfig::with_server_name).
  • TLS handshake times out — a firewall on port 1433, or the server is not configured for encryption.
  • handshake failure / no shared protocol — the server requires a TLS version outside the configured range; widen it with TlsConfig::min_protocol_version / TlsConfig::max_protocol_version.
  • Strict mode rejected — Encrypt=strict requires SQL Server 2022+; use Encrypt=true for older servers.

§Security recommendations

For production: Encrypt=true or strict, TrustServerCertificate=false, TLS 1.2 minimum, and a server certificate from a trusted CA whose name matches the host. PCI DSS, HIPAA, SOC 2, and FedRAMP all require TLS 1.2+ (FedRAMP additionally requires FIPS 140-2 validated cryptography).

Re-exports§

pub use config::ClientAuth;
pub use config::TlsConfig;
pub use config::TlsVersion;
pub use connector::TlsConnector;
pub use connector::default_tls_config;
pub use error::TlsError;
pub use prelogin_wrapper::TlsPreloginWrapper;

Modules§

config
TLS configuration options.
connector
TLS connector for establishing encrypted connections.
error
TLS-related error types.
prelogin_wrapper
TDS PreLogin wrapper for TLS handshake.

Structs§

CertificateDer
A DER-encoded X.509 certificate; as specified in RFC 5280
TlsStream
A wrapper around an underlying raw stream which implements the TLS or SSL protocol.

Enums§

PrivateKeyDer
A DER-encoded X.509 private key, in one of several formats
TlsNegotiationMode
TDS TLS negotiation mode.