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