Skip to content

Commit

Permalink
Update mbedtls to 3.4.0 (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev authored Jul 11, 2023
1 parent 6d2128f commit 2abe941
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 65 deletions.
7 changes: 4 additions & 3 deletions cmake/Findmbedtls.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ find_library(MBEDTLS_CRYPTO_LIBRARY mbedcrypto)
set(MBEDTLS_LIBRARIES ${MBEDTLS_SSL_LIBRARY} ${MBEDTLS_X509_LIBRARY} ${MBEDTLS_CRYPTO_LIBRARY})
set(MBEDTLS_LIBRARIES ${MBEDTLS_LIBRARIES} PARENT_SCOPE)

if (MBEDTLS_INCLUDE_DIR AND EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h" _MBEDTLS_VERSION_STRING REGEX "^#[\t ]*define[\t ]+MBEDTLS_VERSION_STRING[\t ]+\"[0-9]+.[0-9]+.[0-9]+\"")
string(REGEX REPLACE "^.*MBEDTLS_VERSION_STRING.*([0-9]+.[0-9]+.[0-9]+).*" "\\1" MBEDTLS_VERSION "${_MBEDTLS_VERSION_STRING}")
if (MBEDTLS_INCLUDE_DIR AND EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h" _MBEDTLS_VERSION_STRING REGEX "^#define MBEDTLS_VERSION_STRING[ \t\r\n]+\"[0-9]+.[0-9]+.[0-9]+\"")
string(REGEX REPLACE "^#define MBEDTLS_VERSION_STRING[ \t\r\n]+\"([0-9]+.[0-9]+.[0-9]+)\"" "\\1" MBEDTLS_VERSION "${_MBEDTLS_VERSION_STRING}")
message(STATUS "Using system mbedtls libraries version: \"${MBEDTLS_VERSION}\"")
endif ()

if (MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARIES AND MBEDTLS_VERSION)
Expand Down
48 changes: 11 additions & 37 deletions dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ endfunction()
set(USE_SYSTEM_MBEDTLS_LIBS ${DEFAULT_USE_SYSTEM_MBEDTLS_LIBS} CACHE BOOL "use mbedtls library from system")

if (USE_SYSTEM_MBEDTLS_LIBS)
find_package(mbedtls REQUIRED)
if (NOT MBEDTLS_FOUND)
message(STATUS "Could NOT find mbedtls system libraries, build static mbedtls libraries")
message(WARNING "Try using the mbedtls system libraries instead of the linked version. Absolutely no guarantee.")
find_package(mbedtls)
if (NOT MBEDTLS_FOUND OR NOT EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
message(STATUS "Could NOT find system mbedtls 3.0 or higher, fallback to build embedded mbedtl version")
#Fallback: build mbedtls static libray inside project
set(DEFAULT_USE_SYSTEM_MBEDTLS_LIBS OFF PARENT_SCOPE)
set(USE_SYSTEM_MBEDTLS_LIBS OFF)
else()
message(STATUS "Found system MBEDTLS version: ${MBEDTLS_VERSION}")
endif (NOT MBEDTLS_FOUND)
else()
cmake_minimum_required(VERSION 3.2)
Expand All @@ -98,23 +101,14 @@ else()

set(ENABLE_TESTING OFF CACHE BOOL "Disable mbedTLS tests")
set(ENABLE_PROGRAMS OFF CACHE BOOL "Disable mbedTLS programs")
set(MBEDTLS_FATAL_WARNINGS OFF CACHE BOOL "Disable mbedTLS crash on warnings")
set(USE_SHARED_MBEDTLS_LIBRARY OFF CACHE BOOL "Disable mbedTLS shared libraries")
set(USE_STATIC_MBEDTLS_LIBRARY ON CACHE BOOL "Enable mbedTLS static libraries")

set(MBEDTLS_DOWNLOAD_DIR "${CMAKE_BINARY_DIR}/dependencies/external/mbedtls/download")
set(MBEDTLS_SOURCE_DIR "${CMAKE_SOURCE_DIR}/dependencies/external/mbedtls")
set(MBEDTLS_BINARY_DIR "${CMAKE_BINARY_DIR}/dependencies/external/mbedtls/build")
set(MBEDTLS_INSTALL_DIR "${CMAKE_BINARY_DIR}")
if(${CMAKE_BUILD_TYPE} AND ${CMAKE_BUILD_TYPE} EQUAL "Debug")
set(MBEDTLS_LOGGING 1)
else ()
set(MBEDTLS_LOGGING 0)
endif ()

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/mbedtls)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/mbedtls)

set (MBEDTLS_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external/mbedtls/include")

set (MBEDTLS_INCLUDE_DIR "${MBEDTLS_SOURCE_DIR}/include")
set (MBEDTLS_INCLUDE_DIR ${MBEDTLS_INCLUDE_DIR} PARENT_SCOPE)
if (MBEDTLS_INCLUDE_DIR AND EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h" _MBEDTLS_VERSION_STRING REGEX "^#define MBEDTLS_VERSION_STRING[ \t\r\n]+\"[0-9]+.[0-9]+.[0-9]+\"")
string(REGEX REPLACE "^#define MBEDTLS_VERSION_STRING[ \t\r\n]+\"([0-9]+.[0-9]+.[0-9]+)\"" "\\1" MBEDTLS_VERSION "${_MBEDTLS_VERSION_STRING}")
Expand All @@ -125,26 +119,6 @@ else()
message(STATUS "Using static mbedtls libraries (build version \"${MBEDTLS_VERSION}\")")
endif ()

include_directories(${MBEDTLS_INCLUDE_DIR})

if(WIN32)
set (MBEDTLS_LIB_INSTALL_DIR_WITH_LIB_PREFIX "${MBEDTLS_INSTALL_DIR}/lib/${CMAKE_CFG_INTDIR}/")
else()
set (MBEDTLS_LIB_INSTALL_DIR_WITH_LIB_PREFIX "${MBEDTLS_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}")
endif ()

set (MBEDTLS_SSL_LIBRARY "${MBEDTLS_LIB_INSTALL_DIR_WITH_LIB_PREFIX}mbedtls${CMAKE_STATIC_LIBRARY_SUFFIX}")
set (MBEDTLS_X509_LIBRARY "${MBEDTLS_LIB_INSTALL_DIR_WITH_LIB_PREFIX}mbedx509${CMAKE_STATIC_LIBRARY_SUFFIX}")
set (MBEDTLS_CRYPTO_LIBRARY "${MBEDTLS_LIB_INSTALL_DIR_WITH_LIB_PREFIX}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX}")
set (MBEDTLS_LIBRARIES ${MBEDTLS_SSL_LIBRARY} ${MBEDTLS_X509_LIBRARY} ${MBEDTLS_CRYPTO_LIBRARY})
set (MBEDTLS_LIBRARIES ${MBEDTLS_LIBRARIES} PARENT_SCOPE)

if(${CMAKE_BUILD_TYPE} AND ${CMAKE_BUILD_TYPE} EQUAL "Debug")
message(STATUS "mbedtls libraries: ${MBEDTLS_LIBRARIES}")
endif ()

mark_as_advanced (MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARIES MBEDTLS_SSL_LIBRARY MBEDTLS_X509_LIBRARY MBEDTLS_CRYPTO_LIBRARY)

endif (USE_SYSTEM_MBEDTLS_LIBS)

if ( ENABLE_MQTT )
Expand All @@ -164,7 +138,7 @@ ENDIF()
if ( ENABLE_XZ )
option(BUILD_TESTING "" OFF)
if (NOT LIBLZMA_FOUND)
message( WARNING "Could not found system xz library. Using embedded xz library.")
message( STATUS "System library xz could not be found. Using embedded xz library.")
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/xz)
endif()
ENDIF()
2 changes: 1 addition & 1 deletion dependencies/external/mbedtls
28 changes: 10 additions & 18 deletions sources/leddevice/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,16 @@ if(ENABLE_WS281XPWM)
target_link_libraries(leddevice ws281x)
endif()

if (NOT DEFAULT_USE_SYSTEM_MBEDTLS_LIBS)
if (MBEDTLS_LIBRARIES)
include_directories(${MBEDTLS_INCLUDE_DIR})
target_link_libraries(leddevice ${MBEDTLS_LIBRARIES})
target_include_directories(leddevice PRIVATE ${MBEDTLS_INCLUDE_DIR})
endif (MBEDTLS_LIBRARIES)
endif ()

if (EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
target_compile_definitions(leddevice PRIVATE HYPERHDR_MBEDTLS_VER3)
else()
message(WARNING "MBEDTLS version 3 or above not detected")
if (MBEDTLS_LIBRARIES)
message(WARNING "Including system MBEDTLS version")
include_directories(${MBEDTLS_INCLUDE_DIR})
target_link_libraries(leddevice ${MBEDTLS_LIBRARIES})
target_include_directories(leddevice PRIVATE ${MBEDTLS_INCLUDE_DIR})
endif (MBEDTLS_LIBRARIES)
if (TARGET mbedcrypto)
target_link_libraries(leddevice mbedcrypto)
endif()

if (TARGET mbedx509)
target_link_libraries(leddevice mbedx509)
endif()

if (TARGET mbedtls)
target_link_libraries(leddevice mbedtls)
endif()

if(USE_PRECOMPILED_HEADERS AND COMMAND target_precompile_headers)
Expand Down
6 changes: 0 additions & 6 deletions sources/leddevice/dev_net/ProviderUdpSSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@
#include <QThread>

//----------- mbedtls
#if defined(HYPERHDR_MBEDTLS_VER3)
#include <mbedtls/build_info.h>
#elif !defined(MBEDTLS_CONFIG_FILE)
#include <mbedtls/config.h>
#else
#include MBEDTLS_CONFIG_FILE
#endif

#if defined(MBEDTLS_PLATFORM_C)
#include <mbedtls/platform.h>
Expand Down

0 comments on commit 2abe941

Please sign in to comment.