You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tls_certificate_provider.h 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #ifndef GRPCPP_SECURITY_TLS_CERTIFICATE_PROVIDER_H
  17. #define GRPCPP_SECURITY_TLS_CERTIFICATE_PROVIDER_H
  18. #include <memory>
  19. #include <vector>
  20. #include <grpc/grpc_security.h>
  21. #include <grpc/grpc_security_constants.h>
  22. #include <grpc/status.h>
  23. #include <grpc/support/log.h>
  24. #include <grpcpp/impl/codegen/grpc_library.h>
  25. #include <grpcpp/support/config.h>
  26. namespace grpc
  27. {
  28. namespace experimental
  29. {
  30. // Interface for a class that handles the process to fetch credential data.
  31. // Implementations should be a wrapper class of an internal provider
  32. // implementation.
  33. class CertificateProviderInterface
  34. {
  35. public:
  36. virtual ~CertificateProviderInterface() = default;
  37. virtual grpc_tls_certificate_provider* c_provider() = 0;
  38. };
  39. // A struct that stores the credential data presented to the peer in handshake
  40. // to show local identity. The private_key and certificate_chain should always
  41. // match.
  42. struct IdentityKeyCertPair
  43. {
  44. std::string private_key;
  45. std::string certificate_chain;
  46. };
  47. // A basic CertificateProviderInterface implementation that will load credential
  48. // data from static string during initialization. This provider will always
  49. // return the same cert data for all cert names, and reloading is not supported.
  50. class StaticDataCertificateProvider : public CertificateProviderInterface
  51. {
  52. public:
  53. StaticDataCertificateProvider(
  54. const std::string& root_certificate,
  55. const std::vector<IdentityKeyCertPair>& identity_key_cert_pairs
  56. );
  57. explicit StaticDataCertificateProvider(const std::string& root_certificate) :
  58. StaticDataCertificateProvider(root_certificate, {})
  59. {
  60. }
  61. explicit StaticDataCertificateProvider(
  62. const std::vector<IdentityKeyCertPair>& identity_key_cert_pairs
  63. ) :
  64. StaticDataCertificateProvider("", identity_key_cert_pairs)
  65. {
  66. }
  67. ~StaticDataCertificateProvider() override;
  68. grpc_tls_certificate_provider* c_provider() override
  69. {
  70. return c_provider_;
  71. }
  72. private:
  73. grpc_tls_certificate_provider* c_provider_ = nullptr;
  74. };
  75. // A CertificateProviderInterface implementation that will watch the credential
  76. // changes on the file system. This provider will always return the up-to-date
  77. // cert data for all the cert names callers set through |TlsCredentialsOptions|.
  78. // Several things to note:
  79. // 1. This API only supports one key-cert file and hence one set of identity
  80. // key-cert pair, so SNI(Server Name Indication) is not supported.
  81. // 2. The private key and identity certificate should always match. This API
  82. // guarantees atomic read, and it is the callers' responsibility to do atomic
  83. // updates. There are many ways to atomically update the key and certs in the
  84. // file system. To name a few:
  85. // 1) creating a new directory, renaming the old directory to a new name, and
  86. // then renaming the new directory to the original name of the old directory.
  87. // 2) using a symlink for the directory. When need to change, put new
  88. // credential data in a new directory, and change symlink.
  89. class FileWatcherCertificateProvider final : public CertificateProviderInterface
  90. {
  91. public:
  92. // Constructor to get credential updates from root and identity file paths.
  93. //
  94. // @param private_key_path is the file path of the private key.
  95. // @param identity_certificate_path is the file path of the identity
  96. // certificate chain.
  97. // @param root_cert_path is the file path to the root certificate bundle.
  98. // @param refresh_interval_sec is the refreshing interval that we will check
  99. // the files for updates.
  100. FileWatcherCertificateProvider(const std::string& private_key_path, const std::string& identity_certificate_path, const std::string& root_cert_path, unsigned int refresh_interval_sec);
  101. // Constructor to get credential updates from identity file paths only.
  102. FileWatcherCertificateProvider(const std::string& private_key_path, const std::string& identity_certificate_path, unsigned int refresh_interval_sec) :
  103. FileWatcherCertificateProvider(private_key_path, identity_certificate_path, "", refresh_interval_sec)
  104. {
  105. }
  106. // Constructor to get credential updates from root file path only.
  107. FileWatcherCertificateProvider(const std::string& root_cert_path, unsigned int refresh_interval_sec) :
  108. FileWatcherCertificateProvider("", "", root_cert_path, refresh_interval_sec)
  109. {
  110. }
  111. ~FileWatcherCertificateProvider() override;
  112. grpc_tls_certificate_provider* c_provider() override
  113. {
  114. return c_provider_;
  115. }
  116. private:
  117. grpc_tls_certificate_provider* c_provider_ = nullptr;
  118. };
  119. } // namespace experimental
  120. } // namespace grpc
  121. #endif // GRPCPP_SECURITY_TLS_CERTIFICATE_PROVIDER_H