@@ -84,6 +84,7 @@ static std::string extra_root_certs_file; // NOLINT(runtime/string)
8484static std::atomic<bool > has_cached_bundled_root_certs{false };
8585static std::atomic<bool > has_cached_system_root_certs{false };
8686static std::atomic<bool > has_cached_extra_root_certs{false };
87+ static std::atomic<bool > has_use_system_ca{false };
8788
8889// Used for sets of X509.
8990struct X509Less {
@@ -101,11 +102,11 @@ static thread_local X509_STORE* root_cert_store = nullptr;
101102// from this set.
102103static thread_local std::unique_ptr<X509Set> root_certs_from_users;
103104
104- X509_STORE* GetOrCreateRootCertStore () {
105+ X509_STORE* GetOrCreateRootCertStore (Environment* env ) {
105106 if (root_cert_store != nullptr ) {
106107 return root_cert_store;
107108 }
108- root_cert_store = NewRootCertStore ();
109+ root_cert_store = NewRootCertStore (env );
109110 return root_cert_store;
110111}
111112
@@ -873,7 +874,7 @@ static void LoadCACertificates(void* data) {
873874
874875 {
875876 Mutex::ScopedLock cli_lock (node::per_process::cli_options_mutex);
876- if (!per_process::cli_options-> use_system_ca ) {
877+ if (!has_use_system_ca. load () ) {
877878 return ;
878879 }
879880 }
@@ -917,6 +918,8 @@ void StartLoadingCertificatesOffThread(
917918 return ;
918919 }
919920 tried_cert_loading_off_thread.store (true );
921+ Environment* env = Environment::GetCurrent (args);
922+ has_use_system_ca.store (env != nullptr && env->options ()->use_system_ca );
920923 int r = uv_thread_create (&cert_loading_thread, LoadCACertificates, nullptr );
921924 cert_loading_thread_started.store (r == 0 );
922925 if (r != 0 ) {
@@ -947,13 +950,13 @@ void StartLoadingCertificatesOffThread(
947950// with all the other flags.
948951// 7. Certificates from --use-bundled-ca, --use-system-ca and
949952// NODE_EXTRA_CA_CERTS are cached after first load. Certificates
950- // from --use-system -ca are not cached and always reloaded from
953+ // from --use-openssl -ca are not cached and always reloaded from
951954// disk.
952955// 8. If users have reset the root cert store by calling
953956// tls.setDefaultCACertificates(), the store will be populated with
954957// the certificates provided by users.
955958// TODO(joyeecheung): maybe these rules need a bit of consolidation?
956- X509_STORE* NewRootCertStore () {
959+ X509_STORE* NewRootCertStore (Environment* env ) {
957960 X509_STORE* store = X509_STORE_new ();
958961 CHECK_NOT_NULL (store);
959962
@@ -975,14 +978,24 @@ X509_STORE* NewRootCertStore() {
975978 }
976979#endif
977980
978- Mutex::ScopedLock cli_lock (node::per_process::cli_options_mutex);
981+ bool use_system_ca = false ;
982+ {
983+ Mutex::ScopedLock cli_lock (node::per_process::cli_options_mutex);
984+ if (env != nullptr ) {
985+ use_system_ca = env->options ()->use_system_ca ;
986+ } else if (per_process::cli_options->per_isolate != nullptr &&
987+ per_process::cli_options->per_isolate ->per_env != nullptr ) {
988+ use_system_ca =
989+ per_process::cli_options->per_isolate ->per_env ->use_system_ca ;
990+ }
991+ }
979992 if (per_process::cli_options->ssl_openssl_cert_store ) {
980993 CHECK_EQ (1 , X509_STORE_set_default_paths (store));
981994 } else {
982995 for (X509* cert : GetBundledRootCertificates ()) {
983996 CHECK_EQ (1 , X509_STORE_add_cert (store, cert));
984997 }
985- if (per_process::cli_options-> use_system_ca ) {
998+ if (use_system_ca) {
986999 for (X509* cert : GetSystemStoreCACertificates ()) {
9871000 CHECK_EQ (1 , X509_STORE_add_cert (store, cert));
9881001 }
@@ -1189,7 +1202,7 @@ void ResetRootCertStore(const FunctionCallbackInfo<Value>& args) {
11891202
11901203 // TODO(joyeecheung): we can probably just reset it to nullptr
11911204 // and let the next call to NewRootCertStore() create a new one.
1192- root_cert_store = NewRootCertStore () ;
1205+ root_cert_store = nullptr ;
11931206}
11941207
11951208void GetSystemCACertificates (const FunctionCallbackInfo<Value>& args) {
@@ -1700,11 +1713,12 @@ void SecureContext::SetX509StoreFlag(unsigned long flags) {
17001713}
17011714
17021715X509_STORE* SecureContext::GetCertStoreOwnedByThisSecureContext () {
1716+ Environment* env = this ->env ();
17031717 if (own_cert_store_cache_ != nullptr ) return own_cert_store_cache_;
17041718
17051719 X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
1706- if (cert_store == GetOrCreateRootCertStore ()) {
1707- cert_store = NewRootCertStore ();
1720+ if (cert_store == GetOrCreateRootCertStore (env )) {
1721+ cert_store = NewRootCertStore (env );
17081722 SSL_CTX_set_cert_store (ctx_.get (), cert_store);
17091723 }
17101724
@@ -1777,7 +1791,8 @@ void SecureContext::AddCRL(const FunctionCallbackInfo<Value>& args) {
17771791
17781792void SecureContext::SetRootCerts () {
17791793 ClearErrorOnReturn clear_error_on_return;
1780- auto store = GetOrCreateRootCertStore ();
1794+ Environment* env = this ->env ();
1795+ auto store = GetOrCreateRootCertStore (env);
17811796
17821797 // Increment reference count so global store is not deleted along with CTX.
17831798 X509_STORE_up_ref (store);
0 commit comments