Skip to content

Commit d13d902

Browse files
panvaJonasBa
authored andcommitted
crypto: fix output of privateDecrypt with zero-length data
closes nodejs#57553 closes nodejs#57572 closes nodejs#57558 PR-URL: nodejs#57575 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent 478b385 commit d13d902

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

deps/ncrypto/ncrypto.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ Buffer<void> DataPointer::release() {
215215
DataPointer DataPointer::resize(size_t len) {
216216
size_t actual_len = std::min(len_, len);
217217
auto buf = release();
218-
if (actual_len == len_) return DataPointer(buf);
218+
if (actual_len == len_) return DataPointer(buf.data, actual_len);
219219
buf.data = OPENSSL_realloc(buf.data, actual_len);
220220
buf.len = actual_len;
221221
return DataPointer(buf);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const fixtures = require('../common/fixtures');
8+
const assert = require('assert');
9+
const crypto = require('crypto');
10+
11+
const { subtle } = globalThis.crypto;
12+
13+
// Regression test for https://github.com/nodejs/node/issues/57553.
14+
{
15+
const privateKey = crypto.createPrivateKey(fixtures.readKey('rsa_private.pem', 'ascii'));
16+
const publicKey = crypto.createPublicKey(fixtures.readKey('rsa_public.pem', 'ascii'));
17+
18+
const data = Buffer.alloc(0);
19+
{
20+
21+
const ciphertext = crypto.publicEncrypt({
22+
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
23+
key: publicKey,
24+
}, data);
25+
26+
const plaintext = crypto.privateDecrypt({
27+
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
28+
key: privateKey
29+
}, ciphertext);
30+
31+
assert.deepStrictEqual(plaintext, data);
32+
}
33+
34+
{
35+
const ciphertext = crypto.publicEncrypt(publicKey, data);
36+
const plaintext = crypto.privateDecrypt(privateKey, ciphertext);
37+
38+
assert.deepStrictEqual(plaintext, data);
39+
}
40+
41+
{
42+
(async () => {
43+
const pkcs8 = privateKey.export({ format: 'der', type: 'pkcs8' });
44+
const spki = publicKey.export({ format: 'der', type: 'spki' });
45+
46+
const kp = {
47+
privateKey: await subtle.importKey('pkcs8', pkcs8, { name: 'RSA-OAEP', hash: 'SHA-1' }, false, ['decrypt']),
48+
publicKey: await subtle.importKey('spki', spki, { name: 'RSA-OAEP', hash: 'SHA-1' }, false, ['encrypt']),
49+
};
50+
51+
const ciphertext = await subtle.encrypt('RSA-OAEP', kp.publicKey, data);
52+
const plaintext = await subtle.decrypt('RSA-OAEP', kp.privateKey, ciphertext);
53+
assert.deepStrictEqual(plaintext, data.buffer);
54+
})().then(common.mustCall());
55+
}
56+
}

0 commit comments

Comments
 (0)