Skip to content

Commit 4591202

Browse files
tniessencodebytere
authored andcommitted
crypto: assign and use ERR_CRYPTO_UNKNOWN_CIPHER
PR-URL: #31437 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b29bada commit 4591202

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
@@ -3534,7 +3534,7 @@ static NonCopyableMaybe<PrivateKeyEncodingConfig> GetPrivateKeyEncodingFromJs(
35343534
args[*offset].As<String>());
35353535
result.cipher_ = EVP_get_cipherbyname(*cipher_name);
35363536
if (result.cipher_ == nullptr) {
3537-
env->ThrowError("Unknown cipher");
3537+
THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env);
35383538
return NonCopyableMaybe<PrivateKeyEncodingConfig>();
35393539
}
35403540
needs_passphrase = true;
@@ -4040,7 +4040,7 @@ void CipherBase::Init(const char* cipher_type,
40404040

40414041
const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type);
40424042
if (cipher == nullptr)
4043-
return env()->ThrowError("Unknown cipher");
4043+
return THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env());
40444044

40454045
unsigned char key[EVP_MAX_KEY_LENGTH];
40464046
unsigned char iv[EVP_MAX_IV_LENGTH];
@@ -4104,7 +4104,7 @@ void CipherBase::InitIv(const char* cipher_type,
41044104

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

41104110
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)