Debian Codegraph @

crt/s2n/tests/unit/s2n_client_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_client_max_frag_len.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 EXPECT_SUCCESS(s2n_config_send_max_fragment_length(config, S2N_TLS_MAX_FRAG_LEN_EXT_NONE));
35 EXPECT_FALSE(s2n_client_max_frag_len_extension.should_send(conn));
36
37 EXPECT_SUCCESS(s2n_config_send_max_fragment_length(config, S2N_TLS_MAX_FRAG_LEN_512));
38 EXPECT_TRUE(s2n_client_max_frag_len_extension.should_send(conn));
39
40 EXPECT_SUCCESS(s2n_connection_free(conn));
41 EXPECT_SUCCESS(s2n_config_free(config));
42 };
43
44 /* Test send */
45 {
46 struct s2n_config *config = NULL;
47 EXPECT_NOT_NULL(config = s2n_config_new());
48
49 struct s2n_connection *conn = NULL;
50 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
51 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
52
53 struct s2n_stuffer stuffer = { 0 };
54 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
55
56 EXPECT_SUCCESS(s2n_config_send_max_fragment_length(config, S2N_TLS_MAX_FRAG_LEN_512));
57 EXPECT_SUCCESS(s2n_client_max_frag_len_extension.send(conn, &stuffer));
58
59 /* Should have correct fragment length */
60 uint8_t actual_frag_len = 0;
61 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&stuffer, &actual_frag_len));
62 EXPECT_EQUAL(actual_frag_len, S2N_TLS_MAX_FRAG_LEN_512);
63 EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
64
65 EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
66 EXPECT_SUCCESS(s2n_connection_free(conn));
67 EXPECT_SUCCESS(s2n_config_free(config));
68 };
69
70 /* Test receive - accept_mfl not set */
71 {
72 struct s2n_config *config = NULL;
73 EXPECT_NOT_NULL(config = s2n_config_new());
74
75 struct s2n_connection *conn = NULL;
76 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
77 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
78
79 struct s2n_stuffer stuffer = { 0 };
80 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
81
82 EXPECT_SUCCESS(s2n_config_send_max_fragment_length(config, S2N_TLS_MAX_FRAG_LEN_512));
83 EXPECT_SUCCESS(s2n_client_max_frag_len_extension.send(conn, &stuffer));
84
85 /* Ignore fragment length if not accepting max fragment length */
86 EXPECT_FALSE(config->accept_mfl);
87 EXPECT_SUCCESS(s2n_client_max_frag_len_extension.recv(conn, &stuffer));
88 EXPECT_EQUAL(conn->negotiated_mfl_code, S2N_TLS_MAX_FRAG_LEN_EXT_NONE);
89 EXPECT_EQUAL(conn->max_outgoing_fragment_length, S2N_DEFAULT_FRAGMENT_LENGTH);
90
91 EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
92 EXPECT_SUCCESS(s2n_connection_free(conn));
93 EXPECT_SUCCESS(s2n_config_free(config));
94 };
95
96 /* Test receive - invalid mfl code
97 *
98 *= https://www.rfc-editor.org/rfc/rfc6066#section-4
99 *= type=test
100 *# If a server receives a maximum fragment length negotiation request
101 *# for a value other than the allowed values, it MUST abort the
102 *# handshake with an "illegal_parameter" alert.
103 */
104 {
105 struct s2n_config *config = NULL;
106 EXPECT_NOT_NULL(config = s2n_config_new());
107 EXPECT_SUCCESS(s2n_config_accept_max_fragment_length(config));
108
109 struct s2n_connection *conn = NULL;
110 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
111 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
112
113 struct s2n_stuffer stuffer = { 0 };
114 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
115
116 /* Send invalid mfl code */
117 conn->config->mfl_code = UINT8_MAX;
118 EXPECT_SUCCESS(s2n_client_max_frag_len_extension.send(conn, &stuffer));
119
120 /* Ignore invalid mfl code */
121 EXPECT_SUCCESS(s2n_client_max_frag_len_extension.recv(conn, &stuffer));
122 EXPECT_EQUAL(conn->negotiated_mfl_code, S2N_TLS_MAX_FRAG_LEN_EXT_NONE);
123 EXPECT_EQUAL(conn->max_outgoing_fragment_length, S2N_DEFAULT_FRAGMENT_LENGTH);
124
125 EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
126 EXPECT_SUCCESS(s2n_connection_free(conn));
127 EXPECT_SUCCESS(s2n_config_free(config));
128 };
129
130 /* Test receive */
131 {
132 struct s2n_config *config = NULL;
133 EXPECT_NOT_NULL(config = s2n_config_new());
134 EXPECT_SUCCESS(s2n_config_accept_max_fragment_length(config));
135
136 struct s2n_connection *conn = NULL;
137 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
138 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
139
140 struct s2n_stuffer stuffer = { 0 };
141 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
142
143 EXPECT_SUCCESS(s2n_config_send_max_fragment_length(conn->config, S2N_TLS_MAX_FRAG_LEN_512));
144 EXPECT_SUCCESS(s2n_client_max_frag_len_extension.send(conn, &stuffer));
145
146 /* Accept valid mfl code */
147 EXPECT_SUCCESS(s2n_client_max_frag_len_extension.recv(conn, &stuffer));
148 EXPECT_EQUAL(conn->negotiated_mfl_code, S2N_TLS_MAX_FRAG_LEN_512);
149 EXPECT_EQUAL(conn->max_outgoing_fragment_length, mfl_code_to_length[S2N_TLS_MAX_FRAG_LEN_512]);
150 EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
151
152 EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
153 EXPECT_SUCCESS(s2n_connection_free(conn));
154 EXPECT_SUCCESS(s2n_config_free(config));
155 };
156
157 END_TEST();
158 }