efdea942b736b4ee1d8791f375113fe756c85043
[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 const char *sessionfile = NULL;
48
49 static int test_client_hello(int currtest)
50 {
51     SSL_CTX *ctx;
52     SSL *con = NULL;
53     BIO *rbio;
54     BIO *wbio;
55     long len;
56     unsigned char *data;
57     PACKET pkt, pkt2, pkt3;
58     char *dummytick = "Hello World!";
59     unsigned int type;
60     int testresult = 0;
61     size_t msglen;
62     BIO *sessbio = NULL;
63     SSL_SESSION *sess = NULL;
64
65     /*
66      * For each test set up an SSL_CTX and SSL and see what ClientHello gets
67      * produced when we try to connect
68      */
69     ctx = SSL_CTX_new(TLS_method());
70     if (ctx == NULL)
71         goto end;
72
73     switch(currtest) {
74     case TEST_SET_SESSION_TICK_DATA_VER_NEG:
75         /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
76         if (!SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION))
77             goto end;
78         break;
79
80     case TEST_ADD_PADDING_AND_PSK:
81     case TEST_ADD_PADDING:
82     case TEST_PADDING_NOT_NEEDED:
83         SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING);
84         /*
85          * Add lots of ciphersuites so that the ClientHello is at least
86          * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
87          * needed.
88          * In the padding not needed case we assume the test will pass, but then
89          * set testresult to 0 if we see the padding extension.
90          */
91         if (currtest == TEST_ADD_PADDING
92                 && !SSL_CTX_set_cipher_list(ctx, "ALL"))
93             goto end;
94         else if (currtest == TEST_PADDING_NOT_NEEDED)
95             testresult = 1;
96         break;
97
98     default:
99         goto end;
100     }
101
102     con = SSL_new(ctx);
103     if (con == NULL)
104         goto end;
105
106     if (currtest == TEST_ADD_PADDING_AND_PSK) {
107         sessbio = BIO_new_file(sessionfile, "r");
108         if (sessbio == NULL) {
109             printf("Unable to open session.pem\n");
110             goto end;
111         }
112         sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
113         if (sess == NULL) {
114             printf("Unable to load SSL_SESSION\n");
115             goto end;
116         }
117         /*
118          * We reset the creation time so that we don't discard the session as
119          * too old.
120          */
121         if (!SSL_SESSION_set_time(sess, time(NULL))) {
122             printf("Unable to set creation time on SSL_SESSION\n");
123             goto end;
124         }
125         if (!SSL_set_session(con, sess)) {
126             printf("Unable to set the session on the connection\n");
127             goto end;
128         }
129     }
130
131     rbio = BIO_new(BIO_s_mem());
132     wbio = BIO_new(BIO_s_mem());
133     if (rbio == NULL || wbio == NULL) {
134         BIO_free(rbio);
135         BIO_free(wbio);
136         goto end;
137     }
138
139     SSL_set_bio(con, rbio, wbio);
140     SSL_set_connect_state(con);
141
142     if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
143         if (!SSL_set_session_ticket_ext(con, dummytick, strlen(dummytick)))
144             goto end;
145     }
146
147     if (SSL_connect(con) > 0) {
148         /* This shouldn't succeed because we don't have a server! */
149         goto end;
150     }
151
152     len = BIO_get_mem_data(wbio, (char **)&data);
153     if (!PACKET_buf_init(&pkt, data, len))
154         goto end;
155
156     /* Skip the record header */
157     if (!PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
158         goto end;
159
160     msglen = PACKET_remaining(&pkt);
161
162     /* Skip the handshake message header */
163     if (!PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
164         goto end;
165
166     /* Skip client version and random */
167     if (!PACKET_forward(&pkt, CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE))
168         goto end;
169
170     /* Skip session id */
171     if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
172         goto end;
173
174     /* Skip ciphers */
175     if (!PACKET_get_length_prefixed_2(&pkt, &pkt2))
176         goto end;
177
178     /* Skip compression */
179     if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
180         goto end;
181
182     /* Extensions len */
183     if (!PACKET_as_length_prefixed_2(&pkt, &pkt2))
184         goto end;
185
186     /* Loop through all extensions */
187     while (PACKET_remaining(&pkt2)) {
188
189         if (!PACKET_get_net_2(&pkt2, &type) ||
190             !PACKET_get_length_prefixed_2(&pkt2, &pkt3))
191             goto end;
192
193         if (type == TLSEXT_TYPE_session_ticket) {
194             if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
195                 if (PACKET_equal(&pkt3, dummytick, strlen(dummytick))) {
196                     /* Ticket data is as we expected */
197                     testresult = 1;
198                 } else {
199                     printf("Received session ticket is not as expected\n");
200                 }
201                 break;
202             }
203         }
204         if (type == TLSEXT_TYPE_padding) {
205             if (currtest == TEST_ADD_PADDING
206                     || currtest == TEST_ADD_PADDING_AND_PSK)
207                 testresult = (msglen == F5_WORKAROUND_MAX_MSG_LEN);
208             else
209                 testresult = 0;
210         }
211     }
212
213 end:
214     SSL_free(con);
215     SSL_CTX_free(ctx);
216     SSL_SESSION_free(sess);
217     BIO_free(sessbio);
218     if (!testresult)
219         printf("ClientHello test: FAILED (Test %d)\n", currtest);
220
221     return testresult;
222 }
223
224 int test_main(int argc, char *argv[])
225 {
226     if (argc != 2)
227         return 0;
228
229     sessionfile = argv[1];
230
231     ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);
232
233     return run_tests(argv[0]);
234 }