2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 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
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/tlsstream.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/exception.hpp"
|
2014-06-05 15:34:54 +02:00
|
|
|
#include "base/logger_fwd.hpp"
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/bind.hpp>
|
2014-06-05 15:34:54 +02:00
|
|
|
#include <iostream>
|
2012-04-24 14:02:15 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
int I2_EXPORT TlsStream::m_SSLIndex;
|
|
|
|
bool I2_EXPORT TlsStream::m_SSLIndexInitialized = false;
|
2012-04-24 14:54:05 +02:00
|
|
|
|
2012-05-08 15:36:28 +02:00
|
|
|
/**
|
2012-11-22 12:04:32 +01:00
|
|
|
* Constructor for the TlsStream class.
|
2012-05-08 15:36:28 +02:00
|
|
|
*
|
|
|
|
* @param role The role of the client.
|
|
|
|
* @param sslContext The SSL context for the client.
|
|
|
|
*/
|
2014-08-01 14:28:32 +02:00
|
|
|
TlsStream::TlsStream(const Socket::Ptr& socket, ConnectionRole role, const shared_ptr<SSL_CTX>& sslContext)
|
2014-04-23 13:42:59 +02:00
|
|
|
: m_Socket(socket), m_Role(role)
|
2012-11-22 12:04:32 +01:00
|
|
|
{
|
2014-08-05 11:30:06 +02:00
|
|
|
std::ostringstream msgbuf;
|
|
|
|
char errbuf[120];
|
|
|
|
|
2014-04-12 21:22:59 +02:00
|
|
|
m_SSL = shared_ptr<SSL>(SSL_new(sslContext.get()), SSL_free);
|
2012-04-24 14:54:05 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
if (!m_SSL) {
|
2014-08-20 14:07:23 +02:00
|
|
|
msgbuf << "SSL_new() failed with code " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
|
2014-06-05 15:34:54 +02:00
|
|
|
Log(LogCritical, "TlsStream", msgbuf.str());
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
BOOST_THROW_EXCEPTION(openssl_error()
|
|
|
|
<< boost::errinfo_api_function("SSL_new")
|
2014-08-20 14:07:23 +02:00
|
|
|
<< errinfo_openssl_error(ERR_peek_error()));
|
2013-04-04 16:08:02 +02:00
|
|
|
}
|
2012-04-24 14:54:05 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
if (!m_SSLIndexInitialized) {
|
|
|
|
m_SSLIndex = SSL_get_ex_new_index(0, const_cast<char *>("TlsStream"), NULL, NULL, NULL);
|
|
|
|
m_SSLIndexInitialized = true;
|
|
|
|
}
|
2012-04-24 14:02:15 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
SSL_set_ex_data(m_SSL.get(), m_SSLIndex, this);
|
2012-05-07 11:41:23 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
SSL_set_verify(m_SSL.get(), SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
|
2012-07-18 11:15:39 +02:00
|
|
|
|
2014-04-23 13:42:59 +02:00
|
|
|
socket->MakeNonBlocking();
|
|
|
|
|
2014-08-01 14:28:32 +02:00
|
|
|
SSL_set_fd(m_SSL.get(), socket->GetFD());
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
if (m_Role == RoleServer)
|
2013-04-04 16:08:02 +02:00
|
|
|
SSL_set_accept_state(m_SSL.get());
|
|
|
|
else
|
|
|
|
SSL_set_connect_state(m_SSL.get());
|
2012-06-24 02:56:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the X509 certficate for this client.
|
|
|
|
*
|
|
|
|
* @returns The X509 certificate.
|
|
|
|
*/
|
2012-11-22 12:04:32 +01:00
|
|
|
shared_ptr<X509> TlsStream::GetClientCertificate(void) const
|
2012-06-24 02:56:48 +02:00
|
|
|
{
|
2014-08-05 08:40:06 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_SSLLock);
|
2012-08-07 21:02:12 +02:00
|
|
|
return shared_ptr<X509>(SSL_get_certificate(m_SSL.get()), &Utility::NullDeleter);
|
2012-06-24 02:56:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the X509 certficate for the peer.
|
|
|
|
*
|
|
|
|
* @returns The X509 certificate.
|
|
|
|
*/
|
2012-11-22 12:04:32 +01:00
|
|
|
shared_ptr<X509> TlsStream::GetPeerCertificate(void) const
|
2012-06-24 02:56:48 +02:00
|
|
|
{
|
2014-08-05 08:40:06 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_SSLLock);
|
2012-06-24 02:56:48 +02:00
|
|
|
return shared_ptr<X509>(SSL_get_peer_certificate(m_SSL.get()), X509_free);
|
2012-04-24 14:02:15 +02:00
|
|
|
}
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
void TlsStream::Handshake(void)
|
2012-11-22 12:04:32 +01:00
|
|
|
{
|
2014-08-05 11:30:06 +02:00
|
|
|
std::ostringstream msgbuf;
|
|
|
|
char errbuf[120];
|
|
|
|
|
2014-09-05 08:19:47 +02:00
|
|
|
boost::mutex::scoped_lock alock(m_IOActionLock);
|
|
|
|
|
2014-04-23 13:42:59 +02:00
|
|
|
for (;;) {
|
2014-05-03 19:56:47 +02:00
|
|
|
int rc, err;
|
2014-04-23 13:42:59 +02:00
|
|
|
|
2014-08-05 08:40:06 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_SSLLock);
|
|
|
|
rc = SSL_do_handshake(m_SSL.get());
|
2014-04-23 13:42:59 +02:00
|
|
|
|
2014-08-05 08:40:06 +02:00
|
|
|
if (rc > 0)
|
|
|
|
break;
|
2014-05-03 19:56:47 +02:00
|
|
|
|
2014-08-05 08:40:06 +02:00
|
|
|
err = SSL_get_error(m_SSL.get(), rc);
|
|
|
|
}
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2014-04-25 14:33:45 +02:00
|
|
|
switch (err) {
|
2013-04-04 16:08:02 +02:00
|
|
|
case SSL_ERROR_WANT_READ:
|
2014-06-05 16:17:53 +02:00
|
|
|
try {
|
|
|
|
m_Socket->Poll(true, false);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {}
|
2013-04-04 16:08:02 +02:00
|
|
|
continue;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2014-06-05 16:17:53 +02:00
|
|
|
try {
|
|
|
|
m_Socket->Poll(false, true);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {}
|
2013-04-04 16:08:02 +02:00
|
|
|
continue;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
default:
|
2014-08-20 14:07:23 +02:00
|
|
|
msgbuf << "SSL_do_handshake() failed with code " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
|
2014-06-05 15:34:54 +02:00
|
|
|
Log(LogCritical, "TlsStream", msgbuf.str());
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
BOOST_THROW_EXCEPTION(openssl_error()
|
2013-10-18 08:48:15 +02:00
|
|
|
<< boost::errinfo_api_function("SSL_do_handshake")
|
2014-08-20 14:07:23 +02:00
|
|
|
<< errinfo_openssl_error(ERR_peek_error()));
|
2013-04-04 16:08:02 +02:00
|
|
|
}
|
|
|
|
}
|
2012-11-22 12:04:32 +01:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:36:28 +02:00
|
|
|
/**
|
2012-11-22 12:04:32 +01:00
|
|
|
* Processes data for the stream.
|
2012-05-08 15:36:28 +02:00
|
|
|
*/
|
2013-04-04 16:08:02 +02:00
|
|
|
size_t TlsStream::Read(void *buffer, size_t count)
|
2012-04-24 14:02:15 +02:00
|
|
|
{
|
2013-04-04 16:08:02 +02:00
|
|
|
size_t left = count;
|
2014-08-05 11:30:06 +02:00
|
|
|
std::ostringstream msgbuf;
|
|
|
|
char errbuf[120];
|
2013-04-01 16:25:23 +02:00
|
|
|
|
2014-09-05 08:19:47 +02:00
|
|
|
m_Socket->Poll(true, false);
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock alock(m_IOActionLock);
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
while (left > 0) {
|
2014-05-03 19:56:47 +02:00
|
|
|
int rc, err;
|
2014-04-23 13:42:59 +02:00
|
|
|
|
2014-08-05 08:40:06 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_SSLLock);
|
|
|
|
rc = SSL_read(m_SSL.get(), ((char *)buffer) + (count - left), left);
|
2014-05-03 19:56:47 +02:00
|
|
|
|
2014-08-05 08:40:06 +02:00
|
|
|
if (rc <= 0)
|
|
|
|
err = SSL_get_error(m_SSL.get(), rc);
|
|
|
|
}
|
2013-04-01 16:25:23 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
if (rc <= 0) {
|
2014-04-25 14:33:45 +02:00
|
|
|
switch (err) {
|
2012-06-22 11:47:06 +02:00
|
|
|
case SSL_ERROR_WANT_READ:
|
2014-06-05 16:17:53 +02:00
|
|
|
try {
|
|
|
|
m_Socket->Poll(true, false);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {}
|
2013-04-04 16:08:02 +02:00
|
|
|
continue;
|
2012-11-22 12:04:32 +01:00
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2014-06-05 16:17:53 +02:00
|
|
|
try {
|
|
|
|
m_Socket->Poll(false, true);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {}
|
2013-04-04 16:08:02 +02:00
|
|
|
continue;
|
2012-11-22 12:04:32 +01:00
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
Close();
|
2013-04-04 16:08:02 +02:00
|
|
|
return count - left;
|
2012-11-22 12:04:32 +01:00
|
|
|
default:
|
2014-08-20 14:07:23 +02:00
|
|
|
msgbuf << "SSL_read() failed with code " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
|
2014-06-05 15:34:54 +02:00
|
|
|
Log(LogCritical, "TlsStream", msgbuf.str());
|
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
BOOST_THROW_EXCEPTION(openssl_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("SSL_read")
|
2014-08-20 14:07:23 +02:00
|
|
|
<< errinfo_openssl_error(ERR_peek_error()));
|
2012-11-22 12:04:32 +01:00
|
|
|
}
|
2012-08-06 10:01:21 +02:00
|
|
|
}
|
2012-07-16 00:02:31 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
left -= rc;
|
|
|
|
}
|
2012-08-06 10:01:21 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
return count;
|
|
|
|
}
|
2012-07-16 00:02:31 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
void TlsStream::Write(const void *buffer, size_t count)
|
|
|
|
{
|
|
|
|
size_t left = count;
|
2014-08-05 11:30:06 +02:00
|
|
|
std::ostringstream msgbuf;
|
|
|
|
char errbuf[120];
|
2013-04-01 16:25:23 +02:00
|
|
|
|
2014-09-05 08:19:47 +02:00
|
|
|
m_Socket->Poll(false, true);
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock alock(m_IOActionLock);
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
while (left > 0) {
|
2014-05-03 19:56:47 +02:00
|
|
|
int rc, err;
|
2014-04-23 13:42:59 +02:00
|
|
|
|
2014-08-05 08:40:06 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_SSLLock);
|
|
|
|
rc = SSL_write(m_SSL.get(), ((const char *)buffer) + (count - left), left);
|
2014-05-03 19:56:47 +02:00
|
|
|
|
2014-08-05 08:40:06 +02:00
|
|
|
if (rc <= 0)
|
|
|
|
err = SSL_get_error(m_SSL.get(), rc);
|
|
|
|
}
|
2013-04-01 16:25:23 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
if (rc <= 0) {
|
2014-04-25 14:33:45 +02:00
|
|
|
switch (err) {
|
2012-07-16 00:02:31 +02:00
|
|
|
case SSL_ERROR_WANT_READ:
|
2014-06-05 16:17:53 +02:00
|
|
|
try {
|
|
|
|
m_Socket->Poll(true, false);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {}
|
2013-04-04 16:08:02 +02:00
|
|
|
continue;
|
2012-07-16 00:02:31 +02:00
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2014-06-05 16:17:53 +02:00
|
|
|
try {
|
|
|
|
m_Socket->Poll(false, true);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {}
|
2013-04-04 16:08:02 +02:00
|
|
|
continue;
|
2012-07-16 00:02:31 +02:00
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
2012-11-22 12:04:32 +01:00
|
|
|
Close();
|
2012-07-16 00:02:31 +02:00
|
|
|
return;
|
|
|
|
default:
|
2014-08-20 14:07:23 +02:00
|
|
|
msgbuf << "SSL_write() failed with code " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
|
2014-06-05 15:34:54 +02:00
|
|
|
Log(LogCritical, "TlsStream", msgbuf.str());
|
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
BOOST_THROW_EXCEPTION(openssl_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("SSL_write")
|
2014-08-20 14:07:23 +02:00
|
|
|
<< errinfo_openssl_error(ERR_peek_error()));
|
2012-07-16 00:02:31 +02:00
|
|
|
}
|
2012-04-24 14:02:15 +02:00
|
|
|
}
|
2013-04-04 16:08:02 +02:00
|
|
|
|
|
|
|
left -= rc;
|
2012-07-16 00:02:31 +02:00
|
|
|
}
|
2012-04-24 14:02:15 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:36:28 +02:00
|
|
|
/**
|
2012-11-22 12:04:32 +01:00
|
|
|
* Closes the stream.
|
2012-05-08 15:36:28 +02:00
|
|
|
*/
|
2012-11-22 12:04:32 +01:00
|
|
|
void TlsStream::Close(void)
|
2012-04-24 16:27:23 +02:00
|
|
|
{
|
2014-08-05 08:40:06 +02:00
|
|
|
for (int i = 0; i < 5; i++) {
|
2014-05-03 19:56:47 +02:00
|
|
|
int rc, err;
|
|
|
|
|
2014-08-05 09:35:28 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_SSLLock);
|
2014-08-01 14:28:32 +02:00
|
|
|
rc = SSL_shutdown(m_SSL.get());
|
2014-05-03 19:56:47 +02:00
|
|
|
|
2014-08-05 09:35:28 +02:00
|
|
|
if (rc == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (rc > 0)
|
|
|
|
break;
|
2014-05-03 19:56:47 +02:00
|
|
|
|
2014-08-05 09:35:28 +02:00
|
|
|
err = SSL_get_error(m_SSL.get(), rc);
|
|
|
|
}
|
2014-05-03 19:56:47 +02:00
|
|
|
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2014-06-05 16:17:53 +02:00
|
|
|
try {
|
|
|
|
m_Socket->Poll(true, false);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {}
|
2014-05-03 19:56:47 +02:00
|
|
|
continue;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2014-06-05 16:17:53 +02:00
|
|
|
try {
|
|
|
|
m_Socket->Poll(false, true);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {}
|
2014-05-03 19:56:47 +02:00
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
goto close_socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close_socket:
|
2014-04-23 13:42:59 +02:00
|
|
|
m_Socket->Close();
|
2012-11-22 12:04:32 +01:00
|
|
|
}
|
2013-08-27 12:21:41 +02:00
|
|
|
|
|
|
|
bool TlsStream::IsEof(void) const
|
|
|
|
{
|
2014-05-11 06:00:34 +02:00
|
|
|
return (BIO_eof(m_BIO) == 1);
|
2013-08-27 12:21:41 +02:00
|
|
|
}
|