gloox  1.0.22
tlsdefault.cpp
1 /*
2  * Copyright (c) 2007-2017 by Jakob Schröter <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
94 #elif defined( HAVE_OPENSSL )
97 #elif defined( HAVE_WINTLS )
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 
130  {
131  return m_impl ? m_impl->hasChannelBinding() : false;
132  }
133 
134  const std::string TLSDefault::channelBinding() const
135  {
136  return m_impl ? m_impl->channelBinding() : EmptyString;
137  }
138 
139  void TLSDefault::setCACerts( const StringList& cacerts )
140  {
141  if( m_impl )
142  m_impl->setCACerts( cacerts );
143  }
144 
146  {
147  return m_impl ? m_impl->fetchTLSInfo() : m_certInfo;
148  }
149 
150  void TLSDefault::setClientCert( const std::string& clientKey, const std::string& clientCerts )
151  {
152  if( m_impl )
153  m_impl->setClientCert( clientKey, clientCerts );
154  }
155 
156 }
gloox::TLSDefault::init
virtual bool init(const std::string &clientKey=EmptyString, const std::string &clientCerts=EmptyString, const StringList &cacerts=StringList())
Definition: tlsdefault.cpp:79
gloox::TLSBase::fetchTLSInfo
virtual const CertInfo & fetchTLSInfo() const
Definition: tlsbase.h:130
gloox::TLSDefault::VerifyingClient
@ VerifyingClient
Definition: tlsdefault.h:42
gloox::TLSBase::encrypt
virtual bool encrypt(const std::string &data)=0
gloox::TLSHandler
An interface that allows for interacting with TLS implementations derived from TLSBase.
Definition: tlshandler.h:35
gloox::OpenSSLServer
Definition: tlsopensslserver.h:36
gloox::TLSDefault::TLSDefault
TLSDefault(TLSHandler *th, const std::string server, Type type=VerifyingClient)
Definition: tlsdefault.cpp:38
gloox::TLSDefault::AnonymousServer
@ AnonymousServer
Definition: tlsdefault.h:47
gloox::TLSDefault::decrypt
virtual int decrypt(const std::string &data)
Definition: tlsdefault.cpp:108
gloox::GnuTLSServerAnon
This class implements (stream) encryption using GnuTLS server-side.
Definition: tlsgnutlsserveranon.h:39
gloox::OpenSSLClient
Definition: tlsopensslclient.h:36
gloox::GnuTLSClientAnon
This class implements an anonymous TLS backend using GnuTLS.
Definition: tlsgnutlsclientanon.h:39
gloox::TLSBase::setCACerts
virtual void setCACerts(const StringList &cacerts)=0
gloox::TLSDefault::types
static int types()
Definition: tlsdefault.cpp:87
gloox::TLSDefault::isSecure
virtual bool isSecure() const
Definition: tlsdefault.cpp:124
gloox::TLSBase::isSecure
virtual bool isSecure() const
Definition: tlsbase.h:105
gloox::TLSDefault::~TLSDefault
virtual ~TLSDefault()
Definition: tlsdefault.cpp:74
gloox::TLSDefault::fetchTLSInfo
virtual const CertInfo & fetchTLSInfo() const
Definition: tlsdefault.cpp:145
gloox::TLSBase::channelBinding
virtual const std::string channelBinding() const
Definition: tlsbase.h:117
gloox::StringList
std::list< std::string > StringList
Definition: gloox.h:1251
gloox::TLSBase::cleanup
virtual void cleanup()=0
gloox::TLSDefault::channelBinding
virtual const std::string channelBinding() const
Definition: tlsdefault.cpp:134
gloox::TLSDefault::handshake
virtual bool handshake()
Definition: tlsdefault.cpp:119
gloox::GnuTLSClient
This class implements a TLS backend using GnuTLS.
Definition: tlsgnutlsclient.h:39
gloox::TLSDefault::cleanup
virtual void cleanup()
Definition: tlsdefault.cpp:113
gloox::TLSBase::setClientCert
virtual void setClientCert(const std::string &clientKey, const std::string &clientCerts)=0
gloox::CertInfo
Definition: gloox.h:990
gloox::TLSDefault::hasChannelBinding
virtual bool hasChannelBinding() const
Definition: tlsdefault.cpp:129
gloox::TLSBase::hasChannelBinding
virtual bool hasChannelBinding() const
Definition: tlsbase.h:111
gloox
The namespace for the gloox library.
Definition: adhoc.cpp:28
gloox::TLSBase::decrypt
virtual int decrypt(const std::string &data)=0
gloox::SChannel
Definition: tlsschannel.h:39
gloox::TLSDefault::VerifyingServer
@ VerifyingServer
Definition: tlsdefault.h:46
gloox::TLSDefault::Type
Type
Definition: tlsdefault.h:41
gloox::TLSBase
An abstract base class for TLS implementations.
Definition: tlsbase.h:32
gloox::EmptyString
const std::string EmptyString
Definition: gloox.cpp:124
gloox::TLSDefault::setClientCert
virtual void setClientCert(const std::string &clientKey, const std::string &clientCerts)
Definition: tlsdefault.cpp:150
gloox::TLSDefault::setCACerts
virtual void setCACerts(const StringList &cacerts)
Definition: tlsdefault.cpp:139
gloox::TLSBase::handshake
virtual bool handshake()=0
gloox::TLSBase::init
virtual bool init(const std::string &clientKey=EmptyString, const std::string &clientCerts=EmptyString, const StringList &cacerts=StringList())=0
gloox::TLSDefault::encrypt
virtual bool encrypt(const std::string &data)
Definition: tlsdefault.cpp:103
gloox::TLSDefault::AnonymousClient
@ AnonymousClient
Definition: tlsdefault.h:44