Skip to content

Commit 9718497

Browse files
tniessentargos
authored andcommitted
crypto: assign and use ERR_CRYPTO_UNKNOWN_CIPHER
PR-URL: nodejs#31437 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7f34dda commit 9718497

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

doc/api/errors.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,11 @@ A signing `key` was not provided to the [`sign.sign()`][] method.
829829
[`crypto.timingSafeEqual()`][] was called with `Buffer`, `TypedArray`, or
830830
`DataView` arguments of different lengths.
831831

832+
<a id="ERR_CRYPTO_UNKNOWN_CIPHER"></a>
833+
### `ERR_CRYPTO_UNKNOWN_CIPHER`
834+
835+
An unknown cipher was specified.
836+
832837
<a id="ERR_CRYPTO_UNKNOWN_DH_GROUP"></a>
833838
### `ERR_CRYPTO_UNKNOWN_DH_GROUP`
834839

src/node_crypto.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2966,7 +2966,7 @@ static NonCopyableMaybe<PrivateKeyEncodingConfig> GetPrivateKeyEncodingFromJs(
29662966
args[*offset].As<String>());
29672967
result.cipher_ = EVP_get_cipherbyname(*cipher_name);
29682968
if (result.cipher_ == nullptr) {
2969-
env->ThrowError("Unknown cipher");
2969+
THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env);
29702970
return NonCopyableMaybe<PrivateKeyEncodingConfig>();
29712971
}
29722972
needs_passphrase = true;
@@ -3492,7 +3492,7 @@ void CipherBase::Init(const char* cipher_type,
34923492

34933493
const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type);
34943494
if (cipher == nullptr)
3495-
return env()->ThrowError("Unknown cipher");
3495+
return THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env());
34963496

34973497
unsigned char key[EVP_MAX_KEY_LENGTH];
34983498
unsigned char iv[EVP_MAX_IV_LENGTH];
@@ -3556,7 +3556,7 @@ void CipherBase::InitIv(const char* cipher_type,
35563556

35573557
const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type);
35583558
if (cipher == nullptr) {
3559-
return env()->ThrowError("Unknown cipher");
3559+
return THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env());
35603560
}
35613561

35623562
const int expected_iv_len = EVP_CIPHER_iv_length(cipher);

src/node_errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void OnFatalError(const char* location, const char* message);
3737
V(ERR_BUFFER_TOO_LARGE, Error) \
3838
V(ERR_CONSTRUCT_CALL_REQUIRED, TypeError) \
3939
V(ERR_CONSTRUCT_CALL_INVALID, TypeError) \
40+
V(ERR_CRYPTO_UNKNOWN_CIPHER, Error) \
4041
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \
4142
V(ERR_INVALID_ARG_VALUE, TypeError) \
4243
V(ERR_OSSL_EVP_INVALID_DIGEST, Error) \
@@ -90,6 +91,7 @@ void OnFatalError(const char* location, const char* message);
9091
"Buffer is not available for the current Context") \
9192
V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \
9293
V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \
94+
V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher") \
9395
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \
9496
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
9597
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \

test/parallel/test-crypto-cipheriv-decipheriv.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ for (let n = 1; n < 256; n += 1) {
210210
// Passing an invalid cipher name should throw.
211211
assert.throws(
212212
() => crypto.createCipheriv('aes-127', Buffer.alloc(16), null),
213-
/Unknown cipher/);
213+
{
214+
name: 'Error',
215+
code: 'ERR_CRYPTO_UNKNOWN_CIPHER',
216+
message: 'Unknown cipher'
217+
});
214218

215219
// Passing a key with an invalid length should throw.
216220
assert.throws(

test/parallel/test-crypto-keygen.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
822822
}
823823
}), {
824824
name: 'Error',
825+
code: 'ERR_CRYPTO_UNKNOWN_CIPHER',
825826
message: 'Unknown cipher'
826827
});
827828

0 commit comments

Comments
 (0)