src: remove ERR prefix in WebCryptoKeyExportStatus

This commit suggests removing the ERR prefix in the
WebCryptoKeyExportStatus enum.

The motivation for this is that I think it improves the readability of
the code. For example, the following line had me look twice to see what
was going on:

  case WebCryptoKeyExportStatus::ERR_OK:
  // Success!

PR-URL: https://github.com/nodejs/node/pull/35639
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Daniel Bevenius 2020-10-14 12:36:56 +02:00
parent cb62f16164
commit 923f76d523
6 changed files with 25 additions and 25 deletions

View File

@ -524,11 +524,11 @@ WebCryptoKeyExportStatus DHKeyExportTraits::DoExport(
switch (format) { switch (format) {
case kWebCryptoKeyFormatPKCS8: case kWebCryptoKeyFormatPKCS8:
if (key_data->GetKeyType() != kKeyTypePrivate) if (key_data->GetKeyType() != kKeyTypePrivate)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE; return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_PKCS8_Export(key_data.get(), out); return PKEY_PKCS8_Export(key_data.get(), out);
case kWebCryptoKeyFormatSPKI: case kWebCryptoKeyFormatSPKI:
if (key_data->GetKeyType() != kKeyTypePublic) if (key_data->GetKeyType() != kKeyTypePublic)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE; return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_SPKI_Export(key_data.get(), out); return PKEY_SPKI_Export(key_data.get(), out);
default: default:
UNREACHABLE(); UNREACHABLE();

View File

@ -113,14 +113,14 @@ WebCryptoKeyExportStatus DSAKeyExportTraits::DoExport(
switch (format) { switch (format) {
case kWebCryptoKeyFormatRaw: case kWebCryptoKeyFormatRaw:
// Not supported for RSA keys of either type // Not supported for RSA keys of either type
return WebCryptoKeyExportStatus::ERR_FAILED; return WebCryptoKeyExportStatus::FAILED;
case kWebCryptoKeyFormatPKCS8: case kWebCryptoKeyFormatPKCS8:
if (key_data->GetKeyType() != kKeyTypePrivate) if (key_data->GetKeyType() != kKeyTypePrivate)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE; return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_PKCS8_Export(key_data.get(), out); return PKEY_PKCS8_Export(key_data.get(), out);
case kWebCryptoKeyFormatSPKI: case kWebCryptoKeyFormatSPKI:
if (key_data->GetKeyType() != kKeyTypePublic) if (key_data->GetKeyType() != kKeyTypePublic)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE; return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_SPKI_Export(key_data.get(), out); return PKEY_SPKI_Export(key_data.get(), out);
default: default:
UNREACHABLE(); UNREACHABLE();

View File

@ -549,18 +549,18 @@ WebCryptoKeyExportStatus EC_Raw_Export(
// Get the allocated data size... // Get the allocated data size...
size_t len = EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr); size_t len = EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr);
if (len == 0) if (len == 0)
return WebCryptoKeyExportStatus::ERR_FAILED; return WebCryptoKeyExportStatus::FAILED;
unsigned char* data = MallocOpenSSL<unsigned char>(len); unsigned char* data = MallocOpenSSL<unsigned char>(len);
size_t check_len = EC_POINT_point2oct(group, point, form, data, len, nullptr); size_t check_len = EC_POINT_point2oct(group, point, form, data, len, nullptr);
if (check_len == 0) if (check_len == 0)
return WebCryptoKeyExportStatus::ERR_FAILED; return WebCryptoKeyExportStatus::FAILED;
CHECK_EQ(len, check_len); CHECK_EQ(len, check_len);
*out = ByteSource::Allocated(reinterpret_cast<char*>(data), len); *out = ByteSource::Allocated(reinterpret_cast<char*>(data), len);
return WebCryptoKeyExportStatus::ERR_OK; return WebCryptoKeyExportStatus::OK;
} }
} // namespace } // namespace
@ -581,15 +581,15 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport(
switch (format) { switch (format) {
case kWebCryptoKeyFormatRaw: case kWebCryptoKeyFormatRaw:
if (key_data->GetKeyType() != kKeyTypePublic) if (key_data->GetKeyType() != kKeyTypePublic)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE; return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return EC_Raw_Export(key_data.get(), params, out); return EC_Raw_Export(key_data.get(), params, out);
case kWebCryptoKeyFormatPKCS8: case kWebCryptoKeyFormatPKCS8:
if (key_data->GetKeyType() != kKeyTypePrivate) if (key_data->GetKeyType() != kKeyTypePrivate)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE; return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_PKCS8_Export(key_data.get(), out); return PKEY_PKCS8_Export(key_data.get(), out);
case kWebCryptoKeyFormatSPKI: case kWebCryptoKeyFormatSPKI:
if (key_data->GetKeyType() != kKeyTypePublic) if (key_data->GetKeyType() != kKeyTypePublic)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE; return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_SPKI_Export(key_data.get(), out); return PKEY_SPKI_Export(key_data.get(), out);
default: default:
UNREACHABLE(); UNREACHABLE();

View File

@ -1261,10 +1261,10 @@ WebCryptoKeyExportStatus PKEY_SPKI_Export(
CHECK_EQ(key_data->GetKeyType(), kKeyTypePublic); CHECK_EQ(key_data->GetKeyType(), kKeyTypePublic);
BIOPointer bio(BIO_new(BIO_s_mem())); BIOPointer bio(BIO_new(BIO_s_mem()));
if (!i2d_PUBKEY_bio(bio.get(), key_data->GetAsymmetricKey().get())) if (!i2d_PUBKEY_bio(bio.get(), key_data->GetAsymmetricKey().get()))
return WebCryptoKeyExportStatus::ERR_FAILED; return WebCryptoKeyExportStatus::FAILED;
*out = ByteSource::FromBIO(bio); *out = ByteSource::FromBIO(bio);
return WebCryptoKeyExportStatus::ERR_OK; return WebCryptoKeyExportStatus::OK;
} }
WebCryptoKeyExportStatus PKEY_PKCS8_Export( WebCryptoKeyExportStatus PKEY_PKCS8_Export(
@ -1274,10 +1274,10 @@ WebCryptoKeyExportStatus PKEY_PKCS8_Export(
BIOPointer bio(BIO_new(BIO_s_mem())); BIOPointer bio(BIO_new(BIO_s_mem()));
PKCS8Pointer p8inf(EVP_PKEY2PKCS8(key_data->GetAsymmetricKey().get())); PKCS8Pointer p8inf(EVP_PKEY2PKCS8(key_data->GetAsymmetricKey().get()));
if (!i2d_PKCS8_PRIV_KEY_INFO_bio(bio.get(), p8inf.get())) if (!i2d_PKCS8_PRIV_KEY_INFO_bio(bio.get(), p8inf.get()))
return WebCryptoKeyExportStatus::ERR_FAILED; return WebCryptoKeyExportStatus::FAILED;
*out = ByteSource::FromBIO(bio); *out = ByteSource::FromBIO(bio);
return WebCryptoKeyExportStatus::ERR_OK; return WebCryptoKeyExportStatus::OK;
} }
namespace Keys { namespace Keys {

View File

@ -262,9 +262,9 @@ enum WebCryptoKeyFormat {
}; };
enum class WebCryptoKeyExportStatus { enum class WebCryptoKeyExportStatus {
ERR_OK, OK,
ERR_INVALID_KEY_TYPE, INVALID_KEY_TYPE,
ERR_FAILED FAILED
}; };
template <typename KeyExportTraits> template <typename KeyExportTraits>
@ -336,13 +336,13 @@ class KeyExportJob final : public CryptoJob<KeyExportTraits> {
format_, format_,
*CryptoJob<KeyExportTraits>::params(), *CryptoJob<KeyExportTraits>::params(),
&out_)) { &out_)) {
case WebCryptoKeyExportStatus::ERR_OK: case WebCryptoKeyExportStatus::OK:
// Success! // Success!
break; break;
case WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE: case WebCryptoKeyExportStatus::INVALID_KEY_TYPE:
// Fall through // Fall through
// TODO(@jasnell): Separate error for this // TODO(@jasnell): Separate error for this
case WebCryptoKeyExportStatus::ERR_FAILED: { case WebCryptoKeyExportStatus::FAILED: {
CryptoErrorVector* errors = CryptoJob<KeyExportTraits>::errors(); CryptoErrorVector* errors = CryptoJob<KeyExportTraits>::errors();
errors->Capture(); errors->Capture();
if (errors->empty()) if (errors->empty())

View File

@ -179,7 +179,7 @@ WebCryptoKeyExportStatus RSA_JWK_Export(
KeyObjectData* key_data, KeyObjectData* key_data,
const RSAKeyExportConfig& params, const RSAKeyExportConfig& params,
ByteSource* out) { ByteSource* out) {
return WebCryptoKeyExportStatus::ERR_FAILED; return WebCryptoKeyExportStatus::FAILED;
} }
template <PublicKeyCipher::EVP_PKEY_cipher_init_t init, template <PublicKeyCipher::EVP_PKEY_cipher_init_t init,
@ -268,16 +268,16 @@ WebCryptoKeyExportStatus RSAKeyExportTraits::DoExport(
switch (format) { switch (format) {
case kWebCryptoKeyFormatRaw: case kWebCryptoKeyFormatRaw:
// Not supported for RSA keys of either type // Not supported for RSA keys of either type
return WebCryptoKeyExportStatus::ERR_FAILED; return WebCryptoKeyExportStatus::FAILED;
case kWebCryptoKeyFormatJWK: case kWebCryptoKeyFormatJWK:
return RSA_JWK_Export(key_data.get(), params, out); return RSA_JWK_Export(key_data.get(), params, out);
case kWebCryptoKeyFormatPKCS8: case kWebCryptoKeyFormatPKCS8:
if (key_data->GetKeyType() != kKeyTypePrivate) if (key_data->GetKeyType() != kKeyTypePrivate)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE; return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_PKCS8_Export(key_data.get(), out); return PKEY_PKCS8_Export(key_data.get(), out);
case kWebCryptoKeyFormatSPKI: case kWebCryptoKeyFormatSPKI:
if (key_data->GetKeyType() != kKeyTypePublic) if (key_data->GetKeyType() != kKeyTypePublic)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE; return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_SPKI_Export(key_data.get(), out); return PKEY_SPKI_Export(key_data.get(), out);
default: default:
UNREACHABLE(); UNREACHABLE();