3 * Copyright 2018 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <grpc/support/port_platform.h>
21 #include <grpc/slice_buffer.h>
22 #include "src/core/lib/security/security_connector/load_system_roots_linux.h"
26 #include "src/core/lib/security/security_connector/load_system_roots.h"
32 #include <sys/param.h>
34 #include <sys/types.h>
37 #include <grpc/support/alloc.h>
38 #include <grpc/support/log.h>
39 #include <grpc/support/string_util.h>
41 #include "src/core/lib/gpr/string.h"
42 #include "src/core/lib/gpr/useful.h"
43 #include "src/core/lib/gprpp/global_config.h"
44 #include "src/core/lib/gprpp/inlined_vector.h"
45 #include "src/core/lib/iomgr/load_file.h"
47 GPR_GLOBAL_CONFIG_DEFINE_STRING(grpc_system_ssl_roots_dir, "",
48 "Custom directory to SSL Roots");
53 const char* kLinuxCertFiles[] = {
54 "/etc/ssl/certs/ca-certificates.crt", "/etc/pki/tls/certs/ca-bundle.crt",
55 "/etc/ssl/ca-bundle.pem", "/etc/pki/tls/cacert.pem",
56 "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"};
57 const char* kLinuxCertDirectories[] = {
58 "/etc/ssl/certs", "/system/etc/security/cacerts", "/usr/local/share/certs",
59 "/etc/pki/tls/certs", "/etc/openssl/certs"};
61 grpc_slice GetSystemRootCerts() {
62 grpc_slice valid_bundle_slice = grpc_empty_slice();
63 size_t num_cert_files_ = GPR_ARRAY_SIZE(kLinuxCertFiles);
64 for (size_t i = 0; i < num_cert_files_; i++) {
66 grpc_load_file(kLinuxCertFiles[i], 1, &valid_bundle_slice);
67 if (error == GRPC_ERROR_NONE) {
68 return valid_bundle_slice;
71 return grpc_empty_slice();
76 void GetAbsoluteFilePath(const char* valid_file_dir,
77 const char* file_entry_name, char* path_buffer) {
78 if (valid_file_dir != nullptr && file_entry_name != nullptr) {
79 int path_len = snprintf(path_buffer, MAXPATHLEN, "%s/%s", valid_file_dir,
82 gpr_log(GPR_ERROR, "failed to get absolute path for file: %s",
88 grpc_slice CreateRootCertsBundle(const char* certs_directory) {
89 grpc_slice bundle_slice = grpc_empty_slice();
90 if (certs_directory == nullptr) {
93 DIR* ca_directory = opendir(certs_directory);
94 if (ca_directory == nullptr) {
98 char path[MAXPATHLEN];
101 InlinedVector<FileData, 2> roots_filenames;
102 size_t total_bundle_size = 0;
103 struct dirent* directory_entry;
104 while ((directory_entry = readdir(ca_directory)) != nullptr) {
105 struct stat dir_entry_stat;
106 const char* file_entry_name = directory_entry->d_name;
108 GetAbsoluteFilePath(certs_directory, file_entry_name, file_data.path);
109 int stat_return = stat(file_data.path, &dir_entry_stat);
110 if (stat_return == -1 || !S_ISREG(dir_entry_stat.st_mode)) {
111 // no subdirectories.
112 if (stat_return == -1) {
113 gpr_log(GPR_ERROR, "failed to get status for file: %s", file_data.path);
117 file_data.size = dir_entry_stat.st_size;
118 total_bundle_size += file_data.size;
119 roots_filenames.push_back(file_data);
121 closedir(ca_directory);
122 char* bundle_string = static_cast<char*>(gpr_zalloc(total_bundle_size + 1));
123 size_t bytes_read = 0;
124 for (size_t i = 0; i < roots_filenames.size(); i++) {
125 int file_descriptor = open(roots_filenames[i].path, O_RDONLY);
126 if (file_descriptor != -1) {
127 // Read file into bundle.
128 size_t cert_file_size = roots_filenames[i].size;
130 read(file_descriptor, bundle_string + bytes_read, cert_file_size);
131 if (read_ret != -1) {
132 bytes_read += read_ret;
134 gpr_log(GPR_ERROR, "failed to read file: %s", roots_filenames[i].path);
138 bundle_slice = grpc_slice_new(bundle_string, bytes_read, gpr_free);
142 grpc_slice LoadSystemRootCerts() {
143 grpc_slice result = grpc_empty_slice();
144 // Prioritize user-specified custom directory if flag is set.
145 UniquePtr<char> custom_dir = GPR_GLOBAL_CONFIG_GET(grpc_system_ssl_roots_dir);
146 if (strlen(custom_dir.get()) > 0) {
147 result = CreateRootCertsBundle(custom_dir.get());
149 // If the custom directory is empty/invalid/not specified, fallback to
150 // distribution-specific directory.
151 if (GRPC_SLICE_IS_EMPTY(result)) {
152 result = GetSystemRootCerts();
154 if (GRPC_SLICE_IS_EMPTY(result)) {
155 for (size_t i = 0; i < GPR_ARRAY_SIZE(kLinuxCertDirectories); i++) {
156 result = CreateRootCertsBundle(kLinuxCertDirectories[i]);
157 if (!GRPC_SLICE_IS_EMPTY(result)) {
165 } // namespace grpc_core
167 #endif /* GPR_LINUX */