Skip to content

Commit 0903cac

Browse files
committed
openssl: dynamically load libssl and symbols (optionally)
Provide an interface around OpenSSL to dynamically load the libraries and symbols, so that users can distribute a libgit2 library that is not linked directly against OpenSSL. This enables users to target multiple distributions with a single binary. This mechanism is optional and disabled by default. Configure cmake with -DUSE_HTTPS=OpenSSL-Dynamic to use it.
1 parent 150eddd commit 0903cac

File tree

11 files changed

+902
-84
lines changed

11 files changed

+902
-84
lines changed

COPYING

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ The GNU C Library is free software; you can redistribute it and/or
420420
modify it under the terms of the GNU Lesser General Public
421421
License as published by the Free Software Foundation; either
422422
version 2.1 of the License, or (at your option) any later version.
423-
423+
424424
The GNU C Library is distributed in the hope that it will be useful,
425425
but WITHOUT ANY WARRANTY; without even the implied warranty of
426426
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
@@ -1019,3 +1019,111 @@ following restrictions are are met:
10191019
THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
10201020
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
10211021
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1022+
1023+
----------------------------------------------------------------------
1024+
1025+
Portions of the OpenSSL headers are included under the OpenSSL license:
1026+
1027+
Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
1028+
All rights reserved.
1029+
1030+
This package is an SSL implementation written
1031+
by Eric Young (eay@cryptsoft.com).
1032+
The implementation was written so as to conform with Netscapes SSL.
1033+
1034+
This library is free for commercial and non-commercial use as long as
1035+
the following conditions are aheared to. The following conditions
1036+
apply to all code found in this distribution, be it the RC4, RSA,
1037+
lhash, DES, etc., code; not just the SSL code. The SSL documentation
1038+
included with this distribution is covered by the same copyright terms
1039+
except that the holder is Tim Hudson (tjh@cryptsoft.com).
1040+
1041+
Copyright remains Eric Young's, and as such any Copyright notices in
1042+
the code are not to be removed.
1043+
If this package is used in a product, Eric Young should be given attribution
1044+
as the author of the parts of the library used.
1045+
This can be in the form of a textual message at program startup or
1046+
in documentation (online or textual) provided with the package.
1047+
1048+
Redistribution and use in source and binary forms, with or without
1049+
modification, are permitted provided that the following conditions
1050+
are met:
1051+
1. Redistributions of source code must retain the copyright
1052+
notice, this list of conditions and the following disclaimer.
1053+
2. Redistributions in binary form must reproduce the above copyright
1054+
notice, this list of conditions and the following disclaimer in the
1055+
documentation and/or other materials provided with the distribution.
1056+
3. All advertising materials mentioning features or use of this software
1057+
must display the following acknowledgement:
1058+
"This product includes cryptographic software written by
1059+
Eric Young (eay@cryptsoft.com)"
1060+
The word 'cryptographic' can be left out if the rouines from the library
1061+
being used are not cryptographic related :-).
1062+
4. If you include any Windows specific code (or a derivative thereof) from
1063+
the apps directory (application code) you must include an acknowledgement:
1064+
"This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
1065+
1066+
THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
1067+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1068+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1069+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1070+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1071+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1072+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1073+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1074+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1075+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1076+
SUCH DAMAGE.
1077+
1078+
The licence and distribution terms for any publically available version or
1079+
derivative of this code cannot be changed. i.e. this code cannot simply be
1080+
copied and put under another distribution licence
1081+
[including the GNU Public Licence.]
1082+
1083+
====================================================================
1084+
Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
1085+
1086+
Redistribution and use in source and binary forms, with or without
1087+
modification, are permitted provided that the following conditions
1088+
are met:
1089+
1090+
1. Redistributions of source code must retain the above copyright
1091+
notice, this list of conditions and the following disclaimer.
1092+
1093+
2. Redistributions in binary form must reproduce the above copyright
1094+
notice, this list of conditions and the following disclaimer in
1095+
the documentation and/or other materials provided with the
1096+
distribution.
1097+
1098+
3. All advertising materials mentioning features or use of this
1099+
software must display the following acknowledgment:
1100+
"This product includes software developed by the OpenSSL Project
1101+
for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
1102+
1103+
4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1104+
endorse or promote products derived from this software without
1105+
prior written permission. For written permission, please contact
1106+
openssl-core@openssl.org.
1107+
1108+
5. Products derived from this software may not be called "OpenSSL"
1109+
nor may "OpenSSL" appear in their names without prior written
1110+
permission of the OpenSSL Project.
1111+
1112+
6. Redistributions of any form whatsoever must retain the following
1113+
acknowledgment:
1114+
"This product includes software developed by the OpenSSL Project
1115+
for use in the OpenSSL Toolkit (http://www.openssl.org/)"
1116+
1117+
THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1118+
EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1119+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1120+
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1121+
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1122+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1123+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1124+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1125+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1126+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1127+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1128+
OF THE POSSIBILITY OF SUCH DAMAGE.
1129+

cmake/SelectHTTPSBackend.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ IF(USE_HTTPS)
108108
LIST(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES})
109109
ELSEIF (USE_HTTPS STREQUAL "WinHTTP")
110110
# WinHTTP setup was handled in the WinHTTP-specific block above
111+
ELSEIF (USE_HTTPS STREQUAL "OpenSSL-Dynamic")
112+
SET(GIT_OPENSSL 1)
113+
SET(GIT_OPENSSL_DYNAMIC 1)
114+
LIST(APPEND LIBGIT2_LIBS dl)
111115
ELSE()
112116
MESSAGE(FATAL_ERROR "Asked for backend ${USE_HTTPS} but it wasn't found")
113117
ENDIF()

src/features.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#cmakedefine GIT_WINHTTP 1
3535
#cmakedefine GIT_HTTPS 1
3636
#cmakedefine GIT_OPENSSL 1
37+
#cmakedefine GIT_OPENSSL_DYNAMIC 1
3738
#cmakedefine GIT_SECURE_TRANSPORT 1
3839
#cmakedefine GIT_MBEDTLS 1
3940

src/libgit2.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@
3636
# include "win32/w32_leakcheck.h"
3737
#endif
3838

39-
#ifdef GIT_OPENSSL
40-
# include <openssl/err.h>
41-
#endif
42-
43-
#ifdef GIT_MBEDTLS
44-
# include <mbedtls/error.h>
45-
#endif
46-
4739
/* Declarations for tuneable settings */
4840
extern size_t git_mwindow__window_size;
4941
extern size_t git_mwindow__mapped_limit;

src/netops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "net.h"
1515

1616
#ifdef GIT_OPENSSL
17-
# include <openssl/ssl.h>
17+
# include "streams/openssl.h"
1818
#endif
1919

2020
typedef struct gitno_ssl {

src/streams/openssl.c

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
#include "streams/openssl.h"
99
#include "streams/openssl_legacy.h"
10+
#include "streams/openssl_dynamic.h"
1011

1112
#ifdef GIT_OPENSSL
1213

1314
#include <ctype.h>
1415

16+
#include "common.h"
1517
#include "runtime.h"
1618
#include "settings.h"
1719
#include "posix.h"
@@ -27,10 +29,12 @@
2729
# include <netinet/in.h>
2830
#endif
2931

30-
#include <openssl/ssl.h>
31-
#include <openssl/err.h>
32-
#include <openssl/x509v3.h>
33-
#include <openssl/bio.h>
32+
#ifndef GIT_OPENSSL_DYNAMIC
33+
# include <openssl/ssl.h>
34+
# include <openssl/err.h>
35+
# include <openssl/x509v3.h>
36+
# include <openssl/bio.h>
37+
#endif
3438

3539
SSL_CTX *git__ssl_ctx;
3640

@@ -58,27 +62,45 @@ static void shutdown_ssl(void)
5862
}
5963

6064
#ifdef VALGRIND
65+
# if !defined(GIT_OPENSSL_LEGACY) && !defined(GIT_OPENSSL_DYNAMIC)
66+
6167
static void *git_openssl_malloc(size_t bytes, const char *file, int line)
6268
{
6369
GIT_UNUSED(file);
6470
GIT_UNUSED(line);
6571
return git__calloc(1, bytes);
6672
}
67-
73+
6874
static void *git_openssl_realloc(void *mem, size_t size, const char *file, int line)
6975
{
7076
GIT_UNUSED(file);
7177
GIT_UNUSED(line);
7278
return git__realloc(mem, size);
7379
}
74-
80+
7581
static void git_openssl_free(void *mem, const char *file, int line)
7682
{
7783
GIT_UNUSED(file);
7884
GIT_UNUSED(line);
79-
return git__free(mem);
85+
git__free(mem);
86+
}
87+
# else /* !GIT_OPENSSL_LEGACY && !GIT_OPENSSL_DYNAMIC */
88+
static void *git_openssl_malloc(size_t bytes)
89+
{
90+
return git__calloc(1, bytes);
91+
}
92+
93+
static void *git_openssl_realloc(void *mem, size_t size)
94+
{
95+
return git__realloc(mem, size);
8096
}
81-
#endif
97+
98+
static void git_openssl_free(void *mem)
99+
{
100+
git__free(mem);
101+
}
102+
# endif /* !GIT_OPENSSL_LEGACY && !GIT_OPENSSL_DYNAMIC */
103+
#endif /* VALGRIND */
82104

83105
int git_openssl_stream_global_init(void)
84106
{
@@ -93,6 +115,11 @@ int git_openssl_stream_global_init(void)
93115
ssl_opts |= SSL_OP_NO_COMPRESSION;
94116
#endif
95117

118+
#ifdef GIT_OPENSSL_DYNAMIC
119+
if (git_openssl_stream_dynamic_init() < 0)
120+
return -1;
121+
#endif
122+
96123
#ifdef VALGRIND
97124
/*
98125
* Swap in our own allocator functions that initialize
@@ -144,7 +171,7 @@ int git_openssl_stream_global_init(void)
144171
return -1;
145172
}
146173

147-
#ifndef GIT_OPENSSL_LEGACY
174+
#if !defined(GIT_OPENSSL_LEGACY) && !defined(GIT_OPENSSL_DYNAMIC)
148175
int git_openssl_set_locking(void)
149176
{
150177
# ifdef GIT_THREADS

src/streams/openssl.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@
88
#define INCLUDE_streams_openssl_h__
99

1010
#include "common.h"
11+
#include "streams/openssl_legacy.h"
12+
#include "streams/openssl_dynamic.h"
1113

1214
#include "git2/sys/stream.h"
1315

1416
extern int git_openssl_stream_global_init(void);
1517

18+
#if defined(GIT_OPENSSL) && !defined(GIT_OPENSSL_DYNAMIC)
19+
# include <openssl/ssl.h>
20+
# include <openssl/err.h>
21+
# include <openssl/x509v3.h>
22+
# include <openssl/bio.h>
23+
# endif
24+
1625
#ifdef GIT_OPENSSL
1726
extern int git_openssl__set_cert_location(const char *file, const char *path);
18-
1927
extern int git_openssl_stream_new(git_stream **out, const char *host, const char *port);
2028
extern int git_openssl_stream_wrap(git_stream **out, git_stream *in, const char *host);
2129
#endif

0 commit comments

Comments
 (0)