Skip to content

Commit 7611fc1

Browse files
panvaaduh95
authored andcommitted
crypto: fix output of privateDecrypt with zero-length data
closes#57553closes#57572closes#57558 PR-URL: #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 ad5dcc5 commit 7611fc1

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 1 addition & 1 deletion
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_) returnDataPointer(buf);
218+
if (actual_len == len_) returnDataPointer(buf.data, actual_len);
219219
buf.data = OPENSSL_realloc(buf.data, actual_len);
220220
buf.len = actual_len;
221221
returnDataPointer(buf);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
4+
if(!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
constfixtures=require('../common/fixtures');
8+
constassert=require('assert');
9+
constcrypto=require('crypto');
10+
11+
const{ subtle }=globalThis.crypto;
12+
13+
// Regression test for https://github.com/nodejs/node/issues/57553.
14+
{
15+
constprivateKey=crypto.createPrivateKey(fixtures.readKey('rsa_private.pem','ascii'));
16+
constpublicKey=crypto.createPublicKey(fixtures.readKey('rsa_public.pem','ascii'));
17+
18+
constdata=Buffer.alloc(0);
19+
{
20+
21+
constciphertext=crypto.publicEncrypt({
22+
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
23+
key: publicKey,
24+
},data);
25+
26+
constplaintext=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+
constciphertext=crypto.publicEncrypt(publicKey,data);
36+
constplaintext=crypto.privateDecrypt(privateKey,ciphertext);
37+
38+
assert.deepStrictEqual(plaintext,data);
39+
}
40+
41+
{
42+
(async()=>{
43+
constpkcs8=privateKey.export({format: 'der',type: 'pkcs8'});
44+
constspki=publicKey.export({format: 'der',type: 'spki'});
45+
46+
constkp={
47+
privateKey: awaitsubtle.importKey('pkcs8',pkcs8,{name: 'RSA-OAEP',hash: 'SHA-1'},false,['decrypt']),
48+
publicKey: awaitsubtle.importKey('spki',spki,{name: 'RSA-OAEP',hash: 'SHA-1'},false,['encrypt']),
49+
};
50+
51+
constciphertext=awaitsubtle.encrypt('RSA-OAEP',kp.publicKey,data);
52+
constplaintext=awaitsubtle.decrypt('RSA-OAEP',kp.privateKey,ciphertext);
53+
assert.deepStrictEqual(plaintext,data.buffer);
54+
})().then(common.mustCall());
55+
}
56+
}

0 commit comments

Comments
(0)