Skip to content

feat: ssl.mode = argument to dbConnect() to require or relax TLS verification - #650

Merged
krlmlr merged 2 commits into
mainfrom
claude/tls-mode
Nov 29, 2026
Merged

krlmlr merged 2 commits into
mainfrom
claude/tls-mode

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Problem

#341: the server runs with require_secure_transport = ON. The mysql command-line client connects, but dbConnect() fails with "Connections using insecure transport are prohibited". dbConnect() has no argument to require TLS (sslmode = "REQUIRED" lands in ... and is ignored). The only ways are client.flag = CLIENT_SSL or an option file, and the documentation does not explain either well.

#631: after upgrading to a build with MariaDB Connector/C 3.4, connecting to a server with a self-signed certificate fails with "TLS/SSL error: self-signed certificate in certificate chain". Connector/C 3.4 turned on TLS with peer verification by default. client.flag = CLIENT_SSL does not help, because it requests TLS but leaves the default verification on. The only working fix was an option file with ssl-enforce=1 and ssl-verify-server-cert=0, as also found in #256.

Design

A new argument ssl.mode of dbConnect(), modeled after the --ssl-mode option of the mysql command-line client and MariaDB's --ssl plus --ssl-verify-server-cert. It takes "disabled", "preferred", "required", "verify_ca", or "verify_identity". The default NULL sets no option, so nothing changes for anyone who does not pass the argument. The R side validates the value with rlang::arg_match0(), and rejects the combination with the CLIENT_SSL or CLIENT_SSL_VERIFY_SERVER_CERT flags (two ways to say the same thing that may disagree) and the combination of "disabled" with any other ssl.* argument (Connector/C turns TLS back on when one of them is set).

Per client library, detected by header macros as in #628, not by MYSQL_VERSION_ID:

  • MariaDB Connector/C 3.x (MARIADB_PACKAGE_VERSION_ID >= 30000, defined only by Connector/C, so the client libraries of MariaDB server 10.0 and 10.1 from Connection options assume a client library feature set by version number (SSL_ENFORCE, deprecated RECONNECT) #630 are excluded): MYSQL_OPT_SSL_ENFORCE is off for "disabled" and on otherwise, MYSQL_OPT_SSL_VERIFY_SERVER_CERT is on only for "verify_identity". Connector/C has a single verification switch that checks the chain and the host name together, so "verify_ca" is an error that points to "verify_identity". Connector/C silently falls back to an unencrypted connection when the server offers no TLS and verification is off (plugins/auth/my_auth.c in 3.3 and 3.4), so for "required" and "verify_identity" the connection is closed with an error if mysql_get_ssl_cipher() returns NULL after connecting. "preferred" is exactly that fallback behavior.
  • MySQL client library 8.0 and later, including the 26.x series (RMARIADB_MYSQL_8_OR_LATER from fix: Detect MariaDB Connector/C by its macros, not by version number #628): MYSQL_OPT_SSL_MODE with the matching SSL_MODE_* value. All five modes work.
  • Any other client library: every mode is an error naming the client version.

What CLIENT_SSL actually does today, verified with the code and the runs below: with the MySQL client library it sets SSL_MODE_REQUIRED; with Connector/C it sets MYSQL_OPT_SSL_ENFORCE, which encrypts but keeps the library's verification default (off before 3.4, on from 3.4) and falls back to plaintext if the server has no TLS. So the old advice "To enforce SSL, add CLIENT_SSL" was right for MySQL and wrong in two ways for Connector/C. The Client-flags help page now points to ssl.mode and describes the flags per library, and the dbConnect() example uses ssl.mode = "verify_identity", ssl.ca = ... instead of client.flag = CLIENT_SSL (the example also had an uncommented prose line that was a syntax error).

The hint on error 2026, which suggested MARIADB_TLS_DISABLE_PEER_VERIFICATION=1, now suggests ssl.ca or ssl.mode = "required", and only appears if the message mentions a certificate (not for "SSL is required but the server doesn't support it").

Evidence

A separate MariaDB 10.11 instance with a self-signed CA, a server certificate for localhost/127.0.0.1, and --require-secure-transport=ON, listening on 127.0.0.1:3307. Each line is dbConnect(MariaDB(), host = "127.0.0.1", port = 3307, user = "tls", password = "tls", ...) followed by SHOW STATUS LIKE 'Ssl_cipher'. MariaDB 10.11 reports a plaintext login under require_secure_transport as "Access denied" (MySQL server reports the "insecure transport" message from #341); with SET GLOBAL require_secure_transport = OFF the same plaintext login succeeds.

RMariaDB built against the system libmariadb-dev (Connector/C 3.3.17):

ssl.mode = NULL (default)                  ERROR: Failed to connect: Access denied for user 'tls'@'localhost' (using password: YES)
ssl.mode = 'disabled'                      ERROR: Failed to connect: Access denied for user 'tls'@'localhost' (using password: YES)
ssl.mode = 'preferred'                     OK, Ssl_cipher = 'TLS_AES_256_GCM_SHA384'
ssl.mode = 'required'                      OK, Ssl_cipher = 'TLS_AES_256_GCM_SHA384'
ssl.mode = 'verify_ca'                     ERROR: `ssl.mode = "verify_ca"` is not supported by MariaDB Connector/C 3.3.17, which cannot verify the certificate without the host name. Use `ssl.mode = "verify_identity"` instead.
ssl.mode = 'verify_identity'               ERROR: Failed to connect: TLS/SSL error: self-signed certificate in certificate chain | If the server certificate is self-signed, pass its CA certificate as `ssl.ca`, or use `ssl.mode = "required"` to encrypt without verifying it.
ssl.mode = 'verify_identity', ssl.ca       OK, Ssl_cipher = 'TLS_AES_256_GCM_SHA384'
client.flag = CLIENT_SSL                   OK, Ssl_cipher = 'TLS_AES_256_GCM_SHA384'
ssl.mode = 'required', CLIENT_SSL          ERROR: Use either `ssl.mode` or the `CLIENT_SSL` and `CLIENT_SSL_VERIFY_SERVER_CERT` flags in `client.flag`, not both.
ssl.mode = 'disabled', ssl.ca              ERROR: `ssl.mode = "disabled"` cannot be combined with `ssl.key`, `ssl.cert`, `ssl.ca`, `ssl.capath`, or `ssl.cipher`.
ssl.mode = 'strict'                        ERROR: `ssl.mode` must be one of "disabled", "preferred", "required", "verify_ca", or "verify_identity", not "strict".

RMariaDB built against libmysqlclient 8.0.46 (headers from the Ubuntu libmysqlclient-dev package extracted to a local prefix, the installed libmysqlclient21 at run time):

ssl.mode = NULL (default)                  OK, Ssl_cipher = 'TLS_AES_256_GCM_SHA384'
ssl.mode = 'disabled'                      ERROR: Failed to connect: Access denied for user 'tls'@'localhost' (using password: YES)
ssl.mode = 'preferred'                     OK, Ssl_cipher = 'TLS_AES_256_GCM_SHA384'
ssl.mode = 'required'                      OK, Ssl_cipher = 'TLS_AES_256_GCM_SHA384'
ssl.mode = 'verify_ca'                     ERROR: Failed to connect: SSL connection error: CA certificate is required if ssl-mode is VERIFY_CA or VERIFY_IDENTITY | If the server certificate is self-signed, ...
ssl.mode = 'verify_ca', ssl.ca             OK, Ssl_cipher = 'TLS_AES_256_GCM_SHA384'
ssl.mode = 'verify_identity'               ERROR: Failed to connect: SSL connection error: CA certificate is required if ssl-mode is VERIFY_CA or VERIFY_IDENTITY | If the server certificate is self-signed, ...
ssl.mode = 'verify_identity', ssl.ca       OK, Ssl_cipher = 'TLS_AES_256_GCM_SHA384'
client.flag = CLIENT_SSL                   OK, Ssl_cipher = 'TLS_AES_256_GCM_SHA384'

RMariaDB built against Connector/C 3.4.5 from source in a local prefix, which reproduces #631. Connector/C 3.4 skips verification for the host names 127.0.0.1 and ::1, so this run uses host = "vm", an /etc/hosts alias for 127.0.0.1:

ssl.mode = NULL (default)                  ERROR: Failed to connect: TLS/SSL error: Certificate verification failure: The certificate is NOT trusted. | If the server certificate is self-signed, ...
client.flag = CLIENT_SSL                   ERROR: Failed to connect: TLS/SSL error: Certificate verification failure: The certificate is NOT trusted. | If the server certificate is self-signed, ...
ssl.mode = 'preferred'                     OK, Ssl_cipher = 'TLS_AES_256_GCM_SHA384'
ssl.mode = 'required'                      OK, Ssl_cipher = 'TLS_AES_256_GCM_SHA384'
ssl.mode = 'disabled'                      ERROR: Failed to connect: Access denied for user 'tls'@'localhost' (using password: YES)
ssl.mode = 'verify_identity'               ERROR: Failed to connect: TLS/SSL error: Certificate verification failure: The certificate is NOT trusted. | ...
ssl.mode = 'verify_identity', ssl.ca       ERROR: Failed to connect: TLS/SSL error: Hostname verification failed

The last line is expected: the certificate is issued for localhost and 127.0.0.1, not vm.

ssl.mode = "disabled" with Connector/C 3.4.5, whose default is TLS with verification: with require_secure_transport = OFF it connects without TLS (Ssl_cipher = '') for both 127.0.0.1 and vm, where the default connects with TLS on 127.0.0.1 and fails verification on vm. With require_secure_transport = ON it is refused ("Access denied") for both hosts.

Option files with libmysqlclient 8.0.46: ssl-mode=VERIFY_IDENTITY or ssl-mode=DISABLED in [client] or [rs-dbi] has no effect, with or without ssl.mode (the connection uses TLS without verification as before). The file is read, though: ssl-ca=/nonexistent/ca.pem or an invalid ssl-cipher in [client] makes the connection fail, the cipher even with ssl.mode = "disabled". So the client library ignores ssl-mode in option files, and ssl.mode decides the mode. With Connector/C 3.3.17, ssl-verify-server-cert=1 in the option file wins over ssl.mode = "required".

The same instance restarted without TLS (--skip-ssl) shows the fallback and the new check. Connector/C 3.3.17: NULL, "disabled", "preferred", and client.flag = CLIENT_SSL connect unencrypted; "required" fails with "ssl.mode = "required" requires TLS, but the connection is not encrypted."; "verify_identity" fails with "SSL is required, but the server does not support it". libmysqlclient 8.0.46: "required", "verify_identity", and CLIENT_SSL fail with "SSL is required but the server doesn't support it".

Tests

tests/testthat/test-ssl-mode.R has three tests of argument validation that need no server, and six tests that skip unless RMARIADB_TLS_TEST_HOST names a TLS server with require_secure_transport = ON (optional RMARIADB_TLS_TEST_PORT, _USER, _PASSWORD; RMARIADB_TLS_TEST_CA for the verification tests). Results for test-ssl-mode.R: Connector/C 3.3.17 12 passed; libmysqlclient 8.0.46 13 passed, 0 skipped; Connector/C 3.4.5 11 passed, 1 skipped (verification without CA on 127.0.0.1, see above); without the environment variable 6 passed, 6 skipped. The full suite on Connector/C 3.3 has 3 failures in test-timezone.R, which need named time zones that the local test server lacks; they are unrelated.

Not tested: the MySQL 26.x client library and MySQL 5.7 or older clients (rejected with an error by design), Windows and macOS builds, and a real MySQL server. CI does not run the TLS tests, since it has no TLS server configured.

Open questions

  • "verify_ca" is an error on Connector/C. Mapping it to "verify_identity" (stricter) would be an alternative, as would MYSQL_OPT_SSL_ENFORCE with ssl.ca, which in Connector/C 3.3 verifies the chain only but in 3.4 also checks the host name.
  • With Connector/C, "required" detects a missing TLS layer only after authentication, so the authentication exchange (and a cleartext password, if the server asks for mysql_clear_password) has already happened unencrypted. libmysqlclient refuses before authenticating, as does Connector/C with "verify_identity". The check stays; the ssl.mode documentation and the error message now say this and point to "verify_identity" with ssl.ca.
  • With Connector/C, TLS settings from an option file (read for the default group = "rs-dbi") are applied when connecting and win over ssl.mode. libmysqlclient 8.0 ignores ssl-mode in option files but applies ssl-ca and ssl-cipher. The documentation describes both.
  • Without ssl.ca, Connector/C 3.4 and later accept an untrusted certificate with "verify_identity" for the hosts 127.0.0.1 and ::1. The documentation mentions it, and the test that expects a failure skips in that case.
  • Should CLIENT_SSL alone keep its current meaning, or be translated to ssl.mode = "required" on Connector/C as well? This PR leaves it unchanged.

Closes #341, closes #631.

https://claude.ai/code/session_01Hr1rd43pnoHuAnGBgMih89


Generated by Claude Code

…verification

The new argument takes "disabled", "preferred", "required", "verify_ca", or "verify_identity",
modeled after the `--ssl-mode` option of the `mysql` command-line client.
The default `NULL` keeps the behavior of the client library.

With MariaDB Connector/C, the mode sets MYSQL_OPT_SSL_ENFORCE and MYSQL_OPT_SSL_VERIFY_SERVER_CERT.
Connector/C cannot verify the certificate chain without the host name,
so "verify_ca" is an error there.
Connector/C falls back to an unencrypted connection if the server offers no TLS and verification is off,
so "required" checks the cipher after connecting.
With the MySQL client library 8.0 and later, the mode sets MYSQL_OPT_SSL_MODE.
Other client libraries reject any mode.

`ssl.mode` cannot be combined with the CLIENT_SSL flags or, for "disabled", with the other `ssl.*` arguments.
The hint for a certificate error now points to `ssl.ca` and `ssl.mode = "required"`.

Closes #341, closes #631.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd43pnoHuAnGBgMih89

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Resolved: ssl.mode = "verify_ca" on MariaDB Connector/C

MariaDB Connector/C has no option that checks the certificate authority without also checking the host name: MYSQL_OPT_SSL_VERIFY_SERVER_CERT does both. libmysqlclient supports verify_ca directly. The PR currently errors on Connector/C for verify_ca. Mapping it to TLS enforcement plus ssl.ca would check only the CA on Connector/C 3.3 but also the host name on 3.4, so the meaning would depend on the library version. Mapping it to verify_identity would be stricter than asked. If nobody decides, the error stays.

  • Keep the error on Connector/C, with a message that points to verify_identity (recommended)
  • Map verify_ca to verify_identity on Connector/C, with a warning
  • Map verify_ca to enforcement plus ssl.ca, accepting the version-dependent meaning

Generated by Claude Code

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Resolved: Credentials before the TLS check with ssl.mode = "required" on Connector/C

With TLS enforcement but without peer verification, MariaDB Connector/C silently falls back to plaintext when the server offers no TLS. The PR therefore checks after connecting that the connection is encrypted, and errors otherwise. By then, the login has already happened, so the password exchange went over plaintext; whether that exposes the password depends on the authentication plugin (mysql_native_password sends a hash of a challenge, caching_sha2_password and ed25519 differ). libmysqlclient refuses before logging in. Requiring verification would close the gap but would reintroduce the self-signed-certificate failure of #631. If nobody decides, the post-connect check stays and the documentation states the limitation.

  • Keep the post-connect check and document the limitation for Connector/C (recommended)
  • Make required on Connector/C also verify the server certificate, so that it fails before login
  • Drop required on Connector/C and offer only verify_identity there

Generated by Claude Code

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Resolved: Option files overriding ssl.mode on Connector/C

On MariaDB Connector/C, TLS settings in an option file override ssl.mode, because the group (default rs-dbi) is read after the options are set. With libmysqlclient 8.0, further tests showed that an ssl-mode line in an option file has no effect at all (tried VERIFY_IDENTITY and DISABLED, under [client] and [rs-dbi]), while ssl-ca and ssl-cipher from the file do apply. The PR documents both behaviors. Making ssl.mode win on Connector/C means setting the options again after reading the option file, which changes when the file is read and may affect other options. If nobody decides, the documented difference stays.

  • Keep the behavior and the documentation (recommended)
  • Re-apply ssl.mode after the option file is read, so that it wins on every library

Generated by Claude Code

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Resolved: Should CLIENT_SSL alone mean "required" on Connector/C?

client.flag = CLIENT_SSL means "TLS required" with libmysqlclient, but on MariaDB Connector/C it encrypts while keeping the library default for verification and can fall back to plaintext. The documentation recommended CLIENT_SSL to enforce TLS, which #631 found does not help with Connector/C 3.4. The PR leaves CLIENT_SSL unchanged, fixes its documentation, and makes combining it with ssl.mode an error. Changing it would alter behavior for existing users of the flag. If nobody decides, it stays unchanged.

  • Leave CLIENT_SSL as it is and point to ssl.mode in the docs (recommended)
  • Treat CLIENT_SSL as ssl.mode = "required" on Connector/C

Generated by Claude Code

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Resolved: The hint on TLS error 2026

The hint added on main for connect error 2026 (TLS) suggested setting MARIADB_TLS_DISABLE_PEER_VERIFICATION=1, which turns off verification process-wide through an environment variable. The PR changes the hint to point to ssl.ca (to trust the server's CA) or ssl.mode = "required" (to encrypt without verification) instead. If nobody decides, the new hint ships with the PR.

  • Point to ssl.ca and ssl.mode = "required" only (recommended)
  • Also keep mentioning MARIADB_TLS_DISABLE_PEER_VERIFICATION

Generated by Claude Code

With MariaDB Connector/C, `ssl.mode = "required"` detects a missing TLS layer only after authentication.
The documentation and the error message now say so and point to `"verify_identity"` with `ssl.ca`.
The documentation also says that Connector/C 3.4 accepts an untrusted certificate for 127.0.0.1 and ::1 without a CA,
and that the MySQL client library 8.0 ignores `ssl-mode` in an option file but applies `ssl-ca` and `ssl-cipher`.
The tests compare client versions with `package_version()`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd43pnoHuAnGBgMih89
@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 18:24

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Resolved: Name and values of the new argument, compared with RPostgres

RPostgres has no TLS code of its own: dbConnect(Postgres(), sslmode = "require") passes the keyword through ... to libpq, which accepts disable, allow, prefer (the default), require, verify-ca and verify-full, and refuses before authentication in every mode. RMariaDB cannot pass keywords through, because MariaDB Connector/C and libmysqlclient take TLS settings as different mysql_options() calls, so #650 translates one argument per library. It uses MySQL's own names (--ssl-mode: DISABLED, PREFERRED, REQUIRED, VERIFY_CA, VERIFY_IDENTITY) in lower case, as ssl.mode, next to the existing ssl.key, ssl.cert, ssl.ca, ssl.capath and ssl.cipher. The reporter of #341 tried sslmode = 'REQUIRED', which today lands in ... (documented as unused) and is silently ignored.

Aligning names with RPostgres would mean libpq spellings for MySQL concepts that do not map one to one (allow has no MySQL counterpart, and verify-full is MySQL's VERIFY_IDENTITY). Erroring or warning on unknown arguments in ... would have caught the #341 mistake but may break callers that pass extra arguments today. If nobody decides, #650 keeps ssl.mode with MySQL's values and ... stays silent.

  • Keep ssl.mode with MySQL's values; warn on unknown arguments in ... in a separate PR (recommended)
  • Keep ssl.mode with MySQL's values and leave ... as it is
  • Also accept sslmode with libpq's values as an alias, for parity with RPostgres

Generated by Claude Code

@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Responded. Anything else to do? Prepare very brief responses to the issues, each in a separate comment here.

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Draft reply for #341:

Thanks, and sorry this took so long. sslmode is not an argument of dbConnect(), so it was silently ignored. #650 adds ssl.mode, with the values of the mysql client's --ssl-mode: dbConnect(RMariaDB::MariaDB(), ..., ssl.mode = "required") connects over TLS to a server that has require_secure_transport=ON. Once #650 is merged, you can try it with pak::pak("r-dbi/RMariaDB").


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Draft reply for #631:

Thanks for the report. The error comes from MariaDB Connector/C 3.4, which checks the server certificate by default, and the server here uses a self-signed one. CLIENT_SSL does not change that check, and the documentation was wrong to suggest it. #650 adds ssl.mode: ssl.mode = "required" encrypts without checking the certificate, like your option file. To check it, pass the CA with ssl.ca = "path/to/ca.pem" and use ssl.mode = "verify_identity". Once #650 is merged, you can try it with pak::pak("r-dbi/RMariaDB").


Generated by Claude Code

@krlmlr
krlmlr merged commit 402fe3f into main Nov 29, 2026
9 checks passed
@krlmlr
krlmlr deleted the claude/tls-mode branch November 29, 2026 18:59
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.

SSL connection discrepancies in dbConnect() function SSL connection to MySQL

2 participants