Repository navigation
fix(ext/node): fix TLS peer certificate multi-value fields, issuer chain, and EC curve names - #33782
Conversation
…ain, and EC curve names - Support multi-value certificate fields (e.g. multiple OU entries) by using a StringOrArray type that serializes as a string for single values and an array for multiple values, matching Node.js behavior. - Include CA certs in the server's TLS handshake chain (OpenSSL auto-chain behavior) so getPeerCertificate(true) returns the full issuer chain. - Set issuerCertificate to itself for self-signed root CAs in the peer certificate chain, matching Node.js behavior. - Fix EC curve names: use OpenSSL short names for asn1Curve (e.g. prime256v1) and NIST names for nistCurve (e.g. P-256). - Enable test-tls-peer-certificate.js and test-tls-peer-certificate-multi-keys.js in node compat tests.
fibibot
left a comment
There was a problem hiding this comment.
Substance is right and tracks Node's actual behavior on all three fronts:
What's right
- Multi-value field handling via
StringOrArray(Vec<String>)with a hand-rolledSerializeimpl that emits a plain string forlen == 1and an array otherwise. That's the exact shape Node uses (peerCert.subject.OUis a string when there's one OU, an array when there are multiple). Switching allSubjectOrIssuerfields fromOption<String>toStringOrArrayand replacing everyresult.field = Some(value_str)withresult.field.push(value_str)is the minimal change that supports accumulation. StringOrArray::is_emptyforskip_serializing_if: keeps the same JSON-shape contract as the previousOption::is_noneguard (empty fields still don't appear in the serialized output).- CA chain inclusion (per the body): Node/OpenSSL auto-chain on the server side is a well-known gap that breaks
getPeerCertificate(true)against rustls; appendingcacerts to the server's cert chain inbuild_server_configis the right fix. - EC curve naming split:
asn1Curveuses OpenSSL short names (prime256v1),nistCurveuses NIST names (P-256). Node distinguishes these consistently; using the short OpenSSL names where the OID would be wrong is the correct mapping.
Real blocker: the in-tree unit tests don't compile
lint debug linux-x86_64, test libs debug macos-aarch64, and test libs debug linux-aarch64 all fail with the same two-error pattern at ext/node_crypto/x509.rs:1363–1364:
error[E0369]: binary operation `==` cannot be applied to type `StringOrArray`
--> ext/node_crypto/x509.rs:1363:5
1363| assert_eq!(result.cn, Some("CN".to_string()));
error[E0277]: `StringOrArray` doesn't implement `Debug`
The existing in-module tests still use the pre-change shape (Option<String> constructors and assertions) but the new type:
- Doesn't
#[derive(Debug, PartialEq)], soassert_eq!can't print or compare it. - Has changed type signature, so
Some("CN".to_string())is no longer a valid value forresult.cn.
Two-line fix:
-#[derive(Default)]
+#[derive(Default, Debug, PartialEq, Eq)]
struct StringOrArray(Vec<String>);and updating the test assertions from assert_eq!(result.cn, Some("CN".to_string())) to either constructing a StringOrArray directly (e.g. StringOrArray(vec!["CN".into()])) or asserting against result.cn.0 (the inner Vec<String>). The assert_eq!(result.c, None) calls become assert!(result.c.is_empty()). Should be ~10 lines of mechanical test-code updates around x509.rs:1363+.
With that fix the lint shard goes green and the two test libs shards should follow (they're failing the same compile, not on test logic). The 14 in-flight shards include test_node_compat for the two newly-enabled tests (test-tls-peer-certificate.js + -multi-keys.js) — those will be the real signal once the build is green again.
Not blocking on substance — the design is correct and the code body is right; just needs the trait derives and the pre-existing test adapter.
…ain, and EC curve names (denoland#33782) ## Summary - Support multi-value X.509 certificate fields (e.g. multiple OU entries) by serializing as a string for single values and an array for multiple, matching Node.js behavior - Include CA certs in the server's TLS handshake chain (OpenSSL auto-chain behavior) so `getPeerCertificate(true)` returns the full issuer chain with self-signed root circular reference - Fix EC curve names: use OpenSSL short names for `asn1Curve` (`prime256v1` not OID) and NIST names for `nistCurve` (`P-256` not `secp256r1`) - Enable `test-tls-peer-certificate.js` and `test-tls-peer-certificate-multi-keys.js` in node compat tests ## Details ### Multi-value certificate fields (`ext/node_crypto/x509.rs`) `SubjectOrIssuer` fields were `Option<String>`, so when a certificate had multiple values for the same key (e.g. two OU entries), only the last was kept. Introduced a `StringOrArray` type that accumulates values and serializes as a plain string (single value) or array (multiple values), matching Node.js. ### Issuer certificate chain (`ext/node/ops/tls_wrap.rs`, `ext/node/polyfills/_tls_wrap.js`) `getPeerCertificate(true)` wasn't returning the issuer chain because: 1. The server only sent its leaf cert — rustls's `peer_certificates()` returns only what the peer sent. Fixed by appending CA certs from the `ca` option to the server's cert chain in `build_server_config`, replicating OpenSSL's auto-chain behavior. 2. Self-signed root CAs didn't have `issuerCertificate` pointing to themselves. Added the circular reference check in `buildPeerLegacyCertificate`. ### EC curve names (`ext/node_crypto/x509.rs`) `asn1Curve` was returning raw OIDs (e.g. `1.2.840.10045.3.1.7`) instead of OpenSSL short names (`prime256v1`). `nistCurve` was returning `secp256r1` instead of `P-256`. Fixed to match Node.js/OpenSSL naming conventions.
Summary
getPeerCertificate(true)returns the full issuer chain with self-signed root circular referenceasn1Curve(prime256v1not OID) and NIST names fornistCurve(P-256notsecp256r1)test-tls-peer-certificate.jsandtest-tls-peer-certificate-multi-keys.jsin node compat testsDetails
Multi-value certificate fields (
ext/node_crypto/x509.rs)SubjectOrIssuerfields wereOption<String>, so when a certificate had multiple values for the same key (e.g. two OU entries), only the last was kept. Introduced aStringOrArraytype that accumulates values and serializes as a plain string (single value) or array (multiple values), matching Node.js.Issuer certificate chain (
ext/node/ops/tls_wrap.rs,ext/node/polyfills/_tls_wrap.js)getPeerCertificate(true)wasn't returning the issuer chain because:peer_certificates()returns only what the peer sent. Fixed by appending CA certs from thecaoption to the server's cert chain inbuild_server_config, replicating OpenSSL's auto-chain behavior.issuerCertificatepointing to themselves. Added the circular reference check inbuildPeerLegacyCertificate.EC curve names (
ext/node_crypto/x509.rs)asn1Curvewas returning raw OIDs (e.g.1.2.840.10045.3.1.7) instead of OpenSSL short names (prime256v1).nistCurvewas returningsecp256r1instead ofP-256. Fixed to match Node.js/OpenSSL naming conventions.Test plan
./x test-compat test-tls-peer-certificate.jspasses./x test-compat test-tls-peer-certificate-multi-keys.jspasses