From 04fcd5d3bfa029743093d3a44ab244a8558de32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Mon, 20 Jan 2020 18:14:13 -0400 Subject: [PATCH 1/2] crypto: assign and use ERR_CRYPTO_UNKNOWN_CIPHER --- doc/api/errors.md | 5 +++++ src/node_crypto.cc | 6 +++--- src/node_errors.h | 2 ++ test/parallel/test-crypto-cipheriv-decipheriv.js | 6 +++++- test/parallel/test-crypto-keygen.js | 1 + 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 524daa21c515e0..e75cd820a730ef 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -831,6 +831,11 @@ A signing `key` was not provided to the [`sign.sign()`][] method. [`crypto.timingSafeEqual()`][] was called with `Buffer`, `TypedArray`, or `DataView` arguments of different lengths. + +### `ERR_CRYPTO_UNKNOWN_CIPHER` + +An unknown cipher was specified. + ### `ERR_CRYPTO_UNKNOWN_DH_GROUP` diff --git a/src/node_crypto.cc b/src/node_crypto.cc index f03c1e31b994f8..ad594af76742da 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -3549,7 +3549,7 @@ static NonCopyableMaybe GetPrivateKeyEncodingFromJs( args[*offset].As()); result.cipher_ = EVP_get_cipherbyname(*cipher_name); if (result.cipher_ == nullptr) { - env->ThrowError("Unknown cipher"); + THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env); return NonCopyableMaybe(); } needs_passphrase = true; @@ -4055,7 +4055,7 @@ void CipherBase::Init(const char* cipher_type, const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type); if (cipher == nullptr) - return env()->ThrowError("Unknown cipher"); + return THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env()); unsigned char key[EVP_MAX_KEY_LENGTH]; unsigned char iv[EVP_MAX_IV_LENGTH]; @@ -4119,7 +4119,7 @@ void CipherBase::InitIv(const char* cipher_type, const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type); if (cipher == nullptr) { - return env()->ThrowError("Unknown cipher"); + return THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env()); } const int expected_iv_len = EVP_CIPHER_iv_length(cipher); diff --git a/src/node_errors.h b/src/node_errors.h index 96ea94cb563f07..bc180b2a68efd7 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -39,6 +39,7 @@ void PrintErrorString(const char* format, ...); V(ERR_BUFFER_TOO_LARGE, Error) \ V(ERR_CONSTRUCT_CALL_REQUIRED, TypeError) \ V(ERR_CONSTRUCT_CALL_INVALID, TypeError) \ + V(ERR_CRYPTO_UNKNOWN_CIPHER, Error) \ V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \ V(ERR_INVALID_ARG_VALUE, TypeError) \ V(ERR_OSSL_EVP_INVALID_DIGEST, Error) \ @@ -90,6 +91,7 @@ void PrintErrorString(const char* format, ...); "Buffer is not available for the current Context") \ V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \ V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \ + V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher") \ V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \ V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \ V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \ diff --git a/test/parallel/test-crypto-cipheriv-decipheriv.js b/test/parallel/test-crypto-cipheriv-decipheriv.js index e2279a689c05ec..07591fdfffd072 100644 --- a/test/parallel/test-crypto-cipheriv-decipheriv.js +++ b/test/parallel/test-crypto-cipheriv-decipheriv.js @@ -210,7 +210,11 @@ for (let n = 1; n < 256; n += 1) { // Passing an invalid cipher name should throw. assert.throws( () => crypto.createCipheriv('aes-127', Buffer.alloc(16), null), - /Unknown cipher/); + { + name: 'Error', + code: 'ERR_CRYPTO_UNKNOWN_CIPHER', + message: 'Unknown cipher' + }); // Passing a key with an invalid length should throw. assert.throws( diff --git a/test/parallel/test-crypto-keygen.js b/test/parallel/test-crypto-keygen.js index e052c9a16ab312..6a0ef2792d5ddf 100644 --- a/test/parallel/test-crypto-keygen.js +++ b/test/parallel/test-crypto-keygen.js @@ -822,6 +822,7 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher); } }), { name: 'Error', + code: 'ERR_CRYPTO_UNKNOWN_CIPHER', message: 'Unknown cipher' }); From 8bba027926380d361cd8b253c6eabeced9e52a2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Tue, 21 Jan 2020 14:31:27 -0400 Subject: [PATCH 2/2] fixup! crypto: assign and use ERR_CRYPTO_UNKNOWN_CIPHER Co-Authored-By: Colin Ihrig --- doc/api/errors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index e75cd820a730ef..cee8f2ba04ec66 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -831,7 +831,7 @@ A signing `key` was not provided to the [`sign.sign()`][] method. [`crypto.timingSafeEqual()`][] was called with `Buffer`, `TypedArray`, or `DataView` arguments of different lengths. - + ### `ERR_CRYPTO_UNKNOWN_CIPHER` An unknown cipher was specified.