Skip to content

Commit 1a4da22

Browse files
tniessenmarco-ippolito
authored andcommitted
src: use Maybe<void> in ManagedEVPPKey
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 <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent c2bb460 commit 1a4da22

File tree

6 files changed

+52
-57
lines changed

6 files changed

+52
-57
lines changed

src/crypto/crypto_ec.cc

+9-10
Original file line numberDiff line numberDiff line change
@@ -840,10 +840,9 @@ Maybe<void> ExportJWKEcKey(
840840
return JustVoid();
841841
}
842842

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

@@ -868,15 +867,15 @@ Maybe<bool> ExportJWKEdKey(
868867
env->context(),
869868
env->jwk_crv_string(),
870869
OneByteString(env->isolate(), curve)).IsNothing()) {
871-
return Nothing<bool>();
870+
return Nothing<void>();
872871
}
873872

874873
size_t len = 0;
875874
Local<Value> encoded;
876875
Local<Value> error;
877876

878877
if (!EVP_PKEY_get_raw_public_key(pkey.get(), nullptr, &len))
879-
return Nothing<bool>();
878+
return Nothing<void>();
880879

881880
ByteSource::Builder out(len);
882881

@@ -889,7 +888,7 @@ Maybe<bool> ExportJWKEdKey(
889888
!target->Set(env->context(), env->jwk_d_string(), encoded).IsJust()) {
890889
if (!error.IsEmpty())
891890
env->isolate()->ThrowException(error);
892-
return Nothing<bool>();
891+
return Nothing<void>();
893892
}
894893
}
895894

@@ -901,17 +900,17 @@ Maybe<bool> ExportJWKEdKey(
901900
!target->Set(env->context(), env->jwk_x_string(), encoded).IsJust()) {
902901
if (!error.IsEmpty())
903902
env->isolate()->ThrowException(error);
904-
return Nothing<bool>();
903+
return Nothing<void>();
905904
}
906905

907906
if (target->Set(
908907
env->context(),
909908
env->jwk_kty_string(),
910909
env->jwk_okp_string()).IsNothing()) {
911-
return Nothing<bool>();
910+
return Nothing<void>();
912911
}
913912

914-
return Just(true);
913+
return JustVoid();
915914
}
916915

917916
std::shared_ptr<KeyObjectData> ImportJWKEcKey(

src/crypto/crypto_ec.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,9 @@ v8::Maybe<void> ExportJWKEcKey(
148148
std::shared_ptr<KeyObjectData> key,
149149
v8::Local<v8::Object> target);
150150

151-
v8::Maybe<bool> ExportJWKEdKey(
152-
Environment* env,
153-
std::shared_ptr<KeyObjectData> key,
154-
v8::Local<v8::Object> target);
151+
v8::Maybe<void> ExportJWKEdKey(Environment* env,
152+
std::shared_ptr<KeyObjectData> key,
153+
v8::Local<v8::Object> target);
155154

156155
std::shared_ptr<KeyObjectData> ImportJWKEcKey(
157156
Environment* env,

src/crypto/crypto_keys.cc

+27-29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ using v8::FunctionTemplate;
2626
using v8::Int32;
2727
using v8::Isolate;
2828
using v8::Just;
29+
using v8::JustVoid;
2930
using v8::Local;
3031
using v8::Maybe;
3132
using v8::MaybeLocal;
@@ -431,10 +432,9 @@ MaybeLocal<Value> WritePublicKey(Environment* env,
431432
return BIOToStringOrBuffer(env, bio.get(), config.format_);
432433
}
433434

434-
Maybe<bool> ExportJWKSecretKey(
435-
Environment* env,
436-
std::shared_ptr<KeyObjectData> key,
437-
Local<Object> target) {
435+
Maybe<void> ExportJWKSecretKey(Environment* env,
436+
std::shared_ptr<KeyObjectData> key,
437+
Local<Object> target) {
438438
CHECK_EQ(key->GetKeyType(), kKeyTypeSecret);
439439

440440
Local<Value> error;
@@ -449,10 +449,9 @@ Maybe<bool> ExportJWKSecretKey(
449449
if (key_data.IsEmpty()) {
450450
CHECK(!error.IsEmpty());
451451
env->isolate()->ThrowException(error);
452-
return Nothing<bool>();
452+
return Nothing<void>();
453453
}
454-
if (!key_data.ToLocal(&raw))
455-
return Nothing<bool>();
454+
if (!key_data.ToLocal(&raw)) return Nothing<void>();
456455

457456
if (target->Set(
458457
env->context(),
@@ -462,10 +461,10 @@ Maybe<bool> ExportJWKSecretKey(
462461
env->context(),
463462
env->jwk_k_string(),
464463
raw).IsNothing()) {
465-
return Nothing<bool>();
464+
return Nothing<void>();
466465
}
467466

468-
return Just(true);
467+
return JustVoid();
469468
}
470469

471470
std::shared_ptr<KeyObjectData> ImportJWKSecretKey(
@@ -483,19 +482,18 @@ std::shared_ptr<KeyObjectData> ImportJWKSecretKey(
483482
return KeyObjectData::CreateSecret(std::move(key_data));
484483
}
485484

486-
Maybe<bool> ExportJWKAsymmetricKey(
487-
Environment* env,
488-
std::shared_ptr<KeyObjectData> key,
489-
Local<Object> target,
490-
bool handleRsaPss) {
485+
Maybe<void> ExportJWKAsymmetricKey(Environment* env,
486+
std::shared_ptr<KeyObjectData> key,
487+
Local<Object> target,
488+
bool handleRsaPss) {
491489
switch (EVP_PKEY_id(key->GetAsymmetricKey().get())) {
492490
case EVP_PKEY_RSA_PSS: {
493491
if (handleRsaPss) return ExportJWKRsaKey(env, key, target);
494492
break;
495493
}
496494
case EVP_PKEY_RSA: return ExportJWKRsaKey(env, key, target);
497-
case EVP_PKEY_EC: return ExportJWKEcKey(env, key, target).IsJust() ?
498-
Just(true) : Nothing<bool>();
495+
case EVP_PKEY_EC:
496+
return ExportJWKEcKey(env, key, target);
499497
case EVP_PKEY_ED25519:
500498
// Fall through
501499
case EVP_PKEY_ED448:
@@ -505,7 +503,7 @@ Maybe<bool> ExportJWKAsymmetricKey(
505503
case EVP_PKEY_X448: return ExportJWKEdKey(env, key, target);
506504
}
507505
THROW_ERR_CRYPTO_JWK_UNSUPPORTED_KEY_TYPE(env);
508-
return Nothing<bool>();
506+
return Nothing<void>();
509507
}
510508

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

608-
// This maps true to Just<bool>(true) and false to Nothing<bool>().
609-
static inline Maybe<bool> Tristate(bool b) {
610-
return b ? Just(true) : Nothing<bool>();
606+
// This maps true to JustVoid and false to Nothing<void>().
607+
static inline Maybe<void> NothingIfFalse(bool b) {
608+
return b ? JustVoid() : Nothing<void>();
611609
}
612610

613-
Maybe<bool> ExportJWKInner(Environment* env,
611+
Maybe<void> ExportJWKInner(Environment* env,
614612
std::shared_ptr<KeyObjectData> key,
615613
Local<Value> result,
616614
bool handleRsaPss) {
@@ -627,44 +625,44 @@ Maybe<bool> ExportJWKInner(Environment* env,
627625
}
628626
}
629627

630-
Maybe<bool> ManagedEVPPKey::ToEncodedPublicKey(
628+
Maybe<void> ManagedEVPPKey::ToEncodedPublicKey(
631629
Environment* env,
632630
const PublicKeyEncodingConfig& config,
633631
Local<Value>* out) {
634-
if (!*this) return Nothing<bool>();
632+
if (!*this) return Nothing<void>();
635633
if (config.output_key_object_) {
636634
// Note that this has the downside of containing sensitive data of the
637635
// private key.
638636
std::shared_ptr<KeyObjectData> data =
639637
KeyObjectData::CreateAsymmetric(kKeyTypePublic, *this);
640-
return Tristate(KeyObjectHandle::Create(env, data).ToLocal(out));
638+
return NothingIfFalse(KeyObjectHandle::Create(env, data).ToLocal(out));
641639
} else if (config.format_ == kKeyFormatJWK) {
642640
std::shared_ptr<KeyObjectData> data =
643641
KeyObjectData::CreateAsymmetric(kKeyTypePublic, *this);
644642
*out = Object::New(env->isolate());
645643
return ExportJWKInner(env, data, *out, false);
646644
}
647645

648-
return Tristate(WritePublicKey(env, get(), config).ToLocal(out));
646+
return NothingIfFalse(WritePublicKey(env, get(), config).ToLocal(out));
649647
}
650648

651-
Maybe<bool> ManagedEVPPKey::ToEncodedPrivateKey(
649+
Maybe<void> ManagedEVPPKey::ToEncodedPrivateKey(
652650
Environment* env,
653651
const PrivateKeyEncodingConfig& config,
654652
Local<Value>* out) {
655-
if (!*this) return Nothing<bool>();
653+
if (!*this) return Nothing<void>();
656654
if (config.output_key_object_) {
657655
std::shared_ptr<KeyObjectData> data =
658656
KeyObjectData::CreateAsymmetric(kKeyTypePrivate, *this);
659-
return Tristate(KeyObjectHandle::Create(env, data).ToLocal(out));
657+
return NothingIfFalse(KeyObjectHandle::Create(env, data).ToLocal(out));
660658
} else if (config.format_ == kKeyFormatJWK) {
661659
std::shared_ptr<KeyObjectData> data =
662660
KeyObjectData::CreateAsymmetric(kKeyTypePrivate, *this);
663661
*out = Object::New(env->isolate());
664662
return ExportJWKInner(env, data, *out, false);
665663
}
666664

667-
return Tristate(WritePrivateKey(env, get(), config).ToLocal(out));
665+
return NothingIfFalse(WritePrivateKey(env, get(), config).ToLocal(out));
668666
}
669667

670668
NonCopyableMaybe<PrivateKeyEncodingConfig>

src/crypto/crypto_keys.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ class ManagedEVPPKey : public MemoryRetainer {
112112
unsigned int* offset,
113113
bool allow_key_object);
114114

115-
v8::Maybe<bool> ToEncodedPublicKey(Environment* env,
115+
v8::Maybe<void> ToEncodedPublicKey(Environment* env,
116116
const PublicKeyEncodingConfig& config,
117117
v8::Local<v8::Value>* out);
118118

119-
v8::Maybe<bool> ToEncodedPrivateKey(Environment* env,
119+
v8::Maybe<void> ToEncodedPrivateKey(Environment* env,
120120
const PrivateKeyEncodingConfig& config,
121121
v8::Local<v8::Value>* out);
122122

src/crypto/crypto_rsa.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ using v8::BackingStore;
1919
using v8::FunctionCallbackInfo;
2020
using v8::Int32;
2121
using v8::Just;
22+
using v8::JustVoid;
2223
using v8::Local;
2324
using v8::Maybe;
2425
using v8::Nothing;
@@ -359,10 +360,9 @@ WebCryptoCipherStatus RSACipherTraits::DoCipher(
359360
return WebCryptoCipherStatus::FAILED;
360361
}
361362

362-
Maybe<bool> ExportJWKRsaKey(
363-
Environment* env,
364-
std::shared_ptr<KeyObjectData> key,
365-
Local<Object> target) {
363+
Maybe<void> ExportJWKRsaKey(Environment* env,
364+
std::shared_ptr<KeyObjectData> key,
365+
Local<Object> target) {
366366
ManagedEVPPKey m_pkey = key->GetAsymmetricKey();
367367
Mutex::ScopedLock lock(*m_pkey.mutex());
368368
int type = EVP_PKEY_id(m_pkey.get());
@@ -392,12 +392,12 @@ Maybe<bool> ExportJWKRsaKey(
392392
env->context(),
393393
env->jwk_kty_string(),
394394
env->jwk_rsa_string()).IsNothing()) {
395-
return Nothing<bool>();
395+
return Nothing<void>();
396396
}
397397

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

403403
if (key->GetKeyType() == kKeyTypePrivate) {
@@ -409,11 +409,11 @@ Maybe<bool> ExportJWKRsaKey(
409409
SetEncodedValue(env, target, env->jwk_dp_string(), dp).IsNothing() ||
410410
SetEncodedValue(env, target, env->jwk_dq_string(), dq).IsNothing() ||
411411
SetEncodedValue(env, target, env->jwk_qi_string(), qi).IsNothing()) {
412-
return Nothing<bool>();
412+
return Nothing<void>();
413413
}
414414
}
415415

416-
return Just(true);
416+
return JustVoid();
417417
}
418418

419419
std::shared_ptr<KeyObjectData> ImportJWKRsaKey(

src/crypto/crypto_rsa.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ struct RSACipherTraits final {
114114

115115
using RSACipherJob = CipherJob<RSACipherTraits>;
116116

117-
v8::Maybe<bool> ExportJWKRsaKey(
118-
Environment* env,
119-
std::shared_ptr<KeyObjectData> key,
120-
v8::Local<v8::Object> target);
117+
v8::Maybe<void> ExportJWKRsaKey(Environment* env,
118+
std::shared_ptr<KeyObjectData> key,
119+
v8::Local<v8::Object> target);
121120

122121
std::shared_ptr<KeyObjectData> ImportJWKRsaKey(
123122
Environment* env,

0 commit comments

Comments
 (0)