]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/clienthellotest.c
Enable/disable crypto-mdebug just like other features
[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/bio.h>
59 #include <openssl/crypto.h>
60 #include <openssl/evp.h>
61 #include <openssl/ssl.h>
62 #include <openssl/err.h>
63
64
65 #define CLIENT_VERSION_LEN 2
66 #define SESSION_ID_LEN_LEN 1
67 #define CIPHERS_LEN_LEN 2
68 #define COMPRESSION_LEN_LEN 1
69 #define EXTENSIONS_LEN_LEN 2
70 #define EXTENSION_TYPE_LEN 2
71 #define EXTENSION_SIZE_LEN 2
72
73
74 #define TOTAL_NUM_TESTS 2
75
76 /*
77 * Test that explicitly setting ticket data results in it appearing in the
78 * ClientHello for TLS1.2
79 */
80 #define TEST_SET_SESSION_TICK_DATA_TLS_1_2 0
81
82 /*
83 * Test that explicitly setting ticket data results in it appearing in the
84 * ClientHello for a negotiated SSL/TLS version
85 */
86 #define TEST_SET_SESSION_TICK_DATA_VER_NEG 1
87
88 int main(int argc, char *argv[])
89 {
90 SSL_CTX *ctx;
91 SSL *con;
92 BIO *rbio;
93 BIO *wbio;
94 BIO *err;
95 long len;
96 unsigned char *data;
97 unsigned char *dataend;
98 char *dummytick = "Hello World!";
99 unsigned int tmplen;
100 unsigned int type;
101 unsigned int size;
102 int testresult = 0;
103 int currtest = 0;
104
105 SSL_library_init();
106 SSL_load_error_strings();
107
108 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
109
110 CRYPTO_set_mem_debug(1);
111 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
112
113 /*
114 * For each test set up an SSL_CTX and SSL and see what ClientHello gets
115 * produced when we try to connect
116 */
117 for (; currtest < TOTAL_NUM_TESTS; currtest++) {
118 testresult = 0;
119 if (currtest == TEST_SET_SESSION_TICK_DATA_TLS_1_2) {
120 ctx = SSL_CTX_new(TLSv1_2_method());
121 } else {
122 ctx = SSL_CTX_new(TLS_method());
123 }
124 con = SSL_new(ctx);
125
126 rbio = BIO_new(BIO_s_mem());
127 wbio = BIO_new(BIO_s_mem());
128 SSL_set_bio(con, rbio, wbio);
129 SSL_set_connect_state(con);
130
131 if (currtest == TEST_SET_SESSION_TICK_DATA_TLS_1_2
132 || currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
133 if (!SSL_set_session_ticket_ext(con, dummytick, strlen(dummytick)))
134 goto end;
135 }
136
137 if (SSL_connect(con) > 0) {
138 /* This shouldn't succeed because we don't have a server! */
139 goto end;
140 }
141
142 len = BIO_get_mem_data(wbio, (char **)&data);
143 dataend = data + len;
144
145 /* Skip the record header */
146 data += SSL3_RT_HEADER_LENGTH;
147 /* Skip the handshake message header */
148 data += SSL3_HM_HEADER_LENGTH;
149 /* Skip client version and random */
150 data += CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE;
151 if (data + SESSION_ID_LEN_LEN > dataend)
152 goto end;
153 /* Skip session id */
154 tmplen = *data;
155 data += SESSION_ID_LEN_LEN + tmplen;
156 if (data + CIPHERS_LEN_LEN > dataend)
157 goto end;
158 /* Skip ciphers */
159 tmplen = ((*data) << 8) | *(data + 1);
160 data += CIPHERS_LEN_LEN + tmplen;
161 if (data + COMPRESSION_LEN_LEN > dataend)
162 goto end;
163 /* Skip compression */
164 tmplen = *data;
165 data += COMPRESSION_LEN_LEN + tmplen;
166 if (data + EXTENSIONS_LEN_LEN > dataend)
167 goto end;
168 /* Extensions len */
169 tmplen = ((*data) << 8) | *(data + 1);
170 data += EXTENSIONS_LEN_LEN;
171 if (data + tmplen > dataend)
172 goto end;
173
174 /* Loop through all extensions */
175 while (tmplen > EXTENSION_TYPE_LEN + EXTENSION_SIZE_LEN) {
176 type = ((*data) << 8) | *(data + 1);
177 data += EXTENSION_TYPE_LEN;
178 size = ((*data) << 8) | *(data + 1);
179 data += EXTENSION_SIZE_LEN;
180 if (data + size > dataend)
181 goto end;
182
183 if (type == TLSEXT_TYPE_session_ticket) {
184 if (currtest == TEST_SET_SESSION_TICK_DATA_TLS_1_2
185 || currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
186 if (size == strlen(dummytick)
187 && memcmp(data, dummytick, size) == 0) {
188 /* Ticket data is as we expected */
189 testresult = 1;
190 } else {
191 printf("Received session ticket is not as expected\n");
192 }
193 break;
194 }
195 }
196
197 tmplen -= EXTENSION_TYPE_LEN + EXTENSION_SIZE_LEN + size;
198 data += size;
199 }
200
201 end:
202 SSL_free(con);
203 SSL_CTX_free(ctx);
204 if (!testresult) {
205 printf("ClientHello test: FAILED (Test %d)\n", currtest);
206 break;
207 }
208 }
209
210 ERR_free_strings();
211 ERR_remove_thread_state(NULL);
212 EVP_cleanup();
213 CRYPTO_cleanup_all_ex_data();
214 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
215 CRYPTO_mem_leaks(err);
216 #endif
217 BIO_free(err);
218
219 return testresult?0:1;
220 }