|
21 | 21 | # include "streams/curl.h" |
22 | 22 | #endif |
23 | 23 |
|
| 24 | +#include <mbedtls/config.h> |
24 | 25 | #include <mbedtls/ssl.h> |
25 | | -#include <mbedtls/x509.h> |
26 | | -#include <mbedtls/x509_crt.h> |
27 | | -#include <mbedtls/error.h> |
| 26 | +#include <mbedtls/entropy.h> |
| 27 | +#include <mbedtls/ctr_drbg.h> |
| 28 | + |
| 29 | +#define CRT_LOC "/etc/ssl/certs" |
| 30 | + |
| 31 | +mbedtls_ssl_config *git__ssl_conf; |
| 32 | +mbedtls_entropy_context *mbedtls_entropy; |
| 33 | + |
| 34 | +/** |
| 35 | + * This function aims to clean-up the SSL context which |
| 36 | + * we allocated. |
| 37 | + */ |
| 38 | +static void shutdown_ssl(void) |
| 39 | +{ |
| 40 | + if (git__ssl_conf) { |
| 41 | + mbedtls_x509_crt_free(git__ssl_conf->ca_chain); |
| 42 | + git__free(git__ssl_conf->ca_chain); |
| 43 | + mbedtls_ctr_drbg_free(git__ssl_conf->p_rng); |
| 44 | + git__free(git__ssl_conf->p_rng); |
| 45 | + mbedtls_ssl_config_free(git__ssl_conf); |
| 46 | + git__free(git__ssl_conf); |
| 47 | + git__ssl_conf = NULL; |
| 48 | + } |
| 49 | + if (mbedtls_entropy) { |
| 50 | + mbedtls_entropy_free(mbedtls_entropy); |
| 51 | + git__free(mbedtls_entropy); |
| 52 | + mbedtls_entropy = NULL; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +int git_mbedtls_stream_global_init(void) |
| 57 | +{ |
| 58 | + int ret; |
| 59 | + mbedtls_ctr_drbg_context *ctr_drbg = NULL; |
| 60 | + mbedtls_x509_crt *cacert = NULL; |
| 61 | + |
| 62 | + git__ssl_conf = git__malloc(sizeof(mbedtls_ssl_config)); |
| 63 | + mbedtls_ssl_config_init(git__ssl_conf); |
| 64 | + if (mbedtls_ssl_config_defaults(git__ssl_conf, |
| 65 | + MBEDTLS_SSL_IS_CLIENT, |
| 66 | + MBEDTLS_SSL_TRANSPORT_STREAM, |
| 67 | + MBEDTLS_SSL_PRESET_DEFAULT) != 0) { |
| 68 | + giterr_set(GITERR_SSL, "failed to initialize mbedTLS"); |
| 69 | + goto cleanup; |
| 70 | + } |
| 71 | + |
| 72 | + /* configure TLSv1 */ |
| 73 | + mbedtls_ssl_conf_min_version(git__ssl_conf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0); |
| 74 | + mbedtls_ssl_conf_authmode(git__ssl_conf, MBEDTLS_SSL_VERIFY_REQUIRED); |
| 75 | + |
| 76 | + /* Seeding the random number generator */ |
| 77 | + mbedtls_entropy = git__malloc(sizeof(mbedtls_entropy_context)); |
| 78 | + mbedtls_entropy_init(mbedtls_entropy); |
| 79 | + |
| 80 | + ctr_drbg = git__malloc(sizeof(mbedtls_ctr_drbg_context)); |
| 81 | + mbedtls_ctr_drbg_init(ctr_drbg); |
| 82 | + if (mbedtls_ctr_drbg_seed(ctr_drbg, |
| 83 | + mbedtls_entropy_func, |
| 84 | + mbedtls_entropy, NULL, 0) != 0) { |
| 85 | + giterr_set(GITERR_SSL, "failed to initialize mbedTLS entropy pool"); |
| 86 | + goto cleanup; |
| 87 | + } |
| 88 | + |
| 89 | + mbedtls_ssl_conf_rng(git__ssl_conf, mbedtls_ctr_drbg_random, ctr_drbg); |
| 90 | + |
| 91 | + // set root certificates |
| 92 | + cacert = git__malloc(sizeof(mbedtls_x509_crt)); |
| 93 | + mbedtls_x509_crt_init(cacert); |
| 94 | + ret = mbedtls_x509_crt_parse_path(cacert, CRT_LOC); |
| 95 | + if (ret) { |
| 96 | + giterr_set(GITERR_SSL, "failed to load CA certificates: %d", ret); |
| 97 | + goto cleanup; |
| 98 | + } |
| 99 | + |
| 100 | + mbedtls_ssl_conf_ca_chain(git__ssl_conf, cacert, NULL); |
| 101 | + |
| 102 | + git__on_shutdown(shutdown_ssl); |
| 103 | + |
| 104 | + return 0; |
| 105 | + |
| 106 | +cleanup: |
| 107 | + mbedtls_x509_crt_free(cacert); |
| 108 | + git__free(cacert); |
| 109 | + mbedtls_ctr_drbg_free(ctr_drbg); |
| 110 | + git__free(ctr_drbg); |
| 111 | + mbedtls_ssl_config_free(git__ssl_conf); |
| 112 | + git__free(git__ssl_conf); |
| 113 | + git__ssl_conf = NULL; |
| 114 | + |
| 115 | + return -1; |
| 116 | +} |
28 | 117 |
|
29 | 118 | mbedtls_ssl_config *git__ssl_conf; |
30 | 119 |
|
@@ -346,6 +435,11 @@ int git_mbedtls__set_cert_location(const char *file, const char *path) |
346 | 435 |
|
347 | 436 | #include "stream.h" |
348 | 437 |
|
| 438 | +int git_mbedtls_stream_global_init(void) |
| 439 | +{ |
| 440 | + return 0; |
| 441 | +} |
| 442 | + |
349 | 443 | int git_mbedtls_stream_new(git_stream **out, const char *host, const char *port) |
350 | 444 | { |
351 | 445 | GIT_UNUSED(out); |
|
0 commit comments