-
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 13 commits
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
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,7 +88,7 @@ IF(MSVC) | |
ENDIF() | ||
|
||
IF (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin") | ||
OPTION( USE_OPENSSL "Link with and use openssl library" ON ) | ||
OPTION( USE_OPENSSL "Link with and use openssl library" ON ) | ||
ENDIF() | ||
|
||
CHECK_STRUCT_HAS_MEMBER ("struct stat" st_mtim "sys/types.h;sys/stat.h" | ||
|
@@ -109,7 +109,11 @@ ELSE () | |
ENDIF() | ||
|
||
IF (HAVE_STRUCT_STAT_NSEC OR WIN32) | ||
OPTION( USE_NSEC "Care about sub-second file mtimes and ctimes" ON ) | ||
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 ) | ||
ENDIF() | ||
|
||
# This variable will contain the libraries we need to put into | ||
|
@@ -283,6 +287,10 @@ ELSE () | |
FIND_PACKAGE(OpenSSL) | ||
ENDIF () | ||
|
||
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 () | ||
|
||
IF (CURL_FOUND) | ||
ADD_DEFINITIONS(-DGIT_CURL) | ||
INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIRS}) | ||
|
@@ -316,6 +324,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() | ||
|
@@ -543,6 +554,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) | ||
|
@@ -690,7 +706,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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# - Try to find mbedTLS | ||
# Once done this will define | ||
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 don't need this file at all, do we? Our CMake script relies on pkg-config rather than trying to guess where it might be installed. 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. Mbed-TLS/mbedtls#228, so not yet 😉 |
||
# | ||
# Read-Only variables | ||
# MBEDTLS_FOUND - system has mbedTLS | ||
# MBEDTLS_INCLUDE_DIR - the mbedTLS include directory | ||
# MBEDTLS_LIBRARY_DIR - the mbedTLS library directory | ||
# MBEDTLS_LIBRARIES - Link these to use mbedTLS | ||
# MBEDTLS_LIBRARY - path to mbedTLS library | ||
# MBEDX509_LIBRARY - path to mbedTLS X.509 library | ||
# MBEDCRYPTO_LIBRARY - path to mbedTLS Crypto library | ||
|
||
FIND_PATH(MBEDTLS_INCLUDE_DIR mbedtls/version.h) | ||
|
||
IF(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARIES) | ||
# Already in cache, be silent | ||
SET(MBEDTLS_FIND_QUIETLY TRUE) | ||
ENDIF() | ||
|
||
FIND_LIBRARY(MBEDTLS_LIBRARY NAMES mbedtls libmbedtls libmbedx509) | ||
FIND_LIBRARY(MBEDX509_LIBRARY NAMES mbedx509 libmbedx509) | ||
FIND_LIBRARY(MBEDCRYPTO_LIBRARY NAMES mbedcrypto libmbedcrypto) | ||
|
||
IF(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARY AND MBEDX509_LIBRARY AND MBEDCRYPTO_LIBRARY) | ||
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_LIBRARY_DIR ${MBEDTLS_LIBRARY} PATH) | ||
GET_FILENAME_COMPONENT(MBEDTLS_LIBRARY_FILE ${MBEDTLS_LIBRARY} NAME_WE) | ||
GET_FILENAME_COMPONENT(MBEDX509_LIBRARY_FILE ${MBEDX509_LIBRARY} NAME_WE) | ||
GET_FILENAME_COMPONENT(MBEDCRYPTO_LIBRARY_FILE ${MBEDCRYPTO_LIBRARY} NAME_WE) | ||
STRING(REGEX REPLACE "^lib" "" MBEDTLS_LIBRARY_FILE ${MBEDTLS_LIBRARY_FILE}) | ||
STRING(REGEX REPLACE "^lib" "" MBEDX509_LIBRARY_FILE ${MBEDX509_LIBRARY_FILE}) | ||
STRING(REGEX REPLACE "^lib" "" MBEDCRYPTO_LIBRARY_FILE ${MBEDCRYPTO_LIBRARY_FILE}) | ||
SET(MBEDTLS_LIBRARIES "-L${MBEDTLS_LIBRARY_DIR} -l${MBEDTLS_LIBRARY_FILE} -l${MBEDX509_LIBRARY_FILE} -l${MBEDCRYPTO_LIBRARY_FILE}") | ||
|
||
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_LIBRARY}") | ||
MESSAGE(STATUS " X509: ${MBEDX509_LIBRARY}") | ||
MESSAGE(STATUS " Crypto: ${MBEDCRYPTO_LIBRARY}") | ||
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_LIBRARY_DIR | ||
MBEDTLS_LIBRARIES | ||
MBEDTLS_LIBRARY | ||
MBEDX509_LIBRARY | ||
MBEDCRYPTO_LIBRARY | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ curl -L https://github.com/ethomson/poxyproxy/releases/download/v0.1.0/poxyproxy | |
# Run this early so we know it's ready by the time we need it | ||
java -jar poxyproxy.jar -d --port 8080 --credentials foo:bar & | ||
|
||
if [ -n "$MBEDTLS" ]; | ||
then | ||
./script/mbedtls.sh; | ||
fi | ||
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 should be part of the script that installs the dependencies. The Travis rules currently only do it for osx, but we should remove the |
||
|
||
mkdir _build | ||
cd _build | ||
# shellcheck disable=SC2086 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,11 @@ | |
#include "sysdir.h" | ||
#include "filter.h" | ||
#include "merge_driver.h" | ||
#ifdef GIT_OPENSSL | ||
#include "openssl_stream.h" | ||
#elif GIT_MBEDTLS | ||
#include "mbedtls_stream.h" | ||
#endif | ||
#include "thread-utils.h" | ||
#include "git2/global.h" | ||
#include "transports/ssh.h" | ||
|
@@ -61,8 +65,13 @@ static int init_common(void) | |
(ret = git_sysdir_global_init()) == 0 && | ||
(ret = git_filter_global_init()) == 0 && | ||
(ret = git_merge_driver_global_init()) == 0 && | ||
(ret = git_transport_ssh_global_init()) == 0 && | ||
(ret = git_openssl_stream_global_init()) == 0) | ||
(ret = git_transport_ssh_global_init()) == 0 | ||
#ifdef GIT_OPENSSL | ||
&& (ret = git_openssl_stream_global_init()) == 0 | ||
#elif GIT_MBEDTLS | ||
&& (ret = git_mbedtls_stream_global_init()) == 0 | ||
#endif | ||
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 didn't need to ifdef-away the OpenSSL stream init, and we definitely shouldn't need it now. If there is no mbedtls, its init function should no-op. We define an interface and program against it. |
||
) | ||
ret = git_mwindow_global_init(); | ||
|
||
GIT_MEMORY_BARRIER; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,12 @@ typedef struct { | |
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 | ||
|
||
git_global_st *git__global_state(void); | ||
|
||
extern git_mutex git__mwindow_mutex; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <mbedtls/sha1.h> | ||
|
||
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__ */ | ||
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. The line ending is missing. |
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.