Skip to content

crypto: add X.509 generation with CSR support - #67245

Closed
panva wants to merge 4 commits into
nodejs:mainfrom
panva:x509-generation
Closed

panva wants to merge 4 commits into
nodejs:mainfrom
panva:x509-generation

Conversation

@panva

@panva panva commented Nov 4, 2026

Copy link
Copy Markdown
Member

Expose callback-based generateX509Certificate() and synchronous generateX509CertificateSync(), producing X509Certificate objects for self-signed and issuer-signed X.509 v3 certificates. Accept grouped distinguished names or exact subject DER, public keys, serial numbers, validity dates, and an explicit signing digest. Reuse crypto.sign() key inputs and RSA options, including OpenSSL STORE-backed keys.

A PKCS#10 request can supply the subject and public key instead. Require a synchronous selectExtensions callback to accept, replace, or amend the requested extensions. Verify the request signature before signing the certificate, in the selected sync or async mode.

Provide typed Basic Constraints, Key Usage, Extended Key Usage, SAN, SKI, and AKI, plus arbitrary raw extensions. Add none implicitly and leave raw extension semantics to the caller. Copy mutable inputs before queueing asynchronous work. Certificate trust and CA authorization remain the caller's responsibility.

This is PR 4 out of 4 in an X.509 series addressing #48730 and #51169.

panva added 2 commits November 4, 2026 10:52
Expose callback-based generateX509Certificate() and synchronous
generateX509CertificateSync(), producing X509Certificate objects for
self-signed and issuer-signed X.509 v3 certificates. Accept grouped
distinguished names or exact subject DER, public keys, serial numbers,
validity dates, and an explicit signing digest. Reuse crypto.sign() key
inputs and RSA options, including OpenSSL STORE-backed keys.

A PKCS#10 request can supply the subject and public key instead.
Require a synchronous selectExtensions callback to accept, replace, or
amend the requested extensions. Verify the request signature before
signing the certificate, in the selected sync or async mode.

Provide typed Basic Constraints, Key Usage, Extended Key Usage, SAN,
SKI, and AKI, plus arbitrary raw extensions. Add none implicitly and
leave raw extension semantics to the caller. Copy mutable inputs
before queueing asynchronous work. Certificate trust and CA
authorization remain the caller's responsibility.

Fixes: nodejs#51169
Assisted-by: Codex
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
Require a supported backend when selecting the BIT STRING length
capability. Reject other backends at compile time, matching the
surrounding checks used to catch unsupported OpenSSL backports.

Assisted-by: Codex
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/gyp
  • @nodejs/security-wg

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Nov 4, 2026
@panva
panva requested a review from pimterry November 4, 2026 10:43
@panva panva added the commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. label Nov 4, 2026
@panva
panva requested a review from mcollina November 4, 2026 10:43
@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.58188% with 293 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.63%. Comparing base (3b209b2) to head (a44ae0e).
⚠️ Report is 67 commits behind head on main.

Files with missing lines Patch % Lines
src/crypto/crypto_x509_gen.cc 68.26% 102 Missing and 149 partials ⚠️
lib/internal/crypto/x509_generation.js 93.47% 39 Missing and 1 partial ⚠️
src/crypto/crypto_x509_gen.h 33.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67245      +/-   ##
==========================================
- Coverage   90.68%   90.63%   -0.06%     
==========================================
  Files         797      800       +3     
  Lines      279367   280794    +1427     
  Branches    53737    54083     +346     
==========================================
+ Hits       253344   254493    +1149     
- Misses      16319    16443     +124     
- Partials     9704     9858     +154     
Files with missing lines Coverage Δ
lib/crypto.js 94.56% <100.00%> (+0.22%) ⬆️
lib/internal/crypto/sig.js 98.46% <100.00%> (+<0.01%) ⬆️
src/crypto/crypto_util.h 68.53% <ø> (ø)
src/crypto/crypto_x509.cc 71.52% <100.00%> (-0.20%) ⬇️
src/crypto/crypto_x509.h 65.21% <ø> (ø)
src/node_crypto.cc 81.81% <ø> (ø)
src/crypto/crypto_x509_gen.h 33.33% <33.33%> (ø)
lib/internal/crypto/x509_generation.js 93.47% <93.47%> (ø)
src/crypto/crypto_x509_gen.cc 68.26% <68.26%> (ø)

... and 28 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@panva panva added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Nov 9, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 9, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@panva panva added the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 9, 2026
@github-actions github-actions Bot removed the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 9, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@panva panva added semver-minor PRs that contain new features and should be released in the next minor version. baking-for-lts PRs whose changes need more time before an LTS backport. commit-queue PRs queued for automated landing through the Commit Queue. labels Nov 11, 2026
@nodejs-github-bot nodejs-github-bot added the lacks-second-approval Commit Queue PRs awaiting a second collaborator approval or completion of the required wait. label Nov 11, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 77e6379...2336a1f

nodejs-github-bot pushed a commit that referenced this pull request Nov 11, 2026
Expose callback-based generateX509Certificate() and synchronous
generateX509CertificateSync(), producing X509Certificate objects for
self-signed and issuer-signed X.509 v3 certificates. Accept grouped
distinguished names or exact subject DER, public keys, serial numbers,
validity dates, and an explicit signing digest. Reuse crypto.sign() key
inputs and RSA options, including OpenSSL STORE-backed keys.

A PKCS#10 request can supply the subject and public key instead.
Require a synchronous selectExtensions callback to accept, replace, or
amend the requested extensions. Verify the request signature before
signing the certificate, in the selected sync or async mode.

Provide typed Basic Constraints, Key Usage, Extended Key Usage, SAN,
SKI, and AKI, plus arbitrary raw extensions. Add none implicitly and
leave raw extension semantics to the caller. Copy mutable inputs
before queueing asynchronous work. Certificate trust and CA
authorization remain the caller's responsibility.

Fixes: #51169
Assisted-by: Codex
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: #67245
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
nodejs-github-bot pushed a commit that referenced this pull request Nov 11, 2026
Require a supported backend when selecting the BIT STRING length
capability. Reject other backends at compile time, matching the
surrounding checks used to catch unsupported OpenSSL backports.

Assisted-by: Codex
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: #67245
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 11, 2026
@nodejs-github-bot nodejs-github-bot removed the lacks-second-approval Commit Queue PRs awaiting a second collaborator approval or completion of the required wait. label Nov 11, 2026
@panva
panva deleted the x509-generation branch November 11, 2026 11:03
aduh95 pushed a commit that referenced this pull request Nov 11, 2026
Expose callback-based generateX509Certificate() and synchronous
generateX509CertificateSync(), producing X509Certificate objects for
self-signed and issuer-signed X.509 v3 certificates. Accept grouped
distinguished names or exact subject DER, public keys, serial numbers,
validity dates, and an explicit signing digest. Reuse crypto.sign() key
inputs and RSA options, including OpenSSL STORE-backed keys.

A PKCS#10 request can supply the subject and public key instead.
Require a synchronous selectExtensions callback to accept, replace, or
amend the requested extensions. Verify the request signature before
signing the certificate, in the selected sync or async mode.

Provide typed Basic Constraints, Key Usage, Extended Key Usage, SAN,
SKI, and AKI, plus arbitrary raw extensions. Add none implicitly and
leave raw extension semantics to the caller. Copy mutable inputs
before queueing asynchronous work. Certificate trust and CA
authorization remain the caller's responsibility.

Fixes: #51169
Assisted-by: Codex
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: #67245
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
aduh95 pushed a commit that referenced this pull request Nov 11, 2026
Require a supported backend when selecting the BIT STRING length
capability. Reject other backends at compile time, matching the
surrounding checks used to catch unsupported OpenSSL backports.

Assisted-by: Codex
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: #67245
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
nodejs-github-bot added a commit that referenced this pull request Nov 11, 2026
Notable changes:

child_process:
  * (SEMVER-MINOR) add subprocess.timedOut (Marco) #66639
crypto:
  * (SEMVER-MINOR) make X.509 inspection guards explicit (Filip Skokan) #67245
  * (SEMVER-MINOR) add X.509 generation with CSR support (Filip Skokan) #67245
dns:
  * (SEMVER-MINOR) coalesce identical concurrent lookup() requests (Orgad Shaneh) #62599
doc:
  * add efekrskl to collaborators (Efe Karasakal) #67376
  * add Philipp Dunkel to contributors (Philipp Dunkel) #67378
  * add christianaurichzm to collaborators (Christian Aurich Zanettini Martins) #67377
http:
  * (SEMVER-MINOR) batch body spans per parser execute (Guilherme Araújo) #67187
sea:
  * (SEMVER-MINOR) generate a compile cache for the VFS modules (Matteo Collina) #66768
worker:
  * (SEMVER-MINOR) preload streams into startup snapshot (Matteo Collina) #66311

PR-URL: #67415
aduh95 pushed a commit that referenced this pull request Nov 12, 2026
Notable changes:

child_process:
  * (SEMVER-MINOR) add subprocess.timedOut (Marco) #66639
crypto:
  * (SEMVER-MINOR) make X.509 inspection guards explicit (Filip Skokan) #67245
  * (SEMVER-MINOR) add X.509 generation with CSR support (Filip Skokan) #67245
dns:
  * (SEMVER-MINOR) coalesce identical concurrent lookup() requests (Orgad Shaneh) #62599
doc:
  * add efekrskl to collaborators (Efe Karasakal) #67376
  * add Philipp Dunkel to contributors (Philipp Dunkel) #67378
  * add christianaurichzm to collaborators (Christian Aurich Zanettini Martins) #67377
http:
  * (SEMVER-MINOR) batch body spans per parser execute (Guilherme Araújo) #67187
sea:
  * (SEMVER-MINOR) generate a compile cache for the VFS modules (Matteo Collina) #66768
worker:
  * (SEMVER-MINOR) preload streams into startup snapshot (Matteo Collina) #66311

PR-URL: #67415
@pimterry

Copy link
Copy Markdown
Member

I has taken me millennia, but sorry @panva, but I've now had a change to properly look at this and play with it. I've built out a local version of my setup on top to test it in practice, and it does work nicely! Speeds up my cert generation code up to 6x, which is very nice indeed 😃 There are some non-breaking additions which would simplify using it in my setup (we'll get to those later) but excluding that, some other bits worth mentioning here in the meantime:

I found a few small specific tiny issues, which I've opened quick fixes to cover in #67781.

I think the CSR API design could be improved. I think it falls in an unhappy valley right now: it's a specialized feature that most users won't need, but for the users who do need it it's too limited.

I'm particularly thinking about the selectExtensions approach, which doesn't seem like it'd cover most real cases where people use CSRs, where they need real validation and control over what gets signed and how. That means you may well want policy on other CSR params (not just extensions), you might want extension policy to relate to other cert params (selectExtensions only receives the extension data - there's no way to read the rest of the CSR data without a separate parser), you might want async policy decisions, and you'll want to cleanly reject some CSRs up front rather than just filtering details during generation.

If we consider that the current CSR flow looks roughly like this:

generateX509CertificateSync({
  csr, issuer, serialNumber, notBefore, notAfter, signature,
  selectExtensions(requestedExts) { return applyPolicy(requestedExts); }
});

I wonder if it should look more like:

const req = X509CertificateRequest.parse(csr);
doCustomValidation(req); // Read any field, do anything

generateX509Certificate({
  subject: req.subjectName.raw,
  publicKey: req.publicKey,
  extensions: await applyCustomExtensionPolicy(req),
  ...
});

That shape has the advantage of making cert generation completely independent of CSRs, instead of mixing the CSR code into cert generation. They can be separate implementations that users glue together themselves. Makes it much easier to apply policy & transformations freely, and makes both implementations simpler.

Separately: even without that change, I think we might want to consider moving the CSR part of this API back to stage 1.1 experimental, behind a CLI flag, because it's security sensitive. CSRs mean feeding parsing of attacker input into trusted cert generation. I don't see any obvious issues today, but I bet a wave of AI scanners will find some soon enough. It will be much easier to find & fix those bugs in public while this is fully experimental, instead of having to treat all issues as serious vulnerabilities that we have to fix in secret.

Only applies to CSRs specifically since they're generally external input. I think most other X509 generation (barring extremely horrible bugs) wouldn't be security issues, the most likely failure cases just result in broken certs I think. Passing untrusted input into a X509 generation APIs is a bug in your code, not ours.

I have some other notable thoughts on the TBS side, which we briefly discussed in Slack, I still need to dig into that and explore it. I think there's some tiny API changes here that would make that easier to glue together, but nothing major. Otherwise everything looks great to me. Thanks for all the work on this @panva!

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

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. baking-for-lts PRs whose changes need more time before an LTS backport. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants