Debian Codegraph @

crt/s2n/tests/unit/s2n_server_max_frag_len_extension_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/extensions/s2n_server_max_fragment_length.h"
18 #include "tls/s2n_tls.h"
19
20 int main(int argc, char **argv)
21 {
22 BEGIN_TEST();
23 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
24
25 /* Test should_send */
26 {
27 struct s2n_config *config = NULL;
28 EXPECT_NOT_NULL(config = s2n_config_new());
29
30 struct s2n_connection *conn = NULL;
31 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
32 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
33
34 /* Should not send by default */
35 EXPECT_FALSE(s2n_server_max_fragment_length_extension.should_send(conn));
36
37 /* Should send if mfl code set. It is set by the client version of this extension. */
38 conn->negotiated_mfl_code = S2N_TLS_MAX_FRAG_LEN_512;
39 EXPECT_TRUE(s2n_server_max_fragment_length_extension.should_send(conn));
40
41 EXPECT_SUCCESS(s2n_connection_free(conn));
42 EXPECT_SUCCESS(s2n_config_free(config));
43 };
44
45 /* Test send */
46 {
47 struct s2n_config *config = NULL;
48 EXPECT_NOT_NULL(config = s2n_config_new());
49 EXPECT_SUCCESS(s2n_config_send_max_fragment_length(config, S2N_TLS_MAX_FRAG_LEN_512));
50
51 struct s2n_connection *conn = NULL;
52 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
53 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
54
55 struct s2n_stuffer stuffer = { 0 };
56 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
57
58 conn->negotiated_mfl_code = S2N_TLS_MAX_FRAG_LEN_512;
59 EXPECT_SUCCESS(s2n_server_max_fragment_length_extension.send(conn, &stuffer));
60
61 /* Should have correct fragment length */
62 uint8_t actual_fragment_length = 0;
63 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&stuffer, &actual_fragment_length));
64 EXPECT_EQUAL(actual_fragment_length, S2N_TLS_MAX_FRAG_LEN_512);
65
66 EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
67
68 EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
69 EXPECT_SUCCESS(s2n_connection_free(conn));
70 EXPECT_SUCCESS(s2n_config_free(config));
71 };
72
73 /* Test receive - does not match requested value
74 *
75 *= https://www.rfc-editor.org/rfc/rfc6066#section-4
76 *= type=test
77 *# Similarly, if a client
78 *# receives a maximum fragment length negotiation response that differs
79 *# from the length it requested, it MUST also abort the handshake with
80 *# an "illegal_parameter" alert.
81 */
82 {
83 struct s2n_config *config = NULL;
84 EXPECT_NOT_NULL(config = s2n_config_new());
85 EXPECT_SUCCESS(s2n_config_send_max_fragment_length(config, S2N_TLS_MAX_FRAG_LEN_512));
86
87 struct s2n_connection *conn = NULL;
88 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
89 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
90
91 struct s2n_stuffer stuffer = { 0 };
92 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
93
94 conn->negotiated_mfl_code = S2N_TLS_MAX_FRAG_LEN_1024;
95 EXPECT_SUCCESS(s2n_server_max_fragment_length_extension.send(conn, &stuffer));
96
97 EXPECT_FAILURE_WITH_ERRNO(s2n_server_max_fragment_length_extension.recv(conn, &stuffer),
98 S2N_ERR_MAX_FRAG_LEN_MISMATCH);
99
100 EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
101 EXPECT_SUCCESS(s2n_connection_free(conn));
102 EXPECT_SUCCESS(s2n_config_free(config));
103 };
104
105 /* Test receive */
106 {
107 struct s2n_config *config = NULL;
108 EXPECT_NOT_NULL(config = s2n_config_new());
109 EXPECT_SUCCESS(s2n_config_send_max_fragment_length(config, S2N_TLS_MAX_FRAG_LEN_512));
110
111 struct s2n_connection *conn = NULL;
112 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
113 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
114
115 struct s2n_stuffer stuffer = { 0 };
116 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
117
118 conn->negotiated_mfl_code = S2N_TLS_MAX_FRAG_LEN_512;
119 EXPECT_SUCCESS(s2n_server_max_fragment_length_extension.send(conn, &stuffer));
120 EXPECT_NOT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
121
122 conn->negotiated_mfl_code = 0;
123 EXPECT_SUCCESS(s2n_server_max_fragment_length_extension.recv(conn, &stuffer));
124 EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
125
126 EXPECT_EQUAL(conn->negotiated_mfl_code, S2N_TLS_MAX_FRAG_LEN_512);
127 EXPECT_EQUAL(conn->max_outgoing_fragment_length, 512);
128
129 EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
130 EXPECT_SUCCESS(s2n_connection_free(conn));
131 EXPECT_SUCCESS(s2n_config_free(config));
132 };
133
134 /* Test receive - existing mfl value */
135 {
136 struct s2n_config *config = NULL;
137 EXPECT_NOT_NULL(config = s2n_config_new());
138 EXPECT_SUCCESS(s2n_config_send_max_fragment_length(config, S2N_TLS_MAX_FRAG_LEN_1024));
139
140 struct s2n_connection *conn = NULL;
141 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
142 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
143
144 struct s2n_stuffer stuffer = { 0 };
145 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
146
147 /* Existing mfl value lower */
148 {
149 conn->max_outgoing_fragment_length = mfl_code_to_length[S2N_TLS_MAX_FRAG_LEN_512];
150 conn->negotiated_mfl_code = S2N_TLS_MAX_FRAG_LEN_1024;
151
152 EXPECT_SUCCESS(s2n_server_max_fragment_length_extension.send(conn, &stuffer));
153 EXPECT_NOT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
154
155 conn->negotiated_mfl_code = 0;
156 EXPECT_SUCCESS(s2n_server_max_fragment_length_extension.recv(conn, &stuffer));
157 EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
158
159 EXPECT_EQUAL(conn->negotiated_mfl_code, S2N_TLS_MAX_FRAG_LEN_1024);
160 EXPECT_EQUAL(conn->max_outgoing_fragment_length, mfl_code_to_length[S2N_TLS_MAX_FRAG_LEN_512]);
161 };
162
163 /* Existing mfl value higher */
164 {
165 conn->max_outgoing_fragment_length = mfl_code_to_length[S2N_TLS_MAX_FRAG_LEN_2048];
166 conn->negotiated_mfl_code = S2N_TLS_MAX_FRAG_LEN_1024;
167
168 EXPECT_SUCCESS(s2n_server_max_fragment_length_extension.send(conn, &stuffer));
169 EXPECT_NOT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
170
171 conn->negotiated_mfl_code = 0;
172 EXPECT_SUCCESS(s2n_server_max_fragment_length_extension.recv(conn, &stuffer));
173 EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
174
175 EXPECT_EQUAL(conn->negotiated_mfl_code, S2N_TLS_MAX_FRAG_LEN_1024);
176 EXPECT_EQUAL(conn->max_outgoing_fragment_length, mfl_code_to_length[S2N_TLS_MAX_FRAG_LEN_1024]);
177 };
178
179 EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
180 EXPECT_SUCCESS(s2n_connection_free(conn));
181 EXPECT_SUCCESS(s2n_config_free(config));
182 };
183
184 END_TEST();
185 }