• 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/credentials.h>
18 #include <grpc/grpc_security.h>
19 #include <grpcpp/security/tls_certificate_provider.h>
20 
21 #include <string>
22 #include <vector>
23 
24 #include "absl/log/check.h"
25 #include "src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.h"
26 
27 namespace grpc {
28 namespace experimental {
29 
StaticDataCertificateProvider(const std::string & root_certificate,const std::vector<IdentityKeyCertPair> & identity_key_cert_pairs)30 StaticDataCertificateProvider::StaticDataCertificateProvider(
31     const std::string& root_certificate,
32     const std::vector<IdentityKeyCertPair>& identity_key_cert_pairs) {
33   CHECK(!root_certificate.empty() || !identity_key_cert_pairs.empty());
34   grpc_tls_identity_pairs* pairs_core = grpc_tls_identity_pairs_create();
35   for (const IdentityKeyCertPair& pair : identity_key_cert_pairs) {
36     grpc_tls_identity_pairs_add_pair(pairs_core, pair.private_key.c_str(),
37                                      pair.certificate_chain.c_str());
38   }
39   c_provider_ = grpc_tls_certificate_provider_static_data_create(
40       root_certificate.c_str(), pairs_core);
41   CHECK_NE(c_provider_, nullptr);
42 };
43 
~StaticDataCertificateProvider()44 StaticDataCertificateProvider::~StaticDataCertificateProvider() {
45   grpc_tls_certificate_provider_release(c_provider_);
46 };
47 
ValidateCredentials() const48 absl::Status StaticDataCertificateProvider::ValidateCredentials() const {
49   auto* provider =
50       grpc_core::DownCast<grpc_core::StaticDataCertificateProvider*>(
51           c_provider_);
52   return provider->ValidateCredentials();
53 }
54 
FileWatcherCertificateProvider(const std::string & private_key_path,const std::string & identity_certificate_path,const std::string & root_cert_path,unsigned int refresh_interval_sec)55 FileWatcherCertificateProvider::FileWatcherCertificateProvider(
56     const std::string& private_key_path,
57     const std::string& identity_certificate_path,
58     const std::string& root_cert_path, unsigned int refresh_interval_sec) {
59   c_provider_ = grpc_tls_certificate_provider_file_watcher_create(
60       private_key_path.c_str(), identity_certificate_path.c_str(),
61       root_cert_path.c_str(), refresh_interval_sec);
62   CHECK_NE(c_provider_, nullptr);
63 };
64 
~FileWatcherCertificateProvider()65 FileWatcherCertificateProvider::~FileWatcherCertificateProvider() {
66   grpc_tls_certificate_provider_release(c_provider_);
67 };
68 
ValidateCredentials() const69 absl::Status FileWatcherCertificateProvider::ValidateCredentials() const {
70   auto* provider =
71       grpc_core::DownCast<grpc_core::FileWatcherCertificateProvider*>(
72           c_provider_);
73   return provider->ValidateCredentials();
74 }
75 
76 }  // namespace experimental
77 }  // namespace grpc
78