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 <openssl/err.h>
26 #include <openssl/ssl.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/socket.h>
30 #include <unistd.h>
31
32 #include <string>
33
34 #include "absl/strings/str_cat.h"
35
36 #include <grpc/grpc.h>
37 #include <grpc/grpc_security.h>
38 #include <grpc/support/alloc.h>
39 #include <grpc/support/log.h>
40
41 #include "src/core/lib/gprpp/thd.h"
42 #include "src/core/lib/iomgr/load_file.h"
43 #include "test/core/util/port.h"
44 #include "test/core/util/test_config.h"
45
46 #define SSL_CERT_PATH "src/core/tsi/test_creds/server1.pem"
47 #define SSL_KEY_PATH "src/core/tsi/test_creds/server1.key"
48 #define SSL_CA_PATH "src/core/tsi/test_creds/ca.pem"
49
50 // Simple gRPC server. This listens until client_handshake_complete occurs.
51 static gpr_event client_handshake_complete;
52
server_thread(void * arg)53 static void server_thread(void* arg) {
54 const int port = *static_cast<int*>(arg);
55
56 // Load key pair and establish server SSL credentials.
57 grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
58 grpc_slice ca_slice, cert_slice, key_slice;
59 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
60 grpc_load_file(SSL_CA_PATH, 1, &ca_slice)));
61 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
62 grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
63 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
64 grpc_load_file(SSL_KEY_PATH, 1, &key_slice)));
65 const char* ca_cert =
66 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
67 pem_key_cert_pair.private_key =
68 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
69 pem_key_cert_pair.cert_chain =
70 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
71 grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
72 ca_cert, &pem_key_cert_pair, 1, 0, nullptr);
73
74 // Start server listening on local port.
75 std::string addr = absl::StrCat("127.0.0.1:", port);
76 grpc_server* server = grpc_server_create(nullptr, nullptr);
77 GPR_ASSERT(
78 grpc_server_add_secure_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.
verify_peer_options_test(verify_peer_options * 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 = grpc_secure_channel_create(ssl_creds, target.c_str(),
163 &grpc_args, nullptr);
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
verify_callback(const char * target_host,const char * target_pem,void * userdata)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
verify_destruct(void * userdata)234 static void verify_destruct(void* userdata) { destruct_userdata = userdata; }
235
main(int argc,char * argv[])236 int main(int argc, char* argv[]) {
237 grpc::testing::TestEnvironment env(argc, argv);
238
239 int userdata = 42;
240 verify_peer_options verify_options;
241
242 // Load the server's cert so that we can assert it gets passed to the callback
243 grpc_slice cert_slice;
244 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
245 grpc_load_file(SSL_CERT_PATH, 1, &cert_slice)));
246 const char* server_cert =
247 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
248
249 // Running with all-null values should have no effect
250 verify_options.verify_peer_callback = nullptr;
251 verify_options.verify_peer_callback_userdata = nullptr;
252 verify_options.verify_peer_destruct = nullptr;
253 GPR_ASSERT(verify_peer_options_test(&verify_options));
254 GPR_ASSERT(strlen(callback_target_host) == 0);
255 GPR_ASSERT(strlen(callback_target_pem) == 0);
256 GPR_ASSERT(callback_userdata == nullptr);
257 GPR_ASSERT(destruct_userdata == nullptr);
258
259 // Running with the callbacks and verify we get the expected values
260 verify_options.verify_peer_callback = verify_callback;
261 verify_options.verify_peer_callback_userdata = static_cast<void*>(&userdata);
262 verify_options.verify_peer_destruct = verify_destruct;
263 GPR_ASSERT(verify_peer_options_test(&verify_options));
264 GPR_ASSERT(strcmp(callback_target_host, "foo.test.google.fr") == 0);
265 GPR_ASSERT(strcmp(callback_target_pem, server_cert) == 0);
266 GPR_ASSERT(callback_userdata == static_cast<void*>(&userdata));
267 GPR_ASSERT(destruct_userdata == static_cast<void*>(&userdata));
268
269 // If the callback returns non-zero, initializing the channel should fail.
270 callback_return_value = 1;
271 GPR_ASSERT(!verify_peer_options_test(&verify_options));
272
273 grpc_slice_unref(cert_slice);
274
275 return 0;
276 }
277
278 #else /* GRPC_POSIX_SOCKET_TCP */
279
main(int argc,char ** argv)280 int main(int argc, char** argv) { return 1; }
281
282 #endif /* GRPC_POSIX_SOCKET_TCP */
283