Remove old style NewSessionTicket from TLSv1.3
[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
19 #include "../ssl/packet_locl.h"
20
21 #define CLIENT_VERSION_LEN      2
22
23
24 #define TOTAL_NUM_TESTS                         1
25
26 /*
27  * Test that explicitly setting ticket data results in it appearing in the
28  * ClientHello for a negotiated SSL/TLS version
29  */
30 #define TEST_SET_SESSION_TICK_DATA_VER_NEG      0
31
32 int main(int argc, char *argv[])
33 {
34     SSL_CTX *ctx;
35     SSL *con;
36     BIO *rbio;
37     BIO *wbio;
38     BIO *err;
39     long len;
40     unsigned char *data;
41     PACKET pkt, pkt2, pkt3;
42     char *dummytick = "Hello World!";
43     unsigned int type;
44     int testresult = 0;
45     int currtest = 0;
46
47     err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
48
49     CRYPTO_set_mem_debug(1);
50     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
51
52     /*
53      * For each test set up an SSL_CTX and SSL and see what ClientHello gets
54      * produced when we try to connect
55      */
56     for (; currtest < TOTAL_NUM_TESTS; currtest++) {
57         testresult = 0;
58         ctx = SSL_CTX_new(TLS_method());
59
60         /*
61          * This test is testing session tickets for <= TLS1.2. It isn't relevant
62          * for TLS1.3
63          */
64         if (ctx == NULL || !SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION))
65             goto end;
66
67         con = SSL_new(ctx);
68         if (con == NULL)
69             goto end;
70
71         rbio = BIO_new(BIO_s_mem());
72         wbio = BIO_new(BIO_s_mem());
73         if (rbio == NULL || wbio == NULL) {
74             BIO_free(rbio);
75             BIO_free(wbio);
76             goto end;
77         }
78
79         SSL_set_bio(con, rbio, wbio);
80         SSL_set_connect_state(con);
81
82         if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
83             if (!SSL_set_session_ticket_ext(con, dummytick, strlen(dummytick)))
84                 goto end;
85         }
86
87         if (SSL_connect(con) > 0) {
88             /* This shouldn't succeed because we don't have a server! */
89             goto end;
90         }
91
92         len = BIO_get_mem_data(wbio, (char **)&data);
93         if (!PACKET_buf_init(&pkt, data, len))
94             goto end;
95
96         /* Skip the record header */
97         if (!PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
98             goto end;
99
100         /* Skip the handshake message header */
101         if (!PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
102             goto end;
103
104         /* Skip client version and random */
105         if (!PACKET_forward(&pkt, CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE))
106             goto end;
107
108         /* Skip session id */
109         if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
110             goto end;
111
112         /* Skip ciphers */
113         if (!PACKET_get_length_prefixed_2(&pkt, &pkt2))
114             goto end;
115
116         /* Skip compression */
117         if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
118             goto end;
119
120         /* Extensions len */
121         if (!PACKET_as_length_prefixed_2(&pkt, &pkt2))
122             goto end;
123
124         /* Loop through all extensions */
125         while (PACKET_remaining(&pkt2)) {
126
127             if (!PACKET_get_net_2(&pkt2, &type) ||
128                 !PACKET_get_length_prefixed_2(&pkt2, &pkt3))
129                 goto end;
130
131             if (type == TLSEXT_TYPE_session_ticket) {
132                 if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
133                     if (PACKET_equal(&pkt3, dummytick, strlen(dummytick))) {
134                         /* Ticket data is as we expected */
135                         testresult = 1;
136                     } else {
137                         printf("Received session ticket is not as expected\n");
138                     }
139                     break;
140                 }
141             }
142
143         }
144
145  end:
146         SSL_free(con);
147         SSL_CTX_free(ctx);
148         if (!testresult) {
149             printf("ClientHello test: FAILED (Test %d)\n", currtest);
150             break;
151         }
152     }
153
154 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
155     if (CRYPTO_mem_leaks(err) <= 0)
156         testresult = 0;
157 #endif
158     BIO_free(err);
159
160     return testresult?0:1;
161 }