Skip to content

Commit bf882fb

Browse files
AdamMajerMylesBorins
authored andcommitted
crypto: Use reference count to manage cert_store
Setting reference count at the time of setting cert_store instead of trying to manage it by modifying internal states in destructor. PR-URL: #9409 Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Shigeki Ohtsu <[email protected]>
1 parent d3a7fb8 commit bf882fb

File tree

2 files changed

+2
-7
lines changed

2 files changed

+2
-7
lines changed

src/node_crypto.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,8 @@ void SecureContext::AddRootCerts(const FunctionCallbackInfo<Value>& args) {
785785
}
786786

787787
sc->ca_store_ = root_cert_store;
788+
// Increment reference count so global store is not deleted along with CTX.
789+
CRYPTO_add(&root_cert_store->references, 1, CRYPTO_LOCK_X509_STORE);
788790
SSL_CTX_set_cert_store(sc->ctx_, sc->ca_store_);
789791
}
790792

src/node_crypto.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,6 @@ class SecureContext : public BaseObject {
140140
void FreeCTXMem() {
141141
if (ctx_) {
142142
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
143-
if (ctx_->cert_store == root_cert_store) {
144-
// SSL_CTX_free() will attempt to free the cert_store as well.
145-
// Since we want our root_cert_store to stay around forever
146-
// we just clear the field. Hopefully OpenSSL will not modify this
147-
// struct in future versions.
148-
ctx_->cert_store = nullptr;
149-
}
150143
SSL_CTX_free(ctx_);
151144
if (cert_ != nullptr)
152145
X509_free(cert_);

0 commit comments

Comments
 (0)