gloox  1.1-svn
tlsgnutlsclient.cpp
1 /*
2  Copyright (c) 2005-2009 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 "tlsgnutlsclient.h"
16 
17 #ifdef HAVE_GNUTLS
18 
19 #include <errno.h>
20 
21 #ifdef HAVE_PTHREAD
22 extern "C" {
23 GCRY_THREAD_OPTION_PTHREAD_IMPL;
24 }
25 #endif
26 
27 namespace gloox
28 {
29 
30  GnuTLSClient::GnuTLSClient( TLSHandler* th, const std::string& server )
31  : GnuTLSBase( th, server )
32  {
33  }
34 
36  {
37  }
38 
40  {
42  init();
43  }
44 
45  bool GnuTLSClient::init( const std::string& /*clientKey*/,
46  const std::string& /*clientCerts*/,
47  const StringList& /*cacerts*/ )
48  {
49  gcry_control( GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread );
50 
51  const int protocolPriority[] = {
52 #ifdef GNUTLS_TLS1_2
53  GNUTLS_TLS1_2,
54 #endif
55  GNUTLS_TLS1_1, GNUTLS_TLS1, 0 };
56  const int kxPriority[] = { GNUTLS_KX_RSA, GNUTLS_KX_DHE_RSA, GNUTLS_KX_DHE_DSS, 0 };
57  const int cipherPriority[] = { GNUTLS_CIPHER_AES_256_CBC, GNUTLS_CIPHER_AES_128_CBC,
58  GNUTLS_CIPHER_3DES_CBC, GNUTLS_CIPHER_ARCFOUR, 0 };
59  const int compPriority[] = { GNUTLS_COMP_ZLIB, GNUTLS_COMP_NULL, 0 };
60  const int macPriority[] = { GNUTLS_MAC_SHA, GNUTLS_MAC_MD5, 0 };
61 
62  if( m_initLib && gnutls_global_init() != 0 )
63  return false;
64 
65  if( gnutls_certificate_allocate_credentials( &m_credentials ) < 0 )
66  return false;
67 
68  if( gnutls_init( m_session, GNUTLS_CLIENT ) != 0 )
69  {
70  gnutls_certificate_free_credentials( m_credentials );
71  return false;
72  }
73 
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_credentials );
80 
81  gnutls_transport_set_ptr( *m_session, (gnutls_transport_ptr_t)this );
82  gnutls_transport_set_push_function( *m_session, pushFunc );
83  gnutls_transport_set_pull_function( *m_session, pullFunc );
84 
85  m_valid = true;
86  return true;
87  }
88 
89  void GnuTLSClient::setCACerts( const StringList& cacerts )
90  {
91  m_cacerts = cacerts;
92 
93  StringList::const_iterator it = m_cacerts.begin();
94  for( ; it != m_cacerts.end(); ++it )
95  gnutls_certificate_set_x509_trust_file( m_credentials, (*it).c_str(), GNUTLS_X509_FMT_PEM );
96  }
97 
98  void GnuTLSClient::setClientCert( const std::string& clientKey, const std::string& clientCerts )
99  {
100  m_clientKey = clientKey;
101  m_clientCerts = clientCerts;
102 
103  if( !m_clientKey.empty() && !m_clientCerts.empty() )
104  {
105  gnutls_certificate_set_x509_key_file( m_credentials, m_clientCerts.c_str(),
106  m_clientKey.c_str(), GNUTLS_X509_FMT_PEM );
107  }
108  }
109 
110  void GnuTLSClient::getCertInfo()
111  {
112  unsigned int status;
113  bool error = false;
114 
115  gnutls_certificate_free_ca_names( m_credentials );
116 
117  if( gnutls_certificate_verify_peers2( *m_session, &status ) < 0 )
118  error = true;
119 
120  m_certInfo.status = 0;
121  if( status & GNUTLS_CERT_INVALID )
122  m_certInfo.status |= CertInvalid;
123  if( status & GNUTLS_CERT_SIGNER_NOT_FOUND )
124  m_certInfo.status |= CertSignerUnknown;
125  if( status & GNUTLS_CERT_REVOKED )
126  m_certInfo.status |= CertRevoked;
127  if( status & GNUTLS_CERT_SIGNER_NOT_CA )
128  m_certInfo.status |= CertSignerNotCa;
129  const gnutls_datum_t* certList = 0;
130  unsigned int certListSize;
131  if( !error && ( ( certList = gnutls_certificate_get_peers( *m_session, &certListSize ) ) == 0 ) )
132  error = true;
133 
134  gnutls_x509_crt_t* cert = new gnutls_x509_crt_t[certListSize+1];
135  for( unsigned int i=0; !error && ( i<certListSize ); ++i )
136  {
137  if( gnutls_x509_crt_init( &cert[i] ) < 0
138  || gnutls_x509_crt_import( cert[i], &certList[i], GNUTLS_X509_FMT_DER ) < 0 )
139  error = true;
140  }
141 
142  if( ( gnutls_x509_crt_check_issuer( cert[certListSize-1], cert[certListSize-1] ) > 0 )
143  && certListSize > 0 )
144  certListSize--;
145 
146  bool chain = true;
147  for( unsigned int i=1; !error && ( i<certListSize ); ++i )
148  {
149  chain = error = !verifyAgainst( cert[i-1], cert[i] );
150  }
151  if( !chain )
152  m_certInfo.status |= CertInvalid;
153  m_certInfo.chain = chain;
154 
155  m_certInfo.chain = verifyAgainstCAs( cert[certListSize], 0 /*CAList*/, 0 /*CAListSize*/ );
156 
157  int t = (int)gnutls_x509_crt_get_activation_time( cert[0] );
158  if( t == -1 )
159  error = true;
160  else if( t > time( 0 ) )
161  m_certInfo.status |= CertNotActive;
162  m_certInfo.date_from = t;
163 
164  t = (int)gnutls_x509_crt_get_expiration_time( cert[0] );
165  if( t == -1 )
166  error = true;
167  else if( t < time( 0 ) )
168  m_certInfo.status |= CertExpired;
169  m_certInfo.date_to = t;
170 
171  char name[64];
172  size_t nameSize = sizeof( name );
173  gnutls_x509_crt_get_issuer_dn( cert[0], name, &nameSize );
174  m_certInfo.issuer = name;
175 
176  nameSize = sizeof( name );
177  gnutls_x509_crt_get_dn( cert[0], name, &nameSize );
178  m_certInfo.server = name;
179 
180  const char* info;
181  info = gnutls_compression_get_name( gnutls_compression_get( *m_session ) );
182  if( info )
183  m_certInfo.compression = info;
184 
185  info = gnutls_mac_get_name( gnutls_mac_get( *m_session ) );
186  if( info )
187  m_certInfo.mac = info;
188 
189  info = gnutls_cipher_get_name( gnutls_cipher_get( *m_session ) );
190  if( info )
191  m_certInfo.cipher = info;
192 
193  info = gnutls_protocol_get_name( gnutls_protocol_get_version( *m_session ) );
194  if( info )
195  m_certInfo.protocol = info;
196 
197  if( !gnutls_x509_crt_check_hostname( cert[0], m_server.c_str() ) )
198  m_certInfo.status |= CertWrongPeer;
199 
200  for( unsigned int i = 0; i < certListSize; ++i )
201  gnutls_x509_crt_deinit( cert[i] );
202 
203  delete[] cert;
204 
205  m_valid = true;
206  }
207 
208  static bool verifyCert( gnutls_x509_crt_t cert, unsigned result )
209  {
210  return ! ( ( result & GNUTLS_CERT_INVALID )
211  || gnutls_x509_crt_get_expiration_time( cert ) < time( 0 )
212  || gnutls_x509_crt_get_activation_time( cert ) > time( 0 ) );
213  }
214 
215  bool GnuTLSClient::verifyAgainst( gnutls_x509_crt_t cert, gnutls_x509_crt_t issuer )
216  {
217  unsigned int result;
218  gnutls_x509_crt_verify( cert, &issuer, 1, 0, &result );
219  return verifyCert( cert, result );
220  }
221 
222  bool GnuTLSClient::verifyAgainstCAs( gnutls_x509_crt_t cert, gnutls_x509_crt_t* CAList, int CAListSize )
223  {
224  unsigned int result;
225  gnutls_x509_crt_verify( cert, CAList, CAListSize, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT, &result );
226  return verifyCert( cert, result );
227  }
228 
229 }
230 
231 #endif // HAVE_GNUTLS