Skip to content

Commit fbd08e3

Browse files
jasnelladuh95
authored andcommitted
src: switch crypto APIs to use Maybe<void>
PR-URL: #54775 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent f084ea2 commit fbd08e3

30 files changed

+351
-378
lines changed

src/crypto/crypto_aes.cc

+8-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace node {
1818

1919
using v8::FunctionCallbackInfo;
2020
using v8::Just;
21+
using v8::JustVoid;
2122
using v8::Local;
2223
using v8::Maybe;
2324
using v8::Nothing;
@@ -452,7 +453,7 @@ void AESCipherConfig::MemoryInfo(MemoryTracker* tracker) const {
452453
}
453454
}
454455

455-
Maybe<bool> AESCipherTraits::AdditionalConfig(
456+
Maybe<void> AESCipherTraits::AdditionalConfig(
456457
CryptoJobMode mode,
457458
const FunctionCallbackInfo<Value>& args,
458459
unsigned int offset,
@@ -482,22 +483,22 @@ Maybe<bool> AESCipherTraits::AdditionalConfig(
482483
params->cipher = EVP_get_cipherbynid(cipher_nid);
483484
if (params->cipher == nullptr) {
484485
THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env);
485-
return Nothing<bool>();
486+
return Nothing<void>();
486487
}
487488

488489
int cipher_op_mode = EVP_CIPHER_mode(params->cipher);
489490
if (cipher_op_mode != EVP_CIPH_WRAP_MODE) {
490491
if (!ValidateIV(env, mode, args[offset + 1], params)) {
491-
return Nothing<bool>();
492+
return Nothing<void>();
492493
}
493494
if (cipher_op_mode == EVP_CIPH_CTR_MODE) {
494495
if (!ValidateCounter(env, args[offset + 2], params)) {
495-
return Nothing<bool>();
496+
return Nothing<void>();
496497
}
497498
} else if (cipher_op_mode == EVP_CIPH_GCM_MODE) {
498499
if (!ValidateAuthTag(env, mode, cipher_mode, args[offset + 2], params) ||
499500
!ValidateAdditionalData(env, mode, args[offset + 3], params)) {
500-
return Nothing<bool>();
501+
return Nothing<void>();
501502
}
502503
}
503504
} else {
@@ -507,10 +508,10 @@ Maybe<bool> AESCipherTraits::AdditionalConfig(
507508
if (params->iv.size() <
508509
static_cast<size_t>(EVP_CIPHER_iv_length(params->cipher))) {
509510
THROW_ERR_CRYPTO_INVALID_IV(env);
510-
return Nothing<bool>();
511+
return Nothing<void>();
511512
}
512513

513-
return Just(true);
514+
return JustVoid();
514515
}
515516

516517
WebCryptoCipherStatus AESCipherTraits::DoCipher(Environment* env,

src/crypto/crypto_aes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct AESCipherTraits final {
6060

6161
using AdditionalParameters = AESCipherConfig;
6262

63-
static v8::Maybe<bool> AdditionalConfig(
63+
static v8::Maybe<void> AdditionalConfig(
6464
CryptoJobMode mode,
6565
const v8::FunctionCallbackInfo<v8::Value>& args,
6666
unsigned int offset,

src/crypto/crypto_cipher.h

+13-7
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,8 @@ class CipherJob final : public CryptoJob<CipherTraits> {
245245
}
246246
}
247247

248-
v8::Maybe<bool> ToResult(
249-
v8::Local<v8::Value>* err,
250-
v8::Local<v8::Value>* result) override {
248+
v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
249+
v8::Local<v8::Value>* result) override {
251250
Environment* env = AsyncWrap::env();
252251
CryptoErrorStore* errors = CryptoJob<CipherTraits>::errors();
253252

@@ -258,11 +257,18 @@ class CipherJob final : public CryptoJob<CipherTraits> {
258257
CHECK(errors->Empty());
259258
*err = v8::Undefined(env->isolate());
260259
*result = out_.ToArrayBuffer(env);
261-
return v8::Just(!result->IsEmpty());
260+
if (result->IsEmpty()) {
261+
return v8::Nothing<void>();
262+
}
263+
} else {
264+
*result = v8::Undefined(env->isolate());
265+
if (!errors->ToException(env).ToLocal(err)) {
266+
return v8::Nothing<void>();
267+
}
262268
}
263-
264-
*result = v8::Undefined(env->isolate());
265-
return v8::Just(errors->ToException(env).ToLocal(err));
269+
CHECK(!result->IsEmpty());
270+
CHECK(!err->IsEmpty());
271+
return v8::JustVoid();
266272
}
267273

268274
SET_SELF_SIZE(CipherJob)

src/crypto/crypto_dh.cc

+19-22
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using v8::FunctionCallbackInfo;
2323
using v8::FunctionTemplate;
2424
using v8::Int32;
2525
using v8::Isolate;
26-
using v8::Just;
26+
using v8::JustVoid;
2727
using v8::Local;
2828
using v8::Maybe;
2929
using v8::MaybeLocal;
@@ -338,7 +338,7 @@ void Check(const FunctionCallbackInfo<Value>& args) {
338338
// * Private type
339339
// * Cipher
340340
// * Passphrase
341-
Maybe<bool> DhKeyGenTraits::AdditionalConfig(
341+
Maybe<void> DhKeyGenTraits::AdditionalConfig(
342342
CryptoJobMode mode,
343343
const FunctionCallbackInfo<Value>& args,
344344
unsigned int* offset,
@@ -350,7 +350,7 @@ Maybe<bool> DhKeyGenTraits::AdditionalConfig(
350350
auto group = DHPointer::FindGroup(group_name.ToStringView());
351351
if (!group) {
352352
THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env);
353-
return Nothing<bool>();
353+
return Nothing<void>();
354354
}
355355

356356
static constexpr int kStandardizedGenerator = 2;
@@ -363,14 +363,14 @@ Maybe<bool> DhKeyGenTraits::AdditionalConfig(
363363
int size = args[*offset].As<Int32>()->Value();
364364
if (size < 0) {
365365
THROW_ERR_OUT_OF_RANGE(env, "Invalid prime size");
366-
return Nothing<bool>();
366+
return Nothing<void>();
367367
}
368368
params->params.prime = size;
369369
} else {
370370
ArrayBufferOrViewContents<unsigned char> input(args[*offset]);
371371
if (UNLIKELY(!input.CheckSizeInt32())) {
372372
THROW_ERR_OUT_OF_RANGE(env, "prime is too big");
373-
return Nothing<bool>();
373+
return Nothing<void>();
374374
}
375375
params->params.prime = BignumPointer(input.data(), input.size());
376376
}
@@ -380,7 +380,7 @@ Maybe<bool> DhKeyGenTraits::AdditionalConfig(
380380
*offset += 2;
381381
}
382382

383-
return Just(true);
383+
return JustVoid();
384384
}
385385

386386
EVPKeyCtxPointer DhKeyGenTraits::Setup(DhKeyPairGenConfig* params) {
@@ -424,11 +424,11 @@ EVPKeyCtxPointer DhKeyGenTraits::Setup(DhKeyPairGenConfig* params) {
424424
return ctx;
425425
}
426426

427-
Maybe<bool> DHKeyExportTraits::AdditionalConfig(
427+
Maybe<void> DHKeyExportTraits::AdditionalConfig(
428428
const FunctionCallbackInfo<Value>& args,
429429
unsigned int offset,
430430
DHKeyExportConfig* params) {
431-
return Just(true);
431+
return JustVoid();
432432
}
433433

434434
WebCryptoKeyExportStatus DHKeyExportTraits::DoExport(
@@ -487,7 +487,7 @@ void Stateless(const FunctionCallbackInfo<Value>& args) {
487487
}
488488
} // namespace
489489

490-
Maybe<bool> DHBitsTraits::AdditionalConfig(
490+
Maybe<void> DHBitsTraits::AdditionalConfig(
491491
CryptoJobMode mode,
492492
const FunctionCallbackInfo<Value>& args,
493493
unsigned int offset,
@@ -500,28 +500,25 @@ Maybe<bool> DHBitsTraits::AdditionalConfig(
500500
KeyObjectHandle* private_key;
501501
KeyObjectHandle* public_key;
502502

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

506506
if (private_key->Data().GetKeyType() != kKeyTypePrivate ||
507507
public_key->Data().GetKeyType() != kKeyTypePublic) {
508508
THROW_ERR_CRYPTO_INVALID_KEYTYPE(env);
509-
return Nothing<bool>();
509+
return Nothing<void>();
510510
}
511511

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

515-
return Just(true);
515+
return JustVoid();
516516
}
517517

518-
Maybe<bool> DHBitsTraits::EncodeOutput(
519-
Environment* env,
520-
const DHBitsConfig& params,
521-
ByteSource* out,
522-
v8::Local<v8::Value>* result) {
523-
*result = out->ToArrayBuffer(env);
524-
return Just(!result->IsEmpty());
518+
MaybeLocal<Value> DHBitsTraits::EncodeOutput(Environment* env,
519+
const DHBitsConfig& params,
520+
ByteSource* out) {
521+
return out->ToArrayBuffer(env);
525522
}
526523

527524
bool DHBitsTraits::DeriveBits(
@@ -533,11 +530,11 @@ bool DHBitsTraits::DeriveBits(
533530
return true;
534531
}
535532

536-
Maybe<bool> GetDhKeyDetail(Environment* env,
533+
Maybe<void> GetDhKeyDetail(Environment* env,
537534
const KeyObjectData& key,
538535
Local<Object> target) {
539536
CHECK_EQ(EVP_PKEY_id(key.GetAsymmetricKey().get()), EVP_PKEY_DH);
540-
return Just(true);
537+
return JustVoid();
541538
}
542539

543540
void DiffieHellman::Initialize(Environment* env, Local<Object> target) {

src/crypto/crypto_dh.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct DhKeyGenTraits final {
4949

5050
static EVPKeyCtxPointer Setup(DhKeyPairGenConfig* params);
5151

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

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

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

109-
static v8::Maybe<bool> EncodeOutput(
110-
Environment* env,
111-
const DHBitsConfig& params,
112-
ByteSource* out,
113-
v8::Local<v8::Value>* result);
109+
static v8::MaybeLocal<v8::Value> EncodeOutput(Environment* env,
110+
const DHBitsConfig& params,
111+
ByteSource* out);
114112
};
115113

116114
using DHBitsJob = DeriveBitsJob<DHBitsTraits>;
117115

118-
v8::Maybe<bool> GetDhKeyDetail(Environment* env,
116+
v8::Maybe<void> GetDhKeyDetail(Environment* env,
119117
const KeyObjectData& key,
120118
v8::Local<v8::Object> target);
121119

src/crypto/crypto_dsa.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace node {
2727

2828
using v8::FunctionCallbackInfo;
2929
using v8::Int32;
30-
using v8::Just;
30+
using v8::JustVoid;
3131
using v8::Local;
3232
using v8::Maybe;
3333
using v8::Nothing;
@@ -78,7 +78,7 @@ EVPKeyCtxPointer DsaKeyGenTraits::Setup(DsaKeyPairGenConfig* params) {
7878
// 7. Private Type
7979
// 8. Cipher
8080
// 9. Passphrase
81-
Maybe<bool> DsaKeyGenTraits::AdditionalConfig(
81+
Maybe<void> DsaKeyGenTraits::AdditionalConfig(
8282
CryptoJobMode mode,
8383
const FunctionCallbackInfo<Value>& args,
8484
unsigned int* offset,
@@ -92,14 +92,14 @@ Maybe<bool> DsaKeyGenTraits::AdditionalConfig(
9292

9393
*offset += 2;
9494

95-
return Just(true);
95+
return JustVoid();
9696
}
9797

98-
Maybe<bool> DSAKeyExportTraits::AdditionalConfig(
98+
Maybe<void> DSAKeyExportTraits::AdditionalConfig(
9999
const FunctionCallbackInfo<Value>& args,
100100
unsigned int offset,
101101
DSAKeyExportConfig* params) {
102-
return Just(true);
102+
return JustVoid();
103103
}
104104

105105
WebCryptoKeyExportStatus DSAKeyExportTraits::DoExport(
@@ -126,7 +126,7 @@ WebCryptoKeyExportStatus DSAKeyExportTraits::DoExport(
126126
}
127127
}
128128

129-
Maybe<bool> GetDsaKeyDetail(Environment* env,
129+
Maybe<void> GetDsaKeyDetail(Environment* env,
130130
const KeyObjectData& key,
131131
Local<Object> target) {
132132
const BIGNUM* p; // Modulus length
@@ -157,10 +157,10 @@ Maybe<bool> GetDsaKeyDetail(Environment* env,
157157
env->divisor_length_string(),
158158
Number::New(env->isolate(), static_cast<double>(divisor_length)))
159159
.IsNothing()) {
160-
return Nothing<bool>();
160+
return Nothing<void>();
161161
}
162162

163-
return Just(true);
163+
return JustVoid();
164164
}
165165

166166
namespace DSAAlg {

src/crypto/crypto_dsa.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct DsaKeyGenTraits final {
2828

2929
static EVPKeyCtxPointer Setup(DsaKeyPairGenConfig* params);
3030

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

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

6161
using DSAKeyExportJob = KeyExportJob<DSAKeyExportTraits>;
6262

63-
v8::Maybe<bool> GetDsaKeyDetail(Environment* env,
63+
v8::Maybe<void> GetDsaKeyDetail(Environment* env,
6464
const KeyObjectData& key,
6565
v8::Local<v8::Object> target);
6666

0 commit comments

Comments
 (0)