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