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