gloox  1.0.27
tlsdefault.cpp
1 /*
2  * Copyright (c) 2007-2023 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 }
This class implements an anonymous TLS backend using GnuTLS.
This class implements a TLS backend using GnuTLS.
This class implements (stream) encryption using GnuTLS server-side.
An abstract base class for TLS implementations.
Definition: tlsbase.h:32
virtual bool encrypt(const std::string &data)=0
virtual const CertInfo & fetchTLSInfo() const
Definition: tlsbase.h:130
virtual int decrypt(const std::string &data)=0
virtual const std::string channelBinding() const
Definition: tlsbase.h:117
virtual void setClientCert(const std::string &clientKey, const std::string &clientCerts)=0
virtual bool hasChannelBinding() const
Definition: tlsbase.h:111
virtual bool init(const std::string &clientKey=EmptyString, const std::string &clientCerts=EmptyString, const StringList &cacerts=StringList())=0
virtual bool handshake()=0
virtual void cleanup()=0
virtual bool isSecure() const
Definition: tlsbase.h:105
virtual void setCACerts(const StringList &cacerts)=0
virtual bool encrypt(const std::string &data)
Definition: tlsdefault.cpp:103
virtual const CertInfo & fetchTLSInfo() const
Definition: tlsdefault.cpp:145
virtual void setCACerts(const StringList &cacerts)
Definition: tlsdefault.cpp:139
virtual bool handshake()
Definition: tlsdefault.cpp:119
virtual void cleanup()
Definition: tlsdefault.cpp:113
static int types()
Definition: tlsdefault.cpp:87
virtual void setClientCert(const std::string &clientKey, const std::string &clientCerts)
Definition: tlsdefault.cpp:150
virtual bool init(const std::string &clientKey=EmptyString, const std::string &clientCerts=EmptyString, const StringList &cacerts=StringList())
Definition: tlsdefault.cpp:79
virtual const std::string channelBinding() const
Definition: tlsdefault.cpp:134
virtual ~TLSDefault()
Definition: tlsdefault.cpp:74
virtual bool hasChannelBinding() const
Definition: tlsdefault.cpp:129
virtual bool isSecure() const
Definition: tlsdefault.cpp:124
TLSDefault(TLSHandler *th, const std::string server, Type type=VerifyingClient)
Definition: tlsdefault.cpp:38
virtual int decrypt(const std::string &data)
Definition: tlsdefault.cpp:108
An interface that allows for interacting with TLS implementations derived from TLSBase.
Definition: tlshandler.h:35
The namespace for the gloox library.
Definition: adhoc.cpp:28
std::list< std::string > StringList
Definition: gloox.h:1251
const std::string EmptyString
Definition: gloox.cpp:124