Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_min_protocol_version_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 <stdint.h>
17
18 #include "api/s2n.h"
19 #include "s2n_test.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/s2n_connection.h"
22
23 int main(int argc, char **argv)
24 {
25 char cert_chain_pem[S2N_MAX_TEST_PEM_SIZE];
26 char private_key_pem[S2N_MAX_TEST_PEM_SIZE];
27
28 BEGIN_TEST();
29
30 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
31 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
32
33 /* TLS1.2 and TLS1.3 have different version negotiation mechanisms.
34 * We should test both.
35 */
36 for (uint8_t version = S2N_TLS12; version <= S2N_TLS13; version++) {
37 /* Set up server config with TLS 1.2 as minimum version */
38 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(), s2n_config_ptr_free);
39 EXPECT_NOT_NULL(server_config);
40 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(),
41 s2n_cert_chain_and_key_ptr_free);
42 EXPECT_NOT_NULL(chain_and_key);
43 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem));
44 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
45 /* Pick cipher preference with TLSv1.2 as a minimum version */
46 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(server_config, "CloudFront-TLS-1-2-2019"));
47
48 /* Set up client config forcing TLSv1.0 */
49 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(), s2n_config_ptr_free);
50 EXPECT_NOT_NULL(client_config);
51 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
52 if (version >= S2N_TLS13) {
53 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all"));
54 } else {
55 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(client_config, "test_all_tls12"));
56 }
57
58 /* Set up server connection */
59 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
60 s2n_connection_ptr_free);
61 EXPECT_NOT_NULL(server_conn);
62 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
63 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
64
65 /* Set up client connection forcing TLSv1.0 */
66 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
67 s2n_connection_ptr_free);
68 EXPECT_NOT_NULL(client_conn);
69 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
70 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
71 /* Force TLSv1.0 on client so that server will fail handshake */
72 client_conn->client_protocol_version = S2N_TLS10;
73
74 /* Use in-memory IO stuffer pair */
75 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
76 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
77 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client_conn, server_conn, &io_pair));
78
79 /* Negotiate the handshake — expect failure due to unsupported version */
80 EXPECT_FAILURE_WITH_ERRNO(
81 s2n_negotiate_test_server_and_client(server_conn, client_conn),
82 S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
83
84 /* Check that blinding was not invoked */
85 EXPECT_EQUAL(s2n_connection_get_delay(server_conn), 0);
86 }
87
88 END_TEST();
89 }