Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / src / core / lib / security / security_connector / load_system_roots_linux.cc
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/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"
46
47 GPR_GLOBAL_CONFIG_DEFINE_STRING(grpc_system_ssl_roots_dir, "",
48                                 "Custom directory to SSL Roots");
49
50 namespace grpc_core {
51 namespace {
52
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"};
60
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++) {
65     grpc_error* error =
66         grpc_load_file(kLinuxCertFiles[i], 1, &valid_bundle_slice);
67     if (error == GRPC_ERROR_NONE) {
68       return valid_bundle_slice;
69     }
70   }
71   return grpc_empty_slice();
72 }
73
74 }  // namespace
75
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,
80                             file_entry_name);
81     if (path_len == 0) {
82       gpr_log(GPR_ERROR, "failed to get absolute path for file: %s",
83               file_entry_name);
84     }
85   }
86 }
87
88 grpc_slice CreateRootCertsBundle(const char* certs_directory) {
89   grpc_slice bundle_slice = grpc_empty_slice();
90   if (certs_directory == nullptr) {
91     return bundle_slice;
92   }
93   DIR* ca_directory = opendir(certs_directory);
94   if (ca_directory == nullptr) {
95     return bundle_slice;
96   }
97   struct FileData {
98     char path[MAXPATHLEN];
99     off_t size;
100   };
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;
107     FileData file_data;
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);
114       }
115       continue;
116     }
117     file_data.size = dir_entry_stat.st_size;
118     total_bundle_size += file_data.size;
119     roots_filenames.push_back(file_data);
120   }
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;
129       int read_ret =
130           read(file_descriptor, bundle_string + bytes_read, cert_file_size);
131       if (read_ret != -1) {
132         bytes_read += read_ret;
133       } else {
134         gpr_log(GPR_ERROR, "failed to read file: %s", roots_filenames[i].path);
135       }
136     }
137   }
138   bundle_slice = grpc_slice_new(bundle_string, bytes_read, gpr_free);
139   return bundle_slice;
140 }
141
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());
148   }
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();
153   }
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)) {
158         break;
159       }
160     }
161   }
162   return result;
163 }
164
165 }  // namespace grpc_core
166
167 #endif /* GPR_LINUX */