fix(ext/node): support encrypted PEM export and deprecated hash option in crypto keygen#32703
Conversation
…n in crypto keygen - Fix `generateKeyPair` for `rsa-pss` to fall back to deprecated `hash` option when `hashAlgorithm` is not provided (matching Node.js behavior) - Implement encrypted PEM private key export with legacy OpenSSL format (Proc-Type/DEK-Info headers) for PKCS#1, PKCS#8, and SEC1 key types - Support AES-128-CBC, AES-192-CBC, AES-256-CBC, and DES-EDE3-CBC ciphers for PEM encryption using EVP_BytesToKey key derivation - Pass cipher/passphrase from KeyObject.export() to the Rust op - Add node_compat test entries for crypto keygen tests Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
littledivy
left a comment
There was a problem hiding this comment.
The PEM export path introduces a high-severity security/compatibility bug: if callers provide only one of cipher or passphrase, the code silently falls back to exporting an unencrypted private key. This can leak key material unexpectedly and does not match expected option validation behavior. The export API must reject partial encryption configuration rather than downgrading to plaintext output.
ext/node_crypto/keys.rs
Outdated
|
|
||
| if let (Some(cipher), Some(passphrase)) = (cipher, passphrase) { | ||
| return encrypt_private_key_pem( | ||
| label, |
There was a problem hiding this comment.
[high] PEM encryption is applied only when both cipher and passphrase are Some; if only one is provided, the function silently exports an unencrypted key. This is a dangerous implicit downgrade and behavior mismatch. Add explicit validation: if exactly one of cipher/passphrase is set, return an error (TypeError/validation error) instead of exporting plaintext.
Return a TypeError if only one of cipher/passphrase is provided instead of silently exporting an unencrypted key. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
generateKeyPairforrsa-pssto fall back to deprecatedhashoption whenhashAlgorithmis not provided, matching Node.js behaviorcipher/passphraseoptions fromKeyObject.export()through to the Rust optest-crypto-keygen-deprecation,test-crypto-keygen-bit-length(ignored: Rust dsa crate limitation), andtest-crypto-keygen-async-rsa(ignored: legacy PEM decryption not yet supported)Test plan
test-crypto-keygen-deprecation.jspasses (was failing due to missinghash→hashAlgorithmfallback)test-crypto-keygen-bit-length.jsignored with reason (dsa crate only supports fixed key sizes)test-crypto-keygen-async-rsa.jsignored with reason (legacy PEM decryption not yet implemented)🤖 Generated with Claude Code