Debian Codegraph @

crt/s2n/tests/unit/s2n_change_cipher_spec_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 "tls/s2n_cipher_suites.h"
18 #include "tls/s2n_connection.h"
19 #include "tls/s2n_crypto.h"
20 #include "tls/s2n_tls.h"
21
22 int main(int argc, char **argv)
23 {
24 BEGIN_TEST();
25 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
26
27 uint8_t empty_finished_array[S2N_TLS_FINISHED_LEN] = { 0 };
28
29 /* Test s2n_ccs_send */
30 {
31 struct s2n_connection *conn = NULL;
32 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
33
34 EXPECT_SUCCESS(s2n_ccs_send(conn));
35
36 uint8_t result = 0;
37 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&conn->handshake.io, &result));
38 /* Always 0x01: https://tools.ietf.org/html/rfc5246#section-7.1 */
39 EXPECT_EQUAL(result, 0x01);
40
41 EXPECT_SUCCESS(s2n_connection_free(conn));
42 };
43
44 /* Test that s2n_basic_ccs_recv can parse the output of s2n_change_cipher_spec_send */
45 {
46 struct s2n_connection *conn = NULL;
47 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
48
49 EXPECT_SUCCESS(s2n_ccs_send(conn));
50 EXPECT_SUCCESS(s2n_basic_ccs_recv(conn));
51
52 EXPECT_SUCCESS(s2n_connection_free(conn));
53 };
54
55 /* Test that s2n_basic_ccs_recv errors on wrong change cipher spec types */
56 {
57 struct s2n_connection *conn = NULL;
58 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
59
60 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->handshake.io, 0));
61 EXPECT_FAILURE_WITH_ERRNO(s2n_basic_ccs_recv(conn), S2N_ERR_BAD_MESSAGE);
62
63 EXPECT_SUCCESS(s2n_connection_free(conn));
64 };
65
66 /* Test that s2n_client_ccs_recv errors on wrong change cipher spec types */
67 {
68 struct s2n_connection *conn = NULL;
69 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
70
71 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->handshake.io, 0));
72 EXPECT_FAILURE_WITH_ERRNO(s2n_client_ccs_recv(conn), S2N_ERR_BAD_MESSAGE);
73
74 EXPECT_SUCCESS(s2n_connection_free(conn));
75 };
76
77 /* Test that s2n_server_ccs_recv errors on wrong change cipher spec types */
78 {
79 struct s2n_connection *conn = NULL;
80 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
81
82 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->handshake.io, 0));
83 EXPECT_FAILURE_WITH_ERRNO(s2n_server_ccs_recv(conn), S2N_ERR_BAD_MESSAGE);
84
85 EXPECT_SUCCESS(s2n_connection_free(conn));
86 };
87
88 /* Test s2n_client_ccs_recv */
89 {
90 struct s2n_connection *conn = NULL;
91 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
92
93 /* Needed to not break prf */
94 conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
95
96 /* Check preconditions */
97 conn->secure->client_sequence_number[0] = 1;
98 EXPECT_BYTEARRAY_EQUAL(&conn->handshake.client_finished, &empty_finished_array, S2N_TLS_FINISHED_LEN);
99 EXPECT_EQUAL(conn->handshake.finished_len, 0);
100 EXPECT_NOT_EQUAL(conn->client, conn->secure);
101 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->alert_in, 1));
102
103 EXPECT_SUCCESS(s2n_ccs_send(conn));
104 EXPECT_SUCCESS(s2n_client_ccs_recv(conn));
105
106 /* Check for expected updates */
107 EXPECT_EQUAL(conn->secure->client_sequence_number[0], 0);
108 EXPECT_EQUAL(conn->client, conn->secure);
109 EXPECT_FALSE(s2n_stuffer_data_available(&conn->alert_in));
110
111 EXPECT_SUCCESS(s2n_connection_free(conn));
112 };
113
114 /* Test SSLv3 s2n_client_ccs_recv */
115 {
116 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
117 EXPECT_NOT_NULL(conn);
118 conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_128_cbc_sha;
119 conn->actual_protocol_version = S2N_SSLv3;
120
121 /* Check preconditions */
122 conn->secure->client_sequence_number[0] = 1;
123 EXPECT_BYTEARRAY_EQUAL(&conn->handshake.client_finished, &empty_finished_array, S2N_TLS_FINISHED_LEN);
124 EXPECT_EQUAL(conn->handshake.finished_len, 0);
125 EXPECT_NOT_EQUAL(conn->client, conn->secure);
126 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->alert_in, 1));
127
128 EXPECT_SUCCESS(s2n_ccs_send(conn));
129 EXPECT_SUCCESS(s2n_client_ccs_recv(conn));
130
131 /* Check for expected updates */
132 EXPECT_EQUAL(conn->secure->client_sequence_number[0], 0);
133 EXPECT_EQUAL(conn->client, conn->secure);
134 EXPECT_FALSE(s2n_stuffer_data_available(&conn->alert_in));
135 };
136
137 /* Test s2n_server_ccs_recv */
138 {
139 struct s2n_connection *conn = NULL;
140 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
141
142 /* Needed to not break prf */
143 conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
144
145 /* Check preconditions */
146 conn->secure->server_sequence_number[0] = 1;
147 EXPECT_BYTEARRAY_EQUAL(&conn->handshake.server_finished, &empty_finished_array, S2N_TLS_FINISHED_LEN);
148 EXPECT_EQUAL(conn->handshake.finished_len, 0);
149 EXPECT_NOT_EQUAL(conn->server, conn->secure);
150 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->alert_in, 1));
151
152 EXPECT_SUCCESS(s2n_ccs_send(conn));
153 EXPECT_SUCCESS(s2n_server_ccs_recv(conn));
154
155 /* Check for expected updates */
156 EXPECT_EQUAL(conn->secure->server_sequence_number[0], 0);
157 EXPECT_BYTEARRAY_NOT_EQUAL(&conn->handshake.server_finished, &empty_finished_array, S2N_TLS_FINISHED_LEN);
158 EXPECT_EQUAL(conn->handshake.finished_len, S2N_TLS_FINISHED_LEN);
159 EXPECT_EQUAL(conn->server, conn->secure);
160 EXPECT_FALSE(s2n_stuffer_data_available(&conn->alert_in));
161
162 EXPECT_SUCCESS(s2n_connection_free(conn));
163 };
164
165 /* Test SSLv3 s2n_server_ccs_recv */
166 {
167 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
168 EXPECT_NOT_NULL(conn);
169 conn->secure->cipher_suite = &s2n_ecdhe_rsa_with_aes_128_cbc_sha;
170 conn->actual_protocol_version = S2N_SSLv3;
171
172 /* Check preconditions */
173 conn->secure->server_sequence_number[0] = 1;
174 EXPECT_BYTEARRAY_EQUAL(&conn->handshake.server_finished, &empty_finished_array, S2N_TLS_FINISHED_LEN);
175 EXPECT_EQUAL(conn->handshake.finished_len, 0);
176 EXPECT_NOT_EQUAL(conn->server, conn->secure);
177 EXPECT_SUCCESS(s2n_stuffer_write_uint8(&conn->alert_in, 1));
178
179 EXPECT_SUCCESS(s2n_ccs_send(conn));
180 EXPECT_SUCCESS(s2n_server_ccs_recv(conn));
181
182 /* Check for expected updates */
183 EXPECT_EQUAL(conn->secure->server_sequence_number[0], 0);
184 EXPECT_BYTEARRAY_NOT_EQUAL(&conn->handshake.server_finished, &empty_finished_array, S2N_TLS_FINISHED_LEN);
185 EXPECT_EQUAL(conn->handshake.finished_len, S2N_SSL_FINISHED_LEN);
186 EXPECT_EQUAL(conn->server, conn->secure);
187 EXPECT_FALSE(s2n_stuffer_data_available(&conn->alert_in));
188 };
189
190 END_TEST();
191 }