summaryrefslogtreecommitdiff
path: root/src/ssl_openssl.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/ssl_openssl.cc')
-rw-r--r--src/ssl_openssl.cc72
1 files changed, 50 insertions, 22 deletions
diff --git a/src/ssl_openssl.cc b/src/ssl_openssl.cc
index 0c3eed0..de27dd4 100644
--- a/src/ssl_openssl.cc
+++ b/src/ssl_openssl.cc
@@ -4,6 +4,7 @@
#define SSL oSSL
+#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
@@ -14,6 +15,7 @@
#undef SSL
#include <memory>
+#include <string.h>
#include "buffer.hh"
#include "logger.hh"
@@ -21,6 +23,35 @@
namespace {
+#if OPENSSL_VERSION_NUMBER < 0x10100000
+
+BIO_METHOD* BIO_meth_new(int type, const char* name) {
+ auto bm = new BIO_METHOD;
+ memset(bm, 0, sizeof(BIO_METHOD));
+ bm->type = type;
+ bm->name = name;
+ return ret;
+}
+
+void BIO_meth_free(BIO_METHOD* bm) {
+ delete bm;
+}
+
+#define BIO_meth_set_write(bm, f) bm->bwrite = f
+#define BIO_meth_set_read(bm, f) bm->bread = f
+#define BIO_meth_set_puts(bm, f) bm->bputs = f
+#define BIO_meth_set_gets(bm, f) bm->bgets = f
+#define BIO_meth_set_ctrl(bm, f) bm->ctrl = f
+#define BIO_meth_set_create(bm, f) bm->create = f
+#define BIO_meth_set_destroy(bm, f) bm->destroy = f
+
+#define BIO_set_init(b, v) b->init = v
+#define BIO_set_data(b, v) b->ptr = v
+#define BIO_set_shutdown(b, v) b->shutdown = v
+#define BIO_get_data(b) b->ptr
+
+#endif
+
void check_init() {
#if OPENSSL_VERSION_NUMBER < 0x10100000
static bool initialized;
@@ -105,26 +136,23 @@ class SSLImpl : public SSL {
public:
SSLImpl(Logger* logger, uint16_t flags)
: logger_(logger), flags_(flags),
- bio_method({
- (99 | BIO_TYPE_SOURCE_SINK),
- "SSLImpl",
- bio_write,
- bio_read,
- bio_puts,
- bio_gets,
- bio_ctrl,
- bio_create,
- bio_destroy,
- nullptr
- }),
+ bio_meth_(BIO_meth_new(99 | BIO_TYPE_SOURCE_SINK, "SSLImpl")),
ctx_(nullptr), ssl_(nullptr),
bio_(nullptr), rbuf_(nullptr), wbuf_(nullptr) {
+ BIO_meth_set_write(bio_meth_, bio_write);
+ BIO_meth_set_read(bio_meth_, bio_read);
+ BIO_meth_set_puts(bio_meth_, bio_puts);
+ BIO_meth_set_gets(bio_meth_, bio_gets);
+ BIO_meth_set_ctrl(bio_meth_, bio_ctrl);
+ BIO_meth_set_create(bio_meth_, bio_create);
+ BIO_meth_set_destroy(bio_meth_, bio_destroy);
}
~SSLImpl() override {
if (ssl_) SSL_free(ssl_);
if (ctx_) SSL_CTX_free(ctx_);
if (bio_) BIO_free(bio_);
+ if (bio_meth_) BIO_meth_free(bio_meth_);
}
bool unsecure() const {
@@ -268,8 +296,8 @@ protected:
logerr(logger_, "Unable to create SSL");
return false;
}
- bio_ = BIO_new(&bio_method);
- bio_->ptr = this;
+ bio_ = BIO_new(bio_meth_);
+ BIO_set_data(bio_, this);
SSL_set_bio(ssl_, bio_, bio_);
return true;
}
@@ -287,12 +315,11 @@ private:
ERROR,
TRANSFER
};
- BIO_METHOD bio_method;
static int bio_write(BIO* bio, const char* buf, int len) {
BIO_clear_retry_flags(bio);
if (len <= 0) return 0;
- auto impl = reinterpret_cast<SSLImpl*>(bio->ptr);
+ auto impl = reinterpret_cast<SSLImpl*>(BIO_get_data(bio));
if (impl->wbuf_) {
impl->wbuf_->write(buf, len);
return len;
@@ -303,7 +330,7 @@ private:
static int bio_read(BIO* bio, char* buf, int len) {
BIO_clear_retry_flags(bio);
if (len <= 0) return 0;
- auto impl = reinterpret_cast<SSLImpl*>(bio->ptr);
+ auto impl = reinterpret_cast<SSLImpl*>(BIO_get_data(bio));
if (impl->rbuf_) {
auto ret = impl->rbuf_->read(buf, len);
if (ret > 0) return ret;
@@ -325,17 +352,18 @@ private:
return 0;
}
static int bio_create(BIO* bio) {
- bio->shutdown = 0;
- bio->init = 1;
- bio->ptr = nullptr;
+ BIO_set_shutdown(bio, 0);
+ BIO_set_init(bio, 1);
+ BIO_set_data(bio, nullptr);
return 1;
}
static int bio_destroy(BIO* bio) {
if (!bio) return 0;
- bio->init = 0;
- bio->ptr = nullptr;
+ BIO_set_init(bio, 0);
+ BIO_set_data(bio, nullptr);
return 1;
}
+ BIO_METHOD* bio_meth_;
SSL_CTX* ctx_;
oSSL* ssl_;
State state_;