From 9869229bd3459303e2cd71c34901a66a5027355f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Tue, 25 Mar 2025 11:18:43 +0100 Subject: [PATCH 01/19] Bump cmake version to 3.11 Required for builtin `find_package` for OpenSSL. --- CMakeLists.txt | 2 +- src/core/CMakeLists.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 366cc809..48110c6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.8.8) +cmake_minimum_required(VERSION 3.11) project(ipfixcol2) # Description of the project diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index d90cc100..9055892a 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -90,6 +90,7 @@ target_link_libraries(ipfixcol2 ${BASE_LIB}) set_target_properties(ipfixcol2 PROPERTIES # by default, hide all symbols C_VISIBILITY_PRESET hidden CXX_VISIBILITY_PRESET hidden + ENABLE_EXPORTS on # Export symbols from executable for use by plugins ) # Installation targets From 272ed8c151616b4900b9388f21fb6a88ee751bdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Tue, 6 May 2025 10:55:24 +0200 Subject: [PATCH 02/19] Add missing include. --- src/tools/fdsdump/src/aggregator/stdAllocator.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tools/fdsdump/src/aggregator/stdAllocator.hpp b/src/tools/fdsdump/src/aggregator/stdAllocator.hpp index 3414f105..7f172a8d 100644 --- a/src/tools/fdsdump/src/aggregator/stdAllocator.hpp +++ b/src/tools/fdsdump/src/aggregator/stdAllocator.hpp @@ -9,6 +9,7 @@ #pragma once +#include #include #include From 72763c5c74f1ef53917f0ef55b4a34fe6f7d1198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Tue, 18 Mar 2025 11:08:38 +0100 Subject: [PATCH 03/19] TCP input - Fix `DecodeBuffer`. I'm not sure what the issue was, but after this rewrite it properly decodes IPFIX messages and the code structure is simmilar to `IpfixDecoder`. --- src/plugins/input/tcp/src/DecodeBuffer.cpp | 73 +++++++++++++--------- src/plugins/input/tcp/src/DecodeBuffer.hpp | 23 ++++++- 2 files changed, 62 insertions(+), 34 deletions(-) diff --git a/src/plugins/input/tcp/src/DecodeBuffer.cpp b/src/plugins/input/tcp/src/DecodeBuffer.cpp index e9941256..24b0882b 100644 --- a/src/plugins/input/tcp/src/DecodeBuffer.cpp +++ b/src/plugins/input/tcp/src/DecodeBuffer.cpp @@ -23,28 +23,16 @@ void DecodeBuffer::read_from(const uint8_t *data, size_t size) { // read until there is something to read while (size) { // get the message size - if (m_part_decoded.size() < sizeof(struct fds_ipfix_msg_hdr)) { - auto new_data = read_header(data, size); - size -= new_data - data; - data = new_data; + if (!read_header(&data, &size)) { + // There is not enough data to read the whole header. + break; } - if (size == 0) { - return; + // Read the body of the message. + if (!read_body(&data, &size)) { + // There is not enough data to read the whole body of the message. + break; } - - auto remaining = m_decoded_size - m_part_decoded.size(); - - if (read_min(data, size, remaining) != remaining) { - // all data is readed - return; - } - - data += remaining; - size -= remaining; - - add(std::move(m_part_decoded)); - m_part_decoded = ByteVector(); } } @@ -75,26 +63,49 @@ void DecodeBuffer::signal_eof() { m_eof_reached = true; } -const uint8_t *DecodeBuffer::read_header(const uint8_t *data, size_t size) { - constexpr size_t HDR_SIZE = sizeof(fds_ipfix_msg_hdr); +bool DecodeBuffer::read_header(const uint8_t **data, size_t *size) { + if (m_decoded_size != 0) { + // The header is already read, but the message body is incomplete. + return true; + } - auto filled = m_part_decoded.size(); - auto remaining = 0; - if (filled < HDR_SIZE) { - remaining = HDR_SIZE - filled; - if (read_min(data, size, remaining) == size) { - // not enough data for the whole header - return data + size; - } + // Read the header. + if (!read_until_n(sizeof(fds_ipfix_msg_hdr), data, size)) { + // There is not enough data to read the whole header. + return false; } + // The header has been successfully read. Load the size of the message. auto hdr = reinterpret_cast(m_part_decoded.data()); m_decoded_size = ntohs(hdr->length); - m_part_decoded.reserve(m_decoded_size); - return data + remaining; + if (m_decoded_size < sizeof(fds_ipfix_msg_hdr)) { + throw std::runtime_error("Invalid IPFIX message header size."); + } + + return true; } +bool DecodeBuffer::read_body(const uint8_t **data, size_t *size) { + // Read the body + if (!read_until_n(m_decoded_size, data, size)) { + // There is not enough data to read the whole body. + return false; + } + + m_decoded.push_back(std::move(m_part_decoded)); + m_part_decoded = ByteVector(); + m_decoded_size = 0; + return true; +} + +bool DecodeBuffer::read_until_n(size_t n, const uint8_t **data, size_t *data_len) { + m_part_decoded.reserve(n); + auto cnt = read_min(*data, n, *data_len); + *data_len -= cnt; + *data += cnt; + return m_part_decoded.size() == n; +} size_t DecodeBuffer::read_min(const uint8_t *data, size_t size1, size_t size2) { auto size = std::min(size1, size2); diff --git a/src/plugins/input/tcp/src/DecodeBuffer.hpp b/src/plugins/input/tcp/src/DecodeBuffer.hpp index 37c81918..0b835042 100644 --- a/src/plugins/input/tcp/src/DecodeBuffer.hpp +++ b/src/plugins/input/tcp/src/DecodeBuffer.hpp @@ -103,10 +103,27 @@ class DecodeBuffer { private: /** - * @brief reads the length from ipfix header to `m_decoded_size` - * @return Pointer to the first unreaded byte from `data` + * @brief Reads the length from ipfix header to `m_decoded_size`. + * @param[in,out] data Pointer to data. It is modified by the amount of read data. + * @param[in,out] size Size of the data. It is modified by the amount of read data. + * @return `true` if the whole header could be read. */ - const uint8_t *read_header(const uint8_t *data, size_t size); + bool read_header(const uint8_t **data, size_t *size); + /** + * @brief Reads the body of ipfix message. + * @param data Pointer to data. It is modified by the amount of read data. + * @param size Size of the data. It is modified by the amount of read data. + * @return `true` if the whole header could be read. + */ + bool read_body(const uint8_t **data, size_t *size); + /** + * @brief Reads to `m_part_decoded` until it reaches length of `n`. + * @param n Target length for `m_part_decoded`. + * @param[in,out] data Data to read from. It is modified by the read amount. + * @param[in,out] data_len Length of data to read. It is modified by the read amount. + * @return `true` if `m_part_decoded` has reached length of `n`. + */ + bool read_until_n(size_t n, const uint8_t **data, size_t *data_len); /** * @brief Adds N elements from `src` to `m_part_decoded` where N is the smaller of the two * sizes. From 210200c90a11ceec1410ff43a7e965db86e0db41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Tue, 25 Mar 2025 11:20:08 +0100 Subject: [PATCH 04/19] TCP input TLS - Add OpenSSL dependency --- .github/workflows/main.yml | 4 ++-- .github/workflows/packages.yml | 4 ++-- CMakeLists.txt | 3 +++ pkg/deb/templates/control.in | 4 ++-- pkg/rpm/ipfixcol2.spec.in | 4 ++-- src/plugins/input/tcp/CMakeLists.txt | 2 ++ 6 files changed, 13 insertions(+), 8 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index aae1e191..3d0c07e3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -34,7 +34,7 @@ jobs: if: startsWith(matrix.image, 'ubuntu') || startsWith(matrix.image, 'debian') run: | apt-get update - apt-get -y install git gcc g++ cmake make libxml2-dev liblz4-dev libzstd-dev + apt-get -y install git gcc g++ cmake make libxml2-dev liblz4-dev libzstd-dev libssl-dev apt-get -y install python3-docutils zlib1g-dev pkg-config apt-get -y install librdkafka-dev env: @@ -69,7 +69,7 @@ jobs: - name: Install dependencies for libfds and IPFIXcol2 (Rocky Linux, Oracle Linux) if: contains(matrix.image, 'rockylinux') || contains(matrix.image, 'oraclelinux') run: | - yum -y install git gcc gcc-c++ cmake make libxml2-devel lz4-devel libzstd-devel + yum -y install git gcc gcc-c++ cmake make libxml2-devel lz4-devel libzstd-devel openssl-devel yum -y install zlib-devel pkgconfig librdkafka-devel yum -y install python3-docutils || yum -y install python-docutils diff --git a/.github/workflows/packages.yml b/.github/workflows/packages.yml index 8e616e1e..294cff0a 100644 --- a/.github/workflows/packages.yml +++ b/.github/workflows/packages.yml @@ -40,7 +40,7 @@ jobs: - name: Install dependencies for libfds and IPFIXcol2 (Ubuntu/Debian) run: | apt-get update - apt-get -y install git gcc g++ cmake make libxml2-dev liblz4-dev libzstd-dev + apt-get -y install git gcc g++ cmake make libxml2-dev liblz4-dev libzstd-dev libssl-dev apt-get -y install python3-docutils zlib1g-dev pkg-config librdkafka-dev apt-get -y install debhelper devscripts build-essential fakeroot zip env: @@ -143,7 +143,7 @@ jobs: - name: Install dependencies for libfds and IPFIXcol2 (Rocky Linux, Oracle Linux) if: contains(matrix.image, 'rockylinux') || contains(matrix.image, 'oraclelinux') run: | - yum -y install git gcc gcc-c++ cmake make libxml2-devel lz4-devel libzstd-devel + yum -y install git gcc gcc-c++ cmake make libxml2-devel lz4-devel libzstd-devel openssl-devel yum -y install zlib-devel pkgconfig rpm-build librdkafka-devel yum -y install python3-docutils || yum -y install python-docutils diff --git a/CMakeLists.txt b/CMakeLists.txt index 48110c6a..880fe722 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,9 @@ set(CMAKE_THREAD_PREFER_PTHREAD TRUE) set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) +# Find OpenSSL +find_package(OpenSSL REQUIRED) + # ------------------------------------------------------------------------------ # Project components add_subdirectory(include) diff --git a/pkg/deb/templates/control.in b/pkg/deb/templates/control.in index 60ee446a..8679671f 100644 --- a/pkg/deb/templates/control.in +++ b/pkg/deb/templates/control.in @@ -7,12 +7,12 @@ Standards-Version: 3.9.8 Build-Depends: debhelper (>= 9), cmake (>= 2.8.8), make (>= 4.0), libfds-dev, gcc (>= 4.8), g++ (>= 4.8), pkg-config, zlib1g-dev, python3-docutils | python-docutils, - librdkafka-dev + librdkafka-dev, libssl-dev Package: @CPACK_PACKAGE_NAME@ Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, libfds (>= 0.2.0), zlib1g, - librdkafka1 (>= 0.9.3) + librdkafka1 (>= 0.9.3), libssl-dev Description: @CPACK_PACKAGE_DESCRIPTION_SUMMARY@ IPFIXcol is a flexible IPFIX (RFC 7011) flow data collector designed to be extensible by plugins. diff --git a/pkg/rpm/ipfixcol2.spec.in b/pkg/rpm/ipfixcol2.spec.in index 182dc8b2..fde573aa 100644 --- a/pkg/rpm/ipfixcol2.spec.in +++ b/pkg/rpm/ipfixcol2.spec.in @@ -13,8 +13,8 @@ Packager: @CPACK_PACKAGE_CONTACT@ BuildRoot: %{_tmppath}/%{name}-%{version}-%{release} BuildRequires: gcc >= 4.8, gcc-c++ >= 4.8, cmake >= 2.8.8, make BuildRequires: libfds-devel, /usr/bin/rst2man, zlib-devel -BuildRequires: librdkafka-devel -Requires: libfds >= 0.2.0, zlib, librdkafka >= 0.9.3 +BuildRequires: librdkafka-devel, openssl-devel +Requires: libfds >= 0.2.0, zlib, librdkafka >= 0.9.3, openssl-libs %description IPFIXcol is a flexible IPFIX (RFC 7011) flow data collector designed to diff --git a/src/plugins/input/tcp/CMakeLists.txt b/src/plugins/input/tcp/CMakeLists.txt index b2ab68ca..2e65f2d6 100644 --- a/src/plugins/input/tcp/CMakeLists.txt +++ b/src/plugins/input/tcp/CMakeLists.txt @@ -29,6 +29,8 @@ if (CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_HOST_SYSTEM_NAME STREQUAL ) endif() +target_link_libraries(tcp-input OpenSSL::Crypto OpenSSL::SSL) + install( TARGETS tcp-input LIBRARY DESTINATION "${INSTALL_DIR_LIB}/ipfixcol2/" From 425032a156a010893f015199676f247942df654d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Thu, 6 Mar 2025 13:43:27 +0100 Subject: [PATCH 05/19] TCP input TLS - Add helper functions for throwing OpenSSL errors. --- .../input/tcp/src/tls/throw_ssl_err.hpp | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/plugins/input/tcp/src/tls/throw_ssl_err.hpp diff --git a/src/plugins/input/tcp/src/tls/throw_ssl_err.hpp b/src/plugins/input/tcp/src/tls/throw_ssl_err.hpp new file mode 100644 index 00000000..2fa56eaf --- /dev/null +++ b/src/plugins/input/tcp/src/tls/throw_ssl_err.hpp @@ -0,0 +1,90 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief Helper functions for forming OpenSSL error messages. (header file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#pragma once + +#include +#include +#include + +#include + +namespace tcp_in { +namespace tls { + +/** + * @brief Throws exception with the given message appended with arguments and OpenSSL error message + * based on error code. + * @param code Error code of the OpenSSL message. + * @param msg Message describing the error. + * @throws `std::runtime_error` + */ +[[noreturn]] +static void throw_ssl_err(int code, std::ostringstream msg) { + std::array err; + ERR_error_string_n(code, err.data(), err.size()); + msg << err.data(); + + auto res = msg.str(); + if (*res.rbegin() == '.') { + res.pop_back(); + res += ": "; + } + + throw std::runtime_error(res + err.data()); +} + +/** + * @brief Throws exception with the given message appended with arguments and OpenSSL error message + * based on error code. + * @param code Error code of the OpenSSL message. + * @param msg Partial message describing the error. + * @param arg Next argument to be appended to the message. + * @param args This will be converted to string and appended to the message. + * @throws `std::runtime_error` + */ +template +[[noreturn]] +void throw_ssl_err(int code, std::ostringstream msg, ARG &&arg, ARGS &&...args) { + msg << arg; + throw_ssl_err(code, std::move(msg), std::forward(args)...); +} + +/** + * @brief Throws exception with the given message appended with arguments and OpenSSL error message + * based on error code. + * @param code Error code of the OpenSSL message. + * @param what Message describing the error. + * @param args This will be converted to string and appended to the message. + * @throws `std::runtime_error` + */ +template +[[noreturn]] +void throw_ssl_err(int code, const std::string &what, ARGS &&...args) { + std::ostringstream res; + res << what; + throw_ssl_err(code, std::move(res), std::forward(args)...); +} + +/** + * @brief Throws exception with the given message appended with arguments and error message from top + * of the OpenSSL error stack. The error is popped from the error stack. + * @param what Message describing the error. + * @param args This will be converted to string and appended to the message. + * @throws `std::runtime_error` + */ +template +[[noreturn]] +void throw_ssl_err(const std::string &what, ARGS &&...args) { + throw_ssl_err(ERR_get_error(), what, std::forward(args)...); +} + +} // namespace tls +} // namespace tcp_in From bf8ab2b8e48f64581a1d1fc01cd8b4a6474108f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Thu, 6 Mar 2025 13:44:25 +0100 Subject: [PATCH 06/19] TCP input TLS - Add SslBio, wrapper around BIO. --- src/plugins/input/tcp/src/tls/SslBio.hpp | 65 ++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/plugins/input/tcp/src/tls/SslBio.hpp diff --git a/src/plugins/input/tcp/src/tls/SslBio.hpp b/src/plugins/input/tcp/src/tls/SslBio.hpp new file mode 100644 index 00000000..90b82be5 --- /dev/null +++ b/src/plugins/input/tcp/src/tls/SslBio.hpp @@ -0,0 +1,65 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief Wrapper around BIO from OpenSSL. (header file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#pragma once + +#include + +#include + +#include "../UniqueFd.hpp" +#include "throw_ssl_err.hpp" + +namespace tcp_in { +namespace tls { + +/** + * @brief Wrapper around `BIO` from OpenSSL. + * + * This is basically extension of file descriptor used in OpenSSL. + */ +class SslBio { +public: + /** + * @brief Constructs new `SslBio` for the given `BIO_METHOD`. + * @param method Method created for example by `BIO_s_socket()`. + * @throws `std::runtime_error` on failure. + */ + SslBio(const BIO_METHOD *method) : m_bio(BIO_new(method)) { + if (!m_bio.get()) { + throw_ssl_err("Failed to create bio."); + } + } + + /** + * @brief Set file descriptor of the bio. + * @param fd File descriptor for the bio. This will NOT take ownership of the file descriptor. + */ + void set_fd(int fd) noexcept { BIO_set_fd(m_bio.get(), fd, BIO_NOCLOSE); } + + /** + * @brief Gets the pointer to bio and releases ownership of it. + * @return Pointer to the inner bio. + */ + BIO *release_ptr() noexcept { return m_bio.release(); } + +private: + /** Deleter for BIO. */ + class Deleter { + public: + void operator()(BIO *bio) { BIO_free(bio); } + }; + + std::unique_ptr m_bio; +}; + + +} // namespace tls +} // namespace tcp_in From 378bea2b924a619af958f8f0ace9e578d7441faf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Thu, 6 Mar 2025 13:45:54 +0100 Subject: [PATCH 07/19] TCP input TLS - Add Ssl, wrapper around SSL. --- src/plugins/input/tcp/CMakeLists.txt | 1 + src/plugins/input/tcp/src/tls/Ssl.cpp | 57 +++++++++++ src/plugins/input/tcp/src/tls/Ssl.hpp | 136 ++++++++++++++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 src/plugins/input/tcp/src/tls/Ssl.cpp create mode 100644 src/plugins/input/tcp/src/tls/Ssl.hpp diff --git a/src/plugins/input/tcp/CMakeLists.txt b/src/plugins/input/tcp/CMakeLists.txt index 2e65f2d6..adf381fd 100644 --- a/src/plugins/input/tcp/CMakeLists.txt +++ b/src/plugins/input/tcp/CMakeLists.txt @@ -13,6 +13,7 @@ add_library(tcp-input MODULE src/Acceptor.cpp src/Plugin.cpp src/IpxPlugin.cpp + src/tls/Ssl.cpp ) find_package(LibLz4 REQUIRED) diff --git a/src/plugins/input/tcp/src/tls/Ssl.cpp b/src/plugins/input/tcp/src/tls/Ssl.cpp new file mode 100644 index 00000000..ce0d01c5 --- /dev/null +++ b/src/plugins/input/tcp/src/tls/Ssl.cpp @@ -0,0 +1,57 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief Wrapper around SSL from OpenSSL. (source file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "Ssl.hpp" + +namespace tcp_in { +namespace tls { + +bool Ssl::accept() { + auto res = SSL_accept(m_ssl.get()); + if (res <= 0) { + auto err = SSL_get_error(m_ssl.get(), res); + // Nonblocking client needs to wait. + if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { + return false; + } + throw_ssl_err(err, "Failed performing TLS handshake with client."); + } + return true; +} + +ReadResult Ssl::read_ex(std::vector &buf) { + buf.resize(buf.capacity()); + std::size_t len; + auto ret = SSL_read_ex(m_ssl.get(), buf.data(), buf.size(), &len); + + if (ret <= 0) { + auto err = SSL_get_error(m_ssl.get(), ret); + buf.resize(0); + switch (err) { + case SSL_ERROR_SSL: + // The TLS connection is broken. + case SSL_ERROR_ZERO_RETURN: + return ReadResult::FINISHED; + case SSL_ERROR_SYSCALL: + return ReadResult::CLOSED; + case SSL_ERROR_WANT_READ: + case SSL_ERROR_WANT_WRITE: + return ReadResult::WAIT; + default: + throw_ssl_err(err, "SSL failed to read data."); + } + } + + buf.resize(std::min(len, buf.size())); + return ReadResult::READ; +} + +} // namespace tls +} // namespace tcp_in diff --git a/src/plugins/input/tcp/src/tls/Ssl.hpp b/src/plugins/input/tcp/src/tls/Ssl.hpp new file mode 100644 index 00000000..9ea8c114 --- /dev/null +++ b/src/plugins/input/tcp/src/tls/Ssl.hpp @@ -0,0 +1,136 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief Wrapper around SSL from OpenSSL. (header file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#pragma once + +#include +#include +#include + +#include + +#include "SslBio.hpp" +#include "throw_ssl_err.hpp" + +namespace tcp_in { +namespace tls { + +/** Describes result of read operation. */ +enum class ReadResult { + /** Successufully read some data. */ + READ, + /** Non blocking socket needs to wait for more data. */ + WAIT, + /** Peer will not send any further data. */ + FINISHED, + /** The connection has been closed. (socket cannot read) */ + CLOSED, +}; + +/** + * @brief Wrapper around `SSL` from OpenSSL. + * + * This is basically io stream for data encrypted with TLS. + */ +class Ssl { +public: + /** + * @brief Construct `Ssl` from the given `SSL_CTX`. + * @param ctx Base context for the ssl object. + * @throws `std::runtime_error` if failed to create new bio. + */ + Ssl(SSL_CTX *ctx) : m_ssl(SSL_new(ctx)) { + if (!m_ssl) { + throw_ssl_err("Failed to create SSL object."); + } + } + + /** + * @brief Set the bio object from which this will read data. + * @param bio Bio object from which to read. This will take ownership of the given `SslBio`. + */ + void set_bio(SslBio bio) noexcept { + auto raw_bio = bio.release_ptr(); + SSL_set_bio(m_ssl.get(), raw_bio, raw_bio); + } + + /** + * @brief Set the server name for SNI. + * @param hostname Name of the server. + * @throws `std::runtime_error` on failure. + */ + void set_tlsext_host_name(const char *hostname) { + if (!SSL_set_tlsext_host_name(m_ssl.get(), hostname)) { + throw_ssl_err("Failed to set the SNI hostname."); + } + } + + /** + * @brief Set the expected DNS name of the server. + * @param hostname Hostname of the server. + * @throws `std::runtime_error` on failure. + */ + void set1_host(const char *hostname) { + if (!SSL_set1_host(m_ssl.get(), hostname)) { + throw_ssl_err( + "Failed to set the cergificate verification hostname." + ); + } + } + + /** + * @brief Perform TLS handshake. + * + * In case of nonblocking server, this can be multiple times to continue the tls handshake. + * @returns `true` if TLS handshake is successfully complete. `false` if the tls handshake is + * incomplete because the underlaying socket is nonblocking and it needs to wait for more data. + * @throws `std::runtime_error` on failure. + */ + bool accept(); + + /** + * @brief Read application data from the TLS connection. + * + * @param [out] buf Will be cleared. The read data will be put here. Up to the capacity of the + * buffer will be used. + * @return result of the read operation: + * - `READ`: Successfully read some data. `buf` shouldn't be empty. + * - `WAIT`: Non blocking socket needs to wait for more data. `buf` should be empty. + * - `FINISHED`: Peer will not send any further data. `buf` should be empty. + * - `CLOSED`: The connection has been closed. (socket cannot read) `buf` should be empty. + * @throws `std::runtime_error` on failure. + */ + ReadResult read_ex(std::vector &buf); + + /** + * @brief Shuts the connection down. + * @return `true` on successful shutdown, false if shutdown is in process but not complete yet. + * @throws `std::runtime_error` on failure. + */ + bool shutdown() { + auto ret = SSL_shutdown(m_ssl.get()); + if (ret < 0) { + throw_ssl_err("SSL failed to shutdown connection."); + } + return ret == 1; + } + +private: + /** Deleter for SSL. */ + class Deleter { + public: + void operator()(SSL *ssl) { SSL_free(ssl); } + }; + + std::unique_ptr m_ssl; +}; + +} // namespace tls +} // namespace tcp_in From 8aa211cd2c0107bc1ab539ef416b636723d6a66b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Thu, 6 Mar 2025 13:46:47 +0100 Subject: [PATCH 08/19] TCP input TLS - Add SslCtx, wrapper around SSL_CTX. --- src/plugins/input/tcp/src/tls/SslCtx.hpp | 229 +++++++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 src/plugins/input/tcp/src/tls/SslCtx.hpp diff --git a/src/plugins/input/tcp/src/tls/SslCtx.hpp b/src/plugins/input/tcp/src/tls/SslCtx.hpp new file mode 100644 index 00000000..eccc2536 --- /dev/null +++ b/src/plugins/input/tcp/src/tls/SslCtx.hpp @@ -0,0 +1,229 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief Wrapper around SSL_CTX from OpenSSL. (header file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#pragma once + +#include +#include +#include +#include + +#include +#include + +#include "Ssl.hpp" +#include "throw_ssl_err.hpp" + +namespace tcp_in { +namespace tls { + +/** Wrapper around `SSL_CTX` from OpenSSL. */ +class SslCtx { +public: + /** + * @brief Construct this with the given method. + * @param method Method for creating `SSL_CTX`. Can be obtained for example with + * `TLS_server_method()`. + * @throws `std::runtime_error` on failure. + */ + SslCtx(const SSL_METHOD *method) : m_ctx(SSL_CTX_new(method)) { + if (!m_ctx.get()) { + throw_ssl_err("Failed to create ssl context."); + } + } + + /** + * @brief Set the verify mode. (What should be verified about peer) + * @param mode Verify mode (e.g. SSL_VERIFY_NONE). + * @param cb Verify callback. + */ + void set_verify(int mode, SSL_verify_cb cb = nullptr) noexcept { + SSL_CTX_set_verify(m_ctx.get(), mode, cb); + } + + /** + * @brief Sets the paths to trusted certificates to the default locations. + * + * This will try to set the paths from environment variables, and if that fails, it will use the + * os defaults. + * @throws `std::runtime_error` on failure. + */ + void set_default_verify_paths() { + if (!SSL_CTX_set_default_verify_paths(m_ctx.get())) { + throw_ssl_err("Failed to set default trusted certificate paths."); + } + } + + /** + * @brief Sets the path to trusted certificate file. + * + * This will try to set the path from environment variable `SSL_CERT_FILE`, and if that fails, + * it will use the os defaults. + * @throws `std::runtime_error` on failure. + */ + void set_default_verify_file() { + if (!SSL_CTX_set_default_verify_file(m_ctx.get())) { + throw_ssl_err("Failed to set default trusted certificate file."); + } + } + + /** + * @brief Sets the path to trusted certificate file. + * @throws `std::runtime_error` on failure. + */ + void load_verify_file(const char *path) { + if (!SSL_CTX_load_verify_file(m_ctx.get(), path)) { + throw_ssl_err("Failed to set default trusted certificate file `", path, "`."); + } + } + + /** + * @brief Sets the path to trusted certificates directory. + * + * This will try to set the path from environment variable `SSL_CERT_DIR`, and if that fails, it + * will use the os defaults. + * @throws `std::runtime_error` on failure. + */ + void set_default_verify_dir() { + if (!SSL_CTX_set_default_verify_dir(m_ctx.get())) { + throw_ssl_err("Failed to set default trusted certificate directory."); + } + } + + /** + * @brief Sets the path to trusted certificates directory. + * @throws `std::runtime_error` on failure. + */ + void load_verify_dir(const char *path) { + if (!SSL_CTX_load_verify_dir(m_ctx.get(), path)) { + throw_ssl_err("Failed to set default trusted certificate directory `", path, "`."); + } + } + + /** + * @brief Sets the path to trusted certificates store. + * + * It will use the OS defaults. + * @throws `std::runtime_error` on failure. + */ + void set_default_verify_store() { + if (!SSL_CTX_set_default_verify_store(m_ctx.get())) { + throw_ssl_err("Failed to set default trusted certificate store."); + } + } + + /** + * @brief Sets the path to trusted certificates store. + * @throws `std::runtime_error` on failure. + */ + void load_verify_store(const char *path) { + if (!SSL_CTX_load_verify_store(m_ctx.get(), path)) { + throw_ssl_err("Failed to set default trusted certificate store `", path, "`."); + } + } + + /** + * @brief Set the minimum allowed TLS version. + * @param version Minimum allowed tls version. `TLS1_2_VERSION` and higher is recommended. + * @throws `std::runtime_error` on failure. + */ + void set_min_proto_version(long version) { + if (!SSL_CTX_set_min_proto_version(m_ctx.get(), version)) { + throw_ssl_err("Failed to set minimum tls version."); + } + } + + /** + * @brief Set `SslCtx` flags. + * @param opts Flags for `SSL_CTX`. + */ + void set_options(std::uint64_t opts) noexcept { SSL_CTX_set_options(m_ctx.get(), opts); } + + /** + * @brief Set file with certificate with which this will verify to peer. + * @param file_path Path to file in PEM format with certificate (and parent certificate(s)). + * @throws `std::runtime_error` on failure. + */ + void use_certificate_chain_file(const char *file_path) { + if (SSL_CTX_use_certificate_chain_file(m_ctx.get(), file_path) <= 0) { + throw_ssl_err("Failed to load certificate chain file."); + } + } + + /** + * @brief Set private key of used certificate. + * + * This may prompt the user for password for the private key. + * @param file_path Path to file with the private key. + * @param type Format of the file (e.g. `SSL_FILETYPE_PEM`). + * @throws `std::runtime_error` on failure. + */ + void use_private_key_file(const char *file_path, int type) { + if (SSL_CTX_use_PrivateKey_file(m_ctx.get(), file_path, type) <= 0) { + throw_ssl_err("Failed to load private key file."); + } + } + + /** + * @brief Set cache id unique to aplication. + * @param id_data Arbitrary cache id data. + * @param id_len Length of `id_data`. + */ + void set_session_id_context(const char *id_data, unsigned id_len) noexcept { + SSL_CTX_set_session_id_context( + m_ctx.get(), + reinterpret_cast(id_data), + id_len + ); + } + + /** + * @brief Set the session caching mode. (Caching for connections from the same client.) + * @param mode Caching mode (e.g. `SSL_SESS_CACHE_SERVER`). + */ + void set_session_cache_mode(long mode) noexcept { + SSL_CTX_set_session_cache_mode(m_ctx.get(), mode); + } + + /** + * @brief Sets the size of cache. + * @param size Size of the cache. + */ + void sess_set_cache_size(long size) noexcept { + SSL_CTX_sess_set_cache_size(m_ctx.get(), size); + } + + /** + * @brief Set timeout for cached connection data. + * @param seconds Timeout for cached connection data in seconds. + */ + void sess_set_timeout(long seconds) noexcept { + SSL_CTX_set_timeout(m_ctx.get(), seconds); + } + + /** + * @brief Uses this to create new `Ssl`. + * @return Newly create ssl. + * @throws `std::runtime_error` on failure. + */ + Ssl create_ssl() { return { m_ctx.get() }; } + +private: + /** Deleter for SSL_CTX. */ + class Deleter { + public: + void operator()(SSL_CTX *ptr) { SSL_CTX_free(ptr); } + }; + + std::unique_ptr m_ctx; +}; + +} // namespace tls +} // namespace tcp_in From 0062b1d878dadb34681f376b3c9f59592bc000e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Thu, 6 Mar 2025 13:48:12 +0100 Subject: [PATCH 09/19] TCP input TLS - Add TlsDecoder. --- src/plugins/input/tcp/CMakeLists.txt | 1 + src/plugins/input/tcp/src/tls/TlsDecoder.cpp | 63 ++++++++++++++++++++ src/plugins/input/tcp/src/tls/TlsDecoder.hpp | 57 ++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 src/plugins/input/tcp/src/tls/TlsDecoder.cpp create mode 100644 src/plugins/input/tcp/src/tls/TlsDecoder.hpp diff --git a/src/plugins/input/tcp/CMakeLists.txt b/src/plugins/input/tcp/CMakeLists.txt index adf381fd..7a5697ef 100644 --- a/src/plugins/input/tcp/CMakeLists.txt +++ b/src/plugins/input/tcp/CMakeLists.txt @@ -14,6 +14,7 @@ add_library(tcp-input MODULE src/Plugin.cpp src/IpxPlugin.cpp src/tls/Ssl.cpp + src/tls/TlsDecoder.cpp ) find_package(LibLz4 REQUIRED) diff --git a/src/plugins/input/tcp/src/tls/TlsDecoder.cpp b/src/plugins/input/tcp/src/tls/TlsDecoder.cpp new file mode 100644 index 00000000..db90eb01 --- /dev/null +++ b/src/plugins/input/tcp/src/tls/TlsDecoder.cpp @@ -0,0 +1,63 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief TLS decoder for IPFIX plugin. (source file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "TlsDecoder.hpp" + +namespace tcp_in { +namespace tls { + +// Size of buffer for copying data from OpenSSL to DecodeBuffer. +constexpr std::size_t BUFFER_SIZE = 4096; + +TlsDecoder::TlsDecoder(SslCtx &ctx, int fd) : + m_ssl(ctx.create_ssl()), + m_buffer(BUFFER_SIZE) +{ + SslBio bio(BIO_s_socket()); + bio.set_fd(fd); + m_ssl.set_bio(std::move(bio)); + + m_handshake_complete = m_ssl.accept(); +} + +DecodeBuffer &TlsDecoder::decode() { + if (!m_handshake_complete) { + m_handshake_complete = m_ssl.accept(); + if (!m_handshake_complete) { + return m_decoded; + } + } + + // XXX: The copy to intermidiate `m_buffer` can be avoided by extending `DecodeBuffer` to be + // able to read with generic read function. + + while (!m_decoded.enough_data()) { + auto res = m_ssl.read_ex(m_buffer); + m_decoded.read_from(m_buffer.data(), m_buffer.size()); + switch (res) { + case ReadResult::READ: + break; + case ReadResult::WAIT: + return m_decoded; + case ReadResult::FINISHED: + // FIXME: properly check that the shutdown is complete. + m_ssl.shutdown(); + // Intentional falltrough + case ReadResult::CLOSED: + m_decoded.signal_eof(); + return m_decoded; + } + } + + return m_decoded; +} + +} // namespace tls +} // namespace tcp_in diff --git a/src/plugins/input/tcp/src/tls/TlsDecoder.hpp b/src/plugins/input/tcp/src/tls/TlsDecoder.hpp new file mode 100644 index 00000000..7b45a5f7 --- /dev/null +++ b/src/plugins/input/tcp/src/tls/TlsDecoder.hpp @@ -0,0 +1,57 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief TLS decoder for IPFIX plugin. (header file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#pragma once + +#include + +#include "../Decoder.hpp" +#include "../UniqueFd.hpp" +#include "SslCtx.hpp" +#include "Ssl.hpp" + +namespace tcp_in { +namespace tls { + +// TLS content type handshake +/** Identifies data for which `TlsDecoder` should be used. */ +constexpr std::uint8_t TLS_MAGIC = 22; + +/** Decoder for TLS connections. */ +class TlsDecoder : public Decoder { +public: + /** + * @brief Create new TLS decoder with shared context. + * @param ctx Initialized shared context. Its configuration is not modified. + * @param fd Stream for TLS communication (Usually TCP). + */ + TlsDecoder(SslCtx &ctx, int fd); + + /** + * @brief Reads all available data from TLS stream and returns buffer with decoded messages. + * @returns Buffer with decoded messages. + */ + virtual DecodeBuffer &decode() override; + + /** + * @brief Gets the name of the decoder. + * @return returns `"TLS"`. + */ + virtual const char *get_name() const override { return "TLS"; } + +private: + Ssl m_ssl; + bool m_handshake_complete = false; + DecodeBuffer m_decoded; + std::vector m_buffer; +}; + +} // namespace tls +} // namespace tcp_in From 5cd6906380897f5632752ca0f23c3a472d6d73c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Thu, 6 Mar 2025 13:50:43 +0100 Subject: [PATCH 10/19] TCP input TLS - Add DecoderFactory for TlsDecoders. --- src/plugins/input/tcp/CMakeLists.txt | 1 + .../input/tcp/src/tls/DecoderFactory.cpp | 96 +++++++++++++++++++ .../input/tcp/src/tls/DecoderFactory.hpp | 44 +++++++++ 3 files changed, 141 insertions(+) create mode 100644 src/plugins/input/tcp/src/tls/DecoderFactory.cpp create mode 100644 src/plugins/input/tcp/src/tls/DecoderFactory.hpp diff --git a/src/plugins/input/tcp/CMakeLists.txt b/src/plugins/input/tcp/CMakeLists.txt index 7a5697ef..7a2f4a5c 100644 --- a/src/plugins/input/tcp/CMakeLists.txt +++ b/src/plugins/input/tcp/CMakeLists.txt @@ -15,6 +15,7 @@ add_library(tcp-input MODULE src/IpxPlugin.cpp src/tls/Ssl.cpp src/tls/TlsDecoder.cpp + src/tls/DecoderFactory.cpp ) find_package(LibLz4 REQUIRED) diff --git a/src/plugins/input/tcp/src/tls/DecoderFactory.cpp b/src/plugins/input/tcp/src/tls/DecoderFactory.cpp new file mode 100644 index 00000000..9efc5a9f --- /dev/null +++ b/src/plugins/input/tcp/src/tls/DecoderFactory.cpp @@ -0,0 +1,96 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief Factory for tls connections. (source file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "DecoderFactory.hpp" + +#include "../Config.hpp" +#include "TlsDecoder.hpp" + +// OpenSSL v1.1.1 compatibility. +// The flag SSL_OP_IGNORE_UNEXPECTED_EOF is not supported in v1.1.1. This will define it as flag +// without effect if it is not supported so that it may be used in any case. +// The flag is not neccesary, but with it if peer closes the connection without sending proper TLS +// message, it will not be treated as error. This is OK because can check if IPFIX messages are +// complete. +#ifndef SSL_OP_IGNORE_UNEXPECTED_EOF +#define SSL_OP_IGNORE_UNEXPECTED_EOF 0 +#endif // ifndef SSL_OP_IGNORE_UNEXPECTED_EOF + +namespace tcp_in { +namespace tls { + +/** + * @brief Load certificate authority based on the configuration. + * @param conf Configuration of what should be load as certificate authority. + * @param ctx Context to which will the ca load. + */ +static void load_ca(const Config &conf, SslCtx &ctx); + +DecoderFactory::DecoderFactory(const Config &conf) : m_ctx(TLS_server_method()) { + // Cache configuration. Cache is used to speed up initial handshake for clients that were + // recently connected. + constexpr long CACHE_TIMEOUT_SECONDS = 3600; + const std::string cache_id = "ipfixcol2"; + constexpr std::size_t CACHE_SIZE = 1024; + + m_ctx.set_min_proto_version(TLS1_2_VERSION); + m_ctx.set_options( + SSL_OP_IGNORE_UNEXPECTED_EOF | SSL_OP_NO_RENEGOTIATION | SSL_OP_CIPHER_SERVER_PREFERENCE + ); + + sleep(1); + + m_ctx.use_certificate_chain_file(conf.certificate_file.c_str()); + m_ctx.use_private_key_file(conf.private_key_file.c_str(), SSL_FILETYPE_PEM); + + m_ctx.set_session_id_context(cache_id.data(), unsigned(cache_id.size())); + m_ctx.set_session_cache_mode(SSL_SESS_CACHE_SERVER); + m_ctx.sess_set_cache_size(CACHE_SIZE); + m_ctx.sess_set_timeout(CACHE_TIMEOUT_SECONDS); + + if (conf.verify_peer) { + m_ctx.set_verify(SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT); + load_ca(conf, m_ctx); + } else { + m_ctx.set_verify(SSL_VERIFY_NONE); + } +} + +std::unique_ptr DecoderFactory::create(int fd) { + return std::unique_ptr(new TlsDecoder(m_ctx, fd)); +} + +static void load_ca(const Config &conf, SslCtx &ctx) { + if (conf.use_default_ca) { + ctx.set_default_verify_paths(); + return; + } + + if (conf.default_ca_file) { + ctx.set_default_verify_file(); + } else if (!conf.ca_file.empty()) { + ctx.load_verify_file(conf.ca_file.c_str()); + } + + if (conf.default_ca_dir) { + ctx.set_default_verify_dir(); + } else if (!conf.ca_dir.empty()) { + ctx.load_verify_dir(conf.ca_dir.c_str()); + } + + if (conf.default_ca_store) { + ctx.set_default_verify_store(); + } else if (!conf.ca_store.empty()) { + ctx.load_verify_store(conf.ca_store.c_str()); + } +} + +} // namespace tls +} // namespace tcp_in diff --git a/src/plugins/input/tcp/src/tls/DecoderFactory.hpp b/src/plugins/input/tcp/src/tls/DecoderFactory.hpp new file mode 100644 index 00000000..764fb623 --- /dev/null +++ b/src/plugins/input/tcp/src/tls/DecoderFactory.hpp @@ -0,0 +1,44 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief Factory for tls connections. (header file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#pragma once + +#include "../Config.hpp" +#include "../Decoder.hpp" +#include "SslCtx.hpp" + +namespace tcp_in { +namespace tls { + +/** Factory for `TlsDecoder`s. Holds shared data. */ +class DecoderFactory { +public: + /** + * @brief Create new tls decoder factory using certificate and private key in the given file. + * Note that this may prompt the user for password. + * @throws `std::runtime_error` on failure. + */ + DecoderFactory(const Config &conf); + + /** + * @brief Create new tls decoder. + * @param fd Stream for tls communication. (Usually TCP). + * @return TLS decoder. + * @throws `std::runtime_exception` if initialization of the decoder fails. + */ + std::unique_ptr create(int fd); + +private: + SslCtx m_ctx; +}; + +} // namespace tls +} // namespace tcp_in + From cf7331a7c0f25ab284d482da7dc98b63e279aa27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Thu, 6 Mar 2025 13:51:38 +0100 Subject: [PATCH 11/19] TCP input TLS - Add TLS configuration. --- src/plugins/input/tcp/src/Config.cpp | 105 +++++++++++++++++++++++++-- src/plugins/input/tcp/src/Config.hpp | 29 +++++++- 2 files changed, 128 insertions(+), 6 deletions(-) diff --git a/src/plugins/input/tcp/src/Config.cpp b/src/plugins/input/tcp/src/Config.cpp index a5d2448c..4da4e321 100644 --- a/src/plugins/input/tcp/src/Config.cpp +++ b/src/plugins/input/tcp/src/Config.cpp @@ -15,6 +15,7 @@ #include // string #include // assert #include // UINT16_MAX +#include #include // fds_*, FDS_* @@ -30,12 +31,33 @@ namespace tcp_in { * * ... * ... + * ... * */ enum ParamsXmlNodes { PARAM_PORT, PARAM_IPADDR, + PARAM_TLS, + + PARAM_TLS_CERTIFICATE, + PARAM_TLS_PRIVATE_KEY, + PARAM_TLS_VERIFY_PEER, + PARAM_TLS_CA_FILE, + PARAM_TLS_CA_DIR, + PARAM_TLS_CA_STORE, + PARAM_TLS_INSECURE, +}; + +static const struct fds_xml_args args_tls[] = { + FDS_OPTS_ELEM(PARAM_TLS_CERTIFICATE, "certificateFile", FDS_OPTS_T_STRING, 0), + FDS_OPTS_ELEM(PARAM_TLS_PRIVATE_KEY, "privateKeyFile" , FDS_OPTS_T_STRING, FDS_OPTS_P_OPT), + FDS_OPTS_ELEM(PARAM_TLS_VERIFY_PEER, "verifyPeer" , FDS_OPTS_T_BOOL , FDS_OPTS_P_OPT), + FDS_OPTS_ELEM(PARAM_TLS_CA_FILE , "caFile" , FDS_OPTS_T_STRING, FDS_OPTS_P_OPT), + FDS_OPTS_ELEM(PARAM_TLS_CA_DIR , "caDir" , FDS_OPTS_T_STRING, FDS_OPTS_P_OPT), + FDS_OPTS_ELEM(PARAM_TLS_CA_STORE , "caStore" , FDS_OPTS_T_STRING, FDS_OPTS_P_OPT), + FDS_OPTS_ELEM(PARAM_TLS_INSECURE , "allowInsecure" , FDS_OPTS_T_BOOL , FDS_OPTS_P_OPT), + FDS_OPTS_END, }; static const struct fds_xml_args args_params[] = { @@ -43,6 +65,7 @@ static const struct fds_xml_args args_params[] = { FDS_OPTS_ELEM(PARAM_PORT , "localPort" , FDS_OPTS_T_UINT , FDS_OPTS_P_OPT), FDS_OPTS_ELEM(PARAM_IPADDR, "localIPAddress", FDS_OPTS_T_STRING, FDS_OPTS_P_OPT | FDS_OPTS_P_MULTI), + FDS_OPTS_NESTED(PARAM_TLS , "tls" , args_tls , FDS_OPTS_P_OPT), FDS_OPTS_END, }; @@ -85,13 +108,17 @@ void Config::parse_params(ipx_ctx *ctx, fds_xml_ctx_t *params) { break; case PARAM_IPADDR: assert(content->type == FDS_OPTS_T_STRING); - // check if the string is not empty - if (*content->ptr_string) { - local_addrs.push_back(IpAddress(content->ptr_string)); - } else { + // check if the string is empty + if (std::strcmp(content->ptr_string, "") == 0) { empty_address = true; + } else { + local_addrs.push_back(IpAddress(content->ptr_string)); } break; + case PARAM_TLS: + assert(content->type == FDS_OPTS_T_CONTEXT); + parse_tls(ctx, content->ptr_ctx); + break; default: throw std::invalid_argument("Unexpected element within ."); } @@ -100,11 +127,79 @@ void Config::parse_params(ipx_ctx *ctx, fds_xml_ctx_t *params) { if (empty_address && local_addrs.size() != 0) { IPX_CTX_WARNING( ctx, - "Empty address in configuration ignored. Tcp plugin will NOT " + "Empty address in configuration ignored. TCP plugin will NOT " "listen on all interfaces but only on the specified addresses." ); } } +void Config::parse_tls(ipx_ctx *ctx, fds_xml_ctx_t *params) { + const struct fds_xml_cont *content; + bool empty_private_key; + + // The default when TCP is enabled. + allow_insecure = false; + + while (fds_xml_next(params, &content) != FDS_EOC) { + switch (content->id) { + case PARAM_TLS_CERTIFICATE: + assert(content->type == FDS_OPTS_T_STRING); + // check if the string is empty + if (std::strcmp(content->ptr_string, "") == 0) { + throw std::invalid_argument("TLS certificate path must not be empty."); + } else { + certificate_file = content->ptr_string; + } + break; + case PARAM_TLS_PRIVATE_KEY: + assert(content->type == FDS_OPTS_T_STRING); + empty_private_key = std::strcmp(content->ptr_string, "") == 0; + private_key_file = content->ptr_string; + break; + case PARAM_TLS_VERIFY_PEER: + assert(content->type == FDS_OPTS_T_BOOL); + verify_peer = content->val_bool; + break; + case PARAM_TLS_CA_FILE: + assert(content->type == FDS_OPTS_T_STRING); + default_ca_file = std::strcmp(content->ptr_string, "") == 0; + ca_file = content->ptr_string; + break; + case PARAM_TLS_CA_DIR: + assert(content->type == FDS_OPTS_T_STRING); + default_ca_dir = std::strcmp(content->ptr_string, "") == 0; + ca_dir = content->ptr_string; + break; + case PARAM_TLS_CA_STORE: + assert(content->type == FDS_OPTS_T_STRING); + default_ca_store = std::strcmp(content->ptr_string, "") == 0; + ca_store = content->ptr_string; + break; + case PARAM_TLS_INSECURE: + assert(content->type == FDS_OPTS_T_BOOL); + allow_insecure = content->val_bool; + break; + default: + throw std::invalid_argument("Unexpected element within ."); + } + } + + if (empty_private_key) { + IPX_CTX_WARNING( + ctx, + "Empty private key ignored. Ipfixcol will use the same file as " + "certificate." + ); + } + + if (private_key_file.empty()) { + private_key_file = certificate_file; + } + + use_default_ca = !default_ca_file && ca_file.empty() + && !default_ca_dir && ca_dir.empty() + && !default_ca_store && ca_store.empty(); +} + } // namespace tcp_in diff --git a/src/plugins/input/tcp/src/Config.hpp b/src/plugins/input/tcp/src/Config.hpp index 2d1a2c9a..a90537ff 100644 --- a/src/plugins/input/tcp/src/Config.hpp +++ b/src/plugins/input/tcp/src/Config.hpp @@ -10,8 +10,9 @@ #pragma once -#include // std::vector #include // uint16_t +#include +#include // std::vector #include // fds_xml_ctx_t @@ -25,6 +26,31 @@ namespace tcp_in { struct Config { uint16_t local_port; std::vector local_addrs; + /** + * @brief Path to file in pem format which contains certificate and private key for TLS. If + * empty, TLS connections are not accepted. + */ + std::string certificate_file; + /** Path to private key. */ + std::string private_key_file; + /** If true, ipfixcol server will require certificate verification of clients */ + bool verify_peer = false; + /** Path to certificate authority. */ + std::string ca_file; + /** If true, load default ca file. */ + bool default_ca_file = false; + /** Path to certificate authority directory. */ + std::string ca_dir; + /** If true, load default ca directory. */ + bool default_ca_dir = false; + /** Path to certificate authority store. */ + std::string ca_store; + /** If true, load default ca store. */ + bool default_ca_store; + /** If set, ignore any other ca configuration paths and use system defaults. */ + bool use_default_ca = true; + /** If set to false, only TCP connections are accepted. */ + bool allow_insecure = true; /** * @brief Parse configuration of the TCP plugin @@ -36,6 +62,7 @@ struct Config { private: void parse_params(ipx_ctx *ctx, fds_xml_ctx_t *params); + void parse_tls(ipx_ctx *ctx, fds_xml_ctx_t *params); }; } // namespace tcp_in From b8a692061d45ebf558fabaeb469018a32e84c20b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Thu, 6 Mar 2025 13:53:02 +0100 Subject: [PATCH 12/19] TCP input TLS - Use TlsDecoder in DecoderFactory. --- src/plugins/input/tcp/src/ClientManager.hpp | 7 +++ src/plugins/input/tcp/src/DecoderFactory.cpp | 45 ++++++++++++++++++-- src/plugins/input/tcp/src/DecoderFactory.hpp | 16 ++++++- src/plugins/input/tcp/src/IpxPlugin.cpp | 1 + src/plugins/input/tcp/src/Plugin.cpp | 2 +- src/plugins/input/tcp/src/Plugin.hpp | 7 +++ 6 files changed, 72 insertions(+), 6 deletions(-) diff --git a/src/plugins/input/tcp/src/ClientManager.hpp b/src/plugins/input/tcp/src/ClientManager.hpp index 2395376b..7793a10c 100644 --- a/src/plugins/input/tcp/src/ClientManager.hpp +++ b/src/plugins/input/tcp/src/ClientManager.hpp @@ -33,6 +33,13 @@ class ClientManager { */ ClientManager(ipx_ctx_t *ctx, DecoderFactory factory); + /** + * @brief Initialize TLS. This is separately from constructor because it may prompt the user for + * password for private key. + * @param conf Configuration for the initialization. + */ + void initialize_tls(const Config &conf) { m_factory.initialize_tls(conf); } + /** * @brief Adds connection to the vector and epoll. * @param fd file descriptor of the new tcp connection. diff --git a/src/plugins/input/tcp/src/DecoderFactory.cpp b/src/plugins/input/tcp/src/DecoderFactory.cpp index a04643c4..13035755 100644 --- a/src/plugins/input/tcp/src/DecoderFactory.cpp +++ b/src/plugins/input/tcp/src/DecoderFactory.cpp @@ -26,12 +26,15 @@ #include "Decoder.hpp" // Decoder #include "Lz4Decoder.hpp" // LZ4_MAGIC, Lz4Decoder #include "IpfixDecoder.hpp" // IPFIX_MAGIC, IpfixDecoder +#include "tls/TlsDecoder.hpp" #include namespace tcp_in { -DecoderFactory::DecoderFactory() {}; +DecoderFactory::DecoderFactory(ipx_ctx_t *ctx, const Config &conf) : m_ctx(ctx), m_allow_insecure(conf.allow_insecure) { + // TLS is initialized separately because it may prompt the user. +}; std::unique_ptr DecoderFactory::detect_decoder(int fd) { // number of bytes neaded to detect the decoder @@ -48,7 +51,9 @@ std::unique_ptr DecoderFactory::detect_decoder(int fd) { if (res == -1) { const char *err_msg; ipx_strerror(errno, err_msg); - throw std::runtime_error("Failed to receive start of first message: " + std::string(err_msg)); + throw std::runtime_error( + "Failed to receive start of first message: " + std::string(err_msg) + ); } constexpr const char *not_enough_data_err = @@ -56,6 +61,16 @@ std::unique_ptr DecoderFactory::detect_decoder(int fd) { // check decoders in order from shortest magic number to longest + if (res < 1) { + throw std::runtime_error(not_enough_data_err); + } + + // TLS decoder + auto magic_u8 = buf[0]; + if (magic_u8 == tls::TLS_MAGIC) { + return create_tls_decoder(fd); + } + if (res < 2) { throw std::runtime_error(not_enough_data_err); } @@ -79,12 +94,34 @@ std::unique_ptr DecoderFactory::detect_decoder(int fd) { throw std::runtime_error("Failed to recognize the decoder."); } +void DecoderFactory::initialize_tls(const Config &conf) { + if (!conf.certificate_file.empty()) { + IPX_CTX_INFO(m_ctx, "Initializing TLS decoder."); + m_tls_factory = std::unique_ptr(new tls::DecoderFactory(conf)); + } else { + IPX_CTX_INFO(m_ctx, "TLS Decoder is disabled."); + } +} + std::unique_ptr DecoderFactory::create_ipfix_decoder(int fd) { - return std::unique_ptr(new IpfixDecoder(fd)); + if (m_allow_insecure) { + return std::unique_ptr(new IpfixDecoder(fd)); + } + throw std::runtime_error("Insecure connection using IPFIX decoder refused."); } std::unique_ptr DecoderFactory::create_lz4_decoder(int fd) { - return std::unique_ptr(new Lz4Decoder(fd)); + if (m_allow_insecure) { + return std::unique_ptr(new Lz4Decoder(fd)); + } + throw std::runtime_error("Insecure connection using LZ4 decoder refused."); +} + +std::unique_ptr DecoderFactory::create_tls_decoder(int fd) { + if (!m_tls_factory) { + throw std::runtime_error("TLS decoder is not enabled."); + } + return m_tls_factory->create(fd); } } // namespace tcp_in diff --git a/src/plugins/input/tcp/src/DecoderFactory.hpp b/src/plugins/input/tcp/src/DecoderFactory.hpp index 758715eb..82a3bd4b 100644 --- a/src/plugins/input/tcp/src/DecoderFactory.hpp +++ b/src/plugins/input/tcp/src/DecoderFactory.hpp @@ -12,14 +12,16 @@ #include // std::unique_ptr +#include "Config.hpp" #include "Decoder.hpp" // Decoder +#include "tls/DecoderFactory.hpp" namespace tcp_in { /** Factory for TCP decoders. */ class DecoderFactory { public: - DecoderFactory(); + DecoderFactory(ipx_ctx_t *ctx, const Config &conf); /** * @brief Detects the type of decoder that should be used to decode the given stream and @@ -31,9 +33,21 @@ class DecoderFactory { */ std::unique_ptr detect_decoder(int fd); + /** + * @brief Initialize TLS. This is separately from constructor because it may prompt the user for + * password for private key. + * @param conf Configuration for the initialization. + */ + void initialize_tls(const Config &conf); + private: std::unique_ptr create_ipfix_decoder(int fd); std::unique_ptr create_lz4_decoder(int fd); + std::unique_ptr create_tls_decoder(int fd); + + ipx_ctx_t *m_ctx; + std::unique_ptr m_tls_factory; + bool m_allow_insecure; }; } // namespace tcp_in diff --git a/src/plugins/input/tcp/src/IpxPlugin.cpp b/src/plugins/input/tcp/src/IpxPlugin.cpp index 0a29133d..1e96c1f7 100644 --- a/src/plugins/input/tcp/src/IpxPlugin.cpp +++ b/src/plugins/input/tcp/src/IpxPlugin.cpp @@ -38,6 +38,7 @@ int ipx_plugin_init(ipx_ctx_t *ctx, const char *params) { try { Config conf(ctx, params); plugin = new Plugin(ctx, conf); + plugin->initialize_tls(conf); // This may prompt the user for password for private key. } catch (std::exception &ex) { IPX_CTX_ERROR(ctx, "%s", ex.what()); return IPX_ERR_DENIED; diff --git a/src/plugins/input/tcp/src/Plugin.cpp b/src/plugins/input/tcp/src/Plugin.cpp index ab85bac9..f7f7cc37 100644 --- a/src/plugins/input/tcp/src/Plugin.cpp +++ b/src/plugins/input/tcp/src/Plugin.cpp @@ -24,7 +24,7 @@ namespace tcp_in { Plugin::Plugin(ipx_ctx_t *ctx, Config &config) : m_ctx(ctx), - m_clients(ctx, DecoderFactory()), + m_clients(ctx, DecoderFactory(ctx, config)), m_acceptor(m_clients, ctx) { m_acceptor.bind_addresses(config); diff --git a/src/plugins/input/tcp/src/Plugin.hpp b/src/plugins/input/tcp/src/Plugin.hpp index 6d787c37..a06434e0 100644 --- a/src/plugins/input/tcp/src/Plugin.hpp +++ b/src/plugins/input/tcp/src/Plugin.hpp @@ -30,6 +30,13 @@ class Plugin { */ Plugin(ipx_ctx_t *ctx, Config &config); + /** + * @brief Initialize TLS. This is separately from constructor because it may prompt the user for + * password for private key. + * @param conf Configuration for the initialization. + */ + void initialize_tls(const Config &conf) { m_clients.initialize_tls(conf); } + // force that Plugin stays in its original memory (so that reference to `m_clients` in acceptor // stays valid) Plugin(const Plugin &) = delete; From 17fd3e918656310a47c5c22f49627f17e16e8c91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Thu, 6 Mar 2025 13:53:20 +0100 Subject: [PATCH 13/19] TCP input TLS - Update README. --- src/plugins/input/tcp/README.rst | 151 +++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/src/plugins/input/tcp/README.rst b/src/plugins/input/tcp/README.rst index 52356a4c..4ff62459 100644 --- a/src/plugins/input/tcp/README.rst +++ b/src/plugins/input/tcp/README.rst @@ -12,6 +12,9 @@ initial period of inability to interpret flow records does not apply here. Received IPFIX messages may be compressed using LZ4 stream compression. The compression is enabled on the exporter and the plugin detects it automatically. +Received IPFIX messages may be secured with TLS. The TLS decoder is enabled by specifying the +certificateFile parameter. Inbound TLS messages are automatically detected. + Example configuration --------------------- @@ -23,6 +26,14 @@ Example configuration 4739 + @@ -39,7 +50,147 @@ Mandatory parameters: multiple times (one IP address per occurrence) to manually select multiple interfaces. [default: empty] +Optional parameters: + +:``tls``: + Contains TLS configuration. When this is present, all insecure connections are refused by + default. This can be changed with the parameter ``allowInsecure`` inside of the ``tls`` block. + +Tls configuration +----------------- + +All paths may be absolute or relative to the cwd of ipfixcol2. + +Mandatory parameters: + +:``certificateFile``: + Path to a PEM file with certificate. + +:``privateKeyFile``: + Path to a PEM file with private key. If this is not present, ``certificateFile`` is expected to + also contain the private key. [default: same as ``certificateFile``] + +:``allowInsecure``: + Boolean value. If it is set to true, non-tls connections will be accepted. Otherwise only tls + connections will be accepted. [default: false] + +:``verifyPeer``: + Boolean value. If true, ipfixcol as TLS server will verify its clients certificates. Trusted + certificates are set with the following parameters. [default: false] + +The following parameters will set the trusted certificates. If the parameter is not present, the +given source type of trusted certificates is not used. If the value is empty, the default is used. +If none of the parameters are present, the defaults will be used for all. Specifying trusted +certificates (certificate authority) is relevant only if the option ``verifyPeer`` is set to true. + +:``caFile``: + Path to file with certificate authority. Default uses value from environment variable + ``SSL_CERT_FILE`` or uses the system defaults. + +:``caDir``: + Path to directory with trusted certificates (certificate authorities). Default uses value from + environment variable ``SSL_CERT_DIR`` or the system defaults. + +:``caStore``: + Path to certificate store with trusted certificates (certificate authorities). Default uses + system defaults. + Notes ----- The LZ4 compression uses special format that compatible with `ipfixproble `. + +How to create certificates +-------------------------- + +Here I describe how to create custom certificates and certificate authorities using +`easyrsa `. This can be done on single machine or two machines +where one is the CA and other has certificate signed by CA. + +Go to the path where you want to create the certificate infrastructure. + +Download easyrsa and make it executable. This can be done with on both of the machines: + +.. code-block:: sh + + wget https://raw.githubusercontent.com/OpenVPN/easy-rsa/refs/heads/master/easyrsa3/easyrsa + chmod +x easyrsa + +Initialize easyrsa. This will create folder ``pki`` in the current directory with the +infrastructure. This is done on both the system that requests the certificate, and system that signs +the certificate. In case of one system, do this only once. + +.. code-block:: sh + + ./easyrsa init-pki + +Create new certificate authority. This will ask you for name of the authority and new password. The +certificate authority will be created at ``pki/ca.crt``. This is done on the system that creates the +certificate. + +.. code-block:: sh + + ./easyrsa build-ca + +Create certificate request with a name (``Name``). This is done on the system that requests the +certificate. Often the certificate is used on server which may be addressed in multiple ways. The +certificate needs to be signed for all these names. This is done with the parameter ``--san`` +(subject alternative names). The parameter is list of names separated with comma. Each name must +have type. The type may be for example ``IP`` for ip address or ``DNS`` for dns name. Other types +exist. In this example I will set the san to localhost ip address. Creating the certificate request +will ask you for a new password and name of the cretificate. You can leave the name as default and +just press enter. The request file will be created at ``pki/reqs/Name.req`` and the private key will +be created at ``pki/private/Name.key``. + +.. code-block:: sh + + ./easyrsa --san='IP:127.0.0.1' gen-req Name + +If the ca system and system requesting certificate are different, copy the request file to the CA +system and import it with this step. If both systems are the same, you can skip this step. + +.. code-block:: sh + + ./easyrsa import-req path/to/request/Name.req Name + +Now you can sign the request on the server with the CA. The request is signed for specific use +either as server or client. In this example I will create certificate for server. The option +``--copy-ext`` will make sure to copy the SANs. This will ask you for confirmation and than password +for the CA. If you want to create certificate for client, use ``client`` instead of ``server``. The +certificate will be created at ``pki/issued/Name.crt``. + +.. code-block:: sh + + ./easyrsa --copy-ext sign-req server Name + +So right now the important files are on the machines: + +:CA machine: + - ``pki/ca.crt`` - certificate of certificate authority. + - ``pki/issued/Name.crt`` - certificate for the other machine. + +:other machine: + - ``pki/private/Name.key`` - private key for this machine certificate. + +The certificate for the other machine has to be transfered to the other machine. It is no secret so +it doesn't require secure connection. Before you do that, it is good to add the CA certificate to +the certificate of the other machine so that it may send it alongside with its certificate. To do +that, you can just append the contents of ``pki/ca.crt`` to ``pki/issued/Name.crt``. Make sure that +the CA certificate is the second certificate in that file. Now you are free to transfer +``pki/issued/Name.crt`` to the other machine and use it there alongside with its key. + +**Inline files:** + +The last step also created inline files which may be directly used. Inline file contains the +information in more human readable way alongside with the data itself. The inline files differ +depending on whether you used single machine or multiple machines. + +If you used single machine, there will be file ``pki/inline/private/Name.inline``. This file +contains both the machine certificate and CA certificate in correct order and also the private key. +It may be used in place of the certificate file and also in place of the private key file. You don't +have to copy the CA certificate into this file, because it is already here. + +If you used multiple machines, there will be file ``pki/inline/Name.inline`` that contains the +machine certificate and CA certificate. The private key is not in this file, because the CA machine +doesn't have it. You can transfer this onto the other machine instead of the crt file and use it in +its place. You don't have to copy the CA certificate into this file, because it is already there. From af8ed1fef910030941597645705f608b209f1dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Tue, 11 Mar 2025 12:38:45 +0100 Subject: [PATCH 14/19] Support TLS in ipfixsend --- src/tools/ipfixsend/CMakeLists.txt | 4 +- src/tools/ipfixsend/ipfixsend.c | 29 ++++- src/tools/ipfixsend/siso.c | 187 ++++++++++++++++++++++++++++- src/tools/ipfixsend/siso.h | 13 +- 4 files changed, 223 insertions(+), 10 deletions(-) diff --git a/src/tools/ipfixsend/CMakeLists.txt b/src/tools/ipfixsend/CMakeLists.txt index f30d1a73..f8beeae4 100644 --- a/src/tools/ipfixsend/CMakeLists.txt +++ b/src/tools/ipfixsend/CMakeLists.txt @@ -8,8 +8,10 @@ add_executable(ipfixsend2 siso.h ) +target_link_libraries(ipfixsend2 OpenSSL::Crypto OpenSSL::SSL) + # Installation targets install( TARGETS ipfixsend2 DESTINATION bin -) \ No newline at end of file +) diff --git a/src/tools/ipfixsend/ipfixsend.c b/src/tools/ipfixsend/ipfixsend.c index 6a3a2aaf..99135d0e 100644 --- a/src/tools/ipfixsend/ipfixsend.c +++ b/src/tools/ipfixsend/ipfixsend.c @@ -81,7 +81,9 @@ void usage() printf(" -i path IPFIX input file\n"); printf(" -d ip Destination IP address (default: %s)\n", DEFAULT_IP); printf(" -p port Destination port number (default: %s)\n", DEFAULT_PORT); - printf(" -t type Connection type (UDP or TCP) (default: UDP)\n"); + printf(" -t type Connection type (UDP/TCP/SCTP/TLS) (default: UDP)\n"); + printf(" When using TLS you may want to specify trusted certificate with\n"); + printf(" environment variable SSL_CERT_FILE or SSL_CERT_DIR.\n"); printf(" -c Precache input file (for performance tests)\n"); printf(" -n num How many times the file should be sent (default: infinity)\n"); printf(" -s speed Maximum data sending speed/s\n"); @@ -90,6 +92,8 @@ void usage() printf(" -R num Real-time sending\n"); printf(" Allow speed-up sending 'num' times (realtime: 1.0)\n"); printf(" -O num Rewrite Observation Domain ID (ODID)\n"); + printf(" -C path Set path to certificate/private key for TLS verification.\n"); + printf(" This is required only if the server requires clients to verify.\n"); printf("\n"); } @@ -125,6 +129,8 @@ int main(int argc, char** argv) bool odid_rewrite = false; long odid_new; + char *certificate = NULL; + if (argc == 1) { usage(); return 0; @@ -132,7 +138,7 @@ int main(int argc, char** argv) // Parse parameters int c; - while ((c = getopt(argc, argv, "hci:d:p:t:n:s:S:R:O:")) != -1) { + while ((c = getopt(argc, argv, "hci:d:p:t:n:s:S:R:O:C:")) != -1) { switch (c) { case 'h': usage(); @@ -168,6 +174,9 @@ int main(int argc, char** argv) odid_rewrite = true; odid_new = atol(optarg); break; + case 'C': + certificate = optarg; + break; default: fprintf(stderr, "Unknown option.\n"); return 1; @@ -202,6 +211,15 @@ int main(int argc, char** argv) return 1; } + if (certificate && strcmp(type, "TLS") != 0) { + fprintf( + stderr, + "Certificate path is valid only with type TLS, but the type is %s.\n", + type + ); + return 1; + } + // Check whether everything is set if (!input) { fprintf(stderr, "Input file must be set!\n"); @@ -217,6 +235,13 @@ int main(int argc, char** argv) return 1; } + // Load certificate and private key + if (certificate && siso_load_cert(sender, certificate) != SISO_OK) { + fprintf(stderr, "Failed to load certificate file: %s\n", siso_get_last_err(sender)); + siso_destroy(sender); + return 1; + } + // Prepare an input file reader_t *reader = reader_create(input, precache); if (!reader) { diff --git a/src/tools/ipfixsend/siso.c b/src/tools/ipfixsend/siso.c index 42513940..4e6ca43b 100644 --- a/src/tools/ipfixsend/siso.c +++ b/src/tools/ipfixsend/siso.c @@ -41,6 +41,8 @@ #include #include +#include +#include #include #include @@ -57,6 +59,9 @@ #include #include +#include +#include + /** * \brief Check that a pointer is not null. Otherwise returns #SISO_ERR * \param[in] ptr Pointer to check @@ -71,6 +76,14 @@ * \brief Get the lass error message */ #define PERROR_LAST strerror(errno) +/** + * @brief Get the last global OpenSSL error message + */ +#define TLS_LAST_ERROR tls_code_error(ERR_get_error()) +/** + * @brief Get the last OpenSSL error message for SSL connection. + */ +#define TLS_LAST_SSL_ERROR(ssl, ret) tls_ssl_code_error(SSL_get_error((ssl), (ret))) /** Maximum message size that can be send over UDP */ #define SISO_UDP_MAX 65000 /** @@ -86,6 +99,7 @@ enum siso_conn_type { SC_UDP, SC_TCP, SC_SCTP, + SC_TLS, SC_UNKNOWN, }; @@ -109,26 +123,32 @@ static const char *siso_messages[] = { static const char *siso_sc_types[] = { [SC_UDP] = "UDP", [SC_TCP] = "TCP", - [SC_SCTP] = "SCTP" + [SC_SCTP] = "SCTP", + [SC_TLS] = "TLS", }; +/** Buffer for error messages. */ +static char sisco_err_msg_buf[256] = { 0 }; + /** * \brief Main sisolib structure */ struct sisoconf_s { const char *last_error; /**< last error message */ - enum siso_conn_type type; /**< UDP/TCP/SCTP */ + enum siso_conn_type type; /**< UDP/TCP/SCTP/TLS */ struct addrinfo *servinfo; /**< server information */ int sockfd; /**< socket descriptor */ uint64_t max_speed; /**< max sending speed */ uint64_t act_speed; /**< actual speed */ struct timeval begin, end; /**< start/end time for limited transfers */ + SSL_CTX *ssl_ctx; /**< context for creating TLS connecitons */ + SSL *ssl; /**< TLS connection */ }; /** * \brief Constructor */ -sisoconf *siso_create() +sisoconf *siso_create(void) { /* allocate memory */ sisoconf *conf = (sisoconf *) calloc(1, sizeof(sisoconf)); @@ -154,6 +174,11 @@ void siso_destroy(sisoconf *conf) freeaddrinfo(conf->servinfo); } + if (conf->ssl_ctx) { + SSL_CTX_free(conf->ssl_ctx); + conf->ssl_ctx = NULL; + } + free(conf); } @@ -256,6 +281,7 @@ int siso_getaddrinfo(sisoconf *conf, const char *ip, const char *port) hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; break; + case SC_TLS: case SC_TCP: hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; @@ -281,6 +307,129 @@ int siso_getaddrinfo(sisoconf *conf, const char *ip, const char *port) return SISO_OK; } +/** + * @brief Get OpenSSL error message for the given code + */ +static inline const char *tls_code_error(unsigned long code) +{ + if (code == SSL_ERROR_SYSCALL) { + return PERROR_LAST; + } + ERR_error_string_n(code, sisco_err_msg_buf, sizeof(sisco_err_msg_buf)); + return &sisco_err_msg_buf[0]; +} + +/** + * @brief Get OpenSSL error message for the given code from SSL connection. + */ +static inline const char *tls_ssl_code_error(unsigned long code) +{ + if (code == SSL_ERROR_SYSCALL) { + code = ERR_get_error(); + } + return tls_code_error(code); +} + +/** + * @brief Get SSL_CTX. Initialize it if it is not initialized. + * @return NULL on failure. + */ +SSL_CTX *siso_get_ssl_ctx(sisoconf *conf) +{ + if (conf->ssl_ctx) { + return conf->ssl_ctx; + } + + SSL_load_error_strings(); + + SSL_CTX *ctx = SSL_CTX_new(TLS_client_method()); + if (!ctx) { + conf->last_error = TLS_LAST_ERROR; + return NULL; + } + + SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); + + if (!SSL_CTX_set_default_verify_paths(ctx)) { + conf->last_error = TLS_LAST_ERROR; + SSL_CTX_free(ctx); + return NULL; + } + + if (!SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION)) { + conf->last_error = TLS_LAST_ERROR; + SSL_CTX_free(ctx); + return NULL; + } + + SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE); + + // There is no way to pass MSG_SIGIGN when writing using OpenSSL without + // creating custom BIO. So we can just ignore SIGPIPE which can occur if + // peer closes the file descriptor. + signal(SIGPIPE, SIG_IGN); + + return conf->ssl_ctx = ctx; +} + +int siso_load_cert(sisoconf *conf, const char *cert_file) { + SSL_CTX *ctx = siso_get_ssl_ctx(conf); + + if (SSL_CTX_use_certificate_chain_file(ctx, cert_file) <= 0) { + conf->last_error = TLS_LAST_ERROR; + return SISO_ERR; + } + + if (SSL_CTX_use_PrivateKey_file(ctx, cert_file, SSL_FILETYPE_PEM) <= 0) { + conf->last_error = TLS_LAST_ERROR; + return SISO_ERR; + } + + return SISO_OK; +} + +int siso_tls_connect(sisoconf *conf, int new_fd) +{ + assert(!conf->ssl); + + SSL_CTX *ctx = siso_get_ssl_ctx(conf); + if (!ctx) { + return SISO_ERR; + } + + SSL *ssl = SSL_new(ctx); + if (!ssl) { + conf->last_error = TLS_LAST_ERROR; + return SISO_ERR; + } + + BIO *bio = BIO_new(BIO_s_socket()); + if (!bio) { + conf->last_error = TLS_LAST_ERROR; + SSL_free(ssl); + return SISO_ERR; + } + + BIO_set_fd(bio, new_fd, BIO_NOCLOSE); + SSL_set_bio(ssl, bio, bio); + + int res = SSL_connect(ssl); + if (res <= 0) { + // Get the best error message. + long vres = SSL_get_verify_result(ssl); + if (vres == X509_V_OK) { + conf->last_error = TLS_LAST_SSL_ERROR(ssl, res); + } else { + conf->last_error = X509_verify_cert_error_string(vres); + } + SSL_free(ssl); + return SISO_ERR; + } + + conf->ssl = ssl; + return SISO_OK; +} + /** * \brief Create new socket */ @@ -309,6 +458,11 @@ int siso_create_socket(sisoconf *conf) return SISO_ERR; } + if (conf->type == SC_TLS && siso_tls_connect(conf, new_fd) != SISO_OK) { + close(new_fd); + return SISO_ERR; + } + conf->sockfd = new_fd; return SISO_OK; } @@ -345,7 +499,16 @@ int siso_create_connection(sisoconf* conf, const char* ip, const char *port, con */ void siso_close_connection(sisoconf *conf) { - if (conf && conf->sockfd > 0) { + if (!conf) { + return; + } + + if (conf->ssl) { + SSL_free(conf->ssl); + conf->ssl = NULL; + } + + if (conf->sockfd > 0) { close(conf->sockfd); conf->sockfd = -1; } @@ -375,6 +538,7 @@ int siso_send(sisoconf *conf, const char *data, ssize_t length) // data sent per cycle ssize_t sent_now = 0; + int ret_code = 1; // Size of remaining data ssize_t todo = length; @@ -390,12 +554,25 @@ int siso_send(sisoconf *conf, const char *data, ssize_t length) case SC_SCTP: sent_now = send(conf->sockfd, ptr, todo, MSG_NOSIGNAL); break; + case SC_TLS: + ret_code = SSL_write_ex(conf->ssl, ptr, todo, (size_t *)&sent_now); + break; default: break; } // Check for errors - if (sent_now == -1) { + if (ret_code <= 0) { + int err_code = SSL_get_error(conf->ssl, ret_code); + if (err_code != SSL_ERROR_WANT_READ && err_code != SSL_ERROR_WANT_WRITE) { + // Connection is broken, close... + conf->last_error = tls_ssl_code_error(err_code); + siso_close_connection(conf); + return SISO_ERR; + } + + // Don't use continue here, the write may be partial. + } else if (sent_now == -1) { if (errno != EAGAIN && errno != EWOULDBLOCK) { // Connection broken, close... conf->last_error = PERROR_LAST; diff --git a/src/tools/ipfixsend/siso.h b/src/tools/ipfixsend/siso.h index bf8e7b89..5684668e 100644 --- a/src/tools/ipfixsend/siso.h +++ b/src/tools/ipfixsend/siso.h @@ -78,7 +78,7 @@ enum siso_units { * * \return new sisoconf object */ -sisoconf *siso_create(); +sisoconf *siso_create(void); /** * \brief Destructor @@ -151,6 +151,16 @@ void siso_set_speed(sisoconf* conf, uint32_t limit, enum siso_units units); */ void siso_set_speed_str(sisoconf* conf, const char* limit); +/** + * \brief Load certificate and private key for TLS conections. This must be called before + * `siso_create_connection` or not at all. + * + * \param[in] conf susoconf configuration. + * \param[in] cert_file path to file with certificate and private key in PEM format. + * \return #SISO_OK or #SISO_ERR and sets error message. + */ +int siso_load_cert(sisoconf *conf, const char *cert_file); + /** * \brief Create new connection * @@ -199,4 +209,3 @@ int siso_send(sisoconf *conf, const char *data, ssize_t length); #endif #endif /* SISO_H */ - From 010e1cf5ffbe299c0cd2dd04d9fbdbc720137174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Thu, 17 Apr 2025 10:35:05 +0200 Subject: [PATCH 15/19] TCP - split decoder API to `progress` and `buffer`. This may be more intuituve to understand. --- src/plugins/input/tcp/src/Connection.cpp | 3 ++- src/plugins/input/tcp/src/Decoder.hpp | 9 ++++++--- src/plugins/input/tcp/src/IpfixDecoder.cpp | 4 +--- src/plugins/input/tcp/src/IpfixDecoder.hpp | 4 +++- src/plugins/input/tcp/src/Lz4Decoder.cpp | 4 +--- src/plugins/input/tcp/src/Lz4Decoder.hpp | 5 +++-- src/plugins/input/tcp/src/tls/TlsDecoder.cpp | 10 ++++------ src/plugins/input/tcp/src/tls/TlsDecoder.hpp | 5 +++-- 8 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/plugins/input/tcp/src/Connection.cpp b/src/plugins/input/tcp/src/Connection.cpp index 3b6b429f..ba9750c3 100644 --- a/src/plugins/input/tcp/src/Connection.cpp +++ b/src/plugins/input/tcp/src/Connection.cpp @@ -105,7 +105,8 @@ bool Connection::receive(ipx_ctx_t *ctx) { ); } - auto &buffer = m_decoder->decode(); + m_decoder->progress(); + auto &buffer = m_decoder->buffer(); buffer.process_decoded([=](ByteVector &&msg) { send_msg(ctx, std::move(msg)); }); return !buffer.is_eof_reached(); } diff --git a/src/plugins/input/tcp/src/Decoder.hpp b/src/plugins/input/tcp/src/Decoder.hpp index 5d8b1a7c..1acbef21 100644 --- a/src/plugins/input/tcp/src/Decoder.hpp +++ b/src/plugins/input/tcp/src/Decoder.hpp @@ -17,11 +17,14 @@ namespace tcp_in { /** Interface for IPFIX message decoders. */ class Decoder { public: + /** Reads all available data from TCP stream and returns buffer with decoded messages. */ + virtual void progress() = 0; + /** - * @brief Reads all available data from TCP stream and returns buffer with decoded messages. - * @returns Buffer with decoded messages. + * @brief Retrieves decode buffer with decoded data. + * @return Buffer with decoded data. */ - virtual DecodeBuffer &decode() = 0; + virtual DecodeBuffer &buffer() = 0; /** Gets the name of the decoder. */ virtual const char *get_name() const = 0; diff --git a/src/plugins/input/tcp/src/IpfixDecoder.cpp b/src/plugins/input/tcp/src/IpfixDecoder.cpp index 3e135b02..3753d77b 100644 --- a/src/plugins/input/tcp/src/IpfixDecoder.cpp +++ b/src/plugins/input/tcp/src/IpfixDecoder.cpp @@ -24,7 +24,7 @@ namespace tcp_in { -DecodeBuffer &IpfixDecoder::decode() { +void IpfixDecoder::progress() { while (!m_decoded.enough_data()) { // Read the header of the message. if (!read_header()) { @@ -42,8 +42,6 @@ DecodeBuffer &IpfixDecoder::decode() { if (m_decoded.is_eof_reached() && m_part_readed.size() != 0) { throw std::runtime_error("Received incomplete message."); } - - return m_decoded; } bool IpfixDecoder::read_header() { diff --git a/src/plugins/input/tcp/src/IpfixDecoder.hpp b/src/plugins/input/tcp/src/IpfixDecoder.hpp index 405079b4..333b2946 100644 --- a/src/plugins/input/tcp/src/IpfixDecoder.hpp +++ b/src/plugins/input/tcp/src/IpfixDecoder.hpp @@ -31,7 +31,9 @@ class IpfixDecoder : public Decoder { */ IpfixDecoder(int fd) : m_fd(fd), m_decoded(), m_part_readed(), m_msg_size(0) {} - virtual DecodeBuffer &decode() override; + virtual void progress() override; + + virtual DecodeBuffer &buffer() override { return m_decoded; }; virtual const char *get_name() const override { return "IPFIX"; diff --git a/src/plugins/input/tcp/src/Lz4Decoder.cpp b/src/plugins/input/tcp/src/Lz4Decoder.cpp index 6b8a64e7..56d62a52 100644 --- a/src/plugins/input/tcp/src/Lz4Decoder.cpp +++ b/src/plugins/input/tcp/src/Lz4Decoder.cpp @@ -52,7 +52,7 @@ Lz4Decoder::Lz4Decoder(int fd) : } } -DecodeBuffer &Lz4Decoder::decode() { +void Lz4Decoder::progress() { while (!m_decoded.enough_data()) { // Read the header. if (!read_header()) { @@ -73,8 +73,6 @@ DecodeBuffer &Lz4Decoder::decode() { if (m_decoded.is_eof_reached() && m_compressed.size() != 0) { throw std::runtime_error("Incomplete compressed message received"); } - - return m_decoded; } bool Lz4Decoder::read_header() { diff --git a/src/plugins/input/tcp/src/Lz4Decoder.hpp b/src/plugins/input/tcp/src/Lz4Decoder.hpp index f91230fa..cd6c98d7 100644 --- a/src/plugins/input/tcp/src/Lz4Decoder.hpp +++ b/src/plugins/input/tcp/src/Lz4Decoder.hpp @@ -43,11 +43,12 @@ class Lz4Decoder : public Decoder { /** * @brief Decodes the next blocks of data until there is no data or when enough data is readed - * @return decoded data * @throws when fails to read from file descriptor * @throws when fails to decompress the data */ - virtual DecodeBuffer &decode() override; + virtual void progress() override; + + virtual DecodeBuffer &buffer() override { return m_decoded; } virtual const char *get_name() const override { return "LZ4"; diff --git a/src/plugins/input/tcp/src/tls/TlsDecoder.cpp b/src/plugins/input/tcp/src/tls/TlsDecoder.cpp index db90eb01..39ef9313 100644 --- a/src/plugins/input/tcp/src/tls/TlsDecoder.cpp +++ b/src/plugins/input/tcp/src/tls/TlsDecoder.cpp @@ -27,11 +27,11 @@ TlsDecoder::TlsDecoder(SslCtx &ctx, int fd) : m_handshake_complete = m_ssl.accept(); } -DecodeBuffer &TlsDecoder::decode() { +void TlsDecoder::progress() { if (!m_handshake_complete) { m_handshake_complete = m_ssl.accept(); if (!m_handshake_complete) { - return m_decoded; + return; } } @@ -45,18 +45,16 @@ DecodeBuffer &TlsDecoder::decode() { case ReadResult::READ: break; case ReadResult::WAIT: - return m_decoded; + return; case ReadResult::FINISHED: // FIXME: properly check that the shutdown is complete. m_ssl.shutdown(); // Intentional falltrough case ReadResult::CLOSED: m_decoded.signal_eof(); - return m_decoded; + return; } } - - return m_decoded; } } // namespace tls diff --git a/src/plugins/input/tcp/src/tls/TlsDecoder.hpp b/src/plugins/input/tcp/src/tls/TlsDecoder.hpp index 7b45a5f7..6a064242 100644 --- a/src/plugins/input/tcp/src/tls/TlsDecoder.hpp +++ b/src/plugins/input/tcp/src/tls/TlsDecoder.hpp @@ -36,9 +36,10 @@ class TlsDecoder : public Decoder { /** * @brief Reads all available data from TLS stream and returns buffer with decoded messages. - * @returns Buffer with decoded messages. */ - virtual DecodeBuffer &decode() override; + virtual void progress() override; + + virtual DecodeBuffer &buffer() override { return m_decoded; } /** * @brief Gets the name of the decoder. From c93f1ca509785945c737a05d2bd49a62e91604f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Tue, 18 Mar 2025 12:46:41 +0100 Subject: [PATCH 16/19] TCP input TLS - Optimize TlsDecoder. This also brings new api for `DecodeBuffer` and simplification of IpfixDecoder. --- src/plugins/input/tcp/CMakeLists.txt | 2 + src/plugins/input/tcp/src/DecodeBuffer.cpp | 58 +++----------- src/plugins/input/tcp/src/DecodeBuffer.hpp | 49 +++--------- src/plugins/input/tcp/src/IpfixDecoder.cpp | 65 +--------------- src/plugins/input/tcp/src/IpfixDecoder.hpp | 17 +---- src/plugins/input/tcp/src/Lz4Decoder.cpp | 11 ++- src/plugins/input/tcp/src/Lz4Decoder.hpp | 3 +- src/plugins/input/tcp/src/Reader.hpp | 76 +++++++++++++++++++ .../input/tcp/src/RingBufferReader.cpp | 57 ++++++++++++++ .../input/tcp/src/RingBufferReader.hpp | 48 ++++++++++++ src/plugins/input/tcp/src/TcpReader.cpp | 40 ++++++++++ src/plugins/input/tcp/src/TcpReader.hpp | 33 ++++++++ src/plugins/input/tcp/src/read_until_n.hpp | 68 ----------------- src/plugins/input/tcp/src/tls/Ssl.cpp | 16 ++-- src/plugins/input/tcp/src/tls/Ssl.hpp | 28 ++----- src/plugins/input/tcp/src/tls/TlsDecoder.cpp | 29 +------ src/plugins/input/tcp/src/tls/TlsDecoder.hpp | 3 - 17 files changed, 312 insertions(+), 291 deletions(-) create mode 100644 src/plugins/input/tcp/src/Reader.hpp create mode 100644 src/plugins/input/tcp/src/RingBufferReader.cpp create mode 100644 src/plugins/input/tcp/src/RingBufferReader.hpp create mode 100644 src/plugins/input/tcp/src/TcpReader.cpp create mode 100644 src/plugins/input/tcp/src/TcpReader.hpp delete mode 100644 src/plugins/input/tcp/src/read_until_n.hpp diff --git a/src/plugins/input/tcp/CMakeLists.txt b/src/plugins/input/tcp/CMakeLists.txt index 7a2f4a5c..d8564396 100644 --- a/src/plugins/input/tcp/CMakeLists.txt +++ b/src/plugins/input/tcp/CMakeLists.txt @@ -13,6 +13,8 @@ add_library(tcp-input MODULE src/Acceptor.cpp src/Plugin.cpp src/IpxPlugin.cpp + src/TcpReader.cpp + src/RingBufferReader.cpp src/tls/Ssl.cpp src/tls/TlsDecoder.cpp src/tls/DecoderFactory.cpp diff --git a/src/plugins/input/tcp/src/DecodeBuffer.cpp b/src/plugins/input/tcp/src/DecodeBuffer.cpp index 24b0882b..9a9a2a3f 100644 --- a/src/plugins/input/tcp/src/DecodeBuffer.cpp +++ b/src/plugins/input/tcp/src/DecodeBuffer.cpp @@ -19,43 +19,19 @@ namespace tcp_in { -void DecodeBuffer::read_from(const uint8_t *data, size_t size) { - // read until there is something to read - while (size) { - // get the message size - if (!read_header(&data, &size)) { - // There is not enough data to read the whole header. +void DecodeBuffer::read_from(Reader &reader, std::size_t consume) { + consume += m_total_bytes_decoded; + while (!enough_data() || consume < m_total_bytes_decoded) { + if (!read_header(reader)) { break; } - // Read the body of the message. - if (!read_body(&data, &size)) { - // There is not enough data to read the whole body of the message. + if (!read_body(reader)) { break; } } } -void DecodeBuffer::read_from( - const uint8_t *data, - size_t buffer_size, - size_t data_size, - size_t position -) { - // read until wrap - auto block = data + position; - auto block_size = std::min(data_size, buffer_size - position); - read_from(block, block_size); - - if (block_size == data_size) { - // there was no need to wrap - return; - } - - // read the second block (after wrap) - read_from(data, data_size - block_size); -} - void DecodeBuffer::signal_eof() { if (m_part_decoded.size() != 0) { throw std::runtime_error("Received incomplete message."); @@ -63,14 +39,14 @@ void DecodeBuffer::signal_eof() { m_eof_reached = true; } -bool DecodeBuffer::read_header(const uint8_t **data, size_t *size) { +bool DecodeBuffer::read_header(Reader &reader) { if (m_decoded_size != 0) { // The header is already read, but the message body is incomplete. return true; } // Read the header. - if (!read_until_n(sizeof(fds_ipfix_msg_hdr), data, size)) { + if (!read_until_n(sizeof(fds_ipfix_msg_hdr), reader)) { // There is not enough data to read the whole header. return false; } @@ -86,9 +62,9 @@ bool DecodeBuffer::read_header(const uint8_t **data, size_t *size) { return true; } -bool DecodeBuffer::read_body(const uint8_t **data, size_t *size) { +bool DecodeBuffer::read_body(Reader &reader) { // Read the body - if (!read_until_n(m_decoded_size, data, size)) { + if (!read_until_n(m_decoded_size, reader)) { // There is not enough data to read the whole body. return false; } @@ -99,22 +75,10 @@ bool DecodeBuffer::read_body(const uint8_t **data, size_t *size) { return true; } -bool DecodeBuffer::read_until_n(size_t n, const uint8_t **data, size_t *data_len) { - m_part_decoded.reserve(n); - auto cnt = read_min(*data, n, *data_len); - *data_len -= cnt; - *data += cnt; +bool DecodeBuffer::read_until_n(size_t n, Reader &reader) { + m_total_bytes_decoded += reader.read_until_n(n, m_part_decoded, *this); return m_part_decoded.size() == n; } -size_t DecodeBuffer::read_min(const uint8_t *data, size_t size1, size_t size2) { - auto size = std::min(size1, size2); - auto filled = m_part_decoded.size(); - m_part_decoded.resize(filled + size); - std::copy_n(data, size, m_part_decoded.data() + filled); - m_total_bytes_decoded += size; - return size; -} - } // namespace tcp_in diff --git a/src/plugins/input/tcp/src/DecodeBuffer.hpp b/src/plugins/input/tcp/src/DecodeBuffer.hpp index 0b835042..ab583388 100644 --- a/src/plugins/input/tcp/src/DecodeBuffer.hpp +++ b/src/plugins/input/tcp/src/DecodeBuffer.hpp @@ -10,11 +10,12 @@ #pragma once -#include // std::vector #include // uint8_t, UINT16_MAX #include // size_t +#include // std::vector #include "ByteVector.hpp" // ByteVector +#include "Reader.hpp" namespace tcp_in { @@ -50,28 +51,11 @@ class DecodeBuffer { } /** - * @brief Copies IPFIX data from buffer. - * - * The data may be any part of message (possibly incomplete or even multiple messages) but - * multiple calls to this method must be with the message data in correct order so that it can - * be reconstructed. - * @param data data with the message - * @param size size of the data in `data` + * @brief Read data using generic reader function. + * @param reader Generic reader function. + * @param consume Minimum number of bytes that should be read from reader if possible. */ - void read_from(const uint8_t *data, size_t size); - - /** - * @brief Copies IPFIX data from circular buffer. - * - * The data may be any part of message (possibly incomplete or even multiple messages) but - * multiple calls to this metod must be with the message data in correct order so that it can be - * reconstructed. - * @param data data of the circullar buffer - * @param buffer_size size of the circullar buffer (allocated space) - * @param data_size size of data to copy from the buffer - * @param position start position of the data in the buffer. - */ - void read_from(const uint8_t *data, size_t buffer_size, size_t data_size, size_t position); + void read_from(Reader &reader, std::size_t consume = 0); /** * @brief Checks whether enough data has been read since last time. When this returns true, @@ -104,32 +88,23 @@ class DecodeBuffer { private: /** * @brief Reads the length from ipfix header to `m_decoded_size`. - * @param[in,out] data Pointer to data. It is modified by the amount of read data. - * @param[in,out] size Size of the data. It is modified by the amount of read data. + * @param reader Reader from which to read. * @return `true` if the whole header could be read. */ - bool read_header(const uint8_t **data, size_t *size); + bool read_header(Reader &reader); /** * @brief Reads the body of ipfix message. - * @param data Pointer to data. It is modified by the amount of read data. - * @param size Size of the data. It is modified by the amount of read data. + * @param reader Reader from which to read. * @return `true` if the whole header could be read. */ - bool read_body(const uint8_t **data, size_t *size); + bool read_body(Reader &reader); /** * @brief Reads to `m_part_decoded` until it reaches length of `n`. * @param n Target length for `m_part_decoded`. - * @param[in,out] data Data to read from. It is modified by the read amount. - * @param[in,out] data_len Length of data to read. It is modified by the read amount. + * @param reader Generic reader from which to read. * @return `true` if `m_part_decoded` has reached length of `n`. */ - bool read_until_n(size_t n, const uint8_t **data, size_t *data_len); - /** - * @brief Adds N elements from `src` to `m_part_decoded` where N is the smaller of the two - * sizes. - * @return The smaller of the two sizes. - */ - size_t read_min(const uint8_t *data, size_t size1, size_t size2); + bool read_until_n(size_t n, Reader &reader); /** number of bytes readed since last call to `get_decoded` */ size_t m_total_bytes_decoded; diff --git a/src/plugins/input/tcp/src/IpfixDecoder.cpp b/src/plugins/input/tcp/src/IpfixDecoder.cpp index 3753d77b..ddf9c52f 100644 --- a/src/plugins/input/tcp/src/IpfixDecoder.cpp +++ b/src/plugins/input/tcp/src/IpfixDecoder.cpp @@ -10,74 +10,13 @@ #include "IpfixDecoder.hpp" -#include // runtime_error -#include // string -#include // errno, EWOULDBLOCK, EAGAIN -#include // size_t - -#include // read - -#include // fds_ipfix_msg_hdr, ipx_strerror - #include "DecodeBuffer.hpp" // DecodeBuffer -#include "read_until_n.hpp" // read_until_n +#include "Reader.hpp" namespace tcp_in { void IpfixDecoder::progress() { - while (!m_decoded.enough_data()) { - // Read the header of the message. - if (!read_header()) { - // There is not enough data to read the whole header. - break; - } - - // Read the body of the message. - if (!read_body()) { - // There is not enough data to read the whole body of the message. - break; - } - } - - if (m_decoded.is_eof_reached() && m_part_readed.size() != 0) { - throw std::runtime_error("Received incomplete message."); - } -} - -bool IpfixDecoder::read_header() { - if (m_msg_size != 0) { - // The header is already read, but the message body is incomplete. - return true; - } - - // Read the header - if (!read_until_n(sizeof(fds_ipfix_msg_hdr))) { - // There is not enough data to read the whole header. - return false; - } - - // The header has been read, load the size of the message. - auto hdr = reinterpret_cast(m_part_readed.data()); - m_msg_size = ntohs(hdr->length); - return true; -} - -bool IpfixDecoder::read_body() { - // Read the body. - if (!read_until_n(m_msg_size)) { - // There is not enough data to read the whole body - return false; - } - - // Whole message has been read. Add it to the decode buffer. - m_decoded.add(std::move(m_part_readed)); - m_part_readed = ByteVector(); - m_msg_size = 0; - return true; -} - -bool IpfixDecoder::read_until_n(size_t n) { - return ::read_until_n(n, m_fd, m_part_readed, m_decoded); + m_decoded.read_from(m_reader); } } // namespace tcp_in diff --git a/src/plugins/input/tcp/src/IpfixDecoder.hpp b/src/plugins/input/tcp/src/IpfixDecoder.hpp index 333b2946..a4065ff7 100644 --- a/src/plugins/input/tcp/src/IpfixDecoder.hpp +++ b/src/plugins/input/tcp/src/IpfixDecoder.hpp @@ -15,7 +15,7 @@ #include "Decoder.hpp" // Decoder #include "DecodeBuffer.hpp" // DecodeBuffer -#include "ByteVector.hpp" // ByteVector +#include "TcpReader.hpp" namespace tcp_in { @@ -29,7 +29,7 @@ class IpfixDecoder : public Decoder { * @brief Creates ipfix decoder. * @param fd TCP connection file descriptor. */ - IpfixDecoder(int fd) : m_fd(fd), m_decoded(), m_part_readed(), m_msg_size(0) {} + IpfixDecoder(int fd) : m_reader(fd), m_decoded() {} virtual void progress() override; @@ -40,19 +40,8 @@ class IpfixDecoder : public Decoder { } private: - /** returns true if there was enough data to read the header or if the header is already read */ - bool read_header(); - /** returns true if there was enough data to read the body */ - bool read_body(); - /** returns true if there was enough data to read to the given amount */ - bool read_until_n(size_t n); - - int m_fd; + TcpReader m_reader; DecodeBuffer m_decoded; - - ByteVector m_part_readed; - /** Final size of the whole IPFIX message. When 0 IPIFX header is not fully read. */ - size_t m_msg_size; }; } // namespace tcp_in diff --git a/src/plugins/input/tcp/src/Lz4Decoder.cpp b/src/plugins/input/tcp/src/Lz4Decoder.cpp index 56d62a52..cb35884d 100644 --- a/src/plugins/input/tcp/src/Lz4Decoder.cpp +++ b/src/plugins/input/tcp/src/Lz4Decoder.cpp @@ -14,6 +14,7 @@ #include // runtime_error #include // size_t #include // errno, EWOULDBLOCK, EAGAIN +#include #include // string #include // ntohl, ntohs @@ -23,7 +24,7 @@ #include // ipx_strerror #include "DecodeBuffer.hpp" // DecodeBuffer -#include "read_until_n.hpp" // read_until_n +#include "RingBufferReader.hpp" namespace tcp_in { @@ -38,7 +39,7 @@ struct __attribute__((__packed__)) ipfix_start_compress_header { }; Lz4Decoder::Lz4Decoder(int fd) : - m_fd(fd), + m_reader(fd), m_decoded(), m_decoder(LZ4_createStreamDecode()), m_decompressed(), @@ -158,12 +159,13 @@ void Lz4Decoder::decompress() { } // Copy the decompressed data into the decode buffer - m_decoded.read_from( + auto reader = RingBufferReader( m_decompressed.data(), m_decompressed.size(), m_decompressed_size, m_decompressed_pos ); + m_decoded.read_from(reader, m_decompressed_size); m_decompressed_pos += m_decompressed_size; if (m_decompressed_pos >= m_decompressed.size()) { @@ -175,7 +177,8 @@ void Lz4Decoder::decompress() { } bool Lz4Decoder::read_until_n(size_t n) { - return ::read_until_n(n, m_fd, m_compressed, m_decoded); + m_reader.read_until_n(n, m_compressed, m_decoded); + return m_compressed.size() == n; } void Lz4Decoder::reset_stream(size_t buffer_size) { diff --git a/src/plugins/input/tcp/src/Lz4Decoder.hpp b/src/plugins/input/tcp/src/Lz4Decoder.hpp index cd6c98d7..d3343209 100644 --- a/src/plugins/input/tcp/src/Lz4Decoder.hpp +++ b/src/plugins/input/tcp/src/Lz4Decoder.hpp @@ -19,6 +19,7 @@ #include "Decoder.hpp" // Decoder #include "DecodeBuffer.hpp" // DecodeBuffer +#include "TcpReader.hpp" namespace tcp_in { @@ -92,7 +93,7 @@ class Lz4Decoder : public Decoder { */ void reset_stream(size_t buffer_size); - int m_fd; + TcpReader m_reader; DecodeBuffer m_decoded; std::unique_ptr m_decoder; diff --git a/src/plugins/input/tcp/src/Reader.hpp b/src/plugins/input/tcp/src/Reader.hpp new file mode 100644 index 00000000..0afe57c7 --- /dev/null +++ b/src/plugins/input/tcp/src/Reader.hpp @@ -0,0 +1,76 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief Generic reader class. (header file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#pragma once + +#include + +#include + +namespace tcp_in { + + +/** Describes result of read operation. */ +enum class ReadResult { + /** Successufully read some data. */ + READ, + /** Non blocking socket needs to wait for more data. */ + WAIT, + /** Connection has ended. */ + END, +}; + +class Reader { +public: + /** + * @brief Read next portion of data. + * @param[out] data Here will be store read data. + * @param[in,out] length As input is the maximum amount of data to read. Output value will be + * the actual amount of read data. + * @return ReadResult Result of the read operation. + */ + virtual ReadResult read(std::uint8_t *data, std::size_t &length) = 0; + + /** + * @brief Reads to vector from file descriptor until the vector has the desired number of bytes. + * + * If the vector doesn't have n bytes after calling this function, it means that the reader + * doesn't have any more available data, but may have in future. + * + * @tparam Vec vector like object. + * @tparam EofSig `EofSig::signal_eof()` is called when eof is reached. + * @param n Number of bytes that should be in the vector. + * @param result Where to read the bytes to. + * @param eofSignaler Has method to signal eof. + * @return Number of new read bytes. + */ + template + std::size_t read_until_n(std::size_t n, Vec &result, EofSig &eofSignaler) { + auto filled = result.size(); + if (filled >= n) { + return true; + } + + result.resize(n); + + auto read_len = n - filled; + auto res = read(result.data() + filled, read_len); + + result.resize(filled + read_len); + + if (res == ReadResult::END) { + eofSignaler.signal_eof(); + } + + return read_len; + } +}; + +} // namespace tcp_in diff --git a/src/plugins/input/tcp/src/RingBufferReader.cpp b/src/plugins/input/tcp/src/RingBufferReader.cpp new file mode 100644 index 00000000..ddc960d4 --- /dev/null +++ b/src/plugins/input/tcp/src/RingBufferReader.cpp @@ -0,0 +1,57 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief Reader implementation for ring buffer (header file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "RingBufferReader.hpp" + +#include +#include + +namespace tcp_in { + +RingBufferReader::RingBufferReader( + std::uint8_t *buf, + std::size_t buf_len, + std::size_t data_len, + std::size_t data_pos +) { + if (data_pos + data_len > buf_len) { + m_buf1 = buf + data_pos; + m_len1 = buf_len - data_pos; + m_buf2 = buf; + m_len2 = data_len - m_len1; + } else { + m_buf1 = buf; + m_len1 = 0; + m_buf2 = buf + data_pos; + m_len2 = data_len; + } +} + +ReadResult RingBufferReader::read(std::uint8_t *data, std::size_t &length) { + if (m_len1 == 0 && m_len2 == 0) { + return ReadResult::WAIT; + } + + auto r1 = std::min(m_len1, length); + std::memcpy(data, m_buf1, r1); + m_buf1 += r1; + m_len1 -= r1; + + auto r2 = std::min(m_len2, length - r1); + std::memcpy(data + r1, m_buf2, r2); + m_buf2 += r2; + m_len2 += r2; + + length = r1 + r2; + return ReadResult::READ; +} + +} // namespace tcp_in + diff --git a/src/plugins/input/tcp/src/RingBufferReader.hpp b/src/plugins/input/tcp/src/RingBufferReader.hpp new file mode 100644 index 00000000..0334d6c6 --- /dev/null +++ b/src/plugins/input/tcp/src/RingBufferReader.hpp @@ -0,0 +1,48 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief Reader implementation for ring buffer (header file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#pragma once + +#include "Reader.hpp" + +namespace tcp_in { + +/** Reader that reads from ring buffer. */ +class RingBufferReader : public Reader { +public: + + /** + * @brief Creates reader that reads from circullar buffer. The reader will return WAIT when all + * data is read. + * @param buf Circullar buffer to read from. + * @param buf_len Length of the circullar buffer. + * @param data_len Length of data in the circullar buffer. + * @param data_pos Position of first byte of data in the circullar buffer. + */ + RingBufferReader( + std::uint8_t *buf, + std::size_t buf_len, + std::size_t data_len, + std::size_t data_pos + ); + + RingBufferReader() = delete; + + ReadResult read(std::uint8_t *data, std::size_t &length) override; + +private: + std::uint8_t *m_buf1; + std::uint8_t *m_buf2; + std::size_t m_len1; + std::size_t m_len2; +}; + +} // namespace tcp_in + diff --git a/src/plugins/input/tcp/src/TcpReader.cpp b/src/plugins/input/tcp/src/TcpReader.cpp new file mode 100644 index 00000000..e17f16a7 --- /dev/null +++ b/src/plugins/input/tcp/src/TcpReader.cpp @@ -0,0 +1,40 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief Reader implementation for TCP. (header file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "TcpReader.hpp" + +#include +#include +#include + +#include +#include + +namespace tcp_in { + +ReadResult TcpReader::read(std::uint8_t *data, std::size_t &length) { + int res = recv(m_fd, data, length, 0); + if (res == -1) { + length = 0; + int err = errno; + if (err == EWOULDBLOCK || err == EAGAIN) { + return ReadResult::WAIT; + } + const char *err_str; + ipx_strerror(err, err_str); + throw std::runtime_error("Failed to read from descruptor " + std::string(err_str)); + } + + length = std::size_t(res); + return length == 0 ? ReadResult::END : ReadResult::READ; +} + +} // namespace tcp_in + diff --git a/src/plugins/input/tcp/src/TcpReader.hpp b/src/plugins/input/tcp/src/TcpReader.hpp new file mode 100644 index 00000000..c712ad05 --- /dev/null +++ b/src/plugins/input/tcp/src/TcpReader.hpp @@ -0,0 +1,33 @@ +/** + * \file + * \author Jakub Antonín Štigler + * \brief Reader implementation for TCP. (header file) + * \date 2025 + * + * Copyright: (C) 2023 CESNET, z.s.p.o. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#pragma once + +#include "Reader.hpp" + +namespace tcp_in { + +/** Reader that reads from TCP. */ +class TcpReader : public Reader { +public: + /** + * @brief Create new reader that reads from TCP. + * @param fd File descriptor for TCP connection. + */ + TcpReader(int fd) : m_fd(fd) {} + + TcpReader() = delete; + + ReadResult read(std::uint8_t *data, std::size_t &length) override; +private: + int m_fd; +}; + +} // namespace tcp_in diff --git a/src/plugins/input/tcp/src/read_until_n.hpp b/src/plugins/input/tcp/src/read_until_n.hpp deleted file mode 100644 index e7f76f32..00000000 --- a/src/plugins/input/tcp/src/read_until_n.hpp +++ /dev/null @@ -1,68 +0,0 @@ -/** - * \file - * \author Jakub Antonín Štigler - * \brief Function for reading from file descriptor (header file) - * \date 2024 - * - * Copyright: (C) 2023 CESNET, z.s.p.o. - * SPDX-License-Identifier: BSD-3-Clause - */ - -#pragma once - -#include // size_t -#include // errno, EWOULDBLOCK, EAGAIN -#include // std::string -#include // std::runtime_error - -#include // recv - -#include // ipx_strerror - -/** - * @brief Reads to vector from file descriptor until the vector has the desired number of bytes - * - * @tparam Vec vector like object - * @tparam EofSig `EofSig::signal_eof()` is called when eof is reached - * @param n number of bytes that should be in the vector - * @param fd file descriptor to read from - * @param result where to read the bytes to - * @param eofSignaler has method to signal eof - * @return true if the vector has at least `n` bytes after the call - * @throws when fails to read from the file descriptor - */ -template -bool read_until_n(size_t n, int fd, Vec &result, EofSig &eofSignaler) { - auto filled = result.size(); - if (filled >= n) { - return true; - } - - auto remaining = n - filled; - result.resize(n); - - int res = recv(fd, result.data() + filled, remaining, 0); - if (res == -1) { - result.resize(filled); - int err = errno; - if (err == EWOULDBLOCK || err == EAGAIN) { - return false; - } - - const char *err_str; - ipx_strerror(err, err_str); - throw std::runtime_error("Failed to read from descriptor: " + std::string(err_str)); - } - - result.resize(filled + res); - - if (res == 0) { - eofSignaler.signal_eof(); - } - - if (static_cast(res) != remaining) { - return false; - } - - return true; -} diff --git a/src/plugins/input/tcp/src/tls/Ssl.cpp b/src/plugins/input/tcp/src/tls/Ssl.cpp index ce0d01c5..eadbf890 100644 --- a/src/plugins/input/tcp/src/tls/Ssl.cpp +++ b/src/plugins/input/tcp/src/tls/Ssl.cpp @@ -10,6 +10,8 @@ #include "Ssl.hpp" +#include "../Reader.hpp" + namespace tcp_in { namespace tls { @@ -26,21 +28,21 @@ bool Ssl::accept() { return true; } -ReadResult Ssl::read_ex(std::vector &buf) { - buf.resize(buf.capacity()); +ReadResult Ssl::read(std::uint8_t *data, std::size_t &length) { std::size_t len; - auto ret = SSL_read_ex(m_ssl.get(), buf.data(), buf.size(), &len); + auto ret = SSL_read_ex(m_ssl.get(), data, length, &len); if (ret <= 0) { auto err = SSL_get_error(m_ssl.get(), ret); - buf.resize(0); + length = 0; switch (err) { case SSL_ERROR_SSL: // The TLS connection is broken. case SSL_ERROR_ZERO_RETURN: - return ReadResult::FINISHED; + // Don't bother properly closing the connection. We only care about the received + // data but there will be no more data. Peer will handle this. case SSL_ERROR_SYSCALL: - return ReadResult::CLOSED; + return ReadResult::END; case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: return ReadResult::WAIT; @@ -49,7 +51,7 @@ ReadResult Ssl::read_ex(std::vector &buf) { } } - buf.resize(std::min(len, buf.size())); + length = len; return ReadResult::READ; } diff --git a/src/plugins/input/tcp/src/tls/Ssl.hpp b/src/plugins/input/tcp/src/tls/Ssl.hpp index 9ea8c114..d5827b77 100644 --- a/src/plugins/input/tcp/src/tls/Ssl.hpp +++ b/src/plugins/input/tcp/src/tls/Ssl.hpp @@ -16,30 +16,19 @@ #include +#include "../Reader.hpp" #include "SslBio.hpp" #include "throw_ssl_err.hpp" namespace tcp_in { namespace tls { -/** Describes result of read operation. */ -enum class ReadResult { - /** Successufully read some data. */ - READ, - /** Non blocking socket needs to wait for more data. */ - WAIT, - /** Peer will not send any further data. */ - FINISHED, - /** The connection has been closed. (socket cannot read) */ - CLOSED, -}; - /** * @brief Wrapper around `SSL` from OpenSSL. * * This is basically io stream for data encrypted with TLS. */ -class Ssl { +class Ssl : public Reader { public: /** * @brief Construct `Ssl` from the given `SSL_CTX`. @@ -97,17 +86,12 @@ class Ssl { /** * @brief Read application data from the TLS connection. - * - * @param [out] buf Will be cleared. The read data will be put here. Up to the capacity of the - * buffer will be used. - * @return result of the read operation: - * - `READ`: Successfully read some data. `buf` shouldn't be empty. - * - `WAIT`: Non blocking socket needs to wait for more data. `buf` should be empty. - * - `FINISHED`: Peer will not send any further data. `buf` should be empty. - * - `CLOSED`: The connection has been closed. (socket cannot read) `buf` should be empty. + * @param [out] data Where to write the data. + * @param [out,in] length Maximum length of data to write. It will be set to the actual number + * of read bytes. * @throws `std::runtime_error` on failure. */ - ReadResult read_ex(std::vector &buf); + ReadResult read(std::uint8_t *data, std::size_t &length) override; /** * @brief Shuts the connection down. diff --git a/src/plugins/input/tcp/src/tls/TlsDecoder.cpp b/src/plugins/input/tcp/src/tls/TlsDecoder.cpp index 39ef9313..e5f6c03d 100644 --- a/src/plugins/input/tcp/src/tls/TlsDecoder.cpp +++ b/src/plugins/input/tcp/src/tls/TlsDecoder.cpp @@ -10,15 +10,13 @@ #include "TlsDecoder.hpp" +#include "../Reader.hpp" + namespace tcp_in { namespace tls { -// Size of buffer for copying data from OpenSSL to DecodeBuffer. -constexpr std::size_t BUFFER_SIZE = 4096; - TlsDecoder::TlsDecoder(SslCtx &ctx, int fd) : - m_ssl(ctx.create_ssl()), - m_buffer(BUFFER_SIZE) + m_ssl(ctx.create_ssl()) { SslBio bio(BIO_s_socket()); bio.set_fd(fd); @@ -35,26 +33,7 @@ void TlsDecoder::progress() { } } - // XXX: The copy to intermidiate `m_buffer` can be avoided by extending `DecodeBuffer` to be - // able to read with generic read function. - - while (!m_decoded.enough_data()) { - auto res = m_ssl.read_ex(m_buffer); - m_decoded.read_from(m_buffer.data(), m_buffer.size()); - switch (res) { - case ReadResult::READ: - break; - case ReadResult::WAIT: - return; - case ReadResult::FINISHED: - // FIXME: properly check that the shutdown is complete. - m_ssl.shutdown(); - // Intentional falltrough - case ReadResult::CLOSED: - m_decoded.signal_eof(); - return; - } - } + m_decoded.read_from(m_ssl); } } // namespace tls diff --git a/src/plugins/input/tcp/src/tls/TlsDecoder.hpp b/src/plugins/input/tcp/src/tls/TlsDecoder.hpp index 6a064242..850c7750 100644 --- a/src/plugins/input/tcp/src/tls/TlsDecoder.hpp +++ b/src/plugins/input/tcp/src/tls/TlsDecoder.hpp @@ -10,8 +10,6 @@ #pragma once -#include - #include "../Decoder.hpp" #include "../UniqueFd.hpp" #include "SslCtx.hpp" @@ -51,7 +49,6 @@ class TlsDecoder : public Decoder { Ssl m_ssl; bool m_handshake_complete = false; DecodeBuffer m_decoded; - std::vector m_buffer; }; } // namespace tls From 2e321ba081fc425ecc4ced8d9ed0ccba41f6b5fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Tue, 6 May 2025 11:26:34 +0200 Subject: [PATCH 17/19] Improve performance of IpfixDecoder. Copying the functionality of DecodeBuffer directly to IpfixDecoder greatly improves performance of the IpfixDecoder. --- src/plugins/input/tcp/src/IpfixDecoder.cpp | 54 +++++++++++++++++++++- src/plugins/input/tcp/src/IpfixDecoder.hpp | 7 +++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/plugins/input/tcp/src/IpfixDecoder.cpp b/src/plugins/input/tcp/src/IpfixDecoder.cpp index ddf9c52f..cac95a58 100644 --- a/src/plugins/input/tcp/src/IpfixDecoder.cpp +++ b/src/plugins/input/tcp/src/IpfixDecoder.cpp @@ -10,13 +10,65 @@ #include "IpfixDecoder.hpp" +#include + #include "DecodeBuffer.hpp" // DecodeBuffer #include "Reader.hpp" namespace tcp_in { void IpfixDecoder::progress() { - m_decoded.read_from(m_reader); + // Directly calling `m_decoded.read_from` is somehow much slower than copying the code directly + // here. + + while (!m_decoded.enough_data()) { + if (!read_header()) { + break; + } + + if (!read_body()) { + break; + } + } +} + +bool IpfixDecoder::read_header() { + if (m_decoded_size != 0) { + return true; + } + + if (!read_until_n(sizeof(fds_ipfix_msg_hdr))) { + return false; + } + + + // The header has been successfully read. Load the size of the message. + auto hdr = reinterpret_cast(m_part_decoded.data()); + m_decoded_size = ntohs(hdr->length); + + if (m_decoded_size < sizeof(fds_ipfix_msg_hdr)) { + throw std::runtime_error("Invalid IPFIX message header size."); + } + + return true; +} + +bool IpfixDecoder::read_body() { + // Read the body + if (!read_until_n(m_decoded_size)) { + // There is not enough data to read the whole body. + return false; + } + + m_decoded.add(std::move(m_part_decoded)); + m_part_decoded = ByteVector(); + m_decoded_size = 0; + return true; +} + +bool IpfixDecoder::read_until_n(size_t n) { + m_reader.read_until_n(n, m_part_decoded, m_decoded); + return m_part_decoded.size() == n; } } // namespace tcp_in diff --git a/src/plugins/input/tcp/src/IpfixDecoder.hpp b/src/plugins/input/tcp/src/IpfixDecoder.hpp index a4065ff7..843a108c 100644 --- a/src/plugins/input/tcp/src/IpfixDecoder.hpp +++ b/src/plugins/input/tcp/src/IpfixDecoder.hpp @@ -40,6 +40,13 @@ class IpfixDecoder : public Decoder { } private: + bool read_header(); + bool read_body(); + bool read_until_n(size_t n); + + ByteVector m_part_decoded; + size_t m_decoded_size = 0; + TcpReader m_reader; DecodeBuffer m_decoded; }; From 06a90a773fc1db7b0584681be83a1c06dcf5cfb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Thu, 29 May 2025 10:54:06 +0200 Subject: [PATCH 18/19] Support older openssl. --- src/plugins/input/tcp/README.rst | 3 +- src/plugins/input/tcp/src/Config.cpp | 8 +++++ .../input/tcp/src/tls/DecoderFactory.cpp | 14 ++++++-- src/plugins/input/tcp/src/tls/SslCtx.hpp | 35 +++++++++++-------- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/plugins/input/tcp/README.rst b/src/plugins/input/tcp/README.rst index 4ff62459..a15723b7 100644 --- a/src/plugins/input/tcp/README.rst +++ b/src/plugins/input/tcp/README.rst @@ -93,7 +93,8 @@ certificates (certificate authority) is relevant only if the option ``verifyPeer :``caStore``: Path to certificate store with trusted certificates (certificate authorities). Default uses - system defaults. + system defaults. This is available only when using OpenSSL 3 and newer. When used with older + OpenSSL, the plugin will return error. Notes ----- diff --git a/src/plugins/input/tcp/src/Config.cpp b/src/plugins/input/tcp/src/Config.cpp index 4da4e321..603baf82 100644 --- a/src/plugins/input/tcp/src/Config.cpp +++ b/src/plugins/input/tcp/src/Config.cpp @@ -18,6 +18,7 @@ #include #include // fds_*, FDS_* +#include #include // ipx_ctx, IPX_CTX_WARNING @@ -199,6 +200,13 @@ void Config::parse_tls(ipx_ctx *ctx, fds_xml_ctx_t *params) { use_default_ca = !default_ca_file && ca_file.empty() && !default_ca_dir && ca_dir.empty() && !default_ca_store && ca_store.empty(); + +// Supported only from OpenSSL 3.0.0-0 release +#if OPENSSL_VERSION_NUMBER < 0x03000000f + if (default_ca_store || !ca_store.empty()) { + throw std::invalid_argument("Certificate store is not supported before openssl 3."); + } +#endif // OPENSSL_VERSION_MAJOR < 0x03000000f } } // namespace tcp_in diff --git a/src/plugins/input/tcp/src/tls/DecoderFactory.cpp b/src/plugins/input/tcp/src/tls/DecoderFactory.cpp index 9efc5a9f..490b66ec 100644 --- a/src/plugins/input/tcp/src/tls/DecoderFactory.cpp +++ b/src/plugins/input/tcp/src/tls/DecoderFactory.cpp @@ -73,23 +73,33 @@ static void load_ca(const Config &conf, SslCtx &ctx) { return; } + const char *ca_file = nullptr; + const char *ca_dir = nullptr; + if (conf.default_ca_file) { ctx.set_default_verify_file(); } else if (!conf.ca_file.empty()) { - ctx.load_verify_file(conf.ca_file.c_str()); + ca_file = conf.ca_file.c_str(); } if (conf.default_ca_dir) { ctx.set_default_verify_dir(); } else if (!conf.ca_dir.empty()) { - ctx.load_verify_dir(conf.ca_dir.c_str()); + ca_dir = conf.ca_dir.c_str(); + } + + if (ca_file != nullptr || ca_dir != nullptr) { + ctx.load_verify_locations(ca_file, ca_dir); } +// Supported only from OpenSSL 3.0.0-0 release +#if OPENSSL_VERSION_NUMBER >= 0x03000000f if (conf.default_ca_store) { ctx.set_default_verify_store(); } else if (!conf.ca_store.empty()) { ctx.load_verify_store(conf.ca_store.c_str()); } +#endif // OPENSSL_VERSION_NUMBER >= 0x03000000f } } // namespace tls diff --git a/src/plugins/input/tcp/src/tls/SslCtx.hpp b/src/plugins/input/tcp/src/tls/SslCtx.hpp index eccc2536..326e2c17 100644 --- a/src/plugins/input/tcp/src/tls/SslCtx.hpp +++ b/src/plugins/input/tcp/src/tls/SslCtx.hpp @@ -74,16 +74,6 @@ class SslCtx { } } - /** - * @brief Sets the path to trusted certificate file. - * @throws `std::runtime_error` on failure. - */ - void load_verify_file(const char *path) { - if (!SSL_CTX_load_verify_file(m_ctx.get(), path)) { - throw_ssl_err("Failed to set default trusted certificate file `", path, "`."); - } - } - /** * @brief Sets the path to trusted certificates directory. * @@ -98,12 +88,13 @@ class SslCtx { } /** - * @brief Sets the path to trusted certificates directory. - * @throws `std::runtime_error` on failure. + * @brief Sets the path for trusted certificate file and directory. + * @param file Path to trusted certificate file. + * @param dir Path to trusted certificate directory. */ - void load_verify_dir(const char *path) { - if (!SSL_CTX_load_verify_dir(m_ctx.get(), path)) { - throw_ssl_err("Failed to set default trusted certificate directory `", path, "`."); + void load_verify_locations(const char *file, const char *dir) { + if (!SSL_CTX_load_verify_locations(m_ctx.get(), file, dir)) { + throw_ssl_err("Failed to set default trusted certificate locations."); } } @@ -111,22 +102,36 @@ class SslCtx { * @brief Sets the path to trusted certificates store. * * It will use the OS defaults. + * + * This is not available before openssl 3. * @throws `std::runtime_error` on failure. */ void set_default_verify_store() { +// Available only from OpenSSL 3.0.0-0 release +#if OPENSSL_VERSION_NUMBER >= 0x03000000f if (!SSL_CTX_set_default_verify_store(m_ctx.get())) { throw_ssl_err("Failed to set default trusted certificate store."); } +#else // OPENSSL_VERSION_NUMBER >= 0x03000000f + throw std::runtime_error("`set_default_verify_store` is not available before OpenSSL 3."); +#endif // OPENSSL_VERSION_NUMBER >= 0x03000000f } /** * @brief Sets the path to trusted certificates store. + * + * This is not available before openssl 3. * @throws `std::runtime_error` on failure. */ void load_verify_store(const char *path) { +// Available only from OpenSSL 3.0.0-0 release +#if OPENSSL_VERSION_NUMBER >= 0x03000000f if (!SSL_CTX_load_verify_store(m_ctx.get(), path)) { throw_ssl_err("Failed to set default trusted certificate store `", path, "`."); } +#else // OPENSSL_VERSION_NUMBER >= 0x03000000f + throw std::runtime_error("`load_verify_store` is not available before OpenSSL 3."); +#endif // OPENSSL_VERSION_NUMBER >= 0x03000000f } /** From 5db8adec765d0f43f39c003cd7fb1b66bca968da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Anton=C3=ADn=20=C5=A0tigler?= Date: Thu, 29 May 2025 11:51:50 +0200 Subject: [PATCH 19/19] Fix include. --- src/plugins/input/tcp/src/Reader.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/plugins/input/tcp/src/Reader.hpp b/src/plugins/input/tcp/src/Reader.hpp index 0afe57c7..a59a09b0 100644 --- a/src/plugins/input/tcp/src/Reader.hpp +++ b/src/plugins/input/tcp/src/Reader.hpp @@ -10,6 +10,7 @@ #pragma once +#include #include #include