Debian Codegraph @

test/core/tsi/ssl_transport_security_test.cc

raw
1 /*
2 *
3 * Copyright 2017 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/tsi/ssl_transport_security.h"
20
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include <gtest/gtest.h>
26 #include <openssl/crypto.h>
27 #include <openssl/err.h>
28 #include <openssl/pem.h>
29
30 #include <grpc/grpc.h>
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/string_util.h>
34
35 #include "src/core/lib/gprpp/memory.h"
36 #include "src/core/lib/iomgr/load_file.h"
37 #include "src/core/lib/security/security_connector/security_connector.h"
38 #include "src/core/tsi/transport_security.h"
39 #include "src/core/tsi/transport_security_interface.h"
40 #include "test/core/tsi/transport_security_test_lib.h"
41 #include "test/core/util/build.h"
42 #include "test/core/util/test_config.h"
43
44 #define SSL_TSI_TEST_ALPN1 "foo"
45 #define SSL_TSI_TEST_ALPN2 "toto"
46 #define SSL_TSI_TEST_ALPN3 "baz"
47 #define SSL_TSI_TEST_ALPN_NUM 2
48 #define SSL_TSI_TEST_SERVER_KEY_CERT_PAIRS_NUM 2
49 #define SSL_TSI_TEST_BAD_SERVER_KEY_CERT_PAIRS_NUM 1
50 #define SSL_TSI_TEST_CREDENTIALS_DIR "src/core/tsi/test_creds/"
51 #define SSL_TSI_TEST_WRONG_SNI "test.google.cn"
52
53 // OpenSSL 1.1 uses AES256 for encryption session ticket by default so specify
54 // different STEK size.
55 #if OPENSSL_VERSION_NUMBER >= 0x10100000 && !defined(OPENSSL_IS_BORINGSSL)
56 const size_t kSessionTicketEncryptionKeySize = 80;
57 #else
58 const size_t kSessionTicketEncryptionKeySize = 48;
59 #endif
60
61 // Indicates the TLS version used for the test.
62 static tsi_tls_version test_tls_version = tsi_tls_version::TSI_TLS1_3;
63
64 typedef enum AlpnMode {
65 NO_ALPN,
66 ALPN_CLIENT_NO_SERVER,
67 ALPN_SERVER_NO_CLIENT,
68 ALPN_CLIENT_SERVER_OK,
69 ALPN_CLIENT_SERVER_MISMATCH
70 } AlpnMode;
71
72 typedef struct ssl_alpn_lib {
73 AlpnMode alpn_mode;
74 const char** server_alpn_protocols;
75 const char** client_alpn_protocols;
76 uint16_t num_server_alpn_protocols;
77 uint16_t num_client_alpn_protocols;
78 } ssl_alpn_lib;
79
80 typedef struct ssl_key_cert_lib {
81 bool use_bad_server_cert;
82 bool use_bad_client_cert;
83 bool use_root_store;
84 char* root_cert;
85 tsi_ssl_root_certs_store* root_store;
86 tsi_ssl_pem_key_cert_pair* server_pem_key_cert_pairs;
87 tsi_ssl_pem_key_cert_pair* bad_server_pem_key_cert_pairs;
88 tsi_ssl_pem_key_cert_pair client_pem_key_cert_pair;
89 tsi_ssl_pem_key_cert_pair bad_client_pem_key_cert_pair;
90 uint16_t server_num_key_cert_pairs;
91 uint16_t bad_server_num_key_cert_pairs;
92 } ssl_key_cert_lib;
93
94 typedef struct ssl_tsi_test_fixture {
95 tsi_test_fixture base;
96 ssl_key_cert_lib* key_cert_lib;
97 ssl_alpn_lib* alpn_lib;
98 bool force_client_auth;
99 char* server_name_indication;
100 tsi_ssl_session_cache* session_cache;
101 bool session_reused;
102 const char* session_ticket_key;
103 size_t session_ticket_key_size;
104 size_t network_bio_buf_size;
105 size_t ssl_bio_buf_size;
106 tsi_ssl_server_handshaker_factory* server_handshaker_factory;
107 tsi_ssl_client_handshaker_factory* client_handshaker_factory;
108 } ssl_tsi_test_fixture;
109
110 static void ssl_test_setup_handshakers(tsi_test_fixture* fixture) {
111 ssl_tsi_test_fixture* ssl_fixture =
112 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
113 ASSERT_NE(ssl_fixture, nullptr);
114 ASSERT_NE(ssl_fixture->key_cert_lib, nullptr);
115 ASSERT_NE(ssl_fixture->alpn_lib, nullptr);
116 ssl_key_cert_lib* key_cert_lib = ssl_fixture->key_cert_lib;
117 ssl_alpn_lib* alpn_lib = ssl_fixture->alpn_lib;
118 /* Create client handshaker factory. */
119 tsi_ssl_client_handshaker_options client_options;
120 client_options.pem_root_certs = key_cert_lib->root_cert;
121 if (ssl_fixture->force_client_auth) {
122 client_options.pem_key_cert_pair =
123 key_cert_lib->use_bad_client_cert
124 ? &key_cert_lib->bad_client_pem_key_cert_pair
125 : &key_cert_lib->client_pem_key_cert_pair;
126 }
127 if (alpn_lib->alpn_mode == ALPN_CLIENT_NO_SERVER ||
128 alpn_lib->alpn_mode == ALPN_CLIENT_SERVER_OK ||
129 alpn_lib->alpn_mode == ALPN_CLIENT_SERVER_MISMATCH) {
130 client_options.alpn_protocols = alpn_lib->client_alpn_protocols;
131 client_options.num_alpn_protocols = alpn_lib->num_client_alpn_protocols;
132 }
133 client_options.root_store =
134 key_cert_lib->use_root_store ? key_cert_lib->root_store : nullptr;
135 if (ssl_fixture->session_cache != nullptr) {
136 client_options.session_cache = ssl_fixture->session_cache;
137 }
138 client_options.min_tls_version = test_tls_version;
139 client_options.max_tls_version = test_tls_version;
140 ASSERT_EQ(tsi_create_ssl_client_handshaker_factory_with_options(
141 &client_options, &ssl_fixture->client_handshaker_factory),
142 TSI_OK);
143 /* Create server handshaker factory. */
144 tsi_ssl_server_handshaker_options server_options;
145 if (alpn_lib->alpn_mode == ALPN_SERVER_NO_CLIENT ||
146 alpn_lib->alpn_mode == ALPN_CLIENT_SERVER_OK ||
147 alpn_lib->alpn_mode == ALPN_CLIENT_SERVER_MISMATCH) {
148 server_options.alpn_protocols = alpn_lib->server_alpn_protocols;
149 server_options.num_alpn_protocols = alpn_lib->num_server_alpn_protocols;
150 if (alpn_lib->alpn_mode == ALPN_CLIENT_SERVER_MISMATCH) {
151 server_options.num_alpn_protocols--;
152 }
153 }
154 server_options.pem_key_cert_pairs =
155 key_cert_lib->use_bad_server_cert
156 ? key_cert_lib->bad_server_pem_key_cert_pairs
157 : key_cert_lib->server_pem_key_cert_pairs;
158 server_options.num_key_cert_pairs =
159 key_cert_lib->use_bad_server_cert
160 ? key_cert_lib->bad_server_num_key_cert_pairs
161 : key_cert_lib->server_num_key_cert_pairs;
162 server_options.pem_client_root_certs = key_cert_lib->root_cert;
163 if (ssl_fixture->force_client_auth) {
164 server_options.client_certificate_request =
165 TSI_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY;
166 } else {
167 server_options.client_certificate_request =
168 TSI_DONT_REQUEST_CLIENT_CERTIFICATE;
169 }
170 server_options.session_ticket_key = ssl_fixture->session_ticket_key;
171 server_options.session_ticket_key_size = ssl_fixture->session_ticket_key_size;
172 server_options.min_tls_version = test_tls_version;
173 server_options.max_tls_version = test_tls_version;
174 ASSERT_EQ(tsi_create_ssl_server_handshaker_factory_with_options(
175 &server_options, &ssl_fixture->server_handshaker_factory),
176 TSI_OK);
177 /* Create server and client handshakers. */
178 ASSERT_EQ(
179 tsi_ssl_client_handshaker_factory_create_handshaker(
180 ssl_fixture->client_handshaker_factory,
181 ssl_fixture->server_name_indication,
182 ssl_fixture->network_bio_buf_size, ssl_fixture->ssl_bio_buf_size,
183 &ssl_fixture->base.client_handshaker),
184 TSI_OK);
185 ASSERT_EQ(
186 tsi_ssl_server_handshaker_factory_create_handshaker(
187 ssl_fixture->server_handshaker_factory,
188 ssl_fixture->network_bio_buf_size, ssl_fixture->ssl_bio_buf_size,
189 &ssl_fixture->base.server_handshaker),
190 TSI_OK);
191 }
192
193 static void check_alpn(ssl_tsi_test_fixture* ssl_fixture,
194 const tsi_peer* peer) {
195 ASSERT_NE(ssl_fixture, nullptr);
196 ASSERT_NE(ssl_fixture->alpn_lib, nullptr);
197 ssl_alpn_lib* alpn_lib = ssl_fixture->alpn_lib;
198 const tsi_peer_property* alpn_property =
199 tsi_peer_get_property_by_name(peer, TSI_SSL_ALPN_SELECTED_PROTOCOL);
200 if (alpn_lib->alpn_mode != ALPN_CLIENT_SERVER_OK) {
201 ASSERT_EQ(alpn_property, nullptr);
202 } else {
203 ASSERT_NE(alpn_property, nullptr);
204 const char* expected_match = "baz";
205 ASSERT_EQ(memcmp(alpn_property->value.data, expected_match,
206 alpn_property->value.length),
207 0);
208 }
209 }
210
211 static void check_security_level(const tsi_peer* peer) {
212 const tsi_peer_property* security_level =
213 tsi_peer_get_property_by_name(peer, TSI_SECURITY_LEVEL_PEER_PROPERTY);
214 ASSERT_NE(security_level, nullptr);
215 const char* expected_match = "TSI_PRIVACY_AND_INTEGRITY";
216 ASSERT_EQ(memcmp(security_level->value.data, expected_match,
217 security_level->value.length),
218 0);
219 }
220
221 static const tsi_peer_property*
222 check_basic_authenticated_peer_and_get_common_name(const tsi_peer* peer) {
223 const tsi_peer_property* cert_type_property =
224 tsi_peer_get_property_by_name(peer, TSI_CERTIFICATE_TYPE_PEER_PROPERTY);
225 EXPECT_NE(cert_type_property, nullptr);
226 EXPECT_EQ(memcmp(cert_type_property->value.data, TSI_X509_CERTIFICATE_TYPE,
227 cert_type_property->value.length),
228 0);
229 const tsi_peer_property* property = tsi_peer_get_property_by_name(
230 peer, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY);
231 EXPECT_NE(property, nullptr);
232 return property;
233 }
234
235 static void check_session_reusage(ssl_tsi_test_fixture* ssl_fixture,
236 tsi_peer* peer) {
237 const tsi_peer_property* session_reused =
238 tsi_peer_get_property_by_name(peer, TSI_SSL_SESSION_REUSED_PEER_PROPERTY);
239 ASSERT_NE(session_reused, nullptr);
240 if (ssl_fixture->session_reused) {
241 ASSERT_EQ(strncmp(session_reused->value.data, "true",
242 session_reused->value.length),
243 0);
244 } else {
245 ASSERT_EQ(strncmp(session_reused->value.data, "false",
246 session_reused->value.length),
247 0);
248 }
249 }
250
251 void check_server0_peer(tsi_peer* peer) {
252 const tsi_peer_property* property =
253 check_basic_authenticated_peer_and_get_common_name(peer);
254 const char* expected_match = "*.test.google.com.au";
255 ASSERT_EQ(
256 memcmp(property->value.data, expected_match, property->value.length), 0);
257 ASSERT_EQ(tsi_peer_get_property_by_name(
258 peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY),
259 nullptr);
260 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "foo.test.google.com.au"), 1);
261 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "bar.test.google.com.au"), 1);
262 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "BAR.TEST.GOOGLE.COM.AU"), 1);
263 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "Bar.Test.Google.Com.Au"), 1);
264 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "bAr.TeST.gOOgle.cOm.AU"), 1);
265 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "bar.test.google.blah"), 0);
266 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "foo.bar.test.google.com.au"), 0);
267 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "test.google.com.au"), 0);
268 tsi_peer_destruct(peer);
269 }
270
271 static bool check_property(tsi_peer* peer, const char* property_name,
272 const char* property_value) {
273 for (size_t i = 0; i < peer->property_count; i++) {
274 const tsi_peer_property* prop = &peer->properties[i];
275 if (strcmp(prop->name, property_name) == 0) {
276 if (strlen(property_value) == prop->value.length &&
277 memcmp(prop->value.data, property_value, prop->value.length) == 0) {
278 return true;
279 }
280 }
281 }
282 return false;
283 }
284
285 void check_server1_peer(tsi_peer* peer) {
286 const tsi_peer_property* property =
287 check_basic_authenticated_peer_and_get_common_name(peer);
288 const char* expected_match = "*.test.google.com";
289 ASSERT_EQ(
290 memcmp(property->value.data, expected_match, property->value.length), 0);
291 ASSERT_EQ(
292 check_property(peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
293 "*.test.google.fr"),
294 1);
295 ASSERT_EQ(
296 check_property(peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
297 "waterzooi.test.google.be"),
298 1);
299 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "foo.test.google.fr"), 1);
300 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "bar.test.google.fr"), 1);
301 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "waterzooi.test.google.be"), 1);
302 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "foo.test.youtube.com"), 1);
303 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "bar.foo.test.google.com"), 0);
304 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "test.google.fr"), 0);
305 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "tartines.test.google.be"), 0);
306 ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "tartines.youtube.com"), 0);
307 tsi_peer_destruct(peer);
308 }
309
310 static void check_client_peer(ssl_tsi_test_fixture* ssl_fixture,
311 tsi_peer* peer) {
312 ASSERT_NE(ssl_fixture, nullptr);
313 ASSERT_NE(ssl_fixture->alpn_lib, nullptr);
314 ssl_alpn_lib* alpn_lib = ssl_fixture->alpn_lib;
315 if (!ssl_fixture->force_client_auth) {
316 ASSERT_EQ(peer->property_count,
317 (alpn_lib->alpn_mode == ALPN_CLIENT_SERVER_OK ? 3 : 2));
318 } else {
319 const tsi_peer_property* property =
320 check_basic_authenticated_peer_and_get_common_name(peer);
321 const char* expected_match = "testclient";
322 ASSERT_EQ(
323 memcmp(property->value.data, expected_match, property->value.length),
324 0);
325 }
326 tsi_peer_destruct(peer);
327 }
328
329 static void ssl_test_check_handshaker_peers(tsi_test_fixture* fixture) {
330 ssl_tsi_test_fixture* ssl_fixture =
331 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
332 ASSERT_NE(ssl_fixture, nullptr);
333 ASSERT_NE(ssl_fixture->key_cert_lib, nullptr);
334 ssl_key_cert_lib* key_cert_lib = ssl_fixture->key_cert_lib;
335 tsi_peer peer;
336 // In TLS 1.3, the client-side handshake succeeds even if the client sends a
337 // bad certificate. In such a case, the server would fail the TLS handshake
338 // and send an alert to the client as the first application data message. In
339 // TLS 1.2, the client-side handshake will fail if the client sends a bad
340 // certificate.
341 //
342 // For OpenSSL versions < 1.1, TLS 1.3 is not supported, so the client-side
343 // handshake should succeed precisely when the server-side handshake
344 // succeeds.
345 bool expect_server_success =
346 !(key_cert_lib->use_bad_server_cert ||
347 (key_cert_lib->use_bad_client_cert && ssl_fixture->force_client_auth));
348 #if OPENSSL_VERSION_NUMBER >= 0x10100000
349 bool expect_client_success = test_tls_version == tsi_tls_version::TSI_TLS1_2
350 ? expect_server_success
351 : !key_cert_lib->use_bad_server_cert;
352 #else
353 bool expect_client_success = expect_server_success;
354 #endif
355 if (expect_client_success) {
356 ASSERT_EQ(tsi_handshaker_result_extract_peer(
357 ssl_fixture->base.client_result, &peer),
358 TSI_OK);
359 check_session_reusage(ssl_fixture, &peer);
360 check_alpn(ssl_fixture, &peer);
361 check_security_level(&peer);
362 if (ssl_fixture->server_name_indication == nullptr ||
363 strcmp(ssl_fixture->server_name_indication, SSL_TSI_TEST_WRONG_SNI) ==
364 0) {
365 // Expect server to use default server0.pem.
366 check_server0_peer(&peer);
367 } else {
368 // Expect server to use server1.pem.
369 check_server1_peer(&peer);
370 }
371 } else {
372 ASSERT_EQ(ssl_fixture->base.client_result, nullptr);
373 }
374 if (expect_server_success) {
375 ASSERT_EQ(tsi_handshaker_result_extract_peer(
376 ssl_fixture->base.server_result, &peer),
377 TSI_OK);
378 check_session_reusage(ssl_fixture, &peer);
379 check_alpn(ssl_fixture, &peer);
380 check_security_level(&peer);
381 check_client_peer(ssl_fixture, &peer);
382 } else {
383 ASSERT_EQ(ssl_fixture->base.server_result, nullptr);
384 }
385 }
386
387 static void ssl_test_pem_key_cert_pair_destroy(tsi_ssl_pem_key_cert_pair kp) {
388 gpr_free(const_cast<char*>(kp.private_key));
389 gpr_free(const_cast<char*>(kp.cert_chain));
390 }
391
392 static void ssl_test_destruct(tsi_test_fixture* fixture) {
393 ssl_tsi_test_fixture* ssl_fixture =
394 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
395 if (ssl_fixture == nullptr) {
396 return;
397 }
398 /* Destroy ssl_alpn_lib. */
399 ssl_alpn_lib* alpn_lib = ssl_fixture->alpn_lib;
400 for (size_t i = 0; i < alpn_lib->num_server_alpn_protocols; i++) {
401 gpr_free(const_cast<char*>(alpn_lib->server_alpn_protocols[i]));
402 }
403 gpr_free(alpn_lib->server_alpn_protocols);
404 for (size_t i = 0; i < alpn_lib->num_client_alpn_protocols; i++) {
405 gpr_free(const_cast<char*>(alpn_lib->client_alpn_protocols[i]));
406 }
407 gpr_free(alpn_lib->client_alpn_protocols);
408 gpr_free(alpn_lib);
409 /* Destroy ssl_key_cert_lib. */
410 ssl_key_cert_lib* key_cert_lib = ssl_fixture->key_cert_lib;
411 for (size_t i = 0; i < key_cert_lib->server_num_key_cert_pairs; i++) {
412 ssl_test_pem_key_cert_pair_destroy(
413 key_cert_lib->server_pem_key_cert_pairs[i]);
414 }
415 gpr_free(key_cert_lib->server_pem_key_cert_pairs);
416 for (size_t i = 0; i < key_cert_lib->bad_server_num_key_cert_pairs; i++) {
417 ssl_test_pem_key_cert_pair_destroy(
418 key_cert_lib->bad_server_pem_key_cert_pairs[i]);
419 }
420 gpr_free(key_cert_lib->bad_server_pem_key_cert_pairs);
421 ssl_test_pem_key_cert_pair_destroy(key_cert_lib->client_pem_key_cert_pair);
422 ssl_test_pem_key_cert_pair_destroy(
423 key_cert_lib->bad_client_pem_key_cert_pair);
424 gpr_free(key_cert_lib->root_cert);
425 tsi_ssl_root_certs_store_destroy(key_cert_lib->root_store);
426 gpr_free(key_cert_lib);
427 if (ssl_fixture->session_cache != nullptr) {
428 tsi_ssl_session_cache_unref(ssl_fixture->session_cache);
429 }
430 /* Unreference others. */
431 tsi_ssl_server_handshaker_factory_unref(
432 ssl_fixture->server_handshaker_factory);
433 tsi_ssl_client_handshaker_factory_unref(
434 ssl_fixture->client_handshaker_factory);
435 gpr_free(ssl_fixture);
436 }
437
438 static const struct tsi_test_fixture_vtable vtable = {
439 ssl_test_setup_handshakers, ssl_test_check_handshaker_peers,
440 ssl_test_destruct};
441
442 static char* load_file(const char* dir_path, const char* file_name) {
443 char* file_path = static_cast<char*>(
444 gpr_zalloc(sizeof(char) * (strlen(dir_path) + strlen(file_name) + 1)));
445 memcpy(file_path, dir_path, strlen(dir_path));
446 memcpy(file_path + strlen(dir_path), file_name, strlen(file_name));
447 grpc_slice slice;
448 EXPECT_EQ(grpc_load_file(file_path, 1, &slice), absl::OkStatus());
449 char* data = grpc_slice_to_c_string(slice);
450 grpc_slice_unref(slice);
451 gpr_free(file_path);
452 return data;
453 }
454
455 static tsi_test_fixture* ssl_tsi_test_fixture_create() {
456 ssl_tsi_test_fixture* ssl_fixture = grpc_core::Zalloc<ssl_tsi_test_fixture>();
457 tsi_test_fixture_init(&ssl_fixture->base);
458 ssl_fixture->base.test_unused_bytes = true;
459 ssl_fixture->base.vtable = &vtable;
460 /* Create ssl_key_cert_lib. */
461 ssl_key_cert_lib* key_cert_lib = grpc_core::Zalloc<ssl_key_cert_lib>();
462 key_cert_lib->use_bad_server_cert = false;
463 key_cert_lib->use_bad_client_cert = false;
464 key_cert_lib->use_root_store = false;
465 key_cert_lib->server_num_key_cert_pairs =
466 SSL_TSI_TEST_SERVER_KEY_CERT_PAIRS_NUM;
467 key_cert_lib->bad_server_num_key_cert_pairs =
468 SSL_TSI_TEST_BAD_SERVER_KEY_CERT_PAIRS_NUM;
469 key_cert_lib->server_pem_key_cert_pairs =
470 static_cast<tsi_ssl_pem_key_cert_pair*>(
471 gpr_malloc(sizeof(tsi_ssl_pem_key_cert_pair) *
472 key_cert_lib->server_num_key_cert_pairs));
473 key_cert_lib->bad_server_pem_key_cert_pairs =
474 static_cast<tsi_ssl_pem_key_cert_pair*>(
475 gpr_malloc(sizeof(tsi_ssl_pem_key_cert_pair) *
476 key_cert_lib->bad_server_num_key_cert_pairs));
477 key_cert_lib->server_pem_key_cert_pairs[0].private_key =
478 load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server0.key");
479 key_cert_lib->server_pem_key_cert_pairs[0].cert_chain =
480 load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server0.pem");
481 key_cert_lib->server_pem_key_cert_pairs[1].private_key =
482 load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server1.key");
483 key_cert_lib->server_pem_key_cert_pairs[1].cert_chain =
484 load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server1.pem");
485 key_cert_lib->bad_server_pem_key_cert_pairs[0].private_key =
486 load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "badserver.key");
487 key_cert_lib->bad_server_pem_key_cert_pairs[0].cert_chain =
488 load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "badserver.pem");
489 key_cert_lib->client_pem_key_cert_pair.private_key =
490 load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "client.key");
491 key_cert_lib->client_pem_key_cert_pair.cert_chain =
492 load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "client.pem");
493 key_cert_lib->bad_client_pem_key_cert_pair.private_key =
494 load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "badclient.key");
495 key_cert_lib->bad_client_pem_key_cert_pair.cert_chain =
496 load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "badclient.pem");
497 key_cert_lib->root_cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "ca.pem");
498 key_cert_lib->root_store =
499 tsi_ssl_root_certs_store_create(key_cert_lib->root_cert);
500 EXPECT_NE(key_cert_lib->root_store, nullptr);
501 ssl_fixture->key_cert_lib = key_cert_lib;
502 /* Create ssl_alpn_lib. */
503 ssl_alpn_lib* alpn_lib = grpc_core::Zalloc<ssl_alpn_lib>();
504 alpn_lib->server_alpn_protocols = static_cast<const char**>(
505 gpr_zalloc(sizeof(char*) * SSL_TSI_TEST_ALPN_NUM));
506 alpn_lib->client_alpn_protocols = static_cast<const char**>(
507 gpr_zalloc(sizeof(char*) * SSL_TSI_TEST_ALPN_NUM));
508 alpn_lib->server_alpn_protocols[0] = gpr_strdup(SSL_TSI_TEST_ALPN1);
509 alpn_lib->server_alpn_protocols[1] = gpr_strdup(SSL_TSI_TEST_ALPN3);
510 alpn_lib->client_alpn_protocols[0] = gpr_strdup(SSL_TSI_TEST_ALPN2);
511 alpn_lib->client_alpn_protocols[1] = gpr_strdup(SSL_TSI_TEST_ALPN3);
512 alpn_lib->num_server_alpn_protocols = SSL_TSI_TEST_ALPN_NUM;
513 alpn_lib->num_client_alpn_protocols = SSL_TSI_TEST_ALPN_NUM;
514 alpn_lib->alpn_mode = NO_ALPN;
515 ssl_fixture->alpn_lib = alpn_lib;
516 ssl_fixture->base.vtable = &vtable;
517 ssl_fixture->server_name_indication = nullptr;
518 ssl_fixture->session_reused = false;
519 ssl_fixture->session_ticket_key = nullptr;
520 ssl_fixture->session_ticket_key_size = 0;
521 ssl_fixture->force_client_auth = false;
522 ssl_fixture->network_bio_buf_size = 0;
523 ssl_fixture->ssl_bio_buf_size = 0;
524 return &ssl_fixture->base;
525 }
526
527 void ssl_tsi_test_do_handshake_tiny_handshake_buffer() {
528 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake_tiny_handshake_buffer");
529 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
530 fixture->handshake_buffer_size = TSI_TEST_TINY_HANDSHAKE_BUFFER_SIZE;
531 // Handshake buffer is too small to hold both handshake messages and the
532 // unused bytes.
533 fixture->test_unused_bytes = false;
534 tsi_test_do_handshake(fixture);
535 tsi_test_fixture_destroy(fixture);
536 }
537
538 void ssl_tsi_test_do_handshake_small_handshake_buffer() {
539 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake_small_handshake_buffer");
540 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
541 fixture->handshake_buffer_size = TSI_TEST_SMALL_HANDSHAKE_BUFFER_SIZE;
542 tsi_test_do_handshake(fixture);
543 tsi_test_fixture_destroy(fixture);
544 }
545
546 void ssl_tsi_test_do_handshake() {
547 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake");
548 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
549 tsi_test_do_handshake(fixture);
550 tsi_test_fixture_destroy(fixture);
551 }
552
553 void ssl_tsi_test_do_handshake_with_root_store() {
554 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake_with_root_store");
555 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
556 ssl_tsi_test_fixture* ssl_fixture =
557 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
558 ssl_fixture->key_cert_lib->use_root_store = true;
559 tsi_test_do_handshake(fixture);
560 tsi_test_fixture_destroy(fixture);
561 }
562
563 void ssl_tsi_test_do_handshake_with_client_authentication() {
564 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake_with_client_authentication");
565 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
566 ssl_tsi_test_fixture* ssl_fixture =
567 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
568 ssl_fixture->force_client_auth = true;
569 tsi_test_do_handshake(fixture);
570 tsi_test_fixture_destroy(fixture);
571 }
572
573 void ssl_tsi_test_do_handshake_with_client_authentication_and_root_store() {
574 gpr_log(
575 GPR_INFO,
576 "ssl_tsi_test_do_handshake_with_client_authentication_and_root_store");
577 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
578 ssl_tsi_test_fixture* ssl_fixture =
579 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
580 ssl_fixture->force_client_auth = true;
581 ssl_fixture->key_cert_lib->use_root_store = true;
582 tsi_test_do_handshake(fixture);
583 tsi_test_fixture_destroy(fixture);
584 }
585
586 void ssl_tsi_test_do_handshake_with_server_name_indication_exact_domain() {
587 gpr_log(GPR_INFO,
588 "ssl_tsi_test_do_handshake_with_server_name_indication_exact_domain");
589 /* server1 cert contains "waterzooi.test.google.be" in SAN. */
590 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
591 ssl_tsi_test_fixture* ssl_fixture =
592 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
593 ssl_fixture->server_name_indication =
594 const_cast<char*>("waterzooi.test.google.be");
595 tsi_test_do_handshake(fixture);
596 tsi_test_fixture_destroy(fixture);
597 }
598
599 void ssl_tsi_test_do_handshake_with_server_name_indication_wild_star_domain() {
600 gpr_log(
601 GPR_INFO,
602 "ssl_tsi_test_do_handshake_with_server_name_indication_wild_star_domain");
603 /* server1 cert contains "*.test.google.fr" in SAN. */
604 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
605 ssl_tsi_test_fixture* ssl_fixture =
606 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
607 ssl_fixture->server_name_indication =
608 const_cast<char*>("juju.test.google.fr");
609 tsi_test_do_handshake(fixture);
610 tsi_test_fixture_destroy(fixture);
611 }
612
613 void ssl_tsi_test_do_handshake_with_wrong_server_name_indication() {
614 gpr_log(GPR_INFO,
615 "ssl_tsi_test_do_handshake_with_wrong_server_name_indication");
616 /* server certs do not contain "test.google.cn". */
617 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
618 ssl_tsi_test_fixture* ssl_fixture =
619 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
620 ssl_fixture->server_name_indication =
621 const_cast<char*>(SSL_TSI_TEST_WRONG_SNI);
622 tsi_test_do_handshake(fixture);
623 tsi_test_fixture_destroy(fixture);
624 }
625
626 void ssl_tsi_test_do_handshake_with_bad_server_cert() {
627 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake_with_bad_server_cert");
628 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
629 ssl_tsi_test_fixture* ssl_fixture =
630 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
631 ssl_fixture->key_cert_lib->use_bad_server_cert = true;
632 tsi_test_do_handshake(fixture);
633 tsi_test_fixture_destroy(fixture);
634 }
635
636 void ssl_tsi_test_do_handshake_with_bad_client_cert() {
637 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake_with_bad_client_cert");
638 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
639 ssl_tsi_test_fixture* ssl_fixture =
640 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
641 ssl_fixture->key_cert_lib->use_bad_client_cert = true;
642 ssl_fixture->force_client_auth = true;
643 tsi_test_do_handshake(fixture);
644 tsi_test_fixture_destroy(fixture);
645 }
646
647 void ssl_tsi_test_do_handshake_alpn_client_no_server() {
648 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake_alpn_client_no_server");
649 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
650 ssl_tsi_test_fixture* ssl_fixture =
651 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
652 ssl_fixture->alpn_lib->alpn_mode = ALPN_CLIENT_NO_SERVER;
653 tsi_test_do_handshake(fixture);
654 tsi_test_fixture_destroy(fixture);
655 }
656
657 void ssl_tsi_test_do_handshake_alpn_server_no_client() {
658 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake_alpn_server_no_client");
659 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
660 ssl_tsi_test_fixture* ssl_fixture =
661 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
662 ssl_fixture->alpn_lib->alpn_mode = ALPN_SERVER_NO_CLIENT;
663 tsi_test_do_handshake(fixture);
664 tsi_test_fixture_destroy(fixture);
665 }
666
667 void ssl_tsi_test_do_handshake_alpn_client_server_mismatch() {
668 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake_alpn_server_no_client");
669 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
670 ssl_tsi_test_fixture* ssl_fixture =
671 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
672 ssl_fixture->alpn_lib->alpn_mode = ALPN_CLIENT_SERVER_MISMATCH;
673 tsi_test_do_handshake(fixture);
674 tsi_test_fixture_destroy(fixture);
675 }
676
677 void ssl_tsi_test_do_handshake_alpn_client_server_ok() {
678 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake_alpn_client_server_ok");
679 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
680 ssl_tsi_test_fixture* ssl_fixture =
681 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
682 ssl_fixture->alpn_lib->alpn_mode = ALPN_CLIENT_SERVER_OK;
683 tsi_test_do_handshake(fixture);
684 tsi_test_fixture_destroy(fixture);
685 }
686
687 void ssl_tsi_test_do_round_trip_for_all_configs() {
688 gpr_log(GPR_INFO, "ssl_tsi_test_do_round_trip_for_all_configs");
689 unsigned int* bit_array = static_cast<unsigned int*>(
690 gpr_zalloc(sizeof(unsigned int) * TSI_TEST_NUM_OF_ARGUMENTS));
691 const unsigned int mask = 1U << (TSI_TEST_NUM_OF_ARGUMENTS - 1);
692 for (unsigned int val = 0; val < TSI_TEST_NUM_OF_COMBINATIONS; val++) {
693 unsigned int v = val;
694 for (unsigned int ind = 0; ind < TSI_TEST_NUM_OF_ARGUMENTS; ind++) {
695 bit_array[ind] = (v & mask) ? 1 : 0;
696 v <<= 1;
697 }
698 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
699 ssl_tsi_test_fixture* ssl_fixture =
700 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
701 tsi_test_frame_protector_config_destroy(ssl_fixture->base.config);
702 ssl_fixture->base.config = tsi_test_frame_protector_config_create(
703 bit_array[0], bit_array[1], bit_array[2], bit_array[3], bit_array[4],
704 bit_array[5], bit_array[6]);
705 tsi_test_do_round_trip(&ssl_fixture->base);
706 tsi_test_fixture_destroy(fixture);
707 }
708 gpr_free(bit_array);
709 }
710
711 void ssl_tsi_test_do_round_trip_with_error_on_stack() {
712 gpr_log(GPR_INFO, "ssl_tsi_test_do_round_trip_with_error_on_stack");
713 // Invoke an SSL function that causes an error, and ensure the error
714 // makes it to the stack.
715 ASSERT_FALSE(EC_KEY_new_by_curve_name(NID_rsa));
716 ASSERT_NE(ERR_peek_error(), 0);
717 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
718 tsi_test_do_round_trip(fixture);
719 tsi_test_fixture_destroy(fixture);
720 }
721
722 static bool is_slow_build() {
723 #if defined(GPR_ARCH_32) || defined(__APPLE__)
724 return true;
725 #else
726 return BuiltUnderMsan() || BuiltUnderTsan();
727 #endif
728 }
729
730 void ssl_tsi_test_do_round_trip_odd_buffer_size() {
731 gpr_log(GPR_INFO, "ssl_tsi_test_do_round_trip_odd_buffer_size");
732 const size_t odd_sizes[] = {1025, 2051, 4103, 8207, 16409};
733 size_t size = sizeof(odd_sizes) / sizeof(size_t);
734 // 1. This test is extremely slow under MSAN and TSAN.
735 // 2. On 32-bit, the test is much slower (probably due to lack of boringssl
736 // asm optimizations) so we only run a subset of tests to avoid timeout.
737 // 3. On Mac OS, we have slower testing machines so we only run a subset
738 // of tests to avoid timeout.
739 if (is_slow_build()) {
740 size = 1;
741 }
742 for (size_t ind1 = 0; ind1 < size; ind1++) {
743 for (size_t ind2 = 0; ind2 < size; ind2++) {
744 for (size_t ind3 = 0; ind3 < size; ind3++) {
745 for (size_t ind4 = 0; ind4 < size; ind4++) {
746 for (size_t ind5 = 0; ind5 < size; ind5++) {
747 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
748 ssl_tsi_test_fixture* ssl_fixture =
749 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
750 tsi_test_frame_protector_config_set_buffer_size(
751 ssl_fixture->base.config, odd_sizes[ind1], odd_sizes[ind2],
752 odd_sizes[ind3], odd_sizes[ind4], odd_sizes[ind5]);
753 tsi_test_do_round_trip(&ssl_fixture->base);
754 tsi_test_fixture_destroy(fixture);
755 }
756 }
757 }
758 }
759 }
760 }
761
762 void ssl_tsi_test_do_handshake_session_cache() {
763 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake_session_cache");
764 tsi_ssl_session_cache* session_cache = tsi_ssl_session_cache_create_lru(16);
765 char session_ticket_key[kSessionTicketEncryptionKeySize];
766 auto do_handshake = [&session_ticket_key,
767 &session_cache](bool session_reused) {
768 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
769 ssl_tsi_test_fixture* ssl_fixture =
770 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
771 ssl_fixture->server_name_indication =
772 const_cast<char*>("waterzooi.test.google.be");
773 ssl_fixture->session_ticket_key = session_ticket_key;
774 ssl_fixture->session_ticket_key_size = sizeof(session_ticket_key);
775 tsi_ssl_session_cache_ref(session_cache);
776 ssl_fixture->session_cache = session_cache;
777 ssl_fixture->session_reused = session_reused;
778 tsi_test_do_round_trip(&ssl_fixture->base);
779 tsi_test_fixture_destroy(fixture);
780 };
781 memset(session_ticket_key, 'a', sizeof(session_ticket_key));
782 do_handshake(false);
783 do_handshake(true);
784 do_handshake(true);
785 // Changing session_ticket_key on server invalidates ticket.
786 memset(session_ticket_key, 'b', sizeof(session_ticket_key));
787 do_handshake(false);
788 do_handshake(true);
789 memset(session_ticket_key, 'c', sizeof(session_ticket_key));
790 do_handshake(false);
791 do_handshake(true);
792 tsi_ssl_session_cache_unref(session_cache);
793 }
794
795 static const tsi_ssl_handshaker_factory_vtable* original_vtable;
796 static bool handshaker_factory_destructor_called;
797
798 static void ssl_tsi_test_handshaker_factory_destructor(
799 tsi_ssl_handshaker_factory* factory) {
800 ASSERT_NE(factory, nullptr);
801 handshaker_factory_destructor_called = true;
802 if (original_vtable != nullptr && original_vtable->destroy != nullptr) {
803 original_vtable->destroy(factory);
804 }
805 }
806
807 static tsi_ssl_handshaker_factory_vtable test_handshaker_factory_vtable = {
808 ssl_tsi_test_handshaker_factory_destructor};
809
810 void test_tsi_ssl_client_handshaker_factory_refcounting() {
811 int i;
812 char* cert_chain = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "client.pem");
813
814 tsi_ssl_client_handshaker_options options;
815 options.pem_root_certs = cert_chain;
816 tsi_ssl_client_handshaker_factory* client_handshaker_factory;
817 ASSERT_EQ(tsi_create_ssl_client_handshaker_factory_with_options(
818 &options, &client_handshaker_factory),
819 TSI_OK);
820
821 handshaker_factory_destructor_called = false;
822 original_vtable = tsi_ssl_handshaker_factory_swap_vtable(
823 reinterpret_cast<tsi_ssl_handshaker_factory*>(client_handshaker_factory),
824 &test_handshaker_factory_vtable);
825
826 tsi_handshaker* handshaker[3];
827
828 for (i = 0; i < 3; ++i) {
829 ASSERT_EQ(
830 tsi_ssl_client_handshaker_factory_create_handshaker(
831 client_handshaker_factory, "google.com", 0, 0, &handshaker[i]),
832 TSI_OK);
833 }
834
835 tsi_handshaker_destroy(handshaker[1]);
836 ASSERT_FALSE(handshaker_factory_destructor_called);
837
838 tsi_handshaker_destroy(handshaker[0]);
839 ASSERT_FALSE(handshaker_factory_destructor_called);
840
841 tsi_ssl_client_handshaker_factory_unref(client_handshaker_factory);
842 ASSERT_FALSE(handshaker_factory_destructor_called);
843
844 tsi_handshaker_destroy(handshaker[2]);
845 ASSERT_TRUE(handshaker_factory_destructor_called);
846
847 gpr_free(cert_chain);
848 }
849
850 void test_tsi_ssl_server_handshaker_factory_refcounting() {
851 int i;
852 tsi_ssl_server_handshaker_factory* server_handshaker_factory;
853 tsi_handshaker* handshaker[3];
854 const char* cert_chain =
855 load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server0.pem");
856 tsi_ssl_pem_key_cert_pair cert_pair;
857
858 cert_pair.cert_chain = cert_chain;
859 cert_pair.private_key =
860 load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server0.key");
861 tsi_ssl_server_handshaker_options options;
862 options.pem_key_cert_pairs = &cert_pair;
863 options.num_key_cert_pairs = 1;
864 options.pem_client_root_certs = cert_chain;
865
866 ASSERT_EQ(tsi_create_ssl_server_handshaker_factory_with_options(
867 &options, &server_handshaker_factory),
868 TSI_OK);
869
870 handshaker_factory_destructor_called = false;
871 original_vtable = tsi_ssl_handshaker_factory_swap_vtable(
872 reinterpret_cast<tsi_ssl_handshaker_factory*>(server_handshaker_factory),
873 &test_handshaker_factory_vtable);
874
875 for (i = 0; i < 3; ++i) {
876 ASSERT_EQ(tsi_ssl_server_handshaker_factory_create_handshaker(
877 server_handshaker_factory, 0, 0, &handshaker[i]),
878 TSI_OK);
879 }
880
881 tsi_handshaker_destroy(handshaker[1]);
882 ASSERT_FALSE(handshaker_factory_destructor_called);
883
884 tsi_handshaker_destroy(handshaker[0]);
885 ASSERT_FALSE(handshaker_factory_destructor_called);
886
887 tsi_ssl_server_handshaker_factory_unref(server_handshaker_factory);
888 ASSERT_FALSE(handshaker_factory_destructor_called);
889
890 tsi_handshaker_destroy(handshaker[2]);
891 ASSERT_TRUE(handshaker_factory_destructor_called);
892
893 ssl_test_pem_key_cert_pair_destroy(cert_pair);
894 }
895
896 /* Attempting to create a handshaker factory with invalid parameters should fail
897 * but not crash. */
898 void test_tsi_ssl_client_handshaker_factory_bad_params() {
899 const char* cert_chain = "This is not a valid PEM file.";
900
901 tsi_ssl_client_handshaker_factory* client_handshaker_factory;
902 tsi_ssl_client_handshaker_options options;
903 options.pem_root_certs = cert_chain;
904 ASSERT_EQ(tsi_create_ssl_client_handshaker_factory_with_options(
905 &options, &client_handshaker_factory),
906 TSI_INVALID_ARGUMENT);
907 tsi_ssl_client_handshaker_factory_unref(client_handshaker_factory);
908 }
909
910 void ssl_tsi_test_handshaker_factory_internals() {
911 gpr_log(GPR_INFO, "ssl_tsi_test_handshaker_factory_internals");
912 test_tsi_ssl_client_handshaker_factory_refcounting();
913 test_tsi_ssl_server_handshaker_factory_refcounting();
914 test_tsi_ssl_client_handshaker_factory_bad_params();
915 }
916
917 void ssl_tsi_test_duplicate_root_certificates() {
918 gpr_log(GPR_INFO, "ssl_tsi_test_duplicate_root_certificates");
919 char* root_cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "ca.pem");
920 char* dup_root_cert = static_cast<char*>(
921 gpr_zalloc(sizeof(char) * (strlen(root_cert) * 2 + 1)));
922 memcpy(dup_root_cert, root_cert, strlen(root_cert));
923 memcpy(dup_root_cert + strlen(root_cert), root_cert, strlen(root_cert));
924 tsi_ssl_root_certs_store* root_store =
925 tsi_ssl_root_certs_store_create(dup_root_cert);
926 ASSERT_NE(root_store, nullptr);
927 // Free memory.
928 tsi_ssl_root_certs_store_destroy(root_store);
929 gpr_free(root_cert);
930 gpr_free(dup_root_cert);
931 }
932
933 void ssl_tsi_test_extract_x509_subject_names() {
934 gpr_log(GPR_INFO, "ssl_tsi_test_extract_x509_subject_names");
935 char* cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "multi-domain.pem");
936 tsi_peer peer;
937 ASSERT_EQ(tsi_ssl_extract_x509_subject_names_from_pem_cert(cert, &peer),
938 TSI_OK);
939 // tsi_peer should include one subject, one common name, one certificate, one
940 // security level, ten SAN fields, two DNS SAN fields, three URI fields, two
941 // email addresses and two IP addresses.
942 size_t expected_property_count = 22;
943 ASSERT_EQ(peer.property_count, expected_property_count);
944 // Check subject
945 const char* expected_subject = "CN=xpigors,OU=Google,L=SF,ST=CA,C=US";
946 const tsi_peer_property* property =
947 tsi_peer_get_property_by_name(&peer, TSI_X509_SUBJECT_PEER_PROPERTY);
948 ASSERT_NE(property, nullptr);
949 ASSERT_EQ(
950 memcmp(property->value.data, expected_subject, property->value.length),
951 0);
952 // Check common name
953 const char* expected_cn = "xpigors";
954 property = tsi_peer_get_property_by_name(
955 &peer, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY);
956 ASSERT_NE(property, nullptr);
957 ASSERT_EQ(memcmp(property->value.data, expected_cn, property->value.length),
958 0);
959 // Check certificate data
960 property = tsi_peer_get_property_by_name(&peer, TSI_X509_PEM_CERT_PROPERTY);
961 ASSERT_NE(property, nullptr);
962 ASSERT_EQ(memcmp(property->value.data, cert, property->value.length), 0);
963 // Check DNS
964 ASSERT_EQ(
965 check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
966 "foo.test.domain.com"),
967 1);
968 ASSERT_EQ(
969 check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
970 "bar.test.domain.com"),
971 1);
972 ASSERT_EQ(
973 check_property(&peer, TSI_X509_DNS_PEER_PROPERTY, "foo.test.domain.com"),
974 1);
975 ASSERT_EQ(
976 check_property(&peer, TSI_X509_DNS_PEER_PROPERTY, "bar.test.domain.com"),
977 1);
978 // Check URI
979 // Note that a valid SPIFFE certificate should only have one URI.
980 ASSERT_EQ(
981 check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
982 "spiffe://foo.com/bar/baz"),
983 1);
984 ASSERT_EQ(
985 check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
986 "https://foo.test.domain.com/test"),
987 1);
988 ASSERT_EQ(
989 check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
990 "https://bar.test.domain.com/test"),
991 1);
992 ASSERT_EQ(check_property(&peer, TSI_X509_URI_PEER_PROPERTY,
993 "spiffe://foo.com/bar/baz"),
994 1);
995 ASSERT_EQ(check_property(&peer, TSI_X509_URI_PEER_PROPERTY,
996 "https://foo.test.domain.com/test"),
997 1);
998 ASSERT_EQ(check_property(&peer, TSI_X509_URI_PEER_PROPERTY,
999 "https://bar.test.domain.com/test"),
1000 1);
1001 // Check email address
1002 ASSERT_EQ(
1003 check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
1004 "[email protected]"),
1005 1);
1006 ASSERT_EQ(
1007 check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
1008 "[email protected]"),
1009 1);
1010 ASSERT_EQ(check_property(&peer, TSI_X509_EMAIL_PEER_PROPERTY,
1011 "[email protected]"),
1012 1);
1013 ASSERT_EQ(check_property(&peer, TSI_X509_EMAIL_PEER_PROPERTY,
1014 "[email protected]"),
1015 1);
1016 // Check ip address
1017 ASSERT_EQ(
1018 check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
1019 "192.168.7.1"),
1020 1);
1021 ASSERT_EQ(
1022 check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
1023 "13::17"),
1024 1);
1025 ASSERT_EQ(check_property(&peer, TSI_X509_IP_PEER_PROPERTY, "192.168.7.1"), 1);
1026 ASSERT_EQ(check_property(&peer, TSI_X509_IP_PEER_PROPERTY, "13::17"), 1);
1027 // Check other fields
1028 ASSERT_EQ(
1029 check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
1030 "other types of SAN"),
1031 1);
1032 // Free memory
1033 gpr_free(cert);
1034 tsi_peer_destruct(&peer);
1035 }
1036
1037 void ssl_tsi_test_extract_cert_chain() {
1038 gpr_log(GPR_INFO, "ssl_tsi_test_extract_cert_chain");
1039 char* cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "server1.pem");
1040 char* ca = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "ca.pem");
1041 char* chain = static_cast<char*>(
1042 gpr_zalloc(sizeof(char) * (strlen(cert) + strlen(ca) + 1)));
1043 memcpy(chain, cert, strlen(cert));
1044 memcpy(chain + strlen(cert), ca, strlen(ca));
1045 STACK_OF(X509)* cert_chain = sk_X509_new_null();
1046 ASSERT_NE(cert_chain, nullptr);
1047 BIO* bio = BIO_new_mem_buf(chain, strlen(chain));
1048 ASSERT_NE(bio, nullptr);
1049 STACK_OF(X509_INFO)* certInfos =
1050 PEM_X509_INFO_read_bio(bio, nullptr, nullptr, nullptr);
1051 ASSERT_NE(certInfos, nullptr);
1052 for (size_t i = 0; i < sk_X509_INFO_num(certInfos); i++) {
1053 X509_INFO* certInfo = sk_X509_INFO_value(certInfos, i);
1054 if (certInfo->x509 != nullptr) {
1055 ASSERT_NE(sk_X509_push(cert_chain, certInfo->x509), 0);
1056 #if OPENSSL_VERSION_NUMBER >= 0x10100000
1057 X509_up_ref(certInfo->x509);
1058 #else
1059 certInfo->x509->references += 1;
1060 #endif
1061 }
1062 }
1063 tsi_peer_property chain_property;
1064 ASSERT_EQ(tsi_ssl_get_cert_chain_contents(cert_chain, &chain_property),
1065 TSI_OK);
1066 ASSERT_EQ(
1067 memcmp(chain, chain_property.value.data, chain_property.value.length), 0);
1068 BIO_free(bio);
1069 gpr_free(chain);
1070 gpr_free(cert);
1071 gpr_free(ca);
1072 tsi_peer_property_destruct(&chain_property);
1073 sk_X509_INFO_pop_free(certInfos, X509_INFO_free);
1074 sk_X509_pop_free(cert_chain, X509_free);
1075 }
1076
1077 void ssl_tsi_test_do_handshake_with_custom_bio_pair() {
1078 gpr_log(GPR_INFO, "ssl_tsi_test_do_handshake_with_custom_bio_pair");
1079 tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
1080 ssl_tsi_test_fixture* ssl_fixture =
1081 reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
1082 #if OPENSSL_VERSION_NUMBER >= 0x10100000
1083 ssl_fixture->network_bio_buf_size = TSI_TEST_DEFAULT_BUFFER_SIZE;
1084 ssl_fixture->ssl_bio_buf_size = 256;
1085 #endif
1086 ssl_fixture->force_client_auth = true;
1087 tsi_test_do_handshake(fixture);
1088 tsi_test_fixture_destroy(fixture);
1089 }
1090
1091 TEST(SslTransportSecurityTest, MainTest) {
1092 grpc_init();
1093 const size_t number_tls_versions = 2;
1094 const tsi_tls_version tls_versions[] = {tsi_tls_version::TSI_TLS1_2,
1095 tsi_tls_version::TSI_TLS1_3};
1096 for (size_t i = 0; i < number_tls_versions; i++) {
1097 // Set the TLS version to be used in the tests.
1098 test_tls_version = tls_versions[i];
1099 // Run all the tests using that TLS version for both the client and server.
1100 ssl_tsi_test_do_handshake_tiny_handshake_buffer();
1101 ssl_tsi_test_do_handshake_small_handshake_buffer();
1102 ssl_tsi_test_do_handshake();
1103 ssl_tsi_test_do_handshake_with_root_store();
1104 ssl_tsi_test_do_handshake_with_client_authentication();
1105 ssl_tsi_test_do_handshake_with_client_authentication_and_root_store();
1106 ssl_tsi_test_do_handshake_with_server_name_indication_exact_domain();
1107 ssl_tsi_test_do_handshake_with_server_name_indication_wild_star_domain();
1108 ssl_tsi_test_do_handshake_with_wrong_server_name_indication();
1109 ssl_tsi_test_do_handshake_with_bad_server_cert();
1110 ssl_tsi_test_do_handshake_with_bad_client_cert();
1111 #ifdef OPENSSL_IS_BORINGSSL
1112 // BoringSSL and OpenSSL have different behaviors on mismatched ALPN.
1113 ssl_tsi_test_do_handshake_alpn_client_no_server();
1114 ssl_tsi_test_do_handshake_alpn_client_server_mismatch();
1115 #endif
1116 ssl_tsi_test_do_handshake_alpn_server_no_client();
1117 ssl_tsi_test_do_handshake_alpn_client_server_ok();
1118 ssl_tsi_test_do_handshake_session_cache();
1119 ssl_tsi_test_do_round_trip_for_all_configs();
1120 ssl_tsi_test_do_round_trip_with_error_on_stack();
1121 ssl_tsi_test_do_round_trip_odd_buffer_size();
1122 ssl_tsi_test_handshaker_factory_internals();
1123 ssl_tsi_test_duplicate_root_certificates();
1124 ssl_tsi_test_extract_x509_subject_names();
1125 ssl_tsi_test_extract_cert_chain();
1126 ssl_tsi_test_do_handshake_with_custom_bio_pair();
1127 }
1128 grpc_shutdown();
1129 }
1130
1131 int main(int argc, char** argv) {
1132 grpc::testing::TestEnvironment env(&argc, argv);
1133 ::testing::InitGoogleTest(&argc, argv);
1134 grpc::testing::TestGrpcScope grpc_scope;
1135 return RUN_ALL_TESTS();
1136 }