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