gloox  1.0.9
connectiontls.h
1 /*
2  * Copyright (c) 2007-2013 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 #ifndef CONNECTIONTLS_H__
14 #define CONNECTIONTLS_H__
15 
16 #include "gloox.h"
17 #include "logsink.h"
18 #include "connectionbase.h"
19 #include "tlsdefault.h"
20 #include "connectiondatahandler.h"
21 
22 #include <string>
23 
24 namespace gloox
25 {
26 
51  class GLOOX_API ConnectionTLS : public TLSHandler, public ConnectionBase, public ConnectionDataHandler
52  {
53  public:
62  ConnectionTLS( ConnectionDataHandler* cdh, ConnectionBase* conn, const LogSink& log );
63 
71  ConnectionTLS( ConnectionBase* conn, const LogSink& log );
72 
76  virtual ~ConnectionTLS();
77 
84  void setCACerts( const StringList& cacerts )
85  {
86  m_cacerts = cacerts;
87  }
88 
94  const CertInfo& fetchTLSInfo() const { return m_certInfo; }
95 
108  void setClientCert( const std::string& clientKey, const std::string& clientCerts )
109  {
110  m_clientKey = clientKey;
111  m_clientCerts = clientCerts;
112  }
113 
118  void setConnectionImpl( ConnectionBase* connection );
119 
128  void registerTLSHandler( TLSHandler* th ) { m_tlsHandler = th; }
129 
130  // reimplemented from ConnectionBase
131  virtual ConnectionError connect();
132 
133  // reimplemented from ConnectionBase
134  virtual ConnectionError recv( int timeout = -1 );
135 
136  // reimplemented from ConnectionBase
137  virtual bool send( const std::string& data );
138 
139  // reimplemented from ConnectionBase
140  virtual ConnectionError receive();
141 
142  // reimplemented from ConnectionBase
143  virtual void disconnect();
144 
145  // reimplemented from ConnectionBase
146  virtual void cleanup();
147 
148  // reimplemented from ConnectionBase
149  virtual void getStatistics( long int& totalIn, long int& totalOut );
150 
151  // reimplemented from ConnectionDataHandler
152  virtual void handleReceivedData( const ConnectionBase* connection, const std::string& data );
153 
154  // reimplemented from ConnectionDataHandler
155  virtual void handleConnect( const ConnectionBase* connection );
156 
157  // reimplemented from ConnectionDataHandler
158  virtual void handleDisconnect( const ConnectionBase* connection, ConnectionError reason );
159 
160  // reimplemented from ConnectionDataHandler
161  virtual ConnectionBase* newInstance() const;
162 
163  // reimplemented from TLSHandler
164  virtual void handleEncryptedData( const TLSBase*, const std::string& data );
165 
166  // reimplemented from TLSHandler
167  virtual void handleDecryptedData( const TLSBase*, const std::string& data );
168 
169  // reimplemented from TLSHandler
170  virtual void handleHandshakeResult( const TLSBase* base, bool success, CertInfo& certinfo );
171 
172  protected:
178  virtual TLSBase* getTLSBase( TLSHandler* th, const std::string server )
179  {
180  return new TLSDefault( th, server, TLSDefault::VerifyingClient );
181  }
182 
183  ConnectionBase* m_connection;
184  TLSBase* m_tls;
185  TLSHandler* m_tlsHandler;
186  CertInfo m_certInfo;
187  const LogSink& m_log;
188  StringList m_cacerts;
189  std::string m_clientCerts;
190  std::string m_clientKey;
191 
192  private:
193  ConnectionTLS& operator=( const ConnectionTLS& );
194 
195  };
196 
197 }
198 
199 #endif // CONNECTIONTLS_H__