15 #include "tlsopensslbase.h"
24 #include <openssl/err.h>
30 :
TLSBase( th, server ), m_ssl( 0 ), m_ctx( 0 ), m_buf( 0 ), m_bufsize( 17000 )
32 m_buf = (
char*)calloc( m_bufsize + 1,
sizeof(
char ) );
39 SSL_CTX_free( m_ctx );
40 SSL_shutdown( m_ssl );
47 const std::string& clientCerts,
53 SSL_COMP_add_compression_method( 193, COMP_zlib() );
55 OpenSSL_add_all_algorithms();
63 if( !SSL_CTX_set_cipher_list( m_ctx,
"HIGH:MEDIUM:AES:@STRENGTH" ) )
66 m_ssl = SSL_new( m_ctx );
70 if( !BIO_new_bio_pair( &m_ibio, 0, &m_nbio, 0 ) )
73 SSL_set_bio( m_ssl, m_ibio, m_ibio );
74 SSL_set_mode( m_ssl, SSL_MODE_AUTO_RETRY | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_ENABLE_PARTIAL_WRITE );
76 ERR_load_crypto_strings();
77 SSL_load_error_strings();
96 doTLSOperation( TLSWrite );
102 m_recvBuffer += data;
110 doTLSOperation( TLSRead );
118 StringList::const_iterator it = m_cacerts.begin();
119 for( ; it != m_cacerts.end(); ++it )
120 SSL_CTX_load_verify_locations( m_ctx, (*it).c_str(), 0 );
125 m_clientKey = clientKey;
126 m_clientCerts = clientCerts;
128 if( !m_clientKey.empty() && !m_clientCerts.empty() )
130 if( SSL_CTX_use_certificate_chain_file( m_ctx, m_clientCerts.c_str() ) != 1 )
134 if( SSL_CTX_use_RSAPrivateKey_file( m_ctx, m_clientKey.c_str(), SSL_FILETYPE_PEM ) != 1 )
140 if ( SSL_CTX_check_private_key( m_ctx ) != 1 )
157 void OpenSSLBase::doTLSOperation( TLSOperation op )
163 bool onceAgain =
false;
170 ret = handshakeFunction();
173 ret = SSL_write( m_ssl, m_sendBuffer.c_str(), m_sendBuffer.length() );
176 ret = SSL_read( m_ssl, m_buf, m_bufsize );
180 switch( SSL_get_error( m_ssl, ret ) )
182 case SSL_ERROR_WANT_READ:
183 case SSL_ERROR_WANT_WRITE:
187 if( op == TLSHandshake )
189 else if( op == TLSWrite )
190 m_sendBuffer.erase( 0, ret );
191 else if( op == TLSRead )
201 if( !onceAgain && !m_recvBuffer.length() )
206 while( ( ( onceAgain || m_recvBuffer.length() ) && ( !m_secure || op == TLSRead ) )
207 || ( ( op == TLSWrite ) && ( ret > 0 ) ));
210 int OpenSSLBase::openSSLTime2UnixTime(
const char* time_string )
216 for(
int n = 0; n < 12; n += 2 )
218 tstring[m] = time_string[n];
219 tstring[m + 1] = time_string[n + 1];
226 time_st.tm_year = ( atoi( &tstring[3 * 0] ) >= 70 ) ? atoi( &tstring[3 * 0] )
227 : atoi( &tstring[3 * 0] ) + 100;
228 time_st.tm_mon = atoi( &tstring[3 * 1] ) - 1;
229 time_st.tm_mday = atoi( &tstring[3 * 2] );
230 time_st.tm_hour = atoi( &tstring[3 * 3] );
231 time_st.tm_min = atoi( &tstring[3 * 4] );
232 time_st.tm_sec = atoi( &tstring[3 * 5] );
234 time_t unixt = mktime( &time_st );
241 doTLSOperation( TLSHandshake );
246 int res = SSL_get_verify_result( m_ssl );
247 if( res != X509_V_OK )
252 X509* peer = SSL_get_peer_certificate( m_ssl );
256 X509_NAME_get_text_by_NID( X509_get_issuer_name( peer ), NID_commonName, peer_CN,
sizeof( peer_CN ) );
257 m_certInfo.
issuer = peer_CN;
258 X509_NAME_get_text_by_NID( X509_get_subject_name( peer ), NID_commonName, peer_CN,
sizeof( peer_CN ) );
259 m_certInfo.
server = peer_CN;
260 m_certInfo.
date_from = openSSLTime2UnixTime( (
char*) (peer->cert_info->validity->notBefore->data) );
261 m_certInfo.
date_to = openSSLTime2UnixTime( (
char*) (peer->cert_info->validity->notAfter->data) );
262 std::string p( peer_CN );
263 std::transform( p.begin(), p.end(), p.begin(), tolower );
267 if( ASN1_UTCTIME_cmp_time_t( X509_get_notBefore( peer ), time( 0 ) ) != -1 )
270 if( ASN1_UTCTIME_cmp_time_t( X509_get_notAfter( peer ), time( 0 ) ) != 1 )
279 tmp = SSL_get_cipher_name( m_ssl );
283 tmp = SSL_get_cipher_version( m_ssl );
287 tmp = SSL_COMP_get_name( SSL_get_current_compression( m_ssl ) );
297 void OpenSSLBase::pushFunc()
304 while( ( wantwrite = BIO_ctrl_pending( m_nbio ) ) > 0 )
306 if( wantwrite > m_bufsize )
307 wantwrite = m_bufsize;
312 frombio = BIO_read( m_nbio, m_buf, wantwrite );
318 while( ( wantread = BIO_ctrl_get_read_request( m_nbio ) ) > 0 )
320 if( wantread > m_recvBuffer.length() )
321 wantread = m_recvBuffer.length();
326 tobio = BIO_write( m_nbio, m_recvBuffer.c_str(), wantread );
327 m_recvBuffer.erase( 0, tobio );
333 #endif // HAVE_OPENSSL