gloox  1.0.1
tlsopensslbase.h
1 /*
2  Copyright (c) 2009-2012 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 
14 
15 #ifndef TLSOPENSSLBASE_H__
16 #define TLSOPENSSLBASE_H__
17 
18 #include "tlsbase.h"
19 
20 #include "config.h"
21 
22 #ifdef HAVE_OPENSSL
23 
24 #include <openssl/ssl.h>
25 
26 namespace gloox
27 {
28 
36  class OpenSSLBase : public TLSBase
37  {
38  public:
44  OpenSSLBase( TLSHandler* th, const std::string& server = EmptyString );
45 
49  virtual ~OpenSSLBase();
50 
51  // reimplemented from TLSBase
52  virtual bool init( const std::string& clientKey = EmptyString,
53  const std::string& clientCerts = EmptyString,
54  const StringList& cacerts = StringList() );
55 
56  // reimplemented from TLSBase
57  virtual bool encrypt( const std::string& data );
58 
59  // reimplemented from TLSBase
60  virtual int decrypt( const std::string& data );
61 
62  // reimplemented from TLSBase
63  virtual void cleanup();
64 
65  // reimplemented from TLSBase
66  virtual bool handshake();
67 
68  // reimplemented from TLSBase
69  virtual void setCACerts( const StringList& cacerts );
70 
71  // reimplemented from TLSBase
72  virtual void setClientCert( const std::string& clientKey, const std::string& clientCerts );
73 
74  protected:
75  virtual bool setType() = 0;
76  virtual int handshakeFunction() = 0;
77 
78  SSL* m_ssl;
79  SSL_CTX* m_ctx;
80  BIO* m_ibio;
81  BIO* m_nbio;
82 
83  private:
84  void pushFunc();
85  virtual bool privateInit() { return true; }
86 
87  enum TLSOperation
88  {
89  TLSHandshake,
90  TLSWrite,
91  TLSRead
92  };
93 
94  void doTLSOperation( TLSOperation op );
95  int openSSLTime2UnixTime( const char* time_string );
96 
97  std::string m_recvBuffer;
98  std::string m_sendBuffer;
99  char* m_buf;
100  const int m_bufsize;
101 
102  };
103 
104 }
105 
106 #endif // HAVE_OPENSSL
107 
108 #endif // TLSOPENSSLBASE_H__