]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/clienthellotest.c
Update copyright year
[thirdparty/openssl.git] / test / clienthellotest.c
CommitLineData
440e5d80 1/*
6738bf14 2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
3b848c64 3 *
440e5d80
RS
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
3b848c64
MC
8 */
9
10#include <string.h>
11
47c1a0e0 12#include <openssl/opensslconf.h>
3b848c64
MC
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>
6bc6ca62 18#include <time.h>
3b848c64 19
c14e790d 20#include "../ssl/packet_locl.h"
3b848c64 21
6bc6ca62 22#include "testutil.h"
3b848c64 23
6bc6ca62 24#define CLIENT_VERSION_LEN 2
3b848c64 25
6bc6ca62 26#define TOTAL_NUM_TESTS 4
3b848c64
MC
27
28/*
29 * Test that explicitly setting ticket data results in it appearing in the
30 * ClientHello for a negotiated SSL/TLS version
31 */
885e601d 32#define TEST_SET_SESSION_TICK_DATA_VER_NEG 0
6bc6ca62
MC
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
511fbc60 43#define F5_WORKAROUND_MIN_MSG_LEN 0x7f
6bc6ca62
MC
44#define F5_WORKAROUND_MAX_MSG_LEN 0x200
45
6828358c
MC
46static const char *sessionfile = NULL;
47/* Dummy ALPN protocols used to pad out the size of the ClientHello */
48static const char alpn_prots[] =
49 "0123456789012345678901234567890123456789012345678901234567890123456789"
511fbc60
BK
50 "0123456789012345678901234567890123456789012345678901234567890123456789"
51 "01234567890123456789";
3b848c64 52
6bc6ca62 53static int test_client_hello(int currtest)
3b848c64
MC
54{
55 SSL_CTX *ctx;
f231b4e7 56 SSL *con = NULL;
3b848c64
MC
57 BIO *rbio;
58 BIO *wbio;
3b848c64
MC
59 long len;
60 unsigned char *data;
8f3f9623 61 PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0};
3b848c64 62 char *dummytick = "Hello World!";
a105d560 63 unsigned int type = 0;
3b848c64 64 int testresult = 0;
6bc6ca62
MC
65 size_t msglen;
66 BIO *sessbio = NULL;
67 SSL_SESSION *sess = NULL;
3b848c64 68
6828358c
MC
69#ifdef OPENSSL_NO_TLS1_3
70 if (currtest == TEST_ADD_PADDING_AND_PSK)
71 return 1;
72#endif
73
3b848c64
MC
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 */
6bc6ca62 78 ctx = SSL_CTX_new(TLS_method());
a105d560 79 if (!TEST_ptr(ctx))
6bc6ca62 80 goto end;
9362c93e 81
6bc6ca62
MC
82 switch(currtest) {
83 case TEST_SET_SESSION_TICK_DATA_VER_NEG:
c423ecaa
MC
84#if !defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_TLS1_2)
85 /* TLSv1.3 is enabled and TLSv1.2 is disabled so can't do this test */
86 return 1;
87#else
6530c490 88 /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
a105d560 89 if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)))
9362c93e 90 goto end;
c423ecaa 91#endif
6bc6ca62
MC
92 break;
93
94 case TEST_ADD_PADDING_AND_PSK:
0f41dc0e
MC
95 /*
96 * In this case we're doing TLSv1.3 and we're sending a PSK so the
97 * ClientHello is already going to be quite long. To avoid getting one
98 * that is too long for this test we use a restricted ciphersuite list
99 */
100 if (!TEST_true(SSL_CTX_set_cipher_list(ctx,
101 "TLS13-AES-128-GCM-SHA256")))
102 goto end;
103 /* Fall through */
6bc6ca62
MC
104 case TEST_ADD_PADDING:
105 case TEST_PADDING_NOT_NEEDED:
106 SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING);
a5816a5a
MC
107 /* Make sure we get a consistent size across TLS versions */
108 SSL_CTX_clear_options(ctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
6bc6ca62 109 /*
1d2491e2 110 * Add some dummy ALPN protocols so that the ClientHello is at least
6bc6ca62 111 * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
1d2491e2 112 * needed.
6bc6ca62
MC
113 */
114 if (currtest == TEST_ADD_PADDING
1d2491e2
MC
115 && (!TEST_false(SSL_CTX_set_alpn_protos(ctx,
116 (unsigned char *)alpn_prots,
117 sizeof(alpn_prots) - 1))))
9362c93e 118 goto end;
a105d560 119
6bc6ca62 120 break;
3b848c64 121
6bc6ca62
MC
122 default:
123 goto end;
124 }
9362c93e 125
6bc6ca62 126 con = SSL_new(ctx);
a105d560 127 if (!TEST_ptr(con))
6bc6ca62 128 goto end;
3b848c64 129
6bc6ca62
MC
130 if (currtest == TEST_ADD_PADDING_AND_PSK) {
131 sessbio = BIO_new_file(sessionfile, "r");
a105d560
MC
132 if (!TEST_ptr(sessbio)) {
133 TEST_info("Unable to open session.pem");
6bc6ca62 134 goto end;
3b848c64 135 }
6bc6ca62 136 sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
a105d560
MC
137 if (!TEST_ptr(sess)) {
138 TEST_info("Unable to load SSL_SESSION");
3b848c64
MC
139 goto end;
140 }
6bc6ca62
MC
141 /*
142 * We reset the creation time so that we don't discard the session as
143 * too old.
144 */
3a63c0ed 145 if (!TEST_true(SSL_SESSION_set_time(sess, (long)time(NULL)))
a105d560 146 || !TEST_true(SSL_set_session(con, sess)))
c14e790d 147 goto end;
6bc6ca62 148 }
c14e790d 149
6bc6ca62
MC
150 rbio = BIO_new(BIO_s_mem());
151 wbio = BIO_new(BIO_s_mem());
a105d560 152 if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
6bc6ca62
MC
153 BIO_free(rbio);
154 BIO_free(wbio);
155 goto end;
156 }
c14e790d 157
6bc6ca62
MC
158 SSL_set_bio(con, rbio, wbio);
159 SSL_set_connect_state(con);
c14e790d 160
6bc6ca62 161 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
a105d560
MC
162 if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick,
163 strlen(dummytick))))
3b848c64 164 goto end;
6bc6ca62 165 }
c14e790d 166
a105d560 167 if (!TEST_int_le(SSL_connect(con), 0)) {
6bc6ca62
MC
168 /* This shouldn't succeed because we don't have a server! */
169 goto end;
170 }
c14e790d 171
6bc6ca62 172 len = BIO_get_mem_data(wbio, (char **)&data);
a105d560
MC
173 if (!TEST_true(PACKET_buf_init(&pkt, data, len))
174 /* Skip the record header */
175 || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
6bc6ca62
MC
176 goto end;
177
178 msglen = PACKET_remaining(&pkt);
179
180 /* Skip the handshake message header */
a105d560
MC
181 if (!TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
182 /* Skip client version and random */
183 || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
184 + SSL3_RANDOM_SIZE))
185 /* Skip session id */
186 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
187 /* Skip ciphers */
188 || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
189 /* Skip compression */
190 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
191 /* Extensions len */
192 || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
6bc6ca62
MC
193 goto end;
194
195 /* Loop through all extensions */
196 while (PACKET_remaining(&pkt2)) {
197
a105d560
MC
198 if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
199 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
3b848c64
MC
200 goto end;
201
6bc6ca62
MC
202 if (type == TLSEXT_TYPE_session_ticket) {
203 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
a105d560
MC
204 if (TEST_true(PACKET_equal(&pkt3, dummytick,
205 strlen(dummytick)))) {
6bc6ca62
MC
206 /* Ticket data is as we expected */
207 testresult = 1;
3b848c64 208 }
a105d560 209 goto end;
3b848c64 210 }
3b848c64 211 }
6bc6ca62 212 if (type == TLSEXT_TYPE_padding) {
a105d560
MC
213 if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED))
214 goto end;
215 else if (TEST_true(currtest == TEST_ADD_PADDING
216 || currtest == TEST_ADD_PADDING_AND_PSK))
217 testresult = TEST_true(msglen == F5_WORKAROUND_MAX_MSG_LEN);
3b848c64
MC
218 }
219 }
220
a105d560
MC
221 if (currtest == TEST_PADDING_NOT_NEEDED)
222 testresult = 1;
223
6bc6ca62
MC
224end:
225 SSL_free(con);
226 SSL_CTX_free(ctx);
227 SSL_SESSION_free(sess);
228 BIO_free(sessbio);
6bc6ca62
MC
229
230 return testresult;
231}
232
ad887416 233int setup_tests(void)
6bc6ca62 234{
ad887416
P
235 if (!TEST_ptr(sessionfile = test_get_argument(0)))
236 return 0;
6bc6ca62
MC
237
238 ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);
ad887416 239 return 1;
3b848c64 240}