Connect with TLS¶
Encrypt the connection when the network between your device and SQL Server isn't one you trust, or when the server requires encryption. tdslite uses TDS 8.0 strict mode: the TLS handshake comes first, and everything after it, the password included, is encrypted.
Check the server first¶
Strict mode needs:
- SQL Server 2025, SQL Server 2022 on Windows, or Azure SQL. SQL Server 2022 on Linux doesn't accept strict connections.
- A TLS certificate configured on the server: on Linux,
network.tlscertandnetwork.tlskeyinmssql-conf. SQL Server's self-generated certificate isn't enough; with it, the server closes the connection (error 17821 in its log). - The PEM file of the CA that signed the server's certificate, to give to your device.
Connect¶
Use WiFiClientSecure as the client. It does the TLS part, so configure
it through driver.network().client(), then tell tdslite that the
connection is encrypted with set_tls_client(true):
#include <WiFiClientSecure.h>
#include <tdslite.h>
tdsl::uint8_t net_buf [4096] = {};
tdsl::arduino_driver<WiFiClientSecure> driver{net_buf};
// PEM text of the CA that signed the server's certificate
static const char ca_pem [] = R"(-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
)";
void setup() {
// ... connect to Wi-Fi, and set the clock (see below)
driver.network().client().setCACert(ca_pem);
driver.network().set_tls_client(true);
// then driver.connect(params) as usual
}
On ESP8266, BearSSL puts at most 512 bytes into a TLS record by default, and SQL Server drops a strict connection when a TDS packet is split over several records. Raise the buffer, and tell tdslite the limit:
static BearSSL::X509List trusted_ca{ca_pem}; // must outlive the connection
driver.network().client().setTrustAnchors(&trusted_ca);
driver.network().client().setBufferSizes(16384, 2048);
driver.network().set_tls_client(true, 2048);
params.packet_size = 2048;
Set the clock. Checking a certificate needs the current time. Before connecting, get it over NTP:
The complete sketch is examples/boards/esp_wifi_tls.
Use tdsl::esp_tls_driver in place of tdsl::socket_driver. The
tdslite component already depends on esp-tls.
Embed the CA certificate in your component's CMakeLists.txt:
and give it to the driver before connecting:
#include <tdslite-net/esp-idf/tdsl_netimpl_esp_tls.hpp>
extern const char ca_pem [] asm("_binary_ca_pem_start");
static tdsl::uint8_t net_buf [2048] = {};
static tdsl::esp_tls_driver driver{net_buf};
tdsl::net::tdsl_esp_tls_options tls;
tls.ca_pem = ca_pem; // or tls.use_crt_bundle = true for public CAs (Azure SQL)
driver.network().set_tls_options(tls);
// then driver.connect(params) as usual
Check certificate dates. By default ESP-IDF doesn't check a
certificate's validity dates (CONFIG_MBEDTLS_HAVE_TIME_DATE=n), so an
expired certificate from a trusted CA is accepted. To check them, enable
CONFIG_MBEDTLS_HAVE_TIME_DATE and set the clock (e.g. over SNTP, with
esp_sntp) before connecting. Without a set clock, every certificate
then looks not yet valid, and the connection fails. The ESPHome
component does both for you.
Record size. CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN must be at least the
TDS packet size (params.packet_size, 4096 by default, which is also
mbedTLS's default).
The complete project is examples/esp-idf/query-tls.
Link with the tdslite.net.asio.tls CMake target. It exists when CMake
finds OpenSSL: look for tdslite.net.asio.tls: enabled in the CMake
output. tdslite's own CMake project gets OpenSSL from conan, so there's
nothing to install.
#include <tdslite/tdslite.hpp>
#include <tdslite-net/asio/tdsl_netimpl_asio_tls.hpp>
tdsl::detail::tdsl_driver<tdsl::net::tdsl_netimpl_asio_tls> driver;
tdsl::net::tdsl_asio_tls_options tls;
tls.ca_file = "/path/to/ca.pem"; // empty: the system's certificate store
driver.network().set_tls_options(tls);
// then driver.connect(params) as usual
Connect by IP address¶
The certificate is checked against the host name you give to connect. If
you connect by IP address, either the certificate must list that address, or
you name a host that it lists:
On Arduino, WiFiClientSecure checks the certificate against the name you
connect to, so connect by a host name that the certificate lists, as the
example sketch does.
Keep transactions inside one query¶
On a TLS connection, a transaction must start and end within one
execute_query (or one stored procedure):
driver.execute_query(
"BEGIN TRAN; UPDATE stock SET n = n - 1 WHERE id = 7; INSERT INTO log VALUES (7); COMMIT;");
A BEGIN TRAN on its own makes every later request fail with error 3989,
until the driver reconnects.
TDS versions and encryption
explains why.
If it doesn't connect¶
A failed TLS connection makes connect return connection_failed. See
Troubleshoot and the TLS error
codes in Results and error codes.
Related¶
- TDS versions and encryption: what strict mode is, and why tdslite uses it.
- Network backends: the TLS options of each backend.