]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/clienthellotest.c
Implement session id TLSv1.3 middlebox compatibility mode
[thirdparty/openssl.git] / test / clienthellotest.c
1 /*
2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <string.h>
11
12 #include <openssl/opensslconf.h>
13 #include <openssl/bio.h>
14 #include <openssl/crypto.h>
15 #include <openssl/evp.h>
16 #include <openssl/ssl.h>
17 #include <openssl/err.h>
18 #include <time.h>
19
20 #include "../ssl/packet_locl.h"
21
22 #include "testutil.h"
23
24 #define CLIENT_VERSION_LEN 2
25
26 #define TOTAL_NUM_TESTS 4
27
28 /*
29 * Test that explicitly setting ticket data results in it appearing in the
30 * ClientHello for a negotiated SSL/TLS version
31 */
32 #define TEST_SET_SESSION_TICK_DATA_VER_NEG 0
33 /* Enable padding and make sure ClientHello is long enough to require it */
34 #define TEST_ADD_PADDING 1
35 /* Enable padding and make sure ClientHello is short enough to not need it */
36 #define TEST_PADDING_NOT_NEEDED 2
37 /*
38 * Enable padding and add a PSK to the ClientHello (this will also ensure the
39 * ClientHello is long enough to need padding)
40 */
41 #define TEST_ADD_PADDING_AND_PSK 3
42
43 #define F5_WORKAROUND_MIN_MSG_LEN 0x7f
44 #define F5_WORKAROUND_MAX_MSG_LEN 0x200
45
46 static const char *sessionfile = NULL;
47 /* Dummy ALPN protocols used to pad out the size of the ClientHello */
48 static const char alpn_prots[] =
49 "0123456789012345678901234567890123456789012345678901234567890123456789"
50 "0123456789012345678901234567890123456789012345678901234567890123456789"
51 "01234567890123456789";
52
53 static int test_client_hello(int currtest)
54 {
55 SSL_CTX *ctx;
56 SSL *con = NULL;
57 BIO *rbio;
58 BIO *wbio;
59 long len;
60 unsigned char *data;
61 PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0};
62 char *dummytick = "Hello World!";
63 unsigned int type = 0;
64 int testresult = 0;
65 size_t msglen;
66 BIO *sessbio = NULL;
67 SSL_SESSION *sess = NULL;
68
69 #ifdef OPENSSL_NO_TLS1_3
70 if (currtest == TEST_ADD_PADDING_AND_PSK)
71 return 1;
72 #endif
73
74 /*
75 * For each test set up an SSL_CTX and SSL and see what ClientHello gets
76 * produced when we try to connect
77 */
78 ctx = SSL_CTX_new(TLS_method());
79 if (!TEST_ptr(ctx))
80 goto end;
81
82 switch(currtest) {
83 case TEST_SET_SESSION_TICK_DATA_VER_NEG:
84 /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
85 if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)))
86 goto end;
87 break;
88
89 case TEST_ADD_PADDING_AND_PSK:
90 case TEST_ADD_PADDING:
91 case TEST_PADDING_NOT_NEEDED:
92 SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING);
93 /* Make sure we get a consistent size across TLS versions */
94 SSL_CTX_clear_options(ctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
95 /*
96 * Add some dummy ALPN protocols so that the ClientHello is at least
97 * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
98 * needed.
99 */
100 if (currtest == TEST_ADD_PADDING
101 && (!TEST_false(SSL_CTX_set_alpn_protos(ctx,
102 (unsigned char *)alpn_prots,
103 sizeof(alpn_prots) - 1))))
104 goto end;
105
106 break;
107
108 default:
109 goto end;
110 }
111
112 con = SSL_new(ctx);
113 if (!TEST_ptr(con))
114 goto end;
115
116 if (currtest == TEST_ADD_PADDING_AND_PSK) {
117 sessbio = BIO_new_file(sessionfile, "r");
118 if (!TEST_ptr(sessbio)) {
119 TEST_info("Unable to open session.pem");
120 goto end;
121 }
122 sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
123 if (!TEST_ptr(sess)) {
124 TEST_info("Unable to load SSL_SESSION");
125 goto end;
126 }
127 /*
128 * We reset the creation time so that we don't discard the session as
129 * too old.
130 */
131 if (!TEST_true(SSL_SESSION_set_time(sess, (long)time(NULL)))
132 || !TEST_true(SSL_set_session(con, sess)))
133 goto end;
134 }
135
136 rbio = BIO_new(BIO_s_mem());
137 wbio = BIO_new(BIO_s_mem());
138 if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
139 BIO_free(rbio);
140 BIO_free(wbio);
141 goto end;
142 }
143
144 SSL_set_bio(con, rbio, wbio);
145 SSL_set_connect_state(con);
146
147 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
148 if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick,
149 strlen(dummytick))))
150 goto end;
151 }
152
153 if (!TEST_int_le(SSL_connect(con), 0)) {
154 /* This shouldn't succeed because we don't have a server! */
155 goto end;
156 }
157
158 len = BIO_get_mem_data(wbio, (char **)&data);
159 if (!TEST_true(PACKET_buf_init(&pkt, data, len))
160 /* Skip the record header */
161 || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
162 goto end;
163
164 msglen = PACKET_remaining(&pkt);
165
166 /* Skip the handshake message header */
167 if (!TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
168 /* Skip client version and random */
169 || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
170 + SSL3_RANDOM_SIZE))
171 /* Skip session id */
172 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
173 /* Skip ciphers */
174 || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
175 /* Skip compression */
176 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
177 /* Extensions len */
178 || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
179 goto end;
180
181 /* Loop through all extensions */
182 while (PACKET_remaining(&pkt2)) {
183
184 if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
185 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
186 goto end;
187
188 if (type == TLSEXT_TYPE_session_ticket) {
189 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
190 if (TEST_true(PACKET_equal(&pkt3, dummytick,
191 strlen(dummytick)))) {
192 /* Ticket data is as we expected */
193 testresult = 1;
194 }
195 goto end;
196 }
197 }
198 if (type == TLSEXT_TYPE_padding) {
199 if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED))
200 goto end;
201 else if (TEST_true(currtest == TEST_ADD_PADDING
202 || currtest == TEST_ADD_PADDING_AND_PSK))
203 testresult = TEST_true(msglen == F5_WORKAROUND_MAX_MSG_LEN);
204 }
205 }
206
207 if (currtest == TEST_PADDING_NOT_NEEDED)
208 testresult = 1;
209
210 end:
211 SSL_free(con);
212 SSL_CTX_free(ctx);
213 SSL_SESSION_free(sess);
214 BIO_free(sessbio);
215
216 return testresult;
217 }
218
219 int setup_tests(void)
220 {
221 if (!TEST_ptr(sessionfile = test_get_argument(0)))
222 return 0;
223
224 ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);
225 return 1;
226 }