Debian Codegraph @

crt/s2n/tests/unit/s2n_handshake_io_errors_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 "api/s2n.h"
17 #include "s2n_test.h"
18 #include "testlib/s2n_testlib.h"
19 #include "utils/s2n_result.h"
20
21 int main(int argc, char **argv)
22 {
23 BEGIN_TEST();
24
25 /* IO blocking on read does not close connection or invoke blinding */
26 {
27 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
28 EXPECT_NOT_NULL(server_conn);
29 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
30 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
31
32 DEFER_CLEANUP(struct s2n_stuffer io_stuffer = { 0 }, s2n_stuffer_free);
33 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&io_stuffer, 0));
34 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, server_conn));
35
36 /* Try to read the ClientHello, which hasn't been written yet */
37 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
38 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(server_conn, &blocked), S2N_ERR_IO_BLOCKED);
39
40 /* Error did not close connection */
41 EXPECT_TRUE(s2n_connection_check_io_status(server_conn, S2N_IO_FULL_DUPLEX));
42
43 /* Error did not trigger blinding */
44 EXPECT_EQUAL(s2n_connection_get_delay(server_conn), 0);
45
46 EXPECT_SUCCESS(s2n_connection_free(server_conn));
47 };
48
49 /* Failure in read handler closes connection and invokes blinding */
50 {
51 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
52
53 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
54 EXPECT_NOT_NULL(client_conn);
55 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
56
57 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
58 EXPECT_NOT_NULL(server_conn);
59 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
60 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
61
62 DEFER_CLEANUP(struct s2n_stuffer io_stuffer = { 0 }, s2n_stuffer_free);
63 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&io_stuffer, 0));
64 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, client_conn));
65 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, server_conn));
66
67 /* Write the ClientHello */
68 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
69
70 /* Overwrite everything except the headers */
71 uint32_t content_size = s2n_stuffer_data_available(&io_stuffer)
72 - S2N_TLS_RECORD_HEADER_LENGTH - TLS_HANDSHAKE_HEADER_LENGTH;
73 EXPECT_SUCCESS(s2n_stuffer_wipe_n(&io_stuffer, content_size));
74 EXPECT_SUCCESS(s2n_stuffer_skip_write(&io_stuffer, content_size));
75
76 /* Read the ClientHello */
77 EXPECT_ERROR_WITH_ERRNO(s2n_negotiate_until_message(server_conn, &blocked, SERVER_HELLO),
78 S2N_ERR_BAD_MESSAGE);
79
80 /* Error closes connection */
81 EXPECT_TRUE(s2n_connection_check_io_status(server_conn, S2N_IO_CLOSED));
82
83 /* Error triggers blinding */
84 EXPECT_NOT_EQUAL(s2n_connection_get_delay(server_conn), 0);
85
86 EXPECT_SUCCESS(s2n_connection_free(client_conn));
87 EXPECT_SUCCESS(s2n_connection_free(server_conn));
88 };
89
90 /* Decrypt failure closes connection and invokes blinding */
91 {
92 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
93
94 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
95 EXPECT_NOT_NULL(client_conn);
96 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
97
98 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
99 EXPECT_NOT_NULL(server_conn);
100 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
101 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
102
103 DEFER_CLEANUP(struct s2n_stuffer io_stuffer = { 0 }, s2n_stuffer_free);
104 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&io_stuffer, 0));
105 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, client_conn));
106 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_stuffer, &io_stuffer, server_conn));
107
108 /* Write the ClientHello */
109 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
110
111 /* Set up encryption on the server */
112 EXPECT_OK(s2n_connection_set_secrets(server_conn));
113
114 /* Read the ClientHello */
115 EXPECT_ERROR_WITH_ERRNO(s2n_negotiate_until_message(server_conn, &blocked, SERVER_HELLO),
116 S2N_ERR_DECRYPT);
117
118 /* Error closes connection */
119 EXPECT_TRUE(s2n_connection_check_io_status(server_conn, S2N_IO_CLOSED));
120
121 /* Error triggers blinding */
122 EXPECT_NOT_EQUAL(s2n_connection_get_delay(server_conn), 0);
123
124 EXPECT_SUCCESS(s2n_connection_free(client_conn));
125 EXPECT_SUCCESS(s2n_connection_free(server_conn));
126 };
127
128 END_TEST();
129 }