Debian Codegraph @

test/core/tsi/transport_security_test.cc

raw
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 "src/core/tsi/transport_security.h"
20
21 #include <string.h>
22
23 #include <string>
24
25 #include <gtest/gtest.h>
26 #include <openssl/crypto.h>
27
28 #include "absl/strings/str_format.h"
29
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/string_util.h>
33
34 #include "src/core/lib/gpr/string.h"
35 #include "src/core/lib/gpr/useful.h"
36 #include "src/core/tsi/fake_transport_security.h"
37 #include "src/core/tsi/ssl_transport_security.h"
38 #include "test/core/util/test_config.h"
39
40 typedef struct {
41 /* 1 if success, 0 if failure. */
42 int expected;
43
44 /* Host name to match. */
45 const char* host_name;
46
47 /* Common name (CN). */
48 const char* common_name;
49
50 /* Comma separated list of certificate names to match against. Any occurrence
51 of '#' will be replaced with a null character before processing. */
52 const char* dns_names;
53
54 /* Comma separated list of IP SANs to match aggainst */
55 const char* ip_names;
56 } cert_name_test_entry;
57
58 /* Largely inspired from:
59 chromium/src/net/cert/x509_certificate_unittest.cc.
60 TODO(jboeuf) uncomment test cases as we fix tsi_ssl_peer_matches_name. */
61 const cert_name_test_entry cert_name_test_entries[] = {
62 {1, "foo.com", "foo.com", nullptr, nullptr},
63 {1, "f", "f", nullptr, nullptr},
64 {0, "h", "i", nullptr, nullptr},
65 {1, "bar.foo.com", "*.foo.com", nullptr, nullptr},
66 {1, "www.test.fr", "common.name",
67 "*.test.com,*.test.co.uk,*.test.de,*.test.fr", nullptr},
68 /*
69 {1, "wwW.tESt.fr", "common.name", ",*.*,*.test.de,*.test.FR,www"},
70 */
71 {0, "f.uk", ".uk", nullptr, nullptr},
72 {0, "w.bar.foo.com", "?.bar.foo.com", nullptr, nullptr},
73 {0, "www.foo.com", "(www|ftp).foo.com", nullptr, nullptr},
74 {0, "www.foo.com", "www.foo.com#", nullptr, nullptr}, /* # = null char. */
75 {0, "www.foo.com", "", "www.foo.com#*.foo.com,#,#", nullptr},
76 {0, "www.house.example", "ww.house.example", nullptr, nullptr},
77 {0, "test.org", "", "www.test.org,*.test.org,*.org", nullptr},
78 {0, "w.bar.foo.com", "w*.bar.foo.com", nullptr, nullptr},
79 {0, "www.bar.foo.com", "ww*ww.bar.foo.com", nullptr, nullptr},
80 {0, "wwww.bar.foo.com", "ww*ww.bar.foo.com", nullptr, nullptr},
81 {0, "wwww.bar.foo.com", "w*w.bar.foo.com", nullptr, nullptr},
82 {0, "wwww.bar.foo.com", "w*w.bar.foo.c0m", nullptr, nullptr},
83 {0, "WALLY.bar.foo.com", "wa*.bar.foo.com", nullptr, nullptr},
84 {0, "wally.bar.foo.com", "*Ly.bar.foo.com", nullptr, nullptr},
85 /*
86 {1, "ww%57.foo.com", "", "www.foo.com"},
87 {1, "www&.foo.com", "www%26.foo.com", NULL},
88 */
89
90 /* Common name must not be used if subject alternative name was provided. */
91 {0, "www.test.co.jp", "www.test.co.jp",
92 "*.test.de,*.jp,www.test.co.uk,www.*.co.jp", nullptr},
93 {0, "www.bar.foo.com", "www.bar.foo.com",
94 "*.foo.com,*.*.foo.com,*.*.bar.foo.com,*..bar.foo.com,", nullptr},
95
96 /* IDN tests */
97 {1, "xn--poema-9qae5a.com.br", "xn--poema-9qae5a.com.br", nullptr, nullptr},
98 {1, "www.xn--poema-9qae5a.com.br", "*.xn--poema-9qae5a.com.br", nullptr,
99 nullptr},
100 {0, "xn--poema-9qae5a.com.br", "",
101 "*.xn--poema-9qae5a.com.br,"
102 "xn--poema-*.com.br,"
103 "xn--*-9qae5a.com.br,"
104 "*--poema-9qae5a.com.br",
105 nullptr},
106
107 /* The following are adapted from the examples quoted from
108 http://tools.ietf.org/html/rfc6125#section-6.4.3
109 (e.g., *.example.com would match foo.example.com but
110 not bar.foo.example.com or example.com). */
111 {1, "foo.example.com", "*.example.com", nullptr, nullptr},
112 {0, "bar.foo.example.com", "*.example.com", nullptr, nullptr},
113 {0, "example.com", "*.example.com", nullptr, nullptr},
114
115 /* Partial wildcards are disallowed, though RFC 2818 rules allow them.
116 That is, forms such as baz*.example.net, *baz.example.net, and
117 b*z.example.net should NOT match domains. Instead, the wildcard must
118 always be the left-most label, and only a single label. */
119 {0, "baz1.example.net", "baz*.example.net", nullptr, nullptr},
120 {0, "foobaz.example.net", "*baz.example.net", nullptr, nullptr},
121 {0, "buzz.example.net", "b*z.example.net", nullptr, nullptr},
122 {0, "www.test.example.net", "www.*.example.net", nullptr, nullptr},
123
124 /* Wildcards should not be valid for public registry controlled domains,
125 and unknown/unrecognized domains, at least three domain components must
126 be present. */
127 {1, "www.test.example", "*.test.example", nullptr, nullptr},
128 {1, "test.example.co.uk", "*.example.co.uk", nullptr, nullptr},
129 {0, "test.example", "*.example", nullptr, nullptr},
130 /*
131 {0, "example.co.uk", "*.co.uk", NULL},
132 */
133 {0, "foo.com", "*.com", nullptr, nullptr},
134 {0, "foo.us", "*.us", nullptr, nullptr},
135 {0, "foo", "*", nullptr, nullptr},
136
137 /* IDN variants of wildcards and registry controlled domains. */
138 {1, "www.xn--poema-9qae5a.com.br", "*.xn--poema-9qae5a.com.br", nullptr,
139 nullptr},
140 {1, "test.example.xn--mgbaam7a8h", "*.example.xn--mgbaam7a8h", nullptr,
141 nullptr},
142 /*
143 {0, "xn--poema-9qae5a.com.br", "*.com.br", NULL},
144 */
145 {0, "example.xn--mgbaam7a8h", "*.xn--mgbaam7a8h", nullptr, nullptr},
146
147 /* Wildcards should be permissible for 'private' registry controlled
148 domains. */
149 {1, "www.appspot.com", "*.appspot.com", nullptr, nullptr},
150 {1, "foo.s3.amazonaws.com", "*.s3.amazonaws.com", nullptr, nullptr},
151
152 /* Multiple wildcards are not valid. */
153 {0, "foo.example.com", "*.*.com", nullptr, nullptr},
154 {0, "foo.bar.example.com", "*.bar.*.com", nullptr, nullptr},
155
156 /* Absolute vs relative DNS name tests. Although not explicitly specified
157 in RFC 6125, absolute reference names (those ending in a .) should
158 match either absolute or relative presented names. */
159 {1, "foo.com", "foo.com.", nullptr, nullptr},
160 {1, "foo.com.", "foo.com", nullptr, nullptr},
161 {1, "foo.com.", "foo.com.", nullptr, nullptr},
162 {1, "f", "f.", nullptr, nullptr},
163 {1, "f.", "f", nullptr, nullptr},
164 {1, "f.", "f.", nullptr, nullptr},
165 {1, "www-3.bar.foo.com", "*.bar.foo.com.", nullptr, nullptr},
166 {1, "www-3.bar.foo.com.", "*.bar.foo.com", nullptr, nullptr},
167 {1, "www-3.bar.foo.com.", "*.bar.foo.com.", nullptr, nullptr},
168 {0, ".", ".", nullptr, nullptr},
169 {0, "example.com", "*.com.", nullptr, nullptr},
170 {0, "example.com.", "*.com", nullptr, nullptr},
171 {0, "example.com.", "*.com.", nullptr, nullptr},
172 {0, "foo.", "*.", nullptr, nullptr},
173 {0, "foo", "*.", nullptr, nullptr},
174 /*
175 {0, "foo.co.uk", "*.co.uk.", NULL},
176 {0, "foo.co.uk.", "*.co.uk.", NULL},
177 */
178
179 /* An empty CN is OK. */
180 {1, "test.foo.com", "", "test.foo.com", nullptr},
181
182 /* An IP should not be used for the CN. */
183 {0, "173.194.195.139", "173.194.195.139", nullptr, nullptr},
184 /* An IP can be used if the SAN IP is present */
185 {1, "173.194.195.139", "foo.example.com", nullptr, "173.194.195.139"},
186 {0, "173.194.195.139", "foo.example.com", nullptr, "8.8.8.8"},
187 {0, "173.194.195.139", "foo.example.com", nullptr, "8.8.8.8,8.8.4.4"},
188 {1, "173.194.195.139", "foo.example.com", nullptr,
189 "8.8.8.8,173.194.195.139"},
190 {0, "173.194.195.139", "foo.example.com", nullptr, "173.194.195.13"},
191 {0, "2001:db8:a0b:12f0::1", "foo.example.com", nullptr, "173.194.195.13"},
192 {1, "2001:db8:a0b:12f0::1", "foo.example.com", nullptr,
193 "2001:db8:a0b:12f0::1"},
194 {0, "2001:db8:a0b:12f0::1", "foo.example.com", nullptr,
195 "2001:db8:a0b:12f0::2"},
196 {1, "2001:db8:a0b:12f0::1", "foo.example.com", nullptr,
197 "2001:db8:a0b:12f0::2,2001:db8:a0b:12f0::1,8.8.8.8"},
198 };
199
200 typedef struct name_list {
201 const char* name;
202 struct name_list* next;
203 } name_list;
204
205 typedef struct {
206 size_t name_count;
207 char* buffer;
208 name_list* names;
209 } parsed_names;
210
211 name_list* name_list_add(const char* n) {
212 name_list* result = static_cast<name_list*>(gpr_malloc(sizeof(name_list)));
213 result->name = n;
214 result->next = nullptr;
215 return result;
216 }
217
218 static parsed_names parse_names(const char* names_str) {
219 parsed_names result;
220 name_list* current_nl;
221 size_t i;
222 memset(&result, 0, sizeof(parsed_names));
223 if (names_str == nullptr) return result;
224 result.name_count = 1;
225 result.buffer = gpr_strdup(names_str);
226 result.names = name_list_add(result.buffer);
227 current_nl = result.names;
228 for (i = 0; i < strlen(names_str); i++) {
229 if (names_str[i] == ',') {
230 result.buffer[i] = '\0';
231 result.name_count++;
232 i++;
233 current_nl->next = name_list_add(result.buffer + i);
234 current_nl = current_nl->next;
235 }
236 }
237 return result;
238 }
239
240 static void destruct_parsed_names(parsed_names* pdn) {
241 name_list* nl = pdn->names;
242 if (pdn->buffer != nullptr) gpr_free(pdn->buffer);
243 while (nl != nullptr) {
244 name_list* to_be_free = nl;
245 nl = nl->next;
246 gpr_free(to_be_free);
247 }
248 }
249
250 static char* processed_name(const char* name) {
251 char* result = gpr_strdup(name);
252 size_t i;
253 for (i = 0; i < strlen(result); i++) {
254 if (result[i] == '#') {
255 result[i] = '\0';
256 }
257 }
258 return result;
259 }
260
261 static tsi_peer peer_from_cert_name_test_entry(
262 const cert_name_test_entry* entry) {
263 size_t i;
264 tsi_peer peer;
265 name_list* nl;
266 parsed_names dns_entries = parse_names(entry->dns_names);
267 parsed_names ip_entries = parse_names(entry->ip_names);
268 nl = dns_entries.names;
269 EXPECT_EQ(tsi_construct_peer(
270 1 + dns_entries.name_count + ip_entries.name_count, &peer),
271 TSI_OK);
272 EXPECT_EQ(tsi_construct_string_peer_property_from_cstring(
273 TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, entry->common_name,
274 &peer.properties[0]),
275 TSI_OK);
276 i = 1;
277 while (nl != nullptr) {
278 char* processed = processed_name(nl->name);
279 EXPECT_EQ(tsi_construct_string_peer_property(
280 TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, processed,
281 strlen(nl->name), &peer.properties[i++]),
282 TSI_OK);
283 nl = nl->next;
284 gpr_free(processed);
285 }
286
287 nl = ip_entries.names;
288 while (nl != nullptr) {
289 char* processed = processed_name(nl->name);
290 EXPECT_EQ(tsi_construct_string_peer_property(
291 TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, processed,
292 strlen(nl->name), &peer.properties[i++]),
293 TSI_OK);
294 nl = nl->next;
295 gpr_free(processed);
296 }
297 destruct_parsed_names(&dns_entries);
298 destruct_parsed_names(&ip_entries);
299 return peer;
300 }
301
302 std::string cert_name_test_entry_to_string(const cert_name_test_entry* entry) {
303 return absl::StrFormat(
304 "{ success = %s, host_name = %s, common_name = %s, dns_names = "
305 "%s, ip_names = %s}",
306 entry->expected ? "true" : "false", entry->host_name, entry->common_name,
307 entry->dns_names != nullptr ? entry->dns_names : "",
308 entry->ip_names != nullptr ? entry->ip_names : "");
309 }
310
311 TEST(TransportSecurityTest, TestPeerMatchesName) {
312 size_t i = 0;
313 for (i = 0; i < GPR_ARRAY_SIZE(cert_name_test_entries); i++) {
314 const cert_name_test_entry* entry = &cert_name_test_entries[i];
315 tsi_peer peer = peer_from_cert_name_test_entry(entry);
316 int result = tsi_ssl_peer_matches_name(&peer, entry->host_name);
317 if (result != entry->expected) {
318 gpr_log(GPR_ERROR, "%s", cert_name_test_entry_to_string(entry).c_str());
319 ASSERT_TRUE(0); /* Unexpected result. */
320 }
321 tsi_peer_destruct(&peer);
322 }
323 }
324
325 typedef struct {
326 tsi_result res;
327 const char* str;
328 } tsi_result_string_pair;
329
330 TEST(TransportSecurityTest, TestResultStrings) {
331 const tsi_result_string_pair results[] = {
332 {TSI_OK, "TSI_OK"},
333 {TSI_UNKNOWN_ERROR, "TSI_UNKNOWN_ERROR"},
334 {TSI_INVALID_ARGUMENT, "TSI_INVALID_ARGUMENT"},
335 {TSI_PERMISSION_DENIED, "TSI_PERMISSION_DENIED"},
336 {TSI_INCOMPLETE_DATA, "TSI_INCOMPLETE_DATA"},
337 {TSI_FAILED_PRECONDITION, "TSI_FAILED_PRECONDITION"},
338 {TSI_UNIMPLEMENTED, "TSI_UNIMPLEMENTED"},
339 {TSI_INTERNAL_ERROR, "TSI_INTERNAL_ERROR"},
340 {TSI_DATA_CORRUPTED, "TSI_DATA_CORRUPTED"},
341 {TSI_NOT_FOUND, "TSI_NOT_FOUND"},
342 {TSI_PROTOCOL_FAILURE, "TSI_PROTOCOL_FAILURE"},
343 {TSI_HANDSHAKE_IN_PROGRESS, "TSI_HANDSHAKE_IN_PROGRESS"},
344 {TSI_OUT_OF_RESOURCES, "TSI_OUT_OF_RESOURCES"}};
345 size_t i;
346 for (i = 0; i < GPR_ARRAY_SIZE(results); i++) {
347 ASSERT_STREQ(results[i].str, tsi_result_to_string(results[i].res));
348 }
349 ASSERT_STREQ("UNKNOWN", tsi_result_to_string((tsi_result)42));
350 }
351
352 TEST(TransportSecurityTest, TestProtectorInvalidArgs) {
353 ASSERT_EQ(
354 tsi_frame_protector_protect(nullptr, nullptr, nullptr, nullptr, nullptr),
355 TSI_INVALID_ARGUMENT);
356 ASSERT_EQ(
357 tsi_frame_protector_protect_flush(nullptr, nullptr, nullptr, nullptr),
358 TSI_INVALID_ARGUMENT);
359 ASSERT_EQ(tsi_frame_protector_unprotect(nullptr, nullptr, nullptr, nullptr,
360 nullptr),
361 TSI_INVALID_ARGUMENT);
362 }
363
364 TEST(TransportSecurityTest, TestHandshakerInvalidArgs) {
365 ASSERT_EQ(tsi_handshaker_get_result(nullptr), TSI_INVALID_ARGUMENT);
366 ASSERT_EQ(tsi_handshaker_extract_peer(nullptr, nullptr),
367 TSI_INVALID_ARGUMENT);
368 ASSERT_EQ(tsi_handshaker_create_frame_protector(nullptr, nullptr, nullptr),
369 TSI_INVALID_ARGUMENT);
370 ASSERT_EQ(tsi_handshaker_process_bytes_from_peer(nullptr, nullptr, nullptr),
371 TSI_INVALID_ARGUMENT);
372 ASSERT_EQ(tsi_handshaker_get_bytes_to_send_to_peer(nullptr, nullptr, nullptr),
373 TSI_INVALID_ARGUMENT);
374 ASSERT_EQ(tsi_handshaker_next(nullptr, nullptr, 0, nullptr, nullptr, nullptr,
375 nullptr, nullptr),
376 TSI_INVALID_ARGUMENT);
377 }
378
379 TEST(TransportSecurityTest, TestHandshakerInvalidState) {
380 tsi_handshaker* h = tsi_create_fake_handshaker(0);
381 tsi_peer peer;
382 tsi_frame_protector* p;
383 ASSERT_EQ(tsi_handshaker_extract_peer(h, &peer), TSI_FAILED_PRECONDITION);
384 ASSERT_EQ(tsi_handshaker_create_frame_protector(h, nullptr, &p),
385 TSI_FAILED_PRECONDITION);
386 tsi_handshaker_destroy(h);
387 }
388
389 int main(int argc, char** argv) {
390 grpc::testing::TestEnvironment env(&argc, argv);
391 ::testing::InitGoogleTest(&argc, argv);
392 return RUN_ALL_TESTS();
393 }