Debian Codegraph @

crt/s2n/tests/unit/s2n_handshake_io_async_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 /* Get access to s2n_handshake_read_io */
22 #include "tls/s2n_handshake_io.c"
23
24 bool async_blocked = false;
25 size_t blocking_handler_count = 0;
26 static int s2n_blocking_handler(struct s2n_connection *conn)
27 {
28 blocking_handler_count++;
29 if (async_blocked) {
30 POSIX_BAIL(S2N_ERR_ASYNC_BLOCKED);
31 }
32 return S2N_SUCCESS;
33 }
34
35 static int s2n_error_handler(struct s2n_connection *conn)
36 {
37 POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
38 }
39
40 int main(int argc, char **argv)
41 {
42 BEGIN_TEST();
43
44 /* Async blocking errors block handshake negotiation */
45 {
46 const size_t repeat_count = 10;
47
48 DEFER_CLEANUP(struct s2n_stuffer io_buffer = { 0 }, s2n_stuffer_free);
49 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&io_buffer, 0));
50
51 /* Write handles async blocking */
52 {
53 tls13_state_machine[CLIENT_HELLO].handler[S2N_CLIENT] = s2n_blocking_handler;
54 tls13_state_machine[SERVER_HELLO].handler[S2N_CLIENT] = s2n_error_handler;
55
56 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
57 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
58 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(NULL, &io_buffer, conn));
59 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
60
61 /* Consistently blocks */
62 async_blocked = true;
63 blocking_handler_count = 0;
64 for (size_t i = 0; i < repeat_count; i++) {
65 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(conn, &blocked), S2N_ERR_ASYNC_BLOCKED);
66 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_APPLICATION_INPUT);
67 EXPECT_EQUAL(s2n_stuffer_data_available(&io_buffer), 0);
68 }
69 EXPECT_EQUAL(blocking_handler_count, repeat_count);
70
71 /* If unblocked, continues. Fails to read next message because there is no next message. */
72 async_blocked = false;
73 blocking_handler_count = 0;
74 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(conn, &blocked), S2N_ERR_IO);
75 EXPECT_EQUAL(blocking_handler_count, 1);
76
77 /* Only wrote one record/message */
78 EXPECT_EQUAL(s2n_stuffer_data_available(&io_buffer), S2N_TLS_RECORD_HEADER_LENGTH + TLS_HANDSHAKE_HEADER_LENGTH);
79
80 EXPECT_SUCCESS(s2n_connection_free(conn));
81 };
82
83 /* Use the output of writing to test reading */
84 EXPECT_SUCCESS(s2n_stuffer_reread(&io_buffer));
85
86 /* Read handles async blocking */
87 {
88 state_machine[CLIENT_HELLO].handler[S2N_SERVER] = s2n_blocking_handler;
89 state_machine[SERVER_HELLO].handler[S2N_SERVER] = s2n_error_handler;
90
91 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
92 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
93 EXPECT_SUCCESS(s2n_connection_set_blinding(conn, S2N_SELF_SERVICE_BLINDING));
94 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_buffer, NULL, conn));
95
96 /* Consistently blocks */
97 async_blocked = true;
98 blocking_handler_count = 0;
99 for (size_t i = 0; i < repeat_count; i++) {
100 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(conn, &blocked), S2N_ERR_ASYNC_BLOCKED);
101 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_APPLICATION_INPUT);
102 EXPECT_EQUAL(s2n_stuffer_data_available(&io_buffer), 0);
103 }
104 EXPECT_EQUAL(blocking_handler_count, repeat_count);
105
106 /* If unblocked, continues */
107 async_blocked = false;
108 blocking_handler_count = 0;
109 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(conn, &blocked), S2N_ERR_UNIMPLEMENTED);
110 EXPECT_EQUAL(blocking_handler_count, 1);
111
112 EXPECT_SUCCESS(s2n_connection_free(conn));
113 };
114 };
115
116 END_TEST();
117 }