Skip to content
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

Mbedtls support #512

Merged
merged 9 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ matrix:
- meson compile -C builddir
- meson test -v -C builddir

# linux build with mbedtls
- os: linux
env:
- TEST="linux mbedtls (gcc)"
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-6
- libmbedtls-dev
script:
- mkdir build && cd build
- cmake -DENABLE_MBEDTLS=ON ..
- make
- make test
- cd ..

# default osx build
- os: osx
env:
Expand Down
30 changes: 27 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(libsrtp2 LANGUAGES C)

set(PACKAGE_VERSION 2.4.0)
set(PACKAGE_STRING "${CMAKE_PROJECT_NAME} ${PACKAGE_VERSION}")

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR};${CMAKE_MODULE_PATH}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unfortunate. What module are we finding from within the source tree?

Copy link
Contributor Author

@ycyang1229 ycyang1229 Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this for looking for FindMbedTLS.cmake, and it will be used for searching mbedtls related header files and libs. As I remember, I need to use such a cmake files to search mbedtls related stuff. Did I mistake it?

include(TestBigEndian)
include(CheckIncludeFile)
include(CheckFunctionExists)
Expand Down Expand Up @@ -54,15 +54,27 @@ set(ENABLE_DEBUG_LOGGING OFF CACHE BOOL "Enable debug logging in all modules")
set(ERR_REPORTING_STDOUT OFF CACHE BOOL "Enable logging to stdout")
set(ERR_REPORTING_FILE "" CACHE FILEPATH "Use file for logging")
set(ENABLE_OPENSSL OFF CACHE BOOL "Enable OpenSSL crypto engine")
set(ENABLE_MBEDTLS OFF CACHE BOOL "Enable MbedTLS crypto engine")
set(TEST_APPS ON CACHE BOOL "Build test applications")
option(BUILD_SHARED_LIBS "Build shared library" OFF)

if(ENABLE_OPENSSL AND ENABLE_MBEDTLS)
message(FATAL_ERROR "ssl conflict. can not enable openssl and mbedtls simultaneously.")
endif()

if(ENABLE_OPENSSL)
find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})
set(OPENSSL ${ENABLE_OPENSSL} CACHE BOOL INTERNAL)
set(GCM ${ENABLE_OPENSSL} CACHE BOOL INTERNAL)
endif()

if(ENABLE_MBEDTLS)
find_package(MbedTLS REQUIRED)
include_directories(${MBEDTLS_INCLUDE_DIRS})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that the target_link_libraries directive below will do this automatically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same reason as the above stuff, FindMbedTLS.cmake.

set(MBEDTLS ${ENABLE_MBEDTLS} CACHE BOOL INTERNAL)
set(GCM ${ENABLE_MBEDTLS} CACHE BOOL INTERNAL)
endif()
set(OPENSSL ${ENABLE_OPENSSL} CACHE BOOL INTERNAL)
set(GCM ${ENABLE_OPENSSL} CACHE BOOL INTERNAL)

set(CONFIG_FILE_DIR ${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CONFIG_FILE_DIR})
Expand All @@ -85,6 +97,11 @@ if(ENABLE_OPENSSL)
crypto/cipher/aes_icm_ossl.c
crypto/cipher/aes_gcm_ossl.c
)
elseif(ENABLE_MBEDTLS)
list(APPEND CIPHERS_SOURCES_C
crypto/cipher/aes_icm_mbedtls.c
crypto/cipher/aes_gcm_mbedtls.c
)
else()
list(APPEND CIPHERS_SOURCES_C
crypto/cipher/aes.c
Expand All @@ -101,6 +118,10 @@ if(ENABLE_OPENSSL)
list(APPEND HASHES_SOURCES_C
crypto/hash/hmac_ossl.c
)
elseif(ENABLE_MBEDTLS)
list(APPEND HASHES_SOURCES_C
crypto/hash/hmac_mbedtls.c
)
else()
list(APPEND HASHES_SOURCES_C
crypto/hash/hmac.c
Expand Down Expand Up @@ -182,6 +203,9 @@ target_include_directories(srtp2 PUBLIC crypto/include include)
if(ENABLE_OPENSSL)
target_link_libraries(srtp2 OpenSSL::Crypto)
endif()
if(ENABLE_MBEDTLS)
target_link_libraries(srtp2 ${MBEDTLS_LIBRARIES})
endif()
if(WIN32)
target_link_libraries(srtp2 ws2_32)
endif()
Expand Down
38 changes: 38 additions & 0 deletions FindMbedTLS.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
find_path(MBEDTLS_INCLUDE_DIRS mbedtls/ssl.h)

find_library(MBEDTLS_LIBRARY mbedtls)
find_library(MBEDX509_LIBRARY mbedx509)
find_library(MBEDCRYPTO_LIBRARY mbedcrypto)

set(MBEDTLS_LIBRARIES "${MBEDTLS_LIBRARY}" "${MBEDX509_LIBRARY}" "${MBEDCRYPTO_LIBRARY}")

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(MbedTLS DEFAULT_MSG
MBEDTLS_LIBRARY MBEDTLS_INCLUDE_DIRS MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)

mark_as_advanced(MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)

if(NOT TARGET MbedTLS)
message("in mbedtls ${MBEDTLS_LIBRARY}")
add_library(MbedTLS UNKNOWN IMPORTED)
set_target_properties(MbedTLS PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIRS}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${MBEDTLS_LIBRARY}")
endif()

if(NOT TARGET MbedCrypto)
add_library(MbedCrypto UNKNOWN IMPORTED)
set_target_properties(MbedCrypto PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIRS}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${MBEDCRYPTO_LIBRARY}")
endif()

if(NOT TARGET MbedX509)
add_library(MbedX509 UNKNOWN IMPORTED)
set_target_properties(MbedX509 PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIRS}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${MBEDX509_LIBRARY}")
endif()
3 changes: 3 additions & 0 deletions config_in_cmake.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
/* Define this to use AES-GCM. */
#cmakedefine GCM 1

/* Define this to use MBEDTLS. */
#cmakedefine MBEDTLS 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would reorder these to put MBEDTLS just after OPENSSL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I will change the order.


/* Define if building for a CISC machine (e.g. Intel). */
#define CPU_CISC 1

Expand Down