Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a47a6bc

Browse files
committed
Merge branch 'mbedtls' of https://github.com/wildart/libgit2
2 parents 89c332e + 6b556a6 commit a47a6bc

File tree

16 files changed

+716
-8
lines changed

16 files changed

+716
-8
lines changed

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ matrix:
4545
- VALGRIND=1
4646
OPTIONS="-DBUILD_CLAR=ON -DBUILD_EXAMPLES=OFF -DDEBUG_POOL=ON -DCMAKE_BUILD_TYPE=Debug"
4747
os: linux
48+
- compiler: gcc
49+
env:
50+
- MBEDTLS=1
51+
OPTIONS="-DTHREADSAFE=ON -DCMAKE_BUILD_TYPE=Release -DUSE_OPENSSL=OFF -DMBEDTLS_ROOT_DIR=../mbedtls"
52+
os: linux
53+
- compiler: gcc
54+
env:
55+
- MBEDTLS=1
56+
OPTIONS="-DTHREADSAFE=OFF -DBUILD_EXAMPLES=ON -DUSE_OPENSSL=OFF -DMBEDTLS_ROOT_DIR=../mbedtls"
57+
os: linux
4858
allow_failures:
4959
- env: COVERITY=1
5060
- env:

CMakeLists.txt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ IF(MSVC)
8888
ENDIF()
8989

9090
IF (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
91-
OPTION( USE_OPENSSL "Link with and use openssl library" ON )
91+
OPTION( USE_OPENSSL "Link with and use openssl library" ON )
9292
ENDIF()
9393

9494
CHECK_STRUCT_HAS_MEMBER ("struct stat" st_mtim "sys/types.h;sys/stat.h"
@@ -109,7 +109,11 @@ ELSE ()
109109
ENDIF()
110110

111111
IF (HAVE_STRUCT_STAT_NSEC OR WIN32)
112-
OPTION( USE_NSEC "Care about sub-second file mtimes and ctimes" ON )
112+
OPTION( USE_NSEC "Care about sub-second file mtimes and ctimes" ON )
113+
ENDIF()
114+
115+
IF (NOT USE_OPENSSL)
116+
OPTION( USE_MBEDTLS "Link with and use mbedTLS library" ON )
113117
ENDIF()
114118

115119
# This variable will contain the libraries we need to put into
@@ -283,6 +287,10 @@ ELSE ()
283287
FIND_PACKAGE(OpenSSL)
284288
ENDIF ()
285289

290+
IF (NOT AMIGA AND USE_MBEDTLS)
291+
FIND_PACKAGE(mbedTLS)
292+
ENDIF ()
293+
286294
IF (CURL_FOUND)
287295
ADD_DEFINITIONS(-DGIT_CURL)
288296
INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIRS})
@@ -316,6 +324,9 @@ ELSEIF (OPENSSL_FOUND AND NOT SHA1_TYPE STREQUAL "builtin")
316324
ELSE()
317325
SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} openssl")
318326
ENDIF ()
327+
ELSEIF (MBEDTLS_FOUND AND NOT SHA1_TYPE STREQUAL "builtin")
328+
ADD_DEFINITIONS(-DMBEDTLS_SHA1)
329+
FILE(GLOB SRC_SHA1 src/hash/hash_mbedtls.c)
319330
ELSE()
320331
FILE(GLOB SRC_SHA1 src/hash/hash_generic.c)
321332
ENDIF()
@@ -543,6 +554,11 @@ IF (OPENSSL_FOUND)
543554
SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
544555
ENDIF()
545556

557+
IF (MBEDTLS_FOUND)
558+
ADD_DEFINITIONS(-DGIT_MBEDTLS)
559+
INCLUDE_DIRECTORIES(${MBEDTLS_INCLUDE_DIR})
560+
SET(SSL_LIBRARIES ${MBEDTLS_LIBRARIES})
561+
ENDIF()
546562

547563

548564
IF (THREADSAFE)
@@ -690,7 +706,7 @@ IF (BUILD_CLAR)
690706
ENDIF ()
691707

692708
ENABLE_TESTING()
693-
IF (WINHTTP OR OPENSSL_FOUND OR SECURITY_FOUND)
709+
IF (WINHTTP OR OPENSSL_FOUND OR SECURITY_FOUND OR MBEDTLS_FOUND)
694710
ADD_TEST(libgit2_clar libgit2_clar -ionline)
695711
ELSE ()
696712
ADD_TEST(libgit2_clar libgit2_clar -v)

cmake/Modules/FindmbedTLS.cmake

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
FIND_PATH(MBEDTLS_INCLUDE_DIR mbedtls/version.h)
14+
15+
IF(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARIES)
16+
# Already in cache, be silent
17+
SET(MBEDTLS_FIND_QUIETLY TRUE)
18+
ENDIF()
19+
20+
FIND_LIBRARY(MBEDTLS_LIBRARY NAMES mbedtls libmbedtls libmbedx509)
21+
FIND_LIBRARY(MBEDX509_LIBRARY NAMES mbedx509 libmbedx509)
22+
FIND_LIBRARY(MBEDCRYPTO_LIBRARY NAMES mbedcrypto libmbedcrypto)
23+
24+
IF(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARY AND MBEDX509_LIBRARY AND MBEDCRYPTO_LIBRARY)
25+
SET(MBEDTLS_FOUND TRUE)
26+
ENDIF()
27+
28+
IF(MBEDTLS_FOUND)
29+
# split mbedTLS into -L and -l linker options, so we can set them for pkg-config
30+
GET_FILENAME_COMPONENT(MBEDTLS_LIBRARY_DIR ${MBEDTLS_LIBRARY} PATH)
31+
GET_FILENAME_COMPONENT(MBEDTLS_LIBRARY_FILE ${MBEDTLS_LIBRARY} NAME_WE)
32+
GET_FILENAME_COMPONENT(MBEDX509_LIBRARY_FILE ${MBEDX509_LIBRARY} NAME_WE)
33+
GET_FILENAME_COMPONENT(MBEDCRYPTO_LIBRARY_FILE ${MBEDCRYPTO_LIBRARY} NAME_WE)
34+
STRING(REGEX REPLACE "^lib" "" MBEDTLS_LIBRARY_FILE ${MBEDTLS_LIBRARY_FILE})
35+
STRING(REGEX REPLACE "^lib" "" MBEDX509_LIBRARY_FILE ${MBEDX509_LIBRARY_FILE})
36+
STRING(REGEX REPLACE "^lib" "" MBEDCRYPTO_LIBRARY_FILE ${MBEDCRYPTO_LIBRARY_FILE})
37+
SET(MBEDTLS_LIBRARIES "-L${MBEDTLS_LIBRARY_DIR} -l${MBEDTLS_LIBRARY_FILE} -l${MBEDX509_LIBRARY_FILE} -l${MBEDCRYPTO_LIBRARY_FILE}")
38+
39+
IF(NOT MBEDTLS_FIND_QUIETLY)
40+
MESSAGE(STATUS "Found mbedTLS:")
41+
FILE(READ ${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h MBEDTLSCONTENT)
42+
STRING(REGEX MATCH "MBEDTLS_VERSION_STRING +\"[0-9|.]+\"" MBEDTLSMATCH ${MBEDTLSCONTENT})
43+
IF (MBEDTLSMATCH)
44+
STRING(REGEX REPLACE "MBEDTLS_VERSION_STRING +\"([0-9|.]+)\"" "\\1" MBEDTLS_VERSION ${MBEDTLSMATCH})
45+
MESSAGE(STATUS " version ${MBEDTLS_VERSION}")
46+
ENDIF(MBEDTLSMATCH)
47+
MESSAGE(STATUS " TLS: ${MBEDTLS_LIBRARY}")
48+
MESSAGE(STATUS " X509: ${MBEDX509_LIBRARY}")
49+
MESSAGE(STATUS " Crypto: ${MBEDCRYPTO_LIBRARY}")
50+
ENDIF(NOT MBEDTLS_FIND_QUIETLY)
51+
ELSE(MBEDTLS_FOUND)
52+
IF(MBEDTLS_FIND_REQUIRED)
53+
MESSAGE(FATAL_ERROR "Could not find mbedTLS")
54+
ENDIF(MBEDTLS_FIND_REQUIRED)
55+
ENDIF(MBEDTLS_FOUND)
56+
57+
MARK_AS_ADVANCED(
58+
MBEDTLS_INCLUDE_DIR
59+
MBEDTLS_LIBRARY_DIR
60+
MBEDTLS_LIBRARIES
61+
MBEDTLS_LIBRARY
62+
MBEDX509_LIBRARY
63+
MBEDCRYPTO_LIBRARY
64+
)

script/cibuild.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ curl -L https://github.com/ethomson/poxyproxy/releases/download/v0.1.0/poxyproxy
1515
# Run this early so we know it's ready by the time we need it
1616
java -jar poxyproxy.jar -d --port 8080 --credentials foo:bar &
1717

18+
if [ -n "$MBEDTLS" ];
19+
then
20+
./script/mbedtls.sh;
21+
fi
22+
1823
mkdir _build
1924
cd _build
2025
# shellcheck disable=SC2086

script/mbedtls.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
git clone https://github.com/ARMmbed/mbedtls.git mbedtls
4+
cd mbedtls
5+
git checkout mbedtls-2.1.2
6+
make CFLAGS='-fPIC -fpic' -j2 lib

src/global.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
#include "sysdir.h"
1111
#include "filter.h"
1212
#include "merge_driver.h"
13+
#ifdef GIT_OPENSSL
1314
#include "openssl_stream.h"
15+
#elif GIT_MBEDTLS
16+
#include "mbedtls_stream.h"
17+
#endif
1418
#include "thread-utils.h"
1519
#include "git2/global.h"
1620
#include "transports/ssh.h"
@@ -61,8 +65,13 @@ static int init_common(void)
6165
(ret = git_sysdir_global_init()) == 0 &&
6266
(ret = git_filter_global_init()) == 0 &&
6367
(ret = git_merge_driver_global_init()) == 0 &&
64-
(ret = git_transport_ssh_global_init()) == 0 &&
65-
(ret = git_openssl_stream_global_init()) == 0)
68+
(ret = git_transport_ssh_global_init()) == 0
69+
#ifdef GIT_OPENSSL
70+
&& (ret = git_openssl_stream_global_init()) == 0
71+
#elif GIT_MBEDTLS
72+
&& (ret = git_mbedtls_stream_global_init()) == 0
73+
#endif
74+
)
6675
ret = git_mwindow_global_init();
6776

6877
GIT_MEMORY_BARRIER;

src/global.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ typedef struct {
2323
extern SSL_CTX *git__ssl_ctx;
2424
#endif
2525

26+
#ifdef GIT_MBEDTLS
27+
# include "mbedtls/platform.h"
28+
# include "mbedtls/ssl.h"
29+
extern mbedtls_ssl_config *git__ssl_conf;
30+
#endif
31+
2632
git_global_st *git__global_state(void);
2733

2834
extern git_mutex git__mwindow_mutex;

src/hash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ void git_hash_ctx_cleanup(git_hash_ctx *ctx);
2020
# include "hash/hash_common_crypto.h"
2121
#elif defined(OPENSSL_SHA1)
2222
# include "hash/hash_openssl.h"
23+
#elif defined(MBEDTLS_SHA1)
24+
# include "hash/hash_mbedtls.h"
2325
#elif defined(WIN32_SHA1)
2426
# include "hash/hash_win32.h"
2527
#else

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)