2016-05-17 18:20:24 +00:00
|
|
|
/*
|
2017-07-18 01:48:27 +00:00
|
|
|
* Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
|
2015-07-27 11:04:47 +00:00
|
|
|
*
|
2016-05-17 18:20:24 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2015-07-27 11:04:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-02-03 23:25:49 +00:00
|
|
|
#include <openssl/opensslconf.h>
|
2015-07-27 11:04:47 +00:00
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/crypto.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
2017-03-16 12:11:23 +00:00
|
|
|
#include <time.h>
|
2015-07-27 11:04:47 +00:00
|
|
|
|
2016-08-05 13:58:32 +00:00
|
|
|
#include "../ssl/packet_locl.h"
|
2015-07-27 11:04:47 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
#include "testutil.h"
|
2015-07-27 11:04:47 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
#define CLIENT_VERSION_LEN 2
|
2015-07-27 11:04:47 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
#define TOTAL_NUM_TESTS 4
|
2015-07-27 11:04:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test that explicitly setting ticket data results in it appearing in the
|
|
|
|
* ClientHello for a negotiated SSL/TLS version
|
|
|
|
*/
|
2016-03-02 21:38:08 +00:00
|
|
|
#define TEST_SET_SESSION_TICK_DATA_VER_NEG 0
|
2017-03-16 12:11:23 +00:00
|
|
|
/* Enable padding and make sure ClientHello is long enough to require it */
|
|
|
|
#define TEST_ADD_PADDING 1
|
|
|
|
/* Enable padding and make sure ClientHello is short enough to not need it */
|
|
|
|
#define TEST_PADDING_NOT_NEEDED 2
|
|
|
|
/*
|
|
|
|
* Enable padding and add a PSK to the ClientHello (this will also ensure the
|
|
|
|
* ClientHello is long enough to need padding)
|
|
|
|
*/
|
|
|
|
#define TEST_ADD_PADDING_AND_PSK 3
|
|
|
|
|
|
|
|
#define F5_WORKAROUND_MIN_MSG_LEN 0xff
|
|
|
|
#define F5_WORKAROUND_MAX_MSG_LEN 0x200
|
|
|
|
|
2017-03-16 15:09:59 +00:00
|
|
|
static const char *sessionfile = NULL;
|
|
|
|
/* Dummy ALPN protocols used to pad out the size of the ClientHello */
|
|
|
|
static const char alpn_prots[] =
|
|
|
|
"0123456789012345678901234567890123456789012345678901234567890123456789"
|
|
|
|
"0123456789012345678901234567890123456789012345678901234567890123456789";
|
2015-07-27 11:04:47 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
static int test_client_hello(int currtest)
|
2015-07-27 11:04:47 +00:00
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
2016-11-24 09:19:04 +00:00
|
|
|
SSL *con = NULL;
|
2015-07-27 11:04:47 +00:00
|
|
|
BIO *rbio;
|
|
|
|
BIO *wbio;
|
|
|
|
long len;
|
|
|
|
unsigned char *data;
|
2017-04-26 23:00:08 +00:00
|
|
|
PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0};
|
2015-07-27 11:04:47 +00:00
|
|
|
char *dummytick = "Hello World!";
|
2017-04-11 16:36:01 +00:00
|
|
|
unsigned int type = 0;
|
2015-07-27 11:04:47 +00:00
|
|
|
int testresult = 0;
|
2017-03-16 12:11:23 +00:00
|
|
|
size_t msglen;
|
|
|
|
BIO *sessbio = NULL;
|
|
|
|
SSL_SESSION *sess = NULL;
|
2015-07-27 11:04:47 +00:00
|
|
|
|
2017-03-16 15:09:59 +00:00
|
|
|
#ifdef OPENSSL_NO_TLS1_3
|
|
|
|
if (currtest == TEST_ADD_PADDING_AND_PSK)
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
|
2015-07-27 11:04:47 +00:00
|
|
|
/*
|
|
|
|
* For each test set up an SSL_CTX and SSL and see what ClientHello gets
|
|
|
|
* produced when we try to connect
|
|
|
|
*/
|
2017-03-16 12:11:23 +00:00
|
|
|
ctx = SSL_CTX_new(TLS_method());
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_ptr(ctx))
|
2017-03-16 12:11:23 +00:00
|
|
|
goto end;
|
2016-11-08 16:10:21 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
switch(currtest) {
|
|
|
|
case TEST_SET_SESSION_TICK_DATA_VER_NEG:
|
2016-11-23 15:38:32 +00:00
|
|
|
/* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)))
|
2016-11-08 16:10:21 +00:00
|
|
|
goto end;
|
2017-03-16 12:11:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TEST_ADD_PADDING_AND_PSK:
|
|
|
|
case TEST_ADD_PADDING:
|
|
|
|
case TEST_PADDING_NOT_NEEDED:
|
|
|
|
SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING);
|
|
|
|
/*
|
|
|
|
* Add lots of ciphersuites so that the ClientHello is at least
|
|
|
|
* F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
|
2017-03-16 15:09:59 +00:00
|
|
|
* needed. Also add some dummy ALPN protocols in case we still don't
|
|
|
|
* have enough.
|
2017-03-16 12:11:23 +00:00
|
|
|
*/
|
|
|
|
if (currtest == TEST_ADD_PADDING
|
2017-04-11 16:36:01 +00:00
|
|
|
&& (!TEST_true(SSL_CTX_set_cipher_list(ctx, "ALL"))
|
|
|
|
|| !TEST_false(SSL_CTX_set_alpn_protos(ctx,
|
2017-03-16 15:09:59 +00:00
|
|
|
(unsigned char *)alpn_prots,
|
2017-04-11 16:36:01 +00:00
|
|
|
sizeof(alpn_prots) - 1))))
|
2016-11-08 16:10:21 +00:00
|
|
|
goto end;
|
2017-04-11 16:36:01 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
break;
|
2015-07-27 11:04:47 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
default:
|
|
|
|
goto end;
|
|
|
|
}
|
2016-11-08 16:10:21 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
con = SSL_new(ctx);
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_ptr(con))
|
2017-03-16 12:11:23 +00:00
|
|
|
goto end;
|
2015-07-27 11:04:47 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
if (currtest == TEST_ADD_PADDING_AND_PSK) {
|
|
|
|
sessbio = BIO_new_file(sessionfile, "r");
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_ptr(sessbio)) {
|
|
|
|
TEST_info("Unable to open session.pem");
|
2017-03-16 12:11:23 +00:00
|
|
|
goto end;
|
2015-07-27 11:04:47 +00:00
|
|
|
}
|
2017-03-16 12:11:23 +00:00
|
|
|
sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_ptr(sess)) {
|
|
|
|
TEST_info("Unable to load SSL_SESSION");
|
2015-07-27 11:04:47 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-03-16 12:11:23 +00:00
|
|
|
/*
|
|
|
|
* We reset the creation time so that we don't discard the session as
|
|
|
|
* too old.
|
|
|
|
*/
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_true(SSL_SESSION_set_time(sess, time(NULL)))
|
|
|
|
|| !TEST_true(SSL_set_session(con, sess)))
|
2016-08-05 13:58:32 +00:00
|
|
|
goto end;
|
2017-03-16 12:11:23 +00:00
|
|
|
}
|
2016-08-05 13:58:32 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
rbio = BIO_new(BIO_s_mem());
|
|
|
|
wbio = BIO_new(BIO_s_mem());
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
|
2017-03-16 12:11:23 +00:00
|
|
|
BIO_free(rbio);
|
|
|
|
BIO_free(wbio);
|
|
|
|
goto end;
|
|
|
|
}
|
2016-08-05 13:58:32 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
SSL_set_bio(con, rbio, wbio);
|
|
|
|
SSL_set_connect_state(con);
|
2016-08-05 13:58:32 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick,
|
|
|
|
strlen(dummytick))))
|
2015-07-27 11:04:47 +00:00
|
|
|
goto end;
|
2017-03-16 12:11:23 +00:00
|
|
|
}
|
2016-08-05 13:58:32 +00:00
|
|
|
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_int_le(SSL_connect(con), 0)) {
|
2017-03-16 12:11:23 +00:00
|
|
|
/* This shouldn't succeed because we don't have a server! */
|
|
|
|
goto end;
|
|
|
|
}
|
2016-08-05 13:58:32 +00:00
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
len = BIO_get_mem_data(wbio, (char **)&data);
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_true(PACKET_buf_init(&pkt, data, len))
|
|
|
|
/* Skip the record header */
|
|
|
|
|| !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
|
2017-03-16 12:11:23 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
msglen = PACKET_remaining(&pkt);
|
|
|
|
|
|
|
|
/* Skip the handshake message header */
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
|
|
|
|
/* Skip client version and random */
|
|
|
|
|| !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
|
|
|
|
+ SSL3_RANDOM_SIZE))
|
|
|
|
/* Skip session id */
|
|
|
|
|| !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
|
|
|
|
/* Skip ciphers */
|
|
|
|
|| !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
|
|
|
|
/* Skip compression */
|
|
|
|
|| !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
|
|
|
|
/* Extensions len */
|
|
|
|
|| !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
|
2017-03-16 12:11:23 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
/* Loop through all extensions */
|
|
|
|
while (PACKET_remaining(&pkt2)) {
|
|
|
|
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
|
|
|
|
|| !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
|
2015-07-27 11:04:47 +00:00
|
|
|
goto end;
|
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
if (type == TLSEXT_TYPE_session_ticket) {
|
|
|
|
if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
|
2017-04-11 16:36:01 +00:00
|
|
|
if (TEST_true(PACKET_equal(&pkt3, dummytick,
|
|
|
|
strlen(dummytick)))) {
|
2017-03-16 12:11:23 +00:00
|
|
|
/* Ticket data is as we expected */
|
|
|
|
testresult = 1;
|
2015-07-27 11:04:47 +00:00
|
|
|
}
|
2017-04-11 16:36:01 +00:00
|
|
|
goto end;
|
2015-07-27 11:04:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-16 12:11:23 +00:00
|
|
|
if (type == TLSEXT_TYPE_padding) {
|
2017-04-11 16:36:01 +00:00
|
|
|
if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED))
|
|
|
|
goto end;
|
|
|
|
else if (TEST_true(currtest == TEST_ADD_PADDING
|
|
|
|
|| currtest == TEST_ADD_PADDING_AND_PSK))
|
|
|
|
testresult = TEST_true(msglen == F5_WORKAROUND_MAX_MSG_LEN);
|
2015-07-27 11:04:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 16:36:01 +00:00
|
|
|
if (currtest == TEST_PADDING_NOT_NEEDED)
|
|
|
|
testresult = 1;
|
|
|
|
|
2017-03-16 12:11:23 +00:00
|
|
|
end:
|
|
|
|
SSL_free(con);
|
|
|
|
SSL_CTX_free(ctx);
|
|
|
|
SSL_SESSION_free(sess);
|
|
|
|
BIO_free(sessbio);
|
|
|
|
|
|
|
|
return testresult;
|
|
|
|
}
|
|
|
|
|
2017-07-18 01:48:27 +00:00
|
|
|
int setup_tests(void)
|
2017-03-16 12:11:23 +00:00
|
|
|
{
|
2017-07-18 01:48:27 +00:00
|
|
|
if (!TEST_ptr(sessionfile = test_get_argument(0)))
|
|
|
|
return 0;
|
2017-03-16 12:11:23 +00:00
|
|
|
|
|
|
|
ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);
|
2017-07-18 01:48:27 +00:00
|
|
|
return 1;
|
2015-07-27 11:04:47 +00:00
|
|
|
}
|