tls_certificate_provider.cc 2.2 KB

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