From b306018014f6064eb2cb9440a1e0b58628e8063c Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Wed, 20 Apr 2022 14:43:31 +0800 Subject: [PATCH 1/3] src: fix crypto.privateEncrypt fails first time crypto.privateEncrypt fails for the first time after crypto.generateKeyPairSync with certain parameters Because the error stack is not cleaned up when crypto.generateKeyPairSync exits. Fixes: https://github.com/nodejs/node/issues/40814 --- src/crypto/crypto_keys.cc | 1 + test/parallel/test-crypto-aes-128-ecb.js | 35 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 test/parallel/test-crypto-aes-128-ecb.js diff --git a/src/crypto/crypto_keys.cc b/src/crypto/crypto_keys.cc index 5dd04edfbfa1cf..5e07389e0ab411 100644 --- a/src/crypto/crypto_keys.cc +++ b/src/crypto/crypto_keys.cc @@ -318,6 +318,7 @@ MaybeLocal WritePrivateKey( } } + MarkPopErrorOnReturn mark_pop_error_on_return; bool err; PKEncodingType encoding_type = config.type_.ToChecked(); diff --git a/test/parallel/test-crypto-aes-128-ecb.js b/test/parallel/test-crypto-aes-128-ecb.js new file mode 100644 index 00000000000000..2ae1efe75c3aad --- /dev/null +++ b/test/parallel/test-crypto-aes-128-ecb.js @@ -0,0 +1,35 @@ +'use strict'; +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); +const crypto = require('crypto'); + +const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', { + modulusLength: 2048, + publicKeyEncoding: { + type: 'spki', + format: 'pem' + }, + privateKeyEncoding: { + type: 'pkcs8', + format: 'pem', + cipher: 'aes-128-ecb', + passphrase: 'abcdef' + } +}); +assert.notStrictEqual(privateKey.toString(), ''); + +const msg = 'The quick brown fox jumps over the lazy dog'; + +const encryptedString = crypto.privateEncrypt({ + key: privateKey, + passphrase: 'abcdef' +}, Buffer.from(msg)).toString('base64'); +const decryptedString = crypto.publicDecrypt(publicKey, Buffer.from(encryptedString, 'base64')).toString(); +console.log(`Encrypted: ${encryptedString}`); +console.log(`Decrypted: ${decryptedString}`); + +assert.notStrictEqual(encryptedString, ''); +assert.strictEqual(decryptedString, msg); From 9a7c0636b52e075b7a8a4577c1d20c6082fdd562 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Thu, 21 Apr 2022 02:41:36 +0800 Subject: [PATCH 2/3] modify test --- ...128-ecb.js => test-crypto-publicDecrypt-fails-first-time.js} | 2 ++ 1 file changed, 2 insertions(+) rename test/parallel/{test-crypto-aes-128-ecb.js => test-crypto-publicDecrypt-fails-first-time.js} (94%) diff --git a/test/parallel/test-crypto-aes-128-ecb.js b/test/parallel/test-crypto-publicDecrypt-fails-first-time.js similarity index 94% rename from test/parallel/test-crypto-aes-128-ecb.js rename to test/parallel/test-crypto-publicDecrypt-fails-first-time.js index 2ae1efe75c3aad..1e03ad36b5ab8b 100644 --- a/test/parallel/test-crypto-aes-128-ecb.js +++ b/test/parallel/test-crypto-publicDecrypt-fails-first-time.js @@ -3,6 +3,8 @@ const common = require('../common'); if (!common.hasCrypto) common.skip('missing crypto'); +// Test for https://github.com/nodejs/node/issues/40814 + const assert = require('assert'); const crypto = require('crypto'); From 12c8ba79cfe6db1b957d894aa9191fec607524e8 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Wed, 4 May 2022 07:42:58 +0800 Subject: [PATCH 3/3] fix test --- test/parallel/test-crypto-publicDecrypt-fails-first-time.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-crypto-publicDecrypt-fails-first-time.js b/test/parallel/test-crypto-publicDecrypt-fails-first-time.js index 1e03ad36b5ab8b..a60b87dbf65229 100644 --- a/test/parallel/test-crypto-publicDecrypt-fails-first-time.js +++ b/test/parallel/test-crypto-publicDecrypt-fails-first-time.js @@ -1,9 +1,13 @@ 'use strict'; const common = require('../common'); + +// Test for https://github.com/nodejs/node/issues/40814 + if (!common.hasCrypto) common.skip('missing crypto'); -// Test for https://github.com/nodejs/node/issues/40814 +if (!common.hasOpenSSL3) + common.skip('only openssl3'); // https://github.com/nodejs/node/pull/42793#issuecomment-1107491901 const assert = require('assert'); const crypto = require('crypto');