diff --git a/src/crypto/crypto_aes.cc b/src/crypto/crypto_aes.cc index ebf7157eb21898..542d23f2a1f146 100644 --- a/src/crypto/crypto_aes.cc +++ b/src/crypto/crypto_aes.cc @@ -18,6 +18,7 @@ namespace node { using v8::FunctionCallbackInfo; using v8::Just; +using v8::JustVoid; using v8::Local; using v8::Maybe; using v8::Nothing; @@ -452,7 +453,7 @@ void AESCipherConfig::MemoryInfo(MemoryTracker* tracker) const { } } -Maybe AESCipherTraits::AdditionalConfig( +Maybe AESCipherTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -482,22 +483,22 @@ Maybe AESCipherTraits::AdditionalConfig( params->cipher = EVP_get_cipherbynid(cipher_nid); if (params->cipher == nullptr) { THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env); - return Nothing(); + return Nothing(); } int cipher_op_mode = EVP_CIPHER_mode(params->cipher); if (cipher_op_mode != EVP_CIPH_WRAP_MODE) { if (!ValidateIV(env, mode, args[offset + 1], params)) { - return Nothing(); + return Nothing(); } if (cipher_op_mode == EVP_CIPH_CTR_MODE) { if (!ValidateCounter(env, args[offset + 2], params)) { - return Nothing(); + return Nothing(); } } else if (cipher_op_mode == EVP_CIPH_GCM_MODE) { if (!ValidateAuthTag(env, mode, cipher_mode, args[offset + 2], params) || !ValidateAdditionalData(env, mode, args[offset + 3], params)) { - return Nothing(); + return Nothing(); } } } else { @@ -507,10 +508,10 @@ Maybe AESCipherTraits::AdditionalConfig( if (params->iv.size() < static_cast(EVP_CIPHER_iv_length(params->cipher))) { THROW_ERR_CRYPTO_INVALID_IV(env); - return Nothing(); + return Nothing(); } - return Just(true); + return JustVoid(); } WebCryptoCipherStatus AESCipherTraits::DoCipher(Environment* env, diff --git a/src/crypto/crypto_aes.h b/src/crypto/crypto_aes.h index 1de8d8ab8cabca..3f554ac8c15c60 100644 --- a/src/crypto/crypto_aes.h +++ b/src/crypto/crypto_aes.h @@ -60,7 +60,7 @@ struct AESCipherTraits final { using AdditionalParameters = AESCipherConfig; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, diff --git a/src/crypto/crypto_cipher.h b/src/crypto/crypto_cipher.h index 091ae0445572c7..de436e2e9d2df8 100644 --- a/src/crypto/crypto_cipher.h +++ b/src/crypto/crypto_cipher.h @@ -245,9 +245,8 @@ class CipherJob final : public CryptoJob { } } - v8::Maybe ToResult( - v8::Local* err, - v8::Local* result) override { + v8::Maybe ToResult(v8::Local* err, + v8::Local* result) override { Environment* env = AsyncWrap::env(); CryptoErrorStore* errors = CryptoJob::errors(); @@ -258,11 +257,18 @@ class CipherJob final : public CryptoJob { CHECK(errors->Empty()); *err = v8::Undefined(env->isolate()); *result = out_.ToArrayBuffer(env); - return v8::Just(!result->IsEmpty()); + if (result->IsEmpty()) { + return v8::Nothing(); + } + } else { + *result = v8::Undefined(env->isolate()); + if (!errors->ToException(env).ToLocal(err)) { + return v8::Nothing(); + } } - - *result = v8::Undefined(env->isolate()); - return v8::Just(errors->ToException(env).ToLocal(err)); + CHECK(!result->IsEmpty()); + CHECK(!err->IsEmpty()); + return v8::JustVoid(); } SET_SELF_SIZE(CipherJob) diff --git a/src/crypto/crypto_dh.cc b/src/crypto/crypto_dh.cc index f277b52c3a0817..e5664dfa2bc7e1 100644 --- a/src/crypto/crypto_dh.cc +++ b/src/crypto/crypto_dh.cc @@ -23,7 +23,7 @@ using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Int32; using v8::Isolate; -using v8::Just; +using v8::JustVoid; using v8::Local; using v8::Maybe; using v8::MaybeLocal; @@ -338,7 +338,7 @@ void Check(const FunctionCallbackInfo& args) { // * Private type // * Cipher // * Passphrase -Maybe DhKeyGenTraits::AdditionalConfig( +Maybe DhKeyGenTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int* offset, @@ -350,7 +350,7 @@ Maybe DhKeyGenTraits::AdditionalConfig( auto group = DHPointer::FindGroup(group_name.ToStringView()); if (!group) { THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env); - return Nothing(); + return Nothing(); } static constexpr int kStandardizedGenerator = 2; @@ -363,14 +363,14 @@ Maybe DhKeyGenTraits::AdditionalConfig( int size = args[*offset].As()->Value(); if (size < 0) { THROW_ERR_OUT_OF_RANGE(env, "Invalid prime size"); - return Nothing(); + return Nothing(); } params->params.prime = size; } else { ArrayBufferOrViewContents input(args[*offset]); if (UNLIKELY(!input.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "prime is too big"); - return Nothing(); + return Nothing(); } params->params.prime = BignumPointer(input.data(), input.size()); } @@ -380,7 +380,7 @@ Maybe DhKeyGenTraits::AdditionalConfig( *offset += 2; } - return Just(true); + return JustVoid(); } EVPKeyCtxPointer DhKeyGenTraits::Setup(DhKeyPairGenConfig* params) { @@ -424,11 +424,11 @@ EVPKeyCtxPointer DhKeyGenTraits::Setup(DhKeyPairGenConfig* params) { return ctx; } -Maybe DHKeyExportTraits::AdditionalConfig( +Maybe DHKeyExportTraits::AdditionalConfig( const FunctionCallbackInfo& args, unsigned int offset, DHKeyExportConfig* params) { - return Just(true); + return JustVoid(); } WebCryptoKeyExportStatus DHKeyExportTraits::DoExport( @@ -487,7 +487,7 @@ void Stateless(const FunctionCallbackInfo& args) { } } // namespace -Maybe DHBitsTraits::AdditionalConfig( +Maybe DHBitsTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -500,28 +500,25 @@ Maybe DHBitsTraits::AdditionalConfig( KeyObjectHandle* private_key; KeyObjectHandle* public_key; - ASSIGN_OR_RETURN_UNWRAP(&public_key, args[offset], Nothing()); - ASSIGN_OR_RETURN_UNWRAP(&private_key, args[offset + 1], Nothing()); + ASSIGN_OR_RETURN_UNWRAP(&public_key, args[offset], Nothing()); + ASSIGN_OR_RETURN_UNWRAP(&private_key, args[offset + 1], Nothing()); if (private_key->Data().GetKeyType() != kKeyTypePrivate || public_key->Data().GetKeyType() != kKeyTypePublic) { THROW_ERR_CRYPTO_INVALID_KEYTYPE(env); - return Nothing(); + return Nothing(); } params->public_key = public_key->Data().addRef(); params->private_key = private_key->Data().addRef(); - return Just(true); + return JustVoid(); } -Maybe DHBitsTraits::EncodeOutput( - Environment* env, - const DHBitsConfig& params, - ByteSource* out, - v8::Local* result) { - *result = out->ToArrayBuffer(env); - return Just(!result->IsEmpty()); +MaybeLocal DHBitsTraits::EncodeOutput(Environment* env, + const DHBitsConfig& params, + ByteSource* out) { + return out->ToArrayBuffer(env); } bool DHBitsTraits::DeriveBits( @@ -533,11 +530,11 @@ bool DHBitsTraits::DeriveBits( return true; } -Maybe GetDhKeyDetail(Environment* env, +Maybe GetDhKeyDetail(Environment* env, const KeyObjectData& key, Local target) { CHECK_EQ(EVP_PKEY_id(key.GetAsymmetricKey().get()), EVP_PKEY_DH); - return Just(true); + return JustVoid(); } void DiffieHellman::Initialize(Environment* env, Local target) { diff --git a/src/crypto/crypto_dh.h b/src/crypto/crypto_dh.h index 0f5b835056f901..a84993ddd55f02 100644 --- a/src/crypto/crypto_dh.h +++ b/src/crypto/crypto_dh.h @@ -49,7 +49,7 @@ struct DhKeyGenTraits final { static EVPKeyCtxPointer Setup(DhKeyPairGenConfig* params); - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int* offset, @@ -68,7 +68,7 @@ struct DHKeyExportTraits final { static constexpr const char* JobName = "DHKeyExportJob"; using AdditionalParameters = DHKeyExportConfig; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( const v8::FunctionCallbackInfo& args, unsigned int offset, DHKeyExportConfig* config); @@ -95,7 +95,7 @@ struct DHBitsTraits final { static constexpr AsyncWrap::ProviderType Provider = AsyncWrap::PROVIDER_DERIVEBITSREQUEST; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, @@ -106,16 +106,14 @@ struct DHBitsTraits final { const DHBitsConfig& params, ByteSource* out_); - static v8::Maybe EncodeOutput( - Environment* env, - const DHBitsConfig& params, - ByteSource* out, - v8::Local* result); + static v8::MaybeLocal EncodeOutput(Environment* env, + const DHBitsConfig& params, + ByteSource* out); }; using DHBitsJob = DeriveBitsJob; -v8::Maybe GetDhKeyDetail(Environment* env, +v8::Maybe GetDhKeyDetail(Environment* env, const KeyObjectData& key, v8::Local target); diff --git a/src/crypto/crypto_dsa.cc b/src/crypto/crypto_dsa.cc index eb97e03ea11ce1..5d081863cf2dcd 100644 --- a/src/crypto/crypto_dsa.cc +++ b/src/crypto/crypto_dsa.cc @@ -27,7 +27,7 @@ namespace node { using v8::FunctionCallbackInfo; using v8::Int32; -using v8::Just; +using v8::JustVoid; using v8::Local; using v8::Maybe; using v8::Nothing; @@ -78,7 +78,7 @@ EVPKeyCtxPointer DsaKeyGenTraits::Setup(DsaKeyPairGenConfig* params) { // 7. Private Type // 8. Cipher // 9. Passphrase -Maybe DsaKeyGenTraits::AdditionalConfig( +Maybe DsaKeyGenTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int* offset, @@ -92,14 +92,14 @@ Maybe DsaKeyGenTraits::AdditionalConfig( *offset += 2; - return Just(true); + return JustVoid(); } -Maybe DSAKeyExportTraits::AdditionalConfig( +Maybe DSAKeyExportTraits::AdditionalConfig( const FunctionCallbackInfo& args, unsigned int offset, DSAKeyExportConfig* params) { - return Just(true); + return JustVoid(); } WebCryptoKeyExportStatus DSAKeyExportTraits::DoExport( @@ -126,7 +126,7 @@ WebCryptoKeyExportStatus DSAKeyExportTraits::DoExport( } } -Maybe GetDsaKeyDetail(Environment* env, +Maybe GetDsaKeyDetail(Environment* env, const KeyObjectData& key, Local target) { const BIGNUM* p; // Modulus length @@ -157,10 +157,10 @@ Maybe GetDsaKeyDetail(Environment* env, env->divisor_length_string(), Number::New(env->isolate(), static_cast(divisor_length))) .IsNothing()) { - return Nothing(); + return Nothing(); } - return Just(true); + return JustVoid(); } namespace DSAAlg { diff --git a/src/crypto/crypto_dsa.h b/src/crypto/crypto_dsa.h index 5e55a5ace31165..fab167038c8862 100644 --- a/src/crypto/crypto_dsa.h +++ b/src/crypto/crypto_dsa.h @@ -28,7 +28,7 @@ struct DsaKeyGenTraits final { static EVPKeyCtxPointer Setup(DsaKeyPairGenConfig* params); - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int* offset, @@ -47,7 +47,7 @@ struct DSAKeyExportTraits final { static constexpr const char* JobName = "DSAKeyExportJob"; using AdditionalParameters = DSAKeyExportConfig; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( const v8::FunctionCallbackInfo& args, unsigned int offset, DSAKeyExportConfig* config); @@ -60,7 +60,7 @@ struct DSAKeyExportTraits final { using DSAKeyExportJob = KeyExportJob; -v8::Maybe GetDsaKeyDetail(Environment* env, +v8::Maybe GetDsaKeyDetail(Environment* env, const KeyObjectData& key, v8::Local target); diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index c2c1d961fb14bc..0c021eab60d509 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -26,10 +26,10 @@ using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Int32; using v8::Isolate; -using v8::Just; using v8::JustVoid; using v8::Local; using v8::Maybe; +using v8::MaybeLocal; using v8::Nothing; using v8::Object; using v8::String; @@ -437,16 +437,13 @@ void ECDHBitsConfig::MemoryInfo(MemoryTracker* tracker) const { tracker->TrackField("private", private_); } -Maybe ECDHBitsTraits::EncodeOutput( - Environment* env, - const ECDHBitsConfig& params, - ByteSource* out, - v8::Local* result) { - *result = out->ToArrayBuffer(env); - return Just(!result->IsEmpty()); +MaybeLocal ECDHBitsTraits::EncodeOutput(Environment* env, + const ECDHBitsConfig& params, + ByteSource* out) { + return out->ToArrayBuffer(env); } -Maybe ECDHBitsTraits::AdditionalConfig( +Maybe ECDHBitsTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -462,20 +459,20 @@ Maybe ECDHBitsTraits::AdditionalConfig( Utf8Value name(env->isolate(), args[offset]); - ASSIGN_OR_RETURN_UNWRAP(&public_key, args[offset + 1], Nothing()); - ASSIGN_OR_RETURN_UNWRAP(&private_key, args[offset + 2], Nothing()); + ASSIGN_OR_RETURN_UNWRAP(&public_key, args[offset + 1], Nothing()); + ASSIGN_OR_RETURN_UNWRAP(&private_key, args[offset + 2], Nothing()); if (private_key->Data().GetKeyType() != kKeyTypePrivate || public_key->Data().GetKeyType() != kKeyTypePublic) { THROW_ERR_CRYPTO_INVALID_KEYTYPE(env); - return Nothing(); + return Nothing(); } params->id_ = GetOKPCurveFromName(*name); params->private_ = private_key->Data().addRef(); params->public_ = public_key->Data().addRef(); - return Just(true); + return JustVoid(); } bool ECDHBitsTraits::DeriveBits(Environment* env, @@ -591,7 +588,7 @@ EVPKeyCtxPointer EcKeyGenTraits::Setup(EcKeyPairGenConfig* params) { // 7. Private Type // 8. Cipher // 9. Passphrase -Maybe EcKeyGenTraits::AdditionalConfig( +Maybe EcKeyGenTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int* offset, @@ -604,19 +601,19 @@ Maybe EcKeyGenTraits::AdditionalConfig( params->params.curve_nid = GetCurveFromName(*curve_name); if (params->params.curve_nid == NID_undef) { THROW_ERR_CRYPTO_INVALID_CURVE(env); - return Nothing(); + return Nothing(); } params->params.param_encoding = args[*offset + 1].As()->Value(); if (params->params.param_encoding != OPENSSL_EC_NAMED_CURVE && params->params.param_encoding != OPENSSL_EC_EXPLICIT_CURVE) { THROW_ERR_OUT_OF_RANGE(env, "Invalid param_encoding specified"); - return Nothing(); + return Nothing(); } *offset += 2; - return Just(true); + return JustVoid(); } namespace { @@ -677,11 +674,11 @@ WebCryptoKeyExportStatus EC_Raw_Export(const KeyObjectData& key_data, } } // namespace -Maybe ECKeyExportTraits::AdditionalConfig( +Maybe ECKeyExportTraits::AdditionalConfig( const FunctionCallbackInfo& args, unsigned int offset, ECKeyExportConfig* params) { - return Just(true); + return JustVoid(); } WebCryptoKeyExportStatus ECKeyExportTraits::DoExport( @@ -968,7 +965,7 @@ KeyObjectData ImportJWKEcKey(Environment* env, return KeyObjectData::CreateAsymmetric(type, std::move(pkey)); } -Maybe GetEcKeyDetail(Environment* env, +Maybe GetEcKeyDetail(Environment* env, const KeyObjectData& key, Local target) { Mutex::ScopedLock lock(key.mutex()); @@ -981,10 +978,14 @@ Maybe GetEcKeyDetail(Environment* env, const EC_GROUP* group = EC_KEY_get0_group(ec); int nid = EC_GROUP_get_curve_name(group); - return target->Set( - env->context(), - env->named_curve_string(), - OneByteString(env->isolate(), OBJ_nid2sn(nid))); + if (target + ->Set(env->context(), + env->named_curve_string(), + OneByteString(env->isolate(), OBJ_nid2sn(nid))) + .IsNothing()) { + return Nothing(); + } + return JustVoid(); } // WebCrypto requires a different format for ECDSA signatures than diff --git a/src/crypto/crypto_ec.h b/src/crypto/crypto_ec.h index 458bf8892f4d60..49a87d1663e7ee 100644 --- a/src/crypto/crypto_ec.h +++ b/src/crypto/crypto_ec.h @@ -71,7 +71,7 @@ struct ECDHBitsTraits final { static constexpr AsyncWrap::ProviderType Provider = AsyncWrap::PROVIDER_DERIVEBITSREQUEST; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, @@ -82,11 +82,9 @@ struct ECDHBitsTraits final { const ECDHBitsConfig& params, ByteSource* out_); - static v8::Maybe EncodeOutput( - Environment* env, - const ECDHBitsConfig& params, - ByteSource* out, - v8::Local* result); + static v8::MaybeLocal EncodeOutput(Environment* env, + const ECDHBitsConfig& params, + ByteSource* out); }; using ECDHBitsJob = DeriveBitsJob; @@ -107,7 +105,7 @@ struct EcKeyGenTraits final { static EVPKeyCtxPointer Setup(EcKeyPairGenConfig* params); - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int* offset, @@ -129,7 +127,7 @@ struct ECKeyExportTraits final { static constexpr const char* JobName = "ECKeyExportJob"; using AdditionalParameters = ECKeyExportConfig; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( const v8::FunctionCallbackInfo& args, unsigned int offset, ECKeyExportConfig* config); @@ -155,7 +153,7 @@ KeyObjectData ImportJWKEcKey(Environment* env, const v8::FunctionCallbackInfo& args, unsigned int offset); -v8::Maybe GetEcKeyDetail(Environment* env, +v8::Maybe GetEcKeyDetail(Environment* env, const KeyObjectData& key, v8::Local target); } // namespace crypto diff --git a/src/crypto/crypto_hash.cc b/src/crypto/crypto_hash.cc index 09ed200299646a..e27daa469c1915 100644 --- a/src/crypto/crypto_hash.cc +++ b/src/crypto/crypto_hash.cc @@ -17,6 +17,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; @@ -448,16 +449,13 @@ void HashConfig::MemoryInfo(MemoryTracker* tracker) const { tracker->TrackFieldWithSize("in", in.size()); } -Maybe HashTraits::EncodeOutput( - Environment* env, - const HashConfig& params, - ByteSource* out, - v8::Local* result) { - *result = out->ToArrayBuffer(env); - return Just(!result->IsEmpty()); +MaybeLocal HashTraits::EncodeOutput(Environment* env, + const HashConfig& params, + ByteSource* out) { + return out->ToArrayBuffer(env); } -Maybe HashTraits::AdditionalConfig( +Maybe HashTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -471,13 +469,13 @@ Maybe HashTraits::AdditionalConfig( params->digest = EVP_get_digestbyname(*digest); if (UNLIKELY(params->digest == nullptr)) { THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *digest); - return Nothing(); + return Nothing(); } ArrayBufferOrViewContents data(args[offset + 1]); if (UNLIKELY(!data.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "data is too big"); - return Nothing(); + return Nothing(); } params->in = mode == kCryptoJobAsync ? data.ToCopy() @@ -493,12 +491,12 @@ Maybe HashTraits::AdditionalConfig( if (params->length != expected) { if ((EVP_MD_flags(params->digest) & EVP_MD_FLAG_XOF) == 0) { THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Digest method not supported"); - return Nothing(); + return Nothing(); } } } - return Just(true); + return JustVoid(); } bool HashTraits::DeriveBits( diff --git a/src/crypto/crypto_hash.h b/src/crypto/crypto_hash.h index 07e3a2ae4635b8..4289c352a7cdae 100644 --- a/src/crypto/crypto_hash.h +++ b/src/crypto/crypto_hash.h @@ -64,7 +64,7 @@ struct HashTraits final { static constexpr AsyncWrap::ProviderType Provider = AsyncWrap::PROVIDER_HASHREQUEST; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, @@ -75,11 +75,9 @@ struct HashTraits final { const HashConfig& params, ByteSource* out); - static v8::Maybe EncodeOutput( - Environment* env, - const HashConfig& params, - ByteSource* out, - v8::Local* result); + static v8::MaybeLocal EncodeOutput(Environment* env, + const HashConfig& params, + ByteSource* out); }; using HashJob = DeriveBitsJob; diff --git a/src/crypto/crypto_hkdf.cc b/src/crypto/crypto_hkdf.cc index 7f65ee0b713af6..8a1782ef382b0d 100644 --- a/src/crypto/crypto_hkdf.cc +++ b/src/crypto/crypto_hkdf.cc @@ -10,8 +10,9 @@ namespace node { using v8::FunctionCallbackInfo; -using v8::Just; +using v8::JustVoid; using v8::Maybe; +using v8::MaybeLocal; using v8::Nothing; using v8::Uint32; using v8::Value; @@ -31,16 +32,13 @@ HKDFConfig& HKDFConfig::operator=(HKDFConfig&& other) noexcept { return *new (this) HKDFConfig(std::move(other)); } -Maybe HKDFTraits::EncodeOutput( - Environment* env, - const HKDFConfig& params, - ByteSource* out, - v8::Local* result) { - *result = out->ToArrayBuffer(env); - return Just(!result->IsEmpty()); +MaybeLocal HKDFTraits::EncodeOutput(Environment* env, + const HKDFConfig& params, + ByteSource* out) { + return out->ToArrayBuffer(env); } -Maybe HKDFTraits::AdditionalConfig( +Maybe HKDFTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -59,11 +57,11 @@ Maybe HKDFTraits::AdditionalConfig( params->digest = ncrypto::getDigestByName(hash.ToStringView()); if (params->digest == nullptr) { THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *hash); - return Nothing(); + return Nothing(); } KeyObjectHandle* key; - ASSIGN_OR_RETURN_UNWRAP(&key, args[offset + 1], Nothing()); + ASSIGN_OR_RETURN_UNWRAP(&key, args[offset + 1], Nothing()); params->key = key->Data().addRef(); ArrayBufferOrViewContents salt(args[offset + 2]); @@ -71,11 +69,11 @@ Maybe HKDFTraits::AdditionalConfig( if (UNLIKELY(!salt.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "salt is too big"); - return Nothing(); + return Nothing(); } if (UNLIKELY(!info.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "info is too big"); - return Nothing(); + return Nothing(); } params->salt = mode == kCryptoJobAsync @@ -92,10 +90,10 @@ Maybe HKDFTraits::AdditionalConfig( // 8-bit counter to each HMAC'd message, starting at 1. if (!ncrypto::checkHkdfLength(params->digest, params->length)) { THROW_ERR_CRYPTO_INVALID_KEYLEN(env); - return Nothing(); + return Nothing(); } - return Just(true); + return JustVoid(); } bool HKDFTraits::DeriveBits( diff --git a/src/crypto/crypto_hkdf.h b/src/crypto/crypto_hkdf.h index 40f70c2cf2a447..be6e823269d8ab 100644 --- a/src/crypto/crypto_hkdf.h +++ b/src/crypto/crypto_hkdf.h @@ -36,7 +36,7 @@ struct HKDFTraits final { static constexpr AsyncWrap::ProviderType Provider = AsyncWrap::PROVIDER_DERIVEBITSREQUEST; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, @@ -47,11 +47,9 @@ struct HKDFTraits final { const HKDFConfig& params, ByteSource* out); - static v8::Maybe EncodeOutput( - Environment* env, - const HKDFConfig& params, - ByteSource* out, - v8::Local* result); + static v8::MaybeLocal EncodeOutput(Environment* env, + const HKDFConfig& params, + ByteSource* out); }; using HKDFJob = DeriveBitsJob; diff --git a/src/crypto/crypto_hmac.cc b/src/crypto/crypto_hmac.cc index 24e8ce40d5f17a..30cb1205870908 100644 --- a/src/crypto/crypto_hmac.cc +++ b/src/crypto/crypto_hmac.cc @@ -18,7 +18,7 @@ using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; using v8::Isolate; -using v8::Just; +using v8::JustVoid; using v8::Local; using v8::Maybe; using v8::MaybeLocal; @@ -170,7 +170,7 @@ void HmacConfig::MemoryInfo(MemoryTracker* tracker) const { } } -Maybe HmacTraits::AdditionalConfig( +Maybe HmacTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -190,17 +190,17 @@ Maybe HmacTraits::AdditionalConfig( params->digest = EVP_get_digestbyname(*digest); if (params->digest == nullptr) { THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *digest); - return Nothing(); + return Nothing(); } KeyObjectHandle* key; - ASSIGN_OR_RETURN_UNWRAP(&key, args[offset + 2], Nothing()); + ASSIGN_OR_RETURN_UNWRAP(&key, args[offset + 2], Nothing()); params->key = key->Data().addRef(); ArrayBufferOrViewContents data(args[offset + 3]); if (UNLIKELY(!data.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "data is too big"); - return Nothing(); + return Nothing(); } params->data = mode == kCryptoJobAsync ? data.ToCopy() @@ -210,14 +210,14 @@ Maybe HmacTraits::AdditionalConfig( ArrayBufferOrViewContents signature(args[offset + 4]); if (UNLIKELY(!signature.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "signature is too big"); - return Nothing(); + return Nothing(); } params->signature = mode == kCryptoJobAsync ? signature.ToCopy() : signature.ToByteSource(); } - return Just(true); + return JustVoid(); } bool HmacTraits::DeriveBits( @@ -253,25 +253,19 @@ bool HmacTraits::DeriveBits( return true; } -Maybe HmacTraits::EncodeOutput( - Environment* env, - const HmacConfig& params, - ByteSource* out, - Local* result) { +MaybeLocal HmacTraits::EncodeOutput(Environment* env, + const HmacConfig& params, + ByteSource* out) { switch (params.mode) { case SignConfiguration::kSign: - *result = out->ToArrayBuffer(env); - break; + return out->ToArrayBuffer(env); case SignConfiguration::kVerify: - *result = Boolean::New( + return Boolean::New( env->isolate(), out->size() > 0 && out->size() == params.signature.size() && memcmp(out->data(), params.signature.data(), out->size()) == 0); - break; - default: - UNREACHABLE(); } - return Just(!result->IsEmpty()); + UNREACHABLE(); } } // namespace crypto diff --git a/src/crypto/crypto_hmac.h b/src/crypto/crypto_hmac.h index 9057664cc66f08..2f54b1c8a14af6 100644 --- a/src/crypto/crypto_hmac.h +++ b/src/crypto/crypto_hmac.h @@ -67,7 +67,7 @@ struct HmacTraits final { static constexpr AsyncWrap::ProviderType Provider = AsyncWrap::PROVIDER_SIGNREQUEST; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, @@ -78,11 +78,9 @@ struct HmacTraits final { const HmacConfig& params, ByteSource* out); - static v8::Maybe EncodeOutput( - Environment* env, - const HmacConfig& params, - ByteSource* out, - v8::Local* result); + static v8::MaybeLocal EncodeOutput(Environment* env, + const HmacConfig& params, + ByteSource* out); }; using HmacJob = DeriveBitsJob; diff --git a/src/crypto/crypto_keygen.cc b/src/crypto/crypto_keygen.cc index 24b99c184759f5..8a17cde8b9cac5 100644 --- a/src/crypto/crypto_keygen.cc +++ b/src/crypto/crypto_keygen.cc @@ -14,9 +14,10 @@ namespace node { using v8::FunctionCallbackInfo; using v8::Int32; -using v8::Just; +using v8::JustVoid; using v8::Local; using v8::Maybe; +using v8::MaybeLocal; using v8::Object; using v8::Uint32; using v8::Value; @@ -31,7 +32,7 @@ namespace crypto { // 6. Private Type // 7. Cipher // 8. Passphrase -Maybe NidKeyPairGenTraits::AdditionalConfig( +Maybe NidKeyPairGenTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int* offset, @@ -41,7 +42,7 @@ Maybe NidKeyPairGenTraits::AdditionalConfig( *offset += 1; - return Just(true); + return JustVoid(); } EVPKeyCtxPointer NidKeyPairGenTraits::Setup(NidKeyPairGenConfig* params) { @@ -57,7 +58,7 @@ void SecretKeyGenConfig::MemoryInfo(MemoryTracker* tracker) const { if (out) tracker->TrackFieldWithSize("out", length); } -Maybe SecretKeyGenTraits::AdditionalConfig( +Maybe SecretKeyGenTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int* offset, @@ -66,7 +67,7 @@ Maybe SecretKeyGenTraits::AdditionalConfig( uint32_t bits = args[*offset].As()->Value(); params->length = bits / CHAR_BIT; *offset += 1; - return Just(true); + return JustVoid(); } KeyGenJobStatus SecretKeyGenTraits::DoKeyGen(Environment* env, @@ -78,11 +79,14 @@ KeyGenJobStatus SecretKeyGenTraits::DoKeyGen(Environment* env, return KeyGenJobStatus::OK; } -Maybe SecretKeyGenTraits::EncodeKey(Environment* env, - SecretKeyGenConfig* params, - Local* result) { +MaybeLocal SecretKeyGenTraits::EncodeKey(Environment* env, + SecretKeyGenConfig* params) { auto data = KeyObjectData::CreateSecret(std::move(params->out)); - return Just(KeyObjectHandle::Create(env, data).ToLocal(result)); + Local ret; + if (!KeyObjectHandle::Create(env, data).ToLocal(&ret)) { + return MaybeLocal(); + } + return ret; } namespace Keygen { diff --git a/src/crypto/crypto_keygen.h b/src/crypto/crypto_keygen.h index 791ab4adae3fab..120769df3651e6 100644 --- a/src/crypto/crypto_keygen.h +++ b/src/crypto/crypto_keygen.h @@ -91,26 +91,33 @@ class KeyGenJob final : public CryptoJob { } } - v8::Maybe ToResult( - v8::Local* err, - v8::Local* result) override { + v8::Maybe ToResult(v8::Local* err, + v8::Local* result) override { Environment* env = AsyncWrap::env(); CryptoErrorStore* errors = CryptoJob::errors(); AdditionalParams* params = CryptoJob::params(); if (status_ == KeyGenJobStatus::OK) { - v8::Maybe ret = KeyGenTraits::EncodeKey(env, params, result); - if (ret.IsJust() && ret.FromJust()) { + v8::TryCatch try_catch(env->isolate()); + if (KeyGenTraits::EncodeKey(env, params).ToLocal(result)) { *err = Undefined(env->isolate()); + } else { + CHECK(try_catch.HasCaught()); + CHECK(try_catch.CanContinue()); + *result = Undefined(env->isolate()); + *err = try_catch.Exception(); + } + } else { + if (errors->Empty()) errors->Capture(); + CHECK(!errors->Empty()); + *result = Undefined(env->isolate()); + if (!errors->ToException(env).ToLocal(err)) { + return v8::Nothing(); } - return ret; } - - if (errors->Empty()) - errors->Capture(); - CHECK(!errors->Empty()); - *result = Undefined(env->isolate()); - return v8::Just(errors->ToException(env).ToLocal(err)); + CHECK(!result->IsEmpty()); + CHECK(!err->IsEmpty()); + return v8::JustVoid(); } SET_SELF_SIZE(KeyGenJob) @@ -129,7 +136,7 @@ struct KeyPairGenTraits final { AsyncWrap::PROVIDER_KEYPAIRGENREQUEST; static constexpr const char* JobName = KeyPairAlgorithmTraits::JobName; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int* offset, @@ -141,7 +148,7 @@ struct KeyPairGenTraits final { // number of input parameters specific to each job type. if (KeyPairAlgorithmTraits::AdditionalConfig(mode, args, offset, params) .IsNothing()) { - return v8::Just(false); + return v8::Nothing(); } params->public_key_encoding = KeyObjectData::GetPublicKeyEncodingFromJs( @@ -153,7 +160,7 @@ struct KeyPairGenTraits final { if (!private_key_encoding.IsEmpty()) params->private_key_encoding = private_key_encoding.Release(); - return v8::Just(true); + return v8::JustVoid(); } static KeyGenJobStatus DoKeyGen( @@ -176,10 +183,8 @@ struct KeyPairGenTraits final { return KeyGenJobStatus::OK; } - static v8::Maybe EncodeKey( - Environment* env, - AdditionalParameters* params, - v8::Local* result) { + static v8::MaybeLocal EncodeKey(Environment* env, + AdditionalParameters* params) { v8::Local keys[2]; if (params->key .ToEncodedPublicKey(env, params->public_key_encoding, &keys[0]) @@ -187,10 +192,9 @@ struct KeyPairGenTraits final { params->key .ToEncodedPrivateKey(env, params->private_key_encoding, &keys[1]) .IsNothing()) { - return v8::Nothing(); + return v8::MaybeLocal(); } - *result = v8::Array::New(env->isolate(), keys, arraysize(keys)); - return v8::Just(true); + return v8::Array::New(env->isolate(), keys, arraysize(keys)); } }; @@ -209,7 +213,7 @@ struct SecretKeyGenTraits final { AsyncWrap::PROVIDER_KEYGENREQUEST; static constexpr const char* JobName = "SecretKeyGenJob"; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int* offset, @@ -219,10 +223,8 @@ struct SecretKeyGenTraits final { Environment* env, SecretKeyGenConfig* params); - static v8::Maybe EncodeKey( - Environment* env, - SecretKeyGenConfig* params, - v8::Local* result); + static v8::MaybeLocal EncodeKey(Environment* env, + SecretKeyGenConfig* params); }; template @@ -281,7 +283,7 @@ struct NidKeyPairGenTraits final { static EVPKeyCtxPointer Setup(NidKeyPairGenConfig* params); - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int* offset, diff --git a/src/crypto/crypto_keys.cc b/src/crypto/crypto_keys.cc index 1fad03ca28d63e..1d280ddd068835 100644 --- a/src/crypto/crypto_keys.cc +++ b/src/crypto/crypto_keys.cc @@ -516,19 +516,23 @@ KeyObjectData ImportJWKAsymmetricKey(Environment* env, return {}; } -Maybe GetSecretKeyDetail(Environment* env, +Maybe GetSecretKeyDetail(Environment* env, const KeyObjectData& key, Local target) { // For the secret key detail, all we care about is the length, // converted to bits. - size_t length = key.GetSymmetricKeySize() * CHAR_BIT; - return target->Set(env->context(), - env->length_string(), - Number::New(env->isolate(), static_cast(length))); + if (target + ->Set(env->context(), + env->length_string(), + Number::New(env->isolate(), static_cast(length))) + .IsNothing()) { + return Nothing(); + } + return JustVoid(); } -Maybe GetAsymmetricKeyDetail(Environment* env, +Maybe GetAsymmetricKeyDetail(Environment* env, const KeyObjectData& key, Local target) { switch (EVP_PKEY_id(key.GetAsymmetricKey().get())) { @@ -540,7 +544,7 @@ Maybe GetAsymmetricKeyDetail(Environment* env, case EVP_PKEY_DH: return GetDhKeyDetail(env, key, target); } THROW_ERR_CRYPTO_INVALID_KEYTYPE(env); - return Nothing(); + return Nothing(); } } // namespace diff --git a/src/crypto/crypto_keys.h b/src/crypto/crypto_keys.h index 808e22e8e16281..efc6d583dccdea 100644 --- a/src/crypto/crypto_keys.h +++ b/src/crypto/crypto_keys.h @@ -362,23 +362,28 @@ class KeyExportJob final : public CryptoJob { } } - v8::Maybe ToResult( - v8::Local* err, - v8::Local* result) override { + v8::Maybe ToResult(v8::Local* err, + v8::Local* result) override { Environment* env = AsyncWrap::env(); CryptoErrorStore* errors = CryptoJob::errors(); if (out_.size() > 0) { CHECK(errors->Empty()); *err = v8::Undefined(env->isolate()); *result = out_.ToArrayBuffer(env); - return v8::Just(!result->IsEmpty()); + if (result->IsEmpty()) { + return v8::Nothing(); + } + } else { + if (errors->Empty()) errors->Capture(); + CHECK(!errors->Empty()); + *result = v8::Undefined(env->isolate()); + if (!errors->ToException(env).ToLocal(err)) { + return v8::Nothing(); + } } - - if (errors->Empty()) - errors->Capture(); - CHECK(!errors->Empty()); - *result = v8::Undefined(env->isolate()); - return v8::Just(errors->ToException(env).ToLocal(err)); + CHECK(!result->IsEmpty()); + CHECK(!err->IsEmpty()); + return v8::JustVoid(); } SET_SELF_SIZE(KeyExportJob) diff --git a/src/crypto/crypto_pbkdf2.cc b/src/crypto/crypto_pbkdf2.cc index 89b48c8c8761cd..68b7304d85d081 100644 --- a/src/crypto/crypto_pbkdf2.cc +++ b/src/crypto/crypto_pbkdf2.cc @@ -11,8 +11,9 @@ namespace node { using v8::FunctionCallbackInfo; using v8::Int32; -using v8::Just; +using v8::JustVoid; using v8::Maybe; +using v8::MaybeLocal; using v8::Nothing; using v8::Value; @@ -39,13 +40,10 @@ void PBKDF2Config::MemoryInfo(MemoryTracker* tracker) const { } } -Maybe PBKDF2Traits::EncodeOutput( - Environment* env, - const PBKDF2Config& params, - ByteSource* out, - v8::Local* result) { - *result = out->ToArrayBuffer(env); - return Just(!result->IsEmpty()); +MaybeLocal PBKDF2Traits::EncodeOutput(Environment* env, + const PBKDF2Config& params, + ByteSource* out) { + return out->ToArrayBuffer(env); } // The input arguments for the job are: @@ -55,7 +53,7 @@ Maybe PBKDF2Traits::EncodeOutput( // 4. The number of iterations // 5. The number of bytes to generate // 6. The digest algorithm name -Maybe PBKDF2Traits::AdditionalConfig( +Maybe PBKDF2Traits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -69,12 +67,12 @@ Maybe PBKDF2Traits::AdditionalConfig( if (UNLIKELY(!pass.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "pass is too large"); - return Nothing(); + return Nothing(); } if (UNLIKELY(!salt.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "salt is too large"); - return Nothing(); + return Nothing(); } params->pass = mode == kCryptoJobAsync @@ -92,23 +90,23 @@ Maybe PBKDF2Traits::AdditionalConfig( params->iterations = args[offset + 2].As()->Value(); if (params->iterations < 0) { THROW_ERR_OUT_OF_RANGE(env, "iterations must be <= %d", INT_MAX); - return Nothing(); + return Nothing(); } params->length = args[offset + 3].As()->Value(); if (params->length < 0) { THROW_ERR_OUT_OF_RANGE(env, "length must be <= %d", INT_MAX); - return Nothing(); + return Nothing(); } Utf8Value name(args.GetIsolate(), args[offset + 4]); params->digest = ncrypto::getDigestByName(name.ToStringView()); if (params->digest == nullptr) { THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *name); - return Nothing(); + return Nothing(); } - return Just(true); + return JustVoid(); } bool PBKDF2Traits::DeriveBits(Environment* env, diff --git a/src/crypto/crypto_pbkdf2.h b/src/crypto/crypto_pbkdf2.h index 6fda7cd3101002..604736f308b7d6 100644 --- a/src/crypto/crypto_pbkdf2.h +++ b/src/crypto/crypto_pbkdf2.h @@ -49,7 +49,7 @@ struct PBKDF2Traits final { static constexpr AsyncWrap::ProviderType Provider = AsyncWrap::PROVIDER_PBKDF2REQUEST; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, @@ -60,11 +60,9 @@ struct PBKDF2Traits final { const PBKDF2Config& params, ByteSource* out); - static v8::Maybe EncodeOutput( - Environment* env, - const PBKDF2Config& params, - ByteSource* out, - v8::Local* result); + static v8::MaybeLocal EncodeOutput(Environment* env, + const PBKDF2Config& params, + ByteSource* out); }; using PBKDF2Job = DeriveBitsJob; diff --git a/src/crypto/crypto_random.cc b/src/crypto/crypto_random.cc index 7ef2526e986617..b59e394d9a7e2c 100644 --- a/src/crypto/crypto_random.cc +++ b/src/crypto/crypto_random.cc @@ -18,25 +18,22 @@ using v8::BackingStore; using v8::Boolean; using v8::FunctionCallbackInfo; using v8::Int32; -using v8::Just; +using v8::JustVoid; using v8::Local; using v8::Maybe; +using v8::MaybeLocal; using v8::Nothing; using v8::Object; using v8::Uint32; using v8::Value; namespace crypto { -Maybe RandomBytesTraits::EncodeOutput( - Environment* env, - const RandomBytesConfig& params, - ByteSource* unused, - v8::Local* result) { - *result = v8::Undefined(env->isolate()); - return Just(!result->IsEmpty()); +MaybeLocal RandomBytesTraits::EncodeOutput( + Environment* env, const RandomBytesConfig& params, ByteSource* unused) { + return v8::Undefined(env->isolate()); } -Maybe RandomBytesTraits::AdditionalConfig( +Maybe RandomBytesTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -55,7 +52,7 @@ Maybe RandomBytesTraits::AdditionalConfig( params->buffer = in.data() + byte_offset; params->size = size; - return Just(true); + return JustVoid(); } bool RandomBytesTraits::DeriveBits( @@ -69,11 +66,8 @@ void RandomPrimeConfig::MemoryInfo(MemoryTracker* tracker) const { tracker->TrackFieldWithSize("prime", prime ? bits * 8 : 0); } -Maybe RandomPrimeTraits::EncodeOutput( - Environment* env, - const RandomPrimeConfig& params, - ByteSource* unused, - v8::Local* result) { +MaybeLocal RandomPrimeTraits::EncodeOutput( + Environment* env, const RandomPrimeConfig& params, ByteSource* unused) { size_t size = params.prime.byteLength(); std::shared_ptr store = ArrayBuffer::NewBackingStore(env->isolate(), size); @@ -82,11 +76,10 @@ Maybe RandomPrimeTraits::EncodeOutput( params.prime.get(), reinterpret_cast(store->Data()), size)); - *result = ArrayBuffer::New(env->isolate(), store); - return Just(true); + return ArrayBuffer::New(env->isolate(), store); } -Maybe RandomPrimeTraits::AdditionalConfig( +Maybe RandomPrimeTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -104,7 +97,7 @@ Maybe RandomPrimeTraits::AdditionalConfig( params->add.reset(add.data(), add.size()); if (!params->add) { THROW_ERR_CRYPTO_OPERATION_FAILED(env, "could not generate prime"); - return Nothing(); + return Nothing(); } } @@ -113,7 +106,7 @@ Maybe RandomPrimeTraits::AdditionalConfig( params->rem.reset(rem.data(), rem.size()); if (!params->rem) { THROW_ERR_CRYPTO_OPERATION_FAILED(env, "could not generate prime"); - return Nothing(); + return Nothing(); } } @@ -128,14 +121,14 @@ Maybe RandomPrimeTraits::AdditionalConfig( // loop within OpenSSL, blocking the main thread or one of the threads // in the thread pool. THROW_ERR_OUT_OF_RANGE(env, "invalid options.add"); - return Nothing(); + return Nothing(); } if (params->rem && params->add <= params->rem) { // This would definitely lead to an infinite loop if allowed since // OpenSSL does not check this condition. THROW_ERR_OUT_OF_RANGE(env, "invalid options.rem"); - return Nothing(); + return Nothing(); } } @@ -144,10 +137,10 @@ Maybe RandomPrimeTraits::AdditionalConfig( params->prime = BignumPointer::NewSecure(); if (!params->prime) { THROW_ERR_CRYPTO_OPERATION_FAILED(env, "could not generate prime"); - return Nothing(); + return Nothing(); } - return Just(true); + return JustVoid(); } bool RandomPrimeTraits::DeriveBits(Environment* env, @@ -174,7 +167,7 @@ void CheckPrimeConfig::MemoryInfo(MemoryTracker* tracker) const { tracker->TrackFieldWithSize("prime", candidate ? candidate.byteLength() : 0); } -Maybe CheckPrimeTraits::AdditionalConfig( +Maybe CheckPrimeTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -187,7 +180,7 @@ Maybe CheckPrimeTraits::AdditionalConfig( params->checks = args[offset + 1].As()->Value(); CHECK_GE(params->checks, 0); - return Just(true); + return JustVoid(); } bool CheckPrimeTraits::DeriveBits( @@ -209,13 +202,10 @@ bool CheckPrimeTraits::DeriveBits( return true; } -Maybe CheckPrimeTraits::EncodeOutput( - Environment* env, - const CheckPrimeConfig& params, - ByteSource* out, - v8::Local* result) { - *result = Boolean::New(env->isolate(), out->data()[0] != 0); - return Just(true); +MaybeLocal CheckPrimeTraits::EncodeOutput(Environment* env, + const CheckPrimeConfig& params, + ByteSource* out) { + return Boolean::New(env->isolate(), out->data()[0] != 0); } namespace Random { diff --git a/src/crypto/crypto_random.h b/src/crypto/crypto_random.h index a2807ed6ec8743..01cfc38f45e2f9 100644 --- a/src/crypto/crypto_random.h +++ b/src/crypto/crypto_random.h @@ -26,7 +26,7 @@ struct RandomBytesTraits final { static constexpr AsyncWrap::ProviderType Provider = AsyncWrap::PROVIDER_RANDOMBYTESREQUEST; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, @@ -37,11 +37,9 @@ struct RandomBytesTraits final { const RandomBytesConfig& params, ByteSource* out_); - static v8::Maybe EncodeOutput( - Environment* env, - const RandomBytesConfig& params, - ByteSource* unused, - v8::Local* result); + static v8::MaybeLocal EncodeOutput(Environment* env, + const RandomBytesConfig& params, + ByteSource* unused); }; using RandomBytesJob = DeriveBitsJob; @@ -63,7 +61,7 @@ struct RandomPrimeTraits final { static constexpr AsyncWrap::ProviderType Provider = AsyncWrap::PROVIDER_RANDOMPRIMEREQUEST; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, @@ -74,11 +72,9 @@ struct RandomPrimeTraits final { const RandomPrimeConfig& params, ByteSource* out_); - static v8::Maybe EncodeOutput( - Environment* env, - const RandomPrimeConfig& params, - ByteSource* unused, - v8::Local* result); + static v8::MaybeLocal EncodeOutput(Environment* env, + const RandomPrimeConfig& params, + ByteSource* unused); }; using RandomPrimeJob = DeriveBitsJob; @@ -99,7 +95,7 @@ struct CheckPrimeTraits final { static constexpr AsyncWrap::ProviderType Provider = AsyncWrap::PROVIDER_CHECKPRIMEREQUEST; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, @@ -110,11 +106,9 @@ struct CheckPrimeTraits final { const CheckPrimeConfig& params, ByteSource* out); - static v8::Maybe EncodeOutput( - Environment* env, - const CheckPrimeConfig& params, - ByteSource* out, - v8::Local* result); + static v8::MaybeLocal EncodeOutput(Environment* env, + const CheckPrimeConfig& params, + ByteSource* out); }; using CheckPrimeJob = DeriveBitsJob; diff --git a/src/crypto/crypto_rsa.cc b/src/crypto/crypto_rsa.cc index b52982f27b66cf..02e8e24b4054af 100644 --- a/src/crypto/crypto_rsa.cc +++ b/src/crypto/crypto_rsa.cc @@ -18,7 +18,6 @@ using v8::ArrayBuffer; using v8::BackingStore; using v8::FunctionCallbackInfo; using v8::Int32; -using v8::Just; using v8::JustVoid; using v8::Local; using v8::Maybe; @@ -123,7 +122,7 @@ EVPKeyCtxPointer RsaKeyGenTraits::Setup(RsaKeyPairGenConfig* params) { // 11. Private Type // 12. Cipher // 13. Passphrase -Maybe RsaKeyGenTraits::AdditionalConfig( +Maybe RsaKeyGenTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int* offset, @@ -154,7 +153,7 @@ Maybe RsaKeyGenTraits::AdditionalConfig( params->params.md = EVP_get_digestbyname(*digest); if (params->params.md == nullptr) { THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *digest); - return Nothing(); + return Nothing(); } } @@ -165,7 +164,7 @@ Maybe RsaKeyGenTraits::AdditionalConfig( if (params->params.mgf1_md == nullptr) { THROW_ERR_CRYPTO_INVALID_DIGEST( env, "Invalid MGF1 digest: %s", *digest); - return Nothing(); + return Nothing(); } } @@ -176,14 +175,14 @@ Maybe RsaKeyGenTraits::AdditionalConfig( THROW_ERR_OUT_OF_RANGE( env, "salt length is out of range"); - return Nothing(); + return Nothing(); } } *offset += 3; } - return Just(true); + return JustVoid(); } namespace { @@ -246,14 +245,14 @@ WebCryptoCipherStatus RSA_Cipher(Environment* env, } } // namespace -Maybe RSAKeyExportTraits::AdditionalConfig( +Maybe RSAKeyExportTraits::AdditionalConfig( const FunctionCallbackInfo& args, unsigned int offset, RSAKeyExportConfig* params) { CHECK(args[offset]->IsUint32()); // RSAKeyVariant params->variant = static_cast(args[offset].As()->Value()); - return Just(true); + return JustVoid(); } WebCryptoKeyExportStatus RSAKeyExportTraits::DoExport( @@ -293,7 +292,7 @@ void RSACipherConfig::MemoryInfo(MemoryTracker* tracker) const { tracker->TrackFieldWithSize("label", label.size()); } -Maybe RSACipherTraits::AdditionalConfig( +Maybe RSACipherTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -316,14 +315,14 @@ Maybe RSACipherTraits::AdditionalConfig( params->digest = EVP_get_digestbyname(*digest); if (params->digest == nullptr) { THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *digest); - return Nothing(); + return Nothing(); } if (IsAnyBufferSource(args[offset + 2])) { ArrayBufferOrViewContents label(args[offset + 2]); if (UNLIKELY(!label.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "label is too big"); - return Nothing(); + return Nothing(); } params->label = label.ToCopy(); } @@ -331,10 +330,10 @@ Maybe RSACipherTraits::AdditionalConfig( } default: THROW_ERR_CRYPTO_INVALID_KEYTYPE(env); - return Nothing(); + return Nothing(); } - return Just(true); + return JustVoid(); } WebCryptoCipherStatus RSACipherTraits::DoCipher(Environment* env, @@ -500,7 +499,7 @@ KeyObjectData ImportJWKRsaKey(Environment* env, return KeyObjectData::CreateAsymmetric(type, std::move(pkey)); } -Maybe GetRsaKeyDetail(Environment* env, +Maybe GetRsaKeyDetail(Environment* env, const KeyObjectData& key, Local target) { const BIGNUM* e; // Public Exponent @@ -529,7 +528,7 @@ Maybe GetRsaKeyDetail(Environment* env, Number::New(env->isolate(), static_cast(BignumPointer::GetBitCount(n)))) .IsNothing()) { - return Nothing(); + return Nothing(); } std::unique_ptr public_exponent; @@ -549,7 +548,7 @@ Maybe GetRsaKeyDetail(Environment* env, env->public_exponent_string(), ArrayBuffer::New(env->isolate(), std::move(public_exponent))) .IsNothing()) { - return Nothing(); + return Nothing(); } if (type == EVP_PKEY_RSA_PSS) { @@ -581,7 +580,7 @@ Maybe GetRsaKeyDetail(Environment* env, env->hash_algorithm_string(), OneByteString(env->isolate(), OBJ_nid2ln(hash_nid))) .IsNothing()) { - return Nothing(); + return Nothing(); } if (params->maskGenAlgorithm != nullptr) { @@ -604,14 +603,14 @@ Maybe GetRsaKeyDetail(Environment* env, env->mgf1_hash_algorithm_string(), OneByteString(env->isolate(), OBJ_nid2ln(mgf1_hash_nid))) .IsNothing()) { - return Nothing(); + return Nothing(); } } if (params->saltLength != nullptr) { if (ASN1_INTEGER_get_int64(&salt_length, params->saltLength) != 1) { ThrowCryptoError(env, ERR_get_error(), "ASN1_INTEGER_get_in64 error"); - return Nothing(); + return Nothing(); } } @@ -621,12 +620,12 @@ Maybe GetRsaKeyDetail(Environment* env, env->salt_length_string(), Number::New(env->isolate(), static_cast(salt_length))) .IsNothing()) { - return Nothing(); + return Nothing(); } } } - return Just(true); + return JustVoid(); } namespace RSAAlg { diff --git a/src/crypto/crypto_rsa.h b/src/crypto/crypto_rsa.h index 3f7afbd5c286ab..cbab0ef15acdf7 100644 --- a/src/crypto/crypto_rsa.h +++ b/src/crypto/crypto_rsa.h @@ -43,7 +43,7 @@ struct RsaKeyGenTraits final { static EVPKeyCtxPointer Setup(RsaKeyPairGenConfig* params); - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int* offset, @@ -63,7 +63,7 @@ struct RSAKeyExportTraits final { static constexpr const char* JobName = "RSAKeyExportJob"; using AdditionalParameters = RSAKeyExportConfig; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( const v8::FunctionCallbackInfo& args, unsigned int offset, RSAKeyExportConfig* config); @@ -95,7 +95,7 @@ struct RSACipherTraits final { static constexpr const char* JobName = "RSACipherJob"; using AdditionalParameters = RSACipherConfig; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, @@ -121,7 +121,7 @@ KeyObjectData ImportJWKRsaKey(Environment* env, const v8::FunctionCallbackInfo& args, unsigned int offset); -v8::Maybe GetRsaKeyDetail(Environment* env, +v8::Maybe GetRsaKeyDetail(Environment* env, const KeyObjectData& key, v8::Local target); diff --git a/src/crypto/crypto_scrypt.cc b/src/crypto/crypto_scrypt.cc index d0e17fd4ef0837..b63386d876a3b6 100644 --- a/src/crypto/crypto_scrypt.cc +++ b/src/crypto/crypto_scrypt.cc @@ -11,8 +11,9 @@ namespace node { using v8::FunctionCallbackInfo; using v8::Int32; -using v8::Just; +using v8::JustVoid; using v8::Maybe; +using v8::MaybeLocal; using v8::Nothing; using v8::Uint32; using v8::Value; @@ -43,16 +44,13 @@ void ScryptConfig::MemoryInfo(MemoryTracker* tracker) const { } } -Maybe ScryptTraits::EncodeOutput( - Environment* env, - const ScryptConfig& params, - ByteSource* out, - v8::Local* result) { - *result = out->ToArrayBuffer(env); - return Just(!result->IsEmpty()); +MaybeLocal ScryptTraits::EncodeOutput(Environment* env, + const ScryptConfig& params, + ByteSource* out) { + return out->ToArrayBuffer(env); } -Maybe ScryptTraits::AdditionalConfig( +Maybe ScryptTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -66,12 +64,12 @@ Maybe ScryptTraits::AdditionalConfig( if (UNLIKELY(!pass.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "pass is too large"); - return Nothing(); + return Nothing(); } if (UNLIKELY(!salt.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "salt is too large"); - return Nothing(); + return Nothing(); } params->pass = mode == kCryptoJobAsync @@ -109,10 +107,10 @@ Maybe ScryptTraits::AdditionalConfig( } else { THROW_ERR_CRYPTO_INVALID_SCRYPT_PARAMS(env); } - return Nothing(); + return Nothing(); } - return Just(true); + return JustVoid(); } bool ScryptTraits::DeriveBits( diff --git a/src/crypto/crypto_scrypt.h b/src/crypto/crypto_scrypt.h index 3d185637f44be3..68a516907328af 100644 --- a/src/crypto/crypto_scrypt.h +++ b/src/crypto/crypto_scrypt.h @@ -51,7 +51,7 @@ struct ScryptTraits final { static constexpr AsyncWrap::ProviderType Provider = AsyncWrap::PROVIDER_SCRYPTREQUEST; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, @@ -62,11 +62,9 @@ struct ScryptTraits final { const ScryptConfig& params, ByteSource* out); - static v8::Maybe EncodeOutput( - Environment* env, - const ScryptConfig& params, - ByteSource* out, - v8::Local* result); + static v8::MaybeLocal EncodeOutput(Environment* env, + const ScryptConfig& params, + ByteSource* out); }; using ScryptJob = DeriveBitsJob; diff --git a/src/crypto/crypto_sig.cc b/src/crypto/crypto_sig.cc index 98e33fcc2c099a..903ab29cd7e701 100644 --- a/src/crypto/crypto_sig.cc +++ b/src/crypto/crypto_sig.cc @@ -21,8 +21,10 @@ using v8::HandleScope; using v8::Int32; using v8::Isolate; using v8::Just; +using v8::JustVoid; using v8::Local; using v8::Maybe; +using v8::MaybeLocal; using v8::Nothing; using v8::Object; using v8::Uint32; @@ -627,7 +629,7 @@ void SignConfiguration::MemoryInfo(MemoryTracker* tracker) const { } } -Maybe SignTraits::AdditionalConfig( +Maybe SignTraits::AdditionalConfig( CryptoJobMode mode, const FunctionCallbackInfo& args, unsigned int offset, @@ -646,18 +648,18 @@ Maybe SignTraits::AdditionalConfig( if (params->mode == SignConfiguration::kVerify) { auto data = KeyObjectData::GetPublicOrPrivateKeyFromJs(args, &keyParamOffset); - if (!data) return Nothing(); + if (!data) return Nothing(); params->key = std::move(data); } else { auto data = KeyObjectData::GetPrivateKeyFromJs(args, &keyParamOffset, true); - if (!data) return Nothing(); + if (!data) return Nothing(); params->key = std::move(data); } ArrayBufferOrViewContents data(args[offset + 5]); if (UNLIKELY(!data.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "data is too big"); - return Nothing(); + return Nothing(); } params->data = mode == kCryptoJobAsync ? data.ToCopy() @@ -668,7 +670,7 @@ Maybe SignTraits::AdditionalConfig( params->digest = EVP_get_digestbyname(*digest); if (params->digest == nullptr) { THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *digest); - return Nothing(); + return Nothing(); } } @@ -687,7 +689,7 @@ Maybe SignTraits::AdditionalConfig( if (params->dsa_encoding != kSigEncDER && params->dsa_encoding != kSigEncP1363) { THROW_ERR_OUT_OF_RANGE(env, "invalid signature encoding"); - return Nothing(); + return Nothing(); } } @@ -695,7 +697,7 @@ Maybe SignTraits::AdditionalConfig( ArrayBufferOrViewContents signature(args[offset + 10]); if (UNLIKELY(!signature.CheckSizeInt32())) { THROW_ERR_OUT_OF_RANGE(env, "signature is too big"); - return Nothing(); + return Nothing(); } // If this is an EC key (assuming ECDSA) we need to convert the // the signature from WebCrypto format into DER format... @@ -710,7 +712,7 @@ Maybe SignTraits::AdditionalConfig( } } - return Just(true); + return JustVoid(); } bool SignTraits::DeriveBits( @@ -818,22 +820,16 @@ bool SignTraits::DeriveBits( return true; } -Maybe SignTraits::EncodeOutput( - Environment* env, - const SignConfiguration& params, - ByteSource* out, - Local* result) { +MaybeLocal SignTraits::EncodeOutput(Environment* env, + const SignConfiguration& params, + ByteSource* out) { switch (params.mode) { case SignConfiguration::kSign: - *result = out->ToArrayBuffer(env); - break; + return out->ToArrayBuffer(env); case SignConfiguration::kVerify: - *result = Boolean::New(env->isolate(), out->data()[0] == 1); - break; - default: - UNREACHABLE(); + return Boolean::New(env->isolate(), out->data()[0] == 1); } - return Just(!result->IsEmpty()); + UNREACHABLE(); } } // namespace crypto diff --git a/src/crypto/crypto_sig.h b/src/crypto/crypto_sig.h index f0e0dcf2cf4bae..14c553fe3fca66 100644 --- a/src/crypto/crypto_sig.h +++ b/src/crypto/crypto_sig.h @@ -140,7 +140,7 @@ struct SignTraits final { static constexpr AsyncWrap::ProviderType Provider = AsyncWrap::PROVIDER_SIGNREQUEST; - static v8::Maybe AdditionalConfig( + static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, @@ -151,11 +151,9 @@ struct SignTraits final { const SignConfiguration& params, ByteSource* out); - static v8::Maybe EncodeOutput( - Environment* env, - const SignConfiguration& params, - ByteSource* out, - v8::Local* result); + static v8::MaybeLocal EncodeOutput(Environment* env, + const SignConfiguration& params, + ByteSource* out); }; using SignJob = DeriveBitsJob; diff --git a/src/crypto/crypto_util.h b/src/crypto/crypto_util.h index 8756008d4ce3d2..922e77091d7217 100644 --- a/src/crypto/crypto_util.h +++ b/src/crypto/crypto_util.h @@ -361,31 +361,31 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork { v8::HandleScope handle_scope(env->isolate()); v8::Context::Scope context_scope(env->context()); - // TODO(tniessen): Remove the exception handling logic here as soon as we - // can verify that no code path in ToResult will ever throw an exception. v8::Local exception; v8::Local args[2]; { node::errors::TryCatchScope try_catch(env); - v8::Maybe ret = ptr->ToResult(&args[0], &args[1]); - if (!ret.IsJust()) { + // If ToResult returns Nothing, then an exception should have been + // thrown and we should have caught it. Otherwise, args[0] and args[1] + // both should have been set to a value, even if the value is undefined. + if (ptr->ToResult(&args[0], &args[1]).IsNothing()) { CHECK(try_catch.HasCaught()); + CHECK(try_catch.CanContinue()); exception = try_catch.Exception(); - } else if (!ret.FromJust()) { - return; } } - if (exception.IsEmpty()) { - ptr->MakeCallback(env->ondone_string(), arraysize(args), args); - } else { + if (!exception.IsEmpty()) { ptr->MakeCallback(env->ondone_string(), 1, &exception); + } else { + CHECK(!args[0].IsEmpty()); + CHECK(!args[1].IsEmpty()); + ptr->MakeCallback(env->ondone_string(), arraysize(args), args); } } - virtual v8::Maybe ToResult( - v8::Local* err, - v8::Local* result) = 0; + virtual v8::Maybe ToResult(v8::Local* err, + v8::Local* result) = 0; CryptoJobMode mode() const { return mode_; } @@ -413,8 +413,9 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork { v8::Local ret[2]; env->PrintSyncTrace(); job->DoThreadPoolWork(); - v8::Maybe result = job->ToResult(&ret[0], &ret[1]); - if (result.IsJust() && result.FromJust()) { + if (job->ToResult(&ret[0], &ret[1]).IsJust()) { + CHECK(!ret[0].IsEmpty()); + CHECK(!ret[1].IsEmpty()); args.GetReturnValue().Set( v8::Array::New(env->isolate(), ret, arraysize(ret))); } @@ -504,26 +505,29 @@ class DeriveBitsJob final : public CryptoJob { success_ = true; } - v8::Maybe ToResult( - v8::Local* err, - v8::Local* result) override { + v8::Maybe ToResult(v8::Local* err, + v8::Local* result) override { Environment* env = AsyncWrap::env(); CryptoErrorStore* errors = CryptoJob::errors(); if (success_) { CHECK(errors->Empty()); *err = v8::Undefined(env->isolate()); - return DeriveBitsTraits::EncodeOutput( - env, - *CryptoJob::params(), - &out_, - result); + if (!DeriveBitsTraits::EncodeOutput( + env, *CryptoJob::params(), &out_) + .ToLocal(result)) { + return v8::Nothing(); + } + } else { + if (errors->Empty()) errors->Capture(); + CHECK(!errors->Empty()); + *result = v8::Undefined(env->isolate()); + if (!errors->ToException(env).ToLocal(err)) { + return v8::Nothing(); + } } - - if (errors->Empty()) - errors->Capture(); - CHECK(!errors->Empty()); - *result = v8::Undefined(env->isolate()); - return v8::Just(errors->ToException(env).ToLocal(err)); + CHECK(!result->IsEmpty()); + CHECK(!err->IsEmpty()); + return v8::JustVoid(); } SET_SELF_SIZE(DeriveBitsJob)