xref: /openbsd-src/regress/lib/libssl/client/clienttest.c (revision 12237f1a9623274e68888da2318049b0e5a6ee90)
1 /*
2  * Copyright (c) 2015 Joel Sing <jsing@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <openssl/ssl.h>
18 
19 #include <openssl/dtls1.h>
20 #include <openssl/ssl3.h>
21 
22 #include <err.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #define DTLS_RANDOM_OFFSET (DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH + 2)
27 #define SSL3_RANDOM_OFFSET (SSL3_RT_HEADER_LENGTH + SSL3_HM_HEADER_LENGTH + 2)
28 
29 static unsigned char client_hello_dtls1[] = {
30 	0x16, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
31 	0x00, 0x00, 0x00, 0x00, 0x7e, 0x01, 0x00, 0x00,
32 	0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33 	0x72, 0xfe, 0xff, 0xc3, 0xd6, 0x19, 0xf8, 0x5d,
34 	0x6a, 0xe3, 0x6d, 0x16, 0x4a, 0xf7, 0x8f, 0x8e,
35 	0x4a, 0x12, 0x87, 0xcf, 0x07, 0x99, 0xa7, 0x92,
36 	0x40, 0xbd, 0x06, 0x9f, 0xe9, 0xd2, 0x68, 0x84,
37 	0xff, 0x6f, 0xe8, 0x00, 0x00, 0x00, 0x44, 0xc0,
38 	0x14, 0xc0, 0x0a, 0x00, 0x39, 0x00, 0x38, 0xff,
39 	0x85, 0x00, 0x88, 0x00, 0x87, 0x00, 0x81, 0xc0,
40 	0x0f, 0xc0, 0x05, 0x00, 0x35, 0x00, 0x84, 0xc0,
41 	0x13, 0xc0, 0x09, 0x00, 0x33, 0x00, 0x32, 0x00,
42 	0x45, 0x00, 0x44, 0xc0, 0x0e, 0xc0, 0x04, 0x00,
43 	0x2f, 0x00, 0x41, 0x00, 0x07, 0xc0, 0x12, 0xc0,
44 	0x08, 0x00, 0x16, 0x00, 0x13, 0xc0, 0x0d, 0xc0,
45 	0x03, 0x00, 0x0a, 0x00, 0x15, 0x00, 0x12, 0x00,
46 	0x09, 0x00, 0xff, 0x01, 0x00, 0x00, 0x04, 0x00,
47 	0x23, 0x00, 0x00,
48 };
49 
50 static unsigned char client_hello_tls10[] = {
51 	0x16, 0x03, 0x01, 0x00, 0xc7, 0x01, 0x00, 0x00,
52 	0xc3, 0x03, 0x01, 0x06, 0x6a, 0x3f, 0x0f, 0xf5,
53 	0x19, 0x64, 0x2d, 0xfd, 0xb1, 0x4a, 0x91, 0xcd,
54 	0x65, 0x37, 0xf8, 0x51, 0x92, 0xf9, 0xbf, 0xe9,
55 	0x46, 0x41, 0x2e, 0x0a, 0x4d, 0xb1, 0xa8, 0x0c,
56 	0x88, 0xec, 0x03, 0x00, 0x00, 0x50, 0xc0, 0x14,
57 	0xc0, 0x0a, 0x00, 0x39, 0x00, 0x38, 0xff, 0x85,
58 	0x00, 0x88, 0x00, 0x87, 0x00, 0x81, 0xc0, 0x0f,
59 	0xc0, 0x05, 0x00, 0x35, 0x00, 0x84, 0xc0, 0x13,
60 	0xc0, 0x09, 0x00, 0x33, 0x00, 0x32, 0x00, 0x45,
61 	0x00, 0x44, 0xc0, 0x0e, 0xc0, 0x04, 0x00, 0x2f,
62 	0x00, 0x41, 0x00, 0x07, 0xc0, 0x11, 0xc0, 0x07,
63 	0xc0, 0x0c, 0xc0, 0x02, 0x00, 0x05, 0x00, 0x04,
64 	0xc0, 0x12, 0xc0, 0x08, 0x00, 0x16, 0x00, 0x13,
65 	0xc0, 0x0d, 0xc0, 0x03, 0x00, 0x0a, 0x00, 0x15,
66 	0x00, 0x12, 0x00, 0x09, 0x00, 0xff, 0x01, 0x00,
67 	0x00, 0x4a, 0x00, 0x0b, 0x00, 0x04, 0x03, 0x00,
68 	0x01, 0x02, 0x00, 0x0a, 0x00, 0x3a, 0x00, 0x38,
69 	0x00, 0x0e, 0x00, 0x0d, 0x00, 0x19, 0x00, 0x1c,
70 	0x00, 0x0b, 0x00, 0x0c, 0x00, 0x1b, 0x00, 0x18,
71 	0x00, 0x09, 0x00, 0x0a, 0x00, 0x1a, 0x00, 0x16,
72 	0x00, 0x17, 0x00, 0x08, 0x00, 0x06, 0x00, 0x07,
73 	0x00, 0x14, 0x00, 0x15, 0x00, 0x04, 0x00, 0x05,
74 	0x00, 0x12, 0x00, 0x13, 0x00, 0x01, 0x00, 0x02,
75 	0x00, 0x03, 0x00, 0x0f, 0x00, 0x10, 0x00, 0x11,
76 	0x00, 0x23, 0x00, 0x00,
77 };
78 
79 static unsigned char client_hello_tls11[] = {
80 	0x16, 0x03, 0x01, 0x00, 0xc7, 0x01, 0x00, 0x00,
81 	0xc3, 0x03, 0x02, 0x2f, 0x93, 0x9c, 0x37, 0x16,
82 	0x88, 0x53, 0xa1, 0xba, 0xb2, 0x36, 0xc9, 0xdf,
83 	0xa4, 0x5f, 0x80, 0x6a, 0x8b, 0xfe, 0x00, 0x52,
84 	0xd3, 0xd2, 0x68, 0x2a, 0xae, 0xca, 0x72, 0xae,
85 	0x70, 0x77, 0x84, 0x00, 0x00, 0x50, 0xc0, 0x14,
86 	0xc0, 0x0a, 0x00, 0x39, 0x00, 0x38, 0xff, 0x85,
87 	0x00, 0x88, 0x00, 0x87, 0x00, 0x81, 0xc0, 0x0f,
88 	0xc0, 0x05, 0x00, 0x35, 0x00, 0x84, 0xc0, 0x13,
89 	0xc0, 0x09, 0x00, 0x33, 0x00, 0x32, 0x00, 0x45,
90 	0x00, 0x44, 0xc0, 0x0e, 0xc0, 0x04, 0x00, 0x2f,
91 	0x00, 0x41, 0x00, 0x07, 0xc0, 0x11, 0xc0, 0x07,
92 	0xc0, 0x0c, 0xc0, 0x02, 0x00, 0x05, 0x00, 0x04,
93 	0xc0, 0x12, 0xc0, 0x08, 0x00, 0x16, 0x00, 0x13,
94 	0xc0, 0x0d, 0xc0, 0x03, 0x00, 0x0a, 0x00, 0x15,
95 	0x00, 0x12, 0x00, 0x09, 0x00, 0xff, 0x01, 0x00,
96 	0x00, 0x4a, 0x00, 0x0b, 0x00, 0x04, 0x03, 0x00,
97 	0x01, 0x02, 0x00, 0x0a, 0x00, 0x3a, 0x00, 0x38,
98 	0x00, 0x0e, 0x00, 0x0d, 0x00, 0x19, 0x00, 0x1c,
99 	0x00, 0x0b, 0x00, 0x0c, 0x00, 0x1b, 0x00, 0x18,
100 	0x00, 0x09, 0x00, 0x0a, 0x00, 0x1a, 0x00, 0x16,
101 	0x00, 0x17, 0x00, 0x08, 0x00, 0x06, 0x00, 0x07,
102 	0x00, 0x14, 0x00, 0x15, 0x00, 0x04, 0x00, 0x05,
103 	0x00, 0x12, 0x00, 0x13, 0x00, 0x01, 0x00, 0x02,
104 	0x00, 0x03, 0x00, 0x0f, 0x00, 0x10, 0x00, 0x11,
105 	0x00, 0x23, 0x00, 0x00,
106 };
107 
108 static unsigned char client_hello_tls12[] = {
109 	0x16, 0x03, 0x01, 0x01, 0x3b, 0x01, 0x00, 0x01,
110 	0x37, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
111 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
113 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114 	0x00, 0x00, 0x00, 0x00, 0x00, 0x9a, 0xcc, 0x14,
115 	0xcc, 0x13, 0xcc, 0x15, 0xc0, 0x30, 0xc0, 0x2c,
116 	0xc0, 0x28, 0xc0, 0x24, 0xc0, 0x14, 0xc0, 0x0a,
117 	0x00, 0xa3, 0x00, 0x9f, 0x00, 0x6b, 0x00, 0x6a,
118 	0x00, 0x39, 0x00, 0x38, 0xff, 0x85, 0x00, 0xc4,
119 	0x00, 0xc3, 0x00, 0x88, 0x00, 0x87, 0x00, 0x81,
120 	0xc0, 0x32, 0xc0, 0x2e, 0xc0, 0x2a, 0xc0, 0x26,
121 	0xc0, 0x0f, 0xc0, 0x05, 0x00, 0x9d, 0x00, 0x3d,
122 	0x00, 0x35, 0x00, 0xc0, 0x00, 0x84, 0xc0, 0x2f,
123 	0xc0, 0x2b, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x13,
124 	0xc0, 0x09, 0x00, 0xa2, 0x00, 0x9e, 0x00, 0x67,
125 	0x00, 0x40, 0x00, 0x33, 0x00, 0x32, 0x00, 0xbe,
126 	0x00, 0xbd, 0x00, 0x45, 0x00, 0x44, 0xc0, 0x31,
127 	0xc0, 0x2d, 0xc0, 0x29, 0xc0, 0x25, 0xc0, 0x0e,
128 	0xc0, 0x04, 0x00, 0x9c, 0x00, 0x3c, 0x00, 0x2f,
129 	0x00, 0xba, 0x00, 0x41, 0x00, 0x07, 0xc0, 0x11,
130 	0xc0, 0x07, 0xc0, 0x0c, 0xc0, 0x02, 0x00, 0x05,
131 	0x00, 0x04, 0xc0, 0x12, 0xc0, 0x08, 0x00, 0x16,
132 	0x00, 0x13, 0xc0, 0x0d, 0xc0, 0x03, 0x00, 0x0a,
133 	0x00, 0x15, 0x00, 0x12, 0x00, 0x09, 0x00, 0xff,
134 	0x01, 0x00, 0x00, 0x74, 0x00, 0x0b, 0x00, 0x04,
135 	0x03, 0x00, 0x01, 0x02, 0x00, 0x0a, 0x00, 0x3a,
136 	0x00, 0x38, 0x00, 0x0e, 0x00, 0x0d, 0x00, 0x19,
137 	0x00, 0x1c, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x1b,
138 	0x00, 0x18, 0x00, 0x09, 0x00, 0x0a, 0x00, 0x1a,
139 	0x00, 0x16, 0x00, 0x17, 0x00, 0x08, 0x00, 0x06,
140 	0x00, 0x07, 0x00, 0x14, 0x00, 0x15, 0x00, 0x04,
141 	0x00, 0x05, 0x00, 0x12, 0x00, 0x13, 0x00, 0x01,
142 	0x00, 0x02, 0x00, 0x03, 0x00, 0x0f, 0x00, 0x10,
143 	0x00, 0x11, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0d,
144 	0x00, 0x26, 0x00, 0x24, 0x06, 0x01, 0x06, 0x02,
145 	0x06, 0x03, 0xef, 0xef, 0x05, 0x01, 0x05, 0x02,
146 	0x05, 0x03, 0x04, 0x01, 0x04, 0x02, 0x04, 0x03,
147 	0xee, 0xee, 0xed, 0xed, 0x03, 0x01, 0x03, 0x02,
148 	0x03, 0x03, 0x02, 0x01, 0x02, 0x02, 0x02, 0x03,
149 };
150 
151 struct client_hello_test {
152 	const unsigned char *desc;
153 	const unsigned char *client_hello;
154 	const size_t client_hello_len;
155 	const size_t random_start;
156 	const SSL_METHOD *(*ssl_method)(void);
157 	const long ssl_options;
158 };
159 
160 static struct client_hello_test client_hello_tests[] = {
161 	{
162 		.desc = "DTLSv1 client",
163 		.client_hello = client_hello_dtls1,
164 		.client_hello_len = sizeof(client_hello_dtls1),
165 		.random_start = DTLS_RANDOM_OFFSET,
166 		.ssl_method = DTLSv1_client_method,
167 	},
168 	{
169 		.desc = "TLSv1 client",
170 		.client_hello = client_hello_tls10,
171 		.client_hello_len = sizeof(client_hello_tls10),
172 		.random_start = SSL3_RANDOM_OFFSET,
173 		.ssl_method = TLSv1_client_method,
174 	},
175 	{
176 		.desc = "TLSv1_1 client",
177 		.client_hello = client_hello_tls11,
178 		.client_hello_len = sizeof(client_hello_tls11),
179 		.random_start = SSL3_RANDOM_OFFSET,
180 		.ssl_method = TLSv1_1_client_method,
181 	},
182 	{
183 		.desc = "TLSv1_2 client",
184 		.client_hello = client_hello_tls12,
185 		.client_hello_len = sizeof(client_hello_tls12),
186 		.random_start = SSL3_RANDOM_OFFSET,
187 		.ssl_method = TLSv1_2_client_method,
188 	},
189 	{
190 		.desc = "SSLv23 default",
191 		.client_hello = client_hello_tls12,
192 		.client_hello_len = sizeof(client_hello_tls12),
193 		.random_start = SSL3_RANDOM_OFFSET,
194 		.ssl_method = SSLv23_client_method,
195 		.ssl_options = 0,
196 	},
197 	{
198 		.desc = "SSLv23 (no TLSv1.2)",
199 		.client_hello = client_hello_tls11,
200 		.client_hello_len = sizeof(client_hello_tls11),
201 		.random_start = SSL3_RANDOM_OFFSET,
202 		.ssl_method = SSLv23_client_method,
203 		.ssl_options = SSL_OP_NO_TLSv1_2,
204 	},
205 	{
206 		.desc = "SSLv23 (no TLSv1.1)",
207 		.client_hello = client_hello_tls10,
208 		.client_hello_len = sizeof(client_hello_tls10),
209 		.random_start = SSL3_RANDOM_OFFSET,
210 		.ssl_method = SSLv23_client_method,
211 		.ssl_options = SSL_OP_NO_TLSv1_1,
212 	},
213 	{
214 		.desc = "TLS default",
215 		.client_hello = client_hello_tls12,
216 		.client_hello_len = sizeof(client_hello_tls12),
217 		.random_start = SSL3_RANDOM_OFFSET,
218 		.ssl_method = TLS_client_method,
219 		.ssl_options = 0,
220 	},
221 	{
222 		.desc = "TLS (no TLSv1.2)",
223 		.client_hello = client_hello_tls11,
224 		.client_hello_len = sizeof(client_hello_tls11),
225 		.random_start = SSL3_RANDOM_OFFSET,
226 		.ssl_method = TLS_client_method,
227 		.ssl_options = SSL_OP_NO_TLSv1_2,
228 	},
229 	{
230 		.desc = "TLS (no TLSv1.1)",
231 		.client_hello = client_hello_tls10,
232 		.client_hello_len = sizeof(client_hello_tls10),
233 		.random_start = SSL3_RANDOM_OFFSET,
234 		.ssl_method = TLS_client_method,
235 		.ssl_options = SSL_OP_NO_TLSv1_1,
236 	},
237 	{
238 		.desc = "TLS (no TLSv1.0, no TLSv1.1)",
239 		.client_hello = client_hello_tls12,
240 		.client_hello_len = sizeof(client_hello_tls12),
241 		.random_start = SSL3_RANDOM_OFFSET,
242 		.ssl_method = TLS_client_method,
243 		.ssl_options = SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1,
244 	},
245 };
246 
247 #define N_CLIENT_HELLO_TESTS \
248     (sizeof(client_hello_tests) / sizeof(*client_hello_tests))
249 
250 static void
251 hexdump(const unsigned char *buf, size_t len)
252 {
253 	size_t i;
254 
255 	for (i = 1; i <= len; i++)
256 		fprintf(stderr, " 0x%02hhx,%s", buf[i - 1], i % 8 ? "" : "\n");
257 
258 	fprintf(stderr, "\n");
259 }
260 
261 static int
262 client_hello_test(int testno, struct client_hello_test *cht)
263 {
264 	BIO *rbio = NULL, *wbio = NULL;
265 	SSL_CTX *ssl_ctx = NULL;
266 	SSL *ssl = NULL;
267 	char *wbuf, rbuf[1];
268 	int ret = 1;
269 	size_t i;
270 	long len;
271 
272 	fprintf(stdout, "Test %i - %s\n", testno, cht->desc);
273 
274 	/* Providing a small buf causes *_get_server_hello() to return. */
275 	if ((rbio = BIO_new_mem_buf(rbuf, sizeof(rbuf))) == NULL) {
276 		fprintf(stderr, "Failed to setup rbio\n");
277 		goto failure;
278 	}
279 	if ((wbio = BIO_new(BIO_s_mem())) == NULL) {
280 		fprintf(stderr, "Failed to setup wbio\n");
281 		goto failure;
282 	}
283 
284 	if ((ssl_ctx = SSL_CTX_new(cht->ssl_method())) == NULL) {
285 		fprintf(stderr, "SSL_CTX_new() returned NULL\n");
286 		goto failure;
287 	}
288 
289 	SSL_CTX_set_options(ssl_ctx, cht->ssl_options);
290 
291 	if ((ssl = SSL_new(ssl_ctx)) == NULL) {
292 		fprintf(stderr, "SSL_new() returned NULL\n");
293 		goto failure;
294 	}
295 
296 	rbio->references = 2;
297 	wbio->references = 2;
298 
299 	SSL_set_bio(ssl, rbio, wbio);
300 
301 	if (SSL_connect(ssl) != 0) {
302 		fprintf(stderr, "SSL_connect() returned non-zero\n");
303 		goto failure;
304 	}
305 
306 	len = BIO_get_mem_data(wbio, &wbuf);
307 
308 	if ((size_t)len != cht->client_hello_len) {
309 		fprintf(stderr, "FAIL: test returned ClientHello length %li, "
310 		    "want %zu\n", len, cht->client_hello_len);
311 		fprintf(stderr, "received:\n");
312 		hexdump(wbuf, len);
313 		goto failure;
314 	}
315 
316 	/* Skip over the client random, since we expect that to differ. */
317 	i = cht->random_start + SSL3_RANDOM_SIZE;
318 	if (memcmp(cht->client_hello, wbuf, cht->random_start) != 0 ||
319 	    memcmp(&cht->client_hello[cht->random_start],
320 		&wbuf[cht->random_start], SSL3_RANDOM_SIZE) == 0 ||
321 	    memcmp(&cht->client_hello[i], &wbuf[i], len - i) != 0) {
322 		fprintf(stderr, "FAIL: ClientHello differs:\n");
323 		fprintf(stderr, "received:\n");
324 		hexdump(wbuf, len);
325 		fprintf(stderr, "test data:\n");
326 		hexdump(cht->client_hello, cht->client_hello_len);
327 		fprintf(stderr, "\n");
328 		goto failure;
329 	}
330 
331 	ret = 0;
332 
333 failure:
334 	SSL_CTX_free(ssl_ctx);
335 	SSL_free(ssl);
336 
337 	rbio->references = 1;
338 	wbio->references = 1;
339 
340 	BIO_free(rbio);
341 	BIO_free(wbio);
342 
343 	return (ret);
344 }
345 
346 int
347 main(int argc, char **argv)
348 {
349 	int failed = 0;
350 	size_t i;
351 
352 	SSL_library_init();
353 
354 	for (i = 0; i < N_CLIENT_HELLO_TESTS; i++)
355 		failed |= client_hello_test(i, &client_hello_tests[i]);
356 
357 	return (failed);
358 }
359