Home | History | Annotate | Line # | Download | only in libevent
bufferevent_openssl.c revision 1.1.1.1.8.2
      1  1.1.1.1.8.2  tls /*	$NetBSD: bufferevent_openssl.c,v 1.1.1.1.8.2 2014/08/19 23:51:45 tls Exp $	*/
      2  1.1.1.1.8.2  tls 
      3  1.1.1.1.8.2  tls /*
      4  1.1.1.1.8.2  tls  * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
      5  1.1.1.1.8.2  tls  *
      6  1.1.1.1.8.2  tls  * Redistribution and use in source and binary forms, with or without
      7  1.1.1.1.8.2  tls  * modification, are permitted provided that the following conditions
      8  1.1.1.1.8.2  tls  * are met:
      9  1.1.1.1.8.2  tls  * 1. Redistributions of source code must retain the above copyright
     10  1.1.1.1.8.2  tls  *    notice, this list of conditions and the following disclaimer.
     11  1.1.1.1.8.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1.1.1.8.2  tls  *    notice, this list of conditions and the following disclaimer in the
     13  1.1.1.1.8.2  tls  *    documentation and/or other materials provided with the distribution.
     14  1.1.1.1.8.2  tls  * 3. The name of the author may not be used to endorse or promote products
     15  1.1.1.1.8.2  tls  *    derived from this software without specific prior written permission.
     16  1.1.1.1.8.2  tls  *
     17  1.1.1.1.8.2  tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1.1.1.8.2  tls  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1.1.1.8.2  tls  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1.1.1.8.2  tls  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1.1.1.8.2  tls  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1.1.1.8.2  tls  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1.1.1.8.2  tls  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1.1.1.8.2  tls  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1.1.1.8.2  tls  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.1.1.1.8.2  tls  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1.1.1.8.2  tls  */
     28  1.1.1.1.8.2  tls 
     29  1.1.1.1.8.2  tls #include "event2/event-config.h"
     30  1.1.1.1.8.2  tls #include "evconfig-private.h"
     31  1.1.1.1.8.2  tls 
     32  1.1.1.1.8.2  tls #include <sys/types.h>
     33  1.1.1.1.8.2  tls 
     34  1.1.1.1.8.2  tls #ifdef EVENT__HAVE_SYS_TIME_H
     35  1.1.1.1.8.2  tls #include <sys/time.h>
     36  1.1.1.1.8.2  tls #endif
     37  1.1.1.1.8.2  tls 
     38  1.1.1.1.8.2  tls #include <errno.h>
     39  1.1.1.1.8.2  tls #include <stdio.h>
     40  1.1.1.1.8.2  tls #include <stdlib.h>
     41  1.1.1.1.8.2  tls #include <string.h>
     42  1.1.1.1.8.2  tls #ifdef EVENT__HAVE_STDARG_H
     43  1.1.1.1.8.2  tls #include <stdarg.h>
     44  1.1.1.1.8.2  tls #endif
     45  1.1.1.1.8.2  tls #ifdef EVENT__HAVE_UNISTD_H
     46  1.1.1.1.8.2  tls #include <unistd.h>
     47  1.1.1.1.8.2  tls #endif
     48  1.1.1.1.8.2  tls 
     49  1.1.1.1.8.2  tls #ifdef _WIN32
     50  1.1.1.1.8.2  tls #include <winsock2.h>
     51  1.1.1.1.8.2  tls #endif
     52  1.1.1.1.8.2  tls 
     53  1.1.1.1.8.2  tls #include "event2/bufferevent.h"
     54  1.1.1.1.8.2  tls #include "event2/bufferevent_struct.h"
     55  1.1.1.1.8.2  tls #include "event2/bufferevent_ssl.h"
     56  1.1.1.1.8.2  tls #include "event2/buffer.h"
     57  1.1.1.1.8.2  tls #include "event2/event.h"
     58  1.1.1.1.8.2  tls 
     59  1.1.1.1.8.2  tls #include "mm-internal.h"
     60  1.1.1.1.8.2  tls #include "bufferevent-internal.h"
     61  1.1.1.1.8.2  tls #include "log-internal.h"
     62  1.1.1.1.8.2  tls 
     63  1.1.1.1.8.2  tls #include <openssl/bio.h>
     64  1.1.1.1.8.2  tls #include <openssl/ssl.h>
     65  1.1.1.1.8.2  tls #include <openssl/err.h>
     66  1.1.1.1.8.2  tls 
     67  1.1.1.1.8.2  tls /*
     68  1.1.1.1.8.2  tls  * Define an OpenSSL bio that targets a bufferevent.
     69  1.1.1.1.8.2  tls  */
     70  1.1.1.1.8.2  tls 
     71  1.1.1.1.8.2  tls /* --------------------
     72  1.1.1.1.8.2  tls    A BIO is an OpenSSL abstraction that handles reading and writing data.  The
     73  1.1.1.1.8.2  tls    library will happily speak SSL over anything that implements a BIO
     74  1.1.1.1.8.2  tls    interface.
     75  1.1.1.1.8.2  tls 
     76  1.1.1.1.8.2  tls    Here we define a BIO implementation that directs its output to a
     77  1.1.1.1.8.2  tls    bufferevent.  We'll want to use this only when none of OpenSSL's built-in
     78  1.1.1.1.8.2  tls    IO mechanisms work for us.
     79  1.1.1.1.8.2  tls    -------------------- */
     80  1.1.1.1.8.2  tls 
     81  1.1.1.1.8.2  tls /* every BIO type needs its own integer type value. */
     82  1.1.1.1.8.2  tls #define BIO_TYPE_LIBEVENT 57
     83  1.1.1.1.8.2  tls /* ???? Arguably, we should set BIO_TYPE_FILTER or BIO_TYPE_SOURCE_SINK on
     84  1.1.1.1.8.2  tls  * this. */
     85  1.1.1.1.8.2  tls 
     86  1.1.1.1.8.2  tls #if 0
     87  1.1.1.1.8.2  tls static void
     88  1.1.1.1.8.2  tls print_err(int val)
     89  1.1.1.1.8.2  tls {
     90  1.1.1.1.8.2  tls 	int err;
     91  1.1.1.1.8.2  tls 	printf("Error was %d\n", val);
     92  1.1.1.1.8.2  tls 
     93  1.1.1.1.8.2  tls 	while ((err = ERR_get_error())) {
     94  1.1.1.1.8.2  tls 		const char *msg = (const char*)ERR_reason_error_string(err);
     95  1.1.1.1.8.2  tls 		const char *lib = (const char*)ERR_lib_error_string(err);
     96  1.1.1.1.8.2  tls 		const char *func = (const char*)ERR_func_error_string(err);
     97  1.1.1.1.8.2  tls 
     98  1.1.1.1.8.2  tls 		printf("%s in %s %s\n", msg, lib, func);
     99  1.1.1.1.8.2  tls 	}
    100  1.1.1.1.8.2  tls }
    101  1.1.1.1.8.2  tls #else
    102  1.1.1.1.8.2  tls #define print_err(v) ((void)0)
    103  1.1.1.1.8.2  tls #endif
    104  1.1.1.1.8.2  tls 
    105  1.1.1.1.8.2  tls /* Called to initialize a new BIO */
    106  1.1.1.1.8.2  tls static int
    107  1.1.1.1.8.2  tls bio_bufferevent_new(BIO *b)
    108  1.1.1.1.8.2  tls {
    109  1.1.1.1.8.2  tls 	b->init = 0;
    110  1.1.1.1.8.2  tls 	b->num = -1;
    111  1.1.1.1.8.2  tls 	b->ptr = NULL; /* We'll be putting the bufferevent in this field.*/
    112  1.1.1.1.8.2  tls 	b->flags = 0;
    113  1.1.1.1.8.2  tls 	return 1;
    114  1.1.1.1.8.2  tls }
    115  1.1.1.1.8.2  tls 
    116  1.1.1.1.8.2  tls /* Called to uninitialize the BIO. */
    117  1.1.1.1.8.2  tls static int
    118  1.1.1.1.8.2  tls bio_bufferevent_free(BIO *b)
    119  1.1.1.1.8.2  tls {
    120  1.1.1.1.8.2  tls 	if (!b)
    121  1.1.1.1.8.2  tls 		return 0;
    122  1.1.1.1.8.2  tls 	if (b->shutdown) {
    123  1.1.1.1.8.2  tls 		if (b->init && b->ptr)
    124  1.1.1.1.8.2  tls 			bufferevent_free(b->ptr);
    125  1.1.1.1.8.2  tls 		b->init = 0;
    126  1.1.1.1.8.2  tls 		b->flags = 0;
    127  1.1.1.1.8.2  tls 		b->ptr = NULL;
    128  1.1.1.1.8.2  tls 	}
    129  1.1.1.1.8.2  tls 	return 1;
    130  1.1.1.1.8.2  tls }
    131  1.1.1.1.8.2  tls 
    132  1.1.1.1.8.2  tls /* Called to extract data from the BIO. */
    133  1.1.1.1.8.2  tls static int
    134  1.1.1.1.8.2  tls bio_bufferevent_read(BIO *b, char *out, int outlen)
    135  1.1.1.1.8.2  tls {
    136  1.1.1.1.8.2  tls 	int r = 0;
    137  1.1.1.1.8.2  tls 	struct evbuffer *input;
    138  1.1.1.1.8.2  tls 
    139  1.1.1.1.8.2  tls 	BIO_clear_retry_flags(b);
    140  1.1.1.1.8.2  tls 
    141  1.1.1.1.8.2  tls 	if (!out)
    142  1.1.1.1.8.2  tls 		return 0;
    143  1.1.1.1.8.2  tls 	if (!b->ptr)
    144  1.1.1.1.8.2  tls 		return -1;
    145  1.1.1.1.8.2  tls 
    146  1.1.1.1.8.2  tls 	input = bufferevent_get_input(b->ptr);
    147  1.1.1.1.8.2  tls 	if (evbuffer_get_length(input) == 0) {
    148  1.1.1.1.8.2  tls 		/* If there's no data to read, say so. */
    149  1.1.1.1.8.2  tls 		BIO_set_retry_read(b);
    150  1.1.1.1.8.2  tls 		return -1;
    151  1.1.1.1.8.2  tls 	} else {
    152  1.1.1.1.8.2  tls 		r = evbuffer_remove(input, out, outlen);
    153  1.1.1.1.8.2  tls 	}
    154  1.1.1.1.8.2  tls 
    155  1.1.1.1.8.2  tls 	return r;
    156  1.1.1.1.8.2  tls }
    157  1.1.1.1.8.2  tls 
    158  1.1.1.1.8.2  tls /* Called to write data info the BIO */
    159  1.1.1.1.8.2  tls static int
    160  1.1.1.1.8.2  tls bio_bufferevent_write(BIO *b, const char *in, int inlen)
    161  1.1.1.1.8.2  tls {
    162  1.1.1.1.8.2  tls 	struct bufferevent *bufev = b->ptr;
    163  1.1.1.1.8.2  tls 	struct evbuffer *output;
    164  1.1.1.1.8.2  tls 	size_t outlen;
    165  1.1.1.1.8.2  tls 
    166  1.1.1.1.8.2  tls 	BIO_clear_retry_flags(b);
    167  1.1.1.1.8.2  tls 
    168  1.1.1.1.8.2  tls 	if (!b->ptr)
    169  1.1.1.1.8.2  tls 		return -1;
    170  1.1.1.1.8.2  tls 
    171  1.1.1.1.8.2  tls 	output = bufferevent_get_output(bufev);
    172  1.1.1.1.8.2  tls 	outlen = evbuffer_get_length(output);
    173  1.1.1.1.8.2  tls 
    174  1.1.1.1.8.2  tls 	/* Copy only as much data onto the output buffer as can fit under the
    175  1.1.1.1.8.2  tls 	 * high-water mark. */
    176  1.1.1.1.8.2  tls 	if (bufev->wm_write.high && bufev->wm_write.high <= (outlen+inlen)) {
    177  1.1.1.1.8.2  tls 		if (bufev->wm_write.high <= outlen) {
    178  1.1.1.1.8.2  tls 			/* If no data can fit, we'll need to retry later. */
    179  1.1.1.1.8.2  tls 			BIO_set_retry_write(b);
    180  1.1.1.1.8.2  tls 			return -1;
    181  1.1.1.1.8.2  tls 		}
    182  1.1.1.1.8.2  tls 		inlen = bufev->wm_write.high - outlen;
    183  1.1.1.1.8.2  tls 	}
    184  1.1.1.1.8.2  tls 
    185  1.1.1.1.8.2  tls 	EVUTIL_ASSERT(inlen > 0);
    186  1.1.1.1.8.2  tls 	evbuffer_add(output, in, inlen);
    187  1.1.1.1.8.2  tls 	return inlen;
    188  1.1.1.1.8.2  tls }
    189  1.1.1.1.8.2  tls 
    190  1.1.1.1.8.2  tls /* Called to handle various requests */
    191  1.1.1.1.8.2  tls static long
    192  1.1.1.1.8.2  tls bio_bufferevent_ctrl(BIO *b, int cmd, long num, void *ptr)
    193  1.1.1.1.8.2  tls {
    194  1.1.1.1.8.2  tls 	struct bufferevent *bufev = b->ptr;
    195  1.1.1.1.8.2  tls 	long ret = 1;
    196  1.1.1.1.8.2  tls 
    197  1.1.1.1.8.2  tls 	switch (cmd) {
    198  1.1.1.1.8.2  tls 	case BIO_CTRL_GET_CLOSE:
    199  1.1.1.1.8.2  tls 		ret = b->shutdown;
    200  1.1.1.1.8.2  tls 		break;
    201  1.1.1.1.8.2  tls 	case BIO_CTRL_SET_CLOSE:
    202  1.1.1.1.8.2  tls 		b->shutdown = (int)num;
    203  1.1.1.1.8.2  tls 		break;
    204  1.1.1.1.8.2  tls 	case BIO_CTRL_PENDING:
    205  1.1.1.1.8.2  tls 		ret = evbuffer_get_length(bufferevent_get_input(bufev)) != 0;
    206  1.1.1.1.8.2  tls 		break;
    207  1.1.1.1.8.2  tls 	case BIO_CTRL_WPENDING:
    208  1.1.1.1.8.2  tls 		ret = evbuffer_get_length(bufferevent_get_output(bufev)) != 0;
    209  1.1.1.1.8.2  tls 		break;
    210  1.1.1.1.8.2  tls 	/* XXXX These two are given a special-case treatment because
    211  1.1.1.1.8.2  tls 	 * of cargo-cultism.  I should come up with a better reason. */
    212  1.1.1.1.8.2  tls 	case BIO_CTRL_DUP:
    213  1.1.1.1.8.2  tls 	case BIO_CTRL_FLUSH:
    214  1.1.1.1.8.2  tls 		ret = 1;
    215  1.1.1.1.8.2  tls 		break;
    216  1.1.1.1.8.2  tls 	default:
    217  1.1.1.1.8.2  tls 		ret = 0;
    218  1.1.1.1.8.2  tls 		break;
    219  1.1.1.1.8.2  tls 	}
    220  1.1.1.1.8.2  tls 	return ret;
    221  1.1.1.1.8.2  tls }
    222  1.1.1.1.8.2  tls 
    223  1.1.1.1.8.2  tls /* Called to write a string to the BIO */
    224  1.1.1.1.8.2  tls static int
    225  1.1.1.1.8.2  tls bio_bufferevent_puts(BIO *b, const char *s)
    226  1.1.1.1.8.2  tls {
    227  1.1.1.1.8.2  tls 	return bio_bufferevent_write(b, s, strlen(s));
    228  1.1.1.1.8.2  tls }
    229  1.1.1.1.8.2  tls 
    230  1.1.1.1.8.2  tls /* Method table for the bufferevent BIO */
    231  1.1.1.1.8.2  tls static BIO_METHOD methods_bufferevent = {
    232  1.1.1.1.8.2  tls 	BIO_TYPE_LIBEVENT, "bufferevent",
    233  1.1.1.1.8.2  tls 	bio_bufferevent_write,
    234  1.1.1.1.8.2  tls 	bio_bufferevent_read,
    235  1.1.1.1.8.2  tls 	bio_bufferevent_puts,
    236  1.1.1.1.8.2  tls 	NULL /* bio_bufferevent_gets */,
    237  1.1.1.1.8.2  tls 	bio_bufferevent_ctrl,
    238  1.1.1.1.8.2  tls 	bio_bufferevent_new,
    239  1.1.1.1.8.2  tls 	bio_bufferevent_free,
    240  1.1.1.1.8.2  tls 	NULL /* callback_ctrl */,
    241  1.1.1.1.8.2  tls };
    242  1.1.1.1.8.2  tls 
    243  1.1.1.1.8.2  tls /* Return the method table for the bufferevents BIO */
    244  1.1.1.1.8.2  tls static BIO_METHOD *
    245  1.1.1.1.8.2  tls BIO_s_bufferevent(void)
    246  1.1.1.1.8.2  tls {
    247  1.1.1.1.8.2  tls 	return &methods_bufferevent;
    248  1.1.1.1.8.2  tls }
    249  1.1.1.1.8.2  tls 
    250  1.1.1.1.8.2  tls /* Create a new BIO to wrap communication around a bufferevent.  If close_flag
    251  1.1.1.1.8.2  tls  * is true, the bufferevent will be freed when the BIO is closed. */
    252  1.1.1.1.8.2  tls static BIO *
    253  1.1.1.1.8.2  tls BIO_new_bufferevent(struct bufferevent *bufferevent, int close_flag)
    254  1.1.1.1.8.2  tls {
    255  1.1.1.1.8.2  tls 	BIO *result;
    256  1.1.1.1.8.2  tls 	if (!bufferevent)
    257  1.1.1.1.8.2  tls 		return NULL;
    258  1.1.1.1.8.2  tls 	if (!(result = BIO_new(BIO_s_bufferevent())))
    259  1.1.1.1.8.2  tls 		return NULL;
    260  1.1.1.1.8.2  tls 	result->init = 1;
    261  1.1.1.1.8.2  tls 	result->ptr = bufferevent;
    262  1.1.1.1.8.2  tls 	result->shutdown = close_flag ? 1 : 0;
    263  1.1.1.1.8.2  tls 	return result;
    264  1.1.1.1.8.2  tls }
    265  1.1.1.1.8.2  tls 
    266  1.1.1.1.8.2  tls /* --------------------
    267  1.1.1.1.8.2  tls    Now, here's the OpenSSL-based implementation of bufferevent.
    268  1.1.1.1.8.2  tls 
    269  1.1.1.1.8.2  tls    The implementation comes in two flavors: one that connects its SSL object
    270  1.1.1.1.8.2  tls    to an underlying bufferevent using a BIO_bufferevent, and one that has the
    271  1.1.1.1.8.2  tls    SSL object connect to a socket directly.  The latter should generally be
    272  1.1.1.1.8.2  tls    faster, except on Windows, where your best bet is using a
    273  1.1.1.1.8.2  tls    bufferevent_async.
    274  1.1.1.1.8.2  tls 
    275  1.1.1.1.8.2  tls    (OpenSSL supports many other BIO types, too.  But we can't use any unless
    276  1.1.1.1.8.2  tls    we have a good way to get notified when they become readable/writable.)
    277  1.1.1.1.8.2  tls    -------------------- */
    278  1.1.1.1.8.2  tls 
    279  1.1.1.1.8.2  tls struct bio_data_counts {
    280  1.1.1.1.8.2  tls 	unsigned long n_written;
    281  1.1.1.1.8.2  tls 	unsigned long n_read;
    282  1.1.1.1.8.2  tls };
    283  1.1.1.1.8.2  tls 
    284  1.1.1.1.8.2  tls struct bufferevent_openssl {
    285  1.1.1.1.8.2  tls 	/* Shared fields with common bufferevent implementation code.
    286  1.1.1.1.8.2  tls 	   If we were set up with an underlying bufferevent, we use the
    287  1.1.1.1.8.2  tls 	   events here as timers only.  If we have an SSL, then we use
    288  1.1.1.1.8.2  tls 	   the events as socket events.
    289  1.1.1.1.8.2  tls 	 */
    290  1.1.1.1.8.2  tls 	struct bufferevent_private bev;
    291  1.1.1.1.8.2  tls 	/* An underlying bufferevent that we're directing our output to.
    292  1.1.1.1.8.2  tls 	   If it's NULL, then we're connected to an fd, not an evbuffer. */
    293  1.1.1.1.8.2  tls 	struct bufferevent *underlying;
    294  1.1.1.1.8.2  tls 	/* The SSL object doing our encryption. */
    295  1.1.1.1.8.2  tls 	SSL *ssl;
    296  1.1.1.1.8.2  tls 
    297  1.1.1.1.8.2  tls 	/* A callback that's invoked when data arrives on our outbuf so we
    298  1.1.1.1.8.2  tls 	   know to write data to the SSL. */
    299  1.1.1.1.8.2  tls 	struct evbuffer_cb_entry *outbuf_cb;
    300  1.1.1.1.8.2  tls 
    301  1.1.1.1.8.2  tls 	/* A count of how much data the bios have read/written total.  Used
    302  1.1.1.1.8.2  tls 	   for rate-limiting. */
    303  1.1.1.1.8.2  tls 	struct bio_data_counts counts;
    304  1.1.1.1.8.2  tls 
    305  1.1.1.1.8.2  tls 	/* If this value is greater than 0, then the last SSL_write blocked,
    306  1.1.1.1.8.2  tls 	 * and we need to try it again with this many bytes. */
    307  1.1.1.1.8.2  tls 	ev_ssize_t last_write;
    308  1.1.1.1.8.2  tls 
    309  1.1.1.1.8.2  tls #define NUM_ERRORS 3
    310  1.1.1.1.8.2  tls 	ev_uint32_t errors[NUM_ERRORS];
    311  1.1.1.1.8.2  tls 
    312  1.1.1.1.8.2  tls 	/* When we next get available space, we should say "read" instead of
    313  1.1.1.1.8.2  tls 	   "write". This can happen if there's a renegotiation during a read
    314  1.1.1.1.8.2  tls 	   operation. */
    315  1.1.1.1.8.2  tls 	unsigned read_blocked_on_write : 1;
    316  1.1.1.1.8.2  tls 	/* When we next get data, we should say "write" instead of "read". */
    317  1.1.1.1.8.2  tls 	unsigned write_blocked_on_read : 1;
    318  1.1.1.1.8.2  tls 	/* Treat TCP close before SSL close on SSL >= v3 as clean EOF. */
    319  1.1.1.1.8.2  tls 	unsigned allow_dirty_shutdown : 1;
    320  1.1.1.1.8.2  tls 	/* XXXX */
    321  1.1.1.1.8.2  tls 	unsigned fd_is_set : 1;
    322  1.1.1.1.8.2  tls 	/* XXX */
    323  1.1.1.1.8.2  tls 	unsigned n_errors : 2;
    324  1.1.1.1.8.2  tls 
    325  1.1.1.1.8.2  tls 	/* Are we currently connecting, accepting, or doing IO? */
    326  1.1.1.1.8.2  tls 	unsigned state : 2;
    327  1.1.1.1.8.2  tls };
    328  1.1.1.1.8.2  tls 
    329  1.1.1.1.8.2  tls static int be_openssl_enable(struct bufferevent *, short);
    330  1.1.1.1.8.2  tls static int be_openssl_disable(struct bufferevent *, short);
    331  1.1.1.1.8.2  tls static void be_openssl_destruct(struct bufferevent *);
    332  1.1.1.1.8.2  tls static int be_openssl_adj_timeouts(struct bufferevent *);
    333  1.1.1.1.8.2  tls static int be_openssl_flush(struct bufferevent *bufev,
    334  1.1.1.1.8.2  tls     short iotype, enum bufferevent_flush_mode mode);
    335  1.1.1.1.8.2  tls static int be_openssl_ctrl(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
    336  1.1.1.1.8.2  tls 
    337  1.1.1.1.8.2  tls const struct bufferevent_ops bufferevent_ops_openssl = {
    338  1.1.1.1.8.2  tls 	"ssl",
    339  1.1.1.1.8.2  tls 	evutil_offsetof(struct bufferevent_openssl, bev.bev),
    340  1.1.1.1.8.2  tls 	be_openssl_enable,
    341  1.1.1.1.8.2  tls 	be_openssl_disable,
    342  1.1.1.1.8.2  tls 	be_openssl_destruct,
    343  1.1.1.1.8.2  tls 	be_openssl_adj_timeouts,
    344  1.1.1.1.8.2  tls 	be_openssl_flush,
    345  1.1.1.1.8.2  tls 	be_openssl_ctrl,
    346  1.1.1.1.8.2  tls };
    347  1.1.1.1.8.2  tls 
    348  1.1.1.1.8.2  tls /* Given a bufferevent, return a pointer to the bufferevent_openssl that
    349  1.1.1.1.8.2  tls  * contains it, if any. */
    350  1.1.1.1.8.2  tls static inline struct bufferevent_openssl *
    351  1.1.1.1.8.2  tls upcast(struct bufferevent *bev)
    352  1.1.1.1.8.2  tls {
    353  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_o;
    354  1.1.1.1.8.2  tls 	if (bev->be_ops != &bufferevent_ops_openssl)
    355  1.1.1.1.8.2  tls 		return NULL;
    356  1.1.1.1.8.2  tls 	bev_o = (void*)( ((char*)bev) -
    357  1.1.1.1.8.2  tls 			 evutil_offsetof(struct bufferevent_openssl, bev.bev));
    358  1.1.1.1.8.2  tls 	EVUTIL_ASSERT(bev_o->bev.bev.be_ops == &bufferevent_ops_openssl);
    359  1.1.1.1.8.2  tls 	return bev_o;
    360  1.1.1.1.8.2  tls }
    361  1.1.1.1.8.2  tls 
    362  1.1.1.1.8.2  tls static inline void
    363  1.1.1.1.8.2  tls put_error(struct bufferevent_openssl *bev_ssl, unsigned long err)
    364  1.1.1.1.8.2  tls {
    365  1.1.1.1.8.2  tls 	if (bev_ssl->n_errors == NUM_ERRORS)
    366  1.1.1.1.8.2  tls 		return;
    367  1.1.1.1.8.2  tls 	/* The error type according to openssl is "unsigned long", but
    368  1.1.1.1.8.2  tls 	   openssl never uses more than 32 bits of it.  It _can't_ use more
    369  1.1.1.1.8.2  tls 	   than 32 bits of it, since it needs to report errors on systems
    370  1.1.1.1.8.2  tls 	   where long is only 32 bits.
    371  1.1.1.1.8.2  tls 	 */
    372  1.1.1.1.8.2  tls 	bev_ssl->errors[bev_ssl->n_errors++] = (ev_uint32_t) err;
    373  1.1.1.1.8.2  tls }
    374  1.1.1.1.8.2  tls 
    375  1.1.1.1.8.2  tls /* Have the base communications channel (either the underlying bufferevent or
    376  1.1.1.1.8.2  tls  * ev_read and ev_write) start reading.  Take the read-blocked-on-write flag
    377  1.1.1.1.8.2  tls  * into account. */
    378  1.1.1.1.8.2  tls static int
    379  1.1.1.1.8.2  tls start_reading(struct bufferevent_openssl *bev_ssl)
    380  1.1.1.1.8.2  tls {
    381  1.1.1.1.8.2  tls 	if (bev_ssl->underlying) {
    382  1.1.1.1.8.2  tls 		bufferevent_unsuspend_read_(bev_ssl->underlying,
    383  1.1.1.1.8.2  tls 		    BEV_SUSPEND_FILT_READ);
    384  1.1.1.1.8.2  tls 		return 0;
    385  1.1.1.1.8.2  tls 	} else {
    386  1.1.1.1.8.2  tls 		struct bufferevent *bev = &bev_ssl->bev.bev;
    387  1.1.1.1.8.2  tls 		int r;
    388  1.1.1.1.8.2  tls 		r = bufferevent_add_event_(&bev->ev_read, &bev->timeout_read);
    389  1.1.1.1.8.2  tls 		if (r == 0 && bev_ssl->read_blocked_on_write)
    390  1.1.1.1.8.2  tls 			r = bufferevent_add_event_(&bev->ev_write,
    391  1.1.1.1.8.2  tls 			    &bev->timeout_write);
    392  1.1.1.1.8.2  tls 		return r;
    393  1.1.1.1.8.2  tls 	}
    394  1.1.1.1.8.2  tls }
    395  1.1.1.1.8.2  tls 
    396  1.1.1.1.8.2  tls /* Have the base communications channel (either the underlying bufferevent or
    397  1.1.1.1.8.2  tls  * ev_read and ev_write) start writing.  Take the write-blocked-on-read flag
    398  1.1.1.1.8.2  tls  * into account. */
    399  1.1.1.1.8.2  tls static int
    400  1.1.1.1.8.2  tls start_writing(struct bufferevent_openssl *bev_ssl)
    401  1.1.1.1.8.2  tls {
    402  1.1.1.1.8.2  tls 	int r = 0;
    403  1.1.1.1.8.2  tls 	if (bev_ssl->underlying) {
    404  1.1.1.1.8.2  tls 		;
    405  1.1.1.1.8.2  tls 	} else {
    406  1.1.1.1.8.2  tls 		struct bufferevent *bev = &bev_ssl->bev.bev;
    407  1.1.1.1.8.2  tls 		r = bufferevent_add_event_(&bev->ev_write, &bev->timeout_write);
    408  1.1.1.1.8.2  tls 		if (!r && bev_ssl->write_blocked_on_read)
    409  1.1.1.1.8.2  tls 			r = bufferevent_add_event_(&bev->ev_read,
    410  1.1.1.1.8.2  tls 			    &bev->timeout_read);
    411  1.1.1.1.8.2  tls 	}
    412  1.1.1.1.8.2  tls 	return r;
    413  1.1.1.1.8.2  tls }
    414  1.1.1.1.8.2  tls 
    415  1.1.1.1.8.2  tls static void
    416  1.1.1.1.8.2  tls stop_reading(struct bufferevent_openssl *bev_ssl)
    417  1.1.1.1.8.2  tls {
    418  1.1.1.1.8.2  tls 	if (bev_ssl->write_blocked_on_read)
    419  1.1.1.1.8.2  tls 		return;
    420  1.1.1.1.8.2  tls 	if (bev_ssl->underlying) {
    421  1.1.1.1.8.2  tls 		bufferevent_suspend_read_(bev_ssl->underlying,
    422  1.1.1.1.8.2  tls 		    BEV_SUSPEND_FILT_READ);
    423  1.1.1.1.8.2  tls 	} else {
    424  1.1.1.1.8.2  tls 		struct bufferevent *bev = &bev_ssl->bev.bev;
    425  1.1.1.1.8.2  tls 		event_del(&bev->ev_read);
    426  1.1.1.1.8.2  tls 	}
    427  1.1.1.1.8.2  tls }
    428  1.1.1.1.8.2  tls 
    429  1.1.1.1.8.2  tls static void
    430  1.1.1.1.8.2  tls stop_writing(struct bufferevent_openssl *bev_ssl)
    431  1.1.1.1.8.2  tls {
    432  1.1.1.1.8.2  tls 	if (bev_ssl->read_blocked_on_write)
    433  1.1.1.1.8.2  tls 		return;
    434  1.1.1.1.8.2  tls 	if (bev_ssl->underlying) {
    435  1.1.1.1.8.2  tls 		;
    436  1.1.1.1.8.2  tls 	} else {
    437  1.1.1.1.8.2  tls 		struct bufferevent *bev = &bev_ssl->bev.bev;
    438  1.1.1.1.8.2  tls 		event_del(&bev->ev_write);
    439  1.1.1.1.8.2  tls 	}
    440  1.1.1.1.8.2  tls }
    441  1.1.1.1.8.2  tls 
    442  1.1.1.1.8.2  tls static int
    443  1.1.1.1.8.2  tls set_rbow(struct bufferevent_openssl *bev_ssl)
    444  1.1.1.1.8.2  tls {
    445  1.1.1.1.8.2  tls 	if (!bev_ssl->underlying)
    446  1.1.1.1.8.2  tls 		stop_reading(bev_ssl);
    447  1.1.1.1.8.2  tls 	bev_ssl->read_blocked_on_write = 1;
    448  1.1.1.1.8.2  tls 	return start_writing(bev_ssl);
    449  1.1.1.1.8.2  tls }
    450  1.1.1.1.8.2  tls 
    451  1.1.1.1.8.2  tls static int
    452  1.1.1.1.8.2  tls set_wbor(struct bufferevent_openssl *bev_ssl)
    453  1.1.1.1.8.2  tls {
    454  1.1.1.1.8.2  tls 	if (!bev_ssl->underlying)
    455  1.1.1.1.8.2  tls 		stop_writing(bev_ssl);
    456  1.1.1.1.8.2  tls 	bev_ssl->write_blocked_on_read = 1;
    457  1.1.1.1.8.2  tls 	return start_reading(bev_ssl);
    458  1.1.1.1.8.2  tls }
    459  1.1.1.1.8.2  tls 
    460  1.1.1.1.8.2  tls static int
    461  1.1.1.1.8.2  tls clear_rbow(struct bufferevent_openssl *bev_ssl)
    462  1.1.1.1.8.2  tls {
    463  1.1.1.1.8.2  tls 	struct bufferevent *bev = &bev_ssl->bev.bev;
    464  1.1.1.1.8.2  tls 	int r = 0;
    465  1.1.1.1.8.2  tls 	bev_ssl->read_blocked_on_write = 0;
    466  1.1.1.1.8.2  tls 	if (!(bev->enabled & EV_WRITE))
    467  1.1.1.1.8.2  tls 		stop_writing(bev_ssl);
    468  1.1.1.1.8.2  tls 	if (bev->enabled & EV_READ)
    469  1.1.1.1.8.2  tls 		r = start_reading(bev_ssl);
    470  1.1.1.1.8.2  tls 	return r;
    471  1.1.1.1.8.2  tls }
    472  1.1.1.1.8.2  tls 
    473  1.1.1.1.8.2  tls 
    474  1.1.1.1.8.2  tls static int
    475  1.1.1.1.8.2  tls clear_wbor(struct bufferevent_openssl *bev_ssl)
    476  1.1.1.1.8.2  tls {
    477  1.1.1.1.8.2  tls 	struct bufferevent *bev = &bev_ssl->bev.bev;
    478  1.1.1.1.8.2  tls 	int r = 0;
    479  1.1.1.1.8.2  tls 	bev_ssl->write_blocked_on_read = 0;
    480  1.1.1.1.8.2  tls 	if (!(bev->enabled & EV_READ))
    481  1.1.1.1.8.2  tls 		stop_reading(bev_ssl);
    482  1.1.1.1.8.2  tls 	if (bev->enabled & EV_WRITE)
    483  1.1.1.1.8.2  tls 		r = start_writing(bev_ssl);
    484  1.1.1.1.8.2  tls 	return r;
    485  1.1.1.1.8.2  tls }
    486  1.1.1.1.8.2  tls 
    487  1.1.1.1.8.2  tls static void
    488  1.1.1.1.8.2  tls conn_closed(struct bufferevent_openssl *bev_ssl, int when, int errcode, int ret)
    489  1.1.1.1.8.2  tls {
    490  1.1.1.1.8.2  tls 	int event = BEV_EVENT_ERROR;
    491  1.1.1.1.8.2  tls 	int dirty_shutdown = 0;
    492  1.1.1.1.8.2  tls 	unsigned long err;
    493  1.1.1.1.8.2  tls 
    494  1.1.1.1.8.2  tls 	switch (errcode) {
    495  1.1.1.1.8.2  tls 	case SSL_ERROR_ZERO_RETURN:
    496  1.1.1.1.8.2  tls 		/* Possibly a clean shutdown. */
    497  1.1.1.1.8.2  tls 		if (SSL_get_shutdown(bev_ssl->ssl) & SSL_RECEIVED_SHUTDOWN)
    498  1.1.1.1.8.2  tls 			event = BEV_EVENT_EOF;
    499  1.1.1.1.8.2  tls 		else
    500  1.1.1.1.8.2  tls 			dirty_shutdown = 1;
    501  1.1.1.1.8.2  tls 		break;
    502  1.1.1.1.8.2  tls 	case SSL_ERROR_SYSCALL:
    503  1.1.1.1.8.2  tls 		/* IO error; possibly a dirty shutdown. */
    504  1.1.1.1.8.2  tls 		if (ret == 0 && ERR_peek_error() == 0)
    505  1.1.1.1.8.2  tls 			dirty_shutdown = 1;
    506  1.1.1.1.8.2  tls 		break;
    507  1.1.1.1.8.2  tls 	case SSL_ERROR_SSL:
    508  1.1.1.1.8.2  tls 		/* Protocol error. */
    509  1.1.1.1.8.2  tls 		break;
    510  1.1.1.1.8.2  tls 	case SSL_ERROR_WANT_X509_LOOKUP:
    511  1.1.1.1.8.2  tls 		/* XXXX handle this. */
    512  1.1.1.1.8.2  tls 		break;
    513  1.1.1.1.8.2  tls 	case SSL_ERROR_NONE:
    514  1.1.1.1.8.2  tls 	case SSL_ERROR_WANT_READ:
    515  1.1.1.1.8.2  tls 	case SSL_ERROR_WANT_WRITE:
    516  1.1.1.1.8.2  tls 	case SSL_ERROR_WANT_CONNECT:
    517  1.1.1.1.8.2  tls 	case SSL_ERROR_WANT_ACCEPT:
    518  1.1.1.1.8.2  tls 	default:
    519  1.1.1.1.8.2  tls 		/* should be impossible; treat as normal error. */
    520  1.1.1.1.8.2  tls 		event_warnx("BUG: Unexpected OpenSSL error code %d", errcode);
    521  1.1.1.1.8.2  tls 		break;
    522  1.1.1.1.8.2  tls 	}
    523  1.1.1.1.8.2  tls 
    524  1.1.1.1.8.2  tls 	while ((err = ERR_get_error())) {
    525  1.1.1.1.8.2  tls 		put_error(bev_ssl, err);
    526  1.1.1.1.8.2  tls 	}
    527  1.1.1.1.8.2  tls 
    528  1.1.1.1.8.2  tls 	if (dirty_shutdown && bev_ssl->allow_dirty_shutdown)
    529  1.1.1.1.8.2  tls 		event = BEV_EVENT_EOF;
    530  1.1.1.1.8.2  tls 
    531  1.1.1.1.8.2  tls 	stop_reading(bev_ssl);
    532  1.1.1.1.8.2  tls 	stop_writing(bev_ssl);
    533  1.1.1.1.8.2  tls 
    534  1.1.1.1.8.2  tls 	/* when is BEV_EVENT_{READING|WRITING} */
    535  1.1.1.1.8.2  tls 	event = when | event;
    536  1.1.1.1.8.2  tls 	bufferevent_run_eventcb_(&bev_ssl->bev.bev, event);
    537  1.1.1.1.8.2  tls }
    538  1.1.1.1.8.2  tls 
    539  1.1.1.1.8.2  tls static void
    540  1.1.1.1.8.2  tls init_bio_counts(struct bufferevent_openssl *bev_ssl)
    541  1.1.1.1.8.2  tls {
    542  1.1.1.1.8.2  tls 	bev_ssl->counts.n_written =
    543  1.1.1.1.8.2  tls 	    BIO_number_written(SSL_get_wbio(bev_ssl->ssl));
    544  1.1.1.1.8.2  tls 	bev_ssl->counts.n_read =
    545  1.1.1.1.8.2  tls 	    BIO_number_read(SSL_get_rbio(bev_ssl->ssl));
    546  1.1.1.1.8.2  tls }
    547  1.1.1.1.8.2  tls 
    548  1.1.1.1.8.2  tls static inline void
    549  1.1.1.1.8.2  tls decrement_buckets(struct bufferevent_openssl *bev_ssl)
    550  1.1.1.1.8.2  tls {
    551  1.1.1.1.8.2  tls 	unsigned long num_w = BIO_number_written(SSL_get_wbio(bev_ssl->ssl));
    552  1.1.1.1.8.2  tls 	unsigned long num_r = BIO_number_read(SSL_get_rbio(bev_ssl->ssl));
    553  1.1.1.1.8.2  tls 	/* These next two subtractions can wrap around. That's okay. */
    554  1.1.1.1.8.2  tls 	unsigned long w = num_w - bev_ssl->counts.n_written;
    555  1.1.1.1.8.2  tls 	unsigned long r = num_r - bev_ssl->counts.n_read;
    556  1.1.1.1.8.2  tls 	if (w)
    557  1.1.1.1.8.2  tls 		bufferevent_decrement_write_buckets_(&bev_ssl->bev, w);
    558  1.1.1.1.8.2  tls 	if (r)
    559  1.1.1.1.8.2  tls 		bufferevent_decrement_read_buckets_(&bev_ssl->bev, r);
    560  1.1.1.1.8.2  tls 	bev_ssl->counts.n_written = num_w;
    561  1.1.1.1.8.2  tls 	bev_ssl->counts.n_read = num_r;
    562  1.1.1.1.8.2  tls }
    563  1.1.1.1.8.2  tls 
    564  1.1.1.1.8.2  tls #define OP_MADE_PROGRESS 1
    565  1.1.1.1.8.2  tls #define OP_BLOCKED 2
    566  1.1.1.1.8.2  tls #define OP_ERR 4
    567  1.1.1.1.8.2  tls 
    568  1.1.1.1.8.2  tls /* Return a bitmask of OP_MADE_PROGRESS (if we read anything); OP_BLOCKED (if
    569  1.1.1.1.8.2  tls    we're now blocked); and OP_ERR (if an error occurred). */
    570  1.1.1.1.8.2  tls static int
    571  1.1.1.1.8.2  tls do_read(struct bufferevent_openssl *bev_ssl, int n_to_read) {
    572  1.1.1.1.8.2  tls 	/* Requires lock */
    573  1.1.1.1.8.2  tls 	struct bufferevent *bev = &bev_ssl->bev.bev;
    574  1.1.1.1.8.2  tls 	struct evbuffer *input = bev->input;
    575  1.1.1.1.8.2  tls 	int r, n, i, n_used = 0, atmost;
    576  1.1.1.1.8.2  tls 	struct evbuffer_iovec space[2];
    577  1.1.1.1.8.2  tls 	int result = 0;
    578  1.1.1.1.8.2  tls 
    579  1.1.1.1.8.2  tls 	if (bev_ssl->bev.read_suspended)
    580  1.1.1.1.8.2  tls 		return 0;
    581  1.1.1.1.8.2  tls 
    582  1.1.1.1.8.2  tls 	atmost = bufferevent_get_read_max_(&bev_ssl->bev);
    583  1.1.1.1.8.2  tls 	if (n_to_read > atmost)
    584  1.1.1.1.8.2  tls 		n_to_read = atmost;
    585  1.1.1.1.8.2  tls 
    586  1.1.1.1.8.2  tls 	n = evbuffer_reserve_space(input, n_to_read, space, 2);
    587  1.1.1.1.8.2  tls 	if (n < 0)
    588  1.1.1.1.8.2  tls 		return OP_ERR;
    589  1.1.1.1.8.2  tls 
    590  1.1.1.1.8.2  tls 	for (i=0; i<n; ++i) {
    591  1.1.1.1.8.2  tls 		if (bev_ssl->bev.read_suspended)
    592  1.1.1.1.8.2  tls 			break;
    593  1.1.1.1.8.2  tls 		r = SSL_read(bev_ssl->ssl, space[i].iov_base, space[i].iov_len);
    594  1.1.1.1.8.2  tls 		if (r>0) {
    595  1.1.1.1.8.2  tls 			result |= OP_MADE_PROGRESS;
    596  1.1.1.1.8.2  tls 			if (bev_ssl->read_blocked_on_write)
    597  1.1.1.1.8.2  tls 				if (clear_rbow(bev_ssl) < 0)
    598  1.1.1.1.8.2  tls 					return OP_ERR | result;
    599  1.1.1.1.8.2  tls 			++n_used;
    600  1.1.1.1.8.2  tls 			space[i].iov_len = r;
    601  1.1.1.1.8.2  tls 			decrement_buckets(bev_ssl);
    602  1.1.1.1.8.2  tls 		} else {
    603  1.1.1.1.8.2  tls 			int err = SSL_get_error(bev_ssl->ssl, r);
    604  1.1.1.1.8.2  tls 			print_err(err);
    605  1.1.1.1.8.2  tls 			switch (err) {
    606  1.1.1.1.8.2  tls 			case SSL_ERROR_WANT_READ:
    607  1.1.1.1.8.2  tls 				/* Can't read until underlying has more data. */
    608  1.1.1.1.8.2  tls 				if (bev_ssl->read_blocked_on_write)
    609  1.1.1.1.8.2  tls 					if (clear_rbow(bev_ssl) < 0)
    610  1.1.1.1.8.2  tls 						return OP_ERR | result;
    611  1.1.1.1.8.2  tls 				break;
    612  1.1.1.1.8.2  tls 			case SSL_ERROR_WANT_WRITE:
    613  1.1.1.1.8.2  tls 				/* This read operation requires a write, and the
    614  1.1.1.1.8.2  tls 				 * underlying is full */
    615  1.1.1.1.8.2  tls 				if (!bev_ssl->read_blocked_on_write)
    616  1.1.1.1.8.2  tls 					if (set_rbow(bev_ssl) < 0)
    617  1.1.1.1.8.2  tls 						return OP_ERR | result;
    618  1.1.1.1.8.2  tls 				break;
    619  1.1.1.1.8.2  tls 			default:
    620  1.1.1.1.8.2  tls 				conn_closed(bev_ssl, BEV_EVENT_READING, err, r);
    621  1.1.1.1.8.2  tls 				break;
    622  1.1.1.1.8.2  tls 			}
    623  1.1.1.1.8.2  tls 			result |= OP_BLOCKED;
    624  1.1.1.1.8.2  tls 			break; /* out of the loop */
    625  1.1.1.1.8.2  tls 		}
    626  1.1.1.1.8.2  tls 	}
    627  1.1.1.1.8.2  tls 
    628  1.1.1.1.8.2  tls 	if (n_used) {
    629  1.1.1.1.8.2  tls 		evbuffer_commit_space(input, space, n_used);
    630  1.1.1.1.8.2  tls 		if (bev_ssl->underlying)
    631  1.1.1.1.8.2  tls 			BEV_RESET_GENERIC_READ_TIMEOUT(bev);
    632  1.1.1.1.8.2  tls 	}
    633  1.1.1.1.8.2  tls 
    634  1.1.1.1.8.2  tls 	return result;
    635  1.1.1.1.8.2  tls }
    636  1.1.1.1.8.2  tls 
    637  1.1.1.1.8.2  tls /* Return a bitmask of OP_MADE_PROGRESS (if we wrote anything); OP_BLOCKED (if
    638  1.1.1.1.8.2  tls    we're now blocked); and OP_ERR (if an error occurred). */
    639  1.1.1.1.8.2  tls static int
    640  1.1.1.1.8.2  tls do_write(struct bufferevent_openssl *bev_ssl, int atmost)
    641  1.1.1.1.8.2  tls {
    642  1.1.1.1.8.2  tls 	int i, r, n, n_written = 0;
    643  1.1.1.1.8.2  tls 	struct bufferevent *bev = &bev_ssl->bev.bev;
    644  1.1.1.1.8.2  tls 	struct evbuffer *output = bev->output;
    645  1.1.1.1.8.2  tls 	struct evbuffer_iovec space[8];
    646  1.1.1.1.8.2  tls 	int result = 0;
    647  1.1.1.1.8.2  tls 
    648  1.1.1.1.8.2  tls 	if (bev_ssl->last_write > 0)
    649  1.1.1.1.8.2  tls 		atmost = bev_ssl->last_write;
    650  1.1.1.1.8.2  tls 	else
    651  1.1.1.1.8.2  tls 		atmost = bufferevent_get_write_max_(&bev_ssl->bev);
    652  1.1.1.1.8.2  tls 
    653  1.1.1.1.8.2  tls 	n = evbuffer_peek(output, atmost, NULL, space, 8);
    654  1.1.1.1.8.2  tls 	if (n < 0)
    655  1.1.1.1.8.2  tls 		return OP_ERR | result;
    656  1.1.1.1.8.2  tls 
    657  1.1.1.1.8.2  tls 	if (n > 8)
    658  1.1.1.1.8.2  tls 		n = 8;
    659  1.1.1.1.8.2  tls 	for (i=0; i < n; ++i) {
    660  1.1.1.1.8.2  tls 		if (bev_ssl->bev.write_suspended)
    661  1.1.1.1.8.2  tls 			break;
    662  1.1.1.1.8.2  tls 
    663  1.1.1.1.8.2  tls 		/* SSL_write will (reasonably) return 0 if we tell it to
    664  1.1.1.1.8.2  tls 		   send 0 data.  Skip this case so we don't interpret the
    665  1.1.1.1.8.2  tls 		   result as an error */
    666  1.1.1.1.8.2  tls 		if (space[i].iov_len == 0)
    667  1.1.1.1.8.2  tls 			continue;
    668  1.1.1.1.8.2  tls 
    669  1.1.1.1.8.2  tls 		r = SSL_write(bev_ssl->ssl, space[i].iov_base,
    670  1.1.1.1.8.2  tls 		    space[i].iov_len);
    671  1.1.1.1.8.2  tls 		if (r > 0) {
    672  1.1.1.1.8.2  tls 			result |= OP_MADE_PROGRESS;
    673  1.1.1.1.8.2  tls 			if (bev_ssl->write_blocked_on_read)
    674  1.1.1.1.8.2  tls 				if (clear_wbor(bev_ssl) < 0)
    675  1.1.1.1.8.2  tls 					return OP_ERR | result;
    676  1.1.1.1.8.2  tls 			n_written += r;
    677  1.1.1.1.8.2  tls 			bev_ssl->last_write = -1;
    678  1.1.1.1.8.2  tls 			decrement_buckets(bev_ssl);
    679  1.1.1.1.8.2  tls 		} else {
    680  1.1.1.1.8.2  tls 			int err = SSL_get_error(bev_ssl->ssl, r);
    681  1.1.1.1.8.2  tls 			print_err(err);
    682  1.1.1.1.8.2  tls 			switch (err) {
    683  1.1.1.1.8.2  tls 			case SSL_ERROR_WANT_WRITE:
    684  1.1.1.1.8.2  tls 				/* Can't read until underlying has more data. */
    685  1.1.1.1.8.2  tls 				if (bev_ssl->write_blocked_on_read)
    686  1.1.1.1.8.2  tls 					if (clear_wbor(bev_ssl) < 0)
    687  1.1.1.1.8.2  tls 						return OP_ERR | result;
    688  1.1.1.1.8.2  tls 				bev_ssl->last_write = space[i].iov_len;
    689  1.1.1.1.8.2  tls 				break;
    690  1.1.1.1.8.2  tls 			case SSL_ERROR_WANT_READ:
    691  1.1.1.1.8.2  tls 				/* This read operation requires a write, and the
    692  1.1.1.1.8.2  tls 				 * underlying is full */
    693  1.1.1.1.8.2  tls 				if (!bev_ssl->write_blocked_on_read)
    694  1.1.1.1.8.2  tls 					if (set_wbor(bev_ssl) < 0)
    695  1.1.1.1.8.2  tls 						return OP_ERR | result;
    696  1.1.1.1.8.2  tls 				bev_ssl->last_write = space[i].iov_len;
    697  1.1.1.1.8.2  tls 				break;
    698  1.1.1.1.8.2  tls 			default:
    699  1.1.1.1.8.2  tls 				conn_closed(bev_ssl, BEV_EVENT_WRITING, err, r);
    700  1.1.1.1.8.2  tls 				bev_ssl->last_write = -1;
    701  1.1.1.1.8.2  tls 				break;
    702  1.1.1.1.8.2  tls 			}
    703  1.1.1.1.8.2  tls 			result |= OP_BLOCKED;
    704  1.1.1.1.8.2  tls 			break;
    705  1.1.1.1.8.2  tls 		}
    706  1.1.1.1.8.2  tls 	}
    707  1.1.1.1.8.2  tls 	if (n_written) {
    708  1.1.1.1.8.2  tls 		evbuffer_drain(output, n_written);
    709  1.1.1.1.8.2  tls 		if (bev_ssl->underlying)
    710  1.1.1.1.8.2  tls 			BEV_RESET_GENERIC_WRITE_TIMEOUT(bev);
    711  1.1.1.1.8.2  tls 
    712  1.1.1.1.8.2  tls 		if (evbuffer_get_length(output) <= bev->wm_write.low)
    713  1.1.1.1.8.2  tls 			bufferevent_run_writecb_(bev);
    714  1.1.1.1.8.2  tls 	}
    715  1.1.1.1.8.2  tls 	return result;
    716  1.1.1.1.8.2  tls }
    717  1.1.1.1.8.2  tls 
    718  1.1.1.1.8.2  tls #define WRITE_FRAME 15000
    719  1.1.1.1.8.2  tls 
    720  1.1.1.1.8.2  tls #define READ_DEFAULT 4096
    721  1.1.1.1.8.2  tls 
    722  1.1.1.1.8.2  tls /* Try to figure out how many bytes to read; return 0 if we shouldn't be
    723  1.1.1.1.8.2  tls  * reading. */
    724  1.1.1.1.8.2  tls static int
    725  1.1.1.1.8.2  tls bytes_to_read(struct bufferevent_openssl *bev)
    726  1.1.1.1.8.2  tls {
    727  1.1.1.1.8.2  tls 	struct evbuffer *input = bev->bev.bev.input;
    728  1.1.1.1.8.2  tls 	struct event_watermark *wm = &bev->bev.bev.wm_read;
    729  1.1.1.1.8.2  tls 	int result = READ_DEFAULT;
    730  1.1.1.1.8.2  tls 	ev_ssize_t limit;
    731  1.1.1.1.8.2  tls 	/* XXX 99% of this is generic code that nearly all bufferevents will
    732  1.1.1.1.8.2  tls 	 * want. */
    733  1.1.1.1.8.2  tls 
    734  1.1.1.1.8.2  tls 	if (bev->write_blocked_on_read) {
    735  1.1.1.1.8.2  tls 		return 0;
    736  1.1.1.1.8.2  tls 	}
    737  1.1.1.1.8.2  tls 
    738  1.1.1.1.8.2  tls 	if (! (bev->bev.bev.enabled & EV_READ)) {
    739  1.1.1.1.8.2  tls 		return 0;
    740  1.1.1.1.8.2  tls 	}
    741  1.1.1.1.8.2  tls 
    742  1.1.1.1.8.2  tls 	if (bev->bev.read_suspended) {
    743  1.1.1.1.8.2  tls 		return 0;
    744  1.1.1.1.8.2  tls 	}
    745  1.1.1.1.8.2  tls 
    746  1.1.1.1.8.2  tls 	if (wm->high) {
    747  1.1.1.1.8.2  tls 		if (evbuffer_get_length(input) >= wm->high) {
    748  1.1.1.1.8.2  tls 			return 0;
    749  1.1.1.1.8.2  tls 		}
    750  1.1.1.1.8.2  tls 
    751  1.1.1.1.8.2  tls 		result = wm->high - evbuffer_get_length(input);
    752  1.1.1.1.8.2  tls 	} else {
    753  1.1.1.1.8.2  tls 		result = READ_DEFAULT;
    754  1.1.1.1.8.2  tls 	}
    755  1.1.1.1.8.2  tls 
    756  1.1.1.1.8.2  tls 	/* Respect the rate limit */
    757  1.1.1.1.8.2  tls 	limit = bufferevent_get_read_max_(&bev->bev);
    758  1.1.1.1.8.2  tls 	if (result > limit) {
    759  1.1.1.1.8.2  tls 		result = limit;
    760  1.1.1.1.8.2  tls 	}
    761  1.1.1.1.8.2  tls 
    762  1.1.1.1.8.2  tls 	return result;
    763  1.1.1.1.8.2  tls }
    764  1.1.1.1.8.2  tls 
    765  1.1.1.1.8.2  tls 
    766  1.1.1.1.8.2  tls /* Things look readable.  If write is blocked on read, write till it isn't.
    767  1.1.1.1.8.2  tls  * Read from the underlying buffer until we block or we hit our high-water
    768  1.1.1.1.8.2  tls  * mark.
    769  1.1.1.1.8.2  tls  */
    770  1.1.1.1.8.2  tls static void
    771  1.1.1.1.8.2  tls consider_reading(struct bufferevent_openssl *bev_ssl)
    772  1.1.1.1.8.2  tls {
    773  1.1.1.1.8.2  tls 	int r;
    774  1.1.1.1.8.2  tls 	int n_to_read;
    775  1.1.1.1.8.2  tls 	int all_result_flags = 0;
    776  1.1.1.1.8.2  tls 
    777  1.1.1.1.8.2  tls 	while (bev_ssl->write_blocked_on_read) {
    778  1.1.1.1.8.2  tls 		r = do_write(bev_ssl, WRITE_FRAME);
    779  1.1.1.1.8.2  tls 		if (r & (OP_BLOCKED|OP_ERR))
    780  1.1.1.1.8.2  tls 			break;
    781  1.1.1.1.8.2  tls 	}
    782  1.1.1.1.8.2  tls 	if (bev_ssl->write_blocked_on_read)
    783  1.1.1.1.8.2  tls 		return;
    784  1.1.1.1.8.2  tls 
    785  1.1.1.1.8.2  tls 	n_to_read = bytes_to_read(bev_ssl);
    786  1.1.1.1.8.2  tls 
    787  1.1.1.1.8.2  tls 	while (n_to_read) {
    788  1.1.1.1.8.2  tls 		r = do_read(bev_ssl, n_to_read);
    789  1.1.1.1.8.2  tls 		all_result_flags |= r;
    790  1.1.1.1.8.2  tls 
    791  1.1.1.1.8.2  tls 		if (r & (OP_BLOCKED|OP_ERR))
    792  1.1.1.1.8.2  tls 			break;
    793  1.1.1.1.8.2  tls 
    794  1.1.1.1.8.2  tls 		if (bev_ssl->bev.read_suspended)
    795  1.1.1.1.8.2  tls 			break;
    796  1.1.1.1.8.2  tls 
    797  1.1.1.1.8.2  tls 		/* Read all pending data.  This won't hit the network
    798  1.1.1.1.8.2  tls 		 * again, and will (most importantly) put us in a state
    799  1.1.1.1.8.2  tls 		 * where we don't need to read anything else until the
    800  1.1.1.1.8.2  tls 		 * socket is readable again.  It'll potentially make us
    801  1.1.1.1.8.2  tls 		 * overrun our read high-watermark (somewhat
    802  1.1.1.1.8.2  tls 		 * regrettable).  The damage to the rate-limit has
    803  1.1.1.1.8.2  tls 		 * already been done, since OpenSSL went and read a
    804  1.1.1.1.8.2  tls 		 * whole SSL record anyway. */
    805  1.1.1.1.8.2  tls 		n_to_read = SSL_pending(bev_ssl->ssl);
    806  1.1.1.1.8.2  tls 
    807  1.1.1.1.8.2  tls 		/* XXX This if statement is actually a bad bug, added to avoid
    808  1.1.1.1.8.2  tls 		 * XXX a worse bug.
    809  1.1.1.1.8.2  tls 		 *
    810  1.1.1.1.8.2  tls 		 * The bad bug: It can potentially cause resource unfairness
    811  1.1.1.1.8.2  tls 		 * by reading too much data from the underlying bufferevent;
    812  1.1.1.1.8.2  tls 		 * it can potentially cause read looping if the underlying
    813  1.1.1.1.8.2  tls 		 * bufferevent is a bufferevent_pair and deferred callbacks
    814  1.1.1.1.8.2  tls 		 * aren't used.
    815  1.1.1.1.8.2  tls 		 *
    816  1.1.1.1.8.2  tls 		 * The worse bug: If we didn't do this, then we would
    817  1.1.1.1.8.2  tls 		 * potentially not read any more from bev_ssl->underlying
    818  1.1.1.1.8.2  tls 		 * until more data arrived there, which could lead to us
    819  1.1.1.1.8.2  tls 		 * waiting forever.
    820  1.1.1.1.8.2  tls 		 */
    821  1.1.1.1.8.2  tls 		if (!n_to_read && bev_ssl->underlying)
    822  1.1.1.1.8.2  tls 			n_to_read = bytes_to_read(bev_ssl);
    823  1.1.1.1.8.2  tls 	}
    824  1.1.1.1.8.2  tls 
    825  1.1.1.1.8.2  tls 	if (all_result_flags & OP_MADE_PROGRESS) {
    826  1.1.1.1.8.2  tls 		struct bufferevent *bev = &bev_ssl->bev.bev;
    827  1.1.1.1.8.2  tls 		struct evbuffer *input = bev->input;
    828  1.1.1.1.8.2  tls 
    829  1.1.1.1.8.2  tls 		if (evbuffer_get_length(input) >= bev->wm_read.low) {
    830  1.1.1.1.8.2  tls 			bufferevent_run_readcb_(bev);
    831  1.1.1.1.8.2  tls 		}
    832  1.1.1.1.8.2  tls 	}
    833  1.1.1.1.8.2  tls 
    834  1.1.1.1.8.2  tls 	if (!bev_ssl->underlying) {
    835  1.1.1.1.8.2  tls 		/* Should be redundant, but let's avoid busy-looping */
    836  1.1.1.1.8.2  tls 		if (bev_ssl->bev.read_suspended ||
    837  1.1.1.1.8.2  tls 		    !(bev_ssl->bev.bev.enabled & EV_READ)) {
    838  1.1.1.1.8.2  tls 			event_del(&bev_ssl->bev.bev.ev_read);
    839  1.1.1.1.8.2  tls 		}
    840  1.1.1.1.8.2  tls 	}
    841  1.1.1.1.8.2  tls }
    842  1.1.1.1.8.2  tls 
    843  1.1.1.1.8.2  tls static void
    844  1.1.1.1.8.2  tls consider_writing(struct bufferevent_openssl *bev_ssl)
    845  1.1.1.1.8.2  tls {
    846  1.1.1.1.8.2  tls 	int r;
    847  1.1.1.1.8.2  tls 	struct evbuffer *output = bev_ssl->bev.bev.output;
    848  1.1.1.1.8.2  tls 	struct evbuffer *target = NULL;
    849  1.1.1.1.8.2  tls 	struct event_watermark *wm = NULL;
    850  1.1.1.1.8.2  tls 
    851  1.1.1.1.8.2  tls 	while (bev_ssl->read_blocked_on_write) {
    852  1.1.1.1.8.2  tls 		r = do_read(bev_ssl, 1024); /* XXXX 1024 is a hack */
    853  1.1.1.1.8.2  tls 		if (r & OP_MADE_PROGRESS) {
    854  1.1.1.1.8.2  tls 			struct bufferevent *bev = &bev_ssl->bev.bev;
    855  1.1.1.1.8.2  tls 			struct evbuffer *input = bev->input;
    856  1.1.1.1.8.2  tls 
    857  1.1.1.1.8.2  tls 			if (evbuffer_get_length(input) >= bev->wm_read.low) {
    858  1.1.1.1.8.2  tls 				bufferevent_run_readcb_(bev);
    859  1.1.1.1.8.2  tls 			}
    860  1.1.1.1.8.2  tls 		}
    861  1.1.1.1.8.2  tls 		if (r & (OP_ERR|OP_BLOCKED))
    862  1.1.1.1.8.2  tls 			break;
    863  1.1.1.1.8.2  tls 	}
    864  1.1.1.1.8.2  tls 	if (bev_ssl->read_blocked_on_write)
    865  1.1.1.1.8.2  tls 		return;
    866  1.1.1.1.8.2  tls 	if (bev_ssl->underlying) {
    867  1.1.1.1.8.2  tls 		target = bev_ssl->underlying->output;
    868  1.1.1.1.8.2  tls 		wm = &bev_ssl->underlying->wm_write;
    869  1.1.1.1.8.2  tls 	}
    870  1.1.1.1.8.2  tls 	while ((bev_ssl->bev.bev.enabled & EV_WRITE) &&
    871  1.1.1.1.8.2  tls 	    (! bev_ssl->bev.write_suspended) &&
    872  1.1.1.1.8.2  tls 	    evbuffer_get_length(output) &&
    873  1.1.1.1.8.2  tls 	    (!target || (! wm->high || evbuffer_get_length(target) < wm->high))) {
    874  1.1.1.1.8.2  tls 		int n_to_write;
    875  1.1.1.1.8.2  tls 		if (wm && wm->high)
    876  1.1.1.1.8.2  tls 			n_to_write = wm->high - evbuffer_get_length(target);
    877  1.1.1.1.8.2  tls 		else
    878  1.1.1.1.8.2  tls 			n_to_write = WRITE_FRAME;
    879  1.1.1.1.8.2  tls 		r = do_write(bev_ssl, n_to_write);
    880  1.1.1.1.8.2  tls 		if (r & (OP_BLOCKED|OP_ERR))
    881  1.1.1.1.8.2  tls 			break;
    882  1.1.1.1.8.2  tls 	}
    883  1.1.1.1.8.2  tls 
    884  1.1.1.1.8.2  tls 	if (!bev_ssl->underlying) {
    885  1.1.1.1.8.2  tls 		if (evbuffer_get_length(output) == 0) {
    886  1.1.1.1.8.2  tls 			event_del(&bev_ssl->bev.bev.ev_write);
    887  1.1.1.1.8.2  tls 		} else if (bev_ssl->bev.write_suspended ||
    888  1.1.1.1.8.2  tls 		    !(bev_ssl->bev.bev.enabled & EV_WRITE)) {
    889  1.1.1.1.8.2  tls 			/* Should be redundant, but let's avoid busy-looping */
    890  1.1.1.1.8.2  tls 			event_del(&bev_ssl->bev.bev.ev_write);
    891  1.1.1.1.8.2  tls 		}
    892  1.1.1.1.8.2  tls 	}
    893  1.1.1.1.8.2  tls }
    894  1.1.1.1.8.2  tls 
    895  1.1.1.1.8.2  tls static void
    896  1.1.1.1.8.2  tls be_openssl_readcb(struct bufferevent *bev_base, void *ctx)
    897  1.1.1.1.8.2  tls {
    898  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = ctx;
    899  1.1.1.1.8.2  tls 	consider_reading(bev_ssl);
    900  1.1.1.1.8.2  tls }
    901  1.1.1.1.8.2  tls 
    902  1.1.1.1.8.2  tls static void
    903  1.1.1.1.8.2  tls be_openssl_writecb(struct bufferevent *bev_base, void *ctx)
    904  1.1.1.1.8.2  tls {
    905  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = ctx;
    906  1.1.1.1.8.2  tls 	consider_writing(bev_ssl);
    907  1.1.1.1.8.2  tls }
    908  1.1.1.1.8.2  tls 
    909  1.1.1.1.8.2  tls static void
    910  1.1.1.1.8.2  tls be_openssl_eventcb(struct bufferevent *bev_base, short what, void *ctx)
    911  1.1.1.1.8.2  tls {
    912  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = ctx;
    913  1.1.1.1.8.2  tls 	int event = 0;
    914  1.1.1.1.8.2  tls 
    915  1.1.1.1.8.2  tls 	if (what & BEV_EVENT_EOF) {
    916  1.1.1.1.8.2  tls 		if (bev_ssl->allow_dirty_shutdown)
    917  1.1.1.1.8.2  tls 			event = BEV_EVENT_EOF;
    918  1.1.1.1.8.2  tls 		else
    919  1.1.1.1.8.2  tls 			event = BEV_EVENT_ERROR;
    920  1.1.1.1.8.2  tls 	} else if (what & BEV_EVENT_TIMEOUT) {
    921  1.1.1.1.8.2  tls 		/* We sure didn't set this.  Propagate it to the user. */
    922  1.1.1.1.8.2  tls 		event = what;
    923  1.1.1.1.8.2  tls 	} else if (what & BEV_EVENT_ERROR) {
    924  1.1.1.1.8.2  tls 		/* An error occurred on the connection.  Propagate it to the user. */
    925  1.1.1.1.8.2  tls 		event = what;
    926  1.1.1.1.8.2  tls 	} else if (what & BEV_EVENT_CONNECTED) {
    927  1.1.1.1.8.2  tls 		/* Ignore it.  We're saying SSL_connect() already, which will
    928  1.1.1.1.8.2  tls 		   eat it. */
    929  1.1.1.1.8.2  tls 	}
    930  1.1.1.1.8.2  tls 	if (event)
    931  1.1.1.1.8.2  tls 		bufferevent_run_eventcb_(&bev_ssl->bev.bev, event);
    932  1.1.1.1.8.2  tls }
    933  1.1.1.1.8.2  tls 
    934  1.1.1.1.8.2  tls static void
    935  1.1.1.1.8.2  tls be_openssl_readeventcb(evutil_socket_t fd, short what, void *ptr)
    936  1.1.1.1.8.2  tls {
    937  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = ptr;
    938  1.1.1.1.8.2  tls 	bufferevent_incref_and_lock_(&bev_ssl->bev.bev);
    939  1.1.1.1.8.2  tls 	if (what == EV_TIMEOUT) {
    940  1.1.1.1.8.2  tls 		bufferevent_run_eventcb_(&bev_ssl->bev.bev,
    941  1.1.1.1.8.2  tls 		    BEV_EVENT_TIMEOUT|BEV_EVENT_READING);
    942  1.1.1.1.8.2  tls 	} else {
    943  1.1.1.1.8.2  tls 		consider_reading(bev_ssl);
    944  1.1.1.1.8.2  tls 	}
    945  1.1.1.1.8.2  tls 	bufferevent_decref_and_unlock_(&bev_ssl->bev.bev);
    946  1.1.1.1.8.2  tls }
    947  1.1.1.1.8.2  tls 
    948  1.1.1.1.8.2  tls static void
    949  1.1.1.1.8.2  tls be_openssl_writeeventcb(evutil_socket_t fd, short what, void *ptr)
    950  1.1.1.1.8.2  tls {
    951  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = ptr;
    952  1.1.1.1.8.2  tls 	bufferevent_incref_and_lock_(&bev_ssl->bev.bev);
    953  1.1.1.1.8.2  tls 	if (what == EV_TIMEOUT) {
    954  1.1.1.1.8.2  tls 		bufferevent_run_eventcb_(&bev_ssl->bev.bev,
    955  1.1.1.1.8.2  tls 		    BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING);
    956  1.1.1.1.8.2  tls 	} else {
    957  1.1.1.1.8.2  tls 		consider_writing(bev_ssl);
    958  1.1.1.1.8.2  tls 	}
    959  1.1.1.1.8.2  tls 	bufferevent_decref_and_unlock_(&bev_ssl->bev.bev);
    960  1.1.1.1.8.2  tls }
    961  1.1.1.1.8.2  tls 
    962  1.1.1.1.8.2  tls static int
    963  1.1.1.1.8.2  tls set_open_callbacks(struct bufferevent_openssl *bev_ssl, evutil_socket_t fd)
    964  1.1.1.1.8.2  tls {
    965  1.1.1.1.8.2  tls 	if (bev_ssl->underlying) {
    966  1.1.1.1.8.2  tls 		bufferevent_setcb(bev_ssl->underlying,
    967  1.1.1.1.8.2  tls 		    be_openssl_readcb, be_openssl_writecb, be_openssl_eventcb,
    968  1.1.1.1.8.2  tls 		    bev_ssl);
    969  1.1.1.1.8.2  tls 		return 0;
    970  1.1.1.1.8.2  tls 	} else {
    971  1.1.1.1.8.2  tls 		struct bufferevent *bev = &bev_ssl->bev.bev;
    972  1.1.1.1.8.2  tls 		int rpending=0, wpending=0, r1=0, r2=0;
    973  1.1.1.1.8.2  tls 		if (fd < 0 && bev_ssl->fd_is_set)
    974  1.1.1.1.8.2  tls 			fd = event_get_fd(&bev->ev_read);
    975  1.1.1.1.8.2  tls 		if (bev_ssl->fd_is_set) {
    976  1.1.1.1.8.2  tls 			rpending = event_pending(&bev->ev_read, EV_READ, NULL);
    977  1.1.1.1.8.2  tls 			wpending = event_pending(&bev->ev_write, EV_WRITE, NULL);
    978  1.1.1.1.8.2  tls 			event_del(&bev->ev_read);
    979  1.1.1.1.8.2  tls 			event_del(&bev->ev_write);
    980  1.1.1.1.8.2  tls 		}
    981  1.1.1.1.8.2  tls 		event_assign(&bev->ev_read, bev->ev_base, fd,
    982  1.1.1.1.8.2  tls 		    EV_READ|EV_PERSIST, be_openssl_readeventcb, bev_ssl);
    983  1.1.1.1.8.2  tls 		event_assign(&bev->ev_write, bev->ev_base, fd,
    984  1.1.1.1.8.2  tls 		    EV_WRITE|EV_PERSIST, be_openssl_writeeventcb, bev_ssl);
    985  1.1.1.1.8.2  tls 		if (rpending)
    986  1.1.1.1.8.2  tls 			r1 = bufferevent_add_event_(&bev->ev_read, &bev->timeout_read);
    987  1.1.1.1.8.2  tls 		if (wpending)
    988  1.1.1.1.8.2  tls 			r2 = bufferevent_add_event_(&bev->ev_write, &bev->timeout_write);
    989  1.1.1.1.8.2  tls 		if (fd >= 0) {
    990  1.1.1.1.8.2  tls 			bev_ssl->fd_is_set = 1;
    991  1.1.1.1.8.2  tls 		}
    992  1.1.1.1.8.2  tls 		return (r1<0 || r2<0) ? -1 : 0;
    993  1.1.1.1.8.2  tls 	}
    994  1.1.1.1.8.2  tls }
    995  1.1.1.1.8.2  tls 
    996  1.1.1.1.8.2  tls static int
    997  1.1.1.1.8.2  tls do_handshake(struct bufferevent_openssl *bev_ssl)
    998  1.1.1.1.8.2  tls {
    999  1.1.1.1.8.2  tls 	int r;
   1000  1.1.1.1.8.2  tls 
   1001  1.1.1.1.8.2  tls 	switch (bev_ssl->state) {
   1002  1.1.1.1.8.2  tls 	default:
   1003  1.1.1.1.8.2  tls 	case BUFFEREVENT_SSL_OPEN:
   1004  1.1.1.1.8.2  tls 		EVUTIL_ASSERT(0);
   1005  1.1.1.1.8.2  tls 		return -1;
   1006  1.1.1.1.8.2  tls 	case BUFFEREVENT_SSL_CONNECTING:
   1007  1.1.1.1.8.2  tls 	case BUFFEREVENT_SSL_ACCEPTING:
   1008  1.1.1.1.8.2  tls 		r = SSL_do_handshake(bev_ssl->ssl);
   1009  1.1.1.1.8.2  tls 		break;
   1010  1.1.1.1.8.2  tls 	}
   1011  1.1.1.1.8.2  tls 	decrement_buckets(bev_ssl);
   1012  1.1.1.1.8.2  tls 
   1013  1.1.1.1.8.2  tls 	if (r==1) {
   1014  1.1.1.1.8.2  tls 		/* We're done! */
   1015  1.1.1.1.8.2  tls 		bev_ssl->state = BUFFEREVENT_SSL_OPEN;
   1016  1.1.1.1.8.2  tls 		set_open_callbacks(bev_ssl, -1); /* XXXX handle failure */
   1017  1.1.1.1.8.2  tls 		/* Call do_read and do_write as needed */
   1018  1.1.1.1.8.2  tls 		bufferevent_enable(&bev_ssl->bev.bev, bev_ssl->bev.bev.enabled);
   1019  1.1.1.1.8.2  tls 		bufferevent_run_eventcb_(&bev_ssl->bev.bev,
   1020  1.1.1.1.8.2  tls 		    BEV_EVENT_CONNECTED);
   1021  1.1.1.1.8.2  tls 		return 1;
   1022  1.1.1.1.8.2  tls 	} else {
   1023  1.1.1.1.8.2  tls 		int err = SSL_get_error(bev_ssl->ssl, r);
   1024  1.1.1.1.8.2  tls 		print_err(err);
   1025  1.1.1.1.8.2  tls 		switch (err) {
   1026  1.1.1.1.8.2  tls 		case SSL_ERROR_WANT_WRITE:
   1027  1.1.1.1.8.2  tls 			if (!bev_ssl->underlying) {
   1028  1.1.1.1.8.2  tls 				stop_reading(bev_ssl);
   1029  1.1.1.1.8.2  tls 				return start_writing(bev_ssl);
   1030  1.1.1.1.8.2  tls 			}
   1031  1.1.1.1.8.2  tls 			return 0;
   1032  1.1.1.1.8.2  tls 		case SSL_ERROR_WANT_READ:
   1033  1.1.1.1.8.2  tls 			if (!bev_ssl->underlying) {
   1034  1.1.1.1.8.2  tls 				stop_writing(bev_ssl);
   1035  1.1.1.1.8.2  tls 				return start_reading(bev_ssl);
   1036  1.1.1.1.8.2  tls 			}
   1037  1.1.1.1.8.2  tls 			return 0;
   1038  1.1.1.1.8.2  tls 		default:
   1039  1.1.1.1.8.2  tls 			conn_closed(bev_ssl, BEV_EVENT_READING, err, r);
   1040  1.1.1.1.8.2  tls 			return -1;
   1041  1.1.1.1.8.2  tls 		}
   1042  1.1.1.1.8.2  tls 	}
   1043  1.1.1.1.8.2  tls }
   1044  1.1.1.1.8.2  tls 
   1045  1.1.1.1.8.2  tls static void
   1046  1.1.1.1.8.2  tls be_openssl_handshakecb(struct bufferevent *bev_base, void *ctx)
   1047  1.1.1.1.8.2  tls {
   1048  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = ctx;
   1049  1.1.1.1.8.2  tls 	do_handshake(bev_ssl);/* XXX handle failure */
   1050  1.1.1.1.8.2  tls }
   1051  1.1.1.1.8.2  tls 
   1052  1.1.1.1.8.2  tls static void
   1053  1.1.1.1.8.2  tls be_openssl_handshakeeventcb(evutil_socket_t fd, short what, void *ptr)
   1054  1.1.1.1.8.2  tls {
   1055  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = ptr;
   1056  1.1.1.1.8.2  tls 
   1057  1.1.1.1.8.2  tls 	bufferevent_incref_and_lock_(&bev_ssl->bev.bev);
   1058  1.1.1.1.8.2  tls 	if (what & EV_TIMEOUT) {
   1059  1.1.1.1.8.2  tls 		bufferevent_run_eventcb_(&bev_ssl->bev.bev, BEV_EVENT_TIMEOUT);
   1060  1.1.1.1.8.2  tls 	} else
   1061  1.1.1.1.8.2  tls 		do_handshake(bev_ssl);/* XXX handle failure */
   1062  1.1.1.1.8.2  tls 	bufferevent_decref_and_unlock_(&bev_ssl->bev.bev);
   1063  1.1.1.1.8.2  tls }
   1064  1.1.1.1.8.2  tls 
   1065  1.1.1.1.8.2  tls static int
   1066  1.1.1.1.8.2  tls set_handshake_callbacks(struct bufferevent_openssl *bev_ssl, evutil_socket_t fd)
   1067  1.1.1.1.8.2  tls {
   1068  1.1.1.1.8.2  tls 	if (bev_ssl->underlying) {
   1069  1.1.1.1.8.2  tls 		bufferevent_setcb(bev_ssl->underlying,
   1070  1.1.1.1.8.2  tls 		    be_openssl_handshakecb, be_openssl_handshakecb,
   1071  1.1.1.1.8.2  tls 		    be_openssl_eventcb,
   1072  1.1.1.1.8.2  tls 		    bev_ssl);
   1073  1.1.1.1.8.2  tls 		return do_handshake(bev_ssl);
   1074  1.1.1.1.8.2  tls 	} else {
   1075  1.1.1.1.8.2  tls 		struct bufferevent *bev = &bev_ssl->bev.bev;
   1076  1.1.1.1.8.2  tls 		int r1=0, r2=0;
   1077  1.1.1.1.8.2  tls 		if (fd < 0 && bev_ssl->fd_is_set)
   1078  1.1.1.1.8.2  tls 			fd = event_get_fd(&bev->ev_read);
   1079  1.1.1.1.8.2  tls 		if (bev_ssl->fd_is_set) {
   1080  1.1.1.1.8.2  tls 			event_del(&bev->ev_read);
   1081  1.1.1.1.8.2  tls 			event_del(&bev->ev_write);
   1082  1.1.1.1.8.2  tls 		}
   1083  1.1.1.1.8.2  tls 		event_assign(&bev->ev_read, bev->ev_base, fd,
   1084  1.1.1.1.8.2  tls 		    EV_READ|EV_PERSIST, be_openssl_handshakeeventcb, bev_ssl);
   1085  1.1.1.1.8.2  tls 		event_assign(&bev->ev_write, bev->ev_base, fd,
   1086  1.1.1.1.8.2  tls 		    EV_WRITE|EV_PERSIST, be_openssl_handshakeeventcb, bev_ssl);
   1087  1.1.1.1.8.2  tls 		if (fd >= 0) {
   1088  1.1.1.1.8.2  tls 			r1 = bufferevent_add_event_(&bev->ev_read, &bev->timeout_read);
   1089  1.1.1.1.8.2  tls 			r2 = bufferevent_add_event_(&bev->ev_write, &bev->timeout_write);
   1090  1.1.1.1.8.2  tls 			bev_ssl->fd_is_set = 1;
   1091  1.1.1.1.8.2  tls 		}
   1092  1.1.1.1.8.2  tls 		return (r1<0 || r2<0) ? -1 : 0;
   1093  1.1.1.1.8.2  tls 	}
   1094  1.1.1.1.8.2  tls }
   1095  1.1.1.1.8.2  tls 
   1096  1.1.1.1.8.2  tls int
   1097  1.1.1.1.8.2  tls bufferevent_ssl_renegotiate(struct bufferevent *bev)
   1098  1.1.1.1.8.2  tls {
   1099  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = upcast(bev);
   1100  1.1.1.1.8.2  tls 	if (!bev_ssl)
   1101  1.1.1.1.8.2  tls 		return -1;
   1102  1.1.1.1.8.2  tls 	if (SSL_renegotiate(bev_ssl->ssl) < 0)
   1103  1.1.1.1.8.2  tls 		return -1;
   1104  1.1.1.1.8.2  tls 	bev_ssl->state = BUFFEREVENT_SSL_CONNECTING;
   1105  1.1.1.1.8.2  tls 	if (set_handshake_callbacks(bev_ssl, -1) < 0)
   1106  1.1.1.1.8.2  tls 		return -1;
   1107  1.1.1.1.8.2  tls 	if (!bev_ssl->underlying)
   1108  1.1.1.1.8.2  tls 		return do_handshake(bev_ssl);
   1109  1.1.1.1.8.2  tls 	return 0;
   1110  1.1.1.1.8.2  tls }
   1111  1.1.1.1.8.2  tls 
   1112  1.1.1.1.8.2  tls static void
   1113  1.1.1.1.8.2  tls be_openssl_outbuf_cb(struct evbuffer *buf,
   1114  1.1.1.1.8.2  tls     const struct evbuffer_cb_info *cbinfo, void *arg)
   1115  1.1.1.1.8.2  tls {
   1116  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = arg;
   1117  1.1.1.1.8.2  tls 	int r = 0;
   1118  1.1.1.1.8.2  tls 	/* XXX need to hold a reference here. */
   1119  1.1.1.1.8.2  tls 
   1120  1.1.1.1.8.2  tls 	if (cbinfo->n_added && bev_ssl->state == BUFFEREVENT_SSL_OPEN) {
   1121  1.1.1.1.8.2  tls 		if (cbinfo->orig_size == 0)
   1122  1.1.1.1.8.2  tls 			r = bufferevent_add_event_(&bev_ssl->bev.bev.ev_write,
   1123  1.1.1.1.8.2  tls 			    &bev_ssl->bev.bev.timeout_write);
   1124  1.1.1.1.8.2  tls 		consider_writing(bev_ssl);
   1125  1.1.1.1.8.2  tls 	}
   1126  1.1.1.1.8.2  tls 	/* XXX Handle r < 0 */
   1127  1.1.1.1.8.2  tls         (void)r;
   1128  1.1.1.1.8.2  tls }
   1129  1.1.1.1.8.2  tls 
   1130  1.1.1.1.8.2  tls 
   1131  1.1.1.1.8.2  tls static int
   1132  1.1.1.1.8.2  tls be_openssl_enable(struct bufferevent *bev, short events)
   1133  1.1.1.1.8.2  tls {
   1134  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = upcast(bev);
   1135  1.1.1.1.8.2  tls 	int r1 = 0, r2 = 0;
   1136  1.1.1.1.8.2  tls 
   1137  1.1.1.1.8.2  tls 	if (bev_ssl->state != BUFFEREVENT_SSL_OPEN)
   1138  1.1.1.1.8.2  tls 		return 0;
   1139  1.1.1.1.8.2  tls 
   1140  1.1.1.1.8.2  tls 	if (events & EV_READ)
   1141  1.1.1.1.8.2  tls 		r1 = start_reading(bev_ssl);
   1142  1.1.1.1.8.2  tls 	if (events & EV_WRITE)
   1143  1.1.1.1.8.2  tls 		r2 = start_writing(bev_ssl);
   1144  1.1.1.1.8.2  tls 
   1145  1.1.1.1.8.2  tls 	if (bev_ssl->underlying) {
   1146  1.1.1.1.8.2  tls 		if (events & EV_READ)
   1147  1.1.1.1.8.2  tls 			BEV_RESET_GENERIC_READ_TIMEOUT(bev);
   1148  1.1.1.1.8.2  tls 		if (events & EV_WRITE)
   1149  1.1.1.1.8.2  tls 			BEV_RESET_GENERIC_WRITE_TIMEOUT(bev);
   1150  1.1.1.1.8.2  tls 
   1151  1.1.1.1.8.2  tls 		if (events & EV_READ)
   1152  1.1.1.1.8.2  tls 			consider_reading(bev_ssl);
   1153  1.1.1.1.8.2  tls 		if (events & EV_WRITE)
   1154  1.1.1.1.8.2  tls 			consider_writing(bev_ssl);
   1155  1.1.1.1.8.2  tls 	}
   1156  1.1.1.1.8.2  tls 	return (r1 < 0 || r2 < 0) ? -1 : 0;
   1157  1.1.1.1.8.2  tls }
   1158  1.1.1.1.8.2  tls 
   1159  1.1.1.1.8.2  tls static int
   1160  1.1.1.1.8.2  tls be_openssl_disable(struct bufferevent *bev, short events)
   1161  1.1.1.1.8.2  tls {
   1162  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = upcast(bev);
   1163  1.1.1.1.8.2  tls 	if (bev_ssl->state != BUFFEREVENT_SSL_OPEN)
   1164  1.1.1.1.8.2  tls 		return 0;
   1165  1.1.1.1.8.2  tls 
   1166  1.1.1.1.8.2  tls 	if (events & EV_READ)
   1167  1.1.1.1.8.2  tls 		stop_reading(bev_ssl);
   1168  1.1.1.1.8.2  tls 	if (events & EV_WRITE)
   1169  1.1.1.1.8.2  tls 		stop_writing(bev_ssl);
   1170  1.1.1.1.8.2  tls 
   1171  1.1.1.1.8.2  tls 	if (bev_ssl->underlying) {
   1172  1.1.1.1.8.2  tls 		if (events & EV_READ)
   1173  1.1.1.1.8.2  tls 			BEV_DEL_GENERIC_READ_TIMEOUT(bev);
   1174  1.1.1.1.8.2  tls 		if (events & EV_WRITE)
   1175  1.1.1.1.8.2  tls 			BEV_DEL_GENERIC_WRITE_TIMEOUT(bev);
   1176  1.1.1.1.8.2  tls 	}
   1177  1.1.1.1.8.2  tls 	return 0;
   1178  1.1.1.1.8.2  tls }
   1179  1.1.1.1.8.2  tls 
   1180  1.1.1.1.8.2  tls static void
   1181  1.1.1.1.8.2  tls be_openssl_destruct(struct bufferevent *bev)
   1182  1.1.1.1.8.2  tls {
   1183  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = upcast(bev);
   1184  1.1.1.1.8.2  tls 
   1185  1.1.1.1.8.2  tls 	if (bev_ssl->underlying) {
   1186  1.1.1.1.8.2  tls 		bufferevent_del_generic_timeout_cbs_(bev);
   1187  1.1.1.1.8.2  tls 	} else {
   1188  1.1.1.1.8.2  tls 		event_del(&bev->ev_read);
   1189  1.1.1.1.8.2  tls 		event_del(&bev->ev_write);
   1190  1.1.1.1.8.2  tls 	}
   1191  1.1.1.1.8.2  tls 
   1192  1.1.1.1.8.2  tls 	if (bev_ssl->bev.options & BEV_OPT_CLOSE_ON_FREE) {
   1193  1.1.1.1.8.2  tls 		if (bev_ssl->underlying) {
   1194  1.1.1.1.8.2  tls 			if (BEV_UPCAST(bev_ssl->underlying)->refcnt < 2) {
   1195  1.1.1.1.8.2  tls 				event_warnx("BEV_OPT_CLOSE_ON_FREE set on an "
   1196  1.1.1.1.8.2  tls 				    "bufferevent with too few references");
   1197  1.1.1.1.8.2  tls 			} else {
   1198  1.1.1.1.8.2  tls 				bufferevent_free(bev_ssl->underlying);
   1199  1.1.1.1.8.2  tls 				bev_ssl->underlying = NULL;
   1200  1.1.1.1.8.2  tls 			}
   1201  1.1.1.1.8.2  tls 		} else {
   1202  1.1.1.1.8.2  tls 			evutil_socket_t fd = -1;
   1203  1.1.1.1.8.2  tls 			BIO *bio = SSL_get_wbio(bev_ssl->ssl);
   1204  1.1.1.1.8.2  tls 			if (bio)
   1205  1.1.1.1.8.2  tls 				fd = BIO_get_fd(bio, NULL);
   1206  1.1.1.1.8.2  tls 			if (fd >= 0)
   1207  1.1.1.1.8.2  tls 				evutil_closesocket(fd);
   1208  1.1.1.1.8.2  tls 		}
   1209  1.1.1.1.8.2  tls 		SSL_free(bev_ssl->ssl);
   1210  1.1.1.1.8.2  tls 	} else {
   1211  1.1.1.1.8.2  tls 		if (bev_ssl->underlying) {
   1212  1.1.1.1.8.2  tls 			if (bev_ssl->underlying->errorcb == be_openssl_eventcb)
   1213  1.1.1.1.8.2  tls 				bufferevent_setcb(bev_ssl->underlying,
   1214  1.1.1.1.8.2  tls 				    NULL,NULL,NULL,NULL);
   1215  1.1.1.1.8.2  tls 			bufferevent_unsuspend_read_(bev_ssl->underlying,
   1216  1.1.1.1.8.2  tls 			    BEV_SUSPEND_FILT_READ);
   1217  1.1.1.1.8.2  tls 		}
   1218  1.1.1.1.8.2  tls 	}
   1219  1.1.1.1.8.2  tls }
   1220  1.1.1.1.8.2  tls 
   1221  1.1.1.1.8.2  tls static int
   1222  1.1.1.1.8.2  tls be_openssl_adj_timeouts(struct bufferevent *bev)
   1223  1.1.1.1.8.2  tls {
   1224  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = upcast(bev);
   1225  1.1.1.1.8.2  tls 
   1226  1.1.1.1.8.2  tls 	if (bev_ssl->underlying) {
   1227  1.1.1.1.8.2  tls 		return bufferevent_generic_adj_timeouts_(bev);
   1228  1.1.1.1.8.2  tls 	} else {
   1229  1.1.1.1.8.2  tls 		int r1=0, r2=0;
   1230  1.1.1.1.8.2  tls 		if (event_pending(&bev->ev_read, EV_READ, NULL)) {
   1231  1.1.1.1.8.2  tls 			if (evutil_timerisset(&bev->timeout_read)) {
   1232  1.1.1.1.8.2  tls 				r1 = bufferevent_add_event_(&bev->ev_read, &bev->timeout_read);
   1233  1.1.1.1.8.2  tls 			} else {
   1234  1.1.1.1.8.2  tls 				event_remove_timer(&bev->ev_read);
   1235  1.1.1.1.8.2  tls 			}
   1236  1.1.1.1.8.2  tls 		}
   1237  1.1.1.1.8.2  tls 		if (event_pending(&bev->ev_write, EV_WRITE, NULL)) {
   1238  1.1.1.1.8.2  tls 			if (evutil_timerisset(&bev->timeout_write)) {
   1239  1.1.1.1.8.2  tls 				r2 = bufferevent_add_event_(&bev->ev_write, &bev->timeout_write);
   1240  1.1.1.1.8.2  tls 			} else {
   1241  1.1.1.1.8.2  tls 				event_remove_timer(&bev->ev_write);
   1242  1.1.1.1.8.2  tls 			}
   1243  1.1.1.1.8.2  tls 		}
   1244  1.1.1.1.8.2  tls 
   1245  1.1.1.1.8.2  tls 		return (r1<0 || r2<0) ? -1 : 0;
   1246  1.1.1.1.8.2  tls 	}
   1247  1.1.1.1.8.2  tls }
   1248  1.1.1.1.8.2  tls 
   1249  1.1.1.1.8.2  tls static int
   1250  1.1.1.1.8.2  tls be_openssl_flush(struct bufferevent *bufev,
   1251  1.1.1.1.8.2  tls     short iotype, enum bufferevent_flush_mode mode)
   1252  1.1.1.1.8.2  tls {
   1253  1.1.1.1.8.2  tls 	/* XXXX Implement this. */
   1254  1.1.1.1.8.2  tls 	return 0;
   1255  1.1.1.1.8.2  tls }
   1256  1.1.1.1.8.2  tls 
   1257  1.1.1.1.8.2  tls static int
   1258  1.1.1.1.8.2  tls be_openssl_ctrl(struct bufferevent *bev,
   1259  1.1.1.1.8.2  tls     enum bufferevent_ctrl_op op, union bufferevent_ctrl_data *data)
   1260  1.1.1.1.8.2  tls {
   1261  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = upcast(bev);
   1262  1.1.1.1.8.2  tls 	switch (op) {
   1263  1.1.1.1.8.2  tls 	case BEV_CTRL_SET_FD:
   1264  1.1.1.1.8.2  tls 		if (bev_ssl->underlying)
   1265  1.1.1.1.8.2  tls 			return -1;
   1266  1.1.1.1.8.2  tls 		{
   1267  1.1.1.1.8.2  tls 			BIO *bio;
   1268  1.1.1.1.8.2  tls 			bio = BIO_new_socket(data->fd, 0);
   1269  1.1.1.1.8.2  tls 			SSL_set_bio(bev_ssl->ssl, bio, bio);
   1270  1.1.1.1.8.2  tls 			bev_ssl->fd_is_set = 1;
   1271  1.1.1.1.8.2  tls 		}
   1272  1.1.1.1.8.2  tls 		if (bev_ssl->state == BUFFEREVENT_SSL_OPEN)
   1273  1.1.1.1.8.2  tls 			return set_open_callbacks(bev_ssl, data->fd);
   1274  1.1.1.1.8.2  tls 		else {
   1275  1.1.1.1.8.2  tls 			return set_handshake_callbacks(bev_ssl, data->fd);
   1276  1.1.1.1.8.2  tls 		}
   1277  1.1.1.1.8.2  tls 	case BEV_CTRL_GET_FD:
   1278  1.1.1.1.8.2  tls 		if (bev_ssl->underlying)
   1279  1.1.1.1.8.2  tls 			return -1;
   1280  1.1.1.1.8.2  tls 		if (!bev_ssl->fd_is_set)
   1281  1.1.1.1.8.2  tls 			return -1;
   1282  1.1.1.1.8.2  tls 		data->fd = event_get_fd(&bev->ev_read);
   1283  1.1.1.1.8.2  tls 		return 0;
   1284  1.1.1.1.8.2  tls 	case BEV_CTRL_GET_UNDERLYING:
   1285  1.1.1.1.8.2  tls 		if (!bev_ssl->underlying)
   1286  1.1.1.1.8.2  tls 			return -1;
   1287  1.1.1.1.8.2  tls 		data->ptr = bev_ssl->underlying;
   1288  1.1.1.1.8.2  tls 		return 0;
   1289  1.1.1.1.8.2  tls 	case BEV_CTRL_CANCEL_ALL:
   1290  1.1.1.1.8.2  tls 	default:
   1291  1.1.1.1.8.2  tls 		return -1;
   1292  1.1.1.1.8.2  tls 	}
   1293  1.1.1.1.8.2  tls }
   1294  1.1.1.1.8.2  tls 
   1295  1.1.1.1.8.2  tls SSL *
   1296  1.1.1.1.8.2  tls bufferevent_openssl_get_ssl(struct bufferevent *bufev)
   1297  1.1.1.1.8.2  tls {
   1298  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = upcast(bufev);
   1299  1.1.1.1.8.2  tls 	if (!bev_ssl)
   1300  1.1.1.1.8.2  tls 		return NULL;
   1301  1.1.1.1.8.2  tls 	return bev_ssl->ssl;
   1302  1.1.1.1.8.2  tls }
   1303  1.1.1.1.8.2  tls 
   1304  1.1.1.1.8.2  tls static struct bufferevent *
   1305  1.1.1.1.8.2  tls bufferevent_openssl_new_impl(struct event_base *base,
   1306  1.1.1.1.8.2  tls     struct bufferevent *underlying,
   1307  1.1.1.1.8.2  tls     evutil_socket_t fd,
   1308  1.1.1.1.8.2  tls     SSL *ssl,
   1309  1.1.1.1.8.2  tls     enum bufferevent_ssl_state state,
   1310  1.1.1.1.8.2  tls     int options)
   1311  1.1.1.1.8.2  tls {
   1312  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl = NULL;
   1313  1.1.1.1.8.2  tls 	struct bufferevent_private *bev_p = NULL;
   1314  1.1.1.1.8.2  tls 	int tmp_options = options & ~BEV_OPT_THREADSAFE;
   1315  1.1.1.1.8.2  tls 
   1316  1.1.1.1.8.2  tls 	if (underlying != NULL && fd >= 0)
   1317  1.1.1.1.8.2  tls 		return NULL; /* Only one can be set. */
   1318  1.1.1.1.8.2  tls 
   1319  1.1.1.1.8.2  tls 	if (!(bev_ssl = mm_calloc(1, sizeof(struct bufferevent_openssl))))
   1320  1.1.1.1.8.2  tls 		goto err;
   1321  1.1.1.1.8.2  tls 
   1322  1.1.1.1.8.2  tls 	bev_p = &bev_ssl->bev;
   1323  1.1.1.1.8.2  tls 
   1324  1.1.1.1.8.2  tls 	if (bufferevent_init_common_(bev_p, base,
   1325  1.1.1.1.8.2  tls 		&bufferevent_ops_openssl, tmp_options) < 0)
   1326  1.1.1.1.8.2  tls 		goto err;
   1327  1.1.1.1.8.2  tls 
   1328  1.1.1.1.8.2  tls 	/* Don't explode if we decide to realloc a chunk we're writing from in
   1329  1.1.1.1.8.2  tls 	 * the output buffer. */
   1330  1.1.1.1.8.2  tls 	SSL_set_mode(ssl, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
   1331  1.1.1.1.8.2  tls 
   1332  1.1.1.1.8.2  tls 	bev_ssl->underlying = underlying;
   1333  1.1.1.1.8.2  tls 	bev_ssl->ssl = ssl;
   1334  1.1.1.1.8.2  tls 
   1335  1.1.1.1.8.2  tls 	bev_ssl->outbuf_cb = evbuffer_add_cb(bev_p->bev.output,
   1336  1.1.1.1.8.2  tls 	    be_openssl_outbuf_cb, bev_ssl);
   1337  1.1.1.1.8.2  tls 
   1338  1.1.1.1.8.2  tls 	if (options & BEV_OPT_THREADSAFE)
   1339  1.1.1.1.8.2  tls 		bufferevent_enable_locking_(&bev_ssl->bev.bev, NULL);
   1340  1.1.1.1.8.2  tls 
   1341  1.1.1.1.8.2  tls 	if (underlying) {
   1342  1.1.1.1.8.2  tls 		bufferevent_init_generic_timeout_cbs_(&bev_ssl->bev.bev);
   1343  1.1.1.1.8.2  tls 		bufferevent_incref_(underlying);
   1344  1.1.1.1.8.2  tls 	}
   1345  1.1.1.1.8.2  tls 
   1346  1.1.1.1.8.2  tls 	bev_ssl->state = state;
   1347  1.1.1.1.8.2  tls 	bev_ssl->last_write = -1;
   1348  1.1.1.1.8.2  tls 
   1349  1.1.1.1.8.2  tls 	init_bio_counts(bev_ssl);
   1350  1.1.1.1.8.2  tls 
   1351  1.1.1.1.8.2  tls 	switch (state) {
   1352  1.1.1.1.8.2  tls 	case BUFFEREVENT_SSL_ACCEPTING:
   1353  1.1.1.1.8.2  tls 		SSL_set_accept_state(bev_ssl->ssl);
   1354  1.1.1.1.8.2  tls 		if (set_handshake_callbacks(bev_ssl, fd) < 0)
   1355  1.1.1.1.8.2  tls 			goto err;
   1356  1.1.1.1.8.2  tls 		break;
   1357  1.1.1.1.8.2  tls 	case BUFFEREVENT_SSL_CONNECTING:
   1358  1.1.1.1.8.2  tls 		SSL_set_connect_state(bev_ssl->ssl);
   1359  1.1.1.1.8.2  tls 		if (set_handshake_callbacks(bev_ssl, fd) < 0)
   1360  1.1.1.1.8.2  tls 			goto err;
   1361  1.1.1.1.8.2  tls 		break;
   1362  1.1.1.1.8.2  tls 	case BUFFEREVENT_SSL_OPEN:
   1363  1.1.1.1.8.2  tls 		if (set_open_callbacks(bev_ssl, fd) < 0)
   1364  1.1.1.1.8.2  tls 			goto err;
   1365  1.1.1.1.8.2  tls 		break;
   1366  1.1.1.1.8.2  tls 	default:
   1367  1.1.1.1.8.2  tls 		goto err;
   1368  1.1.1.1.8.2  tls 	}
   1369  1.1.1.1.8.2  tls 
   1370  1.1.1.1.8.2  tls 	if (underlying) {
   1371  1.1.1.1.8.2  tls 		bufferevent_setwatermark(underlying, EV_READ, 0, 0);
   1372  1.1.1.1.8.2  tls 		bufferevent_enable(underlying, EV_READ|EV_WRITE);
   1373  1.1.1.1.8.2  tls 		if (state == BUFFEREVENT_SSL_OPEN)
   1374  1.1.1.1.8.2  tls 			bufferevent_suspend_read_(underlying,
   1375  1.1.1.1.8.2  tls 			    BEV_SUSPEND_FILT_READ);
   1376  1.1.1.1.8.2  tls 	} else {
   1377  1.1.1.1.8.2  tls 		bev_ssl->bev.bev.enabled = EV_READ|EV_WRITE;
   1378  1.1.1.1.8.2  tls 		if (bev_ssl->fd_is_set) {
   1379  1.1.1.1.8.2  tls 			if (state != BUFFEREVENT_SSL_OPEN)
   1380  1.1.1.1.8.2  tls 				if (event_add(&bev_ssl->bev.bev.ev_read, NULL) < 0)
   1381  1.1.1.1.8.2  tls 					goto err;
   1382  1.1.1.1.8.2  tls 			if (event_add(&bev_ssl->bev.bev.ev_write, NULL) < 0)
   1383  1.1.1.1.8.2  tls 				goto err;
   1384  1.1.1.1.8.2  tls 		}
   1385  1.1.1.1.8.2  tls 	}
   1386  1.1.1.1.8.2  tls 
   1387  1.1.1.1.8.2  tls 	return &bev_ssl->bev.bev;
   1388  1.1.1.1.8.2  tls err:
   1389  1.1.1.1.8.2  tls 	if (bev_ssl)
   1390  1.1.1.1.8.2  tls 		bufferevent_free(&bev_ssl->bev.bev);
   1391  1.1.1.1.8.2  tls 	return NULL;
   1392  1.1.1.1.8.2  tls }
   1393  1.1.1.1.8.2  tls 
   1394  1.1.1.1.8.2  tls struct bufferevent *
   1395  1.1.1.1.8.2  tls bufferevent_openssl_filter_new(struct event_base *base,
   1396  1.1.1.1.8.2  tls     struct bufferevent *underlying,
   1397  1.1.1.1.8.2  tls     SSL *ssl,
   1398  1.1.1.1.8.2  tls     enum bufferevent_ssl_state state,
   1399  1.1.1.1.8.2  tls     int options)
   1400  1.1.1.1.8.2  tls {
   1401  1.1.1.1.8.2  tls 	/* We don't tell the BIO to close the bufferevent; we do it ourselves
   1402  1.1.1.1.8.2  tls 	 * on be_openssl_destruct */
   1403  1.1.1.1.8.2  tls 	int close_flag = 0; /* options & BEV_OPT_CLOSE_ON_FREE; */
   1404  1.1.1.1.8.2  tls 	BIO *bio;
   1405  1.1.1.1.8.2  tls 	if (!underlying)
   1406  1.1.1.1.8.2  tls 		return NULL;
   1407  1.1.1.1.8.2  tls 	if (!(bio = BIO_new_bufferevent(underlying, close_flag)))
   1408  1.1.1.1.8.2  tls 		return NULL;
   1409  1.1.1.1.8.2  tls 
   1410  1.1.1.1.8.2  tls 	SSL_set_bio(ssl, bio, bio);
   1411  1.1.1.1.8.2  tls 
   1412  1.1.1.1.8.2  tls 	return bufferevent_openssl_new_impl(
   1413  1.1.1.1.8.2  tls 		base, underlying, -1, ssl, state, options);
   1414  1.1.1.1.8.2  tls }
   1415  1.1.1.1.8.2  tls 
   1416  1.1.1.1.8.2  tls struct bufferevent *
   1417  1.1.1.1.8.2  tls bufferevent_openssl_socket_new(struct event_base *base,
   1418  1.1.1.1.8.2  tls     evutil_socket_t fd,
   1419  1.1.1.1.8.2  tls     SSL *ssl,
   1420  1.1.1.1.8.2  tls     enum bufferevent_ssl_state state,
   1421  1.1.1.1.8.2  tls     int options)
   1422  1.1.1.1.8.2  tls {
   1423  1.1.1.1.8.2  tls 	/* Does the SSL already have an fd? */
   1424  1.1.1.1.8.2  tls 	BIO *bio = SSL_get_wbio(ssl);
   1425  1.1.1.1.8.2  tls 	long have_fd = -1;
   1426  1.1.1.1.8.2  tls 
   1427  1.1.1.1.8.2  tls 	if (bio)
   1428  1.1.1.1.8.2  tls 		have_fd = BIO_get_fd(bio, NULL);
   1429  1.1.1.1.8.2  tls 
   1430  1.1.1.1.8.2  tls 	if (have_fd >= 0) {
   1431  1.1.1.1.8.2  tls 		/* The SSL is already configured with an fd. */
   1432  1.1.1.1.8.2  tls 		if (fd < 0) {
   1433  1.1.1.1.8.2  tls 			/* We should learn the fd from the SSL. */
   1434  1.1.1.1.8.2  tls 			fd = (evutil_socket_t) have_fd;
   1435  1.1.1.1.8.2  tls 		} else if (have_fd == (long)fd) {
   1436  1.1.1.1.8.2  tls 			/* We already know the fd from the SSL; do nothing */
   1437  1.1.1.1.8.2  tls 		} else {
   1438  1.1.1.1.8.2  tls 			/* We specified an fd different from that of the SSL.
   1439  1.1.1.1.8.2  tls 			   This is probably an error on our part.  Fail. */
   1440  1.1.1.1.8.2  tls 			return NULL;
   1441  1.1.1.1.8.2  tls 		}
   1442  1.1.1.1.8.2  tls 		(void) BIO_set_close(bio, 0);
   1443  1.1.1.1.8.2  tls 	} else {
   1444  1.1.1.1.8.2  tls 		/* The SSL isn't configured with a BIO with an fd. */
   1445  1.1.1.1.8.2  tls 		if (fd >= 0) {
   1446  1.1.1.1.8.2  tls 			/* ... and we have an fd we want to use. */
   1447  1.1.1.1.8.2  tls 			bio = BIO_new_socket(fd, 0);
   1448  1.1.1.1.8.2  tls 			SSL_set_bio(ssl, bio, bio);
   1449  1.1.1.1.8.2  tls 		} else {
   1450  1.1.1.1.8.2  tls 			/* Leave the fd unset. */
   1451  1.1.1.1.8.2  tls 		}
   1452  1.1.1.1.8.2  tls 	}
   1453  1.1.1.1.8.2  tls 
   1454  1.1.1.1.8.2  tls 	return bufferevent_openssl_new_impl(
   1455  1.1.1.1.8.2  tls 		base, NULL, fd, ssl, state, options);
   1456  1.1.1.1.8.2  tls }
   1457  1.1.1.1.8.2  tls 
   1458  1.1.1.1.8.2  tls int
   1459  1.1.1.1.8.2  tls bufferevent_openssl_get_allow_dirty_shutdown(struct bufferevent *bev)
   1460  1.1.1.1.8.2  tls {
   1461  1.1.1.1.8.2  tls 	int allow_dirty_shutdown = -1;
   1462  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl;
   1463  1.1.1.1.8.2  tls 	BEV_LOCK(bev);
   1464  1.1.1.1.8.2  tls 	bev_ssl = upcast(bev);
   1465  1.1.1.1.8.2  tls 	if (bev_ssl)
   1466  1.1.1.1.8.2  tls 		allow_dirty_shutdown = bev_ssl->allow_dirty_shutdown;
   1467  1.1.1.1.8.2  tls 	BEV_UNLOCK(bev);
   1468  1.1.1.1.8.2  tls 	return allow_dirty_shutdown;
   1469  1.1.1.1.8.2  tls }
   1470  1.1.1.1.8.2  tls 
   1471  1.1.1.1.8.2  tls void
   1472  1.1.1.1.8.2  tls bufferevent_openssl_set_allow_dirty_shutdown(struct bufferevent *bev,
   1473  1.1.1.1.8.2  tls     int allow_dirty_shutdown)
   1474  1.1.1.1.8.2  tls {
   1475  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl;
   1476  1.1.1.1.8.2  tls 	BEV_LOCK(bev);
   1477  1.1.1.1.8.2  tls 	bev_ssl = upcast(bev);
   1478  1.1.1.1.8.2  tls 	if (bev_ssl)
   1479  1.1.1.1.8.2  tls 		bev_ssl->allow_dirty_shutdown = !!allow_dirty_shutdown;
   1480  1.1.1.1.8.2  tls 	BEV_UNLOCK(bev);
   1481  1.1.1.1.8.2  tls }
   1482  1.1.1.1.8.2  tls 
   1483  1.1.1.1.8.2  tls unsigned long
   1484  1.1.1.1.8.2  tls bufferevent_get_openssl_error(struct bufferevent *bev)
   1485  1.1.1.1.8.2  tls {
   1486  1.1.1.1.8.2  tls 	unsigned long err = 0;
   1487  1.1.1.1.8.2  tls 	struct bufferevent_openssl *bev_ssl;
   1488  1.1.1.1.8.2  tls 	BEV_LOCK(bev);
   1489  1.1.1.1.8.2  tls 	bev_ssl = upcast(bev);
   1490  1.1.1.1.8.2  tls 	if (bev_ssl && bev_ssl->n_errors) {
   1491  1.1.1.1.8.2  tls 		err = bev_ssl->errors[--bev_ssl->n_errors];
   1492  1.1.1.1.8.2  tls 	}
   1493  1.1.1.1.8.2  tls 	BEV_UNLOCK(bev);
   1494  1.1.1.1.8.2  tls 	return err;
   1495  1.1.1.1.8.2  tls }
   1496