Skip to content
Memory corruption Disclosed report Advanced

mbedTLS private-key blob null-termination asymmetry in lib/vtls/mbedtls.c (mbed_load_privkey)

The private-key blob loading path in curl's mbedTLS backend passes a pointer and length straight to mbedTLS's key parser without ensuring the PEM data is null-terminated, unlike the certificate and CA blob paths in the same file. The author claims only an API contract violation reachable through public options, with possible undefined behaviour such as a parse failure or out-of-bounds read, and demonstrates no exploit.

shecantcode2 Follow

Originally disclosed on HackerOne

  • Disclosed after 25 hours

Summary:

In lib/vtls/mbedtls.c, function mbed_load_privkey (lines 653-738) passes raw ssl_key_blob->data and ssl_key_blob->len directly to mbedtls_pk_parse_key() at lines 706-708 (mbedTLS 4.x branch) and 718-722 (mbedTLS 3.x branch), without ensuring null-termination. The mbedTLS API contract for mbedtls_pk_parse_key() requires PEM input to be null-terminated and keylen to equal strlen(key) + 1. The adjacent mbed_load_cacert (lines 512-518) and mbed_load_clicert (lines 626-631) handle this correctly using curlx_memdup0() and passing len + 1, but the private-key path does not. The path is reachable through public options CURLOPT_SSLKEY_BLOB and CURLOPT_PROXY_SSLKEY_BLOB, which use struct curl_blob (pointer-plus-length, no null-termination guarantee). I am not claiming a demonstrated out-of-bounds read or proof-of-concept; the defensible claim is API contract violation in a public-option path with asymmetric handling versus cert/CA blobs in the same file. Suggested fix: mirror the cert/CA pattern by detecting non-null-terminated PEM, duplicating via curlx_memdup0(), and passing blob->len + 1.

Affected version

curl from git, commit 2c81cf620e559b6c448dcfd8ef389f214746b533 (master, May 2026). Backend: USE_MBEDTLS with PEM private-key parsing enabled in linked mbedTLS.

Steps To Reproduce:

This report is based on static source analysis; no runtime exploit is demonstrated. To verify the asymmetry:

  1. git clone https://github.com/curl/curl
  2. cd curl && git checkout 2c81cf620e559b6c448dcfd8ef389f214746b533
  3. Inspect lib/vtls/mbedtls.c lines 653-738 (mbed_load_privkey).
  4. Note that lines 706-708 and 718-722 call mbedtls_pk_parse_key with raw ssl_key_blob->data and ssl_key_blob->len, with no curlx_memdup0 or len + 1.
  5. Compare to lib/vtls/mbedtls.c lines 474-572 (mbed_load_cacert) and 574-651 (mbed_load_clicert), which check final byte, duplicate non-null-terminated PEM with curlx_memdup0, and pass len + 1.
  6. Confirm reachability via lib/setopt.c (CURLOPT_SSLKEY_BLOB at 2843-2847, CURLOPT_PROXY_SSLKEY_BLOB at 2822-2826) and lib/vtls/vtls.c (Curl_ssl_conn_config_update at 345 and 386) where blobs[BLOB_KEY] is assigned to sslc->key_blob.

Impact

Summary:

For non-null-terminated PEM private-key blob input, behavior is undefined per the mbedTLS API contract; possible outcomes include parse failure, out-of-bounds read, or other undefined behavior depending on linked mbedTLS internals. The path is reachable through public curl options. The asymmetry with cert/CA blob handling in the same file means curl currently enforces this contract for two of three blob types and does not for the third, creating inconsistent guarantees for application authors. I defer to the curl security team on whether this should be classified as a security issue or as a correctness/hardening fix.

The original report as published on HackerOne, by shecantcode2. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the private-key blob path omits the null-termination step that the certificate and CA blob paths perform, so a pointer-plus-length blob is passed to an API that expects a terminated string.

Impact

Behaviour is undefined for non-terminated key blobs, so outcomes could include a parse failure or an out-of-bounds read depending on the linked mbedTLS internals; no exploit was demonstrated.

What the attacker needed

Application passes a key blob via a public option; blob data is not null-terminated; curl built with the mbedTLS backend

The fix

The private-key path should mirror the certificate and CA handling by detecting non-terminated PEM, duplicating it and passing the length plus one; the text does not say it was fixed.

How it was found

  1. Clone the curl repository and check out the stated commit.
  2. Open lib/vtls/mbedtls.c and read the private-key loading function.
  3. Note the key parser is called with the raw blob pointer and length, with no duplication or length increment.
  4. Compare with the CA and client certificate loading functions, which check the last byte, duplicate the data and pass length plus one.
  5. Confirm the path is reachable from the public key-blob options via the setopt and vtls code.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When a program hands text to a library, the library sometimes expects the text to end with an invisible zero byte that marks where it stops. curl's certificate-loading code adds that byte, but its private-key-loading code does not, so the library may read past the end of the supplied data. The reporter found this by reading the source code and did not show it actually crashing or leaking anything.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More from curl

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.