Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: fix output of privateDecrypt with zero-length data #57575

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deps/ncrypto/ncrypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Buffer<void> DataPointer::release() {
DataPointer DataPointer::resize(size_t len) {
size_t actual_len = std::min(len_, len);
auto buf = release();
if (actual_len == len_) return DataPointer(buf);
if (actual_len == len_) return DataPointer(buf.data, actual_len);
buf.data = OPENSSL_realloc(buf.data, actual_len);
buf.len = actual_len;
return DataPointer(buf);
Expand Down
51 changes: 51 additions & 0 deletions test/parallel/test-crypto-zero-lengths.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as common from '../common/index.mjs';

if (!common.hasCrypto)
common.skip('missing crypto');

import * as fixtures from '../common/fixtures.mjs';
import assert from 'assert';
import crypto from 'crypto';

const { subtle } = globalThis.crypto;

{
const privateKey = crypto.createPrivateKey(fixtures.readKey('rsa_private.pem', 'ascii'));
const publicKey = crypto.createPublicKey(fixtures.readKey('rsa_public.pem', 'ascii'));

const data = Buffer.alloc(0);
{

const ciphertext = crypto.publicEncrypt({
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
key: publicKey,
}, data);

const plaintext = crypto.privateDecrypt({
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
key: privateKey
}, ciphertext);

assert.deepStrictEqual(plaintext, data);
}

{
const ciphertext = crypto.publicEncrypt(publicKey, data);
const plaintext = crypto.privateDecrypt(privateKey, ciphertext);

assert.deepStrictEqual(plaintext, data);
}

{
const pkcs8 = privateKey.export({ format: 'der', type: 'pkcs8' });
const spki = publicKey.export({ format: 'der', type: 'spki' });
const kp = {
privateKey: await subtle.importKey('pkcs8', pkcs8, { name: 'RSA-OAEP', hash: 'SHA-1' }, false, ['decrypt']),
publicKey: await subtle.importKey('spki', spki, { name: 'RSA-OAEP', hash: 'SHA-1' }, false, ['encrypt']),
};

const ciphertext = await subtle.encrypt('RSA-OAEP', kp.publicKey, data);
const plaintext = await subtle.decrypt('RSA-OAEP', kp.privateKey, ciphertext);
assert.deepStrictEqual(plaintext, data.buffer);
}
}
Loading