Skip to content

Commit 86353a7

Browse files
authored
Merge pull request libgit2#4173 from tiennou/mbedtls
mbedTLS support
2 parents 5d346c1 + cb2da47 commit 86353a7

File tree

15 files changed

+735
-9
lines changed

15 files changed

+735
-9
lines changed

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ matrix:
5555
OPTIONS="-DBUILD_CLAR=ON -DBUILD_EXAMPLES=OFF -DDEBUG_POOL=ON -DCMAKE_BUILD_TYPE=Debug"
5656
os: linux
5757
dist: trusty
58+
- compiler: gcc
59+
env:
60+
MBEDTLS=1
61+
OPTIONS="-DTHREADSAFE=ON -DCMAKE_BUILD_TYPE=Release -DUSE_HTTPS=mbedTLS -DMBEDTLS_ROOT_DIR=../deps/mbedtls"
62+
os: linux
63+
- compiler: gcc
64+
env:
65+
MBEDTLS=1
66+
OPTIONS="-DTHREADSAFE=OFF -DBUILD_EXAMPLES=ON -DUSE_HTTPS=mbedTLS -DMBEDTLS_ROOT_DIR=../deps/mbedtls"
67+
os: linux
5868
allow_failures:
5969
- env: COVERITY=1
6070

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ OPTION( PROFILE "Generate profiling information" OFF )
4848
OPTION( ENABLE_TRACE "Enables tracing support" OFF )
4949
OPTION( LIBGIT2_FILENAME "Name of the produced binary" OFF )
5050

51-
SET(SHA1_BACKEND "CollisionDetection" CACHE STRING "Backend to use for SHA1. One of Generic, OpenSSL, Win32, CommonCrypto, CollisionDetection. ")
51+
SET(SHA1_BACKEND "CollisionDetection" CACHE STRING "Backend to use for SHA1. One of Generic, OpenSSL, Win32, CommonCrypto, mbedTLS, CollisionDetection. ")
5252
OPTION( USE_SSH "Link with libssh to enable SSH support" ON )
5353
OPTION( USE_HTTPS "Enable HTTPS support. Can be set to a specific backend" ON )
5454
OPTION( USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF )

cmake/Modules/FindmbedTLS.cmake

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# - Try to find mbedTLS
2+
# Once done this will define
3+
#
4+
# Read-Only variables
5+
# MBEDTLS_FOUND - system has mbedTLS
6+
# MBEDTLS_INCLUDE_DIR - the mbedTLS include directory
7+
# MBEDTLS_LIBRARY_DIR - the mbedTLS library directory
8+
# MBEDTLS_LIBRARIES - Link these to use mbedTLS
9+
# MBEDTLS_LIBRARY - path to mbedTLS library
10+
# MBEDX509_LIBRARY - path to mbedTLS X.509 library
11+
# MBEDCRYPTO_LIBRARY - path to mbedTLS Crypto library
12+
#
13+
# Hint
14+
# MBEDTLS_ROOT_DIR can be pointed to a local mbedTLS installation.
15+
16+
SET(_MBEDTLS_ROOT_HINTS
17+
${MBEDTLS_ROOT_DIR}
18+
ENV MBEDTLS_ROOT_DIR
19+
)
20+
21+
SET(_MBEDTLS_ROOT_HINTS_AND_PATHS
22+
HINTS ${_MBEDTLS_ROOT_HINTS}
23+
PATHS ${_MBEDTLS_ROOT_PATHS}
24+
)
25+
26+
FIND_PATH(MBEDTLS_INCLUDE_DIR
27+
NAMES mbedtls/version.h
28+
${_MBEDTLS_ROOT_HINTS_AND_PATHS}
29+
PATH_SUFFIXES include
30+
)
31+
32+
IF(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARIES)
33+
# Already in cache, be silent
34+
SET(MBEDTLS_FIND_QUIETLY TRUE)
35+
ENDIF()
36+
37+
FIND_LIBRARY(MBEDTLS_LIBRARY
38+
NAMES mbedtls libmbedtls
39+
${_MBEDTLS_ROOT_HINTS_AND_PATHS}
40+
PATH_SUFFIXES library
41+
)
42+
FIND_LIBRARY(MBEDX509_LIBRARY
43+
NAMES mbedx509 libmbedx509
44+
${_MBEDTLS_ROOT_HINTS_AND_PATHS}
45+
PATH_SUFFIXES library
46+
)
47+
FIND_LIBRARY(MBEDCRYPTO_LIBRARY
48+
NAMES mbedcrypto libmbedcrypto
49+
${_MBEDTLS_ROOT_HINTS_AND_PATHS}
50+
PATH_SUFFIXES library
51+
)
52+
53+
IF(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARY AND MBEDX509_LIBRARY AND MBEDCRYPTO_LIBRARY)
54+
SET(MBEDTLS_FOUND TRUE)
55+
ENDIF()
56+
57+
IF(MBEDTLS_FOUND)
58+
# split mbedTLS into -L and -l linker options, so we can set them for pkg-config
59+
GET_FILENAME_COMPONENT(MBEDTLS_LIBRARY_DIR ${MBEDTLS_LIBRARY} PATH)
60+
GET_FILENAME_COMPONENT(MBEDTLS_LIBRARY_FILE ${MBEDTLS_LIBRARY} NAME_WE)
61+
GET_FILENAME_COMPONENT(MBEDX509_LIBRARY_FILE ${MBEDX509_LIBRARY} NAME_WE)
62+
GET_FILENAME_COMPONENT(MBEDCRYPTO_LIBRARY_FILE ${MBEDCRYPTO_LIBRARY} NAME_WE)
63+
STRING(REGEX REPLACE "^lib" "" MBEDTLS_LIBRARY_FILE ${MBEDTLS_LIBRARY_FILE})
64+
STRING(REGEX REPLACE "^lib" "" MBEDX509_LIBRARY_FILE ${MBEDX509_LIBRARY_FILE})
65+
STRING(REGEX REPLACE "^lib" "" MBEDCRYPTO_LIBRARY_FILE ${MBEDCRYPTO_LIBRARY_FILE})
66+
SET(MBEDTLS_LIBRARIES "-L${MBEDTLS_LIBRARY_DIR} -l${MBEDTLS_LIBRARY_FILE} -l${MBEDX509_LIBRARY_FILE} -l${MBEDCRYPTO_LIBRARY_FILE}")
67+
68+
IF(NOT MBEDTLS_FIND_QUIETLY)
69+
MESSAGE(STATUS "Found mbedTLS:")
70+
FILE(READ ${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h MBEDTLSCONTENT)
71+
STRING(REGEX MATCH "MBEDTLS_VERSION_STRING +\"[0-9|.]+\"" MBEDTLSMATCH ${MBEDTLSCONTENT})
72+
IF (MBEDTLSMATCH)
73+
STRING(REGEX REPLACE "MBEDTLS_VERSION_STRING +\"([0-9|.]+)\"" "\\1" MBEDTLS_VERSION ${MBEDTLSMATCH})
74+
MESSAGE(STATUS " version ${MBEDTLS_VERSION}")
75+
ENDIF(MBEDTLSMATCH)
76+
MESSAGE(STATUS " TLS: ${MBEDTLS_LIBRARY}")
77+
MESSAGE(STATUS " X509: ${MBEDX509_LIBRARY}")
78+
MESSAGE(STATUS " Crypto: ${MBEDCRYPTO_LIBRARY}")
79+
ENDIF(NOT MBEDTLS_FIND_QUIETLY)
80+
ELSE(MBEDTLS_FOUND)
81+
IF(MBEDTLS_FIND_REQUIRED)
82+
MESSAGE(FATAL_ERROR "Could not find mbedTLS")
83+
ENDIF(MBEDTLS_FIND_REQUIRED)
84+
ENDIF(MBEDTLS_FOUND)
85+
86+
MARK_AS_ADVANCED(
87+
MBEDTLS_INCLUDE_DIR
88+
MBEDTLS_LIBRARY_DIR
89+
MBEDTLS_LIBRARIES
90+
MBEDTLS_LIBRARY
91+
MBEDX509_LIBRARY
92+
MBEDCRYPTO_LIBRARY
93+
)

script/install-deps-linux.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
3+
set -x
4+
5+
if [ "$MBEDTLS" ]; then
6+
git clone --depth 10 --single-branch --branch mbedtls-2.6.1 https://github.com/ARMmbed/mbedtls.git ./deps/mbedtls
7+
cd ./deps/mbedtls
8+
# We pass -fPIC explicitely because we'll include it in libgit2.so
9+
CFLAGS=-fPIC cmake -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DUSE_SHARED_MBEDTLS_LIBRARY=OFF -DUSE_STATIC_MBEDTLS_LIBRARY=ON .
10+
cmake --build .
11+
12+
echo "mbedTLS built in `pwd`"
13+
fi

src/CMakeLists.txt

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ ELSE ()
133133
ENDIF()
134134

135135
IF (USE_HTTPS)
136+
# We try to find any packages our backends might use
137+
FIND_PACKAGE(OpenSSL)
138+
FIND_PACKAGE(mbedTLS)
136139
IF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
137140
FIND_PACKAGE(Security)
138141
FIND_PACKAGE(CoreFoundation)
@@ -149,8 +152,13 @@ IF (USE_HTTPS)
149152
ENDIF()
150153
ELSEIF (WINHTTP)
151154
SET(HTTPS_BACKEND "WinHTTP")
152-
ELSE()
155+
ELSEIF(OPENSSL_FOUND)
153156
SET(HTTPS_BACKEND "OpenSSL")
157+
ELSEIF(MBEDTLS_FOUND)
158+
SET(HTTPS_BACKEND "mbedTLS")
159+
ELSE()
160+
MESSAGE(FATAL_ERROR "Unable to autodetect a usable HTTPS backend."
161+
"Please pass the backend name explicitly (-DUSE_HTTPS=backend)")
154162
ENDIF()
155163
ELSE()
156164
# Backend was explicitly set
@@ -174,8 +182,6 @@ IF (USE_HTTPS)
174182
LIST(APPEND LIBGIT2_LIBS ${COREFOUNDATION_LIBRARIES} ${SECURITY_LIBRARIES})
175183
LIST(APPEND LIBGIT2_PC_LIBS ${COREFOUNDATION_LDFLAGS} ${SECURITY_LDFLAGS})
176184
ELSEIF (HTTPS_BACKEND STREQUAL "OpenSSL")
177-
FIND_PACKAGE(OpenSSL)
178-
179185
IF (NOT OPENSSL_FOUND)
180186
MESSAGE(FATAL_ERROR "Asked for OpenSSL TLS backend, but it wasn't found")
181187
ENDIF()
@@ -185,6 +191,53 @@ IF (USE_HTTPS)
185191
LIST(APPEND LIBGIT2_LIBS ${OPENSSL_LIBRARIES})
186192
LIST(APPEND LIBGIT2_PC_LIBS ${OPENSSL_LDFLAGS})
187193
LIST(APPEND LIBGIT2_PC_REQUIRES "openssl")
194+
ELSEIF(HTTPS_BACKEND STREQUAL "mbedTLS")
195+
IF (NOT MBEDTLS_FOUND)
196+
MESSAGE(FATAL_ERROR "Asked for mbedTLS backend, but it wasn't found")
197+
ENDIF()
198+
199+
IF(NOT CERT_LOCATION)
200+
MESSAGE("Auto-detecting default certificates location")
201+
IF(CMAKE_SYSTEM_NAME MATCHES Darwin)
202+
# Check for an Homebrew installation
203+
SET(OPENSSL_CMD "/usr/local/opt/openssl/bin/openssl")
204+
ELSE()
205+
SET(OPENSSL_CMD "openssl")
206+
ENDIF()
207+
EXECUTE_PROCESS(COMMAND ${OPENSSL_CMD} version -d OUTPUT_VARIABLE OPENSSL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
208+
IF(OPENSSL_DIR)
209+
STRING(REGEX REPLACE "^OPENSSLDIR: \"(.*)\"$" "\\1/" OPENSSL_DIR ${OPENSSL_DIR})
210+
211+
SET(OPENSSL_CA_LOCATIONS
212+
"ca-bundle.pem" # OpenSUSE Leap 42.1
213+
"cert.pem" # Ubuntu 14.04, FreeBSD
214+
"certs/ca-certificates.crt" # Ubuntu 16.04
215+
"certs/ca.pem" # Debian 7
216+
)
217+
FOREACH(SUFFIX IN LISTS OPENSSL_CA_LOCATIONS)
218+
SET(LOC "${OPENSSL_DIR}${SUFFIX}")
219+
IF(NOT CERT_LOCATION AND EXISTS "${OPENSSL_DIR}${SUFFIX}")
220+
SET(CERT_LOCATION ${LOC})
221+
ENDIF()
222+
ENDFOREACH()
223+
ELSE()
224+
MESSAGE("Unable to find OpenSSL executable. Please provide default certificate location via CERT_LOCATION")
225+
ENDIF()
226+
ENDIF()
227+
228+
IF(CERT_LOCATION)
229+
IF(NOT EXISTS ${CERT_LOCATION})
230+
MESSAGE(FATAL_ERROR "Cannot use CERT_LOCATION=${CERT_LOCATION} as it doesn't exist")
231+
ENDIF()
232+
ADD_FEATURE_INFO(CERT_LOCATION ON "using certificates from ${CERT_LOCATION}")
233+
ADD_DEFINITIONS(-DGIT_DEFAULT_CERT_LOCATION="${CERT_LOCATION}")
234+
ENDIF()
235+
236+
SET(GIT_MBEDTLS 1)
237+
LIST(APPEND LIBGIT2_INCLUDES ${MBEDTLS_INCLUDE_DIR})
238+
LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES})
239+
LIST(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LDFLAGS})
240+
LIST(APPEND LIBGIT2_PC_REQUIRES "mbedtls")
188241
ELSEIF (HTTPS_BACKEND STREQUAL "WinHTTP")
189242
# WinHTTP setup was handled in the WinHTTP-specific block above
190243
ELSE()
@@ -230,6 +283,11 @@ ELSEIF(SHA1_BACKEND STREQUAL "Win32")
230283
ELSEIF(SHA1_BACKEND STREQUAL "CommonCrypto")
231284
ADD_FEATURE_INFO(SHA ON "using CommonCrypto")
232285
SET(GIT_SHA1_COMMON_CRYPTO 1)
286+
ELSEIF (SHA1_BACKEND STREQUAL "mbedTLS")
287+
ADD_FEATURE_INFO(SHA ON "using mbedTLS")
288+
SET(GIT_SHA1_MBEDTLS 1)
289+
FILE(GLOB SRC_SHA1 src/hash/hash_mbedtls.c)
290+
LIST(APPEND LIBGIT2_PC_REQUIRES "mbedtls")
233291
ELSE()
234292
MESSAGE(FATAL_ERROR "Asked for unknown SHA1 backend ${SHA1_BACKEND}")
235293
ENDIF()

src/features.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
#cmakedefine GIT_HTTPS 1
2828
#cmakedefine GIT_OPENSSL 1
2929
#cmakedefine GIT_SECURE_TRANSPORT 1
30+
#cmakedefine GIT_MBEDTLS 1
3031

3132
#cmakedefine GIT_SHA1_COLLISIONDETECT 1
3233
#cmakedefine GIT_SHA1_WIN32 1
3334
#cmakedefine GIT_SHA1_COMMON_CRYPTO 1
3435
#cmakedefine GIT_SHA1_OPENSSL 1
36+
#cmakedefine GIT_SHA1_MBEDTLS 1
3537

3638
#endif

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/hash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ void git_hash_ctx_cleanup(git_hash_ctx *ctx);
2626
# include "hash/hash_openssl.h"
2727
#elif defined(GIT_SHA1_WIN32)
2828
# include "hash/hash_win32.h"
29+
#elif defined(GIT_SHA1_MBEDTLS)
30+
# include "hash/hash_mbedtls.h"
2931
#else
3032
# include "hash/hash_generic.h"
3133
#endif

src/hash/hash_mbedtls.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include "common.h"
9+
#include "hash.h"
10+
#include "hash/hash_mbedtls.h"
11+
12+
void git_hash_ctx_cleanup(git_hash_ctx *ctx)
13+
{
14+
assert(ctx);
15+
mbedtls_sha1_free(&ctx->c);
16+
}
17+
18+
int git_hash_init(git_hash_ctx *ctx)
19+
{
20+
assert(ctx);
21+
mbedtls_sha1_init(&ctx->c);
22+
mbedtls_sha1_starts(&ctx->c);
23+
return 0;
24+
}
25+
26+
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
27+
{
28+
assert(ctx);
29+
mbedtls_sha1_update(&ctx->c, data, len);
30+
return 0;
31+
}
32+
33+
int git_hash_final(git_oid *out, git_hash_ctx *ctx)
34+
{
35+
assert(ctx);
36+
mbedtls_sha1_finish(&ctx->c, out->id);
37+
return 0;
38+
}

src/hash/hash_mbedtls.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#ifndef INCLUDE_hash_mbedtld_h__
9+
#define INCLUDE_hash_mbedtld_h__
10+
11+
#include <mbedtls/sha1.h>
12+
13+
struct git_hash_ctx {
14+
mbedtls_sha1_context c;
15+
};
16+
17+
#define git_hash_global_init() 0
18+
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
19+
20+
#endif /* INCLUDE_hash_mbedtld_h__ */

0 commit comments

Comments
 (0)