Skip to content

Commit 75395c8

Browse files
committed
streams: report OpenSSL errors if global init fails
In case when the global initialization of the OpenSSL stream fails, the user is left without any hint as to what went wrong as we do not provide any error message at all. This commit refactors the init function to have a common error path, which now also sets an error message including the error string provided by OpenSSL.
1 parent 01574d4 commit 75395c8

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

src/streams/openssl.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -218,39 +218,34 @@ int git_openssl_stream_global_init(void)
218218
* compatibility. We then disable SSL so we only allow OpenSSL
219219
* to speak TLSv1 to perform the encryption itself.
220220
*/
221-
git__ssl_ctx = SSL_CTX_new(SSLv23_method());
222-
if (!git__ssl_ctx) {
223-
return -1;
224-
}
221+
if (!(git__ssl_ctx = SSL_CTX_new(SSLv23_method())))
222+
goto error;
225223

226224
SSL_CTX_set_options(git__ssl_ctx, ssl_opts);
227225
SSL_CTX_set_mode(git__ssl_ctx, SSL_MODE_AUTO_RETRY);
228226
SSL_CTX_set_verify(git__ssl_ctx, SSL_VERIFY_NONE, NULL);
229-
if (!SSL_CTX_set_default_verify_paths(git__ssl_ctx)) {
230-
SSL_CTX_free(git__ssl_ctx);
231-
git__ssl_ctx = NULL;
232-
return -1;
233-
}
227+
if (!SSL_CTX_set_default_verify_paths(git__ssl_ctx))
228+
goto error;
234229

235-
if (!ciphers) {
230+
if (!ciphers)
236231
ciphers = GIT_SSL_DEFAULT_CIPHERS;
237-
}
238232

239-
if(!SSL_CTX_set_cipher_list(git__ssl_ctx, ciphers)) {
240-
SSL_CTX_free(git__ssl_ctx);
241-
git__ssl_ctx = NULL;
242-
return -1;
243-
}
233+
if(!SSL_CTX_set_cipher_list(git__ssl_ctx, ciphers))
234+
goto error;
244235

245-
if (init_bio_method() < 0) {
246-
SSL_CTX_free(git__ssl_ctx);
247-
git__ssl_ctx = NULL;
248-
return -1;
249-
}
236+
if (init_bio_method() < 0)
237+
goto error;
250238

251239
git__on_shutdown(shutdown_ssl);
252240

253241
return 0;
242+
243+
error:
244+
giterr_set(GITERR_NET, "could not initialize openssl: %s",
245+
ERR_error_string(ERR_get_error(), NULL));
246+
SSL_CTX_free(git__ssl_ctx);
247+
git__ssl_ctx = NULL;
248+
return -1;
254249
}
255250

256251
#if defined(GIT_THREADS) && defined(OPENSSL_LEGACY_API)

0 commit comments

Comments
 (0)