Skip to content

rustls: consume CURLOPT_SSLCERT_BLOB / CURLOPT_SSLKEY_BLOB - #23196

Closed
ns-ylambert wants to merge 4 commits into
curl:masterfrom
ns-ylambert:fix-rustls-sslcert-blob
Closed

ns-ylambert wants to merge 4 commits into
curl:masterfrom
ns-ylambert:fix-rustls-sslcert-blob

Conversation

@ns-ylambert

@ns-ylambert ns-ylambert commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Problem

lib/vtls/rustls.c reads only the file-path client-cert fields (conn_config->clientcert, conn_config->key) via read_file_into(). The blob fields populated by CURLOPT_SSLCERT_BLOB / CURLOPT_SSLKEY_BLOB (and their PROXY_ variants) are never consulted. Callers that hand the client identity as in-memory blobs have their cert and key silently dropped, and the server sees an anonymous TLS connection instead of receiving a CertificateVerify message.

CURLOPT_CAINFO_BLOB is already handled in rustls.c (see cr_set_ssl_x509_store). CURLOPT_SSLCERT_BLOB / CURLOPT_SSLKEY_BLOB are the missing counterparts. openssl.c and schannel.c both consult cert_blob / key_blob before falling back to file paths.

Fix

At both call sites in init_config_builder_client_auth:

  • Check the blob field first (conn_config->cert_blob / conn_config->key_blob).
  • If set, copy its bytes into the dyn buffer via curlx_dyn_addn().
  • Fall back to read_file_into() otherwise.

Also extend the outer entry gate in cr_init_backend from

if(conn_config->clientcert || conn_config->key)

to

if(conn_config->clientcert || conn_config->cert_blob ||
   conn_config->key || conn_config->key_blob)

so init_config_builder_client_auth is reached when only blobs are set (no file paths).

The cert-without-key and key-without-cert validation is updated to check the presence of either form (path or blob) rather than file paths alone.

CURLOPT_SSLCERTTYPE / CURLOPT_SSLKEYTYPE (and their PROXY_ variants) are now validated: rustls-ffi only accepts PEM-encoded material, so any type other than NULL or "PEM" is rejected with a clear error instead of being silently ignored and failing later inside rustls_certified_key_build() with an opaque parse error. Same shape the other backends already apply (schannel.c rejects types other than "P12"; openssl.c fails on unsupported file types).

Out of scope

openssl.c has a convenience: when a cert source is set but no key source is given, it reuses the cert bytes/file as the key source. That fallback exists for legacy combined-PEM bundles and relies on the OpenSSL PEM parser tolerating mismatched blocks. It is not mirrored here: rustls-ffi is designed explicit-over-implicit, and callers with a combined PEM should split cert chain and key before calling curl.

Also out of scope, tracked as follow-ups:

  • GnuTLS ignores the blob options entirely (gtls.c does not consult cert_blob / key_blob); test2095 is gated with !GnuTLS until that is addressed.
  • mbedTLS fails key parsing for non-NUL-terminated blobs; fixed separately in mbedtls: pass correct length for PEM/DER key blobs to mbedtls_pk_parse_key #23274. test2095 is gated with !mbedtls until that fix lands.
  • The blob options are accepted without consulting the backend's capability report (no SSLSUPP_* bit exists for them), so a build whose backend ignores them accepts them silently. Adding a SSLSUPP_* gate in setopt.c is a cross-backend change and belongs in its own PR.

Test

tests/libtest/lib2095.c + tests/data/test2095: loads the CA cert, client certificate, and private key from disk into memory and passes them via CURLOPT_CAINFO_BLOB, CURLOPT_SSLCERT_BLOB, and CURLOPT_SSLKEY_BLOB to an https-mtls server. The test is skipped on Schannel builds and on any build where CURLOPT_SSLCERT_BLOB is absent. It is also gated off for GnuTLS (blob options ignored) and mbedTLS (fixed in #23274) for now. Coverage specific to the rustls backend comes from the AM rustls CI job.

Verification

Verified with a server requiring client certificate authentication, passing the certificate chain and private key via CURLOPT_SSLCERT_BLOB / CURLOPT_SSLKEY_BLOB with curl built against rustls-ffi. Pre-patch: the blobs are silently ignored, no client certificate is presented, and the server rejects the connection. Post-patch: the CertificateVerify handshake message is sent and the connection succeeds.

Also verified: a blob larger than the 100 KiB dyn-buffer limit fails with CURLE_TOO_LARGE, and CURLOPT_SSLCERTTYPE "DER" fails with a clear error instead of an opaque parse failure.

Related

@github-actions github-actions Bot added the TLS label Sep 30, 2026
@ns-ylambert
ns-ylambert force-pushed the fix-rustls-sslcert-blob branch 2 times, most recently from 1d09aee to 03a4139 Compare September 30, 2026 18:18
@testclutch

Copy link
Copy Markdown

Analysis of PR #23196 at 03a41392:

Test ../../tests/http/test_02_download.py::TestDownload::test_02_34_max_total_conns[False-0-h2] failed, but it has been 0.5% flaky lately, so it's probably NOT a fault of the PR. Note that this test has failed in 2 different CI jobs (the link just goes to one of them). Note that this CI job has had a number of other flaky tests recently (3, to be specific) so it may be that this failure is rather a systemic issue with this job and not with this specific PR.

Generated by Testclutch

Comment thread lib/vtls/rustls.c Resolved
@github-actions github-actions Bot added the tests label Oct 1, 2026
@ns-ylambert
ns-ylambert force-pushed the fix-rustls-sslcert-blob branch 3 times, most recently from 8d78947 to f407945 Compare October 1, 2026 07:11
@ns-ylambert

Copy link
Copy Markdown
Contributor Author

@vszakats Thanks for the review.

I added a test to exercise the CURLOPT_SSLCERT_BLOB / CURLOPT_SSLKEY_BLOB path in any backend that supports it. It could be scoped to the rustls only (the backend this PR fixes), but running it across all supporting backends gives broader coverage of the blob path.

@cpu

cpu commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

running it across all supporting backends gives broader coverage of the blob path.

+1, and will prevent the next new vTLS backend from overlooking wiring up the same functionality.

@cpu cpu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a closer look at this diff, mainly focused on the rustls-ffi API updates. Those look good to me, but I had some other general feedback to consider.

Firstly, I think there could be some docs updates to go with this? I think all of these would be candidates to be updated to add rustls to the TLS-Backend header data:

  • docs/libcurl/opts/CURLOPT_SSLCERT_BLOB.md:13
  • docs/libcurl/opts/CURLOPT_SSLKEY_BLOB.md:12
  • docs/libcurl/opts/CURLOPT_PROXY_SSLCERT_BLOB.md:14
  • docs/libcurl/opts/CURLOPT_PROXY_SSLKEY_BLOB.md:13

More generally, it feels like the behavior of Curl_setblobopt taking the option data, and then passing it to a backend that ignores it is a deeper anti-pattern that might be nice to fix irrespective of making rustls support the opt?

E.g. should this have a new capability bit that curl should be using with Curl_ssl_supports(...) to be able to understand that the backend actually supports the option? I did a quick scan and it seemed like the GnuTLS vTLS backend might be in the same state as rustls before this PR (though I'm also surprised the test isn't catching that if so).

Comment thread tests/data/test2095 Resolved
@ns-ylambert
ns-ylambert requested a review from vszakats October 2, 2026 18:57
Comment thread tests/data/test2095 Resolved
@ns-ylambert

ns-ylambert commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Summary of changes (head 0f9ff35015):

  • test2095: renamed to fit the 70-column limit, gated !Schannel !GnuTLS !mbedtls.
  • Docs: rustls added to the TLS-backend header of CURLOPT_SSLCERT_BLOB, CURLOPT_SSLKEY_BLOB and the two PROXY_ variants.

Gate rationale:

  • !GnuTLS: gtls.c ignores the cert/key blob fields entirely; handshake succeeds but no client cert is presented. Implementing blob consumption there is left as a follow-up.
  • !mbedtls: backend already consumes blobs but mbed_load_privkey() passes the key length without the NUL terminator to mbedtls_pk_parse_key() (-0x3D00 for every PEM key). One-line pre-existing bug, fixed in mbedtls: pass correct length for PEM/DER key blobs to mbedtls_pk_parse_key #23274; gate can be dropped once that lands.

Out of scope (follow-ups):

  • GnuTLS blob consumption.
  • CI guard against over-long test names: the original 93-char name tripped runtests.pl's 70-column check but CI stayed green (the test is silently dropped from the run).
  • @cpu suggestion:

More generally, it feels like the behavior of Curl_setblobopt taking the option data, and then passing it to a backend that ignores it is a deeper anti-pattern that might be nice to fix.

@ns-ylambert
ns-ylambert requested review from bagder and vszakats October 5, 2026 17:13
@cpu

cpu commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Most recent diff LGTM. Considering the other feedback as follow-ups seems OK. Thanks again for the patch.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The test precheck cannot detect unsupported TLS backends, error codes are misclassified, and the updated documentation is internally inconsistent.

Review effort: Balanced
Findings: 3 Medium severity · 5 Low severity

Open (8)
What changed in this PR

Adds Rustls support for in-memory client certificate and key blobs, including proxy TLS configuration.

Changes:

  • Loads certificate/key blobs before file paths in Rustls.
  • Adds an mTLS blob integration test.
  • Documents Rustls support for four blob options.
File Description
lib/​vtls/​rustls.c Consumes client certificate and key blobs.
tests/​libtest/​lib2095.c Adds blob-based mTLS test logic.
tests/​libtest/​Makefile.inc Registers the libtest source.
tests/​data/​test2095 Defines the mTLS test case.
tests/​data/​Makefile.am Registers test 2095.
docs/​libcurl/​opts/​CURLOPT_SSLKEY_BLOB.md Advertises Rustls support.
docs/​libcurl/​opts/​CURLOPT_SSLCERT_BLOB.md Advertises Rustls support.
docs/​libcurl/​opts/​CURLOPT_PROXY_SSLKEY_BLOB.md Advertises proxy-key blob support.
docs/​libcurl/​opts/​CURLOPT_PROXY_SSLCERT_BLOB.md Advertises proxy-certificate blob support.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread lib/vtls/rustls.c Resolved
Comment thread lib/vtls/rustls.c Resolved
Comment thread tests/libtest/lib2095.c Resolved
Comment thread tests/data/test2095 Resolved
…tls backend

`curl/lib/vtls/rustls.c` reads only the file-path client-cert fields
(`conn_config->clientcert`, `conn_config->key`) via `read_file_into()`.
The blob fields populated by `CURLOPT_SSLCERT_BLOB` / `CURLOPT_SSLKEY_BLOB` are
never consulted, so callers that hand the client identity as in-memory blobs
have their cert and key silently dropped and the server sees an anonymous TLS
connection.

At both call sites in `init_config_builder_client_auth`: check the blob field
first (`conn_config->cert_blob` / `conn_config->key_blob`), copy its bytes into
the dyn buffer if set, fall back to `read_file_into()` otherwise.
Also extend the outer entry gate at `cr_init_backend` so
`init_config_builder_client_auth` is reached when only the blob fields are set
(no file paths).

Same shape `openssl.c` and `schannel.c` already have: see how each consults
`ca_info_blob` / `cert_blob` / `key_blob` before falling back to file paths.
No new option is introduced; existing `CURLOPT_SSLCERT_BLOB` /
`CURLOPT_SSLKEY_BLOB` just start working on curl+rustls-ffi.

Scope excluded: `openssl.c`'s "reuse cert source as key source when no separate
key is set" convenience is not mirrored here. That fallback exists for legacy
combined-PEM bundles and relies on the backend's PEM parser being tolerant of
mismatched blocks; rustls-ffi is designed explicit-over-implicit and callers
with a combined PEM should split cert and key upstream.
Exercises the in-memory client-auth path added to the rustls backend.
Uses the https-mtls server and loads CA cert, client certificate, and
private key from disk into memory, then passes them as blobs via
CURLOPT_CAINFO_BLOB, CURLOPT_SSLCERT_BLOB, and CURLOPT_SSLKEY_BLOB.
Skipped on Schannel (native blobs not applicable) and on builds where
CURLOPT_SSLCERT_BLOB is absent.
`CURLOPT_SSLCERT_BLOB` / `CURLOPT_SSLKEY_BLOB` (and their `PROXY_` variants)
are now consumed by the rustls backend.
`init_config_builder_client_auth` never reads `conn_config->cert_type` /
`conn_config->key_type`, so `CURLOPT_SSLCERTTYPE` / `CURLOPT_SSLKEYTYPE`
set to anything but the default "PEM" were silently ignored. A "DER" or
"P12" blob failed with an opaque parse error out of
`rustls_certified_key_build` with no hint the type option was dropped.

Reject both options unless they are NULL or "PEM", matching the checks
`schannel.c` and `openssl.c` already apply: rustls-ffi only accepts PEM-encoded material
(`PrivateKeyDer::from_pem_slice` / `CertificateDer::pem_slice_iter`).
@ns-ylambert
ns-ylambert force-pushed the fix-rustls-sslcert-blob branch from 0f9ff35 to 544b73f Compare October 6, 2026 09:33
@ns-ylambert

ns-ylambert commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor Author

PR description updated, Copilot review's findings addressed.

The dl-mingw, CM 16.2.0-x86_64 schannel +analyzer job on the latest run looks like CI flake rather than a real failure. @vszakats @bagder could you kindly re-run it?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread tests/data/test2095
HTTPS with in-memory client auth using blob options
</name>
<command>
https://localhost:%HTTPS-MTLSPORT/%TESTNUMBER %CERTDIR/certs/test-ca.crt %CERTDIR/certs/test-client-cert.crt %CERTDIR/certs/test-client-cert.key

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
https://localhost:%HTTPS-MTLSPORT/%TESTNUMBER %CERTDIR/certs/test-ca.crt %CERTDIR/certs/test-client-cert.crt %CERTDIR/certs/test-client-cert.key
https://localhost:%HTTPS-MTLSPORT/%TESTNUMBER
%CERTDIR/certs/test-ca.crt
%CERTDIR/certs/test-client-cert.crt
%CERTDIR/certs/test-client-cert.key

perhaps folding the line like this makes it more readable
and avoids this long line?

PR LGTM with or without this change.

@vszakats vszakats left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With one (optional) style suggestion:
https://github.com/curl/curl/pull/23196/files#r4212402570

@bagder bagder closed this in bea2e5d Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

6 participants