gloox  1.0.10
tlsgnutlsclient.cpp
1 /*
2  Copyright (c) 2005-2013 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 namespace gloox
22 {
23 
24  GnuTLSClient::GnuTLSClient( TLSHandler* th, const std::string& server )
25  : GnuTLSBase( th, server )
26  {
27  }
28 
30  {
31  }
32 
34  {
36  if( m_credentials )
37  gnutls_certificate_free_credentials( m_credentials );
38  init();
39  }
40 
41  bool GnuTLSClient::init( const std::string& /*clientKey*/,
42  const std::string& /*clientCerts*/,
43  const StringList& /*cacerts*/ )
44  {
45  const int protocolPriority[] = {
46 #ifdef GNUTLS_TLS1_2
47  GNUTLS_TLS1_2,
48 #endif
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 };
55 
56  if( m_initLib && gnutls_global_init() != 0 )
57  return false;
58 
59  if( gnutls_certificate_allocate_credentials( &m_credentials ) < 0 )
60  return false;
61 
62  if( gnutls_init( m_session, GNUTLS_CLIENT ) != 0 )
63  {
64  gnutls_certificate_free_credentials( m_credentials );
65  return false;
66  }
67 
68  gnutls_protocol_set_priority( *m_session, protocolPriority );
69  gnutls_cipher_set_priority( *m_session, cipherPriority );
70  gnutls_compression_set_priority( *m_session, compPriority );
71  gnutls_kx_set_priority( *m_session, kxPriority );
72  gnutls_mac_set_priority( *m_session, macPriority );
73  gnutls_credentials_set( *m_session, GNUTLS_CRD_CERTIFICATE, m_credentials );
74 
75  gnutls_transport_set_ptr( *m_session, (gnutls_transport_ptr_t)this );
76  gnutls_transport_set_push_function( *m_session, pushFunc );
77  gnutls_transport_set_pull_function( *m_session, pullFunc );
78 
79  m_valid = true;
80  return true;
81  }
82 
83  void GnuTLSClient::setCACerts( const StringList& cacerts )
84  {
85  m_cacerts = cacerts;
86 
87  StringList::const_iterator it = m_cacerts.begin();
88  for( ; it != m_cacerts.end(); ++it )
89  gnutls_certificate_set_x509_trust_file( m_credentials, (*it).c_str(), GNUTLS_X509_FMT_PEM );
90  }
91 
92  void GnuTLSClient::setClientCert( const std::string& clientKey, const std::string& clientCerts )
93  {
94  m_clientKey = clientKey;
95  m_clientCerts = clientCerts;
96 
97  if( !m_clientKey.empty() && !m_clientCerts.empty() )
98  {
99  gnutls_certificate_set_x509_key_file( m_credentials, m_clientCerts.c_str(),
100  m_clientKey.c_str(), GNUTLS_X509_FMT_PEM );
101  }
102  }
103 
104  void GnuTLSClient::getCertInfo()
105  {
106  unsigned int status;
107  bool error = false;
108 
109  gnutls_certificate_free_ca_names( m_credentials );
110 
111  if( gnutls_certificate_verify_peers2( *m_session, &status ) < 0 )
112  error = true;
113 
114  m_certInfo.status = 0;
115  if( status & GNUTLS_CERT_INVALID )
116  m_certInfo.status |= CertInvalid;
117  if( status & GNUTLS_CERT_SIGNER_NOT_FOUND )
118  m_certInfo.status |= CertSignerUnknown;
119  if( status & GNUTLS_CERT_REVOKED )
120  m_certInfo.status |= CertRevoked;
121  if( status & GNUTLS_CERT_SIGNER_NOT_CA )
122  m_certInfo.status |= CertSignerNotCa;
123  const gnutls_datum_t* certList = 0;
124  unsigned int certListSize = 0;
125  if( !error && ( ( certList = gnutls_certificate_get_peers( *m_session, &certListSize ) ) == 0 ) )
126  error = true;
127 
128  unsigned int certListSizeFull = certListSize;
129 
130  gnutls_x509_crt_t* cert = new gnutls_x509_crt_t[certListSize+1];
131  for( unsigned int i=0; !error && ( i<certListSize ); ++i )
132  {
133  if( gnutls_x509_crt_init( &cert[i] ) < 0
134  || gnutls_x509_crt_import( cert[i], &certList[i], GNUTLS_X509_FMT_DER ) < 0 )
135  error = true;
136  }
137 
138  if( ( gnutls_x509_crt_check_issuer( cert[certListSize-1], cert[certListSize-1] ) > 0 )
139  && certListSize > 0 )
140  certListSize--;
141 
142  bool chain = true;
143  for( unsigned int i=1; !error && ( i<certListSize ); ++i )
144  {
145  chain = error = !verifyAgainst( cert[i-1], cert[i] );
146  }
147  if( !chain )
148  m_certInfo.status |= CertInvalid;
149  m_certInfo.chain = chain;
150 
151  m_certInfo.chain = verifyAgainstCAs( cert[certListSize], 0 /*CAList*/, 0 /*CAListSize*/ );
152 
153  int t = (int)gnutls_x509_crt_get_activation_time( cert[0] );
154  if( t == -1 )
155  error = true;
156  else if( t > time( 0 ) )
157  m_certInfo.status |= CertNotActive;
158  m_certInfo.date_from = t;
159 
160  t = (int)gnutls_x509_crt_get_expiration_time( cert[0] );
161  if( t == -1 )
162  error = true;
163  else if( t < time( 0 ) )
164  m_certInfo.status |= CertExpired;
165  m_certInfo.date_to = t;
166 
167  char name[64];
168  size_t nameSize = sizeof( name );
169  gnutls_x509_crt_get_issuer_dn( cert[0], name, &nameSize );
170  m_certInfo.issuer = name;
171 
172  nameSize = sizeof( name );
173  gnutls_x509_crt_get_dn( cert[0], name, &nameSize );
174  m_certInfo.server = name;
175 
176  const char* info;
177  info = gnutls_compression_get_name( gnutls_compression_get( *m_session ) );
178  if( info )
179  m_certInfo.compression = info;
180 
181  info = gnutls_mac_get_name( gnutls_mac_get( *m_session ) );
182  if( info )
183  m_certInfo.mac = info;
184 
185  info = gnutls_cipher_get_name( gnutls_cipher_get( *m_session ) );
186  if( info )
187  m_certInfo.cipher = info;
188 
189  info = gnutls_protocol_get_name( gnutls_protocol_get_version( *m_session ) );
190  if( info )
191  m_certInfo.protocol = info;
192 
193  if( !gnutls_x509_crt_check_hostname( cert[0], m_server.c_str() ) )
194  m_certInfo.status |= CertWrongPeer;
195 
196  for( unsigned int i = 0; i < certListSizeFull; ++i )
197  gnutls_x509_crt_deinit( cert[i] );
198 
199  delete[] cert;
200 
201  m_valid = true;
202  }
203 
204  static bool verifyCert( gnutls_x509_crt_t cert, unsigned result )
205  {
206  return ! ( ( result & GNUTLS_CERT_INVALID )
207  || gnutls_x509_crt_get_expiration_time( cert ) < time( 0 )
208  || gnutls_x509_crt_get_activation_time( cert ) > time( 0 ) );
209  }
210 
211  bool GnuTLSClient::verifyAgainst( gnutls_x509_crt_t cert, gnutls_x509_crt_t issuer )
212  {
213  unsigned int result;
214  gnutls_x509_crt_verify( cert, &issuer, 1, 0, &result );
215  return verifyCert( cert, result );
216  }
217 
218  bool GnuTLSClient::verifyAgainstCAs( gnutls_x509_crt_t cert, gnutls_x509_crt_t* CAList, int CAListSize )
219  {
220  unsigned int result;
221  gnutls_x509_crt_verify( cert, CAList, CAListSize, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT, &result );
222  return verifyCert( cert, result );
223  }
224 
225 }
226 
227 #endif // HAVE_GNUTLS
std::list< std::string > StringList
Definition: gloox.h:1241
virtual void cleanup()
bool chain
Definition: gloox.h:984
std::string cipher
Definition: gloox.h:992
virtual bool init(const std::string &clientKey=EmptyString, const std::string &clientCerts=EmptyString, const StringList &cacerts=StringList())
std::string issuer
Definition: gloox.h:985
std::string server
Definition: gloox.h:986
std::string mac
Definition: gloox.h:993
The namespace for the gloox library.
Definition: adhoc.cpp:25
int date_from
Definition: gloox.h:987
This is the common base class for (stream) encryption using GnuTLS.
Definition: tlsgnutlsbase.h:38
std::string protocol
Definition: gloox.h:991
GnuTLSClient(TLSHandler *th, const std::string &server)
virtual void setCACerts(const StringList &cacerts)
virtual void setClientCert(const std::string &clientKey, const std::string &clientCerts)
virtual void cleanup()
An interface that allows for interacting with TLS implementations derived from TLSBase.
Definition: tlshandler.h:34
std::string compression
Definition: gloox.h:994