gloox  0.9.9.12
tlsgnutlsclientanon.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 "tlsgnutlsclientanon.h"
16 
17 #ifdef HAVE_GNUTLS
18 
19 #include <errno.h>
20 
21 namespace gloox
22 {
23 
25  : GnuTLSBase( th )
26  {
27  init();
28  }
29 
31  {
32  gnutls_anon_free_client_credentials( m_anoncred );
33  }
34 
36  {
38  init();
39  }
40 
41  void GnuTLSClientAnon::init()
42  {
43  const int protocolPriority[] = { GNUTLS_TLS1, 0 };
44  const int kxPriority[] = { GNUTLS_KX_ANON_DH, 0 };
45  const int cipherPriority[] = { GNUTLS_CIPHER_AES_256_CBC, GNUTLS_CIPHER_AES_128_CBC,
46  GNUTLS_CIPHER_3DES_CBC, GNUTLS_CIPHER_ARCFOUR, 0 };
47  const int compPriority[] = { GNUTLS_COMP_ZLIB, GNUTLS_COMP_NULL, 0 };
48  const int macPriority[] = { GNUTLS_MAC_SHA, GNUTLS_MAC_MD5, 0 };
49 
50  if( gnutls_global_init() != 0 )
51  return;
52 
53  if( gnutls_anon_allocate_client_credentials( &m_anoncred ) < 0 )
54  return;
55 
56  if( gnutls_init( m_session, GNUTLS_CLIENT ) != 0 )
57  return;
58 
59  gnutls_protocol_set_priority( *m_session, protocolPriority );
60  gnutls_cipher_set_priority( *m_session, cipherPriority );
61  gnutls_compression_set_priority( *m_session, compPriority );
62  gnutls_kx_set_priority( *m_session, kxPriority );
63  gnutls_mac_set_priority( *m_session, macPriority );
64  gnutls_credentials_set( *m_session, GNUTLS_CRD_ANON, m_anoncred );
65 
66  gnutls_transport_set_ptr( *m_session, (gnutls_transport_ptr_t)this );
67  gnutls_transport_set_push_function( *m_session, pushFunc );
68  gnutls_transport_set_pull_function( *m_session, pullFunc );
69  }
70 
71  void GnuTLSClientAnon::getCertInfo()
72  {
73  m_certInfo.status = CertOk;
74 
75  const char* info;
76  info = gnutls_compression_get_name( gnutls_compression_get( *m_session ) );
77  if( info )
78  m_certInfo.compression = info;
79 
80  info = gnutls_mac_get_name( gnutls_mac_get( *m_session ) );
81  if( info )
82  m_certInfo.mac = info;
83 
84  info = gnutls_cipher_get_name( gnutls_cipher_get( *m_session ) );
85  if( info )
86  m_certInfo.cipher = info;
87 
88  info = gnutls_protocol_get_name( gnutls_protocol_get_version( *m_session ) );
89  if( info )
90  m_certInfo.protocol = info;
91 
92  m_valid = true;
93  }
94 
95 }
96 
97 #endif // HAVE_GNUTLS