Debian Codegraph @

crt/s2n/tests/unit/s2n_handshake_partial_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 "s2n_test.h"
17 #include "testlib/s2n_testlib.h"
18 #include "tls/s2n_handshake.h"
19 #include "tls/s2n_record.h"
20 #include "tls/s2n_tls.h"
21
22 static S2N_RESULT s2n_get_test_client_and_server(struct s2n_connection **client_conn, struct s2n_connection **server_conn,
23 struct s2n_test_io_pair *io_pair, struct s2n_config *config)
24 {
25 *client_conn = s2n_connection_new(S2N_CLIENT);
26 RESULT_ENSURE_REF(*client_conn);
27
28 *server_conn = s2n_connection_new(S2N_SERVER);
29 RESULT_GUARD_POSIX(s2n_connection_set_blinding(*server_conn, S2N_SELF_SERVICE_BLINDING));
30 RESULT_ENSURE_REF(*server_conn);
31
32 RESULT_GUARD_POSIX(s2n_connection_set_config(*client_conn, config));
33 RESULT_GUARD_POSIX(s2n_connection_set_config(*server_conn, config));
34
35 RESULT_GUARD_POSIX(s2n_io_pair_init_non_blocking(io_pair));
36 RESULT_GUARD_POSIX(s2n_connections_set_io_pair(*client_conn, *server_conn, io_pair));
37
38 return S2N_RESULT_OK;
39 }
40
41 int main()
42 {
43 BEGIN_TEST();
44
45 if (!s2n_is_tls13_fully_supported()) {
46 END_TEST();
47 }
48
49 const uint8_t test_psk_data[] = "very secret";
50 DEFER_CLEANUP(struct s2n_psk *test_psk = s2n_external_psk_new(), s2n_psk_free);
51 EXPECT_SUCCESS(s2n_psk_set_identity(test_psk, test_psk_data, sizeof(test_psk_data)));
52 EXPECT_SUCCESS(s2n_psk_set_secret(test_psk, test_psk_data, sizeof(test_psk_data)));
53 EXPECT_SUCCESS(s2n_psk_configure_early_data(test_psk, 100, 0x13, 0x01));
54
55 struct s2n_cert_chain_and_key *cert_chain = NULL;
56 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&cert_chain,
57 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
58
59 struct s2n_config *config = s2n_config_new();
60 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, cert_chain));
61 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
62 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
63
64 /* Test s2n_negotiate_until_message */
65 {
66 /* Safety */
67 {
68 struct s2n_connection conn = { 0 };
69 s2n_blocked_status blocked = 0;
70 EXPECT_ERROR_WITH_ERRNO(s2n_negotiate_until_message(NULL, &blocked, CLIENT_HELLO), S2N_ERR_NULL);
71 EXPECT_ERROR_WITH_ERRNO(s2n_negotiate_until_message(&conn, NULL, CLIENT_HELLO), S2N_ERR_NULL);
72 };
73
74 /* If message is never encountered, complete the handshake */
75 {
76 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
77 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
78 EXPECT_OK(s2n_get_test_client_and_server(&client_conn, &server_conn, &io_pair, config));
79
80 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
81 SERVER_KEY));
82 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), APPLICATION_DATA);
83 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), APPLICATION_DATA);
84
85 EXPECT_SUCCESS(s2n_connection_free(client_conn));
86 EXPECT_SUCCESS(s2n_connection_free(server_conn));
87 };
88
89 /* Can stop on a given message */
90 {
91 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
92 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
93 EXPECT_OK(s2n_get_test_client_and_server(&client_conn, &server_conn, &io_pair, config));
94
95 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
96 SERVER_CERT_VERIFY));
97 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_CERT_VERIFY);
98 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_CERT_VERIFY);
99
100 EXPECT_SUCCESS(s2n_connection_free(client_conn));
101 EXPECT_SUCCESS(s2n_connection_free(server_conn));
102 };
103
104 /* Can be called repeatedly */
105 {
106 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
107 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
108 EXPECT_OK(s2n_get_test_client_and_server(&client_conn, &server_conn, &io_pair, config));
109
110 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
111 CLIENT_HELLO));
112 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), CLIENT_HELLO);
113 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_HELLO);
114
115 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
116 CLIENT_HELLO));
117 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), CLIENT_HELLO);
118 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_HELLO);
119
120 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
121 SERVER_HELLO));
122 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_HELLO);
123 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_HELLO);
124
125 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
126 SERVER_HELLO));
127 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), SERVER_HELLO);
128 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), SERVER_HELLO);
129
130 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
131 APPLICATION_DATA));
132 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), APPLICATION_DATA);
133 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), APPLICATION_DATA);
134
135 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
136 APPLICATION_DATA));
137 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), APPLICATION_DATA);
138 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), APPLICATION_DATA);
139
140 EXPECT_SUCCESS(s2n_connection_free(client_conn));
141 EXPECT_SUCCESS(s2n_connection_free(server_conn));
142 };
143
144 /* Can continue as normal after stopping */
145 {
146 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
147 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
148 EXPECT_OK(s2n_get_test_client_and_server(&client_conn, &server_conn, &io_pair, config));
149
150 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
151 CLIENT_FINISHED));
152 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), CLIENT_FINISHED);
153 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), CLIENT_FINISHED);
154
155 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
156 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), APPLICATION_DATA);
157 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), APPLICATION_DATA);
158
159 EXPECT_SUCCESS(s2n_connection_free(client_conn));
160 EXPECT_SUCCESS(s2n_connection_free(server_conn));
161 };
162
163 /* Can stop on END_OF_EARLY_DATA when using early data, then continue.
164 * (This is the non-test use case for this feature) */
165 {
166 struct s2n_connection *client_conn = NULL, *server_conn = NULL;
167 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
168 EXPECT_OK(s2n_get_test_client_and_server(&client_conn, &server_conn, &io_pair, config));
169
170 EXPECT_SUCCESS(s2n_connection_append_psk(client_conn, test_psk));
171 EXPECT_SUCCESS(s2n_connection_append_psk(server_conn, test_psk));
172 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
173 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(server_conn));
174
175 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
176 END_OF_EARLY_DATA));
177 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), END_OF_EARLY_DATA);
178 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), END_OF_EARLY_DATA);
179
180 EXPECT_SUCCESS(s2n_connection_set_end_of_early_data(client_conn));
181 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
182 EXPECT_EQUAL(s2n_conn_get_current_message_type(client_conn), APPLICATION_DATA);
183 EXPECT_EQUAL(s2n_conn_get_current_message_type(server_conn), APPLICATION_DATA);
184
185 EXPECT_TRUE(WITH_EARLY_DATA(client_conn));
186 EXPECT_TRUE(WITH_EARLY_DATA(server_conn));
187
188 EXPECT_SUCCESS(s2n_connection_free(client_conn));
189 EXPECT_SUCCESS(s2n_connection_free(server_conn));
190 };
191 };
192
193 EXPECT_SUCCESS(s2n_config_free(config));
194 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(cert_chain));
195 END_TEST();
196 }