Skip to content

Commit 2979c58

Browse files
tniessentargos
authored andcommitted
crypto: throw errors in SignTraits::DeriveBits
Fixes: #40794 PR-URL: #40796 Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7d127d2 commit 2979c58

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/crypto/crypto_sig.cc

+14-5
Original file line numberDiff line numberDiff line change
@@ -738,19 +738,25 @@ bool SignTraits::DeriveBits(
738738
size_t len;
739739
unsigned char* data = nullptr;
740740
if (IsOneShot(params.key)) {
741-
EVP_DigestSign(
741+
if (!EVP_DigestSign(
742742
context.get(),
743743
nullptr,
744744
&len,
745745
params.data.data<unsigned char>(),
746-
params.data.size());
746+
params.data.size())) {
747+
crypto::CheckThrow(env, SignBase::Error::kSignPrivateKey);
748+
return false;
749+
}
747750
data = MallocOpenSSL<unsigned char>(len);
748-
EVP_DigestSign(
751+
if (!EVP_DigestSign(
749752
context.get(),
750753
data,
751754
&len,
752755
params.data.data<unsigned char>(),
753-
params.data.size());
756+
params.data.size())) {
757+
crypto::CheckThrow(env, SignBase::Error::kSignPrivateKey);
758+
return false;
759+
}
754760
ByteSource buf =
755761
ByteSource::Allocated(reinterpret_cast<char*>(data), len);
756762
*out = std::move(buf);
@@ -760,13 +766,16 @@ bool SignTraits::DeriveBits(
760766
params.data.data<unsigned char>(),
761767
params.data.size()) ||
762768
!EVP_DigestSignFinal(context.get(), nullptr, &len)) {
769+
crypto::CheckThrow(env, SignBase::Error::kSignPrivateKey);
763770
return false;
764771
}
765772
data = MallocOpenSSL<unsigned char>(len);
766773
ByteSource buf =
767774
ByteSource::Allocated(reinterpret_cast<char*>(data), len);
768-
if (!EVP_DigestSignFinal(context.get(), data, &len))
775+
if (!EVP_DigestSignFinal(context.get(), data, &len)) {
776+
crypto::CheckThrow(env, SignBase::Error::kSignPrivateKey);
769777
return false;
778+
}
770779

771780
if (UseP1363Encoding(params.key, params.dsa_encoding)) {
772781
*out = ConvertSignatureToP1363(env, params.key, buf);

test/parallel/test-crypto-sign-verify.js

+14
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,17 @@ assert.throws(
742742
}
743743
}
744744
}
745+
746+
// The sign function should not swallow OpenSSL errors.
747+
// Regression test for https://github.com/nodejs/node/issues/40794.
748+
{
749+
assert.throws(() => {
750+
const { privateKey } = crypto.generateKeyPairSync('rsa', {
751+
modulusLength: 512
752+
});
753+
crypto.sign('sha512', 'message', privateKey);
754+
}, {
755+
code: 'ERR_OSSL_RSA_DIGEST_TOO_BIG_FOR_RSA_KEY',
756+
message: /digest too big for rsa key/
757+
});
758+
}

0 commit comments

Comments
 (0)