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_credentials_options.h 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. {
  31. namespace experimental
  32. {
  33. // Base class of configurable options specified by users to configure their
  34. // certain security features supported in TLS. It is used for experimental
  35. // purposes for now and it is subject to change.
  36. class TlsCredentialsOptions
  37. {
  38. public:
  39. // Constructor for base class TlsCredentialsOptions.
  40. //
  41. // @param certificate_provider the provider which fetches TLS credentials that
  42. // will be used in the TLS handshake
  43. TlsCredentialsOptions();
  44. // ---- Setters for member fields ----
  45. // Sets the certificate provider used to store root certs and identity certs.
  46. void set_certificate_provider(
  47. std::shared_ptr<CertificateProviderInterface> certificate_provider
  48. );
  49. // Watches the updates of root certificates with name |root_cert_name|.
  50. // If used in TLS credentials, setting this field is optional for both the
  51. // client side and the server side.
  52. // If this is not set on the client side, we will use the root certificates
  53. // stored in the default system location, since client side must provide root
  54. // certificates in TLS(no matter single-side TLS or mutual TLS).
  55. // If this is not set on the server side, we will not watch any root
  56. // certificate updates, and assume no root certificates needed for the server
  57. // (in the one-side TLS scenario, the server is not required to provide root
  58. // certs). We don't support default root certs on server side.
  59. void watch_root_certs();
  60. // Sets the name of root certificates being watched, if |watch_root_certs| is
  61. // called. If not set, an empty string will be used as the name.
  62. //
  63. // @param root_cert_name the name of root certs being set.
  64. void set_root_cert_name(const std::string& root_cert_name);
  65. // Watches the updates of identity key-cert pairs with name
  66. // |identity_cert_name|. If used in TLS credentials, it is required to be set
  67. // on the server side, and optional for the client side(in the one-side
  68. // TLS scenario, the client is not required to provide identity certs).
  69. void watch_identity_key_cert_pairs();
  70. // Sets the name of identity key-cert pairs being watched, if
  71. // |watch_identity_key_cert_pairs| is called. If not set, an empty string will
  72. // be used as the name.
  73. //
  74. // @param identity_cert_name the name of identity key-cert pairs being set.
  75. void set_identity_cert_name(const std::string& identity_cert_name);
  76. // Sets the Tls session key logging configuration. If not set, tls
  77. // session key logging is disabled. Note that this should be used only for
  78. // debugging purposes. It should never be used in a production environment
  79. // due to security concerns.
  80. //
  81. // @param tls_session_key_log_file_path: Path where tls session keys would
  82. // be logged.
  83. void set_tls_session_key_log_file_path(
  84. const std::string& tls_session_key_log_file_path
  85. );
  86. // Sets the certificate verifier used to perform post-handshake peer identity
  87. // checks.
  88. void set_certificate_verifier(
  89. std::shared_ptr<CertificateVerifier> certificate_verifier
  90. );
  91. // Sets the options of whether to check the hostname of the peer on a per-call
  92. // basis. This is usually used in a combination with virtual hosting at the
  93. // client side, where each individual call on a channel can have a different
  94. // host associated with it.
  95. // This check is intended to verify that the host specified for the individual
  96. // call is covered by the cert that the peer presented.
  97. // We will perform such checks by default. This should be disabled if
  98. // verifiers other than the host name verifier is used.
  99. void set_check_call_host(bool check_call_host);
  100. // TODO(zhenlian): This is an experimental API is likely to change in the
  101. // future. Before de-experiementalizing, verify the API is up to date.
  102. // If set, gRPC will read all hashed x.509 CRL files in the directory and
  103. // enforce the CRL files on all TLS handshakes. Only supported for OpenSSL
  104. // version > 1.1.
  105. void set_crl_directory(const std::string& path);
  106. // ----- Getters for member fields ----
  107. // Get the internal c options. This function shall be used only internally.
  108. grpc_tls_credentials_options* c_credentials_options() const
  109. {
  110. return c_credentials_options_;
  111. }
  112. private:
  113. std::shared_ptr<CertificateProviderInterface> certificate_provider_;
  114. std::shared_ptr<CertificateVerifier> certificate_verifier_;
  115. grpc_tls_credentials_options* c_credentials_options_ = nullptr;
  116. };
  117. // Contains configurable options on the client side.
  118. // Client side doesn't need to always use certificate provider. When the
  119. // certificate provider is not set, we will use the root certificates stored
  120. // in the system default locations, and assume client won't provide any
  121. // identity certificates(single side TLS).
  122. // It is used for experimental purposes for now and it is subject to change.
  123. class TlsChannelCredentialsOptions final : public TlsCredentialsOptions
  124. {
  125. public:
  126. // Sets the decision of whether to do a crypto check on the server certs.
  127. // The default is true.
  128. void set_verify_server_certs(bool verify_server_certs);
  129. private:
  130. };
  131. // Contains configurable options on the server side.
  132. // It is used for experimental purposes for now and it is subject to change.
  133. class TlsServerCredentialsOptions final : public TlsCredentialsOptions
  134. {
  135. public:
  136. // Server side is required to use a provider, because server always needs to
  137. // use identity certs.
  138. explicit TlsServerCredentialsOptions(
  139. std::shared_ptr<CertificateProviderInterface> certificate_provider
  140. ) :
  141. TlsCredentialsOptions()
  142. {
  143. set_certificate_provider(certificate_provider);
  144. }
  145. // Sets option to request the certificates from the client.
  146. // The default is GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE.
  147. void set_cert_request_type(
  148. grpc_ssl_client_certificate_request_type cert_request_type
  149. );
  150. private:
  151. };
  152. } // namespace experimental
  153. } // namespace grpc
  154. #endif // GRPCPP_SECURITY_TLS_CREDENTIALS_OPTIONS_H