Skip to content

Commit 9a688d7

Browse files
tniessenmarco-ippolito
authored andcommitted
crypto: fix propagation of "memory limit exceeded"
When we throw ERR_CRYPTO_INVALID_SCRYPT_PARAMS after a call to EVP_PBE_scrypt, check if OpenSSL reported an error and if so, append the OpenSSL error message to the default generic error message. In particular, this catches cases when `maxmem` is not sufficient, which otherwise is difficult to identify because our documentation only provides an approximation of the required `maxmem` value. Fixes: #53291 PR-URL: #53300 Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent f363c70 commit 9a688d7

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/crypto/crypto_scrypt.cc

+11-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,17 @@ Maybe<bool> ScryptTraits::AdditionalConfig(
104104
params->maxmem,
105105
nullptr,
106106
0) != 1) {
107-
THROW_ERR_CRYPTO_INVALID_SCRYPT_PARAMS(env);
107+
// Do not use CryptoErrorStore or ThrowCryptoError here in order to maintain
108+
// backward compatibility with ERR_CRYPTO_INVALID_SCRYPT_PARAMS.
109+
uint32_t err = ERR_peek_last_error();
110+
if (err != 0) {
111+
char buf[256];
112+
ERR_error_string_n(err, buf, sizeof(buf));
113+
THROW_ERR_CRYPTO_INVALID_SCRYPT_PARAMS(
114+
env, "Invalid scrypt params: %s", buf);
115+
} else {
116+
THROW_ERR_CRYPTO_INVALID_SCRYPT_PARAMS(env);
117+
}
108118
return Nothing<bool>();
109119
}
110120

test/parallel/test-crypto-scrypt.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ for (const options of bad) {
178178

179179
for (const options of toobig) {
180180
const expected = {
181-
message: /Invalid scrypt param/
181+
message: /Invalid scrypt params:.*memory limit exceeded/,
182+
code: 'ERR_CRYPTO_INVALID_SCRYPT_PARAMS',
182183
};
183184
assert.throws(() => crypto.scrypt('pass', 'salt', 1, options, () => {}),
184185
expected);

0 commit comments

Comments
 (0)