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