Providers: for the digest_final operation, pass a output buffer size
[openssl.git] / test / clienthellotest.c
1 /*
2  * Copyright 2015-2018 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 "../ssl/packet_locl.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 static const char alpn_prots[] =
49     "0123456789012345678901234567890123456789012345678901234567890123456789"
50     "0123456789012345678901234567890123456789012345678901234567890123456789"
51     "01234567890123456789";
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 #ifdef OPENSSL_NO_TLS1_3
70     if (currtest == TEST_ADD_PADDING_AND_PSK)
71         return 1;
72 #endif
73
74     memset(&pkt, 0, sizeof(pkt));
75     memset(&pkt2, 0, sizeof(pkt2));
76     memset(&pkt3, 0, sizeof(pkt3));
77
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      */
82     ctx = SSL_CTX_new(TLS_method());
83     if (!TEST_ptr(ctx))
84         goto end;
85     if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, 0)))
86         goto end;
87
88     switch(currtest) {
89     case TEST_SET_SESSION_TICK_DATA_VER_NEG:
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 */
92         return 1;
93 #else
94         /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
95         if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)))
96             goto end;
97 #endif
98         break;
99
100     case TEST_ADD_PADDING_AND_PSK:
101         /*
102          * In this case we're doing TLSv1.3 and we're sending a PSK so the
103          * ClientHello is already going to be quite long. To avoid getting one
104          * that is too long for this test we use a restricted ciphersuite list
105          */
106         if (!TEST_false(SSL_CTX_set_cipher_list(ctx, "")))
107             goto end;
108         ERR_clear_error();
109          /* Fall through */
110     case TEST_ADD_PADDING:
111     case TEST_PADDING_NOT_NEEDED:
112         SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING);
113         /* Make sure we get a consistent size across TLS versions */
114         SSL_CTX_clear_options(ctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
115         /*
116          * Add some dummy ALPN protocols so that the ClientHello is at least
117          * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
118          * needed.
119          */
120         if (currtest == TEST_ADD_PADDING) {
121              if (!TEST_false(SSL_CTX_set_alpn_protos(ctx,
122                                     (unsigned char *)alpn_prots,
123                                     sizeof(alpn_prots) - 1)))
124                 goto end;
125         /*
126          * Otherwise we need to make sure we have a small enough message to
127          * not need padding.
128          */
129         } else if (!TEST_true(SSL_CTX_set_cipher_list(ctx,
130                               "AES128-SHA"))
131                    || !TEST_true(SSL_CTX_set_ciphersuites(ctx,
132                                  "TLS_AES_128_GCM_SHA256"))) {
133             goto end;
134         }
135         break;
136
137     default:
138         goto end;
139     }
140
141     con = SSL_new(ctx);
142     if (!TEST_ptr(con))
143         goto end;
144
145     if (currtest == TEST_ADD_PADDING_AND_PSK) {
146         sessbio = BIO_new_file(sessionfile, "r");
147         if (!TEST_ptr(sessbio)) {
148             TEST_info("Unable to open session.pem");
149             goto end;
150         }
151         sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
152         if (!TEST_ptr(sess)) {
153             TEST_info("Unable to load SSL_SESSION");
154             goto end;
155         }
156         /*
157          * We reset the creation time so that we don't discard the session as
158          * too old.
159          */
160         if (!TEST_true(SSL_SESSION_set_time(sess, (long)time(NULL)))
161                 || !TEST_true(SSL_set_session(con, sess)))
162             goto end;
163     }
164
165     rbio = BIO_new(BIO_s_mem());
166     wbio = BIO_new(BIO_s_mem());
167     if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
168         BIO_free(rbio);
169         BIO_free(wbio);
170         goto end;
171     }
172
173     SSL_set_bio(con, rbio, wbio);
174     SSL_set_connect_state(con);
175
176     if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
177         if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick,
178                                                   strlen(dummytick))))
179             goto end;
180     }
181
182     if (!TEST_int_le(SSL_connect(con), 0)) {
183         /* This shouldn't succeed because we don't have a server! */
184         goto end;
185     }
186
187     len = BIO_get_mem_data(wbio, (char **)&data);
188     if (!TEST_true(PACKET_buf_init(&pkt, data, len))
189                /* Skip the record header */
190             || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
191         goto end;
192
193     msglen = PACKET_remaining(&pkt);
194
195     /* Skip the handshake message header */
196     if (!TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
197                /* Skip client version and random */
198             || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
199                                                + SSL3_RANDOM_SIZE))
200                /* Skip session id */
201             || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
202                /* Skip ciphers */
203             || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
204                /* Skip compression */
205             || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
206                /* Extensions len */
207             || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
208         goto end;
209
210     /* Loop through all extensions */
211     while (PACKET_remaining(&pkt2)) {
212
213         if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
214                 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
215             goto end;
216
217         if (type == TLSEXT_TYPE_session_ticket) {
218             if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
219                 if (TEST_true(PACKET_equal(&pkt3, dummytick,
220                                            strlen(dummytick)))) {
221                     /* Ticket data is as we expected */
222                     testresult = 1;
223                 }
224                 goto end;
225             }
226         }
227         if (type == TLSEXT_TYPE_padding) {
228             if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED))
229                 goto end;
230             else if (TEST_true(currtest == TEST_ADD_PADDING
231                     || currtest == TEST_ADD_PADDING_AND_PSK))
232                 testresult = TEST_true(msglen == F5_WORKAROUND_MAX_MSG_LEN);
233         }
234     }
235
236     if (currtest == TEST_PADDING_NOT_NEEDED)
237         testresult = 1;
238
239 end:
240     SSL_free(con);
241     SSL_CTX_free(ctx);
242     SSL_SESSION_free(sess);
243     BIO_free(sessbio);
244
245     return testresult;
246 }
247
248 OPT_TEST_DECLARE_USAGE("sessionfile\n")
249
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 }