Skip to content

Commit 8023165

Browse files
Roytakmichalvasko
authored andcommitted
netopeer2 UPDATE add mbedTLS support
1 parent a8f31e5 commit 8023165

File tree

5 files changed

+357
-15
lines changed

5 files changed

+357
-15
lines changed

CMakeLists.txt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,20 @@ endif()
213213
# put all binaries into one directory (even from subprojects)
214214
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
215215

216-
# dependencies - OpenSSL (required by later libnetconf2 checks and not really the server itself)
217-
find_package(OpenSSL 3.0.0)
218-
if(OPENSSL_FOUND)
219-
list(APPEND CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
220-
list(APPEND CMAKE_REQUIRED_LIBRARIES ${OPENSSL_LIBRARIES})
216+
# dependencies - SSL library (required by later libnetconf2 checks and not really the server itself)
217+
find_package(MbedTLS 3.5.0)
218+
if (MBEDTLS_FOUND)
219+
# dependencies - mbedtls
220+
set(HAVE_MBEDTLS TRUE)
221+
list(APPEND CMAKE_REQUIRED_INCLUDES ${MBEDTLS_INCLUDE_DIRS})
222+
list(APPEND CMAKE_REQUIRED_LIBRARIES ${MBEDTLS_LIBRARIES})
223+
else()
224+
# dependencies - OpenSSL
225+
find_package(OpenSSL 3.0.0)
226+
if(OPENSSL_FOUND)
227+
list(APPEND CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
228+
list(APPEND CMAKE_REQUIRED_LIBRARIES ${OPENSSL_LIBRARIES})
229+
endif()
221230
endif()
222231

223232
# dependencies - libssh (also required by libnetconf2 checks)

CMakeModules/FindMbedTLS.cmake

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# - Try to find MbedTLS
2+
# Once done this will define
3+
#
4+
# MBEDTLS_FOUND - MbedTLS was found
5+
# MBEDTLS_INCLUDE_DIRS - MbedTLS include directories
6+
# MBEDTLS_LIBRARIES - link these to use MbedTLS
7+
# MBEDTLS_VERSION - version of MbedTLS
8+
#
9+
# Author Roman Janota <[email protected]>
10+
# Copyright (c) 2025 CESNET, z.s.p.o.
11+
#
12+
# Redistribution and use in source and binary forms, with or without
13+
# modification, are permitted provided that the following conditions
14+
# are met:
15+
#
16+
# 1. Redistributions of source code must retain the copyright
17+
# notice, this list of conditions and the following disclaimer.
18+
# 2. Redistributions in binary form must reproduce the copyright
19+
# notice, this list of conditions and the following disclaimer in the
20+
# documentation and/or other materials provided with the distribution.
21+
# 3. The name of the author may not be used to endorse or promote products
22+
# derived from this software without specific prior written permission.
23+
#
24+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26+
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27+
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29+
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33+
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
#
35+
include(FindPackageHandleStandardArgs)
36+
37+
if(MBEDTLS_LIBRARIES AND MBEDTLS_INCLUDE_DIRS)
38+
# in cache already
39+
set(MBEDTLS_FOUND TRUE)
40+
else()
41+
find_path(MBEDTLS_INCLUDE_DIR
42+
NAMES
43+
mbedtls/ssl.h
44+
PATHS
45+
/opt/local/include
46+
/sw/include
47+
${CMAKE_INCLUDE_PATH}
48+
${CMAKE_INSTALL_PREFIX}/include
49+
)
50+
51+
find_library(MBEDTLS_LIBRARY
52+
NAMES
53+
libmbedtls.so
54+
PATHS
55+
/usr/lib
56+
/usr/lib64
57+
/opt/local/lib
58+
/sw/lib
59+
${CMAKE_LIBRARY_PATH}
60+
${CMAKE_INSTALL_PREFIX}/lib
61+
)
62+
63+
find_library(MBEDX509_LIBRARY
64+
NAMES
65+
libmbedx509.so
66+
PATHS
67+
/usr/lib
68+
/usr/lib64
69+
/opt/local/lib
70+
/sw/lib
71+
${CMAKE_LIBRARY_PATH}
72+
${CMAKE_INSTALL_PREFIX}/lib
73+
)
74+
75+
find_library(MBEDCRYPTO_LIBRARY
76+
NAMES
77+
libmbedcrypto.so
78+
PATHS
79+
/usr/lib
80+
/usr/lib64
81+
/opt/local/lib
82+
/sw/lib
83+
${CMAKE_LIBRARY_PATH}
84+
${CMAKE_INSTALL_PREFIX}/lib
85+
)
86+
87+
if(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARY AND MBEDX509_LIBRARY AND MBEDCRYPTO_LIBRARY)
88+
# learn MbedTLS version
89+
if(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
90+
file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h" MBEDTLS_VERSION
91+
REGEX "#define[ \t]+MBEDTLS_VERSION_STRING[ \t]+\"([0-9]+\.[0-9]+\.[0-9]+)\"")
92+
string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" MBEDTLS_VERSION ${MBEDTLS_VERSION})
93+
endif()
94+
if(NOT MBEDTLS_VERSION)
95+
message(STATUS "MBEDTLS_VERSION not found, assuming MbedTLS is too old and cannot be used!")
96+
set(MBEDTLS_INCLUDE_DIR "MBEDTLS_INCLUDE_DIR-NOTFOUND")
97+
set(MBEDTLS_LIBRARY "MBEDTLS_LIBRARY-NOTFOUND")
98+
endif()
99+
endif()
100+
101+
set(MBEDTLS_INCLUDE_DIRS ${MBEDTLS_INCLUDE_DIR})
102+
set(MBEDTLS_LIBRARIES ${MBEDTLS_LIBRARY} ${MBEDX509_LIBRARY} ${MBEDCRYPTO_LIBRARY})
103+
104+
find_package_handle_standard_args(MbedTLS FOUND_VAR MBEDTLS_FOUND
105+
REQUIRED_VARS MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARIES
106+
VERSION_VAR MBEDTLS_VERSION)
107+
108+
# show the MBEDTLS_INCLUDE_DIR and MBEDTLS_LIBRARIES variables only in the advanced view
109+
mark_as_advanced(MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARIES)
110+
endif()

cli/CMakeLists.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,18 @@ if(LIBNETCONF2_ENABLED_SSH_TLS)
4141
target_link_libraries(netopeer2-cli ${LIBSSH_LIBRARIES})
4242
include_directories(${LIBSSH_INCLUDE_DIRS})
4343

44-
# - openssl
45-
if(NOT OPENSSL_FOUND)
46-
message(FATAL_ERROR "libnetconf2 supports TLS but OpenSSL was not found, CLI compilation failed!")
44+
# - SSL library
45+
if (MBEDTLS_FOUND)
46+
# - MbedTLS (has priority over OpenSSL)
47+
target_link_libraries(netopeer2-cli ${MBEDTLS_LIBRARIES})
48+
include_directories(${MBEDTLS_INCLUDE_DIRS})
49+
elseif(OPENSSL_FOUND)
50+
# - OpenSSL
51+
target_link_libraries(netopeer2-cli ${OPENSSL_LIBRARIES})
52+
include_directories(${OPENSSL_INCLUDE_DIR})
53+
else()
54+
message(FATAL_ERROR "libnetconf2 supports TLS but neither MbedTLS nor OpenSSL were found, CLI compilation failed!")
4755
endif()
48-
target_link_libraries(netopeer2-cli ${OPENSSL_LIBRARIES})
49-
include_directories(${OPENSSL_INCLUDE_DIR})
5056
endif()
5157

5258
# compat checks

cli/cli_config.h.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@
1818
#define CLI_VERSION "@NP2CLI_VERSION@"
1919

2020
#define NC_CLI_PROMPT "@CLI_PROMPT@ "
21+
22+
/**
23+
* @brief Whether mbedTLS is used for TLS support.
24+
*/
25+
#cmakedefine HAVE_MBEDTLS

0 commit comments

Comments
 (0)