Skip to content

Commit

Permalink
src: remove ERR prefix in WebCryptoKeyExportStatus
Browse files Browse the repository at this point in the history
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: #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>
  • Loading branch information
danbev authored and targos committed Nov 3, 2020
1 parent 3db4354 commit c922578
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,11 @@ WebCryptoKeyExportStatus DHKeyExportTraits::DoExport(
switch (format) {
case kWebCryptoKeyFormatPKCS8:
if (key_data->GetKeyType() != kKeyTypePrivate)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE;
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_PKCS8_Export(key_data.get(), out);
case kWebCryptoKeyFormatSPKI:
if (key_data->GetKeyType() != kKeyTypePublic)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE;
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_SPKI_Export(key_data.get(), out);
default:
UNREACHABLE();
Expand Down
6 changes: 3 additions & 3 deletions src/crypto/crypto_dsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ WebCryptoKeyExportStatus DSAKeyExportTraits::DoExport(
switch (format) {
case kWebCryptoKeyFormatRaw:
// Not supported for RSA keys of either type
return WebCryptoKeyExportStatus::ERR_FAILED;
return WebCryptoKeyExportStatus::FAILED;
case kWebCryptoKeyFormatPKCS8:
if (key_data->GetKeyType() != kKeyTypePrivate)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE;
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_PKCS8_Export(key_data.get(), out);
case kWebCryptoKeyFormatSPKI:
if (key_data->GetKeyType() != kKeyTypePublic)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE;
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_SPKI_Export(key_data.get(), out);
default:
UNREACHABLE();
Expand Down
12 changes: 6 additions & 6 deletions src/crypto/crypto_ecdh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -549,18 +549,18 @@ WebCryptoKeyExportStatus EC_Raw_Export(
// Get the allocated data size...
size_t len = EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr);
if (len == 0)
return WebCryptoKeyExportStatus::ERR_FAILED;
return WebCryptoKeyExportStatus::FAILED;

unsigned char* data = MallocOpenSSL<unsigned char>(len);
size_t check_len = EC_POINT_point2oct(group, point, form, data, len, nullptr);
if (check_len == 0)
return WebCryptoKeyExportStatus::ERR_FAILED;
return WebCryptoKeyExportStatus::FAILED;

CHECK_EQ(len, check_len);

*out = ByteSource::Allocated(reinterpret_cast<char*>(data), len);

return WebCryptoKeyExportStatus::ERR_OK;
return WebCryptoKeyExportStatus::OK;
}
} // namespace

Expand All @@ -581,15 +581,15 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport(
switch (format) {
case kWebCryptoKeyFormatRaw:
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);
case kWebCryptoKeyFormatPKCS8:
if (key_data->GetKeyType() != kKeyTypePrivate)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE;
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_PKCS8_Export(key_data.get(), out);
case kWebCryptoKeyFormatSPKI:
if (key_data->GetKeyType() != kKeyTypePublic)
return WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE;
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
return PKEY_SPKI_Export(key_data.get(), out);
default:
UNREACHABLE();
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/crypto_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1261,10 +1261,10 @@ WebCryptoKeyExportStatus PKEY_SPKI_Export(
CHECK_EQ(key_data->GetKeyType(), kKeyTypePublic);
BIOPointer bio(BIO_new(BIO_s_mem()));
if (!i2d_PUBKEY_bio(bio.get(), key_data->GetAsymmetricKey().get()))
return WebCryptoKeyExportStatus::ERR_FAILED;
return WebCryptoKeyExportStatus::FAILED;

*out = ByteSource::FromBIO(bio);
return WebCryptoKeyExportStatus::ERR_OK;
return WebCryptoKeyExportStatus::OK;
}

WebCryptoKeyExportStatus PKEY_PKCS8_Export(
Expand All @@ -1274,10 +1274,10 @@ WebCryptoKeyExportStatus PKEY_PKCS8_Export(
BIOPointer bio(BIO_new(BIO_s_mem()));
PKCS8Pointer p8inf(EVP_PKEY2PKCS8(key_data->GetAsymmetricKey().get()));
if (!i2d_PKCS8_PRIV_KEY_INFO_bio(bio.get(), p8inf.get()))
return WebCryptoKeyExportStatus::ERR_FAILED;
return WebCryptoKeyExportStatus::FAILED;

*out = ByteSource::FromBIO(bio);
return WebCryptoKeyExportStatus::ERR_OK;
return WebCryptoKeyExportStatus::OK;
}

namespace Keys {
Expand Down
12 changes: 6 additions & 6 deletions src/crypto/crypto_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ enum WebCryptoKeyFormat {
};

enum class WebCryptoKeyExportStatus {
ERR_OK,
ERR_INVALID_KEY_TYPE,
ERR_FAILED
OK,
INVALID_KEY_TYPE,
FAILED
};

template <typename KeyExportTraits>
Expand Down Expand Up @@ -336,13 +336,13 @@ class KeyExportJob final : public CryptoJob<KeyExportTraits> {
format_,
*CryptoJob<KeyExportTraits>::params(),
&out_)) {
case WebCryptoKeyExportStatus::ERR_OK:
case WebCryptoKeyExportStatus::OK:
// Success!
break;
case WebCryptoKeyExportStatus::ERR_INVALID_KEY_TYPE:
case WebCryptoKeyExportStatus::INVALID_KEY_TYPE:
// Fall through
// TODO(@jasnell): Separate error for this
case WebCryptoKeyExportStatus::ERR_FAILED: {
case WebCryptoKeyExportStatus::FAILED: {
CryptoErrorVector* errors = CryptoJob<KeyExportTraits>::errors();
errors->Capture();
if (errors->empty())
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/crypto_rsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ WebCryptoKeyExportStatus RSA_JWK_Export(
KeyObjectData* key_data,
const RSAKeyExportConfig& params,
ByteSource* out) {
return WebCryptoKeyExportStatus::ERR_FAILED;
return WebCryptoKeyExportStatus::FAILED;
}

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

0 comments on commit c922578

Please sign in to comment.