Skip to content

Commit b306018

Browse files
committed
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: #40814
1 parent 1fe5d56 commit b306018

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/crypto/crypto_keys.cc

+1
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ MaybeLocal<Value> WritePrivateKey(
318318
}
319319
}
320320

321+
MarkPopErrorOnReturn mark_pop_error_on_return;
321322
bool err;
322323

323324
PKEncodingType encoding_type = config.type_.ToChecked();
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
const assert = require('assert');
7+
const crypto = require('crypto');
8+
9+
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
10+
modulusLength: 2048,
11+
publicKeyEncoding: {
12+
type: 'spki',
13+
format: 'pem'
14+
},
15+
privateKeyEncoding: {
16+
type: 'pkcs8',
17+
format: 'pem',
18+
cipher: 'aes-128-ecb',
19+
passphrase: 'abcdef'
20+
}
21+
});
22+
assert.notStrictEqual(privateKey.toString(), '');
23+
24+
const msg = 'The quick brown fox jumps over the lazy dog';
25+
26+
const encryptedString = crypto.privateEncrypt({
27+
key: privateKey,
28+
passphrase: 'abcdef'
29+
}, Buffer.from(msg)).toString('base64');
30+
const decryptedString = crypto.publicDecrypt(publicKey, Buffer.from(encryptedString, 'base64')).toString();
31+
console.log(`Encrypted: ${encryptedString}`);
32+
console.log(`Decrypted: ${decryptedString}`);
33+
34+
assert.notStrictEqual(encryptedString, '');
35+
assert.strictEqual(decryptedString, msg);

0 commit comments

Comments
 (0)