1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <string.h> 11e1051a39Sopenharmony_ci 12e1051a39Sopenharmony_ci#include <openssl/opensslconf.h> 13e1051a39Sopenharmony_ci#include <openssl/bio.h> 14e1051a39Sopenharmony_ci#include <openssl/crypto.h> 15e1051a39Sopenharmony_ci#include <openssl/evp.h> 16e1051a39Sopenharmony_ci#include <openssl/ssl.h> 17e1051a39Sopenharmony_ci#include <openssl/err.h> 18e1051a39Sopenharmony_ci#include <time.h> 19e1051a39Sopenharmony_ci 20e1051a39Sopenharmony_ci#include "internal/packet.h" 21e1051a39Sopenharmony_ci 22e1051a39Sopenharmony_ci#include "testutil.h" 23e1051a39Sopenharmony_ci 24e1051a39Sopenharmony_ci#define CLIENT_VERSION_LEN 2 25e1051a39Sopenharmony_ci 26e1051a39Sopenharmony_ci#define TOTAL_NUM_TESTS 4 27e1051a39Sopenharmony_ci 28e1051a39Sopenharmony_ci/* 29e1051a39Sopenharmony_ci * Test that explicitly setting ticket data results in it appearing in the 30e1051a39Sopenharmony_ci * ClientHello for a negotiated SSL/TLS version 31e1051a39Sopenharmony_ci */ 32e1051a39Sopenharmony_ci#define TEST_SET_SESSION_TICK_DATA_VER_NEG 0 33e1051a39Sopenharmony_ci/* Enable padding and make sure ClientHello is long enough to require it */ 34e1051a39Sopenharmony_ci#define TEST_ADD_PADDING 1 35e1051a39Sopenharmony_ci/* Enable padding and make sure ClientHello is short enough to not need it */ 36e1051a39Sopenharmony_ci#define TEST_PADDING_NOT_NEEDED 2 37e1051a39Sopenharmony_ci/* 38e1051a39Sopenharmony_ci * Enable padding and add a PSK to the ClientHello (this will also ensure the 39e1051a39Sopenharmony_ci * ClientHello is long enough to need padding) 40e1051a39Sopenharmony_ci */ 41e1051a39Sopenharmony_ci#define TEST_ADD_PADDING_AND_PSK 3 42e1051a39Sopenharmony_ci 43e1051a39Sopenharmony_ci#define F5_WORKAROUND_MIN_MSG_LEN 0x7f 44e1051a39Sopenharmony_ci#define F5_WORKAROUND_MAX_MSG_LEN 0x200 45e1051a39Sopenharmony_ci 46e1051a39Sopenharmony_cistatic const char *sessionfile = NULL; 47e1051a39Sopenharmony_ci/* Dummy ALPN protocols used to pad out the size of the ClientHello */ 48e1051a39Sopenharmony_ci/* ASCII 'O' = 79 = 0x4F = EBCDIC '|'*/ 49e1051a39Sopenharmony_ci#ifdef CHARSET_EBCDIC 50e1051a39Sopenharmony_cistatic const char alpn_prots[] = 51e1051a39Sopenharmony_ci "|1234567890123456789012345678901234567890123456789012345678901234567890123456789" 52e1051a39Sopenharmony_ci "|1234567890123456789012345678901234567890123456789012345678901234567890123456789"; 53e1051a39Sopenharmony_ci#else 54e1051a39Sopenharmony_cistatic const char alpn_prots[] = 55e1051a39Sopenharmony_ci "O1234567890123456789012345678901234567890123456789012345678901234567890123456789" 56e1051a39Sopenharmony_ci "O1234567890123456789012345678901234567890123456789012345678901234567890123456789"; 57e1051a39Sopenharmony_ci#endif 58e1051a39Sopenharmony_ci 59e1051a39Sopenharmony_cistatic int test_client_hello(int currtest) 60e1051a39Sopenharmony_ci{ 61e1051a39Sopenharmony_ci SSL_CTX *ctx; 62e1051a39Sopenharmony_ci SSL *con = NULL; 63e1051a39Sopenharmony_ci BIO *rbio; 64e1051a39Sopenharmony_ci BIO *wbio; 65e1051a39Sopenharmony_ci long len; 66e1051a39Sopenharmony_ci unsigned char *data; 67e1051a39Sopenharmony_ci PACKET pkt, pkt2, pkt3; 68e1051a39Sopenharmony_ci char *dummytick = "Hello World!"; 69e1051a39Sopenharmony_ci unsigned int type = 0; 70e1051a39Sopenharmony_ci int testresult = 0; 71e1051a39Sopenharmony_ci size_t msglen; 72e1051a39Sopenharmony_ci BIO *sessbio = NULL; 73e1051a39Sopenharmony_ci SSL_SESSION *sess = NULL; 74e1051a39Sopenharmony_ci 75e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_TLS1_3 76e1051a39Sopenharmony_ci if (currtest == TEST_ADD_PADDING_AND_PSK) 77e1051a39Sopenharmony_ci return 1; 78e1051a39Sopenharmony_ci#endif 79e1051a39Sopenharmony_ci 80e1051a39Sopenharmony_ci memset(&pkt, 0, sizeof(pkt)); 81e1051a39Sopenharmony_ci memset(&pkt2, 0, sizeof(pkt2)); 82e1051a39Sopenharmony_ci memset(&pkt3, 0, sizeof(pkt3)); 83e1051a39Sopenharmony_ci 84e1051a39Sopenharmony_ci /* 85e1051a39Sopenharmony_ci * For each test set up an SSL_CTX and SSL and see what ClientHello gets 86e1051a39Sopenharmony_ci * produced when we try to connect 87e1051a39Sopenharmony_ci */ 88e1051a39Sopenharmony_ci ctx = SSL_CTX_new(TLS_method()); 89e1051a39Sopenharmony_ci if (!TEST_ptr(ctx)) 90e1051a39Sopenharmony_ci goto end; 91e1051a39Sopenharmony_ci if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, 0))) 92e1051a39Sopenharmony_ci goto end; 93e1051a39Sopenharmony_ci 94e1051a39Sopenharmony_ci switch(currtest) { 95e1051a39Sopenharmony_ci case TEST_SET_SESSION_TICK_DATA_VER_NEG: 96e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_TLS1_2) 97e1051a39Sopenharmony_ci /* TLSv1.3 is enabled and TLSv1.2 is disabled so can't do this test */ 98e1051a39Sopenharmony_ci SSL_CTX_free(ctx); 99e1051a39Sopenharmony_ci return 1; 100e1051a39Sopenharmony_ci#else 101e1051a39Sopenharmony_ci /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */ 102e1051a39Sopenharmony_ci if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION))) 103e1051a39Sopenharmony_ci goto end; 104e1051a39Sopenharmony_ci#endif 105e1051a39Sopenharmony_ci break; 106e1051a39Sopenharmony_ci 107e1051a39Sopenharmony_ci case TEST_ADD_PADDING_AND_PSK: 108e1051a39Sopenharmony_ci /* 109e1051a39Sopenharmony_ci * In this case we're doing TLSv1.3 and we're sending a PSK so the 110e1051a39Sopenharmony_ci * ClientHello is already going to be quite long. To avoid getting one 111e1051a39Sopenharmony_ci * that is too long for this test we use a restricted ciphersuite list 112e1051a39Sopenharmony_ci */ 113e1051a39Sopenharmony_ci if (!TEST_false(SSL_CTX_set_cipher_list(ctx, ""))) 114e1051a39Sopenharmony_ci goto end; 115e1051a39Sopenharmony_ci ERR_clear_error(); 116e1051a39Sopenharmony_ci /* Fall through */ 117e1051a39Sopenharmony_ci case TEST_ADD_PADDING: 118e1051a39Sopenharmony_ci case TEST_PADDING_NOT_NEEDED: 119e1051a39Sopenharmony_ci SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING); 120e1051a39Sopenharmony_ci /* Make sure we get a consistent size across TLS versions */ 121e1051a39Sopenharmony_ci SSL_CTX_clear_options(ctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT); 122e1051a39Sopenharmony_ci /* 123e1051a39Sopenharmony_ci * Add some dummy ALPN protocols so that the ClientHello is at least 124e1051a39Sopenharmony_ci * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be 125e1051a39Sopenharmony_ci * needed. 126e1051a39Sopenharmony_ci */ 127e1051a39Sopenharmony_ci if (currtest == TEST_ADD_PADDING) { 128e1051a39Sopenharmony_ci if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, 129e1051a39Sopenharmony_ci (unsigned char *)alpn_prots, 130e1051a39Sopenharmony_ci sizeof(alpn_prots) - 1))) 131e1051a39Sopenharmony_ci goto end; 132e1051a39Sopenharmony_ci /* 133e1051a39Sopenharmony_ci * Otherwise we need to make sure we have a small enough message to 134e1051a39Sopenharmony_ci * not need padding. 135e1051a39Sopenharmony_ci */ 136e1051a39Sopenharmony_ci } else if (!TEST_true(SSL_CTX_set_cipher_list(ctx, 137e1051a39Sopenharmony_ci "AES128-SHA")) 138e1051a39Sopenharmony_ci || !TEST_true(SSL_CTX_set_ciphersuites(ctx, 139e1051a39Sopenharmony_ci "TLS_AES_128_GCM_SHA256"))) { 140e1051a39Sopenharmony_ci goto end; 141e1051a39Sopenharmony_ci } 142e1051a39Sopenharmony_ci break; 143e1051a39Sopenharmony_ci 144e1051a39Sopenharmony_ci default: 145e1051a39Sopenharmony_ci goto end; 146e1051a39Sopenharmony_ci } 147e1051a39Sopenharmony_ci 148e1051a39Sopenharmony_ci con = SSL_new(ctx); 149e1051a39Sopenharmony_ci if (!TEST_ptr(con)) 150e1051a39Sopenharmony_ci goto end; 151e1051a39Sopenharmony_ci 152e1051a39Sopenharmony_ci if (currtest == TEST_ADD_PADDING_AND_PSK) { 153e1051a39Sopenharmony_ci sessbio = BIO_new_file(sessionfile, "r"); 154e1051a39Sopenharmony_ci if (!TEST_ptr(sessbio)) { 155e1051a39Sopenharmony_ci TEST_info("Unable to open session.pem"); 156e1051a39Sopenharmony_ci goto end; 157e1051a39Sopenharmony_ci } 158e1051a39Sopenharmony_ci sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL); 159e1051a39Sopenharmony_ci if (!TEST_ptr(sess)) { 160e1051a39Sopenharmony_ci TEST_info("Unable to load SSL_SESSION"); 161e1051a39Sopenharmony_ci goto end; 162e1051a39Sopenharmony_ci } 163e1051a39Sopenharmony_ci /* 164e1051a39Sopenharmony_ci * We reset the creation time so that we don't discard the session as 165e1051a39Sopenharmony_ci * too old. 166e1051a39Sopenharmony_ci */ 167e1051a39Sopenharmony_ci if (!TEST_true(SSL_SESSION_set_time(sess, (long)time(NULL))) 168e1051a39Sopenharmony_ci || !TEST_true(SSL_set_session(con, sess))) 169e1051a39Sopenharmony_ci goto end; 170e1051a39Sopenharmony_ci } 171e1051a39Sopenharmony_ci 172e1051a39Sopenharmony_ci rbio = BIO_new(BIO_s_mem()); 173e1051a39Sopenharmony_ci wbio = BIO_new(BIO_s_mem()); 174e1051a39Sopenharmony_ci if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) { 175e1051a39Sopenharmony_ci BIO_free(rbio); 176e1051a39Sopenharmony_ci BIO_free(wbio); 177e1051a39Sopenharmony_ci goto end; 178e1051a39Sopenharmony_ci } 179e1051a39Sopenharmony_ci 180e1051a39Sopenharmony_ci SSL_set_bio(con, rbio, wbio); 181e1051a39Sopenharmony_ci SSL_set_connect_state(con); 182e1051a39Sopenharmony_ci 183e1051a39Sopenharmony_ci if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) { 184e1051a39Sopenharmony_ci if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick, 185e1051a39Sopenharmony_ci strlen(dummytick)))) 186e1051a39Sopenharmony_ci goto end; 187e1051a39Sopenharmony_ci } 188e1051a39Sopenharmony_ci 189e1051a39Sopenharmony_ci if (!TEST_int_le(SSL_connect(con), 0)) { 190e1051a39Sopenharmony_ci /* This shouldn't succeed because we don't have a server! */ 191e1051a39Sopenharmony_ci goto end; 192e1051a39Sopenharmony_ci } 193e1051a39Sopenharmony_ci 194e1051a39Sopenharmony_ci if (!TEST_long_ge(len = BIO_get_mem_data(wbio, (char **)&data), 0) 195e1051a39Sopenharmony_ci || !TEST_true(PACKET_buf_init(&pkt, data, len)) 196e1051a39Sopenharmony_ci /* Skip the record header */ 197e1051a39Sopenharmony_ci || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)) 198e1051a39Sopenharmony_ci goto end; 199e1051a39Sopenharmony_ci 200e1051a39Sopenharmony_ci msglen = PACKET_remaining(&pkt); 201e1051a39Sopenharmony_ci 202e1051a39Sopenharmony_ci /* Skip the handshake message header */ 203e1051a39Sopenharmony_ci if (!TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH)) 204e1051a39Sopenharmony_ci /* Skip client version and random */ 205e1051a39Sopenharmony_ci || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN 206e1051a39Sopenharmony_ci + SSL3_RANDOM_SIZE)) 207e1051a39Sopenharmony_ci /* Skip session id */ 208e1051a39Sopenharmony_ci || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) 209e1051a39Sopenharmony_ci /* Skip ciphers */ 210e1051a39Sopenharmony_ci || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2)) 211e1051a39Sopenharmony_ci /* Skip compression */ 212e1051a39Sopenharmony_ci || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) 213e1051a39Sopenharmony_ci /* Extensions len */ 214e1051a39Sopenharmony_ci || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2))) 215e1051a39Sopenharmony_ci goto end; 216e1051a39Sopenharmony_ci 217e1051a39Sopenharmony_ci /* Loop through all extensions */ 218e1051a39Sopenharmony_ci while (PACKET_remaining(&pkt2)) { 219e1051a39Sopenharmony_ci 220e1051a39Sopenharmony_ci if (!TEST_true(PACKET_get_net_2(&pkt2, &type)) 221e1051a39Sopenharmony_ci || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3))) 222e1051a39Sopenharmony_ci goto end; 223e1051a39Sopenharmony_ci 224e1051a39Sopenharmony_ci if (type == TLSEXT_TYPE_session_ticket) { 225e1051a39Sopenharmony_ci if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) { 226e1051a39Sopenharmony_ci if (TEST_true(PACKET_equal(&pkt3, dummytick, 227e1051a39Sopenharmony_ci strlen(dummytick)))) { 228e1051a39Sopenharmony_ci /* Ticket data is as we expected */ 229e1051a39Sopenharmony_ci testresult = 1; 230e1051a39Sopenharmony_ci } 231e1051a39Sopenharmony_ci goto end; 232e1051a39Sopenharmony_ci } 233e1051a39Sopenharmony_ci } 234e1051a39Sopenharmony_ci if (type == TLSEXT_TYPE_padding) { 235e1051a39Sopenharmony_ci if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED)) 236e1051a39Sopenharmony_ci goto end; 237e1051a39Sopenharmony_ci else if (TEST_true(currtest == TEST_ADD_PADDING 238e1051a39Sopenharmony_ci || currtest == TEST_ADD_PADDING_AND_PSK)) 239e1051a39Sopenharmony_ci testresult = TEST_true(msglen == F5_WORKAROUND_MAX_MSG_LEN); 240e1051a39Sopenharmony_ci } 241e1051a39Sopenharmony_ci } 242e1051a39Sopenharmony_ci 243e1051a39Sopenharmony_ci if (currtest == TEST_PADDING_NOT_NEEDED) 244e1051a39Sopenharmony_ci testresult = 1; 245e1051a39Sopenharmony_ci 246e1051a39Sopenharmony_ciend: 247e1051a39Sopenharmony_ci SSL_free(con); 248e1051a39Sopenharmony_ci SSL_CTX_free(ctx); 249e1051a39Sopenharmony_ci SSL_SESSION_free(sess); 250e1051a39Sopenharmony_ci BIO_free(sessbio); 251e1051a39Sopenharmony_ci 252e1051a39Sopenharmony_ci return testresult; 253e1051a39Sopenharmony_ci} 254e1051a39Sopenharmony_ci 255e1051a39Sopenharmony_ciOPT_TEST_DECLARE_USAGE("sessionfile\n") 256e1051a39Sopenharmony_ci 257e1051a39Sopenharmony_ciint setup_tests(void) 258e1051a39Sopenharmony_ci{ 259e1051a39Sopenharmony_ci if (!test_skip_common_options()) { 260e1051a39Sopenharmony_ci TEST_error("Error parsing test options\n"); 261e1051a39Sopenharmony_ci return 0; 262e1051a39Sopenharmony_ci } 263e1051a39Sopenharmony_ci 264e1051a39Sopenharmony_ci if (!TEST_ptr(sessionfile = test_get_argument(0))) 265e1051a39Sopenharmony_ci return 0; 266e1051a39Sopenharmony_ci 267e1051a39Sopenharmony_ci ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS); 268e1051a39Sopenharmony_ci return 1; 269e1051a39Sopenharmony_ci} 270