2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-05-10 06:06:41 -04:00
|
|
|
|
2012-11-22 06:04:32 -05:00
|
|
|
#ifndef TLSSTREAM_H
|
|
|
|
|
#define TLSSTREAM_H
|
2012-04-24 08:02:15 -04:00
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/i2-base.hpp"
|
2019-07-25 08:34:29 -04:00
|
|
|
#include "base/shared.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/socket.hpp"
|
|
|
|
|
#include "base/stream.hpp"
|
|
|
|
|
#include "base/tlsutility.hpp"
|
2015-02-13 15:02:48 -05:00
|
|
|
#include "base/fifo.hpp"
|
2020-02-13 12:18:58 -05:00
|
|
|
#include "base/utility.hpp"
|
|
|
|
|
#include <atomic>
|
2019-04-23 05:25:26 -04:00
|
|
|
#include <memory>
|
2019-02-12 08:56:47 -05:00
|
|
|
#include <utility>
|
|
|
|
|
#include <boost/asio/buffered_stream.hpp>
|
2019-09-09 09:11:38 -04:00
|
|
|
#include <boost/asio/io_context.hpp>
|
2019-02-12 08:56:47 -05:00
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
2020-02-13 12:18:58 -05:00
|
|
|
#include <boost/asio/spawn.hpp>
|
2019-02-08 08:23:10 -05:00
|
|
|
#include <boost/asio/ssl/context.hpp>
|
2019-02-12 08:56:47 -05:00
|
|
|
#include <boost/asio/ssl/stream.hpp>
|
2013-03-16 16:18:53 -04:00
|
|
|
|
2012-04-24 08:02:15 -04:00
|
|
|
namespace icinga
|
|
|
|
|
{
|
|
|
|
|
|
2020-02-13 12:18:58 -05:00
|
|
|
template<class ARS>
|
|
|
|
|
class SeenStream : public ARS
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
template<class... Args>
|
|
|
|
|
SeenStream(Args&&... args) : ARS(std::forward<Args>(args)...)
|
|
|
|
|
{
|
|
|
|
|
m_Seen.store(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class... Args>
|
|
|
|
|
auto async_read_some(Args&&... args) -> decltype(((ARS*)nullptr)->async_read_some(std::forward<Args>(args)...))
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
auto seen (m_Seen.load());
|
|
|
|
|
|
|
|
|
|
if (seen) {
|
|
|
|
|
*seen = Utility::GetTime();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ((ARS*)this)->async_read_some(std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void SetSeen(double* seen)
|
|
|
|
|
{
|
|
|
|
|
m_Seen.store(seen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::atomic<double*> m_Seen;
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-25 10:18:48 -05:00
|
|
|
struct UnbufferedAsioTlsStreamParams
|
|
|
|
|
{
|
2019-09-09 09:11:38 -04:00
|
|
|
boost::asio::io_context& IoContext;
|
2019-02-25 10:18:48 -05:00
|
|
|
boost::asio::ssl::context& SslContext;
|
|
|
|
|
const String& Hostname;
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-13 12:18:58 -05:00
|
|
|
typedef SeenStream<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>> AsioTcpTlsStream;
|
2019-02-25 10:18:48 -05:00
|
|
|
|
2019-03-12 06:36:30 -04:00
|
|
|
class UnbufferedAsioTlsStream : public AsioTcpTlsStream
|
|
|
|
|
{
|
2019-02-12 08:56:47 -05:00
|
|
|
public:
|
|
|
|
|
inline
|
2019-02-25 10:18:48 -05:00
|
|
|
UnbufferedAsioTlsStream(UnbufferedAsioTlsStreamParams& init)
|
2020-02-13 12:18:58 -05:00
|
|
|
: AsioTcpTlsStream(init.IoContext, init.SslContext), m_VerifyOK(true), m_Hostname(init.Hostname)
|
2019-02-12 08:56:47 -05:00
|
|
|
{
|
|
|
|
|
}
|
2019-02-25 10:18:48 -05:00
|
|
|
|
|
|
|
|
bool IsVerifyOK() const;
|
|
|
|
|
String GetVerifyError() const;
|
2019-02-25 11:22:00 -05:00
|
|
|
std::shared_ptr<X509> GetPeerCertificate();
|
2019-02-25 10:18:48 -05:00
|
|
|
|
|
|
|
|
template<class... Args>
|
|
|
|
|
inline
|
2019-03-12 06:47:53 -04:00
|
|
|
auto async_handshake(handshake_type type, Args&&... args) -> decltype(((AsioTcpTlsStream*)nullptr)->async_handshake(type, std::forward<Args>(args)...))
|
2019-02-25 10:18:48 -05:00
|
|
|
{
|
|
|
|
|
BeforeHandshake(type);
|
|
|
|
|
|
2019-03-12 06:36:30 -04:00
|
|
|
return AsioTcpTlsStream::async_handshake(type, std::forward<Args>(args)...);
|
2019-02-25 10:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class... Args>
|
|
|
|
|
inline
|
2019-03-12 06:47:53 -04:00
|
|
|
auto handshake(handshake_type type, Args&&... args) -> decltype(((AsioTcpTlsStream*)nullptr)->handshake(type, std::forward<Args>(args)...))
|
2019-02-25 10:18:48 -05:00
|
|
|
{
|
|
|
|
|
BeforeHandshake(type);
|
|
|
|
|
|
2019-03-12 06:36:30 -04:00
|
|
|
return AsioTcpTlsStream::handshake(type, std::forward<Args>(args)...);
|
2019-02-25 10:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool m_VerifyOK;
|
|
|
|
|
String m_VerifyError;
|
|
|
|
|
String m_Hostname;
|
|
|
|
|
|
|
|
|
|
void BeforeHandshake(handshake_type type);
|
2019-02-12 08:56:47 -05:00
|
|
|
};
|
|
|
|
|
|
2019-02-22 09:42:48 -05:00
|
|
|
class AsioTlsStream : public boost::asio::buffered_stream<UnbufferedAsioTlsStream>
|
2019-02-12 08:56:47 -05:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
inline
|
2019-09-09 09:11:38 -04:00
|
|
|
AsioTlsStream(boost::asio::io_context& ioContext, boost::asio::ssl::context& sslContext, const String& hostname = String())
|
|
|
|
|
: AsioTlsStream(UnbufferedAsioTlsStreamParams{ioContext, sslContext, hostname})
|
2019-02-12 08:56:47 -05:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
inline
|
2019-02-25 10:18:48 -05:00
|
|
|
AsioTlsStream(UnbufferedAsioTlsStreamParams init)
|
2019-02-12 08:56:47 -05:00
|
|
|
: buffered_stream(init)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-23 05:25:26 -04:00
|
|
|
typedef boost::asio::buffered_stream<boost::asio::ip::tcp::socket> AsioTcpStream;
|
2019-07-25 08:55:22 -04:00
|
|
|
typedef std::pair<Shared<AsioTlsStream>::Ptr, Shared<AsioTcpStream>::Ptr> OptionalTlsStream;
|
2019-04-23 05:25:26 -04:00
|
|
|
|
2012-04-24 08:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
2012-11-22 06:04:32 -05:00
|
|
|
#endif /* TLSSTREAM_H */
|