Skip to content

Commit ba318d2

Browse files
committed
src: simplify error handling in crypto_common
1 parent f780e87 commit ba318d2

File tree

5 files changed

+17
-21
lines changed

5 files changed

+17
-21
lines changed

src/crypto/crypto_common.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,10 @@ MaybeLocal<Object> GetEphemeralKey(Environment* env, const SSLPointer& ssl) {
243243
MaybeLocal<Object> ECPointToBuffer(Environment* env,
244244
const EC_GROUP* group,
245245
const EC_POINT* point,
246-
point_conversion_form_t form,
247-
const char** error) {
246+
point_conversion_form_t form) {
248247
size_t len = EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr);
249248
if (len == 0) {
250-
if (error != nullptr) *error = "Failed to get public key length";
249+
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to get public key length");
251250
return MaybeLocal<Object>();
252251
}
253252

@@ -261,7 +260,7 @@ MaybeLocal<Object> ECPointToBuffer(Environment* env,
261260
bs->ByteLength(),
262261
nullptr);
263262
if (len == 0) {
264-
if (error != nullptr) *error = "Failed to get public key";
263+
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to get public key");
265264
return MaybeLocal<Object>();
266265
}
267266

src/crypto/crypto_common.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ v8::MaybeLocal<v8::Value> GetPeerCert(Environment* env,
3535
bool abbreviated = false,
3636
bool is_server = false);
3737

38-
v8::MaybeLocal<v8::Object> ECPointToBuffer(
39-
Environment* env,
40-
const EC_GROUP* group,
41-
const EC_POINT* point,
42-
point_conversion_form_t form,
43-
const char** error);
38+
v8::MaybeLocal<v8::Object> ECPointToBuffer(Environment* env,
39+
const EC_GROUP* group,
40+
const EC_POINT* point,
41+
point_conversion_form_t form);
4442

4543
} // namespace crypto
4644
} // namespace node

src/crypto/crypto_ec.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,10 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
235235
uint32_t val = args[0].As<Uint32>()->Value();
236236
point_conversion_form_t form = static_cast<point_conversion_form_t>(val);
237237

238-
const char* error;
239238
Local<Object> buf;
240-
if (!ECPointToBuffer(env, group, pub, form, &error).ToLocal(&buf))
241-
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, error);
242-
args.GetReturnValue().Set(buf);
239+
if (ECPointToBuffer(env, group, pub, form).ToLocal(&buf)) {
240+
args.GetReturnValue().Set(buf);
241+
}
243242
}
244243

245244
void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
@@ -397,11 +396,10 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {
397396
uint32_t val = args[2].As<Uint32>()->Value();
398397
point_conversion_form_t form = static_cast<point_conversion_form_t>(val);
399398

400-
const char* error;
401399
Local<Object> buf;
402-
if (!ECPointToBuffer(env, group, pub, form, &error).ToLocal(&buf))
403-
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, error);
404-
args.GetReturnValue().Set(buf);
400+
if (ECPointToBuffer(env, group, pub, form).ToLocal(&buf)) {
401+
args.GetReturnValue().Set(buf);
402+
}
405403
}
406404

407405
void ECDHBitsConfig::MemoryInfo(MemoryTracker* tracker) const {

src/crypto/crypto_util.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,14 @@ MaybeLocal<Value> CryptoErrorStore::ToException(
285285
// Use last element as the error message, everything else goes
286286
// into the .opensslErrorStack property on the exception object.
287287
const std::string& last_error_string = copy.errors_.back();
288-
Local<String> exception_string;
288+
Local<Value> exception_string;
289289
if (!ToV8Value(env->context(), last_error_string)
290290
.ToLocal(&exception_string)) {
291291
return MaybeLocal<Value>();
292292
}
293+
DCHECK(exception_string->IsString());
293294
copy.errors_.pop_back();
294-
return copy.ToException(env, exception_string);
295+
return copy.ToException(env, exception_string.As<v8::String>());
295296
}
296297

297298
Local<Value> exception_v = Exception::Error(exception_string);

src/crypto/crypto_x509.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ MaybeLocal<Value> GetECPubKey(Environment* env,
650650
if (pubkey == nullptr) [[unlikely]]
651651
return Undefined(env->isolate());
652652

653-
return ECPointToBuffer(env, group, pubkey, EC_KEY_get_conv_form(ec), nullptr)
653+
return ECPointToBuffer(env, group, pubkey, EC_KEY_get_conv_form(ec))
654654
.FromMaybe(Local<Object>());
655655
}
656656

0 commit comments

Comments
 (0)