您最多选择25个标签 标签必须以中文、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tls_certificate_verifier.h 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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, grpc_status_code status, const char* error_details
  40. );
  41. extern "C" grpc_tls_certificate_verifier*
  42. grpc_tls_certificate_verifier_external_create(
  43. grpc_tls_certificate_verifier_external* external_verifier
  44. );
  45. namespace grpc
  46. {
  47. namespace experimental
  48. {
  49. // Contains the verification-related information associated with a connection
  50. // request. Users should not directly create or destroy this request object, but
  51. // shall interact with it through CertificateVerifier's Verify() and Cancel().
  52. class TlsCustomVerificationCheckRequest
  53. {
  54. public:
  55. explicit TlsCustomVerificationCheckRequest(
  56. grpc_tls_custom_verification_check_request* request
  57. );
  58. ~TlsCustomVerificationCheckRequest()
  59. {
  60. }
  61. grpc::string_ref target_name() const;
  62. grpc::string_ref peer_cert() const;
  63. grpc::string_ref peer_cert_full_chain() const;
  64. grpc::string_ref common_name() const;
  65. std::vector<grpc::string_ref> uri_names() const;
  66. std::vector<grpc::string_ref> dns_names() const;
  67. std::vector<grpc::string_ref> email_names() const;
  68. std::vector<grpc::string_ref> ip_names() const;
  69. grpc_tls_custom_verification_check_request* c_request()
  70. {
  71. return c_request_;
  72. }
  73. private:
  74. grpc_tls_custom_verification_check_request* c_request_ = nullptr;
  75. };
  76. // The base class of all internal verifier implementations, and the ultimate
  77. // class that all external verifiers will eventually be transformed into.
  78. // To implement a custom verifier, do not extend this class; instead,
  79. // implement a subclass of ExternalCertificateVerifier. Note that custom
  80. // verifier implementations can compose their functionality with existing
  81. // implementations of this interface, such as HostnameVerifier, by delegating
  82. // to an instance of that class.
  83. class CertificateVerifier
  84. {
  85. public:
  86. explicit CertificateVerifier(grpc_tls_certificate_verifier* v);
  87. ~CertificateVerifier();
  88. // Verifies a connection request, based on the logic specified in an internal
  89. // verifier. The check on each internal verifier could be either synchronous
  90. // or asynchronous, and we will need to use return value to know.
  91. //
  92. // request: the verification information associated with this request
  93. // callback: This will only take effect if the verifier is asynchronous.
  94. // The function that gRPC will invoke when the verifier has already
  95. // completed its asynchronous check. Callers can use this function
  96. // to perform any additional checks. The input parameter of the
  97. // std::function indicates the status of the verifier check.
  98. // sync_status: This will only be useful if the verifier is synchronous.
  99. // The status of the verifier as it has already done it's
  100. // synchronous check.
  101. // return: return true if executed synchronously, otherwise return false
  102. bool Verify(TlsCustomVerificationCheckRequest* request, std::function<void(grpc::Status)> callback, grpc::Status* sync_status);
  103. // Cancels a verification request previously started via Verify().
  104. // Used when the connection attempt times out or is cancelled while an async
  105. // verification request is pending.
  106. //
  107. // request: the verification information associated with this request
  108. void Cancel(TlsCustomVerificationCheckRequest* request);
  109. // Gets the core verifier used internally.
  110. grpc_tls_certificate_verifier* c_verifier()
  111. {
  112. return verifier_;
  113. }
  114. private:
  115. static void AsyncCheckDone(
  116. grpc_tls_custom_verification_check_request* request, void* callback_arg, grpc_status_code status, const char* error_details
  117. );
  118. grpc_tls_certificate_verifier* verifier_ = nullptr;
  119. grpc::internal::Mutex mu_;
  120. std::map<grpc_tls_custom_verification_check_request*, std::function<void(grpc::Status)>>
  121. request_map_ ABSL_GUARDED_BY(mu_);
  122. };
  123. // The base class of all external, user-specified verifiers. Users should
  124. // inherit this class to implement a custom verifier.
  125. // Note that while implementing the custom verifier that extends this class, it
  126. // is possible to compose an existing ExternalCertificateVerifier or
  127. // CertificateVerifier, inside the Verify() and Cancel() function of the new
  128. // custom verifier.
  129. class ExternalCertificateVerifier
  130. {
  131. public:
  132. // A factory method for creating a |CertificateVerifier| from this class. All
  133. // the user-implemented verifiers should use this function to be converted to
  134. // verifiers compatible with |TlsCredentialsOptions|.
  135. // The resulting CertificateVerifier takes ownership of the newly instantiated
  136. // Subclass.
  137. template<typename Subclass, typename... Args>
  138. static std::shared_ptr<CertificateVerifier> Create(Args&&... args)
  139. {
  140. grpc::internal::GrpcLibraryInitializer g_gli_initializer;
  141. g_gli_initializer.summon();
  142. auto* external_verifier = new Subclass(std::forward<Args>(args)...);
  143. return std::make_shared<CertificateVerifier>(
  144. grpc_tls_certificate_verifier_external_create(
  145. external_verifier->base_
  146. )
  147. );
  148. }
  149. // The verification logic that will be performed after the TLS handshake
  150. // completes. Implementers can choose to do their checks synchronously or
  151. // asynchronously.
  152. //
  153. // request: the verification information associated with this request
  154. // callback: This should only be used if your check is done asynchronously.
  155. // When the asynchronous work is done, invoke this callback function
  156. // with the proper status, indicating the success or the failure of
  157. // the check. The implementer MUST NOT invoke this |callback| in the
  158. // same thread before Verify() returns, otherwise it can lead to
  159. // deadlocks.
  160. // sync_status: This should only be used if your check is done synchronously.
  161. // Modifies this value to indicate the success or the failure of
  162. // the check.
  163. // return: return true if your check is done synchronously, otherwise return
  164. // false
  165. virtual bool Verify(TlsCustomVerificationCheckRequest* request, std::function<void(grpc::Status)> callback, grpc::Status* sync_status) = 0;
  166. // Cancels a verification request previously started via Verify().
  167. // Used when the connection attempt times out or is cancelled while an async
  168. // verification request is pending. The implementation should abort whatever
  169. // async operation it is waiting for and quickly invoke the callback that was
  170. // passed to Verify() with a status indicating the cancellation.
  171. //
  172. // request: the verification information associated with this request
  173. virtual void Cancel(TlsCustomVerificationCheckRequest* request) = 0;
  174. protected:
  175. ExternalCertificateVerifier();
  176. virtual ~ExternalCertificateVerifier();
  177. private:
  178. struct AsyncRequestState
  179. {
  180. AsyncRequestState(grpc_tls_on_custom_verification_check_done_cb cb, void* arg, grpc_tls_custom_verification_check_request* request) :
  181. callback(cb),
  182. callback_arg(arg),
  183. cpp_request(request)
  184. {
  185. }
  186. grpc_tls_on_custom_verification_check_done_cb callback;
  187. void* callback_arg;
  188. TlsCustomVerificationCheckRequest cpp_request;
  189. };
  190. static int VerifyInCoreExternalVerifier(
  191. void* user_data, grpc_tls_custom_verification_check_request* request, grpc_tls_on_custom_verification_check_done_cb callback, void* callback_arg, grpc_status_code* sync_status, char** sync_error_details
  192. );
  193. static void CancelInCoreExternalVerifier(
  194. void* user_data, grpc_tls_custom_verification_check_request* request
  195. );
  196. static void DestructInCoreExternalVerifier(void* user_data);
  197. // TODO(yihuazhang): after the insecure build is removed, make this an object
  198. // member instead of a pointer.
  199. grpc_tls_certificate_verifier_external* base_ = nullptr;
  200. grpc::internal::Mutex mu_;
  201. std::map<grpc_tls_custom_verification_check_request*, AsyncRequestState>
  202. request_map_ ABSL_GUARDED_BY(mu_);
  203. };
  204. // A CertificateVerifier that doesn't perform any additional checks other than
  205. // certificate verification, if specified.
  206. // Note: using this solely without any other authentication mechanisms on the
  207. // peer identity will leave your applications to the MITM(Man-In-The-Middle)
  208. // attacks. Users should avoid doing so in production environments.
  209. class NoOpCertificateVerifier : public CertificateVerifier
  210. {
  211. public:
  212. NoOpCertificateVerifier();
  213. };
  214. // A CertificateVerifier that will perform hostname verification, to see if the
  215. // target name set from the client side matches the identity information
  216. // specified on the server's certificate.
  217. class HostNameCertificateVerifier : public CertificateVerifier
  218. {
  219. public:
  220. HostNameCertificateVerifier();
  221. };
  222. } // namespace experimental
  223. } // namespace grpc
  224. #endif // GRPCPP_SECURITY_TLS_CERTIFICATE_VERIFIER_H