1 /*
2 *
3 * Copyright 2015 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 "test/core/end2end/end2end_tests.h"
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpcpp/support/string_ref.h>
27
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/gpr/string.h"
30 #include "src/core/lib/gpr/tmpfile.h"
31 #include "src/core/lib/gprpp/host_port.h"
32 #include "src/core/lib/security/credentials/credentials.h"
33 #include "src/core/lib/security/security_connector/ssl_utils_config.h"
34 #include "test/core/end2end/cq_verifier.h"
35 #include "test/core/end2end/data/ssl_test_data.h"
36 #include "test/core/util/port.h"
37 #include "test/core/util/test_config.h"
38
39 #include <gtest/gtest.h>
40
41 extern "C" {
42 #include <openssl/crypto.h>
43 }
44
45 static std::string test_server1_key_id;
46
47 namespace grpc {
48 namespace testing {
49
50 struct fullstack_secure_fixture_data {
51 std::string localaddr;
52 };
53
chttp2_create_fixture_secure_fullstack(grpc_channel_args *,grpc_channel_args *)54 static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
55 grpc_channel_args* /*client_args*/, grpc_channel_args* /*server_args*/) {
56 grpc_end2end_test_fixture f;
57 int port = grpc_pick_unused_port_or_die();
58 fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data();
59 memset(&f, 0, sizeof(f));
60
61 ffd->localaddr = grpc_core::JoinHostPort("localhost", port);
62
63 f.fixture_data = ffd;
64 f.cq = grpc_completion_queue_create_for_next(nullptr);
65 return f;
66 }
67
process_auth_failure(void * state,grpc_auth_context *,const grpc_metadata *,size_t,grpc_process_auth_metadata_done_cb cb,void * user_data)68 static void process_auth_failure(void* state, grpc_auth_context* /*ctx*/,
69 const grpc_metadata* /*md*/,
70 size_t /*md_count*/,
71 grpc_process_auth_metadata_done_cb cb,
72 void* user_data) {
73 GPR_ASSERT(state == nullptr);
74 cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
75 }
76
chttp2_init_client_secure_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * client_args,grpc_channel_credentials * creds)77 static void chttp2_init_client_secure_fullstack(
78 grpc_end2end_test_fixture* f, grpc_channel_args* client_args,
79 grpc_channel_credentials* creds) {
80 fullstack_secure_fixture_data* ffd =
81 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
82 f->client = grpc_secure_channel_create(creds, ffd->localaddr.c_str(),
83 client_args, nullptr);
84 GPR_ASSERT(f->client != nullptr);
85 grpc_channel_credentials_release(creds);
86 }
87
chttp2_init_server_secure_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * server_args,grpc_server_credentials * server_creds)88 static void chttp2_init_server_secure_fullstack(
89 grpc_end2end_test_fixture* f, grpc_channel_args* server_args,
90 grpc_server_credentials* server_creds) {
91 fullstack_secure_fixture_data* ffd =
92 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
93 if (f->server) {
94 grpc_server_destroy(f->server);
95 }
96 f->server = grpc_server_create(server_args, nullptr);
97 grpc_server_register_completion_queue(f->server, f->cq, nullptr);
98 GPR_ASSERT(grpc_server_add_secure_http2_port(
99 f->server, ffd->localaddr.c_str(), server_creds));
100 grpc_server_credentials_release(server_creds);
101 grpc_server_start(f->server);
102 }
103
chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture * f)104 void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture* f) {
105 fullstack_secure_fixture_data* ffd =
106 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
107 delete ffd;
108 }
109
fail_server_auth_check(grpc_channel_args * server_args)110 static int fail_server_auth_check(grpc_channel_args* server_args) {
111 size_t i;
112 if (server_args == nullptr) return 0;
113 for (i = 0; i < server_args->num_args; i++) {
114 if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
115 0) {
116 return 1;
117 }
118 }
119 return 0;
120 }
121
122 #define SERVER_INIT_NAME(REQUEST_TYPE) \
123 chttp2_init_server_simple_ssl_secure_fullstack_##REQUEST_TYPE
124
125 #define SERVER_INIT(REQUEST_TYPE) \
126 static void SERVER_INIT_NAME(REQUEST_TYPE)( \
127 grpc_end2end_test_fixture * f, grpc_channel_args * server_args) { \
128 grpc_ssl_pem_key_cert_pair pem_cert_key_pair; \
129 if (!test_server1_key_id.empty()) { \
130 pem_cert_key_pair.private_key = test_server1_key_id.c_str(); \
131 pem_cert_key_pair.cert_chain = test_server1_cert; \
132 } else { \
133 pem_cert_key_pair.private_key = test_server1_key; \
134 pem_cert_key_pair.cert_chain = test_server1_cert; \
135 } \
136 grpc_server_credentials* ssl_creds = \
137 grpc_ssl_server_credentials_create_ex( \
138 test_root_cert, &pem_cert_key_pair, 1, REQUEST_TYPE, NULL); \
139 if (fail_server_auth_check(server_args)) { \
140 grpc_auth_metadata_processor processor = {process_auth_failure, NULL, \
141 NULL}; \
142 grpc_server_credentials_set_auth_metadata_processor(ssl_creds, \
143 processor); \
144 } \
145 chttp2_init_server_secure_fullstack(f, server_args, ssl_creds); \
146 }
147
148 SERVER_INIT(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE)
149 SERVER_INIT(GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_BUT_DONT_VERIFY)
150 SERVER_INIT(GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY)
151 SERVER_INIT(GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_BUT_DONT_VERIFY)
152 SERVER_INIT(GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY)
153
154 #define CLIENT_INIT_NAME(cert_type) \
155 chttp2_init_client_simple_ssl_secure_fullstack_##cert_type
156
157 typedef enum { NONE, SELF_SIGNED, SIGNED, BAD_CERT_PAIR } certtype;
158
159 #define CLIENT_INIT(cert_type) \
160 static void CLIENT_INIT_NAME(cert_type)(grpc_end2end_test_fixture * f, \
161 grpc_channel_args * client_args) { \
162 grpc_channel_credentials* ssl_creds = NULL; \
163 grpc_ssl_pem_key_cert_pair self_signed_client_key_cert_pair = { \
164 test_self_signed_client_key, test_self_signed_client_cert}; \
165 grpc_ssl_pem_key_cert_pair signed_client_key_cert_pair = { \
166 test_signed_client_key, test_signed_client_cert}; \
167 grpc_ssl_pem_key_cert_pair bad_client_key_cert_pair = { \
168 test_self_signed_client_key, test_signed_client_cert}; \
169 grpc_ssl_pem_key_cert_pair* key_cert_pair = NULL; \
170 switch (cert_type) { \
171 case SELF_SIGNED: \
172 key_cert_pair = &self_signed_client_key_cert_pair; \
173 break; \
174 case SIGNED: \
175 key_cert_pair = &signed_client_key_cert_pair; \
176 break; \
177 case BAD_CERT_PAIR: \
178 key_cert_pair = &bad_client_key_cert_pair; \
179 break; \
180 default: \
181 break; \
182 } \
183 ssl_creds = grpc_ssl_credentials_create(test_root_cert, key_cert_pair, \
184 NULL, NULL); \
185 grpc_arg ssl_name_override = { \
186 GRPC_ARG_STRING, \
187 const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG), \
188 {const_cast<char*>("foo.test.google.fr")}}; \
189 grpc_channel_args* new_client_args = \
190 grpc_channel_args_copy_and_add(client_args, &ssl_name_override, 1); \
191 chttp2_init_client_secure_fullstack(f, new_client_args, ssl_creds); \
192 { \
193 grpc_core::ExecCtx exec_ctx; \
194 grpc_channel_args_destroy(new_client_args); \
195 } \
196 }
197
198 CLIENT_INIT(NONE)
199 CLIENT_INIT(SELF_SIGNED)
200 CLIENT_INIT(SIGNED)
201 CLIENT_INIT(BAD_CERT_PAIR)
202
203 #define TEST_NAME(enum_name, cert_type, result) \
204 "chttp2/ssl_" #enum_name "_" #cert_type "_" #result "_"
205
206 typedef enum { SUCCESS, FAIL } test_result;
207
208 #define SSL_TEST(request_type, cert_type, result) \
209 { \
210 {TEST_NAME(request_type, cert_type, result), \
211 FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION | \
212 FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS | \
213 FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL, \
214 "foo.test.google.fr", \
215 chttp2_create_fixture_secure_fullstack, \
216 CLIENT_INIT_NAME(cert_type), \
217 SERVER_INIT_NAME(request_type), \
218 chttp2_tear_down_secure_fullstack}, \
219 result \
220 }
221
222 /* All test configurations */
223 typedef struct grpc_end2end_test_config_wrapper {
224 grpc_end2end_test_config config;
225 test_result result;
226 } grpc_end2end_test_config_wrapper;
227
228 static grpc_end2end_test_config_wrapper configs[] = {
229 SSL_TEST(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE, NONE, SUCCESS),
230 SSL_TEST(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE, SELF_SIGNED, SUCCESS),
231 SSL_TEST(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE, SIGNED, SUCCESS),
232 SSL_TEST(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE, BAD_CERT_PAIR, FAIL),
233
234 SSL_TEST(GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_BUT_DONT_VERIFY, NONE,
235 SUCCESS),
236 SSL_TEST(GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_BUT_DONT_VERIFY, SELF_SIGNED,
237 SUCCESS),
238 SSL_TEST(GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_BUT_DONT_VERIFY, SIGNED,
239 SUCCESS),
240 SSL_TEST(GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_BUT_DONT_VERIFY, BAD_CERT_PAIR,
241 FAIL),
242
243 SSL_TEST(GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY, NONE, SUCCESS),
244 SSL_TEST(GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY, SELF_SIGNED, FAIL),
245 SSL_TEST(GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY, SIGNED, SUCCESS),
246 SSL_TEST(GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY, BAD_CERT_PAIR,
247 FAIL),
248
249 SSL_TEST(GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_BUT_DONT_VERIFY,
250 NONE, FAIL),
251 SSL_TEST(GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_BUT_DONT_VERIFY,
252 SELF_SIGNED, SUCCESS),
253 SSL_TEST(GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_BUT_DONT_VERIFY,
254 SIGNED, SUCCESS),
255 SSL_TEST(GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_BUT_DONT_VERIFY,
256 BAD_CERT_PAIR, FAIL),
257
258 SSL_TEST(GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY, NONE,
259 FAIL),
260 SSL_TEST(GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY,
261 SELF_SIGNED, FAIL),
262 SSL_TEST(GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY, SIGNED,
263 SUCCESS),
264 SSL_TEST(GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY,
265 BAD_CERT_PAIR, FAIL),
266 };
267
tag(intptr_t t)268 static void* tag(intptr_t t) { return (void*)t; }
269
n_seconds_time(int n)270 static gpr_timespec n_seconds_time(int n) {
271 return grpc_timeout_seconds_to_deadline(n);
272 }
273
five_seconds_time(void)274 static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
275
drain_cq(grpc_completion_queue * cq)276 static void drain_cq(grpc_completion_queue* cq) {
277 grpc_event ev;
278 do {
279 ev = grpc_completion_queue_next(cq, five_seconds_time(), nullptr);
280 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
281 }
282
283 // Shuts down the server.
284 // Side effect - Also shuts down and drains the completion queue.
shutdown_server(grpc_end2end_test_fixture * f)285 static void shutdown_server(grpc_end2end_test_fixture* f) {
286 if (!f->server) return;
287 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
288 grpc_completion_queue_shutdown(f->cq);
289 drain_cq(f->cq);
290 grpc_server_destroy(f->server);
291 f->server = nullptr;
292 }
293
shutdown_client(grpc_end2end_test_fixture * f)294 static void shutdown_client(grpc_end2end_test_fixture* f) {
295 if (!f->client) return;
296 grpc_channel_destroy(f->client);
297 f->client = nullptr;
298 }
299
end_test(grpc_end2end_test_fixture * f)300 static void end_test(grpc_end2end_test_fixture* f) {
301 shutdown_client(f);
302 shutdown_server(f);
303 grpc_completion_queue_destroy(f->cq);
304 }
305
simple_request_body(grpc_end2end_test_fixture f,test_result expected_result)306 static void simple_request_body(grpc_end2end_test_fixture f,
307 test_result expected_result) {
308 grpc_call* c;
309 gpr_timespec deadline = five_seconds_time();
310 cq_verifier* cqv = cq_verifier_create(f.cq);
311 grpc_op ops[6];
312 grpc_op* op;
313 grpc_call_error error;
314
315 grpc_slice host = grpc_slice_from_static_string("foo.test.google.fr:1234");
316 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
317 grpc_slice_from_static_string("/foo"), &host,
318 deadline, nullptr);
319 GPR_ASSERT(c);
320
321 memset(ops, 0, sizeof(ops));
322 op = ops;
323 op->op = GRPC_OP_SEND_INITIAL_METADATA;
324 op->data.send_initial_metadata.count = 0;
325 op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY;
326 op->reserved = nullptr;
327 op++;
328 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
329 nullptr);
330 GPR_ASSERT(GRPC_CALL_OK == error);
331
332 CQ_EXPECT_COMPLETION(cqv, tag(1), expected_result == SUCCESS);
333 cq_verify(cqv);
334
335 grpc_call_unref(c);
336 cq_verifier_destroy(cqv);
337 }
338
339 class H2SslCertTest
340 : public ::testing::TestWithParam<grpc_end2end_test_config_wrapper> {
341 protected:
H2SslCertTest()342 H2SslCertTest() {
343 gpr_log(GPR_INFO, "SSL_CERT_tests/%s", GetParam().config.name);
344 }
SetUp()345 void SetUp() override {
346 fixture_ = GetParam().config.create_fixture(nullptr, nullptr);
347 GetParam().config.init_server(&fixture_, nullptr);
348 GetParam().config.init_client(&fixture_, nullptr);
349 }
TearDown()350 void TearDown() override {
351 end_test(&fixture_);
352 GetParam().config.tear_down_data(&fixture_);
353 }
354
355 grpc_end2end_test_fixture fixture_;
356 };
357
TEST_P(H2SslCertTest,SimpleRequestBody)358 TEST_P(H2SslCertTest, SimpleRequestBody) {
359 simple_request_body(fixture_, GetParam().result);
360 }
361
362 #ifndef OPENSSL_IS_BORINGSSL
363 #if GPR_LINUX
TEST_P(H2SslCertTest,SimpleRequestBodyUseEngine)364 TEST_P(H2SslCertTest, SimpleRequestBodyUseEngine) {
365 test_server1_key_id.clear();
366 test_server1_key_id.append("engine:libengine_passthrough:");
367 test_server1_key_id.append(test_server1_key);
368 simple_request_body(fixture_, GetParam().result);
369 }
370 #endif
371 #endif
372
373 INSTANTIATE_TEST_SUITE_P(H2SslCert, H2SslCertTest,
374 ::testing::ValuesIn(configs));
375
376 } // namespace testing
377 } // namespace grpc
378
main(int argc,char ** argv)379 int main(int argc, char** argv) {
380 FILE* roots_file;
381 size_t roots_size = strlen(test_root_cert);
382 char* roots_filename;
383
384 grpc::testing::TestEnvironment env(argc, argv);
385 /* Set the SSL roots env var. */
386 roots_file =
387 gpr_tmpfile("chttp2_simple_ssl_cert_fullstack_test", &roots_filename);
388 GPR_ASSERT(roots_filename != nullptr);
389 GPR_ASSERT(roots_file != nullptr);
390 GPR_ASSERT(fwrite(test_root_cert, 1, roots_size, roots_file) == roots_size);
391 fclose(roots_file);
392 GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, roots_filename);
393
394 grpc_init();
395 ::testing::InitGoogleTest(&argc, argv);
396 int ret = RUN_ALL_TESTS();
397 grpc_shutdown();
398
399 /* Cleanup. */
400 remove(roots_filename);
401 gpr_free(roots_filename);
402
403 return ret;
404 }
405