fix(ext/node): fix TLS client certificate authentication verification#33576
Conversation
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
left a comment
There was a problem hiding this comment.
LGTM. Five coordinated changes all map to specific Node/OpenSSL behavior gaps:
-
PEM header normalization —
normalize_pem_headersrewritesTRUSTED CERTIFICATEandX509 CERTIFICATEtoCERTIFICATEbeforerustls_pemfile::certsparses. Fast-path withwindows(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 bothbuild_client_configandbuild_server_config. -
X.509v1 chain verification via DER walking — replaces the prior "blindly accept v1 certs (skips chain check entirely)" workaround. The new
verify_chain_structurewalks issuer→subject DER name matches up the chain and returns the right Node code (UNABLE_TO_GET_ISSUER_CERT_LOCALLYif no explicit CA was provided,UNABLE_TO_VERIFY_LEAF_SIGNATUREif it was) when the chain doesn't reach a trusted root. Iteration capintermediates.len() + 2defends against cycles. Theder_read_elementwalker 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. -
NodeClientCertVerifier::verify_client_certalways succeeds — stores the error in a sharedverify_error: VerifyErrorStoreand lets the TLS handshake complete. JSonServerSocketSecurethen drives rejection viasocket.destroy(), producing client-side ECONNRESET instead of a TLS fatal alert. Matches Node/OpenSSL exactly. The store is shared withTLSWrapInner.verify_errorviainner.verify_error = client_cert_verify_errorsoverifyError()on the server-side TLSWrap returns the right code. -
CertificateRequiredalert mapping with TLS-version branching —protocol_version()is now threaded intorustls_error_to_node_error. TLS 1.2 rustls→OpenSSL getsSSLV3_ALERT_HANDSHAKE_FAILURE(matching OpenSSL's TLS 1.2 alert numbering), TLS 1.3 getsTLSV13_ALERT_CERTIFICATE_REQUIRED. -
Error code accuracy —
UNABLE_TO_VERIFY_LEAF_SIGNATUREvsUNABLE_TO_GET_ISSUER_CERT_LOCALLYdistinction 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.
Summary
Fixes several issues in the
node:tlsclient certificate authentication flow:BEGIN TRUSTED CERTIFICATEandBEGIN X509 CERTIFICATEheaders are now normalized toBEGIN CERTIFICATEbefore parsing (both client and server configs), matching OpenSSL behaviorNodeClientCertVerifier::verify_client_certnow always succeeds the TLS handshake, storing errors for the JS layer (onServerSocketSecure) to handle rejection viathis.destroy(), producingECONNRESETon the client instead of a TLS fatal alert, matching Node.js behaviorCertificateRequiredalert mapping with version-aware logic (TLS 1.2 maps toSSLV3_ALERT_HANDSHAKE_FAILUREto match OpenSSL)UNABLE_TO_GET_ISSUER_CERT_LOCALLYvsUNABLE_TO_VERIFY_LEAF_SIGNATUREdistinction based on whether explicit CAs were providedTest plan
./x test-compat test-tls-client-auth.jspasses (was failing)./x test-compat test-tls-client-- same pass/fail set as before (no regressions), plustest-tls-client-auth.jsnow passing./tools/lint.jspasses./tools/format.jspasses