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