Debian Codegraph @

test/core/handshake/verify_peer_options.cc

raw
1 /*
2 *
3 * Copyright 2018 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
19 #include "src/core/lib/iomgr/port.h"
20
21 // This test won't work except with posix sockets enabled
22 #ifdef GRPC_POSIX_SOCKET_TCP
23
24 #include <arpa/inet.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/socket.h>
28 #include <unistd.h>
29
30 #include <string>
31
32 #include <openssl/err.h>
33 #include <openssl/ssl.h>
34
35 #include "absl/strings/str_cat.h"
36
37 #include <grpc/grpc.h>
38 #include <grpc/grpc_security.h>
39 #include <grpc/support/alloc.h>
40 #include <grpc/support/log.h>
41
42 #include "src/core/lib/gprpp/thd.h"
43 #include "src/core/lib/iomgr/load_file.h"
44 #include "test/core/util/port.h"
45 #include "test/core/util/test_config.h"
46
47 #define SSL_CERT_PATH "src/core/tsi/test_creds/server1.pem"
48 #define SSL_KEY_PATH "src/core/tsi/test_creds/server1.key"
49 #define SSL_CA_PATH "src/core/tsi/test_creds/ca.pem"
50
51 // Simple gRPC server. This listens until client_handshake_complete occurs.
52 static gpr_event client_handshake_complete;
53
54 static void server_thread(void* arg) {
55 const int port = *static_cast<int*>(arg);
56
57 // Load key pair and establish server SSL credentials.
58 grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
59 grpc_slice ca_slice, cert_slice, key_slice;
60 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
61 grpc_load_file(SSL_CA_PATH, 1, &ca_slice)));
62 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
63 grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
64 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
65 grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
66 const char* ca_cert =
67 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
68 pem_key_cert_pair.private_key =
69 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
70 pem_key_cert_pair.cert_chain =
71 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
72 grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
73 ca_cert, &pem_key_cert_pair, 1, 0, nullptr);
74
75 // Start server listening on local port.
76 std::string addr = absl::StrCat("127.0.0.1:", port);
77 grpc_server* server = grpc_server_create(nullptr, nullptr);
78 GPR_ASSERT(grpc_server_add_http2_port(server, addr.c_str(), ssl_creds));
79
80 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
81
82 grpc_server_register_completion_queue(server, cq, nullptr);
83 grpc_server_start(server);
84
85 // Wait a bounded number of time until client_handshake_complete is set,
86 // sleeping between polls. The total time spent (deadline * retries)
87 // should be strictly greater than the client retry limit so that the
88 // client will always timeout first.
89 int retries = 60;
90 while (!gpr_event_get(&client_handshake_complete) && retries-- > 0) {
91 const gpr_timespec cq_deadline = grpc_timeout_seconds_to_deadline(1);
92 grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, nullptr);
93 GPR_ASSERT(ev.type == GRPC_QUEUE_TIMEOUT);
94 }
95
96 gpr_log(GPR_INFO, "Shutting down server");
97 grpc_server_shutdown_and_notify(server, cq, nullptr);
98 grpc_server_cancel_all_calls(server);
99 grpc_completion_queue_shutdown(cq);
100
101 const gpr_timespec cq_deadline = grpc_timeout_seconds_to_deadline(60);
102 grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, nullptr);
103 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
104
105 grpc_server_destroy(server);
106 grpc_completion_queue_destroy(cq);
107 grpc_server_credentials_release(ssl_creds);
108 grpc_slice_unref(cert_slice);
109 grpc_slice_unref(key_slice);
110 grpc_slice_unref(ca_slice);
111 }
112
113 // This test launches a minimal TLS grpc server on a separate thread and then
114 // establishes a TLS handshake via the core library to the server. The client
115 // uses the supplied verify options.
116 static bool verify_peer_options_test(verify_peer_options* verify_options) {
117 bool success = true;
118
119 grpc_init();
120 int port = grpc_pick_unused_port_or_die();
121 gpr_event_init(&client_handshake_complete);
122
123 // Load key pair and establish client SSL credentials.
124 // NOTE: we intentionally load the credential files before starting
125 // the server thread because grpc_load_file can experience trouble
126 // when two threads attempt to load the same file concurrently
127 // and server thread also reads the same files as soon as it starts.
128 // See https://github.com/grpc/grpc/issues/23503 for details.
129 grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
130 grpc_slice ca_slice, cert_slice, key_slice;
131 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
132 grpc_load_file(SSL_CA_PATH, 1, &ca_slice)));
133 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
134 grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
135 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
136 grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
137 const char* ca_cert =
138 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
139 pem_key_cert_pair.private_key =
140 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
141 pem_key_cert_pair.cert_chain =
142 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
143 grpc_channel_credentials* ssl_creds = grpc_ssl_credentials_create(
144 ca_cert, &pem_key_cert_pair, verify_options, nullptr);
145
146 // Launch the gRPC server thread.
147 bool ok;
148 grpc_core::Thread thd("grpc_client_ssl_test", server_thread, &port, &ok);
149 GPR_ASSERT(ok);
150 thd.Start();
151
152 // Establish a channel pointing at the TLS server. Since the gRPC runtime is
153 // lazy, this won't necessarily establish a connection yet.
154 std::string target = absl::StrCat("127.0.0.1:", port);
155 grpc_arg ssl_name_override = {
156 GRPC_ARG_STRING,
157 const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
158 {const_cast<char*>("foo.test.google.fr")}};
159 grpc_channel_args grpc_args;
160 grpc_args.num_args = 1;
161 grpc_args.args = &ssl_name_override;
162 grpc_channel* channel =
163 grpc_channel_create(target.c_str(), ssl_creds, &grpc_args);
164 GPR_ASSERT(channel);
165
166 // Initially the channel will be idle, the
167 // grpc_channel_check_connectivity_state triggers an attempt to connect.
168 GPR_ASSERT(grpc_channel_check_connectivity_state(
169 channel, 1 /* try_to_connect */) == GRPC_CHANNEL_IDLE);
170
171 // Wait a bounded number of times for the channel to be ready. When the
172 // channel is ready, the initial TLS handshake will have successfully
173 // completed. The total time spent on the client side (retries * deadline)
174 // should be greater than the server side time limit.
175 int retries = 10;
176 grpc_connectivity_state state = GRPC_CHANNEL_IDLE;
177 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
178
179 while (state != GRPC_CHANNEL_READY && retries-- > 0) {
180 grpc_channel_watch_connectivity_state(
181 channel, state, grpc_timeout_seconds_to_deadline(3), cq, nullptr);
182 gpr_timespec cq_deadline = grpc_timeout_seconds_to_deadline(5);
183 grpc_event ev = grpc_completion_queue_next(cq, cq_deadline, nullptr);
184 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
185 state =
186 grpc_channel_check_connectivity_state(channel, 0 /* try_to_connect */);
187 }
188 grpc_completion_queue_destroy(cq);
189 if (retries < 0) {
190 success = false;
191 }
192
193 grpc_channel_destroy(channel);
194 grpc_channel_credentials_release(ssl_creds);
195 grpc_slice_unref(cert_slice);
196 grpc_slice_unref(key_slice);
197 grpc_slice_unref(ca_slice);
198
199 // Now that the client is completely cleaned up, trigger the server to
200 // shutdown
201 gpr_event_set(&client_handshake_complete, &client_handshake_complete);
202 // Wait for the server to completely shutdown
203 thd.Join();
204
205 grpc_shutdown();
206
207 return success;
208 }
209
210 static int callback_return_value = 0;
211 static char callback_target_host[4096];
212 static char callback_target_pem[4096];
213 static void* callback_userdata = nullptr;
214 static void* destruct_userdata = nullptr;
215
216 static int verify_callback(const char* target_host, const char* target_pem,
217 void* userdata) {
218 if (target_host != nullptr) {
219 snprintf(callback_target_host, sizeof(callback_target_host), "%s",
220 target_host);
221 } else {
222 callback_target_host[0] = '\0';
223 }
224 if (target_pem != nullptr) {
225 snprintf(callback_target_pem, sizeof(callback_target_pem), "%s",
226 target_pem);
227 } else {
228 callback_target_pem[0] = '\0';
229 }
230 callback_userdata = userdata;
231 return callback_return_value;
232 }
233
234 static void verify_destruct(void* userdata) { destruct_userdata = userdata; }
235
236 int main(int argc, char* argv[]) {
237 grpc::testing::TestEnvironment env(&argc, argv);
238 grpc_init();
239
240 int userdata = 42;
241 verify_peer_options verify_options;
242
243 // Load the server's cert so that we can assert it gets passed to the callback
244 grpc_slice cert_slice;
245 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
246 grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
247 const char* server_cert =
248 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
249
250 // Running with all-null values should have no effect
251 verify_options.verify_peer_callback = nullptr;
252 verify_options.verify_peer_callback_userdata = nullptr;
253 verify_options.verify_peer_destruct = nullptr;
254 GPR_ASSERT(verify_peer_options_test(&verify_options));
255 GPR_ASSERT(strlen(callback_target_host) == 0);
256 GPR_ASSERT(strlen(callback_target_pem) == 0);
257 GPR_ASSERT(callback_userdata == nullptr);
258 GPR_ASSERT(destruct_userdata == nullptr);
259
260 // Running with the callbacks and verify we get the expected values
261 verify_options.verify_peer_callback = verify_callback;
262 verify_options.verify_peer_callback_userdata = static_cast<void*>(&userdata);
263 verify_options.verify_peer_destruct = verify_destruct;
264 GPR_ASSERT(verify_peer_options_test(&verify_options));
265 GPR_ASSERT(strcmp(callback_target_host, "foo.test.google.fr") == 0);
266 GPR_ASSERT(strcmp(callback_target_pem, server_cert) == 0);
267 GPR_ASSERT(callback_userdata == static_cast<void*>(&userdata));
268 GPR_ASSERT(destruct_userdata == static_cast<void*>(&userdata));
269
270 // If the callback returns non-zero, initializing the channel should fail.
271 callback_return_value = 1;
272 GPR_ASSERT(!verify_peer_options_test(&verify_options));
273
274 grpc_slice_unref(cert_slice);
275
276 grpc_shutdown();
277 return 0;
278 }
279
280 #else /* GRPC_POSIX_SOCKET_TCP */
281
282 int main(int argc, char** argv) { return 1; }
283
284 #endif /* GRPC_POSIX_SOCKET_TCP */