Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

- name: Setup
run: |
brew install json-c libmd
brew install json-c libmd mbedtls@3

- name: Build minimal version
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/openwrt-ci-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

env:
CI_ENABLE_UNIT_TESTING: 1
CI_TARGET_BUILD_DEPENDS: libnl-tiny ubus uci
CI_TARGET_BUILD_DEPENDS: libnl-tiny ubus uci mbedtls

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include(CheckSymbolExists)
include(CheckCSourceCompiles)

project(ucode C)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
add_definitions(-Os -Wall -Werror --std=gnu99 -ffunction-sections -fwrapv -D_GNU_SOURCE)

if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 6)
Expand Down Expand Up @@ -40,6 +41,7 @@ find_library(libubus NAMES ubus)
find_library(libblobmsg_json NAMES blobmsg_json)
find_package(ZLIB)
find_library(libmd NAMES libmd.a md)
find_package(MbedTLS 2.16.0)

if(LINUX)
find_library(libnl_tiny NAMES nl-tiny)
Expand Down Expand Up @@ -69,6 +71,10 @@ if(libmd)
set(DEFAULT_DIGEST_SUPPORT ON)
endif()

if(MbedTLS_FOUND)
set(DEFAULT_CRYPTO_MBEDTLS_SUPPORT ON)
endif()

option(DEBUG_SUPPORT "Debug plugin support" ON)
option(FS_SUPPORT "Filesystem plugin support" ON)
option(MATH_SUPPORT "Math plugin support" ON)
Expand All @@ -84,6 +90,7 @@ option(SOCKET_SUPPORT "Socket plugin support" ON)
option(ZLIB_SUPPORT "Zlib plugin support" ${DEFAULT_ZLIB_SUPPORT})
option(DIGEST_SUPPORT "Digest plugin support" ${DEFAULT_DIGEST_SUPPORT})
option(DIGEST_SUPPORT_EXTENDED "Enable additional hash algorithms" ${DEFAULT_DIGEST_SUPPORT})
option(CRYPTO_MBEDTLS_SUPPORT "Crypto Mbed-TLS plugin support" ${DEFAULT_CRYPTO_MBEDTLS_SUPPORT})

set(LIB_SEARCH_PATH "${CMAKE_INSTALL_PREFIX}/lib/ucode/*.so:${CMAKE_INSTALL_PREFIX}/share/ucode/*.uc:./*.so:./*.uc" CACHE STRING "Default library search path")
string(REPLACE ":" "\", \"" LIB_SEARCH_DEFINE "${LIB_SEARCH_PATH}")
Expand Down Expand Up @@ -342,6 +349,16 @@ if(DIGEST_SUPPORT)
target_link_libraries(digest_lib ${libmd})
endif()

if(CRYPTO_MBEDTLS_SUPPORT)
set(LIBRARIES ${LIBRARIES} crypto_mbedtls_lib)
add_library(crypto_mbedtls_lib MODULE lib/crypto-mbedtls.c)
set_target_properties(crypto_mbedtls_lib PROPERTIES OUTPUT_NAME crypto_mbedtls PREFIX "")
target_include_directories(crypto_mbedtls_lib PRIVATE ${MBEDTLS_INCLUDE_DIR})
target_link_options(crypto_mbedtls_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS})
target_link_libraries(crypto_mbedtls_lib libucode ${MBEDTLS_LIBRARIES})
install(FILES lib/crypto.uc DESTINATION share/ucode)
endif()

if(UNIT_TESTING)
enable_testing()
add_definitions(-DUNIT_TESTING)
Expand Down
182 changes: 182 additions & 0 deletions cmake/FindMbedTLS.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# Copyright 2024 AVSystem <avsystem@avsystem.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#.rst:
# FindMbedTLS
# -----------
#
# Find the mbedTLS encryption library.
#
# Imported Targets
# ^^^^^^^^^^^^^^^^
#
# This module defines the following :prop_tgt:`IMPORTED` targets:
#
# ``mbedtls``
# The mbedTLS ``mbedtls`` library, if found.
# ``mbedcrypto``
# The mbedtls ``crypto`` library, if found.
# ``mbedx509``
# The mbedtls ``x509`` library, if found.
#
# Result Variables
# ^^^^^^^^^^^^^^^^
#
# This module will set the following variables in your project:
#
# ``MBEDTLS_FOUND``
# System has the mbedTLS library.
# ``MBEDTLS_INCLUDE_DIR``
# The mbedTLS include directory.
# ``MBEDTLS_LIBRARY``
# The mbedTLS SSL library.
# ``MBEDTLS_CRYPTO_LIBRARY``
# The mbedTLS crypto library.
# ``MBEDTLS_X509_LIBRARY``
# The mbedTLS x509 library.
# ``MBEDTLS_LIBRARIES``
# All mbedTLS libraries.
# ``MBEDTLS_VERSION``
# This is set to ``$major.$minor.$patch``.
# ``MBEDTLS_VERSION_MAJOR``
# Set to major mbedTLS version number.
# ``MBEDTLS_VERSION_MINOR``
# Set to minor mbedTLS version number.
# ``MBEDTLS_VERSION_PATCH``
# Set to patch mbedTLS version number.
#
# Hints
# ^^^^^
#
# Set ``MBEDTLS_ROOT_DIR`` to the root directory of an mbedTLS installation.
# Set ``MBEDTLS_USE_STATIC_LIBS`` to ``TRUE`` to look for static libraries.

set(_ORIG_FIND_ROOT_PATH_MODE_INCLUDE "${CMAKE_FIND_ROOT_PATH_MODE_INCLUDE}")
set(_ORIG_FIND_ROOT_PATH_MODE_LIBRARY "${CMAKE_FIND_ROOT_PATH_MODE_LIBRARY}")

if(MBEDTLS_ROOT_DIR)
# Disable re-rooting paths in find_path/find_library.
# This assumes MBEDTLS_ROOT_DIR is an absolute path.
set(_EXTRA_FIND_ARGS "NO_DEFAULT_PATH")
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)
endif()

find_path(MBEDTLS_INCLUDE_DIR
NAMES mbedtls/ssl.h
PATH_SUFFIXES include
HINTS ${MBEDTLS_ROOT_DIR}
${_EXTRA_FIND_ARGS})

# based on https://github.com/ARMmbed/mbedtls/issues/298
set(MBEDTLS_BUILD_INFO_FILE)
if(MBEDTLS_INCLUDE_DIR)
if(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
# Mbed TLS 3.x
set(MBEDTLS_BUILD_INFO_FILE "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
elseif(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")
# Mbed TLS 2.x
set(MBEDTLS_BUILD_INFO_FILE "${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h")
endif()
endif()

if(MBEDTLS_BUILD_INFO_FILE)
file(STRINGS "${MBEDTLS_BUILD_INFO_FILE}" VERSION_STRING_LINE REGEX "^#define MBEDTLS_VERSION_STRING[ \\t\\n\\r]+\"[^\"]*\"$")
file(STRINGS "${MBEDTLS_BUILD_INFO_FILE}" VERSION_MAJOR_LINE REGEX "^#define MBEDTLS_VERSION_MAJOR[ \\t\\n\\r]+[0-9]+$")
file(STRINGS "${MBEDTLS_BUILD_INFO_FILE}" VERSION_MINOR_LINE REGEX "^#define MBEDTLS_VERSION_MINOR[ \\t\\n\\r]+[0-9]+$")
file(STRINGS "${MBEDTLS_BUILD_INFO_FILE}" VERSION_PATCH_LINE REGEX "^#define MBEDTLS_VERSION_PATCH[ \\t\\n\\r]+[0-9]+$")

string(REGEX REPLACE "^#define MBEDTLS_VERSION_STRING[ \\t\\n\\r]+\"([^\"]*)\"$" "\\1" MBEDTLS_VERSION "${VERSION_STRING_LINE}")
string(REGEX REPLACE "^#define MBEDTLS_VERSION_MAJOR[ \\t\\n\\r]+([0-9]+)$" "\\1" MBEDTLS_VERSION_MAJOR "${VERSION_MAJOR_LINE}")
string(REGEX REPLACE "^#define MBEDTLS_VERSION_MINOR[ \\t\\n\\r]+([0-9]+)$" "\\1" MBEDTLS_VERSION_MINOR "${VERSION_MINOR_LINE}")
string(REGEX REPLACE "^#define MBEDTLS_VERSION_PATCH[ \\t\\n\\r]+([0-9]+)$" "\\1" MBEDTLS_VERSION_PATCH "${VERSION_PATCH_LINE}")
endif()


if(MBEDTLS_USE_STATIC_LIBS)
set(_MBEDTLS_LIB_NAME libmbedtls.a)
set(_MBEDTLS_CRYPTO_LIB_NAME libmbedcrypto.a)
set(_MBEDTLS_X509_LIB_NAME libmbedx509.a)
else()
set(_MBEDTLS_LIB_NAME mbedtls)
set(_MBEDTLS_CRYPTO_LIB_NAME mbedcrypto)
set(_MBEDTLS_X509_LIB_NAME mbedx509)
endif()

find_library(MBEDTLS_LIBRARY
NAMES ${_MBEDTLS_LIB_NAME}
PATH_SUFFIXES lib
HINTS ${MBEDTLS_ROOT_DIR}
${_EXTRA_FIND_ARGS})

find_library(MBEDTLS_CRYPTO_LIBRARY
NAMES ${_MBEDTLS_CRYPTO_LIB_NAME}
PATH_SUFFIXES lib
HINTS ${MBEDTLS_ROOT_DIR}
${_EXTRA_FIND_ARGS})

find_library(MBEDTLS_X509_LIBRARY
NAMES ${_MBEDTLS_X509_LIB_NAME}
PATH_SUFFIXES lib
HINTS ${MBEDTLS_ROOT_DIR}
${_EXTRA_FIND_ARGS})

set(MBEDTLS_LIBRARIES ${MBEDTLS_LIBRARY} ${MBEDTLS_CRYPTO_LIBRARY} ${MBEDTLS_X509_LIBRARY})

if(MBEDTLS_INCLUDE_DIR)
set(MBEDTLS_FOUND TRUE)
endif()

set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE "${_ORIG_FIND_ROOT_PATH_MODE_INCLUDE}")
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY "${_ORIG_FIND_ROOT_PATH_MODE_LIBRARY}")


include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(MbedTLS
FOUND_VAR MBEDTLS_FOUND
REQUIRED_VARS
MBEDTLS_INCLUDE_DIR
MBEDTLS_LIBRARY
MBEDTLS_CRYPTO_LIBRARY
MBEDTLS_X509_LIBRARY
MBEDTLS_LIBRARIES
MBEDTLS_VERSION
VERSION_VAR MBEDTLS_VERSION)


if(NOT TARGET mbedcrypto)
add_library(mbedcrypto UNKNOWN IMPORTED)
set_target_properties(mbedcrypto PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${MBEDTLS_CRYPTO_LIBRARY}")
endif()

if(NOT TARGET mbedx509)
add_library(mbedx509 UNKNOWN IMPORTED)
set_target_properties(mbedx509 PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES mbedcrypto
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${MBEDTLS_X509_LIBRARY}")
endif()

if(NOT TARGET mbedtls)
add_library(mbedtls UNKNOWN IMPORTED)
set_target_properties(mbedtls PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES mbedx509
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${MBEDTLS_LIBRARY}")
endif()
4 changes: 2 additions & 2 deletions jsdoc/conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
},
"source": {
"include": ["."],
"exclude": ["CMakeFiles"],
"includePattern": ".+\\.c$"
"exclude": ["CMakeFiles", "tests"],
"includePattern": ".+\\.(c|uc)$"
},
"plugins": [
"plugins/markdown",
Expand Down
Loading