tls_utils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // Copyright 2020 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. #include <deque>
  17. #include "src/core/lib/gprpp/thd.h"
  18. #include "src/core/lib/security/security_connector/ssl_utils.h"
  19. #include "test/core/util/test_config.h"
  20. namespace grpc_core {
  21. namespace testing {
  22. class TmpFile {
  23. public:
  24. // Create a temporary file with |data| written in.
  25. explicit TmpFile(absl::string_view data);
  26. ~TmpFile();
  27. const std::string& name() { return name_; }
  28. // Rewrite |data| to the temporary file, in an atomic way.
  29. void RewriteFile(absl::string_view data);
  30. private:
  31. std::string CreateTmpFileAndWriteData(absl::string_view data);
  32. std::string name_;
  33. };
  34. PemKeyCertPairList MakeCertKeyPairs(absl::string_view private_key,
  35. absl::string_view certs);
  36. std::string GetFileContents(const char* path);
  37. // A synchronous external verifier implementation that simply returns
  38. // verification results based on users' inputs. Note that it will delete itself
  39. // in Destruct(), so create it like
  40. // ```
  41. // auto* sync_verifier_ = new SyncExternalVerifier(false);
  42. // ```
  43. // and no need to delete it later. This is basically to keep consistent with the
  44. // semantics in AsyncExternalVerifier.
  45. class SyncExternalVerifier {
  46. public:
  47. explicit SyncExternalVerifier(bool success)
  48. : success_(success), base_{this, Verify, Cancel, Destruct} {}
  49. grpc_tls_certificate_verifier_external* base() { return &base_; }
  50. private:
  51. static int Verify(void* user_data,
  52. grpc_tls_custom_verification_check_request* request,
  53. grpc_tls_on_custom_verification_check_done_cb callback,
  54. void* callback_arg, grpc_status_code* sync_status,
  55. char** sync_error_details);
  56. static void Cancel(void*, grpc_tls_custom_verification_check_request*) {}
  57. static void Destruct(void* user_data);
  58. bool success_ = false;
  59. grpc_tls_certificate_verifier_external base_;
  60. };
  61. // An asynchronous external verifier implementation that runs a thread and
  62. // process each request received from the verifier sequentially. Note that it
  63. // will delete itself in Destruct(), so create it like
  64. // ```
  65. // auto* async_verifier = new AsyncExternalVerifier(true, &event);
  66. // auto* core_external_verifier =
  67. // new ExternalCertificateVerifier(async_verifier->base());
  68. // ```
  69. // and no need to delete it later.
  70. // We delete AsyncExternalVerifier in Destruct() instead of its dtor because we
  71. // wanted AsyncExternalVerifier to outlive the underlying core
  72. // ExternalCertificateVerifier implementation.
  73. class AsyncExternalVerifier {
  74. public:
  75. explicit AsyncExternalVerifier(bool success)
  76. : success_(success),
  77. thread_("AsyncExternalVerifierWorkerThread", WorkerThread, this),
  78. base_{this, Verify, Cancel, Destruct} {
  79. grpc_init();
  80. thread_.Start();
  81. }
  82. ~AsyncExternalVerifier();
  83. grpc_tls_certificate_verifier_external* base() { return &base_; }
  84. private:
  85. // A request to pass to the worker thread.
  86. struct Request {
  87. grpc_tls_custom_verification_check_request* request;
  88. grpc_tls_on_custom_verification_check_done_cb callback;
  89. void* callback_arg;
  90. bool shutdown; // If true, thread will exit.
  91. };
  92. static int Verify(void* user_data,
  93. grpc_tls_custom_verification_check_request* request,
  94. grpc_tls_on_custom_verification_check_done_cb callback,
  95. void* callback_arg, grpc_status_code* sync_status,
  96. char** sync_error_details);
  97. static void Cancel(void*, grpc_tls_custom_verification_check_request*) {}
  98. static void Destruct(void* user_data);
  99. static void WorkerThread(void* arg);
  100. bool success_ = false;
  101. Thread thread_;
  102. grpc_tls_certificate_verifier_external base_;
  103. Mutex mu_;
  104. std::deque<Request> queue_ ABSL_GUARDED_BY(mu_);
  105. };
  106. } // namespace testing
  107. } // namespace grpc_core