Debian Codegraph @

crt/s2n/tests/unit/s2n_record_write_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 "testlib/s2n_testlib.h"
18 #include "tls/s2n_record.h"
19 #include "tls/s2n_tls.h"
20 #include "utils/s2n_safety.h"
21
22 int main(int argc, char *argv[])
23 {
24 BEGIN_TEST();
25
26 /* Test: Records sent before the ServerHello include a sane protocol version */
27 {
28 const uint8_t expected_version = S2N_TLS10;
29
30 /* Test: ClientHellos include a sane protocol version */
31 {
32 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
33 s2n_connection_ptr_free);
34 EXPECT_NOT_NULL(client_conn);
35 EXPECT_OK(s2n_connection_set_tls12_security_policy(client_conn));
36
37 DEFER_CLEANUP(struct s2n_stuffer out = { 0 }, s2n_stuffer_free);
38 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
39 EXPECT_SUCCESS(s2n_connection_set_send_io_stuffer(&out, client_conn));
40
41 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
42 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
43
44 uint8_t content_type = 0;
45 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &content_type));
46 EXPECT_EQUAL(content_type, TLS_HANDSHAKE);
47
48 uint8_t version_high = 0;
49 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &version_high));
50 EXPECT_EQUAL(version_high, expected_version / 10);
51
52 uint8_t version_low = 0;
53 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &version_low));
54 EXPECT_EQUAL(version_low, expected_version % 10);
55 }
56
57 /* Test: Alerts include a sane protocol version */
58 {
59 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
60 s2n_connection_ptr_free);
61 EXPECT_NOT_NULL(server_conn);
62
63 DEFER_CLEANUP(struct s2n_stuffer out = { 0 }, s2n_stuffer_free);
64 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));
65 EXPECT_SUCCESS(s2n_connection_set_send_io_stuffer(&out, server_conn));
66
67 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
68 EXPECT_SUCCESS(s2n_shutdown_send(server_conn, &blocked));
69
70 uint8_t content_type = 0;
71 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &content_type));
72 EXPECT_EQUAL(content_type, TLS_ALERT);
73
74 uint8_t version_high = 0;
75 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &version_high));
76 EXPECT_EQUAL(version_high, expected_version / 10);
77
78 uint8_t version_low = 0;
79 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &version_low));
80 EXPECT_EQUAL(version_low, expected_version % 10);
81 }
82 }
83
84 END_TEST();
85 }