Skip to content

Fix crypto-rsa code #57572

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions package-lock.json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please remove this?

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 28 additions & 5 deletions src/crypto/crypto_rsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ using Cipher_t = DataPointer(const EVPKeyPointer& key,
const ncrypto::Rsa::CipherParams& params,
const ncrypto::Buffer<const void> in);

template <Cipher_t cipher>
template <Cipher_t cipher>
WebCryptoCipherStatus RSA_Cipher(Environment* env,
const KeyObjectData& key_data,
const RSACipherConfig& params,
const ByteSource& in,
ByteSource* out) {
const KeyObjectData& key_data,
const RSACipherConfig& params,
const ByteSource& in,
ByteSource* out) {
CHECK_NE(key_data.GetKeyType(), kKeyTypeSecret);
Mutex::ScopedLock lock(key_data.mutex());
const auto& m_pkey = key_data.GetAsymmetricKey();
Expand All @@ -206,10 +207,32 @@ WebCryptoCipherStatus RSA_Cipher(Environment* env,
if (!data) return WebCryptoCipherStatus::FAILED;
DCHECK(!data.isSecure());

// Check if we're in a decryption operation and the key is private
// (which would indicate we're decrypting)
if (key_data.GetKeyType() == kKeyTypePrivate) {
bool is_effectively_empty = true;
const unsigned char* data_ptr = static_cast<const unsigned char*>(data.data());
size_t data_size = data.size();

// Check if all bytes are zero
for (size_t i = 0; i < data_size; i++) {
if (data_ptr[i] != 0) {
is_effectively_empty = false;
break;
}
}

// If we have an effectively empty result, return a truly empty buffer
if (is_effectively_empty && data_size > 0) {
*out = ByteSource::Allocated(0);
return WebCryptoCipherStatus::OK;
}
}

// Normal case - data contains actual content
*out = ByteSource::Allocated(data.release());
return WebCryptoCipherStatus::OK;
}
} // namespace

Maybe<void> RSAKeyExportTraits::AdditionalConfig(
const FunctionCallbackInfo<Value>& args,
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/package-lock.json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please remove this?

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions test/parallel/test-crypto-private-decrypt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// file: test-crypto-private-decrypt.js
const crypto = require('crypto');

const keys = crypto.generateKeyPairSync(
'rsa',
{
modulusLength: 2048,
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
}
);

const empty = '';

const ciphertext = crypto.publicEncrypt({
oaepHash:'sha1',
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
key: keys.publicKey,
}, Buffer.from(empty)).toString('base64');

const plaintext = crypto.privateDecrypt({
oaepHash: 'sha1',
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
key: keys.privateKey
}, Buffer.from(ciphertext, 'base64')).toString('utf8');
console.assert(empty === plaintext, 'rsa-oaep `encrypt` empty string is success, but `decrypt` got unexpected string.');