Make default_method mostly compile-time
[openssl.git] / test / clienthellotest.c
1 /*
2  * Copyright 2015-2016 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_locl.h"
21
22 #include "testutil.h"
23 #include "test_main_custom.h"
24
25 #define CLIENT_VERSION_LEN      2
26
27 #define TOTAL_NUM_TESTS                         4
28
29 /*
30  * Test that explicitly setting ticket data results in it appearing in the
31  * ClientHello for a negotiated SSL/TLS version
32  */
33 #define TEST_SET_SESSION_TICK_DATA_VER_NEG      0
34 /* Enable padding and make sure ClientHello is long enough to require it */
35 #define TEST_ADD_PADDING                        1
36 /* Enable padding and make sure ClientHello is short enough to not need it */
37 #define TEST_PADDING_NOT_NEEDED                 2
38 /*
39  * Enable padding and add a PSK to the ClientHello (this will also ensure the
40  * ClientHello is long enough to need padding)
41  */
42 #define TEST_ADD_PADDING_AND_PSK                3
43
44 #define F5_WORKAROUND_MIN_MSG_LEN   0xff
45 #define F5_WORKAROUND_MAX_MSG_LEN   0x200
46
47 static const char *sessionfile = NULL;
48 /* Dummy ALPN protocols used to pad out the size of the ClientHello */
49 static const char alpn_prots[] =
50     "0123456789012345678901234567890123456789012345678901234567890123456789"
51     "0123456789012345678901234567890123456789012345678901234567890123456789";
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;
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     /*
75      * For each test set up an SSL_CTX and SSL and see what ClientHello gets
76      * produced when we try to connect
77      */
78     ctx = SSL_CTX_new(TLS_method());
79     if (ctx == NULL)
80         goto end;
81
82     switch(currtest) {
83     case TEST_SET_SESSION_TICK_DATA_VER_NEG:
84         /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
85         if (!SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION))
86             goto end;
87         break;
88
89     case TEST_ADD_PADDING_AND_PSK:
90     case TEST_ADD_PADDING:
91     case TEST_PADDING_NOT_NEEDED:
92         SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING);
93         /*
94          * Add lots of ciphersuites so that the ClientHello is at least
95          * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
96          * needed. Also add some dummy ALPN protocols in case we still don't
97          * have enough.
98          * In the padding not needed case we assume the test will pass, but then
99          * set testresult to 0 if we see the padding extension.
100          */
101         if (currtest == TEST_ADD_PADDING
102                 && (!SSL_CTX_set_cipher_list(ctx, "ALL")
103                     || SSL_CTX_set_alpn_protos(ctx,
104                                                (unsigned char *)alpn_prots,
105                                                sizeof(alpn_prots) - 1)))
106             goto end;
107         else if (currtest == TEST_PADDING_NOT_NEEDED)
108             testresult = 1;
109         break;
110
111     default:
112         goto end;
113     }
114
115     con = SSL_new(ctx);
116     if (con == NULL)
117         goto end;
118
119     if (currtest == TEST_ADD_PADDING_AND_PSK) {
120         sessbio = BIO_new_file(sessionfile, "r");
121         if (sessbio == NULL) {
122             printf("Unable to open session.pem\n");
123             goto end;
124         }
125         sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
126         if (sess == NULL) {
127             printf("Unable to load SSL_SESSION\n");
128             goto end;
129         }
130         /*
131          * We reset the creation time so that we don't discard the session as
132          * too old.
133          */
134         if (!SSL_SESSION_set_time(sess, time(NULL))) {
135             printf("Unable to set creation time on SSL_SESSION\n");
136             goto end;
137         }
138         if (!SSL_set_session(con, sess)) {
139             printf("Unable to set the session on the connection\n");
140             goto end;
141         }
142     }
143
144     rbio = BIO_new(BIO_s_mem());
145     wbio = BIO_new(BIO_s_mem());
146     if (rbio == NULL || wbio == NULL) {
147         BIO_free(rbio);
148         BIO_free(wbio);
149         goto end;
150     }
151
152     SSL_set_bio(con, rbio, wbio);
153     SSL_set_connect_state(con);
154
155     if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
156         if (!SSL_set_session_ticket_ext(con, dummytick, strlen(dummytick)))
157             goto end;
158     }
159
160     if (SSL_connect(con) > 0) {
161         /* This shouldn't succeed because we don't have a server! */
162         goto end;
163     }
164
165     len = BIO_get_mem_data(wbio, (char **)&data);
166     if (!PACKET_buf_init(&pkt, data, len))
167         goto end;
168
169     /* Skip the record header */
170     if (!PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
171         goto end;
172
173     msglen = PACKET_remaining(&pkt);
174
175     /* Skip the handshake message header */
176     if (!PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
177         goto end;
178
179     /* Skip client version and random */
180     if (!PACKET_forward(&pkt, CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE))
181         goto end;
182
183     /* Skip session id */
184     if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
185         goto end;
186
187     /* Skip ciphers */
188     if (!PACKET_get_length_prefixed_2(&pkt, &pkt2))
189         goto end;
190
191     /* Skip compression */
192     if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
193         goto end;
194
195     /* Extensions len */
196     if (!PACKET_as_length_prefixed_2(&pkt, &pkt2))
197         goto end;
198
199     /* Loop through all extensions */
200     while (PACKET_remaining(&pkt2)) {
201
202         if (!PACKET_get_net_2(&pkt2, &type) ||
203             !PACKET_get_length_prefixed_2(&pkt2, &pkt3))
204             goto end;
205
206         if (type == TLSEXT_TYPE_session_ticket) {
207             if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
208                 if (PACKET_equal(&pkt3, dummytick, strlen(dummytick))) {
209                     /* Ticket data is as we expected */
210                     testresult = 1;
211                 } else {
212                     printf("Received session ticket is not as expected\n");
213                 }
214                 break;
215             }
216         }
217         if (type == TLSEXT_TYPE_padding) {
218             if (currtest == TEST_ADD_PADDING
219                     || currtest == TEST_ADD_PADDING_AND_PSK)
220                 testresult = (msglen == F5_WORKAROUND_MAX_MSG_LEN);
221             else
222                 testresult = 0;
223         }
224     }
225
226 end:
227     SSL_free(con);
228     SSL_CTX_free(ctx);
229     SSL_SESSION_free(sess);
230     BIO_free(sessbio);
231     if (!testresult)
232         printf("ClientHello test: FAILED (Test %d)\n", currtest);
233
234     return testresult;
235 }
236
237 int test_main(int argc, char *argv[])
238 {
239     if (argc != 2)
240         return EXIT_FAILURE;
241
242     sessionfile = argv[1];
243
244     ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);
245
246     return run_tests(argv[0]);
247 }