Skip to content

Commit

Permalink
src: switch crypto APIs to use Maybe<void>
Browse files Browse the repository at this point in the history
PR-URL: #54775
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
jasnell authored and aduh95 committed Sep 13, 2024
1 parent f084ea2 commit fbd08e3
Show file tree
Hide file tree
Showing 30 changed files with 351 additions and 378 deletions.
15 changes: 8 additions & 7 deletions src/crypto/crypto_aes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace node {

using v8::FunctionCallbackInfo;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
using v8::Nothing;
Expand Down Expand Up @@ -452,7 +453,7 @@ void AESCipherConfig::MemoryInfo(MemoryTracker* tracker) const {
}
}

Maybe<bool> AESCipherTraits::AdditionalConfig(
Maybe<void> AESCipherTraits::AdditionalConfig(
CryptoJobMode mode,
const FunctionCallbackInfo<Value>& args,
unsigned int offset,
Expand Down Expand Up @@ -482,22 +483,22 @@ Maybe<bool> AESCipherTraits::AdditionalConfig(
params->cipher = EVP_get_cipherbynid(cipher_nid);
if (params->cipher == nullptr) {
THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env);
return Nothing<bool>();
return Nothing<void>();
}

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<bool>();
return Nothing<void>();
}
if (cipher_op_mode == EVP_CIPH_CTR_MODE) {
if (!ValidateCounter(env, args[offset + 2], params)) {
return Nothing<bool>();
return Nothing<void>();
}
} 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<bool>();
return Nothing<void>();
}
}
} else {
Expand All @@ -507,10 +508,10 @@ Maybe<bool> AESCipherTraits::AdditionalConfig(
if (params->iv.size() <
static_cast<size_t>(EVP_CIPHER_iv_length(params->cipher))) {
THROW_ERR_CRYPTO_INVALID_IV(env);
return Nothing<bool>();
return Nothing<void>();
}

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

WebCryptoCipherStatus AESCipherTraits::DoCipher(Environment* env,
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct AESCipherTraits final {

using AdditionalParameters = AESCipherConfig;

static v8::Maybe<bool> AdditionalConfig(
static v8::Maybe<void> AdditionalConfig(
CryptoJobMode mode,
const v8::FunctionCallbackInfo<v8::Value>& args,
unsigned int offset,
Expand Down
20 changes: 13 additions & 7 deletions src/crypto/crypto_cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,8 @@ class CipherJob final : public CryptoJob<CipherTraits> {
}
}

v8::Maybe<bool> ToResult(
v8::Local<v8::Value>* err,
v8::Local<v8::Value>* result) override {
v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
v8::Local<v8::Value>* result) override {
Environment* env = AsyncWrap::env();
CryptoErrorStore* errors = CryptoJob<CipherTraits>::errors();

Expand All @@ -258,11 +257,18 @@ class CipherJob final : public CryptoJob<CipherTraits> {
CHECK(errors->Empty());
*err = v8::Undefined(env->isolate());
*result = out_.ToArrayBuffer(env);
return v8::Just(!result->IsEmpty());
if (result->IsEmpty()) {
return v8::Nothing<void>();
}
} else {
*result = v8::Undefined(env->isolate());
if (!errors->ToException(env).ToLocal(err)) {
return v8::Nothing<void>();
}
}

*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)
Expand Down
41 changes: 19 additions & 22 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -338,7 +338,7 @@ void Check(const FunctionCallbackInfo<Value>& args) {
// * Private type
// * Cipher
// * Passphrase
Maybe<bool> DhKeyGenTraits::AdditionalConfig(
Maybe<void> DhKeyGenTraits::AdditionalConfig(
CryptoJobMode mode,
const FunctionCallbackInfo<Value>& args,
unsigned int* offset,
Expand All @@ -350,7 +350,7 @@ Maybe<bool> DhKeyGenTraits::AdditionalConfig(
auto group = DHPointer::FindGroup(group_name.ToStringView());
if (!group) {
THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env);
return Nothing<bool>();
return Nothing<void>();
}

static constexpr int kStandardizedGenerator = 2;
Expand All @@ -363,14 +363,14 @@ Maybe<bool> DhKeyGenTraits::AdditionalConfig(
int size = args[*offset].As<Int32>()->Value();
if (size < 0) {
THROW_ERR_OUT_OF_RANGE(env, "Invalid prime size");
return Nothing<bool>();
return Nothing<void>();
}
params->params.prime = size;
} else {
ArrayBufferOrViewContents<unsigned char> input(args[*offset]);
if (UNLIKELY(!input.CheckSizeInt32())) {
THROW_ERR_OUT_OF_RANGE(env, "prime is too big");
return Nothing<bool>();
return Nothing<void>();
}
params->params.prime = BignumPointer(input.data(), input.size());
}
Expand All @@ -380,7 +380,7 @@ Maybe<bool> DhKeyGenTraits::AdditionalConfig(
*offset += 2;
}

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

EVPKeyCtxPointer DhKeyGenTraits::Setup(DhKeyPairGenConfig* params) {
Expand Down Expand Up @@ -424,11 +424,11 @@ EVPKeyCtxPointer DhKeyGenTraits::Setup(DhKeyPairGenConfig* params) {
return ctx;
}

Maybe<bool> DHKeyExportTraits::AdditionalConfig(
Maybe<void> DHKeyExportTraits::AdditionalConfig(
const FunctionCallbackInfo<Value>& args,
unsigned int offset,
DHKeyExportConfig* params) {
return Just(true);
return JustVoid();
}

WebCryptoKeyExportStatus DHKeyExportTraits::DoExport(
Expand Down Expand Up @@ -487,7 +487,7 @@ void Stateless(const FunctionCallbackInfo<Value>& args) {
}
} // namespace

Maybe<bool> DHBitsTraits::AdditionalConfig(
Maybe<void> DHBitsTraits::AdditionalConfig(
CryptoJobMode mode,
const FunctionCallbackInfo<Value>& args,
unsigned int offset,
Expand All @@ -500,28 +500,25 @@ Maybe<bool> DHBitsTraits::AdditionalConfig(
KeyObjectHandle* private_key;
KeyObjectHandle* public_key;

ASSIGN_OR_RETURN_UNWRAP(&public_key, args[offset], Nothing<bool>());
ASSIGN_OR_RETURN_UNWRAP(&private_key, args[offset + 1], Nothing<bool>());
ASSIGN_OR_RETURN_UNWRAP(&public_key, args[offset], Nothing<void>());
ASSIGN_OR_RETURN_UNWRAP(&private_key, args[offset + 1], Nothing<void>());

if (private_key->Data().GetKeyType() != kKeyTypePrivate ||
public_key->Data().GetKeyType() != kKeyTypePublic) {
THROW_ERR_CRYPTO_INVALID_KEYTYPE(env);
return Nothing<bool>();
return Nothing<void>();
}

params->public_key = public_key->Data().addRef();
params->private_key = private_key->Data().addRef();

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

Maybe<bool> DHBitsTraits::EncodeOutput(
Environment* env,
const DHBitsConfig& params,
ByteSource* out,
v8::Local<v8::Value>* result) {
*result = out->ToArrayBuffer(env);
return Just(!result->IsEmpty());
MaybeLocal<Value> DHBitsTraits::EncodeOutput(Environment* env,
const DHBitsConfig& params,
ByteSource* out) {
return out->ToArrayBuffer(env);
}

bool DHBitsTraits::DeriveBits(
Expand All @@ -533,11 +530,11 @@ bool DHBitsTraits::DeriveBits(
return true;
}

Maybe<bool> GetDhKeyDetail(Environment* env,
Maybe<void> GetDhKeyDetail(Environment* env,
const KeyObjectData& key,
Local<Object> target) {
CHECK_EQ(EVP_PKEY_id(key.GetAsymmetricKey().get()), EVP_PKEY_DH);
return Just(true);
return JustVoid();
}

void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
Expand Down
16 changes: 7 additions & 9 deletions src/crypto/crypto_dh.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct DhKeyGenTraits final {

static EVPKeyCtxPointer Setup(DhKeyPairGenConfig* params);

static v8::Maybe<bool> AdditionalConfig(
static v8::Maybe<void> AdditionalConfig(
CryptoJobMode mode,
const v8::FunctionCallbackInfo<v8::Value>& args,
unsigned int* offset,
Expand All @@ -68,7 +68,7 @@ struct DHKeyExportTraits final {
static constexpr const char* JobName = "DHKeyExportJob";
using AdditionalParameters = DHKeyExportConfig;

static v8::Maybe<bool> AdditionalConfig(
static v8::Maybe<void> AdditionalConfig(
const v8::FunctionCallbackInfo<v8::Value>& args,
unsigned int offset,
DHKeyExportConfig* config);
Expand All @@ -95,7 +95,7 @@ struct DHBitsTraits final {
static constexpr AsyncWrap::ProviderType Provider =
AsyncWrap::PROVIDER_DERIVEBITSREQUEST;

static v8::Maybe<bool> AdditionalConfig(
static v8::Maybe<void> AdditionalConfig(
CryptoJobMode mode,
const v8::FunctionCallbackInfo<v8::Value>& args,
unsigned int offset,
Expand All @@ -106,16 +106,14 @@ struct DHBitsTraits final {
const DHBitsConfig& params,
ByteSource* out_);

static v8::Maybe<bool> EncodeOutput(
Environment* env,
const DHBitsConfig& params,
ByteSource* out,
v8::Local<v8::Value>* result);
static v8::MaybeLocal<v8::Value> EncodeOutput(Environment* env,
const DHBitsConfig& params,
ByteSource* out);
};

using DHBitsJob = DeriveBitsJob<DHBitsTraits>;

v8::Maybe<bool> GetDhKeyDetail(Environment* env,
v8::Maybe<void> GetDhKeyDetail(Environment* env,
const KeyObjectData& key,
v8::Local<v8::Object> target);

Expand Down
16 changes: 8 additions & 8 deletions src/crypto/crypto_dsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,7 +78,7 @@ EVPKeyCtxPointer DsaKeyGenTraits::Setup(DsaKeyPairGenConfig* params) {
// 7. Private Type
// 8. Cipher
// 9. Passphrase
Maybe<bool> DsaKeyGenTraits::AdditionalConfig(
Maybe<void> DsaKeyGenTraits::AdditionalConfig(
CryptoJobMode mode,
const FunctionCallbackInfo<Value>& args,
unsigned int* offset,
Expand All @@ -92,14 +92,14 @@ Maybe<bool> DsaKeyGenTraits::AdditionalConfig(

*offset += 2;

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

Maybe<bool> DSAKeyExportTraits::AdditionalConfig(
Maybe<void> DSAKeyExportTraits::AdditionalConfig(
const FunctionCallbackInfo<Value>& args,
unsigned int offset,
DSAKeyExportConfig* params) {
return Just(true);
return JustVoid();
}

WebCryptoKeyExportStatus DSAKeyExportTraits::DoExport(
Expand All @@ -126,7 +126,7 @@ WebCryptoKeyExportStatus DSAKeyExportTraits::DoExport(
}
}

Maybe<bool> GetDsaKeyDetail(Environment* env,
Maybe<void> GetDsaKeyDetail(Environment* env,
const KeyObjectData& key,
Local<Object> target) {
const BIGNUM* p; // Modulus length
Expand Down Expand Up @@ -157,10 +157,10 @@ Maybe<bool> GetDsaKeyDetail(Environment* env,
env->divisor_length_string(),
Number::New(env->isolate(), static_cast<double>(divisor_length)))
.IsNothing()) {
return Nothing<bool>();
return Nothing<void>();
}

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

namespace DSAAlg {
Expand Down
6 changes: 3 additions & 3 deletions src/crypto/crypto_dsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct DsaKeyGenTraits final {

static EVPKeyCtxPointer Setup(DsaKeyPairGenConfig* params);

static v8::Maybe<bool> AdditionalConfig(
static v8::Maybe<void> AdditionalConfig(
CryptoJobMode mode,
const v8::FunctionCallbackInfo<v8::Value>& args,
unsigned int* offset,
Expand All @@ -47,7 +47,7 @@ struct DSAKeyExportTraits final {
static constexpr const char* JobName = "DSAKeyExportJob";
using AdditionalParameters = DSAKeyExportConfig;

static v8::Maybe<bool> AdditionalConfig(
static v8::Maybe<void> AdditionalConfig(
const v8::FunctionCallbackInfo<v8::Value>& args,
unsigned int offset,
DSAKeyExportConfig* config);
Expand All @@ -60,7 +60,7 @@ struct DSAKeyExportTraits final {

using DSAKeyExportJob = KeyExportJob<DSAKeyExportTraits>;

v8::Maybe<bool> GetDsaKeyDetail(Environment* env,
v8::Maybe<void> GetDsaKeyDetail(Environment* env,
const KeyObjectData& key,
v8::Local<v8::Object> target);

Expand Down
Loading

0 comments on commit fbd08e3

Please sign in to comment.