From 33180fad81970b7f959cae79d2163985dc8b8399 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Mon, 1 Mar 2021 19:32:50 +0530 Subject: [PATCH] crypto: add separate error for INVALID_KEY_TYPE PR-URL: https://github.com/nodejs/node/pull/37555 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- src/crypto/crypto_cipher.h | 43 ++++++++++++++++++++++---------------- src/crypto/crypto_keys.h | 39 ++++++++++++++++++++-------------- 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/src/crypto/crypto_cipher.h b/src/crypto/crypto_cipher.h index bae187a6e139a3..70dea9ff3c5531 100644 --- a/src/crypto/crypto_cipher.h +++ b/src/crypto/crypto_cipher.h @@ -215,24 +215,31 @@ class CipherJob final : public CryptoJob { WebCryptoCipherMode cipher_mode() const { return cipher_mode_; } void DoThreadPoolWork() override { - switch (CipherTraits::DoCipher( - AsyncWrap::env(), - key(), - cipher_mode_, - *CryptoJob::params(), - in_, - &out_)) { - case WebCryptoCipherStatus::OK: - // Success! - break; - case WebCryptoCipherStatus::INVALID_KEY_TYPE: - // Fall through - // TODO(@jasnell): Separate error for this - case WebCryptoCipherStatus::FAILED: { - CryptoErrorVector* errors = CryptoJob::errors(); - errors->Capture(); - if (errors->empty()) - errors->push_back(std::string("Cipher job failed.")); + const WebCryptoCipherStatus status = + CipherTraits::DoCipher( + AsyncWrap::env(), + key(), + cipher_mode_, + *CryptoJob::params(), + in_, + &out_); + if (status == WebCryptoCipherStatus::OK) { + // Success! + return; + } + CryptoErrorVector* errors = CryptoJob::errors(); + errors->Capture(); + if (errors->empty()) { + switch (status) { + case WebCryptoCipherStatus::OK: + UNREACHABLE(); + break; + case WebCryptoCipherStatus::INVALID_KEY_TYPE: + errors->emplace_back("Invalid key type."); + break; + case WebCryptoCipherStatus::FAILED: + errors->emplace_back("Cipher job failed."); + break; } } } diff --git a/src/crypto/crypto_keys.h b/src/crypto/crypto_keys.h index 5f9fcc8510a781..03d1f9a33b1b61 100644 --- a/src/crypto/crypto_keys.h +++ b/src/crypto/crypto_keys.h @@ -337,22 +337,29 @@ class KeyExportJob final : public CryptoJob { WebCryptoKeyFormat format() const { return format_; } void DoThreadPoolWork() override { - switch (KeyExportTraits::DoExport( - key_, - format_, - *CryptoJob::params(), - &out_)) { - case WebCryptoKeyExportStatus::OK: - // Success! - break; - case WebCryptoKeyExportStatus::INVALID_KEY_TYPE: - // Fall through - // TODO(@jasnell): Separate error for this - case WebCryptoKeyExportStatus::FAILED: { - CryptoErrorVector* errors = CryptoJob::errors(); - errors->Capture(); - if (errors->empty()) - errors->push_back("Key export failed."); + const WebCryptoKeyExportStatus status = + KeyExportTraits::DoExport( + key_, + format_, + *CryptoJob::params(), + &out_); + if (status == WebCryptoKeyExportStatus::OK) { + // Success! + return; + } + CryptoErrorVector* errors = CryptoJob::errors(); + errors->Capture(); + if (errors->empty()) { + switch (status) { + case WebCryptoKeyExportStatus::OK: + UNREACHABLE(); + break; + case WebCryptoKeyExportStatus::INVALID_KEY_TYPE: + errors->emplace_back("Invalid key type."); + break; + case WebCryptoKeyExportStatus::FAILED: + errors->emplace_back("Cipher job failed."); + break; } } }