@@ -47,10 +47,15 @@ static const char* const root_certs[] = {
47
47
48
48
static const char system_cert_path[] = NODE_OPENSSL_SYSTEM_CERT_PATH;
49
49
50
- static X509_STORE* root_cert_store;
51
-
52
50
static bool extra_root_certs_loaded = false ;
53
51
52
+ inline X509_STORE* GetOrCreateRootCertStore () {
53
+ static X509_STORE* store;
54
+ static uv_once_t once = UV_ONCE_INIT;
55
+ uv_once (&once, [] { store = NewRootCertStore (); });
56
+ return store;
57
+ }
58
+
54
59
// Takes a string or buffer and loads it into a BIO.
55
60
// Caller responsible for BIO_free_all-ing the returned object.
56
61
BIOPointer LoadBIO (Environment* env, Local<Value> v) {
@@ -701,7 +706,7 @@ void SecureContext::AddCACert(const FunctionCallbackInfo<Value>& args) {
701
706
X509_STORE* cert_store = SSL_CTX_get_cert_store (sc->ctx_ .get ());
702
707
while (X509Pointer x509 = X509Pointer (PEM_read_bio_X509_AUX (
703
708
bio.get (), nullptr , NoPasswordCallback, nullptr ))) {
704
- if (cert_store == root_cert_store ) {
709
+ if (cert_store == GetOrCreateRootCertStore () ) {
705
710
cert_store = NewRootCertStore ();
706
711
SSL_CTX_set_cert_store (sc->ctx_ .get (), cert_store);
707
712
}
@@ -731,7 +736,7 @@ void SecureContext::AddCRL(const FunctionCallbackInfo<Value>& args) {
731
736
return THROW_ERR_CRYPTO_OPERATION_FAILED (env, " Failed to parse CRL" );
732
737
733
738
X509_STORE* cert_store = SSL_CTX_get_cert_store (sc->ctx_ .get ());
734
- if (cert_store == root_cert_store ) {
739
+ if (cert_store == GetOrCreateRootCertStore () ) {
735
740
cert_store = NewRootCertStore ();
736
741
SSL_CTX_set_cert_store (sc->ctx_ .get (), cert_store);
737
742
}
@@ -745,14 +750,10 @@ void SecureContext::AddRootCerts(const FunctionCallbackInfo<Value>& args) {
745
750
SecureContext* sc;
746
751
ASSIGN_OR_RETURN_UNWRAP (&sc, args.Holder ());
747
752
ClearErrorOnReturn clear_error_on_return;
748
-
749
- if (root_cert_store == nullptr ) {
750
- root_cert_store = NewRootCertStore ();
751
- }
752
-
753
+ X509_STORE* store = GetOrCreateRootCertStore ();
753
754
// Increment reference count so global store is not deleted along with CTX.
754
- X509_STORE_up_ref (root_cert_store );
755
- SSL_CTX_set_cert_store (sc->ctx_ .get (), root_cert_store );
755
+ X509_STORE_up_ref (store );
756
+ SSL_CTX_set_cert_store (sc->ctx_ .get (), store );
756
757
}
757
758
758
759
void SecureContext::SetCipherSuites (const FunctionCallbackInfo<Value>& args) {
@@ -1025,7 +1026,7 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
1025
1026
for (int i = 0 ; i < sk_X509_num (extra_certs.get ()); i++) {
1026
1027
X509* ca = sk_X509_value (extra_certs.get (), i);
1027
1028
1028
- if (cert_store == root_cert_store ) {
1029
+ if (cert_store == GetOrCreateRootCertStore () ) {
1029
1030
cert_store = NewRootCertStore ();
1030
1031
SSL_CTX_set_cert_store (sc->ctx_ .get (), cert_store);
1031
1032
}
@@ -1328,24 +1329,17 @@ unsigned long AddCertsFromFile( // NOLINT(runtime/int)
1328
1329
1329
1330
// UseExtraCaCerts is called only once at the start of the Node.js process.
1330
1331
void UseExtraCaCerts (const std::string& file) {
1332
+ if (file.empty ()) return ;
1331
1333
ClearErrorOnReturn clear_error_on_return;
1332
-
1333
- if (root_cert_store == nullptr ) {
1334
- root_cert_store = NewRootCertStore ();
1335
-
1336
- if (!file.empty ()) {
1337
- unsigned long err = AddCertsFromFile ( // NOLINT(runtime/int)
1338
- root_cert_store,
1339
- file.c_str ());
1340
- if (err) {
1341
- fprintf (stderr,
1342
- " Warning: Ignoring extra certs from `%s`, load failed: %s\n " ,
1343
- file.c_str (),
1344
- ERR_error_string (err, nullptr ));
1345
- } else {
1346
- extra_root_certs_loaded = true ;
1347
- }
1348
- }
1334
+ X509_STORE* store = GetOrCreateRootCertStore ();
1335
+ if (auto err = AddCertsFromFile (store, file.c_str ())) {
1336
+ char buf[256 ];
1337
+ ERR_error_string_n (err, buf, sizeof (buf));
1338
+ fprintf (stderr,
1339
+ " Warning: Ignoring extra certs from `%s`, load failed: %s\n " ,
1340
+ file.c_str (), buf);
1341
+ } else {
1342
+ extra_root_certs_loaded = true ;
1349
1343
}
1350
1344
}
1351
1345
0 commit comments