Repository navigation
Exception when using privateEncrypt/privateDecrypt with certain encrypted keys #40814
Copy link
Copy link
Closed
Closed
Copy link
Labels
confirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.cryptoIssues and PRs related to the crypto subsystem.Issues and PRs related to the crypto subsystem.
Description
Version
v16.13.0
Platform
Linux EURO01 5.4.0-88-generic #99-Ubuntu SMP Thu Sep 23 17:29:00 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
crypto
What steps will reproduce the bug?
- Create an public/private RSA key-pair by using either
aes-128-ecboraes-128-ocb - Attempt to decrypt/encrypt something
Code:
const crypto = require("crypto");
let {privateKey, publicKey} = crypto.generateKeyPairSync("rsa", {
modulusLength: 2048,
publicKeyEncoding: {
type: "spki",
format: "pem"
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: "aes-128-ecb",
passphrase: "abcdef"
}
});
const encryptedString = crypto.privateEncrypt({
key: privateKey,
passphrase: "abcdef"
}, Buffer.from("The quick brown fox jumps over the lazy dog")).toString("base64");
const decryptedString = crypto.publicDecrypt(publicKey, Buffer.from(encryptedString, "base64")).toString();
console.log(`Encrypted: ${encryptedString}`);
console.log(`Decrypted: ${decryptedString}`);How often does it reproduce? Is there a required condition?
This only happens with some ciphers, some like aes-128-ccm or aes-128-cbc and other variants works just fine, other ciphers fail with an error
What is the expected behavior?
An successful encryption/decryption with the specified ciphers
What do you see instead?
node:internal/crypto/cipher:79
return method(data, format, type, passphrase, buffer, padding, oaepHash,
^
Error: error:060CC07A:digital envelope routines:EVP_CIPHER_asn1_to_param:cipher parameter error
at Object.privateEncrypt (node:internal/crypto/cipher:79:12)
at Object.<anonymous> (/home/pancho7532/Documents/AnotherBotXDDXD/rsaTest.js:25:32)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
opensslErrorStack: [
'error:060CC07A:digital envelope routines:EVP_CIPHER_asn1_to_param:cipher parameter error'
],
library: 'digital envelope routines',
function: 'EVP_CIPHER_asn1_to_param',
reason: 'cipher parameter error',
code: 'ERR_OSSL_EVP_CIPHER_PARAMETER_ERROR'
}
Additional information
In v14.17.3 (the version i had previously) Node.JS would crash with a core dump on some ciphers (like GCM based ciphers)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
confirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.cryptoIssues and PRs related to the crypto subsystem.Issues and PRs related to the crypto subsystem.
Activity
[-]Exception when using `privateEncrypt/privateDecrypt` on certain key types[/-][+]Exception when using `privateEncrypt`/`privateDecrypt` with certain encrypted keys[/+]tniessen commentedon Nov 16, 2021
Thank you for the report. This is an interesting one. I've been debugging this for a while now and something seems very wrong here.
Most spectacularly, in Node.js 17 (both on Windows and Ubuntu), the first attempt to load an
aes-128-ecbencrypted key appears to fail, but the second succeeds:PhilDay-CT commentedon Dec 29, 2021
I'm seeing something similar on node 16.3.1 on Ubuntu 20.04, but with a sightly different error:
Error: error:0D0C5006:asn1 encoding routines:ASN1_item_verify:EVP lib
at Object.createPrivateKey (node:internal/crypto/keys:608:12)
at readPrivateKeyBuffer (/opt/ct/node_modules/@configuredthings/sfjs-language/lib/parser/sfjsCrypto.js:51:22)
at readPrivateKeyFile (/opt/ct/node_modules/@configuredthings/sfjs-language/lib/parser/sfjsCrypto.js:43:12)
at decryptDoc (/opt/ct/node_modules/@configuredthings/sfjs-language/lib/parser/sfjsCrypto.js:748:55)
at ConfigGateway. (/opt/ct/node_modules/@configuredthings/sfjs-components/lib/ConfigGateway/ConfigGateway.js:132:66)
at ConfigGateway. (/opt/ct/node_modules/@configuredthings/sfjs-components/lib/ConfigGateway/ConfigGateway.js:59:103)
at FileMonitor. (/opt/ct/node_modules/@configuredthings/sfjs-components/lib/Abstract/FileMonitor.js:185:17)
at FileMonitor. (/opt/ct/node_modules/@configuredthings/sfjs-components/lib/Abstract/FileMonitor.js:166:18)
at FSWatcher.emit (node:events:390:28)
at FSWatcher.emitWithAll (/opt/ct/node_modules/chokidar/index.js:540:8) {
opensslErrorStack: [ 'error:0D0C5006:asn1 encoding routines:ASN1_item_verify:EVP lib' ],
library: 'asn1 encoding routines',
function: 'ASN1_item_verify',
reason: 'EVP lib',
code: 'ERR_OSSL_ASN1_EVP_LIB'
}
It doesn't seem to be a first or second call that always fails, I'm getting this both after a successful call and when this is the fist call.
What I have seen is that catching the exception and retrying the call always seems to work.
src: fix `crypto.privateEncrypt` fails first time
src: fix `crypto.privateEncrypt` fails first time
PANCHO7532B commentedon Nov 2, 2022
Alright, fix seemingly works, but for only ECB ciphers, other ciphers like -ccm -ctr -ocb -xts variants and other cipher types are still broken with more or less the same error... or a variant of it
I used the following snippet to test all ciphers given by
crypto.getCiphers()Broken ciphers:
Additionally, the following ciphers throw an Segmentation fault:
With the exception of
des3-wrapwho fails witherror:1C800066:Provider routines::cipher operation failedbefore throwing the following as I try a few more times to encrypt:Versions i tried:
I'm not really sure if these would apply for an separate issue, or it should be an continuation of this issue... or even if it should be valid, in my opinion it is since I would expect this to work with all ciphers at
crypto.getCiphers()at least.aduh95 commentedon Nov 2, 2022
@PANCHO7532B you should probably open a new issue, or a directly a PR if you know what is the fix.
src: fix `crypto.privateEncrypt` fails first time