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