Skip to content

Commit

Permalink
src: move BaseObject subclass dtors/ctors out of node_crypto.h
Browse files Browse the repository at this point in the history
Move constructor and destructors for subclasses of `BaseObject`
from node_crypto.h to node_crypto.cc. This removes the need to
include base_object-inl.h when using node_crypto.h in some cases.

PR-URL: nodejs#220
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax authored and jasnell committed Feb 3, 2020
1 parent e077f1a commit 7c5d69a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 81 deletions.
80 changes: 79 additions & 1 deletion src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "node_errors.h"
#include "node_mutex.h"
#include "node_process.h"
#include "base_object-inl.h"
#include "tls_wrap.h" // TLSWrap

#include "async_wrap-inl.h"
Expand Down Expand Up @@ -526,6 +525,24 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
env->set_secure_context_constructor_template(t);
}

SecureContext::SecureContext(Environment* env, v8::Local<v8::Object> wrap)
: BaseObject(env, wrap) {
MakeWeak();
env->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize);
}

inline void SecureContext::Reset() {
if (ctx_ != nullptr) {
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
}
ctx_.reset();
cert_.reset();
issuer_.reset();
}

SecureContext::~SecureContext() {
Reset();
}

void SecureContext::New(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand Down Expand Up @@ -3707,6 +3724,15 @@ KeyType KeyObject::GetKeyType() const {
return this->key_type_;
}

KeyObject::KeyObject(Environment* env,
v8::Local<v8::Object> wrap,
KeyType key_type)
: BaseObject(env, wrap),
key_type_(key_type),
symmetric_key_(nullptr, nullptr) {
MakeWeak();
}

void KeyObject::Init(const FunctionCallbackInfo<Value>& args) {
KeyObject* key;
ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder());
Expand Down Expand Up @@ -3851,6 +3877,17 @@ MaybeLocal<Value> KeyObject::ExportPrivateKey(
return WritePrivateKey(env(), asymmetric_key_.get(), config);
}

CipherBase::CipherBase(Environment* env,
v8::Local<v8::Object> wrap,
CipherKind kind)
: BaseObject(env, wrap),
ctx_(nullptr),
kind_(kind),
auth_tag_state_(kAuthTagUnknown),
auth_tag_len_(kNoAuthTagLength),
pending_auth_failed_(false) {
MakeWeak();
}

void CipherBase::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
Expand Down Expand Up @@ -4473,6 +4510,11 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
}

Hmac::Hmac(Environment* env, v8::Local<v8::Object> wrap)
: BaseObject(env, wrap),
ctx_(nullptr) {
MakeWeak();
}

void Hmac::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
Expand Down Expand Up @@ -4592,6 +4634,13 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(rc.ToLocalChecked());
}

Hash::Hash(Environment* env, v8::Local<v8::Object> wrap)
: BaseObject(env, wrap),
mdctx_(nullptr),
has_md_(false),
md_value_(nullptr) {
MakeWeak();
}

void Hash::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
Expand All @@ -4606,6 +4655,10 @@ void Hash::Initialize(Environment* env, Local<Object> target) {
t->GetFunction(env->context()).ToLocalChecked()).Check();
}

Hash::~Hash() {
if (md_value_ != nullptr)
OPENSSL_clear_free(md_value_, md_len_);
}

void Hash::New(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand Down Expand Up @@ -4830,6 +4883,10 @@ void CheckThrow(Environment* env, SignBase::Error error) {
}
}

SignBase::SignBase(Environment* env, v8::Local<v8::Object> wrap)
: BaseObject(env, wrap) {
}

void SignBase::CheckThrow(SignBase::Error error) {
node::crypto::CheckThrow(env(), error);
}
Expand All @@ -4853,6 +4910,9 @@ static bool ApplyRSAOptions(const ManagedEVPPKey& pkey,
}


Sign::Sign(Environment* env, v8::Local<v8::Object> wrap) : SignBase(env, wrap) {
MakeWeak();
}

void Sign::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
Expand Down Expand Up @@ -5173,6 +5233,11 @@ void SignOneShot(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(signature.ToBuffer().ToLocalChecked());
}

Verify::Verify(Environment* env, v8::Local<v8::Object> wrap) :
SignBase(env, wrap) {
MakeWeak();
}

void Verify::Initialize(Environment* env, Local<Object> target) {
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);

Expand Down Expand Up @@ -5480,6 +5545,10 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
}

DiffieHellman::DiffieHellman(Environment* env, v8::Local<v8::Object> wrap)
: BaseObject(env, wrap), verifyError_(0) {
MakeWeak();
}

void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
auto make = [&] (Local<String> name, FunctionCallback callback) {
Expand Down Expand Up @@ -5849,6 +5918,15 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
t->GetFunction(env->context()).ToLocalChecked()).Check();
}

ECDH::ECDH(Environment* env, v8::Local<v8::Object> wrap, ECKeyPointer&& key)
: BaseObject(env, wrap),
key_(std::move(key)),
group_(EC_KEY_get0_group(key_.get())) {
MakeWeak();
CHECK_NOT_NULL(group_);
}

ECDH::~ECDH() {}

void ECDH::New(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand Down
98 changes: 18 additions & 80 deletions src/node_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,9 @@ extern void UseExtraCaCerts(const std::string& file);

void InitCryptoOnce();

class SecureContext : public BaseObject {
class SecureContext final : public BaseObject {
public:
~SecureContext() override {
Reset();
}
~SecureContext() override;

static void Initialize(Environment* env, v8::Local<v8::Object> target);

Expand Down Expand Up @@ -185,20 +183,8 @@ class SecureContext : public BaseObject {
HMAC_CTX* hctx,
int enc);

SecureContext(Environment* env, v8::Local<v8::Object> wrap)
: BaseObject(env, wrap) {
MakeWeak();
env->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize);
}

inline void Reset() {
if (ctx_ != nullptr) {
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
}
ctx_.reset();
cert_.reset();
issuer_.reset();
}
SecureContext(Environment* env, v8::Local<v8::Object> wrap);
void Reset();
};

#define SSLWRAP_TYPES(V) \
Expand Down Expand Up @@ -472,14 +458,7 @@ class KeyObject : public BaseObject {
v8::MaybeLocal<v8::Value> ExportPrivateKey(
const PrivateKeyEncodingConfig& config) const;

KeyObject(Environment* env,
v8::Local<v8::Object> wrap,
KeyType key_type)
: BaseObject(env, wrap),
key_type_(key_type),
symmetric_key_(nullptr, nullptr) {
MakeWeak();
}
KeyObject(Environment* env, v8::Local<v8::Object> wrap, KeyType key_type);

private:
const KeyType key_type_;
Expand Down Expand Up @@ -553,17 +532,7 @@ class CipherBase : public BaseObject {
static void SetAuthTag(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetAAD(const v8::FunctionCallbackInfo<v8::Value>& args);

CipherBase(Environment* env,
v8::Local<v8::Object> wrap,
CipherKind kind)
: BaseObject(env, wrap),
ctx_(nullptr),
kind_(kind),
auth_tag_state_(kAuthTagUnknown),
auth_tag_len_(kNoAuthTagLength),
pending_auth_failed_(false) {
MakeWeak();
}
CipherBase(Environment* env, v8::Local<v8::Object> wrap, CipherKind kind);

private:
DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free> ctx_;
Expand Down Expand Up @@ -593,18 +562,16 @@ class Hmac : public BaseObject {
static void HmacUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
static void HmacDigest(const v8::FunctionCallbackInfo<v8::Value>& args);

Hmac(Environment* env, v8::Local<v8::Object> wrap)
: BaseObject(env, wrap),
ctx_(nullptr) {
MakeWeak();
}
Hmac(Environment* env, v8::Local<v8::Object> wrap);

private:
DeleteFnPtr<HMAC_CTX, HMAC_CTX_free> ctx_;
};

class Hash : public BaseObject {
class Hash final : public BaseObject {
public:
~Hash() override;

static void Initialize(Environment* env, v8::Local<v8::Object> target);

// TODO(joyeecheung): track the memory used by OpenSSL types
Expand All @@ -620,18 +587,7 @@ class Hash : public BaseObject {
static void HashUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
static void HashDigest(const v8::FunctionCallbackInfo<v8::Value>& args);

Hash(Environment* env, v8::Local<v8::Object> wrap)
: BaseObject(env, wrap),
mdctx_(nullptr),
has_md_(false),
md_value_(nullptr) {
MakeWeak();
}

~Hash() override {
if (md_value_ != nullptr)
OPENSSL_clear_free(md_value_, md_len_);
}
Hash(Environment* env, v8::Local<v8::Object> wrap);

private:
EVPMDPointer mdctx_;
Expand All @@ -653,9 +609,7 @@ class SignBase : public BaseObject {
kSignMalformedSignature
} Error;

SignBase(Environment* env, v8::Local<v8::Object> wrap)
: BaseObject(env, wrap) {
}
SignBase(Environment* env, v8::Local<v8::Object> wrap);

Error Init(const char* sign_type);
Error Update(const char* data, int len);
Expand Down Expand Up @@ -701,9 +655,7 @@ class Sign : public SignBase {
static void SignUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SignFinal(const v8::FunctionCallbackInfo<v8::Value>& args);

Sign(Environment* env, v8::Local<v8::Object> wrap) : SignBase(env, wrap) {
MakeWeak();
}
Sign(Environment* env, v8::Local<v8::Object> wrap);
};

class Verify : public SignBase {
Expand All @@ -723,9 +675,7 @@ class Verify : public SignBase {
static void VerifyUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
static void VerifyFinal(const v8::FunctionCallbackInfo<v8::Value>& args);

Verify(Environment* env, v8::Local<v8::Object> wrap) : SignBase(env, wrap) {
MakeWeak();
}
Verify(Environment* env, v8::Local<v8::Object> wrap);
};

class PublicKeyCipher {
Expand Down Expand Up @@ -782,11 +732,7 @@ class DiffieHellman : public BaseObject {
static void VerifyErrorGetter(
const v8::FunctionCallbackInfo<v8::Value>& args);

DiffieHellman(Environment* env, v8::Local<v8::Object> wrap)
: BaseObject(env, wrap),
verifyError_(0) {
MakeWeak();
}
DiffieHellman(Environment* env, v8::Local<v8::Object> wrap);

// TODO(joyeecheung): track the memory used by OpenSSL types
SET_NO_MEMORY_INFO()
Expand All @@ -805,11 +751,9 @@ class DiffieHellman : public BaseObject {
DHPointer dh_;
};

class ECDH : public BaseObject {
class ECDH final : public BaseObject {
public:
~ECDH() override {
group_ = nullptr;
}
~ECDH() override;

static void Initialize(Environment* env, v8::Local<v8::Object> target);
static ECPointPointer BufferToPoint(Environment* env,
Expand All @@ -822,13 +766,7 @@ class ECDH : public BaseObject {
SET_SELF_SIZE(ECDH)

protected:
ECDH(Environment* env, v8::Local<v8::Object> wrap, ECKeyPointer&& key)
: BaseObject(env, wrap),
key_(std::move(key)),
group_(EC_KEY_get0_group(key_.get())) {
MakeWeak();
CHECK_NOT_NULL(group_);
}
ECDH(Environment* env, v8::Local<v8::Object> wrap, ECKeyPointer&& key);

static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GenerateKeys(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down

0 comments on commit 7c5d69a

Please sign in to comment.