15 #include "tlsgnutlsbase.h"
28 :
TLSBase( th, server ), m_session( new gnutls_session_t ), m_buf( 0 ), m_bufsize( 17000 )
30 m_buf =
static_cast<char*
>( calloc( m_bufsize + 1,
sizeof(
char ) ) );
54 std::string::size_type sum = 0;
57 ret = gnutls_record_send( *m_session, data.c_str() + sum, data.length() - sum );
60 while( ( ret == GNUTLS_E_AGAIN ) || ( ret == GNUTLS_E_INTERRUPTED ) || sum < data.length() );
71 return static_cast<int>( data.length() );
79 if( ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
84 ret =
static_cast<int>( gnutls_record_recv( *m_session, m_buf, m_bufsize ) );
85 if( ret > 0 && m_handler )
93 while( ret > 0 || ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED );
105 gnutls_bye( *m_session, GNUTLS_SHUT_RDWR );
106 gnutls_db_remove_session( *m_session );
107 gnutls_credentials_clear( *m_session );
109 gnutls_deinit( *m_session );
116 m_session =
new gnutls_session_t;
127 int ret = gnutls_handshake( *m_session );
128 if( ret < 0 && gnutls_error_is_fatal( ret ) )
130 gnutls_perror( ret );
131 gnutls_db_remove_session( *m_session );
137 else if( ret == GNUTLS_E_AGAIN )
152 #ifdef HAVE_GNUTLS_SESSION_CHANNEL_BINDING
161 #ifdef HAVE_GNUTLS_SESSION_CHANNEL_BINDING
164 rc = gnutls_session_channel_binding( *m_session,
165 #ifdef GNUTLS_CB_TLS_EXPORTER
166 ( m_certInfo.
protocol ==
"TLSv1.3" ) ? GNUTLS_CB_TLS_EXPORTER : GNUTLS_CB_TLS_UNIQUE,
168 GNUTLS_CB_TLS_UNIQUE,
172 return std::string(
reinterpret_cast<char*
>( cb.data ), cb.size );
178 ssize_t GnuTLSBase::pullFunc(
void* data,
size_t len )
180 ssize_t cpy = ( len > m_recvBuffer.length() ) ? ( m_recvBuffer.length() ) : ( len );
183 memcpy( data,
static_cast<const void*
>( m_recvBuffer.c_str() ), cpy );
184 m_recvBuffer.erase( 0, cpy );
190 return GNUTLS_E_AGAIN;
194 ssize_t GnuTLSBase::pullFunc( gnutls_transport_ptr_t ptr,
void* data,
size_t len )
196 return static_cast<GnuTLSBase*
>( ptr )->pullFunc( data, len );
199 ssize_t GnuTLSBase::pushFunc(
const void* data,
size_t len )
202 m_handler->
handleEncryptedData(
this, std::string(
static_cast<const char*
>( data ), len ) );
207 ssize_t GnuTLSBase::pushFunc( gnutls_transport_ptr_t ptr,
const void* data,
size_t len )
209 return static_cast<GnuTLSBase*
>( ptr )->pushFunc( data, len );
212 void GnuTLSBase::getCommonCertInfo()
215 info = gnutls_compression_get_name( gnutls_compression_get( *m_session ) );
219 info = gnutls_mac_get_name( gnutls_mac_get( *m_session ) );
221 m_certInfo.
mac = info;
223 info = gnutls_cipher_get_name( gnutls_cipher_get( *m_session ) );
227 switch( gnutls_protocol_get_version( *m_session ) )
251 m_certInfo.
protocol =
"Unknown protocol";
virtual bool encrypt(const std::string &data)
virtual const std::string channelBinding() const
virtual bool hasChannelBinding() const
GnuTLSBase(TLSHandler *th, const std::string &server=EmptyString)
virtual int decrypt(const std::string &data)
An abstract base class for TLS implementations.
An interface that allows for interacting with TLS implementations derived from TLSBase.
virtual void handleDecryptedData(const TLSBase *base, const std::string &data)=0
virtual void handleEncryptedData(const TLSBase *base, const std::string &data)=0
virtual void handleHandshakeResult(const TLSBase *base, bool success, CertInfo &certinfo)=0
The namespace for the gloox library.
const std::string EmptyString