From 1a4da22a604748e123dff12e1c328b831c55bf9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sat, 13 Jul 2024 14:55:01 +0200 Subject: [PATCH] src: use Maybe in ManagedEVPPKey With recent versions of V8, it is not necessary to use Maybe anymore. This changes member functions of ManagedEVPPKey to use Maybe instead, as well as (transitive) dependencies. PR-URL: https://github.com/nodejs/node/pull/53811 Reviewed-By: James M Snell Reviewed-By: Yagiz Nizipli Reviewed-By: Luigi Pinca --- src/crypto/crypto_ec.cc | 19 +++++++------ src/crypto/crypto_ec.h | 7 +++-- src/crypto/crypto_keys.cc | 56 +++++++++++++++++++-------------------- src/crypto/crypto_keys.h | 4 +-- src/crypto/crypto_rsa.cc | 16 +++++------ src/crypto/crypto_rsa.h | 7 +++-- 6 files changed, 52 insertions(+), 57 deletions(-) diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index 860d5048db7611..98273ac4eea91b 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -840,10 +840,9 @@ Maybe ExportJWKEcKey( return JustVoid(); } -Maybe ExportJWKEdKey( - Environment* env, - std::shared_ptr key, - Local target) { +Maybe ExportJWKEdKey(Environment* env, + std::shared_ptr key, + Local target) { ManagedEVPPKey pkey = key->GetAsymmetricKey(); Mutex::ScopedLock lock(*pkey.mutex()); @@ -868,7 +867,7 @@ Maybe ExportJWKEdKey( env->context(), env->jwk_crv_string(), OneByteString(env->isolate(), curve)).IsNothing()) { - return Nothing(); + return Nothing(); } size_t len = 0; @@ -876,7 +875,7 @@ Maybe ExportJWKEdKey( Local error; if (!EVP_PKEY_get_raw_public_key(pkey.get(), nullptr, &len)) - return Nothing(); + return Nothing(); ByteSource::Builder out(len); @@ -889,7 +888,7 @@ Maybe ExportJWKEdKey( !target->Set(env->context(), env->jwk_d_string(), encoded).IsJust()) { if (!error.IsEmpty()) env->isolate()->ThrowException(error); - return Nothing(); + return Nothing(); } } @@ -901,17 +900,17 @@ Maybe ExportJWKEdKey( !target->Set(env->context(), env->jwk_x_string(), encoded).IsJust()) { if (!error.IsEmpty()) env->isolate()->ThrowException(error); - return Nothing(); + return Nothing(); } if (target->Set( env->context(), env->jwk_kty_string(), env->jwk_okp_string()).IsNothing()) { - return Nothing(); + return Nothing(); } - return Just(true); + return JustVoid(); } std::shared_ptr ImportJWKEcKey( diff --git a/src/crypto/crypto_ec.h b/src/crypto/crypto_ec.h index f9570bd41f92eb..adeef8e3a3a92d 100644 --- a/src/crypto/crypto_ec.h +++ b/src/crypto/crypto_ec.h @@ -148,10 +148,9 @@ v8::Maybe ExportJWKEcKey( std::shared_ptr key, v8::Local target); -v8::Maybe ExportJWKEdKey( - Environment* env, - std::shared_ptr key, - v8::Local target); +v8::Maybe ExportJWKEdKey(Environment* env, + std::shared_ptr key, + v8::Local target); std::shared_ptr ImportJWKEcKey( Environment* env, diff --git a/src/crypto/crypto_keys.cc b/src/crypto/crypto_keys.cc index 4ab9f21dbcbb53..836ae11ff27a5f 100644 --- a/src/crypto/crypto_keys.cc +++ b/src/crypto/crypto_keys.cc @@ -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; @@ -431,10 +432,9 @@ MaybeLocal WritePublicKey(Environment* env, return BIOToStringOrBuffer(env, bio.get(), config.format_); } -Maybe ExportJWKSecretKey( - Environment* env, - std::shared_ptr key, - Local target) { +Maybe ExportJWKSecretKey(Environment* env, + std::shared_ptr key, + Local target) { CHECK_EQ(key->GetKeyType(), kKeyTypeSecret); Local error; @@ -449,10 +449,9 @@ Maybe ExportJWKSecretKey( if (key_data.IsEmpty()) { CHECK(!error.IsEmpty()); env->isolate()->ThrowException(error); - return Nothing(); + return Nothing(); } - if (!key_data.ToLocal(&raw)) - return Nothing(); + if (!key_data.ToLocal(&raw)) return Nothing(); if (target->Set( env->context(), @@ -462,10 +461,10 @@ Maybe ExportJWKSecretKey( env->context(), env->jwk_k_string(), raw).IsNothing()) { - return Nothing(); + return Nothing(); } - return Just(true); + return JustVoid(); } std::shared_ptr ImportJWKSecretKey( @@ -483,19 +482,18 @@ std::shared_ptr ImportJWKSecretKey( return KeyObjectData::CreateSecret(std::move(key_data)); } -Maybe ExportJWKAsymmetricKey( - Environment* env, - std::shared_ptr key, - Local target, - bool handleRsaPss) { +Maybe ExportJWKAsymmetricKey(Environment* env, + std::shared_ptr key, + Local 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(); + case EVP_PKEY_EC: + return ExportJWKEcKey(env, key, target); case EVP_PKEY_ED25519: // Fall through case EVP_PKEY_ED448: @@ -505,7 +503,7 @@ Maybe ExportJWKAsymmetricKey( case EVP_PKEY_X448: return ExportJWKEdKey(env, key, target); } THROW_ERR_CRYPTO_JWK_UNSUPPORTED_KEY_TYPE(env); - return Nothing(); + return Nothing(); } std::shared_ptr ImportJWKAsymmetricKey( @@ -605,12 +603,12 @@ size_t ManagedEVPPKey::size_of_public_key() const { pkey_.get(), nullptr, &len) == 1) ? len : 0; } -// This maps true to Just(true) and false to Nothing(). -static inline Maybe Tristate(bool b) { - return b ? Just(true) : Nothing(); +// This maps true to JustVoid and false to Nothing(). +static inline Maybe NothingIfFalse(bool b) { + return b ? JustVoid() : Nothing(); } -Maybe ExportJWKInner(Environment* env, +Maybe ExportJWKInner(Environment* env, std::shared_ptr key, Local result, bool handleRsaPss) { @@ -627,17 +625,17 @@ Maybe ExportJWKInner(Environment* env, } } -Maybe ManagedEVPPKey::ToEncodedPublicKey( +Maybe ManagedEVPPKey::ToEncodedPublicKey( Environment* env, const PublicKeyEncodingConfig& config, Local* out) { - if (!*this) return Nothing(); + if (!*this) return Nothing(); if (config.output_key_object_) { // Note that this has the downside of containing sensitive data of the // private key. std::shared_ptr 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 data = KeyObjectData::CreateAsymmetric(kKeyTypePublic, *this); @@ -645,18 +643,18 @@ Maybe ManagedEVPPKey::ToEncodedPublicKey( return ExportJWKInner(env, data, *out, false); } - return Tristate(WritePublicKey(env, get(), config).ToLocal(out)); + return NothingIfFalse(WritePublicKey(env, get(), config).ToLocal(out)); } -Maybe ManagedEVPPKey::ToEncodedPrivateKey( +Maybe ManagedEVPPKey::ToEncodedPrivateKey( Environment* env, const PrivateKeyEncodingConfig& config, Local* out) { - if (!*this) return Nothing(); + if (!*this) return Nothing(); if (config.output_key_object_) { std::shared_ptr 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 data = KeyObjectData::CreateAsymmetric(kKeyTypePrivate, *this); @@ -664,7 +662,7 @@ Maybe ManagedEVPPKey::ToEncodedPrivateKey( return ExportJWKInner(env, data, *out, false); } - return Tristate(WritePrivateKey(env, get(), config).ToLocal(out)); + return NothingIfFalse(WritePrivateKey(env, get(), config).ToLocal(out)); } NonCopyableMaybe diff --git a/src/crypto/crypto_keys.h b/src/crypto/crypto_keys.h index 820d26cc153177..32c50b52188a85 100644 --- a/src/crypto/crypto_keys.h +++ b/src/crypto/crypto_keys.h @@ -112,11 +112,11 @@ class ManagedEVPPKey : public MemoryRetainer { unsigned int* offset, bool allow_key_object); - v8::Maybe ToEncodedPublicKey(Environment* env, + v8::Maybe ToEncodedPublicKey(Environment* env, const PublicKeyEncodingConfig& config, v8::Local* out); - v8::Maybe ToEncodedPrivateKey(Environment* env, + v8::Maybe ToEncodedPrivateKey(Environment* env, const PrivateKeyEncodingConfig& config, v8::Local* out); diff --git a/src/crypto/crypto_rsa.cc b/src/crypto/crypto_rsa.cc index f222ab9cf5ccbc..23b2b8c56dec8a 100644 --- a/src/crypto/crypto_rsa.cc +++ b/src/crypto/crypto_rsa.cc @@ -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; @@ -359,10 +360,9 @@ WebCryptoCipherStatus RSACipherTraits::DoCipher( return WebCryptoCipherStatus::FAILED; } -Maybe ExportJWKRsaKey( - Environment* env, - std::shared_ptr key, - Local target) { +Maybe ExportJWKRsaKey(Environment* env, + std::shared_ptr key, + Local target) { ManagedEVPPKey m_pkey = key->GetAsymmetricKey(); Mutex::ScopedLock lock(*m_pkey.mutex()); int type = EVP_PKEY_id(m_pkey.get()); @@ -392,12 +392,12 @@ Maybe ExportJWKRsaKey( env->context(), env->jwk_kty_string(), env->jwk_rsa_string()).IsNothing()) { - return Nothing(); + return Nothing(); } if (SetEncodedValue(env, target, env->jwk_n_string(), n).IsNothing() || SetEncodedValue(env, target, env->jwk_e_string(), e).IsNothing()) { - return Nothing(); + return Nothing(); } if (key->GetKeyType() == kKeyTypePrivate) { @@ -409,11 +409,11 @@ Maybe 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(); + return Nothing(); } } - return Just(true); + return JustVoid(); } std::shared_ptr ImportJWKRsaKey( diff --git a/src/crypto/crypto_rsa.h b/src/crypto/crypto_rsa.h index bd00320ca8a5be..e444159daa9404 100644 --- a/src/crypto/crypto_rsa.h +++ b/src/crypto/crypto_rsa.h @@ -114,10 +114,9 @@ struct RSACipherTraits final { using RSACipherJob = CipherJob; -v8::Maybe ExportJWKRsaKey( - Environment* env, - std::shared_ptr key, - v8::Local target); +v8::Maybe ExportJWKRsaKey(Environment* env, + std::shared_ptr key, + v8::Local target); std::shared_ptr ImportJWKRsaKey( Environment* env,