From 9cb55b7eefdeaf5e2819a8281393d50b14240cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sat, 13 Oct 2018 23:08:24 +0200 Subject: [PATCH 1/2] crypto: simplify internal state handling Uninitialized instances are not exposed to users, so this condition should always be true. --- src/node_crypto.cc | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 627c14360e18fd..9397e71db54ab8 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -4102,10 +4102,7 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo& args) { DiffieHellman* diffieHellman; ASSIGN_OR_RETURN_UNWRAP(&diffieHellman, args.Holder()); - - if (!diffieHellman->initialised_) { - return ThrowCryptoError(env, ERR_get_error(), "Not initialized"); - } + CHECK(diffieHellman->initialised_); if (!DH_generate_key(diffieHellman->dh_.get())) { return ThrowCryptoError(env, ERR_get_error(), "Key generation failed"); @@ -4127,7 +4124,7 @@ void DiffieHellman::GetField(const FunctionCallbackInfo& args, DiffieHellman* dh; ASSIGN_OR_RETURN_UNWRAP(&dh, args.Holder()); - if (!dh->initialised_) return env->ThrowError("Not initialized"); + CHECK(dh->initialised_); const BIGNUM* num = get_field(dh->dh_.get()); if (num == nullptr) return env->ThrowError(err_if_null); @@ -4179,10 +4176,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo& args) { DiffieHellman* diffieHellman; ASSIGN_OR_RETURN_UNWRAP(&diffieHellman, args.Holder()); - - if (!diffieHellman->initialised_) { - return ThrowCryptoError(env, ERR_get_error(), "Not initialized"); - } + CHECK(diffieHellman->initialised_); ClearErrorOnReturn clear_error_on_return; @@ -4250,7 +4244,7 @@ void DiffieHellman::SetKey(const v8::FunctionCallbackInfo& args, DiffieHellman* dh; ASSIGN_OR_RETURN_UNWRAP(&dh, args.Holder()); - if (!dh->initialised_) return env->ThrowError("Not initialized"); + CHECK(dh->initialised_); char errmsg[64]; @@ -4296,10 +4290,7 @@ void DiffieHellman::VerifyErrorGetter(const FunctionCallbackInfo& args) { DiffieHellman* diffieHellman; ASSIGN_OR_RETURN_UNWRAP(&diffieHellman, args.Holder()); - - if (!diffieHellman->initialised_) - return ThrowCryptoError(diffieHellman->env(), ERR_get_error(), - "Not initialized"); + CHECK(dh->initialised_); args.GetReturnValue().Set(diffieHellman->verifyError_); } From 72d65f74e943840f342483a87da5228cb47fe942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sat, 13 Oct 2018 23:19:29 +0200 Subject: [PATCH 2/2] fixup! crypto: simplify internal state handling --- src/node_crypto.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 9397e71db54ab8..1c37e8aadfdaad 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -4290,7 +4290,7 @@ void DiffieHellman::VerifyErrorGetter(const FunctionCallbackInfo& args) { DiffieHellman* diffieHellman; ASSIGN_OR_RETURN_UNWRAP(&diffieHellman, args.Holder()); - CHECK(dh->initialised_); + CHECK(diffieHellman->initialised_); args.GetReturnValue().Set(diffieHellman->verifyError_); }