1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! # 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)
//! ```text
//! TCP Connect → PreLogin (cleartext) → TLS Handshake → Login7 (encrypted)
//! ```
//!
//! ### TDS 8.0 (SQL Server 2022+ strict mode)
//! ```text
//! 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)
//!
//! ## 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.
//!
//! ```rust,ignore
//! 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:
//!
//! | `Encrypt` | TLS | Negotiation | Notes |
//! |-----------|-----|-------------|-------|
//! | `strict` | Yes | [`TlsNegotiationMode::Strict`] (TDS 8.0) | All traffic encrypted, including PreLogin. SQL Server 2022+ only. |
//! | `true` / `mandatory` | Yes | [`TlsNegotiationMode::PostPreLogin`] | PreLogin is cleartext; Login7 onward is encrypted. |
//! | `no_tls` | No | — | 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
//!
//! | Version | TLS support | TDS 8.0 strict |
//! |---------|-------------|----------------|
//! | 2008-2016 | TLS 1.0/1.1 by default — use `no_tls`, or configure the server for 1.2 | No |
//! | 2017-2019 | TLS 1.2 | No |
//! | 2022+ | TLS 1.2 / 1.3 | Yes |
//! | Azure SQL | TLS 1.2 minimum | Varies |
//!
//! ## Certificate validation
//!
//! By default the server certificate is validated against the Mozilla root CA
//! store, must be unexpired, and must match the server hostname.
//!
//! ```rust,no_run
//! 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).
pub use ;
pub use ;
pub use TlsError;
pub use TlsPreloginWrapper;
// Re-export tokio-rustls stream type for convenience
pub use TlsStream;
// Re-export rustls PKI types so users can construct TLS configs without adding
// a direct dependency on the `rustls` crate. Changing these re-exports is a
// semver-breaking change (this crate is coupled to rustls 0.23.x).
pub use ;
/// TDS TLS negotiation mode.
///
/// This determines when TLS handshake occurs relative to TDS protocol messages.