Debian Codegraph @

crt/s2n/tests/unit/s2n_server_alpn_extension_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 "tls/extensions/s2n_server_alpn.h"
18 #include "tls/s2n_connection.h"
19
20 int main(int argc, char **argv)
21 {
22 BEGIN_TEST();
23 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
24
25 const char *test_protocol_name = "chosen_protocol";
26 const uint8_t test_protocol_name_size = strlen(test_protocol_name);
27
28 /* Test should_send */
29 {
30 struct s2n_connection *conn = NULL;
31 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
32
33 /* Should not send if protocol not set. Protocol not set by default. */
34 EXPECT_FALSE(s2n_server_alpn_extension.should_send(conn));
35
36 /* Should send if protocol set. */
37 EXPECT_MEMCPY_SUCCESS(conn->application_protocol, test_protocol_name, test_protocol_name_size);
38 EXPECT_TRUE(s2n_server_alpn_extension.should_send(conn));
39
40 EXPECT_SUCCESS(s2n_connection_free(conn));
41 };
42
43 /* Test send */
44 {
45 struct s2n_connection *conn = NULL;
46 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
47 EXPECT_MEMCPY_SUCCESS(conn->application_protocol, test_protocol_name, test_protocol_name_size);
48
49 struct s2n_stuffer stuffer = { 0 };
50 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
51
52 EXPECT_SUCCESS(s2n_server_alpn_extension.send(conn, &stuffer));
53
54 /* Should have correct total size */
55 uint16_t protocol_name_list_size = 0;
56 EXPECT_SUCCESS(s2n_stuffer_read_uint16(&stuffer, &protocol_name_list_size));
57 EXPECT_EQUAL(protocol_name_list_size, s2n_stuffer_data_available(&stuffer));
58
59 /* Should have correct protocol name size */
60 uint8_t protocol_name_size = 0;
61 EXPECT_SUCCESS(s2n_stuffer_read_uint8(&stuffer, &protocol_name_size));
62 EXPECT_EQUAL(protocol_name_size, s2n_stuffer_data_available(&stuffer));
63 EXPECT_EQUAL(protocol_name_size, test_protocol_name_size);
64
65 /* Should have correct protocol name */
66 uint8_t *protocol_name = NULL;
67 EXPECT_NOT_NULL(protocol_name = s2n_stuffer_raw_read(&stuffer, protocol_name_size));
68 EXPECT_BYTEARRAY_EQUAL(protocol_name, test_protocol_name, test_protocol_name_size);
69
70 EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
71 EXPECT_SUCCESS(s2n_connection_free(conn));
72 };
73
74 /* Test recv */
75 {
76 struct s2n_connection *server_conn = NULL;
77 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
78 EXPECT_MEMCPY_SUCCESS(server_conn->application_protocol, test_protocol_name, test_protocol_name_size);
79
80 /* Should accept extension written by send */
81 {
82 struct s2n_connection *client_conn = NULL;
83 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
84
85 struct s2n_stuffer stuffer = { 0 };
86 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
87
88 EXPECT_SUCCESS(s2n_server_alpn_extension.send(server_conn, &stuffer));
89
90 EXPECT_NULL(s2n_get_application_protocol(client_conn));
91 EXPECT_SUCCESS(s2n_server_alpn_extension.recv(client_conn, &stuffer));
92 EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn));
93 EXPECT_STRING_EQUAL(s2n_get_application_protocol(client_conn), test_protocol_name);
94
95 EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
96
97 EXPECT_SUCCESS(s2n_connection_free(client_conn));
98 EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
99 };
100
101 /* Should ignore extension if protocol name list size incorrect */
102 {
103 struct s2n_connection *client_conn = NULL;
104 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
105
106 struct s2n_stuffer stuffer = { 0 };
107 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
108
109 EXPECT_SUCCESS(s2n_server_alpn_extension.send(server_conn, &stuffer));
110 EXPECT_SUCCESS(s2n_stuffer_wipe_n(&stuffer, 1));
111
112 EXPECT_NULL(s2n_get_application_protocol(client_conn));
113 EXPECT_SUCCESS(s2n_server_alpn_extension.recv(client_conn, &stuffer));
114 EXPECT_NULL(s2n_get_application_protocol(client_conn));
115
116 EXPECT_SUCCESS(s2n_connection_free(client_conn));
117 EXPECT_SUCCESS(s2n_stuffer_free(&stuffer));
118 };
119
120 EXPECT_SUCCESS(s2n_connection_free(server_conn));
121 };
122
123 /* Send and receive maximum-sized alpn */
124 {
125 const uint8_t max_size = UINT8_MAX;
126
127 DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
128 s2n_connection_ptr_free);
129 EXPECT_NOT_NULL(client);
130 EXPECT_NULL(s2n_get_application_protocol(client));
131
132 DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
133 s2n_connection_ptr_free);
134 EXPECT_NOT_NULL(server);
135 memset(server->application_protocol, 'a', sizeof(server->application_protocol) - 1);
136
137 DEFER_CLEANUP(struct s2n_stuffer stuffer = { 0 }, s2n_stuffer_free);
138 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
139 EXPECT_SUCCESS(s2n_server_alpn_extension.send(server, &stuffer));
140 EXPECT_SUCCESS(s2n_server_alpn_extension.recv(client, &stuffer));
141 EXPECT_EQUAL(s2n_stuffer_data_available(&stuffer), 0);
142
143 const char *client_alpn = s2n_get_application_protocol(client);
144 EXPECT_NOT_NULL(client_alpn);
145 EXPECT_EQUAL(strlen(client_alpn), max_size);
146 EXPECT_STRING_EQUAL(client_alpn, server->application_protocol);
147 };
148
149 END_TEST();
150 }