tls_credentials_options.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. //
  3. // Copyright 2019 gRPC authors.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. //
  18. #ifndef GRPCPP_SECURITY_TLS_CREDENTIALS_OPTIONS_H
  19. #define GRPCPP_SECURITY_TLS_CREDENTIALS_OPTIONS_H
  20. #include <memory>
  21. #include <vector>
  22. #include <grpc/grpc_security.h>
  23. #include <grpc/grpc_security_constants.h>
  24. #include <grpc/status.h>
  25. #include <grpc/support/log.h>
  26. #include <grpcpp/security/tls_certificate_provider.h>
  27. #include <grpcpp/security/tls_certificate_verifier.h>
  28. #include <grpcpp/support/config.h>
  29. namespace grpc {
  30. namespace experimental {
  31. // Base class of configurable options specified by users to configure their
  32. // certain security features supported in TLS. It is used for experimental
  33. // purposes for now and it is subject to change.
  34. class TlsCredentialsOptions {
  35. public:
  36. // Constructor for base class TlsCredentialsOptions.
  37. //
  38. // @param certificate_provider the provider which fetches TLS credentials that
  39. // will be used in the TLS handshake
  40. TlsCredentialsOptions();
  41. // ---- Setters for member fields ----
  42. // Sets the certificate provider used to store root certs and identity certs.
  43. void set_certificate_provider(
  44. std::shared_ptr<CertificateProviderInterface> certificate_provider);
  45. // Watches the updates of root certificates with name |root_cert_name|.
  46. // If used in TLS credentials, setting this field is optional for both the
  47. // client side and the server side.
  48. // If this is not set on the client side, we will use the root certificates
  49. // stored in the default system location, since client side must provide root
  50. // certificates in TLS(no matter single-side TLS or mutual TLS).
  51. // If this is not set on the server side, we will not watch any root
  52. // certificate updates, and assume no root certificates needed for the server
  53. // (in the one-side TLS scenario, the server is not required to provide root
  54. // certs). We don't support default root certs on server side.
  55. void watch_root_certs();
  56. // Sets the name of root certificates being watched, if |watch_root_certs| is
  57. // called. If not set, an empty string will be used as the name.
  58. //
  59. // @param root_cert_name the name of root certs being set.
  60. void set_root_cert_name(const TString& root_cert_name);
  61. // Watches the updates of identity key-cert pairs with name
  62. // |identity_cert_name|. If used in TLS credentials, it is required to be set
  63. // on the server side, and optional for the client side(in the one-side
  64. // TLS scenario, the client is not required to provide identity certs).
  65. void watch_identity_key_cert_pairs();
  66. // Sets the name of identity key-cert pairs being watched, if
  67. // |watch_identity_key_cert_pairs| is called. If not set, an empty string will
  68. // be used as the name.
  69. //
  70. // @param identity_cert_name the name of identity key-cert pairs being set.
  71. void set_identity_cert_name(const TString& identity_cert_name);
  72. // Sets the Tls session key logging configuration. If not set, tls
  73. // session key logging is disabled. Note that this should be used only for
  74. // debugging purposes. It should never be used in a production environment
  75. // due to security concerns.
  76. //
  77. // @param tls_session_key_log_file_path: Path where tls session keys would
  78. // be logged.
  79. void set_tls_session_key_log_file_path(
  80. const TString& tls_session_key_log_file_path);
  81. // Sets the certificate verifier used to perform post-handshake peer identity
  82. // checks.
  83. void set_certificate_verifier(
  84. std::shared_ptr<CertificateVerifier> certificate_verifier);
  85. // Sets the options of whether to check the hostname of the peer on a per-call
  86. // basis. This is usually used in a combination with virtual hosting at the
  87. // client side, where each individual call on a channel can have a different
  88. // host associated with it.
  89. // This check is intended to verify that the host specified for the individual
  90. // call is covered by the cert that the peer presented.
  91. // We will perform such checks by default. This should be disabled if
  92. // verifiers other than the host name verifier is used.
  93. void set_check_call_host(bool check_call_host);
  94. // TODO(zhenlian): This is an experimental API is likely to change in the
  95. // future. Before de-experiementalizing, verify the API is up to date.
  96. // If set, gRPC will read all hashed x.509 CRL files in the directory and
  97. // enforce the CRL files on all TLS handshakes. Only supported for OpenSSL
  98. // version > 1.1.
  99. void set_crl_directory(const TString& path);
  100. // ----- Getters for member fields ----
  101. // Get the internal c options. This function shall be used only internally.
  102. grpc_tls_credentials_options* c_credentials_options() const {
  103. return c_credentials_options_;
  104. }
  105. private:
  106. std::shared_ptr<CertificateProviderInterface> certificate_provider_;
  107. std::shared_ptr<CertificateVerifier> certificate_verifier_;
  108. grpc_tls_credentials_options* c_credentials_options_ = nullptr;
  109. };
  110. // Contains configurable options on the client side.
  111. // Client side doesn't need to always use certificate provider. When the
  112. // certificate provider is not set, we will use the root certificates stored
  113. // in the system default locations, and assume client won't provide any
  114. // identity certificates(single side TLS).
  115. // It is used for experimental purposes for now and it is subject to change.
  116. class TlsChannelCredentialsOptions final : public TlsCredentialsOptions {
  117. public:
  118. // Sets the decision of whether to do a crypto check on the server certs.
  119. // The default is true.
  120. void set_verify_server_certs(bool verify_server_certs);
  121. private:
  122. };
  123. // Contains configurable options on the server side.
  124. // It is used for experimental purposes for now and it is subject to change.
  125. class TlsServerCredentialsOptions final : public TlsCredentialsOptions {
  126. public:
  127. // Server side is required to use a provider, because server always needs to
  128. // use identity certs.
  129. explicit TlsServerCredentialsOptions(
  130. std::shared_ptr<CertificateProviderInterface> certificate_provider)
  131. : TlsCredentialsOptions() {
  132. set_certificate_provider(certificate_provider);
  133. }
  134. // Sets option to request the certificates from the client.
  135. // The default is GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE.
  136. void set_cert_request_type(
  137. grpc_ssl_client_certificate_request_type cert_request_type);
  138. private:
  139. };
  140. } // namespace experimental
  141. } // namespace grpc
  142. #endif // GRPCPP_SECURITY_TLS_CREDENTIALS_OPTIONS_H