2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
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
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>
20 #include "../ssl/packet_locl.h"
24 #define CLIENT_VERSION_LEN 2
26 #define TOTAL_NUM_TESTS 4
29 * Test that explicitly setting ticket data results in it appearing in the
30 * ClientHello for a negotiated SSL/TLS version
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
38 * Enable padding and add a PSK to the ClientHello (this will also ensure the
39 * ClientHello is long enough to need padding)
41 #define TEST_ADD_PADDING_AND_PSK 3
43 #define F5_WORKAROUND_MIN_MSG_LEN 0xff
44 #define F5_WORKAROUND_MAX_MSG_LEN 0x200
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";
52 static int test_client_hello(int currtest)
60 PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0};
61 char *dummytick = "Hello World!";
62 unsigned int type = 0;
66 SSL_SESSION *sess = NULL;
68 #ifdef OPENSSL_NO_TLS1_3
69 if (currtest == TEST_ADD_PADDING_AND_PSK)
74 * For each test set up an SSL_CTX and SSL and see what ClientHello gets
75 * produced when we try to connect
77 ctx = SSL_CTX_new(TLS_method());
82 case TEST_SET_SESSION_TICK_DATA_VER_NEG:
83 /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
84 if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)))
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);
93 * Add some dummy ALPN protocols so that the ClientHello is at least
94 * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
97 if (currtest == TEST_ADD_PADDING
98 && (!TEST_false(SSL_CTX_set_alpn_protos(ctx,
99 (unsigned char *)alpn_prots,
100 sizeof(alpn_prots) - 1))))
113 if (currtest == TEST_ADD_PADDING_AND_PSK) {
114 sessbio = BIO_new_file(sessionfile, "r");
115 if (!TEST_ptr(sessbio)) {
116 TEST_info("Unable to open session.pem");
119 sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
120 if (!TEST_ptr(sess)) {
121 TEST_info("Unable to load SSL_SESSION");
125 * We reset the creation time so that we don't discard the session as
128 if (!TEST_true(SSL_SESSION_set_time(sess, time(NULL)))
129 || !TEST_true(SSL_set_session(con, sess)))
133 rbio = BIO_new(BIO_s_mem());
134 wbio = BIO_new(BIO_s_mem());
135 if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
141 SSL_set_bio(con, rbio, wbio);
142 SSL_set_connect_state(con);
144 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
145 if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick,
150 if (!TEST_int_le(SSL_connect(con), 0)) {
151 /* This shouldn't succeed because we don't have a server! */
155 len = BIO_get_mem_data(wbio, (char **)&data);
156 if (!TEST_true(PACKET_buf_init(&pkt, data, len))
157 /* Skip the record header */
158 || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
161 msglen = PACKET_remaining(&pkt);
163 /* Skip the handshake message header */
164 if (!TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
165 /* Skip client version and random */
166 || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
168 /* Skip session id */
169 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
171 || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
172 /* Skip compression */
173 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
175 || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
178 /* Loop through all extensions */
179 while (PACKET_remaining(&pkt2)) {
181 if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
182 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
185 if (type == TLSEXT_TYPE_session_ticket) {
186 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
187 if (TEST_true(PACKET_equal(&pkt3, dummytick,
188 strlen(dummytick)))) {
189 /* Ticket data is as we expected */
195 if (type == TLSEXT_TYPE_padding) {
196 if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED))
198 else if (TEST_true(currtest == TEST_ADD_PADDING
199 || currtest == TEST_ADD_PADDING_AND_PSK))
200 testresult = TEST_true(msglen == F5_WORKAROUND_MAX_MSG_LEN);
204 if (currtest == TEST_PADDING_NOT_NEEDED)
210 SSL_SESSION_free(sess);
216 int setup_tests(void)
218 if (!TEST_ptr(sessionfile = test_get_argument(0)))
221 ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);