Skip to content

Add ssl_client_config to MySQL and Postgres connect options for a prebuilt Rustls ClientConfig - #4550

Open
carl-wallace wants to merge 4 commits into
transact-rs:mainfrom
carl-wallace:rustls-client-config
Open

carl-wallace wants to merge 4 commits into
transact-rs:mainfrom
carl-wallace:rustls-client-config

Conversation

@carl-wallace

Copy link
Copy Markdown

Does your PR solve an issue?

No issue is closed by this PR. It implements the "everything else" path from the proposal in #4408 (a user-supplied rustls::ClientConfig), but that also covers using an installed CryptoProvider::get_default() and a broader feature reorganization, neither of which is done here. It also builds on the idea in #4051, which was Postgres only.

Is this a breaking change?

No. The change only adds API: the ssl_client_config setters on MySqlConnectOptions and PgConnectOptions, plus PrebuiltTlsConfig, IntoPrebuiltTlsConfig and connector_from_prebuilt in sqlx-core::net::tls. Nothing changes unless the setter is called. The SSL mode, the other ssl_* options, the environment variables and URL handling behave as before. The one change to existing code makes RustlsConnector.config pub(super), which is still internal to sqlx-core.

Description

This implements the "everything else" path from #4408 and builds on ideas from #4051. It allows an application to hand SQLx a rustls::ClientConfig it prepared for SQLx to use.

The reason for my preparing this PR is a need to use an HSM to authenticate to a MariaDB instance. Generic rationale includes support for:

  • Keys that can't be files. A rustls::client::ResolvesClientCert lets the client key stay in an HSM or token (PKCS #11 here), which ssl_client_key cannot express.
  • Choosing the crypto provider. The FIPS 140-3 use case in RFC: Reorganize rustls Features #4408 (provider per OS, e.g. AWS-LC FIPS, Apple corecrypto, Windows CNG) needs the database connection to use the application's provider rather than the one SQLx constructs.
  • Tightly controlled trust anchor set. Trusting only a private CA bundle, with no webpki or native roots added (also raised in RFC: Reorganize rustls Features #4408).

This PR covers MySQL and Postgres and adds a public setter. The changes are relatively minimal and include the following:

  • MySqlConnectOptions::ssl_client_config and PgConnectOptions::ssl_client_config take a rustls::ClientConfig or an Arc of one.
  • In sqlx-core::net::tls: PrebuiltTlsConfig (an opaque newtype the driver crates can carry without TLS features of their own), the IntoPrebuiltTlsConfig trait, and connector_from_prebuilt.

Aside from the configuration options opened up by using an application-provided Rustls configuration, behavior changes are limited and include the following:

  • When a config is set, SQLx builds no part of the TLS configuration. ssl_ca / ssl_root_cert, ssl_client_cert and ssl_client_key, and on Postgres the PGSSLROOTCERT / PGSSLCERT / PGSSLKEY variables, are not used. Certificate and hostname verification are whatever the config does.
  • The SSL mode is unchanged and still decides whether TLS is used. Preferred / Prefer fall back as they do today.
  • The setter goes through ssl_options_mut(), so a cached connector is reset like with the other ssl_* setters.
  • The configuration cannot be expressed in a connection URL, so ConnectOptions::to_url_lossy does not include it.

Native-tls is not supported. IntoPrebuiltTlsConfig is implemented only when Rustls is the backend, i.e. a tls-rustls* feature without tls-native-tls. With both enabled, native-tls takes precedence, and passing a config fails at compile time with an #[diagnostic::on_unimplemented] message explaining why (using the same approach as SqlSafeStr). The alternative was a runtime error on every connect, or silently ignoring the config. connector_from_prebuilt is therefore infallible.

The following tests were added and successfully run. In addition to these tests, I have tested using an Entrust HSM to authenticate to a MariaDB instance.

  • A unit test in tls_rustls.rs, modeled on flush_and_shutdown_do_not_wait_for_a_silent_peer: a prebuilt config's resolver answers a server that requires client authentication.
  • connect_with_a_client_key_in_a_pkcs11_token in the Postgres and MySQL test targets. CI imports the gen-certs.sh client key into a SoftHSM token, and the test connects to the *_client_ssl server through ssl_client_config, with the key signing inside the token and the server name (sqlx.test) verified. It runs in the Rustls cells of the Postgres, MySQL and MariaDB SSL Auth jobs and returns early unless SQLX_TEST_PKCS11_MODULE is set. The PKCS #11 helper (cryptoki) lives in sqlx-test, so no published crate gains a dependency.
  • On Postgres, pg_hba.conf only admits the login with a certificate. The MySQL and MariaDB test servers ask for a client certificate but don't require one (no REQUIRE X509 on root), so that test also checks the token actually signed. This also means the existing MySQL/MariaDB SSL Auth jobs would still pass if a client certificate stopped being sent; a REQUIRE X509 test account could close that, but it's out of scope here.

CI has been successfully run on my fork for SQLx and Examples: https://github.com/carl-wallace/sqlx/actions/runs/45179834452, https://github.com/carl-wallace/sqlx/actions/runs/45179834391. CI for SQLx CLI is green except for the macOS-latest integration test, which has not gotten a hosted runner: https://github.com/carl-wallace/sqlx/actions/runs/45179834271

…ls ClientConfig

- Adds ssl_client_config to SslOptions to enable applications to prepare a rustls::ClientConfig, including a crypto provider, trust anchors and a client certificate resolver whose key can stay in an HSM or token, for use by SQLx. When ssl_client_config is set, the ssl_ca, ssl_client_cert and ssl_client_key options are not used, and the SSL mode only decides whether TLS is used.

- The configuration is passed through a new IntoPrebuiltTlsConfig trait that is implemented only when Rustls is the TLS backend. If tls-native-tls is also enabled, native-tls is the backend, and passing a configuration fails to compile with an on_unimplemented message explaining why, rather than failing at connect time.

- Relates to transact-rs#4408.
…ClientConfig

- The Postgres counterpart of MySqlConnectOptions::ssl_client_config change. It uses the same IntoPrebuiltTlsConfig trait and connector. When it is set, the ssl_root_cert, ssl_client_cert and ssl_client_key options (and the PGSSLROOTCERT, PGSSLCERT and PGSSLKEY environment variables) are not used, and the SSL mode only decides whether TLS is used.

- Relates to transact-rs#4408 and transact-rs#4051.
- Adds connect_with_a_client_key_in_a_pkcs11_token to the Postgres and MySQL test targets. Each passes ssl_client_config a Rustls ClientConfig whose client certificate resolver signs with the gen-certs.sh client key held in a PKCS transact-rs#11 token, connects to the client_ssl server as sqlx.test so the server's name is verified, and checks that the token was used: on Postgres, pg_hba.conf only admits the login with a client certificate; on MySQL and MariaDB, which do not require one, the token's signature count must be above zero. The tests are compiled only for Rustls-only builds and return early unless SQLX_TEST_PKCS11_MODULE is set.

- Adds sqlx-test's pkcs11 module, which builds that configuration with cryptoki (CKM_EDDSA for the Ed25519 test key). sqlx-test is not published, so no published crate gains a dependency.

- In the Postgres, MySQL and MariaDB SSL Auth jobs, a step after gen-certs.sh installs SoftHSM, imports tests/certs/keys/client.key into a token and sets SQLX_TEST_PKCS11_MODULE and SOFTHSM2_CONF for the job's full test run.

- Relates to transact-rs#4408.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant