1 /* 2 * Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (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 8 */ 9 10 #include <string.h> 11 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> 18 #include <time.h> 19 20 #include "internal/packet.h" 21 22 #include "testutil.h" 23 24 #define CLIENT_VERSION_LEN 2 25 26 #define TOTAL_NUM_TESTS 3 27 28 /* 29 * Test that explicitly setting ticket data results in it appearing in the 30 * ClientHello for a negotiated SSL/TLS version 31 */ 32 #define TEST_SET_SESSION_TICK_DATA_VER_NEG 0 33 /* Enable padding and make sure ClientHello is long enough to require it */ 34 #define TEST_ADD_PADDING 1 35 /* Enable padding and make sure ClientHello is short enough to not need it */ 36 #define TEST_PADDING_NOT_NEEDED 2 37 38 #define F5_WORKAROUND_MIN_MSG_LEN 0x7f 39 #define F5_WORKAROUND_MAX_MSG_LEN 0x200 40 41 /* Dummy ALPN protocols used to pad out the size of the ClientHello */ 42 /* ASCII 'O' = 79 = 0x4F = EBCDIC '|'*/ 43 #ifdef CHARSET_EBCDIC 44 static const char alpn_prots[] = 45 "|1234567890123456789012345678901234567890123456789012345678901234567890123456789" 46 "|1234567890123456789012345678901234567890123456789012345678901234567890123456789"; 47 #else 48 static const char alpn_prots[] = 49 "O1234567890123456789012345678901234567890123456789012345678901234567890123456789" 50 "O1234567890123456789012345678901234567890123456789012345678901234567890123456789"; 51 #endif 52 53 static int test_client_hello(int currtest) 54 { 55 SSL_CTX *ctx; 56 SSL *con = NULL; 57 BIO *rbio; 58 BIO *wbio; 59 long len; 60 unsigned char *data; 61 PACKET pkt, pkt2, pkt3; 62 char *dummytick = "Hello World!"; 63 unsigned int type = 0; 64 int testresult = 0; 65 size_t msglen; 66 BIO *sessbio = NULL; 67 SSL_SESSION *sess = NULL; 68 69 memset(&pkt, 0, sizeof(pkt)); 70 memset(&pkt2, 0, sizeof(pkt2)); 71 memset(&pkt3, 0, sizeof(pkt3)); 72 73 /* 74 * For each test set up an SSL_CTX and SSL and see what ClientHello gets 75 * produced when we try to connect 76 */ 77 ctx = SSL_CTX_new(TLS_method()); 78 if (!TEST_ptr(ctx)) 79 goto end; 80 if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, 0))) 81 goto end; 82 83 switch (currtest) { 84 case TEST_SET_SESSION_TICK_DATA_VER_NEG: 85 #if !defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_TLS1_2) 86 /* TLSv1.3 is enabled and TLSv1.2 is disabled so can't do this test */ 87 SSL_CTX_free(ctx); 88 return 1; 89 #else 90 /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */ 91 if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION))) 92 goto end; 93 #endif 94 break; 95 96 case TEST_ADD_PADDING: 97 case TEST_PADDING_NOT_NEEDED: 98 SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING); 99 /* Make sure we get a consistent size across TLS versions */ 100 SSL_CTX_clear_options(ctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT); 101 /* Avoid large keyshares */ 102 if (!TEST_true(SSL_CTX_set1_groups_list(ctx, 103 "?X25519:?secp256r1:?ffdhe2048:?ffdhe3072"))) 104 goto end; 105 /* 106 * Add some dummy ALPN protocols so that the ClientHello is at least 107 * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be 108 * needed. 109 */ 110 if (currtest == TEST_ADD_PADDING) { 111 if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, 112 (unsigned char *)alpn_prots, 113 sizeof(alpn_prots) - 1))) 114 goto end; 115 /* 116 * Otherwise we need to make sure we have a small enough message to 117 * not need padding. 118 */ 119 } else if (!TEST_true(SSL_CTX_set_cipher_list(ctx, 120 "AES128-SHA")) 121 || !TEST_true(SSL_CTX_set_ciphersuites(ctx, 122 "TLS_AES_128_GCM_SHA256"))) { 123 goto end; 124 } 125 break; 126 127 default: 128 goto end; 129 } 130 131 con = SSL_new(ctx); 132 if (!TEST_ptr(con)) 133 goto end; 134 135 rbio = BIO_new(BIO_s_mem()); 136 wbio = BIO_new(BIO_s_mem()); 137 if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) { 138 BIO_free(rbio); 139 BIO_free(wbio); 140 goto end; 141 } 142 143 SSL_set_bio(con, rbio, wbio); 144 SSL_set_connect_state(con); 145 146 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) { 147 if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick, 148 strlen(dummytick)))) 149 goto end; 150 } 151 152 if (!TEST_int_le(SSL_connect(con), 0)) { 153 /* This shouldn't succeed because we don't have a server! */ 154 goto end; 155 } 156 157 if (!TEST_long_ge(len = BIO_get_mem_data(wbio, (char **)&data), 0) 158 || !TEST_true(PACKET_buf_init(&pkt, data, len)) 159 /* Skip the record header */ 160 || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)) 161 goto end; 162 163 msglen = PACKET_remaining(&pkt); 164 165 /* Skip the handshake message header */ 166 if (!TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH)) 167 /* Skip client version and random */ 168 || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN 169 + SSL3_RANDOM_SIZE)) 170 /* Skip session id */ 171 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) 172 /* Skip ciphers */ 173 || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2)) 174 /* Skip compression */ 175 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) 176 /* Extensions len */ 177 || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2))) 178 goto end; 179 180 /* Loop through all extensions */ 181 while (PACKET_remaining(&pkt2)) { 182 183 if (!TEST_true(PACKET_get_net_2(&pkt2, &type)) 184 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3))) 185 goto end; 186 187 if (type == TLSEXT_TYPE_session_ticket) { 188 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) { 189 if (TEST_true(PACKET_equal(&pkt3, dummytick, 190 strlen(dummytick)))) { 191 /* Ticket data is as we expected */ 192 testresult = 1; 193 } 194 goto end; 195 } 196 } 197 if (type == TLSEXT_TYPE_padding) { 198 if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED)) 199 goto end; 200 else if (TEST_true(currtest == TEST_ADD_PADDING)) 201 testresult = TEST_true(msglen == F5_WORKAROUND_MAX_MSG_LEN); 202 } 203 } 204 205 if (currtest == TEST_PADDING_NOT_NEEDED) 206 testresult = 1; 207 208 end: 209 SSL_free(con); 210 SSL_CTX_free(ctx); 211 SSL_SESSION_free(sess); 212 BIO_free(sessbio); 213 214 return testresult; 215 } 216 217 int setup_tests(void) 218 { 219 if (!test_skip_common_options()) { 220 TEST_error("Error parsing test options\n"); 221 return 0; 222 } 223 224 ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS); 225 return 1; 226 } 227