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