tls_certificate_verifier.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // Copyright 2021 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_VERIFIER_H
  17. #define GRPCPP_SECURITY_TLS_CERTIFICATE_VERIFIER_H
  18. #include <functional>
  19. #include <map>
  20. #include <memory>
  21. #include <utility>
  22. #include <vector>
  23. #include <grpc/grpc_security_constants.h>
  24. #include <grpc/status.h>
  25. #include <grpc/support/log.h>
  26. #include <grpcpp/impl/codegen/grpc_library.h>
  27. #include <grpcpp/impl/codegen/sync.h>
  28. #include <grpcpp/impl/grpc_library.h>
  29. #include <grpcpp/support/config.h>
  30. #include <grpcpp/support/string_ref.h>
  31. // TODO(yihuazhang): remove the forward declaration here and include
  32. // <grpc/grpc_security.h> directly once the insecure builds are cleaned up.
  33. typedef struct grpc_tls_custom_verification_check_request
  34. grpc_tls_custom_verification_check_request;
  35. typedef struct grpc_tls_certificate_verifier grpc_tls_certificate_verifier;
  36. typedef struct grpc_tls_certificate_verifier_external
  37. grpc_tls_certificate_verifier_external;
  38. typedef void (*grpc_tls_on_custom_verification_check_done_cb)(
  39. grpc_tls_custom_verification_check_request* request, void* callback_arg,
  40. grpc_status_code status, const char* error_details);
  41. extern "C" grpc_tls_certificate_verifier*
  42. grpc_tls_certificate_verifier_external_create(
  43. grpc_tls_certificate_verifier_external* external_verifier);
  44. namespace grpc {
  45. namespace experimental {
  46. // Contains the verification-related information associated with a connection
  47. // request. Users should not directly create or destroy this request object, but
  48. // shall interact with it through CertificateVerifier's Verify() and Cancel().
  49. class TlsCustomVerificationCheckRequest {
  50. public:
  51. explicit TlsCustomVerificationCheckRequest(
  52. grpc_tls_custom_verification_check_request* request);
  53. ~TlsCustomVerificationCheckRequest() {}
  54. grpc::string_ref target_name() const;
  55. grpc::string_ref peer_cert() const;
  56. grpc::string_ref peer_cert_full_chain() const;
  57. grpc::string_ref common_name() const;
  58. std::vector<grpc::string_ref> uri_names() const;
  59. std::vector<grpc::string_ref> dns_names() const;
  60. std::vector<grpc::string_ref> email_names() const;
  61. std::vector<grpc::string_ref> ip_names() const;
  62. grpc_tls_custom_verification_check_request* c_request() { return c_request_; }
  63. private:
  64. grpc_tls_custom_verification_check_request* c_request_ = nullptr;
  65. };
  66. // The base class of all internal verifier implementations, and the ultimate
  67. // class that all external verifiers will eventually be transformed into.
  68. // To implement a custom verifier, do not extend this class; instead,
  69. // implement a subclass of ExternalCertificateVerifier. Note that custom
  70. // verifier implementations can compose their functionality with existing
  71. // implementations of this interface, such as HostnameVerifier, by delegating
  72. // to an instance of that class.
  73. class CertificateVerifier {
  74. public:
  75. explicit CertificateVerifier(grpc_tls_certificate_verifier* v);
  76. ~CertificateVerifier();
  77. // Verifies a connection request, based on the logic specified in an internal
  78. // verifier. The check on each internal verifier could be either synchronous
  79. // or asynchronous, and we will need to use return value to know.
  80. //
  81. // request: the verification information associated with this request
  82. // callback: This will only take effect if the verifier is asynchronous.
  83. // The function that gRPC will invoke when the verifier has already
  84. // completed its asynchronous check. Callers can use this function
  85. // to perform any additional checks. The input parameter of the
  86. // std::function indicates the status of the verifier check.
  87. // sync_status: This will only be useful if the verifier is synchronous.
  88. // The status of the verifier as it has already done it's
  89. // synchronous check.
  90. // return: return true if executed synchronously, otherwise return false
  91. bool Verify(TlsCustomVerificationCheckRequest* request,
  92. std::function<void(grpc::Status)> callback,
  93. grpc::Status* sync_status);
  94. // Cancels a verification request previously started via Verify().
  95. // Used when the connection attempt times out or is cancelled while an async
  96. // verification request is pending.
  97. //
  98. // request: the verification information associated with this request
  99. void Cancel(TlsCustomVerificationCheckRequest* request);
  100. // Gets the core verifier used internally.
  101. grpc_tls_certificate_verifier* c_verifier() { return verifier_; }
  102. private:
  103. static void AsyncCheckDone(
  104. grpc_tls_custom_verification_check_request* request, void* callback_arg,
  105. grpc_status_code status, const char* error_details);
  106. grpc_tls_certificate_verifier* verifier_ = nullptr;
  107. grpc::internal::Mutex mu_;
  108. std::map<grpc_tls_custom_verification_check_request*,
  109. std::function<void(grpc::Status)>>
  110. request_map_ ABSL_GUARDED_BY(mu_);
  111. };
  112. // The base class of all external, user-specified verifiers. Users should
  113. // inherit this class to implement a custom verifier.
  114. // Note that while implementing the custom verifier that extends this class, it
  115. // is possible to compose an existing ExternalCertificateVerifier or
  116. // CertificateVerifier, inside the Verify() and Cancel() function of the new
  117. // custom verifier.
  118. class ExternalCertificateVerifier {
  119. public:
  120. // A factory method for creating a |CertificateVerifier| from this class. All
  121. // the user-implemented verifiers should use this function to be converted to
  122. // verifiers compatible with |TlsCredentialsOptions|.
  123. // The resulting CertificateVerifier takes ownership of the newly instantiated
  124. // Subclass.
  125. template <typename Subclass, typename... Args>
  126. static std::shared_ptr<CertificateVerifier> Create(Args&&... args) {
  127. grpc::internal::GrpcLibraryInitializer g_gli_initializer;
  128. g_gli_initializer.summon();
  129. auto* external_verifier = new Subclass(std::forward<Args>(args)...);
  130. return std::make_shared<CertificateVerifier>(
  131. grpc_tls_certificate_verifier_external_create(
  132. external_verifier->base_));
  133. }
  134. // The verification logic that will be performed after the TLS handshake
  135. // completes. Implementers can choose to do their checks synchronously or
  136. // asynchronously.
  137. //
  138. // request: the verification information associated with this request
  139. // callback: This should only be used if your check is done asynchronously.
  140. // When the asynchronous work is done, invoke this callback function
  141. // with the proper status, indicating the success or the failure of
  142. // the check. The implementer MUST NOT invoke this |callback| in the
  143. // same thread before Verify() returns, otherwise it can lead to
  144. // deadlocks.
  145. // sync_status: This should only be used if your check is done synchronously.
  146. // Modifies this value to indicate the success or the failure of
  147. // the check.
  148. // return: return true if your check is done synchronously, otherwise return
  149. // false
  150. virtual bool Verify(TlsCustomVerificationCheckRequest* request,
  151. std::function<void(grpc::Status)> callback,
  152. grpc::Status* sync_status) = 0;
  153. // Cancels a verification request previously started via Verify().
  154. // Used when the connection attempt times out or is cancelled while an async
  155. // verification request is pending. The implementation should abort whatever
  156. // async operation it is waiting for and quickly invoke the callback that was
  157. // passed to Verify() with a status indicating the cancellation.
  158. //
  159. // request: the verification information associated with this request
  160. virtual void Cancel(TlsCustomVerificationCheckRequest* request) = 0;
  161. protected:
  162. ExternalCertificateVerifier();
  163. virtual ~ExternalCertificateVerifier();
  164. private:
  165. struct AsyncRequestState {
  166. AsyncRequestState(grpc_tls_on_custom_verification_check_done_cb cb,
  167. void* arg,
  168. grpc_tls_custom_verification_check_request* request)
  169. : callback(cb), callback_arg(arg), cpp_request(request) {}
  170. grpc_tls_on_custom_verification_check_done_cb callback;
  171. void* callback_arg;
  172. TlsCustomVerificationCheckRequest cpp_request;
  173. };
  174. static int VerifyInCoreExternalVerifier(
  175. void* user_data, grpc_tls_custom_verification_check_request* request,
  176. grpc_tls_on_custom_verification_check_done_cb callback,
  177. void* callback_arg, grpc_status_code* sync_status,
  178. char** sync_error_details);
  179. static void CancelInCoreExternalVerifier(
  180. void* user_data, grpc_tls_custom_verification_check_request* request);
  181. static void DestructInCoreExternalVerifier(void* user_data);
  182. // TODO(yihuazhang): after the insecure build is removed, make this an object
  183. // member instead of a pointer.
  184. grpc_tls_certificate_verifier_external* base_ = nullptr;
  185. grpc::internal::Mutex mu_;
  186. std::map<grpc_tls_custom_verification_check_request*, AsyncRequestState>
  187. request_map_ ABSL_GUARDED_BY(mu_);
  188. };
  189. class HostNameCertificateVerifier : public CertificateVerifier {
  190. public:
  191. HostNameCertificateVerifier();
  192. };
  193. } // namespace experimental
  194. } // namespace grpc
  195. #endif // GRPCPP_SECURITY_TLS_CERTIFICATE_VERIFIER_H