|
| 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 |
0 commit comments