• 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/grpc_security.h>
18 #include <grpc/support/alloc.h>
19 #include <grpcpp/security/tls_certificate_provider.h>
20 
21 #include "absl/container/inlined_vector.h"
22 
23 namespace grpc {
24 namespace experimental {
25 
StaticDataCertificateProvider(const std::string & root_certificate,const std::vector<IdentityKeyCertPair> & identity_key_cert_pairs)26 StaticDataCertificateProvider::StaticDataCertificateProvider(
27     const std::string& root_certificate,
28     const std::vector<IdentityKeyCertPair>& identity_key_cert_pairs) {
29   GPR_ASSERT(!root_certificate.empty() || !identity_key_cert_pairs.empty());
30   grpc_tls_identity_pairs* pairs_core = grpc_tls_identity_pairs_create();
31   for (const IdentityKeyCertPair& pair : identity_key_cert_pairs) {
32     grpc_tls_identity_pairs_add_pair(pairs_core, pair.private_key.c_str(),
33                                      pair.certificate_chain.c_str());
34   }
35   c_provider_ = grpc_tls_certificate_provider_static_data_create(
36       root_certificate.c_str(), pairs_core);
37   GPR_ASSERT(c_provider_ != nullptr);
38 };
39 
~StaticDataCertificateProvider()40 StaticDataCertificateProvider::~StaticDataCertificateProvider() {
41   grpc_tls_certificate_provider_release(c_provider_);
42 };
43 
FileWatcherCertificateProvider(const std::string & private_key_path,const std::string & identity_certificate_path,const std::string & root_cert_path,unsigned int refresh_interval_sec)44 FileWatcherCertificateProvider::FileWatcherCertificateProvider(
45     const std::string& private_key_path,
46     const std::string& identity_certificate_path,
47     const std::string& root_cert_path, unsigned int refresh_interval_sec) {
48   c_provider_ = grpc_tls_certificate_provider_file_watcher_create(
49       private_key_path.c_str(), identity_certificate_path.c_str(),
50       root_cert_path.c_str(), refresh_interval_sec);
51   GPR_ASSERT(c_provider_ != nullptr);
52 };
53 
~FileWatcherCertificateProvider()54 FileWatcherCertificateProvider::~FileWatcherCertificateProvider() {
55   grpc_tls_certificate_provider_release(c_provider_);
56 };
57 
58 }  // namespace experimental
59 }  // namespace grpc
60