diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d25b337f..42a69523 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -84,6 +84,41 @@ jobs: run: | cd build-${{matrix.TLS}}-${{matrix.CC}} libtool --mode=execute valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet --suppressions=$GITHUB_WORKSPACE/tests/valgrind_suppression tests/testdriver + examples/coap-server -? 2>&1 | head -10 + build-linux2: + runs-on: ubuntu-latest + strategy: + matrix: + CC: ["gcc", "clang"] + TLS: ["mbedtls"] + OSCORE_ONLY: ["yes", "no"] + steps: + - uses: actions/checkout@v6 + - name: setup + run: | + sudo apt-get update && sudo apt-get install -y libcunit1-dev libmbedtls-dev libgnutls28-dev libwolfssl-dev libtool libtool-bin exuberant-ctags valgrind + ./autogen.sh + - name: configure OSCORE Only + if: matrix.OSCORE_ONLY == 'yes' + run: | + mkdir build-${{matrix.TLS}}-${{matrix.CC}}-${{matrix.OSCORE_ONLY}} + cd build-${{matrix.TLS}}-${{matrix.CC}}-${{matrix.OSCORE_ONLY}} + $GITHUB_WORKSPACE/configure --disable-silent-rules --disable-documentation --enable-examples --enable-tests --with-${{matrix.TLS}}-oscore --disable-dtls CC=${{matrix.CC}} + - name: configure OSCORE + DTLS + if: matrix.OSCORE_ONLY == 'no' + run: | + mkdir build-${{matrix.TLS}}-${{matrix.CC}}-${{matrix.OSCORE_ONLY}} + cd build-${{matrix.TLS}}-${{matrix.CC}}-${{matrix.OSCORE_ONLY}} + $GITHUB_WORKSPACE/configure --disable-silent-rules --disable-documentation --enable-examples --enable-tests --with-${{matrix.TLS}}-oscore --enable-dtls CC=${{matrix.CC}} + - name: compile + run: | + cd build-${{matrix.TLS}}-${{matrix.CC}}-${{matrix.OSCORE_ONLY}} + make EXTRA_CFLAGS=-Werror && make check EXTRA_CFLAGS=-Werror + - name: test + run: | + cd build-${{matrix.TLS}}-${{matrix.CC}}-${{matrix.OSCORE_ONLY}} + libtool --mode=execute valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet --suppressions=$GITHUB_WORKSPACE/tests/valgrind_suppression tests/testdriver + examples/coap-server -? 2>&1 | head -10 tinydtls-build: runs-on: ubuntu-latest steps: @@ -114,7 +149,7 @@ jobs: submodules: recursive - name: setup run: | - sudo apt-get update && sudo apt-get install -y libcunit1-dev libmbedtls-dev libgnutls28-dev libwolfssl-dev + sudo apt-get update && sudo apt-get install -y libcunit1-dev libmbedtls-dev libgnutls28-dev libwolfssl-dev valgrind cmake -E make_directory $GITHUB_WORKSPACE/build-${{matrix.TLS}}-cmake - name: configure no-TLS if: matrix.TLS == 'no' @@ -122,7 +157,7 @@ jobs: cd $GITHUB_WORKSPACE/build-${{matrix.TLS}}-cmake cmake $GITHUB_WORKSPACE -DENABLE_EXAMPLES=ON -DENABLE_TESTS=ON -DENABLE_DTLS=OFF -DENABLE_DOCS=OFF -DWARNING_TO_ERROR=ON - name: configure TLS - if: matrix.TLS != 'no' + if: matrix.TLS != 'no' && matrix.TLS != 'mbedtls-oscore-only' run: | cd $GITHUB_WORKSPACE/build-${{matrix.TLS}}-cmake cmake $GITHUB_WORKSPACE -DENABLE_EXAMPLES=ON -DENABLE_TESTS=ON -DENABLE_DTLS=ON -DENABLE_DOCS=OFF -DDTLS_BACKEND=${{matrix.TLS}} -DWARNING_TO_ERROR=ON @@ -130,6 +165,44 @@ jobs: run: | cd $GITHUB_WORKSPACE/build-${{matrix.TLS}}-cmake cmake --build . + - name: test + run: | + cd $GITHUB_WORKSPACE/build-${{matrix.TLS}}-cmake + valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet --suppressions=$GITHUB_WORKSPACE/tests/valgrind_suppression ./testdriver + ./coap-server -? 2>&1 | head -10 + cmake-build2: + runs-on: ubuntu-latest + strategy: + matrix: + TLS: ["mbedtls"] + OSCORE_ONLY: ["yes", "no"] + steps: + - uses: actions/checkout@v6 + with: + submodules: recursive + - name: setup + run: | + sudo apt-get update && sudo apt-get install -y libcunit1-dev libmbedtls-dev libgnutls28-dev libwolfssl-dev valgrind + cmake -E make_directory $GITHUB_WORKSPACE/build-${{matrix.TLS}}-${{matrix.OSCORE_ONLY}}-cmake + - name: configure OSCORE Only + if: matrix.OSCORE_ONLY == 'yes' + run: | + cd $GITHUB_WORKSPACE/build-${{matrix.TLS}}-${{matrix.OSCORE_ONLY}}-cmake + cmake $GITHUB_WORKSPACE -DENABLE_EXAMPLES=ON -DENABLE_TESTS=ON -DOSCORE_BACKEND=${{matrix.TLS}} -DENABLE_DTLS=OFF -DENABLE_DOCS=OFF -DWARNING_TO_ERROR=ON + - name: configure OSCORE and DTLS + if: matrix.OSCORE_ONLY == 'no' + run: | + cd $GITHUB_WORKSPACE/build-${{matrix.TLS}}-${{matrix.OSCORE_ONLY}}-cmake + cmake $GITHUB_WORKSPACE -DENABLE_EXAMPLES=ON -DENABLE_TESTS=ON -DOSCORE_BACKEND=${{matrix.TLS}} -DENABLE_DTLS=ON -DENABLE_DOCS=OFF -DWARNING_TO_ERROR=ON + - name: build + run: | + cd $GITHUB_WORKSPACE/build-${{matrix.TLS}}-${{matrix.OSCORE_ONLY}}-cmake + cmake --build . + - name: test + run: | + cd $GITHUB_WORKSPACE/build-${{matrix.TLS}}-${{matrix.OSCORE_ONLY}}-cmake + valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet --suppressions=$GITHUB_WORKSPACE/tests/valgrind_suppression ./testdriver + ./coap-server -? 2>&1 | head -10 cmake-macos: runs-on: macos-latest diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f9ba917..0bf8c4e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -134,6 +134,21 @@ set_property( wolfssl mbedtls tinydtls) +set(OSCORE_BACKEND + "none" + CACHE + STRING + "\ +Name of the oscore backend, only relevant if `ENABLE_DTLS` is OFF which is not the \ +default. Possible values: none and mbedtls. \ +If specified then this library will be searched and if found also used. \ +If not found then the cmake configuration will stop with an error. \ + ") +set_property( + CACHE OSCORE_BACKEND + PROPERTY STRINGS + none + mbedtls) option( USE_VENDORED_TINYDTLS "compile with the tinydtls project in the submodule if on, otherwise try to find the compiled lib with find_package" @@ -355,7 +370,7 @@ if(NOT ZEPHYR_BASE) sys/socket.h HAVE_STRUCT_CMSGHDR) endif() -endif() +endif() # ! ZEPHYR_BASE if(${ENABLE_CLIENT_MODE}) set(COAP_CLIENT_SUPPORT "1") @@ -384,8 +399,9 @@ else() message(STATUS "compiling without proxy support") endif() -if(${ENABLE_OSCORE}) +if((${ENABLE_OSCORE}) OR (${WITH_MBEDTLS_OSCORE})) set(COAP_OSCORE_SUPPORT "1") + set(ENABLE_OSCORE ON) message(STATUS "compiling with OSCORE support") else() message(STATUS "compiling without OSCORE support") @@ -509,6 +525,7 @@ set(WITH_GNUTLS OFF) set(WITH_OPENSSL OFF) set(WITH_TINYDTLS OFF) set(WITH_MBEDTLS OFF) +set(WITH_MBEDTLS_OSCORE OFF) set(WITH_WOLFSSL OFF) if(ENABLE_DTLS) @@ -557,6 +574,17 @@ if(ENABLE_DTLS) endif() if((DTLS_BACKEND STREQUAL "mbedtls") OR ((DTLS_BACKEND STREQUAL "default") AND (NOT DTLS_FOUND))) + # libmbedtls (e.g. debian libmbedtls-dev) + find_package(MbedTLS ${DTLS_REQUIRED}) + if(MbedTLS_FOUND) + set(WITH_MBEDTLS_OSCORE ON) + message(STATUS "compiling with mbedtls support") + set(COAP_WITH_LIBMBEDTLS 1) + set(DTLS_FOUND ON) + endif() + endif() + + if((OSCORE_BACKEND STREQUAL "mbedtls") OR ((DTLS_BACKEND STREQUAL "default") AND (NOT DTLS_FOUND))) # libmbedtls (e.g. debian libmbedtls-dev) find_package(MbedTLS ${DTLS_REQUIRED}) if(MbedTLS_FOUND) @@ -592,11 +620,39 @@ if(ENABLE_DTLS) endif() endif() - if((DTLS_BACKEND STREQUAL "default") AND (NOT DTLS_FOUND)) + if(((DTLS_BACKEND STREQUAL "default") OR (DTLS_BACKEND STREQUAL "ignore")) AND (NOT DTLS_FOUND)) # no cryto lib found message( FATAL_ERROR - "cannot find any cryto lib, either install one or compile without DTLS support" + "cannot find any defined cryto lib, either install one or compile without DTLS support" + ) + endif() + +elseif(NOT ((OSCORE_BACKEND STREQUAL "none") OR (OSCORE_BACKEND STREQUAL ""))) + + if((OSCORE_BACKEND STREQUAL "mbedtls") AND (NOT DTLS_FOUND)) + # libmbedtls (e.g. debian libmbedtls-dev) + find_package(MbedTLS REQUIRED) + if(MbedTLS_FOUND) + set(WITH_MBEDTLS_OSCORE ON) + message(STATUS "compiling with mbedtls OSCORE support") + set(COAP_WITH_LIBMBEDTLS_OSCORE 1) + set(DTLS_FOUND ON) + endif() + endif() + + if((ZEPHYR_BASE) AND (OSCORE_BACKEND STREQUAL "zephyr") AND (NOT DTLS_FOUND)) + set(WITH_MBEDTLS_OSCORE ON) + message(STATUS "compiling with mbedtls OSCORE support") + set(COAP_WITH_LIBMBEDTLS_OSCORE 1) + set(DTLS_FOUND ON) + endif() + + if((NOT ZEPHYR_BASE) AND (NOT DTLS_FOUND)) + # no defined cryto lib found + message( + FATAL_ERROR + "cannot find any defined cryto lib for OSCORE, either install one or compile without OSCORE support" ) endif() @@ -654,16 +710,20 @@ message(STATUS "ENABLE_DOCS:.....................${ENABLE_DOCS}") message(STATUS "ENABLE_EXAMPLES:.................${ENABLE_EXAMPLES}") message(STATUS "ENABLE_SANITIZER:................${ENABLE_SANITIZER}") message(STATUS "DTLS_BACKEND:....................${DTLS_BACKEND}") +if(NOT ENABLE_DTLS) +message(STATUS "OSCORE_BACKEND:..................${OSCORE_BACKEND}") +endif() message(STATUS "WITH_GNUTLS:.....................${WITH_GNUTLS}") message(STATUS "WITH_TINYDTLS:...................${WITH_TINYDTLS}") message(STATUS "WITH_OPENSSL:....................${WITH_OPENSSL}") message(STATUS "WITH_WOLFSSL:....................${WITH_WOLFSSL}") message(STATUS "WITH_MBEDTLS:....................${WITH_MBEDTLS}") -message(STATUS "HAVE_LIBTINYDTLS:................${COAP_WITH_LIBTINYDTLS}") -message(STATUS "HAVE_LIBGNUTLS:..................${COAP_WITH_LIBGNUTLS}") -message(STATUS "HAVE_LIBOPENSSL:.................${COAP_WITH_LIBOPENSSL}") -message(STATUS "HAVE_LIBWOLFSSL:.................${COAP_WITH_LIBWOLFSSL}") -message(STATUS "HAVE_LIBMBEDTLS:.................${COAP_WITH_LIBMBEDTLS}") +message(STATUS "WITH_MBEDTLS_OSCORE:.............${WITH_MBEDTLS_OSCORE}") +message(STATUS "HAVE_LIBTINYDTLS:................${TinyDTLS_VERSION}") +message(STATUS "HAVE_LIBGNUTLS:..................${GNUTLS_VERSION}") +message(STATUS "HAVE_LIBOPENSSL:.................${OPENSSL_VERSION}") +message(STATUS "HAVE_LIBWOLFSSL:.................${wolfssl_VERSION}") +message(STATUS "HAVE_LIBMBEDTLS:.................${MbedTLS_VERSION}") message(STATUS "WITH_EPOLL:......................${WITH_EPOLL}") message(STATUS "WITH_OBSERVE_PERSIST:............${WITH_OBSERVE_PERSIST}") message(STATUS "BUILD_SHARED_LIBS:...............${BUILD_SHARED_LIBS}") @@ -746,6 +806,7 @@ target_sources( $<$:${CMAKE_CURRENT_LIST_DIR}/src/coap_tinydtls.c> $<$:${CMAKE_CURRENT_LIST_DIR}/src/coap_gnutls.c> $<$:${CMAKE_CURRENT_LIST_DIR}/src/coap_mbedtls.c> + $<$:${CMAKE_CURRENT_LIST_DIR}/src/coap_mbedtls.c> # needed for OSCORE if enabled $<$:${CMAKE_CURRENT_LIST_DIR}/src/oscore/oscore.c> $<$:${CMAKE_CURRENT_LIST_DIR}/src/oscore/oscore_cbor.c> @@ -791,6 +852,7 @@ target_link_libraries( $<$:GnuTLS::GnuTLS> $<$:TinyDTLS::tinydtls> $<$:MbedTLS::mbedtls> + $<$:MbedTLS::mbedtls> $<$:wolfssl::wolfssl> $<$:ws2_32> $<$:iphlpapi> diff --git a/cmake_coap_defines.h.in b/cmake_coap_defines.h.in index 3d4c80ac..d3cf5b33 100644 --- a/cmake_coap_defines.h.in +++ b/cmake_coap_defines.h.in @@ -74,6 +74,9 @@ /* Define to 1 if the system has libmbedtls */ #cmakedefine COAP_WITH_LIBMBEDTLS @COAP_WITH_LIBMBEDTLS@ +/* Define to 1 if the system has libmbedtls and OSCORE only */ +#cmakedefine COAP_WITH_LIBMBEDTLS_OSCORE @COAP_WITH_LIBMBEDTLS_OSCORE@ + /* Define to 1 if the system has openssl */ #cmakedefine COAP_WITH_LIBOPENSSL @COAP_WITH_LIBOPENSSL@ diff --git a/configure.ac b/configure.ac index 53b2cabe..b2386ae3 100644 --- a/configure.ac +++ b/configure.ac @@ -398,11 +398,12 @@ AM_CONDITIONAL(BUILD_MANPAGES, [test "x$build_manpages" = "xyes"]) # __dtls__ # The Datagram Transport Layer Security (DTLS) feature needs cryptography # functions. -# We currently support the GnuTLS and OpenSSL library. The user can preselect -# the cryptography library that should be used by adding '--with-gnutls' or -# '--with-openssl'. +# We currently support the GnuTLS, OpenSSL, WolfSSL, MbedTLS and TinyDLTS libraries. +# The user can preselect the cryptography library that should be used by adding '--with-gnutls', +# '--with-openssl', --with-wolfssl, --with-mbedtls or --with-tinydtls. +# # If the user isn't using a selection we first search for GnuTLS and fallback -# to OpenSSL if the GnuTLS library couldn't be found. +# to OpenSSL, WolfSSL, mbedtls or TinyDTLS if a TLS library couldn't be found. gnutls_version_required=3.3.0 openssl_version_required=1.1.0 @@ -418,28 +419,34 @@ AC_ARG_ENABLE([dtls], AC_ARG_WITH([gnutls], [AS_HELP_STRING([--with-gnutls], - [Use GnuTLS for DTLS functions])], + [Use GnuTLS for DTLS/OSCORE functions])], [with_gnutls="$withval"], [with_gnutls="no"]) AC_ARG_WITH([openssl], [AS_HELP_STRING([--with-openssl], - [Use OpenSSL for DTLS functions])], + [Use OpenSSL for DTLS/OSCORE functions])], [with_openssl="$withval"], [with_openssl="no"]) AC_ARG_WITH([wolfssl], [AS_HELP_STRING([--with-wolfssl], - [Use wolfSSL for DTLS functions])], + [Use wolfSSL for DTLS/OSCORE functions])], [with_wolfssl="$withval"], [with_wolfssl="no"]) AC_ARG_WITH([mbedtls], [AS_HELP_STRING([--with-mbedtls], - [Use Mbed TLS for DTLS functions])], + [Use Mbed TLS for DTLS/OSCORE functions])], [with_mbedtls="$withval"], [with_mbedtls="no"]) +AC_ARG_WITH([mbedtls-oscore], + [AS_HELP_STRING([--with-mbedtls-oscore], + [Use Mbed TLS for OSCORE functions])], + [with_mbedtls_oscore="$withval"], + [with_mbedtls_oscore="no"]) + AC_ARG_WITH([tinydtls], [AS_HELP_STRING([--with-tinydtls], [Use TinyDTLS for DTLS functions])], @@ -457,6 +464,12 @@ if test "x$with_gnutls" = "xyes" -o "x$with_openssl" = "xyes" -o "x$with_wolfssl # Give an advice that '--with_gnutls', '--with_openssl', '--with_wolfssl', '--with-mbedtls' or '--with-tinydtls' was used but # DTLS support isn't configured. AC_MSG_WARN([==> Using the configure options '--with-gnutls', '--with-openssl', '--with_wolfssl', '--with-mbedtls' or '--with-tinydtls' without '--enable-dtls' is useless and will be ignored.]) + with_gnutls="no" + with_openssl="no" + with_wolfssl="no" + with_mbedtls="no" + with_tinydtls="no" + with_submodule_tinydtls="no" fi fi if test "x$with_submodule_tinydtls" = "xyes"; then @@ -482,11 +495,14 @@ if test "x$build_dtls" = "xyes"; then if test "x$with_mbedtls" = "xyes"; then TLSCOUNT=`expr $TLSCOUNT + 1` fi + if test "x$with_mbedtls_oscore" = "xyes"; then + TLSCOUNT=`expr $TLSCOUNT + 1` + fi if test "x$with_tinydtls" = "xyes"; then TLSCOUNT=`expr $TLSCOUNT + 1` fi if test "$TLSCOUNT" -gt 1; then - AC_MSG_ERROR([==> You can't use more than 1 of the options '--with-gnutls', '--with-openssl', '--with-mbedtls' or '--with-tinydtls' at the same time while '--enable-dtls' is selected! + AC_MSG_ERROR([==> You can't use more than 1 of the options '--with-gnutls', '--with-openssl', '--with-mbedtls', '--with-mbedtls-odcore' or '--with-tinydtls' at the same time while '--enable-dtls' is selected! ==> Please note, the option '--enable-dtls' is turned on by default if not explicitly disabled!]) fi @@ -510,98 +526,10 @@ if test "x$build_dtls" = "xyes"; then [have_wolfssl="no"]) # Mbed TLS - if test "x${MbedTLS_CFLAGS+set}" = "xset"; then - mbedtls_cflags_overridden="yes" - else - mbedtls_cflags_overridden="no" - fi - - if test "x${MbedTLS_LIBS+set}" = "xset"; then - mbedtls_libs_overridden="yes" - else - mbedtls_libs_overridden="no" - fi - - # If MbedTLS_CFLAGS and MbedTLS_LIBS are overridden, pkg-config will always assume the library was found, even - # though MbedTLS doesn't necessarily have a pkg-config file. Therefore, we might as well use the "old" way to - # determine whether we can link MbedTLS (and which version we have). - if test "x$mbedtls_libs_overridden" = "xyes" -a "x$mbedtls_cflags_overridden" = "xyes"; then - have_mbedtls="no" - mbedtls_has_pkgconfig="no" - else - # Attempt to find MbedTLS using pkg-config. - - # When statically linking against libcoap, all transitive dependencies need to be specified as linker flags - # as well. Use pkg-config --static for that. - if test "x$enable_static" = "xyes"; then - KEEP_PKG_CONFIG=$PKG_CONFIG - PKG_CONFIG="$PKG_CONFIG --static" - PKG_CHECK_MODULES([MbedTLS], - [mbedtls], - [have_mbedtls="yes"; mbedtls_has_pkgconfig="yes"], - [have_mbedtls="no"; mbedtls_has_pkgconfig="no"]) - PKG_CONFIG=$KEEP_PKG_CONFIG - else - PKG_CHECK_MODULES([MbedTLS], - [mbedtls], - [have_mbedtls="yes"; mbedtls_has_pkgconfig="yes"], - [have_mbedtls="no"; mbedtls_has_pkgconfig="no"]) - fi - fi - - if test "x$have_mbedtls" = "xno"; then - # Attempt to find mbedtls without pkg-config. - - # default linker flags (default CFLAGS are empty, so they don't need to be set). - if test "x$mbedtls_libs_overridden" == "xno"; then - MbedTLS_LIBS="-lmbedtls -lmbedcrypto -lmbedx509" - fi - - # check whether we can find MbedTLS - local_MbedTLS_save_CFLAGS=$CFLAGS - CFLAGS="$MbedTLS_CFLAGS $CFLAGS" - local_MbedTLS_save_LIBS=$LIBS - LIBS="$MbedTLS_LIBS $LIBS" - AC_CHECK_LIB(mbedtls, mbedtls_version_get_string, - [have_mbedtls="yes"], - [have_mbedtls="no"]) - LIBS=$local_MbedTLS_save_LIBS - CFLAGS=$local_MbedTLS_save_CFLAGS - fi - - # here, we know whether we can find MbedTLS, but need to determine the version. - if test "x$have_mbedtls" = "xyes"; then - if test "x$mbedtls_has_pkgconfig" = "xyes"; then - # If pkg-config found mbedtls, use it to determine the version. - mbedtls_version=`$PKG_CONFIG --modversion mbedtls`; - elif test "x$cross_compiling" = "xyes" ; then - # Have no option but to do this - mbedtls_version=$mbedtls_version_required - else - # Get actual library version by compiling a test program. - local_MbedTLS_save_CFLAGS=$CFLAGS - CFLAGS="$MbedTLS_CFLAGS $CFLAGS" - local_MbedTLS_save_LIBS=$LIBS - LIBS="$MbedTLS_LIBS $LIBS" - AC_LANG_PUSH(C) - AC_LINK_IFELSE([dnl - AC_LANG_SOURCE( - [[#include - #include - int main () { - char str[20]; - mbedtls_version_get_string(str); - fprintf(stdout,"%s\n",str); - return 0; - }]])], - [mbedtls_version=$(./conftest$EXEEXT)], - [AC_MSG_WARN(Failed to determine Mbed TLS version) - have_mbedtls=no]) - AC_LANG_POP(C) - LIBS=$local_MbedTLS_save_LIBS - CFLAGS=$local_MbedTLS_save_CFLAGS - fi - fi + AX_PKG_CHECK_MBEDTLS([MbedTLS], + [mbedtls], + [have_mbedtls="yes"], + [have_mbedtls="no"]) if test "${TinyDTLS_CFLAGS+set}" = "set"; then tinydtls_cflags_overridden="yes" @@ -831,6 +759,14 @@ if test "x$build_dtls" = "xyes"; then DTLS_LIBS="$MbedTLS_LIBS" AC_DEFINE(COAP_WITH_LIBMBEDTLS, [1], [Define to 1 if the system has libmbedtls2.7.10.]) fi + if test "x$with_mbedtls_oscore" = "xyes"; then + DTLS_CFLAGS="$MbedTLS_CFLAGS" + DTLS_LIBS="$MbedTLS_LIBS" + AC_DEFINE(COAP_WITH_LIBMBEDTLS, [1], [Define to 1 if the system has libmbedtls2.7.10.]) + # As we have both DTLS and OSCORE + with_mbedtls="yes" + with_mbedtls_oscore="no" + fi if test "x$with_tinydtls" = "xyes" -o "x$with_tinydtls_auto" = "xyes"; then DTLS_CFLAGS="$TinyDTLS_CFLAGS" DTLS_LIBS="$TinyDTLS_LIBS" @@ -838,6 +774,29 @@ if test "x$build_dtls" = "xyes"; then fi AC_SUBST(DTLS_CFLAGS) AC_SUBST(DTLS_LIBS) +else + # The user can't select multiple crypto libraries. + TLSCOUNT=0 + if test "x$with_mbedtls_oscore" = "xyes"; then + TLSCOUNT=`expr $TLSCOUNT + 1` + fi + if test "$TLSCOUNT" -gt 1; then + AC_MSG_ERROR([==> You can't use more than 1 of the options '--with-mbedtls-oscore' at the same time while '--disable-dtls' is selected! + ==> Please note, the option '--enable-dtls' is turned on by default if not explicitly disabled!]) + fi + + # Mbed TLS + AX_PKG_CHECK_MBEDTLS([MbedTLS], + [mbedtls_oscore], + [have_mbedtls_oscore="yes"], + [have_mbedtls_oscore="no"]) + + if test "x$with_mbedtls_oscore" = "xyes"; then + DTLS_CFLAGS="$MbedTLS_CFLAGS" + DTLS_LIBS="$MbedTLS_LIBS" + AC_DEFINE(COAP_WITH_LIBMBEDTLS_OSCORE, [1], [Define to 1 if the system has libmbedtls2.7.10.]) + fi + fi # Define the Library name extension for the TLS the library was linked against @@ -849,6 +808,8 @@ elif test "x$with_gnutls" = "xyes" -o "x$with_gnutls_auto" = "xyes"; then LIBCOAP_DTLS_LIB_EXTENSION_NAME=-gnutls elif test "x$with_mbedtls" = "xyes" -o "x$with_mbedtls_auto" = "xyes"; then LIBCOAP_DTLS_LIB_EXTENSION_NAME=-mbedtls +elif test "x$with_mbedtls_oscore" = "xyes"; then + LIBCOAP_DTLS_LIB_EXTENSION_NAME=-mbedtls-oscore elif test "x$with_tinydtls" = "xyes"; then LIBCOAP_DTLS_LIB_EXTENSION_NAME=-tinydtls else @@ -876,6 +837,10 @@ if test "x$build_oscore" = "xyes"; then if test "x$LIBCOAP_DTLS_LIB_EXTENSION_NAME" = "x-notls"; then AC_MSG_WARN([==> --enable-oscore requires crypto support from TLS library or OS]) fi +else + if test "x$with_mbedtls_oscore" = "xyes"; then + AC_MSG_ERROR([==> --with-mbedtls-oscore and --dsable-oscore incompatible]) + fi fi if test "x$build_oscore" = "xyes"; then @@ -1514,6 +1479,20 @@ if test "x$with_mbedtls" = "xyes" -o "x$with_mbedtls_auto" = "xyes"; then AC_MSG_RESULT([ MbedTLS_CFLAGS : "$MbedTLS_CFLAGS"]) AC_MSG_RESULT([ MbedTLS_LIBS : "$MbedTLS_LIBS"]) fi +if test "x$with_mbedtls_oscore" = "xyes" ; then + if test "x$build_dtls" = "xyes"; then + AC_MSG_RESULT([ build DTLS support : "yes"]) + else + AC_MSG_RESULT([ build OSCORE support : "yes"]) + fi + if test "x$mbedtls_has_pkgconfig" = "xyes"; then + AC_MSG_RESULT([ --> Mbed TLS around : "yes" (found Mbed TLS $mbedtls_version using pkg-config)]) + else + AC_MSG_RESULT([ --> Mbed TLS around : "yes" (found Mbed TLS $mbedtls_version without pkg-config)]) + fi + AC_MSG_RESULT([ MbedTLS_CFLAGS : "$MbedTLS_CFLAGS"]) + AC_MSG_RESULT([ MbedTLS_LIBS : "$MbedTLS_LIBS"]) +fi if test "x$with_tinydtls" = "xyes"; then AC_MSG_RESULT([ build DTLS support : "yes"]) if test "x$have_submodule_tinydtls" = "xyes"; then @@ -1551,6 +1530,11 @@ if test "x$build_oscore" != "xno"; then else AC_MSG_RESULT([ enable OSCORE support : "no"]) fi +if test "x$build_ws" != "xno"; then + AC_MSG_RESULT([ enable WebSockets support : "yes"]) +else + AC_MSG_RESULT([ enable WebSockets support : "no"]) +fi if test "x$build_q_block" != "xno"; then AC_MSG_RESULT([ enable Q-Block support : "yes"]) else diff --git a/examples/zephyr/client-src/libcoap-mbedtls.conf b/examples/zephyr/client-src/libcoap-mbedtls.conf index 5c77ba21..39ef0f17 100644 --- a/examples/zephyr/client-src/libcoap-mbedtls.conf +++ b/examples/zephyr/client-src/libcoap-mbedtls.conf @@ -9,6 +9,12 @@ CONFIG_MBEDTLS_ECP_DP_SECP256R1_ENABLED=y CONFIG_MBEDTLS_ENTROPY_C=y CONFIG_MBEDTLS_CIPHER_GCM_ENABLED=y +# Set this if OSCORE Support wanted +# CONFIG_LIBCOAP_OSCORE_SUPPORT=y + +# Set this if OSCORE (and not (D)TLS support) is required +# CONFIG_MBEDTLS_OSCORE=y + CONFIG_NET_BUF_RX_COUNT=80 CONFIG_NET_BUF_TX_COUNT=80 diff --git a/m4/ac_check_cryptolibs.m4 b/m4/ac_check_cryptolibs.m4 index 2d4c7538..1e27f248 100644 --- a/m4/ac_check_cryptolibs.m4 +++ b/m4/ac_check_cryptolibs.m4 @@ -1,14 +1,14 @@ # # SYNOPSIS # -# AX_CHECK_{GNUTLS|OPENSSL}_VERSION +# AX_CHECK_{GNUTLS|OPENSSL|MBEDTLS|WOLFSSL|TINYDTLS}_VERSION # # DESCRIPTION # # This m4 file contains helper functions for checking the version of the -# respective cryptographic library version of GnuTLS, Mbed TLS or OpenSSL on -# the host. The variables '$gnutls_version_required', -# '$mbedtls_version_required' and '$openssl_version_required' hold the +# respective cryptographic library version of GnuTLS, OpenSSL, Mbed TLS, +# WolfSSL and TinyDTLS on the host. The variables '$gnutls_version_required', +# '$mbedtls_version_required' and '$openssl_version_required' etc. hold the # minimum required version and are set up externaly in configure.ac. # # Example: diff --git a/m4/ax_pkg_check_mbedtls.m4 b/m4/ax_pkg_check_mbedtls.m4 new file mode 100644 index 00000000..e8802f03 --- /dev/null +++ b/m4/ax_pkg_check_mbedtls.m4 @@ -0,0 +1,119 @@ +# +# SYNOPSIS +# +# AX_PKG_CHECK_MBEDTLS(VARIABLE-PREFIX, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]) +# +# DESCRIPTION +# +# This m4 file contains helper functions for checking the version and libraries +# for MbedTLS which does not always have a pkg-config file. +# +# LICENSE +# +# Copyright (c) 2026 Jon Shallow +# +# Copying and distribution of this file, with or without modification, are +# permitted in any medium without royalty provided the copyright notice +# and this notice are preserved. This file is offered as-is, without any +# warranty. + +AC_DEFUN([AX_PKG_CHECK_MBEDTLS], [ +AC_REQUIRE([PKG_PROG_PKG_CONFIG]) +AC_REQUIRE([AC_PROG_CC]) +AC_ARG_VAR([MbedTLS_CFLAGS], [C compiler flags for MbedTLS, overriding pkg-config]) +AC_ARG_VAR([MbedTLS_LIBS], [linker flags for MbedTLS, overriding pkg-config]) +AC_LANG_PUSH([C]) + +if test "x${MbedTLS_CFLAGS+set}" = "xset"; then + mbedtls_cflags_overridden="yes" +else + mbedtls_cflags_overridden="no" +fi + +if test "x${MbedTLS_LIBS+set}" = "xset"; then + mbedtls_libs_overridden="yes" +else + mbedtls_libs_overridden="no" +fi + +# If MbedTLS_CFLAGS and MbedTLS_LIBS are overridden, pkg-config will always assume the library was found, even +# though MbedTLS doesn't necessarily have a pkg-config file. Therefore, we might as well use the "old" way to +# determine whether we can link MbedTLS (and which version we have). +if test "x$mbedtls_libs_overridden" = "xyes" -a "x$mbedtls_cflags_overridden" = "xyes"; then + have_$2="no" + have_mbedtls="no" + mbedtls_has_pkgconfig="no" +else + # Attempt to find MbedTLS using pkg-config. + + # When statically linking against libcoap, all transitive dependencies need to be specified as linker flags + # as well. Use pkg-config --static for that. + if test "x$enable_static" = "xyes"; then + KEEP_PKG_CONFIG=$PKG_CONFIG + PKG_CONFIG="$PKG_CONFIG --static" + PKG_CHECK_MODULES([MbedTLS], + [mbedtls], + [_have_mbedtls="yes"; $3; mbedtls_has_pkgconfig="yes"], + [_have_mbedtls="no" ; $4; mbedtls_has_pkgconfig="no"]) + PKG_CONFIG=$KEEP_PKG_CONFIG + else + PKG_CHECK_MODULES([MbedTLS], + [mbedtls], + [_have_mbedtls="yes"; $3; mbedtls_has_pkgconfig="yes"], + [_have_mbedtls="no" ; $4; mbedtls_has_pkgconfig="no"]) + fi +fi + +if test "x$_have_mbedtls" = "xno"; then + # Attempt to find mbedtls without pkg-config. + + # default linker flags (default CFLAGS are empty, so they don't need to be set). + if test "x$mbedtls_libs_overridden" == "xno"; then + MbedTLS_LIBS="-lmbedtls -lmbedcrypto -lmbedx509" + fi + + # check whether we can find MbedTLS + local_MbedTLS_save_CFLAGS=$CFLAGS + CFLAGS="$MbedTLS_CFLAGS $CFLAGS" + local_MbedTLS_save_LIBS=$LIBS + LIBS="$MbedTLS_LIBS $LIBS" + AC_CHECK_LIB(mbedtls, mbedtls_version_get_string, + [_have_mbedtls="yes"; $3], + [_have_mbedtls="no" ; $4]) + LIBS=$local_MbedTLS_save_LIBS + CFLAGS=$local_MbedTLS_save_CFLAGS +fi + +# here, we know whether we can find MbedTLS, but need to determine the version. +if test "x$_have_mbedtls" = "xyes"; then + if test "x$mbedtls_has_pkgconfig" = "xyes"; then + # If pkg-config found mbedtls, use it to determine the version. + mbedtls_version=`$PKG_CONFIG --modversion mbedtls` ; + elif test "x$cross_compiling" = "xyes" ; then + # Have no option but to do this + mbedtls_version=$mbedtls_version_required + else + # Get actual library version by compiling a test program. + local_MbedTLS_save_CFLAGS=$CFLAGS + CFLAGS="$MbedTLS_CFLAGS $CFLAGS" + local_MbedTLS_save_LIBS=$LIBS + LIBS="$MbedTLS_LIBS $LIBS" + AC_LINK_IFELSE([ + AC_LANG_SOURCE( + [[#include + #include + int main () { + char str[20]; + mbedtls_version_get_string(str); + fprintf(stdout,"%s\n",str); + return 0; + }]])], + [mbedtls_version=$(./conftest$EXEEXT)], + [AC_MSG_WARN(Failed to determine Mbed TLS version) + $4]) + LIBS=$local_MbedTLS_save_LIBS + CFLAGS=$local_MbedTLS_save_CFLAGS + fi +fi +AC_LANG_POP +]) dnl AX_PKG_CHECK_MBEDTLS diff --git a/src/coap_debug.c b/src/coap_debug.c index b7671e15..7744c428 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -1314,7 +1314,9 @@ coap_string_tls_support(char *buffer, size_t bufsize) { const int have_ws = coap_ws_is_supported(); if (have_dtls == 0 && have_tls == 0) { - snprintf(buffer, bufsize, "(No DTLS or TLS support)"); + snprintf(buffer, bufsize, "(No DTLS or TLS support)\n(%sOSCORE)\n(%sWebSockets)", + have_oscore ? "Have " : "No ", + have_ws ? "Have " : "No "); return buffer; } snprintf(buffer, bufsize, diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index 0273aa90..dc215002 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -34,7 +34,7 @@ #include "coap3/coap_libcoap_build.h" -#if COAP_WITH_LIBMBEDTLS +#if COAP_WITH_LIBMBEDTLS || COAP_WITH_LIBMBEDTLS_OSCORE /* * This code can be conditionally compiled to remove some components if @@ -72,7 +72,7 @@ #define COAP_USE_PSA_CRYPTO 1 #else #define COAP_USE_PSA_CRYPTO 0 -#endif +#endif /* MBEDTLS_VERSION_NUMBER < 0x04000000 */ #if COAP_USE_PSA_CRYPTO #include @@ -83,6 +83,7 @@ #include #include #endif /* COAP_USE_PSA_CRYPTO */ + #include #include #include @@ -119,12 +120,46 @@ typedef mbedtls_md_type_t coap_crypto_md_type_t; #define COAP_CRYPTO_MD_NONE MBEDTLS_MD_NONE #endif /* !COAP_USE_PSA_CRYPTO */ +coap_tls_version_t * +coap_get_tls_library_version(void) { + static coap_tls_version_t version; + version.version = mbedtls_version_get_number(); + version.built_version = MBEDTLS_VERSION_NUMBER; + version.type = COAP_TLS_LIBRARY_MBEDTLS; + return &version; +} + +#if COAP_WITH_LIBMBEDTLS_OSCORE +void +coap_dtls_startup(void) { +#if COAP_USE_PSA_CRYPTO || defined(MBEDTLS_PSA_CRYPTO_C) + psa_crypto_init(); +#endif /* COAP_USE_PSA_CRYPTO || MBEDTLS_PSA_CRYPTO_C */ +} + +void +coap_dtls_shutdown(void) { + coap_dtls_set_log_level(COAP_LOG_EMERG); +#if COAP_USE_PSA_CRYPTO || defined(MBEDTLS_PSA_CRYPTO_C) + mbedtls_psa_crypto_free(); +#endif /* COAP_USE_PSA_CRYPTO || MBEDTLS_PSA_CRYPTO_C */ +} + +#endif /* COAP_WITH_LIBMBEDTLS_OSCORE */ + +void +coap_dtls_thread_shutdown(void) { +} + +#ifdef _WIN32 +#include +#include +#endif /* _WIN32 */ + /* * Default mbedTLS helpers in this file to the C library heap. Zephyr can set * mbedtls_malloc / mbedtls_realloc / mbedtls_free via zephyr/CMakeLists.txt. */ -#include -#include #ifndef mbedtls_malloc #define mbedtls_malloc(a) malloc(a) @@ -136,6 +171,7 @@ typedef mbedtls_md_type_t coap_crypto_md_type_t; #define mbedtls_free(a) free(a) #endif +#if ! COAP_WITH_LIBMBEDTLS_OSCORE static char * coap_mbedtls_strdup_alloc(const char *s) { size_t len; @@ -168,6 +204,7 @@ coap_mbedtls_strndup_alloc(const char *s, size_t n) { } return copy; } +#endif /* ! COAP_WITH_LIBMBEDTLS_OSCORE */ #ifndef mbedtls_strdup #define mbedtls_strdup(a) coap_mbedtls_strdup_alloc(a) @@ -194,6 +231,121 @@ coap_mbedtls_strndup_alloc(const char *s, size_t n) { #define strcasecmp _stricmp #endif +/* + * Crypto Abstraction Functions + * These provide a unified API for both legacy mbedTLS and PSA Crypto. + */ + +#if COAP_SERVER_SUPPORT +/* SHA-256 Digest Functions */ + +static int +coap_crypto_sha256_init(coap_crypto_sha256_ctx_t *ctx) { +#if COAP_USE_PSA_CRYPTO + *ctx = psa_hash_operation_init(); + return (psa_hash_setup(ctx, PSA_ALG_SHA_256) == PSA_SUCCESS) ? 0 : -1; +#else + mbedtls_sha256_init(ctx); +#if (MBEDTLS_VERSION_NUMBER < 0x03000000) + return mbedtls_sha256_starts_ret(ctx, 0); +#else + return mbedtls_sha256_starts(ctx, 0); +#endif +#endif +} + +static int +coap_crypto_sha256_update(coap_crypto_sha256_ctx_t *ctx, + const uint8_t *data, size_t len) { +#if COAP_USE_PSA_CRYPTO + return (psa_hash_update(ctx, data, len) == PSA_SUCCESS) ? 0 : -1; +#else +#if (MBEDTLS_VERSION_NUMBER < 0x03000000) + return mbedtls_sha256_update_ret(ctx, data, len); +#else + return mbedtls_sha256_update(ctx, data, len); +#endif +#endif +} + +static int +coap_crypto_sha256_finish(coap_crypto_sha256_ctx_t *ctx, uint8_t *output) { +#if COAP_USE_PSA_CRYPTO + size_t hash_len; + return (psa_hash_finish(ctx, output, 32, &hash_len) == PSA_SUCCESS) ? 0 : -1; +#else +#if (MBEDTLS_VERSION_NUMBER < 0x03000000) + return mbedtls_sha256_finish_ret(ctx, output); +#else + return mbedtls_sha256_finish(ctx, output); +#endif +#endif +} + +static void +coap_crypto_sha256_free(coap_crypto_sha256_ctx_t *ctx) { +#if COAP_USE_PSA_CRYPTO + psa_hash_abort(ctx); +#else + mbedtls_sha256_free(ctx); +#endif +} +#endif /* COAP_SERVER_SUPPORT */ + +/* General Hash Functions */ +#if COAP_WS_SUPPORT +static size_t +coap_crypto_hash_size(coap_crypto_md_type_t md_type) { +#if COAP_USE_PSA_CRYPTO + switch ((int)md_type) { + case PSA_ALG_SHA_1: + return 20; + case PSA_ALG_SHA_256: + return 32; + case PSA_ALG_SHA_384: + return 48; + case PSA_ALG_SHA_512: + return 64; + default: + return 0; + } +#else + const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type); + return md_info ? mbedtls_md_get_size(md_info) : 0; +#endif +} + +static int +coap_crypto_hash_compute(coap_crypto_md_type_t md_type, + const uint8_t *input, size_t ilen, + uint8_t *output, size_t output_size, + size_t *output_len) { +#if COAP_USE_PSA_CRYPTO + size_t actual_len; + psa_status_t status = psa_hash_compute(md_type, input, ilen, + output, output_size, &actual_len); + if (output_len) + *output_len = actual_len; + return (status == PSA_SUCCESS) ? 0 : -1; +#else + const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type); + if (!md_info) + return -1; + size_t hash_len = mbedtls_md_get_size(md_info); + if (hash_len > output_size) + return -1; + if (output_len) + *output_len = hash_len; + return mbedtls_md(md_info, input, ilen, output); +#endif +} +#endif /* COAP_WS_SUPPORT */ + + +#endif /* COAP_WITH_LIBMBEDTLS || COAP_WITH_LIBMBEDTLS_OSCORE */ + +#if COAP_WITH_LIBMBEDTLS + #define IS_PSK (1 << 0) #define IS_PKI (1 << 1) #define IS_CLIENT (1 << 6) @@ -315,351 +467,6 @@ zephyr_timing_get_delay(void *data) { #endif /* __ZEPHYR__ */ -/* - * Crypto Abstraction Functions - * These provide a unified API for both legacy mbedTLS and PSA Crypto. - */ - -#if COAP_SERVER_SUPPORT -/* SHA-256 Digest Functions */ - -static int -coap_crypto_sha256_init(coap_crypto_sha256_ctx_t *ctx) { -#if COAP_USE_PSA_CRYPTO - *ctx = psa_hash_operation_init(); - return (psa_hash_setup(ctx, PSA_ALG_SHA_256) == PSA_SUCCESS) ? 0 : -1; -#else - mbedtls_sha256_init(ctx); -#if (MBEDTLS_VERSION_NUMBER < 0x03000000) - return mbedtls_sha256_starts_ret(ctx, 0); -#else - return mbedtls_sha256_starts(ctx, 0); -#endif -#endif -} - -static int -coap_crypto_sha256_update(coap_crypto_sha256_ctx_t *ctx, - const uint8_t *data, size_t len) { -#if COAP_USE_PSA_CRYPTO - return (psa_hash_update(ctx, data, len) == PSA_SUCCESS) ? 0 : -1; -#else -#if (MBEDTLS_VERSION_NUMBER < 0x03000000) - return mbedtls_sha256_update_ret(ctx, data, len); -#else - return mbedtls_sha256_update(ctx, data, len); -#endif -#endif -} - -static int -coap_crypto_sha256_finish(coap_crypto_sha256_ctx_t *ctx, uint8_t *output) { -#if COAP_USE_PSA_CRYPTO - size_t hash_len; - return (psa_hash_finish(ctx, output, 32, &hash_len) == PSA_SUCCESS) ? 0 : -1; -#else -#if (MBEDTLS_VERSION_NUMBER < 0x03000000) - return mbedtls_sha256_finish_ret(ctx, output); -#else - return mbedtls_sha256_finish(ctx, output); -#endif -#endif -} - -static void -coap_crypto_sha256_free(coap_crypto_sha256_ctx_t *ctx) { -#if COAP_USE_PSA_CRYPTO - psa_hash_abort(ctx); -#else - mbedtls_sha256_free(ctx); -#endif -} -#endif /* COAP_SERVER_SUPPORT */ - -/* General Hash Functions */ -#if COAP_WS_SUPPORT -static size_t -coap_crypto_hash_size(coap_crypto_md_type_t md_type) { -#if COAP_USE_PSA_CRYPTO - switch ((int)md_type) { - case PSA_ALG_SHA_1: - return 20; - case PSA_ALG_SHA_256: - return 32; - case PSA_ALG_SHA_384: - return 48; - case PSA_ALG_SHA_512: - return 64; - default: - return 0; - } -#else - const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type); - return md_info ? mbedtls_md_get_size(md_info) : 0; -#endif -} - -static int -coap_crypto_hash_compute(coap_crypto_md_type_t md_type, - const uint8_t *input, size_t ilen, - uint8_t *output, size_t output_size, - size_t *output_len) { -#if COAP_USE_PSA_CRYPTO - size_t actual_len; - psa_status_t status = psa_hash_compute(md_type, input, ilen, - output, output_size, &actual_len); - if (output_len) - *output_len = actual_len; - return (status == PSA_SUCCESS) ? 0 : -1; -#else - const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type); - if (!md_info) - return -1; - size_t hash_len = mbedtls_md_get_size(md_info); - if (hash_len > output_size) - return -1; - if (output_len) - *output_len = hash_len; - return mbedtls_md(md_info, input, ilen, output); -#endif -} -#endif /* COAP_WS_SUPPORT */ - -/* HMAC Functions */ - -#if COAP_OSCORE_SUPPORT -static int -coap_crypto_hmac_compute(coap_crypto_md_type_t md_type, - const uint8_t *key, size_t key_len, - const uint8_t *input, size_t ilen, - uint8_t *output, size_t output_size, - size_t *output_len) { -#if COAP_USE_PSA_CRYPTO - psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; - psa_key_id_t key_id; - psa_algorithm_t psa_alg = PSA_ALG_HMAC(md_type); - size_t mac_len; - psa_status_t status; - - psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); - psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); - psa_set_key_algorithm(&attributes, psa_alg); - - if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) { - return -1; - } - - status = psa_mac_compute(key_id, psa_alg, input, ilen, - output, output_size, &mac_len); - psa_destroy_key(key_id); - - if (output_len) - *output_len = mac_len; - return (status == PSA_SUCCESS) ? 0 : -1; -#else - const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type); - if (!md_info) - return -1; - size_t mac_size = mbedtls_md_get_size(md_info); - if (mac_size > output_size) - return -1; - if (output_len) - *output_len = mac_size; - return mbedtls_md_hmac(md_info, key, key_len, input, ilen, output); -#endif -} - -/* AEAD (AES-CCM) Functions */ - -static int -coap_crypto_aead_encrypt_ccm(const uint8_t *key, size_t key_len, - const uint8_t *nonce, size_t nonce_len, - const uint8_t *aad, size_t aad_len, - const uint8_t *plaintext, size_t plaintext_len, - uint8_t *ciphertext, size_t ciphertext_size, - size_t *ciphertext_len, size_t tag_len) { -#if COAP_USE_PSA_CRYPTO - psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; - psa_key_id_t key_id; - psa_algorithm_t psa_alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, tag_len); - size_t output_len; - psa_status_t status; - - psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); - psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); - psa_set_key_algorithm(&attributes, psa_alg); - psa_set_key_bits(&attributes, key_len * 8); - - if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) { - return -1; - } - - status = psa_aead_encrypt(key_id, psa_alg, - nonce, nonce_len, - aad, aad_len, - plaintext, plaintext_len, - ciphertext, ciphertext_size, &output_len); - psa_destroy_key(key_id); - - if (ciphertext_len) - *ciphertext_len = output_len; - return (status == PSA_SUCCESS) ? 0 : -1; -#else - mbedtls_cipher_context_t ctx; - const mbedtls_cipher_info_t *cipher_info; - mbedtls_cipher_type_t cipher_type; - int ret = -1; - size_t result_len = ciphertext_size; - - if (key_len == 16) { - cipher_type = MBEDTLS_CIPHER_AES_128_CCM; - } else if (key_len == 32) { - cipher_type = MBEDTLS_CIPHER_AES_256_CCM; - } else { - return -1; - } - - cipher_info = mbedtls_cipher_info_from_type(cipher_type); - if (!cipher_info) - return -1; - - mbedtls_cipher_init(&ctx); - if (mbedtls_cipher_setup(&ctx, cipher_info) != 0) - goto cleanup; - if (mbedtls_cipher_setkey(&ctx, key, key_len * 8, MBEDTLS_ENCRYPT) != 0) - goto cleanup; - -#if (MBEDTLS_VERSION_NUMBER < 0x02150000) - { - unsigned char tag[16]; - if (mbedtls_cipher_auth_encrypt(&ctx, - nonce, nonce_len, - aad, aad_len, - plaintext, plaintext_len, - ciphertext, &result_len, - tag, tag_len) != 0) { - goto cleanup; - } - if ((result_len + tag_len) > ciphertext_size) - goto cleanup; - memcpy(ciphertext + result_len, tag, tag_len); - result_len += tag_len; - } -#else - if (mbedtls_cipher_auth_encrypt_ext(&ctx, - nonce, nonce_len, - aad, aad_len, - plaintext, plaintext_len, - ciphertext, ciphertext_size, - &result_len, tag_len) != 0) { - goto cleanup; - } -#endif - - if (ciphertext_len) - *ciphertext_len = result_len; - ret = 0; - -cleanup: - mbedtls_cipher_free(&ctx); - return ret; -#endif -} - -static int -coap_crypto_aead_decrypt_ccm(const uint8_t *key, size_t key_len, - const uint8_t *nonce, size_t nonce_len, - const uint8_t *aad, size_t aad_len, - const uint8_t *ciphertext, size_t ciphertext_len, - uint8_t *plaintext, size_t plaintext_size, - size_t *plaintext_len, size_t tag_len) { -#if COAP_USE_PSA_CRYPTO - psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; - psa_key_id_t key_id; - psa_algorithm_t psa_alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, tag_len); - size_t output_len; - psa_status_t status; - - psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); - psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); - psa_set_key_algorithm(&attributes, psa_alg); - psa_set_key_bits(&attributes, key_len * 8); - - if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) { - return -1; - } - - status = psa_aead_decrypt(key_id, psa_alg, - nonce, nonce_len, - aad, aad_len, - ciphertext, ciphertext_len, - plaintext, plaintext_size, &output_len); - psa_destroy_key(key_id); - - if (plaintext_len) - *plaintext_len = output_len; - return (status == PSA_SUCCESS) ? 0 : -1; -#else - mbedtls_cipher_context_t ctx; - const mbedtls_cipher_info_t *cipher_info; - mbedtls_cipher_type_t cipher_type; - int ret = -1; - size_t result_len = plaintext_size; - - if (key_len == 16) { - cipher_type = MBEDTLS_CIPHER_AES_128_CCM; - } else if (key_len == 32) { - cipher_type = MBEDTLS_CIPHER_AES_256_CCM; - } else { - return -1; - } - - cipher_info = mbedtls_cipher_info_from_type(cipher_type); - if (!cipher_info) - return -1; - - mbedtls_cipher_init(&ctx); - if (mbedtls_cipher_setup(&ctx, cipher_info) != 0) - goto cleanup; - if (mbedtls_cipher_setkey(&ctx, key, key_len * 8, MBEDTLS_DECRYPT) != 0) - goto cleanup; - -#if (MBEDTLS_VERSION_NUMBER < 0x02150000) - { - const unsigned char *tag = ciphertext + ciphertext_len - tag_len; - if (mbedtls_cipher_auth_decrypt(&ctx, - nonce, nonce_len, - aad, aad_len, - ciphertext, ciphertext_len - tag_len, - plaintext, &result_len, - tag, tag_len) != 0) { - goto cleanup; - } - } -#else - if (mbedtls_cipher_auth_decrypt_ext(&ctx, - nonce, nonce_len, - aad, aad_len, - ciphertext, ciphertext_len, - plaintext, plaintext_size, - &result_len, tag_len) != 0) { - goto cleanup; - } -#endif - - if (plaintext_len) - *plaintext_len = result_len; - ret = 0; - -cleanup: - mbedtls_cipher_free(&ctx); - return ret; -#endif -} -#endif /* COAP_OSCORE_SUPPORT */ - -/* End of Crypto Abstraction Functions */ - #if !COAP_USE_PSA_CRYPTO #ifndef MBEDTLS_2_X_COMPAT /* @@ -3411,9 +3218,6 @@ coap_dtls_shutdown(void) { #endif /* COAP_USE_PSA_CRYPTO || MBEDTLS_PSA_CRYPTO_C */ } -void -coap_dtls_thread_shutdown(void) { -} void * coap_dtls_get_tls(const coap_session_t *c_session, @@ -3476,14 +3280,9 @@ coap_dtls_get_log_level(void) { return keep_log_level; } -coap_tls_version_t * -coap_get_tls_library_version(void) { - static coap_tls_version_t version; - version.version = mbedtls_version_get_number(); - version.built_version = MBEDTLS_VERSION_NUMBER; - version.type = COAP_TLS_LIBRARY_MBEDTLS; - return &version; -} +#endif /* COAP_WITH_LIBMBEDTLS */ + +#if COAP_WITH_LIBMBEDTLS || COAP_WITH_LIBMBEDTLS_OSCORE #if COAP_SERVER_SUPPORT coap_digest_ctx_t * @@ -3565,7 +3364,240 @@ coap_crypto_hash(cose_alg_t alg, } #endif /* COAP_WS_SUPPORT */ +/* HMAC Functions */ + #if COAP_OSCORE_SUPPORT +static int +coap_crypto_hmac_compute(coap_crypto_md_type_t md_type, + const uint8_t *key, size_t key_len, + const uint8_t *input, size_t ilen, + uint8_t *output, size_t output_size, + size_t *output_len) { +#if COAP_USE_PSA_CRYPTO + psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; + psa_key_id_t key_id; + psa_algorithm_t psa_alg = PSA_ALG_HMAC(md_type); + size_t mac_len; + psa_status_t status; + + psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); + psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); + psa_set_key_algorithm(&attributes, psa_alg); + + if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) { + return -1; + } + + status = psa_mac_compute(key_id, psa_alg, input, ilen, + output, output_size, &mac_len); + psa_destroy_key(key_id); + + if (output_len) + *output_len = mac_len; + return (status == PSA_SUCCESS) ? 0 : -1; +#else + const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type); + if (!md_info) + return -1; + size_t mac_size = mbedtls_md_get_size(md_info); + if (mac_size > output_size) + return -1; + if (output_len) + *output_len = mac_size; + return mbedtls_md_hmac(md_info, key, key_len, input, ilen, output); +#endif +} + +/* AEAD (AES-CCM) Functions */ + +static int +coap_crypto_aead_encrypt_ccm(const uint8_t *key, size_t key_len, + const uint8_t *nonce, size_t nonce_len, + const uint8_t *aad, size_t aad_len, + const uint8_t *plaintext, size_t plaintext_len, + uint8_t *ciphertext, size_t ciphertext_size, + size_t *ciphertext_len, size_t tag_len) { +#if COAP_USE_PSA_CRYPTO + psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; + psa_key_id_t key_id; + psa_algorithm_t psa_alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, tag_len); + size_t output_len; + psa_status_t status; + + psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); + psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); + psa_set_key_algorithm(&attributes, psa_alg); + psa_set_key_bits(&attributes, key_len * 8); + + if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) { + return -1; + } + + status = psa_aead_encrypt(key_id, psa_alg, + nonce, nonce_len, + aad, aad_len, + plaintext, plaintext_len, + ciphertext, ciphertext_size, &output_len); + psa_destroy_key(key_id); + + if (ciphertext_len) + *ciphertext_len = output_len; + return (status == PSA_SUCCESS) ? 0 : -1; +#else + mbedtls_cipher_context_t ctx; + const mbedtls_cipher_info_t *cipher_info; + mbedtls_cipher_type_t cipher_type; + int ret = -1; + size_t result_len = ciphertext_size; + + if (key_len == 16) { + cipher_type = MBEDTLS_CIPHER_AES_128_CCM; + } else if (key_len == 32) { + cipher_type = MBEDTLS_CIPHER_AES_256_CCM; + } else { + return -1; + } + + cipher_info = mbedtls_cipher_info_from_type(cipher_type); + if (!cipher_info) + return -1; + + mbedtls_cipher_init(&ctx); + if (mbedtls_cipher_setup(&ctx, cipher_info) != 0) + goto cleanup; + if (mbedtls_cipher_setkey(&ctx, key, key_len * 8, MBEDTLS_ENCRYPT) != 0) + goto cleanup; + +#if (MBEDTLS_VERSION_NUMBER < 0x02150000) + { + unsigned char tag[16]; + if (mbedtls_cipher_auth_encrypt(&ctx, + nonce, nonce_len, + aad, aad_len, + plaintext, plaintext_len, + ciphertext, &result_len, + tag, tag_len) != 0) { + goto cleanup; + } + if ((result_len + tag_len) > ciphertext_size) + goto cleanup; + memcpy(ciphertext + result_len, tag, tag_len); + result_len += tag_len; + } +#else + if (mbedtls_cipher_auth_encrypt_ext(&ctx, + nonce, nonce_len, + aad, aad_len, + plaintext, plaintext_len, + ciphertext, ciphertext_size, + &result_len, tag_len) != 0) { + goto cleanup; + } +#endif + + if (ciphertext_len) + *ciphertext_len = result_len; + ret = 0; + +cleanup: + mbedtls_cipher_free(&ctx); + return ret; +#endif +} + +static int +coap_crypto_aead_decrypt_ccm(const uint8_t *key, size_t key_len, + const uint8_t *nonce, size_t nonce_len, + const uint8_t *aad, size_t aad_len, + const uint8_t *ciphertext, size_t ciphertext_len, + uint8_t *plaintext, size_t plaintext_size, + size_t *plaintext_len, size_t tag_len) { +#if COAP_USE_PSA_CRYPTO + psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; + psa_key_id_t key_id; + psa_algorithm_t psa_alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, tag_len); + size_t output_len; + psa_status_t status; + + psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); + psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); + psa_set_key_algorithm(&attributes, psa_alg); + psa_set_key_bits(&attributes, key_len * 8); + + if (psa_import_key(&attributes, key, key_len, &key_id) != PSA_SUCCESS) { + return -1; + } + + status = psa_aead_decrypt(key_id, psa_alg, + nonce, nonce_len, + aad, aad_len, + ciphertext, ciphertext_len, + plaintext, plaintext_size, &output_len); + psa_destroy_key(key_id); + + if (plaintext_len) + *plaintext_len = output_len; + return (status == PSA_SUCCESS) ? 0 : -1; +#else + mbedtls_cipher_context_t ctx; + const mbedtls_cipher_info_t *cipher_info; + mbedtls_cipher_type_t cipher_type; + int ret = -1; + size_t result_len = plaintext_size; + + if (key_len == 16) { + cipher_type = MBEDTLS_CIPHER_AES_128_CCM; + } else if (key_len == 32) { + cipher_type = MBEDTLS_CIPHER_AES_256_CCM; + } else { + return -1; + } + + cipher_info = mbedtls_cipher_info_from_type(cipher_type); + if (!cipher_info) + return -1; + + mbedtls_cipher_init(&ctx); + if (mbedtls_cipher_setup(&ctx, cipher_info) != 0) + goto cleanup; + if (mbedtls_cipher_setkey(&ctx, key, key_len * 8, MBEDTLS_DECRYPT) != 0) + goto cleanup; + +#if (MBEDTLS_VERSION_NUMBER < 0x02150000) + { + const unsigned char *tag = ciphertext + ciphertext_len - tag_len; + if (mbedtls_cipher_auth_decrypt(&ctx, + nonce, nonce_len, + aad, aad_len, + ciphertext, ciphertext_len - tag_len, + plaintext, &result_len, + tag, tag_len) != 0) { + goto cleanup; + } + } +#else + if (mbedtls_cipher_auth_decrypt_ext(&ctx, + nonce, nonce_len, + aad, aad_len, + ciphertext, ciphertext_len, + plaintext, plaintext_size, + &result_len, tag_len) != 0) { + goto cleanup; + } +#endif + + if (plaintext_len) + *plaintext_len = result_len; + ret = 0; + +cleanup: + mbedtls_cipher_free(&ctx); + return ret; +#endif +} + +/* End of Crypto Abstraction Functions */ + int coap_oscore_is_supported(void) { return 1; @@ -3754,7 +3786,7 @@ coap_crypto_hmac(cose_hmac_alg_t hmac_alg, #endif /* COAP_OSCORE_SUPPORT */ -#else /* ! COAP_WITH_LIBMBEDTLS */ +#else /* ! COAP_WITH_LIBMBEDTLS && ! COAP_WITH_LIBMBEDTLS_OSCORE*/ #ifdef __clang__ /* Make compilers happy that do not like empty modules. As this function is diff --git a/src/coap_notls.c b/src/coap_notls.c index 08f77925..43a367f1 100644 --- a/src/coap_notls.c +++ b/src/coap_notls.c @@ -96,6 +96,7 @@ coap_dtls_set_cid_tuple_change(coap_context_t *c_context, uint8_t every) { } #endif /* COAP_CLIENT_SUPPORT */ +#if ! COAP_WITH_LIBMBEDTLS_OSCORE coap_tls_version_t * coap_get_tls_library_version(void) { static coap_tls_version_t version; @@ -104,6 +105,155 @@ coap_get_tls_library_version(void) { return &version; } +void +coap_dtls_startup(void) { +} + +void +coap_dtls_shutdown(void) { + coap_dtls_set_log_level(COAP_LOG_EMERG); +} + +void +coap_dtls_thread_shutdown(void) { +} + +#if COAP_WS_SUPPORT +int +coap_crypto_hash(cose_alg_t alg, + const coap_bin_const_t *data, + coap_bin_const_t **hash) { + SHA1Context sha1_context; + coap_binary_t *dummy = NULL; + + (void)alg; + + SHA1Reset(&sha1_context); + if (SHA1Input(&sha1_context, data->s, data->length) != shaSuccess) + return 0; + dummy = coap_new_binary(SHA1HashSize); + if (!dummy) + return 0; + if (SHA1Result(&sha1_context, dummy->s) != shaSuccess) { + coap_delete_binary(dummy); + return 0; + } + *hash = (coap_bin_const_t *)(dummy); + return 1; +} +#endif /* COAP_WS_SUPPORT */ + +#if COAP_OSCORE_SUPPORT + +int +coap_oscore_is_supported(void) { + return 0; +} + +int +coap_crypto_check_cipher_alg(cose_alg_t alg) { + (void)alg; + return 0; +} + +int +coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg) { + (void)hkdf_alg; + return 0; +} + +int +coap_crypto_aead_encrypt(const coap_crypto_param_t *params, + coap_bin_const_t *data, + coap_bin_const_t *aad, + uint8_t *result, + size_t *max_result_len) { + (void)params; + (void)data; + (void)aad; + (void)result; + *max_result_len = 0; + return 0; +} + +int +coap_crypto_aead_decrypt(const coap_crypto_param_t *params, + coap_bin_const_t *data, + coap_bin_const_t *aad, + uint8_t *result, + size_t *max_result_len) { + (void)params; + (void)data; + (void)aad; + (void)result; + *max_result_len = 0; + return 0; +} + +int +coap_crypto_hmac(cose_hmac_alg_t hmac_alg, + coap_bin_const_t *key, + coap_bin_const_t *data, + coap_bin_const_t **hmac) { + (void)hmac_alg; + (void)key; + (void)data; + (void)hmac; + return 0; +} + +#endif /* COAP_OSCORE_SUPPORT */ + +#if COAP_SERVER_SUPPORT +typedef struct coap_local_hash_t { + size_t ofs; + coap_key_t key[8]; /* 32 bytes in total */ +} coap_local_hash_t; + +coap_digest_ctx_t * +coap_digest_setup(void) { + coap_key_t *digest_ctx = coap_malloc_type(COAP_DIGEST_CTX, sizeof(coap_local_hash_t)); + + if (digest_ctx) { + memset(digest_ctx, 0, sizeof(coap_local_hash_t)); + } + + return digest_ctx; +} + +void +coap_digest_free(coap_digest_ctx_t *digest_ctx) { + coap_free_type(COAP_DIGEST_CTX, digest_ctx); +} + +int +coap_digest_update(coap_digest_ctx_t *digest_ctx, + const uint8_t *data, + size_t data_len) { + coap_local_hash_t *local = (coap_local_hash_t *)digest_ctx; + + coap_hash(data, data_len, local->key[local->ofs]); + + local->ofs = (local->ofs + 1) % 7; + return 1; +} + +int +coap_digest_final(coap_digest_ctx_t *digest_ctx, + coap_digest_t *digest_buffer) { + coap_local_hash_t *local = (coap_local_hash_t *)digest_ctx; + + memcpy(digest_buffer, local->key, sizeof(coap_digest_t)); + + coap_digest_free(digest_ctx); + return 1; +} +#endif /* COAP_SERVER_SUPPORT */ + +#endif /* ! COAP_WITH_LIBMBEDTLS_OSCORE */ + +/* (D)TLS specific functions */ + int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, @@ -150,10 +300,6 @@ coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED) { static coap_log_t dtls_log_level = COAP_LOG_EMERG; -void -coap_dtls_startup(void) { -} - void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib) { @@ -162,15 +308,6 @@ coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, return NULL; } -void -coap_dtls_shutdown(void) { - coap_dtls_set_log_level(COAP_LOG_EMERG); -} - -void -coap_dtls_thread_shutdown(void) { -} - void coap_dtls_set_log_level(coap_log_t level) { dtls_log_level = level; @@ -308,138 +445,6 @@ coap_tls_read(coap_session_t *session COAP_UNUSED, return -1; } -#if COAP_SERVER_SUPPORT -typedef struct coap_local_hash_t { - size_t ofs; - coap_key_t key[8]; /* 32 bytes in total */ -} coap_local_hash_t; - -coap_digest_ctx_t * -coap_digest_setup(void) { - coap_key_t *digest_ctx = coap_malloc_type(COAP_DIGEST_CTX, sizeof(coap_local_hash_t)); - - if (digest_ctx) { - memset(digest_ctx, 0, sizeof(coap_local_hash_t)); - } - - return digest_ctx; -} - -void -coap_digest_free(coap_digest_ctx_t *digest_ctx) { - coap_free_type(COAP_DIGEST_CTX, digest_ctx); -} - -int -coap_digest_update(coap_digest_ctx_t *digest_ctx, - const uint8_t *data, - size_t data_len) { - coap_local_hash_t *local = (coap_local_hash_t *)digest_ctx; - - coap_hash(data, data_len, local->key[local->ofs]); - - local->ofs = (local->ofs + 1) % 7; - return 1; -} - -int -coap_digest_final(coap_digest_ctx_t *digest_ctx, - coap_digest_t *digest_buffer) { - coap_local_hash_t *local = (coap_local_hash_t *)digest_ctx; - - memcpy(digest_buffer, local->key, sizeof(coap_digest_t)); - - coap_digest_free(digest_ctx); - return 1; -} -#endif /* COAP_SERVER_SUPPORT */ - -#if COAP_WS_SUPPORT -int -coap_crypto_hash(cose_alg_t alg, - const coap_bin_const_t *data, - coap_bin_const_t **hash) { - SHA1Context sha1_context; - coap_binary_t *dummy = NULL; - - (void)alg; - - SHA1Reset(&sha1_context); - if (SHA1Input(&sha1_context, data->s, data->length) != shaSuccess) - return 0; - dummy = coap_new_binary(SHA1HashSize); - if (!dummy) - return 0; - if (SHA1Result(&sha1_context, dummy->s) != shaSuccess) { - coap_delete_binary(dummy); - return 0; - } - *hash = (coap_bin_const_t *)(dummy); - return 1; -} -#endif /* COAP_WS_SUPPORT */ - -#if COAP_OSCORE_SUPPORT - -int -coap_oscore_is_supported(void) { - return 0; -} - -int -coap_crypto_check_cipher_alg(cose_alg_t alg) { - (void)alg; - return 0; -} - -int -coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg) { - (void)hkdf_alg; - return 0; -} - -int -coap_crypto_aead_encrypt(const coap_crypto_param_t *params, - coap_bin_const_t *data, - coap_bin_const_t *aad, - uint8_t *result, - size_t *max_result_len) { - (void)params; - (void)data; - (void)aad; - (void)result; - *max_result_len = 0; - return 0; -} - -int -coap_crypto_aead_decrypt(const coap_crypto_param_t *params, - coap_bin_const_t *data, - coap_bin_const_t *aad, - uint8_t *result, - size_t *max_result_len) { - (void)params; - (void)data; - (void)aad; - (void)result; - *max_result_len = 0; - return 0; -} - -int -coap_crypto_hmac(cose_hmac_alg_t hmac_alg, - coap_bin_const_t *key, - coap_bin_const_t *data, - coap_bin_const_t **hmac) { - (void)hmac_alg; - (void)key; - (void)data; - (void)hmac; - return 0; -} - -#endif /* COAP_OSCORE_SUPPORT */ - #else /* !COAP_WITH_LIBTINYDTLS && !COAP_WITH_LIBOPENSSL && !COAP_WITH_LIBWOLFSSL && !COAP_WITH_LIBGNUTLS */ #ifdef __clang__ diff --git a/src/coap_sha1.c b/src/coap_sha1.c index 5eb43049..0fdb184f 100644 --- a/src/coap_sha1.c +++ b/src/coap_sha1.c @@ -70,7 +70,7 @@ #include "coap3/coap_libcoap_build.h" -#if COAP_WS_SUPPORT && ! COAP_WITH_LIBOPENSSL && ! COAP_WITH_LIBGNUTLS && ! COAP_WITH_LIBMBEDTLS && ! COAP_WITH_LIBWOLFSSL +#if COAP_WS_SUPPORT && ! COAP_WITH_LIBOPENSSL && ! COAP_WITH_LIBGNUTLS && ! COAP_WITH_LIBMBEDTLS && ! COAP_WITH_LIBMBEDTLS_OSCORE && ! COAP_WITH_LIBWOLFSSL /* * Define the SHA1 circular left shift macro */ @@ -396,4 +396,4 @@ SHA1PadMessage(SHA1Context *context) { SHA1ProcessMessageBlock(context); } -#endif /* COAP_WS_SUPPORT && ! COAP_WITH_LIBOPENSSL && ! COAP_WITH_LIBGNUTLS && ! COAP_WITH_LIBMBEDTLS */ +#endif /* COAP_WS_SUPPORT && ! COAP_WITH_LIBOPENSSL && ! COAP_WITH_LIBGNUTLS && ! COAP_WITH_LIBMBEDTLS && ! COAP_WITH_LIBMBEDTLS_OSCORE */ diff --git a/tests/test_tls.c b/tests/test_tls.c index 637d49c4..2a9df213 100644 --- a/tests/test_tls.c +++ b/tests/test_tls.c @@ -45,11 +45,15 @@ #include #endif /* COAP_WITH_LIBGNUTLS */ -#ifdef COAP_WITH_LIBMBEDTLS +#if COAP_WITH_LIBMBEDTLS #define HAVE_DTLS 1 #include #endif /* COAP_WITH_LIBMBEDTLS */ +#if COAP_WITH_LIBMBEDTLS_OSCORE +#include +#endif /* COAP_WITH_LIBMBEDTLS_OSCORE */ + #define ReturnIf_CU_ASSERT_PTR_NOT_NULL(value) \ CU_ASSERT_PTR_NOT_NULL(value); \ if ((void*)value == NULL) return; @@ -97,7 +101,7 @@ t_tls2(void) { #elif defined(COAP_WITH_LIBGNUTLS) version.version = GNUTLS_VERSION_NUMBER; version.type = COAP_TLS_LIBRARY_GNUTLS; -#elif defined(COAP_WITH_LIBMBEDTLS) +#elif COAP_WITH_LIBMBEDTLS || COAP_WITH_LIBMBEDTLS_OSCORE version.version = MBEDTLS_VERSION_NUMBER; version.type = COAP_TLS_LIBRARY_MBEDTLS; #else /* no DTLS */ diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 93e7679f..62a730ae 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -12,15 +12,25 @@ cmake_minimum_required(VERSION 3.20.0) if(CONFIG_LIBCOAP) - if(CONFIG_MBEDTLS AND NOT CONFIG_WOLFSSL) + if(CONFIG_MBEDTLS_OSCORE) + set(ENABLE_DTLS OFF) + set(DTLS_BACKEND OFF) + set(OSCORE_BACKEND "zephyr" CACHE STRING "Zephyr OSCORE build") + set_property(CACHE OSCORE_BACKEND PROPERTY STRINGS none zephyr) + set(ENABLE_OSCORE ON) + set(COAP_WITH_LIBMBEDTLS_OSCORE 1) + message(STATUS "libcoap: Using MbedTLS for OSCORE") + elseif(CONFIG_MBEDTLS AND NOT CONFIG_WOLFSSL) set(ENABLE_DTLS ON) set(DTLS_BACKEND "zephyr" CACHE STRING "Zephyr build") + set(OSCORE_BACKEND "zephyr") set_property(CACHE DTLS_BACKEND PROPERTY STRINGS zephyr wolfssl) set(COAP_WITH_LIBMBEDTLS 1) - message(STATUS "libcoap: Using mbedTLS for DTLS") + message(STATUS "libcoap: Using MbedTLS for DTLS") elseif(CONFIG_WOLFSSL) set(ENABLE_DTLS ON) set(DTLS_BACKEND "wolfssl" CACHE STRING "Zephyr build") + set(OSCORE_BACKEND OFF) set_property(CACHE DTLS_BACKEND PROPERTY STRINGS zephyr wolfssl) set(COAP_WITH_LIBWOLFSSL 1) # Set wolfSSL variables to prevent find_package call @@ -31,6 +41,8 @@ if(CONFIG_LIBCOAP) message(STATUS "libcoap: Using wolfSSL for DTLS") else() set(ENABLE_DTLS OFF) + set(DTLS_BACKEND OFF) + set(OSCORE_BACKEND OFF) endif() if (CONFIG_LIBCOAP_CLIENT_SUPPORT) set (ENABLE_CLIENT_MODE ON) diff --git a/zephyr/Kconfig b/zephyr/Kconfig index 6dd68b1b..39359017 100644 --- a/zephyr/Kconfig +++ b/zephyr/Kconfig @@ -224,27 +224,22 @@ config LIBCOAP_DEFAULT_SESSION_TIMEOUT if MBEDTLS +config MBEDTLS_OSCORE + bool "Set if OSCORE only" + default n + config MBEDTLS_CIPHER_CCM_ENABLED bool default y config MBEDTLS_DEBUG bool default y -config MBEDTLS_SERVER_NAME_INDICATION - bool - default y config MBEDTLS_KEY_EXCHANGE_RSA_ENABLED bool default y config MBEDTLS_ENTROPY_ENABLED bool default y -config MBEDTLS_SSL_PROTO_DTLS - bool - default y -config MBEDTLS_SSL_DTLS_CONNECTION_ID - bool - default y config MBEDTLS_ZEPHYR_ENTROPY bool default y @@ -278,12 +273,24 @@ config MBEDTLS_ECJPAKE_C config MBEDTLS_ECP_C bool default y +if ! MBEDTLS_OSCORE +config MBEDTLS_SERVER_NAME_INDICATION + bool + default y +config MBEDTLS_SSL_PROTO_DTLS + bool + default y +config MBEDTLS_SSL_DTLS_CONNECTION_ID + bool + default y config MBEDTLS_PEM_CERTIFICATE_FORMAT bool default y config MBEDTLS_SSL_EXTENDED_MASTER_SECRET bool default y +endif # ! MBEDTLS_OSCORE + config MBEDTLS_HAVE_TIME_DATE bool default y