Debian Codegraph @

crt/s2n/tests/unit/s2n_tls13_zero_length_payload_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 <fcntl.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19
20 #include "api/s2n.h"
21 #include "s2n_test.h"
22 #include "stuffer/s2n_stuffer.h"
23 #include "testlib/s2n_testlib.h"
24 #include "tls/s2n_config.h"
25 #include "tls/s2n_connection.h"
26 #include "tls/s2n_handshake_io.c"
27 #include "tls/s2n_record.h"
28 #include "tls/s2n_tls.h"
29 #include "tls/s2n_tls13.h"
30 #include "utils/s2n_safety.h"
31
32 /* In TLS 1.3, encrypted handshake records would appear to be of record type TLS_APPLICATION_DATA.
33 * The actual record content type is found after
34 */
35 const char tls13_zero_length_application_record_hex[] = "170303000117";
36 const char tls13_zero_length_handshake_record_hex[] = "1603030000";
37 const char tls13_zero_length_alert_record_hex[] = "1503030000";
38
39 int main(int argc, char **argv)
40 {
41 BEGIN_TEST();
42
43 EXPECT_SUCCESS(s2n_enable_tls13_in_test());
44
45 /** Test 0 length application data record handled gracefully in client and server mode
46 *
47 *= https://www.rfc-editor.org/rfc/rfc8446#section-5.4
48 *= type=test
49 *# Application Data records may contain a zero-length
50 *# TLSInnerPlaintext.content if the sender desires.
51 **/
52 {
53 struct s2n_connection *server_conn = NULL;
54 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
55 server_conn->actual_protocol_version = S2N_TLS13;
56 EXPECT_OK(s2n_connection_set_secrets(server_conn));
57
58 struct s2n_connection *client_conn = NULL;
59 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
60 client_conn->actual_protocol_version = S2N_TLS13;
61 EXPECT_OK(s2n_connection_set_secrets(client_conn));
62
63 struct s2n_blob zero_length_data = { 0 };
64 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
65
66 DEFER_CLEANUP(struct s2n_stuffer client_to_server = { 0 }, s2n_stuffer_free);
67 DEFER_CLEANUP(struct s2n_stuffer server_to_client = { 0 }, s2n_stuffer_free);
68
69 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_to_server, 0));
70 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&server_to_client, 0));
71
72 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_to_server, &server_to_client, server_conn));
73 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&server_to_client, &client_to_server, client_conn));
74
75 EXPECT_OK(s2n_record_write(client_conn, TLS_APPLICATION_DATA, &zero_length_data));
76 EXPECT_SUCCESS(s2n_flush(client_conn, &blocked));
77 EXPECT_TRUE(s2n_stuffer_data_available(&client_to_server) > 0);
78
79 EXPECT_OK(s2n_record_write(server_conn, TLS_APPLICATION_DATA, &zero_length_data));
80 EXPECT_SUCCESS(s2n_flush(server_conn, &blocked));
81 EXPECT_TRUE(s2n_stuffer_data_available(&server_to_client) > 0);
82
83 uint8_t record_type = 0;
84 int isSSLv2 = 0;
85
86 EXPECT_SUCCESS(s2n_read_full_record(server_conn, &record_type, &isSSLv2));
87 EXPECT_EQUAL(record_type, TLS_APPLICATION_DATA);
88 record_type = 0;
89 EXPECT_SUCCESS(s2n_read_full_record(client_conn, &record_type, &isSSLv2));
90 EXPECT_EQUAL(record_type, TLS_APPLICATION_DATA);
91
92 EXPECT_SUCCESS(s2n_connection_free(server_conn));
93 EXPECT_SUCCESS(s2n_connection_free(client_conn));
94 };
95
96 /** Test 0 length payload in handshake record terminates connection in client and server mode
97 *
98 *= https://www.rfc-editor.org/rfc/rfc8446#section-5.4
99 *= type=test
100 *# Implementations MUST NOT send Handshake and Alert records that have a zero-length
101 *# TLSInnerPlaintext.content; if such a message is received, the receiving
102 *# implementation MUST terminate the connection with an "unexpected_message" alert.
103 **/
104 {
105 struct s2n_connection *server_conn = NULL;
106 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
107 server_conn->actual_protocol_version = S2N_TLS13;
108
109 struct s2n_connection *client_conn = NULL;
110 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
111 client_conn->actual_protocol_version = S2N_TLS13;
112
113 DEFER_CLEANUP(struct s2n_stuffer client_to_server = { 0 }, s2n_stuffer_free);
114 DEFER_CLEANUP(struct s2n_stuffer server_to_client = { 0 }, s2n_stuffer_free);
115
116 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_to_server, 0));
117 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&server_to_client, 0));
118
119 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_to_server, &server_to_client, server_conn));
120 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&server_to_client, &client_to_server, client_conn));
121
122 S2N_BLOB_FROM_HEX(record_header_blob, tls13_zero_length_handshake_record_hex);
123 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&client_to_server, record_header_blob.data, record_header_blob.size));
124 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&server_to_client, record_header_blob.data, record_header_blob.size));
125 EXPECT_EQUAL(s2n_stuffer_data_available(&client_to_server), S2N_TLS_RECORD_HEADER_LENGTH);
126 EXPECT_EQUAL(s2n_stuffer_data_available(&server_to_client), S2N_TLS_RECORD_HEADER_LENGTH);
127
128 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(server_conn), S2N_ERR_BAD_MESSAGE);
129 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(client_conn), S2N_ERR_BAD_MESSAGE);
130
131 EXPECT_SUCCESS(s2n_connection_free(server_conn));
132 EXPECT_SUCCESS(s2n_connection_free(client_conn));
133 };
134
135 /** Test 0 length payload in alert record terminates connection in client and server modes
136 *
137 *= https://www.rfc-editor.org/rfc/rfc8446#section-5.4
138 *= type=test
139 *# Implementations MUST NOT send Handshake and Alert records that have a zero-length
140 *# TLSInnerPlaintext.content; if such a message is received, the receiving
141 *# implementation MUST terminate the connection with an "unexpected_message" alert.
142 **/
143 {
144 struct s2n_connection *server_conn = NULL;
145 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
146 server_conn->actual_protocol_version = S2N_TLS13;
147
148 struct s2n_connection *client_conn = NULL;
149 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
150 client_conn->actual_protocol_version = S2N_TLS13;
151
152 DEFER_CLEANUP(struct s2n_stuffer client_to_server = { 0 }, s2n_stuffer_free);
153 DEFER_CLEANUP(struct s2n_stuffer server_to_client = { 0 }, s2n_stuffer_free);
154
155 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&client_to_server, 0));
156 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&server_to_client, 0));
157
158 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&client_to_server, &server_to_client, server_conn));
159 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&server_to_client, &client_to_server, client_conn));
160
161 S2N_BLOB_FROM_HEX(record_header_blob, tls13_zero_length_alert_record_hex);
162 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&client_to_server, record_header_blob.data, record_header_blob.size));
163 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&server_to_client, record_header_blob.data, record_header_blob.size));
164 EXPECT_EQUAL(s2n_stuffer_data_available(&client_to_server), S2N_TLS_RECORD_HEADER_LENGTH);
165 EXPECT_EQUAL(s2n_stuffer_data_available(&server_to_client), S2N_TLS_RECORD_HEADER_LENGTH);
166
167 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(server_conn), S2N_ERR_BAD_MESSAGE);
168 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(client_conn), S2N_ERR_BAD_MESSAGE);
169
170 EXPECT_SUCCESS(s2n_connection_free(server_conn));
171 EXPECT_SUCCESS(s2n_connection_free(client_conn));
172 };
173
174 END_TEST();
175
176 return 0;
177 }