Skip to content

Commit 2505cbf

Browse files
committed
streams: openssl: move OpenSSL compat layer into implementation
OpenSSL version 1.1 has broken its API in quite a few ways. To avoid having to use ifdef's everywhere, we have implemented the BIO functions added in version 1.1 ourselves in case we are using the legacy API. We were implementing them in the header file, though, which doesn't make a lot of sense, since these functions are only ever being used the the openssl stream implementation. Move these functions to the implementation file and mark them static.
1 parent d9007dc commit 2505cbf

File tree

2 files changed

+98
-107
lines changed

2 files changed

+98
-107
lines changed

src/streams/openssl.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,104 @@ SSL_CTX *git__ssl_ctx;
3838

3939
#define GIT_SSL_DEFAULT_CIPHERS "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-DSS-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-DSS-AES128-SHA256:DHE-DSS-AES256-SHA256:DHE-DSS-AES128-SHA:DHE-DSS-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA"
4040

41+
/*
42+
* OpenSSL 1.1 made BIO opaque so we have to use functions to interact with it
43+
* which do not exist in previous versions. We define these inline functions so
44+
* we can program against the interface instead of littering the implementation
45+
* with ifdefs.
46+
*/
47+
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
48+
(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
49+
50+
static BIO_METHOD* BIO_meth_new(int type, const char *name)
51+
{
52+
BIO_METHOD *meth = git__calloc(1, sizeof(BIO_METHOD));
53+
if (!meth) {
54+
return NULL;
55+
}
56+
57+
meth->type = type;
58+
meth->name = name;
59+
60+
return meth;
61+
}
62+
63+
static void BIO_meth_free(BIO_METHOD *biom)
64+
{
65+
git__free(biom);
66+
}
67+
68+
static int BIO_meth_set_write(BIO_METHOD *biom, int (*write) (BIO *, const char *, int))
69+
{
70+
biom->bwrite = write;
71+
return 1;
72+
}
73+
74+
static int BIO_meth_set_read(BIO_METHOD *biom, int (*read) (BIO *, char *, int))
75+
{
76+
biom->bread = read;
77+
return 1;
78+
}
79+
80+
static int BIO_meth_set_puts(BIO_METHOD *biom, int (*puts) (BIO *, const char *))
81+
{
82+
biom->bputs = puts;
83+
return 1;
84+
}
85+
86+
static int BIO_meth_set_gets(BIO_METHOD *biom, int (*gets) (BIO *, char *, int))
87+
88+
{
89+
biom->bgets = gets;
90+
return 1;
91+
}
92+
93+
static int BIO_meth_set_ctrl(BIO_METHOD *biom, long (*ctrl) (BIO *, int, long, void *))
94+
{
95+
biom->ctrl = ctrl;
96+
return 1;
97+
}
98+
99+
static int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
100+
{
101+
biom->create = create;
102+
return 1;
103+
}
104+
105+
static int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
106+
{
107+
biom->destroy = destroy;
108+
return 1;
109+
}
110+
111+
static int BIO_get_new_index(void)
112+
{
113+
/* This exists as of 1.1 so before we'd just have 0 */
114+
return 0;
115+
}
116+
117+
static void BIO_set_init(BIO *b, int init)
118+
{
119+
b->init = init;
120+
}
121+
122+
static void BIO_set_data(BIO *a, void *ptr)
123+
{
124+
a->ptr = ptr;
125+
}
126+
127+
static void *BIO_get_data(BIO *a)
128+
{
129+
return a->ptr;
130+
}
131+
132+
static const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
133+
{
134+
return ASN1_STRING_data((ASN1_STRING *)x);
135+
}
136+
137+
#endif
138+
41139
#if defined(GIT_THREADS) && OPENSSL_VERSION_NUMBER < 0x10100000L
42140

43141
static git_mutex *openssl_locks;

src/streams/openssl.h

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -17,111 +17,4 @@ extern int git_openssl_stream_new(git_stream **out, const char *host, const char
1717

1818
extern int git_openssl__set_cert_location(const char *file, const char *path);
1919

20-
/*
21-
* OpenSSL 1.1 made BIO opaque so we have to use functions to interact with it
22-
* which do not exist in previous versions. We define these inline functions so
23-
* we can program against the interface instead of littering the implementation
24-
* with ifdefs.
25-
*/
26-
#ifdef GIT_OPENSSL
27-
# include <openssl/ssl.h>
28-
# include <openssl/err.h>
29-
# include <openssl/x509v3.h>
30-
# include <openssl/bio.h>
31-
32-
33-
34-
# if OPENSSL_VERSION_NUMBER < 0x10100000L || \
35-
(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
36-
37-
GIT_INLINE(BIO_METHOD*) BIO_meth_new(int type, const char *name)
38-
{
39-
BIO_METHOD *meth = git__calloc(1, sizeof(BIO_METHOD));
40-
if (!meth) {
41-
return NULL;
42-
}
43-
44-
meth->type = type;
45-
meth->name = name;
46-
47-
return meth;
48-
}
49-
50-
GIT_INLINE(void) BIO_meth_free(BIO_METHOD *biom)
51-
{
52-
git__free(biom);
53-
}
54-
55-
GIT_INLINE(int) BIO_meth_set_write(BIO_METHOD *biom, int (*write) (BIO *, const char *, int))
56-
{
57-
biom->bwrite = write;
58-
return 1;
59-
}
60-
61-
GIT_INLINE(int) BIO_meth_set_read(BIO_METHOD *biom, int (*read) (BIO *, char *, int))
62-
{
63-
biom->bread = read;
64-
return 1;
65-
}
66-
67-
GIT_INLINE(int) BIO_meth_set_puts(BIO_METHOD *biom, int (*puts) (BIO *, const char *))
68-
{
69-
biom->bputs = puts;
70-
return 1;
71-
}
72-
73-
GIT_INLINE(int) BIO_meth_set_gets(BIO_METHOD *biom, int (*gets) (BIO *, char *, int))
74-
75-
{
76-
biom->bgets = gets;
77-
return 1;
78-
}
79-
80-
GIT_INLINE(int) BIO_meth_set_ctrl(BIO_METHOD *biom, long (*ctrl) (BIO *, int, long, void *))
81-
{
82-
biom->ctrl = ctrl;
83-
return 1;
84-
}
85-
86-
GIT_INLINE(int) BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
87-
{
88-
biom->create = create;
89-
return 1;
90-
}
91-
92-
GIT_INLINE(int) BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
93-
{
94-
biom->destroy = destroy;
95-
return 1;
96-
}
97-
98-
GIT_INLINE(int) BIO_get_new_index(void)
99-
{
100-
/* This exists as of 1.1 so before we'd just have 0 */
101-
return 0;
102-
}
103-
104-
GIT_INLINE(void) BIO_set_init(BIO *b, int init)
105-
{
106-
b->init = init;
107-
}
108-
109-
GIT_INLINE(void) BIO_set_data(BIO *a, void *ptr)
110-
{
111-
a->ptr = ptr;
112-
}
113-
114-
GIT_INLINE(void*) BIO_get_data(BIO *a)
115-
{
116-
return a->ptr;
117-
}
118-
119-
GIT_INLINE(const unsigned char *) ASN1_STRING_get0_data(const ASN1_STRING *x)
120-
{
121-
return ASN1_STRING_data((ASN1_STRING *)x);
122-
}
123-
124-
# endif // OpenSSL < 1.1
125-
#endif // GIT_OPENSSL
126-
12720
#endif

0 commit comments

Comments
 (0)