File: s2n_self_talk_io_mem_test.c

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (298 lines) | stat: -rw-r--r-- 14,526 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

#include <stdint.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

#include "api/s2n.h"
#include "s2n_test.h"
#include "testlib/s2n_testlib.h"
#include "tls/s2n_connection.h"
#include "tls/s2n_handshake.h"
#include "tls/s2n_tls13.h"

int main(int argc, char **argv)
{
    BEGIN_TEST();

    if (!s2n_is_tls13_fully_supported()) {
        END_TEST();
    }

    struct s2n_cert_chain_and_key *chain_and_key;
    EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
            S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));

    struct s2n_config *config = s2n_config_new();
    EXPECT_NOT_NULL(config);
    EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
    EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
    EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));

    /* Allocate output buffer based on default fragment length */
    {
        struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
        EXPECT_NOT_NULL(client_conn);
        EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));

        struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
        EXPECT_NOT_NULL(server_conn);
        EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));

        /* Create nonblocking pipes */
        struct s2n_test_io_pair io_pair;
        EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
        EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
        EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));

        /* Do handshake */
        EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, SERVER_CERT));
        EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
        EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);

        /* Output memory allocated according to max fragment length. */
        EXPECT_EQUAL(client_conn->max_outgoing_fragment_length, S2N_DEFAULT_FRAGMENT_LENGTH);
        EXPECT_EQUAL(server_conn->max_outgoing_fragment_length, S2N_DEFAULT_FRAGMENT_LENGTH);
        /* The client allocates the protocol-agnostic max record size because when it sends its
         * first message (ClientHello) the protocol hasn't been negotiated yet. */
        EXPECT_EQUAL(client_conn->out.blob.size, S2N_TLS_MAX_RECORD_LEN_FOR(S2N_DEFAULT_FRAGMENT_LENGTH));
        /* The server allocates only enough memory for TLS1.3 records because when it sends its
         * first message (ServerHello) the protocol has already been negotiated. */
        EXPECT_EQUAL(server_conn->out.blob.size, S2N_TLS13_MAX_RECORD_LEN_FOR(S2N_DEFAULT_FRAGMENT_LENGTH));

        EXPECT_SUCCESS(s2n_connection_free(client_conn));
        EXPECT_SUCCESS(s2n_connection_free(server_conn));
    };

    /* Allocate output buffer to max fragment size set manually */
    {
        struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
        EXPECT_NOT_NULL(client_conn);
        EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));

        struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
        EXPECT_NOT_NULL(server_conn);
        EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));

        /* Create nonblocking pipes */
        struct s2n_test_io_pair io_pair;
        EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
        EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
        EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));

        /* Set connections to use different fragment sizes */
        EXPECT_SUCCESS(s2n_connection_prefer_low_latency(client_conn));
        EXPECT_SUCCESS(s2n_connection_prefer_throughput(server_conn));

        /* Do handshake */
        EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, SERVER_CERT));
        EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
        EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);

        /* Output memory allocated according to max fragment length. */
        EXPECT_EQUAL(client_conn->max_outgoing_fragment_length, S2N_SMALL_FRAGMENT_LENGTH);
        EXPECT_EQUAL(server_conn->max_outgoing_fragment_length, S2N_LARGE_FRAGMENT_LENGTH);
        EXPECT_EQUAL(client_conn->out.blob.size, S2N_TLS_MAX_RECORD_LEN_FOR(S2N_SMALL_FRAGMENT_LENGTH));
        EXPECT_EQUAL(server_conn->out.blob.size, S2N_TLS13_MAX_RECORD_LEN_FOR(S2N_LARGE_FRAGMENT_LENGTH));

        /* Switch max fragment lengths after handshake to verify whether buffers are resized */
        EXPECT_SUCCESS(s2n_connection_prefer_throughput(client_conn));
        EXPECT_SUCCESS(s2n_connection_prefer_low_latency(server_conn));
        EXPECT_EQUAL(client_conn->max_outgoing_fragment_length, S2N_LARGE_FRAGMENT_LENGTH);
        EXPECT_EQUAL(server_conn->max_outgoing_fragment_length, S2N_SMALL_FRAGMENT_LENGTH);
        EXPECT_EQUAL(client_conn->out.blob.size, S2N_TLS13_MAX_RECORD_LEN_FOR(S2N_LARGE_FRAGMENT_LENGTH));
        EXPECT_EQUAL(server_conn->out.blob.size, S2N_TLS13_MAX_RECORD_LEN_FOR(S2N_LARGE_FRAGMENT_LENGTH));

        EXPECT_SUCCESS(s2n_connection_free(client_conn));
        EXPECT_SUCCESS(s2n_connection_free(server_conn));
    };

    /* Allocate output buffer to value negotiated with max_fragment_length extension */
    {
        const s2n_max_frag_len mfl_code = S2N_TLS_MAX_FRAG_LEN_2048;
        const uint16_t expected_mfl = 2048;

        struct s2n_config *config_for_mfl = s2n_config_new();
        EXPECT_NOT_NULL(config_for_mfl);
        EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config_for_mfl, "default_tls13"));
        EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config_for_mfl));
        EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config_for_mfl, chain_and_key));
        EXPECT_SUCCESS(s2n_config_send_max_fragment_length(config_for_mfl, mfl_code));
        EXPECT_SUCCESS(s2n_config_accept_max_fragment_length(config_for_mfl));

        struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
        EXPECT_NOT_NULL(client_conn);
        EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config_for_mfl));

        struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
        EXPECT_NOT_NULL(server_conn);
        EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config_for_mfl));

        /* Create nonblocking pipes */
        struct s2n_test_io_pair io_pair;
        EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
        EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
        EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));

        /* Set connections to use fragment size larger than what will be negotiated
         * via the max_fragment_length extension */
        EXPECT_SUCCESS(s2n_connection_prefer_throughput(client_conn));
        EXPECT_SUCCESS(s2n_connection_prefer_throughput(server_conn));

        /* Do handshake */
        EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, SERVER_CERT));
        EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
        EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);

        /* Output memory allocated according to max fragment length. */
        EXPECT_EQUAL(client_conn->max_outgoing_fragment_length, expected_mfl);
        EXPECT_EQUAL(server_conn->max_outgoing_fragment_length, expected_mfl);
        /* The client allocates enough memory for the initial large max_fragment_length, because the
         * final smaller max_fragment_length has not be negotiated yet when its first message (the ClientHello) is sent. */
        EXPECT_EQUAL(client_conn->out.blob.size, S2N_TLS_MAX_RECORD_LEN_FOR(S2N_LARGE_FRAGMENT_LENGTH));
        /* The server only allocates enough memory for the negotiated small max_fragment_length, because the
         * max_fragment_length is negotiated before its first message (the ServerHello) is sent. */
        EXPECT_EQUAL(server_conn->out.blob.size, S2N_TLS13_MAX_RECORD_LEN_FOR(expected_mfl));

        EXPECT_SUCCESS(s2n_connection_free(client_conn));
        EXPECT_SUCCESS(s2n_connection_free(server_conn));
        EXPECT_SUCCESS(s2n_config_free(config_for_mfl));
    };

    /* Output and input buffers both freed on connection wipe */
    {
        struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
        EXPECT_NOT_NULL(client_conn);
        EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));

        struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
        EXPECT_NOT_NULL(server_conn);
        EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));

        /* All IO buffers empty */
        EXPECT_EQUAL(client_conn->in.blob.size, 0);
        EXPECT_EQUAL(server_conn->in.blob.size, 0);
        EXPECT_EQUAL(client_conn->out.blob.size, 0);
        EXPECT_EQUAL(server_conn->out.blob.size, 0);

        /* Create nonblocking pipes */
        struct s2n_test_io_pair io_pair;
        EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
        EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
        EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));

        /* Do handshake */
        EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, SERVER_CERT));

        /* All IO buffers not empty */
        EXPECT_NOT_EQUAL(client_conn->in.blob.size, 0);
        EXPECT_NOT_EQUAL(server_conn->in.blob.size, 0);
        EXPECT_NOT_EQUAL(client_conn->out.blob.size, 0);
        EXPECT_NOT_EQUAL(server_conn->out.blob.size, 0);

        /* Wipe connections */
        EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
        EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
        EXPECT_EQUAL(client_conn->in.blob.size, 0);
        EXPECT_EQUAL(server_conn->in.blob.size, 0);
        EXPECT_EQUAL(client_conn->out.blob.size, 0);
        EXPECT_EQUAL(server_conn->out.blob.size, 0);

        EXPECT_SUCCESS(s2n_connection_free(client_conn));
        EXPECT_SUCCESS(s2n_connection_free(server_conn));
    };

    /* Test that dynamic buffers work correctly */
    {
        DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
        EXPECT_NOT_NULL(client_conn);
        EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));

        DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
        EXPECT_NOT_NULL(server_conn);
        EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));

        /* Enable the dynamic buffers setting */
        EXPECT_SUCCESS(s2n_connection_set_dynamic_buffers(client_conn, true));
        EXPECT_SUCCESS(s2n_connection_set_dynamic_buffers(server_conn, true));

        /* Configure the connection to use stuffers instead of fds.
         * This will let us block the send.
         */
        DEFER_CLEANUP(struct s2n_stuffer client_in = { 0 }, s2n_stuffer_free);
        DEFER_CLEANUP(struct s2n_stuffer client_out = { 0 }, s2n_stuffer_free);
        EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_in, 0));
        EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_out, 0));
        EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_in, &client_out, client_conn));
        EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_out, &client_in, server_conn));

        /* Do handshake */
        EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));

        /* all IO buffers should be empty after the handshake */
        EXPECT_EQUAL(client_conn->in.blob.size, 0);
        EXPECT_EQUAL(client_conn->out.blob.size, 0);
        EXPECT_EQUAL(server_conn->in.blob.size, 0);
        EXPECT_EQUAL(server_conn->out.blob.size, 0);

        /* block the server from sending */
        EXPECT_SUCCESS(s2n_stuffer_free(&client_in));

        s2n_blocked_status blocked = 0;
        int send_status = S2N_SUCCESS;

        /* choose a large enough payload to send a full record, 4k is a common page size so we'll go with that */
        uint8_t buf[4096] = { 42 };

        send_status = s2n_send(server_conn, &buf, s2n_array_len(buf), &blocked);

        /* the first send call should block */
        EXPECT_FAILURE(send_status);
        EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);

        /* the `out` buffer should not be freed until it's completely flushed to the socket */
        EXPECT_NOT_EQUAL(server_conn->out.blob.size, 0);

        /* unblock the send call by letting the stuffer grow */
        EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_in, 0));

        send_status = s2n_send(server_conn, &buf, s2n_array_len(buf), &blocked);
        EXPECT_SUCCESS(send_status);

        /* the entire payload should have been sent */
        EXPECT_EQUAL(send_status, s2n_array_len(buf));

        /* make sure the `out` buffer was freed after sending */
        EXPECT_EQUAL(server_conn->out.blob.size, 0);

        /* Receive half of the payload on the first call */
        EXPECT_EQUAL(s2n_recv(client_conn, &buf, s2n_array_len(buf) / 2, &blocked), s2n_array_len(buf) / 2);

        /* the `in` buffer should not be freed until it's completely flushed to the application */
        EXPECT_NOT_EQUAL(client_conn->in.blob.size, 0);

        /* Receive the second half of the payload on the second call */
        EXPECT_EQUAL(s2n_recv(client_conn, &buf, s2n_array_len(buf) / 2, &blocked), s2n_array_len(buf) / 2);

        /* at this point the application has received the full message and the `in` buffer should be freed */
        EXPECT_EQUAL(client_conn->in.blob.size, 0);
    };

    EXPECT_SUCCESS(s2n_config_free(config));
    EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
    END_TEST();
}