2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-05-10 12:06:41 +02:00
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
#ifndef TLSSTREAM_H
|
|
|
|
#define TLSSTREAM_H
|
2012-04-24 14:02:15 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/i2-base.hpp"
|
|
|
|
#include "base/socket.hpp"
|
|
|
|
#include "base/stream.hpp"
|
|
|
|
#include "base/tlsutility.hpp"
|
2015-02-13 21:02:48 +01:00
|
|
|
#include "base/fifo.hpp"
|
2019-04-23 11:25:26 +02:00
|
|
|
#include <memory>
|
2019-02-12 14:56:47 +01:00
|
|
|
#include <utility>
|
|
|
|
#include <boost/asio/buffered_stream.hpp>
|
|
|
|
#include <boost/asio/io_service.hpp>
|
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
2019-02-08 14:23:10 +01:00
|
|
|
#include <boost/asio/ssl/context.hpp>
|
2019-02-12 14:56:47 +01:00
|
|
|
#include <boost/asio/ssl/stream.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2019-02-25 16:18:48 +01:00
|
|
|
struct UnbufferedAsioTlsStreamParams
|
|
|
|
{
|
|
|
|
boost::asio::io_service& IoService;
|
|
|
|
boost::asio::ssl::context& SslContext;
|
|
|
|
const String& Hostname;
|
|
|
|
};
|
|
|
|
|
2019-03-12 11:36:30 +01:00
|
|
|
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> AsioTcpTlsStream;
|
2019-02-25 16:18:48 +01:00
|
|
|
|
2019-03-12 11:36:30 +01:00
|
|
|
class UnbufferedAsioTlsStream : public AsioTcpTlsStream
|
|
|
|
{
|
2019-02-12 14:56:47 +01:00
|
|
|
public:
|
|
|
|
inline
|
2019-02-25 16:18:48 +01:00
|
|
|
UnbufferedAsioTlsStream(UnbufferedAsioTlsStreamParams& init)
|
|
|
|
: stream(init.IoService, init.SslContext), m_VerifyOK(true), m_Hostname(init.Hostname)
|
2019-02-12 14:56:47 +01:00
|
|
|
{
|
|
|
|
}
|
2019-02-25 16:18:48 +01:00
|
|
|
|
|
|
|
bool IsVerifyOK() const;
|
|
|
|
String GetVerifyError() const;
|
2019-02-25 17:22:00 +01:00
|
|
|
std::shared_ptr<X509> GetPeerCertificate();
|
2019-02-25 16:18:48 +01:00
|
|
|
|
|
|
|
template<class... Args>
|
|
|
|
inline
|
2019-03-12 11:47:53 +01:00
|
|
|
auto async_handshake(handshake_type type, Args&&... args) -> decltype(((AsioTcpTlsStream*)nullptr)->async_handshake(type, std::forward<Args>(args)...))
|
2019-02-25 16:18:48 +01:00
|
|
|
{
|
|
|
|
BeforeHandshake(type);
|
|
|
|
|
2019-03-12 11:36:30 +01:00
|
|
|
return AsioTcpTlsStream::async_handshake(type, std::forward<Args>(args)...);
|
2019-02-25 16:18:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class... Args>
|
|
|
|
inline
|
2019-03-12 11:47:53 +01:00
|
|
|
auto handshake(handshake_type type, Args&&... args) -> decltype(((AsioTcpTlsStream*)nullptr)->handshake(type, std::forward<Args>(args)...))
|
2019-02-25 16:18:48 +01:00
|
|
|
{
|
|
|
|
BeforeHandshake(type);
|
|
|
|
|
2019-03-12 11:36:30 +01:00
|
|
|
return AsioTcpTlsStream::handshake(type, std::forward<Args>(args)...);
|
2019-02-25 16:18:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_VerifyOK;
|
|
|
|
String m_VerifyError;
|
|
|
|
String m_Hostname;
|
|
|
|
|
|
|
|
void BeforeHandshake(handshake_type type);
|
2019-02-12 14:56:47 +01:00
|
|
|
};
|
|
|
|
|
2019-02-22 15:42:48 +01:00
|
|
|
class AsioTlsStream : public boost::asio::buffered_stream<UnbufferedAsioTlsStream>
|
2019-02-12 14:56:47 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline
|
2019-02-25 16:18:48 +01:00
|
|
|
AsioTlsStream(boost::asio::io_service& ioService, boost::asio::ssl::context& sslContext, const String& hostname = String())
|
|
|
|
: AsioTlsStream(UnbufferedAsioTlsStreamParams{ioService, sslContext, hostname})
|
2019-02-12 14:56:47 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
inline
|
2019-02-25 16:18:48 +01:00
|
|
|
AsioTlsStream(UnbufferedAsioTlsStreamParams init)
|
2019-02-12 14:56:47 +01:00
|
|
|
: buffered_stream(init)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-23 11:25:26 +02:00
|
|
|
typedef boost::asio::buffered_stream<boost::asio::ip::tcp::socket> AsioTcpStream;
|
|
|
|
typedef std::pair<std::shared_ptr<AsioTlsStream>, std::shared_ptr<AsioTcpStream>> OptionalTlsStream;
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
}
|
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
#endif /* TLSSTREAM_H */
|