2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2012-05-10 12:06:41 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
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"
|
2015-02-13 21:02:48 +01:00
|
|
|
#include "base/socketevents.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/stream.hpp"
|
|
|
|
#include "base/tlsutility.hpp"
|
2015-02-13 21:02:48 +01:00
|
|
|
#include "base/fifo.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
enum TlsAction
|
|
|
|
{
|
|
|
|
TlsActionNone,
|
|
|
|
TlsActionRead,
|
|
|
|
TlsActionWrite,
|
2015-02-25 13:21:38 +01:00
|
|
|
TlsActionHandshake
|
2015-02-13 21:02:48 +01:00
|
|
|
};
|
|
|
|
|
2012-05-15 10:58:14 +02:00
|
|
|
/**
|
2012-11-22 12:04:32 +01:00
|
|
|
* A TLS stream.
|
2012-05-18 22:21:28 +02:00
|
|
|
*
|
|
|
|
* @ingroup base
|
2012-05-15 10:58:14 +02:00
|
|
|
*/
|
2015-02-13 21:02:48 +01:00
|
|
|
class I2_BASE_API TlsStream : public Stream, private SocketEvents
|
2012-04-24 14:02:15 +02:00
|
|
|
{
|
2012-05-21 23:42:54 +02:00
|
|
|
public:
|
2014-11-07 12:32:25 +01:00
|
|
|
DECLARE_PTR_TYPEDEFS(TlsStream);
|
2012-05-21 23:42:54 +02:00
|
|
|
|
2015-03-05 14:15:42 +01:00
|
|
|
TlsStream(const Socket::Ptr& socket, const String& hostname, ConnectionRole role, const boost::shared_ptr<SSL_CTX>& sslContext);
|
2015-02-15 18:50:25 +01:00
|
|
|
~TlsStream(void);
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
boost::shared_ptr<X509> GetClientCertificate(void) const;
|
|
|
|
boost::shared_ptr<X509> GetPeerCertificate(void) const;
|
2012-05-21 23:42:54 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
void Handshake(void);
|
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
virtual void Close(void);
|
2012-05-21 23:42:54 +02:00
|
|
|
|
2015-02-14 16:34:36 +01:00
|
|
|
virtual size_t Read(void *buffer, size_t count, bool allow_partial = false);
|
2012-11-22 12:04:32 +01:00
|
|
|
virtual void Write(const void *buffer, size_t count);
|
|
|
|
|
2013-08-27 12:21:41 +02:00
|
|
|
virtual bool IsEof(void) const;
|
|
|
|
|
2015-02-14 16:34:36 +01:00
|
|
|
virtual bool SupportsWaiting(void) const;
|
|
|
|
virtual bool IsDataAvailable(void) const;
|
|
|
|
|
2014-10-16 09:01:18 +02:00
|
|
|
bool IsVerifyOK(void) const;
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
private:
|
2014-11-08 21:17:16 +01:00
|
|
|
boost::shared_ptr<SSL> m_SSL;
|
2014-09-10 08:51:25 +02:00
|
|
|
bool m_Eof;
|
2015-02-13 21:02:48 +01:00
|
|
|
mutable boost::mutex m_Mutex;
|
|
|
|
mutable boost::condition_variable m_CV;
|
|
|
|
bool m_HandshakeOK;
|
2014-10-16 09:01:18 +02:00
|
|
|
bool m_VerifyOK;
|
2015-02-13 21:02:48 +01:00
|
|
|
int m_ErrorCode;
|
|
|
|
bool m_ErrorOccurred;
|
2012-04-24 14:02:15 +02:00
|
|
|
|
2014-04-23 13:42:59 +02:00
|
|
|
Socket::Ptr m_Socket;
|
2014-05-03 20:02:22 +02:00
|
|
|
ConnectionRole m_Role;
|
2012-04-27 14:15:22 +02:00
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
FIFO::Ptr m_SendQ;
|
|
|
|
FIFO::Ptr m_RecvQ;
|
|
|
|
|
|
|
|
TlsAction m_CurrentAction;
|
|
|
|
bool m_Retry;
|
|
|
|
|
2012-04-24 14:54:05 +02:00
|
|
|
static int m_SSLIndex;
|
|
|
|
static bool m_SSLIndexInitialized;
|
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
virtual void OnEvent(int revents);
|
|
|
|
|
|
|
|
void HandleError(void) const;
|
2014-12-19 12:07:06 +01:00
|
|
|
|
2014-10-16 09:01:18 +02:00
|
|
|
static int ValidateCertificate(int preverify_ok, X509_STORE_CTX *ctx);
|
2012-11-23 11:04:08 +01:00
|
|
|
static void NullCertificateDeleter(X509 *certificate);
|
2012-11-22 12:04:32 +01:00
|
|
|
};
|
2012-04-24 14:02:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
#endif /* TLSSTREAM_H */
|