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