1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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. *
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. *
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 ******************************************************************************/
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"
46 class TlsStream final : public Stream, private SocketEvents
49 DECLARE_PTR_TYPEDEFS(TlsStream);
51 TlsStream(const Socket::Ptr& socket, const String& hostname, ConnectionRole role, const std::shared_ptr<SSL_CTX>& sslContext = MakeSSLContext());
52 ~TlsStream() override;
54 Socket::Ptr GetSocket() const;
56 std::shared_ptr<X509> GetClientCertificate() const;
57 std::shared_ptr<X509> GetPeerCertificate() const;
61 void Close() override;
62 void Shutdown() override;
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;
68 bool IsEof() const override;
70 bool SupportsWaiting() const override;
71 bool IsDataAvailable() const override;
73 void SetCorked(bool corked) override;
75 bool IsVerifyOK() const;
76 String GetVerifyError() const;
79 std::shared_ptr<SSL> m_SSL;
81 mutable boost::mutex m_Mutex;
82 mutable boost::condition_variable m_CV;
90 ConnectionRole m_Role;
95 TlsAction m_CurrentAction;
99 static int m_SSLIndex;
100 static bool m_SSLIndexInitialized;
102 void OnEvent(int revents) override;
104 void HandleError() const;
106 static int ValidateCertificate(int preverify_ok, X509_STORE_CTX *ctx);
107 static void NullCertificateDeleter(X509 *certificate);
109 void CloseInternal(bool inDestructor);
114 #endif /* TLSSTREAM_H */