]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/clienthellotest.c
Ignore -named_curve auto value to improve backwards compatibility
[thirdparty/openssl.git] / test / clienthellotest.c
CommitLineData
440e5d80
RS
1/*
2 * Copyright 2015-2016 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
43#define F5_WORKAROUND_MIN_MSG_LEN 0xff
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"
50 "0123456789012345678901234567890123456789012345678901234567890123456789";
3b848c64 51
6bc6ca62 52static int test_client_hello(int currtest)
3b848c64
MC
53{
54 SSL_CTX *ctx;
f231b4e7 55 SSL *con = NULL;
3b848c64
MC
56 BIO *rbio;
57 BIO *wbio;
3b848c64
MC
58 long len;
59 unsigned char *data;
8f3f9623 60 PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0};
3b848c64 61 char *dummytick = "Hello World!";
a105d560 62 unsigned int type = 0;
3b848c64 63 int testresult = 0;
6bc6ca62
MC
64 size_t msglen;
65 BIO *sessbio = NULL;
66 SSL_SESSION *sess = NULL;
3b848c64 67
6828358c
MC
68#ifdef OPENSSL_NO_TLS1_3
69 if (currtest == TEST_ADD_PADDING_AND_PSK)
70 return 1;
71#endif
72
3b848c64
MC
73 /*
74 * For each test set up an SSL_CTX and SSL and see what ClientHello gets
75 * produced when we try to connect
76 */
6bc6ca62 77 ctx = SSL_CTX_new(TLS_method());
a105d560 78 if (!TEST_ptr(ctx))
6bc6ca62 79 goto end;
9362c93e 80
6bc6ca62
MC
81 switch(currtest) {
82 case TEST_SET_SESSION_TICK_DATA_VER_NEG:
6530c490 83 /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
a105d560 84 if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)))
9362c93e 85 goto end;
6bc6ca62
MC
86 break;
87
88 case TEST_ADD_PADDING_AND_PSK:
89 case TEST_ADD_PADDING:
90 case TEST_PADDING_NOT_NEEDED:
91 SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING);
92 /*
93 * Add lots of ciphersuites so that the ClientHello is at least
94 * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
6828358c
MC
95 * needed. Also add some dummy ALPN protocols in case we still don't
96 * have enough.
6bc6ca62
MC
97 */
98 if (currtest == TEST_ADD_PADDING
a105d560
MC
99 && (!TEST_true(SSL_CTX_set_cipher_list(ctx, "ALL"))
100 || !TEST_false(SSL_CTX_set_alpn_protos(ctx,
6828358c 101 (unsigned char *)alpn_prots,
a105d560 102 sizeof(alpn_prots) - 1))))
9362c93e 103 goto end;
a105d560 104
6bc6ca62 105 break;
3b848c64 106
6bc6ca62
MC
107 default:
108 goto end;
109 }
9362c93e 110
6bc6ca62 111 con = SSL_new(ctx);
a105d560 112 if (!TEST_ptr(con))
6bc6ca62 113 goto end;
3b848c64 114
6bc6ca62
MC
115 if (currtest == TEST_ADD_PADDING_AND_PSK) {
116 sessbio = BIO_new_file(sessionfile, "r");
a105d560
MC
117 if (!TEST_ptr(sessbio)) {
118 TEST_info("Unable to open session.pem");
6bc6ca62 119 goto end;
3b848c64 120 }
6bc6ca62 121 sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
a105d560
MC
122 if (!TEST_ptr(sess)) {
123 TEST_info("Unable to load SSL_SESSION");
3b848c64
MC
124 goto end;
125 }
6bc6ca62
MC
126 /*
127 * We reset the creation time so that we don't discard the session as
128 * too old.
129 */
a105d560
MC
130 if (!TEST_true(SSL_SESSION_set_time(sess, time(NULL)))
131 || !TEST_true(SSL_set_session(con, sess)))
c14e790d 132 goto end;
6bc6ca62 133 }
c14e790d 134
6bc6ca62
MC
135 rbio = BIO_new(BIO_s_mem());
136 wbio = BIO_new(BIO_s_mem());
a105d560 137 if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
6bc6ca62
MC
138 BIO_free(rbio);
139 BIO_free(wbio);
140 goto end;
141 }
c14e790d 142
6bc6ca62
MC
143 SSL_set_bio(con, rbio, wbio);
144 SSL_set_connect_state(con);
c14e790d 145
6bc6ca62 146 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
a105d560
MC
147 if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick,
148 strlen(dummytick))))
3b848c64 149 goto end;
6bc6ca62 150 }
c14e790d 151
a105d560 152 if (!TEST_int_le(SSL_connect(con), 0)) {
6bc6ca62
MC
153 /* This shouldn't succeed because we don't have a server! */
154 goto end;
155 }
c14e790d 156
6bc6ca62 157 len = BIO_get_mem_data(wbio, (char **)&data);
a105d560
MC
158 if (!TEST_true(PACKET_buf_init(&pkt, data, len))
159 /* Skip the record header */
160 || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
6bc6ca62
MC
161 goto end;
162
163 msglen = PACKET_remaining(&pkt);
164
165 /* Skip the handshake message header */
a105d560
MC
166 if (!TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
167 /* Skip client version and random */
168 || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
169 + SSL3_RANDOM_SIZE))
170 /* Skip session id */
171 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
172 /* Skip ciphers */
173 || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
174 /* Skip compression */
175 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
176 /* Extensions len */
177 || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
6bc6ca62
MC
178 goto end;
179
180 /* Loop through all extensions */
181 while (PACKET_remaining(&pkt2)) {
182
a105d560
MC
183 if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
184 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
3b848c64
MC
185 goto end;
186
6bc6ca62
MC
187 if (type == TLSEXT_TYPE_session_ticket) {
188 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
a105d560
MC
189 if (TEST_true(PACKET_equal(&pkt3, dummytick,
190 strlen(dummytick)))) {
6bc6ca62
MC
191 /* Ticket data is as we expected */
192 testresult = 1;
3b848c64 193 }
a105d560 194 goto end;
3b848c64 195 }
3b848c64 196 }
6bc6ca62 197 if (type == TLSEXT_TYPE_padding) {
a105d560
MC
198 if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED))
199 goto end;
200 else if (TEST_true(currtest == TEST_ADD_PADDING
201 || currtest == TEST_ADD_PADDING_AND_PSK))
202 testresult = TEST_true(msglen == F5_WORKAROUND_MAX_MSG_LEN);
3b848c64
MC
203 }
204 }
205
a105d560
MC
206 if (currtest == TEST_PADDING_NOT_NEEDED)
207 testresult = 1;
208
6bc6ca62
MC
209end:
210 SSL_free(con);
211 SSL_CTX_free(ctx);
212 SSL_SESSION_free(sess);
213 BIO_free(sessbio);
6bc6ca62
MC
214
215 return testresult;
216}
217
218int test_main(int argc, char *argv[])
219{
220 if (argc != 2)
6828358c 221 return EXIT_FAILURE;
6bc6ca62
MC
222
223 sessionfile = argv[1];
224
225 ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);
3b848c64 226
6bc6ca62 227 return run_tests(argv[0]);
3b848c64 228}