Merge ssl implementations (openssl and mbedtls)

This patch splits common part out to avoid copy-paste from the
- bufferevent_openssl.c
- bufferevent_mbedtls.c

It uses VFS/bufferevent-like approach, i.e. structure of callbacks.
This commit is contained in:
okhowang(王沛文) 2020-07-07 17:26:46 +08:00 committed by Azat Khuzhin
parent dad699cc04
commit d095b834a9
7 changed files with 1552 additions and 2248 deletions

View File

@ -864,7 +864,7 @@ if (NOT EVENT__DISABLE_OPENSSL)
include_directories(${OPENSSL_INCLUDE_DIR})
list(APPEND SRC_OPENSSL bufferevent_openssl.c)
list(APPEND SRC_OPENSSL bufferevent_openssl.c bufferevent_ssl.c)
list(APPEND HDR_PUBLIC include/event2/bufferevent_ssl.h)
list(APPEND LIB_APPS ${OPENSSL_LIBRARIES})
endif()
@ -879,7 +879,7 @@ if (NOT EVENT__DISABLE_MBEDTLS)
include_directories(${MBEDTLS_INCLUDE_DIR})
list(APPEND SRC_MBEDTLS bufferevent_mbedtls.c)
list(APPEND SRC_MBEDTLS bufferevent_mbedtls.c bufferevent_ssl.c)
list(APPEND HDR_PUBLIC include/event2/bufferevent_ssl.h)
list(APPEND LIB_APPS ${MBEDTLS_LIBRARIES})
endif()

View File

@ -102,6 +102,7 @@ LIBEVENT_PKGCONFIG=libevent.pc libevent_core.pc libevent_extra.pc
PLATFORM_DEPENDENT_SRC = \
arc4random.c \
epoll_sub.c \
bufferevent_ssl.c \
test/regress_ssl.c
CMAKE_FILES = \
@ -293,14 +294,14 @@ libevent_extra_la_LIBADD = $(MAYBE_CORE) $(SYS_LIBS)
libevent_extra_la_LDFLAGS = $(GENERIC_LDFLAGS)
if OPENSSL
libevent_openssl_la_SOURCES = bufferevent_openssl.c
libevent_openssl_la_SOURCES = bufferevent_openssl.c bufferevent_ssl.c
libevent_openssl_la_LIBADD = $(MAYBE_CORE) $(OPENSSL_LIBS)
libevent_openssl_la_LDFLAGS = $(GENERIC_LDFLAGS)
libevent_openssl_la_CPPFLAGS = $(AM_CPPFLAGS) $(OPENSSL_INCS)
endif
if MBEDTLS
libevent_mbedtls_la_SOURCES = bufferevent_mbedtls.c
libevent_mbedtls_la_SOURCES = bufferevent_mbedtls.c bufferevent_ssl.c
libevent_mbedtls_la_LIBADD = $(MAYBE_CORE) $(MBEDTLS_LIBS)
libevent_mbedtls_la_LDFLAGS = $(GENERIC_LDFLAGS)
libevent_mbedtls_la_CPPFLAGS = $(AM_CPPFLAGS) $(MBEDTLS_INCS)
@ -336,6 +337,7 @@ noinst_HEADERS += \
time-internal.h \
util-internal.h \
openssl-compat.h \
ssl-compat.h \
wepoll.h
EVENT1_HDRS = \

View File

@ -306,11 +306,11 @@ extern const struct bufferevent_ops bufferevent_ops_pair;
#define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter)
#define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair)
#if defined(EVENT__HAVE_OPENSSL)
extern const struct bufferevent_ops bufferevent_ops_openssl;
#define BEV_IS_OPENSSL(bevp) ((bevp)->be_ops == &bufferevent_ops_openssl)
#if defined(EVENT__HAVE_OPENSSL) | defined(EVENT__HAVE_MBEDTLS)
extern const struct bufferevent_ops bufferevent_ops_ssl;
#define BEV_IS_SSL(bevp) ((bevp)->be_ops == &bufferevent_ops_ssl)
#else
#define BEV_IS_OPENSSL(bevp) 0
#define BEV_IS_SSL(bevp) 0
#endif
#if defined(EVENT__HAVE_MBEDTLS)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

1093
bufferevent_ssl.c Normal file

File diff suppressed because it is too large Load Diff

102
ssl-compat.h Normal file
View File

@ -0,0 +1,102 @@
#ifndef SSL_COMPACT_H
#define SSL_COMPACT_H
#include "event.h"
#include "bufferevent-internal.h"
#include "event2/bufferevent_ssl.h"
struct bufferevent_ssl;
struct le_ssl_ops {
void *(*init)(void *ssl);
void (*free)(void *ssl, int flags);
void (*free_raw)(void *ssl);
int (*renegotiate)(void *ssl);
int (*write)(void *ssl, const unsigned char *buf, size_t len);
int (*read)(void *ssl, unsigned char *buf, size_t len);
size_t (*pending)(void *ssl);
int (*handshake)(void *ssl);
int (*get_error)(void *ssl, int ret);
void (*clear_error)(void);
int (*clear)(void *ssl);
void (*set_connect_state)(void *ssl);
void (*set_accept_state)(void *ssl);
int (*err_is_ok)(int err);
int (*err_is_want_read)(int err);
int (*err_is_want_write)(int err);
evutil_socket_t (*get_fd)(void *ssl);
int (*bio_set_fd)(struct bufferevent_ssl *ssl, evutil_socket_t fd);
void (*post_init)(void *ssl);
void (*init_bio_counts)(struct bufferevent_ssl *bev);
void (*decrement_buckets)(struct bufferevent_ssl *bev);
void (*conn_closed)(
struct bufferevent_ssl *bev, int when, int errcode, int ret);
void (*print_err)(int err);
};
struct bio_data_counts {
unsigned long n_written;
unsigned long n_read;
};
struct bufferevent_ssl {
/* Shared fields with common bufferevent implementation code.
If we were set up with an underlying bufferevent, we use the
events here as timers only. If we have an SSL, then we use
the events as socket events.
*/
struct bufferevent_private bev;
/* An underlying bufferevent that we're directing our output to.
If it's NULL, then we're connected to an fd, not an evbuffer. */
struct bufferevent *underlying;
/* The SSL context doing our encryption. */
void *ssl;
/* The SSL operations doing on ssl. */
struct le_ssl_ops *ssl_ops;
/* A callback that's invoked when data arrives on our outbuf so we
know to write data to the SSL. */
struct evbuffer_cb_entry *outbuf_cb;
/* A count of how much data the bios have read/written total. Used
for rate-limiting. */
struct bio_data_counts counts;
/* If this value is greater than 0, then the last SSL_write blocked,
* and we need to try it again with this many bytes. */
ev_ssize_t last_write;
#define NUM_ERRORS 3
ev_uint32_t errors[NUM_ERRORS];
/* When we next get available space, we should say "read" instead of
"write". This can happen if there's a renegotiation during a read
operation. */
unsigned read_blocked_on_write : 1;
/* When we next get data, we should say "write" instead of "read". */
unsigned write_blocked_on_read : 1;
/* Treat TCP close before SSL close on SSL >= v3 as clean EOF. */
unsigned allow_dirty_shutdown : 1;
/* XXX */
unsigned n_errors : 2;
/* Are we currently connecting, accepting, or doing IO? */
unsigned state : 2;
/* If we reset fd, we sould reset state too */
unsigned old_state : 2;
};
struct bufferevent *bufferevent_ssl_new_impl(struct event_base *base,
struct bufferevent *underlying, evutil_socket_t fd, void *ssl,
enum bufferevent_ssl_state state, int options, struct le_ssl_ops *ssl_ops);
struct bufferevent_ssl *bufferevent_ssl_upcast(struct bufferevent *bev);
void bufferevent_ssl_put_error(
struct bufferevent_ssl *bev_ssl, unsigned long err);
void bufferevent_ssl_stop_reading(struct bufferevent_ssl *bev_ssl);
void bufferevent_ssl_stop_writing(struct bufferevent_ssl *bev_ssl);
int bufferevent_ssl_renegotiate_impl(struct bufferevent *bev);
unsigned long bufferevent_get_ssl_error(struct bufferevent *bev);
int bufferevent_ssl_get_allow_dirty_shutdown(struct bufferevent *bev);
void bufferevent_ssl_set_allow_dirty_shutdown(
struct bufferevent *bev, int allow_dirty_shutdown);
#endif /* SSL_COMPACT_H */