From 0915b3ef56dfac3113cce55a59a5765dc94976be Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:34:54 +0200 Subject: Adding upstream version 2.13.6. Signed-off-by: Daniel Baumann --- lib/base/tlsstream.hpp | 129 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 lib/base/tlsstream.hpp (limited to 'lib/base/tlsstream.hpp') diff --git a/lib/base/tlsstream.hpp b/lib/base/tlsstream.hpp new file mode 100644 index 0000000..f6e5209 --- /dev/null +++ b/lib/base/tlsstream.hpp @@ -0,0 +1,129 @@ +/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ + +#ifndef TLSSTREAM_H +#define TLSSTREAM_H + +#include "base/i2-base.hpp" +#include "base/shared.hpp" +#include "base/socket.hpp" +#include "base/stream.hpp" +#include "base/tlsutility.hpp" +#include "base/fifo.hpp" +#include "base/utility.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace icinga +{ + +template +class SeenStream : public ARS +{ +public: + template + SeenStream(Args&&... args) : ARS(std::forward(args)...) + { + m_Seen.store(nullptr); + } + + template + auto async_read_some(Args&&... args) -> decltype(((ARS*)nullptr)->async_read_some(std::forward(args)...)) + { + { + auto seen (m_Seen.load()); + + if (seen) { + *seen = Utility::GetTime(); + } + } + + return ((ARS*)this)->async_read_some(std::forward(args)...); + } + + inline void SetSeen(double* seen) + { + m_Seen.store(seen); + } + +private: + std::atomic m_Seen; +}; + +struct UnbufferedAsioTlsStreamParams +{ + boost::asio::io_context& IoContext; + boost::asio::ssl::context& SslContext; + const String& Hostname; +}; + +typedef SeenStream> AsioTcpTlsStream; + +class UnbufferedAsioTlsStream : public AsioTcpTlsStream +{ +public: + inline + UnbufferedAsioTlsStream(UnbufferedAsioTlsStreamParams& init) + : AsioTcpTlsStream(init.IoContext, init.SslContext), m_VerifyOK(true), m_Hostname(init.Hostname) + { + } + + bool IsVerifyOK() const; + String GetVerifyError() const; + std::shared_ptr GetPeerCertificate(); + + template + inline + auto async_handshake(handshake_type type, Args&&... args) -> decltype(((AsioTcpTlsStream*)nullptr)->async_handshake(type, std::forward(args)...)) + { + BeforeHandshake(type); + + return AsioTcpTlsStream::async_handshake(type, std::forward(args)...); + } + + template + inline + auto handshake(handshake_type type, Args&&... args) -> decltype(((AsioTcpTlsStream*)nullptr)->handshake(type, std::forward(args)...)) + { + BeforeHandshake(type); + + return AsioTcpTlsStream::handshake(type, std::forward(args)...); + } + +private: + bool m_VerifyOK; + String m_VerifyError; + String m_Hostname; + + void BeforeHandshake(handshake_type type); +}; + +class AsioTlsStream : public boost::asio::buffered_stream +{ +public: + inline + AsioTlsStream(boost::asio::io_context& ioContext, boost::asio::ssl::context& sslContext, const String& hostname = String()) + : AsioTlsStream(UnbufferedAsioTlsStreamParams{ioContext, sslContext, hostname}) + { + } + +private: + inline + AsioTlsStream(UnbufferedAsioTlsStreamParams init) + : buffered_stream(init) + { + } +}; + +typedef boost::asio::buffered_stream AsioTcpStream; +typedef std::pair::Ptr, Shared::Ptr> OptionalTlsStream; + +} + +#endif /* TLSSTREAM_H */ -- cgit v1.2.3