Skip to content

Commit 446f422

Browse files
committed
Mbed TLS support
Closes libcpr#420
1 parent ef5291f commit 446f422

File tree

3 files changed

+95
-9
lines changed

3 files changed

+95
-9
lines changed

.github/workflows/ci.yml

+54
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,60 @@ jobs:
8181
build-type: Release
8282
run-test: false
8383

84+
ubuntu-20-static-gcc-mbedtls:
85+
runs-on: ubuntu-20.04
86+
steps:
87+
- name: Checkout
88+
uses: actions/checkout@v3
89+
with:
90+
submodules: true
91+
- name: Update package list
92+
run: sudo apt update
93+
- name: Install libmbedtls-dev
94+
run: sudo apt install libmbedtls-dev
95+
- name: "[Release g++] Build & Test"
96+
env:
97+
CPR_BUILD_TESTS: ON
98+
CPR_BUILD_TESTS_SSL: OFF
99+
CPR_FORCE_MBEDTLS_BACKEND: ON
100+
uses: ashutoshvarma/action-cmake-build@master
101+
with:
102+
build-dir: ${{github.workspace}}/build
103+
source-dir: ${{github.workspace}}
104+
cc: gcc
105+
cxx: g++
106+
build-type: Release
107+
run-test: true
108+
ctest-options: -V
109+
configure-options: -DBUILD_SHARED_LIBS=OFF
110+
111+
ubuntu-20-shared-gcc-mbedtls:
112+
runs-on: ubuntu-20.04
113+
steps:
114+
- name: Checkout
115+
uses: actions/checkout@v3
116+
with:
117+
submodules: true
118+
- name: Update package list
119+
run: sudo apt update
120+
- name: Install libmbedtls-dev
121+
run: sudo apt install libmbedtls-dev
122+
- name: "[Release g++] Build & Test"
123+
env:
124+
CPR_BUILD_TESTS: ON
125+
CPR_BUILD_TESTS_SSL: OFF
126+
CPR_FORCE_MBEDTLS_BACKEND: ON
127+
uses: ashutoshvarma/action-cmake-build@master
128+
with:
129+
build-dir: ${{github.workspace}}/build
130+
source-dir: ${{github.workspace}}
131+
cc: gcc
132+
cxx: g++
133+
build-type: Release
134+
run-test: true
135+
ctest-options: -V
136+
configure-options: -DBUILD_SHARED_LIBS=ON
137+
84138
ubuntu-20-static-gcc-ssl:
85139
runs-on: ubuntu-20.04
86140
steps:

CMakeLists.txt

+27-9
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ cpr_option(CPR_CURL_NOSIGNAL "Set to ON to disable use of signals in libcurl." O
3939
cpr_option(CPR_USE_SYSTEM_GTEST "If ON, this project will look in the system paths for an installed gtest library. If none is found it will use the build in one." OFF)
4040
cpr_option(CPR_FORCE_USE_SYSTEM_CURL "If enabled we will use the curl lib already installed on this system." OFF)
4141
cpr_option(CPR_ENABLE_SSL "Enables or disables the SSL backend. Required to perform HTTPS requests." ON)
42-
cpr_option(CPR_FORCE_OPENSSL_BACKEND "Force to use the OpenSSL backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
43-
cpr_option(CPR_FORCE_WINSSL_BACKEND "Force to use the WinSSL backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
44-
cpr_option(CPR_FORCE_DARWINSSL_BACKEND "Force to use the DarwinSSL backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
42+
cpr_option(CPR_FORCE_OPENSSL_BACKEND "Force to use the OpenSSL backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, CPR_FORCE_MBEDTLS_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
43+
cpr_option(CPR_FORCE_WINSSL_BACKEND "Force to use the WinSSL backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, CPR_FORCE_MBEDTLS_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
44+
cpr_option(CPR_FORCE_DARWINSSL_BACKEND "Force to use the DarwinSSL backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, CPR_FORCE_MBEDTLS_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
45+
cpr_option(CPR_FORCE_MBEDTLS_BACKEND "Force to use the Mbed TLS backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, CPR_FORCE_MBEDTLS_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
4546
cpr_option(CPR_ENABLE_LINTING "Set to ON to enable clang linting." OFF)
4647
cpr_option(CPR_ENABLE_CPPCHECK "Set to ON to enable Cppcheck static analysis. Requires CPR_BUILD_TESTS and CPR_BUILD_TESTS_SSL to be OFF to prevent checking google tests source code." OFF)
4748
cpr_option(CPR_BUILD_TESTS "Set to ON to build cpr tests." OFF)
@@ -54,6 +55,9 @@ include(cmake/code_coverage.cmake)
5455
include(cmake/sanitizer.cmake)
5556
include(cmake/clear_variable.cmake)
5657

58+
# So CMake can find FindMbedTLS.cmake
59+
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
60+
5761
# Linting
5862
if(CPR_ENABLE_LINTING)
5963
include(cmake/clang-tidy.cmake)
@@ -74,8 +78,8 @@ endif()
7478

7579
# SSL
7680
if(CPR_ENABLE_SSL)
77-
if(CPR_FORCE_OPENSSL_BACKEND OR CPR_FORCE_WINSSL_BACKEND OR CPR_FORCE_DARWINSSL_BACKEND)
78-
message(STATUS "Disabled SSL backend auto detect since either CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, or CPR_FORCE_WINSSL_BACKEND is enabled.")
81+
if(CPR_FORCE_OPENSSL_BACKEND OR CPR_FORCE_WINSSL_BACKEND OR CPR_FORCE_DARWINSSL_BACKEND OR CPR_FORCE_MBEDTLS_BACKEND)
82+
message(STATUS "Disabled SSL backend auto detect since either CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, CPR_FORCE_MBEDTLS_BACKEND, or CPR_FORCE_WINSSL_BACKEND is enabled.")
7983
set(DETECT_SSL_BACKEND OFF CACHE INTERNAL "" FORCE)
8084
else()
8185
message(STATUS "Automatically detecting SSL backend.")
@@ -101,7 +105,12 @@ if(CPR_ENABLE_SSL)
101105
message(STATUS "SSL auto detect: Using OpenSSL.")
102106
set(SSL_BACKEND_USED "OpenSSL")
103107
else()
104-
message(FATAL_ERROR "No valid SSL backend found! Please install OpenSSL or disable SSL by setting CPR_ENABLE_SSL to OFF.")
108+
find_package(MbedTLS)
109+
if(MBEDTLS_FOUND)
110+
set(SSL_BACKEND_USED "MbedTLS")
111+
else()
112+
message(FATAL_ERROR "No valid SSL backend found! Please install OpenSSL, Mbed TLS or disable SSL by setting CPR_ENABLE_SSL to OFF.")
113+
endif()
105114
endif()
106115
endif()
107116
else()
@@ -120,6 +129,10 @@ if(CPR_ENABLE_SSL)
120129
message(STATUS "Using DarwinSSL.")
121130
set(CPR_BUILD_TESTS_SSL OFF)
122131
set(SSL_BACKEND_USED "DarwinSSL")
132+
elseif(CPR_FORCE_MBEDTLS_BACKEND)
133+
message(STATUS "Using Mbed TLS.")
134+
set(CPR_BUILD_TESTS_SSL OFF)
135+
set(SSL_BACKEND_USED "MbedTLS")
123136
endif()
124137
endif()
125138
endif()
@@ -135,9 +148,9 @@ if (NOT isMultiConfig)
135148
else ()
136149
unset(CMAKE_BUILD_TYPE)
137150
foreach(TYPE ${ALLOWED_BUILD_TYPES})
138-
if (NOT ${TYPE} IN_LIST CMAKE_CONFIGURATION_TYPES)
139-
list(APPEND CMAKE_CONFIGURATION_TYPES ${TYPE})
140-
endif()
151+
if (NOT ${TYPE} IN_LIST CMAKE_CONFIGURATION_TYPES)
152+
list(APPEND CMAKE_CONFIGURATION_TYPES ${TYPE})
153+
endif()
141154
endforeach()
142155
endif()
143156

@@ -206,13 +219,18 @@ else()
206219
set(CURL_USE_SECTRANSP ON CACHE INTERNAL "" FORCE)
207220
endif()
208221

222+
if(SSL_BACKEND_USED STREQUAL "MbedTLS")
223+
set(CURL_USE_MBEDTLS ON CACHE INTERNAL "" FORCE)
224+
endif()
225+
209226
message(STATUS "Enabled curl SSL")
210227
else()
211228
set(SSL_ENABLED OFF CACHE INTERNAL "" FORCE)
212229
set(CURL_CA_PATH "none" CACHE INTERNAL "" FORCE)
213230
set(CURL_USE_SCHANNEL OFF CACHE INTERNAL "" FORCE)
214231
set(CURL_USE_OPENSSL OFF CACHE INTERNAL "" FORCE)
215232
set(CURL_USE_SECTRANSP OFF CACHE INTERNAL "" FORCE)
233+
set(CURL_USE_MBEDTLS OFF CACHE INTERNAL "" FORCE)
216234
message(STATUS "Disabled curl SSL")
217235
endif()
218236
# Disable linting for curl

cmake/FindMbedTLS.cmake

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Source: https://github.com/curl/curl/blob/curl-7_82_0/CMake/FindMbedTLS.cmake
2+
find_path(MBEDTLS_INCLUDE_DIRS mbedtls/ssl.h)
3+
4+
find_library(MBEDTLS_LIBRARY mbedtls)
5+
find_library(MBEDX509_LIBRARY mbedx509)
6+
find_library(MBEDCRYPTO_LIBRARY mbedcrypto)
7+
8+
set(MBEDTLS_LIBRARIES "${MBEDTLS_LIBRARY}" "${MBEDX509_LIBRARY}" "${MBEDCRYPTO_LIBRARY}")
9+
10+
include(FindPackageHandleStandardArgs)
11+
find_package_handle_standard_args(MbedTLS DEFAULT_MSG
12+
MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)
13+
14+
mark_as_advanced(MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)

0 commit comments

Comments
 (0)