2 * Copyright 2015-2016 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"
23 #include "test_main_custom.h"
25 #define CLIENT_VERSION_LEN 2
27 #define TOTAL_NUM_TESTS 4
30 * Test that explicitly setting ticket data results in it appearing in the
31 * ClientHello for a negotiated SSL/TLS version
33 #define TEST_SET_SESSION_TICK_DATA_VER_NEG 0
34 /* Enable padding and make sure ClientHello is long enough to require it */
35 #define TEST_ADD_PADDING 1
36 /* Enable padding and make sure ClientHello is short enough to not need it */
37 #define TEST_PADDING_NOT_NEEDED 2
39 * Enable padding and add a PSK to the ClientHello (this will also ensure the
40 * ClientHello is long enough to need padding)
42 #define TEST_ADD_PADDING_AND_PSK 3
44 #define F5_WORKAROUND_MIN_MSG_LEN 0xff
45 #define F5_WORKAROUND_MAX_MSG_LEN 0x200
47 static const char *sessionfile = NULL;
48 /* Dummy ALPN protocols used to pad out the size of the ClientHello */
49 static const char alpn_prots[] =
50 "0123456789012345678901234567890123456789012345678901234567890123456789"
51 "0123456789012345678901234567890123456789012345678901234567890123456789";
53 static int test_client_hello(int currtest)
61 PACKET pkt, pkt2, pkt3;
62 char *dummytick = "Hello World!";
63 unsigned int type = 0;
67 SSL_SESSION *sess = NULL;
69 #ifdef OPENSSL_NO_TLS1_3
70 if (currtest == TEST_ADD_PADDING_AND_PSK)
75 * For each test set up an SSL_CTX and SSL and see what ClientHello gets
76 * produced when we try to connect
78 ctx = SSL_CTX_new(TLS_method());
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)))
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);
94 * Add lots of ciphersuites so that the ClientHello is at least
95 * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
96 * needed. Also add some dummy ALPN protocols in case we still don't
99 if (currtest == TEST_ADD_PADDING
100 && (!TEST_true(SSL_CTX_set_cipher_list(ctx, "ALL"))
101 || !TEST_false(SSL_CTX_set_alpn_protos(ctx,
102 (unsigned char *)alpn_prots,
103 sizeof(alpn_prots) - 1))))
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");
122 sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
123 if (!TEST_ptr(sess)) {
124 TEST_info("Unable to load SSL_SESSION");
128 * We reset the creation time so that we don't discard the session as
131 if (!TEST_true(SSL_SESSION_set_time(sess, time(NULL)))
132 || !TEST_true(SSL_set_session(con, sess)))
136 rbio = BIO_new(BIO_s_mem());
137 wbio = BIO_new(BIO_s_mem());
138 if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
144 SSL_set_bio(con, rbio, wbio);
145 SSL_set_connect_state(con);
147 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
148 if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick,
153 if (!TEST_int_le(SSL_connect(con), 0)) {
154 /* This shouldn't succeed because we don't have a server! */
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))
164 msglen = PACKET_remaining(&pkt);
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
171 /* Skip session id */
172 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
174 || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
175 /* Skip compression */
176 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
178 || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
181 /* Loop through all extensions */
182 while (PACKET_remaining(&pkt2)) {
184 if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
185 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
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 */
198 if (type == TLSEXT_TYPE_padding) {
199 if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED))
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);
207 if (currtest == TEST_PADDING_NOT_NEEDED)
213 SSL_SESSION_free(sess);
219 int test_main(int argc, char *argv[])
224 sessionfile = argv[1];
226 ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);
228 return run_tests(argv[0]);