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

Skip to content

Commit 27c3d68

Browse files
wildarttiennou
authored andcommitted
mbedtls: load default CA certificates
1 parent ca05c83 commit 27c3d68

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

src/streams/mbedtls.c

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@
2222

2323
#include <mbedtls/config.h>
2424
#include <mbedtls/ssl.h>
25+
#include <mbedtls/error.h>
2526
#include <mbedtls/entropy.h>
2627
#include <mbedtls/ctr_drbg.h>
2728

28-
#define CRT_LOC "/etc/ssl/certs"
29+
#ifndef OPENSSLDIR
30+
# define OPENSSLDIR "/usr/lib/ssl"
31+
#endif
32+
#define X509_CERT_DIR OPENSSLDIR "/certs"
33+
#define X509_CERT_FILE OPENSSLDIR "/cert.pem"
34+
#define X509_CERT_DIR_EVP "SSL_CERT_DIR"
35+
#define X509_CERT_FILE_EVP "SSL_CERT_FILE"
2936

3037
mbedtls_ssl_config *git__ssl_conf;
3138
mbedtls_entropy_context *mbedtls_entropy;
@@ -55,9 +62,13 @@ static void shutdown_ssl(void)
5562
}
5663
}
5764

65+
int git_mbedtls__set_cert_location(const char *file, const char *path);
66+
5867
int git_mbedtls_stream_global_init(void)
5968
{
60-
int ret;
69+
int found, isdir;
70+
char *crtpath;
71+
struct stat statbuf;
6172
mbedtls_ctr_drbg_context *ctr_drbg = NULL;
6273

6374
int *ciphers_list = NULL;
@@ -115,16 +126,33 @@ int git_mbedtls_stream_global_init(void)
115126

116127
mbedtls_ssl_conf_rng(git__ssl_conf, mbedtls_ctr_drbg_random, ctr_drbg);
117128

118-
// set root certificates
119-
cacert = git__malloc(sizeof(mbedtls_x509_crt));
120-
mbedtls_x509_crt_init(cacert);
121-
ret = mbedtls_x509_crt_parse_path(cacert, CRT_LOC);
122-
if (ret) {
123-
giterr_set(GITERR_SSL, "failed to load CA certificates: %d", ret);
124-
goto cleanup;
129+
// find locations for which CA certificates
130+
isdir = 0;
131+
crtpath = getenv(X509_CERT_FILE_EVP);
132+
found = crtpath != NULL && stat(crtpath, &statbuf) == 0 && S_ISREG(statbuf.st_mode);
133+
if (!found) {
134+
isdir = 1;
135+
crtpath = getenv(X509_CERT_DIR_EVP);
136+
found = crtpath != NULL && stat(crtpath, &statbuf) == 0 && S_ISDIR(statbuf.st_mode);
137+
}
138+
if (!found) {
139+
isdir = 0;
140+
crtpath = X509_CERT_FILE;
141+
found = crtpath != NULL && stat(crtpath, &statbuf) == 0 && S_ISREG(statbuf.st_mode);
142+
}
143+
if (!found) {
144+
isdir = 1;
145+
crtpath = X509_CERT_DIR;
146+
found = crtpath != NULL && stat(crtpath, &statbuf) == 0 && S_ISDIR(statbuf.st_mode);
125147
}
126148

127-
mbedtls_ssl_conf_ca_chain(git__ssl_conf, cacert, NULL);
149+
// set root certificates
150+
if (found) {
151+
if (isdir)
152+
git_mbedtls__set_cert_location(NULL, crtpath);
153+
else
154+
git_mbedtls__set_cert_location(crtpath, NULL);
155+
}
128156

129157
git__on_shutdown(shutdown_ssl);
130158

@@ -445,16 +473,26 @@ int git_mbedtls__set_cert_location(const char *file, const char *path)
445473
{
446474
int ret = 0;
447475
char errbuf[512];
448-
if (!file) {
449-
ret = mbedtls_x509_crt_parse_file(git__ssl_conf->ca_chain, file);
450-
} else if (!path) {
451-
ret = mbedtls_x509_crt_parse_path(git__ssl_conf->ca_chain, path);
476+
mbedtls_x509_crt *cacert;
477+
cacert = git__malloc(sizeof(mbedtls_x509_crt));
478+
mbedtls_x509_crt_init(cacert);
479+
if (file) {
480+
ret = mbedtls_x509_crt_parse_file(cacert, file);
481+
} else if (path) {
482+
ret = mbedtls_x509_crt_parse_path(cacert, path);
452483
}
453-
if (ret != 0) {
484+
if (!ret) {
485+
mbedtls_x509_crt_free(cacert);
486+
git__free(cacert);
454487
mbedtls_strerror( ret, errbuf, 512 );
455-
giterr_set(GITERR_NET, "SSL error: %d - %s", ret, errbuf);
488+
giterr_set(GITERR_SSL, "failed to load CA certificates : %s (%d)", errbuf, ret);
456489
return -1;
457490
}
491+
492+
mbedtls_x509_crt_free(git__ssl_conf->ca_chain);
493+
git__free(git__ssl_conf->ca_chain);
494+
mbedtls_ssl_conf_ca_chain(git__ssl_conf, cacert, NULL);
495+
458496
return 0;
459497
}
460498

0 commit comments

Comments
 (0)