Debian Codegraph @

crt/s2n/tests/unit/s2n_send_key_update_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 #include <time.h>
18 #include <unistd.h>
19
20 #include "api/s2n.h"
21 #include "s2n_test.h"
22 #include "testlib/s2n_testlib.h"
23 #include "tls/s2n_connection.h"
24 #include "tls/s2n_post_handshake.h"
25 #include "tls/s2n_record.h"
26 #include "tls/s2n_tls.h"
27 #include "tls/s2n_tls13.h"
28 #include "tls/s2n_tls13_handshake.h"
29
30 int s2n_key_update_write(struct s2n_blob *out);
31
32 static int s2n_test_init_encryption(struct s2n_connection *conn)
33 {
34 struct s2n_cipher_suite *cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
35 conn->server->cipher_suite = cipher_suite;
36 conn->client->cipher_suite = cipher_suite;
37 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
38
39 /* Just some data that's the right length */
40 S2N_BLOB_FROM_HEX(key, "0123456789abcdef0123456789abcdef");
41 S2N_BLOB_FROM_HEX(iv, "0123456789abcdef01234567");
42 S2N_BLOB_FROM_HEX(application_secret,
43 "4bc28934ddd802b00f479e14a72d7725dab45d32b3b145f29"
44 "e4c5b56677560eb5236b168c71c5c75aa52f3e20ee89bfb");
45
46 struct s2n_session_key *server_session_key = &conn->server->server_key;
47 struct s2n_session_key *client_session_key = &conn->server->server_key;
48 uint8_t *server_implicit_iv = conn->server->server_implicit_iv;
49 uint8_t *client_implicit_iv = conn->client->client_implicit_iv;
50
51 /* Initialize record algorithm */
52 POSIX_GUARD_RESULT(cipher_suite->record_alg->cipher->init(server_session_key));
53 POSIX_GUARD_RESULT(cipher_suite->record_alg->cipher->init(client_session_key));
54 POSIX_GUARD_RESULT(cipher_suite->record_alg->cipher->set_encryption_key(server_session_key, &key));
55 POSIX_GUARD_RESULT(cipher_suite->record_alg->cipher->set_encryption_key(client_session_key, &key));
56 POSIX_GUARD_RESULT(cipher_suite->record_alg->cipher->set_decryption_key(server_session_key, &key));
57 POSIX_GUARD_RESULT(cipher_suite->record_alg->cipher->set_decryption_key(client_session_key, &key));
58
59 /* Initialized secrets */
60 POSIX_CHECKED_MEMCPY(conn->secrets.version.tls13.server_app_secret, application_secret.data, application_secret.size);
61 POSIX_CHECKED_MEMCPY(conn->secrets.version.tls13.client_app_secret, application_secret.data, application_secret.size);
62
63 /* Copy iv bytes from input data */
64 POSIX_CHECKED_MEMCPY(server_implicit_iv, iv.data, iv.size);
65 POSIX_CHECKED_MEMCPY(client_implicit_iv, iv.data, iv.size);
66
67 return S2N_SUCCESS;
68 }
69
70 int main(int argc, char **argv)
71 {
72 BEGIN_TEST();
73 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
74
75 /* The maximum record number converted to base 256 */
76 uint8_t max_record_limit[S2N_TLS_SEQUENCE_NUM_LEN] = { 0, 0, 0, 0, 1, 106, 9, 229 };
77
78 /* s2n_send sends key update if necessary */
79 {
80 struct s2n_connection *server_conn = NULL;
81 struct s2n_connection *client_conn = NULL;
82 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
83 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
84 server_conn->actual_protocol_version = S2N_TLS13;
85 client_conn->actual_protocol_version = S2N_TLS13;
86
87 uint8_t zero_sequence_number[S2N_TLS_SEQUENCE_NUM_LEN] = { 0 };
88
89 EXPECT_SUCCESS(s2n_test_init_encryption(server_conn));
90 EXPECT_SUCCESS(s2n_test_init_encryption(client_conn));
91
92 DEFER_CLEANUP(struct s2n_stuffer input, s2n_stuffer_free);
93 DEFER_CLEANUP(struct s2n_stuffer output, s2n_stuffer_free);
94 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&input, 0));
95 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&output, 0));
96
97 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&input, &output, server_conn));
98 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&output, &input, client_conn));
99
100 /* Mimic key update send conditions */
101 for (size_t i = 0; i < S2N_TLS_SEQUENCE_NUM_LEN; i++) {
102 server_conn->secure->server_sequence_number[i] = max_record_limit[i];
103 }
104
105 /* Next message to send will trigger key update message*/
106 s2n_blocked_status blocked;
107 char message[] = "sent message";
108 EXPECT_SUCCESS(s2n_send(server_conn, message, sizeof(message), &blocked));
109
110 /* Verify key update happened */
111 EXPECT_BYTEARRAY_NOT_EQUAL(server_conn->secrets.version.tls13.server_app_secret, client_conn->secrets.version.tls13.server_app_secret, S2N_TLS13_SECRET_MAX_LEN);
112 EXPECT_BYTEARRAY_EQUAL(server_conn->secure->server_sequence_number, zero_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN);
113
114 /* Receive keyupdate message */
115 uint8_t data[100];
116 EXPECT_SUCCESS(s2n_recv(client_conn, data, sizeof(message), &blocked));
117 EXPECT_BYTEARRAY_EQUAL(data, message, sizeof(message));
118 EXPECT_BYTEARRAY_EQUAL(client_conn->secrets.version.tls13.server_app_secret, server_conn->secrets.version.tls13.server_app_secret, S2N_TLS13_SECRET_MAX_LEN);
119 EXPECT_BYTEARRAY_EQUAL(client_conn->secure->server_sequence_number, zero_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN);
120
121 EXPECT_SUCCESS(s2n_connection_free(server_conn));
122 EXPECT_SUCCESS(s2n_connection_free(client_conn));
123 };
124
125 /* TLS 1.2 Server that receives TLS 1.3 KeyUpdate from Client should close connection */
126 {
127 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
128
129 char *cert_chain = NULL;
130 char *private_key = NULL;
131 EXPECT_NOT_NULL(cert_chain = malloc(S2N_MAX_TEST_PEM_SIZE));
132 EXPECT_NOT_NULL(private_key = malloc(S2N_MAX_TEST_PEM_SIZE));
133 EXPECT_SUCCESS(setenv("S2N_DONT_MLOCK", "1", 0));
134 struct s2n_test_io_pair io_pair;
135 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
136
137 struct s2n_connection *client_conn = NULL;
138 struct s2n_connection *server_conn = NULL;
139 struct s2n_config *server_config = NULL;
140 struct s2n_cert_chain_and_key *chain_and_key = NULL;
141 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
142
143 struct s2n_config *client_config = NULL;
144 EXPECT_NOT_NULL(client_config = s2n_config_new());
145 EXPECT_OK(s2n_config_set_tls12_security_policy(client_config));
146 EXPECT_SUCCESS(s2n_config_set_check_stapled_ocsp_response(client_config, 0));
147 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
148 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
149 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
150 client_conn->actual_protocol_version = S2N_TLS12;
151 client_conn->server_protocol_version = S2N_TLS12;
152 client_conn->client_protocol_version = S2N_TLS12;
153
154 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
155 server_conn->actual_protocol_version = S2N_TLS12;
156 server_conn->server_protocol_version = S2N_TLS12;
157 server_conn->client_protocol_version = S2N_TLS12;
158
159 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
160
161 EXPECT_NOT_NULL(server_config = s2n_config_new());
162 EXPECT_OK(s2n_config_set_tls12_security_policy(server_config));
163 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain, S2N_MAX_TEST_PEM_SIZE));
164 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key, S2N_MAX_TEST_PEM_SIZE));
165 EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
166 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain, private_key));
167 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
168 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
169
170 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
171
172 /* Force Client to send a TLS 1.3 KeyUpdate Message over TLS 1.2 connection */
173 uint8_t key_update_data[S2N_KEY_UPDATE_MESSAGE_SIZE] = { 0 };
174 struct s2n_blob key_update_blob = { 0 };
175 EXPECT_SUCCESS(s2n_blob_init(&key_update_blob, key_update_data, sizeof(key_update_data)));
176 EXPECT_SUCCESS(s2n_key_update_write(&key_update_blob));
177 EXPECT_OK(s2n_record_write(client_conn, TLS_HANDSHAKE, &key_update_blob));
178 EXPECT_SUCCESS(s2n_flush(client_conn, &blocked));
179
180 /* Attempt to recv on Server conn, see KeyUpdate Message, and confirm connection is closed. */
181 uint8_t server_message[128];
182 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
183 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server_conn, server_message, sizeof(server_message), &blocked), S2N_ERR_BAD_MESSAGE);
184
185 EXPECT_SUCCESS(s2n_connection_free(server_conn));
186 EXPECT_SUCCESS(s2n_connection_free(client_conn));
187 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
188 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
189 EXPECT_SUCCESS(s2n_config_free(server_config));
190 EXPECT_SUCCESS(s2n_config_free(client_config));
191 free(cert_chain);
192 free(private_key);
193 };
194
195 END_TEST();
196 }