Skip to content

Commit 150eddd

Browse files
committed
openssl: separate legacy api
Refactor the OpenSSL stream implementation so that the legacy code is better abstracted. This will enable future development.
1 parent a09d436 commit 150eddd

File tree

3 files changed

+249
-188
lines changed

3 files changed

+249
-188
lines changed

src/streams/openssl.c

Lines changed: 6 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include "streams/openssl.h"
9+
#include "streams/openssl_legacy.h"
910

1011
#ifdef GIT_OPENSSL
1112

@@ -35,147 +36,6 @@ SSL_CTX *git__ssl_ctx;
3536

3637
#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"
3738

38-
#if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L) || \
39-
(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
40-
# define OPENSSL_LEGACY_API
41-
#endif
42-
43-
/*
44-
* OpenSSL 1.1 made BIO opaque so we have to use functions to interact with it
45-
* which do not exist in previous versions. We define these inline functions so
46-
* we can program against the interface instead of littering the implementation
47-
* with ifdefs. We do the same for OPENSSL_init_ssl.
48-
*/
49-
#if defined(OPENSSL_LEGACY_API)
50-
static int OPENSSL_init_ssl(int opts, void *settings)
51-
{
52-
GIT_UNUSED(opts);
53-
GIT_UNUSED(settings);
54-
SSL_load_error_strings();
55-
OpenSSL_add_ssl_algorithms();
56-
return 0;
57-
}
58-
59-
static BIO_METHOD* BIO_meth_new(int type, const char *name)
60-
{
61-
BIO_METHOD *meth = git__calloc(1, sizeof(BIO_METHOD));
62-
if (!meth) {
63-
return NULL;
64-
}
65-
66-
meth->type = type;
67-
meth->name = name;
68-
69-
return meth;
70-
}
71-
72-
static void BIO_meth_free(BIO_METHOD *biom)
73-
{
74-
git__free(biom);
75-
}
76-
77-
static int BIO_meth_set_write(BIO_METHOD *biom, int (*write) (BIO *, const char *, int))
78-
{
79-
biom->bwrite = write;
80-
return 1;
81-
}
82-
83-
static int BIO_meth_set_read(BIO_METHOD *biom, int (*read) (BIO *, char *, int))
84-
{
85-
biom->bread = read;
86-
return 1;
87-
}
88-
89-
static int BIO_meth_set_puts(BIO_METHOD *biom, int (*puts) (BIO *, const char *))
90-
{
91-
biom->bputs = puts;
92-
return 1;
93-
}
94-
95-
static int BIO_meth_set_gets(BIO_METHOD *biom, int (*gets) (BIO *, char *, int))
96-
97-
{
98-
biom->bgets = gets;
99-
return 1;
100-
}
101-
102-
static int BIO_meth_set_ctrl(BIO_METHOD *biom, long (*ctrl) (BIO *, int, long, void *))
103-
{
104-
biom->ctrl = ctrl;
105-
return 1;
106-
}
107-
108-
static int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
109-
{
110-
biom->create = create;
111-
return 1;
112-
}
113-
114-
static int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
115-
{
116-
biom->destroy = destroy;
117-
return 1;
118-
}
119-
120-
static int BIO_get_new_index(void)
121-
{
122-
/* This exists as of 1.1 so before we'd just have 0 */
123-
return 0;
124-
}
125-
126-
static void BIO_set_init(BIO *b, int init)
127-
{
128-
b->init = init;
129-
}
130-
131-
static void BIO_set_data(BIO *a, void *ptr)
132-
{
133-
a->ptr = ptr;
134-
}
135-
136-
static void *BIO_get_data(BIO *a)
137-
{
138-
return a->ptr;
139-
}
140-
141-
static const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
142-
{
143-
return ASN1_STRING_data((ASN1_STRING *)x);
144-
}
145-
146-
# if defined(GIT_THREADS)
147-
static git_mutex *openssl_locks;
148-
149-
static void openssl_locking_function(
150-
int mode, int n, const char *file, int line)
151-
{
152-
int lock;
153-
154-
GIT_UNUSED(file);
155-
GIT_UNUSED(line);
156-
157-
lock = mode & CRYPTO_LOCK;
158-
159-
if (lock) {
160-
(void)git_mutex_lock(&openssl_locks[n]);
161-
} else {
162-
git_mutex_unlock(&openssl_locks[n]);
163-
}
164-
}
165-
166-
static void shutdown_ssl_locking(void)
167-
{
168-
int num_locks, i;
169-
170-
num_locks = CRYPTO_num_locks();
171-
CRYPTO_set_locking_callback(NULL);
172-
173-
for (i = 0; i < num_locks; ++i)
174-
git_mutex_free(&openssl_locks[i]);
175-
git__free(openssl_locks);
176-
}
177-
# endif /* GIT_THREADS */
178-
#endif /* OPENSSL_LEGACY_API */
17939

18040
static BIO_METHOD *git_stream_bio_method;
18141
static int init_bio_method(void);
@@ -198,22 +58,6 @@ static void shutdown_ssl(void)
19858
}
19959

20060
#ifdef VALGRIND
201-
#ifdef OPENSSL_LEGACY_API
202-
static void *git_openssl_malloc(size_t bytes)
203-
{
204-
return git__calloc(1, bytes);
205-
}
206-
207-
static void *git_openssl_realloc(void *mem, size_t size)
208-
{
209-
return git__realloc(mem, size);
210-
}
211-
212-
static void git_openssl_free(void *mem)
213-
{
214-
return git__free(mem);
215-
}
216-
#else
21761
static void *git_openssl_malloc(size_t bytes, const char *file, int line)
21862
{
21963
GIT_UNUSED(file);
@@ -235,7 +79,6 @@ static void git_openssl_free(void *mem, const char *file, int line)
23579
return git__free(mem);
23680
}
23781
#endif
238-
#endif
23982

24083
int git_openssl_stream_global_init(void)
24184
{
@@ -301,42 +144,17 @@ int git_openssl_stream_global_init(void)
301144
return -1;
302145
}
303146

304-
#if defined(GIT_THREADS) && defined(OPENSSL_LEGACY_API)
305-
static void threadid_cb(CRYPTO_THREADID *threadid)
306-
{
307-
GIT_UNUSED(threadid);
308-
CRYPTO_THREADID_set_numeric(threadid, git_thread_currentid());
309-
}
310-
#endif
311-
147+
#ifndef GIT_OPENSSL_LEGACY
312148
int git_openssl_set_locking(void)
313149
{
314-
#if defined(GIT_THREADS) && defined(OPENSSL_LEGACY_API)
315-
int num_locks, i;
316-
317-
CRYPTO_THREADID_set_callback(threadid_cb);
318-
319-
num_locks = CRYPTO_num_locks();
320-
openssl_locks = git__calloc(num_locks, sizeof(git_mutex));
321-
GIT_ERROR_CHECK_ALLOC(openssl_locks);
322-
323-
for (i = 0; i < num_locks; i++) {
324-
if (git_mutex_init(&openssl_locks[i]) != 0) {
325-
git_error_set(GIT_ERROR_SSL, "failed to initialize openssl locks");
326-
return -1;
327-
}
328-
}
329-
330-
CRYPTO_set_locking_callback(openssl_locking_function);
331-
return git_runtime_shutdown_register(shutdown_ssl_locking);
332-
333-
#elif !defined(OPENSSL_LEGACY_API)
150+
# ifdef GIT_THREADS
334151
return 0;
335-
#else
152+
# else
336153
git_error_set(GIT_ERROR_THREAD, "libgit2 was not built with threads");
337154
return -1;
338-
#endif
155+
# endif
339156
}
157+
#endif
340158

341159

342160
static int bio_create(BIO *b)

0 commit comments

Comments
 (0)