13
13
#include "posix.h"
14
14
#include "stream.h"
15
15
#include "socket_stream.h"
16
+ #include "openssl_stream.h"
16
17
#include "netops.h"
17
18
#include "git2/transport.h"
18
19
#include "git2/sys/openssl.h"
@@ -71,12 +72,20 @@ static void shutdown_ssl_locking(void)
71
72
72
73
#endif /* GIT_THREADS */
73
74
75
+ static BIO_METHOD * git_stream_bio_method ;
76
+ static int init_bio_method (void );
77
+
74
78
/**
75
79
* This function aims to clean-up the SSL context which
76
80
* we allocated.
77
81
*/
78
82
static void shutdown_ssl (void )
79
83
{
84
+ if (git_stream_bio_method ) {
85
+ BIO_meth_free (git_stream_bio_method );
86
+ git_stream_bio_method = NULL ;
87
+ }
88
+
80
89
if (git__ssl_ctx ) {
81
90
SSL_CTX_free (git__ssl_ctx );
82
91
git__ssl_ctx = NULL ;
@@ -121,6 +130,13 @@ int git_openssl_stream_global_init(void)
121
130
git__ssl_ctx = NULL ;
122
131
return -1 ;
123
132
}
133
+
134
+ if (init_bio_method () < 0 ) {
135
+ SSL_CTX_free (git__ssl_ctx );
136
+ git__ssl_ctx = NULL ;
137
+ return -1 ;
138
+ }
139
+
124
140
#endif
125
141
126
142
git__on_shutdown (shutdown_ssl );
@@ -156,10 +172,8 @@ int git_openssl_set_locking(void)
156
172
157
173
static int bio_create (BIO * b )
158
174
{
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 );
163
177
164
178
return 1 ;
165
179
}
@@ -169,23 +183,22 @@ static int bio_destroy(BIO *b)
169
183
if (!b )
170
184
return 0 ;
171
185
172
- b -> init = 0 ;
173
- b -> num = 0 ;
174
- b -> ptr = NULL ;
175
- b -> flags = 0 ;
186
+ BIO_set_data (b , NULL );
176
187
177
188
return 1 ;
178
189
}
179
190
180
191
static int bio_read (BIO * b , char * buf , int len )
181
192
{
182
- git_stream * io = (git_stream * ) b -> ptr ;
193
+ git_stream * io = (git_stream * ) BIO_get_data (b );
194
+
183
195
return (int ) git_stream_read (io , buf , len );
184
196
}
185
197
186
198
static int bio_write (BIO * b , const char * buf , int len )
187
199
{
188
- git_stream * io = (git_stream * ) b -> ptr ;
200
+ git_stream * io = (git_stream * ) BIO_get_data (b );
201
+
189
202
return (int ) git_stream_write (io , buf , len , 0 );
190
203
}
191
204
@@ -214,17 +227,22 @@ static int bio_puts(BIO *b, const char *str)
214
227
return bio_write (b , str , strlen (str ));
215
228
}
216
229
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
+ }
228
246
229
247
static int ssl_set_error (SSL * ssl , int error )
230
248
{
@@ -339,7 +357,7 @@ static int verify_server_cert(SSL *ssl, const char *host)
339
357
num = sk_GENERAL_NAME_num (alts );
340
358
for (i = 0 ; i < num && matched != 1 ; i ++ ) {
341
359
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 );
343
361
size_t namelen = (size_t ) ASN1_STRING_length (gn -> d .ia5 );
344
362
345
363
/* 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)
394
412
if (size > 0 ) {
395
413
peer_cn = OPENSSL_malloc (size + 1 );
396
414
GITERR_CHECK_ALLOC (peer_cn );
397
- memcpy (peer_cn , ASN1_STRING_data (str ), size );
415
+ memcpy (peer_cn , ASN1_STRING_get0_data (str ), size );
398
416
peer_cn [size ] = '\0' ;
399
417
} else {
400
418
goto cert_fail_name ;
@@ -445,11 +463,12 @@ int openssl_connect(git_stream *stream)
445
463
446
464
st -> connected = true;
447
465
448
- bio = BIO_new (& git_stream_bio_method );
466
+ bio = BIO_new (git_stream_bio_method );
449
467
GITERR_CHECK_ALLOC (bio );
450
- bio -> ptr = st -> io ;
451
468
469
+ BIO_set_data (bio , st -> io );
452
470
SSL_set_bio (st -> ssl , bio , bio );
471
+
453
472
/* specify the host in case SNI is needed */
454
473
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
455
474
SSL_set_tlsext_host_name (st -> ssl , st -> host );
0 commit comments