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 const char *sessionfile = NULL;
49 static int test_client_hello(int currtest)
57 PACKET pkt, pkt2, pkt3;
58 char *dummytick = "Hello World!";
63 SSL_SESSION *sess = NULL;
66 * For each test set up an SSL_CTX and SSL and see what ClientHello gets
67 * produced when we try to connect
69 ctx = SSL_CTX_new(TLS_method());
74 case TEST_SET_SESSION_TICK_DATA_VER_NEG:
75 /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
76 if (!SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION))
80 case TEST_ADD_PADDING_AND_PSK:
81 case TEST_ADD_PADDING:
82 case TEST_PADDING_NOT_NEEDED:
83 SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING);
85 * Add lots of ciphersuites so that the ClientHello is at least
86 * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
88 * In the padding not needed case we assume the test will pass, but then
89 * set testresult to 0 if we see the padding extension.
91 if (currtest == TEST_ADD_PADDING
92 && !SSL_CTX_set_cipher_list(ctx, "ALL"))
94 else if (currtest == TEST_PADDING_NOT_NEEDED)
106 if (currtest == TEST_ADD_PADDING_AND_PSK) {
107 sessbio = BIO_new_file(sessionfile, "r");
108 if (sessbio == NULL) {
109 printf("Unable to open session.pem\n");
112 sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
114 printf("Unable to load SSL_SESSION\n");
118 * We reset the creation time so that we don't discard the session as
121 if (!SSL_SESSION_set_time(sess, time(NULL))) {
122 printf("Unable to set creation time on SSL_SESSION\n");
125 if (!SSL_set_session(con, sess)) {
126 printf("Unable to set the session on the connection\n");
131 rbio = BIO_new(BIO_s_mem());
132 wbio = BIO_new(BIO_s_mem());
133 if (rbio == NULL || wbio == NULL) {
139 SSL_set_bio(con, rbio, wbio);
140 SSL_set_connect_state(con);
142 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
143 if (!SSL_set_session_ticket_ext(con, dummytick, strlen(dummytick)))
147 if (SSL_connect(con) > 0) {
148 /* This shouldn't succeed because we don't have a server! */
152 len = BIO_get_mem_data(wbio, (char **)&data);
153 if (!PACKET_buf_init(&pkt, data, len))
156 /* Skip the record header */
157 if (!PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
160 msglen = PACKET_remaining(&pkt);
162 /* Skip the handshake message header */
163 if (!PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
166 /* Skip client version and random */
167 if (!PACKET_forward(&pkt, CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE))
170 /* Skip session id */
171 if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
175 if (!PACKET_get_length_prefixed_2(&pkt, &pkt2))
178 /* Skip compression */
179 if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
183 if (!PACKET_as_length_prefixed_2(&pkt, &pkt2))
186 /* Loop through all extensions */
187 while (PACKET_remaining(&pkt2)) {
189 if (!PACKET_get_net_2(&pkt2, &type) ||
190 !PACKET_get_length_prefixed_2(&pkt2, &pkt3))
193 if (type == TLSEXT_TYPE_session_ticket) {
194 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
195 if (PACKET_equal(&pkt3, dummytick, strlen(dummytick))) {
196 /* Ticket data is as we expected */
199 printf("Received session ticket is not as expected\n");
204 if (type == TLSEXT_TYPE_padding) {
205 if (currtest == TEST_ADD_PADDING
206 || currtest == TEST_ADD_PADDING_AND_PSK)
207 testresult = (msglen == F5_WORKAROUND_MAX_MSG_LEN);
216 SSL_SESSION_free(sess);
219 printf("ClientHello test: FAILED (Test %d)\n", currtest);
224 int test_main(int argc, char *argv[])
229 sessionfile = argv[1];
231 ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);
233 return run_tests(argv[0]);