Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1ec732

Browse files
dbussinkEdward Thomson
authored and
Edward Thomson
committed
Setup better defaults for OpenSSL ciphers
This ensures that when using OpenSSL a safe default set of ciphers is selected. This is done so that the client communicates securely and we don't accidentally enable unsafe ciphers like RC4, or even worse some old export ciphers. Implements the first part of libgit2#3682
1 parent 89e7604 commit c1ec732

File tree

6 files changed

+53
-0
lines changed

6 files changed

+53
-0
lines changed

include/git2/common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ typedef enum {
149149
GIT_OPT_SET_SSL_CERT_LOCATIONS,
150150
GIT_OPT_SET_USER_AGENT,
151151
GIT_OPT_ENABLE_STRICT_OBJECT_CREATION,
152+
GIT_OPT_SET_SSL_CIPHERS,
152153
} git_libgit2_opt_t;
153154

154155
/**
@@ -260,6 +261,11 @@ typedef enum {
260261
* > example, when this is enabled, the parent(s) and tree inputs
261262
* > will be validated when creating a new commit. This defaults
262263
* > to disabled.
264+
* * opts(GIT_OPT_SET_SSL_CIPHERS, const char *ciphers)
265+
*
266+
* > Set the SSL ciphers use for HTTPS connections.
267+
* >
268+
* > - `ciphers` is the list of ciphers that are eanbled.
263269
*
264270
* @param option Option key
265271
* @param ... value to set the option

src/global.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB];
2727
static git_atomic git__n_shutdown_callbacks;
2828
static git_atomic git__n_inits;
2929
char *git__user_agent;
30+
char *git__ssl_ciphers;
3031

3132
void git__on_shutdown(git_global_shutdown_fn callback)
3233
{
@@ -83,6 +84,7 @@ static void shutdown_common(void)
8384
}
8485

8586
git__free(git__user_agent);
87+
git__free(git__ssl_ciphers);
8688

8789
#if defined(GIT_MSVC_CRTDBG)
8890
git_win32__crtdbg_stacktrace_cleanup();

src/global.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ extern void git__on_shutdown(git_global_shutdown_fn callback);
3636
extern void git__free_tls_data(void);
3737

3838
extern const char *git_libgit2__user_agent(void);
39+
extern const char *git_libgit2__ssl_ciphers(void);
3940

4041
#endif

src/openssl_stream.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
SSL_CTX *git__ssl_ctx;
3636

37+
#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"
38+
3739
#ifdef GIT_THREADS
3840

3941
static git_mutex *openssl_locks;
@@ -85,6 +87,7 @@ int git_openssl_stream_global_init(void)
8587
{
8688
#ifdef GIT_OPENSSL
8789
long ssl_opts = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
90+
const char *ciphers = git_libgit2__ssl_ciphers();
8891

8992
/* Older OpenSSL and MacOS OpenSSL doesn't have this */
9093
#ifdef SSL_OP_NO_COMPRESSION
@@ -108,6 +111,16 @@ int git_openssl_stream_global_init(void)
108111
git__ssl_ctx = NULL;
109112
return -1;
110113
}
114+
115+
if (!ciphers) {
116+
ciphers = GIT_SSL_DEFAULT_CIPHERS;
117+
}
118+
119+
if(!SSL_CTX_set_cipher_list(git__ssl_ctx, ciphers)) {
120+
SSL_CTX_free(git__ssl_ctx);
121+
git__ssl_ctx = NULL;
122+
return -1;
123+
}
111124
#endif
112125

113126
git__on_shutdown(shutdown_ssl);

src/settings.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ static int config_level_to_sysdir(int config_level)
7171
}
7272

7373
extern char *git__user_agent;
74+
extern char *git__ssl_ciphers;
7475

7576
const char *git_libgit2__user_agent()
7677
{
7778
return git__user_agent;
7879
}
7980

81+
const char *git_libgit2__ssl_ciphers()
82+
{
83+
return git__ssl_ciphers;
84+
}
85+
8086
int git_libgit2_opts(int key, ...)
8187
{
8288
int error = 0;
@@ -187,6 +193,22 @@ int git_libgit2_opts(int key, ...)
187193
git_object__strict_input_validation = (va_arg(ap, int) != 0);
188194
break;
189195

196+
case GIT_OPT_SET_SSL_CIPHERS:
197+
#ifdef GIT_OPENSSL
198+
{
199+
git__free(git__ssl_ciphers);
200+
git__ssl_ciphers = git__strdup(va_arg(ap, const char *));
201+
if (!git__ssl_ciphers) {
202+
giterr_set_oom();
203+
error = -1;
204+
}
205+
}
206+
#else
207+
giterr_set(GITERR_NET, "Cannot set custom ciphers: OpenSSL is not enabled");
208+
error = -1;
209+
#endif
210+
break;
211+
190212
default:
191213
giterr_set(GITERR_INVALID, "invalid option key");
192214
error = -1;

tests/online/badssl.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ void test_online_badssl__self_signed(void)
3636
cl_git_fail_with(GIT_ECERTIFICATE,
3737
git_clone(&g_repo, "https://self-signed.badssl.com/fake.git", "./fake", NULL));
3838
}
39+
40+
void test_online_badssl__old_cipher(void)
41+
{
42+
if (!g_has_ssl)
43+
cl_skip();
44+
45+
cl_git_fail_with(GIT_ERROR,
46+
git_clone(&g_repo, "https://rc4.badssl.com/fake.git", "./fake", NULL));
47+
}

0 commit comments

Comments
 (0)