Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26a09a9

Browse files
authored
Merge pull request #4608 from pks-t/pks/openssl-api-cleanup
OpenSSL legacy API cleanups
2 parents b33b6d3 + 173a037 commit 26a09a9

File tree

2 files changed

+112
-121
lines changed

2 files changed

+112
-121
lines changed

src/streams/openssl.c

Lines changed: 112 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,115 @@ 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-
#if defined(GIT_THREADS) && OPENSSL_VERSION_NUMBER < 0x10100000L
41+
#if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L) || \
42+
(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
43+
# define OPENSSL_LEGACY_API
44+
#endif
45+
46+
/*
47+
* OpenSSL 1.1 made BIO opaque so we have to use functions to interact with it
48+
* which do not exist in previous versions. We define these inline functions so
49+
* we can program against the interface instead of littering the implementation
50+
* with ifdefs. We do the same for OPENSSL_init_ssl.
51+
*/
52+
#if defined(OPENSSL_LEGACY_API)
53+
static int OPENSSL_init_ssl(int opts, void *settings)
54+
{
55+
GIT_UNUSED(opts);
56+
GIT_UNUSED(settings);
57+
SSL_load_error_strings();
58+
OpenSSL_add_ssl_algorithms();
59+
return 0;
60+
}
61+
62+
static BIO_METHOD* BIO_meth_new(int type, const char *name)
63+
{
64+
BIO_METHOD *meth = git__calloc(1, sizeof(BIO_METHOD));
65+
if (!meth) {
66+
return NULL;
67+
}
68+
69+
meth->type = type;
70+
meth->name = name;
71+
72+
return meth;
73+
}
74+
75+
static void BIO_meth_free(BIO_METHOD *biom)
76+
{
77+
git__free(biom);
78+
}
4279

80+
static int BIO_meth_set_write(BIO_METHOD *biom, int (*write) (BIO *, const char *, int))
81+
{
82+
biom->bwrite = write;
83+
return 1;
84+
}
85+
86+
static int BIO_meth_set_read(BIO_METHOD *biom, int (*read) (BIO *, char *, int))
87+
{
88+
biom->bread = read;
89+
return 1;
90+
}
91+
92+
static int BIO_meth_set_puts(BIO_METHOD *biom, int (*puts) (BIO *, const char *))
93+
{
94+
biom->bputs = puts;
95+
return 1;
96+
}
97+
98+
static int BIO_meth_set_gets(BIO_METHOD *biom, int (*gets) (BIO *, char *, int))
99+
100+
{
101+
biom->bgets = gets;
102+
return 1;
103+
}
104+
105+
static int BIO_meth_set_ctrl(BIO_METHOD *biom, long (*ctrl) (BIO *, int, long, void *))
106+
{
107+
biom->ctrl = ctrl;
108+
return 1;
109+
}
110+
111+
static int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
112+
{
113+
biom->create = create;
114+
return 1;
115+
}
116+
117+
static int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
118+
{
119+
biom->destroy = destroy;
120+
return 1;
121+
}
122+
123+
static int BIO_get_new_index(void)
124+
{
125+
/* This exists as of 1.1 so before we'd just have 0 */
126+
return 0;
127+
}
128+
129+
static void BIO_set_init(BIO *b, int init)
130+
{
131+
b->init = init;
132+
}
133+
134+
static void BIO_set_data(BIO *a, void *ptr)
135+
{
136+
a->ptr = ptr;
137+
}
138+
139+
static void *BIO_get_data(BIO *a)
140+
{
141+
return a->ptr;
142+
}
143+
144+
static const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
145+
{
146+
return ASN1_STRING_data((ASN1_STRING *)x);
147+
}
148+
149+
# if defined(GIT_THREADS)
43150
static git_mutex *openssl_locks;
44151

45152
static void openssl_locking_function(
@@ -70,8 +177,8 @@ static void shutdown_ssl_locking(void)
70177
git_mutex_free(&openssl_locks[i]);
71178
git__free(openssl_locks);
72179
}
73-
74-
#endif /* GIT_THREADS && OPENSSL_VERSION_NUMBER < 0x10100000L */
180+
# endif /* GIT_THREADS */
181+
#endif /* OPENSSL_LEGACY_API */
75182

76183
static BIO_METHOD *git_stream_bio_method;
77184
static int init_bio_method(void);
@@ -95,7 +202,6 @@ static void shutdown_ssl(void)
95202

96203
int git_openssl_stream_global_init(void)
97204
{
98-
#ifdef GIT_OPENSSL
99205
long ssl_opts = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
100206
const char *ciphers = git_libgit2__ssl_ciphers();
101207

@@ -104,13 +210,7 @@ int git_openssl_stream_global_init(void)
104210
ssl_opts |= SSL_OP_NO_COMPRESSION;
105211
#endif
106212

107-
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
108-
(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
109-
SSL_load_error_strings();
110-
OpenSSL_add_ssl_algorithms();
111-
#else
112213
OPENSSL_init_ssl(0, NULL);
113-
#endif
114214

115215
/*
116216
* Load SSLv{2,3} and TLSv1 so that we can talk with servers
@@ -144,8 +244,6 @@ int git_openssl_stream_global_init(void)
144244
return -1;
145245
}
146246

147-
#endif
148-
149247
git__on_shutdown(shutdown_ssl);
150248

151249
return 0;
@@ -160,7 +258,7 @@ static void threadid_cb(CRYPTO_THREADID *threadid)
160258

161259
int git_openssl_set_locking(void)
162260
{
163-
#if defined(GIT_THREADS) && OPENSSL_VERSION_NUMBER < 0x10100000L
261+
#if defined(GIT_THREADS) && defined(OPENSSL_LEGACY_API)
164262
int num_locks, i;
165263

166264
CRYPTO_THREADID_set_callback(threadid_cb);
@@ -179,7 +277,7 @@ int git_openssl_set_locking(void)
179277
CRYPTO_set_locking_callback(openssl_locking_function);
180278
git__on_shutdown(shutdown_ssl_locking);
181279
return 0;
182-
#elif OPENSSL_VERSION_NUMBER >= 0x10100000L
280+
#elif !defined(OPENSSL_LEGACY_API)
183281
return 0;
184282
#else
185283
giterr_set(GITERR_THREAD, "libgit2 was not built with threads");

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)