Skip to content

Commit 3b12309

Browse files
committed
TCP input TLS - Add DecoderFactory for TlsDecoders.
1 parent 5087679 commit 3b12309

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

src/plugins/input/tcp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_library(tcp-input MODULE
1515
src/IpxPlugin.cpp
1616
src/tls/Ssl.cpp
1717
src/tls/TlsDecoder.cpp
18+
src/tls/DecoderFactory.cpp
1819
)
1920

2021
find_package(LibLz4 REQUIRED)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* \file
3+
* \author Jakub Antonín Štigler <[email protected]>
4+
* \brief Factory for tls connections. (source file)
5+
* \date 2025
6+
*
7+
* Copyright: (C) 2023 CESNET, z.s.p.o.
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
#include "DecoderFactory.hpp"
12+
13+
#include "TlsDecoder.hpp"
14+
15+
// OpenSSL v1.1.1 compatibility
16+
#ifndef SSL_OP_IGNORE_UNEXPECTED_EOF
17+
#define SSL_OP_IGNORE_UNEXPECTED_EOF 0
18+
#endif // ifndef SSL_OP_IGNORE_UNEXPECTED_EOF
19+
20+
namespace tcp_in {
21+
namespace tls {
22+
23+
DecoderFactory::DecoderFactory(const std::string &cert_path) : m_ctx(TLS_server_method()) {
24+
// Cache configuration
25+
constexpr std::chrono::seconds CACHE_TIMEOUT(3600);
26+
const std::string cache_id = "ipfixcol2";
27+
constexpr std::size_t CACHE_SIZE = 1024;
28+
29+
m_ctx.set_min_proto_version(TLS1_2_VERSION);
30+
m_ctx.set_options(
31+
SSL_OP_IGNORE_UNEXPECTED_EOF | SSL_OP_NO_RENEGOTIATION | SSL_OP_CIPHER_SERVER_PREFERENCE
32+
);
33+
34+
m_ctx.use_certificate_chain_file(cert_path.c_str());
35+
m_ctx.use_private_key_file(cert_path.c_str(), SSL_FILETYPE_PEM);
36+
37+
m_ctx.set_session_id_context(cache_id.data(), unsigned(cache_id.size()));
38+
m_ctx.set_session_cache_mode(SSL_SESS_CACHE_SERVER);
39+
m_ctx.sess_set_cache_size(CACHE_SIZE);
40+
m_ctx.sess_set_timeout(CACHE_TIMEOUT);
41+
42+
// Don't verify peer. Servers usually don't verify clients, but maybe we want to verify clients?
43+
m_ctx.set_verify(SSL_VERIFY_NONE);
44+
}
45+
46+
std::unique_ptr<Decoder> DecoderFactory::create(int fd) {
47+
return std::unique_ptr<Decoder>(new TlsDecoder(m_ctx, fd));
48+
}
49+
50+
} // namespace tls
51+
} // namespace tcp_in
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* \file
3+
* \author Jakub Antonín Štigler <[email protected]>
4+
* \brief Factory for tls connections. (header file)
5+
* \date 2025
6+
*
7+
* Copyright: (C) 2023 CESNET, z.s.p.o.
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
#pragma once
12+
13+
#include "../Decoder.hpp"
14+
#include "SslCtx.hpp"
15+
16+
namespace tcp_in {
17+
namespace tls {
18+
19+
/** Factory for `TlsDecoder`s. Holds shared data. */
20+
class DecoderFactory {
21+
public:
22+
/**
23+
* @brief Create new tls decoder factory using certificate and private key in the given file.
24+
* Note that this may prompt the user for password.
25+
*/
26+
DecoderFactory(const std::string &cert_path);
27+
28+
/** Create new tls decoder. */
29+
std::unique_ptr<Decoder> create(int fd);
30+
31+
private:
32+
SslCtx m_ctx;
33+
};
34+
35+
} // namespace tls
36+
} // namespace tcp_in
37+

0 commit comments

Comments
 (0)