Skip to content

Commit 8c313ce

Browse files
tniessenTrott
authored andcommitted
crypto: assign and use ERR_CRYPTO_UNKNOWN_CIPHER
PR-URL: #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 085a5c7 commit 8c313ce

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
@@ -831,6 +831,11 @@ A signing `key` was not provided to the [`sign.sign()`][] method.
831831
[`crypto.timingSafeEqual()`][] was called with `Buffer`, `TypedArray`, or
832832
`DataView` arguments of different lengths.
833833

834+
<a id="ERR_CRYPTO_UNKNOWN_CIPHER"></a>
835+
### `ERR_CRYPTO_UNKNOWN_CIPHER`
836+
837+
An unknown cipher was specified.
838+
834839
<a id="ERR_CRYPTO_UNKNOWN_DH_GROUP"></a>
835840
### `ERR_CRYPTO_UNKNOWN_DH_GROUP`
836841

src/node_crypto.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,7 +3531,7 @@ static NonCopyableMaybe<PrivateKeyEncodingConfig> GetPrivateKeyEncodingFromJs(
35313531
args[*offset].As<String>());
35323532
result.cipher_ = EVP_get_cipherbyname(*cipher_name);
35333533
if (result.cipher_ == nullptr) {
3534-
env->ThrowError("Unknown cipher");
3534+
THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env);
35353535
return NonCopyableMaybe<PrivateKeyEncodingConfig>();
35363536
}
35373537
needs_passphrase = true;
@@ -4037,7 +4037,7 @@ void CipherBase::Init(const char* cipher_type,
40374037

40384038
const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type);
40394039
if (cipher == nullptr)
4040-
return env()->ThrowError("Unknown cipher");
4040+
return THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env());
40414041

40424042
unsigned char key[EVP_MAX_KEY_LENGTH];
40434043
unsigned char iv[EVP_MAX_IV_LENGTH];
@@ -4101,7 +4101,7 @@ void CipherBase::InitIv(const char* cipher_type,
41014101

41024102
const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type);
41034103
if (cipher == nullptr) {
4104-
return env()->ThrowError("Unknown cipher");
4104+
return THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env());
41054105
}
41064106

41074107
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
@@ -39,6 +39,7 @@ void PrintErrorString(const char* format, ...);
3939
V(ERR_BUFFER_TOO_LARGE, Error) \
4040
V(ERR_CONSTRUCT_CALL_REQUIRED, TypeError) \
4141
V(ERR_CONSTRUCT_CALL_INVALID, TypeError) \
42+
V(ERR_CRYPTO_UNKNOWN_CIPHER, Error) \
4243
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \
4344
V(ERR_INVALID_ARG_VALUE, TypeError) \
4445
V(ERR_OSSL_EVP_INVALID_DIGEST, Error) \
@@ -90,6 +91,7 @@ void PrintErrorString(const char* format, ...);
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)