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