]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/clienthellotest.c
If memory debugging enabled return error on leaks.
[thirdparty/openssl.git] / test / clienthellotest.c
1 /* Written by Matt Caswell for the OpenSSL Project */
2 /* ====================================================================
3 * Copyright (c) 1998-2015 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56 #include <string.h>
57
58 #include <openssl/opensslconf.h>
59 #include <openssl/bio.h>
60 #include <openssl/crypto.h>
61 #include <openssl/evp.h>
62 #include <openssl/ssl.h>
63 #include <openssl/err.h>
64
65
66 #define CLIENT_VERSION_LEN 2
67 #define SESSION_ID_LEN_LEN 1
68 #define CIPHERS_LEN_LEN 2
69 #define COMPRESSION_LEN_LEN 1
70 #define EXTENSIONS_LEN_LEN 2
71 #define EXTENSION_TYPE_LEN 2
72 #define EXTENSION_SIZE_LEN 2
73
74
75 #define TOTAL_NUM_TESTS 2
76
77 /*
78 * Test that explicitly setting ticket data results in it appearing in the
79 * ClientHello for TLS1.2
80 */
81 #define TEST_SET_SESSION_TICK_DATA_TLS_1_2 0
82
83 /*
84 * Test that explicitly setting ticket data results in it appearing in the
85 * ClientHello for a negotiated SSL/TLS version
86 */
87 #define TEST_SET_SESSION_TICK_DATA_VER_NEG 1
88
89 int main(int argc, char *argv[])
90 {
91 SSL_CTX *ctx;
92 SSL *con;
93 BIO *rbio;
94 BIO *wbio;
95 BIO *err;
96 long len;
97 unsigned char *data;
98 unsigned char *dataend;
99 char *dummytick = "Hello World!";
100 unsigned int tmplen;
101 unsigned int type;
102 unsigned int size;
103 int testresult = 0;
104 int currtest = 0;
105
106 SSL_library_init();
107 SSL_load_error_strings();
108
109 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
110
111 CRYPTO_set_mem_debug(1);
112 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
113
114 /*
115 * For each test set up an SSL_CTX and SSL and see what ClientHello gets
116 * produced when we try to connect
117 */
118 for (; currtest < TOTAL_NUM_TESTS; currtest++) {
119 testresult = 0;
120 if (currtest == TEST_SET_SESSION_TICK_DATA_TLS_1_2) {
121 #ifndef OPENSSL_NO_TLS1_2
122 ctx = SSL_CTX_new(TLSv1_2_method());
123 #else
124 continue;
125 #endif
126 } else {
127 ctx = SSL_CTX_new(TLS_method());
128 }
129 con = SSL_new(ctx);
130
131 rbio = BIO_new(BIO_s_mem());
132 wbio = BIO_new(BIO_s_mem());
133 SSL_set_bio(con, rbio, wbio);
134 SSL_set_connect_state(con);
135
136 if (currtest == TEST_SET_SESSION_TICK_DATA_TLS_1_2
137 || currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
138 if (!SSL_set_session_ticket_ext(con, dummytick, strlen(dummytick)))
139 goto end;
140 }
141
142 if (SSL_connect(con) > 0) {
143 /* This shouldn't succeed because we don't have a server! */
144 goto end;
145 }
146
147 len = BIO_get_mem_data(wbio, (char **)&data);
148 dataend = data + len;
149
150 /* Skip the record header */
151 data += SSL3_RT_HEADER_LENGTH;
152 /* Skip the handshake message header */
153 data += SSL3_HM_HEADER_LENGTH;
154 /* Skip client version and random */
155 data += CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE;
156 if (data + SESSION_ID_LEN_LEN > dataend)
157 goto end;
158 /* Skip session id */
159 tmplen = *data;
160 data += SESSION_ID_LEN_LEN + tmplen;
161 if (data + CIPHERS_LEN_LEN > dataend)
162 goto end;
163 /* Skip ciphers */
164 tmplen = ((*data) << 8) | *(data + 1);
165 data += CIPHERS_LEN_LEN + tmplen;
166 if (data + COMPRESSION_LEN_LEN > dataend)
167 goto end;
168 /* Skip compression */
169 tmplen = *data;
170 data += COMPRESSION_LEN_LEN + tmplen;
171 if (data + EXTENSIONS_LEN_LEN > dataend)
172 goto end;
173 /* Extensions len */
174 tmplen = ((*data) << 8) | *(data + 1);
175 data += EXTENSIONS_LEN_LEN;
176 if (data + tmplen > dataend)
177 goto end;
178
179 /* Loop through all extensions */
180 while (tmplen > EXTENSION_TYPE_LEN + EXTENSION_SIZE_LEN) {
181 type = ((*data) << 8) | *(data + 1);
182 data += EXTENSION_TYPE_LEN;
183 size = ((*data) << 8) | *(data + 1);
184 data += EXTENSION_SIZE_LEN;
185 if (data + size > dataend)
186 goto end;
187
188 if (type == TLSEXT_TYPE_session_ticket) {
189 if (currtest == TEST_SET_SESSION_TICK_DATA_TLS_1_2
190 || currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
191 if (size == strlen(dummytick)
192 && memcmp(data, dummytick, size) == 0) {
193 /* Ticket data is as we expected */
194 testresult = 1;
195 } else {
196 printf("Received session ticket is not as expected\n");
197 }
198 break;
199 }
200 }
201
202 tmplen -= EXTENSION_TYPE_LEN + EXTENSION_SIZE_LEN + size;
203 data += size;
204 }
205
206 end:
207 SSL_free(con);
208 SSL_CTX_free(ctx);
209 if (!testresult) {
210 printf("ClientHello test: FAILED (Test %d)\n", currtest);
211 break;
212 }
213 }
214
215 ERR_free_strings();
216 ERR_remove_thread_state(NULL);
217 EVP_cleanup();
218 CRYPTO_cleanup_all_ex_data();
219 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
220 if (CRYPTO_mem_leaks(err) <= 0)
221 testresult = 0;
222 #endif
223 BIO_free(err);
224
225 return testresult?0:1;
226 }