tls_credentials_options.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_constants.h>
  23. #include <grpc/status.h>
  24. #include <grpc/support/log.h>
  25. #include <grpcpp/security/tls_certificate_provider.h>
  26. #include <grpcpp/security/tls_certificate_verifier.h>
  27. #include <grpcpp/support/config.h>
  28. // TODO(yihuazhang): remove the forward declaration here and include
  29. // <grpc/grpc_security.h> directly once the insecure builds are cleaned up.
  30. typedef struct grpc_tls_server_authorization_check_arg
  31. grpc_tls_server_authorization_check_arg;
  32. typedef struct grpc_tls_server_authorization_check_config
  33. grpc_tls_server_authorization_check_config;
  34. typedef struct grpc_tls_credentials_options grpc_tls_credentials_options;
  35. typedef struct grpc_tls_certificate_provider grpc_tls_certificate_provider;
  36. typedef struct grpc_tls_certificate_verifier grpc_tls_certificate_verifier;
  37. namespace grpc {
  38. namespace experimental {
  39. // Base class of configurable options specified by users to configure their
  40. // certain security features supported in TLS. It is used for experimental
  41. // purposes for now and it is subject to change.
  42. class TlsCredentialsOptions {
  43. public:
  44. // Constructor for base class TlsCredentialsOptions.
  45. //
  46. // @param certificate_provider the provider which fetches TLS credentials that
  47. // will be used in the TLS handshake
  48. TlsCredentialsOptions();
  49. // ---- Setters for member fields ----
  50. // Sets the certificate provider used to store root certs and identity certs.
  51. void set_certificate_provider(
  52. std::shared_ptr<CertificateProviderInterface> certificate_provider);
  53. // Watches the updates of root certificates with name |root_cert_name|.
  54. // If used in TLS credentials, setting this field is optional for both the
  55. // client side and the server side.
  56. // If this is not set on the client side, we will use the root certificates
  57. // stored in the default system location, since client side must provide root
  58. // certificates in TLS(no matter single-side TLS or mutual TLS).
  59. // If this is not set on the server side, we will not watch any root
  60. // certificate updates, and assume no root certificates needed for the server
  61. // (in the one-side TLS scenario, the server is not required to provide root
  62. // certs). We don't support default root certs on server side.
  63. void watch_root_certs();
  64. // Sets the name of root certificates being watched, if |watch_root_certs| is
  65. // called. If not set, an empty string will be used as the name.
  66. //
  67. // @param root_cert_name the name of root certs being set.
  68. void set_root_cert_name(const std::string& root_cert_name);
  69. // Watches the updates of identity key-cert pairs with name
  70. // |identity_cert_name|. If used in TLS credentials, it is required to be set
  71. // on the server side, and optional for the client side(in the one-side
  72. // TLS scenario, the client is not required to provide identity certs).
  73. void watch_identity_key_cert_pairs();
  74. // Sets the name of identity key-cert pairs being watched, if
  75. // |watch_identity_key_cert_pairs| is called. If not set, an empty string will
  76. // be used as the name.
  77. //
  78. // @param identity_cert_name the name of identity key-cert pairs being set.
  79. void set_identity_cert_name(const std::string& identity_cert_name);
  80. // Sets the certificate verifier used to perform post-handshake peer identity
  81. // checks.
  82. void set_certificate_verifier(
  83. std::shared_ptr<CertificateVerifier> certificate_verifier);
  84. // Sets the options of whether to check the hostname of the peer on a per-call
  85. // basis. This is usually used in a combination with virtual hosting at the
  86. // client side, where each individual call on a channel can have a different
  87. // host associated with it.
  88. // This check is intended to verify that the host specified for the individual
  89. // call is covered by the cert that the peer presented.
  90. // We will perform such checks by default. This should be disabled if
  91. // verifiers other than the host name verifier is used.
  92. void set_check_call_host(bool check_call_host);
  93. // ----- Getters for member fields ----
  94. // Get the internal c options. This function shall be used only internally.
  95. grpc_tls_credentials_options* c_credentials_options() const {
  96. return c_credentials_options_;
  97. }
  98. private:
  99. std::shared_ptr<CertificateProviderInterface> certificate_provider_;
  100. std::shared_ptr<CertificateVerifier> certificate_verifier_;
  101. grpc_tls_credentials_options* c_credentials_options_ = nullptr;
  102. };
  103. // Contains configurable options on the client side.
  104. // Client side doesn't need to always use certificate provider. When the
  105. // certificate provider is not set, we will use the root certificates stored
  106. // in the system default locations, and assume client won't provide any
  107. // identity certificates(single side TLS).
  108. // It is used for experimental purposes for now and it is subject to change.
  109. class TlsChannelCredentialsOptions final : public TlsCredentialsOptions {
  110. public:
  111. // Sets the decision of whether to do a crypto check on the server certs.
  112. // The default is true.
  113. void set_verify_server_certs(bool verify_server_certs);
  114. private:
  115. };
  116. // Contains configurable options on the server side.
  117. // It is used for experimental purposes for now and it is subject to change.
  118. class TlsServerCredentialsOptions final : public TlsCredentialsOptions {
  119. public:
  120. // Server side is required to use a provider, because server always needs to
  121. // use identity certs.
  122. explicit TlsServerCredentialsOptions(
  123. std::shared_ptr<CertificateProviderInterface> certificate_provider)
  124. : TlsCredentialsOptions() {
  125. set_certificate_provider(certificate_provider);
  126. }
  127. // Sets option to request the certificates from the client.
  128. // The default is GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE.
  129. void set_cert_request_type(
  130. grpc_ssl_client_certificate_request_type cert_request_type);
  131. private:
  132. };
  133. } // namespace experimental
  134. } // namespace grpc
  135. #endif // GRPCPP_SECURITY_TLS_CREDENTIALS_OPTIONS_H