From 6b4e8493d97e6720299b89e3dbbe68c32e68b5b3 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Wed, 9 Mar 2022 04:10:28 +0530 Subject: [PATCH] src,crypto: avoid tristate Maybe in ExportJWKEcKey() The function currently uses the return value to convey whether an exception was thrown while it was running by using either Just(true) or Nothing(). Unfortunately, Maybe also has a third state - Just(false), which doesn't make any sense here. So this change avoids the possibility of a tristate return value by making use of Maybe which only has two valid states - JustVoid() / Nothing(), which fits right in. Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/42223 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- src/crypto/crypto_ec.cc | 15 ++++++++------- src/crypto/crypto_ec.h | 2 +- src/crypto/crypto_keys.cc | 3 ++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index 868c3465ad33d1..7f1ba4dbff6036 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -24,6 +24,7 @@ using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Int32; using v8::Just; +using v8::JustVoid; using v8::Local; using v8::Maybe; using v8::Nothing; @@ -711,7 +712,7 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport( } } -Maybe ExportJWKEcKey( +Maybe ExportJWKEcKey( Environment* env, std::shared_ptr key, Local target) { @@ -738,7 +739,7 @@ Maybe ExportJWKEcKey( env->context(), env->jwk_kty_string(), env->jwk_ec_string()).IsNothing()) { - return Nothing(); + return Nothing(); } if (SetEncodedValue( @@ -753,7 +754,7 @@ Maybe ExportJWKEcKey( env->jwk_y_string(), y.get(), degree_bytes).IsNothing()) { - return Nothing(); + return Nothing(); } Local crv_name; @@ -774,14 +775,14 @@ Maybe ExportJWKEcKey( default: { THROW_ERR_CRYPTO_JWK_UNSUPPORTED_CURVE( env, "Unsupported JWK EC curve: %s.", OBJ_nid2sn(nid)); - return Nothing(); + return Nothing(); } } if (target->Set( env->context(), env->jwk_crv_string(), crv_name).IsNothing()) { - return Nothing(); + return Nothing(); } if (key->GetKeyType() == kKeyTypePrivate) { @@ -791,10 +792,10 @@ Maybe ExportJWKEcKey( target, env->jwk_d_string(), pvt, - degree_bytes); + degree_bytes).IsJust() ? JustVoid() : Nothing(); } - return Just(true); + return JustVoid(); } Maybe ExportJWKEdKey( diff --git a/src/crypto/crypto_ec.h b/src/crypto/crypto_ec.h index 34d37c7e44220d..bc4160fc8bee01 100644 --- a/src/crypto/crypto_ec.h +++ b/src/crypto/crypto_ec.h @@ -144,7 +144,7 @@ struct ECKeyExportTraits final { using ECKeyExportJob = KeyExportJob; -v8::Maybe ExportJWKEcKey( +v8::Maybe ExportJWKEcKey( Environment* env, std::shared_ptr key, v8::Local target); diff --git a/src/crypto/crypto_keys.cc b/src/crypto/crypto_keys.cc index 441f5736ec8a70..5dd04edfbfa1cf 100644 --- a/src/crypto/crypto_keys.cc +++ b/src/crypto/crypto_keys.cc @@ -497,7 +497,8 @@ Maybe ExportJWKAsymmetricKey( break; } case EVP_PKEY_RSA: return ExportJWKRsaKey(env, key, target); - case EVP_PKEY_EC: return ExportJWKEcKey(env, key, target); + case EVP_PKEY_EC: return ExportJWKEcKey(env, key, target).IsJust() ? + Just(true) : Nothing(); case EVP_PKEY_ED25519: // Fall through case EVP_PKEY_ED448: