diff --git a/.travis.yml b/.travis.yml index 9022fdec282..1205124f296 100644 --- a/.travis.yml +++ b/.travis.yml @@ -48,6 +48,16 @@ matrix: - VALGRIND=1 OPTIONS="-DBUILD_CLAR=ON -DBUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Debug" os: linux + - compiler: gcc + env: + - MBEDTLS=1 + OPTIONS="-DTHREADSAFE=ON -DCMAKE_BUILD_TYPE=Release -DUSE_OPENSSL=OFF -DMBEDTLS_ROOT_DIR=../mbedtls" + os: linux + - compiler: gcc + env: + - MBEDTLS=1 + OPTIONS="-DTHREADSAFE=OFF -DBUILD_EXAMPLES=ON -DUSE_OPENSSL=OFF -DMBEDTLS_ROOT_DIR=../mbedtls" + os: linux allow_failures: - env: COVERITY=1 - env: diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f0d5070082..6ef62fac37d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,6 +85,10 @@ IF (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin") OPTION( USE_OPENSSL "Link with and use openssl library" ON ) ENDIF() +IF (NOT USE_OPENSSL) + OPTION( USE_MBEDTLS "Link with and use mbedtls library" ON ) +ENDIF() + # This variable will contain the libraries we need to put into # libgit2.pc's Requires.private. That is, what we're linking to or # what someone who's statically linking us needs to link to. @@ -249,6 +253,10 @@ ELSE () FIND_PACKAGE(OpenSSL) ENDIF () + IF (NOT AMIGA AND USE_MBEDTLS AND NOT USE_OPENSSL) + FIND_PACKAGE(mbedTLS) + ENDIF () + IF (CURL_FOUND) ADD_DEFINITIONS(-DGIT_CURL) INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIRS}) @@ -281,6 +289,9 @@ ELSEIF (OPENSSL_FOUND AND NOT SHA1_TYPE STREQUAL "builtin") ELSE() SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} openssl") ENDIF () +ELSEIF (MBEDTLS_FOUND AND NOT SHA1_TYPE STREQUAL "builtin") + ADD_DEFINITIONS(-DMBEDTLS_SHA1) + FILE(GLOB SRC_SHA1 src/hash/hash_mbedtls.c) ELSE() FILE(GLOB SRC_SHA1 src/hash/hash_generic.c) ENDIF() @@ -506,6 +517,11 @@ IF (OPENSSL_FOUND) SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES}) ENDIF() +IF (MBEDTLS_FOUND) + ADD_DEFINITIONS(-DGIT_MBEDTLS) + INCLUDE_DIRECTORIES(${MBEDTLS_INCLUDE_DIR}) + SET(SSL_LIBRARIES ${MBEDTLS_LIBRARIES}) +ENDIF() IF (THREADSAFE) @@ -632,7 +648,7 @@ IF (BUILD_CLAR) ENDIF () ENABLE_TESTING() - IF (WINHTTP OR OPENSSL_FOUND OR SECURITY_FOUND) + IF (WINHTTP OR OPENSSL_FOUND OR SECURITY_FOUND OR MBEDTLS_FOUND) ADD_TEST(libgit2_clar libgit2_clar -ionline) ELSE () ADD_TEST(libgit2_clar libgit2_clar -v) diff --git a/cmake/Modules/FindmbedTLS.cmake b/cmake/Modules/FindmbedTLS.cmake new file mode 100644 index 00000000000..c1a68563629 --- /dev/null +++ b/cmake/Modules/FindmbedTLS.cmake @@ -0,0 +1,78 @@ +# - Try to find mbedTLS +# Once done this will define +# +# MBEDTLS_ROOT_DIR - Set this variable to the root installation of mbedTLS +# +# Read-Only variables +# MBEDTLS_FOUND - system has mbedTLS +# MBEDTLS_INCLUDE_DIR - the mbedTLS include directory +# MBEDTLS_LIBRARIES - Link these to use mbedTLS +# + +FIND_PATH(MBEDTLS_ROOT_DIR NAMES include/mbedtls/version.h) + +IF(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARIES) + # Already in cache, be silent + SET(MBEDTLS_FIND_QUIETLY TRUE) +ENDIF() +FIND_PATH(MBEDTLS_INCLUDE_DIR + NAMES mbedtls/ssl.h + PATHS + ${MBEDTLS_ROOT_DIR}/include +) +FIND_LIBRARY(mbedtls_lib + NAMES mbedtls libmbedtls libmbedx509 + PATHS + ${MBEDTLS_ROOT_DIR}/library + ${MBEDTLS_ROOT_DIR}/build/library +) +FIND_LIBRARY(mbedx509_lib + NAMES mbedx509 libmbedx509 + PATHS + ${MBEDTLS_ROOT_DIR}/library + ${MBEDTLS_ROOT_DIR}/build/library +) +FIND_LIBRARY(mbedcrypto_lib + NAMES mbedcrypto libmbedcrypto + PATHS + ${MBEDTLS_ROOT_DIR}/library + ${MBEDTLS_ROOT_DIR}/build/library +) + +IF(MBEDTLS_INCLUDE_DIR AND mbedtls_lib AND mbedx509_lib AND mbedcrypto_lib) + SET(MBEDTLS_FOUND TRUE) +ENDIF() + +IF(MBEDTLS_FOUND) + # split mbedTLS into -L and -l linker options, so we can set them for pkg-config + GET_FILENAME_COMPONENT(mbedtls_path ${mbedtls_lib} PATH) + GET_FILENAME_COMPONENT(mbedtls_name ${mbedtls_lib} NAME_WE) + GET_FILENAME_COMPONENT(mbedx509_name ${mbedx509_lib} NAME_WE) + GET_FILENAME_COMPONENT(mbedcrypto_name ${mbedcrypto_lib} NAME_WE) + STRING(REGEX REPLACE "^lib" "" mbedtls_name ${mbedtls_name}) + STRING(REGEX REPLACE "^lib" "" mbedx509_name ${mbedx509_name}) + STRING(REGEX REPLACE "^lib" "" mbedcrypto_name ${mbedcrypto_name}) + SET(MBEDTLS_LIBRARIES "-L${mbedtls_path} -l${mbedtls_name} -l${mbedx509_name} -l${mbedcrypto_name}") + + IF(NOT MBEDTLS_FIND_QUIETLY) + MESSAGE(STATUS "Found mbedTLS:") + FILE(READ ${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h MBEDTLSCONTENT) + STRING(REGEX MATCH "MBEDTLS_VERSION_STRING +\"[0-9|.]+\"" MBEDTLSMATCH ${MBEDTLSCONTENT}) + IF (MBEDTLSMATCH) + STRING(REGEX REPLACE "MBEDTLS_VERSION_STRING +\"([0-9|.]+)\"" "\\1" MBEDTLS_VERSION ${MBEDTLSMATCH}) + MESSAGE(STATUS " version ${MBEDTLS_VERSION}") + ENDIF(MBEDTLSMATCH) + MESSAGE(STATUS " TLS: ${mbedtls_lib}") + MESSAGE(STATUS " X509: ${mbedx509_lib}") + MESSAGE(STATUS " Crypto: ${mbedcrypto_lib}") + ENDIF(NOT MBEDTLS_FIND_QUIETLY) +ELSE(MBEDTLS_FOUND) + IF(MBEDTLS_FIND_REQUIRED) + MESSAGE(FATAL_ERROR "Could not find mbedTLS") + ENDIF(MBEDTLS_FIND_REQUIRED) +ENDIF(MBEDTLS_FOUND) + +MARK_AS_ADVANCED( + MBEDTLS_INCLUDE_DIR + MBEDTLS_LIBRARIES +) diff --git a/script/cibuild.sh b/script/cibuild.sh index de5df9ea897..881bec914df 100755 --- a/script/cibuild.sh +++ b/script/cibuild.sh @@ -6,6 +6,11 @@ then exit $?; fi +if [ -n "$MBEDTLS" ]; +then + ./script/mbedtls.sh; +fi + mkdir _build cd _build # shellcheck disable=SC2086 diff --git a/script/mbedtls.sh b/script/mbedtls.sh new file mode 100755 index 00000000000..fda7abc6fbc --- /dev/null +++ b/script/mbedtls.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +git clone https://github.com/ARMmbed/mbedtls.git mbedtls +cd mbedtls +git checkout mbedtls-2.1.2 +make CFLAGS='-fPIC -fpic' -j2 lib diff --git a/src/global.c b/src/global.c index 3d37ee4dec2..db702d1721c 100644 --- a/src/global.c +++ b/src/global.c @@ -28,6 +28,20 @@ static git_mutex *openssl_locks; # endif #endif +#ifdef GIT_MBEDTLS +#include "mbedtls/config.h" +#include "mbedtls/platform.h" +#include "mbedtls/ssl.h" +#include "mbedtls/entropy.h" +#include "mbedtls/ctr_drbg.h" +#include "mbedtls/certs.h" + +#define CRT_LOC "/etc/ssl/certs" + +mbedtls_ssl_config *git__ssl_conf; +mbedtls_entropy_context *mbedtls_entropy; +#endif + static git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB]; static git_atomic git__n_shutdown_callbacks; static git_atomic git__n_inits; @@ -117,6 +131,60 @@ static void init_ssl(void) git__ssl_ctx = NULL; } #endif + +#ifdef GIT_MBEDTLS + int ret = 0; + 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 (!ret && ( ret = mbedtls_ctr_drbg_seed(ctr_drbg, + mbedtls_entropy_func, + mbedtls_entropy, NULL, 0) ) != 0) { + mbedtls_ctr_drbg_free(ctr_drbg); + git__free(ctr_drbg); + } + + // Configure TLSv1 + if (!ret) { + git__ssl_conf = git__malloc(sizeof(mbedtls_ssl_config)); + mbedtls_ssl_config_init(git__ssl_conf); + if ( (ret = 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; + } else { + mbedtls_ssl_conf_authmode(git__ssl_conf, MBEDTLS_SSL_VERIFY_REQUIRED); + mbedtls_ssl_conf_rng(git__ssl_conf, mbedtls_ctr_drbg_random, ctr_drbg); + + 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); + } + } + } +#endif } /** @@ -131,6 +199,22 @@ static void uninit_ssl(void) git__ssl_ctx = NULL; } #endif +#ifdef GIT_MBEDTLS + 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; + } +#endif } int git_openssl_set_locking(void) diff --git a/src/global.h b/src/global.h index 37e909ac6f7..c9fea955c42 100644 --- a/src/global.h +++ b/src/global.h @@ -23,6 +23,11 @@ typedef struct { extern SSL_CTX *git__ssl_ctx; #endif +#ifdef GIT_MBEDTLS +#include "mbedtls/ssl.h" +extern mbedtls_ssl_config *git__ssl_conf; +#endif + git_global_st *git__global_state(void); extern git_mutex git__mwindow_mutex; diff --git a/src/hash.h b/src/hash.h index 0bc02a8a995..958d23bacef 100644 --- a/src/hash.h +++ b/src/hash.h @@ -20,6 +20,8 @@ void git_hash_ctx_cleanup(git_hash_ctx *ctx); # include "hash/hash_common_crypto.h" #elif defined(OPENSSL_SHA1) # include "hash/hash_openssl.h" +#elif defined(MBEDTLS_SHA1) +# include "hash/hash_mbedtls.h" #elif defined(WIN32_SHA1) # include "hash/hash_win32.h" #else diff --git a/src/hash/hash_mbedtls.c b/src/hash/hash_mbedtls.c new file mode 100644 index 00000000000..a19d7630827 --- /dev/null +++ b/src/hash/hash_mbedtls.c @@ -0,0 +1,38 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" +#include "hash.h" +#include "hash/hash_mbedtls.h" + +void git_hash_ctx_cleanup(git_hash_ctx *ctx) +{ + assert(ctx); + mbedtls_sha1_free(&ctx->c); +} + +int git_hash_init(git_hash_ctx *ctx) +{ + assert(ctx); + mbedtls_sha1_init(&ctx->c); + mbedtls_sha1_starts(&ctx->c); + return 0; +} + +int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) +{ + assert(ctx); + mbedtls_sha1_update(&ctx->c, data, len); + return 0; +} + +int git_hash_final(git_oid *out, git_hash_ctx *ctx) +{ + assert(ctx); + mbedtls_sha1_finish(&ctx->c, out->id); + return 0; +} diff --git a/src/hash/hash_mbedtls.h b/src/hash/hash_mbedtls.h new file mode 100644 index 00000000000..e50d2953442 --- /dev/null +++ b/src/hash/hash_mbedtls.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_hash_mbedtld_h__ +#define INCLUDE_hash_mbedtld_h__ + +#include + +struct git_hash_ctx { + mbedtls_sha1_context c; +}; + +#define git_hash_global_init() 0 +#define git_hash_ctx_init(ctx) git_hash_init(ctx) + +#endif /* INCLUDE_hash_mbedtld_h__ */ \ No newline at end of file diff --git a/src/mbedtls_stream.c b/src/mbedtls_stream.c new file mode 100644 index 00000000000..24b8fda10b6 --- /dev/null +++ b/src/mbedtls_stream.c @@ -0,0 +1,329 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifdef GIT_MBEDTLS + +#include + +#include "global.h" +#include "stream.h" +#include "socket_stream.h" +#include "git2/transport.h" + +#ifdef GIT_CURL +# include "curl_stream.h" +#endif + +#include +#include +#include +#include + +static int bio_read(void *b, unsigned char *buf, size_t len) +{ + git_stream *io = (git_stream *) b; + return (int) git_stream_read(io, buf, len); +} + +static int bio_write(void *b, const unsigned char *buf, size_t len) +{ + git_stream *io = (git_stream *) b; + return (int) git_stream_write(io, (const char *)buf, len, 0); +} + +static int ssl_set_error(mbedtls_ssl_context *ssl, int error) +{ + char errbuf[512]; + int ret = -1; + + assert(error != MBEDTLS_ERR_SSL_WANT_READ); + assert(error != MBEDTLS_ERR_SSL_WANT_WRITE); + + if (error != 0) + mbedtls_strerror( error, errbuf, 512 ); + + switch(error){ + case 0: + giterr_set(GITERR_SSL, "SSL error: unknown error"); + break; + case MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: + giterr_set(GITERR_SSL, "SSL error: %x[%x] - %s", error, ssl->session_negotiate->verify_result, errbuf); + ret = GIT_ECERTIFICATE; + break; + default: + giterr_set(GITERR_SSL, "SSL error: %x - %s", error, errbuf); + } + + return ret; +} + +static int ssl_teardown(mbedtls_ssl_context *ssl) +{ + int ret = 0; + + ret = mbedtls_ssl_close_notify(ssl); + if (ret < 0) + ret = ssl_set_error(ssl, ret); + + mbedtls_ssl_free(ssl); + return ret; +} + +static int check_host_name(const char *name, const char *host) +{ + if (!strcasecmp(name, host)) + return 0; + + if (gitno__match_host(name, host) < 0) + return -1; + + return 0; +} + +static int verify_server_cert(mbedtls_ssl_context *ssl, const char *host) +{ + const mbedtls_x509_crt *cert; + const mbedtls_x509_sequence *alts; + int ret, matched = -1; + size_t sn_size = 512; + char subject_name[sn_size], alt_name[sn_size]; + + + if (( ret = mbedtls_ssl_get_verify_result(ssl) ) != 0) { + char vrfy_buf[512]; + mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", ret ); + giterr_set(GITERR_SSL, "The SSL certificate is invalid: %s", vrfy_buf); + return GIT_ECERTIFICATE; + } + + cert = mbedtls_ssl_get_peer_cert(ssl); + if (!cert) { + giterr_set(GITERR_SSL, "the server did not provide a certificate"); + return -1; + } + + /* Check the alternative names */ + alts = &cert->subject_alt_names; + while (alts != NULL && matched != 1) { + // Buffer is too small + if( alts->buf.len >= sn_size ) + goto on_error; + + memcpy(alt_name, alts->buf.p, alts->buf.len); + alt_name[alts->buf.len] = '\0'; + + if (!memchr(alt_name, '\0', alts->buf.len)) { + if (check_host_name(alt_name, host) < 0) + matched = 0; + else + matched = 1; + } + + alts = alts->next; + } + if (matched == 0) + goto cert_fail_name; + + if (matched == 1) + return 0; + + /* If no alternative names are available, check the common name */ + ret = mbedtls_x509_dn_gets(subject_name, sn_size, &cert->subject); + if (ret == 0) + goto on_error; + if (memchr(subject_name, '\0', ret)) + goto cert_fail_name; + + if (check_host_name(subject_name, host) < 0) + goto cert_fail_name; + + return 0; + +on_error: + return ssl_set_error(ssl, 0); + +cert_fail_name: + giterr_set(GITERR_SSL, "hostname does not match certificate"); + return GIT_ECERTIFICATE; +} + +typedef struct { + git_stream parent; + git_stream *io; + bool connected; + char *host; + mbedtls_ssl_context *ssl; + git_cert_x509 cert_info; +} mbedtls_stream; + + +int mbedtls_connect(git_stream *stream) +{ + int ret; + mbedtls_stream *st = (mbedtls_stream *) stream; + + if ((ret = git_stream_connect(st->io)) < 0) + return ret; + + st->connected = true; + + mbedtls_ssl_set_hostname(st->ssl, st->host); + + mbedtls_ssl_set_bio(st->ssl, st->io, bio_write, bio_read, NULL); + + if ((ret = mbedtls_ssl_handshake(st->ssl)) != 0) + return ssl_set_error(st->ssl, ret); + + return verify_server_cert(st->ssl, st->host); +} + +int mbedtls_certificate(git_cert **out, git_stream *stream) +{ + unsigned char *encoded_cert; + mbedtls_stream *st = (mbedtls_stream *) stream; + + const mbedtls_x509_crt *cert = mbedtls_ssl_get_peer_cert(st->ssl); + if (!cert) { + giterr_set(GITERR_SSL, "the server did not provide a certificate"); + return -1; + } + + /* Retrieve the length of the certificate first */ + if (cert->raw.len == 0) { + giterr_set(GITERR_NET, "failed to retrieve certificate information"); + return -1; + } + + encoded_cert = git__malloc(cert->raw.len); + GITERR_CHECK_ALLOC(encoded_cert); + memcpy(encoded_cert, cert->raw.p, cert->raw.len); + + st->cert_info.parent.cert_type = GIT_CERT_X509; + st->cert_info.data = encoded_cert; + st->cert_info.len = cert->raw.len; + + *out = &st->cert_info.parent; + + return 0; +} + +static int mbedtls_set_proxy(git_stream *stream, const char *proxy_url) +{ + mbedtls_stream *st = (mbedtls_stream *) stream; + + return git_stream_set_proxy(st->io, proxy_url); +} + +ssize_t mbedtls_write(git_stream *stream, const char *data, size_t len, int flags) +{ + mbedtls_stream *st = (mbedtls_stream *) stream; + int ret; + + GIT_UNUSED(flags); + + if ((ret = mbedtls_ssl_write(st->ssl, (const unsigned char *)data, len)) <= 0) { + return ssl_set_error(st->ssl, ret); + } + + return ret; +} + +ssize_t mbedtls_read(git_stream *stream, void *data, size_t len) +{ + mbedtls_stream *st = (mbedtls_stream *) stream; + int ret; + + if ((ret = mbedtls_ssl_read(st->ssl, (unsigned char *)data, len)) <= 0) + ssl_set_error(st->ssl, ret); + + return ret; +} + +int mbedtls_close(git_stream *stream) +{ + mbedtls_stream *st = (mbedtls_stream *) stream; + int ret = 0; + + if (st->connected && (ret = ssl_teardown(st->ssl)) != 0) + return -1; + + st->connected = false; + + return git_stream_close(st->io); +} + +void mbedtls_free(git_stream *stream) +{ + mbedtls_stream *st = (mbedtls_stream *) stream; + + git__free(st->host); + git__free(st->cert_info.data); + git_stream_free(st->io); + git__free(st->ssl); + git__free(st); +} + +int git_mbedtls_stream_new(git_stream **out, const char *host, const char *port) +{ + int error; + mbedtls_stream *st; + + st = git__calloc(1, sizeof(mbedtls_stream)); + GITERR_CHECK_ALLOC(st); + +#ifdef GIT_CURL + error = git_curl_stream_new(&st->io, host, port); +#else + error = git_socket_stream_new(&st->io, host, port); +#endif + + if (error < 0) + return error; + + st->ssl = git__malloc(sizeof(mbedtls_ssl_context)); + GITERR_CHECK_ALLOC(st->ssl); + mbedtls_ssl_init(st->ssl); + if( (error = mbedtls_ssl_setup(st->ssl, git__ssl_conf)) != 0 ) { + mbedtls_ssl_free(st->ssl); + giterr_set(GITERR_SSL, "failed to create ssl object"); + return -1; + } + + st->host = git__strdup(host); + GITERR_CHECK_ALLOC(st->host); + + st->parent.version = GIT_STREAM_VERSION; + st->parent.encrypted = 1; + st->parent.proxy_support = git_stream_supports_proxy(st->io); + 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; + + *out = (git_stream *) st; + return 0; +} + +#else + +#include "stream.h" + +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"); + return -1; +} + +#endif + diff --git a/src/mbedtls_stream.h b/src/mbedtls_stream.h new file mode 100644 index 00000000000..170b10a2e49 --- /dev/null +++ b/src/mbedtls_stream.h @@ -0,0 +1,14 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_mbedtls_stream_h__ +#define INCLUDE_mbedtls_stream_h__ + +#include "git2/sys/stream.h" + +extern int git_mbedtls_stream_new(git_stream **out, const char *host, const char *port); + +#endif diff --git a/src/settings.c b/src/settings.c index 2097ca31419..62c1a31ea8c 100644 --- a/src/settings.c +++ b/src/settings.c @@ -9,6 +9,10 @@ # include #endif +#ifdef GIT_MBEDTLS +# include +#endif + #include #include "common.h" #include "sysdir.h" @@ -148,8 +152,25 @@ int git_libgit2_opts(int key, ...) error = -1; } } +#elif GIT_MBEDTLS + { + const char *file = va_arg(ap, const char *); + const char *path = va_arg(ap, const char *); + int ret = 0; + char errbuf[512]; + if (!file) { + ret = mbedtls_x509_crt_parse_file(git__ssl_conf->ca_chain, file); + } else if (!path) { + ret = mbedtls_x509_crt_parse_path(git__ssl_conf->ca_chain, path); + } + if (ret != 0) { + mbedtls_strerror( ret, errbuf, 512 ); + giterr_set(GITERR_NET, "SSL error: %d - %s", ret, errbuf); + error = -1; + } + } #else - giterr_set(GITERR_NET, "Cannot set certificate locations: OpenSSL is not enabled"); + giterr_set(GITERR_NET, "Cannot set certificate locations: OpenSSL or mbedTLS is not enabled"); error = -1; #endif break; diff --git a/src/tls_stream.c b/src/tls_stream.c index 39a8ce343b3..9a7046a189e 100644 --- a/src/tls_stream.c +++ b/src/tls_stream.c @@ -9,6 +9,7 @@ #include "common.h" #include "openssl_stream.h" +#include "mbedtls_stream.h" #include "stransport_stream.h" int git_tls_stream_new(git_stream **out, const char *host, const char *port) @@ -17,6 +18,8 @@ int git_tls_stream_new(git_stream **out, const char *host, const char *port) return git_stransport_stream_new(out, host, port); #elif defined(GIT_OPENSSL) return git_openssl_stream_new(out, host, port); +#elif defined(GIT_MBEDTLS) + return git_mbedtls_stream_new(out, host, port); #else GIT_UNUSED(out); GIT_UNUSED(host); diff --git a/src/transport.c b/src/transport.c index 5c65c7c06ce..b936a501634 100644 --- a/src/transport.c +++ b/src/transport.c @@ -29,7 +29,7 @@ static transport_definition local_transport_definition = { "file://", git_transp static transport_definition transports[] = { { "git://", git_transport_smart, &git_subtransport_definition }, { "http://", git_transport_smart, &http_subtransport_definition }, -#if defined(GIT_OPENSSL) || defined(GIT_WINHTTP) || defined(GIT_SECURE_TRANSPORT) +#if defined(GIT_OPENSSL) || defined(GIT_WINHTTP) || defined(GIT_SECURE_TRANSPORT) || defined(GIT_MBEDTLS) { "https://", git_transport_smart, &http_subtransport_definition }, #endif { "file://", git_transport_local, NULL }, diff --git a/src/transports/http.c b/src/transports/http.c index e5f2b9f28b2..9817294e410 100644 --- a/src/transports/http.c +++ b/src/transports/http.c @@ -586,7 +586,7 @@ static int http_connect(http_subtransport *t) error = git_stream_connect(t->io); -#if defined(GIT_OPENSSL) || defined(GIT_SECURE_TRANSPORT) || defined(GIT_CURL) +#if defined(GIT_OPENSSL) || defined(GIT_SECURE_TRANSPORT) || defined(GIT_CURL) || defined(GIT_MBEDTLS) if ((!error || error == GIT_ECERTIFICATE) && t->owner->certificate_check_cb != NULL && git_stream_is_encrypted(t->io)) { git_cert *cert; diff --git a/tests/online/badssl.c b/tests/online/badssl.c index 850468320e9..e076a77f128 100644 --- a/tests/online/badssl.c +++ b/tests/online/badssl.c @@ -4,7 +4,7 @@ static git_repository *g_repo; -#if defined(GIT_OPENSSL) || defined(GIT_WINHTTP) || defined(GIT_SECURE_TRANSPORT) +#if defined(GIT_OPENSSL) || defined(GIT_WINHTTP) || defined(GIT_SECURE_TRANSPORT) || defined(GIT_MBEDTLS) void test_online_badssl__expired(void) {