cmake UPDATE improve FindMbedTLS module
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a1fdd0e..690e37f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -234,14 +234,14 @@
if(ENABLE_SSH_TLS)
# dependencies - mbedTLS (higher preference) or OpenSSL
- find_package(LibMbedTLS 3.5.2)
- if (LIBMBEDTLS_FOUND)
+ find_package(MbedTLS 3.5.2)
+ if (MBEDTLS_FOUND)
# dependencies - mbedtls
- set(HAVE_LIBMBEDTLS TRUE)
+ set(HAVE_MBEDTLS TRUE)
list(APPEND libsrc src/session_mbedtls.c)
- include_directories(${LIBMBEDTLS_INCLUDE_DIRS})
- target_link_libraries(netconf2 ${LIBMBEDTLS_LIBRARIES})
- list(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBMBEDTLS_LIBRARIES})
+ include_directories(${MBEDTLS_INCLUDE_DIR})
+ target_link_libraries(netconf2 ${MBEDTLS_LIBRARIES})
+ list(APPEND CMAKE_REQUIRED_LIBRARIES ${MBEDTLS_LIBRARIES})
else()
# dependencies - openssl
find_package(OpenSSL 3.0.0 REQUIRED)
diff --git a/CMakeModules/FindLibMbedTLS.cmake b/CMakeModules/FindLibMbedTLS.cmake
deleted file mode 100644
index 7ab6f33..0000000
--- a/CMakeModules/FindLibMbedTLS.cmake
+++ /dev/null
@@ -1,99 +0,0 @@
-# - Try to find LibMbedTLS
-# Once done this will define
-#
-# LIBMBEDTLS_FOUND - system has LibPAM
-# LIBMBEDTLS_INCLUDE_DIRS - the LibPAM include directory
-# LIBMBEDTLS_LIBRARIES - link these to use LibPAM
-#
-# Author Roman Janota <xjanot04@fit.vutbr.cz>
-# Copyright (c) 2024 CESNET, z.s.p.o.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-# 1. Redistributions of source code must retain the copyright
-# notice, this list of conditions and the following disclaimer.
-# 2. Redistributions in binary form must reproduce the copyright
-# notice, this list of conditions and the following disclaimer in the
-# documentation and/or other materials provided with the distribution.
-# 3. The name of the author may not be used to endorse or promote products
-# derived from this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
-
-if(LIBMBEDTLS_LIBRARIES AND LIBMBEDTLS_INCLUDE_DIRS)
- # in cache already
- set(LIBMBEDTLS_FOUND TRUE)
-else()
- find_path(LIBMBEDTLS_INCLUDE_DIR
- NAMES
- mbedtls/ssl.h
- PATHS
- /opt/local/include
- /sw/include
- ${CMAKE_INCLUDE_PATH}
- ${CMAKE_INSTALL_PREFIX}/include
- )
-
- find_library(LIBMBEDTLS_LIBRARY
- NAMES
- libmbedtls.so # TODO
- PATHS
- /usr/lib
- /usr/lib64
- /opt/local/lib
- /sw/lib
- ${CMAKE_LIBRARY_PATH}
- ${CMAKE_INSTALL_PREFIX}/lib
- )
-
- find_library(LIBMBEDX509_LIBRARY
- NAMES
- libmbedx509.so
- PATHS
- /usr/lib
- /usr/lib64
- /opt/local/lib
- /sw/lib
- ${CMAKE_LIBRARY_PATH}
- ${CMAKE_INSTALL_PREFIX}/lib
- )
-
- find_library(LIBMBEDCRYPTO_LIBRARY
- NAMES
- libmbedcrypto.so
- PATHS
- /usr/lib
- /usr/lib64
- /opt/local/lib
- /sw/lib
- ${CMAKE_LIBRARY_PATH}
- ${CMAKE_INSTALL_PREFIX}/lib
- )
-
- if(LIBMBEDTLS_INCLUDE_DIR AND LIBMBEDTLS_LIBRARY AND LIBMBEDX509_LIBRARY AND LIBMBEDCRYPTO_LIBRARY)
- set(LIBMBEDTLS_FOUND TRUE)
- else()
- set(LIBMBEDTLS_FOUND FALSE)
- endif()
-
- set(LIBMBEDTLS_INCLUDE_DIRS ${LIBMBEDTLS_INCLUDE_DIR})
- set(LIBMBEDTLS_LIBRARIES ${LIBMBEDTLS_LIBRARY} ${LIBMBEDX509_LIBRARY} ${LIBMBEDCRYPTO_LIBRARY})
-
- include(FindPackageHandleStandardArgs)
- find_package_handle_standard_args(LibMbedTLS DEFAULT_MSG LIBMBEDTLS_LIBRARIES LIBMBEDTLS_INCLUDE_DIRS)
-
- # show the LIBMBEDTLS_INCLUDE_DIRS and LIBMBEDTLS_LIBRARIES variables only in the advanced view
- mark_as_advanced(LIBMBEDTLS_INCLUDE_DIRS LIBMBEDTLS_LIBRARIES)
-endif()
diff --git a/CMakeModules/FindMbedTLS.cmake b/CMakeModules/FindMbedTLS.cmake
new file mode 100644
index 0000000..6f1b03e
--- /dev/null
+++ b/CMakeModules/FindMbedTLS.cmake
@@ -0,0 +1,110 @@
+# - Try to find MbedTLS
+# Once done this will define
+#
+# MBEDTLS_FOUND - MbedTLS was found
+# MBEDTLS_INCLUDE_DIR - MbedTLS include directories
+# MBEDTLS_LIBRARIES - link these to use MbedTLS
+# MBEDTLS_VERSION - version of MbedTLS
+#
+# Author Roman Janota <janota@cesnet.cz>
+# Copyright (c) 2024 CESNET, z.s.p.o.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+# 3. The name of the author may not be used to endorse or promote products
+# derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+include(FindPackageHandleStandardArgs)
+
+if(MBEDTLS_LIBRARIES AND MBEDTLS_INCLUDE_DIR)
+ # in cache already
+ set(MBEDTLS_FOUND TRUE)
+else()
+ find_path(MBEDTLS_INCLUDE_DIR
+ NAMES
+ mbedtls/ssl.h
+ PATHS
+ /opt/local/include
+ /sw/include
+ ${CMAKE_INCLUDE_PATH}
+ ${CMAKE_INSTALL_PREFIX}/include
+ )
+
+ find_library(MBEDTLS_LIBRARY
+ NAMES
+ libmbedtls.so
+ PATHS
+ /usr/lib
+ /usr/lib64
+ /opt/local/lib
+ /sw/lib
+ ${CMAKE_LIBRARY_PATH}
+ ${CMAKE_INSTALL_PREFIX}/lib
+ )
+
+ find_library(MBEDX509_LIBRARY
+ NAMES
+ libmbedx509.so
+ PATHS
+ /usr/lib
+ /usr/lib64
+ /opt/local/lib
+ /sw/lib
+ ${CMAKE_LIBRARY_PATH}
+ ${CMAKE_INSTALL_PREFIX}/lib
+ )
+
+ find_library(MBEDCRYPTO_LIBRARY
+ NAMES
+ libmbedcrypto.so
+ PATHS
+ /usr/lib
+ /usr/lib64
+ /opt/local/lib
+ /sw/lib
+ ${CMAKE_LIBRARY_PATH}
+ ${CMAKE_INSTALL_PREFIX}/lib
+ )
+
+ if(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARY AND MBEDX509_LIBRARY AND MBEDCRYPTO_LIBRARY)
+ # learn MbedTLS version
+ if(EXISTS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h")
+ file(STRINGS "${MBEDTLS_INCLUDE_DIR}/mbedtls/build_info.h" MBEDTLS_VERSION
+ REGEX "#define[ \t]+MBEDTLS_VERSION_STRING[ \t]+\"([0-9]+\.[0-9]+\.[0-9]+)\"")
+ string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" MBEDTLS_VERSION ${MBEDTLS_VERSION})
+ endif()
+ if(NOT MBEDTLS_VERSION)
+ message(STATUS "MBEDTLS_VERSION not found, assuming MbedTLS is too old and cannot be used!")
+ set(MBEDTLS_INCLUDE_DIR "MBEDTLS_INCLUDE_DIR-NOTFOUND")
+ set(MBEDTLS_LIBRARY "MBEDTLS_LIBRARY-NOTFOUND")
+ endif()
+ endif()
+
+ set(MBEDTLS_INCLUDE_DIR ${MBEDTLS_INCLUDE_DIR})
+ set(MBEDTLS_LIBRARIES ${MBEDTLS_LIBRARY} ${MBEDX509_LIBRARY} ${MBEDCRYPTO_LIBRARY})
+
+ find_package_handle_standard_args(MbedTLS FOUND_VAR MBEDTLS_FOUND
+ REQUIRED_VARS MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARIES
+ VERSION_VAR MBEDTLS_VERSION)
+
+ # show the MBEDTLS_INCLUDE_DIR and MBEDTLS_LIBRARIES variables only in the advanced view
+ mark_as_advanced(MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARIES)
+endif()
diff --git a/src/config.h.in b/src/config.h.in
index c64db02..d366e0d 100644
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -47,7 +47,7 @@
/*
* Use MbedTLS as TLS back-end
*/
-#cmakedefine HAVE_LIBMBEDTLS
+#cmakedefine HAVE_MBEDTLS
/*
* Location of installed YANG modules on the system
diff --git a/src/session_wrapper.h b/src/session_wrapper.h
index 654a7d1..d5a5286 100644
--- a/src/session_wrapper.h
+++ b/src/session_wrapper.h
@@ -20,7 +20,7 @@
#include "config.h"
-#ifdef HAVE_LIBMBEDTLS
+#ifdef HAVE_MBEDTLS
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/entropy.h>