-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Support mbedTLS #3935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support mbedTLS #3935
Changes from 1 commit
04a2b1d
cfc27a3
d44d020
a5958d0
ad2b2e2
101ab46
46db15f
369d23a
4f8968b
cc156e4
f814681
6b556a6
a47a6bc
05ca19a
77cdc58
dab8d43
81555f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,9 +110,10 @@ ENDIF() | |
|
||
IF (HAVE_STRUCT_STAT_NSEC OR WIN32) | ||
OPTION( USE_NSEC "Care about sub-second file mtimes and ctimes" ON ) | ||
ENDIF() | ||
|
||
IF (NOT USE_OPENSSL) | ||
OPTION( USE_MBEDTLS "Link with and use mbedtls library" ON ) | ||
OPTION( USE_MBEDTLS "Link with and use mbedTLS library" ON ) | ||
ENDIF() | ||
|
||
# This variable will contain the libraries we need to put into | ||
|
@@ -286,7 +287,7 @@ ELSE () | |
FIND_PACKAGE(OpenSSL) | ||
ENDIF () | ||
|
||
IF (NOT AMIGA AND USE_MBEDTLS AND NOT USE_OPENSSL) | ||
IF (NOT AMIGA AND USE_MBEDTLS) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is amiga relevant? wonder why openssl is gated on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that it is relevant for the Amiga port right now. However, the identification of OpenSSL is disabled on Amiga, because FIND_PACKAGE produced wrong results when cross compiling (it added wrong (i.e., native) include paths), at least, if I remember it correctly. Instead, it is assumed that OpenSSL is available on this platform in the (cross) compiler's standard include path and libgit2 is only statically linked. In essence, special care for Amiga doesn't need to be done in new stuff. From time to time, I'll take care of it and submit pull requests. |
||
FIND_PACKAGE(mbedTLS) | ||
ENDIF () | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ extern SSL_CTX *git__ssl_ctx; | |
#endif | ||
|
||
#ifdef GIT_MBEDTLS | ||
# include "mbedtls/platform.h" | ||
# include "mbedtls/ssl.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these specified as relative imports? Why woldn't |
||
extern mbedtls_ssl_config *git__ssl_conf; | ||
#endif | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,116 @@ | |
# include "curl_stream.h" | ||
#endif | ||
|
||
#include <mbedtls/ssl.h> | ||
#include "mbedtls/config.h" | ||
#include <mbedtls/x509.h> | ||
#include <mbedtls/x509_crt.h> | ||
#include <mbedtls/error.h> | ||
#include "mbedtls/net.h" | ||
#include "mbedtls/debug.h" | ||
#include "mbedtls/entropy.h" | ||
#include "mbedtls/ctr_drbg.h" | ||
#include "mbedtls/error.h" | ||
#include "mbedtls/certs.h" | ||
|
||
#define CRT_LOC "/etc/ssl/certs" | ||
|
||
mbedtls_ssl_config *git__ssl_conf; | ||
mbedtls_entropy_context *mbedtls_entropy; | ||
|
||
#define GIT_SSL_DEFAULT_CIPHERS "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-DSS-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-DSS-AES128-SHA256:DHE-DSS-AES256-SHA256:DHE-DSS-AES128-SHA:DHE-DSS-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA" | ||
|
||
/** | ||
* This function aims to clean-up the SSL context which | ||
* we allocated. | ||
*/ | ||
static void shutdown_ssl(void) | ||
{ | ||
if (git__ssl_conf) { | ||
mbedtls_x509_crt_free(git__ssl_conf->ca_chain); | ||
git__free(git__ssl_conf->ca_chain); | ||
mbedtls_ctr_drbg_free(git__ssl_conf->p_rng); | ||
git__free(git__ssl_conf->p_rng); | ||
mbedtls_ssl_config_free(git__ssl_conf); | ||
git__free(git__ssl_conf); | ||
git__ssl_conf = NULL; | ||
} | ||
if (mbedtls_entropy) { | ||
mbedtls_entropy_free(mbedtls_entropy); | ||
git__free(mbedtls_entropy); | ||
mbedtls_entropy = NULL; | ||
} | ||
} | ||
|
||
int git_mbedtls_stream_global_init(void) | ||
{ | ||
int ret; | ||
// const int *cipherids; | ||
// const char *ciphers = git_libgit2__ssl_ciphers(); | ||
|
||
mbedtls_ctr_drbg_context *ctr_drbg; | ||
mbedtls_x509_crt *cacert; | ||
|
||
mbedtls_entropy = git__malloc(sizeof(mbedtls_entropy_context)); | ||
mbedtls_entropy_init(mbedtls_entropy); | ||
|
||
// Seeding the random number generator | ||
ctr_drbg = git__malloc(sizeof(mbedtls_ctr_drbg_context)); | ||
mbedtls_ctr_drbg_init(ctr_drbg); | ||
if (mbedtls_ctr_drbg_seed(ctr_drbg, | ||
mbedtls_entropy_func, | ||
mbedtls_entropy, NULL, 0) != 0) { | ||
mbedtls_ctr_drbg_free(ctr_drbg); | ||
mbedtls_entropy_free(mbedtls_entropy); | ||
git__free(ctr_drbg); | ||
git__free(mbedtls_entropy); | ||
return -1; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are we seeding the random number generator for crypto? This is something the library should be asking the operating system whenever it needs a random number. |
||
|
||
// configure TLSv1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment implies that we're setting the minimum SSL/TLS version to be TLS, but there's no code to do this. The documentation for mbedtls says the default is TLS 1.0 so leaving the defaults should be OK, but the comment should be saying explicitly how we're getting the minimum version. |
||
git__ssl_conf = git__malloc(sizeof(mbedtls_ssl_config)); | ||
mbedtls_ssl_config_init(git__ssl_conf); | ||
if ( mbedtls_ssl_config_defaults(git__ssl_conf, | ||
MBEDTLS_SSL_IS_CLIENT, | ||
MBEDTLS_SSL_TRANSPORT_STREAM, | ||
MBEDTLS_SSL_PRESET_DEFAULT ) != 0) { | ||
mbedtls_ctr_drbg_free(ctr_drbg); | ||
git__free(ctr_drbg); | ||
mbedtls_ssl_config_free(git__ssl_conf); | ||
git__free(git__ssl_conf); | ||
git__ssl_conf = NULL; | ||
return -1; | ||
} | ||
|
||
mbedtls_ssl_conf_authmode(git__ssl_conf, MBEDTLS_SSL_VERIFY_REQUIRED); | ||
mbedtls_ssl_conf_rng(git__ssl_conf, mbedtls_ctr_drbg_random, ctr_drbg); | ||
|
||
// set the list of allowed ciphersuites | ||
// if (!ciphers) { | ||
// cipherids = mbedtls_ssl_list_ciphersuites(); | ||
// } | ||
// mbedtls_ssl_conf_ciphersuites(git__ssl_conf, cipherids); | ||
|
||
// set root certificates | ||
cacert = git__malloc(sizeof(mbedtls_x509_crt)); | ||
mbedtls_x509_crt_init(cacert); | ||
ret = mbedtls_x509_crt_parse_path(cacert, CRT_LOC); | ||
if (ret) { | ||
giterr_set(GITERR_SSL, "failed to load CA certificates: %d", ret); | ||
mbedtls_x509_crt_free(cacert); | ||
git__free(cacert); | ||
mbedtls_ctr_drbg_free(ctr_drbg); | ||
git__free(ctr_drbg); | ||
mbedtls_ssl_config_free(git__ssl_conf); | ||
git__free(git__ssl_conf); | ||
git__ssl_conf = NULL; | ||
} else { | ||
mbedtls_ssl_conf_ca_chain(git__ssl_conf, cacert, NULL); | ||
} | ||
|
||
git__on_shutdown(shutdown_ssl); | ||
|
||
return 0; | ||
} | ||
|
||
static int bio_read(void *b, unsigned char *buf, size_t len) | ||
{ | ||
|
@@ -218,7 +324,7 @@ static int mbedtls_set_proxy(git_stream *stream, const char *proxy_url) | |
return git_stream_set_proxy(st->io, proxy_url); | ||
} | ||
|
||
ssize_t mbedtls_write(git_stream *stream, const char *data, size_t len, int flags) | ||
ssize_t mbedtls_stream_write(git_stream *stream, const char *data, size_t len, int flags) | ||
{ | ||
mbedtls_stream *st = (mbedtls_stream *) stream; | ||
int ret; | ||
|
@@ -232,7 +338,7 @@ ssize_t mbedtls_write(git_stream *stream, const char *data, size_t len, int flag | |
return ret; | ||
} | ||
|
||
ssize_t mbedtls_read(git_stream *stream, void *data, size_t len) | ||
ssize_t mbedtls_stream_read(git_stream *stream, void *data, size_t len) | ||
{ | ||
mbedtls_stream *st = (mbedtls_stream *) stream; | ||
int ret; | ||
|
@@ -243,7 +349,7 @@ ssize_t mbedtls_read(git_stream *stream, void *data, size_t len) | |
return ret; | ||
} | ||
|
||
int mbedtls_close(git_stream *stream) | ||
int mbedtls_stream_close(git_stream *stream) | ||
{ | ||
mbedtls_stream *st = (mbedtls_stream *) stream; | ||
int ret = 0; | ||
|
@@ -256,7 +362,7 @@ int mbedtls_close(git_stream *stream) | |
return git_stream_close(st->io); | ||
} | ||
|
||
void mbedtls_free(git_stream *stream) | ||
void mbedtls_stream_free(git_stream *stream) | ||
{ | ||
mbedtls_stream *st = (mbedtls_stream *) stream; | ||
|
||
|
@@ -302,10 +408,10 @@ int git_mbedtls_stream_new(git_stream **out, const char *host, const char *port) | |
st->parent.connect = mbedtls_connect; | ||
st->parent.certificate = mbedtls_certificate; | ||
st->parent.set_proxy = mbedtls_set_proxy; | ||
st->parent.read = mbedtls_read; | ||
st->parent.write = mbedtls_write; | ||
st->parent.close = mbedtls_close; | ||
st->parent.free = mbedtls_free; | ||
st->parent.read = mbedtls_stream_read; | ||
st->parent.write = mbedtls_stream_write; | ||
st->parent.close = mbedtls_stream_close; | ||
st->parent.free = mbedtls_stream_free; | ||
|
||
*out = (git_stream *) st; | ||
return 0; | ||
|
@@ -314,14 +420,20 @@ int git_mbedtls_stream_new(git_stream **out, const char *host, const char *port) | |
#else | ||
|
||
#include "stream.h" | ||
#include "git2/sys/openssl.h" | ||
|
||
int git_mbedtls_stream_global_init(void) | ||
{ | ||
return 0; | ||
} | ||
|
||
int git_mbedtls_stream_new(git_stream **out, const char *host, const char *port) | ||
{ | ||
GIT_UNUSED(out); | ||
GIT_UNUSED(host); | ||
GIT_UNUSED(port); | ||
|
||
giterr_set(GITERR_SSL, "mbedtls is not supported in this version"); | ||
giterr_set(GITERR_SSL, "mbedTLS is not supported in this version"); | ||
return -1; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should default this to off, otherwise on darwin it'll try to use both the apple crypto and mbedtls if found
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it needs to default to on, but it needs to get wrapped in the same "not darwin". Will add a patch to fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why should it default to on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it might be possible to use openssl or mbedtls on darwin instead of the apple crypto, but that might not be supported here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should default to on when not on Darwin and not using OpenSSL, to attempt to autodetect mbedTLS if it doesn't find OpenSSL. If it defaulted to off, you'd have to explicitly enable mbedTLS even if you have mbedTLS installed and OpenSSL not installed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right that makes sense, but that's what should determine the default, not whether or not the option gets defined at all? though since the openssl option isn't defined at all for darwin, at least this would be consistent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right; consistency seems preferable here. It might make sense for a future patch to go through CMakeLists.txt, define all options unconditionally, and change all the conditionals to just set the default values. But I don't think this patch should make that unrelated change.