gloox  0.9.9.12
tlsgnutlsserveranon.cpp
1 /*
2  Copyright (c) 2005-2008 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 
15 #include "tlsgnutlsserveranon.h"
16 
17 #ifdef HAVE_GNUTLS
18 
19 #include <errno.h>
20 
21 namespace gloox
22 {
23 
25  : GnuTLSBase( th ), m_dhBits( 1024 )
26  {
27  init();
28  }
29 
31  {
32  gnutls_anon_free_server_credentials( m_anoncred );
33  gnutls_dh_params_deinit( m_dhParams );
34  }
35 
37  {
39  init();
40  }
41 
42  void GnuTLSServerAnon::init()
43  {
44  const int protocolPriority[] = { GNUTLS_TLS1, 0 };
45  const int kxPriority[] = { GNUTLS_KX_ANON_DH, 0 };
46  const int cipherPriority[] = { GNUTLS_CIPHER_AES_256_CBC, GNUTLS_CIPHER_AES_128_CBC,
47  GNUTLS_CIPHER_3DES_CBC, GNUTLS_CIPHER_ARCFOUR, 0 };
48  const int compPriority[] = { GNUTLS_COMP_ZLIB, GNUTLS_COMP_NULL, 0 };
49  const int macPriority[] = { GNUTLS_MAC_SHA, GNUTLS_MAC_MD5, 0 };
50 
51  if( gnutls_global_init() != 0 )
52  return;
53 
54  if( gnutls_anon_allocate_server_credentials( &m_anoncred ) < 0 )
55  return;
56 
57  generateDH();
58  gnutls_anon_set_server_dh_params( m_anoncred, m_dhParams );
59 
60  if( gnutls_init( m_session, GNUTLS_SERVER ) != 0 )
61  return;
62 
63  gnutls_protocol_set_priority( *m_session, protocolPriority );
64  gnutls_cipher_set_priority( *m_session, cipherPriority );
65  gnutls_compression_set_priority( *m_session, compPriority );
66  gnutls_kx_set_priority( *m_session, kxPriority );
67  gnutls_mac_set_priority( *m_session, macPriority );
68  gnutls_credentials_set( *m_session, GNUTLS_CRD_ANON, m_anoncred );
69 
70  gnutls_dh_set_prime_bits( *m_session, m_dhBits );
71 
72  gnutls_transport_set_ptr( *m_session, (gnutls_transport_ptr_t)this );
73  gnutls_transport_set_push_function( *m_session, pushFunc );
74  gnutls_transport_set_pull_function( *m_session, pullFunc );
75  }
76 
77  void GnuTLSServerAnon::generateDH()
78  {
79  gnutls_dh_params_init( &m_dhParams );
80  gnutls_dh_params_generate2( m_dhParams, m_dhBits );
81  }
82 
83  void GnuTLSServerAnon::getCertInfo()
84  {
85  m_certInfo.status = CertOk;
86 
87  const char* info;
88  info = gnutls_compression_get_name( gnutls_compression_get( *m_session ) );
89  if( info )
90  m_certInfo.compression = info;
91 
92  info = gnutls_mac_get_name( gnutls_mac_get( *m_session ) );
93  if( info )
94  m_certInfo.mac = info;
95 
96  info = gnutls_cipher_get_name( gnutls_cipher_get( *m_session ) );
97  if( info )
98  m_certInfo.cipher = info;
99 
100  info = gnutls_protocol_get_name( gnutls_protocol_get_version( *m_session ) );
101  if( info )
102  m_certInfo.protocol = info;
103 
104  m_valid = true;
105  }
106 
107 }
108 
109 #endif // HAVE_GNUTLS