@@ -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 )
43150static git_mutex * openssl_locks ;
44151
45152static 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
76183static BIO_METHOD * git_stream_bio_method ;
77184static int init_bio_method (void );
@@ -95,7 +202,6 @@ static void shutdown_ssl(void)
95202
96203int 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
161259int 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" );
0 commit comments