Skip to content

Commit 60e1ad9

Browse files
committed
mbedtls: add global initialization
1 parent 6c6be3c commit 60e1ad9

File tree

3 files changed

+102
-4
lines changed

3 files changed

+102
-4
lines changed

src/global.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "filter.h"
1313
#include "merge_driver.h"
1414
#include "streams/curl.h"
15+
#include "streams/mbedtls.h"
1516
#include "streams/openssl.h"
1617
#include "thread-utils.h"
1718
#include "git2/global.h"
@@ -65,7 +66,8 @@ static int init_common(void)
6566
(ret = git_merge_driver_global_init()) == 0 &&
6667
(ret = git_transport_ssh_global_init()) == 0 &&
6768
(ret = git_openssl_stream_global_init()) == 0 &&
68-
(ret = git_curl_stream_global_init()) == 0)
69+
(ret = git_curl_stream_global_init()) == 0 &&
70+
(ret = git_mbedtls_stream_global_init()) == 0)
6971
ret = git_mwindow_global_init();
7072

7173
GIT_MEMORY_BARRIER;

src/streams/mbedtls.c

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,99 @@
2121
# include "streams/curl.h"
2222
#endif
2323

24+
#include <mbedtls/config.h>
2425
#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+
}
28117

29118
mbedtls_ssl_config *git__ssl_conf;
30119

@@ -346,6 +435,11 @@ int git_mbedtls__set_cert_location(const char *file, const char *path)
346435

347436
#include "stream.h"
348437

438+
int git_mbedtls_stream_global_init(void)
439+
{
440+
return 0;
441+
}
442+
349443
int git_mbedtls_stream_new(git_stream **out, const char *host, const char *port)
350444
{
351445
GIT_UNUSED(out);

src/streams/mbedtls.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include "git2/sys/stream.h"
1313

14+
extern int git_mbedtls_stream_global_init(void);
15+
1416
extern int git_mbedtls_stream_new(git_stream **out, const char *host, const char *port);
1517

1618
extern int git_mbedtls__set_cert_location(const char *file, const char *path);

0 commit comments

Comments
 (0)