Debian Codegraph @

test/core/util/tls_utils.h

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