Skip to content

Commit

Permalink
Work on mbedTLS for ESP32 (#1413)
Browse files Browse the repository at this point in the history
***NO_CI***
  • Loading branch information
josesimoes authored Jul 22, 2019
1 parent de8700f commit f45f2d7
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 32 deletions.
6 changes: 5 additions & 1 deletion CMake/Modules/FindNF_CoreCLR.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ set(NF_CoreCLR_SRCS

# Helpers
printf.c
base64.c

# HAL
nanoHAL_Time.cpp
Expand All @@ -178,6 +177,11 @@ set(NF_CoreCLR_SRCS
target_BlockStorage.c
)

# need a conditional include because of ESP32 building network as a library
if(NOT USE_SECURITY_MBEDTLS_OPTION)
list(APPEND NF_CoreCLR_SRCS base64.c)
endif()

# include configuration manager file
if(NF_FEATURE_HAS_CONFIG_BLOCK)
# feature enabled, full support
Expand Down
8 changes: 6 additions & 2 deletions CMake/Modules/FindNF_Networking.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ set(NF_Networking_SRCS
LwIP_Sockets.cpp
LwIP_Sockets_functions.cpp

ssl_stubs.cpp
)

# need a conditional include because of ESP32 building network as a library
if(NOT USE_SECURITY_MBEDTLS_OPTION)
list(APPEND NF_Networking_SRCS ssl_stubs.cpp)
endif()

# source files for security layer
set(NF_Networking_Security_SRCS

Expand Down Expand Up @@ -66,7 +70,7 @@ foreach(SRC_FILE ${NF_Networking_SRCS})
${PROJECT_SOURCE_DIR}/src/PAL/COM/sockets/ssl

if(USE_SECURITY_MBEDTLS_OPTION)
# ${PROJECT_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS
${PROJECT_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS
elseif(USE_SECURITY_OPENSSL_OPTION)
${PROJECT_SOURCE_DIR}/src/PAL/COM/sockets/ssl/openssl
endif()
Expand Down
8 changes: 7 additions & 1 deletion CMake/Modules/FindmbedTLS.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# we are replicating their CMakeList here. Actually this is more a simplified version...

# List of the required include paths
list(APPEND mbedTLS_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/mbedTLS_Source/include/)
list(APPEND mbedTLS_INCLUDE_DIRS ${PROJECT_BINARY_DIR}/mbedTLS_Source/include/)

option(USE_PKCS11_HELPER_LIBRARY "Build mbed TLS with the pkcs11-helper library." OFF)
option(ENABLE_ZLIB_SUPPORT "Build mbed TLS with zlib library." OFF)
Expand Down Expand Up @@ -81,6 +81,9 @@ set(src_crypto
version.c
version_features.c
xtea.c

# platform implementation of hardware random provider
mbedtls_entropy_hardware_pool.c
)

set(src_x509
Expand Down Expand Up @@ -113,6 +116,8 @@ foreach(SRC_FILE ${src_crypto})
PATHS
${PROJECT_BINARY_DIR}/mbedTLS_Source/library

${BASE_PATH_FOR_CLASS_LIBRARIES_MODULES}/

CMAKE_FIND_ROOT_PATH_BOTH
)
# message("${SRC_FILE} >> ${MBEDTLS_SRC_FILE}") # debug helper
Expand All @@ -121,6 +126,7 @@ endforeach()

# unset this warning as error required for this source file
SET_SOURCE_FILES_PROPERTIES( ${PROJECT_BINARY_DIR}/mbedTLS_Source/library/hmac_drbg.c PROPERTIES COMPILE_FLAGS -Wno-maybe-uninitialized)
SET_SOURCE_FILES_PROPERTIES( ${PROJECT_BINARY_DIR}/mbedTLS_Source/library/x509_crt.c PROPERTIES COMPILE_FLAGS -Wno-maybe-uninitialized)

foreach(SRC_FILE ${src_x509})
set(MBEDTLS_SRC_FILE SRC_FILE -NOTFOUND)
Expand Down
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -625,11 +625,14 @@ endif()
# ChibiOS
if(RTOS_CHIBIOS_CHECK)

add_subdirectory(targets/CMSIS-OS/common/Include)
# Define base path for the class libraries
set(BASE_PATH_FOR_CLASS_LIBRARIES_MODULES "${PROJECT_SOURCE_DIR}/targets/CMSIS-OS/ChibiOS/nanoCLR")

add_subdirectory(targets/CMSIS-OS/ChibiOS)
add_subdirectory(targets/CMSIS-OS/common)
add_subdirectory(targets/CMSIS-OS/common/Include)
add_subdirectory(targets/CMSIS-OS/nanoBooter)
add_subdirectory(targets/CMSIS-OS/nanoCLR)
add_subdirectory(targets/CMSIS-OS/ChibiOS)

# now add the subdirectory for the board
# try to find board in the targets folder
Expand Down Expand Up @@ -677,6 +680,8 @@ elseif(RTOS_FREERTOS_ESP32_CHECK)
# Define base path for the class libraries
set(BASE_PATH_FOR_CLASS_LIBRARIES_MODULES "${PROJECT_SOURCE_DIR}/targets/FreeRTOS_ESP32/${ESP32_BOARD}/nanoCLR")

add_subdirectory(targets/FreeRTOS_ESP32)

# set target base location
set(TARGET_BASE_LOCATION "${PROJECT_SOURCE_DIR}/targets/FreeRTOS_ESP32/${ESP32_BOARD}")
add_subdirectory(targets/FreeRTOS_ESP32/${ESP32_BOARD})
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ jobs:
matrix:
ESP32_WROOM_32:
BoardName: ESP32_WROOM_32
BuildOptions: -DTARGET_SERIES=ESP32 -DRTOS=FREERTOS_ESP32 -DNF_WP_IMPLEMENTS_CRC32=OFF -DNF_FEATURE_DEBUGGER=ON -DNF_FEATURE_RTC=ON -DNF_FEATURE_HAS_CONFIG_BLOCK=ON -DNF_FEATURE_HAS_SDCARD=ON -DAPI_System.Math=ON -DAPI_Windows.Devices.Gpio=ON -DAPI_Windows.Devices.Spi=ON -DAPI_Windows.Devices.I2c=ON -DAPI_Windows.Devices.Pwm=ON -DAPI_Windows.Devices.SerialCommunication=ON -DAPI_Windows.Devices.Adc=ON -DAPI_System.Net=ON -DAPI_Windows.Devices.Wifi=ON -DAPI_Windows.Storage=ON -DNF_SECURITY_OPENSSL=ON -DAPI_Hardware.Esp32=ON -DSUPPORT_ANY_BASE_CONVERSION=ON -DAPI_nanoFramework.Devices.OneWire=ON
BuildOptions: -DTARGET_SERIES=ESP32 -DRTOS=FREERTOS_ESP32 -DNF_WP_IMPLEMENTS_CRC32=OFF -DNF_FEATURE_DEBUGGER=ON -DNF_FEATURE_RTC=ON -DNF_FEATURE_HAS_CONFIG_BLOCK=ON -DNF_FEATURE_HAS_SDCARD=ON -DAPI_System.Math=ON -DAPI_Windows.Devices.Gpio=ON -DAPI_Windows.Devices.Spi=ON -DAPI_Windows.Devices.I2c=ON -DAPI_Windows.Devices.Pwm=ON -DAPI_Windows.Devices.SerialCommunication=ON -DAPI_Windows.Devices.Adc=ON -DAPI_System.Net=ON -DAPI_Windows.Devices.Wifi=ON -DAPI_Windows.Storage=ON -DNF_SECURITY_MBEDTLS=ON -DAPI_Hardware.Esp32=ON -DSUPPORT_ANY_BASE_CONVERSION=ON -DAPI_nanoFramework.Devices.OneWire=ON

variables:
ESP32_TOOLCHAIN_PATH: $(Agent.TempDirectory)\ESP32_Tools
Expand Down
6 changes: 0 additions & 6 deletions targets/CMSIS-OS/ChibiOS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -314,18 +314,12 @@ if(NF_SECURITY_MBEDTLS)

endif()

# get source dir for mbedTLS CMake project
ExternalProject_Get_Property(mbedTLS SOURCE_DIR)

set(mbedTLS_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/3rdparty/humblelogging/include")
set(mbedTLS_LIBRARIES "${CMAKE_SHARED_LIBRARY_PREFIX}mbedTLS${CMAKE_SHARED_LIBRARY_SUFFIX}")
include_directories(${mbedTLS_INCLUDE_DIRS})

endif()

# Define base path for the class libraries
set(BASE_PATH_FOR_CLASS_LIBRARIES_MODULES "${PROJECT_SOURCE_DIR}/targets/CMSIS-OS/ChibiOS/nanoCLR")

# set target base location
# this has to be set before the class library modules are pulled in
if(EXISTS ${PROJECT_SOURCE_DIR}/targets/CMSIS-OS/ChibiOS/${CHIBIOS_BOARD})
Expand Down
6 changes: 0 additions & 6 deletions targets/CMSIS-OS/ChibiOS/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ endif()
if(USE_NETWORKING_OPTION)
list(APPEND TARGET_CHIBIOS_COMMON_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/targetHAL_Network.cpp")
list(APPEND TARGET_CHIBIOS_COMMON_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/target_Network.cpp")

# append mbed TLS entropy generator, if hardware has it
if(NF_SECURITY_MBEDTLS AND USE_RNG)
list(APPEND TARGET_CHIBIOS_COMMON_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/mbedtls_entropy_hardware_pool.c")
endif()

endif()

# make var global
Expand Down
107 changes: 107 additions & 0 deletions targets/FreeRTOS_ESP32/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#
# Copyright (c) 2019 The nanoFramework project contributors
# See LICENSE file in the project root for full license information.
#

# if mbed TLS is enabled add it to the build
if(USE_SECURITY_MBEDTLS_OPTION)

# check if MBEDTLS_SOURCE was specified or if it's empty (default is empty)
set(NO_MBEDTLS_SOURCE TRUE)

if(MBEDTLS_SOURCE)
if(NOT "${MBEDTLS_SOURCE}" STREQUAL "")
set(NO_MBEDTLS_SOURCE FALSE)
endif()
endif()

# set options for mbed TLS
option(ENABLE_TESTING "no testing when building mbed TLS." OFF)

if(NO_MBEDTLS_SOURCE)
# no mbed TLS source specified, download it from it's repo

# check for Git (needed here for advanced warning to user if it's not installed)
find_package(Git)

# check if Git was found, if not report to user and abort
if(NOT GIT_EXECUTABLE)
message(FATAL_ERROR "error: could not find Git, make sure you have it installed.")
endif()

# set tag for currently supported version
set(MBEDTLS_GIT_TAG "mbedtls-2.14")

# need to setup a separate CMake project to download the code from the GitHub repository
# otherwise it won't be available before the actual build step
configure_file("${PROJECT_SOURCE_DIR}/CMake/mbedTLS.CMakeLists.cmake.in"
"${CMAKE_BINARY_DIR}/mbedTLS_Download/CMakeLists.txt")

# setup CMake project for mbedTLS download
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/mbedTLS_Download")

# run build on mbedTLS download CMake project to perform the download
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/mbedTLS_Download")

# add mbedTLS as external project
ExternalProject_Add(
mbedTLS
PREFIX mbedTLS
SOURCE_DIR ${CMAKE_BINARY_DIR}/mbedTLS_Source
GIT_REPOSITORY https://github.com/nanoframework/mbedtls
GIT_TAG ${MBEDTLS_GIT_TAG} # target specified branch
GIT_SHALLOW 1 # download only the tip of the branch, not the complete history
TIMEOUT 10
LOG_DOWNLOAD 1

# Disable all other steps
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)

else()
# mbedTLS source was specified

# sanity check is source path exists
if(EXISTS "${MBEDTLS_SOURCE}/")

# check if we already have the sources, no need to copy again
if(NOT EXISTS "${CMAKE_BINARY_DIR}/mbedTLS_Source")
message(STATUS "mbedTLS source from: ${MBEDTLS_SOURCE}")
file(COPY "${MBEDTLS_SOURCE}/" DESTINATION "${CMAKE_BINARY_DIR}/mbedTLS_Source")
else()
message(STATUS "Using local cache of mbedTLS source from ${MBEDTLS_SOURCE}")
endif()

set(MBEDTLS_INCLUDE_DIR ${CMAKE_BINARY_DIR}/mbedTLS_Source/include)
else()
message(FATAL_ERROR "Couldn't find mbedTLS source at ${MBEDTLS_SOURCE}/")
endif()

# add mbedTLS as external project
ExternalProject_Add(
mbedTLS
PREFIX mbedTLS
SOURCE_DIR ${CMAKE_BINARY_DIR}/mbedTLS_Source

# Disable all other steps
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)

endif()

# get source dir for mbedTLS CMake project
ExternalProject_Get_Property(mbedTLS SOURCE_DIR)

set(mbedTLS_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/3rdparty/humblelogging/include")
set(mbedTLS_LIBRARIES "${CMAKE_SHARED_LIBRARY_PREFIX}mbedTLS${CMAKE_SHARED_LIBRARY_SUFFIX}")
include_directories(${mbedTLS_INCLUDE_DIRS})

endif()
28 changes: 25 additions & 3 deletions targets/FreeRTOS_ESP32/ESP32_WROOM_32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ else()
set(HAL_USE_RTC_OPTION FALSE CACHE INTERNAL "NF feature RTC")
endif()

# security provider is mbedTLS
if(NF_SECURITY_MBEDTLS)
find_package(mbedTLS REQUIRED)
endif()

#######################################

add_subdirectory("common")
Expand All @@ -54,12 +59,23 @@ add_subdirectory("IDF")
add_subdirectory("Network")
add_subdirectory("nanoCLR")

# mbed TLS requires a config file
if(USE_SECURITY_MBEDTLS_OPTION)
# this seems to be only option to properly set a compiler define through the command line that needs to be a string literal
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBEDTLS_CONFIG_FILE=\"<${PROJECT_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS/nf_mbedtls_config.h>\"")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBEDTLS_CONFIG_FILE=\"<${PROJECT_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS/nf_mbedtls_config.h>\"")
endif()

# Build the networking components as a separate library
# This is done this way to stop "Createprocess: file no found" errors in linker when object input file is greater than 32k
if(USE_NETWORKING_OPTION)
add_library(NetworkLib STATIC ${NF_Networking_SOURCES} "${TARGET_ESP32_NETWORK_SOURCES}" "${TARGET_LWIP_SOURCES}" )
add_library(NetworkLib STATIC ${NF_Networking_SOURCES} ${TARGET_ESP32_NETWORK_SOURCES} ${TARGET_LWIP_SOURCES} ${mbedTLS_SOURCES} )
endif()

# add dependency for security provider mbedTLS
if(USE_SECURITY_MBEDTLS_OPTION)
add_dependencies(NetworkLib mbedTLS)
endif()

#######################
# nanoCLR executable
Expand Down Expand Up @@ -130,7 +146,7 @@ foreach( IDF_libraries ${PROJECT_LINK_LIBS} )
endforeach( IDF_libraries )

if(USE_NETWORKING_OPTION)
set_property(TARGET ${NANOCLR_PROJECT_NAME}.elf APPEND_STRING PROPERTY LINK_FLAGS " -L${CMAKE_CURRENT_BINARY_DIR} -lNetworkLib " )
set_property(TARGET ${NANOCLR_PROJECT_NAME}.elf APPEND_STRING PROPERTY LINK_FLAGS " -L${CMAKE_CURRENT_BINARY_DIR} -lNetworkLib " )
endif()

set_property(TARGET ${NANOCLR_PROJECT_NAME}.elf APPEND_STRING PROPERTY LINK_FLAGS " -lgcc -lstdc++ -Wl,--end-group -Wl,-EL ")
Expand Down Expand Up @@ -175,8 +191,9 @@ target_include_directories(${NANOCLR_PROJECT_NAME}.elf PUBLIC
if(USE_NETWORKING_OPTION)
target_include_directories(NetworkLib PUBLIC
${CMAKE_CURRENT_BINARY_DIR}/nanoCLR
${NF_CoreCLR_INCLUDE_DIRS}
${NF_CoreCLR_INCLUDE_DIRS}
${NF_Networking_INCLUDE_DIRS}
${mbedTLS_INCLUDE_DIRS}
)

target_compile_definitions(NetworkLib PUBLIC "-DPLATFORM_ESP32 " )
Expand Down Expand Up @@ -228,6 +245,11 @@ set_property(TARGET ${NANOCLR_PROJECT_NAME}.elf APPEND_STRING PROPERTY LINK_F
# add linker flags to generate map file
set_property(TARGET ${NANOCLR_PROJECT_NAME}.elf APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-Map=${PROJECT_BINARY_DIR}/${NANOCLR_PROJECT_NAME}.map")

# add dependency for networking
if(USE_SECURITY_MBEDTLS_OPTION)
add_dependencies(${NANOCLR_PROJECT_NAME}.elf NetworkLib)
endif()

# Create bin file from elf
set(GEN_ESP32_BIN_PATH ${EXECUTABLE_OUTPUT_PATH} )
set(NANOCLR_BIN_FILE ${GEN_ESP32_BIN_PATH}/${NANOCLR_PROJECT_NAME}.bin)
Expand Down
8 changes: 4 additions & 4 deletions targets/FreeRTOS_ESP32/ESP32_WROOM_32/IDF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ list(APPEND TARGET_ESP32_IDF_INCLUDES "${COMPONENT_PATH}/heap/include")
list(APPEND TARGET_ESP32_IDF_INCLUDES "${COMPONENT_PATH}/app_trace/include")
list(APPEND TARGET_ESP32_IDF_INCLUDES "${COMPONENT_PATH}/bt/include")
list(APPEND TARGET_ESP32_IDF_INCLUDES "${COMPONENT_PATH}/wear_levelling/include")
list(APPEND TARGET_ESP32_IDF_INCLUDES "${COMPONENT_PATH}/mbedtls/port/include" "${COMPONENT_PATH}/mbedtls/include" )
# list(APPEND TARGET_ESP32_IDF_INCLUDES "${COMPONENT_PATH}/mbedtls/port/include" "${COMPONENT_PATH}/mbedtls/mbedtls/include" )

list(APPEND TARGET_ESP32_IDF_INCLUDES "${COMPONENT_PATH}/nghttp/port/include")
list(APPEND TARGET_ESP32_IDF_INCLUDES "${COMPONENT_PATH}/nghttp/nghttp2/lib/includes")

list(APPEND TARGET_ESP32_IDF_INCLUDES "${COMPONENT_PATH}/openssl/include")
# list(APPEND TARGET_ESP32_IDF_INCLUDES "${COMPONENT_PATH}/openssl/include")

list(APPEND TARGET_ESP32_IDF_INCLUDES "${COMPONENT_PATH}/app_update/include")
list(APPEND TARGET_ESP32_IDF_INCLUDES "${COMPONENT_PATH}/vfs/include")
Expand Down Expand Up @@ -319,13 +319,13 @@ list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libjsmn.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libjson.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/liblibsodium.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/liblog.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libmbedtls.a)
# list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libmbedtls.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libmdns.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libmicro-ecc.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libnewlib.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libnghttp.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libnvs_flash.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libopenssl.a)
# list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libopenssl.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libsdmmc.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libsoc.a)
list(APPEND DIRECT_LINK_LIBS ${DIRECT_LINK_PATH}/libspi_flash.a)
Expand Down
7 changes: 1 addition & 6 deletions targets/FreeRTOS_ESP32/ESP32_WROOM_32/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,5 @@ list(APPEND TARGET_ESP32_COMMON_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/GenericPort
list(APPEND TARGET_ESP32_COMMON_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/targetHAL.c")
list(APPEND TARGET_ESP32_COMMON_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/targetHAL_Time.cpp")

# append networking files, if enabled
if(USE_NETWORKING_OPTION)
list(APPEND TARGET_CHIBIOS_COMMON_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/targetHAL_Network.cpp")
endif()

# make var global
# make vars global
set(TARGET_ESP32_COMMON_SOURCES ${TARGET_ESP32_COMMON_SOURCES} CACHE INTERNAL "make global")
Loading

0 comments on commit f45f2d7

Please sign in to comment.