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