regress_ssl.c revision 1.4 1 /* $NetBSD: regress_ssl.c,v 1.4 2015/07/10 14:20:35 christos Exp $ */
2
3 /*
4 * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 // Get rid of OSX 10.7 and greater deprecation warnings.
30 #if defined(__APPLE__) && defined(__clang__)
31 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
32 #endif
33
34 #ifdef _WIN32
35 #include <winsock2.h>
36 #include <windows.h>
37 #endif
38
39 #ifndef _WIN32
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #endif
44
45 #include "event2/util.h"
46 #include "event2/event.h"
47 #include "event2/bufferevent_ssl.h"
48 #include "event2/buffer.h"
49 #include "event2/listener.h"
50
51 #include "regress.h"
52 #include "tinytest.h"
53 #include "tinytest_macros.h"
54
55 #include <openssl/ssl.h>
56 #include <openssl/bio.h>
57 #include <openssl/err.h>
58 #include <openssl/pem.h>
59
60 #include <string.h>
61
62 /* A short pre-generated key, to save the cost of doing an RSA key generation
63 * step during the unit tests. It's only 512 bits long, and it is published
64 * in this file, so you would have to be very foolish to consider using it in
65 * your own code. */
66 static const char KEY[] =
67 "-----BEGIN RSA PRIVATE KEY-----\n"
68 "MIIBOgIBAAJBAKibTEzXjj+sqpipePX1lEk5BNFuL/dDBbw8QCXgaJWikOiKHeJq\n"
69 "3FQ0OmCnmpkdsPFE4x3ojYmmdgE2i0dJwq0CAwEAAQJAZ08gpUS+qE1IClps/2gG\n"
70 "AAer6Bc31K2AaiIQvCSQcH440cp062QtWMC3V5sEoWmdLsbAHFH26/9ZHn5zAflp\n"
71 "gQIhANWOx/UYeR8HD0WREU5kcuSzgzNLwUErHLzxP7U6aojpAiEAyh2H35CjN/P7\n"
72 "NhcZ4QYw3PeUWpqgJnaE/4i80BSYkSUCIQDLHFhLYLJZ80HwHTADif/ISn9/Ow6b\n"
73 "p6BWh3DbMar/eQIgBPS6azH5vpp983KXkNv9AL4VZi9ac/b+BeINdzC6GP0CIDmB\n"
74 "U6GFEQTZ3IfuiVabG5pummdC4DNbcdI+WKrSFNmQ\n"
75 "-----END RSA PRIVATE KEY-----\n";
76
77 static EVP_PKEY *
78 getkey(void)
79 {
80 EVP_PKEY *key;
81 BIO *bio;
82
83 /* new read-only BIO backed by KEY. */
84 bio = BIO_new_mem_buf((char*)KEY, -1);
85 tt_assert(bio);
86
87 key = PEM_read_bio_PrivateKey(bio,NULL,NULL,NULL);
88 BIO_free(bio);
89 tt_assert(key);
90
91 return key;
92 end:
93 return NULL;
94 }
95
96 static X509 *
97 getcert(void)
98 {
99 /* Dummy code to make a quick-and-dirty valid certificate with
100 OpenSSL. Don't copy this code into your own program! It does a
101 number of things in a stupid and insecure way. */
102 X509 *x509 = NULL;
103 X509_NAME *name = NULL;
104 EVP_PKEY *key = getkey();
105 int nid;
106 time_t now = time(NULL);
107
108 tt_assert(key);
109
110 x509 = X509_new();
111 tt_assert(x509);
112 tt_assert(0 != X509_set_version(x509, 2));
113 tt_assert(0 != ASN1_INTEGER_set(X509_get_serialNumber(x509),
114 (long)now));
115
116 name = X509_NAME_new();
117 tt_assert(name);
118 nid = OBJ_txt2nid("commonName");
119 tt_assert(NID_undef != nid);
120 tt_assert(0 != X509_NAME_add_entry_by_NID(
121 name, nid, MBSTRING_ASC, (unsigned char*)"example.com",
122 -1, -1, 0));
123
124 X509_set_subject_name(x509, name);
125 X509_set_issuer_name(x509, name);
126
127 X509_time_adj(X509_get_notBefore(x509), 0, &now);
128 now += 3600;
129 X509_time_adj(X509_get_notAfter(x509), 0, &now);
130 X509_set_pubkey(x509, key);
131 tt_assert(0 != X509_sign(x509, key, EVP_sha1()));
132
133 return x509;
134 end:
135 X509_free(x509);
136 return NULL;
137 }
138
139 static int disable_tls_11_and_12 = 0;
140 static SSL_CTX *the_ssl_ctx = NULL;
141
142 static SSL_CTX *
143 get_ssl_ctx(void)
144 {
145 if (the_ssl_ctx)
146 return the_ssl_ctx;
147 the_ssl_ctx = SSL_CTX_new(SSLv23_method());
148 if (!the_ssl_ctx)
149 return NULL;
150 if (disable_tls_11_and_12) {
151 #ifdef SSL_OP_NO_TLSv1_2
152 SSL_CTX_set_options(the_ssl_ctx, SSL_OP_NO_TLSv1_2);
153 #endif
154 #ifdef SSL_OP_NO_TLSv1_1
155 SSL_CTX_set_options(the_ssl_ctx, SSL_OP_NO_TLSv1_1);
156 #endif
157 }
158 return the_ssl_ctx;
159 }
160
161 static void
162 init_ssl(void)
163 {
164 SSL_library_init();
165 ERR_load_crypto_strings();
166 SSL_load_error_strings();
167 OpenSSL_add_all_algorithms();
168 if (SSLeay() != OPENSSL_VERSION_NUMBER) {
169 TT_DECLARE("WARN", ("Version mismatch for openssl: compiled with %lx but running with %lx", (unsigned long)OPENSSL_VERSION_NUMBER, (unsigned long) SSLeay()));
170 }
171 }
172
173 /* ====================
174 Here's a simple test: we read a number from the input, increment it, and
175 reply, until we get to 1001.
176 */
177
178 static int test_is_done = 0;
179 static int n_connected = 0;
180 static int got_close = 0;
181 static int got_error = 0;
182 static int renegotiate_at = -1;
183 static int stop_when_connected = 0;
184 static int pending_connect_events = 0;
185 static struct event_base *exit_base = NULL;
186
187 static void
188 respond_to_number(struct bufferevent *bev, void *ctx)
189 {
190 struct evbuffer *b = bufferevent_get_input(bev);
191 char *line;
192 int n;
193 line = evbuffer_readln(b, NULL, EVBUFFER_EOL_LF);
194 if (! line)
195 return;
196 n = atoi(line);
197 if (n <= 0)
198 TT_FAIL(("Bad number: %s", line));
199 free(line);
200 TT_BLATHER(("The number was %d", n));
201 if (n == 1001) {
202 ++test_is_done;
203 bufferevent_free(bev); /* Should trigger close on other side. */
204 return;
205 }
206 if (!strcmp(ctx, "client") && n == renegotiate_at) {
207 SSL_renegotiate(bufferevent_openssl_get_ssl(bev));
208 }
209 ++n;
210 evbuffer_add_printf(bufferevent_get_output(bev),
211 "%d\n", n);
212 TT_BLATHER(("Done reading; now writing."));
213 bufferevent_enable(bev, EV_WRITE);
214 bufferevent_disable(bev, EV_READ);
215 }
216
217 static void
218 done_writing_cb(struct bufferevent *bev, void *ctx)
219 {
220 struct evbuffer *b = bufferevent_get_output(bev);
221 if (evbuffer_get_length(b))
222 return;
223 TT_BLATHER(("Done writing."));
224 bufferevent_disable(bev, EV_WRITE);
225 bufferevent_enable(bev, EV_READ);
226 }
227
228 static void
229 eventcb(struct bufferevent *bev, short what, void *ctx)
230 {
231 TT_BLATHER(("Got event %d", (int)what));
232 if (what & BEV_EVENT_CONNECTED) {
233 SSL *ssl;
234 X509 *peer_cert;
235 ++n_connected;
236 ssl = bufferevent_openssl_get_ssl(bev);
237 tt_assert(ssl);
238 peer_cert = SSL_get_peer_certificate(ssl);
239 if (0==strcmp(ctx, "server")) {
240 tt_assert(peer_cert == NULL);
241 } else {
242 tt_assert(peer_cert != NULL);
243 }
244 if (stop_when_connected) {
245 if (--pending_connect_events == 0)
246 event_base_loopexit(exit_base, NULL);
247 }
248 } else if (what & BEV_EVENT_EOF) {
249 TT_BLATHER(("Got a good EOF"));
250 ++got_close;
251 bufferevent_free(bev);
252 } else if (what & BEV_EVENT_ERROR) {
253 TT_BLATHER(("Got an error."));
254 ++got_error;
255 bufferevent_free(bev);
256 }
257 end:
258 ;
259 }
260
261 static void
262 open_ssl_bufevs(struct bufferevent **bev1_out, struct bufferevent **bev2_out,
263 struct event_base *base, int is_open, int flags, SSL *ssl1, SSL *ssl2,
264 evutil_socket_t *fd_pair, struct bufferevent **underlying_pair)
265 {
266 int state1 = is_open ? BUFFEREVENT_SSL_OPEN :BUFFEREVENT_SSL_CONNECTING;
267 int state2 = is_open ? BUFFEREVENT_SSL_OPEN :BUFFEREVENT_SSL_ACCEPTING;
268 if (fd_pair) {
269 *bev1_out = bufferevent_openssl_socket_new(
270 base, fd_pair[0], ssl1, state1, flags);
271 *bev2_out = bufferevent_openssl_socket_new(
272 base, fd_pair[1], ssl2, state2, flags);
273 } else {
274 *bev1_out = bufferevent_openssl_filter_new(
275 base, underlying_pair[0], ssl1, state1, flags);
276 *bev2_out = bufferevent_openssl_filter_new(
277 base, underlying_pair[1], ssl2, state2, flags);
278
279 }
280 bufferevent_setcb(*bev1_out, respond_to_number, done_writing_cb,
281 eventcb, (void*)"client");
282 bufferevent_setcb(*bev2_out, respond_to_number, done_writing_cb,
283 eventcb, (void*)"server");
284 }
285
286 static void
287 regress_bufferevent_openssl(void *arg)
288 {
289 struct basic_test_data *data = arg;
290
291 struct bufferevent *bev1, *bev2;
292 SSL *ssl1, *ssl2;
293 X509 *cert = getcert();
294 EVP_PKEY *key = getkey();
295 const int start_open = strstr((char*)data->setup_data, "open")!=NULL;
296 const int filter = strstr((char*)data->setup_data, "filter")!=NULL;
297 int flags = BEV_OPT_DEFER_CALLBACKS;
298 struct bufferevent *bev_ll[2] = { NULL, NULL };
299 evutil_socket_t *fd_pair = NULL;
300
301 tt_assert(cert);
302 tt_assert(key);
303
304 init_ssl();
305
306 if (strstr((char*)data->setup_data, "renegotiate")) {
307 if (SSLeay() >= 0x10001000 &&
308 SSLeay() < 0x1000104f) {
309 /* 1.0.1 up to 1.0.1c has a bug where TLS1.1 and 1.2
310 * can't renegotiate with themselves. Disable. */
311 disable_tls_11_and_12 = 1;
312 }
313 renegotiate_at = 600;
314 }
315
316 ssl1 = SSL_new(get_ssl_ctx());
317 ssl2 = SSL_new(get_ssl_ctx());
318
319 SSL_use_certificate(ssl2, cert);
320 SSL_use_PrivateKey(ssl2, key);
321
322 if (! start_open)
323 flags |= BEV_OPT_CLOSE_ON_FREE;
324
325 if (!filter) {
326 tt_assert(strstr((char*)data->setup_data, "socketpair"));
327 fd_pair = data->pair;
328 } else {
329 bev_ll[0] = bufferevent_socket_new(data->base, data->pair[0],
330 BEV_OPT_CLOSE_ON_FREE);
331 bev_ll[1] = bufferevent_socket_new(data->base, data->pair[1],
332 BEV_OPT_CLOSE_ON_FREE);
333 }
334
335 open_ssl_bufevs(&bev1, &bev2, data->base, 0, flags, ssl1, ssl2,
336 fd_pair, bev_ll);
337
338 if (!filter) {
339 tt_int_op(bufferevent_getfd(bev1), ==, data->pair[0]);
340 } else {
341 tt_ptr_op(bufferevent_get_underlying(bev1), ==, bev_ll[0]);
342 }
343
344 if (start_open) {
345 pending_connect_events = 2;
346 stop_when_connected = 1;
347 exit_base = data->base;
348 event_base_dispatch(data->base);
349 /* Okay, now the renegotiation is done. Make new
350 * bufferevents to test opening in BUFFEREVENT_SSL_OPEN */
351 flags |= BEV_OPT_CLOSE_ON_FREE;
352 bufferevent_free(bev1);
353 bufferevent_free(bev2);
354 bev1 = bev2 = NULL;
355 open_ssl_bufevs(&bev1, &bev2, data->base, 1, flags, ssl1, ssl2,
356 fd_pair, bev_ll);
357 }
358
359 bufferevent_enable(bev1, EV_READ|EV_WRITE);
360 bufferevent_enable(bev2, EV_READ|EV_WRITE);
361
362 evbuffer_add_printf(bufferevent_get_output(bev1), "1\n");
363
364 event_base_dispatch(data->base);
365
366 tt_assert(test_is_done == 1);
367 tt_assert(n_connected == 2);
368
369 /* We don't handle shutdown properly yet.
370 tt_int_op(got_close, ==, 1);
371 tt_int_op(got_error, ==, 0);
372 */
373 end:
374 return;
375 }
376
377 static void
378 acceptcb(struct evconnlistener *listener, evutil_socket_t fd,
379 struct sockaddr *addr, int socklen, void *arg)
380 {
381 struct basic_test_data *data = arg;
382 struct bufferevent *bev;
383 SSL *ssl = SSL_new(get_ssl_ctx());
384
385 SSL_use_certificate(ssl, getcert());
386 SSL_use_PrivateKey(ssl, getkey());
387
388 bev = bufferevent_openssl_socket_new(
389 data->base,
390 fd,
391 ssl,
392 BUFFEREVENT_SSL_ACCEPTING,
393 BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
394
395 bufferevent_setcb(bev, respond_to_number, NULL, eventcb,
396 (void*)"server");
397
398 bufferevent_enable(bev, EV_READ|EV_WRITE);
399
400 /* Only accept once, then disable ourself. */
401 evconnlistener_disable(listener);
402 }
403
404 static void
405 regress_bufferevent_openssl_connect(void *arg)
406 {
407 struct basic_test_data *data = arg;
408
409 struct event_base *base = data->base;
410
411 struct evconnlistener *listener;
412 struct bufferevent *bev;
413 struct sockaddr_in sin;
414 struct sockaddr_storage ss;
415 ev_socklen_t slen;
416
417 init_ssl();
418
419 memset(&sin, 0, sizeof(sin));
420 sin.sin_family = AF_INET;
421 sin.sin_addr.s_addr = htonl(0x7f000001);
422
423 memset(&ss, 0, sizeof(ss));
424 slen = sizeof(ss);
425
426 listener = evconnlistener_new_bind(base, acceptcb, data,
427 LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
428 -1, (struct sockaddr *)&sin, sizeof(sin));
429
430 tt_assert(listener);
431 tt_assert(evconnlistener_get_fd(listener) >= 0);
432
433 bev = bufferevent_openssl_socket_new(
434 data->base, -1, SSL_new(get_ssl_ctx()),
435 BUFFEREVENT_SSL_CONNECTING,
436 BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
437 tt_assert(bev);
438
439 bufferevent_setcb(bev, respond_to_number, NULL, eventcb,
440 (void*)"client");
441
442 tt_assert(getsockname(evconnlistener_get_fd(listener),
443 (struct sockaddr*)&ss, &slen) == 0);
444 tt_assert(slen == sizeof(struct sockaddr_in));
445 tt_int_op(((struct sockaddr*)&ss)->sa_family, ==, AF_INET);
446 tt_int_op(((struct sockaddr*)&ss)->sa_family, ==, AF_INET);
447
448 tt_assert(0 ==
449 bufferevent_socket_connect(bev, (struct sockaddr*)&ss, slen));
450 evbuffer_add_printf(bufferevent_get_output(bev), "1\n");
451 bufferevent_enable(bev, EV_READ|EV_WRITE);
452
453 event_base_dispatch(base);
454 end:
455 ;
456 }
457
458 struct testcase_t ssl_testcases[] = {
459
460 { "bufferevent_socketpair", regress_bufferevent_openssl, TT_ISOLATED,
461 &basic_setup, (void*)"socketpair" },
462 { "bufferevent_filter", regress_bufferevent_openssl,
463 TT_ISOLATED,
464 &basic_setup, (void*)"filter" },
465 { "bufferevent_renegotiate_socketpair", regress_bufferevent_openssl,
466 TT_ISOLATED,
467 &basic_setup, (void*)"socketpair renegotiate" },
468 { "bufferevent_renegotiate_filter", regress_bufferevent_openssl,
469 TT_ISOLATED,
470 &basic_setup, (void*)"filter renegotiate" },
471 { "bufferevent_socketpair_startopen", regress_bufferevent_openssl,
472 TT_ISOLATED, &basic_setup, (void*)"socketpair open" },
473 { "bufferevent_filter_startopen", regress_bufferevent_openssl,
474 TT_ISOLATED, &basic_setup, (void*)"filter open" },
475
476 { "bufferevent_connect", regress_bufferevent_openssl_connect,
477 TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
478
479 END_OF_TESTCASES,
480 };
481