15 #include "tlsgnutlsclient.h"
23 GCRY_THREAD_OPTION_PTHREAD_IMPL;
43 gnutls_certificate_free_credentials( m_credentials );
51 gcry_control( GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread );
53 const int protocolPriority[] = {
57 GNUTLS_TLS1_1, GNUTLS_TLS1, 0 };
58 const int kxPriority[] = { GNUTLS_KX_RSA, GNUTLS_KX_DHE_RSA, GNUTLS_KX_DHE_DSS, 0 };
59 const int cipherPriority[] = { GNUTLS_CIPHER_AES_256_CBC, GNUTLS_CIPHER_AES_128_CBC,
60 GNUTLS_CIPHER_3DES_CBC, GNUTLS_CIPHER_ARCFOUR, 0 };
61 const int compPriority[] = { GNUTLS_COMP_ZLIB, GNUTLS_COMP_NULL, 0 };
62 const int macPriority[] = { GNUTLS_MAC_SHA, GNUTLS_MAC_MD5, 0 };
64 if( m_initLib && gnutls_global_init() != 0 )
67 if( gnutls_certificate_allocate_credentials( &m_credentials ) < 0 )
70 if( gnutls_init( m_session, GNUTLS_CLIENT ) != 0 )
72 gnutls_certificate_free_credentials( m_credentials );
76 gnutls_protocol_set_priority( *m_session, protocolPriority );
77 gnutls_cipher_set_priority( *m_session, cipherPriority );
78 gnutls_compression_set_priority( *m_session, compPriority );
79 gnutls_kx_set_priority( *m_session, kxPriority );
80 gnutls_mac_set_priority( *m_session, macPriority );
81 gnutls_credentials_set( *m_session, GNUTLS_CRD_CERTIFICATE, m_credentials );
83 gnutls_transport_set_ptr( *m_session, (gnutls_transport_ptr_t)
this );
84 gnutls_transport_set_push_function( *m_session, pushFunc );
85 gnutls_transport_set_pull_function( *m_session, pullFunc );
95 StringList::const_iterator it = m_cacerts.begin();
96 for( ; it != m_cacerts.end(); ++it )
97 gnutls_certificate_set_x509_trust_file( m_credentials, (*it).c_str(), GNUTLS_X509_FMT_PEM );
102 m_clientKey = clientKey;
103 m_clientCerts = clientCerts;
105 if( !m_clientKey.empty() && !m_clientCerts.empty() )
107 gnutls_certificate_set_x509_key_file( m_credentials, m_clientCerts.c_str(),
108 m_clientKey.c_str(), GNUTLS_X509_FMT_PEM );
112 void GnuTLSClient::getCertInfo()
117 gnutls_certificate_free_ca_names( m_credentials );
119 if( gnutls_certificate_verify_peers2( *m_session, &status ) < 0 )
123 if( status & GNUTLS_CERT_INVALID )
125 if( status & GNUTLS_CERT_SIGNER_NOT_FOUND )
127 if( status & GNUTLS_CERT_REVOKED )
129 if( status & GNUTLS_CERT_SIGNER_NOT_CA )
131 const gnutls_datum_t* certList = 0;
132 unsigned int certListSize = 0;
133 if( !error && ( ( certList = gnutls_certificate_get_peers( *m_session, &certListSize ) ) == 0 ) )
136 unsigned int certListSizeFull = certListSize;
138 gnutls_x509_crt_t* cert =
new gnutls_x509_crt_t[certListSize+1];
139 for(
unsigned int i=0; !error && ( i<certListSize ); ++i )
141 if( gnutls_x509_crt_init( &cert[i] ) < 0
142 || gnutls_x509_crt_import( cert[i], &certList[i], GNUTLS_X509_FMT_DER ) < 0 )
146 if( ( gnutls_x509_crt_check_issuer( cert[certListSize-1], cert[certListSize-1] ) > 0 )
147 && certListSize > 0 )
151 for(
unsigned int i=1; !error && ( i<certListSize ); ++i )
153 chain = error = !verifyAgainst( cert[i-1], cert[i] );
157 m_certInfo.
chain = chain;
159 m_certInfo.
chain = verifyAgainstCAs( cert[certListSize], 0 , 0 );
161 int t = (int)gnutls_x509_crt_get_activation_time( cert[0] );
164 else if( t > time( 0 ) )
168 t = (
int)gnutls_x509_crt_get_expiration_time( cert[0] );
171 else if( t < time( 0 ) )
176 size_t nameSize =
sizeof( name );
177 gnutls_x509_crt_get_issuer_dn( cert[0], name, &nameSize );
180 nameSize =
sizeof( name );
181 gnutls_x509_crt_get_dn( cert[0], name, &nameSize );
185 info = gnutls_compression_get_name( gnutls_compression_get( *m_session ) );
189 info = gnutls_mac_get_name( gnutls_mac_get( *m_session ) );
191 m_certInfo.
mac = info;
193 info = gnutls_cipher_get_name( gnutls_cipher_get( *m_session ) );
197 info = gnutls_protocol_get_name( gnutls_protocol_get_version( *m_session ) );
201 if( !gnutls_x509_crt_check_hostname( cert[0], m_server.c_str() ) )
204 for(
unsigned int i = 0; i < certListSizeFull; ++i )
205 gnutls_x509_crt_deinit( cert[i] );
212 static bool verifyCert( gnutls_x509_crt_t cert,
unsigned result )
214 return ! ( ( result & GNUTLS_CERT_INVALID )
215 || gnutls_x509_crt_get_expiration_time( cert ) < time( 0 )
216 || gnutls_x509_crt_get_activation_time( cert ) > time( 0 ) );
219 bool GnuTLSClient::verifyAgainst( gnutls_x509_crt_t cert, gnutls_x509_crt_t issuer )
222 gnutls_x509_crt_verify( cert, &issuer, 1, 0, &result );
223 return verifyCert( cert, result );
226 bool GnuTLSClient::verifyAgainstCAs( gnutls_x509_crt_t cert, gnutls_x509_crt_t* CAList,
int CAListSize )
229 gnutls_x509_crt_verify( cert, CAList, CAListSize, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT, &result );
230 return verifyCert( cert, result );
235 #endif // HAVE_GNUTLS