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