Skip to content

tls: skip the parse and the DER copy of a bundled root in a ca list - #49387

Open
robobun wants to merge 4 commits into
mainfrom
robobun/a885ff5a/skip-parse-of-bundled-root-ca
Open

robobun wants to merge 4 commits into
mainfrom
robobun/a885ff5a/skip-parse-of-bundled-root-ca

Conversation

@robobun

@robobun robobun commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • tls.createSecureContext({ ca: [...tls.rootCertificates, extra] }) costs 12.97 M instructions (774 µs). fetch through an HTTP proxy builds one per CONNECT tunnel.
  • us_collect_ca_pem (packages/bun-usockets/src/crypto/openssl.cpp:856) parses each certificate to validate it, then drops the result. It also copies each DER: 127 KB of a live context's 177 KB.

Fix

Background

Downsides

  • A list with no bundled root gains nothing. Each certificate pays one lookup: 59 to 257 instructions beside a 108 K parse.
  • The first ca context of a process builds a table: 13,858 instructions, 10 KB.
  • The binary grows by 4,147 bytes.
Notes

What changed

  • us_find_bundled_root_cert() (root_certs.cpp) returns the embedded copy of the bundled root with exactly the given bytes, or NULL. It keeps the 118 roots in the order of their lengths, with the index of the first root of each length. A certificate of another length is a miss with no read of its bytes. The table is built on the first call.
  • us_collect_ca_pem asks it for each certificate. A hit becomes a CRYPTO_BUFFER over the embedded bytes (CRYPTO_BUFFER_new_from_static_data_unsafe, as X509_LAZY_CERT_SET_new_static does for the default store). A miss is copied and parsed as before.
  • The subject index of the bundled roots (us_get_bundled_root_cert_set) has a std::call_once of its own. Before, each reader of the DER table built it. So the first read of tls.rootCertificates no longer builds it: 2.00 M to 0.21 M instructions.
  • bun:internal-for-testing exports sslCaCertsParsed(), beside sslCtxLiveCount(). It counts the certificates that a ca option parsed.

The first two commits can be read one at a time. The first is the skip. The second is the reference to the embedded bytes. The two after them answer review notes: two tests run concurrently, and one comment is shorter.

Why the result is the same

X509_parse_from_buffer is a function of the bytes. The test rootCertificates are this checkout's root_certs.der, in order builds an X509Certificate from each bundled root, so a root that does not parse fails CI. An encrypted CERTIFICATE block is compared after it is decrypted. The lookup compares all the bytes. It does not read the CA store mode (--use-system-ca, --use-openssl-ca): those flags change what the default store holds, not which bytes parse.

What it reaches

Each ca option of each TLS context goes through us_collect_ca_pem: node:tls, Bun.connect, Bun.listen, Bun.serve, fetch and its CONNECT tunnel, WebSocket, Bun.sql. A context with no ca after tls.setDefaultCACertificates() goes through it too, because Bun gives that list to the context as its ca (src/js/node/tls.ts:543).

Numbers

Linux x64, release builds of 98eae32 (main), 8870ab4 (first commit) and d173c07 (both commits). NODE_EXTRA_CA_CERTS is not set.

User instructions for one tls.createSecureContext(), from perf_event_open over the whole process. Each value is the difference of two process runs, and the median of 5 such values. The script builds only the list under test.

list of CA certificates main first commit both commits Node 26.3
ca: 1 certificate, not bundled 153,674 153,758 153,706 1,641,937
ca: 118 bundled roots 12,815,355 4,219,556 4,231,229 53,297,626
ca: 118 bundled roots and 1 certificate 12,969,712 4,350,257 4,485,726 53,731,051
ca: the same in one string 12,385,034 3,794,320 3,730,438 52,230,824
ca: 59 bundled roots, 59 others 12,726,599 8,418,624 8,397,086 53,365,390
ca: 118 certificates, none bundled 12,760,577 12,767,383 12,767,501 53,464,519
ca: 118 bundled roots, each with 1 byte changed 12,757,268 12,788,156 12,787,571 53,438,767
ca: /etc/ssl/certs/ca-certificates.crt as one string (151 certificates) 15,817,861 7,940,863 7,899,900 64,239,667
no ca, after tls.setDefaultCACertificates() of 118 bundled roots and 1 certificate 12,877,533 4,287,884 4,229,898 1,189,309
  • The two rows with none bundled are the cost of the lookup. A certificate of another length than every root pays 59 instructions. A root with its last byte changed is the worst case: the lookup reads all of it, 257 instructions.
  • The last row is the one where Node leads. Node keeps one shared store for the override. Bun builds the list into each context.
  • The second commit moves a row by -64 K to +135 K instructions. With no copy to keep it in use, the page of a temporary DER buffer can become empty at each free, and mimalloc then frees the page and takes one again. Whether that happens depends on what else is on the heap. Wall time drops in each row all the same, because the context no longer writes 127 KB of new memory.

Wall time for the same call in µs. Each process run reports the best of 5 rounds of 200 calls. The table has the minimum and the median of 9 interleaved process runs.

list of CA certificates main main again first commit both commits Bun 1.4.3 Node 26.3
ca: 118 bundled roots and 1 certificate 762.6 / 774.4 767.8 / 774.4 346.4 / 350.8 294.9 / 295.8 781.9 3039.1
ca: 118 bundled roots 755.3 / 765.1 762.5 / 766.5 336.1 / 338.1 298.9 / 301.5
ca: the same in one string 723.6 / 735.5 726.1 / 734.3 308.0 / 313.7 262.0 / 265.0
ca: 118 certificates, none bundled 751.9 / 761.8 748.6 / 756.5 761.3 / 766.8 751.3 / 759.7 768.7 3079.6
ca: 118 bundled roots, each with 1 byte changed 748.1 / 755.3 750.1 / 755.6 763.3 / 770.0 760.0 / 763.6
no ca, after tls.setDefaultCACertificates() 754.9 / 771.6 761.5 / 767.8 337.1 / 341.5 287.2 / 290.2 778.5 77.0
ca: 1 certificate 9.3 / 9.5 9.2 / 9.4 9.2 / 9.5 9.1 / 9.4 9.7 100.5

The Bun 1.4.3 and Node columns are medians of 5 runs from a second batch. The worst case for the lookup costs 8 µs (1.1%): it reads each embedded root in full.

bench/snippets/tls-ca.mjs (new) has the same rows. Median of 3 alternating runs, µs per call:

main this PR Node 26.3
ca: 1 certificate 9.65 9.15 106.67
ca: 118 bundled roots 757.23 278.74 2980
ca: 118 bundled roots, 1 string 728.01 259.59 2800
ca: 118 bundled roots + 1 certificate 764.67 288.58 3010
ca: half bundled roots, half not 765.60 535.02 2970
ca: 118 certificates 767.97 771.00 2960
ca: 118 roots but for 1 byte 768.78 782.09 2940
ca: the system bundle 945.74 510.26 3460
setDefaultCACertificates: 118 bundled roots + 1 777.48 293.37 79.00

fetch(url, { proxy, tls: { ca }, keepalive: false }) through an HTTP proxy, 119 certificates, a new CONNECT tunnel for each request:

main both commits
instructions per request 14,302,999 5,690,098
µs per request, median of 9 interleaved runs 1558.1 (again: 1575.8) 1127.7
instructions per request with 1 certificate 2,033,950 2,034,618
instructions per pooled direct request, 119 certificates 521,333 521,336

Memory. RSS that 64 more live contexts of the 118 roots add, after two warm-up rounds: 11.06 to 11.09 MiB on main, 11.34 MiB with the first commit, 2.57 MiB with both (177 KB to 41 KB per context). Under ASAN: 14.3 to 14.9 MiB, then 3.6 to 4.0 MiB.

One use in a new process, total instructions, median of 11 runs:

main both commits
a context with no ca (builds the default store) 33,061,422 33,074,841
the same with SSL_CERT_FILE set to a bundle of 151 certificates 35,765,837 35,764,272
a context with 1 certificate that is not bundled 31,701,576 31,721,357
a context with the 118 bundled roots and 1 certificate 47,224,123 37,024,614

The 11 runs of one row span about 30 K to 50 K, so the first two rows show no change. The third has the table: a harness that runs the same code on root_certs.der gives 13,858 instructions for it. The table is 1.9 KB for the roots and 8 KB for the index of lengths.

Size: text 155,774,875 to 155,779,022 bytes.

Tests

test/js/node/tls/node-tls-internals.test.ts, the block bundled roots in a ca option. On release builds: main 3 pass and 2 fail, the first commit 4 pass and 1 fail, both commits 5 pass.

change to the code tests that fail
no skip parse count, memory
first commit only memory
a lookup that compares lengths only parse count, chain, trust, server names
a hit that returns another root of the same length chain
  • only the certificates that are not bundled roots are parsed reads sslCaCertsParsed() in a new process for 10 lists, and for a context with no ca after tls.setDefaultCACertificates(). It also pins which lists a context refuses. A root with a public key that does not parse is refused by the parse alone: the store's own index accepts it.
  • a context holds no copy of the DER of a bundled root bounds the RSS of 64 contexts (6 MiB, 8 MiB under ASAN).
  • a chain that names a bundled root finds that root and no other serves a certificate that names a bundled root as its issuer. A client whose ca has that root gets CERT_SIGNATURE_FAILURE. A client whose ca has another root of the same length gets UNABLE_TO_VERIFY_LEAF_SIGNATURE. Node 26.3 gives the same verdicts. The issuer is the RSA root that expires last among those that share a length with another root (2048 today).
  • the other certificates of the list are trusted and a server names a bundled root to its clients pin results and pass on main.

Also run on the debug build with ASAN: all 29 files of test/js/node/tls/, test/js/web/fetch/fetch.tls.test.ts, test/js/bun/http/proxy.test.ts, two proxy-stress files, test/js/web/websocket/websocket-proxy.test.ts, test/js/node/crypto/x509.test.ts, test/js/bun/http/bun-serve-ssl.test.ts, test/js/node/quic/quic-sni.test.ts, test/js/bun/net/socket.test.ts, and 53 Node tests (test-tls-*ca*, *root*, *cert*, test-crypto-x509). Seven tests fail, and the same seven fail on a release build of main on this machine: four in node-tls-connect.test.ts that need the network, one in node-tls-server.test.ts, one in fetch.tls.test.ts and one in socket.test.ts. The new block is clean under LeakSanitizer with the options of the CI runner.

Not in this PR

Other open PRs

A file by file merge of this branch with the head of #46528 (36f10f4) and with the head of #49360 (c48e652) has no conflict, so they can land in either order. #include <algorithm> is after <vector> for that reason.

In #46528 the list of tls.setDefaultCACertificates() becomes the ca of each native client too (ca_is_default), so each of those contexts passes it through us_collect_ca_pem and gets this skip. The certificates of that list that are not bundled roots are still parsed again for each context.

A context built from a `ca` option parsed every certificate of the list whole
to know that it parses, and dropped the result. A certificate whose DER is a
bundled root is known to parse, so it skips that parse.

- us_find_bundled_root_cert() finds a bundled root by its bytes, in a table
  sorted by length and then by bytes.
- The subject index of the bundled roots has a once of its own, so a reader of
  their DER does not build it.
- bun:internal-for-testing counts the certificates that a `ca` option parsed.
…`ca`

A context kept a heap copy of the DER of each certificate of its `ca` option.
For a bundled root it now refers to the bytes that the binary embeds, as the
default store does. That is 127 KB less for each context whose `ca` holds
every bundled root.
@robobun

robobun commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 8:50 AM PT - Nov 21st, 2026

✅ @robobun, your commit 61ba059110506fdbcc96ad289e6694bf7d7b8f25 passed in Build #143467! 🎉


🧪   To try this PR locally:

bunx bun-pr 49387

That installs a local version of the PR into your bun-49387 executable, so you can run:

bun-49387 --bun

@robobun

robobun commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator Author

Status: ready for review. CI runs.

How to see the cost: run bun bench/snippets/tls-ca.mjs in bench/ (after bun install there). On main, the row ca: 118 bundled roots + 1 certificate takes about 765 µs for each tls.createSecureContext(). With this PR it takes about 290 µs.

Test: bun bd test test/js/node/tls/node-tls-internals.test.ts -t "bundled roots in a ca option". On main, two of the five tests fail: the count of parsed certificates and the memory bound.

@claude claude Bot 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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline nit, I also checked the lookup table in us_find_bundled_root_cert (the first[] index is filled in reverse so it lands on the lowest index per length, and a length no root has falls through to an empty loop), the ownership of the static-data CRYPTO_BUFFER through sk_CRYPTO_BUFFER_sort_and_dedup/CRYPTO_BUFFER_free and the lazy set, and the caCertsParsed binding chain against the existing liveCount precedent — all consistent. Not run here: the build, so the presence of CRYPTO_BUFFER_new_from_static_data_unsafe in the vendored BoringSSL fork (not in this checkout) rests on CI.

Extended reasoning...

The change touches the TLS trust-store construction path in packages/bun-usockets (openssl.cpp, root_certs.cpp) so that a ca certificate byte-identical to a bundled Mozilla root is neither re-parsed nor copied, plus a test-only counter exposed through bun:internal-for-testing and ~300 lines of new tests. The security-relevant surface is which certificates a context trusts; the lookup requires an exact full-DER match, so the set of trusted bytes is unchanged and a near-miss (one byte changed, trailing byte, wrong tag) still takes the parse-and-validate path, which the new subprocess test asserts. The only surviving finding is a test.concurrent nit, and the ruled-out candidates (pointer into the binary, static destruction at shutdown, GC size estimate, atomic counter cost) are either pre-existing patterns or negligible.

Comment thread src/runtime/api/bun/SecureContext.rs Resolved
@robobun

robobun commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator Author

Both review notes are applied.

  • c2c7dff: the two tests that spawn a process are test.concurrent. The three socket tests share the servers array and its afterEach, so they stay serial.
  • 61ba059: the comment on ca_certs_parsed() in src/runtime/api/bun/SecureContext.rs is one line.

On CRYPTO_BUFFER_new_from_static_data_unsafe: the BoringSSL that the build fetches declares it in include/openssl/pool.h, and X509_LAZY_CERT_SET_new_static already calls it for the default store. The debug and the release builds of this branch link.

@claude claude Bot 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.

Code review completed

Nothing new to post: everything this review found is already covered by existing comments on this pull request or didn't merit a separate one.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants