connectiontls.cpp
/*
* Copyright (c) 2007-2023 by Jakob Schröter <js@camaya.net>
* This file is part of the gloox library. http://camaya.net/gloox
*
* This software is distributed under a license. The full license
* agreement can be found in the file LICENSE in this distribution.
* This software may not be copied, modified, sold or distributed
* other than expressed in the named license agreement.
*
* This software is distributed without any warranty.
*/
#include "connectiontls.h"
#include "tlsdefault.h"
#include <cstdio>
namespace gloox
{
ConnectionTLS::ConnectionTLS( ConnectionDataHandler* cdh, ConnectionBase* conn, const LogSink& log )
: ConnectionBase( cdh ),
m_connection( conn ), m_tls( 0 ), m_tlsHandler( 0 ),
m_log( log )
{
if( m_connection )
m_connection->registerConnectionDataHandler( this );
}
ConnectionTLS::ConnectionTLS( ConnectionBase* conn, const LogSink& log )
: ConnectionBase( 0 ),
m_connection( conn ), m_tls( 0 ), m_tlsHandler( 0 ), m_log( log )
{
if( m_connection )
m_connection->registerConnectionDataHandler( this );
}
ConnectionTLS::~ConnectionTLS()
{
delete m_connection;
delete m_tls;
}
void ConnectionTLS::setConnectionImpl( ConnectionBase* connection )
{
if( m_connection )
{
m_connection->registerConnectionDataHandler( 0 );
delete m_connection;
}
m_connection = connection;
if( m_connection )
m_connection->registerConnectionDataHandler( this );
}
ConnectionError ConnectionTLS::connect()
{
if( !m_connection )
return ConnNotConnected;
if( m_state == StateConnected )
return ConnNoError;
if( !m_tls )
m_tls = getTLSBase( this, m_connection->server() );
if( !m_tls )
return ConnTlsNotAvailable;
if( !m_tls->init( m_clientKey, m_clientCerts, m_cacerts ) )
return ConnTlsFailed;
// m_tls->setCACerts( m_cacerts );
// m_tls->setClientCert( m_clientKey, m_clientCerts );
m_state = StateConnecting;
if( m_connection->state() != StateConnected )
return m_connection->connect();
if( m_tls->handshake() )
return ConnNoError;
else
return ConnTlsFailed;
}
ConnectionError ConnectionTLS::recv( int timeout )
{
return m_connection ? m_connection->recv( timeout ) : ConnNotConnected;
}
bool ConnectionTLS::send( const std::string& data )
{
if( m_state != StateConnected )
return false;
m_tls->encrypt( data );
return true;
}
ConnectionError ConnectionTLS::receive()
{
if( m_connection )
return m_connection->receive();
else
return ConnNotConnected;
}
void ConnectionTLS::disconnect()
{
if( m_connection )
m_connection->disconnect();
cleanup();
}
void ConnectionTLS::cleanup()
{
if( m_connection )
m_connection->cleanup();
if( m_tls )
m_tls->cleanup();
m_state = StateDisconnected;
}
void ConnectionTLS::getStatistics( long int& totalIn, long int& totalOut )
{
if( m_connection )
m_connection->getStatistics( totalIn, totalOut );
}
ConnectionBase* ConnectionTLS::newInstance() const
{
ConnectionBase* newConn = 0;
if( m_connection )
newConn = m_connection->newInstance();
return new ConnectionTLS( m_handler, newConn, m_log );
}
void ConnectionTLS::handleReceivedData( const ConnectionBase* /*connection*/, const std::string& data )
{
if( m_tls )
m_tls->decrypt( data );
}
void ConnectionTLS::handleConnect( const ConnectionBase* /*connection*/ )
{
if( m_tls )
m_tls->handshake();
}
void ConnectionTLS::handleDisconnect( const ConnectionBase* /*connection*/, ConnectionError reason )
{
if( m_handler )
m_handler->handleDisconnect( this, reason );
cleanup();
}
void ConnectionTLS::handleEncryptedData( const TLSBase* /*tls*/, const std::string& data )
{
if( m_connection )
m_connection->send( data );
}
void ConnectionTLS::handleDecryptedData( const TLSBase* /*tls*/, const std::string& data )
{
if( m_handler )
m_handler->handleReceivedData( this, data );
else
{
m_log.log( LogLevelDebug, LogAreaClassConnectionTLS, "Data received and decrypted but no handler" );
}
}
void ConnectionTLS::handleHandshakeResult( const TLSBase* tls, bool success, CertInfo& certinfo )
{
if( success )
{
m_state = StateConnected;
m_log.log( LogLevelDebug, LogAreaClassConnectionTLS, "TLS handshake succeeded" );
if( m_tlsHandler )
m_tlsHandler->handleHandshakeResult( tls, success, certinfo );
if( m_handler )
m_handler->handleConnect( this );
}
else
{
m_state = StateDisconnected;
m_log.log( LogLevelWarning, LogAreaClassConnectionTLS, "TLS handshake failed" );
if( m_tlsHandler )
m_tlsHandler->handleHandshakeResult( tls, success, certinfo );
cleanup();
if( m_handler )
m_handler->handleDisconnect( this, ConnTlsFailed );
}
}
}
gloox::ConnectionBase::m_state
ConnectionState m_state
Definition connectionbase.h:155
gloox::ConnectionBase::newInstance
virtual ConnectionBase * newInstance() const =0
gloox::ConnectionBase::m_handler
ConnectionDataHandler * m_handler
Definition connectionbase.h:152
gloox::ConnectionBase::ConnectionBase
ConnectionBase(ConnectionDataHandler *cdh)
Definition connectionbase.h:42
gloox::ConnectionBase::registerConnectionDataHandler
void registerConnectionDataHandler(ConnectionDataHandler *cdh)
Definition connectionbase.h:102
gloox::ConnectionDataHandler
This is an abstract base class to receive events from a ConnectionBase-derived object.
Definition connectiondatahandler.h:36
gloox::ConnectionTLS::~ConnectionTLS
virtual ~ConnectionTLS()
Definition connectiontls.cpp:39
gloox::ConnectionTLS::handleEncryptedData
virtual void handleEncryptedData(const TLSBase *, const std::string &data)
Definition connectiontls.cpp:164
gloox::ConnectionTLS::ConnectionTLS
ConnectionTLS(ConnectionDataHandler *cdh, ConnectionBase *conn, const LogSink &log)
Definition connectiontls.cpp:22
gloox::ConnectionTLS::handleDisconnect
virtual void handleDisconnect(const ConnectionBase *connection, ConnectionError reason)
Definition connectiontls.cpp:156
gloox::ConnectionTLS::recv
virtual ConnectionError recv(int timeout=-1)
Definition connectiontls.cpp:90
gloox::ConnectionTLS::cleanup
virtual void cleanup()
Definition connectiontls.cpp:120
gloox::ConnectionTLS::handleConnect
virtual void handleConnect(const ConnectionBase *connection)
Definition connectiontls.cpp:150
gloox::ConnectionTLS::handleReceivedData
virtual void handleReceivedData(const ConnectionBase *connection, const std::string &data)
Definition connectiontls.cpp:144
gloox::ConnectionTLS::getTLSBase
virtual TLSBase * getTLSBase(TLSHandler *th, const std::string server)
Definition connectiontls.h:179
gloox::ConnectionTLS::disconnect
virtual void disconnect()
Definition connectiontls.cpp:112
gloox::ConnectionTLS::handleDecryptedData
virtual void handleDecryptedData(const TLSBase *, const std::string &data)
Definition connectiontls.cpp:170
gloox::ConnectionTLS::newInstance
virtual ConnectionBase * newInstance() const
Definition connectiontls.cpp:136
gloox::ConnectionTLS::connect
virtual ConnectionError connect()
Definition connectiontls.cpp:59
gloox::ConnectionTLS::setConnectionImpl
void setConnectionImpl(ConnectionBase *connection)
Definition connectiontls.cpp:45
gloox::ConnectionTLS::handleHandshakeResult
virtual void handleHandshakeResult(const TLSBase *base, bool success, CertInfo &certinfo)
Definition connectiontls.cpp:180
gloox::ConnectionTLS::send
virtual bool send(const std::string &data)
Definition connectiontls.cpp:95
gloox::ConnectionTLS::receive
virtual ConnectionError receive()
Definition connectiontls.cpp:104
gloox::ConnectionTLS::getStatistics
virtual void getStatistics(long int &totalIn, long int &totalOut)
Definition connectiontls.cpp:130
gloox::LogSink
An implementation of log sink and source.
Definition logsink.h:39
gloox::TLSBase
An abstract base class for TLS implementations.
Definition tlsbase.h:32
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::ConnectionError
ConnectionError
Definition gloox.h:684
gloox::ConnNotConnected
@ ConnNotConnected
Definition gloox.h:715
gloox::ConnTlsFailed
@ ConnTlsFailed
Definition gloox.h:705
gloox::ConnNoError
@ ConnNoError
Definition gloox.h:685
gloox::ConnTlsNotAvailable
@ ConnTlsNotAvailable
Definition gloox.h:707
gloox::LogAreaClassConnectionTLS
@ LogAreaClassConnectionTLS
Definition gloox.h:1067
gloox::LogLevelWarning
@ LogLevelWarning
Definition gloox.h:1082
gloox::LogLevelDebug
@ LogLevelDebug
Definition gloox.h:1081
gloox::StateDisconnected
@ StateDisconnected
Definition gloox.h:642
gloox::StateConnected
@ StateConnected
Definition gloox.h:644
gloox::StateConnecting
@ StateConnecting
Definition gloox.h:643
gloox::CertInfo
Definition gloox.h:990