@@ -49,6 +49,10 @@ static const char system_cert_path[] = NODE_OPENSSL_SYSTEM_CERT_PATH;
49
49
50
50
static X509_STORE* root_cert_store;
51
51
52
+ static std::vector<X509Pointer> root_certs_vector;
53
+ static Mutex root_certs_vector_mutex;
54
+ static bool root_certs_loaded = false ;
55
+
52
56
static bool extra_root_certs_loaded = false ;
53
57
54
58
// Takes a string or buffer and loads it into a BIO.
@@ -191,25 +195,25 @@ int SSL_CTX_use_certificate_chain(SSL_CTX* ctx,
191
195
} // namespace
192
196
193
197
X509_STORE* NewRootCertStore () {
194
- static std::vector<X509*> root_certs_vector;
195
- static Mutex root_certs_vector_mutex;
196
198
Mutex::ScopedLock lock (root_certs_vector_mutex);
197
199
198
- if (root_certs_vector. empty () &&
200
+ if (!root_certs_loaded &&
199
201
per_process::cli_options->ssl_openssl_cert_store == false ) {
200
202
for (size_t i = 0 ; i < arraysize (root_certs); i++) {
201
- X509* x509 =
203
+ X509Pointer x509 = X509Pointer (
202
204
PEM_read_bio_X509 (NodeBIO::NewFixed (root_certs[i],
203
205
strlen (root_certs[i])).get (),
204
- nullptr , // no re-use of X509 structure
206
+ nullptr , // no re-use of X509 structure
205
207
NoPasswordCallback,
206
- nullptr ); // no callback data
208
+ nullptr )) ; // no callback data
207
209
208
210
// Parse errors from the built-in roots are fatal.
209
211
CHECK_NOT_NULL (x509);
210
212
211
- root_certs_vector.push_back (x509);
213
+ root_certs_vector.push_back (std::move ( x509) );
212
214
}
215
+
216
+ root_certs_loaded = true ;
213
217
}
214
218
215
219
X509_STORE* store = X509_STORE_new ();
@@ -223,10 +227,8 @@ X509_STORE* NewRootCertStore() {
223
227
if (per_process::cli_options->ssl_openssl_cert_store ) {
224
228
X509_STORE_set_default_paths (store);
225
229
} else {
226
- for (X509* cert : root_certs_vector) {
227
- X509_up_ref (cert);
228
- X509_STORE_add_cert (store, cert);
229
- }
230
+ for (X509Pointer& cert : root_certs_vector)
231
+ X509_STORE_add_cert (store, cert.get ());
230
232
}
231
233
232
234
return store;
@@ -1299,7 +1301,7 @@ void SecureContext::GetCertificate(const FunctionCallbackInfo<Value>& args) {
1299
1301
1300
1302
namespace {
1301
1303
unsigned long AddCertsFromFile ( // NOLINT(runtime/int)
1302
- X509_STORE* store ,
1304
+ std::vector<X509Pointer>& certs ,
1303
1305
const char * file) {
1304
1306
ERR_clear_error ();
1305
1307
MarkPopErrorOnReturn mark_pop_error_on_return;
@@ -1308,10 +1310,9 @@ unsigned long AddCertsFromFile( // NOLINT(runtime/int)
1308
1310
if (!bio)
1309
1311
return ERR_get_error ();
1310
1312
1311
- while (X509* x509 =
1312
- PEM_read_bio_X509 (bio.get (), nullptr , NoPasswordCallback, nullptr )) {
1313
- X509_STORE_add_cert (store, x509);
1314
- X509_free (x509);
1313
+ while (X509Pointer x509 = X509Pointer (
1314
+ PEM_read_bio_X509 (bio.get (), nullptr , NoPasswordCallback, nullptr ))) {
1315
+ certs.push_back (std::move (x509));
1315
1316
}
1316
1317
1317
1318
unsigned long err = ERR_peek_error (); // NOLINT(runtime/int)
@@ -1329,21 +1330,17 @@ unsigned long AddCertsFromFile( // NOLINT(runtime/int)
1329
1330
void UseExtraCaCerts (const std::string& file) {
1330
1331
ClearErrorOnReturn clear_error_on_return;
1331
1332
1332
- if (root_cert_store == nullptr ) {
1333
- root_cert_store = NewRootCertStore ();
1334
-
1335
- if (!file.empty ()) {
1336
- unsigned long err = AddCertsFromFile ( // NOLINT(runtime/int)
1337
- root_cert_store,
1338
- file.c_str ());
1339
- if (err) {
1340
- fprintf (stderr,
1341
- " Warning: Ignoring extra certs from `%s`, load failed: %s\n " ,
1342
- file.c_str (),
1343
- ERR_error_string (err, nullptr ));
1344
- } else {
1345
- extra_root_certs_loaded = true ;
1346
- }
1333
+ if (!file.empty ()) {
1334
+ unsigned long err = AddCertsFromFile ( // NOLINT(runtime/int)
1335
+ root_certs_vector,
1336
+ file.c_str ());
1337
+ if (err) {
1338
+ fprintf (stderr,
1339
+ " Warning: Ignoring extra certs from `%s`, load failed: %s\n " ,
1340
+ file.c_str (),
1341
+ ERR_error_string (err, nullptr ));
1342
+ } else {
1343
+ extra_root_certs_loaded = true ;
1347
1344
}
1348
1345
}
1349
1346
}
0 commit comments