2 * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
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
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 "internal/packet.h"
24 #define CLIENT_VERSION_LEN 2
26 #define TOTAL_NUM_TESTS 4
29 * Test that explicitly setting ticket data results in it appearing in the
30 * ClientHello for a negotiated SSL/TLS version
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
38 * Enable padding and add a PSK to the ClientHello (this will also ensure the
39 * ClientHello is long enough to need padding)
41 #define TEST_ADD_PADDING_AND_PSK 3
43 #define F5_WORKAROUND_MIN_MSG_LEN 0x7f
44 #define F5_WORKAROUND_MAX_MSG_LEN 0x200
46 static const char *sessionfile
= NULL
;
47 /* Dummy ALPN protocols used to pad out the size of the ClientHello */
48 /* ASCII 'O' = 79 = 0x4F = EBCDIC '|'*/
50 static const char alpn_prots
[] =
51 "|1234567890123456789012345678901234567890123456789012345678901234567890123456789"
52 "|1234567890123456789012345678901234567890123456789012345678901234567890123456789";
54 static const char alpn_prots
[] =
55 "O1234567890123456789012345678901234567890123456789012345678901234567890123456789"
56 "O1234567890123456789012345678901234567890123456789012345678901234567890123456789";
59 static int test_client_hello(int currtest
)
67 PACKET pkt
, pkt2
, pkt3
;
68 char *dummytick
= "Hello World!";
69 unsigned int type
= 0;
73 SSL_SESSION
*sess
= NULL
;
75 #ifdef OPENSSL_NO_TLS1_3
76 if (currtest
== TEST_ADD_PADDING_AND_PSK
)
80 memset(&pkt
, 0, sizeof(pkt
));
81 memset(&pkt2
, 0, sizeof(pkt2
));
82 memset(&pkt3
, 0, sizeof(pkt3
));
85 * For each test set up an SSL_CTX and SSL and see what ClientHello gets
86 * produced when we try to connect
88 ctx
= SSL_CTX_new(TLS_method());
91 if (!TEST_true(SSL_CTX_set_max_proto_version(ctx
, 0)))
95 case TEST_SET_SESSION_TICK_DATA_VER_NEG
:
96 #if !defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_TLS1_2)
97 /* TLSv1.3 is enabled and TLSv1.2 is disabled so can't do this test */
101 /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
102 if (!TEST_true(SSL_CTX_set_max_proto_version(ctx
, TLS1_2_VERSION
)))
107 case TEST_ADD_PADDING_AND_PSK
:
109 * In this case we're doing TLSv1.3 and we're sending a PSK so the
110 * ClientHello is already going to be quite long. To avoid getting one
111 * that is too long for this test we use a restricted ciphersuite list
113 if (!TEST_false(SSL_CTX_set_cipher_list(ctx
, "")))
117 case TEST_ADD_PADDING
:
118 case TEST_PADDING_NOT_NEEDED
:
119 SSL_CTX_set_options(ctx
, SSL_OP_TLSEXT_PADDING
);
120 /* Make sure we get a consistent size across TLS versions */
121 SSL_CTX_clear_options(ctx
, SSL_OP_ENABLE_MIDDLEBOX_COMPAT
);
123 * Add some dummy ALPN protocols so that the ClientHello is at least
124 * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
127 if (currtest
== TEST_ADD_PADDING
) {
128 if (!TEST_false(SSL_CTX_set_alpn_protos(ctx
,
129 (unsigned char *)alpn_prots
,
130 sizeof(alpn_prots
) - 1)))
133 * Otherwise we need to make sure we have a small enough message to
136 } else if (!TEST_true(SSL_CTX_set_cipher_list(ctx
,
138 || !TEST_true(SSL_CTX_set_ciphersuites(ctx
,
139 "TLS_AES_128_GCM_SHA256"))) {
152 if (currtest
== TEST_ADD_PADDING_AND_PSK
) {
153 sessbio
= BIO_new_file(sessionfile
, "r");
154 if (!TEST_ptr(sessbio
)) {
155 TEST_info("Unable to open session.pem");
158 sess
= PEM_read_bio_SSL_SESSION(sessbio
, NULL
, NULL
, NULL
);
159 if (!TEST_ptr(sess
)) {
160 TEST_info("Unable to load SSL_SESSION");
164 * We reset the creation time so that we don't discard the session as
167 if (!TEST_true(SSL_SESSION_set_time(sess
, (long)time(NULL
)))
168 || !TEST_true(SSL_set_session(con
, sess
)))
172 rbio
= BIO_new(BIO_s_mem());
173 wbio
= BIO_new(BIO_s_mem());
174 if (!TEST_ptr(rbio
)|| !TEST_ptr(wbio
)) {
180 SSL_set_bio(con
, rbio
, wbio
);
181 SSL_set_connect_state(con
);
183 if (currtest
== TEST_SET_SESSION_TICK_DATA_VER_NEG
) {
184 if (!TEST_true(SSL_set_session_ticket_ext(con
, dummytick
,
189 if (!TEST_int_le(SSL_connect(con
), 0)) {
190 /* This shouldn't succeed because we don't have a server! */
194 if (!TEST_long_ge(len
= BIO_get_mem_data(wbio
, (char **)&data
), 0)
195 || !TEST_true(PACKET_buf_init(&pkt
, data
, len
))
196 /* Skip the record header */
197 || !PACKET_forward(&pkt
, SSL3_RT_HEADER_LENGTH
))
200 msglen
= PACKET_remaining(&pkt
);
202 /* Skip the handshake message header */
203 if (!TEST_true(PACKET_forward(&pkt
, SSL3_HM_HEADER_LENGTH
))
204 /* Skip client version and random */
205 || !TEST_true(PACKET_forward(&pkt
, CLIENT_VERSION_LEN
207 /* Skip session id */
208 || !TEST_true(PACKET_get_length_prefixed_1(&pkt
, &pkt2
))
210 || !TEST_true(PACKET_get_length_prefixed_2(&pkt
, &pkt2
))
211 /* Skip compression */
212 || !TEST_true(PACKET_get_length_prefixed_1(&pkt
, &pkt2
))
214 || !TEST_true(PACKET_as_length_prefixed_2(&pkt
, &pkt2
)))
217 /* Loop through all extensions */
218 while (PACKET_remaining(&pkt2
)) {
220 if (!TEST_true(PACKET_get_net_2(&pkt2
, &type
))
221 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2
, &pkt3
)))
224 if (type
== TLSEXT_TYPE_session_ticket
) {
225 if (currtest
== TEST_SET_SESSION_TICK_DATA_VER_NEG
) {
226 if (TEST_true(PACKET_equal(&pkt3
, dummytick
,
227 strlen(dummytick
)))) {
228 /* Ticket data is as we expected */
234 if (type
== TLSEXT_TYPE_padding
) {
235 if (!TEST_false(currtest
== TEST_PADDING_NOT_NEEDED
))
237 else if (TEST_true(currtest
== TEST_ADD_PADDING
238 || currtest
== TEST_ADD_PADDING_AND_PSK
))
239 testresult
= TEST_true(msglen
== F5_WORKAROUND_MAX_MSG_LEN
);
243 if (currtest
== TEST_PADDING_NOT_NEEDED
)
249 SSL_SESSION_free(sess
);
255 OPT_TEST_DECLARE_USAGE("sessionfile\n")
257 int setup_tests(void)
259 if (!test_skip_common_options()) {
260 TEST_error("Error parsing test options\n");
264 if (!TEST_ptr(sessionfile
= test_get_argument(0)))
267 ADD_ALL_TESTS(test_client_hello
, TOTAL_NUM_TESTS
);