Skip to content

Commit

Permalink
src: use Maybe<void> in ManagedEVPPKey
Browse files Browse the repository at this point in the history
With recent versions of V8, it is not necessary to use Maybe<bool>
anymore. This changes member functions of ManagedEVPPKey to use
Maybe<void> instead, as well as (transitive) dependencies.

PR-URL: #53811
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
tniessen authored and marco-ippolito committed Aug 19, 2024
1 parent 0b6d1db commit d9c0486
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 57 deletions.
19 changes: 9 additions & 10 deletions src/crypto/crypto_ec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -840,10 +840,9 @@ Maybe<void> ExportJWKEcKey(
return JustVoid();
}

Maybe<bool> ExportJWKEdKey(
Environment* env,
std::shared_ptr<KeyObjectData> key,
Local<Object> target) {
Maybe<void> ExportJWKEdKey(Environment* env,
std::shared_ptr<KeyObjectData> key,
Local<Object> target) {
ManagedEVPPKey pkey = key->GetAsymmetricKey();
Mutex::ScopedLock lock(*pkey.mutex());

Expand All @@ -868,15 +867,15 @@ Maybe<bool> ExportJWKEdKey(
env->context(),
env->jwk_crv_string(),
OneByteString(env->isolate(), curve)).IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}

size_t len = 0;
Local<Value> encoded;
Local<Value> error;

if (!EVP_PKEY_get_raw_public_key(pkey.get(), nullptr, &len))
return Nothing<bool>();
return Nothing<void>();

ByteSource::Builder out(len);

Expand All @@ -889,7 +888,7 @@ Maybe<bool> ExportJWKEdKey(
!target->Set(env->context(), env->jwk_d_string(), encoded).IsJust()) {
if (!error.IsEmpty())
env->isolate()->ThrowException(error);
return Nothing<bool>();
return Nothing<void>();
}
}

Expand All @@ -901,17 +900,17 @@ Maybe<bool> ExportJWKEdKey(
!target->Set(env->context(), env->jwk_x_string(), encoded).IsJust()) {
if (!error.IsEmpty())
env->isolate()->ThrowException(error);
return Nothing<bool>();
return Nothing<void>();
}

if (target->Set(
env->context(),
env->jwk_kty_string(),
env->jwk_okp_string()).IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}

return Just(true);
return JustVoid();
}

std::shared_ptr<KeyObjectData> ImportJWKEcKey(
Expand Down
7 changes: 3 additions & 4 deletions src/crypto/crypto_ec.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,9 @@ v8::Maybe<void> ExportJWKEcKey(
std::shared_ptr<KeyObjectData> key,
v8::Local<v8::Object> target);

v8::Maybe<bool> ExportJWKEdKey(
Environment* env,
std::shared_ptr<KeyObjectData> key,
v8::Local<v8::Object> target);
v8::Maybe<void> ExportJWKEdKey(Environment* env,
std::shared_ptr<KeyObjectData> key,
v8::Local<v8::Object> target);

std::shared_ptr<KeyObjectData> ImportJWKEcKey(
Environment* env,
Expand Down
56 changes: 27 additions & 29 deletions src/crypto/crypto_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ using v8::FunctionTemplate;
using v8::Int32;
using v8::Isolate;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
using v8::MaybeLocal;
Expand Down Expand Up @@ -431,10 +432,9 @@ MaybeLocal<Value> WritePublicKey(Environment* env,
return BIOToStringOrBuffer(env, bio.get(), config.format_);
}

Maybe<bool> ExportJWKSecretKey(
Environment* env,
std::shared_ptr<KeyObjectData> key,
Local<Object> target) {
Maybe<void> ExportJWKSecretKey(Environment* env,
std::shared_ptr<KeyObjectData> key,
Local<Object> target) {
CHECK_EQ(key->GetKeyType(), kKeyTypeSecret);

Local<Value> error;
Expand All @@ -449,10 +449,9 @@ Maybe<bool> ExportJWKSecretKey(
if (key_data.IsEmpty()) {
CHECK(!error.IsEmpty());
env->isolate()->ThrowException(error);
return Nothing<bool>();
return Nothing<void>();
}
if (!key_data.ToLocal(&raw))
return Nothing<bool>();
if (!key_data.ToLocal(&raw)) return Nothing<void>();

if (target->Set(
env->context(),
Expand All @@ -462,10 +461,10 @@ Maybe<bool> ExportJWKSecretKey(
env->context(),
env->jwk_k_string(),
raw).IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}

return Just(true);
return JustVoid();
}

std::shared_ptr<KeyObjectData> ImportJWKSecretKey(
Expand All @@ -483,19 +482,18 @@ std::shared_ptr<KeyObjectData> ImportJWKSecretKey(
return KeyObjectData::CreateSecret(std::move(key_data));
}

Maybe<bool> ExportJWKAsymmetricKey(
Environment* env,
std::shared_ptr<KeyObjectData> key,
Local<Object> target,
bool handleRsaPss) {
Maybe<void> ExportJWKAsymmetricKey(Environment* env,
std::shared_ptr<KeyObjectData> key,
Local<Object> target,
bool handleRsaPss) {
switch (EVP_PKEY_id(key->GetAsymmetricKey().get())) {
case EVP_PKEY_RSA_PSS: {
if (handleRsaPss) return ExportJWKRsaKey(env, key, target);
break;
}
case EVP_PKEY_RSA: return ExportJWKRsaKey(env, key, target);
case EVP_PKEY_EC: return ExportJWKEcKey(env, key, target).IsJust() ?
Just(true) : Nothing<bool>();
case EVP_PKEY_EC:
return ExportJWKEcKey(env, key, target);
case EVP_PKEY_ED25519:
// Fall through
case EVP_PKEY_ED448:
Expand All @@ -505,7 +503,7 @@ Maybe<bool> ExportJWKAsymmetricKey(
case EVP_PKEY_X448: return ExportJWKEdKey(env, key, target);
}
THROW_ERR_CRYPTO_JWK_UNSUPPORTED_KEY_TYPE(env);
return Nothing<bool>();
return Nothing<void>();
}

std::shared_ptr<KeyObjectData> ImportJWKAsymmetricKey(
Expand Down Expand Up @@ -605,12 +603,12 @@ size_t ManagedEVPPKey::size_of_public_key() const {
pkey_.get(), nullptr, &len) == 1) ? len : 0;
}

// This maps true to Just<bool>(true) and false to Nothing<bool>().
static inline Maybe<bool> Tristate(bool b) {
return b ? Just(true) : Nothing<bool>();
// This maps true to JustVoid and false to Nothing<void>().
static inline Maybe<void> NothingIfFalse(bool b) {
return b ? JustVoid() : Nothing<void>();
}

Maybe<bool> ExportJWKInner(Environment* env,
Maybe<void> ExportJWKInner(Environment* env,
std::shared_ptr<KeyObjectData> key,
Local<Value> result,
bool handleRsaPss) {
Expand All @@ -627,44 +625,44 @@ Maybe<bool> ExportJWKInner(Environment* env,
}
}

Maybe<bool> ManagedEVPPKey::ToEncodedPublicKey(
Maybe<void> ManagedEVPPKey::ToEncodedPublicKey(
Environment* env,
const PublicKeyEncodingConfig& config,
Local<Value>* out) {
if (!*this) return Nothing<bool>();
if (!*this) return Nothing<void>();
if (config.output_key_object_) {
// Note that this has the downside of containing sensitive data of the
// private key.
std::shared_ptr<KeyObjectData> data =
KeyObjectData::CreateAsymmetric(kKeyTypePublic, *this);
return Tristate(KeyObjectHandle::Create(env, data).ToLocal(out));
return NothingIfFalse(KeyObjectHandle::Create(env, data).ToLocal(out));
} else if (config.format_ == kKeyFormatJWK) {
std::shared_ptr<KeyObjectData> data =
KeyObjectData::CreateAsymmetric(kKeyTypePublic, *this);
*out = Object::New(env->isolate());
return ExportJWKInner(env, data, *out, false);
}

return Tristate(WritePublicKey(env, get(), config).ToLocal(out));
return NothingIfFalse(WritePublicKey(env, get(), config).ToLocal(out));
}

Maybe<bool> ManagedEVPPKey::ToEncodedPrivateKey(
Maybe<void> ManagedEVPPKey::ToEncodedPrivateKey(
Environment* env,
const PrivateKeyEncodingConfig& config,
Local<Value>* out) {
if (!*this) return Nothing<bool>();
if (!*this) return Nothing<void>();
if (config.output_key_object_) {
std::shared_ptr<KeyObjectData> data =
KeyObjectData::CreateAsymmetric(kKeyTypePrivate, *this);
return Tristate(KeyObjectHandle::Create(env, data).ToLocal(out));
return NothingIfFalse(KeyObjectHandle::Create(env, data).ToLocal(out));
} else if (config.format_ == kKeyFormatJWK) {
std::shared_ptr<KeyObjectData> data =
KeyObjectData::CreateAsymmetric(kKeyTypePrivate, *this);
*out = Object::New(env->isolate());
return ExportJWKInner(env, data, *out, false);
}

return Tristate(WritePrivateKey(env, get(), config).ToLocal(out));
return NothingIfFalse(WritePrivateKey(env, get(), config).ToLocal(out));
}

NonCopyableMaybe<PrivateKeyEncodingConfig>
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/crypto_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ class ManagedEVPPKey : public MemoryRetainer {
unsigned int* offset,
bool allow_key_object);

v8::Maybe<bool> ToEncodedPublicKey(Environment* env,
v8::Maybe<void> ToEncodedPublicKey(Environment* env,
const PublicKeyEncodingConfig& config,
v8::Local<v8::Value>* out);

v8::Maybe<bool> ToEncodedPrivateKey(Environment* env,
v8::Maybe<void> ToEncodedPrivateKey(Environment* env,
const PrivateKeyEncodingConfig& config,
v8::Local<v8::Value>* out);

Expand Down
16 changes: 8 additions & 8 deletions src/crypto/crypto_rsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using v8::BackingStore;
using v8::FunctionCallbackInfo;
using v8::Int32;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
using v8::Nothing;
Expand Down Expand Up @@ -359,10 +360,9 @@ WebCryptoCipherStatus RSACipherTraits::DoCipher(
return WebCryptoCipherStatus::FAILED;
}

Maybe<bool> ExportJWKRsaKey(
Environment* env,
std::shared_ptr<KeyObjectData> key,
Local<Object> target) {
Maybe<void> ExportJWKRsaKey(Environment* env,
std::shared_ptr<KeyObjectData> key,
Local<Object> target) {
ManagedEVPPKey m_pkey = key->GetAsymmetricKey();
Mutex::ScopedLock lock(*m_pkey.mutex());
int type = EVP_PKEY_id(m_pkey.get());
Expand Down Expand Up @@ -392,12 +392,12 @@ Maybe<bool> ExportJWKRsaKey(
env->context(),
env->jwk_kty_string(),
env->jwk_rsa_string()).IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}

if (SetEncodedValue(env, target, env->jwk_n_string(), n).IsNothing() ||
SetEncodedValue(env, target, env->jwk_e_string(), e).IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}

if (key->GetKeyType() == kKeyTypePrivate) {
Expand All @@ -409,11 +409,11 @@ Maybe<bool> ExportJWKRsaKey(
SetEncodedValue(env, target, env->jwk_dp_string(), dp).IsNothing() ||
SetEncodedValue(env, target, env->jwk_dq_string(), dq).IsNothing() ||
SetEncodedValue(env, target, env->jwk_qi_string(), qi).IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}
}

return Just(true);
return JustVoid();
}

std::shared_ptr<KeyObjectData> ImportJWKRsaKey(
Expand Down
7 changes: 3 additions & 4 deletions src/crypto/crypto_rsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ struct RSACipherTraits final {

using RSACipherJob = CipherJob<RSACipherTraits>;

v8::Maybe<bool> ExportJWKRsaKey(
Environment* env,
std::shared_ptr<KeyObjectData> key,
v8::Local<v8::Object> target);
v8::Maybe<void> ExportJWKRsaKey(Environment* env,
std::shared_ptr<KeyObjectData> key,
v8::Local<v8::Object> target);

std::shared_ptr<KeyObjectData> ImportJWKRsaKey(
Environment* env,
Expand Down

0 comments on commit d9c0486

Please sign in to comment.