1313#include "posix.h"
1414#include "stream.h"
1515#include "socket_stream.h"
16+ #include "openssl_stream.h"
1617#include "netops.h"
1718#include "git2/transport.h"
1819#include "git2/sys/openssl.h"
@@ -71,12 +72,20 @@ static void shutdown_ssl_locking(void)
7172
7273#endif /* GIT_THREADS */
7374
75+ static BIO_METHOD * git_stream_bio_method ;
76+ static int init_bio_method (void );
77+
7478/**
7579 * This function aims to clean-up the SSL context which
7680 * we allocated.
7781 */
7882static void shutdown_ssl (void )
7983{
84+ if (git_stream_bio_method ) {
85+ BIO_meth_free (git_stream_bio_method );
86+ git_stream_bio_method = NULL ;
87+ }
88+
8089 if (git__ssl_ctx ) {
8190 SSL_CTX_free (git__ssl_ctx );
8291 git__ssl_ctx = NULL ;
@@ -121,6 +130,13 @@ int git_openssl_stream_global_init(void)
121130 git__ssl_ctx = NULL ;
122131 return -1 ;
123132 }
133+
134+ if (init_bio_method () < 0 ) {
135+ SSL_CTX_free (git__ssl_ctx );
136+ git__ssl_ctx = NULL ;
137+ return -1 ;
138+ }
139+
124140#endif
125141
126142 git__on_shutdown (shutdown_ssl );
@@ -156,10 +172,8 @@ int git_openssl_set_locking(void)
156172
157173static int bio_create (BIO * b )
158174{
159- b -> init = 1 ;
160- b -> num = 0 ;
161- b -> ptr = NULL ;
162- b -> flags = 0 ;
175+ BIO_set_init (b , 1 );
176+ BIO_set_data (b , NULL );
163177
164178 return 1 ;
165179}
@@ -169,23 +183,22 @@ static int bio_destroy(BIO *b)
169183 if (!b )
170184 return 0 ;
171185
172- b -> init = 0 ;
173- b -> num = 0 ;
174- b -> ptr = NULL ;
175- b -> flags = 0 ;
186+ BIO_set_data (b , NULL );
176187
177188 return 1 ;
178189}
179190
180191static int bio_read (BIO * b , char * buf , int len )
181192{
182- git_stream * io = (git_stream * ) b -> ptr ;
193+ git_stream * io = (git_stream * ) BIO_get_data (b );
194+
183195 return (int ) git_stream_read (io , buf , len );
184196}
185197
186198static int bio_write (BIO * b , const char * buf , int len )
187199{
188- git_stream * io = (git_stream * ) b -> ptr ;
200+ git_stream * io = (git_stream * ) BIO_get_data (b );
201+
189202 return (int ) git_stream_write (io , buf , len , 0 );
190203}
191204
@@ -214,17 +227,22 @@ static int bio_puts(BIO *b, const char *str)
214227 return bio_write (b , str , strlen (str ));
215228}
216229
217- static BIO_METHOD git_stream_bio_method = {
218- BIO_TYPE_SOURCE_SINK ,
219- "git_stream" ,
220- bio_write ,
221- bio_read ,
222- bio_puts ,
223- bio_gets ,
224- bio_ctrl ,
225- bio_create ,
226- bio_destroy
227- };
230+ static int init_bio_method (void )
231+ {
232+ /* Set up the BIO_METHOD we use for wrapping our own stream implementations */
233+ git_stream_bio_method = BIO_meth_new (BIO_TYPE_SOURCE_SINK | BIO_get_new_index (), "git_stream" );
234+ GITERR_CHECK_ALLOC (git_stream_bio_method );
235+
236+ BIO_meth_set_write (git_stream_bio_method , bio_write );
237+ BIO_meth_set_read (git_stream_bio_method , bio_read );
238+ BIO_meth_set_puts (git_stream_bio_method , bio_puts );
239+ BIO_meth_set_gets (git_stream_bio_method , bio_gets );
240+ BIO_meth_set_ctrl (git_stream_bio_method , bio_ctrl );
241+ BIO_meth_set_create (git_stream_bio_method , bio_create );
242+ BIO_meth_set_destroy (git_stream_bio_method , bio_destroy );
243+
244+ return 0 ;
245+ }
228246
229247static int ssl_set_error (SSL * ssl , int error )
230248{
@@ -339,7 +357,7 @@ static int verify_server_cert(SSL *ssl, const char *host)
339357 num = sk_GENERAL_NAME_num (alts );
340358 for (i = 0 ; i < num && matched != 1 ; i ++ ) {
341359 const GENERAL_NAME * gn = sk_GENERAL_NAME_value (alts , i );
342- const char * name = (char * ) ASN1_STRING_data (gn -> d .ia5 );
360+ const char * name = (char * ) ASN1_STRING_get0_data (gn -> d .ia5 );
343361 size_t namelen = (size_t ) ASN1_STRING_length (gn -> d .ia5 );
344362
345363 /* Skip any names of a type we're not looking for */
@@ -394,7 +412,7 @@ static int verify_server_cert(SSL *ssl, const char *host)
394412 if (size > 0 ) {
395413 peer_cn = OPENSSL_malloc (size + 1 );
396414 GITERR_CHECK_ALLOC (peer_cn );
397- memcpy (peer_cn , ASN1_STRING_data (str ), size );
415+ memcpy (peer_cn , ASN1_STRING_get0_data (str ), size );
398416 peer_cn [size ] = '\0' ;
399417 } else {
400418 goto cert_fail_name ;
@@ -445,11 +463,12 @@ int openssl_connect(git_stream *stream)
445463
446464 st -> connected = true;
447465
448- bio = BIO_new (& git_stream_bio_method );
466+ bio = BIO_new (git_stream_bio_method );
449467 GITERR_CHECK_ALLOC (bio );
450- bio -> ptr = st -> io ;
451468
469+ BIO_set_data (bio , st -> io );
452470 SSL_set_bio (st -> ssl , bio , bio );
471+
453472 /* specify the host in case SNI is needed */
454473#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
455474 SSL_set_tlsext_host_name (st -> ssl , st -> host );
0 commit comments