summaryrefslogtreecommitdiff
path: root/src/openssl_stream.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2016-11-02 12:28:25 +0100
committerCarlos Martín Nieto <cmn@dwim.me>2016-11-02 13:00:30 +0100
commitf15eedb3a390dcbe441cd77231c3449ff941d189 (patch)
tree0cee77738e37f36d30bd78e0a2e1ffba01344ebf /src/openssl_stream.c
parentfeb330d50d0fc10aceec6309131e912e152a1027 (diff)
downloadlibgit2-f15eedb3a390dcbe441cd77231c3449ff941d189.tar.gz
openssl: recreate the OpenSSL 1.1 BIO interface for older versions
We want to program against the interface, so recreate it when we compile against pre-1.1 versions.
Diffstat (limited to 'src/openssl_stream.c')
-rw-r--r--src/openssl_stream.c92
1 files changed, 37 insertions, 55 deletions
diff --git a/src/openssl_stream.c b/src/openssl_stream.c
index 9c5e0b1e8..80c6b9828 100644
--- a/src/openssl_stream.c
+++ b/src/openssl_stream.c
@@ -13,6 +13,7 @@
#include "posix.h"
#include "stream.h"
#include "socket_stream.h"
+#include "openssl_stream.h"
#include "netops.h"
#include "git2/transport.h"
#include "git2/sys/openssl.h"
@@ -71,12 +72,20 @@ static void shutdown_ssl_locking(void)
#endif /* GIT_THREADS */
+static BIO_METHOD *git_stream_bio_method;
+static int init_bio_method(void);
+
/**
* This function aims to clean-up the SSL context which
* we allocated.
*/
static void shutdown_ssl(void)
{
+ if (git_stream_bio_method) {
+ BIO_meth_free(git_stream_bio_method);
+ git_stream_bio_method = NULL;
+ }
+
if (git__ssl_ctx) {
SSL_CTX_free(git__ssl_ctx);
git__ssl_ctx = NULL;
@@ -121,6 +130,13 @@ int git_openssl_stream_global_init(void)
git__ssl_ctx = NULL;
return -1;
}
+
+ if (init_bio_method() < 0) {
+ SSL_CTX_free(git__ssl_ctx);
+ git__ssl_ctx = NULL;
+ return -1;
+ }
+
#endif
git__on_shutdown(shutdown_ssl);
@@ -156,14 +172,8 @@ int git_openssl_set_locking(void)
static int bio_create(BIO *b)
{
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
- b->init = 1;
- b->num = 0;
- b->ptr = NULL;
- b->flags = 0;
-#else
BIO_set_init(b, 1);
-#endif
+ BIO_set_data(b, NULL);
return 1;
}
@@ -173,36 +183,22 @@ static int bio_destroy(BIO *b)
if (!b)
return 0;
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
- b->init = 0;
- b->num = 0;
- b->ptr = NULL;
- b->flags = 0;
-#else
- BIO_set_init(b, 0);
BIO_set_data(b, NULL);
-#endif
return 1;
}
static int bio_read(BIO *b, char *buf, int len)
{
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
- git_stream *io = (git_stream *) b->ptr;
-#else
git_stream *io = (git_stream *) BIO_get_data(b);
-#endif
+
return (int) git_stream_read(io, buf, len);
}
static int bio_write(BIO *b, const char *buf, int len)
{
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
- git_stream *io = (git_stream *) b->ptr;
-#else
git_stream *io = (git_stream *) BIO_get_data(b);
-#endif
+
return (int) git_stream_write(io, buf, len, 0);
}
@@ -231,21 +227,22 @@ static int bio_puts(BIO *b, const char *str)
return bio_write(b, str, strlen(str));
}
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
-static BIO_METHOD git_stream_bio_method = {
- BIO_TYPE_SOURCE_SINK,
- "git_stream",
- bio_write,
- bio_read,
- bio_puts,
- bio_gets,
- bio_ctrl,
- bio_create,
- bio_destroy
-};
-#else
-static BIO_METHOD *git_stream_bio_method = NULL;
-#endif
+static int init_bio_method(void)
+{
+ /* Set up the BIO_METHOD we use for wrapping our own stream implementations */
+ git_stream_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK | BIO_get_new_index(), "git_stream");
+ GITERR_CHECK_ALLOC(git_stream_bio_method);
+
+ BIO_meth_set_write(git_stream_bio_method, bio_write);
+ BIO_meth_set_read(git_stream_bio_method, bio_read);
+ BIO_meth_set_puts(git_stream_bio_method, bio_puts);
+ BIO_meth_set_gets(git_stream_bio_method, bio_gets);
+ BIO_meth_set_ctrl(git_stream_bio_method, bio_ctrl);
+ BIO_meth_set_create(git_stream_bio_method, bio_create);
+ BIO_meth_set_destroy(git_stream_bio_method, bio_destroy);
+
+ return 0;
+}
static int ssl_set_error(SSL *ssl, int error)
{
@@ -466,27 +463,12 @@ int openssl_connect(git_stream *stream)
st->connected = true;
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
- bio = BIO_new(&git_stream_bio_method);
-#else
- git_stream_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK | BIO_get_new_index(), "git_stream");
- BIO_meth_set_write(git_stream_bio_method, bio_write);
- BIO_meth_set_read(git_stream_bio_method, bio_read);
- BIO_meth_set_puts(git_stream_bio_method, bio_puts);
- BIO_meth_set_gets(git_stream_bio_method, bio_gets);
- BIO_meth_set_ctrl(git_stream_bio_method, bio_ctrl);
- BIO_meth_set_create(git_stream_bio_method, bio_create);
- BIO_meth_set_destroy(git_stream_bio_method, bio_destroy);
bio = BIO_new(git_stream_bio_method);
-#endif
GITERR_CHECK_ALLOC(bio);
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
- bio->ptr = st->io;
-#else
- BIO_set_data(bio, st->io);
-#endif
+ BIO_set_data(bio, st->io);
SSL_set_bio(st->ssl, bio, bio);
+
/* specify the host in case SNI is needed */
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
SSL_set_tlsext_host_name(st->ssl, st->host);