Skip to content

Commit d27ef62

Browse files
committed
Merge pull request #49 from talnikov/empty_server_name
Don't send the server name if it's not specified
2 parents 66b53ef + 4c68879 commit d27ef62

File tree

2 files changed

+336
-16
lines changed

2 files changed

+336
-16
lines changed
Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
/*
2+
* Copyright 2015 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+
18+
#include <sys/wait.h>
19+
#include <unistd.h>
20+
#include <stdint.h>
21+
#include <fcntl.h>
22+
23+
#include <s2n.h>
24+
25+
#include "tls/s2n_connection.h"
26+
#include "tls/s2n_handshake.h"
27+
28+
#define ZERO_TO_THIRTY_ONE 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, \
29+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
30+
31+
static char certificate[] =
32+
"-----BEGIN CERTIFICATE-----\n"
33+
"MIIDLjCCAhYCCQDL1lr6N8/gvzANBgkqhkiG9w0BAQUFADBZMQswCQYDVQQGEwJB\n"
34+
"VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0\n"
35+
"cyBQdHkgTHRkMRIwEAYDVQQDEwlsb2NhbGhvc3QwHhcNMTQwNTEwMTcwODIzWhcN\n"
36+
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0PvePgI5/b3KQ52R0\n"
70+
"8zXHVX/4lSQVtCuhOVwKOCQq97/Zhlp5oTTShdQ0Qa1GQRl5wbTS6hrYEWSi9AQP\n"
71+
"BVUPZ+RIcxx00DfBNURkId8xEpvCOmvySN8sUlUCgYAtXmHbEqkB3qulwRJGhHi5\n"
72+
"UGsfmJBlwSE6wn9wTdKStZ/1k0o1KkiJrJ2ffUzdXxuvSbmgyA5nyBlMSBdurZOp\n"
73+
"+/0qtU4abUQq058OC1b2KEryix/nuzQjha25WJ8eNiQDwUNABZfa9rwUdMIwUh2g\n"
74+
"CHG5Mnjy7Vjz3u2JOtFXCQKBgQCVRo1EIHyLauLuaMINM9HWhWJGqeWXBM8v0GD1\n"
75+
"pRsovQKpiHQNgHizkwM861GqqrfisZZSyKfFlcynkACoVmyu7fv9VoD2VCMiqdUq\n"
76+
"IvjNmfE5RnXVQwja+668AS+MHi+GF77DTFBxoC5VHDAnXfLyIL9WWh9GEBoNLnKT\n"
77+
"hVm8RQKBgQCB9Skzdftc+14a4Vj3NCgdHZHz9mcdPhzJXUiQyZ3tYhaytX9E8mWq\n"
78+
"pm/OFqahbxw6EQd86mgANBMKayD6B1Id1INqtXN1XYI50bSs1D2nOGsBM7MK9aWD\n"
79+
"JXlJ2hwsIc4q9En/LR3GtBaL84xTHGfznNylNhXi7GbO1wNMJuAukA==\n"
80+
"-----END RSA PRIVATE KEY-----\n";
81+
82+
int main(int argc, char **argv)
83+
{
84+
BEGIN_TEST();
85+
86+
EXPECT_SUCCESS(setenv("S2N_ENABLE_INSECURE_CLIENT", "1", 0));
87+
EXPECT_SUCCESS(s2n_init());
88+
89+
/* Client doens't use the server name extension. */
90+
{
91+
struct s2n_connection *client_conn;
92+
struct s2n_connection *server_conn;
93+
struct s2n_config *server_config;
94+
int client_more;
95+
int server_more;
96+
int server_to_client[2];
97+
int client_to_server[2];
98+
99+
/* Create nonblocking pipes */
100+
EXPECT_SUCCESS(pipe(server_to_client));
101+
EXPECT_SUCCESS(pipe(client_to_server));
102+
for (int i = 0; i < 2; i++) {
103+
EXPECT_NOT_EQUAL(fcntl(server_to_client[i], F_SETFL, fcntl(server_to_client[i], F_GETFL) | O_NONBLOCK), -1);
104+
EXPECT_NOT_EQUAL(fcntl(client_to_server[i], F_SETFL, fcntl(client_to_server[i], F_GETFL) | O_NONBLOCK), -1);
105+
}
106+
107+
EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
108+
EXPECT_SUCCESS(s2n_connection_set_read_fd(client_conn, server_to_client[0]));
109+
EXPECT_SUCCESS(s2n_connection_set_write_fd(client_conn, client_to_server[1]));
110+
111+
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
112+
EXPECT_SUCCESS(s2n_connection_set_read_fd(server_conn, client_to_server[0]));
113+
EXPECT_SUCCESS(s2n_connection_set_write_fd(server_conn, server_to_client[1]));
114+
115+
EXPECT_NOT_NULL(server_config = s2n_config_new());
116+
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key(server_config, certificate, private_key));
117+
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
118+
119+
do {
120+
int ret;
121+
ret = s2n_negotiate(client_conn, &client_more);
122+
EXPECT_TRUE(ret == 0 || client_more);
123+
ret = s2n_negotiate(server_conn, &server_more);
124+
EXPECT_TRUE(ret == 0 || server_more);
125+
} while (client_more || server_more);
126+
127+
/* Verify that the server didn't receive the server name. */
128+
EXPECT_NULL(s2n_get_server_name(server_conn));
129+
130+
EXPECT_SUCCESS(s2n_shutdown(client_conn, &client_more));
131+
EXPECT_SUCCESS(s2n_connection_free(client_conn));
132+
EXPECT_SUCCESS(s2n_shutdown(server_conn, &server_more));
133+
EXPECT_SUCCESS(s2n_connection_free(server_conn));
134+
135+
EXPECT_SUCCESS(s2n_config_free(server_config));
136+
137+
for (int i = 0; i < 2; i++) {
138+
EXPECT_SUCCESS(close(server_to_client[i]));
139+
EXPECT_SUCCESS(close(client_to_server[i]));
140+
}
141+
}
142+
143+
/* Client uses the server name extension. */
144+
{
145+
struct s2n_connection *client_conn;
146+
struct s2n_connection *server_conn;
147+
struct s2n_config *server_config;
148+
int client_more;
149+
int server_more;
150+
int server_to_client[2];
151+
int client_to_server[2];
152+
153+
const char *sent_server_name = "awesome.amazonaws.com";
154+
const char *received_server_name;
155+
156+
/* Create nonblocking pipes */
157+
EXPECT_SUCCESS(pipe(server_to_client));
158+
EXPECT_SUCCESS(pipe(client_to_server));
159+
for (int i = 0; i < 2; i++) {
160+
EXPECT_NOT_EQUAL(fcntl(server_to_client[i], F_SETFL, fcntl(server_to_client[i], F_GETFL) | O_NONBLOCK), -1);
161+
EXPECT_NOT_EQUAL(fcntl(client_to_server[i], F_SETFL, fcntl(client_to_server[i], F_GETFL) | O_NONBLOCK), -1);
162+
}
163+
164+
EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
165+
EXPECT_SUCCESS(s2n_connection_set_read_fd(client_conn, server_to_client[0]));
166+
EXPECT_SUCCESS(s2n_connection_set_write_fd(client_conn, client_to_server[1]));
167+
168+
/* Set the server name */
169+
EXPECT_SUCCESS(s2n_set_server_name(client_conn, sent_server_name));
170+
171+
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
172+
EXPECT_SUCCESS(s2n_connection_set_read_fd(server_conn, client_to_server[0]));
173+
EXPECT_SUCCESS(s2n_connection_set_write_fd(server_conn, server_to_client[1]));
174+
175+
EXPECT_NOT_NULL(server_config = s2n_config_new());
176+
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key(server_config, certificate, private_key));
177+
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
178+
179+
do {
180+
int ret;
181+
ret = s2n_negotiate(client_conn, &client_more);
182+
EXPECT_TRUE(ret == 0 || client_more);
183+
ret = s2n_negotiate(server_conn, &server_more);
184+
EXPECT_TRUE(ret == 0 || server_more);
185+
} while (client_more || server_more);
186+
187+
/* Verify that the server name was received intact. */
188+
EXPECT_NOT_NULL(received_server_name = s2n_get_server_name(server_conn));
189+
EXPECT_EQUAL(strlen(received_server_name), strlen(sent_server_name));
190+
EXPECT_BYTEARRAY_EQUAL(received_server_name, sent_server_name, strlen(received_server_name));
191+
192+
EXPECT_SUCCESS(s2n_shutdown(client_conn, &client_more));
193+
EXPECT_SUCCESS(s2n_connection_free(client_conn));
194+
EXPECT_SUCCESS(s2n_shutdown(server_conn, &server_more));
195+
EXPECT_SUCCESS(s2n_connection_free(server_conn));
196+
197+
EXPECT_SUCCESS(s2n_config_free(server_config));
198+
for (int i = 0; i < 2; i++) {
199+
EXPECT_SUCCESS(close(server_to_client[i]));
200+
EXPECT_SUCCESS(close(client_to_server[i]));
201+
}
202+
}
203+
204+
/* Client sends multiple server names. */
205+
{
206+
struct s2n_connection *server_conn;
207+
struct s2n_config *server_config;
208+
int server_more;
209+
int server_to_client[2];
210+
int client_to_server[2];
211+
const char *sent_server_name = "svr";
212+
const char *received_server_name;
213+
214+
uint8_t client_extensions[] = {
215+
/* Extension type TLS_EXTENSION_SERVER_NAME */
216+
0x00, 0x00,
217+
/* Extension size */
218+
0x00, 0x0C,
219+
/* All server names len */
220+
0x00, 0x0A,
221+
/* First server name type - host name */
222+
0x00,
223+
/* First server name len */
224+
0x00, 0x03,
225+
/* First server name, matches sent_server_name */
226+
's', 'v', 'r',
227+
/* Second server name type - host name */
228+
0x00,
229+
/* Second server name len */
230+
0x00, 0x01,
231+
/* Second server name */
232+
0xFF,
233+
};
234+
int client_extensions_len = sizeof(client_extensions);
235+
uint8_t client_hello_message[] = {
236+
/* Protocol version TLS 1.2 */
237+
0x03, 0x03,
238+
/* Client random */
239+
ZERO_TO_THIRTY_ONE,
240+
/* SessionID len - 32 bytes */
241+
0x20,
242+
/* Session ID */
243+
ZERO_TO_THIRTY_ONE,
244+
/* Cipher suites len */
245+
0x00, 0x02,
246+
/* Cipher suite - TLS_RSA_WITH_AES_128_CBC_SHA256 */
247+
0x00, 0x3C,
248+
/* Compression methods len */
249+
0x01,
250+
/* Compression method - none */
251+
0x00,
252+
/* Extensions len */
253+
(client_extensions_len >> 8) & 0xff, (client_extensions_len & 0xff),
254+
};
255+
int body_len = sizeof(client_hello_message) + client_extensions_len;
256+
uint8_t message_header[] = {
257+
/* Handshake message type CLIENT HELLO */
258+
0x01,
259+
/* Body len */
260+
(body_len >> 16) & 0xff, (body_len >> 8) & 0xff, (body_len & 0xff),
261+
};
262+
int message_len = sizeof(message_header) + body_len;
263+
uint8_t record_header[] = {
264+
/* Record type HANDSHAKE */
265+
0x16,
266+
/* Protocol version TLS 1.2 */
267+
0x03, 0x03,
268+
/* Message len */
269+
(message_len >> 8) & 0xff, (message_len & 0xff),
270+
};
271+
272+
/* Create nonblocking pipes */
273+
EXPECT_SUCCESS(pipe(server_to_client));
274+
EXPECT_SUCCESS(pipe(client_to_server));
275+
for (int i = 0; i < 2; i++) {
276+
EXPECT_NOT_EQUAL(fcntl(server_to_client[i], F_SETFL, fcntl(server_to_client[i], F_GETFL) | O_NONBLOCK), -1);
277+
EXPECT_NOT_EQUAL(fcntl(client_to_server[i], F_SETFL, fcntl(client_to_server[i], F_GETFL) | O_NONBLOCK), -1);
278+
}
279+
280+
EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
281+
EXPECT_SUCCESS(s2n_connection_set_read_fd(server_conn, client_to_server[0]));
282+
EXPECT_SUCCESS(s2n_connection_set_write_fd(server_conn, server_to_client[1]));
283+
284+
EXPECT_NOT_NULL(server_config = s2n_config_new());
285+
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key(server_config, certificate, private_key));
286+
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
287+
288+
/* Send the client hello */
289+
EXPECT_EQUAL(write(client_to_server[1], record_header, sizeof(record_header)), sizeof(record_header));
290+
EXPECT_EQUAL(write(client_to_server[1], message_header, sizeof(message_header)), sizeof(message_header));
291+
EXPECT_EQUAL(write(client_to_server[1], client_hello_message, sizeof(client_hello_message)), sizeof(client_hello_message));
292+
EXPECT_EQUAL(write(client_to_server[1], client_extensions, sizeof(client_extensions)), sizeof(client_extensions));
293+
294+
/* Verify that the CLIENT HELLO is accepted */
295+
s2n_negotiate(server_conn, &server_more);
296+
EXPECT_EQUAL(server_more, 1);
297+
EXPECT_EQUAL(server_conn->handshake.state, CLIENT_KEY);
298+
299+
/* Verify that the server name was received intact. */
300+
EXPECT_NOT_NULL(received_server_name = s2n_get_server_name(server_conn));
301+
EXPECT_EQUAL(strlen(received_server_name), strlen(sent_server_name));
302+
EXPECT_BYTEARRAY_EQUAL(received_server_name, sent_server_name, strlen(received_server_name));
303+
304+
EXPECT_SUCCESS(s2n_shutdown(server_conn, &server_more));
305+
EXPECT_SUCCESS(s2n_connection_free(server_conn));
306+
307+
EXPECT_SUCCESS(s2n_config_free(server_config));
308+
for (int i = 0; i < 2; i++) {
309+
EXPECT_SUCCESS(close(server_to_client[i]));
310+
EXPECT_SUCCESS(close(client_to_server[i]));
311+
}
312+
}
313+
314+
END_TEST();
315+
return 0;
316+
}
317+

tls/s2n_client_extensions.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ int s2n_client_extensions_send(struct s2n_connection *conn, struct s2n_stuffer *
3030
{
3131
uint16_t total_size = 0;
3232

33+
/* Signature algorithms */
34+
if (conn->actual_protocol_version == S2N_TLS12) {
35+
total_size += 8;
36+
}
37+
3338
uint16_t server_name_len = strlen(conn->server_name);
3439
if (server_name_len) {
3540
total_size += 9 + server_name_len;
3641
}
3742

38-
if (conn->actual_protocol_version == S2N_TLS12) {
39-
total_size += 8;
40-
}
41-
4243
GUARD(s2n_stuffer_write_uint16(out, total_size));
4344

4445
if (conn->actual_protocol_version == S2N_TLS12) {
@@ -52,21 +53,23 @@ int s2n_client_extensions_send(struct s2n_connection *conn, struct s2n_stuffer *
5253
GUARD(s2n_stuffer_write_uint8(out, TLS_SIGNATURE_ALGORITHM_RSA));
5354
}
5455

55-
/* Write the server name */
56-
GUARD(s2n_stuffer_write_uint16(out, TLS_EXTENSION_SERVER_NAME));
57-
GUARD(s2n_stuffer_write_uint16(out, server_name_len + 5));
56+
if (server_name_len) {
57+
/* Write the server name */
58+
GUARD(s2n_stuffer_write_uint16(out, TLS_EXTENSION_SERVER_NAME));
59+
GUARD(s2n_stuffer_write_uint16(out, server_name_len + 5));
5860

59-
/* Size of all of the server names */
60-
GUARD(s2n_stuffer_write_uint16(out, server_name_len + 3));
61+
/* Size of all of the server names */
62+
GUARD(s2n_stuffer_write_uint16(out, server_name_len + 3));
6163

62-
/* Name type - host name, RFC3546 */
63-
GUARD(s2n_stuffer_write_uint8(out, 0));
64+
/* Name type - host name, RFC3546 */
65+
GUARD(s2n_stuffer_write_uint8(out, 0));
6466

65-
struct s2n_blob server_name;
66-
server_name.data = (uint8_t *) conn->server_name;
67-
server_name.size = server_name_len;
68-
GUARD(s2n_stuffer_write_uint16(out, server_name_len));
69-
GUARD(s2n_stuffer_write(out, &server_name));
67+
struct s2n_blob server_name;
68+
server_name.data = (uint8_t *) conn->server_name;
69+
server_name.size = server_name_len;
70+
GUARD(s2n_stuffer_write_uint16(out, server_name_len));
71+
GUARD(s2n_stuffer_write(out, &server_name));
72+
}
7073

7174
return 0;
7275
}

0 commit comments

Comments
 (0)