Debian Codegraph @

crt/s2n/tests/unit/s2n_wildcard_hostname_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "api/s2n.h"
20 #include "crypto/s2n_certificate.h"
21 #include "s2n_test.h"
22 #include "testlib/s2n_testlib.h"
23 #include "utils/s2n_safety.h"
24
25 struct wildcardify_test_case {
26 const char *hostname;
27 const char *output;
28 };
29
30 struct wildcardify_test_case wildcardify_test_cases[] = {
31 { .hostname = "foo.bar.com", .output = "*.bar.com" },
32 { .hostname = "localhost", .output = NULL },
33 { .hostname = "one.com", .output = "*.com" },
34 { .hostname = "foo*.bar*.com*", .output = "*.bar*.com*" },
35 { .hostname = "foo.bar.com.", .output = "*.bar.com." },
36 { .hostname = "*.a.c", .output = "*.a.c" },
37 { .hostname = "*", .output = NULL },
38 { .hostname = "foo.", .output = "*." },
39 };
40
41 int main(int argc, char **argv)
42 {
43 BEGIN_TEST();
44
45 const int num_wildcardify_tests = s2n_array_len(wildcardify_test_cases);
46 for (size_t i = 0; i < num_wildcardify_tests; i++) {
47 const char *hostname = wildcardify_test_cases[i].hostname;
48 struct s2n_blob hostname_blob = { 0 };
49 EXPECT_SUCCESS(s2n_blob_init(&hostname_blob, (uint8_t *) (uintptr_t) hostname, strlen(hostname)));
50 uint8_t output[S2N_MAX_SERVER_NAME] = { 0 };
51 struct s2n_blob output_blob = { 0 };
52 EXPECT_SUCCESS(s2n_blob_init(&output_blob, (uint8_t *) (uintptr_t) output, sizeof(output)));
53 struct s2n_stuffer hostname_stuffer = { 0 };
54 struct s2n_stuffer output_stuffer = { 0 };
55 EXPECT_SUCCESS(s2n_stuffer_init(&hostname_stuffer, &hostname_blob));
56 EXPECT_SUCCESS(s2n_stuffer_skip_write(&hostname_stuffer, hostname_blob.size));
57 EXPECT_SUCCESS(s2n_stuffer_init(&output_stuffer, &output_blob));
58 EXPECT_SUCCESS(s2n_create_wildcard_hostname(&hostname_stuffer, &output_stuffer));
59
60 /* Make sure the wildcard generated matches the output we expect. */
61 const uint32_t wildcard_len = s2n_stuffer_data_available(&output_stuffer);
62 const char *expected_output = wildcardify_test_cases[i].output;
63 if (wildcard_len > 0) {
64 EXPECT_EQUAL(wildcard_len, strlen(expected_output));
65 EXPECT_SUCCESS(memcmp(output, expected_output, wildcard_len));
66 } else {
67 EXPECT_EQUAL(expected_output, NULL);
68 }
69 }
70
71 /* s2n_connection_get_certificate_match */
72 {
73 /* Safety checks */
74 {
75 s2n_cert_sni_match match_status = 0;
76 struct s2n_connection *conn = NULL;
77 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_certificate_match(NULL, &match_status),
78 S2N_ERR_INVALID_ARGUMENT);
79 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_certificate_match(conn, NULL),
80 S2N_ERR_INVALID_ARGUMENT);
81
82 /* This API does not work on a client connection */
83 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
84 s2n_connection_ptr_free);
85 EXPECT_NOT_NULL(client_conn);
86 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_certificate_match(client_conn, &match_status),
87 S2N_ERR_CLIENT_MODE);
88
89 /* This API will not work if a certificate isn't selected yet. */
90 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
91 s2n_connection_ptr_free);
92 EXPECT_NOT_NULL(server_conn);
93 EXPECT_FAILURE_WITH_ERRNO(s2n_connection_get_certificate_match(server_conn, &match_status),
94 S2N_ERR_NO_CERT_FOUND);
95 }
96
97 const struct {
98 const char *cert_path;
99 const char *key_path;
100 const char *server_name;
101 s2n_cert_sni_match expected_match_status;
102 } test_cases[] = {
103 /* Client does not send an SNI extension */
104 {
105 .cert_path = S2N_DEFAULT_TEST_CERT_CHAIN,
106 .key_path = S2N_DEFAULT_TEST_PRIVATE_KEY,
107 .server_name = NULL,
108 .expected_match_status = S2N_SNI_NONE,
109 },
110 /* Server has a certificate that matches the client's SNI extension */
111 {
112 .cert_path = S2N_DEFAULT_TEST_CERT_CHAIN,
113 .key_path = S2N_DEFAULT_TEST_PRIVATE_KEY,
114 .server_name = "localhost",
115 .expected_match_status = S2N_SNI_EXACT_MATCH,
116 },
117 /* Server has a certificate with a domain name containing a wildcard character
118 * which can be matched to the client's SNI extension */
119 {
120 .cert_path = S2N_RSA_2048_SHA256_WILDCARD_CERT,
121 .key_path = S2N_RSA_2048_SHA256_WILDCARD_KEY,
122 .server_name = "alligator.localhost",
123 .expected_match_status = S2N_SNI_WILDCARD_MATCH,
124 },
125 /* Server does not have a certificate that can be matched to the client's
126 * SNI extension. */
127 {
128 .cert_path = S2N_DEFAULT_TEST_CERT_CHAIN,
129 .key_path = S2N_DEFAULT_TEST_PRIVATE_KEY,
130 .server_name = "This cert name is unlikely to exist.",
131 .expected_match_status = S2N_SNI_NO_MATCH,
132 },
133 };
134
135 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
136 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
137 s2n_connection_ptr_free);
138 EXPECT_NOT_NULL(client_conn);
139
140 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
141 s2n_connection_ptr_free);
142 EXPECT_NOT_NULL(server_conn);
143
144 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
145 EXPECT_NOT_NULL(config);
146 EXPECT_OK(s2n_config_set_tls12_security_policy(config));
147 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
148 s2n_cert_chain_and_key_ptr_free);
149 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
150 test_cases[i].cert_path, test_cases[i].key_path));
151 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
152 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
153
154 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
155 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
156
157 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
158 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
159 EXPECT_OK(s2n_connections_set_io_stuffer_pair(server_conn, client_conn, &io_pair));
160
161 const char *server_name = test_cases[i].server_name;
162 if (server_name) {
163 EXPECT_SUCCESS(s2n_set_server_name(client_conn, server_name));
164 }
165
166 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
167
168 s2n_cert_sni_match match_status = 0;
169 EXPECT_SUCCESS(s2n_connection_get_certificate_match(server_conn, &match_status));
170 EXPECT_EQUAL(match_status, test_cases[i].expected_match_status);
171 }
172
173 /* Test that cert info can still be retrieved in the case of a failed handshake */
174 {
175 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
176 s2n_connection_ptr_free);
177 EXPECT_NOT_NULL(client_conn);
178 EXPECT_OK(s2n_connection_set_tls12_security_policy(client_conn));
179
180 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
181 s2n_connection_ptr_free);
182 EXPECT_NOT_NULL(server_conn);
183
184 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
185 EXPECT_NOT_NULL(config);
186 EXPECT_OK(s2n_config_set_tls12_security_policy(config));
187 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
188 s2n_cert_chain_and_key_ptr_free);
189 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
190 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
191 EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));
192 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
193
194 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
195 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
196
197 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
198 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
199 EXPECT_OK(s2n_connections_set_io_stuffer_pair(server_conn, client_conn, &io_pair));
200
201 EXPECT_SUCCESS(s2n_set_server_name(client_conn, "This cert name is unlikely to exist."));
202 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
203 S2N_ERR_CERT_INVALID_HOSTNAME);
204
205 s2n_cert_sni_match match_status = 0;
206 EXPECT_SUCCESS(s2n_connection_get_certificate_match(server_conn, &match_status));
207 EXPECT_EQUAL(match_status, S2N_SNI_NO_MATCH);
208 }
209 }
210
211 END_TEST();
212 }