Skip to content

Commit a78327f

Browse files
committed
crypto: migrate setEngine to internal/errors
PR-URL: #16429 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent d2e44d5 commit a78327f

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

doc/api/errors.md

+7
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,12 @@ Used when the native call from `process.cpuUsage` cannot be processed properly.
637637
Used when an invalid value for the `format` argument has been passed to the
638638
`crypto.ECDH()` class `getPublicKey()` method.
639639

640+
<a id="ERR_CRYPTO_ENGINE_UNKNOWN"></a>
641+
### ERR_CRYPTO_ENGINE_UNKNOWN
642+
643+
Used when an invalid crypto engine identifier is passed to
644+
[`require('crypto').setEngine()`][].
645+
640646
<a id="ERR_CRYPTO_INVALID_DIGEST"></a>
641647
### ERR_CRYPTO_INVALID_DIGEST
642648

@@ -1357,6 +1363,7 @@ closed.
13571363
[`new URLSearchParams(iterable)`]: url.html#url_constructor_new_urlsearchparams_iterable
13581364
[`process.on('uncaughtException')`]: process.html#process_event_uncaughtexception
13591365
[`process.send()`]: process.html#process_process_send_message_sendhandle_options_callback
1366+
[`require('crypto').setEngine()`]: crypto.html#crypto_crypto_setengine_engine_flags
13601367
[Node.js Error Codes]: #nodejs-error-codes
13611368
[V8's stack trace API]: https://github.com/v8/v8/wiki/Stack-Trace-API
13621369
[WHATWG URL API]: url.html#url_the_whatwg_url_api

lib/internal/crypto/util.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ function setEngine(id, flags) {
5656
if (flags === 0)
5757
flags = ENGINE_METHOD_ALL;
5858

59-
return _setEngine(id, flags);
59+
if (!_setEngine(id, flags))
60+
throw new errors.Error('ERR_CRYPTO_ENGINE_UNKNOWN', id);
6061
}
6162

6263
module.exports = {

lib/internal/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ E('ERR_CONSOLE_WRITABLE_STREAM',
154154
'Console expects a writable stream instance for %s');
155155
E('ERR_CPU_USAGE', 'Unable to obtain cpu usage %s');
156156
E('ERR_CRYPTO_ECDH_INVALID_FORMAT', 'Invalid ECDH format: %s');
157+
E('ERR_CRYPTO_ENGINE_UNKNOWN', 'Engine "%s" was not found');
157158
E('ERR_CRYPTO_HASH_DIGEST_NO_UTF16', 'hash.digest() does not support UTF-16');
158159
E('ERR_CRYPTO_HASH_FINALIZED', 'Digest already called');
159160
E('ERR_CRYPTO_HASH_UPDATE_FAILED', 'Hash update failed');

src/node_crypto.cc

+5-7
Original file line numberDiff line numberDiff line change
@@ -5946,19 +5946,17 @@ void SetEngine(const FunctionCallbackInfo<Value>& args) {
59465946

59475947
if (engine == nullptr) {
59485948
int err = ERR_get_error();
5949-
if (err == 0) {
5950-
char tmp[1024];
5951-
snprintf(tmp, sizeof(tmp), "Engine \"%s\" was not found", *engine_id);
5952-
return env->ThrowError(tmp);
5953-
} else {
5954-
return ThrowCryptoError(env, err);
5955-
}
5949+
if (err == 0)
5950+
return args.GetReturnValue().Set(false);
5951+
return ThrowCryptoError(env, err);
59565952
}
59575953

59585954
int r = ENGINE_set_default(engine, flags);
59595955
ENGINE_free(engine);
59605956
if (r == 0)
59615957
return ThrowCryptoError(env, ERR_get_error());
5958+
5959+
args.GetReturnValue().Set(true);
59625960
}
59635961
#endif // !OPENSSL_NO_ENGINE
59645962

0 commit comments

Comments
 (0)