gloox  1.0
tlsdefault.cpp
1 /*
2  * Copyright (c) 2007-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 #include "tlsdefault.h"
14 
15 #include "tlshandler.h"
16 
17 #include "config.h"
18 
19 #if defined( HAVE_GNUTLS )
20 # define HAVE_TLS
21 # include "tlsgnutlsclient.h"
22 # include "tlsgnutlsclientanon.h"
23 # include "tlsgnutlsserveranon.h"
24 #elif defined( HAVE_OPENSSL )
25 # define HAVE_TLS
26 # include "tlsopensslclient.h"
27 #ifndef __SYMBIAN32__
28 # include "tlsopensslserver.h"
29 #endif
30 #elif defined( HAVE_WINTLS )
31 # define HAVE_TLS
32 # include "tlsschannel.h"
33 #endif
34 
35 namespace gloox
36 {
37 
38  TLSDefault::TLSDefault( TLSHandler* th, const std::string server, Type type )
39  : TLSBase( th, server ), m_impl( 0 )
40  {
41  switch( type )
42  {
43  case VerifyingClient:
44 #ifdef HAVE_GNUTLS
45  m_impl = new GnuTLSClient( th, server );
46 #elif defined( HAVE_OPENSSL )
47  m_impl = new OpenSSLClient( th, server );
48 #elif defined( HAVE_WINTLS )
49  m_impl = new SChannel( th, server );
50 #endif
51  break;
52  case AnonymousClient:
53 #ifdef HAVE_GNUTLS
54  m_impl = new GnuTLSClientAnon( th );
55 #endif
56  break;
57  case AnonymousServer:
58 #ifdef HAVE_GNUTLS
59  m_impl = new GnuTLSServerAnon( th );
60 #endif
61  break;
62  case VerifyingServer:
63 #ifdef HAVE_OPENSSL
64 #ifndef __SYMBIAN32__
65  m_impl = new OpenSSLServer( th );
66 #endif
67 #endif
68  break;
69  default:
70  break;
71  }
72  }
73 
75  {
76  delete m_impl;
77  }
78 
79  bool TLSDefault::init( const std::string& clientKey,
80  const std::string& clientCerts,
81  const StringList& cacerts )
82  {
83  return m_impl ? m_impl->init( clientKey, clientCerts,
84  cacerts ) : false;
85  }
86 
88  {
89  int types = 0;
90 #ifdef HAVE_GNUTLS
91  types |= VerifyingClient;
92  types |= AnonymousClient;
93  types |= AnonymousServer;
94 #elif defined( HAVE_OPENSSL )
95  types |= VerifyingClient;
96  types |= VerifyingServer;
97 #elif defined( HAVE_WINTLS )
98  types |= VerifyingClient;
99 #endif
100  return types;
101  }
102 
103  bool TLSDefault::encrypt( const std::string& data )
104  {
105  return m_impl ? m_impl->encrypt( data ) : false;
106  }
107 
108  int TLSDefault::decrypt( const std::string& data )
109  {
110  return m_impl ? m_impl->decrypt( data ) : 0;
111  }
112 
114  {
115  if( m_impl )
116  m_impl->cleanup();
117  }
118 
120  {
121  return m_impl ? m_impl->handshake() : false;
122  }
123 
124  bool TLSDefault::isSecure() const
125  {
126  return m_impl ? m_impl->isSecure() : false;
127  }
128 
129  void TLSDefault::setCACerts( const StringList& cacerts )
130  {
131  if( m_impl )
132  m_impl->setCACerts( cacerts );
133  }
134 
136  {
137  return m_impl ? m_impl->fetchTLSInfo() : m_certInfo;
138  }
139 
140  void TLSDefault::setClientCert( const std::string& clientKey, const std::string& clientCerts )
141  {
142  if( m_impl )
143  m_impl->setClientCert( clientKey, clientCerts );
144  }
145 
146 }