gloox  1.1-svn
connectiontls.h
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 #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 
107  void setClientCert( const std::string& clientKey, const std::string& clientCerts )
108  {
109  m_clientKey = clientKey;
110  m_clientCerts = clientCerts;
111  }
112 
125  virtual void setSubject( const std::string& subject ) { m_subject = subject; }
126 
131  void setConnectionImpl( ConnectionBase* connection );
132 
141  void registerTLSHandler( TLSHandler* th ) { m_tlsHandler = th; }
142 
143  // reimplemented from ConnectionBase
144  virtual ConnectionError connect();
145 
146  // reimplemented from ConnectionBase
147  virtual ConnectionError recv( int timeout = -1 );
148 
149  // reimplemented from ConnectionBase
150  virtual bool send( const std::string& data );
151 
152  // reimplemented from ConnectionBase
153  virtual ConnectionError receive();
154 
155  // reimplemented from ConnectionBase
156  virtual void disconnect();
157 
158  // reimplemented from ConnectionBase
159  virtual void cleanup();
160 
161  // reimplemented from ConnectionBase
162  virtual void getStatistics( long int& totalIn, long int& totalOut );
163 
164  // reimplemented from ConnectionDataHandler
165  virtual void handleReceivedData( const ConnectionBase* connection, const std::string& data );
166 
167  // reimplemented from ConnectionDataHandler
168  virtual void handleConnect( const ConnectionBase* connection );
169 
170  // reimplemented from ConnectionDataHandler
171  virtual void handleDisconnect( const ConnectionBase* connection, ConnectionError reason );
172 
173  // reimplemented from ConnectionDataHandler
174  virtual ConnectionBase* newInstance() const;
175 
176  // reimplemented from TLSHandler
177  virtual void handleEncryptedData( const TLSBase*, const std::string& data );
178 
179  // reimplemented from TLSHandler
180  virtual void handleDecryptedData( const TLSBase*, const std::string& data );
181 
182  // reimplemented from TLSHandler
183  virtual void handleHandshakeResult( const TLSBase* base, bool success, CertInfo& certinfo );
184 
185  protected:
191  virtual TLSBase* getTLSBase( TLSHandler* th, const std::string server )
192  {
193  return new TLSDefault( th, server, TLSDefault::VerifyingClient );
194  }
195 
196  ConnectionBase* m_connection;
197  TLSBase* m_tls;
198  TLSHandler* m_tlsHandler;
199  CertInfo m_certInfo;
200  const LogSink& m_log;
201  StringList m_cacerts;
202  std::string m_clientCerts;
203  std::string m_clientKey;
204  std::string m_subject;
205 
206  private:
207  ConnectionTLS& operator=( const ConnectionTLS& );
208 
209  };
210 
211 }
212 
213 #endif // CONNECTIONTLS_H__