• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2020 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include <grpc/support/port_platform.h>
18 
19 #include "src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.h"
20 
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/log.h>
23 #include <grpc/support/string_util.h>
24 
25 #include "src/core/lib/gprpp/stat.h"
26 #include "src/core/lib/slice/slice_internal.h"
27 #include "src/core/lib/surface/api_trace.h"
28 
29 namespace grpc_core {
30 
StaticDataCertificateProvider(std::string root_certificate,grpc_core::PemKeyCertPairList pem_key_cert_pairs)31 StaticDataCertificateProvider::StaticDataCertificateProvider(
32     std::string root_certificate,
33     grpc_core::PemKeyCertPairList pem_key_cert_pairs)
34     : distributor_(MakeRefCounted<grpc_tls_certificate_distributor>()),
35       root_certificate_(std::move(root_certificate)),
36       pem_key_cert_pairs_(std::move(pem_key_cert_pairs)) {
37   distributor_->SetWatchStatusCallback([this](std::string cert_name,
38                                               bool root_being_watched,
39                                               bool identity_being_watched) {
40     grpc_core::MutexLock lock(&mu_);
41     absl::optional<std::string> root_certificate;
42     absl::optional<grpc_core::PemKeyCertPairList> pem_key_cert_pairs;
43     StaticDataCertificateProvider::WatcherInfo& info = watcher_info_[cert_name];
44     if (!info.root_being_watched && root_being_watched &&
45         !root_certificate_.empty()) {
46       root_certificate = root_certificate_;
47     }
48     info.root_being_watched = root_being_watched;
49     if (!info.identity_being_watched && identity_being_watched &&
50         !pem_key_cert_pairs_.empty()) {
51       pem_key_cert_pairs = pem_key_cert_pairs_;
52     }
53     info.identity_being_watched = identity_being_watched;
54     if (!info.root_being_watched && !info.identity_being_watched) {
55       watcher_info_.erase(cert_name);
56     }
57     const bool root_has_update = root_certificate.has_value();
58     const bool identity_has_update = pem_key_cert_pairs.has_value();
59     if (root_has_update || identity_has_update) {
60       distributor_->SetKeyMaterials(cert_name, std::move(root_certificate),
61                                     std::move(pem_key_cert_pairs));
62     }
63     grpc_error* root_cert_error = GRPC_ERROR_NONE;
64     grpc_error* identity_cert_error = GRPC_ERROR_NONE;
65     if (root_being_watched && !root_has_update) {
66       root_cert_error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
67           "Unable to get latest root certificates.");
68     }
69     if (identity_being_watched && !identity_has_update) {
70       identity_cert_error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
71           "Unable to get latest identity certificates.");
72     }
73     if (root_cert_error != GRPC_ERROR_NONE ||
74         identity_cert_error != GRPC_ERROR_NONE) {
75       distributor_->SetErrorForCert(cert_name, root_cert_error,
76                                     identity_cert_error);
77     }
78   });
79 }
80 
~StaticDataCertificateProvider()81 StaticDataCertificateProvider::~StaticDataCertificateProvider() {
82   // Reset distributor's callback to make sure the callback won't be invoked
83   // again after this object(provider) is destroyed.
84   distributor_->SetWatchStatusCallback(nullptr);
85 }
86 
87 namespace {
88 
TimeoutSecondsToDeadline(int64_t seconds)89 gpr_timespec TimeoutSecondsToDeadline(int64_t seconds) {
90   return gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
91                       gpr_time_from_seconds(seconds, GPR_TIMESPAN));
92 }
93 
94 }  // namespace
95 
FileWatcherCertificateProvider(std::string private_key_path,std::string identity_certificate_path,std::string root_cert_path,unsigned int refresh_interval_sec)96 FileWatcherCertificateProvider::FileWatcherCertificateProvider(
97     std::string private_key_path, std::string identity_certificate_path,
98     std::string root_cert_path, unsigned int refresh_interval_sec)
99     : private_key_path_(std::move(private_key_path)),
100       identity_certificate_path_(std::move(identity_certificate_path)),
101       root_cert_path_(std::move(root_cert_path)),
102       refresh_interval_sec_(refresh_interval_sec),
103       distributor_(MakeRefCounted<grpc_tls_certificate_distributor>()) {
104   // Private key and identity cert files must be both set or both unset.
105   GPR_ASSERT(private_key_path_.empty() == identity_certificate_path_.empty());
106   // Must be watching either root or identity certs.
107   GPR_ASSERT(!private_key_path_.empty() || !root_cert_path_.empty());
108   gpr_event_init(&shutdown_event_);
109   ForceUpdate();
110   auto thread_lambda = [](void* arg) {
111     FileWatcherCertificateProvider* provider =
112         static_cast<FileWatcherCertificateProvider*>(arg);
113     GPR_ASSERT(provider != nullptr);
114     while (true) {
115       void* value = gpr_event_wait(
116           &provider->shutdown_event_,
117           TimeoutSecondsToDeadline(provider->refresh_interval_sec_));
118       if (value != nullptr) {
119         return;
120       };
121       provider->ForceUpdate();
122     }
123   };
124   refresh_thread_ = grpc_core::Thread(
125       "FileWatcherCertificateProvider_refreshing_thread", thread_lambda, this);
126   refresh_thread_.Start();
127   distributor_->SetWatchStatusCallback([this](std::string cert_name,
128                                               bool root_being_watched,
129                                               bool identity_being_watched) {
130     grpc_core::MutexLock lock(&mu_);
131     absl::optional<std::string> root_certificate;
132     absl::optional<grpc_core::PemKeyCertPairList> pem_key_cert_pairs;
133     FileWatcherCertificateProvider::WatcherInfo& info =
134         watcher_info_[cert_name];
135     if (!info.root_being_watched && root_being_watched &&
136         !root_certificate_.empty()) {
137       root_certificate = root_certificate_;
138     }
139     info.root_being_watched = root_being_watched;
140     if (!info.identity_being_watched && identity_being_watched &&
141         !pem_key_cert_pairs_.empty()) {
142       pem_key_cert_pairs = pem_key_cert_pairs_;
143     }
144     info.identity_being_watched = identity_being_watched;
145     if (!info.root_being_watched && !info.identity_being_watched) {
146       watcher_info_.erase(cert_name);
147     }
148     ExecCtx exec_ctx;
149     if (root_certificate.has_value() || pem_key_cert_pairs.has_value()) {
150       distributor_->SetKeyMaterials(cert_name, root_certificate,
151                                     pem_key_cert_pairs);
152     }
153     grpc_error* root_cert_error = GRPC_ERROR_NONE;
154     grpc_error* identity_cert_error = GRPC_ERROR_NONE;
155     if (root_being_watched && !root_certificate.has_value()) {
156       root_cert_error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
157           "Unable to get latest root certificates.");
158     }
159     if (identity_being_watched && !pem_key_cert_pairs.has_value()) {
160       identity_cert_error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
161           "Unable to get latest identity certificates.");
162     }
163     if (root_cert_error != GRPC_ERROR_NONE ||
164         identity_cert_error != GRPC_ERROR_NONE) {
165       distributor_->SetErrorForCert(cert_name, root_cert_error,
166                                     identity_cert_error);
167     }
168   });
169 }
170 
~FileWatcherCertificateProvider()171 FileWatcherCertificateProvider::~FileWatcherCertificateProvider() {
172   // Reset distributor's callback to make sure the callback won't be invoked
173   // again after this object(provider) is destroyed.
174   distributor_->SetWatchStatusCallback(nullptr);
175   gpr_event_set(&shutdown_event_, reinterpret_cast<void*>(1));
176   refresh_thread_.Join();
177 }
178 
ForceUpdate()179 void FileWatcherCertificateProvider::ForceUpdate() {
180   absl::optional<std::string> root_certificate;
181   absl::optional<grpc_core::PemKeyCertPairList> pem_key_cert_pairs;
182   if (!root_cert_path_.empty()) {
183     root_certificate = ReadRootCertificatesFromFile(root_cert_path_);
184   }
185   if (!private_key_path_.empty()) {
186     pem_key_cert_pairs = ReadIdentityKeyCertPairFromFiles(
187         private_key_path_, identity_certificate_path_);
188   }
189   grpc_core::MutexLock lock(&mu_);
190   const bool root_cert_changed =
191       (!root_certificate.has_value() && !root_certificate_.empty()) ||
192       (root_certificate.has_value() && root_certificate_ != *root_certificate);
193   if (root_cert_changed) {
194     if (root_certificate.has_value()) {
195       root_certificate_ = std::move(*root_certificate);
196     } else {
197       root_certificate_ = "";
198     }
199   }
200   const bool identity_cert_changed =
201       (!pem_key_cert_pairs.has_value() && !pem_key_cert_pairs_.empty()) ||
202       (pem_key_cert_pairs.has_value() &&
203        pem_key_cert_pairs_ != *pem_key_cert_pairs);
204   if (identity_cert_changed) {
205     if (pem_key_cert_pairs.has_value()) {
206       pem_key_cert_pairs_ = std::move(*pem_key_cert_pairs);
207     } else {
208       pem_key_cert_pairs_ = {};
209     }
210   }
211   if (root_cert_changed || identity_cert_changed) {
212     ExecCtx exec_ctx;
213     grpc_error* root_cert_error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
214         "Unable to get latest root certificates.");
215     grpc_error* identity_cert_error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
216         "Unable to get latest identity certificates.");
217     for (const auto& p : watcher_info_) {
218       const std::string& cert_name = p.first;
219       const WatcherInfo& info = p.second;
220       absl::optional<std::string> root_to_report;
221       absl::optional<grpc_core::PemKeyCertPairList> identity_to_report;
222       // Set key materials to the distributor if their contents changed.
223       if (info.root_being_watched && !root_certificate_.empty() &&
224           root_cert_changed) {
225         root_to_report = root_certificate_;
226       }
227       if (info.identity_being_watched && !pem_key_cert_pairs_.empty() &&
228           identity_cert_changed) {
229         identity_to_report = pem_key_cert_pairs_;
230       }
231       if (root_to_report.has_value() || identity_to_report.has_value()) {
232         distributor_->SetKeyMaterials(cert_name, std::move(root_to_report),
233                                       std::move(identity_to_report));
234       }
235       // Report errors to the distributor if the contents are empty.
236       const bool report_root_error =
237           info.root_being_watched && root_certificate_.empty();
238       const bool report_identity_error =
239           info.identity_being_watched && pem_key_cert_pairs_.empty();
240       if (report_root_error || report_identity_error) {
241         distributor_->SetErrorForCert(
242             cert_name,
243             report_root_error ? GRPC_ERROR_REF(root_cert_error)
244                               : GRPC_ERROR_NONE,
245             report_identity_error ? GRPC_ERROR_REF(identity_cert_error)
246                                   : GRPC_ERROR_NONE);
247       }
248     }
249     GRPC_ERROR_UNREF(root_cert_error);
250     GRPC_ERROR_UNREF(identity_cert_error);
251   }
252 }
253 
254 absl::optional<std::string>
ReadRootCertificatesFromFile(const std::string & root_cert_full_path)255 FileWatcherCertificateProvider::ReadRootCertificatesFromFile(
256     const std::string& root_cert_full_path) {
257   // Read the root file.
258   grpc_slice root_slice = grpc_empty_slice();
259   grpc_error* root_error =
260       grpc_load_file(root_cert_full_path.c_str(), 0, &root_slice);
261   if (root_error != GRPC_ERROR_NONE) {
262     gpr_log(GPR_ERROR, "Reading file %s failed: %s",
263             root_cert_full_path.c_str(), grpc_error_string(root_error));
264     GRPC_ERROR_UNREF(root_error);
265     return absl::nullopt;
266   }
267   std::string root_cert(StringViewFromSlice(root_slice));
268   grpc_slice_unref_internal(root_slice);
269   return root_cert;
270 }
271 
272 namespace {
273 
274 // This helper function gets the last-modified time of |filename|. When failed,
275 // it logs the error and returns 0.
GetModificationTime(const char * filename)276 time_t GetModificationTime(const char* filename) {
277   time_t ts = 0;
278   absl::Status status = grpc_core::GetFileModificationTime(filename, &ts);
279   return ts;
280 }
281 
282 }  // namespace
283 
284 absl::optional<PemKeyCertPairList>
ReadIdentityKeyCertPairFromFiles(const std::string & private_key_path,const std::string & identity_certificate_path)285 FileWatcherCertificateProvider::ReadIdentityKeyCertPairFromFiles(
286     const std::string& private_key_path,
287     const std::string& identity_certificate_path) {
288   struct SliceWrapper {
289     grpc_slice slice = grpc_empty_slice();
290     ~SliceWrapper() { grpc_slice_unref_internal(slice); }
291   };
292   const int kNumRetryAttempts = 3;
293   for (int i = 0; i < kNumRetryAttempts; ++i) {
294     // TODO(ZhenLian): replace the timestamp approach with key-match approach
295     //  once the latter is implemented.
296     // Checking the last modification of identity files before reading.
297     time_t identity_key_ts_before =
298         GetModificationTime(private_key_path.c_str());
299     if (identity_key_ts_before == 0) {
300       gpr_log(
301           GPR_ERROR,
302           "Failed to get the file's modification time of %s. Start retrying...",
303           private_key_path.c_str());
304       continue;
305     }
306     time_t identity_cert_ts_before =
307         GetModificationTime(identity_certificate_path.c_str());
308     if (identity_cert_ts_before == 0) {
309       gpr_log(
310           GPR_ERROR,
311           "Failed to get the file's modification time of %s. Start retrying...",
312           identity_certificate_path.c_str());
313       continue;
314     }
315     // Read the identity files.
316     SliceWrapper key_slice, cert_slice;
317     grpc_error* key_error =
318         grpc_load_file(private_key_path.c_str(), 0, &key_slice.slice);
319     if (key_error != GRPC_ERROR_NONE) {
320       gpr_log(GPR_ERROR, "Reading file %s failed: %s. Start retrying...",
321               private_key_path.c_str(), grpc_error_string(key_error));
322       GRPC_ERROR_UNREF(key_error);
323       continue;
324     }
325     grpc_error* cert_error =
326         grpc_load_file(identity_certificate_path.c_str(), 0, &cert_slice.slice);
327     if (cert_error != GRPC_ERROR_NONE) {
328       gpr_log(GPR_ERROR, "Reading file %s failed: %s. Start retrying...",
329               identity_certificate_path.c_str(), grpc_error_string(cert_error));
330       GRPC_ERROR_UNREF(cert_error);
331       continue;
332     }
333     std::string private_key(StringViewFromSlice(key_slice.slice));
334     std::string cert_chain(StringViewFromSlice(cert_slice.slice));
335     PemKeyCertPairList identity_pairs;
336     identity_pairs.emplace_back(private_key, cert_chain);
337     // Checking the last modification of identity files before reading.
338     time_t identity_key_ts_after =
339         GetModificationTime(private_key_path.c_str());
340     if (identity_key_ts_before != identity_key_ts_after) {
341       gpr_log(GPR_ERROR,
342               "Last modified time before and after reading %s is not the same. "
343               "Start retrying...",
344               private_key_path.c_str());
345       continue;
346     }
347     time_t identity_cert_ts_after =
348         GetModificationTime(identity_certificate_path.c_str());
349     if (identity_cert_ts_before != identity_cert_ts_after) {
350       gpr_log(GPR_ERROR,
351               "Last modified time before and after reading %s is not the same. "
352               "Start retrying...",
353               identity_certificate_path.c_str());
354       continue;
355     }
356     return identity_pairs;
357   }
358   gpr_log(GPR_ERROR,
359           "All retry attempts failed. Will try again after the next interval.");
360   return absl::nullopt;
361 }
362 
363 }  // namespace grpc_core
364 
365 /** -- Wrapper APIs declared in grpc_security.h -- **/
366 
grpc_tls_certificate_provider_static_data_create(const char * root_certificate,grpc_tls_identity_pairs * pem_key_cert_pairs)367 grpc_tls_certificate_provider* grpc_tls_certificate_provider_static_data_create(
368     const char* root_certificate, grpc_tls_identity_pairs* pem_key_cert_pairs) {
369   GPR_ASSERT(root_certificate != nullptr || pem_key_cert_pairs != nullptr);
370   grpc_core::PemKeyCertPairList identity_pairs_core;
371   if (pem_key_cert_pairs != nullptr) {
372     identity_pairs_core = std::move(pem_key_cert_pairs->pem_key_cert_pairs);
373     delete pem_key_cert_pairs;
374   }
375   std::string root_cert_core;
376   if (root_certificate != nullptr) {
377     root_cert_core = root_certificate;
378   }
379   return new grpc_core::StaticDataCertificateProvider(
380       std::move(root_cert_core), std::move(identity_pairs_core));
381 }
382 
383 grpc_tls_certificate_provider*
grpc_tls_certificate_provider_file_watcher_create(const char * private_key_path,const char * identity_certificate_path,const char * root_cert_path,unsigned int refresh_interval_sec)384 grpc_tls_certificate_provider_file_watcher_create(
385     const char* private_key_path, const char* identity_certificate_path,
386     const char* root_cert_path, unsigned int refresh_interval_sec) {
387   return new grpc_core::FileWatcherCertificateProvider(
388       private_key_path == nullptr ? "" : private_key_path,
389       identity_certificate_path == nullptr ? "" : identity_certificate_path,
390       root_cert_path == nullptr ? "" : root_cert_path, refresh_interval_sec);
391 }
392 
grpc_tls_certificate_provider_release(grpc_tls_certificate_provider * provider)393 void grpc_tls_certificate_provider_release(
394     grpc_tls_certificate_provider* provider) {
395   GRPC_API_TRACE("grpc_tls_certificate_provider_release(provider=%p)", 1,
396                  (provider));
397   grpc_core::ExecCtx exec_ctx;
398   if (provider != nullptr) provider->Unref();
399 }
400