]> granicus.if.org Git - icinga2/blob - lib/base/tlsstream.hpp
lib->compat->statusdatawriter: fix notifications_enabled
[icinga2] / lib / base / tlsstream.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef TLSSTREAM_H
21 #define TLSSTREAM_H
22
23 #include "base/i2-base.hpp"
24 #include "base/socket.hpp"
25 #include "base/socketevents.hpp"
26 #include "base/stream.hpp"
27 #include "base/tlsutility.hpp"
28 #include "base/fifo.hpp"
29
30 namespace icinga
31 {
32
33 enum TlsAction
34 {
35         TlsActionNone,
36         TlsActionRead,
37         TlsActionWrite,
38         TlsActionHandshake
39 };
40
41 /**
42  * A TLS stream.
43  *
44  * @ingroup base
45  */
46 class TlsStream final : public Stream, private SocketEvents
47 {
48 public:
49         DECLARE_PTR_TYPEDEFS(TlsStream);
50
51         TlsStream(const Socket::Ptr& socket, const String& hostname, ConnectionRole role, const std::shared_ptr<SSL_CTX>& sslContext = MakeSSLContext());
52         ~TlsStream() override;
53
54         Socket::Ptr GetSocket() const;
55
56         std::shared_ptr<X509> GetClientCertificate() const;
57         std::shared_ptr<X509> GetPeerCertificate() const;
58
59         void Handshake();
60
61         void Close() override;
62         void Shutdown() override;
63
64         size_t Peek(void *buffer, size_t count, bool allow_partial = false) override;
65         size_t Read(void *buffer, size_t count, bool allow_partial = false) override;
66         void Write(const void *buffer, size_t count) override;
67
68         bool IsEof() const override;
69
70         bool SupportsWaiting() const override;
71         bool IsDataAvailable() const override;
72
73         void SetCorked(bool corked) override;
74
75         bool IsVerifyOK() const;
76         String GetVerifyError() const;
77
78 private:
79         std::shared_ptr<SSL> m_SSL;
80         bool m_Eof;
81         mutable boost::mutex m_Mutex;
82         mutable boost::condition_variable m_CV;
83         bool m_HandshakeOK;
84         bool m_VerifyOK;
85         String m_VerifyError;
86         int m_ErrorCode;
87         bool m_ErrorOccurred;
88
89         Socket::Ptr m_Socket;
90         ConnectionRole m_Role;
91
92         FIFO::Ptr m_SendQ;
93         FIFO::Ptr m_RecvQ;
94
95         TlsAction m_CurrentAction;
96         bool m_Retry;
97         bool m_Shutdown;
98
99         static int m_SSLIndex;
100         static bool m_SSLIndexInitialized;
101
102         void OnEvent(int revents) override;
103
104         void HandleError() const;
105
106         static int ValidateCertificate(int preverify_ok, X509_STORE_CTX *ctx);
107         static void NullCertificateDeleter(X509 *certificate);
108
109         void CloseInternal(bool inDestructor);
110 };
111
112 }
113
114 #endif /* TLSSTREAM_H */