|
22 | 22 |
|
23 | 23 | #include <mbedtls/config.h>
|
24 | 24 | #include <mbedtls/ssl.h>
|
| 25 | +#include <mbedtls/error.h> |
25 | 26 | #include <mbedtls/entropy.h>
|
26 | 27 | #include <mbedtls/ctr_drbg.h>
|
27 | 28 |
|
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" |
29 | 36 |
|
30 | 37 | mbedtls_ssl_config *git__ssl_conf;
|
31 | 38 | mbedtls_entropy_context *mbedtls_entropy;
|
@@ -55,9 +62,13 @@ static void shutdown_ssl(void)
|
55 | 62 | }
|
56 | 63 | }
|
57 | 64 |
|
| 65 | +int git_mbedtls__set_cert_location(const char *file, const char *path); |
| 66 | + |
58 | 67 | int git_mbedtls_stream_global_init(void)
|
59 | 68 | {
|
60 |
| - int ret; |
| 69 | + int found, isdir; |
| 70 | + char *crtpath; |
| 71 | + struct stat statbuf; |
61 | 72 | mbedtls_ctr_drbg_context *ctr_drbg = NULL;
|
62 | 73 |
|
63 | 74 | int *ciphers_list = NULL;
|
@@ -115,16 +126,33 @@ int git_mbedtls_stream_global_init(void)
|
115 | 126 |
|
116 | 127 | mbedtls_ssl_conf_rng(git__ssl_conf, mbedtls_ctr_drbg_random, ctr_drbg);
|
117 | 128 |
|
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); |
125 | 147 | }
|
126 | 148 |
|
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 | + } |
128 | 156 |
|
129 | 157 | git__on_shutdown(shutdown_ssl);
|
130 | 158 |
|
@@ -445,16 +473,26 @@ int git_mbedtls__set_cert_location(const char *file, const char *path)
|
445 | 473 | {
|
446 | 474 | int ret = 0;
|
447 | 475 | 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); |
452 | 483 | }
|
453 |
| - if (ret != 0) { |
| 484 | + if (!ret) { |
| 485 | + mbedtls_x509_crt_free(cacert); |
| 486 | + git__free(cacert); |
454 | 487 | 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); |
456 | 489 | return -1;
|
457 | 490 | }
|
| 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 | + |
458 | 496 | return 0;
|
459 | 497 | }
|
460 | 498 |
|
|
0 commit comments