15 #include "tlsgnutlsserver.h"
31 gnutls_certificate_free_credentials( m_x509cred );
32 gnutls_dh_params_deinit( m_dhParams );
42 const std::string& clientCerts,
45 const int protocolPriority[] = {
49 GNUTLS_TLS1_1, GNUTLS_TLS1, 0 };
50 const int kxPriority[] = { GNUTLS_KX_RSA, GNUTLS_KX_DHE_RSA, GNUTLS_KX_DHE_DSS, 0 };
51 const int cipherPriority[] = { GNUTLS_CIPHER_AES_256_CBC, GNUTLS_CIPHER_AES_128_CBC,
52 GNUTLS_CIPHER_3DES_CBC, GNUTLS_CIPHER_ARCFOUR, 0 };
53 const int compPriority[] = { GNUTLS_COMP_ZLIB, GNUTLS_COMP_NULL, 0 };
54 const int macPriority[] = { GNUTLS_MAC_SHA, GNUTLS_MAC_MD5, 0 };
56 if( m_initLib && gnutls_global_init() != 0 )
59 if( gnutls_certificate_allocate_credentials( &m_x509cred ) < 0 )
62 setClientCert( clientKey, clientCerts );
63 setCACerts( cacerts );
66 gnutls_certificate_set_dh_params( m_x509cred, m_dhParams );
67 gnutls_certificate_set_rsa_export_params( m_x509cred, m_rsaParams);
70 if( gnutls_init( m_session, GNUTLS_SERVER ) != 0 )
74 gnutls_protocol_set_priority( *m_session, protocolPriority );
75 gnutls_cipher_set_priority( *m_session, cipherPriority );
76 gnutls_compression_set_priority( *m_session, compPriority );
77 gnutls_kx_set_priority( *m_session, kxPriority );
78 gnutls_mac_set_priority( *m_session, macPriority );
79 gnutls_credentials_set( *m_session, GNUTLS_CRD_CERTIFICATE, m_x509cred );
81 gnutls_certificate_server_set_request( *m_session, GNUTLS_CERT_REQUEST );
83 gnutls_dh_set_prime_bits( *m_session, m_dhBits );
85 gnutls_transport_set_ptr( *m_session, (gnutls_transport_ptr_t)
this );
86 gnutls_transport_set_push_function( *m_session, pushFunc );
87 gnutls_transport_set_pull_function( *m_session, pullFunc );
93 void GnuTLSServer::setCACerts(
const StringList& cacerts )
97 StringList::const_iterator it = m_cacerts.begin();
98 for( ; it != m_cacerts.end(); ++it )
99 gnutls_certificate_set_x509_trust_file( m_x509cred, (*it).c_str(), GNUTLS_X509_FMT_PEM );
102 void GnuTLSServer::setClientCert(
const std::string& clientKey,
const std::string& clientCerts )
104 m_clientKey = clientKey;
105 m_clientCerts = clientCerts;
107 if( !m_clientKey.empty() && !m_clientCerts.empty() )
109 gnutls_certificate_set_x509_key_file( m_x509cred,
110 m_clientCerts.c_str(),
112 GNUTLS_X509_FMT_PEM );
117 void GnuTLSServer::generateDH()
119 gnutls_dh_params_init( &m_dhParams );
120 gnutls_dh_params_generate2( m_dhParams, m_dhBits );
121 gnutls_rsa_params_init( &m_rsaParams );
122 gnutls_rsa_params_generate2( m_rsaParams, 512 );
125 void GnuTLSServer::getCertInfo()
130 info = gnutls_compression_get_name( gnutls_compression_get( *m_session ) );
134 info = gnutls_mac_get_name( gnutls_mac_get( *m_session ) );
136 m_certInfo.
mac = info;
138 info = gnutls_cipher_get_name( gnutls_cipher_get( *m_session ) );
142 info = gnutls_protocol_get_name( gnutls_protocol_get_version( *m_session ) );
151 #endif // HAVE_GNUTLS