Skip to content

fix(ext/node): fix TLS peer certificate multi-value fields, issuer chain, and EC curve names - #33782

Merged
bartlomieju merged 2 commits into
mainfrom
fix/tls-peer-certificate
May 3, 2026
Merged

bartlomieju merged 2 commits into
mainfrom
fix/tls-peer-certificate

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

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.

Test plan

  • ./x test-compat test-tls-peer-certificate.js passes
  • ./x test-compat test-tls-peer-certificate-multi-keys.js passes

…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 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.

Substance is right and tracks Node's actual behavior on all three fronts:

What's right

  1. Multi-value field handling via StringOrArray(Vec<String>) with a hand-rolled Serialize impl that emits a plain string for len == 1 and an array otherwise. That's the exact shape Node uses (peerCert.subject.OU is a string when there's one OU, an array when there are multiple). Switching all SubjectOrIssuer fields from Option<String> to StringOrArray and replacing every result.field = Some(value_str) with result.field.push(value_str) is the minimal change that supports accumulation.
  2. StringOrArray::is_empty for skip_serializing_if: keeps the same JSON-shape contract as the previous Option::is_none guard (empty fields still don't appear in the serialized output).
  3. 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; appending ca certs to the server's cert chain in build_server_config is the right fix.
  4. EC curve naming split: asn1Curve uses OpenSSL short names (prime256v1), nistCurve uses 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)], so assert_eq! can't print or compare it.
  • Has changed type signature, so Some("CN".to_string()) is no longer a valid value for result.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.

@bartlomieju
bartlomieju merged commit e277e4d into main May 3, 2026
136 checks passed
@bartlomieju
bartlomieju deleted the fix/tls-peer-certificate branch May 3, 2026 07:41
littledivy pushed a commit to crowlKats/deno that referenced this pull request Jun 10, 2026
…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.
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