Skip to content

fix(ext/node): decrypt encrypted private keys in publicEncrypt/privateDecrypt and ignore passphrase#33770

Merged
bartlomieju merged 1 commit into
denoland:mainfrom
divybot:claude/test-crypto-keygen-async-encrypted-private-key
May 1, 2026
Merged

fix(ext/node): decrypt encrypted private keys in publicEncrypt/privateDecrypt and ignore passphrase#33770
bartlomieju merged 1 commit into
denoland:mainfrom
divybot:claude/test-crypto-keygen-async-encrypted-private-key

Conversation

@divybot

@divybot divybot commented May 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Enables test-crypto-keygen-async-encrypted-private-key in node_compat suite.

Test plan

  • cargo test --test node_compat -- test-crypto-keygen-async-encrypted-private-key

…eDecrypt and ignore passphrase

Enables tests/node_compat/runner/suite/test/parallel/test-crypto-keygen-async-encrypted-private-key.js

Co-authored-by: Divy Srivastava <[email protected]>

@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. Two-sided fix that lines up well:

  • JS side prepareKey (cipher.ts:760–786): when the user supplies { key, passphrase } to publicEncrypt / privateDecrypt with raw key material, build a KeyObject via op_node_create_private_key(key, format, type, passphrase) and re-export as unencrypted PKCS#8 PEM, then hand that to the existing downstream RSA op. Default-format inference (format ?? (typeof data === "string" ? "pem" : "der")) matches Node's own crypto.createPrivateKey defaulting.
  • Rust side KeyObjectHandle::* PKCS#8 DER + passphrase (keys.rs:903–916): when both passphrase and key arrive, branch on EncryptedPrivateKeyInfo::try_from(key).is_ok() — if actually encrypted, decrypt; if the key isn't encrypted, ignore the passphrase and parse as plain PKCS#8. Comment is exactly right: Node treats a passphrase on an unencrypted key as a no-op rather than rejecting, so this matches the semantics CryptoKey/createPrivateKey users rely on.
  • The new EncryptedPkcs8DerRequiresPassphrase variant is reused from #33756, so the encrypted-without-passphrase path stays on the ERR_MISSING_PASSPHRASE branch — depends on #33756 landing first (which is already approved).
  • All 48 test node_compat + test unit_node shards across 4 platforms × debug/release are SUCCESS at bdc8371. Only wpt release linux-x86_64 is still in flight, and WPT doesn't exercise crypto in any way relevant to this change.

@bartlomieju
bartlomieju merged commit cc844ee into denoland:main May 1, 2026
136 checks passed
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.

3 participants