File: s2n_npn_extension_test.c

package info (click to toggle)
aws-crt-python 0.24.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,932 kB
  • sloc: ansic: 418,984; python: 23,626; makefile: 6,035; sh: 4,075; ruby: 208; java: 82; perl: 73; cpp: 25; xml: 11
file content (289 lines) | stat: -rw-r--r-- 13,684 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

#include <stdint.h>
#include <string.h>

#include "s2n_test.h"
#include "stuffer/s2n_stuffer.h"
#include "testlib/s2n_testlib.h"
#include "tests/s2n_test.h"
#include "tls/extensions/s2n_client_alpn.h"
#include "tls/extensions/s2n_npn.h"
#include "tls/s2n_tls.h"
#include "utils/s2n_safety.h"

#define HTTP11 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31
#define SPDY1  0x73, 0x70, 0x64, 0x79, 0x2f, 0x31
#define SPDY2  0x73, 0x70, 0x64, 0x79, 0x2f, 0x32
#define SPDY3  0x73, 0x70, 0x64, 0x79, 0x2f, 0x33

S2N_RESULT s2n_calculate_padding(uint8_t protocol_len, uint8_t *padding_len);

int main(int argc, char **argv)
{
    BEGIN_TEST();

    const char *protocols[] = { "http/1.1", "spdy/1", "spdy/2" };
    const uint8_t protocols_count = s2n_array_len(protocols);

    /* Should-send tests on the client side */
    {
        /* No connection */
        EXPECT_FALSE(s2n_client_npn_extension.should_send(NULL));

        /* No config */
        DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
        EXPECT_NOT_NULL(client_conn);
        EXPECT_FALSE(s2n_client_npn_extension.should_send(client_conn));

        /* No application protocols set */
        DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
        EXPECT_NOT_NULL(config);
        EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
        EXPECT_FALSE(s2n_client_npn_extension.should_send(client_conn));

        /* Application protocols set but NPN not supported. In this case the ALPN extension will be sent. */
        EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
        EXPECT_FALSE(s2n_client_npn_extension.should_send(client_conn));
        EXPECT_TRUE(s2n_client_alpn_extension.should_send(client_conn));

        /* Both ALPN and NPN extensions will be sent */
        client_conn->config->npn_supported = true;
        EXPECT_TRUE(s2n_client_npn_extension.should_send(client_conn));
        EXPECT_TRUE(s2n_client_alpn_extension.should_send(client_conn));

        /*
         *= https://datatracker.ietf.org/doc/id/draft-agl-tls-nextprotoneg-03#section-3
         *= type=test
         *# For the same reasons, after a handshake has been performed for a
         *# given connection, renegotiations on the same connection MUST NOT
         *# include the "next_protocol_negotiation" extension.
         */
        client_conn->handshake.renegotiation = true;
        EXPECT_FALSE(s2n_client_npn_extension.should_send(client_conn));
        EXPECT_TRUE(s2n_client_alpn_extension.should_send(client_conn));
    };

    /* s2n_client_npn_recv */
    {
        DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
        EXPECT_NOT_NULL(server_conn);
        DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
        EXPECT_NOT_NULL(config);
        EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
        EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));

        DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free);
        EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));

        /* NPN not supported */
        EXPECT_SUCCESS(s2n_client_npn_extension.recv(server_conn, &extension));
        EXPECT_FALSE(server_conn->npn_negotiated);

        /* NPN supported */
        server_conn->config->npn_supported = true;
        EXPECT_SUCCESS(s2n_client_npn_extension.recv(server_conn, &extension));
        EXPECT_TRUE(server_conn->npn_negotiated);

        /* Server has already negotiated a protocol with the ALPN extension */
        uint8_t first_protocol_len = strlen(protocols[0]);
        EXPECT_MEMCPY_SUCCESS(server_conn->application_protocol, protocols[0], first_protocol_len + 1);
        server_conn->npn_negotiated = false;
        EXPECT_SUCCESS(s2n_client_npn_extension.recv(server_conn, &extension));
        EXPECT_FALSE(server_conn->npn_negotiated);
    };

    /* Should-send tests on the server side */
    {
        DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
        EXPECT_NOT_NULL(server_conn);
        DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
        EXPECT_NOT_NULL(config);
        EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
        EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));

        /* NPN not negotiated */
        EXPECT_FALSE(s2n_server_npn_extension.should_send(server_conn));

        /* NPN negotiated */
        server_conn->npn_negotiated = true;
        EXPECT_TRUE(s2n_server_npn_extension.should_send(server_conn));
    };

    /* s2n_server_npn_send */
    {
        DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
        EXPECT_NOT_NULL(server_conn);
        DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
        EXPECT_NOT_NULL(config);
        EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
        EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
        DEFER_CLEANUP(struct s2n_stuffer out = { 0 }, s2n_stuffer_free);
        EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0));

        EXPECT_SUCCESS(s2n_server_npn_extension.send(server_conn, &out));

        uint8_t protocol_len = 0;
        uint8_t protocol[UINT8_MAX] = { 0 };
        for (size_t i = 0; i < protocols_count; i++) {
            EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &protocol_len));
            EXPECT_EQUAL(protocol_len, strlen(protocols[i]));

            EXPECT_SUCCESS(s2n_stuffer_read_bytes(&out, protocol, protocol_len));
            EXPECT_BYTEARRAY_EQUAL(protocol, protocols[i], protocol_len);
        }

        EXPECT_EQUAL(s2n_stuffer_data_available(&out), 0);
    };

    /* s2n_server_npn_recv */
    {
        /* Client has no application protocols configured. Not sure how this
         * could happen, but added to be thorough. */
        {
            DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
            EXPECT_NOT_NULL(client_conn);
            DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));

            EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension));
            EXPECT_NULL(s2n_get_application_protocol(client_conn));
        };

        /* NPN recv extension can read NPN send extension */
        {
            DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
            EXPECT_NOT_NULL(client_conn);
            DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
            EXPECT_NOT_NULL(config);
            EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
            EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));

            DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));

            EXPECT_SUCCESS(s2n_server_npn_extension.send(client_conn, &extension));
            EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension));

            /* Server sent the same list that the client configured so the first protocol in the list is chosen */
            EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn));
            EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[0], strlen(protocols[0]));
        };

        /* No match exists */
        {
            DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
            EXPECT_NOT_NULL(client_conn);
            DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
            EXPECT_NOT_NULL(config);
            EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
            EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));

            DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));

            uint8_t protocol[] = { SPDY3 };
            EXPECT_SUCCESS(s2n_stuffer_write_uint8(&extension, sizeof(protocol)));
            EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, protocol, sizeof(protocol)));

            /*
             *= https://datatracker.ietf.org/doc/id/draft-agl-tls-nextprotoneg-03#section-4
             *= type=test
             *# In the event that the client doesn't support any of server's protocols, or
             *# the server doesn't advertise any, it SHOULD select the first protocol
             *# that it supports.
             */
            EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension));
            EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn));
            EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[0], strlen(protocols[0]));
        };

        /* Server sends empty list */
        {
            DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
            EXPECT_NOT_NULL(client_conn);
            DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
            EXPECT_NOT_NULL(config);
            EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
            EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));

            DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));

            /*
             *= https://datatracker.ietf.org/doc/id/draft-agl-tls-nextprotoneg-03#section-4
             *= type=test
             *# In the event that the client doesn't support any of server's protocols, or
             *# the server doesn't advertise any, it SHOULD select the first protocol
             *# that it supports.
             */
            EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension));
            EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn));
            EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[0], strlen(protocols[0]));
        };

        /* Multiple matches exist and server's preferred choice is selected */
        {
            DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
            EXPECT_NOT_NULL(client_conn);
            DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
            EXPECT_NOT_NULL(config);
            EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
            EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));

            DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free);
            EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0));

            uint8_t wire_bytes[] = {
                /* Size and bytes of first protocol */
                0x06,
                SPDY1,
                /* Size and bytes of second protocol */
                0x08,
                HTTP11,
                /* Size and bytes of second protocol */
                0x06,
                SPDY2,
            };

            EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, wire_bytes, sizeof(wire_bytes)));
            EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension));

            EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn));

            /* Client's second protocol is selected because the server prefers it over client's first protocol */
            EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[1], strlen(protocols[1]));
        };
    };

    /* Check application protocol array can hold the largest uint8_t value.
     *
     * We frequently copy a uint8_t's worth of data into this array. Adding
     * checks to ensure that the array will be large enough causes compilers
     * to give warnings that the check will always be true.
     * This test will fail if we ever make that array smaller, so we remember
     * to go back and add those checks.
     */
    {
        DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
        EXPECT_NOT_NULL(server_conn);
        /* Not <= because the application protocol is a string, which needs to
         * be terminated by a null character */
        EXPECT_TRUE(UINT8_MAX < sizeof(server_conn->application_protocol));
    };

    END_TEST();
}