@@ -221,15 +221,15 @@ int SSL_CTX_use_certificate_chain(SSL_CTX* ctx,
221
221
issuer);
222
222
}
223
223
224
- unsigned long LoadCertsFromFile ( // NOLINT(runtime/int)
224
+ static unsigned long LoadCertsFromFile ( // NOLINT(runtime/int)
225
225
std::vector<X509*>* certs,
226
226
const char * file) {
227
227
MarkPopErrorOnReturn mark_pop_error_on_return;
228
228
229
229
auto bio = BIOPointer::NewFile (file, " r" );
230
230
if (!bio) return ERR_get_error ();
231
231
232
- while (X509* x509 = PEM_read_bio_X509 (
232
+ while (X509* x509 = PEM_read_bio_X509_AUX (
233
233
bio.get (), nullptr , NoPasswordCallback, nullptr )) {
234
234
certs->push_back (x509);
235
235
}
@@ -643,6 +643,73 @@ void ReadWindowsCertificates(
643
643
}
644
644
#endif
645
645
646
+ void LoadCertsFromDir (std::vector<X509*>* certs, std::string_view cert_dir) {
647
+ uv_fs_t dir_req;
648
+ auto cleanup = OnScopeLeave ([&dir_req]() { uv_fs_req_cleanup (&dir_req); });
649
+ int err = uv_fs_scandir (nullptr , &dir_req, cert_dir.data (), 0 , nullptr );
650
+ if (err < 0 ) {
651
+ fprintf (stderr,
652
+ " Cannot open directory %s to load OpenSSL certificates.\n " ,
653
+ cert_dir.data ());
654
+ return ;
655
+ }
656
+
657
+ uv_fs_t stats_req;
658
+ auto cleanup_stats =
659
+ OnScopeLeave ([&stats_req]() { uv_fs_req_cleanup (&stats_req); });
660
+ for (;;) {
661
+ uv_dirent_t ent;
662
+
663
+ int r = uv_fs_scandir_next (&dir_req, &ent);
664
+ if (r == UV_EOF) {
665
+ break ;
666
+ }
667
+ if (r < 0 ) {
668
+ char message[64 ];
669
+ uv_strerror_r (r, message, sizeof (message));
670
+ fprintf (stderr,
671
+ " Cannot scan directory %s to load OpenSSL certificates.\n " ,
672
+ cert_dir.data ());
673
+ return ;
674
+ }
675
+
676
+ std::string file_path = std::string (cert_dir) + " /" + ent.name ;
677
+ int stats_r = uv_fs_stat (nullptr , &stats_req, file_path.c_str (), nullptr );
678
+ if (stats_r == 0 &&
679
+ (static_cast <uv_stat_t *>(stats_req.ptr )->st_mode & S_IFREG)) {
680
+ LoadCertsFromFile (certs, file_path.c_str ());
681
+ }
682
+ }
683
+ }
684
+
685
+ // Loads CA certificates from the default certificate paths respected by
686
+ // OpenSSL.
687
+ void GetOpenSSLSystemCertificates (std::vector<X509*>* system_store_certs) {
688
+ std::string cert_file;
689
+ // While configurable when OpenSSL is built, this is usually SSL_CERT_FILE.
690
+ if (!credentials::SafeGetenv (X509_get_default_cert_file_env (), &cert_file)) {
691
+ // This is usually /etc/ssl/cert.pem if we are using the OpenSSL statically
692
+ // linked and built with default configurations.
693
+ cert_file = X509_get_default_cert_file ();
694
+ }
695
+
696
+ std::string cert_dir;
697
+ // While configurable when OpenSSL is built, this is usually SSL_CERT_DIR.
698
+ if (!credentials::SafeGetenv (X509_get_default_cert_dir_env (), &cert_dir)) {
699
+ // This is usually /etc/ssl/certs if we are using the OpenSSL statically
700
+ // linked and built with default configurations.
701
+ cert_dir = X509_get_default_cert_dir ();
702
+ }
703
+
704
+ if (!cert_file.empty ()) {
705
+ LoadCertsFromFile (system_store_certs, cert_file.c_str ());
706
+ }
707
+
708
+ if (!cert_dir.empty ()) {
709
+ LoadCertsFromDir (system_store_certs, cert_dir.c_str ());
710
+ }
711
+ }
712
+
646
713
static std::vector<X509*> InitializeBundledRootCertificates () {
647
714
// Read the bundled certificates in node_root_certs.h into
648
715
// bundled_root_certs_vector.
@@ -683,6 +750,9 @@ static std::vector<X509*> InitializeSystemStoreCertificates() {
683
750
#endif
684
751
#ifdef _WIN32
685
752
ReadWindowsCertificates (&system_store_certs);
753
+ #endif
754
+ #if !defined(__APPLE__) && !defined(_WIN32)
755
+ GetOpenSSLSystemCertificates (&system_store_certs);
686
756
#endif
687
757
return system_store_certs;
688
758
}
@@ -1297,7 +1367,7 @@ void SecureContext::SetAllowPartialTrustChain(
1297
1367
void SecureContext::SetCACert (const BIOPointer& bio) {
1298
1368
ClearErrorOnReturn clear_error_on_return;
1299
1369
if (!bio) return ;
1300
- while (X509Pointer x509 = X509Pointer (PEM_read_bio_X509_AUX (
1370
+ while (X509Pointer x509 = X509Pointer (PEM_read_bio_X509 (
1301
1371
bio.get (), nullptr , NoPasswordCallback, nullptr ))) {
1302
1372
CHECK_EQ (1 ,
1303
1373
X509_STORE_add_cert (GetCertStoreOwnedByThisSecureContext (), x509));
0 commit comments