1 /*
2 *
3 * Copyright 2018 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include <grpc/slice_buffer.h>
22 #include "src/core/lib/security/security_connector/load_system_roots_linux.h"
23
24 #ifdef GPR_LINUX
25
26 #include "src/core/lib/security/security_connector/load_system_roots.h"
27
28 #include <dirent.h>
29 #include <fcntl.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #include <grpc/support/alloc.h>
38 #include <grpc/support/log.h>
39 #include <grpc/support/string_util.h>
40
41 #include "src/core/lib/gpr/env.h"
42 #include "src/core/lib/gpr/string.h"
43 #include "src/core/lib/gpr/useful.h"
44 #include "src/core/lib/gprpp/inlined_vector.h"
45 #include "src/core/lib/iomgr/load_file.h"
46
47 namespace grpc_core {
48 namespace {
49
50 const char* kLinuxCertFiles[] = {
51 "/etc/ssl/certs/ca-certificates.crt", "/etc/pki/tls/certs/ca-bundle.crt",
52 "/etc/ssl/ca-bundle.pem", "/etc/pki/tls/cacert.pem",
53 "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"};
54 const char* kLinuxCertDirectories[] = {
55 "/etc/ssl/certs", "/system/etc/security/cacerts", "/usr/local/share/certs",
56 "/etc/pki/tls/certs", "/etc/openssl/certs"};
57
GetSystemRootCerts()58 grpc_slice GetSystemRootCerts() {
59 grpc_slice valid_bundle_slice = grpc_empty_slice();
60 size_t num_cert_files_ = GPR_ARRAY_SIZE(kLinuxCertFiles);
61 for (size_t i = 0; i < num_cert_files_; i++) {
62 grpc_error* error =
63 grpc_load_file(kLinuxCertFiles[i], 1, &valid_bundle_slice);
64 if (error == GRPC_ERROR_NONE) {
65 return valid_bundle_slice;
66 }
67 }
68 return grpc_empty_slice();
69 }
70
71 } // namespace
72
GetAbsoluteFilePath(const char * valid_file_dir,const char * file_entry_name,char * path_buffer)73 void GetAbsoluteFilePath(const char* valid_file_dir,
74 const char* file_entry_name, char* path_buffer) {
75 if (valid_file_dir != nullptr && file_entry_name != nullptr) {
76 int path_len = snprintf(path_buffer, MAXPATHLEN, "%s/%s", valid_file_dir,
77 file_entry_name);
78 if (path_len == 0) {
79 gpr_log(GPR_ERROR, "failed to get absolute path for file: %s",
80 file_entry_name);
81 }
82 }
83 }
84
CreateRootCertsBundle(const char * certs_directory)85 grpc_slice CreateRootCertsBundle(const char* certs_directory) {
86 grpc_slice bundle_slice = grpc_empty_slice();
87 if (certs_directory == nullptr) {
88 return bundle_slice;
89 }
90 DIR* ca_directory = opendir(certs_directory);
91 if (ca_directory == nullptr) {
92 return bundle_slice;
93 }
94 struct FileData {
95 char path[MAXPATHLEN];
96 off_t size;
97 };
98 InlinedVector<FileData, 2> roots_filenames;
99 size_t total_bundle_size = 0;
100 struct dirent* directory_entry;
101 while ((directory_entry = readdir(ca_directory)) != nullptr) {
102 struct stat dir_entry_stat;
103 const char* file_entry_name = directory_entry->d_name;
104 FileData file_data;
105 GetAbsoluteFilePath(certs_directory, file_entry_name, file_data.path);
106 int stat_return = stat(file_data.path, &dir_entry_stat);
107 if (stat_return == -1 || !S_ISREG(dir_entry_stat.st_mode)) {
108 // no subdirectories.
109 if (stat_return == -1) {
110 gpr_log(GPR_ERROR, "failed to get status for file: %s", file_data.path);
111 }
112 continue;
113 }
114 file_data.size = dir_entry_stat.st_size;
115 total_bundle_size += file_data.size;
116 roots_filenames.push_back(file_data);
117 }
118 closedir(ca_directory);
119 char* bundle_string = static_cast<char*>(gpr_zalloc(total_bundle_size + 1));
120 size_t bytes_read = 0;
121 for (size_t i = 0; i < roots_filenames.size(); i++) {
122 int file_descriptor = open(roots_filenames[i].path, O_RDONLY);
123 if (file_descriptor != -1) {
124 // Read file into bundle.
125 size_t cert_file_size = roots_filenames[i].size;
126 int read_ret =
127 read(file_descriptor, bundle_string + bytes_read, cert_file_size);
128 if (read_ret != -1) {
129 bytes_read += read_ret;
130 } else {
131 gpr_log(GPR_ERROR, "failed to read file: %s", roots_filenames[i].path);
132 }
133 }
134 }
135 bundle_slice = grpc_slice_new(bundle_string, bytes_read, gpr_free);
136 return bundle_slice;
137 }
138
LoadSystemRootCerts()139 grpc_slice LoadSystemRootCerts() {
140 grpc_slice result = grpc_empty_slice();
141 // Prioritize user-specified custom directory if flag is set.
142 char* custom_dir = gpr_getenv("GRPC_SYSTEM_SSL_ROOTS_DIR");
143 if (custom_dir != nullptr) {
144 result = CreateRootCertsBundle(custom_dir);
145 gpr_free(custom_dir);
146 }
147 // If the custom directory is empty/invalid/not specified, fallback to
148 // distribution-specific directory.
149 if (GRPC_SLICE_IS_EMPTY(result)) {
150 result = GetSystemRootCerts();
151 }
152 if (GRPC_SLICE_IS_EMPTY(result)) {
153 for (size_t i = 0; i < GPR_ARRAY_SIZE(kLinuxCertDirectories); i++) {
154 result = CreateRootCertsBundle(kLinuxCertDirectories[i]);
155 if (!GRPC_SLICE_IS_EMPTY(result)) {
156 break;
157 }
158 }
159 }
160 return result;
161 }
162
163 } // namespace grpc_core
164
165 #endif /* GPR_LINUX */
166