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!";
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 (!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
98 * In the padding not needed case we assume the test will pass, but then
99 * set testresult to 0 if we see the padding extension.
101 if (currtest == TEST_ADD_PADDING
102 && (!SSL_CTX_set_cipher_list(ctx, "ALL")
103 || SSL_CTX_set_alpn_protos(ctx,
104 (unsigned char *)alpn_prots,
105 sizeof(alpn_prots) - 1)))
107 else if (currtest == TEST_PADDING_NOT_NEEDED)
119 if (currtest == TEST_ADD_PADDING_AND_PSK) {
120 sessbio = BIO_new_file(sessionfile, "r");
121 if (sessbio == NULL) {
122 printf("Unable to open session.pem\n");
125 sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
127 printf("Unable to load SSL_SESSION\n");
131 * We reset the creation time so that we don't discard the session as
134 if (!SSL_SESSION_set_time(sess, time(NULL))) {
135 printf("Unable to set creation time on SSL_SESSION\n");
138 if (!SSL_set_session(con, sess)) {
139 printf("Unable to set the session on the connection\n");
144 rbio = BIO_new(BIO_s_mem());
145 wbio = BIO_new(BIO_s_mem());
146 if (rbio == NULL || wbio == NULL) {
152 SSL_set_bio(con, rbio, wbio);
153 SSL_set_connect_state(con);
155 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
156 if (!SSL_set_session_ticket_ext(con, dummytick, strlen(dummytick)))
160 if (SSL_connect(con) > 0) {
161 /* This shouldn't succeed because we don't have a server! */
165 len = BIO_get_mem_data(wbio, (char **)&data);
166 if (!PACKET_buf_init(&pkt, data, len))
169 /* Skip the record header */
170 if (!PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
173 msglen = PACKET_remaining(&pkt);
175 /* Skip the handshake message header */
176 if (!PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
179 /* Skip client version and random */
180 if (!PACKET_forward(&pkt, CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE))
183 /* Skip session id */
184 if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
188 if (!PACKET_get_length_prefixed_2(&pkt, &pkt2))
191 /* Skip compression */
192 if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
196 if (!PACKET_as_length_prefixed_2(&pkt, &pkt2))
199 /* Loop through all extensions */
200 while (PACKET_remaining(&pkt2)) {
202 if (!PACKET_get_net_2(&pkt2, &type) ||
203 !PACKET_get_length_prefixed_2(&pkt2, &pkt3))
206 if (type == TLSEXT_TYPE_session_ticket) {
207 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
208 if (PACKET_equal(&pkt3, dummytick, strlen(dummytick))) {
209 /* Ticket data is as we expected */
212 printf("Received session ticket is not as expected\n");
217 if (type == TLSEXT_TYPE_padding) {
218 if (currtest == TEST_ADD_PADDING
219 || currtest == TEST_ADD_PADDING_AND_PSK)
220 testresult = (msglen == F5_WORKAROUND_MAX_MSG_LEN);
229 SSL_SESSION_free(sess);
232 printf("ClientHello test: FAILED (Test %d)\n", currtest);
237 int test_main(int argc, char *argv[])
242 sessionfile = argv[1];
244 ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);
246 return run_tests(argv[0]);