Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 88ccb44

Browse filesBrowse files
addaleaxcodebytere
authored andcommitted
src: move BaseObject subclass dtors/ctors out of node_crypto.h
Originally landed in the QUIC repo 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. Original review metadata: ``` PR-URL: nodejs/quic#220 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> ``` PR-URL: #31872 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 15cc9b0 commit 88ccb44
Copy full SHA for 88ccb44

File tree

Expand file treeCollapse file tree

2 files changed

+97
-80
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+97
-80
lines changed
Open diff view settings
Collapse file

‎src/node_crypto.cc‎

Copy file name to clipboardExpand all lines: src/node_crypto.cc
+79Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,24 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
531531
env->set_secure_context_constructor_template(t);
532532
}
533533

534+
SecureContext::SecureContext(Environment* env, v8::Local<v8::Object> wrap)
535+
: BaseObject(env, wrap) {
536+
MakeWeak();
537+
env->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize);
538+
}
539+
540+
inline void SecureContext::Reset() {
541+
if (ctx_ != nullptr) {
542+
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
543+
}
544+
ctx_.reset();
545+
cert_.reset();
546+
issuer_.reset();
547+
}
548+
549+
SecureContext::~SecureContext() {
550+
Reset();
551+
}
534552

535553
void SecureContext::New(const FunctionCallbackInfo<Value>& args) {
536554
Environment* env = Environment::GetCurrent(args);
@@ -3854,6 +3872,15 @@ KeyType KeyObject::GetKeyType() const {
38543872
return this->key_type_;
38553873
}
38563874

3875+
KeyObject::KeyObject(Environment* env,
3876+
v8::Local<v8::Object> wrap,
3877+
KeyType key_type)
3878+
: BaseObject(env, wrap),
3879+
key_type_(key_type),
3880+
symmetric_key_(nullptr, nullptr) {
3881+
MakeWeak();
3882+
}
3883+
38573884
void KeyObject::Init(const FunctionCallbackInfo<Value>& args) {
38583885
KeyObject* key;
38593886
ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder());
@@ -3998,6 +4025,17 @@ MaybeLocal<Value> KeyObject::ExportPrivateKey(
39984025
return WritePrivateKey(env(), asymmetric_key_.get(), config);
39994026
}
40004027

4028+
CipherBase::CipherBase(Environment* env,
4029+
v8::Local<v8::Object> wrap,
4030+
CipherKind kind)
4031+
: BaseObject(env, wrap),
4032+
ctx_(nullptr),
4033+
kind_(kind),
4034+
auth_tag_state_(kAuthTagUnknown),
4035+
auth_tag_len_(kNoAuthTagLength),
4036+
pending_auth_failed_(false) {
4037+
MakeWeak();
4038+
}
40014039

40024040
void CipherBase::Initialize(Environment* env, Local<Object> target) {
40034041
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
@@ -4620,6 +4658,11 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
46204658
args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
46214659
}
46224660

4661+
Hmac::Hmac(Environment* env, v8::Local<v8::Object> wrap)
4662+
: BaseObject(env, wrap),
4663+
ctx_(nullptr) {
4664+
MakeWeak();
4665+
}
46234666

46244667
void Hmac::Initialize(Environment* env, Local<Object> target) {
46254668
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
@@ -4739,6 +4782,13 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
47394782
args.GetReturnValue().Set(rc.ToLocalChecked());
47404783
}
47414784

4785+
Hash::Hash(Environment* env, v8::Local<v8::Object> wrap)
4786+
: BaseObject(env, wrap),
4787+
mdctx_(nullptr),
4788+
has_md_(false),
4789+
md_value_(nullptr) {
4790+
MakeWeak();
4791+
}
47424792

47434793
void Hash::Initialize(Environment* env, Local<Object> target) {
47444794
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
@@ -4753,6 +4803,10 @@ void Hash::Initialize(Environment* env, Local<Object> target) {
47534803
t->GetFunction(env->context()).ToLocalChecked()).Check();
47544804
}
47554805

4806+
Hash::~Hash() {
4807+
if (md_value_ != nullptr)
4808+
OPENSSL_clear_free(md_value_, md_len_);
4809+
}
47564810

47574811
void Hash::New(const FunctionCallbackInfo<Value>& args) {
47584812
Environment* env = Environment::GetCurrent(args);
@@ -4977,6 +5031,10 @@ void CheckThrow(Environment* env, SignBase::Error error) {
49775031
}
49785032
}
49795033

5034+
SignBase::SignBase(Environment* env, v8::Local<v8::Object> wrap)
5035+
: BaseObject(env, wrap) {
5036+
}
5037+
49805038
void SignBase::CheckThrow(SignBase::Error error) {
49815039
node::crypto::CheckThrow(env(), error);
49825040
}
@@ -5000,6 +5058,9 @@ static bool ApplyRSAOptions(const ManagedEVPPKey& pkey,
50005058
}
50015059

50025060

5061+
Sign::Sign(Environment* env, v8::Local<v8::Object> wrap) : SignBase(env, wrap) {
5062+
MakeWeak();
5063+
}
50035064

50045065
void Sign::Initialize(Environment* env, Local<Object> target) {
50055066
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
@@ -5320,6 +5381,11 @@ void SignOneShot(const FunctionCallbackInfo<Value>& args) {
53205381
args.GetReturnValue().Set(signature.ToBuffer().ToLocalChecked());
53215382
}
53225383

5384+
Verify::Verify(Environment* env, v8::Local<v8::Object> wrap) :
5385+
SignBase(env, wrap) {
5386+
MakeWeak();
5387+
}
5388+
53235389
void Verify::Initialize(Environment* env, Local<Object> target) {
53245390
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
53255391

@@ -5623,6 +5689,10 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
56235689
args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
56245690
}
56255691

5692+
DiffieHellman::DiffieHellman(Environment* env, v8::Local<v8::Object> wrap)
5693+
: BaseObject(env, wrap), verifyError_(0) {
5694+
MakeWeak();
5695+
}
56265696

56275697
void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
56285698
auto make = [&] (Local<String> name, FunctionCallback callback) {
@@ -5992,6 +6062,15 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
59926062
t->GetFunction(env->context()).ToLocalChecked()).Check();
59936063
}
59946064

6065+
ECDH::ECDH(Environment* env, v8::Local<v8::Object> wrap, ECKeyPointer&& key)
6066+
: BaseObject(env, wrap),
6067+
key_(std::move(key)),
6068+
group_(EC_KEY_get0_group(key_.get())) {
6069+
MakeWeak();
6070+
CHECK_NOT_NULL(group_);
6071+
}
6072+
6073+
ECDH::~ECDH() {}
59956074

59966075
void ECDH::New(const FunctionCallbackInfo<Value>& args) {
59976076
Environment* env = Environment::GetCurrent(args);
Collapse file

‎src/node_crypto.h‎

Copy file name to clipboardExpand all lines: src/node_crypto.h
+18-80Lines changed: 18 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,9 @@ extern void UseExtraCaCerts(const std::string& file);
8484

8585
void InitCryptoOnce();
8686

87-
class SecureContext : public BaseObject {
87+
class SecureContext final : public BaseObject {
8888
public:
89-
~SecureContext() override {
90-
Reset();
91-
}
89+
~SecureContext() override;
9290

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

@@ -177,20 +175,8 @@ class SecureContext : public BaseObject {
177175
HMAC_CTX* hctx,
178176
int enc);
179177

180-
SecureContext(Environment* env, v8::Local<v8::Object> wrap)
181-
: BaseObject(env, wrap) {
182-
MakeWeak();
183-
env->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize);
184-
}
185-
186-
inline void Reset() {
187-
if (ctx_ != nullptr) {
188-
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
189-
}
190-
ctx_.reset();
191-
cert_.reset();
192-
issuer_.reset();
193-
}
178+
SecureContext(Environment* env, v8::Local<v8::Object> wrap);
179+
void Reset();
194180
};
195181

196182
// SSLWrap implicitly depends on the inheriting class' handle having an
@@ -463,14 +449,7 @@ class KeyObject : public BaseObject {
463449
v8::MaybeLocal<v8::Value> ExportPrivateKey(
464450
const PrivateKeyEncodingConfig& config) const;
465451

466-
KeyObject(Environment* env,
467-
v8::Local<v8::Object> wrap,
468-
KeyType key_type)
469-
: BaseObject(env, wrap),
470-
key_type_(key_type),
471-
symmetric_key_(nullptr, nullptr) {
472-
MakeWeak();
473-
}
452+
KeyObject(Environment* env, v8::Local<v8::Object> wrap, KeyType key_type);
474453

475454
private:
476455
const KeyType key_type_;
@@ -544,17 +523,7 @@ class CipherBase : public BaseObject {
544523
static void SetAuthTag(const v8::FunctionCallbackInfo<v8::Value>& args);
545524
static void SetAAD(const v8::FunctionCallbackInfo<v8::Value>& args);
546525

547-
CipherBase(Environment* env,
548-
v8::Local<v8::Object> wrap,
549-
CipherKind kind)
550-
: BaseObject(env, wrap),
551-
ctx_(nullptr),
552-
kind_(kind),
553-
auth_tag_state_(kAuthTagUnknown),
554-
auth_tag_len_(kNoAuthTagLength),
555-
pending_auth_failed_(false) {
556-
MakeWeak();
557-
}
526+
CipherBase(Environment* env, v8::Local<v8::Object> wrap, CipherKind kind);
558527

559528
private:
560529
DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free> ctx_;
@@ -584,18 +553,16 @@ class Hmac : public BaseObject {
584553
static void HmacUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
585554
static void HmacDigest(const v8::FunctionCallbackInfo<v8::Value>& args);
586555

587-
Hmac(Environment* env, v8::Local<v8::Object> wrap)
588-
: BaseObject(env, wrap),
589-
ctx_(nullptr) {
590-
MakeWeak();
591-
}
556+
Hmac(Environment* env, v8::Local<v8::Object> wrap);
592557

593558
private:
594559
DeleteFnPtr<HMAC_CTX, HMAC_CTX_free> ctx_;
595560
};
596561

597-
class Hash : public BaseObject {
562+
class Hash final : public BaseObject {
598563
public:
564+
~Hash() override;
565+
599566
static void Initialize(Environment* env, v8::Local<v8::Object> target);
600567

601568
// TODO(joyeecheung): track the memory used by OpenSSL types
@@ -611,18 +578,7 @@ class Hash : public BaseObject {
611578
static void HashUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
612579
static void HashDigest(const v8::FunctionCallbackInfo<v8::Value>& args);
613580

614-
Hash(Environment* env, v8::Local<v8::Object> wrap)
615-
: BaseObject(env, wrap),
616-
mdctx_(nullptr),
617-
has_md_(false),
618-
md_value_(nullptr) {
619-
MakeWeak();
620-
}
621-
622-
~Hash() override {
623-
if (md_value_ != nullptr)
624-
OPENSSL_clear_free(md_value_, md_len_);
625-
}
581+
Hash(Environment* env, v8::Local<v8::Object> wrap);
626582

627583
private:
628584
EVPMDPointer mdctx_;
@@ -644,9 +600,7 @@ class SignBase : public BaseObject {
644600
kSignMalformedSignature
645601
} Error;
646602

647-
SignBase(Environment* env, v8::Local<v8::Object> wrap)
648-
: BaseObject(env, wrap) {
649-
}
603+
SignBase(Environment* env, v8::Local<v8::Object> wrap);
650604

651605
Error Init(const char* sign_type);
652606
Error Update(const char* data, int len);
@@ -692,9 +646,7 @@ class Sign : public SignBase {
692646
static void SignUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
693647
static void SignFinal(const v8::FunctionCallbackInfo<v8::Value>& args);
694648

695-
Sign(Environment* env, v8::Local<v8::Object> wrap) : SignBase(env, wrap) {
696-
MakeWeak();
697-
}
649+
Sign(Environment* env, v8::Local<v8::Object> wrap);
698650
};
699651

700652
class Verify : public SignBase {
@@ -713,9 +665,7 @@ class Verify : public SignBase {
713665
static void VerifyUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
714666
static void VerifyFinal(const v8::FunctionCallbackInfo<v8::Value>& args);
715667

716-
Verify(Environment* env, v8::Local<v8::Object> wrap) : SignBase(env, wrap) {
717-
MakeWeak();
718-
}
668+
Verify(Environment* env, v8::Local<v8::Object> wrap);
719669
};
720670

721671
class PublicKeyCipher {
@@ -772,11 +722,7 @@ class DiffieHellman : public BaseObject {
772722
static void VerifyErrorGetter(
773723
const v8::FunctionCallbackInfo<v8::Value>& args);
774724

775-
DiffieHellman(Environment* env, v8::Local<v8::Object> wrap)
776-
: BaseObject(env, wrap),
777-
verifyError_(0) {
778-
MakeWeak();
779-
}
725+
DiffieHellman(Environment* env, v8::Local<v8::Object> wrap);
780726

781727
// TODO(joyeecheung): track the memory used by OpenSSL types
782728
SET_NO_MEMORY_INFO()
@@ -795,11 +741,9 @@ class DiffieHellman : public BaseObject {
795741
DHPointer dh_;
796742
};
797743

798-
class ECDH : public BaseObject {
744+
class ECDH final : public BaseObject {
799745
public:
800-
~ECDH() override {
801-
group_ = nullptr;
802-
}
746+
~ECDH() override;
803747

804748
static void Initialize(Environment* env, v8::Local<v8::Object> target);
805749
static ECPointPointer BufferToPoint(Environment* env,
@@ -812,13 +756,7 @@ class ECDH : public BaseObject {
812756
SET_SELF_SIZE(ECDH)
813757

814758
protected:
815-
ECDH(Environment* env, v8::Local<v8::Object> wrap, ECKeyPointer&& key)
816-
: BaseObject(env, wrap),
817-
key_(std::move(key)),
818-
group_(EC_KEY_get0_group(key_.get())) {
819-
MakeWeak();
820-
CHECK_NOT_NULL(group_);
821-
}
759+
ECDH(Environment* env, v8::Local<v8::Object> wrap, ECKeyPointer&& key);
822760

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.