Repository navigation
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
Open
carl-wallace wants to merge 4 commits into
carl-wallace wants to merge 4 commits into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 installedCryptoProvider::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_configsetters onMySqlConnectOptionsandPgConnectOptions, plusPrebuiltTlsConfig,IntoPrebuiltTlsConfigandconnector_from_prebuiltinsqlx-core::net::tls. Nothing changes unless the setter is called. The SSL mode, the otherssl_*options, the environment variables and URL handling behave as before. The one change to existing code makesRustlsConnector.configpub(super), which is still internal tosqlx-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::ClientConfigit 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:
rustls::client::ResolvesClientCertlets the client key stay in an HSM or token (PKCS #11here), whichssl_client_keycannot express.rustlsFeatures #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.rustlsFeatures #4408).This PR covers MySQL and Postgres and adds a public setter. The changes are relatively minimal and include the following:
MySqlConnectOptions::ssl_client_configandPgConnectOptions::ssl_client_configtake arustls::ClientConfigor anArcof one.sqlx-core::net::tls:PrebuiltTlsConfig(an opaque newtype the driver crates can carry without TLS features of their own), theIntoPrebuiltTlsConfigtrait, andconnector_from_prebuilt.Aside from the configuration options opened up by using an application-provided Rustls configuration, behavior changes are limited and include the following:
ssl_ca/ssl_root_cert,ssl_client_certandssl_client_key, and on Postgres thePGSSLROOTCERT/PGSSLCERT/PGSSLKEYvariables, are not used. Certificate and hostname verification are whatever the config does.Preferred/Preferfall back as they do today.ssl_options_mut(), so a cached connector is reset like with the otherssl_*setters.ConnectOptions::to_url_lossydoes not include it.Native-tls is not supported.
IntoPrebuiltTlsConfigis implemented only when Rustls is the backend, i.e. atls-rustls*feature withouttls-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 asSqlSafeStr). The alternative was a runtime error on every connect, or silently ignoring the config.connector_from_prebuiltis 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.
tls_rustls.rs, modeled onflush_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_tokenin the Postgres and MySQL test targets. CI imports thegen-certs.shclient key into a SoftHSM token, and the test connects to the*_client_sslserver throughssl_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 unlessSQLX_TEST_PKCS11_MODULEis set. ThePKCS #11helper (cryptoki) lives insqlx-test, so no published crate gains a dependency.pg_hba.confonly admits the login with a certificate. The MySQL and MariaDB test servers ask for a client certificate but don't require one (noREQUIRE X509onroot), 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; aREQUIRE X509test 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