Skip to content

fix(ext/node): fix TLS client certificate authentication verification#33576

Merged
bartlomieju merged 1 commit into
mainfrom
fix/tls-client-auth
Apr 28, 2026
Merged

fix(ext/node): fix TLS client certificate authentication verification#33576
bartlomieju merged 1 commit into
mainfrom
fix/tls-client-auth

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

Summary

Fixes several issues in the node:tls client certificate authentication flow:

  • PEM header normalization: BEGIN TRUSTED CERTIFICATE and BEGIN X509 CERTIFICATE headers are now normalized to BEGIN CERTIFICATE before parsing (both client and server configs), matching OpenSSL behavior
  • X.509v1 chain verification: Instead of blindly accepting v1 certs (which bypassed all chain verification), we now do structural chain checking via issuer/subject DER matching -- valid chains pass, broken chains produce the correct Node/OpenSSL error codes
  • Client cert rejection flow: NodeClientCertVerifier::verify_client_cert now always succeeds the TLS handshake, storing errors for the JS layer (onServerSocketSecure) to handle rejection via this.destroy(), producing ECONNRESET on the client instead of a TLS fatal alert, matching Node.js behavior
  • TLS alert mapping: Added CertificateRequired alert mapping with version-aware logic (TLS 1.2 maps to SSLV3_ALERT_HANDSHAKE_FAILURE to match OpenSSL)
  • Error code accuracy: Fixed UNABLE_TO_GET_ISSUER_CERT_LOCALLY vs UNABLE_TO_VERIFY_LEAF_SIGNATURE distinction based on whether explicit CAs were provided

Test plan

  • ./x test-compat test-tls-client-auth.js passes (was failing)
  • ./x test-compat test-tls-client -- same pass/fail set as before (no regressions), plus test-tls-client-auth.js now passing
  • ./tools/lint.js passes
  • ./tools/format.js passes

Fixes several issues in the node:tls client certificate authentication
flow so that `test-tls-client-auth.js` passes:

- Normalize `BEGIN TRUSTED CERTIFICATE` and `BEGIN X509 CERTIFICATE`
  PEM headers to `BEGIN CERTIFICATE` before passing to rustls_pemfile,
  matching OpenSSL behavior.

- Replace blanket acceptance of X.509v1 certs (UnsupportedCertVersion)
  with structural chain verification via issuer/subject DER matching,
  so v1 certs with valid chains succeed while broken chains produce
  the correct Node/OpenSSL error codes.

- Make NodeClientCertVerifier store errors and always succeed the TLS
  handshake, letting the JS layer (onServerSocketSecure) handle client
  cert rejection. This produces ECONNRESET on the client instead of a
  TLS fatal alert, matching Node.js behavior.

- Add CertificateRequired alert mapping with TLS-version awareness
  (TLS 1.2 maps to SSLV3_ALERT_HANDSHAKE_FAILURE to match OpenSSL).

- Fix UNABLE_TO_GET_ISSUER_CERT_LOCALLY vs UNABLE_TO_VERIFY_LEAF_SIGNATURE
  distinction based on whether explicit CAs were provided.

@fibibot fibibot 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.

LGTM. Five coordinated changes all map to specific Node/OpenSSL behavior gaps:

  1. PEM header normalizationnormalize_pem_headers rewrites TRUSTED CERTIFICATE and X509 CERTIFICATE to CERTIFICATE before rustls_pemfile::certs parses. Fast-path with windows(N).any(...) avoids allocation when no rewrite is needed. PEM body is base64 (no spaces) so the substrings can't appear inside the data — the textual search is safe. Applied symmetrically in both build_client_config and build_server_config.

  2. X.509v1 chain verification via DER walking — replaces the prior "blindly accept v1 certs (skips chain check entirely)" workaround. The new verify_chain_structure walks issuer→subject DER name matches up the chain and returns the right Node code (UNABLE_TO_GET_ISSUER_CERT_LOCALLY if no explicit CA was provided, UNABLE_TO_VERIFY_LEAF_SIGNATURE if it was) when the chain doesn't reach a trusted root. Iteration cap intermediates.len() + 2 defends against cycles. The der_read_element walker correctly handles short-form (<0x80) and long-form (multi-byte) length octets, rejects num_bytes=0 and num_bytes>4, and validates input bounds before slicing.

  3. NodeClientCertVerifier::verify_client_cert always succeeds — stores the error in a shared verify_error: VerifyErrorStore and lets the TLS handshake complete. JS onServerSocketSecure then drives rejection via socket.destroy(), producing client-side ECONNRESET instead of a TLS fatal alert. Matches Node/OpenSSL exactly. The store is shared with TLSWrapInner.verify_error via inner.verify_error = client_cert_verify_error so verifyError() on the server-side TLSWrap returns the right code.

  4. CertificateRequired alert mapping with TLS-version branchingprotocol_version() is now threaded into rustls_error_to_node_error. TLS 1.2 rustls→OpenSSL gets SSLV3_ALERT_HANDSHAKE_FAILURE (matching OpenSSL's TLS 1.2 alert numbering), TLS 1.3 gets TLSV13_ALERT_CERTIFICATE_REQUIRED.

  5. Error code accuracyUNABLE_TO_VERIFY_LEAF_SIGNATURE vs UNABLE_TO_GET_ISSUER_CERT_LOCALLY distinction now keyed on whether explicit CAs were provided, matching OpenSSL's reporting.

Test enrollment alphabetically positioned correctly (client-abort2 < client-auth < client-renegotiation-limit).

CI fully green: 110/110 checks pass.

One observation worth keeping in mind (not blocking): the structural chain check in (2) doesn't verify signatures, validity dates, key usage, or path constraints — it's a graceful degradation for v1 certs that webpki refuses to parse. The comment header acknowledges this. For v3 certs the normal webpki path runs first and only falls through to the structural check on the specific UnsupportedCertVersion / BadEncoding errors, so this only relaxes verification for certs OpenSSL would have accepted with full v1 fallback semantics anyway.

@bartlomieju
bartlomieju merged commit 5dca210 into main Apr 28, 2026
112 checks passed
@bartlomieju
bartlomieju deleted the fix/tls-client-auth branch April 28, 2026 09:07
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.

2 participants