2 * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/types.h>
29 #include "event2/event-config.h"
31 #ifdef _EVENT_HAVE_SYS_TIME_H
39 #ifdef _EVENT_HAVE_STDARG_H
42 #ifdef _EVENT_HAVE_UNISTD_H
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"
56 #include "mm-internal.h"
57 #include "bufferevent-internal.h"
58 #include "log-internal.h"
60 #include <openssl/bio.h>
61 #include <openssl/ssl.h>
62 #include <openssl/err.h>
65 * Define an OpenSSL bio that targets a bufferevent.
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
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 -------------------- */
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
88 printf("Error was %d\n", val
);
90 while ((err
= ERR_get_error())) {
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
);
95 printf("%s in %s %s\n", msg
, lib
, func
);
99 #define print_err(v) ((void)0)
102 /* Called to initialize a new BIO */
104 bio_bufferevent_new(BIO
*b
)
108 b
->ptr
= NULL
; /* We'll be putting the bufferevent in this field.*/
113 /* Called to uninitialize the BIO. */
115 bio_bufferevent_free(BIO
*b
)
120 if (b
->init
&& b
->ptr
)
121 bufferevent_free(b
->ptr
);
129 /* Called to extract data from the BIO. */
131 bio_bufferevent_read(BIO
*b
, char *out
, int outlen
)
134 struct evbuffer
*input
;
136 BIO_clear_retry_flags(b
);
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
);
149 r
= evbuffer_remove(input
, out
, outlen
);
155 /* Called to write data info the BIO */
157 bio_bufferevent_write(BIO
*b
, const char *in
, int inlen
)
159 struct bufferevent
*bufev
= b
->ptr
;
160 struct evbuffer
*output
;
163 BIO_clear_retry_flags(b
);
168 output
= bufferevent_get_output(bufev
);
169 outlen
= evbuffer_get_length(output
);
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
);
179 inlen
= bufev
->wm_write
.high
- outlen
;
182 EVUTIL_ASSERT(inlen
> 0);
183 evbuffer_add(output
, in
, inlen
);
187 /* Called to handle various requests */
189 bio_bufferevent_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
191 struct bufferevent
*bufev
= b
->ptr
;
195 case BIO_CTRL_GET_CLOSE
:
198 case BIO_CTRL_SET_CLOSE
:
199 b
->shutdown
= (int)num
;
201 case BIO_CTRL_PENDING
:
202 ret
= evbuffer_get_length(bufferevent_get_input(bufev
)) != 0;
204 case BIO_CTRL_WPENDING
:
205 ret
= evbuffer_get_length(bufferevent_get_output(bufev
)) != 0;
207 /* XXXX These two are given a special-case treatment because
208 * of cargo-cultism. I should come up with a better reason. */
220 /* Called to write a string to the BIO */
222 bio_bufferevent_puts(BIO
*b
, const char *s
)
224 return bio_bufferevent_write(b
, s
, strlen(s
));
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
,
236 bio_bufferevent_free
,
237 NULL
/* callback_ctrl */,
240 /* Return the method table for the bufferevents BIO */
242 BIO_s_bufferevent(void)
244 return &methods_bufferevent
;
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. */
250 BIO_new_bufferevent(struct bufferevent
*bufferevent
, int close_flag
)
255 if (!(result
= BIO_new(BIO_s_bufferevent())))
258 result
->ptr
= bufferevent
;
259 result
->shutdown
= close_flag
? 1 : 0;
263 /* --------------------
264 Now, here's the OpenSSL-based implementation of bufferevent.
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
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 -------------------- */
276 struct bio_data_counts
{
277 unsigned long n_written
;
278 unsigned long n_read
;
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.
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. */
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
;
298 /* A count of how much data the bios have read/written total. Used
299 for rate-limiting. */
300 struct bio_data_counts counts
;
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
;
307 ev_uint32_t errors
[NUM_ERRORS
];
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
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;
316 unsigned allow_dirty_shutdown
: 1;
318 unsigned fd_is_set
: 1;
320 unsigned n_errors
: 2;
322 /* Are we currently connecting, accepting, or doing IO? */
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
*);
334 const struct bufferevent_ops bufferevent_ops_openssl
= {
336 evutil_offsetof(struct bufferevent_openssl
, bev
.bev
),
340 be_openssl_adj_timeouts
,
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
)
350 struct bufferevent_openssl
*bev_o
;
351 if (bev
->be_ops
!= &bufferevent_ops_openssl
)
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
);
360 put_error(struct bufferevent_openssl
*bev_ssl
, unsigned long err
)
362 if (bev_ssl
->n_errors
== NUM_ERRORS
)
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.
369 bev_ssl
->errors
[bev_ssl
->n_errors
++] = (ev_uint32_t
) err
;
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
376 start_reading(struct bufferevent_openssl
*bev_ssl
)
378 if (bev_ssl
->underlying
) {
379 bufferevent_unsuspend_read(bev_ssl
->underlying
,
380 BEV_SUSPEND_FILT_READ
);
383 struct bufferevent
*bev
= &bev_ssl
->bev
.bev
;
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
);
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
397 start_writing(struct bufferevent_openssl
*bev_ssl
)
400 if (bev_ssl
->underlying
) {
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
,
413 stop_reading(struct bufferevent_openssl
*bev_ssl
)
415 if (bev_ssl
->write_blocked_on_read
)
417 if (bev_ssl
->underlying
) {
418 bufferevent_suspend_read(bev_ssl
->underlying
,
419 BEV_SUSPEND_FILT_READ
);
421 struct bufferevent
*bev
= &bev_ssl
->bev
.bev
;
422 event_del(&bev
->ev_read
);
427 stop_writing(struct bufferevent_openssl
*bev_ssl
)
429 if (bev_ssl
->read_blocked_on_write
)
431 if (bev_ssl
->underlying
) {
434 struct bufferevent
*bev
= &bev_ssl
->bev
.bev
;
435 event_del(&bev
->ev_write
);
440 set_rbow(struct bufferevent_openssl
*bev_ssl
)
442 if (!bev_ssl
->underlying
)
443 stop_reading(bev_ssl
);
444 bev_ssl
->read_blocked_on_write
= 1;
445 return start_writing(bev_ssl
);
449 set_wbor(struct bufferevent_openssl
*bev_ssl
)
451 if (!bev_ssl
->underlying
)
452 stop_writing(bev_ssl
);
453 bev_ssl
->write_blocked_on_read
= 1;
454 return start_reading(bev_ssl
);
458 clear_rbow(struct bufferevent_openssl
*bev_ssl
)
460 struct bufferevent
*bev
= &bev_ssl
->bev
.bev
;
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
);
472 clear_wbor(struct bufferevent_openssl
*bev_ssl
)
474 struct bufferevent
*bev
= &bev_ssl
->bev
.bev
;
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
);
485 conn_closed(struct bufferevent_openssl
*bev_ssl
, int errcode
, int ret
)
487 int event
= BEV_EVENT_ERROR
;
488 int dirty_shutdown
= 0;
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
;
499 case SSL_ERROR_SYSCALL
:
500 /* IO error; possibly a dirty shutdown. */
501 if (ret
== 0 && ERR_peek_error() == 0)
505 /* Protocol error. */
507 case SSL_ERROR_WANT_X509_LOOKUP
:
508 /* XXXX handle this. */
511 case SSL_ERROR_WANT_READ
:
512 case SSL_ERROR_WANT_WRITE
:
513 case SSL_ERROR_WANT_CONNECT
:
514 case SSL_ERROR_WANT_ACCEPT
:
516 /* should be impossible; treat as normal error. */
517 event_warnx("BUG: Unexpected OpenSSL error code %d", errcode
);
521 while ((err
= ERR_get_error())) {
522 put_error(bev_ssl
, err
);
525 if (dirty_shutdown
&& bev_ssl
->allow_dirty_shutdown
)
526 event
= BEV_EVENT_EOF
;
528 stop_reading(bev_ssl
);
529 stop_writing(bev_ssl
);
531 _bufferevent_run_eventcb(&bev_ssl
->bev
.bev
, event
);
535 init_bio_counts(struct bufferevent_openssl
*bev_ssl
)
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
));
544 decrement_buckets(struct bufferevent_openssl
*bev_ssl
)
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
;
552 _bufferevent_decrement_write_buckets(&bev_ssl
->bev
, w
);
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
;
559 #define OP_MADE_PROGRESS 1
563 /* Return a bitmask of OP_MADE_PROGRESS (if we read anything); OP_BLOCKED (if
564 we're now blocked); and OP_ERR (if an error occurred). */
566 do_read(struct bufferevent_openssl
*bev_ssl
, int n_to_read
) {
568 struct bufferevent
*bev
= &bev_ssl
->bev
.bev
;
569 struct evbuffer
*input
= bev
->input
;
570 int r
, n
, i
, n_used
= 0, atmost
;
571 struct evbuffer_iovec space
[2];
574 if (bev_ssl
->bev
.read_suspended
)
577 atmost
= _bufferevent_get_read_max(&bev_ssl
->bev
);
578 if (n_to_read
> atmost
)
581 n
= evbuffer_reserve_space(input
, n_to_read
, space
, 2);
585 for (i
=0; i
<n
; ++i
) {
586 if (bev_ssl
->bev
.read_suspended
)
588 r
= SSL_read(bev_ssl
->ssl
, space
[i
].iov_base
, space
[i
].iov_len
);
590 result
|= OP_MADE_PROGRESS
;
591 if (bev_ssl
->read_blocked_on_write
)
592 if (clear_rbow(bev_ssl
) < 0)
593 return OP_ERR
| result
;
595 space
[i
].iov_len
= r
;
596 decrement_buckets(bev_ssl
);
598 int err
= SSL_get_error(bev_ssl
->ssl
, r
);
601 case SSL_ERROR_WANT_READ
:
602 /* Can't read until underlying has more data. */
603 if (bev_ssl
->read_blocked_on_write
)
604 if (clear_rbow(bev_ssl
) < 0)
605 return OP_ERR
| result
;
607 case SSL_ERROR_WANT_WRITE
:
608 /* This read operation requires a write, and the
609 * underlying is full */
610 if (!bev_ssl
->read_blocked_on_write
)
611 if (set_rbow(bev_ssl
) < 0)
612 return OP_ERR
| result
;
615 conn_closed(bev_ssl
, err
, r
);
618 result
|= OP_BLOCKED
;
619 break; /* out of the loop */
624 evbuffer_commit_space(input
, space
, n_used
);
625 if (bev_ssl
->underlying
)
626 BEV_RESET_GENERIC_READ_TIMEOUT(bev
);
632 /* Return a bitmask of OP_MADE_PROGRESS (if we wrote anything); OP_BLOCKED (if
633 we're now blocked); and OP_ERR (if an error occurred). */
635 do_write(struct bufferevent_openssl
*bev_ssl
, int atmost
)
637 int i
, r
, n
, n_written
= 0;
638 struct bufferevent
*bev
= &bev_ssl
->bev
.bev
;
639 struct evbuffer
*output
= bev
->output
;
640 struct evbuffer_iovec space
[8];
643 if (bev_ssl
->last_write
> 0)
644 atmost
= bev_ssl
->last_write
;
646 atmost
= _bufferevent_get_write_max(&bev_ssl
->bev
);
648 n
= evbuffer_peek(output
, atmost
, NULL
, space
, 8);
650 return OP_ERR
| result
;
654 for (i
=0; i
< n
; ++i
) {
655 if (bev_ssl
->bev
.write_suspended
)
658 /* SSL_write will (reasonably) return 0 if we tell it to
659 send 0 data. Skip this case so we don't interpret the
660 result as an error */
661 if (space
[i
].iov_len
== 0)
664 r
= SSL_write(bev_ssl
->ssl
, space
[i
].iov_base
,
667 result
|= OP_MADE_PROGRESS
;
668 if (bev_ssl
->write_blocked_on_read
)
669 if (clear_wbor(bev_ssl
) < 0)
670 return OP_ERR
| result
;
672 bev_ssl
->last_write
= -1;
673 decrement_buckets(bev_ssl
);
675 int err
= SSL_get_error(bev_ssl
->ssl
, r
);
678 case SSL_ERROR_WANT_WRITE
:
679 /* Can't read until underlying has more data. */
680 if (bev_ssl
->write_blocked_on_read
)
681 if (clear_wbor(bev_ssl
) < 0)
682 return OP_ERR
| result
;
683 bev_ssl
->last_write
= space
[i
].iov_len
;
685 case SSL_ERROR_WANT_READ
:
686 /* This read operation requires a write, and the
687 * underlying is full */
688 if (!bev_ssl
->write_blocked_on_read
)
689 if (set_wbor(bev_ssl
) < 0)
690 return OP_ERR
| result
;
691 bev_ssl
->last_write
= space
[i
].iov_len
;
694 conn_closed(bev_ssl
, err
, r
);
695 bev_ssl
->last_write
= -1;
698 result
|= OP_BLOCKED
;
703 evbuffer_drain(output
, n_written
);
704 if (bev_ssl
->underlying
)
705 BEV_RESET_GENERIC_WRITE_TIMEOUT(bev
);
707 if (evbuffer_get_length(output
) <= bev
->wm_write
.low
)
708 _bufferevent_run_writecb(bev
);
713 #define WRITE_FRAME 15000
715 #define READ_DEFAULT 4096
717 /* Try to figure out how many bytes to read; return 0 if we shouldn't be
720 bytes_to_read(struct bufferevent_openssl
*bev
)
722 struct evbuffer
*input
= bev
->bev
.bev
.input
;
723 struct event_watermark
*wm
= &bev
->bev
.bev
.wm_read
;
724 int result
= READ_DEFAULT
;
726 /* XXX 99% of this is generic code that nearly all bufferevents will
729 if (bev
->write_blocked_on_read
) {
733 if (! (bev
->bev
.bev
.enabled
& EV_READ
)) {
737 if (bev
->bev
.read_suspended
) {
742 if (evbuffer_get_length(input
) >= wm
->high
) {
746 result
= wm
->high
- evbuffer_get_length(input
);
748 result
= READ_DEFAULT
;
751 /* Respect the rate limit */
752 limit
= _bufferevent_get_read_max(&bev
->bev
);
753 if (result
> limit
) {
761 /* Things look readable. If write is blocked on read, write till it isn't.
762 * Read from the underlying buffer until we block or we hit our high-water
766 consider_reading(struct bufferevent_openssl
*bev_ssl
)
770 int all_result_flags
= 0;
772 while (bev_ssl
->write_blocked_on_read
) {
773 r
= do_write(bev_ssl
, WRITE_FRAME
);
774 if (r
& (OP_BLOCKED
|OP_ERR
))
777 if (bev_ssl
->write_blocked_on_read
)
780 n_to_read
= bytes_to_read(bev_ssl
);
783 r
= do_read(bev_ssl
, n_to_read
);
784 all_result_flags
|= r
;
786 if (r
& (OP_BLOCKED
|OP_ERR
))
789 if (bev_ssl
->bev
.read_suspended
)
792 /* Read all pending data. This won't hit the network
793 * again, and will (most importantly) put us in a state
794 * where we don't need to read anything else until the
795 * socket is readable again. It'll potentially make us
796 * overrun our read high-watermark (somewhat
797 * regrettable). The damage to the rate-limit has
798 * already been done, since OpenSSL went and read a
799 * whole SSL record anyway. */
800 n_to_read
= SSL_pending(bev_ssl
->ssl
);
802 /* XXX This if statement is actually a bad bug, added to avoid
805 * The bad bug: It can potentially cause resource unfairness
806 * by reading too much data from the underlying bufferevent;
807 * it can potentially cause read looping if the underlying
808 * bufferevent is a bufferevent_pair and deferred callbacks
811 * The worse bug: If we didn't do this, then we would
812 * potentially not read any more from bev_ssl->underlying
813 * until more data arrived there, which could lead to us
816 if (!n_to_read
&& bev_ssl
->underlying
)
817 n_to_read
= bytes_to_read(bev_ssl
);
820 if (all_result_flags
& OP_MADE_PROGRESS
) {
821 struct bufferevent
*bev
= &bev_ssl
->bev
.bev
;
822 struct evbuffer
*input
= bev
->input
;
824 if (evbuffer_get_length(input
) >= bev
->wm_read
.low
) {
825 _bufferevent_run_readcb(bev
);
829 if (!bev_ssl
->underlying
) {
830 /* Should be redundant, but let's avoid busy-looping */
831 if (bev_ssl
->bev
.read_suspended
||
832 !(bev_ssl
->bev
.bev
.enabled
& EV_READ
)) {
833 event_del(&bev_ssl
->bev
.bev
.ev_read
);
839 consider_writing(struct bufferevent_openssl
*bev_ssl
)
842 struct evbuffer
*output
= bev_ssl
->bev
.bev
.output
;
843 struct evbuffer
*target
= NULL
;
844 struct event_watermark
*wm
= NULL
;
846 while (bev_ssl
->read_blocked_on_write
) {
847 r
= do_read(bev_ssl
, 1024); /* XXXX 1024 is a hack */
848 if (r
& OP_MADE_PROGRESS
) {
849 struct bufferevent
*bev
= &bev_ssl
->bev
.bev
;
850 struct evbuffer
*input
= bev
->input
;
852 if (evbuffer_get_length(input
) >= bev
->wm_read
.low
) {
853 _bufferevent_run_readcb(bev
);
856 if (r
& (OP_ERR
|OP_BLOCKED
))
859 if (bev_ssl
->read_blocked_on_write
)
861 if (bev_ssl
->underlying
) {
862 target
= bev_ssl
->underlying
->output
;
863 wm
= &bev_ssl
->underlying
->wm_write
;
865 while ((bev_ssl
->bev
.bev
.enabled
& EV_WRITE
) &&
866 (! bev_ssl
->bev
.write_suspended
) &&
867 evbuffer_get_length(output
) &&
868 (!target
|| (! wm
->high
|| evbuffer_get_length(target
) < wm
->high
))) {
871 n_to_write
= wm
->high
- evbuffer_get_length(target
);
873 n_to_write
= WRITE_FRAME
;
874 r
= do_write(bev_ssl
, n_to_write
);
875 if (r
& (OP_BLOCKED
|OP_ERR
))
879 if (!bev_ssl
->underlying
) {
880 if (evbuffer_get_length(output
) == 0) {
881 event_del(&bev_ssl
->bev
.bev
.ev_write
);
882 } else if (bev_ssl
->bev
.write_suspended
||
883 !(bev_ssl
->bev
.bev
.enabled
& EV_WRITE
)) {
884 /* Should be redundant, but let's avoid busy-looping */
885 event_del(&bev_ssl
->bev
.bev
.ev_write
);
891 be_openssl_readcb(struct bufferevent
*bev_base
, void *ctx
)
893 struct bufferevent_openssl
*bev_ssl
= ctx
;
894 consider_reading(bev_ssl
);
898 be_openssl_writecb(struct bufferevent
*bev_base
, void *ctx
)
900 struct bufferevent_openssl
*bev_ssl
= ctx
;
901 consider_writing(bev_ssl
);
905 be_openssl_eventcb(struct bufferevent
*bev_base
, short what
, void *ctx
)
907 struct bufferevent_openssl
*bev_ssl
= ctx
;
910 if (what
& BEV_EVENT_EOF
) {
911 if (bev_ssl
->allow_dirty_shutdown
)
912 event
= BEV_EVENT_EOF
;
914 event
= BEV_EVENT_ERROR
;
915 } else if (what
& BEV_EVENT_TIMEOUT
) {
916 /* We sure didn't set this. Propagate it to the user. */
918 } else if (what
& BEV_EVENT_ERROR
) {
919 /* An error occurred on the connection. Propagate it to the user. */
921 } else if (what
& BEV_EVENT_CONNECTED
) {
922 /* Ignore it. We're saying SSL_connect() already, which will
926 _bufferevent_run_eventcb(&bev_ssl
->bev
.bev
, event
);
930 be_openssl_readeventcb(evutil_socket_t fd
, short what
, void *ptr
)
932 struct bufferevent_openssl
*bev_ssl
= ptr
;
933 _bufferevent_incref_and_lock(&bev_ssl
->bev
.bev
);
934 if (what
== EV_TIMEOUT
) {
935 _bufferevent_run_eventcb(&bev_ssl
->bev
.bev
,
936 BEV_EVENT_TIMEOUT
|BEV_EVENT_READING
);
938 consider_reading(bev_ssl
);
940 _bufferevent_decref_and_unlock(&bev_ssl
->bev
.bev
);
944 be_openssl_writeeventcb(evutil_socket_t fd
, short what
, void *ptr
)
946 struct bufferevent_openssl
*bev_ssl
= ptr
;
947 _bufferevent_incref_and_lock(&bev_ssl
->bev
.bev
);
948 if (what
== EV_TIMEOUT
) {
949 _bufferevent_run_eventcb(&bev_ssl
->bev
.bev
,
950 BEV_EVENT_TIMEOUT
|BEV_EVENT_WRITING
);
952 consider_writing(bev_ssl
);
954 _bufferevent_decref_and_unlock(&bev_ssl
->bev
.bev
);
958 set_open_callbacks(struct bufferevent_openssl
*bev_ssl
, evutil_socket_t fd
)
960 if (bev_ssl
->underlying
) {
961 bufferevent_setcb(bev_ssl
->underlying
,
962 be_openssl_readcb
, be_openssl_writecb
, be_openssl_eventcb
,
966 struct bufferevent
*bev
= &bev_ssl
->bev
.bev
;
967 int rpending
=0, wpending
=0, r1
=0, r2
=0;
968 if (fd
< 0 && bev_ssl
->fd_is_set
)
969 fd
= event_get_fd(&bev
->ev_read
);
970 if (bev_ssl
->fd_is_set
) {
971 rpending
= event_pending(&bev
->ev_read
, EV_READ
, NULL
);
972 wpending
= event_pending(&bev
->ev_write
, EV_WRITE
, NULL
);
973 event_del(&bev
->ev_read
);
974 event_del(&bev
->ev_write
);
976 event_assign(&bev
->ev_read
, bev
->ev_base
, fd
,
977 EV_READ
|EV_PERSIST
, be_openssl_readeventcb
, bev_ssl
);
978 event_assign(&bev
->ev_write
, bev
->ev_base
, fd
,
979 EV_WRITE
|EV_PERSIST
, be_openssl_writeeventcb
, bev_ssl
);
981 r1
= _bufferevent_add_event(&bev
->ev_read
, &bev
->timeout_read
);
983 r2
= _bufferevent_add_event(&bev
->ev_write
, &bev
->timeout_write
);
985 bev_ssl
->fd_is_set
= 1;
987 return (r1
<0 || r2
<0) ? -1 : 0;
992 do_handshake(struct bufferevent_openssl
*bev_ssl
)
996 switch (bev_ssl
->state
) {
998 case BUFFEREVENT_SSL_OPEN
:
1001 case BUFFEREVENT_SSL_CONNECTING
:
1002 case BUFFEREVENT_SSL_ACCEPTING
:
1003 r
= SSL_do_handshake(bev_ssl
->ssl
);
1006 decrement_buckets(bev_ssl
);
1010 bev_ssl
->state
= BUFFEREVENT_SSL_OPEN
;
1011 set_open_callbacks(bev_ssl
, -1); /* XXXX handle failure */
1012 /* Call do_read and do_write as needed */
1013 bufferevent_enable(&bev_ssl
->bev
.bev
, bev_ssl
->bev
.bev
.enabled
);
1014 _bufferevent_run_eventcb(&bev_ssl
->bev
.bev
,
1015 BEV_EVENT_CONNECTED
);
1018 int err
= SSL_get_error(bev_ssl
->ssl
, r
);
1021 case SSL_ERROR_WANT_WRITE
:
1022 if (!bev_ssl
->underlying
) {
1023 stop_reading(bev_ssl
);
1024 return start_writing(bev_ssl
);
1027 case SSL_ERROR_WANT_READ
:
1028 if (!bev_ssl
->underlying
) {
1029 stop_writing(bev_ssl
);
1030 return start_reading(bev_ssl
);
1034 conn_closed(bev_ssl
, err
, r
);
1041 be_openssl_handshakecb(struct bufferevent
*bev_base
, void *ctx
)
1043 struct bufferevent_openssl
*bev_ssl
= ctx
;
1044 do_handshake(bev_ssl
);/* XXX handle failure */
1048 be_openssl_handshakeeventcb(evutil_socket_t fd
, short what
, void *ptr
)
1050 struct bufferevent_openssl
*bev_ssl
= ptr
;
1052 _bufferevent_incref_and_lock(&bev_ssl
->bev
.bev
);
1053 if (what
& EV_TIMEOUT
) {
1054 _bufferevent_run_eventcb(&bev_ssl
->bev
.bev
, BEV_EVENT_TIMEOUT
);
1056 do_handshake(bev_ssl
);/* XXX handle failure */
1057 _bufferevent_decref_and_unlock(&bev_ssl
->bev
.bev
);
1061 set_handshake_callbacks(struct bufferevent_openssl
*bev_ssl
, evutil_socket_t fd
)
1063 if (bev_ssl
->underlying
) {
1064 bufferevent_setcb(bev_ssl
->underlying
,
1065 be_openssl_handshakecb
, be_openssl_handshakecb
,
1068 return do_handshake(bev_ssl
);
1070 struct bufferevent
*bev
= &bev_ssl
->bev
.bev
;
1072 if (fd
< 0 && bev_ssl
->fd_is_set
)
1073 fd
= event_get_fd(&bev
->ev_read
);
1074 if (bev_ssl
->fd_is_set
) {
1075 event_del(&bev
->ev_read
);
1076 event_del(&bev
->ev_write
);
1078 event_assign(&bev
->ev_read
, bev
->ev_base
, fd
,
1079 EV_READ
|EV_PERSIST
, be_openssl_handshakeeventcb
, bev_ssl
);
1080 event_assign(&bev
->ev_write
, bev
->ev_base
, fd
,
1081 EV_WRITE
|EV_PERSIST
, be_openssl_handshakeeventcb
, bev_ssl
);
1083 r1
= _bufferevent_add_event(&bev
->ev_read
, &bev
->timeout_read
);
1084 r2
= _bufferevent_add_event(&bev
->ev_write
, &bev
->timeout_write
);
1085 bev_ssl
->fd_is_set
= 1;
1087 return (r1
<0 || r2
<0) ? -1 : 0;
1092 bufferevent_ssl_renegotiate(struct bufferevent
*bev
)
1094 struct bufferevent_openssl
*bev_ssl
= upcast(bev
);
1097 if (SSL_renegotiate(bev_ssl
->ssl
) < 0)
1099 bev_ssl
->state
= BUFFEREVENT_SSL_CONNECTING
;
1100 if (set_handshake_callbacks(bev_ssl
, -1) < 0)
1102 if (!bev_ssl
->underlying
)
1103 return do_handshake(bev_ssl
);
1108 be_openssl_outbuf_cb(struct evbuffer
*buf
,
1109 const struct evbuffer_cb_info
*cbinfo
, void *arg
)
1111 struct bufferevent_openssl
*bev_ssl
= arg
;
1113 /* XXX need to hold a reference here. */
1115 if (cbinfo
->n_added
&& bev_ssl
->state
== BUFFEREVENT_SSL_OPEN
) {
1116 if (cbinfo
->orig_size
== 0)
1117 r
= _bufferevent_add_event(&bev_ssl
->bev
.bev
.ev_write
,
1118 &bev_ssl
->bev
.bev
.timeout_write
);
1119 consider_writing(bev_ssl
);
1121 /* XXX Handle r < 0 */
1127 be_openssl_enable(struct bufferevent
*bev
, short events
)
1129 struct bufferevent_openssl
*bev_ssl
= upcast(bev
);
1132 if (bev_ssl
->state
!= BUFFEREVENT_SSL_OPEN
)
1135 if (events
& EV_READ
)
1136 r1
= start_reading(bev_ssl
);
1137 if (events
& EV_WRITE
)
1138 r2
= start_writing(bev_ssl
);
1140 if (bev_ssl
->underlying
) {
1141 if (events
& EV_READ
)
1142 BEV_RESET_GENERIC_READ_TIMEOUT(bev
);
1143 if (events
& EV_WRITE
)
1144 BEV_RESET_GENERIC_WRITE_TIMEOUT(bev
);
1146 if (events
& EV_READ
)
1147 consider_reading(bev_ssl
);
1148 if (events
& EV_WRITE
)
1149 consider_writing(bev_ssl
);
1151 return (r1
< 0 || r2
< 0) ? -1 : 0;
1155 be_openssl_disable(struct bufferevent
*bev
, short events
)
1157 struct bufferevent_openssl
*bev_ssl
= upcast(bev
);
1158 if (bev_ssl
->state
!= BUFFEREVENT_SSL_OPEN
)
1161 if (events
& EV_READ
)
1162 stop_reading(bev_ssl
);
1163 if (events
& EV_WRITE
)
1164 stop_writing(bev_ssl
);
1166 if (bev_ssl
->underlying
) {
1167 if (events
& EV_READ
)
1168 BEV_DEL_GENERIC_READ_TIMEOUT(bev
);
1169 if (events
& EV_WRITE
)
1170 BEV_DEL_GENERIC_WRITE_TIMEOUT(bev
);
1176 be_openssl_destruct(struct bufferevent
*bev
)
1178 struct bufferevent_openssl
*bev_ssl
= upcast(bev
);
1180 if (bev_ssl
->underlying
) {
1181 _bufferevent_del_generic_timeout_cbs(bev
);
1183 event_del(&bev
->ev_read
);
1184 event_del(&bev
->ev_write
);
1187 if (bev_ssl
->bev
.options
& BEV_OPT_CLOSE_ON_FREE
) {
1188 if (bev_ssl
->underlying
) {
1189 if (BEV_UPCAST(bev_ssl
->underlying
)->refcnt
< 2) {
1190 event_warnx("BEV_OPT_CLOSE_ON_FREE set on an "
1191 "bufferevent with too few references");
1193 bufferevent_free(bev_ssl
->underlying
);
1194 bev_ssl
->underlying
= NULL
;
1197 evutil_socket_t fd
= -1;
1198 BIO
*bio
= SSL_get_wbio(bev_ssl
->ssl
);
1200 fd
= BIO_get_fd(bio
, NULL
);
1202 evutil_closesocket(fd
);
1204 SSL_free(bev_ssl
->ssl
);
1206 if (bev_ssl
->underlying
) {
1207 if (bev_ssl
->underlying
->errorcb
== be_openssl_eventcb
)
1208 bufferevent_setcb(bev_ssl
->underlying
,
1209 NULL
,NULL
,NULL
,NULL
);
1210 bufferevent_unsuspend_read(bev_ssl
->underlying
,
1211 BEV_SUSPEND_FILT_READ
);
1217 be_openssl_adj_timeouts(struct bufferevent
*bev
)
1219 struct bufferevent_openssl
*bev_ssl
= upcast(bev
);
1221 if (bev_ssl
->underlying
)
1222 return _bufferevent_generic_adj_timeouts(bev
);
1225 if (event_pending(&bev
->ev_read
, EV_READ
, NULL
))
1226 r1
= _bufferevent_add_event(&bev
->ev_read
, &bev
->timeout_read
);
1227 if (event_pending(&bev
->ev_write
, EV_WRITE
, NULL
))
1228 r2
= _bufferevent_add_event(&bev
->ev_write
, &bev
->timeout_write
);
1229 return (r1
<0 || r2
<0) ? -1 : 0;
1234 be_openssl_flush(struct bufferevent
*bufev
,
1235 short iotype
, enum bufferevent_flush_mode mode
)
1237 /* XXXX Implement this. */
1242 be_openssl_ctrl(struct bufferevent
*bev
,
1243 enum bufferevent_ctrl_op op
, union bufferevent_ctrl_data
*data
)
1245 struct bufferevent_openssl
*bev_ssl
= upcast(bev
);
1247 case BEV_CTRL_SET_FD
:
1248 if (bev_ssl
->underlying
)
1252 bio
= BIO_new_socket(data
->fd
, 0);
1253 SSL_set_bio(bev_ssl
->ssl
, bio
, bio
);
1254 bev_ssl
->fd_is_set
= 1;
1256 if (bev_ssl
->state
== BUFFEREVENT_SSL_OPEN
)
1257 return set_open_callbacks(bev_ssl
, data
->fd
);
1259 return set_handshake_callbacks(bev_ssl
, data
->fd
);
1261 case BEV_CTRL_GET_FD
:
1262 if (bev_ssl
->underlying
)
1264 if (!bev_ssl
->fd_is_set
)
1266 data
->fd
= event_get_fd(&bev
->ev_read
);
1268 case BEV_CTRL_GET_UNDERLYING
:
1269 if (!bev_ssl
->underlying
)
1271 data
->ptr
= bev_ssl
->underlying
;
1273 case BEV_CTRL_CANCEL_ALL
:
1280 bufferevent_openssl_get_ssl(struct bufferevent
*bufev
)
1282 struct bufferevent_openssl
*bev_ssl
= upcast(bufev
);
1285 return bev_ssl
->ssl
;
1288 static struct bufferevent
*
1289 bufferevent_openssl_new_impl(struct event_base
*base
,
1290 struct bufferevent
*underlying
,
1293 enum bufferevent_ssl_state state
,
1296 struct bufferevent_openssl
*bev_ssl
= NULL
;
1297 struct bufferevent_private
*bev_p
= NULL
;
1298 int tmp_options
= options
& ~BEV_OPT_THREADSAFE
;
1300 if (underlying
!= NULL
&& fd
>= 0)
1301 return NULL
; /* Only one can be set. */
1303 if (!(bev_ssl
= mm_calloc(1, sizeof(struct bufferevent_openssl
))))
1306 bev_p
= &bev_ssl
->bev
;
1308 if (bufferevent_init_common(bev_p
, base
,
1309 &bufferevent_ops_openssl
, tmp_options
) < 0)
1312 /* Don't explode if we decide to realloc a chunk we're writing from in
1313 * the output buffer. */
1314 SSL_set_mode(ssl
, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
);
1316 bev_ssl
->underlying
= underlying
;
1319 bev_ssl
->outbuf_cb
= evbuffer_add_cb(bev_p
->bev
.output
,
1320 be_openssl_outbuf_cb
, bev_ssl
);
1322 if (options
& BEV_OPT_THREADSAFE
)
1323 bufferevent_enable_locking(&bev_ssl
->bev
.bev
, NULL
);
1326 _bufferevent_init_generic_timeout_cbs(&bev_ssl
->bev
.bev
);
1327 bufferevent_incref(underlying
);
1330 bev_ssl
->state
= state
;
1331 bev_ssl
->last_write
= -1;
1333 init_bio_counts(bev_ssl
);
1336 case BUFFEREVENT_SSL_ACCEPTING
:
1337 SSL_set_accept_state(bev_ssl
->ssl
);
1338 if (set_handshake_callbacks(bev_ssl
, fd
) < 0)
1341 case BUFFEREVENT_SSL_CONNECTING
:
1342 SSL_set_connect_state(bev_ssl
->ssl
);
1343 if (set_handshake_callbacks(bev_ssl
, fd
) < 0)
1346 case BUFFEREVENT_SSL_OPEN
:
1347 if (set_open_callbacks(bev_ssl
, fd
) < 0)
1355 bufferevent_setwatermark(underlying
, EV_READ
, 0, 0);
1356 bufferevent_enable(underlying
, EV_READ
|EV_WRITE
);
1357 if (state
== BUFFEREVENT_SSL_OPEN
)
1358 bufferevent_suspend_read(underlying
,
1359 BEV_SUSPEND_FILT_READ
);
1361 bev_ssl
->bev
.bev
.enabled
= EV_READ
|EV_WRITE
;
1362 if (bev_ssl
->fd_is_set
) {
1363 if (state
!= BUFFEREVENT_SSL_OPEN
)
1364 if (event_add(&bev_ssl
->bev
.bev
.ev_read
, NULL
) < 0)
1366 if (event_add(&bev_ssl
->bev
.bev
.ev_write
, NULL
) < 0)
1371 return &bev_ssl
->bev
.bev
;
1374 bufferevent_free(&bev_ssl
->bev
.bev
);
1378 struct bufferevent
*
1379 bufferevent_openssl_filter_new(struct event_base
*base
,
1380 struct bufferevent
*underlying
,
1382 enum bufferevent_ssl_state state
,
1385 /* We don't tell the BIO to close the bufferevent; we do it ourselves
1386 * on be_openssl_destruct */
1387 int close_flag
= 0; /* options & BEV_OPT_CLOSE_ON_FREE; */
1391 if (!(bio
= BIO_new_bufferevent(underlying
, close_flag
)))
1394 SSL_set_bio(ssl
, bio
, bio
);
1396 return bufferevent_openssl_new_impl(
1397 base
, underlying
, -1, ssl
, state
, options
);
1400 struct bufferevent
*
1401 bufferevent_openssl_socket_new(struct event_base
*base
,
1404 enum bufferevent_ssl_state state
,
1407 /* Does the SSL already have an fd? */
1408 BIO
*bio
= SSL_get_wbio(ssl
);
1412 have_fd
= BIO_get_fd(bio
, NULL
);
1415 /* The SSL is already configured with an fd. */
1417 /* We should learn the fd from the SSL. */
1418 fd
= (evutil_socket_t
) have_fd
;
1419 } else if (have_fd
== (long)fd
) {
1420 /* We already know the fd from the SSL; do nothing */
1422 /* We specified an fd different from that of the SSL.
1423 This is probably an error on our part. Fail. */
1426 (void) BIO_set_close(bio
, 0);
1428 /* The SSL isn't configured with a BIO with an fd. */
1430 /* ... and we have an fd we want to use. */
1431 bio
= BIO_new_socket(fd
, 0);
1432 SSL_set_bio(ssl
, bio
, bio
);
1434 /* Leave the fd unset. */
1438 return bufferevent_openssl_new_impl(
1439 base
, NULL
, fd
, ssl
, state
, options
);
1443 bufferevent_get_openssl_error(struct bufferevent
*bev
)
1445 unsigned long err
= 0;
1446 struct bufferevent_openssl
*bev_ssl
;
1448 bev_ssl
= upcast(bev
);
1449 if (bev_ssl
&& bev_ssl
->n_errors
) {
1450 err
= bev_ssl
->errors
[--bev_ssl
->n_errors
];