Debian Codegraph @

crt/s2n/tests/unit/s2n_client_auth_handshake_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 <errno.h>
17 #include <fcntl.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 #include "api/s2n.h"
23 #include "crypto/s2n_fips.h"
24 #include "s2n_test.h"
25 #include "testlib/s2n_testlib.h"
26 #include "tls/s2n_cipher_preferences.h"
27 #include "tls/s2n_cipher_suites.h"
28 #include "tls/s2n_connection.h"
29 #include "tls/s2n_handshake.h"
30 #include "tls/s2n_tls13.h"
31 #include "utils/s2n_safety.h"
32
33 /* To get access to the static functions / variables we need to test */
34 #include "tls/s2n_handshake_io.c"
35 #include "tls/s2n_tls13_handshake.c"
36
37 int s2n_test_client_auth_negotiation(struct s2n_config *server_config, struct s2n_config *client_config, struct s2n_cert_chain_and_key *ecdsa_cert, bool no_cert)
38 {
39 /* Set up client and server connections */
40 struct s2n_connection *client_conn = NULL;
41 struct s2n_connection *server_conn = NULL;
42 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
43 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
44
45 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
46 client_conn->x509_validator.skip_cert_validation = 1;
47 client_conn->server_protocol_version = S2N_TLS13;
48 client_conn->client_protocol_version = S2N_TLS13;
49 client_conn->actual_protocol_version = S2N_TLS13;
50 client_conn->handshake_params.server_cert_sig_scheme = &s2n_ecdsa_sha256;
51 client_conn->handshake_params.client_cert_sig_scheme = &s2n_ecdsa_sha256;
52 client_conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
53 if (!no_cert) {
54 client_conn->handshake_params.our_chain_and_key = ecdsa_cert;
55 }
56
57 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
58 server_conn->server_protocol_version = S2N_TLS13;
59 server_conn->client_protocol_version = S2N_TLS13;
60 server_conn->actual_protocol_version = S2N_TLS13;
61 server_conn->handshake_params.server_cert_sig_scheme = &s2n_ecdsa_sha256;
62 server_conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
63
64 if (no_cert) {
65 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_OPTIONAL));
66 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_OPTIONAL));
67 } else {
68 server_conn->x509_validator.skip_cert_validation = 1;
69 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_REQUIRED));
70 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_REQUIRED));
71 }
72
73 /* Create nonblocking pipes */
74 struct s2n_test_io_pair io_pair;
75 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
76 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
77
78 /* Negotiate handshake */
79 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
80
81 EXPECT_TRUE(IS_CLIENT_AUTH_HANDSHAKE(server_conn));
82 EXPECT_TRUE(IS_CLIENT_AUTH_HANDSHAKE(client_conn));
83 EXPECT_EQUAL(IS_CLIENT_AUTH_NO_CERT(server_conn), no_cert);
84 EXPECT_EQUAL(IS_CLIENT_AUTH_NO_CERT(client_conn), no_cert);
85
86 const char *app_data_str = "APPLICATION_DATA";
87 EXPECT_EQUAL(strcmp(app_data_str, s2n_connection_get_last_message_name(client_conn)), 0);
88
89 /* Clean up */
90 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
91 EXPECT_SUCCESS(s2n_connection_free(server_conn));
92 EXPECT_SUCCESS(s2n_connection_free(client_conn));
93 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
94
95 return 0;
96 }
97
98 /* Test to verify the explicit ordering of client_auth handshake with and without a client
99 * certificate. This includes some pre and post condition checks that pertain to client
100 * authentication between messages.
101 */
102 int s2n_test_client_auth_message_by_message(bool no_cert)
103 {
104 struct s2n_connection *client_conn = NULL;
105 struct s2n_connection *server_conn = NULL;
106
107 struct s2n_config *server_config = NULL, *client_config = NULL;
108 EXPECT_NOT_NULL(server_config = s2n_config_new());
109 EXPECT_NOT_NULL(client_config = s2n_config_new());
110 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
111 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "20190801"));
112 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "20190801"));
113
114 char *cert_chain = NULL;
115 char *private_key = NULL;
116 EXPECT_NOT_NULL(cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE));
117 EXPECT_NOT_NULL(private_key = malloc(S2N_MAX_TEST_PEM_SIZE));
118
119 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_CERT_CHAIN, cert_chain, S2N_MAX_TEST_PEM_SIZE));
120 EXPECT_SUCCESS(s2n_read_test_pem(S2N_ECDSA_P384_PKCS1_KEY, private_key, S2N_MAX_TEST_PEM_SIZE));
121
122 struct s2n_cert_chain_and_key *default_cert = NULL;
123 EXPECT_NOT_NULL(default_cert = s2n_cert_chain_and_key_new());
124 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(default_cert, cert_chain, private_key));
125 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, default_cert));
126 if (!no_cert) {
127 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, default_cert));
128 }
129
130 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
131 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
132 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
133 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
134 if (no_cert) {
135 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_OPTIONAL));
136 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_OPTIONAL));
137 } else {
138 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server_conn, S2N_CERT_AUTH_REQUIRED));
139 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client_conn, S2N_CERT_AUTH_REQUIRED));
140 client_conn->x509_validator.skip_cert_validation = 1;
141 server_conn->x509_validator.skip_cert_validation = 1;
142 }
143
144 struct s2n_stuffer client_to_server = { 0 };
145 struct s2n_stuffer server_to_client = { 0 };
146
147 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_to_server, 0));
148 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&server_to_client, 0));
149
150 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&server_to_client, &client_to_server, client_conn));
151 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_to_server, &server_to_client, server_conn));
152
153 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
154 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
155
156 /* Client sends ClientHello */
157 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), CLIENT_HELLO);
158 EXPECT_SUCCESS(s2n_handshake_write_io(client_conn));
159
160 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
161 EXPECT_EQUAL(server_conn->actual_protocol_version, 0);
162
163 EXPECT_EQUAL(server_conn->handshake.handshake_type, INITIAL);
164
165 /* Server reads ClientHello */
166 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_HELLO);
167 EXPECT_SUCCESS(s2n_handshake_read_io(server_conn));
168
169 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13); /* Server is now on TLS13 */
170 EXPECT_EQUAL(server_conn->handshake.handshake_type, NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | MIDDLEBOX_COMPAT);
171
172 EXPECT_SUCCESS(s2n_conn_set_handshake_type(server_conn));
173
174 /* Server sends ServerHello */
175 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_HELLO);
176 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
177
178 /* Server sends CCS */
179 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_CHANGE_CIPHER_SPEC);
180 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
181
182 /* Server sends EncryptedExtensions */
183 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), ENCRYPTED_EXTENSIONS);
184 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
185
186 /* Server sends CertificateRequest */
187 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_CERT_REQ);
188 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
189
190 /* Server sends ServerCert */
191 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_CERT);
192 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
193
194 /* Server sends CertVerify */
195 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_CERT_VERIFY);
196 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
197
198 /* Server sends ServerFinished */
199 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_FINISHED);
200 EXPECT_SUCCESS(s2n_handshake_write_io(server_conn));
201
202 /* Client reads ServerHello */
203 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_HELLO);
204 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
205
206 /* Client reads CCS
207 * The CCS message does not affect its place in the state machine. */
208 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), ENCRYPTED_EXTENSIONS);
209 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
210
211 /* Client reads EncryptedExtensions */
212 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), ENCRYPTED_EXTENSIONS);
213 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
214
215 if (no_cert) {
216 /* Client reads CertificateRequest but expects Cert */
217 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_CERT);
218 } else {
219 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_CERT_REQ);
220 }
221 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
222
223 EXPECT_EQUAL(client_conn->handshake.handshake_type, NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | MIDDLEBOX_COMPAT);
224
225 /* Client reads ServerCert */
226 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_CERT);
227 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
228
229 /* Client reads CertVerify */
230 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_CERT_VERIFY);
231 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
232
233 /* Client reads ServerFinished */
234 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_FINISHED);
235 EXPECT_SUCCESS(s2n_handshake_read_io(client_conn));
236
237 /* Client sends CCS */
238 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), CLIENT_CHANGE_CIPHER_SPEC);
239 EXPECT_SUCCESS(s2n_handshake_write_io(client_conn));
240
241 /* Client sends ClientCert */
242 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), CLIENT_CERT);
243 EXPECT_SUCCESS(s2n_handshake_write_io(client_conn));
244
245 if (no_cert) {
246 EXPECT_EQUAL(client_conn->handshake.handshake_type, NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | NO_CLIENT_CERT | MIDDLEBOX_COMPAT);
247 } else {
248 EXPECT_EQUAL(client_conn->handshake.handshake_type, NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | MIDDLEBOX_COMPAT);
249
250 /* Client sends CertVerify */
251 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), CLIENT_CERT_VERIFY);
252 EXPECT_SUCCESS(s2n_handshake_write_io(client_conn));
253 }
254
255 /* Client sends ClientFinished */
256 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), CLIENT_FINISHED);
257 EXPECT_SUCCESS(s2n_handshake_write_io(client_conn));
258
259 /* Server reads CCS
260 * The CCS message does not affect its place in the state machine. */
261 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_CERT);
262 EXPECT_SUCCESS(s2n_handshake_read_io(server_conn));
263
264 /* Server reads ClientCert */
265 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_CERT);
266 EXPECT_SUCCESS(s2n_handshake_read_io(server_conn));
267
268 if (no_cert) {
269 EXPECT_EQUAL(server_conn->handshake.handshake_type, NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | NO_CLIENT_CERT | MIDDLEBOX_COMPAT);
270 } else {
271 EXPECT_EQUAL(server_conn->handshake.handshake_type, NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | MIDDLEBOX_COMPAT);
272
273 /* Server reads CertVerify */
274 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_CERT_VERIFY);
275 EXPECT_SUCCESS(s2n_handshake_read_io(server_conn));
276 }
277
278 /* Server reads ClientFinished */
279 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_FINISHED);
280 EXPECT_SUCCESS(s2n_handshake_read_io(server_conn));
281
282 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), APPLICATION_DATA);
283 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), APPLICATION_DATA);
284
285 /* Clean up */
286 EXPECT_SUCCESS(s2n_stuffer_free(&client_to_server));
287 EXPECT_SUCCESS(s2n_stuffer_free(&server_to_client));
288
289 EXPECT_SUCCESS(s2n_connection_free(client_conn));
290 EXPECT_SUCCESS(s2n_connection_free(server_conn));
291
292 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(default_cert));
293 EXPECT_SUCCESS(s2n_config_free(server_config));
294 EXPECT_SUCCESS(s2n_config_free(client_config));
295
296 free(private_key);
297 free(cert_chain);
298
299 return 0;
300 }
301
302 int main(int argc, char **argv)
303 {
304 BEGIN_TEST();
305
306 if (!s2n_is_tls13_fully_supported()) {
307 END_TEST();
308 }
309
310 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
311
312 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL,
313 s2n_cert_chain_and_key_ptr_free);
314 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
315 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
316
317 /* client_auth handshake negotiation */
318 {
319 struct s2n_config *server_config = NULL, *client_config = NULL;
320 uint8_t *cert_chain_pem = NULL;
321 uint8_t *private_key_pem = NULL;
322 uint32_t cert_chain_len = 0;
323 uint32_t private_key_len = 0;
324 struct s2n_cert_chain_and_key *ecdsa_cert = NULL;
325
326 EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
327 EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
328
329 EXPECT_NOT_NULL(server_config = s2n_config_new());
330 EXPECT_NOT_NULL(client_config = s2n_config_new());
331
332 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "20190801"));
333 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "20190801"));
334
335 EXPECT_SUCCESS(s2n_read_test_pem_and_len(S2N_ECDSA_P384_PKCS1_CERT_CHAIN, cert_chain_pem, &cert_chain_len, S2N_MAX_TEST_PEM_SIZE));
336 EXPECT_SUCCESS(s2n_read_test_pem_and_len(S2N_ECDSA_P384_PKCS1_KEY, private_key_pem, &private_key_len, S2N_MAX_TEST_PEM_SIZE));
337 EXPECT_NOT_NULL(ecdsa_cert = s2n_cert_chain_and_key_new());
338 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem_bytes(ecdsa_cert, cert_chain_pem, cert_chain_len, private_key_pem, private_key_len));
339 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, ecdsa_cert));
340
341 EXPECT_SUCCESS(s2n_config_set_verification_ca_location(client_config, S2N_ECDSA_P384_PKCS1_CERT_CHAIN, NULL));
342
343 /* client_auth with no cert */
344 EXPECT_SUCCESS(s2n_test_client_auth_negotiation(server_config, client_config, ecdsa_cert, 1));
345
346 /* client_auth with cert */
347 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, ecdsa_cert));
348 EXPECT_SUCCESS(s2n_test_client_auth_negotiation(server_config, client_config, ecdsa_cert, 0));
349
350 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(ecdsa_cert));
351 EXPECT_SUCCESS(s2n_config_free(server_config));
352 EXPECT_SUCCESS(s2n_config_free(client_config));
353 free(cert_chain_pem);
354 free(private_key_pem);
355 };
356
357 /* Test each message is sent and in the correct order */
358 {
359 /* Test messsage by message with no cert */
360 s2n_test_client_auth_message_by_message(1);
361
362 /* Test message by message with a cert */
363 s2n_test_client_auth_message_by_message(0);
364 };
365
366 /* Test unexpected certificate request */
367 {
368 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
369 s2n_connection_ptr_free);
370 EXPECT_NOT_NULL(client);
371
372 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
373 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
374
375 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
376 s2n_connection_ptr_free);
377 EXPECT_NOT_NULL(server);
378 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
379 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
380
381 /* Enable client auth on the server, but not on the client */
382 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client, S2N_CERT_AUTH_NONE));
383 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server, S2N_CERT_AUTH_OPTIONAL));
384
385 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
386 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
387 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
388
389 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server, client),
390 S2N_ERR_UNEXPECTED_CERT_REQUEST);
391 };
392
393 /* Test missing certificate request */
394 {
395 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
396 s2n_connection_ptr_free);
397 EXPECT_NOT_NULL(client);
398 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
399
400 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
401 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
402
403 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
404 s2n_connection_ptr_free);
405 EXPECT_NOT_NULL(server);
406 EXPECT_SUCCESS(s2n_connection_set_config(server, config));
407
408 /* Require client auth on the client, but not on the server */
409 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client, S2N_CERT_AUTH_REQUIRED));
410 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server, S2N_CERT_AUTH_NONE));
411
412 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
413 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
414 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
415
416 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server, client),
417 S2N_ERR_MISSING_CERT_REQUEST);
418 };
419
420 /* By default, client accepts certificate requests */
421 {
422 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
423 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
424 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
425 s2n_connection_ptr_free);
426 EXPECT_NOT_NULL(client);
427 EXPECT_SUCCESS(s2n_connection_set_config(client, client_config));
428
429 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
430 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
431 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
432 s2n_connection_ptr_free);
433 EXPECT_NOT_NULL(server);
434 EXPECT_SUCCESS(s2n_connection_set_config(server, server_config));
435
436 /* Enable client auth on the server */
437 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server, S2N_CERT_AUTH_OPTIONAL));
438
439 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
440 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
441 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
442
443 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
444 };
445
446 /* Test: all combinations of client and server mutual auth settings produce useful errors
447 *
448 * Customers have struggled to correctly configure client auth in the past.
449 * We should provide very specific and helpful errors to facilitate debugging.
450 * Do not allow any generic errors like S2N_ERR_BAD_MESSAGE.
451 */
452 {
453 const int S2N_CERT_AUTH_DEFAULT = -255;
454 int all_options[] = {
455 S2N_CERT_AUTH_DEFAULT,
456 S2N_CERT_AUTH_NONE,
457 S2N_CERT_AUTH_OPTIONAL,
458 S2N_CERT_AUTH_REQUIRED
459 };
460
461 struct {
462 int client_auth_type;
463 int server_auth_type;
464 bool client_cert_exists;
465 uint8_t version;
466 } test_cases[100] = { 0 };
467 size_t test_case_count = 0;
468
469 for (size_t client_i = 0; client_i < s2n_array_len(all_options); client_i++) {
470 for (size_t server_i = 0; server_i < s2n_array_len(all_options); server_i++) {
471 for (size_t cert_i = 0; cert_i <= 1; cert_i++) {
472 for (size_t version = S2N_TLS12; version <= S2N_TLS13; version++) {
473 EXPECT_TRUE(test_case_count < s2n_array_len(test_cases));
474 test_cases[test_case_count].client_auth_type = all_options[client_i];
475 test_cases[test_case_count].server_auth_type = all_options[server_i];
476 test_cases[test_case_count].client_cert_exists = (cert_i == 1);
477 test_cases[test_case_count].version = version;
478 test_case_count++;
479 }
480 }
481 }
482 }
483
484 for (size_t i = 0; i < test_case_count; i++) {
485 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
486 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(client_config));
487 if (test_cases[i].client_cert_exists) {
488 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(client_config, chain_and_key));
489 }
490
491 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
492 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(server_config));
493 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
494
495 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
496 s2n_connection_ptr_free);
497 EXPECT_NOT_NULL(client);
498 EXPECT_SUCCESS(s2n_connection_set_blinding(client, S2N_SELF_SERVICE_BLINDING));
499 EXPECT_SUCCESS(s2n_connection_set_config(client, client_config));
500
501 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
502 s2n_connection_ptr_free);
503 EXPECT_NOT_NULL(server);
504 EXPECT_SUCCESS(s2n_connection_set_blinding(server, S2N_SELF_SERVICE_BLINDING));
505 EXPECT_SUCCESS(s2n_connection_set_config(server, server_config));
506 server->server_protocol_version = test_cases[i].version;
507
508 if (test_cases[i].client_auth_type != S2N_CERT_AUTH_DEFAULT) {
509 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(client, test_cases[i].client_auth_type));
510 }
511
512 if (test_cases[i].server_auth_type != S2N_CERT_AUTH_DEFAULT) {
513 EXPECT_SUCCESS(s2n_connection_set_client_auth_type(server, test_cases[i].server_auth_type));
514 }
515
516 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
517 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
518 EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair));
519
520 int result = s2n_negotiate_test_server_and_client(server, client);
521 EXPECT_EQUAL(client->actual_protocol_version, test_cases[i].version);
522 EXPECT_EQUAL(server->actual_protocol_version, test_cases[i].version);
523 if (result != S2N_SUCCESS) {
524 EXPECT_TRUE(s2n_errno == S2N_ERR_MISSING_CERT_REQUEST
525 || s2n_errno == S2N_ERR_UNEXPECTED_CERT_REQUEST
526 || s2n_errno == S2N_ERR_MISSING_CLIENT_CERT);
527 }
528 }
529 };
530
531 END_TEST();
532 }