|
|
@ -77,10 +77,8 @@ Connection* SslConnection::clone(const int socket)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
|
Connection *conn = new SslConnection( socket,
|
|
|
|
SslConnection *conn = new SslConnection( socket, m_message->clone(), m_bufferLength );
|
|
|
|
m_message->clone(),
|
|
|
|
conn->initClientContext();
|
|
|
|
m_bufferLength );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -92,13 +90,20 @@ bool SslConnection::connect()
|
|
|
|
if ( !m_tcpConnection.connect() )
|
|
|
|
if ( !m_tcpConnection.connect() )
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
if ( !initHandlers() )
|
|
|
|
// if ( !initHandlers() )
|
|
|
|
return false;
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( SSL_set_fd(m_sslHandle, m_tcpConnection.getSocket() ) == 0 ) {
|
|
|
|
|
|
|
|
getSslError("SSL set connection socket failed. ");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOG( Logger::INFO, "itt" );
|
|
|
|
if ( SSL_connect (m_sslHandle) != 1 ) {
|
|
|
|
if ( SSL_connect (m_sslHandle) != 1 ) {
|
|
|
|
LOG (Logger::ERR, getSslError("SSL handshake failed. ").c_str() );
|
|
|
|
LOG (Logger::ERR, getSslError("SSL handshake failed. ").c_str() );
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG( Logger::INFO, "de itt mar nem?" );
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -111,8 +116,8 @@ bool SslConnection::bind()
|
|
|
|
if ( !m_tcpConnection.bind() )
|
|
|
|
if ( !m_tcpConnection.bind() )
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
if ( !initHandlers() )
|
|
|
|
// if ( !initHandlers() )
|
|
|
|
return false;
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
@ -134,16 +139,22 @@ int SslConnection::accept()
|
|
|
|
if ( client_socket == -1)
|
|
|
|
if ( client_socket == -1)
|
|
|
|
return client_socket;
|
|
|
|
return client_socket;
|
|
|
|
|
|
|
|
|
|
|
|
if ( SSL_accept(m_sslHandle) == -1 ) {
|
|
|
|
LOG( Logger::INFO, "server itt");
|
|
|
|
getSslError("SSL accept failed. ");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( SSL_set_fd(m_sslHandle, client_socket) == 0 ) {
|
|
|
|
if ( SSL_set_fd(m_sslHandle, client_socket) == 0 ) {
|
|
|
|
getSslError("SSL set connection socket failed. ");
|
|
|
|
getSslError("SSL set connection socket failed. ");
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOG( Logger::INFO, "server itt 2");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( SSL_accept(m_sslHandle) == -1 ) {
|
|
|
|
|
|
|
|
getSslError("SSL accept failed. ");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOG( Logger::INFO, "server itt 3");
|
|
|
|
|
|
|
|
|
|
|
|
return client_socket;
|
|
|
|
return client_socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -187,6 +198,38 @@ bool SslConnection::disconnect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool SslConnection::initServerContext( const std::string certificateFile,
|
|
|
|
|
|
|
|
const std::string privateKeyFile )
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_sslContext = SSL_CTX_new (SSLv2_server_method ());
|
|
|
|
|
|
|
|
if ( m_sslContext == NULL ) {
|
|
|
|
|
|
|
|
LOG (Logger::ERR, getSslError("Creating SSL context failed. ").c_str() );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( !loadCertificates(certificateFile, privateKeyFile) )
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return initHandle();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool SslConnection::initClientContext()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_sslContext = SSL_CTX_new (SSLv23_client_method ());
|
|
|
|
|
|
|
|
if ( m_sslContext == NULL ) {
|
|
|
|
|
|
|
|
LOG (Logger::ERR, getSslError("Creating SSL context failed. ").c_str() );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return initHandle();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool SslConnection::send( const void* message, const size_t length )
|
|
|
|
bool SslConnection::send( const void* message, const size_t length )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
@ -234,16 +277,10 @@ int SslConnection::getSocket() const
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool SslConnection::initHandlers()
|
|
|
|
bool SslConnection::initHandle()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
|
m_sslContext = SSL_CTX_new (SSLv23_client_method ());
|
|
|
|
|
|
|
|
if ( m_sslContext == NULL ) {
|
|
|
|
|
|
|
|
LOG (Logger::ERR, getSslError("Creating SSL context failed. ").c_str() );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_sslHandle = SSL_new (m_sslContext);
|
|
|
|
m_sslHandle = SSL_new (m_sslContext);
|
|
|
|
if ( m_sslHandle == NULL ) {
|
|
|
|
if ( m_sslHandle == NULL ) {
|
|
|
|
LOG (Logger::ERR, getSslError("Creating SSL structure for connection failed. ").c_str() );
|
|
|
|
LOG (Logger::ERR, getSslError("Creating SSL structure for connection failed. ").c_str() );
|
|
|
@ -269,3 +306,51 @@ std::string SslConnection::getSslError(const std::string &msg)
|
|
|
|
|
|
|
|
|
|
|
|
return std::string(msg).append(buffer);
|
|
|
|
return std::string(msg).append(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool SslConnection::loadCertificates( const std::string certificateFile,
|
|
|
|
|
|
|
|
const std::string privateKeyFile )
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if ( SSL_CTX_use_certificate_file(m_sslContext, certificateFile.c_str(), SSL_FILETYPE_PEM) != 1 )
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
getSslError("SSL certificate file loading failed. ");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( SSL_CTX_use_PrivateKey_file(m_sslContext, privateKeyFile.c_str(), SSL_FILETYPE_PEM) != 1 )
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
getSslError("SSL private Key file loading failed. ");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( SSL_CTX_check_private_key(m_sslContext) != 1 )
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
LOG( Logger::ERR, "Private key does not match the public certificate\n");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*--- ShowCerts - print out certificates. ---*/
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// void showCertificates(SSL* ssl)
|
|
|
|
|
|
|
|
// { X509 *cert;
|
|
|
|
|
|
|
|
// char *line;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// cert = SSL_get_peer_certificate(ssl); /* Get certificates (if available) */
|
|
|
|
|
|
|
|
// if ( cert != NULL )
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// printf("Server certificates:\n");
|
|
|
|
|
|
|
|
// line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
|
|
|
|
|
|
|
|
// printf("Subject: %s\n", line);
|
|
|
|
|
|
|
|
// free(line);
|
|
|
|
|
|
|
|
// line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
|
|
|
|
|
|
|
|
// printf("Issuer: %s\n", line);
|
|
|
|
|
|
|
|
// free(line);
|
|
|
|
|
|
|
|
// X509_free(cert);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// else
|
|
|
|
|
|
|
|
// printf("No certificates.\n");
|
|
|
|
|
|
|
|
// }
|
|
|
|