Skip to content

Commit 4375e8a

Browse files
committed
TCP input TLS - Add DecoderFactory for TlsDecoders.
1 parent 490921c commit 4375e8a

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 "../Config.hpp"
14+
#include "TlsDecoder.hpp"
15+
16+
// OpenSSL v1.1.1 compatibility.
17+
// The flag SSL_OP_IGNORE_UNEXPECTED_EOF is not supported in v1.1.1. This will define it as flag
18+
// without effect if it is not supported so that it may be used in any case.
19+
// The flag is not neccesary, but with it if peer closes the connection without sending proper TLS
20+
// message, it will not be treated as error. This is OK because can check if IPFIX messages are
21+
// complete.
22+
#ifndef SSL_OP_IGNORE_UNEXPECTED_EOF
23+
#define SSL_OP_IGNORE_UNEXPECTED_EOF 0
24+
#endif // ifndef SSL_OP_IGNORE_UNEXPECTED_EOF
25+
26+
namespace tcp_in {
27+
namespace tls {
28+
29+
DecoderFactory::DecoderFactory(const Config &conf) : m_ctx(TLS_server_method()) {
30+
// Cache configuration. Cache is used to speed up initial handshake for clients that were
31+
// recently connected.
32+
constexpr long CACHE_TIMEOUT_SECONDS = 3600;
33+
const std::string cache_id = "ipfixcol2";
34+
constexpr std::size_t CACHE_SIZE = 1024;
35+
36+
m_ctx.set_min_proto_version(TLS1_2_VERSION);
37+
m_ctx.set_options(
38+
SSL_OP_IGNORE_UNEXPECTED_EOF | SSL_OP_NO_RENEGOTIATION | SSL_OP_CIPHER_SERVER_PREFERENCE
39+
);
40+
41+
m_ctx.use_certificate_chain_file(conf.certificate_file.c_str());
42+
m_ctx.use_private_key_file(conf.certificate_file.c_str(), SSL_FILETYPE_PEM);
43+
44+
m_ctx.set_session_id_context(cache_id.data(), unsigned(cache_id.size()));
45+
m_ctx.set_session_cache_mode(SSL_SESS_CACHE_SERVER);
46+
m_ctx.sess_set_cache_size(CACHE_SIZE);
47+
m_ctx.sess_set_timeout(CACHE_TIMEOUT_SECONDS);
48+
49+
if (conf.verify_peer) {
50+
m_ctx.set_verify(SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
51+
m_ctx.set_default_verify_paths();
52+
} else {
53+
m_ctx.set_verify(SSL_VERIFY_NONE);
54+
}
55+
}
56+
57+
std::unique_ptr<Decoder> DecoderFactory::create(int fd) {
58+
return std::unique_ptr<Decoder>(new TlsDecoder(m_ctx, fd));
59+
}
60+
61+
} // namespace tls
62+
} // namespace tcp_in
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 "../Config.hpp"
14+
#include "../Decoder.hpp"
15+
#include "SslCtx.hpp"
16+
17+
namespace tcp_in {
18+
namespace tls {
19+
20+
/** Factory for `TlsDecoder`s. Holds shared data. */
21+
class DecoderFactory {
22+
public:
23+
/**
24+
* @brief Create new tls decoder factory using certificate and private key in the given file.
25+
* Note that this may prompt the user for password.
26+
* @throws `std::runtime_error` on failure.
27+
*/
28+
DecoderFactory(const Config &conf);
29+
30+
/**
31+
* @brief Create new tls decoder.
32+
* @param fd Stream for tls communication. (Usually TCP).
33+
* @return TLS decoder.
34+
* @throws `std::runtime_exception` if initialization of the decoder fails.
35+
*/
36+
std::unique_ptr<Decoder> create(int fd);
37+
38+
private:
39+
SslCtx m_ctx;
40+
};
41+
42+
} // namespace tls
43+
} // namespace tcp_in
44+

0 commit comments

Comments
 (0)