Skip to content

Commit cdb9f39

Browse files
mikezacklesethomson
authored andcommitted
mbedTLS: Fix setting certificate directory
fixes libgit2#6003
1 parent 0e04726 commit cdb9f39

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

src/libgit2.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,7 @@ int git_libgit2_opts(int key, ...)
261261
{
262262
const char *file = va_arg(ap, const char *);
263263
const char *path = va_arg(ap, const char *);
264-
if (file)
265-
error = git_mbedtls__set_cert_location(file, 0);
266-
if (error && path)
267-
error = git_mbedtls__set_cert_location(path, 1);
264+
error = git_mbedtls__set_cert_location(file, path);
268265
}
269266
#else
270267
git_error_set(GIT_ERROR_SSL, "TLS backend doesn't support certificate locations");

src/streams/mbedtls.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ static void shutdown_ssl(void)
6868
}
6969
}
7070

71-
int git_mbedtls__set_cert_location(const char *path, int is_dir);
72-
7371
int git_mbedtls_stream_global_init(void)
7472
{
7573
int loaded = 0;
@@ -148,9 +146,9 @@ int git_mbedtls_stream_global_init(void)
148146

149147
/* load default certificates */
150148
if (crtpath != NULL && stat(crtpath, &statbuf) == 0 && S_ISREG(statbuf.st_mode))
151-
loaded = (git_mbedtls__set_cert_location(crtpath, 0) == 0);
149+
loaded = (git_mbedtls__set_cert_location(crtpath, NULL) == 0);
152150
if (!loaded && crtpath != NULL && stat(crtpath, &statbuf) == 0 && S_ISDIR(statbuf.st_mode))
153-
loaded = (git_mbedtls__set_cert_location(crtpath, 1) == 0);
151+
loaded = (git_mbedtls__set_cert_location(NULL, crtpath) == 0);
154152

155153
return git_runtime_shutdown_register(shutdown_ssl);
156154

@@ -438,23 +436,22 @@ int git_mbedtls_stream_new(
438436
return error;
439437
}
440438

441-
int git_mbedtls__set_cert_location(const char *path, int is_dir)
439+
int git_mbedtls__set_cert_location(const char *file, const char *path)
442440
{
443441
int ret = 0;
444442
char errbuf[512];
445443
mbedtls_x509_crt *cacert;
446444

447-
GIT_ASSERT_ARG(path);
445+
GIT_ASSERT_ARG(file || path);
448446

449447
cacert = git__malloc(sizeof(mbedtls_x509_crt));
450448
GIT_ERROR_CHECK_ALLOC(cacert);
451449

452450
mbedtls_x509_crt_init(cacert);
453-
if (is_dir) {
451+
if (file)
452+
ret = mbedtls_x509_crt_parse_file(cacert, file);
453+
if (ret >= 0 && path)
454454
ret = mbedtls_x509_crt_parse_path(cacert, path);
455-
} else {
456-
ret = mbedtls_x509_crt_parse_file(cacert, path);
457-
}
458455
/* mbedtls_x509_crt_parse_path returns the number of invalid certs on success */
459456
if (ret < 0) {
460457
mbedtls_x509_crt_free(cacert);

src/streams/mbedtls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
extern int git_mbedtls_stream_global_init(void);
1515

1616
#ifdef GIT_MBEDTLS
17-
extern int git_mbedtls__set_cert_location(const char *path, int is_dir);
17+
extern int git_mbedtls__set_cert_location(const char *file, const char *path);
1818

1919
extern int git_mbedtls_stream_new(git_stream **out, const char *host, const char *port);
2020
extern int git_mbedtls_stream_wrap(git_stream **out, git_stream *in, const char *host);

0 commit comments

Comments
 (0)