Skip to content

Commit e6fbdde

Browse files
Sean-Dertytan652paullouisageneau
committed
Implement Mbed TLS Backend
Co-authored-by: tytan652 <17492366+tytan652@users.noreply.github.com> Co-authored-by: Paul-Louis Ageneau <paul-louis@ageneau.org>
1 parent 16f95dc commit e6fbdde

15 files changed

+1156
-21
lines changed

.github/workflows/build-mbedtls.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Build with Mbed TLS
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
jobs:
8+
build-linux:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- name: Set up Homebrew
13+
uses: Homebrew/actions/setup-homebrew@master
14+
- name: Install Mbed TLS
15+
run: brew update && brew install mbedtls
16+
- name: submodules
17+
run: git submodule update --init --recursive --depth 1
18+
- name: cmake
19+
run: cmake -B build -DUSE_MBEDTLS=1 -DWARNINGS_AS_ERRORS=1 -DCMAKE_PREFIX_PATH=$(brew --prefix mbedtls)
20+
- name: make
21+
run: (cd build; make -j2)
22+
- name: test
23+
run: ./build/tests
24+
build-macos:
25+
runs-on: macos-latest
26+
steps:
27+
- uses: actions/checkout@v2
28+
- name: Install Mbed TLS
29+
run: brew update && brew install mbedtls
30+
- name: submodules
31+
run: git submodule update --init --recursive --depth 1
32+
- name: cmake
33+
run: cmake -B build -DUSE_MBEDTLS=1 -DWARNINGS_AS_ERRORS=1 -DENABLE_LOCAL_ADDRESS_TRANSLATION=1 -DCMAKE_PREFIX_PATH=$(brew --prefix mbedtls)
34+
- name: make
35+
run: (cd build; make -j2)
36+
- name: test
37+
run: ./build/tests

CMakeLists.txt

+19-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ project(libdatachannel
55
set(PROJECT_DESCRIPTION "C/C++ WebRTC network library featuring Data Channels, Media Transport, and WebSockets")
66

77
# Options
8+
option(USE_MBEDTLS "Use Mbed TLS instead of OpenSSL" OFF)
89
option(USE_GNUTLS "Use GnuTLS instead of OpenSSL" OFF)
910
option(USE_NICE "Use libnice instead of libjuice" OFF)
1011
option(PREFER_SYSTEM_LIB "Prefer system libraries over deps folder" OFF)
@@ -21,12 +22,22 @@ option(WARNINGS_AS_ERRORS "Treat warnings as errors" OFF)
2122
option(CAPI_STDCALL "Set calling convention of C API callbacks stdcall" OFF)
2223
option(SCTP_DEBUG "Enable SCTP debugging output to verbose log" OFF)
2324

25+
if (USE_MBEDTLS AND USE_GNUTLS)
26+
message(FATAL_ERROR "Both USE_MBEDTLS and USE_GNUTLS can not be enabled at the same time")
27+
endif()
28+
29+
2430
if(USE_GNUTLS)
2531
option(USE_NETTLE "Use Nettle in libjuice" ON)
2632
else()
2733
option(USE_NETTLE "Use Nettle in libjuice" OFF)
34+
2835
if(NOT USE_SYSTEM_SRTP)
29-
option(ENABLE_OPENSSL "Enable OpenSSL crypto engine for SRTP" ON)
36+
if (USE_MBEDTLS)
37+
option(ENABLE_MBEDTLS "Enable Mbed TLS crypto engine for SRTP" ON)
38+
else()
39+
option(ENABLE_OPENSSL "Enable OpenSSL crypto engine for SRTP" ON)
40+
endif()
3041
endif()
3142
endif()
3243

@@ -337,6 +348,13 @@ if (USE_GNUTLS)
337348
target_link_libraries(datachannel PRIVATE Nettle::Nettle)
338349
target_link_libraries(datachannel-static PRIVATE Nettle::Nettle)
339350
endif()
351+
elseif(USE_MBEDTLS)
352+
find_package(MbedTLS 3 REQUIRED)
353+
354+
target_compile_definitions(datachannel PRIVATE USE_MBEDTLS)
355+
target_compile_definitions(datachannel-static PRIVATE USE_MBEDTLS)
356+
target_link_libraries(datachannel PRIVATE MbedTLS::MbedTLS)
357+
target_link_libraries(datachannel-static PRIVATE MbedTLS::MbedTLS)
340358
else()
341359
if(APPLE)
342360
# This is a bug in CMake that causes it to prefer the system version over

cmake/Modules/FindMbedTLS.cmake

+214
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
#[=======================================================================[.rst
2+
FindMbedTLS
3+
-----------
4+
5+
FindModule for MbedTLS and associated libraries
6+
7+
Components
8+
^^^^^^^^^^
9+
10+
This module contains provides several components:
11+
12+
``MbedCrypto``
13+
``MbedTLS``
14+
``MbedX509``
15+
16+
Import targets exist for each component.
17+
18+
Imported Targets
19+
^^^^^^^^^^^^^^^^
20+
21+
This module defines the :prop_tgt:`IMPORTED` targets:
22+
23+
``MbedTLS::MbedCrypto``
24+
Crypto component
25+
26+
``MbedTLS::MbedTLS``
27+
TLS component
28+
29+
``MbedTLS::MbedX509``
30+
X509 component
31+
32+
Result Variables
33+
^^^^^^^^^^^^^^^^
34+
35+
This module sets the following variables:
36+
37+
``MbedTLS_FOUND``
38+
True, if all required components and the core library were found.
39+
``MbedTLS_VERSION``
40+
Detected version of found MbedTLS libraries.
41+
42+
``MbedTLS_<COMPONENT>_VERSION``
43+
Detected version of found MbedTLS component library.
44+
45+
Cache variables
46+
^^^^^^^^^^^^^^^
47+
48+
The following cache variables may also be set:
49+
50+
``MbedTLS_<COMPONENT>_LIBRARY``
51+
Path to the library component of MbedTLS.
52+
``MbedTLS_<COMPONENT>_INCLUDE_DIR``
53+
Directory containing ``<COMPONENT>.h``.
54+
55+
Distributed under the MIT License, see accompanying LICENSE file or
56+
https://github.com/PatTheMav/cmake-finders/blob/master/LICENSE for details.
57+
(c) 2023 Patrick Heyer
58+
59+
#]=======================================================================]
60+
61+
# cmake-format: off
62+
# cmake-lint: disable=C0103
63+
# cmake-lint: disable=C0301
64+
# cmake-lint: disable=C0307
65+
# cmake-format: on
66+
67+
include(FindPackageHandleStandardArgs)
68+
69+
find_package(PkgConfig QUIET)
70+
if(PKG_CONFIG_FOUND)
71+
pkg_check_modules(PC_MbedTLS QUIET mbedtls mbedcrypto mbedx509)
72+
endif()
73+
74+
# MbedTLS_set_soname: Set SONAME on imported library targets
75+
macro(MbedTLS_set_soname component)
76+
if(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
77+
execute_process(
78+
COMMAND sh -c "otool -D '${Mbed${component}_LIBRARY}' | grep -v '${Mbed${component}_LIBRARY}'"
79+
OUTPUT_VARIABLE _output
80+
RESULT_VARIABLE _result)
81+
82+
if(_result EQUAL 0 AND _output MATCHES "^@rpath/")
83+
set_property(TARGET MbedTLS::Mbed${component} PROPERTY IMPORTED_SONAME "${_output}")
84+
endif()
85+
elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|FreeBSD")
86+
execute_process(
87+
COMMAND sh -c "objdump -p '${Mbed${component}_LIBRARY}' | grep SONAME"
88+
OUTPUT_VARIABLE _output
89+
RESULT_VARIABLE _result)
90+
91+
if(_result EQUAL 0)
92+
string(REGEX REPLACE "[ \t]+SONAME[ \t]+([^ \t]+)" "\\1" _soname "${_output}")
93+
set_property(TARGET MbedTLS::Mbed${component} PROPERTY IMPORTED_SONAME "${_soname}")
94+
unset(_soname)
95+
endif()
96+
endif()
97+
unset(_output)
98+
unset(_result)
99+
endmacro()
100+
101+
find_path(
102+
MbedTLS_INCLUDE_DIR
103+
NAMES mbedtls/ssl.h
104+
HINTS "${PC_MbedTLS_INCLUDE_DIRS}"
105+
PATHS /usr/include /usr/local/include
106+
DOC "MbedTLS include directory")
107+
108+
if(PC_MbedTLS_VERSION VERSION_GREATER 0)
109+
set(MbedTLS_VERSION ${PC_MbedTLS_VERSION})
110+
elseif(EXISTS "${MbedTLS_INCLUDE_DIR}/mbedtls/build_info.h")
111+
file(STRINGS "${MbedTLS_INCLUDE_DIR}/mbedtls/build_info.h" _VERSION_STRING
112+
REGEX "#define[ \t]+MBEDTLS_VERSION_STRING[ \t]+.+")
113+
string(REGEX REPLACE ".*#define[ \t]+MBEDTLS_VERSION_STRING[ \t]+\"(.+)\".*" "\\1" MbedTLS_VERSION
114+
"${_VERSION_STRING}")
115+
else()
116+
if(NOT MbedTLS_FIND_QUIETLY)
117+
message(AUTHOR_WARNING "Failed to find MbedTLS version.")
118+
endif()
119+
set(MbedTLS_VERSION 0.0.0)
120+
endif()
121+
122+
find_library(
123+
MbedTLS_LIBRARY
124+
NAMES libmbedtls mbedtls
125+
HINTS "${PC_MbedTLS_LIBRARY_DIRS}"
126+
PATHS /usr/lib /usr/local/lib
127+
DOC "MbedTLS location")
128+
129+
find_library(
130+
MbedCrypto_LIBRARY
131+
NAMES libmbedcrypto mbedcrypto
132+
HINTS "${PC_MbedTLS_LIBRARY_DIRS}"
133+
PATHS /usr/lib /usr/local/lib
134+
DOC "MbedCrypto location")
135+
136+
find_library(
137+
MbedX509_LIBRARY
138+
NAMES libmbedx509 mbedx509
139+
HINTS "${PC_MbedTLS_LIBRARY_DIRS}"
140+
PATHS /usr/lib /usr/local/lib
141+
DOC "MbedX509 location")
142+
143+
if(MbedTLS_LIBRARY
144+
AND NOT MbedCrypto_LIBRARY
145+
AND NOT MbedX509_LIBRARY)
146+
set(CMAKE_REQUIRED_LIBRARIES "${MbedTLS_LIBRARY}")
147+
set(CMAKE_REQUIRED_INCLUDES "${MbedTLS_INCLUDE_DIR}")
148+
149+
check_symbol_exists(mbedtls_x509_crt_init "mbedtls/x590_crt.h" MbedTLS_INCLUDES_X509)
150+
check_symbol_exists(mbedtls_sha256_init "mbedtls/sha256.h" MbedTLS_INCLUDES_CRYPTO)
151+
unset(CMAKE_REQUIRED_LIBRARIES)
152+
unset(CMAKE_REQUIRED_INCLUDES)
153+
endif()
154+
155+
if(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin|Windows")
156+
set(MbedTLS_ERROR_REASON "Ensure that an MbedTLS distribution is provided as part of CMAKE_PREFIX_PATH.")
157+
elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|FreeBSD")
158+
set(MbedTLS_ERROR_REASON "Ensure that MbedTLS is installed on the system.")
159+
endif()
160+
161+
if(MbedTLS_INCLUDES_X509 AND MbedTLS_INCLUDES_CRYPTO)
162+
find_package_handle_standard_args(
163+
MbedTLS
164+
REQUIRED_VARS MbedTLS_LIBRARY MbedTLS_INCLUDE_DIR
165+
VERSION_VAR MbedTLS_VERSION REASON_FAILURE_MESSAGE "${MbedTLS_ERROR_REASON}")
166+
mark_as_advanced(MbedTLS_LIBRARY MbedTLS_INCLUDE_DIR)
167+
list(APPEND _COMPONENTS TLS)
168+
else()
169+
find_package_handle_standard_args(
170+
MbedTLS
171+
REQUIRED_VARS MbedTLS_LIBRARY MbedCrypto_LIBRARY MbedX509_LIBRARY MbedTLS_INCLUDE_DIR
172+
VERSION_VAR MbedTLS_VERSION REASON_FAILURE_MESSAGE "${MbedTLS_ERROR_REASON}")
173+
mark_as_advanced(MbedTLS_LIBRARY MbedCrypto_LIBRARY MbedX509_LIBRARY MbedTLS_INCLUDE_DIR)
174+
list(APPEND _COMPONENTS TLS Crypto X509)
175+
endif()
176+
unset(MbedTLS_ERROR_REASON)
177+
178+
if(MbedTLS_FOUND)
179+
foreach(component IN LISTS _COMPONENTS)
180+
if(NOT TARGET MbedTLS::Mbed${component})
181+
if(IS_ABSOLUTE "${Mbed${component}_LIBRARY}")
182+
add_library(MbedTLS::Mbed${component} UNKNOWN IMPORTED)
183+
set_property(TARGET MbedTLS::Mbed${component} PROPERTY IMPORTED_LOCATION "${Mbed${component}_LIBRARY}")
184+
else()
185+
add_library(MbedTLS::Mbed${component} INTERFACE IMPORTED)
186+
set_property(TARGET MbedTLS::Mbed${component} PROPERTY IMPORTED_LIBNAME "${Mbed${component}_LIBRARY}")
187+
endif()
188+
189+
mbedtls_set_soname(${component})
190+
set_target_properties(
191+
MbedTLS::MbedTLS
192+
PROPERTIES INTERFACE_COMPILE_OPTIONS "${PC_MbedTLS_CFLAGS_OTHER}"
193+
INTERFACE_INCLUDE_DIRECTORIES "${MbedTLS_INCLUDE_DIR}"
194+
VERSION ${MbedTLS_VERSION})
195+
endif()
196+
endforeach()
197+
198+
if(MbedTLS_INCLUDES_X509 AND MbedTLS_INCLUDES_CRYPTO)
199+
set(MbedTLS_LIBRARIES ${MbedTLS_LIBRARY})
200+
set(MBEDTLS_INCLUDE_DIRS ${MbedTLS_INCLUDE_DIR})
201+
else()
202+
set(MbedTLS_LIBRARIES ${MbedTLS_LIBRARY} ${MbedCrypto_LIBRARY} ${MbedX509_LIBRARY})
203+
set_property(TARGET MbedTLS::MbedTLS PROPERTY INTERFACE_LINK_LIBRARIES MbedTLS::MbedCrypto MbedTLS::MbedX509)
204+
set(MBEDTLS_INCLUDE_DIRS ${MbedTLS_INCLUDE_DIR})
205+
endif()
206+
endif()
207+
208+
include(FeatureSummary)
209+
set_package_properties(
210+
MbedTLS PROPERTIES
211+
URL "https://www.trustedfirmware.org/projects/mbed-tls"
212+
DESCRIPTION
213+
"A C library implementing cryptographic primitives, X.509 certificate manipulation, and the SSL/TLS and DTLS protocols."
214+
)

0 commit comments

Comments
 (0)