-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
crypto: support --use-system-ca on non-Windows and non-macOS #57009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
crypto: support --use-system-ca on non-Windows and non-macOS
On other platforms, load from the OpenSSL default certificate file and diretory. This is different from --use-openssl-ca in that it caches the certificates on first load, instead of always reading from disk every time a new root store is needed. When used together with the statically-linked OpenSSL, the default configuration usually leads to this behavior: - If SSL_CERT_FILE is used, load from SSL_CERT_FILE. Otherwise load from /etc/ssl/cert.pem - If SSL_CERT_DIR is used, load from all the files under SSL_CERT_DIR. Otherwise, load from all the files under /etc/ssl/certs
- Loading branch information
commit 891de467b144a78b61af046415cddc83cd15b131
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -221,7 +221,7 @@ int SSL_CTX_use_certificate_chain(SSL_CTX* ctx, | |
| issuer); | ||
| } | ||
|
|
||
| unsigned long LoadCertsFromFile( // NOLINT(runtime/int) | ||
| static unsigned long LoadCertsFromFile( // NOLINT(runtime/int) | ||
| std::vector<X509*>* certs, | ||
| const char* file) { | ||
| MarkPopErrorOnReturn mark_pop_error_on_return; | ||
|
|
@@ -643,6 +643,74 @@ void ReadWindowsCertificates( | |
| } | ||
| #endif | ||
|
|
||
| static void LoadCertsFromDir(std::vector<X509*>* certs, | ||
| std::string_view cert_dir) { | ||
| uv_fs_t dir_req; | ||
| auto cleanup = OnScopeLeave([&dir_req]() { uv_fs_req_cleanup(&dir_req); }); | ||
| int err = uv_fs_scandir(nullptr, &dir_req, cert_dir.data(), 0, nullptr); | ||
| if (err < 0) { | ||
| fprintf(stderr, | ||
| "Cannot open directory %s to load OpenSSL certificates.\n", | ||
| cert_dir.data()); | ||
| return; | ||
| } | ||
|
|
||
| uv_fs_t stats_req; | ||
| auto cleanup_stats = | ||
| OnScopeLeave([&stats_req]() { uv_fs_req_cleanup(&stats_req); }); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not for this PR, just thinking out loud... it would be nice if we had smart pointer type wrappers for these so we could simplify this kind of code. |
||
| for (;;) { | ||
| uv_dirent_t ent; | ||
|
|
||
| int r = uv_fs_scandir_next(&dir_req, &ent); | ||
| if (r == UV_EOF) { | ||
| break; | ||
| } | ||
| if (r < 0) { | ||
| char message[64]; | ||
| uv_strerror_r(r, message, sizeof(message)); | ||
| fprintf(stderr, | ||
| "Cannot scan directory %s to load OpenSSL certificates.\n", | ||
| cert_dir.data()); | ||
| return; | ||
| } | ||
|
|
||
| std::string file_path = std::string(cert_dir) + "/" + ent.name; | ||
|
jasnell marked this conversation as resolved.
|
||
| int stats_r = uv_fs_stat(nullptr, &stats_req, file_path.c_str(), nullptr); | ||
| if (stats_r == 0 && | ||
| (static_cast<uv_stat_t*>(stats_req.ptr)->st_mode & S_IFREG)) { | ||
| LoadCertsFromFile(certs, file_path.c_str()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Loads CA certificates from the default certificate paths respected by | ||
| // OpenSSL. | ||
| void GetOpenSSLSystemCertificates(std::vector<X509*>* system_store_certs) { | ||
| std::string cert_file; | ||
| // While configurable when OpenSSL is built, this is usually SSL_CERT_FILE. | ||
| if (!credentials::SafeGetenv(X509_get_default_cert_file_env(), &cert_file)) { | ||
| // This is usually /etc/ssl/cert.pem if we are using the OpenSSL statically | ||
| // linked and built with default configurations. | ||
| cert_file = X509_get_default_cert_file(); | ||
| } | ||
|
|
||
| std::string cert_dir; | ||
| // While configurable when OpenSSL is built, this is usually SSL_CERT_DIR. | ||
| if (!credentials::SafeGetenv(X509_get_default_cert_dir_env(), &cert_dir)) { | ||
| // This is usually /etc/ssl/certs if we are using the OpenSSL statically | ||
| // linked and built with default configurations. | ||
| cert_dir = X509_get_default_cert_dir(); | ||
| } | ||
|
|
||
| if (!cert_file.empty()) { | ||
| LoadCertsFromFile(system_store_certs, cert_file.c_str()); | ||
| } | ||
|
|
||
| if (!cert_dir.empty()) { | ||
| LoadCertsFromDir(system_store_certs, cert_dir.c_str()); | ||
| } | ||
| } | ||
|
|
||
| static std::vector<X509*> InitializeBundledRootCertificates() { | ||
| // Read the bundled certificates in node_root_certs.h into | ||
| // bundled_root_certs_vector. | ||
|
|
@@ -683,6 +751,9 @@ static std::vector<X509*> InitializeSystemStoreCertificates() { | |
| #endif | ||
| #ifdef _WIN32 | ||
| ReadWindowsCertificates(&system_store_certs); | ||
| #endif | ||
| #if !defined(__APPLE__) && !defined(_WIN32) | ||
| GetOpenSSLSystemCertificates(&system_store_certs); | ||
| #endif | ||
| return system_store_certs; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.